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.
Files changed (127) hide show
  1. package/dist/ai-model.d.ts +20 -22
  2. package/dist/ai-model.d.ts.map +1 -1
  3. package/dist/ai-model.js +26 -23
  4. package/dist/ai-model.js.map +1 -1
  5. package/dist/client.d.ts +5 -5
  6. package/dist/client.d.ts.map +1 -1
  7. package/dist/client.js +17 -9
  8. package/dist/client.js.map +1 -1
  9. package/dist/http.d.ts +2 -0
  10. package/dist/http.d.ts.map +1 -1
  11. package/dist/http.js +1 -0
  12. package/dist/http.js.map +1 -1
  13. package/dist/index.d.ts +3 -3
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +4 -4
  16. package/dist/index.js.map +1 -1
  17. package/dist/interfaces.d.ts +49 -11
  18. package/dist/interfaces.d.ts.map +1 -1
  19. package/dist/interfaces.js +14 -0
  20. package/dist/interfaces.js.map +1 -1
  21. package/dist/providers/anthropic.d.ts +56 -0
  22. package/dist/providers/anthropic.d.ts.map +1 -0
  23. package/dist/providers/anthropic.js +524 -0
  24. package/dist/providers/anthropic.js.map +1 -0
  25. package/dist/providers/google.d.ts +5 -0
  26. package/dist/providers/google.d.ts.map +1 -1
  27. package/dist/providers/google.js +64 -8
  28. package/dist/providers/google.js.map +1 -1
  29. package/dist/providers/index.d.ts +1 -0
  30. package/dist/providers/index.d.ts.map +1 -1
  31. package/dist/providers/index.js +1 -0
  32. package/dist/providers/index.js.map +1 -1
  33. package/dist/providers/ollama.d.ts.map +1 -1
  34. package/dist/providers/ollama.js +38 -11
  35. package/dist/providers/ollama.js.map +1 -1
  36. package/dist/providers/openai.d.ts.map +1 -1
  37. package/dist/providers/openai.js +9 -7
  38. package/dist/providers/openai.js.map +1 -1
  39. package/dist/router.d.ts +13 -33
  40. package/dist/router.d.ts.map +1 -1
  41. package/dist/router.js +33 -57
  42. package/dist/router.js.map +1 -1
  43. package/dist/stream-decoder.d.ts +29 -2
  44. package/dist/stream-decoder.d.ts.map +1 -1
  45. package/dist/stream-decoder.js +39 -11
  46. package/dist/stream-decoder.js.map +1 -1
  47. package/dist/structured-output.d.ts +107 -181
  48. package/dist/structured-output.d.ts.map +1 -1
  49. package/dist/structured-output.js +137 -192
  50. package/dist/structured-output.js.map +1 -1
  51. package/dist/zod-adapter.d.ts +44 -0
  52. package/dist/zod-adapter.d.ts.map +1 -0
  53. package/dist/zod-adapter.js +61 -0
  54. package/dist/zod-adapter.js.map +1 -0
  55. package/package.json +9 -1
  56. package/src/ai-model.ts +350 -0
  57. package/src/auditor.ts +213 -0
  58. package/src/client.ts +402 -0
  59. package/src/debug/debug-google-streaming.ts +97 -0
  60. package/src/debug/debug-tool-execution.ts +86 -0
  61. package/src/debug/test-lmstudio-tools.ts +155 -0
  62. package/src/demos/README.md +47 -0
  63. package/src/demos/basic/universal-llm-examples.ts +161 -0
  64. package/src/demos/mcp/astrid-memory-demo.ts +295 -0
  65. package/src/demos/mcp/astrid-persona-memory.ts +357 -0
  66. package/src/demos/mcp/mcp-mongodb-demo.ts +275 -0
  67. package/src/demos/mcp/simple-astrid-memory.ts +148 -0
  68. package/src/demos/mcp/simple-mcp-demo.ts +68 -0
  69. package/src/demos/mcp/working-mcp-demo.ts +62 -0
  70. package/src/demos/model-alias-demo.ts +0 -0
  71. package/src/demos/tools/RAG_MEMORY_INTEGRATION.md +267 -0
  72. package/src/demos/tools/astrid-memory-demo.ts +270 -0
  73. package/src/demos/tools/astrid-production-memory-clean.ts +785 -0
  74. package/src/demos/tools/astrid-production-memory.ts +558 -0
  75. package/src/demos/tools/basic-translation-test.ts +66 -0
  76. package/src/demos/tools/chromadb-similarity-tuning.ts +390 -0
  77. package/src/demos/tools/clean-multilingual-conversation.ts +209 -0
  78. package/src/demos/tools/clean-translation-test.ts +119 -0
  79. package/src/demos/tools/clean-universal-multilingual-test.ts +131 -0
  80. package/src/demos/tools/complete-rag-demo.ts +369 -0
  81. package/src/demos/tools/complete-tool-demo.ts +132 -0
  82. package/src/demos/tools/demo-tool-calling.ts +124 -0
  83. package/src/demos/tools/dynamic-language-switching-test.ts +251 -0
  84. package/src/demos/tools/hybrid-thinking-test.ts +154 -0
  85. package/src/demos/tools/memory-integration-test.ts +420 -0
  86. package/src/demos/tools/multilingual-memory-system.ts +802 -0
  87. package/src/demos/tools/ondemand-translation-demo.ts +655 -0
  88. package/src/demos/tools/production-tool-demo.ts +245 -0
  89. package/src/demos/tools/revolutionary-multilingual-test.ts +151 -0
  90. package/src/demos/tools/rigorous-language-analysis.ts +218 -0
  91. package/src/demos/tools/test-universal-memory-system.ts +126 -0
  92. package/src/demos/tools/translation-integration-guide.ts +346 -0
  93. package/src/demos/tools/universal-memory-system.ts +560 -0
  94. package/src/http.ts +247 -0
  95. package/src/index.ts +161 -0
  96. package/src/interfaces.ts +657 -0
  97. package/src/mcp.ts +345 -0
  98. package/src/providers/anthropic.ts +762 -0
  99. package/src/providers/google.ts +620 -0
  100. package/src/providers/index.ts +8 -0
  101. package/src/providers/ollama.ts +469 -0
  102. package/src/providers/openai.ts +392 -0
  103. package/src/router.ts +780 -0
  104. package/src/stream-decoder.ts +361 -0
  105. package/src/structured-output.ts +759 -0
  106. package/src/test-scripts/test-advanced-tools.ts +310 -0
  107. package/src/test-scripts/test-google-streaming-enhanced.ts +147 -0
  108. package/src/test-scripts/test-google-streaming.ts +63 -0
  109. package/src/test-scripts/test-google-system-prompt-comprehensive.ts +189 -0
  110. package/src/test-scripts/test-mcp-config.ts +28 -0
  111. package/src/test-scripts/test-mcp-connection.ts +29 -0
  112. package/src/test-scripts/test-system-message-positions.ts +163 -0
  113. package/src/test-scripts/test-system-prompt-improvement-demo.ts +83 -0
  114. package/src/test-scripts/test-tool-calling.ts +231 -0
  115. package/src/tests/ai-model.test.ts +1614 -0
  116. package/src/tests/auditor.test.ts +224 -0
  117. package/src/tests/http.test.ts +200 -0
  118. package/src/tests/interfaces.test.ts +117 -0
  119. package/src/tests/providers/google.test.ts +660 -0
  120. package/src/tests/providers/ollama.test.ts +954 -0
  121. package/src/tests/providers/openai.test.ts +1122 -0
  122. package/src/tests/router.test.ts +254 -0
  123. package/src/tests/stream-decoder.test.ts +179 -0
  124. package/src/tests/structured-output.test.ts +1450 -0
  125. package/src/tests/tools.test.ts +175 -0
  126. package/src/tools.ts +246 -0
  127. 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
+ }