Open-source, local-first AI agent for real work.
TypeScript agent-toolsai-agentbyokcoding-agentcross-platformdesktop-appdeveloper-toolselectronharnessknowledge-basellmwikilocal-firstmcpplugin-systemprivacy-firstskillstypescriptvettaworkflow-automation
import { useActiveConversation, useTranslation } from "@vetta-org/plugin-sdk";
import { useCallback, useState } from "react";
import { parseStatus } from "../git/parseStatus";
import { stagePaths, statusPorcelain, unstageAll } from "../git/run";
import { emitRefreshSignal, notifyError, openPanel, requestCommit } from "../git/runtime";
import { requestReview } from "../git/turnReview";
import { touchedPaths } from "../git/turnSnapshot";
import { ConfirmDialog } from "./ConfirmDialog";
import { CommitIcon, GitIcon, ReviewIcon, UndoIcon } from "./icons";
import { TurnFileRow } from "./TurnFileRow";
import { useTurnFileActions } from "./useTurnFileActions";
import { useTurnReview } from "./useTurnReview";
/** Most items the card shows inline; beyond this it collapses to a "view all" row. */
const MAX_ITEMS = 8;
const FOOTER_BUTTON =
"inline-flex h-6 items-center gap-1 rounded-md px-1.5 text-[12px] font-medium outline-none transition-colors focus-visible:ring-1 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50";
/**
* 消息列表底部的「本轮改动」卡(turn 卡,不绑定 tool 调用)。
*
* 只列**这一轮**改过内容的文件:回合开始与结束各给工作区拍一次内容快照(见
* turnReview),两棵树之间的差异就是本轮改动——之前已改未提交的文件、用户自己的
* 改动都不会混进来。每一行可以单独撤回 / 恢复;底部是审阅、整轮撤回与只提交本轮。
* turn 进行中隐藏,切会话再切回来仍能复显。
*/
export function GitTurnCard(): JSX.Element | null {
const { cwd } = useActiveConversation();
const { t } = useTranslation();
const review = useTurnReview(cwd);
const files = useTurnFileActions(cwd);
const [committing, setCommitting] = useState(false);
// 暂存区里已经有别的文件,等用户裁决是「一并提交」还是「只提交本轮」。
const [askMixed, setAskMixed] = useState<{ root: string; paths: string[] } | null>(null);
/**
* Stage exactly this turn's files, then hand over to the panel's commit box.
*
* The turn card is the only surface that knows which files belong to THIS turn
* (the panel only sees every uncommitted file), which is what makes a one-click
* atomic commit possible here.
*/
const stageAndCommit = useCallback(async (root: string, paths: string[], resetIndexFirst: boolean): Promise<void> => {
setCommitting(true);
try {
if (resetIndexFirst) await unstageAll(root);
await stagePaths(root, paths);
emitRefreshSignal();
openPanel();
requestCommit(root);
} catch (error) {
notifyError(t("turnCard.commitFailed"), error);
} finally {
setCommitting(false);
}
}, [t]);
const kept = review ? review.files.filter((file) => !review.reverted.has(file.path)) : [];
const commitThisTurn = useCallback((): void => {
if (!review || committing || kept.length === 0) return;
const paths = touchedPaths(kept);
void (async () => {
try {
const groups = parseStatus(await statusPorcelain(review.root));
// 暂存区里已经躺着别的文件:直接提交会把它们一起带走,违背原子提交。
if (groups.staged.length > 0) {
setAskMixed({ root: review.root, paths });
return;
}
await stageAndCommit(review.root, paths, false);
} catch (error) {
notifyError(t("turnCard.commitFailed"), error);
}
})();
}, [review, committing, kept, stageAndCommit, t]);
if (!review || !cwd) return null;
const sorted = [...review.files].sort((a, b) => a.path.localeCompare(b.path));
const overflow = sorted.length > MAX_ITEMS;
const shown = overflow ? sorted.slice(0, MAX_ITEMS - 1) : sorted;
const remaining = sorted.length - shown.length;
const revertedCount = review.reverted.size;
const allReverted = kept.length === 0;
const showReview = (path: string | null = null): void => {
openPanel();
requestReview(cwd, path);
};
return (
// 去线留白:卡片只保留一层淡边与内边距,内部各区靠间距分隔,不再用分隔线切块。
<div className="rounded-lg border border-border/60 bg-card/60 p-2 text-[12px]">
<div className="flex items-center gap-2 px-1 py-0.5">
<GitIcon className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
<button
type="button"
onClick={() => showReview()}
title={t("turnCard.open")}
className="min-w-0 truncate text-left font-medium text-foreground hover:underline"
>
{t("turnCard.summary", { count: sorted.length })}
</button>
{revertedCount > 0 && (
<span className="shrink-0 text-[11px] text-muted-foreground">{t("turnCard.revertedCount", { count: revertedCount })}</span>
)}
<span className="ml-auto flex shrink-0 items-center gap-1.5 font-medium tabular-nums">
<span className="text-emerald-500/90">+{review.additions}</span>
<span className="text-rose-500/90">−{review.deletions}</span>
</span>
</div>
<div className="mt-1.5 flex flex-col">
{shown.map((file) => (
<TurnFileRow
key={file.path}
file={file}
reverted={review.reverted.has(file.path)}
commentCount={review.comments.filter((comment) => comment.path === file.path).length}
busy={files.busy}
onOpen={() => showReview(file.path)}
onToggle={() => files.apply([file.path], review.reverted.has(file.path) ? "restore" : "revert")}
/>
))}
{overflow && (
<button
type="button"
onClick={() => showReview()}
className="rounded-md px-1 py-1 text-left text-muted-foreground transition-colors hover:bg-accent/40 hover:text-foreground"
>
{t("turnCard.viewAll", { count: remaining })}
</button>
)}
</div>
<div className="mt-1.5 flex items-center gap-0.5 px-0.5">
<button
type="button"
onClick={() => showReview()}
className={`${FOOTER_BUTTON} text-foreground hover:bg-accent/50`}
>
<ReviewIcon className="h-3.5 w-3.5" />
{t("turnCard.review")}
</button>
{allReverted ? null : (
<button
type="button"
disabled={files.busy}
onClick={() => files.apply(kept.map((file) => file.path), "revert")}
className={`${FOOTER_BUTTON} text-muted-foreground hover:bg-accent/50 hover:text-foreground`}
>
<UndoIcon className="h-3.5 w-3.5" />
{t("turnCard.revertAll")}
</button>
)}
<button
type="button"
disabled={committing || allReverted}
onClick={commitThisTurn}
className={`${FOOTER_BUTTON} ml-auto text-sky-500 hover:bg-sky-500/10 hover:text-sky-400`}
>
<CommitIcon className="h-3.5 w-3.5" />
{t("turnCard.commitTurn")}
</button>
</div>
{files.dialog}
{/* 默认「仅提交本轮」:取消暂存只动索引、不丢改动,错了点两下就回来;
「一并提交」错了则要改历史。默认值必须站在可逆的那一侧。 */}
<ConfirmDialog
open={askMixed !== null}
title={t("turnCard.mixedTitle")}
description={t("turnCard.mixedDescription")}
confirmLabel={t("turnCard.mixedOnlyTurn")}
detail={
<button
type="button"
className="w-full rounded border border-border px-2 py-1.5 text-left text-[12px] text-muted-foreground transition-colors hover:bg-accent"
onClick={() => {
const target = askMixed;
setAskMixed(null);
if (target) void stageAndCommit(target.root, target.paths, false);
}}
>
{t("turnCard.mixedIncludeAll")}
</button>
}
onConfirm={() => {
const target = askMixed;
setAskMixed(null);
if (target) void stageAndCommit(target.root, target.paths, true);
}}
onCancel={() => setAskMixed(null)}
/>
</div>
);
}