(
parsed: Record<string, unknown>,
req: http.IncomingMessage,
res: http.ServerResponse,
)
| 252 | // ---- Private: streaming handler ---- |
| 253 | |
| 254 | private async handleStreamingMessage( |
| 255 | parsed: Record<string, unknown>, |
| 256 | req: http.IncomingMessage, |
| 257 | res: http.ServerResponse, |
| 258 | ): Promise<void> { |
| 259 | const params = parsed.params as Record<string, unknown> | undefined; |
| 260 | const id = parsed.id as string | number; |
| 261 | const text = extractText(params); |
| 262 | const entry = findStreamingMatch(text, this.agents); |
| 263 | |
| 264 | if (!entry) { |
| 265 | res.writeHead(200, { |
| 266 | "Content-Type": "application/json", |
| 267 | "A2A-Version": "1.0", |
| 268 | }); |
| 269 | res.end( |
| 270 | JSON.stringify({ |
| 271 | jsonrpc: "2.0", |
| 272 | id, |
| 273 | error: { code: -32000, message: "No matching pattern for message" }, |
| 274 | }), |
| 275 | ); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | // Create task for the streaming response |
| 280 | const taskId = generateId("task"); |
| 281 | const contextId = generateId("ctx"); |
| 282 | const userParts: A2APart[] = params?.message |
| 283 | ? (((params.message as Record<string, unknown>).parts as A2APart[]) ?? [{ text }]) |
| 284 | : [{ text }]; |
| 285 | |
| 286 | const task: A2ATask = { |
| 287 | id: taskId, |
| 288 | contextId, |
| 289 | status: { state: "TASK_STATE_WORKING", timestamp: new Date().toISOString() }, |
| 290 | artifacts: [], |
| 291 | history: [ |
| 292 | { |
| 293 | messageId: generateId("msg"), |
| 294 | role: "ROLE_USER", |
| 295 | parts: userParts, |
| 296 | }, |
| 297 | ], |
| 298 | }; |
| 299 | this.tasks.set(taskId, task); |
| 300 | |
| 301 | // Write SSE response |
| 302 | res.writeHead(200, { |
| 303 | "Content-Type": "text/event-stream", |
| 304 | "Cache-Control": "no-cache", |
| 305 | Connection: "keep-alive", |
| 306 | "A2A-Version": "1.0", |
| 307 | }); |
| 308 | |
| 309 | const delayMs = entry.delayMs ?? 0; |
| 310 | |
| 311 | for (const event of entry.events) { |
no test coverage detected