checkout.ts

 1import { Type } from "@sinclair/typebox";
 2import type { AgentTool } from "@mariozechner/pi-ai";
 3import simpleGit from "simple-git";
 4import { ToolInputError } from "../../../util/errors.js";
 5
 6const CheckoutSchema = Type.Object({
 7  ref: Type.String({ description: "Ref to checkout" }),
 8});
 9
10export const createGitCheckoutTool = (workspacePath: string): AgentTool => ({
11  name: "git_checkout",
12  label: "Git Checkout",
13  description: "Checkout a branch, tag, or commit.",
14  parameters: CheckoutSchema as any,
15  execute: async (_toolCallId: string, params: any) => {
16    if (!String(params.ref ?? "").trim()) {
17      throw new ToolInputError("ref must be a non-empty string");
18    }
19    const git = simpleGit(workspacePath);
20    await git.checkout(params.ref);
21
22    return {
23      content: [{ type: "text", text: `Checked out ${params.ref}` }],
24      details: { ref: params.ref },
25    };
26  },
27});