universal-llm-client 4.1.0 → 4.3.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 (103) hide show
  1. package/CHANGELOG.md +139 -103
  2. package/LICENSE +21 -21
  3. package/README.md +591 -591
  4. package/dist/ai-model.js.map +1 -1
  5. package/dist/auditor.js.map +1 -1
  6. package/dist/client.js.map +1 -1
  7. package/dist/http.js.map +1 -1
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/interfaces.d.ts +20 -0
  13. package/dist/interfaces.d.ts.map +1 -1
  14. package/dist/interfaces.js.map +1 -1
  15. package/dist/mcp.js.map +1 -1
  16. package/dist/providers/anthropic.js.map +1 -1
  17. package/dist/providers/google.d.ts.map +1 -1
  18. package/dist/providers/google.js +2 -0
  19. package/dist/providers/google.js.map +1 -1
  20. package/dist/providers/index.js.map +1 -1
  21. package/dist/providers/ollama.js.map +1 -1
  22. package/dist/providers/openai.js.map +1 -1
  23. package/dist/router.js.map +1 -1
  24. package/dist/stream-decoder.js.map +1 -1
  25. package/dist/structured-output.d.ts +24 -1
  26. package/dist/structured-output.d.ts.map +1 -1
  27. package/dist/structured-output.js +58 -5
  28. package/dist/structured-output.js.map +1 -1
  29. package/dist/tools.js.map +1 -1
  30. package/dist/zod-adapter.js.map +1 -1
  31. package/package.json +115 -116
  32. package/src/ai-model.ts +0 -350
  33. package/src/auditor.ts +0 -213
  34. package/src/client.ts +0 -402
  35. package/src/debug/debug-google-streaming.ts +0 -97
  36. package/src/debug/debug-tool-execution.ts +0 -86
  37. package/src/debug/test-lmstudio-tools.ts +0 -155
  38. package/src/demos/README.md +0 -47
  39. package/src/demos/basic/universal-llm-examples.ts +0 -161
  40. package/src/demos/mcp/astrid-memory-demo.ts +0 -295
  41. package/src/demos/mcp/astrid-persona-memory.ts +0 -357
  42. package/src/demos/mcp/mcp-mongodb-demo.ts +0 -275
  43. package/src/demos/mcp/simple-astrid-memory.ts +0 -148
  44. package/src/demos/mcp/simple-mcp-demo.ts +0 -68
  45. package/src/demos/mcp/working-mcp-demo.ts +0 -62
  46. package/src/demos/model-alias-demo.ts +0 -0
  47. package/src/demos/tools/RAG_MEMORY_INTEGRATION.md +0 -267
  48. package/src/demos/tools/astrid-memory-demo.ts +0 -270
  49. package/src/demos/tools/astrid-production-memory-clean.ts +0 -785
  50. package/src/demos/tools/astrid-production-memory.ts +0 -558
  51. package/src/demos/tools/basic-translation-test.ts +0 -66
  52. package/src/demos/tools/chromadb-similarity-tuning.ts +0 -390
  53. package/src/demos/tools/clean-multilingual-conversation.ts +0 -209
  54. package/src/demos/tools/clean-translation-test.ts +0 -119
  55. package/src/demos/tools/clean-universal-multilingual-test.ts +0 -131
  56. package/src/demos/tools/complete-rag-demo.ts +0 -369
  57. package/src/demos/tools/complete-tool-demo.ts +0 -132
  58. package/src/demos/tools/demo-tool-calling.ts +0 -124
  59. package/src/demos/tools/dynamic-language-switching-test.ts +0 -251
  60. package/src/demos/tools/hybrid-thinking-test.ts +0 -154
  61. package/src/demos/tools/memory-integration-test.ts +0 -420
  62. package/src/demos/tools/multilingual-memory-system.ts +0 -802
  63. package/src/demos/tools/ondemand-translation-demo.ts +0 -655
  64. package/src/demos/tools/production-tool-demo.ts +0 -245
  65. package/src/demos/tools/revolutionary-multilingual-test.ts +0 -151
  66. package/src/demos/tools/rigorous-language-analysis.ts +0 -218
  67. package/src/demos/tools/test-universal-memory-system.ts +0 -126
  68. package/src/demos/tools/translation-integration-guide.ts +0 -346
  69. package/src/demos/tools/universal-memory-system.ts +0 -560
  70. package/src/http.ts +0 -247
  71. package/src/index.ts +0 -160
  72. package/src/interfaces.ts +0 -657
  73. package/src/mcp.ts +0 -345
  74. package/src/providers/anthropic.ts +0 -762
  75. package/src/providers/google.ts +0 -620
  76. package/src/providers/index.ts +0 -8
  77. package/src/providers/ollama.ts +0 -469
  78. package/src/providers/openai.ts +0 -392
  79. package/src/router.ts +0 -780
  80. package/src/stream-decoder.ts +0 -361
  81. package/src/structured-output.ts +0 -702
  82. package/src/test-scripts/test-advanced-tools.ts +0 -310
  83. package/src/test-scripts/test-google-streaming-enhanced.ts +0 -147
  84. package/src/test-scripts/test-google-streaming.ts +0 -63
  85. package/src/test-scripts/test-google-system-prompt-comprehensive.ts +0 -189
  86. package/src/test-scripts/test-mcp-config.ts +0 -28
  87. package/src/test-scripts/test-mcp-connection.ts +0 -29
  88. package/src/test-scripts/test-system-message-positions.ts +0 -163
  89. package/src/test-scripts/test-system-prompt-improvement-demo.ts +0 -83
  90. package/src/test-scripts/test-tool-calling.ts +0 -231
  91. package/src/tests/ai-model.test.ts +0 -1614
  92. package/src/tests/auditor.test.ts +0 -224
  93. package/src/tests/http.test.ts +0 -200
  94. package/src/tests/interfaces.test.ts +0 -117
  95. package/src/tests/providers/google.test.ts +0 -660
  96. package/src/tests/providers/ollama.test.ts +0 -954
  97. package/src/tests/providers/openai.test.ts +0 -1122
  98. package/src/tests/router.test.ts +0 -254
  99. package/src/tests/stream-decoder.test.ts +0 -179
  100. package/src/tests/structured-output.test.ts +0 -1340
  101. package/src/tests/tools.test.ts +0 -175
  102. package/src/tools.ts +0 -246
  103. package/src/zod-adapter.ts +0 -72
