Compare commits
6 Commits
cecec8bf23
...
1.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
| a7f07dbeef | |||
| 4fc21100a9 | |||
| 18061a9156 | |||
| c887635a0c | |||
| 55df24e0fd | |||
| fd4fd87ea8 |
@@ -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
|
||||
Generated
+570
-570
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -2,7 +2,7 @@
|
||||
"name": "ai-commit-ext",
|
||||
"displayName": "AI Commit Ext",
|
||||
"description": "Generate commit messages using OpenCode AI",
|
||||
"version": "1.0.0",
|
||||
"version": "1.2.0",
|
||||
"publisher": "local",
|
||||
"engines": {
|
||||
"vscode": "^1.110.0"
|
||||
|
||||
+19
-5
@@ -41,7 +41,11 @@ async function handleGenerateCommitMessage(): Promise<void> {
|
||||
const showNotification = config.get<boolean>("showNotification", true);
|
||||
|
||||
try {
|
||||
const opencodeAvailable = await isOpenCodeAvailable();
|
||||
const [opencodeAvailable, gitExtension] = await Promise.all([
|
||||
isOpenCodeAvailable(),
|
||||
vscode.extensions.getExtension<GitExtension>("vscode.git"),
|
||||
]);
|
||||
|
||||
if (!opencodeAvailable) {
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage(
|
||||
@@ -51,8 +55,6 @@ async function handleGenerateCommitMessage(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
const gitExtension =
|
||||
vscode.extensions.getExtension<GitExtension>("vscode.git");
|
||||
if (!gitExtension) {
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage("Git extension not found");
|
||||
@@ -86,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 });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -114,3 +126,5 @@ async function handleGenerateCommitMessage(): Promise<void> {
|
||||
}
|
||||
|
||||
export function deactivate(): void {}
|
||||
|
||||
export const output = vscode.window.createOutputChannel("ai-commit-ext");
|
||||
|
||||
+135
-132
@@ -1,170 +1,173 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as vscode from "vscode";
|
||||
|
||||
interface GitExtension {
|
||||
getAPI(version: number): GitAPI;
|
||||
getAPI(version: number): GitAPI;
|
||||
}
|
||||
|
||||
interface GitAPI {
|
||||
repositories: Repository[];
|
||||
repositories: Repository[];
|
||||
}
|
||||
|
||||
interface Repository {
|
||||
root: string;
|
||||
rootUri: vscode.Uri;
|
||||
state: RepositoryState;
|
||||
inputBox: { value: string };
|
||||
diffWithHEAD(path?: string): Promise<string>;
|
||||
diffIndexWithHEAD(path?: string): Promise<string>;
|
||||
status(): Promise<void>;
|
||||
root: string;
|
||||
rootUri: vscode.Uri;
|
||||
state: RepositoryState;
|
||||
inputBox: { value: string };
|
||||
diffWithHEAD(path?: string): Promise<string>;
|
||||
diffIndexWithHEAD(path?: string): Promise<string>;
|
||||
status(): Promise<void>;
|
||||
}
|
||||
|
||||
interface RepositoryState {
|
||||
indexChanges: Change[];
|
||||
workingTreeChanges: Change[];
|
||||
indexChanges: Change[];
|
||||
workingTreeChanges: Change[];
|
||||
}
|
||||
|
||||
interface Change {
|
||||
uri: vscode.Uri;
|
||||
status: number;
|
||||
uri: vscode.Uri;
|
||||
status: number;
|
||||
}
|
||||
|
||||
export interface GitChange {
|
||||
path: string;
|
||||
status: 'added' | 'modified' | 'deleted' | 'renamed' | 'untracked';
|
||||
diff?: string;
|
||||
path: string;
|
||||
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');
|
||||
}
|
||||
|
||||
const state = repository.state;
|
||||
const changes: GitChange[] = [];
|
||||
|
||||
const stagedChanges = state.indexChanges || [];
|
||||
const unstagedChanges = state.workingTreeChanges || [];
|
||||
|
||||
if (stagedChanges.length > 0) {
|
||||
for (const change of stagedChanges) {
|
||||
changes.push({
|
||||
path: change.uri.fsPath,
|
||||
status: getChangeStatus(change.status),
|
||||
});
|
||||
const repository = await getActiveGitRepository();
|
||||
if (!repository) {
|
||||
throw new Error("No Git repository found");
|
||||
}
|
||||
} else if (unstagedChanges.length > 0) {
|
||||
const config = vscode.workspace.getConfiguration('aiCommitExt');
|
||||
const includeUnstaged = config.get<boolean>('includeUnstaged', false);
|
||||
|
||||
if (includeUnstaged) {
|
||||
for (const change of unstagedChanges) {
|
||||
changes.push({
|
||||
path: change.uri.fsPath,
|
||||
status: getChangeStatus(change.status),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changes;
|
||||
const state = repository.state;
|
||||
const changes: GitChange[] = [];
|
||||
|
||||
const stagedChanges = state.indexChanges || [];
|
||||
const unstagedChanges = state.workingTreeChanges || [];
|
||||
|
||||
if (stagedChanges.length > 0) {
|
||||
for (const change of stagedChanges) {
|
||||
changes.push({
|
||||
path: change.uri.fsPath,
|
||||
status: getChangeStatus(change.status),
|
||||
});
|
||||
}
|
||||
} else if (unstagedChanges.length > 0) {
|
||||
const config = vscode.workspace.getConfiguration("aiCommitExt");
|
||||
const includeUnstaged = config.get<boolean>("includeUnstaged", false);
|
||||
|
||||
if (includeUnstaged) {
|
||||
for (const change of unstagedChanges) {
|
||||
changes.push({
|
||||
path: change.uri.fsPath,
|
||||
status: getChangeStatus(change.status),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changes;
|
||||
}
|
||||
|
||||
export async function getGitDiff(): Promise<string> {
|
||||
const repository = await getActiveGitRepository();
|
||||
if (!repository) {
|
||||
throw new Error('No Git repository found');
|
||||
}
|
||||
|
||||
const state = repository.state;
|
||||
let diffOutput = '';
|
||||
|
||||
const stagedChanges = state.indexChanges || [];
|
||||
const unstagedChanges = state.workingTreeChanges || [];
|
||||
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() || '';
|
||||
diffOutput += `## ${fileName} (staged)\n`;
|
||||
|
||||
try {
|
||||
const diff = await repository.diffIndexWithHEAD(change.uri.fsPath);
|
||||
if (diff) {
|
||||
diffOutput += diff + '\n';
|
||||
}
|
||||
} catch {
|
||||
diffOutput += '(Unable to get diff)\n';
|
||||
}
|
||||
const repository = await getActiveGitRepository();
|
||||
if (!repository) {
|
||||
throw new Error("No Git repository found");
|
||||
}
|
||||
} else if (unstagedChanges.length > 0 && includeUnstaged) {
|
||||
for (const change of unstagedChanges) {
|
||||
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';
|
||||
|
||||
const state = repository.state;
|
||||
let diffOutput = "";
|
||||
|
||||
const stagedChanges = state.indexChanges || [];
|
||||
const unstagedChanges = state.workingTreeChanges || [];
|
||||
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() || "";
|
||||
diffOutput += `## ${fileName} (staged)\n`;
|
||||
|
||||
try {
|
||||
const diff = await repository.diffIndexWithHEAD(
|
||||
change.uri.fsPath,
|
||||
);
|
||||
if (diff) {
|
||||
diffOutput += diff + "\n";
|
||||
}
|
||||
} catch {
|
||||
diffOutput += "(Unable to get diff)\n";
|
||||
}
|
||||
}
|
||||
} else if (unstagedChanges.length > 0 && includeUnstaged) {
|
||||
for (const change of unstagedChanges) {
|
||||
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";
|
||||
}
|
||||
} catch {
|
||||
diffOutput += "(Unable to get diff)\n";
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
diffOutput += '(Unable to get diff)\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!diffOutput) {
|
||||
throw new Error('No changes to commit');
|
||||
}
|
||||
if (!diffOutput) {
|
||||
throw new Error("No changes to commit");
|
||||
}
|
||||
|
||||
return diffOutput;
|
||||
return diffOutput;
|
||||
}
|
||||
|
||||
async function getActiveGitRepository(): Promise<Repository | null> {
|
||||
const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git');
|
||||
if (!gitExtension) {
|
||||
throw new Error('Git extension not found');
|
||||
}
|
||||
|
||||
const api = gitExtension.exports.getAPI(1);
|
||||
if (api.repositories.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return api.repositories[0];
|
||||
const gitExtension =
|
||||
vscode.extensions.getExtension<GitExtension>("vscode.git");
|
||||
if (!gitExtension) {
|
||||
throw new Error("Git extension not found");
|
||||
}
|
||||
|
||||
const api = gitExtension.exports.getAPI(1);
|
||||
if (api.repositories.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return api.repositories[0];
|
||||
}
|
||||
|
||||
function getChangeStatus(status: number): GitChange['status'] {
|
||||
const Status = {
|
||||
INDEX_MODIFIED: 0,
|
||||
INDEX_ADDED: 1,
|
||||
INDEX_DELETED: 2,
|
||||
INDEX_RENAMED: 3,
|
||||
MODIFIED: 4,
|
||||
DELETED: 5,
|
||||
UNTRACKED: 6,
|
||||
};
|
||||
|
||||
if (status === Status.INDEX_ADDED || status === Status.UNTRACKED) {
|
||||
return 'added';
|
||||
}
|
||||
if (status === Status.INDEX_DELETED || status === Status.DELETED) {
|
||||
return 'deleted';
|
||||
}
|
||||
if (status === Status.INDEX_RENAMED) {
|
||||
return 'renamed';
|
||||
}
|
||||
|
||||
return 'modified';
|
||||
function getChangeStatus(status: number): GitChange["status"] {
|
||||
const Status = {
|
||||
INDEX_MODIFIED: 0,
|
||||
INDEX_ADDED: 1,
|
||||
INDEX_DELETED: 2,
|
||||
INDEX_RENAMED: 3,
|
||||
MODIFIED: 4,
|
||||
DELETED: 5,
|
||||
UNTRACKED: 6,
|
||||
};
|
||||
|
||||
if (status === Status.INDEX_ADDED || status === Status.UNTRACKED) {
|
||||
return "added";
|
||||
}
|
||||
if (status === Status.INDEX_DELETED || status === Status.DELETED) {
|
||||
return "deleted";
|
||||
}
|
||||
if (status === Status.INDEX_RENAMED) {
|
||||
return "renamed";
|
||||
}
|
||||
|
||||
return "modified";
|
||||
}
|
||||
|
||||
export async function getRepositoryRoot(): Promise<string | null> {
|
||||
const repository = await getActiveGitRepository();
|
||||
if (!repository) {
|
||||
return null;
|
||||
}
|
||||
if (typeof repository.root === 'string') {
|
||||
return repository.root;
|
||||
}
|
||||
return repository.rootUri.fsPath;
|
||||
}
|
||||
const repository = await getActiveGitRepository();
|
||||
if (!repository) {
|
||||
return null;
|
||||
}
|
||||
if (typeof repository.root === "string") {
|
||||
return repository.root;
|
||||
}
|
||||
return repository.rootUri.fsPath;
|
||||
}
|
||||
|
||||
+42
-5
@@ -1,11 +1,15 @@
|
||||
import { exec, spawn, ExecException } from "child_process";
|
||||
import * as vscode from "vscode";
|
||||
import { getGitDiff, getRepositoryRoot } from "./gitService";
|
||||
import { output } from "./extension";
|
||||
|
||||
export interface GenerateOptions {
|
||||
model?: string;
|
||||
userSuggestion?: string;
|
||||
}
|
||||
|
||||
let opencodeAvailableCache: boolean | null = null;
|
||||
|
||||
const DEFAULT_PROMPT = `You are a helpful assistant that generates git commit messages.
|
||||
Generate a concise Conventional Commit message (max 72 characters for the subject line).
|
||||
Format: <type>(<scope>): <description>
|
||||
@@ -15,9 +19,13 @@ Types: feat, fix, refactor, docs, style, test, chore, perf, ci, build, revert
|
||||
Only output the commit message, nothing else.`;
|
||||
|
||||
export async function isOpenCodeAvailable(): Promise<boolean> {
|
||||
if (opencodeAvailableCache !== null) {
|
||||
return opencodeAvailableCache;
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
exec("which opencode", (error: ExecException | null) => {
|
||||
resolve(!error);
|
||||
opencodeAvailableCache = !error;
|
||||
resolve(opencodeAvailableCache);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -32,21 +40,50 @@ export async function generateCommitMessage(
|
||||
);
|
||||
}
|
||||
|
||||
const diff = await getGitDiff();
|
||||
const repoRoot = await getRepositoryRoot();
|
||||
const [diff, repoRoot] = await Promise.all([
|
||||
getGitDiff(),
|
||||
getRepositoryRoot(),
|
||||
]);
|
||||
|
||||
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);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const args: string[] = ["run", "--format", "default"];
|
||||
const args: string[] = [
|
||||
"run",
|
||||
"--pure",
|
||||
"--format",
|
||||
"default",
|
||||
"-m",
|
||||
"opencode/gpt-5-nano",
|
||||
"--variant",
|
||||
"minimal",
|
||||
];
|
||||
|
||||
if (model) {
|
||||
args.push("--model", model);
|
||||
|
||||
Reference in New Issue
Block a user