copilot-relay 0.6.2 → 0.7.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 +2 -2
- package/config.default.yaml +5 -2
- package/dist/main.js +699 -556
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -1113,38 +1113,129 @@ function getRequestReasoningEffort(request) {
|
|
|
1113
1113
|
return requested;
|
|
1114
1114
|
}
|
|
1115
1115
|
const resolveReasoningEffort = (requested) => requested ?? getRuntimeState().thinkEffort ?? defaultReasoningEffort;
|
|
1116
|
-
const
|
|
1117
|
-
const
|
|
1118
|
-
return
|
|
1116
|
+
const currentCatalog = () => {
|
|
1117
|
+
const state = getRuntimeState();
|
|
1118
|
+
return state.modelCatalog?.baseUrl === state.upstreamBaseUrl ? state.modelCatalog?.models : void 0;
|
|
1119
1119
|
};
|
|
1120
|
+
const normalizeCopilotModelId = (model) => canonicalModelId(model, currentCatalog());
|
|
1121
|
+
const isClaudeModelId = (model) => model.startsWith("claude-");
|
|
1122
|
+
/**
|
|
1123
|
+
* The ID Claude Code sees. A model whose own known window is at least 1M is offered only as its 1M
|
|
1124
|
+
* version, `<id>[1m]`, Claude models included; a smaller known window shows the plain ID, and an
|
|
1125
|
+
* unknown window leaves the ID as given. [1m] overrides Claude Code's numeric context setting.
|
|
1126
|
+
*/
|
|
1120
1127
|
const normalizeClaudeModelId = (model, contextWindowTokens) => {
|
|
1121
|
-
const
|
|
1122
|
-
|
|
1123
|
-
const
|
|
1124
|
-
|
|
1125
|
-
return
|
|
1128
|
+
const catalog = currentCatalog();
|
|
1129
|
+
const canonical$1 = canonicalModelId(model, catalog);
|
|
1130
|
+
const contextWindow = contextWindowTokens ?? catalog?.get(canonical$1)?.limits?.max_context_window_tokens;
|
|
1131
|
+
if (contextWindow === void 0) return model;
|
|
1132
|
+
return contextWindow >= 1e6 ? `${canonical$1}[1m]` : canonical$1;
|
|
1126
1133
|
};
|
|
1127
|
-
const normalizeCopilotModelId = (model) => normalizeOneMillionContextModel(model) ?? model;
|
|
1128
|
-
const isClaudeModelId = (model) => model.startsWith("claude-");
|
|
1129
1134
|
const getConfiguredModelRouting = () => getRuntimeState().modelRouting ?? defaultModelRouting;
|
|
1130
1135
|
const getModelRouting = () => {
|
|
1131
1136
|
const routing = getConfiguredModelRouting();
|
|
1132
1137
|
return {
|
|
1138
|
+
...routing,
|
|
1133
1139
|
gptModel: normalizeCopilotModelId(routing.gptModel),
|
|
1134
1140
|
opusModel: normalizeCopilotModelId(routing.opusModel)
|
|
1135
1141
|
};
|
|
1136
1142
|
};
|
|
1143
|
+
const getExposedUpstreamModelIds = () => resolveExposedModels(getConfiguredModelRouting(), currentCatalog());
|
|
1137
1144
|
const getExposedModelIds = () => {
|
|
1138
1145
|
const routing = getConfiguredModelRouting();
|
|
1139
|
-
|
|
1146
|
+
const slots = [routing.gptModel, routing.opusModel];
|
|
1147
|
+
return getExposedUpstreamModelIds().map((id) => {
|
|
1148
|
+
return normalizeClaudeModelId(slots.find((configured) => normalizeCopilotModelId(configured).toLowerCase() === id.toLowerCase()) ?? id);
|
|
1149
|
+
});
|
|
1140
1150
|
};
|
|
1141
1151
|
const getUpstreamModelIds = () => {
|
|
1142
1152
|
const routing = getModelRouting();
|
|
1143
1153
|
return [routing.gptModel, routing.opusModel];
|
|
1144
1154
|
};
|
|
1145
1155
|
const routeModelId = (model) => {
|
|
1156
|
+
const requested = normalizeCopilotModelId(model.trim()).toLowerCase();
|
|
1157
|
+
const exposed = getExposedUpstreamModelIds().find((id) => id.toLowerCase() === requested);
|
|
1158
|
+
if (exposed !== void 0) return exposed;
|
|
1146
1159
|
const routing = getModelRouting();
|
|
1147
|
-
return
|
|
1160
|
+
return requested.includes("opus") ? routing.opusModel : routing.gptModel;
|
|
1161
|
+
};
|
|
1162
|
+
const modelIdPattern = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
1163
|
+
const invalidModelsMessage = "Invalid models: expected auto, empty, or comma-separated Copilot model IDs";
|
|
1164
|
+
const parseModelSelection = (value) => {
|
|
1165
|
+
const trimmed = value.trim();
|
|
1166
|
+
if (trimmed.toLowerCase() === "auto") return "auto";
|
|
1167
|
+
if (trimmed === "") return [];
|
|
1168
|
+
const ids = trimmed.split(",").map((id) => id.trim());
|
|
1169
|
+
if (ids.some((id) => !modelIdPattern.test(id) || id.toLowerCase() === "auto")) throw new Error(invalidModelsMessage);
|
|
1170
|
+
return ids;
|
|
1171
|
+
};
|
|
1172
|
+
const canonicalModelId = (id, catalog) => {
|
|
1173
|
+
const lower = id.replace(/\[1m\]/gi, "").toLowerCase();
|
|
1174
|
+
for (const key of catalog?.keys() ?? []) if (key.toLowerCase() === lower) return key;
|
|
1175
|
+
return lower;
|
|
1176
|
+
};
|
|
1177
|
+
const pickerCategories = [
|
|
1178
|
+
"powerful",
|
|
1179
|
+
"versatile",
|
|
1180
|
+
"lightweight"
|
|
1181
|
+
];
|
|
1182
|
+
const categoryRank = (model) => {
|
|
1183
|
+
const rank = pickerCategories.indexOf(model.pickerCategory ?? "");
|
|
1184
|
+
return rank === -1 ? pickerCategories.length : rank;
|
|
1185
|
+
};
|
|
1186
|
+
const compareVersions = (left, right) => {
|
|
1187
|
+
for (let index = 0; index < Math.max(left.length, right.length); index++) {
|
|
1188
|
+
const difference = (left[index] ?? 0) - (right[index] ?? 0);
|
|
1189
|
+
if (difference !== 0) return difference;
|
|
1190
|
+
}
|
|
1191
|
+
return 0;
|
|
1192
|
+
};
|
|
1193
|
+
const isBetterPick = (left, right) => {
|
|
1194
|
+
const byVersion = compareVersions(left.version, right.version);
|
|
1195
|
+
if (byVersion !== 0) return byVersion > 0;
|
|
1196
|
+
const byCategory = categoryRank(right.model) - categoryRank(left.model);
|
|
1197
|
+
if (byCategory !== 0) return byCategory > 0;
|
|
1198
|
+
return left.id < right.id;
|
|
1199
|
+
};
|
|
1200
|
+
/** The newest picker-enabled chat model of each vendor except Experimental, in catalog order. */
|
|
1201
|
+
const selectNewestPerVendor = (catalog) => {
|
|
1202
|
+
const best = /* @__PURE__ */ new Map();
|
|
1203
|
+
for (const [id, model] of catalog) {
|
|
1204
|
+
const vendor = model.vendor;
|
|
1205
|
+
const version = /\d+(?:\.\d+)*/.exec(id)?.[0].split(".").map(Number);
|
|
1206
|
+
if (!(model.pickerEnabled === true && model.type === "chat" && vendor !== "Experimental") || vendor === void 0 || version === void 0) continue;
|
|
1207
|
+
const candidate = {
|
|
1208
|
+
id,
|
|
1209
|
+
model,
|
|
1210
|
+
version
|
|
1211
|
+
};
|
|
1212
|
+
const current = best.get(vendor);
|
|
1213
|
+
if (current === void 0 || isBetterPick(candidate, current)) best.set(vendor, candidate);
|
|
1214
|
+
}
|
|
1215
|
+
const winners = new Set([...best.values()].map((candidate) => candidate.id));
|
|
1216
|
+
return [...catalog.keys()].filter((id) => winners.has(id));
|
|
1217
|
+
};
|
|
1218
|
+
/**
|
|
1219
|
+
* The upstream IDs clients are offered: gptModel, opusModel, then the selection, deduplicated
|
|
1220
|
+
* case-insensitively in catalog casing. A listed ID the catalog lacks is skipped; without a
|
|
1221
|
+
* catalog, auto adds nothing and every listed ID is kept, lowercased.
|
|
1222
|
+
*/
|
|
1223
|
+
const resolveExposedModels = (routing, catalog) => {
|
|
1224
|
+
const present = (routing.models === "auto" ? selectNewestPerVendor(catalog ?? /* @__PURE__ */ new Map()) : routing.models ?? []).filter((id) => catalog === void 0 || catalog.has(canonicalModelId(id, catalog)));
|
|
1225
|
+
const exposed = [];
|
|
1226
|
+
for (const id of [
|
|
1227
|
+
routing.gptModel,
|
|
1228
|
+
routing.opusModel,
|
|
1229
|
+
...present
|
|
1230
|
+
]) {
|
|
1231
|
+
const canonical$1 = canonicalModelId(id, catalog);
|
|
1232
|
+
if (!exposed.some((known) => known.toLowerCase() === canonical$1.toLowerCase())) exposed.push(canonical$1);
|
|
1233
|
+
}
|
|
1234
|
+
return exposed;
|
|
1235
|
+
};
|
|
1236
|
+
const findMissingModels = (routing, catalog) => {
|
|
1237
|
+
if (!Array.isArray(routing.models)) return [];
|
|
1238
|
+
return routing.models.filter((id) => !catalog.has(canonicalModelId(id, catalog)));
|
|
1148
1239
|
};
|
|
1149
1240
|
|
|
1150
1241
|
//#endregion
|
|
@@ -1343,6 +1434,7 @@ const defaultConfig = {
|
|
|
1343
1434
|
logLevel: "info",
|
|
1344
1435
|
logRetentionDays: 3,
|
|
1345
1436
|
opusModel: "claude-opus-5.5",
|
|
1437
|
+
models: "auto",
|
|
1346
1438
|
port: 4142,
|
|
1347
1439
|
thinkEffort: defaultReasoningEffort,
|
|
1348
1440
|
upstreamTimeoutSeconds: 180,
|
|
@@ -1378,6 +1470,12 @@ const normalizeInteger = (value, key, minimum, maximum = Number.MAX_SAFE_INTEGER
|
|
|
1378
1470
|
if (typeof candidate !== "number" || !Number.isSafeInteger(candidate) || candidate < minimum || candidate > maximum) throw new Error(`Invalid ${key}: expected an integer from ${minimum} to ${maximum}`);
|
|
1379
1471
|
return candidate;
|
|
1380
1472
|
};
|
|
1473
|
+
const normalizeModels = (value) => {
|
|
1474
|
+
if (value === void 0) return;
|
|
1475
|
+
if (typeof value !== "string") throw new Error("Invalid models: expected auto, empty, or comma-separated Copilot model IDs");
|
|
1476
|
+
parseModelSelection(value);
|
|
1477
|
+
return value.trim();
|
|
1478
|
+
};
|
|
1381
1479
|
const normalizeString = (value) => typeof value === "string" && value.trim() ? value.trim() : void 0;
|
|
1382
1480
|
const normalizeRequiredString = (value, key) => {
|
|
1383
1481
|
if (value === void 0) return;
|
|
@@ -1679,6 +1777,9 @@ const serializeConfig = (config) => [
|
|
|
1679
1777
|
"",
|
|
1680
1778
|
"# Upstream Copilot model used for requests containing \"opus\".",
|
|
1681
1779
|
`opusModel: ${config.opusModel}`,
|
|
1780
|
+
"",
|
|
1781
|
+
"# Extra models offered to clients: auto (newest model of each vendor), empty, or comma-separated Copilot IDs.",
|
|
1782
|
+
`models: ${config.models}`,
|
|
1682
1783
|
""
|
|
1683
1784
|
].join("\n");
|
|
1684
1785
|
const resolveConfig = (raw) => ({
|
|
@@ -1691,6 +1792,7 @@ const resolveConfig = (raw) => ({
|
|
|
1691
1792
|
logLevel: normalizeLogLevel(raw.logLevel) ?? defaultConfig.logLevel,
|
|
1692
1793
|
logRetentionDays: normalizeInteger(raw.logRetentionDays, "logRetentionDays", 1) ?? defaultConfig.logRetentionDays,
|
|
1693
1794
|
opusModel: normalizeRequiredString(raw.opusModel, "opusModel") ?? defaultConfig.opusModel,
|
|
1795
|
+
models: normalizeModels(raw.models) ?? defaultConfig.models,
|
|
1694
1796
|
port: normalizeInteger(raw.port, "port", 1, 65535) ?? defaultConfig.port,
|
|
1695
1797
|
thinkEffort: normalizeThinkEffort(raw.thinkEffort) ?? defaultConfig.thinkEffort,
|
|
1696
1798
|
upstreamTimeoutSeconds: normalizeUpstreamTimeoutSeconds(raw.upstreamTimeoutSeconds) ?? defaultConfig.upstreamTimeoutSeconds,
|
|
@@ -3397,8 +3499,43 @@ function getCachedCopilotModel(config, model) {
|
|
|
3397
3499
|
return catalog.models.get(normalizeCopilotModelId(model));
|
|
3398
3500
|
}
|
|
3399
3501
|
const advertisesNoReasoningEffort = (capabilities$1) => capabilities$1?.reasoningEfforts?.length === 0;
|
|
3502
|
+
const copilotReasoningEfforts = [
|
|
3503
|
+
"none",
|
|
3504
|
+
"minimal",
|
|
3505
|
+
"low",
|
|
3506
|
+
"medium",
|
|
3507
|
+
"high",
|
|
3508
|
+
"xhigh",
|
|
3509
|
+
"max"
|
|
3510
|
+
];
|
|
3511
|
+
const effortOrder = [
|
|
3512
|
+
"none",
|
|
3513
|
+
"minimal",
|
|
3514
|
+
"low",
|
|
3515
|
+
"medium",
|
|
3516
|
+
"high",
|
|
3517
|
+
"xhigh",
|
|
3518
|
+
"max",
|
|
3519
|
+
"ultra"
|
|
3520
|
+
];
|
|
3521
|
+
const effortRank = (effort) => effortOrder.indexOf(effort);
|
|
3522
|
+
/**
|
|
3523
|
+
* The reasoning effort to send for a requested one.
|
|
3524
|
+
*
|
|
3525
|
+
* A level the model advertises, or one this relay does not know, is sent as requested. Any other
|
|
3526
|
+
* level becomes the highest advertised level below it, or the lowest advertised level when none is
|
|
3527
|
+
* below it. A model that advertises no levels gets none, since Copilot rejects the field for it.
|
|
3528
|
+
*/
|
|
3529
|
+
const clampReasoningEffort = (requested, advertised) => {
|
|
3530
|
+
if (advertised?.length === 0) return;
|
|
3531
|
+
const accepted = advertised ?? copilotReasoningEfforts;
|
|
3532
|
+
if (accepted.includes(requested) || effortRank(requested) === -1) return requested;
|
|
3533
|
+
const ranked = accepted.filter((effort) => effortRank(effort) !== -1).sort((left, right) => effortRank(left) - effortRank(right));
|
|
3534
|
+
return ranked.filter((effort) => effortRank(effort) <= effortRank(requested)).at(-1) ?? ranked[0];
|
|
3535
|
+
};
|
|
3400
3536
|
function resolveModelReasoningEffort(config, model, requested) {
|
|
3401
|
-
|
|
3537
|
+
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
3538
|
+
if (!advertisesNoReasoningEffort(capabilities$1)) return clampReasoningEffort(resolveReasoningEffort(requested), capabilities$1?.reasoningEfforts);
|
|
3402
3539
|
if (requested === void 0) return;
|
|
3403
3540
|
const message = "The selected upstream model does not advertise reasoning effort support; omit the explicit effort control.";
|
|
3404
3541
|
throw new HTTPError(message, Response.json({
|
|
@@ -3449,7 +3586,10 @@ async function loadCopilotModelCatalog(config) {
|
|
|
3449
3586
|
...supportedEndpoints !== void 0 && { supportedEndpoints },
|
|
3450
3587
|
...typeof capabilities$1?.type === "string" && { type: capabilities$1.type },
|
|
3451
3588
|
...reasoningEfforts !== void 0 && { reasoningEfforts },
|
|
3452
|
-
...typeof model.name === "string" && model.name !== "" && { name: model.name }
|
|
3589
|
+
...typeof model.name === "string" && model.name !== "" && { name: model.name },
|
|
3590
|
+
...typeof model.vendor === "string" && { vendor: model.vendor },
|
|
3591
|
+
...typeof model.model_picker_enabled === "boolean" && { pickerEnabled: model.model_picker_enabled },
|
|
3592
|
+
...typeof model.model_picker_category === "string" && { pickerCategory: model.model_picker_category }
|
|
3453
3593
|
});
|
|
3454
3594
|
}
|
|
3455
3595
|
const catalog = {
|
|
@@ -6098,9 +6238,11 @@ function getUpstreamErrorDetail(response, body) {
|
|
|
6098
6238
|
return body.slice(0, 240);
|
|
6099
6239
|
}
|
|
6100
6240
|
}
|
|
6241
|
+
const responsesRetryCodes = new Set(["unsupported_api_for_model", "no_available_model_endpoints"]);
|
|
6101
6242
|
async function shouldRetryWithResponses(response, signal, timeoutMs) {
|
|
6102
6243
|
try {
|
|
6103
|
-
|
|
6244
|
+
const errorBody = await readCopilotJson(response.clone(), signal, timeoutMs);
|
|
6245
|
+
return responsesRetryCodes.has(errorBody.error?.code ?? "");
|
|
6104
6246
|
} catch {
|
|
6105
6247
|
return false;
|
|
6106
6248
|
}
|
|
@@ -6196,6 +6338,18 @@ function normalizeNativeHistory(messages) {
|
|
|
6196
6338
|
}
|
|
6197
6339
|
return combined;
|
|
6198
6340
|
}
|
|
6341
|
+
const clampSystemEfforts = (config, model, messages) => messages.map((message) => {
|
|
6342
|
+
if (message.role !== "system" || message.output_config?.effort === void 0) return message;
|
|
6343
|
+
const effort = resolveModelReasoningEffort(config, model, message.output_config.effort);
|
|
6344
|
+
if (effort === void 0) return message;
|
|
6345
|
+
return {
|
|
6346
|
+
...message,
|
|
6347
|
+
output_config: {
|
|
6348
|
+
...message.output_config,
|
|
6349
|
+
effort
|
|
6350
|
+
}
|
|
6351
|
+
};
|
|
6352
|
+
});
|
|
6199
6353
|
function shouldUseNativeMessages(config, model) {
|
|
6200
6354
|
return selectCopilotEndpoint(config, model).endpoint === "/v1/messages";
|
|
6201
6355
|
}
|
|
@@ -6234,7 +6388,7 @@ async function createNativeMessages(config, payload, options) {
|
|
|
6234
6388
|
};
|
|
6235
6389
|
const body = {
|
|
6236
6390
|
...payload,
|
|
6237
|
-
messages: normalizeNativeHistory(payload.messages),
|
|
6391
|
+
messages: clampSystemEfforts(config, payload.model, normalizeNativeHistory(payload.messages)),
|
|
6238
6392
|
max_tokens: maxTokens,
|
|
6239
6393
|
stream: Boolean(payload.stream || exceedsNonStreamingLimit),
|
|
6240
6394
|
output_config: outputConfig,
|
|
@@ -6248,7 +6402,8 @@ async function createNativeMessages(config, payload, options) {
|
|
|
6248
6402
|
};
|
|
6249
6403
|
const beta = options.headers?.get("anthropic-beta");
|
|
6250
6404
|
if (beta) headers["anthropic-beta"] = beta;
|
|
6251
|
-
const
|
|
6405
|
+
const advertised = getCachedCopilotModel(config, payload.model)?.reasoningEfforts;
|
|
6406
|
+
const effortLabel = effort === void 0 ? "omitted" : clampReasoningEffort(getClaudeTurnEffort(payload).effective, advertised) ?? "omitted";
|
|
6252
6407
|
const initiator = options.initiator ?? "agent";
|
|
6253
6408
|
log.info(sanitizeTerminalString(`request_id=${options.requestId} Model request client=${options.client ?? "claude"} requested_model=${options.requestedModel ?? payload.model} upstream_model=${payload.model} upstream_api=messages effective_think_effort=${effortLabel} initiator=${initiator}`));
|
|
6254
6409
|
const response = await fetchCopilot(getCopilotProviderContext(config), "/v1/messages", {
|
|
@@ -6744,8 +6899,7 @@ const translationEndpoint = (config, model) => {
|
|
|
6744
6899
|
const selection = selectCopilotEndpoint(config, normalizeCopilotModelId(model));
|
|
6745
6900
|
return selection.endpoint && !selection.responsesFallback ? selection.endpoint : void 0;
|
|
6746
6901
|
};
|
|
6747
|
-
const handleClaudeMessageRequest = async (config, claudePayload, initiator, requestSignal, requestId, writeEvent, requestHeaders) => {
|
|
6748
|
-
const upstreamModel = translateModelName(claudePayload.model);
|
|
6902
|
+
const handleClaudeMessageRequest = async (config, claudePayload, upstreamModel, initiator, requestSignal, requestId, writeEvent, requestHeaders) => {
|
|
6749
6903
|
if (shouldUseNativeMessages(config, upstreamModel)) return handleNativeMessages(config, {
|
|
6750
6904
|
...claudePayload,
|
|
6751
6905
|
model: upstreamModel
|
|
@@ -6758,7 +6912,10 @@ const handleClaudeMessageRequest = async (config, claudePayload, initiator, requ
|
|
|
6758
6912
|
const shouldLetModelDecideWebSearch = hasClaudeWebSearch(claudePayload);
|
|
6759
6913
|
const decisionPayload = shouldLetModelDecideWebSearch ? prepareClaudeWebSearchDecisionPayload(claudePayload) : claudePayload;
|
|
6760
6914
|
const toolNameMapper = createClaudeToolNameMapper(decisionPayload.tools, { ...getToolNameMapperOptionsForModel(upstreamModel) });
|
|
6761
|
-
const openAIPayload = translateToOpenAI(
|
|
6915
|
+
const openAIPayload = translateToOpenAI({
|
|
6916
|
+
...decisionPayload,
|
|
6917
|
+
model: upstreamModel
|
|
6918
|
+
}, void 0, toolNameMapper, { endpoint: translationEndpoint(config, upstreamModel) });
|
|
6762
6919
|
const canStreamWebSearchDecision = shouldLetModelDecideWebSearch && !!writeEvent;
|
|
6763
6920
|
if (shouldLetModelDecideWebSearch && !canStreamWebSearchDecision) openAIPayload.stream = false;
|
|
6764
6921
|
const response = await createChatCompletions(config, openAIPayload, {
|
|
@@ -6920,9 +7077,10 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6920
7077
|
const initiator = c.env?.relayProbe === true ? "agent" : getClaudeRequestInitiator(receivedPayload);
|
|
6921
7078
|
const claudePayload = removeBillingLine(receivedPayload);
|
|
6922
7079
|
const requestSignal = createCopilotRequestSignal(c.req.raw.signal, config.upstreamTimeoutMs);
|
|
7080
|
+
let upstreamModel = "";
|
|
6923
7081
|
try {
|
|
6924
7082
|
getRequestReasoningEffort(claudePayload);
|
|
6925
|
-
|
|
7083
|
+
upstreamModel = translateModelName(claudePayload.model);
|
|
6926
7084
|
const validate = () => {
|
|
6927
7085
|
if (shouldUseNativeMessages(config, upstreamModel)) validateNativeMessages(claudePayload);
|
|
6928
7086
|
else validateClaudeMessages(claudePayload.messages);
|
|
@@ -6932,6 +7090,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6932
7090
|
await ensureCopilotModelCatalog(config, upstreamModel);
|
|
6933
7091
|
requestSignal?.throwIfAborted();
|
|
6934
7092
|
pinCopilotModelCatalog(config);
|
|
7093
|
+
upstreamModel = translateModelName(claudePayload.model);
|
|
6935
7094
|
const selection = requireCopilotEndpoint(config, upstreamModel);
|
|
6936
7095
|
validate();
|
|
6937
7096
|
if (selection.endpoint === "/v1/messages") validateNativeModelEffort(config, upstreamModel, claudePayload);
|
|
@@ -6969,7 +7128,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6969
7128
|
data: JSON.stringify(event)
|
|
6970
7129
|
}));
|
|
6971
7130
|
try {
|
|
6972
|
-
await handleClaudeMessageRequest(config, claudePayload, initiator, requestSignal, requestId, writeEvent, c.req.raw.headers);
|
|
7131
|
+
await handleClaudeMessageRequest(config, claudePayload, upstreamModel, initiator, requestSignal, requestId, writeEvent, c.req.raw.headers);
|
|
6973
7132
|
} catch (error) {
|
|
6974
7133
|
log.error(`request_id=${requestId} Error during Claude stream request:`, error);
|
|
6975
7134
|
if (error instanceof UpstreamToolInputError) trace?.recordFailure("invalid-tool-input");
|
|
@@ -6983,7 +7142,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6983
7142
|
});
|
|
6984
7143
|
}
|
|
6985
7144
|
try {
|
|
6986
|
-
return c.json(await handleClaudeMessageRequest(config, claudePayload, initiator, requestSignal, requestId, void 0, c.req.raw.headers));
|
|
7145
|
+
return c.json(await handleClaudeMessageRequest(config, claudePayload, upstreamModel, initiator, requestSignal, requestId, void 0, c.req.raw.headers));
|
|
6987
7146
|
} catch (error) {
|
|
6988
7147
|
if (error instanceof ProxyNotImplementedError) {
|
|
6989
7148
|
c.set("requestErrorMessage", error.message);
|
|
@@ -7008,18 +7167,18 @@ claudeRoutes.post("/messages/count_tokens", async (c) => {
|
|
|
7008
7167
|
try {
|
|
7009
7168
|
const claudeBeta = c.req.header("claude-beta");
|
|
7010
7169
|
const claudePayload = removeBillingLine(await c.req.json());
|
|
7011
|
-
const
|
|
7170
|
+
const routedModel = translateModelName(claudePayload.model);
|
|
7171
|
+
const openAIPayload = translateToOpenAI(shouldUseNativeMessages(c.get("config"), routedModel) ? {
|
|
7012
7172
|
...claudePayload,
|
|
7013
7173
|
messages: claudePayload.messages.map((message) => {
|
|
7014
7174
|
if (message.role !== "system") return message;
|
|
7015
7175
|
const { output_config: _outputConfig, clear_at: _clearAt,...textMessage } = message;
|
|
7016
7176
|
return textMessage;
|
|
7017
7177
|
})
|
|
7018
|
-
} : claudePayload, void 0, void 0, { endpoint: translationEndpoint(c.get("config"),
|
|
7019
|
-
const exposedModels = getExposedModelIds();
|
|
7178
|
+
} : claudePayload, void 0, void 0, { endpoint: translationEndpoint(c.get("config"), routedModel) });
|
|
7020
7179
|
const upstreamModel = getCachedCopilotModel(c.get("config"), openAIPayload.model);
|
|
7021
7180
|
const hasDiscoveredTokenizer = upstreamModel?.tokenizer !== void 0 && isSupportedTokenizer(upstreamModel.tokenizer);
|
|
7022
|
-
const selectedModel = createTokenCountModel(openAIPayload.model
|
|
7181
|
+
const selectedModel = createTokenCountModel(openAIPayload.model, hasDiscoveredTokenizer ? upstreamModel?.tokenizer : void 0);
|
|
7023
7182
|
const tokenCount = await getTokenCount(openAIPayload, selectedModel);
|
|
7024
7183
|
const effectiveModelId = selectedModel.id;
|
|
7025
7184
|
if (claudePayload.tools && claudePayload.tools.length > 0) {
|
|
@@ -7158,187 +7317,6 @@ const decodeThinking = (encrypted) => {
|
|
|
7158
7317
|
};
|
|
7159
7318
|
};
|
|
7160
7319
|
|
|
7161
|
-
//#endregion
|
|
7162
|
-
//#region src/codex/request.ts
|
|
7163
|
-
const copilotReasoningEfforts = [
|
|
7164
|
-
"none",
|
|
7165
|
-
"minimal",
|
|
7166
|
-
"low",
|
|
7167
|
-
"medium",
|
|
7168
|
-
"high",
|
|
7169
|
-
"xhigh",
|
|
7170
|
-
"max"
|
|
7171
|
-
];
|
|
7172
|
-
const effortOrder = [
|
|
7173
|
-
"none",
|
|
7174
|
-
"minimal",
|
|
7175
|
-
"low",
|
|
7176
|
-
"medium",
|
|
7177
|
-
"high",
|
|
7178
|
-
"xhigh",
|
|
7179
|
-
"max",
|
|
7180
|
-
"ultra"
|
|
7181
|
-
];
|
|
7182
|
-
const isRecord$5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7183
|
-
const codexRequestError = (message, code, status$1 = 400) => new HTTPError(message, Response.json({ error: {
|
|
7184
|
-
message,
|
|
7185
|
-
type: "invalid_request_error",
|
|
7186
|
-
code
|
|
7187
|
-
} }, { status: status$1 }), message);
|
|
7188
|
-
const parseCodexPayload = (text$1) => {
|
|
7189
|
-
let body;
|
|
7190
|
-
try {
|
|
7191
|
-
body = JSON.parse(text$1);
|
|
7192
|
-
} catch {
|
|
7193
|
-
throw codexRequestError("The request body is not valid JSON.", "invalid_json");
|
|
7194
|
-
}
|
|
7195
|
-
if (!isRecord$5(body) || typeof body.model !== "string" || body.model.trim() === "") throw codexRequestError("The request needs a model.", "missing_model");
|
|
7196
|
-
if (typeof body.input !== "string" && !Array.isArray(body.input)) throw codexRequestError("The request needs an input string or array.", "missing_input");
|
|
7197
|
-
return body;
|
|
7198
|
-
};
|
|
7199
|
-
const catalogDescribesUpstream = (config) => config.modelCatalog?.baseUrl === config.copilotBaseUrl;
|
|
7200
|
-
/**
|
|
7201
|
-
* The Copilot model a Codex request is sent to, and the API that serves it.
|
|
7202
|
-
*
|
|
7203
|
-
* A model Copilot's catalog lists is used as named, so Codex's own model IDs reach the same
|
|
7204
|
-
* Copilot model. A GPT model listed with /responses is forwarded there; a Claude model listed with
|
|
7205
|
-
* /v1/messages is translated to Messages, because Copilot serves Claude only there and on Chat
|
|
7206
|
-
* Completions. A name the catalog does not list, such as Codex's internal codex-auto-review, goes
|
|
7207
|
-
* to the configured gptModel, as Claude Code's aliases do. Without a catalog a non-Claude name is
|
|
7208
|
-
* forwarded unchanged.
|
|
7209
|
-
*/
|
|
7210
|
-
const resolveCodexModel = async (config, requested) => {
|
|
7211
|
-
const normalized = normalizeCopilotModelId(requested);
|
|
7212
|
-
await ensureCopilotModelCatalog(config, normalized);
|
|
7213
|
-
pinCopilotModelCatalog(config);
|
|
7214
|
-
if (isClaudeModelId(normalized)) {
|
|
7215
|
-
const capabilities$2 = getCachedCopilotModel(config, normalized);
|
|
7216
|
-
if (capabilities$2 === void 0) throw codexRequestError(`${requested} is not in Copilot's model list. Check copilot-relay models for the exact Claude model ID.`, "relay_unsupported_model");
|
|
7217
|
-
if (capabilities$2.supportedEndpoints !== void 0 && !capabilities$2.supportedEndpoints.includes("/v1/messages")) throw codexRequestError(`${normalized} does not serve Claude Messages on Copilot, which Codex needs for Claude models.`, "relay_unsupported_endpoint");
|
|
7218
|
-
return {
|
|
7219
|
-
model: normalized,
|
|
7220
|
-
api: "messages"
|
|
7221
|
-
};
|
|
7222
|
-
}
|
|
7223
|
-
const model = !(getCachedCopilotModel(config, normalized) !== void 0) && catalogDescribesUpstream(config) ? getModelRouting().gptModel : normalized;
|
|
7224
|
-
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
7225
|
-
const notChat = capabilities$1?.type !== void 0 && capabilities$1.type !== "chat";
|
|
7226
|
-
const noResponses = capabilities$1?.supportedEndpoints !== void 0 && !capabilities$1.supportedEndpoints.includes("/responses");
|
|
7227
|
-
if (notChat || noResponses) throw codexRequestError(`${model} does not serve the Responses API on Copilot. Choose a model that lists /responses in copilot-relay models.`, "relay_unsupported_endpoint");
|
|
7228
|
-
return {
|
|
7229
|
-
model,
|
|
7230
|
-
api: "responses"
|
|
7231
|
-
};
|
|
7232
|
-
};
|
|
7233
|
-
const effortRank = (effort) => effortOrder.indexOf(effort);
|
|
7234
|
-
/**
|
|
7235
|
-
* The reasoning effort to send for a requested one.
|
|
7236
|
-
*
|
|
7237
|
-
* A level the model advertises, or one this relay does not know, is sent as requested. Any other
|
|
7238
|
-
* level becomes the highest advertised level below it, or the lowest advertised level when none is
|
|
7239
|
-
* below it. A model that advertises no levels gets none, since Copilot rejects the field for it.
|
|
7240
|
-
*/
|
|
7241
|
-
const clampReasoningEffort = (requested, advertised) => {
|
|
7242
|
-
if (advertised?.length === 0) return;
|
|
7243
|
-
const accepted = advertised ?? copilotReasoningEfforts;
|
|
7244
|
-
if (accepted.includes(requested) || effortRank(requested) === -1) return requested;
|
|
7245
|
-
const ranked = accepted.filter((effort) => effortRank(effort) !== -1).sort((left, right) => effortRank(left) - effortRank(right));
|
|
7246
|
-
return ranked.filter((effort) => effortRank(effort) <= effortRank(requested)).at(-1) ?? ranked[0];
|
|
7247
|
-
};
|
|
7248
|
-
/**
|
|
7249
|
-
* The body sent to Copilot for a Codex request.
|
|
7250
|
-
*
|
|
7251
|
-
* Copilot answers HTTP 400 to `service_tier`, to an `image_generation` tool, and to an effort it
|
|
7252
|
-
* does not accept, so those change. Reasoning items holding the relay's Claude thinking are removed.
|
|
7253
|
-
* Every other field, including Codex's prompt_cache_key and GPT's encrypted reasoning, is forwarded
|
|
7254
|
-
* unchanged.
|
|
7255
|
-
*/
|
|
7256
|
-
const prepareCodexPayload = (payload, model, capabilities$1) => {
|
|
7257
|
-
const prepared = {
|
|
7258
|
-
...payload,
|
|
7259
|
-
model
|
|
7260
|
-
};
|
|
7261
|
-
const removed$1 = [];
|
|
7262
|
-
if (Object.hasOwn(prepared, "service_tier")) {
|
|
7263
|
-
delete prepared.service_tier;
|
|
7264
|
-
removed$1.push("service_tier");
|
|
7265
|
-
}
|
|
7266
|
-
if (Array.isArray(prepared.input)) {
|
|
7267
|
-
const input = prepared.input.filter((item) => !(isRecord$5(item) && item.type === "reasoning" && isRelayThinking(item.encrypted_content)));
|
|
7268
|
-
if (input.length !== prepared.input.length) {
|
|
7269
|
-
prepared.input = input;
|
|
7270
|
-
removed$1.push("claude_reasoning");
|
|
7271
|
-
}
|
|
7272
|
-
}
|
|
7273
|
-
if (Array.isArray(prepared.tools)) {
|
|
7274
|
-
const tools = prepared.tools.filter((tool) => !(isRecord$5(tool) && tool.type === "image_generation"));
|
|
7275
|
-
if (tools.length !== prepared.tools.length) {
|
|
7276
|
-
prepared.tools = tools;
|
|
7277
|
-
removed$1.push("image_generation");
|
|
7278
|
-
}
|
|
7279
|
-
}
|
|
7280
|
-
const reasoning = isRecord$5(prepared.reasoning) ? prepared.reasoning : void 0;
|
|
7281
|
-
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
7282
|
-
let effectiveEffort = requestedEffort;
|
|
7283
|
-
if (reasoning !== void 0 && requestedEffort !== void 0) {
|
|
7284
|
-
effectiveEffort = clampReasoningEffort(requestedEffort, capabilities$1?.reasoningEfforts);
|
|
7285
|
-
if (effectiveEffort === void 0) {
|
|
7286
|
-
const withoutEffort = { ...reasoning };
|
|
7287
|
-
delete withoutEffort.effort;
|
|
7288
|
-
prepared.reasoning = withoutEffort;
|
|
7289
|
-
} else if (effectiveEffort !== requestedEffort) prepared.reasoning = {
|
|
7290
|
-
...reasoning,
|
|
7291
|
-
effort: effectiveEffort
|
|
7292
|
-
};
|
|
7293
|
-
}
|
|
7294
|
-
return {
|
|
7295
|
-
payload: prepared,
|
|
7296
|
-
removed: removed$1,
|
|
7297
|
-
requestedEffort,
|
|
7298
|
-
effectiveEffort
|
|
7299
|
-
};
|
|
7300
|
-
};
|
|
7301
|
-
/**
|
|
7302
|
-
* Who started a Codex request, for Copilot's x-initiator header.
|
|
7303
|
-
*
|
|
7304
|
-
* A request continues the agent's own work unless its last input item is a user message: after a
|
|
7305
|
-
* tool call the last item is the tool's output. Codex marks its subagents (review, compaction,
|
|
7306
|
-
* memory consolidation, spawned agents) with x-openai-subagent, and those are always the agent.
|
|
7307
|
-
*/
|
|
7308
|
-
const getCodexInitiator = (input, subagent) => {
|
|
7309
|
-
if (subagent !== void 0) return "agent";
|
|
7310
|
-
if (typeof input === "string") return "user";
|
|
7311
|
-
const last = Array.isArray(input) ? input.at(-1) : void 0;
|
|
7312
|
-
return isRecord$5(last) && last.role === "user" && (last.type === void 0 || last.type === "message") ? "user" : "agent";
|
|
7313
|
-
};
|
|
7314
|
-
const includesImage = (value) => {
|
|
7315
|
-
if (Array.isArray(value)) return value.some(includesImage);
|
|
7316
|
-
if (!isRecord$5(value)) return false;
|
|
7317
|
-
return value.type === "input_image" || Object.values(value).some(includesImage);
|
|
7318
|
-
};
|
|
7319
|
-
/**
|
|
7320
|
-
* A stream holding only `response.failed` with code `context_length_exceeded`.
|
|
7321
|
-
*
|
|
7322
|
-
* Copilot reports a prompt over the model's input limit as HTTP 400, which Codex treats as a fatal
|
|
7323
|
-
* error. Codex recognizes a context-window overflow only from this event, and answers it by
|
|
7324
|
-
* compacting the conversation.
|
|
7325
|
-
*/
|
|
7326
|
-
const contextOverflowStream = (requestId, message) => {
|
|
7327
|
-
const event = {
|
|
7328
|
-
type: "response.failed",
|
|
7329
|
-
response: {
|
|
7330
|
-
id: `resp_relay_${requestId}`,
|
|
7331
|
-
object: "response",
|
|
7332
|
-
status: "failed",
|
|
7333
|
-
error: {
|
|
7334
|
-
code: "context_length_exceeded",
|
|
7335
|
-
message
|
|
7336
|
-
}
|
|
7337
|
-
}
|
|
7338
|
-
};
|
|
7339
|
-
return `event: response.failed\ndata: ${JSON.stringify(event)}\n\n`;
|
|
7340
|
-
};
|
|
7341
|
-
|
|
7342
7320
|
//#endregion
|
|
7343
7321
|
//#region src/codex/messages.ts
|
|
7344
7322
|
const claudeReasoningEfforts = [
|
|
@@ -7349,7 +7327,7 @@ const claudeReasoningEfforts = [
|
|
|
7349
7327
|
"max"
|
|
7350
7328
|
];
|
|
7351
7329
|
const defaultMaxTokens = 32e3;
|
|
7352
|
-
const isRecord$
|
|
7330
|
+
const isRecord$5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7353
7331
|
const nameLimit = 64;
|
|
7354
7332
|
/**
|
|
7355
7333
|
* Claude tool names for Codex tools, and the way back.
|
|
@@ -7394,14 +7372,14 @@ const customInputSchema = {
|
|
|
7394
7372
|
};
|
|
7395
7373
|
const customDescription = (tool) => {
|
|
7396
7374
|
const description = typeof tool.description === "string" ? tool.description : "";
|
|
7397
|
-
const format = isRecord$
|
|
7375
|
+
const format = isRecord$5(tool.format) ? tool.format : void 0;
|
|
7398
7376
|
if (format?.type !== "grammar" || typeof format.definition !== "string") return description;
|
|
7399
7377
|
return `${description}\n\nPut the raw input in the input field. It must match this ${String(format.syntax ?? "")} grammar:\n${format.definition}`.trim();
|
|
7400
7378
|
};
|
|
7401
7379
|
const functionTool = (tool, name) => ({
|
|
7402
7380
|
name,
|
|
7403
7381
|
...typeof tool.description === "string" && tool.description && { description: tool.description },
|
|
7404
|
-
input_schema: isRecord$
|
|
7382
|
+
input_schema: isRecord$5(tool.parameters) ? tool.parameters : {
|
|
7405
7383
|
type: "object",
|
|
7406
7384
|
properties: {}
|
|
7407
7385
|
}
|
|
@@ -7428,10 +7406,10 @@ const convertTools = (tools, names, dropped) => {
|
|
|
7428
7406
|
if (!Array.isArray(tools)) return [];
|
|
7429
7407
|
const converted = [];
|
|
7430
7408
|
for (const tool of tools) {
|
|
7431
|
-
if (!isRecord$
|
|
7409
|
+
if (!isRecord$5(tool) || typeof tool.type !== "string") continue;
|
|
7432
7410
|
if (tool.type === "namespace" && typeof tool.name === "string" && Array.isArray(tool.tools)) {
|
|
7433
7411
|
for (const inner of tool.tools) {
|
|
7434
|
-
if (!isRecord$
|
|
7412
|
+
if (!isRecord$5(inner)) continue;
|
|
7435
7413
|
const claudeTool$1 = convertTool(inner, names, tool.name);
|
|
7436
7414
|
if (claudeTool$1) converted.push(claudeTool$1);
|
|
7437
7415
|
else dropped.add(`tool:${tool.name}.${String(inner.type)}`);
|
|
@@ -7470,7 +7448,7 @@ const contentBlocks = (content, allowImages) => {
|
|
|
7470
7448
|
if (!Array.isArray(content)) return [];
|
|
7471
7449
|
const blocks = [];
|
|
7472
7450
|
for (const part of content) {
|
|
7473
|
-
if (!isRecord$
|
|
7451
|
+
if (!isRecord$5(part)) continue;
|
|
7474
7452
|
const text$1 = part.type === "refusal" ? part.refusal : part.text;
|
|
7475
7453
|
if ((part.type === "input_text" || part.type === "output_text" || part.type === "refusal") && typeof text$1 === "string" && text$1) blocks.push({
|
|
7476
7454
|
type: "text",
|
|
@@ -7508,7 +7486,7 @@ const parseArguments = (value) => {
|
|
|
7508
7486
|
if (typeof value !== "string") return {};
|
|
7509
7487
|
try {
|
|
7510
7488
|
const parsed = JSON.parse(value);
|
|
7511
|
-
return isRecord$
|
|
7489
|
+
return isRecord$5(parsed) ? parsed : {};
|
|
7512
7490
|
} catch {
|
|
7513
7491
|
return {};
|
|
7514
7492
|
}
|
|
@@ -7533,7 +7511,7 @@ const toolChoice = (choice, parallel, names) => {
|
|
|
7533
7511
|
let selected;
|
|
7534
7512
|
if (choice === "required") selected = { type: "any" };
|
|
7535
7513
|
else if (choice === "none") selected = { type: "none" };
|
|
7536
|
-
else if (isRecord$
|
|
7514
|
+
else if (isRecord$5(choice) && choice.type === "function" && typeof choice.name === "string") selected = {
|
|
7537
7515
|
type: "tool",
|
|
7538
7516
|
name: names.claudeName({
|
|
7539
7517
|
kind: "function",
|
|
@@ -7545,8 +7523,8 @@ const toolChoice = (choice, parallel, names) => {
|
|
|
7545
7523
|
return selected;
|
|
7546
7524
|
};
|
|
7547
7525
|
const jsonSchemaFormat = (text$1) => {
|
|
7548
|
-
const format = isRecord$
|
|
7549
|
-
if (format?.type !== "json_schema" || !isRecord$
|
|
7526
|
+
const format = isRecord$5(text$1) && isRecord$5(text$1.format) ? text$1.format : void 0;
|
|
7527
|
+
if (format?.type !== "json_schema" || !isRecord$5(format.schema)) return;
|
|
7550
7528
|
return `Your final answer must be a single JSON value that matches this JSON Schema, with no other text and no code fence:\n${JSON.stringify(format.schema)}`;
|
|
7551
7529
|
};
|
|
7552
7530
|
/**
|
|
@@ -7578,7 +7556,7 @@ const toClaudeRequest = (payload, model, capabilities$1) => {
|
|
|
7578
7556
|
content: payload.input
|
|
7579
7557
|
}] : payload.input;
|
|
7580
7558
|
for (const item of items) {
|
|
7581
|
-
if (!isRecord$
|
|
7559
|
+
if (!isRecord$5(item)) continue;
|
|
7582
7560
|
const type = typeof item.type === "string" ? item.type : "message";
|
|
7583
7561
|
if (type === "message" && (item.role === "developer" || item.role === "system")) {
|
|
7584
7562
|
const blocks = contentBlocks(item.content, false);
|
|
@@ -7610,7 +7588,7 @@ const toClaudeRequest = (payload, model, capabilities$1) => {
|
|
|
7610
7588
|
}
|
|
7611
7589
|
for (const turn of turns) if (turn.role === "user") turn.content.sort((left, right) => Number(right.type === "tool_result") - Number(left.type === "tool_result"));
|
|
7612
7590
|
markCacheBreakpoints(system, tools, turns);
|
|
7613
|
-
const reasoning = isRecord$
|
|
7591
|
+
const reasoning = isRecord$5(payload.reasoning) ? payload.reasoning : void 0;
|
|
7614
7592
|
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
7615
7593
|
const advertised = capabilities$1?.reasoningEfforts?.filter((effort) => claudeReasoningEfforts.includes(effort));
|
|
7616
7594
|
const effectiveEffort = requestedEffort === void 0 ? void 0 : clampReasoningEffort(requestedEffort, advertised ?? claudeReasoningEfforts);
|
|
@@ -7641,7 +7619,7 @@ const callItem = (block, status$1) => {
|
|
|
7641
7619
|
let input = block.json;
|
|
7642
7620
|
try {
|
|
7643
7621
|
const parsed = JSON.parse(block.json);
|
|
7644
|
-
if (isRecord$
|
|
7622
|
+
if (isRecord$5(parsed) && typeof parsed.input === "string") input = parsed.input;
|
|
7645
7623
|
} catch {}
|
|
7646
7624
|
return {
|
|
7647
7625
|
type: "custom_tool_call",
|
|
@@ -7952,11 +7930,11 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7952
7930
|
};
|
|
7953
7931
|
}
|
|
7954
7932
|
if (event.type === "message_start") {
|
|
7955
|
-
const message = isRecord$
|
|
7933
|
+
const message = isRecord$5(event.message) ? event.message : {};
|
|
7956
7934
|
if (typeof message.id === "string") responseId = `resp_${message.id}`;
|
|
7957
7935
|
usage$1 = {
|
|
7958
7936
|
...usage$1,
|
|
7959
|
-
...isRecord$
|
|
7937
|
+
...isRecord$5(message.usage) ? message.usage : {}
|
|
7960
7938
|
};
|
|
7961
7939
|
yield {
|
|
7962
7940
|
type: "response.created",
|
|
@@ -7966,7 +7944,7 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7966
7944
|
type: "response.in_progress",
|
|
7967
7945
|
response: response("in_progress")
|
|
7968
7946
|
};
|
|
7969
|
-
} else if (event.type === "content_block_start" && typeof event.index === "number" && isRecord$
|
|
7947
|
+
} else if (event.type === "content_block_start" && typeof event.index === "number" && isRecord$5(event.content_block)) {
|
|
7970
7948
|
const block = newBlock(event.content_block, names, `${responseId}_call_${event.index}`);
|
|
7971
7949
|
if (block?.kind === "call" && !holding) {
|
|
7972
7950
|
holding = true;
|
|
@@ -7984,7 +7962,7 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7984
7962
|
yield* startEvents(placed);
|
|
7985
7963
|
yield* deltaEvents(placed, streamed(placed));
|
|
7986
7964
|
}
|
|
7987
|
-
} else if (event.type === "content_block_delta" && typeof event.index === "number" && isRecord$
|
|
7965
|
+
} else if (event.type === "content_block_delta" && typeof event.index === "number" && isRecord$5(event.delta)) {
|
|
7988
7966
|
const entry = open.get(event.index);
|
|
7989
7967
|
if (entry !== void 0) {
|
|
7990
7968
|
const piece = applyDelta(entry.block, event.delta);
|
|
@@ -7999,10 +7977,10 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7999
7977
|
yield* doneEvents(entry.placed, item);
|
|
8000
7978
|
}
|
|
8001
7979
|
} else if (event.type === "message_delta") {
|
|
8002
|
-
if (isRecord$
|
|
7980
|
+
if (isRecord$5(event.delta) && typeof event.delta.stop_reason === "string") stopReason = event.delta.stop_reason;
|
|
8003
7981
|
usage$1 = {
|
|
8004
7982
|
...usage$1,
|
|
8005
|
-
...isRecord$
|
|
7983
|
+
...isRecord$5(event.usage) ? event.usage : {}
|
|
8006
7984
|
};
|
|
8007
7985
|
} else if (event.type === "message_stop") {
|
|
8008
7986
|
stopped = true;
|
|
@@ -8050,7 +8028,7 @@ const collectCodexResponse = async (events$1) => {
|
|
|
8050
8028
|
message: "Claude stream produced no response."
|
|
8051
8029
|
}
|
|
8052
8030
|
};
|
|
8053
|
-
for await (const event of events$1) if ((event.type === "response.completed" || event.type === "response.incomplete" || event.type === "response.failed") && isRecord$
|
|
8031
|
+
for await (const event of events$1) if ((event.type === "response.completed" || event.type === "response.incomplete" || event.type === "response.failed") && isRecord$5(event.response)) final = event.response;
|
|
8054
8032
|
return final;
|
|
8055
8033
|
};
|
|
8056
8034
|
/** Responses events as SSE bytes. */
|
|
@@ -8060,26 +8038,173 @@ async function* encodeSse(events$1) {
|
|
|
8060
8038
|
}
|
|
8061
8039
|
|
|
8062
8040
|
//#endregion
|
|
8063
|
-
//#region src/
|
|
8064
|
-
const
|
|
8065
|
-
const
|
|
8066
|
-
|
|
8067
|
-
"
|
|
8068
|
-
|
|
8069
|
-
|
|
8070
|
-
|
|
8071
|
-
|
|
8072
|
-
|
|
8073
|
-
|
|
8074
|
-
|
|
8075
|
-
|
|
8076
|
-
|
|
8077
|
-
|
|
8078
|
-
|
|
8079
|
-
return
|
|
8080
|
-
};
|
|
8081
|
-
const
|
|
8082
|
-
|
|
8041
|
+
//#region src/codex/request.ts
|
|
8042
|
+
const isRecord$4 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8043
|
+
const codexRequestError = (message, code, status$1 = 400) => new HTTPError(message, Response.json({ error: {
|
|
8044
|
+
message,
|
|
8045
|
+
type: "invalid_request_error",
|
|
8046
|
+
code
|
|
8047
|
+
} }, { status: status$1 }), message);
|
|
8048
|
+
const parseCodexPayload = (text$1) => {
|
|
8049
|
+
let body;
|
|
8050
|
+
try {
|
|
8051
|
+
body = JSON.parse(text$1);
|
|
8052
|
+
} catch {
|
|
8053
|
+
throw codexRequestError("The request body is not valid JSON.", "invalid_json");
|
|
8054
|
+
}
|
|
8055
|
+
if (!isRecord$4(body) || typeof body.model !== "string" || body.model.trim() === "") throw codexRequestError("The request needs a model.", "missing_model");
|
|
8056
|
+
if (typeof body.input !== "string" && !Array.isArray(body.input)) throw codexRequestError("The request needs an input string or array.", "missing_input");
|
|
8057
|
+
return body;
|
|
8058
|
+
};
|
|
8059
|
+
const catalogDescribesUpstream = (config) => config.modelCatalog?.baseUrl === config.copilotBaseUrl;
|
|
8060
|
+
/**
|
|
8061
|
+
* The Copilot model a Codex request is sent to, and the API that serves it.
|
|
8062
|
+
*
|
|
8063
|
+
* A model Copilot's catalog lists is used as named, so Codex's own model IDs reach the same
|
|
8064
|
+
* Copilot model. A GPT model listed with /responses is forwarded there; a Claude model listed with
|
|
8065
|
+
* /v1/messages is translated to Messages, because Copilot serves Claude only there and on Chat
|
|
8066
|
+
* Completions. A name the catalog does not list, such as Codex's internal codex-auto-review, goes
|
|
8067
|
+
* to the configured gptModel, as Claude Code's aliases do. Without a catalog a non-Claude name is
|
|
8068
|
+
* forwarded unchanged.
|
|
8069
|
+
*/
|
|
8070
|
+
const resolveCodexModel = async (config, requested) => {
|
|
8071
|
+
const normalized = normalizeCopilotModelId(requested);
|
|
8072
|
+
await ensureCopilotModelCatalog(config, normalized);
|
|
8073
|
+
pinCopilotModelCatalog(config);
|
|
8074
|
+
if (isClaudeModelId(normalized)) {
|
|
8075
|
+
const capabilities$2 = getCachedCopilotModel(config, normalized);
|
|
8076
|
+
if (capabilities$2 === void 0) throw codexRequestError(`${requested} is not in Copilot's model list. Check copilot-relay models for the exact Claude model ID.`, "relay_unsupported_model");
|
|
8077
|
+
if (capabilities$2.supportedEndpoints !== void 0 && !capabilities$2.supportedEndpoints.includes("/v1/messages")) throw codexRequestError(`${normalized} does not serve Claude Messages on Copilot, which Codex needs for Claude models.`, "relay_unsupported_endpoint");
|
|
8078
|
+
return {
|
|
8079
|
+
model: normalized,
|
|
8080
|
+
api: "messages"
|
|
8081
|
+
};
|
|
8082
|
+
}
|
|
8083
|
+
const model = !(getCachedCopilotModel(config, normalized) !== void 0) && catalogDescribesUpstream(config) ? getModelRouting().gptModel : normalized;
|
|
8084
|
+
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
8085
|
+
const notChat = capabilities$1?.type !== void 0 && capabilities$1.type !== "chat";
|
|
8086
|
+
const noResponses = capabilities$1?.supportedEndpoints !== void 0 && !capabilities$1.supportedEndpoints.includes("/responses");
|
|
8087
|
+
if (notChat || noResponses) throw codexRequestError(`${model} does not serve the Responses API on Copilot. Choose a model that lists /responses in copilot-relay models.`, "relay_unsupported_endpoint");
|
|
8088
|
+
return {
|
|
8089
|
+
model,
|
|
8090
|
+
api: "responses"
|
|
8091
|
+
};
|
|
8092
|
+
};
|
|
8093
|
+
/**
|
|
8094
|
+
* The body sent to Copilot for a Codex request.
|
|
8095
|
+
*
|
|
8096
|
+
* Copilot answers HTTP 400 to `service_tier`, to an `image_generation` tool, and to an effort it
|
|
8097
|
+
* does not accept, so those change. Reasoning items holding the relay's Claude thinking are removed.
|
|
8098
|
+
* Every other field, including Codex's prompt_cache_key and GPT's encrypted reasoning, is forwarded
|
|
8099
|
+
* unchanged.
|
|
8100
|
+
*/
|
|
8101
|
+
const prepareCodexPayload = (payload, model, capabilities$1) => {
|
|
8102
|
+
const prepared = {
|
|
8103
|
+
...payload,
|
|
8104
|
+
model
|
|
8105
|
+
};
|
|
8106
|
+
const removed$1 = [];
|
|
8107
|
+
if (Object.hasOwn(prepared, "service_tier")) {
|
|
8108
|
+
delete prepared.service_tier;
|
|
8109
|
+
removed$1.push("service_tier");
|
|
8110
|
+
}
|
|
8111
|
+
if (Array.isArray(prepared.input)) {
|
|
8112
|
+
const input = prepared.input.filter((item) => !(isRecord$4(item) && item.type === "reasoning" && isRelayThinking(item.encrypted_content)));
|
|
8113
|
+
if (input.length !== prepared.input.length) {
|
|
8114
|
+
prepared.input = input;
|
|
8115
|
+
removed$1.push("claude_reasoning");
|
|
8116
|
+
}
|
|
8117
|
+
}
|
|
8118
|
+
if (Array.isArray(prepared.tools)) {
|
|
8119
|
+
const tools = prepared.tools.filter((tool) => !(isRecord$4(tool) && tool.type === "image_generation"));
|
|
8120
|
+
if (tools.length !== prepared.tools.length) {
|
|
8121
|
+
prepared.tools = tools;
|
|
8122
|
+
removed$1.push("image_generation");
|
|
8123
|
+
}
|
|
8124
|
+
}
|
|
8125
|
+
const reasoning = isRecord$4(prepared.reasoning) ? prepared.reasoning : void 0;
|
|
8126
|
+
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
8127
|
+
let effectiveEffort = requestedEffort;
|
|
8128
|
+
if (reasoning !== void 0 && requestedEffort !== void 0) {
|
|
8129
|
+
effectiveEffort = clampReasoningEffort(requestedEffort, capabilities$1?.reasoningEfforts);
|
|
8130
|
+
if (effectiveEffort === void 0) {
|
|
8131
|
+
const withoutEffort = { ...reasoning };
|
|
8132
|
+
delete withoutEffort.effort;
|
|
8133
|
+
prepared.reasoning = withoutEffort;
|
|
8134
|
+
} else if (effectiveEffort !== requestedEffort) prepared.reasoning = {
|
|
8135
|
+
...reasoning,
|
|
8136
|
+
effort: effectiveEffort
|
|
8137
|
+
};
|
|
8138
|
+
}
|
|
8139
|
+
return {
|
|
8140
|
+
payload: prepared,
|
|
8141
|
+
removed: removed$1,
|
|
8142
|
+
requestedEffort,
|
|
8143
|
+
effectiveEffort
|
|
8144
|
+
};
|
|
8145
|
+
};
|
|
8146
|
+
/**
|
|
8147
|
+
* Who started a Codex request, for Copilot's x-initiator header.
|
|
8148
|
+
*
|
|
8149
|
+
* A request continues the agent's own work unless its last input item is a user message: after a
|
|
8150
|
+
* tool call the last item is the tool's output. Codex marks its subagents (review, compaction,
|
|
8151
|
+
* memory consolidation, spawned agents) with x-openai-subagent, and those are always the agent.
|
|
8152
|
+
*/
|
|
8153
|
+
const getCodexInitiator = (input, subagent) => {
|
|
8154
|
+
if (subagent !== void 0) return "agent";
|
|
8155
|
+
if (typeof input === "string") return "user";
|
|
8156
|
+
const last = Array.isArray(input) ? input.at(-1) : void 0;
|
|
8157
|
+
return isRecord$4(last) && last.role === "user" && (last.type === void 0 || last.type === "message") ? "user" : "agent";
|
|
8158
|
+
};
|
|
8159
|
+
const includesImage = (value) => {
|
|
8160
|
+
if (Array.isArray(value)) return value.some(includesImage);
|
|
8161
|
+
if (!isRecord$4(value)) return false;
|
|
8162
|
+
return value.type === "input_image" || Object.values(value).some(includesImage);
|
|
8163
|
+
};
|
|
8164
|
+
/**
|
|
8165
|
+
* A stream holding only `response.failed` with code `context_length_exceeded`.
|
|
8166
|
+
*
|
|
8167
|
+
* Copilot reports a prompt over the model's input limit as HTTP 400, which Codex treats as a fatal
|
|
8168
|
+
* error. Codex recognizes a context-window overflow only from this event, and answers it by
|
|
8169
|
+
* compacting the conversation.
|
|
8170
|
+
*/
|
|
8171
|
+
const contextOverflowStream = (requestId, message) => {
|
|
8172
|
+
const event = {
|
|
8173
|
+
type: "response.failed",
|
|
8174
|
+
response: {
|
|
8175
|
+
id: `resp_relay_${requestId}`,
|
|
8176
|
+
object: "response",
|
|
8177
|
+
status: "failed",
|
|
8178
|
+
error: {
|
|
8179
|
+
code: "context_length_exceeded",
|
|
8180
|
+
message
|
|
8181
|
+
}
|
|
8182
|
+
}
|
|
8183
|
+
};
|
|
8184
|
+
return `event: response.failed\ndata: ${JSON.stringify(event)}\n\n`;
|
|
8185
|
+
};
|
|
8186
|
+
|
|
8187
|
+
//#endregion
|
|
8188
|
+
//#region src/routes/responses.ts
|
|
8189
|
+
const responsesRoutes = new Hono();
|
|
8190
|
+
const forwardedHeaders = [
|
|
8191
|
+
"content-type",
|
|
8192
|
+
"retry-after",
|
|
8193
|
+
"openai-model",
|
|
8194
|
+
"x-request-id",
|
|
8195
|
+
"x-github-request-id",
|
|
8196
|
+
"x-copilot-service-request-id"
|
|
8197
|
+
];
|
|
8198
|
+
const copyForwardedHeaders = (source) => {
|
|
8199
|
+
const headers = new Headers();
|
|
8200
|
+
for (const name of forwardedHeaders) {
|
|
8201
|
+
const value = source.get(name);
|
|
8202
|
+
if (value !== null) headers.set(name, value);
|
|
8203
|
+
}
|
|
8204
|
+
return headers;
|
|
8205
|
+
};
|
|
8206
|
+
const readSubagent = (value) => {
|
|
8207
|
+
if (value === void 0) return;
|
|
8083
8208
|
return /^[\w.-]{1,64}$/.test(value) ? value : "other";
|
|
8084
8209
|
};
|
|
8085
8210
|
const sourceOf = (chunks) => ({
|
|
@@ -9073,7 +9198,7 @@ const validateManifest = (value) => {
|
|
|
9073
9198
|
const runtime = value.runtime;
|
|
9074
9199
|
requireValid(record(runtime) && (runtime.debug === void 0 || typeof runtime.debug === "boolean"));
|
|
9075
9200
|
requireValid(runtime.thinkEffort === void 0 || isConfiguredReasoningEffort(runtime.thinkEffort));
|
|
9076
|
-
if (runtime.modelRouting !== void 0) requireValid(record(runtime.modelRouting) && text(runtime.modelRouting.gptModel) && text(runtime.modelRouting.opusModel));
|
|
9201
|
+
if (runtime.modelRouting !== void 0) requireValid(record(runtime.modelRouting) && text(runtime.modelRouting.gptModel) && text(runtime.modelRouting.opusModel) && (runtime.modelRouting.models === void 0 || runtime.modelRouting.models === "auto" || Array.isArray(runtime.modelRouting.models) && runtime.modelRouting.models.every((id) => text(id))));
|
|
9077
9202
|
requireValid(runtime.catalogCurrent === void 0 || typeof runtime.catalogCurrent === "boolean");
|
|
9078
9203
|
requireValid(runtime.catalogAgeMs === void 0 || typeof runtime.catalogAgeMs === "number" && Number.isSafeInteger(runtime.catalogAgeMs) && runtime.catalogAgeMs >= 0);
|
|
9079
9204
|
if (runtime.models !== void 0) {
|
|
@@ -10029,10 +10154,11 @@ async function applyClaudeConfig(input) {
|
|
|
10029
10154
|
const previousBaseUrl = typeof env.ANTHROPIC_BASE_URL === "string" ? env.ANTHROPIC_BASE_URL : void 0;
|
|
10030
10155
|
const hadPrimaryModelOverride = hasPrimaryModelOverride(parsed, env);
|
|
10031
10156
|
const contextWindow = input.gptLimits?.max_context_window_tokens;
|
|
10032
|
-
const
|
|
10157
|
+
const windowOf = (model) => input.contextWindowOf?.(normalizeCopilotModelId(model));
|
|
10158
|
+
const claudeGptModel = normalizeClaudeModelId(gptModel, windowOf(gptModel) ?? contextWindow);
|
|
10033
10159
|
const shouldSeedTopLevelModel = !hadPrimaryModelOverride;
|
|
10034
|
-
const nextModel = typeof parsed.model === "string" ? normalizeClaudeModelId(parsed.model,
|
|
10035
|
-
for (const key of knownModelEnvKeys) if (typeof env[key] === "string") env[key] = normalizeClaudeModelId(env[key],
|
|
10160
|
+
const nextModel = typeof parsed.model === "string" ? normalizeClaudeModelId(parsed.model, windowOf(parsed.model)) : void 0;
|
|
10161
|
+
for (const key of knownModelEnvKeys) if (typeof env[key] === "string") env[key] = normalizeClaudeModelId(env[key], windowOf(env[key]));
|
|
10036
10162
|
env.ANTHROPIC_BASE_URL = baseUrl;
|
|
10037
10163
|
if (input.apiKey) env.ANTHROPIC_AUTH_TOKEN = input.apiKey;
|
|
10038
10164
|
else if (typeof env.ANTHROPIC_AUTH_TOKEN !== "string" || !env.ANTHROPIC_AUTH_TOKEN) env.ANTHROPIC_AUTH_TOKEN = "dummy";
|
|
@@ -10430,308 +10556,42 @@ const applyCodexSetup = async (input) => {
|
|
|
10430
10556
|
};
|
|
10431
10557
|
|
|
10432
10558
|
//#endregion
|
|
10433
|
-
//#region src/lib/
|
|
10434
|
-
const
|
|
10435
|
-
const
|
|
10436
|
-
|
|
10437
|
-
|
|
10438
|
-
|
|
10439
|
-
|
|
10440
|
-
const catalog = await loadCopilotModelCatalog(config);
|
|
10441
|
-
const requiredModels = getUpstreamModelIds();
|
|
10442
|
-
const missingModels = requiredModels.filter((model) => !catalog.models.has(model));
|
|
10443
|
-
if (missingModels.length > 0) throw new Error(`Required Copilot model(s) unavailable upstream: ${missingModels.join(", ")}`);
|
|
10444
|
-
log.info(`Upstream models available: ${requiredModels.join(", ")}`);
|
|
10445
|
-
for (const model of requiredModels) {
|
|
10446
|
-
const limits = catalog.models.get(model)?.limits;
|
|
10447
|
-
if (limits) log.info(`Model token limits: model=${model} context=${limits.max_context_window_tokens} input=${limits.max_prompt_tokens} output=${limits.max_output_tokens} non_streaming_output=${limits.max_non_streaming_output_tokens ?? limits.max_output_tokens}`);
|
|
10448
|
-
else log.error(`Model token limits unavailable: model=${model}; preserving client budgets without assuming a capacity.`);
|
|
10449
|
-
}
|
|
10450
|
-
};
|
|
10451
|
-
const createProbePayload = (model) => ({
|
|
10452
|
-
model,
|
|
10453
|
-
max_tokens: 16,
|
|
10454
|
-
stream: false,
|
|
10455
|
-
messages: [{
|
|
10456
|
-
role: "user",
|
|
10457
|
-
content: "Reply with OK only."
|
|
10458
|
-
}]
|
|
10559
|
+
//#region src/lib/claude-app.ts
|
|
10560
|
+
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10561
|
+
const configIdPattern = /^[a-f0-9-]{36}$/;
|
|
10562
|
+
const currentPlatform = () => ({
|
|
10563
|
+
platform: process.platform,
|
|
10564
|
+
env: process.env,
|
|
10565
|
+
home: os.homedir()
|
|
10459
10566
|
});
|
|
10460
|
-
|
|
10461
|
-
|
|
10462
|
-
|
|
10463
|
-
|
|
10464
|
-
|
|
10465
|
-
|
|
10466
|
-
|
|
10467
|
-
|
|
10468
|
-
timeoutMs: config.upstreamTimeoutMs
|
|
10469
|
-
});
|
|
10470
|
-
if (typeof response !== "object" || response === null || !("choices" in response)) throw new Error(`Preflight request for ${model} unexpectedly streamed`);
|
|
10471
|
-
}
|
|
10472
|
-
log.info(`Preflight OK: model=${model} think_effort=${effortLabel}`);
|
|
10473
|
-
} catch (error) {
|
|
10474
|
-
if (error instanceof HTTPError) {
|
|
10475
|
-
const responseBody = await error.response.text().catch(() => "");
|
|
10476
|
-
throw new Error(`Preflight failed for model=${model} think_effort=${effortLabel}: ${error.response.status} ${error.response.statusText}${responseBody ? ` ${responseBody}` : ""}`);
|
|
10477
|
-
}
|
|
10478
|
-
throw error;
|
|
10567
|
+
/**
|
|
10568
|
+
* The app's third-party data directory: Electron's userData for "Claude" with "-3p" appended, or
|
|
10569
|
+
* %LOCALAPPDATA%\Claude-3p on Windows.
|
|
10570
|
+
*/
|
|
10571
|
+
const claudeAppDataDir = ({ platform, env, home } = currentPlatform()) => {
|
|
10572
|
+
if (platform === "win32") {
|
|
10573
|
+
const localAppData = env.LOCALAPPDATA || path.win32.join(home, "AppData", "Local");
|
|
10574
|
+
return path.win32.join(localAppData, "Claude-3p");
|
|
10479
10575
|
}
|
|
10576
|
+
if (platform === "darwin") return path.posix.join(home, "Library", "Application Support", "Claude-3p");
|
|
10577
|
+
const configHome = env.XDG_CONFIG_HOME || path.posix.join(home, ".config");
|
|
10578
|
+
return path.posix.join(configHome, "Claude-3p");
|
|
10480
10579
|
};
|
|
10481
|
-
const
|
|
10482
|
-
|
|
10483
|
-
|
|
10484
|
-
|
|
10485
|
-
|
|
10486
|
-
|
|
10487
|
-
|
|
10488
|
-
|
|
10489
|
-
|
|
10580
|
+
const standardDataDir = (dataDir) => dataDir.replace(/-3p$/, "");
|
|
10581
|
+
const otherVendorMarker = /ark-code|astron|command-r|deepseek|doubao|gemini|gemma|glm|gpt|grok|hermes|hy3|kimi|lfm|\bling\b|llama|longcat|mimo|minimax|mistral|mixtral|moonshot|nemotron|openai|phi-|qianfan|qwen|tc-code|\bunic\b|yi-|stepfun|step-3|seed-|bytedance|hunyuan|granite|amazon\.nova|nova-|devstral|ministral|ernie|codex|arcee|trinity|abab|phi\d|\bk2\.|\bm2\.|jamba|arctic|solar|mercury|zamba|kat-coder|\bds-|dpsk/;
|
|
10582
|
+
const looksAnthropic = (id) => {
|
|
10583
|
+
const lower = id.toLowerCase();
|
|
10584
|
+
return !otherVendorMarker.test(lower) && [
|
|
10585
|
+
"claude",
|
|
10586
|
+
"anthropic",
|
|
10587
|
+
"opus",
|
|
10588
|
+
"sonnet",
|
|
10589
|
+
"haiku",
|
|
10590
|
+
"fable",
|
|
10591
|
+
"mythos"
|
|
10592
|
+
].some((word) => lower.includes(word));
|
|
10490
10593
|
};
|
|
10491
|
-
|
|
10492
|
-
//#endregion
|
|
10493
|
-
//#region src/start.ts
|
|
10494
|
-
const canCloseConnections = (server) => typeof server.closeIdleConnections === "function" && typeof server.closeAllConnections === "function";
|
|
10495
|
-
/**
|
|
10496
|
-
* The startup warning for a listener that other machines may reach without a key.
|
|
10497
|
-
*
|
|
10498
|
-
* A warning rather than a refusal, so an existing `host: 0.0.0.0` setup keeps
|
|
10499
|
-
* working after an upgrade. It names the apiKey setting and never prints a key.
|
|
10500
|
-
*/
|
|
10501
|
-
const unauthenticatedListenerWarning = (config) => {
|
|
10502
|
-
if (config.apiKey || isLoopbackHost(config.host)) return;
|
|
10503
|
-
return terminalText(`apiKey is empty and host ${config.host} is not a loopback address: any client that can reach port ${config.port} can consume your Copilot usage. Set apiKey in ${paths.configPath}, or bind host to 127.0.0.1.`);
|
|
10504
|
-
};
|
|
10505
|
-
/**
|
|
10506
|
-
* The warning for a config.yaml that other local users can access while it holds the apiKey.
|
|
10507
|
-
*
|
|
10508
|
-
* A new config.yaml is created 0600, but an existing one keeps its mode, and the relay never
|
|
10509
|
-
* changes the mode of the user's own file. stat follows a symlink, so the mode checked is the
|
|
10510
|
-
* target's. Windows has no POSIX mode bits. It never prints a key.
|
|
10511
|
-
*/
|
|
10512
|
-
const exposedConfigWarning = (config, configPath$1 = paths.configPath) => {
|
|
10513
|
-
if (!config.apiKey || process.platform === "win32") return;
|
|
10514
|
-
let mode;
|
|
10515
|
-
try {
|
|
10516
|
-
mode = statSync(configPath$1).mode & 511;
|
|
10517
|
-
} catch {
|
|
10518
|
-
return;
|
|
10519
|
-
}
|
|
10520
|
-
if ((mode & 63) === 0) return;
|
|
10521
|
-
return terminalText(`${configPath$1} holds the apiKey and has mode ${mode.toString(8).padStart(4, "0")}, which lets other local users access it. Run chmod 600 ${configPath$1}.`);
|
|
10522
|
-
};
|
|
10523
|
-
async function startRelay(appConfig) {
|
|
10524
|
-
appConfig ??= await readAppConfig();
|
|
10525
|
-
appConfig = {
|
|
10526
|
-
...appConfig,
|
|
10527
|
-
thinkEffort: normalizeThinkEffort(appConfig.thinkEffort) ?? defaultReasoningEffort
|
|
10528
|
-
};
|
|
10529
|
-
setLogLevel(appConfig.logLevel);
|
|
10530
|
-
await cleanupLogs(appConfig.logRetentionDays);
|
|
10531
|
-
await cleanupCaptures(appConfig.logRetentionDays);
|
|
10532
|
-
configureUpstreamDispatcher(appConfig.upstreamProxy);
|
|
10533
|
-
const claudeConfigPath = getClaudeConfigPath();
|
|
10534
|
-
const config = readProxyConfig(appConfig);
|
|
10535
|
-
const applyRuntimeConfig = (nextConfig) => {
|
|
10536
|
-
registerSensitiveOrigin(nextConfig.copilotBaseUrl);
|
|
10537
|
-
registerLogSecret(nextConfig.apiKey);
|
|
10538
|
-
setLogLevel(nextConfig.logLevel);
|
|
10539
|
-
cleanupLogs(nextConfig.logRetentionDays).catch(() => log.error("Log retention failed; existing logs retained."));
|
|
10540
|
-
cleanupCaptures(nextConfig.logRetentionDays);
|
|
10541
|
-
if (nextConfig.logLevel === "debug" && !runtimeState.debug) log.info("Debug capture stores full request and response bodies privately; prompts may contain secrets. Do not share captures without review.");
|
|
10542
|
-
runtimeState.debug = nextConfig.logLevel === "debug";
|
|
10543
|
-
runtimeState.thinkEffort = nextConfig.thinkEffort;
|
|
10544
|
-
config.copilotBaseUrl = nextConfig.copilotBaseUrl;
|
|
10545
|
-
runtimeState.upstreamBaseUrl = nextConfig.copilotBaseUrl;
|
|
10546
|
-
config.host = nextConfig.host;
|
|
10547
|
-
config.port = nextConfig.port;
|
|
10548
|
-
config.upstreamTimeoutMs = nextConfig.upstreamTimeoutSeconds * 1e3;
|
|
10549
|
-
config.webSearchBackend = nextConfig.webSearchBackend;
|
|
10550
|
-
config.claudeUpstreamApi = nextConfig.claudeUpstreamApi;
|
|
10551
|
-
config.apiKey = nextConfig.apiKey;
|
|
10552
|
-
runtimeState.modelRouting = {
|
|
10553
|
-
gptModel: nextConfig.gptModel,
|
|
10554
|
-
opusModel: nextConfig.opusModel
|
|
10555
|
-
};
|
|
10556
|
-
};
|
|
10557
|
-
applyRuntimeConfig(appConfig);
|
|
10558
|
-
log.info(`Log level: ${appConfig.logLevel}`);
|
|
10559
|
-
log.info(`Default think effort: ${appConfig.thinkEffort}`);
|
|
10560
|
-
log.info(`Upstream timeout: ${appConfig.upstreamTimeoutSeconds}s`);
|
|
10561
|
-
if (appConfig.upstreamProxy !== void 0) log.info(`Upstream proxy: ${formatUpstreamProxyForDisplay(appConfig.upstreamProxy)}`);
|
|
10562
|
-
const listenerWarning = unauthenticatedListenerWarning(appConfig);
|
|
10563
|
-
if (listenerWarning) log.error(listenerWarning);
|
|
10564
|
-
const configWarning = exposedConfigWarning(appConfig);
|
|
10565
|
-
if (configWarning) log.error(configWarning);
|
|
10566
|
-
const loginLookup = new AbortController();
|
|
10567
|
-
const authSession = await setupProxyAuth(config, { loginSignal: loginLookup.signal });
|
|
10568
|
-
try {
|
|
10569
|
-
await validateUpstream(config, appConfig.thinkEffort);
|
|
10570
|
-
} catch (error) {
|
|
10571
|
-
log.error("Startup preflight failed:", error);
|
|
10572
|
-
process.exit(1);
|
|
10573
|
-
}
|
|
10574
|
-
log.info(`Exposed models: ${getExposedModelIds().join(", ")}`);
|
|
10575
|
-
try {
|
|
10576
|
-
await preloadTokenizers(getUpstreamModelIds().map((id) => getCachedCopilotModel(config, id)?.tokenizer));
|
|
10577
|
-
log.info(`Tokenizers loaded: ${loadedTokenizers().join(", ")}`);
|
|
10578
|
-
} catch (error) {
|
|
10579
|
-
log.error("Tokenizer preload failed; count_tokens loads tokenizers on first use:", error);
|
|
10580
|
-
}
|
|
10581
|
-
const server = await startServer(config);
|
|
10582
|
-
await writeRelayPidFile(config);
|
|
10583
|
-
log.info(`copilot-relay version: ${appVersion}`);
|
|
10584
|
-
authSession.githubLogin.then((githubLogin) => {
|
|
10585
|
-
if (githubLogin) log.info(`GitHub user: ${githubLogin}`);
|
|
10586
|
-
else log.error("GitHub user: unavailable");
|
|
10587
|
-
});
|
|
10588
|
-
const baseUrl = getRelayBaseUrl(config.host, config.port);
|
|
10589
|
-
log.info(`copilot-relay listening on ${baseUrl}`);
|
|
10590
|
-
log.info(`copilot base url: ${formatUrlForDisplay(config.copilotBaseUrl)}`);
|
|
10591
|
-
if (appConfig.claudeSetup) try {
|
|
10592
|
-
const gptLimits = getCachedCopilotModel(config, appConfig.gptModel)?.limits;
|
|
10593
|
-
const opusLimits = getCachedCopilotModel(config, appConfig.opusModel)?.limits;
|
|
10594
|
-
const claudeResult = await applyClaudeConfig({
|
|
10595
|
-
apiKey: appConfig.apiKey,
|
|
10596
|
-
baseUrl,
|
|
10597
|
-
configPath: claudeConfigPath,
|
|
10598
|
-
gptModel: appConfig.gptModel,
|
|
10599
|
-
gptLimits,
|
|
10600
|
-
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
10601
|
-
});
|
|
10602
|
-
if (claudeResult.changed) {
|
|
10603
|
-
log.info(`claude settings ${claudeResult.created ? "created" : "updated"}: ${claudeResult.configPath}`);
|
|
10604
|
-
if (claudeResult.previousBaseUrl && claudeResult.previousBaseUrl !== baseUrl) log.info(`ANTHROPIC_BASE_URL: ${claudeResult.previousBaseUrl} → ${baseUrl}`);
|
|
10605
|
-
} else log.info(`claude settings already up to date: ${claudeResult.configPath}`);
|
|
10606
|
-
} catch (error) {
|
|
10607
|
-
log.error(`Could not update claude settings (${claudeConfigPath}):`, error);
|
|
10608
|
-
}
|
|
10609
|
-
if (appConfig.codexSetup) {
|
|
10610
|
-
const codexHome = getCodexHome();
|
|
10611
|
-
try {
|
|
10612
|
-
const codexResult = await applyCodexSetup({
|
|
10613
|
-
apiKey: appConfig.apiKey,
|
|
10614
|
-
baseUrl,
|
|
10615
|
-
catalogPath: paths.codexCatalogPath,
|
|
10616
|
-
codexHome,
|
|
10617
|
-
models: [appConfig.gptModel, appConfig.opusModel].map((id) => ({
|
|
10618
|
-
id,
|
|
10619
|
-
capabilities: getCachedCopilotModel(config, id)
|
|
10620
|
-
}))
|
|
10621
|
-
});
|
|
10622
|
-
if (codexResult.skipped) log.info(`codex setup skipped: ${codexHome} does not exist`);
|
|
10623
|
-
else if (codexResult.changed) log.info(`codex settings updated: ${codexResult.configPath}`);
|
|
10624
|
-
else log.info(`codex settings already up to date: ${codexResult.configPath}`);
|
|
10625
|
-
} catch (error) {
|
|
10626
|
-
log.error(`Could not update codex settings (${codexHome}):`, error);
|
|
10627
|
-
}
|
|
10628
|
-
}
|
|
10629
|
-
const configWatcher = watchAppConfig((nextConfig) => {
|
|
10630
|
-
const previousApiKey = config.apiKey;
|
|
10631
|
-
applyRuntimeConfig(nextConfig);
|
|
10632
|
-
log.info(`Config reloaded: logLevel=${nextConfig.logLevel} thinkEffort=${nextConfig.thinkEffort} upstreamTimeoutSeconds=${nextConfig.upstreamTimeoutSeconds}`);
|
|
10633
|
-
const reloadedConfigWarning = nextConfig.apiKey === previousApiKey ? void 0 : exposedConfigWarning(nextConfig);
|
|
10634
|
-
if (reloadedConfigWarning) log.error(reloadedConfigWarning);
|
|
10635
|
-
});
|
|
10636
|
-
const shutdownGraceMs = 2e3;
|
|
10637
|
-
const shutdown = (signal) => {
|
|
10638
|
-
log.info(`Received ${signal}; shutting down copilot-relay`);
|
|
10639
|
-
loginLookup.abort();
|
|
10640
|
-
server.close((error) => {
|
|
10641
|
-
if (error) {
|
|
10642
|
-
log.error("Error while shutting down copilot-relay:", error);
|
|
10643
|
-
process.exitCode = 1;
|
|
10644
|
-
}
|
|
10645
|
-
});
|
|
10646
|
-
if (canCloseConnections(server)) {
|
|
10647
|
-
server.closeIdleConnections();
|
|
10648
|
-
setTimeout(() => {
|
|
10649
|
-
log.info("Grace period elapsed; closing remaining connections");
|
|
10650
|
-
server.closeAllConnections();
|
|
10651
|
-
}, shutdownGraceMs).unref();
|
|
10652
|
-
}
|
|
10653
|
-
};
|
|
10654
|
-
process.once("SIGINT", shutdown);
|
|
10655
|
-
process.once("SIGTERM", shutdown);
|
|
10656
|
-
try {
|
|
10657
|
-
await new Promise((resolve$1, reject) => {
|
|
10658
|
-
server.on("close", resolve$1);
|
|
10659
|
-
server.on("error", reject);
|
|
10660
|
-
});
|
|
10661
|
-
} finally {
|
|
10662
|
-
process.off("SIGINT", shutdown);
|
|
10663
|
-
process.off("SIGTERM", shutdown);
|
|
10664
|
-
clearInterval(configWatcher);
|
|
10665
|
-
await clearRelayPidFile();
|
|
10666
|
-
await flushCaptures();
|
|
10667
|
-
await flushLogs();
|
|
10668
|
-
}
|
|
10669
|
-
}
|
|
10670
|
-
const start = defineCommand({
|
|
10671
|
-
meta: {
|
|
10672
|
-
name: "start",
|
|
10673
|
-
description: "Start the copilot-relay HTTP server."
|
|
10674
|
-
},
|
|
10675
|
-
async run() {
|
|
10676
|
-
await startRelay();
|
|
10677
|
-
}
|
|
10678
|
-
});
|
|
10679
|
-
|
|
10680
|
-
//#endregion
|
|
10681
|
-
//#region src/restart.ts
|
|
10682
|
-
const restart = defineCommand({
|
|
10683
|
-
meta: {
|
|
10684
|
-
name: "restart",
|
|
10685
|
-
description: "Stop any existing copilot-relay instance and start a new one."
|
|
10686
|
-
},
|
|
10687
|
-
async run() {
|
|
10688
|
-
const appConfig = await readAppConfig();
|
|
10689
|
-
setLogLevel(appConfig.logLevel);
|
|
10690
|
-
await cleanupLogs(appConfig.logRetentionDays);
|
|
10691
|
-
const stopped = await stopExistingRelay(readProxyConfig(appConfig));
|
|
10692
|
-
if (stopped.length > 0) log.info(`Stopped copilot-relay pid(s): ${stopped.join(", ")}`);
|
|
10693
|
-
log.info("Starting copilot-relay");
|
|
10694
|
-
await startRelay(appConfig);
|
|
10695
|
-
}
|
|
10696
|
-
});
|
|
10697
|
-
|
|
10698
|
-
//#endregion
|
|
10699
|
-
//#region src/lib/claude-app.ts
|
|
10700
|
-
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10701
|
-
const configIdPattern = /^[a-f0-9-]{36}$/;
|
|
10702
|
-
const currentPlatform = () => ({
|
|
10703
|
-
platform: process.platform,
|
|
10704
|
-
env: process.env,
|
|
10705
|
-
home: os.homedir()
|
|
10706
|
-
});
|
|
10707
|
-
/**
|
|
10708
|
-
* The app's third-party data directory: Electron's userData for "Claude" with "-3p" appended, or
|
|
10709
|
-
* %LOCALAPPDATA%\Claude-3p on Windows.
|
|
10710
|
-
*/
|
|
10711
|
-
const claudeAppDataDir = ({ platform, env, home } = currentPlatform()) => {
|
|
10712
|
-
if (platform === "win32") {
|
|
10713
|
-
const localAppData = env.LOCALAPPDATA || path.win32.join(home, "AppData", "Local");
|
|
10714
|
-
return path.win32.join(localAppData, "Claude-3p");
|
|
10715
|
-
}
|
|
10716
|
-
if (platform === "darwin") return path.posix.join(home, "Library", "Application Support", "Claude-3p");
|
|
10717
|
-
const configHome = env.XDG_CONFIG_HOME || path.posix.join(home, ".config");
|
|
10718
|
-
return path.posix.join(configHome, "Claude-3p");
|
|
10719
|
-
};
|
|
10720
|
-
const standardDataDir = (dataDir) => dataDir.replace(/-3p$/, "");
|
|
10721
|
-
const otherVendorMarker = /ark-code|astron|command-r|deepseek|doubao|gemini|gemma|glm|gpt|grok|hermes|hy3|kimi|lfm|\bling\b|llama|longcat|mimo|minimax|mistral|mixtral|moonshot|nemotron|openai|phi-|qianfan|qwen|tc-code|\bunic\b|yi-|stepfun|step-3|seed-|bytedance|hunyuan|granite|amazon\.nova|nova-|devstral|ministral|ernie|codex|arcee|trinity|abab|phi\d|\bk2\.|\bm2\.|jamba|arctic|solar|mercury|zamba|kat-coder|\bds-|dpsk/;
|
|
10722
|
-
const looksAnthropic = (id) => {
|
|
10723
|
-
const lower = id.toLowerCase();
|
|
10724
|
-
return !otherVendorMarker.test(lower) && [
|
|
10725
|
-
"claude",
|
|
10726
|
-
"anthropic",
|
|
10727
|
-
"opus",
|
|
10728
|
-
"sonnet",
|
|
10729
|
-
"haiku",
|
|
10730
|
-
"fable",
|
|
10731
|
-
"mythos"
|
|
10732
|
-
].some((word) => lower.includes(word));
|
|
10733
|
-
};
|
|
10734
|
-
const routesToTier = (name, tier) => name.toLowerCase().includes("opus") === (tier === "opus");
|
|
10594
|
+
const routesToTier = (name, tier) => name.toLowerCase().includes("opus") === (tier === "opus");
|
|
10735
10595
|
/**
|
|
10736
10596
|
* The name the app sends for a model: its own ID when the app accepts it and the relay routes that
|
|
10737
10597
|
* ID to the model's own slot, otherwise a Claude-style route for its tier, such as
|
|
@@ -10840,6 +10700,11 @@ const applyClaudeAppSetup = async (input) => {
|
|
|
10840
10700
|
};
|
|
10841
10701
|
};
|
|
10842
10702
|
|
|
10703
|
+
//#endregion
|
|
10704
|
+
//#region src/lib/defaults.ts
|
|
10705
|
+
const getClaudeConfigPath = () => path.join(process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), ".claude"), "settings.json");
|
|
10706
|
+
const getCodexHome = () => process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
|
|
10707
|
+
|
|
10843
10708
|
//#endregion
|
|
10844
10709
|
//#region src/lib/client-setup.ts
|
|
10845
10710
|
const setupTargets = [
|
|
@@ -10867,6 +10732,7 @@ const setupClaudeCodeCli = async (context) => {
|
|
|
10867
10732
|
configPath: configPath$1,
|
|
10868
10733
|
gptModel: appConfig.gptModel,
|
|
10869
10734
|
gptLimits,
|
|
10735
|
+
contextWindowOf: (model) => capabilities(context, model)?.limits?.max_context_window_tokens,
|
|
10870
10736
|
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
10871
10737
|
});
|
|
10872
10738
|
if (result.skippedReason) return {
|
|
@@ -10880,6 +10746,15 @@ const setupClaudeCodeCli = async (context) => {
|
|
|
10880
10746
|
status: result.changed ? "updated" : "already up to date"
|
|
10881
10747
|
};
|
|
10882
10748
|
};
|
|
10749
|
+
/** Codex's models: the exposed upstream IDs without [1m], gptModel first as Codex's default. */
|
|
10750
|
+
const codexModels = (appConfig, catalog) => resolveExposedModels({
|
|
10751
|
+
gptModel: appConfig.gptModel,
|
|
10752
|
+
opusModel: appConfig.opusModel,
|
|
10753
|
+
models: parseModelSelection(appConfig.models)
|
|
10754
|
+
}, catalog?.models).map((id) => ({
|
|
10755
|
+
id,
|
|
10756
|
+
capabilities: catalog?.models.get(id)
|
|
10757
|
+
}));
|
|
10883
10758
|
const setupCodex = async (context, target) => {
|
|
10884
10759
|
const { appConfig } = context;
|
|
10885
10760
|
const result = await applyCodexSetup({
|
|
@@ -10887,10 +10762,7 @@ const setupCodex = async (context, target) => {
|
|
|
10887
10762
|
baseUrl: context.baseUrl,
|
|
10888
10763
|
catalogPath: paths.codexCatalogPath,
|
|
10889
10764
|
codexHome: getCodexHome(),
|
|
10890
|
-
models:
|
|
10891
|
-
id,
|
|
10892
|
-
capabilities: capabilities(context, id)
|
|
10893
|
-
})),
|
|
10765
|
+
models: codexModels(appConfig, context.catalog),
|
|
10894
10766
|
force: context.force
|
|
10895
10767
|
});
|
|
10896
10768
|
if (result.skipped) return {
|
|
@@ -10904,6 +10776,26 @@ const setupCodex = async (context, target) => {
|
|
|
10904
10776
|
status: result.changed ? "updated" : "already up to date"
|
|
10905
10777
|
};
|
|
10906
10778
|
};
|
|
10779
|
+
/**
|
|
10780
|
+
* `start`'s Codex setup when codexSetup is on: the same setup as `copilot-relay setup`, never forced,
|
|
10781
|
+
* so a machine where Codex has not run is skipped. A failure is logged rather than stopping the relay.
|
|
10782
|
+
*/
|
|
10783
|
+
const startupCodexSetup = async (appConfig, catalog, baseUrl) => {
|
|
10784
|
+
const codexHome = getCodexHome();
|
|
10785
|
+
try {
|
|
10786
|
+
const outcome = await setupCodex({
|
|
10787
|
+
appConfig,
|
|
10788
|
+
baseUrl,
|
|
10789
|
+
catalog,
|
|
10790
|
+
force: false
|
|
10791
|
+
}, "codex");
|
|
10792
|
+
if (outcome.status.startsWith("skipped")) log.info(`codex setup skipped: ${codexHome} does not exist`);
|
|
10793
|
+
else if (outcome.status === "updated") log.info(`codex settings updated: ${outcome.path}`);
|
|
10794
|
+
else log.info(`codex settings already up to date: ${outcome.path}`);
|
|
10795
|
+
} catch (error) {
|
|
10796
|
+
log.error(`Could not update codex settings (${codexHome}):`, error);
|
|
10797
|
+
}
|
|
10798
|
+
};
|
|
10907
10799
|
const setupClaudeCodeApp = async (context) => {
|
|
10908
10800
|
const { appConfig } = context;
|
|
10909
10801
|
const result = await applyClaudeAppSetup({
|
|
@@ -10956,6 +10848,255 @@ const runSetup = async (targets, context) => {
|
|
|
10956
10848
|
return outcomes;
|
|
10957
10849
|
};
|
|
10958
10850
|
|
|
10851
|
+
//#endregion
|
|
10852
|
+
//#region src/lib/preflight.ts
|
|
10853
|
+
const ensureRequiredModels = async (config) => {
|
|
10854
|
+
const catalog = await loadCopilotModelCatalog(config);
|
|
10855
|
+
const requiredModels = getUpstreamModelIds();
|
|
10856
|
+
const missingModels = requiredModels.filter((model) => !catalog.models.has(model));
|
|
10857
|
+
if (missingModels.length > 0) throw new Error(`Required Copilot model(s) unavailable upstream: ${missingModels.join(", ")}`);
|
|
10858
|
+
log.info(`Upstream models available: ${requiredModels.join(", ")}`);
|
|
10859
|
+
const missingSelected = findMissingModels(getModelRouting(), catalog.models);
|
|
10860
|
+
if (missingSelected.length > 0) log.error(`Selected models not in Copilot's catalog, skipped: ${missingSelected.join(", ")}`);
|
|
10861
|
+
for (const model of requiredModels) {
|
|
10862
|
+
const limits = catalog.models.get(model)?.limits;
|
|
10863
|
+
if (limits) log.info(`Model token limits: model=${model} context=${limits.max_context_window_tokens} input=${limits.max_prompt_tokens} output=${limits.max_output_tokens} non_streaming_output=${limits.max_non_streaming_output_tokens ?? limits.max_output_tokens}`);
|
|
10864
|
+
else log.error(`Model token limits unavailable: model=${model}; preserving client budgets without assuming a capacity.`);
|
|
10865
|
+
}
|
|
10866
|
+
};
|
|
10867
|
+
const createProbePayload = (model) => ({
|
|
10868
|
+
model,
|
|
10869
|
+
max_tokens: 16,
|
|
10870
|
+
stream: false,
|
|
10871
|
+
messages: [{
|
|
10872
|
+
role: "user",
|
|
10873
|
+
content: "Reply with OK only."
|
|
10874
|
+
}]
|
|
10875
|
+
});
|
|
10876
|
+
const validateModelRequest = async (config, model) => {
|
|
10877
|
+
const effortLabel = resolveModelReasoningEffort(config, model) ?? "omitted";
|
|
10878
|
+
try {
|
|
10879
|
+
if (requireCopilotEndpoint(config, model).endpoint === "/v1/messages") await handleNativeMessages(config, createProbePayload(model), { requestId: randomUUID() });
|
|
10880
|
+
else {
|
|
10881
|
+
const response = await createChatCompletions(config, createProbePayload(model), {
|
|
10882
|
+
client: "generic",
|
|
10883
|
+
requestedModel: model,
|
|
10884
|
+
timeoutMs: config.upstreamTimeoutMs
|
|
10885
|
+
});
|
|
10886
|
+
if (typeof response !== "object" || response === null || !("choices" in response)) throw new Error(`Preflight request for ${model} unexpectedly streamed`);
|
|
10887
|
+
}
|
|
10888
|
+
log.info(`Preflight OK: model=${model} think_effort=${effortLabel}`);
|
|
10889
|
+
} catch (error) {
|
|
10890
|
+
if (error instanceof HTTPError) {
|
|
10891
|
+
const responseBody = await error.response.text().catch(() => "");
|
|
10892
|
+
throw new Error(`Preflight failed for model=${model} think_effort=${effortLabel}: ${error.response.status} ${error.response.statusText}${responseBody ? ` ${responseBody}` : ""}`);
|
|
10893
|
+
}
|
|
10894
|
+
throw error;
|
|
10895
|
+
}
|
|
10896
|
+
};
|
|
10897
|
+
const validateUpstream = async (config, thinkEffort) => {
|
|
10898
|
+
log.info("Running upstream preflight");
|
|
10899
|
+
await ensureRequiredModels(config);
|
|
10900
|
+
await withRuntimeState({
|
|
10901
|
+
...snapshotRuntimeState(),
|
|
10902
|
+
thinkEffort
|
|
10903
|
+
}, async () => {
|
|
10904
|
+
for (const model of getUpstreamModelIds()) await validateModelRequest(config, model);
|
|
10905
|
+
});
|
|
10906
|
+
};
|
|
10907
|
+
|
|
10908
|
+
//#endregion
|
|
10909
|
+
//#region src/start.ts
|
|
10910
|
+
const canCloseConnections = (server) => typeof server.closeIdleConnections === "function" && typeof server.closeAllConnections === "function";
|
|
10911
|
+
/**
|
|
10912
|
+
* The startup warning for a listener that other machines may reach without a key.
|
|
10913
|
+
*
|
|
10914
|
+
* A warning rather than a refusal, so an existing `host: 0.0.0.0` setup keeps
|
|
10915
|
+
* working after an upgrade. It names the apiKey setting and never prints a key.
|
|
10916
|
+
*/
|
|
10917
|
+
const unauthenticatedListenerWarning = (config) => {
|
|
10918
|
+
if (config.apiKey || isLoopbackHost(config.host)) return;
|
|
10919
|
+
return terminalText(`apiKey is empty and host ${config.host} is not a loopback address: any client that can reach port ${config.port} can consume your Copilot usage. Set apiKey in ${paths.configPath}, or bind host to 127.0.0.1.`);
|
|
10920
|
+
};
|
|
10921
|
+
/**
|
|
10922
|
+
* The warning for a config.yaml that other local users can access while it holds the apiKey.
|
|
10923
|
+
*
|
|
10924
|
+
* A new config.yaml is created 0600, but an existing one keeps its mode, and the relay never
|
|
10925
|
+
* changes the mode of the user's own file. stat follows a symlink, so the mode checked is the
|
|
10926
|
+
* target's. Windows has no POSIX mode bits. It never prints a key.
|
|
10927
|
+
*/
|
|
10928
|
+
const exposedConfigWarning = (config, configPath$1 = paths.configPath) => {
|
|
10929
|
+
if (!config.apiKey || process.platform === "win32") return;
|
|
10930
|
+
let mode;
|
|
10931
|
+
try {
|
|
10932
|
+
mode = statSync(configPath$1).mode & 511;
|
|
10933
|
+
} catch {
|
|
10934
|
+
return;
|
|
10935
|
+
}
|
|
10936
|
+
if ((mode & 63) === 0) return;
|
|
10937
|
+
return terminalText(`${configPath$1} holds the apiKey and has mode ${mode.toString(8).padStart(4, "0")}, which lets other local users access it. Run chmod 600 ${configPath$1}.`);
|
|
10938
|
+
};
|
|
10939
|
+
async function startRelay(appConfig) {
|
|
10940
|
+
appConfig ??= await readAppConfig();
|
|
10941
|
+
appConfig = {
|
|
10942
|
+
...appConfig,
|
|
10943
|
+
thinkEffort: normalizeThinkEffort(appConfig.thinkEffort) ?? defaultReasoningEffort
|
|
10944
|
+
};
|
|
10945
|
+
setLogLevel(appConfig.logLevel);
|
|
10946
|
+
await cleanupLogs(appConfig.logRetentionDays);
|
|
10947
|
+
await cleanupCaptures(appConfig.logRetentionDays);
|
|
10948
|
+
configureUpstreamDispatcher(appConfig.upstreamProxy);
|
|
10949
|
+
const claudeConfigPath = getClaudeConfigPath();
|
|
10950
|
+
const config = readProxyConfig(appConfig);
|
|
10951
|
+
const applyRuntimeConfig = (nextConfig) => {
|
|
10952
|
+
registerSensitiveOrigin(nextConfig.copilotBaseUrl);
|
|
10953
|
+
registerLogSecret(nextConfig.apiKey);
|
|
10954
|
+
setLogLevel(nextConfig.logLevel);
|
|
10955
|
+
cleanupLogs(nextConfig.logRetentionDays).catch(() => log.error("Log retention failed; existing logs retained."));
|
|
10956
|
+
cleanupCaptures(nextConfig.logRetentionDays);
|
|
10957
|
+
if (nextConfig.logLevel === "debug" && !runtimeState.debug) log.info("Debug capture stores full request and response bodies privately; prompts may contain secrets. Do not share captures without review.");
|
|
10958
|
+
runtimeState.debug = nextConfig.logLevel === "debug";
|
|
10959
|
+
runtimeState.thinkEffort = nextConfig.thinkEffort;
|
|
10960
|
+
config.copilotBaseUrl = nextConfig.copilotBaseUrl;
|
|
10961
|
+
runtimeState.upstreamBaseUrl = nextConfig.copilotBaseUrl;
|
|
10962
|
+
config.host = nextConfig.host;
|
|
10963
|
+
config.port = nextConfig.port;
|
|
10964
|
+
config.upstreamTimeoutMs = nextConfig.upstreamTimeoutSeconds * 1e3;
|
|
10965
|
+
config.webSearchBackend = nextConfig.webSearchBackend;
|
|
10966
|
+
config.claudeUpstreamApi = nextConfig.claudeUpstreamApi;
|
|
10967
|
+
config.apiKey = nextConfig.apiKey;
|
|
10968
|
+
runtimeState.modelRouting = {
|
|
10969
|
+
gptModel: nextConfig.gptModel,
|
|
10970
|
+
opusModel: nextConfig.opusModel,
|
|
10971
|
+
models: parseModelSelection(nextConfig.models)
|
|
10972
|
+
};
|
|
10973
|
+
};
|
|
10974
|
+
applyRuntimeConfig(appConfig);
|
|
10975
|
+
log.info(`Log level: ${appConfig.logLevel}`);
|
|
10976
|
+
log.info(`Default think effort: ${appConfig.thinkEffort}`);
|
|
10977
|
+
log.info(`Upstream timeout: ${appConfig.upstreamTimeoutSeconds}s`);
|
|
10978
|
+
if (appConfig.upstreamProxy !== void 0) log.info(`Upstream proxy: ${formatUpstreamProxyForDisplay(appConfig.upstreamProxy)}`);
|
|
10979
|
+
const listenerWarning = unauthenticatedListenerWarning(appConfig);
|
|
10980
|
+
if (listenerWarning) log.error(listenerWarning);
|
|
10981
|
+
const configWarning = exposedConfigWarning(appConfig);
|
|
10982
|
+
if (configWarning) log.error(configWarning);
|
|
10983
|
+
const loginLookup = new AbortController();
|
|
10984
|
+
const authSession = await setupProxyAuth(config, { loginSignal: loginLookup.signal });
|
|
10985
|
+
try {
|
|
10986
|
+
await validateUpstream(config, appConfig.thinkEffort);
|
|
10987
|
+
} catch (error) {
|
|
10988
|
+
log.error("Startup preflight failed:", error);
|
|
10989
|
+
process.exit(1);
|
|
10990
|
+
}
|
|
10991
|
+
log.info(`Exposed models: ${getExposedModelIds().join(", ")}`);
|
|
10992
|
+
try {
|
|
10993
|
+
await preloadTokenizers(getUpstreamModelIds().map((id) => getCachedCopilotModel(config, id)?.tokenizer));
|
|
10994
|
+
log.info(`Tokenizers loaded: ${loadedTokenizers().join(", ")}`);
|
|
10995
|
+
} catch (error) {
|
|
10996
|
+
log.error("Tokenizer preload failed; count_tokens loads tokenizers on first use:", error);
|
|
10997
|
+
}
|
|
10998
|
+
const server = await startServer(config);
|
|
10999
|
+
await writeRelayPidFile(config);
|
|
11000
|
+
log.info(`copilot-relay version: ${appVersion}`);
|
|
11001
|
+
authSession.githubLogin.then((githubLogin) => {
|
|
11002
|
+
if (githubLogin) log.info(`GitHub user: ${githubLogin}`);
|
|
11003
|
+
else log.error("GitHub user: unavailable");
|
|
11004
|
+
});
|
|
11005
|
+
const baseUrl = getRelayBaseUrl(config.host, config.port);
|
|
11006
|
+
log.info(`copilot-relay listening on ${baseUrl}`);
|
|
11007
|
+
log.info(`copilot base url: ${formatUrlForDisplay(config.copilotBaseUrl)}`);
|
|
11008
|
+
if (appConfig.claudeSetup) try {
|
|
11009
|
+
const gptLimits = getCachedCopilotModel(config, appConfig.gptModel)?.limits;
|
|
11010
|
+
const opusLimits = getCachedCopilotModel(config, appConfig.opusModel)?.limits;
|
|
11011
|
+
const claudeResult = await applyClaudeConfig({
|
|
11012
|
+
apiKey: appConfig.apiKey,
|
|
11013
|
+
baseUrl,
|
|
11014
|
+
configPath: claudeConfigPath,
|
|
11015
|
+
gptModel: appConfig.gptModel,
|
|
11016
|
+
gptLimits,
|
|
11017
|
+
contextWindowOf: (model) => getCachedCopilotModel(config, model)?.limits?.max_context_window_tokens,
|
|
11018
|
+
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
11019
|
+
});
|
|
11020
|
+
if (claudeResult.changed) {
|
|
11021
|
+
log.info(`claude settings ${claudeResult.created ? "created" : "updated"}: ${claudeResult.configPath}`);
|
|
11022
|
+
if (claudeResult.previousBaseUrl && claudeResult.previousBaseUrl !== baseUrl) log.info(`ANTHROPIC_BASE_URL: ${claudeResult.previousBaseUrl} → ${baseUrl}`);
|
|
11023
|
+
} else log.info(`claude settings already up to date: ${claudeResult.configPath}`);
|
|
11024
|
+
} catch (error) {
|
|
11025
|
+
log.error(`Could not update claude settings (${claudeConfigPath}):`, error);
|
|
11026
|
+
}
|
|
11027
|
+
if (appConfig.codexSetup) {
|
|
11028
|
+
const catalog = config.modelCatalog?.baseUrl === config.copilotBaseUrl ? config.modelCatalog : void 0;
|
|
11029
|
+
await startupCodexSetup(appConfig, catalog, baseUrl);
|
|
11030
|
+
}
|
|
11031
|
+
const configWatcher = watchAppConfig((nextConfig) => {
|
|
11032
|
+
const previousApiKey = config.apiKey;
|
|
11033
|
+
applyRuntimeConfig(nextConfig);
|
|
11034
|
+
log.info(`Config reloaded: logLevel=${nextConfig.logLevel} thinkEffort=${nextConfig.thinkEffort} upstreamTimeoutSeconds=${nextConfig.upstreamTimeoutSeconds}`);
|
|
11035
|
+
const reloadedConfigWarning = nextConfig.apiKey === previousApiKey ? void 0 : exposedConfigWarning(nextConfig);
|
|
11036
|
+
if (reloadedConfigWarning) log.error(reloadedConfigWarning);
|
|
11037
|
+
});
|
|
11038
|
+
const shutdownGraceMs = 2e3;
|
|
11039
|
+
const shutdown = (signal) => {
|
|
11040
|
+
log.info(`Received ${signal}; shutting down copilot-relay`);
|
|
11041
|
+
loginLookup.abort();
|
|
11042
|
+
server.close((error) => {
|
|
11043
|
+
if (error) {
|
|
11044
|
+
log.error("Error while shutting down copilot-relay:", error);
|
|
11045
|
+
process.exitCode = 1;
|
|
11046
|
+
}
|
|
11047
|
+
});
|
|
11048
|
+
if (canCloseConnections(server)) {
|
|
11049
|
+
server.closeIdleConnections();
|
|
11050
|
+
setTimeout(() => {
|
|
11051
|
+
log.info("Grace period elapsed; closing remaining connections");
|
|
11052
|
+
server.closeAllConnections();
|
|
11053
|
+
}, shutdownGraceMs).unref();
|
|
11054
|
+
}
|
|
11055
|
+
};
|
|
11056
|
+
process.once("SIGINT", shutdown);
|
|
11057
|
+
process.once("SIGTERM", shutdown);
|
|
11058
|
+
try {
|
|
11059
|
+
await new Promise((resolve$1, reject) => {
|
|
11060
|
+
server.on("close", resolve$1);
|
|
11061
|
+
server.on("error", reject);
|
|
11062
|
+
});
|
|
11063
|
+
} finally {
|
|
11064
|
+
process.off("SIGINT", shutdown);
|
|
11065
|
+
process.off("SIGTERM", shutdown);
|
|
11066
|
+
clearInterval(configWatcher);
|
|
11067
|
+
await clearRelayPidFile();
|
|
11068
|
+
await flushCaptures();
|
|
11069
|
+
await flushLogs();
|
|
11070
|
+
}
|
|
11071
|
+
}
|
|
11072
|
+
const start = defineCommand({
|
|
11073
|
+
meta: {
|
|
11074
|
+
name: "start",
|
|
11075
|
+
description: "Start the copilot-relay HTTP server."
|
|
11076
|
+
},
|
|
11077
|
+
async run() {
|
|
11078
|
+
await startRelay();
|
|
11079
|
+
}
|
|
11080
|
+
});
|
|
11081
|
+
|
|
11082
|
+
//#endregion
|
|
11083
|
+
//#region src/restart.ts
|
|
11084
|
+
const restart = defineCommand({
|
|
11085
|
+
meta: {
|
|
11086
|
+
name: "restart",
|
|
11087
|
+
description: "Stop any existing copilot-relay instance and start a new one."
|
|
11088
|
+
},
|
|
11089
|
+
async run() {
|
|
11090
|
+
const appConfig = await readAppConfig();
|
|
11091
|
+
setLogLevel(appConfig.logLevel);
|
|
11092
|
+
await cleanupLogs(appConfig.logRetentionDays);
|
|
11093
|
+
const stopped = await stopExistingRelay(readProxyConfig(appConfig));
|
|
11094
|
+
if (stopped.length > 0) log.info(`Stopped copilot-relay pid(s): ${stopped.join(", ")}`);
|
|
11095
|
+
log.info("Starting copilot-relay");
|
|
11096
|
+
await startRelay(appConfig);
|
|
11097
|
+
}
|
|
11098
|
+
});
|
|
11099
|
+
|
|
10959
11100
|
//#endregion
|
|
10960
11101
|
//#region src/setup.ts
|
|
10961
11102
|
const printable = (text$1) => scrubLogSecrets(terminalText(text$1));
|
|
@@ -11118,13 +11259,15 @@ const configRowOrder = {
|
|
|
11118
11259
|
webSearchBackend: 11,
|
|
11119
11260
|
gptModel: 12,
|
|
11120
11261
|
opusModel: 13,
|
|
11121
|
-
|
|
11262
|
+
models: 14,
|
|
11263
|
+
claudeUpstreamApi: 15
|
|
11122
11264
|
};
|
|
11123
11265
|
const configRowKeys = Object.keys(configRowOrder).sort((a, b) => configRowOrder[a] - configRowOrder[b]);
|
|
11124
11266
|
const unsetConfigText = {
|
|
11125
11267
|
apiKey: "(unset — clients need no key)",
|
|
11126
11268
|
upstreamProxy: "(unset — connects directly)",
|
|
11127
|
-
webSearchBackend: "(unset — uses gptModel)"
|
|
11269
|
+
webSearchBackend: "(unset — uses gptModel)",
|
|
11270
|
+
models: "(empty — gptModel and opusModel only)"
|
|
11128
11271
|
};
|
|
11129
11272
|
/**
|
|
11130
11273
|
* The resolved config, one key per line.
|