import type { CommitReviewResult, GeneratedCommitMessage, GitChangeContext } from "../types.ts"; import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent"; const COMMIT_OPTION = "Commit Changes"; const EDIT_OPTION = "Edit Message"; const REGENERATE_OPTION = "Regenerate"; const CANCEL_OPTION = "Cancel"; export interface CommitReviewOptions { signoff?: boolean; } /** 显示生成结果,支持手动编辑消息,并让用户选择提交、重新生成或取消。 */ export async function showCommitReviewDialog( generated: GeneratedCommitMessage, context: GitChangeContext, ctx: ExtensionCommandContext, options: CommitReviewOptions = {}, ): Promise { const sourceLabel = context.resolvedSource === "staged" ? "Staged changes" : "All changes"; let message = generated.message; let edited = false; for (;;) { const editedLabel = edited ? " · edited" : ""; const titleSections = [ `Generated Commit · ${sourceLabel} · ${generated.modelLabel}${editedLabel}`, ]; if (options.signoff) { titleSections.push("DCO sign-off enabled: Git will append a Signed-off-by trailer."); } titleSections.push("", message, "", "Choose the next action:"); const title = titleSections.join("\n"); const selected = await ctx.ui.select(title, [ COMMIT_OPTION, EDIT_OPTION, REGENERATE_OPTION, CANCEL_OPTION, ]); if (selected === EDIT_OPTION) { const editorResult = await ctx.ui.editor("Edit commit message", message); // 取消编辑(undefined)时保留原消息,回到审阅界面。 if (editorResult !== undefined) { const trimmed = editorResult.trim(); if (trimmed.length === 0) { ctx.ui.notify("Commit message cannot be empty; keeping the previous message.", "warning"); } else if (trimmed !== message) { message = trimmed; edited = true; } } continue; } if (selected === COMMIT_OPTION) { return { action: "commit", message }; } if (selected === REGENERATE_OPTION) { return { action: "regenerate", message }; } return { action: "cancel", message }; } }