universal-llm-client 4.0.0 ā 4.2.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/dist/ai-model.d.ts +20 -22
- package/dist/ai-model.d.ts.map +1 -1
- package/dist/ai-model.js +26 -23
- package/dist/ai-model.js.map +1 -1
- package/dist/client.d.ts +5 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +17 -9
- package/dist/client.js.map +1 -1
- package/dist/http.d.ts +2 -0
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +1 -0
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +49 -11
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/interfaces.js +14 -0
- package/dist/interfaces.js.map +1 -1
- package/dist/providers/anthropic.d.ts +56 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +524 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/google.d.ts +5 -0
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +64 -8
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/ollama.d.ts.map +1 -1
- package/dist/providers/ollama.js +38 -11
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +9 -7
- package/dist/providers/openai.js.map +1 -1
- package/dist/router.d.ts +13 -33
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +33 -57
- package/dist/router.js.map +1 -1
- package/dist/stream-decoder.d.ts +29 -2
- package/dist/stream-decoder.d.ts.map +1 -1
- package/dist/stream-decoder.js +39 -11
- package/dist/stream-decoder.js.map +1 -1
- package/dist/structured-output.d.ts +107 -181
- package/dist/structured-output.d.ts.map +1 -1
- package/dist/structured-output.js +137 -192
- package/dist/structured-output.js.map +1 -1
- package/dist/zod-adapter.d.ts +44 -0
- package/dist/zod-adapter.d.ts.map +1 -0
- package/dist/zod-adapter.js +61 -0
- package/dist/zod-adapter.js.map +1 -0
- package/package.json +9 -1
- package/src/ai-model.ts +350 -0
- package/src/auditor.ts +213 -0
- package/src/client.ts +402 -0
- package/src/debug/debug-google-streaming.ts +97 -0
- package/src/debug/debug-tool-execution.ts +86 -0
- package/src/debug/test-lmstudio-tools.ts +155 -0
- package/src/demos/README.md +47 -0
- package/src/demos/basic/universal-llm-examples.ts +161 -0
- package/src/demos/mcp/astrid-memory-demo.ts +295 -0
- package/src/demos/mcp/astrid-persona-memory.ts +357 -0
- package/src/demos/mcp/mcp-mongodb-demo.ts +275 -0
- package/src/demos/mcp/simple-astrid-memory.ts +148 -0
- package/src/demos/mcp/simple-mcp-demo.ts +68 -0
- package/src/demos/mcp/working-mcp-demo.ts +62 -0
- package/src/demos/model-alias-demo.ts +0 -0
- package/src/demos/tools/RAG_MEMORY_INTEGRATION.md +267 -0
- package/src/demos/tools/astrid-memory-demo.ts +270 -0
- package/src/demos/tools/astrid-production-memory-clean.ts +785 -0
- package/src/demos/tools/astrid-production-memory.ts +558 -0
- package/src/demos/tools/basic-translation-test.ts +66 -0
- package/src/demos/tools/chromadb-similarity-tuning.ts +390 -0
- package/src/demos/tools/clean-multilingual-conversation.ts +209 -0
- package/src/demos/tools/clean-translation-test.ts +119 -0
- package/src/demos/tools/clean-universal-multilingual-test.ts +131 -0
- package/src/demos/tools/complete-rag-demo.ts +369 -0
- package/src/demos/tools/complete-tool-demo.ts +132 -0
- package/src/demos/tools/demo-tool-calling.ts +124 -0
- package/src/demos/tools/dynamic-language-switching-test.ts +251 -0
- package/src/demos/tools/hybrid-thinking-test.ts +154 -0
- package/src/demos/tools/memory-integration-test.ts +420 -0
- package/src/demos/tools/multilingual-memory-system.ts +802 -0
- package/src/demos/tools/ondemand-translation-demo.ts +655 -0
- package/src/demos/tools/production-tool-demo.ts +245 -0
- package/src/demos/tools/revolutionary-multilingual-test.ts +151 -0
- package/src/demos/tools/rigorous-language-analysis.ts +218 -0
- package/src/demos/tools/test-universal-memory-system.ts +126 -0
- package/src/demos/tools/translation-integration-guide.ts +346 -0
- package/src/demos/tools/universal-memory-system.ts +560 -0
- package/src/http.ts +247 -0
- package/src/index.ts +161 -0
- package/src/interfaces.ts +657 -0
- package/src/mcp.ts +345 -0
- package/src/providers/anthropic.ts +762 -0
- package/src/providers/google.ts +620 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/ollama.ts +469 -0
- package/src/providers/openai.ts +392 -0
- package/src/router.ts +780 -0
- package/src/stream-decoder.ts +361 -0
- package/src/structured-output.ts +759 -0
- package/src/test-scripts/test-advanced-tools.ts +310 -0
- package/src/test-scripts/test-google-streaming-enhanced.ts +147 -0
- package/src/test-scripts/test-google-streaming.ts +63 -0
- package/src/test-scripts/test-google-system-prompt-comprehensive.ts +189 -0
- package/src/test-scripts/test-mcp-config.ts +28 -0
- package/src/test-scripts/test-mcp-connection.ts +29 -0
- package/src/test-scripts/test-system-message-positions.ts +163 -0
- package/src/test-scripts/test-system-prompt-improvement-demo.ts +83 -0
- package/src/test-scripts/test-tool-calling.ts +231 -0
- package/src/tests/ai-model.test.ts +1614 -0
- package/src/tests/auditor.test.ts +224 -0
- package/src/tests/http.test.ts +200 -0
- package/src/tests/interfaces.test.ts +117 -0
- package/src/tests/providers/google.test.ts +660 -0
- package/src/tests/providers/ollama.test.ts +954 -0
- package/src/tests/providers/openai.test.ts +1122 -0
- package/src/tests/router.test.ts +254 -0
- package/src/tests/stream-decoder.test.ts +179 -0
- package/src/tests/structured-output.test.ts +1450 -0
- package/src/tests/tools.test.ts +175 -0
- package/src/tools.ts +246 -0
- package/src/zod-adapter.ts +72 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Universal LLM Client v3 ā Base LLM Client
|
|
3
|
+
*
|
|
4
|
+
* Abstract base class for all LLM providers.
|
|
5
|
+
* Handles tool registration, execution, and the autonomous
|
|
6
|
+
* multi-turn tool execution loop.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
LLMClientOptions,
|
|
11
|
+
LLMChatMessage,
|
|
12
|
+
LLMChatResponse,
|
|
13
|
+
LLMToolDefinition,
|
|
14
|
+
LLMToolCall,
|
|
15
|
+
LLMFunction,
|
|
16
|
+
ToolRegistry,
|
|
17
|
+
ToolHandler,
|
|
18
|
+
ToolExecutionResult,
|
|
19
|
+
ChatOptions,
|
|
20
|
+
ModelMetadata,
|
|
21
|
+
} from './interfaces.js';
|
|
22
|
+
import {
|
|
23
|
+
StructuredOutputError,
|
|
24
|
+
type StructuredOutputOptions,
|
|
25
|
+
type SchemaConfig,
|
|
26
|
+
} from './structured-output.js';
|
|
27
|
+
import type { DecodedEvent } from './stream-decoder.js';
|
|
28
|
+
import type { Auditor } from './auditor.js';
|
|
29
|
+
import { NoopAuditor } from './auditor.js';
|
|
30
|
+
|
|
31
|
+
// ============================================================================
|
|
32
|
+
// Abstract Base Client
|
|
33
|
+
// ============================================================================
|
|
34
|
+
|
|
35
|
+
export abstract class BaseLLMClient {
|
|
36
|
+
protected options: LLMClientOptions;
|
|
37
|
+
protected toolRegistry: ToolRegistry = {};
|
|
38
|
+
protected auditor: Auditor;
|
|
39
|
+
protected debug: boolean;
|
|
40
|
+
|
|
41
|
+
constructor(options: LLMClientOptions, auditor?: Auditor) {
|
|
42
|
+
this.options = options;
|
|
43
|
+
this.auditor = auditor ?? new NoopAuditor();
|
|
44
|
+
this.debug = options.debug ?? false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ========================================================================
|
|
48
|
+
// Abstract Methods (implemented by providers)
|
|
49
|
+
// ========================================================================
|
|
50
|
+
|
|
51
|
+
/** Send a chat request and get a response */
|
|
52
|
+
abstract chat(
|
|
53
|
+
messages: LLMChatMessage[],
|
|
54
|
+
options?: ChatOptions,
|
|
55
|
+
): Promise<LLMChatResponse>;
|
|
56
|
+
|
|
57
|
+
/** Stream a chat response as decoded events */
|
|
58
|
+
abstract chatStream(
|
|
59
|
+
messages: LLMChatMessage[],
|
|
60
|
+
options?: ChatOptions,
|
|
61
|
+
): AsyncGenerator<DecodedEvent, LLMChatResponse | void, unknown>;
|
|
62
|
+
|
|
63
|
+
/** Get available models */
|
|
64
|
+
abstract getModels(): Promise<string[]>;
|
|
65
|
+
|
|
66
|
+
/** Generate embeddings for text */
|
|
67
|
+
abstract embed(text: string): Promise<number[]>;
|
|
68
|
+
|
|
69
|
+
/** Generate embeddings for multiple texts */
|
|
70
|
+
async embedArray(texts: string[]): Promise<number[][]> {
|
|
71
|
+
return Promise.all(texts.map(t => this.embed(t)));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Get metadata about a model (context length, architecture, etc.)
|
|
76
|
+
* Override per-provider for accurate data.
|
|
77
|
+
*/
|
|
78
|
+
async getModelInfo(_modelName?: string): Promise<ModelMetadata> {
|
|
79
|
+
return { contextLength: 8192 }; // Conservative default
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ========================================================================
|
|
83
|
+
// Tool Registration
|
|
84
|
+
// ========================================================================
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Sanitize tool name for LLM compatibility.
|
|
88
|
+
* LLM APIs require function names matching [a-zA-Z0-9_-].
|
|
89
|
+
* Module-prefixed names like "@core/computer:list_windows" are cleaned.
|
|
90
|
+
*/
|
|
91
|
+
private sanitizeToolName(name: string): string {
|
|
92
|
+
return name
|
|
93
|
+
.replace(/^@[^:]+:/, '') // Strip module prefix
|
|
94
|
+
.replace(/[^a-zA-Z0-9_-]/g, '_') // Replace illegal chars
|
|
95
|
+
.replace(/_+/g, '_') // Collapse
|
|
96
|
+
.replace(/^_|_$/g, ''); // Trim
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Register a tool/function callable by the model */
|
|
100
|
+
registerTool(
|
|
101
|
+
name: string,
|
|
102
|
+
description: string,
|
|
103
|
+
parameters: LLMFunction['parameters'],
|
|
104
|
+
handler: ToolHandler,
|
|
105
|
+
): void {
|
|
106
|
+
const safeName = this.sanitizeToolName(name);
|
|
107
|
+
this.toolRegistry[name] = {
|
|
108
|
+
definition: { name: safeName, description, parameters },
|
|
109
|
+
handler,
|
|
110
|
+
};
|
|
111
|
+
// Index by sanitized name for reverse lookup
|
|
112
|
+
if (safeName !== name && !this.toolRegistry[safeName]) {
|
|
113
|
+
this.toolRegistry[safeName] = this.toolRegistry[name]!;
|
|
114
|
+
}
|
|
115
|
+
this.debugLog(`Registered tool: ${name} (LLM name: ${safeName})`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Register multiple tools at once */
|
|
119
|
+
registerTools(
|
|
120
|
+
tools: Array<{
|
|
121
|
+
name: string;
|
|
122
|
+
description: string;
|
|
123
|
+
parameters: LLMFunction['parameters'];
|
|
124
|
+
handler: ToolHandler;
|
|
125
|
+
}>,
|
|
126
|
+
): void {
|
|
127
|
+
for (const tool of tools) {
|
|
128
|
+
this.registerTool(tool.name, tool.description, tool.parameters, tool.handler);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Get all registered tool definitions (deduplicated by sanitized name) */
|
|
133
|
+
getToolDefinitions(): LLMToolDefinition[] {
|
|
134
|
+
const seen = new Set<string>();
|
|
135
|
+
const defs: LLMToolDefinition[] = [];
|
|
136
|
+
for (const { definition } of Object.values(this.toolRegistry)) {
|
|
137
|
+
if (seen.has(definition.name)) continue;
|
|
138
|
+
seen.add(definition.name);
|
|
139
|
+
defs.push({ type: 'function' as const, function: definition });
|
|
140
|
+
}
|
|
141
|
+
return defs;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ========================================================================
|
|
145
|
+
// Tool Execution
|
|
146
|
+
// ========================================================================
|
|
147
|
+
|
|
148
|
+
/** Execute a single tool call with fuzzy name matching */
|
|
149
|
+
async executeTool(toolCall: LLMToolCall): Promise<ToolExecutionResult> {
|
|
150
|
+
const toolName = toolCall.function.name;
|
|
151
|
+
const start = Date.now();
|
|
152
|
+
let tool = this.toolRegistry[toolName];
|
|
153
|
+
|
|
154
|
+
// Fuzzy lookup: try suffix match (LLM stripped module prefix)
|
|
155
|
+
if (!tool) {
|
|
156
|
+
const entries = Object.entries(this.toolRegistry);
|
|
157
|
+
const bySuffix = entries.find(([k]) => k.endsWith(`:${toolName}`));
|
|
158
|
+
if (bySuffix) {
|
|
159
|
+
tool = bySuffix[1];
|
|
160
|
+
this.debugLog(`Fuzzy tool match: "${toolName}" ā "${bySuffix[0]}"`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Try prefix match: if only one tool in that module, use it
|
|
164
|
+
if (!tool) {
|
|
165
|
+
const byPrefix = entries.filter(([k]) => k.startsWith(`${toolName}:`));
|
|
166
|
+
if (byPrefix.length === 1) {
|
|
167
|
+
tool = byPrefix[0]![1];
|
|
168
|
+
this.debugLog(`Fuzzy tool match (single): "${toolName}" ā "${byPrefix[0]![0]}"`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!tool) {
|
|
174
|
+
const result: ToolExecutionResult = {
|
|
175
|
+
tool_call_id: toolCall.id,
|
|
176
|
+
output: null,
|
|
177
|
+
error: `Unknown tool: ${toolName}`,
|
|
178
|
+
duration: Date.now() - start,
|
|
179
|
+
};
|
|
180
|
+
this.auditor.record({
|
|
181
|
+
timestamp: Date.now(),
|
|
182
|
+
type: 'tool_result',
|
|
183
|
+
toolExecution: result,
|
|
184
|
+
error: result.error,
|
|
185
|
+
});
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
this.auditor.record({
|
|
190
|
+
timestamp: Date.now(),
|
|
191
|
+
type: 'tool_call',
|
|
192
|
+
metadata: { toolName, arguments: toolCall.function.arguments },
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
const args = JSON.parse(toolCall.function.arguments);
|
|
197
|
+
const output = await tool.handler(args);
|
|
198
|
+
const result: ToolExecutionResult = {
|
|
199
|
+
tool_call_id: toolCall.id,
|
|
200
|
+
output,
|
|
201
|
+
duration: Date.now() - start,
|
|
202
|
+
};
|
|
203
|
+
this.auditor.record({
|
|
204
|
+
timestamp: Date.now(),
|
|
205
|
+
type: 'tool_result',
|
|
206
|
+
toolExecution: result,
|
|
207
|
+
});
|
|
208
|
+
return result;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
const result: ToolExecutionResult = {
|
|
211
|
+
tool_call_id: toolCall.id,
|
|
212
|
+
output: null,
|
|
213
|
+
error: error instanceof Error ? error.message : String(error),
|
|
214
|
+
duration: Date.now() - start,
|
|
215
|
+
};
|
|
216
|
+
this.auditor.record({
|
|
217
|
+
timestamp: Date.now(),
|
|
218
|
+
type: 'tool_result',
|
|
219
|
+
toolExecution: result,
|
|
220
|
+
error: result.error,
|
|
221
|
+
});
|
|
222
|
+
return result;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Execute multiple tool calls in parallel */
|
|
227
|
+
async executeTools(toolCalls: LLMToolCall[]): Promise<ToolExecutionResult[]> {
|
|
228
|
+
return Promise.all(toolCalls.map(tc => this.executeTool(tc)));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ========================================================================
|
|
232
|
+
// Chat with Tools (multi-turn autonomous loop)
|
|
233
|
+
// ========================================================================
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Chat with automatic tool execution.
|
|
237
|
+
* Continues until the model stops calling tools or max iterations reached.
|
|
238
|
+
* Returns the complete execution trace in `toolExecutions`.
|
|
239
|
+
*/
|
|
240
|
+
async chatWithTools(
|
|
241
|
+
messages: LLMChatMessage[],
|
|
242
|
+
options?: ChatOptions & { maxIterations?: number },
|
|
243
|
+
): Promise<LLMChatResponse> {
|
|
244
|
+
const maxIterations = options?.maxIterations ?? 10;
|
|
245
|
+
const conversationMessages = [...messages];
|
|
246
|
+
const allToolExecutions: ToolExecutionResult[] = [];
|
|
247
|
+
let iterations = 0;
|
|
248
|
+
|
|
249
|
+
while (iterations < maxIterations) {
|
|
250
|
+
const response = await this.chat(conversationMessages, {
|
|
251
|
+
...options,
|
|
252
|
+
tools: this.getToolDefinitions(),
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// If no tool calls, return with full trace
|
|
256
|
+
if (!response.message.tool_calls?.length) {
|
|
257
|
+
return {
|
|
258
|
+
...response,
|
|
259
|
+
toolExecutions: allToolExecutions.length > 0 ? allToolExecutions : undefined,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Add assistant message with tool calls
|
|
264
|
+
conversationMessages.push(response.message);
|
|
265
|
+
|
|
266
|
+
// Execute tools in parallel
|
|
267
|
+
const toolResults = await this.executeTools(response.message.tool_calls);
|
|
268
|
+
allToolExecutions.push(...toolResults);
|
|
269
|
+
|
|
270
|
+
// Add tool results as messages
|
|
271
|
+
for (const result of toolResults) {
|
|
272
|
+
conversationMessages.push({
|
|
273
|
+
role: 'tool',
|
|
274
|
+
content: typeof result.output === 'string'
|
|
275
|
+
? result.output
|
|
276
|
+
: JSON.stringify(result.output),
|
|
277
|
+
tool_call_id: result.tool_call_id,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
iterations++;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Max iterations ā final call without tools
|
|
285
|
+
const finalResponse = await this.chat(conversationMessages);
|
|
286
|
+
return {
|
|
287
|
+
...finalResponse,
|
|
288
|
+
toolExecutions: allToolExecutions,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ========================================================================
|
|
293
|
+
// Helpers
|
|
294
|
+
// ========================================================================
|
|
295
|
+
|
|
296
|
+
/** Set the model name at runtime */
|
|
297
|
+
setModel(modelName: string): void {
|
|
298
|
+
this.options.model = modelName;
|
|
299
|
+
this.debugLog(`Model switched to: ${modelName}`);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Get the current model name */
|
|
303
|
+
get model(): string {
|
|
304
|
+
return this.options.model;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Get the base URL */
|
|
308
|
+
get url(): string {
|
|
309
|
+
return this.options.url;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Set the auditor instance */
|
|
313
|
+
setAuditor(auditor: Auditor): void {
|
|
314
|
+
this.auditor = auditor;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
protected debugLog(message: string, data?: unknown): void {
|
|
318
|
+
if (this.debug) {
|
|
319
|
+
console.log(`[LLM:${this.options.model}] ${message}`, data ?? '');
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Generate a unique ID for tool calls when the provider doesn't provide one.
|
|
325
|
+
*/
|
|
326
|
+
protected generateToolCallId(): string {
|
|
327
|
+
return `call_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// ========================================================================
|
|
331
|
+
// Structured Output Helpers (shared across all providers)
|
|
332
|
+
// ========================================================================
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Extract schema options from ChatOptions.
|
|
336
|
+
* Returns null if no schema is provided.
|
|
337
|
+
* Returns a SchemaConfig if a schema was found.
|
|
338
|
+
*/
|
|
339
|
+
protected extractSchemaOptions(options?: ChatOptions): (StructuredOutputOptions<unknown> & { schemaConfig: SchemaConfig<unknown> }) | null {
|
|
340
|
+
if (!options) return null;
|
|
341
|
+
|
|
342
|
+
if (options.schema) {
|
|
343
|
+
return {
|
|
344
|
+
schemaConfig: options.schema,
|
|
345
|
+
name: options.schemaName,
|
|
346
|
+
description: options.schemaDescription,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (options.jsonSchema) {
|
|
351
|
+
// Raw JSON Schema without validation
|
|
352
|
+
const config: SchemaConfig<unknown> = {
|
|
353
|
+
jsonSchema: options.jsonSchema,
|
|
354
|
+
};
|
|
355
|
+
return {
|
|
356
|
+
schemaConfig: config,
|
|
357
|
+
name: options.schemaName,
|
|
358
|
+
description: options.schemaDescription,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Validate structured response using a SchemaConfig.
|
|
367
|
+
* Throws StructuredOutputError on failure.
|
|
368
|
+
*/
|
|
369
|
+
protected validateStructuredResponse(content: string, config: SchemaConfig<unknown>): void {
|
|
370
|
+
if (!content) {
|
|
371
|
+
throw new StructuredOutputError(
|
|
372
|
+
'Empty response from LLM',
|
|
373
|
+
{ rawOutput: content },
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
let parsed: unknown;
|
|
378
|
+
try {
|
|
379
|
+
parsed = JSON.parse(content);
|
|
380
|
+
} catch (error) {
|
|
381
|
+
const syntaxError = error instanceof SyntaxError
|
|
382
|
+
? error
|
|
383
|
+
: new SyntaxError(String(error));
|
|
384
|
+
throw new StructuredOutputError(
|
|
385
|
+
`Failed to parse JSON: ${syntaxError.message}`,
|
|
386
|
+
{ rawOutput: content, cause: syntaxError },
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (config.validate) {
|
|
391
|
+
try {
|
|
392
|
+
config.validate(parsed);
|
|
393
|
+
} catch (error) {
|
|
394
|
+
const validationError = error instanceof Error ? error : new Error(String(error));
|
|
395
|
+
throw new StructuredOutputError(
|
|
396
|
+
`Validation failed: ${validationError.message}`,
|
|
397
|
+
{ rawOutput: content, cause: validationError },
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {request} from 'undici';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Debug Google streaming by examining raw responses
|
|
5
|
+
*/
|
|
6
|
+
async function debugGoogleStreaming() {
|
|
7
|
+
console.log('š Debugging Google Generative AI Streaming...\n');
|
|
8
|
+
|
|
9
|
+
const apiKey = 'AIzaSyBDbo7iVNEuCcRNTgDIgRrkGpFKisXXnm0';
|
|
10
|
+
const model = 'gemma-3-4b-it';
|
|
11
|
+
const endpoint = `https://generativelanguage.googleapis.com/v1beta/models/${model}:streamGenerateContent`;
|
|
12
|
+
|
|
13
|
+
// First, let's try the raw Google API call to see what the response looks like
|
|
14
|
+
const url = new URL(endpoint);
|
|
15
|
+
url.searchParams.set('key', apiKey);
|
|
16
|
+
|
|
17
|
+
const requestBody = {
|
|
18
|
+
contents: [
|
|
19
|
+
{
|
|
20
|
+
role: 'user',
|
|
21
|
+
parts: [{ text: 'Count from 1 to 3 briefly.' }]
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
generationConfig: {
|
|
25
|
+
responseMimeType: 'text/plain'
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
console.log('š” Making raw request to:', url.toString());
|
|
30
|
+
console.log('š¦ Request body:', JSON.stringify(requestBody, null, 2));
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const response = await request(url.toString(), {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: {
|
|
36
|
+
'Content-Type': 'application/json',
|
|
37
|
+
'Accept': 'text/event-stream',
|
|
38
|
+
},
|
|
39
|
+
body: JSON.stringify(requestBody)
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log('š Response status:', response.statusCode);
|
|
43
|
+
console.log('š Response headers:', response.headers);
|
|
44
|
+
|
|
45
|
+
if (response.statusCode >= 400) {
|
|
46
|
+
const errorText = await response.body.text();
|
|
47
|
+
console.error('ā Error response:', errorText);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
console.log('\nš Raw streaming response:');
|
|
52
|
+
console.log('---');
|
|
53
|
+
|
|
54
|
+
const decoder = new TextDecoder();
|
|
55
|
+
let chunkCount = 0;
|
|
56
|
+
|
|
57
|
+
for await (const chunk of response.body) {
|
|
58
|
+
chunkCount++;
|
|
59
|
+
const text = decoder.decode(chunk, { stream: true });
|
|
60
|
+
console.log(`[Raw Chunk ${chunkCount}]:`, JSON.stringify(text));
|
|
61
|
+
|
|
62
|
+
// Parse each line
|
|
63
|
+
const lines = text.split('\n').filter(line => line.trim());
|
|
64
|
+
for (const line of lines) {
|
|
65
|
+
console.log(`[Line]: "${line}"`);
|
|
66
|
+
|
|
67
|
+
if (line.startsWith('data: ')) {
|
|
68
|
+
const data = line.slice(6);
|
|
69
|
+
console.log(`[Data]: "${data}"`);
|
|
70
|
+
|
|
71
|
+
if (data !== '[DONE]') {
|
|
72
|
+
try {
|
|
73
|
+
const parsed = JSON.parse(data);
|
|
74
|
+
console.log('[Parsed JSON]:', JSON.stringify(parsed, null, 2));
|
|
75
|
+
|
|
76
|
+
// Check Google's response structure
|
|
77
|
+
if (parsed.candidates?.[0]?.content?.parts?.[0]?.text) {
|
|
78
|
+
console.log('[Content]:', parsed.candidates[0].content.parts[0].text);
|
|
79
|
+
}
|
|
80
|
+
} catch (parseError) {
|
|
81
|
+
console.log('[Parse Error]:', parseError);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
console.log('---');
|
|
89
|
+
console.log(`ā
Raw test completed with ${chunkCount} chunks`);
|
|
90
|
+
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error('ā Raw test failed:', error);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Run the debug test
|
|
97
|
+
debugGoogleStreaming();
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Debug tool calling execution
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { AIModelFactory, ToolBuilder } from './index';
|
|
6
|
+
|
|
7
|
+
async function debugToolCalling() {
|
|
8
|
+
console.log('š Debug Tool Calling Execution\n');
|
|
9
|
+
|
|
10
|
+
const model = AIModelFactory.createOllamaChatModel('qwen3:8b');
|
|
11
|
+
|
|
12
|
+
// Simple calculator tool
|
|
13
|
+
const calcTool = ToolBuilder.createTool<{ expression: string }>(
|
|
14
|
+
'calculator',
|
|
15
|
+
'Calculate mathematical expressions',
|
|
16
|
+
{
|
|
17
|
+
properties: {
|
|
18
|
+
expression: { type: 'string', description: 'Math expression to evaluate' }
|
|
19
|
+
},
|
|
20
|
+
required: ['expression']
|
|
21
|
+
},
|
|
22
|
+
(args) => {
|
|
23
|
+
console.log('š§ TOOL EXECUTING: calculator with', args);
|
|
24
|
+
try {
|
|
25
|
+
const result = Function(`"use strict"; return (${args.expression})`)();
|
|
26
|
+
return { expression: args.expression, result, answer: `${args.expression} = ${result}` };
|
|
27
|
+
} catch (error) {
|
|
28
|
+
return { expression: args.expression, error: 'Invalid expression', result: null };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
model.registerTool(calcTool.name, calcTool.description, calcTool.parameters, calcTool.handler);
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
await model.ensureReady();
|
|
37
|
+
|
|
38
|
+
console.log('š Test 1: Manual Tool Call Detection\n');
|
|
39
|
+
|
|
40
|
+
const response1 = await model.chat([
|
|
41
|
+
{ role: 'user', content: 'What is 5 + 3?' }
|
|
42
|
+
], {}, { tool_choice: 'auto' });
|
|
43
|
+
|
|
44
|
+
console.log('Response content:', response1.content);
|
|
45
|
+
console.log('Tool calls detected:', response1.tool_calls?.length || 0);
|
|
46
|
+
|
|
47
|
+
if (response1.tool_calls) {
|
|
48
|
+
console.log('Tool call details:');
|
|
49
|
+
response1.tool_calls.forEach((call, index) => {
|
|
50
|
+
console.log(` ${index + 1}. ${call.function.name}: ${call.function.arguments}`);
|
|
51
|
+
console.log(` Raw tool call:`, JSON.stringify(call, null, 2));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Manual tool execution
|
|
55
|
+
console.log('\nš§ Manual Tool Execution:');
|
|
56
|
+
for (const toolCall of response1.tool_calls) {
|
|
57
|
+
try {
|
|
58
|
+
const args = JSON.parse(toolCall.function.arguments);
|
|
59
|
+
console.log('Parsed arguments:', args);
|
|
60
|
+
const result = await calcTool.handler(args);
|
|
61
|
+
console.log('Tool result:', result);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.log('Tool execution error:', error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
console.log('\nš Test 2: Automatic Tool Execution\n');
|
|
69
|
+
|
|
70
|
+
const response2 = await model.chatWithTools([
|
|
71
|
+
{ role: 'user', content: 'Calculate 10 * 7 for me' }
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
console.log('Automatic response:', response2.content);
|
|
75
|
+
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error('ā Debug failed:', error);
|
|
78
|
+
} finally {
|
|
79
|
+
model.dispose();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Run the debug
|
|
84
|
+
if (require.main === module) {
|
|
85
|
+
debugToolCalling().catch(console.error);
|
|
86
|
+
}
|