Merge pull request #477 from samcharles93/fix/archie-core-tb3a
fix(chat): harden inline tool call streaming
This commit is contained in:
commit
1e5e1fbcc8
12 files changed
+537
-62
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
@@ -18,7 +19,7 @@ func (s *recordingStream) Delta(text string) {
|
||||
}
|
||||
|
||||
func (s *recordingStream) ToolCall(event gateway.ToolCallEvent) {
|
||||
s.events = append(s.events, "tool:"+event.Name+":"+event.Summary())
|
||||
s.events = append(s.events, "tool:"+event.Name+":"+event.Parameters+":"+event.Summary())
|
||||
}
|
||||
|
||||
func streamOf(parts ...core.StreamPart) <-chan core.StreamPart {
|
||||
@@ -55,14 +56,14 @@ func TestDrainChatStream(t *testing.T) {
|
||||
{Type: core.StreamPartTextDelta, TextDelta: " done"},
|
||||
},
|
||||
wantText: "checking done",
|
||||
wantEvents: []string{"text:checking", "tool:shell:exit 0", "text: done"},
|
||||
wantEvents: []string{"text:checking", `tool:shell:{"cmd":"[string]"}: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"},
|
||||
wantEvents: []string{"tool:shell::failed: exit status 2"},
|
||||
},
|
||||
{
|
||||
// The tool call part carries no outcome yet: reporting it as
|
||||
@@ -107,6 +108,49 @@ func TestDrainChatStream(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDrainChatStreamCorrelatesBoundsAndRedactsToolParameters(t *testing.T) {
|
||||
secret := strings.Repeat("s", 200)
|
||||
sink := &recordingStream{}
|
||||
drainChatStream(streamOf(
|
||||
core.StreamPart{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{
|
||||
ToolCallID: "call-2",
|
||||
ToolName: "shell",
|
||||
Input: `{"cmd":"curl -H 'Authorization: Bearer ` + secret + `'","nested":{"api_token":"` + secret + `"}}`,
|
||||
}},
|
||||
core.StreamPart{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{
|
||||
ToolCallID: "call-1",
|
||||
ToolName: "read",
|
||||
Input: `{"path":"/tmp/one"}`,
|
||||
}},
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "call-1",
|
||||
ToolName: "read",
|
||||
Output: "one",
|
||||
}},
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "call-2",
|
||||
ToolName: "shell",
|
||||
Output: "ok",
|
||||
}},
|
||||
), sink)
|
||||
|
||||
if len(sink.events) != 2 {
|
||||
t.Fatalf("events = %v, want two completed calls", sink.events)
|
||||
}
|
||||
if sink.events[0] != `tool:read:{"path":"[string]"}:one` {
|
||||
t.Fatalf("first event = %q, want parameters correlated by call ID", sink.events[0])
|
||||
}
|
||||
if strings.Contains(sink.events[1], secret) {
|
||||
t.Fatalf("second event exposed secret parameters: %q", sink.events[1])
|
||||
}
|
||||
if !strings.Contains(sink.events[1], `"api_token":"[redacted]"`) {
|
||||
t.Fatalf("second event = %q, want redacted token", sink.events[1])
|
||||
}
|
||||
if len([]rune(sink.events[1])) > 180 {
|
||||
t.Fatalf("second event is %d runes, want bounded parameters", len([]rune(sink.events[1])))
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
||||
@@ -86,7 +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.SetShowToolCalls(cfg.Chat.Telegram.ShowToolCalls)
|
||||
tg.ReleaseAnnouncements = &releaseannounce.Announcer{
|
||||
StatePath: releaseAnnouncementStatePath(cfg.WorkDir, cfg.BotUser),
|
||||
Components: []releaseannounce.Component{
|
||||
@@ -156,10 +156,10 @@ func makeTelegramReload(s telegramSetup) func(*telegram.Gateway) error {
|
||||
}
|
||||
g.Token = token
|
||||
g.AllowedUserIDs = newCfg.Chat.Telegram.AllowedUserIDs
|
||||
g.ShowToolCalls = newCfg.Chat.Telegram.ShowToolCalls
|
||||
g.SetShowToolCalls(newCfg.Chat.Telegram.ShowToolCalls)
|
||||
s.Log.Info("chat gateway config reloaded",
|
||||
"allowed_user_ids", len(g.AllowedUserIDs),
|
||||
"show_tool_calls", g.ShowToolCalls)
|
||||
"show_tool_calls", g.ShowToolCalls())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -266,7 +266,13 @@ func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, o
|
||||
// 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 {
|
||||
type pendingToolCall struct {
|
||||
name string
|
||||
parameters string
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
pending := make(map[string]pendingToolCall)
|
||||
for part := range parts {
|
||||
switch part.Type {
|
||||
case core.StreamPartTextDelta:
|
||||
@@ -277,14 +283,30 @@ func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream) stri
|
||||
if turn != nil {
|
||||
turn.Delta(part.TextDelta)
|
||||
}
|
||||
case core.StreamPartToolCall:
|
||||
if part.ToolCall == nil || part.ToolCall.ToolCallID == "" {
|
||||
continue
|
||||
}
|
||||
pending[part.ToolCall.ToolCallID] = pendingToolCall{
|
||||
name: part.ToolCall.ToolName,
|
||||
parameters: gateway.SummarizeToolParameters(part.ToolCall.Input),
|
||||
}
|
||||
case core.StreamPartToolResult:
|
||||
if part.ToolResult == nil || turn == nil {
|
||||
continue
|
||||
}
|
||||
call := pending[part.ToolResult.ToolCallID]
|
||||
delete(pending, part.ToolResult.ToolCallID)
|
||||
name := part.ToolResult.ToolName
|
||||
if name == "" {
|
||||
name = call.name
|
||||
}
|
||||
turn.ToolCall(gateway.ToolCallEvent{
|
||||
Name: part.ToolResult.ToolName,
|
||||
Output: part.ToolResult.Output,
|
||||
Err: part.ToolResult.Error,
|
||||
ID: part.ToolResult.ToolCallID,
|
||||
Name: name,
|
||||
Parameters: call.parameters,
|
||||
Output: part.ToolResult.Output,
|
||||
Err: part.ToolResult.Error,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -61,9 +63,14 @@ type liveReply struct {
|
||||
b *bot.Bot
|
||||
chatID int64
|
||||
messageThreadID int
|
||||
showToolCalls bool
|
||||
// interval throttles updates; zero renders every change.
|
||||
interval time.Duration
|
||||
|
||||
cancelRender context.CancelFunc
|
||||
renderRequests chan chan struct{}
|
||||
renderDone chan struct{}
|
||||
|
||||
mu sync.Mutex
|
||||
// buf is the canonical buffer: assistant text and tool lines in the
|
||||
// order the model produced them.
|
||||
@@ -78,15 +85,63 @@ type liveReply struct {
|
||||
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{
|
||||
// newLiveReply creates a renderer bound to one chat/thread. showToolCalls is
|
||||
// copied into the reply so a config reload cannot change a turn mid-stream.
|
||||
func (g *Gateway) newLiveReply(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, showToolCalls bool) *liveReply {
|
||||
renderCtx, cancelRender := context.WithCancel(ctx)
|
||||
live := &liveReply{
|
||||
g: g,
|
||||
b: b,
|
||||
chatID: chatID,
|
||||
messageThreadID: messageThreadID,
|
||||
showToolCalls: showToolCalls,
|
||||
interval: liveInterval,
|
||||
cancelRender: cancelRender,
|
||||
renderRequests: make(chan chan struct{}, 1),
|
||||
renderDone: make(chan struct{}),
|
||||
}
|
||||
go live.runRenderer(renderCtx)
|
||||
return live
|
||||
}
|
||||
|
||||
func (l *liveReply) runRenderer(ctx context.Context) {
|
||||
defer close(l.renderDone)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case completed := <-l.renderRequests:
|
||||
l.render(ctx)
|
||||
if completed != nil {
|
||||
close(completed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *liveReply) requestRender() {
|
||||
select {
|
||||
case l.renderRequests <- nil:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (l *liveReply) flushRendering() {
|
||||
completed := make(chan struct{})
|
||||
select {
|
||||
case l.renderRequests <- completed:
|
||||
case <-l.renderDone:
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-completed:
|
||||
case <-l.renderDone:
|
||||
}
|
||||
}
|
||||
|
||||
func (l *liveReply) stopRendering() {
|
||||
l.cancelRender()
|
||||
<-l.renderDone
|
||||
}
|
||||
|
||||
// Delta appends the next fragment of assistant text. It satisfies
|
||||
@@ -102,7 +157,7 @@ func (l *liveReply) Delta(text string) {
|
||||
if throttled {
|
||||
return
|
||||
}
|
||||
l.render(context.Background())
|
||||
l.requestRender()
|
||||
}
|
||||
|
||||
// ToolCall appends one tool-activity line. It satisfies gateway.TurnStream.
|
||||
@@ -111,10 +166,14 @@ func (l *liveReply) Delta(text string) {
|
||||
// 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 == "" {
|
||||
if !l.showToolCalls || event.Name == "" {
|
||||
return
|
||||
}
|
||||
line := toolCallPrefix + event.Name + " — " + event.Summary()
|
||||
line := toolCallPrefix + event.Name
|
||||
if event.Parameters != "" {
|
||||
line += " " + event.Parameters
|
||||
}
|
||||
line += " — " + event.Summary()
|
||||
|
||||
l.mu.Lock()
|
||||
if l.buf.Len() > 0 && !strings.HasSuffix(l.buf.String(), "\n") {
|
||||
@@ -126,9 +185,9 @@ func (l *liveReply) ToolCall(event gateway.ToolCallEvent) {
|
||||
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())
|
||||
// few of them: wake the renderer immediately so the user sees what ran
|
||||
// while it is still relevant. The callback itself never waits on HTTP.
|
||||
l.requestRender()
|
||||
}
|
||||
|
||||
// render writes the current buffer, cursor and all, to the live message.
|
||||
@@ -139,8 +198,6 @@ func (l *liveReply) render(ctx context.Context) {
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
l.rendered = body
|
||||
l.last = time.Now()
|
||||
messageID := l.messageID
|
||||
l.mu.Unlock()
|
||||
|
||||
@@ -151,7 +208,16 @@ func (l *liveReply) render(ctx context.Context) {
|
||||
l.open(ctx, body)
|
||||
return
|
||||
}
|
||||
l.edit(ctx, messageID, body)
|
||||
if err := l.edit(ctx, messageID, body); err == nil {
|
||||
l.markRendered(body)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *liveReply) markRendered(body string) {
|
||||
l.mu.Lock()
|
||||
l.rendered = body
|
||||
l.last = time.Now()
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// finalize replaces the live message with the turn's authoritative reply,
|
||||
@@ -159,14 +225,18 @@ func (l *liveReply) render(ctx context.Context) {
|
||||
// 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) {
|
||||
// Finish any queued live frame before replacing it, then retire the
|
||||
// worker so no stale edit can race the authoritative delivery.
|
||||
l.flushRendering()
|
||||
l.stopRendering()
|
||||
|
||||
l.mu.Lock()
|
||||
content := l.finalText(reply)
|
||||
l.rendered = content
|
||||
messageID := l.messageID
|
||||
l.mu.Unlock()
|
||||
|
||||
if content == "" {
|
||||
l.abandon(ctx)
|
||||
l.abandonStopped(ctx)
|
||||
return
|
||||
}
|
||||
if messageID == 0 {
|
||||
@@ -175,9 +245,16 @@ func (l *liveReply) finalize(ctx context.Context, reply string) {
|
||||
}
|
||||
|
||||
// The live message is one message, so an oversized reply keeps it as
|
||||
// the first part and sends the remainder after it.
|
||||
// the first part and sends the remainder after it. If the edit cannot
|
||||
// be delivered in either rich or plain form, send the complete reply
|
||||
// through the normal split path rather than losing the authoritative
|
||||
// answer behind a stale live frame.
|
||||
parts := splitLongMessage(content, messageMaxLen)
|
||||
l.edit(ctx, messageID, parts[0])
|
||||
if err := l.edit(ctx, messageID, parts[0]); err != nil {
|
||||
l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, content)
|
||||
return
|
||||
}
|
||||
l.markRendered(parts[0])
|
||||
for _, part := range parts[1:] {
|
||||
l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, part)
|
||||
}
|
||||
@@ -186,20 +263,30 @@ func (l *liveReply) finalize(ctx context.Context, reply string) {
|
||||
// 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.stopRendering()
|
||||
l.abandonStopped(ctx)
|
||||
}
|
||||
|
||||
func (l *liveReply) abandonStopped(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 {
|
||||
if text == "" || text == l.rendered {
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
l.rendered = text
|
||||
l.mu.Unlock()
|
||||
|
||||
l.edit(ctx, messageID, text)
|
||||
if messageID == 0 {
|
||||
l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, text)
|
||||
return
|
||||
}
|
||||
if err := l.edit(ctx, messageID, text); err == nil {
|
||||
l.markRendered(text)
|
||||
}
|
||||
}
|
||||
|
||||
// body renders the canonical buffer for a mid-turn frame. The caller holds
|
||||
@@ -263,6 +350,8 @@ func (l *liveReply) open(ctx context.Context, body string) {
|
||||
}
|
||||
l.mu.Lock()
|
||||
l.messageID = msg.ID
|
||||
l.rendered = body
|
||||
l.last = time.Now()
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
@@ -272,22 +361,27 @@ func (l *liveReply) open(ctx context.Context, body string) {
|
||||
// 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{
|
||||
func (l *liveReply) edit(ctx context.Context, messageID int, body string) error {
|
||||
_, richErr := l.b.EditMessageText(ctx, &bot.EditMessageTextParams{
|
||||
ChatID: l.chatID,
|
||||
MessageID: messageID,
|
||||
RichMessage: &models.InputRichMessage{Markdown: body},
|
||||
})
|
||||
if err == nil {
|
||||
return
|
||||
if richErr == nil {
|
||||
return nil
|
||||
}
|
||||
l.g.log.Debug("live reply rich edit failed, retrying unformatted", "error", err)
|
||||
l.g.log.Debug("live reply rich edit failed, retrying unformatted", "error", richErr)
|
||||
|
||||
if _, err := l.b.EditMessageText(ctx, &bot.EditMessageTextParams{
|
||||
if _, plainErr := 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)
|
||||
}); plainErr != nil {
|
||||
l.g.log.Debug("live reply edit failed", "error", plainErr)
|
||||
return errors.Join(
|
||||
fmt.Errorf("rich edit: %w", richErr),
|
||||
fmt.Errorf("plain edit: %w", plainErr),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
@@ -81,8 +83,9 @@ 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)
|
||||
g.SetShowToolCalls(showToolCalls)
|
||||
live := g.newLiveReply(context.Background(), b, 7, 0, g.ShowToolCalls())
|
||||
t.Cleanup(live.stopRendering)
|
||||
live.interval = 0
|
||||
return live, calls
|
||||
}
|
||||
@@ -94,8 +97,11 @@ func TestLiveReplySendsOnceThenEditsWithTheWholeBuffer(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
live.Delta("Now let me look")
|
||||
live.flushRendering()
|
||||
live.Delta(" at the shell tool")
|
||||
live.flushRendering()
|
||||
live.Delta(". Now I have the full picture")
|
||||
live.flushRendering()
|
||||
live.finalize(ctx, "Now let me look at the shell tool. Now I have the full picture")
|
||||
|
||||
if len(*calls) != 4 {
|
||||
@@ -133,8 +139,11 @@ func TestLiveReplyKeepsExactlyOneCursorAtTheEnd(t *testing.T) {
|
||||
live, calls := newTestLiveReply(t, true)
|
||||
|
||||
live.Delta("first")
|
||||
live.flushRendering()
|
||||
live.ToolCall(toolEvent("shell", "exit 0", ""))
|
||||
live.flushRendering()
|
||||
live.Delta("second")
|
||||
live.flushRendering()
|
||||
|
||||
for i, call := range *calls {
|
||||
if n := strings.Count(call.markdown, liveCursor); n != 1 {
|
||||
@@ -152,6 +161,7 @@ func TestLiveReplySkipsUnchangedRenders(t *testing.T) {
|
||||
live, calls := newTestLiveReply(t, false)
|
||||
|
||||
live.Delta("same")
|
||||
live.flushRendering()
|
||||
live.render(context.Background())
|
||||
live.render(context.Background())
|
||||
|
||||
@@ -160,6 +170,33 @@ func TestLiveReplySkipsUnchangedRenders(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveReplySnapshotsToolCallVisibility(t *testing.T) {
|
||||
live, calls := newTestLiveReply(t, true)
|
||||
live.g.SetShowToolCalls(false)
|
||||
|
||||
live.ToolCall(gateway.ToolCallEvent{Name: "shell", Parameters: `{"cmd":"true"}`, Output: "exit 0"})
|
||||
live.finalize(context.Background(), "done")
|
||||
|
||||
last := (*calls)[len(*calls)-1]
|
||||
if !strings.Contains(last.markdown, `🔧 shell {"cmd":"true"} — exit 0`) {
|
||||
t.Fatalf("final reply = %q, want the visibility captured when the reply started", last.markdown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveReplyToolCallSnapshotIsRaceFreeDuringReload(t *testing.T) {
|
||||
live, _ := newTestLiveReply(t, true)
|
||||
var wg sync.WaitGroup
|
||||
wg.Go(func() {
|
||||
for i := range 100 {
|
||||
live.g.SetShowToolCalls(i%2 == 0)
|
||||
}
|
||||
})
|
||||
for range 100 {
|
||||
live.ToolCall(toolEvent("shell", "ok", ""))
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestLiveReplyToolCalls(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -225,6 +262,7 @@ func TestLiveReplyAbandonDropsTheCursor(t *testing.T) {
|
||||
live, calls := newTestLiveReply(t, false)
|
||||
|
||||
live.Delta("half an ans")
|
||||
live.flushRendering()
|
||||
live.abandon(context.Background())
|
||||
|
||||
last := (*calls)[len(*calls)-1]
|
||||
@@ -270,7 +308,8 @@ func TestLiveReplyEditFallsBackToPlainText(t *testing.T) {
|
||||
t.Fatalf("new test bot: %v", err)
|
||||
}
|
||||
g := New("1:test", "", "", []int64{42}, slog.New(slog.DiscardHandler))
|
||||
live := g.newLiveReply(b, 7, 0)
|
||||
live := g.newLiveReply(context.Background(), b, 7, 0, g.ShowToolCalls())
|
||||
t.Cleanup(live.stopRendering)
|
||||
live.interval = 0
|
||||
|
||||
live.Delta("streamed")
|
||||
@@ -285,6 +324,145 @@ func TestLiveReplyEditFallsBackToPlainText(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveReplyCallbacksDoNotWaitForTelegram(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
call func(*liveReply)
|
||||
}{
|
||||
{name: "delta", call: func(live *liveReply) { live.Delta("streamed") }},
|
||||
{name: "tool call", call: func(live *liveReply) { live.ToolCall(toolEvent("shell", "ok", "")) }},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
release := make(chan struct{})
|
||||
started := make(chan struct{}, 1)
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
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)
|
||||
}
|
||||
g := New("1:test", "", "", []int64{42}, slog.New(slog.DiscardHandler))
|
||||
g.SetShowToolCalls(true)
|
||||
live := g.newLiveReply(context.Background(), b, 7, 0, g.ShowToolCalls())
|
||||
t.Cleanup(live.stopRendering)
|
||||
live.interval = 0
|
||||
|
||||
returned := make(chan struct{})
|
||||
go func() {
|
||||
tc.call(live)
|
||||
close(returned)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-returned:
|
||||
case <-started:
|
||||
select {
|
||||
case <-returned:
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
close(release)
|
||||
t.Fatal("stream callback waited for Telegram HTTP")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
close(release)
|
||||
t.Fatal("callback neither returned nor reached Telegram")
|
||||
}
|
||||
close(release)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveReplyStopCancelsInFlightRendering(t *testing.T) {
|
||||
started := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
api := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
|
||||
close(started)
|
||||
<-release
|
||||
}))
|
||||
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))
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
live := g.newLiveReply(ctx, b, 7, 0, false)
|
||||
t.Cleanup(live.stopRendering)
|
||||
live.interval = 0
|
||||
|
||||
live.Delta("partial")
|
||||
select {
|
||||
case <-started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Telegram render did not start")
|
||||
}
|
||||
cancel()
|
||||
|
||||
select {
|
||||
case <-live.renderDone:
|
||||
close(release)
|
||||
case <-time.After(time.Second):
|
||||
close(release)
|
||||
t.Fatal("turn cancellation did not cancel Telegram HTTP")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveReplyFinalEditFailureSendsAuthoritativeReplyNormally(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" {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
_, _ = w.Write([]byte(`{"ok":false,"error_code":502,"description":"edit unavailable"}`))
|
||||
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(context.Background(), b, 7, 0, g.ShowToolCalls())
|
||||
t.Cleanup(live.stopRendering)
|
||||
live.interval = 0
|
||||
|
||||
authoritative := strings.Repeat("a\n", messageMaxLen)
|
||||
live.Delta("partial")
|
||||
live.finalize(context.Background(), authoritative)
|
||||
|
||||
seenEdit := false
|
||||
var fallback []apiCall
|
||||
for _, call := range *calls {
|
||||
if call.method == "editMessageText" {
|
||||
seenEdit = true
|
||||
continue
|
||||
}
|
||||
if seenEdit && call.method == "sendRichMessage" {
|
||||
fallback = append(fallback, call)
|
||||
}
|
||||
}
|
||||
if len(fallback) < 2 {
|
||||
t.Fatalf("fallback calls = %+v, want normal split delivery", fallback)
|
||||
}
|
||||
var rendered strings.Builder
|
||||
for _, call := range fallback {
|
||||
rendered.WriteString(strings.TrimSuffix(call.markdown, "\n\n_(continued...)_"))
|
||||
}
|
||||
if strings.Count(rendered.String(), "a") != strings.Count(authoritative, "a") {
|
||||
t.Fatalf("fallback delivered %d of %d reply lines", strings.Count(rendered.String(), "a"), strings.Count(authoritative, "a"))
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -294,6 +472,7 @@ func TestLiveReplyFramesStayWithinOneMessage(t *testing.T) {
|
||||
|
||||
for range 4 {
|
||||
live.Delta(strings.Repeat("a", liveBodyMaxRunes))
|
||||
live.flushRendering()
|
||||
}
|
||||
live.abandon(context.Background())
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
@@ -55,11 +56,9 @@ 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
|
||||
// showToolCalls controls whether new live replies render completed tool
|
||||
// calls. Each reply snapshots it so reloads affect only later turns.
|
||||
showToolCalls atomic.Bool
|
||||
|
||||
// restartCh carries /restart requests from a bot handler to the
|
||||
// supervisor loop in Start. Buffered so a handler never blocks.
|
||||
@@ -139,6 +138,16 @@ func New(token, webhookURL, webhookSecret string, allowedUserIDs []int64, log *s
|
||||
|
||||
func (g *Gateway) Name() string { return "telegram" }
|
||||
|
||||
// SetShowToolCalls controls tool visibility for replies created after the call.
|
||||
func (g *Gateway) SetShowToolCalls(show bool) {
|
||||
g.showToolCalls.Store(show)
|
||||
}
|
||||
|
||||
// ShowToolCalls reports the visibility setting that a new reply must snapshot.
|
||||
func (g *Gateway) ShowToolCalls() bool {
|
||||
return g.showToolCalls.Load()
|
||||
}
|
||||
|
||||
// RequestRestart asks the gateway supervisor to reload this adapter. It is
|
||||
// safe for callers outside Telegram (such as the local Web UI) and never
|
||||
// blocks when a restart is already queued.
|
||||
@@ -542,7 +551,7 @@ 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)
|
||||
live := g.newLiveReply(b, chatID, threadID)
|
||||
live := g.newLiveReply(turnCtx, b, chatID, threadID, g.ShowToolCalls())
|
||||
|
||||
// If it starts with / but wasn't matched by a registered
|
||||
// handler, it's unknown -- let the router handle it (which
|
||||
|
||||
@@ -1,19 +1,31 @@
|
||||
package gateway
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"encoding/json"
|
||||
"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
|
||||
const (
|
||||
toolSummaryMaxRunes = 80
|
||||
toolParametersMaxRunes = 120
|
||||
redactedParameterValue = "[redacted]"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
// ID correlates the completed result with the model's invocation.
|
||||
ID string
|
||||
// Name is the tool the model invoked.
|
||||
Name string
|
||||
// Parameters is a bounded, redacted JSON summary of the invocation input.
|
||||
// It is safe for channel adapters to render directly.
|
||||
Parameters string
|
||||
// Output is what the tool returned, verbatim.
|
||||
Output string
|
||||
// Err is non-empty when the tool failed, in which case Output is
|
||||
@@ -34,6 +46,75 @@ func (e ToolCallEvent) Summary() string {
|
||||
return "done"
|
||||
}
|
||||
|
||||
// SummarizeToolParameters returns a bounded JSON shape of tool input. Every
|
||||
// string value is replaced rather than echoed because credentials can appear
|
||||
// inside ordinary parameters such as command, content, or body. Values under
|
||||
// secret-bearing keys use an explicit redaction marker; other strings retain
|
||||
// only their type. Invalid JSON is never echoed.
|
||||
func SummarizeToolParameters(input string) string {
|
||||
if strings.TrimSpace(input) == "" {
|
||||
return ""
|
||||
}
|
||||
decoder := json.NewDecoder(strings.NewReader(input))
|
||||
decoder.UseNumber()
|
||||
var value any
|
||||
if err := decoder.Decode(&value); err != nil {
|
||||
return "[unavailable]"
|
||||
}
|
||||
value = redactToolParameters(value, false)
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return "[unavailable]"
|
||||
}
|
||||
return truncateRunesWithinLimit(string(encoded), toolParametersMaxRunes)
|
||||
}
|
||||
|
||||
func redactToolParameters(value any, sensitive bool) any {
|
||||
if sensitive {
|
||||
return redactedParameterValue
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
for key, child := range typed {
|
||||
typed[key] = redactToolParameters(child, sensitiveParameterKey(key))
|
||||
}
|
||||
return typed
|
||||
case []any:
|
||||
for i, child := range typed {
|
||||
typed[i] = redactToolParameters(child, sensitive)
|
||||
}
|
||||
return typed
|
||||
case string:
|
||||
return "[string]"
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
func sensitiveParameterKey(key string) bool {
|
||||
normalized := strings.NewReplacer("-", "_", " ", "_").Replace(strings.ToLower(key))
|
||||
for _, marker := range []string{
|
||||
"token", "secret", "password", "passwd", "api_key", "apikey",
|
||||
"authorization", "credential", "cookie", "private_key",
|
||||
} {
|
||||
if strings.Contains(normalized, marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func truncateRunesWithinLimit(s string, limit int) string {
|
||||
runes := []rune(s)
|
||||
if len(runes) <= limit {
|
||||
return s
|
||||
}
|
||||
if limit <= 1 {
|
||||
return "…"
|
||||
}
|
||||
return string(runes[:limit-1]) + "…"
|
||||
}
|
||||
|
||||
// TurnStream receives a turn's output as it is produced.
|
||||
//
|
||||
// It replaces the plain delta callback so a channel can render tool activity
|
||||
|
||||
@@ -5,6 +5,39 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSummarizeToolParameters(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{name: "empty", input: "", want: ""},
|
||||
{name: "compact object", input: `{ "path": "/tmp/file", "line": 2 }`, want: `{"line":2,"path":"[string]"}`},
|
||||
{name: "nested secrets", input: `{"headers":{"Authorization":"Bearer secret"},"api-token":"secret","safe":"visible"}`, want: `{"api-token":"[redacted]","headers":{"Authorization":"[redacted]"},"safe":"[string]"}`},
|
||||
{name: "secret embedded in ordinary value", input: `{"command":"curl -H 'Authorization: Bearer secret'"}`, want: `{"command":"[string]"}`},
|
||||
{name: "secret in array", input: `[{"password":"secret"}]`, want: `[{"password":"[redacted]"}]`},
|
||||
{name: "non-string secret", input: `{"password":123456,"token":true}`, want: `{"password":"[redacted]","token":"[redacted]"}`},
|
||||
{name: "nested value under sensitive key", input: `{"credentials":{"user":"archie","password":"secret"}}`, want: `{"credentials":"[redacted]"}`},
|
||||
{name: "invalid JSON", input: `token=secret`, want: "[unavailable]"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := SummarizeToolParameters(tc.input); got != tc.want {
|
||||
t.Fatalf("SummarizeToolParameters() = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
got := SummarizeToolParameters(`{"value":[` + strings.Repeat("0,", toolParametersMaxRunes) + `0]}`)
|
||||
if len([]rune(got)) != toolParametersMaxRunes {
|
||||
t.Fatalf("bounded summary is %d runes, want %d", len([]rune(got)), toolParametersMaxRunes)
|
||||
}
|
||||
if !strings.HasSuffix(got, "…") {
|
||||
t.Fatalf("bounded summary = %q, want ellipsis", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolCallEventSummary(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -209,10 +209,12 @@ func (s *Server) handleChatMessage(w http.ResponseWriter, r *http.Request) {
|
||||
// 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"`
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
Tool string `json:"tool,omitempty"`
|
||||
ToolCallID string `json:"tool_call_id,omitempty"`
|
||||
Parameters string `json:"parameters,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
}
|
||||
|
||||
// chatStreamSink adapts the stream writer to gateway.TurnStream so text and
|
||||
@@ -224,7 +226,13 @@ func (f chatStreamSink) Delta(text string) {
|
||||
}
|
||||
|
||||
func (f chatStreamSink) ToolCall(event gateway.ToolCallEvent) {
|
||||
f(chatStreamEvent{Type: "tool", Tool: event.Name, Text: event.Summary()})
|
||||
f(chatStreamEvent{
|
||||
Type: "tool",
|
||||
Tool: event.Name,
|
||||
ToolCallID: event.ID,
|
||||
Parameters: event.Parameters,
|
||||
Text: event.Summary(),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) handleChatStream(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -511,10 +511,12 @@ func TestChatSessionsFiltersNonWeb(t *testing.T) {
|
||||
|
||||
// chatSSEEvent is one parsed `data: {...}` frame from the stream endpoint.
|
||||
type chatSSEEvent struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
Tool string `json:"tool"`
|
||||
SessionID string `json:"session_id"`
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
Tool string `json:"tool"`
|
||||
ToolCallID string `json:"tool_call_id"`
|
||||
Parameters string `json:"parameters"`
|
||||
SessionID string `json:"session_id"`
|
||||
}
|
||||
|
||||
func parseChatSSE(t *testing.T, body string) []chatSSEEvent {
|
||||
@@ -606,7 +608,7 @@ func TestChatStreamDeltas(t *testing.T) {
|
||||
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{ID: "call-1", Name: "shell", Parameters: `{"cmd":"true"}`, 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
|
||||
@@ -644,13 +646,13 @@ func TestChatStreamReportsToolCalls(t *testing.T) {
|
||||
case "delta":
|
||||
got = append(got, "text:"+ev.Text)
|
||||
case "tool":
|
||||
got = append(got, "tool:"+ev.Tool+":"+ev.Text)
|
||||
got = append(got, "tool:"+ev.ToolCallID+":"+ev.Tool+":"+ev.Parameters+":"+ev.Text)
|
||||
}
|
||||
}
|
||||
want := []string{
|
||||
"text:checking",
|
||||
"tool:shell:exit 0",
|
||||
"tool:read:failed: no such file",
|
||||
`tool:call-1:shell:{"cmd":"true"}:exit 0`,
|
||||
"tool::read::failed: no such file",
|
||||
"text: and answering",
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
|
||||
Vendored
+1
-1
@@ -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(){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
|
||||
`)))),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.parameters??``,a=n.text??``,o=e(`div.chat-tool`,e(`span.chat-tool-icon`,`🔧`),e(`span.chat-tool-name`,r),i&&e(`code.chat-tool-parameters`,i),e(`span.chat-tool-summary`,a));return a.startsWith(`failed:`)&&(o.className=`chat-tool failed`),He(t).append(o),o}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,
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===`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
|
||||
@@ -12,11 +12,13 @@ export function appendToolCall(host, event) {
|
||||
const name = event?.tool ?? "";
|
||||
if (!host || !name) return null;
|
||||
|
||||
const parameters = event.parameters ?? "";
|
||||
const summary = event.text ?? "";
|
||||
const line = el(
|
||||
"div.chat-tool",
|
||||
el("span.chat-tool-icon", "🔧"),
|
||||
el("span.chat-tool-name", name),
|
||||
parameters && el("code.chat-tool-parameters", parameters),
|
||||
el("span.chat-tool-summary", summary),
|
||||
);
|
||||
// A failure reads as a completed step unless it is marked as one.
|
||||
|
||||
@@ -15,11 +15,12 @@ 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" });
|
||||
appendToolCall(host, { tool: "shell", parameters: '{"cmd":"true"}', 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-parameters").textContent, '{"cmd":"true"}');
|
||||
assert.equal(host.querySelector(".chat-tool-summary").textContent, "exit 0");
|
||||
assert.ok(
|
||||
childIndex(host, ".chat-tools") < childIndex(host, ".chat-bubble-text"),
|
||||
|
||||
Reference in new issue
Block a user