illuma-agents 1.0.79 → 1.0.80
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/graphs/Graph.cjs +1 -1
- package/dist/cjs/llm/openai/index.cjs +1 -1
- package/dist/cjs/tools/ToolNode.cjs +1 -1
- package/dist/cjs/utils/tokens.cjs +21 -8
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +1 -1
- package/dist/esm/llm/openai/index.mjs +1 -1
- package/dist/esm/tools/ToolNode.mjs +1 -1
- package/dist/esm/utils/tokens.mjs +21 -8
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/utils/tokens.d.ts +2 -1
- package/package.json +1 -1
- package/src/utils/tokens.ts +23 -10
|
@@ -16,7 +16,7 @@ var tools = require('../messages/tools.cjs');
|
|
|
16
16
|
var graph = require('../utils/graph.cjs');
|
|
17
17
|
var llm = require('../utils/llm.cjs');
|
|
18
18
|
var run = require('../utils/run.cjs');
|
|
19
|
-
require('js-tiktoken
|
|
19
|
+
require('js-tiktoken');
|
|
20
20
|
require('../utils/toonFormat.cjs');
|
|
21
21
|
var contextAnalytics = require('../utils/contextAnalytics.cjs');
|
|
22
22
|
require('zod-to-json-schema');
|
|
@@ -13,7 +13,7 @@ require('nanoid');
|
|
|
13
13
|
require('../../messages/core.cjs');
|
|
14
14
|
require('../../utils/toonFormat.cjs');
|
|
15
15
|
var run = require('../../utils/run.cjs');
|
|
16
|
-
require('js-tiktoken
|
|
16
|
+
require('js-tiktoken');
|
|
17
17
|
require('zod-to-json-schema');
|
|
18
18
|
|
|
19
19
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
@@ -7,7 +7,7 @@ require('nanoid');
|
|
|
7
7
|
require('../messages/core.cjs');
|
|
8
8
|
var toonFormat = require('../utils/toonFormat.cjs');
|
|
9
9
|
var run = require('../utils/run.cjs');
|
|
10
|
-
require('js-tiktoken
|
|
10
|
+
require('js-tiktoken');
|
|
11
11
|
require('zod-to-json-schema');
|
|
12
12
|
var events = require('../utils/events.cjs');
|
|
13
13
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var jsTiktoken = require('js-tiktoken');
|
|
4
4
|
var _enum = require('../common/enum.cjs');
|
|
5
5
|
|
|
6
6
|
function getTokenCountForMessage(message, getTokenCount) {
|
|
@@ -52,20 +52,31 @@ function getTokenCountForMessage(message, getTokenCount) {
|
|
|
52
52
|
}
|
|
53
53
|
let encoderPromise;
|
|
54
54
|
let tokenCounterPromise;
|
|
55
|
+
/**
|
|
56
|
+
* Simple character-based token estimation (~4 chars per token).
|
|
57
|
+
* Used as fallback when tiktoken encoder fails to load.
|
|
58
|
+
*/
|
|
59
|
+
const estimateTokens = (text) => Math.ceil(text.length / 4);
|
|
55
60
|
async function getSharedEncoder() {
|
|
56
61
|
if (encoderPromise) {
|
|
57
62
|
return encoderPromise;
|
|
58
63
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
// Use bundled tokenizer data - no external network calls
|
|
65
|
+
encoderPromise = Promise.resolve().then(() => {
|
|
66
|
+
try {
|
|
67
|
+
return jsTiktoken.getEncoding('o200k_base');
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// Fallback to null - will use character estimation
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
64
74
|
return encoderPromise;
|
|
65
75
|
}
|
|
66
76
|
/**
|
|
67
77
|
* Creates a singleton token counter function that reuses the same encoder instance.
|
|
68
|
-
*
|
|
78
|
+
* Falls back to character-based estimation (~4 chars per token) if encoder fails.
|
|
79
|
+
* This ensures token counting never blocks LLM calls.
|
|
69
80
|
*/
|
|
70
81
|
const createTokenCounter = async () => {
|
|
71
82
|
if (tokenCounterPromise) {
|
|
@@ -73,7 +84,9 @@ const createTokenCounter = async () => {
|
|
|
73
84
|
}
|
|
74
85
|
tokenCounterPromise = (async () => {
|
|
75
86
|
const enc = await getSharedEncoder();
|
|
76
|
-
const countTokens =
|
|
87
|
+
const countTokens = enc
|
|
88
|
+
? (text) => enc.encode(text).length
|
|
89
|
+
: estimateTokens;
|
|
77
90
|
return (message) => getTokenCountForMessage(message, countTokens);
|
|
78
91
|
})();
|
|
79
92
|
return tokenCounterPromise;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokens.cjs","sources":["../../../src/utils/tokens.ts"],"sourcesContent":["import { Tiktoken } from 'js-tiktoken
|
|
1
|
+
{"version":3,"file":"tokens.cjs","sources":["../../../src/utils/tokens.ts"],"sourcesContent":["import { getEncoding, type Tiktoken } from 'js-tiktoken';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport { ContentTypes } from '@/common/enum';\n\nexport function getTokenCountForMessage(\n message: BaseMessage,\n getTokenCount: (text: string) => number\n): number {\n const tokensPerMessage = 3;\n\n const processValue = (value: unknown): void => {\n if (Array.isArray(value)) {\n for (const item of value) {\n if (\n !item ||\n !item.type ||\n item.type === ContentTypes.ERROR ||\n item.type === ContentTypes.IMAGE_URL\n ) {\n continue;\n }\n\n if (item.type === ContentTypes.TOOL_CALL && item.tool_call != null) {\n const toolName = item.tool_call?.name || '';\n if (toolName != null && toolName && typeof toolName === 'string') {\n numTokens += getTokenCount(toolName);\n }\n\n const args = item.tool_call?.args || '';\n if (args != null && args && typeof args === 'string') {\n numTokens += getTokenCount(args);\n }\n\n const output = item.tool_call?.output || '';\n if (output != null && output && typeof output === 'string') {\n numTokens += getTokenCount(output);\n }\n continue;\n }\n\n const nestedValue = item[item.type];\n\n if (!nestedValue) {\n continue;\n }\n\n processValue(nestedValue);\n }\n } else if (typeof value === 'string') {\n numTokens += getTokenCount(value);\n } else if (typeof value === 'number') {\n numTokens += getTokenCount(value.toString());\n } else if (typeof value === 'boolean') {\n numTokens += getTokenCount(value.toString());\n }\n };\n\n let numTokens = tokensPerMessage;\n processValue(message.content);\n return numTokens;\n}\n\nlet encoderPromise: Promise<Tiktoken | null> | undefined;\nlet tokenCounterPromise: Promise<(message: BaseMessage) => number> | undefined;\n\n/**\n * Simple character-based token estimation (~4 chars per token).\n * Used as fallback when tiktoken encoder fails to load.\n */\nconst estimateTokens = (text: string): number => Math.ceil(text.length / 4);\n\nasync function getSharedEncoder(): Promise<Tiktoken | null> {\n if (encoderPromise) {\n return encoderPromise;\n }\n // Use bundled tokenizer data - no external network calls\n encoderPromise = Promise.resolve().then(() => {\n try {\n return getEncoding('o200k_base');\n } catch {\n // Fallback to null - will use character estimation\n return null;\n }\n });\n return encoderPromise;\n}\n\n/**\n * Creates a singleton token counter function that reuses the same encoder instance.\n * Falls back to character-based estimation (~4 chars per token) if encoder fails.\n * This ensures token counting never blocks LLM calls.\n */\nexport const createTokenCounter = async (): Promise<\n (message: BaseMessage) => number\n> => {\n if (tokenCounterPromise) {\n return tokenCounterPromise;\n }\n\n tokenCounterPromise = (async (): Promise<\n (message: BaseMessage) => number\n > => {\n const enc = await getSharedEncoder();\n const countTokens = enc\n ? (text: string): number => enc.encode(text).length\n : estimateTokens;\n return (message: BaseMessage): number =>\n getTokenCountForMessage(message, countTokens);\n })();\n\n return tokenCounterPromise;\n};\n\n/**\n * Utility to manage the token encoder lifecycle explicitly.\n * Useful for applications that need fine-grained control over resource management.\n */\nexport const TokenEncoderManager = {\n /**\n * Pre-initializes the encoder. This can be called during app startup\n * to avoid lazy loading delays later.\n */\n async initialize(): Promise<void> {\n await getSharedEncoder();\n },\n\n /**\n * Clears the cached encoder and token counter.\n * Useful for testing or when you need to force a fresh reload.\n */\n reset(): void {\n encoderPromise = undefined;\n tokenCounterPromise = undefined;\n },\n\n /**\n * Checks if the encoder has been initialized.\n */\n isInitialized(): boolean {\n return encoderPromise !== undefined;\n },\n};\n"],"names":["ContentTypes","getEncoding"],"mappings":";;;;;AAIgB,SAAA,uBAAuB,CACrC,OAAoB,EACpB,a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OAAO,CAAC,OAAoB,KAC1B,uBAAuB,CAAC,OAAO,EAAE,WAAW,CAAC;KAChD,GAAG;AAEJ,IAAA,OAAO,mBAAmB;AAC5B;AAEA;;;AAGG;AACU,MAAA,mBAAmB,GAAG;AACjC;;;AAGG;AACH,IAAA,MAAM,UAAU,GAAA;QACd,MAAM,gBAAgB,EAAE;KACzB;AAED;;;AAGG;IACH,KAAK,GAAA;QACH,cAAc,GAAG,SAAS;QAC1B,mBAAmB,GAAG,SAAS;KAChC;AAED;;AAEG;IACH,aAAa,GAAA;QACX,OAAO,cAAc,KAAK,SAAS;KACpC;;;;;;;"}
|
|
@@ -14,7 +14,7 @@ import { extractToolDiscoveries } from '../messages/tools.mjs';
|
|
|
14
14
|
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
|
-
import 'js-tiktoken
|
|
17
|
+
import 'js-tiktoken';
|
|
18
18
|
import '../utils/toonFormat.mjs';
|
|
19
19
|
import { buildContextAnalytics } from '../utils/contextAnalytics.mjs';
|
|
20
20
|
import 'zod-to-json-schema';
|
|
@@ -11,7 +11,7 @@ import 'nanoid';
|
|
|
11
11
|
import '../../messages/core.mjs';
|
|
12
12
|
import '../../utils/toonFormat.mjs';
|
|
13
13
|
import { sleep } from '../../utils/run.mjs';
|
|
14
|
-
import 'js-tiktoken
|
|
14
|
+
import 'js-tiktoken';
|
|
15
15
|
import 'zod-to-json-schema';
|
|
16
16
|
|
|
17
17
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
@@ -5,7 +5,7 @@ import 'nanoid';
|
|
|
5
5
|
import '../messages/core.mjs';
|
|
6
6
|
import { processToolOutput } from '../utils/toonFormat.mjs';
|
|
7
7
|
import { RunnableCallable } from '../utils/run.mjs';
|
|
8
|
-
import 'js-tiktoken
|
|
8
|
+
import 'js-tiktoken';
|
|
9
9
|
import 'zod-to-json-schema';
|
|
10
10
|
import { safeDispatchCustomEvent } from '../utils/events.mjs';
|
|
11
11
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getEncoding } from 'js-tiktoken';
|
|
2
2
|
import { ContentTypes } from '../common/enum.mjs';
|
|
3
3
|
|
|
4
4
|
function getTokenCountForMessage(message, getTokenCount) {
|
|
@@ -50,20 +50,31 @@ function getTokenCountForMessage(message, getTokenCount) {
|
|
|
50
50
|
}
|
|
51
51
|
let encoderPromise;
|
|
52
52
|
let tokenCounterPromise;
|
|
53
|
+
/**
|
|
54
|
+
* Simple character-based token estimation (~4 chars per token).
|
|
55
|
+
* Used as fallback when tiktoken encoder fails to load.
|
|
56
|
+
*/
|
|
57
|
+
const estimateTokens = (text) => Math.ceil(text.length / 4);
|
|
53
58
|
async function getSharedEncoder() {
|
|
54
59
|
if (encoderPromise) {
|
|
55
60
|
return encoderPromise;
|
|
56
61
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
// Use bundled tokenizer data - no external network calls
|
|
63
|
+
encoderPromise = Promise.resolve().then(() => {
|
|
64
|
+
try {
|
|
65
|
+
return getEncoding('o200k_base');
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Fallback to null - will use character estimation
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
62
72
|
return encoderPromise;
|
|
63
73
|
}
|
|
64
74
|
/**
|
|
65
75
|
* Creates a singleton token counter function that reuses the same encoder instance.
|
|
66
|
-
*
|
|
76
|
+
* Falls back to character-based estimation (~4 chars per token) if encoder fails.
|
|
77
|
+
* This ensures token counting never blocks LLM calls.
|
|
67
78
|
*/
|
|
68
79
|
const createTokenCounter = async () => {
|
|
69
80
|
if (tokenCounterPromise) {
|
|
@@ -71,7 +82,9 @@ const createTokenCounter = async () => {
|
|
|
71
82
|
}
|
|
72
83
|
tokenCounterPromise = (async () => {
|
|
73
84
|
const enc = await getSharedEncoder();
|
|
74
|
-
const countTokens =
|
|
85
|
+
const countTokens = enc
|
|
86
|
+
? (text) => enc.encode(text).length
|
|
87
|
+
: estimateTokens;
|
|
75
88
|
return (message) => getTokenCountForMessage(message, countTokens);
|
|
76
89
|
})();
|
|
77
90
|
return tokenCounterPromise;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokens.mjs","sources":["../../../src/utils/tokens.ts"],"sourcesContent":["import { Tiktoken } from 'js-tiktoken
|
|
1
|
+
{"version":3,"file":"tokens.mjs","sources":["../../../src/utils/tokens.ts"],"sourcesContent":["import { getEncoding, type Tiktoken } from 'js-tiktoken';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport { ContentTypes } from '@/common/enum';\n\nexport function getTokenCountForMessage(\n message: BaseMessage,\n getTokenCount: (text: string) => number\n): number {\n const tokensPerMessage = 3;\n\n const processValue = (value: unknown): void => {\n if (Array.isArray(value)) {\n for (const item of value) {\n if (\n !item ||\n !item.type ||\n item.type === ContentTypes.ERROR ||\n item.type === ContentTypes.IMAGE_URL\n ) {\n continue;\n }\n\n if (item.type === ContentTypes.TOOL_CALL && item.tool_call != null) {\n const toolName = item.tool_call?.name || '';\n if (toolName != null && toolName && typeof toolName === 'string') {\n numTokens += getTokenCount(toolName);\n }\n\n const args = item.tool_call?.args || '';\n if (args != null && args && typeof args === 'string') {\n numTokens += getTokenCount(args);\n }\n\n const output = item.tool_call?.output || '';\n if (output != null && output && typeof output === 'string') {\n numTokens += getTokenCount(output);\n }\n continue;\n }\n\n const nestedValue = item[item.type];\n\n if (!nestedValue) {\n continue;\n }\n\n processValue(nestedValue);\n }\n } else if (typeof value === 'string') {\n numTokens += getTokenCount(value);\n } else if (typeof value === 'number') {\n numTokens += getTokenCount(value.toString());\n } else if (typeof value === 'boolean') {\n numTokens += getTokenCount(value.toString());\n }\n };\n\n let numTokens = tokensPerMessage;\n processValue(message.content);\n return numTokens;\n}\n\nlet encoderPromise: Promise<Tiktoken | null> | undefined;\nlet tokenCounterPromise: Promise<(message: BaseMessage) => number> | undefined;\n\n/**\n * Simple character-based token estimation (~4 chars per token).\n * Used as fallback when tiktoken encoder fails to load.\n */\nconst estimateTokens = (text: string): number => Math.ceil(text.length / 4);\n\nasync function getSharedEncoder(): Promise<Tiktoken | null> {\n if (encoderPromise) {\n return encoderPromise;\n }\n // Use bundled tokenizer data - no external network calls\n encoderPromise = Promise.resolve().then(() => {\n try {\n return getEncoding('o200k_base');\n } catch {\n // Fallback to null - will use character estimation\n return null;\n }\n });\n return encoderPromise;\n}\n\n/**\n * Creates a singleton token counter function that reuses the same encoder instance.\n * Falls back to character-based estimation (~4 chars per token) if encoder fails.\n * This ensures token counting never blocks LLM calls.\n */\nexport const createTokenCounter = async (): Promise<\n (message: BaseMessage) => number\n> => {\n if (tokenCounterPromise) {\n return tokenCounterPromise;\n }\n\n tokenCounterPromise = (async (): Promise<\n (message: BaseMessage) => number\n > => {\n const enc = await getSharedEncoder();\n const countTokens = enc\n ? (text: string): number => enc.encode(text).length\n : estimateTokens;\n return (message: BaseMessage): number =>\n getTokenCountForMessage(message, countTokens);\n })();\n\n return tokenCounterPromise;\n};\n\n/**\n * Utility to manage the token encoder lifecycle explicitly.\n * Useful for applications that need fine-grained control over resource management.\n */\nexport const TokenEncoderManager = {\n /**\n * Pre-initializes the encoder. This can be called during app startup\n * to avoid lazy loading delays later.\n */\n async initialize(): Promise<void> {\n await getSharedEncoder();\n },\n\n /**\n * Clears the cached encoder and token counter.\n * Useful for testing or when you need to force a fresh reload.\n */\n reset(): void {\n encoderPromise = undefined;\n tokenCounterPromise = undefined;\n },\n\n /**\n * Checks if the encoder has been initialized.\n */\n isInitialized(): boolean {\n return encoderPromise !== undefined;\n },\n};\n"],"names":[],"mappings":";;;AAIgB,SAAA,uBAAuB,CACrC,OAAoB,EACpB,aAAuC,EAAA;IAEvC,MAAM,gBAAgB,GAAG,CAAC;AAE1B,IAAA,MAAM,Y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;AAEJ,IAAA,OAAO,mBAAmB;AAC5B;AAEA;;;AAGG;AACU,MAAA,mBAAmB,GAAG;AACjC;;;AAGG;AACH,IAAA,MAAM,UAAU,GAAA;QACd,MAAM,gBAAgB,EAAE;KACzB;AAED;;;AAGG;IACH,KAAK,GAAA;QACH,cAAc,GAAG,SAAS;QAC1B,mBAAmB,GAAG,SAAS;KAChC;AAED;;AAEG;IACH,aAAa,GAAA;QACX,OAAO,cAAc,KAAK,SAAS;KACpC;;;;;"}
|
|
@@ -2,7 +2,8 @@ import type { BaseMessage } from '@langchain/core/messages';
|
|
|
2
2
|
export declare function getTokenCountForMessage(message: BaseMessage, getTokenCount: (text: string) => number): number;
|
|
3
3
|
/**
|
|
4
4
|
* Creates a singleton token counter function that reuses the same encoder instance.
|
|
5
|
-
*
|
|
5
|
+
* Falls back to character-based estimation (~4 chars per token) if encoder fails.
|
|
6
|
+
* This ensures token counting never blocks LLM calls.
|
|
6
7
|
*/
|
|
7
8
|
export declare const createTokenCounter: () => Promise<(message: BaseMessage) => number>;
|
|
8
9
|
/**
|
package/package.json
CHANGED
package/src/utils/tokens.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Tiktoken } from 'js-tiktoken
|
|
1
|
+
import { getEncoding, type Tiktoken } from 'js-tiktoken';
|
|
2
2
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
3
3
|
import { ContentTypes } from '@/common/enum';
|
|
4
4
|
|
|
@@ -60,24 +60,35 @@ export function getTokenCountForMessage(
|
|
|
60
60
|
return numTokens;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
let encoderPromise: Promise<Tiktoken> | undefined;
|
|
63
|
+
let encoderPromise: Promise<Tiktoken | null> | undefined;
|
|
64
64
|
let tokenCounterPromise: Promise<(message: BaseMessage) => number> | undefined;
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Simple character-based token estimation (~4 chars per token).
|
|
68
|
+
* Used as fallback when tiktoken encoder fails to load.
|
|
69
|
+
*/
|
|
70
|
+
const estimateTokens = (text: string): number => Math.ceil(text.length / 4);
|
|
71
|
+
|
|
72
|
+
async function getSharedEncoder(): Promise<Tiktoken | null> {
|
|
67
73
|
if (encoderPromise) {
|
|
68
74
|
return encoderPromise;
|
|
69
75
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
// Use bundled tokenizer data - no external network calls
|
|
77
|
+
encoderPromise = Promise.resolve().then(() => {
|
|
78
|
+
try {
|
|
79
|
+
return getEncoding('o200k_base');
|
|
80
|
+
} catch {
|
|
81
|
+
// Fallback to null - will use character estimation
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
75
85
|
return encoderPromise;
|
|
76
86
|
}
|
|
77
87
|
|
|
78
88
|
/**
|
|
79
89
|
* Creates a singleton token counter function that reuses the same encoder instance.
|
|
80
|
-
*
|
|
90
|
+
* Falls back to character-based estimation (~4 chars per token) if encoder fails.
|
|
91
|
+
* This ensures token counting never blocks LLM calls.
|
|
81
92
|
*/
|
|
82
93
|
export const createTokenCounter = async (): Promise<
|
|
83
94
|
(message: BaseMessage) => number
|
|
@@ -90,7 +101,9 @@ export const createTokenCounter = async (): Promise<
|
|
|
90
101
|
(message: BaseMessage) => number
|
|
91
102
|
> => {
|
|
92
103
|
const enc = await getSharedEncoder();
|
|
93
|
-
const countTokens =
|
|
104
|
+
const countTokens = enc
|
|
105
|
+
? (text: string): number => enc.encode(text).length
|
|
106
|
+
: estimateTokens;
|
|
94
107
|
return (message: BaseMessage): number =>
|
|
95
108
|
getTokenCountForMessage(message, countTokens);
|
|
96
109
|
})();
|