import {z} from 'zod' export type PreviewPlanRequest = {intent: string}; export type CreateApiApiV1VoiceAgentsVoiceAgentIdPreviewPlanArgs = {voice_agent_id: string, body: PreviewPlanRequest}; export type CreateApiApiV1VoiceAgentsVoiceAgentIdPreviewPlanOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const subjectRead = z.object({kind: z.enum(["agent", "group"]), id: z.string()}); export const flywheelRunStatus = z.enum(["draft", "aggregating", "plan_ready", "applied", "sim_running", "sim_passed", "sim_failed", "promoted", "rolled_back", "dismissed"]); export const editBaseInstructions = z.object({kind: z.literal("edit_base_instructions"), new_instructions: z.string(), addresses_finding_ids: z.array(z.string()), rationale: z.string()}); export const editScenarioInstruction = z.object({kind: z.literal("edit_scenario_instruction"), scenario_id: z.string(), field: z.enum(["pre_execution_instruction", "post_execution_instruction", "condition"]), new_text: z.string(), addresses_finding_ids: z.array(z.string()), rationale: z.string()}); export const adjustPreset = z.object({kind: z.literal("adjust_preset"), scenario_id: z.string(), preset_updates: z.record(z.string(), z.unknown()), addresses_finding_ids: z.array(z.string()), rationale: z.string()}); export const promptEditPlan = z.object({edits: z.array(z.discriminatedUnion("kind", [editBaseInstructions, editScenarioInstruction, adjustPreset])), overall_rationale: z.string()}); export const planEntry = z.object({voice_agent_id: z.string(), plan: promptEditPlan}); export const previewPlanResponse = z.object({run_id: z.string(), conversation_id: z.string(), subject: subjectRead, status: flywheelRunStatus, plans: z.array(planEntry), findings_cited: 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 }; /** * Preview Plan Agent Route * * POST /api/v1/voice-agents/{voice_agent_id}/preview-plan */ export const createApiApiV1VoiceAgentsVoiceAgentIdPreviewPlan = async (args: CreateApiApiV1VoiceAgentsVoiceAgentIdPreviewPlanArgs, options: CreateApiApiV1VoiceAgentsVoiceAgentIdPreviewPlanOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/voice-agents/${encodeURIComponent(String(args.voice_agent_id))}/preview-plan${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(previewPlanResponse, await res.json()) };