4 Commits

5 changed files with 190 additions and 136 deletions
+22
View File
@@ -0,0 +1,22 @@
# AGENTS.md
## Build & Run
- **Compile**: `npm run compile` (or `tsc -p ./`)
- **Watch**: `npm run watch`
- **Package extension**: `npm run build` → produces `.vsix` file
- **Test**: Press F5 to launch extension in debug mode
## Project Structure
- `src/extension.ts` - Extension entry point, registers `aiCommitExt.generate` command
- `src/opencodeService.ts` - Spawns `opencode run` CLI, parses output for commit message
- `src/gitService.ts` - Uses VS Code Git extension API to get diffs and repo root
## Key Details
- Output compiled to `out/` directory
- Extension activates only on command invocation (not on startup)
- OpenCode CLI is required on PATH; checked via `which opencode`
- Generated message written to SCM input box via `repository.inputBox.value`
- Timeout: 120 seconds for OpenCode response
+1 -1
View File
@@ -2,7 +2,7 @@
"name": "ai-commit-ext",
"displayName": "AI Commit Ext",
"description": "Generate commit messages using OpenCode AI",
"version": "1.0.1",
"version": "1.2.0",
"publisher": "local",
"engines": {
"vscode": "^1.110.0"
+12 -2
View File
@@ -88,14 +88,24 @@ async function handleGenerateCommitMessage(): Promise<void> {
return;
}
const userSuggestion = await vscode.window.showInputBox({
prompt: "Suggest a commit message (optional)",
placeHolder: "e.g., added login button",
ignoreFocusOut: true,
});
if (userSuggestion === undefined) {
return;
}
const commitMessage = await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: "Generating commit message...",
title: userSuggestion ? "Improving commit message..." : "Generating commit message...",
},
async () => {
return await generateCommitMessage();
return await generateCommitMessage({ userSuggestion: userSuggestion || undefined });
},
);
+28 -25
View File
@@ -1,4 +1,4 @@
import * as vscode from 'vscode';
import * as vscode from "vscode";
interface GitExtension {
getAPI(version: number): GitAPI;
@@ -30,14 +30,14 @@ interface Change {
export interface GitChange {
path: string;
status: 'added' | 'modified' | 'deleted' | 'renamed' | 'untracked';
status: "added" | "modified" | "deleted" | "renamed" | "untracked";
diff?: string;
}
export async function getGitChanges(): Promise<GitChange[]> {
const repository = await getActiveGitRepository();
if (!repository) {
throw new Error('No Git repository found');
throw new Error("No Git repository found");
}
const state = repository.state;
@@ -54,8 +54,8 @@ export async function getGitChanges(): Promise<GitChange[]> {
});
}
} else if (unstagedChanges.length > 0) {
const config = vscode.workspace.getConfiguration('aiCommitExt');
const includeUnstaged = config.get<boolean>('includeUnstaged', false);
const config = vscode.workspace.getConfiguration("aiCommitExt");
const includeUnstaged = config.get<boolean>("includeUnstaged", false);
if (includeUnstaged) {
for (const change of unstagedChanges) {
@@ -73,58 +73,61 @@ export async function getGitChanges(): Promise<GitChange[]> {
export async function getGitDiff(): Promise<string> {
const repository = await getActiveGitRepository();
if (!repository) {
throw new Error('No Git repository found');
throw new Error("No Git repository found");
}
const state = repository.state;
let diffOutput = '';
let diffOutput = "";
const stagedChanges = state.indexChanges || [];
const unstagedChanges = state.workingTreeChanges || [];
const config = vscode.workspace.getConfiguration('aiCommitExt');
const includeUnstaged = config.get<boolean>('includeUnstaged', false);
const config = vscode.workspace.getConfiguration("aiCommitExt");
const includeUnstaged = config.get<boolean>("includeUnstaged", false);
if (stagedChanges.length > 0) {
for (const change of stagedChanges) {
const fileName = change.uri.fsPath.split('/').pop() || '';
const fileName = change.uri.fsPath.split("/").pop() || "";
diffOutput += `## ${fileName} (staged)\n`;
try {
const diff = await repository.diffIndexWithHEAD(change.uri.fsPath);
const diff = await repository.diffIndexWithHEAD(
change.uri.fsPath,
);
if (diff) {
diffOutput += diff + '\n';
diffOutput += diff + "\n";
}
} catch {
diffOutput += '(Unable to get diff)\n';
diffOutput += "(Unable to get diff)\n";
}
}
} else if (unstagedChanges.length > 0 && includeUnstaged) {
for (const change of unstagedChanges) {
const fileName = change.uri.fsPath.split('/').pop() || '';
const fileName = change.uri.fsPath.split("/").pop() || "";
diffOutput += `## ${fileName} (unstaged)\n`;
try {
const diff = await repository.diffWithHEAD(change.uri.fsPath);
if (diff) {
diffOutput += diff + '\n';
diffOutput += diff + "\n";
}
} catch {
diffOutput += '(Unable to get diff)\n';
diffOutput += "(Unable to get diff)\n";
}
}
}
if (!diffOutput) {
throw new Error('No changes to commit');
throw new Error("No changes to commit");
}
return diffOutput;
}
async function getActiveGitRepository(): Promise<Repository | null> {
const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git');
const gitExtension =
vscode.extensions.getExtension<GitExtension>("vscode.git");
if (!gitExtension) {
throw new Error('Git extension not found');
throw new Error("Git extension not found");
}
const api = gitExtension.exports.getAPI(1);
@@ -134,7 +137,7 @@ async function getActiveGitRepository(): Promise<Repository | null> {
return api.repositories[0];
}
function getChangeStatus(status: number): GitChange['status'] {
function getChangeStatus(status: number): GitChange["status"] {
const Status = {
INDEX_MODIFIED: 0,
INDEX_ADDED: 1,
@@ -146,16 +149,16 @@ function getChangeStatus(status: number): GitChange['status'] {
};
if (status === Status.INDEX_ADDED || status === Status.UNTRACKED) {
return 'added';
return "added";
}
if (status === Status.INDEX_DELETED || status === Status.DELETED) {
return 'deleted';
return "deleted";
}
if (status === Status.INDEX_RENAMED) {
return 'renamed';
return "renamed";
}
return 'modified';
return "modified";
}
export async function getRepositoryRoot(): Promise<string | null> {
@@ -163,7 +166,7 @@ export async function getRepositoryRoot(): Promise<string | null> {
if (!repository) {
return null;
}
if (typeof repository.root === 'string') {
if (typeof repository.root === "string") {
return repository.root;
}
return repository.rootUri.fsPath;
+20 -1
View File
@@ -5,6 +5,7 @@ import { output } from "./extension";
export interface GenerateOptions {
model?: string;
userSuggestion?: string;
}
let opencodeAvailableCache: boolean | null = null;
@@ -47,12 +48,27 @@ export async function generateCommitMessage(
const config = vscode.workspace.getConfiguration("aiCommitExt");
const model = options.model || config.get<string>("model", "");
const prompt = `${DEFAULT_PROMPT}
let prompt: string;
if (options.userSuggestion) {
prompt = `The user suggested: "${options.userSuggestion}"
Improve this commit message to be concise and follow Conventional Commit format.
Format: <type>(<scope>): <description>
Max 72 characters for the subject line.
Types: feat, fix, refactor, docs, style, test, chore, perf, ci, build, revert
Here are the git changes:
${diff}
Only output the improved commit message, nothing else.`;
} else {
prompt = `${DEFAULT_PROMPT}
Here are the git changes:
${diff}
Generate a concise Conventional Commit message for these changes:`;
}
const log = `[${Date.now()}]\r\n${prompt}`;
output.appendLine(log);
@@ -60,8 +76,11 @@ Generate a concise Conventional Commit message for these changes:`;
return new Promise((resolve, reject) => {
const args: string[] = [
"run",
"--pure",
"--format",
"default",
"-m",
"opencode/gpt-5-nano",
"--variant",
"minimal",
];