From f6d7d0dce2277e3cb5d4411548a324851b2101d2 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 23 Aug 2026 02:48:06 +1000 Subject: [PATCH] feat(webui): render a media event as a link fallback The dashboard has no inline media rendering path yet (archie-core-1786748942243-6-f109697e). chatStreamSink.Media degrades a MediaEvent to a link appended to the delta stream -- the same fallback Telegram's liveReply uses when its own SendMedia call fails -- rather than the event reaching nowhere. --- internal/webui/api_chat.go | 15 ++++++++ internal/webui/api_chat_test.go | 66 +++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/internal/webui/api_chat.go b/internal/webui/api_chat.go index 187a2b48f..ce03ca74f 100644 --- a/internal/webui/api_chat.go +++ b/internal/webui/api_chat.go @@ -254,6 +254,21 @@ func (s chatStreamSink) ToolCall(event gateway.ToolCallEvent) { }) } +// Media has no inline rendering path on the dashboard yet (see +// archie-core-1786748942243-6-f109697e), so it degrades to a link in the +// delta stream -- the same fallback Telegram's liveReply uses when its own +// SendMedia call fails. An attachment with no URL has nothing to link to +// and is skipped rather than emitting an empty line. +func (s chatStreamSink) Media(event gateway.MediaEvent) { + if event.Attachment.URL == "" { + return + } + s.write(chatStreamEvent{ + Type: "delta", + Text: "\n\nšŸ“Ž " + event.Attachment.Type + ": " + event.Attachment.URL, + }) +} + // chatShowToolCalls reports config.ChatConfig.ShowToolCalls, the one setting // shared by every chat channel. Off (the default) when Cfg is nil, matching // the field's own off-by-default doc comment: a dashboard embedded without diff --git a/internal/webui/api_chat_test.go b/internal/webui/api_chat_test.go index c0d19ed3f..a3b61590c 100644 --- a/internal/webui/api_chat_test.go +++ b/internal/webui/api_chat_test.go @@ -679,6 +679,72 @@ func TestChatStreamReportsToolCalls(t *testing.T) { } } +// The dashboard has no inline media rendering path yet (see +// archie-core-1786748942243-6-f109697e), so a MediaEvent has to degrade to +// a link in the text stream rather than being silently dropped -- the same +// fallback Telegram's liveReply uses when SendMedia itself fails. +func TestChatStreamRendersMediaAsALinkFallback(t *testing.T) { + stream := func(_ context.Context, _ gateway.Message, turn gateway.TurnStream) (string, error) { + turn.Delta("here you go") + turn.Media(gateway.MediaEvent{ + ToolName: "video_gen", + Attachment: gateway.MediaAttachment{Type: "video", URL: "https://example.com/v.mp4"}, + }) + return "here you go", nil + } + + server, sessions := chatTestServer(t) + server.Chat.Router.LLMStream = stream + saveWebSession(t, sessions, "web-1") + + req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/api/chat/stream", + strings.NewReader(`{"channel_id":"browser-web-1","text":"make me a video"}`)) + 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 joined string + for _, ev := range parseChatSSE(t, res.Body.String()) { + if ev.Type == "delta" { + joined += ev.Text + } + } + if !strings.Contains(joined, "https://example.com/v.mp4") { + t.Fatalf("delta stream = %q, want it to contain the asset URL", joined) + } +} + +// An event with no URL has nothing to link to, so it must not add an +// empty or broken-looking line to the transcript. +func TestChatStreamSkipsMediaWithNoURL(t *testing.T) { + stream := func(_ context.Context, _ gateway.Message, turn gateway.TurnStream) (string, error) { + turn.Media(gateway.MediaEvent{ToolName: "video_gen", Attachment: gateway.MediaAttachment{Type: "video"}}) + return "", nil + } + + server, sessions := chatTestServer(t) + server.Chat.Router.LLMStream = stream + saveWebSession(t, sessions, "web-1") + + req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/api/chat/stream", + strings.NewReader(`{"channel_id":"browser-web-1","text":"make me a video"}`)) + req.Header.Set("Content-Type", "application/json") + res := httptest.NewRecorder() + server.Handler().ServeHTTP(res, req) + if res.Code != http.StatusOK { + t.Fatalf("status = %d, body = %s", res.Code, res.Body) + } + + for _, ev := range parseChatSSE(t, res.Body.String()) { + if ev.Type == "delta" && ev.Text != "" { + t.Fatalf("got a delta for a URL-less media event: %q", ev.Text) + } + } +} + // config.ChatConfig.ShowToolCalls is off by default and shared by every chat // channel: an operator who left it unset (or explicitly turned it off) must // see no tool activity on the dashboard, not just in Telegram. Previously the