1import { Type } from "@sinclair/typebox";
2import type { AgentTool } from "@mariozechner/pi-ai";
3import simpleGit from "simple-git";
4import { ToolInputError } from "../../../util/errors.js";
5import { formatSize, truncateHead } from "../../../util/truncate.js";
6
7const ShowSchema = Type.Object({
8 ref: Type.String({ description: "Commit hash or ref" }),
9});
10
11export const createGitShowTool = (workspacePath: string): AgentTool => ({
12 name: "git_show",
13 label: "Git Show",
14 description: "Show details for a commit or object.",
15 parameters: ShowSchema as any,
16 execute: async (_toolCallId: string, params: any) => {
17 if (!String(params.ref ?? "").trim()) {
18 throw new ToolInputError("ref must be a non-empty string");
19 }
20 const git = simpleGit(workspacePath);
21 const raw = await git.show([params.ref]);
22 const truncation = truncateHead(raw);
23
24 let text = truncation.content;
25 if (truncation.truncated) {
26 text += `\n\n[truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines (${formatSize(truncation.outputBytes)} of ${formatSize(truncation.totalBytes)})]`;
27 }
28
29 return {
30 content: [{ type: "text", text }],
31 details: { ref: params.ref, ...(truncation.truncated ? { truncation } : {}) },
32 };
33 },
34});