@kodelyth/codex 2026.5.42 → 2026.6.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.
- package/package.json +16 -1
- package/doctor-contract-api.test.ts +0 -44
- package/doctor-contract-api.ts +0 -68
- package/harness.ts +0 -72
- package/index.test.ts +0 -230
- package/index.ts +0 -66
- package/media-understanding-provider.test.ts +0 -486
- package/media-understanding-provider.ts +0 -521
- package/prompt-overlay-runtime-contract.test.ts +0 -48
- package/prompt-overlay.ts +0 -21
- package/provider-catalog.ts +0 -83
- package/provider-discovery.ts +0 -45
- package/provider.test.ts +0 -384
- package/provider.ts +0 -243
- package/src/app-server/app-inventory-cache.test.ts +0 -176
- package/src/app-server/app-inventory-cache.ts +0 -324
- package/src/app-server/approval-bridge.test.ts +0 -1471
- package/src/app-server/approval-bridge.ts +0 -1211
- package/src/app-server/auth-bridge.test.ts +0 -1449
- package/src/app-server/auth-bridge.ts +0 -614
- package/src/app-server/auth-profile-runtime-contract.test.ts +0 -239
- package/src/app-server/capabilities.ts +0 -27
- package/src/app-server/client-factory.ts +0 -24
- package/src/app-server/client.test.ts +0 -563
- package/src/app-server/client.ts +0 -715
- package/src/app-server/compact.test.ts +0 -710
- package/src/app-server/compact.ts +0 -500
- package/src/app-server/computer-use.test.ts +0 -788
- package/src/app-server/computer-use.ts +0 -683
- package/src/app-server/config.test.ts +0 -879
- package/src/app-server/config.ts +0 -1038
- package/src/app-server/context-engine-projection.test.ts +0 -252
- package/src/app-server/context-engine-projection.ts +0 -403
- package/src/app-server/delivery-no-reply-runtime-contract.test.ts +0 -80
- package/src/app-server/dynamic-tool-diagnostics.ts +0 -73
- package/src/app-server/dynamic-tool-profile.ts +0 -69
- package/src/app-server/dynamic-tools.test.ts +0 -1302
- package/src/app-server/dynamic-tools.ts +0 -623
- package/src/app-server/elicitation-bridge.test.ts +0 -1056
- package/src/app-server/elicitation-bridge.ts +0 -783
- package/src/app-server/event-projector.test.ts +0 -2668
- package/src/app-server/event-projector.ts +0 -2057
- package/src/app-server/image-payload-sanitizer.test.ts +0 -49
- package/src/app-server/image-payload-sanitizer.ts +0 -167
- package/src/app-server/klaw-owned-tool-runtime-contract.test.ts +0 -456
- package/src/app-server/local-runtime-attribution.ts +0 -39
- package/src/app-server/managed-binary.test.ts +0 -139
- package/src/app-server/managed-binary.ts +0 -193
- package/src/app-server/models.test.ts +0 -246
- package/src/app-server/models.ts +0 -172
- package/src/app-server/native-hook-relay.test.ts +0 -271
- package/src/app-server/native-hook-relay.ts +0 -150
- package/src/app-server/native-subagent-task-mirror.test.ts +0 -573
- package/src/app-server/native-subagent-task-mirror.ts +0 -497
- package/src/app-server/outcome-fallback-runtime-contract.test.ts +0 -404
- package/src/app-server/plugin-activation.test.ts +0 -336
- package/src/app-server/plugin-activation.ts +0 -283
- package/src/app-server/plugin-app-cache-key.ts +0 -74
- package/src/app-server/plugin-approval-roundtrip.ts +0 -122
- package/src/app-server/plugin-inventory.test.ts +0 -355
- package/src/app-server/plugin-inventory.ts +0 -357
- package/src/app-server/plugin-thread-config.test.ts +0 -865
- package/src/app-server/plugin-thread-config.ts +0 -455
- package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +0 -33
- package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +0 -199
- package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +0 -102
- package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +0 -227
- package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +0 -2630
- package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +0 -2630
- package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +0 -1659
- package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +0 -1655
- package/src/app-server/protocol-validators.test.ts +0 -75
- package/src/app-server/protocol-validators.ts +0 -203
- package/src/app-server/protocol.ts +0 -520
- package/src/app-server/rate-limit-cache.ts +0 -48
- package/src/app-server/rate-limits.test.ts +0 -202
- package/src/app-server/rate-limits.ts +0 -583
- package/src/app-server/request.ts +0 -73
- package/src/app-server/run-attempt.context-engine.test.ts +0 -1004
- package/src/app-server/run-attempt.test.ts +0 -9477
- package/src/app-server/run-attempt.ts +0 -4683
- package/src/app-server/run-attempt.vision-tools.test.ts +0 -35
- package/src/app-server/schema-normalization-runtime-contract.test.ts +0 -206
- package/src/app-server/session-binding.test.ts +0 -303
- package/src/app-server/session-binding.ts +0 -398
- package/src/app-server/session-history.ts +0 -44
- package/src/app-server/shared-client.test.ts +0 -589
- package/src/app-server/shared-client.ts +0 -289
- package/src/app-server/side-question.test.ts +0 -1175
- package/src/app-server/side-question.ts +0 -1007
- package/src/app-server/test-support.ts +0 -48
- package/src/app-server/thread-lifecycle.test.ts +0 -447
- package/src/app-server/thread-lifecycle.ts +0 -939
- package/src/app-server/thread-lifecycle.user-mcp-servers.test.ts +0 -442
- package/src/app-server/timeout.ts +0 -9
- package/src/app-server/tool-progress-normalization.ts +0 -77
- package/src/app-server/trajectory.test.ts +0 -205
- package/src/app-server/trajectory.ts +0 -365
- package/src/app-server/transcript-mirror.test.ts +0 -524
- package/src/app-server/transcript-mirror.ts +0 -208
- package/src/app-server/transcript-repair-runtime-contract.test.ts +0 -44
- package/src/app-server/transport-stdio.test.ts +0 -171
- package/src/app-server/transport-stdio.ts +0 -107
- package/src/app-server/transport-websocket.test.ts +0 -69
- package/src/app-server/transport-websocket.ts +0 -90
- package/src/app-server/transport.ts +0 -117
- package/src/app-server/user-input-bridge.test.ts +0 -249
- package/src/app-server/user-input-bridge.ts +0 -316
- package/src/app-server/version.ts +0 -4
- package/src/app-server/vision-tools.ts +0 -12
- package/src/command-account.ts +0 -544
- package/src/command-formatters.ts +0 -425
- package/src/command-handlers.ts +0 -2004
- package/src/command-rpc.test.ts +0 -16
- package/src/command-rpc.ts +0 -142
- package/src/commands.test.ts +0 -3312
- package/src/commands.ts +0 -65
- package/src/conversation-binding-data.ts +0 -124
- package/src/conversation-binding.test.ts +0 -599
- package/src/conversation-binding.ts +0 -561
- package/src/conversation-control.test.ts +0 -126
- package/src/conversation-control.ts +0 -303
- package/src/conversation-turn-collector.test.ts +0 -191
- package/src/conversation-turn-collector.ts +0 -186
- package/src/conversation-turn-input.test.ts +0 -141
- package/src/conversation-turn-input.ts +0 -106
- package/src/manifest.test.ts +0 -20
- package/src/migration/apply.ts +0 -501
- package/src/migration/helpers.ts +0 -55
- package/src/migration/plan.ts +0 -461
- package/src/migration/provider.test.ts +0 -1741
- package/src/migration/provider.ts +0 -41
- package/src/migration/source.ts +0 -643
- package/src/migration/targets.ts +0 -25
- package/src/node-cli-sessions.test.ts +0 -180
- package/src/node-cli-sessions.ts +0 -711
- package/test-api.ts +0 -82
- package/tsconfig.json +0 -16
|
@@ -1,561 +0,0 @@
|
|
|
1
|
-
import { formatErrorMessage } from "klaw/plugin-sdk/agent-harness-runtime";
|
|
2
|
-
import type {
|
|
3
|
-
PluginConversationBindingResolvedEvent,
|
|
4
|
-
PluginHookInboundClaimContext,
|
|
5
|
-
PluginHookInboundClaimEvent,
|
|
6
|
-
} from "klaw/plugin-sdk/plugin-entry";
|
|
7
|
-
import type { ReplyPayload } from "klaw/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("klaw.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 Klaw conversation. Answer normally; Klaw 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: "Klaw native Codex conversation binding does not expose dynamic Klaw 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
|
-
"Klaw 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
|
-
"Klaw 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
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs/promises";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { clearRuntimeAuthProfileStoreSnapshots } from "klaw/plugin-sdk/agent-runtime";
|
|
5
|
-
import { upsertAuthProfile } from "klaw/plugin-sdk/provider-auth";
|
|
6
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
7
|
-
import {
|
|
8
|
-
readCodexAppServerBinding,
|
|
9
|
-
writeCodexAppServerBinding,
|
|
10
|
-
} from "./app-server/session-binding.js";
|
|
11
|
-
import {
|
|
12
|
-
setCodexConversationFastMode,
|
|
13
|
-
setCodexConversationModel,
|
|
14
|
-
setCodexConversationPermissions,
|
|
15
|
-
} from "./conversation-control.js";
|
|
16
|
-
|
|
17
|
-
let tempDir: string;
|
|
18
|
-
|
|
19
|
-
const sharedClientMocks = vi.hoisted(() => ({
|
|
20
|
-
getSharedCodexAppServerClient: vi.fn(),
|
|
21
|
-
}));
|
|
22
|
-
|
|
23
|
-
vi.mock("./app-server/shared-client.js", () => sharedClientMocks);
|
|
24
|
-
|
|
25
|
-
describe("codex conversation controls", () => {
|
|
26
|
-
beforeEach(async () => {
|
|
27
|
-
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "klaw-codex-control-"));
|
|
28
|
-
vi.stubEnv("KLAW_STATE_DIR", tempDir);
|
|
29
|
-
sharedClientMocks.getSharedCodexAppServerClient.mockReset();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
afterEach(async () => {
|
|
33
|
-
vi.unstubAllEnvs();
|
|
34
|
-
clearRuntimeAuthProfileStoreSnapshots();
|
|
35
|
-
await fs.rm(tempDir, { recursive: true, force: true });
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("persists fast mode and permissions for later bound turns", async () => {
|
|
39
|
-
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
40
|
-
await writeCodexAppServerBinding(sessionFile, {
|
|
41
|
-
threadId: "thread-1",
|
|
42
|
-
cwd: tempDir,
|
|
43
|
-
model: "gpt-5.4",
|
|
44
|
-
modelProvider: "openai",
|
|
45
|
-
approvalPolicy: "never",
|
|
46
|
-
sandbox: "danger-full-access",
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
await expect(setCodexConversationFastMode({ sessionFile, enabled: true })).resolves.toBe(
|
|
50
|
-
"Codex fast mode enabled.",
|
|
51
|
-
);
|
|
52
|
-
await expect(setCodexConversationPermissions({ sessionFile, mode: "default" })).resolves.toBe(
|
|
53
|
-
"Codex permissions set to default.",
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
const binding = await readCodexAppServerBinding(sessionFile);
|
|
57
|
-
expect(binding?.threadId).toBe("thread-1");
|
|
58
|
-
expect(binding?.serviceTier).toBe("priority");
|
|
59
|
-
expect(binding?.approvalPolicy).toBe("on-request");
|
|
60
|
-
expect(binding?.sandbox).toBe("workspace-write");
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("does not persist public OpenAI provider after model changes on native auth bindings", async () => {
|
|
64
|
-
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
65
|
-
const agentDir = path.join(tempDir, "agents", "bot-a", "agent");
|
|
66
|
-
upsertAuthProfile({
|
|
67
|
-
profileId: "work",
|
|
68
|
-
credential: {
|
|
69
|
-
type: "oauth",
|
|
70
|
-
provider: "openai-codex",
|
|
71
|
-
access: "access-token",
|
|
72
|
-
refresh: "refresh-token",
|
|
73
|
-
expires: Date.now() + 60_000,
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
await writeCodexAppServerBinding(sessionFile, {
|
|
77
|
-
threadId: "thread-1",
|
|
78
|
-
cwd: tempDir,
|
|
79
|
-
authProfileId: "work",
|
|
80
|
-
model: "gpt-5.4",
|
|
81
|
-
modelProvider: "openai",
|
|
82
|
-
});
|
|
83
|
-
sharedClientMocks.getSharedCodexAppServerClient.mockResolvedValue({
|
|
84
|
-
request: vi.fn(async () => ({
|
|
85
|
-
thread: { id: "thread-1", cwd: tempDir },
|
|
86
|
-
model: "gpt-5.5",
|
|
87
|
-
modelProvider: "openai",
|
|
88
|
-
})),
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
await expect(
|
|
92
|
-
setCodexConversationModel({ sessionFile, agentDir, model: "gpt-5.5" }),
|
|
93
|
-
).resolves.toBe("Codex model set to gpt-5.5.");
|
|
94
|
-
|
|
95
|
-
const raw = await fs.readFile(`${sessionFile}.codex-app-server.json`, "utf8");
|
|
96
|
-
const binding = await readCodexAppServerBinding(sessionFile);
|
|
97
|
-
const sharedClientParams = sharedClientMocks.getSharedCodexAppServerClient.mock.calls[0]?.[0];
|
|
98
|
-
expect(sharedClientParams?.agentDir).toBe(agentDir);
|
|
99
|
-
expect(raw).not.toContain('"modelProvider": "openai"');
|
|
100
|
-
expect(binding?.threadId).toBe("thread-1");
|
|
101
|
-
expect(binding?.authProfileId).toBe("work");
|
|
102
|
-
expect(binding?.model).toBe("gpt-5.5");
|
|
103
|
-
expect(binding?.modelProvider).toBeUndefined();
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("escapes model names returned from Codex before chat display", async () => {
|
|
107
|
-
const sessionFile = path.join(tempDir, "session.jsonl");
|
|
108
|
-
await writeCodexAppServerBinding(sessionFile, {
|
|
109
|
-
threadId: "thread-1",
|
|
110
|
-
cwd: tempDir,
|
|
111
|
-
model: "gpt-5.4",
|
|
112
|
-
modelProvider: "openai",
|
|
113
|
-
});
|
|
114
|
-
sharedClientMocks.getSharedCodexAppServerClient.mockResolvedValue({
|
|
115
|
-
request: vi.fn(async () => ({
|
|
116
|
-
thread: { id: "thread-1", cwd: tempDir },
|
|
117
|
-
model: "gpt-5.5 <@U123> [trusted](https://evil)",
|
|
118
|
-
modelProvider: "openai",
|
|
119
|
-
})),
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
await expect(setCodexConversationModel({ sessionFile, model: "gpt-5.5" })).resolves.toBe(
|
|
123
|
-
"Codex model set to gpt-5.5 <\uff20U123> \uff3btrusted\uff3d\uff08https://evil\uff09.",
|
|
124
|
-
);
|
|
125
|
-
});
|
|
126
|
-
});
|