File size: 21,583 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import type { AgentTranscriptSnapshot } from '../ops/operation';
import type { TranscriptAttachment } from '../model/attachment';
import type { TranscriptFrame, TranscriptUserOrigin } from '../model/frame';
import type { TranscriptItem, TranscriptMarker } from '../model/item';
import type { TurnOrigin } from '../model/turn';
import { daemonFileRefFromPairingPart } from '../contract/mediaRef';
import { projectTranscriptUserOrigin, projectTranscriptUserTurnOrigin } from '../contract/origin';

export type HistoryMediaSource =
  | { readonly kind: 'url'; readonly url: string }
  | { readonly kind: 'base64'; readonly media_type: string; readonly data: string }
  | { readonly kind: 'file' | 'session_media'; readonly file_id: string };

export type HistoryContentPart =
  | { readonly type: 'text'; readonly text: string }
  | { readonly type: 'think'; readonly think: string; readonly hidden?: boolean }
  | { readonly type: 'image' | 'video' | 'audio'; readonly source: HistoryMediaSource; readonly name?: string }
  | {
      readonly type: 'file';
      readonly file_id: string;
      readonly name: string;
      readonly media_type: string;
      readonly size: number;
    }
  | { readonly type: string };

export interface HistoryToolCall {
  readonly id: string;
  readonly name: string;
  readonly arguments: string | null;
}

export interface HistoryMessage {
  readonly id?: string;
  readonly role: string;
  readonly content?: readonly HistoryContentPart[];
  readonly toolCalls?: readonly HistoryToolCall[];
  readonly toolCallId?: string;
  readonly isError?: boolean;
  readonly origin?: { readonly kind: string };
}

interface TurnDraft {
  turnId: string;
  ordinal: number;
  triggerPromptId?: string;
  origin: TurnOrigin;
  prompt?: string;
  attachmentIds?: string[];
  steps: StepDraft[];
}

interface StepDraft {
  stepId: string;
  ordinal: number;
  frames: TranscriptFrame[];
}

const HIDDEN_USER_ORIGINS = new Set(['injection', 'system_trigger', 'retry']);
const TURN_OPENING_SYSTEM_TRIGGERS = new Set(['goal_continuation', 'subagent']);
const MARKER_USER_ORIGINS: Readonly<Record<string, string>> = {
  skill_activation: 'skill',
  plugin_command: 'skill',
  compaction_summary: 'compaction',
};

const FALLBACK_ORIGIN: TurnOrigin = { kind: 'other' };

