| 221 | * Streaming (Server-Sent Events) helpers for Workers |
| 222 | * ------------------------------------------------------------------------------------------------ */ |
| 223 | async function* retoolStream(fullMsg: string, modelId: string) { |
| 224 | const sid = crypto.randomUUID(); |
| 225 | const created = Math.floor(Date.now() / 1000); |
| 226 | yield `data: ${JSON.stringify({ |
| 227 | id: sid, |
| 228 | object: "chat.completion.chunk", |
| 229 | created, |
| 230 | model: modelId, |
| 231 | choices: [{ delta: { role: "assistant" }, index: 0 }], |
| 232 | })}\n\n`; |
| 233 | for (let i = 0; i < fullMsg.length; i += 5) { |
| 234 | const delta = fullMsg.slice(i, i + 5); |
| 235 | yield `data: ${JSON.stringify({ |
| 236 | id: sid, |
| 237 | object: "chat.completion.chunk", |
| 238 | created, |
| 239 | model: modelId, |
| 240 | choices: [{ delta: { content: delta }, index: 0 }], |
| 241 | })}\n\n`; |
| 242 | await new Promise((r) => setTimeout(r, 10)); |
| 243 | } |
| 244 | yield `data: ${JSON.stringify({ |
| 245 | id: sid, |
| 246 | object: "chat.completion.chunk", |
| 247 | created, |
| 248 | model: modelId, |
| 249 | choices: [{ delta: {}, index: 0, finish_reason: "stop" }], |
| 250 | })}\n\ndata: [DONE]\n\n`; |
| 251 | } |
| 252 | async function* errorStream(msg: string, code = 503) { |
| 253 | yield `data: ${JSON.stringify({ error: { message: msg, code } })}\n\n`; |
| 254 | yield "data: [DONE]\n\n"; |