@nodetool-ai/chat 0.7.0-rc.11
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.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/message-processor.d.ts +55 -0
- package/dist/message-processor.d.ts.map +1 -0
- package/dist/message-processor.js +154 -0
- package/dist/message-processor.js.map +1 -0
- package/dist/token-counter.d.ts +29 -0
- package/dist/token-counter.d.ts.map +1 -0
- package/dist/token-counter.js +78 -0
- package/dist/token-counter.js.map +1 -0
- package/package.json +46 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nodetool-ai/chat -- Chat processing with streaming and tool calling.
|
|
3
|
+
*/
|
|
4
|
+
export { countTextTokens, countMessageTokens, countMessagesTokens } from "./token-counter.js";
|
|
5
|
+
export { runTool, processChat } from "./message-processor.js";
|
|
6
|
+
export type { ChatCallbacks } from "./message-processor.js";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC9D,YAAY,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nodetool-ai/chat -- Chat processing with streaming and tool calling.
|
|
3
|
+
*/
|
|
4
|
+
export { countTextTokens, countMessageTokens, countMessagesTokens } from "./token-counter.js";
|
|
5
|
+
export { runTool, processChat } from "./message-processor.js";
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core chat processing loop with streaming and tool calling.
|
|
3
|
+
*
|
|
4
|
+
* Port of src/nodetool/chat/regular_chat.py (process_regular_chat).
|
|
5
|
+
*/
|
|
6
|
+
import type { BaseProvider } from "@nodetool-ai/runtime";
|
|
7
|
+
import type { Message, ToolCall } from "@nodetool-ai/runtime";
|
|
8
|
+
import type { ProcessingContext } from "@nodetool-ai/runtime";
|
|
9
|
+
import type { Tool } from "@nodetool-ai/agents";
|
|
10
|
+
export interface ChatCallbacks {
|
|
11
|
+
/** Called for each text chunk streamed from the provider. */
|
|
12
|
+
onChunk?: (text: string) => void;
|
|
13
|
+
/** Called when a tool call is received from the provider. */
|
|
14
|
+
onToolCall?: (toolCall: ToolCall) => void;
|
|
15
|
+
/** Called after a tool has been executed. */
|
|
16
|
+
onToolResult?: (toolCall: ToolCall, result: unknown) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Find and execute a tool by name, returning the ToolCall updated with the result.
|
|
20
|
+
*/
|
|
21
|
+
export declare function runTool(context: ProcessingContext, toolCall: ToolCall, tools: Tool[]): Promise<ToolCall>;
|
|
22
|
+
/**
|
|
23
|
+
* Serializer that handles objects with a `toJSON` method or falls back to
|
|
24
|
+
* stringification, similar to the Python `default_serializer`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function defaultSerializer(_key: string, value: unknown): unknown;
|
|
27
|
+
/**
|
|
28
|
+
* Process a user message through the provider with streaming and tool calling.
|
|
29
|
+
*
|
|
30
|
+
* Implements the core loop from `process_regular_chat`:
|
|
31
|
+
* 1. Append user message.
|
|
32
|
+
* 2. Stream provider response, accumulating text chunks into an assistant message.
|
|
33
|
+
* 3. When tool calls are received, execute each tool and append assistant + tool messages.
|
|
34
|
+
* 4. If tool calls were processed, re-send the new messages to get the next response.
|
|
35
|
+
* 5. When no more tool calls are pending, return the full message history.
|
|
36
|
+
*/
|
|
37
|
+
export declare function processChat(opts: {
|
|
38
|
+
userInput: string;
|
|
39
|
+
messages: Message[];
|
|
40
|
+
model: string;
|
|
41
|
+
provider: BaseProvider;
|
|
42
|
+
context: ProcessingContext;
|
|
43
|
+
tools?: Tool[];
|
|
44
|
+
callbacks?: ChatCallbacks;
|
|
45
|
+
threadId?: string;
|
|
46
|
+
signal?: AbortSignal;
|
|
47
|
+
/**
|
|
48
|
+
* Cap on tool-calling rounds before we stop and let the user intervene.
|
|
49
|
+
* Each round = one provider stream + parallel execution of any tool calls
|
|
50
|
+
* it produced. Prevents runaway loops when the model repeatedly emits
|
|
51
|
+
* invalid tool calls and gets the same error back. Defaults to 25.
|
|
52
|
+
*/
|
|
53
|
+
maxIterations?: number;
|
|
54
|
+
}): Promise<Message[]>;
|
|
55
|
+
//# sourceMappingURL=message-processor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-processor.d.ts","sourceRoot":"","sources":["../src/message-processor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAsB,MAAM,sBAAsB,CAAC;AAClF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAMhD,MAAM,WAAW,aAAa;IAC5B,6DAA6D;IAC7D,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,6DAA6D;IAC7D,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;IAC1C,6CAA6C;IAC7C,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;CAC9D;AAMD;;GAEG;AACH,wBAAsB,OAAO,CAC3B,OAAO,EAAE,iBAAiB,EAC1B,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,IAAI,EAAE,GACZ,OAAO,CAAC,QAAQ,CAAC,CAcnB;AAcD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAKvE;AAED;;;;;;;;;GASG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,iBAAiB,CAAC;IAC3B,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;IACf,SAAS,CAAC,EAAE,aAAa,CAAC;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAsIrB"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core chat processing loop with streaming and tool calling.
|
|
3
|
+
*
|
|
4
|
+
* Port of src/nodetool/chat/regular_chat.py (process_regular_chat).
|
|
5
|
+
*/
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// Tool runner
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
/**
|
|
10
|
+
* Find and execute a tool by name, returning the ToolCall updated with the result.
|
|
11
|
+
*/
|
|
12
|
+
export async function runTool(context, toolCall, tools) {
|
|
13
|
+
const tool = tools.find((t) => t.name === toolCall.name);
|
|
14
|
+
if (!tool) {
|
|
15
|
+
throw new Error(`Tool "${toolCall.name}" not found`);
|
|
16
|
+
}
|
|
17
|
+
const result = await tool.process(context, toolCall.args);
|
|
18
|
+
return {
|
|
19
|
+
id: toolCall.id,
|
|
20
|
+
name: toolCall.name,
|
|
21
|
+
args: toolCall.args,
|
|
22
|
+
result
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Chat processing loop
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
function isChunk(item) {
|
|
29
|
+
return "type" in item && item.type === "chunk";
|
|
30
|
+
}
|
|
31
|
+
function isToolCall(item) {
|
|
32
|
+
return "name" in item && "id" in item && !("type" in item);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Serializer that handles objects with a `toJSON` method or falls back to
|
|
36
|
+
* stringification, similar to the Python `default_serializer`.
|
|
37
|
+
*/
|
|
38
|
+
export function defaultSerializer(_key, value) {
|
|
39
|
+
if (value !== null && typeof value === "object" && "toJSON" in value) {
|
|
40
|
+
return value.toJSON();
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Process a user message through the provider with streaming and tool calling.
|
|
46
|
+
*
|
|
47
|
+
* Implements the core loop from `process_regular_chat`:
|
|
48
|
+
* 1. Append user message.
|
|
49
|
+
* 2. Stream provider response, accumulating text chunks into an assistant message.
|
|
50
|
+
* 3. When tool calls are received, execute each tool and append assistant + tool messages.
|
|
51
|
+
* 4. If tool calls were processed, re-send the new messages to get the next response.
|
|
52
|
+
* 5. When no more tool calls are pending, return the full message history.
|
|
53
|
+
*/
|
|
54
|
+
export async function processChat(opts) {
|
|
55
|
+
const { userInput, messages, model, provider, context, tools = [], callbacks, threadId, signal, maxIterations = 25 } = opts;
|
|
56
|
+
// 1. Add user message
|
|
57
|
+
messages.push({ role: "user", content: userInput });
|
|
58
|
+
const providerTools = tools.length > 0 ? tools.map((t) => t.toProviderTool()) : undefined;
|
|
59
|
+
let messagesToSend = messages;
|
|
60
|
+
for (let iteration = 0; iteration < maxIterations; iteration++) {
|
|
61
|
+
const toolCallResults = [];
|
|
62
|
+
let assistantText = "";
|
|
63
|
+
const stream = provider.generateMessagesTraced({
|
|
64
|
+
messages: messagesToSend,
|
|
65
|
+
model,
|
|
66
|
+
tools: providerTools,
|
|
67
|
+
threadId,
|
|
68
|
+
onToolCall: tools.length > 0
|
|
69
|
+
? async (name, args) => {
|
|
70
|
+
const toolCall = {
|
|
71
|
+
id: `call_${Date.now()}`,
|
|
72
|
+
name,
|
|
73
|
+
args
|
|
74
|
+
};
|
|
75
|
+
callbacks?.onToolCall?.(toolCall);
|
|
76
|
+
const executed = await runTool(context, toolCall, tools);
|
|
77
|
+
const result = executed.result;
|
|
78
|
+
callbacks?.onToolResult?.(toolCall, result);
|
|
79
|
+
return typeof result === "string"
|
|
80
|
+
? result
|
|
81
|
+
: JSON.stringify(result, null, 2);
|
|
82
|
+
}
|
|
83
|
+
: undefined,
|
|
84
|
+
signal
|
|
85
|
+
});
|
|
86
|
+
// Phase 1: Stream chunks and collect tool calls
|
|
87
|
+
const pendingToolCalls = [];
|
|
88
|
+
for await (const item of stream) {
|
|
89
|
+
if (signal?.aborted)
|
|
90
|
+
break;
|
|
91
|
+
// --- Text chunk ---
|
|
92
|
+
if (isChunk(item)) {
|
|
93
|
+
const text = item.content ?? "";
|
|
94
|
+
callbacks?.onChunk?.(text);
|
|
95
|
+
assistantText += text;
|
|
96
|
+
const last = messages[messages.length - 1];
|
|
97
|
+
if (last &&
|
|
98
|
+
last.role === "assistant" &&
|
|
99
|
+
typeof last.content === "string" &&
|
|
100
|
+
!last.toolCalls?.length) {
|
|
101
|
+
last.content += text;
|
|
102
|
+
}
|
|
103
|
+
else if (!last ||
|
|
104
|
+
last.role !== "assistant" ||
|
|
105
|
+
last.toolCalls?.length) {
|
|
106
|
+
messages.push({ role: "assistant", content: text });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// --- Tool call (collect, don't execute yet) ---
|
|
110
|
+
if (isToolCall(item)) {
|
|
111
|
+
callbacks?.onToolCall?.(item);
|
|
112
|
+
pendingToolCalls.push(item);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Phase 2: Execute all collected tool calls in parallel
|
|
116
|
+
if (pendingToolCalls.length > 0) {
|
|
117
|
+
const results = await Promise.all(pendingToolCalls.map(async (tc) => {
|
|
118
|
+
const toolResult = await runTool(context, tc, tools);
|
|
119
|
+
callbacks?.onToolResult?.(tc, toolResult.result);
|
|
120
|
+
return toolResult;
|
|
121
|
+
}));
|
|
122
|
+
toolCallResults.push(...results);
|
|
123
|
+
}
|
|
124
|
+
// If tool calls were processed, consolidate into a single assistant message + tool results
|
|
125
|
+
if (toolCallResults.length > 0) {
|
|
126
|
+
// Remove the streaming text-only assistant message (if any) — we'll replace it
|
|
127
|
+
// with a consolidated assistant message that includes both text and tool calls.
|
|
128
|
+
const last = messages[messages.length - 1];
|
|
129
|
+
if (last && last.role === "assistant" && !last.toolCalls?.length) {
|
|
130
|
+
messages.pop();
|
|
131
|
+
}
|
|
132
|
+
// One assistant message with all tool calls (and any accumulated text)
|
|
133
|
+
messages.push({
|
|
134
|
+
role: "assistant",
|
|
135
|
+
content: assistantText || undefined,
|
|
136
|
+
toolCalls: toolCallResults
|
|
137
|
+
});
|
|
138
|
+
// One tool-result message per tool call
|
|
139
|
+
for (const tc of toolCallResults) {
|
|
140
|
+
messages.push({
|
|
141
|
+
role: "tool",
|
|
142
|
+
toolCallId: tc.id,
|
|
143
|
+
content: JSON.stringify(tc.result, defaultSerializer)
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
messagesToSend = messages;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return messages;
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=message-processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-processor.js","sourceRoot":"","sources":["../src/message-processor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAqBH,8EAA8E;AAC9E,cAAc;AACd,8EAA8E;AAE9E;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAC3B,OAA0B,EAC1B,QAAkB,EAClB,KAAa;IAEb,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,IAAI,CAAC,CAAC;IACzD,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,SAAS,QAAQ,CAAC,IAAI,aAAa,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC;IAE1D,OAAO;QACL,EAAE,EAAE,QAAQ,CAAC,EAAE;QACf,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,MAAM;KAC2B,CAAC;AACtC,CAAC;AAED,8EAA8E;AAC9E,uBAAuB;AACvB,8EAA8E;AAE9E,SAAS,OAAO,CAAC,IAAwB;IACvC,OAAO,MAAM,IAAI,IAAI,IAAK,IAAc,CAAC,IAAI,KAAK,OAAO,CAAC;AAC5D,CAAC;AAED,SAAS,UAAU,CAAC,IAAwB;IAC1C,OAAO,MAAM,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,IAAI,CAAC,CAAC;AAC7D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY,EA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,CAAC,IAAI,CAAC;gBACZ,IAAI,EAAE,WAAW;gBACjB,OAAO,EAAE,aAAa,IAAI,SAAS;gBACnC,SAAS,EAAE,eAAe;aAC3B,CAAC,CAAC;YAEH,wCAAwC;YACxC,KAAK,MAAM,EAAE,IAAI,eAAe,EAAE,CAAC;gBACjC,QAAQ,CAAC,IAAI,CAAC;oBACZ,IAAI,EAAE,MAAM;oBACZ,UAAU,EAAE,EAAE,CAAC,EAAE;oBACjB,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,iBAAiB,CAAC;iBACtD,CAAC,CAAC;YACL,CAAC;YAED,cAAc,GAAG,QAAQ,CAAC;QAC5B,CAAC;aAAM,CAAC;YACN,MAAM;QACR,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token counting utilities for chat messages.
|
|
3
|
+
*
|
|
4
|
+
* Port of src/nodetool/chat/token_counter.py.
|
|
5
|
+
*
|
|
6
|
+
* Since tiktoken is not available in JS, this uses a simple word-based
|
|
7
|
+
* approximation: split by whitespace and apply a ~0.75 ratio to account
|
|
8
|
+
* for sub-word tokenization.
|
|
9
|
+
*/
|
|
10
|
+
import type { Message } from "@nodetool-ai/runtime";
|
|
11
|
+
/**
|
|
12
|
+
* Approximate token count for a plain text string.
|
|
13
|
+
*
|
|
14
|
+
* Uses whitespace splitting as a proxy. Most LLM tokenizers produce roughly
|
|
15
|
+
* 1.3 tokens per whitespace-delimited word, so we multiply by 1.33 and round.
|
|
16
|
+
*/
|
|
17
|
+
export declare function countTextTokens(text: string | null | undefined): number;
|
|
18
|
+
/**
|
|
19
|
+
* Count tokens for a single Message.
|
|
20
|
+
*
|
|
21
|
+
* Handles `content` as a plain string or as an array of content parts
|
|
22
|
+
* (only text parts are counted). Also counts tokens from any tool calls.
|
|
23
|
+
*/
|
|
24
|
+
export declare function countMessageTokens(message: Message): number;
|
|
25
|
+
/**
|
|
26
|
+
* Count tokens across an array of Messages.
|
|
27
|
+
*/
|
|
28
|
+
export declare function countMessagesTokens(messages: Message[]): number;
|
|
29
|
+
//# sourceMappingURL=token-counter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-counter.d.ts","sourceRoot":"","sources":["../src/token-counter.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAY,MAAM,sBAAsB,CAAC;AAE9D;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAIvE;AAyBD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAmB3D;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,MAAM,CAM/D"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token counting utilities for chat messages.
|
|
3
|
+
*
|
|
4
|
+
* Port of src/nodetool/chat/token_counter.py.
|
|
5
|
+
*
|
|
6
|
+
* Since tiktoken is not available in JS, this uses a simple word-based
|
|
7
|
+
* approximation: split by whitespace and apply a ~0.75 ratio to account
|
|
8
|
+
* for sub-word tokenization.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Approximate token count for a plain text string.
|
|
12
|
+
*
|
|
13
|
+
* Uses whitespace splitting as a proxy. Most LLM tokenizers produce roughly
|
|
14
|
+
* 1.3 tokens per whitespace-delimited word, so we multiply by 1.33 and round.
|
|
15
|
+
*/
|
|
16
|
+
export function countTextTokens(text) {
|
|
17
|
+
if (!text)
|
|
18
|
+
return 0;
|
|
19
|
+
const words = text.split(/\s+/).filter(Boolean);
|
|
20
|
+
return Math.ceil(words.length * 1.33);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Count tokens consumed by an array of tool calls.
|
|
24
|
+
*/
|
|
25
|
+
function countToolCallsTokens(toolCalls) {
|
|
26
|
+
if (!toolCalls)
|
|
27
|
+
return 0;
|
|
28
|
+
let count = 0;
|
|
29
|
+
for (const tc of toolCalls) {
|
|
30
|
+
if (tc.name) {
|
|
31
|
+
count += countTextTokens(tc.name);
|
|
32
|
+
}
|
|
33
|
+
if (tc.args) {
|
|
34
|
+
try {
|
|
35
|
+
count += countTextTokens(JSON.stringify(tc.args));
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Be resilient to unexpected structures.
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return count;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Count tokens for a single Message.
|
|
46
|
+
*
|
|
47
|
+
* Handles `content` as a plain string or as an array of content parts
|
|
48
|
+
* (only text parts are counted). Also counts tokens from any tool calls.
|
|
49
|
+
*/
|
|
50
|
+
export function countMessageTokens(message) {
|
|
51
|
+
let tokenCount = 0;
|
|
52
|
+
const content = message.content;
|
|
53
|
+
if (content) {
|
|
54
|
+
if (typeof content === "string") {
|
|
55
|
+
tokenCount += countTextTokens(content);
|
|
56
|
+
}
|
|
57
|
+
else if (Array.isArray(content)) {
|
|
58
|
+
for (const part of content) {
|
|
59
|
+
if (part.type === "text") {
|
|
60
|
+
tokenCount += countTextTokens(part.text);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
tokenCount += countToolCallsTokens(message.toolCalls ?? null);
|
|
66
|
+
return tokenCount;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Count tokens across an array of Messages.
|
|
70
|
+
*/
|
|
71
|
+
export function countMessagesTokens(messages) {
|
|
72
|
+
let total = 0;
|
|
73
|
+
for (const msg of messages) {
|
|
74
|
+
total += countMessageTokens(msg);
|
|
75
|
+
}
|
|
76
|
+
return total;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=token-counter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-counter.js","sourceRoot":"","sources":["../src/token-counter.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,IAA+B;IAC7D,IAAI,CAAC,IAAI;QAAE,OAAO,CAAC,CAAC;IACpB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAChD,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;AACxC,CAAC;AAED;;GAEG;AACH,SAAS,oBAAoB,CAC3B,SAAwC;IAExC,IAAI,CAAC,SAAS;QAAE,OAAO,CAAC,CAAC;IACzB,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,EAAE,IAAI,SAAS,EAAE,CAAC;QAC3B,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;YACZ,KAAK,IAAI,eAAe,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;QACpC,CAAC;QACD,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;YACZ,IAAI,CAAC;gBACH,KAAK,IAAI,eAAe,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;YACpD,CAAC;YAAC,MAAM,CAAC;gBACP,yCAAyC;YAC3C,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAgB;IACjD,IAAI,UAAU,GAAG,CAAC,CAAC;IAEnB,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAChC,IAAI,OAAO,EAAE,CAAC;QACZ,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAChC,UAAU,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;QACzC,CAAC;aAAM,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YAClC,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE,CAAC;gBAC3B,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;oBACzB,UAAU,IAAI,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC3C,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,UAAU,IAAI,oBAAoB,CAAC,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC;IAE9D,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB,CAAC,QAAmB;IACrD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC3B,KAAK,IAAI,kBAAkB,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nodetool-ai/chat",
|
|
3
|
+
"version": "0.7.0-rc.11",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "node ../../scripts/build-typescript-workspace.mjs",
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"lint": "tsc --noEmit"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@nodetool-ai/protocol": "^0.7.0-rc.11",
|
|
14
|
+
"@nodetool-ai/runtime": "^0.7.0-rc.11",
|
|
15
|
+
"@nodetool-ai/agents": "^0.7.0-rc.11"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"vitest": "^4.1.2",
|
|
19
|
+
"typescript": "^5.7.2"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"nodetool-dev": "./src/index.ts",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/nodetool-ai/nodetool.git",
|
|
33
|
+
"directory": "packages/chat"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://nodetool.ai",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/nodetool-ai/nodetool/issues"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"README.md"
|
|
45
|
+
]
|
|
46
|
+
}
|