feat: initial implementation of AI Commit Ext VS Code extension
- Add VS Code extension with OpenCode AI integration - Generate Conventional Commit messages via SCM header button - Include gitService for staged changes detection - Include opencodeService for CLI invocation - Add configuration options for model, includeUnstaged, notifications - Add button disable during generation with progress indicator - Add sparkle icon for the action button - Include package.json, tsconfig, and basic project setup
This commit is contained in:
116
src/extension.ts
Normal file
116
src/extension.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import * as vscode from "vscode";
|
||||
import { generateCommitMessage, isOpenCodeAvailable } from "./opencodeService";
|
||||
|
||||
interface GitExtension {
|
||||
getAPI(version: number): GitAPI;
|
||||
}
|
||||
|
||||
interface GitAPI {
|
||||
repositories: Repository[];
|
||||
}
|
||||
|
||||
interface Repository {
|
||||
root: string;
|
||||
rootUri: vscode.Uri;
|
||||
state: RepositoryState;
|
||||
inputBox: { value: string };
|
||||
diffIndexWithHEAD(path?: string): Promise<string>;
|
||||
diffWithHEAD(path?: string): Promise<string>;
|
||||
status(): Promise<void>;
|
||||
}
|
||||
|
||||
interface RepositoryState {
|
||||
indexChanges: any[];
|
||||
workingTreeChanges: any[];
|
||||
}
|
||||
|
||||
export async function activate(
|
||||
context: vscode.ExtensionContext,
|
||||
): Promise<void> {
|
||||
const commandHandler = vscode.commands.registerCommand(
|
||||
"aiCommitExt.generate",
|
||||
async () => {
|
||||
await handleGenerateCommitMessage();
|
||||
},
|
||||
);
|
||||
context.subscriptions.push(commandHandler);
|
||||
}
|
||||
|
||||
async function handleGenerateCommitMessage(): Promise<void> {
|
||||
const config = vscode.workspace.getConfiguration("aiCommitExt");
|
||||
const showNotification = config.get<boolean>("showNotification", true);
|
||||
|
||||
try {
|
||||
const opencodeAvailable = await isOpenCodeAvailable();
|
||||
if (!opencodeAvailable) {
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage(
|
||||
"OpenCode is not installed. Please install from https://opencode.ai",
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const gitExtension =
|
||||
vscode.extensions.getExtension<GitExtension>("vscode.git");
|
||||
if (!gitExtension) {
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage("Git extension not found");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const gitApi = gitExtension.exports.getAPI(1);
|
||||
const repositories = gitApi.repositories;
|
||||
|
||||
if (repositories.length === 0) {
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage("No Git repository found");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const repository = repositories[0];
|
||||
|
||||
await repository.status();
|
||||
|
||||
const state = repository.state;
|
||||
|
||||
const stagedChanges = state.indexChanges || [];
|
||||
const unstagedChanges = state.workingTreeChanges || [];
|
||||
|
||||
if (stagedChanges.length === 0 && unstagedChanges.length === 0) {
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage("No changes to commit");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const commitMessage = await vscode.window.withProgress(
|
||||
{
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
cancellable: false,
|
||||
title: "Generating commit message...",
|
||||
},
|
||||
async () => {
|
||||
return await generateCommitMessage();
|
||||
},
|
||||
);
|
||||
|
||||
repository.inputBox.value = commitMessage;
|
||||
|
||||
if (showNotification) {
|
||||
vscode.window.showInformationMessage("Commit message generated");
|
||||
}
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error";
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage(
|
||||
`Failed to generate commit message: ${message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function deactivate(): void {}
|
||||
170
src/gitService.ts
Normal file
170
src/gitService.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
interface GitExtension {
|
||||
getAPI(version: number): GitAPI;
|
||||
}
|
||||
|
||||
interface GitAPI {
|
||||
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>;
|
||||
}
|
||||
|
||||
interface RepositoryState {
|
||||
indexChanges: Change[];
|
||||
workingTreeChanges: Change[];
|
||||
}
|
||||
|
||||
interface Change {
|
||||
uri: vscode.Uri;
|
||||
status: number;
|
||||
}
|
||||
|
||||
export interface GitChange {
|
||||
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),
|
||||
});
|
||||
}
|
||||
} 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';
|
||||
}
|
||||
}
|
||||
} 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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!diffOutput) {
|
||||
throw new Error('No changes to commit');
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
128
src/opencodeService.ts
Normal file
128
src/opencodeService.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { exec, spawn, ExecException } from "child_process";
|
||||
import * as vscode from "vscode";
|
||||
import { getGitDiff, getRepositoryRoot } from "./gitService";
|
||||
|
||||
export interface GenerateOptions {
|
||||
model?: string;
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
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> {
|
||||
return new Promise((resolve) => {
|
||||
exec("which opencode", (error: ExecException | null) => {
|
||||
resolve(!error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function generateCommitMessage(
|
||||
options: GenerateOptions = {},
|
||||
): Promise<string> {
|
||||
const opencodeAvailable = await isOpenCodeAvailable();
|
||||
if (!opencodeAvailable) {
|
||||
throw new Error(
|
||||
"OpenCode is not installed. Please install it from https://opencode.ai",
|
||||
);
|
||||
}
|
||||
|
||||
const diff = await getGitDiff();
|
||||
const repoRoot = await getRepositoryRoot();
|
||||
|
||||
const config = vscode.workspace.getConfiguration("aiCommitExt");
|
||||
const model = options.model || config.get<string>("model", "");
|
||||
|
||||
const prompt = `${DEFAULT_PROMPT}
|
||||
|
||||
Here are the git changes:
|
||||
${diff}
|
||||
|
||||
Generate a concise Conventional Commit message for these changes:`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const args: string[] = ["run", "--format", "default"];
|
||||
|
||||
if (model) {
|
||||
args.push("--model", model);
|
||||
}
|
||||
|
||||
const proc = spawn("opencode", args, {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
shell: false,
|
||||
cwd: repoRoot || undefined,
|
||||
});
|
||||
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
|
||||
proc.stdout.on("data", (data: Buffer) => {
|
||||
stdout += data.toString();
|
||||
});
|
||||
proc.stderr.on("data", (data: Buffer) => {
|
||||
stderr += data.toString();
|
||||
});
|
||||
proc.stdin?.write(prompt + "\n", (err) => {
|
||||
if (err) {
|
||||
new Error(`OpenCode write error with ${err}`);
|
||||
}
|
||||
proc.stdin?.end();
|
||||
});
|
||||
|
||||
proc.on("close", (code: number | null) => {
|
||||
if (code !== 0) {
|
||||
reject(
|
||||
new Error(`OpenCode exited with code ${code}: ${stderr}`),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const message = parseCommitMessage(stdout);
|
||||
resolve(message);
|
||||
});
|
||||
|
||||
proc.on("error", (err: Error) => {
|
||||
reject(new Error(`Failed to run opencode: ${err.message}`));
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
proc.kill();
|
||||
reject(new Error("OpenCode timed out"));
|
||||
}, 120000);
|
||||
});
|
||||
}
|
||||
|
||||
function parseCommitMessage(output: string): string {
|
||||
const lines = output.split("\n").filter((line) => line.trim());
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
|
||||
if (
|
||||
trimmed.includes(":") &&
|
||||
(trimmed.startsWith("feat") ||
|
||||
trimmed.startsWith("fix") ||
|
||||
trimmed.startsWith("refactor") ||
|
||||
trimmed.startsWith("docs") ||
|
||||
trimmed.startsWith("style") ||
|
||||
trimmed.startsWith("test") ||
|
||||
trimmed.startsWith("chore") ||
|
||||
trimmed.startsWith("perf") ||
|
||||
trimmed.startsWith("ci") ||
|
||||
trimmed.startsWith("build") ||
|
||||
trimmed.startsWith("revert"))
|
||||
) {
|
||||
return trimmed.substring(0, 200);
|
||||
}
|
||||
}
|
||||
|
||||
const cleaned =
|
||||
output.trim().split("\n")[0]?.substring(0, 200) ||
|
||||
"chore: generated commit message";
|
||||
return cleaned;
|
||||
}
|
||||
Reference in New Issue
Block a user