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.
- package/CHANGELOG.md +139 -103
- package/LICENSE +21 -21
- package/README.md +591 -591
- package/dist/ai-model.js.map +1 -1
- package/dist/auditor.js.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/http.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +20 -0
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/interfaces.js.map +1 -1
- package/dist/mcp.js.map +1 -1
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +2 -0
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.js.map +1 -1
- package/dist/router.js.map +1 -1
- package/dist/stream-decoder.js.map +1 -1
- package/dist/structured-output.js.map +1 -1
- package/dist/tools.js.map +1 -1
- package/dist/zod-adapter.js.map +1 -1
- package/package.json +115 -116
- package/src/ai-model.ts +0 -350
- package/src/auditor.ts +0 -213
- package/src/client.ts +0 -402
- package/src/debug/debug-google-streaming.ts +0 -97
- package/src/debug/debug-tool-execution.ts +0 -86
- package/src/debug/test-lmstudio-tools.ts +0 -155
- package/src/demos/README.md +0 -47
- package/src/demos/basic/universal-llm-examples.ts +0 -161
- package/src/demos/mcp/astrid-memory-demo.ts +0 -295
- package/src/demos/mcp/astrid-persona-memory.ts +0 -357
- package/src/demos/mcp/mcp-mongodb-demo.ts +0 -275
- package/src/demos/mcp/simple-astrid-memory.ts +0 -148
- package/src/demos/mcp/simple-mcp-demo.ts +0 -68
- package/src/demos/mcp/working-mcp-demo.ts +0 -62
- package/src/demos/model-alias-demo.ts +0 -0
- package/src/demos/tools/RAG_MEMORY_INTEGRATION.md +0 -267
- package/src/demos/tools/astrid-memory-demo.ts +0 -270
- package/src/demos/tools/astrid-production-memory-clean.ts +0 -785
- package/src/demos/tools/astrid-production-memory.ts +0 -558
- package/src/demos/tools/basic-translation-test.ts +0 -66
- package/src/demos/tools/chromadb-similarity-tuning.ts +0 -390
- package/src/demos/tools/clean-multilingual-conversation.ts +0 -209
- package/src/demos/tools/clean-translation-test.ts +0 -119
- package/src/demos/tools/clean-universal-multilingual-test.ts +0 -131
- package/src/demos/tools/complete-rag-demo.ts +0 -369
- package/src/demos/tools/complete-tool-demo.ts +0 -132
- package/src/demos/tools/demo-tool-calling.ts +0 -124
- package/src/demos/tools/dynamic-language-switching-test.ts +0 -251
- package/src/demos/tools/hybrid-thinking-test.ts +0 -154
- package/src/demos/tools/memory-integration-test.ts +0 -420
- package/src/demos/tools/multilingual-memory-system.ts +0 -802
- package/src/demos/tools/ondemand-translation-demo.ts +0 -655
- package/src/demos/tools/production-tool-demo.ts +0 -245
- package/src/demos/tools/revolutionary-multilingual-test.ts +0 -151
- package/src/demos/tools/rigorous-language-analysis.ts +0 -218
- package/src/demos/tools/test-universal-memory-system.ts +0 -126
- package/src/demos/tools/translation-integration-guide.ts +0 -346
- package/src/demos/tools/universal-memory-system.ts +0 -560
- package/src/http.ts +0 -247
- package/src/index.ts +0 -161
- package/src/interfaces.ts +0 -657
- package/src/mcp.ts +0 -345
- package/src/providers/anthropic.ts +0 -762
- package/src/providers/google.ts +0 -620
- package/src/providers/index.ts +0 -8
- package/src/providers/ollama.ts +0 -469
- package/src/providers/openai.ts +0 -392
- package/src/router.ts +0 -780
- package/src/stream-decoder.ts +0 -361
- package/src/structured-output.ts +0 -759
- package/src/test-scripts/test-advanced-tools.ts +0 -310
- package/src/test-scripts/test-google-streaming-enhanced.ts +0 -147
- package/src/test-scripts/test-google-streaming.ts +0 -63
- package/src/test-scripts/test-google-system-prompt-comprehensive.ts +0 -189
- package/src/test-scripts/test-mcp-config.ts +0 -28
- package/src/test-scripts/test-mcp-connection.ts +0 -29
- package/src/test-scripts/test-system-message-positions.ts +0 -163
- package/src/test-scripts/test-system-prompt-improvement-demo.ts +0 -83
- package/src/test-scripts/test-tool-calling.ts +0 -231
- package/src/tests/ai-model.test.ts +0 -1614
- package/src/tests/auditor.test.ts +0 -224
- package/src/tests/http.test.ts +0 -200
- package/src/tests/interfaces.test.ts +0 -117
- package/src/tests/providers/google.test.ts +0 -660
- package/src/tests/providers/ollama.test.ts +0 -954
- package/src/tests/providers/openai.test.ts +0 -1122
- package/src/tests/router.test.ts +0 -254
- package/src/tests/stream-decoder.test.ts +0 -179
- package/src/tests/structured-output.test.ts +0 -1450
- package/src/tests/tools.test.ts +0 -175
- package/src/tools.ts +0 -246
- package/src/zod-adapter.ts +0 -72
package/package.json
CHANGED
|
@@ -1,116 +1,115 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "universal-llm-client",
|
|
3
|
-
"version": "4.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "A universal LLM client with transparent provider failover, streaming tool execution, pluggable reasoning, and native observability.",
|
|
6
|
-
"main": "./dist/index.js",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"import": "./dist/index.js",
|
|
11
|
-
"types": "./dist/index.d.ts"
|
|
12
|
-
},
|
|
13
|
-
"./interfaces": {
|
|
14
|
-
"import": "./dist/interfaces.js",
|
|
15
|
-
"types": "./dist/interfaces.d.ts"
|
|
16
|
-
},
|
|
17
|
-
"./tools": {
|
|
18
|
-
"import": "./dist/tools.js",
|
|
19
|
-
"types": "./dist/tools.d.ts"
|
|
20
|
-
},
|
|
21
|
-
"./auditor": {
|
|
22
|
-
"import": "./dist/auditor.js",
|
|
23
|
-
"types": "./dist/auditor.d.ts"
|
|
24
|
-
},
|
|
25
|
-
"./stream-decoder": {
|
|
26
|
-
"import": "./dist/stream-decoder.js",
|
|
27
|
-
"types": "./dist/stream-decoder.d.ts"
|
|
28
|
-
},
|
|
29
|
-
"./http": {
|
|
30
|
-
"import": "./dist/http.js",
|
|
31
|
-
"types": "./dist/http.d.ts"
|
|
32
|
-
},
|
|
33
|
-
"./structured-output": {
|
|
34
|
-
"import": "./dist/structured-output.js",
|
|
35
|
-
"types": "./dist/structured-output.d.ts"
|
|
36
|
-
},
|
|
37
|
-
"./zod": {
|
|
38
|
-
"import": "./dist/zod-adapter.js",
|
|
39
|
-
"types": "./dist/zod-adapter.d.ts"
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
"files": [
|
|
43
|
-
"dist",
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"docs:
|
|
60
|
-
"docs:
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
"
|
|
82
|
-
|
|
83
|
-
"
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
"
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
"
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
"
|
|
107
|
-
"
|
|
108
|
-
|
|
109
|
-
"
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
"
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "universal-llm-client",
|
|
3
|
+
"version": "4.3.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A universal LLM client with transparent provider failover, streaming tool execution, pluggable reasoning, and native observability.",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./interfaces": {
|
|
14
|
+
"import": "./dist/interfaces.js",
|
|
15
|
+
"types": "./dist/interfaces.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./tools": {
|
|
18
|
+
"import": "./dist/tools.js",
|
|
19
|
+
"types": "./dist/tools.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./auditor": {
|
|
22
|
+
"import": "./dist/auditor.js",
|
|
23
|
+
"types": "./dist/auditor.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./stream-decoder": {
|
|
26
|
+
"import": "./dist/stream-decoder.js",
|
|
27
|
+
"types": "./dist/stream-decoder.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./http": {
|
|
30
|
+
"import": "./dist/http.js",
|
|
31
|
+
"types": "./dist/http.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./structured-output": {
|
|
34
|
+
"import": "./dist/structured-output.js",
|
|
35
|
+
"types": "./dist/structured-output.d.ts"
|
|
36
|
+
},
|
|
37
|
+
"./zod": {
|
|
38
|
+
"import": "./dist/zod-adapter.js",
|
|
39
|
+
"types": "./dist/zod-adapter.d.ts"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"README.md",
|
|
45
|
+
"CHANGELOG.md",
|
|
46
|
+
"LICENSE"
|
|
47
|
+
],
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "bun run clean && bun run compile",
|
|
50
|
+
"clean": "rimraf dist",
|
|
51
|
+
"compile": "tsc --project tsconfig.json",
|
|
52
|
+
"dev": "tsc --watch --project tsconfig.json",
|
|
53
|
+
"prepublishOnly": "bun run build",
|
|
54
|
+
"test": "bun test",
|
|
55
|
+
"typecheck": "tsc --noEmit",
|
|
56
|
+
"lint": "tsc --noEmit --strict",
|
|
57
|
+
"prepack": "bun run build",
|
|
58
|
+
"docs:dev": "vitepress dev docs",
|
|
59
|
+
"docs:build": "vitepress build docs",
|
|
60
|
+
"docs:preview": "vitepress preview docs"
|
|
61
|
+
},
|
|
62
|
+
"keywords": [
|
|
63
|
+
"llm",
|
|
64
|
+
"ai",
|
|
65
|
+
"artificial-intelligence",
|
|
66
|
+
"openai",
|
|
67
|
+
"ollama",
|
|
68
|
+
"google-gemini",
|
|
69
|
+
"vertex-ai",
|
|
70
|
+
"universal",
|
|
71
|
+
"failover",
|
|
72
|
+
"streaming",
|
|
73
|
+
"tool-calling",
|
|
74
|
+
"function-calling",
|
|
75
|
+
"mcp",
|
|
76
|
+
"observability",
|
|
77
|
+
"typescript",
|
|
78
|
+
"esm"
|
|
79
|
+
],
|
|
80
|
+
"dependencies": {},
|
|
81
|
+
"devDependencies": {
|
|
82
|
+
"@types/node": "^22.0.0",
|
|
83
|
+
"rimraf": "^6.0.1",
|
|
84
|
+
"typescript": "^5.8.3",
|
|
85
|
+
"vitepress": "^1.6.4",
|
|
86
|
+
"vue": "^3.5.30",
|
|
87
|
+
"zod": "^4.0.0"
|
|
88
|
+
},
|
|
89
|
+
"peerDependencies": {
|
|
90
|
+
"@modelcontextprotocol/sdk": ">=1.24.0",
|
|
91
|
+
"zod": "^4.0.0"
|
|
92
|
+
},
|
|
93
|
+
"peerDependenciesMeta": {
|
|
94
|
+
"@modelcontextprotocol/sdk": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
97
|
+
"zod": {
|
|
98
|
+
"optional": true
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
"engines": {
|
|
102
|
+
"node": ">=22.0.0",
|
|
103
|
+
"bun": ">=1.0.0"
|
|
104
|
+
},
|
|
105
|
+
"author": "Igor Lins e Silva",
|
|
106
|
+
"license": "MIT",
|
|
107
|
+
"repository": {
|
|
108
|
+
"type": "git",
|
|
109
|
+
"url": "https://github.com/igorls/universal-llm-client.git"
|
|
110
|
+
},
|
|
111
|
+
"homepage": "https://github.com/igorls/universal-llm-client#readme",
|
|
112
|
+
"bugs": {
|
|
113
|
+
"url": "https://github.com/igorls/universal-llm-client/issues"
|
|
114
|
+
}
|
|
115
|
+
}
|
package/src/ai-model.ts
DELETED
|
@@ -1,350 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Universal LLM Client v3 — AIModel (The Universal Client)
|
|
3
|
-
*
|
|
4
|
-
* The only public-facing class. Developers configure one model with
|
|
5
|
-
* multiple provider backends for transparent failover.
|
|
6
|
-
*
|
|
7
|
-
* Provider classes are internal — the user never imports them.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
AIModelApiType,
|
|
12
|
-
type AIModelConfig,
|
|
13
|
-
type ProviderConfig,
|
|
14
|
-
type LLMClientOptions,
|
|
15
|
-
type LLMChatMessage,
|
|
16
|
-
type LLMChatResponse,
|
|
17
|
-
type ChatOptions,
|
|
18
|
-
type ModelMetadata,
|
|
19
|
-
type LLMFunction,
|
|
20
|
-
type ToolHandler,
|
|
21
|
-
} from './interfaces.js';
|
|
22
|
-
import type { DecodedEvent } from './stream-decoder.js';
|
|
23
|
-
import { Router, type RouterConfig, type ProviderStatus } from './router.js';
|
|
24
|
-
import type { Auditor } from './auditor.js';
|
|
25
|
-
import { NoopAuditor } from './auditor.js';
|
|
26
|
-
import { OllamaClient } from './providers/ollama.js';
|
|
27
|
-
import { OpenAICompatibleClient } from './providers/openai.js';
|
|
28
|
-
import { GoogleClient } from './providers/google.js';
|
|
29
|
-
import { AnthropicClient } from './providers/anthropic.js';
|
|
30
|
-
import { BaseLLMClient } from './client.js';
|
|
31
|
-
import {
|
|
32
|
-
type StructuredOutputResult,
|
|
33
|
-
type SchemaConfig,
|
|
34
|
-
} from './structured-output.js';
|
|
35
|
-
|
|
36
|
-
// ============================================================================
|
|
37
|
-
// Default Provider URLs
|
|
38
|
-
// ============================================================================
|
|
39
|
-
|
|
40
|
-
const DEFAULT_URLS: Record<string, string> = {
|
|
41
|
-
ollama: 'http://localhost:11434',
|
|
42
|
-
openai: 'https://api.openai.com',
|
|
43
|
-
llamacpp: 'http://localhost:8080',
|
|
44
|
-
anthropic: 'https://api.anthropic.com',
|
|
45
|
-
// google and vertex build their own URLs internally
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// ============================================================================
|
|
49
|
-
// AIModel — The Universal Client
|
|
50
|
-
// ============================================================================
|
|
51
|
-
|
|
52
|
-
export class AIModel {
|
|
53
|
-
private router: Router;
|
|
54
|
-
private auditor: Auditor;
|
|
55
|
-
private config: AIModelConfig;
|
|
56
|
-
|
|
57
|
-
constructor(config: AIModelConfig) {
|
|
58
|
-
this.config = config;
|
|
59
|
-
this.auditor = config.auditor ?? new NoopAuditor();
|
|
60
|
-
|
|
61
|
-
const routerConfig: RouterConfig = {
|
|
62
|
-
retriesPerProvider: config.retries ?? 2,
|
|
63
|
-
auditor: this.auditor,
|
|
64
|
-
};
|
|
65
|
-
this.router = new Router(routerConfig);
|
|
66
|
-
|
|
67
|
-
// Initialize providers in order
|
|
68
|
-
for (let i = 0; i < config.providers.length; i++) {
|
|
69
|
-
const providerConfig = config.providers[i]!;
|
|
70
|
-
const client = this.createClient(providerConfig);
|
|
71
|
-
const id = `${this.normalizeType(providerConfig.type)}-${i}`;
|
|
72
|
-
|
|
73
|
-
this.router.addProvider({
|
|
74
|
-
id,
|
|
75
|
-
client,
|
|
76
|
-
priority: providerConfig.priority ?? i,
|
|
77
|
-
modelOverride: providerConfig.model,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// ========================================================================
|
|
83
|
-
// Chat
|
|
84
|
-
// ========================================================================
|
|
85
|
-
|
|
86
|
-
/** Send a chat request with automatic failover across providers */
|
|
87
|
-
async chat(
|
|
88
|
-
messages: LLMChatMessage[],
|
|
89
|
-
options?: ChatOptions,
|
|
90
|
-
): Promise<LLMChatResponse> {
|
|
91
|
-
return this.router.chat(messages, options);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Chat with automatic tool execution (multi-turn loop) */
|
|
95
|
-
async chatWithTools(
|
|
96
|
-
messages: LLMChatMessage[],
|
|
97
|
-
options?: ChatOptions & { maxIterations?: number },
|
|
98
|
-
): Promise<LLMChatResponse> {
|
|
99
|
-
return this.router.chatWithTools(messages, options);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** Stream chat response with pluggable decoder strategy */
|
|
103
|
-
async *chatStream(
|
|
104
|
-
messages: LLMChatMessage[],
|
|
105
|
-
options?: ChatOptions,
|
|
106
|
-
): AsyncGenerator<DecodedEvent, LLMChatResponse | void, unknown> {
|
|
107
|
-
return yield* this.router.chatStream(messages, options);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// ========================================================================
|
|
111
|
-
// Structured Output
|
|
112
|
-
// ========================================================================
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Generate structured output from the LLM with automatic failover.
|
|
116
|
-
* Validates the response against the provided Zod schema.
|
|
117
|
-
* Throws StructuredOutputError on validation failure.
|
|
118
|
-
*
|
|
119
|
-
* @template T The output type
|
|
120
|
-
* @param config Schema configuration (JSON Schema + optional validator)
|
|
121
|
-
* @param messages Chat messages to send
|
|
122
|
-
* @param options Additional options (temperature, maxTokens, etc.)
|
|
123
|
-
* @returns Promise resolving to validated structured output
|
|
124
|
-
* @throws StructuredOutputError if JSON parsing fails or validation fails
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* ```typescript
|
|
128
|
-
* import { fromZod } from 'universal-llm-client/zod';
|
|
129
|
-
* const UserConfig = fromZod(z.object({
|
|
130
|
-
* name: z.string(),
|
|
131
|
-
* age: z.number(),
|
|
132
|
-
* }));
|
|
133
|
-
*
|
|
134
|
-
* const user = await model.generateStructured(UserConfig, [
|
|
135
|
-
* { role: 'user', content: 'Generate a user profile' },
|
|
136
|
-
* ]);
|
|
137
|
-
* // user.name: string, user.age: number
|
|
138
|
-
* ```
|
|
139
|
-
*/
|
|
140
|
-
async generateStructured<T>(
|
|
141
|
-
config: SchemaConfig<T>,
|
|
142
|
-
messages: LLMChatMessage[],
|
|
143
|
-
options?: ChatOptions,
|
|
144
|
-
): Promise<T> {
|
|
145
|
-
return this.router.generateStructured(config, messages, options);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Try to generate structured output, returning a result object instead of throwing.
|
|
150
|
-
* Same as generateStructured but returns { ok: true, value } on success
|
|
151
|
-
* and { ok: false, error, rawOutput } on failure.
|
|
152
|
-
*
|
|
153
|
-
* @template T The output type
|
|
154
|
-
* @param config Schema configuration (JSON Schema + optional validator)
|
|
155
|
-
* @param messages Chat messages to send
|
|
156
|
-
* @param options Additional options (temperature, maxTokens, etc.)
|
|
157
|
-
* @returns StructuredOutputResult<T> - either success with value or failure with error
|
|
158
|
-
*
|
|
159
|
-
* @example
|
|
160
|
-
* ```typescript
|
|
161
|
-
* const result = await model.tryParseStructured(config, messages);
|
|
162
|
-
*
|
|
163
|
-
* if (result.ok) {
|
|
164
|
-
* console.log('User:', result.value.name);
|
|
165
|
-
* } else {
|
|
166
|
-
* console.log('Error:', result.error.message);
|
|
167
|
-
* console.log('Raw output:', result.rawOutput);
|
|
168
|
-
* }
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
|
-
async tryParseStructured<T>(
|
|
172
|
-
config: SchemaConfig<T>,
|
|
173
|
-
messages: LLMChatMessage[],
|
|
174
|
-
options?: ChatOptions,
|
|
175
|
-
): Promise<StructuredOutputResult<T>> {
|
|
176
|
-
return this.router.tryParseStructured(config, messages, options);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Stream structured output with partial validated objects.
|
|
181
|
-
*
|
|
182
|
-
* Yields partial validated objects as JSON generates, then returns the
|
|
183
|
-
* complete validated object on stream completion.
|
|
184
|
-
*
|
|
185
|
-
* For invalid partial JSON, no yield occurs (partial validation is best-effort).
|
|
186
|
-
* On stream completion, if the final JSON fails validation, throws StructuredOutputError.
|
|
187
|
-
*
|
|
188
|
-
* @template T The output type
|
|
189
|
-
* @param config Schema configuration (JSON Schema + optional validator)
|
|
190
|
-
* @param messages Chat messages to send
|
|
191
|
-
* @param options Additional options (temperature, maxTokens, etc.)
|
|
192
|
-
* @yields Partial validated objects as the JSON stream progresses
|
|
193
|
-
* @returns Complete validated object on stream completion
|
|
194
|
-
* @throws StructuredOutputError if final validation fails
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```typescript
|
|
198
|
-
* import { fromZod } from 'universal-llm-client/zod';
|
|
199
|
-
* const UserConfig = fromZod(z.object({
|
|
200
|
-
* name: z.string(),
|
|
201
|
-
* age: z.number(),
|
|
202
|
-
* }));
|
|
203
|
-
*
|
|
204
|
-
* const stream = model.generateStructuredStream(UserConfig, [
|
|
205
|
-
* { role: 'user', content: 'Generate a user' },
|
|
206
|
-
* ]);
|
|
207
|
-
*
|
|
208
|
-
* for await (const partial of stream) {
|
|
209
|
-
* console.log('Partial user:', partial);
|
|
210
|
-
* }
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
async *generateStructuredStream<T>(
|
|
214
|
-
config: SchemaConfig<T>,
|
|
215
|
-
messages: LLMChatMessage[],
|
|
216
|
-
options?: ChatOptions,
|
|
217
|
-
): AsyncGenerator<T, T, unknown> {
|
|
218
|
-
return yield* this.router.generateStructuredStream(config, messages, options);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// ========================================================================
|
|
222
|
-
// Embeddings
|
|
223
|
-
// ========================================================================
|
|
224
|
-
|
|
225
|
-
/** Generate embedding for a single text */
|
|
226
|
-
async embed(text: string): Promise<number[]> {
|
|
227
|
-
return this.router.embed(text);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/** Generate embeddings for multiple texts */
|
|
231
|
-
async embedArray(texts: string[]): Promise<number[][]> {
|
|
232
|
-
return this.router.embedArray(texts);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// ========================================================================
|
|
236
|
-
// Tool Registration
|
|
237
|
-
// ========================================================================
|
|
238
|
-
|
|
239
|
-
/** Register a tool callable by the LLM (broadcast to all providers) */
|
|
240
|
-
registerTool(
|
|
241
|
-
name: string,
|
|
242
|
-
description: string,
|
|
243
|
-
parameters: LLMFunction['parameters'],
|
|
244
|
-
handler: ToolHandler,
|
|
245
|
-
): void {
|
|
246
|
-
this.router.registerTool(name, description, parameters, handler);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/** Register multiple tools at once */
|
|
250
|
-
registerTools(
|
|
251
|
-
tools: Array<{
|
|
252
|
-
name: string;
|
|
253
|
-
description: string;
|
|
254
|
-
parameters: LLMFunction['parameters'];
|
|
255
|
-
handler: ToolHandler;
|
|
256
|
-
}>,
|
|
257
|
-
): void {
|
|
258
|
-
this.router.registerTools(tools);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// ========================================================================
|
|
262
|
-
// Model Management
|
|
263
|
-
// ========================================================================
|
|
264
|
-
|
|
265
|
-
/** Get available models from all configured providers */
|
|
266
|
-
async getModels(): Promise<string[]> {
|
|
267
|
-
return this.router.getModels();
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/** Get metadata about the current model (context length, capabilities) */
|
|
271
|
-
async getModelInfo(): Promise<ModelMetadata> {
|
|
272
|
-
return this.router.getModelInfo();
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/** Switch model at runtime (updates all providers) */
|
|
276
|
-
setModel(name: string): void {
|
|
277
|
-
this.config.model = name;
|
|
278
|
-
// The model name change will be picked up by the providers
|
|
279
|
-
// through the router on next request
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/** Get the current model name */
|
|
283
|
-
get model(): string {
|
|
284
|
-
return this.config.model;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// ========================================================================
|
|
288
|
-
// Provider Status
|
|
289
|
-
// ========================================================================
|
|
290
|
-
|
|
291
|
-
/** Get health/status of all configured providers */
|
|
292
|
-
getProviderStatus(): ProviderStatus[] {
|
|
293
|
-
return this.router.getStatus();
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// ========================================================================
|
|
297
|
-
// Lifecycle
|
|
298
|
-
// ========================================================================
|
|
299
|
-
|
|
300
|
-
/** Clean shutdown — flush auditor, disconnect MCP, etc. */
|
|
301
|
-
async dispose(): Promise<void> {
|
|
302
|
-
await this.auditor.flush?.();
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// ========================================================================
|
|
306
|
-
// Internal: Provider Factory
|
|
307
|
-
// ========================================================================
|
|
308
|
-
|
|
309
|
-
private createClient(providerConfig: ProviderConfig): BaseLLMClient {
|
|
310
|
-
const type = this.normalizeType(providerConfig.type);
|
|
311
|
-
const modelName = providerConfig.model ?? this.config.model;
|
|
312
|
-
|
|
313
|
-
const clientOptions: LLMClientOptions = {
|
|
314
|
-
model: modelName,
|
|
315
|
-
url: providerConfig.url ?? DEFAULT_URLS[type] ?? '',
|
|
316
|
-
apiType: type as AIModelApiType,
|
|
317
|
-
apiKey: providerConfig.apiKey,
|
|
318
|
-
timeout: this.config.timeout ?? 30000,
|
|
319
|
-
retries: this.config.retries ?? 2,
|
|
320
|
-
debug: this.config.debug ?? false,
|
|
321
|
-
defaultParameters: this.config.defaultParameters,
|
|
322
|
-
thinking: this.config.thinking ?? false,
|
|
323
|
-
region: providerConfig.region,
|
|
324
|
-
apiVersion: providerConfig.apiVersion,
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
switch (type) {
|
|
328
|
-
case 'ollama':
|
|
329
|
-
return new OllamaClient(clientOptions, this.auditor);
|
|
330
|
-
|
|
331
|
-
case 'openai':
|
|
332
|
-
case 'llamacpp':
|
|
333
|
-
return new OpenAICompatibleClient(clientOptions, this.auditor);
|
|
334
|
-
|
|
335
|
-
case 'google':
|
|
336
|
-
case 'vertex':
|
|
337
|
-
return new GoogleClient(clientOptions, this.auditor);
|
|
338
|
-
|
|
339
|
-
case 'anthropic':
|
|
340
|
-
return new AnthropicClient(clientOptions, this.auditor);
|
|
341
|
-
|
|
342
|
-
default:
|
|
343
|
-
throw new Error(`Unknown provider type: ${type}`);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
private normalizeType(type: string): string {
|
|
348
|
-
return type.toLowerCase();
|
|
349
|
-
}
|
|
350
|
-
}
|