import {z} from 'zod' export type CreateDebugChatBody = {clientEnv: string, asrLangCode?: string | undefined, ttsLangCode?: string | undefined, conversationId?: string | undefined, variantId?: string | undefined, channel?: string | undefined, integrationAttributes?: Record | undefined}; export type CreateApiV1AgentsAgentIdDebugChatArgs = {body: CreateDebugChatBody}; export type CreateApiV1AgentsAgentIdDebugChatOptions = {baseUrl?: string | undefined, polyApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const chatMetadataResponse = z.object({state: z.object({}), currentNode: z.string(), nodeTrace: z.array(z.string()), metrics: z.object({}), functionEvents: z.array(z.unknown()), handoffDestination: z.string().optional(), retrievedTopics: z.array(z.string()).optional(), retrievedSources: z.array(z.string()).optional(), citedTopic: z.string().optional(), inFlow: z.string().optional(), inStep: z.string().optional(), llmRequests: z.array(z.object({})).optional(), asrLangCode: z.string().optional(), ttsLangCode: z.string().optional()}); export const debugChatResponse = z.object({conversationId: z.string(), userInput: z.string(), response: z.string(), metadata: chatMetadataResponse, conversationEnded: z.boolean(), delayedResponse: z.boolean()}); 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 a new debug chat session * * POST /v1/agents/{agentId}/debug-chat */ export const createApiV1AgentsAgentIdDebugChat = async (args: CreateApiV1AgentsAgentIdDebugChatArgs, options?: CreateApiV1AgentsAgentIdDebugChatOptions) => { const {baseUrl = "https://api.us.poly.ai", fetch: fetchFn = fetch, polyApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (polyApiKey !== undefined) headers.set("X-API-KEY", polyApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agents/${encodeURIComponent(String(args.agentId))}/debug-chat${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(debugChatResponse, await res.json()) };