diff --git a/cmd/archied/telegram_setup.go b/cmd/archied/telegram_setup.go index a08e031d5..7c699c3b0 100644 --- a/cmd/archied/telegram_setup.go +++ b/cmd/archied/telegram_setup.go @@ -86,7 +86,7 @@ func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), o configureTelegramUpdates(tg, s) } tg.Dangerous = s.Dangerous - tg.SetShowToolCalls(cfg.Chat.Telegram.ShowToolCalls) + tg.SetShowToolCalls(cfg.Chat.ShowToolCalls) tg.ReleaseAnnouncements = &releaseannounce.Announcer{ StatePath: releaseAnnouncementStatePath(cfg.WorkDir, cfg.BotUser), Components: []releaseannounce.Component{ @@ -156,7 +156,7 @@ func makeTelegramReload(s telegramSetup) func(*telegram.Gateway) error { } g.Token = token g.AllowedUserIDs = newCfg.Chat.Telegram.AllowedUserIDs - g.SetShowToolCalls(newCfg.Chat.Telegram.ShowToolCalls) + g.SetShowToolCalls(newCfg.Chat.ShowToolCalls) s.Log.Info("chat gateway config reloaded", "allowed_user_ids", len(g.AllowedUserIDs), "show_tool_calls", g.ShowToolCalls()) diff --git a/config.example.toml b/config.example.toml index bf724467d..ca06d2964 100644 --- a/config.example.toml +++ b/config.example.toml @@ -88,6 +88,13 @@ class = "ollama" # cross-filesystem (disk ops, system edits, etc.). Grant deliberately. # unrestricted_filesystem = true +# Render each completed tool call inline in the reply, naming the tool and one +# line of its result, before the answer built from them. Off by default: it is +# there to make a wrong tool choice visible from the chat, at the cost of +# narrating every internal step. Applies to every chat channel -- Telegram +# and the web dashboard both read this one setting. +# show_tool_calls = true + # Optional interactive-chat catalog. /provider groups these references by # their first path segment, and /model shows only the selected provider's # entries. When omitted, Archie uses the distinct refs in [models]. @@ -102,12 +109,6 @@ class = "ollama" # [chat.telegram] # update_check_command = ["/usr/local/bin/archie-update-check"] # update_install_command = ["/usr/local/bin/archie-update-install"] -# -# Render each completed tool call inline in the reply, naming the tool and one -# line of its result, before the answer built from them. Off by default: it is -# there to make a wrong tool choice visible from the chat, at the cost of -# narrating every internal step. -# show_tool_calls = true # External MCP tools are launched and managed by Archie's MCP client. # [[tools.mcp_servers]] diff --git a/internal/config/config.go b/internal/config/config.go index 34e4e428c..4868d2533 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -667,6 +667,18 @@ type ChatConfig struct { // difference between an agent that can edit /etc directly and one that has // to be asked to. UnrestrictedFilesystem bool `toml:"unrestricted_filesystem" yaml:"unrestricted_filesystem"` + // ShowToolCalls renders each completed tool call inline in the reply, + // naming the tool and one line of its result, before the answer built + // from them. It is off by default: it exists to make a wrong tool + // choice visible from the chat, and it narrates every internal step + // to do so, which is a privacy/noise cost on a chat surface anyone + // with dashboard or bot access can read. + // + // One setting, every chat channel: it lived under [chat.telegram] + // alone until the web dashboard grew its own tool narration with no + // way to turn it off, contradicting an operator who had already set + // this to false expecting no tool activity to be shown anywhere. + ShowToolCalls bool `toml:"show_tool_calls" yaml:"show_tool_calls"` // MaxSteps caps how many model/tool round-trips one chat turn may take // before the runtime stops and returns what it has. Zero uses the // default. @@ -708,11 +720,6 @@ type TelegramConfig struct { // are argv arrays, never shell snippets. UpdateCheckCommand []string `toml:"update_check_command" yaml:"update_check_command"` UpdateInstallCommand []string `toml:"update_install_command" yaml:"update_install_command"` - // ShowToolCalls renders each completed tool call inline in the reply, - // naming the tool and one line of its result. It is off by default: it - // exists to make a wrong tool choice visible from the chat, and it - // narrates every internal step to do so. - ShowToolCalls bool `toml:"show_tool_calls" yaml:"show_tool_calls"` } // EmailConfig configures the inbound email channel via SMTP. diff --git a/internal/webui/api_chat.go b/internal/webui/api_chat.go index 4c878354d..289a230ad 100644 --- a/internal/webui/api_chat.go +++ b/internal/webui/api_chat.go @@ -222,14 +222,28 @@ type chatStreamEvent struct { // chatStreamSink adapts the stream writer to gateway.TurnStream so text and // tool activity reach the browser through one ordered path. -type chatStreamSink func(chatStreamEvent) - -func (f chatStreamSink) Delta(text string) { - f(chatStreamEvent{Type: "delta", Text: text}) +// +// showToolCalls gates ToolCall the same way Telegram's liveReply gates its +// own tool narration: config.ChatConfig.ShowToolCalls is one setting for +// every chat channel, so an operator who turned it off gets no tool frames +// on the dashboard either, not just in Telegram. It is snapshotted into the +// sink at stream start rather than read live, for the same reason Telegram +// snapshots it per-reply -- a config reload mid-turn must not change what a +// turn already in flight narrates. +type chatStreamSink struct { + write func(chatStreamEvent) + showToolCalls bool } -func (f chatStreamSink) ToolCall(event gateway.ToolCallEvent) { - f(chatStreamEvent{ +func (s chatStreamSink) Delta(text string) { + s.write(chatStreamEvent{Type: "delta", Text: text}) +} + +func (s chatStreamSink) ToolCall(event gateway.ToolCallEvent) { + if !s.showToolCalls || event.Name == "" { + return + } + s.write(chatStreamEvent{ Type: "tool", Tool: event.Name, ToolCallID: event.ID, @@ -239,6 +253,17 @@ func (f chatStreamSink) ToolCall(event gateway.ToolCallEvent) { }) } +// chatShowToolCalls reports config.ChatConfig.ShowToolCalls, the one setting +// shared by every chat channel. Off (the default) when Cfg is nil, matching +// the field's own off-by-default doc comment: a dashboard embedded without +// live config must not narrate tool activity nobody opted into. +func (s *Server) chatShowToolCalls() bool { + if s.Cfg == nil { + return false + } + return s.Cfg.Get().Chat.ShowToolCalls +} + func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) { chat, ok := s.chatReady(w) if !ok { @@ -268,11 +293,15 @@ func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) { return } writeChatEvent(chatStreamEvent{Type: "started"}, sessionID) + showToolCalls := s.chatShowToolCalls() var reply string if chat.Turns == nil { - reply, err = chat.Router.RouteStream(r.Context(), msg, chatStreamSink(func(event chatStreamEvent) { - writeChatEvent(event, sessionID) - })) + reply, err = chat.Router.RouteStream(r.Context(), msg, chatStreamSink{ + showToolCalls: showToolCalls, + write: func(event chatStreamEvent) { + writeChatEvent(event, sessionID) + }, + }) } else { type turnResult struct { reply string @@ -285,12 +314,15 @@ func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) { events := make(chan chatStreamEvent, 32) result := make(chan turnResult, 1) chat.Turns.Submit(r.Context(), sessionID, func(turnCtx context.Context) { - turnReply, turnErr := chat.Router.RouteStream(turnCtx, msg, chatStreamSink(func(event chatStreamEvent) { - select { - case events <- event: - case <-r.Context().Done(): - } - })) + turnReply, turnErr := chat.Router.RouteStream(turnCtx, msg, chatStreamSink{ + showToolCalls: showToolCalls, + write: func(event chatStreamEvent) { + select { + case events <- event: + case <-r.Context().Done(): + } + }, + }) result <- turnResult{reply: turnReply, err: turnErr} }) for { diff --git a/internal/webui/api_chat_test.go b/internal/webui/api_chat_test.go index f6994071a..37e1fb216 100644 --- a/internal/webui/api_chat_test.go +++ b/internal/webui/api_chat_test.go @@ -14,6 +14,7 @@ import ( "testing" "time" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/releaseupdate" ) @@ -632,6 +633,10 @@ func TestChatStreamReportsToolCalls(t *testing.T) { t.Run(tc.name, func(t *testing.T) { server, sessions := chatTestServer(t) server.Chat.Router.LLMStream = stream + // Enabled explicitly: this test exercises the tool-narration + // path itself, which is off by default (see + // TestChatStreamHidesToolCallsWhenShowToolCallsIsOff). + server.Cfg = config.NewHolder(config.Config{Chat: config.ChatConfig{ShowToolCalls: true}}) if tc.queued { server.Chat.Turns = gateway.NewTurns(slog.Default()) } @@ -674,6 +679,53 @@ func TestChatStreamReportsToolCalls(t *testing.T) { } } +// config.ChatConfig.ShowToolCalls is off by default and shared by every chat +// channel: an operator who left it unset (or explicitly turned it off) must +// see no tool activity on the dashboard, not just in Telegram. Previously the +// web stream narrated every tool call unconditionally, because nothing here +// read this setting at all. +func TestChatStreamHidesToolCallsWhenShowToolCallsIsOff(t *testing.T) { + stream := func(_ context.Context, _ gateway.Message, turn gateway.TurnStream) (string, error) { + turn.Delta("checking") + turn.ToolCall(gateway.ToolCallEvent{Name: "shell", Output: "exit 0"}) + turn.Delta(" and answering") + return "checking and answering", nil + } + + tests := []struct { + name string + cfg *config.Holder + }{ + {name: "no Cfg wired at all", cfg: nil}, + {name: "Cfg present but show_tool_calls unset", cfg: config.NewHolder(config.Config{})}, + {name: "show_tool_calls explicitly false", cfg: config.NewHolder(config.Config{Chat: config.ChatConfig{ShowToolCalls: false}})}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + server, sessions := chatTestServer(t) + server.Chat.Router.LLMStream = stream + server.Cfg = tc.cfg + saveWebSession(t, sessions, "web-1") + + req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/api/chat/stream", + strings.NewReader(`{"channel_id":"browser-web-1","text":"hello"}`)) + req.Header.Set("Content-Type", "application/json") + res := httptest.NewRecorder() + server.Handler().ServeHTTP(res, req) + if res.Code != http.StatusOK { + t.Fatalf("status = %d, body = %s", res.Code, res.Body) + } + + for _, ev := range parseChatSSE(t, res.Body.String()) { + if ev.Type == "tool" { + t.Fatalf("got a tool frame with show_tool_calls off: %+v", ev) + } + } + }) + } +} + // TestChatPersonaEndpoint proves the persona surface: switching to a known // persona works, an unknown name is refused, a non-web session is invisible, // and an unconfigured registry degrades to 501.