assistant-stream.client.spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { describe, expect, it } from 'vitest'
  2. import { LlmAttemptId, createAssistantMessage } from '@deepseek-ai/dsh-llm'
  3. import { SessionSeq, type SessionEvent } from '@deepseek-ai/dsh-session'
  4. import type {
  5. SessionAssistantStreamBaseline,
  6. SessionAssistantStreamFrame,
  7. } from '../src/types.ts'
  8. import { ClientAssistantStream } from '../src/client/sessions/assistant-stream.ts'
  9. import type { SessionLiveEventEntry } from '../src/client/contract/events.ts'
  10. const ATTEMPT = LlmAttemptId('session:1')
  11. function entry(event: SessionEvent): SessionLiveEventEntry {
  12. return { type: 'event', event }
  13. }
  14. function ordinary(seq: number): SessionLiveEventEntry {
  15. return entry({ type: 'turn/start', seq: SessionSeq(seq), time: seq, data: { turn: 1 } })
  16. }
  17. function attemptEvent(seq: number, turn = 1, step = 1): SessionLiveEventEntry {
  18. return entry({
  19. type: 'assistant/attempt',
  20. seq: SessionSeq(seq),
  21. time: seq,
  22. data: { turn, step, stream: [] },
  23. })
  24. }
  25. function messageEvent(
  26. seq: number,
  27. turn = 1,
  28. step = 1,
  29. surfaceOp: 'append' | { readonly op: 'replace'; readonly start: number; readonly end: number } = 'append',
  30. ): SessionLiveEventEntry {
  31. return entry({
  32. type: 'assistant/message',
  33. seq: SessionSeq(seq),
  34. time: seq,
  35. data: {
  36. turn,
  37. step,
  38. message: createAssistantMessage({
  39. content: [{ type: 'text', text: 'done' }],
  40. source: { provider: 'mock', model: 'mock' },
  41. }),
  42. stream: [],
  43. },
  44. surfaceOp: surfaceOp === 'append'
  45. ? surfaceOp
  46. : { ...surfaceOp, start: SessionSeq(surfaceOp.start), end: SessionSeq(surfaceOp.end) },
  47. })
  48. }
  49. function start(
  50. attemptId = ATTEMPT,
  51. startedAfterSeq = -1,
  52. ): SessionAssistantStreamFrame {
  53. return {
  54. type: 'start', attemptId, revision: 1, startedTime: 10,
  55. startedAfterSeq: startedAfterSeq === -1 ? -1 : SessionSeq(startedAfterSeq),
  56. turn: 1, step: 1,
  57. }
  58. }
  59. function chunkFrame(
  60. index: number,
  61. attemptId = ATTEMPT,
  62. ): SessionAssistantStreamFrame {
  63. return {
  64. type: 'chunk', attemptId, revision: index + 2, index, time: 20 + index,
  65. chunk: { type: 'text-delta', index: 0, text: `chunk-${index}` },
  66. }
  67. }
  68. function end(
  69. index: number,
  70. outcome: Extract<SessionAssistantStreamFrame, { type: 'end' }>['outcome'],
  71. attemptId = ATTEMPT,
  72. ): SessionAssistantStreamFrame {
  73. return { type: 'end', attemptId, revision: index + 2, index, outcome }
  74. }
  75. function baseline(nextIndex = 1): SessionAssistantStreamBaseline {
  76. return {
  77. revision: nextIndex + 1,
  78. activeAttempt: {
  79. attemptId: ATTEMPT,
  80. startedTime: 10,
  81. startedAfterSeq: -1,
  82. turn: 1,
  83. step: 1,
  84. nextIndex,
  85. stream: [
  86. { type: 'chunk', time: 20, chunk: { type: 'text-delta', index: 0, text: 'first' } },
  87. { type: 'chunk', time: 21, chunk: { type: 'text-delta', index: 0, text: 'second' } },
  88. ],
  89. },
  90. }
  91. }
  92. function opened(): ClientAssistantStream {
  93. const stream = new ClientAssistantStream()
  94. expect(stream.acceptFrame(start())).toBeUndefined()
  95. return stream
  96. }
  97. describe('ClientAssistantStream', () => {
  98. it('replaces the durable window and reconstructs only the baseline prefix', () => {
  99. const stream = new ClientAssistantStream()
  100. const durable = ordinary(4)
  101. const visible = stream.replace([durable], baseline(1))
  102. expect(visible[0]).toBe(durable)
  103. expect(visible.slice(1)).toEqual([expect.objectContaining({
  104. type: 'transient',
  105. event: expect.objectContaining({
  106. type: 'assistant/live-chunk',
  107. seq: 4.5,
  108. time: 20,
  109. }),
  110. })])
  111. expect(stream.replace([], baseline(3))).toHaveLength(2)
  112. expect(stream.replace([])).toEqual([])
  113. })
  114. it('passes through durable events not owned by the active attempt', () => {
  115. const stream = new ClientAssistantStream()
  116. stream.acceptFrame(start(ATTEMPT, 1))
  117. for (const durable of [
  118. ordinary(1),
  119. messageEvent(2, 1, 1, { op: 'replace', start: 0, end: 0 }),
  120. attemptEvent(0),
  121. attemptEvent(3, 2, 1),
  122. attemptEvent(4, 1, 2),
  123. ]) {
  124. expect(stream.acceptDurable(durable)).toEqual({ type: 'publish', entry: durable })
  125. }
  126. })
  127. it('stages one owned settlement and releases it from the matching end frame', () => {
  128. const stream = opened()
  129. const durable = messageEvent(2)
  130. expect(stream.acceptDurable(durable)).toBeUndefined()
  131. expect(stream.acceptFrame(chunkFrame(0))).toEqual(expect.objectContaining({ type: 'transient' }))
  132. expect(stream.acceptFrame(end(1, {
  133. kind: 'committed', eventType: 'assistant/message', seq: 2,
  134. }))).toEqual({ type: 'settlement', attemptId: String(ATTEMPT), entry: durable })
  135. })
  136. it('rebaselines duplicate durable settlements or starts', () => {
  137. const duplicate = opened()
  138. const durable = attemptEvent(2)
  139. expect(duplicate.acceptDurable(durable)).toBeUndefined()
  140. expect(duplicate.acceptDurable(durable)).toEqual({ type: 'rebaseline' })
  141. expect(duplicate.acceptFrame(start(LlmAttemptId('session:2')))).toEqual({ type: 'rebaseline' })
  142. const clean = new ClientAssistantStream()
  143. expect(clean.acceptFrame(start())).toBeUndefined()
  144. })
  145. it('falls back to durable settlement for frames from an unknown attempt', () => {
  146. const stream = new ClientAssistantStream()
  147. const unknown = LlmAttemptId('session:unknown')
  148. expect(stream.acceptFrame(chunkFrame(0, unknown))).toBeUndefined()
  149. expect(stream.acceptFrame(end(0, { kind: 'abandoned' }, unknown))).toBeUndefined()
  150. const durable = attemptEvent(2)
  151. expect(stream.acceptDurable(durable)).toEqual({ type: 'publish', entry: durable })
  152. const known = opened()
  153. expect(known.acceptFrame(chunkFrame(0, unknown))).toBeUndefined()
  154. expect(known.acceptFrame(end(0, { kind: 'abandoned' }, unknown))).toBeUndefined()
  155. })
  156. it('rebaselines known attempts on chunk or terminal index mismatch', () => {
  157. const chunkMismatch = opened()
  158. expect(chunkMismatch.acceptFrame(chunkFrame(1))).toEqual({ type: 'rebaseline' })
  159. const endMismatch = opened()
  160. expect(endMismatch.acceptFrame(end(1, { kind: 'abandoned' }))).toEqual({ type: 'rebaseline' })
  161. })
  162. it('settles abandonment only when no durable settlement remains pending', () => {
  163. const empty = opened()
  164. expect(empty.acceptFrame(end(0, { kind: 'abandoned' }))).toBeUndefined()
  165. const pending = opened()
  166. expect(pending.acceptDurable(attemptEvent(2))).toBeUndefined()
  167. expect(pending.acceptFrame(end(0, { kind: 'abandoned' }))).toEqual({ type: 'rebaseline' })
  168. })
  169. it('rebaselines committed outcomes without one exact staged settlement', () => {
  170. const published = new ClientAssistantStream()
  171. published.replace([attemptEvent(2)], baseline(0))
  172. expect(published.acceptFrame(end(0, {
  173. kind: 'committed', eventType: 'assistant/attempt', seq: 2,
  174. }))).toBeUndefined()
  175. const missing = opened()
  176. expect(missing.acceptFrame(end(0, {
  177. kind: 'committed', eventType: 'assistant/attempt', seq: 2,
  178. }))).toEqual({ type: 'rebaseline' })
  179. const wrongType = opened()
  180. expect(wrongType.acceptDurable(messageEvent(2))).toBeUndefined()
  181. expect(wrongType.acceptFrame(end(0, {
  182. kind: 'committed', eventType: 'assistant/attempt', seq: 2,
  183. }))).toEqual({ type: 'rebaseline' })
  184. })
  185. })