| 17 | } |
| 18 | |
| 19 | export async function* streamChat( |
| 20 | messages: Pick<Message, "role" | "content">[], |
| 21 | model: string, |
| 22 | signal?: AbortSignal |
| 23 | ): AsyncGenerator<StreamChunk> { |
| 24 | const response = await fetch("/api/chat", { |
| 25 | method: "POST", |
| 26 | headers: { "Content-Type": "application/json" }, |
| 27 | body: JSON.stringify({ messages, model, stream: true }), |
| 28 | signal, |
| 29 | }); |
| 30 | |
| 31 | if (!response.ok) { |
| 32 | const err = await response.text(); |
| 33 | yield { type: "error", error: err }; |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | const reader = response.body?.getReader(); |
| 38 | if (!reader) { |
| 39 | yield { type: "error", error: "No response body" }; |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | const decoder = new TextDecoder(); |
| 44 | let buffer = ""; |
| 45 | |
| 46 | try { |
| 47 | while (true) { |
| 48 | const { done, value } = await reader.read(); |
| 49 | if (done) break; |
| 50 | |
| 51 | buffer += decoder.decode(value, { stream: true }); |
| 52 | const lines = buffer.split("\n"); |
| 53 | buffer = lines.pop() ?? ""; |
| 54 | |
| 55 | for (const line of lines) { |
| 56 | if (line.startsWith("data: ")) { |
| 57 | const data = line.slice(6).trim(); |
| 58 | if (data === "[DONE]") { |
| 59 | yield { type: "done" }; |
| 60 | return; |
| 61 | } |
| 62 | try { |
| 63 | const chunk = JSON.parse(data) as StreamChunk; |
| 64 | yield chunk; |
| 65 | } catch { |
| 66 | // skip malformed chunks |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } finally { |
| 72 | reader.releaseLock(); |
| 73 | } |
| 74 | |
| 75 | yield { type: "done" }; |
| 76 | } |