universal-llm-client 3.0.0 → 4.0.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/CHANGELOG.md +103 -0
- package/LICENSE +1 -1
- package/README.md +181 -4
- package/dist/ai-model.d.ts +89 -0
- package/dist/ai-model.d.ts.map +1 -1
- package/dist/ai-model.js +96 -0
- package/dist/ai-model.js.map +1 -1
- package/dist/auditor.d.ts +5 -1
- package/dist/auditor.d.ts.map +1 -1
- package/dist/auditor.js +9 -0
- package/dist/auditor.js.map +1 -1
- package/dist/client.d.ts +14 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +52 -0
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +143 -1
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/interfaces.js.map +1 -1
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +21 -0
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/ollama.d.ts +13 -1
- package/dist/providers/ollama.d.ts.map +1 -1
- package/dist/providers/ollama.js +94 -25
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.d.ts +4 -0
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +51 -1
- package/dist/providers/openai.js.map +1 -1
- package/dist/router.d.ts +70 -0
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +343 -0
- package/dist/router.js.map +1 -1
- package/dist/structured-output.d.ts +467 -0
- package/dist/structured-output.d.ts.map +1 -0
- package/dist/structured-output.js +505 -0
- package/dist/structured-output.js.map +1 -0
- package/package.json +21 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
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.
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -27,6 +27,7 @@ const response = await model.chat([
|
|
|
27
27
|
|
|
28
28
|
- 🔄 **Transparent Failover** — Priority-ordered provider chain with retries, health tracking, and cooldowns
|
|
29
29
|
- 🛠️ **Tool Calling** — Register tools once, works across all providers. Autonomous multi-turn execution loop
|
|
30
|
+
- 📋 **Structured Output** — Zod schema validation, JSON Schema support, streaming, and type-safe responses
|
|
30
31
|
- 🌊 **Streaming** — First-class async generator streaming with pluggable decoder strategies
|
|
31
32
|
- 🧠 **Reasoning** — Native `<think>` tag parsing, interleaved reasoning, and model thinking support
|
|
32
33
|
- 🔍 **Observability** — Built-in auditor interface for logging, cost tracking, and behavioral analysis
|
|
@@ -38,7 +39,7 @@ const response = await model.chat([
|
|
|
38
39
|
|
|
39
40
|
| Provider | Type | Notes |
|
|
40
41
|
|---|---|---|
|
|
41
|
-
| **Ollama** | `ollama` | Local or cloud models, NDJSON streaming, model pulling |
|
|
42
|
+
| **Ollama** | `ollama` | Local or cloud models, NDJSON streaming, model pulling, vision/multimodal |
|
|
42
43
|
| **OpenAI** | `openai` | GPT-4o, o3, etc. Also works with OpenRouter, Groq, LM Studio, vLLM |
|
|
43
44
|
| **Google AI Studio** | `google` | Gemini models, system instructions, multimodal |
|
|
44
45
|
| **Vertex AI** | `vertex` | Same as Google AI but with regional endpoints and Bearer tokens |
|
|
@@ -183,6 +184,133 @@ const vectors = await embedModel.embedArray(['Hello', 'World']);
|
|
|
183
184
|
// [[0.006, ...], [0.012, ...]]
|
|
184
185
|
```
|
|
185
186
|
|
|
187
|
+
### Structured Output
|
|
188
|
+
|
|
189
|
+
Get typed, validated JSON responses from any LLM using Zod schemas:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { AIModel } from 'universal-llm-client';
|
|
193
|
+
import { z } from 'zod';
|
|
194
|
+
|
|
195
|
+
const model = new AIModel({
|
|
196
|
+
model: 'gemini-2.5-flash',
|
|
197
|
+
providers: [
|
|
198
|
+
{ type: 'google', apiKey: process.env.GOOGLE_API_KEY },
|
|
199
|
+
{ type: 'ollama' },
|
|
200
|
+
],
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Define your schema
|
|
204
|
+
const UserSchema = z.object({
|
|
205
|
+
name: z.string(),
|
|
206
|
+
age: z.number(),
|
|
207
|
+
email: z.string().email(),
|
|
208
|
+
interests: z.array(z.string()),
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// Method 1: generateStructured (throws on validation failure)
|
|
212
|
+
const user = await model.generateStructured(UserSchema, [
|
|
213
|
+
{ role: 'user', content: 'Generate a user profile for a software developer' },
|
|
214
|
+
]);
|
|
215
|
+
|
|
216
|
+
console.log(user.name); // TypeScript knows this is string
|
|
217
|
+
console.log(user.age); // TypeScript knows this is number
|
|
218
|
+
console.log(user.email); // TypeScript knows this is string
|
|
219
|
+
console.log(user.interests); // TypeScript knows this is string[]
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Non-throwing variant:**
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
// Method 2: tryParseStructured (returns result object, never throws)
|
|
226
|
+
const result = await model.tryParseStructured(UserSchema, messages);
|
|
227
|
+
|
|
228
|
+
if (result.ok) {
|
|
229
|
+
console.log('User:', result.value.name);
|
|
230
|
+
} else {
|
|
231
|
+
console.log('Error:', result.error.message);
|
|
232
|
+
console.log('Raw LLM output:', result.rawOutput);
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**Via chat options:**
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
// Method 3: chat with output parameter
|
|
240
|
+
const response = await model.chat(messages, {
|
|
241
|
+
output: { schema: UserSchema },
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// response.structured is typed as { name: string, age: number, ... }
|
|
245
|
+
if (response.structured) {
|
|
246
|
+
console.log(response.structured.name);
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
**Streaming structured output:**
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
// Stream partial validated objects as JSON generates
|
|
254
|
+
for await (const partial of model.generateStructuredStream(UserSchema, messages)) {
|
|
255
|
+
console.log('Partial:', partial);
|
|
256
|
+
// Partial: { name: 'Alice' }
|
|
257
|
+
// Partial: { name: 'Alice', age: 30 }
|
|
258
|
+
// Partial: { name: 'Alice', age: 30, email: 'alice@example.com' }
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
**Raw JSON Schema (without Zod):**
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
const response = await model.chat(messages, {
|
|
266
|
+
jsonSchema: {
|
|
267
|
+
type: 'object',
|
|
268
|
+
properties: {
|
|
269
|
+
name: { type: 'string' },
|
|
270
|
+
age: { type: 'number' },
|
|
271
|
+
},
|
|
272
|
+
required: ['name', 'age'],
|
|
273
|
+
},
|
|
274
|
+
name: 'Person', // Optional, used for LLM guidance
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
**Separate module import (tree-shaking):**
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
// Import only structured output types if you don't need the full client
|
|
282
|
+
import {
|
|
283
|
+
StructuredOutputError,
|
|
284
|
+
type StructuredOutputResult,
|
|
285
|
+
type StructuredOutputOptions,
|
|
286
|
+
parseStructured,
|
|
287
|
+
tryParseStructured,
|
|
288
|
+
zodToJsonSchema,
|
|
289
|
+
} from 'universal-llm-client/structured-output';
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
**Vision with structured output:**
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
const ImageAnalysisSchema = z.object({
|
|
296
|
+
objects: z.array(z.string()),
|
|
297
|
+
scene: z.string(),
|
|
298
|
+
mood: z.string(),
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const response = await model.generateStructured(ImageAnalysisSchema, [
|
|
302
|
+
multimodalMessage('Analyze this image', ['https://example.com/photo.jpg']),
|
|
303
|
+
]);
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
**Provider compatibility:**
|
|
307
|
+
|
|
308
|
+
| Provider | Method | Notes |
|
|
309
|
+
|----------|--------|-------|
|
|
310
|
+
| OpenAI | `response_format.json_schema` | Strict mode enabled |
|
|
311
|
+
| Ollama | `format: { schema }` | Model must support grammar |
|
|
312
|
+
| Google | `responseMimeType + responseSchema` | Some features stripped |
|
|
313
|
+
|
|
186
314
|
### Observability
|
|
187
315
|
|
|
188
316
|
```typescript
|
|
@@ -308,6 +436,9 @@ new AIModel(config: AIModelConfig)
|
|
|
308
436
|
| `chat(messages, options?)` | `Promise<LLMChatResponse>` | Send chat request |
|
|
309
437
|
| `chatWithTools(messages, options?)` | `Promise<LLMChatResponse>` | Chat with autonomous tool execution |
|
|
310
438
|
| `chatStream(messages, options?)` | `AsyncGenerator<DecodedEvent>` | Stream chat response |
|
|
439
|
+
| `generateStructured(schema, messages, options?)` | `Promise<T>` | Generate typed JSON validated against Zod schema |
|
|
440
|
+
| `tryParseStructured(schema, messages, options?)` | `Promise<StructuredOutputResult<T>>` | Non-throwing variant returning result object |
|
|
441
|
+
| `generateStructuredStream(schema, messages, options?)` | `AsyncGenerator<T, T>` | Stream partial validated objects as JSON generates |
|
|
311
442
|
| `embed(text)` | `Promise<number[]>` | Generate single embedding |
|
|
312
443
|
| `embedArray(texts)` | `Promise<number[][]>` | Generate batch embeddings |
|
|
313
444
|
| `registerTool(name, desc, params, handler)` | `void` | Register a callable tool |
|
|
@@ -318,6 +449,52 @@ new AIModel(config: AIModelConfig)
|
|
|
318
449
|
| `setModel(name)` | `void` | Switch model at runtime |
|
|
319
450
|
| `dispose()` | `Promise<void>` | Clean shutdown |
|
|
320
451
|
|
|
452
|
+
### Structured Output
|
|
453
|
+
|
|
454
|
+
```typescript
|
|
455
|
+
import { z } from 'zod';
|
|
456
|
+
|
|
457
|
+
// Define your schema
|
|
458
|
+
const UserSchema = z.object({
|
|
459
|
+
name: z.string(),
|
|
460
|
+
age: z.number(),
|
|
461
|
+
email: z.string().email(),
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
// Generate typed JSON
|
|
465
|
+
const user = await model.generateStructured(UserSchema, messages);
|
|
466
|
+
// TypeScript infers: { name: string; age: number; email: string }
|
|
467
|
+
|
|
468
|
+
// Non-throwing variant
|
|
469
|
+
const result = await model.tryParseStructured(UserSchema, messages);
|
|
470
|
+
if (result.ok) {
|
|
471
|
+
console.log(result.value.name); // Fully typed
|
|
472
|
+
} else {
|
|
473
|
+
console.log(result.error.message);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// Stream partial objects
|
|
477
|
+
for await (const partial of model.generateStructuredStream(UserSchema, messages)) {
|
|
478
|
+
console.log(partial); // Partial validated objects
|
|
479
|
+
}
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
**Separate module import (tree-shaking):**
|
|
483
|
+
|
|
484
|
+
```typescript
|
|
485
|
+
import {
|
|
486
|
+
StructuredOutputError,
|
|
487
|
+
type StructuredOutputResult,
|
|
488
|
+
parseStructured,
|
|
489
|
+
tryParseStructured,
|
|
490
|
+
zodToJsonSchema,
|
|
491
|
+
} from 'universal-llm-client/structured-output';
|
|
492
|
+
|
|
493
|
+
// Use without importing the full client
|
|
494
|
+
const schema = z.object({ name: z.string() });
|
|
495
|
+
const jsonSchema = zodToJsonSchema(schema);
|
|
496
|
+
```
|
|
497
|
+
|
|
321
498
|
### `ToolBuilder` / `ToolExecutor`
|
|
322
499
|
|
|
323
500
|
```typescript
|
|
@@ -360,14 +537,14 @@ interface Auditor {
|
|
|
360
537
|
## Architecture
|
|
361
538
|
|
|
362
539
|
```
|
|
363
|
-
|
|
540
|
+
universal-llm-client
|
|
364
541
|
├── AIModel ← Public API (the only class you import)
|
|
365
542
|
├── Router ← Internal failover engine
|
|
366
543
|
├── BaseLLMClient ← Abstract client with tool execution
|
|
367
544
|
├── Providers
|
|
368
545
|
│ ├── OllamaClient
|
|
369
|
-
│ ├── OpenAICompatibleClient
|
|
370
|
-
│ └── GoogleClient
|
|
546
|
+
│ ├── OpenAICompatibleClient (OpenAI, OpenRouter, Groq, LM Studio, vLLM, LlamaCpp)
|
|
547
|
+
│ └── GoogleClient (AI Studio + Vertex AI)
|
|
371
548
|
├── StreamDecoder ← Pluggable reasoning strategies
|
|
372
549
|
├── Auditor ← Observability interface
|
|
373
550
|
├── MCPToolBridge ← MCP server integration
|
package/dist/ai-model.d.ts
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
import { type AIModelConfig, type LLMChatMessage, type LLMChatResponse, type ChatOptions, type ModelMetadata, type LLMFunction, type ToolHandler } from './interfaces.js';
|
|
10
10
|
import type { DecodedEvent } from './stream-decoder.js';
|
|
11
11
|
import { type ProviderStatus } from './router.js';
|
|
12
|
+
import { type StructuredOutputResult } from './structured-output.js';
|
|
13
|
+
import { z } from 'zod';
|
|
12
14
|
export declare class AIModel {
|
|
13
15
|
private router;
|
|
14
16
|
private auditor;
|
|
@@ -22,6 +24,93 @@ export declare class AIModel {
|
|
|
22
24
|
}): Promise<LLMChatResponse>;
|
|
23
25
|
/** Stream chat response with pluggable decoder strategy */
|
|
24
26
|
chatStream(messages: LLMChatMessage[], options?: ChatOptions): AsyncGenerator<DecodedEvent, LLMChatResponse | void, unknown>;
|
|
27
|
+
/**
|
|
28
|
+
* Generate structured output from the LLM with automatic failover.
|
|
29
|
+
* Validates the response against the provided Zod schema.
|
|
30
|
+
* Throws StructuredOutputError on validation failure.
|
|
31
|
+
*
|
|
32
|
+
* @template T The type inferred from the Zod schema
|
|
33
|
+
* @param schema Zod schema for validation
|
|
34
|
+
* @param messages Chat messages to send
|
|
35
|
+
* @param options Additional options (temperature, maxTokens, etc.)
|
|
36
|
+
* @returns Promise resolving to validated structured output
|
|
37
|
+
* @throws StructuredOutputError if JSON parsing fails or schema validation fails
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const UserSchema = z.object({
|
|
42
|
+
* name: z.string(),
|
|
43
|
+
* age: z.number(),
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* const user = await model.generateStructured(UserSchema, [
|
|
47
|
+
* { role: 'user', content: 'Generate a user profile' },
|
|
48
|
+
* ]);
|
|
49
|
+
* // user.name: string, user.age: number
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
generateStructured<T>(schema: z.ZodType<T>, messages: LLMChatMessage[], options?: ChatOptions): Promise<T>;
|
|
53
|
+
/**
|
|
54
|
+
* Try to generate structured output, returning a result object instead of throwing.
|
|
55
|
+
* Same as generateStructured but returns { ok: true, value } on success
|
|
56
|
+
* and { ok: false, error, rawOutput } on failure.
|
|
57
|
+
*
|
|
58
|
+
* @template T The type inferred from the Zod schema
|
|
59
|
+
* @param schema Zod schema for validation
|
|
60
|
+
* @param messages Chat messages to send
|
|
61
|
+
* @param options Additional options (temperature, maxTokens, etc.)
|
|
62
|
+
* @returns StructuredOutputResult<T> - either success with value or failure with error
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const result = await model.tryParseStructured(UserSchema, messages);
|
|
67
|
+
*
|
|
68
|
+
* if (result.ok) {
|
|
69
|
+
* console.log('User:', result.value.name);
|
|
70
|
+
* } else {
|
|
71
|
+
* console.log('Error:', result.error.message);
|
|
72
|
+
* console.log('Raw output:', result.rawOutput);
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
tryParseStructured<T>(schema: z.ZodType<T>, messages: LLMChatMessage[], options?: ChatOptions): Promise<StructuredOutputResult<T>>;
|
|
77
|
+
/**
|
|
78
|
+
* Stream structured output with partial validated objects.
|
|
79
|
+
*
|
|
80
|
+
* Yields partial validated objects as JSON generates, then returns the
|
|
81
|
+
* complete validated object on stream completion.
|
|
82
|
+
*
|
|
83
|
+
* For invalid partial JSON, no yield occurs (partial validation is best-effort).
|
|
84
|
+
* On stream completion, if the final JSON fails validation, throws StructuredOutputError.
|
|
85
|
+
*
|
|
86
|
+
* @template T The type inferred from the Zod schema
|
|
87
|
+
* @param schema Zod schema for validation
|
|
88
|
+
* @param messages Chat messages to send
|
|
89
|
+
* @param options Additional options (temperature, maxTokens, etc.)
|
|
90
|
+
* @yields Partial validated objects as the JSON stream progresses
|
|
91
|
+
* @returns Complete validated object on stream completion
|
|
92
|
+
* @throws StructuredOutputError if final validation fails
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const UserSchema = z.object({
|
|
97
|
+
* name: z.string(),
|
|
98
|
+
* age: z.number(),
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* const stream = model.generateStructuredStream(UserSchema, [
|
|
102
|
+
* { role: 'user', content: 'Generate a user' },
|
|
103
|
+
* ]);
|
|
104
|
+
*
|
|
105
|
+
* for await (const partial of stream) {
|
|
106
|
+
* console.log('Partial user:', partial);
|
|
107
|
+
* // Partial user: { name: 'Alice' }
|
|
108
|
+
* // Partial user: { name: 'Alice', age: 30 }
|
|
109
|
+
* }
|
|
110
|
+
* // Stream returns complete validated object on completion
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
generateStructuredStream<T>(schema: z.ZodType<T>, messages: LLMChatMessage[], options?: ChatOptions): AsyncGenerator<T, T, unknown>;
|
|
25
114
|
/** Generate embedding for a single text */
|
|
26
115
|
embed(text: string): Promise<number[]>;
|
|
27
116
|
/** Generate embeddings for multiple texts */
|
package/dist/ai-model.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-model.d.ts","sourceRoot":"","sources":["../src/ai-model.ts"],"names":[],"mappings":"AAAA;;;;;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,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;QACpB,UAAU,EAAE,WAAW,CAAC,YAAY,CAAC,CAAC;QACtC,OAAO,EAAE,WAAW,CAAC;KACxB,CAAC,GACH,IAAI;IAQP,yDAAyD;IACnD,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAIpC,0EAA0E;IACpE,YAAY,IAAI,OAAO,CAAC,aAAa,CAAC;IAI5C,sDAAsD;IACtD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAM5B,iCAAiC;IACjC,IAAI,KAAK,IAAI,MAAM,CAElB;IAMD,oDAAoD;IACpD,iBAAiB,IAAI,cAAc,EAAE;IAQrC,2DAA2D;IACrD,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ9B,OAAO,CAAC,YAAY;IAmCpB,OAAO,CAAC,aAAa;CAGxB"}
|
package/dist/ai-model.js
CHANGED
|
@@ -64,6 +64,102 @@ export class AIModel {
|
|
|
64
64
|
return yield* this.router.chatStream(messages, options);
|
|
65
65
|
}
|
|
66
66
|
// ========================================================================
|
|
67
|
+
// Structured Output
|
|
68
|
+
// ========================================================================
|
|
69
|
+
/**
|
|
70
|
+
* Generate structured output from the LLM with automatic failover.
|
|
71
|
+
* Validates the response against the provided Zod schema.
|
|
72
|
+
* Throws StructuredOutputError on validation failure.
|
|
73
|
+
*
|
|
74
|
+
* @template T The type inferred from the Zod schema
|
|
75
|
+
* @param schema Zod schema for validation
|
|
76
|
+
* @param messages Chat messages to send
|
|
77
|
+
* @param options Additional options (temperature, maxTokens, etc.)
|
|
78
|
+
* @returns Promise resolving to validated structured output
|
|
79
|
+
* @throws StructuredOutputError if JSON parsing fails or schema validation fails
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const UserSchema = z.object({
|
|
84
|
+
* name: z.string(),
|
|
85
|
+
* age: z.number(),
|
|
86
|
+
* });
|
|
87
|
+
*
|
|
88
|
+
* const user = await model.generateStructured(UserSchema, [
|
|
89
|
+
* { role: 'user', content: 'Generate a user profile' },
|
|
90
|
+
* ]);
|
|
91
|
+
* // user.name: string, user.age: number
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
async generateStructured(schema, messages, options) {
|
|
95
|
+
return this.router.generateStructured(schema, messages, options);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Try to generate structured output, returning a result object instead of throwing.
|
|
99
|
+
* Same as generateStructured but returns { ok: true, value } on success
|
|
100
|
+
* and { ok: false, error, rawOutput } on failure.
|
|
101
|
+
*
|
|
102
|
+
* @template T The type inferred from the Zod schema
|
|
103
|
+
* @param schema Zod schema for validation
|
|
104
|
+
* @param messages Chat messages to send
|
|
105
|
+
* @param options Additional options (temperature, maxTokens, etc.)
|
|
106
|
+
* @returns StructuredOutputResult<T> - either success with value or failure with error
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* const result = await model.tryParseStructured(UserSchema, messages);
|
|
111
|
+
*
|
|
112
|
+
* if (result.ok) {
|
|
113
|
+
* console.log('User:', result.value.name);
|
|
114
|
+
* } else {
|
|
115
|
+
* console.log('Error:', result.error.message);
|
|
116
|
+
* console.log('Raw output:', result.rawOutput);
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
async tryParseStructured(schema, messages, options) {
|
|
121
|
+
return this.router.tryParseStructured(schema, messages, options);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Stream structured output with partial validated objects.
|
|
125
|
+
*
|
|
126
|
+
* Yields partial validated objects as JSON generates, then returns the
|
|
127
|
+
* complete validated object on stream completion.
|
|
128
|
+
*
|
|
129
|
+
* For invalid partial JSON, no yield occurs (partial validation is best-effort).
|
|
130
|
+
* On stream completion, if the final JSON fails validation, throws StructuredOutputError.
|
|
131
|
+
*
|
|
132
|
+
* @template T The type inferred from the Zod schema
|
|
133
|
+
* @param schema Zod schema for validation
|
|
134
|
+
* @param messages Chat messages to send
|
|
135
|
+
* @param options Additional options (temperature, maxTokens, etc.)
|
|
136
|
+
* @yields Partial validated objects as the JSON stream progresses
|
|
137
|
+
* @returns Complete validated object on stream completion
|
|
138
|
+
* @throws StructuredOutputError if final validation fails
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const UserSchema = z.object({
|
|
143
|
+
* name: z.string(),
|
|
144
|
+
* age: z.number(),
|
|
145
|
+
* });
|
|
146
|
+
*
|
|
147
|
+
* const stream = model.generateStructuredStream(UserSchema, [
|
|
148
|
+
* { role: 'user', content: 'Generate a user' },
|
|
149
|
+
* ]);
|
|
150
|
+
*
|
|
151
|
+
* for await (const partial of stream) {
|
|
152
|
+
* console.log('Partial user:', partial);
|
|
153
|
+
* // Partial user: { name: 'Alice' }
|
|
154
|
+
* // Partial user: { name: 'Alice', age: 30 }
|
|
155
|
+
* }
|
|
156
|
+
* // Stream returns complete validated object on completion
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
async *generateStructuredStream(schema, messages, options) {
|
|
160
|
+
return yield* this.router.generateStructuredStream(schema, messages, options);
|
|
161
|
+
}
|
|
162
|
+
// ========================================================================
|
|
67
163
|
// Embeddings
|
|
68
164
|
// ========================================================================
|
|
69
165
|
/** Generate embedding for a single text */
|
package/dist/ai-model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-model.js","sourceRoot":"","sources":["../src/ai-model.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAeH,OAAO,EAAE,MAAM,EAA0C,MAAM,aAAa,CAAC;AAE7E,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAu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Q,IAAI,KAAK;YACvC,MAAM,EAAE,cAAc,CAAC,MAAM;YAC7B,UAAU,EAAE,cAAc,CAAC,UAAU;SACxC,CAAC;QAEF,QAAQ,IAAI,EAAE,CAAC;YACX,KAAK,QAAQ;gBACT,OAAO,IAAI,YAAY,CAAC,aAAa,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAEzD,KAAK,QAAQ,CAAC;YACd,KAAK,UAAU;gBACX,OAAO,IAAI,sBAAsB,CAAC,aAAa,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAEnE,KAAK,QAAQ,CAAC;YACd,KAAK,QAAQ;gBACT,OAAO,IAAI,YAAY,CAAC,aAAa,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAEzD;gBACI,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,EAAE,CAAC,CAAC;QAC1D,CAAC;IACL,CAAC;IAEO,aAAa,CAAC,IAAY;QAC9B,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC;IAC9B,CAAC;CACJ"}
|
package/dist/auditor.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* Auditor for dashboards, cost tracking, or behavioral scoring.
|
|
7
7
|
*/
|
|
8
8
|
import type { TokenUsageInfo, ToolExecutionResult } from './interfaces.js';
|
|
9
|
-
export type AuditEventType = 'request' | 'response' | 'stream_start' | 'stream_end' | 'tool_call' | 'tool_result' | 'error' | 'retry' | 'failover';
|
|
9
|
+
export type AuditEventType = 'request' | 'response' | 'stream_start' | 'stream_end' | 'tool_call' | 'tool_result' | 'error' | 'retry' | 'failover' | 'structured_request' | 'structured_response' | 'structured_validation_error';
|
|
10
10
|
export interface AuditEvent {
|
|
11
11
|
/** Unix timestamp in ms */
|
|
12
12
|
timestamp: number;
|
|
@@ -26,6 +26,10 @@ export interface AuditEvent {
|
|
|
26
26
|
error?: string;
|
|
27
27
|
/** Arbitrary metadata for framework-specific data */
|
|
28
28
|
metadata?: Record<string, unknown>;
|
|
29
|
+
/** Schema name for structured output events */
|
|
30
|
+
schemaName?: string;
|
|
31
|
+
/** Raw output snippet for validation errors */
|
|
32
|
+
rawOutput?: string;
|
|
29
33
|
}
|
|
30
34
|
/**
|
|
31
35
|
* Interface for LLM observability.
|
package/dist/auditor.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auditor.d.ts","sourceRoot":"","sources":["../src/auditor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAM3E,MAAM,MAAM,cAAc,GACpB,SAAS,GACT,UAAU,GACV,cAAc,GACd,YAAY,GACZ,WAAW,GACX,aAAa,GACb,OAAO,GACP,OAAO,GACP,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"auditor.d.ts","sourceRoot":"","sources":["../src/auditor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAM3E,MAAM,MAAM,cAAc,GACpB,SAAS,GACT,UAAU,GACV,cAAc,GACd,YAAY,GACZ,WAAW,GACX,aAAa,GACb,OAAO,GACP,OAAO,GACP,UAAU,GACV,oBAAoB,GACpB,qBAAqB,GACrB,6BAA6B,CAAC;AAEpC,MAAM,WAAW,UAAU;IACvB,2BAA2B;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,iBAAiB;IACjB,IAAI,EAAE,cAAc,CAAC;IACrB,yCAAyC;IACzC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iBAAiB;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,kDAAkD;IAClD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wCAAwC;IACxC,KAAK,CAAC,EAAE,cAAc,CAAC;IACvB,gEAAgE;IAChE,aAAa,CAAC,EAAE,mBAAmB,CAAC;IACpC,6CAA6C;IAC7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oleAuditor {
|
|
|
61
61
|
case 'failover':
|
|
62
62
|
console.warn(parts.join(' '), '→', event.metadata?.['nextProvider'] ?? '');
|
|
63
63
|
break;
|
|
64
|
+
case 'structured_request':
|
|
65
|
+
console.log(parts.join(' '), `schema=${event.schemaName ?? 'unknown'}`, '→');
|
|
66
|
+
break;
|
|
67
|
+
case 'structured_response':
|
|
68
|
+
console.log(parts.join(' '), event.duration ? `${event.duration}ms` : '', `schema=${event.schemaName ?? 'unknown'}`);
|
|
69
|
+
break;
|
|
70
|
+
case 'structured_validation_error':
|
|
71
|
+
console.error(parts.join(' '), `schema=${event.schemaName ?? 'unknown'}`, event.error ?? 'Validation failed', event.rawOutput ? `raw=${event.rawOutput.slice(0, 50)}...` : '');
|
|
72
|
+
break;
|
|
64
73
|
}
|
|
65
74
|
}
|
|
66
75
|
}
|
package/dist/auditor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auditor.js","sourceRoot":"","sources":["../src/auditor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
1
|
+
{"version":3,"file":"auditor.js","sourceRoot":"","sources":["../src/auditor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAgEH,+EAA+E;AAC/E,oBAAoB;AACpB,+EAA+E;AAE/E;;;GAGG;AACH,MAAM,OAAO,WAAW;IACpB,MAAM,CAAC,MAAkB;QACrB,sBAAsB;IAC1B,CAAC;CACJ;AAED;;;GAGG;AACH,MAAM,OAAO,cAAc;IACf,MAAM,CAAS;IAEvB,YAAY,SAAiB,OAAO;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED,MAAM,CAAC,KAAiB;QACpB,MAAM,KAAK,GAAG;YACV,IAAI,CAAC,MAAM;YACX,KAAK,CAAC,IAAI,CAAC,WAAW,EAAE;YACxB,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,EA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