Files
apes-sidebar/src/client/changes/CommitModelSettings.tsx
T
Menghuan1918andrainmanhhh 6615f63602 feat(git): AI 生成提交信息(Ctrl+G、多行提交框、可固定生成模型)
移植 PR #642(refs/tmp/pr642 → 当前 dev 重写,不机械解冲突)。

复用(与 dev clean 或纯逻辑无冲突面):
- src/commit-message.ts 纯逻辑模块原样取用:prompt 契约、provider/model 路由
  词法、reasoning effort 取最低档、宽容的 provider/model 目录读取、建议文本清理。
- host 半边(git.suggest-message / git.commit-model / git.models 三条路由 +
  pinned / 会话 / agent-default-model 三级路由解析)按 dev 现状重贴:ctx.llm、
  agentDefaultModel、git.status/diff、sessionId 参数名逐条对照本机 0.2.0-rc.1 核实。
- 设置缝复用 dev 已有的 SidebarSettingsRenderProps + settings.render(PR 侧写在
  同一接口上,零适配)。
- 三个新 spec 原样取用,只有 git-suggest 的 harness 改了「visible 默认 true」——
  dev 的共享 git-status store 只在可见时订阅,不可见时 snapshot 恒 null,
  commit bar 根本不渲染(PR 作者当年跑绿的是本机未提交状态)。

丢弃 / 按 dev 重写:
- PR 的 GitLens 客户端半边基于 #471 重写前的 GitLens:提交栏在 dev 已是
  primitives Input + commitBar/commitRow/commitInput,且 Ctrl/Cmd+Enter 提交
  已存在(重复实现)。现按 dev 的 .commitRow 结构重做多行 textarea + 自动增高,
  保留既有快捷键,并追加 Ctrl/Cmd+G。
- 六个 Icon*16 具名导入在 0.2.0-rc.1 一个不剩(§3.1 第 2 条:缺导出 = render 期
  Element type is invalid),改用 IconSparkleRegular size={14}。
- CSS 类名跟随 dev 的 commit* 前缀(PR 的 gitCommit*/gitSuggest*/gitError 在 dev
  已不存在;错误行改走 dev 的 Notice,不新增自绘错误样式)。
- 消息 source kind 用生产者自有值 plugin:dsh-better-sidebar:0.1.7 起
  kind:'plugin' 会被 format v4 硬拒。
- locales:dev 已删 commitError 锚点,且 19 份词典与 locales.ts 必须键集相等,
  故按各文件在 commit 行后逐份插入,不整份取 PR 侧。

验收:pnpm typecheck 0;pnpm exec vitest run --maxWorkers=3 全绿(163 files /
1836 tests);新增 spec 单独跑绿;真机挂载冒烟未跑(见报告)。

Refs #642

Co-authored-by: rainmanhhh <13862623+rainmanhhh@users.noreply.github.com>
2026-10-05 02:19:58 +08:00

258 lines
11 KiB
TypeScript

