import {z} from 'zod' export type UserMessage = {role: 'user', content: string}; export type RecordType = 'documentation' | 'site' | 'discourse_post' | 'github_issue' | 'github_discussion' | 'stackoverflow_question' | 'discord_forum_post' | 'discord_message' | 'custom_question_answer' | string; export type Record = {type: RecordType, url?: (string | null) | undefined, title?: (string | null) | undefined, description?: (string | null) | undefined, breadcrumbs?: (Array | null) | undefined}; export type Citation = {number?: number | undefined, record: Record, hit_url?: (string | null) | undefined}; export type RecordsCited = {citations: Array}; export type AssistantMessage = {role: 'assistant', content: string, records_cited?: RecordsCited | undefined}; export type Message = UserMessage | AssistantMessage; export type ContinueChatSessionWithChatResultInput = {integration_id: string, message: Message, stream?: boolean | undefined}; export type CreateApiV0ChatSessionsChatSessionIdChatResultsArgs = {chat_session_id: string, body: ContinueChatSessionWithChatResultInput}; export type CreateApiV0ChatSessionsChatSessionIdChatResultsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const recordType = z.union([z.enum(["documentation", "site", "discourse_post", "github_issue", "github_discussion", "stackoverflow_question", "discord_forum_post", "discord_message", "custom_question_answer"]), z.string()]); export const record = z.object({type: recordType, url: z.string().nullable().optional(), title: z.string().nullable().optional(), description: z.string().nullable().optional(), breadcrumbs: z.array(z.string()).nullable().optional()}); export const citation = z.object({number: z.number().int().optional(), record: record, hit_url: z.string().nullable().optional()}); export const recordsCited = z.object({citations: z.array(citation)}); export const assistantMessage = z.object({role: z.literal("assistant"), content: z.string(), records_cited: recordsCited.optional()}); export const chatResult = z.object({chat_session_id: z.string(), message: assistantMessage}); 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 }; /** * Continue Chat Session * * POST /v0/chat_sessions/{chat_session_id}/chat_results */ export const createApiV0ChatSessionsChatSessionIdChatResults = async (args: CreateApiV0ChatSessionsChatSessionIdChatResultsArgs, options?: CreateApiV0ChatSessionsChatSessionIdChatResultsOptions) => { const {baseUrl = "https://api.inkeep.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v0/chat_sessions/${encodeURIComponent(String(args.chat_session_id))}/chat_results${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(chatResult, await res.json()) };