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.
@@ -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
+ }