import {z} from 'zod' export type ConversationMessage = {role: string, content: string}; export type InterviewQueryRequest = {query: string, anonymize?: boolean | undefined, top_k?: number | undefined, conversation_history?: (Array | null) | undefined, model?: (string | null) | undefined}; export type CreateApiV1InterviewsInterviewIdGraphQueryArgs = {interview_id: string, 'X-API-Key'?: (string | null) | undefined, body: InterviewQueryRequest}; export type CreateApiV1InterviewsInterviewIdGraphQueryOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const patternOut = z.object({theme: z.string(), description: z.string(), mention_count: z.number().int(), total_respondents: z.number().int(), percentage: z.number(), departments_affected: z.array(z.string()), supporting_passage_keys: z.array(z.string())}); export const divergenceOut = z.object({topic: z.string(), position_a: z.string(), sources_a: z.array(z.string()), departments_a: z.array(z.string()), position_b: z.string(), sources_b: z.array(z.string()), departments_b: z.array(z.string()), magnitude: z.string()}); export const passageOut = z.object({key: z.string(), text: z.string(), contact_name: z.string().nullable().optional(), department: z.string().nullable().optional(), job_title: z.string().nullable().optional(), seniority_level: z.number().int().nullable().optional(), similarity_score: z.number().optional(), verification: z.string().nullable().optional()}); export const provenanceLinkOut = z.object({claim: z.string(), passages: z.array(passageOut)}); export const verificationOut = z.object({passed: z.boolean(), department_coverage: z.number(), departments_hit: z.array(z.string()), departments_missing: z.array(z.string()), passage_count: z.number().int(), unique_contacts: z.number().int()}); export const interviewQueryResponse = z.object({answer: z.string(), patterns: z.array(patternOut), divergences: z.array(divergenceOut), provenance: z.array(provenanceLinkOut), verification: verificationOut, query_complexity: z.string(), strategies_used: z.array(z.string())}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Query Interview Graph * * POST /v1/interviews/{interview_id}/graph/query */ export const createApiV1InterviewsInterviewIdGraphQuery = async (args: CreateApiV1InterviewsInterviewIdGraphQueryArgs, options: CreateApiV1InterviewsInterviewIdGraphQueryOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/interviews/${encodeURIComponent(String(args.interview_id))}/graph/query${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(interviewQueryResponse, await res.json()) };