feat(chat): show tool calls inline and stream replies into one message
Chat returned only the final answer, so a wrong tool choice, a hallucinated action, or a footgun like `systemctl stop` against `restart` was invisible from the conversation. Only text deltas were threaded through the turn, and tool calls executed inside Generate with nowhere to report. Replace the delta callback with gateway.TurnStream, which carries text fragments and completed tool calls on one ordered path from the model loop to the channel. The sequence is the point: a tool line that overtakes the sentence it interrupted describes a turn that never happened. Text-only callers use gateway.DeltaFunc and are unaffected. A tool is reported on its result, never on its call -- the call part carries no outcome yet, so reporting both would list every tool twice, once with nothing to say. Telegram renders it behind [chat.telegram] show_tool_calls, off by default: this is debugging visibility, and it narrates every internal step to provide it. The dashboard always shows it, being the operator's own observability surface; its tool lines are live-only and are not persisted with the conversation. The Telegram live reply changes mechanism, and with it what a frame MEANS. It used to stream through sendMessageDraft: drafts are ephemeral 30-second previews, so a tool call longer than that outlived the preview, and successive frames were observed concatenated with their cursors intact. It now sends one real message and edits it. An update used to be "the next preview of the answer"; it is now "the whole answer as it currently stands, replacing what is there". The cursor is appended at render time and never stored, so exactly one exists and always at the end, and a frame too long for one message is cut to its tail rather than rejected outright. A stopped or failed turn now edits the cursor away instead of leaving it blinking on a message nothing will finish. That cleanup runs on a live context because turnCtx is already dead, and it runs on both failure branches: ai-sdk raises an aborted stream as ErrAborted with ctx.Err() formatted in rather than wrapped, so errors.Is cannot be trusted to tell a /stop from a fault. Closes #467
This commit is contained in:
1 parent
fd8f9f925f
commit
06dc034a72
27 files changed
+1492
-179
No files matched your search
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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]]
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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"))
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
+13
-11
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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]) + "…"
|
||||
}
|
||||
@@ -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"})
|
||||
}
|
||||
+18
-11
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+48
-17
@@ -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) {
|
||||
|
||||
@@ -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.
|
||||
|
||||
Vendored
+1
-1
@@ -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(--accenLine truncated
|
||||
: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(--accenLine truncated
|
||||
Vendored
+2
-2
@@ -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<t.length&&n.append(document.createTextNode(t.slice(i))),n}function Ie(t){let n=e(`div.chat-bubble-text`),r=String(t||``).split(`
|
||||
`),i=[],a=null,o=null,s=()=>{i.length&&(n.append(e(`p`,J(i.join(` `)))),i=[])},c=()=>{a&&n.append(a),a=null};for(let t=0;t<r.length;t++){let l=r[t];if(l.startsWith("```")){s(),c(),o?(n.append(e(`pre`,e(`code`,o.join(`
|
||||
`)))),o=null):o=[];continue}if(o){o.push(l);continue}let u=Y(l),d=t+1<r.length?Y(r[t+1]):null;if(u&&d&&Le(d)&&u.length===d.length){s(),c();let i=[];for(t+=2;t<r.length;t++){let e=Y(r[t]);if(!e||e.length!==u.length){t--;break}i.push(e)}let a=e(`tbody`);for(let t of i)a.append(e(`tr`,...t.map(t=>e(`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
Line truncated
|
||||
`)))),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
Line truncated
|
||||
|
||||
`);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.has
Line truncated
|
||||
`).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(`hashLine truncated
|
||||
@@ -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 ?? "";
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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; }
|
||||
|
||||
+10
-2
@@ -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) {
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
Reference in new issue
Block a user