codex-copilot-dx 1.3.20 → 1.3.22
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 +23 -2
- package/docs/REFERENCE.md +9 -3
- package/package.json +2 -1
- package/src/access-settings.mjs +48 -0
- package/src/claude-gateway.mjs +66 -4
- 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/copilot-responses-compat.mjs +9 -1
- package/src/messages-tool-schema.mjs +155 -0
- package/src/privacy-transform.mjs +32 -7
- package/src/providers/api-network.mjs +1 -1
- package/src/providers/api.mjs +27 -3
- package/src/providers/github.mjs +5 -4
- package/src/proxy-tunnel.mjs +17 -1
- package/src/responses-bridge.mjs +61 -5
- package/src/responses-chat-payload.mjs +6 -5
- package/src/responses-handler.mjs +13 -4
- package/src/responses-proxy.mjs +30 -9
- package/src/responses-tool-output.mjs +42 -0
- package/src/safe-upstream-error.mjs +8 -2
- package/src/source-lifecycle.mjs +61 -0
- package/src/stream-errors.mjs +29 -5
|
@@ -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
|
+
}
|
|
@@ -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
|
+
}
|