event-script.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }
  41. /** One complete plain turn (turn/start → user → step → assistant → turn/end), 6 events from startSeq. */
  42. export function plainTurn(startSeq: number, turn: number, ask: string, answer: string): SessionEvent[] {
  43. return [
  44. ev.turnStart(startSeq, turn),
  45. ev.user(startSeq + 1, ask),
  46. ev.stepStart(startSeq + 2, turn),
  47. ev.assistant(startSeq + 3, turn, answer),
  48. ev.stepEnd(startSeq + 4, turn),
  49. ev.turnEnd(startSeq + 5, turn),
  50. ]
  51. }
  52. /** Wrap raw events as view-less history entries (the wire shape history now returns). */
  53. export function entries(events: readonly SessionEvent[]): { event: SessionEvent }[] {
  54. return events.map(event => ({ event }))
  55. }