1import { Type } from "@sinclair/typebox";
2import type { AgentTool } from "@mariozechner/pi-agent-core";
3import simpleGit from "simple-git";
4import { ToolInputError } from "../../../util/errors.js";
5import { formatSize, truncateHead } from "../../../util/truncate.js";
6
7// Trust boundary: refs and paths are passed directly to simple-git, which is
8// scoped to the workspace. The user chose to clone this repo, so its contents
9// are trusted. See AGENTS.md § Workspace Sandboxing.
10
11const DiffSchema = Type.Object({
12 ref: Type.Optional(Type.String({ description: "Base ref (optional)" })),
13 ref2: Type.Optional(Type.String({ description: "Compare ref (optional)" })),
14 path: Type.Optional(Type.String({ description: "Limit diff to path" })),
15});
16
17export const createGitDiffTool = (workspacePath: string): AgentTool => ({
18 name: "git_diff",
19 label: "Git Diff",
20 description: "Show diff between refs, or between a ref and the working tree. Omit both refs for unstaged changes.",
21 parameters: DiffSchema as any,
22 execute: async (_toolCallId: string, params: any) => {
23 const git = simpleGit(workspacePath);
24 const args: string[] = [];
25
26 if (params.ref && !String(params.ref).trim()) {
27 throw new ToolInputError("ref must be a non-empty string");
28 }
29 if (params.ref2 && !String(params.ref2).trim()) {
30 throw new ToolInputError("ref2 must be a non-empty string");
31 }
32 if (params.path && !String(params.path).trim()) {
33 throw new ToolInputError("path must be a non-empty string");
34 }
35 if (params.ref) args.push(params.ref);
36 if (params.ref2) args.push(params.ref2);
37 if (params.path) args.push("--", params.path);
38
39 const raw = await git.diff(args);
40 const truncation = truncateHead(raw);
41
42 let text = truncation.content;
43 if (truncation.truncated) {
44 text += `\n\n[truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines (${formatSize(truncation.outputBytes)} of ${formatSize(truncation.totalBytes)})]`;
45 }
46
47 return {
48 content: [{ type: "text", text }],
49 details: { path: params.path ?? null, ...(truncation.truncated ? { truncation } : {}) },
50 };
51 },
52});