@node-llm/core 1.5.2 → 1.6.0
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 +84 -110
- package/dist/aliases.js +239 -239
- package/dist/chat/Chat.d.ts +18 -17
- package/dist/chat/Chat.d.ts.map +1 -1
- package/dist/chat/Chat.js +63 -42
- package/dist/chat/ChatOptions.d.ts +8 -9
- package/dist/chat/ChatOptions.d.ts.map +1 -1
- package/dist/chat/ChatResponse.d.ts +1 -1
- package/dist/chat/ChatResponse.d.ts.map +1 -1
- package/dist/chat/ChatResponse.js +22 -8
- package/dist/chat/ChatStream.d.ts.map +1 -1
- package/dist/chat/ChatStream.js +16 -22
- package/dist/chat/Content.d.ts +3 -3
- package/dist/chat/Content.d.ts.map +1 -1
- package/dist/chat/Content.js +3 -6
- package/dist/chat/Message.d.ts +3 -1
- package/dist/chat/Message.d.ts.map +1 -1
- package/dist/chat/Role.d.ts.map +1 -1
- package/dist/chat/Tool.d.ts +15 -15
- package/dist/chat/Tool.d.ts.map +1 -1
- package/dist/chat/Tool.js +9 -7
- package/dist/chat/ToolHandler.d.ts +4 -3
- package/dist/chat/ToolHandler.d.ts.map +1 -1
- package/dist/chat/ToolHandler.js +10 -14
- package/dist/chat/Validation.d.ts.map +1 -1
- package/dist/chat/Validation.js +13 -6
- package/dist/config.d.ts +4 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +80 -25
- package/dist/constants.js +1 -1
- package/dist/errors/index.d.ts +21 -7
- package/dist/errors/index.d.ts.map +1 -1
- package/dist/errors/index.js +14 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/llm.d.ts +44 -46
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +206 -134
- package/dist/model_aliases.d.ts.map +1 -1
- package/dist/models/ModelRegistry.d.ts.map +1 -1
- package/dist/models/ModelRegistry.js +12 -10
- package/dist/models/PricingRegistry.d.ts +31 -0
- package/dist/models/PricingRegistry.d.ts.map +1 -0
- package/dist/models/PricingRegistry.js +101 -0
- package/dist/models/models.d.ts.map +1 -1
- package/dist/models/models.js +5954 -7352
- package/dist/models/types.d.ts +37 -34
- package/dist/models/types.d.ts.map +1 -1
- package/dist/moderation/Moderation.d.ts.map +1 -1
- package/dist/moderation/Moderation.js +15 -5
- package/dist/providers/BaseProvider.d.ts +12 -8
- package/dist/providers/BaseProvider.d.ts.map +1 -1
- package/dist/providers/BaseProvider.js +17 -7
- package/dist/providers/Provider.d.ts +20 -5
- package/dist/providers/Provider.d.ts.map +1 -1
- package/dist/providers/anthropic/AnthropicProvider.d.ts +1 -1
- package/dist/providers/anthropic/AnthropicProvider.d.ts.map +1 -1
- package/dist/providers/anthropic/AnthropicProvider.js +3 -3
- package/dist/providers/anthropic/Capabilities.d.ts +2 -1
- package/dist/providers/anthropic/Capabilities.d.ts.map +1 -1
- package/dist/providers/anthropic/Capabilities.js +3 -20
- package/dist/providers/anthropic/Chat.d.ts.map +1 -1
- package/dist/providers/anthropic/Chat.js +27 -17
- package/dist/providers/anthropic/Errors.d.ts.map +1 -1
- package/dist/providers/anthropic/Errors.js +5 -2
- package/dist/providers/anthropic/Models.d.ts.map +1 -1
- package/dist/providers/anthropic/Models.js +6 -6
- package/dist/providers/anthropic/Streaming.d.ts.map +1 -1
- package/dist/providers/anthropic/Streaming.js +17 -12
- package/dist/providers/anthropic/Utils.js +8 -5
- package/dist/providers/anthropic/index.d.ts.map +1 -1
- package/dist/providers/anthropic/index.js +4 -3
- package/dist/providers/anthropic/types.d.ts +11 -4
- package/dist/providers/anthropic/types.d.ts.map +1 -1
- package/dist/providers/deepseek/Capabilities.d.ts +7 -5
- package/dist/providers/deepseek/Capabilities.d.ts.map +1 -1
- package/dist/providers/deepseek/Capabilities.js +9 -5
- package/dist/providers/deepseek/Chat.d.ts.map +1 -1
- package/dist/providers/deepseek/Chat.js +10 -9
- package/dist/providers/deepseek/DeepSeekProvider.d.ts +1 -1
- package/dist/providers/deepseek/DeepSeekProvider.d.ts.map +1 -1
- package/dist/providers/deepseek/DeepSeekProvider.js +4 -4
- package/dist/providers/deepseek/Models.d.ts.map +1 -1
- package/dist/providers/deepseek/Models.js +7 -7
- package/dist/providers/deepseek/Streaming.d.ts.map +1 -1
- package/dist/providers/deepseek/Streaming.js +11 -8
- package/dist/providers/deepseek/index.d.ts.map +1 -1
- package/dist/providers/deepseek/index.js +5 -4
- package/dist/providers/gemini/Capabilities.d.ts +5 -33
- package/dist/providers/gemini/Capabilities.d.ts.map +1 -1
- package/dist/providers/gemini/Capabilities.js +7 -30
- package/dist/providers/gemini/Chat.d.ts.map +1 -1
- package/dist/providers/gemini/Chat.js +24 -19
- package/dist/providers/gemini/ChatUtils.d.ts.map +1 -1
- package/dist/providers/gemini/ChatUtils.js +10 -10
- package/dist/providers/gemini/Embeddings.d.ts.map +1 -1
- package/dist/providers/gemini/Embeddings.js +2 -2
- package/dist/providers/gemini/Errors.d.ts.map +1 -1
- package/dist/providers/gemini/Errors.js +5 -2
- package/dist/providers/gemini/GeminiProvider.d.ts +1 -1
- package/dist/providers/gemini/GeminiProvider.d.ts.map +1 -1
- package/dist/providers/gemini/GeminiProvider.js +3 -3
- package/dist/providers/gemini/Image.d.ts.map +1 -1
- package/dist/providers/gemini/Image.js +8 -8
- package/dist/providers/gemini/Models.d.ts.map +1 -1
- package/dist/providers/gemini/Models.js +6 -6
- package/dist/providers/gemini/Streaming.d.ts.map +1 -1
- package/dist/providers/gemini/Streaming.js +18 -14
- package/dist/providers/gemini/Transcription.d.ts.map +1 -1
- package/dist/providers/gemini/Transcription.js +11 -11
- package/dist/providers/gemini/index.d.ts +1 -1
- package/dist/providers/gemini/index.d.ts.map +1 -1
- package/dist/providers/gemini/index.js +5 -4
- package/dist/providers/gemini/types.d.ts +4 -4
- package/dist/providers/gemini/types.d.ts.map +1 -1
- package/dist/providers/ollama/Capabilities.d.ts.map +1 -1
- package/dist/providers/ollama/Capabilities.js +6 -2
- package/dist/providers/ollama/Models.js +1 -1
- package/dist/providers/ollama/OllamaProvider.d.ts +1 -1
- package/dist/providers/ollama/OllamaProvider.d.ts.map +1 -1
- package/dist/providers/ollama/OllamaProvider.js +2 -2
- package/dist/providers/ollama/index.d.ts +1 -1
- package/dist/providers/ollama/index.d.ts.map +1 -1
- package/dist/providers/ollama/index.js +7 -3
- package/dist/providers/openai/Capabilities.d.ts +2 -1
- package/dist/providers/openai/Capabilities.d.ts.map +1 -1
- package/dist/providers/openai/Capabilities.js +9 -21
- package/dist/providers/openai/Chat.d.ts.map +1 -1
- package/dist/providers/openai/Chat.js +18 -14
- package/dist/providers/openai/Embedding.d.ts.map +1 -1
- package/dist/providers/openai/Embedding.js +11 -7
- package/dist/providers/openai/Errors.d.ts.map +1 -1
- package/dist/providers/openai/Errors.js +5 -2
- package/dist/providers/openai/Image.d.ts.map +1 -1
- package/dist/providers/openai/Image.js +6 -6
- package/dist/providers/openai/Models.d.ts +1 -1
- package/dist/providers/openai/Models.d.ts.map +1 -1
- package/dist/providers/openai/Models.js +12 -8
- package/dist/providers/openai/Moderation.d.ts.map +1 -1
- package/dist/providers/openai/Moderation.js +6 -6
- package/dist/providers/openai/OpenAIProvider.d.ts +2 -3
- package/dist/providers/openai/OpenAIProvider.d.ts.map +1 -1
- package/dist/providers/openai/OpenAIProvider.js +4 -4
- package/dist/providers/openai/Streaming.d.ts.map +1 -1
- package/dist/providers/openai/Streaming.js +18 -13
- package/dist/providers/openai/Transcription.d.ts.map +1 -1
- package/dist/providers/openai/Transcription.js +15 -12
- package/dist/providers/openai/index.d.ts +1 -1
- package/dist/providers/openai/index.d.ts.map +1 -1
- package/dist/providers/openai/index.js +6 -5
- package/dist/providers/openai/types.d.ts +1 -1
- package/dist/providers/openai/utils.js +2 -2
- package/dist/providers/openrouter/Capabilities.d.ts +3 -3
- package/dist/providers/openrouter/Capabilities.d.ts.map +1 -1
- package/dist/providers/openrouter/Capabilities.js +21 -24
- package/dist/providers/openrouter/Models.d.ts.map +1 -1
- package/dist/providers/openrouter/Models.js +20 -16
- package/dist/providers/openrouter/OpenRouterProvider.d.ts.map +1 -1
- package/dist/providers/openrouter/OpenRouterProvider.js +1 -1
- package/dist/providers/openrouter/index.d.ts +1 -1
- package/dist/providers/openrouter/index.d.ts.map +1 -1
- package/dist/providers/openrouter/index.js +6 -5
- package/dist/providers/registry.d.ts +18 -2
- package/dist/providers/registry.d.ts.map +1 -1
- package/dist/providers/registry.js +17 -2
- package/dist/providers/utils.js +1 -1
- package/dist/schema/Schema.d.ts +3 -3
- package/dist/schema/Schema.d.ts.map +1 -1
- package/dist/schema/Schema.js +2 -2
- package/dist/schema/to-json-schema.d.ts +1 -1
- package/dist/schema/to-json-schema.d.ts.map +1 -1
- package/dist/streaming/Stream.d.ts.map +1 -1
- package/dist/streaming/Stream.js +3 -3
- package/dist/utils/Binary.d.ts.map +1 -1
- package/dist/utils/Binary.js +32 -23
- package/dist/utils/FileLoader.d.ts.map +1 -1
- package/dist/utils/FileLoader.js +25 -4
- package/dist/utils/audio.js +2 -2
- package/dist/utils/fetch.d.ts.map +1 -1
- package/dist/utils/fetch.js +14 -4
- package/dist/utils/logger.d.ts +3 -3
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +2 -2
- package/package.json +1 -1
package/dist/chat/Chat.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export interface AskOptions {
|
|
|
15
15
|
headers?: Record<string, string>;
|
|
16
16
|
maxToolCalls?: number;
|
|
17
17
|
requestTimeout?: number;
|
|
18
|
+
signal?: AbortSignal;
|
|
18
19
|
}
|
|
19
20
|
import { ChatResponseString } from "./ChatResponse.js";
|
|
20
21
|
export declare class Chat {
|
|
@@ -43,13 +44,13 @@ export declare class Chat {
|
|
|
43
44
|
*/
|
|
44
45
|
withTool(tool: ToolResolvable): this;
|
|
45
46
|
/**
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
* Add multiple tools to the chat session.
|
|
48
|
+
* Supports passing Tool classes (which will be instantiated) or instances.
|
|
49
|
+
* Can replace existing tools if options.replace is true.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* chat.withTools([WeatherTool, new CalculatorTool()], { replace: true });
|
|
53
|
+
*/
|
|
53
54
|
withTools(tools: ToolResolvable[], options?: {
|
|
54
55
|
replace?: boolean;
|
|
55
56
|
}): this;
|
|
@@ -82,31 +83,31 @@ export declare class Chat {
|
|
|
82
83
|
*/
|
|
83
84
|
withRequestOptions(options: {
|
|
84
85
|
headers?: Record<string, string>;
|
|
85
|
-
responseFormat?:
|
|
86
|
+
responseFormat?: unknown;
|
|
86
87
|
}): this;
|
|
87
88
|
/**
|
|
88
89
|
* Set provider-specific parameters.
|
|
89
90
|
* These will be merged into the final request payload.
|
|
90
91
|
*/
|
|
91
|
-
withParams(params: Record<string,
|
|
92
|
+
withParams(params: Record<string, unknown>): this;
|
|
92
93
|
/**
|
|
93
94
|
* Enforce a specific schema for the output.
|
|
94
95
|
* Can accept a Schema object or a Zod schema/JSON Schema directly.
|
|
95
96
|
*/
|
|
96
|
-
withSchema(schema: Schema | z.ZodType<
|
|
97
|
+
withSchema(schema: Schema | z.ZodType<unknown> | Record<string, unknown> | null): this;
|
|
97
98
|
onNewMessage(handler: () => void): this;
|
|
98
99
|
onEndMessage(handler: (message: ChatResponseString) => void): this;
|
|
99
|
-
onToolCall(handler: (toolCall:
|
|
100
|
-
onToolResult(handler: (result:
|
|
100
|
+
onToolCall(handler: (toolCall: unknown) => void): this;
|
|
101
|
+
onToolResult(handler: (result: unknown) => void): this;
|
|
101
102
|
/**
|
|
102
103
|
* Called when a tool call starts.
|
|
103
104
|
*/
|
|
104
|
-
onToolCallStart(handler: (toolCall:
|
|
105
|
+
onToolCallStart(handler: (toolCall: unknown) => void): this;
|
|
105
106
|
/**
|
|
106
107
|
* Called when a tool call ends successfully.
|
|
107
108
|
*/
|
|
108
|
-
onToolCallEnd(handler: (toolCall:
|
|
109
|
-
onToolCallError(handler: (toolCall:
|
|
109
|
+
onToolCallEnd(handler: (toolCall: unknown, result: unknown) => void): this;
|
|
110
|
+
onToolCallError(handler: (toolCall: unknown, error: Error) => "STOP" | "CONTINUE" | "RETRY" | void | Promise<"STOP" | "CONTINUE" | "RETRY" | void>): this;
|
|
110
111
|
/**
|
|
111
112
|
* Set the tool execution mode.
|
|
112
113
|
* - "auto": (Default) Automatically execute all tool calls.
|
|
@@ -118,7 +119,7 @@ export declare class Chat {
|
|
|
118
119
|
* Hook for confirming tool execution in "confirm" mode.
|
|
119
120
|
* Return true to proceed, false to cancel the specific call.
|
|
120
121
|
*/
|
|
121
|
-
onConfirmToolCall(handler: (toolCall:
|
|
122
|
+
onConfirmToolCall(handler: (toolCall: unknown) => Promise<boolean> | boolean): this;
|
|
122
123
|
/**
|
|
123
124
|
* Add a hook to process messages before sending to the LLM.
|
|
124
125
|
* Useful for PII detection, redaction, and input moderation.
|
|
@@ -132,7 +133,7 @@ export declare class Chat {
|
|
|
132
133
|
/**
|
|
133
134
|
* Ask the model a question
|
|
134
135
|
*/
|
|
135
|
-
ask(content: string |
|
|
136
|
+
ask(content: string | ContentPart[], options?: AskOptions): Promise<ChatResponseString>;
|
|
136
137
|
/**
|
|
137
138
|
* Streams the model's response to a user question.
|
|
138
139
|
*/
|
package/dist/chat/Chat.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../src/chat/Chat.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../src/chat/Chat.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,WAAW,EAA4D,MAAM,cA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G,WAAW,EAAE,EAAE,OAAO,GAAE,UAAe,GAAG,MAAM,CAAC,SAAS,CAAC;IAWpF;;OAEG;IACH,OAAO,CAAC,aAAa;CAiDtB"}
|
package/dist/chat/Chat.js
CHANGED
|
@@ -10,6 +10,7 @@ import { ToolExecutionMode } from "../constants.js";
|
|
|
10
10
|
import { ConfigurationError } from "../errors/index.js";
|
|
11
11
|
import { ChatValidator } from "./Validation.js";
|
|
12
12
|
import { ToolHandler } from "./ToolHandler.js";
|
|
13
|
+
import { logger } from "../utils/logger.js";
|
|
13
14
|
import { ChatResponseString } from "./ChatResponse.js";
|
|
14
15
|
export class Chat {
|
|
15
16
|
provider;
|
|
@@ -64,11 +65,19 @@ export class Chat {
|
|
|
64
65
|
acc.output_tokens += msg.usage.output_tokens;
|
|
65
66
|
acc.total_tokens += msg.usage.total_tokens;
|
|
66
67
|
acc.cached_tokens = (acc.cached_tokens ?? 0) + (msg.usage.cached_tokens ?? 0);
|
|
67
|
-
acc.cache_creation_tokens =
|
|
68
|
+
acc.cache_creation_tokens =
|
|
69
|
+
(acc.cache_creation_tokens ?? 0) + (msg.usage.cache_creation_tokens ?? 0);
|
|
68
70
|
acc.cost = (acc.cost ?? 0) + (msg.usage.cost ?? 0);
|
|
69
71
|
}
|
|
70
72
|
return acc;
|
|
71
|
-
}, {
|
|
73
|
+
}, {
|
|
74
|
+
input_tokens: 0,
|
|
75
|
+
output_tokens: 0,
|
|
76
|
+
total_tokens: 0,
|
|
77
|
+
cached_tokens: 0,
|
|
78
|
+
cache_creation_tokens: 0,
|
|
79
|
+
cost: 0
|
|
80
|
+
});
|
|
72
81
|
}
|
|
73
82
|
/**
|
|
74
83
|
* Add a tool to the chat session (fluent API)
|
|
@@ -78,13 +87,13 @@ export class Chat {
|
|
|
78
87
|
return this.withTools([tool]);
|
|
79
88
|
}
|
|
80
89
|
/**
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
* Add multiple tools to the chat session.
|
|
91
|
+
* Supports passing Tool classes (which will be instantiated) or instances.
|
|
92
|
+
* Can replace existing tools if options.replace is true.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* chat.withTools([WeatherTool, new CalculatorTool()], { replace: true });
|
|
96
|
+
*/
|
|
88
97
|
withTools(tools, options) {
|
|
89
98
|
if (options?.replace) {
|
|
90
99
|
this.options.tools = [];
|
|
@@ -259,7 +268,7 @@ export class Chat {
|
|
|
259
268
|
}
|
|
260
269
|
this.messages.push({
|
|
261
270
|
role: "user",
|
|
262
|
-
content: messageContent
|
|
271
|
+
content: messageContent
|
|
263
272
|
});
|
|
264
273
|
// Process Schema/Structured Output
|
|
265
274
|
let responseFormat = this.options.responseFormat;
|
|
@@ -272,7 +281,7 @@ export class Chat {
|
|
|
272
281
|
name: this.options.schema.definition.name,
|
|
273
282
|
description: this.options.schema.definition.description,
|
|
274
283
|
strict: this.options.schema.definition.strict ?? true,
|
|
275
|
-
schema: jsonSchema
|
|
284
|
+
schema: jsonSchema
|
|
276
285
|
}
|
|
277
286
|
};
|
|
278
287
|
}
|
|
@@ -285,7 +294,8 @@ export class Chat {
|
|
|
285
294
|
headers: { ...this.options.headers, ...options?.headers },
|
|
286
295
|
response_format: responseFormat, // Pass to provider
|
|
287
296
|
requestTimeout: options?.requestTimeout ?? this.options.requestTimeout ?? config.requestTimeout,
|
|
288
|
-
|
|
297
|
+
signal: options?.signal,
|
|
298
|
+
...this.options.params
|
|
289
299
|
};
|
|
290
300
|
// --- Content Policy Hooks (Input) ---
|
|
291
301
|
if (this.options.onBeforeRequest) {
|
|
@@ -296,7 +306,7 @@ export class Chat {
|
|
|
296
306
|
executeOptions.messages = result;
|
|
297
307
|
}
|
|
298
308
|
}
|
|
299
|
-
|
|
309
|
+
const totalUsage = { input_tokens: 0, output_tokens: 0, total_tokens: 0 };
|
|
300
310
|
const trackUsage = (u) => {
|
|
301
311
|
if (u) {
|
|
302
312
|
totalUsage.input_tokens += u.input_tokens;
|
|
@@ -325,9 +335,9 @@ export class Chat {
|
|
|
325
335
|
}
|
|
326
336
|
this.messages.push({
|
|
327
337
|
role: "assistant",
|
|
328
|
-
content: assistantMessage || null,
|
|
338
|
+
content: assistantMessage?.toString() || null,
|
|
329
339
|
tool_calls: response.tool_calls,
|
|
330
|
-
usage: response.usage
|
|
340
|
+
usage: response.usage
|
|
331
341
|
});
|
|
332
342
|
if (this.options.onEndMessage && (!response.tool_calls || response.tool_calls.length === 0)) {
|
|
333
343
|
this.options.onEndMessage(assistantMessage);
|
|
@@ -348,45 +358,55 @@ export class Chat {
|
|
|
348
358
|
if (this.options.toolExecution === ToolExecutionMode.CONFIRM) {
|
|
349
359
|
const approved = await ToolHandler.requestToolConfirmation(toolCall, this.options.onConfirmToolCall);
|
|
350
360
|
if (!approved) {
|
|
351
|
-
this.messages.push(
|
|
352
|
-
role: "tool",
|
|
353
|
-
tool_call_id: toolCall.id,
|
|
354
|
-
content: "Action cancelled by user.",
|
|
355
|
-
});
|
|
361
|
+
this.messages.push(this.provider.formatToolResultMessage(toolCall.id, "Action cancelled by user."));
|
|
356
362
|
continue;
|
|
357
363
|
}
|
|
358
364
|
}
|
|
359
365
|
try {
|
|
360
366
|
const toolResult = await ToolHandler.execute(toolCall, this.options.tools, this.options.onToolCallStart, this.options.onToolCallEnd);
|
|
361
|
-
this.messages.push(toolResult);
|
|
367
|
+
this.messages.push(this.provider.formatToolResultMessage(toolResult.tool_call_id, toolResult.content));
|
|
362
368
|
}
|
|
363
369
|
catch (error) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
370
|
+
let currentError = error;
|
|
371
|
+
const directive = await this.options.onToolCallError?.(toolCall, currentError);
|
|
372
|
+
if (directive === "STOP") {
|
|
373
|
+
throw currentError;
|
|
374
|
+
}
|
|
375
|
+
if (directive === "RETRY") {
|
|
376
|
+
try {
|
|
377
|
+
const toolResult = await ToolHandler.execute(toolCall, this.options.tools, this.options.onToolCallStart, this.options.onToolCallEnd);
|
|
378
|
+
this.messages.push(this.provider.formatToolResultMessage(toolResult.tool_call_id, toolResult.content));
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
catch (retryError) {
|
|
382
|
+
// If retry also fails, fall through to default logic
|
|
383
|
+
currentError = retryError;
|
|
384
|
+
}
|
|
367
385
|
}
|
|
368
|
-
this.messages.push({
|
|
369
|
-
|
|
370
|
-
tool_call_id: toolCall.id,
|
|
371
|
-
content: `Fatal error executing tool '${toolCall.function.name}': ${error.message}`,
|
|
372
|
-
});
|
|
373
|
-
if (directive === 'CONTINUE') {
|
|
386
|
+
this.messages.push(this.provider.formatToolResultMessage(toolCall.id, `Fatal error executing tool '${toolCall.function.name}': ${currentError.message}`, { isError: true }));
|
|
387
|
+
if (directive === "CONTINUE") {
|
|
374
388
|
continue;
|
|
375
389
|
}
|
|
376
390
|
// Default short-circuit logic
|
|
377
|
-
const
|
|
391
|
+
const errorObj = currentError;
|
|
392
|
+
const isFatal = errorObj.fatal === true || errorObj.status === 401 || errorObj.status === 403;
|
|
378
393
|
if (isFatal) {
|
|
379
|
-
throw
|
|
394
|
+
throw currentError;
|
|
380
395
|
}
|
|
381
|
-
|
|
396
|
+
logger.error(`Tool execution failed for '${toolCall.function.name}':`, currentError);
|
|
382
397
|
}
|
|
383
398
|
}
|
|
384
399
|
response = await this.executor.executeChat({
|
|
385
400
|
model: this.model,
|
|
386
401
|
messages: [...this.systemMessages, ...this.messages],
|
|
387
402
|
tools: this.options.tools,
|
|
403
|
+
temperature: options?.temperature ?? this.options.temperature,
|
|
404
|
+
max_tokens: options?.maxTokens ?? this.options.maxTokens ?? config.maxTokens,
|
|
388
405
|
headers: this.options.headers,
|
|
406
|
+
response_format: responseFormat,
|
|
389
407
|
requestTimeout: options?.requestTimeout ?? this.options.requestTimeout ?? config.requestTimeout,
|
|
408
|
+
signal: options?.signal,
|
|
409
|
+
...this.options.params
|
|
390
410
|
});
|
|
391
411
|
trackUsage(response.usage);
|
|
392
412
|
assistantMessage = new ChatResponseString(response.content ?? "", response.usage ?? { input_tokens: 0, output_tokens: 0, total_tokens: 0 }, this.model, this.provider.id, response.reasoning);
|
|
@@ -399,15 +419,15 @@ export class Chat {
|
|
|
399
419
|
}
|
|
400
420
|
this.messages.push({
|
|
401
421
|
role: "assistant",
|
|
402
|
-
content: assistantMessage || null,
|
|
422
|
+
content: assistantMessage?.toString() || null,
|
|
403
423
|
tool_calls: response.tool_calls,
|
|
404
|
-
usage: response.usage
|
|
424
|
+
usage: response.usage
|
|
405
425
|
});
|
|
406
426
|
if (this.options.onEndMessage && (!response.tool_calls || response.tool_calls.length === 0)) {
|
|
407
427
|
this.options.onEndMessage(assistantMessage);
|
|
408
428
|
}
|
|
409
429
|
}
|
|
410
|
-
// For the final return, we might want to aggregate reasoning too if it happened in multiple turns?
|
|
430
|
+
// For the final return, we might want to aggregate reasoning too if it happened in multiple turns?
|
|
411
431
|
// Usually reasoning only happens once or we just want the last one.
|
|
412
432
|
return new ChatResponseString(assistantMessage.toString() || "", totalUsage, this.model, this.provider.id, assistantMessage.reasoning, response.tool_calls);
|
|
413
433
|
}
|
|
@@ -429,7 +449,7 @@ export class Chat {
|
|
|
429
449
|
toolInstance = new tool();
|
|
430
450
|
}
|
|
431
451
|
catch (e) {
|
|
432
|
-
|
|
452
|
+
logger.error(`Failed to instantiate tool class: ${tool.name}`, e);
|
|
433
453
|
return null;
|
|
434
454
|
}
|
|
435
455
|
}
|
|
@@ -442,17 +462,18 @@ export class Chat {
|
|
|
442
462
|
if (typeof toolInstance.toLLMTool === "function") {
|
|
443
463
|
return toolInstance.toLLMTool();
|
|
444
464
|
}
|
|
445
|
-
|
|
465
|
+
const toolObj = toolInstance;
|
|
466
|
+
if (!toolObj.function || !toolObj.function.name) {
|
|
446
467
|
// 1. Validate structure
|
|
447
468
|
throw new ConfigurationError(`[NodeLLM] Tool validation failed: 'function.name' is required for raw tool objects.`);
|
|
448
469
|
}
|
|
449
|
-
if (
|
|
470
|
+
if (toolObj.type !== "function") {
|
|
450
471
|
// 2. Ensure 'type: function' exists (standardize for providers)
|
|
451
|
-
|
|
472
|
+
toolObj.type = "function";
|
|
452
473
|
}
|
|
453
|
-
if (typeof
|
|
474
|
+
if (typeof toolObj.handler !== "function") {
|
|
454
475
|
// 3. Validate handler existence
|
|
455
|
-
throw new ConfigurationError(`[NodeLLM] Tool validation failed: Tool '${
|
|
476
|
+
throw new ConfigurationError(`[NodeLLM] Tool validation failed: Tool '${toolObj.function.name}' must have a 'handler' function. (Note: Only Tool subclasses use 'execute()')`);
|
|
456
477
|
}
|
|
457
478
|
return toolInstance;
|
|
458
479
|
}
|
|
@@ -3,6 +3,7 @@ import { ToolResolvable } from "./Tool.js";
|
|
|
3
3
|
import { Schema } from "../schema/Schema.js";
|
|
4
4
|
import { ChatResponseString } from "./ChatResponse.js";
|
|
5
5
|
import { ToolExecutionMode } from "../constants.js";
|
|
6
|
+
import { ResponseFormat } from "../providers/Provider.js";
|
|
6
7
|
export interface ChatOptions {
|
|
7
8
|
systemPrompt?: string;
|
|
8
9
|
messages?: Message[];
|
|
@@ -10,22 +11,20 @@ export interface ChatOptions {
|
|
|
10
11
|
temperature?: number;
|
|
11
12
|
maxTokens?: number;
|
|
12
13
|
onNewMessage?: () => void;
|
|
13
|
-
onEndMessage?: (message:
|
|
14
|
-
onToolCallStart?: (toolCall:
|
|
15
|
-
onToolCallEnd?: (toolCall:
|
|
16
|
-
onToolCallError?: (toolCall:
|
|
14
|
+
onEndMessage?: (message: ChatResponseString) => void;
|
|
15
|
+
onToolCallStart?: (toolCall: unknown) => void;
|
|
16
|
+
onToolCallEnd?: (toolCall: unknown, result: unknown) => void;
|
|
17
|
+
onToolCallError?: (toolCall: unknown, error: Error) => "STOP" | "CONTINUE" | "RETRY" | void | Promise<"STOP" | "CONTINUE" | "RETRY" | void>;
|
|
17
18
|
headers?: Record<string, string>;
|
|
18
19
|
schema?: Schema;
|
|
19
|
-
responseFormat?:
|
|
20
|
-
|
|
21
|
-
};
|
|
22
|
-
params?: Record<string, any>;
|
|
20
|
+
responseFormat?: ResponseFormat;
|
|
21
|
+
params?: Record<string, unknown>;
|
|
23
22
|
assumeModelExists?: boolean;
|
|
24
23
|
provider?: string;
|
|
25
24
|
maxToolCalls?: number;
|
|
26
25
|
requestTimeout?: number;
|
|
27
26
|
toolExecution?: ToolExecutionMode;
|
|
28
|
-
onConfirmToolCall?: (toolCall:
|
|
27
|
+
onConfirmToolCall?: (toolCall: unknown) => Promise<boolean> | boolean;
|
|
29
28
|
onBeforeRequest?: (messages: Message[]) => Promise<Message[] | void>;
|
|
30
29
|
onAfterResponse?: (response: ChatResponseString) => Promise<ChatResponseString | void>;
|
|
31
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ChatOptions.d.ts","sourceRoot":"","sources":["../../src/chat/ChatOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,
|
|
1
|
+
{"version":3,"file":"ChatOptions.d.ts","sourceRoot":"","sources":["../../src/chat/ChatOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAE1D,MAAM,WAAW,WAAW;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;IACrB,KAAK,CAAC,EAAE,cAAc,EAAE,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,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"file":"ChatResponse.d.ts","sourceRoot":"","sources":["../../src/chat/ChatResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAErC;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,MAAM;aAG1B,KAAK,EAAE,KAAK;aACZ,KAAK,EAAE,MAAM;aACb,QAAQ,EAAE,MAAM;aAChB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI;aACzB,UAAU,CAAC,EAAE,QAAQ,EAAE;gBALvC,OAAO,EAAE,MAAM,EACC,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,YAAA,EACzB,UAAU,CAAC,EAAE,QAAQ,EAAE,YAAA;IAKzC,IAAI,YAAY,
|
|
1
|
+
{"version":3,"file":"ChatResponse.d.ts","sourceRoot":"","sources":["../../src/chat/ChatResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAErC;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,MAAM;aAG1B,KAAK,EAAE,KAAK;aACZ,KAAK,EAAE,MAAM;aACb,QAAQ,EAAE,MAAM;aAChB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI;aACzB,UAAU,CAAC,EAAE,QAAQ,EAAE;gBALvC,OAAO,EAAE,MAAM,EACC,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,YAAA,EACzB,UAAU,CAAC,EAAE,QAAQ,EAAE,YAAA;IAKzC,IAAI,YAAY,WAEf;IACD,IAAI,aAAa,WAEhB;IACD,IAAI,YAAY,WAEf;IACD,IAAI,aAAa,uBAEhB;IACD,IAAI,IAAI,uBAEP;IACD,IAAI,UAAU,uBAEb;IACD,IAAI,WAAW,uBAEd;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,QAAQ;IAIR;;OAEG;IACH,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,kBAAkB;IAWnD;;;OAGG;IACH,IAAI,MAAM,IAAI,OAAO,CAMpB;CACF"}
|
|
@@ -16,13 +16,27 @@ export class ChatResponseString extends String {
|
|
|
16
16
|
this.reasoning = reasoning;
|
|
17
17
|
this.tool_calls = tool_calls;
|
|
18
18
|
}
|
|
19
|
-
get input_tokens() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
get
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
get
|
|
19
|
+
get input_tokens() {
|
|
20
|
+
return this.usage.input_tokens;
|
|
21
|
+
}
|
|
22
|
+
get output_tokens() {
|
|
23
|
+
return this.usage.output_tokens;
|
|
24
|
+
}
|
|
25
|
+
get total_tokens() {
|
|
26
|
+
return this.usage.total_tokens;
|
|
27
|
+
}
|
|
28
|
+
get cached_tokens() {
|
|
29
|
+
return this.usage.cached_tokens;
|
|
30
|
+
}
|
|
31
|
+
get cost() {
|
|
32
|
+
return this.usage.cost;
|
|
33
|
+
}
|
|
34
|
+
get input_cost() {
|
|
35
|
+
return this.usage.input_cost;
|
|
36
|
+
}
|
|
37
|
+
get output_cost() {
|
|
38
|
+
return this.usage.output_cost;
|
|
39
|
+
}
|
|
26
40
|
get content() {
|
|
27
41
|
return this.valueOf();
|
|
28
42
|
}
|
|
@@ -46,7 +60,7 @@ export class ChatResponseString extends String {
|
|
|
46
60
|
try {
|
|
47
61
|
return JSON.parse(this.valueOf());
|
|
48
62
|
}
|
|
49
|
-
catch
|
|
63
|
+
catch {
|
|
50
64
|
return null;
|
|
51
65
|
}
|
|
52
66
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ChatStream.d.ts","sourceRoot":"","sources":["../../src/chat/ChatStream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ChatStream.d.ts","sourceRoot":"","sources":["../../src/chat/ChatStream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,WAAW,EAA4D,MAAM,cAAc,CAAC;AACrG,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAS,MAAM,0BAA0B,CAAC;AAEtE,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAGhD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AASvC;;;GAGG;AACH,qBAAa,UAAU;IAKnB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAN1B,OAAO,CAAC,QAAQ,CAAY;IAC5B,OAAO,CAAC,cAAc,CAAY;gBAGf,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,WAAgB,EAC1C,QAAQ,CAAC,EAAE,OAAO,EAAE,EACpB,cAAc,CAAC,EAAE,OAAO,EAAE;IA6B5B,IAAI,OAAO,IAAI,SAAS,OAAO,EAAE,CAEhC;IAED,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,EAAE,EAAE,OAAO,GAAE,UAAe,GAAG,MAAM,CAAC,SAAS,CAAC;CAsPrF"}
|
package/dist/chat/ChatStream.js
CHANGED
|
@@ -7,6 +7,7 @@ import { FileLoader } from "../utils/FileLoader.js";
|
|
|
7
7
|
import { toJsonSchema } from "../schema/to-json-schema.js";
|
|
8
8
|
import { ChatValidator } from "./Validation.js";
|
|
9
9
|
import { ToolHandler } from "./ToolHandler.js";
|
|
10
|
+
import { logger } from "../utils/logger.js";
|
|
10
11
|
/**
|
|
11
12
|
* Internal handler for chat streaming logic.
|
|
12
13
|
* Wraps the provider's stream with side effects like history updates and events.
|
|
@@ -27,7 +28,7 @@ export class ChatStream {
|
|
|
27
28
|
if (options.systemPrompt) {
|
|
28
29
|
this.systemMessages.push({
|
|
29
30
|
role: "system",
|
|
30
|
-
content: options.systemPrompt
|
|
31
|
+
content: options.systemPrompt
|
|
31
32
|
});
|
|
32
33
|
}
|
|
33
34
|
if (options.messages) {
|
|
@@ -83,7 +84,7 @@ export class ChatStream {
|
|
|
83
84
|
name: options.schema.definition.name,
|
|
84
85
|
description: options.schema.definition.description,
|
|
85
86
|
strict: options.schema.definition.strict ?? true,
|
|
86
|
-
schema: jsonSchema
|
|
87
|
+
schema: jsonSchema
|
|
87
88
|
}
|
|
88
89
|
};
|
|
89
90
|
}
|
|
@@ -93,7 +94,7 @@ export class ChatStream {
|
|
|
93
94
|
let isFirst = true;
|
|
94
95
|
const maxToolCalls = options.maxToolCalls ?? 5;
|
|
95
96
|
let stepCount = 0;
|
|
96
|
-
|
|
97
|
+
const totalUsage = { input_tokens: 0, output_tokens: 0, total_tokens: 0 };
|
|
97
98
|
const trackUsage = (u) => {
|
|
98
99
|
if (u) {
|
|
99
100
|
totalUsage.input_tokens += u.input_tokens;
|
|
@@ -131,7 +132,7 @@ export class ChatStream {
|
|
|
131
132
|
headers: options.headers,
|
|
132
133
|
requestTimeout: options.requestTimeout ?? config.requestTimeout,
|
|
133
134
|
signal: abortController.signal,
|
|
134
|
-
...options.params
|
|
135
|
+
...options.params
|
|
135
136
|
})) {
|
|
136
137
|
if (isFirst) {
|
|
137
138
|
if (options.onNewMessage)
|
|
@@ -181,41 +182,34 @@ export class ChatStream {
|
|
|
181
182
|
if (options.toolExecution === ToolExecutionMode.CONFIRM) {
|
|
182
183
|
const approved = await ToolHandler.requestToolConfirmation(toolCall, options.onConfirmToolCall);
|
|
183
184
|
if (!approved) {
|
|
184
|
-
messages.push(
|
|
185
|
-
role: "tool",
|
|
186
|
-
tool_call_id: toolCall.id,
|
|
187
|
-
content: "Action cancelled by user.",
|
|
188
|
-
});
|
|
185
|
+
messages.push(provider.formatToolResultMessage(toolCall.id, "Action cancelled by user."));
|
|
189
186
|
continue;
|
|
190
187
|
}
|
|
191
188
|
}
|
|
192
189
|
try {
|
|
193
190
|
const toolResult = await ToolHandler.execute(toolCall, options.tools, options.onToolCallStart, options.onToolCallEnd);
|
|
194
|
-
messages.push(toolResult);
|
|
191
|
+
messages.push(provider.formatToolResultMessage(toolResult.tool_call_id, toolResult.content));
|
|
195
192
|
}
|
|
196
193
|
catch (error) {
|
|
197
|
-
const
|
|
198
|
-
|
|
194
|
+
const err = error;
|
|
195
|
+
const directive = await options.onToolCallError?.(toolCall, err);
|
|
196
|
+
if (directive === "STOP") {
|
|
199
197
|
throw error;
|
|
200
198
|
}
|
|
201
|
-
messages.push({
|
|
202
|
-
|
|
203
|
-
tool_call_id: toolCall.id,
|
|
204
|
-
content: `Fatal error executing tool '${toolCall.function.name}': ${error.message}`,
|
|
205
|
-
});
|
|
206
|
-
if (directive === 'CONTINUE') {
|
|
199
|
+
messages.push(provider.formatToolResultMessage(toolCall.id, `Fatal error executing tool '${toolCall.function.name}': ${err.message}`, { isError: true }));
|
|
200
|
+
if (directive === "CONTINUE") {
|
|
207
201
|
continue;
|
|
208
202
|
}
|
|
209
|
-
const isFatal =
|
|
203
|
+
const isFatal = err.fatal === true || err.status === 401 || err.status === 403;
|
|
210
204
|
if (isFatal) {
|
|
211
|
-
throw
|
|
205
|
+
throw err;
|
|
212
206
|
}
|
|
213
|
-
|
|
207
|
+
logger.error(`Tool execution failed for '${toolCall.function.name}':`, error);
|
|
214
208
|
}
|
|
215
209
|
}
|
|
216
210
|
}
|
|
217
211
|
catch (error) {
|
|
218
|
-
if (error instanceof Error && error.name ===
|
|
212
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
219
213
|
// Aborted
|
|
220
214
|
}
|
|
221
215
|
throw error;
|
package/dist/chat/Content.d.ts
CHANGED
|
@@ -21,12 +21,12 @@ export type ContentPart = {
|
|
|
21
21
|
export declare const isBinaryContent: (part: ContentPart) => boolean;
|
|
22
22
|
export declare const isTextContent: (part: ContentPart) => boolean;
|
|
23
23
|
export declare const partitionContentParts: (parts: ContentPart[]) => {
|
|
24
|
-
textParts:
|
|
24
|
+
textParts: {
|
|
25
25
|
type: "text";
|
|
26
26
|
text: string;
|
|
27
|
-
}
|
|
27
|
+
}[];
|
|
28
28
|
binaryParts: ContentPart[];
|
|
29
29
|
};
|
|
30
30
|
export declare const formatMultimodalContent: (content: string | ContentPart[], parts: ContentPart[]) => MessageContent;
|
|
31
|
-
export type MessageContent = string |
|
|
31
|
+
export type MessageContent = string | string | ContentPart[];
|
|
32
32
|
//# sourceMappingURL=Content.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Content.d.ts","sourceRoot":"","sources":["../../src/chat/Content.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GACnB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,GACjD;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,WAAW,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,GACtE;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAAC;AAEtD,eAAO,MAAM,eAAe,GAAI,MAAM,WAAW,KAAG,OACmC,CAAC;AAExF,eAAO,MAAM,aAAa,GAAI,MAAM,WAAW,KAAG,
|
|
1
|
+
{"version":3,"file":"Content.d.ts","sourceRoot":"","sources":["../../src/chat/Content.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GACnB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,GACjD;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,WAAW,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,GACtE;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAAC;AAEtD,eAAO,MAAM,eAAe,GAAI,MAAM,WAAW,KAAG,OACmC,CAAC;AAExF,eAAO,MAAM,aAAa,GAAI,MAAM,WAAW,KAAG,OAA+B,CAAC;AAElF,eAAO,MAAM,qBAAqB,GAAI,OAAO,WAAW,EAAE;eAEZ;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE;;CAG7E,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAClC,SAAS,MAAM,GAAG,WAAW,EAAE,EAC/B,OAAO,WAAW,EAAE,KACnB,cAwBF,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,MAAM,GAAG,WAAW,EAAE,CAAC"}
|
package/dist/chat/Content.js
CHANGED
|
@@ -9,9 +9,9 @@ export const partitionContentParts = (parts) => {
|
|
|
9
9
|
export const formatMultimodalContent = (content, parts) => {
|
|
10
10
|
const { textParts, binaryParts } = partitionContentParts(parts);
|
|
11
11
|
let fullText = typeof content === "string" ? content : "";
|
|
12
|
-
|
|
12
|
+
const currentParts = typeof content === "string" ? [] : content;
|
|
13
13
|
if (textParts.length > 0) {
|
|
14
|
-
const additionalText = textParts.map(f => f.text).join("\n");
|
|
14
|
+
const additionalText = textParts.map((f) => f.text).join("\n");
|
|
15
15
|
if (typeof content === "string") {
|
|
16
16
|
fullText += "\n" + additionalText;
|
|
17
17
|
}
|
|
@@ -21,10 +21,7 @@ export const formatMultimodalContent = (content, parts) => {
|
|
|
21
21
|
}
|
|
22
22
|
if (binaryParts.length > 0) {
|
|
23
23
|
if (typeof content === "string") {
|
|
24
|
-
return [
|
|
25
|
-
{ type: "text", text: fullText },
|
|
26
|
-
...binaryParts
|
|
27
|
-
];
|
|
24
|
+
return [{ type: "text", text: fullText }, ...binaryParts];
|
|
28
25
|
}
|
|
29
26
|
else {
|
|
30
27
|
return [...currentParts, ...binaryParts];
|
package/dist/chat/Message.d.ts
CHANGED
|
@@ -2,13 +2,15 @@ import { Role } from "./Role.js";
|
|
|
2
2
|
import { ToolCall } from "./Tool.js";
|
|
3
3
|
import { MessageContent } from "./Content.js";
|
|
4
4
|
import { Usage } from "../providers/Provider.js";
|
|
5
|
+
import { ChatResponseString } from "./ChatResponse.js";
|
|
5
6
|
export interface Message {
|
|
6
7
|
role: Role;
|
|
7
|
-
content: MessageContent | null;
|
|
8
|
+
content: MessageContent | ChatResponseString | null;
|
|
8
9
|
tool_calls?: ToolCall[];
|
|
9
10
|
tool_call_id?: string;
|
|
10
11
|
name?: string;
|
|
11
12
|
usage?: Usage;
|
|
12
13
|
reasoning?: string;
|
|
14
|
+
isError?: boolean;
|
|
13
15
|
}
|
|
14
16
|
//# sourceMappingURL=Message.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Message.d.ts","sourceRoot":"","sources":["../../src/chat/Message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"Message.d.ts","sourceRoot":"","sources":["../../src/chat/Message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,IAAI,CAAC;IACX,OAAO,EAAE,cAAc,GAAG,kBAAkB,GAAG,IAAI,CAAC;IACpD,UAAU,CAAC,EAAE,QAAQ,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB"}
|
package/dist/chat/Role.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Role.d.ts","sourceRoot":"","sources":["../../src/chat/Role.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,IAAI,
|
|
1
|
+
{"version":3,"file":"Role.d.ts","sourceRoot":"","sources":["../../src/chat/Role.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,IAAI,GAAG,QAAQ,GAAG,WAAW,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,CAAC"}
|