event-script.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Minimal SessionEvent builders for orchestration tests (shape mirrors what the
  2. // host emits; only the fields the object layer reads).
  3. import type { ContentBlock } from '@deepseek-ai/dsh-llm/types'
  4. import type { SessionEvent } from '@deepseek-ai/dsh-session/types'
  5. /** One text content block (local helper). */
  6. const text = (t: string): ContentBlock[] => [{ type: 'text', text: t }]
  7. const at = (seq: number, e: Record<string, unknown>): SessionEvent =>
  8. ({ seq, time: 1_700_000_000_000 + seq, ...e }) as unknown as SessionEvent
  9. export const ev = {
  10. turnStart: (seq: number, turn: number): SessionEvent =>
  11. at(seq, { type: 'turn/start', data: { turn, trigger: { kind: 'message', source: { kind: 'user' } } } }),
  12. user: (seq: number, body: string): SessionEvent =>
  13. at(seq, { type: 'user/message', surfaceOp: 'append', data: { content: text(body), source: { kind: 'user' } } }),
  14. stepStart: (seq: number, turn: number, step = 0): SessionEvent =>
  15. at(seq, { type: 'step/start', data: { turn, step } }),
  16. chunkStart: (seq: number, turn: number, step = 0, index = 0): SessionEvent =>
  17. at(seq, { type: 'assistant/chunk', data: { turn, step, chunk: { type: 'block-start', index, blockType: 'text' } } }),
  18. chunkText: (seq: number, turn: number, piece: string, step = 0, index = 0): SessionEvent =>
  19. at(seq, { type: 'assistant/chunk', data: { turn, step, chunk: { type: 'text-delta', index, text: piece } } }),
  20. assistant: (seq: number, turn: number, body: string, step = 0): SessionEvent =>
  21. at(seq, { type: 'assistant/message', surfaceOp: 'append', data: { turn, step, content: text(body), provenance: { provider: 'fake', model: 'fk-1' } } }),
  22. toolCall: (seq: number, turn: number, callId: string, name: string, args: string, step = 0): SessionEvent =>
  23. at(seq, { type: 'tool/call', data: { turn, step, callId, name, arguments: args } }),
  24. toolResult: (seq: number, turn: number, callId: string, body: string, step = 0): SessionEvent =>
  25. at(seq, { type: 'tool/result', surfaceOp: 'append', data: { turn, step, callId, content: text(body), isError: false } }),
  26. codeDispatchStart: (seq: number, parentCallId: string, n: number, name: string, args: unknown): SessionEvent =>
  27. at(seq, {
  28. type: 'tool/code-dispatch-start',
  29. data: { parentCallId, subCallId: `${parentCallId}:code:${n}`, name, arguments: args },
  30. }),
  31. codeDispatch: (seq: number, parentCallId: string, n: number, name: string, args: unknown, body: string, isError = false): SessionEvent =>
  32. at(seq, {
  33. type: 'tool/code-dispatch',
  34. data: { parentCallId, subCallId: `${parentCallId}:code:${n}`, name, arguments: args, isError, content: text(body) },
  35. }),
  36. stepEnd: (seq: number, turn: number, step = 0): SessionEvent =>
  37. at(seq, { type: 'step/end', data: { turn, step } }),
  38. turnEnd: (seq: number, turn: number, reason: 'completed' | 'cancelled' = 'completed'): SessionEvent =>
  39. at(seq, { type: 'turn/end', data: { turn, reason: { kind: reason } } }),
  40. todoWrite: (seq: number, todos: { content: string; status: 'pending' | 'in_progress' | 'completed' }[]): SessionEvent =>
  41. at(seq, { type: 'todo/write', data: { todos } }),
  42. }
  43. /** One complete plain turn (turn/start → user → step → assistant → turn/end), 6 events from startSeq. */
  44. export function plainTurn(startSeq: number, turn: number, ask: string, answer: string): SessionEvent[] {
  45. return [
  46. ev.turnStart(startSeq, turn),
  47. ev.user(startSeq + 1, ask),
  48. ev.stepStart(startSeq + 2, turn),
  49. ev.assistant(startSeq + 3, turn, answer),
  50. ev.stepEnd(startSeq + 4, turn),
  51. ev.turnEnd(startSeq + 5, turn),
  52. ]
  53. }
  54. /** Wrap raw events as view-less history entries (the wire shape history now returns). */
  55. export function entries(events: readonly SessionEvent[]): { event: SessionEvent }[] {
  56. return events.map(event => ({ event }))
  57. }