import {z} from 'zod' export type MessageIn = {content?: (string | null) | undefined, chat_id?: (number | null) | undefined, updated_at?: (string | null) | undefined, project_id?: (number | null) | undefined, model?: (string | null) | undefined, temperature?: (number | null) | undefined, similarity_top_k?: (number | null) | undefined, system_prompt?: (string | null) | undefined, assistant_id?: (number | null) | undefined, private?: (boolean | null) | undefined, form_data?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiMessagesArgs = {cookie_name?: (string | null) | undefined, body: MessageIn}; export type CreateApiMessagesOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const message = z.object({state: z.string().nullable().optional(), state_reason: z.string().nullable().optional(), state_changed_at: z.string().nullable().optional(), id: z.number().int().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), creator_user_id: z.number().int(), chat_id: z.number().int(), role: z.string(), content: z.string(), sent_user_msg: z.string().nullable(), parent_id: z.number().int().nullable(), completed: z.boolean().optional(), error: z.string().nullable(), hint: z.string().nullable(), llm_catalog_id: z.number().int().nullable().optional(), llm_settings_id: z.number().int().nullable().optional(), usage: z.record(z.string(), z.unknown()).nullable().optional(), celery_task_id: z.string().nullable().optional()}); 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 message * * POST /messages/ */ export const createApiMessages = async (args: CreateApiMessagesArgs, options?: CreateApiMessagesOptions) => { const {baseUrl = "https://api.your-domain.com", 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 (args.cookie_name != null) query.append("cookie_name", String(args.cookie_name)) if (APIKeyHeader !== undefined) headers.set("X-API-KEY", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/messages/${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(message, await res.json()) };