@nick3/copilot-api 1.4.0 → 1.4.4
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/LICENSE +1 -1
- package/README.md +50 -12
- package/dist/{accounts-manager-BQCAoBZp.js → accounts-manager-BvbpPVp1.js} +7 -4
- package/dist/accounts-manager-BvbpPVp1.js.map +1 -0
- package/dist/admin/assets/index-Y2SvOXge.js +57 -0
- package/dist/admin/assets/{index-CvffOmW7.css → index-geiCIixE.css} +1 -1
- package/dist/admin/index.html +2 -2
- package/dist/{auth-cdKpP998.js → auth-C_MhaDzM.js} +3 -3
- package/dist/{auth-cdKpP998.js.map → auth-C_MhaDzM.js.map} +1 -1
- package/dist/{check-usage-CZioMC9b.js → check-usage-Cw1fo31i.js} +4 -4
- package/dist/{check-usage-CZioMC9b.js.map → check-usage-Cw1fo31i.js.map} +1 -1
- package/dist/{get-copilot-token-BA1FaCgQ.js → get-copilot-token-CbzXMTE5.js} +2 -2
- package/dist/{get-copilot-token-BA1FaCgQ.js.map → get-copilot-token-CbzXMTE5.js.map} +1 -1
- package/dist/main.js +3 -3
- package/dist/{poll-access-token-CDS97hy8.js → poll-access-token-BMaaEFhO.js} +2 -2
- package/dist/{poll-access-token-CDS97hy8.js.map → poll-access-token-BMaaEFhO.js.map} +1 -1
- package/dist/{server-D3sySKxC.js → server-BKsjCvqp.js} +45 -6
- package/dist/server-BKsjCvqp.js.map +1 -0
- package/dist/{start-DLXiT8QI.js → start-cJXCnfYv.js} +35 -31
- package/dist/start-cJXCnfYv.js.map +1 -0
- package/dist/{utils-D8j9lvS0.js → utils-DkUv9TDj.js} +7 -3
- package/dist/utils-DkUv9TDj.js.map +1 -0
- package/package.json +1 -1
- package/dist/accounts-manager-BQCAoBZp.js.map +0 -1
- package/dist/admin/assets/index-BB9SaCFS.js +0 -57
- package/dist/server-D3sySKxC.js.map +0 -1
- package/dist/start-DLXiT8QI.js.map +0 -1
- package/dist/utils-D8j9lvS0.js.map +0 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { PATHS } from "./paths-DoT4SZ8f.js";
|
|
2
2
|
import { listAccountsFromRegistry } from "./accounts-registry-c7rs5Ed9.js";
|
|
3
|
-
import { HTTPError, accountFromState, cacheModels, copilotBaseUrl, copilotHeaders, forwardError, generateRequestIdFromPayload, getCopilotUsage, getRootSessionId, getUUID, isNullish, parseUserIdMetadata, prepareForCompact, prepareInteractionHeaders, sleep, state } from "./utils-
|
|
4
|
-
import "./get-copilot-token-
|
|
5
|
-
import { PROVIDER_TYPE_ANTHROPIC, accountsManager, getAliasTargetSet, getConfig, getExtraPromptForModel, getModelAliases, getModelAliasesInfo, getModelRefreshIntervalMs, getProviderConfig, getReasoningEffortForModel, getSmallModel, isForceAgentEnabled, isFreeModelLoadBalancingEnabled, isMessageStartInputTokensFallbackEnabled, isMessagesApiEnabled, isResponsesApiContextManagementModel, mergeConfigWithDefaults, shouldCompactUseSmallModel } from "./accounts-manager-
|
|
3
|
+
import { HTTPError, accountFromState, cacheModels, copilotBaseUrl, copilotHeaders, forwardError, generateRequestIdFromPayload, getCopilotUsage, getRootSessionId, getUUID, isNullish, parseUserIdMetadata, prepareForCompact, prepareInteractionHeaders, sleep, state } from "./utils-DkUv9TDj.js";
|
|
4
|
+
import "./get-copilot-token-CbzXMTE5.js";
|
|
5
|
+
import { PROVIDER_TYPE_ANTHROPIC, accountsManager, getAliasTargetSet, getAnthropicApiKey, getConfig, getExtraPromptForModel, getModelAliases, getModelAliasesInfo, getModelRefreshIntervalMs, getProviderConfig, getReasoningEffortForModel, getSmallModel, isForceAgentEnabled, isFreeModelLoadBalancingEnabled, isMessageStartInputTokensFallbackEnabled, isMessagesApiEnabled, isResponsesApiContextManagementModel, mergeConfigWithDefaults, shouldCompactUseSmallModel } from "./accounts-manager-BvbpPVp1.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";
|
|
@@ -765,6 +765,7 @@ const CONFIG_KEYS = new Set([
|
|
|
765
765
|
"smallModel",
|
|
766
766
|
"freeModelLoadBalancing",
|
|
767
767
|
"apiKey",
|
|
768
|
+
"anthropicApiKey",
|
|
768
769
|
"providers",
|
|
769
770
|
"responsesApiContextManagementModels",
|
|
770
771
|
"modelReasoningEfforts",
|
|
@@ -1125,6 +1126,7 @@ const CONFIG_PATCH_HANDLERS = {
|
|
|
1125
1126
|
smallModel: (next, value) => applyOptionalString(next, "smallModel", value),
|
|
1126
1127
|
freeModelLoadBalancing: (next, value) => applyOptionalBoolean(next, "freeModelLoadBalancing", value),
|
|
1127
1128
|
apiKey: (next, value) => applyOptionalString(next, "apiKey", value),
|
|
1129
|
+
anthropicApiKey: (next, value) => applyOptionalString(next, "anthropicApiKey", value),
|
|
1128
1130
|
providers: applyProvidersConfig,
|
|
1129
1131
|
responsesApiContextManagementModels: applyResponsesApiContextManagementModels,
|
|
1130
1132
|
modelReasoningEfforts: applyReasoningEfforts,
|
|
@@ -3459,12 +3461,48 @@ function getAnthropicToolUseBlocks(toolCalls) {
|
|
|
3459
3461
|
//#endregion
|
|
3460
3462
|
//#region src/routes/messages/count-tokens-handler.ts
|
|
3461
3463
|
/**
|
|
3462
|
-
*
|
|
3464
|
+
* Forwards token counting to Anthropic's real /v1/messages/count_tokens endpoint.
|
|
3465
|
+
* Returns the result on success, or null to fall through to estimation.
|
|
3466
|
+
*/
|
|
3467
|
+
async function countTokensViaAnthropic(c, payload) {
|
|
3468
|
+
if (!payload.model.startsWith("claude")) return null;
|
|
3469
|
+
const apiKey = getAnthropicApiKey();
|
|
3470
|
+
if (!apiKey) return null;
|
|
3471
|
+
const model = payload.model.replaceAll(".", "-");
|
|
3472
|
+
const res = await fetch("https://api.anthropic.com/v1/messages/count_tokens", {
|
|
3473
|
+
method: "POST",
|
|
3474
|
+
headers: {
|
|
3475
|
+
"content-type": "application/json",
|
|
3476
|
+
"x-api-key": apiKey,
|
|
3477
|
+
"anthropic-version": "2023-06-01",
|
|
3478
|
+
"anthropic-beta": "token-counting-2024-11-01"
|
|
3479
|
+
},
|
|
3480
|
+
body: JSON.stringify({
|
|
3481
|
+
...payload,
|
|
3482
|
+
model
|
|
3483
|
+
})
|
|
3484
|
+
});
|
|
3485
|
+
if (!res.ok) {
|
|
3486
|
+
consola.warn("Anthropic count_tokens failed:", res.status, await res.text().catch(() => ""), "- falling back to estimation");
|
|
3487
|
+
return null;
|
|
3488
|
+
}
|
|
3489
|
+
const result = await res.json();
|
|
3490
|
+
consola.info("Token count (Anthropic API):", result.input_tokens);
|
|
3491
|
+
return c.json(result);
|
|
3492
|
+
}
|
|
3493
|
+
/**
|
|
3494
|
+
* Handles token counting for Anthropic messages.
|
|
3495
|
+
*
|
|
3496
|
+
* When an Anthropic API key is available (via config or ANTHROPIC_API_KEY env var)
|
|
3497
|
+
* and the model is a Claude model, forwards to Anthropic's free /v1/messages/count_tokens
|
|
3498
|
+
* endpoint for accurate counts. Otherwise falls back to GPT tokenizer estimation.
|
|
3463
3499
|
*/
|
|
3464
3500
|
async function handleCountTokens(c) {
|
|
3465
3501
|
try {
|
|
3466
|
-
const anthropicBeta = c.req.header("anthropic-beta");
|
|
3467
3502
|
const anthropicPayload = await c.req.json();
|
|
3503
|
+
const anthropicResult = await countTokensViaAnthropic(c, anthropicPayload);
|
|
3504
|
+
if (anthropicResult) return anthropicResult;
|
|
3505
|
+
const anthropicBeta = c.req.header("anthropic-beta");
|
|
3468
3506
|
const openAIPayload = translateToOpenAI(anthropicPayload);
|
|
3469
3507
|
const selectedModel = findEndpointModel(anthropicPayload.model);
|
|
3470
3508
|
anthropicPayload.model = selectedModel?.id ?? anthropicPayload.model;
|
|
@@ -5728,6 +5766,7 @@ async function handleProviderMessages(c) {
|
|
|
5728
5766
|
}
|
|
5729
5767
|
let data = chunk.data;
|
|
5730
5768
|
if (!data) continue;
|
|
5769
|
+
if (chunk.data === "[DONE]") break;
|
|
5731
5770
|
try {
|
|
5732
5771
|
const parsed = JSON.parse(data);
|
|
5733
5772
|
if (parsed.type === "message_start") adjustInputTokens(providerConfig, parsed.message.usage);
|
|
@@ -6395,4 +6434,4 @@ server.route("/:provider/v1/models", providerModelRoutes);
|
|
|
6395
6434
|
|
|
6396
6435
|
//#endregion
|
|
6397
6436
|
export { server };
|
|
6398
|
-
//# sourceMappingURL=server-
|
|
6437
|
+
//# sourceMappingURL=server-BKsjCvqp.js.map
|