(command string)
| 727 | } |
| 728 | |
| 729 | func (m *appModel) handleAgentCommand(command string) (tea.Model, tea.Cmd) { |
| 730 | ctx := m.ctx() |
| 731 | |
| 732 | // Inspect the command before resolving so we can detect /commands that |
| 733 | // switch to a sub-agent. For those, we switch first and only then send |
| 734 | // the resolved message — otherwise the message would be processed by |
| 735 | // the previous agent. |
| 736 | cmd, _, ok := m.application.LookupCommand(ctx, command) |
| 737 | |
| 738 | // URL commands open the configured URL in the browser instead of sending |
| 739 | // a prompt to the agent. |
| 740 | if ok && cmd.URL != "" { |
| 741 | return m, core.CmdHandler(messages.OpenURLMsg{URL: m.expandURLPlaceholders(cmd.URL)}) |
| 742 | } |
| 743 | |
| 744 | resolved := m.application.ResolveCommand(ctx, command) |
| 745 | |
| 746 | var cmds []tea.Cmd |
| 747 | switchSucceeded := true |
| 748 | if ok && cmd.Agent != "" && cmd.Agent != m.sessionState.CurrentAgentName() { |
| 749 | // Attempt to switch agents. If the switch fails, handleSwitchAgent |
| 750 | // returns an error notification command. We check if the agent actually |
| 751 | // changed to determine success, rather than relying on the command type. |
| 752 | prevAgent := m.sessionState.CurrentAgentName() |
| 753 | switched, switchCmd := m.handleSwitchAgent(cmd.Agent) |
| 754 | var ok bool |
| 755 | if m, ok = switched.(*appModel); !ok { |
| 756 | // This should never happen, but if it does, log and continue with the original model |
| 757 | slog.WarnContext(ctx, "handleSwitchAgent returned unexpected type", "type", fmt.Sprintf("%T", switched)) |
| 758 | switchSucceeded = false |
| 759 | } else { |
| 760 | // Check if the agent actually changed to determine if the switch succeeded. |
| 761 | // If it failed, we must not send the message to the wrong agent. |
| 762 | switchSucceeded = m.sessionState.CurrentAgentName() != prevAgent |
| 763 | } |
| 764 | if switchCmd != nil { |
| 765 | cmds = append(cmds, switchCmd) |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if resolved != "" && switchSucceeded { |
| 770 | cmds = append(cmds, core.CmdHandler(messages.SendMsg{Content: resolved, BypassQueue: true})) |
| 771 | } |
| 772 | |
| 773 | return m, tea.Batch(cmds...) |
| 774 | } |
| 775 | |
| 776 | // expandURLPlaceholders substitutes runtime placeholders in a command URL. |
| 777 | // Currently only {{session_id}} is supported. The token is intentionally |
no test coverage detected