handleStream 处理流式对话
(_ *actor.Context, msg *StreamMsg)
| 313 | |
| 314 | // handleStream 处理流式对话 |
| 315 | func (a *AgentActor) handleStream(_ *actor.Context, msg *StreamMsg) { |
| 316 | execCtx, cancel := a.withCancellation(msg.Ctx) |
| 317 | defer cancel() |
| 318 | |
| 319 | go func() { |
| 320 | reader := a.agent.Stream(execCtx, msg.Text) |
| 321 | |
| 322 | for { |
| 323 | event, err := reader.Recv() |
| 324 | if err != nil { |
| 325 | // EOF 或错误 |
| 326 | if msg.EventCh != nil { |
| 327 | msg.EventCh <- &StreamEventMsg{ |
| 328 | Type: "done", |
| 329 | Error: err, |
| 330 | } |
| 331 | close(msg.EventCh) |
| 332 | } |
| 333 | return |
| 334 | } |
| 335 | |
| 336 | if event != nil && msg.EventCh != nil { |
| 337 | msg.EventCh <- &StreamEventMsg{ |
| 338 | Type: "event", |
| 339 | Content: event, |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | }() |
| 344 | } |
| 345 | |
| 346 | // handleToolCall 处理工具调用 |
| 347 | func (a *AgentActor) handleToolCall(ctx *actor.Context, msg *ToolCallMsg) { |
no test coverage detected