event-script.client.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {
  2. ToolCallId, createMessage, createToolResultMessage, createUserMessage,
  3. } from '@deepseek-ai/dsh-llm'
  4. import { SessionSeq } from '@deepseek-ai/dsh-session/types'
  5. // Minimal SessionEvent builders for orchestration tests (shape mirrors what the
  6. // host emits; only the fields the object layer reads).
  7. import type { ContentBlock } from '@deepseek-ai/dsh-llm/types'
  8. import type { SessionEvent } from '@deepseek-ai/dsh-session/types'
  9. import type {
  10. SessionEventEntry,
  11. SessionPage,
  12. SessionWireEvent,
  13. } from '../src/types.ts'
  14. /** One text content block (local helper). */
  15. const text = (t: string): ContentBlock[] => [{ type: 'text', text: t }]
  16. const at = (seq: SessionSeq, e: Record<string, unknown>): SessionEvent =>
  17. ({ seq, time: 1_700_000_000_000 + seq, ...e }) as unknown as SessionEvent
  18. export const ev = {
  19. turnStart: (seq: SessionSeq, turn: number): SessionEvent =>
  20. at(seq, { type: 'turn/start', data: { turn } }),
  21. user: (seq: SessionSeq, body: string): SessionEvent =>
  22. at(seq, { type: 'user/message', surfaceOp: 'append', data: createUserMessage({
  23. content: text(body), source: { kind: 'user' },
  24. }) }),
  25. stepStart: (seq: SessionSeq, turn: number, step = 0): SessionEvent =>
  26. at(seq, { type: 'step/start', data: { turn, step } }),
  27. chunkStart: (seq: SessionSeq, turn: number, step = 0, index = 0): SessionEvent =>
  28. at(seq, { type: 'assistant/chunk', data: { turn, step, chunk: { type: 'block-start', index, blockType: 'text' } } }),
  29. chunkText: (seq: SessionSeq, turn: number, piece: string, step = 0, index = 0): SessionEvent =>
  30. at(seq, { type: 'assistant/chunk', data: { turn, step, chunk: { type: 'text-delta', index, text: piece } } }),
  31. assistant: (seq: SessionSeq, turn: number, body: string, step = 0): SessionEvent =>
  32. at(seq, { type: 'assistant/message', surfaceOp: 'append', data: {
  33. turn, step,
  34. message: createMessage({
  35. role: 'assistant',
  36. content: text(body),
  37. source: {
  38. kind: 'model',
  39. ...{ provider: 'fake', model: 'fk-1' },
  40. },
  41. }),
  42. } }),
  43. toolCall: (seq: SessionSeq, turn: number, callId: string, name: string, args: string, step = 0): SessionEvent =>
  44. at(seq, { type: 'tool/call', data: { turn, step, callId, name, arguments: args } }),
  45. toolResult: (seq: SessionSeq, turn: number, callId: string, body: string, step = 0): SessionEvent =>
  46. at(seq, {
  47. type: 'tool/result',
  48. surfaceOp: 'append',
  49. data: {
  50. turn,
  51. step,
  52. message: createToolResultMessage({
  53. callId: ToolCallId(callId),
  54. content: text(body),
  55. isError: false,
  56. }),
  57. },
  58. }),
  59. codeDispatchStart: (seq: SessionSeq, parentCallId: string, n: number, name: string, args: unknown): SessionEvent =>
  60. at(seq, {
  61. type: 'tool/code-dispatch-start',
  62. data: { rootCallId: parentCallId, parentCallId, subCallId: `${parentCallId}:code:${n}`, name, arguments: args },
  63. }),
  64. codeDispatch: (
  65. seq: SessionSeq,
  66. parentCallId: string,
  67. n: number,
  68. name: string,
  69. args: unknown,
  70. body: string,
  71. isError = false,
  72. ): SessionEvent =>
  73. at(seq, {
  74. type: 'tool/code-dispatch',
  75. data: { rootCallId: parentCallId, parentCallId, subCallId: `${parentCallId}:code:${n}`, name, arguments: args, isError, content: text(body) },
  76. }),
  77. stepEnd: (seq: SessionSeq, turn: number, step = 0): SessionEvent =>
  78. at(seq, { type: 'step/end', data: { turn, step } }),
  79. retry: (
  80. seq: SessionSeq,
  81. turn: number,
  82. step = 0,
  83. retry = 1,
  84. maxRetries = 2,
  85. delayMs = 500,
  86. message = 'temporary transport failure',
  87. ): SessionEvent =>
  88. at(seq, {
  89. type: 'llm/retry',
  90. data: {
  91. turn, step,
  92. provider: 'fake', mode: 'normal', policyKey: 'fake-normal',
  93. retry, maxRetries, delayMs,
  94. failure: { code: 'TRANSPORT', message },
  95. },
  96. }),
  97. turnEnd: (seq: SessionSeq, turn: number, reason: 'completed' | 'aborted' | 'disposed' = 'completed'): SessionEvent =>
  98. at(seq, { type: 'turn/end', data: {
  99. turn,
  100. reason: reason === 'completed'
  101. ? { kind: 'completed' }
  102. : { kind: 'aborted', reason: { kind: reason === 'disposed' ? 'disposed' : 'user' } },
  103. } }),
  104. commandRun: (seq: SessionSeq, commandId: string, name: string, args = ''): SessionEvent =>
  105. at(seq, { type: 'command/run', data: { commandId, name, args, source: { kind: 'user' } } }),
  106. commandRunWithoutInput: (seq: SessionSeq, commandId: string, name: string): SessionEvent =>
  107. at(seq, { type: 'command/run', data: { commandId, name, source: { kind: 'user' } } }),
  108. commandDone: (
  109. seq: SessionSeq,
  110. commandId: string,
  111. kind: 'success' | 'error' = 'success',
  112. text?: string,
  113. sourceEventSeq?: SessionSeq,
  114. ): SessionEvent =>
  115. at(seq, { type: 'command/done', data: {
  116. commandId,
  117. kind,
  118. ...text === undefined ? {} : { text },
  119. ...sourceEventSeq === undefined ? {} : { sourceEventSeq },
  120. } }),
  121. /** A compaction's log-only `compaction/summary` record. */
  122. compactSummary: (seq: SessionSeq, summary: string, start: SessionSeq, end: SessionSeq): SessionEvent =>
  123. at(seq, { type: 'compaction/summary', data: {
  124. summary: text(summary),
  125. shadowedRange: { start, end },
  126. shadowedSeqs: [start, end],
  127. shadowedTokenCount: 100,
  128. provider: 'fake',
  129. model: 'compact-1',
  130. } }),
  131. /** The replacement user message a compaction backend lands (the checkpoint). */
  132. compactCheckpoint: (
  133. seq: SessionSeq,
  134. summarySeq: SessionSeq,
  135. start: SessionSeq,
  136. end: SessionSeq,
  137. ): SessionEvent =>
  138. at(seq, {
  139. type: 'user/message',
  140. surfaceOp: { op: 'replace', start, end },
  141. sourceEventSeqs: [summarySeq, start, end],
  142. data: createUserMessage({
  143. content: text('<context_checkpoint>model only</context_checkpoint>'),
  144. source: { kind: 'plugin', plugin: 'compact' },
  145. }),
  146. }),
  147. }
  148. /** One complete plain turn (turn/start → user → step → assistant → turn/end), 6 events from startSeq. */
  149. export function plainTurn(startSeq: SessionSeq, turn: number, ask: string, answer: string): SessionEvent[] {
  150. return [
  151. ev.turnStart(startSeq, turn),
  152. ev.user(SessionSeq(startSeq + 1), ask),
  153. ev.stepStart(SessionSeq(startSeq + 2), turn),
  154. ev.assistant(SessionSeq(startSeq + 3), turn, answer),
  155. ev.stepEnd(SessionSeq(startSeq + 4), turn),
  156. ev.turnEnd(SessionSeq(startSeq + 5), turn),
  157. ]
  158. }
  159. /** Wrap raw events in the journal envelope returned by history. */
  160. export function entries(events: readonly SessionEvent[]): SessionEventEntry[] {
  161. return events.map(event => ({ type: 'event', event: event as unknown as SessionWireEvent }))
  162. }
  163. /** Build one view-less history response value. */
  164. export function historyValue(events: readonly SessionEvent[], hasMore = false): SessionPage {
  165. return {
  166. records: entries(events),
  167. hasMore,
  168. }
  169. }