✨ feat(vscode): 问 AI 和重写注释各用各的 profile,设置里加重写注释默认模型

This commit is contained in:
cruldra committed 2026-10-12 16:30:41 +11:00
1 parent 925f1ed39a
commit b83c445dd0
25 files changed
+363 -111

No files matched your search

@@ -1,8 +1,9 @@
import type { DiffBase, OpenedFrom } from '../diff/diffBase'
import type { ProfileChoice } from '../qa/askProfiles'
import type { RewriteRequest } from '../qa/codeQa'
import type { ExtensionToWebview, WebviewToExtension } from './messages'
import { describe, expect, it, vi } from 'vitest'
import { workspace } from 'vscode'
import { window, workspace } from 'vscode'
import { PreviewEditorProvider } from './previewEditor'
vi.mock('vscode', () => ({
@@ -49,7 +50,7 @@ function preview(rewrite: (uri: unknown, request: RewriteRequest) => Promise<str
const uri = { fsPath: '/workspace/web/a.ts', scheme: 'file', toString: () => 'file:///workspace/web/a.ts' }
const document = { uri, isDirty: dirty, lineAt: (line: number) => ({ text: SOURCE[line] }), lineCount: SOURCE.length, eol: 1 }
vi.mocked(workspace.openTextDocument).mockResolvedValue(document as never)
const qa = { imageRoot: () => undefined, onDidChange: subscribe, onDidAnswer: subscribe, onDidRewrite: subscribe, profiles: { subscribe }, rewrite: vi.fn(rewrite) }
const qa = { imageRoot: () => undefined, onDidChange: subscribe, onDidAnswer: subscribe, onDidRewrite: subscribe, profiles: { ask: { subscribe }, rewrite: { subscribe } }, rewrite: vi.fn(rewrite) }
const provider = new PreviewEditorProvider(...[
{},
{},
@@ -75,7 +76,7 @@ function preview(rewrite: (uri: unknown, request: RewriteRequest) => Promise<str
describe('预览里重写注释', () => {
it('点按钮:发技能命令、开新会话,记在这一轮上的就是这句命令;开不了时回一条带 askId 的失败,能再试', async () => {
const error = '文件问答模型不可用:official。请重新选择。'
const error = '重写注释模型不可用:official。请重新选择。'
const editor = preview(async () => error)
try {
await editor.send({ type: 'rewriteComment', askId: 'r1', anchor: 'block', images: [] })
@@ -139,8 +140,24 @@ describe('预览里重写注释', () => {
const HEAD_BASE: DiffBase = { label: '上一次提交', key: 'head:web/a.ts', read: async () => '// 改之前', missing: '' }
const OPTIONS = [{ id: 'offical', label: 'offical' }, { id: 'sonnet', label: 'sonnet' }, { id: 'dsflash', label: 'dsflash' }]
/** 一套 profile 选择:眼下选的是 selected,publish 模拟它变了、通知订阅的预览 */
function profileSet(selected: string) {
const listeners = new Set<(choice: ProfileChoice) => void>()
return {
subscribe: (listener: (choice: ProfileChoice) => void) => {
listeners.add(listener)
return { dispose: () => listeners.delete(listener) }
},
choice: async (): Promise<ProfileChoice> => ({ options: OPTIONS, selected }),
select: vi.fn(async (_id: string) => {}),
publish: (choice: ProfileChoice) => listeners.forEach(listener => listener(choice)),
}
}
/** 对比基准只认从「未提交的更改」点开的:从那儿点开的对比上一次提交,别处打开的不比 */
function opening() {
function opening(profiles = { ask: profileSet(''), rewrite: profileSet('') }) {
const subscribe = () => ({ dispose: () => {} })
const diffBase = vi.fn((_uri: unknown, from: OpenedFrom): DiffBase | undefined => from === 'uncommitted' ? HEAD_BASE : undefined)
const qa = {
@@ -148,7 +165,7 @@ function opening() {
onDidChange: subscribe,
onDidAnswer: subscribe,
onDidRewrite: subscribe,
profiles: { subscribe, choice: async () => ({ profiles: [], selected: '' }) },
profiles,
view: () => ({ type: 'qa' }),
}
const provider = new PreviewEditorProvider(...[
@@ -290,3 +307,41 @@ describe('预览对比什么看这个标签页是从哪儿打开的', () => {
expect(diffBase).not.toHaveBeenCalledWith(document.uri, 'uncommitted')
})
})
describe('问 AI 和重写注释的 profile 下拉各管各的', () => {
it('ready 后两套各发一次;哪一套变了只发哪一套', async () => {
const profiles = { ask: profileSet('sonnet'), rewrite: profileSet('dsflash') }
const { provider, document } = opening(profiles)
const page = tab()
provider.resolveCustomTextEditor(document as never, page.panel)
await page.send({ type: 'ready' })
await vi.waitFor(() => {
expect(page.posted()).toContainEqual({ type: 'profiles', kind: 'ask', options: OPTIONS, selected: 'sonnet' })
expect(page.posted()).toContainEqual({ type: 'profiles', kind: 'rewrite', options: OPTIONS, selected: 'dsflash' })
})
page.clear()
profiles.rewrite.publish({ options: OPTIONS, selected: 'offical' })
expect(page.posted()).toContainEqual({ type: 'profiles', kind: 'rewrite', options: OPTIONS, selected: 'offical' })
expect(page.posted()).not.toContainEqual(expect.objectContaining({ type: 'profiles', kind: 'ask' }))
})
it('下拉换了哪一套就只改哪一套;换不成时报错,并把那一套眼下的选择发回去', async () => {
const profiles = { ask: profileSet('sonnet'), rewrite: profileSet('dsflash') }
const { provider, document } = opening(profiles)
const page = tab()
provider.resolveCustomTextEditor(document as never, page.panel)
await page.send({ type: 'ready' })
await page.send({ type: 'selectProfile', kind: 'rewrite', id: 'offical' })
expect(profiles.rewrite.select).toHaveBeenCalledWith('offical')
expect(profiles.ask.select).not.toHaveBeenCalled()
profiles.ask.select.mockRejectedValueOnce(new Error('问答模型不可用:gone。请重新选择。'))
page.clear()
await page.send({ type: 'selectProfile', kind: 'ask', id: 'gone' })
expect(vi.mocked(window.showErrorMessage)).toHaveBeenCalledWith('问答模型不可用:gone。请重新选择。')
expect(page.posted()).toContainEqual({ type: 'profiles', kind: 'ask', options: OPTIONS, selected: 'sonnet' })
expect(page.posted()).not.toContainEqual(expect.objectContaining({ type: 'profiles', kind: 'rewrite' }))
})
})