codex-copilot-dx 1.3.20 → 1.3.21
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/bin/cli.mjs +10 -1
- package/docs/ARCHITECTURE.md +3 -1
- package/docs/REFERENCE.md +4 -2
- package/package.json +1 -1
- package/src/access-settings.mjs +48 -0
- package/src/claude-gateway.mjs +26 -1
- package/src/connection-client-sync.mjs +182 -0
- package/src/connection-management.mjs +264 -0
- package/src/connection-policy.mjs +321 -0
- package/src/connection-routing.mjs +183 -0
- package/src/connection-runtime.mjs +43 -1334
- package/src/connection-view.mjs +320 -0
- package/src/privacy-transform.mjs +32 -7
- package/src/responses-handler.mjs +7 -1
- package/src/responses-proxy.mjs +30 -9
- package/src/source-lifecycle.mjs +61 -0
- package/src/stream-errors.mjs +29 -5
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { checkedApi, checkedAccessSettings, accessError, CLIENT_IDS } from "./access-settings.mjs";
|
|
3
|
+
import { checkedApiKey } from "./api-secrets.mjs";
|
|
4
|
+
import { imageModelSelection } from "./image-models.mjs";
|
|
5
|
+
import { compileClientModels, sourceModels } from "./client-model-plan.mjs";
|
|
6
|
+
import { deploymentModelId } from "./model-capabilities.mjs";
|
|
7
|
+
import { autoReviewReasoningEfforts } from "./models.mjs";
|
|
8
|
+
import { claudeSharedCapabilityPlan, inspectClaudeCapabilityConfiguration } from "./claude-capability-config.mjs";
|
|
9
|
+
|
|
10
|
+
export function createConnectionPolicy({ validObject, binding, catalogRefresh, save, discoveries, getSettings, env, imageModels, enabled,
|
|
11
|
+
getSource, legacy, capabilitySupport, capabilitySupportForSync, plan, snapshot, options, permitted, sourceImpact }) {
|
|
12
|
+
function removeApi(input, next) {
|
|
13
|
+
const existing = next.apis.find(api => api.id === input.id);
|
|
14
|
+
if (
|
|
15
|
+
!validObject(input, ["operation", "id", "impact_revision"]) ||
|
|
16
|
+
!existing ||
|
|
17
|
+
existing.enabled ||
|
|
18
|
+
CLIENT_IDS.some(
|
|
19
|
+
id =>
|
|
20
|
+
binding(id).source === input.id || binding(id).deployments?.some(item => item.source === input.id)
|
|
21
|
+
)
|
|
22
|
+
)
|
|
23
|
+
throw accessError("ccdx_source_in_use", 409);
|
|
24
|
+
const previousRef = existing.secret_ref;
|
|
25
|
+
catalogRefresh.cancel(input.id);
|
|
26
|
+
next.apis = next.apis.filter(api => api.id !== input.id);
|
|
27
|
+
for (const id of CLIENT_IDS)
|
|
28
|
+
next.clients[id].allowed = next.clients[id].allowed.filter(source => source !== input.id);
|
|
29
|
+
save(next);
|
|
30
|
+
discoveries.delete(input.id);
|
|
31
|
+
return previousRef;
|
|
32
|
+
}
|
|
33
|
+
function prepareApiSave(input, next) {
|
|
34
|
+
let freshRef, previousRef;
|
|
35
|
+
const existing = next.apis.find(api => api.id === input.id);
|
|
36
|
+
if (!existing && next.apis.length >= 16) throw accessError("ccdx_api_limit");
|
|
37
|
+
if (
|
|
38
|
+
!validObject(input.config, [
|
|
39
|
+
"name",
|
|
40
|
+
"base_url",
|
|
41
|
+
"endpoints",
|
|
42
|
+
"auth_header",
|
|
43
|
+
"models",
|
|
44
|
+
"key_env",
|
|
45
|
+
"model_display",
|
|
46
|
+
])
|
|
47
|
+
)
|
|
48
|
+
throw accessError("ccdx_api_config_invalid");
|
|
49
|
+
// Existing clients round-trip catalog data. New display-only fields must
|
|
50
|
+
// remain readable without becoming editable connection configuration.
|
|
51
|
+
const { model_display, ...editableConfig } = input.config;
|
|
52
|
+
input = {
|
|
53
|
+
...input,
|
|
54
|
+
config: {
|
|
55
|
+
...editableConfig,
|
|
56
|
+
models: Array.isArray(editableConfig.models)
|
|
57
|
+
? editableConfig.models.map(model => {
|
|
58
|
+
if (!model || typeof model !== "object" || Array.isArray(model)) return model;
|
|
59
|
+
const { display, ...editable } = model;
|
|
60
|
+
return editable;
|
|
61
|
+
})
|
|
62
|
+
: editableConfig.models,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
const id = existing?.id || "api_" + randomBytes(8).toString("hex");
|
|
66
|
+
if (input.key !== undefined && input.key !== "") {
|
|
67
|
+
checkedApiKey(input.key);
|
|
68
|
+
freshRef = randomBytes(16).toString("hex");
|
|
69
|
+
}
|
|
70
|
+
if ((input.config.key_env && freshRef) || (!input.config.key_env && !freshRef && !existing?.secret_ref))
|
|
71
|
+
throw accessError("ccdx_api_key_invalid");
|
|
72
|
+
const config = checkedApi({
|
|
73
|
+
...input.config,
|
|
74
|
+
id,
|
|
75
|
+
enabled: existing?.enabled ?? true,
|
|
76
|
+
...(!input.config.key_env ? { secret_ref: freshRef || existing.secret_ref } : {}),
|
|
77
|
+
});
|
|
78
|
+
if (!Array.isArray(input.allowed) || input.allowed.some(id => !CLIENT_IDS.includes(id)))
|
|
79
|
+
throw accessError("ccdx_connections_action_invalid");
|
|
80
|
+
for (const id of CLIENT_IDS) {
|
|
81
|
+
const c = next.clients[id];
|
|
82
|
+
if (
|
|
83
|
+
c.deployments?.some(
|
|
84
|
+
item =>
|
|
85
|
+
item.source === config.id &&
|
|
86
|
+
(!input.allowed.includes(id) ||
|
|
87
|
+
!config.models.some(
|
|
88
|
+
model =>
|
|
89
|
+
model.id === item.model &&
|
|
90
|
+
model.protocols.includes(id === "codex" ? "responses" : "messages")
|
|
91
|
+
))
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
throw accessError("ccdx_source_in_use", 409);
|
|
95
|
+
if (c.source === config.id && !input.allowed.includes(id)) throw accessError("ccdx_source_in_use", 409);
|
|
96
|
+
if (
|
|
97
|
+
c.source === config.id &&
|
|
98
|
+
c.enabled &&
|
|
99
|
+
!Array.isArray(c.deployments) &&
|
|
100
|
+
(!config.models.some(model => model.protocols.includes(id === "codex" ? "responses" : "messages")) ||
|
|
101
|
+
(id === "codex" &&
|
|
102
|
+
[c.model, c.auto_review_model]
|
|
103
|
+
.filter(Boolean)
|
|
104
|
+
.some(
|
|
105
|
+
value =>
|
|
106
|
+
!config.models.some(model => model.id === value && model.protocols.includes("responses"))
|
|
107
|
+
)))
|
|
108
|
+
)
|
|
109
|
+
throw accessError("ccdx_api_model_unavailable");
|
|
110
|
+
if (input.allowed.includes(id) && !config.endpoints[id === "codex" ? "responses" : "messages"])
|
|
111
|
+
throw accessError("ccdx_api_protocol_required");
|
|
112
|
+
c.allowed = [
|
|
113
|
+
...new Set([
|
|
114
|
+
...c.allowed.filter(value => value !== config.id),
|
|
115
|
+
...(input.allowed.includes(id) ? [config.id] : []),
|
|
116
|
+
]),
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
if (existing) {
|
|
120
|
+
previousRef = existing.secret_ref;
|
|
121
|
+
next.apis[next.apis.indexOf(existing)] = config;
|
|
122
|
+
} else next.apis.push(config);
|
|
123
|
+
checkedAccessSettings(next);
|
|
124
|
+
return { input, freshRef, previousRef };
|
|
125
|
+
}
|
|
126
|
+
function applyClientSelection(input, next) {
|
|
127
|
+
if (
|
|
128
|
+
!CLIENT_IDS.includes(input.id) ||
|
|
129
|
+
typeof input.enabled !== "boolean" ||
|
|
130
|
+
!Array.isArray(input.allowed) ||
|
|
131
|
+
(input.source !== null && input.source !== undefined && !input.allowed.includes(input.source))
|
|
132
|
+
)
|
|
133
|
+
throw accessError("ccdx_connections_action_invalid");
|
|
134
|
+
const old = next.clients[input.id],
|
|
135
|
+
retained = { ...old };
|
|
136
|
+
if (old.source !== input.source || (Array.isArray(old.deployments) && input.deployments === null))
|
|
137
|
+
for (const key of ["model", "auto_review_model", "auto_review_effort"]) delete retained[key];
|
|
138
|
+
next.clients[input.id] = {
|
|
139
|
+
...retained,
|
|
140
|
+
enabled: input.enabled,
|
|
141
|
+
allowed: input.allowed,
|
|
142
|
+
source: input.enabled ? (input.source ?? old.source) : null,
|
|
143
|
+
...(input.deployments !== undefined
|
|
144
|
+
? {
|
|
145
|
+
deployments: input.deployments,
|
|
146
|
+
default_model: input.default_model ?? null,
|
|
147
|
+
auto_sync: input.auto_sync ?? true,
|
|
148
|
+
}
|
|
149
|
+
: old.source !== input.source
|
|
150
|
+
? { deployments: null, default_model: null }
|
|
151
|
+
: {}),
|
|
152
|
+
...(input.model ? { model: input.model } : {}),
|
|
153
|
+
...(input.auto_review_model ? { auto_review_model: input.auto_review_model } : {}),
|
|
154
|
+
...(input.auto_review_effort !== undefined ? { auto_review_effort: input.auto_review_effort } : {}),
|
|
155
|
+
};
|
|
156
|
+
if (input.image_model !== undefined) {
|
|
157
|
+
if (input.id !== "codex" || input.image_model !== old.image_model && !imageModels().models.some(model => model.id === input.image_model)) throw accessError("ccdx_api_model_unavailable");
|
|
158
|
+
next.clients.codex.image_model = input.image_model;
|
|
159
|
+
}
|
|
160
|
+
if (input.id !== "codex") next.claude_private_keys = true;
|
|
161
|
+
if (old.source !== input.source) next.clients[input.id].history_isolation = true;
|
|
162
|
+
const c = next.clients[input.id];
|
|
163
|
+
if (Array.isArray(input.deployments)) {
|
|
164
|
+
checkedAccessSettings(next);
|
|
165
|
+
for (const item of c.deployments || []) {
|
|
166
|
+
if (
|
|
167
|
+
!enabled(item.source) ||
|
|
168
|
+
!sourceModels(getSource(item.source), input.id).some(model => model.id === item.model)
|
|
169
|
+
)
|
|
170
|
+
throw accessError("ccdx_api_model_unavailable");
|
|
171
|
+
}
|
|
172
|
+
const names = Object.fromEntries(next.apis.map(api => [api.id, api.name]));
|
|
173
|
+
const compiled = compileClientModels({
|
|
174
|
+
client: input.id,
|
|
175
|
+
binding: c,
|
|
176
|
+
source: getSource,
|
|
177
|
+
enabled,
|
|
178
|
+
names,
|
|
179
|
+
desktopEffortAliases: capabilitySupport.app,
|
|
180
|
+
});
|
|
181
|
+
if (c.default_model && !compiled.routes.has(c.default_model))
|
|
182
|
+
throw accessError("ccdx_api_model_unavailable");
|
|
183
|
+
c.default_model = compiled.routes.get(compiled.defaultModel)?.model.route_id || null;
|
|
184
|
+
c.source = c.enabled ? compiled.routes.get(c.default_model)?.source || null : null;
|
|
185
|
+
c.history_isolation = true;
|
|
186
|
+
if (input.id === "codex") {
|
|
187
|
+
c.model = compiled.routes.get(c.default_model)?.upstreamModel;
|
|
188
|
+
const review =
|
|
189
|
+
compiled.routes.get(old.auto_review_model) ||
|
|
190
|
+
[...compiled.routes.values()].find(
|
|
191
|
+
record => record.source === old.source && record.upstreamModel === old.auto_review_model
|
|
192
|
+
);
|
|
193
|
+
if (c.default_model)
|
|
194
|
+
c.auto_review_model = review?.source === c.source ? review.model.route_id : c.default_model;
|
|
195
|
+
else delete c.auto_review_model;
|
|
196
|
+
if (review?.source === c.source && old.auto_review_effort !== undefined)
|
|
197
|
+
c.auto_review_effort = old.auto_review_effort;
|
|
198
|
+
else delete c.auto_review_effort;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (c.source && !Array.isArray(input.deployments)) {
|
|
202
|
+
if (!enabled(c.source)) throw accessError("ccdx_client_source_disabled", 503);
|
|
203
|
+
const target = getSource(c.source),
|
|
204
|
+
family = input.id === "codex" ? "gpt" : "claude";
|
|
205
|
+
const api = getSettings().apis.find(api => api.id === c.source);
|
|
206
|
+
if (api?.key_env && !env[api.key_env]) throw accessError("ccdx_api_environment_key_missing", 503);
|
|
207
|
+
if (
|
|
208
|
+
(input.id !== "codex" && c.source === "github") ||
|
|
209
|
+
(c.source !== "github" && !target?.models(family).length)
|
|
210
|
+
)
|
|
211
|
+
throw accessError("ccdx_api_model_unavailable");
|
|
212
|
+
if (input.id === "codex" && c.source !== "github") {
|
|
213
|
+
const previous = c.source === "m365" ? legacy.selection().m365 : null;
|
|
214
|
+
c.model = input.model || previous?.model || target.models("gpt")[0].id;
|
|
215
|
+
c.auto_review_model = input.auto_review_model || previous?.auto_review_model || c.model;
|
|
216
|
+
if (![c.model, c.auto_review_model].every(id => target.models("gpt").some(model => model.id === id)))
|
|
217
|
+
throw accessError("ccdx_api_model_unavailable");
|
|
218
|
+
const efforts = api
|
|
219
|
+
? sourceModels(target, "codex").find(model => model.id === c.auto_review_model)?.capability.values
|
|
220
|
+
.reasoning_efforts || []
|
|
221
|
+
: autoReviewReasoningEfforts(target.models("gpt").find(model => model.id === c.auto_review_model));
|
|
222
|
+
if (
|
|
223
|
+
c.auto_review_effort !== undefined &&
|
|
224
|
+
c.auto_review_effort !== null &&
|
|
225
|
+
!efforts.includes(c.auto_review_effort)
|
|
226
|
+
)
|
|
227
|
+
throw accessError("ccdx_api_effort_unavailable");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (input.operation === "preview_client") {
|
|
231
|
+
const proposed = compileClientModels({
|
|
232
|
+
client: input.id,
|
|
233
|
+
binding: c,
|
|
234
|
+
source: getSource,
|
|
235
|
+
enabled,
|
|
236
|
+
desktopEffortAliases: capabilitySupport.app,
|
|
237
|
+
});
|
|
238
|
+
const shared =
|
|
239
|
+
input.id === "codex"
|
|
240
|
+
? null
|
|
241
|
+
: claudeSharedCapabilityPlan({
|
|
242
|
+
app: input.id === "claude_app" ? proposed.models : plan("claude_app").models,
|
|
243
|
+
code: input.id === "claude_code" ? proposed.models : plan("claude_code").models,
|
|
244
|
+
support: capabilitySupportForSync(),
|
|
245
|
+
});
|
|
246
|
+
return {
|
|
247
|
+
...snapshot(),
|
|
248
|
+
preview: {
|
|
249
|
+
...(input.id === "codex" ? { image: imageModelSelection(imageModels().models, c.image_model) } : {}),
|
|
250
|
+
models: proposed.models.map(model => ({
|
|
251
|
+
source: model.source_id,
|
|
252
|
+
id: model.id,
|
|
253
|
+
public_id: model.public_id,
|
|
254
|
+
capability: model.capability,
|
|
255
|
+
})),
|
|
256
|
+
shared,
|
|
257
|
+
current:
|
|
258
|
+
input.id === "codex" ? null : inspectClaudeCapabilityConfiguration(proposed.models, options),
|
|
259
|
+
saved: false,
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
save(next);
|
|
264
|
+
}
|
|
265
|
+
function applyProviderEnabled(input, next) {
|
|
266
|
+
if (
|
|
267
|
+
typeof input.enabled !== "boolean" ||
|
|
268
|
+
!["github", "m365", ...next.apis.map(api => api.id)].includes(input.id)
|
|
269
|
+
)
|
|
270
|
+
throw accessError("ccdx_connections_action_invalid");
|
|
271
|
+
if (input.id === "m365" && input.enabled && env.CCDX_M365 === "0")
|
|
272
|
+
throw accessError("ccdx_provider_environment_disabled", 409);
|
|
273
|
+
if (input.id === "m365" && !input.enabled && legacy.snapshot().m365.claude.requests?.active > 0)
|
|
274
|
+
throw accessError("ccdx_source_busy", 409);
|
|
275
|
+
if (!input.enabled) {
|
|
276
|
+
const impact = sourceImpact(input.id);
|
|
277
|
+
if (input.impact_revision !== undefined && input.impact_revision !== impact.revision)
|
|
278
|
+
throw accessError("ccdx_connections_conflict", 409);
|
|
279
|
+
const affected = impact.clients.filter(client => client.needs_replacement).map(client => client.id);
|
|
280
|
+
if (affected.some(id => id !== "codex")) next.claude_private_keys = true;
|
|
281
|
+
if (affected.length && !validObject(input.replacements, affected))
|
|
282
|
+
throw accessError("ccdx_source_in_use", 409);
|
|
283
|
+
for (const id of affected) {
|
|
284
|
+
const replacement = input.replacements[id];
|
|
285
|
+
if (
|
|
286
|
+
replacement !== null &&
|
|
287
|
+
(replacement === input.id ||
|
|
288
|
+
!permitted(id, replacement) ||
|
|
289
|
+
(replacement !== "github" &&
|
|
290
|
+
!getSource(replacement)?.models(id === "codex" ? "gpt" : "claude").length))
|
|
291
|
+
)
|
|
292
|
+
throw accessError("ccdx_connections_action_invalid");
|
|
293
|
+
next.clients[id].source = replacement;
|
|
294
|
+
next.clients[id].history_isolation = true;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (!input.enabled)
|
|
298
|
+
for (const id of CLIENT_IDS) {
|
|
299
|
+
const c = next.clients[id];
|
|
300
|
+
if (!Array.isArray(c.deployments)) continue;
|
|
301
|
+
const had = c.deployments.some(item => item.source === input.id);
|
|
302
|
+
if (!had) continue;
|
|
303
|
+
c.deployments = c.deployments.filter(item => item.source !== input.id);
|
|
304
|
+
if (input.replacements && Object.hasOwn(input.replacements, id)) {
|
|
305
|
+
const replacement = input.replacements[id];
|
|
306
|
+
if (!replacement) c.deployments = [];
|
|
307
|
+
else if (!c.deployments.some(item => item.source === replacement)) {
|
|
308
|
+
const model = sourceModels(getSource(replacement), id)[0];
|
|
309
|
+
if (model) c.deployments.push({ source: replacement, model: model.id });
|
|
310
|
+
}
|
|
311
|
+
c.default_model = c.deployments.find(item => item.source === replacement)
|
|
312
|
+
? deploymentModelId(replacement, c.deployments.find(item => item.source === replacement).model)
|
|
313
|
+
: null;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (input.id.startsWith("api_")) next.apis.find(api => api.id === input.id).enabled = input.enabled;
|
|
317
|
+
else next.providers[input.id] = input.enabled;
|
|
318
|
+
save(next);
|
|
319
|
+
}
|
|
320
|
+
return { removeApi, prepareApiSave, applyClientSelection, applyProviderEnabled };
|
|
321
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { accessError } from "./access-settings.mjs";
|
|
2
|
+
import { gatewayClient } from "./client-gateway-keys.mjs";
|
|
3
|
+
import { createClaudeGatewayHandler } from "./claude-gateway.mjs";
|
|
4
|
+
import { codexClientCatalog, codexSourceCatalog } from "./clients/codex-models.mjs";
|
|
5
|
+
import { currentRequestContext } from "./request-context.mjs";
|
|
6
|
+
|
|
7
|
+
export function createConnectionRouting({ legacy, legacyClaudeAccess, isManaged, getSettings, binding, plan, permitted, getSource,
|
|
8
|
+
resolveDeployment, codexAccessMode, codexRequiresKey, requireCodexKey, identities, track, guardConversation, messagesTransfer,
|
|
9
|
+
acquireClaudeRequest, privacy, env, log, lastClients }) {
|
|
10
|
+
const handlers = new Map();
|
|
11
|
+
function requireSource(clientId, id) {
|
|
12
|
+
if (!permitted(clientId, id)) throw accessError("ccdx_client_source_disabled", 503);
|
|
13
|
+
if (id === "m365" && legacy.snapshot().m365.account_change_in_flight)
|
|
14
|
+
throw accessError("m365_account_busy", 409);
|
|
15
|
+
const source = getSource(id);
|
|
16
|
+
if (!source) throw accessError("ccdx_api_not_ready", 503);
|
|
17
|
+
return source;
|
|
18
|
+
}
|
|
19
|
+
function validateConversation(body, headers, source, signal, client = "codex") {
|
|
20
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) return;
|
|
21
|
+
if (
|
|
22
|
+
isManaged() &&
|
|
23
|
+
(binding(client).history_isolation || source?.id.startsWith("api_") || client === "claude_app")
|
|
24
|
+
) {
|
|
25
|
+
const transfer =
|
|
26
|
+
client === "claude_app"
|
|
27
|
+
? {
|
|
28
|
+
profiles: [...new Set(plan(client).models.map(model => model.source_id))].map(id =>
|
|
29
|
+
getSource(id).profileId("messages")
|
|
30
|
+
),
|
|
31
|
+
prepare: input =>
|
|
32
|
+
messagesTransfer.prepare({
|
|
33
|
+
...input,
|
|
34
|
+
countTokens: currentRequestContext()?.route === "messages_count_tokens",
|
|
35
|
+
}),
|
|
36
|
+
}
|
|
37
|
+
: undefined;
|
|
38
|
+
return guardConversation(
|
|
39
|
+
client,
|
|
40
|
+
source.profileId(client === "codex" ? "responses" : "messages"),
|
|
41
|
+
body,
|
|
42
|
+
headers,
|
|
43
|
+
identities()[client],
|
|
44
|
+
transfer,
|
|
45
|
+
signal
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { requireSource, methods: {
|
|
50
|
+
current(req) {
|
|
51
|
+
if (!isManaged()) return legacy.current();
|
|
52
|
+
const mode = codexAccessMode();
|
|
53
|
+
if (mode === "paused" || mode === "disabled") throw accessError("ccdx_client_source_disabled", 503);
|
|
54
|
+
const c = binding("codex"),
|
|
55
|
+
compiled = plan("codex");
|
|
56
|
+
if (Array.isArray(c.deployments) && req?.method === "GET") {
|
|
57
|
+
if (codexRequiresKey()) requireCodexKey(req);
|
|
58
|
+
return {
|
|
59
|
+
id: "connections",
|
|
60
|
+
registry: { models: { data: compiled.models.map(model => ({ ...model, id: model.public_id })) } },
|
|
61
|
+
client: {
|
|
62
|
+
listModels: async () => ({
|
|
63
|
+
status: 200,
|
|
64
|
+
body: JSON.stringify({
|
|
65
|
+
data: compiled.models.map(model => ({ ...model, id: model.public_id })),
|
|
66
|
+
}),
|
|
67
|
+
}),
|
|
68
|
+
},
|
|
69
|
+
codexCatalog: codexClientCatalog(compiled, () => getSource("github")?.registry?.models),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(c.deployments) && compiled.defaultUnavailable) {
|
|
73
|
+
if (req && codexRequiresKey()) requireCodexKey(req);
|
|
74
|
+
// Admission uses the common handler; resolveCodex must resolve the
|
|
75
|
+
// explicitly requested model before any provider request is opened.
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const source = requireSource(
|
|
79
|
+
"codex",
|
|
80
|
+
Array.isArray(c.deployments) ? compiled.routes.get(compiled.defaultModel)?.source : c.source
|
|
81
|
+
);
|
|
82
|
+
if (source?.id.startsWith("api_") && req) requireCodexKey(req);
|
|
83
|
+
return req?.method === "GET" && source?.configuredModels && source.id.startsWith("api_")
|
|
84
|
+
? { ...source, codexCatalog: codexSourceCatalog(source) }
|
|
85
|
+
: source;
|
|
86
|
+
},
|
|
87
|
+
hasModelBindings(client = "codex") {
|
|
88
|
+
return isManaged() && Array.isArray(binding(client).deployments);
|
|
89
|
+
},
|
|
90
|
+
resolveCodex(body, req) {
|
|
91
|
+
if (!isManaged()) return null;
|
|
92
|
+
const mode = codexAccessMode();
|
|
93
|
+
if (mode === "paused" || mode === "disabled") throw accessError("ccdx_client_source_disabled", 503);
|
|
94
|
+
if (!Array.isArray(binding("codex").deployments)) return null;
|
|
95
|
+
if (codexRequiresKey()) requireCodexKey(req);
|
|
96
|
+
const requested =
|
|
97
|
+
body.model === "codex-auto-review"
|
|
98
|
+
? binding("codex").auto_review_model || plan("codex").defaultModel
|
|
99
|
+
: body.model;
|
|
100
|
+
if (body.model === "codex-auto-review") {
|
|
101
|
+
delete body.service_tier;
|
|
102
|
+
if (binding("codex").auto_review_effort)
|
|
103
|
+
body.reasoning = { ...body.reasoning, effort: binding("codex").auto_review_effort };
|
|
104
|
+
}
|
|
105
|
+
const record = resolveDeployment("codex", requested),
|
|
106
|
+
context = currentRequestContext();
|
|
107
|
+
if (context) {
|
|
108
|
+
context.finishProvider?.();
|
|
109
|
+
context.finishProvider = track(record.provider);
|
|
110
|
+
context.provider = record.provider.id;
|
|
111
|
+
context.streamPerformance?.setProvider?.(record.provider.id);
|
|
112
|
+
Object.assign(context, this.historyContext(req.headers, record.provider));
|
|
113
|
+
}
|
|
114
|
+
body.model = record.upstreamModel;
|
|
115
|
+
return record.provider;
|
|
116
|
+
},
|
|
117
|
+
track,
|
|
118
|
+
validateConversation,
|
|
119
|
+
historyContext(headers, target) {
|
|
120
|
+
if (target.id === "connections") return {};
|
|
121
|
+
if (!target.id.startsWith("api_")) return legacy.historyContext(headers, target);
|
|
122
|
+
// Opaque Responses state never transfers to another API/key/configuration.
|
|
123
|
+
return { historyNamespace: target.profileId() };
|
|
124
|
+
},
|
|
125
|
+
async handleClaude(req, res) {
|
|
126
|
+
if (!isManaged()) {
|
|
127
|
+
if (!legacyClaudeAccess) throw accessError("ccdx_client_source_disabled", 503);
|
|
128
|
+
return legacy.handleClaude(req, res);
|
|
129
|
+
}
|
|
130
|
+
if (!getSettings().claude_private_keys) {
|
|
131
|
+
if (["claude_app", "claude_code"].every(id => permitted(id, "m365") && binding(id).source === "m365"))
|
|
132
|
+
return legacy.handleClaude(req, res);
|
|
133
|
+
throw accessError("ccdx_client_source_disabled", 503);
|
|
134
|
+
}
|
|
135
|
+
const id = gatewayClient(req.headers, identities());
|
|
136
|
+
if (!id || id === "codex") throw accessError("ccdx_client_credential_required", 401);
|
|
137
|
+
const compiled = plan(id);
|
|
138
|
+
if (!binding(id).enabled || !compiled.models.length)
|
|
139
|
+
throw accessError("ccdx_client_source_disabled", 503);
|
|
140
|
+
// A request resolves once after its bounded body read. Its source and
|
|
141
|
+
// capability snapshot stay pinned while catalogs/configuration change.
|
|
142
|
+
const catalog = { id: "connections", configuredModels: true, models: () => compiled.models };
|
|
143
|
+
let cached = handlers.get(id);
|
|
144
|
+
if (cached?.compiled !== compiled) {
|
|
145
|
+
const handler = createClaudeGatewayHandler({
|
|
146
|
+
provider: catalog,
|
|
147
|
+
acquireRequest: acquireClaudeRequest,
|
|
148
|
+
apiKey: identities()[id],
|
|
149
|
+
privacy,
|
|
150
|
+
privacyClient: id,
|
|
151
|
+
env,
|
|
152
|
+
log,
|
|
153
|
+
performance: legacy.claudePerformance,
|
|
154
|
+
desktopAliases: id === "claude_app",
|
|
155
|
+
messagesSystemMode: id === "claude_app" ? "legacy" : "passthrough",
|
|
156
|
+
resolveRequest(body) {
|
|
157
|
+
const record = resolveDeployment(id, body.model),
|
|
158
|
+
finish = track(record.provider);
|
|
159
|
+
return {
|
|
160
|
+
provider: record.provider,
|
|
161
|
+
model: record.upstreamModel,
|
|
162
|
+
capability: record.model.capability,
|
|
163
|
+
finish,
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
validateConversation: (body, headers, source, signal) => validateConversation(body, headers, source, signal, id),
|
|
167
|
+
onRequest() {
|
|
168
|
+
lastClients[id] = new Date().toISOString();
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
cached = { compiled, handler };
|
|
172
|
+
handlers.set(id, cached);
|
|
173
|
+
}
|
|
174
|
+
const headers = req.headers;
|
|
175
|
+
req.headers = { ...headers, "x-ccdx-client": id === "claude_app" ? "claude-app" : "claude-code" };
|
|
176
|
+
try {
|
|
177
|
+
return await cached.handler(req, res);
|
|
178
|
+
} finally {
|
|
179
|
+
req.headers = headers;
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
} };
|
|
183
|
+
}
|