feat(chat): the dashboard chat answers tool approvals and questions, and /update offers Install
This commit is contained in:
1 parent
12ac6daff7
commit
12eab7683a
11 files changed
+454
-103
No files matched your search
@@ -318,6 +318,8 @@ type chatStreamEvent struct {
|
||||
// only on a navigate event.
|
||||
Path string `json:"path,omitempty"`
|
||||
Label string `json:"label,omitempty"`
|
||||
// Ask is a question the turn waits on; answered by POST /api/chat/answer.
|
||||
Ask *chatAsk `json:"ask,omitempty"`
|
||||
}
|
||||
|
||||
// chatStreamSink writes turn text and tool activity to the browser stream.
|
||||
@@ -408,13 +410,19 @@ 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")
|
||||
// An ask is written from the gateway client's goroutine while this one
|
||||
// writes the turn's events.
|
||||
var writeMu sync.Mutex
|
||||
writeChatEvent := func(event chatStreamEvent, sessionID string) {
|
||||
writeMu.Lock()
|
||||
defer writeMu.Unlock()
|
||||
event.SessionID = sessionID
|
||||
payload, _ := json.Marshal(event)
|
||||
_, _ = fmt.Fprintf(w, "data: %s\n\n", payload)
|
||||
flusher.Flush()
|
||||
}
|
||||
events, err := chat.Contract.Stream(r.Context(), msg)
|
||||
asker := dashboardAsker{asks: &s.chatAsks, write: func(frame chatStreamEvent) { writeChatEvent(frame, "") }}
|
||||
events, err := chat.Contract.Stream(withDashboardAsks(r.Context(), asker), msg)
|
||||
if err != nil {
|
||||
writeChatEvent(chatStreamEvent{Type: "error", Text: err.Error()}, "")
|
||||
return
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
// chatAsk is a question a turn is blocked on, rendered as a card in the chat.
|
||||
type chatAsk struct {
|
||||
ID string `json:"id"`
|
||||
Kind string `json:"kind"` // approval, clarify or picker
|
||||
Prompt string `json:"prompt"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
Choices []chatAskChoice `json:"choices,omitempty"`
|
||||
}
|
||||
|
||||
type chatAskChoice struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
}
|
||||
|
||||
// chatAsks holds the asks waiting for the browser's answer, keyed by id.
|
||||
type chatAsks struct {
|
||||
mu sync.Mutex
|
||||
waiting map[string]chan string
|
||||
}
|
||||
|
||||
func (a *chatAsks) open() (string, chan string) {
|
||||
var b [12]byte
|
||||
_, _ = rand.Read(b[:])
|
||||
id := hex.EncodeToString(b[:])
|
||||
ch := make(chan string, 1)
|
||||
a.mu.Lock()
|
||||
if a.waiting == nil {
|
||||
a.waiting = make(map[string]chan string)
|
||||
}
|
||||
a.waiting[id] = ch
|
||||
a.mu.Unlock()
|
||||
return id, ch
|
||||
}
|
||||
|
||||
func (a *chatAsks) close(id string) {
|
||||
a.mu.Lock()
|
||||
delete(a.waiting, id)
|
||||
a.mu.Unlock()
|
||||
}
|
||||
|
||||
func (a *chatAsks) answer(id, answer string) bool {
|
||||
a.mu.Lock()
|
||||
ch, ok := a.waiting[id]
|
||||
a.mu.Unlock()
|
||||
if ok {
|
||||
select {
|
||||
case ch <- answer:
|
||||
default:
|
||||
}
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// dashboardAsker answers a turn's approvals and questions through cards in
|
||||
// the chat stream, so the dashboard carries everything Telegram does.
|
||||
type dashboardAsker struct {
|
||||
asks *chatAsks
|
||||
write func(chatStreamEvent)
|
||||
}
|
||||
|
||||
func (d dashboardAsker) ask(ctx context.Context, q chatAsk) (string, error) {
|
||||
id, ch := d.asks.open()
|
||||
defer d.asks.close(id)
|
||||
q.ID = id
|
||||
d.write(chatStreamEvent{Type: "ask", Ask: &q})
|
||||
select {
|
||||
case answer := <-ch:
|
||||
return answer, nil
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (d dashboardAsker) RequestApproval(ctx context.Context, action, description string) (messaging.ApprovalDecision, error) {
|
||||
answer, err := d.ask(ctx, chatAsk{Kind: "approval", Prompt: action, Detail: description})
|
||||
switch {
|
||||
case err != nil:
|
||||
return messaging.ApprovalDenied, err
|
||||
case answer == "approve":
|
||||
return messaging.ApprovalApproved, nil
|
||||
case answer == "always":
|
||||
return messaging.ApprovalPermanentlyApproved, nil
|
||||
}
|
||||
return messaging.ApprovalDenied, nil
|
||||
}
|
||||
|
||||
func (d dashboardAsker) RequestClarification(ctx context.Context, req messaging.ClarifyRequest) (string, error) {
|
||||
answer, err := d.ask(ctx, chatAsk{Kind: "clarify", Prompt: req.Question, Choices: askChoices(req.Suggestions)})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if answer = strings.TrimSpace(answer); answer == "" {
|
||||
return "", messaging.ErrEmptyReply
|
||||
}
|
||||
return answer, nil
|
||||
}
|
||||
|
||||
func (d dashboardAsker) RequestChoice(ctx context.Context, req messaging.PickerRequest) (messaging.InteractiveChoice, error) {
|
||||
if len(req.Options) == 0 {
|
||||
return messaging.InteractiveChoice{}, messaging.ErrNoPickerOptions
|
||||
}
|
||||
answer, err := d.ask(ctx, chatAsk{Kind: "picker", Prompt: req.Prompt, Choices: askChoices(req.Options)})
|
||||
if err != nil {
|
||||
return messaging.InteractiveChoice{}, err
|
||||
}
|
||||
for _, option := range req.Options {
|
||||
if option.ID == answer {
|
||||
return option, nil
|
||||
}
|
||||
}
|
||||
return messaging.InteractiveChoice{}, errors.New("picker answer matches no option")
|
||||
}
|
||||
|
||||
func askChoices(choices []messaging.InteractiveChoice) []chatAskChoice {
|
||||
out := make([]chatAskChoice, 0, len(choices))
|
||||
for _, c := range choices {
|
||||
out = append(out, chatAskChoice{ID: c.ID, Label: c.Label})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// withDashboardAsks puts the dashboard's askers on a turn's context.
|
||||
func withDashboardAsks(ctx context.Context, asker dashboardAsker) context.Context {
|
||||
ctx = messaging.WithApprovalRequester(ctx, asker)
|
||||
return messaging.WithInteractive(ctx, messaging.Interactive{Clarifier: asker, Picker: asker})
|
||||
}
|
||||
|
||||
type chatAnswerRequest struct {
|
||||
ID string `json:"id"`
|
||||
Answer string `json:"answer"`
|
||||
}
|
||||
|
||||
// handleChatAnswer delivers the operator's answer to the ask a turn waits on.
|
||||
func (s *Server) handleChatAnswer(w http.ResponseWriter, r *http.Request) {
|
||||
var req chatAnswerRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.ID == "" {
|
||||
http.Error(w, "id is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !s.chatAsks.answer(req.ID, req.Answer) {
|
||||
http.Error(w, "that question is no longer waiting", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]bool{"answered": true})
|
||||
}
|
||||
@@ -25,6 +25,9 @@ import (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
// chatAsks holds the chat questions waiting for the operator's answer.
|
||||
chatAsks chatAsks
|
||||
|
||||
Store storecontract.TaskStore
|
||||
Log *slog.Logger
|
||||
|
||||
@@ -358,6 +361,7 @@ func (s *Server) registerChatRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("POST /api/chat/message", s.handleChatMessage)
|
||||
mux.HandleFunc("POST /api/chat/stream", s.handleChatStream)
|
||||
mux.HandleFunc("POST /api/chat/cancel", s.handleChatCancel)
|
||||
mux.HandleFunc("POST /api/chat/answer", s.handleChatAnswer)
|
||||
mux.HandleFunc("POST /api/chat/persona", s.handleChatPersona)
|
||||
mux.HandleFunc("GET /api/chat/update", s.handleChatUpdate)
|
||||
mux.HandleFunc("POST /api/chat/update/defer", s.handleChatUpdateDefer)
|
||||
|
||||
Reference in new issue
Block a user