Project Files
src / tools / git-log-tool.ts
import { tool } from "@lmstudio/sdk"
import { simpleGit } from "simple-git"
import { z } from "zod"
import { formatGitError } from "../git/error-formatting"
import { resolveAndValidateRepoPath } from "../path/validation"
import type { Tool, ToolsProviderController } from "@lmstudio/sdk"
/**
* Create the git log tool.
*
* @param ctl Tools provider controller supplied by the LM Studio SDK.
* @returns The configured git log tool.
*/
export function createGitLogTool(ctl: ToolsProviderController): Tool {
return tool({
name: "git_log",
description:
"View the recent commit history from a cloned git repository. Optionally specify a branch and number of commits (default 10, max 100).",
parameters: {
repoName: z.string().describe("Directory name of the cloned git repository."),
count: z.number().min(1).max(100).default(10).describe("Number of recent commits to return (1-100)."),
branch: z.string().optional().describe("Branch name to show log for (defaults to current branch)."),
},
/**
* Executes the git log command.
*
* @param arguments_ Validated tool parameters.
* @param arguments_.repoName Repository directory name relative to the working directory.
* @param arguments_.count Number of commits to return.
* @param arguments_.branch Optional branch name to log; defaults to current branch.
* @param context Runtime tool context supplied by the SDK.
* @returns The commit log as JSON, or a user-facing error string.
*/
implementation: async (arguments_, context) => {
const { repoName, count, branch } = arguments_
const repoPath = resolveAndValidateRepoPath(repoName, ctl.getWorkingDirectory())
const git = simpleGit(repoPath)
const target = typeof branch === "string" ? `"${repoName}@${branch}"` : `"${repoName}"`
context.status(`Fetching last ${count} commit(s) from ${target}…`)
try {
const log = await git.log({
from: branch,
maxCount: count,
})
return JSON.stringify(log.all, undefined, 2)
} catch (error) {
return formatGitError("git_log", repoName, error)
}
},
})
}