handleChat 处理同步对话
(ctx *actor.Context, msg *ChatMsg)
| 280 | |
| 281 | // handleChat 处理同步对话 |
| 282 | func (a *AgentActor) handleChat(ctx *actor.Context, msg *ChatMsg) { |
| 283 | execCtx, cancel := a.withCancellation(msg.Ctx) |
| 284 | defer cancel() |
| 285 | |
| 286 | go func() { |
| 287 | result, err := a.agent.Chat(execCtx, msg.Text) |
| 288 | |
| 289 | response := &ChatResultMsg{ |
| 290 | Result: result, |
| 291 | Error: err, |
| 292 | } |
| 293 | |
| 294 | if err != nil && !errors.Is(err, context.Canceled) { |
| 295 | a.recordError(err) |
| 296 | } |
| 297 | |
| 298 | // 通过 channel 返回结果 |
| 299 | if msg.ReplyTo != nil { |
| 300 | select { |
| 301 | case msg.ReplyTo <- response: |
| 302 | default: |
| 303 | agentLog.Warn(context.Background(), "chat reply channel full or closed", map[string]any{"agent_id": a.agent.ID()}) |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // 也通过 Actor 消息回复 |
| 308 | if ctx.Sender != nil { |
| 309 | ctx.Reply(response) |
| 310 | } |
| 311 | }() |
| 312 | } |
| 313 | |
| 314 | // handleStream 处理流式对话 |
| 315 | func (a *AgentActor) handleStream(_ *actor.Context, msg *StreamMsg) { |
no test coverage detected