Chat sends a message to an agent and returns immediately. Responses come via SSE.
(name, message string)
| 397 | |
| 398 | // Chat sends a message to an agent and returns immediately. Responses come via SSE. |
| 399 | func (s *AgentPoolService) Chat(name, message string) (string, error) { |
| 400 | ag := s.localAGI.pool.GetAgent(name) |
| 401 | if ag == nil { |
| 402 | return "", fmt.Errorf("%w: %s", ErrAgentNotFound, name) |
| 403 | } |
| 404 | manager := s.localAGI.pool.GetManager(name) |
| 405 | if manager == nil { |
| 406 | return "", fmt.Errorf("SSE manager not found for agent: %s", name) |
| 407 | } |
| 408 | |
| 409 | messageID := fmt.Sprintf("%d", time.Now().UnixNano()) |
| 410 | |
| 411 | // Send user message via SSE |
| 412 | userMsg, _ := json.Marshal(map[string]any{ |
| 413 | "id": messageID + "-user", |
| 414 | "sender": "user", |
| 415 | "content": message, |
| 416 | "timestamp": time.Now().Format(time.RFC3339), |
| 417 | }) |
| 418 | manager.Send(sse.NewMessage(string(userMsg)).WithEvent("json_message")) |
| 419 | |
| 420 | // Send processing status |
| 421 | statusMsg, _ := json.Marshal(map[string]any{ |
| 422 | "status": "processing", |
| 423 | "timestamp": time.Now().Format(time.RFC3339), |
| 424 | }) |
| 425 | manager.Send(sse.NewMessage(string(statusMsg)).WithEvent("json_message_status")) |
| 426 | |
| 427 | // Process asynchronously |
| 428 | go func() { |
| 429 | response := ag.Ask(coreTypes.WithText(message)) |
| 430 | |
| 431 | if response == nil { |
| 432 | errMsg, _ := json.Marshal(map[string]any{ |
| 433 | "error": "agent request failed or was cancelled", |
| 434 | "timestamp": time.Now().Format(time.RFC3339), |
| 435 | }) |
| 436 | manager.Send(sse.NewMessage(string(errMsg)).WithEvent("json_error")) |
| 437 | } else if response.Error != nil { |
| 438 | errMsg, _ := json.Marshal(map[string]any{ |
| 439 | "error": response.Error.Error(), |
| 440 | "timestamp": time.Now().Format(time.RFC3339), |
| 441 | }) |
| 442 | manager.Send(sse.NewMessage(string(errMsg)).WithEvent("json_error")) |
| 443 | } else { |
| 444 | // Collect metadata from all action states |
| 445 | metadata := map[string]any{} |
| 446 | for _, state := range response.State { |
| 447 | for k, v := range state.Metadata { |
| 448 | if existing, ok := metadata[k]; ok { |
| 449 | if existList, ok := existing.([]string); ok { |
| 450 | if newList, ok := v.([]string); ok { |
| 451 | metadata[k] = append(existList, newList...) |
| 452 | continue |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | metadata[k] = v |
nothing calls this directly
no test coverage detected