blun-king-cli 9.1.355 → 9.1.356
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/bin/system-prompt-token-cache-policy.cjs +60 -0
- package/blun.mjs +6 -4
- package/package.json +1 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_MAX_ENTRIES = 4;
|
|
4
|
+
const DEFAULT_MAX_PROMPT_CHARS = 500_000;
|
|
5
|
+
const DEFAULT_MAX_TOTAL_CHARS = 1_000_000;
|
|
6
|
+
|
|
7
|
+
function positiveInteger(value, fallback, name) {
|
|
8
|
+
const resolved = value === undefined ? fallback : value;
|
|
9
|
+
if (!Number.isSafeInteger(resolved) || resolved < 1) {
|
|
10
|
+
throw new TypeError(`${name} must be a positive integer`);
|
|
11
|
+
}
|
|
12
|
+
return resolved;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function createSystemPromptTokenEstimator(estimateTextTokens, options = {}) {
|
|
16
|
+
if (typeof estimateTextTokens !== 'function') {
|
|
17
|
+
throw new TypeError('estimateTextTokens must be a function');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const maxEntries = positiveInteger(options.maxEntries, DEFAULT_MAX_ENTRIES, 'maxEntries');
|
|
21
|
+
const maxPromptChars = positiveInteger(
|
|
22
|
+
options.maxPromptChars,
|
|
23
|
+
DEFAULT_MAX_PROMPT_CHARS,
|
|
24
|
+
'maxPromptChars',
|
|
25
|
+
);
|
|
26
|
+
const maxTotalChars = positiveInteger(
|
|
27
|
+
options.maxTotalChars,
|
|
28
|
+
DEFAULT_MAX_TOTAL_CHARS,
|
|
29
|
+
'maxTotalChars',
|
|
30
|
+
);
|
|
31
|
+
const cache = new Map();
|
|
32
|
+
let cachedChars = 0;
|
|
33
|
+
|
|
34
|
+
return function estimateSystemPrompt(prompt) {
|
|
35
|
+
const cached = cache.get(prompt);
|
|
36
|
+
if (cached !== undefined) {
|
|
37
|
+
cache.delete(prompt);
|
|
38
|
+
cache.set(prompt, cached);
|
|
39
|
+
return cached;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const tokens = estimateTextTokens(prompt);
|
|
43
|
+
if (prompt.length > maxPromptChars || prompt.length > maxTotalChars) return tokens;
|
|
44
|
+
|
|
45
|
+
while (cache.size >= maxEntries || cachedChars + prompt.length > maxTotalChars) {
|
|
46
|
+
const oldestPrompt = cache.keys().next().value;
|
|
47
|
+
if (oldestPrompt === undefined) break;
|
|
48
|
+
cachedChars -= oldestPrompt.length;
|
|
49
|
+
cache.delete(oldestPrompt);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
cache.set(prompt, tokens);
|
|
53
|
+
cachedChars += prompt.length;
|
|
54
|
+
return tokens;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = {
|
|
59
|
+
createSystemPromptTokenEstimator,
|
|
60
|
+
};
|
package/blun.mjs
CHANGED
|
@@ -31019,11 +31019,13 @@ function estimateTokensForContentPart(part) {
|
|
|
31019
31019
|
default: return 0;
|
|
31020
31020
|
}
|
|
31021
31021
|
}
|
|
31022
|
-
var createToolSchemaTokenEstimator, estimateTokensForTools, messageTokenEstimateCache, MEDIA_TOKEN_ESTIMATE, MAX_IMAGE_HEADER_BYTES;
|
|
31022
|
+
var createToolSchemaTokenEstimator, createSystemPromptTokenEstimator, estimateTokensForTools, estimateSystemPromptTokens, messageTokenEstimateCache, MEDIA_TOKEN_ESTIMATE, MAX_IMAGE_HEADER_BYTES;
|
|
31023
31023
|
var init_tokens = __esmMin((() => {
|
|
31024
31024
|
init_file_type$1();
|
|
31025
31025
|
({ createToolSchemaTokenEstimator } = createRequire(import.meta.url)("./bin/tool-schema-token-cache-policy.cjs"));
|
|
31026
|
+
({ createSystemPromptTokenEstimator } = createRequire(import.meta.url)("./bin/system-prompt-token-cache-policy.cjs"));
|
|
31026
31027
|
estimateTokensForTools = createToolSchemaTokenEstimator(estimateTokens$1);
|
|
31028
|
+
estimateSystemPromptTokens = createSystemPromptTokenEstimator(estimateTokens$1);
|
|
31027
31029
|
messageTokenEstimateCache = /* @__PURE__ */ new WeakMap();
|
|
31028
31030
|
MEDIA_TOKEN_ESTIMATE = 2e3;
|
|
31029
31031
|
MAX_IMAGE_HEADER_BYTES = 1024 * 1024;
|
|
@@ -74736,7 +74738,7 @@ var init_kosong_llm = __esmMin((() => {
|
|
|
74736
74738
|
const outgoingMessages = downgradeUnsupportedMedia(enrichedMessages, this.capability, (dropped) => {
|
|
74737
74739
|
this.notifyMediaDropped(dropped);
|
|
74738
74740
|
});
|
|
74739
|
-
const outgoingRequestTokens =
|
|
74741
|
+
const outgoingRequestTokens = estimateSystemPromptTokens(this.systemPrompt) + estimateTokensForTools(tools) + estimateTokensForMessages(outgoingMessages);
|
|
74740
74742
|
const reportedContextTokens = this.reportedContextTokens?.() ?? 0;
|
|
74741
74743
|
const usedContextTokens = Math.max(outgoingRequestTokens, reportedContextTokens);
|
|
74742
74744
|
completionBudget = applyCompletionBudgetWithDetails({
|
|
@@ -75454,7 +75456,7 @@ var init_full = __esmMin((() => {
|
|
|
75454
75456
|
return this.agent.context.tokenCountWithPending;
|
|
75455
75457
|
}
|
|
75456
75458
|
estimateRequestTokens(messages, systemPrompt = this.agent.effectiveSystemPrompt, tools = this.agent.tools.loopTools) {
|
|
75457
|
-
return
|
|
75459
|
+
return estimateSystemPromptTokens(systemPrompt) + estimateTokensForTools(tools) + estimateTokensForMessages(messages);
|
|
75458
75460
|
}
|
|
75459
75461
|
resetForTurn() {
|
|
75460
75462
|
this.compactionCountInTurn = 0;
|
|
@@ -265521,7 +265523,7 @@ var init_agent = __esmMin((() => {
|
|
|
265521
265523
|
projectedTokenCount: this.fullCompaction.estimateCurrentRequestTokens(),
|
|
265522
265524
|
effectiveMaxContextTokens: this.fullCompaction.getEffectiveMaxContextTokens(),
|
|
265523
265525
|
compactionBudgetTokens: this.fullCompaction.getCompactionBudgetTokens(),
|
|
265524
|
-
systemPromptTokens:
|
|
265526
|
+
systemPromptTokens: estimateSystemPromptTokens(systemPrompt),
|
|
265525
265527
|
personalityPromptTokens,
|
|
265526
265528
|
socialPromptTokens,
|
|
265527
265529
|
skillPromptTokens: includedTokens(skillPrompt),
|