-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommitmsg.ts
More file actions
214 lines (179 loc) · 6.39 KB
/
commitmsg.ts
File metadata and controls
214 lines (179 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* `codebase commitmsg` — Generate a commit message from git diff using AI.
*
* @module
*/
import * as results from "@eser/primitives/results";
import * as streams from "@eser/streams";
import * as span from "@eser/streams/span";
import * as shellExec from "@eser/shell/exec";
import type * as shellArgs from "@eser/shell/args";
import * as ai from "@eser/ai/mod";
import type * as workflows from "@eser/workflows/mod";
// =============================================================================
// System Prompt
// =============================================================================
const SYSTEM_PROMPT = `Output ONLY a conventional commit message. Nothing else.
STRICT RULES:
- One line only. Max 72 characters.
- Format: type(scope): description
- Types: feat, fix, chore, docs, refactor, test, style, perf, ci, build
- No body. No explanation. No markdown. No code blocks. No bullet points.
- No quotes. No backticks. No decorative formatting. No insight blocks.
- No preamble. No commentary. Just the commit message line.
Example of correct output (the ENTIRE response is this one line):
feat(ai): add streaming support for Claude Code adapter`;
// =============================================================================
// Main
// =============================================================================
export const main = async (
_args?: readonly string[],
): Promise<shellArgs.CliResult<void>> => {
const out = streams.output({
renderer: streams.renderers.ansi(),
sink: streams.sinks.stdout(),
});
try {
// Get git diff (staged first, fall back to unstaged)
let diff = await shellExec.exec`git diff --cached`.noThrow().text();
if (diff.length === 0) {
diff = await shellExec.exec`git diff`.noThrow().text();
}
if (diff.length === 0) {
out.writeln(
span.dim("No changes detected (nothing staged or modified)."),
);
await out.close();
return results.ok(undefined);
}
const message = await generateCommitMessage(diff);
// Escape shell-special characters inside double quotes:
// " → \" backtick → \` $ → \$ ! → \! \ → \\
const escaped = message
.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')
.replace(/`/g, "\\`")
.replace(/\$/g, "\\$")
.replace(/!/g, "\\!");
out.writeln("");
out.writeln(span.bold("Plain Format:"));
out.writeln("```");
out.writeln(message);
out.writeln("```");
out.writeln("");
out.writeln(span.bold("Shell Command:"));
out.writeln("```");
out.writeln(`git commit -m "${escaped}"`);
out.writeln("```");
out.writeln("");
out.writeln(span.bold("Copy to Clipboard:"));
out.writeln("```");
out.writeln(`echo "${escaped}" | pbcopy`);
out.writeln("```");
await out.close();
return results.ok(undefined);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
out.writeln(span.red(`Error: ${message}`));
await out.close();
return results.fail({ message, exitCode: 1 });
}
};
// =============================================================================
// Direct AI usage (for programmatic consumers and workflow tools)
// =============================================================================
export const generateCommitMessage = async (
diff: string,
providerName?: string,
): Promise<string> => {
// Build a minimal registry with auto-detected provider
const adapters = await import("@eser/ai/adapters");
const factories = await adapters.defaultFactories();
const registry = new ai.Registry({ factories });
// Determine provider
let provider = providerName;
if (provider === undefined) {
// Try claude-code first
try {
const code = await shellExec.exec`which claude`.noThrow().code();
if (code === 0) {
provider = "claude-code";
}
} catch {
// Not found
}
}
if (provider === undefined) {
provider = "anthropic"; // fallback to API
}
const model = "default";
await registry.addModel("default", { provider, model });
const languageModel = registry.getDefault();
if (languageModel === null) {
throw new Error("No AI model available for commit message generation");
}
const result = await languageModel.generateText({
system: SYSTEM_PROMPT,
messages: [
ai.textMessage(
"user",
`Generate a commit message for this diff:\n\n${diff}`,
),
],
maxTokens: 256,
});
await registry.close();
return ai.text(result).trim();
};
// =============================================================================
// Workflow Tool
// =============================================================================
export const workflowTool: workflows.WorkflowTool = {
name: "ai-commitmsg",
description: "Generate commit message from git diff using AI",
run: async (options): Promise<workflows.WorkflowToolResult> => {
try {
const provider = options["provider"] as string | undefined;
let diff = await shellExec.exec`git diff --cached`.noThrow().text();
if (diff.length === 0) {
diff = await shellExec.exec`git diff`.noThrow().text();
}
if (diff.length === 0) {
return {
name: "ai-commitmsg",
passed: true,
issues: [],
mutations: [],
stats: { skipped: 1 } as Record<string, number>,
};
}
const message = await generateCommitMessage(diff, provider);
return {
name: "ai-commitmsg",
passed: true,
issues: [{ message: `Suggested: ${message}` }],
mutations: [],
stats: { generated: 1 } as Record<string, number>,
};
} catch (err) {
return {
name: "ai-commitmsg",
passed: false,
issues: [{ message: err instanceof Error ? err.message : String(err) }],
mutations: [],
stats: { errors: 1 } as Record<string, number>,
};
}
},
};
// =============================================================================
// Standalone execution
// =============================================================================
if (import.meta.main) {
const { runCliMain, createCliOutput } = await import("./cli-support.ts");
const out = createCliOutput();
const result = await main();
runCliMain(result, out);
await out.close();
}