export function groupMessagesIntoSnapshot(
  messages: readonly HistoryMessage[],
  options?: {
    readonly taskOriginTurnTaskIds?: ReadonlySet<string>;
    readonly steeredContents?: ReadonlyMap<string, ReadonlyMap<string, number>>;
  },
): AgentTranscriptSnapshot {
  const items: TranscriptItem[] = [];
  const attachments: TranscriptAttachment[] = [];
  const steeredContents = new Map(
    [...(options?.steeredContents ?? [])].map(([key, byKind]) => [key, new Map(byKind)]),
  );
  let turn: TurnDraft | undefined;
  let pendingNotificationFrames: {
    text: string;
    taskId: string | undefined;
    attachmentIds?: string[];
    promptIds?: readonly string[];
    origin?: TranscriptUserOrigin;
    steered?: boolean;
  }[] = [];
  let nextOrdinal = 0;
  let markerCount = 0;

  const foldTurnOpeningInput = (
    message: HistoryMessage,
  ): { text: string; attachmentIds?: string[] } => {
    const parts = message.content ?? [];
    const ids: string[] = [];
    const texts: string[] = [];
    for (const part of parts) {
      if (part.type === 'text' && 'text' in part && typeof part.text === 'string') {
        texts.push(part.text);
        continue;
      }
      if (part.type === 'image' || part.type === 'video' || part.type === 'audio') {
        if (!('source' in part) || part.source === undefined) continue;
        const source = part.source as HistoryMediaSource;
        const entity: TranscriptAttachment = {
          attachmentId: `att_${attachments.length + 1}`,
          mediaType:
            source.kind === 'base64' ? source.media_type : `${part.type}/*`,
          name: part.name,
          source:
            source.kind === 'url'
              ? { kind: 'url', url: source.url }
              : source.kind === 'file' || source.kind === 'session_media'
                ? { kind: source.kind, fileId: source.file_id }
                : undefined,
        };
        attachments.push(entity);
        ids.push(entity.attachmentId);
        continue;
      }
      if (part.type === 'file' && 'file_id' in part) {
        const entity: TranscriptAttachment = {
          attachmentId: `att_${attachments.length + 1}`,
          mediaType: part.media_type as string,
          name: part.name as string,
          size: part.size as number,
          source: { kind: 'file', fileId: part.file_id as string },
        };
        attachments.push(entity);
        ids.push(entity.attachmentId);
        continue;
      }
      const ref = daemonFileRefFromPairingPart(part);
      if (ref !== undefined) {
        const entity: TranscriptAttachment = {
          attachmentId: `att_${attachments.length + 1}`,
          mediaType: `${ref.kind}/*`,
          name: ref.name,
          source: { kind: 'session_media', fileId: ref.ref.fileId },
        };
        attachments.push(entity);
        ids.push(entity.attachmentId);
      }
    }
    for (const attachment of originFileAttachments(message)) {
      const entity: TranscriptAttachment = {
        attachmentId: `att_${attachments.length + 1}`,
        mediaType: attachment.mediaType,
        name: attachment.name,
        size: attachment.size,
      };
      attachments.push(entity);
      ids.push(entity.attachmentId);
    }
    return { text: texts.join(''), attachmentIds: ids.length > 0 ? ids : undefined };
  };

  const ensureTurn = (origin: TurnOrigin = FALLBACK_ORIGIN): TurnDraft => {
    if (!turn) {
      const ordinal = nextOrdinal;
      nextOrdinal += 1;
      turn = { turnId: `t${ordinal}`, ordinal, origin, steps: [] };
      items.push(draftToTurnItem(turn));
    }
    return turn;
  };

  const flushSteeredLeftovers = (): void => {
    const leftovers = pendingNotificationFrames.filter((pending) => pending.steered);
    if (leftovers.length === 0) return;
    pendingNotificationFrames = pendingNotificationFrames.filter((pending) => !pending.steered);
    for (const pending of leftovers) {
      const targetTurn = turn ?? startTurn({ kind: 'user' });
      let lastStep = targetTurn.steps.at(-1);
      if (lastStep === undefined) {
        const ordinal = targetTurn.steps.length + 1;
        lastStep = {
          stepId: `${targetTurn.turnId}.${ordinal}`,
          ordinal,
          frames: [],
        };
        targetTurn.steps.push(lastStep);
      }
      lastStep.frames.push({
        kind: 'text',
        frameId: `${lastStep.stepId}.f${lastStep.frames.length + 1}`,
        role: 'user',
        text: pending.text,
        attachmentIds: pending.attachmentIds,
        promptIds: pending.promptIds,
        origin: pending.origin,
      });
      syncTurnItem(items, targetTurn);
    }
  };

  const startTurn = (
    origin: TurnOrigin,
    prompt?: string,
    attachmentIds?: string[],
    triggerPromptId?: string,
  ): TurnDraft => {
    flushSteeredLeftovers();
    const ordinal = nextOrdinal;
    nextOrdinal += 1;
    pendingNotificationFrames = [];
    turn = { turnId: `t${ordinal}`, ordinal, triggerPromptId, origin, prompt, attachmentIds, steps: [] };
    items.push(draftToTurnItem(turn));
    return turn;
  };

  const pushMarker = (marker: string, payload?: unknown): void => {
    markerCount += 1;
    const item: TranscriptMarker = { kind: 'marker', markerId: `m${markerCount}`, marker, payload };
    items.push(item);
  };

  let prevNonTaskRole: string | undefined;
  for (const message of messages) {
    if (message.role === 'system') continue;
    const originKind = message.origin?.kind;
    const isTaskOrigin =
      originKind === 'task' || originKind === 'background_task' || originKind === 'task_notification';
    const prevRoleAtEntry = prevNonTaskRole;
    if (!isTaskOrigin) prevNonTaskRole = message.role;

    if (message.role === 'user') {
      if (originKind !== undefined && HIDDEN_USER_ORIGINS.has(originKind)) {
        if (opensOwnTurn(message)) {
          const opening =
            (message.origin as { name?: unknown }).name === 'subagent'
              ? foldTurnOpeningInput(message)
              : undefined;
          startTurn(mapOrigin(message), opening?.text || undefined, opening?.attachmentIds);
        }
        continue;
      }
      const markerKey = originKind !== undefined ? MARKER_USER_ORIGINS[originKind] : undefined;
      if (markerKey !== undefined && !isUserSlashPrompt(message)) {
        pushMarker(markerKey, { text: textOf(message), origin: message.origin });
        continue;
      }
      const contentKey = JSON.stringify(message.content ?? []);
      const steerKind = originKind ?? 'user';
      const steeredByKind = steeredContents.get(contentKey);
      const steeredRemaining = steeredByKind?.get(steerKind) ?? 0;
      if (steeredByKind !== undefined && steeredRemaining > 0) {
        steeredByKind.set(steerKind, steeredRemaining - 1);
        const bundled = bundledSkillActivations(message);
        const parts = message.content ?? [];
        bundled.forEach((activation, index) => {
          const block = parts[index];
          pushMarker('skill', {
            text: block !== undefined && block.type === 'text' && 'text' in block ? block.text : '',
            origin: { kind: 'skill_activation', trigger: 'user-slash', ...activation },
          });
        });
        const opening = foldTurnOpeningInput({ ...message, content: parts.slice(bundled.length) });
        pendingNotificationFrames.push({
          text: opening.text,
          taskId: undefined,
          attachmentIds: opening.attachmentIds,
          origin: projectTranscriptUserOrigin(message.origin),
          steered: true,
        });
        continue;
      }
      if (markerKey !== undefined) {
        const opening = isUserSlashPrompt(message) ? foldTurnOpeningInput(message) : undefined;
        pushMarker(markerKey, { text: opening?.text ?? textOf(message), origin: message.origin });
        if (opening !== undefined) {
          startTurn(mapOrigin(message), opening.text, opening.attachmentIds, triggerPromptIdOf(message));
        }
        continue;
      }
      if (isTaskOrigin) {
        const origin = message.origin as { taskId?: unknown } | undefined;
        const taskId = typeof origin?.taskId === 'string' ? origin.taskId : undefined;
        const opensOwn = options?.taskOriginTurnTaskIds === undefined
          ? prevRoleAtEntry !== 'assistant' && prevRoleAtEntry !== 'tool'
          : taskId === undefined ||
            options.taskOriginTurnTaskIds.has(taskId) ||
            originKind === 'background_task';
        if (opensOwn) {
          const opening = foldTurnOpeningInput(message);
          startTurn(mapOrigin(message), opening.text, opening.attachmentIds);
          continue;
        }
        pendingNotificationFrames.push({ text: notificationFrameText(textOf(message)), taskId });
        continue;
      }
      const bundled = bundledSkillActivations(message);
      if (bundled.length > 0) {
        const parts = message.content ?? [];
        bundled.forEach((activation, index) => {
          const block = parts[index];
          pushMarker('skill', {
            text: block !== undefined && block.type === 'text' && 'text' in block ? block.text : '',
            origin: { kind: 'skill_activation', trigger: 'user-slash', ...activation },
          });
        });
        const callerMessage = { ...message, content: parts.slice(bundled.length) };
        const opening = foldTurnOpeningInput(callerMessage);
        startTurn(mapOrigin(message), opening.text, opening.attachmentIds, triggerPromptIdOf(message));
        continue;
      }
      const opening = foldTurnOpeningInput(message);
      startTurn(mapOrigin(message), opening.text, opening.attachmentIds, triggerPromptIdOf(message));
      continue;
    }

    if (message.role === 'assistant') {
      const current = ensureTurn();
      const stepOrdinal = current.steps.length + 1;
      const step: StepDraft = {
        stepId: `${current.turnId}.${stepOrdinal}`,
        ordinal: stepOrdinal,
        frames: [],
      };
      current.steps.push(step);
      let frameCount = 0;
      const nextFrameId = (): string => {
        frameCount += 1;
        return `${step.stepId}.f${frameCount}`;
      };
      for (const pending of pendingNotificationFrames) {
        step.frames.push({
          kind: 'text',
          frameId: nextFrameId(),
          role: 'user',
          text: pending.text,
          taskId: pending.taskId,
          attachmentIds: pending.attachmentIds,
          promptIds: pending.promptIds,
          origin: pending.origin,
        });
      }
      pendingNotificationFrames = [];
      for (const part of message.content ?? []) {
        if (part.type === 'text' && 'text' in part && typeof part.text === 'string' && part.text.length > 0) {
          step.frames.push({ kind: 'text', frameId: nextFrameId(), role: 'assistant', text: part.text });
        } else if (part.type === 'think' && 'think' in part && typeof part.think === 'string' && part.think.length > 0 && part.hidden !== true) {
          step.frames.push({ kind: 'thinking', frameId: nextFrameId(), text: part.think });
        }
      }
      for (const call of message.toolCalls ?? []) {
        step.frames.push({
          kind: 'tool',
          frameId: `${step.stepId}.${call.id}`,
          toolCallId: call.id,
          name: call.name,
          state: 'running',
          input: parseArguments(call.arguments),
        });
      }
      syncTurnItem(items, current);
      continue;
    }

    if (message.role === 'tool') {
      const frame = currentTurnToolFrame(turn, message.toolCallId);
      if (frame && frame.kind === 'tool') {
        const output = textOf(message);
        const patched: TranscriptFrame = {
          ...frame,
          state: message.isError ? 'error' : 'done',
          output,
          error: message.isError ? output : undefined,
        };
        replaceToolFrame(turn!, message.toolCallId!, patched);
        syncTurnItem(items, turn!);
      }
    }
  }

  flushSteeredLeftovers();

  return { items, tasks: [], interactions: [], attachments, todos: [], prompts: [], meta: {} };
}

