22
33import { lamaticClient } from "@/lib/lamatic-client"
44import { config } from "../orchestrate.js"
5+ import { z } from "zod"
56
67export async function generateQuestions (
78 jobTitle : string ,
@@ -27,16 +28,27 @@ export async function generateQuestions(
2728 jobDesc
2829 }
2930
31+ const inputSchema = z . object ( {
32+ jobTitle : z . string ( ) . min ( 1 , "Job title is required" ) ,
33+ yearsOfExp : z . number ( ) . min ( 0 , "Years of experience must be a valid number" ) ,
34+ jobDesc : z . string ( ) . optional ( )
35+ } )
36+
37+ inputSchema . parse ( inputs )
38+
3039 // console.log(`[v0] Executing flow: ${flow.workflowId}`)
3140
3241 const resData = await lamaticClient . executeFlow ( flow . workflowId , inputs )
3342 // console.log("[v0] Flow execution completed")
3443
35- const questions = resData ?. result ?. data
44+ const responseSchema = z . object ( {
45+ result : z . object ( {
46+ data : z . array ( z . string ( ) ) . min ( 1 , "No questions found in response" )
47+ } )
48+ } )
3649
37- if ( ! questions || ! Array . isArray ( questions ) || questions . length === 0 ) {
38- throw new Error ( "No questions found in response" )
39- }
50+ const validatedResponse = responseSchema . parse ( resData )
51+ const questions = validatedResponse . result . data
4052
4153 return {
4254 success : true ,
@@ -46,7 +58,9 @@ export async function generateQuestions(
4658 console . error ( "[v0] Generation error:" , error )
4759
4860 let errorMessage = "Unknown error occurred"
49- if ( error instanceof Error ) {
61+ if ( error instanceof z . ZodError ) {
62+ errorMessage = "Validation error: Invalid data format."
63+ } else if ( error instanceof Error ) {
5064 errorMessage = error . message
5165 if ( error . message . includes ( "fetch failed" ) ) {
5266 errorMessage =
@@ -84,30 +98,48 @@ export async function evaluateAnswers(
8498 candidateResponses
8599 }
86100
101+ const inputSchema = z . object ( {
102+ candidateResponses : z . array (
103+ z . object ( {
104+ question : z . string ( ) ,
105+ answers : z . string ( )
106+ } )
107+ ) . min ( 1 , "At least one candidate response is required" )
108+ } )
109+
110+ inputSchema . parse ( inputs )
111+
87112 // console.log(`[v0] Executing flow: ${flow.workflowId}`)
88113
89114 const resData = await lamaticClient . executeFlow ( flow . workflowId , inputs )
90115 console . log ( "[v0] Flow execution completed" )
91116
92- const result = resData ?. result
117+ const responseSchema = z . object ( {
118+ result : z . object ( {
119+ positives : z . array ( z . string ( ) ) . optional ( ) . default ( [ ] ) ,
120+ negatives : z . array ( z . string ( ) ) . optional ( ) . default ( [ ] ) ,
121+ rating : z . number ( )
122+ } )
123+ } )
93124
94- if ( ! result || typeof result . rating !== 'number' ) {
95- throw new Error ( "No feedback found in response" )
96- }
125+ const validatedResponse = responseSchema . parse ( resData )
126+ const result = validatedResponse . result
97127
98128 return {
99129 success : true ,
100130 feedback : {
101- positives : result . positives || [ ] ,
102- negatives : result . negatives || [ ] ,
131+ positives : result . positives ,
132+ negatives : result . negatives ,
103133 rating : result . rating
104134 } ,
105135 }
106136 } catch ( error ) {
107137 console . error ( "[v0] Evaluation error:" , error )
108138
109139 let errorMessage = "Unknown error occurred"
110- if ( error instanceof Error ) {
140+ if ( error instanceof z . ZodError ) {
141+ errorMessage = "Validation error: Invalid data format."
142+ } else if ( error instanceof Error ) {
111143 errorMessage = error . message
112144 if ( error . message . includes ( "fetch failed" ) ) {
113145 errorMessage =
0 commit comments