@juspay/neurolink 10.10.2 → 10.10.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/CHANGELOG.md +12 -0
- package/dist/browser/neurolink.min.js +355 -355
- package/dist/context/budgetChecker.d.ts +16 -0
- package/dist/context/budgetChecker.js +32 -0
- package/dist/context/stepBudgetGuard.js +32 -3
- package/dist/context/summarizationEngine.js +7 -5
- package/dist/lib/context/budgetChecker.d.ts +16 -0
- package/dist/lib/context/budgetChecker.js +32 -0
- package/dist/lib/context/stepBudgetGuard.js +32 -3
- package/dist/lib/context/summarizationEngine.js +7 -5
- package/dist/lib/neurolink.js +28 -6
- package/dist/lib/utils/tokenEstimation.d.ts +12 -0
- package/dist/lib/utils/tokenEstimation.js +46 -2
- package/dist/neurolink.js +28 -6
- package/dist/utils/tokenEstimation.d.ts +12 -0
- package/dist/utils/tokenEstimation.js +46 -2
- package/package.json +3 -1
|
@@ -33,6 +33,12 @@ export const TOKENS_PER_MESSAGE = 4;
|
|
|
33
33
|
export const TOKENS_PER_CONVERSATION = 24;
|
|
34
34
|
/** Image token estimate (flat) */
|
|
35
35
|
export const IMAGE_TOKEN_ESTIMATE = 1_024;
|
|
36
|
+
/**
|
|
37
|
+
* Chars charged for a value that cannot be serialized for estimation (V8 max
|
|
38
|
+
* string length). Deliberately large: such a value is enormous by definition,
|
|
39
|
+
* and under-charging it would defeat the budget check it feeds.
|
|
40
|
+
*/
|
|
41
|
+
const OVERSIZED_VALUE_FALLBACK_CHARS = 200_000;
|
|
36
42
|
/**
|
|
37
43
|
* Per-provider token multipliers.
|
|
38
44
|
* Applied on top of the base GPT-style character estimate.
|
|
@@ -80,9 +86,39 @@ export function estimateTokens(text, provider, isCode) {
|
|
|
80
86
|
const safetyBuffer = baseTokens * TOKEN_SAFETY_MARGIN_ADDITIVE;
|
|
81
87
|
return Math.ceil(providerAdjusted + safetyBuffer);
|
|
82
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Serialize an arbitrary value for estimation. Never throws: `JSON.stringify`
|
|
91
|
+
* raises RangeError once a value exceeds V8's max string length, and a tool
|
|
92
|
+
* argument blob is exactly the shape that gets there. A payload that large is
|
|
93
|
+
* charged at the fallback size rather than aborting the estimate (and with it
|
|
94
|
+
* the whole turn).
|
|
95
|
+
*/
|
|
96
|
+
function serializeForEstimate(value) {
|
|
97
|
+
if (typeof value === "string") {
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
return JSON.stringify(value) ?? "";
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return "x".repeat(OVERSIZED_VALUE_FALLBACK_CHARS);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
83
107
|
/**
|
|
84
108
|
* Estimate token count for a single ChatMessage.
|
|
85
109
|
* Includes message framing overhead.
|
|
110
|
+
*
|
|
111
|
+
* Counts `content` AND `args`. A `tool_call` is persisted with an EMPTY
|
|
112
|
+
* `content` and its entire payload in `args` (see flushPendingToolData),
|
|
113
|
+
* so a content-only estimate scored a 39 KB Write call at ~28 tokens against a
|
|
114
|
+
* real cost near 9,750 — the budget checker, the compaction trigger and the
|
|
115
|
+
* summarization threshold were all blind to the single largest source of
|
|
116
|
+
* context growth in an agentic session.
|
|
117
|
+
*
|
|
118
|
+
* `result` is deliberately NOT counted: `result.result` is re-hydrated FROM
|
|
119
|
+
* `content` at read time (redisConversationMemoryManager), so counting both
|
|
120
|
+
* double-bills the same bytes. Only `result.error`, which has no counterpart in
|
|
121
|
+
* `content`, is included.
|
|
86
122
|
*/
|
|
87
123
|
export function estimateMessageTokens(message, provider) {
|
|
88
124
|
let contentStr = "";
|
|
@@ -100,8 +136,16 @@ export function estimateMessageTokens(message, provider) {
|
|
|
100
136
|
}
|
|
101
137
|
}
|
|
102
138
|
}
|
|
103
|
-
|
|
104
|
-
|
|
139
|
+
let total = estimateTokens(contentStr, provider) + TOKENS_PER_MESSAGE;
|
|
140
|
+
const args = message.args;
|
|
141
|
+
if (args) {
|
|
142
|
+
total += estimateTokens(serializeForEstimate(args), provider);
|
|
143
|
+
}
|
|
144
|
+
const resultError = message.result?.error;
|
|
145
|
+
if (resultError) {
|
|
146
|
+
total += estimateTokens(serializeForEstimate(resultError), provider);
|
|
147
|
+
}
|
|
148
|
+
return total;
|
|
105
149
|
}
|
|
106
150
|
/**
|
|
107
151
|
* Estimate total token count for an array of messages.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juspay/neurolink",
|
|
3
|
-
"version": "10.10.
|
|
3
|
+
"version": "10.10.4",
|
|
4
4
|
"packageManager": "pnpm@10.15.1",
|
|
5
5
|
"description": "TypeScript AI SDK with 24+ LLM providers behind one consistent API. MCP-native (connect any MCP server), voice TTS/STT/realtime, RAG, agents, memory, context compaction. OpenAI · Anthropic · Gemini · Bedrock · Azure · Ollama · DeepSeek · NVIDIA NIM and more.",
|
|
6
6
|
"author": {
|
|
@@ -83,6 +83,8 @@
|
|
|
83
83
|
"test:litellm-parity": "npx tsx test/continuous-test-suite-litellm-parity.ts",
|
|
84
84
|
"test:memory": "npx tsx test/continuous-test-suite-memory.ts",
|
|
85
85
|
"test:tool-pairing": "npx tsx test/continuous-test-suite-tool-pairing.ts",
|
|
86
|
+
"test:token-accounting": "npx tsx test/continuous-test-suite-token-accounting.ts",
|
|
87
|
+
"test:step-guard": "npx tsx test/continuous-test-suite-step-guard.ts",
|
|
86
88
|
"test:middleware": "npx tsx test/continuous-test-suite-middleware.ts",
|
|
87
89
|
"test:observability": "npx tsx test/continuous-test-suite-observability.ts",
|
|
88
90
|
"test:ppt": "npx tsx test/continuous-test-suite-ppt.ts",
|