ai-commit-report-generator-cli
Version:
An AI-powered CLI tool that generates weekly reports from your Git activity
95 lines (91 loc) • 4.44 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommitAIProcessorAgent = void 0;
const google_genai_1 = require("@langchain/google-genai");
const zod_1 = require("zod");
const prompts_1 = require("@langchain/core/prompts");
const output_parsers_1 = require("langchain/output_parsers");
const commitSummary_1 = require("./commitSummary");
const tools_1 = require("@langchain/core/tools");
const schemas_1 = require("./schemas");
const commitFetcher_1 = require("./commitFetcher");
const agents_1 = require("langchain/agents");
class CommitAIProcessorAgent {
constructor() {
this.parser = output_parsers_1.StructuredOutputParser.fromZodSchema(commitSummary_1.CommitSummarySchema);
this.prompt = new prompts_1.PromptTemplate({
template: `
You are a commit analyzer. Follow these steps in order:
1. Here is the commit information:\n
- Commit message: {message}
- Commit hash: {hash}
- Global stats: {statistics}
2. Depending on the commit attributes and statistics decide to use the "file_diffs_tool" to get the diffs or the code changes of a specific file in the commit statistics, You can use the tool as many times as needed before proceeding to the next step.\n
3. Analyse the commit attributes, statistics and code changes to generate a summary of the commit.\n
4. The final summary should respect the format instructions below.\n
{format_instructions}
`,
inputVariables: ['message', 'hash', 'statistics', 'format_instructions', 'agent_scratchpad'],
});
this.llm = new google_genai_1.ChatGoogleGenerativeAI({
modelName: "gemini-2.0-flash-exp",
apiKey: process.env.GOOGLE_API_KEY,
});
}
init() {
return __awaiter(this, void 0, void 0, function* () {
const tools = [this.getCommitDiffsTool()];
const agent = yield (0, agents_1.createOpenAIFunctionsAgent)({
llm: this.llm,
//@ts-ignore
prompt: this.prompt,
tools
});
this.agentExecutor = agents_1.AgentExecutor.fromAgentAndTools({
agent,
tools,
//verbose: true,
callbacks: [
//new ConsoleCallbackHandler(),
],
});
});
}
generateCommitSummary(_a) {
return __awaiter(this, arguments, void 0, function* ({ commit, statistics }) {
const result = yield this.agentExecutor.invoke({
message: commit.message,
hash: commit.hash,
statistics: JSON.stringify(statistics),
format_instructions: this.parser.getFormatInstructions()
});
return this.parser.parse(result.output);
});
}
getCommitDiffsTool() {
return (0, tools_1.tool)((_a) => __awaiter(this, [_a], void 0, function* ({ filePath, hash }) {
return (0, commitFetcher_1.fetchDiffs)({
filePath,
hash
});
}), {
name: "file_diffs_tool",
verbose: true,
description: "You can optionally use this tool to see the actual code changes before creating any summary. It shows what was modified in the source files.",
schema: zod_1.z.object({
filePath: zod_1.z.string().describe("Path of the source file to examine"),
hash: schemas_1.CommitSchema.shape.hash.describe("The commit hash to analyze")
}),
});
}
}
exports.CommitAIProcessorAgent = CommitAIProcessorAgent;