codex-copilot-dx 0.9.40 → 0.10.0
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 +25 -4
- package/bin/cli.mjs +37 -11
- package/docs/ARCHITECTURE.md +84 -13
- package/docs/REFERENCE.md +207 -16
- package/package.json +4 -1
- package/src/adapter.mjs +98 -152
- package/src/auto-review-model.mjs +4 -3
- package/src/claude-client-config.mjs +217 -0
- package/src/claude-gateway.mjs +163 -0
- package/src/cli-m365.mjs +133 -0
- package/src/cli-models.mjs +1 -1
- package/src/cli-options.mjs +20 -6
- package/src/cli-status.mjs +23 -7
- package/src/codex-model-catalog.mjs +7 -2
- package/src/copilot-client.mjs +4 -0
- package/src/copilot-responses-compat.mjs +1 -1
- package/src/copilot-responses-policy.mjs +11 -64
- package/src/dashboard/index.html +14 -4
- package/src/dashboard/language.js +40 -2
- package/src/dashboard/status.js +26 -4
- package/src/dashboard/ui.css +2 -2
- package/src/dashboard/ui.js +16 -7
- package/src/dashboard/usage.js +3 -1
- package/src/dashboard-api.mjs +10 -7
- package/src/doctor-repair.mjs +2 -0
- package/src/doctor.mjs +18 -2
- package/src/http-transport.mjs +6 -3
- package/src/m365-settings.mjs +89 -0
- package/src/management-routes.mjs +118 -0
- package/src/native-prediction-windows.mjs +2 -7
- package/src/optional-routes.mjs +33 -0
- package/src/profile-model-runtime.mjs +4 -2
- package/src/provider-management.mjs +28 -0
- package/src/provider-runtime.mjs +179 -0
- package/src/provider-settings.mjs +32 -0
- package/src/providers/github.mjs +62 -0
- package/src/providers/m365-auth-helper.mjs +72 -0
- package/src/providers/m365-auth.mjs +105 -0
- package/src/providers/m365-messages-policy.mjs +20 -0
- package/src/providers/m365-network.mjs +73 -0
- package/src/providers/m365.mjs +238 -0
- package/src/response-failures.mjs +66 -5
- package/src/response-history.mjs +20 -3
- package/src/responses-bridge.mjs +1 -1
- package/src/responses-handler.mjs +40 -103
- package/src/responses-image-tools.mjs +61 -0
- package/src/responses-proxy.mjs +5 -5
- package/src/responses-request.mjs +25 -0
- package/src/stream-performance.mjs +42 -10
- package/src/usage-analysis.mjs +3 -2
- package/src/usage-store.mjs +41 -12
- package/src/usage.mjs +8 -2
- package/src/windows-http-network.mjs +122 -0
- package/src/windows-prediction-network.cs +13 -6
- package/src/windows-prediction-network.mjs +4 -108
- package/src/windows-process.mjs +8 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codex-copilot-dx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Use Codex App with GitHub Copilot",
|
|
5
5
|
"author": "Dale Xiao",
|
|
6
6
|
"type": "module",
|
|
@@ -53,5 +53,8 @@
|
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"sharp": "0.35.3",
|
|
55
55
|
"smol-toml": "1.8.0"
|
|
56
|
+
},
|
|
57
|
+
"optionalDependencies": {
|
|
58
|
+
"@azure/msal-node-runtime": "0.20.6"
|
|
56
59
|
}
|
|
57
60
|
}
|
package/src/adapter.mjs
CHANGED
|
@@ -8,30 +8,26 @@ import {
|
|
|
8
8
|
createRequestMetrics,
|
|
9
9
|
runtimeStatusPayload,
|
|
10
10
|
} from "./observability.mjs";
|
|
11
|
-
import { createRequestId, runWithRequestContext } from "./request-context.mjs";
|
|
11
|
+
import { createRequestId, runWithRequestContext, currentRequestContext } from "./request-context.mjs";
|
|
12
12
|
import { createStreamPerformanceMetrics } from "./stream-performance.mjs";
|
|
13
13
|
import { status } from "./status.mjs";
|
|
14
14
|
import { createTerminalActivityIndicator } from "./terminal-activity.mjs";
|
|
15
|
-
import { ADAPTER_HEALTH_PATH, ADAPTER_PREDICTION_FIX_PATH
|
|
15
|
+
import { ADAPTER_HEALTH_PATH, ADAPTER_PREDICTION_FIX_PATH } from "./running-adapter.mjs";
|
|
16
|
+
import { createManagementHandler, isManagementRoute } from "./management-routes.mjs";
|
|
17
|
+
import { createOptionalRoutes, isOptionalRoute, DASHBOARD_PATHS, DASHBOARD_API_PATHS } from "./optional-routes.mjs";
|
|
16
18
|
import { createResponsesCompactHandler, createResponsesHandler } from "./responses-handler.mjs";
|
|
17
19
|
import { createResponsesImagePressureController } from "./responses-image-pressure.mjs";
|
|
18
|
-
import { createResponseFailureDiagnostics, buildHttpFailureDetails } from "./response-failures.mjs";
|
|
19
|
-
import {
|
|
20
|
-
ADAPTER_CACHE_PATH,
|
|
21
|
-
cacheRuntimeSnapshot,
|
|
22
|
-
cleanRuntimeCaches,
|
|
23
|
-
setHistoryCacheLimit,
|
|
24
|
-
} from "./cache-control.mjs";
|
|
20
|
+
import { createResponseFailureDiagnostics, createHttpFailureDiagnostics, buildHttpFailureDetails } from "./response-failures.mjs";
|
|
21
|
+
import { ADAPTER_CACHE_PATH } from "./cache-control.mjs";
|
|
25
22
|
import { loadRuntimeConfig, parsePositiveInteger } from "./runtime-config.mjs";
|
|
26
23
|
import {
|
|
27
24
|
createRequestAbort,
|
|
28
25
|
logRequestFailure,
|
|
29
|
-
readJsonBody,
|
|
30
26
|
sendJsonError,
|
|
31
27
|
} from "./http-transport.mjs";
|
|
32
28
|
import { httpError } from "./http-errors.mjs";
|
|
33
29
|
import { createRequestAdmission } from "./request-admission.mjs";
|
|
34
|
-
import {
|
|
30
|
+
import { PROVIDER_MANAGEMENT_PATH, handleProviderManagement } from "./provider-management.mjs";
|
|
35
31
|
|
|
36
32
|
export {
|
|
37
33
|
abortErrorStatusCode,
|
|
@@ -63,8 +59,6 @@ export {
|
|
|
63
59
|
} from "./responses-request.mjs";
|
|
64
60
|
|
|
65
61
|
const ADAPTER_RUNTIME_CONFIG = loadRuntimeConfig();
|
|
66
|
-
const DASHBOARD_PATHS = new Set(["/", "/ui.css", "/theme.js", "/language.js", "/refresh.js", "/status.js", "/usage.js", "/ui.js"]);
|
|
67
|
-
const DASHBOARD_API_PATHS = new Set(["/_ccdx/ui/animation", "/_ccdx/ui/models/live", "/_ccdx/ui/usage", "/_ccdx/ui/auth"]);
|
|
68
62
|
|
|
69
63
|
export function requestPath(reqUrl) {
|
|
70
64
|
return new URL(reqUrl || "/", "http://localhost").pathname;
|
|
@@ -80,7 +74,7 @@ function sendRetiredClaudeSurface(res) {
|
|
|
80
74
|
error: {
|
|
81
75
|
type: "not_found_error",
|
|
82
76
|
code: "ccdx_claude_retired",
|
|
83
|
-
message: "
|
|
77
|
+
message: "The GitHub Claude endpoint was retired in ccdx 0.7.0. Microsoft 365 Claude uses the separate gateway; run ccdx m365 status.",
|
|
84
78
|
},
|
|
85
79
|
}));
|
|
86
80
|
}
|
|
@@ -103,13 +97,14 @@ async function sendModelList(res, models, {
|
|
|
103
97
|
headers = {},
|
|
104
98
|
rawBody,
|
|
105
99
|
statusCode = 200,
|
|
100
|
+
restrictToUpstream = false,
|
|
106
101
|
} = {}) {
|
|
107
102
|
let body = rawBody || JSON.stringify(models);
|
|
108
103
|
if (clientVersion && typeof codexModelCatalog?.load === "function") {
|
|
109
104
|
let response = null;
|
|
110
105
|
try {
|
|
111
106
|
const codexCatalog = await codexModelCatalog.load({ clientVersion });
|
|
112
|
-
response = buildCodexModelResponse({ copilotModels: models, codexCatalog });
|
|
107
|
+
response = buildCodexModelResponse({ copilotModels: models, codexCatalog, restrictToUpstream });
|
|
113
108
|
} catch {}
|
|
114
109
|
if (!response) {
|
|
115
110
|
if (res.destroyed || res.writableEnded) return false;
|
|
@@ -170,9 +165,28 @@ export function createAdapterHandler(options = {}) {
|
|
|
170
165
|
const streamPerformanceMetrics = options.streamPerformanceMetrics || createStreamPerformanceMetrics();
|
|
171
166
|
const imagePressure = options.imagePressure || createResponsesImagePressureController();
|
|
172
167
|
const responseFailures = options.responseFailures || createResponseFailureDiagnostics();
|
|
173
|
-
const httpErrors =
|
|
174
|
-
|
|
168
|
+
const httpErrors = createHttpFailureDiagnostics();
|
|
169
|
+
const optionalRoutes = createOptionalRoutes({ ...options, dashboardOptions: { ...options.dashboardOptions,
|
|
170
|
+
...(options.providerRuntime ? { providerCatalogFn: () => options.providerRuntime.dashboardCatalog() } : {}) } });
|
|
171
|
+
const managementHandler = createManagementHandler({
|
|
172
|
+
nativePredictionPac: options.nativePredictionPac, fixPrediction: options.fixPrediction, requestBodyTimeoutMs,
|
|
173
|
+
statusSnapshot: () => {
|
|
174
|
+
const snapshot = runtimeStatusPayload({ metrics: requestMetrics, streamPerformance: streamPerformanceMetrics, admission: acquireRequest,
|
|
175
|
+
imagePressure, responseFailures, imageGeneration: optionalRoutes.imageStats(),
|
|
176
|
+
modelRegistry: codexModelRegistry, codexClient, codexModelRegistry, codexModelCatalog,
|
|
177
|
+
clientIntegrations: options.clientIntegrationSnapshot?.() });
|
|
178
|
+
const providers = options.providerRuntime?.snapshot();
|
|
179
|
+
if (providers?.codex_provider === "m365") {
|
|
180
|
+
snapshot.routing = { ...snapshot.routing, responses: "m365" };
|
|
181
|
+
snapshot.models = { ...snapshot.models, models: providers.m365.models?.gpt || 0,
|
|
182
|
+
source: providers.m365.models?.source || "unavailable", provider: "m365",
|
|
183
|
+
refresh_in_flight: providers.m365.refresh_in_flight === true };
|
|
184
|
+
}
|
|
185
|
+
return { ...snapshot, http_errors: httpErrors.snapshot(), ...(providers ? { providers } : {}) };
|
|
186
|
+
},
|
|
187
|
+
});
|
|
175
188
|
const responsesHandler = createResponsesHandler({
|
|
189
|
+
provider: options.provider || options.providerRuntime?.github,
|
|
176
190
|
acquireRequest,
|
|
177
191
|
autoReviewModelResolver: options.autoReviewModelResolver,
|
|
178
192
|
chatCompletionsFn,
|
|
@@ -191,6 +205,7 @@ export function createAdapterHandler(options = {}) {
|
|
|
191
205
|
upstreamTimeoutMs,
|
|
192
206
|
});
|
|
193
207
|
const responsesCompactHandler = createResponsesCompactHandler({
|
|
208
|
+
provider: options.provider || options.providerRuntime?.github,
|
|
194
209
|
acquireRequest,
|
|
195
210
|
autoReviewModelResolver: options.autoReviewModelResolver,
|
|
196
211
|
imagePressure,
|
|
@@ -203,6 +218,25 @@ export function createAdapterHandler(options = {}) {
|
|
|
203
218
|
streamIdleTimeoutMs,
|
|
204
219
|
upstreamTimeoutMs,
|
|
205
220
|
});
|
|
221
|
+
const providerHandlers = new WeakMap();
|
|
222
|
+
const githubHandlers = { responses: responsesHandler, compact: responsesCompactHandler };
|
|
223
|
+
function handlersFor(provider) {
|
|
224
|
+
if (!provider || provider.id === "github") return githubHandlers;
|
|
225
|
+
if (!providerHandlers.has(provider)) {
|
|
226
|
+
const common = { provider, acquireRequest, imagePressure, keepaliveIntervalMs: options.keepaliveIntervalMs,
|
|
227
|
+
modelRegistry: provider.registry, now: options.now, openAIModelEnv: { ...openAIModelEnv, CCDX_AUTO_REVIEW_MODEL: "" },
|
|
228
|
+
autoReviewModelResolver: () => { const preference = options.providerRuntime.selection().m365;
|
|
229
|
+
return { model: preference.auto_review_model, reasoningEffort: preference.auto_review_effort === undefined ? "low" : preference.auto_review_effort }; },
|
|
230
|
+
requestBodyTimeoutMs, responsesPayloadOptions: options.responsesPayloadOptions, responseFailures,
|
|
231
|
+
streamHandshakeTimeoutMs, streamIdleTimeoutMs, upstreamTimeoutMs };
|
|
232
|
+
providerHandlers.set(provider, {
|
|
233
|
+
responses: createResponsesHandler({ ...common, responsesFn: provider.client.responses, chatCompletionsFn: provider.client.chatCompletions,
|
|
234
|
+
getCachedModelEndpointsFn: provider.client.getCachedModelEndpoints }),
|
|
235
|
+
compact: createResponsesCompactHandler({ ...common, responsesCompactFn: provider.client.responsesCompact }),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
return providerHandlers.get(provider);
|
|
239
|
+
}
|
|
206
240
|
const dispatch = async (req, res, pathname) => {
|
|
207
241
|
if (req.method === "POST" && ["/v1/responses", "/v1/responses/compact"].includes(pathname)
|
|
208
242
|
&& Object.hasOwn(req.headers || {}, "origin")) {
|
|
@@ -211,180 +245,77 @@ export function createAdapterHandler(options = {}) {
|
|
|
211
245
|
sendJsonError(res, error, 403);
|
|
212
246
|
return;
|
|
213
247
|
}
|
|
214
|
-
if (
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
if ((req.method === "GET" || req.method === "HEAD") && DASHBOARD_PATHS.has(pathname)) {
|
|
219
|
-
const { serveDashboard } = await import("./dashboard.mjs");
|
|
220
|
-
return serveDashboard(req, res, pathname);
|
|
221
|
-
}
|
|
222
|
-
if (pathname === "/mcp/image") {
|
|
223
|
-
if (imageMcpHandler) return imageMcpHandler(req, res);
|
|
224
|
-
return import("./image-mcp.mjs").then(({ createImageMcpHandler }) => {
|
|
225
|
-
imageMcpHandler ||= createImageMcpHandler({ localHostname: options.imageMcpHostname });
|
|
226
|
-
return imageMcpHandler(req, res);
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
if (req.method === "GET" && pathname === ADAPTER_STATUS_PATH) {
|
|
230
|
-
if (!isLoopbackAddress(req.socket?.remoteAddress) || !isLoopbackHostHeader(req.headers?.host)) {
|
|
231
|
-
res.writeHead(403, { "Content-Type": "application/json" });
|
|
232
|
-
res.end(JSON.stringify({ error: "Runtime status is available only from loopback" }));
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-store" });
|
|
236
|
-
res.end(JSON.stringify({ ...runtimeStatusPayload({
|
|
237
|
-
metrics: requestMetrics,
|
|
238
|
-
streamPerformance: streamPerformanceMetrics,
|
|
239
|
-
admission: acquireRequest,
|
|
240
|
-
imagePressure,
|
|
241
|
-
responseFailures,
|
|
242
|
-
imageGeneration: imageMcpHandler?.stats?.(),
|
|
243
|
-
modelRegistry: codexModelRegistry,
|
|
244
|
-
codexClient,
|
|
245
|
-
codexModelRegistry,
|
|
246
|
-
codexModelCatalog,
|
|
247
|
-
clientIntegrations: options.clientIntegrationSnapshot?.(),
|
|
248
|
-
}), http_errors: httpErrors.snapshot() }));
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
if (req.method === "GET" && pathname === ADAPTER_HEALTH_PATH) {
|
|
253
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
254
|
-
const predictionPac = isLoopbackAddress(req.socket?.remoteAddress) && isLoopbackHostHeader(req.headers?.host)
|
|
255
|
-
? options.nativePredictionPac?.() : null;
|
|
256
|
-
const health = adapterHealthPayload();
|
|
257
|
-
res.end(JSON.stringify({ ...health,
|
|
258
|
-
capabilities: [...health.capabilities, ...(typeof options.fixPrediction === "function" ? [PREDICTION_FIX_CAPABILITY] : [])],
|
|
259
|
-
...(predictionPac ? { native_prediction_pac: predictionPac } : {}) }));
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (pathname === ADAPTER_PREDICTION_FIX_PATH) {
|
|
264
|
-
if (!isLoopbackAddress(req.socket?.remoteAddress) || req.headers?.host === undefined
|
|
265
|
-
|| !isLoopbackHostHeader(req.headers.host) || Object.hasOwn(req.headers, "origin")) {
|
|
266
|
-
sendJsonError(res, httpError("Prediction repair is available only to native loopback clients", 403));return;
|
|
267
|
-
}
|
|
268
|
-
if (req.method !== "POST") {
|
|
269
|
-
res.setHeader?.("Allow", "POST");sendJsonError(res, httpError("Prediction repair requires POST", 405));return;
|
|
270
|
-
}
|
|
271
|
-
if (String(req.headers["content-type"] || "").split(";", 1)[0].trim().toLowerCase() !== "application/json") {
|
|
272
|
-
sendJsonError(res, httpError("Prediction repair requires application/json", 415));return;
|
|
273
|
-
}
|
|
274
|
-
if (typeof options.fixPrediction !== "function") {
|
|
275
|
-
sendJsonError(res, httpError("This adapter does not support prediction repair; upgrade and restart CCDX once", 503));return;
|
|
276
|
-
}
|
|
277
|
-
const abort = createRequestAbort(req, res);abort.setTimeout(2000);
|
|
278
|
-
try {
|
|
279
|
-
const body = await readJsonBody(req, { maxBodyBytes: 1024, maxDecodedBodyBytes: 1024, signal: abort.signal });
|
|
280
|
-
if (!body || Object.keys(body).length !== 1 || body.action !== "fix") throw httpError("Expected prediction repair action: fix", 400);
|
|
281
|
-
abort.clearTimeout();
|
|
282
|
-
const result = await options.fixPrediction();
|
|
283
|
-
if (!res.destroyed && !res.writableEnded) {
|
|
284
|
-
res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-store" });res.end(JSON.stringify(result));
|
|
285
|
-
}
|
|
286
|
-
} catch (error) { sendJsonError(res, error, 503); }
|
|
287
|
-
finally { abort.cleanup(); }
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
248
|
+
if (isOptionalRoute(req.method, pathname)) return optionalRoutes.handle(req, res, pathname);
|
|
249
|
+
if (isManagementRoute(req.method, pathname)) return managementHandler(req, res, pathname);
|
|
250
|
+
if (pathname === PROVIDER_MANAGEMENT_PATH) return handleProviderManagement(req, res, options.providerRuntime);
|
|
290
251
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}
|
|
307
|
-
if (Object.hasOwn(req.headers || {}, "origin")) {
|
|
308
|
-
res.writeHead(403, { "Content-Type": "application/json" });
|
|
309
|
-
res.end(JSON.stringify({ error: "Browser origins are not allowed for cache control" }));
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
const mediaType = String(req.headers?.["content-type"] || "").split(";", 1)[0].trim().toLowerCase();
|
|
313
|
-
if (mediaType !== "application/json") {
|
|
314
|
-
res.writeHead(415, { "Content-Type": "application/json" });
|
|
315
|
-
res.end(JSON.stringify({ error: "Cache control requires application/json" }));
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
const abort = createRequestAbort(req, res);
|
|
319
|
-
abort.setTimeout(requestBodyTimeoutMs, "request_body_timeout");
|
|
320
|
-
try {
|
|
321
|
-
const body = await readJsonBody(req, {
|
|
322
|
-
maxBodyBytes: 8 * 1024,
|
|
323
|
-
maxDecodedBodyBytes: 8 * 1024,
|
|
324
|
-
signal: abort.signal,
|
|
325
|
-
});
|
|
326
|
-
let result;
|
|
327
|
-
if (body?.action === "set_limit") result = setHistoryCacheLimit(body.max_bytes);
|
|
328
|
-
else if (body?.action === "clean") result = cleanRuntimeCaches({ history: body.history === true });
|
|
329
|
-
else throw httpError("Unknown cache action", 400);
|
|
330
|
-
res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-store" });
|
|
331
|
-
res.end(JSON.stringify(result));
|
|
332
|
-
} catch (error) {
|
|
333
|
-
if (["ccdx_cache_busy", "ccdx_cache_limit_below_usage"].includes(error?.code)) error.statusCode = 409;
|
|
334
|
-
sendJsonError(res, error, 400);
|
|
335
|
-
} finally {
|
|
336
|
-
abort.cleanup();
|
|
252
|
+
let target;
|
|
253
|
+
if (["/v1/responses", "/v1/responses/compact", "/v1/models"].includes(pathname) && options.providerRuntime) {
|
|
254
|
+
try { target = options.providerRuntime.current(); }
|
|
255
|
+
catch (error) { sendJsonError(res, error, 503);return; }
|
|
256
|
+
const context = currentRequestContext();
|
|
257
|
+
if (context) {
|
|
258
|
+
context.provider = target.id;
|
|
259
|
+
context.streamPerformance?.setProvider?.(target.id);
|
|
260
|
+
context.finishProvider = options.providerRuntime.track?.(target);
|
|
261
|
+
Object.assign(context, options.providerRuntime.historyContext?.(req.headers, target));
|
|
262
|
+
context.clientHeaders = {
|
|
263
|
+
session_id: typeof req.headers.session_id === "string" ? req.headers.session_id : undefined,
|
|
264
|
+
"session-id": typeof req.headers["session-id"] === "string" ? req.headers["session-id"] : undefined,
|
|
265
|
+
"x-conversation-id": typeof req.headers["x-conversation-id"] === "string" ? req.headers["x-conversation-id"] : undefined,
|
|
266
|
+
};
|
|
337
267
|
}
|
|
338
|
-
return;
|
|
339
268
|
}
|
|
340
269
|
|
|
341
270
|
if (req.method === "POST" && pathname === "/v1/responses") {
|
|
342
|
-
return
|
|
271
|
+
return handlersFor(target).responses(req, res);
|
|
343
272
|
}
|
|
344
273
|
|
|
345
274
|
if (req.method === "POST" && pathname === "/v1/responses/compact") {
|
|
346
|
-
return
|
|
275
|
+
return handlersFor(target).compact(req, res);
|
|
347
276
|
}
|
|
348
277
|
|
|
349
278
|
if (req.method === "GET" && pathname === "/v1/models") {
|
|
350
279
|
const clientVersion = modelClientVersion(req.url);
|
|
280
|
+
const selectedRegistry = target?.registry || codexModelRegistry;
|
|
281
|
+
const selectedListModels = target?.id === "m365" ? target.client.listModels : listModelsFn;
|
|
282
|
+
const modelOptions = { codexModelCatalog, clientVersion, restrictToUpstream: target?.id === "m365" };
|
|
351
283
|
const abort = createRequestAbort(req, res);
|
|
352
284
|
// Keep live discovery and explicit auth failures authoritative, but do
|
|
353
285
|
// not make a usable cached picker wait the full inference timeout.
|
|
354
|
-
const modelTimeoutMs = isValidModelList(
|
|
286
|
+
const modelTimeoutMs = isValidModelList(selectedRegistry?.models)
|
|
355
287
|
? Math.min(upstreamTimeoutMs, parsePositiveInteger(options.cachedModelsTimeoutMs, 1500))
|
|
356
288
|
: upstreamTimeoutMs;
|
|
357
289
|
abort.setTimeout(modelTimeoutMs);
|
|
358
|
-
|
|
290
|
+
selectedListModels({ signal: abort.signal })
|
|
359
291
|
.then(async ({ status, body }) => {
|
|
360
292
|
abort.clearTimeout();
|
|
361
293
|
if (status >= 200 && status < 300) {
|
|
362
294
|
let models;
|
|
363
295
|
try { models = JSON.parse(body); } catch {}
|
|
364
296
|
if (isValidModelList(models)) {
|
|
365
|
-
if (
|
|
366
|
-
|
|
297
|
+
if (selectedRegistry) {
|
|
298
|
+
selectedRegistry.models = models;
|
|
367
299
|
}
|
|
368
300
|
await sendModelList(res, models, {
|
|
369
|
-
|
|
370
|
-
clientVersion,
|
|
301
|
+
...modelOptions,
|
|
371
302
|
rawBody: body,
|
|
372
303
|
statusCode: status,
|
|
373
304
|
});
|
|
374
305
|
return;
|
|
375
306
|
}
|
|
376
|
-
if (await sendLastKnownGoodModels(res,
|
|
307
|
+
if (await sendLastKnownGoodModels(res, selectedRegistry, modelOptions)) return;
|
|
377
308
|
sendJsonError(res, httpError("Copilot models response contained no valid models", 502), 502);
|
|
378
309
|
return;
|
|
379
310
|
}
|
|
380
311
|
if (modelListCanUseLastKnownGood(status)
|
|
381
|
-
&& await sendLastKnownGoodModels(res,
|
|
312
|
+
&& await sendLastKnownGoodModels(res, selectedRegistry, modelOptions)) return;
|
|
382
313
|
res.writeHead(status, { "Content-Type": "application/json" });
|
|
383
314
|
res.end(body);
|
|
384
315
|
})
|
|
385
316
|
.catch(async (e) => {
|
|
386
317
|
logRequestFailure("Models", e, abort);
|
|
387
|
-
if (await sendLastKnownGoodModels(res,
|
|
318
|
+
if (await sendLastKnownGoodModels(res, selectedRegistry, modelOptions)) return;
|
|
388
319
|
if (!res.destroyed && !res.writableEnded) {
|
|
389
320
|
if (!res.headersSent) res.writeHead(e?.statusCode || 502);
|
|
390
321
|
res.end(JSON.stringify({ error: e.message }));
|
|
@@ -429,6 +360,7 @@ export function createAdapterHandler(options = {}) {
|
|
|
429
360
|
&& pathname !== ADAPTER_STATUS_PATH
|
|
430
361
|
&& pathname !== ADAPTER_CACHE_PATH
|
|
431
362
|
&& pathname !== ADAPTER_PREDICTION_FIX_PATH
|
|
363
|
+
&& pathname !== PROVIDER_MANAGEMENT_PATH
|
|
432
364
|
&& !DASHBOARD_API_PATHS.has(pathname)
|
|
433
365
|
&& !((req.method === "GET" || req.method === "HEAD") && DASHBOARD_PATHS.has(pathname));
|
|
434
366
|
const routeName = classifyAdapterRoute(req.method, pathname);
|
|
@@ -447,11 +379,18 @@ export function createAdapterHandler(options = {}) {
|
|
|
447
379
|
if (requestFinished) return;
|
|
448
380
|
requestFinished = true;
|
|
449
381
|
if (trackRequest && statusCode >= 400) {
|
|
450
|
-
try { httpErrors.record(buildHttpFailureDetails({ ...httpEvidence, statusCode, route: routeName, requestId, model: context?.model
|
|
382
|
+
try { httpErrors.record(buildHttpFailureDetails({ ...httpEvidence, statusCode, route: routeName, requestId, model: context?.model,
|
|
383
|
+
method: ["GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"].includes(req.method) ? req.method : "OTHER",
|
|
384
|
+
target: ["/v1/responses", "/v1/responses/compact", "/mcp/image", "/.well-known/oauth-authorization-server", "/.well-known/oauth-protected-resource", "/.well-known/oauth-protected-resource/mcp/image"].includes(pathname) ? pathname : "unrecognized_path",
|
|
385
|
+
request_options: context?.requestOptions, upstream_attempts: streamPerformance?.attemptCount?.(),
|
|
386
|
+
attempt_timings: streamPerformance?.attemptTimings?.(),
|
|
387
|
+
requested_model: context?.requestedModel, provider: context?.provider,
|
|
388
|
+
})); } catch {}
|
|
451
389
|
}
|
|
452
390
|
try { complete({ statusCode, aborted }); } catch {}
|
|
453
391
|
try { streamPerformance?.finish({ failed: aborted || statusCode >= 400, aborted, statusCode }); } catch {}
|
|
454
392
|
try { finishTerminalActivity(); } catch {}
|
|
393
|
+
try { context?.finishProvider?.(); } catch {}
|
|
455
394
|
};
|
|
456
395
|
const containUnexpectedError = (error) => {
|
|
457
396
|
try { finishRequest({ statusCode: res.statusCode >= 400 ? res.statusCode : 500 }); } catch {}
|
|
@@ -484,7 +423,14 @@ export function createAdapterHandler(options = {}) {
|
|
|
484
423
|
requestId,
|
|
485
424
|
pathname,
|
|
486
425
|
showRequestId: options.showRequestId === true,
|
|
426
|
+
includeHttpErrorDetails: options.includeHttpErrorDetails === true || process.env.CCDX_HTTP_ERROR_DETAILS === "1",
|
|
487
427
|
streamPerformance,
|
|
428
|
+
provider: undefined,
|
|
429
|
+
finishProvider: undefined,
|
|
430
|
+
historyNamespace: undefined,
|
|
431
|
+
historyTransferNamespace: undefined,
|
|
432
|
+
onHistoryStored: undefined,
|
|
433
|
+
clientHeaders: undefined,
|
|
488
434
|
onHttpError: evidence => { httpEvidence = evidence; },
|
|
489
435
|
};
|
|
490
436
|
const result = runWithRequestContext(context, () => dispatchRequest(req, res, pathname));
|
|
@@ -88,13 +88,15 @@ export async function runAutoReviewModelCommand({
|
|
|
88
88
|
loadCatalog = loadAutoReviewModelCatalog,
|
|
89
89
|
output = process.stdout,
|
|
90
90
|
prompt,
|
|
91
|
+
getPreference = autoReviewPreference,
|
|
92
|
+
savePreference = (model, options) => writeAutoReviewModel(model === DEFAULT_CODEX_AUTO_REVIEW_MODEL ? "" : model, options),
|
|
91
93
|
} = {}) {
|
|
92
94
|
if (!prompt && (!input.isTTY || !output.isTTY)) {
|
|
93
95
|
throw new Error(`${commandName} auto-review-model requires an interactive terminal`);
|
|
94
96
|
}
|
|
95
97
|
|
|
96
98
|
readUserSettings({ env, home, strict: true });
|
|
97
|
-
const current =
|
|
99
|
+
const current = getPreference({ env, home });
|
|
98
100
|
const catalog = await loadCatalog({ env, fetchImpl, home });
|
|
99
101
|
const modelIds = sortedModelIds(catalog.modelIds, current.model);
|
|
100
102
|
if (!modelIds.length) throw new Error("No selectable Responses models are available");
|
|
@@ -167,8 +169,7 @@ export async function runAutoReviewModelCommand({
|
|
|
167
169
|
readline?.close();
|
|
168
170
|
}
|
|
169
171
|
|
|
170
|
-
const
|
|
171
|
-
const result = writeAutoReviewModel(savedModel, { env, home, reasoningEffort: selectedEffort });
|
|
172
|
+
const result = await savePreference(selectedModel, { env, home, reasoningEffort: selectedEffort });
|
|
172
173
|
output.write(`${result.changed ? "Saved" : "Kept"} Auto Review model: ${selectedModel}\n`);
|
|
173
174
|
output.write(`Reasoning: ${selectedEffort || "follow client (low when omitted)"}\n`);
|
|
174
175
|
if (String(env.CCDX_AUTO_REVIEW_MODEL || "").trim()) {
|