billion-context-pi 0.1.49 → 0.1.50-pr.222.22
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 +45 -16
- package/dist/index.js.map +1 -1
- package/dist/messages.d.ts +1 -0
- package/dist/tokens.d.ts +11 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3753,6 +3753,15 @@ function extractText(content) {
|
|
|
3753
3753
|
function stripRefTag(text) {
|
|
3754
3754
|
return text.replace(REF_TAG, "").replace(TRAILING_REF_TAG, "");
|
|
3755
3755
|
}
|
|
3756
|
+
function countImageBlocks(content) {
|
|
3757
|
+
if (!Array.isArray(content)) return 0;
|
|
3758
|
+
let n = 0;
|
|
3759
|
+
for (const block of content) {
|
|
3760
|
+
const b = block;
|
|
3761
|
+
if (b?.type === "image") n++;
|
|
3762
|
+
}
|
|
3763
|
+
return n;
|
|
3764
|
+
}
|
|
3756
3765
|
function messageIdentity(message) {
|
|
3757
3766
|
return JSON.stringify(normalizeIdentityValue(message, true));
|
|
3758
3767
|
}
|
|
@@ -9172,12 +9181,29 @@ function collectCoveredMessageIds(state) {
|
|
|
9172
9181
|
}
|
|
9173
9182
|
return ids;
|
|
9174
9183
|
}
|
|
9175
|
-
|
|
9184
|
+
var IMAGE_TOKEN_COST = 1600;
|
|
9185
|
+
function modelSupportsImages(model) {
|
|
9186
|
+
const input = model?.input;
|
|
9187
|
+
return Array.isArray(input) && input.includes("image");
|
|
9188
|
+
}
|
|
9189
|
+
function collectImageTokens(entries, visionCapable) {
|
|
9190
|
+
const out = /* @__PURE__ */ new Map();
|
|
9191
|
+
if (!visionCapable) return out;
|
|
9192
|
+
for (const e of entries) {
|
|
9193
|
+
if (e.type !== "message") continue;
|
|
9194
|
+
const n = countImageBlocks(e.message?.content);
|
|
9195
|
+
if (n > 0) out.set(e.id, n * IMAGE_TOKEN_COST);
|
|
9196
|
+
}
|
|
9197
|
+
return out;
|
|
9198
|
+
}
|
|
9199
|
+
function estimateTokens(messages, coveredIds, imageTokensById) {
|
|
9176
9200
|
let tokens = 0;
|
|
9177
9201
|
for (const m of messages) {
|
|
9178
9202
|
if (m.toolName === "compress") continue;
|
|
9179
9203
|
if (coveredIds?.has(m.id)) continue;
|
|
9180
9204
|
tokens += defaultCountTokens(m.text ?? "");
|
|
9205
|
+
const img = imageTokensById?.get(m.id);
|
|
9206
|
+
if (img) tokens += img;
|
|
9181
9207
|
}
|
|
9182
9208
|
return tokens;
|
|
9183
9209
|
}
|
|
@@ -9306,12 +9332,13 @@ async function handleCompress(args, runtime, ctx, toolCallId) {
|
|
|
9306
9332
|
if (typeof maybeRanges === "string") throw new Error(maybeRanges);
|
|
9307
9333
|
const ranges = maybeRanges;
|
|
9308
9334
|
if (ranges.length === 0) return "No ranges provided.";
|
|
9309
|
-
const { state: initialState, coreMessages } = await runtime.stateFor(ctx);
|
|
9335
|
+
const { state: initialState, coreMessages, entries } = await runtime.stateFor(ctx);
|
|
9310
9336
|
const config = runtime.configFor(ctx);
|
|
9311
9337
|
const modelId = ctx.model?.id ?? "default";
|
|
9312
9338
|
const systemPromptText = getSystemPromptText(ctx);
|
|
9313
9339
|
const systemPromptTokens = systemPromptText ? defaultCountTokens(systemPromptText) : 0;
|
|
9314
|
-
const
|
|
9340
|
+
const imageTokens = collectImageTokens(entries, modelSupportsImages(ctx.model));
|
|
9341
|
+
const sentTokens = estimateTokens(coreMessages, collectCoveredMessageIds(initialState), imageTokens) + systemPromptTokens;
|
|
9315
9342
|
const turn = runtime.core.processTurn({
|
|
9316
9343
|
messages: coreMessages,
|
|
9317
9344
|
state: initialState,
|
|
@@ -9321,7 +9348,7 @@ async function handleCompress(args, runtime, ctx, toolCallId) {
|
|
|
9321
9348
|
const state = turn.state;
|
|
9322
9349
|
const messages = turn.messages;
|
|
9323
9350
|
const density = runtime.density.densityFor(modelId);
|
|
9324
|
-
const beforeTokens = calibrateTokens(estimateTokens(messages, collectCoveredMessageIds(state)), density);
|
|
9351
|
+
const beforeTokens = calibrateTokens(estimateTokens(messages, collectCoveredMessageIds(state), imageTokens), density);
|
|
9325
9352
|
const summaryMaxChars = args.summaryMaxChars;
|
|
9326
9353
|
const topLevelTopic = args.topic;
|
|
9327
9354
|
debug.event("compress-in", {
|
|
@@ -9361,7 +9388,7 @@ async function handleCompress(args, runtime, ctx, toolCallId) {
|
|
|
9361
9388
|
config,
|
|
9362
9389
|
tokenCount: calibrateTokens(sentTokens, density)
|
|
9363
9390
|
});
|
|
9364
|
-
const afterTokens = calibrateTokens(estimateTokens(afterTurn.messages, collectCoveredMessageIds(applied.state)), density);
|
|
9391
|
+
const afterTokens = calibrateTokens(estimateTokens(afterTurn.messages, collectCoveredMessageIds(applied.state), imageTokens), density);
|
|
9365
9392
|
const reclaimed = Math.max(0, beforeTokens - afterTokens);
|
|
9366
9393
|
const newBlocks = applied.state.blocks.slice(-blocksCreated);
|
|
9367
9394
|
debug.event("compress-out", {
|
|
@@ -11038,13 +11065,13 @@ function makeStatusTool(runtime) {
|
|
|
11038
11065
|
};
|
|
11039
11066
|
}
|
|
11040
11067
|
async function handleStatus(args, runtime, ctx) {
|
|
11041
|
-
const { state, coreMessages } = await runtime.stateFor(ctx);
|
|
11068
|
+
const { state, coreMessages, entries } = await runtime.stateFor(ctx);
|
|
11042
11069
|
const config = runtime.configFor(ctx);
|
|
11043
11070
|
const coveredIds = collectCoveredMessageIds(state);
|
|
11044
11071
|
const modelId = ctx.model?.id ?? "default";
|
|
11045
11072
|
const systemPromptText = getSystemPromptText(ctx);
|
|
11046
11073
|
const systemPromptTokens = systemPromptText ? defaultCountTokens(systemPromptText) : 0;
|
|
11047
|
-
const sentTokens = estimateTokens(coreMessages, coveredIds) + systemPromptTokens;
|
|
11074
|
+
const sentTokens = estimateTokens(coreMessages, coveredIds, collectImageTokens(entries, modelSupportsImages(ctx.model))) + systemPromptTokens;
|
|
11048
11075
|
const turn = runtime.core.processTurn({
|
|
11049
11076
|
messages: coreMessages,
|
|
11050
11077
|
state,
|
|
@@ -11528,12 +11555,14 @@ async function statusReport(runtime, ctx) {
|
|
|
11528
11555
|
const realUsage = ctx.getContextUsage?.();
|
|
11529
11556
|
const systemPromptText = getSystemPromptText(ctx);
|
|
11530
11557
|
const systemPromptTokens = systemPromptText ? defaultCountTokens(systemPromptText) : 0;
|
|
11531
|
-
const
|
|
11558
|
+
const imageTokens = collectImageTokens(entries, modelSupportsImages(ctx.model));
|
|
11559
|
+
const imageTokensTotal = [...imageTokens.values()].reduce((a, b) => a + b, 0);
|
|
11560
|
+
const sessionTokens = realUsage?.tokens && realUsage.tokens > 0 ? realUsage.tokens : defaultCountTokens(coreMessages.map((m) => m.text ?? "").join("\n")) + imageTokensTotal;
|
|
11532
11561
|
const coveredIds = collectCoveredMessageIds(state);
|
|
11533
11562
|
const modelId = ctx.model?.id ?? "default";
|
|
11534
|
-
const sentTokens = estimateTokens(coreMessages, coveredIds) + systemPromptTokens;
|
|
11563
|
+
const sentTokens = estimateTokens(coreMessages, coveredIds, imageTokens) + systemPromptTokens;
|
|
11535
11564
|
const turn = runtime.core.processTurn({ messages: coreMessages, state, config, tokenCount: calibrateTokens(sentTokens, runtime.density.densityFor(modelId)) });
|
|
11536
|
-
const versionStr = "0.1.
|
|
11565
|
+
const versionStr = "0.1.50" ? `billion-context-pi@${"0.1.50"}` : void 0;
|
|
11537
11566
|
let text = buildStatusPanel({
|
|
11538
11567
|
version: versionStr,
|
|
11539
11568
|
tokenCount: sessionTokens,
|
|
@@ -11541,7 +11570,7 @@ async function statusReport(runtime, ctx) {
|
|
|
11541
11570
|
state: turn.state,
|
|
11542
11571
|
nudge: turn.nudge,
|
|
11543
11572
|
modelContextLimit: config.modelContextLimit,
|
|
11544
|
-
unprunedTokens: coreMessages.reduce((sum, m) => sum + defaultCountTokens(m.text ?? ""), 0),
|
|
11573
|
+
unprunedTokens: coreMessages.reduce((sum, m) => sum + defaultCountTokens(m.text ?? "") + (imageTokens.get(m.id) ?? 0), 0),
|
|
11545
11574
|
cacheUsages: cacheUsageSamples(entries ?? [])
|
|
11546
11575
|
});
|
|
11547
11576
|
const delegateUsage = getDelegateUsage();
|
|
@@ -11896,7 +11925,7 @@ async function autoInstallLatest(latest, extDirOverride) {
|
|
|
11896
11925
|
"--no-fund",
|
|
11897
11926
|
"--no-save"
|
|
11898
11927
|
];
|
|
11899
|
-
const prevVersion = (await readPackageJson(join8(extDir, "package.json")))?.version ?? "0.1.
|
|
11928
|
+
const prevVersion = (await readPackageJson(join8(extDir, "package.json")))?.version ?? "0.1.50";
|
|
11900
11929
|
const { code, stderr } = await runNpmImpl(installArgs(latest), { cwd: npmDir, timeout: 6e4 });
|
|
11901
11930
|
if (code !== 0) {
|
|
11902
11931
|
logWarn("update", {
|
|
@@ -11909,7 +11938,7 @@ async function autoInstallLatest(latest, extDirOverride) {
|
|
|
11909
11938
|
}
|
|
11910
11939
|
const verify = await verifyInstall(npmDir, latest);
|
|
11911
11940
|
if (!verify.ok) {
|
|
11912
|
-
const rollbackTo = SEMVER_RE.test(prevVersion) ? prevVersion : "0.1.
|
|
11941
|
+
const rollbackTo = SEMVER_RE.test(prevVersion) ? prevVersion : "0.1.50";
|
|
11913
11942
|
logWarn("update", { event: "auto-install-verify-failed", latest, reason: verify.reason, rollbackTo });
|
|
11914
11943
|
const rb = await runNpmImpl(installArgs(rollbackTo), { cwd: npmDir, timeout: 6e4 });
|
|
11915
11944
|
logInfo("update", { event: "rollback", from: latest, to: rollbackTo, ok: rb.code === 0 });
|
|
@@ -11970,7 +11999,7 @@ async function checkForUpdate(autoUpdate, notify) {
|
|
|
11970
11999
|
const runtimeVersion = await getRuntimeVersion();
|
|
11971
12000
|
const latest = await fetchLatestVersion();
|
|
11972
12001
|
if (!latest) return;
|
|
11973
|
-
const current = runtimeVersion ?? "0.1.
|
|
12002
|
+
const current = runtimeVersion ?? "0.1.50";
|
|
11974
12003
|
const hasUpdate = isNewer(latest, current);
|
|
11975
12004
|
debug.event("update-check", {
|
|
11976
12005
|
current,
|
|
@@ -12049,7 +12078,7 @@ function wireSessionLifecycle(pi, runtime) {
|
|
|
12049
12078
|
const sid = ctx.sessionManager.getSessionId();
|
|
12050
12079
|
runtime.clearSessionTracking(sid);
|
|
12051
12080
|
const modelInfo = ctx.model;
|
|
12052
|
-
logInfo("session", { event: "start", sid, cwd: ctx.cwd, debug: runtime.adapter.debug ?? null, version: true ? "0.1.
|
|
12081
|
+
logInfo("session", { event: "start", sid, cwd: ctx.cwd, debug: runtime.adapter.debug ?? null, version: true ? "0.1.50" : null, model: modelInfo?.id ?? null, modelApi: modelInfo?.api ?? null, contextWindow: modelInfo?.contextWindow ?? null });
|
|
12053
12082
|
try {
|
|
12054
12083
|
await runtime.reloadConfig(ctx.cwd);
|
|
12055
12084
|
setDelegateDisplayUsage(resolveDelegate(runtime.adapter).displayUsage);
|
|
@@ -12108,7 +12137,7 @@ function wireContextTransform(pi, runtime) {
|
|
|
12108
12137
|
const realUsage = ctx.getContextUsage?.();
|
|
12109
12138
|
const systemPromptText = getSystemPromptText(ctx);
|
|
12110
12139
|
const systemPromptTokens = systemPromptText ? defaultCountTokens(systemPromptText) : 0;
|
|
12111
|
-
const sentTokens = estimateTokens(coreMessages, coveredIds) + systemPromptTokens;
|
|
12140
|
+
const sentTokens = estimateTokens(coreMessages, coveredIds, collectImageTokens(entries, modelSupportsImages(ctx.model))) + systemPromptTokens;
|
|
12112
12141
|
let tokenCount = calibrateTokens(sentTokens, runtime.density.densityFor(modelId));
|
|
12113
12142
|
if (ov.armed && config.modelContextLimit > 0) {
|
|
12114
12143
|
ov.armed = false;
|