diff --git a/cmd/archied/chat_stream_test.go b/cmd/archied/chat_stream_test.go new file mode 100644 index 000000000..84a20f001 --- /dev/null +++ b/cmd/archied/chat_stream_test.go @@ -0,0 +1,123 @@ +package main + +import ( + "testing" + + "github.com/samcharles93/ai-sdk/core" + + "github.com/samcharles93/archie-core/internal/gateway" +) + +// recordingStream captures the turn's reported progress in arrival order. +type recordingStream struct { + events []string +} + +func (s *recordingStream) Delta(text string) { + s.events = append(s.events, "text:"+text) +} + +func (s *recordingStream) ToolCall(event gateway.ToolCallEvent) { + s.events = append(s.events, "tool:"+event.Name+":"+event.Summary()) +} + +func streamOf(parts ...core.StreamPart) <-chan core.StreamPart { + ch := make(chan core.StreamPart, len(parts)) + for _, part := range parts { + ch <- part + } + close(ch) + return ch +} + +func TestDrainChatStream(t *testing.T) { + tests := []struct { + name string + parts []core.StreamPart + wantText string + wantEvents []string + }{ + { + name: "text only", + parts: []core.StreamPart{ + {Type: core.StreamPartTextDelta, TextDelta: "hello "}, + {Type: core.StreamPartTextDelta, TextDelta: "world"}, + }, + wantText: "hello world", + wantEvents: []string{"text:hello ", "text:world"}, + }, + { + name: "a tool result is reported between the text around it", + parts: []core.StreamPart{ + {Type: core.StreamPartTextDelta, TextDelta: "checking"}, + {Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "shell", Input: `{"cmd":"true"}`}}, + {Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolCallID: "1", ToolName: "shell", Output: "exit 0"}}, + {Type: core.StreamPartTextDelta, TextDelta: " done"}, + }, + wantText: "checking done", + wantEvents: []string{"text:checking", "tool:shell:exit 0", "text: done"}, + }, + { + name: "a failed tool is reported as a failure, not silence", + parts: []core.StreamPart{ + {Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "shell", Error: "exit status 2"}}, + }, + wantEvents: []string{"tool:shell:failed: exit status 2"}, + }, + { + // The tool call part carries no outcome yet: reporting it as + // well would show every tool twice. + name: "the tool call part alone reports nothing", + parts: []core.StreamPart{ + {Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolName: "shell"}}, + }, + wantEvents: nil, + }, + { + name: "parts with nothing to render are skipped", + parts: []core.StreamPart{ + {Type: core.StreamPartStartStep}, + {Type: core.StreamPartTextDelta, TextDelta: ""}, + {Type: core.StreamPartReasoningDelta, ReasoningDelta: "thinking"}, + {Type: core.StreamPartToolResult}, + {Type: core.StreamPartFinishStep}, + {Type: core.StreamPartFinish}, + }, + wantEvents: nil, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + sink := &recordingStream{} + text := drainChatStream(streamOf(tc.parts...), sink) + + if text != tc.wantText { + t.Fatalf("text = %q, want %q", text, tc.wantText) + } + if len(sink.events) != len(tc.wantEvents) { + t.Fatalf("events = %v, want %v", sink.events, tc.wantEvents) + } + for i := range tc.wantEvents { + if sink.events[i] != tc.wantEvents[i] { + t.Fatalf("events = %v, want %v", sink.events, tc.wantEvents) + } + } + }) + } +} + +// The stream must be drained to close even when nobody is rendering it: +// ai-sdk writes to FullStream synchronously, so an abandoned stream stalls +// the generating goroutine. +func TestDrainChatStreamWithoutASinkStillCollectsText(t *testing.T) { + text := drainChatStream(streamOf( + core.StreamPart{Type: core.StreamPartTextDelta, TextDelta: "a"}, + core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "shell", Output: "ok"}}, + core.StreamPart{Type: core.StreamPartTextDelta, TextDelta: "b"}, + ), nil) + + if text != "ab" { + t.Fatalf("text = %q, want %q", text, "ab") + } +} diff --git a/cmd/archied/chat_turn_model.go b/cmd/archied/chat_turn_model.go index 3950ba041..d22f54012 100644 --- a/cmd/archied/chat_turn_model.go +++ b/cmd/archied/chat_turn_model.go @@ -78,7 +78,7 @@ func (m *preparedChatTurnModel) ToolSchemaTokens() int { func (m *preparedChatTurnModel) Generate( ctx context.Context, request gateway.TurnModelRequest, - onDelta func(string), + stream gateway.TurnStream, ) (string, error) { options := m.options options.Messages = make([]chat.Message, len(request.Messages)) @@ -93,5 +93,5 @@ func (m *preparedChatTurnModel) Generate( options.Messages[i] = chat.Message{Role: role, Content: message.Content} } options.MaxTokens = request.MaxTokens - return sendChatTurn(ctx, m.llm, m.model, options, onDelta) + return sendChatTurn(ctx, m.llm, m.model, options, stream) } diff --git a/cmd/archied/telegram_setup.go b/cmd/archied/telegram_setup.go index 454d74512..0af0d7a62 100644 --- a/cmd/archied/telegram_setup.go +++ b/cmd/archied/telegram_setup.go @@ -86,6 +86,7 @@ func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), o configureTelegramUpdates(tg, s) } tg.Dangerous = s.Dangerous + tg.ShowToolCalls = cfg.Chat.Telegram.ShowToolCalls tg.ReleaseAnnouncements = &releaseannounce.Announcer{ StatePath: releaseAnnouncementStatePath(cfg.WorkDir, cfg.BotUser), Components: []releaseannounce.Component{ @@ -155,7 +156,10 @@ func makeTelegramReload(s telegramSetup) func(*telegram.Gateway) error { } g.Token = token g.AllowedUserIDs = newCfg.Chat.Telegram.AllowedUserIDs - s.Log.Info("chat gateway config reloaded", "allowed_user_ids", len(g.AllowedUserIDs)) + g.ShowToolCalls = newCfg.Chat.Telegram.ShowToolCalls + s.Log.Info("chat gateway config reloaded", + "allowed_user_ids", len(g.AllowedUserIDs), + "show_tool_calls", g.ShowToolCalls) return nil } } @@ -232,8 +236,8 @@ func newChatTurnRunner( return runner } -func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, onDelta func(string)) (string, error) { - if onDelta == nil { +func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, turn gateway.TurnStream) (string, error) { + if turn == nil { result, err := llm.Chat(ctx, chatModel, options) if err != nil { return "", fmt.Errorf("llm chat: %w", err) @@ -244,18 +248,47 @@ func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, o if err != nil { return "", fmt.Errorf("llm chat stream: %w", err) } - var sb strings.Builder - for part := range stream.FullStream { - if part.Type != core.StreamPartTextDelta || part.TextDelta == "" { - continue - } - sb.WriteString(part.TextDelta) - onDelta(part.TextDelta) - } + text := drainChatStream(stream.FullStream, turn) if _, err := stream.FinishReason(); err != nil { return "", fmt.Errorf("llm chat stream: %w", err) } - return sb.String(), nil + return text, nil +} + +// drainChatStream consumes a model stream to close, reporting assistant text +// and each completed tool call to turn, and returns the assembled reply. +// +// FullStream is authoritative and its writes are synchronous, so it must be +// drained to close even when turn is nil -- an unread stream stalls the +// generating goroutine. +// +// A tool is reported on its result, never on its call: the call part carries +// no outcome yet, so reporting both would show every tool twice, once with +// nothing to say. +func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream) string { + var sb strings.Builder + for part := range parts { + switch part.Type { + case core.StreamPartTextDelta: + if part.TextDelta == "" { + continue + } + sb.WriteString(part.TextDelta) + if turn != nil { + turn.Delta(part.TextDelta) + } + case core.StreamPartToolResult: + if part.ToolResult == nil || turn == nil { + continue + } + turn.ToolCall(gateway.ToolCallEvent{ + Name: part.ToolResult.ToolName, + Output: part.ToolResult.Output, + Err: part.ToolResult.Error, + }) + } + } + return sb.String() } // chatTitleGenerator proposes session titles through the chat model. It diff --git a/config.example.toml b/config.example.toml index ad5a4bff2..bf724467d 100644 --- a/config.example.toml +++ b/config.example.toml @@ -102,6 +102,12 @@ 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/channels/telegram/draft.go b/internal/channels/telegram/draft.go deleted file mode 100644 index 4f8bf3625..000000000 --- a/internal/channels/telegram/draft.go +++ /dev/null @@ -1,82 +0,0 @@ -package telegram - -import ( - "context" - "strings" - "sync" - "time" - - "github.com/go-telegram/bot" - "github.com/go-telegram/bot/models" -) - -// draftInterval is the minimum gap between draft updates. Telegram rate -// limits per-chat writes, and an LLM emits tokens far faster than any -// human reads, so deltas are coalesced into one update per tick rather -// than sent individually. -const draftInterval = 700 * time.Millisecond - -// draft streams a reply into a Telegram message draft (sendMessageDraft) -// so the user watches the answer being written instead of waiting on a -// silent "typing…" indicator. -// -// A draft is deliberately best-effort and self-contained: it never blocks -// the generating goroutine and never surfaces an error. The authoritative -// reply is always the final SendMessage the caller makes once the turn -// completes -- if every draft update failed, the user simply sees the -// finished message appear the way it did before streaming existed. -type draft struct { - g *Gateway - b *bot.Bot - chatID int64 - messageThreadID int - draftID int - - mu sync.Mutex - text strings.Builder - last time.Time -} - -// newDraft creates a draft bound to one chat/thread. The draft ID only has -// to be unique per streaming session. -func (g *Gateway) newDraft(b *bot.Bot, chatID int64, messageThreadID int) *draft { - return &draft{ - g: g, - b: b, - chatID: chatID, - messageThreadID: messageThreadID, - draftID: int(time.Now().UnixNano() & 0x7fffffff), - } -} - -// onDelta accumulates a fragment and pushes a throttled draft update. -// It satisfies the onDelta callback of gateway.Router.RouteStream. -func (d *draft) onDelta(delta string) { - if delta == "" { - return - } - - d.mu.Lock() - d.text.WriteString(delta) - if time.Since(d.last) < draftInterval { - d.mu.Unlock() - return - } - d.last = time.Now() - // A block cursor marks the reply as still being written. - body := d.text.String() + " ▌" - d.mu.Unlock() - - // Stream as a rich message so partial Markdown renders the same way - // the final reply will. Mid-stream text is frequently unbalanced (an - // unclosed ** or code fence); a failed draft update is logged and - // skipped, and the next tick -- or the final message -- corrects it. - if _, err := d.b.SendRichMessageDraft(context.Background(), &bot.SendRichMessageDraftParams{ - ChatID: d.chatID, - MessageThreadID: d.messageThreadID, - DraftID: d.draftID, - RichMessage: models.InputRichMessage{Markdown: body}, - }); err != nil { - d.g.log.Debug("send rich message draft failed", "error", err) - } -} diff --git a/internal/channels/telegram/live.go b/internal/channels/telegram/live.go new file mode 100644 index 000000000..a0040dc1a --- /dev/null +++ b/internal/channels/telegram/live.go @@ -0,0 +1,293 @@ +package telegram + +import ( + "context" + "strings" + "sync" + "time" + + "github.com/go-telegram/bot" + "github.com/go-telegram/bot/models" + + "github.com/samcharles93/archie-core/internal/gateway" +) + +const ( + // liveInterval is the minimum gap between live updates. Telegram rate + // limits per-chat writes, and an LLM emits tokens far faster than any + // human reads, so deltas are coalesced into one edit per tick rather + // than sent individually. + liveInterval = 1 * time.Second + + // liveCursor marks the reply as still being written. It is appended at + // render time and never stored in the buffer, so every frame carries + // exactly one cursor and it is always at the current end. + liveCursor = "▌" + + // toolCallPrefix opens an inline tool-activity line. + toolCallPrefix = "🔧 " + + // liveBodyMaxRunes bounds one mid-turn frame. Telegram rejects a + // message over its character limit outright, so an unbounded frame + // means every edit past that point fails and the live message freezes + // at the last frame that fit -- for the rest of the turn, since the + // buffer only grows. The bound is in runes rather than bytes because + // the limit Telegram applies is a character count, not a byte count. + // + // Only the live frame is bounded. finalize sends the whole reply, + // split across as many messages as it needs. + liveBodyMaxRunes = 3900 +) + +// liveReply renders one chat turn into a single Telegram message as it is +// generated, so the user watches the answer being written instead of waiting +// on a silent "typing…" indicator. +// +// It keeps one canonical buffer and sends the whole of it on every update: +// the first update creates the message, every later one edits that same +// message. Editing has replace semantics, which is what makes a partial frame +// impossible to append to the last one. (It previously streamed through +// sendMessageDraft, whose drafts are ephemeral 30-second previews -- a tool +// call longer than that outlived the preview, and successive frames were +// observed concatenated, cursors and all.) +// +// Rendering is deliberately best-effort: it never blocks the generating +// goroutine and never surfaces an error. The authoritative reply is the one +// finalize writes when the turn completes -- if every live update failed, +// the user simply sees the finished message appear the way it did before +// streaming existed. +type liveReply struct { + g *Gateway + b *bot.Bot + chatID int64 + messageThreadID int + // interval throttles updates; zero renders every change. + interval time.Duration + + mu sync.Mutex + // buf is the canonical buffer: assistant text and tool lines in the + // order the model produced them. + buf strings.Builder + // toolLines holds the same tool entries separately, so the finished + // message can lead with them rather than with them buried mid-answer. + toolLines []string + // rendered is the last body actually sent. Telegram rejects an edit + // that changes nothing, so an unchanged body is not sent at all. + rendered string + messageID int + last time.Time +} + +// newLiveReply creates a renderer bound to one chat/thread. +func (g *Gateway) newLiveReply(b *bot.Bot, chatID int64, messageThreadID int) *liveReply { + return &liveReply{ + g: g, + b: b, + chatID: chatID, + messageThreadID: messageThreadID, + interval: liveInterval, + } +} + +// Delta appends the next fragment of assistant text. It satisfies +// gateway.TurnStream. +func (l *liveReply) Delta(text string) { + if text == "" { + return + } + l.mu.Lock() + l.buf.WriteString(text) + throttled := time.Since(l.last) < l.interval + l.mu.Unlock() + if throttled { + return + } + l.render(context.Background()) +} + +// ToolCall appends one tool-activity line. It satisfies gateway.TurnStream. +// +// Tool activity is opt-in per deployment: without it the user cannot tell a +// hallucinated action from a real one, but it also narrates every internal +// step, which is noise in a conversation that is going fine. +func (l *liveReply) ToolCall(event gateway.ToolCallEvent) { + if !l.g.ShowToolCalls || event.Name == "" { + return + } + line := toolCallPrefix + event.Name + " — " + event.Summary() + + l.mu.Lock() + if l.buf.Len() > 0 && !strings.HasSuffix(l.buf.String(), "\n") { + l.buf.WriteString("\n") + } + l.buf.WriteString(line) + l.buf.WriteString("\n") + l.toolLines = append(l.toolLines, line) + l.mu.Unlock() + + // A tool call is a discrete event rather than a token, and there are + // few of them: render it immediately so the user sees what ran while + // it is still relevant. + l.render(context.Background()) +} + +// render writes the current buffer, cursor and all, to the live message. +func (l *liveReply) render(ctx context.Context) { + l.mu.Lock() + body := l.body() + if body == "" || body == l.rendered { + l.mu.Unlock() + return + } + l.rendered = body + l.last = time.Now() + messageID := l.messageID + l.mu.Unlock() + + // Mid-stream text is frequently unbalanced Markdown (an unclosed ** or + // code fence); a rejected update is logged and skipped, and the next + // tick -- or finalize -- corrects it. + if messageID == 0 { + l.open(ctx, body) + return + } + l.edit(ctx, messageID, body) +} + +// finalize replaces the live message with the turn's authoritative reply, +// led by the tool activity that produced it, and drops the cursor. When +// nothing was ever streamed -- a non-streaming provider, or a reply served +// from the turn ledger -- it sends the reply as a new message instead. +func (l *liveReply) finalize(ctx context.Context, reply string) { + l.mu.Lock() + content := l.finalText(reply) + l.rendered = content + messageID := l.messageID + l.mu.Unlock() + + if content == "" { + l.abandon(ctx) + return + } + if messageID == 0 { + l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, content) + return + } + + // The live message is one message, so an oversized reply keeps it as + // the first part and sends the remainder after it. + parts := splitLongMessage(content, messageMaxLen) + l.edit(ctx, messageID, parts[0]) + for _, part := range parts[1:] { + l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, part) + } +} + +// abandon leaves a stopped or failed turn readable: the partial text stays, +// but the cursor goes, so the message does not claim to still be writing. +func (l *liveReply) abandon(ctx context.Context) { + l.mu.Lock() + // Bounded for the same reason a live frame is: this is still one edit + // of one message, and a stopped turn that had already streamed past + // the limit would keep its cursor if the edit were rejected. + text := clampToOneMessage(strings.TrimRight(l.buf.String(), " \t\n")) + messageID := l.messageID + if messageID == 0 || text == "" || text == l.rendered { + l.mu.Unlock() + return + } + l.rendered = text + l.mu.Unlock() + + l.edit(ctx, messageID, text) +} + +// body renders the canonical buffer for a mid-turn frame. The caller holds +// the lock. +// +// A frame too long to send is cut to its tail: that is where the writing is +// happening, and finalize replaces the whole thing with the authoritative +// reply when the turn ends. +func (l *liveReply) body() string { + text := strings.TrimRight(l.buf.String(), " \t\n") + if text == "" { + return "" + } + return clampToOneMessage(text) + " " + liveCursor +} + +// clampToOneMessage cuts s to the tail that fits in a single Telegram +// message, marking the cut with a leading ellipsis. Counting runes rather +// than bytes matches how Telegram measures the limit and keeps the cut off a +// character boundary. +func clampToOneMessage(s string) string { + runes := []rune(s) + if len(runes) <= liveBodyMaxRunes { + return s + } + return "…" + string(runes[len(runes)-liveBodyMaxRunes:]) +} + +// finalText composes the finished message: the tool activity in the order it +// happened, then the authoritative reply. The caller holds the lock. +// +// The reply is used rather than the streamed buffer because it is what the +// turn actually returned and what was persisted to the conversation -- a +// dropped or throttled frame cannot leave the visible message disagreeing +// with the stored one. +func (l *liveReply) finalText(reply string) string { + reply = strings.TrimSpace(reply) + if len(l.toolLines) == 0 { + return reply + } + block := strings.Join(l.toolLines, "\n") + if reply == "" { + return block + } + return block + "\n\n" + reply +} + +// open creates the live message and records its ID for later edits. +func (l *liveReply) open(ctx context.Context, body string) { + params := &bot.SendRichMessageParams{ + ChatID: l.chatID, + RichMessage: models.InputRichMessage{Markdown: body}, + } + if l.messageThreadID != 0 { + params.MessageThreadID = l.messageThreadID + } + msg, err := l.b.SendRichMessage(ctx, params) + if err != nil { + l.g.log.Debug("live reply send failed", "error", err) + return + } + l.mu.Lock() + l.messageID = msg.ID + l.mu.Unlock() +} + +// edit replaces the live message's content. +// +// Rich messages are a recent Bot API addition, and text is only optional on +// an edit that carries one, so a server that rejects the rich body rejects +// the whole edit. As with send, that is treated as "unsupported" rather than +// fatal: retry unformatted so the user still gets the reply. +func (l *liveReply) edit(ctx context.Context, messageID int, body string) { + _, err := l.b.EditMessageText(ctx, &bot.EditMessageTextParams{ + ChatID: l.chatID, + MessageID: messageID, + RichMessage: &models.InputRichMessage{Markdown: body}, + }) + if err == nil { + return + } + l.g.log.Debug("live reply rich edit failed, retrying unformatted", "error", err) + + if _, err := l.b.EditMessageText(ctx, &bot.EditMessageTextParams{ + ChatID: l.chatID, + MessageID: messageID, + Text: body, + }); err != nil { + l.g.log.Debug("live reply edit failed", "error", err) + } +} diff --git a/internal/channels/telegram/live_test.go b/internal/channels/telegram/live_test.go new file mode 100644 index 000000000..c00793458 --- /dev/null +++ b/internal/channels/telegram/live_test.go @@ -0,0 +1,351 @@ +package telegram + +import ( + "context" + "encoding/json" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/go-telegram/bot" + "github.com/go-telegram/bot/models" + + "github.com/samcharles93/archie-core/internal/gateway" +) + +func toolEvent(name, output, failure string) gateway.ToolCallEvent { + return gateway.ToolCallEvent{Name: name, Output: output, Err: failure} +} + +// apiCall is one captured Bot API request: the method name and the fields the +// renderer is asserted on. +type apiCall struct { + method string + messageID string + markdown string + // rich records whether the call carried a rich_message body rather + // than plain text. + rich bool +} + +// capturedCall reads the fields the renderer is asserted on out of one Bot +// API request. Rich messages carry their text in a JSON field rather than in +// text, so both are read and the rich one wins when present. +func capturedCall(t *testing.T, r *http.Request) apiCall { + t.Helper() + call := apiCall{method: r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]} + if err := r.ParseMultipartForm(1 << 20); err != nil { + t.Errorf("parse Telegram request: %v", err) + return call + } + call.messageID = r.FormValue("message_id") + call.markdown = r.FormValue("text") + raw := r.FormValue("rich_message") + if raw == "" { + return call + } + var rich models.InputRichMessage + if err := json.Unmarshal([]byte(raw), &rich); err != nil { + t.Errorf("decode Telegram rich_message: %v", err) + return call + } + call.markdown = rich.Markdown + call.rich = true + return call +} + +// fakeAPI serves a Bot API that records every call and answers with a message +// whose ID is fixed, so the renderer has something to edit. +func fakeAPI(t *testing.T) (*bot.Bot, *[]apiCall) { + t.Helper() + calls := &[]apiCall{} + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + *calls = append(*calls, capturedCall(t, r)) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"ok":true,"result":{"message_id":55,"date":1,"chat":{"id":7,"type":"private"}}}`)) + })) + t.Cleanup(api.Close) + + b, err := bot.New("1:test", bot.WithServerURL(api.URL), bot.WithSkipGetMe()) + if err != nil { + t.Fatalf("new test bot: %v", err) + } + return b, calls +} + +// newTestLiveReply builds a renderer that writes on every update, so tests +// assert on content rather than on wall-clock throttling. +func newTestLiveReply(t *testing.T, showToolCalls bool) (*liveReply, *[]apiCall) { + t.Helper() + b, calls := fakeAPI(t) + g := New("1:test", "", "", []int64{42}, slog.New(slog.DiscardHandler)) + g.ShowToolCalls = showToolCalls + live := g.newLiveReply(b, 7, 0) + live.interval = 0 + return live, calls +} + +// The whole point of the canonical buffer: every update carries the complete +// text so far, and it replaces the message rather than adding to it. +func TestLiveReplySendsOnceThenEditsWithTheWholeBuffer(t *testing.T) { + live, calls := newTestLiveReply(t, false) + ctx := context.Background() + + live.Delta("Now let me look") + live.Delta(" at the shell tool") + live.Delta(". Now I have the full picture") + live.finalize(ctx, "Now let me look at the shell tool. Now I have the full picture") + + if len(*calls) != 4 { + t.Fatalf("calls = %d (%v), want 4", len(*calls), *calls) + } + first := (*calls)[0] + if first.method != "sendRichMessage" { + t.Fatalf("first call = %q, want sendRichMessage", first.method) + } + for i, call := range (*calls)[1:] { + if call.method != "editMessageText" { + t.Fatalf("call %d = %q, want editMessageText", i+1, call.method) + } + if call.messageID != "55" { + t.Fatalf("call %d edited message %q, want the message the first call created", i+1, call.messageID) + } + } + + want := []string{ + "Now let me look ▌", + "Now let me look at the shell tool ▌", + "Now let me look at the shell tool. Now I have the full picture ▌", + "Now let me look at the shell tool. Now I have the full picture", + } + for i, call := range *calls { + if call.markdown != want[i] { + t.Fatalf("call %d rendered\n %q\nwant\n %q", i, call.markdown, want[i]) + } + } +} + +// Exactly one cursor, always at the end: a frame that kept an earlier cursor +// is the rendering fault this buffer exists to prevent. +func TestLiveReplyKeepsExactlyOneCursorAtTheEnd(t *testing.T) { + live, calls := newTestLiveReply(t, true) + + live.Delta("first") + live.ToolCall(toolEvent("shell", "exit 0", "")) + live.Delta("second") + + for i, call := range *calls { + if n := strings.Count(call.markdown, liveCursor); n != 1 { + t.Fatalf("call %d has %d cursors, want 1:\n%s", i, n, call.markdown) + } + if !strings.HasSuffix(call.markdown, liveCursor) { + t.Fatalf("call %d does not end with the cursor:\n%s", i, call.markdown) + } + } +} + +// Telegram rejects an edit that does not change the message, so a repeated +// render must not be sent at all. +func TestLiveReplySkipsUnchangedRenders(t *testing.T) { + live, calls := newTestLiveReply(t, false) + + live.Delta("same") + live.render(context.Background()) + live.render(context.Background()) + + if len(*calls) != 1 { + t.Fatalf("calls = %d (%v), want 1 -- an unchanged body must not be re-sent", len(*calls), *calls) + } +} + +func TestLiveReplyToolCalls(t *testing.T) { + tests := []struct { + name string + showToolCalls bool + wantLive string + wantFinal string + }{ + { + name: "shown", + showToolCalls: true, + wantLive: "checking\n🔧 shell — exit 0 ▌", + wantFinal: "🔧 shell — exit 0\n\nchecking", + }, + { + name: "hidden", + showToolCalls: false, + wantLive: "checking ▌", + wantFinal: "checking", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + live, calls := newTestLiveReply(t, tc.showToolCalls) + + live.Delta("checking") + live.ToolCall(toolEvent("shell", "exit 0", "")) + live.finalize(context.Background(), "checking") + + gotLive := (*calls)[len(*calls)-2].markdown + gotFinal := (*calls)[len(*calls)-1].markdown + if gotLive != tc.wantLive { + t.Fatalf("live render = %q, want %q", gotLive, tc.wantLive) + } + if gotFinal != tc.wantFinal { + t.Fatalf("final render = %q, want %q", gotFinal, tc.wantFinal) + } + }) + } +} + +// Nothing streamed -- a non-streaming provider, or a reply served from the +// turn ledger -- still has to deliver the answer as a new message. +func TestLiveReplyFinalizeWithoutAnyStreamSendsTheReply(t *testing.T) { + live, calls := newTestLiveReply(t, false) + + live.finalize(context.Background(), "the whole answer") + + if len(*calls) != 1 { + t.Fatalf("calls = %v, want one send", *calls) + } + if (*calls)[0].method != "sendRichMessage" { + t.Fatalf("method = %q, want sendRichMessage", (*calls)[0].method) + } + if (*calls)[0].markdown != "the whole answer" { + t.Fatalf("markdown = %q", (*calls)[0].markdown) + } +} + +// A stopped or failed turn must not leave a cursor blinking forever on a +// message nothing will ever finish. +func TestLiveReplyAbandonDropsTheCursor(t *testing.T) { + live, calls := newTestLiveReply(t, false) + + live.Delta("half an ans") + live.abandon(context.Background()) + + last := (*calls)[len(*calls)-1] + if last.method != "editMessageText" { + t.Fatalf("last call = %q, want editMessageText", last.method) + } + if last.markdown != "half an ans" { + t.Fatalf("abandoned render = %q, want the buffer without a cursor", last.markdown) + } +} + +// Abandoning a turn that never rendered anything must stay silent rather than +// posting an empty message. +func TestLiveReplyAbandonWithoutAMessageIsSilent(t *testing.T) { + live, calls := newTestLiveReply(t, false) + + live.abandon(context.Background()) + + if len(*calls) != 0 { + t.Fatalf("calls = %v, want none", *calls) + } +} + +// Rich messages are a recent Bot API addition, and an edit that carries only +// a rich body is rejected by a server that does not support it. The reply +// must still arrive, unformatted, rather than being lost to a failed edit. +func TestLiveReplyEditFallsBackToPlainText(t *testing.T) { + calls := &[]apiCall{} + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + call := capturedCall(t, r) + *calls = append(*calls, call) + w.Header().Set("Content-Type", "application/json") + if call.method == "editMessageText" && call.rich { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"ok":false,"error_code":400,"description":"Bad Request: rich_message is not supported"}`)) + return + } + _, _ = w.Write([]byte(`{"ok":true,"result":{"message_id":55,"date":1,"chat":{"id":7,"type":"private"}}}`)) + })) + t.Cleanup(api.Close) + b, err := bot.New("1:test", bot.WithServerURL(api.URL), bot.WithSkipGetMe()) + if err != nil { + t.Fatalf("new test bot: %v", err) + } + g := New("1:test", "", "", []int64{42}, slog.New(slog.DiscardHandler)) + live := g.newLiveReply(b, 7, 0) + live.interval = 0 + + live.Delta("streamed") + live.finalize(context.Background(), "the answer") + + last := (*calls)[len(*calls)-1] + if last.method != "editMessageText" || last.rich { + t.Fatalf("last call = %+v, want a plain-text editMessageText retry", last) + } + if last.markdown != "the answer" { + t.Fatalf("plain retry sent %q, want the reply", last.markdown) + } +} + +// Telegram rejects an over-long message outright, so a reply that keeps +// growing past the limit must not freeze the live message: every mid-turn +// frame, and the abandoned frame a stopped turn leaves behind, has to stay +// inside one message. +func TestLiveReplyFramesStayWithinOneMessage(t *testing.T) { + live, calls := newTestLiveReply(t, false) + + for range 4 { + live.Delta(strings.Repeat("a", liveBodyMaxRunes)) + } + live.abandon(context.Background()) + + // Telegram's hard limit on one message, stated here rather than + // derived from liveBodyMaxRunes so that raising the bound past what + // Telegram accepts fails this test instead of moving with it. + const telegramMessageMaxRunes = 4096 + + if len(*calls) < 3 { + t.Fatalf("calls = %d, want a frame per delta plus the abandon edit", len(*calls)) + } + for i, call := range *calls { + if n := len([]rune(call.markdown)); n > telegramMessageMaxRunes { + t.Fatalf("frame %d is %d runes, want at most %d -- Telegram rejects it whole", + i, n, telegramMessageMaxRunes) + } + } + // The tail is what the user is watching being written, so that is the + // end that survives the cut. + last := (*calls)[len(*calls)-1] + if !strings.HasPrefix(last.markdown, "…") { + t.Fatalf("truncated frame = %.20q…, want a leading ellipsis marking the cut", last.markdown) + } +} + +// A reply longer than one Telegram message keeps the live message as its +// first part and sends the rest, instead of failing the edit and losing it. +func TestLiveReplyFinalizeSplitsAnOversizedReply(t *testing.T) { + live, calls := newTestLiveReply(t, false) + long := strings.Repeat("a\n", messageMaxLen) + + live.Delta("start") + live.finalize(context.Background(), long) + + tail := (*calls)[1:] + if len(tail) < 2 { + t.Fatalf("calls after the first send = %d, want the edit plus at least one follow-up", len(tail)) + } + if tail[0].method != "editMessageText" { + t.Fatalf("first finalize call = %q, want editMessageText", tail[0].method) + } + for i, call := range tail[1:] { + if call.method != "sendRichMessage" { + t.Fatalf("follow-up %d = %q, want sendRichMessage", i, call.method) + } + } + var rendered strings.Builder + for _, call := range tail { + rendered.WriteString(call.markdown) + } + if strings.Count(rendered.String(), "a") != strings.Count(long, "a") { + t.Fatalf("split reply lost content: rendered %d of %d lines", + strings.Count(rendered.String(), "a"), strings.Count(long, "a")) + } +} diff --git a/internal/channels/telegram/telegram.go b/internal/channels/telegram/telegram.go index cf500c871..7639006db 100644 --- a/internal/channels/telegram/telegram.go +++ b/internal/channels/telegram/telegram.go @@ -55,6 +55,12 @@ type Gateway struct { // root supplies this; Telegram only renders approval UX. Dangerous DangerousCommandAuthority + // ShowToolCalls renders each completed tool call as an inline line in + // the reply, so the human can see which tools ran and what they + // returned rather than only the answer built from them. Off by default: + // it is debugging visibility, and it narrates every internal step. + ShowToolCalls bool + // restartCh carries /restart requests from a bot handler to the // supervisor loop in Start. Buffered so a handler never blocks. restartCh chan restartRequest @@ -536,28 +542,38 @@ func (g *Gateway) submitTurn(ctx context.Context, b *bot.Bot, msg *models.Messag // reply appears as it is written; the typing indicator covers the // gap before the first token and any non-streaming path. stopTyping := g.startTyping(turnCtx, b, chatID, threadID) - draft := g.newDraft(b, chatID, threadID) + live := g.newLiveReply(b, chatID, threadID) // If it starts with / but wasn't matched by a registered // handler, it's unknown -- let the router handle it (which // will say "unrecognized"). - reply, err := router.RouteStream(turnCtx, gm, draft.onDelta) + reply, err := router.RouteStream(turnCtx, gm, live) stopTyping() // A cancelled turn is a /stop, not a fault. The stop handler has // already acknowledged it, and turnCtx is dead, so there is - // nothing useful left to send from here. - if errors.Is(err, context.Canceled) { - g.log.Info("chat turn stopped", "session", session) - return - } + // nothing useful left to send from here -- but whatever was + // already streamed stays, minus the cursor that would otherwise + // claim the answer is still being written. + // + // Both failure branches abandon through a live context, not + // turnCtx. A /stop is not reliably reported as context.Canceled: + // ai-sdk raises an aborted stream as ErrAborted with ctx.Err() + // formatted in, not wrapped, so errors.Is misses a cancellation + // that landed inside the step loop or a tool call and it arrives + // here as a plain error -- with turnCtx already dead. Editing + // through a dead context fails, and the cursor this cleanup + // exists to remove would blink forever. if err != nil { - g.log.Error("route failed", "error", err) + if errors.Is(err, context.Canceled) { + g.log.Info("chat turn stopped", "session", session) + } else { + g.log.Error("route failed", "error", err) + } + live.abandon(context.WithoutCancel(turnCtx)) return } - if reply != "" { - g.sendMessage(turnCtx, b, chatID, threadID, reply) - } + live.finalize(turnCtx, reply) }) } @@ -638,14 +654,16 @@ func (g *Gateway) isSenderAllowed(userID int64) bool { return slices.Contains(g.AllowedUserIDs, userID) } +// messageMaxLen keeps a single message under Telegram's 4096 character +// limit, with room for the continuation marker split adds. +const messageMaxLen = 4000 + func (g *Gateway) sendMessage(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, text string) { - // Split long messages to stay under Telegram's 4096 character limit. - const maxLen = 4000 - if len(text) <= maxLen { + if len(text) <= messageMaxLen { g.send(ctx, b, chatID, messageThreadID, text, "send message failed") return } - parts := splitLongMessage(text, maxLen) + parts := splitLongMessage(text, messageMaxLen) for i, part := range parts { partText := part if i < len(parts)-1 { diff --git a/internal/config/config.go b/internal/config/config.go index fb68c525f..34e4e428c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -708,6 +708,11 @@ 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/gateway/gateway.go b/internal/gateway/gateway.go index c07ba8490..6a588c8b6 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -84,15 +84,16 @@ type StatusReader interface { type LLMResponder func(ctx context.Context, msg Message) (string, error) // LLMStreamResponder is the streaming counterpart of LLMResponder. It -// reports the reply as it is generated by calling onDelta with each new -// text fragment, and returns the complete reply when the turn finishes. +// reports the turn's progress -- text fragments and completed tool calls +// -- to stream as it is generated, and returns the complete reply when the +// turn finishes. // // It is optional: a gateway that cannot render partial output, or a // deployment whose provider does not stream, simply leaves it nil and the -// Router falls back to the blocking LLMResponder. onDelta may be called -// from the generating goroutine and must not block for long -- adapters -// should throttle their own network writes rather than stall the stream. -type LLMStreamResponder func(ctx context.Context, msg Message, onDelta func(string)) (string, error) +// Router falls back to the blocking LLMResponder. stream is called from the +// generating goroutine and must not block for long -- adapters should +// throttle their own network writes rather than stall the stream. +type LLMStreamResponder func(ctx context.Context, msg Message, stream TurnStream) (string, error) // ModelManager provides access to available models and allows switching the // active LLM model. The daemon supplies an implementation backed by its @@ -435,8 +436,9 @@ func (r *Router) dispatchSessionCommand(ctx context.Context, msg Message, cmd, r } // RouteStream is Route for adapters that can render a reply progressively. -// onDelta receives each new fragment of an LLM reply as it is generated; -// the complete reply is returned as usual. +// stream receives each new fragment of an LLM reply as it is generated, plus +// each completed tool call, in the order the model produced them; the +// complete reply is returned as usual. // // Only free-text messages stream. Gateway-local commands (/status, /model, // …) answer from local state in a single step, so they return through the @@ -444,15 +446,15 @@ func (r *Router) dispatchSessionCommand(ctx context.Context, msg Message, cmd, r // configured, RouteStream is exactly Route -- callers get the whole reply // at the end and simply never see a delta, so an adapter can always call // RouteStream without checking first. -func (r *Router) RouteStream(ctx context.Context, msg Message, onDelta func(string)) (string, error) { - if r.LLMStream == nil || onDelta == nil { +func (r *Router) RouteStream(ctx context.Context, msg Message, stream TurnStream) (string, error) { + if r.LLMStream == nil || stream == nil { return r.Route(ctx, msg) } cmd, _ := parseCmd(strings.TrimSpace(msg.Text), r.gatewayName) if isLocalCommand(cmd) || strings.HasPrefix(cmd, "/") { return r.Route(ctx, msg) } - reply, err := r.LLMStream(ctx, msg, onDelta) + reply, err := r.LLMStream(ctx, msg, stream) if err == nil { r.maybeAutoTitle(ctx, msg) } diff --git a/internal/gateway/gateway_stream_test.go b/internal/gateway/gateway_stream_test.go index 6a2a946ee..5a5a2d481 100644 --- a/internal/gateway/gateway_stream_test.go +++ b/internal/gateway/gateway_stream_test.go @@ -14,9 +14,9 @@ func TestRouteStreamFallsBackWhenNoStreamResponder(t *testing.T) { }, "telegram") var deltas []string - got, err := r.RouteStream(context.Background(), Message{Text: "hello"}, func(d string) { + got, err := r.RouteStream(context.Background(), Message{Text: "hello"}, DeltaFunc(func(d string) { deltas = append(deltas, d) - }) + })) if err != nil { t.Fatalf("RouteStream: %v", err) } @@ -30,17 +30,17 @@ func TestRouteStreamFallsBackWhenNoStreamResponder(t *testing.T) { func TestRouteStreamStreamsFreeText(t *testing.T) { r := NewRouter(nil, nil, "telegram") - r.LLMStream = func(_ context.Context, _ Message, onDelta func(string)) (string, error) { + r.LLMStream = func(_ context.Context, _ Message, stream TurnStream) (string, error) { for _, d := range []string{"a", "b", "c"} { - onDelta(d) + stream.Delta(d) } return "abc", nil } var sb strings.Builder - got, err := r.RouteStream(context.Background(), Message{Text: "hello"}, func(d string) { + got, err := r.RouteStream(context.Background(), Message{Text: "hello"}, DeltaFunc(func(d string) { sb.WriteString(d) - }) + })) if err != nil { t.Fatalf("RouteStream: %v", err) } @@ -54,12 +54,12 @@ func TestRouteStreamStreamsFreeText(t *testing.T) { func TestRouteStreamDoesNotStreamLocalCommands(t *testing.T) { r := NewRouter(nil, nil, "telegram") streamed := false - r.LLMStream = func(context.Context, Message, func(string)) (string, error) { + r.LLMStream = func(context.Context, Message, TurnStream) (string, error) { streamed = true return "", nil } - if _, err := r.RouteStream(context.Background(), Message{Text: "/status"}, func(string) {}); err != nil { + if _, err := r.RouteStream(context.Background(), Message{Text: "/status"}, DeltaFunc(func(string) {})); err != nil { t.Fatalf("RouteStream: %v", err) } if streamed { @@ -70,12 +70,12 @@ func TestRouteStreamDoesNotStreamLocalCommands(t *testing.T) { func TestRouteStreamRejectsUnknownCommandLocally(t *testing.T) { r := NewRouter(nil, nil, "telegram") streamed := false - r.LLMStream = func(context.Context, Message, func(string)) (string, error) { + r.LLMStream = func(context.Context, Message, TurnStream) (string, error) { streamed = true return "ok", nil } - reply, err := r.RouteStream(context.Background(), Message{Text: "/commands"}, func(string) {}) + reply, err := r.RouteStream(context.Background(), Message{Text: "/commands"}, DeltaFunc(func(string) {})) if err != nil { t.Fatalf("RouteStream: %v", err) } diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index 591dc281e..4c539efcc 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -116,12 +116,12 @@ func TestRouterRejectsUnknownCommand(t *testing.T) { llmCalls++ return "fabricated command behavior", nil }, "test") - r.LLMStream = func(context.Context, Message, func(string)) (string, error) { + r.LLMStream = func(context.Context, Message, TurnStream) (string, error) { llmCalls++ return "fabricated streaming behavior", nil } - reply, err := r.RouteStream(context.Background(), Message{Text: text}, func(string) {}) + reply, err := r.RouteStream(context.Background(), Message{Text: text}, DeltaFunc(func(string) {})) if err != nil { t.Fatalf("RouteStream: %v", err) } diff --git a/internal/gateway/session_titles_test.go b/internal/gateway/session_titles_test.go index 0b8208944..d48636366 100644 --- a/internal/gateway/session_titles_test.go +++ b/internal/gateway/session_titles_test.go @@ -349,8 +349,8 @@ func TestRouteStreamAutoTitlesUntitledSession(t *testing.T) { r.InitSessions(store) gen := &stubTitleGenerator{titles: map[string]string{}} r.Titles = gen - r.LLMStream = func(_ context.Context, _ Message, onDelta func(string)) (string, error) { - onDelta("r") + r.LLMStream = func(_ context.Context, _ Message, stream TurnStream) (string, error) { + stream.Delta("r") return "reply ok", nil } ctx := context.Background() @@ -359,7 +359,7 @@ func TestRouteStreamAutoTitlesUntitledSession(t *testing.T) { t.Fatalf("resolve: %v", err) } gen.titles[sessionID] = "Stream topic" - if _, err := r.RouteStream(ctx, Message{ChannelID: "ch", From: "u", Text: "stream me"}, func(string) {}); err != nil { + if _, err := r.RouteStream(ctx, Message{ChannelID: "ch", From: "u", Text: "stream me"}, DeltaFunc(func(string) {})); err != nil { t.Fatalf("RouteStream: %v", err) } waitForTitle(t, store, sessionID, "Stream topic") diff --git a/internal/gateway/stream.go b/internal/gateway/stream.go new file mode 100644 index 000000000..b92556ff9 --- /dev/null +++ b/internal/gateway/stream.go @@ -0,0 +1,89 @@ +package gateway + +import "strings" + +// toolSummaryMaxRunes bounds a rendered tool summary. Tool output is +// unbounded -- a grep can return thousands of lines -- and the summary is +// one line in a chat transcript, so it is cut rather than allowed to bury the +// answer it precedes. +const toolSummaryMaxRunes = 80 + +// ToolCallEvent reports one completed tool invocation within a turn. It +// carries the raw outcome rather than rendered text: presentation (emoji, +// layout, whether to show it at all) belongs to the channel adapter. +type ToolCallEvent struct { + // Name is the tool the model invoked. + Name string + // Output is what the tool returned, verbatim. + Output string + // Err is non-empty when the tool failed, in which case Output is + // unreliable and usually empty. + Err string +} + +// Summary reduces the outcome to a single short line suitable for an inline +// status entry. It never returns an empty string: a tool that returned +// nothing still ran, and a blank summary would read as a rendering fault. +func (e ToolCallEvent) Summary() string { + if line := firstNonEmptyLine(e.Err); line != "" { + return "failed: " + truncateRunes(line, toolSummaryMaxRunes) + } + if line := firstNonEmptyLine(e.Output); line != "" { + return truncateRunes(line, toolSummaryMaxRunes) + } + return "done" +} + +// TurnStream receives a turn's output as it is produced. +// +// It replaces the plain delta callback so a channel can render tool activity +// in the same ordered pass as the text: both arrive on the generating +// goroutine, in the order the model produced them. Implementations must not +// block -- a slow renderer stalls generation -- and must be safe to call +// from that goroutine only. +// +// Callers that render text alone use DeltaFunc rather than implementing this. +type TurnStream interface { + // Delta appends the next fragment of assistant text. + Delta(text string) + // ToolCall reports a tool invocation that has finished executing. + ToolCall(event ToolCallEvent) +} + +// DeltaFunc adapts a plain text-delta callback to TurnStream for callers that +// render text only. Tool events are discarded. A nil DeltaFunc is inert, so +// DeltaFunc(nil) is a usable "stream nothing" sink. +type DeltaFunc func(string) + +// Delta forwards the fragment to the wrapped callback. +func (f DeltaFunc) Delta(text string) { + if f == nil { + return + } + f(text) +} + +// ToolCall discards the event: a text-only caller has nowhere to put it. +func (f DeltaFunc) ToolCall(ToolCallEvent) {} + +// firstNonEmptyLine returns the first line of s that holds something other +// than whitespace, trimmed. It returns "" when there is no such line. +func firstNonEmptyLine(s string) string { + for line := range strings.SplitSeq(s, "\n") { + if trimmed := strings.TrimSpace(line); trimmed != "" { + return trimmed + } + } + return "" +} + +// truncateRunes shortens s to at most maxRunes runes, marking the cut with an +// ellipsis. Counting runes rather than bytes keeps multi-byte text from being +// cut mid-character. +func truncateRunes(s string, maxRunes int) string { + runes := []rune(s) + if len(runes) <= maxRunes { + return s + } + return string(runes[:maxRunes]) + "…" +} diff --git a/internal/gateway/stream_test.go b/internal/gateway/stream_test.go new file mode 100644 index 000000000..0bc214402 --- /dev/null +++ b/internal/gateway/stream_test.go @@ -0,0 +1,97 @@ +package gateway + +import ( + "strings" + "testing" +) + +func TestToolCallEventSummary(t *testing.T) { + tests := []struct { + name string + event ToolCallEvent + want string + }{ + { + name: "single line output is the summary", + event: ToolCallEvent{Name: "memory_edit", Output: "added entry"}, + want: "added entry", + }, + { + name: "only the first non-empty line is kept", + event: ToolCallEvent{Name: "read", Output: "\n\nfirst line\nsecond line"}, + want: "first line", + }, + { + name: "surrounding whitespace is trimmed", + event: ToolCallEvent{Name: "shell", Output: " exit 0 \n"}, + want: "exit 0", + }, + { + name: "an error replaces the output", + event: ToolCallEvent{Name: "shell", Output: "partial", Err: "exit status 2"}, + want: "failed: exit status 2", + }, + { + name: "a multi-line error is reduced the same way", + event: ToolCallEvent{Name: "shell", Err: "exit status 2\nstack trace"}, + want: "failed: exit status 2", + }, + { + name: "no output at all still reads as a completed call", + event: ToolCallEvent{Name: "write"}, + want: "done", + }, + { + name: "whitespace-only output reads as a completed call", + event: ToolCallEvent{Name: "write", Output: " \n "}, + want: "done", + }, + { + name: "a long line is truncated with an ellipsis", + event: ToolCallEvent{Name: "grep", Output: strings.Repeat("x", toolSummaryMaxRunes+10)}, + want: strings.Repeat("x", toolSummaryMaxRunes) + "…", + }, + { + name: "truncation counts runes, not bytes", + event: ToolCallEvent{Name: "grep", Output: strings.Repeat("é", toolSummaryMaxRunes+10)}, + want: strings.Repeat("é", toolSummaryMaxRunes) + "…", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := tc.event.Summary(); got != tc.want { + t.Fatalf("Summary() = %q, want %q", got, tc.want) + } + }) + } +} + +// A caller that only renders text must be able to keep passing a plain +// callback, and must never be handed a tool event it cannot use. +func TestDeltaFuncIsTextOnly(t *testing.T) { + var got []string + stream := DeltaFunc(func(delta string) { got = append(got, delta) }) + + stream.Delta("one") + stream.ToolCall(ToolCallEvent{Name: "shell", Output: "exit 0"}) + stream.Delta("two") + + want := []string{"one", "two"} + if len(got) != len(want) { + t.Fatalf("deltas = %v, want %v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("deltas = %v, want %v", got, want) + } + } +} + +// RouteStream and TurnRunner accept a nil sink to mean "not streaming", so a +// nil DeltaFunc must be inert rather than a panic waiting for the first token. +func TestDeltaFuncNilIsInert(t *testing.T) { + stream := DeltaFunc(nil) + stream.Delta("anything") + stream.ToolCall(ToolCallEvent{Name: "shell"}) +} diff --git a/internal/gateway/turn.go b/internal/gateway/turn.go index 0b026ff19..65137e9cf 100644 --- a/internal/gateway/turn.go +++ b/internal/gateway/turn.go @@ -25,7 +25,10 @@ type TurnModel interface { type PreparedTurnModel interface { ToolSummaries() []ToolSummary ToolSchemaTokens() int - Generate(ctx context.Context, request TurnModelRequest, onDelta func(string)) (string, error) + // Generate runs the turn, reporting progress to stream as it goes. + // stream is nil when the caller cannot render partial output; an + // implementation must then generate without reporting anything. + Generate(ctx context.Context, request TurnModelRequest, stream TurnStream) (string, error) } // TurnModelRequest is the provider-neutral request assembled by the gateway. @@ -116,13 +119,17 @@ func (r *TurnRunner) Respond(ctx context.Context, msg Message) (string, error) { } // RespondStream adapts the runner to Router.LLMStream. -func (r *TurnRunner) RespondStream(ctx context.Context, msg Message, onDelta func(string)) (string, error) { - return r.Run(ctx, msg, onDelta) +func (r *TurnRunner) RespondStream(ctx context.Context, msg Message, stream TurnStream) (string, error) { + return r.Run(ctx, msg, stream) } -// Run executes one turn. A completed duplicate source message replays its -// durable response without invoking the model again. -func (r *TurnRunner) Run(ctx context.Context, msg Message, onDelta func(string)) (string, error) { +// Run executes one turn, reporting progress to stream. A completed duplicate +// source message replays its durable response without invoking the model +// again; the replay reaches stream as one whole-text fragment, since there is +// no generation left to watch. +// +// stream may be nil, which means the caller renders only the returned reply. +func (r *TurnRunner) Run(ctx context.Context, msg Message, stream TurnStream) (string, error) { if r.Router == nil { return "", fmt.Errorf("chat turn router is not configured") } @@ -165,8 +172,8 @@ func (r *TurnRunner) Run(ctx context.Context, msg Message, onDelta func(string)) } switch claim { case TurnClaimCompleted: - if onDelta != nil && turn.ResponseText != "" { - onDelta(turn.ResponseText) + if stream != nil && turn.ResponseText != "" { + stream.Delta(turn.ResponseText) } return turn.ResponseText, nil case TurnClaimInProgress: @@ -198,8 +205,8 @@ func (r *TurnRunner) Run(ctx context.Context, msg Message, onDelta func(string)) r.Log.Info("replaying the reply to a redelivered message", "session", sessionID, "source_id", msg.SourceID) } - if onDelta != nil { - onDelta(prior) + if stream != nil { + stream.Delta(prior) } return prior, nil } @@ -280,7 +287,7 @@ func (r *TurnRunner) Run(ctx context.Context, msg Message, onDelta func(string)) text, err := prepared.Generate(ctx, TurnModelRequest{ Messages: view.Messages, MaxTokens: modelDetails.MaxOutputTokens, - }, onDelta) + }, stream) if err != nil { return "", r.failTurn(ctx, turn, err) } diff --git a/internal/gateway/turn_stream_test.go b/internal/gateway/turn_stream_test.go new file mode 100644 index 000000000..d7b0d1485 --- /dev/null +++ b/internal/gateway/turn_stream_test.go @@ -0,0 +1,116 @@ +package gateway + +import ( + "context" + "log/slog" + "testing" +) + +// recordingStream captures everything a turn reports, in arrival order, so a +// test can assert that text and tool activity stay interleaved as produced. +type recordingStream struct { + events []string +} + +func (s *recordingStream) Delta(text string) { + s.events = append(s.events, "text:"+text) +} + +func (s *recordingStream) ToolCall(event ToolCallEvent) { + s.events = append(s.events, "tool:"+event.Name+":"+event.Summary()) +} + +func newStreamTestRunner(t *testing.T, prepared *turnTestPreparedModel) *TurnRunner { + t.Helper() + store := NewSessionStoreMemory() + router := NewRouter(nil, nil, "telegram") + router.Identity = "archie" + router.InitSessions(store) + return NewTurnRunner(TurnRunnerConfig{ + Router: router, + Sessions: store, + Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"}, + Personas: NewPersonaRegistry(DefaultPersonas()), + Model: &turnTestModel{prepared: prepared}, + BotUser: "archie", + Channel: "telegram", + Log: slog.New(slog.DiscardHandler), + }) +} + +// A channel that renders tool activity has to receive it in the order the +// model produced it, not as a batch after the answer. +func TestTurnRunnerForwardsToolCallsInOrderWithText(t *testing.T) { + prepared := &turnTestPreparedModel{reply: "done"} + prepared.generateStream = func(stream TurnStream) (string, error) { + stream.Delta("looking") + stream.ToolCall(ToolCallEvent{Name: "shell", Output: "exit 0"}) + stream.Delta(" and answering") + return "looking and answering", nil + } + runner := newStreamTestRunner(t, prepared) + + sink := &recordingStream{} + reply, err := runner.Run(context.Background(), Message{ + From: "user", ChannelID: "chat-1", SourceID: "source-1", Text: "hello", + }, sink) + if err != nil { + t.Fatalf("Run() error = %v", err) + } + if reply != "looking and answering" { + t.Fatalf("reply = %q", reply) + } + + want := []string{"text:looking", "tool:shell:exit 0", "text: and answering"} + if len(sink.events) != len(want) { + t.Fatalf("stream events = %v, want %v", sink.events, want) + } + for i := range want { + if sink.events[i] != want[i] { + t.Fatalf("stream events = %v, want %v", sink.events, want) + } + } +} + +// A redelivered message replays its stored reply without calling the model, +// so the channel gets the whole answer as one fragment and no tool activity. +func TestTurnRunnerReplayStreamsStoredReplyOnce(t *testing.T) { + prepared := &turnTestPreparedModel{reply: "the answer"} + runner := newStreamTestRunner(t, prepared) + msg := Message{From: "user", ChannelID: "chat-1", SourceID: "source-1", Text: "hello"} + + if _, err := runner.Run(context.Background(), msg, DeltaFunc(nil)); err != nil { + t.Fatalf("first Run() error = %v", err) + } + + sink := &recordingStream{} + reply, err := runner.Run(context.Background(), msg, sink) + if err != nil { + t.Fatalf("replay Run() error = %v", err) + } + if reply != "the answer" { + t.Fatalf("replayed reply = %q", reply) + } + if len(sink.events) != 1 || sink.events[0] != "text:the answer" { + t.Fatalf("replay stream events = %v, want one full-text delta", sink.events) + } + if prepared.generateN != 1 { + t.Fatalf("Generate calls = %d, want 1 -- a replay must not call the model", prepared.generateN) + } +} + +// A nil sink means "not streaming": the turn still runs and returns its reply. +func TestTurnRunnerAcceptsNoStream(t *testing.T) { + prepared := &turnTestPreparedModel{reply: "quiet answer"} + runner := newStreamTestRunner(t, prepared) + + reply, err := runner.Run(context.Background(), Message{ + From: "user", ChannelID: "chat-1", SourceID: "source-1", Text: "hello", + }, nil) + if err != nil { + t.Fatalf("Run() error = %v", err) + } + if reply != "quiet answer" { + t.Fatalf("reply = %q", reply) + } +} diff --git a/internal/gateway/turn_test.go b/internal/gateway/turn_test.go index 9c5bbe6e6..4d235b01a 100644 --- a/internal/gateway/turn_test.go +++ b/internal/gateway/turn_test.go @@ -33,7 +33,10 @@ type turnTestPreparedModel struct { generateN int generateErr error generate func(context.Context) (string, error) - lastRequest TurnModelRequest + // generateStream lets a test drive the reported progress -- text and + // tool activity -- rather than only the returned reply. + generateStream func(TurnStream) (string, error) + lastRequest TurnModelRequest } func (m *turnTestPreparedModel) ToolSummaries() []ToolSummary { @@ -44,17 +47,20 @@ func (m *turnTestPreparedModel) ToolSchemaTokens() int { return m.toolTokens } -func (m *turnTestPreparedModel) Generate(ctx context.Context, req TurnModelRequest, onDelta func(string)) (string, error) { +func (m *turnTestPreparedModel) Generate(ctx context.Context, req TurnModelRequest, stream TurnStream) (string, error) { m.generateN++ m.lastRequest = req + if m.generateStream != nil { + return m.generateStream(stream) + } if m.generate != nil { return m.generate(ctx) } if m.generateErr != nil { return "", m.generateErr } - if onDelta != nil { - onDelta(m.reply) + if stream != nil { + stream.Delta(m.reply) } return m.reply, nil } @@ -99,7 +105,7 @@ func TestTurnRunnerPersistsOneTurnAndPublishesCompletion(t *testing.T) { ChannelID: "chat-1", SourceID: "source-1", Text: "hello", - }, func(delta string) { streamed += delta }) + }, DeltaFunc(func(delta string) { streamed += delta })) if err != nil { t.Fatalf("Run() error = %v", err) } @@ -342,7 +348,7 @@ func TestTurnRunnerReplaysCompletedDuplicateWithoutGenerating(t *testing.T) { t.Fatalf("first Run() error = %v", err) } var replay string - reply, err := runner.Run(context.Background(), msg, func(delta string) { replay += delta }) + reply, err := runner.Run(context.Background(), msg, DeltaFunc(func(delta string) { replay += delta })) if err != nil { t.Fatalf("duplicate Run() error = %v", err) } diff --git a/internal/webui/api_chat.go b/internal/webui/api_chat.go index 74ef52964..e5c1e66a1 100644 --- a/internal/webui/api_chat.go +++ b/internal/webui/api_chat.go @@ -201,6 +201,32 @@ func (s *Server) handleChatMessage(w http.ResponseWriter, r *http.Request) { writeJSON(w, map[string]string{"reply": reply, "session_id": sessionID}) } +// chatStreamEvent is one `data: {...}` frame of the chat stream. A tool frame +// names the tool in Tool and carries its one-line outcome in Text, so the +// browser can style the two apart instead of parsing a rendered string. +// +// Text is always emitted, empty or not: the browser concatenates it and +// assigns it, so a missing key would put the string "undefined" into the +// transcript on a turn whose reply is empty. +type chatStreamEvent struct { + Type string `json:"type"` + Text string `json:"text"` + Tool string `json:"tool,omitempty"` + SessionID string `json:"session_id,omitempty"` +} + +// 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}) +} + +func (f chatStreamSink) ToolCall(event gateway.ToolCallEvent) { + f(chatStreamEvent{Type: "tool", Tool: event.Name, Text: event.Summary()}) +} + func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) { chat, ok := s.chatReady(w) if !ok { @@ -218,47 +244,52 @@ func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") w.Header().Set("X-Accel-Buffering", "no") - writeChatEvent := func(kind, text, sessionID string) { - payload, _ := json.Marshal(map[string]string{"type": kind, "text": text, "session_id": sessionID}) + writeChatEvent := func(event chatStreamEvent, sessionID string) { + event.SessionID = sessionID + payload, _ := json.Marshal(event) _, _ = fmt.Fprintf(w, "data: %s\n\n", payload) flusher.Flush() } sessionID, err := chat.Router.ResolveSessionKey(r.Context(), msg) if err != nil { - writeChatEvent("error", err.Error(), "") + writeChatEvent(chatStreamEvent{Type: "error", Text: err.Error()}, "") return } - writeChatEvent("started", "", sessionID) + writeChatEvent(chatStreamEvent{Type: "started"}, sessionID) var reply string if chat.Turns == nil { - reply, err = chat.Router.RouteStream(r.Context(), msg, func(delta string) { - writeChatEvent("delta", delta, sessionID) - }) + reply, err = chat.Router.RouteStream(r.Context(), msg, chatStreamSink(func(event chatStreamEvent) { + writeChatEvent(event, sessionID) + })) } else { type turnResult struct { reply string err error } - events := make(chan string, 32) + // One channel carries both text and tool events, because the + // order between them is the point: a tool line that overtakes + // the sentence it interrupted describes a turn that never + // happened. + 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, func(delta string) { + turnReply, turnErr := chat.Router.RouteStream(turnCtx, msg, chatStreamSink(func(event chatStreamEvent) { select { - case events <- delta: + case events <- event: case <-r.Context().Done(): } - }) + })) result <- turnResult{reply: turnReply, err: turnErr} }) for { select { - case delta := <-events: - writeChatEvent("delta", delta, sessionID) + case event := <-events: + writeChatEvent(event, sessionID) case turn := <-result: for { select { - case delta := <-events: - writeChatEvent("delta", delta, sessionID) + case event := <-events: + writeChatEvent(event, sessionID) default: goto drained } @@ -274,13 +305,13 @@ func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) { turnDone: } if err != nil { - writeChatEvent("error", err.Error(), sessionID) + writeChatEvent(chatStreamEvent{Type: "error", Text: err.Error()}, sessionID) return } if resolved, resolveErr := chat.Router.ResolveSessionKey(r.Context(), msg); resolveErr == nil { sessionID = resolved } - writeChatEvent("done", reply, sessionID) + writeChatEvent(chatStreamEvent{Type: "done", Text: reply}, sessionID) } func (s *Server) handleChatCancel(w http.ResponseWriter, r *http.Request) { diff --git a/internal/webui/api_chat_test.go b/internal/webui/api_chat_test.go index 3b830475b..2da679aa7 100644 --- a/internal/webui/api_chat_test.go +++ b/internal/webui/api_chat_test.go @@ -513,6 +513,7 @@ func TestChatSessionsFiltersNonWeb(t *testing.T) { type chatSSEEvent struct { Type string `json:"type"` Text string `json:"text"` + Tool string `json:"tool"` SessionID string `json:"session_id"` } @@ -570,9 +571,9 @@ func TestChatStreamEndsWithDone(t *testing.T) { func TestChatStreamDeltas(t *testing.T) { server, sessions := chatTestServer(t) var gotDeltas []string - server.Chat.Router.LLMStream = func(_ context.Context, _ gateway.Message, onDelta func(string)) (string, error) { - onDelta("part one") - onDelta("part two") + server.Chat.Router.LLMStream = func(_ context.Context, _ gateway.Message, stream gateway.TurnStream) (string, error) { + stream.Delta("part one") + stream.Delta("part two") return "full reply", nil } saveWebSession(t, sessions, "web-1") @@ -598,6 +599,72 @@ func TestChatStreamDeltas(t *testing.T) { } } +// Tool activity has to reach the browser interleaved with the text that +// surrounds it, on both stream paths: without a turn queue the responder +// writes straight to the SSE writer, with one it hands events across a +// goroutine, and an ordering that survives only the first is not a fix. +func TestChatStreamReportsToolCalls(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\nignored trailing line"}) + turn.ToolCall(gateway.ToolCallEvent{Name: "read", Err: "no such file"}) + turn.Delta(" and answering") + return "checking and answering", nil + } + + tests := []struct { + name string + queued bool + }{ + {name: "direct"}, + {name: "queued", queued: true}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + server, sessions := chatTestServer(t) + server.Chat.Router.LLMStream = stream + if tc.queued { + server.Chat.Turns = gateway.NewTurns(slog.Default()) + } + 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) + } + + var got []string + for _, ev := range parseChatSSE(t, res.Body.String()) { + switch ev.Type { + case "delta": + got = append(got, "text:"+ev.Text) + case "tool": + got = append(got, "tool:"+ev.Tool+":"+ev.Text) + } + } + want := []string{ + "text:checking", + "tool:shell:exit 0", + "tool:read:failed: no such file", + "text: and answering", + } + if len(got) != len(want) { + t.Fatalf("stream events = %v, want %v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("stream events = %v, want %v", got, want) + } + } + }) + } +} + // 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. diff --git a/ui/dist/assets/index.css b/ui/dist/assets/index.css index e3e63e061..9ed8fc69d 100644 --- a/ui/dist/assets/index.css +++ b/ui/dist/assets/index.css @@ -1 +1 @@ -:root{--font-sans:ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;--font-mono:ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;--text-xs:.6875rem;--text-sm:.8125rem;--text-base:.875rem;--text-lg:1.0625rem;--text-xl:1.375rem;--text-2xl:1.875rem;--text-3xl:2.5rem;--sp-1:.25rem;--sp-2:.5rem;--sp-3:.75rem;--sp-4:1rem;--sp-5:1.5rem;--sp-6:2rem;--sp-7:3rem;--radius-sm:6px;--radius-md:10px;--radius-lg:14px;--radius-xl:20px;--radius-pill:999px;--transition:.14s cubic-bezier(.4, 0, .2, 1)}[data-theme=dark]{--bg:#0d1014;--bg-raised:#141920;--surface:#171d25;--surface-hover:#1d242e;--border:#232b36;--border-strong:#303a48;--fg:#e8edf4;--fg-muted:#8b97a8;--fg-subtle:#5d6878;--accent:#4c8dff;--accent-fg:#fff;--accent-soft:#4c8dff24;--ok:#3fbf7f;--ok-soft:#3fbf7f24;--warn:#e8a33d;--warn-soft:#e8a33d24;--danger:#f0616d;--danger-soft:#f0616d24;--info:#4c8dff;--info-soft:#4c8dff24;--idle:#6b7688;--idle-soft:#6b768824;--accent-2:#7b5cff;--bg-blur:#1419208c;--surface-glass:#171d259e;--surface-glass-strong:#141920c7;--workspace:#11151b8c;--hairline:#ffffff0f;--wash-a:#4c8dff1a;--wash-b:#7b5cff1f;--wash-c:#3fbf7f0f;--shadow-workspace:0 24px 70px #00000080;--shadow-sm:0 1px 2px #0006;--shadow-md:0 8px 28px #0000006b}[data-theme=light]{--bg:#f6f8fb;--bg-raised:#fff;--surface:#fff;--surface-hover:#f1f4f9;--border:#e2e8f0;--border-strong:#cbd5e1;--fg:#16202e;--fg-muted:#5a6779;--fg-subtle:#8c99ab;--accent:#2563eb;--accent-fg:#fff;--accent-soft:#2563eb1a;--ok:#10935c;--ok-soft:#10935c1f;--warn:#b87514;--warn-soft:#b875141f;--danger:#d4344a;--danger-soft:#d4344a1f;--info:#2563eb;--info-soft:#2563eb1a;--idle:#64748b;--idle-soft:#64748b1f;--accent-2:#7c3aed;--bg-blur:#fff9;--surface-glass:#ffffffb8;--surface-glass-strong:#ffffffe0;--workspace:#ffffff8c;--hairline:#0f172a0f;--wash-a:#2563eb1a;--wash-b:#7c3aed1f;--wash-c:#10935c12;--shadow-workspace:0 24px 70px #0f172a24;--shadow-sm:0 1px 2px #0f172a0f;--shadow-md:0 8px 28px #0f172a1a}*,:before,:after{box-sizing:border-box}html,body{height:100%;margin:0}body{color:var(--fg);font-family:var(--font-sans);font-size:var(--text-base);-webkit-font-smoothing:antialiased;line-height:1.5}a{color:inherit;text-decoration:none}body{background:radial-gradient(70rem 40rem at 8% -10%, var(--wash-a), transparent 62%), radial-gradient(56rem 34rem at 92% 4%, var(--wash-b), transparent 58%), radial-gradient(48rem 30rem at 50% 108%, var(--wash-c), transparent 60%), var(--bg);padding:var(--sp-4);background-attachment:fixed}.shell{min-height:calc(100vh - var(--sp-4) * 2);border-radius:var(--radius-xl);border:1px solid var(--hairline);background:var(--workspace);-webkit-backdrop-filter:blur(28px)saturate(120%);backdrop-filter:blur(28px)saturate(120%);box-shadow:var(--shadow-workspace);flex-direction:column;display:flex;overflow:hidden}.topbar{align-items:center;gap:var(--sp-4);padding:var(--sp-3) var(--sp-4);border-bottom:1px solid var(--hairline);background:var(--bg-blur);z-index:10;display:flex;position:sticky;top:0}.brand{align-items:center;gap:var(--sp-2);font-weight:600;font-size:var(--text-sm);letter-spacing:-.01em;flex:none;display:flex}.brand-mark{background:linear-gradient(135deg, var(--accent), var(--accent-2));color:#fff;width:26px;height:26px;font-size:var(--text-xs);border-radius:8px;place-items:center;font-weight:700;display:grid}.nav{border-radius:var(--radius-pill);background:var(--surface-glass-strong);border:1px solid var(--hairline);align-items:center;gap:2px;margin:0 auto;padding:3px;display:flex}.nav-item{align-items:center;gap:var(--sp-2);padding:var(--sp-2) var(--sp-3);border-radius:var(--radius-pill);color:var(--fg-muted);font-size:var(--text-sm);white-space:nowrap;cursor:pointer;transition:background var(--transition), color var(--transition);display:inline-flex}.nav-item:hover{color:var(--fg);background:var(--surface-hover)}.nav-item[aria-current=page]{background:var(--accent);color:var(--accent-fg);font-weight:500}.nav-item .nav-label{display:inline}.topbar-end{align-items:center;gap:var(--sp-2);flex:none;display:flex}.topbar-search{align-items:center;gap:var(--sp-2);padding:var(--sp-2) var(--sp-3);border-radius:var(--radius-pill);background:var(--surface-glass-strong);border:1px solid var(--hairline);color:var(--fg-subtle);display:flex}.topbar-search input{color:var(--fg);font:inherit;font-size:var(--text-sm);background:0 0;border:0;outline:none;width:8rem}.topbar-search input::placeholder{color:var(--fg-subtle)}.mobile-search-toggle{display:none}.icon-btn{border-radius:var(--radius-pill);border:1px solid var(--hairline);background:var(--surface-glass-strong);width:32px;height:32px;color:var(--fg-muted);cursor:pointer;transition:color var(--transition), background var(--transition);place-items:center;display:grid}.icon-btn:hover{color:var(--fg);background:var(--surface-hover)}.avatar{border-radius:var(--radius-pill);width:32px;height:32px;font-size:var(--text-xs);color:#fff;background:linear-gradient(135deg, var(--accent), var(--accent-2));cursor:pointer;flex:none;place-items:center;font-weight:600;display:grid}.ico{flex:none;display:block}.main{padding:var(--sp-6);flex:1;width:100%;overflow-x:hidden}@media (width<=1220px) and (width>=901px){.topbar{flex-wrap:wrap}.nav{border-radius:var(--radius-lg);flex-wrap:wrap;order:3;justify-content:center;width:100%;margin:0}}@media (width<=900px){body{padding:0}.shell{min-height:100vh;box-shadow:none;border:0;border-radius:0}.topbar{padding:var(--sp-3);gap:var(--sp-2);flex-wrap:wrap}.nav{border-radius:var(--radius-lg);scrollbar-width:none;flex-wrap:nowrap;order:3;justify-content:flex-start;width:100%;overflow-x:auto}.nav::-webkit-scrollbar{display:none}.nav-item .nav-label{display:inline}.topbar-search{background:0 0;border:0;padding:0;overflow:hidden}.topbar-search-icon{display:none}.mobile-search-toggle{display:grid}.topbar-search input{opacity:0;pointer-events:none;width:0;transition:width var(--transition), opacity var(--transition);padding:0}.topbar-search.is-open,.topbar-search:focus-within{padding-right:var(--sp-2);border:1px solid var(--hairline);background:var(--surface-glass-strong)}.topbar-search.is-open input,.topbar-search:focus-within input{opacity:1;pointer-events:auto;width:min(9rem,34vw)}.main{padding:var(--sp-4)}}.page-head{justify-content:space-between;align-items:flex-start;gap:var(--sp-5);margin-bottom:var(--sp-6);flex-wrap:wrap;display:flex}.page-title{font-size:var(--text-2xl);letter-spacing:-.02em;margin:0;font-weight:600}.page-sub{color:var(--fg-muted);font-size:var(--text-sm);margin:var(--sp-1) 0 0}.page-actions{gap:var(--sp-2);align-items:center;display:flex}.btn{align-items:center;gap:var(--sp-2);padding:var(--sp-2) var(--sp-4);border-radius:var(--radius-pill);border:1px solid var(--border-strong);background:var(--surface);color:var(--fg);font:inherit;font-size:var(--text-sm);cursor:pointer;transition:background var(--transition), border-color var(--transition);display:inline-flex}.btn:hover{background:var(--surface-hover)}.btn-primary{background:var(--accent);border-color:var(--accent);color:var(--accent-fg);font-weight:500}.btn-primary:hover{filter:brightness(1.08);background:var(--accent)}.btn-icon{padding:var(--sp-2);border-radius:var(--radius-sm)}.card{background:var(--surface-glass);-webkit-backdrop-filter:blur(14px);backdrop-filter:blur(14px);border:1px solid var(--hairline);border-radius:var(--radius-lg);padding:var(--sp-5);box-shadow:var(--shadow-sm);overflow-x:auto}.card-head{justify-content:space-between;align-items:center;gap:var(--sp-3);margin-bottom:var(--sp-4);flex-wrap:wrap;display:flex}.card-title{font-size:var(--text-base);margin:0;font-weight:600}.card-sub{font-size:var(--text-xs);color:var(--fg-muted);margin:var(--sp-1) 0 0}.grid{gap:var(--sp-4);display:grid}.grid>*{min-width:0}.grid-4{grid-template-columns:repeat(auto-fit,minmax(220px,1fr))}.grid-2{grid-template-columns:repeat(auto-fit,minmax(340px,1fr))}.pill{align-items:center;gap:var(--sp-1);padding:2px var(--sp-2);border-radius:var(--radius-pill);font-size:var(--text-xs);white-space:nowrap;font-weight:500;display:inline-flex}.pill:before{content:"";background:currentColor;border-radius:50%;width:5px;height:5px}.pill-ok{color:var(--ok);background:var(--ok-soft)}.pill-warn{color:var(--warn);background:var(--warn-soft)}.pill-danger{color:var(--danger);background:var(--danger-soft)}.pill-info{color:var(--info);background:var(--info-soft)}.pill-idle{color:var(--idle);background:var(--idle-soft)}.table{border-collapse:collapse;width:100%;font-size:var(--text-sm)}.table-scroll{-webkit-overflow-scrolling:touch;min-width:0;max-width:100%;overflow-x:auto}.table-scroll>.table{min-width:42rem}.table th{text-align:left;font-weight:500;font-size:var(--text-xs);text-transform:uppercase;letter-spacing:.05em;color:var(--fg-subtle);padding:0 var(--sp-3) var(--sp-3);border-bottom:1px solid var(--border)}.table td{padding:var(--sp-3);border-bottom:1px solid var(--border);color:var(--fg-muted)}.table tbody tr:last-child td{border-bottom:0}.table tbody tr:hover td{background:var(--surface-hover)}.table td.strong{color:var(--fg);font-weight:500}.mono{font-family:var(--font-mono);font-size:.95em}.empty{text-align:center;padding:var(--sp-7) var(--sp-4);color:var(--fg-muted)}.empty-title{color:var(--fg);margin-bottom:var(--sp-1);font-weight:500}.skeleton{background:linear-gradient(90deg, var(--surface) 25%, var(--surface-hover) 50%, var(--surface) 75%);border-radius:var(--radius-sm);background-size:200% 100%;animation:1.4s infinite shimmer}@keyframes shimmer{to{background-position:-200% 0}}@media (prefers-reduced-motion:reduce){*,:before,:after{transition-duration:.01ms!important;animation-duration:.01ms!important}}.nav-soon{font-size:var(--text-xs);color:var(--fg-subtle);background:var(--surface-hover);padding:1px var(--sp-2);border-radius:var(--radius-pill);margin-left:auto}.nav-item[aria-disabled]{opacity:.55;cursor:default}.nav-item[aria-disabled]:hover{color:var(--fg-muted);background:0 0}.stat-head{justify-content:space-between;align-items:center;gap:var(--sp-2);display:flex}.stat-label{font-size:var(--text-sm);color:var(--fg-muted)}.stat-trend{font-size:var(--text-xs);align-items:center;gap:3px;font-weight:500;display:inline-flex}.stat-trend.up{color:var(--ok)}.stat-trend.down{color:var(--danger)}.stat-value{font-size:var(--text-3xl);letter-spacing:-.03em;margin:var(--sp-2) 0 0;font-weight:600}.stat-unit{font-size:var(--text-lg);color:var(--fg-muted);margin-left:var(--sp-1)}.stat-compare{font-size:var(--text-xs);color:var(--fg-subtle);margin-top:var(--sp-1)}.spark{width:100%;height:34px;margin-top:var(--sp-3);display:block}.setup{margin-bottom:var(--sp-4)}.setup-pct{font-size:var(--text-lg);color:var(--accent);font-weight:600}.setup-complete-actions{align-items:center;gap:var(--sp-2);display:flex}.setup-dismiss{width:28px;height:28px;font-size:var(--text-lg);line-height:1}.setup-done .setup-pct{color:var(--ok)}.setup-bar{border-radius:var(--radius-pill);background:var(--border);height:5px;margin-bottom:var(--sp-4);overflow:hidden}.setup-bar-fill{background:var(--accent);border-radius:var(--radius-pill);height:100%;transition:width var(--transition)}.setup-list{gap:var(--sp-3);flex-direction:column;margin:0;padding:0;list-style:none;display:flex}.setup-step{gap:var(--sp-3);align-items:flex-start;display:flex}.setup-check{border:1.5px solid var(--border-strong);width:18px;height:18px;font-size:var(--text-xs);color:var(--fg-subtle);border-radius:50%;flex:none;place-items:center;margin-top:1px;display:grid}.setup-step.done .setup-check{background:var(--ok-soft);color:var(--ok);border-color:#0000}.setup-step-title{font-size:var(--text-sm)}.setup-step.done .setup-step-title{color:var(--fg-muted);text-decoration:line-through}.setup-step-detail{font-size:var(--text-xs);color:var(--fg-subtle);margin-top:2px}.hero{justify-content:space-between;align-items:flex-start;gap:var(--sp-5);margin-bottom:var(--sp-5);flex-wrap:wrap;display:flex}.hero-title{font-size:var(--text-3xl);letter-spacing:-.03em;margin:0;font-weight:600}.hero-sub{color:var(--fg-muted);font-size:var(--text-sm);margin:var(--sp-2) 0 0}.hero-actions{gap:var(--sp-2);flex-wrap:wrap;align-items:center;display:flex}.dash-top{gap:var(--sp-4);margin-bottom:var(--sp-4);grid-template-columns:minmax(0,1.6fr) minmax(0,1fr);display:grid}.dash-top>*,.dash-split>*{min-width:0}.dash-top-standalone{grid-template-columns:minmax(0,1fr)}@media (width<=1080px){.dash-top{grid-template-columns:minmax(0,1fr)}}.gauge-wrap{place-items:center;display:grid;position:relative}.gauge{width:100%;max-width:200px;display:block}.gauge-value{transition:stroke-dashoffset .6s cubic-bezier(.4,0,.2,1)}.gauge-center{text-align:center;position:absolute;top:52%;transform:translateY(-50%)}.gauge-pct{font-size:var(--text-2xl);letter-spacing:-.03em;font-weight:600}.gauge-label{font-size:var(--text-xs);color:var(--fg-muted)}.pulse-list{margin:var(--sp-4) 0 0;gap:var(--sp-2);flex-direction:column;padding:0;list-style:none;display:flex}.pulse-item{justify-content:space-between;align-items:center;gap:var(--sp-3);font-size:var(--text-sm);padding:var(--sp-2) var(--sp-3);border-radius:var(--radius-sm);background:var(--bg-raised);display:flex}.pulse-name{color:var(--fg-muted);text-overflow:ellipsis;white-space:nowrap;overflow:hidden}.segbar{border-radius:var(--radius-pill);background:var(--border);gap:2px;height:10px;display:flex;overflow:hidden}.segbar-part{height:100%;display:block}.segbar-ok{background:var(--ok)}.segbar-warn{background:var(--warn)}.segbar-danger{background:var(--danger)}.segbar-info{background:var(--info)}.segbar-idle{background:var(--idle)}.segbar-legend{gap:var(--sp-4);margin-top:var(--sp-3);flex-wrap:wrap;display:flex}.segbar-key{align-items:center;gap:var(--sp-2);font-size:var(--text-xs);color:var(--fg-muted);display:inline-flex}.segbar-dot{border-radius:50%;width:8px;height:8px}.forecast-value{font-size:var(--text-3xl);letter-spacing:-.03em;margin-bottom:var(--sp-1);font-weight:600}.forecast-note{font-size:var(--text-xs);color:var(--fg-subtle);margin-bottom:var(--sp-4)}.dash-split{gap:var(--sp-4);margin-top:var(--sp-4);grid-template-columns:minmax(0,1fr) minmax(0,1.5fr);display:grid}@media (width<=1080px){.dash-split{grid-template-columns:minmax(0,1fr)}}.stat-card{position:relative;overflow:hidden}.dash-section{margin-bottom:var(--sp-5)}.dash-section-head{align-items:baseline;gap:var(--sp-3);margin-bottom:var(--sp-3);padding-left:var(--sp-1);display:flex}.dash-section-title{font-size:var(--text-xs);text-transform:uppercase;letter-spacing:.08em;color:var(--fg-subtle);margin:0;font-weight:600}.dash-section-note{font-size:var(--text-xs);color:var(--fg-subtle)}.hero-actions-inner{gap:var(--sp-2);flex-wrap:wrap;align-items:center;display:flex}.btn-attention{border-color:var(--warn);color:var(--warn);background:var(--warn-soft);font-weight:500}.btn-attention:hover{background:var(--warn-soft);filter:brightness(1.1)}.task-summary{margin-bottom:var(--sp-5)}.task-filters{gap:var(--sp-2);flex-wrap:wrap;align-items:center;display:flex}.task-search,.task-filter{font:inherit;font-size:var(--text-sm);color:var(--fg);background:var(--surface);border:1px solid var(--border-strong);border-radius:var(--radius-sm);padding:var(--sp-2) var(--sp-3)}.task-search{width:220px}.task-search:focus,.task-filter:focus{outline:2px solid var(--accent-soft);outline-offset:1px}.task-row{cursor:pointer}.task-expand{width:1.5rem;height:1.5rem;margin-right:var(--sp-2);color:var(--fg-muted);border:1px solid var(--border);font:inherit;cursor:pointer;background:0 0;border-radius:50%;padding:0;line-height:1}.task-expand:hover,.task-expand:focus-visible{color:var(--fg);border-color:var(--accent)}.activity-row{cursor:pointer}.activity-row:hover td,.activity-row:focus td{background:var(--surface-hover)}.activity-row:focus{outline:2px solid var(--accent);outline-offset:-2px}.task-source-link{color:var(--accent);text-decoration:none}.task-source-link:hover{text-decoration:underline}.task-actions{gap:var(--sp-1);white-space:nowrap;flex-wrap:wrap;display:flex}.btn-small{padding:var(--sp-1) var(--sp-2);font-size:var(--text-xs)}.btn-quiet{color:var(--fg-muted)}.task-action-error{max-width:180px;color:var(--danger);font-size:var(--text-xs);white-space:normal}.task-decision{margin-bottom:var(--sp-4);padding:var(--sp-3);border-left:3px solid var(--accent);background:var(--surface)}.task-detail-actions{justify-content:space-between;align-items:center;gap:var(--sp-3);margin-bottom:var(--sp-4);padding-bottom:var(--sp-3);border-bottom:1px solid var(--hairline);display:flex}.task-decision-title{color:var(--fg);font-weight:600}.task-decision-plan{margin-top:var(--sp-2);color:var(--fg-muted);white-space:pre-wrap}@media (width<=700px){.task-filters,.task-search{width:100%}.task-filter{flex:100%}.table-scroll:has(.task-row){overflow:visible}.table:has(.task-row){display:block}.table:has(.task-row) tbody{display:block}.table-scroll>.table:has(.task-row){width:100%;min-width:0}.table:has(.task-row) thead{display:none}.task-row{gap:var(--sp-2) var(--sp-4);margin-bottom:var(--sp-3);padding:var(--sp-3);border:1px solid var(--hairline);border-radius:var(--radius-lg);background:var(--surface-glass);grid-template-columns:1fr 1fr;display:grid}.task-row td{justify-content:space-between;align-items:center;gap:var(--sp-2);padding:var(--sp-1) 0;text-align:right;border:0;display:flex}.task-row td:before{content:attr(data-label);color:var(--fg-subtle);font-size:var(--text-xs);text-align:left;font-weight:500}.task-row td:nth-child(3),.task-row td:last-child{grid-column:1/-1}.task-row td:last-child{align-items:flex-start}.task-timeline-row,.task-timeline-row td{width:100%;display:block}.task-timeline-row td{margin:calc(var(--sp-2) * -1) 0 var(--sp-4);border-radius:0 0 var(--radius-lg) var(--radius-lg)}.task-detail-actions{flex-direction:column;align-items:flex-start}}@media (width<=480px){.task-row{grid-template-columns:1fr}.task-row td:nth-child(3),.task-row td:last-child{grid-column:auto}}.task-timeline-row td{background:var(--bg-raised);padding:var(--sp-4)}.task-timeline-loading{color:var(--fg-muted);font-size:var(--text-sm)}.task-timeline-error{align-items:flex-start;gap:var(--sp-2);flex-direction:column;display:flex}.task-timeline-error .task-timeline-title{color:var(--fg);font-weight:500}.task-timeline{gap:var(--sp-3);flex-direction:column;margin:0;padding:0;list-style:none;display:flex}.task-timeline-entry{gap:var(--sp-3);align-items:flex-start;display:flex}.task-timeline-dot{background:var(--accent);border-radius:50%;flex:none;width:8px;height:8px;margin-top:5px}.task-timeline-kind{font-size:var(--text-sm);color:var(--fg);font-weight:500}.task-timeline-detail{font-size:var(--text-xs);color:var(--fg-muted);margin-top:2px}.task-timeline-when{font-size:var(--text-xs);color:var(--fg-subtle);margin-top:2px}.skill-search{font:inherit;font-size:var(--text-sm);color:var(--fg);background:var(--surface);border:1px solid var(--border-strong);border-radius:var(--radius-sm);padding:var(--sp-2) var(--sp-3);width:220px}.skill-search:focus{outline:2px solid var(--accent-soft);outline-offset:1px}.skill-card{flex-direction:column;display:flex}.skill-desc{color:var(--fg-muted);font-size:var(--text-sm);margin:0 0 var(--sp-3);flex:1}.skill-source{font-size:var(--text-xs);color:var(--fg-subtle)}.wf-stage-card{margin-top:var(--sp-4)}.wf-rate{font-size:var(--text-sm);color:var(--fg);align-items:baseline;gap:var(--sp-2);display:flex}.wf-rate-sub{font-size:var(--text-xs);color:var(--fg-subtle)}.wf-bar{border-radius:var(--radius-pill);background:var(--border);height:5px;margin-top:var(--sp-2);width:140px;overflow:hidden}.wf-bar-fill{border-radius:var(--radius-pill);height:100%;transition:width var(--transition)}.wf-bar-ok{background:var(--ok)}.wf-bar-warn{background:var(--warn)}.wf-bar-danger{background:var(--danger)}.wf-bar-info{background:var(--info)}.wf-form{gap:var(--sp-2);flex-direction:column;display:flex}.channel-card{gap:var(--sp-1);flex-direction:column;display:flex}.channel-desc{color:var(--fg-muted);font-size:var(--text-sm);margin:0}.channel-detail{color:var(--fg-subtle);font-size:var(--text-xs);margin:0}.cfg-page{max-width:70rem;margin:0 auto}.cfg-section{margin-bottom:var(--sp-4)}.cfg-subhead{font-size:var(--text-sm);color:var(--fg-muted);margin:var(--sp-5) 0 var(--sp-2);font-weight:600}.cfg-subhead:first-child{margin-top:0}.kv-list{column-gap:var(--sp-4);grid-template-columns:max-content minmax(0,1fr) auto;row-gap:0;display:grid}.kv{grid-template-columns:subgrid;align-items:center;row-gap:var(--sp-1);padding:var(--sp-2) 0;border-radius:var(--radius-sm);grid-column:1/-1;display:grid}@supports not (grid-template-columns:subgrid){.kv-list{flex-direction:column;width:auto;display:flex}.kv{column-gap:var(--sp-4);grid-template-columns:minmax(8rem,16rem) minmax(0,1fr) auto}}.kv+.kv{border-top:1px solid var(--border)}.kv:hover{background:var(--surface-hover)}.kv-label{font-size:var(--text-sm);color:var(--fg-muted)}.kv-value{min-width:0;font-size:var(--text-sm);color:var(--fg);text-overflow:ellipsis;white-space:nowrap;overflow:hidden}.kv-value.is-empty{color:var(--fg-subtle)}.kv-actions{align-items:center;gap:var(--sp-2);justify-self:end;display:flex}.kv-note{font-size:var(--text-xs);color:var(--fg-subtle);grid-column:2/-1}.kv-note.is-error{color:var(--danger)}.kv.is-locked .kv-value{color:var(--fg-muted)}.kv-btn{font:inherit;font-size:var(--text-xs);color:var(--fg-subtle);border:1px solid var(--border);border-radius:var(--radius-sm);padding:2px var(--sp-2);cursor:pointer;background:0 0;flex:none;transition:color .12s,border-color .12s,background .12s}.kv:hover .kv-btn,.kv:focus-within .kv-btn{color:var(--fg-muted);border-color:var(--border-strong)}.kv-btn:hover{color:var(--fg);border-color:var(--accent);background:var(--accent-soft)}.kv-btn:focus-visible{outline:2px solid var(--accent);outline-offset:1px}.kv-flag{font-size:var(--text-xs);color:var(--warn);background:var(--warn-soft);border-radius:var(--radius-pill);padding:1px var(--sp-2);flex:none}.kv-input{font:inherit;font-size:var(--text-sm);color:var(--fg);background:var(--bg,var(--surface));border:1px solid var(--border-strong);border-radius:var(--radius-sm);padding:var(--sp-1) var(--sp-2);width:100%;min-width:0}.kv-input:focus{outline:2px solid var(--accent);outline-offset:-1px}.kv-save,.kv-cancel{font:inherit;font-size:var(--text-xs);border-radius:var(--radius-sm);padding:var(--sp-1) var(--sp-2);cursor:pointer;flex:none}.kv-save{background:var(--accent);border:1px solid var(--accent);color:var(--accent-fg);font-weight:500}.kv-save:disabled{opacity:.6;cursor:default}.kv-cancel{border:1px solid var(--border-strong);color:var(--fg-muted);background:0 0}.kv-cancel:hover{color:var(--fg)}.cfg-notice{border-left:3px solid var(--warn)}.cfg-notice p{font-size:var(--text-sm);color:var(--warn);margin:0}@media (width<=700px){.kv-list{flex-direction:column;width:auto;display:flex}.kv{column-gap:var(--sp-4);grid-template-columns:minmax(0,1fr) auto}.kv-label{grid-column:1}.kv-actions{grid-area:1/2}.kv-value,.kv-note{white-space:normal;overflow-wrap:anywhere;grid-column:1/-1}}.log-filters{gap:var(--sp-2);flex-wrap:wrap;align-items:center;display:flex}.log-status{gap:var(--sp-3);align-items:center;display:flex}.log-meta{font-size:var(--text-xs);color:var(--fg-subtle);font-family:var(--font-mono)}.log-select,.log-search{background:var(--bg-raised);border:1px solid var(--border-strong);color:var(--fg);border-radius:var(--radius-sm);padding:var(--sp-2) var(--sp-3);font:inherit;font-size:var(--text-sm)}.log-search{min-width:220px}.log-select:focus,.log-search:focus{outline:2px solid var(--accent);outline-offset:-1px}.log-list{max-height:62vh;font-family:var(--font-mono);font-size:var(--text-xs);border-top:1px solid var(--border);overflow-y:auto}.log-loading{padding:var(--sp-5);color:var(--fg-muted);text-align:center}.log-row{gap:var(--sp-3);padding:var(--sp-2) var(--sp-1);border-bottom:1px solid var(--border);grid-template-columns:62px 52px 1fr;align-items:baseline;display:grid}.log-row:hover{background:var(--surface-hover)}.log-time{color:var(--fg-subtle)}.log-level{letter-spacing:.04em;font-size:10px;font-weight:600}.log-danger .log-level{color:var(--danger)}.log-warn .log-level{color:var(--warn)}.log-info .log-level{color:var(--info)}.log-idle .log-level{color:var(--idle)}.log-danger .log-msg{color:var(--fg)}.log-body{gap:var(--sp-2);flex-wrap:wrap;align-items:baseline;min-width:0;display:flex}.log-msg{color:var(--fg);word-break:break-word}.log-field{color:var(--fg-muted);background:var(--bg-raised);border-radius:var(--radius-sm);padding:0 var(--sp-2);white-space:nowrap;text-overflow:ellipsis;max-width:100%;overflow:hidden}.log-field-key{color:var(--fg-subtle)}.log-field-key:after{content:"=";opacity:.6}@media (width<=860px){.log-row{grid-template-columns:56px 1fr}.log-level{display:none}.log-search{flex:1;min-width:0}}.mem-loading{padding:var(--sp-5);text-align:center;color:var(--fg-muted)}.mem-warning{font-size:var(--text-sm);color:var(--warn);background:var(--warn-soft);border-radius:var(--radius-sm);padding:var(--sp-2) var(--sp-3);margin:0 0 var(--sp-3)}.mem-tools{gap:var(--sp-2);flex-direction:column;margin:0;padding:0;list-style:none;display:flex}.mem-tool{padding:var(--sp-3);border-radius:var(--radius-sm);background:var(--bg-raised);border:1px solid var(--hairline)}.mem-tool-name{font-size:var(--text-sm);color:var(--fg);font-weight:500}.mem-tool-desc{font-size:var(--text-xs);color:var(--fg-muted);margin-top:2px}.chat-page{flex-direction:column;height:calc(100vh - 5rem);display:flex;overflow:hidden}.chat-page-content{flex-direction:column;flex:auto;min-height:0;display:flex;overflow:hidden}.chat-topbar{flex:none;justify-content:space-between;align-items:center;gap:1rem;margin-bottom:1rem;display:flex}.chat-kicker,.chat-workspace-label{color:var(--fg-muted);letter-spacing:.12em;font-size:.68rem;font-weight:700}.chat-topbar-actions{align-items:center;gap:.55rem;display:flex}.chat-topbar-actions .chat-select{min-width:0;max-width:14rem}.chat-update-panel{border:1px solid var(--border);color:var(--fg-muted);border-radius:.75rem;flex:none;justify-content:space-between;align-items:center;gap:1rem;margin:0 0 .75rem;padding:.7rem .9rem;font-size:.8rem;display:flex}.chat-update-panel>div:first-child{gap:.2rem;display:grid}.chat-update-panel strong{color:var(--fg)}.chat-update-actions{flex-shrink:0;gap:.45rem;display:flex}.chat-dangerous-panel{border:1px solid color-mix(in srgb, var(--danger) 45%, var(--border));border-radius:.75rem;flex:none;margin:0 0 .75rem;padding:.8rem .9rem}.chat-dangerous-head,.chat-dangerous-controls,.chat-dangerous-pending{justify-content:space-between;align-items:center;gap:.6rem;display:flex}.chat-dangerous-head{margin-bottom:.7rem}.chat-dangerous-controls{flex-wrap:wrap;align-items:stretch}.chat-dangerous-controls>div{flex:18rem;gap:.45rem;display:flex}.chat-dangerous-input{background:var(--surface-hover);min-width:0;color:var(--fg);border:1px solid var(--border-strong);font:inherit;border-radius:.5rem;flex:1;padding:.5rem .65rem}.chat-dangerous-pending-list{gap:.45rem;margin-top:.75rem;display:grid}.chat-dangerous-pending{border-top:1px solid var(--border);color:var(--fg-muted);padding-top:.55rem;font-size:.8rem}.chat-layout{flex:auto;grid-template-columns:242px minmax(0,1fr);gap:.75rem;min-height:0;display:grid;overflow:hidden}.chat-sidebar,.chat-workspace{background:var(--surface);border:1px solid var(--border);border-radius:1rem}.chat-sidebar{padding:.8rem;overflow-y:auto}.chat-sidebar-head,.chat-toolbar,.chat-compose-actions{justify-content:space-between;align-items:center;gap:.6rem;display:flex}.chat-session-list{gap:.35rem;margin-top:1rem;display:grid}.chat-session{text-align:left;color:var(--fg-muted);cursor:pointer;background:0 0;border:1px solid #0000;border-radius:.65rem;gap:.25rem;padding:.7rem;display:grid}.chat-session:hover,.chat-session.active{background:var(--surface-hover);color:var(--fg);border-color:var(--border-strong)}.chat-session span{font-size:.72rem}.chat-workspace{grid-template-rows:auto minmax(0,1fr) auto;min-width:0;display:grid;overflow:hidden}.chat-workspace-head{border-bottom:1px solid var(--border);justify-content:space-between;align-items:center;padding:.85rem 1.1rem;display:flex}.chat-session-tools{color:var(--fg-muted);margin-left:.6rem;font-size:.78rem;position:relative}.chat-session-tools summary{cursor:pointer}.chat-session-tool-list{z-index:2;background:var(--surface);border:1px solid var(--border-strong);border-radius:.65rem;flex-wrap:wrap;gap:.35rem;width:min(22rem,72vw);padding:.55rem;display:flex;position:absolute;top:calc(100% + .45rem);right:0;box-shadow:0 .7rem 2rem #0000004d}.chat-select{background:var(--surface-hover);color:var(--fg);border:1px solid var(--border-strong);border-radius:.5rem;padding:.5rem .65rem}.chat-transcript{align-content:start;gap:1rem;padding:1.25rem;display:grid;overflow:auto}.chat-empty-state{text-align:center;place-self:center;width:min(100%,40rem);padding:3rem 1rem}.chat-empty-mark{border:1px solid var(--border-strong);width:2.6rem;height:2.6rem;color:var(--accent);border-radius:.8rem;place-items:center;margin:0 auto 1rem;font-weight:700;display:grid}.chat-empty-state h2{letter-spacing:-.04em;margin:0;font-size:clamp(1.5rem,3vw,2.25rem)}.chat-empty-state p{color:var(--fg-muted);margin:.6rem 0 1.4rem}.chat-prompt-grid{flex-wrap:wrap;justify-content:center;gap:.5rem;display:flex}.chat-prompt{border:1px solid var(--border);background:var(--surface-hover);color:var(--fg-muted);cursor:pointer;border-radius:999px;padding:.55rem .75rem}.chat-prompt:hover{color:var(--fg);border-color:var(--border-strong)}.chat-bubble-row{display:flex}.chat-bubble-row.user{justify-content:flex-end}.chat-bubble{background:var(--surface-hover);border-radius:.85rem;max-width:min(78ch,86%);padding:.8rem 1rem;line-height:1.5}.chat-bubble-row.user .chat-bubble{background:color-mix(in srgb, var(--accent) 18%, var(--surface))}.chat-bubble-meta{color:var(--fg-muted);margin-bottom:.3rem;font-size:.7rem}.chat-bubble-text{white-space:pre-wrap;overflow-wrap:anywhere}.chat-bubble-text p{margin:0 0 .7rem}.chat-bubble-text p:last-child{margin-bottom:0}.chat-bubble-text h1,.chat-bubble-text h2,.chat-bubble-text h3{letter-spacing:-.02em;margin:.2rem 0 .6rem;line-height:1.2}.chat-bubble-text h1{font-size:1.25rem}.chat-bubble-text h2{font-size:1.1rem}.chat-bubble-text h3{font-size:1rem}.chat-bubble-text ul,.chat-bubble-text ol{margin:.25rem 0 .75rem;padding-left:1.35rem}.chat-bubble-text blockquote{border-left:3px solid var(--accent);color:var(--fg-muted);margin:.5rem 0;padding:.35rem .75rem}.chat-bubble-text code{background:color-mix(in srgb, var(--fg) 10%, transparent);border-radius:.3rem;padding:.1rem .3rem;font-size:.9em}.chat-bubble-text pre{border:1px solid var(--border);background:color-mix(in srgb, var(--surface) 75%, #000);border-radius:.5rem;margin:.6rem 0;padding:.75rem;overflow:auto}.chat-bubble-text pre code{background:0 0;padding:0}.chat-bubble-text a{color:var(--accent)}.chat-bubble-text table{border-collapse:collapse;width:100%;margin:.7rem 0;font-size:.92em}.chat-bubble-text th,.chat-bubble-text td{text-align:left;border:1px solid var(--border);padding:.45rem .6rem}.chat-bubble-text th{color:var(--fg);background:color-mix(in srgb, var(--surface) 70%, transparent);font-weight:700}.chat-compose{border-top:1px solid var(--border);padding:.85rem 1rem 1rem}.chat-composer-shell{background:var(--surface-hover);border:1px solid var(--border-strong);border-radius:.9rem;padding:.7rem .8rem .55rem;position:relative}.chat-composer{resize:vertical;box-sizing:border-box;width:100%;min-height:3.3rem;color:var(--fg);font:inherit;background:0 0;border:0;padding:.25rem 0}.chat-composer:focus{outline:0}.chat-command-menu{z-index:3;background:var(--surface);border:1px solid var(--border-strong);border-radius:.7rem;gap:.15rem;max-height:18rem;padding:.35rem;display:grid;position:absolute;bottom:calc(100% - .25rem);left:.8rem;right:.8rem;overflow:auto;box-shadow:0 .7rem 2rem #0000004d}.chat-command-menu[hidden]{display:none}.chat-command-option{text-align:left;color:var(--fg-muted);cursor:pointer;background:0 0;border:1px solid #0000;border-radius:.45rem;grid-template-columns:minmax(8rem,14rem) 1fr;align-items:center;gap:.65rem;padding:.5rem .6rem;display:grid}.chat-command-option:hover,.chat-command-option[aria-selected=true]{color:var(--fg);background:var(--surface-hover);border-color:var(--border)}.chat-command-option code{color:var(--fg)}.chat-compose-actions{margin-top:.55rem}.chat-hint,.chat-empty{color:var(--fg-muted);font-size:.78rem}.chat-command-help{color:var(--fg-muted);margin-right:auto;font-size:.78rem}.chat-command-help summary{cursor:pointer}.chat-command-list{min-width:min(34rem,70vw);max-width:40rem;padding-top:.4rem;line-height:1.45}.chat-command-item{grid-template-columns:minmax(10rem,16rem) 1fr;gap:.7rem;padding:.3rem 0;display:grid}.chat-command-item code{color:var(--fg)}@media (width<=760px){.chat-topbar,.chat-topbar-actions{flex-direction:column;align-items:flex-start}.chat-layout{grid-template-columns:1fr}.chat-sidebar{max-height:14rem;overflow:auto}}@media (width<=520px){.chat-update-panel{flex-direction:column;align-items:flex-start}} +:root{--font-sans:ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;--font-mono:ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;--text-xs:.6875rem;--text-sm:.8125rem;--text-base:.875rem;--text-lg:1.0625rem;--text-xl:1.375rem;--text-2xl:1.875rem;--text-3xl:2.5rem;--sp-1:.25rem;--sp-2:.5rem;--sp-3:.75rem;--sp-4:1rem;--sp-5:1.5rem;--sp-6:2rem;--sp-7:3rem;--radius-sm:6px;--radius-md:10px;--radius-lg:14px;--radius-xl:20px;--radius-pill:999px;--transition:.14s cubic-bezier(.4, 0, .2, 1)}[data-theme=dark]{--bg:#0d1014;--bg-raised:#141920;--surface:#171d25;--surface-hover:#1d242e;--border:#232b36;--border-strong:#303a48;--fg:#e8edf4;--fg-muted:#8b97a8;--fg-subtle:#5d6878;--accent:#4c8dff;--accent-fg:#fff;--accent-soft:#4c8dff24;--ok:#3fbf7f;--ok-soft:#3fbf7f24;--warn:#e8a33d;--warn-soft:#e8a33d24;--danger:#f0616d;--danger-soft:#f0616d24;--info:#4c8dff;--info-soft:#4c8dff24;--idle:#6b7688;--idle-soft:#6b768824;--accent-2:#7b5cff;--bg-blur:#1419208c;--surface-glass:#171d259e;--surface-glass-strong:#141920c7;--workspace:#11151b8c;--hairline:#ffffff0f;--wash-a:#4c8dff1a;--wash-b:#7b5cff1f;--wash-c:#3fbf7f0f;--shadow-workspace:0 24px 70px #00000080;--shadow-sm:0 1px 2px #0006;--shadow-md:0 8px 28px #0000006b}[data-theme=light]{--bg:#f6f8fb;--bg-raised:#fff;--surface:#fff;--surface-hover:#f1f4f9;--border:#e2e8f0;--border-strong:#cbd5e1;--fg:#16202e;--fg-muted:#5a6779;--fg-subtle:#8c99ab;--accent:#2563eb;--accent-fg:#fff;--accent-soft:#2563eb1a;--ok:#10935c;--ok-soft:#10935c1f;--warn:#b87514;--warn-soft:#b875141f;--danger:#d4344a;--danger-soft:#d4344a1f;--info:#2563eb;--info-soft:#2563eb1a;--idle:#64748b;--idle-soft:#64748b1f;--accent-2:#7c3aed;--bg-blur:#fff9;--surface-glass:#ffffffb8;--surface-glass-strong:#ffffffe0;--workspace:#ffffff8c;--hairline:#0f172a0f;--wash-a:#2563eb1a;--wash-b:#7c3aed1f;--wash-c:#10935c12;--shadow-workspace:0 24px 70px #0f172a24;--shadow-sm:0 1px 2px #0f172a0f;--shadow-md:0 8px 28px #0f172a1a}*,:before,:after{box-sizing:border-box}html,body{height:100%;margin:0}body{color:var(--fg);font-family:var(--font-sans);font-size:var(--text-base);-webkit-font-smoothing:antialiased;line-height:1.5}a{color:inherit;text-decoration:none}body{background:radial-gradient(70rem 40rem at 8% -10%, var(--wash-a), transparent 62%), radial-gradient(56rem 34rem at 92% 4%, var(--wash-b), transparent 58%), radial-gradient(48rem 30rem at 50% 108%, var(--wash-c), transparent 60%), var(--bg);padding:var(--sp-4);background-attachment:fixed}.shell{min-height:calc(100vh - var(--sp-4) * 2);border-radius:var(--radius-xl);border:1px solid var(--hairline);background:var(--workspace);-webkit-backdrop-filter:blur(28px)saturate(120%);backdrop-filter:blur(28px)saturate(120%);box-shadow:var(--shadow-workspace);flex-direction:column;display:flex;overflow:hidden}.topbar{align-items:center;gap:var(--sp-4);padding:var(--sp-3) var(--sp-4);border-bottom:1px solid var(--hairline);background:var(--bg-blur);z-index:10;display:flex;position:sticky;top:0}.brand{align-items:center;gap:var(--sp-2);font-weight:600;font-size:var(--text-sm);letter-spacing:-.01em;flex:none;display:flex}.brand-mark{background:linear-gradient(135deg, var(--accent), var(--accent-2));color:#fff;width:26px;height:26px;font-size:var(--text-xs);border-radius:8px;place-items:center;font-weight:700;display:grid}.nav{border-radius:var(--radius-pill);background:var(--surface-glass-strong);border:1px solid var(--hairline);align-items:center;gap:2px;margin:0 auto;padding:3px;display:flex}.nav-item{align-items:center;gap:var(--sp-2);padding:var(--sp-2) var(--sp-3);border-radius:var(--radius-pill);color:var(--fg-muted);font-size:var(--text-sm);white-space:nowrap;cursor:pointer;transition:background var(--transition), color var(--transition);display:inline-flex}.nav-item:hover{color:var(--fg);background:var(--surface-hover)}.nav-item[aria-current=page]{background:var(--accent);color:var(--accent-fg);font-weight:500}.nav-item .nav-label{display:inline}.topbar-end{align-items:center;gap:var(--sp-2);flex:none;display:flex}.topbar-search{align-items:center;gap:var(--sp-2);padding:var(--sp-2) var(--sp-3);border-radius:var(--radius-pill);background:var(--surface-glass-strong);border:1px solid var(--hairline);color:var(--fg-subtle);display:flex}.topbar-search input{color:var(--fg);font:inherit;font-size:var(--text-sm);background:0 0;border:0;outline:none;width:8rem}.topbar-search input::placeholder{color:var(--fg-subtle)}.mobile-search-toggle{display:none}.icon-btn{border-radius:var(--radius-pill);border:1px solid var(--hairline);background:var(--surface-glass-strong);width:32px;height:32px;color:var(--fg-muted);cursor:pointer;transition:color var(--transition), background var(--transition);place-items:center;display:grid}.icon-btn:hover{color:var(--fg);background:var(--surface-hover)}.avatar{border-radius:var(--radius-pill);width:32px;height:32px;font-size:var(--text-xs);color:#fff;background:linear-gradient(135deg, var(--accent), var(--accent-2));cursor:pointer;flex:none;place-items:center;font-weight:600;display:grid}.ico{flex:none;display:block}.main{padding:var(--sp-6);flex:1;width:100%;overflow-x:hidden}@media (width<=1220px) and (width>=901px){.topbar{flex-wrap:wrap}.nav{border-radius:var(--radius-lg);flex-wrap:wrap;order:3;justify-content:center;width:100%;margin:0}}@media (width<=900px){body{padding:0}.shell{min-height:100vh;box-shadow:none;border:0;border-radius:0}.topbar{padding:var(--sp-3);gap:var(--sp-2);flex-wrap:wrap}.nav{border-radius:var(--radius-lg);scrollbar-width:none;flex-wrap:nowrap;order:3;justify-content:flex-start;width:100%;overflow-x:auto}.nav::-webkit-scrollbar{display:none}.nav-item .nav-label{display:inline}.topbar-search{background:0 0;border:0;padding:0;overflow:hidden}.topbar-search-icon{display:none}.mobile-search-toggle{display:grid}.topbar-search input{opacity:0;pointer-events:none;width:0;transition:width var(--transition), opacity var(--transition);padding:0}.topbar-search.is-open,.topbar-search:focus-within{padding-right:var(--sp-2);border:1px solid var(--hairline);background:var(--surface-glass-strong)}.topbar-search.is-open input,.topbar-search:focus-within input{opacity:1;pointer-events:auto;width:min(9rem,34vw)}.main{padding:var(--sp-4)}}.page-head{justify-content:space-between;align-items:flex-start;gap:var(--sp-5);margin-bottom:var(--sp-6);flex-wrap:wrap;display:flex}.page-title{font-size:var(--text-2xl);letter-spacing:-.02em;margin:0;font-weight:600}.page-sub{color:var(--fg-muted);font-size:var(--text-sm);margin:var(--sp-1) 0 0}.page-actions{gap:var(--sp-2);align-items:center;display:flex}.btn{align-items:center;gap:var(--sp-2);padding:var(--sp-2) var(--sp-4);border-radius:var(--radius-pill);border:1px solid var(--border-strong);background:var(--surface);color:var(--fg);font:inherit;font-size:var(--text-sm);cursor:pointer;transition:background var(--transition), border-color var(--transition);display:inline-flex}.btn:hover{background:var(--surface-hover)}.btn-primary{background:var(--accent);border-color:var(--accent);color:var(--accent-fg);font-weight:500}.btn-primary:hover{filter:brightness(1.08);background:var(--accent)}.btn-icon{padding:var(--sp-2);border-radius:var(--radius-sm)}.card{background:var(--surface-glass);-webkit-backdrop-filter:blur(14px);backdrop-filter:blur(14px);border:1px solid var(--hairline);border-radius:var(--radius-lg);padding:var(--sp-5);box-shadow:var(--shadow-sm);overflow-x:auto}.card-head{justify-content:space-between;align-items:center;gap:var(--sp-3);margin-bottom:var(--sp-4);flex-wrap:wrap;display:flex}.card-title{font-size:var(--text-base);margin:0;font-weight:600}.card-sub{font-size:var(--text-xs);color:var(--fg-muted);margin:var(--sp-1) 0 0}.grid{gap:var(--sp-4);display:grid}.grid>*{min-width:0}.grid-4{grid-template-columns:repeat(auto-fit,minmax(220px,1fr))}.grid-2{grid-template-columns:repeat(auto-fit,minmax(340px,1fr))}.pill{align-items:center;gap:var(--sp-1);padding:2px var(--sp-2);border-radius:var(--radius-pill);font-size:var(--text-xs);white-space:nowrap;font-weight:500;display:inline-flex}.pill:before{content:"";background:currentColor;border-radius:50%;width:5px;height:5px}.pill-ok{color:var(--ok);background:var(--ok-soft)}.pill-warn{color:var(--warn);background:var(--warn-soft)}.pill-danger{color:var(--danger);background:var(--danger-soft)}.pill-info{color:var(--info);background:var(--info-soft)}.pill-idle{color:var(--idle);background:var(--idle-soft)}.table{border-collapse:collapse;width:100%;font-size:var(--text-sm)}.table-scroll{-webkit-overflow-scrolling:touch;min-width:0;max-width:100%;overflow-x:auto}.table-scroll>.table{min-width:42rem}.table th{text-align:left;font-weight:500;font-size:var(--text-xs);text-transform:uppercase;letter-spacing:.05em;color:var(--fg-subtle);padding:0 var(--sp-3) var(--sp-3);border-bottom:1px solid var(--border)}.table td{padding:var(--sp-3);border-bottom:1px solid var(--border);color:var(--fg-muted)}.table tbody tr:last-child td{border-bottom:0}.table tbody tr:hover td{background:var(--surface-hover)}.table td.strong{color:var(--fg);font-weight:500}.mono{font-family:var(--font-mono);font-size:.95em}.empty{text-align:center;padding:var(--sp-7) var(--sp-4);color:var(--fg-muted)}.empty-title{color:var(--fg);margin-bottom:var(--sp-1);font-weight:500}.skeleton{background:linear-gradient(90deg, var(--surface) 25%, var(--surface-hover) 50%, var(--surface) 75%);border-radius:var(--radius-sm);background-size:200% 100%;animation:1.4s infinite shimmer}@keyframes shimmer{to{background-position:-200% 0}}@media (prefers-reduced-motion:reduce){*,:before,:after{transition-duration:.01ms!important;animation-duration:.01ms!important}}.nav-soon{font-size:var(--text-xs);color:var(--fg-subtle);background:var(--surface-hover);padding:1px var(--sp-2);border-radius:var(--radius-pill);margin-left:auto}.nav-item[aria-disabled]{opacity:.55;cursor:default}.nav-item[aria-disabled]:hover{color:var(--fg-muted);background:0 0}.stat-head{justify-content:space-between;align-items:center;gap:var(--sp-2);display:flex}.stat-label{font-size:var(--text-sm);color:var(--fg-muted)}.stat-trend{font-size:var(--text-xs);align-items:center;gap:3px;font-weight:500;display:inline-flex}.stat-trend.up{color:var(--ok)}.stat-trend.down{color:var(--danger)}.stat-value{font-size:var(--text-3xl);letter-spacing:-.03em;margin:var(--sp-2) 0 0;font-weight:600}.stat-unit{font-size:var(--text-lg);color:var(--fg-muted);margin-left:var(--sp-1)}.stat-compare{font-size:var(--text-xs);color:var(--fg-subtle);margin-top:var(--sp-1)}.spark{width:100%;height:34px;margin-top:var(--sp-3);display:block}.setup{margin-bottom:var(--sp-4)}.setup-pct{font-size:var(--text-lg);color:var(--accent);font-weight:600}.setup-complete-actions{align-items:center;gap:var(--sp-2);display:flex}.setup-dismiss{width:28px;height:28px;font-size:var(--text-lg);line-height:1}.setup-done .setup-pct{color:var(--ok)}.setup-bar{border-radius:var(--radius-pill);background:var(--border);height:5px;margin-bottom:var(--sp-4);overflow:hidden}.setup-bar-fill{background:var(--accent);border-radius:var(--radius-pill);height:100%;transition:width var(--transition)}.setup-list{gap:var(--sp-3);flex-direction:column;margin:0;padding:0;list-style:none;display:flex}.setup-step{gap:var(--sp-3);align-items:flex-start;display:flex}.setup-check{border:1.5px solid var(--border-strong);width:18px;height:18px;font-size:var(--text-xs);color:var(--fg-subtle);border-radius:50%;flex:none;place-items:center;margin-top:1px;display:grid}.setup-step.done .setup-check{background:var(--ok-soft);color:var(--ok);border-color:#0000}.setup-step-title{font-size:var(--text-sm)}.setup-step.done .setup-step-title{color:var(--fg-muted);text-decoration:line-through}.setup-step-detail{font-size:var(--text-xs);color:var(--fg-subtle);margin-top:2px}.hero{justify-content:space-between;align-items:flex-start;gap:var(--sp-5);margin-bottom:var(--sp-5);flex-wrap:wrap;display:flex}.hero-title{font-size:var(--text-3xl);letter-spacing:-.03em;margin:0;font-weight:600}.hero-sub{color:var(--fg-muted);font-size:var(--text-sm);margin:var(--sp-2) 0 0}.hero-actions{gap:var(--sp-2);flex-wrap:wrap;align-items:center;display:flex}.dash-top{gap:var(--sp-4);margin-bottom:var(--sp-4);grid-template-columns:minmax(0,1.6fr) minmax(0,1fr);display:grid}.dash-top>*,.dash-split>*{min-width:0}.dash-top-standalone{grid-template-columns:minmax(0,1fr)}@media (width<=1080px){.dash-top{grid-template-columns:minmax(0,1fr)}}.gauge-wrap{place-items:center;display:grid;position:relative}.gauge{width:100%;max-width:200px;display:block}.gauge-value{transition:stroke-dashoffset .6s cubic-bezier(.4,0,.2,1)}.gauge-center{text-align:center;position:absolute;top:52%;transform:translateY(-50%)}.gauge-pct{font-size:var(--text-2xl);letter-spacing:-.03em;font-weight:600}.gauge-label{font-size:var(--text-xs);color:var(--fg-muted)}.pulse-list{margin:var(--sp-4) 0 0;gap:var(--sp-2);flex-direction:column;padding:0;list-style:none;display:flex}.pulse-item{justify-content:space-between;align-items:center;gap:var(--sp-3);font-size:var(--text-sm);padding:var(--sp-2) var(--sp-3);border-radius:var(--radius-sm);background:var(--bg-raised);display:flex}.pulse-name{color:var(--fg-muted);text-overflow:ellipsis;white-space:nowrap;overflow:hidden}.segbar{border-radius:var(--radius-pill);background:var(--border);gap:2px;height:10px;display:flex;overflow:hidden}.segbar-part{height:100%;display:block}.segbar-ok{background:var(--ok)}.segbar-warn{background:var(--warn)}.segbar-danger{background:var(--danger)}.segbar-info{background:var(--info)}.segbar-idle{background:var(--idle)}.segbar-legend{gap:var(--sp-4);margin-top:var(--sp-3);flex-wrap:wrap;display:flex}.segbar-key{align-items:center;gap:var(--sp-2);font-size:var(--text-xs);color:var(--fg-muted);display:inline-flex}.segbar-dot{border-radius:50%;width:8px;height:8px}.forecast-value{font-size:var(--text-3xl);letter-spacing:-.03em;margin-bottom:var(--sp-1);font-weight:600}.forecast-note{font-size:var(--text-xs);color:var(--fg-subtle);margin-bottom:var(--sp-4)}.dash-split{gap:var(--sp-4);margin-top:var(--sp-4);grid-template-columns:minmax(0,1fr) minmax(0,1.5fr);display:grid}@media (width<=1080px){.dash-split{grid-template-columns:minmax(0,1fr)}}.stat-card{position:relative;overflow:hidden}.dash-section{margin-bottom:var(--sp-5)}.dash-section-head{align-items:baseline;gap:var(--sp-3);margin-bottom:var(--sp-3);padding-left:var(--sp-1);display:flex}.dash-section-title{font-size:var(--text-xs);text-transform:uppercase;letter-spacing:.08em;color:var(--fg-subtle);margin:0;font-weight:600}.dash-section-note{font-size:var(--text-xs);color:var(--fg-subtle)}.hero-actions-inner{gap:var(--sp-2);flex-wrap:wrap;align-items:center;display:flex}.btn-attention{border-color:var(--warn);color:var(--warn);background:var(--warn-soft);font-weight:500}.btn-attention:hover{background:var(--warn-soft);filter:brightness(1.1)}.task-summary{margin-bottom:var(--sp-5)}.task-filters{gap:var(--sp-2);flex-wrap:wrap;align-items:center;display:flex}.task-search,.task-filter{font:inherit;font-size:var(--text-sm);color:var(--fg);background:var(--surface);border:1px solid var(--border-strong);border-radius:var(--radius-sm);padding:var(--sp-2) var(--sp-3)}.task-search{width:220px}.task-search:focus,.task-filter:focus{outline:2px solid var(--accent-soft);outline-offset:1px}.task-row{cursor:pointer}.task-expand{width:1.5rem;height:1.5rem;margin-right:var(--sp-2);color:var(--fg-muted);border:1px solid var(--border);font:inherit;cursor:pointer;background:0 0;border-radius:50%;padding:0;line-height:1}.task-expand:hover,.task-expand:focus-visible{color:var(--fg);border-color:var(--accent)}.activity-row{cursor:pointer}.activity-row:hover td,.activity-row:focus td{background:var(--surface-hover)}.activity-row:focus{outline:2px solid var(--accent);outline-offset:-2px}.task-source-link{color:var(--accent);text-decoration:none}.task-source-link:hover{text-decoration:underline}.task-actions{gap:var(--sp-1);white-space:nowrap;flex-wrap:wrap;display:flex}.btn-small{padding:var(--sp-1) var(--sp-2);font-size:var(--text-xs)}.btn-quiet{color:var(--fg-muted)}.task-action-error{max-width:180px;color:var(--danger);font-size:var(--text-xs);white-space:normal}.task-decision{margin-bottom:var(--sp-4);padding:var(--sp-3);border-left:3px solid var(--accent);background:var(--surface)}.task-detail-actions{justify-content:space-between;align-items:center;gap:var(--sp-3);margin-bottom:var(--sp-4);padding-bottom:var(--sp-3);border-bottom:1px solid var(--hairline);display:flex}.task-decision-title{color:var(--fg);font-weight:600}.task-decision-plan{margin-top:var(--sp-2);color:var(--fg-muted);white-space:pre-wrap}@media (width<=700px){.task-filters,.task-search{width:100%}.task-filter{flex:100%}.table-scroll:has(.task-row){overflow:visible}.table:has(.task-row){display:block}.table:has(.task-row) tbody{display:block}.table-scroll>.table:has(.task-row){width:100%;min-width:0}.table:has(.task-row) thead{display:none}.task-row{gap:var(--sp-2) var(--sp-4);margin-bottom:var(--sp-3);padding:var(--sp-3);border:1px solid var(--hairline);border-radius:var(--radius-lg);background:var(--surface-glass);grid-template-columns:1fr 1fr;display:grid}.task-row td{justify-content:space-between;align-items:center;gap:var(--sp-2);padding:var(--sp-1) 0;text-align:right;border:0;display:flex}.task-row td:before{content:attr(data-label);color:var(--fg-subtle);font-size:var(--text-xs);text-align:left;font-weight:500}.task-row td:nth-child(3),.task-row td:last-child{grid-column:1/-1}.task-row td:last-child{align-items:flex-start}.task-timeline-row,.task-timeline-row td{width:100%;display:block}.task-timeline-row td{margin:calc(var(--sp-2) * -1) 0 var(--sp-4);border-radius:0 0 var(--radius-lg) var(--radius-lg)}.task-detail-actions{flex-direction:column;align-items:flex-start}}@media (width<=480px){.task-row{grid-template-columns:1fr}.task-row td:nth-child(3),.task-row td:last-child{grid-column:auto}}.task-timeline-row td{background:var(--bg-raised);padding:var(--sp-4)}.task-timeline-loading{color:var(--fg-muted);font-size:var(--text-sm)}.task-timeline-error{align-items:flex-start;gap:var(--sp-2);flex-direction:column;display:flex}.task-timeline-error .task-timeline-title{color:var(--fg);font-weight:500}.task-timeline{gap:var(--sp-3);flex-direction:column;margin:0;padding:0;list-style:none;display:flex}.task-timeline-entry{gap:var(--sp-3);align-items:flex-start;display:flex}.task-timeline-dot{background:var(--accent);border-radius:50%;flex:none;width:8px;height:8px;margin-top:5px}.task-timeline-kind{font-size:var(--text-sm);color:var(--fg);font-weight:500}.task-timeline-detail{font-size:var(--text-xs);color:var(--fg-muted);margin-top:2px}.task-timeline-when{font-size:var(--text-xs);color:var(--fg-subtle);margin-top:2px}.skill-search{font:inherit;font-size:var(--text-sm);color:var(--fg);background:var(--surface);border:1px solid var(--border-strong);border-radius:var(--radius-sm);padding:var(--sp-2) var(--sp-3);width:220px}.skill-search:focus{outline:2px solid var(--accent-soft);outline-offset:1px}.skill-card{flex-direction:column;display:flex}.skill-desc{color:var(--fg-muted);font-size:var(--text-sm);margin:0 0 var(--sp-3);flex:1}.skill-source{font-size:var(--text-xs);color:var(--fg-subtle)}.wf-stage-card{margin-top:var(--sp-4)}.wf-rate{font-size:var(--text-sm);color:var(--fg);align-items:baseline;gap:var(--sp-2);display:flex}.wf-rate-sub{font-size:var(--text-xs);color:var(--fg-subtle)}.wf-bar{border-radius:var(--radius-pill);background:var(--border);height:5px;margin-top:var(--sp-2);width:140px;overflow:hidden}.wf-bar-fill{border-radius:var(--radius-pill);height:100%;transition:width var(--transition)}.wf-bar-ok{background:var(--ok)}.wf-bar-warn{background:var(--warn)}.wf-bar-danger{background:var(--danger)}.wf-bar-info{background:var(--info)}.wf-form{gap:var(--sp-2);flex-direction:column;display:flex}.channel-card{gap:var(--sp-1);flex-direction:column;display:flex}.channel-desc{color:var(--fg-muted);font-size:var(--text-sm);margin:0}.channel-detail{color:var(--fg-subtle);font-size:var(--text-xs);margin:0}.cfg-page{max-width:70rem;margin:0 auto}.cfg-section{margin-bottom:var(--sp-4)}.cfg-subhead{font-size:var(--text-sm);color:var(--fg-muted);margin:var(--sp-5) 0 var(--sp-2);font-weight:600}.cfg-subhead:first-child{margin-top:0}.kv-list{column-gap:var(--sp-4);grid-template-columns:max-content minmax(0,1fr) auto;row-gap:0;display:grid}.kv{grid-template-columns:subgrid;align-items:center;row-gap:var(--sp-1);padding:var(--sp-2) 0;border-radius:var(--radius-sm);grid-column:1/-1;display:grid}@supports not (grid-template-columns:subgrid){.kv-list{flex-direction:column;width:auto;display:flex}.kv{column-gap:var(--sp-4);grid-template-columns:minmax(8rem,16rem) minmax(0,1fr) auto}}.kv+.kv{border-top:1px solid var(--border)}.kv:hover{background:var(--surface-hover)}.kv-label{font-size:var(--text-sm);color:var(--fg-muted)}.kv-value{min-width:0;font-size:var(--text-sm);color:var(--fg);text-overflow:ellipsis;white-space:nowrap;overflow:hidden}.kv-value.is-empty{color:var(--fg-subtle)}.kv-actions{align-items:center;gap:var(--sp-2);justify-self:end;display:flex}.kv-note{font-size:var(--text-xs);color:var(--fg-subtle);grid-column:2/-1}.kv-note.is-error{color:var(--danger)}.kv.is-locked .kv-value{color:var(--fg-muted)}.kv-btn{font:inherit;font-size:var(--text-xs);color:var(--fg-subtle);border:1px solid var(--border);border-radius:var(--radius-sm);padding:2px var(--sp-2);cursor:pointer;background:0 0;flex:none;transition:color .12s,border-color .12s,background .12s}.kv:hover .kv-btn,.kv:focus-within .kv-btn{color:var(--fg-muted);border-color:var(--border-strong)}.kv-btn:hover{color:var(--fg);border-color:var(--accent);background:var(--accent-soft)}.kv-btn:focus-visible{outline:2px solid var(--accent);outline-offset:1px}.kv-flag{font-size:var(--text-xs);color:var(--warn);background:var(--warn-soft);border-radius:var(--radius-pill);padding:1px var(--sp-2);flex:none}.kv-input{font:inherit;font-size:var(--text-sm);color:var(--fg);background:var(--bg,var(--surface));border:1px solid var(--border-strong);border-radius:var(--radius-sm);padding:var(--sp-1) var(--sp-2);width:100%;min-width:0}.kv-input:focus{outline:2px solid var(--accent);outline-offset:-1px}.kv-save,.kv-cancel{font:inherit;font-size:var(--text-xs);border-radius:var(--radius-sm);padding:var(--sp-1) var(--sp-2);cursor:pointer;flex:none}.kv-save{background:var(--accent);border:1px solid var(--accent);color:var(--accent-fg);font-weight:500}.kv-save:disabled{opacity:.6;cursor:default}.kv-cancel{border:1px solid var(--border-strong);color:var(--fg-muted);background:0 0}.kv-cancel:hover{color:var(--fg)}.cfg-notice{border-left:3px solid var(--warn)}.cfg-notice p{font-size:var(--text-sm);color:var(--warn);margin:0}@media (width<=700px){.kv-list{flex-direction:column;width:auto;display:flex}.kv{column-gap:var(--sp-4);grid-template-columns:minmax(0,1fr) auto}.kv-label{grid-column:1}.kv-actions{grid-area:1/2}.kv-value,.kv-note{white-space:normal;overflow-wrap:anywhere;grid-column:1/-1}}.log-filters{gap:var(--sp-2);flex-wrap:wrap;align-items:center;display:flex}.log-status{gap:var(--sp-3);align-items:center;display:flex}.log-meta{font-size:var(--text-xs);color:var(--fg-subtle);font-family:var(--font-mono)}.log-select,.log-search{background:var(--bg-raised);border:1px solid var(--border-strong);color:var(--fg);border-radius:var(--radius-sm);padding:var(--sp-2) var(--sp-3);font:inherit;font-size:var(--text-sm)}.log-search{min-width:220px}.log-select:focus,.log-search:focus{outline:2px solid var(--accent);outline-offset:-1px}.log-list{max-height:62vh;font-family:var(--font-mono);font-size:var(--text-xs);border-top:1px solid var(--border);overflow-y:auto}.log-loading{padding:var(--sp-5);color:var(--fg-muted);text-align:center}.log-row{gap:var(--sp-3);padding:var(--sp-2) var(--sp-1);border-bottom:1px solid var(--border);grid-template-columns:62px 52px 1fr;align-items:baseline;display:grid}.log-row:hover{background:var(--surface-hover)}.log-time{color:var(--fg-subtle)}.log-level{letter-spacing:.04em;font-size:10px;font-weight:600}.log-danger .log-level{color:var(--danger)}.log-warn .log-level{color:var(--warn)}.log-info .log-level{color:var(--info)}.log-idle .log-level{color:var(--idle)}.log-danger .log-msg{color:var(--fg)}.log-body{gap:var(--sp-2);flex-wrap:wrap;align-items:baseline;min-width:0;display:flex}.log-msg{color:var(--fg);word-break:break-word}.log-field{color:var(--fg-muted);background:var(--bg-raised);border-radius:var(--radius-sm);padding:0 var(--sp-2);white-space:nowrap;text-overflow:ellipsis;max-width:100%;overflow:hidden}.log-field-key{color:var(--fg-subtle)}.log-field-key:after{content:"=";opacity:.6}@media (width<=860px){.log-row{grid-template-columns:56px 1fr}.log-level{display:none}.log-search{flex:1;min-width:0}}.mem-loading{padding:var(--sp-5);text-align:center;color:var(--fg-muted)}.mem-warning{font-size:var(--text-sm);color:var(--warn);background:var(--warn-soft);border-radius:var(--radius-sm);padding:var(--sp-2) var(--sp-3);margin:0 0 var(--sp-3)}.mem-tools{gap:var(--sp-2);flex-direction:column;margin:0;padding:0;list-style:none;display:flex}.mem-tool{padding:var(--sp-3);border-radius:var(--radius-sm);background:var(--bg-raised);border:1px solid var(--hairline)}.mem-tool-name{font-size:var(--text-sm);color:var(--fg);font-weight:500}.mem-tool-desc{font-size:var(--text-xs);color:var(--fg-muted);margin-top:2px}.chat-page{flex-direction:column;height:calc(100vh - 5rem);display:flex;overflow:hidden}.chat-page-content{flex-direction:column;flex:auto;min-height:0;display:flex;overflow:hidden}.chat-topbar{flex:none;justify-content:space-between;align-items:center;gap:1rem;margin-bottom:1rem;display:flex}.chat-kicker,.chat-workspace-label{color:var(--fg-muted);letter-spacing:.12em;font-size:.68rem;font-weight:700}.chat-topbar-actions{align-items:center;gap:.55rem;display:flex}.chat-topbar-actions .chat-select{min-width:0;max-width:14rem}.chat-update-panel{border:1px solid var(--border);color:var(--fg-muted);border-radius:.75rem;flex:none;justify-content:space-between;align-items:center;gap:1rem;margin:0 0 .75rem;padding:.7rem .9rem;font-size:.8rem;display:flex}.chat-update-panel>div:first-child{gap:.2rem;display:grid}.chat-update-panel strong{color:var(--fg)}.chat-update-actions{flex-shrink:0;gap:.45rem;display:flex}.chat-dangerous-panel{border:1px solid color-mix(in srgb, var(--danger) 45%, var(--border));border-radius:.75rem;flex:none;margin:0 0 .75rem;padding:.8rem .9rem}.chat-dangerous-head,.chat-dangerous-controls,.chat-dangerous-pending{justify-content:space-between;align-items:center;gap:.6rem;display:flex}.chat-dangerous-head{margin-bottom:.7rem}.chat-dangerous-controls{flex-wrap:wrap;align-items:stretch}.chat-dangerous-controls>div{flex:18rem;gap:.45rem;display:flex}.chat-dangerous-input{background:var(--surface-hover);min-width:0;color:var(--fg);border:1px solid var(--border-strong);font:inherit;border-radius:.5rem;flex:1;padding:.5rem .65rem}.chat-dangerous-pending-list{gap:.45rem;margin-top:.75rem;display:grid}.chat-dangerous-pending{border-top:1px solid var(--border);color:var(--fg-muted);padding-top:.55rem;font-size:.8rem}.chat-layout{flex:auto;grid-template-columns:242px minmax(0,1fr);gap:.75rem;min-height:0;display:grid;overflow:hidden}.chat-sidebar,.chat-workspace{background:var(--surface);border:1px solid var(--border);border-radius:1rem}.chat-sidebar{padding:.8rem;overflow-y:auto}.chat-sidebar-head,.chat-toolbar,.chat-compose-actions{justify-content:space-between;align-items:center;gap:.6rem;display:flex}.chat-session-list{gap:.35rem;margin-top:1rem;display:grid}.chat-session{text-align:left;color:var(--fg-muted);cursor:pointer;background:0 0;border:1px solid #0000;border-radius:.65rem;gap:.25rem;padding:.7rem;display:grid}.chat-session:hover,.chat-session.active{background:var(--surface-hover);color:var(--fg);border-color:var(--border-strong)}.chat-session span{font-size:.72rem}.chat-workspace{grid-template-rows:auto minmax(0,1fr) auto;min-width:0;display:grid;overflow:hidden}.chat-workspace-head{border-bottom:1px solid var(--border);justify-content:space-between;align-items:center;padding:.85rem 1.1rem;display:flex}.chat-session-tools{color:var(--fg-muted);margin-left:.6rem;font-size:.78rem;position:relative}.chat-session-tools summary{cursor:pointer}.chat-session-tool-list{z-index:2;background:var(--surface);border:1px solid var(--border-strong);border-radius:.65rem;flex-wrap:wrap;gap:.35rem;width:min(22rem,72vw);padding:.55rem;display:flex;position:absolute;top:calc(100% + .45rem);right:0;box-shadow:0 .7rem 2rem #0000004d}.chat-select{background:var(--surface-hover);color:var(--fg);border:1px solid var(--border-strong);border-radius:.5rem;padding:.5rem .65rem}.chat-transcript{align-content:start;gap:1rem;padding:1.25rem;display:grid;overflow:auto}.chat-empty-state{text-align:center;place-self:center;width:min(100%,40rem);padding:3rem 1rem}.chat-empty-mark{border:1px solid var(--border-strong);width:2.6rem;height:2.6rem;color:var(--accent);border-radius:.8rem;place-items:center;margin:0 auto 1rem;font-weight:700;display:grid}.chat-empty-state h2{letter-spacing:-.04em;margin:0;font-size:clamp(1.5rem,3vw,2.25rem)}.chat-empty-state p{color:var(--fg-muted);margin:.6rem 0 1.4rem}.chat-prompt-grid{flex-wrap:wrap;justify-content:center;gap:.5rem;display:flex}.chat-prompt{border:1px solid var(--border);background:var(--surface-hover);color:var(--fg-muted);cursor:pointer;border-radius:999px;padding:.55rem .75rem}.chat-prompt:hover{color:var(--fg);border-color:var(--border-strong)}.chat-bubble-row{display:flex}.chat-bubble-row.user{justify-content:flex-end}.chat-bubble{background:var(--surface-hover);border-radius:.85rem;max-width:min(78ch,86%);padding:.8rem 1rem;line-height:1.5}.chat-bubble-row.user .chat-bubble{background:color-mix(in srgb, var(--accent) 18%, var(--surface))}.chat-bubble-meta{color:var(--fg-muted);margin-bottom:.3rem;font-size:.7rem}.chat-bubble-text{white-space:pre-wrap;overflow-wrap:anywhere}.chat-bubble-text p{margin:0 0 .7rem}.chat-bubble-text p:last-child{margin-bottom:0}.chat-bubble-text h1,.chat-bubble-text h2,.chat-bubble-text h3{letter-spacing:-.02em;margin:.2rem 0 .6rem;line-height:1.2}.chat-bubble-text h1{font-size:1.25rem}.chat-bubble-text h2{font-size:1.1rem}.chat-bubble-text h3{font-size:1rem}.chat-bubble-text ul,.chat-bubble-text ol{margin:.25rem 0 .75rem;padding-left:1.35rem}.chat-bubble-text blockquote{border-left:3px solid var(--accent);color:var(--fg-muted);margin:.5rem 0;padding:.35rem .75rem}.chat-bubble-text code{background:color-mix(in srgb, var(--fg) 10%, transparent);border-radius:.3rem;padding:.1rem .3rem;font-size:.9em}.chat-bubble-text pre{border:1px solid var(--border);background:color-mix(in srgb, var(--surface) 75%, #000);border-radius:.5rem;margin:.6rem 0;padding:.75rem;overflow:auto}.chat-bubble-text pre code{background:0 0;padding:0}.chat-bubble-text a{color:var(--accent)}.chat-bubble-text table{border-collapse:collapse;width:100%;margin:.7rem 0;font-size:.92em}.chat-bubble-text th,.chat-bubble-text td{text-align:left;border:1px solid var(--border);padding:.45rem .6rem}.chat-bubble-text th{color:var(--fg);background:color-mix(in srgb, var(--surface) 70%, transparent);font-weight:700}.chat-tools{gap:.2rem;margin-bottom:.5rem;font-size:.78rem;display:grid}.chat-tools:empty{display:none}.chat-tool{color:var(--fg-muted);overflow-wrap:anywhere;align-items:baseline;gap:.4rem;display:flex}.chat-tool-name{color:var(--fg);font-weight:600}.chat-tool.failed .chat-tool-summary{color:var(--danger)}.chat-compose{border-top:1px solid var(--border);padding:.85rem 1rem 1rem}.chat-composer-shell{background:var(--surface-hover);border:1px solid var(--border-strong);border-radius:.9rem;padding:.7rem .8rem .55rem;position:relative}.chat-composer{resize:vertical;box-sizing:border-box;width:100%;min-height:3.3rem;color:var(--fg);font:inherit;background:0 0;border:0;padding:.25rem 0}.chat-composer:focus{outline:0}.chat-command-menu{z-index:3;background:var(--surface);border:1px solid var(--border-strong);border-radius:.7rem;gap:.15rem;max-height:18rem;padding:.35rem;display:grid;position:absolute;bottom:calc(100% - .25rem);left:.8rem;right:.8rem;overflow:auto;box-shadow:0 .7rem 2rem #0000004d}.chat-command-menu[hidden]{display:none}.chat-command-option{text-align:left;color:var(--fg-muted);cursor:pointer;background:0 0;border:1px solid #0000;border-radius:.45rem;grid-template-columns:minmax(8rem,14rem) 1fr;align-items:center;gap:.65rem;padding:.5rem .6rem;display:grid}.chat-command-option:hover,.chat-command-option[aria-selected=true]{color:var(--fg);background:var(--surface-hover);border-color:var(--border)}.chat-command-option code{color:var(--fg)}.chat-compose-actions{margin-top:.55rem}.chat-hint,.chat-empty{color:var(--fg-muted);font-size:.78rem}.chat-command-help{color:var(--fg-muted);margin-right:auto;font-size:.78rem}.chat-command-help summary{cursor:pointer}.chat-command-list{min-width:min(34rem,70vw);max-width:40rem;padding-top:.4rem;line-height:1.45}.chat-command-item{grid-template-columns:minmax(10rem,16rem) 1fr;gap:.7rem;padding:.3rem 0;display:grid}.chat-command-item code{color:var(--fg)}@media (width<=760px){.chat-topbar,.chat-topbar-actions{flex-direction:column;align-items:flex-start}.chat-layout{grid-template-columns:1fr}.chat-sidebar{max-height:14rem;overflow:auto}}@media (width<=520px){.chat-update-panel{flex-direction:column;align-items:flex-start}} diff --git a/ui/dist/assets/index.js b/ui/dist/assets/index.js index 443b5b776..95727ab4a 100644 --- a/ui/dist/assets/index.js +++ b/ui/dist/assets/index.js @@ -74,7 +74,7 @@ `;function Fe(){let e=document.createElement(`template`);e.innerHTML=Pe.trim();let t=e.content.firstElementChild,n={};for(let e of t.querySelectorAll(`[data-chat-ref]`))n[e.dataset.chatRef]=e;return{root:t,refs:n}}function J(t){let n=document.createDocumentFragment(),r=/(~~[^~]+~~|\*\*[^*]+\*\*|__[^_]+__|`[^`]+`|\[[^\]]+\]\(https?:\/\/[^\s)]+\)|\*[^*]+\*|_[^_]+_)/g,i=0;for(let a of t.matchAll(r)){a.index>i&&n.append(document.createTextNode(t.slice(i,a.index)));let r=a[0];if(r.startsWith(`~~`))n.append(e(`del`,r.slice(2,-2)));else if(r.startsWith(`**`)||r.startsWith(`__`))n.append(e(`strong`,r.slice(2,-2)));else if(r.startsWith("`"))n.append(e(`code`,r.slice(1,-1)));else if(r.startsWith(`[`)){let t=r.match(/^\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)$/);n.append(e(`a`,{href:t[2],target:`_blank`,rel:`noreferrer`},t[1]))}else n.append(e(`em`,r.slice(1,-1)));i=a.index+r.length}return i{i.length&&(n.append(e(`p`,J(i.join(` `)))),i=[])},c=()=>{a&&n.append(a),a=null};for(let t=0;te(`td`,J(t)))));n.append(e(`table`,e(`thead`,e(`tr`,...u.map(t=>e(`th`,J(t))))),a));continue}if(!l.trim()){s(),c();continue}let f=l.match(/^(#{1,3})\s+(.+)$/);if(f){s(),c(),n.append(e(`h${f[1].length}`,J(f[2])));continue}let p=l.match(/^\s*[-*]\s+(.+)$/),m=l.match(/^\s*\d+[.)]\s+(.+)$/);if(p||m){s();let t=m?`ol`:`ul`;(!a||a.tagName.toLowerCase()!==t)&&(c(),a=e(t)),a.append(e(`li`,J((p||m)[1])));continue}if(l.startsWith(`>`)){s(),c(),n.append(e(`blockquote`,J(l.replace(/^>\s?/,``))));continue}i.push(l)}return s(),c(),o&&n.append(e(`pre`,e(`code`,o.join(` -`)))),n}function Y(e){let t=String(e||``).trim();if(!t.includes(`|`))return null;let n=t.replace(/^\|/,``).replace(/\|$/,``).split(`|`).map(e=>e.trim());return n.length>1&&n.every(e=>e!==``)?n:null}function Le(e){return e.every(e=>/^:?-{3,}:?$/.test(e))}function Re(e){return e.title||e.branch_name||`Conversation ${e.session_id.slice(0,8)}`}function ze(t){let n=t.from??t.From??``,r=t.text??t.Text??``,i=n!==`web`;return e(`div.chat-bubble-row.${i?`assistant`:`user`}`,e(`div.chat-bubble`,e(`div.chat-bubble-meta`,i?`Archie`:`You`),i?Ie(r):e(`div.chat-bubble-text`,r)))}function X(e,t){let n=Ie(t);return typeof e?.replaceWith==`function`?e.replaceWith(n):e?.parentNode&&e.parentNode.replaceChild(n,e),n}var Be=`archie.web.chat.channel`;function Z(){let e=localStorage.getItem(Be);return e||(e=crypto.randomUUID(),localStorage.setItem(Be,e)),e}function Ve(e){return{text:e,sourceID:crypto.randomUUID()}}function He(e){return{...e}}function Ue(e,t){let n=!!(e&&typeof e==`object`&&e.turn&&typeof e.turn.text==`string`),r=n?e.turn.text:t.trim();return{text:r,turn:n?He(e.turn):Ve(r),isRetry:n}}function We({sessionList:t,sessionTools:n,sessions:r,currentSession:i,onSelect:a}){if(t.replaceChildren(),n.hidden=!i,!r.length){t.append(e(`div.chat-empty`,`Your conversations will appear here.`));return}for(let n of r)t.append(e(`button.chat-session`,{class:i===n.session_id?`active`:``,onclick:()=>a(n.session_id)},e(`strong`,Re(n)),e(`span`,new Date(n.last_active_at).toLocaleString())))}function Ge(t,n,r){t.persona.replaceChildren(e(`option`,{value:``},`Personality`));for(let r of n.personas||[])t.persona.append(e(`option`,{value:r},r));t.persona.value=n.active_persona||``;let i=n.providers||[];t.provider.hidden=i.length===0,t.provider.replaceChildren(e(`option`,{value:``},`Provider`));for(let n of i)t.provider.append(e(`option`,{value:n},n));t.provider.value=n.active_provider||i[0]||``,Ke(t,n,t.provider.value),t.restart.hidden=!n.restart_available,t.dangerousPanel.hidden=!n.dangerous_available,n.dangerous_available&&r()}function Ke(t,n,r){let i=n.models_by_provider||{},a=r&&i[r]?i[r]:n.models||[];t.model.replaceChildren(e(`option`,{value:``},`Model`));for(let n of a)t.model.append(e(`option`,{value:n},n));t.model.value=n.active_model||``}function qe(t,n){let r=(n||[]).map(e=>typeof e==`string`?{command:e,usage:e,description:``}:e),i=e(`div.chat-command-list`);r.length||i.append(e(`span`,`Send a message to begin.`));for(let t of r)i.append(e(`div.chat-command-item`,e(`code`,t.usage||t.command),e(`span`,t.description||``)));return t.replaceChildren(e(`summary`,`Available commands (${r.length})`),i),r}function Je(t,n,{onDefer:r,onInstall:i}){t.replaceChildren();let a=n?.snapshot||null,o=n?.available||[];if(!o.length){t.append(e(`span`,a?.deferred?`Update deferred.`:`Archie is up to date.`));return}t.append(e(`div`,e(`strong`,`Update available`),e(`span`,o.map(e=>`${e.Label||e.label}: ${e.Available||e.available}`).join(` · `))),e(`div.chat-update-actions`,e(`button.btn`,{onclick:r},`Defer`),n.can_install?e(`button.btn.btn-primary`,{onclick:i},`Install update`):null))}function Ye(t,n,{onRequest:r,onDecision:i}){t.replaceChildren();let a=n?.checkpoints||[],o=n?.pending||[],s=e(`select.chat-select`,{"aria-label":`Checkpoint`},e(`option`,{value:``},`Select checkpoint`),...a.map(t=>e(`option`,{value:t.Number??t.number},`${t.Number??t.number} — ${t.Label??t.label??`checkpoint`}`))),c=e(`input.chat-dangerous-input`,{placeholder:`Process name or id`,"aria-label":`Process name or id`}),l=(e,t)=>()=>r(e,String(t.value||t).trim()),u=(t,n,r)=>e(`button.btn`,{onclick:()=>i(t,n)},r),d=o.length?o.map(t=>e(`div.chat-dangerous-pending`,e(`span`,t.description),e(`div.chat-update-actions`,u(t.id,`approve`,`Approve`),u(t.id,`permanent`,`Approve for 24h`),u(t.id,`deny`,`Deny`)))):[e(`span.chat-hint`,`No pending dangerous actions.`)];t.append(e(`div.chat-dangerous-head`,e(`strong`,`Dangerous actions`),e(`span.chat-hint`,`Every action requires approval.`)),e(`div.chat-dangerous-controls`,e(`div`,s,e(`button.btn`,{onclick:l(`rollback`,s)},`Request rollback`)),e(`div`,c,e(`button.btn`,{onclick:l(`stop`,c)},`Request stop`))),e(`div.chat-dangerous-pending-list`,...d))}function Xe(e){return e?.status===501?null:`Could not check for updates.`}function Ze(){let{root:t,refs:n}=Fe(),{sessionList:r,transcript:i,composer:a,send:o,stop:s,status:c,persona:l,provider:u,model:d,restart:f,emptyState:p,updatePanel:h,dangerousPanel:g,commandMenu:_,sessionTools:v,commandHelp:y,checkUpdates:b,newChat:x}=n,S=``,C=[],w=!1,T=null,E=null,D={},O=[],k=[],A=0;f.onclick=()=>{window.confirm(`Reload the Telegram chat adapter?`)&&(a.value=`/restart`,H())},x.onclick=()=>{a.value=`/new`,H()};for(let e of t.querySelectorAll(`[data-chat-prompt]`))e.onclick=()=>{a.value=e.dataset.chatPrompt,a.focus()};function j(e){p.remove(),i.append(ze(e)),i.scrollTop=i.scrollHeight}function M(){let e=a.value.slice(0,a.selectionStart).match(/(?:^|\s)\/([^\s]*)$/);return e?e[1].toLowerCase():null}function N(){_.hidden=!0,k=[],A=0}function P(){let t=M();if(t===null){N();return}if(k=O.filter(e=>`${e.command} ${e.description}`.toLowerCase().includes(t)).slice(0,8),!k.length){N();return}A=Math.min(A,k.length-1),_.replaceChildren(...k.map((t,n)=>e(`button.chat-command-option`,{type:`button`,role:`option`,"aria-selected":String(n===A),onmousedown:e=>e.preventDefault(),onclick:()=>F(n)},e(`code`,t.command),e(`span`,t.description||t.usage||``)))),_.hidden=!1}function F(e){let t=k[e];if(!t)return;let n=a.value.slice(0,a.selectionStart),r=a.value.slice(a.selectionStart),i=n.search(/(?:^|\s)\/[^\s]*$/),o=i<0?n.length:i+ +(n[i]===` `);a.value=`${n.slice(0,o)}${t.command} ${r}`;let s=o+t.command.length+1;a.setSelectionRange(s,s),N(),a.focus()}async function I(){try{let e=await m.chatUpdate();E=e?.snapshot||null,Je(h,e,{onDefer:async()=>{await m.chatUpdateDefer(E),Je(h,{snapshot:{deferred:!0},available:[]},{onDefer:()=>{},onInstall:()=>{}}),c.textContent=`Update deferred`},onInstall:async()=>{c.textContent=`Installing…`,await m.chatUpdateInstall(E),c.textContent=`Update started`,I()}})}catch(t){let n=Xe(t);h.replaceChildren(...n?[e(`span`,n)]:[])}}async function L(){try{Ye(g,await m.chatDangerous(),{onRequest:async(e,t)=>{if(t)try{await m.chatDangerousRequest(e,t),c.textContent=`Approval requested`,await L()}catch(e){c.textContent=e.message||`Could not request action`}},onDecision:async(e,t)=>{try{await m.chatDangerousDecision(e,t),c.textContent=t===`deny`?`Action denied`:`Action approved`,await L()}catch(e){c.textContent=e.message||`Approval failed`}}})}catch(t){g.replaceChildren(e(`span.chat-hint`,t.message||`Dangerous actions unavailable`))}}async function R(){let e=await m.chatSessions();C=e.sessions||[],D=e,Ge({persona:l,provider:u,model:d,restart:f,dangerousPanel:g},e,L),O=qe(y,e.commands),We({sessionList:r,sessionTools:v,sessions:C,currentSession:S,onSelect:V}),!S&&C[0]&&await V(C[0].session_id)}async function z(e){if(S)try{c.textContent=`Working…`;let t=await m.chatMessage(Z(),e);t.session_id&&(S=t.session_id),c.textContent=t.reply||`Session updated`,await R(),S&&await V(S),v.open=!1}catch(e){c.textContent=e.message||`Session action failed`}}function B(e,t){let n=window.prompt(t);n?.trim()&&z(`${e} ${n.trim()}`)}async function V(e){S=e,We({sessionList:r,sessionTools:v,sessions:C,currentSession:S,onSelect:V}),i.replaceChildren(),await m.chatMessage(Z(),`/resume ${e}`),l.value=D.active_personas?.[e]||D.active_persona||``;let t=await m.chatMessages(e);for(let e of t)j(e);i.children.length||i.append(p)}async function H(t=null){let{text:n,turn:r,isRetry:l}=Ue(t,a.value);if(!n||w)return;N(),w=!0,o.disabled=!0,s.disabled=!1,a.disabled=!0,c.textContent=l?`Retrying…`:`Thinking…`,l||(j({from:`web`,text:n}),a.value=``);let u=l?t.replyBubble:e(`div.chat-bubble-row.assistant`,e(`div.chat-bubble`,e(`div.chat-bubble-meta`,`Archie`),e(`div.chat-bubble-text`)));l||i.append(u),u.querySelector(`.chat-retry`)?.remove();let d=u.querySelector(`.chat-bubble-text`),f=``,p=!1,m=!1,h;try{T=new AbortController,h=setTimeout(()=>{m=!0,T.abort()},12e4);let e=await fetch(`/api/chat/stream`,{method:`POST`,headers:{"Content-Type":`application/json`,Accept:`text/event-stream`},body:JSON.stringify({channel_id:Z(),source_id:r.sourceID,text:r.text}),signal:T.signal});if(!e.ok)throw Error(await e.text()||`${e.status} ${e.statusText}`);let t=e.body.getReader(),n=new TextDecoder,i=``;for(;;){let{value:e,done:r}=await t.read();i+=n.decode(e||new Uint8Array,{stream:!r});let a=i.split(` +`)))),n}function Y(e){let t=String(e||``).trim();if(!t.includes(`|`))return null;let n=t.replace(/^\|/,``).replace(/\|$/,``).split(`|`).map(e=>e.trim());return n.length>1&&n.every(e=>e!==``)?n:null}function Le(e){return e.every(e=>/^:?-{3,}:?$/.test(e))}function Re(e){return e.title||e.branch_name||`Conversation ${e.session_id.slice(0,8)}`}function ze(){return e(`div.chat-bubble-row.assistant`,e(`div.chat-bubble`,e(`div.chat-bubble-meta`,`Archie`),e(`div.chat-tools`),e(`div.chat-bubble-text`)))}function Be(t){let n=t.from??t.From??``,r=t.text??t.Text??``,i=n!==`web`;return e(`div.chat-bubble-row.${i?`assistant`:`user`}`,e(`div.chat-bubble`,e(`div.chat-bubble-meta`,i?`Archie`:`You`),i?Ie(r):e(`div.chat-bubble-text`,r)))}function X(e,t){let n=Ie(t);return typeof e?.replaceWith==`function`?e.replaceWith(n):e?.parentNode&&e.parentNode.replaceChild(n,e),n}function Ve(t,n){let r=n?.tool??``;if(!t||!r)return null;let i=n.text??``,a=e(`div.chat-tool`,e(`span.chat-tool-icon`,`🔧`),e(`span.chat-tool-name`,r),e(`span.chat-tool-summary`,i));return i.startsWith(`failed:`)&&(a.className=`chat-tool failed`),He(t).append(a),a}function He(t){let n=t.querySelector(`.chat-tools`);if(n)return n;let r=e(`div.chat-tools`);return t.append(r),r}var Ue=`archie.web.chat.channel`;function Z(){let e=localStorage.getItem(Ue);return e||(e=crypto.randomUUID(),localStorage.setItem(Ue,e)),e}function We(e){return{text:e,sourceID:crypto.randomUUID()}}function Ge(e){return{...e}}function Ke(e,t){let n=!!(e&&typeof e==`object`&&e.turn&&typeof e.turn.text==`string`),r=n?e.turn.text:t.trim();return{text:r,turn:n?Ge(e.turn):We(r),isRetry:n}}function qe({sessionList:t,sessionTools:n,sessions:r,currentSession:i,onSelect:a}){if(t.replaceChildren(),n.hidden=!i,!r.length){t.append(e(`div.chat-empty`,`Your conversations will appear here.`));return}for(let n of r)t.append(e(`button.chat-session`,{class:i===n.session_id?`active`:``,onclick:()=>a(n.session_id)},e(`strong`,Re(n)),e(`span`,new Date(n.last_active_at).toLocaleString())))}function Je(t,n,r){t.persona.replaceChildren(e(`option`,{value:``},`Personality`));for(let r of n.personas||[])t.persona.append(e(`option`,{value:r},r));t.persona.value=n.active_persona||``;let i=n.providers||[];t.provider.hidden=i.length===0,t.provider.replaceChildren(e(`option`,{value:``},`Provider`));for(let n of i)t.provider.append(e(`option`,{value:n},n));t.provider.value=n.active_provider||i[0]||``,Ye(t,n,t.provider.value),t.restart.hidden=!n.restart_available,t.dangerousPanel.hidden=!n.dangerous_available,n.dangerous_available&&r()}function Ye(t,n,r){let i=n.models_by_provider||{},a=r&&i[r]?i[r]:n.models||[];t.model.replaceChildren(e(`option`,{value:``},`Model`));for(let n of a)t.model.append(e(`option`,{value:n},n));t.model.value=n.active_model||``}function Xe(t,n){let r=(n||[]).map(e=>typeof e==`string`?{command:e,usage:e,description:``}:e),i=e(`div.chat-command-list`);r.length||i.append(e(`span`,`Send a message to begin.`));for(let t of r)i.append(e(`div.chat-command-item`,e(`code`,t.usage||t.command),e(`span`,t.description||``)));return t.replaceChildren(e(`summary`,`Available commands (${r.length})`),i),r}function Ze(t,n,{onDefer:r,onInstall:i}){t.replaceChildren();let a=n?.snapshot||null,o=n?.available||[];if(!o.length){t.append(e(`span`,a?.deferred?`Update deferred.`:`Archie is up to date.`));return}t.append(e(`div`,e(`strong`,`Update available`),e(`span`,o.map(e=>`${e.Label||e.label}: ${e.Available||e.available}`).join(` · `))),e(`div.chat-update-actions`,e(`button.btn`,{onclick:r},`Defer`),n.can_install?e(`button.btn.btn-primary`,{onclick:i},`Install update`):null))}function Qe(t,n,{onRequest:r,onDecision:i}){t.replaceChildren();let a=n?.checkpoints||[],o=n?.pending||[],s=e(`select.chat-select`,{"aria-label":`Checkpoint`},e(`option`,{value:``},`Select checkpoint`),...a.map(t=>e(`option`,{value:t.Number??t.number},`${t.Number??t.number} — ${t.Label??t.label??`checkpoint`}`))),c=e(`input.chat-dangerous-input`,{placeholder:`Process name or id`,"aria-label":`Process name or id`}),l=(e,t)=>()=>r(e,String(t.value||t).trim()),u=(t,n,r)=>e(`button.btn`,{onclick:()=>i(t,n)},r),d=o.length?o.map(t=>e(`div.chat-dangerous-pending`,e(`span`,t.description),e(`div.chat-update-actions`,u(t.id,`approve`,`Approve`),u(t.id,`permanent`,`Approve for 24h`),u(t.id,`deny`,`Deny`)))):[e(`span.chat-hint`,`No pending dangerous actions.`)];t.append(e(`div.chat-dangerous-head`,e(`strong`,`Dangerous actions`),e(`span.chat-hint`,`Every action requires approval.`)),e(`div.chat-dangerous-controls`,e(`div`,s,e(`button.btn`,{onclick:l(`rollback`,s)},`Request rollback`)),e(`div`,c,e(`button.btn`,{onclick:l(`stop`,c)},`Request stop`))),e(`div.chat-dangerous-pending-list`,...d))}function $e(e){return e?.status===501?null:`Could not check for updates.`}function et(){let{root:t,refs:n}=Fe(),{sessionList:r,transcript:i,composer:a,send:o,stop:s,status:c,persona:l,provider:u,model:d,restart:f,emptyState:p,updatePanel:h,dangerousPanel:g,commandMenu:_,sessionTools:v,commandHelp:y,checkUpdates:b,newChat:x}=n,S=``,C=[],w=!1,T=null,E=null,D={},O=[],k=[],A=0;f.onclick=()=>{window.confirm(`Reload the Telegram chat adapter?`)&&(a.value=`/restart`,H())},x.onclick=()=>{a.value=`/new`,H()};for(let e of t.querySelectorAll(`[data-chat-prompt]`))e.onclick=()=>{a.value=e.dataset.chatPrompt,a.focus()};function j(e){p.remove(),i.append(Be(e)),i.scrollTop=i.scrollHeight}function M(){let e=a.value.slice(0,a.selectionStart).match(/(?:^|\s)\/([^\s]*)$/);return e?e[1].toLowerCase():null}function N(){_.hidden=!0,k=[],A=0}function P(){let t=M();if(t===null){N();return}if(k=O.filter(e=>`${e.command} ${e.description}`.toLowerCase().includes(t)).slice(0,8),!k.length){N();return}A=Math.min(A,k.length-1),_.replaceChildren(...k.map((t,n)=>e(`button.chat-command-option`,{type:`button`,role:`option`,"aria-selected":String(n===A),onmousedown:e=>e.preventDefault(),onclick:()=>F(n)},e(`code`,t.command),e(`span`,t.description||t.usage||``)))),_.hidden=!1}function F(e){let t=k[e];if(!t)return;let n=a.value.slice(0,a.selectionStart),r=a.value.slice(a.selectionStart),i=n.search(/(?:^|\s)\/[^\s]*$/),o=i<0?n.length:i+ +(n[i]===` `);a.value=`${n.slice(0,o)}${t.command} ${r}`;let s=o+t.command.length+1;a.setSelectionRange(s,s),N(),a.focus()}async function I(){try{let e=await m.chatUpdate();E=e?.snapshot||null,Ze(h,e,{onDefer:async()=>{await m.chatUpdateDefer(E),Ze(h,{snapshot:{deferred:!0},available:[]},{onDefer:()=>{},onInstall:()=>{}}),c.textContent=`Update deferred`},onInstall:async()=>{c.textContent=`Installing…`,await m.chatUpdateInstall(E),c.textContent=`Update started`,I()}})}catch(t){let n=$e(t);h.replaceChildren(...n?[e(`span`,n)]:[])}}async function L(){try{Qe(g,await m.chatDangerous(),{onRequest:async(e,t)=>{if(t)try{await m.chatDangerousRequest(e,t),c.textContent=`Approval requested`,await L()}catch(e){c.textContent=e.message||`Could not request action`}},onDecision:async(e,t)=>{try{await m.chatDangerousDecision(e,t),c.textContent=t===`deny`?`Action denied`:`Action approved`,await L()}catch(e){c.textContent=e.message||`Approval failed`}}})}catch(t){g.replaceChildren(e(`span.chat-hint`,t.message||`Dangerous actions unavailable`))}}async function R(){let e=await m.chatSessions();C=e.sessions||[],D=e,Je({persona:l,provider:u,model:d,restart:f,dangerousPanel:g},e,L),O=Xe(y,e.commands),qe({sessionList:r,sessionTools:v,sessions:C,currentSession:S,onSelect:V}),!S&&C[0]&&await V(C[0].session_id)}async function z(e){if(S)try{c.textContent=`Working…`;let t=await m.chatMessage(Z(),e);t.session_id&&(S=t.session_id),c.textContent=t.reply||`Session updated`,await R(),S&&await V(S),v.open=!1}catch(e){c.textContent=e.message||`Session action failed`}}function B(e,t){let n=window.prompt(t);n?.trim()&&z(`${e} ${n.trim()}`)}async function V(e){S=e,qe({sessionList:r,sessionTools:v,sessions:C,currentSession:S,onSelect:V}),i.replaceChildren(),await m.chatMessage(Z(),`/resume ${e}`),l.value=D.active_personas?.[e]||D.active_persona||``;let t=await m.chatMessages(e);for(let e of t)j(e);i.children.length||i.append(p)}async function H(t=null){let{text:n,turn:r,isRetry:l}=Ke(t,a.value);if(!n||w)return;N(),w=!0,o.disabled=!0,s.disabled=!1,a.disabled=!0,c.textContent=l?`Retrying…`:`Thinking…`,l||(j({from:`web`,text:n}),a.value=``);let u=l?t.replyBubble:ze();l||i.append(u),u.querySelector(`.chat-retry`)?.remove(),u.querySelector(`.chat-tools`)?.replaceChildren();let d=u.querySelector(`.chat-bubble-text`),f=``,p=!1,m=!1,h;try{T=new AbortController,h=setTimeout(()=>{m=!0,T.abort()},12e4);let e=await fetch(`/api/chat/stream`,{method:`POST`,headers:{"Content-Type":`application/json`,Accept:`text/event-stream`},body:JSON.stringify({channel_id:Z(),source_id:r.sourceID,text:r.text}),signal:T.signal});if(!e.ok)throw Error(await e.text()||`${e.status} ${e.statusText}`);let t=e.body.getReader(),n=new TextDecoder,i=``;for(;;){let{value:e,done:r}=await t.read();i+=n.decode(e||new Uint8Array,{stream:!r});let a=i.split(` `);i=a.pop()||``;for(let e of a){let t=e.split(` -`).find(e=>e.startsWith(`data: `));if(!t)continue;let n=JSON.parse(t.slice(6));if(n.session_id&&(S=n.session_id),n.type===`delta`&&(f+=n.text,d=X(d,f)),n.type===`done`&&(p=!0,f||(f=n.text,d=X(d,f))),n.type===`error`)throw Error(n.text)}if(r)break}if(!p)throw Error(`chat stream ended before completion`);d.isConnected&&(d=X(d,f)),c.textContent=`Ready`,await R()}catch(t){if(t.name===`AbortError`&&!m)d=X(d,`Turn stopped.`),c.textContent=`Stopped`;else{d=X(d,`Unable to complete that turn: ${t.message||t}`);let n=He(r),i=e(`button.btn.chat-retry`,{type:`button`,onclick:()=>H({turn:n,replyBubble:u})},`Retry`);u.append(i),c.textContent=`Error — retry available`}}finally{clearTimeout(h),T=null,w=!1,o.disabled=!1,s.disabled=!0,a.disabled=!1,a.focus()}}o.onclick=()=>H(),s.onclick=()=>{let e=S;c.textContent=`Stopping…`,e&&m.chatCancel(e).catch(()=>{}),T?.abort()},a.oninput=P,a.onkeydown=e=>{if(!_.hidden&&k.length){if(e.key===`Enter`&&!e.shiftKey&&!e.ctrlKey&&!e.metaKey&&k.some(e=>e.command===a.value.trim())){e.preventDefault(),N(),H();return}if(e.key===`ArrowDown`){e.preventDefault(),A=(A+1)%k.length,P();return}if(e.key===`ArrowUp`){e.preventDefault(),A=(A+k.length-1)%k.length,P();return}if(e.key===`Enter`&&!e.shiftKey&&!e.ctrlKey&&!e.metaKey){e.preventDefault(),F(A);return}if(e.key===`Escape`){e.preventDefault(),N();return}}e.key===`Enter`&&!e.shiftKey&&(e.preventDefault(),H())},l.onchange=async()=>{!S||!l.value||(await m.chatPersona(S,l.value),D.active_personas={...D.active_personas||{},[S]:l.value},c.textContent=`Personality: ${l.value}`)},u.onchange=async()=>{Ke({model:d},D,u.value),u.value&&await U(`/model --provider ${u.value}`)},d.onchange=async()=>{d.value&&await U(`/model ${d.value}`)};async function U(e){try{let t=await m.chatMessage(Z(),e);c.textContent=t.reply||`Model updated`,await R()}catch(e){c.textContent=`Model update failed: ${e.message||e}`}}b.onclick=I;let W={retry:()=>z(`/retry`),undo:()=>z(`/undo`),title:()=>B(`/title`,`Session title`),branch:()=>B(`/branch`,`Branch name (optional)`),compress:()=>z(`/compress --preview`)};for(let e of t.querySelectorAll(`[data-chat-action]`))e.onclick=W[e.dataset.chatAction];return R().catch(e=>{c.textContent=e.message||`Chat unavailable`}),I(),t}var Q=[{path:`/`,label:`Dashboard`,icon:`dashboard`,view:E},{path:`/chat`,label:`Chat`,icon:`chat`,view:Ze},{path:`/tasks`,label:`Tasks`,icon:`tasks`,view:B},{path:`/logs`,label:`Logs`,icon:`logs`,view:we},{path:`/skills`,label:`Skills`,icon:`skills`,view:V},{path:`/workflows`,label:`Workflows`,icon:`workflows`,view:te},{path:`/memory`,label:`Memory`,icon:`memory`,view:je},{path:`/channels`,label:`Channels`,icon:`channels`,view:ne},{path:`/settings`,label:`Configuration`,icon:`settings`,view:le}],Qe=`archie.theme`;function $e(e){document.documentElement.dataset.theme=e,localStorage.setItem(Qe,e)}function $(){return localStorage.getItem(Qe)||`dark`}function et(t){let r=new Map,i=e(`nav.nav`,...[null].flatMap(()=>[...Q.map(n=>{let i=e(`a.nav-item`,{href:`#${n.path}`,onclick:e=>{n.soon?e.preventDefault():t(n.path)},"aria-disabled":n.soon||void 0,title:n.soon?`Coming soon`:void 0},l(n.icon),e(`span.nav-label`,n.label),n.soon&&e(`span.nav-soon`,`soon`));return r.set(n.path,i),i})])),a=e(`button.icon-btn`,{title:`Toggle light and dark`,"aria-label":`Toggle light and dark`,onclick:()=>{$e($()===`dark`?`light`:`dark`),n(a,l($()===`dark`?`moon`:`sun`))}});n(a,l($()===`dark`?`moon`:`sun`));let o,s=()=>{o?.classList.remove(`is-open`),c.setAttribute(`aria-expanded`,`false`)},c=e(`button.icon-btn.mobile-search-toggle`,{type:`button`,"aria-label":`Open Jump to navigation`,"aria-expanded":`false`,onclick:()=>{o.classList.add(`is-open`),c.setAttribute(`aria-expanded`,`true`),u.focus()}},l(`search`,{size:15})),u=e(`input`,{type:`search`,placeholder:`Jump to…`,"aria-label":`Jump to a section`,onkeydown:e=>{if(e.key===`Escape`){s(),e.target.blur();return}if(e.key!==`Enter`)return;let n=e.target.value.trim().toLowerCase(),r=Q.find(e=>!e.soon&&e.label.toLowerCase().startsWith(n));r&&(t(r.path),e.target.value=``,e.target.blur(),s())}});return o=e(`div.topbar-search`,e(`span.topbar-search-icon`,{"aria-hidden":`true`},l(`search`,{size:15})),c,u),{node:e(`header.topbar`,e(`div.brand`,e(`span.brand-mark`,`A`),`Archie`),i,e(`div.topbar-end`,o,e(`button.icon-btn`,{title:`Documentation`,"aria-label":`Documentation`},l(`help`)),a,e(`div.avatar`,{title:`Signed in locally`},`A`))),highlight(e){for(let[t,n]of r)t===e?n.setAttribute(`aria-current`,`page`):n.removeAttribute(`aria-current`)}}}function tt(){$e($());let t=e(`main.main`),r=et(i);n(document.getElementById(`app`),e(`div.shell`,r.node,t));function i(e){location.hash===`#${e}`?a(e):location.hash=e}function a(e){t.firstElementChild?.dispatchEvent(new CustomEvent(`archie:teardown`));let[i,a=``]=e.split(`?`,2),o=Q.find(e=>e.path===i)||Q[0];r.highlight(o.path),n(t,o.view?o.view(new URLSearchParams(a)):nt(o))}window.addEventListener(`hashchange`,()=>a(location.hash.slice(1)||`/`)),a(location.hash.slice(1)||`/`)}function nt(t){return e(`div`,e(`div.page-head`,e(`div`,e(`h1.page-title`,t.label))),e(`div.card`,e(`div.empty`,e(`div.empty-title`,`${t.label} is not built yet`),e(`div`,`This section is next up. Nothing is broken.`))))}tt(); \ No newline at end of file +`).find(e=>e.startsWith(`data: `));if(!t)continue;let n=JSON.parse(t.slice(6));if(n.session_id&&(S=n.session_id),n.type===`delta`&&(f+=n.text,d=X(d,f)),n.type===`tool`&&Ve(u.querySelector(`.chat-bubble`)??u,n),n.type===`done`&&(p=!0,f||(f=n.text,d=X(d,f))),n.type===`error`)throw Error(n.text)}if(r)break}if(!p)throw Error(`chat stream ended before completion`);d.isConnected&&(d=X(d,f)),c.textContent=`Ready`,await R()}catch(t){if(t.name===`AbortError`&&!m)d=X(d,`Turn stopped.`),c.textContent=`Stopped`;else{d=X(d,`Unable to complete that turn: ${t.message||t}`);let n=Ge(r),i=e(`button.btn.chat-retry`,{type:`button`,onclick:()=>H({turn:n,replyBubble:u})},`Retry`);u.append(i),c.textContent=`Error — retry available`}}finally{clearTimeout(h),T=null,w=!1,o.disabled=!1,s.disabled=!0,a.disabled=!1,a.focus()}}o.onclick=()=>H(),s.onclick=()=>{let e=S;c.textContent=`Stopping…`,e&&m.chatCancel(e).catch(()=>{}),T?.abort()},a.oninput=P,a.onkeydown=e=>{if(!_.hidden&&k.length){if(e.key===`Enter`&&!e.shiftKey&&!e.ctrlKey&&!e.metaKey&&k.some(e=>e.command===a.value.trim())){e.preventDefault(),N(),H();return}if(e.key===`ArrowDown`){e.preventDefault(),A=(A+1)%k.length,P();return}if(e.key===`ArrowUp`){e.preventDefault(),A=(A+k.length-1)%k.length,P();return}if(e.key===`Enter`&&!e.shiftKey&&!e.ctrlKey&&!e.metaKey){e.preventDefault(),F(A);return}if(e.key===`Escape`){e.preventDefault(),N();return}}e.key===`Enter`&&!e.shiftKey&&(e.preventDefault(),H())},l.onchange=async()=>{!S||!l.value||(await m.chatPersona(S,l.value),D.active_personas={...D.active_personas||{},[S]:l.value},c.textContent=`Personality: ${l.value}`)},u.onchange=async()=>{Ye({model:d},D,u.value),u.value&&await U(`/model --provider ${u.value}`)},d.onchange=async()=>{d.value&&await U(`/model ${d.value}`)};async function U(e){try{let t=await m.chatMessage(Z(),e);c.textContent=t.reply||`Model updated`,await R()}catch(e){c.textContent=`Model update failed: ${e.message||e}`}}b.onclick=I;let W={retry:()=>z(`/retry`),undo:()=>z(`/undo`),title:()=>B(`/title`,`Session title`),branch:()=>B(`/branch`,`Branch name (optional)`),compress:()=>z(`/compress --preview`)};for(let e of t.querySelectorAll(`[data-chat-action]`))e.onclick=W[e.dataset.chatAction];return R().catch(e=>{c.textContent=e.message||`Chat unavailable`}),I(),t}var Q=[{path:`/`,label:`Dashboard`,icon:`dashboard`,view:E},{path:`/chat`,label:`Chat`,icon:`chat`,view:et},{path:`/tasks`,label:`Tasks`,icon:`tasks`,view:B},{path:`/logs`,label:`Logs`,icon:`logs`,view:we},{path:`/skills`,label:`Skills`,icon:`skills`,view:V},{path:`/workflows`,label:`Workflows`,icon:`workflows`,view:te},{path:`/memory`,label:`Memory`,icon:`memory`,view:je},{path:`/channels`,label:`Channels`,icon:`channels`,view:ne},{path:`/settings`,label:`Configuration`,icon:`settings`,view:le}],tt=`archie.theme`;function nt(e){document.documentElement.dataset.theme=e,localStorage.setItem(tt,e)}function $(){return localStorage.getItem(tt)||`dark`}function rt(t){let r=new Map,i=e(`nav.nav`,...[null].flatMap(()=>[...Q.map(n=>{let i=e(`a.nav-item`,{href:`#${n.path}`,onclick:e=>{n.soon?e.preventDefault():t(n.path)},"aria-disabled":n.soon||void 0,title:n.soon?`Coming soon`:void 0},l(n.icon),e(`span.nav-label`,n.label),n.soon&&e(`span.nav-soon`,`soon`));return r.set(n.path,i),i})])),a=e(`button.icon-btn`,{title:`Toggle light and dark`,"aria-label":`Toggle light and dark`,onclick:()=>{nt($()===`dark`?`light`:`dark`),n(a,l($()===`dark`?`moon`:`sun`))}});n(a,l($()===`dark`?`moon`:`sun`));let o,s=()=>{o?.classList.remove(`is-open`),c.setAttribute(`aria-expanded`,`false`)},c=e(`button.icon-btn.mobile-search-toggle`,{type:`button`,"aria-label":`Open Jump to navigation`,"aria-expanded":`false`,onclick:()=>{o.classList.add(`is-open`),c.setAttribute(`aria-expanded`,`true`),u.focus()}},l(`search`,{size:15})),u=e(`input`,{type:`search`,placeholder:`Jump to…`,"aria-label":`Jump to a section`,onkeydown:e=>{if(e.key===`Escape`){s(),e.target.blur();return}if(e.key!==`Enter`)return;let n=e.target.value.trim().toLowerCase(),r=Q.find(e=>!e.soon&&e.label.toLowerCase().startsWith(n));r&&(t(r.path),e.target.value=``,e.target.blur(),s())}});return o=e(`div.topbar-search`,e(`span.topbar-search-icon`,{"aria-hidden":`true`},l(`search`,{size:15})),c,u),{node:e(`header.topbar`,e(`div.brand`,e(`span.brand-mark`,`A`),`Archie`),i,e(`div.topbar-end`,o,e(`button.icon-btn`,{title:`Documentation`,"aria-label":`Documentation`},l(`help`)),a,e(`div.avatar`,{title:`Signed in locally`},`A`))),highlight(e){for(let[t,n]of r)t===e?n.setAttribute(`aria-current`,`page`):n.removeAttribute(`aria-current`)}}}function it(){nt($());let t=e(`main.main`),r=rt(i);n(document.getElementById(`app`),e(`div.shell`,r.node,t));function i(e){location.hash===`#${e}`?a(e):location.hash=e}function a(e){t.firstElementChild?.dispatchEvent(new CustomEvent(`archie:teardown`));let[i,a=``]=e.split(`?`,2),o=Q.find(e=>e.path===i)||Q[0];r.highlight(o.path),n(t,o.view?o.view(new URLSearchParams(a)):at(o))}window.addEventListener(`hashchange`,()=>a(location.hash.slice(1)||`/`)),a(location.hash.slice(1)||`/`)}function at(t){return e(`div`,e(`div.page-head`,e(`div`,e(`h1.page-title`,t.label))),e(`div.card`,e(`div.empty`,e(`div.empty-title`,`${t.label} is not built yet`),e(`div`,`This section is next up. Nothing is broken.`))))}it(); \ No newline at end of file diff --git a/ui/src/chat/chat-render.js b/ui/src/chat/chat-render.js index 2e808ce48..bd2a6a21d 100644 --- a/ui/src/chat/chat-render.js +++ b/ui/src/chat/chat-render.js @@ -5,6 +5,22 @@ export function sessionTitle(session) { return session.title || session.branch_name || `Conversation ${session.session_id.slice(0, 8)}`; } +// assistantBubble builds the row a live reply streams into. The tool list is +// created empty and up front so tool activity has somewhere to land that the +// streaming text replacement cannot destroy, and so its position above the +// reply is fixed by the bubble rather than by insertion order. +export function assistantBubble() { + return el( + "div.chat-bubble-row.assistant", + el( + "div.chat-bubble", + el("div.chat-bubble-meta", "Archie"), + el("div.chat-tools"), + el("div.chat-bubble-text"), + ), + ); +} + export function chatBubble(message) { const from = message.from ?? message.From ?? ""; const text = message.text ?? message.Text ?? ""; diff --git a/ui/src/chat/chat-tools.js b/ui/src/chat/chat-tools.js new file mode 100644 index 000000000..aa74954f9 --- /dev/null +++ b/ui/src/chat/chat-tools.js @@ -0,0 +1,38 @@ +import { el } from "../base/dom.js"; + +// Tool activity is rendered into its own list inside the assistant bubble +// rather than folded into the reply text: the text node is replaced wholesale +// on every streamed frame, and again on the final reply, so anything written +// into it is lost the next time the answer grows. +// +// The list is part of the bubble from the start (see assistantBubble) and +// sits above the reply, matching the shape the same turn takes in Telegram: +// the work first, then the answer built from it. +export function appendToolCall(host, event) { + const name = event?.tool ?? ""; + if (!host || !name) return null; + + const summary = event.text ?? ""; + const line = el( + "div.chat-tool", + el("span.chat-tool-icon", "🔧"), + el("span.chat-tool-name", name), + el("span.chat-tool-summary", summary), + ); + // A failure reads as a completed step unless it is marked as one. + if (summary.startsWith("failed:")) line.className = "chat-tool failed"; + + toolList(host).append(line); + return line; +} + +// toolList returns the bubble's tool list. A bubble built elsewhere may not +// have one, in which case the list is appended rather than dropped -- tool +// activity out of position still beats tool activity nowhere. +function toolList(host) { + const existing = host.querySelector(".chat-tools"); + if (existing) return existing; + const list = el("div.chat-tools"); + host.append(list); + return list; +} diff --git a/ui/src/chat/chat.css b/ui/src/chat/chat.css index 5c53f5e24..d65642eb5 100644 --- a/ui/src/chat/chat.css +++ b/ui/src/chat/chat.css @@ -59,6 +59,15 @@ .chat-bubble-text table { width: 100%; margin: .7rem 0; border-collapse: collapse; font-size: .92em; } .chat-bubble-text th, .chat-bubble-text td { padding: .45rem .6rem; text-align: left; border: 1px solid var(--border); } .chat-bubble-text th { color: var(--fg); background: color-mix(in srgb, var(--surface) 70%, transparent); font-weight: 700; } +/* Tool activity: a quiet log above the answer it produced. It is deliberately + smaller and dimmer than the reply -- it is evidence, not the point. An empty + list renders nothing, so a turn that called no tools looks untouched. */ +.chat-tools { display: grid; gap: .2rem; margin-bottom: .5rem; font-size: .78rem; } +.chat-tools:empty { display: none; } +.chat-tool { display: flex; gap: .4rem; align-items: baseline; color: var(--fg-muted); overflow-wrap: anywhere; } +.chat-tool-name { color: var(--fg); font-weight: 600; } +.chat-tool.failed .chat-tool-summary { color: var(--danger); } + .chat-compose { padding: .85rem 1rem 1rem; border-top: 1px solid var(--border); } .chat-composer-shell { position: relative; padding: .7rem .8rem .55rem; background: var(--surface-hover); border: 1px solid var(--border-strong); border-radius: .9rem; } .chat-composer { width: 100%; min-height: 3.3rem; resize: vertical; box-sizing: border-box; background: transparent; color: var(--fg); border: 0; padding: .25rem 0; font: inherit; } diff --git a/ui/src/chat/chat.js b/ui/src/chat/chat.js index 9cd64e617..229609b91 100644 --- a/ui/src/chat/chat.js +++ b/ui/src/chat/chat.js @@ -1,8 +1,9 @@ import { el } from "../base/dom.js"; import { api } from "../base/api.js"; import { createChatView } from "./chat-view.js"; -import { chatBubble } from "./chat-render.js"; +import { assistantBubble, chatBubble } from "./chat-render.js"; import { updateStreamingReply } from "./chat-stream.js"; +import { appendToolCall } from "./chat-tools.js"; import { channelID } from "./chat-state.js"; import { retryChatTurn, resolveTurn } from "./chat-retry.js"; import { renderCommands, renderModels, renderSelectors, renderSessions } from "./chat-catalog.js"; @@ -207,9 +208,13 @@ export function chatPage() { appendMessage({ from: "web", text }); composer.value = ""; } - const replyBubble = isRetry ? retry.replyBubble : el("div.chat-bubble-row.assistant", el("div.chat-bubble", el("div.chat-bubble-meta", "Archie"), el("div.chat-bubble-text"))); + const replyBubble = isRetry ? retry.replyBubble : assistantBubble(); if (!isRetry) transcript.append(replyBubble); replyBubble.querySelector(".chat-retry")?.remove(); + // A retry reuses the failed turn's bubble, so its tool list still holds + // the attempt that did not finish. Clear it: appending to it would + // present another turn's work as this one's evidence. + replyBubble.querySelector(".chat-tools")?.replaceChildren(); let replyText = replyBubble.querySelector(".chat-bubble-text"); let streamedText = ""; let finished = false; @@ -245,6 +250,9 @@ export function chatPage() { streamedText += event.text; replyText = updateStreamingReply(replyText, streamedText); } + if (event.type === "tool") { + appendToolCall(replyBubble.querySelector(".chat-bubble") ?? replyBubble, event); + } if (event.type === "done") { finished = true; if (!streamedText) { diff --git a/ui/test/chat-tools.test.js b/ui/test/chat-tools.test.js new file mode 100644 index 000000000..08f05e3bc --- /dev/null +++ b/ui/test/chat-tools.test.js @@ -0,0 +1,80 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import "./shim.js"; +import { assistantBubble } from "../src/chat/chat-render.js"; +import { appendToolCall } from "../src/chat/chat-tools.js"; + +// childIndex reports where a node sits among its parent's children, which is +// how the tests assert that the work is listed above the answer. +function childIndex(parent, selector) { + const node = parent.querySelector(selector); + return parent.childNodes.indexOf(node); +} + +test("a tool call is listed above the answer it produced", () => { + const row = assistantBubble(); + const host = row.querySelector(".chat-bubble"); + + appendToolCall(host, { tool: "shell", text: "exit 0" }); + + const tools = host.querySelector(".chat-tools"); + assert.equal(tools.childNodes.length, 1); + assert.equal(host.querySelector(".chat-tool-name").textContent, "shell"); + assert.equal(host.querySelector(".chat-tool-summary").textContent, "exit 0"); + assert.ok( + childIndex(host, ".chat-tools") < childIndex(host, ".chat-bubble-text"), + "tool activity must render above the reply text", + ); +}); + +test("further tool calls join the same list in order", () => { + const row = assistantBubble(); + const host = row.querySelector(".chat-bubble"); + + appendToolCall(host, { tool: "memory_edit", text: "added entry" }); + appendToolCall(host, { tool: "shell", text: "exit 0" }); + + const list = host.querySelector(".chat-tools"); + const names = list.childNodes.map((line) => line.querySelector(".chat-tool-name").textContent); + assert.deepEqual(names, ["memory_edit", "shell"]); +}); + +test("a failed tool call is marked so it does not read as success", () => { + const row = assistantBubble(); + const host = row.querySelector(".chat-bubble"); + + appendToolCall(host, { tool: "read", text: "failed: no such file" }); + + const line = host.querySelector(".chat-tool"); + assert.match(line.className, /failed/); +}); + +test("a successful tool call is not marked as failed", () => { + const row = assistantBubble(); + const host = row.querySelector(".chat-bubble"); + + appendToolCall(host, { tool: "read", text: "42 lines" }); + + assert.doesNotMatch(host.querySelector(".chat-tool").className, /failed/); +}); + +test("a tool event with no name is ignored rather than rendered blank", () => { + const row = assistantBubble(); + const host = row.querySelector(".chat-bubble"); + + appendToolCall(host, { tool: "", text: "exit 0" }); + + assert.equal(host.querySelector(".chat-tools").childNodes.length, 0); +}); + +// A bubble that predates the tool list -- a retried turn reusing an older +// one -- must still show tool activity rather than silently dropping it. +test("a bubble without a tool list still records tool activity", () => { + const row = assistantBubble(); + const host = row.querySelector(".chat-bubble"); + host.childNodes = host.childNodes.filter((node) => node.className !== "chat-tools"); + + appendToolCall(host, { tool: "shell", text: "exit 0" }); + + assert.equal(host.querySelector(".chat-tool-name").textContent, "shell"); +});