@gakr-gakr/codex 0.1.0 → 0.1.1

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 (115) hide show
  1. package/dist/client-DNN2uyJW.js +642 -0
  2. package/dist/client-factory-Bu9OClHJ.js +9 -0
  3. package/dist/command-formatters-BpPOTePl.js +520 -0
  4. package/dist/command-handlers-BBs7Vws9.js +1533 -0
  5. package/dist/compact-CDboBy7o.js +329 -0
  6. package/dist/computer-use-DCZB46Sw.js +367 -0
  7. package/dist/config-CLMSw0p2.js +510 -0
  8. package/dist/doctor-contract-api.js +53 -0
  9. package/dist/harness.js +51 -0
  10. package/dist/index.js +1171 -0
  11. package/dist/media-understanding-provider.js +335 -0
  12. package/dist/models-jLA2SIvd.js +110 -0
  13. package/dist/node-cli-sessions-BLRDs_US.js +1216 -0
  14. package/dist/plugin-activation-CEy_oYpx.js +452 -0
  15. package/dist/prompt-overlay.js +12 -0
  16. package/dist/protocol-C9UWI98H.js +9 -0
  17. package/dist/protocol-validators-BGBspNmF.js +5988 -0
  18. package/dist/provider-catalog.js +84 -0
  19. package/dist/provider-discovery.js +33 -0
  20. package/dist/provider.js +150 -0
  21. package/dist/rate-limit-cache-9LxQdE0K.js +24 -0
  22. package/dist/request-DbSPeTcV.js +89 -0
  23. package/dist/rolldown-runtime-DUslC3ob.js +14 -0
  24. package/dist/run-attempt-BoEwzQCv.js +5463 -0
  25. package/dist/session-binding-e2GFp9VH.js +222 -0
  26. package/dist/shared-client-D7Vy0glq.js +631 -0
  27. package/dist/side-question-BDLuEzFP.js +668 -0
  28. package/dist/test-api.js +49 -0
  29. package/dist/thread-lifecycle-Clo0EHMk.js +1565 -0
  30. package/dist/vision-tools-Cofrv35p.js +1379 -0
  31. package/package.json +16 -1
  32. package/doctor-contract-api.ts +0 -68
  33. package/harness.ts +0 -72
  34. package/index.ts +0 -124
  35. package/media-understanding-provider.ts +0 -521
  36. package/prompt-overlay.ts +0 -21
  37. package/provider-catalog.ts +0 -83
  38. package/provider-discovery.ts +0 -45
  39. package/provider.ts +0 -243
  40. package/src/app-server/app-inventory-cache.ts +0 -324
  41. package/src/app-server/approval-bridge.ts +0 -1211
  42. package/src/app-server/auth-bridge.ts +0 -614
  43. package/src/app-server/capabilities.ts +0 -27
  44. package/src/app-server/client-factory.ts +0 -24
  45. package/src/app-server/client.ts +0 -715
  46. package/src/app-server/compact.ts +0 -512
  47. package/src/app-server/computer-use.ts +0 -683
  48. package/src/app-server/config.ts +0 -1038
  49. package/src/app-server/context-engine-projection.ts +0 -403
  50. package/src/app-server/dynamic-tool-diagnostics.ts +0 -73
  51. package/src/app-server/dynamic-tool-profile.ts +0 -70
  52. package/src/app-server/dynamic-tools.ts +0 -623
  53. package/src/app-server/elicitation-bridge.ts +0 -783
  54. package/src/app-server/event-projector.ts +0 -2065
  55. package/src/app-server/image-payload-sanitizer.ts +0 -167
  56. package/src/app-server/local-runtime-attribution.ts +0 -39
  57. package/src/app-server/managed-binary.ts +0 -193
  58. package/src/app-server/models.ts +0 -172
  59. package/src/app-server/native-hook-relay.ts +0 -150
  60. package/src/app-server/native-subagent-task-mirror.ts +0 -497
  61. package/src/app-server/plugin-activation.ts +0 -283
  62. package/src/app-server/plugin-app-cache-key.ts +0 -74
  63. package/src/app-server/plugin-approval-roundtrip.ts +0 -122
  64. package/src/app-server/plugin-inventory.ts +0 -357
  65. package/src/app-server/plugin-thread-config.ts +0 -455
  66. package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +0 -33
  67. package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +0 -199
  68. package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +0 -102
  69. package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +0 -227
  70. package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +0 -2630
  71. package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +0 -2630
  72. package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +0 -1659
  73. package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +0 -1655
  74. package/src/app-server/protocol-validators.ts +0 -203
  75. package/src/app-server/protocol.ts +0 -520
  76. package/src/app-server/rate-limit-cache.ts +0 -48
  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.ts +0 -4862
  80. package/src/app-server/session-binding.ts +0 -398
  81. package/src/app-server/session-history.ts +0 -44
  82. package/src/app-server/shared-client.ts +0 -289
  83. package/src/app-server/side-question.ts +0 -1009
  84. package/src/app-server/test-support.ts +0 -48
  85. package/src/app-server/thread-lifecycle.ts +0 -959
  86. package/src/app-server/timeout.ts +0 -9
  87. package/src/app-server/tool-progress-normalization.ts +0 -77
  88. package/src/app-server/trajectory.ts +0 -368
  89. package/src/app-server/transcript-mirror.ts +0 -208
  90. package/src/app-server/transport-stdio.ts +0 -107
  91. package/src/app-server/transport-websocket.ts +0 -90
  92. package/src/app-server/transport.ts +0 -117
  93. package/src/app-server/user-input-bridge.ts +0 -316
  94. package/src/app-server/version.ts +0 -4
  95. package/src/app-server/vision-tools.ts +0 -12
  96. package/src/command-account.ts +0 -544
  97. package/src/command-formatters.ts +0 -426
  98. package/src/command-handlers.ts +0 -2021
  99. package/src/command-plugins-management.ts +0 -137
  100. package/src/command-rpc.ts +0 -142
  101. package/src/commands.ts +0 -65
  102. package/src/conversation-binding-data.ts +0 -124
  103. package/src/conversation-binding.ts +0 -561
  104. package/src/conversation-control.ts +0 -303
  105. package/src/conversation-turn-collector.ts +0 -186
  106. package/src/conversation-turn-input.ts +0 -106
  107. package/src/migration/apply.ts +0 -501
  108. package/src/migration/helpers.ts +0 -55
  109. package/src/migration/plan.ts +0 -461
  110. package/src/migration/provider.ts +0 -41
  111. package/src/migration/source.ts +0 -643
  112. package/src/migration/targets.ts +0 -25
  113. package/src/node-cli-sessions.ts +0 -711
  114. package/test-api.ts +0 -95
  115. package/tsconfig.json +0 -16
