(
messages: { role: string; content: string }[],
onPart: (part: StreamPart) => void,
signal: AbortSignal,
)
| 43 | } |
| 44 | |
| 45 | async function streamChat( |
| 46 | messages: { role: string; content: string }[], |
| 47 | onPart: (part: StreamPart) => void, |
| 48 | signal: AbortSignal, |
| 49 | ): Promise<void> { |
| 50 | const res = await fetch("/api/chat", { |
| 51 | method: "POST", |
| 52 | headers: { "Content-Type": "application/json" }, |
| 53 | body: JSON.stringify({ messages }), |
| 54 | signal, |
| 55 | }); |
| 56 | |
| 57 | if (!res.ok) { |
| 58 | let detail = res.statusText; |
| 59 | try { |
| 60 | const json = (await res.json()) as { error?: string }; |
| 61 | detail = json.error ?? detail; |
| 62 | } catch { |
| 63 | detail = (await res.text()) || detail; |
| 64 | } |
| 65 | throw new Error(detail); |
| 66 | } |
| 67 | |
| 68 | if (!res.body) { |
| 69 | throw new Error("No response stream"); |
| 70 | } |
| 71 | |
| 72 | await readStreamParts(res.body, onPart, signal); |
| 73 | } |
| 74 | |
| 75 | function newMessage( |
| 76 | role: ChatMessage["role"], |
no test coverage detected