transcript-adapter.spec.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /**
  2. * TranscriptAdapter over the raw append-only window: log-ordered projection of
  3. * append-origin events, one marker per landed compaction, replacement copies
  4. * hidden, command-lifecycle folding, node/array identity, call pairing, and
  5. * host-provided wire views.
  6. */
  7. import { createUserMessage, CallId, createMessage, createToolResultMessage } from '@deepseek-ai/dsh-llm'
  8. import { describe, expect, it } from 'vitest'
  9. import type { SessionEvent } from '@deepseek-ai/dsh-session/types'
  10. import { TranscriptAdapter } from '../src/client/sessions/transcript-adapter.ts'
  11. import { ev, plainTurn } from './event-script.ts'
  12. const at = (seq: number, e: Record<string, unknown>): SessionEvent =>
  13. ({ seq, time: 1_700_000_000_000 + seq, ...e }) as unknown as SessionEvent
  14. /** A `compact/summary` provenance event (log-only, no surfaceOp). */
  15. function compactSummary(seq: number, summary: unknown = [{ type: 'text', text: '# 摘要\n\n保留事实' }]): SessionEvent {
  16. return at(seq, {
  17. type: 'compact/summary',
  18. data: {
  19. summary,
  20. shadowedRange: { start: 1, end: 3 },
  21. shadowedSeqs: [1, 3],
  22. shadowedTokenCount: 100,
  23. provider: 'fake',
  24. model: 'compact-1',
  25. },
  26. })
  27. }
  28. /** The replacement user message a compaction backend lands (the checkpoint). */
  29. function checkpoint(
  30. seq: number,
  31. summarySeq: number,
  32. { start = 1, end = 3, sourceEventSeqs = [summarySeq, start, end] }: {
  33. start?: number
  34. end?: number
  35. sourceEventSeqs?: number[]
  36. } = {},
  37. ): SessionEvent {
  38. return at(seq, {
  39. type: 'user/message',
  40. surfaceOp: { op: 'replace', start, end },
  41. sourceEventSeqs,
  42. data: createUserMessage({
  43. content: [{ type: 'text', text: '<context_checkpoint>model only</context_checkpoint>' }],
  44. source: { kind: 'plugin', plugin: 'compact' },
  45. }),
  46. })
  47. }
  48. describe('TranscriptAdapter', () => {
  49. it('projects a window starting past seq 0 at its own log positions', () => {
  50. const adapter = new TranscriptAdapter()
  51. adapter.reset(plainTurn(100, 5, '偏移问', '偏移答'))
  52. expect(adapter.nodes().map(n => [n.kind, n.seq])).toEqual([['user', 101], ['assistant', 103]])
  53. })
  54. it('appends incrementally keeping old node references (materialize-once identity)', () => {
  55. const adapter = new TranscriptAdapter()
  56. adapter.reset(plainTurn(0, 0, 'a', 'b'))
  57. const first = adapter.nodes()
  58. adapter.append(ev.user(6, '追加'))
  59. const second = adapter.nodes()
  60. expect(second).toHaveLength(3)
  61. expect(second[0]).toBe(first[0])
  62. expect(second[1]).toBe(first[1])
  63. expect(second).not.toBe(first) // a real change swaps the array
  64. })
  65. it('keeps the array reference across a chunk storm and swaps it when a node lands', () => {
  66. const adapter = new TranscriptAdapter()
  67. adapter.reset(plainTurn(0, 0, 'a', 'b'))
  68. const settled = adapter.nodes()
  69. adapter.append(ev.chunkStart(6, 1))
  70. expect(adapter.nodes()).toBe(settled)
  71. adapter.append(ev.chunkText(7, 1, '流式'))
  72. expect(adapter.nodes()).toBe(settled)
  73. adapter.append(ev.assistant(8, 1, '流式完成'))
  74. const finalized = adapter.nodes()
  75. expect(finalized).not.toBe(settled)
  76. expect(finalized.at(-1)).toMatchObject({ kind: 'assistant', seq: 8 })
  77. })
  78. it('materializes every append-origin variant with field mapping', () => {
  79. const adapter = new TranscriptAdapter()
  80. adapter.reset([
  81. ev.user(0, '用户'),
  82. ev.assistant(1, 0, '助手'),
  83. at(2, { type: 'steering/message', surfaceOp: 'append', data: {
  84. turn: 0,
  85. message: createUserMessage({
  86. content: [{ type: 'text', text: '插话' }],
  87. source: { kind: 'user' },
  88. }),
  89. } }),
  90. at(3, { type: 'user/message', surfaceOp: 'append', data: createUserMessage({
  91. content: [{ type: 'text', text: '上下文' }], source: { kind: 'plugin', plugin: 'p' },
  92. }) }),
  93. ev.toolCall(4, 0, 'c1', 'echo', '{"x":1}'),
  94. ev.toolResult(5, 0, 'c1', '结果'),
  95. ])
  96. const nodes = adapter.nodes()
  97. expect(nodes.map(n => n.kind)).toEqual(['user', 'assistant', 'steering', 'context', 'tool-result'])
  98. expect(nodes.find(n => n.kind === 'tool-result')).toMatchObject({
  99. callId: 'c1', call: { name: 'echo', argsRaw: '{"x":1}' }, isError: false,
  100. })
  101. })
  102. it('skips events core does not call surface-eligible, marker or not', () => {
  103. // The transcript is the append-origin surface, so log-only events (a chunk,
  104. // a turn boundary, a compact/* provenance record) and a future type core
  105. // has not admitted contribute no node.
  106. const adapter = new TranscriptAdapter()
  107. adapter.reset([
  108. ev.turnStart(0, 1),
  109. at(1, { type: 'notice/message', surfaceOp: 'append', data: { note: 1 } }),
  110. compactSummary(2),
  111. ev.user(3, '唯一的一条'),
  112. ev.turnEnd(4, 1),
  113. ])
  114. expect(adapter.nodes().map(n => [n.kind, n.seq])).toEqual([['user', 3]])
  115. })
  116. describe('compaction markers', () => {
  117. it('keeps the original messages and full tool output, hiding replacement copies', () => {
  118. const adapter = new TranscriptAdapter()
  119. adapter.reset([
  120. ev.user(0, '原始问题'),
  121. ev.assistant(1, 0, '原始回答'),
  122. ev.toolCall(4, 0, 'c1', 'echo', '{}'),
  123. ev.toolResult(5, 0, 'c1', '完整工具输出'),
  124. // A pruned tool/result copy: rewrites one node for the model, marks nothing.
  125. at(6, { type: 'tool/result', surfaceOp: { op: 'replace', start: 5, end: 5 }, sourceEventSeqs: [5], data: {
  126. turn: 0, step: 0,
  127. message: createToolResultMessage({ callId: CallId('c1'), content: [{ type: 'text', text: '已裁剪' }], isError: false }),
  128. } }),
  129. compactSummary(7),
  130. checkpoint(8, 7, { start: 1, end: 5, sourceEventSeqs: [7, 1, 5] }),
  131. // A regenerated assistant/message: also a silent model-only rewrite.
  132. at(9, { type: 'assistant/message', surfaceOp: { op: 'replace', start: 8, end: 8 }, sourceEventSeqs: [8], data: {
  133. turn: 0, step: 0,
  134. message: createMessage({
  135. role: 'assistant',
  136. content: [{ type: 'text', text: '通用 replacement 副本' }],
  137. source: { kind: 'model', ...{ provider: 'x', model: 'copy' } },
  138. }),
  139. } }),
  140. ])
  141. const nodes = adapter.nodes()
  142. expect(nodes.map(n => [n.kind, n.seq])).toEqual([
  143. ['user', 0], ['assistant', 1], ['tool-result', 5], ['compaction', 8],
  144. ])
  145. expect(nodes[2]).toMatchObject({ kind: 'tool-result', content: [{ type: 'text', text: '完整工具输出' }] })
  146. expect(nodes[3]).toMatchObject({ kind: 'compaction', summary: '# 摘要\n\n保留事实' })
  147. })
  148. it('adds one marker per landed compaction, in log order', () => {
  149. const adapter = new TranscriptAdapter()
  150. adapter.reset([
  151. ev.user(0, 'a'),
  152. compactSummary(1, [{ type: 'text', text: 'first' }]),
  153. checkpoint(2, 1, { start: 0, end: 0, sourceEventSeqs: [1, 0] }),
  154. ev.user(3, 'b'),
  155. compactSummary(4, [{ type: 'text', text: 'second' }]),
  156. checkpoint(5, 4, { start: 2, end: 3, sourceEventSeqs: [4, 2, 3] }),
  157. ])
  158. expect(adapter.nodes().filter(n => n.kind === 'compaction')).toEqual([
  159. { kind: 'compaction', seq: 2, time: 1_700_000_000_002, summary: 'first' },
  160. { kind: 'compaction', seq: 5, time: 1_700_000_000_005, summary: 'second' },
  161. ])
  162. })
  163. it('renders the marker when the shadowed range is outside the window and logs nothing', () => {
  164. // The pagination hole A1 left open: quota is no longer spent on
  165. // replacement copies, so a page can carry a checkpoint whose
  166. // surfaceOp.start lies below the window head. The old surface fold threw
  167. // on the missing range and degraded with a console error; a log-ordered
  168. // projection has no range to resolve.
  169. const adapter = new TranscriptAdapter()
  170. const noise = { error: console.error, warn: console.warn }
  171. const logged: unknown[] = []
  172. console.error = (...args: unknown[]) => logged.push(args)
  173. console.warn = (...args: unknown[]) => logged.push(args)
  174. try {
  175. adapter.reset([
  176. compactSummary(80, [{ type: 'text', text: '窗外范围' }]),
  177. checkpoint(81, 80, { start: 3, end: 40, sourceEventSeqs: [80, 3, 40] }),
  178. ev.user(82, '压缩后的新问题'),
  179. ])
  180. expect(adapter.nodes().map(n => [n.kind, n.seq])).toEqual([['compaction', 81], ['user', 82]])
  181. expect(adapter.nodes()[0]).toMatchObject({ summary: '窗外范围' })
  182. } finally {
  183. console.error = noise.error
  184. console.warn = noise.warn
  185. }
  186. expect(logged).toEqual([])
  187. })
  188. it('treats an APPENDING plugin-sourced user/message as injected context, not a compaction', () => {
  189. // A session-reference card carries the same plugin source shape; only the
  190. // replacement marker makes an event a checkpoint.
  191. const adapter = new TranscriptAdapter()
  192. adapter.reset([
  193. at(0, { type: 'user/message', surfaceOp: 'append', data: createUserMessage({
  194. content: [{ type: 'text', text: '注入的上下文' }],
  195. source: { kind: 'plugin', plugin: 'compact' },
  196. }) }),
  197. ])
  198. expect(adapter.nodes()).toMatchObject([{ kind: 'context', seq: 0 }])
  199. })
  200. it('ignores a foreign plugin s replacement user/message', () => {
  201. const adapter = new TranscriptAdapter()
  202. adapter.reset([
  203. ev.user(0, '保留'),
  204. at(1, { type: 'user/message', surfaceOp: { op: 'replace', start: 0, end: 0 }, sourceEventSeqs: [0], data: createUserMessage({
  205. content: [{ type: 'text', text: '别的插件重写' }],
  206. source: { kind: 'plugin', plugin: 'not-compact' },
  207. }) }),
  208. ])
  209. expect(adapter.nodes().map(n => [n.kind, n.seq])).toEqual([['user', 0]])
  210. })
  211. it.each([
  212. ['absent provenance', undefined],
  213. ['text-less summary blocks', compactSummary(1, [{ type: 'image', data: 'nope' }])],
  214. ['a whitespace-only summary', compactSummary(1, [{ type: 'text', text: ' ' }])],
  215. ['an empty summary array', compactSummary(1, [])],
  216. ['a non-array summary', compactSummary(1, 'plain string')],
  217. ])('degrades %s to a non-expandable marker', (_label, summary) => {
  218. const adapter = new TranscriptAdapter()
  219. adapter.reset([
  220. ...(summary === undefined ? [] : [summary]),
  221. checkpoint(2, 1, { start: 0, end: 0, sourceEventSeqs: [1, 0] }),
  222. ])
  223. expect(adapter.nodes()).toEqual([
  224. { kind: 'compaction', seq: 2, time: 1_700_000_000_002, summary: null },
  225. ])
  226. })
  227. it('keeps the text of a mixed-block summary, skipping the blocks it cannot render', () => {
  228. // ContentBlock is merge-extensible and the payload type is ContentBlock[],
  229. // so a non-text block must not discard recoverable text beside it.
  230. const adapter = new TranscriptAdapter()
  231. adapter.reset([
  232. compactSummary(1, [{ type: 'text', text: '可用摘要' }, { type: 'image', data: 'nope' }]),
  233. checkpoint(2, 1, { start: 0, end: 0, sourceEventSeqs: [1, 0] }),
  234. ])
  235. expect(adapter.nodes()).toEqual([
  236. { kind: 'compaction', seq: 2, time: 1_700_000_000_002, summary: '可用摘要' },
  237. ])
  238. })
  239. it('leaves the summary null when the checkpoint records no provenance at all', () => {
  240. const adapter = new TranscriptAdapter()
  241. adapter.reset([at(2, {
  242. type: 'user/message',
  243. surfaceOp: { op: 'replace', start: 0, end: 0 },
  244. data: createUserMessage({
  245. content: [{ type: 'text', text: '<context_checkpoint>x</context_checkpoint>' }],
  246. source: { kind: 'plugin', plugin: 'compact' },
  247. }),
  248. })])
  249. expect(adapter.nodes()).toEqual([{ kind: 'compaction', seq: 2, time: 1_700_000_000_002, summary: null }])
  250. })
  251. it('skips a non-summary provenance seq before reaching the real one', () => {
  252. const adapter = new TranscriptAdapter()
  253. adapter.reset([
  254. ev.user(0, '被压缩的问题'),
  255. at(1, { type: 'compact/start', data: { turn: 0 } }),
  256. compactSummary(2, [{ type: 'text', text: '第三个来源才是摘要' }]),
  257. checkpoint(3, 2, { start: 0, end: 0, sourceEventSeqs: [1, 2, 0] }),
  258. ])
  259. expect(adapter.nodes().at(-1)).toMatchObject({ kind: 'compaction', summary: '第三个来源才是摘要' })
  260. })
  261. it('resolves the summary once an older page supplies the provenance', () => {
  262. const adapter = new TranscriptAdapter()
  263. const landed = checkpoint(8, 7, { start: 0, end: 0, sourceEventSeqs: [7, 0] })
  264. adapter.reset([landed])
  265. expect(adapter.nodes()[0]).toMatchObject({ kind: 'compaction', summary: null })
  266. adapter.reset([compactSummary(7, [{ type: 'text', text: '分页补齐的摘要' }]), landed])
  267. expect(adapter.nodes()[0]).toMatchObject({ kind: 'compaction', summary: '分页补齐的摘要' })
  268. })
  269. it('creates the marker on the live append path', () => {
  270. const adapter = new TranscriptAdapter()
  271. adapter.reset(plainTurn(0, 0, 'a', 'b'))
  272. adapter.append(compactSummary(6, [{ type: 'text', text: '直播摘要' }]))
  273. adapter.append(checkpoint(7, 6, { start: 1, end: 3, sourceEventSeqs: [6, 1, 3] }))
  274. const nodes = adapter.nodes()
  275. // The compacted history is still there; the marker is one more row after it.
  276. expect(nodes.map(n => [n.kind, n.seq])).toEqual([['user', 1], ['assistant', 3], ['compaction', 7]])
  277. expect(nodes.at(-1)).toMatchObject({ kind: 'compaction', seq: 7, summary: '直播摘要' })
  278. })
  279. })
  280. it('returns call:null for a tool-result whose call fell outside the window', () => {
  281. const adapter = new TranscriptAdapter()
  282. adapter.reset([ev.toolResult(50, 3, 'outside-call', '孤儿结果')])
  283. expect(adapter.nodes()[0]).toMatchObject({ kind: 'tool-result', callId: 'outside-call', call: null })
  284. })
  285. it('materializes a tool-result error field when present', () => {
  286. const adapter = new TranscriptAdapter()
  287. adapter.reset([
  288. at(0, { type: 'tool/result', surfaceOp: 'append', data: {
  289. turn: 0, step: 0,
  290. message: createToolResultMessage({ callId: CallId('c1'), content: [], isError: true }),
  291. error: { name: 'Boom', code: 'boom' },
  292. } }),
  293. ])
  294. expect(adapter.nodes()[0]).toMatchObject({ kind: 'tool-result', isError: true, error: { code: 'boom' } })
  295. })
  296. it('attaches wire views to the materialized result node', () => {
  297. const adapter = new TranscriptAdapter()
  298. const callView = { for: 'call' as const, view: { card: 'terminal' as const, command: 'ls' } }
  299. const resultView = { for: 'result' as const, view: { card: 'generic' as const, title: '完成' } }
  300. adapter.reset([
  301. ev.toolCall(0, 1, 'c1', 'bash', '{"cmd":"ls"}'),
  302. ev.toolResult(1, 1, 'c1', 'listing'),
  303. ], [callView, resultView] as never)
  304. expect(adapter.nodes().find(n => n.kind === 'tool-result')).toMatchObject({
  305. callView: { card: 'terminal' }, resultView: { card: 'generic', title: '完成' },
  306. })
  307. })
  308. it('attaches views on the live append path and defaults to null without views', () => {
  309. const adapter = new TranscriptAdapter()
  310. adapter.reset(plainTurn(0, 0, 'a', 'b')) // no views argument
  311. adapter.append(ev.toolCall(6, 1, 'c2', 'echo', '{}'), { for: 'call', view: { card: 'generic', title: '回声' } } as never)
  312. adapter.append(ev.toolResult(7, 1, 'c2', 'ok')) // no view on the result
  313. expect(adapter.nodes().find(n => n.kind === 'tool-result')).toMatchObject({
  314. callView: { title: '回声' }, resultView: null,
  315. })
  316. })
  317. it('leaves callView null when the paired call fell outside the window (cross-page break)', () => {
  318. const adapter = new TranscriptAdapter()
  319. const resultView = { for: 'result' as const, view: { card: 'generic' as const, title: '孤儿' } }
  320. adapter.reset([ev.toolResult(50, 3, 'outside', '窗外配对')], [resultView] as never)
  321. expect(adapter.nodes()[0]).toMatchObject({
  322. kind: 'tool-result', call: null, callView: null, resultView: { title: '孤儿' },
  323. })
  324. })
  325. describe('command lifecycle nodes', () => {
  326. it('folds a run/done pair into one settled node merged into flow order by seq', () => {
  327. const adapter = new TranscriptAdapter()
  328. adapter.reset([
  329. ev.user(0, '先说话'),
  330. ev.commandRun(1, 'cmd-1', 'plan'),
  331. ev.commandDone(2, 'cmd-1', 'success', '已进入 plan mode'),
  332. ev.assistant(3, 0, '然后回答'),
  333. ])
  334. const nodes = adapter.nodes()
  335. expect(nodes.map(n => [n.kind, n.seq])).toEqual([['user', 0], ['command', 1], ['assistant', 3]])
  336. expect(nodes[1]).toMatchObject({
  337. kind: 'command', commandId: 'cmd-1', name: 'plan', args: '',
  338. outcome: { kind: 'success', text: '已进入 plan mode' },
  339. })
  340. })
  341. it('renders a run with no done as still executing (outcome null)', () => {
  342. const adapter = new TranscriptAdapter()
  343. adapter.reset([ev.commandRun(0, 'cmd-2', 'goal', ' ship it')])
  344. expect(adapter.nodes()[0]).toMatchObject({ kind: 'command', name: 'goal', args: ' ship it', outcome: null })
  345. })
  346. it('soft-falls a done-only window into a node built from the done (cross-window cut)', () => {
  347. const adapter = new TranscriptAdapter()
  348. adapter.reset([ev.commandDone(80, 'cmd-3', 'error', '失败了')])
  349. expect(adapter.nodes()[0]).toMatchObject({
  350. kind: 'command', seq: 80, commandId: 'cmd-3', name: null, args: null,
  351. outcome: { kind: 'error', text: '失败了' },
  352. })
  353. })
  354. it('settles a live-appended done in place, keeping the node at the run seq', () => {
  355. const adapter = new TranscriptAdapter()
  356. adapter.reset(plainTurn(0, 0, 'q', 'a'))
  357. adapter.append(ev.commandRun(6, 'cmd-4', 'clear'))
  358. const running = adapter.nodes().find(n => n.kind === 'command')
  359. expect(running).toMatchObject({ outcome: null })
  360. adapter.append(ev.commandDone(7, 'cmd-4'))
  361. const settled = adapter.nodes().find(n => n.kind === 'command')
  362. expect(settled).toMatchObject({ seq: 6, outcome: { kind: 'success' } })
  363. // Settlement replaced the node object rather than mutating the published one.
  364. expect(settled).not.toBe(running)
  365. })
  366. it('tails command nodes whose seq is past every transcript node', () => {
  367. const adapter = new TranscriptAdapter()
  368. adapter.reset([ev.user(0, '问'), ev.commandRun(1, 'cmd-tail', 'plan')])
  369. expect(adapter.nodes().map(n => n.kind)).toEqual(['user', 'command'])
  370. })
  371. it('renders the /compact row alongside the marker its own command produced', () => {
  372. // The row that reports the compaction is a command node; dropping command
  373. // folding would delete it together with every other slash-command row.
  374. const adapter = new TranscriptAdapter()
  375. adapter.reset([
  376. ev.user(0, '压缩前的问题'),
  377. ev.commandRun(1, 'cmd-compact', 'compact'),
  378. compactSummary(2, [{ type: 'text', text: '手动压缩摘要' }]),
  379. checkpoint(3, 2, { start: 0, end: 0, sourceEventSeqs: [2, 0] }),
  380. ev.commandDone(4, 'cmd-compact', 'success', '已压缩'),
  381. ])
  382. const nodes = adapter.nodes()
  383. expect(nodes.map(n => [n.kind, n.seq])).toEqual([['user', 0], ['command', 1], ['compaction', 3]])
  384. expect(nodes[1]).toMatchObject({ name: 'compact', outcome: { kind: 'success', text: '已压缩' } })
  385. })
  386. })
  387. })