27 Commits

Author SHA1 Message Date
Mplan d0506381f5 refactor(cli): remove interactive prompts for empty states and add clean repo test
Build / bun-build (push) Successful in 29s
Build / bun-build (pull_request) Successful in 37s
2026-06-12 00:42:49 +08:00
Mplan 14df49b110 refactor(cli): replace process.exit prompts with interactive selection for empty states 2026-06-12 00:36:01 +08:00
Mplan 962b76d20f refactor(cli): show empty state prompt in current page
Empty commit/PR states no longer auto-return to the menu. Instead, the
user is shown an in-page prompt with a Back option, mirroring the rest
of the CLI: pressing Enter on Back (or ←/backspace) closes the prompt
and returns control to the previous step.
2026-06-12 00:35:23 +08:00
Mplan 12e71a0af7 feat(config): add interactive editor with inline editing and navigation
Build / bun-build (push) Successful in 9m25s
2026-06-11 21:18:34 +08:00
Mplan e1354e8651 feat(menu): add back key navigation
Build / bun-build (push) Successful in 21s
2026-06-11 20:10:34 +08:00
Mplan 7e662b25cc refactor(pr): rely on selected CLI for PR creation 2026-06-11 19:43:19 +08:00
Mplan 5bb2dc8e8a chore(config): bump version to 0.1.2 2026-06-11 19:39:50 +08:00
Mplan 1dbfac7985 feat(pr): add GitLab support 2026-06-11 19:38:36 +08:00
Mplan c8626ff69a ci(gitea): add build workflow for Gitea CI
Build / bun-build (push) Successful in 1m2s
2026-06-11 18:56:08 +08:00
Mplan 7e9cab5805 docs(readme): add PR creation details and update badges 2026-06-11 18:51:44 +08:00
Mplan 9885096229 feat(cli): add branch push check before PR creation 2026-06-11 18:24:53 +08:00
Mplan 1ea180387c refactor(cli): extract interactive menu into reusable module (#3)
Extract duplicate menu rendering logic from `index.ts` into a new `src/menu.ts` module, providing generic `selectOne` and `selectMany` functions. This reduces code duplication, improves maintainability, and adds consistent UI controls display across the commit flow and platform selection.

Reviewed-on: #3
2026-06-11 18:12:12 +08:00
Mplan 6ff541284e feat(cli): add pull request creation with AI-generated messages (#2)
Add a new `gai pr` subcommand that generates pull request titles and descriptions using AI, then creates the PR via GitHub CLI (`gh`) or Gitea CLI (`tea`). This extends the existing commit-generation system by reusing retry logic and prompt infrastructure, and introduces a `callAI` function that returns raw output (instead of pre-cleaned messages) to support structured PR responses.

Reviewed-on: #2
2026-06-11 00:39:20 +08:00
Mplan 76a5bac11a chore(config): ignore bun.lock 2026-06-11 00:01:40 +08:00
Mplan 0bfde59725 chore(config): rename project to gai 2026-06-10 23:57:25 +08:00
Mplan 890a192157 feat(cli): add help subcommand to show usage information 2026-06-09 18:52:03 +08:00
Mplan a4e0b6f747 feat: implement gai CLI for AI-generated Conventional Commits (#1)
Reviewed-on: #1
2026-06-09 18:21:23 +08:00
Mplan c2e60db196 chore(config): update default model to deepseek-v4-flash 2026-06-09 17:43:00 +08:00
Mplan 647d1096ba refactor(cli): extract commit result display and extend commit return with stats 2026-06-09 17:38:23 +08:00
Mplan 6d007698f9 feat(cli): show commit hash and summary on success 2026-06-09 17:38:00 +08:00
Mplan 68e98be653 refactor(cli): replace external editor with inline terminal editing 2026-06-09 17:31:52 +08:00
Mplan 37916f6c49 feat(cli): use external editor for commit message editing 2026-06-09 17:24:07 +08:00
Mplan 8be9f51532 docs: add comprehensive project documentation 2026-06-09 17:22:03 +08:00
Mplan 455c5d9b41 docs: add MIT license 2026-06-09 17:20:53 +08:00
Mplan de96c8862e feat: add retry logic to AI client and interactive unstaged file selector 2026-06-09 17:13:34 +08:00
Mplan 9f33d0f2ed chore: update default API and model to DeepSeek 2026-06-09 17:09:32 +08:00
Mplan 401f6ccbe0 test: add unit tests for config and prompt modules 2026-06-09 17:09:06 +08:00
20 changed files with 2049 additions and 181 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
# gai configuration # gai configuration
GAI_API_KEY=sk-your-api-key-here GAI_API_KEY=sk-your-api-key-here
GAI_API_BASE=https://api.openai.com/v1 GAI_API_BASE=https://api.deepseek.com/v1
GAI_MODEL=gpt-4o GAI_MODEL=deepseek-v4-flash
GAI_MAX_TOKENS=500 GAI_MAX_TOKENS=500
GAI_TEMPERATURE=0.7 GAI_TEMPERATURE=0.7
+25
View File
@@ -0,0 +1,25 @@
name: Build
on:
push:
pull_request:
jobs:
bun-build:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: bun install
- name: Run tests
run: bun test
- name: Build binary
run: bun run build
+3
View File
@@ -10,6 +10,9 @@ dist
coverage coverage
*.lcov *.lcov
# lock
bun.lock
# logs # logs
logs logs
_.log _.log
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 mplan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+215 -5
View File
@@ -1,15 +1,225 @@
# git-ai <div align="center">
To install dependencies: # gai
**AI-powered Git commit and pull request helper**
[![Release](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fgit.catpl.top%2Fapi%2Fv1%2Frepos%2FMplan%2Fgai%2Freleases%2Flatest&query=%24.tag_name&label=release&color=blue)](https://git.catpl.top/Mplan/gai/releases)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)
[![Bun](https://img.shields.io/badge/runtime-Bun-f9d71c.svg)](https://bun.sh)
[![TypeScript](https://img.shields.io/badge/lang-TypeScript-3178c6.svg)](https://www.typescriptlang.org/)
Generate **Conventional Commits** messages and pull request descriptions using AI, based on your project context, code diff, branch changes, and commit history.
</div>
---
## Features
- **Interactive menu** — `gai` opens a menu, select actions with ↑/↓ + space/enter
- **Context-aware commits** — reads project overview, staged diff, and recent commit history
- **Conventional Commits** — `feat(scope): description` format by default
- **Interactive file selection** — ↑/↓ to navigate, space to select, top-level "Select all"
- **Inline editing** — edit AI-generated messages right in the terminal with cursor movement
- **AI-generated PRs** — create GitHub, Gitea, or GitLab pull requests with generated title and body
- **OpenAI-compatible API** — works with DeepSeek, OpenAI, Ollama, OpenRouter, and more
- **Review before commit** — confirm, edit, or abort the generated message
- **Bun-native runtime** — built on Bun APIs with no runtime npm dependencies
## Quick Start
```bash ```bash
# Install dependencies
bun install bun install
# Configure your API key
gai config
# Open interactive menu
gai
# Or directly generate a commit message
gai commit
# Or create an AI-generated pull request
gai pr
``` ```
To run: ## Usage
```
gai Open interactive menu
gai commit Generate commit message (interactive file selection)
gai commit --auto Auto-stage all changed files
gai commit -d Generate message without committing
gai pr Create a PR with AI-generated title and body
gai pr --draft Create a draft PR
gai config Configure API settings
gai --help Show help
gai --version Show version
```
### Interactive Menu
```
$ gai
gai
Choose a workflow
↑/↓ navigate · enter/space select · ctrl+c cancel
● commit Generate AI commit message
○ pr Create a PR with AI-generated title
○ config Configure API settings
```
### Commit Flow
```
$ gai commit
Staged files (will be included):
✓ src/git.ts (modified)
Select files to stage:
2 unstaged files available
↑/↓ navigate · space toggle · enter confirm · ←/backspace back · ctrl+c cancel
□ Select all
□ src/ai.ts modified
■ src/newfile.ts new
Generating commit message...
Generated commit message:
feat(git): add interactive file staging and commit wrapper
Use this message? [Y/n/e] Y
✔ Committed successfully!
[main a3f7c2b] feat(git): add interactive file staging and commit wrapper
1 file changed, 45 insertions(+), 12 deletions(-)
```
### Pull Request Flow
`gai pr` detects the remote platform from `origin`:
- GitHub remotes use the `gh` CLI
- Gitea remotes use the `tea` CLI
- GitLab remotes use the `glab` CLI
- Unknown remotes prompt you to choose a platform
The command compares your current branch against the default branch, pushes the branch if needed, generates a PR title/body from the branch commits and diff, then asks for confirmation before creating the PR.
```bash ```bash
bun run index.ts gai pr
gai pr --draft
``` ```
This project was created using `bun init` in bun v1.3.14. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime. ## Configuration
### Via `gai config` (interactive)
```bash
gai config
```
### Via environment variables
| Variable | Default | Description |
|---|---|---|
| `GAI_API_KEY` | — | **Required.** Your API key |
| `GAI_API_BASE` | `https://api.deepseek.com/v1` | API base URL |
| `GAI_MODEL` | `deepseek-v4-flash` | Model name |
| `GAI_MAX_TOKENS` | `500` | Max response tokens |
| `GAI_TEMPERATURE` | `0.7` | Sampling temperature |
### Via `.env` file
Bun auto-loads `.env` — no dotenv needed:
```bash
GAI_API_KEY=sk-your-key
GAI_API_BASE=https://api.deepseek.com/v1
GAI_MODEL=deepseek-v4-flash
```
### Using other providers
<details>
<summary><strong>OpenAI</strong></summary>
```bash
GAI_API_KEY=sk-xxx
GAI_API_BASE=https://api.openai.com/v1
GAI_MODEL=gpt-4o
```
</details>
<details>
<summary><strong>Ollama (local)</strong></summary>
```bash
GAI_API_KEY=ollama
GAI_API_BASE=http://localhost:11434/v1
GAI_MODEL=llama3
```
</details>
<details>
<summary><strong>OpenRouter</strong></summary>
```bash
GAI_API_KEY=sk-or-xxx
GAI_API_BASE=https://openrouter.ai/api/v1
GAI_MODEL=anthropic/claude-sonnet-4
```
</details>
## How It Works
```
┌─────────────────────────────────────────────┐
│ gai │
├─────────────────────────────────────────────┤
│ 1. Collect project context │
│ ├─ README.md / package.json │
│ └─ Directory structure │
│ │
│ 2. Collect code changes │
│ ├─ git diff --staged for commits │
│ └─ branch diff for pull requests │
│ │
│ 3. Collect commit history │
│ ├─ git log --oneline -10 for commits │
│ └─ branch commits for pull requests │
│ │
│ 4. Build prompt → Call AI API │
│ │
│ 5. Review → Confirm → Commit or Create PR │
└─────────────────────────────────────────────┘
```
## Build
Compile to a standalone binary:
```bash
bun run build
# Output: ./gai
```
## Test
```bash
bun test
```
## License
[MIT](./LICENSE)
-26
View File
@@ -1,26 +0,0 @@
{
"lockfileVersion": 1,
"configVersion": 1,
"workspaces": {
"": {
"name": "git-ai",
"devDependencies": {
"@types/bun": "latest",
},
"peerDependencies": {
"typescript": "^5",
},
},
},
"packages": {
"@types/bun": ["@types/bun@1.3.14", "https://registry.npmmirror.com/@types/bun/-/bun-1.3.14.tgz", { "dependencies": { "bun-types": "1.3.14" } }, "sha512-h1hFqFVcvAvD9j9K7ZW7vd82aSA+rTdznZa+5bwvCwqSB1jmmfLcbIWhOLx1/+boy/xmjgCs/OMUL8hRJSmnPw=="],
"@types/node": ["@types/node@25.9.2", "https://registry.npmmirror.com/@types/node/-/node-25.9.2.tgz", { "dependencies": { "undici-types": ">=7.24.0 <7.24.7" } }, "sha512-G05zqtJhcDLb8uslf5EjCxXg9G1KQxiV8OS0R26IC//Eoyitzqe8z37I7cqvnZlrlSfgocQRfSn/AHBZJJFyGw=="],
"bun-types": ["bun-types@1.3.14", "https://registry.npmmirror.com/bun-types/-/bun-types-1.3.14.tgz", { "dependencies": { "@types/node": "*" } }, "sha512-4N0ig0fEomHt5R0KCFWjovxow98rIoRwKolrYdCcknNwMekCXRnWEUvgu5soYV8QXtVsrUD8B95MBOZGPvr6KQ=="],
"typescript": ["typescript@5.9.3", "https://registry.npmmirror.com/typescript/-/typescript-5.9.3.tgz", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
"undici-types": ["undici-types@7.24.6", "https://registry.npmmirror.com/undici-types/-/undici-types-7.24.6.tgz", {}, "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg=="],
}
}
+806 -67
View File
@@ -13,12 +13,28 @@ import {
commit, commit,
} from "./src/git"; } from "./src/git";
import { selectFiles } from "./src/selector"; import { selectFiles } from "./src/selector";
import { BACK, selectOne } from "./src/menu";
import type { PromptBack } from "./src/menu";
import { collectProjectContext } from "./src/context"; import { collectProjectContext } from "./src/context";
import { buildPrompt, SYSTEM_PROMPT } from "./src/prompt"; import { buildPrompt, SYSTEM_PROMPT } from "./src/prompt";
import { generateCommitMessage } from "./src/ai"; import { generateCommitMessage } from "./src/ai";
import { copyToClipboard } from "./src/clipboard"; import { copyToClipboard } from "./src/clipboard";
import { BOLD, GREEN, YELLOW, CYAN, RED, DIM, RESET } from "./src/terminal"; import { BOLD, GREEN, YELLOW, CYAN, RED, DIM, RESET } from "./src/terminal";
import type { Config } from "./src/types"; import type { Config } from "./src/types";
import {
getDefaultBranch,
getBranchName,
getBranchPushStatus,
pushCurrentBranch,
getBranchCommits,
getBranchDiff,
detectPlatform,
getRemoteHostname,
createPR,
} from "./src/pr";
import type { Platform } from "./src/pr";
import { PR_SYSTEM_PROMPT, buildPRPrompt } from "./src/prompt";
import { generatePRMessage } from "./src/ai";
const args = process.argv.slice(2); const args = process.argv.slice(2);
@@ -27,9 +43,12 @@ function showHelp() {
${BOLD}gai${RESET} — AI-powered git commit message generator ${BOLD}gai${RESET} — AI-powered git commit message generator
${BOLD}Usage:${RESET} ${BOLD}Usage:${RESET}
gai Generate commit message for staged/changed files gai Open interactive menu
gai --auto Auto-stage all changed files gai commit Generate commit message for staged/changed files
gai --dry-run Generate message without committing gai commit --auto Auto-stage all changed files
gai commit -d Generate message without committing
gai pr Create a PR with AI-generated title and body
gai pr --draft Create a draft PR
gai config Configure API settings gai config Configure API settings
gai --help Show this help message gai --help Show this help message
gai --version Show version gai --version Show version
@@ -37,8 +56,8 @@ ${BOLD}Usage:${RESET}
${BOLD}Configuration:${RESET} ${BOLD}Configuration:${RESET}
Set via ${CYAN}gai config${RESET} or environment variables: Set via ${CYAN}gai config${RESET} or environment variables:
GAI_API_KEY OpenAI-compatible API key GAI_API_KEY OpenAI-compatible API key
GAI_API_BASE API base URL (default: https://api.openai.com/v1) GAI_API_BASE API base URL (default: https://api.deepseek.com/v1)
GAI_MODEL Model name (default: gpt-4o) GAI_MODEL Model name (default: deepseek-chat)
GAI_MAX_TOKENS Max tokens (default: 500) GAI_MAX_TOKENS Max tokens (default: 500)
GAI_TEMPERATURE Temperature (default: 0.7) GAI_TEMPERATURE Temperature (default: 0.7)
`); `);
@@ -57,41 +76,333 @@ function ask(question: string): Promise<string> {
}); });
} }
async function handleConfig() { type ConfigKey = keyof Config;
const config = await loadConfig();
console.log(`\n ${BOLD}Current configuration:${RESET}`); interface ConfigField {
console.log( key: ConfigKey;
` API Key: ${config.apiKey ? "****" + config.apiKey.slice(-4) : `${YELLOW}(not set)${RESET}`}`, label: string;
); format: (config: Config) => string;
console.log(` API Base: ${config.apiBase}`); initialEditValue: (config: Config) => string;
console.log(` Model: ${config.model}`); parse: (value: string) => { value: Config[ConfigKey] } | { error: string };
console.log(` Max Tokens: ${config.maxTokens}`); }
console.log(` Temperature: ${config.temperature}`);
console.log( const CONFIG_FIELDS: ConfigField[] = [
`\n ${BOLD}Enter new values (leave empty to keep current):${RESET}`, {
); key: "apiKey",
label: "API Key",
format: (config) =>
config.apiKey ? `****${config.apiKey.slice(-4)}` : `${YELLOW}(not set)${RESET}`,
initialEditValue: () => "",
parse: (value) => ({ value }),
},
{
key: "apiBase",
label: "API Base",
format: (config) => config.apiBase,
initialEditValue: (config) => config.apiBase,
parse: (value) => ({ value }),
},
{
key: "model",
label: "Model",
format: (config) => config.model,
initialEditValue: (config) => config.model,
parse: (value) => ({ value }),
},
{
key: "maxTokens",
label: "Max Tokens",
format: (config) => String(config.maxTokens),
initialEditValue: (config) => String(config.maxTokens),
parse: (value) => {
const parsed = Number(value);
if (!Number.isInteger(parsed) || parsed <= 0) {
return { error: "Max Tokens must be a positive integer." };
}
return { value: parsed };
},
},
{
key: "temperature",
label: "Temperature",
format: (config) => String(config.temperature),
initialEditValue: (config) => String(config.temperature),
parse: (value) => {
const parsed = Number(value);
if (!Number.isFinite(parsed)) {
return { error: "Temperature must be a finite number." };
}
return { value: parsed };
},
},
];
const apiKey = await ask(" API Key: "); function visibleLength(value: string) {
const apiBase = await ask(` API Base [${config.apiBase}]: `); return value.replace(/\x1b\[[0-9;]*m/g, "").length;
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> = {}; function clearLine() {
if (apiKey) updates.apiKey = apiKey; process.stdout.write("\r\x1b[2K");
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) { function moveUp(lines: number) {
await saveConfig(updates); if (lines > 0) process.stdout.write(`\x1b[${lines}A`);
console.log(`\n ${GREEN}Configuration saved!${RESET}`); }
} else {
console.log("\n No changes."); function renderConfigPage(
config: Config,
cursor: number,
previousLines: number,
status: string | null,
editState: { buffer: string; cursor: number } | null,
) {
if (previousLines > 0) {
for (let i = 0; i < previousLines; i++) {
clearLine();
process.stdout.write("\n");
}
moveUp(previousLines);
} }
const labelWidth = Math.max(...CONFIG_FIELDS.map((field) => field.label.length)) + 2;
const lines = [
"",
` ${BOLD}Configuration${RESET}`,
editState
? ` ${DIM}editing · enter save · esc cancel · ctrl+c cancel${RESET}`
: ` ${DIM}↑/↓ navigate · space edit · ←/backspace back · ctrl+c cancel${RESET}`,
"",
];
let activeValueOffset = 0;
for (let i = 0; i < CONFIG_FIELDS.length; i++) {
const field = CONFIG_FIELDS[i]!;
const active = i === cursor;
const pointer = active ? `${CYAN}${RESET}` : " ";
const marker = active ? `${GREEN}${RESET}` : `${DIM}${RESET}`;
const label = active ? `${BOLD}${field.label}${RESET}` : field.label;
const padding = " ".repeat(Math.max(1, labelWidth - visibleLength(field.label)));
const value = active && editState ? editState.buffer : field.format(config);
if (active && editState) {
activeValueOffset = visibleLength(` ${pointer} ${marker} ${label}${padding}`);
}
lines.push(` ${pointer} ${marker} ${label}${padding}${value}`);
}
if (status) {
lines.push("", ` ${status}`);
}
for (const line of lines) {
process.stdout.write(`${line}\n`);
}
if (editState) {
moveUp(lines.length - (4 + cursor));
const column = activeValueOffset + editState.cursor;
process.stdout.write(`\r${column > 0 ? `\x1b[${column}C` : ""}`);
} else {
moveUp(lines.length);
}
return lines.length;
}
async function handleConfig(): Promise<"done" | "back"> {
if (process.stdin.isTTY !== true) {
console.error(` ${RED}Error: Configuration requires a TTY.${RESET}`);
process.exit(1);
}
let config = await loadConfig();
let cursor = 0;
let renderedLines = 0;
let escapeBuf = "";
let status: string | null = null;
let editState: { buffer: string; cursor: number } | null = null;
let renderedCursorRow = 0;
const wasRaw = process.stdin.isRaw;
if (wasRaw !== true) process.stdin.setRawMode(true);
process.stdin.resume();
process.stdout.write("\x1b[?25l");
const render = () => {
moveUp(renderedCursorRow);
renderedLines = renderConfigPage(config, cursor, renderedLines, status, editState);
renderedCursorRow = editState ? 4 + cursor : 0;
process.stdout.write(editState ? "\x1b[?25h" : "\x1b[?25l");
};
render();
return new Promise((resolve) => {
const finish = (value: "done" | "back") => {
process.stdin.setRawMode(wasRaw === true);
process.stdin.pause();
process.stdin.removeListener("data", onData);
moveUp(renderedCursorRow);
for (let i = 0; i < renderedLines; i++) {
clearLine();
process.stdout.write("\n");
}
moveUp(renderedLines);
process.stdout.write("\x1b[?25h");
resolve(value);
};
const saveEdit = async () => {
if (!editState) return;
const field = CONFIG_FIELDS[cursor]!;
const value = editState.buffer.trim();
editState = null;
if (value === "") {
status = `${DIM}No changes.${RESET}`;
} else {
const parsed = field.parse(value);
if ("error" in parsed) {
status = `${RED}${parsed.error}${RESET}`;
} else {
await saveConfig({ [field.key]: parsed.value } as Partial<Config>);
config = await loadConfig();
status = `${GREEN}${field.label} saved.${RESET}`;
}
}
render();
};
const onData = (data: Buffer) => {
const key = data.toString();
const UP = "\x1b[A";
const DOWN = "\x1b[B";
const LEFT = "\x1b[D";
const ALT_UP = "\x1bOA";
const ALT_DOWN = "\x1bOB";
const ALT_LEFT = "\x1bOD";
const SPACE = " ";
const ENTER = "\r";
const ESC = "\x1b";
const RIGHT = "\x1b[C";
const ALT_RIGHT = "\x1bOC";
const CTRL_C = "\x03";
const BACKSPACE = "\x7f";
if (editState) {
if (key === CTRL_C || key === ESC) {
editState = null;
status = `${DIM}No changes.${RESET}`;
render();
return;
}
if (key === ENTER) {
void saveEdit();
return;
}
if (key === "\x01") {
editState.cursor = 0;
render();
return;
}
if (key === "\x05") {
editState.cursor = editState.buffer.length;
render();
return;
}
if (key === "\x0b") {
editState.buffer = editState.buffer.slice(0, editState.cursor);
render();
return;
}
if (key === "\x15") {
editState.buffer = editState.buffer.slice(editState.cursor);
editState.cursor = 0;
render();
return;
}
if (key === BACKSPACE) {
if (editState.cursor > 0) {
editState.buffer =
editState.buffer.slice(0, editState.cursor - 1) +
editState.buffer.slice(editState.cursor);
editState.cursor--;
render();
}
return;
}
if (key === LEFT || key === ALT_LEFT) {
if (editState.cursor > 0) editState.cursor--;
render();
return;
}
if (key === RIGHT || key === ALT_RIGHT) {
if (editState.cursor < editState.buffer.length) editState.cursor++;
render();
return;
}
if (key.startsWith("\x1b[")) {
if (key === "\x1b[H" || key === "\x1b[1~") {
editState.cursor = 0;
} else if (key === "\x1b[F" || key === "\x1b[4~") {
editState.cursor = editState.buffer.length;
} else if (key === "\x1b[3~" && editState.cursor < editState.buffer.length) {
editState.buffer =
editState.buffer.slice(0, editState.cursor) +
editState.buffer.slice(editState.cursor + 1);
}
render();
return;
}
if (key >= " " && key !== "\x7f") {
editState.buffer =
editState.buffer.slice(0, editState.cursor) +
key +
editState.buffer.slice(editState.cursor);
editState.cursor += key.length;
render();
}
return;
}
const action = (() => {
if (key === UP || key === ALT_UP) return "up";
if (key === DOWN || key === ALT_DOWN) return "down";
if (key === LEFT || key === ALT_LEFT || key === BACKSPACE) return "back";
if (key === SPACE) return "edit";
if (key === CTRL_C) return "cancel";
if (key === "\x1b" || key.startsWith("\x1b[")) {
escapeBuf = key;
return null;
}
if (escapeBuf) {
const next = escapeBuf + key;
escapeBuf = /^[A-Za-z~]$/.test(key) || next.length > 8 ? "" : next;
if (next === UP || next === ALT_UP) return "up";
if (next === DOWN || next === ALT_DOWN) return "down";
if (next === LEFT || next === ALT_LEFT) return "back";
}
return null;
})();
if (action === "cancel") return finish("done");
if (action === "back") return finish("back");
if (action === "up" && cursor > 0) {
cursor--;
status = null;
render();
} else if (action === "down" && cursor < CONFIG_FIELDS.length - 1) {
cursor++;
status = null;
render();
} else if (action === "edit") {
const value = CONFIG_FIELDS[cursor]!.initialEditValue(config);
editState = { buffer: value, cursor: value.length };
status = null;
render();
}
};
process.stdin.on("data", onData);
});
} }
async function confirmCommit(message: string): Promise<"y" | "n" | "e"> { async function confirmCommit(message: string): Promise<"y" | "n" | "e"> {
@@ -105,40 +416,235 @@ async function confirmCommit(message: string): Promise<"y" | "n" | "e"> {
} }
async function editMessage(current: string): Promise<string | null> { async function editMessage(current: string): Promise<string | null> {
console.log(` Current: ${DIM}${current}${RESET}`); if (!process.stdin.isTTY) return null;
console.log(` Enter new message (empty to abort):`);
const newMsg = await ask(" > "); process.stdout.write(` ${DIM}Edit message (Enter to confirm, Esc to abort):${RESET}\n`);
return newMsg || null;
const savedRaw = process.stdin.isRaw;
process.stdin.setRawMode(true);
process.stdin.resume();
let buffer = current;
let cursor = current.length;
const ESC = "\x1b";
const ENTER = "\r";
const CTRL_C = "\x03";
const BACKSPACE = "\x7f";
function render() {
process.stdout.write("\x1b[2K\r > " + buffer);
if (cursor < buffer.length) {
process.stdout.write(`\x1b[${buffer.length - cursor}D`);
}
}
process.stdout.write(" > ");
process.stdout.write(buffer);
return new Promise((resolve) => {
let escapeBuf = "";
function handleSeq(seq: string) {
if (seq === "\x1b[D" || seq === "\x1bOD") {
if (cursor > 0) {
cursor--;
process.stdout.write("\x1b[D");
}
} else if (seq === "\x1b[C" || seq === "\x1bOC") {
if (cursor < buffer.length) {
cursor++;
process.stdout.write("\x1b[C");
}
} else if (seq === "\x1b[H" || seq === "\x1b[1~" || seq === "\x1bOH") {
if (cursor > 0) {
process.stdout.write(`\x1b[${cursor}D`);
cursor = 0;
}
} else if (seq === "\x1b[F" || seq === "\x1b[4~" || seq === "\x1bOF") {
if (cursor < buffer.length) {
process.stdout.write(`\x1b[${buffer.length - cursor}C`);
cursor = buffer.length;
}
} else if (seq === "\x1b[3~") {
if (cursor < buffer.length) {
buffer = buffer.slice(0, cursor) + buffer.slice(cursor + 1);
render();
}
}
}
process.stdin.on("data", (data: Buffer) => {
const key = data.toString();
if (key === CTRL_C) {
process.stdin.setRawMode(savedRaw === true);
process.stdin.pause();
process.stdin.removeAllListeners("data");
process.stdout.write("\n");
resolve(null);
return;
}
if (key === ESC || key.startsWith("\x1b[")) {
escapeBuf = key;
if (key.length >= 3) {
handleSeq(key);
escapeBuf = "";
}
return;
}
if (escapeBuf) {
escapeBuf += key;
if (/^[A-Za-z~]$/.test(key)) {
handleSeq(escapeBuf);
escapeBuf = "";
} else if (escapeBuf.length > 8) {
escapeBuf = "";
}
return;
}
if (key === ENTER) {
process.stdin.setRawMode(savedRaw === true);
process.stdin.pause();
process.stdin.removeAllListeners("data");
process.stdout.write("\n");
const result = buffer.trim();
resolve(result || null);
return;
}
if (key === BACKSPACE) {
if (cursor > 0) {
buffer = buffer.slice(0, cursor - 1) + buffer.slice(cursor);
cursor--;
render();
}
return;
}
if (key === "\x01") {
if (cursor > 0) {
process.stdout.write(`\x1b[${cursor}D`);
cursor = 0;
}
return;
}
if (key === "\x05") {
if (cursor < buffer.length) {
process.stdout.write(`\x1b[${buffer.length - cursor}C`);
cursor = buffer.length;
}
return;
}
if (key === "\x0b") {
buffer = buffer.slice(0, cursor);
render();
return;
}
if (key === "\x15") {
buffer = buffer.slice(cursor);
cursor = 0;
render();
return;
}
if (key.charCodeAt(0) >= 32 && key.charCodeAt(0) < 127) {
buffer = buffer.slice(0, cursor) + key + buffer.slice(cursor);
cursor++;
render();
}
});
});
} }
async function main() { function printCommitResult(
if (args.includes("--help") || args.includes("-h")) { result: { branch: string; hash: string; files: number; insertions: number; deletions: number },
showHelp(); msg: string,
return; ) {
} console.log(`\n ${GREEN}${BOLD}✔ Committed successfully!${RESET}`);
if (args.includes("--version") || args.includes("-v")) { const id = result.branch && result.hash
console.log("gai v0.1.0"); ? `${YELLOW}[${result.branch} ${result.hash}]${RESET}`
return; : result.hash
} ? `${YELLOW}${result.hash}${RESET}`
: "";
console.log(` ${id} ${msg}`);
if (args[0] === "config") { const parts: string[] = [];
await handleConfig(); if (result.files > 0) parts.push(`${YELLOW}${result.files} file${result.files > 1 ? "s" : ""} changed${RESET}`);
return; if (result.insertions > 0) parts.push(`${GREEN}${result.insertions} insertion${result.insertions > 1 ? "s" : ""}(+)${RESET}`);
} if (result.deletions > 0) parts.push(`${RED}${result.deletions} deletion${result.deletions > 1 ? "s" : ""}(-)${RESET}`);
if (parts.length > 0) console.log(` ${parts.join(", ")}`);
}
const autoMode = args.includes("--auto") || args.includes("-a"); interface MenuAction {
const dryRun = args.includes("--dry-run") || args.includes("-d"); key: "commit" | "pr" | "config";
label: string;
description: string;
}
const config = await loadConfig(); const MENU_ACTIONS: MenuAction[] = [
{ key: "commit", label: "commit", description: "Generate AI commit message" },
{ key: "pr", label: "pr", description: "Create a PR with AI-generated title" },
{ key: "config", label: "config", description: "Configure API settings" },
];
if (!config.apiKey) { async function showMenu(): Promise<void> {
console.error( if (!process.stdin.isTTY) {
` ${RED}Error: API key not set. Run ${BOLD}gai config${RESET}${RED} to configure.${RESET}`, console.error(` ${RED}Error: Interactive menu requires a TTY.${RESET}`);
);
process.exit(1); process.exit(1);
} }
while (true) {
const selected = await selectOne({
title: "gai",
subtitle: "Choose a workflow",
allowBack: false,
items: MENU_ACTIONS.map((action) => ({
label: action.label,
value: action.key,
description: action.description,
})),
});
if (selected === null || selected === BACK) return;
const result =
selected === "commit"
? await handleCommit(false, false)
: selected === "pr"
? await handlePR(false)
: await handleConfig();
if (result !== "back") return;
}
}
async function selectPlatform(
hostname: string,
): Promise<Platform | null | PromptBack> {
if (!process.stdin.isTTY) {
console.error(` ${RED}Error: Platform selection requires a TTY.${RESET}`);
process.exit(1);
}
return selectOne({
title: "Select remote platform",
subtitle: `Remote ${hostname} could not be auto-detected`,
items: [
{ label: "GitHub", value: "github" as Platform, description: "Use gh CLI" },
{ label: "Gitea", value: "gitea" as Platform, description: "Use tea CLI" },
{ label: "GitLab", value: "gitlab" as Platform, description: "Use glab CLI" },
],
});
}
async function handleCommit(
autoMode: boolean,
dryRun: boolean,
): Promise<"done" | "back"> {
if (!(await isGitRepo())) { if (!(await isGitRepo())) {
console.error(` ${RED}Error: Not a git repository.${RESET}`); console.error(` ${RED}Error: Not a git repository.${RESET}`);
process.exit(1); process.exit(1);
@@ -148,8 +654,8 @@ async function main() {
const unstagedFiles = await getUnstagedFiles(); const unstagedFiles = await getUnstagedFiles();
if (stagedFiles.length === 0 && unstagedFiles.length === 0) { if (stagedFiles.length === 0 && unstagedFiles.length === 0) {
console.log(" Nothing to commit."); console.log(` ${DIM}Nothing to commit. No staged or unstaged changes.${RESET}`);
return; return "done";
} }
if (unstagedFiles.length > 0) { if (unstagedFiles.length > 0) {
@@ -160,6 +666,7 @@ async function main() {
); );
} else { } else {
const selected = await selectFiles(stagedFiles, unstagedFiles); const selected = await selectFiles(stagedFiles, unstagedFiles);
if (selected === BACK) return "back";
if (selected.length > 0) { if (selected.length > 0) {
await stageFiles(selected); await stageFiles(selected);
console.log( console.log(
@@ -171,8 +678,17 @@ async function main() {
const diff = await getStagedDiff(); const diff = await getStagedDiff();
if (!diff) { if (!diff) {
console.log(" No staged changes to commit."); console.log(` ${DIM}Nothing to commit. No staged changes to commit.${RESET}`);
return; return "done";
}
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);
} }
const MAX_DIFF_SIZE = 15000; const MAX_DIFF_SIZE = 15000;
@@ -210,15 +726,15 @@ async function main() {
console.log(` ${GREEN}${message}${RESET}`); console.log(` ${GREEN}${message}${RESET}`);
await copyToClipboard(message); await copyToClipboard(message);
console.log(`\n ${DIM}(dry-run, message copied to clipboard)${RESET}`); console.log(`\n ${DIM}(dry-run, message copied to clipboard)${RESET}`);
return; return "done";
} }
const action = await confirmCommit(message); const action = await confirmCommit(message);
if (action === "y") { if (action === "y") {
try { try {
await commit(message); const result = await commit(message);
console.log(`\n ${GREEN}Committed successfully!${RESET}`); printCommitResult(result, message);
} catch (err) { } catch (err) {
console.error( console.error(
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`, ` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
@@ -229,8 +745,8 @@ async function main() {
const edited = await editMessage(message); const edited = await editMessage(message);
if (edited) { if (edited) {
try { try {
await commit(edited); const result = await commit(edited);
console.log(`\n ${GREEN}Committed successfully!${RESET}`); printCommitResult(result, edited);
} catch (err) { } catch (err) {
console.error( console.error(
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`, ` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
@@ -248,8 +764,231 @@ async function main() {
: ` Aborted. Message: ${message}`, : ` Aborted. Message: ${message}`,
); );
} }
return "done";
} }
async function handlePR(draft: boolean): Promise<"done" | "back"> {
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);
}
let platform = await detectPlatform();
if (!platform) {
const hostname = (await getRemoteHostname()) || "unknown";
const chosen = await selectPlatform(hostname);
if (chosen === BACK) return "back";
if (!chosen) {
console.log(" Aborted.");
process.exit(0);
}
platform = chosen;
}
const platformLabel =
platform === "github" ? "GitHub" : platform === "gitlab" ? "GitLab" : "Gitea";
console.log(` Using: ${CYAN}${platformLabel}${RESET}`);
const baseBranch = await getDefaultBranch();
const branchName = await getBranchName();
if (branchName === baseBranch) {
console.error(
` ${RED}Error: You are on the default branch (${baseBranch}). Switch to a feature branch first.${RESET}`,
);
process.exit(1);
}
console.log(
` Branch: ${CYAN}${branchName}${RESET} → base: ${CYAN}${baseBranch}${RESET}`,
);
const commits = await getBranchCommits(baseBranch);
if (commits.length === 0) {
const choice = await selectOne({
title: "No commits to compare",
subtitle: `No commits on ${branchName} compared to ${baseBranch}. Commit something first.`,
items: [{ label: "Back", value: "back" as const }],
});
if (choice === null) process.exit(0);
return "done";
}
console.log(
` ${commits.length} commit${commits.length > 1 ? "s" : ""} on this branch`,
);
const pushStatus = await getBranchPushStatus();
if (!pushStatus.pushed) {
const target = pushStatus.upstream ?? `origin/${branchName}`;
const answer = await ask(
` Branch is not pushed to ${CYAN}${target}${RESET}. Push now? [${GREEN}Y${RESET}/n] `,
);
if (answer.toLowerCase() === "n") {
console.log(" Aborted.");
return "done";
}
console.log(` Pushing ${CYAN}${branchName}${RESET}...`);
try {
await pushCurrentBranch(branchName, pushStatus.upstream);
console.log(` ${GREEN}Pushed ${branchName}.${RESET}`);
} catch (err) {
console.error(
` ${RED}Push failed: ${err instanceof Error ? err.message : err}${RESET}`,
);
process.exit(1);
}
}
const diff = await getBranchDiff(baseBranch);
if (!diff) {
console.error(` ${RED}Error: No diff from base branch.${RESET}`);
process.exit(1);
}
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 userPrompt = buildPRPrompt({
readme: projectCtx.readme,
packageDescription: projectCtx.packageDescription,
structure: projectCtx.structure,
branchName,
baseBranch,
branchCommits: commits,
diff: truncatedDiff,
});
console.log("\n Generating PR title...");
let title: string;
let body: string;
try {
const result = await generatePRMessage(config, PR_SYSTEM_PROMPT, userPrompt);
title = result.title;
body = result.body;
} catch (err) {
console.error(
` ${RED}AI request failed: ${err instanceof Error ? err.message : err}${RESET}`,
);
process.exit(1);
}
console.log(`\n ${BOLD}Generated PR:${RESET}`);
console.log(` Title: ${GREEN}${title}${RESET}`);
if (body) {
console.log(
` Body: ${DIM}${body.replace(/\n/g, "\n ")}${RESET}`,
);
}
console.log("");
const answer = await ask(` Create this PR? [${GREEN}Y${RESET}/n/e] `);
const lower = answer.toLowerCase();
if (lower === "n") {
console.log(" Aborted.");
return "done";
}
if (lower === "e") {
const newTitle = await ask(" Title: ");
const newBody = await ask(" Body (optional): ");
if (!newTitle.trim()) {
console.log(" Aborted.");
return "done";
}
title = newTitle;
body = newBody;
}
console.log(`\n Creating PR...`);
try {
const url = await createPR(platform, title, body, baseBranch, draft);
console.log(` ${GREEN}${BOLD}✔ PR created!${RESET}`);
console.log(` ${CYAN}${url}${RESET}`);
} catch (err) {
console.error(
` ${RED}PR creation failed: ${err instanceof Error ? err.message : err}${RESET}`,
);
process.exit(1);
}
return "done";
}
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;
}
const subcommand = args[0];
if (subcommand === "config") {
await handleConfig();
return;
}
if (subcommand === "help") {
showHelp();
return;
}
if (subcommand === "commit") {
const autoMode = args.includes("--auto") || args.includes("-a");
const dryRun = args.includes("--dry-run") || args.includes("-d");
await handleCommit(autoMode, dryRun);
return;
}
if (subcommand === "pr") {
const draft = args.includes("--draft");
await handlePR(draft);
return;
}
if (!subcommand) {
await showMenu();
return;
}
console.error(` ${RED}Unknown command: ${subcommand}${RESET}`);
showHelp();
process.exit(1);
}
process.on("SIGINT", () => {
process.stdout.write("\x1b[?25h");
process.stdout.write("\n");
process.exit(130);
});
main().catch((err) => { main().catch((err) => {
console.error(` ${RED}Unexpected error: ${err}${RESET}`); console.error(` ${RED}Unexpected error: ${err}${RESET}`);
process.exit(1); process.exit(1);
+2 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "git-ai", "name": "gai",
"version": "0.1.0", "version": "0.1.2",
"description": "AI-powered git commit message generator", "description": "AI-powered git commit message generator",
"module": "index.ts", "module": "index.ts",
"type": "module", "type": "module",
+124 -26
View File
@@ -8,12 +8,38 @@ interface ChatMessage {
interface ChatCompletionResponse { interface ChatCompletionResponse {
choices?: Array<{ choices?: Array<{
message?: { message?: {
content?: string; content?: string | null;
}; };
finish_reason?: string;
}>; }>;
error?: {
message?: string;
type?: string;
code?: string;
};
} }
export async function generateCommitMessage( const MAX_RETRIES = 3;
const RETRY_DELAY = 1000;
function cleanMessage(raw: string): string {
let msg = raw.trim();
if (msg.startsWith("```") && msg.endsWith("```")) {
const lines = msg.split("\n");
if (lines.length > 2) {
lines.shift();
lines.pop();
msg = lines.join("\n").trim();
}
}
return msg;
}
async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function callAI(
config: Config, config: Config,
systemPrompt: string, systemPrompt: string,
userPrompt: string, userPrompt: string,
@@ -25,31 +51,103 @@ export async function generateCommitMessage(
{ role: "user", content: userPrompt }, { role: "user", content: userPrompt },
]; ];
const response = await fetch(url, { for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
method: "POST", try {
headers: { const response = await fetch(url, {
"Content-Type": "application/json", method: "POST",
Authorization: `Bearer ${config.apiKey}`, headers: {
}, "Content-Type": "application/json",
body: JSON.stringify({ Authorization: `Bearer ${config.apiKey}`,
model: config.model, },
max_tokens: config.maxTokens, body: JSON.stringify({
temperature: config.temperature, model: config.model,
messages, max_tokens: config.maxTokens,
}), temperature: config.temperature,
}); messages,
}),
});
if (!response.ok) { if (!response.ok) {
const text = await response.text(); const text = await response.text();
throw new Error(`API request failed (${response.status}): ${text}`); if (response.status === 429 && attempt < MAX_RETRIES) {
await sleep(RETRY_DELAY * attempt);
continue;
}
throw new Error(`API request failed (${response.status}): ${text}`);
}
const data = (await response.json()) as ChatCompletionResponse;
if (data.error) {
throw new Error(
`API error: ${data.error.message ?? JSON.stringify(data.error)}`,
);
}
const raw = data.choices?.[0]?.message?.content;
const finishReason = data.choices?.[0]?.finish_reason;
if (raw && raw.trim()) {
return raw;
}
if (finishReason === "length") {
throw new Error(
"Response truncated (max_tokens too low). Try increasing GAI_MAX_TOKENS.",
);
}
if (finishReason === "content_filter") {
throw new Error("Response blocked by content filter.");
}
if (attempt < MAX_RETRIES) {
await sleep(RETRY_DELAY * attempt);
continue;
}
throw new Error(
`Empty response from AI after ${MAX_RETRIES} attempts. finish_reason: ${finishReason ?? "unknown"}`,
);
} catch (err) {
if (attempt >= MAX_RETRIES) throw err;
if (err instanceof Error && err.message.startsWith("API error")) throw err;
if (err instanceof Error && err.message.includes("max_tokens")) throw err;
if (err instanceof Error && err.message.includes("content filter"))
throw err;
await sleep(RETRY_DELAY * attempt);
}
} }
const data = (await response.json()) as ChatCompletionResponse; throw new Error("Failed to generate response");
const message = data.choices?.[0]?.message?.content?.trim(); }
if (!message) { export async function generateCommitMessage(
throw new Error("Empty response from AI"); config: Config,
} systemPrompt: string,
userPrompt: string,
return message; ): Promise<string> {
const raw = await callAI(config, systemPrompt, userPrompt);
return cleanMessage(raw);
}
export async function generatePRMessage(
config: Config,
systemPrompt: string,
userPrompt: string,
): Promise<{ title: string; body: string }> {
const raw = await callAI(config, systemPrompt, userPrompt);
const cleaned = cleanMessage(raw);
const lines = cleaned.split("\n");
const title = lines[0]?.trim() || "Update";
let bodyStart = 1;
while (bodyStart < lines.length && lines[bodyStart]?.trim() === "") {
bodyStart++;
}
const body = lines.slice(bodyStart).join("\n").trim();
return { title, body };
} }
+2 -2
View File
@@ -5,8 +5,8 @@ import type { Config } from "./types";
const DEFAULT_CONFIG: Config = { const DEFAULT_CONFIG: Config = {
apiKey: "", apiKey: "",
apiBase: "https://api.openai.com/v1", apiBase: "https://api.deepseek.com/v1",
model: "gpt-4o", model: "deepseek-v4-flash",
maxTokens: 500, maxTokens: 500,
temperature: 0.7, temperature: 0.7,
}; };
+25 -4
View File
@@ -98,13 +98,34 @@ export async function stageFiles(paths: string[]): Promise<void> {
await Bun.$`git add -- ${paths}`; await Bun.$`git add -- ${paths}`;
} }
export async function commit(message: string): Promise<void> { export async function commit(
message: string,
): Promise<{ branch: string; hash: string; files: number; insertions: number; deletions: number }> {
const proc = Bun.spawn(["git", "commit", "-m", message], { const proc = Bun.spawn(["git", "commit", "-m", message], {
stdout: "inherit", stdout: "pipe",
stderr: "inherit", stderr: "pipe",
}); });
const exitCode = await proc.exited; const exitCode = await proc.exited;
const stdout = await new Response(proc.stdout).text();
const stderr = await new Response(proc.stderr).text();
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error(`git commit failed (exit code ${exitCode})`); throw new Error(stderr.trim() || `git commit failed (exit code ${exitCode})`);
} }
const branchHashMatch = stdout.match(/\[(\S+)\s+([0-9a-f]{7,})/);
const branch = branchHashMatch?.[1] ?? "";
const hash = branchHashMatch?.[2] ?? "";
const filesMatch = stdout.match(/(\d+)\s+file/);
const insertionsMatch = stdout.match(/(\d+)\s+insertion/);
const deletionsMatch = stdout.match(/(\d+)\s+deletion/);
return {
branch,
hash,
files: parseInt(filesMatch?.[1] ?? "0"),
insertions: parseInt(insertionsMatch?.[1] ?? "0"),
deletions: parseInt(deletionsMatch?.[1] ?? "0"),
};
} }
+328
View File
@@ -0,0 +1,328 @@
import { BOLD, GREEN, CYAN, DIM, RESET } from "./terminal";
const UP = "\x1b[A";
const DOWN = "\x1b[B";
const LEFT = "\x1b[D";
const ALT_UP = "\x1bOA";
const ALT_DOWN = "\x1bOB";
const ALT_LEFT = "\x1bOD";
const SPACE = " ";
const ENTER = "\r";
const CTRL_C = "\x03";
const BACKSPACE = "\x7f";
export const BACK = Symbol("prompt-back");
export type PromptBack = typeof BACK;
export interface Choice<T> {
label: string;
value: T;
description?: string;
selected?: boolean;
}
interface BasePromptOptions {
title: string;
subtitle?: string;
cancelMessage?: string;
allowBack?: boolean;
}
interface SinglePromptOptions<T> extends BasePromptOptions {
items: Choice<T>[];
}
interface MultiPromptOptions<T> extends BasePromptOptions {
items: Choice<T>[];
selectAllLabel?: string;
doneLabel?: string;
}
function hideCursor() {
process.stdout.write("\x1b[?25l");
}
function showCursor() {
process.stdout.write("\x1b[?25h");
}
function moveUp(lines: number) {
if (lines > 0) process.stdout.write(`\x1b[${lines}A`);
}
function clearLine() {
process.stdout.write("\r\x1b[2K");
}
function visibleLength(value: string) {
return value.replace(/\x1b\[[0-9;]*m/g, "").length;
}
function padLabel(label: string, width: number) {
return label + " ".repeat(Math.max(1, width - visibleLength(label)));
}
function controls(mode: "single" | "multi", showBackHint = true) {
if (mode === "single") {
const backHint = showBackHint ? " · ←/backspace back" : "";
return `${DIM}↑/↓ navigate · enter/space select${backHint} · ctrl+c cancel${RESET}`;
}
const backHint = showBackHint ? " · ←/backspace back" : "";
return `${DIM}↑/↓ navigate · space toggle · enter confirm${backHint} · ctrl+c cancel${RESET}`;
}
function renderPrompt(lines: string[], previousLines: number) {
if (previousLines > 0) {
for (let i = 0; i < previousLines; i++) {
clearLine();
process.stdout.write("\n");
}
moveUp(previousLines);
}
for (const line of lines) {
process.stdout.write(`${line}\n`);
}
moveUp(lines.length);
return lines.length;
}
function clearPrompt(lines: number) {
for (let i = 0; i < lines; i++) {
clearLine();
process.stdout.write("\n");
}
moveUp(lines);
}
function normalizeKey(key: string, escapeBuf: string) {
if (key === UP || key === ALT_UP) return { action: "up", escapeBuf: "" };
if (key === DOWN || key === ALT_DOWN) return { action: "down", escapeBuf: "" };
if (key === LEFT || key === ALT_LEFT || key === BACKSPACE) {
return { action: "back", escapeBuf: "" };
}
if (key === SPACE) return { action: "space", escapeBuf: "" };
if (key === ENTER) return { action: "enter", escapeBuf: "" };
if (key === CTRL_C) return { action: "cancel", escapeBuf: "" };
if (key === "\x1b" || key.startsWith("\x1b[")) {
return { action: null, escapeBuf: key };
}
if (escapeBuf) {
const next = escapeBuf + key;
if (next === UP || next === ALT_UP) return { action: "up", escapeBuf: "" };
if (next === DOWN || next === ALT_DOWN) return { action: "down", escapeBuf: "" };
if (next === LEFT || next === ALT_LEFT) {
return { action: "back", escapeBuf: "" };
}
return {
action: null,
escapeBuf: /^[A-Za-z~]$/.test(key) || next.length > 8 ? "" : next,
};
}
return { action: null, escapeBuf: "" };
}
function createLines<T>(
options: BasePromptOptions & { items: Choice<T>[] },
mode: "single" | "multi",
cursor: number,
) {
const labelWidth = Math.max(
...options.items.map((item) => visibleLength(item.label)),
0,
) + 2;
const lines = [
"",
` ${BOLD}${options.title}${RESET}`,
];
if (options.subtitle) lines.push(` ${DIM}${options.subtitle}${RESET}`);
lines.push(` ${controls(mode, options.allowBack !== false)}`, "");
for (let i = 0; i < options.items.length; i++) {
const item = options.items[i]!;
const active = i === cursor;
const pointer = active ? `${CYAN}${RESET}` : " ";
const marker = mode === "single"
? active ? `${GREEN}${RESET}` : `${DIM}${RESET}`
: item.selected ? `${GREEN}${RESET}` : `${DIM}${RESET}`;
const label = active ? `${BOLD}${item.label}${RESET}` : item.label;
const description = item.description
? active ? item.description : `${DIM}${item.description}${RESET}`
: "";
lines.push(
` ${pointer} ${marker} ${padLabel(label, labelWidth)}${description}`,
);
}
return lines;
}
function ensureTTY(title: string) {
if (process.stdin.isTTY !== true) {
throw new Error(`${title} requires a TTY.`);
}
}
export async function selectOne<T>(
options: SinglePromptOptions<T>,
): Promise<T | null | PromptBack> {
ensureTTY(options.title);
let cursor = 0;
let renderedLines = 0;
let escapeBuf = "";
const wasRaw = process.stdin.isRaw;
if (wasRaw !== true) process.stdin.setRawMode(true);
process.stdin.resume();
hideCursor();
const render = () => {
renderedLines = renderPrompt(
createLines(options, "single", cursor),
renderedLines,
);
};
render();
return new Promise((resolve) => {
const finish = (value: T | null | PromptBack) => {
process.stdin.setRawMode(wasRaw === true);
process.stdin.pause();
process.stdin.removeListener("data", onData);
clearPrompt(renderedLines);
showCursor();
if (value === null && options.cancelMessage) {
process.stdout.write(` ${options.cancelMessage}\n`);
}
resolve(value);
};
const onData = (data: Buffer) => {
const result = normalizeKey(data.toString(), escapeBuf);
escapeBuf = result.escapeBuf;
if (result.action === "cancel") return finish(null);
if (result.action === "back" && options.allowBack !== false) {
return finish(BACK);
}
if (result.action === "up" && cursor > 0) {
cursor--;
render();
} else if (result.action === "down" && cursor < options.items.length - 1) {
cursor++;
render();
} else if (result.action === "space" || result.action === "enter") {
finish(options.items[cursor]!.value);
}
};
process.stdin.on("data", onData);
});
}
export async function selectMany<T>(
options: MultiPromptOptions<T>,
): Promise<T[] | null | PromptBack> {
ensureTTY(options.title);
const items: Choice<T | null>[] = options.selectAllLabel
? [
{ label: options.selectAllLabel, value: null, selected: false },
...options.items.map((item) => ({ ...item })),
]
: options.items.map((item) => ({ ...item }));
let cursor = 0;
let renderedLines = 0;
let escapeBuf = "";
const wasRaw = process.stdin.isRaw;
if (wasRaw !== true) process.stdin.setRawMode(true);
process.stdin.resume();
hideCursor();
const syncSelectAll = () => {
if (!options.selectAllLabel) return;
items[0]!.selected = items.slice(1).every((item) => item.selected);
};
const toggle = (index: number) => {
const item = items[index]!;
item.selected = !item.selected;
if (options.selectAllLabel && index === 0) {
for (let i = 1; i < items.length; i++) {
items[i]!.selected = item.selected;
}
} else {
syncSelectAll();
}
};
const render = () => {
renderedLines = renderPrompt(
createLines(
{
title: options.title,
subtitle: options.subtitle,
items,
},
"multi",
cursor,
),
renderedLines,
);
};
render();
return new Promise((resolve) => {
const finish = (value: T[] | null | PromptBack) => {
process.stdin.setRawMode(wasRaw === true);
process.stdin.pause();
process.stdin.removeListener("data", onData);
clearPrompt(renderedLines);
showCursor();
if (value === null && options.cancelMessage) {
process.stdout.write(` ${options.cancelMessage}\n`);
}
resolve(value);
};
const onData = (data: Buffer) => {
const result = normalizeKey(data.toString(), escapeBuf);
escapeBuf = result.escapeBuf;
if (result.action === "cancel") return finish(null);
if (result.action === "back" && options.allowBack !== false) {
return finish(BACK);
}
if (result.action === "up" && cursor > 0) {
cursor--;
render();
} else if (result.action === "down" && cursor < items.length - 1) {
cursor++;
render();
} else if (result.action === "space") {
toggle(cursor);
render();
} else if (result.action === "enter") {
finish(
items
.filter((item) => item.selected && item.value !== null)
.map((item) => item.value as T),
);
}
};
process.stdin.on("data", onData);
});
}
+223
View File
@@ -0,0 +1,223 @@
export type Platform = "github" | "gitea" | "gitlab";
export async function getDefaultBranch(): Promise<string> {
try {
const result =
await Bun.$`git symbolic-ref refs/remotes/origin/HEAD`.quiet().text();
return result.trim().replace("refs/remotes/origin/", "");
} catch {
try {
const branches = await Bun.$`git branch -r`.quiet().text();
for (const line of branches.split("\n")) {
const trimmed = line.trim();
if (trimmed === "origin/main" || trimmed === "origin/master") {
return trimmed.replace("origin/", "");
}
}
} catch {}
return "main";
}
}
export async function getBranchName(): Promise<string> {
const result =
await Bun.$`git rev-parse --abbrev-ref HEAD`.quiet().text();
return result.trim();
}
export async function getBranchPushStatus(): Promise<{
pushed: boolean;
upstream: string | null;
}> {
try {
const upstream = (await Bun.$`git rev-parse --abbrev-ref --symbolic-full-name @{u}`.quiet().text()).trim();
const localHead = (await Bun.$`git rev-parse HEAD`.quiet().text()).trim();
const remoteHead = (await Bun.$`git rev-parse @{u}`.quiet().text()).trim();
return { pushed: localHead === remoteHead, upstream };
} catch {
return { pushed: false, upstream: null };
}
}
export async function pushCurrentBranch(branch: string, upstream: string | null): Promise<void> {
const args = upstream
? ["push"]
: ["push", "-u", "origin", branch];
const proc = Bun.spawn(["git", ...args], {
stdout: "pipe",
stderr: "pipe",
});
const exitCode = await proc.exited;
const stdout = await new Response(proc.stdout).text();
const stderr = await new Response(proc.stderr).text();
if (exitCode !== 0) {
throw new Error(stderr.trim() || stdout.trim() || `git push failed (exit code ${exitCode})`);
}
}
export async function getBranchCommits(base: string): Promise<string[]> {
try {
const result =
await Bun.$`git log --oneline origin/${base}..HEAD`.quiet().text();
return result.trim().split("\n").filter(Boolean);
} catch {
try {
const result =
await Bun.$`git log --oneline ${base}..HEAD`.quiet().text();
return result.trim().split("\n").filter(Boolean);
} catch {
return [];
}
}
}
export async function getBranchDiff(base: string): Promise<string> {
try {
const result =
await Bun.$`git diff ${base}...HEAD`.quiet().text();
return result.trim();
} catch {
return "";
}
}
function parseRemoteHostname(url: string): string | null {
const hostname = url
.trim()
.toLowerCase()
.replace(/^(https?:\/\/|ssh:\/\/|git:\/\/)/, "")
.replace(/^[^@]+@/, "")
.split(/[:/]/)[0];
return hostname || null;
}
export function detectPlatformFromHostname(hostname: string | null): Platform | null {
if (!hostname) return null;
if (hostname === "github.com") return "github";
if (hostname === "gitlab.com" || hostname.includes("gitlab")) {
return "gitlab";
}
if (hostname.includes("gitea")) return "gitea";
return null;
}
export async function detectPlatform(): Promise<Platform | null> {
try {
const url = await Bun.$`git remote get-url origin`.quiet().text();
const hostname = parseRemoteHostname(url);
return detectPlatformFromHostname(hostname);
} catch {
return null;
}
}
export async function getRemoteHostname(): Promise<string | null> {
try {
const url = await Bun.$`git remote get-url origin`.quiet().text();
return parseRemoteHostname(url);
} catch {
return null;
}
}
export async function createPR(
platform: Platform,
title: string,
body: string,
base: string,
draft: boolean,
): Promise<string> {
if (platform === "github") {
const args = [
"pr",
"create",
"--title",
title,
"--body",
body,
"--base",
base,
];
if (draft) args.push("--draft");
const proc = Bun.spawn(["gh", ...args], {
stdout: "pipe",
stderr: "pipe",
});
const exitCode = await proc.exited;
const stdout = await new Response(proc.stdout).text();
const stderr = await new Response(proc.stderr).text();
if (exitCode !== 0) {
throw new Error(
stderr.trim() || `gh pr create failed (exit code ${exitCode})`,
);
}
const match = stdout.match(/(https?:\/\/[^\s]+)/);
return match?.[1] ?? stdout.trim();
}
if (platform === "gitlab") {
const args = [
"mr",
"create",
"--title",
title,
"--description",
body,
"--target-branch",
base,
];
if (draft) args.push("--draft");
const proc = Bun.spawn(["glab", ...args], {
stdout: "pipe",
stderr: "pipe",
});
const exitCode = await proc.exited;
const stdout = await new Response(proc.stdout).text();
const stderr = await new Response(proc.stderr).text();
if (exitCode !== 0) {
throw new Error(
stderr.trim() || `glab mr create failed (exit code ${exitCode})`,
);
}
const match = stdout.match(/(https?:\/\/[^\s]+)/);
return match?.[1] ?? stdout.trim();
}
const args = [
"pulls",
"create",
"--title",
title,
"--description",
body,
"--base",
base,
];
const proc = Bun.spawn(["tea", ...args], {
stdout: "pipe",
stderr: "pipe",
});
const exitCode = await proc.exited;
const stdout = await new Response(proc.stdout).text();
const stderr = await new Response(proc.stderr).text();
if (exitCode !== 0) {
throw new Error(
stderr.trim() || `tea pulls create failed (exit code ${exitCode})`,
);
}
const match = stdout.match(/(https?:\/\/[^\s]+)/);
return match?.[1] ?? stdout.trim();
}
+63 -1
View File
@@ -1,4 +1,4 @@
import type { ProjectContext } from "./types"; import type { PRContext, ProjectContext } from "./types";
export const SYSTEM_PROMPT = `You are an expert at writing concise, meaningful git commit messages following the Conventional Commits specification. export const SYSTEM_PROMPT = `You are an expert at writing concise, meaningful git commit messages following the Conventional Commits specification.
@@ -54,3 +54,65 @@ export function buildPrompt(context: ProjectContext): string {
return parts.join("\n"); return parts.join("\n");
} }
export const PR_SYSTEM_PROMPT = `You are an expert at writing clear, concise pull request titles and descriptions.
Format:
<pr title>
<blank line>
<pr body>
Rules:
1. Title must be under 72 characters, in imperative mood
2. Follow the Conventional Commits style for the title (e.g., "feat(api): add user authentication")
3. Body should be 2-3 sentences in plain text explaining WHAT was changed and WHY
4. Be specific — avoid vague messages
5. Match the language and style of recent commits if provided
6. If the branch name hints at the type (e.g., "feat/..." or "fix/..."), reflect that in the title
7. Output ONLY the PR text — no markdown, no code blocks, no prefixes`;
export function buildPRPrompt(context: PRContext): string {
const parts: string[] = [];
if (
context.packageDescription ||
context.readme ||
context.structure
) {
parts.push("## Project Context");
if (context.packageDescription) {
parts.push(`Description: ${context.packageDescription}`);
}
if (context.structure) {
parts.push(`Structure: ${context.structure}`);
}
if (context.readme) {
parts.push(`README:\n${context.readme}`);
}
parts.push("");
}
parts.push("## Branch Info");
parts.push(`Branch: ${context.branchName}`);
parts.push(`Target base: ${context.baseBranch}`);
parts.push("");
if (context.branchCommits.length > 0) {
parts.push("## Commits on This Branch");
for (const c of context.branchCommits) {
parts.push(c);
}
parts.push("");
}
parts.push("## Changes (diff from base)");
parts.push("```diff");
parts.push(context.diff);
parts.push("```");
parts.push("");
parts.push(
"Generate a pull request title and brief body for the above changes.",
);
return parts.join("\n");
}
+27 -46
View File
@@ -1,62 +1,43 @@
import * as readline from "node:readline";
import type { FileEntry } from "./types"; import type { FileEntry } from "./types";
import { BOLD, GREEN, YELLOW, CYAN, RESET } from "./terminal"; import { BOLD, GREEN, YELLOW, RESET } from "./terminal";
import { BACK, selectMany } from "./menu";
import type { PromptBack } from "./menu";
export async function selectFiles( export async function selectFiles(
stagedFiles: FileEntry[], stagedFiles: FileEntry[],
unstagedFiles: FileEntry[], unstagedFiles: FileEntry[],
): Promise<string[]> { ): Promise<string[] | PromptBack> {
if (unstagedFiles.length === 0) return []; if (unstagedFiles.length === 0) return [];
if (stagedFiles.length > 0) { if (stagedFiles.length > 0) {
console.log(`\n ${BOLD}Staged files (will be included):${RESET}`); process.stdout.write(`\n ${BOLD}Staged files (will be included):${RESET}\n`);
for (const f of stagedFiles) { for (const f of stagedFiles) {
console.log(` ${GREEN}${RESET} ${f.path} (${YELLOW}${f.label}${RESET})`); process.stdout.write(` ${GREEN}${RESET} ${f.path} (${YELLOW}${f.label}${RESET})\n`);
} }
} }
console.log(`\n ${BOLD}Unstaged files:${RESET}`); if (process.stdin.isTTY !== true) return [];
for (let i = 0; i < unstagedFiles.length; i++) {
const f = unstagedFiles[i]!; const selected = await selectMany({
console.log( title: "Select files to stage",
` ${CYAN}${i + 1}.${RESET} ${f.path} (${YELLOW}${f.label}${RESET})`, subtitle: `${unstagedFiles.length} unstaged file${unstagedFiles.length > 1 ? "s" : ""} available`,
selectAllLabel: "Select all",
cancelMessage: "Aborted.",
items: unstagedFiles.map((f) => ({
label: f.path,
value: f.path,
description: f.label,
})),
});
if (selected === null) process.exit(1);
if (selected === BACK) return BACK;
if (selected.length > 0) {
process.stdout.write(
` ${GREEN}Staged ${selected.length} file(s):${RESET} ${selected.join(", ")}\n`,
); );
} }
console.log(""); return selected;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(
" Enter files to stage (e.g. 1,3) or 'a' for all: ",
(answer) => {
rl.close();
const trimmed = answer.trim().toLowerCase();
if (trimmed === "a" || trimmed === "all") {
resolve(unstagedFiles.map((f) => f.path));
return;
}
if (trimmed === "") {
resolve([]);
return;
}
const indices = trimmed
.split(/[,\s]+/)
.map((s) => parseInt(s.trim()))
.filter(
(n) => !isNaN(n) && n >= 1 && n <= unstagedFiles.length,
)
.map((n) => n - 1);
const uniqueIndices = [...new Set(indices)];
resolve(uniqueIndices.map((i) => unstagedFiles[i]!.path));
},
);
});
} }
+10
View File
@@ -19,3 +19,13 @@ export interface ProjectContext {
recentCommits: string[]; recentCommits: string[];
diff: string; diff: string;
} }
export interface PRContext {
readme: string | null;
packageDescription: string | null;
structure: string | null;
branchName: string;
baseBranch: string;
branchCommits: string[];
diff: string;
}
+50
View File
@@ -0,0 +1,50 @@
import { mkdtempSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { test, expect, describe } from "bun:test";
async function run(command: string[], cwd: string, env: Record<string, string> = {}) {
const proc = Bun.spawn(command, {
cwd,
stdout: "pipe",
stderr: "pipe",
env: {
PATH: process.env.PATH ?? "",
HOME: env.HOME ?? process.env.HOME ?? "",
...env,
},
});
const [exitCode, stdout, stderr] = await Promise.all([
proc.exited,
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
return { exitCode, stdout, stderr };
}
describe("commit command", () => {
test("clean repository exits without requiring API key", async () => {
const repo = mkdtempSync(join(tmpdir(), "gai-clean-repo-"));
const home = mkdtempSync(join(tmpdir(), "gai-empty-home-"));
const init = await run(["git", "init"], repo, { HOME: home });
expect(init.exitCode).toBe(0);
const result = await run(
["bun", "run", join(import.meta.dir, "..", "index.ts"), "commit"],
repo,
{
HOME: home,
GAI_API_KEY: "",
},
);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("Nothing to commit");
expect(result.stdout).toContain("No staged or unstaged changes");
expect(result.stderr).not.toContain("API key not set");
expect(result.stderr).not.toContain("requires a TTY");
});
});
+40
View File
@@ -0,0 +1,40 @@
import { test, expect, describe } from "bun:test";
import { loadConfig } from "../src/config";
describe("config", () => {
test("loadConfig env variables override config file and defaults", async () => {
const origBase = process.env.GAI_API_BASE;
const origModel = process.env.GAI_MODEL;
process.env.GAI_API_BASE = "https://custom.api.com/v1";
process.env.GAI_MODEL = "custom-model";
const config = await loadConfig();
expect(config.apiBase).toBe("https://custom.api.com/v1");
expect(config.model).toBe("custom-model");
if (origBase) process.env.GAI_API_BASE = origBase;
else delete process.env.GAI_API_BASE;
if (origModel) process.env.GAI_MODEL = origModel;
else delete process.env.GAI_MODEL;
});
test("loadConfig reads from environment variables", async () => {
const origBase = process.env.GAI_API_BASE;
const origModel = process.env.GAI_MODEL;
process.env.GAI_API_BASE = "https://api.deepseek.com/v1";
process.env.GAI_MODEL = "deepseek-v4-flash";
const config = await loadConfig();
expect(config.apiBase).toBe("https://api.deepseek.com/v1");
expect(config.model).toBe("deepseek-v4-flash");
if (origBase) process.env.GAI_API_BASE = origBase;
else delete process.env.GAI_API_BASE;
if (origModel) process.env.GAI_MODEL = origModel;
else delete process.env.GAI_MODEL;
});
});
+19
View File
@@ -0,0 +1,19 @@
import { test, expect, describe } from "bun:test";
import { detectPlatformFromHostname } from "../src/pr";
describe("pr platform detection", () => {
test("detects supported hosted platforms", () => {
expect(detectPlatformFromHostname("github.com")).toBe("github");
expect(detectPlatformFromHostname("gitlab.com")).toBe("gitlab");
});
test("detects self-hosted platform hostnames", () => {
expect(detectPlatformFromHostname("gitlab.example.com")).toBe("gitlab");
expect(detectPlatformFromHostname("gitea.example.com")).toBe("gitea");
});
test("returns null for unknown hostnames", () => {
expect(detectPlatformFromHostname("git.example.com")).toBeNull();
expect(detectPlatformFromHostname(null)).toBeNull();
});
});
+64
View File
@@ -0,0 +1,64 @@
import { test, expect, describe } from "bun:test";
import { buildPrompt, SYSTEM_PROMPT } from "../src/prompt";
import type { ProjectContext } from "../src/types";
describe("prompt", () => {
test("SYSTEM_PROMPT contains Conventional Commits format", () => {
expect(SYSTEM_PROMPT).toContain("<type>(<scope>)");
expect(SYSTEM_PROMPT).toContain("feat");
expect(SYSTEM_PROMPT).toContain("fix");
});
test("buildPrompt with full context", () => {
const ctx: ProjectContext = {
readme: "A test project",
packageDescription: "Test package",
structure: "src/, tests/",
recentCommits: ["abc123 feat: initial commit"],
diff: "+new line\n-old line",
};
const prompt = buildPrompt(ctx);
expect(prompt).toContain("## Project Context");
expect(prompt).toContain("Test package");
expect(prompt).toContain("src/, tests/");
expect(prompt).toContain("A test project");
expect(prompt).toContain("## Recent Commits");
expect(prompt).toContain("abc123 feat: initial commit");
expect(prompt).toContain("## Staged Changes");
expect(prompt).toContain("+new line");
expect(prompt).toContain("Generate a commit message");
});
test("buildPrompt with minimal context (only diff)", () => {
const ctx: ProjectContext = {
readme: null,
packageDescription: null,
structure: null,
recentCommits: [],
diff: "+added code",
};
const prompt = buildPrompt(ctx);
expect(prompt).not.toContain("## Project Context");
expect(prompt).not.toContain("## Recent Commits");
expect(prompt).toContain("## Staged Changes");
expect(prompt).toContain("+added code");
});
test("buildPrompt includes diff as-is (truncation is caller's responsibility)", () => {
const diff = "+short diff content";
const ctx: ProjectContext = {
readme: null,
packageDescription: null,
structure: null,
recentCommits: [],
diff,
};
const prompt = buildPrompt(ctx);
expect(prompt).toContain(diff);
});
});