( request: APIRequestContext, baseURL: string, options: SummarizeOptions, )
| 298 | * Call the summarize API with streaming |
| 299 | */ |
| 300 | export async function callSummarizeAPIStreaming( |
| 301 | request: APIRequestContext, |
| 302 | baseURL: string, |
| 303 | options: SummarizeOptions, |
| 304 | ): Promise<{ |
| 305 | summary: string |
| 306 | provider: string |
| 307 | model: string |
| 308 | chunkCount: number |
| 309 | }> { |
| 310 | const response = await request.post(`${baseURL}/api/summarize`, { |
| 311 | data: { |
| 312 | text: options.text, |
| 313 | provider: options.provider, |
| 314 | model: options.model, |
| 315 | maxLength: options.maxLength || 100, |
| 316 | style: options.style || 'concise', |
| 317 | stream: true, |
| 318 | }, |
| 319 | }) |
| 320 | |
| 321 | if (!response.ok()) { |
| 322 | const errorBody = await response.text() |
| 323 | throw new Error( |
| 324 | `Summarize API streaming failed: ${response.status()} - ${errorBody}`, |
| 325 | ) |
| 326 | } |
| 327 | |
| 328 | // Parse SSE response |
| 329 | const text = await response.text() |
| 330 | const lines = text.split('\n') |
| 331 | |
| 332 | let summary = '' |
| 333 | let provider = '' |
| 334 | let model = '' |
| 335 | let chunkCount = 0 |
| 336 | |
| 337 | for (const line of lines) { |
| 338 | if (line.startsWith('data: ')) { |
| 339 | const data = line.slice(6) |
| 340 | if (data === '[DONE]') continue |
| 341 | |
| 342 | try { |
| 343 | const parsed = JSON.parse(data) |
| 344 | if (parsed.type === 'error') { |
| 345 | throw new Error(parsed.error) |
| 346 | } |
| 347 | if (parsed.type === 'TEXT_MESSAGE_CONTENT') { |
| 348 | chunkCount++ |
| 349 | if (parsed.delta) { |
| 350 | summary += parsed.delta |
| 351 | } else if (parsed.content) { |
| 352 | summary = parsed.content |
| 353 | } |
| 354 | provider = parsed.provider || provider |
| 355 | model = parsed.model || model |
| 356 | } |
| 357 | } catch { |
no test coverage detected