export type ConversationMessage = {role?: ('user' | 'assistant') | undefined, content?: string | undefined, encoded?: boolean | undefined}; export type AIChatRequest = {prompt: string, conversationHistory?: Array | undefined, sessionId?: string | undefined, sessionContinuationIds?: Array | undefined, model?: string | undefined, provider?: ('vertex' | 'aws' | 'mock') | undefined, temperature?: number | undefined, maxTokens?: number | undefined}; export type CreateApiApiAiChatStreamArgs = {body: AIChatRequest}; export type CreateApiApiAiChatStreamOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; /** * Streaming AI Chat * * POST /api/ai/chat/stream */ export const createApiApiAiChatStream = async (args: CreateApiApiAiChatStreamArgs, options?: CreateApiApiAiChatStreamOptions) => { const {baseUrl = "https://api.privacypal.ai", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "text/event-stream") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/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() };