log.ts

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: GPL-3.0-or-later
 4
 5import { Type } from "@sinclair/typebox";
 6import type { AgentTool } from "@mariozechner/pi-agent-core";
 7import simpleGit from "simple-git";
 8import { ToolInputError } from "../../../util/errors.js";
 9
10// Trust boundary: refs and paths are passed directly to simple-git, which is
11// scoped to the workspace. The user chose to clone this repo, so its contents
12// are trusted. See AGENTS.md § Workspace Sandboxing.
13
14const DEFAULT_LOG_LIMIT = 20;
15
16const LogSchema = Type.Object({
17  path: Type.Optional(Type.String({ description: "Filter to commits touching this path" })),
18  author: Type.Optional(Type.String({ description: "Filter by author name/email" })),
19  since: Type.Optional(Type.String({ description: "Commits after this date (e.g., 2024-01-01)" })),
20  until: Type.Optional(Type.String({ description: "Commits before this date" })),
21  n: Type.Optional(Type.Number({ description: "Maximum number of commits (default: 20)" })),
22  oneline: Type.Optional(Type.Boolean({ description: "Compact one-line format (default: false)" })),
23});
24
25export const createGitLogTool = (workspacePath: string): AgentTool => ({
26  name: "git_log",
27  label: "Git Log",
28  description: "View commit history with optional path, author, and date filters.",
29  parameters: LogSchema as any,
30  execute: async (_toolCallId: string, params: any) => {
31    const git = simpleGit(workspacePath);
32    const options: string[] = [];
33
34    const limit = params.n !== undefined ? params.n : DEFAULT_LOG_LIMIT;
35    if (typeof limit !== "number" || Number.isNaN(limit) || limit <= 0) {
36      throw new ToolInputError("n must be a positive number");
37    }
38    options.push("-n", String(Math.floor(limit)));
39    if (params.oneline) options.push("--oneline");
40    if (params.author !== undefined) {
41      if (!String(params.author).trim()) {
42        throw new ToolInputError("author must be a non-empty string");
43      }
44      options.push(`--author=${params.author}`);
45    }
46    if (params.since !== undefined) {
47      if (!String(params.since).trim()) {
48        throw new ToolInputError("since must be a non-empty string");
49      }
50      options.push(`--since=${params.since}`);
51    }
52    if (params.until !== undefined) {
53      if (!String(params.until).trim()) {
54        throw new ToolInputError("until must be a non-empty string");
55      }
56      options.push(`--until=${params.until}`);
57    }
58
59    const result = await git.log(options.concat(params.path ? ["--", params.path] : []));
60
61    const text = result.all
62      .map((entry) =>
63        params.oneline
64          ? `${entry.hash} ${entry.message}`
65          : `${entry.hash} ${entry.date} ${entry.author_name} ${entry.message}`,
66      )
67      .join("\n");
68
69    return {
70      content: [{ type: "text", text }],
71      details: { count: result.all.length },
72    };
73  },
74});