/**
* The Git card's pinned-model setting: which provider/model drafts commit
* messages. It renders through the descriptor's `settings.render` seam because
* the option list is DISCOVERED at runtime (`git.models` asks the harness what
* it can serve) — a declarative toggle can only declare a static option set.
*
* Shape: a "follow the conversation" SWITCH plus a searchable `provider/model`
* box. Following is the default (an empty stored value), and it disables the
* box: while the panel tracks the conversation there is nothing to pin. The
* box is a filter over the discovered catalog AND accepts a hand-typed
* `provider/model` route, so a model the harness never advertises stays
* selectable.
*
* The value lives in the descriptor's own plugin-settings blob
* (`pluginSettings.git.commitModel`); empty/absent means "follow".
*/
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { Menu } from '@deepseek-ai/dsh-client-ui-primitives'
import { api, type GitModelCatalog } from '../api.ts'
import { t } from '../locales.ts'
import type { SidebarSettingsRenderProps } from '../service.ts'
import { Notice } from '../ui/index.ts'
import css from './changes.module.css'
/** The plugin-owned setting key (mirrored host-side in src/index.ts). */
export const COMMIT_MODEL_SETTING_KEY = 'commitModel'
/** Cap of the filtered candidate list (a huge catalog stays scrollable). */
const OPTION_LIMIT = 40
/**
* One selectable route. The displayed text IS the route (`provider/model`) —
* the same string the search box filters on and the host dispatches on — so a
* catalog entry and the harness default read identically instead of one being
* a decorated "<provider> · <model>" rendering.
*/
interface ModelOption {
/** The stored and displayed `provider/model` route. */
value: string
/** Source badge text ('' for a plain catalog entry). */
note: string
}
/** The stored `provider/model` route of one option ('' = follow). */
function routeValue(provider: string, model: string): string {
return `${provider}/${model}`
}
/** Build the candidate list: harness default and used routes first, then the
* adapter catalog. De-duplicated by route, newest/most-relevant first. */
function optionsOf(catalog: GitModelCatalog | null): ModelOption[] {
if (catalog === null) return []
const options: ModelOption[] = []
const seen = new Set<string>()
const push = (provider: string, model: string, note: string): void => {
const value = routeValue(provider, model)
if (seen.has(value)) return
seen.add(value)
options.push({ value, note })
}
if (catalog.default !== undefined) {
push(catalog.default.provider, catalog.default.model, t('commitModelDefaultNote'))
}
for (const route of catalog.recent) {
push(route.provider, route.model, t('commitModelRecentNote'))
}
for (const provider of catalog.providers) {
for (const model of provider.models) {
push(provider.provider, model.id, '')
}
}
return options
}
export function CommitModelSettings(props: SidebarSettingsRenderProps) {
const { store, pluginSettings, updatePluginSetting } = props
const sessionId = store.getSnapshot().sessionId
const stored = pluginSettings[COMMIT_MODEL_SETTING_KEY]
const value = typeof stored === 'string' ? stored : ''
const [catalog, setCatalog] = useState<GitModelCatalog | null>(null)
const [error, setError] = useState<string | null>(null)
// Following is the DEFAULT (an empty stored value). The switch keeps its own
// draft so it can be turned off before any model is picked — the stored value
// then stays empty and generation still follows the conversation.
const [follow, setFollow] = useState(value === '')
// The search box is a FILTER, never a projection of the stored value: after
// picking a route it clears, so reopening the menu lists every candidate
// instead of filtering down to the one already selected. The selection
// itself is shown on its own line (below).
const [query, setQuery] = useState('')
const [invalid, setInvalid] = useState(false)
const [open, setOpen] = useState(false)
useEffect(() => {
if (sessionId === undefined) return
let cancelled = false
setError(null)
api.gitModels({ sessionId })
.then(view => { if (!cancelled) setCatalog(view) })
.catch((reason: unknown) => {
if (cancelled) return
setError(reason instanceof Error ? reason.message : String(reason))
})
return () => { cancelled = true }
}, [sessionId])
const inputRef = useRef<HTMLInputElement | null>(null)
const options = useMemo(() => optionsOf(catalog), [catalog])
const needle = query.trim().toLowerCase()
const matches = useMemo(
() => (needle === ''
? options.slice(0, OPTION_LIMIT)
: options.filter(option => option.value.toLowerCase().includes(needle)).slice(0, OPTION_LIMIT)),
[options, needle],
)
const listOpen = open && !follow && matches.length > 0
/**
* The primitives Menu lays its portalled list out from the anchor rect but
* sizes it from CSS (`min-width: 218px`, `max-width: 360px`) — it has no
* "match the anchor width" mode, and its `className` only reaches the root
* wrapper. The list is a direct child of <body> (portal), so it is reachable
* here: while open, pin its width to the input's own width so the dropdown
* is never narrower than the box it drops out of.
*/
useLayoutEffect(() => {
if (!listOpen) return
const sync = (): void => {
const input = inputRef.current
if (input === null) return
const width = Math.round(input.getBoundingClientRect().width)
if (width <= 0) return
const children: Element[] = Array.from(document.body.children)
for (let index = children.length - 1; index >= 0; index--) {
const element = children[index]
if (!(element instanceof HTMLElement) || element.getAttribute('role') !== 'menu') continue
// A floor, never a ceiling: the card may grow past the input to fit a
// long route plus its source badge (the Menu's own max-width caps it).
element.style.minWidth = `${width}px`
element.style.removeProperty('max-width')
return
}
}
sync()
window.addEventListener('resize', sync)
return () => { window.removeEventListener('resize', sync) }
}, [listOpen, matches.length])
/** Commit one typed/clicked route (both halves must be non-empty). */
const commitRoute = (raw: string): boolean => {
const trimmed = raw.trim()
const slash = trimmed.indexOf('/')
const provider = slash <= 0 ? '' : trimmed.slice(0, slash).trim()
const model = slash < 0 ? '' : trimmed.slice(slash + 1).trim()
if (provider === '' || model === '') {
setInvalid(true)
return false
}
setInvalid(false)
setQuery('')
updatePluginSetting(COMMIT_MODEL_SETTING_KEY, routeValue(provider, model))
return true
}
const pick = (option: ModelOption): void => {
// Clear the filter (see the `query` note above) and close.
setQuery('')
setInvalid(false)
setOpen(false)
updatePluginSetting(COMMIT_MODEL_SETTING_KEY, option.value)
}
return (
<div className={css.commitModel}>
<div className={css.commitModelRow}>
<span className={css.commitModelTitle}>{t('commitModelTitle')}</span>
<label className={css.commitModelSwitch}>
<input
type="checkbox"
checked={follow}
onChange={(event) => {
const next = event.currentTarget.checked
setFollow(next)
if (next) {
setQuery('')
setInvalid(false)
updatePluginSetting(COMMIT_MODEL_SETTING_KEY, '')
}
}}
/>
<span>{t('commitModelFollow')}</span>
</label>
</div>
<div className={css.commitModelDesc}>{t('commitModelDesc')}</div>
{/* The candidate menu is PORTALLED (the primitives Menu), like every
other dropdown in the side card: this panel renders inside the
settings modal, whose body clips overflow, so an in-flow or absolute
list would be cut off at the dialog edge. The input itself is the
menu's anchor, so typing filters the open menu in place. */}
<div className={css.commitModelBox}>
{!follow && value !== '' && (
<div className={css.commitModelCurrent}>{t('commitModelCurrent', { route: value })}</div>
)}
<Menu
open={open && !follow && matches.length > 0}
className={css.commitModelMenuRoot}
anchor={(
<input
ref={inputRef}
className={css.commitModelInput}
type="text"
value={query}
disabled={follow}
placeholder={t('commitModelSearchPlaceholder')}
aria-label={t('commitModelSearchPlaceholder')}
onChange={(event) => {
setQuery(event.currentTarget.value)
setInvalid(false)
setOpen(true)
}}
onFocus={() => { setOpen(true) }}
onKeyDown={(event) => {
if (event.key === 'Enter') {
if (commitRoute(event.currentTarget.value)) setOpen(false)
}
if (event.key === 'Escape') setOpen(false)
}}
/>
)}
items={matches.map(option => ({
id: option.value,
label: (
<span className={css.commitModelOption}>
<span className={css.commitModelOptionText}>{option.value}</span>
{option.note !== '' && <span className={css.commitModelOptionNote}>{option.note}</span>}
</span>
),
}))}
selectedId={value === '' ? undefined : value}
onSelect={(id) => {
const option = matches.find(candidate => candidate.value === id)
if (option !== undefined) pick(option)
}}
onClose={() => { setOpen(false) }}
portal
/>
</div>
{invalid && <Notice kind="error" tone="inline">{t('commitModelInvalid')}</Notice>}
{catalog !== null && !catalog.llm && <Notice kind="hint" tone="inline">{t('commitModelUnavailable')}</Notice>}
{catalog !== null && catalog.llm && options.length === 0 && (
<Notice kind="hint" tone="inline">{t('commitModelEmpty')}</Notice>
)}
{error !== null && <Notice kind="error" tone="inline">{error}</Notice>}
</div>
)
}