package server import ( "context" "crypto/rand" "encoding/hex" "encoding/json" "log" "net/http" "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) } // 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 } func newChatToolService(tokens chatToolTokenStore, shown chatShownStore, hub *chatHub, registry operatorRegistry) *chatToolService { return &chatToolService{tokens: tokens, shown: shown, hub: hub, registry: registry, home: OperatorHomeDirectory, notify: hub.publishToolNotification, limiter: newChatNotifyLimiter(5, time.Minute), now: time.Now} } // 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} } item, result := s.run(ctx, sessionID, tool, arguments) if item == nil { return result } item.ID = newChatShownID() item.CreatedAt = s.now().UnixMilli() if s.shown != nil { if err := s.shown.AppendChatShownItem(ctx, sessionID, *item); 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} } } if s.hub != nil { s.hub.fanout(sessionID, protocol.Message{Type: protocol.ChatShown, ChatShown: &protocol.ChatShownFrame{SessionID: sessionID, Items: []protocol.ChatShownItem{*item}}}, "") } return result } // 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 } return &protocol.ChatShownFrame{SessionID: sessionID, Items: items} }