universal-llm-client 4.2.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 (97) 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.js.map +1 -1
  9. package/dist/interfaces.d.ts +20 -0
  10. package/dist/interfaces.d.ts.map +1 -1
  11. package/dist/interfaces.js.map +1 -1
  12. package/dist/mcp.js.map +1 -1
  13. package/dist/providers/anthropic.js.map +1 -1
  14. package/dist/providers/google.d.ts.map +1 -1
  15. package/dist/providers/google.js +2 -0
  16. package/dist/providers/google.js.map +1 -1
  17. package/dist/providers/index.js.map +1 -1
  18. package/dist/providers/ollama.js.map +1 -1
  19. package/dist/providers/openai.js.map +1 -1
  20. package/dist/router.js.map +1 -1
  21. package/dist/stream-decoder.js.map +1 -1
  22. package/dist/structured-output.js.map +1 -1
  23. package/dist/tools.js.map +1 -1
  24. package/dist/zod-adapter.js.map +1 -1
  25. package/package.json +115 -116
  26. package/src/ai-model.ts +0 -350
  27. package/src/auditor.ts +0 -213
  28. package/src/client.ts +0 -402
  29. package/src/debug/debug-google-streaming.ts +0 -97
  30. package/src/debug/debug-tool-execution.ts +0 -86
  31. package/src/debug/test-lmstudio-tools.ts +0 -155
  32. package/src/demos/README.md +0 -47
  33. package/src/demos/basic/universal-llm-examples.ts +0 -161
  34. package/src/demos/mcp/astrid-memory-demo.ts +0 -295
  35. package/src/demos/mcp/astrid-persona-memory.ts +0 -357
  36. package/src/demos/mcp/mcp-mongodb-demo.ts +0 -275
  37. package/src/demos/mcp/simple-astrid-memory.ts +0 -148
  38. package/src/demos/mcp/simple-mcp-demo.ts +0 -68
  39. package/src/demos/mcp/working-mcp-demo.ts +0 -62
  40. package/src/demos/model-alias-demo.ts +0 -0
  41. package/src/demos/tools/RAG_MEMORY_INTEGRATION.md +0 -267
  42. package/src/demos/tools/astrid-memory-demo.ts +0 -270
  43. package/src/demos/tools/astrid-production-memory-clean.ts +0 -785
  44. package/src/demos/tools/astrid-production-memory.ts +0 -558
  45. package/src/demos/tools/basic-translation-test.ts +0 -66
  46. package/src/demos/tools/chromadb-similarity-tuning.ts +0 -390
  47. package/src/demos/tools/clean-multilingual-conversation.ts +0 -209
  48. package/src/demos/tools/clean-translation-test.ts +0 -119
  49. package/src/demos/tools/clean-universal-multilingual-test.ts +0 -131
  50. package/src/demos/tools/complete-rag-demo.ts +0 -369
  51. package/src/demos/tools/complete-tool-demo.ts +0 -132
  52. package/src/demos/tools/demo-tool-calling.ts +0 -124
  53. package/src/demos/tools/dynamic-language-switching-test.ts +0 -251
  54. package/src/demos/tools/hybrid-thinking-test.ts +0 -154
  55. package/src/demos/tools/memory-integration-test.ts +0 -420
  56. package/src/demos/tools/multilingual-memory-system.ts +0 -802
  57. package/src/demos/tools/ondemand-translation-demo.ts +0 -655
  58. package/src/demos/tools/production-tool-demo.ts +0 -245
  59. package/src/demos/tools/revolutionary-multilingual-test.ts +0 -151
  60. package/src/demos/tools/rigorous-language-analysis.ts +0 -218
  61. package/src/demos/tools/test-universal-memory-system.ts +0 -126
  62. package/src/demos/tools/translation-integration-guide.ts +0 -346
  63. package/src/demos/tools/universal-memory-system.ts +0 -560
  64. package/src/http.ts +0 -247
  65. package/src/index.ts +0 -161
  66. package/src/interfaces.ts +0 -657
  67. package/src/mcp.ts +0 -345
  68. package/src/providers/anthropic.ts +0 -762
  69. package/src/providers/google.ts +0 -620
  70. package/src/providers/index.ts +0 -8
  71. package/src/providers/ollama.ts +0 -469
  72. package/src/providers/openai.ts +0 -392
  73. package/src/router.ts +0 -780
  74. package/src/stream-decoder.ts +0 -361
  75. package/src/structured-output.ts +0 -759
  76. package/src/test-scripts/test-advanced-tools.ts +0 -310
  77. package/src/test-scripts/test-google-streaming-enhanced.ts +0 -147
  78. package/src/test-scripts/test-google-streaming.ts +0 -63
  79. package/src/test-scripts/test-google-system-prompt-comprehensive.ts +0 -189
  80. package/src/test-scripts/test-mcp-config.ts +0 -28
  81. package/src/test-scripts/test-mcp-connection.ts +0 -29
  82. package/src/test-scripts/test-system-message-positions.ts +0 -163
  83. package/src/test-scripts/test-system-prompt-improvement-demo.ts +0 -83
  84. package/src/test-scripts/test-tool-calling.ts +0 -231
  85. package/src/tests/ai-model.test.ts +0 -1614
  86. package/src/tests/auditor.test.ts +0 -224
  87. package/src/tests/http.test.ts +0 -200
  88. package/src/tests/interfaces.test.ts +0 -117
  89. package/src/tests/providers/google.test.ts +0 -660
  90. package/src/tests/providers/ollama.test.ts +0 -954
  91. package/src/tests/providers/openai.test.ts +0 -1122
  92. package/src/tests/router.test.ts +0 -254
  93. package/src/tests/stream-decoder.test.ts +0 -179
  94. package/src/tests/structured-output.test.ts +0 -1450
  95. package/src/tests/tools.test.ts +0 -175
  96. package/src/tools.ts +0 -246
  97. package/src/zod-adapter.ts +0 -72
