event-script.ts 5.7 KB

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