fix(desktop): selection回填只留用户意见+圈选截图,去掉selector/DOM/页面噪音

圈选标注截图作为引用承载元素位置;输入框文字只放用户修改意见+具体改动(人话),
不再写 selector/nthPath/computedStyles/页面URL/「请在源码中落地」。沿用进输入框+手动发送。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-05-29 18:01:00 +08:00
parent 81ed3e2530
commit ca2de74f87
3 changed files with 24 additions and 11 deletions

View File

@ -32,7 +32,7 @@ describe('subscribePreviewEvents', () => {
const payload = { pageUrl: 'http://x/', element: { selector: '#t', tag: 'h1', classes: [] }, change: { description: '改一下' }, screenshot: { dataUrl: 'data:image/png;base64,AAAA', kind: 'element' } }
listeners['preview://event']!({ payload: JSON.stringify({ v: 1, type: 'selection', payload }) })
expect(prefill).toHaveBeenCalledWith('s1', expect.objectContaining({
text: expect.stringContaining('#t'),
text: expect.stringContaining('改一下'),
attachments: [expect.objectContaining({ type: 'image', data: 'data:image/png;base64,AAAA' })],
}))
})

View File

@ -1,15 +1,26 @@
import { expect, it } from 'vitest'
import { buildSelectionComposerText } from './selectionComposer'
it('renders a readable block with locator + change + source', () => {
it('renders only the user instruction + concrete changes (no selector/DOM/page noise)', () => {
const text = buildSelectionComposerText({
pageUrl: 'http://127.0.0.1:4321/preview-fs/s1/index.html',
sourceHint: 'index.html',
element: { selector: '#title', tag: 'h1', text: '模型还没用熟', classes: [] } as never,
change: { text: { from: '模型还没用熟', to: '模型用熟了' }, description: '标题改积极一点' } as never,
})
expect(text).toContain('#title')
expect(text).toContain('index.html')
expect(text).toContain('模型用熟了')
// 用户的话 + 具体改动在
expect(text).toContain('标题改积极一点')
expect(text).toContain('模型用熟了')
// selector / 页面 URL / 「请在源码中落地」 等 DOM 噪音不在(截图才是引用)
expect(text).not.toContain('#title')
expect(text).not.toContain('index.html')
expect(text).not.toContain('请在源码中落地')
})
it('returns empty text when there is no description or change (image alone is the reference)', () => {
const text = buildSelectionComposerText({
pageUrl: 'http://x/',
element: { selector: '#t', tag: 'div', classes: [] } as never,
})
expect(text).toBe('')
})

View File

@ -7,18 +7,20 @@ export type SelectionPayload = {
change?: EditDiff & { description?: string }
}
/**
*
* + **** selector / DOM /
* computedStyles / URL DOM
*
*/
export function buildSelectionComposerText(p: SelectionPayload): string {
const lines: string[] = []
lines.push(`针对页面元素 \`${p.element.selector}\`${p.element.tag}${p.element.text ? `,文本「${p.element.text}` : ''}`)
if (p.sourceHint) lines.push(`- 来源:${p.sourceHint}`)
lines.push(`- 页面:${p.pageUrl}`)
const c = p.change
const lines: string[] = []
if (c?.description) lines.push(c.description)
if (c?.text) lines.push(`- 文本:「${c.text.from}」→「${c.text.to}`)
if (c?.color) lines.push(`- 文字颜色:${c.color.from}${c.color.to}`)
if (c?.background) lines.push(`- 背景:${c.background.from}${c.background.to}`)
if (c?.opacity) lines.push(`- 不透明度:${c.opacity.from}${c.opacity.to}`)
if (c?.fontFamily) lines.push(`- 字体:${c.fontFamily.from}${c.fontFamily.to}`)
if (c?.description) lines.push(`- 说明:${c.description}`)
lines.push('请在源码中落地以上修改。')
return lines.join('\n')
}