@jrkropp/codex-js 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +2 -2
- package/dist/ClientNotification-B6-FhXQf.d.ts +5 -0
- package/dist/DynamicToolCallResponse-82DFjES2.d.ts +8 -0
- package/dist/DynamicToolSpec-CfnhqAYK.d.ts +29 -0
- package/dist/PermissionsRequestApprovalResponse-DxzPPDRb.d.ts +55 -0
- package/dist/ProviderStatusBanner-BlP6lzwE.d.ts +441 -0
- package/dist/ServerRequest-B5cKVJjr.d.ts +2181 -0
- package/dist/{session-DPhHN7RZ.d.ts → ThreadResumeResponse-DvmE1juU.d.ts} +3 -306
- package/dist/ToolRequestUserInputQuestion-CeZa5X1J.d.ts +23 -0
- package/dist/ToolRequestUserInputResponse-zcPLwbiK.d.ts +17 -0
- package/dist/TurnSteerResponse-0kBCfplh.d.ts +209 -0
- package/dist/WebSearchToolConfig-D3ep0625.d.ts +18 -0
- package/dist/chat-runtime-9RkXHC_w.d.ts +382 -0
- package/dist/chunk-2DZRMCI2.js +1258 -0
- package/dist/chunk-2DZRMCI2.js.map +1 -0
- package/dist/chunk-4DPLJPB5.js +396 -0
- package/dist/chunk-4DPLJPB5.js.map +1 -0
- package/dist/chunk-5JMJ6OI5.js +3 -0
- package/dist/chunk-5JMJ6OI5.js.map +1 -0
- package/dist/chunk-6ZMJ34KE.js +1153 -0
- package/dist/chunk-6ZMJ34KE.js.map +1 -0
- package/dist/chunk-CGBS37IU.js +128 -0
- package/dist/chunk-CGBS37IU.js.map +1 -0
- package/dist/chunk-DCMKA2A6.js +18 -0
- package/dist/chunk-DCMKA2A6.js.map +1 -0
- package/dist/chunk-DYLHN3HG.js +937 -0
- package/dist/chunk-DYLHN3HG.js.map +1 -0
- package/dist/{chunk-SVK6PLGO.js → chunk-LWQNX4LI.js} +12009 -18768
- package/dist/chunk-LWQNX4LI.js.map +1 -0
- package/dist/{chunk-JLDH4U5L.js → chunk-NCI4MAWZ.js} +317 -1967
- package/dist/chunk-NCI4MAWZ.js.map +1 -0
- package/dist/chunk-O44XP7LH.js +214 -0
- package/dist/chunk-O44XP7LH.js.map +1 -0
- package/dist/chunk-PST3ZWX2.js +555 -0
- package/dist/chunk-PST3ZWX2.js.map +1 -0
- package/dist/chunk-SYPHCDRD.js +1133 -0
- package/dist/chunk-SYPHCDRD.js.map +1 -0
- package/dist/chunk-V4BMZWBM.js +2401 -0
- package/dist/chunk-V4BMZWBM.js.map +1 -0
- package/dist/chunk-W7S6HFCQ.js +1983 -0
- package/dist/chunk-W7S6HFCQ.js.map +1 -0
- package/dist/chunk-YHVCFD2D.js +117 -0
- package/dist/chunk-YHVCFD2D.js.map +1 -0
- package/dist/chunk-Z63UPBS3.js +152 -0
- package/dist/chunk-Z63UPBS3.js.map +1 -0
- package/dist/client/index.d.ts +16 -4
- package/dist/client/index.js +13 -1
- package/dist/codex-rs/app-server/index.d.ts +161 -0
- package/dist/codex-rs/app-server/index.js +13 -0
- package/dist/codex-rs/app-server/index.js.map +1 -0
- package/dist/codex-rs/app-server-protocol/index.d.ts +1722 -0
- package/dist/codex-rs/app-server-protocol/index.js +6 -0
- package/dist/codex-rs/app-server-protocol/index.js.map +1 -0
- package/dist/codex-rs/app-server-protocol/protocol.d.ts +19 -0
- package/dist/codex-rs/app-server-protocol/protocol.js +4 -0
- package/dist/codex-rs/app-server-protocol/protocol.js.map +1 -0
- package/dist/codex-rs/codex-api/index.d.ts +104 -0
- package/dist/codex-rs/codex-api/index.js +11 -0
- package/dist/codex-rs/codex-api/index.js.map +1 -0
- package/dist/codex-rs/config/index.d.ts +88 -0
- package/dist/codex-rs/config/index.js +4 -0
- package/dist/codex-rs/config/index.js.map +1 -0
- package/dist/codex-rs/core/config/index.d.ts +61 -0
- package/dist/codex-rs/core/config/index.js +5 -0
- package/dist/codex-rs/core/config/index.js.map +1 -0
- package/dist/codex-rs/core/index.d.ts +1393 -0
- package/dist/codex-rs/core/index.js +11 -0
- package/dist/codex-rs/core/index.js.map +1 -0
- package/dist/codex-rs/model-provider/index.d.ts +2 -0
- package/dist/codex-rs/model-provider/index.js +4 -0
- package/dist/codex-rs/model-provider/index.js.map +1 -0
- package/dist/codex-rs/models-manager/index.d.ts +2 -0
- package/dist/codex-rs/models-manager/index.js +4 -0
- package/dist/codex-rs/models-manager/index.js.map +1 -0
- package/dist/codex-rs/parity.d.ts +26 -0
- package/dist/codex-rs/parity.js +3 -0
- package/dist/codex-rs/parity.js.map +1 -0
- package/dist/codex-rs/thread-store/index.d.ts +5 -0
- package/dist/codex-rs/thread-store/index.js +4 -0
- package/dist/codex-rs/thread-store/index.js.map +1 -0
- package/dist/codex-rs/unsupported.d.ts +15 -0
- package/dist/codex-rs/unsupported.js +22 -0
- package/dist/codex-rs/unsupported.js.map +1 -0
- package/dist/codex-rs/utils/output-truncation.d.ts +21 -0
- package/dist/codex-rs/utils/output-truncation.js +4 -0
- package/dist/codex-rs/utils/output-truncation.js.map +1 -0
- package/dist/codex-rs/utils/string.d.ts +7 -0
- package/dist/codex-rs/utils/string.js +3 -0
- package/dist/codex-rs/utils/string.js.map +1 -0
- package/dist/common-CTyph5x8.d.ts +40 -0
- package/dist/event-mapping-CbISdQ1D.d.ts +43 -0
- package/dist/history-CfM-4V7b.d.ts +1654 -0
- package/dist/index-77U_Oc-a.d.ts +63 -0
- package/dist/index-CoDZosq0.d.ts +261 -0
- package/dist/index.d.ts +18 -7
- package/dist/index.js +16 -2
- package/dist/lib-nXlaKiS-.d.ts +48 -0
- package/dist/live-thread-BMvlflzM.d.ts +30 -0
- package/dist/merge-B_AWVmnI.d.ts +24 -0
- package/dist/mod-DYVLSWO4.d.ts +91 -0
- package/dist/plan-mode-Cv6KWb_S.d.ts +14 -0
- package/dist/proposed-plan-DpN1ma0Y.d.ts +53 -0
- package/dist/protocol-mpBcYHrm.d.ts +1655 -0
- package/dist/react/index.d.ts +56 -48
- package/dist/react/index.js +16 -2
- package/dist/{remote-_6TDvg-g.d.ts → remote-ClZbq9KN.d.ts} +3 -1
- package/dist/rendered-thread-AOxw3V5b.d.ts +29 -0
- package/dist/responses_websocket-BhxSgCzK.d.ts +183 -0
- package/dist/runtime-Cm6ml53h.d.ts +528 -0
- package/dist/server/index.d.ts +29 -2416
- package/dist/server/index.js +13 -1
- package/dist/session-BRYzi8OT.d.ts +46 -0
- package/dist/shadcn/index.d.ts +1 -1
- package/dist/{sidebar-DT2XoitN.d.ts → sidebar-DMMij22z.d.ts} +1 -1
- package/dist/spec_plan_types-CmsJ-Tfn.d.ts +260 -0
- package/dist/{store-GYldc9EJ.d.ts → store-AGRxhgQ3.d.ts} +2 -1
- package/dist/t3code/apps/web/components/chat.d.ts +508 -0
- package/dist/t3code/apps/web/components/chat.js +12 -0
- package/dist/t3code/apps/web/components/chat.js.map +1 -0
- package/dist/t3code/apps/web/index.d.ts +12 -0
- package/dist/t3code/apps/web/index.js +13 -0
- package/dist/t3code/apps/web/index.js.map +1 -0
- package/dist/testing/index.d.ts +9 -91
- package/dist/testing/index.js +13 -1
- package/dist/thread-history-builder-zW0zeqcS.d.ts +58 -0
- package/dist/thread_event_store-C0zYzukG.d.ts +77 -0
- package/dist/types-BTeabLYr.d.ts +126 -0
- package/package.json +152 -88
- package/dist/chat-runtime-D7wu_KbX.d.ts +0 -747
- package/dist/chunk-JLDH4U5L.js.map +0 -1
- package/dist/chunk-SVK6PLGO.js.map +0 -1
- package/dist/index-CB9la6xE.d.ts +0 -112
- package/dist/thread_event_store-B9CoQUIA.d.ts +0 -3868
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
// src/upstream/codex-rs/core/src/event-mapping.ts
|
|
2
|
+
var CONTEXTUAL_DEVELOPER_PREFIXES = [
|
|
3
|
+
"<permissions instructions>",
|
|
4
|
+
"<developer_instructions>",
|
|
5
|
+
"<model_switch>",
|
|
6
|
+
"<collaboration_mode>",
|
|
7
|
+
"<realtime_conversation>",
|
|
8
|
+
"<personality_spec>",
|
|
9
|
+
"<hook_additional_context>"
|
|
10
|
+
];
|
|
11
|
+
function isContextualUserMessageContent(message) {
|
|
12
|
+
return message.some(isContextualUserFragment);
|
|
13
|
+
}
|
|
14
|
+
function isContextualDevMessageContent(message) {
|
|
15
|
+
return message.some(isContextualDevFragment);
|
|
16
|
+
}
|
|
17
|
+
function hasNonContextualDevMessageContent(message) {
|
|
18
|
+
return message.some((contentItem) => !isContextualDevFragment(contentItem));
|
|
19
|
+
}
|
|
20
|
+
function parseUserMessage(id, message) {
|
|
21
|
+
if (isContextualUserMessageContent(message)) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const content = [];
|
|
25
|
+
for (const [index, contentItem] of message.entries()) {
|
|
26
|
+
if (contentItem.type === "input_text") {
|
|
27
|
+
if ((isLocalImageOpenTagText(contentItem.text) || isImageOpenTagText(contentItem.text)) && message[index + 1]?.type === "input_image" || index > 0 && (isLocalImageCloseTagText(contentItem.text) || isImageCloseTagText(contentItem.text)) && message[index - 1]?.type === "input_image") {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
content.push({
|
|
31
|
+
type: "text",
|
|
32
|
+
text: contentItem.text,
|
|
33
|
+
text_elements: []
|
|
34
|
+
});
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (contentItem.type === "input_image") {
|
|
38
|
+
content.push({
|
|
39
|
+
type: "image",
|
|
40
|
+
image_url: contentItem.image_url
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
type: "UserMessage",
|
|
46
|
+
id: id ?? "",
|
|
47
|
+
content
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function parseAgentMessage(id, message, phase) {
|
|
51
|
+
const content = [];
|
|
52
|
+
for (const contentItem of message) {
|
|
53
|
+
if (contentItem.type === "input_text" || contentItem.type === "output_text") {
|
|
54
|
+
content.push({ type: "Text", text: contentItem.text });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
type: "AgentMessage",
|
|
59
|
+
id: id ?? "",
|
|
60
|
+
content,
|
|
61
|
+
phase: phase ?? null,
|
|
62
|
+
memory_citation: null
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function parseVisibleHookPromptMessage(id, message) {
|
|
66
|
+
const fragments = [];
|
|
67
|
+
for (const contentItem of message) {
|
|
68
|
+
if (contentItem.type !== "input_text") {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const fragment = parseHookPromptFragment(contentItem.text);
|
|
72
|
+
if (fragment) {
|
|
73
|
+
fragments.push(fragment);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (isStandardContextualUserText(contentItem.text)) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
if (fragments.length === 0) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
type: "HookPrompt",
|
|
86
|
+
id: id ?? "",
|
|
87
|
+
fragments
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function parseTurnItem(item) {
|
|
91
|
+
switch (item.type) {
|
|
92
|
+
case "message":
|
|
93
|
+
if (item.role === "user") {
|
|
94
|
+
return parseVisibleHookPromptMessage(item.id, item.content) ?? parseUserMessage(item.id, item.content);
|
|
95
|
+
}
|
|
96
|
+
if (item.role === "assistant" || !item.role) {
|
|
97
|
+
return parseAgentMessage(item.id, item.content, item.phase);
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
case "reasoning":
|
|
101
|
+
return parseReasoningItem(item);
|
|
102
|
+
case "web_search_call":
|
|
103
|
+
return parseWebSearchCall(item);
|
|
104
|
+
case "image_generation_call":
|
|
105
|
+
return parseImageGenerationCall(item);
|
|
106
|
+
default:
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function parseReasoningItem(item) {
|
|
111
|
+
return {
|
|
112
|
+
type: "Reasoning",
|
|
113
|
+
id: item.id ?? "",
|
|
114
|
+
summary_text: item.summary.flatMap((entry) => {
|
|
115
|
+
if (typeof entry.text === "string") {
|
|
116
|
+
return [entry.text];
|
|
117
|
+
}
|
|
118
|
+
return [];
|
|
119
|
+
}),
|
|
120
|
+
raw_content: (item.content ?? []).flatMap((entry) => {
|
|
121
|
+
if ((entry.type === "reasoning_text" || entry.type === "text") && typeof entry.text === "string") {
|
|
122
|
+
return [entry.text];
|
|
123
|
+
}
|
|
124
|
+
return [];
|
|
125
|
+
})
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function parseWebSearchCall(item) {
|
|
129
|
+
const action = webSearchActionFromRecord(item.action);
|
|
130
|
+
return {
|
|
131
|
+
type: "WebSearch",
|
|
132
|
+
id: item.id ?? "",
|
|
133
|
+
query: webSearchActionDetail(action),
|
|
134
|
+
action
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function parseImageGenerationCall(item) {
|
|
138
|
+
return {
|
|
139
|
+
type: "ImageGeneration",
|
|
140
|
+
id: item.id,
|
|
141
|
+
status: item.status,
|
|
142
|
+
revised_prompt: item.revised_prompt ?? void 0,
|
|
143
|
+
result: item.result,
|
|
144
|
+
saved_path: item.saved_path
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function webSearchActionFromRecord(action) {
|
|
148
|
+
if (!action) {
|
|
149
|
+
return { type: "other" };
|
|
150
|
+
}
|
|
151
|
+
if (action.type === "search") {
|
|
152
|
+
return {
|
|
153
|
+
type: "search",
|
|
154
|
+
...typeof action.query === "string" ? { query: action.query } : {},
|
|
155
|
+
...Array.isArray(action.queries) ? { queries: action.queries.filter(isString) } : {}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (action.type === "open_page") {
|
|
159
|
+
return {
|
|
160
|
+
type: "open_page",
|
|
161
|
+
...typeof action.url === "string" ? { url: action.url } : {}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (action.type === "find_in_page") {
|
|
165
|
+
return {
|
|
166
|
+
type: "find_in_page",
|
|
167
|
+
...typeof action.url === "string" ? { url: action.url } : {},
|
|
168
|
+
...typeof action.pattern === "string" ? { pattern: action.pattern } : {}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return { type: "other" };
|
|
172
|
+
}
|
|
173
|
+
function webSearchActionDetail(action) {
|
|
174
|
+
switch (action.type) {
|
|
175
|
+
case "search":
|
|
176
|
+
return action.query ?? action.queries?.join(", ") ?? "";
|
|
177
|
+
case "open_page":
|
|
178
|
+
return action.url ?? "";
|
|
179
|
+
case "find_in_page": {
|
|
180
|
+
const pattern = action.pattern ?? "";
|
|
181
|
+
const url = action.url ?? "";
|
|
182
|
+
return pattern && url ? `'${pattern}' in ${url}` : pattern || url;
|
|
183
|
+
}
|
|
184
|
+
case "other":
|
|
185
|
+
return "";
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function isContextualUserFragment(contentItem) {
|
|
189
|
+
return contentItem.type === "input_text" && (parseHookPromptFragment(contentItem.text) !== null || isStandardContextualUserText(contentItem.text));
|
|
190
|
+
}
|
|
191
|
+
function isContextualDevFragment(contentItem) {
|
|
192
|
+
if (contentItem.type !== "input_text") {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
const trimmed = contentItem.text.trimStart();
|
|
196
|
+
return CONTEXTUAL_DEVELOPER_PREFIXES.some(
|
|
197
|
+
(prefix) => trimmed.slice(0, prefix.length).toLowerCase() === prefix.toLowerCase()
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
function isStandardContextualUserText(text) {
|
|
201
|
+
const trimmed = text.trimStart();
|
|
202
|
+
return trimmed.startsWith("# AGENTS.md instructions for ") || trimmed.startsWith("<environment_context>") || trimmed.startsWith("<skill>") || trimmed.startsWith("<user_shell_command>") || trimmed.startsWith("<turn_aborted>") || trimmed.startsWith("<thread_goal_budget_limit>") || trimmed.startsWith("<subagent_notification>");
|
|
203
|
+
}
|
|
204
|
+
function parseHookPromptFragment(text) {
|
|
205
|
+
const trimmed = text.trim();
|
|
206
|
+
const match = trimmed.match(
|
|
207
|
+
/^<hook_prompt\s+hook_run_id=(["'])(.*?)\1>([\s\S]*)<\/hook_prompt>$/u
|
|
208
|
+
);
|
|
209
|
+
if (!match) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
const hookRunId = decodeXmlEntities(match[2] ?? "").trim();
|
|
213
|
+
if (!hookRunId) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
hookRunId,
|
|
218
|
+
text: decodeXmlEntities(match[3] ?? "")
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function isImageOpenTagText(text) {
|
|
222
|
+
return text === "<image>";
|
|
223
|
+
}
|
|
224
|
+
function isImageCloseTagText(text) {
|
|
225
|
+
return text === "</image>";
|
|
226
|
+
}
|
|
227
|
+
function isLocalImageOpenTagText(text) {
|
|
228
|
+
return text.startsWith("<image name=") && text.endsWith(">");
|
|
229
|
+
}
|
|
230
|
+
function isLocalImageCloseTagText(text) {
|
|
231
|
+
return isImageCloseTagText(text);
|
|
232
|
+
}
|
|
233
|
+
function decodeXmlEntities(value) {
|
|
234
|
+
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll(""", '"').replaceAll("'", "'");
|
|
235
|
+
}
|
|
236
|
+
function isString(value) {
|
|
237
|
+
return typeof value === "string";
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// src/upstream/codex-rs/core/src/thread-history-builder.ts
|
|
241
|
+
var ThreadHistoryBuilder = class _ThreadHistoryBuilder {
|
|
242
|
+
turns = [];
|
|
243
|
+
next_item_index = 1;
|
|
244
|
+
static fromTurns(turns) {
|
|
245
|
+
const builder = new _ThreadHistoryBuilder();
|
|
246
|
+
builder.turns = turns.map((turn) => ({
|
|
247
|
+
...cloneTurn(turn),
|
|
248
|
+
opened_explicitly: true
|
|
249
|
+
}));
|
|
250
|
+
builder.next_item_index = nextItemIndexFromTurns(turns);
|
|
251
|
+
return builder;
|
|
252
|
+
}
|
|
253
|
+
handle_rollout_item(item, index = this.next_item_index) {
|
|
254
|
+
if (item.type === "event_msg") {
|
|
255
|
+
this.handle_event(item.payload, `rollout-${index}`);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (item.type === "response_item") {
|
|
259
|
+
this.handle_response_item(item.payload, `response-${index}`);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (item.type === "compacted") {
|
|
263
|
+
this.append_to_current_turn({
|
|
264
|
+
type: "ContextCompaction",
|
|
265
|
+
id: `compacted-${index}`
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
handle_event(event, eventId = `event-${this.next_item_index}`) {
|
|
270
|
+
switch (event.type) {
|
|
271
|
+
case "turn_started":
|
|
272
|
+
this.open_explicit_turn(event.turn_id);
|
|
273
|
+
if (event.started_at !== void 0 && event.started_at !== null) {
|
|
274
|
+
const turn = this.turn_by_id(event.turn_id);
|
|
275
|
+
if (turn) {
|
|
276
|
+
turn.started_at = event.started_at;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return;
|
|
280
|
+
case "turn_complete":
|
|
281
|
+
this.complete_turn(event.turn_id, {
|
|
282
|
+
status: "completed",
|
|
283
|
+
completed_at: event.completed_at ?? null,
|
|
284
|
+
duration_ms: event.duration_ms ?? null
|
|
285
|
+
});
|
|
286
|
+
return;
|
|
287
|
+
case "turn_aborted":
|
|
288
|
+
this.complete_turn(event.turn_id, {
|
|
289
|
+
status: "interrupted",
|
|
290
|
+
completed_at: event.completed_at ?? event.aborted_at ?? null,
|
|
291
|
+
duration_ms: event.duration_ms ?? null
|
|
292
|
+
});
|
|
293
|
+
return;
|
|
294
|
+
case "user_message":
|
|
295
|
+
this.append_user_message(userMessageTurnItem(event, userMessageId(eventId)), {
|
|
296
|
+
replaceDuplicate: true
|
|
297
|
+
});
|
|
298
|
+
return;
|
|
299
|
+
case "agent_message":
|
|
300
|
+
this.upsert_agent_message({
|
|
301
|
+
type: "AgentMessage",
|
|
302
|
+
id: agentMessageId(eventId),
|
|
303
|
+
content: [{ type: "Text", text: event.message }],
|
|
304
|
+
phase: event.phase ?? null,
|
|
305
|
+
memory_citation: event.memory_citation ?? null
|
|
306
|
+
});
|
|
307
|
+
return;
|
|
308
|
+
case "item_started":
|
|
309
|
+
this.upsert_item_in_turn(event.turn_id, event.item, {
|
|
310
|
+
started: true
|
|
311
|
+
});
|
|
312
|
+
return;
|
|
313
|
+
case "item_completed":
|
|
314
|
+
this.upsert_item_in_turn(event.turn_id, completedItem(event.item));
|
|
315
|
+
return;
|
|
316
|
+
case "agent_message_content_delta":
|
|
317
|
+
this.apply_agent_message_delta(event.item_id, event.delta);
|
|
318
|
+
return;
|
|
319
|
+
case "plan_delta":
|
|
320
|
+
this.apply_plan_delta(event.item_id, event.delta);
|
|
321
|
+
return;
|
|
322
|
+
case "exec_command_begin":
|
|
323
|
+
this.upsert_item_in_turn(event.turn_id, {
|
|
324
|
+
type: "CommandExecution",
|
|
325
|
+
id: event.call_id,
|
|
326
|
+
command: event.command,
|
|
327
|
+
cwd: event.cwd,
|
|
328
|
+
status: "in_progress"
|
|
329
|
+
});
|
|
330
|
+
return;
|
|
331
|
+
case "exec_command_output_delta":
|
|
332
|
+
this.apply_exec_command_output_delta(event);
|
|
333
|
+
return;
|
|
334
|
+
case "exec_command_end":
|
|
335
|
+
this.apply_exec_command_end(event);
|
|
336
|
+
return;
|
|
337
|
+
case "patch_apply_updated":
|
|
338
|
+
this.upsert_item_in_current_or_existing_turn(event.call_id, {
|
|
339
|
+
type: "FileChange",
|
|
340
|
+
id: event.call_id,
|
|
341
|
+
changes: event.changes,
|
|
342
|
+
status: event.status ?? null,
|
|
343
|
+
auto_approved: false,
|
|
344
|
+
stdout: event.stdout ?? "",
|
|
345
|
+
stderr: event.stderr ?? ""
|
|
346
|
+
});
|
|
347
|
+
return;
|
|
348
|
+
case "dynamic_tool_call_request":
|
|
349
|
+
this.upsert_item_in_turn(event.turn_id, dynamicToolRequestTurnItem(event));
|
|
350
|
+
return;
|
|
351
|
+
case "dynamic_tool_call_response":
|
|
352
|
+
this.upsert_item_in_turn(event.turn_id, {
|
|
353
|
+
type: "DynamicToolCall",
|
|
354
|
+
id: event.call_id,
|
|
355
|
+
namespace: event.namespace ?? null,
|
|
356
|
+
tool: event.tool,
|
|
357
|
+
arguments: event.arguments,
|
|
358
|
+
status: event.success ? "completed" : "failed",
|
|
359
|
+
content_items: event.content_items,
|
|
360
|
+
success: event.success,
|
|
361
|
+
duration: event.duration
|
|
362
|
+
});
|
|
363
|
+
return;
|
|
364
|
+
case "mcp_tool_call_progress":
|
|
365
|
+
this.upsert_item_in_turn(event.turn_id, {
|
|
366
|
+
type: "McpToolCall",
|
|
367
|
+
id: event.call_id,
|
|
368
|
+
server: event.server_name,
|
|
369
|
+
tool: event.tool_name,
|
|
370
|
+
arguments: {},
|
|
371
|
+
status: "inProgress",
|
|
372
|
+
result: event.progress ?? event.message ?? null
|
|
373
|
+
});
|
|
374
|
+
return;
|
|
375
|
+
case "image_generation_end":
|
|
376
|
+
this.upsert_item_in_current_or_existing_turn(
|
|
377
|
+
event.call_id,
|
|
378
|
+
imageGenerationTurnItem(event, eventId)
|
|
379
|
+
);
|
|
380
|
+
return;
|
|
381
|
+
case "error": {
|
|
382
|
+
const turn = this.active_turn();
|
|
383
|
+
if (turn) {
|
|
384
|
+
turn.status = "failed";
|
|
385
|
+
turn.error = {
|
|
386
|
+
message: event.message,
|
|
387
|
+
codex_error_info: event.codex_error_info ?? null
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
default:
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
handle_response_item(item, id) {
|
|
397
|
+
const turnItem = parseTurnItem(item);
|
|
398
|
+
if (!turnItem) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const itemWithFallbackId = turnItem.id.length > 0 ? turnItem : { ...turnItem, id };
|
|
402
|
+
if (itemWithFallbackId.type === "AgentMessage") {
|
|
403
|
+
this.upsert_agent_message(itemWithFallbackId);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
if (itemWithFallbackId.type === "UserMessage") {
|
|
407
|
+
this.append_user_message(itemWithFallbackId, {
|
|
408
|
+
replaceDuplicate: false
|
|
409
|
+
});
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
this.append_to_current_turn(itemWithFallbackId);
|
|
413
|
+
}
|
|
414
|
+
active_turn_snapshot() {
|
|
415
|
+
const turn = this.active_turn();
|
|
416
|
+
return turn ? stripInternalTurn(turn) : null;
|
|
417
|
+
}
|
|
418
|
+
active_turn_position() {
|
|
419
|
+
const turn = this.active_turn();
|
|
420
|
+
return turn ? this.turns.indexOf(turn) : null;
|
|
421
|
+
}
|
|
422
|
+
finish() {
|
|
423
|
+
return this.turns.map(stripInternalTurn);
|
|
424
|
+
}
|
|
425
|
+
has_active_turn() {
|
|
426
|
+
return this.active_turn() !== null;
|
|
427
|
+
}
|
|
428
|
+
reset() {
|
|
429
|
+
this.turns = [];
|
|
430
|
+
this.next_item_index = 1;
|
|
431
|
+
}
|
|
432
|
+
open_explicit_turn(turnId) {
|
|
433
|
+
const existing = this.turns.find((turn) => turn.id === turnId);
|
|
434
|
+
if (existing) {
|
|
435
|
+
existing.status = "in_progress";
|
|
436
|
+
existing.opened_explicitly = true;
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
this.turns.push(newTurn(turnId, true));
|
|
440
|
+
}
|
|
441
|
+
complete_turn(turnId, input) {
|
|
442
|
+
const turn = this.turn_by_id(turnId) ?? this.ensure_turn(turnId, true);
|
|
443
|
+
turn.status = input.status;
|
|
444
|
+
turn.completed_at = input.completed_at;
|
|
445
|
+
turn.duration_ms = input.duration_ms;
|
|
446
|
+
}
|
|
447
|
+
append_user_message(item, options) {
|
|
448
|
+
const active = this.active_turn();
|
|
449
|
+
if (!active || !active.opened_explicitly && active.items.length > 0) {
|
|
450
|
+
this.turns.push(newTurn(`turn-${this.turns.length + 1}`, false));
|
|
451
|
+
}
|
|
452
|
+
const turn = this.active_turn() ?? this.ensure_turn();
|
|
453
|
+
const lastItem = turn.items.at(-1);
|
|
454
|
+
if (lastItem?.type === "UserMessage" && userMessageFingerprint(lastItem) === userMessageFingerprint(item)) {
|
|
455
|
+
if (options.replaceDuplicate) {
|
|
456
|
+
turn.items[turn.items.length - 1] = item;
|
|
457
|
+
}
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
turn.items.push(item);
|
|
461
|
+
}
|
|
462
|
+
upsert_agent_message(item) {
|
|
463
|
+
const turn = this.ensure_turn();
|
|
464
|
+
const lastItem = turn.items.at(-1);
|
|
465
|
+
if (item.phase === "streaming") {
|
|
466
|
+
if (lastItem?.type === "AgentMessage" && lastItem.phase === "streaming") {
|
|
467
|
+
turn.items[turn.items.length - 1] = { ...item, id: lastItem.id };
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
turn.items.push({ ...item, id: `streaming-${item.id}` });
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
if (lastItem?.type === "AgentMessage" && lastItem.phase === "streaming") {
|
|
474
|
+
turn.items[turn.items.length - 1] = { ...item, id: lastItem.id };
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
if (lastItem?.type === "AgentMessage" && agentMessageText(lastItem) === agentMessageText(item)) {
|
|
478
|
+
turn.items[turn.items.length - 1] = { ...item, id: lastItem.id };
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
turn.items.push(item);
|
|
482
|
+
}
|
|
483
|
+
upsert_item_in_turn(turnId, item, options = {}) {
|
|
484
|
+
const turn = turnId ? this.ensure_turn(turnId, true) : this.ensure_turn();
|
|
485
|
+
upsertItem(turn, options.started ? startedItem(item) : item);
|
|
486
|
+
}
|
|
487
|
+
upsert_item_in_current_or_existing_turn(itemId, item) {
|
|
488
|
+
const existingTurn = this.turns.find(
|
|
489
|
+
(turn) => turn.items.some((candidate) => candidate.id === itemId)
|
|
490
|
+
);
|
|
491
|
+
upsertItem(existingTurn ?? this.ensure_turn(), item);
|
|
492
|
+
}
|
|
493
|
+
append_to_current_turn(item) {
|
|
494
|
+
this.ensure_turn().items.push(item);
|
|
495
|
+
}
|
|
496
|
+
apply_agent_message_delta(itemId, delta) {
|
|
497
|
+
const existing = this.find_item(itemId);
|
|
498
|
+
if (!existing || existing.item.type !== "AgentMessage") {
|
|
499
|
+
this.append_to_current_turn({
|
|
500
|
+
type: "AgentMessage",
|
|
501
|
+
id: itemId,
|
|
502
|
+
content: [{ type: "Text", text: delta }],
|
|
503
|
+
phase: "streaming",
|
|
504
|
+
memory_citation: null
|
|
505
|
+
});
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const item = existing.item;
|
|
509
|
+
const lastContent = item.content.at(-1);
|
|
510
|
+
const nextContent = lastContent?.type === "Text" ? [
|
|
511
|
+
...item.content.slice(0, -1),
|
|
512
|
+
{ type: "Text", text: `${lastContent.text}${delta}` }
|
|
513
|
+
] : [...item.content, { type: "Text", text: delta }];
|
|
514
|
+
existing.turn.items[existing.index] = {
|
|
515
|
+
...item,
|
|
516
|
+
content: nextContent,
|
|
517
|
+
phase: "streaming"
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
apply_plan_delta(itemId, delta) {
|
|
521
|
+
const existing = this.find_item(itemId);
|
|
522
|
+
if (!existing || existing.item.type !== "Plan") {
|
|
523
|
+
this.append_to_current_turn({ type: "Plan", id: itemId, text: delta });
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
existing.turn.items[existing.index] = {
|
|
527
|
+
...existing.item,
|
|
528
|
+
text: `${existing.item.text}${delta}`
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
apply_exec_command_output_delta(msg) {
|
|
532
|
+
const existing = this.find_item(msg.call_id);
|
|
533
|
+
if (!existing || existing.item.type !== "CommandExecution") {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
existing.turn.items[existing.index] = {
|
|
537
|
+
...existing.item,
|
|
538
|
+
stdout: msg.stream === "stdout" ? `${existing.item.stdout ?? ""}${msg.chunk}` : existing.item.stdout,
|
|
539
|
+
stderr: msg.stream === "stderr" ? `${existing.item.stderr ?? ""}${msg.chunk}` : existing.item.stderr
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
apply_exec_command_end(msg) {
|
|
543
|
+
const existing = this.find_item(msg.call_id);
|
|
544
|
+
const item = existing?.item.type === "CommandExecution" ? existing.item : null;
|
|
545
|
+
const turn = msg.turn_id ? this.ensure_turn(msg.turn_id, true) : existing?.turn ?? this.ensure_turn();
|
|
546
|
+
upsertItem(turn, {
|
|
547
|
+
type: "CommandExecution",
|
|
548
|
+
id: msg.call_id,
|
|
549
|
+
command: item?.command ?? [],
|
|
550
|
+
cwd: item?.cwd ?? "",
|
|
551
|
+
status: msg.status === "completed" ? "completed" : msg.status === "cancelled" ? "cancelled" : "failed",
|
|
552
|
+
stdout: msg.stdout ?? msg.output?.stdout.text ?? item?.stdout ?? "",
|
|
553
|
+
stderr: msg.stderr ?? msg.output?.stderr.text ?? item?.stderr ?? "",
|
|
554
|
+
exit_code: msg.exit_code,
|
|
555
|
+
duration_ms: msg.duration_ms ?? msg.output?.duration_ms ?? null
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
ensure_turn(id, explicit = false) {
|
|
559
|
+
if (id) {
|
|
560
|
+
const existing = this.turn_by_id(id);
|
|
561
|
+
if (existing) {
|
|
562
|
+
return existing;
|
|
563
|
+
}
|
|
564
|
+
const turn2 = newTurn(id, explicit);
|
|
565
|
+
this.turns.push(turn2);
|
|
566
|
+
return turn2;
|
|
567
|
+
}
|
|
568
|
+
const active = this.active_turn();
|
|
569
|
+
if (active) {
|
|
570
|
+
return active;
|
|
571
|
+
}
|
|
572
|
+
const turn = newTurn(`turn-${this.turns.length + 1}`, false);
|
|
573
|
+
this.turns.push(turn);
|
|
574
|
+
return turn;
|
|
575
|
+
}
|
|
576
|
+
active_turn() {
|
|
577
|
+
for (let index = this.turns.length - 1; index >= 0; index -= 1) {
|
|
578
|
+
const turn = this.turns[index];
|
|
579
|
+
if (turn?.status === "in_progress") {
|
|
580
|
+
return turn;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
return null;
|
|
584
|
+
}
|
|
585
|
+
turn_by_id(id) {
|
|
586
|
+
return this.turns.find((turn) => turn.id === id) ?? null;
|
|
587
|
+
}
|
|
588
|
+
find_item(itemId) {
|
|
589
|
+
for (const turn of this.turns) {
|
|
590
|
+
const index = turn.items.findIndex((item) => item.id === itemId);
|
|
591
|
+
if (index !== -1) {
|
|
592
|
+
return { turn, item: turn.items[index], index };
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
function buildTurnsFromRolloutItems(items) {
|
|
599
|
+
const builder = new ThreadHistoryBuilder();
|
|
600
|
+
for (const [index, item] of items.entries()) {
|
|
601
|
+
builder.handle_rollout_item(item, index);
|
|
602
|
+
}
|
|
603
|
+
return builder.finish();
|
|
604
|
+
}
|
|
605
|
+
function applyEventToThreadHistoryBuilder(builder, event, eventId) {
|
|
606
|
+
builder.handle_event(event, eventId);
|
|
607
|
+
return builder;
|
|
608
|
+
}
|
|
609
|
+
function flattenTurnsToTurnItems(turns) {
|
|
610
|
+
return turns.flatMap((turn) => turn.items);
|
|
611
|
+
}
|
|
612
|
+
function applyEventToTurns(turns, event, eventId) {
|
|
613
|
+
const builder = ThreadHistoryBuilder.fromTurns(turns);
|
|
614
|
+
builder.handle_event(event, eventId);
|
|
615
|
+
return builder.finish();
|
|
616
|
+
}
|
|
617
|
+
function applyResponseItemToTurns(turns, item, id) {
|
|
618
|
+
const builder = ThreadHistoryBuilder.fromTurns(turns);
|
|
619
|
+
builder.handle_response_item(item, id);
|
|
620
|
+
return builder.finish();
|
|
621
|
+
}
|
|
622
|
+
function appendCompactionTurnToTurns(turns, id) {
|
|
623
|
+
const builder = ThreadHistoryBuilder.fromTurns(turns);
|
|
624
|
+
builder.handle_rollout_item({ type: "compacted", payload: { message: "" } }, 0);
|
|
625
|
+
const next = builder.finish();
|
|
626
|
+
const last = next.at(-1);
|
|
627
|
+
if (last?.items.at(-1)?.type === "ContextCompaction") {
|
|
628
|
+
last.items[last.items.length - 1] = { type: "ContextCompaction", id };
|
|
629
|
+
}
|
|
630
|
+
return next;
|
|
631
|
+
}
|
|
632
|
+
function newTurn(id, openedExplicitly) {
|
|
633
|
+
return {
|
|
634
|
+
id,
|
|
635
|
+
items: [],
|
|
636
|
+
items_view: "full",
|
|
637
|
+
status: "in_progress",
|
|
638
|
+
error: null,
|
|
639
|
+
started_at: null,
|
|
640
|
+
completed_at: null,
|
|
641
|
+
duration_ms: null,
|
|
642
|
+
opened_explicitly: openedExplicitly
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function stripInternalTurn(turn) {
|
|
646
|
+
const { opened_explicitly, ...publicTurn } = turn;
|
|
647
|
+
return cloneTurn(publicTurn);
|
|
648
|
+
}
|
|
649
|
+
function cloneTurn(turn) {
|
|
650
|
+
return {
|
|
651
|
+
...turn,
|
|
652
|
+
items: turn.items.map((item) => structuredClone(item)),
|
|
653
|
+
error: turn.error ? { ...turn.error } : null
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
function nextItemIndexFromTurns(turns) {
|
|
657
|
+
return turns.reduce((count, turn) => count + turn.items.length, 0) + 1;
|
|
658
|
+
}
|
|
659
|
+
function upsertItem(turn, item) {
|
|
660
|
+
const index = turn.items.findIndex((candidate) => candidate.id === item.id);
|
|
661
|
+
if (index === -1) {
|
|
662
|
+
turn.items.push(item);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
turn.items[index] = item;
|
|
666
|
+
}
|
|
667
|
+
function startedItem(item) {
|
|
668
|
+
return item.type === "AgentMessage" ? { ...item, phase: item.phase ?? "streaming" } : item;
|
|
669
|
+
}
|
|
670
|
+
function completedItem(item) {
|
|
671
|
+
return item.type === "AgentMessage" ? { ...item, phase: item.phase ?? null } : item;
|
|
672
|
+
}
|
|
673
|
+
function dynamicToolRequestTurnItem(request) {
|
|
674
|
+
return {
|
|
675
|
+
type: "DynamicToolCall",
|
|
676
|
+
id: request.call_id,
|
|
677
|
+
namespace: request.namespace ?? null,
|
|
678
|
+
tool: request.tool,
|
|
679
|
+
arguments: request.arguments,
|
|
680
|
+
status: "inProgress",
|
|
681
|
+
content_items: null,
|
|
682
|
+
success: null,
|
|
683
|
+
duration: null
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
function userMessageId(eventId) {
|
|
687
|
+
return `user-${eventId}`;
|
|
688
|
+
}
|
|
689
|
+
function agentMessageId(eventId) {
|
|
690
|
+
return `agent-${eventId}`;
|
|
691
|
+
}
|
|
692
|
+
function userMessageTurnItem(msg, id) {
|
|
693
|
+
return {
|
|
694
|
+
type: "UserMessage",
|
|
695
|
+
id,
|
|
696
|
+
content: userInputFromUserMessage(msg)
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
function userInputFromUserMessage(msg) {
|
|
700
|
+
const items = [];
|
|
701
|
+
if (msg.message.length > 0) {
|
|
702
|
+
items.push({
|
|
703
|
+
type: "text",
|
|
704
|
+
text: msg.message,
|
|
705
|
+
text_elements: msg.text_elements
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
for (const imageUrl of msg.images ?? []) {
|
|
709
|
+
items.push({ type: "image", image_url: imageUrl });
|
|
710
|
+
}
|
|
711
|
+
for (const path of msg.local_images ?? []) {
|
|
712
|
+
items.push({ type: "local_image", path });
|
|
713
|
+
}
|
|
714
|
+
return items;
|
|
715
|
+
}
|
|
716
|
+
function imageGenerationTurnItem(msg, id) {
|
|
717
|
+
return {
|
|
718
|
+
type: "ImageGeneration",
|
|
719
|
+
id: msg.call_id || id,
|
|
720
|
+
status: msg.status,
|
|
721
|
+
revised_prompt: msg.revised_prompt,
|
|
722
|
+
result: msg.result,
|
|
723
|
+
saved_path: msg.saved_path
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
function agentMessageText(item) {
|
|
727
|
+
return item.content.map((part) => part.text).join("");
|
|
728
|
+
}
|
|
729
|
+
function userMessageFingerprint(item) {
|
|
730
|
+
return item.content.map((content) => {
|
|
731
|
+
if (content.type === "text") {
|
|
732
|
+
return `text:${content.text}`;
|
|
733
|
+
}
|
|
734
|
+
if (content.type === "image") {
|
|
735
|
+
return `image:${content.image_url}`;
|
|
736
|
+
}
|
|
737
|
+
if (content.type === "local_image") {
|
|
738
|
+
return `local_image:${content.path}`;
|
|
739
|
+
}
|
|
740
|
+
if (content.type === "skill") {
|
|
741
|
+
return `skill:${content.name}:${content.path}`;
|
|
742
|
+
}
|
|
743
|
+
return `mention:${content.name}:${content.path}`;
|
|
744
|
+
}).join("\n");
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
// src/upstream/codex-rs/core/src/rendered-thread.ts
|
|
748
|
+
function createRenderedThreadState(threadId) {
|
|
749
|
+
return {
|
|
750
|
+
thread_id: threadId,
|
|
751
|
+
turns: [],
|
|
752
|
+
items: [],
|
|
753
|
+
pending_request_user_input: null,
|
|
754
|
+
pending_request_permissions: null,
|
|
755
|
+
pending_dynamic_tool_call_requests: [],
|
|
756
|
+
running_turn_ids: [],
|
|
757
|
+
warnings: [],
|
|
758
|
+
errors: [],
|
|
759
|
+
last_history_fingerprint: null,
|
|
760
|
+
connection_status: "idle"
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
function renderThreadFromHistory(history) {
|
|
764
|
+
let state = createRenderedThreadState(history.thread_id);
|
|
765
|
+
for (const [index, item] of history.items.entries()) {
|
|
766
|
+
state = applyRolloutItemToRenderedThread(state, item, index);
|
|
767
|
+
}
|
|
768
|
+
return {
|
|
769
|
+
...state,
|
|
770
|
+
running_turn_ids: [],
|
|
771
|
+
last_history_fingerprint: fingerprintRolloutItems(history.items)
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
function applyRolloutItemToRenderedThread(state, item, index = state.items.length) {
|
|
775
|
+
if (item.type === "event_msg") {
|
|
776
|
+
return applyEventMsgToRenderedThread(state, item.payload, {
|
|
777
|
+
event_id: `rollout-${index}`
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
if (item.type === "response_item") {
|
|
781
|
+
return withTurns(
|
|
782
|
+
state,
|
|
783
|
+
applyResponseItemToTurns(state.turns, item.payload, `response-${index}`)
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
if (item.type === "compacted") {
|
|
787
|
+
return withTurns(
|
|
788
|
+
state,
|
|
789
|
+
appendCompactionTurnToTurns(state.turns, `compacted-${index}`)
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
return state;
|
|
793
|
+
}
|
|
794
|
+
function applyEventMsgToRenderedThread(state, msg, options = {}) {
|
|
795
|
+
const eventId = options.event_id ?? `event-${state.items.length}`;
|
|
796
|
+
const hadActiveTurn = hasActiveTurn(state.turns);
|
|
797
|
+
const next = withTurns(state, applyEventToTurns(state.turns, msg, eventId));
|
|
798
|
+
switch (msg.type) {
|
|
799
|
+
case "turn_started":
|
|
800
|
+
return {
|
|
801
|
+
...next,
|
|
802
|
+
running_turn_ids: addUnique(next.running_turn_ids, msg.turn_id)
|
|
803
|
+
};
|
|
804
|
+
case "turn_complete":
|
|
805
|
+
return clearPendingForTurn(
|
|
806
|
+
{
|
|
807
|
+
...next,
|
|
808
|
+
running_turn_ids: next.running_turn_ids.filter(
|
|
809
|
+
(turnId) => turnId !== msg.turn_id
|
|
810
|
+
)
|
|
811
|
+
},
|
|
812
|
+
msg.turn_id
|
|
813
|
+
);
|
|
814
|
+
case "turn_aborted":
|
|
815
|
+
return clearPendingForTurn(
|
|
816
|
+
{
|
|
817
|
+
...next,
|
|
818
|
+
running_turn_ids: next.running_turn_ids.filter(
|
|
819
|
+
(turnId) => turnId !== msg.turn_id
|
|
820
|
+
)
|
|
821
|
+
},
|
|
822
|
+
msg.turn_id
|
|
823
|
+
);
|
|
824
|
+
case "request_user_input":
|
|
825
|
+
return {
|
|
826
|
+
...next,
|
|
827
|
+
pending_request_user_input: {
|
|
828
|
+
call_id: msg.call_id,
|
|
829
|
+
turn_id: msg.turn_id,
|
|
830
|
+
questions: msg.questions.map((question) => ({
|
|
831
|
+
...question,
|
|
832
|
+
options: question.options.map((option) => ({ ...option }))
|
|
833
|
+
}))
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
case "request_permissions":
|
|
837
|
+
return {
|
|
838
|
+
...next,
|
|
839
|
+
pending_request_permissions: {
|
|
840
|
+
call_id: msg.call_id,
|
|
841
|
+
turn_id: msg.turn_id,
|
|
842
|
+
reason: msg.reason ?? null,
|
|
843
|
+
permissions: JSON.parse(JSON.stringify(msg.permissions)),
|
|
844
|
+
cwd: msg.cwd ?? null
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
case "dynamic_tool_call_request":
|
|
848
|
+
return {
|
|
849
|
+
...next,
|
|
850
|
+
pending_dynamic_tool_call_requests: upsertDynamicToolCallRequest(
|
|
851
|
+
next.pending_dynamic_tool_call_requests,
|
|
852
|
+
msg
|
|
853
|
+
)
|
|
854
|
+
};
|
|
855
|
+
case "dynamic_tool_call_response":
|
|
856
|
+
return {
|
|
857
|
+
...next,
|
|
858
|
+
pending_dynamic_tool_call_requests: next.pending_dynamic_tool_call_requests.filter(
|
|
859
|
+
(request) => request.call_id !== msg.call_id
|
|
860
|
+
)
|
|
861
|
+
};
|
|
862
|
+
case "warning":
|
|
863
|
+
return {
|
|
864
|
+
...next,
|
|
865
|
+
warnings: [...next.warnings, msg.message]
|
|
866
|
+
};
|
|
867
|
+
case "error":
|
|
868
|
+
return hadActiveTurn ? next : {
|
|
869
|
+
...next,
|
|
870
|
+
errors: [...next.errors, msg.message]
|
|
871
|
+
};
|
|
872
|
+
default:
|
|
873
|
+
return next;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
function setRenderedThreadConnectionStatus(state, connectionStatus) {
|
|
877
|
+
return {
|
|
878
|
+
...state,
|
|
879
|
+
connection_status: connectionStatus
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
function fingerprintRolloutItems(items) {
|
|
883
|
+
const last = items.at(-1);
|
|
884
|
+
return `${items.length}:${hashString(JSON.stringify(last ?? null))}`;
|
|
885
|
+
}
|
|
886
|
+
function withTurns(state, turns) {
|
|
887
|
+
const nextTurns = turns.map((turn) => ({
|
|
888
|
+
...turn,
|
|
889
|
+
items: turn.items.map((item) => structuredClone(item)),
|
|
890
|
+
error: turn.error ? { ...turn.error } : null
|
|
891
|
+
}));
|
|
892
|
+
return {
|
|
893
|
+
...state,
|
|
894
|
+
turns: nextTurns,
|
|
895
|
+
items: flattenTurnsToTurnItems(nextTurns)
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
function clearPendingForTurn(state, turnId) {
|
|
899
|
+
return {
|
|
900
|
+
...state,
|
|
901
|
+
pending_request_user_input: state.pending_request_user_input?.turn_id === turnId ? null : state.pending_request_user_input,
|
|
902
|
+
pending_dynamic_tool_call_requests: state.pending_dynamic_tool_call_requests.filter(
|
|
903
|
+
(request) => request.turn_id !== turnId
|
|
904
|
+
),
|
|
905
|
+
pending_request_permissions: state.pending_request_permissions?.turn_id === turnId ? null : state.pending_request_permissions
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
function upsertDynamicToolCallRequest(requests, request) {
|
|
909
|
+
return [
|
|
910
|
+
...requests.filter((item) => item.call_id !== request.call_id),
|
|
911
|
+
{
|
|
912
|
+
call_id: request.call_id,
|
|
913
|
+
turn_id: request.turn_id,
|
|
914
|
+
started_at_ms: request.started_at_ms,
|
|
915
|
+
namespace: request.namespace ?? null,
|
|
916
|
+
tool: request.tool,
|
|
917
|
+
arguments: request.arguments
|
|
918
|
+
}
|
|
919
|
+
];
|
|
920
|
+
}
|
|
921
|
+
function hasActiveTurn(turns) {
|
|
922
|
+
return turns.some((turn) => turn.status === "in_progress");
|
|
923
|
+
}
|
|
924
|
+
function addUnique(values, value) {
|
|
925
|
+
return values.includes(value) ? values : [...values, value];
|
|
926
|
+
}
|
|
927
|
+
function hashString(value) {
|
|
928
|
+
let hash = 0;
|
|
929
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
930
|
+
hash = hash * 31 + value.charCodeAt(index) >>> 0;
|
|
931
|
+
}
|
|
932
|
+
return hash.toString(16);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
export { ThreadHistoryBuilder, appendCompactionTurnToTurns, applyEventMsgToRenderedThread, applyEventToThreadHistoryBuilder, applyEventToTurns, applyResponseItemToTurns, applyRolloutItemToRenderedThread, buildTurnsFromRolloutItems, createRenderedThreadState, fingerprintRolloutItems, flattenTurnsToTurnItems, hasNonContextualDevMessageContent, isContextualDevMessageContent, isContextualUserMessageContent, parseAgentMessage, parseTurnItem, parseUserMessage, parseVisibleHookPromptMessage, renderThreadFromHistory, setRenderedThreadConnectionStatus };
|
|
936
|
+
//# sourceMappingURL=chunk-DYLHN3HG.js.map
|
|
937
|
+
//# sourceMappingURL=chunk-DYLHN3HG.js.map
|