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 ConversationCreate = {end_user_id?: (string | null) | undefined, org_external_end_user_id?: (string | null) | undefined, assistant_id?: (string | null) | undefined, assistant_revision_id?: (string | null) | undefined, team_id?: (string | null) | undefined, team_revision_id?: (string | null) | undefined, session_type?: ('live' | 'test' | 'simulation') | undefined, external_id?: (string | null) | undefined, subject?: (string | null) | undefined, metadata?: ({[key: string]: unknown} | 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 CreateApiV1ConversationsArgs = {body: ConversationCreate}; export type CreateApiV1ConversationsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const inputArgType = z.enum(["default", "pinned"]); export const inputArgConfig = z.object({value: z.unknown(), type: inputArgType.optional()}); 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 conversationResponse = z.object({id: z.string(), organization_id: z.string(), end_user_id: z.string(), channel: z.string(), surface: z.string(), status: z.string(), session_type: z.string(), assistant_id: z.string().nullable().optional(), assistant_revision_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), team_revision_id: z.string().nullable().optional(), external_id: z.string().nullable().optional(), subject: z.string().nullable().optional(), tool_input_overrides: z.record(z.string(), z.record(z.string(), inputArgConfig)).nullable().optional(), test_features: testSessionFeatures.nullable().optional(), created_at: z.string(), updated_at: 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 }; /** * Get or create a v2 conversation * * POST /v1/conversations */ export const createApiV1Conversations = async (args: CreateApiV1ConversationsArgs, options: CreateApiV1ConversationsOptions) => { 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${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(conversationResponse, await res.json()) };