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
package/src/http.ts DELETED
@@ -1,247 +0,0 @@
1
- /**
2
- * Universal LLM Client v3 — HTTP Utilities
3
- *
4
- * Zero-dependency HTTP layer using native fetch.
5
- * Works on Node 22+, Bun, Deno, and browsers.
6
- */
7
-
8
- import type { LLMClientOptions } from './interfaces.js';
9
-
10
- // ============================================================================
11
- // Types
12
- // ============================================================================
13
-
14
- export interface HttpRequestOptions {
15
- method?: 'GET' | 'POST';
16
- headers?: Record<string, string>;
17
- body?: unknown;
18
- timeout?: number;
19
- signal?: AbortSignal;
20
- }
21
-
22
- export interface HttpResponse<T = unknown> {
23
- ok: boolean;
24
- status: number;
25
- data: T;
26
- /** Response headers (when available) */
27
- headers?: Headers;
28
- }
29
-
30
- // ============================================================================
31
- // HTTP Request
32
- // ============================================================================
33
-
34
- /**
35
- * Make an HTTP request with timeout and error handling.
36
- * Uses native fetch (available in all target runtimes).
37
- */
38
- export async function httpRequest<T = unknown>(
39
- url: string,
40
- options: HttpRequestOptions = {},
41
- ): Promise<HttpResponse<T>> {
42
- const { method = 'GET', headers = {}, body, timeout = 30000, signal } = options;
43
-
44
- const controller = new AbortController();
45
- const timeoutId = setTimeout(() => controller.abort(), timeout);
46
-
47
- // Combine external signal with timeout
48
- const combinedSignal = signal
49
- ? AbortSignal.any([signal, controller.signal])
50
- : controller.signal;
51
-
52
- try {
53
- const response = await fetch(url, {
54
- method,
55
- headers: {
56
- 'Content-Type': 'application/json',
57
- ...headers,
58
- },
59
- body: body ? JSON.stringify(body) : undefined,
60
- signal: combinedSignal,
61
- });
62
-
63
- clearTimeout(timeoutId);
64
-
65
- if (!response.ok) {
66
- const errorText = await response.text().catch(() => 'Unknown error');
67
- throw new Error(`HTTP ${response.status}: ${errorText}`);
68
- }
69
-
70
- const data = (await response.json()) as T;
71
-
72
- return {
73
- ok: response.ok,
74
- status: response.status,
75
- data,
76
- headers: response.headers,
77
- };
78
- } catch (error) {
79
- clearTimeout(timeoutId);
80
-
81
- if (error instanceof Error && error.name === 'AbortError') {
82
- throw new Error(`Request timeout after ${timeout}ms: ${url}`);
83
- }
84
- throw error;
85
- }
86
- }
87
-
88
- // ============================================================================
89
- // Streaming HTTP
90
- // ============================================================================
91
-
92
- /**
93
- * Make a streaming HTTP request.
94
- * Yields raw string chunks as they arrive.
95
- */
96
- export async function* httpStream(
97
- url: string,
98
- options: HttpRequestOptions = {},
99
- ): AsyncGenerator<string, void, unknown> {
100
- const { method = 'POST', headers = {}, body, timeout = 120000, signal } = options;
101
-
102
- const controller = new AbortController();
103
- const timeoutId = setTimeout(() => controller.abort(), timeout);
104
-
105
- const combinedSignal = signal
106
- ? AbortSignal.any([signal, controller.signal])
107
- : controller.signal;
108
-
109
- try {
110
- const response = await fetch(url, {
111
- method,
112
- headers: {
113
- 'Content-Type': 'application/json',
114
- ...headers,
115
- },
116
- body: body ? JSON.stringify(body) : undefined,
117
- signal: combinedSignal,
118
- });
119
-
120
- clearTimeout(timeoutId);
121
-
122
- if (!response.ok) {
123
- const errorText = await response.text().catch(() => 'Unknown error');
124
- throw new Error(`HTTP ${response.status}: ${errorText}`);
125
- }
126
-
127
- if (!response.body) {
128
- throw new Error('No response body for streaming');
129
- }
130
-
131
- const reader = response.body.getReader();
132
- const decoder = new TextDecoder();
133
-
134
- try {
135
- while (true) {
136
- const { done, value } = await reader.read();
137
- if (done) break;
138
- yield decoder.decode(value, { stream: true });
139
- }
140
- } finally {
141
- reader.releaseLock();
142
- }
143
- } catch (error) {
144
- clearTimeout(timeoutId);
145
-
146
- if (error instanceof Error && error.name === 'AbortError') {
147
- throw new Error(`Stream timeout after ${timeout}ms: ${url}`);
148
- }
149
- throw error;
150
- }
151
- }
152
-
153
- // ============================================================================
154
- // Protocol Parsers
155
- // ============================================================================
156
-
157
- /**
158
- * Parse NDJSON (newline-delimited JSON) stream.
159
- * Used by Ollama's streaming API.
160
- */
161
- export async function* parseNDJSON<T = unknown>(
162
- stream: AsyncGenerator<string>,
163
- ): AsyncGenerator<T, void, unknown> {
164
- let buffer = '';
165
-
166
- for await (const chunk of stream) {
167
- buffer += chunk;
168
- const lines = buffer.split('\n');
169
- buffer = lines.pop() ?? '';
170
-
171
- for (const line of lines) {
172
- const trimmed = line.trim();
173
- if (!trimmed) continue;
174
-
175
- try {
176
- yield JSON.parse(trimmed) as T;
177
- } catch {
178
- // Skip invalid JSON lines
179
- }
180
- }
181
- }
182
-
183
- // Handle remaining buffer
184
- if (buffer.trim()) {
185
- try {
186
- yield JSON.parse(buffer) as T;
187
- } catch {
188
- // Skip invalid JSON
189
- }
190
- }
191
- }
192
-
193
- /**
194
- * Parse Server-Sent Events stream.
195
- * Used by OpenAI-compatible APIs and LlamaCpp/vLLM.
196
- */
197
- export async function* parseSSE(
198
- stream: AsyncGenerator<string>,
199
- ): AsyncGenerator<{ event?: string; data: string }, void, unknown> {
200
- let buffer = '';
201
-
202
- for await (const chunk of stream) {
203
- buffer += chunk;
204
-
205
- // Split by double newlines (SSE event delimiter)
206
- const events = buffer.split('\n\n');
207
- buffer = events.pop() ?? '';
208
-
209
- for (const event of events) {
210
- const lines = event.split('\n');
211
- let eventType: string | undefined;
212
- const dataLines: string[] = [];
213
-
214
- for (const line of lines) {
215
- if (line.startsWith('event:')) {
216
- eventType = line.slice(6).trim();
217
- } else if (line.startsWith('data:')) {
218
- dataLines.push(line.slice(5).trim());
219
- }
220
- }
221
-
222
- const data = dataLines.join('\n');
223
- if (data && data !== '[DONE]') {
224
- yield { event: eventType, data };
225
- }
226
- }
227
- }
228
- }
229
-
230
- // ============================================================================
231
- // Header Utilities
232
- // ============================================================================
233
-
234
- /**
235
- * Build standard headers for LLM API requests.
236
- */
237
- export function buildHeaders(options: LLMClientOptions): Record<string, string> {
238
- const headers: Record<string, string> = {
239
- 'Content-Type': 'application/json',
240
- };
241
-
242
- if (options.apiKey) {
243
- headers['Authorization'] = `Bearer ${options.apiKey}`;
244
- }
245
-
246
- return headers;
247
- }
package/src/index.ts DELETED
@@ -1,160 +0,0 @@
1
- /**
2
- * Universal LLM Client v3
3
- *
4
- * A universal LLM client with transparent provider failover,
5
- * streaming tool execution, pluggable reasoning, and native observability.
6
- *
7
- * @module universal-llm-client
8
- */
9
-
10
- // ============================================================================
11
- // Public API — The Universal Client
12
- // ============================================================================
13
-
14
- export { AIModel } from './ai-model.js';
15
-
16
- // ============================================================================
17
- // Types & Interfaces
18
- // ============================================================================
19
-
20
- export {
21
- // Enums
22
- AIModelApiType,
23
- AIModelType,
24
- // Config
25
- type AIModelConfig,
26
- type ProviderConfig,
27
- type LLMClientOptions,
28
- // Messages
29
- type LLMChatMessage,
30
- type LLMMessageContent,
31
- type LLMContentPart,
32
- type LLMTextContent,
33
- type LLMImageContent,
34
- type LLMAudioContent,
35
- // Responses
36
- type LLMChatResponse,
37
- type TokenUsageInfo,
38
- // Tools
39
- type LLMToolCall,
40
- type LLMToolDefinition,
41
- type LLMFunction,
42
- type ToolHandler,
43
- type ToolExecutionResult,
44
- type ToolRegistry,
45
- type ToolRegistryEntry,
46
- // Options
47
- type ChatOptions,
48
- type ResponseFormat,
49
- type OutputOptions,
50
- // Model info
51
- type ModelMetadata,
52
- // Helpers
53
- textContent,
54
- imageContent,
55
- multimodalMessage,
56
- extractTextContent,
57
- hasImages,
58
- audioContent,
59
- hasAudio,
60
- } from './interfaces.js';
61
-
62
- // ============================================================================
63
- // Observability
64
- // ============================================================================
65
-
66
- export {
67
- type Auditor,
68
- type AuditEvent,
69
- type AuditEventType,
70
- NoopAuditor,
71
- ConsoleAuditor,
72
- BufferedAuditor,
73
- } from './auditor.js';
74
-
75
- // ============================================================================
76
- // Stream Decoding
77
- // ============================================================================
78
-
79
- export {
80
- type StreamDecoder,
81
- type DecodedEvent,
82
- type DecoderCallback,
83
- type DecoderType,
84
- type DecoderOptions,
85
- type DecoderFactory,
86
- createDecoder,
87
- registerDecoder,
88
- getRegisteredDecoders,
89
- PassthroughDecoder,
90
- StandardChatDecoder,
91
- InterleavedReasoningDecoder,
92
- } from './stream-decoder.js';
93
-
94
- // ============================================================================
95
- // Tool Utilities
96
- // ============================================================================
97
-
98
- export {
99
- ToolBuilder,
100
- ToolExecutor,
101
- createTimeTool,
102
- createRandomNumberTool,
103
- } from './tools.js';
104
-
105
- // ============================================================================
106
- // HTTP Utilities (for advanced use cases)
107
- // ============================================================================
108
-
109
- export {
110
- httpRequest,
111
- httpStream,
112
- parseNDJSON,
113
- parseSSE,
114
- buildHeaders,
115
- type HttpRequestOptions,
116
- type HttpResponse,
117
- } from './http.js';
118
-
119
- // ============================================================================
120
- // MCP Integration
121
- // ============================================================================
122
-
123
- export {
124
- MCPToolBridge,
125
- type MCPBridgeConfig,
126
- type MCPServerConfig,
127
- type MCPTool,
128
- } from './mcp.js';
129
-
130
- // ============================================================================
131
- // Structured Output
132
- // ============================================================================
133
-
134
- export {
135
- StructuredOutputError,
136
- type StructuredOutputErrorOptions,
137
- type StructuredOutputOptions,
138
- type StructuredOutputResult,
139
- type StructuredOutputSuccess,
140
- type StructuredOutputFailure,
141
- type JSONSchema,
142
- type SchemaProvider,
143
- type ProviderSchema,
144
- type SchemaConfig,
145
- isStructuredOutputSuccess,
146
- isStructuredOutputFailure,
147
- // Schema conversion utilities
148
- normalizeJsonSchema,
149
- convertToProviderSchema,
150
- stripUnsupportedFeatures,
151
- getJsonSchema,
152
- getJsonSchemaFromConfig,
153
- // Validation functions
154
- parseStructured,
155
- tryParseStructured,
156
- validateStructuredOutput,
157
- // Streaming parser
158
- StreamingJsonParser,
159
- type StreamingStructuredResult,
160
- } from './structured-output.js';