1
>
/*---------------------------------------------------------------------------------------------
codexReplayMapper.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { generateUuid } from '../../../../base/common/uuid.js';
7
>
import { toToolCallMeta } from '../../common/meta/agentToolCallMeta.js';
8
>
import {
9
>
MessageKind,
10
>
ResponsePartKind,
11
>
ToolCallConfirmationReason,
12
>
ToolCallStatus,
13
>
ToolResultContentType,
14
>
type ResponsePart,
15
>
type ToolCallResponsePart,
16
>
type ToolResultContent,
17
>
type Turn,
18
>
} from '../../common/state/sessionState.js';
19
>
import {
20
>
describeFileChange,
21
>
describeWebSearch,
22
>
fileChangeOutput,
23
>
turnStateFromStatus,
24
>
} from './codexMapAppServerEvents.js';
25
>
import { unwrapShellInvocation } from './codexShellCommand.js';
26
>
import type { Thread } from './protocol/generated/v2/Thread.js';
27
>
import type { ThreadItem } from './protocol/generated/v2/ThreadItem.js';
28
>
import type { Turn as CodexTurn } from './protocol/generated/v2/Turn.js';
29
>
30
>
/**
31
>
* Reconstruct protocol {@link Turn}s from codex's `thread/read` response.
32
>
*
33
>
* Codex stores each conversation as a stream of {@link CodexTurn}, each
34
>
* with an array of {@link ThreadItem}s. We collapse that into the agent
35
>
* host's turn shape: each user message opens a turn; subsequent assistant
36
>
* items become response parts on that turn until `turn/completed` closes it.
37
>
*
38
>
* Produces:
39
>
* - `userMessage` → opens a `Turn` with `userMessage: { text }`
40
>
* - `agentMessage` → `MarkdownResponsePart` with the full text
41
>
* - `commandExecution` → completed terminal `ToolCallResponsePart`
42
>
* - `webSearch` → completed web-search `ToolCallResponsePart`
43
>
* - `fileChange` → completed file-edit `ToolCallResponsePart`
44
>
* - everything else → currently dropped (reasoning/plan/mcp/collab)
45
>
*
46
>
* Mirrors the live mapper's translation kernel — including the sandbox
47
>
* pre-flight coalescing (see {@link codexMapAppServerEvents}) — so restored
48
>
* sessions render identically to active ones.
49
>
*/
50
>
export function replayThreadToTurns(thread: Thread): Turn[] {
51
const turns: Turn[] = [];
52
for (const codexTurn of thread.turns ?? []) {