@node-llm/core 1.5.4 → 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 +57 -41
- package/dist/aliases.js +239 -239
- package/dist/chat/Chat.d.ts +17 -17
- package/dist/chat/Chat.d.ts.map +1 -1
- package/dist/chat/Chat.js +56 -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 +14 -21
- 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 -15
- package/dist/chat/Validation.d.ts.map +1 -1
- package/dist/chat/Validation.js +9 -3
- 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 +201 -130
- 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 +7 -7
- 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 -15
- 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 +23 -13
- package/dist/utils/FileLoader.d.ts.map +1 -1
- package/dist/utils/FileLoader.js +25 -4
- package/dist/utils/audio.js +1 -1
- package/dist/utils/fetch.d.ts.map +1 -1
- package/dist/utils/fetch.js +3 -2
- 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
|
@@ -44,13 +44,13 @@ export declare class Chat {
|
|
|
44
44
|
*/
|
|
45
45
|
withTool(tool: ToolResolvable): this;
|
|
46
46
|
/**
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
+
*/
|
|
54
54
|
withTools(tools: ToolResolvable[], options?: {
|
|
55
55
|
replace?: boolean;
|
|
56
56
|
}): this;
|
|
@@ -83,31 +83,31 @@ export declare class Chat {
|
|
|
83
83
|
*/
|
|
84
84
|
withRequestOptions(options: {
|
|
85
85
|
headers?: Record<string, string>;
|
|
86
|
-
responseFormat?:
|
|
86
|
+
responseFormat?: unknown;
|
|
87
87
|
}): this;
|
|
88
88
|
/**
|
|
89
89
|
* Set provider-specific parameters.
|
|
90
90
|
* These will be merged into the final request payload.
|
|
91
91
|
*/
|
|
92
|
-
withParams(params: Record<string,
|
|
92
|
+
withParams(params: Record<string, unknown>): this;
|
|
93
93
|
/**
|
|
94
94
|
* Enforce a specific schema for the output.
|
|
95
95
|
* Can accept a Schema object or a Zod schema/JSON Schema directly.
|
|
96
96
|
*/
|
|
97
|
-
withSchema(schema: Schema | z.ZodType<
|
|
97
|
+
withSchema(schema: Schema | z.ZodType<unknown> | Record<string, unknown> | null): this;
|
|
98
98
|
onNewMessage(handler: () => void): this;
|
|
99
99
|
onEndMessage(handler: (message: ChatResponseString) => void): this;
|
|
100
|
-
onToolCall(handler: (toolCall:
|
|
101
|
-
onToolResult(handler: (result:
|
|
100
|
+
onToolCall(handler: (toolCall: unknown) => void): this;
|
|
101
|
+
onToolResult(handler: (result: unknown) => void): this;
|
|
102
102
|
/**
|
|
103
103
|
* Called when a tool call starts.
|
|
104
104
|
*/
|
|
105
|
-
onToolCallStart(handler: (toolCall:
|
|
105
|
+
onToolCallStart(handler: (toolCall: unknown) => void): this;
|
|
106
106
|
/**
|
|
107
107
|
* Called when a tool call ends successfully.
|
|
108
108
|
*/
|
|
109
|
-
onToolCallEnd(handler: (toolCall:
|
|
110
|
-
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;
|
|
111
111
|
/**
|
|
112
112
|
* Set the tool execution mode.
|
|
113
113
|
* - "auto": (Default) Automatically execute all tool calls.
|
|
@@ -119,7 +119,7 @@ export declare class Chat {
|
|
|
119
119
|
* Hook for confirming tool execution in "confirm" mode.
|
|
120
120
|
* Return true to proceed, false to cancel the specific call.
|
|
121
121
|
*/
|
|
122
|
-
onConfirmToolCall(handler: (toolCall:
|
|
122
|
+
onConfirmToolCall(handler: (toolCall: unknown) => Promise<boolean> | boolean): this;
|
|
123
123
|
/**
|
|
124
124
|
* Add a hook to process messages before sending to the LLM.
|
|
125
125
|
* Useful for PII detection, redaction, and input moderation.
|
|
@@ -133,7 +133,7 @@ export declare class Chat {
|
|
|
133
133
|
/**
|
|
134
134
|
* Ask the model a question
|
|
135
135
|
*/
|
|
136
|
-
ask(content: string |
|
|
136
|
+
ask(content: string | ContentPart[], options?: AskOptions): Promise<ChatResponseString>;
|
|
137
137
|
/**
|
|
138
138
|
* Streams the model's response to a user question.
|
|
139
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,cAAc,CAAC;AACrG,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAkB,MAAM,0BAA0B,CAAC;AAGtF,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAkB,cAAc,EAAE,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66
|
acc.total_tokens += msg.usage.total_tokens;
|
|
67
67
|
acc.cached_tokens = (acc.cached_tokens ?? 0) + (msg.usage.cached_tokens ?? 0);
|
|
68
|
-
acc.cache_creation_tokens =
|
|
68
|
+
acc.cache_creation_tokens =
|
|
69
|
+
(acc.cache_creation_tokens ?? 0) + (msg.usage.cache_creation_tokens ?? 0);
|
|
69
70
|
acc.cost = (acc.cost ?? 0) + (msg.usage.cost ?? 0);
|
|
70
71
|
}
|
|
71
72
|
return acc;
|
|
72
|
-
}, {
|
|
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
|
+
});
|
|
73
81
|
}
|
|
74
82
|
/**
|
|
75
83
|
* Add a tool to the chat session (fluent API)
|
|
@@ -79,13 +87,13 @@ export class Chat {
|
|
|
79
87
|
return this.withTools([tool]);
|
|
80
88
|
}
|
|
81
89
|
/**
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
+
*/
|
|
89
97
|
withTools(tools, options) {
|
|
90
98
|
if (options?.replace) {
|
|
91
99
|
this.options.tools = [];
|
|
@@ -260,7 +268,7 @@ export class Chat {
|
|
|
260
268
|
}
|
|
261
269
|
this.messages.push({
|
|
262
270
|
role: "user",
|
|
263
|
-
content: messageContent
|
|
271
|
+
content: messageContent
|
|
264
272
|
});
|
|
265
273
|
// Process Schema/Structured Output
|
|
266
274
|
let responseFormat = this.options.responseFormat;
|
|
@@ -273,7 +281,7 @@ export class Chat {
|
|
|
273
281
|
name: this.options.schema.definition.name,
|
|
274
282
|
description: this.options.schema.definition.description,
|
|
275
283
|
strict: this.options.schema.definition.strict ?? true,
|
|
276
|
-
schema: jsonSchema
|
|
284
|
+
schema: jsonSchema
|
|
277
285
|
}
|
|
278
286
|
};
|
|
279
287
|
}
|
|
@@ -287,7 +295,7 @@ export class Chat {
|
|
|
287
295
|
response_format: responseFormat, // Pass to provider
|
|
288
296
|
requestTimeout: options?.requestTimeout ?? this.options.requestTimeout ?? config.requestTimeout,
|
|
289
297
|
signal: options?.signal,
|
|
290
|
-
...this.options.params
|
|
298
|
+
...this.options.params
|
|
291
299
|
};
|
|
292
300
|
// --- Content Policy Hooks (Input) ---
|
|
293
301
|
if (this.options.onBeforeRequest) {
|
|
@@ -298,7 +306,7 @@ export class Chat {
|
|
|
298
306
|
executeOptions.messages = result;
|
|
299
307
|
}
|
|
300
308
|
}
|
|
301
|
-
|
|
309
|
+
const totalUsage = { input_tokens: 0, output_tokens: 0, total_tokens: 0 };
|
|
302
310
|
const trackUsage = (u) => {
|
|
303
311
|
if (u) {
|
|
304
312
|
totalUsage.input_tokens += u.input_tokens;
|
|
@@ -327,9 +335,9 @@ export class Chat {
|
|
|
327
335
|
}
|
|
328
336
|
this.messages.push({
|
|
329
337
|
role: "assistant",
|
|
330
|
-
content: assistantMessage || null,
|
|
338
|
+
content: assistantMessage?.toString() || null,
|
|
331
339
|
tool_calls: response.tool_calls,
|
|
332
|
-
usage: response.usage
|
|
340
|
+
usage: response.usage
|
|
333
341
|
});
|
|
334
342
|
if (this.options.onEndMessage && (!response.tool_calls || response.tool_calls.length === 0)) {
|
|
335
343
|
this.options.onEndMessage(assistantMessage);
|
|
@@ -350,37 +358,42 @@ export class Chat {
|
|
|
350
358
|
if (this.options.toolExecution === ToolExecutionMode.CONFIRM) {
|
|
351
359
|
const approved = await ToolHandler.requestToolConfirmation(toolCall, this.options.onConfirmToolCall);
|
|
352
360
|
if (!approved) {
|
|
353
|
-
this.messages.push(
|
|
354
|
-
role: "tool",
|
|
355
|
-
tool_call_id: toolCall.id,
|
|
356
|
-
content: "Action cancelled by user.",
|
|
357
|
-
});
|
|
361
|
+
this.messages.push(this.provider.formatToolResultMessage(toolCall.id, "Action cancelled by user."));
|
|
358
362
|
continue;
|
|
359
363
|
}
|
|
360
364
|
}
|
|
361
365
|
try {
|
|
362
366
|
const toolResult = await ToolHandler.execute(toolCall, this.options.tools, this.options.onToolCallStart, this.options.onToolCallEnd);
|
|
363
|
-
this.messages.push(toolResult);
|
|
367
|
+
this.messages.push(this.provider.formatToolResultMessage(toolResult.tool_call_id, toolResult.content));
|
|
364
368
|
}
|
|
365
369
|
catch (error) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
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
|
+
}
|
|
369
385
|
}
|
|
370
|
-
this.messages.push({
|
|
371
|
-
|
|
372
|
-
tool_call_id: toolCall.id,
|
|
373
|
-
content: `Fatal error executing tool '${toolCall.function.name}': ${error.message}`,
|
|
374
|
-
});
|
|
375
|
-
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") {
|
|
376
388
|
continue;
|
|
377
389
|
}
|
|
378
390
|
// Default short-circuit logic
|
|
379
|
-
const
|
|
391
|
+
const errorObj = currentError;
|
|
392
|
+
const isFatal = errorObj.fatal === true || errorObj.status === 401 || errorObj.status === 403;
|
|
380
393
|
if (isFatal) {
|
|
381
|
-
throw
|
|
394
|
+
throw currentError;
|
|
382
395
|
}
|
|
383
|
-
logger.error(`Tool execution failed for '${toolCall.function.name}':`,
|
|
396
|
+
logger.error(`Tool execution failed for '${toolCall.function.name}':`, currentError);
|
|
384
397
|
}
|
|
385
398
|
}
|
|
386
399
|
response = await this.executor.executeChat({
|
|
@@ -393,7 +406,7 @@ export class Chat {
|
|
|
393
406
|
response_format: responseFormat,
|
|
394
407
|
requestTimeout: options?.requestTimeout ?? this.options.requestTimeout ?? config.requestTimeout,
|
|
395
408
|
signal: options?.signal,
|
|
396
|
-
...this.options.params
|
|
409
|
+
...this.options.params
|
|
397
410
|
});
|
|
398
411
|
trackUsage(response.usage);
|
|
399
412
|
assistantMessage = new ChatResponseString(response.content ?? "", response.usage ?? { input_tokens: 0, output_tokens: 0, total_tokens: 0 }, this.model, this.provider.id, response.reasoning);
|
|
@@ -406,15 +419,15 @@ export class Chat {
|
|
|
406
419
|
}
|
|
407
420
|
this.messages.push({
|
|
408
421
|
role: "assistant",
|
|
409
|
-
content: assistantMessage || null,
|
|
422
|
+
content: assistantMessage?.toString() || null,
|
|
410
423
|
tool_calls: response.tool_calls,
|
|
411
|
-
usage: response.usage
|
|
424
|
+
usage: response.usage
|
|
412
425
|
});
|
|
413
426
|
if (this.options.onEndMessage && (!response.tool_calls || response.tool_calls.length === 0)) {
|
|
414
427
|
this.options.onEndMessage(assistantMessage);
|
|
415
428
|
}
|
|
416
429
|
}
|
|
417
|
-
// 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?
|
|
418
431
|
// Usually reasoning only happens once or we just want the last one.
|
|
419
432
|
return new ChatResponseString(assistantMessage.toString() || "", totalUsage, this.model, this.provider.id, assistantMessage.reasoning, response.tool_calls);
|
|
420
433
|
}
|
|
@@ -449,17 +462,18 @@ export class Chat {
|
|
|
449
462
|
if (typeof toolInstance.toLLMTool === "function") {
|
|
450
463
|
return toolInstance.toLLMTool();
|
|
451
464
|
}
|
|
452
|
-
|
|
465
|
+
const toolObj = toolInstance;
|
|
466
|
+
if (!toolObj.function || !toolObj.function.name) {
|
|
453
467
|
// 1. Validate structure
|
|
454
468
|
throw new ConfigurationError(`[NodeLLM] Tool validation failed: 'function.name' is required for raw tool objects.`);
|
|
455
469
|
}
|
|
456
|
-
if (
|
|
470
|
+
if (toolObj.type !== "function") {
|
|
457
471
|
// 2. Ensure 'type: function' exists (standardize for providers)
|
|
458
|
-
|
|
472
|
+
toolObj.type = "function";
|
|
459
473
|
}
|
|
460
|
-
if (typeof
|
|
474
|
+
if (typeof toolObj.handler !== "function") {
|
|
461
475
|
// 3. Validate handler existence
|
|
462
|
-
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()')`);
|
|
463
477
|
}
|
|
464
478
|
return toolInstance;
|
|
465
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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aAAa,CAAC,EAAE,iBAAiB,CAAC;IAClC,iBAAiB,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;IACtE,eAAe,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;IACrE,eAAe,CAAC,EAAE,CAAC,QAAQ,EAAE,kBAAkB,KAAK,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAAC;CACxF"}
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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
|
@@ -28,7 +28,7 @@ export class ChatStream {
|
|
|
28
28
|
if (options.systemPrompt) {
|
|
29
29
|
this.systemMessages.push({
|
|
30
30
|
role: "system",
|
|
31
|
-
content: options.systemPrompt
|
|
31
|
+
content: options.systemPrompt
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
if (options.messages) {
|
|
@@ -84,7 +84,7 @@ export class ChatStream {
|
|
|
84
84
|
name: options.schema.definition.name,
|
|
85
85
|
description: options.schema.definition.description,
|
|
86
86
|
strict: options.schema.definition.strict ?? true,
|
|
87
|
-
schema: jsonSchema
|
|
87
|
+
schema: jsonSchema
|
|
88
88
|
}
|
|
89
89
|
};
|
|
90
90
|
}
|
|
@@ -94,7 +94,7 @@ export class ChatStream {
|
|
|
94
94
|
let isFirst = true;
|
|
95
95
|
const maxToolCalls = options.maxToolCalls ?? 5;
|
|
96
96
|
let stepCount = 0;
|
|
97
|
-
|
|
97
|
+
const totalUsage = { input_tokens: 0, output_tokens: 0, total_tokens: 0 };
|
|
98
98
|
const trackUsage = (u) => {
|
|
99
99
|
if (u) {
|
|
100
100
|
totalUsage.input_tokens += u.input_tokens;
|
|
@@ -132,7 +132,7 @@ export class ChatStream {
|
|
|
132
132
|
headers: options.headers,
|
|
133
133
|
requestTimeout: options.requestTimeout ?? config.requestTimeout,
|
|
134
134
|
signal: abortController.signal,
|
|
135
|
-
...options.params
|
|
135
|
+
...options.params
|
|
136
136
|
})) {
|
|
137
137
|
if (isFirst) {
|
|
138
138
|
if (options.onNewMessage)
|
|
@@ -182,41 +182,34 @@ export class ChatStream {
|
|
|
182
182
|
if (options.toolExecution === ToolExecutionMode.CONFIRM) {
|
|
183
183
|
const approved = await ToolHandler.requestToolConfirmation(toolCall, options.onConfirmToolCall);
|
|
184
184
|
if (!approved) {
|
|
185
|
-
messages.push(
|
|
186
|
-
role: "tool",
|
|
187
|
-
tool_call_id: toolCall.id,
|
|
188
|
-
content: "Action cancelled by user.",
|
|
189
|
-
});
|
|
185
|
+
messages.push(provider.formatToolResultMessage(toolCall.id, "Action cancelled by user."));
|
|
190
186
|
continue;
|
|
191
187
|
}
|
|
192
188
|
}
|
|
193
189
|
try {
|
|
194
190
|
const toolResult = await ToolHandler.execute(toolCall, options.tools, options.onToolCallStart, options.onToolCallEnd);
|
|
195
|
-
messages.push(toolResult);
|
|
191
|
+
messages.push(provider.formatToolResultMessage(toolResult.tool_call_id, toolResult.content));
|
|
196
192
|
}
|
|
197
193
|
catch (error) {
|
|
198
|
-
const
|
|
199
|
-
|
|
194
|
+
const err = error;
|
|
195
|
+
const directive = await options.onToolCallError?.(toolCall, err);
|
|
196
|
+
if (directive === "STOP") {
|
|
200
197
|
throw error;
|
|
201
198
|
}
|
|
202
|
-
messages.push({
|
|
203
|
-
|
|
204
|
-
tool_call_id: toolCall.id,
|
|
205
|
-
content: `Fatal error executing tool '${toolCall.function.name}': ${error.message}`,
|
|
206
|
-
});
|
|
207
|
-
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") {
|
|
208
201
|
continue;
|
|
209
202
|
}
|
|
210
|
-
const isFatal =
|
|
203
|
+
const isFatal = err.fatal === true || err.status === 401 || err.status === 403;
|
|
211
204
|
if (isFatal) {
|
|
212
|
-
throw
|
|
205
|
+
throw err;
|
|
213
206
|
}
|
|
214
207
|
logger.error(`Tool execution failed for '${toolCall.function.name}':`, error);
|
|
215
208
|
}
|
|
216
209
|
}
|
|
217
210
|
}
|
|
218
211
|
catch (error) {
|
|
219
|
-
if (error instanceof Error && error.name ===
|
|
212
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
220
213
|
// Aborted
|
|
221
214
|
}
|
|
222
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"}
|