@kodelyth/codex 2026.5.42 → 2026.6.2

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.
Files changed (138) hide show
  1. package/package.json +17 -2
  2. package/doctor-contract-api.test.ts +0 -44
  3. package/doctor-contract-api.ts +0 -68
  4. package/harness.ts +0 -72
  5. package/index.test.ts +0 -230
  6. package/index.ts +0 -66
  7. package/media-understanding-provider.test.ts +0 -486
  8. package/media-understanding-provider.ts +0 -521
  9. package/prompt-overlay-runtime-contract.test.ts +0 -48
  10. package/prompt-overlay.ts +0 -21
  11. package/provider-catalog.ts +0 -83
  12. package/provider-discovery.ts +0 -45
  13. package/provider.test.ts +0 -384
  14. package/provider.ts +0 -243
  15. package/src/app-server/app-inventory-cache.test.ts +0 -176
  16. package/src/app-server/app-inventory-cache.ts +0 -324
  17. package/src/app-server/approval-bridge.test.ts +0 -1471
  18. package/src/app-server/approval-bridge.ts +0 -1211
  19. package/src/app-server/auth-bridge.test.ts +0 -1449
  20. package/src/app-server/auth-bridge.ts +0 -614
  21. package/src/app-server/auth-profile-runtime-contract.test.ts +0 -239
  22. package/src/app-server/capabilities.ts +0 -27
  23. package/src/app-server/client-factory.ts +0 -24
  24. package/src/app-server/client.test.ts +0 -563
  25. package/src/app-server/client.ts +0 -715
  26. package/src/app-server/compact.test.ts +0 -710
  27. package/src/app-server/compact.ts +0 -500
  28. package/src/app-server/computer-use.test.ts +0 -788
  29. package/src/app-server/computer-use.ts +0 -683
  30. package/src/app-server/config.test.ts +0 -879
  31. package/src/app-server/config.ts +0 -1038
  32. package/src/app-server/context-engine-projection.test.ts +0 -252
  33. package/src/app-server/context-engine-projection.ts +0 -403
  34. package/src/app-server/delivery-no-reply-runtime-contract.test.ts +0 -80
  35. package/src/app-server/dynamic-tool-diagnostics.ts +0 -73
  36. package/src/app-server/dynamic-tool-profile.ts +0 -69
  37. package/src/app-server/dynamic-tools.test.ts +0 -1302
  38. package/src/app-server/dynamic-tools.ts +0 -623
  39. package/src/app-server/elicitation-bridge.test.ts +0 -1056
  40. package/src/app-server/elicitation-bridge.ts +0 -783
  41. package/src/app-server/event-projector.test.ts +0 -2668
  42. package/src/app-server/event-projector.ts +0 -2057
  43. package/src/app-server/image-payload-sanitizer.test.ts +0 -49
  44. package/src/app-server/image-payload-sanitizer.ts +0 -167
  45. package/src/app-server/klaw-owned-tool-runtime-contract.test.ts +0 -456
  46. package/src/app-server/local-runtime-attribution.ts +0 -39
  47. package/src/app-server/managed-binary.test.ts +0 -139
  48. package/src/app-server/managed-binary.ts +0 -193
  49. package/src/app-server/models.test.ts +0 -246
  50. package/src/app-server/models.ts +0 -172
  51. package/src/app-server/native-hook-relay.test.ts +0 -271
  52. package/src/app-server/native-hook-relay.ts +0 -150
  53. package/src/app-server/native-subagent-task-mirror.test.ts +0 -573
  54. package/src/app-server/native-subagent-task-mirror.ts +0 -497
  55. package/src/app-server/outcome-fallback-runtime-contract.test.ts +0 -404
  56. package/src/app-server/plugin-activation.test.ts +0 -336
  57. package/src/app-server/plugin-activation.ts +0 -283
  58. package/src/app-server/plugin-app-cache-key.ts +0 -74
  59. package/src/app-server/plugin-approval-roundtrip.ts +0 -122
  60. package/src/app-server/plugin-inventory.test.ts +0 -355
  61. package/src/app-server/plugin-inventory.ts +0 -357
  62. package/src/app-server/plugin-thread-config.test.ts +0 -865
  63. package/src/app-server/plugin-thread-config.ts +0 -455
  64. package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +0 -33
  65. package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +0 -199
  66. package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +0 -102
  67. package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +0 -227
  68. package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +0 -2630
  69. package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +0 -2630
  70. package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +0 -1659
  71. package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +0 -1655
  72. package/src/app-server/protocol-validators.test.ts +0 -75
  73. package/src/app-server/protocol-validators.ts +0 -203
  74. package/src/app-server/protocol.ts +0 -520
  75. package/src/app-server/rate-limit-cache.ts +0 -48
  76. package/src/app-server/rate-limits.test.ts +0 -202
  77. package/src/app-server/rate-limits.ts +0 -583
  78. package/src/app-server/request.ts +0 -73
  79. package/src/app-server/run-attempt.context-engine.test.ts +0 -1004
  80. package/src/app-server/run-attempt.test.ts +0 -9477
  81. package/src/app-server/run-attempt.ts +0 -4683
  82. package/src/app-server/run-attempt.vision-tools.test.ts +0 -35
  83. package/src/app-server/schema-normalization-runtime-contract.test.ts +0 -206
  84. package/src/app-server/session-binding.test.ts +0 -303
  85. package/src/app-server/session-binding.ts +0 -398
  86. package/src/app-server/session-history.ts +0 -44
  87. package/src/app-server/shared-client.test.ts +0 -589
  88. package/src/app-server/shared-client.ts +0 -289
  89. package/src/app-server/side-question.test.ts +0 -1175
  90. package/src/app-server/side-question.ts +0 -1007
  91. package/src/app-server/test-support.ts +0 -48
  92. package/src/app-server/thread-lifecycle.test.ts +0 -447
  93. package/src/app-server/thread-lifecycle.ts +0 -939
  94. package/src/app-server/thread-lifecycle.user-mcp-servers.test.ts +0 -442
  95. package/src/app-server/timeout.ts +0 -9
  96. package/src/app-server/tool-progress-normalization.ts +0 -77
  97. package/src/app-server/trajectory.test.ts +0 -205
  98. package/src/app-server/trajectory.ts +0 -365
  99. package/src/app-server/transcript-mirror.test.ts +0 -524
  100. package/src/app-server/transcript-mirror.ts +0 -208
  101. package/src/app-server/transcript-repair-runtime-contract.test.ts +0 -44
  102. package/src/app-server/transport-stdio.test.ts +0 -171
  103. package/src/app-server/transport-stdio.ts +0 -107
  104. package/src/app-server/transport-websocket.test.ts +0 -69
  105. package/src/app-server/transport-websocket.ts +0 -90
  106. package/src/app-server/transport.ts +0 -117
  107. package/src/app-server/user-input-bridge.test.ts +0 -249
  108. package/src/app-server/user-input-bridge.ts +0 -316
  109. package/src/app-server/version.ts +0 -4
  110. package/src/app-server/vision-tools.ts +0 -12
  111. package/src/command-account.ts +0 -544
  112. package/src/command-formatters.ts +0 -425
  113. package/src/command-handlers.ts +0 -2004
  114. package/src/command-rpc.test.ts +0 -16
  115. package/src/command-rpc.ts +0 -142
  116. package/src/commands.test.ts +0 -3312
  117. package/src/commands.ts +0 -65
  118. package/src/conversation-binding-data.ts +0 -124
  119. package/src/conversation-binding.test.ts +0 -599
  120. package/src/conversation-binding.ts +0 -561
  121. package/src/conversation-control.test.ts +0 -126
  122. package/src/conversation-control.ts +0 -303
  123. package/src/conversation-turn-collector.test.ts +0 -191
  124. package/src/conversation-turn-collector.ts +0 -186
  125. package/src/conversation-turn-input.test.ts +0 -141
  126. package/src/conversation-turn-input.ts +0 -106
  127. package/src/manifest.test.ts +0 -20
  128. package/src/migration/apply.ts +0 -501
  129. package/src/migration/helpers.ts +0 -55
  130. package/src/migration/plan.ts +0 -461
  131. package/src/migration/provider.test.ts +0 -1741
  132. package/src/migration/provider.ts +0 -41
  133. package/src/migration/source.ts +0 -643
  134. package/src/migration/targets.ts +0 -25
  135. package/src/node-cli-sessions.test.ts +0 -180
  136. package/src/node-cli-sessions.ts +0 -711
  137. package/test-api.ts +0 -82
  138. package/tsconfig.json +0 -16
