Merge branch 'worktree-agent-abc9e7b0a4e0a0f54'
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,46 +11,48 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// run executes one tool for sessionID. A non-nil item is recorded and shown;
|
||||
// the result is the short text the model reads.
|
||||
func (s *chatToolService) run(ctx context.Context, sessionID, tool string, arguments json.RawMessage) (*protocol.ChatShownItem, chattools.Result) {
|
||||
// run executes one tool for sessionID, whose arguments already passed
|
||||
// chattools.ValidateArguments. id is the shown item's id (a replaced item's
|
||||
// own); a non-nil item is recorded and shown, and the result is the short
|
||||
// text the model reads.
|
||||
func (s *chatToolService) run(ctx context.Context, sessionID, tool, id string, arguments json.RawMessage) (*protocol.ChatShownItem, chattools.Result) {
|
||||
fail := func(format string, args ...any) (*protocol.ChatShownItem, chattools.Result) {
|
||||
return nil, chattools.Result{Text: fmt.Sprintf(format, args...), IsError: true}
|
||||
}
|
||||
var in struct {
|
||||
Path string `json:"path"`
|
||||
URLOrPath string `json:"url_or_path"`
|
||||
Title string `json:"title"`
|
||||
Alt string `json:"alt"`
|
||||
HTML string `json:"html"`
|
||||
HeightHint int `json:"height_hint"`
|
||||
Message string `json:"message"`
|
||||
Priority string `json:"priority"`
|
||||
Base string `json:"base"`
|
||||
Committed bool `json:"committed_only"`
|
||||
}
|
||||
if err := json.Unmarshal(arguments, &in); err != nil {
|
||||
return fail("The arguments must be a JSON object matching the %s schema: %v", tool, err)
|
||||
}
|
||||
title := boundedLine(in.Title, 200)
|
||||
decode := func(into any) error { return json.Unmarshal(arguments, into) }
|
||||
switch tool {
|
||||
case chattools.ToolShowFile:
|
||||
var in struct {
|
||||
Path string `json:"path"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
if err := decode(&in); err != nil {
|
||||
return fail("Nothing was shown: %v", err)
|
||||
}
|
||||
file, err := s.resolveShownFile(ctx, sessionID, in.Path)
|
||||
if err != nil {
|
||||
return fail("Nothing was shown: %v", err)
|
||||
}
|
||||
item := file.item(protocol.ChatShownFile, title)
|
||||
return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat: %s (%s).", file.name, humanBytes(file.size))}
|
||||
item := s.fileItem(sessionID, id, file, protocol.ChatShownFile, boundedLine(in.Title, 200))
|
||||
return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat: %s (%s%s).", file.name, humanBytes(file.size), shownKeptNote(item))}
|
||||
case chattools.ToolShowImage:
|
||||
var in struct {
|
||||
URLOrPath string `json:"url_or_path"`
|
||||
Title string `json:"title"`
|
||||
Alt string `json:"alt"`
|
||||
}
|
||||
if err := decode(&in); err != nil {
|
||||
return fail("Nothing was shown: %v", err)
|
||||
}
|
||||
title, alt := boundedLine(in.Title, 200), boundedLine(in.Alt, 500)
|
||||
source := strings.TrimSpace(in.URLOrPath)
|
||||
alt := boundedLine(in.Alt, 500)
|
||||
if strings.Contains(source, "://") {
|
||||
address, err := shownImageURL(source)
|
||||
item, err := s.fetchShownImage(ctx, sessionID, id, source)
|
||||
if err != nil {
|
||||
return fail("Nothing was shown: %v", err)
|
||||
}
|
||||
item := protocol.ChatShownItem{Kind: protocol.ChatShownImage, Title: title, URL: address, Alt: alt}
|
||||
return &item, chattools.Result{Text: "Shown in the operator's Pipelines chat: the image at " + address + "."}
|
||||
item.Title, item.Alt = title, alt
|
||||
return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat: the image at %s (%s, %s, downloaded by Pipelines).", item.URL, item.Mime, humanBytes(item.Size))}
|
||||
}
|
||||
file, err := s.resolveShownFile(ctx, sessionID, source)
|
||||
if err != nil {
|
||||
@@ -59,10 +61,18 @@ func (s *chatToolService) run(ctx context.Context, sessionID, tool string, argum
|
||||
if !strings.HasPrefix(file.mime, "image/") {
|
||||
return fail("Nothing was shown: %s is %s, not an image; use show_file for documents.", file.name, file.mime)
|
||||
}
|
||||
item := file.item(protocol.ChatShownImage, title)
|
||||
item := s.fileItem(sessionID, id, file, protocol.ChatShownImage, title)
|
||||
item.Alt = alt
|
||||
return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat: the image %s (%s).", file.name, humanBytes(file.size))}
|
||||
return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat: the image %s (%s%s).", file.name, humanBytes(file.size), shownKeptNote(item))}
|
||||
case chattools.ToolShowHTML:
|
||||
var in struct {
|
||||
HTML string `json:"html"`
|
||||
Title string `json:"title"`
|
||||
HeightHint int `json:"height_hint"`
|
||||
}
|
||||
if err := decode(&in); err != nil {
|
||||
return fail("Nothing was shown: %v", err)
|
||||
}
|
||||
if strings.TrimSpace(in.HTML) == "" {
|
||||
return fail("Nothing was shown: html is empty.")
|
||||
}
|
||||
@@ -72,16 +82,62 @@ func (s *chatToolService) run(ctx context.Context, sessionID, tool string, argum
|
||||
if !utf8.ValidString(in.HTML) {
|
||||
return fail("Nothing was shown: html is not valid UTF-8.")
|
||||
}
|
||||
item := protocol.ChatShownItem{Kind: protocol.ChatShownHTML, Title: title, HTML: in.HTML, HTMLBytes: len(in.HTML), HeightHint: clampHeightHint(in.HeightHint)}
|
||||
return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat (%s, sandboxed: scripts and network are disabled).", humanBytes(int64(len(in.HTML))))}
|
||||
item := protocol.ChatShownItem{Kind: protocol.ChatShownHTML, Title: boundedLine(in.Title, 200), HTML: in.HTML, HTMLBytes: len(in.HTML), HeightHint: clampHeightHint(in.HeightHint)}
|
||||
return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat (%s, sandboxed: scripts and network are disabled; it follows the operator's light or dark theme only if it styles itself for dark mode).", humanBytes(int64(len(in.HTML))))}
|
||||
case chattools.ToolShowDiff:
|
||||
return s.runShowDiff(ctx, sessionID, showDiffRequest{Path: in.Path, Base: in.Base, CommittedOnly: in.Committed, Title: title})
|
||||
var in struct {
|
||||
Path string `json:"path"`
|
||||
Base string `json:"base"`
|
||||
Head string `json:"head"`
|
||||
Paths []string `json:"paths"`
|
||||
Committed bool `json:"committed_only"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
if err := decode(&in); err != nil {
|
||||
return fail("Nothing was shown: %v", err)
|
||||
}
|
||||
return s.runShowDiff(ctx, sessionID, showDiffRequest{Path: in.Path, Base: in.Base, Head: in.Head, Paths: in.Paths, CommittedOnly: in.Committed, Title: boundedLine(in.Title, 200)})
|
||||
case chattools.ToolShowRun:
|
||||
var in struct {
|
||||
RunID int64 `json:"run_id"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
if err := decode(&in); err != nil {
|
||||
return fail("Nothing was shown: %v", err)
|
||||
}
|
||||
return s.runShowRun(ctx, in.RunID, boundedLine(in.Title, 200))
|
||||
case chattools.ToolScheduleFollowup:
|
||||
var in struct {
|
||||
Message string `json:"message"`
|
||||
At string `json:"at"`
|
||||
AfterMinutes *int64 `json:"after_minutes"`
|
||||
}
|
||||
if err := decode(&in); err != nil {
|
||||
return fail("Nothing was scheduled: %v", err)
|
||||
}
|
||||
return nil, s.runScheduleFollowup(ctx, sessionID, in.Message, in.At, in.AfterMinutes)
|
||||
case chattools.ToolNotify:
|
||||
return s.runNotify(ctx, sessionID, title, in.Message, in.Priority)
|
||||
var in struct {
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
Priority string `json:"priority"`
|
||||
}
|
||||
if err := decode(&in); err != nil {
|
||||
return fail("Not sent: %v", err)
|
||||
}
|
||||
return s.runNotify(ctx, sessionID, boundedLine(in.Title, 200), in.Message, in.Priority)
|
||||
}
|
||||
return fail("Unknown tool %s.", tool)
|
||||
}
|
||||
|
||||
// shownKeptNote says how a shown file is kept: a copy, or the live file.
|
||||
func shownKeptNote(item protocol.ChatShownItem) string {
|
||||
if item.Snapshot {
|
||||
return "; Pipelines keeps a copy of this version"
|
||||
}
|
||||
return "; shown from the live file, so the card notes later changes"
|
||||
}
|
||||
|
||||
func (s *chatToolService) runNotify(ctx context.Context, sessionID, title, message, priority string) (*protocol.ChatShownItem, chattools.Result) {
|
||||
message = boundedLine(message, 2000)
|
||||
if title == "" || message == "" {
|
||||
|
||||
Reference in New Issue
Block a user