import {z} from 'zod' export type InputArgType = 'default' | 'pinned'; export type InputArgConfig = {value: unknown, type?: InputArgType | undefined}; export type TestSessionFeatureInput = {identity_resolution?: boolean | undefined, memory?: boolean | undefined, summarization?: boolean | undefined, extraction?: boolean | undefined, evaluations?: boolean | undefined, knowledge_gap_analysis?: boolean | undefined}; export type TestChatStartRequest = {agent_id?: (string | null) | undefined, assistant_revision_id?: (string | null) | undefined, team_id?: (string | null) | undefined, team_revision_id?: (string | null) | undefined, tool_input_overrides?: ({[key: string]: {[key: string]: InputArgConfig}} | null) | undefined, context_variables?: ({[key: string]: unknown} | null) | undefined, test_features?: (TestSessionFeatureInput | null) | undefined}; export type CreateApiV1ConversationsTestChatArgs = {body: TestChatStartRequest}; export type CreateApiV1ConversationsTestChatOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const testSessionFeatures = z.object({version: z.literal(1).optional(), identity_resolution: z.boolean(), memory: z.boolean(), summarization: z.boolean(), extraction: z.boolean(), evaluations: z.boolean(), knowledge_gap_analysis: z.boolean()}); export const testChatStartResponse = z.object({session_id: z.string(), end_user_id: z.string(), test_features: testSessionFeatures}); 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 }; /** * Mint a fresh test-type v2 conversation bound to an agent or team * * POST /v1/conversations/test-chat */ export const createApiV1ConversationsTestChat = async (args: CreateApiV1ConversationsTestChatArgs, options: CreateApiV1ConversationsTestChatOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/conversations/test-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(testChatStartResponse, await res.json()) };