Files
pipelines/internal/server/chat_tools_service.go
T

286 lines
9.7 KiB
Go

package server
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"slices"
"strings"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/chattools"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// chatShownStore persists the items a chat's model showed (pipeline.Store).
type chatShownStore interface {
AppendChatShownItem(ctx context.Context, sessionID string, item protocol.ChatShownItem) error
ChatShownItems(ctx context.Context, sessionID string) ([]protocol.ChatShownItem, error)
ChatShownItem(ctx context.Context, sessionID, id string) (protocol.ChatShownItem, bool, error)
ReplaceChatShownItem(ctx context.Context, sessionID string, item protocol.ChatShownItem) (bool, error)
}
// chatShownRunStore reads the runs show_run follows (pipeline.Store).
type chatShownRunStore interface {
RunSummaryByID(ctx context.Context, id int64) (protocol.RunSummary, bool, error)
}
// chatToolCorrelationWait is how long a call on a shared (instance) endpoint
// waits for the harness stream to report the matching running tool call.
var chatToolCorrelationWait = 3 * time.Second
// chatToolService runs the Pipelines tools (chattools.Service) for the chat an
// endpoint token serves, records each shown item and pushes it to the chat's
// subscribers.
type chatToolService struct {
tokens chatToolTokenStore
shown chatShownStore
hub *chatHub
registry operatorRegistry
home func() (string, error)
notify chatToolNotifier
limiter *chatNotifyLimiter
now func() time.Time
// runs reads the runs show_run follows; broker delivers follow-ups.
runs chatShownRunStore
broker chatBroker
// snapshotRoot is where shown files are copied; it must lie inside the
// operator's home, where /files serves it.
snapshotRoot func() (string, error)
// fetch is how web images are downloaded (tests replace its guard).
fetch shownImageFetcher
watch runWatches
}
func newChatToolService(tokens chatToolTokenStore, shown chatShownStore, hub *chatHub, registry operatorRegistry) *chatToolService {
s := &chatToolService{tokens: tokens, shown: shown, hub: hub, registry: registry, home: OperatorHomeDirectory,
notify: hub.publishToolNotification, limiter: newChatNotifyLimiter(5, time.Minute), now: time.Now,
snapshotRoot: defaultSnapshotRoot, fetch: shownImageFetcher{allowed: publicAddress}}
if hub != nil {
hub.shownRefresh = s.refreshShown
}
return s
}
// registerChatTools serves the MCP endpoint on mux. The route exists only on
// the server's own listener; neither the web proxy nor the peer relay forwards
// it.
func registerChatTools(mux *http.ServeMux, service *chatToolService, version string) {
handler := chattools.Handler{Service: service, Version: version}
mux.HandleFunc(chatToolPath+"{token}", func(w http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(w, r, r.PathValue("token"))
})
}
func (s *chatToolService) Known(ctx context.Context, token string) bool {
if s == nil || s.tokens == nil || len(token) != 64 {
return false
}
_, found, err := s.tokens.ChatToolScope(ctx, token)
return err == nil && found
}
func (s *chatToolService) Call(ctx context.Context, token, tool string, arguments json.RawMessage) chattools.Result {
sessionID, refusal := s.session(ctx, token, tool, arguments)
if refusal != "" {
return chattools.Result{Text: refusal, IsError: true}
}
if err := chattools.ValidateArguments(tool, arguments); err != nil {
return chattools.Result{Text: "Nothing was done: " + err.Error() + ".", IsError: true}
}
var common struct {
Replace string `json:"replace"`
}
_ = json.Unmarshal(arguments, &common)
id := newChatShownID()
var previous *protocol.ChatShownItem
if replace := strings.TrimSpace(common.Replace); replace != "" {
target, err := s.replaceTarget(ctx, sessionID, tool, replace)
if err != nil {
return chattools.Result{Text: "Nothing was shown: " + err.Error(), IsError: true}
}
previous, id = &target, target.ID
}
item, result := s.run(ctx, sessionID, tool, id, arguments)
if item == nil {
return result
}
if item.ID == "" {
item.ID = id
}
now := s.now().UnixMilli()
item.CreatedAt = now
if previous != nil {
item.CreatedAt, item.UpdatedAt = previous.CreatedAt, now
}
if s.shown != nil {
var err error
if previous != nil {
var found bool
if found, err = s.shown.ReplaceChatShownItem(ctx, sessionID, *item); err == nil && !found {
err = errors.New("the item to replace is no longer retained")
}
} else {
err = s.shown.AppendChatShownItem(ctx, sessionID, *item)
}
if err != nil {
log.Printf("chat tools session=%s: record %s: %v", sessionID, tool, err)
return chattools.Result{Text: "Pipelines could not record this item: " + err.Error(), IsError: true}
}
s.sweepSnapshots(ctx, sessionID)
}
if item.Run != nil {
s.watchRun(item.Run.ID, sessionID, item.ID)
}
if s.hub != nil {
s.hub.fanout(sessionID, protocol.Message{Type: protocol.ChatShown,
ChatShown: &protocol.ChatShownFrame{SessionID: sessionID, Items: []protocol.ChatShownItem{*item}}}, "")
}
if tool != chattools.ToolNotify {
if previous != nil {
result.Text += " Updated item " + item.ID + " in place."
} else {
result.Text += " Item id: " + item.ID + " (pass it as replace to update this card)."
}
}
return result
}
// replaceTarget is the item a replace id names: one this chat's model showed,
// of a kind the tool can update.
func (s *chatToolService) replaceTarget(ctx context.Context, sessionID, tool, id string) (protocol.ChatShownItem, error) {
if s.shown == nil {
return protocol.ChatShownItem{}, errors.New("items cannot be replaced on this server")
}
item, found, err := s.shown.ChatShownItem(ctx, sessionID, id)
if err != nil {
return protocol.ChatShownItem{}, err
}
if !found {
return protocol.ChatShownItem{}, fmt.Errorf("replace %q is not an item shown in this chat (use the id from an earlier show_* result here)", id)
}
kinds := map[string][]string{
chattools.ToolShowFile: {protocol.ChatShownFile, protocol.ChatShownImage},
chattools.ToolShowImage: {protocol.ChatShownFile, protocol.ChatShownImage},
chattools.ToolShowHTML: {protocol.ChatShownHTML},
chattools.ToolShowDiff: {protocol.ChatShownDiff},
chattools.ToolShowRun: {protocol.ChatShownRun},
}[tool]
if !slices.Contains(kinds, item.Kind) {
return protocol.ChatShownItem{}, fmt.Errorf("replace %q is a %s item; %s can only replace %s", id, item.Kind, tool, strings.Join(kinds, " or "))
}
return item, nil
}
// session resolves the chat a call belongs to. A session token names it; a
// shared harness instance token is attributed to the one chat whose stream
// shows this exact call running, and refused when none or several do.
func (s *chatToolService) session(ctx context.Context, token, tool string, arguments json.RawMessage) (string, string) {
scope, found, err := s.tokens.ChatToolScope(ctx, token)
if err != nil || !found {
return "", "This Pipelines endpoint is not recognised."
}
if id, ok := strings.CutPrefix(scope, chatToolSessionScope); ok && id != "" {
return id, ""
}
if _, ok := strings.CutPrefix(scope, chatToolInstanceScope); ok && s.hub != nil {
id, ambiguous := s.hub.awaitToolCallSession(ctx, tool, arguments, chatToolCorrelationWait)
if ambiguous {
return "", "Pipelines could not tell which chat made this call (another chat made an identical one); retry with a different title."
}
if id == "" {
return "", "Pipelines could not match this call to a chat it is showing; the tools work only in chats opened through Pipelines."
}
return id, ""
}
return "", "This chat is not ready for Pipelines tools yet."
}
// chatSessionDirectory is the chat's recorded working directory, or "".
func (s *chatToolService) chatSessionDirectory(ctx context.Context, sessionID string) string {
if s.registry == nil {
return ""
}
session, found, err := s.registry.OperatorSession(ctx, sessionID)
if err != nil || !found {
return ""
}
return session.Directory
}
func (s *chatToolService) chatTitle(ctx context.Context, sessionID string) string {
if s.registry == nil {
return ""
}
session, found, err := s.registry.OperatorSession(ctx, sessionID)
if err != nil || !found {
return ""
}
return session.Title
}
func newChatShownID() string {
var b [12]byte
_, _ = rand.Read(b[:])
return "shown_" + hex.EncodeToString(b[:])
}
// chatNotifyLimiter is a per-chat sliding-window rate limit.
type chatNotifyLimiter struct {
mu sync.Mutex
limit int
window time.Duration
sent map[string][]time.Time
}
func newChatNotifyLimiter(limit int, window time.Duration) *chatNotifyLimiter {
return &chatNotifyLimiter{limit: limit, window: window, sent: map[string][]time.Time{}}
}
// allow records one notification for sessionID at now unless the window is
// full.
func (l *chatNotifyLimiter) allow(sessionID string, now time.Time) bool {
l.mu.Lock()
defer l.mu.Unlock()
kept := l.sent[sessionID][:0]
for _, at := range l.sent[sessionID] {
if now.Sub(at) < l.window {
kept = append(kept, at)
}
}
if len(kept) >= l.limit {
l.sent[sessionID] = kept
return false
}
l.sent[sessionID] = append(kept, now)
return true
}
// shownFrame is a chat's retained shown items for its subscribe reply, or nil
// when it has none (or none are recorded on this server).
func (h *chatHub) shownFrame(sessionID string) *protocol.ChatShownFrame {
if h == nil || h.shown == nil {
return nil
}
ctx, cancel := context.WithTimeout(h.baseCtx, 5*time.Second)
defer cancel()
items, err := h.shown.ChatShownItems(ctx, sessionID)
if err != nil {
log.Printf("chat shown items session=%s: %v", sessionID, err)
return nil
}
if len(items) == 0 {
return nil
}
if h.shownRefresh != nil {
items = h.shownRefresh(ctx, sessionID, items)
}
return &protocol.ChatShownFrame{SessionID: sessionID, Items: items}
}