import {z} from 'zod' export type PromptVariablesConfiguration = {mode?: ('inline' | 'call_context') | undefined, call_context_note?: (string | null) | undefined, include_weekday?: boolean | undefined}; export type PromptReplayRequest = {prompt_variables?: PromptVariablesConfiguration | undefined, limit?: number | undefined, max_turns?: number | undefined}; export type CreateApiApiV1WorkflowWorkflowIdPromptReplayArgs = {workflow_id: number, authorization?: (string | null) | undefined, 'X-API-Key'?: (string | null) | undefined, body: PromptReplayRequest}; export type CreateApiApiV1WorkflowWorkflowIdPromptReplayOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const promptReplayResponse = z.object({turns: z.array(z.record(z.string(), z.unknown())), valid_turns: z.number().int(), failed_turns: z.number().int(), action_agreement: z.number().nullable(), inline_noise_agreement: z.number().nullable(), call_context_noise_agreement: z.number().nullable(), notices: z.array(z.string()), semantic_agreement: z.record(z.string(), z.record(z.string(), z.union([z.number(), z.number().int()]).nullable())), sample_metrics: z.record(z.string(), z.record(z.string(), z.union([z.number(), z.number().int()]).nullable()))}); 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 }; /** * Replay Workflow Prompts * * POST /api/v1/workflow/{workflow_id}/prompt-replay */ export const createApiApiV1WorkflowWorkflowIdPromptReplay = async (args: CreateApiApiV1WorkflowWorkflowIdPromptReplayArgs, options?: CreateApiApiV1WorkflowWorkflowIdPromptReplayOptions) => { const {baseUrl = "https://app.dograh.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/workflow/${encodeURIComponent(String(args.workflow_id))}/prompt-replay${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(promptReplayResponse, await res.json()) };