Files
gai/index.ts
T

288 lines
8.1 KiB
TypeScript

#!/usr/bin/env bun
import * as readline from "node:readline";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { unlink } from "node:fs/promises";
import { loadConfig, saveConfig } from "./src/config";
import {
isGitRepo,
getRepoRoot,
getStagedFiles,
getUnstagedFiles,
getStagedDiff,
getRecentCommits,
stageFiles,
commit,
} from "./src/git";
import { selectFiles } from "./src/selector";
import { collectProjectContext } from "./src/context";
import { buildPrompt, SYSTEM_PROMPT } from "./src/prompt";
import { generateCommitMessage } from "./src/ai";
import { copyToClipboard } from "./src/clipboard";
import { BOLD, GREEN, YELLOW, CYAN, RED, DIM, RESET } from "./src/terminal";
import type { Config } from "./src/types";
const args = process.argv.slice(2);
function showHelp() {
console.log(`
${BOLD}gai${RESET} — AI-powered git commit message generator
${BOLD}Usage:${RESET}
gai Generate commit message for staged/changed files
gai --auto Auto-stage all changed files
gai --dry-run Generate message without committing
gai config Configure API settings
gai --help Show this help message
gai --version Show version
${BOLD}Configuration:${RESET}
Set via ${CYAN}gai config${RESET} or environment variables:
GAI_API_KEY OpenAI-compatible API key
GAI_API_BASE API base URL (default: https://api.deepseek.com/v1)
GAI_MODEL Model name (default: deepseek-chat)
GAI_MAX_TOKENS Max tokens (default: 500)
GAI_TEMPERATURE Temperature (default: 0.7)
`);
}
function ask(question: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
async function handleConfig() {
const config = await loadConfig();
console.log(`\n ${BOLD}Current configuration:${RESET}`);
console.log(
` API Key: ${config.apiKey ? "****" + config.apiKey.slice(-4) : `${YELLOW}(not set)${RESET}`}`,
);
console.log(` API Base: ${config.apiBase}`);
console.log(` Model: ${config.model}`);
console.log(` Max Tokens: ${config.maxTokens}`);
console.log(` Temperature: ${config.temperature}`);
console.log(
`\n ${BOLD}Enter new values (leave empty to keep current):${RESET}`,
);
const apiKey = await ask(" API Key: ");
const apiBase = await ask(` API Base [${config.apiBase}]: `);
const model = await ask(` Model [${config.model}]: `);
const maxTokens = await ask(` Max Tokens [${config.maxTokens}]: `);
const temperature = await ask(` Temperature [${config.temperature}]: `);
const updates: Partial<Config> = {};
if (apiKey) updates.apiKey = apiKey;
if (apiBase) updates.apiBase = apiBase;
if (model) updates.model = model;
if (maxTokens) updates.maxTokens = parseInt(maxTokens);
if (temperature) updates.temperature = parseFloat(temperature);
if (Object.keys(updates).length > 0) {
await saveConfig(updates);
console.log(`\n ${GREEN}Configuration saved!${RESET}`);
} else {
console.log("\n No changes.");
}
}
async function confirmCommit(message: string): Promise<"y" | "n" | "e"> {
console.log(`\n ${BOLD}Generated commit message:${RESET}`);
console.log(` ${GREEN}${message}${RESET}\n`);
const answer = await ask(` Use this message? [${GREEN}Y${RESET}/n/e] `);
const lower = answer.toLowerCase();
if (lower === "n") return "n";
if (lower === "e") return "e";
return "y";
}
async function editMessage(current: string): Promise<string | null> {
const tmpPath = join(tmpdir(), `gai-msg-${Date.now()}`);
const header = `# Edit commit message below. Save and close to confirm.\n# Delete all lines to abort.\n`;
await Bun.write(tmpPath, header + current);
const editor =
process.env.VISUAL ||
process.env.EDITOR ||
(process.platform === "darwin" ? "vi" : "nano");
const proc = Bun.spawn([editor, tmpPath], {
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
});
const exitCode = await proc.exited;
if (exitCode !== 0) {
await unlink(tmpPath).catch(() => {});
return null;
}
const content = await Bun.file(tmpPath).text();
await unlink(tmpPath).catch(() => {});
const lines = content
.split("\n")
.filter((line) => !line.startsWith("#"))
.join("\n")
.trim();
return lines || null;
}
async function main() {
if (args.includes("--help") || args.includes("-h")) {
showHelp();
return;
}
if (args.includes("--version") || args.includes("-v")) {
console.log("gai v0.1.0");
return;
}
if (args[0] === "config") {
await handleConfig();
return;
}
const autoMode = args.includes("--auto") || args.includes("-a");
const dryRun = args.includes("--dry-run") || args.includes("-d");
const config = await loadConfig();
if (!config.apiKey) {
console.error(
` ${RED}Error: API key not set. Run ${BOLD}gai config${RESET}${RED} to configure.${RESET}`,
);
process.exit(1);
}
if (!(await isGitRepo())) {
console.error(` ${RED}Error: Not a git repository.${RESET}`);
process.exit(1);
}
const stagedFiles = await getStagedFiles();
const unstagedFiles = await getUnstagedFiles();
if (stagedFiles.length === 0 && unstagedFiles.length === 0) {
console.log(" Nothing to commit.");
return;
}
if (unstagedFiles.length > 0) {
if (autoMode) {
await stageFiles(unstagedFiles.map((f) => f.path));
console.log(
` ${GREEN}Auto-staged ${unstagedFiles.length} file(s).${RESET}`,
);
} else {
const selected = await selectFiles(stagedFiles, unstagedFiles);
if (selected.length > 0) {
await stageFiles(selected);
console.log(
` ${GREEN}Staged ${selected.length} file(s).${RESET}`,
);
}
}
}
const diff = await getStagedDiff();
if (!diff) {
console.log(" No staged changes to commit.");
return;
}
const MAX_DIFF_SIZE = 15000;
const truncatedDiff =
diff.length > MAX_DIFF_SIZE
? diff.substring(0, MAX_DIFF_SIZE) + "\n... (truncated)"
: diff;
const repoRoot = await getRepoRoot();
const projectCtx = await collectProjectContext(repoRoot);
const recentCommits = await getRecentCommits(10);
const userPrompt = buildPrompt({
readme: projectCtx.readme,
packageDescription: projectCtx.packageDescription,
structure: projectCtx.structure,
recentCommits,
diff: truncatedDiff,
});
console.log("\n Generating commit message...");
let message: string;
try {
message = await generateCommitMessage(config, SYSTEM_PROMPT, userPrompt);
} catch (err) {
console.error(
` ${RED}AI request failed: ${err instanceof Error ? err.message : err}${RESET}`,
);
process.exit(1);
}
if (dryRun) {
console.log(`\n ${BOLD}Generated commit message:${RESET}`);
console.log(` ${GREEN}${message}${RESET}`);
await copyToClipboard(message);
console.log(`\n ${DIM}(dry-run, message copied to clipboard)${RESET}`);
return;
}
const action = await confirmCommit(message);
if (action === "y") {
try {
await commit(message);
console.log(`\n ${GREEN}Committed successfully!${RESET}`);
} catch (err) {
console.error(
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
);
process.exit(1);
}
} else if (action === "e") {
const edited = await editMessage(message);
if (edited) {
try {
await commit(edited);
console.log(`\n ${GREEN}Committed successfully!${RESET}`);
} catch (err) {
console.error(
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
);
process.exit(1);
}
} else {
console.log(" Aborted.");
}
} else {
const copied = await copyToClipboard(message);
console.log(
copied
? ` Aborted. Message copied to clipboard.`
: ` Aborted. Message: ${message}`,
);
}
}
main().catch((err) => {
console.error(` ${RED}Unexpected error: ${err}${RESET}`);
process.exit(1);
});