function notificationFrameText(text: string): string {
  if (!text.startsWith('<notification')) return text;
  const openingEnd = text.indexOf('>');
  const closingStart = text.lastIndexOf('</notification>');
  if (openingEnd === -1 || closingStart <= openingEnd) return text;
  const inner = text.slice(openingEnd + 1, closingStart);
  const lines = inner.split('\n');
  let headerEnd = 0;
  while (headerEnd < lines.length && lines[headerEnd]!.trim() === '') headerEnd += 1;
  let title = '';
  let bodyStart = headerEnd;
  const titleLine = lines[bodyStart];
  if (titleLine !== undefined && titleLine.startsWith('Title: ')) {
    title = titleLine.slice('Title: '.length);
    bodyStart += 1;
  }
  const severityLine = lines[bodyStart];
  if (severityLine !== undefined && severityLine.startsWith('Severity: ')) {
    bodyStart += 1;
  }
  const bodyLines = lines.slice(bodyStart);
  const childStart = bodyLines.findIndex((line) => {
    const trimmed = line.trimStart();
    return (
      trimmed.startsWith('<output-file') ||
      trimmed.startsWith('<output-preview') ||
      trimmed.startsWith('<answer')
    );
  });
  const body = (childStart === -1 ? bodyLines : bodyLines.slice(0, childStart)).join('\n').trim();
  if (title.length > 0 && body.length > 0) return `${title}\n${body}`;
  return title.length > 0 ? title : body.length > 0 ? body : text;
}

