ai-providers 0.2.0 → 0.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/.turbo/turbo-build.log +5 -0
- package/.turbo/turbo-test.log +47 -0
- package/README.md +204 -0
- package/dist/index.d.ts +12 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -0
- package/dist/llm.do.d.ts +209 -0
- package/dist/llm.do.d.ts.map +1 -0
- package/dist/llm.do.js +408 -0
- package/dist/llm.do.js.map +1 -0
- package/dist/providers/cloudflare.d.ts +92 -0
- package/dist/providers/cloudflare.d.ts.map +1 -0
- package/dist/providers/cloudflare.js +127 -0
- package/dist/providers/cloudflare.js.map +1 -0
- package/dist/registry.d.ts +136 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +393 -0
- package/dist/registry.js.map +1 -0
- package/package.json +45 -30
- package/src/index.test.ts +341 -0
- package/src/index.ts +37 -0
- package/src/integration.test.ts +317 -0
- package/src/llm.do.test.ts +781 -0
- package/src/llm.do.ts +532 -0
- package/src/providers/cloudflare.test.ts +574 -0
- package/src/providers/cloudflare.ts +216 -0
- package/src/registry.test.ts +491 -0
- package/src/registry.ts +480 -0
- package/tsconfig.json +9 -0
- package/vitest.config.ts +36 -0
- package/dist/provider.d.ts +0 -50
- package/dist/provider.js +0 -128
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
|
|
2
|
+
> ai-providers@0.0.1 test /Users/nathanclevenger/projects/mdx.org.ai/primitives/packages/ai-providers
|
|
3
|
+
> vitest
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
DEV v2.1.9 /Users/nathanclevenger/projects/mdx.org.ai/primitives/packages/ai-providers
|
|
7
|
+
|
|
8
|
+
✓ src/llm.do.test.ts (42 tests) 44ms
|
|
9
|
+
✓ src/providers/cloudflare.test.ts (45 tests) 7ms
|
|
10
|
+
✓ src/registry.test.ts (51 tests) 37ms
|
|
11
|
+
✓ src/index.test.ts (37 tests) 4ms
|
|
12
|
+
✓ src/integration.test.ts (26 tests | 6 skipped) 8ms
|
|
13
|
+
|
|
14
|
+
Test Files 5 passed (5)
|
|
15
|
+
Tests 195 passed | 6 skipped (201)
|
|
16
|
+
Start at 14:14:37
|
|
17
|
+
Duration 786ms (transform 114ms, setup 0ms, collect 245ms, tests 101ms, environment 0ms, prepare 45ms)
|
|
18
|
+
|
|
19
|
+
PASS Waiting for file changes...
|
|
20
|
+
press h to show help, press q to quit
|
|
21
|
+
c[3J RERUN ../language-models/dist/aliases.js
|
|
22
|
+
|
|
23
|
+
✓ src/registry.test.ts (51 tests) 116ms
|
|
24
|
+
✓ src/integration.test.ts (26 tests | 6 skipped) 10ms
|
|
25
|
+
✓ src/index.test.ts (37 tests) 7ms
|
|
26
|
+
|
|
27
|
+
Test Files 3 passed (3)
|
|
28
|
+
Tests 108 passed | 6 skipped (114)
|
|
29
|
+
Start at 14:22:55
|
|
30
|
+
Duration 436ms
|
|
31
|
+
|
|
32
|
+
PASS Waiting for file changes...
|
|
33
|
+
press h to show help, press q to quit
|
|
34
|
+
c[3J RERUN ../language-models/dist/aliases.js
|
|
35
|
+
|
|
36
|
+
✓ src/registry.test.ts (51 tests) 36ms
|
|
37
|
+
✓ src/integration.test.ts (26 tests | 6 skipped) 6ms
|
|
38
|
+
✓ src/index.test.ts (37 tests) 5ms
|
|
39
|
+
|
|
40
|
+
Test Files 3 passed (3)
|
|
41
|
+
Tests 108 passed | 6 skipped (114)
|
|
42
|
+
Start at 14:31:30
|
|
43
|
+
Duration 121ms
|
|
44
|
+
|
|
45
|
+
PASS Waiting for file changes...
|
|
46
|
+
press h to show help, press q to quit
|
|
47
|
+
ELIFECYCLE Test failed. See above for more details.
|
package/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# ai-providers
|
|
2
|
+
|
|
3
|
+
Unified AI provider registry with Cloudflare AI Gateway support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add ai-providers
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { model } from 'ai-providers'
|
|
15
|
+
import { generateText } from 'ai'
|
|
16
|
+
|
|
17
|
+
// Simple aliases - just works
|
|
18
|
+
const { text } = await generateText({
|
|
19
|
+
model: await model('sonnet'), // → anthropic/claude-sonnet-4.5
|
|
20
|
+
prompt: 'Hello!'
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
// All these work too
|
|
24
|
+
await model('opus') // → anthropic/claude-opus-4.5
|
|
25
|
+
await model('gpt-4o') // → openai/gpt-4o
|
|
26
|
+
await model('gemini') // → google/gemini-2.5-flash
|
|
27
|
+
await model('llama-70b') // → meta-llama/llama-3.3-70b-instruct
|
|
28
|
+
await model('mistral') // → mistralai/mistral-large-2411
|
|
29
|
+
await model('deepseek') // → deepseek/deepseek-chat
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## How It Works
|
|
33
|
+
|
|
34
|
+
### Smart Routing
|
|
35
|
+
|
|
36
|
+
The `model()` function uses intelligent routing based on model data from OpenRouter:
|
|
37
|
+
|
|
38
|
+
1. **Direct Provider Routing** - When `provider_model_id` is available and the provider matches (openai, anthropic, google), routes directly to the provider's native SDK. This enables provider-specific features like:
|
|
39
|
+
- Anthropic: MCP (Model Context Protocol), extended thinking
|
|
40
|
+
- OpenAI: Function calling, JSON mode, vision
|
|
41
|
+
- Google: Grounding, code execution
|
|
42
|
+
|
|
43
|
+
2. **OpenRouter Fallback** - All other models route through OpenRouter, which provides:
|
|
44
|
+
- 200+ models from all major providers
|
|
45
|
+
- Unified API with consistent model ID format
|
|
46
|
+
- Automatic model ID translation
|
|
47
|
+
- Fallback routing if a provider is down
|
|
48
|
+
|
|
49
|
+
Model aliases are resolved by the `language-models` package, which includes `provider_model_id` data from OpenRouter's API for direct routing when available.
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
### Cloudflare AI Gateway (Recommended)
|
|
54
|
+
|
|
55
|
+
Set up a Cloudflare AI Gateway with stored secrets for each provider:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
export AI_GATEWAY_URL=https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}
|
|
59
|
+
export AI_GATEWAY_TOKEN=your-gateway-auth-token
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The gateway handles authentication - you don't need individual API keys.
|
|
63
|
+
|
|
64
|
+
### Direct API Keys (Fallback)
|
|
65
|
+
|
|
66
|
+
If not using a gateway:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
export OPENROUTER_API_KEY=sk-or-...
|
|
70
|
+
export OPENAI_API_KEY=sk-... # for embeddings
|
|
71
|
+
export CLOUDFLARE_ACCOUNT_ID=... # for CF embeddings
|
|
72
|
+
export CLOUDFLARE_API_TOKEN=... # for CF embeddings
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## API
|
|
76
|
+
|
|
77
|
+
### `model(id: string)`
|
|
78
|
+
|
|
79
|
+
Get a language model by alias or full ID.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { model } from 'ai-providers'
|
|
83
|
+
|
|
84
|
+
// Aliases (requires language-models package)
|
|
85
|
+
await model('opus') // anthropic/claude-opus-4.5
|
|
86
|
+
await model('sonnet') // anthropic/claude-sonnet-4.5
|
|
87
|
+
await model('gpt-4o') // openai/gpt-4o
|
|
88
|
+
await model('llama') // meta-llama/llama-4-maverick
|
|
89
|
+
|
|
90
|
+
// Full IDs always work
|
|
91
|
+
await model('anthropic/claude-opus-4.5')
|
|
92
|
+
await model('mistralai/codestral-2501')
|
|
93
|
+
await model('meta-llama/llama-3.3-70b-instruct')
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `embeddingModel(id: string)`
|
|
97
|
+
|
|
98
|
+
Get an embedding model.
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { embeddingModel } from 'ai-providers'
|
|
102
|
+
|
|
103
|
+
await embeddingModel('openai:text-embedding-3-small')
|
|
104
|
+
await embeddingModel('cloudflare:@cf/baai/bge-m3')
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### `createRegistry(config?)`
|
|
108
|
+
|
|
109
|
+
Create a custom provider registry.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { createRegistry } from 'ai-providers'
|
|
113
|
+
|
|
114
|
+
const registry = await createRegistry({
|
|
115
|
+
gatewayUrl: 'https://gateway.ai.cloudflare.com/v1/...',
|
|
116
|
+
gatewayToken: 'your-token'
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
// Use registry directly
|
|
120
|
+
const model = registry.languageModel('openrouter:anthropic/claude-sonnet-4.5')
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### `getRegistry()`
|
|
124
|
+
|
|
125
|
+
Get the default singleton registry (lazily created).
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { getRegistry } from 'ai-providers'
|
|
129
|
+
|
|
130
|
+
const registry = await getRegistry()
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Model Aliases
|
|
134
|
+
|
|
135
|
+
When `language-models` is installed, these aliases work:
|
|
136
|
+
|
|
137
|
+
| Alias | Model ID |
|
|
138
|
+
|-------|----------|
|
|
139
|
+
| `opus` | anthropic/claude-opus-4.5 |
|
|
140
|
+
| `sonnet` | anthropic/claude-sonnet-4.5 |
|
|
141
|
+
| `haiku` | anthropic/claude-haiku-4.5 |
|
|
142
|
+
| `gpt`, `gpt-4o` | openai/gpt-4o |
|
|
143
|
+
| `o1`, `o3` | openai/o1, openai/o3 |
|
|
144
|
+
| `gemini`, `flash` | google/gemini-2.5-flash |
|
|
145
|
+
| `llama`, `llama-4` | meta-llama/llama-4-maverick |
|
|
146
|
+
| `mistral` | mistralai/mistral-large-2411 |
|
|
147
|
+
| `deepseek`, `r1` | deepseek/deepseek-chat, deepseek/deepseek-r1 |
|
|
148
|
+
| `qwen` | qwen/qwen3-235b-a22b |
|
|
149
|
+
| `grok` | x-ai/grok-3 |
|
|
150
|
+
|
|
151
|
+
## Cloudflare Embeddings
|
|
152
|
+
|
|
153
|
+
The package includes a Cloudflare Workers AI embedding provider:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import { cloudflareEmbedding } from 'ai-providers/cloudflare'
|
|
157
|
+
import { embed } from 'ai'
|
|
158
|
+
|
|
159
|
+
const model = cloudflareEmbedding('@cf/baai/bge-m3')
|
|
160
|
+
const { embedding } = await embed({ model, value: 'Hello world' })
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Available models:
|
|
164
|
+
- `@cf/baai/bge-m3` (default, multilingual)
|
|
165
|
+
- `@cf/baai/bge-base-en-v1.5`
|
|
166
|
+
- `@cf/baai/bge-large-en-v1.5`
|
|
167
|
+
- `@cf/baai/bge-small-en-v1.5`
|
|
168
|
+
|
|
169
|
+
## Architecture
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
┌─────────────────┐
|
|
173
|
+
│ ai-functions │ Uses model() for generation
|
|
174
|
+
└────────┬────────┘
|
|
175
|
+
│
|
|
176
|
+
┌────────▼────────┐
|
|
177
|
+
│ ai-providers │ Resolves aliases, smart routing
|
|
178
|
+
└────────┬────────┘
|
|
179
|
+
│
|
|
180
|
+
┌────────▼────────┐
|
|
181
|
+
│ language-models │ Model data with provider_model_id
|
|
182
|
+
└────────┬────────┘
|
|
183
|
+
│
|
|
184
|
+
┌────┴────┐
|
|
185
|
+
│ │
|
|
186
|
+
┌───▼───┐ ┌───▼───┐
|
|
187
|
+
│Direct │ │OpenRou│ Direct: openai, anthropic, google
|
|
188
|
+
│SDK │ │ter │ OpenRouter: all other providers
|
|
189
|
+
└───────┘ └───────┘
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Gateway Authentication
|
|
193
|
+
|
|
194
|
+
When using Cloudflare AI Gateway with stored secrets:
|
|
195
|
+
|
|
196
|
+
1. Gateway URL points to your gateway endpoint
|
|
197
|
+
2. `AI_GATEWAY_TOKEN` authenticates with the gateway
|
|
198
|
+
3. Gateway injects provider API keys from its stored secrets
|
|
199
|
+
4. No individual API keys needed in your app
|
|
200
|
+
|
|
201
|
+
The package automatically:
|
|
202
|
+
- Strips SDK-added API key headers
|
|
203
|
+
- Adds `cf-aig-authorization` header for gateway auth
|
|
204
|
+
- Lets the gateway inject the real API keys
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* ai-providers - Unified AI Provider Registry
|
|
3
|
+
*
|
|
4
|
+
* Access multiple AI providers via simple string identifiers.
|
|
5
|
+
* Supports Cloudflare AI Gateway for unified routing and auth.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export { createRegistry, getRegistry, configureRegistry, model, embeddingModel, DIRECT_PROVIDERS, type ProviderId, type DirectProvider, type ProviderConfig } from './registry.js';
|
|
10
|
+
export { LLM, getLLM, createLLMFetch, type LLMConfig, type UniversalRequest, type UniversalCreated, type UniversalStream, type UniversalDone, type UniversalError, type GatewayMessage } from './llm.do.js';
|
|
11
|
+
export type { Provider, ProviderRegistryProvider } from 'ai';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,cAAc,EACd,WAAW,EACX,iBAAiB,EACjB,KAAK,EACL,cAAc,EACd,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,cAAc,EACpB,MAAM,eAAe,CAAA;AAGtB,OAAO,EACL,GAAG,EACH,MAAM,EACN,cAAc,EACd,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACpB,MAAM,aAAa,CAAA;AAGpB,YAAY,EAAE,QAAQ,EAAE,wBAAwB,EAAE,MAAM,IAAI,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* ai-providers - Unified AI Provider Registry
|
|
3
|
+
*
|
|
4
|
+
* Access multiple AI providers via simple string identifiers.
|
|
5
|
+
* Supports Cloudflare AI Gateway for unified routing and auth.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export { createRegistry, getRegistry, configureRegistry, model, embeddingModel, DIRECT_PROVIDERS } from './registry.js';
|
|
10
|
+
// Export llm.do WebSocket transport
|
|
11
|
+
export { LLM, getLLM, createLLMFetch } from './llm.do.js';
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,cAAc,EACd,WAAW,EACX,iBAAiB,EACjB,KAAK,EACL,cAAc,EACd,gBAAgB,EAIjB,MAAM,eAAe,CAAA;AAEtB,oCAAoC;AACpC,OAAO,EACL,GAAG,EACH,MAAM,EACN,cAAc,EAQf,MAAM,aAAa,CAAA"}
|
package/dist/llm.do.d.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* llm.do - Universal LLM Gateway WebSocket Transport
|
|
3
|
+
*
|
|
4
|
+
* Provides a persistent WebSocket connection to llm.do gateway
|
|
5
|
+
* that replaces repeated fetch calls with multiplexed requests.
|
|
6
|
+
*
|
|
7
|
+
* Benefits:
|
|
8
|
+
* - Persistent connection (no repeated handshakes)
|
|
9
|
+
* - Lower latency for multiple requests
|
|
10
|
+
* - Unified authentication across all providers
|
|
11
|
+
* - Automatic provider routing
|
|
12
|
+
*
|
|
13
|
+
* @packageDocumentation
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* WebSocket message types (Cloudflare AI Gateway protocol)
|
|
17
|
+
*/
|
|
18
|
+
export interface UniversalRequest {
|
|
19
|
+
type: 'universal.create';
|
|
20
|
+
request: {
|
|
21
|
+
eventId: string;
|
|
22
|
+
provider: string;
|
|
23
|
+
endpoint: string;
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
query?: Record<string, unknown>;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export interface UniversalCreated {
|
|
29
|
+
type: 'universal.created';
|
|
30
|
+
eventId: string;
|
|
31
|
+
metadata?: {
|
|
32
|
+
cacheStatus?: string;
|
|
33
|
+
requestId?: string;
|
|
34
|
+
};
|
|
35
|
+
response: {
|
|
36
|
+
status: number;
|
|
37
|
+
headers?: Record<string, string>;
|
|
38
|
+
body: unknown;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export interface UniversalStream {
|
|
42
|
+
type: 'universal.stream';
|
|
43
|
+
eventId: string;
|
|
44
|
+
chunk: string | unknown;
|
|
45
|
+
}
|
|
46
|
+
export interface UniversalDone {
|
|
47
|
+
type: 'universal.done';
|
|
48
|
+
eventId: string;
|
|
49
|
+
metadata?: {
|
|
50
|
+
cacheStatus?: string;
|
|
51
|
+
requestId?: string;
|
|
52
|
+
usage?: {
|
|
53
|
+
promptTokens?: number;
|
|
54
|
+
completionTokens?: number;
|
|
55
|
+
totalTokens?: number;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export interface UniversalError {
|
|
60
|
+
type: 'universal.error';
|
|
61
|
+
eventId: string;
|
|
62
|
+
error: {
|
|
63
|
+
message: string;
|
|
64
|
+
code?: string;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export type GatewayMessage = UniversalCreated | UniversalStream | UniversalDone | UniversalError;
|
|
68
|
+
/**
|
|
69
|
+
* Connection state
|
|
70
|
+
*/
|
|
71
|
+
type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'closed';
|
|
72
|
+
/**
|
|
73
|
+
* llm.do configuration
|
|
74
|
+
*/
|
|
75
|
+
export interface LLMConfig {
|
|
76
|
+
/** llm.do WebSocket URL (default: wss://llm.do/ws) */
|
|
77
|
+
url?: string;
|
|
78
|
+
/** Authentication token (DO_TOKEN) */
|
|
79
|
+
token: string;
|
|
80
|
+
/** Auto-reconnect on disconnect (default: true) */
|
|
81
|
+
autoReconnect?: boolean;
|
|
82
|
+
/** Max reconnect attempts (default: 5) */
|
|
83
|
+
maxReconnectAttempts?: number;
|
|
84
|
+
/** Reconnect delay in ms (default: 1000) */
|
|
85
|
+
reconnectDelay?: number;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* llm.do WebSocket client
|
|
89
|
+
*
|
|
90
|
+
* Maintains a persistent WebSocket connection and provides a fetch-compatible
|
|
91
|
+
* interface that routes requests through llm.do gateway.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* const llm = new LLM({ token: process.env.DO_TOKEN })
|
|
96
|
+
*
|
|
97
|
+
* await llm.connect()
|
|
98
|
+
*
|
|
99
|
+
* // Use as fetch replacement
|
|
100
|
+
* const response = await llm.fetch('https://api.openai.com/v1/chat/completions', {
|
|
101
|
+
* method: 'POST',
|
|
102
|
+
* body: JSON.stringify({ ... })
|
|
103
|
+
* })
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare class LLM {
|
|
107
|
+
private ws;
|
|
108
|
+
private state;
|
|
109
|
+
private pendingRequests;
|
|
110
|
+
private connectPromise;
|
|
111
|
+
private reconnectAttempts;
|
|
112
|
+
private eventIdCounter;
|
|
113
|
+
readonly config: Required<LLMConfig>;
|
|
114
|
+
constructor(config: LLMConfig);
|
|
115
|
+
/**
|
|
116
|
+
* Get the WebSocket URL
|
|
117
|
+
*/
|
|
118
|
+
get wsUrl(): string;
|
|
119
|
+
/**
|
|
120
|
+
* Get current connection state
|
|
121
|
+
*/
|
|
122
|
+
get connectionState(): ConnectionState;
|
|
123
|
+
/**
|
|
124
|
+
* Check if connected
|
|
125
|
+
*/
|
|
126
|
+
get isConnected(): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Connect to llm.do
|
|
129
|
+
*/
|
|
130
|
+
connect(): Promise<void>;
|
|
131
|
+
private doConnect;
|
|
132
|
+
private handleDisconnect;
|
|
133
|
+
private handleMessage;
|
|
134
|
+
/**
|
|
135
|
+
* Generate a unique event ID
|
|
136
|
+
*/
|
|
137
|
+
private generateEventId;
|
|
138
|
+
/**
|
|
139
|
+
* Detect provider from URL
|
|
140
|
+
*/
|
|
141
|
+
private detectProvider;
|
|
142
|
+
/**
|
|
143
|
+
* Extract endpoint path from URL
|
|
144
|
+
*/
|
|
145
|
+
private extractEndpoint;
|
|
146
|
+
/**
|
|
147
|
+
* Fetch-compatible interface that routes through llm.do
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const response = await llm.fetch('https://api.openai.com/v1/chat/completions', {
|
|
152
|
+
* method: 'POST',
|
|
153
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
154
|
+
* body: JSON.stringify({
|
|
155
|
+
* model: 'gpt-4o',
|
|
156
|
+
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
157
|
+
* })
|
|
158
|
+
* })
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
fetch(url: string | URL, init?: RequestInit): Promise<Response>;
|
|
162
|
+
/**
|
|
163
|
+
* Create a fetch function bound to this connection
|
|
164
|
+
*
|
|
165
|
+
* Returns a function that can be used as a drop-in replacement for fetch
|
|
166
|
+
* in AI SDK provider configurations.
|
|
167
|
+
*/
|
|
168
|
+
createFetch(): typeof fetch;
|
|
169
|
+
/**
|
|
170
|
+
* Close the WebSocket connection
|
|
171
|
+
*/
|
|
172
|
+
close(): void;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get or create the default llm.do connection
|
|
176
|
+
*
|
|
177
|
+
* Uses environment variables for configuration:
|
|
178
|
+
* - LLM_URL: WebSocket URL (default: wss://llm.do/ws)
|
|
179
|
+
* - DO_TOKEN: Authentication token
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* const llm = getLLM()
|
|
184
|
+
* await llm.connect()
|
|
185
|
+
* const response = await llm.fetch(url, options)
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export declare function getLLM(config?: LLMConfig): LLM;
|
|
189
|
+
/**
|
|
190
|
+
* Create a fetch function that uses llm.do WebSocket
|
|
191
|
+
*
|
|
192
|
+
* Drop-in replacement for the custom fetch in provider configuration.
|
|
193
|
+
* Automatically connects on first use.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* import { createOpenAI } from '@ai-sdk/openai'
|
|
198
|
+
* import { createLLMFetch } from 'ai-providers'
|
|
199
|
+
*
|
|
200
|
+
* const openai = createOpenAI({
|
|
201
|
+
* apiKey: 'llm.do', // Placeholder - llm.do handles auth
|
|
202
|
+
* baseURL: 'https://api.openai.com/v1',
|
|
203
|
+
* fetch: createLLMFetch()
|
|
204
|
+
* })
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
export declare function createLLMFetch(config?: LLMConfig): typeof fetch;
|
|
208
|
+
export {};
|
|
209
|
+
//# sourceMappingURL=llm.do.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.do.d.ts","sourceRoot":"","sources":["../src/llm.do.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,kBAAkB,CA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