Files
archie/modules/dashboard/stream.go
T
samandCopilot 731417ab48 feat(ui/chat): run the Vue chat on the archie binary (#2023)
Sessions, turns, streamed replies, thinking and tool cards, held-action
approvals and resume now use modules/chat; /api/stream forwards the chat
topic. Sub-features the binary can't back yet stay hidden.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-20 01:45:35 +11:00

87 lines
2.8 KiB
Go

package dashboard
import (
"context"
"fmt"
"net/http"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/internal/platform/hub"
"github.com/samcharles93/archie-core/modules/governance"
)
// keepalive is how often an idle stream writes a comment, so proxies keep
// the connection open and the browser notices one that died.
const keepalive = 25 * time.Second
// chatTopic is the hub topic the chat module publishes when an action a
// conversation held settles after its turn ended.
const chatTopic = "chat"
// chatOutcome is the frame the dashboard re-reads the open conversation on.
const chatOutcome = `data: {"topic":"tasks","data":{"kind":"chat_outcome"}}` + "\n\n"
// Register adds the dashboard's live stream: the one EventSource the SPA
// keeps open, framing each update as `data: {"topic":…,"data":…}`. A settled
// chat action is the only update with a dashboard reader; a turn's own reply
// streams from the chat API. Keepalives tell the connection indicator the
// server is up, and a reconnect makes every mounted page re-read.
func Register(api huma.API, h *hub.Hub) {
huma.Register(api, httpapi.SignedIn(huma.Operation{
OperationID: "stream-dashboard", Method: http.MethodGet, Path: "/api/stream",
Summary: "Stream live dashboard updates", Tags: []string{"Dashboard"},
Responses: map[string]*huma.Response{"200": {
Description: "Server-sent events: one data frame of {\"topic\", \"data\"} per update, and a keepalive comment while idle",
Content: map[string]*huma.MediaType{"text/event-stream": {Schema: &huma.Schema{Type: huma.TypeString}}},
}},
}), func(ctx context.Context, _ *struct{}) (*huma.StreamResponse, error) {
// Chat is an administrator's; nobody else hears that it changed.
var chat <-chan hub.Note
stop := func() {}
if p, _ := governance.PrincipalFromContext(ctx); p.InstanceAdmin() {
chat, stop = h.Subscribe(chatTopic)
}
return &huma.StreamResponse{Body: func(hc huma.Context) {
defer stop()
serve(hc, chat)
}}, nil
})
}
func serve(hc huma.Context, chat <-chan hub.Note) {
w := hc.BodyWriter()
flusher, ok := w.(http.Flusher)
if !ok {
hc.SetStatus(http.StatusInternalServerError)
return
}
hc.SetHeader("Content-Type", "text/event-stream")
hc.SetHeader("Cache-Control", "no-store")
hc.SetStatus(http.StatusOK)
flusher.Flush()
tick := time.NewTicker(keepalive)
defer tick.Stop()
for {
frame := ": keepalive\n\n"
select {
case <-hc.Context().Done():
return
case <-tick.C:
case _, ok := <-chat:
if !ok {
// The hub dropped a reader that fell behind; it reconnects
// and re-reads.
return
}
frame = chatOutcome
}
if _, err := fmt.Fprint(w, frame); err != nil {
return
}
flusher.Flush()
}
}