function opensOwnTurn(message: HistoryMessage): boolean {
  const origin = message.origin as { kind?: unknown; name?: unknown } | undefined;
  return (
    origin?.kind === 'system_trigger' &&
    typeof origin.name === 'string' &&
    TURN_OPENING_SYSTEM_TRIGGERS.has(origin.name)
  );
}

function isUserSlashPrompt(message: HistoryMessage): boolean {
  const origin = message.origin as { kind?: unknown; trigger?: unknown } | undefined;
  return (
    (origin?.kind === 'skill_activation' || origin?.kind === 'plugin_command') &&
    origin.trigger === 'user-slash'
  );
}

function triggerPromptIdOf(message: HistoryMessage): string | undefined {
  if (typeof message.id !== 'string' || message.id.length === 0) return undefined;
  const origin = message.origin as { kind?: unknown; trigger?: unknown } | undefined;
  if (origin?.kind === undefined || origin.kind === 'user') return message.id;
  return (
    (origin.kind === 'skill_activation' || origin.kind === 'plugin_command') &&
    origin.trigger === 'user-slash'
  )
    ? message.id
    : undefined;
}

function mapOrigin(message: HistoryMessage): TurnOrigin {
  const origin = message.origin;
  switch (origin?.kind) {
    case 'cron_job':
    case 'cron_missed': {
      const jobId = (origin as { jobId?: unknown }).jobId;
      return { kind: 'cron', taskId: typeof jobId === 'string' ? jobId : undefined, payload: origin };
    }
    case 'task':
    case 'background_task': {
      const taskId = (origin as { taskId?: unknown }).taskId;
      return taskId !== undefined && typeof taskId === 'string'
        ? { kind: 'task', taskId, payload: origin }
        : { kind: 'other', payload: origin };
    }
    case 'hook_result':
      return { kind: 'hook', payload: origin };
    case 'shell_command':
      return { kind: 'user', payload: origin };
    case 'user':
      return projectTranscriptUserTurnOrigin(origin);
    case undefined:
      return { kind: 'user' };
    default:
      return { kind: 'other', payload: origin };
  }
}