@@ -1,561 +0,0 @@
1
- import { formatErrorMessage } from "autobot/plugin-sdk/agent-harness-runtime";
2
- import type {
3
- PluginConversationBindingResolvedEvent,
4
- PluginHookInboundClaimContext,
5
- PluginHookInboundClaimEvent,
6
- } from "autobot/plugin-sdk/plugin-entry";
7
- import type { ReplyPayload } from "autobot/plugin-sdk/reply-payload";
8
- import { resolveCodexAppServerAuthProfileIdForAgent } from "./app-server/auth-bridge.js";
9
- import { CODEX_CONTROL_METHODS } from "./app-server/capabilities.js";
10
- import {
11
- codexSandboxPolicyForTurn,
12
- resolveCodexAppServerRuntimeOptions,
13
- type CodexAppServerApprovalPolicy,
14
- type CodexAppServerSandboxMode,
15
- } from "./app-server/config.js";
16
- import {
17
- type CodexServiceTier,
18
- type CodexThreadResumeResponse,
19
- type CodexThreadStartResponse,
20
- type CodexTurnStartResponse,
21
- type JsonValue,
22
- } from "./app-server/protocol.js";
23
- import {
24
- clearCodexAppServerBinding,
25
- isCodexAppServerNativeAuthProfile,
26
- normalizeCodexAppServerBindingModelProvider,
27
- readCodexAppServerBinding,
28
- writeCodexAppServerBinding,
29
- type CodexAppServerAuthProfileLookup,
30
- } from "./app-server/session-binding.js";
31
- import { getSharedCodexAppServerClient } from "./app-server/shared-client.js";
32
- import { formatCodexDisplayText } from "./command-formatters.js";
33
- import {
34
- createCodexConversationBindingData,
35
- readCodexConversationBindingData,
36
- readCodexConversationBindingDataRecord,
37
- resolveCodexDefaultWorkspaceDir,
38
- type CodexAppServerConversationBindingData,
39
- } from "./conversation-binding-data.js";
40
- import { trackCodexConversationActiveTurn } from "./conversation-control.js";
41
- import { createCodexConversationTurnCollector } from "./conversation-turn-collector.js";
42
- import { buildCodexConversationTurnInput } from "./conversation-turn-input.js";
43
- import { resumeCodexCliSessionOnNode } from "./node-cli-sessions.js";
44
-
45
- const DEFAULT_BOUND_TURN_TIMEOUT_MS = 20 * 60_000;
46
-
47
- export {
48
- createCodexCliNodeConversationBindingData,
49
- readCodexConversationBindingData,
50
- resolveCodexDefaultWorkspaceDir,
51
- } from "./conversation-binding-data.js";
52
-
53
- type CodexConversationRunOptions = {
54
- pluginConfig?: unknown;
55
- timeoutMs?: number;
56
- resumeCodexCliSessionOnNode?: ResumeCodexCliSessionOnNodeFn;
57
- };
58
-
59
- type ResumeCodexCliSessionOnNodeFn = (
60
- params: Omit<Parameters<typeof resumeCodexCliSessionOnNode>[0], "runtime">,
61
- ) => ReturnType<typeof resumeCodexCliSessionOnNode>;
62
-
63
- type CodexConversationStartParams = {
64
- pluginConfig?: unknown;
65
- config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
66
- sessionFile: string;
67
- workspaceDir?: string;
68
- agentDir?: string;
69
- threadId?: string;
70
- model?: string;
71
- modelProvider?: string;
72
- authProfileId?: string;
73
- approvalPolicy?: CodexAppServerApprovalPolicy;
74
- sandbox?: CodexAppServerSandboxMode;
75
- serviceTier?: CodexServiceTier;
76
- };
77
-
78
- type BoundTurnResult = {
79
- reply: ReplyPayload;
80
- };
81
-
82
- type CodexConversationGlobalState = {
83
- queues: Map<string, Promise<void>>;
84
- };
85
-
86
- const CODEX_CONVERSATION_GLOBAL_STATE = Symbol.for("autobot.codex.conversationBinding");
87
-
88
- function getGlobalState(): CodexConversationGlobalState {
89
- const globalState = globalThis as typeof globalThis & {
90
- [CODEX_CONVERSATION_GLOBAL_STATE]?: CodexConversationGlobalState;
91
- };
92
- globalState[CODEX_CONVERSATION_GLOBAL_STATE] ??= { queues: new Map() };
93
- return globalState[CODEX_CONVERSATION_GLOBAL_STATE];
94
- }
95
-
96
- export async function startCodexConversationThread(
97
- params: CodexConversationStartParams,
98
- ): Promise<CodexAppServerConversationBindingData> {
99
- const workspaceDir =
100
- params.workspaceDir?.trim() || resolveCodexDefaultWorkspaceDir(params.pluginConfig);
101
- const agentDir = params.agentDir?.trim();
102
- const agentLookup = buildAgentLookup({ agentDir, config: params.config });
103
- const existingBinding = await readCodexAppServerBinding(params.sessionFile, {
104
- ...agentLookup,
105
- });
106
- const authProfileId = resolveCodexAppServerAuthProfileIdForAgent({
107
- authProfileId: params.authProfileId ?? existingBinding?.authProfileId,
108
- ...agentLookup,
109
- });
110
- if (params.threadId?.trim()) {
111
- await attachExistingThread({
112
- pluginConfig: params.pluginConfig,
113
- sessionFile: params.sessionFile,
114
- threadId: params.threadId.trim(),
115
- workspaceDir,
116
- ...(agentDir ? { agentDir } : {}),
117
- model: params.model,
118
- modelProvider: params.modelProvider,
119
- authProfileId,
120
- approvalPolicy: params.approvalPolicy,
121
- sandbox: params.sandbox,
122
- serviceTier: params.serviceTier,
123
- config: params.config,
124
- });
125
- } else {
126
- await createThread({
127
- pluginConfig: params.pluginConfig,
128
- sessionFile: params.sessionFile,
129
- workspaceDir,
130
- ...(agentDir ? { agentDir } : {}),
131
- model: params.model,
132
- modelProvider: params.modelProvider,
133
- authProfileId,
134
- approvalPolicy: params.approvalPolicy,
135
- sandbox: params.sandbox,
136
- serviceTier: params.serviceTier,
137
- config: params.config,
138
- });
139
- }
140
- return createCodexConversationBindingData({
141
- sessionFile: params.sessionFile,
142
- workspaceDir,
143
- ...(agentDir ? { agentDir } : {}),
144
- });
145
- }
146
-
147
- export async function handleCodexConversationInboundClaim(
148
- event: PluginHookInboundClaimEvent,
149
- ctx: PluginHookInboundClaimContext,
150
- options: CodexConversationRunOptions = {},
151
- ): Promise<{ handled: boolean; reply?: ReplyPayload } | undefined> {
152
- const data = readCodexConversationBindingData(ctx.pluginBinding);
153
- if (!data) {
154
- return undefined;
155
- }
156
- if (event.commandAuthorized !== true) {
157
- return { handled: true };
158
- }
159
- const prompt = event.bodyForAgent?.trim() || event.content?.trim() || "";
160
- if (!prompt) {
161
- return { handled: true };
162
- }
163
- if (data.kind === "codex-cli-node-session") {
164
- const resume = options.resumeCodexCliSessionOnNode;
165
- if (!resume) {
166
- return {
167
- handled: true,
168
- reply: {
169
- text: "Codex CLI node binding is unavailable because Gateway node runtime is not attached.",
170
- },
171
- };
172
- }
173
- try {
174
- const result = await enqueueBoundTurn(`${data.nodeId}:${data.sessionId}`, async () => {
175
- const resumed = await resume({
176
- nodeId: data.nodeId,
177
- sessionId: data.sessionId,
178
- prompt,
179
- cwd: data.cwd,
180
- timeoutMs: options.timeoutMs,
181
- });
182
- return { reply: { text: resumed.text.trim() || "Codex completed without a text reply." } };
183
- });
184
- return { handled: true, reply: result.reply };
185
- } catch (error) {
186
- return {
187
- handled: true,
188
- reply: {
189
- text: `Codex CLI node turn failed: ${formatCodexDisplayText(formatErrorMessage(error))}`,
190
- },
191
- };
192
- }
193
- }
194
- try {
195
- const result = await enqueueBoundTurn(data.sessionFile, () =>
196
- runBoundTurnWithMissingThreadRecovery({
197
- data,
198
- prompt,
199
- event,
200
- pluginConfig: options.pluginConfig,
201
- timeoutMs: options.timeoutMs,
202
- }),
203
- );
204
- return { handled: true, reply: result.reply };
205
- } catch (error) {
206
- return {
207
- handled: true,
208
- reply: {
209
- text: `Codex app-server turn failed: ${formatCodexDisplayText(formatErrorMessage(error))}`,
210
- },
211
- };
212
- }
213
- }
214
-
215
- export async function handleCodexConversationBindingResolved(
216
- event: PluginConversationBindingResolvedEvent,
217
- ): Promise<void> {
218
- if (event.status !== "denied") {
219
- return;
220
- }
221
- const data = readCodexConversationBindingDataRecord(event.request.data ?? {});
222
- if (!data || data.kind !== "codex-app-server-session") {
223
- return;
224
- }
225
- await clearCodexAppServerBinding(data.sessionFile);
226
- }
227
-
228
- async function attachExistingThread(params: {
229
- pluginConfig?: unknown;
230
- sessionFile: string;
231
- threadId: string;
232
- workspaceDir: string;
233
- agentDir?: string;
234
- model?: string;
235
- modelProvider?: string;
236
- authProfileId?: string;
237
- approvalPolicy?: CodexAppServerApprovalPolicy;
238
- sandbox?: CodexAppServerSandboxMode;
239
- serviceTier?: CodexServiceTier;
240
- config?: CodexAppServerAuthProfileLookup["config"];
241
- }): Promise<void> {
242
- const runtime = resolveCodexAppServerRuntimeOptions({
243
- pluginConfig: params.pluginConfig,
244
- });
245
- const agentLookup = buildAgentLookup({ agentDir: params.agentDir, config: params.config });
246
- const modelProvider = resolveThreadRequestModelProvider({
247
- authProfileId: params.authProfileId,
248
- modelProvider: params.modelProvider,
249
- ...agentLookup,
250
- });
251
- const client = await getSharedCodexAppServerClient({
252
- startOptions: runtime.start,
253
- timeoutMs: runtime.requestTimeoutMs,
254
- authProfileId: params.authProfileId,
255
- ...agentLookup,
256
- });
257
- const response: CodexThreadResumeResponse = await client.request(
258
- CODEX_CONTROL_METHODS.resumeThread,
259
- {
260
- threadId: params.threadId,
261
- ...(params.model ? { model: params.model } : {}),
262
- ...(modelProvider ? { modelProvider } : {}),
263
- approvalPolicy: params.approvalPolicy ?? runtime.approvalPolicy,
264
- approvalsReviewer: runtime.approvalsReviewer,
265
- sandbox: params.sandbox ?? runtime.sandbox,
266
- ...((params.serviceTier ?? runtime.serviceTier)
267
- ? { serviceTier: params.serviceTier ?? runtime.serviceTier }
268
- : {}),
269
- persistExtendedHistory: true,
270
- },
271
- { timeoutMs: runtime.requestTimeoutMs },
272
- );
273
- const thread = response.thread;
274
- const runtimeApprovalPolicy =
275
- typeof runtime.approvalPolicy === "string" ? runtime.approvalPolicy : undefined;
276
- await writeCodexAppServerBinding(
277
- params.sessionFile,
278
- {
279
- threadId: thread.id,
280
- cwd: thread.cwd ?? params.workspaceDir,
281
- authProfileId: params.authProfileId,
282
- model: response.model ?? params.model,
283
- modelProvider: normalizeCodexAppServerBindingModelProvider({
284
- authProfileId: params.authProfileId,
285
- modelProvider: response.modelProvider ?? params.modelProvider,
286
- ...agentLookup,
287
- }),
288
- approvalPolicy: params.approvalPolicy ?? runtimeApprovalPolicy,
289
- sandbox: params.sandbox ?? runtime.sandbox,
290
- serviceTier: params.serviceTier ?? runtime.serviceTier,
291
- },
292
- {
293
- ...agentLookup,
294
- },
295
- );
296
- }
297
-
298
- async function createThread(params: {
299
- pluginConfig?: unknown;
300
- sessionFile: string;
301
- workspaceDir: string;
302
- agentDir?: string;
303
- model?: string;
304
- modelProvider?: string;
305
- authProfileId?: string;
306
- approvalPolicy?: CodexAppServerApprovalPolicy;
307
- sandbox?: CodexAppServerSandboxMode;
308
- serviceTier?: CodexServiceTier;
309
- config?: CodexAppServerAuthProfileLookup["config"];
310
- }): Promise<void> {
311
- const runtime = resolveCodexAppServerRuntimeOptions({
312
- pluginConfig: params.pluginConfig,
313
- });
314
- const agentLookup = buildAgentLookup({ agentDir: params.agentDir, config: params.config });
315
- const modelProvider = resolveThreadRequestModelProvider({
316
- authProfileId: params.authProfileId,
317
- modelProvider: params.modelProvider,
318
- ...agentLookup,
319
- });
320
- const client = await getSharedCodexAppServerClient({
321
- startOptions: runtime.start,
322
- timeoutMs: runtime.requestTimeoutMs,
323
- authProfileId: params.authProfileId,
324
- ...agentLookup,
325
- });
326
- const response: CodexThreadStartResponse = await client.request(
327
- "thread/start",
328
- {
329
- cwd: params.workspaceDir,
330
- ...(params.model ? { model: params.model } : {}),
331
- ...(modelProvider ? { modelProvider } : {}),
332
- approvalPolicy: params.approvalPolicy ?? runtime.approvalPolicy,
333
- approvalsReviewer: runtime.approvalsReviewer,
334
- sandbox: params.sandbox ?? runtime.sandbox,
335
- ...((params.serviceTier ?? runtime.serviceTier)
336
- ? { serviceTier: params.serviceTier ?? runtime.serviceTier }
337
- : {}),
338
- developerInstructions:
339
- "This Codex thread is bound to an AutoBot conversation. Answer normally; AutoBot will deliver your final response back to the conversation.",
340
- experimentalRawEvents: true,
341
- persistExtendedHistory: true,
342
- },
343
- { timeoutMs: runtime.requestTimeoutMs },
344
- );
345
- const runtimeApprovalPolicy =
346
- typeof runtime.approvalPolicy === "string" ? runtime.approvalPolicy : undefined;
347
- await writeCodexAppServerBinding(
348
- params.sessionFile,
349
- {
350
- threadId: response.thread.id,
351
- cwd: response.thread.cwd ?? params.workspaceDir,
352
- authProfileId: params.authProfileId,
353
- model: response.model ?? params.model,
354
- modelProvider: normalizeCodexAppServerBindingModelProvider({
355
- authProfileId: params.authProfileId,
356
- modelProvider: response.modelProvider ?? params.modelProvider,
357
- ...agentLookup,
358
- }),
359
- approvalPolicy: params.approvalPolicy ?? runtimeApprovalPolicy,
360
- sandbox: params.sandbox ?? runtime.sandbox,
361
- serviceTier: params.serviceTier ?? runtime.serviceTier,
362
- },
363
- {
364
- ...agentLookup,
365
- },
366
- );
367
- }
368
-
369
- async function runBoundTurn(params: {
370
- data: CodexAppServerConversationBindingData;
371
- prompt: string;
372
- event: PluginHookInboundClaimEvent;
373
- pluginConfig?: unknown;
374
- timeoutMs?: number;
375
- }): Promise<BoundTurnResult> {
376
- const runtime = resolveCodexAppServerRuntimeOptions({
377
- pluginConfig: params.pluginConfig,
378
- });
379
- const agentLookup = buildAgentLookup({ agentDir: params.data.agentDir });
380
- const binding = await readCodexAppServerBinding(params.data.sessionFile, agentLookup);
381
- const threadId = binding?.threadId;
382
- if (!threadId) {
383
- throw new Error("bound Codex conversation has no thread binding");
384
- }
385
-
386
- const client = await getSharedCodexAppServerClient({
387
- startOptions: runtime.start,
388
- timeoutMs: runtime.requestTimeoutMs,
389
- authProfileId: binding.authProfileId,
390
- ...agentLookup,
391
- });
392
- const collector = createCodexConversationTurnCollector(threadId);
393
- const notificationCleanup = client.addNotificationHandler((notification) =>
394
- collector.handleNotification(notification),
395
- );
396
- const requestCleanup = client.addRequestHandler(
397
- async (request): Promise<JsonValue | undefined> => {
398
- if (request.method === "item/tool/call") {
399
- return {
400
- contentItems: [
401
- {
402
- type: "inputText",
403
- text: "AutoBot native Codex conversation binding does not expose dynamic AutoBot tools yet.",
404
- },
405
- ],
406
- success: false,
407
- };
408
- }
409
- if (
410
- request.method === "item/commandExecution/requestApproval" ||
411
- request.method === "item/fileChange/requestApproval"
412
- ) {
413
- return {
414
- decision: "decline",
415
- reason:
416
- "AutoBot native Codex conversation binding cannot route interactive approvals yet; use the Codex harness or explicit /acp spawn codex for that workflow.",
417
- };
418
- }
419
- if (request.method === "item/permissions/requestApproval") {
420
- return { permissions: {}, scope: "turn" };
421
- }
422
- if (request.method.includes("requestApproval")) {
423
- return {
424
- decision: "decline",
425
- reason:
426
- "AutoBot native Codex conversation binding cannot route interactive approvals yet; use the Codex harness or explicit /acp spawn codex for that workflow.",
427
- };
428
- }
429
- return undefined;
430
- },
431
- );
432
- try {
433
- const response: CodexTurnStartResponse = await client.request(
434
- "turn/start",
435
- {
436
- threadId,
437
- input: buildCodexConversationTurnInput({
438
- prompt: params.prompt,
439
- event: params.event,
440
- }),
441
- cwd: binding.cwd || params.data.workspaceDir,
442
- approvalPolicy: binding.approvalPolicy ?? runtime.approvalPolicy,
443
- approvalsReviewer: runtime.approvalsReviewer,
444
- sandboxPolicy: codexSandboxPolicyForTurn(
445
- binding.sandbox ?? runtime.sandbox,
446
- binding.cwd || params.data.workspaceDir,
447
- ),
448
- ...(binding.model ? { model: binding.model } : {}),
449
- ...((binding.serviceTier ?? runtime.serviceTier)
450
- ? { serviceTier: binding.serviceTier ?? runtime.serviceTier }
451
- : {}),
452
- },
453
- { timeoutMs: runtime.requestTimeoutMs },
454
- );
455
- const turnId = response.turn.id;
456
- const activeCleanup = trackCodexConversationActiveTurn({
457
- sessionFile: params.data.sessionFile,
458
- threadId,
459
- turnId,
460
- });
461
- collector.setTurnId(turnId);
462
- const completion = await collector
463
- .wait({
464
- timeoutMs: params.timeoutMs ?? DEFAULT_BOUND_TURN_TIMEOUT_MS,
465
- })
466
- .finally(activeCleanup);
467
- const replyText = completion.replyText.trim();
468
- return {
469
- reply: {
470
- text: replyText || "Codex completed without a text reply.",
471
- },
472
- };
473
- } finally {
474
- notificationCleanup();
475
- requestCleanup();
476
- }
477
- }
478
-
479
- async function runBoundTurnWithMissingThreadRecovery(params: {
480
- data: CodexAppServerConversationBindingData;
481
- prompt: string;
482
- event: PluginHookInboundClaimEvent;
483
- pluginConfig?: unknown;
484
- timeoutMs?: number;
485
- }): Promise<BoundTurnResult> {
486
- try {
487
- return await runBoundTurn(params);
488
- } catch (error) {
489
- if (!isCodexThreadNotFoundError(error)) {
490
- throw error;
491
- }
492
- const agentLookup = buildAgentLookup({ agentDir: params.data.agentDir });
493
- const binding = await readCodexAppServerBinding(params.data.sessionFile, agentLookup);
494
- await startCodexConversationThread({
495
- pluginConfig: params.pluginConfig,
496
- sessionFile: params.data.sessionFile,
497
- workspaceDir: binding?.cwd || params.data.workspaceDir,
498
- ...agentLookup,
499
- model: binding?.model,
500
- modelProvider: binding?.modelProvider,
501
- authProfileId: binding?.authProfileId,
502
- approvalPolicy: binding?.approvalPolicy,
503
- sandbox: binding?.sandbox,
504
- serviceTier: binding?.serviceTier,
505
- });
506
- return await runBoundTurn(params);
507
- }
508
- }
509
-
510
- function isCodexThreadNotFoundError(error: unknown): boolean {
511
- return /\bthread not found:/iu.test(formatErrorMessage(error));
512
- }
513
-
514
- function enqueueBoundTurn<T>(key: string, run: () => Promise<T>): Promise<T> {
515
- const state = getGlobalState();
516
- const previous = state.queues.get(key) ?? Promise.resolve();
517
- const next = previous.then(run, run);
518
- const queued = next.then(
519
- () => undefined,
520
- () => undefined,
521
- );
522
- state.queues.set(key, queued);
523
- void next
524
- .finally(() => {
525
- if (state.queues.get(key) === queued) {
526
- state.queues.delete(key);
527
- }
528
- })
529
- .catch(() => undefined);
530
- return next;
531
- }
532
-
533
- function resolveThreadRequestModelProvider(params: {
534
- authProfileId?: string;
535
- modelProvider?: string;
536
- agentDir?: string;
537
- config?: CodexAppServerAuthProfileLookup["config"];
538
- }): string | undefined {
539
- const modelProvider = params.modelProvider?.trim();
540
- if (!modelProvider || modelProvider.toLowerCase() === "codex") {
541
- return undefined;
542
- }
543
- if (
544
- isCodexAppServerNativeAuthProfile(params) &&
545
- (modelProvider.toLowerCase() === "openai" || modelProvider.toLowerCase() === "openai-codex")
546
- ) {
547
- return undefined;
548
- }
549
- return modelProvider.toLowerCase() === "openai-codex" ? "openai" : modelProvider;
550
- }
551
-
552
- function buildAgentLookup(params: {
553
- agentDir?: string;
554
- config?: CodexAppServerAuthProfileLookup["config"];
555
- }): Pick<CodexAppServerAuthProfileLookup, "agentDir" | "config"> {
556
- const agentDir = params.agentDir?.trim();
557
- return {
558
- ...(agentDir ? { agentDir } : {}),
559
- ...(params.config ? { config: params.config } : {}),
560
- };
561
- }