@@ -1,175 +0,0 @@
1
- /**
2
- * Tests for tools.ts — ToolBuilder and ToolExecutor
3
- */
4
- import { describe, it, expect } from 'bun:test';
5
- import { ToolBuilder, ToolExecutor, createTimeTool, createRandomNumberTool } from '../tools.js';
6
-
7
- describe('ToolBuilder', () => {
8
- it('builds a basic tool definition', () => {
9
- const tool = new ToolBuilder('test_tool')
10
- .description('A test tool')
11
- .build();
12
-
13
- expect(tool.type).toBe('function');
14
- expect(tool.function.name).toBe('test_tool');
15
- expect(tool.function.description).toBe('A test tool');
16
- expect(tool.function.parameters.type).toBe('object');
17
- });
18
-
19
- it('adds required parameters', () => {
20
- const tool = new ToolBuilder('search')
21
- .description('Search')
22
- .addParameter('query', 'string', 'Search query', true)
23
- .build();
24
-
25
- expect(tool.function.parameters.properties).toHaveProperty('query');
26
- expect(tool.function.parameters.required).toEqual(['query']);
27
- });
28
-
29
- it('adds optional parameters', () => {
30
- const tool = new ToolBuilder('search')
31
- .description('Search')
32
- .addParameter('query', 'string', 'Search query', true)
33
- .addParameter('limit', 'number', 'Max results', false)
34
- .build();
35
-
36
- expect(tool.function.parameters.properties).toHaveProperty('limit');
37
- expect(tool.function.parameters.required).toEqual(['query']);
38
- });
39
-
40
- it('adds parameters with extra schema properties', () => {
41
- const tool = new ToolBuilder('config')
42
- .description('Configure')
43
- .addParameter('theme', 'string', 'Color theme', false, {
44
- enum: ['light', 'dark'],
45
- })
46
- .build();
47
-
48
- const themeParam = (tool.function.parameters.properties as Record<string, Record<string, unknown>>)?.['theme'];
49
- expect(themeParam?.enum).toEqual(['light', 'dark']);
50
- });
51
-
52
- it('builds function definition only', () => {
53
- const fn = new ToolBuilder('test')
54
- .description('Test')
55
- .buildFunction();
56
-
57
- expect(fn.name).toBe('test');
58
- expect(fn.description).toBe('Test');
59
- });
60
- });
61
-
62
- describe('ToolExecutor', () => {
63
- describe('withTimeout', () => {
64
- it('resolves if handler completes in time', async () => {
65
- const handler = async () => 'result';
66
- const wrapped = ToolExecutor.withTimeout(handler, 1000);
67
- expect(await wrapped({})).toBe('result');
68
- });
69
-
70
- it('rejects if handler exceeds timeout', async () => {
71
- const handler = async () => {
72
- await new Promise(r => setTimeout(r, 500));
73
- return 'too late';
74
- };
75
- const wrapped = ToolExecutor.withTimeout(handler, 50);
76
- expect(wrapped({})).rejects.toThrow('timeout');
77
- });
78
- });
79
-
80
- describe('safe', () => {
81
- it('returns result on success', async () => {
82
- const handler = async () => 42;
83
- const wrapped = ToolExecutor.safe(handler);
84
- expect(await wrapped({})).toBe(42);
85
- });
86
-
87
- it('catches errors and returns error object', async () => {
88
- const handler = async () => { throw new Error('boom'); };
89
- const wrapped = ToolExecutor.safe(handler);
90
- const result = await wrapped({}) as { error: string };
91
- expect(result.error).toBe('boom');
92
- });
93
- });
94
-
95
- describe('withValidation', () => {
96
- it('passes through if all required fields present', async () => {
97
- const handler = async (args: unknown) => args;
98
- const wrapped = ToolExecutor.withValidation(handler, ['name']);
99
- expect(await wrapped({ name: 'test' })).toEqual({ name: 'test' });
100
- });
101
-
102
- it('throws if required field is missing', async () => {
103
- const handler = async () => 'ok';
104
- const wrapped = ToolExecutor.withValidation(handler, ['name']);
105
- expect(wrapped({ other: 'value' })).rejects.toThrow('Missing required argument: name');
106
- });
107
-
108
- it('throws if args is not an object', async () => {
109
- const handler = async () => 'ok';
110
- const wrapped = ToolExecutor.withValidation(handler, ['name']);
111
- expect(wrapped(null)).rejects.toThrow('must be an object');
112
- });
113
- });
114
-
115
- describe('timed', () => {
116
- it('returns result with duration', async () => {
117
- const handler = async () => 'hello';
118
- const wrapped = ToolExecutor.timed(handler);
119
- const result = await wrapped({}) as { result: string; duration: number };
120
- expect(result.result).toBe('hello');
121
- expect(result.duration).toBeGreaterThanOrEqual(0);
122
- });
123
- });
124
-
125
- describe('compose', () => {
126
- it('composes multiple wrappers', async () => {
127
- const handler = async () => 'result';
128
- const composed = ToolExecutor.compose(
129
- handler,
130
- h => ToolExecutor.safe(h),
131
- h => ToolExecutor.withTimeout(h, 5000),
132
- );
133
- expect(await composed({})).toBe('result');
134
- });
135
- });
136
- });
137
-
138
- describe('Common Tools', () => {
139
- describe('createTimeTool', () => {
140
- it('returns a valid tool definition', () => {
141
- const tool = createTimeTool();
142
- expect(tool.name).toBe('get_current_time');
143
- expect(tool.description).toBeTruthy();
144
- expect(tool.parameters.type).toBe('object');
145
- });
146
-
147
- it('handler returns time info', async () => {
148
- const tool = createTimeTool();
149
- const result = await tool.handler({}) as { iso: string; timestamp: number };
150
- expect(result.iso).toBeTruthy();
151
- expect(result.timestamp).toBeGreaterThan(0);
152
- });
153
-
154
- it('handler respects timezone parameter', async () => {
155
- const tool = createTimeTool();
156
- const result = await tool.handler({ timezone: 'America/New_York' }) as { timezone: string };
157
- expect(result.timezone).toBe('America/New_York');
158
- });
159
- });
160
-
161
- describe('createRandomNumberTool', () => {
162
- it('returns a valid tool definition', () => {
163
- const tool = createRandomNumberTool();
164
- expect(tool.name).toBe('random_number');
165
- expect(tool.description).toBeTruthy();
166
- });
167
-
168
- it('handler returns number in range', async () => {
169
- const tool = createRandomNumberTool();
170
- const result = await tool.handler({ min: 1, max: 10 }) as { value: number };
171
- expect(result.value).toBeGreaterThanOrEqual(1);
172
- expect(result.value).toBeLessThanOrEqual(10);
173
- });
174
- });
175
- });
package/src/tools.ts DELETED
@@ -1,246 +0,0 @@
1
- /**
2
- * Universal LLM Client v3 — Tool Utilities
3
- *
4
- * ToolBuilder: Type-safe tool definition builder with fluent API.
5
- * ToolExecutor: Execution wrappers with timeout and validation.
6
- */
7
-
8
- import type { LLMFunction, LLMToolDefinition, ToolHandler } from './interfaces.js';
9
-
10
- // ============================================================================
11
- // ToolBuilder
12
- // ============================================================================
13
-
14
- /**
15
- * Fluent builder for LLM tool definitions.
16
- *
17
- * Usage:
18
- * const tool = new ToolBuilder('get_weather')
19
- * .description('Get current weather for a location')
20
- * .addParameter('location', 'string', 'City name', true)
21
- * .addParameter('units', 'string', 'Temperature units', false, { enum: ['celsius', 'fahrenheit'] })
22
- * .build();
23
- */
24
- export class ToolBuilder {
25
- private name: string;
26
- private desc: string = '';
27
- private properties: Record<string, unknown> = {};
28
- private required: string[] = [];
29
-
30
- constructor(name: string) {
31
- this.name = name;
32
- }
33
-
34
- description(desc: string): this {
35
- this.desc = desc;
36
- return this;
37
- }
38
-
39
- addParameter(
40
- name: string,
41
- type: string,
42
- description: string,
43
- isRequired: boolean = false,
44
- extra?: Record<string, unknown>,
45
- ): this {
46
- this.properties[name] = {
47
- type,
48
- description,
49
- ...extra,
50
- };
51
- if (isRequired) {
52
- this.required.push(name);
53
- }
54
- return this;
55
- }
56
-
57
- build(): LLMToolDefinition {
58
- return {
59
- type: 'function',
60
- function: {
61
- name: this.name,
62
- description: this.desc,
63
- parameters: {
64
- type: 'object',
65
- properties: this.properties,
66
- required: this.required.length > 0 ? this.required : undefined,
67
- },
68
- },
69
- };
70
- }
71
-
72
- /** Build and return the function definition only */
73
- buildFunction(): LLMFunction {
74
- return this.build().function;
75
- }
76
- }
77
-
78
- // ============================================================================
79
- // ToolExecutor
80
- // ============================================================================
81
-
82
- /**
83
- * Utility wrappers for creating safe tool handlers.
84
- */
85
- export class ToolExecutor {
86
- /**
87
- * Wrap a handler with a timeout.
88
- * Rejects if the handler doesn't complete within the specified ms.
89
- */
90
- static withTimeout(handler: ToolHandler, timeoutMs: number): ToolHandler {
91
- return async (args: unknown) => {
92
- const result = await Promise.race([
93
- Promise.resolve(handler(args)),
94
- new Promise<never>((_, reject) =>
95
- setTimeout(() => reject(new Error(`Tool execution timeout after ${timeoutMs}ms`)), timeoutMs),
96
- ),
97
- ]);
98
- return result;
99
- };
100
- }
101
-
102
- /**
103
- * Wrap a handler to catch errors and return them as strings
104
- * instead of throwing.
105
- */
106
- static safe(handler: ToolHandler): ToolHandler {
107
- return async (args: unknown) => {
108
- try {
109
- return await handler(args);
110
- } catch (error) {
111
- return {
112
- error: error instanceof Error ? error.message : String(error),
113
- };
114
- }
115
- };
116
- }
117
-
118
- /**
119
- * Wrap a handler with argument validation.
120
- * Checks that required fields are present before execution.
121
- */
122
- static withValidation(
123
- handler: ToolHandler,
124
- requiredFields: string[],
125
- ): ToolHandler {
126
- return async (args: unknown) => {
127
- if (!args || typeof args !== 'object') {
128
- throw new Error('Tool arguments must be an object');
129
- }
130
- const obj = args as Record<string, unknown>;
131
- for (const field of requiredFields) {
132
- if (obj[field] === undefined || obj[field] === null) {
133
- throw new Error(`Missing required argument: ${field}`);
134
- }
135
- }
136
- return handler(args);
137
- };
138
- }
139
-
140
- /**
141
- * Create a handler that measures execution time and
142
- * returns both the result and duration.
143
- */
144
- static timed(handler: ToolHandler): ToolHandler {
145
- return async (args: unknown) => {
146
- const start = Date.now();
147
- const result = await handler(args);
148
- return {
149
- result,
150
- duration: Date.now() - start,
151
- };
152
- };
153
- }
154
-
155
- /**
156
- * Compose multiple wrappers around a handler.
157
- * Applied from right to left (innermost to outermost).
158
- */
159
- static compose(
160
- handler: ToolHandler,
161
- ...wrappers: Array<(h: ToolHandler) => ToolHandler>
162
- ): ToolHandler {
163
- return wrappers.reduceRight((h, wrapper) => wrapper(h), handler);
164
- }
165
- }
166
-
167
- // ============================================================================
168
- // Common Tool Definitions
169
- // ============================================================================
170
-
171
- /**
172
- * Create a get_current_time tool definition and handler.
173
- */
174
- export function createTimeTool(): {
175
- name: string;
176
- description: string;
177
- parameters: LLMFunction['parameters'];
178
- handler: ToolHandler;
179
- } {
180
- return {
181
- name: 'get_current_time',
182
- description: 'Get the current date and time',
183
- parameters: {
184
- type: 'object',
185
- properties: {
186
- timezone: {
187
- type: 'string',
188
- description: 'IANA timezone (e.g. "America/New_York"). Defaults to UTC.',
189
- },
190
- },
191
- },
192
- handler: (args: unknown) => {
193
- const { timezone } = (args ?? {}) as { timezone?: string };
194
- const now = new Date();
195
- try {
196
- return {
197
- iso: now.toISOString(),
198
- formatted: now.toLocaleString('en-US', {
199
- timeZone: timezone || 'UTC',
200
- dateStyle: 'full',
201
- timeStyle: 'long',
202
- }),
203
- timezone: timezone || 'UTC',
204
- timestamp: now.getTime(),
205
- };
206
- } catch {
207
- return {
208
- iso: now.toISOString(),
209
- formatted: now.toUTCString(),
210
- timezone: 'UTC',
211
- timestamp: now.getTime(),
212
- };
213
- }
214
- },
215
- };
216
- }
217
-
218
- /**
219
- * Create a random_number tool definition and handler.
220
- */
221
- export function createRandomNumberTool(): {
222
- name: string;
223
- description: string;
224
- parameters: LLMFunction['parameters'];
225
- handler: ToolHandler;
226
- } {
227
- return {
228
- name: 'random_number',
229
- description: 'Generate a random number within a range',
230
- parameters: {
231
- type: 'object',
232
- properties: {
233
- min: { type: 'number', description: 'Minimum value (default 0)' },
234
- max: { type: 'number', description: 'Maximum value (default 100)' },
235
- },
236
- },
237
- handler: (args: unknown) => {
238
- const { min = 0, max = 100 } = (args ?? {}) as { min?: number; max?: number };
239
- return {
240
- value: Math.floor(Math.random() * (max - min + 1)) + min,
241
- min,
242
- max,
243
- };
244
- },
245
- };
246
- }
@@ -1,72 +0,0 @@
1
- /**
2
- * Zod Adapter for Universal LLM Client
3
- *
4
- * Optional entrypoint for projects that use Zod for schema validation.
5
- * Import from 'universal-llm-client/zod' to use.
6
- *
7
- * @module universal-llm-client/zod
8
- */
9
-
10
- import { z } from 'zod';
11
- import type { SchemaConfig } from './structured-output.js';
12
-
13
- /**
14
- * Create a SchemaConfig from a Zod schema.
15
- *
16
- * This bridges Zod's type-safe schema definitions to the library's
17
- * generic SchemaConfig interface, using Zod 4's native `z.toJSONSchema()`.
18
- *
19
- * @template T The type inferred from the Zod schema
20
- * @param schema The Zod schema
21
- * @param options Optional name and description for LLM guidance
22
- * @returns SchemaConfig ready for use with generateStructured, etc.
23
- *
24
- * @example
25
- * ```typescript
26
- * import { fromZod } from 'universal-llm-client/zod';
27
- * import { z } from 'zod';
28
- *
29
- * const UserSchema = z.object({
30
- * name: z.string(),
31
- * age: z.number(),
32
- * });
33
- *
34
- * const config = fromZod(UserSchema, { name: 'User' });
35
- *
36
- * const user = await model.generateStructured(config, messages);
37
- * // user.name: string, user.age: number (fully typed)
38
- * ```
39
- */
40
- export function fromZod<T>(
41
- schema: z.ZodType<T>,
42
- options?: { name?: string; description?: string },
43
- ): SchemaConfig<T> {
44
- // Convert Zod schema to JSON Schema using Zod 4's native method
45
- const rawJsonSchema = z.toJSONSchema(schema, {
46
- target: 'draft-07',
47
- unrepresentable: 'any',
48
- });
49
-
50
- // Clean up — remove $schema since providers don't need it
51
- const jsonSchema = { ...rawJsonSchema } as Record<string, unknown>;
52
- delete jsonSchema.$schema;
53
-
54
- return {
55
- jsonSchema: jsonSchema as import('./structured-output.js').JSONSchema,
56
- validate: (data: unknown): T => {
57
- const result = schema.safeParse(data);
58
- if (!result.success) {
59
- throw result.error;
60
- }
61
- return result.data;
62
- },
63
- name: options?.name,
64
- description: options?.description,
65
- };
66
- }
67
-
68
- // Re-export z for convenience (users importing from /zod likely want it)
69
- export { z } from 'zod';
70
-
71
- // Re-export SchemaConfig for type usage
72
- export type { SchemaConfig } from './structured-output.js';