@nodetool-ai/chat 0.7.0-rc.23 → 0.8.2
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/README.md +43 -0
- package/dist/.nodetool-build-stamp +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/message-processor.d.ts +37 -6
- package/dist/message-processor.d.ts.map +1 -1
- package/dist/message-processor.js +79 -104
- package/dist/message-processor.js.map +1 -1
- package/package.json +24 -8
- package/dist/token-counter.d.ts +0 -29
- package/dist/token-counter.d.ts.map +0 -1
- package/dist/token-counter.js +0 -78
- package/dist/token-counter.js.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @nodetool-ai/chat
|
|
2
|
+
|
|
3
|
+
Chat message processing for [NodeTool](https://nodetool.ai) — tool-call orchestration and streaming responses.
|
|
4
|
+
|
|
5
|
+
This package drives a chat turn end to end: it streams a provider response, runs any tool calls the model emits, loops until the turn settles, and reports progress through callbacks.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @nodetool-ai/chat
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Exported symbols
|
|
14
|
+
|
|
15
|
+
| Symbol | Kind | Description |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| `processChat` | function | Runs a full chat turn: stream, execute tool calls, loop to completion |
|
|
18
|
+
| `runTool` | function | Executes a single tool call against a tool list |
|
|
19
|
+
| `ChatCallbacks` | interface | Hooks for chunks, tool calls, tool results, and session updates |
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { processChat } from "@nodetool-ai/chat";
|
|
25
|
+
|
|
26
|
+
await processChat({
|
|
27
|
+
userInput: "Summarize the attached report",
|
|
28
|
+
messages,
|
|
29
|
+
model: "claude-sonnet-5",
|
|
30
|
+
provider,
|
|
31
|
+
context,
|
|
32
|
+
tools,
|
|
33
|
+
callbacks: {
|
|
34
|
+
onChunk: (text) => process.stdout.write(text),
|
|
35
|
+
onToolCall: (call) => console.log("tool:", call.name)
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Links
|
|
41
|
+
|
|
42
|
+
- [NodeTool](https://nodetool.ai)
|
|
43
|
+
- [GitHub](https://github.com/nodetool-ai/nodetool)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2026-10-07T23:09:49.201Z
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @nodetool-ai/chat -- Chat processing with streaming and tool calling.
|
|
3
3
|
*/
|
|
4
|
-
export { countTextTokens, countMessageTokens, countMessagesTokens } from "./token-counter.js";
|
|
5
4
|
export { runTool, processChat } from "./message-processor.js";
|
|
6
5
|
export type { ChatCallbacks } from "./message-processor.js";
|
|
7
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC9D,YAAY,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @nodetool-ai/chat -- Chat processing with streaming and tool calling.
|
|
3
3
|
*/
|
|
4
|
-
export { countTextTokens, countMessageTokens, countMessagesTokens } from "./token-counter.js";
|
|
5
4
|
export { runTool, processChat } from "./message-processor.js";
|
|
6
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC"}
|
|
@@ -4,26 +4,44 @@
|
|
|
4
4
|
* Port of src/nodetool/chat/regular_chat.py (process_regular_chat).
|
|
5
5
|
*/
|
|
6
6
|
import type { BaseProvider } from "@nodetool-ai/runtime";
|
|
7
|
-
import type { Message, ToolCall } from "@nodetool-ai/runtime";
|
|
7
|
+
import type { Message, ToolCall, ProviderSession, RunBudget, TurnBudget } from "@nodetool-ai/runtime";
|
|
8
8
|
import type { ProcessingContext } from "@nodetool-ai/runtime";
|
|
9
|
-
import
|
|
9
|
+
import { Tool } from "@nodetool-ai/agents";
|
|
10
|
+
/**
|
|
11
|
+
* A tool call together with what running it produced. The provider-facing
|
|
12
|
+
* `ToolCall` describes a call the model asked for and has no room for an
|
|
13
|
+
* answer, so the executed form is its own type rather than an intersection
|
|
14
|
+
* asserted onto the call at each use.
|
|
15
|
+
*/
|
|
16
|
+
export interface ExecutedToolCall extends ToolCall {
|
|
17
|
+
result: unknown;
|
|
18
|
+
}
|
|
10
19
|
export interface ChatCallbacks {
|
|
11
20
|
/** Called for each text chunk streamed from the provider. */
|
|
12
21
|
onChunk?: (text: string) => void;
|
|
13
22
|
/** Called when a tool call is received from the provider. */
|
|
14
23
|
onToolCall?: (toolCall: ToolCall) => void;
|
|
15
|
-
/** Called after a tool has been executed. */
|
|
16
|
-
onToolResult?: (toolCall: ToolCall, result:
|
|
24
|
+
/** Called after a tool has been executed, with that call's result. */
|
|
25
|
+
onToolResult?: (toolCall: ToolCall, result: ExecutedToolCall["result"]) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Called when the provider emits a session-continuity update. The caller
|
|
28
|
+
* persists the token onto the assistant message so the next turn can resume.
|
|
29
|
+
*/
|
|
30
|
+
onProviderSession?: (session: ProviderSession) => void;
|
|
17
31
|
}
|
|
18
32
|
/**
|
|
19
33
|
* Find and execute a tool by name, returning the ToolCall updated with the result.
|
|
20
34
|
*/
|
|
21
|
-
export declare function runTool(context: ProcessingContext, toolCall: ToolCall, tools: Tool[]): Promise<
|
|
35
|
+
export declare function runTool(context: ProcessingContext, toolCall: ToolCall, tools: Tool[]): Promise<ExecutedToolCall>;
|
|
22
36
|
/**
|
|
23
37
|
* Serializer that handles objects with a `toJSON` method or falls back to
|
|
24
38
|
* stringification, similar to the Python `default_serializer`.
|
|
25
39
|
*/
|
|
26
|
-
|
|
40
|
+
/** A value `JSON.stringify` can emit directly. */
|
|
41
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
42
|
+
[key: string]: JsonValue;
|
|
43
|
+
};
|
|
44
|
+
export declare function defaultSerializer<T>(_key: string, value: T): T | JsonValue;
|
|
27
45
|
/**
|
|
28
46
|
* Process a user message through the provider with streaming and tool calling.
|
|
29
47
|
*
|
|
@@ -43,6 +61,12 @@ export declare function processChat(opts: {
|
|
|
43
61
|
tools?: Tool[];
|
|
44
62
|
callbacks?: ChatCallbacks;
|
|
45
63
|
threadId?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Opaque continuation token from a prior turn (read off the last assistant
|
|
66
|
+
* message). Threaded straight through to the provider, which resumes from it
|
|
67
|
+
* and sends only the new turn; stateless providers ignore it.
|
|
68
|
+
*/
|
|
69
|
+
providerSession?: ProviderSession | null;
|
|
46
70
|
signal?: AbortSignal;
|
|
47
71
|
/**
|
|
48
72
|
* Cap on tool-calling rounds before we stop and let the user intervene.
|
|
@@ -51,5 +75,12 @@ export declare function processChat(opts: {
|
|
|
51
75
|
* invalid tool calls and gets the same error back. Defaults to 25.
|
|
52
76
|
*/
|
|
53
77
|
maxIterations?: number;
|
|
78
|
+
/**
|
|
79
|
+
* The run's spend/deadline/turn admission, consulted before every model
|
|
80
|
+
* turn. Passed straight to `generateLoop`, which refuses a turn that would
|
|
81
|
+
* cross a ceiling instead of making the call. Absent means unbudgeted.
|
|
82
|
+
*/
|
|
83
|
+
turnBudget?: TurnBudget | RunBudget;
|
|
54
84
|
}): Promise<Message[]>;
|
|
85
|
+
export {};
|
|
55
86
|
//# sourceMappingURL=message-processor.d.ts.map
|
|
@@ -1 +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,
|
|
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,EACV,OAAO,EAEP,QAAQ,EACR,eAAe,EACf,SAAS,EACT,UAAU,EACX,MAAM,sBAAsB,CAAC;AAU9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EACL,IAAI,EAIL,MAAM,qBAAqB,CAAC;AAM7B;;;;;GAKG;AACH,MAAM,WAAW,gBAAiB,SAAQ,QAAQ;IAChD,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,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,sEAAsE;IACtE,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAA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4
4
|
* Port of src/nodetool/chat/regular_chat.py (process_regular_chat).
|
|
5
5
|
*/
|
|
6
|
+
import { ACTIVE_MODEL_CONTEXT_KEY, isChunk, isProviderSessionUpdate, isProviderMessageEvent, isToolCall, mediaResolverFor } from "@nodetool-ai/runtime";
|
|
7
|
+
import { Tool, extractInjectableImages, stripImagePayload, truncateToolResult } from "@nodetool-ai/agents";
|
|
6
8
|
// ---------------------------------------------------------------------------
|
|
7
9
|
// Tool runner
|
|
8
10
|
// ---------------------------------------------------------------------------
|
|
@@ -14,7 +16,7 @@ export async function runTool(context, toolCall, tools) {
|
|
|
14
16
|
if (!tool) {
|
|
15
17
|
throw new Error(`Tool "${toolCall.name}" not found`);
|
|
16
18
|
}
|
|
17
|
-
const result = await tool.process(context, toolCall.args);
|
|
19
|
+
const result = await tool.process(context, Tool.stripMessage(toolCall.args));
|
|
18
20
|
return {
|
|
19
21
|
id: toolCall.id,
|
|
20
22
|
name: toolCall.name,
|
|
@@ -25,21 +27,20 @@ export async function runTool(context, toolCall, tools) {
|
|
|
25
27
|
// ---------------------------------------------------------------------------
|
|
26
28
|
// Chat processing loop
|
|
27
29
|
// ---------------------------------------------------------------------------
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
function isToolCall(item) {
|
|
32
|
-
return "name" in item && "id" in item && !("type" in item);
|
|
30
|
+
/** A tool result that is already plain text, so it needs no JSON encoding. */
|
|
31
|
+
function isTextResult(result) {
|
|
32
|
+
return typeof result === "string";
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
35
|
+
* Whether a value implements the `JSON.stringify` hook. By that protocol's
|
|
36
|
+
* contract `toJSON` returns something JSON-representable, which is what lets
|
|
37
|
+
* the serializer hand the result straight back.
|
|
37
38
|
*/
|
|
39
|
+
function hasToJson(value) {
|
|
40
|
+
return value !== null && typeof value === "object" && "toJSON" in value;
|
|
41
|
+
}
|
|
38
42
|
export function defaultSerializer(_key, value) {
|
|
39
|
-
|
|
40
|
-
return value.toJSON();
|
|
41
|
-
}
|
|
42
|
-
return value;
|
|
43
|
+
return hasToJson(value) ? value.toJSON() : value;
|
|
43
44
|
}
|
|
44
45
|
/**
|
|
45
46
|
* Process a user message through the provider with streaming and tool calling.
|
|
@@ -52,106 +53,80 @@ export function defaultSerializer(_key, value) {
|
|
|
52
53
|
* 5. When no more tool calls are pending, return the full message history.
|
|
53
54
|
*/
|
|
54
55
|
export async function processChat(opts) {
|
|
55
|
-
const { userInput, messages, model, provider, context, tools = [], callbacks, threadId, signal, maxIterations = 25 } = opts;
|
|
56
|
+
const { userInput, messages, model, provider, context, tools = [], callbacks, threadId, providerSession, signal, maxIterations = 25, turnBudget } = opts;
|
|
57
|
+
// Stamp the turn's own selection so a tool that launches another harness
|
|
58
|
+
// inherits this chat's provider/model when the call doesn't name one.
|
|
59
|
+
context.set(ACTIVE_MODEL_CONTEXT_KEY, {
|
|
60
|
+
provider: provider.provider,
|
|
61
|
+
model
|
|
62
|
+
});
|
|
56
63
|
// 1. Add user message
|
|
57
64
|
messages.push({ role: "user", content: userInput });
|
|
58
65
|
const providerTools = tools.length > 0 ? tools.map((t) => t.toProviderTool()) : undefined;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
assistantText += text;
|
|
99
|
-
const last = messages[messages.length - 1];
|
|
100
|
-
if (last &&
|
|
101
|
-
last.role === "assistant" &&
|
|
102
|
-
typeof last.content === "string" &&
|
|
103
|
-
!last.toolCalls?.length) {
|
|
104
|
-
last.content += text;
|
|
105
|
-
}
|
|
106
|
-
else if (!last ||
|
|
107
|
-
last.role !== "assistant" ||
|
|
108
|
-
last.toolCalls?.length) {
|
|
109
|
-
messages.push({ role: "assistant", content: text });
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
// --- Tool call (collect, don't execute yet) ---
|
|
113
|
-
if (isToolCall(item)) {
|
|
114
|
-
callbacks?.onToolCall?.(item);
|
|
115
|
-
pendingToolCalls.push(item);
|
|
116
|
-
}
|
|
66
|
+
// Run one tool call and return the result text to feed back to the model.
|
|
67
|
+
// Owns tool resolution + the onToolResult callback; the provider's loop
|
|
68
|
+
// orchestrates the rounds and assembles the messages.
|
|
69
|
+
const executeTool = async (toolCall) => {
|
|
70
|
+
const executed = await runTool(context, toolCall, tools);
|
|
71
|
+
callbacks?.onToolResult?.(toolCall, executed.result);
|
|
72
|
+
// A view-image-style result carries pixels the model asked for. Forward
|
|
73
|
+
// them as image content beside a light textual note; the base64 never
|
|
74
|
+
// enters the tool-result text.
|
|
75
|
+
const injected = extractInjectableImages(executed.result);
|
|
76
|
+
const value = injected
|
|
77
|
+
? stripImagePayload(executed.result)
|
|
78
|
+
: executed.result;
|
|
79
|
+
const text = truncateToolResult(isTextResult(value)
|
|
80
|
+
? value
|
|
81
|
+
: (JSON.stringify(value, defaultSerializer) ?? ""));
|
|
82
|
+
return injected ? [{ type: "text", text }, ...injected.images] : text;
|
|
83
|
+
};
|
|
84
|
+
// The provider owns the agent loop now. It runs on its own copy of the
|
|
85
|
+
// messages, so we collect the finalized assistant/tool messages it emits
|
|
86
|
+
// into `messages`
|
|
87
|
+
// — keeping the memory prefix out of the returned, persisted history.
|
|
88
|
+
for await (const item of provider.generateLoop({
|
|
89
|
+
messages: messages,
|
|
90
|
+
model,
|
|
91
|
+
tools: providerTools,
|
|
92
|
+
threadId,
|
|
93
|
+
providerSession,
|
|
94
|
+
executeTool: tools.length > 0 ? executeTool : undefined,
|
|
95
|
+
maxIterations,
|
|
96
|
+
turnBudget,
|
|
97
|
+
resolveMedia: mediaResolverFor(context),
|
|
98
|
+
signal
|
|
99
|
+
})) {
|
|
100
|
+
if (signal?.aborted)
|
|
101
|
+
break;
|
|
102
|
+
if (isProviderSessionUpdate(item)) {
|
|
103
|
+
callbacks?.onProviderSession?.(item.session);
|
|
104
|
+
continue;
|
|
117
105
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
toolCallResults.push(...results);
|
|
106
|
+
if (isProviderMessageEvent(item)) {
|
|
107
|
+
const m = item.message;
|
|
108
|
+
// Drop a contentless, tool-less assistant turn (e.g. thinking-only).
|
|
109
|
+
if (m.role === "assistant" && !m.content && !m.toolCalls?.length)
|
|
110
|
+
continue;
|
|
111
|
+
messages.push(m);
|
|
112
|
+
continue;
|
|
126
113
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
// with a consolidated assistant message that includes both text and tool calls.
|
|
131
|
-
const last = messages[messages.length - 1];
|
|
132
|
-
if (last && last.role === "assistant" && !last.toolCalls?.length) {
|
|
133
|
-
messages.pop();
|
|
134
|
-
}
|
|
135
|
-
// One assistant message with all tool calls (and any accumulated text)
|
|
136
|
-
messages.push({
|
|
137
|
-
role: "assistant",
|
|
138
|
-
content: assistantText || undefined,
|
|
139
|
-
toolCalls: toolCallResults
|
|
140
|
-
});
|
|
141
|
-
// One tool-result message per tool call
|
|
142
|
-
for (const tc of toolCallResults) {
|
|
143
|
-
messages.push({
|
|
144
|
-
role: "tool",
|
|
145
|
-
toolCallId: tc.id,
|
|
146
|
-
content: JSON.stringify(tc.result, defaultSerializer)
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
messagesToSend = messages;
|
|
114
|
+
if (isToolCall(item)) {
|
|
115
|
+
callbacks?.onToolCall?.(item);
|
|
116
|
+
continue;
|
|
150
117
|
}
|
|
151
|
-
|
|
152
|
-
|
|
118
|
+
if (isChunk(item)) {
|
|
119
|
+
if (item.thinking)
|
|
120
|
+
continue;
|
|
121
|
+
// `isChunk` already excludes raw audio samples; the check narrows
|
|
122
|
+
// `content` to the string `onChunk` takes.
|
|
123
|
+
if (item.content instanceof Float32Array)
|
|
124
|
+
continue;
|
|
125
|
+
callbacks?.onChunk?.(item.content);
|
|
153
126
|
}
|
|
154
127
|
}
|
|
128
|
+
// Mine the completed turn for new long-term memories.
|
|
129
|
+
//
|
|
155
130
|
return messages;
|
|
156
131
|
}
|
|
157
132
|
//# sourceMappingURL=message-processor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-processor.js","sourceRoot":"","sources":["../src/message-processor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"message-processor.js","sourceRoot":"","sources":["../src/message-processor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAWH,OAAO,EACL,wBAAwB,EACxB,OAAO,EACP,uBAAuB,EACvB,sBAAsB,EACtB,UAAU,EACV,gBAAgB,EAEjB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EACL,IAAI,EACJ,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,qBAAqB,CAAC;AA8B7B,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,M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,CAAC;IACH,CAAC;IAED,sDAAsD;IACtD,EAAE;IACF,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nodetool-ai/chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=22.0.0"
|
|
7
|
+
},
|
|
5
8
|
"main": "dist/index.js",
|
|
6
9
|
"types": "dist/index.d.ts",
|
|
7
10
|
"scripts": {
|
|
8
11
|
"build": "node ../../scripts/build-typescript-workspace.mjs",
|
|
9
|
-
"test": "vitest run",
|
|
10
|
-
"lint": "tsc --noEmit"
|
|
12
|
+
"test": "node ../../scripts/run-vitest.mjs run",
|
|
13
|
+
"lint": "node ../../scripts/run-tsc.mjs --noEmit"
|
|
11
14
|
},
|
|
12
15
|
"dependencies": {
|
|
13
|
-
"@nodetool-ai/protocol": "^0.
|
|
14
|
-
"@nodetool-ai/runtime": "^0.
|
|
15
|
-
"@nodetool-ai/agents": "^0.
|
|
16
|
+
"@nodetool-ai/protocol": "^0.8.2",
|
|
17
|
+
"@nodetool-ai/runtime": "^0.8.2",
|
|
18
|
+
"@nodetool-ai/agents": "^0.8.2"
|
|
16
19
|
},
|
|
17
20
|
"devDependencies": {
|
|
18
|
-
"vitest": "^
|
|
19
|
-
"typescript": "
|
|
21
|
+
"vitest": "^5.0.1",
|
|
22
|
+
"typescript": "npm:@typescript/typescript6@6.0.2"
|
|
20
23
|
},
|
|
21
24
|
"exports": {
|
|
22
25
|
".": {
|
|
@@ -42,5 +45,18 @@
|
|
|
42
45
|
"files": [
|
|
43
46
|
"dist",
|
|
44
47
|
"README.md"
|
|
48
|
+
],
|
|
49
|
+
"description": "Chat message processing for NodeTool — tool-call orchestration, streaming responses, and token counting",
|
|
50
|
+
"keywords": [
|
|
51
|
+
"nodetool",
|
|
52
|
+
"ai",
|
|
53
|
+
"workflow",
|
|
54
|
+
"workflow-automation",
|
|
55
|
+
"typescript",
|
|
56
|
+
"chat",
|
|
57
|
+
"llm-chat",
|
|
58
|
+
"streaming",
|
|
59
|
+
"token-counting",
|
|
60
|
+
"tool-calls"
|
|
45
61
|
]
|
|
46
62
|
}
|
package/dist/token-counter.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/token-counter.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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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