@@ -1,497 +0,0 @@
1
- import {
2
- CODEX_NATIVE_SUBAGENT_RUN_ID_PREFIX,
3
- CODEX_NATIVE_SUBAGENT_RUNTIME,
4
- CODEX_NATIVE_SUBAGENT_TASK_KIND,
5
- createRunningTaskRun,
6
- finalizeTaskRunByRunId,
7
- recordTaskRunProgressByRunId,
8
- } from "klaw/plugin-sdk/codex-native-task-runtime";
9
- import type {
10
- CodexServerNotification,
11
- CodexSessionSource,
12
- CodexSubAgentThreadSpawnSource,
13
- CodexThread,
14
- CodexThreadStartedNotification,
15
- CodexThreadStatus,
16
- CodexThreadStatusChangedNotification,
17
- JsonObject,
18
- JsonValue,
19
- } from "./protocol.js";
20
- import { isJsonObject } from "./protocol.js";
21
-
22
- export type TaskLifecycleRuntime = {
23
- createRunningTaskRun: typeof createRunningTaskRun;
24
- recordTaskRunProgressByRunId: typeof recordTaskRunProgressByRunId;
25
- finalizeTaskRunByRunId: typeof finalizeTaskRunByRunId;
26
- };
27
-
28
- export type CodexNativeSubagentTaskMirrorParams = {
29
- parentThreadId: string;
30
- requesterSessionKey?: string;
31
- agentId?: string;
32
- now?: () => number;
33
- };
34
-
35
- const defaultRuntime: TaskLifecycleRuntime = {
36
- createRunningTaskRun,
37
- recordTaskRunProgressByRunId,
38
- finalizeTaskRunByRunId,
39
- };
40
-
41
- export class CodexNativeSubagentTaskMirror {
42
- private readonly mirroredThreadIds = new Set<string>();
43
- private readonly terminalRunIds = new Set<string>();
44
- private readonly now: () => number;
45
-
46
- constructor(
47
- private readonly params: CodexNativeSubagentTaskMirrorParams,
48
- private readonly runtime: TaskLifecycleRuntime = defaultRuntime,
49
- ) {
50
- this.now = params.now ?? Date.now;
51
- }
52
-
53
- handleNotification(notification: CodexServerNotification): void {
54
- const params = isJsonObject(notification.params) ? notification.params : undefined;
55
- if (!params) {
56
- return;
57
- }
58
- if (notification.method === "thread/started") {
59
- this.handleThreadStarted(params);
60
- return;
61
- }
62
- if (notification.method === "thread/status/changed") {
63
- this.handleThreadStatusChanged(params);
64
- return;
65
- }
66
- if (notification.method === "item/started" || notification.method === "item/completed") {
67
- this.handleCollabAgentItem(params);
68
- }
69
- }
70
-
71
- private handleThreadStarted(params: JsonObject): void {
72
- const notification = readThreadStartedNotification(params);
73
- if (!notification) {
74
- return;
75
- }
76
- const thread = notification.thread;
77
- const spawn = readSubagentThreadSpawnSource(thread.source, this.params.parentThreadId);
78
- if (!spawn) {
79
- return;
80
- }
81
- const threadId = thread.id.trim();
82
- if (!threadId || this.mirroredThreadIds.has(threadId)) {
83
- return;
84
- }
85
- this.mirroredThreadIds.add(threadId);
86
- const runId = codexNativeSubagentRunId(threadId);
87
- const label =
88
- trimOptional(spawn.agent_nickname) ??
89
- trimOptional(thread.agentNickname) ??
90
- trimOptional(spawn.agent_role) ??
91
- trimOptional(thread.agentRole) ??
92
- "Codex subagent";
93
- const task =
94
- trimOptional(thread.preview) ??
95
- `Codex native subagent${label === "Codex subagent" ? "" : ` ${label}`}`;
96
- const createdAt = secondsToMillis(thread.createdAt) ?? this.now();
97
- this.runtime.createRunningTaskRun({
98
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
99
- taskKind: CODEX_NATIVE_SUBAGENT_TASK_KIND,
100
- sourceId: runId,
101
- requesterSessionKey: this.params.requesterSessionKey,
102
- ...(this.params.requesterSessionKey
103
- ? {
104
- ownerKey: this.params.requesterSessionKey,
105
- scopeKind: "session" as const,
106
- }
107
- : {}),
108
- agentId: this.params.agentId,
109
- runId,
110
- label,
111
- task,
112
- notifyPolicy: "silent",
113
- deliveryStatus: "not_applicable",
114
- preferMetadata: true,
115
- startedAt: createdAt,
116
- lastEventAt: this.now(),
117
- progressSummary: "Codex native subagent started.",
118
- });
119
- this.applyStatus(threadId, thread.status);
120
- }
121
-
122
- private handleThreadStatusChanged(params: JsonObject): void {
123
- const notification = readThreadStatusChangedNotification(params);
124
- if (!notification) {
125
- return;
126
- }
127
- this.applyStatus(notification.threadId, notification.status);
128
- }
129
-
130
- private applyStatus(threadId: string, status: CodexThreadStatus | null | undefined): void {
131
- const statusType = status?.type;
132
- if (!statusType) {
133
- return;
134
- }
135
- const runId = codexNativeSubagentRunId(threadId);
136
- if (this.terminalRunIds.has(runId) && statusType !== "systemError") {
137
- return;
138
- }
139
- const eventAt = this.now();
140
- if (statusType === "active") {
141
- this.runtime.recordTaskRunProgressByRunId({
142
- runId,
143
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
144
- lastEventAt: eventAt,
145
- progressSummary: "Codex native subagent is active.",
146
- });
147
- return;
148
- }
149
- if (statusType === "idle") {
150
- this.terminalRunIds.add(runId);
151
- this.runtime.finalizeTaskRunByRunId({
152
- runId,
153
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
154
- status: "succeeded",
155
- endedAt: eventAt,
156
- lastEventAt: eventAt,
157
- progressSummary: "Codex native subagent is idle.",
158
- terminalSummary: "Codex native subagent finished.",
159
- });
160
- return;
161
- }
162
- if (statusType === "systemError") {
163
- this.terminalRunIds.add(runId);
164
- this.runtime.finalizeTaskRunByRunId({
165
- runId,
166
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
167
- status: "failed",
168
- endedAt: eventAt,
169
- lastEventAt: eventAt,
170
- error: "Codex app-server reported a system error for the native subagent thread.",
171
- progressSummary: "Codex native subagent hit a system error.",
172
- terminalSummary: "Codex native subagent failed.",
173
- });
174
- return;
175
- }
176
- if (statusType === "notLoaded") {
177
- this.runtime.recordTaskRunProgressByRunId({
178
- runId,
179
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
180
- lastEventAt: eventAt,
181
- progressSummary: "Codex native subagent is not loaded.",
182
- });
183
- }
184
- }
185
-
186
- private handleCollabAgentItem(params: JsonObject): void {
187
- const item = isJsonObject(params.item) ? params.item : undefined;
188
- if (!item || readString(item, "type") !== "collabAgentToolCall") {
189
- return;
190
- }
191
- if (readString(item, "senderThreadId") !== this.params.parentThreadId) {
192
- return;
193
- }
194
- const receiverThreadIds = readStringArray(item.receiverThreadIds);
195
- const isSpawnAgentTool = normalizeToolName(readString(item, "tool")) === "spawnagent";
196
- if (isSpawnAgentTool) {
197
- for (const receiverThreadId of receiverThreadIds) {
198
- this.createTaskFromCollabSpawnItem(receiverThreadId, item);
199
- }
200
- }
201
- const agentsStates = readAgentsStates(item.agentsStates);
202
- const toolCallStatus = normalizeCollabToolCallStatus(readString(item, "status"));
203
- const terminalToolCallThreadIds = new Set<string>();
204
- if (isSpawnAgentTool && isBlockedOrFailedCollabToolCallStatus(toolCallStatus)) {
205
- for (const threadId of receiverThreadIds) {
206
- terminalToolCallThreadIds.add(threadId);
207
- }
208
- for (const threadId of agentsStates.keys()) {
209
- terminalToolCallThreadIds.add(threadId);
210
- }
211
- }
212
- const terminalAgentStateThreadIds = new Set<string>();
213
- for (const [threadId, state] of agentsStates) {
214
- const normalizedStatus = normalizeAgentStateStatus(state.status);
215
- if (
216
- terminalToolCallThreadIds.has(threadId) &&
217
- isNonTerminalAgentStateStatus(normalizedStatus)
218
- ) {
219
- continue;
220
- }
221
- this.applyCollabAgentStatus(threadId, normalizedStatus, state.message);
222
- if (isTerminalAgentStateStatus(normalizedStatus)) {
223
- terminalAgentStateThreadIds.add(threadId);
224
- }
225
- }
226
- if (isBlockedOrFailedCollabToolCallStatus(toolCallStatus)) {
227
- for (const threadId of terminalToolCallThreadIds) {
228
- if (terminalAgentStateThreadIds.has(threadId)) {
229
- continue;
230
- }
231
- const state = agentsStates.get(threadId);
232
- this.applyCollabAgentStatus(threadId, toolCallStatus, state?.message);
233
- }
234
- }
235
- }
236
-
237
- private createTaskFromCollabSpawnItem(threadId: string, item: JsonObject): void {
238
- const normalizedThreadId = threadId.trim();
239
- if (!normalizedThreadId || this.mirroredThreadIds.has(normalizedThreadId)) {
240
- return;
241
- }
242
- this.mirroredThreadIds.add(normalizedThreadId);
243
- const prompt = trimOptional(readString(item, "prompt"));
244
- const runId = codexNativeSubagentRunId(normalizedThreadId);
245
- const createdAt = this.now();
246
- this.runtime.createRunningTaskRun({
247
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
248
- taskKind: CODEX_NATIVE_SUBAGENT_TASK_KIND,
249
- sourceId: runId,
250
- requesterSessionKey: this.params.requesterSessionKey,
251
- ...(this.params.requesterSessionKey
252
- ? {
253
- ownerKey: this.params.requesterSessionKey,
254
- scopeKind: "session" as const,
255
- }
256
- : {}),
257
- agentId: this.params.agentId,
258
- runId,
259
- label: "Codex subagent",
260
- task: prompt ?? "Codex native subagent",
261
- notifyPolicy: "silent",
262
- deliveryStatus: "not_applicable",
263
- preferMetadata: true,
264
- startedAt: createdAt,
265
- lastEventAt: createdAt,
266
- progressSummary: "Codex native subagent spawned.",
267
- });
268
- }
269
-
270
- private applyCollabAgentStatus(
271
- threadId: string,
272
- status: string | undefined,
273
- message: string | null | undefined,
274
- ): void {
275
- const normalizedStatus = normalizeAgentStateStatus(status);
276
- if (!normalizedStatus) {
277
- return;
278
- }
279
- const runId = codexNativeSubagentRunId(threadId);
280
- if (this.terminalRunIds.has(runId) && isNonTerminalAgentStateStatus(normalizedStatus)) {
281
- return;
282
- }
283
- const eventAt = this.now();
284
- if (normalizedStatus === "pendingInit" || normalizedStatus === "running") {
285
- this.runtime.recordTaskRunProgressByRunId({
286
- runId,
287
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
288
- lastEventAt: eventAt,
289
- progressSummary:
290
- trimOptional(message) ??
291
- (normalizedStatus === "pendingInit"
292
- ? "Codex native subagent is initializing."
293
- : "Codex native subagent is running."),
294
- });
295
- return;
296
- }
297
- if (normalizedStatus === "completed") {
298
- this.terminalRunIds.add(runId);
299
- this.runtime.finalizeTaskRunByRunId({
300
- runId,
301
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
302
- status: "succeeded",
303
- endedAt: eventAt,
304
- lastEventAt: eventAt,
305
- progressSummary: trimOptional(message) ?? "Codex native subagent completed.",
306
- terminalSummary: trimOptional(message) ?? "Codex native subagent finished.",
307
- });
308
- return;
309
- }
310
- if (normalizedStatus === "blocked") {
311
- this.terminalRunIds.add(runId);
312
- this.runtime.finalizeTaskRunByRunId({
313
- runId,
314
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
315
- status: "succeeded",
316
- endedAt: eventAt,
317
- lastEventAt: eventAt,
318
- progressSummary: trimOptional(message) ?? "Codex native subagent blocked.",
319
- terminalSummary: trimOptional(message) ?? "Codex native subagent blocked.",
320
- terminalOutcome: "blocked",
321
- });
322
- return;
323
- }
324
- this.terminalRunIds.add(runId);
325
- this.runtime.finalizeTaskRunByRunId({
326
- runId,
327
- runtime: CODEX_NATIVE_SUBAGENT_RUNTIME,
328
- status:
329
- normalizedStatus === "interrupted" || normalizedStatus === "shutdown"
330
- ? "cancelled"
331
- : "failed",
332
- endedAt: eventAt,
333
- lastEventAt: eventAt,
334
- error: trimOptional(message) ?? `Codex native subagent status: ${normalizedStatus}`,
335
- progressSummary: trimOptional(message) ?? `Codex native subagent ${normalizedStatus}.`,
336
- terminalSummary: trimOptional(message) ?? "Codex native subagent did not complete.",
337
- });
338
- }
339
- }
340
-
341
- export function codexNativeSubagentRunId(threadId: string): string {
342
- return `${CODEX_NATIVE_SUBAGENT_RUN_ID_PREFIX}${threadId.trim()}`;
343
- }
344
-
345
- export function readSubagentThreadSpawnSource(
346
- source: CodexSessionSource | null | undefined,
347
- parentThreadId: string,
348
- ): CodexSubAgentThreadSpawnSource | undefined {
349
- if (!source || typeof source !== "object" || !("subAgent" in source)) {
350
- return undefined;
351
- }
352
- const subAgent = source.subAgent;
353
- if (!subAgent || typeof subAgent !== "object" || !("thread_spawn" in subAgent)) {
354
- return undefined;
355
- }
356
- const spawn = subAgent.thread_spawn;
357
- if (!spawn || typeof spawn !== "object") {
358
- return undefined;
359
- }
360
- return spawn.parent_thread_id === parentThreadId ? spawn : undefined;
361
- }
362
-
363
- function readThreadStartedNotification(
364
- params: JsonObject,
365
- ): CodexThreadStartedNotification | undefined {
366
- const thread = params.thread;
367
- if (!isJsonObject(thread) || typeof thread.id !== "string") {
368
- return undefined;
369
- }
370
- return { thread: thread as CodexThread };
371
- }
372
-
373
- function readThreadStatusChangedNotification(
374
- params: JsonObject,
375
- ): CodexThreadStatusChangedNotification | undefined {
376
- if (typeof params.threadId !== "string") {
377
- return undefined;
378
- }
379
- const status = params.status;
380
- if (!isJsonObject(status) || !isCodexThreadStatusType(status.type)) {
381
- return undefined;
382
- }
383
- return {
384
- threadId: params.threadId,
385
- status: status as CodexThreadStatus,
386
- };
387
- }
388
-
389
- function isCodexThreadStatusType(value: unknown): value is CodexThreadStatus["type"] {
390
- return value === "notLoaded" || value === "idle" || value === "systemError" || value === "active";
391
- }
392
-
393
- function readAgentsStates(
394
- value: JsonValue | undefined,
395
- ): Map<string, { status?: string; message?: string | null }> {
396
- const states = new Map<string, { status?: string; message?: string | null }>();
397
- if (!isJsonObject(value)) {
398
- return states;
399
- }
400
- for (const [threadId, rawState] of Object.entries(value)) {
401
- if (!isJsonObject(rawState)) {
402
- continue;
403
- }
404
- const status = readString(rawState, "status");
405
- const message = readNullableString(rawState, "message");
406
- states.set(threadId, { status, message });
407
- }
408
- return states;
409
- }
410
-
411
- function readStringArray(value: JsonValue | undefined): string[] {
412
- if (!Array.isArray(value)) {
413
- return [];
414
- }
415
- return value.filter((entry): entry is string => typeof entry === "string" && entry.trim() !== "");
416
- }
417
-
418
- function readString(value: JsonObject, key: string): string | undefined {
419
- const entry = value[key];
420
- return typeof entry === "string" ? entry : undefined;
421
- }
422
-
423
- function readNullableString(value: JsonObject, key: string): string | null | undefined {
424
- const entry = value[key];
425
- return typeof entry === "string" || entry === null ? entry : undefined;
426
- }
427
-
428
- function normalizeToolName(value: string | undefined): string | undefined {
429
- return value?.replace(/[^a-z0-9]/giu, "").toLowerCase();
430
- }
431
-
432
- function normalizeCollabToolCallStatus(value: string | undefined): string | undefined {
433
- const key = value?.replace(/[^a-z0-9]/giu, "").toLowerCase();
434
- if (key === "completed" || key === "succeeded" || key === "success") {
435
- return "completed";
436
- }
437
- if (key === "failed" || key === "error" || key === "errored") {
438
- return "failed";
439
- }
440
- if (key === "blocked" || key === "declined") {
441
- return "blocked";
442
- }
443
- if (key === "inprogress" || key === "running") {
444
- return "running";
445
- }
446
- return value?.trim();
447
- }
448
-
449
- function isBlockedOrFailedCollabToolCallStatus(value: string | undefined): boolean {
450
- return value === "failed" || value === "blocked";
451
- }
452
-
453
- function isNonTerminalAgentStateStatus(value: string | undefined): boolean {
454
- return value === "pendingInit" || value === "running";
455
- }
456
-
457
- function isTerminalAgentStateStatus(value: string | undefined): boolean {
458
- return value !== undefined && !isNonTerminalAgentStateStatus(value);
459
- }
460
-
461
- function normalizeAgentStateStatus(value: string | undefined): string | undefined {
462
- const key = value?.replace(/[^a-z0-9]/giu, "").toLowerCase();
463
- if (!key) {
464
- return undefined;
465
- }
466
- if (key === "pendinginit") {
467
- return "pendingInit";
468
- }
469
- if (key === "inprogress" || key === "running") {
470
- return "running";
471
- }
472
- if (key === "completed" || key === "succeeded" || key === "success") {
473
- return "completed";
474
- }
475
- if (key === "interrupted" || key === "cancelled" || key === "canceled" || key === "shutdown") {
476
- return key === "shutdown" ? "shutdown" : "interrupted";
477
- }
478
- if (key === "failed" || key === "error" || key === "systemerror") {
479
- return "failed";
480
- }
481
- if (key === "blocked" || key === "declined") {
482
- return "blocked";
483
- }
484
- return value?.trim();
485
- }
486
-
487
- function secondsToMillis(value: number | null | undefined): number | undefined {
488
- if (typeof value !== "number" || !Number.isFinite(value)) {
489
- return undefined;
490
- }
491
- return value * 1000;
492
- }
493
-
494
- function trimOptional(value: string | null | undefined): string | undefined {
495
- const trimmed = value?.trim();
496
- return trimmed ? trimmed : undefined;
497
- }