codex-copilot-dx 1.3.19 → 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/README.md +1 -0
- package/bin/cli.mjs +32 -3
- package/docs/ARCHITECTURE.md +11 -1
- package/docs/REFERENCE.md +9 -2
- package/package.json +1 -1
- package/src/access-settings.mjs +48 -0
- package/src/adapter.mjs +3 -3
- package/src/claude-capability-config.mjs +6 -0
- package/src/claude-gateway.mjs +78 -8
- package/src/client-versions.mjs +139 -0
- package/src/codex-client-doctor.mjs +3 -1
- package/src/codex-model-catalog.mjs +33 -10
- 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 +44 -1327
- package/src/connection-view.mjs +320 -0
- package/src/dashboard/console.js +17 -3
- package/src/dashboard/context.js +2 -1
- package/src/dashboard/features.js +8 -4
- package/src/dashboard/index.html +7 -3
- package/src/dashboard/language.js +5 -1
- package/src/dashboard/tools.js +1 -0
- package/src/doctor.mjs +8 -0
- package/src/local-tools.mjs +6 -3
- package/src/native-prediction-windows.mjs +8 -0
- package/src/observability.mjs +2 -1
- package/src/privacy-stream.mjs +10 -7
- package/src/privacy-transform.mjs +32 -7
- package/src/responses-handler.mjs +7 -1
- package/src/responses-proxy.mjs +125 -36
- package/src/service-control.mjs +7 -1
- package/src/source-lifecycle.mjs +61 -0
- package/src/stream-errors.mjs +29 -5
- package/src/stream.mjs +42 -5
|
@@ -260,6 +260,8 @@ export function createCodexModelCatalog({
|
|
|
260
260
|
const versionFailures = new Map();
|
|
261
261
|
const versionPending = new Map();
|
|
262
262
|
const catalogCache = new Map();
|
|
263
|
+
const exactCatalogCache = new Map();
|
|
264
|
+
const ambiguousVersions = new Set();
|
|
263
265
|
const catalogFailures = new Map();
|
|
264
266
|
const catalogPending = new Map();
|
|
265
267
|
let activeBinaryKeys = new Set();
|
|
@@ -280,8 +282,8 @@ export function createCodexModelCatalog({
|
|
|
280
282
|
const promise = (async () => {
|
|
281
283
|
try {
|
|
282
284
|
const stdout = await runCodexCommand(execFileFn, candidate.binaryPath, ["--version"], commandOptions);
|
|
283
|
-
const version =
|
|
284
|
-
if (!version) throw new Error("Invalid Codex version output");
|
|
285
|
+
const version = parsedCodexVersion(stdout);
|
|
286
|
+
if (!wholeSemver(version)) throw new Error("Invalid Codex version output");
|
|
285
287
|
if (activeBinaryKeys.has(candidate.key)) {
|
|
286
288
|
versionCache.set(candidate.key, version);
|
|
287
289
|
versionFailures.delete(candidate.key);
|
|
@@ -313,8 +315,10 @@ export function createCodexModelCatalog({
|
|
|
313
315
|
if (!rawVersion) return installed[0];
|
|
314
316
|
const expectedVersion = wholeSemver(rawVersion);
|
|
315
317
|
if (!expectedVersion) return null;
|
|
318
|
+
const exact = rawVersion !== expectedVersion;
|
|
316
319
|
for (const candidate of installed) {
|
|
317
|
-
|
|
320
|
+
const version = await probeVersion(candidate);
|
|
321
|
+
if (version && (exact ? version === rawVersion : wholeSemver(version) === expectedVersion)) return { ...candidate, fullVersion: version, precision: exact ? "exact" : "core_only" };
|
|
318
322
|
}
|
|
319
323
|
return null;
|
|
320
324
|
}
|
|
@@ -356,14 +360,33 @@ export function createCodexModelCatalog({
|
|
|
356
360
|
}
|
|
357
361
|
|
|
358
362
|
async function load({ clientVersion = "" } = {}) {
|
|
363
|
+
const requested = String(clientVersion || "").trim(), core = wholeSemver(requested);
|
|
364
|
+
const exact = Boolean(core && requested !== core);
|
|
359
365
|
const candidate = await selectBinary(clientVersion);
|
|
360
|
-
|
|
361
|
-
|
|
366
|
+
let catalog = candidate ? await loadCatalog(candidate) : null;
|
|
367
|
+
let app = candidate?.binaryPath.match(/(?:^|\/)(ChatGPT|Codex)\.app\//)?.[1];
|
|
368
|
+
let source = catalog ? "bundled" : "unavailable";
|
|
369
|
+
if (candidate && catalog && exact && !ambiguousVersions.has(requested)) {
|
|
370
|
+
const previous = exactCatalogCache.get(requested);
|
|
371
|
+
if (previous && previous.key !== candidate.key) {
|
|
372
|
+
exactCatalogCache.delete(requested);ambiguousVersions.add(requested);
|
|
373
|
+
if (ambiguousVersions.size > 8) ambiguousVersions.delete(ambiguousVersions.values().next().value);
|
|
374
|
+
} else if (!previous) try {
|
|
375
|
+
const stats = await statFn(candidate.binaryPath);
|
|
376
|
+
if (stats?.isFile?.() && binaryCacheKey(candidate.binaryPath, stats) === candidate.key) {
|
|
377
|
+
exactCatalogCache.set(requested, { key: candidate.key, catalog, app: app || null });
|
|
378
|
+
while (exactCatalogCache.size > 2) exactCatalogCache.delete(exactCatalogCache.keys().next().value);
|
|
379
|
+
}
|
|
380
|
+
} catch { /* A changing App cannot supply a verified retained catalog. */ }
|
|
381
|
+
} else if (!candidate && exact && exactCatalogCache.has(requested) && !ambiguousVersions.has(requested)) {
|
|
382
|
+
const previous = exactCatalogCache.get(requested);catalog = previous.catalog;app = previous.app;source = "cached_bundled";
|
|
383
|
+
}
|
|
362
384
|
lastObservation = {
|
|
363
|
-
client_version:
|
|
364
|
-
application: app ? `${app}.app` : candidate ? "custom binary" : null,
|
|
365
|
-
source
|
|
366
|
-
matched: Boolean(catalog &&
|
|
385
|
+
client_version: core || null,
|
|
386
|
+
application: app ? `${app}.app` : candidate || source === "cached_bundled" ? "custom binary" : null,
|
|
387
|
+
source,
|
|
388
|
+
matched: Boolean(catalog && core),
|
|
389
|
+
match_precision: catalog && core ? exact ? "exact" : "core_only" : null,
|
|
367
390
|
observed_at: new Date(now()).toISOString(),
|
|
368
391
|
};
|
|
369
392
|
return catalog;
|
|
@@ -371,7 +394,7 @@ export function createCodexModelCatalog({
|
|
|
371
394
|
|
|
372
395
|
return { load, snapshot: () => ({
|
|
373
396
|
last_request: lastObservation ? { ...lastObservation } : null,
|
|
374
|
-
observed_versions: [...new Set(versionCache.values())].slice(0, 8),
|
|
397
|
+
observed_versions: [...new Set([...versionCache.values()].map(wholeSemver).filter(Boolean))].slice(0, 8),
|
|
375
398
|
client_cache_verified: false,
|
|
376
399
|
}) };
|
|
377
400
|
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { CLIENT_IDS } from "./access-settings.mjs";
|
|
2
|
+
import { claudeSharedCapabilityPlan, claudeSupportForSync, syncClaudeCapabilities } from "./claude-capability-config.mjs";
|
|
3
|
+
|
|
4
|
+
export function createConnectionClientSync({ options, corePort, configureCodex, configureClaude, restoreClaude, inspectDesktop,
|
|
5
|
+
binding, plan, permitted, getSource, identities, codexAccessMode, capabilitySupport, clientVersionsSnapshot, setup, credentialStates,
|
|
6
|
+
isManaged, isClosed, getSettings }) {
|
|
7
|
+
let sharedSync = null, syncFlight = null, syncAgain = false;
|
|
8
|
+
const syncTargets = new Set(), manualTargets = new Set();
|
|
9
|
+
function capabilitySupportForSync() {
|
|
10
|
+
try {
|
|
11
|
+
return claudeSupportForSync(capabilitySupport, clientVersionsSnapshot?.());
|
|
12
|
+
} catch { /* Optional observations cannot change routing or interrupt synchronization. */ }
|
|
13
|
+
return capabilitySupport;
|
|
14
|
+
}
|
|
15
|
+
async function syncClients(ids = CLIENT_IDS, { manual = false } = {}) {
|
|
16
|
+
if (!ids.length) return;
|
|
17
|
+
for (const id of ids) {
|
|
18
|
+
syncTargets.add(id);
|
|
19
|
+
if (manual) manualTargets.add(id);
|
|
20
|
+
}
|
|
21
|
+
syncAgain = true;
|
|
22
|
+
if (syncFlight) return syncFlight;
|
|
23
|
+
syncFlight = (async () => {
|
|
24
|
+
do {
|
|
25
|
+
syncAgain = false;
|
|
26
|
+
const targets = new Set(syncTargets);
|
|
27
|
+
syncTargets.clear();
|
|
28
|
+
const requested = new Set(manualTargets);
|
|
29
|
+
manualTargets.clear();
|
|
30
|
+
await performClientSync(targets, requested);
|
|
31
|
+
} while (syncAgain && !isClosed());
|
|
32
|
+
})();
|
|
33
|
+
try {
|
|
34
|
+
return await syncFlight;
|
|
35
|
+
} finally {
|
|
36
|
+
syncFlight = null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function syncCodexClient(codex, compiledCodex, requested, mode) {
|
|
40
|
+
const explicit = Array.isArray(codex.deployments),
|
|
41
|
+
native = mode === "native";
|
|
42
|
+
try {
|
|
43
|
+
if (mode === "paused" || mode === "disabled") {
|
|
44
|
+
if (mode === "disabled" || codex.auto_sync !== false || requested.has("codex"))
|
|
45
|
+
configureCodex({ ...options, restore: true });
|
|
46
|
+
setup.codex = mode;
|
|
47
|
+
} else if (codex.auto_sync === false && !requested.has("codex")) setup.codex = "manual_sync";
|
|
48
|
+
else if (codex.enabled && compiledCodex.defaultUnavailable && !native)
|
|
49
|
+
setup.codex = "default_unavailable";
|
|
50
|
+
else {
|
|
51
|
+
setup.codex = configureCodex({
|
|
52
|
+
...options,
|
|
53
|
+
...(codex.enabled &&
|
|
54
|
+
(native || (explicit && codex.deployments.length) || codex.source?.startsWith("api_"))
|
|
55
|
+
? {
|
|
56
|
+
baseUrl: `http://127.0.0.1:${corePort}/v1`,
|
|
57
|
+
native,
|
|
58
|
+
...(native ? { nativeModels: compiledCodex.models } : {}),
|
|
59
|
+
...(!native ? { apiKey: identities().codex } : {}),
|
|
60
|
+
...(explicit || !native
|
|
61
|
+
? {
|
|
62
|
+
model:
|
|
63
|
+
compiledCodex.defaultModel ||
|
|
64
|
+
(!compiledCodex.defaultUnavailable ? codex.model : undefined),
|
|
65
|
+
}
|
|
66
|
+
: {}),
|
|
67
|
+
}
|
|
68
|
+
: { restore: true }),
|
|
69
|
+
}).state;
|
|
70
|
+
if (codex.enabled && compiledCodex.defaultUnavailable && setup.codex === "configured")
|
|
71
|
+
setup.codex = "default_unavailable";
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
setup.codex = "configuration_unavailable";
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function prepareClaudeSync(id, requested) {
|
|
78
|
+
const selected = binding(id),
|
|
79
|
+
compiled = plan(id);
|
|
80
|
+
const source = selected.source && permitted(id, selected.source) ? getSource(selected.source) : null;
|
|
81
|
+
try {
|
|
82
|
+
if (selected.enabled && compiled.defaultUnavailable) {
|
|
83
|
+
setup[id] = "default_unavailable";
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
if (!selected.enabled || !compiled.models.length) {
|
|
87
|
+
const result = restoreClaude({ ...options, client: id });
|
|
88
|
+
setup[id] = result.preserved ? "preserved" : "not_configured";
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
if (selected.auto_sync === false && !requested.has(id)) {
|
|
92
|
+
setup[id] = "manual_sync";
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
return { selected, compiled, source };
|
|
96
|
+
} catch {
|
|
97
|
+
setup[id] = "configuration_unavailable";
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function writeClaudeSync(id, { selected, compiled, source }, desktopManaged) {
|
|
102
|
+
const m365Only = compiled.models.every(model => model.source_id === "m365");
|
|
103
|
+
const result = configureClaude({
|
|
104
|
+
...options,
|
|
105
|
+
client: id,
|
|
106
|
+
profileName: "CCDX",
|
|
107
|
+
desktopAliases: id === "claude_app" && source?.id.startsWith("api_"),
|
|
108
|
+
disableExperimentalBetas: m365Only,
|
|
109
|
+
desktopManaged: id === "claude_app" ? desktopManaged : false,
|
|
110
|
+
baseUrl: `http://127.0.0.1:${corePort}/anthropic`,
|
|
111
|
+
apiKey: identities()[id],
|
|
112
|
+
models: compiled.models,
|
|
113
|
+
defaultModel: Array.isArray(selected.deployments) ? compiled.defaultModel : undefined,
|
|
114
|
+
});
|
|
115
|
+
setup[id] = result[id === "claude_app" ? "app" : "code"] || "not_configured";
|
|
116
|
+
if (id === "claude_app" && result.app_credential) credentialStates[id] = result.app_credential;
|
|
117
|
+
}
|
|
118
|
+
function syncSharedClaudeCapabilities(targets) {
|
|
119
|
+
if (["claude_app", "claude_code"].some(id => setup[id] === "default_unavailable")) {
|
|
120
|
+
sharedSync = {
|
|
121
|
+
...sharedSync,
|
|
122
|
+
state: "partially_preserved",
|
|
123
|
+
limitations: [{ code: "default_model_unavailable" }],
|
|
124
|
+
};
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (!["claude_app", "claude_code"].some(id => targets.has(id))) return;
|
|
128
|
+
if (
|
|
129
|
+
["claude_app", "claude_code"].some(
|
|
130
|
+
id => !targets.has(id) && !setup[id] && binding(id).enabled && plan(id).models.length
|
|
131
|
+
)
|
|
132
|
+
) {
|
|
133
|
+
sharedSync = { state: "partially_preserved", limitations: [{ code: "other_client_not_checked" }] };
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const shared = claudeSharedCapabilityPlan({
|
|
137
|
+
app: setup.claude_app === "configured" ? plan("claude_app").models : [],
|
|
138
|
+
code: setup.claude_code === "configured" ? plan("claude_code").models : [],
|
|
139
|
+
support: capabilitySupportForSync(),
|
|
140
|
+
});
|
|
141
|
+
if (["claude_app", "claude_code"].some(id => setup[id] === "manual_sync")) {
|
|
142
|
+
sharedSync = {
|
|
143
|
+
...shared,
|
|
144
|
+
state: "partially_preserved",
|
|
145
|
+
limitations: [...shared.limitations, { code: "manual_sync" }],
|
|
146
|
+
};
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
sharedSync = { ...shared, ...syncClaudeCapabilities(shared, options) };
|
|
151
|
+
} catch {
|
|
152
|
+
sharedSync = { state: "configuration_unavailable", limitations: shared.limitations };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async function performClientSync(targets, requested = new Set()) {
|
|
156
|
+
if (!isManaged() || isClosed()) return;
|
|
157
|
+
for (const id of targets) delete credentialStates[id];
|
|
158
|
+
if (options.preserveClientConfiguration?.()) {
|
|
159
|
+
for (const id of targets) setup[id] = "preserved";
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const codex = binding("codex"),
|
|
163
|
+
compiledCodex = plan("codex"),
|
|
164
|
+
mode = codexAccessMode();
|
|
165
|
+
if (targets.has("codex")) syncCodexClient(codex, compiledCodex, requested, mode);
|
|
166
|
+
if (!getSettings().claude_private_keys || !["claude_app", "claude_code"].some(id => targets.has(id))) return;
|
|
167
|
+
let desktopManaged;
|
|
168
|
+
for (const id of ["claude_app", "claude_code"]) {
|
|
169
|
+
if (!targets.has(id)) continue;
|
|
170
|
+
const sync = prepareClaudeSync(id, requested);
|
|
171
|
+
if (!sync) continue;
|
|
172
|
+
try {
|
|
173
|
+
if (id === "claude_app") desktopManaged ??= await inspectDesktop(options);
|
|
174
|
+
writeClaudeSync(id, sync, desktopManaged);
|
|
175
|
+
} catch {
|
|
176
|
+
setup[id] = "configuration_unavailable";
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
syncSharedClaudeCapabilities(targets);
|
|
180
|
+
}
|
|
181
|
+
return { syncClients, capabilitySupportForSync, shared: () => sharedSync, busy: () => Boolean(syncFlight), pending: () => syncFlight };
|
|
182
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { prepareSourceAccess, accessError, CLIENT_IDS } from "./access-settings.mjs";
|
|
2
|
+
import { publicModelCatalog } from "./provider-metadata.mjs";
|
|
3
|
+
import { createConnectionPolicy } from "./connection-policy.mjs";
|
|
4
|
+
|
|
5
|
+
const validObject = (value, keys) =>
|
|
6
|
+
value &&
|
|
7
|
+
typeof value === "object" &&
|
|
8
|
+
!Array.isArray(value) &&
|
|
9
|
+
Object.keys(value).every(key => keys.includes(key));
|
|
10
|
+
export function createConnectionManagement({ binding, catalogRefresh, save, discoveries, getSettings, env,
|
|
11
|
+
imageModels, enabled, getSource, legacy, capabilitySupport, capabilitySupportForSync, plan, snapshot, options, permitted, sourceImpact,
|
|
12
|
+
isManaged, isClosed, clientRevision, editable, syncClients, clientAccessChanged, setup, secrets, lifecycle, retire, memoryModel, enqueue, catalogVisibility }) {
|
|
13
|
+
const { removeApi, prepareApiSave, applyClientSelection, applyProviderEnabled } = createConnectionPolicy({
|
|
14
|
+
validObject, binding, catalogRefresh, save, discoveries, getSettings, env, imageModels, enabled, getSource, legacy,
|
|
15
|
+
capabilitySupport, capabilitySupportForSync, plan, snapshot, options, permitted, sourceImpact });
|
|
16
|
+
|
|
17
|
+
async function change(input) {
|
|
18
|
+
if (isClosed()) throw accessError("ccdx_connections_closed", 503);
|
|
19
|
+
if (
|
|
20
|
+
!validObject(input, [
|
|
21
|
+
"operation",
|
|
22
|
+
"id",
|
|
23
|
+
"config",
|
|
24
|
+
"key",
|
|
25
|
+
"allowed",
|
|
26
|
+
"source",
|
|
27
|
+
"enabled",
|
|
28
|
+
"replacements",
|
|
29
|
+
"impact_revision",
|
|
30
|
+
"model",
|
|
31
|
+
"auto_review_model",
|
|
32
|
+
"auto_review_effort",
|
|
33
|
+
"deployments",
|
|
34
|
+
"default_model",
|
|
35
|
+
"auto_sync",
|
|
36
|
+
"image_model",
|
|
37
|
+
"client_revision",
|
|
38
|
+
])
|
|
39
|
+
)
|
|
40
|
+
throw accessError("ccdx_connections_action_invalid");
|
|
41
|
+
if (
|
|
42
|
+
["client", "preview_client", "sync_client", "client_state", "image_model"].includes(input.operation) &&
|
|
43
|
+
input.client_revision !== undefined &&
|
|
44
|
+
(!CLIENT_IDS.includes(input.id) || input.client_revision !== clientRevision(input.id))
|
|
45
|
+
)
|
|
46
|
+
throw accessError("ccdx_connections_conflict", 409);
|
|
47
|
+
if (
|
|
48
|
+
!["client", "preview_client", "sync_client", "client_state", "image_model"].includes(input.operation) &&
|
|
49
|
+
input.impact_revision !== undefined &&
|
|
50
|
+
input.impact_revision !== sourceImpact(input.id).revision
|
|
51
|
+
)
|
|
52
|
+
throw accessError("ccdx_connections_conflict", 409);
|
|
53
|
+
if (input.operation === "image_model") {
|
|
54
|
+
if (!validObject(input, ["operation", "id", "model", "client_revision"]) || input.id !== "codex"
|
|
55
|
+
|| !imageModels().models.some(model => model.id === input.model)) throw accessError("ccdx_api_model_unavailable");
|
|
56
|
+
const next = editable();next.clients.codex.image_model = input.model;save(next);
|
|
57
|
+
await syncClients(["codex"]);
|
|
58
|
+
return snapshot();
|
|
59
|
+
}
|
|
60
|
+
if (input.operation === "sync_client") {
|
|
61
|
+
if (
|
|
62
|
+
!validObject(input, ["operation", "id", "client_revision"]) ||
|
|
63
|
+
!CLIENT_IDS.includes(input.id) ||
|
|
64
|
+
typeof input.client_revision !== "string"
|
|
65
|
+
)
|
|
66
|
+
throw accessError("ccdx_connections_action_invalid");
|
|
67
|
+
await syncClients([input.id], { manual: true });
|
|
68
|
+
return snapshot();
|
|
69
|
+
}
|
|
70
|
+
if (input.operation === "client_state") {
|
|
71
|
+
if (
|
|
72
|
+
!validObject(input, ["operation", "id", "enabled", "client_revision"]) ||
|
|
73
|
+
!CLIENT_IDS.includes(input.id) ||
|
|
74
|
+
typeof input.enabled !== "boolean" ||
|
|
75
|
+
typeof input.client_revision !== "string"
|
|
76
|
+
)
|
|
77
|
+
throw accessError("ccdx_connections_action_invalid");
|
|
78
|
+
const next = editable();
|
|
79
|
+
next.clients[input.id].enabled = input.enabled;
|
|
80
|
+
save(next);
|
|
81
|
+
await syncClients([input.id]);
|
|
82
|
+
if (input.id === "codex") await clientAccessChanged?.(input.enabled);
|
|
83
|
+
return snapshot();
|
|
84
|
+
}
|
|
85
|
+
const next = editable();
|
|
86
|
+
let freshRef, previousRef;
|
|
87
|
+
if (input.operation === "source_access") {
|
|
88
|
+
const { targets, migrateClaude } = prepareSourceAccess(input, next);
|
|
89
|
+
save(next);
|
|
90
|
+
await syncClients([...targets]);
|
|
91
|
+
const retained = migrateClaude
|
|
92
|
+
? [...targets].filter(
|
|
93
|
+
id => id !== "codex" && next.clients[id].enabled && next.clients[id].source === "m365"
|
|
94
|
+
)
|
|
95
|
+
: [];
|
|
96
|
+
return {
|
|
97
|
+
...snapshot(),
|
|
98
|
+
client_refresh: retained.filter(id => setup[id] === "configured"),
|
|
99
|
+
client_review: retained.filter(id =>
|
|
100
|
+
["preserved", "partially_preserved", "manual_sync", "configuration_unavailable"].includes(setup[id])
|
|
101
|
+
),
|
|
102
|
+
};
|
|
103
|
+
} else if (input.operation === "remove_api") {
|
|
104
|
+
previousRef = removeApi(input, next);
|
|
105
|
+
} else if (input.operation === "save_api") {
|
|
106
|
+
({ input, freshRef, previousRef } = prepareApiSave(input, next));
|
|
107
|
+
try {
|
|
108
|
+
if (freshRef) await secrets.set(freshRef, input.key);
|
|
109
|
+
save(next);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (freshRef) await secrets.remove(freshRef).catch(() => {});
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
} else if (["client", "preview_client"].includes(input.operation)) {
|
|
115
|
+
const preview = applyClientSelection(input, next);
|
|
116
|
+
if (input.operation === "preview_client") return preview;
|
|
117
|
+
} else if (input.operation === "provider") {
|
|
118
|
+
applyProviderEnabled(input, next);
|
|
119
|
+
if (input.id === "m365") {
|
|
120
|
+
if (input.enabled) await legacy.action({ action: "on" });
|
|
121
|
+
else {
|
|
122
|
+
const source = await legacy.pauseM365();
|
|
123
|
+
if (source) retire(source);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
} else throw accessError("ccdx_connections_action_invalid");
|
|
127
|
+
await syncClients();
|
|
128
|
+
if (input.operation === "client" && input.id === "codex")
|
|
129
|
+
await clientAccessChanged?.(binding("codex").enabled);
|
|
130
|
+
if (previousRef && (freshRef || input.config?.key_env || input.operation === "remove_api")) {
|
|
131
|
+
lifecycle.retireSecret(input.id, previousRef);
|
|
132
|
+
}
|
|
133
|
+
return snapshot();
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
async manageConnections(input) {
|
|
137
|
+
if (!input) {
|
|
138
|
+
if (binding("codex").enabled) void memoryModel.refresh();
|
|
139
|
+
return snapshot();
|
|
140
|
+
}
|
|
141
|
+
if (validObject(input, ["operation", "id"]) && input.operation === "source_catalog") {
|
|
142
|
+
if (!["github", "m365", ...getSettings().apis.map(api => api.id)].includes(input.id))
|
|
143
|
+
throw accessError("ccdx_connections_action_invalid");
|
|
144
|
+
const source = getSource(input.id),
|
|
145
|
+
raw = source?.catalog?.() || source?.registry?.models?.data;
|
|
146
|
+
return catalogVisibility({
|
|
147
|
+
source: input.id,
|
|
148
|
+
loaded: Array.isArray(raw),
|
|
149
|
+
evidence: input.id.startsWith("api_") ? "configured" : "cached",
|
|
150
|
+
access_checked: false,
|
|
151
|
+
models: publicModelCatalog(raw || []).map(({ selectable, clients, ...model }) => model),
|
|
152
|
+
}, input.id);
|
|
153
|
+
}
|
|
154
|
+
if (validObject(input, ["operation", "id"]) && input.operation === "discover") {
|
|
155
|
+
const models = await catalogRefresh.refresh(input.id, true);
|
|
156
|
+
return { ...snapshot(), discovered: models };
|
|
157
|
+
}
|
|
158
|
+
if (validObject(input, ["operation", "id"]) && input.operation === "cancel_discovery") {
|
|
159
|
+
catalogRefresh.cancel(input.id);
|
|
160
|
+
return snapshot();
|
|
161
|
+
}
|
|
162
|
+
const result = await enqueue(() => change(input));
|
|
163
|
+
if (input.operation === "save_api") {
|
|
164
|
+
const id =
|
|
165
|
+
input.id || result.providers.find(api => !api.builtin && api.name === input.config.name)?.id;
|
|
166
|
+
if (enabled(id)) void catalogRefresh.refresh(id).catch(() => {});
|
|
167
|
+
} else if (input.operation === "provider" && input.enabled && input.id?.startsWith("api_"))
|
|
168
|
+
void catalogRefresh.refresh(input.id).catch(() => {});
|
|
169
|
+
return input.operation === "save_api" ? snapshot() : result;
|
|
170
|
+
},
|
|
171
|
+
async action(input) {
|
|
172
|
+
if (isManaged() && input.action === "auto-review" && Array.isArray(binding("codex").deployments)) {
|
|
173
|
+
const c = binding("codex"),
|
|
174
|
+
compiled = plan("codex");
|
|
175
|
+
if (!c.enabled || input.provider !== c.source) throw accessError("ccdx_client_source_mismatch", 409);
|
|
176
|
+
const record = [...compiled.routes.values()].find(
|
|
177
|
+
record => record.source === input.provider && record.upstreamModel === input.model
|
|
178
|
+
);
|
|
179
|
+
if (!record) throw accessError("ccdx_api_model_unavailable");
|
|
180
|
+
if (input.effort && !record.model.capability.values.reasoning_efforts?.includes(input.effort))
|
|
181
|
+
throw accessError("ccdx_api_effort_unavailable");
|
|
182
|
+
const next = editable();
|
|
183
|
+
next.clients.codex.auto_review_model = record.model.route_id;
|
|
184
|
+
next.clients.codex.auto_review_effort = input.effort || null;
|
|
185
|
+
save(next);
|
|
186
|
+
return this.snapshot();
|
|
187
|
+
}
|
|
188
|
+
if (
|
|
189
|
+
isManaged() &&
|
|
190
|
+
input.action === "auto-review" &&
|
|
191
|
+
input.provider &&
|
|
192
|
+
input.provider !== this.selection().codex
|
|
193
|
+
)
|
|
194
|
+
throw accessError("ccdx_client_source_mismatch", 409);
|
|
195
|
+
if (
|
|
196
|
+
isManaged() &&
|
|
197
|
+
input.action === "auto-review" &&
|
|
198
|
+
(!binding("codex").enabled || !binding("codex").source)
|
|
199
|
+
)
|
|
200
|
+
throw accessError("ccdx_client_source_disabled", 503);
|
|
201
|
+
if (isManaged() && ["on", "off", "remove"].includes(input.action)) {
|
|
202
|
+
if (input.action === "remove" && legacy.snapshot().m365.active_requests > 0)
|
|
203
|
+
throw accessError("ccdx_source_busy", 409);
|
|
204
|
+
const replacements = Object.fromEntries(
|
|
205
|
+
CLIENT_IDS.filter(id => binding(id).source === "m365" && binding(id).enabled).map(id => [id, null])
|
|
206
|
+
);
|
|
207
|
+
await this.manageConnections({
|
|
208
|
+
operation: "provider",
|
|
209
|
+
id: "m365",
|
|
210
|
+
enabled: input.action === "on",
|
|
211
|
+
replacements,
|
|
212
|
+
});
|
|
213
|
+
if (input.action === "remove") legacy.removeM365SavedProfile();
|
|
214
|
+
return this.snapshot();
|
|
215
|
+
}
|
|
216
|
+
if (isManaged() && input.action === "switch" && !binding("codex").enabled)
|
|
217
|
+
throw accessError("ccdx_client_source_disabled", 503);
|
|
218
|
+
const { history_isolation, ...clientInput } = binding("codex");
|
|
219
|
+
if (input.action === "switch" && input.provider?.startsWith("api_")) {
|
|
220
|
+
await this.manageConnections({
|
|
221
|
+
operation: "client",
|
|
222
|
+
id: "codex",
|
|
223
|
+
...clientInput,
|
|
224
|
+
deployments: null,
|
|
225
|
+
default_model: null,
|
|
226
|
+
source: input.provider,
|
|
227
|
+
model: input.model,
|
|
228
|
+
auto_review_model: input.auto_review_model,
|
|
229
|
+
auto_review_effort: input.auto_review_effort,
|
|
230
|
+
});
|
|
231
|
+
return this.snapshot();
|
|
232
|
+
}
|
|
233
|
+
if (input.action === "auto-review" && isManaged() && binding("codex").source !== "github") {
|
|
234
|
+
await this.manageConnections({
|
|
235
|
+
operation: "client",
|
|
236
|
+
id: "codex",
|
|
237
|
+
...clientInput,
|
|
238
|
+
auto_review_model: input.model,
|
|
239
|
+
auto_review_effort: input.effort,
|
|
240
|
+
});
|
|
241
|
+
return this.snapshot();
|
|
242
|
+
}
|
|
243
|
+
if (input.action === "auto-review" && isManaged() && legacy.selection().codex !== "github")
|
|
244
|
+
await legacy.action({ action: "switch", provider: "github" });
|
|
245
|
+
if (isManaged() && input.action === "switch" && !enabled(input.provider))
|
|
246
|
+
throw accessError("ccdx_client_source_disabled", 503);
|
|
247
|
+
const result = await legacy.action(input);
|
|
248
|
+
if (isManaged() && input.action === "switch") {
|
|
249
|
+
const next = editable();
|
|
250
|
+
if (next.clients.codex.source !== input.provider) next.clients.codex.history_isolation = true;
|
|
251
|
+
next.clients.codex.allowed = [...new Set([...next.clients.codex.allowed, input.provider])];
|
|
252
|
+
next.clients.codex.source = input.provider;
|
|
253
|
+
next.clients.codex.deployments = null;
|
|
254
|
+
next.clients.codex.default_model = null;
|
|
255
|
+
for (const key of ["model", "auto_review_model", "auto_review_effort"])
|
|
256
|
+
delete next.clients.codex[key];
|
|
257
|
+
if (input.provider === "m365") Object.assign(next.clients.codex, legacy.selection().m365);
|
|
258
|
+
save(next);
|
|
259
|
+
await syncClients(["codex"]);
|
|
260
|
+
}
|
|
261
|
+
return result;
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
}
|