@nick3/copilot-api 1.4.6 → 1.5.2
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/dist/{account-DhQb2A6q.js → account-CipKmikF.js} +2 -2
- package/dist/account-CipKmikF.js.map +1 -0
- package/dist/{accounts-manager-B8Y2Y4ab.js → accounts-manager-DhwGrhHF.js} +40 -43
- package/dist/accounts-manager-DhwGrhHF.js.map +1 -0
- package/dist/{accounts-registry-c7rs5Ed9.js → accounts-registry-CQYvRe65.js} +3 -3
- package/dist/{accounts-registry-c7rs5Ed9.js.map → accounts-registry-CQYvRe65.js.map} +1 -1
- package/dist/admin/assets/{index-BFvCJZIK.js → index-B-G_GrPI.js} +7 -7
- package/dist/admin/index.html +1 -1
- package/dist/{auth-Bt0FGr8C.js → auth-BtUQnU2m.js} +6 -6
- package/dist/{auth-Bt0FGr8C.js.map → auth-BtUQnU2m.js.map} +1 -1
- package/dist/{check-usage-By-sJOch.js → check-usage-DiomC9Dl.js} +6 -5
- package/dist/check-usage-DiomC9Dl.js.map +1 -0
- package/dist/{debug-hQJWwXtC.js → debug-BzR5ZQUk.js} +3 -3
- package/dist/{debug-hQJWwXtC.js.map → debug-BzR5ZQUk.js.map} +1 -1
- package/dist/{get-copilot-token-Q4FXtOE9.js → get-copilot-token-DN1P6qll.js} +3 -3
- package/dist/{get-copilot-token-Q4FXtOE9.js.map → get-copilot-token-DN1P6qll.js.map} +1 -1
- package/dist/main.js +6 -7
- package/dist/main.js.map +1 -1
- package/dist/{paths-DoT4SZ8f.js → paths-Cvzy-eLX.js} +2 -2
- package/dist/{paths-DoT4SZ8f.js.map → paths-Cvzy-eLX.js.map} +1 -1
- package/dist/{poll-access-token-Cp_DfHGH.js → poll-access-token-BrRUFB1F.js} +3 -3
- package/dist/{poll-access-token-Cp_DfHGH.js.map → poll-access-token-BrRUFB1F.js.map} +1 -1
- package/dist/{server-Cln-GCLU.js → server-D9M_wFyO.js} +86 -105
- package/dist/server-D9M_wFyO.js.map +1 -0
- package/dist/{start-DH-QnZxL.js → start-D3yEfZnW.js} +15 -17
- package/dist/{start-DH-QnZxL.js.map → start-D3yEfZnW.js.map} +1 -1
- package/dist/{utils-SV0FyqXk.js → utils-53JWve7i.js} +52 -19
- package/dist/utils-53JWve7i.js.map +1 -0
- package/package.json +3 -1
- package/dist/account-DhQb2A6q.js.map +0 -1
- package/dist/accounts-manager-B8Y2Y4ab.js.map +0 -1
- package/dist/check-usage-By-sJOch.js.map +0 -1
- package/dist/server-Cln-GCLU.js.map +0 -1
- package/dist/utils-SV0FyqXk.js.map +0 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { PATHS } from "./paths-
|
|
2
|
-
import { listAccountsFromRegistry } from "./accounts-registry-
|
|
3
|
-
import {
|
|
4
|
-
import "./get-copilot-token-
|
|
5
|
-
import {
|
|
1
|
+
import { t as PATHS } from "./paths-Cvzy-eLX.js";
|
|
2
|
+
import { i as listAccountsFromRegistry } from "./accounts-registry-CQYvRe65.js";
|
|
3
|
+
import { C as prepareInteractionHeaders, D as state, E as accountFromState, S as prepareForCompact, T as resolveTraceId, _ as copilotHeaders, a as generateRequestIdFromPayload, c as isNullish, g as copilotBaseUrl, h as forwardError, l as parseUserIdMetadata, m as HTTPError, n as cacheModels, o as getRootSessionId, p as getCopilotUsage, s as getUUID, u as sleep, w as requestContext } from "./utils-53JWve7i.js";
|
|
4
|
+
import "./get-copilot-token-DN1P6qll.js";
|
|
5
|
+
import { _ as isMessagesApiEnabled, a as getClaudeTokenMultiplier, b as mergeConfigWithDefaults, c as getModelAliases, d as getProviderConfig, f as getReasoningEffortForModel, g as isMessageStartInputTokensFallbackEnabled, h as isForceAgentEnabled, i as getAnthropicApiKey, l as getModelAliasesInfo, m as isAccountAffinityEnabled, n as PROVIDER_TYPE_ANTHROPIC, o as getConfig, p as getSmallModel, r as getAliasTargetSet, s as getExtraPromptForModel, t as accountsManager, u as getModelRefreshIntervalMs, v as isResponsesApiContextManagementModel, x as shouldCompactUseSmallModel, y as isResponsesApiWebSearchEnabled } from "./accounts-manager-DhwGrhHF.js";
|
|
6
6
|
import consola from "consola";
|
|
7
7
|
import fs, { readFile } from "node:fs/promises";
|
|
8
8
|
import * as path$1 from "node:path";
|
|
@@ -12,7 +12,6 @@ import { Hono } from "hono";
|
|
|
12
12
|
import { cors } from "hono/cors";
|
|
13
13
|
import { logger } from "hono/logger";
|
|
14
14
|
import fs$1, { existsSync } from "node:fs";
|
|
15
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
16
15
|
import { Database } from "bun:sqlite";
|
|
17
16
|
import { fileURLToPath } from "node:url";
|
|
18
17
|
import { streamSSE } from "hono/streaming";
|
|
@@ -105,26 +104,6 @@ function createAuthMiddleware(options = {}) {
|
|
|
105
104
|
};
|
|
106
105
|
}
|
|
107
106
|
|
|
108
|
-
//#endregion
|
|
109
|
-
//#region src/lib/request-context.ts
|
|
110
|
-
const TRACE_ID_MAX_LENGTH = 64;
|
|
111
|
-
const TRACE_ID_PATTERN = /^\w[\w.-]*$/;
|
|
112
|
-
const asyncLocalStorage = new AsyncLocalStorage();
|
|
113
|
-
const requestContext = {
|
|
114
|
-
getStore: () => asyncLocalStorage.getStore(),
|
|
115
|
-
run: (context, callback) => asyncLocalStorage.run(context, callback)
|
|
116
|
-
};
|
|
117
|
-
function generateTraceId() {
|
|
118
|
-
const timestamp = Date.now().toString(36);
|
|
119
|
-
const random = Math.random().toString(36).slice(2, 8);
|
|
120
|
-
return `${timestamp}-${random}`;
|
|
121
|
-
}
|
|
122
|
-
function resolveTraceId(traceId) {
|
|
123
|
-
const candidate = traceId?.trim();
|
|
124
|
-
if (!candidate || candidate.length > TRACE_ID_MAX_LENGTH || !TRACE_ID_PATTERN.test(candidate)) return generateTraceId();
|
|
125
|
-
return candidate;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
107
|
//#endregion
|
|
129
108
|
//#region src/lib/trace.ts
|
|
130
109
|
const traceIdMiddleware = async (c, next) => {
|
|
@@ -132,7 +111,8 @@ const traceIdMiddleware = async (c, next) => {
|
|
|
132
111
|
c.header("x-trace-id", traceId);
|
|
133
112
|
const context = {
|
|
134
113
|
traceId,
|
|
135
|
-
startTime: Date.now()
|
|
114
|
+
startTime: Date.now(),
|
|
115
|
+
userAgent: c.req.header("user-agent") || ""
|
|
136
116
|
};
|
|
137
117
|
await requestContext.run(context, async () => {
|
|
138
118
|
await next();
|
|
@@ -377,14 +357,11 @@ function normalizeMessagesUsage(usage) {
|
|
|
377
357
|
const output = usage.output_tokens;
|
|
378
358
|
const hasInput = typeof input === "number";
|
|
379
359
|
const hasOutput = typeof output === "number";
|
|
380
|
-
const tokensInput = hasInput ? Math.max(0, input - cached) : void 0;
|
|
381
|
-
const tokensOutput = hasOutput ? output : void 0;
|
|
382
|
-
const tokensTotal = hasInput || hasOutput ? (input ?? 0) + (output ?? 0) : void 0;
|
|
383
360
|
return {
|
|
384
361
|
tokensCachedInput: cached,
|
|
385
|
-
tokensInput,
|
|
386
|
-
tokensOutput,
|
|
387
|
-
tokensTotal,
|
|
362
|
+
tokensInput: hasInput ? Math.max(0, input - cached) : void 0,
|
|
363
|
+
tokensOutput: hasOutput ? output : void 0,
|
|
364
|
+
tokensTotal: hasInput || hasOutput ? (input ?? 0) + (output ?? 0) : void 0,
|
|
388
365
|
usageJson: JSON.stringify(usage)
|
|
389
366
|
};
|
|
390
367
|
}
|
|
@@ -598,10 +575,9 @@ var RequestHistoryStore = class {
|
|
|
598
575
|
const rows = this.db.query(sql).all(...values, limit + 1);
|
|
599
576
|
const items = rows.slice(0, limit);
|
|
600
577
|
const hasMore = rows.length > limit;
|
|
601
|
-
const nextCursorId = hasMore ? items.at(-1)?.id : void 0;
|
|
602
578
|
return {
|
|
603
579
|
items,
|
|
604
|
-
nextCursorId,
|
|
580
|
+
nextCursorId: hasMore ? items.at(-1)?.id : void 0,
|
|
605
581
|
hasMore
|
|
606
582
|
};
|
|
607
583
|
}
|
|
@@ -1309,12 +1285,10 @@ function parseAdminModelDetailsItem(raw, aliasesByTarget) {
|
|
|
1309
1285
|
if (!isPlainObject(raw)) return null;
|
|
1310
1286
|
const id = parseNonEmptyString(raw.id);
|
|
1311
1287
|
if (!id) return null;
|
|
1312
|
-
const name = parseNonEmptyString(raw.name) ?? id;
|
|
1313
|
-
const preview = toBooleanOrUndefined(raw.preview) ?? false;
|
|
1314
1288
|
return {
|
|
1315
1289
|
id,
|
|
1316
|
-
name,
|
|
1317
|
-
preview,
|
|
1290
|
+
name: parseNonEmptyString(raw.name) ?? id,
|
|
1291
|
+
preview: toBooleanOrUndefined(raw.preview) ?? false,
|
|
1318
1292
|
billing: parseBilling(raw.billing),
|
|
1319
1293
|
supported_endpoints: parseStringArray(raw.supported_endpoints),
|
|
1320
1294
|
capabilities: parseCapabilities(raw.capabilities),
|
|
@@ -2081,6 +2055,7 @@ function toAccountContext(account) {
|
|
|
2081
2055
|
return {
|
|
2082
2056
|
githubToken: account.githubToken,
|
|
2083
2057
|
copilotToken: account.copilotToken,
|
|
2058
|
+
...account.copilotApiUrl !== void 0 ? { copilotApiUrl: account.copilotApiUrl } : {},
|
|
2084
2059
|
accountType: account.accountType,
|
|
2085
2060
|
vsCodeVersion: account.vsCodeVersion
|
|
2086
2061
|
};
|
|
@@ -2207,8 +2182,7 @@ const createHandlerLogger = (name) => {
|
|
|
2207
2182
|
const filePath = path.join(LOG_DIR, `${sanitizedName}-${dateKey}.log`);
|
|
2208
2183
|
const message = formatArgs(logObj.args);
|
|
2209
2184
|
const traceIdStr = traceId ? ` [${traceId}]` : "";
|
|
2210
|
-
|
|
2211
|
-
appendLine(filePath, line);
|
|
2185
|
+
appendLine(filePath, `[${timestamp}] [${logObj.type}] [${logObj.tag || name}]${traceIdStr}${message ? ` ${message}` : ""}`);
|
|
2212
2186
|
} });
|
|
2213
2187
|
return instance;
|
|
2214
2188
|
};
|
|
@@ -2440,8 +2414,7 @@ const numTokensForTools = (tools, encoder, constants) => {
|
|
|
2440
2414
|
* Calculate the token count of messages, supporting multiple GPT encoders
|
|
2441
2415
|
*/
|
|
2442
2416
|
const getTokenCount = async (payload, model) => {
|
|
2443
|
-
const
|
|
2444
|
-
const encoder = await getEncodeChatFunction(tokenizer);
|
|
2417
|
+
const encoder = await getEncodeChatFunction(getTokenizerFromModel(model));
|
|
2445
2418
|
const simplifiedMessages = payload.messages;
|
|
2446
2419
|
const inputMessages = simplifiedMessages.filter((msg) => msg.role !== "assistant");
|
|
2447
2420
|
const outputMessages = simplifiedMessages.filter((msg) => msg.role === "assistant");
|
|
@@ -2532,13 +2505,11 @@ async function handleCompletion$1(c) {
|
|
|
2532
2505
|
});
|
|
2533
2506
|
}
|
|
2534
2507
|
logger$6.debug("Request payload:", JSON.stringify(payload).slice(-400));
|
|
2508
|
+
const upstreamRequestId = generateRequestIdFromPayload(payload, normalizedPromptCacheKey);
|
|
2535
2509
|
const selection = await accountsManager.selectAccountForRequest([{
|
|
2536
2510
|
modelId: clientModel,
|
|
2537
2511
|
endpoint: CHAT_COMPLETIONS_ENDPOINT$1
|
|
2538
|
-
}], {
|
|
2539
|
-
promptCacheKey: normalizedPromptCacheKey,
|
|
2540
|
-
safetyIdentifier: normalizedSafetyIdentifier
|
|
2541
|
-
});
|
|
2512
|
+
}], { requestId: upstreamRequestId });
|
|
2542
2513
|
if (!selection.ok) {
|
|
2543
2514
|
recordSelectionFailure$2(store, {
|
|
2544
2515
|
request,
|
|
@@ -2567,7 +2538,6 @@ async function handleCompletion$1(c) {
|
|
|
2567
2538
|
if (state.manualApprove) await awaitApproval();
|
|
2568
2539
|
const payloadWithMaxTokens = applyDefaultMaxTokens(upstreamPayload, selectedModel);
|
|
2569
2540
|
const accountCtx = toAccountContext(account);
|
|
2570
|
-
const upstreamRequestId = generateRequestIdFromPayload(payloadWithMaxTokens);
|
|
2571
2541
|
const upstreamSessionId = getUUID(upstreamRequestId);
|
|
2572
2542
|
request.upstreamRequestId = upstreamRequestId;
|
|
2573
2543
|
request.upstreamSessionId = upstreamSessionId;
|
|
@@ -2600,7 +2570,6 @@ function buildRequestContext$1(c) {
|
|
|
2600
2570
|
const method = c.req.raw.method;
|
|
2601
2571
|
const path$2 = new URL(c.req.url, "http://local").pathname;
|
|
2602
2572
|
const { ip: clientIp, source: clientIpSource } = getClientIpInfo(c);
|
|
2603
|
-
const userAgent = c.req.header("user-agent") ?? void 0;
|
|
2604
2573
|
return {
|
|
2605
2574
|
requestId,
|
|
2606
2575
|
startedAtMs,
|
|
@@ -2608,7 +2577,7 @@ function buildRequestContext$1(c) {
|
|
|
2608
2577
|
path: path$2,
|
|
2609
2578
|
clientIp,
|
|
2610
2579
|
clientIpSource,
|
|
2611
|
-
userAgent
|
|
2580
|
+
userAgent: c.req.header("user-agent") ?? void 0
|
|
2612
2581
|
};
|
|
2613
2582
|
}
|
|
2614
2583
|
function insertRequestLog$2(store, request, record) {
|
|
@@ -2954,7 +2923,6 @@ embeddingRoutes.post("/", async (c) => {
|
|
|
2954
2923
|
const method = c.req.raw.method;
|
|
2955
2924
|
const path$2 = new URL(c.req.url, "http://local").pathname;
|
|
2956
2925
|
const { ip: clientIp, source: clientIpSource } = getClientIpInfo(c);
|
|
2957
|
-
const userAgent = c.req.header("user-agent") ?? void 0;
|
|
2958
2926
|
const ctx = {
|
|
2959
2927
|
requestId,
|
|
2960
2928
|
startedAtMs,
|
|
@@ -2962,7 +2930,7 @@ embeddingRoutes.post("/", async (c) => {
|
|
|
2962
2930
|
path: path$2,
|
|
2963
2931
|
clientIp,
|
|
2964
2932
|
clientIpSource,
|
|
2965
|
-
userAgent
|
|
2933
|
+
userAgent: c.req.header("user-agent") ?? void 0
|
|
2966
2934
|
};
|
|
2967
2935
|
const payload = await c.req.json();
|
|
2968
2936
|
const clientModel = payload.model;
|
|
@@ -2986,15 +2954,14 @@ embeddingRoutes.post("/", async (c) => {
|
|
|
2986
2954
|
});
|
|
2987
2955
|
return selectionFailureResponse$1(c, clientModel, selection.reason);
|
|
2988
2956
|
}
|
|
2989
|
-
const upstreamPayload = {
|
|
2990
|
-
...payload,
|
|
2991
|
-
model: selection.selectedModel.id
|
|
2992
|
-
};
|
|
2993
2957
|
return await runEmbeddingsWithAccount({
|
|
2994
2958
|
c,
|
|
2995
2959
|
store,
|
|
2996
2960
|
ctx,
|
|
2997
|
-
payload:
|
|
2961
|
+
payload: {
|
|
2962
|
+
...payload,
|
|
2963
|
+
model: selection.selectedModel.id
|
|
2964
|
+
},
|
|
2998
2965
|
clientModel,
|
|
2999
2966
|
selection
|
|
3000
2967
|
});
|
|
@@ -3043,8 +3010,7 @@ async function runEmbeddingsWithAccount({ c, store, ctx, payload, clientModel, s
|
|
|
3043
3010
|
let errorMessage;
|
|
3044
3011
|
let finishedAtMs;
|
|
3045
3012
|
try {
|
|
3046
|
-
const
|
|
3047
|
-
const response = await createEmbeddings(payload, accountCtx);
|
|
3013
|
+
const response = await createEmbeddings(payload, toAccountContext(account));
|
|
3048
3014
|
usage = normalizeEmbeddingsUsage(response.usage);
|
|
3049
3015
|
finishedAtMs = Date.now();
|
|
3050
3016
|
return c.json(response);
|
|
@@ -3199,6 +3165,32 @@ const mergeToolResultForClaude = (anthropicPayload) => {
|
|
|
3199
3165
|
msg.content = mergeToolResult(toolResults, textBlocks);
|
|
3200
3166
|
}
|
|
3201
3167
|
};
|
|
3168
|
+
const stripUnsupportedCacheControl = (block) => {
|
|
3169
|
+
const cacheControl = block.cache_control;
|
|
3170
|
+
if (!cacheControl || typeof cacheControl !== "object" || Array.isArray(cacheControl)) return;
|
|
3171
|
+
const type = cacheControl.type;
|
|
3172
|
+
if (typeof type === "string") {
|
|
3173
|
+
block.cache_control = { type };
|
|
3174
|
+
return;
|
|
3175
|
+
}
|
|
3176
|
+
delete block.cache_control;
|
|
3177
|
+
};
|
|
3178
|
+
const stripTextBlockCacheControl = (block) => {
|
|
3179
|
+
if (!block || typeof block !== "object" || Array.isArray(block)) return;
|
|
3180
|
+
const record = block;
|
|
3181
|
+
if (record.type !== "text") return;
|
|
3182
|
+
stripUnsupportedCacheControl(record);
|
|
3183
|
+
};
|
|
3184
|
+
const stripCacheControl = (anthropicPayload) => {
|
|
3185
|
+
if (Array.isArray(anthropicPayload.system)) for (const block of anthropicPayload.system) stripTextBlockCacheControl(block);
|
|
3186
|
+
for (const msg of anthropicPayload.messages) {
|
|
3187
|
+
if (!Array.isArray(msg.content)) continue;
|
|
3188
|
+
for (const block of msg.content) {
|
|
3189
|
+
stripTextBlockCacheControl(block);
|
|
3190
|
+
if (block.type === "tool_result" && Array.isArray(block.content)) for (const nestedBlock of block.content) stripTextBlockCacheControl(nestedBlock);
|
|
3191
|
+
}
|
|
3192
|
+
}
|
|
3193
|
+
};
|
|
3202
3194
|
const estimateInputTokens = async (payload, selectedModel, logger$7) => {
|
|
3203
3195
|
try {
|
|
3204
3196
|
return (await getTokenCount(payload, selectedModel)).input;
|
|
@@ -3264,19 +3256,26 @@ const maybeBlockOriginalModelName = (context) => {
|
|
|
3264
3256
|
});
|
|
3265
3257
|
};
|
|
3266
3258
|
const compactSystemPromptStart = "You are a helpful AI assistant tasked with summarizing conversations";
|
|
3267
|
-
const
|
|
3268
|
-
const
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
if (
|
|
3272
|
-
if (
|
|
3273
|
-
return
|
|
3259
|
+
const compactTextOnlyGuard = "CRITICAL: Respond with TEXT ONLY. Do NOT call any tools.";
|
|
3260
|
+
const compactSummaryPromptStart = "Your task is to create a detailed summary of the conversation so far";
|
|
3261
|
+
const compactMessageSections = ["Pending Tasks:", "Current Work:"];
|
|
3262
|
+
const getCompactCandidateText = (message) => {
|
|
3263
|
+
if (message.role !== "user") return "";
|
|
3264
|
+
if (typeof message.content === "string") return message.content;
|
|
3265
|
+
return message.content.filter((block) => block.type === "text").map((block) => block.text.startsWith("<system-reminder>") ? "" : block.text).filter((text) => text.length > 0).join("\n\n");
|
|
3266
|
+
};
|
|
3267
|
+
const isCompactMessage = (lastMessage) => {
|
|
3268
|
+
const text = getCompactCandidateText(lastMessage);
|
|
3269
|
+
if (!text) return false;
|
|
3270
|
+
return text.includes(compactTextOnlyGuard) && text.includes(compactSummaryPromptStart) && compactMessageSections.some((section) => text.includes(section));
|
|
3274
3271
|
};
|
|
3275
3272
|
const isCompactRequest = (anthropicPayload) => {
|
|
3273
|
+
const lastMessage = anthropicPayload.messages.at(-1);
|
|
3274
|
+
if (lastMessage && isCompactMessage(lastMessage)) return true;
|
|
3276
3275
|
const system = anthropicPayload.system;
|
|
3277
3276
|
if (typeof system === "string" && system.startsWith(compactSystemPromptStart)) return true;
|
|
3278
3277
|
if (Array.isArray(system) && system.some((msg) => typeof msg.text === "string" && msg.text.startsWith(compactSystemPromptStart))) return true;
|
|
3279
|
-
return
|
|
3278
|
+
return false;
|
|
3280
3279
|
};
|
|
3281
3280
|
|
|
3282
3281
|
//#endregion
|
|
@@ -3562,7 +3561,7 @@ async function handleCountTokens(c) {
|
|
|
3562
3561
|
}
|
|
3563
3562
|
}
|
|
3564
3563
|
let finalTokenCount = tokenCount.input + tokenCount.output;
|
|
3565
|
-
if (anthropicPayload.model.startsWith("claude")) finalTokenCount = Math.round(finalTokenCount *
|
|
3564
|
+
if (anthropicPayload.model.startsWith("claude")) finalTokenCount = Math.round(finalTokenCount * getClaudeTokenMultiplier());
|
|
3566
3565
|
consola.info("Token count:", finalTokenCount);
|
|
3567
3566
|
return c.json({ input_tokens: finalTokenCount });
|
|
3568
3567
|
} catch (error) {
|
|
@@ -3609,7 +3608,7 @@ const translateAnthropicMessagesToResponsesPayload = (payload, modelOverride) =>
|
|
|
3609
3608
|
for (const message of payload.messages) input.push(...translateMessage(message, payload.model, applyPhase));
|
|
3610
3609
|
const translatedTools = convertAnthropicTools(payload.tools);
|
|
3611
3610
|
const toolChoice = convertAnthropicToolChoice(payload.tool_choice);
|
|
3612
|
-
const {
|
|
3611
|
+
const { sessionId: promptCacheKey } = parseUserIdMetadata(payload.metadata?.user_id);
|
|
3613
3612
|
return {
|
|
3614
3613
|
model,
|
|
3615
3614
|
input,
|
|
@@ -3620,7 +3619,6 @@ const translateAnthropicMessagesToResponsesPayload = (payload, modelOverride) =>
|
|
|
3620
3619
|
tools: translatedTools,
|
|
3621
3620
|
tool_choice: toolChoice,
|
|
3622
3621
|
metadata: payload.metadata ? { ...payload.metadata } : null,
|
|
3623
|
-
safety_identifier: safetyIdentifier,
|
|
3624
3622
|
prompt_cache_key: promptCacheKey,
|
|
3625
3623
|
stream: payload.stream ?? null,
|
|
3626
3624
|
store: false,
|
|
@@ -3812,7 +3810,7 @@ const translateSystemPrompt = (system, model) => {
|
|
|
3812
3810
|
const extraPrompt = getExtraPromptForModel(model);
|
|
3813
3811
|
if (typeof system === "string") return system + extraPrompt;
|
|
3814
3812
|
const text = system.map((block, index) => {
|
|
3815
|
-
if (index === 0) return block.text + extraPrompt;
|
|
3813
|
+
if (index === 0) return block.text + "\n\n" + extraPrompt + "\n\n";
|
|
3816
3814
|
return block.text;
|
|
3817
3815
|
}).join(" ");
|
|
3818
3816
|
return text.length > 0 ? text : null;
|
|
@@ -3991,9 +3989,8 @@ const mapResponsesStopReason = (response) => {
|
|
|
3991
3989
|
const mapResponsesUsage = (response) => {
|
|
3992
3990
|
const inputTokens = response.usage?.input_tokens ?? 0;
|
|
3993
3991
|
const outputTokens = response.usage?.output_tokens ?? 0;
|
|
3994
|
-
const inputCachedTokens = response.usage?.input_tokens_details?.cached_tokens;
|
|
3995
3992
|
return {
|
|
3996
|
-
input_tokens: inputTokens - (
|
|
3993
|
+
input_tokens: inputTokens - (response.usage?.input_tokens_details?.cached_tokens ?? 0),
|
|
3997
3994
|
output_tokens: outputTokens,
|
|
3998
3995
|
...response.usage?.input_tokens_details?.cached_tokens !== void 0 && { cache_read_input_tokens: response.usage.input_tokens_details.cached_tokens }
|
|
3999
3996
|
};
|
|
@@ -4413,10 +4410,9 @@ const openFunctionCallBlock = (state$1, params) => {
|
|
|
4413
4410
|
if (!functionCallState) {
|
|
4414
4411
|
const blockIndex$1 = state$1.nextContentBlockIndex;
|
|
4415
4412
|
state$1.nextContentBlockIndex += 1;
|
|
4416
|
-
const resolvedToolCallId = toolCallId ?? `tool_call_${blockIndex$1}`;
|
|
4417
4413
|
functionCallState = {
|
|
4418
4414
|
blockIndex: blockIndex$1,
|
|
4419
|
-
toolCallId:
|
|
4415
|
+
toolCallId: toolCallId ?? `tool_call_${blockIndex$1}`,
|
|
4420
4416
|
name: name ?? "function",
|
|
4421
4417
|
consecutiveWhitespaceCount: 0
|
|
4422
4418
|
};
|
|
@@ -4442,26 +4438,20 @@ const openFunctionCallBlock = (state$1, params) => {
|
|
|
4442
4438
|
const extractFunctionCallDetails = (rawEvent) => {
|
|
4443
4439
|
const item = rawEvent.item;
|
|
4444
4440
|
if (item.type !== "function_call") return;
|
|
4445
|
-
const outputIndex = rawEvent.output_index;
|
|
4446
|
-
const toolCallId = item.call_id;
|
|
4447
|
-
const name = item.name;
|
|
4448
|
-
const initialArguments = item.arguments;
|
|
4449
4441
|
return {
|
|
4450
|
-
outputIndex,
|
|
4451
|
-
toolCallId,
|
|
4452
|
-
name,
|
|
4453
|
-
initialArguments
|
|
4442
|
+
outputIndex: rawEvent.output_index,
|
|
4443
|
+
toolCallId: item.call_id,
|
|
4444
|
+
name: item.name,
|
|
4445
|
+
initialArguments: item.arguments
|
|
4454
4446
|
};
|
|
4455
4447
|
};
|
|
4456
4448
|
|
|
4457
4449
|
//#endregion
|
|
4458
4450
|
//#region src/routes/responses/utils.ts
|
|
4459
4451
|
const getResponsesRequestOptions = (payload) => {
|
|
4460
|
-
const vision = hasVisionInput(payload);
|
|
4461
|
-
const initiator = hasAgentInitiator(payload) ? "agent" : "user";
|
|
4462
4452
|
return {
|
|
4463
|
-
vision,
|
|
4464
|
-
initiator
|
|
4453
|
+
vision: hasVisionInput(payload),
|
|
4454
|
+
initiator: hasAgentInitiator(payload) ? "agent" : "user"
|
|
4465
4455
|
};
|
|
4466
4456
|
};
|
|
4467
4457
|
const hasAgentInitiator = (payload) => {
|
|
@@ -4953,11 +4943,7 @@ async function handleCompletion(c) {
|
|
|
4953
4943
|
modelId: endpointModel?.id ?? openAIPayload.model,
|
|
4954
4944
|
endpoint: CHAT_COMPLETIONS_ENDPOINT
|
|
4955
4945
|
});
|
|
4956
|
-
const selection = await accountsManager.selectAccountForRequest(candidates, {
|
|
4957
|
-
promptCacheKey: normalizedPromptCacheKey,
|
|
4958
|
-
sessionId,
|
|
4959
|
-
safetyIdentifier: normalizedSafetyIdentifier
|
|
4960
|
-
});
|
|
4946
|
+
const selection = await accountsManager.selectAccountForRequest(candidates, { requestId: upstreamRequestId });
|
|
4961
4947
|
if (!selection.ok) return handleSelectionFailure({
|
|
4962
4948
|
c,
|
|
4963
4949
|
store,
|
|
@@ -5166,10 +5152,9 @@ function insertRequestLog$1(instr, record) {
|
|
|
5166
5152
|
async function finalizeQuotaAndGetPremiumSnapshot(instr) {
|
|
5167
5153
|
await accountsManager.finalizeQuota(instr.account, instr.reservation);
|
|
5168
5154
|
const premiumRemainingAfter = instr.account.premiumRemaining;
|
|
5169
|
-
const premiumUnlimitedAfter = instr.account.unlimited;
|
|
5170
5155
|
return {
|
|
5171
5156
|
premiumRemainingAfter,
|
|
5172
|
-
premiumUnlimitedAfter,
|
|
5157
|
+
premiumUnlimitedAfter: instr.account.unlimited,
|
|
5173
5158
|
premiumRemainingDiff: computeDiff(instr.premiumRemainingBefore, premiumRemainingAfter)
|
|
5174
5159
|
};
|
|
5175
5160
|
}
|
|
@@ -5557,6 +5542,7 @@ async function streamMessagesAndLog(params) {
|
|
|
5557
5542
|
}
|
|
5558
5543
|
const handleWithMessagesApi = async (params) => {
|
|
5559
5544
|
const { c, anthropicPayload, anthropicBetaHeader, initiatorOverride, subagentMarker, sessionId, instr, selectedModel, isCompact } = params;
|
|
5545
|
+
stripCacheControl(anthropicPayload);
|
|
5560
5546
|
for (const msg of anthropicPayload.messages) if (msg.role === "assistant" && Array.isArray(msg.content)) msg.content = msg.content.filter((block) => {
|
|
5561
5547
|
if (block.type !== "thinking") return true;
|
|
5562
5548
|
return block.thinking && block.thinking !== "Thinking..." && block.signature && !block.signature.includes("@");
|
|
@@ -5781,7 +5767,7 @@ async function forwardProviderModels(providerConfig, requestHeaders) {
|
|
|
5781
5767
|
//#region src/routes/provider/messages/handler.ts
|
|
5782
5768
|
const logger$3 = createHandlerLogger("provider-messages-handler");
|
|
5783
5769
|
async function handleProviderMessages(c) {
|
|
5784
|
-
const provider = c.req.param("provider");
|
|
5770
|
+
const provider = c.req.param("provider") ?? "";
|
|
5785
5771
|
const providerConfig = getProviderConfig(provider);
|
|
5786
5772
|
if (!providerConfig) return c.json({ error: {
|
|
5787
5773
|
message: `Provider '${provider}' not found or disabled`,
|
|
@@ -5972,13 +5958,11 @@ const handleResponses = async (c) => {
|
|
|
5972
5958
|
message: "This model is only available via an alias. Please use the alias model name."
|
|
5973
5959
|
});
|
|
5974
5960
|
}
|
|
5961
|
+
const upstreamRequestId = generateRequestIdFromPayload({ messages: payload.input }, normalizedPromptCacheKey);
|
|
5975
5962
|
const selection = await accountsManager.selectAccountForRequest([{
|
|
5976
5963
|
modelId: clientModel,
|
|
5977
5964
|
endpoint: RESPONSES_ENDPOINT
|
|
5978
|
-
}], {
|
|
5979
|
-
promptCacheKey: normalizedPromptCacheKey,
|
|
5980
|
-
safetyIdentifier: normalizedSafetyIdentifier
|
|
5981
|
-
});
|
|
5965
|
+
}], { requestId: upstreamRequestId });
|
|
5982
5966
|
if (!selection.ok) {
|
|
5983
5967
|
recordSelectionFailure(store, {
|
|
5984
5968
|
request,
|
|
@@ -6004,7 +5988,6 @@ const handleResponses = async (c) => {
|
|
|
6004
5988
|
request.initiator = initiator;
|
|
6005
5989
|
if (state.manualApprove) await awaitApproval();
|
|
6006
5990
|
const accountCtx = toAccountContext(account);
|
|
6007
|
-
const upstreamRequestId = generateRequestIdFromPayload({ messages: upstreamPayload.input });
|
|
6008
5991
|
const upstreamSessionId = getUUID(upstreamRequestId);
|
|
6009
5992
|
request.upstreamRequestId = upstreamRequestId;
|
|
6010
5993
|
request.upstreamSessionId = upstreamSessionId;
|
|
@@ -6041,7 +6024,6 @@ function buildRequestContext(c) {
|
|
|
6041
6024
|
const method = c.req.raw.method;
|
|
6042
6025
|
const path$2 = new URL(c.req.url, "http://local").pathname;
|
|
6043
6026
|
const { ip: clientIp, source: clientIpSource } = getClientIpInfo(c);
|
|
6044
|
-
const userAgent = c.req.header("user-agent") ?? void 0;
|
|
6045
6027
|
return {
|
|
6046
6028
|
requestId,
|
|
6047
6029
|
startedAtMs,
|
|
@@ -6049,7 +6031,7 @@ function buildRequestContext(c) {
|
|
|
6049
6031
|
path: path$2,
|
|
6050
6032
|
clientIp,
|
|
6051
6033
|
clientIpSource,
|
|
6052
|
-
userAgent
|
|
6034
|
+
userAgent: c.req.header("user-agent") ?? void 0
|
|
6053
6035
|
};
|
|
6054
6036
|
}
|
|
6055
6037
|
function insertRequestLog(store, request, record) {
|
|
@@ -6098,8 +6080,7 @@ function selectionFailureResponse(c, params) {
|
|
|
6098
6080
|
function extractUsageFromChunkData(data) {
|
|
6099
6081
|
if (!data) return void 0;
|
|
6100
6082
|
try {
|
|
6101
|
-
const
|
|
6102
|
-
const usage = extractResponsesUsageFromStreamEvent(event);
|
|
6083
|
+
const usage = extractResponsesUsageFromStreamEvent(JSON.parse(data));
|
|
6103
6084
|
return usage.usageJson ? usage : void 0;
|
|
6104
6085
|
} catch {
|
|
6105
6086
|
return;
|
|
@@ -6495,4 +6476,4 @@ server.route("/:provider/v1/models", providerModelRoutes);
|
|
|
6495
6476
|
|
|
6496
6477
|
//#endregion
|
|
6497
6478
|
export { server };
|
|
6498
|
-
//# sourceMappingURL=server-
|
|
6479
|
+
//# sourceMappingURL=server-D9M_wFyO.js.map
|