package/CHANGELOG.md CHANGED
@@ -1,103 +1,139 @@
1
- # Changelog
2
-
3
- All notable changes to this project will be documented in this file.
4
-
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
- and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
-
8
- ## [4.0.0] - 2026-03-13
9
-
10
- ### ⚠ BREAKING CHANGES
11
-
12
- - **Zod 4 required** `zod@^4.0.0` is now a peer dependency (upgraded from Zod 3). Consumers must install `zod@^4.0.0`.
13
- - **Zero-dependency core** — Removed `zod-to-json-schema` from dependencies. Schema conversion now uses Zod 4's native `z.toJSONSchema()`. The library has no runtime dependencies (only peer deps).
14
-
15
- ### Added
16
-
17
- - **Structured Output** — First-class structured output with Zod schema validation
18
- - `generateStructured(schema, messages, options)` — returns typed, validated output
19
- - `tryParseStructured(schema, messages, options)` — non-throwing variant returning `Result<T>`
20
- - `generateStructuredStream(schema, messages, options)` — streaming with partial validated objects
21
- - `chat(messages, { output: { schema } })` — structured output via `output` parameter on `chat()`
22
- - Custom `StructuredOutputError` with `rawOutput` and `cause` for debuggability
23
- - **Provider Structured Output Support** — Native format negotiation per provider
24
- - OpenAI: `response_format: { type: 'json_schema' }` with configurable `strict` mode
25
- - Google/Vertex AI: `responseMimeType` + `responseSchema` with automatic unsupported feature stripping
26
- - Ollama: `format` parameter with JSON Schema objects
27
- - **Streaming JSON Parser** — `StreamingJsonParser` for progressive partial object validation
28
- - **Audit Events** — `structured_request`, `structured_response`, `structured_validation_error` events
29
- - **Schema Utilities** — `zodToJsonSchema()`, `normalizeJsonSchema()`, `stripUnsupportedFeatures()`, `getJsonSchema()`
30
- - **`./structured-output` sub-path export** — Direct import of structured output utilities
31
-
32
- ### Fixed
33
-
34
- - Validation logic deduplicated into `BaseLLMClient` (was copy-pasted across 3 providers)
35
- - Removed double validation (Router + Provider both validated the same response)
36
- - Audit events no longer log `"assistant"` as the model name
37
- - `chatStream()` now throws a clear error if `output` parameter is provided (use `generateStructuredStream()`)
38
- - `zodToJsonSchema()` preserves `definitions`/`$defs` when `$ref` references exist in the schema tree
39
- - OpenAI `strict` mode is now configurable via `output.strict` (defaults to `true`)
40
-
41
- ### Deprecated
42
-
43
- - `ChatOptions.schema`, `.jsonSchema`, `.schemaName`, `.schemaDescription` — use `output` parameter or `generateStructured()` instead
44
-
45
- ---
46
-
47
- ## [3.1.0] - 2026-03-12
48
-
49
- ### Added
50
-
51
- - **Ollama Vision/Multimodal** — Ollama provider now converts OpenAI-style multimodal content (text + image parts) into Ollama's native `images[]` format
52
- - Supports base64 data URLs, raw base64 strings, and gracefully skips HTTP URLs
53
- - Multiple images and mixed text+image messages handled correctly
54
- - **CHANGELOG.md** Added changelog following [Keep a Changelog](https://keepachangelog.com/) format
55
- - **Ollama Provider Tests** — 13 new unit tests covering vision, tool call arguments, options mapping, and response normalization
56
-
57
- ### Fixed
58
-
59
- - Architecture section in README referenced `@akaito/universal-llm-client` instead of the correct unscoped package name
60
- - LlamaCpp missing from the README architecture tree despite being a supported provider
61
- - `@module` JSDoc in barrel export referenced wrong scoped name
62
-
63
- ### Changed
64
-
65
- - Added `author`, `homepage`, `bugs` fields to `package.json`
66
- - Updated LICENSE copyright year to 2025-2026
67
- - Improved `.npmignore` to exclude dev-only files from tarball
68
-
69
- ---
70
-
71
- ## [3.0.0] - 2026-03-12
72
-
73
- ### BREAKING CHANGES
74
-
75
- - Complete rewrite with a new modular architecture
76
- - `AIModel` is now the sole public-facing class (replaces direct provider instantiation)
77
- - Provider configuration moved to a declarative `providers[]` array
78
- - Minimum runtime: Node.js 22+, Bun 1.0+
79
-
80
- ### Added
81
-
82
- - **Transparent Failover** — Priority-ordered provider chain with retries, health tracking, and configurable cooldowns
83
- - **Streaming** First-class async generator streaming with pluggable decoder strategies (passthrough, standard chat, interleaved reasoning)
84
- - **Tool Calling** — Register tools once, works across all providers. Autonomous multi-turn execution loop via `chatWithTools()`
85
- - **Tool Utilities** — `ToolBuilder` (fluent API), `ToolExecutor` (timeout, validation, safe wrappers, composition)
86
- - **Reasoning/Thinking** — Native `<think>` tag parsing, model thinking mode, and interleaved reasoning support
87
- - **Observability** — Built-in `Auditor` interface with `ConsoleAuditor`, `BufferedAuditor`, and `NoopAuditor` implementations
88
- - **MCP Integration** `MCPToolBridge` for bridging MCP servers to LLM tools with zero glue code
89
- - **Multimodal/Vision** `multimodalMessage()`, `imageContent()`, `textContent()` helpers; vision support across Google, OpenAI, and Ollama providers
90
- - **Embeddings** — Single (`embed()`) and batch (`embedArray()`) embedding generation
91
- - **Model Discovery** — `getModels()` and `getModelInfo()` for runtime model introspection
92
- - **Gemini 3.x Support** — `thoughtSignature` handling for multi-turn function calling
93
- - **Provider Support** — Ollama, OpenAI (+ OpenRouter, Groq, LM Studio, vLLM), Google AI Studio, Vertex AI, LlamaCpp
94
-
95
- ### Changed
96
-
97
- - Zero runtime dependencies core library uses only native `fetch`
98
- - ESM-only distribution (no CJS bundle)
99
- - Full TypeScript strict mode with declaration maps and source maps
100
-
101
- ## [2.x] - Pre-rewrite
102
-
103
- Legacy versions with direct provider APIs. Not documented here — see git history for details.
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [4.3.0] - 2026-05-14
9
+
10
+ ### Added
11
+
12
+ - **`TokenUsageInfo.reasoningTokens`** new optional field reporting server-side thinking/reasoning tokens that are billed but not emitted as visible text. Currently populated by the Google provider from `usageMetadata.thoughtsTokenCount` for Gemini thinking models (2.5 Pro / 3.x Pro). Other providers either roll thinking into `outputTokens` (Ollama) or surface it via `DecodedEvent { type: 'thinking' }`.
13
+ - **`GoogleResponse.usageMetadata.thoughtsTokenCount`** typed on the raw provider response shape.
14
+
15
+ ### Changed
16
+
17
+ - **Slimmer published tarball** — `src/` is no longer shipped to npm. The published package drops from ~271 kB / 144 files to ~146 kB / 72 files. Source maps now embed original sources (`inlineSources: true`), so debuggers stepping into the library continue to show TypeScript source without needing the raw `.ts` files in the tarball.
18
+
19
+ ## [4.2.0] - Previously released
20
+
21
+ ### Added
22
+
23
+ - **Ollama native thinking enabled by default** — `think` parameter is now sent on every request to prevent model-default thinking from leaking into tool selection / reasoning paths.
24
+ - **Ollama stream idle timeout** `OllamaClient` enforces a minimum response wait time to avoid hung streams.
25
+ - **Ollama live context length** — provider now fetches the running context length from `/api/ps` and reports model capabilities.
26
+ - **Google Gemini service tiers** — configurable service tier with per-tier timeouts and retry logic.
27
+ - **`LLMAudioContent` type** — audio content support across providers.
28
+ - **Structured output tolerates markdown fences** — JSON output wrapped in ```` ```json ```` fences is now parsed correctly.
29
+
30
+ ## [4.1.0] - Previously released
31
+
32
+ ### Added
33
+
34
+ - **Prompt caching support** first-class cache configuration plumbed through providers.
35
+ - **Anthropic provider** `AnthropicClient` implementation of the universal protocol.
36
+ - **Request cancellation** `signal: AbortSignal` accepted on all chat methods.
37
+ - **Decoupled structured output from Zod** new generic `SchemaConfig` interface; Zod becomes an adapter (`./zod` sub-path) rather than a hard dependency on the schema path.
38
+ - **Wider JSONSchema tuple types** schema utilities accept more JSON Schema tuple shapes.
39
+
40
+ ### Changed
41
+
42
+ - Tests migrated to the new `fromZod` adapter API.
43
+
44
+ ## [4.0.0] - 2026-03-13
45
+
46
+ ### ⚠ BREAKING CHANGES
47
+
48
+ - **Zod 4 required** — `zod@^4.0.0` is now a peer dependency (upgraded from Zod 3). Consumers must install `zod@^4.0.0`.
49
+ - **Zero-dependency core** — Removed `zod-to-json-schema` from dependencies. Schema conversion now uses Zod 4's native `z.toJSONSchema()`. The library has no runtime dependencies (only peer deps).
50
+
51
+ ### Added
52
+
53
+ - **Structured Output** First-class structured output with Zod schema validation
54
+ - `generateStructured(schema, messages, options)` returns typed, validated output
55
+ - `tryParseStructured(schema, messages, options)` non-throwing variant returning `Result<T>`
56
+ - `generateStructuredStream(schema, messages, options)` — streaming with partial validated objects
57
+ - `chat(messages, { output: { schema } })` — structured output via `output` parameter on `chat()`
58
+ - Custom `StructuredOutputError` with `rawOutput` and `cause` for debuggability
59
+ - **Provider Structured Output Support** Native format negotiation per provider
60
+ - OpenAI: `response_format: { type: 'json_schema' }` with configurable `strict` mode
61
+ - Google/Vertex AI: `responseMimeType` + `responseSchema` with automatic unsupported feature stripping
62
+ - Ollama: `format` parameter with JSON Schema objects
63
+ - **Streaming JSON Parser** — `StreamingJsonParser` for progressive partial object validation
64
+ - **Audit Events** — `structured_request`, `structured_response`, `structured_validation_error` events
65
+ - **Schema Utilities** — `zodToJsonSchema()`, `normalizeJsonSchema()`, `stripUnsupportedFeatures()`, `getJsonSchema()`
66
+ - **`./structured-output` sub-path export** Direct import of structured output utilities
67
+
68
+ ### Fixed
69
+
70
+ - Validation logic deduplicated into `BaseLLMClient` (was copy-pasted across 3 providers)
71
+ - Removed double validation (Router + Provider both validated the same response)
72
+ - Audit events no longer log `"assistant"` as the model name
73
+ - `chatStream()` now throws a clear error if `output` parameter is provided (use `generateStructuredStream()`)
74
+ - `zodToJsonSchema()` preserves `definitions`/`$defs` when `$ref` references exist in the schema tree
75
+ - OpenAI `strict` mode is now configurable via `output.strict` (defaults to `true`)
76
+
77
+ ### Deprecated
78
+
79
+ - `ChatOptions.schema`, `.jsonSchema`, `.schemaName`, `.schemaDescription` — use `output` parameter or `generateStructured()` instead
80
+
81
+ ---
82
+
83
+ ## [3.1.0] - 2026-03-12
84
+
85
+ ### Added
86
+
87
+ - **Ollama Vision/Multimodal** — Ollama provider now converts OpenAI-style multimodal content (text + image parts) into Ollama's native `images[]` format
88
+ - Supports base64 data URLs, raw base64 strings, and gracefully skips HTTP URLs
89
+ - Multiple images and mixed text+image messages handled correctly
90
+ - **CHANGELOG.md** — Added changelog following [Keep a Changelog](https://keepachangelog.com/) format
91
+ - **Ollama Provider Tests** — 13 new unit tests covering vision, tool call arguments, options mapping, and response normalization
92
+
93
+ ### Fixed
94
+
95
+ - Architecture section in README referenced `@akaito/universal-llm-client` instead of the correct unscoped package name
96
+ - LlamaCpp missing from the README architecture tree despite being a supported provider
97
+ - `@module` JSDoc in barrel export referenced wrong scoped name
98
+
99
+ ### Changed
100
+
101
+ - Added `author`, `homepage`, `bugs` fields to `package.json`
102
+ - Updated LICENSE copyright year to 2025-2026
103
+ - Improved `.npmignore` to exclude dev-only files from tarball
104
+
105
+ ---
106
+
107
+ ## [3.0.0] - 2026-03-12
108
+
109
+ ### ⚠ BREAKING CHANGES
110
+
111
+ - Complete rewrite with a new modular architecture
112
+ - `AIModel` is now the sole public-facing class (replaces direct provider instantiation)
113
+ - Provider configuration moved to a declarative `providers[]` array
114
+ - Minimum runtime: Node.js 22+, Bun 1.0+
115
+
116
+ ### Added
117
+
118
+ - **Transparent Failover** — Priority-ordered provider chain with retries, health tracking, and configurable cooldowns
119
+ - **Streaming** — First-class async generator streaming with pluggable decoder strategies (passthrough, standard chat, interleaved reasoning)
120
+ - **Tool Calling** — Register tools once, works across all providers. Autonomous multi-turn execution loop via `chatWithTools()`
121
+ - **Tool Utilities** — `ToolBuilder` (fluent API), `ToolExecutor` (timeout, validation, safe wrappers, composition)
122
+ - **Reasoning/Thinking** — Native `<think>` tag parsing, model thinking mode, and interleaved reasoning support
123
+ - **Observability** — Built-in `Auditor` interface with `ConsoleAuditor`, `BufferedAuditor`, and `NoopAuditor` implementations
124
+ - **MCP Integration** — `MCPToolBridge` for bridging MCP servers to LLM tools with zero glue code
125
+ - **Multimodal/Vision** — `multimodalMessage()`, `imageContent()`, `textContent()` helpers; vision support across Google, OpenAI, and Ollama providers
126
+ - **Embeddings** — Single (`embed()`) and batch (`embedArray()`) embedding generation
127
+ - **Model Discovery** — `getModels()` and `getModelInfo()` for runtime model introspection
128
+ - **Gemini 3.x Support** — `thoughtSignature` handling for multi-turn function calling
129
+ - **Provider Support** — Ollama, OpenAI (+ OpenRouter, Groq, LM Studio, vLLM), Google AI Studio, Vertex AI, LlamaCpp
130
+
131
+ ### Changed
132
+
133
+ - Zero runtime dependencies — core library uses only native `fetch`
134
+ - ESM-only distribution (no CJS bundle)
135
+ - Full TypeScript strict mode with declaration maps and source maps
136
+
137
+ ## [2.x] - Pre-rewrite
138
+
139
+ Legacy versions with direct provider APIs. Not documented here — see git history for details.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025-2026 Igor Lins e Silva
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025-2026 Igor Lins e Silva
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.