Chat tools: argument validation, replace, file snapshots, server-fetched images, show_run, show_diff head/paths, schedule_followup
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,8 +5,11 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -19,6 +22,13 @@ import (
|
||||
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
|
||||
@@ -37,11 +47,25 @@ type chatToolService struct {
|
||||
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 {
|
||||
return &chatToolService{tokens: tokens, shown: shown, hub: hub, registry: registry, home: OperatorHomeDirectory,
|
||||
notify: hub.publishToolNotification, limiter: newChatNotifyLimiter(5, time.Minute), now: time.Now}
|
||||
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
|
||||
@@ -67,27 +91,93 @@ func (s *chatToolService) Call(ctx context.Context, token, tool string, argument
|
||||
if refusal != "" {
|
||||
return chattools.Result{Text: refusal, IsError: true}
|
||||
}
|
||||
item, result := s.run(ctx, sessionID, tool, arguments)
|
||||
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 = newChatShownID()
|
||||
item.ID = id
|
||||
}
|
||||
now := s.now().UnixMilli()
|
||||
item.CreatedAt = now
|
||||
if previous != nil {
|
||||
item.CreatedAt, item.UpdatedAt = previous.CreatedAt, now
|
||||
}
|
||||
item.CreatedAt = s.now().UnixMilli()
|
||||
if s.shown != nil {
|
||||
if err := s.shown.AppendChatShownItem(ctx, sessionID, *item); err != 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.
|
||||
@@ -188,5 +278,8 @@ func (h *chatHub) shownFrame(sessionID string) *protocol.ChatShownFrame {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
if h.shownRefresh != nil {
|
||||
items = h.shownRefresh(ctx, sessionID, items)
|
||||
}
|
||||
return &protocol.ChatShownFrame{SessionID: sessionID, Items: items}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user