feat(changes): AI commit-message suggestion + push/pull in the git lens

Port PR #434's two Git-panel features onto the current dev structure. The
PR's third commit (file-tree context-menu rename) is dropped: dev already
has it (#550, e39ea92) with a different `fs.rename` payload contract
(`name`, not `newName`) and its own i18n keys, so keeping it would break the
route and duplicate dictionary keys (TS1117).

Why this is a rewrite rather than the PR's diff applied:

- The client carrier moved. `src/client/GitView.tsx` was deleted by 24a3813
  (the unified "changes" tab), so the client half lands in
  `src/client/changes/GitLens.tsx` and follows that file's conventions: the
  bar's single `actionError` status line instead of a second `remoteError`
  channel, `runAction` for the git mutations, the kit's `IconButton`, and the
  host's own `IconSparkleRegular` rather than a new react-icons import.
- The host route could not read `Session.events` at all: it was renamed to
  `snapshotEvents()` in DSH 0.1.2-alpha.4, and 0.2.0-rc.1 marks it `new calls
  are prohibited`. `git.suggest-message` resolves its provider/model from the
  incrementally folded `Session.requestHeader()` — the same `request/header`
  record the PR was after, but O(new events) instead of a full reverse scan.
  `SidebarSessionStore` gains that face; `snapshotEvents()` is marked
  deprecated there.
- Message source is the plugin's own producer kind
  (`plugin:dsh-better-sidebar`, the existing `MessageSourceMap` augmentation):
  a bare `plugin` kind is rejected outright in session format v4.
- The commit row stays where dev put it. PR moved it to the top of the panel
  because it was buried between the two file lists; dev's commit bar is
  already a `position: sticky` footer, so that problem is gone — and
  `changes-tab.spec.tsx` pins the bar's status line to the message row.

Beyond the port, the route reads `assembler.finish` so a provider-side failure
is reported with the provider's own reason instead of "the model returned an
empty message", bounds the call with a 30s deadline, and truncates the diff at
12K characters.

Refs #434

Co-authored-by: Ahdong&Shouey <131278957+LiuStar656@users.noreply.github.com>
This commit is contained in:
Menghuan1918andAhdong&Shouey committed 2026-10-05 02:11:10 +08:00
1 parent 3bd5e1947b
commit 7cf3d609ff
30 files changed
+1155 -6

No files matched your search

+69 -6
View File
@@ -29,16 +29,16 @@
import { memo, useCallback, useEffect, useMemo, useRef, useState, type MouseEvent, type ReactNode } from 'react'
import {
Button, IconChevronRightOutlineRegular, IconCodeOutlineRegular, IconCopyOutlineRegular, IconPlusOutlineRegular,
IconTrashOutlineRegular, Input, Menu, writeClipboard,
IconSparkleRegular, IconTrashOutlineRegular, Input, Menu, writeClipboard,
} from '@deepseek-ai/dsh-client-ui-primitives'
import type { Context } from '../../context-types.ts'
import type { GitLogEntry, GitStatusEntry, GitStatusResult, GitWorktree, SessionScope } from '../api.ts'
import { api } from '../api.ts'
import { api, SidebarApiError } from '../api.ts'
import { builtinFileIcon, builtinFolderIcon } from '../file-icons.tsx'
import { usePolling } from '../use-polling.ts'
import { baseName, relativeTo } from '../paths.ts'
import { resolveSidebarPath } from '../paths.ts'
import { relativeTime, t } from '../locales.ts'
import { relativeTime, isZh, t } from '../locales.ts'
import type { SidebarDiffRef, SidebarStore } from '../state.ts'
import {
ConfirmDialog, IconButton, Notice, SectionHeader, StatusBadge, invalidateGitStatus, statusOfXY,
@@ -296,6 +296,8 @@ export function GitLens(props: GitLensProps) {
const [logFailed, setLogFailed] = useState(false)
const [commitMsg, setCommitMsg] = useState('')
const [busy, setBusy] = useState(false)
/** Whether a commit-message suggestion is streaming from the host LLM. */
const [suggesting, setSuggesting] = useState(false)
/** The commit bar's ONE status line (commit / stage / discard / revert). */
const [actionError, setActionError] = useState<string | null>(null)
/** The lens-level error banner (refresh / branch switch / history paging). */
@@ -533,6 +535,11 @@ export function GitLens(props: GitLensProps) {
const stageError = (reason: unknown): string => t('changesStageFailed', { message: errorMessage(reason) })
/** The remote actions prepend their own label: git's message for a missing
* upstream or a diverged branch says nothing about which button ran. */
const pushError = (reason: unknown): string => `${t('pushError')}: ${errorMessage(reason)}`
const pullError = (reason: unknown): string => `${t('pullError')}: ${errorMessage(reason)}`
const stageEntry = (path: string, staged: boolean): void => {
void runAction(
() => (staged ? api.gitUnstage(gitScopeNow(), path, selectedWorktree) : api.gitStage(gitScopeNow(), path, selectedWorktree)),
@@ -549,10 +556,45 @@ export function GitLens(props: GitLensProps) {
const commit = (): void => {
const message = commitMsg.trim()
if (message === '' || busy) return
if (message === '' || busy || suggesting) return
void runAction(() => api.gitCommit(gitScopeNow(), message, selectedWorktree), errorMessage, () => { setCommitMsg('') })
}
/** Ask the host to write a Conventional-Commits message from the pending
* changes and fill the box. The text stays editable and re-generable, and
* the call is a read (no git mutation), so it neither locks the bar's git
* actions nor refreshes the status — only its own `suggesting` flag. */
const suggestMessage = async (): Promise<void> => {
if (busy || suggesting) return
setSuggesting(true)
setActionError(null)
try {
const { message } = await api.gitSuggestMessage(gitScopeNow(), isZh() ? 'zh' : 'en', selectedWorktree)
setCommitMsg(message)
} catch (reason) {
// An empty pending set is the button's own precondition racing with a
// status poll, not a failure worth the provider's message.
setActionError(reason instanceof SidebarApiError && reason.code === 'git-suggest-empty'
? t('suggestCommitEmpty')
: `${t('suggestCommitError')}: ${errorMessage(reason)}`)
} finally {
setSuggesting(false)
}
}
/** Push the selected checkout's branch. Local commits and the remote ride the
* SAME status line as every other git action — the failures share one cause
* (no upstream, auth, divergence), so a second error channel would only
* split the user's attention. */
const push = (): void => {
void runAction(() => api.gitPush(gitScopeNow(), selectedWorktree), pushError)
}
/** Pull into the selected checkout (fast-forward only host-side). */
const pull = (): void => {
void runAction(() => api.gitPull(gitScopeNow(), selectedWorktree), pullError)
}
/** Switching the selected checkout changes which rows are legitimate to act
* on: clear the derived surfaces before the asynchronous refresh resolves
* so a stale history row can never run over the new checkout's repository. */
@@ -879,22 +921,43 @@ export function GitLens(props: GitLensProps) {
className={css.commitInput}
placeholder={t('commitPlaceholder')}
value={commitMsg}
disabled={busy}
disabled={busy || suggesting}
onChange={(event) => { setCommitMsg(event.target.value); setActionError(null) }}
onKeyDown={(event) => {
if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') commit()
}}
/>
{/* The suggestion reads the pending changes, so it is offered
exactly while there is something to describe (the host also
refuses an empty set). The accessible name follows the state —
the spinner alone says nothing to a screen reader. */}
<IconButton
size="sm"
label={suggesting ? t('generatingCommitMessage') : t('generateCommitMessage')}
disabled={busy || suggesting || (stagedEntries.length === 0 && unstagedEntries.length === 0)}
icon={suggesting ? <span className={css.suggestSpinner} /> : <IconSparkleRegular size={14} />}
onClick={() => { void suggestMessage() }}
/>
<Button
variant="primary"
size="sm"
disabled={busy || commitMsg.trim() === '' || stagedEntries.length === 0}
disabled={busy || suggesting || commitMsg.trim() === '' || stagedEntries.length === 0}
onClick={commit}
>
{t('commit')}
</Button>
</div>
{/* The bar's ONE status line stays directly under the message row it
belongs to (the placement `changes-tab.spec.tsx` pins), so it keeps
reading as "your commit/stage/push failed" rather than as a tab-level
banner. */}
{actionError !== null && <Notice kind="error" tone="inline" role="alert">{actionError}</Notice>}
{/* Shipping the commit without leaving the panel: two hairlines at
the foot of the bar, sharing its busy lock and status line. */}
<div className={css.commitRemoteRow}>
<Button variant="ghost" size="sm" disabled={busy || suggesting} onClick={push}>{t('push')}</Button>
<Button variant="ghost" size="sm" disabled={busy || suggesting} onClick={pull}>{t('pull')}</Button>
</div>
</div>
)}
+27
View File
@@ -210,6 +210,33 @@
.commitRow { display: flex; align-items: center; gap: 6px; }
.commitInput { flex: 1; min-width: 0; }
/* Push / Pull: the two remote actions sit at the foot of the commit bar, so a
commit can be shipped without leaving the panel. */
.commitRemoteRow { display: flex; align-items: center; gap: 6px; }
/* The inline spinner the suggest button swaps in while the host streams a
message: same 14px footprint as the sparkle it replaces, so the row's layout
does not shift mid-generation. */
.suggestSpinner {
display: block;
width: 14px;
height: 14px;
border: 1.5px solid var(--dsw-alias-border-l2);
border-top-color: var(--dsw-alias-label-primary);
border-radius: 50%;
animation: dsh-suggest-spin 800ms linear infinite;
}
@keyframes dsh-suggest-spin {
to { transform: rotate(360deg); }
}
/* Respect a reduced-motion preference: the button still shows its busy state
(the spinner replaces the icon), it just stops rotating. */
@media (prefers-reduced-motion: reduce) {
.suggestSpinner { animation: none; }
}
/* ── History ───────────────────────────────────────────────────────────── */
/* A history row: line 1 = short-hash badge + subject, line 2 = ref pills +