File size: 7,029 Bytes
09aec41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {
  ToolCallId, createMessage, createToolResultMessage, createUserMessage,
} from '@deepseek-ai/dsh-llm'
import { SessionSeq } from '@deepseek-ai/dsh-session/types'
// Minimal SessionEvent builders for orchestration tests (shape mirrors what the
// host emits; only the fields the object layer reads).
import type { ContentBlock } from '@deepseek-ai/dsh-llm/types'
import type { SessionEvent } from '@deepseek-ai/dsh-session/types'
import type {
  SessionEventEntry,
  SessionPage,
  SessionWireEvent,
} from '../src/types.ts'

/** One text content block (local helper). */
const text = (t: string): ContentBlock[] => [{ type: 'text', text: t }]

const at = (seq: SessionSeq, e: Record<string, unknown>): SessionEvent =>
  ({ seq, time: 1_700_000_000_000 + seq, ...e }) as unknown as SessionEvent

export const ev = {
  turnStart: (seq: SessionSeq, turn: number): SessionEvent =>
    at(seq, { type: 'turn/start', data: { turn } }),
  user: (seq: SessionSeq, body: string): SessionEvent =>
    at(seq, { type: 'user/message', surfaceOp: 'append', data: createUserMessage({
      content: text(body), source: { kind: 'user' },
    }) }),
  stepStart: (seq: SessionSeq, turn: number, step = 0): SessionEvent =>
    at(seq, { type: 'step/start', data: { turn, step } }),
  assistant: (seq: SessionSeq, turn: number, body: string, step = 0): SessionEvent =>
    at(seq, { type: 'assistant/message', surfaceOp: 'append', data: {
      turn, step,
      stream: [
        { type: 'chunk', time: 1_700_000_000_000 + seq, chunk: { type: 'block-start', index: 0, blockType: 'text' } },
        { type: 'text-chunks', time0: 1_700_000_000_000 + seq, index: 0, dt: [], texts: [body] },
        {
          type: 'chunk', time: 1_700_000_000_000 + seq,
          chunk: { type: 'block-end', index: 0, block: { type: 'text', text: body } },
        },
        { type: 'chunk', time: 1_700_000_000_000 + seq, chunk: { type: 'finish', reason: { kind: 'stop' } } },
      ],
      message: createMessage({
        role: 'assistant',
        content: text(body),
        source: {
          kind: 'model',
          ...{ provider: 'fake', model: 'fk-1' },
        },
      }),
    } }),
  toolCall: (seq: SessionSeq, turn: number, callId: string, name: string, args: string, step = 0): SessionEvent =>
    at(seq, { type: 'tool/call', data: { turn, step, callId, name, arguments: args } }),
  toolResult: (seq: SessionSeq, turn: number, callId: string, body: string, step = 0): SessionEvent =>
    at(seq, {
      type: 'tool/result',
      surfaceOp: 'append',
      data: {
        turn,
        step,
        message: createToolResultMessage({
          callId: ToolCallId(callId),
          content: text(body),
          isError: false,
        }),
      },
    }),
  codeDispatchStart: (seq: SessionSeq, parentCallId: string, n: number, name: string, args: unknown): SessionEvent =>
    at(seq, {
      type: 'tool/ptc-dispatch-start',
      data: { rootCallId: parentCallId, parentCallId, subCallId: `${parentCallId}:ptc:${n}`, name, arguments: args },
    }),
  codeDispatch: (
    seq: SessionSeq,
    parentCallId: string,
    n: number,
    name: string,
    args: unknown,
    body: string,
    isError = false,
  ): SessionEvent =>
    at(seq, {
      type: 'tool/ptc-dispatch',
      data: { rootCallId: parentCallId, parentCallId, subCallId: `${parentCallId}:ptc:${n}`, name, arguments: args, isError, content: text(body) },
    }),
  stepEnd: (seq: SessionSeq, turn: number, step = 0): SessionEvent =>
    at(seq, { type: 'step/end', data: { turn, step } }),
  retry: (
    seq: SessionSeq,
    turn: number,
    step = 0,
    retry = 1,
    maxRetries = 2,
    delayMs = 500,
    message = 'temporary transport failure',
  ): SessionEvent =>
    at(seq, {
      type: 'llm/retry',
      data: {
        turn, step,
        provider: 'fake', mode: 'normal', policyKey: 'fake-normal',
        retry, maxRetries, delayMs,
        failure: { code: 'TRANSPORT', message },
      },
    }),
  turnEnd: (seq: SessionSeq, turn: number, reason: 'completed' | 'aborted' | 'disposed' = 'completed'): SessionEvent =>
    at(seq, { type: 'turn/end', data: {
      turn,
      reason: reason === 'completed'
        ? { kind: 'completed' }
        : { kind: 'aborted', reason: { kind: reason === 'disposed' ? 'disposed' : 'user' } },
    } }),
  commandRun: (seq: SessionSeq, commandId: string, name: string, args = ''): SessionEvent =>
    at(seq, { type: 'command/run', data: { commandId, name, args, source: { kind: 'user' } } }),
  commandRunWithoutInput: (seq: SessionSeq, commandId: string, name: string): SessionEvent =>
    at(seq, { type: 'command/run', data: { commandId, name, source: { kind: 'user' } } }),
  commandDone: (
    seq: SessionSeq,
    commandId: string,
    kind: 'success' | 'error' = 'success',
    text?: string,
    sourceEventSeq?: SessionSeq,
  ): SessionEvent =>
    at(seq, { type: 'command/done', data: {
      commandId,
      kind,
      ...text === undefined ? {} : { text },
      ...sourceEventSeq === undefined ? {} : { sourceEventSeq },
    } }),
  /** A compaction's log-only `compaction/summary` record. */
  compactSummary: (seq: SessionSeq, summary: string, start: SessionSeq, end: SessionSeq): SessionEvent =>
    at(seq, { type: 'compaction/summary', data: {
      summary: text(summary),
      shadowedRange: { start, end },
      shadowedSeqs: [start, end],
      shadowedTokenCount: 100,
      provider: 'fake',
      model: 'compact-1',
    } }),
  /** The replacement user message a compaction backend lands (the checkpoint). */
  compactCheckpoint: (
    seq: SessionSeq,
    summarySeq: SessionSeq,
    start: SessionSeq,
    end: SessionSeq,
  ): SessionEvent =>
    at(seq, {
      type: 'user/message',
      surfaceOp: { op: 'replace', startSeq: start, endSeq: end },
      sourceEventSeqs: [summarySeq, start, end],
      data: createUserMessage({
        content: text('<context_checkpoint>model only</context_checkpoint>'),
        source: { kind: 'plugin', plugin: 'compact' },
      }),
    }),
}

/** One complete plain turn (turn/start → user → step → assistant → turn/end), 6 events from startSeq. */
export function plainTurn(startSeq: SessionSeq, turn: number, ask: string, answer: string): SessionEvent[] {
  return [
    ev.turnStart(startSeq, turn),
    ev.user(SessionSeq(startSeq + 1), ask),
    ev.stepStart(SessionSeq(startSeq + 2), turn),
    ev.assistant(SessionSeq(startSeq + 3), turn, answer),
    ev.stepEnd(SessionSeq(startSeq + 4), turn),
    ev.turnEnd(SessionSeq(startSeq + 5), turn),
  ]
}

/** Wrap raw events in the journal envelope returned by history. */
export function entries(events: readonly SessionEvent[]): SessionEventEntry[] {
  return events.map(event => ({ type: 'event', event: event as unknown as SessionWireEvent }))
}

/** Build one view-less history response value. */
export function historyValue(events: readonly SessionEvent[], hasMore = false): SessionPage {
  return {
    records: entries(events),
    hasMore,
  }
}