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:
@@ -32,10 +32,56 @@ const (
|
||||
type showDiffRequest struct {
|
||||
Path string
|
||||
Base string
|
||||
Head string
|
||||
Paths []string
|
||||
CommittedOnly bool
|
||||
Title string
|
||||
}
|
||||
|
||||
// showDiffMaxPaths bounds the paths argument.
|
||||
const showDiffMaxPaths = 50
|
||||
|
||||
// showDiffPathspecs turns the model's paths into Git pathspecs confined to the
|
||||
// repository: each must be relative, without a .. component, option prefix,
|
||||
// pathspec magic or control character. A path with glob characters matches as
|
||||
// a glob (":(glob)"), any other literally.
|
||||
func showDiffPathspecs(paths []string) ([]string, []string, error) {
|
||||
if len(paths) > showDiffMaxPaths {
|
||||
return nil, nil, fmt.Errorf("paths has %d entries; the limit is %d", len(paths), showDiffMaxPaths)
|
||||
}
|
||||
var specs, kept []string
|
||||
for _, raw := range paths {
|
||||
path := strings.TrimSpace(raw)
|
||||
if path == "" {
|
||||
continue
|
||||
}
|
||||
if len(path) > 500 || strings.HasPrefix(path, "-") || strings.HasPrefix(path, ":") || filepath.IsAbs(path) || strings.HasPrefix(path, "~") {
|
||||
return nil, nil, fmt.Errorf("paths entry %q must be a path inside the repository, relative to its top", raw)
|
||||
}
|
||||
for _, r := range path {
|
||||
if unicode.IsControl(r) {
|
||||
return nil, nil, fmt.Errorf("paths entry %q contains a control character", raw)
|
||||
}
|
||||
}
|
||||
for _, part := range strings.Split(filepath.ToSlash(path), "/") {
|
||||
if part == ".." {
|
||||
return nil, nil, fmt.Errorf("paths entry %q leaves the repository", raw)
|
||||
}
|
||||
}
|
||||
path = strings.TrimSuffix(filepath.ToSlash(filepath.Clean(path)), "/")
|
||||
if path == "." {
|
||||
continue
|
||||
}
|
||||
kept = append(kept, path)
|
||||
if strings.ContainsAny(path, "*?[") {
|
||||
specs = append(specs, ":(glob)"+path)
|
||||
} else {
|
||||
specs = append(specs, ":(literal)"+path)
|
||||
}
|
||||
}
|
||||
return specs, kept, nil
|
||||
}
|
||||
|
||||
// runShowDiff compares a repository inside the operator's home and records
|
||||
// the per-file patches as one diff item.
|
||||
func (s *chatToolService) runShowDiff(ctx context.Context, sessionID string, in showDiffRequest) (*protocol.ChatShownItem, chattools.Result) {
|
||||
@@ -49,6 +95,17 @@ func (s *chatToolService) runShowDiff(ctx context.Context, sessionID string, in
|
||||
if base != "" && !validGitRevision(base) {
|
||||
return fail(fmt.Errorf("base %q is not a branch, tag or commit name", base))
|
||||
}
|
||||
headName := strings.TrimSpace(in.Head)
|
||||
if headName != "" && !validGitRevision(headName) {
|
||||
return fail(fmt.Errorf("head %q is not a branch, tag or commit name", headName))
|
||||
}
|
||||
if headName != "" && in.CommittedOnly {
|
||||
return fail(errors.New("committed_only does not apply with head, which is always committed"))
|
||||
}
|
||||
pathspecs, paths, err := showDiffPathspecs(in.Paths)
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
directory, err := s.resolveDiffDirectory(ctx, sessionID, in.Path)
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
@@ -70,8 +127,23 @@ func (s *chatToolService) runShowDiff(ctx context.Context, sessionID string, in
|
||||
} else if head != "" {
|
||||
body.Head = shortCommit(head)
|
||||
}
|
||||
current := head
|
||||
if headName != "" {
|
||||
target, err := git.output("rev-parse", "--verify", "--quiet", "--end-of-options", headName+"^{commit}")
|
||||
if err != nil {
|
||||
return fail(fmt.Errorf("head %q is not a commit in %s", headName, git.directory))
|
||||
}
|
||||
head, headErr = strings.TrimSpace(target), nil
|
||||
body.Head, body.HeadRevision = headName, headName
|
||||
}
|
||||
body.Paths = paths
|
||||
var compare string
|
||||
switch {
|
||||
case headName != "" && base == "":
|
||||
if current == "" {
|
||||
return fail(errors.New("this repository has no HEAD commit to compare " + headName + " with; pass base"))
|
||||
}
|
||||
compare = current
|
||||
case base != "":
|
||||
if headErr != nil || head == "" {
|
||||
return fail(errors.New("this repository has no commits yet, so there is nothing to compare with " + base))
|
||||
@@ -94,11 +166,12 @@ func (s *chatToolService) runShowDiff(ctx context.Context, sessionID string, in
|
||||
body.BaseCommit = shortCommit(compare)
|
||||
}
|
||||
revisions := []string{compare}
|
||||
if in.CommittedOnly {
|
||||
worktree := !in.CommittedOnly && headName == ""
|
||||
if !worktree {
|
||||
revisions = append(revisions, head)
|
||||
}
|
||||
|
||||
changes, err := collectGitDiff(git, revisions, !in.CommittedOnly)
|
||||
changes, err := collectGitDiff(git, revisions, worktree, pathspecs)
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
@@ -118,6 +191,10 @@ func (s *chatToolService) runShowDiff(ctx context.Context, sessionID string, in
|
||||
func showDiffSummary(body protocol.ChatShownDiffBody) string {
|
||||
against := "HEAD"
|
||||
switch {
|
||||
case body.HeadRevision != "" && body.Base != "":
|
||||
against = fmt.Sprintf("%s (merge base %s), showing %s", body.Base, body.BaseCommit, body.HeadRevision)
|
||||
case body.HeadRevision != "":
|
||||
against = fmt.Sprintf("HEAD (%s), showing %s", body.BaseCommit, body.HeadRevision)
|
||||
case body.Base != "" && body.CommittedOnly:
|
||||
against = fmt.Sprintf("%s (merge base %s), committed changes only", body.Base, body.BaseCommit)
|
||||
case body.Base != "":
|
||||
@@ -125,6 +202,9 @@ func showDiffSummary(body protocol.ChatShownDiffBody) string {
|
||||
case body.BaseCommit == "":
|
||||
against = "an empty repository"
|
||||
}
|
||||
if len(body.Paths) > 0 {
|
||||
against += ", limited to " + strings.Join(body.Paths, ", ")
|
||||
}
|
||||
if body.Files == 0 {
|
||||
return fmt.Sprintf("Shown in the operator's Pipelines chat: no changes in %s against %s.", body.Repo, against)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user