billion-context 0.1.82 → 0.1.83-pr.425.342
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/agent/omp.js +4 -0
- package/dist/agent/omp.js.map +1 -1
- package/dist/agent/pi.js +4 -0
- package/dist/agent/pi.js.map +1 -1
- package/dist/index.js +255 -55
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -48360,33 +48360,64 @@ async function contextFromRegistry(model, host) {
|
|
|
48360
48360
|
function peekRegistryContext(model, host) {
|
|
48361
48361
|
return registryLookup(cache2, model, host);
|
|
48362
48362
|
}
|
|
48363
|
+
var VARIANT_SUFFIXES = [
|
|
48364
|
+
"-interleaved-thinking",
|
|
48365
|
+
"-thinking",
|
|
48366
|
+
"-reasoning",
|
|
48367
|
+
"-extended",
|
|
48368
|
+
"-fast",
|
|
48369
|
+
"-high",
|
|
48370
|
+
"-medium",
|
|
48371
|
+
"-low"
|
|
48372
|
+
];
|
|
48373
|
+
function modelVariants(name) {
|
|
48374
|
+
const variants = [name];
|
|
48375
|
+
let current = name;
|
|
48376
|
+
for (; ; ) {
|
|
48377
|
+
let stripped = false;
|
|
48378
|
+
for (const suffix of VARIANT_SUFFIXES) {
|
|
48379
|
+
if (current.length > suffix.length && current.endsWith(suffix)) {
|
|
48380
|
+
current = current.slice(0, -suffix.length);
|
|
48381
|
+
variants.push(current);
|
|
48382
|
+
stripped = true;
|
|
48383
|
+
break;
|
|
48384
|
+
}
|
|
48385
|
+
}
|
|
48386
|
+
if (!stripped) break;
|
|
48387
|
+
}
|
|
48388
|
+
return variants;
|
|
48389
|
+
}
|
|
48363
48390
|
function registryLookup(reg, model, host) {
|
|
48364
48391
|
if (!reg || !model) return void 0;
|
|
48365
48392
|
const provider = host ? providerFromHost(host) : void 0;
|
|
48366
|
-
const
|
|
48367
|
-
|
|
48368
|
-
const
|
|
48369
|
-
|
|
48370
|
-
|
|
48371
|
-
|
|
48372
|
-
|
|
48373
|
-
|
|
48374
|
-
|
|
48375
|
-
|
|
48376
|
-
|
|
48377
|
-
|
|
48378
|
-
|
|
48379
|
-
|
|
48380
|
-
|
|
48381
|
-
|
|
48382
|
-
|
|
48383
|
-
|
|
48384
|
-
|
|
48385
|
-
|
|
48386
|
-
|
|
48387
|
-
|
|
48393
|
+
for (const name of modelVariants(model)) {
|
|
48394
|
+
const candidates = provider ? [`${provider}/${name}`, name] : [name];
|
|
48395
|
+
for (const key of candidates) {
|
|
48396
|
+
const entry = reg[key];
|
|
48397
|
+
const ctx = entry?.limit?.context;
|
|
48398
|
+
if (typeof ctx === "number" && ctx > 0) return ctx;
|
|
48399
|
+
}
|
|
48400
|
+
if (provider === void 0) {
|
|
48401
|
+
const suffix = `/${name}`;
|
|
48402
|
+
let max;
|
|
48403
|
+
const distinct = /* @__PURE__ */ new Set();
|
|
48404
|
+
const parts = [];
|
|
48405
|
+
for (const key of Object.keys(reg)) {
|
|
48406
|
+
if (!key.endsWith(suffix)) continue;
|
|
48407
|
+
const ctx = reg[key].limit?.context;
|
|
48408
|
+
if (typeof ctx !== "number" || ctx <= 0) continue;
|
|
48409
|
+
if (max === void 0 || ctx > max) max = ctx;
|
|
48410
|
+
distinct.add(ctx);
|
|
48411
|
+
parts.push(`${key}=${ctx}`);
|
|
48412
|
+
}
|
|
48413
|
+
if (max !== void 0) {
|
|
48414
|
+
if (distinct.size > 1 && !warnedConflicts.has(name)) {
|
|
48415
|
+
warnedConflicts.add(name);
|
|
48416
|
+
log("warn", `[acp-registry] conflicting context windows for "${name}" (${parts.join(", ")}) \u2014 using max ${max}`);
|
|
48417
|
+
}
|
|
48418
|
+
return max;
|
|
48419
|
+
}
|
|
48388
48420
|
}
|
|
48389
|
-
return max;
|
|
48390
48421
|
}
|
|
48391
48422
|
return void 0;
|
|
48392
48423
|
}
|
|
@@ -49829,8 +49860,17 @@ var SessionStore = class {
|
|
|
49829
49860
|
async loadAll() {
|
|
49830
49861
|
const out = /* @__PURE__ */ new Map();
|
|
49831
49862
|
if (!this.enabled) return out;
|
|
49863
|
+
let clamped = 0;
|
|
49832
49864
|
for (const [id, envelope] of await this.store.loadAll()) {
|
|
49833
|
-
|
|
49865
|
+
const session = buildSession(envelope.payload);
|
|
49866
|
+
if (hasNegativePersistedTokens(envelope.payload)) {
|
|
49867
|
+
await this.store.writeNow(id, () => buildRecord(session));
|
|
49868
|
+
clamped++;
|
|
49869
|
+
}
|
|
49870
|
+
out.set(id, session);
|
|
49871
|
+
}
|
|
49872
|
+
if (clamped > 0) {
|
|
49873
|
+
log("info", `[persist] one-time migration (#408): clamped negative lastInputTokens/contextTokens in ${clamped} session(s) to 0`);
|
|
49834
49874
|
}
|
|
49835
49875
|
return out;
|
|
49836
49876
|
}
|
|
@@ -49911,7 +49951,14 @@ var SessionStore = class {
|
|
|
49911
49951
|
meta?.protocol ? this.store.loadSync(id, relPathFor(id)) : null
|
|
49912
49952
|
];
|
|
49913
49953
|
for (const envelope of envelopes) {
|
|
49914
|
-
if (envelope)
|
|
49954
|
+
if (envelope) {
|
|
49955
|
+
const session = buildSession(envelope.payload);
|
|
49956
|
+
if (hasNegativePersistedTokens(envelope.payload)) {
|
|
49957
|
+
this.scheduleSave(session);
|
|
49958
|
+
log("info", `[persist] clamped negative token stats on reload for ${id} (#408)`);
|
|
49959
|
+
}
|
|
49960
|
+
return session;
|
|
49961
|
+
}
|
|
49915
49962
|
}
|
|
49916
49963
|
return null;
|
|
49917
49964
|
}
|
|
@@ -49988,10 +50035,14 @@ function buildSession(parsed) {
|
|
|
49988
50035
|
cachedTokens: stats.cachedTokens ?? parsed.cachedTokens ?? 0,
|
|
49989
50036
|
outputTokens: stats.outputTokens ?? parsed.outputTokens ?? 0,
|
|
49990
50037
|
cacheSamples: stats.cacheSamples ?? parsed.cacheSamples ?? 0,
|
|
49991
|
-
|
|
50038
|
+
// #408: clamp at restore — pre-clamp versions persisted negative
|
|
50039
|
+
// values (lastInputTokens = total − credit before the Math.max
|
|
50040
|
+
// guard existed) which would otherwise revive after upgrade and
|
|
50041
|
+
// feed the /acp panel + web stats as negative percentages.
|
|
50042
|
+
lastInputTokens: Math.max(0, stats.lastInputTokens ?? parsed.lastInputTokens ?? 0),
|
|
49992
50043
|
// In-memory only — a fresh process has no pending compress fold.
|
|
49993
50044
|
compressCreditTokens: 0,
|
|
49994
|
-
contextTokens: stats.contextTokens ?? parsed.contextTokens ?? 0
|
|
50045
|
+
contextTokens: Math.max(0, stats.contextTokens ?? parsed.contextTokens ?? 0)
|
|
49995
50046
|
},
|
|
49996
50047
|
metadata: parsed.metadata ?? {},
|
|
49997
50048
|
state: mergeState(parsed.state),
|
|
@@ -50014,6 +50065,12 @@ function isValidRecord(parsed) {
|
|
|
50014
50065
|
const r = parsed;
|
|
50015
50066
|
return typeof r.id === "string" && typeof r.state === "object" && r.state !== null && Array.isArray(r.state.blocks);
|
|
50016
50067
|
}
|
|
50068
|
+
function hasNegativePersistedTokens(parsed) {
|
|
50069
|
+
const stats = parsed.stats ?? {};
|
|
50070
|
+
const last = stats.lastInputTokens ?? parsed.lastInputTokens;
|
|
50071
|
+
const ctx = stats.contextTokens ?? parsed.contextTokens;
|
|
50072
|
+
return typeof last === "number" && last < 0 || typeof ctx === "number" && ctx < 0;
|
|
50073
|
+
}
|
|
50017
50074
|
function defaultDir() {
|
|
50018
50075
|
return sessionsDir();
|
|
50019
50076
|
}
|
|
@@ -50147,6 +50204,8 @@ function resetSessionCompression(session) {
|
|
|
50147
50204
|
session.blockContents.clear();
|
|
50148
50205
|
session.stats.lastInputTokens = 0;
|
|
50149
50206
|
session.stats.contextTokens = 0;
|
|
50207
|
+
session.hostCreditTokens = 0;
|
|
50208
|
+
session.hostContextTokens = 0;
|
|
50150
50209
|
session.metadata.nativeCompactionAt = Date.now();
|
|
50151
50210
|
markDirty(session);
|
|
50152
50211
|
}
|
|
@@ -51736,6 +51795,7 @@ function recordUsage(ctx, usage, round) {
|
|
|
51736
51795
|
const total = (typeof prompt === "number" ? prompt : 0) + (!includesCached && typeof cached === "number" ? cached : 0);
|
|
51737
51796
|
if (total > 0) ctx.session.stats.inputTokens += total;
|
|
51738
51797
|
ctx.session.stats.lastInputTokens = Math.max(0, total - (ctx.session.stats.compressCreditTokens ?? 0));
|
|
51798
|
+
ctx.session.hostContextTokens = total + (ctx.session.hostCreditTokens ?? 0);
|
|
51739
51799
|
if (typeof cached === "number") {
|
|
51740
51800
|
ctx.session.stats.cachedTokens += cached;
|
|
51741
51801
|
ctx.session.stats.cacheSamples += 1;
|
|
@@ -51873,6 +51933,10 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
|
|
|
51873
51933
|
if (usage.inputTokens !== void 0 || usage.outputTokens !== void 0 || usage.cachedTokens !== void 0) {
|
|
51874
51934
|
recordUsage(ctx, usage, round);
|
|
51875
51935
|
}
|
|
51936
|
+
const hostCredit = ctx.session.hostCreditTokens ?? 0;
|
|
51937
|
+
if (hostCredit > 0 && typeof usage.inputTokens === "number") {
|
|
51938
|
+
usage.inputTokens += hostCredit;
|
|
51939
|
+
}
|
|
51876
51940
|
let resolvedText = assistantText;
|
|
51877
51941
|
let allCalls = calls;
|
|
51878
51942
|
if (ctx.textProtocol && assistantText.length > 0 && adapter.extractTextTriggers) {
|
|
@@ -52117,6 +52181,23 @@ function usageTotals(protocol, usage) {
|
|
|
52117
52181
|
cached: num3(usage["input_tokens_details"]?.["cached_tokens"])
|
|
52118
52182
|
};
|
|
52119
52183
|
}
|
|
52184
|
+
function backfillHostUsage(protocol, usage, credit) {
|
|
52185
|
+
if (!Number.isFinite(credit) || credit <= 0) return false;
|
|
52186
|
+
let patched = false;
|
|
52187
|
+
const add = (key) => {
|
|
52188
|
+
if (typeof usage[key] === "number" && Number.isFinite(usage[key])) {
|
|
52189
|
+
usage[key] = usage[key] + credit;
|
|
52190
|
+
patched = true;
|
|
52191
|
+
}
|
|
52192
|
+
};
|
|
52193
|
+
if (protocol === "openai") {
|
|
52194
|
+
add("prompt_tokens");
|
|
52195
|
+
add("total_tokens");
|
|
52196
|
+
} else {
|
|
52197
|
+
add("input_tokens");
|
|
52198
|
+
}
|
|
52199
|
+
return patched;
|
|
52200
|
+
}
|
|
52120
52201
|
var CONTEXT_OVERFLOW_PATTERNS = [
|
|
52121
52202
|
/context_length_exceeded/i,
|
|
52122
52203
|
/context_window_exceeded/i,
|
|
@@ -52805,7 +52886,7 @@ function stripFinishReasonChunk(buf) {
|
|
|
52805
52886
|
return buf;
|
|
52806
52887
|
}
|
|
52807
52888
|
}
|
|
52808
|
-
function createOpenaiAdapter(requestBody, clientSystem) {
|
|
52889
|
+
function createOpenaiAdapter(requestBody, clientSystem, hostCredit = 0) {
|
|
52809
52890
|
const model = requestBody.model ?? "unknown";
|
|
52810
52891
|
let responseId = `chatcmpl-proxy-${Date.now()}`;
|
|
52811
52892
|
let toolIndex = 0;
|
|
@@ -52999,7 +53080,24 @@ function createOpenaiAdapter(requestBody, clientSystem) {
|
|
|
52999
53080
|
cachedTokens: typeof pd?.cached_tokens === "number" ? pd.cached_tokens : void 0
|
|
53000
53081
|
};
|
|
53001
53082
|
if (sawRealToolCall) {
|
|
53002
|
-
|
|
53083
|
+
let chunk = rawBuf;
|
|
53084
|
+
if (hostCredit > 0 && u2) {
|
|
53085
|
+
const pu = typeof u2.prompt_tokens === "number" ? u2.prompt_tokens : void 0;
|
|
53086
|
+
const tu = typeof u2.total_tokens === "number" ? u2.total_tokens : void 0;
|
|
53087
|
+
if (pu !== void 0 || tu !== void 0) {
|
|
53088
|
+
const patched = {
|
|
53089
|
+
...parsed,
|
|
53090
|
+
usage: {
|
|
53091
|
+
...u2,
|
|
53092
|
+
...pu !== void 0 ? { prompt_tokens: pu + hostCredit } : {},
|
|
53093
|
+
...tu !== void 0 ? { total_tokens: tu + hostCredit } : {}
|
|
53094
|
+
}
|
|
53095
|
+
};
|
|
53096
|
+
const out = eventStr.split("\n").map((l) => l.startsWith("data:") ? `data: ${JSON.stringify(patched)}` : l).join("\n");
|
|
53097
|
+
chunk = Buffer.from(out + "\n\n", "utf8");
|
|
53098
|
+
}
|
|
53099
|
+
}
|
|
53100
|
+
yield { kind: "meta", chunk };
|
|
53003
53101
|
yield { kind: "done", finishReason, suppressCompletion: true };
|
|
53004
53102
|
continue;
|
|
53005
53103
|
} else {
|
|
@@ -53185,7 +53283,7 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: "tex
|
|
|
53185
53283
|
"utf8"
|
|
53186
53284
|
);
|
|
53187
53285
|
}
|
|
53188
|
-
function createAnthropicAdapter(requestBody, originalSystem) {
|
|
53286
|
+
function createAnthropicAdapter(requestBody, originalSystem, hostCredit = 0) {
|
|
53189
53287
|
const model = requestBody.model ?? void 0;
|
|
53190
53288
|
let messageId;
|
|
53191
53289
|
let clientIndex = 0;
|
|
@@ -53299,8 +53397,19 @@ ${systemPrompt}` : systemPrompt;
|
|
|
53299
53397
|
if (typeof u2.input_tokens === "number") roundInput = u2.input_tokens;
|
|
53300
53398
|
if (typeof u2.cache_read_input_tokens === "number") roundCached = u2.cache_read_input_tokens;
|
|
53301
53399
|
if (round === 1) {
|
|
53400
|
+
let chunk = rawBuf;
|
|
53401
|
+
if (hostCredit > 0 && typeof u2.input_tokens === "number") {
|
|
53402
|
+
const patched = structuredClone(data);
|
|
53403
|
+
const pmsg = patched["message"];
|
|
53404
|
+
const pu = pmsg?.["usage"] ?? {};
|
|
53405
|
+
if (typeof pu.input_tokens === "number") {
|
|
53406
|
+
pu.input_tokens += hostCredit;
|
|
53407
|
+
const out = eventStr.split("\n").map((l) => l.startsWith("data:") ? `data: ${JSON.stringify(patched)}` : l).join("\n");
|
|
53408
|
+
chunk = Buffer.from(out + "\n\n", "utf8");
|
|
53409
|
+
}
|
|
53410
|
+
}
|
|
53302
53411
|
messageStartForwarded = true;
|
|
53303
|
-
yield { kind: "meta", chunk
|
|
53412
|
+
yield { kind: "meta", chunk, firstRoundOnly: true };
|
|
53304
53413
|
}
|
|
53305
53414
|
} else if (type === "ping") {
|
|
53306
53415
|
yield { kind: "meta", chunk: rawBuf };
|
|
@@ -53483,10 +53592,10 @@ data: ${JSON.stringify({ type: "content_block_stop", index })}
|
|
|
53483
53592
|
}
|
|
53484
53593
|
|
|
53485
53594
|
// src/loop/index.ts
|
|
53486
|
-
function pickAdapter(protocol, requestBody, textProtocol, responsesProjection, anthropicSystem, openaiSystem) {
|
|
53595
|
+
function pickAdapter(protocol, requestBody, textProtocol, responsesProjection, anthropicSystem, openaiSystem, hostCredit = 0) {
|
|
53487
53596
|
if (protocol === "responses") return createResponsesAdapter(textProtocol, responsesProjection);
|
|
53488
|
-
if (protocol === "openai") return createOpenaiAdapter(requestBody, openaiSystem);
|
|
53489
|
-
if (protocol === "anthropic") return createAnthropicAdapter(requestBody, anthropicSystem);
|
|
53597
|
+
if (protocol === "openai") return createOpenaiAdapter(requestBody, openaiSystem, hostCredit);
|
|
53598
|
+
if (protocol === "anthropic") return createAnthropicAdapter(requestBody, anthropicSystem, hostCredit);
|
|
53490
53599
|
throw new Error(`[acp-loop] unknown protocol: ${protocol}`);
|
|
53491
53600
|
}
|
|
53492
53601
|
|
|
@@ -54705,12 +54814,12 @@ function handlePluginStatus(conversationId2, res, fallbackLatest = false) {
|
|
|
54705
54814
|
if (entry) entry.lastSeen = Date.now();
|
|
54706
54815
|
const limit = session.metadata.effectiveContextLimit;
|
|
54707
54816
|
const mem = remembered.get(session.id);
|
|
54708
|
-
const modelContextLimit = typeof limit === "number" && limit > 0 ? limit :
|
|
54817
|
+
const modelContextLimit = typeof limit === "number" && limit > 0 ? limit : 0;
|
|
54709
54818
|
let panel;
|
|
54710
54819
|
try {
|
|
54711
54820
|
panel = buildStatusPanel({
|
|
54712
54821
|
version: `billion-context@${PROXY_VERSION}`,
|
|
54713
|
-
tokenCount: session.stats.lastInputTokens,
|
|
54822
|
+
tokenCount: session.hostContextTokens ?? session.stats.lastInputTokens,
|
|
54714
54823
|
systemPromptTokens: 0,
|
|
54715
54824
|
state: session.state,
|
|
54716
54825
|
nudge: mem?.nudge,
|
|
@@ -54728,7 +54837,8 @@ function handlePluginStatus(conversationId2, res, fallbackLatest = false) {
|
|
|
54728
54837
|
label: session.meta.label ?? null,
|
|
54729
54838
|
pluginAgent: session.metadata.pluginAgent ?? null,
|
|
54730
54839
|
contextLimit: typeof limit === "number" ? limit : null,
|
|
54731
|
-
contextTokens: session.stats.lastInputTokens,
|
|
54840
|
+
contextTokens: session.hostContextTokens ?? session.stats.lastInputTokens,
|
|
54841
|
+
hostCredit: session.hostCreditTokens ?? 0,
|
|
54732
54842
|
inputTokens: session.stats.inputTokens,
|
|
54733
54843
|
outputTokens: session.stats.outputTokens,
|
|
54734
54844
|
cachedTokens: session.stats.cachedTokens,
|
|
@@ -54845,6 +54955,7 @@ function applyUsageSample(session, sample, protocol) {
|
|
|
54845
54955
|
session.stats.inputTokens += total;
|
|
54846
54956
|
session.stats.lastInputTokens = Math.max(0, total - (session.stats.compressCreditTokens ?? 0));
|
|
54847
54957
|
warnCacheCollapse(session, total, sample.cachedTokens ?? 0);
|
|
54958
|
+
session.hostContextTokens = total + (session.hostCreditTokens ?? 0);
|
|
54848
54959
|
}
|
|
54849
54960
|
if (sample.outputTokens !== void 0) session.stats.outputTokens += sample.outputTokens;
|
|
54850
54961
|
}
|
|
@@ -54858,6 +54969,7 @@ async function pipePluginChatWithStrip(stream2, res, protocol, session, log2) {
|
|
|
54858
54969
|
const decoder = new TextDecoder("utf-8");
|
|
54859
54970
|
let buf = "";
|
|
54860
54971
|
const acc = {};
|
|
54972
|
+
const credit = session?.hostCreditTokens ?? 0;
|
|
54861
54973
|
const onDrop = (snippet) => {
|
|
54862
54974
|
log("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
|
|
54863
54975
|
log2?.(`[tag-echo] stripped model-emitted render tag from plugin passthrough text`);
|
|
@@ -55015,12 +55127,23 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: delt
|
|
|
55015
55127
|
if (ev["type"] === "message_stop") sawTerminal = true;
|
|
55016
55128
|
const sample = usageFromSseEvent(ev);
|
|
55017
55129
|
if (sample) mergeUsageSample(acc, sample);
|
|
55130
|
+
let backfilled = false;
|
|
55131
|
+
if (credit > 0 && protocol) {
|
|
55132
|
+
const usage = ev["type"] === "message_start" ? ev["message"]?.["usage"] : ev["usage"];
|
|
55133
|
+
const deltaEcho = ev["type"] === "message_delta" && (num2(usage?.["input_tokens"]) ?? 0) <= 0;
|
|
55134
|
+
if (usage && !deltaEcho && backfillHostUsage(protocol, usage, credit)) backfilled = true;
|
|
55135
|
+
}
|
|
55018
55136
|
const out = protocol === "anthropic" ? processAnthropic(ev, rawEvent) : processOpenai(ev, rawEvent);
|
|
55019
|
-
if (out
|
|
55137
|
+
if (backfilled && out === rawEvent + "\n\n") {
|
|
55138
|
+
await write(rebuildEvent(rawEvent, ev));
|
|
55139
|
+
} else if (out.length > 0) {
|
|
55140
|
+
await write(out);
|
|
55141
|
+
}
|
|
55020
55142
|
}
|
|
55021
55143
|
}
|
|
55022
55144
|
if (res.destroyed || res.writableEnded) break;
|
|
55023
55145
|
}
|
|
55146
|
+
if (buf.length > 0 && !res.destroyed && !res.writableEnded) await write(buf);
|
|
55024
55147
|
const rest = flushTails();
|
|
55025
55148
|
if (rest.length > 0 && !res.destroyed && !res.writableEnded) await write(rest);
|
|
55026
55149
|
settleUsage();
|
|
@@ -55065,6 +55188,7 @@ async function pipePluginResponsesWithStrip(stream2, res, session, log2) {
|
|
|
55065
55188
|
if (!res.write(Buffer.from(s3, "utf8"))) {
|
|
55066
55189
|
return new Promise((r) => res.once("drain", () => r()));
|
|
55067
55190
|
}
|
|
55191
|
+
return Promise.resolve();
|
|
55068
55192
|
};
|
|
55069
55193
|
const settleUsage = () => {
|
|
55070
55194
|
if (session && (acc.inputTokens !== void 0 || acc.outputTokens !== void 0 || acc.cachedTokens !== void 0)) {
|
|
@@ -55121,7 +55245,17 @@ async function pipePluginResponsesWithStrip(stream2, res, session, log2) {
|
|
|
55121
55245
|
const type = ev["type"];
|
|
55122
55246
|
if (type === "response.output_text.done" || type === "response.content_part.done" || type === "response.output_item.done" || type === "response.completed" || type === "response.failed" || type === "response.incomplete") {
|
|
55123
55247
|
if (type === "response.completed" || type === "response.failed" || type === "response.incomplete") sawTerminal = true;
|
|
55124
|
-
|
|
55248
|
+
let evOut = ev;
|
|
55249
|
+
let rebuild = containsRenderTagText(jsonStr);
|
|
55250
|
+
if (rebuild) evOut = stripResponsesText(ev);
|
|
55251
|
+
if (type === "response.completed") {
|
|
55252
|
+
const credit = session?.hostCreditTokens ?? 0;
|
|
55253
|
+
const usage = evOut["response"]?.["usage"];
|
|
55254
|
+
if (credit > 0 && usage && backfillHostUsage("responses", usage, credit)) {
|
|
55255
|
+
rebuild = true;
|
|
55256
|
+
}
|
|
55257
|
+
}
|
|
55258
|
+
const out = rebuild ? rebuildEvent(rawEvent, evOut) : rawEvent + "\n\n";
|
|
55125
55259
|
await write(flushTail(out));
|
|
55126
55260
|
continue;
|
|
55127
55261
|
}
|
|
@@ -55202,8 +55336,10 @@ async function pipePluginJson(stream2, res, session, protocol) {
|
|
|
55202
55336
|
}
|
|
55203
55337
|
reader.releaseLock();
|
|
55204
55338
|
const text = Buffer.concat(chunks).toString("utf8");
|
|
55339
|
+
let json;
|
|
55340
|
+
let mutated = false;
|
|
55205
55341
|
try {
|
|
55206
|
-
|
|
55342
|
+
json = JSON.parse(text);
|
|
55207
55343
|
const usage = json["usage"];
|
|
55208
55344
|
if (session && usage) {
|
|
55209
55345
|
const input = num2(usage["prompt_tokens"]) ?? num2(usage["input_tokens"]);
|
|
@@ -55214,18 +55350,21 @@ async function pipePluginJson(stream2, res, session, protocol) {
|
|
|
55214
55350
|
cachedTokens: num2(usage["prompt_tokens_details"]?.["cached_tokens"]) ?? num2(usage["input_tokens_details"]?.["cached_tokens"]) ?? num2(usage["cache_read_input_tokens"])
|
|
55215
55351
|
}, protocol);
|
|
55216
55352
|
markDirty(session);
|
|
55353
|
+
const credit = session.hostCreditTokens ?? 0;
|
|
55354
|
+
if (credit > 0 && protocol && backfillHostUsage(protocol, usage, credit)) {
|
|
55355
|
+
mutated = true;
|
|
55356
|
+
}
|
|
55217
55357
|
}
|
|
55218
55358
|
}
|
|
55219
55359
|
} catch {
|
|
55220
55360
|
}
|
|
55221
|
-
if (containsRenderTagText(text)) {
|
|
55222
|
-
|
|
55223
|
-
|
|
55224
|
-
|
|
55225
|
-
|
|
55226
|
-
|
|
55227
|
-
|
|
55228
|
-
}
|
|
55361
|
+
if (json && containsRenderTagText(text)) {
|
|
55362
|
+
json = protocol === "responses" ? stripResponsesText(json) : protocol === "anthropic" ? stripAnthropicText(json) : stripOpenaiChatText(json);
|
|
55363
|
+
mutated = true;
|
|
55364
|
+
}
|
|
55365
|
+
if (mutated && json) {
|
|
55366
|
+
res.end(Buffer.from(JSON.stringify(json), "utf8"));
|
|
55367
|
+
return;
|
|
55229
55368
|
}
|
|
55230
55369
|
res.end(text);
|
|
55231
55370
|
}
|
|
@@ -56729,6 +56868,9 @@ function logUnrecognizedPath(log2, url) {
|
|
|
56729
56868
|
log2("info", `unrecognized path ${key}: forwarding unchanged; further occurrences suppressed`);
|
|
56730
56869
|
}
|
|
56731
56870
|
}
|
|
56871
|
+
function isModelDiscoveryPath(path18) {
|
|
56872
|
+
return path18.replace(/\/+$/, "").endsWith("/models");
|
|
56873
|
+
}
|
|
56732
56874
|
var BILI_HOP_HEADER = "x-bili-hop";
|
|
56733
56875
|
function parseLauncherModelWindows(raw) {
|
|
56734
56876
|
if (!raw) return {};
|
|
@@ -56930,7 +57072,7 @@ Content-Length: ${Buffer.byteLength(body)}\r
|
|
|
56930
57072
|
);
|
|
56931
57073
|
}
|
|
56932
57074
|
if (opts.debug) {
|
|
56933
|
-
log2("info", `[debug] build features: raw-HTTP-capture(on) | remote_compaction_v2-strip(on) | cert-MITM-launcher(on) | strip-acp-summary(on) \u2014 seeing this line confirms the launcher build (not registry 0.1.34)`);
|
|
57075
|
+
log2("info", `[debug] build features: raw-HTTP-capture(${bodyDumpEnabled() ? "on" : "off"}) | remote_compaction_v2-strip(on) | cert-MITM-launcher(on) | strip-acp-summary(on) \u2014 seeing this line confirms the launcher build (not registry 0.1.34)`);
|
|
56934
57076
|
}
|
|
56935
57077
|
};
|
|
56936
57078
|
const attemptListen = (port) => {
|
|
@@ -56998,6 +57140,13 @@ Content-Length: ${Buffer.byteLength(body)}\r
|
|
|
56998
57140
|
}
|
|
56999
57141
|
return server;
|
|
57000
57142
|
}
|
|
57143
|
+
var SIDE_REQUEST_MAX_TOKENS = 200;
|
|
57144
|
+
function isSideRequest(parsed) {
|
|
57145
|
+
if (!parsed || typeof parsed !== "object") return false;
|
|
57146
|
+
const p2 = parsed;
|
|
57147
|
+
const raw = p2.max_tokens ?? p2.max_completion_tokens ?? p2.max_output_tokens;
|
|
57148
|
+
return typeof raw === "number" && raw > 0 && raw <= SIDE_REQUEST_MAX_TOKENS;
|
|
57149
|
+
}
|
|
57001
57150
|
function isTrustedAdminOrigin(origin, host, trustedHosts) {
|
|
57002
57151
|
if (!host || !trustedHosts.has(host.toLowerCase())) return false;
|
|
57003
57152
|
if (!origin) return true;
|
|
@@ -57274,11 +57423,11 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
57274
57423
|
const pluginWindow = pluginReportedContextWindow(req.headers);
|
|
57275
57424
|
const launcherWindow = launcherContextWindow(model);
|
|
57276
57425
|
const configuredWindow = resolveConfiguredContextLimit(opts.routes, embeddedUrl, model);
|
|
57277
|
-
const peekWindow =
|
|
57426
|
+
const peekWindow = peekRegistryContext(model, host);
|
|
57278
57427
|
let native = betaWindow ?? pluginWindow ?? launcherWindow ?? configuredWindow ?? peekWindow ?? lookupContextLimit(model);
|
|
57279
57428
|
const operatorWindowTuned = resolveCompress(opts.routes, embeddedUrl, model, opts.compress).modelContextLimit !== void 0;
|
|
57280
57429
|
nativeFromFallback = !betaWindow && !pluginWindow && !launcherWindow && !peekWindow && !configuredWindow && !operatorWindowTuned;
|
|
57281
|
-
if (!native
|
|
57430
|
+
if (!native) {
|
|
57282
57431
|
native = await contextFromRegistry(model, host);
|
|
57283
57432
|
if (native) nativeFromFallback = false;
|
|
57284
57433
|
}
|
|
@@ -57379,7 +57528,6 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
57379
57528
|
if (pluginAgent && !pluginConversation) pluginConversation = conversation;
|
|
57380
57529
|
if (pluginAgent) {
|
|
57381
57530
|
if (session.metadata.pluginAgent !== pluginAgent) session.metadata.pluginAgent = pluginAgent;
|
|
57382
|
-
session.metadata.effectiveContextLimit = reqConfig.modelContextLimit;
|
|
57383
57531
|
recordPluginSession(pluginConversation ?? conversation, session.id);
|
|
57384
57532
|
}
|
|
57385
57533
|
if (protocol === "responses" || protocol === "openai" || protocol === "anthropic") {
|
|
@@ -57389,6 +57537,21 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
57389
57537
|
}
|
|
57390
57538
|
}
|
|
57391
57539
|
const pluginMode = pluginAgent !== void 0;
|
|
57540
|
+
if (!countTokens && !responsesCompact && protocol !== null && isSideRequest(parsed)) {
|
|
57541
|
+
log2("info", `[${session.id}] side request (max_tokens<=${SIDE_REQUEST_MAX_TOKENS}) \u2192 passthrough + tag strip only, kernel state untouched`);
|
|
57542
|
+
const sidePrepared = {
|
|
57543
|
+
body: bodyBuffer,
|
|
57544
|
+
session,
|
|
57545
|
+
processedMessages: [],
|
|
57546
|
+
originalMessages: [],
|
|
57547
|
+
protocol,
|
|
57548
|
+
stream: parsed.stream === true,
|
|
57549
|
+
compressInjected: false,
|
|
57550
|
+
sidePassthrough: true
|
|
57551
|
+
};
|
|
57552
|
+
await forward(req, res, opts, bodyBuffer, sidePrepared, core, reqConfig, log2, route, instanceId, affinity);
|
|
57553
|
+
return;
|
|
57554
|
+
}
|
|
57392
57555
|
const reqModel = parsed.model;
|
|
57393
57556
|
const learnedMap = session.metadata.learnedContextLimits;
|
|
57394
57557
|
const learnedLimit = (reqModel && learnedMap ? learnedMap[reqModel] : void 0) ?? session.metadata.learnedContextLimit;
|
|
@@ -57397,6 +57560,22 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
57397
57560
|
reqConfig = { ...reqConfig, modelContextLimit: learnedLimit };
|
|
57398
57561
|
nativeFromFallback = false;
|
|
57399
57562
|
log2("info", `[${session.id}] self-healed context window: ${resolved} \u2192 ${learnedLimit} (learned from an upstream overflow)`);
|
|
57563
|
+
} else if (nativeFromFallback && reqModel) {
|
|
57564
|
+
const prevInput = session.stats.lastInputTokens ?? 0;
|
|
57565
|
+
const prevWindow = session.metadata.lastTurnWindow;
|
|
57566
|
+
const resolved = reqConfig.modelContextLimit;
|
|
57567
|
+
if (prevWindow !== void 0 && prevInput > prevWindow && prevInput > resolved && prevInput >= 1e3) {
|
|
57568
|
+
const map = session.metadata.learnedContextLimits ?? {};
|
|
57569
|
+
const prev = map[reqModel];
|
|
57570
|
+
if (prev === void 0 || prevInput > prev) {
|
|
57571
|
+
map[reqModel] = prevInput;
|
|
57572
|
+
session.metadata.learnedContextLimits = map;
|
|
57573
|
+
markDirty(session);
|
|
57574
|
+
}
|
|
57575
|
+
reqConfig = { ...reqConfig, modelContextLimit: prevInput };
|
|
57576
|
+
nativeFromFallback = false;
|
|
57577
|
+
log2("info", `[${session.id}] self-healed context window upward: ${resolved} \u2192 ${prevInput} (a prior turn used ${prevInput} input tokens, exceeding the fallback window)`);
|
|
57578
|
+
}
|
|
57400
57579
|
}
|
|
57401
57580
|
const nativeWindow = reqConfig.modelContextLimit;
|
|
57402
57581
|
if (shouldReserveOutputHeadroom(protocol)) {
|
|
@@ -57410,6 +57589,8 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
57410
57589
|
}
|
|
57411
57590
|
if (reserved !== reqConfig.modelContextLimit) reqConfig = { ...reqConfig, modelContextLimit: reserved };
|
|
57412
57591
|
}
|
|
57592
|
+
session.metadata.effectiveContextLimit = reqConfig.modelContextLimit;
|
|
57593
|
+
session.metadata.lastTurnWindow = reqConfig.modelContextLimit;
|
|
57413
57594
|
acquireInFlight(session);
|
|
57414
57595
|
try {
|
|
57415
57596
|
await withSessionLock(session, async () => {
|
|
@@ -57474,7 +57655,7 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
57474
57655
|
}
|
|
57475
57656
|
}
|
|
57476
57657
|
if (!prepared) {
|
|
57477
|
-
if (protocol === null && !opts.passthrough) {
|
|
57658
|
+
if (protocol === null && !opts.passthrough && !isModelDiscoveryPath(urlPath)) {
|
|
57478
57659
|
logUnrecognizedPath(log2, req.url ?? "");
|
|
57479
57660
|
}
|
|
57480
57661
|
await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log2, route, instanceId, void 0);
|
|
@@ -57520,6 +57701,7 @@ function prepareAnthropic(parsed, req, opts, core, config, prompts, log2, sessio
|
|
|
57520
57701
|
const sessionId = session.id;
|
|
57521
57702
|
const stream2 = parsed.stream === true;
|
|
57522
57703
|
++session.stats.requests;
|
|
57704
|
+
session.hostCreditTokens = 0;
|
|
57523
57705
|
const injectTools = opts.compress.injectTool && !pluginMode;
|
|
57524
57706
|
if (isAutoModeClassifier(parsed)) {
|
|
57525
57707
|
log2("info", `[${sessionId}] auto-mode classifier passthrough (skipping compress injection)`);
|
|
@@ -57575,6 +57757,10 @@ function prepareAnthropic(parsed, req, opts, core, config, prompts, log2, sessio
|
|
|
57575
57757
|
markDirty(session);
|
|
57576
57758
|
const rebuilt = { ...parsed, messages: rebuiltMessages, system: systemOut, tools: toolsOut };
|
|
57577
57759
|
delete rebuilt.prompt_cache_key;
|
|
57760
|
+
session.hostCreditTokens = processedMessages.length > 0 ? Math.max(0, estimateCoreMessages(originalMessages) - estimateCoreMessages(processedMessages)) : 0;
|
|
57761
|
+
if (session.hostCreditTokens > 0) {
|
|
57762
|
+
log2("info", `[${sessionId}] host usage backfill armed: +${session.hostCreditTokens} tok (forwarded view is folded); host usage will report the uncompressed baseline`);
|
|
57763
|
+
}
|
|
57578
57764
|
return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, anthropicSystem: parsed.system, protocol: "anthropic", stream: stream2, compressInjected: injectTools, pluginMode, nudge, prompts, renderTags: "text-only" };
|
|
57579
57765
|
}
|
|
57580
57766
|
var OUTPUT_CLAMP_MARGIN_PCT = 0.05;
|
|
@@ -57610,6 +57796,7 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session,
|
|
|
57610
57796
|
const sessionId = session.id;
|
|
57611
57797
|
const stream2 = parsed.stream === true;
|
|
57612
57798
|
++session.stats.requests;
|
|
57799
|
+
session.hostCreditTokens = 0;
|
|
57613
57800
|
let openaiSystemText = "";
|
|
57614
57801
|
let processedMessages = [];
|
|
57615
57802
|
let originalMessages = [];
|
|
@@ -57669,6 +57856,10 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session,
|
|
|
57669
57856
|
if (stream2 && rebuilt.stream_options === void 0) {
|
|
57670
57857
|
rebuilt.stream_options = { include_usage: true };
|
|
57671
57858
|
}
|
|
57859
|
+
session.hostCreditTokens = processedMessages.length > 0 ? Math.max(0, estimateCoreMessages(originalMessages) - estimateCoreMessages(processedMessages)) : 0;
|
|
57860
|
+
if (session.hostCreditTokens > 0) {
|
|
57861
|
+
log2("info", `[${sessionId}] host usage backfill armed: +${session.hostCreditTokens} tok (forwarded view is folded); host usage will report the uncompressed baseline`);
|
|
57862
|
+
}
|
|
57672
57863
|
snapshotMessages(session, originalMessages);
|
|
57673
57864
|
markDirty(session);
|
|
57674
57865
|
return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "openai", stream: stream2, compressInjected: injectTools, pluginMode, nudge, prompts, openaiSystemText, renderTags: "text-only" };
|
|
@@ -57677,6 +57868,7 @@ function prepareResponses(parsed, req, opts, core, config, prompts, log2, sessio
|
|
|
57677
57868
|
const sessionId = session.id;
|
|
57678
57869
|
const stream2 = parsed.stream === true;
|
|
57679
57870
|
++session.stats.requests;
|
|
57871
|
+
session.hostCreditTokens = 0;
|
|
57680
57872
|
if (reconcileNativeCompactionBoundary(session)) {
|
|
57681
57873
|
log2("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`);
|
|
57682
57874
|
}
|
|
@@ -57802,6 +57994,10 @@ function prepareResponses(parsed, req, opts, core, config, prompts, log2, sessio
|
|
|
57802
57994
|
});
|
|
57803
57995
|
log2("info", `[${sessionId}] responses forward tools=[${fwdTools.join(",")}] injectTool=${injectTools}${pluginMode ? " (plugin mode: wire injection suppressed)" : ""} NO_INJECT_TOOL=${!!process.env.ACP_NO_INJECT_TOOL} NO_COMPRESS_PROMPT=${!!process.env.ACP_NO_COMPRESS_PROMPT}`);
|
|
57804
57996
|
}
|
|
57997
|
+
session.hostCreditTokens = processedMessages.length > 0 ? Math.max(0, estimateCoreMessages(originalMessages) - estimateCoreMessages(processedMessages)) : 0;
|
|
57998
|
+
if (session.hostCreditTokens > 0) {
|
|
57999
|
+
log2("info", `[${sessionId}] host usage backfill armed: +${session.hostCreditTokens} tok (forwarded view is folded); host usage will report the uncompressed baseline`);
|
|
58000
|
+
}
|
|
57805
58001
|
snapshotMessages(session, originalMessages);
|
|
57806
58002
|
markDirty(session);
|
|
57807
58003
|
return {
|
|
@@ -58311,7 +58507,7 @@ ${hdrText}
|
|
|
58311
58507
|
}
|
|
58312
58508
|
let responseBody = upstream.body;
|
|
58313
58509
|
try {
|
|
58314
|
-
if (prepared !== null && prepared.stream && maxFakeCompletionRetries() > 0) {
|
|
58510
|
+
if (prepared !== null && prepared.stream && !prepared.sidePassthrough && maxFakeCompletionRetries() > 0) {
|
|
58315
58511
|
const resolvedBuf = await resolveFakeCompletion(upstream.body, {
|
|
58316
58512
|
protocol: prepared.protocol,
|
|
58317
58513
|
body,
|
|
@@ -58378,7 +58574,7 @@ ${hdrText}
|
|
|
58378
58574
|
const reqHeaders = buildForwardHeaders(headers);
|
|
58379
58575
|
const textProtocol = prepared.protocol === "responses" && !!prepared.responsesTextProtocol;
|
|
58380
58576
|
const systemPrompt = textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts);
|
|
58381
|
-
const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText);
|
|
58577
|
+
const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, prepared.session.hostCreditTokens ?? 0);
|
|
58382
58578
|
const refreshFolded = (current) => {
|
|
58383
58579
|
const turn = core.processTurn({
|
|
58384
58580
|
messages: prepared.originalMessages,
|
|
@@ -58460,6 +58656,10 @@ ${hdrText}
|
|
|
58460
58656
|
const out = u2.completion_tokens ?? u2.output_tokens;
|
|
58461
58657
|
if (typeof out === "number") prepared.session.stats.outputTokens += out;
|
|
58462
58658
|
}
|
|
58659
|
+
const credit = prepared.session.hostCreditTokens ?? 0;
|
|
58660
|
+
if (credit > 0 && backfillHostUsage(prepared.protocol, u2, credit)) {
|
|
58661
|
+
prepared.session.hostContextTokens = (typeof total === "number" ? total : 0) + credit;
|
|
58662
|
+
}
|
|
58463
58663
|
if (prepared.protocol === "openai") {
|
|
58464
58664
|
rewriteOpenaiJsonResponse(json, ctx);
|
|
58465
58665
|
} else if (prepared.protocol === "responses") {
|