illuma-agents 1.0.16 → 1.0.17
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/cjs/common/enum.cjs +18 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +63 -25
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +1 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +13 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +16 -8
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +8 -2
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +17 -10
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/stream.cjs +1 -0
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +73 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +1 -0
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/utils/contextAnalytics.cjs +64 -0
- package/dist/cjs/utils/contextAnalytics.cjs.map +1 -0
- package/dist/cjs/utils/toonFormat.cjs +358 -0
- package/dist/cjs/utils/toonFormat.cjs.map +1 -0
- package/dist/esm/common/enum.mjs +19 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +65 -27
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +1 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +3 -1
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +18 -10
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +9 -3
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +19 -12
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/stream.mjs +1 -0
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +73 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +1 -0
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/utils/contextAnalytics.mjs +62 -0
- package/dist/esm/utils/contextAnalytics.mjs.map +1 -0
- package/dist/esm/utils/toonFormat.mjs +351 -0
- package/dist/esm/utils/toonFormat.mjs.map +1 -0
- package/dist/types/common/enum.d.ts +17 -0
- package/dist/types/graphs/Graph.d.ts +8 -0
- package/dist/types/utils/contextAnalytics.d.ts +37 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/toonFormat.d.ts +111 -0
- package/package.json +2 -1
- package/src/common/enum.ts +18 -0
- package/src/graphs/Graph.ts +84 -33
- package/src/messages/core.ts +27 -19
- package/src/messages/format.ts +10 -3
- package/src/messages/tools.ts +20 -13
- package/src/tools/ToolNode.ts +78 -5
- package/src/utils/contextAnalytics.ts +95 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/toonFormat.ts +437 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TOON Format Utility
|
|
5
|
+
*
|
|
6
|
+
* Provides JSON to TOON conversion for token-efficient encoding.
|
|
7
|
+
* TOON format can reduce token count by 30-60% for JSON data.
|
|
8
|
+
*
|
|
9
|
+
* Uses the official @toon-format/toon library when available.
|
|
10
|
+
*/
|
|
11
|
+
// Dynamic import holder for the TOON library (ESM)
|
|
12
|
+
let toonEncode = null;
|
|
13
|
+
let toonLoadAttempted = false;
|
|
14
|
+
let toonLoadPromise = null;
|
|
15
|
+
/**
|
|
16
|
+
* Lazily loads the TOON library
|
|
17
|
+
* Tries both ESM dynamic import and CommonJS require for compatibility
|
|
18
|
+
*/
|
|
19
|
+
async function loadToonLibrary() {
|
|
20
|
+
if (toonLoadAttempted)
|
|
21
|
+
return toonEncode !== null;
|
|
22
|
+
toonLoadAttempted = true;
|
|
23
|
+
// Try ESM dynamic import first
|
|
24
|
+
try {
|
|
25
|
+
const mod = await import('@toon-format/toon');
|
|
26
|
+
toonEncode = mod.encode;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// ESM import failed, try CommonJS require
|
|
31
|
+
try {
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
33
|
+
const mod = require('@toon-format/toon');
|
|
34
|
+
toonEncode = mod.encode;
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Library not available - that's OK, we'll return original content
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Ensures the TOON library is loaded. Call this before using jsonToToon in tests.
|
|
45
|
+
*/
|
|
46
|
+
async function ensureToonLoaded() {
|
|
47
|
+
if (!toonLoadPromise) {
|
|
48
|
+
toonLoadPromise = loadToonLibrary();
|
|
49
|
+
}
|
|
50
|
+
return toonLoadPromise;
|
|
51
|
+
}
|
|
52
|
+
// Start loading immediately
|
|
53
|
+
ensureToonLoaded();
|
|
54
|
+
/**
|
|
55
|
+
* Check if a string appears to be in TOON format.
|
|
56
|
+
* TOON format characteristics:
|
|
57
|
+
* - Uses key: value syntax (like YAML)
|
|
58
|
+
* - Uses array notation like [3] or {fields}
|
|
59
|
+
* - Does NOT start with { or [
|
|
60
|
+
* - Often has patterns like "name[N]:" or "name{fields}:"
|
|
61
|
+
*
|
|
62
|
+
* @param str - String to check
|
|
63
|
+
* @returns true if string appears to be TOON format
|
|
64
|
+
*/
|
|
65
|
+
function isToonFormat(str) {
|
|
66
|
+
if (typeof str !== 'string' || str.length === 0)
|
|
67
|
+
return false;
|
|
68
|
+
const trimmed = str.trim();
|
|
69
|
+
// TOON doesn't start with JSON brackets
|
|
70
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('['))
|
|
71
|
+
return false;
|
|
72
|
+
// Check for TOON-specific patterns:
|
|
73
|
+
// 1. Key-value with colon (but not URL-like patterns)
|
|
74
|
+
// 2. Array notation like "items[3]:" or "data[10]:"
|
|
75
|
+
// 3. Object schema notation like "items{id,name}:"
|
|
76
|
+
const toonPatterns = [
|
|
77
|
+
/^\w+:$/m, // Simple key: at start of line
|
|
78
|
+
/^\w+\[\d+\]:/m, // Array notation: items[3]:
|
|
79
|
+
/^\w+\{[^}]+\}:/m, // Schema notation: items{id,name}:
|
|
80
|
+
/^\w+\[\d+\]\{[^}]+\}:/m, // Combined: items[3]{id,name}:
|
|
81
|
+
];
|
|
82
|
+
// Must match at least one TOON pattern
|
|
83
|
+
const hasToonPattern = toonPatterns.some(pattern => pattern.test(trimmed));
|
|
84
|
+
// Additional check: TOON typically has multiple lines with consistent indentation
|
|
85
|
+
const lines = trimmed.split('\n').filter(l => l.trim());
|
|
86
|
+
const hasMultipleKeyValueLines = lines.filter(l => /^\s*\w+.*:/.test(l)).length >= 2;
|
|
87
|
+
return hasToonPattern || hasMultipleKeyValueLines;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Extract the first valid JSON object or array from a string.
|
|
91
|
+
* Handles pure JSON or JSON embedded in text (e.g., tool responses).
|
|
92
|
+
*
|
|
93
|
+
* @param str - The string to extract JSON from
|
|
94
|
+
* @returns Object with found status, parsed JSON, and indices
|
|
95
|
+
*/
|
|
96
|
+
function extractFirstJson(str) {
|
|
97
|
+
if (typeof str !== 'string' || str.length === 0) {
|
|
98
|
+
return { found: false, parsed: null, startIndex: -1, endIndex: -1 };
|
|
99
|
+
}
|
|
100
|
+
// Find the first { or [ character
|
|
101
|
+
const objStart = str.indexOf('{');
|
|
102
|
+
const arrStart = str.indexOf('[');
|
|
103
|
+
if (objStart === -1 && arrStart === -1) {
|
|
104
|
+
return { found: false, parsed: null, startIndex: -1, endIndex: -1 };
|
|
105
|
+
}
|
|
106
|
+
let startIndex;
|
|
107
|
+
if (objStart === -1) {
|
|
108
|
+
startIndex = arrStart;
|
|
109
|
+
}
|
|
110
|
+
else if (arrStart === -1) {
|
|
111
|
+
startIndex = objStart;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
startIndex = Math.min(objStart, arrStart);
|
|
115
|
+
}
|
|
116
|
+
// Find the matching closing bracket using a stack-based approach
|
|
117
|
+
// Properly handle strings to avoid counting brackets inside strings
|
|
118
|
+
let depth = 0;
|
|
119
|
+
let inString = false;
|
|
120
|
+
let escapeNext = false;
|
|
121
|
+
for (let i = startIndex; i < str.length; i++) {
|
|
122
|
+
const char = str[i];
|
|
123
|
+
if (escapeNext) {
|
|
124
|
+
escapeNext = false;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (char === '\\' && inString) {
|
|
128
|
+
escapeNext = true;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (char === '"') {
|
|
132
|
+
inString = !inString;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (inString)
|
|
136
|
+
continue;
|
|
137
|
+
if (char === '{' || char === '[')
|
|
138
|
+
depth++;
|
|
139
|
+
if (char === '}' || char === ']')
|
|
140
|
+
depth--;
|
|
141
|
+
if (depth === 0) {
|
|
142
|
+
// Found matching bracket, try to parse
|
|
143
|
+
const jsonStr = str.substring(startIndex, i + 1);
|
|
144
|
+
try {
|
|
145
|
+
const parsed = JSON.parse(jsonStr);
|
|
146
|
+
return { found: true, parsed, startIndex, endIndex: i };
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// Not valid JSON at this bracket level, continue searching
|
|
150
|
+
// Reset and look for the next JSON start after this position
|
|
151
|
+
const nextResult = extractFirstJson(str.substring(i + 1));
|
|
152
|
+
if (nextResult.found) {
|
|
153
|
+
return {
|
|
154
|
+
found: true,
|
|
155
|
+
parsed: nextResult.parsed,
|
|
156
|
+
startIndex: i + 1 + nextResult.startIndex,
|
|
157
|
+
endIndex: i + 1 + nextResult.endIndex,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return { found: false, parsed: null, startIndex: -1, endIndex: -1 };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return { found: false, parsed: null, startIndex: -1, endIndex: -1 };
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Convert JSON content to TOON format for token efficiency.
|
|
168
|
+
* Extracts JSON from string if embedded, converts to TOON.
|
|
169
|
+
* Returns original string if:
|
|
170
|
+
* - Already in TOON format
|
|
171
|
+
* - Not JSON
|
|
172
|
+
* - TOON conversion fails or is larger
|
|
173
|
+
*
|
|
174
|
+
* @param str - The string containing JSON to convert
|
|
175
|
+
* @returns Object with conversion status, result, and reduction percentage
|
|
176
|
+
*/
|
|
177
|
+
function jsonToToon(str) {
|
|
178
|
+
if (!toonEncode || typeof str !== 'string') {
|
|
179
|
+
return { converted: false, result: str, reduction: 0, error: !toonEncode ? 'TOON library not loaded' : undefined };
|
|
180
|
+
}
|
|
181
|
+
// Check if already in TOON format - skip conversion
|
|
182
|
+
if (isToonFormat(str)) {
|
|
183
|
+
return { converted: false, result: str, reduction: 0, alreadyToon: true };
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const { found, parsed, startIndex, endIndex } = extractFirstJson(str);
|
|
187
|
+
if (!found || !parsed) {
|
|
188
|
+
return { converted: false, result: str, reduction: 0, error: 'No JSON found in content' };
|
|
189
|
+
}
|
|
190
|
+
// Check if this JSON structure would benefit from TOON
|
|
191
|
+
// Text-heavy content (emails, documents) won't compress well
|
|
192
|
+
if (!isToonBeneficial(parsed)) {
|
|
193
|
+
return { converted: false, result: str, reduction: 0, error: 'Content is text-heavy (>70% string values), TOON not beneficial' };
|
|
194
|
+
}
|
|
195
|
+
const toonResult = toonEncode(parsed);
|
|
196
|
+
// Preserve text before and after the JSON block
|
|
197
|
+
const textBefore = str.substring(0, startIndex);
|
|
198
|
+
const textAfter = str.substring(endIndex + 1);
|
|
199
|
+
// Build the full result: textBefore + TOON + textAfter
|
|
200
|
+
const fullResult = textBefore + toonResult + textAfter;
|
|
201
|
+
if (fullResult.length < str.length) {
|
|
202
|
+
const reduction = Math.round(((str.length - fullResult.length) / str.length) * 100);
|
|
203
|
+
return { converted: true, result: fullResult, reduction };
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
// TOON output was larger or same size - not beneficial
|
|
207
|
+
return { converted: false, result: str, reduction: 0, error: `TOON output not smaller (${fullResult.length} >= ${str.length})` };
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch (err) {
|
|
211
|
+
// TOON encoding or extraction failed - log error for debugging
|
|
212
|
+
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
213
|
+
return { converted: false, result: str, reduction: 0, error: `TOON conversion error: ${errorMsg}` };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Check if TOON library is loaded and available
|
|
218
|
+
*/
|
|
219
|
+
function isToonAvailable() {
|
|
220
|
+
return toonEncode !== null;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Analyze JSON structure to determine if TOON conversion would be beneficial.
|
|
224
|
+
* Text-heavy content (emails, documents) doesn't compress well with TOON.
|
|
225
|
+
* Structured data (API responses, metadata) compresses much better.
|
|
226
|
+
*
|
|
227
|
+
* @param parsed - Parsed JSON object
|
|
228
|
+
* @returns true if TOON conversion is likely beneficial
|
|
229
|
+
*/
|
|
230
|
+
function isToonBeneficial(parsed) {
|
|
231
|
+
if (!parsed || typeof parsed !== 'object')
|
|
232
|
+
return false;
|
|
233
|
+
const jsonStr = JSON.stringify(parsed);
|
|
234
|
+
const totalLength = jsonStr.length;
|
|
235
|
+
// Count characters in string values (text content)
|
|
236
|
+
let textContentLength = 0;
|
|
237
|
+
function countTextContent(obj) {
|
|
238
|
+
if (typeof obj === 'string') {
|
|
239
|
+
// Count string length (this is text content that TOON can't compress)
|
|
240
|
+
textContentLength += obj.length;
|
|
241
|
+
}
|
|
242
|
+
else if (Array.isArray(obj)) {
|
|
243
|
+
obj.forEach(countTextContent);
|
|
244
|
+
}
|
|
245
|
+
else if (obj && typeof obj === 'object') {
|
|
246
|
+
Object.values(obj).forEach(countTextContent);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
countTextContent(parsed);
|
|
250
|
+
// Calculate ratio of text content to total JSON
|
|
251
|
+
const textRatio = textContentLength / totalLength;
|
|
252
|
+
// If more than 70% is text content, TOON won't help much
|
|
253
|
+
// TOON compresses structure (field names, brackets), not text
|
|
254
|
+
return textRatio < 0.7;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Process tool output: apply TOON conversion if beneficial, then truncate if needed.
|
|
258
|
+
* This is the main entry point for processing any tool output (regular or MCP).
|
|
259
|
+
*
|
|
260
|
+
* Flow:
|
|
261
|
+
* 1. Check if already TOON format → skip conversion
|
|
262
|
+
* 2. Try TOON conversion if content is JSON and large enough
|
|
263
|
+
* 3. Truncate if still exceeds maxLength (with smart break points)
|
|
264
|
+
*
|
|
265
|
+
* @param content - The tool output content
|
|
266
|
+
* @param options - Processing options
|
|
267
|
+
* @returns Processed content and metadata
|
|
268
|
+
*/
|
|
269
|
+
function processToolOutput(content, options = {}) {
|
|
270
|
+
const { maxLength = 100000, enableToon = true, minSizeForToon = 1000, minReductionPercent = 10, // Increased from 5% - only apply TOON when clearly beneficial
|
|
271
|
+
} = options;
|
|
272
|
+
const originalLength = content.length;
|
|
273
|
+
const originalTokens = Math.ceil(originalLength / 4);
|
|
274
|
+
let result = content;
|
|
275
|
+
let toonConverted = false;
|
|
276
|
+
let alreadyToon = false;
|
|
277
|
+
let reduction = 0;
|
|
278
|
+
let truncated = false;
|
|
279
|
+
let toonError;
|
|
280
|
+
// Step 1: Check if already TOON format
|
|
281
|
+
if (isToonFormat(content)) {
|
|
282
|
+
alreadyToon = true;
|
|
283
|
+
}
|
|
284
|
+
// Step 2: Apply TOON conversion if enabled and content is large enough
|
|
285
|
+
else if (enableToon && content.length > minSizeForToon) {
|
|
286
|
+
const toonResult = jsonToToon(content);
|
|
287
|
+
if (toonResult.alreadyToon) {
|
|
288
|
+
alreadyToon = true;
|
|
289
|
+
}
|
|
290
|
+
else if (toonResult.converted && toonResult.reduction >= minReductionPercent) {
|
|
291
|
+
result = toonResult.result;
|
|
292
|
+
toonConverted = true;
|
|
293
|
+
reduction = toonResult.reduction;
|
|
294
|
+
}
|
|
295
|
+
else if (toonResult.error) {
|
|
296
|
+
// Track error for debugging
|
|
297
|
+
toonError = toonResult.error;
|
|
298
|
+
}
|
|
299
|
+
else if (toonResult.converted && toonResult.reduction < minReductionPercent) {
|
|
300
|
+
// TOON converted but reduction was too small to be worth it
|
|
301
|
+
toonError = `TOON reduction ${toonResult.reduction}% below threshold ${minReductionPercent}%`;
|
|
302
|
+
}
|
|
303
|
+
else if (!toonResult.converted) {
|
|
304
|
+
// Conversion failed without explicit error - investigate
|
|
305
|
+
toonError = toonResult.error || 'TOON conversion returned false with no error';
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// Step 3: Truncate if still too long (with smart break points)
|
|
309
|
+
if (result.length > maxLength) {
|
|
310
|
+
let truncatedStr = result.substring(0, maxLength);
|
|
311
|
+
// Try to find a clean break point
|
|
312
|
+
if (toonConverted || alreadyToon) {
|
|
313
|
+
// For TOON format, break at newline for cleaner output
|
|
314
|
+
const lastNewline = truncatedStr.lastIndexOf('\n');
|
|
315
|
+
if (lastNewline > maxLength * 0.7) {
|
|
316
|
+
truncatedStr = truncatedStr.substring(0, lastNewline);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
// For JSON, try to find a clean JSON break point
|
|
321
|
+
const lastCompleteItem = truncatedStr.lastIndexOf('},');
|
|
322
|
+
const lastArrayItem = truncatedStr.lastIndexOf('],');
|
|
323
|
+
const breakPoint = Math.max(lastCompleteItem, lastArrayItem);
|
|
324
|
+
if (breakPoint > maxLength * 0.5) {
|
|
325
|
+
truncatedStr = truncatedStr.substring(0, breakPoint + 1);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
// Build truncation message
|
|
329
|
+
const truncationInfo = toonConverted
|
|
330
|
+
? `Original ${originalLength.toLocaleString()} chars → TOON ${result.length.toLocaleString()} chars (${reduction}% saved). ` +
|
|
331
|
+
`Still exceeds ${maxLength.toLocaleString()} char limit.`
|
|
332
|
+
: `Original output was ${originalLength.toLocaleString()} characters (~${originalTokens.toLocaleString()} tokens).`;
|
|
333
|
+
result = truncatedStr + `\n\n[OUTPUT_TRUNCATED: ${truncationInfo} Please use more specific queries or smaller date ranges.]`;
|
|
334
|
+
truncated = true;
|
|
335
|
+
}
|
|
336
|
+
const finalLength = result.length;
|
|
337
|
+
const finalTokens = Math.ceil(finalLength / 4);
|
|
338
|
+
return {
|
|
339
|
+
content: result,
|
|
340
|
+
toonConverted,
|
|
341
|
+
truncated,
|
|
342
|
+
reduction,
|
|
343
|
+
alreadyToon,
|
|
344
|
+
originalLength,
|
|
345
|
+
originalTokens,
|
|
346
|
+
finalLength,
|
|
347
|
+
finalTokens,
|
|
348
|
+
toonError,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
exports.ensureToonLoaded = ensureToonLoaded;
|
|
353
|
+
exports.extractFirstJson = extractFirstJson;
|
|
354
|
+
exports.isToonAvailable = isToonAvailable;
|
|
355
|
+
exports.isToonFormat = isToonFormat;
|
|
356
|
+
exports.jsonToToon = jsonToToon;
|
|
357
|
+
exports.processToolOutput = processToolOutput;
|
|
358
|
+
//# sourceMappingURL=toonFormat.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toonFormat.cjs","sources":["../../../src/utils/toonFormat.ts"],"sourcesContent":["/**\r\n * TOON Format Utility\r\n * \r\n * Provides JSON to TOON conversion for token-efficient encoding.\r\n * TOON format can reduce token count by 30-60% for JSON data.\r\n * \r\n * Uses the official @toon-format/toon library when available.\r\n */\r\n\r\n// Dynamic import holder for the TOON library (ESM)\r\nlet toonEncode: ((data: unknown) => string) | null = null;\r\nlet toonLoadAttempted = false;\r\nlet toonLoadPromise: Promise<boolean> | null = null;\r\n\r\n/**\r\n * Lazily loads the TOON library\r\n * Tries both ESM dynamic import and CommonJS require for compatibility\r\n */\r\nasync function loadToonLibrary(): Promise<boolean> {\r\n if (toonLoadAttempted) return toonEncode !== null;\r\n toonLoadAttempted = true;\r\n\r\n // Try ESM dynamic import first\r\n try {\r\n const mod = await import('@toon-format/toon');\r\n toonEncode = mod.encode;\r\n return true;\r\n } catch {\r\n // ESM import failed, try CommonJS require\r\n try {\r\n // eslint-disable-next-line @typescript-eslint/no-require-imports\r\n const mod = require('@toon-format/toon');\r\n toonEncode = mod.encode;\r\n return true;\r\n } catch {\r\n // Library not available - that's OK, we'll return original content\r\n return false;\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Ensures the TOON library is loaded. Call this before using jsonToToon in tests.\r\n */\r\nexport async function ensureToonLoaded(): Promise<boolean> {\r\n if (!toonLoadPromise) {\r\n toonLoadPromise = loadToonLibrary();\r\n }\r\n return toonLoadPromise;\r\n}\r\n\r\n// Start loading immediately\r\nensureToonLoaded();\r\n\r\n/**\r\n * Check if a string appears to be in TOON format.\r\n * TOON format characteristics:\r\n * - Uses key: value syntax (like YAML)\r\n * - Uses array notation like [3] or {fields}\r\n * - Does NOT start with { or [\r\n * - Often has patterns like \"name[N]:\" or \"name{fields}:\"\r\n * \r\n * @param str - String to check\r\n * @returns true if string appears to be TOON format\r\n */\r\nexport function isToonFormat(str: string): boolean {\r\n if (typeof str !== 'string' || str.length === 0) return false;\r\n \r\n const trimmed = str.trim();\r\n \r\n // TOON doesn't start with JSON brackets\r\n if (trimmed.startsWith('{') || trimmed.startsWith('[')) return false;\r\n \r\n // Check for TOON-specific patterns:\r\n // 1. Key-value with colon (but not URL-like patterns)\r\n // 2. Array notation like \"items[3]:\" or \"data[10]:\"\r\n // 3. Object schema notation like \"items{id,name}:\"\r\n const toonPatterns = [\r\n /^\\w+:$/m, // Simple key: at start of line\r\n /^\\w+\\[\\d+\\]:/m, // Array notation: items[3]:\r\n /^\\w+\\{[^}]+\\}:/m, // Schema notation: items{id,name}:\r\n /^\\w+\\[\\d+\\]\\{[^}]+\\}:/m, // Combined: items[3]{id,name}:\r\n ];\r\n \r\n // Must match at least one TOON pattern\r\n const hasToonPattern = toonPatterns.some(pattern => pattern.test(trimmed));\r\n \r\n // Additional check: TOON typically has multiple lines with consistent indentation\r\n const lines = trimmed.split('\\n').filter(l => l.trim());\r\n const hasMultipleKeyValueLines = lines.filter(l => /^\\s*\\w+.*:/.test(l)).length >= 2;\r\n \r\n return hasToonPattern || hasMultipleKeyValueLines;\r\n}\r\n\r\n/**\r\n * Extract the first valid JSON object or array from a string.\r\n * Handles pure JSON or JSON embedded in text (e.g., tool responses).\r\n * \r\n * @param str - The string to extract JSON from\r\n * @returns Object with found status, parsed JSON, and indices\r\n */\r\nexport function extractFirstJson(str: string): {\r\n found: boolean;\r\n parsed: unknown;\r\n startIndex: number;\r\n endIndex: number;\r\n} {\r\n if (typeof str !== 'string' || str.length === 0) {\r\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\r\n }\r\n\r\n // Find the first { or [ character\r\n const objStart = str.indexOf('{');\r\n const arrStart = str.indexOf('[');\r\n\r\n if (objStart === -1 && arrStart === -1) {\r\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\r\n }\r\n\r\n let startIndex: number;\r\n if (objStart === -1) {\r\n startIndex = arrStart;\r\n } else if (arrStart === -1) {\r\n startIndex = objStart;\r\n } else {\r\n startIndex = Math.min(objStart, arrStart);\r\n }\r\n\r\n // Find the matching closing bracket using a stack-based approach\r\n // Properly handle strings to avoid counting brackets inside strings\r\n let depth = 0;\r\n let inString = false;\r\n let escapeNext = false;\r\n\r\n for (let i = startIndex; i < str.length; i++) {\r\n const char = str[i];\r\n\r\n if (escapeNext) {\r\n escapeNext = false;\r\n continue;\r\n }\r\n\r\n if (char === '\\\\' && inString) {\r\n escapeNext = true;\r\n continue;\r\n }\r\n\r\n if (char === '\"') {\r\n inString = !inString;\r\n continue;\r\n }\r\n\r\n if (inString) continue;\r\n\r\n if (char === '{' || char === '[') depth++;\r\n if (char === '}' || char === ']') depth--;\r\n\r\n if (depth === 0) {\r\n // Found matching bracket, try to parse\r\n const jsonStr = str.substring(startIndex, i + 1);\r\n try {\r\n const parsed = JSON.parse(jsonStr);\r\n return { found: true, parsed, startIndex, endIndex: i };\r\n } catch {\r\n // Not valid JSON at this bracket level, continue searching\r\n // Reset and look for the next JSON start after this position\r\n const nextResult = extractFirstJson(str.substring(i + 1));\r\n if (nextResult.found) {\r\n return {\r\n found: true,\r\n parsed: nextResult.parsed,\r\n startIndex: i + 1 + nextResult.startIndex,\r\n endIndex: i + 1 + nextResult.endIndex,\r\n };\r\n }\r\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\r\n }\r\n }\r\n }\r\n\r\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\r\n}\r\n\r\n/**\r\n * Convert JSON content to TOON format for token efficiency.\r\n * Extracts JSON from string if embedded, converts to TOON.\r\n * Returns original string if:\r\n * - Already in TOON format\r\n * - Not JSON\r\n * - TOON conversion fails or is larger\r\n * \r\n * @param str - The string containing JSON to convert\r\n * @returns Object with conversion status, result, and reduction percentage\r\n */\r\nexport function jsonToToon(str: string): {\r\n converted: boolean;\r\n result: string;\r\n reduction: number;\r\n alreadyToon?: boolean;\r\n error?: string;\r\n} {\r\n if (!toonEncode || typeof str !== 'string') {\r\n return { converted: false, result: str, reduction: 0, error: !toonEncode ? 'TOON library not loaded' : undefined };\r\n }\r\n\r\n // Check if already in TOON format - skip conversion\r\n if (isToonFormat(str)) {\r\n return { converted: false, result: str, reduction: 0, alreadyToon: true };\r\n }\r\n\r\n try {\r\n const { found, parsed, startIndex, endIndex } = extractFirstJson(str);\r\n\r\n if (!found || !parsed) {\r\n return { converted: false, result: str, reduction: 0, error: 'No JSON found in content' };\r\n }\r\n\r\n // Check if this JSON structure would benefit from TOON\r\n // Text-heavy content (emails, documents) won't compress well\r\n if (!isToonBeneficial(parsed)) {\r\n return { converted: false, result: str, reduction: 0, error: 'Content is text-heavy (>70% string values), TOON not beneficial' };\r\n }\r\n\r\n const toonResult = toonEncode(parsed);\r\n \r\n // Preserve text before and after the JSON block\r\n const textBefore = str.substring(0, startIndex);\r\n const textAfter = str.substring(endIndex + 1);\r\n \r\n // Build the full result: textBefore + TOON + textAfter\r\n const fullResult = textBefore + toonResult + textAfter;\r\n\r\n if (fullResult.length < str.length) {\r\n const reduction = Math.round(((str.length - fullResult.length) / str.length) * 100);\r\n return { converted: true, result: fullResult, reduction };\r\n } else {\r\n // TOON output was larger or same size - not beneficial\r\n return { converted: false, result: str, reduction: 0, error: `TOON output not smaller (${fullResult.length} >= ${str.length})` };\r\n }\r\n } catch (err) {\r\n // TOON encoding or extraction failed - log error for debugging\r\n const errorMsg = err instanceof Error ? err.message : String(err);\r\n return { converted: false, result: str, reduction: 0, error: `TOON conversion error: ${errorMsg}` };\r\n }\r\n}\r\n\r\n/**\r\n * Check if TOON library is loaded and available\r\n */\r\nexport function isToonAvailable(): boolean {\r\n return toonEncode !== null;\r\n}\r\n\r\n/**\r\n * Result of processing tool output\r\n */\r\nexport interface ProcessToolOutputResult {\r\n /** The processed content string */\r\n content: string;\r\n /** Whether TOON conversion was applied */\r\n toonConverted: boolean;\r\n /** Whether content was truncated */\r\n truncated: boolean;\r\n /** Percentage reduction from TOON (0 if not converted) */\r\n reduction: number;\r\n /** Whether input was already in TOON format */\r\n alreadyToon: boolean;\r\n /** Original content length */\r\n originalLength: number;\r\n /** Estimated original tokens (~4 chars per token) */\r\n originalTokens: number;\r\n /** Final content length */\r\n finalLength: number;\r\n /** Estimated final tokens */\r\n finalTokens: number;\r\n /** Error message if TOON conversion failed (for debugging) */\r\n toonError?: string;\r\n}\r\n\r\n/**\r\n * Options for processing tool output\r\n */\r\nexport interface ProcessToolOutputOptions {\r\n /** Maximum output length in characters (default: 100000) */\r\n maxLength?: number;\r\n /** Whether to apply TOON conversion (default: true) */\r\n enableToon?: boolean;\r\n /** Minimum content size to attempt TOON conversion (default: 1000) */\r\n minSizeForToon?: number;\r\n /** Minimum reduction % to accept TOON result (default: 10) */\r\n minReductionPercent?: number;\r\n}\r\n\r\n/**\r\n * Analyze JSON structure to determine if TOON conversion would be beneficial.\r\n * Text-heavy content (emails, documents) doesn't compress well with TOON.\r\n * Structured data (API responses, metadata) compresses much better.\r\n * \r\n * @param parsed - Parsed JSON object\r\n * @returns true if TOON conversion is likely beneficial\r\n */\r\nfunction isToonBeneficial(parsed: unknown): boolean {\r\n if (!parsed || typeof parsed !== 'object') return false;\r\n \r\n const jsonStr = JSON.stringify(parsed);\r\n const totalLength = jsonStr.length;\r\n \r\n // Count characters in string values (text content)\r\n let textContentLength = 0;\r\n \r\n function countTextContent(obj: unknown): void {\r\n if (typeof obj === 'string') {\r\n // Count string length (this is text content that TOON can't compress)\r\n textContentLength += obj.length;\r\n } else if (Array.isArray(obj)) {\r\n obj.forEach(countTextContent);\r\n } else if (obj && typeof obj === 'object') {\r\n Object.values(obj).forEach(countTextContent);\r\n }\r\n }\r\n \r\n countTextContent(parsed);\r\n \r\n // Calculate ratio of text content to total JSON\r\n const textRatio = textContentLength / totalLength;\r\n \r\n // If more than 70% is text content, TOON won't help much\r\n // TOON compresses structure (field names, brackets), not text\r\n return textRatio < 0.7;\r\n}\r\n\r\n/**\r\n * Process tool output: apply TOON conversion if beneficial, then truncate if needed.\r\n * This is the main entry point for processing any tool output (regular or MCP).\r\n * \r\n * Flow:\r\n * 1. Check if already TOON format → skip conversion\r\n * 2. Try TOON conversion if content is JSON and large enough\r\n * 3. Truncate if still exceeds maxLength (with smart break points)\r\n * \r\n * @param content - The tool output content\r\n * @param options - Processing options\r\n * @returns Processed content and metadata\r\n */\r\nexport function processToolOutput(\r\n content: string,\r\n options: ProcessToolOutputOptions = {}\r\n): ProcessToolOutputResult {\r\n const { \r\n maxLength = 100000, \r\n enableToon = true,\r\n minSizeForToon = 1000,\r\n minReductionPercent = 10, // Increased from 5% - only apply TOON when clearly beneficial\r\n } = options;\r\n\r\n const originalLength = content.length;\r\n const originalTokens = Math.ceil(originalLength / 4);\r\n \r\n let result = content;\r\n let toonConverted = false;\r\n let alreadyToon = false;\r\n let reduction = 0;\r\n let truncated = false;\r\n let toonError: string | undefined;\r\n\r\n // Step 1: Check if already TOON format\r\n if (isToonFormat(content)) {\r\n alreadyToon = true;\r\n }\r\n // Step 2: Apply TOON conversion if enabled and content is large enough\r\n else if (enableToon && content.length > minSizeForToon) {\r\n const toonResult = jsonToToon(content);\r\n if (toonResult.alreadyToon) {\r\n alreadyToon = true;\r\n } else if (toonResult.converted && toonResult.reduction >= minReductionPercent) {\r\n result = toonResult.result;\r\n toonConverted = true;\r\n reduction = toonResult.reduction;\r\n } else if (toonResult.error) {\r\n // Track error for debugging\r\n toonError = toonResult.error;\r\n } else if (toonResult.converted && toonResult.reduction < minReductionPercent) {\r\n // TOON converted but reduction was too small to be worth it\r\n toonError = `TOON reduction ${toonResult.reduction}% below threshold ${minReductionPercent}%`;\r\n } else if (!toonResult.converted) {\r\n // Conversion failed without explicit error - investigate\r\n toonError = toonResult.error || 'TOON conversion returned false with no error';\r\n }\r\n }\r\n\r\n // Step 3: Truncate if still too long (with smart break points)\r\n if (result.length > maxLength) {\r\n let truncatedStr = result.substring(0, maxLength);\r\n \r\n // Try to find a clean break point\r\n if (toonConverted || alreadyToon) {\r\n // For TOON format, break at newline for cleaner output\r\n const lastNewline = truncatedStr.lastIndexOf('\\n');\r\n if (lastNewline > maxLength * 0.7) {\r\n truncatedStr = truncatedStr.substring(0, lastNewline);\r\n }\r\n } else {\r\n // For JSON, try to find a clean JSON break point\r\n const lastCompleteItem = truncatedStr.lastIndexOf('},');\r\n const lastArrayItem = truncatedStr.lastIndexOf('],');\r\n const breakPoint = Math.max(lastCompleteItem, lastArrayItem);\r\n if (breakPoint > maxLength * 0.5) {\r\n truncatedStr = truncatedStr.substring(0, breakPoint + 1);\r\n }\r\n }\r\n \r\n // Build truncation message\r\n const truncationInfo = toonConverted\r\n ? `Original ${originalLength.toLocaleString()} chars → TOON ${result.length.toLocaleString()} chars (${reduction}% saved). ` +\r\n `Still exceeds ${maxLength.toLocaleString()} char limit.`\r\n : `Original output was ${originalLength.toLocaleString()} characters (~${originalTokens.toLocaleString()} tokens).`;\r\n \r\n result = truncatedStr + `\\n\\n[OUTPUT_TRUNCATED: ${truncationInfo} Please use more specific queries or smaller date ranges.]`;\r\n truncated = true;\r\n }\r\n\r\n const finalLength = result.length;\r\n const finalTokens = Math.ceil(finalLength / 4);\r\n\r\n return { \r\n content: result, \r\n toonConverted, \r\n truncated, \r\n reduction, \r\n alreadyToon,\r\n originalLength,\r\n originalTokens,\r\n finalLength,\r\n finalTokens,\r\n toonError,\r\n };\r\n}\r\n"],"names":[],"mappings":";;AAAA;;;;;;;AAOG;AAEH;AACA,IAAI,UAAU,GAAuC,IAAI;AACzD,IAAI,iBAAiB,GAAG,KAAK;AAC7B,IAAI,eAAe,GAA4B,IAAI;AAEnD;;;AAGG;AACH,eAAe,eAAe,GAAA;AAC5B,IAAA,IAAI,iBAAiB;QAAE,OAAO,UAAU,KAAK,IAAI;IACjD,iBAAiB,GAAG,IAAI;;AAGxB,IAAA,IAAI;AACF,QAAA,MAAM,GAAG,GAAG,MAAM,OAAO,mBAAmB,CAAC;AAC7C,QAAA,UAAU,GAAG,GAAG,CAAC,MAAM;AACvB,QAAA,OAAO,IAAI;;AACX,IAAA,MAAM;;AAEN,QAAA,IAAI;;AAEF,YAAA,MAAM,GAAG,GAAG,OAAO,CAAC,mBAAmB,CAAC;AACxC,YAAA,UAAU,GAAG,GAAG,CAAC,MAAM;AACvB,YAAA,OAAO,IAAI;;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AAE,CAAA,oBAAA,EAAuB,cAAc,CAAC,cAAc,EAAE,CAAiB,cAAA,EAAA,cAAc,CAAC,cAAc,EAAE,CAAA,SAAA,CAAW;AAErH,QAAA,MAAM,GAAG,YAAY,GAAG,CAA0B,uBAAA,EAAA,cAAc,4DAA4D;QAC5H,SAAS,GAAG,IAAI;;AAGlB,IAAA,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM;IACjC,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;IAE9C,OAAO;AACL,QAAA,OAAO,EAAE,MAAM;QACf,aAAa;QACb,SAAS;QACT,SAAS;QACT,WAAW;QACX,cAAc;QACd,cAAc;QACd,WAAW;QACX,WAAW;QACX,SAAS;KACV;AACH;;;;;;;;;"}
|
package/dist/esm/common/enum.mjs
CHANGED
|
@@ -19,6 +19,8 @@ var GraphEvents;
|
|
|
19
19
|
GraphEvents["ON_MESSAGE_DELTA"] = "on_message_delta";
|
|
20
20
|
/** [Custom] Reasoning Delta events for messages */
|
|
21
21
|
GraphEvents["ON_REASONING_DELTA"] = "on_reasoning_delta";
|
|
22
|
+
/** [Custom] Context analytics event for traces */
|
|
23
|
+
GraphEvents["ON_CONTEXT_ANALYTICS"] = "on_context_analytics";
|
|
22
24
|
/* Official Events */
|
|
23
25
|
/** Custom event, emitted by system */
|
|
24
26
|
GraphEvents["ON_CUSTOM_EVENT"] = "on_custom_event";
|
|
@@ -160,6 +162,22 @@ var EnvVar;
|
|
|
160
162
|
EnvVar["CODE_API_KEY"] = "CODE_EXECUTOR_API_KEY";
|
|
161
163
|
EnvVar["CODE_BASEURL"] = "CODE_EXECUTOR_BASEURL";
|
|
162
164
|
})(EnvVar || (EnvVar = {}));
|
|
165
|
+
/**
|
|
166
|
+
* Message type identifiers used by LangChain's BaseMessage.getType().
|
|
167
|
+
* Use these constants instead of instanceof checks to avoid module mismatch issues
|
|
168
|
+
* when different copies of @langchain/core/messages are loaded.
|
|
169
|
+
*/
|
|
170
|
+
var MessageTypes;
|
|
171
|
+
(function (MessageTypes) {
|
|
172
|
+
MessageTypes["HUMAN"] = "human";
|
|
173
|
+
MessageTypes["AI"] = "ai";
|
|
174
|
+
MessageTypes["SYSTEM"] = "system";
|
|
175
|
+
MessageTypes["TOOL"] = "tool";
|
|
176
|
+
MessageTypes["FUNCTION"] = "function";
|
|
177
|
+
MessageTypes["GENERIC"] = "generic";
|
|
178
|
+
MessageTypes["DEVELOPER"] = "developer";
|
|
179
|
+
MessageTypes["REMOVE"] = "remove";
|
|
180
|
+
})(MessageTypes || (MessageTypes = {}));
|
|
163
181
|
|
|
164
|
-
export { Callback, CommonEvents, Constants, ContentTypes, EnvVar, GraphEvents, GraphNodeActions, GraphNodeKeys, Providers, StepTypes, TitleMethod, ToolCallTypes };
|
|
182
|
+
export { Callback, CommonEvents, Constants, ContentTypes, EnvVar, GraphEvents, GraphNodeActions, GraphNodeKeys, MessageTypes, Providers, StepTypes, TitleMethod, ToolCallTypes };
|
|
165
183
|
//# sourceMappingURL=enum.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.mjs","sources":["../../../src/common/enum.ts"],"sourcesContent":["/**\n * Enum representing the various event types emitted during the execution of runnables.\n * These events provide real-time information about the progress and state of different components.\n *\n * @enum {string}\n */\nexport enum GraphEvents {\n /* Custom Events */\n\n /** [Custom] Agent update event in multi-agent graph/workflow */\n ON_AGENT_UPDATE = 'on_agent_update',\n /** [Custom] Delta event for run steps (message creation and tool calls) */\n ON_RUN_STEP = 'on_run_step',\n /** [Custom] Delta event for run steps (tool calls) */\n ON_RUN_STEP_DELTA = 'on_run_step_delta',\n /** [Custom] Completed event for run steps (tool calls) */\n ON_RUN_STEP_COMPLETED = 'on_run_step_completed',\n /** [Custom] Delta events for messages */\n ON_MESSAGE_DELTA = 'on_message_delta',\n /** [Custom] Reasoning Delta events for messages */\n ON_REASONING_DELTA = 'on_reasoning_delta',\n\n /* Official Events */\n\n /** Custom event, emitted by system */\n ON_CUSTOM_EVENT = 'on_custom_event',\n /** Emitted when a chat model starts processing. */\n CHAT_MODEL_START = 'on_chat_model_start',\n\n /** Emitted when a chat model streams a chunk of its response. */\n CHAT_MODEL_STREAM = 'on_chat_model_stream',\n\n /** Emitted when a chat model completes its processing. */\n CHAT_MODEL_END = 'on_chat_model_end',\n\n /** Emitted when a language model starts processing. */\n LLM_START = 'on_llm_start',\n\n /** Emitted when a language model streams a chunk of its response. */\n LLM_STREAM = 'on_llm_stream',\n\n /** Emitted when a language model completes its processing. */\n LLM_END = 'on_llm_end',\n\n /** Emitted when a chain starts processing. */\n CHAIN_START = 'on_chain_start',\n\n /** Emitted when a chain streams a chunk of its output. */\n CHAIN_STREAM = 'on_chain_stream',\n\n /** Emitted when a chain completes its processing. */\n CHAIN_END = 'on_chain_end',\n\n /** Emitted when a tool starts its operation. */\n TOOL_START = 'on_tool_start',\n\n /** Emitted when a tool completes its operation. */\n TOOL_END = 'on_tool_end',\n\n /** Emitted when a retriever starts its operation. */\n RETRIEVER_START = 'on_retriever_start',\n\n /** Emitted when a retriever completes its operation. */\n RETRIEVER_END = 'on_retriever_end',\n\n /** Emitted when a prompt starts processing. */\n PROMPT_START = 'on_prompt_start',\n\n /** Emitted when a prompt completes its processing. */\n PROMPT_END = 'on_prompt_end',\n}\n\nexport enum Providers {\n OPENAI = 'openAI',\n VERTEXAI = 'vertexai',\n BEDROCK = 'bedrock',\n ANTHROPIC = 'anthropic',\n MISTRALAI = 'mistralai',\n MISTRAL = 'mistral',\n GOOGLE = 'google',\n AZURE = 'azureOpenAI',\n DEEPSEEK = 'deepseek',\n OPENROUTER = 'openrouter',\n XAI = 'xai',\n}\n\nexport enum GraphNodeKeys {\n TOOLS = 'tools=',\n AGENT = 'agent=',\n ROUTER = 'router',\n PRE_TOOLS = 'pre_tools',\n POST_TOOLS = 'post_tools',\n}\n\nexport enum GraphNodeActions {\n TOOL_NODE = 'tool_node',\n CALL_MODEL = 'call_model',\n ROUTE_MESSAGE = 'route_message',\n}\n\nexport enum CommonEvents {\n LANGGRAPH = 'LangGraph',\n}\n\nexport enum StepTypes {\n TOOL_CALLS = 'tool_calls',\n MESSAGE_CREATION = 'message_creation',\n}\n\nexport enum ContentTypes {\n TEXT = 'text',\n ERROR = 'error',\n THINK = 'think',\n TOOL_CALL = 'tool_call',\n IMAGE_URL = 'image_url',\n IMAGE_FILE = 'image_file',\n /** Anthropic */\n THINKING = 'thinking',\n /** Vertex AI / Google Common */\n REASONING = 'reasoning',\n /** Multi-Agent Switch */\n AGENT_UPDATE = 'agent_update',\n /** Bedrock */\n REASONING_CONTENT = 'reasoning_content',\n}\n\nexport enum ToolCallTypes {\n FUNCTION = 'function',\n RETRIEVAL = 'retrieval',\n FILE_SEARCH = 'file_search',\n CODE_INTERPRETER = 'code_interpreter',\n /* Agents Tool Call */\n TOOL_CALL = 'tool_call',\n}\n\nexport enum Callback {\n TOOL_ERROR = 'handleToolError',\n TOOL_START = 'handleToolStart',\n TOOL_END = 'handleToolEnd',\n CUSTOM_EVENT = 'handleCustomEvent',\n /*\n LLM_START = 'handleLLMStart',\n LLM_NEW_TOKEN = 'handleLLMNewToken',\n LLM_ERROR = 'handleLLMError',\n LLM_END = 'handleLLMEnd',\n CHAT_MODEL_START = 'handleChatModelStart',\n CHAIN_START = 'handleChainStart',\n CHAIN_ERROR = 'handleChainError',\n CHAIN_END = 'handleChainEnd',\n TEXT = 'handleText',\n AGENT_ACTION = 'handleAgentAction',\n AGENT_END = 'handleAgentEnd',\n RETRIEVER_START = 'handleRetrieverStart',\n RETRIEVER_END = 'handleRetrieverEnd',\n RETRIEVER_ERROR = 'handleRetrieverError',\n */\n}\n\nexport enum Constants {\n OFFICIAL_CODE_BASEURL = 'https://api.illuma.ai/v1',\n EXECUTE_CODE = 'execute_code',\n TOOL_SEARCH_REGEX = 'tool_search_regex',\n PROGRAMMATIC_TOOL_CALLING = 'run_tools_with_code',\n WEB_SEARCH = 'web_search',\n CONTENT_AND_ARTIFACT = 'content_and_artifact',\n LC_TRANSFER_TO_ = 'lc_transfer_to_',\n}\n\nexport enum TitleMethod {\n STRUCTURED = 'structured',\n FUNCTIONS = 'functions',\n COMPLETION = 'completion',\n}\n\nexport enum EnvVar {\n CODE_API_KEY = 'CODE_EXECUTOR_API_KEY',\n CODE_BASEURL = 'CODE_EXECUTOR_BASEURL',\n}\n"],"names":[],"mappings":"AAAA;;;;;AAKG;IACS;AAAZ,CAAA,UAAY,WAAW,EAAA;;;AAIrB,IAAA,WAAA,CAAA,iBAAA,CAAA,GAAA,iBAAmC;;AAEnC,IAAA,WAAA,CAAA,aAAA,CAAA,GAAA,aAA2B;;AAE3B,IAAA,WAAA,CAAA,mBAAA,CAAA,GAAA,mBAAuC;;AAEvC,IAAA,WAAA,CAAA,uBAAA,CAAA,GAAA,uBAA+C;;AAE/C,IAAA,WAAA,CAAA,kBAAA,CAAA,GAAA,kBAAqC;;AAErC,IAAA,WAAA,CAAA,oBAAA,CAAA,GAAA,oBAAyC;;;AAKzC,IAAA,WAAA,CAAA,iBAAA,CAAA,GAAA,iBAAmC;;AAEnC,IAAA,WAAA,CAAA,kBAAA,CAAA,GAAA,qBAAwC;;AAGxC,IAAA,WAAA,CAAA,mBAAA,CAAA,GAAA,sBAA0C;;AAG1C,IAAA,WAAA,CAAA,gBAAA,CAAA,GAAA,mBAAoC;;AAGpC,IAAA,WAAA,CAAA,WAAA,CAAA,GAAA,cAA0B;;AAG1B,IAAA,WAAA,CAAA,YAAA,CAAA,GAAA,eAA4B;;AAG5B,IAAA,WAAA,CAAA,SAAA,CAAA,GAAA,YAAsB;;AAGtB,IAAA,WAAA,CAAA,aAAA,CAAA,GAAA,gBAA8B;;AAG9B,IAAA,WAAA,CAAA,cAAA,CAAA,GAAA,iBAAgC;;AAGhC,IAAA,WAAA,CAAA,WAAA,CAAA,GAAA,cAA0B;;AAG1B,IAAA,WAAA,CAAA,YAAA,CAAA,GAAA,eAA4B;;AAG5B,IAAA,WAAA,CAAA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,YAAA,CAAA,GAAA,YAAyB;AACzB,IAAA,WAAA,CAAA,WAAA,CAAA,GAAA,WAAuB;AACvB,IAAA,WAAA,CAAA,YAAA,CAAA,GAAA,YAAyB;AAC3B,CAAC,EAJW,WAAW,KAAX,WAAW,GAItB,EAAA,CAAA,CAAA;IAEW;AAAZ,CAAA,UAAY,MAAM,EAAA;AAChB,IAAA,MAAA,CAAA,cAAA,CAAA,GAAA,uBAAsC;AACtC,IAAA,MAAA,CAAA,cAAA,CAAA,GAAA,uBAAsC;AACxC,CAAC,EAHW,MAAM,KAAN,MAAM,GAGjB,EAAA,CAAA,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"enum.mjs","sources":["../../../src/common/enum.ts"],"sourcesContent":["/**\n * Enum representing the various event types emitted during the execution of runnables.\n * These events provide real-time information about the progress and state of different components.\n *\n * @enum {string}\n */\nexport enum GraphEvents {\n /* Custom Events */\n\n /** [Custom] Agent update event in multi-agent graph/workflow */\n ON_AGENT_UPDATE = 'on_agent_update',\n /** [Custom] Delta event for run steps (message creation and tool calls) */\n ON_RUN_STEP = 'on_run_step',\n /** [Custom] Delta event for run steps (tool calls) */\n ON_RUN_STEP_DELTA = 'on_run_step_delta',\n /** [Custom] Completed event for run steps (tool calls) */\n ON_RUN_STEP_COMPLETED = 'on_run_step_completed',\n /** [Custom] Delta events for messages */\n ON_MESSAGE_DELTA = 'on_message_delta',\n /** [Custom] Reasoning Delta events for messages */\n ON_REASONING_DELTA = 'on_reasoning_delta',\n /** [Custom] Context analytics event for traces */\n ON_CONTEXT_ANALYTICS = 'on_context_analytics',\n\n /* Official Events */\n\n /** Custom event, emitted by system */\n ON_CUSTOM_EVENT = 'on_custom_event',\n /** Emitted when a chat model starts processing. */\n CHAT_MODEL_START = 'on_chat_model_start',\n\n /** Emitted when a chat model streams a chunk of its response. */\n CHAT_MODEL_STREAM = 'on_chat_model_stream',\n\n /** Emitted when a chat model completes its processing. */\n CHAT_MODEL_END = 'on_chat_model_end',\n\n /** Emitted when a language model starts processing. */\n LLM_START = 'on_llm_start',\n\n /** Emitted when a language model streams a chunk of its response. */\n LLM_STREAM = 'on_llm_stream',\n\n /** Emitted when a language model completes its processing. */\n LLM_END = 'on_llm_end',\n\n /** Emitted when a chain starts processing. */\n CHAIN_START = 'on_chain_start',\n\n /** Emitted when a chain streams a chunk of its output. */\n CHAIN_STREAM = 'on_chain_stream',\n\n /** Emitted when a chain completes its processing. */\n CHAIN_END = 'on_chain_end',\n\n /** Emitted when a tool starts its operation. */\n TOOL_START = 'on_tool_start',\n\n /** Emitted when a tool completes its operation. */\n TOOL_END = 'on_tool_end',\n\n /** Emitted when a retriever starts its operation. */\n RETRIEVER_START = 'on_retriever_start',\n\n /** Emitted when a retriever completes its operation. */\n RETRIEVER_END = 'on_retriever_end',\n\n /** Emitted when a prompt starts processing. */\n PROMPT_START = 'on_prompt_start',\n\n /** Emitted when a prompt completes its processing. */\n PROMPT_END = 'on_prompt_end',\n}\n\nexport enum Providers {\n OPENAI = 'openAI',\n VERTEXAI = 'vertexai',\n BEDROCK = 'bedrock',\n ANTHROPIC = 'anthropic',\n MISTRALAI = 'mistralai',\n MISTRAL = 'mistral',\n GOOGLE = 'google',\n AZURE = 'azureOpenAI',\n DEEPSEEK = 'deepseek',\n OPENROUTER = 'openrouter',\n XAI = 'xai',\n}\n\nexport enum GraphNodeKeys {\n TOOLS = 'tools=',\n AGENT = 'agent=',\n ROUTER = 'router',\n PRE_TOOLS = 'pre_tools',\n POST_TOOLS = 'post_tools',\n}\n\nexport enum GraphNodeActions {\n TOOL_NODE = 'tool_node',\n CALL_MODEL = 'call_model',\n ROUTE_MESSAGE = 'route_message',\n}\n\nexport enum CommonEvents {\n LANGGRAPH = 'LangGraph',\n}\n\nexport enum StepTypes {\n TOOL_CALLS = 'tool_calls',\n MESSAGE_CREATION = 'message_creation',\n}\n\nexport enum ContentTypes {\n TEXT = 'text',\n ERROR = 'error',\n THINK = 'think',\n TOOL_CALL = 'tool_call',\n IMAGE_URL = 'image_url',\n IMAGE_FILE = 'image_file',\n /** Anthropic */\n THINKING = 'thinking',\n /** Vertex AI / Google Common */\n REASONING = 'reasoning',\n /** Multi-Agent Switch */\n AGENT_UPDATE = 'agent_update',\n /** Bedrock */\n REASONING_CONTENT = 'reasoning_content',\n}\n\nexport enum ToolCallTypes {\n FUNCTION = 'function',\n RETRIEVAL = 'retrieval',\n FILE_SEARCH = 'file_search',\n CODE_INTERPRETER = 'code_interpreter',\n /* Agents Tool Call */\n TOOL_CALL = 'tool_call',\n}\n\nexport enum Callback {\n TOOL_ERROR = 'handleToolError',\n TOOL_START = 'handleToolStart',\n TOOL_END = 'handleToolEnd',\n CUSTOM_EVENT = 'handleCustomEvent',\n /*\n LLM_START = 'handleLLMStart',\n LLM_NEW_TOKEN = 'handleLLMNewToken',\n LLM_ERROR = 'handleLLMError',\n LLM_END = 'handleLLMEnd',\n CHAT_MODEL_START = 'handleChatModelStart',\n CHAIN_START = 'handleChainStart',\n CHAIN_ERROR = 'handleChainError',\n CHAIN_END = 'handleChainEnd',\n TEXT = 'handleText',\n AGENT_ACTION = 'handleAgentAction',\n AGENT_END = 'handleAgentEnd',\n RETRIEVER_START = 'handleRetrieverStart',\n RETRIEVER_END = 'handleRetrieverEnd',\n RETRIEVER_ERROR = 'handleRetrieverError',\n */\n}\n\nexport enum Constants {\n OFFICIAL_CODE_BASEURL = 'https://api.illuma.ai/v1',\n EXECUTE_CODE = 'execute_code',\n TOOL_SEARCH_REGEX = 'tool_search_regex',\n PROGRAMMATIC_TOOL_CALLING = 'run_tools_with_code',\n WEB_SEARCH = 'web_search',\n CONTENT_AND_ARTIFACT = 'content_and_artifact',\n LC_TRANSFER_TO_ = 'lc_transfer_to_',\n}\n\nexport enum TitleMethod {\n STRUCTURED = 'structured',\n FUNCTIONS = 'functions',\n COMPLETION = 'completion',\n}\n\nexport enum EnvVar {\n CODE_API_KEY = 'CODE_EXECUTOR_API_KEY',\n CODE_BASEURL = 'CODE_EXECUTOR_BASEURL',\n}\n\n/**\n * Message type identifiers used by LangChain's BaseMessage.getType().\n * Use these constants instead of instanceof checks to avoid module mismatch issues\n * when different copies of @langchain/core/messages are loaded.\n */\nexport enum MessageTypes {\n HUMAN = 'human',\n AI = 'ai',\n SYSTEM = 'system',\n TOOL = 'tool',\n FUNCTION = 'function',\n GENERIC = 'generic',\n DEVELOPER = 'developer',\n REMOVE = 'remove',\n}\n"],"names":[],"mappings":"AAAA;;;;;AAKG;IACS;AAAZ,CAAA,UAAY,WAAW,EAAA;;;AAIrB,IAAA,WAAA,CAAA,iBAAA,CAAA,GAAA,iBAAmC;;AAEnC,IAAA,WAAA,CAAA,aAAA,CAAA,GAAA,aAA2B;;AAE3B,IAAA,WAAA,CAAA,mBAAA,CAAA,GAAA,mBAAuC;;AAEvC,IAAA,WAAA,CAAA,uBAAA,CAAA,GAAA,uBAA+C;;AAE/C,IAAA,WAAA,CAAA,kBAAA,CAAA,GAAA,kBAAqC;;AAErC,IAAA,WAAA,CAAA,oBAAA,CAAA,GAAA,oBAAyC;;AAEzC,IAAA,WAAA,CAAA,sBAAA,CAAA,GAAA,sBAA6C;;;AAK7C,IAAA,WAAA,CAAA,iBAAA,CAAA,GAAA,iBAAmC;;AAEnC,IAAA,WAAA,CAAA,kBAAA,CAAA,GAAA,qBAAwC;;AAGxC,IAAA,WAAA,CAAA,mBAAA,CAAA,GAAA,sBAA0C;;AAG1C,IAAA,WAAA,CAAA,gBAAA,CAAA,GAAA,mBAAoC;;AAGpC,IAAA,WAAA,CAAA,WAAA,CAAA,GAAA,cAA0B;;AAG1B,IAAA,WAAA,CAAA,YAAA,CAAA,GAAA,eAA4B;;AAG5B,IAAA,WAAA,CAAA,SAAA,CAAA,GAAA,YAAsB;;AAGtB,IAAA,WAAA,CAAA,aAAA,CAAA,GAAA,gBAA8B;;AAG9B,IAAA,WAAA,CAAA,cAAA,CAAA,GAAA,iBAAgC;;AAGhC,IAAA,WAAA,CAAA,WAAA,CAAA,GAAA,cAA0B;;A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A,CAAA,CAAA;IAEW;AAAZ,CAAA,UAAY,WAAW,EAAA;AACrB,IAAA,WAAA,CAAA,YAAA,CAAA,GAAA,YAAyB;AACzB,IAAA,WAAA,CAAA,WAAA,CAAA,GAAA,WAAuB;AACvB,IAAA,WAAA,CAAA,YAAA,CAAA,GAAA,YAAyB;AAC3B,CAAC,EAJW,WAAW,KAAX,WAAW,GAItB,EAAA,CAAA,CAAA;IAEW;AAAZ,CAAA,UAAY,MAAM,EAAA;AAChB,IAAA,MAAA,CAAA,cAAA,CAAA,GAAA,uBAAsC;AACtC,IAAA,MAAA,CAAA,cAAA,CAAA,GAAA,uBAAsC;AACxC,CAAC,EAHW,MAAM,KAAN,MAAM,GAGjB,EAAA,CAAA,CAAA;AAED;;;;AAIG;IACS;AAAZ,CAAA,UAAY,YAAY,EAAA;AACtB,IAAA,YAAA,CAAA,OAAA,CAAA,GAAA,OAAe;AACf,IAAA,YAAA,CAAA,IAAA,CAAA,GAAA,IAAS;AACT,IAAA,YAAA,CAAA,QAAA,CAAA,GAAA,QAAiB;AACjB,IAAA,YAAA,CAAA,MAAA,CAAA,GAAA,MAAa;AACb,IAAA,YAAA,CAAA,UAAA,CAAA,GAAA,UAAqB;AACrB,IAAA,YAAA,CAAA,SAAA,CAAA,GAAA,SAAmB;AACnB,IAAA,YAAA,CAAA,WAAA,CAAA,GAAA,WAAuB;AACvB,IAAA,YAAA,CAAA,QAAA,CAAA,GAAA,QAAiB;AACnB,CAAC,EATW,YAAY,KAAZ,YAAY,GASvB,EAAA,CAAA,CAAA;;;;"}
|
|
@@ -3,8 +3,8 @@ import { concat } from '@langchain/core/utils/stream';
|
|
|
3
3
|
import { ChatVertexAI } from '@langchain/google-vertexai';
|
|
4
4
|
import { Annotation, messagesStateReducer, StateGraph, START, END } from '@langchain/langgraph';
|
|
5
5
|
import { RunnableLambda } from '@langchain/core/runnables';
|
|
6
|
-
import { SystemMessage, HumanMessage, AIMessageChunk
|
|
7
|
-
import { GraphNodeKeys, ContentTypes, Providers, GraphEvents, StepTypes } from '../common/enum.mjs';
|
|
6
|
+
import { SystemMessage, HumanMessage, AIMessageChunk } from '@langchain/core/messages';
|
|
7
|
+
import { GraphNodeKeys, ContentTypes, Providers, GraphEvents, MessageTypes, StepTypes } from '../common/enum.mjs';
|
|
8
8
|
import { convertMessagesToContent, modifyDeltaProperties, formatAnthropicArtifactContent, formatArtifactPayload } from '../messages/core.mjs';
|
|
9
9
|
import { createPruneMessages } from '../messages/prune.mjs';
|
|
10
10
|
import { ensureThinkingBlockInMessages } from '../messages/format.mjs';
|
|
@@ -15,6 +15,8 @@ import { resetIfNotEmpty, joinKeys } from '../utils/graph.mjs';
|
|
|
15
15
|
import { isOpenAILike, isGoogleLike } from '../utils/llm.mjs';
|
|
16
16
|
import { sleep } from '../utils/run.mjs';
|
|
17
17
|
import 'js-tiktoken/lite';
|
|
18
|
+
import '../utils/toonFormat.mjs';
|
|
19
|
+
import { buildContextAnalytics } from '../utils/contextAnalytics.mjs';
|
|
18
20
|
import { getChatModelClass, manualToolStreamProviders } from '../llm/providers.mjs';
|
|
19
21
|
import { ToolNode, toolsCondition } from '../tools/ToolNode.mjs';
|
|
20
22
|
import { ChatOpenAI, AzureChatOpenAI } from '../llm/openai/index.mjs';
|
|
@@ -280,6 +282,15 @@ class StandardGraph extends Graph {
|
|
|
280
282
|
}
|
|
281
283
|
return primaryContext.getContextBreakdown();
|
|
282
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* Get the latest context analytics from the graph.
|
|
287
|
+
* Returns metrics like utilization %, TOON stats, message breakdown.
|
|
288
|
+
*/
|
|
289
|
+
getContextAnalytics() {
|
|
290
|
+
return this.lastContextAnalytics ?? null;
|
|
291
|
+
}
|
|
292
|
+
/** Store the latest context analytics for retrieval after run */
|
|
293
|
+
lastContextAnalytics = null;
|
|
283
294
|
/* Graph */
|
|
284
295
|
createSystemRunnable({ provider, clientOptions, instructions, additional_instructions, }) {
|
|
285
296
|
let finalInstructions = instructions;
|
|
@@ -468,14 +479,6 @@ class StandardGraph extends Graph {
|
|
|
468
479
|
}
|
|
469
480
|
this.config = config;
|
|
470
481
|
let messagesToUse = messages;
|
|
471
|
-
// Debug logging for pruneMessages creation conditions
|
|
472
|
-
const hasPruneMessages = !!agentContext.pruneMessages;
|
|
473
|
-
const hasTokenCounter = !!agentContext.tokenCounter;
|
|
474
|
-
const hasMaxContextTokens = agentContext.maxContextTokens != null;
|
|
475
|
-
const hasIndex0TokenCount = agentContext.indexTokenCountMap[0] != null;
|
|
476
|
-
if (!hasPruneMessages && hasTokenCounter && hasMaxContextTokens && !hasIndex0TokenCount) {
|
|
477
|
-
console.warn('[Graph] Cannot create pruneMessages - missing indexTokenCountMap[0]. Token map keys:', Object.keys(agentContext.indexTokenCountMap));
|
|
478
|
-
}
|
|
479
482
|
if (!agentContext.pruneMessages &&
|
|
480
483
|
agentContext.tokenCounter &&
|
|
481
484
|
agentContext.maxContextTokens != null &&
|
|
@@ -519,11 +522,12 @@ class StandardGraph extends Graph {
|
|
|
519
522
|
: null;
|
|
520
523
|
if (agentContext.provider === Providers.BEDROCK &&
|
|
521
524
|
lastMessageX instanceof AIMessageChunk &&
|
|
522
|
-
lastMessageY
|
|
525
|
+
lastMessageY?.getType() === MessageTypes.TOOL &&
|
|
523
526
|
typeof lastMessageX.content === 'string') {
|
|
524
527
|
finalMessages[finalMessages.length - 2].content = '';
|
|
525
528
|
}
|
|
526
|
-
|
|
529
|
+
// Use getType() instead of instanceof to avoid module mismatch issues
|
|
530
|
+
const isLatestToolMessage = lastMessageY?.getType() === MessageTypes.TOOL;
|
|
527
531
|
if (isLatestToolMessage &&
|
|
528
532
|
agentContext.provider === Providers.ANTHROPIC) {
|
|
529
533
|
formatAnthropicArtifactContent(finalMessages);
|
|
@@ -534,6 +538,27 @@ class StandardGraph extends Graph {
|
|
|
534
538
|
isGoogleLike(agentContext.provider))) {
|
|
535
539
|
formatArtifactPayload(finalMessages);
|
|
536
540
|
}
|
|
541
|
+
/**
|
|
542
|
+
* Handle edge case: when switching from a non-thinking agent to a thinking-enabled agent,
|
|
543
|
+
* convert AI messages with tool calls to HumanMessages to avoid thinking block requirements.
|
|
544
|
+
* This is required by Anthropic/Bedrock when thinking is enabled.
|
|
545
|
+
*
|
|
546
|
+
* IMPORTANT: This MUST happen BEFORE cache control is applied.
|
|
547
|
+
* If we add cachePoint to an AI message first, then convert that AI message to a HumanMessage,
|
|
548
|
+
* the cachePoint is lost. By converting first, we ensure cache control is applied to the
|
|
549
|
+
* final message structure that will be sent to the API.
|
|
550
|
+
*/
|
|
551
|
+
const isAnthropicWithThinking = (agentContext.provider === Providers.ANTHROPIC &&
|
|
552
|
+
agentContext.clientOptions.thinking !=
|
|
553
|
+
null) ||
|
|
554
|
+
(agentContext.provider === Providers.BEDROCK &&
|
|
555
|
+
agentContext.clientOptions
|
|
556
|
+
.additionalModelRequestFields?.['thinking'] != null);
|
|
557
|
+
if (isAnthropicWithThinking) {
|
|
558
|
+
finalMessages = ensureThinkingBlockInMessages(finalMessages, agentContext.provider);
|
|
559
|
+
}
|
|
560
|
+
// Apply cache control AFTER thinking block handling to ensure cachePoints aren't lost
|
|
561
|
+
// when AI messages are converted to HumanMessages
|
|
537
562
|
if (agentContext.provider === Providers.ANTHROPIC) {
|
|
538
563
|
const anthropicOptions = agentContext.clientOptions;
|
|
539
564
|
const defaultHeaders = anthropicOptions?.clientOptions
|
|
@@ -554,20 +579,6 @@ class StandardGraph extends Graph {
|
|
|
554
579
|
finalMessages = addBedrockCacheControl(finalMessages);
|
|
555
580
|
}
|
|
556
581
|
}
|
|
557
|
-
/**
|
|
558
|
-
* Handle edge case: when switching from a non-thinking agent to a thinking-enabled agent,
|
|
559
|
-
* convert AI messages with tool calls to HumanMessages to avoid thinking block requirements.
|
|
560
|
-
* This is required by Anthropic/Bedrock when thinking is enabled.
|
|
561
|
-
*/
|
|
562
|
-
const isAnthropicWithThinking = (agentContext.provider === Providers.ANTHROPIC &&
|
|
563
|
-
agentContext.clientOptions.thinking !=
|
|
564
|
-
null) ||
|
|
565
|
-
(agentContext.provider === Providers.BEDROCK &&
|
|
566
|
-
agentContext.clientOptions
|
|
567
|
-
.additionalModelRequestFields?.['thinking'] != null);
|
|
568
|
-
if (isAnthropicWithThinking) {
|
|
569
|
-
finalMessages = ensureThinkingBlockInMessages(finalMessages, agentContext.provider);
|
|
570
|
-
}
|
|
571
582
|
if (agentContext.lastStreamCall != null &&
|
|
572
583
|
agentContext.streamBuffer != null) {
|
|
573
584
|
const timeSinceLastCall = Date.now() - agentContext.lastStreamCall;
|
|
@@ -587,6 +598,27 @@ class StandardGraph extends Graph {
|
|
|
587
598
|
info: 'Message pruning removed all messages as none fit in the context window. Please increase the context window size or make your message shorter.',
|
|
588
599
|
}));
|
|
589
600
|
}
|
|
601
|
+
// Get model info for analytics
|
|
602
|
+
const bedrockOpts = agentContext.clientOptions;
|
|
603
|
+
const modelId = bedrockOpts?.model || agentContext.clientOptions?.modelName;
|
|
604
|
+
const thinkingConfig = bedrockOpts?.additionalModelRequestFields?.['thinking'] ||
|
|
605
|
+
agentContext.clientOptions?.thinking;
|
|
606
|
+
// Build and emit context analytics for traces
|
|
607
|
+
const contextAnalytics = buildContextAnalytics(finalMessages, {
|
|
608
|
+
tokenCounter: agentContext.tokenCounter,
|
|
609
|
+
maxContextTokens: agentContext.maxContextTokens,
|
|
610
|
+
instructionTokens: agentContext.instructionTokens,
|
|
611
|
+
indexTokenCountMap: agentContext.indexTokenCountMap,
|
|
612
|
+
});
|
|
613
|
+
// Store for retrieval via getContextAnalytics() after run completes
|
|
614
|
+
this.lastContextAnalytics = contextAnalytics;
|
|
615
|
+
await safeDispatchCustomEvent(GraphEvents.ON_CONTEXT_ANALYTICS, {
|
|
616
|
+
provider: agentContext.provider,
|
|
617
|
+
model: modelId,
|
|
618
|
+
thinkingEnabled: thinkingConfig != null,
|
|
619
|
+
cacheEnabled: bedrockOpts?.promptCache === true,
|
|
620
|
+
analytics: contextAnalytics,
|
|
621
|
+
}, config);
|
|
590
622
|
try {
|
|
591
623
|
result = await this.attemptInvoke({
|
|
592
624
|
currentModel: model,
|
|
@@ -675,7 +707,13 @@ If I seem to be missing something we discussed earlier, just give me a quick rem
|
|
|
675
707
|
let retryMessages = agentContext.useLegacyContent
|
|
676
708
|
? formatContentStrings(messagesWithNotice)
|
|
677
709
|
: messagesWithNotice;
|
|
678
|
-
// Apply
|
|
710
|
+
// Apply thinking block handling first (before cache control)
|
|
711
|
+
// This ensures AI+Tool sequences are converted to HumanMessages
|
|
712
|
+
// before we add cache points that could be lost in the conversion
|
|
713
|
+
if (isAnthropicWithThinking) {
|
|
714
|
+
retryMessages = ensureThinkingBlockInMessages(retryMessages, agentContext.provider);
|
|
715
|
+
}
|
|
716
|
+
// Apply Bedrock cache control if needed (after thinking block handling)
|
|
679
717
|
if (agentContext.provider === Providers.BEDROCK) {
|
|
680
718
|
const bedrockOptions = agentContext.clientOptions;
|
|
681
719
|
const modelId = bedrockOptions?.model?.toLowerCase() ?? '';
|