interface BundledSkillActivation {
  readonly activationId: string;
  readonly skillName: string;
  readonly skillArgs?: string;
  readonly skillType?: string;
  readonly skillPath?: string;
  readonly skillSource?: string;
}

function bundledSkillActivations(message: HistoryMessage): readonly BundledSkillActivation[] {
  if (message.origin?.kind !== 'user') return [];
  const activations = (message.origin as { readonly skillActivations?: unknown }).skillActivations;
  if (!Array.isArray(activations)) return [];
  return activations.filter(
    (activation): activation is BundledSkillActivation =>
      typeof activation === 'object' &&
      activation !== null &&
      typeof (activation as { activationId?: unknown }).activationId === 'string' &&
      typeof (activation as { skillName?: unknown }).skillName === 'string',
  );
}

interface OriginFileAttachment {
  readonly name: string;
  readonly mediaType: string;
  readonly size: number;
  readonly path: string;
}

function originFileAttachments(message: HistoryMessage): readonly OriginFileAttachment[] {
  if (message.origin?.kind !== 'user' && message.origin?.kind !== 'skill_activation') return [];
  const attachments = (message.origin as { readonly attachments?: unknown }).attachments;
  if (!Array.isArray(attachments)) return [];
  return attachments.filter(
    (attachment): attachment is OriginFileAttachment =>
      typeof attachment === 'object' &&
      attachment !== null &&
      typeof (attachment as { name?: unknown }).name === 'string' &&
      typeof (attachment as { mediaType?: unknown }).mediaType === 'string' &&
      typeof (attachment as { size?: unknown }).size === 'number' &&
      typeof (attachment as { path?: unknown }).path === 'string',
  );
}

function textOf(message: HistoryMessage): string {
  return (message.content ?? [])
    .filter((part): part is { readonly type: 'text'; readonly text: string } => part.type === 'text' && 'text' in part)
    .map((part) => part.text)
    .join('');
}

function parseArguments(raw: string | null): unknown {
  if (!raw) return undefined;
  try {
    return JSON.parse(raw) as unknown;
  } catch {
    return raw;
  }
}

function draftToTurnItem(draft: TurnDraft): TranscriptItem {
  return {
    kind: 'turn',
    turnId: draft.turnId,
    triggerPromptId: draft.triggerPromptId,
    ordinal: draft.ordinal,
    state: 'completed',
    origin: draft.origin,
    prompt: draft.prompt,
    attachmentIds: draft.attachmentIds,
    steps: draft.steps.map((step) => ({
      kind: 'step' as const,
      stepId: step.stepId,
      turnId: draft.turnId,
      ordinal: step.ordinal,
      state: 'completed' as const,
      frames: step.frames,
    })),
  };
}

function syncTurnItem(items: TranscriptItem[], draft: TurnDraft): void {
  const index = items.findIndex((entry) => entry.kind === 'turn' && entry.turnId === draft.turnId);
  if (index >= 0) items[index] = draftToTurnItem(draft);
}

function currentTurnToolFrame(turn: TurnDraft | undefined, toolCallId: string | undefined): TranscriptFrame | undefined {
  if (!turn || toolCallId === undefined) return undefined;
  for (let s = turn.steps.length - 1; s >= 0; s -= 1) {
    const frames = turn.steps[s]?.frames ?? [];
    for (let f = frames.length - 1; f >= 0; f -= 1) {
      const frame = frames[f];
      if (frame?.kind === 'tool' && frame.toolCallId === toolCallId) return frame;
    }
  }
  return undefined;
}

function replaceToolFrame(turn: TurnDraft, toolCallId: string, next: TranscriptFrame): void {
  for (let s = turn.steps.length - 1; s >= 0; s -= 1) {
    const step = turn.steps[s];
    if (!step) continue;
    const index = step.frames.findIndex((frame) => frame.kind === 'tool' && frame.toolCallId === toolCallId);
    if (index >= 0) {
      step.frames[index] = next;
      return;
    }
  }
}