event-script.client.ts 6.1 KB

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