export type ImageAttachment = {data: string, filename?: (string | null) | undefined, mime_type: string}; export type CreateApiApiOrgIdAiChatStreamArgs = {org_id: string, body: {confirm_timeout_secs?: (number | null) | undefined, context?: Record | undefined, images?: (Array | null) | undefined, messages: Array>, model?: string | undefined, provider?: string | undefined}}; export type CreateApiApiOrgIdAiChatStreamOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; /** * Generate streaming AI chat response * * POST /api/{org_id}/ai/chat_stream */ export const createApiApiOrgIdAiChatStream = async (args: CreateApiApiOrgIdAiChatStreamArgs, options: CreateApiApiOrgIdAiChatStreamOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "text/event-stream") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/ai/chat_stream${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 res.text() };