billion-context 0.1.75-pr.486.305 → 0.1.75-pr.489.307
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/index.js +87 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -50696,7 +50696,7 @@ function summaryPayload(protocol, model, system, content) {
|
|
|
50696
50696
|
if (protocol === "openai") {
|
|
50697
50697
|
return { model, max_tokens: MAX_SUMMARY_OUTPUT_TOKENS, messages: [{ role: "system", content: system }, { role: "user", content }], stream: false };
|
|
50698
50698
|
}
|
|
50699
|
-
return { model, max_output_tokens: MAX_SUMMARY_OUTPUT_TOKENS, instructions: system, input: [{ role: "user", content }], stream: false };
|
|
50699
|
+
return { model, max_output_tokens: MAX_SUMMARY_OUTPUT_TOKENS, instructions: system, input: [{ role: "user", content }], stream: false, store: false };
|
|
50700
50700
|
}
|
|
50701
50701
|
function extractSummaryText(protocol, json) {
|
|
50702
50702
|
if (protocol === "anthropic") {
|
|
@@ -50762,7 +50762,7 @@ TASK: The conversation segment below (messages ${startRef}\u2013${endRef}) must
|
|
|
50762
50762
|
var ABORTED_FAILURE = { kind: "aborted", detail: "the client disconnected during preflight compression" };
|
|
50763
50763
|
async function preflightCompress(deps, messages) {
|
|
50764
50764
|
const limit = deps.config.modelContextLimit;
|
|
50765
|
-
const result = { compressedRanges: 0, savedTokens: 0, payloadEstimate: estimateCoreMessages(messages) };
|
|
50765
|
+
const result = { compressedRanges: 0, savedTokens: 0, payloadEstimate: estimateCoreMessages(messages) + (deps.imageFloor ?? 0) };
|
|
50766
50766
|
if (limit <= 0) return result;
|
|
50767
50767
|
const budget = Math.max(MIN_CHUNK_TOKENS, Math.floor(limit * CHUNK_FRACTION));
|
|
50768
50768
|
const minChars = deps.config.compress.minCompressRange;
|
|
@@ -50782,8 +50782,8 @@ async function preflightCompress(deps, messages) {
|
|
|
50782
50782
|
renderTags: "text-only"
|
|
50783
50783
|
});
|
|
50784
50784
|
deps.session.state = turn.state;
|
|
50785
|
-
currentTokens = Math.max(deps.session.stats.lastInputTokens, estimateCoreMessages(turn.messages));
|
|
50786
|
-
result.payloadEstimate = estimateCoreMessages(turn.messages);
|
|
50785
|
+
currentTokens = Math.max(deps.session.stats.lastInputTokens, estimateCoreMessages(turn.messages) + (deps.imageFloor ?? 0));
|
|
50786
|
+
result.payloadEstimate = estimateCoreMessages(turn.messages) + (deps.imageFloor ?? 0);
|
|
50787
50787
|
if (startTokens < 0) startTokens = currentTokens;
|
|
50788
50788
|
if (currentTokens < limit) break;
|
|
50789
50789
|
const ranges = viableRanges(turn.nudge?.compressibleRanges ?? []);
|
|
@@ -50869,6 +50869,78 @@ async function preflightCompress(deps, messages) {
|
|
|
50869
50869
|
return result;
|
|
50870
50870
|
}
|
|
50871
50871
|
|
|
50872
|
+
// src/image-tokens.ts
|
|
50873
|
+
var REMOTE_IMAGE_TOKENS = 4096;
|
|
50874
|
+
function imageTokenCap() {
|
|
50875
|
+
const v2 = Number(process.env.BILI_IMAGE_TOKEN_CAP ?? "");
|
|
50876
|
+
return Number.isInteger(v2) && v2 > 0 ? v2 : 0;
|
|
50877
|
+
}
|
|
50878
|
+
function applyCap(cost) {
|
|
50879
|
+
const cap = imageTokenCap();
|
|
50880
|
+
return cap > 0 ? Math.min(cost, cap) : cost;
|
|
50881
|
+
}
|
|
50882
|
+
function costForUrl(url) {
|
|
50883
|
+
if (url.startsWith("data:")) {
|
|
50884
|
+
const i = url.indexOf("base64,");
|
|
50885
|
+
if (i >= 0) return applyCap(Math.ceil((url.length - i - "base64,".length) / 4));
|
|
50886
|
+
}
|
|
50887
|
+
return applyCap(REMOTE_IMAGE_TOKENS);
|
|
50888
|
+
}
|
|
50889
|
+
function isObj(v2) {
|
|
50890
|
+
return typeof v2 === "object" && v2 !== null;
|
|
50891
|
+
}
|
|
50892
|
+
function urlOf(v2) {
|
|
50893
|
+
if (typeof v2 === "string") return v2;
|
|
50894
|
+
if (isObj(v2) && typeof v2.url === "string") return v2.url;
|
|
50895
|
+
return void 0;
|
|
50896
|
+
}
|
|
50897
|
+
function imageTokensInParsedBody(protocol, body) {
|
|
50898
|
+
if (!isObj(body)) return 0;
|
|
50899
|
+
let total = 0;
|
|
50900
|
+
if (protocol === "responses") {
|
|
50901
|
+
const input = body.input;
|
|
50902
|
+
if (!Array.isArray(input)) return 0;
|
|
50903
|
+
for (const item of input) {
|
|
50904
|
+
if (!isObj(item) || !Array.isArray(item.content)) continue;
|
|
50905
|
+
for (const part of item.content) {
|
|
50906
|
+
if (!isObj(part) || part.type !== "input_image") continue;
|
|
50907
|
+
const url = urlOf(part.image_url);
|
|
50908
|
+
if (url) total += costForUrl(url);
|
|
50909
|
+
}
|
|
50910
|
+
}
|
|
50911
|
+
return total;
|
|
50912
|
+
}
|
|
50913
|
+
const messages = body.messages;
|
|
50914
|
+
if (!Array.isArray(messages)) return 0;
|
|
50915
|
+
for (const m2 of messages) {
|
|
50916
|
+
if (!isObj(m2) || !Array.isArray(m2.content)) continue;
|
|
50917
|
+
for (const part of m2.content) {
|
|
50918
|
+
if (!isObj(part)) continue;
|
|
50919
|
+
if (protocol === "openai") {
|
|
50920
|
+
if (part.type !== "image_url") continue;
|
|
50921
|
+
const url = urlOf(part.image_url);
|
|
50922
|
+
if (url) total += costForUrl(url);
|
|
50923
|
+
} else {
|
|
50924
|
+
if (part.type !== "image") continue;
|
|
50925
|
+
const src = part.source;
|
|
50926
|
+
if (isObj(src) && src.type === "base64" && typeof src.data === "string") total += applyCap(Math.ceil(src.data.length / 4));
|
|
50927
|
+
else if (isObj(src) && src.type === "url" && typeof src.url === "string") total += costForUrl(src.url);
|
|
50928
|
+
}
|
|
50929
|
+
}
|
|
50930
|
+
}
|
|
50931
|
+
return total;
|
|
50932
|
+
}
|
|
50933
|
+
function imageTokensInRawBody(protocol, raw) {
|
|
50934
|
+
const s3 = typeof raw === "string" ? raw : raw.toString("utf8");
|
|
50935
|
+
const probe = protocol === "responses" ? s3.includes("input_image") : protocol === "openai" ? s3.includes("image_url") : s3.includes('"type":"image"') || s3.includes('"type": "image"');
|
|
50936
|
+
if (!probe) return 0;
|
|
50937
|
+
try {
|
|
50938
|
+
return imageTokensInParsedBody(protocol, JSON.parse(s3));
|
|
50939
|
+
} catch {
|
|
50940
|
+
return 0;
|
|
50941
|
+
}
|
|
50942
|
+
}
|
|
50943
|
+
|
|
50872
50944
|
// src/web/index.ts
|
|
50873
50945
|
import { readFileSync as readFileSync3 } from "fs";
|
|
50874
50946
|
import { dirname as dirname4, join as join3 } from "path";
|
|
@@ -57630,11 +57702,13 @@ function isPreflightFailFast(outcome) {
|
|
|
57630
57702
|
async function preflightCompressIfNeeded(prepared, runPrepare, req, res, opts, core, config, model, route, affinity, log2, instanceId) {
|
|
57631
57703
|
const session = prepared.session;
|
|
57632
57704
|
const limit = config.modelContextLimit;
|
|
57633
|
-
const
|
|
57705
|
+
const imageTokens = imageTokensInRawBody(prepared.protocol, prepared.body);
|
|
57706
|
+
const payloadEstimate = estimateCoreMessages(prepared.processedMessages) + imageTokens;
|
|
57634
57707
|
const tokenCount = Math.max(session.stats.lastInputTokens, payloadEstimate);
|
|
57635
57708
|
if (limit <= 0 || !model || tokenCount < limit) return prepared;
|
|
57636
57709
|
const failFast = (status2, detail, retryable) => {
|
|
57637
|
-
const
|
|
57710
|
+
const imageNote = imageTokens >= limit ? ` Images alone account for ~${imageTokens} tokens (\u2265 window ${limit}); compression cannot remove them \u2014 shrink or remove the images, or raise the window.` : "";
|
|
57711
|
+
const message = `context ~${tokenCount} tokens exceeds the model window ${limit} (model=${model}) and preflight compression could not bring it under: ${detail}.` + imageNote + ` The over-window payload was NOT forwarded.`;
|
|
57638
57712
|
log2("error", `[${session.id}] preflight fail-fast ${status2} (retryable=${retryable}): ${message}`);
|
|
57639
57713
|
return { failFast: true, status: status2, message, retryable, respond: !res.writableEnded };
|
|
57640
57714
|
};
|
|
@@ -57664,7 +57738,8 @@ async function preflightCompressIfNeeded(prepared, runPrepare, req, res, opts, c
|
|
|
57664
57738
|
model,
|
|
57665
57739
|
proxyUrl,
|
|
57666
57740
|
signal: clientAbort.signal,
|
|
57667
|
-
log: log2
|
|
57741
|
+
log: log2,
|
|
57742
|
+
imageFloor: imageTokens
|
|
57668
57743
|
},
|
|
57669
57744
|
prepared.originalMessages
|
|
57670
57745
|
);
|
|
@@ -57833,9 +57908,12 @@ ${hdrText}
|
|
|
57833
57908
|
const info = inspectContextOverflow(upstream.status, errBody.toString("utf8"));
|
|
57834
57909
|
if (info.isOverflow) {
|
|
57835
57910
|
let reqModel;
|
|
57911
|
+
let rejectedImageTokens = 0;
|
|
57836
57912
|
try {
|
|
57837
57913
|
const rawBody = typeof prepared.body === "string" ? prepared.body : prepared.body.toString("utf8");
|
|
57838
|
-
|
|
57914
|
+
const parsedBody = JSON.parse(rawBody);
|
|
57915
|
+
reqModel = typeof parsedBody.model === "string" ? parsedBody.model : void 0;
|
|
57916
|
+
rejectedImageTokens = imageTokensInParsedBody(prepared.protocol, parsedBody);
|
|
57839
57917
|
} catch {
|
|
57840
57918
|
reqModel = void 0;
|
|
57841
57919
|
}
|
|
@@ -57847,7 +57925,7 @@ ${hdrText}
|
|
|
57847
57925
|
s3.metadata.learnedContextLimits = learnedMap;
|
|
57848
57926
|
log2("warn", `[${s3.id}] upstream context overflow \u2014 learned real window ${info.window} for ${reqModel ?? "(unknown model)"} (was ${prev ?? "unset"}); arming emergency shrink`);
|
|
57849
57927
|
} else {
|
|
57850
|
-
const payloadEstimate = estimateCoreMessages(prepared.processedMessages);
|
|
57928
|
+
const payloadEstimate = estimateCoreMessages(prepared.processedMessages) + rejectedImageTokens;
|
|
57851
57929
|
const prev = (reqModel ? learnedMap[reqModel] : void 0) ?? s3.metadata.learnedContextLimit;
|
|
57852
57930
|
if (payloadEstimate >= 1e3 && (prev === void 0 || payloadEstimate < prev)) {
|
|
57853
57931
|
if (reqModel) learnedMap[reqModel] = payloadEstimate;
|