import {z} from 'zod' export type CreateApiPersonasFromTranscriptArgs = {projectId?: string | undefined, body: {projectReference?: string | undefined, sessionId?: string | undefined, transcriptContent?: ({messages: Array<{source: 'user' | 'bot' | 'agent' | 'suggestion', content: string, timestamp: string}>}) | undefined, flowId?: string | undefined, numberOfSuccessCriteria?: number | undefined, analysisOptions?: ({maxTurns?: number | undefined, focusOnUserGoals?: boolean | undefined}) | undefined}}; export type CreateApiPersonasFromTranscriptOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPersonasFromTranscriptResponse = z.object({success: z.boolean(), personaPackage: z.object({simulationName: z.string(), personaName: z.string(), personaDescription: z.string(), mission: z.string(), successCriteria: z.array(z.object({name: z.string(), text: z.string()}))}), metadata: z.object({transcriptSummary: z.string(), analysisTimestamp: z.string(), warnings: z.array(z.string()).optional(), turnCount: z.number().int().optional(), extractedIntents: z.array(z.string()).optional(), messages: z.array(z.object({source: z.enum(["user", "bot", "agent", "suggestion"]), content: z.string(), timestamp: 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 }; /** * Create scenario from transcript * * POST /personas/from-transcript * * Also accepts auth: apiKeyAuth */ export const createApiPersonasFromTranscript = async (args: CreateApiPersonasFromTranscriptArgs, options?: CreateApiPersonasFromTranscriptOptions) => { const {baseUrl = "https://api-trial.cognigy.ai/testing", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.projectId != null) query.append("projectId", String(args.projectId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/personas/from-transcript${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(createApiPersonasFromTranscriptResponse, await res.json()) };