feat(config): add interactive config editor and GitLab PR support #4

Merged
Mplan merged 8 commits from v0.1.2 into main 2026-06-12 09:00:29 +08:00
4 changed files with 78 additions and 33 deletions
Showing only changes of commit e1354e8651 - Show all commits
+1 -1
View File
@@ -84,7 +84,7 @@ $ gai commit
Select files to stage: Select files to stage:
2 unstaged files available 2 unstaged files available
↑/↓ navigate · space toggle · enter confirm · ctrl+c cancel ↑/↓ navigate · space toggle · enter confirm · ←/backspace back · ctrl+c cancel
□ Select all □ Select all
□ src/ai.ts modified □ src/ai.ts modified
+44 -22
View File
@@ -13,7 +13,8 @@ import {
commit, commit,
} from "./src/git"; } from "./src/git";
import { selectFiles } from "./src/selector"; import { selectFiles } from "./src/selector";
import { selectOne } from "./src/menu"; 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";
@@ -304,22 +305,34 @@ async function showMenu(): Promise<void> {
process.exit(1); process.exit(1);
} }
const selected = await selectOne({ while (true) {
title: "gai", const selected = await selectOne({
subtitle: "Choose a workflow", title: "gai",
items: MENU_ACTIONS.map((action) => ({ subtitle: "Choose a workflow",
label: action.label, allowBack: false,
value: action.key, items: MENU_ACTIONS.map((action) => ({
description: action.description, label: action.label,
})), value: action.key,
}); description: action.description,
})),
});
if (selected === "commit") await handleCommit(false, false); if (selected === null || selected === BACK) return;
if (selected === "pr") await handlePR(false);
if (selected === "config") await handleConfig(); const result =
selected === "commit"
? await handleCommit(false, false)
: selected === "pr"
? await handlePR(false)
: await handleConfig().then(() => "done" as const);
if (result !== "back") return;
}
} }
async function selectPlatform(hostname: string): Promise<Platform | null> { async function selectPlatform(
hostname: string,
): Promise<Platform | null | PromptBack> {
if (!process.stdin.isTTY) { if (!process.stdin.isTTY) {
console.error(` ${RED}Error: Platform selection requires a TTY.${RESET}`); console.error(` ${RED}Error: Platform selection requires a TTY.${RESET}`);
process.exit(1); process.exit(1);
@@ -336,7 +349,10 @@ async function selectPlatform(hostname: string): Promise<Platform | null> {
}); });
} }
async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> { async function handleCommit(
autoMode: boolean,
dryRun: boolean,
): Promise<"done" | "back"> {
const config = await loadConfig(); const config = await loadConfig();
if (!config.apiKey) { if (!config.apiKey) {
@@ -356,7 +372,7 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
if (stagedFiles.length === 0 && unstagedFiles.length === 0) { if (stagedFiles.length === 0 && unstagedFiles.length === 0) {
console.log(" Nothing to commit."); console.log(" Nothing to commit.");
return; return "done";
} }
if (unstagedFiles.length > 0) { if (unstagedFiles.length > 0) {
@@ -367,6 +383,7 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
); );
} 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(
@@ -379,7 +396,7 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
const diff = await getStagedDiff(); const diff = await getStagedDiff();
if (!diff) { if (!diff) {
console.log(" No staged changes to commit."); console.log(" No staged changes to commit.");
return; return "done";
} }
const MAX_DIFF_SIZE = 15000; const MAX_DIFF_SIZE = 15000;
@@ -417,7 +434,7 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
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);
@@ -455,9 +472,11 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
: ` Aborted. Message: ${message}`, : ` Aborted. Message: ${message}`,
); );
} }
return "done";
} }
async function handlePR(draft: boolean): Promise<void> { async function handlePR(draft: boolean): Promise<"done" | "back"> {
const config = await loadConfig(); const config = await loadConfig();
if (!config.apiKey) { if (!config.apiKey) {
@@ -476,6 +495,7 @@ async function handlePR(draft: boolean): Promise<void> {
if (!platform) { if (!platform) {
const hostname = (await getRemoteHostname()) || "unknown"; const hostname = (await getRemoteHostname()) || "unknown";
const chosen = await selectPlatform(hostname); const chosen = await selectPlatform(hostname);
if (chosen === BACK) return "back";
if (!chosen) { if (!chosen) {
console.log(" Aborted."); console.log(" Aborted.");
process.exit(0); process.exit(0);
@@ -523,7 +543,7 @@ async function handlePR(draft: boolean): Promise<void> {
if (answer.toLowerCase() === "n") { if (answer.toLowerCase() === "n") {
console.log(" Aborted."); console.log(" Aborted.");
return; return "done";
} }
console.log(` Pushing ${CYAN}${branchName}${RESET}...`); console.log(` Pushing ${CYAN}${branchName}${RESET}...`);
@@ -592,7 +612,7 @@ async function handlePR(draft: boolean): Promise<void> {
if (lower === "n") { if (lower === "n") {
console.log(" Aborted."); console.log(" Aborted.");
return; return "done";
} }
if (lower === "e") { if (lower === "e") {
@@ -600,7 +620,7 @@ async function handlePR(draft: boolean): Promise<void> {
const newBody = await ask(" Body (optional): "); const newBody = await ask(" Body (optional): ");
if (!newTitle.trim()) { if (!newTitle.trim()) {
console.log(" Aborted."); console.log(" Aborted.");
return; return "done";
} }
title = newTitle; title = newTitle;
body = newBody; body = newBody;
@@ -618,6 +638,8 @@ async function handlePR(draft: boolean): Promise<void> {
); );
process.exit(1); process.exit(1);
} }
return "done";
} }
async function main() { async function main() {
+29 -8
View File
@@ -2,11 +2,17 @@ import { BOLD, GREEN, CYAN, DIM, RESET } from "./terminal";
const UP = "\x1b[A"; const UP = "\x1b[A";
const DOWN = "\x1b[B"; const DOWN = "\x1b[B";
const LEFT = "\x1b[D";
const ALT_UP = "\x1bOA"; const ALT_UP = "\x1bOA";
const ALT_DOWN = "\x1bOB"; const ALT_DOWN = "\x1bOB";
const ALT_LEFT = "\x1bOD";
const SPACE = " "; const SPACE = " ";
const ENTER = "\r"; const ENTER = "\r";
const CTRL_C = "\x03"; const CTRL_C = "\x03";
const BACKSPACE = "\x7f";
export const BACK = Symbol("prompt-back");
export type PromptBack = typeof BACK;
export interface Choice<T> { export interface Choice<T> {
label: string; label: string;
@@ -19,6 +25,7 @@ interface BasePromptOptions {
title: string; title: string;
subtitle?: string; subtitle?: string;
cancelMessage?: string; cancelMessage?: string;
allowBack?: boolean;
} }
interface SinglePromptOptions<T> extends BasePromptOptions { interface SinglePromptOptions<T> extends BasePromptOptions {
@@ -55,11 +62,13 @@ function padLabel(label: string, width: number) {
return label + " ".repeat(Math.max(1, width - visibleLength(label))); return label + " ".repeat(Math.max(1, width - visibleLength(label)));
} }
function controls(mode: "single" | "multi") { function controls(mode: "single" | "multi", showBackHint = true) {
if (mode === "single") { if (mode === "single") {
return `${DIM}↑/↓ navigate · enter/space select · ctrl+c cancel${RESET}`; const backHint = showBackHint ? " · ←/backspace back" : "";
return `${DIM}↑/↓ navigate · enter/space select${backHint} · ctrl+c cancel${RESET}`;
} }
return `${DIM}↑/↓ navigate · space toggle · enter confirm · 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) { function renderPrompt(lines: string[], previousLines: number) {
@@ -89,6 +98,9 @@ function clearPrompt(lines: number) {
function normalizeKey(key: string, escapeBuf: string) { function normalizeKey(key: string, escapeBuf: string) {
if (key === UP || key === ALT_UP) return { action: "up", escapeBuf: "" }; if (key === UP || key === ALT_UP) return { action: "up", escapeBuf: "" };
if (key === DOWN || key === ALT_DOWN) return { action: "down", 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 === SPACE) return { action: "space", escapeBuf: "" };
if (key === ENTER) return { action: "enter", escapeBuf: "" }; if (key === ENTER) return { action: "enter", escapeBuf: "" };
if (key === CTRL_C) return { action: "cancel", escapeBuf: "" }; if (key === CTRL_C) return { action: "cancel", escapeBuf: "" };
@@ -101,6 +113,9 @@ function normalizeKey(key: string, escapeBuf: string) {
const next = escapeBuf + key; const next = escapeBuf + key;
if (next === UP || next === ALT_UP) return { action: "up", escapeBuf: "" }; if (next === UP || next === ALT_UP) return { action: "up", escapeBuf: "" };
if (next === DOWN || next === ALT_DOWN) return { action: "down", escapeBuf: "" }; if (next === DOWN || next === ALT_DOWN) return { action: "down", escapeBuf: "" };
if (next === LEFT || next === ALT_LEFT) {
return { action: "back", escapeBuf: "" };
}
return { return {
action: null, action: null,
escapeBuf: /^[A-Za-z~]$/.test(key) || next.length > 8 ? "" : next, escapeBuf: /^[A-Za-z~]$/.test(key) || next.length > 8 ? "" : next,
@@ -126,7 +141,7 @@ function createLines<T>(
]; ];
if (options.subtitle) lines.push(` ${DIM}${options.subtitle}${RESET}`); if (options.subtitle) lines.push(` ${DIM}${options.subtitle}${RESET}`);
lines.push(` ${controls(mode)}`, ""); lines.push(` ${controls(mode, options.allowBack !== false)}`, "");
for (let i = 0; i < options.items.length; i++) { for (let i = 0; i < options.items.length; i++) {
const item = options.items[i]!; const item = options.items[i]!;
@@ -156,7 +171,7 @@ function ensureTTY(title: string) {
export async function selectOne<T>( export async function selectOne<T>(
options: SinglePromptOptions<T>, options: SinglePromptOptions<T>,
): Promise<T | null> { ): Promise<T | null | PromptBack> {
ensureTTY(options.title); ensureTTY(options.title);
let cursor = 0; let cursor = 0;
@@ -178,7 +193,7 @@ export async function selectOne<T>(
render(); render();
return new Promise((resolve) => { return new Promise((resolve) => {
const finish = (value: T | null) => { const finish = (value: T | null | PromptBack) => {
process.stdin.setRawMode(wasRaw === true); process.stdin.setRawMode(wasRaw === true);
process.stdin.pause(); process.stdin.pause();
process.stdin.removeListener("data", onData); process.stdin.removeListener("data", onData);
@@ -195,6 +210,9 @@ export async function selectOne<T>(
escapeBuf = result.escapeBuf; escapeBuf = result.escapeBuf;
if (result.action === "cancel") return finish(null); if (result.action === "cancel") return finish(null);
if (result.action === "back" && options.allowBack !== false) {
return finish(BACK);
}
if (result.action === "up" && cursor > 0) { if (result.action === "up" && cursor > 0) {
cursor--; cursor--;
render(); render();
@@ -212,7 +230,7 @@ export async function selectOne<T>(
export async function selectMany<T>( export async function selectMany<T>(
options: MultiPromptOptions<T>, options: MultiPromptOptions<T>,
): Promise<T[] | null> { ): Promise<T[] | null | PromptBack> {
ensureTTY(options.title); ensureTTY(options.title);
const items: Choice<T | null>[] = options.selectAllLabel const items: Choice<T | null>[] = options.selectAllLabel
@@ -267,7 +285,7 @@ export async function selectMany<T>(
render(); render();
return new Promise((resolve) => { return new Promise((resolve) => {
const finish = (value: T[] | null) => { const finish = (value: T[] | null | PromptBack) => {
process.stdin.setRawMode(wasRaw === true); process.stdin.setRawMode(wasRaw === true);
process.stdin.pause(); process.stdin.pause();
process.stdin.removeListener("data", onData); process.stdin.removeListener("data", onData);
@@ -284,6 +302,9 @@ export async function selectMany<T>(
escapeBuf = result.escapeBuf; escapeBuf = result.escapeBuf;
if (result.action === "cancel") return finish(null); if (result.action === "cancel") return finish(null);
if (result.action === "back" && options.allowBack !== false) {
return finish(BACK);
}
if (result.action === "up" && cursor > 0) { if (result.action === "up" && cursor > 0) {
cursor--; cursor--;
render(); render();
+4 -2
View File
@@ -1,11 +1,12 @@
import type { FileEntry } from "./types"; import type { FileEntry } from "./types";
import { BOLD, GREEN, YELLOW, RESET } from "./terminal"; import { BOLD, GREEN, YELLOW, RESET } from "./terminal";
import { selectMany } from "./menu"; 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) {
@@ -30,6 +31,7 @@ export async function selectFiles(
}); });
if (selected === null) process.exit(1); if (selected === null) process.exit(1);
if (selected === BACK) return BACK;
if (selected.length > 0) { if (selected.length > 0) {
process.stdout.write( process.stdout.write(