Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435import type { AnalyzeRequest, StreamEvent } from "./types";
export async function fetchModels(baseUrl: string, apiKey: string): Promise<string[]> { const res = await fetch("/api/models", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ base_url: baseUrl, api_key: apiKey }), }); if (!res.ok) throw new Error(`models: HTTP ${res.status}`); return (await res.json()).models;}
export async function* analyzeStream(req: AnalyzeRequest): AsyncGenerator<StreamEvent> { const res = await fetch("/api/analyze", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(req), }); if (!res.ok || !res.body) throw new Error(`analyze: HTTP ${res.status} ${await res.text()}`); const reader = res.body.getReader(); const decoder = new TextDecoder(); let buf = ""; for (;;) { const { done, value } = await reader.read(); if (done) break; buf += decoder.decode(value, { stream: true }); let nl; while ((nl = buf.indexOf("\n")) >= 0) { const line = buf.slice(0, nl).trim(); buf = buf.slice(nl + 1); if (line) yield JSON.parse(line) as StreamEvent; } }}