import {z} from 'zod' export type GetApiApiOrgIdAiChatsSessionIdArgs = {org_id: string, session_id: string, known_seq?: number | undefined, known_version?: string | undefined, limit?: number | undefined}; export type GetApiApiOrgIdAiChatsSessionIdOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiOrgIdAiChatsSessionIdResponse = z.object({agent_type: z.string(), created_at: z.number().int(), forked_from_share: z.string().nullable().optional(), last_committed_seq: z.number().int(), session_id: z.string(), title: z.string(), updated_at: z.number().int(), active_turn: z.boolean(), has_more: z.boolean(), not_modified: z.boolean(), partial_from_seq: z.number().int().nullable().optional(), state_version: z.string(), turns: z.array(z.object({})).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 }; /** * Get one AI conversation * * GET /api/{org_id}/ai/chats/{session_id} */ export const getApiApiOrgIdAiChatsSessionId = async (args: GetApiApiOrgIdAiChatsSessionIdArgs, options: GetApiApiOrgIdAiChatsSessionIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.known_seq != null) query.append("known_seq", String(args.known_seq)) if (args.known_version != null) query.append("known_version", String(args.known_version)) if (args.limit != null) query.append("limit", String(args.limit)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/ai/chats/${encodeURIComponent(String(args.session_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiOrgIdAiChatsSessionIdResponse, await res.json()) };