✨ feat(vscode): 问 AI 收起再展开、重启后接着这块最近的会话问;代码改了对不上的问答不再显示
This commit is contained in:
1 parent
cbe447ade8
commit
f0a3ad80e1
16 files changed
+82
-211
No files matched your search
@@ -4,7 +4,6 @@
|
||||
|
||||
import type { FileAttribute } from '../attributes'
|
||||
import type { BlockView } from '../blockView'
|
||||
import type { AnchorStep } from '../qa/anchor'
|
||||
import type { RunningAsk } from '../qa/asks'
|
||||
import type { Excluded } from './blockFilter'
|
||||
|
||||
@@ -54,10 +53,9 @@ export interface QaEntryView {
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
/** 挂在一块上的问答。where 是记号拆回的各层(种类和名字),块找不到了时写它原来在哪。 */
|
||||
/** 挂在一块上的问答;记号对不上这份文件里任何一块的不显示。 */
|
||||
export interface QaThreadView {
|
||||
anchor: string
|
||||
where: AnchorStep[]
|
||||
entries: QaEntryView[]
|
||||
}
|
||||
|
||||
@@ -79,7 +77,7 @@ export interface QaMessage {
|
||||
* filter:整个工作区共用的块筛选;ready 后先于第一份文档发一次,之后任何一个预览改了都发给所有预览
|
||||
* qa:这份文件的问答;ready 后发一次,问答、正在答的问题变了再发
|
||||
* answer:askId 那个问题的回答长了(text 是到现在为止的全文)、或换了在做的事
|
||||
* answerEnd:askId 那个问题答完了:done 带最终的回答、存下的那条的 id 和会话 id(追问接着这个会话),failed 带给人看的原因,stopped 是被叫停
|
||||
* answerEnd:askId 那个问题答完了:done 带最终的回答和存下的那条的 id,failed 带给人看的原因,stopped 是被叫停
|
||||
*/
|
||||
export type ExtensionToWebview
|
||||
= | DocumentMessage
|
||||
@@ -89,7 +87,7 @@ export type ExtensionToWebview
|
||||
| { type: 'filter', excluded: Excluded }
|
||||
| QaMessage
|
||||
| { type: 'answer', askId: string, text: string, activity?: string }
|
||||
| { type: 'answerEnd', askId: string, status: 'done' | 'failed' | 'stopped', text?: string, error?: string, entryId?: string, sessionId?: string }
|
||||
| { type: 'answerEnd', askId: string, status: 'done' | 'failed' | 'stopped', text?: string, error?: string, entryId?: string }
|
||||
|
||||
/**
|
||||
* edit:把 startLine..endLine 这几行整行替换成 text(扩展端给每行补回原来的缩进)
|
||||
@@ -99,7 +97,7 @@ export type ExtensionToWebview
|
||||
* goToDefinition:⌘ / Ctrl + 点击,跳到 offset 处名字的定义
|
||||
* findOccurrences:单击带下标的名字,问它在这份文档里出现在哪几处;id、version 同 probeDefinition
|
||||
* setFilter:改块筛选(整份替换),扩展端记下后发 filter 给所有预览
|
||||
* ask:问 anchor 那块代码;askId 由 webview 起,回答的消息带回去;resume 是追问时上一次的会话 id
|
||||
* ask:问 anchor 那块代码;askId 由 webview 起,回答的消息带回去;扩展端接着这块最近一次问答的会话问
|
||||
* stopAsk:叫停 askId 那个问题,这次不存
|
||||
* deleteQa:删掉 anchor 那块下面的一次问答(已经理解了)
|
||||
*/
|
||||
@@ -112,6 +110,6 @@ export type WebviewToExtension
|
||||
| { type: 'probeDefinition', id: number, version: number, offset: number }
|
||||
| { type: 'goToDefinition', version: number, offset: number }
|
||||
| { type: 'findOccurrences', id: number, version: number, offset: number }
|
||||
| { type: 'ask', askId: string, anchor: string, question: string, resume?: string }
|
||||
| { type: 'ask', askId: string, anchor: string, question: string }
|
||||
| { type: 'stopAsk', askId: string }
|
||||
| { type: 'deleteQa', anchor: string, entryId: string }
|
||||
@@ -157,7 +157,7 @@ class Preview {
|
||||
askId: event.ask.askId,
|
||||
status: end.status,
|
||||
...(end.status === 'failed' && { error: end.error }),
|
||||
...(end.status === 'done' && { text: end.answer.text, entryId, sessionId: end.answer.sessionId }),
|
||||
...(end.status === 'done' && { text: end.answer.text, entryId }),
|
||||
})
|
||||
this.postQa()
|
||||
}),
|
||||
@@ -219,17 +219,17 @@ class Preview {
|
||||
}
|
||||
|
||||
/**
|
||||
* 问 anchor 那块代码:按最近一次发出去的块树找到它、带上代码上下文交给 CodeQa;resume 是追问时上一次的会话 id。
|
||||
* 问 anchor 那块代码:按最近一次发出去的块树找到它,把完整问题和追问的话都交给 CodeQa,接不接着上次的会话由它定。
|
||||
* 找不到这块(代码刚改过、还没重新发)或问不了时,直接回一条失败。
|
||||
*/
|
||||
private ask(askId: string, anchor: string, question: string, resume: string | undefined): void {
|
||||
private ask(askId: string, anchor: string, question: string): void {
|
||||
const at = locate(this.document.uri)
|
||||
const subject = at && this.sent && { path: at.key, languageId: this.document.languageId, ...this.sent, anchor }
|
||||
const prompt = subject && askPrompt(subject, question)
|
||||
const followUp = subject && resume ? followUpPrompt(subject, question) : undefined
|
||||
const refused = prompt === undefined
|
||||
const followUp = subject && followUpPrompt(subject, question)
|
||||
const refused = prompt === undefined || followUp === undefined
|
||||
? '这块代码刚改过,找不到了。等预览刷新后重新点「问 AI」再问。'
|
||||
: this.owner.qa.ask(this.document.uri, { askId, anchor, question, prompt, ...(resume && followUp && { followUp: { sessionId: resume, prompt: followUp } }) })
|
||||
: this.owner.qa.ask(this.document.uri, { askId, anchor, question, prompt, followUp })
|
||||
if (refused !== undefined) {
|
||||
this.post({ type: 'answerEnd', askId, status: 'failed', error: refused })
|
||||
return
|
||||
@@ -252,7 +252,7 @@ class Preview {
|
||||
await this.push()
|
||||
return
|
||||
case 'ask':
|
||||
this.ask(msg.askId, msg.anchor, msg.question, msg.resume)
|
||||
this.ask(msg.askId, msg.anchor, msg.question)
|
||||
return
|
||||
case 'stopAsk':
|
||||
this.owner.qa.stop(msg.askId)
|
||||
|
||||
@@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'
|
||||
import { toBlockViews } from '../blockView'
|
||||
import { createPythonAdapter } from '../languages/python'
|
||||
import { WASM_DIR, withBlocks } from '../testSupport'
|
||||
import { anchorSteps, BlockAnchors } from './anchor'
|
||||
import { BlockAnchors } from './anchor'
|
||||
|
||||
const py = createPythonAdapter(WASM_DIR)
|
||||
|
||||
@@ -83,15 +83,6 @@ describe('块的位置记号', () => {
|
||||
expect(anchorOf(after, 'def helper(x, y=1):')).toBe(anchorOf(before, 'def helper(x):'))
|
||||
})
|
||||
|
||||
it('拆回各层:没名字的一层只有种类,编号去掉', () => {
|
||||
expect(anchorSteps('class ReferralRelation > function display[1] > if @3fa1c2d0[2]')).toEqual([
|
||||
{ kind: 'class', name: 'ReferralRelation' },
|
||||
{ kind: 'function', name: 'display' },
|
||||
{ kind: 'if' },
|
||||
])
|
||||
expect(anchorSteps('variable { a, b }')).toEqual([{ kind: 'variable', name: '{ a, b }' }])
|
||||
})
|
||||
|
||||
it('名字里的空白压成一个空格,空名字按没名字算', () => {
|
||||
const anchors = new BlockAnchors()
|
||||
expect(anchors.place('', 'variable', '{\n a,\n b\n}', '').anchor).toBe('variable { a, b }')
|
||||
|
||||
@@ -40,20 +40,3 @@ export class BlockAnchors {
|
||||
return { anchor, inner: label === '' ? scope : anchor }
|
||||
}
|
||||
}
|
||||
|
||||
/** 记号里的一层:种类和名字(没名字的块只有种类),给「找不到原来位置的问答」写它原来在哪。 */
|
||||
export interface AnchorStep {
|
||||
kind: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
/** 把记号拆回各层;拆不出种类的一层丢掉。 */
|
||||
export function anchorSteps(anchor: string): AnchorStep[] {
|
||||
return anchor.split(SEPARATOR).flatMap((segment) => {
|
||||
const match = /^(\S+) (.*?)(?:\[\d+\])?$/.exec(segment)
|
||||
if (!match)
|
||||
return []
|
||||
const [, kind, rest] = match
|
||||
return rest.startsWith(HASH_MARK) || rest === '' ? [{ kind }] : [{ kind, name: rest }]
|
||||
})
|
||||
}
|
||||
@@ -6,22 +6,22 @@ import type { QaEntry } from './qaMap'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { EventEmitter } from 'vscode'
|
||||
import { FolderStore, locate } from '../folderStore'
|
||||
import { anchorSteps } from './anchor'
|
||||
import { QaAsks } from './asks'
|
||||
import { askClaude } from './claudeAsk'
|
||||
import { QA_FILE, withEntry, withoutEntry } from './qaMap'
|
||||
import { latestSession, QA_FILE, withEntry, withoutEntry } from './qaMap'
|
||||
|
||||
/** 正在答的问题有了进展(update),或答完了(end;答成了带存下的那条的 id)。owner 是问的那份文件的 uri。 */
|
||||
export type AskEvent
|
||||
= | { owner: string, kind: 'update', ask: RunningAsk }
|
||||
| { owner: string, kind: 'end', ask: RunningAsk, end: AskEnd, entryId?: string }
|
||||
|
||||
/** prompt 是带代码上下文的完整问题;followUp 是接着这块上一次的会话问时发的话(只有问题和是哪块) */
|
||||
export interface AskRequest {
|
||||
askId: string
|
||||
anchor: string
|
||||
question: string
|
||||
prompt: string
|
||||
followUp?: { sessionId: string, prompt: string }
|
||||
followUp: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,21 +49,25 @@ export class CodeQa implements Disposable {
|
||||
available: at !== undefined,
|
||||
threads: threads.map(({ anchor, entries }) => ({
|
||||
anchor,
|
||||
where: anchorSteps(anchor),
|
||||
entries: entries.map(({ id, question, answer, createdAt }) => ({ id, question, answer, createdAt })),
|
||||
})),
|
||||
running: this.asks.runningFor(uri.toString()),
|
||||
}
|
||||
}
|
||||
|
||||
/** 开始问;问不了时返回原因(给人看的话)。在工作区文件夹里跑 claude,它读得到整个项目。 */
|
||||
/**
|
||||
* 开始问;问不了时返回原因(给人看的话)。在工作区文件夹里跑 claude,它读得到整个项目。
|
||||
* 这块问过的就接着最近一次的会话问(收起再展开、重启 VS Code 都一样),没问过就带上代码上下文开新会话。
|
||||
*/
|
||||
ask(uri: Uri, request: AskRequest): string | undefined {
|
||||
const at = locate(uri)
|
||||
if (!at)
|
||||
return '这个文件不在工作区文件夹里,问答没地方存。'
|
||||
const owner = uri.toString()
|
||||
const { askId, anchor, question, prompt, followUp } = request
|
||||
return this.asks.start({ owner, askId, anchor, question, run: { prompt, followUp, cwd: at.folder.uri.fsPath } }, {
|
||||
const sessionId = latestSession(this.store.get(at.folder, at.key), anchor)
|
||||
const run = { prompt, ...(sessionId && { followUp: { sessionId, prompt: followUp } }), cwd: at.folder.uri.fsPath }
|
||||
return this.asks.start({ owner, askId, anchor, question, run }, {
|
||||
update: ask => this.answered.fire({ owner, kind: 'update', ask }),
|
||||
end: async (ask, end) => {
|
||||
if (end.status !== 'done') {
|
||||
|
||||
@@ -4,7 +4,7 @@ import * as os from 'node:os'
|
||||
import * as path from 'node:path'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import { dropEntries, moveEntries, readPathMap, writePathMap } from '../pathMap'
|
||||
import { QA_FILE, withEntry, withoutEntry } from './qaMap'
|
||||
import { latestSession, QA_FILE, withEntry, withoutEntry } from './qaMap'
|
||||
|
||||
function entry(id: string, createdAt: string, question = `问 ${id}`): QaEntry {
|
||||
return { id, question, answer: `答 ${id}`, createdAt, sessionId: `s-${id}` }
|
||||
@@ -67,6 +67,24 @@ describe('记一次、删一次', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('再问时接着哪个会话', () => {
|
||||
it('接着这块最近一次问答的会话;别的块、没问过的块、没记会话 id 的都重新开', () => {
|
||||
let map: QaMap = {}
|
||||
map = withEntry(map, 'a.py', IF, entry('new', '2026-09-26T10:00:00.000Z'))
|
||||
map = withEntry(map, 'a.py', IF, entry('old', '2026-09-25T10:00:00.000Z'))
|
||||
map = withEntry(map, 'a.py', DISPLAY, { ...entry('bare', '2026-09-26T11:00:00.000Z'), sessionId: '' })
|
||||
expect(latestSession(map['a.py'], IF)).toBe('s-new')
|
||||
expect(latestSession(map['a.py'], DISPLAY)).toBeUndefined()
|
||||
expect(latestSession(map['a.py'], 'function gone')).toBeUndefined()
|
||||
expect(latestSession(undefined, IF)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('删掉最近那次,就接着剩下里最近的', () => {
|
||||
const map = withEntry(withEntry({}, 'a.py', IF, entry('a', '2026-09-25T10:00:00.000Z')), 'a.py', IF, entry('b', '2026-09-26T10:00:00.000Z'))
|
||||
expect(latestSession(withoutEntry(map, 'a.py', IF, 'b')['a.py'], IF)).toBe('s-a')
|
||||
})
|
||||
})
|
||||
|
||||
describe('读写 .spx/code-qa.json', () => {
|
||||
it('文件不存在、解析失败、不是对象都当没有', async () => {
|
||||
expect(await readPathMap(root, QA_FILE)).toEqual({})
|
||||
|
||||
@@ -88,3 +88,8 @@ export function withoutEntry(map: QaMap, key: string, anchor: string, entryId: s
|
||||
delete next[key]
|
||||
return threads.length > 0 ? { ...next, [key]: threads } : next
|
||||
}
|
||||
|
||||
/** 再问 anchor 那块时接着的会话:这块最近一次问答的会话 id;没问过、或那次没记会话 id 时没有,重新开会话。 */
|
||||
export function latestSession(threads: readonly QaThread[] | undefined, anchor: string): string | undefined {
|
||||
return threads?.find(thread => thread.anchor === anchor)?.entries.at(-1)?.sessionId || undefined
|
||||
}
|
||||
Reference in new issue
Block a user