@blockrun/llm 0.3.0 → 1.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/README.md +75 -13
- package/dist/index.cjs +90 -0
- package/dist/index.d.cts +69 -1
- package/dist/index.d.ts +69 -1
- package/dist/index.js +90 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -59,12 +59,10 @@ For Solana, set `BLOCKRUN_SOLANA_KEY` environment variable with your base58-enco
|
|
|
59
59
|
| Model | Input Price | Output Price |
|
|
60
60
|
|-------|-------------|--------------|
|
|
61
61
|
| `openai/gpt-5.2` | $1.75/M | $14.00/M |
|
|
62
|
-
| `openai/gpt-5.1` | $1.25/M | $10.00/M |
|
|
63
|
-
| `openai/gpt-5` | $1.25/M | $10.00/M |
|
|
64
62
|
| `openai/gpt-5-mini` | $0.25/M | $2.00/M |
|
|
65
63
|
| `openai/gpt-5-nano` | $0.05/M | $0.40/M |
|
|
66
64
|
| `openai/gpt-5.2-pro` | $21.00/M | $168.00/M |
|
|
67
|
-
| `openai/gpt-5-
|
|
65
|
+
| `openai/gpt-5.2-codex` | $1.75/M | $14.00/M |
|
|
68
66
|
|
|
69
67
|
### OpenAI GPT-4 Family
|
|
70
68
|
| Model | Input Price | Output Price |
|
|
@@ -87,7 +85,10 @@ For Solana, set `BLOCKRUN_SOLANA_KEY` environment variable with your base58-enco
|
|
|
87
85
|
### Anthropic Claude
|
|
88
86
|
| Model | Input Price | Output Price |
|
|
89
87
|
|-------|-------------|--------------|
|
|
88
|
+
| `anthropic/claude-opus-4.6` | $5.00/M | $25.00/M |
|
|
89
|
+
| `anthropic/claude-opus-4.5` | $5.00/M | $25.00/M |
|
|
90
90
|
| `anthropic/claude-opus-4` | $15.00/M | $75.00/M |
|
|
91
|
+
| `anthropic/claude-sonnet-4.6` | $3.00/M | $15.00/M |
|
|
91
92
|
| `anthropic/claude-sonnet-4` | $3.00/M | $15.00/M |
|
|
92
93
|
| `anthropic/claude-haiku-4.5` | $1.00/M | $5.00/M |
|
|
93
94
|
|
|
@@ -96,7 +97,7 @@ For Solana, set `BLOCKRUN_SOLANA_KEY` environment variable with your base58-enco
|
|
|
96
97
|
|-------|-------------|--------------|
|
|
97
98
|
| `google/gemini-3-pro-preview` | $2.00/M | $12.00/M |
|
|
98
99
|
| `google/gemini-2.5-pro` | $1.25/M | $10.00/M |
|
|
99
|
-
| `google/gemini-2.5-flash` | $0.
|
|
100
|
+
| `google/gemini-2.5-flash` | $0.30/M | $2.50/M |
|
|
100
101
|
|
|
101
102
|
### DeepSeek
|
|
102
103
|
| Model | Input Price | Output Price |
|
|
@@ -105,17 +106,29 @@ For Solana, set `BLOCKRUN_SOLANA_KEY` environment variable with your base58-enco
|
|
|
105
106
|
| `deepseek/deepseek-reasoner` | $0.28/M | $0.42/M |
|
|
106
107
|
|
|
107
108
|
### xAI Grok
|
|
108
|
-
| Model | Input Price | Output Price |
|
|
109
|
-
|
|
110
|
-
| `xai/grok-3` | $3.00/M | $15.00/M |
|
|
111
|
-
| `xai/grok-3-
|
|
112
|
-
| `xai/grok-
|
|
109
|
+
| Model | Input Price | Output Price | Context | Notes |
|
|
110
|
+
|-------|-------------|--------------|---------|-------|
|
|
111
|
+
| `xai/grok-3` | $3.00/M | $15.00/M | 131K | Flagship |
|
|
112
|
+
| `xai/grok-3-mini` | $0.30/M | $0.50/M | 131K | Fast & affordable |
|
|
113
|
+
| `xai/grok-4-1-fast-reasoning` | $0.20/M | $0.50/M | **2M** | Latest, chain-of-thought |
|
|
114
|
+
| `xai/grok-4-1-fast-non-reasoning` | $0.20/M | $0.50/M | **2M** | Latest, direct response |
|
|
115
|
+
| `xai/grok-4-fast-reasoning` | $0.20/M | $0.50/M | **2M** | Step-by-step reasoning |
|
|
116
|
+
| `xai/grok-4-fast-non-reasoning` | $0.20/M | $0.50/M | **2M** | Quick responses |
|
|
117
|
+
| `xai/grok-code-fast-1` | $0.20/M | $1.50/M | 256K | Code generation |
|
|
118
|
+
| `xai/grok-4-0709` | $0.20/M | $1.50/M | 256K | Premium quality |
|
|
119
|
+
| `xai/grok-2-vision` | $2.00/M | $10.00/M | 32K | Vision capabilities |
|
|
113
120
|
|
|
114
121
|
### Moonshot Kimi
|
|
115
122
|
| Model | Input Price | Output Price |
|
|
116
123
|
|-------|-------------|--------------|
|
|
117
124
|
| `moonshot/kimi-k2.5` | $0.60/M | $3.00/M |
|
|
118
125
|
|
|
126
|
+
### NVIDIA (Free & Hosted)
|
|
127
|
+
| Model | Input Price | Output Price | Notes |
|
|
128
|
+
|-------|-------------|--------------|-------|
|
|
129
|
+
| `nvidia/gpt-oss-120b` | **FREE** | **FREE** | OpenAI open-weight 120B (Apache 2.0) |
|
|
130
|
+
| `nvidia/kimi-k2.5` | $0.60/M | $3.00/M | Moonshot 1T MoE with vision |
|
|
131
|
+
|
|
119
132
|
### E2E Verified Models
|
|
120
133
|
|
|
121
134
|
All models below have been tested end-to-end via the TypeScript SDK (Feb 2026):
|
|
@@ -123,10 +136,12 @@ All models below have been tested end-to-end via the TypeScript SDK (Feb 2026):
|
|
|
123
136
|
| Provider | Model | Status |
|
|
124
137
|
|----------|-------|--------|
|
|
125
138
|
| OpenAI | `openai/gpt-4o-mini` | Passed |
|
|
139
|
+
| OpenAI | `openai/gpt-5.2-codex` | Passed |
|
|
140
|
+
| Anthropic | `anthropic/claude-opus-4.6` | Passed |
|
|
126
141
|
| Anthropic | `anthropic/claude-sonnet-4` | Passed |
|
|
127
142
|
| Google | `google/gemini-2.5-flash` | Passed |
|
|
128
143
|
| DeepSeek | `deepseek/deepseek-chat` | Passed |
|
|
129
|
-
| xAI | `xai/grok-3
|
|
144
|
+
| xAI | `xai/grok-3` | Passed |
|
|
130
145
|
| Moonshot | `moonshot/kimi-k2.5` | Passed |
|
|
131
146
|
|
|
132
147
|
### Image Generation
|
|
@@ -206,6 +221,47 @@ const response2 = await client.chat('anthropic/claude-sonnet-4', 'Write a haiku'
|
|
|
206
221
|
});
|
|
207
222
|
```
|
|
208
223
|
|
|
224
|
+
### Smart Routing (ClawRouter)
|
|
225
|
+
|
|
226
|
+
Save up to 78% on inference costs with intelligent model routing. ClawRouter uses a 14-dimension rule-based scoring algorithm to select the cheapest model that can handle your request (<1ms, 100% local).
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
import { LLMClient } from '@blockrun/llm';
|
|
230
|
+
|
|
231
|
+
const client = new LLMClient();
|
|
232
|
+
|
|
233
|
+
// Auto-route to cheapest capable model
|
|
234
|
+
const result = await client.smartChat('What is 2+2?');
|
|
235
|
+
console.log(result.response); // '4'
|
|
236
|
+
console.log(result.model); // 'google/gemini-2.5-flash'
|
|
237
|
+
console.log(result.routing.tier); // 'SIMPLE'
|
|
238
|
+
console.log(`Saved ${(result.routing.savings * 100).toFixed(0)}%`); // 'Saved 78%'
|
|
239
|
+
|
|
240
|
+
// Routing profiles
|
|
241
|
+
const free = await client.smartChat('Hello!', { routingProfile: 'free' }); // Zero cost
|
|
242
|
+
const eco = await client.smartChat('Explain AI', { routingProfile: 'eco' }); // Budget optimized
|
|
243
|
+
const auto = await client.smartChat('Code review', { routingProfile: 'auto' }); // Balanced (default)
|
|
244
|
+
const premium = await client.smartChat('Write a legal brief', { routingProfile: 'premium' }); // Best quality
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
**Routing Profiles:**
|
|
248
|
+
|
|
249
|
+
| Profile | Description | Best For |
|
|
250
|
+
|---------|-------------|----------|
|
|
251
|
+
| `free` | NVIDIA free models only | Testing, simple queries |
|
|
252
|
+
| `eco` | Budget-optimized | Cost-sensitive workloads |
|
|
253
|
+
| `auto` | Intelligent routing (default) | General use |
|
|
254
|
+
| `premium` | Best quality models | Critical tasks |
|
|
255
|
+
|
|
256
|
+
**Tiers:**
|
|
257
|
+
|
|
258
|
+
| Tier | Example Tasks | Typical Models |
|
|
259
|
+
|------|---------------|----------------|
|
|
260
|
+
| SIMPLE | Greetings, math, lookups | Gemini Flash, GPT-4o-mini |
|
|
261
|
+
| MEDIUM | Explanations, summaries | GPT-4o, Claude Sonnet |
|
|
262
|
+
| COMPLEX | Analysis, code generation | GPT-5.2, Claude Opus |
|
|
263
|
+
| REASONING | Multi-step logic, planning | o3, DeepSeek Reasoner |
|
|
264
|
+
|
|
209
265
|
### Full Chat Completion
|
|
210
266
|
|
|
211
267
|
```typescript
|
|
@@ -389,6 +445,12 @@ import {
|
|
|
389
445
|
type ChatResponse,
|
|
390
446
|
type ChatOptions,
|
|
391
447
|
type Model,
|
|
448
|
+
// Smart routing types
|
|
449
|
+
type SmartChatOptions,
|
|
450
|
+
type SmartChatResponse,
|
|
451
|
+
type RoutingDecision,
|
|
452
|
+
type RoutingProfile,
|
|
453
|
+
type RoutingTier,
|
|
392
454
|
APIError,
|
|
393
455
|
PaymentError,
|
|
394
456
|
} from '@blockrun/llm';
|
|
@@ -397,9 +459,9 @@ import {
|
|
|
397
459
|
## Links
|
|
398
460
|
|
|
399
461
|
- [Website](https://blockrun.ai)
|
|
400
|
-
- [Documentation](https://
|
|
401
|
-
- [GitHub](https://github.com/
|
|
402
|
-
- [
|
|
462
|
+
- [Documentation](https://github.com/BlockRunAI/awesome-blockrun/tree/main/docs)
|
|
463
|
+
- [GitHub](https://github.com/blockrunai/blockrun-llm-ts)
|
|
464
|
+
- [Telegram](https://t.me/+mroQv4-4hGgzOGUx)
|
|
403
465
|
|
|
404
466
|
## License
|
|
405
467
|
|
package/dist/index.cjs
CHANGED
|
@@ -83,6 +83,9 @@ var APIError = class extends BlockrunError {
|
|
|
83
83
|
}
|
|
84
84
|
};
|
|
85
85
|
|
|
86
|
+
// src/client.ts
|
|
87
|
+
var import_clawrouter = require("@blockrun/clawrouter");
|
|
88
|
+
|
|
86
89
|
// src/x402.ts
|
|
87
90
|
var import_accounts = require("viem/accounts");
|
|
88
91
|
var BASE_CHAIN_ID = 8453;
|
|
@@ -293,6 +296,8 @@ var LLMClient = class {
|
|
|
293
296
|
timeout;
|
|
294
297
|
sessionTotalUsd = 0;
|
|
295
298
|
sessionCalls = 0;
|
|
299
|
+
modelPricingCache = null;
|
|
300
|
+
modelPricingPromise = null;
|
|
296
301
|
/**
|
|
297
302
|
* Initialize the BlockRun LLM client.
|
|
298
303
|
*
|
|
@@ -341,6 +346,91 @@ var LLMClient = class {
|
|
|
341
346
|
});
|
|
342
347
|
return result.choices[0].message.content || "";
|
|
343
348
|
}
|
|
349
|
+
/**
|
|
350
|
+
* Smart chat with automatic model routing.
|
|
351
|
+
*
|
|
352
|
+
* Uses ClawRouter's 14-dimension rule-based scoring algorithm (<1ms, 100% local)
|
|
353
|
+
* to select the cheapest model that can handle your request.
|
|
354
|
+
*
|
|
355
|
+
* @param prompt - User message
|
|
356
|
+
* @param options - Optional chat and routing parameters
|
|
357
|
+
* @returns SmartChatResponse with response text, selected model, and routing metadata
|
|
358
|
+
*
|
|
359
|
+
* @example Simple usage (auto profile)
|
|
360
|
+
* ```ts
|
|
361
|
+
* const result = await client.smartChat('What is 2+2?');
|
|
362
|
+
* console.log(result.response); // '4'
|
|
363
|
+
* console.log(result.model); // 'google/gemini-2.5-flash'
|
|
364
|
+
* console.log(result.routing.savings); // 0.78 (78% savings)
|
|
365
|
+
* ```
|
|
366
|
+
*
|
|
367
|
+
* @example With routing profile
|
|
368
|
+
* ```ts
|
|
369
|
+
* // Free tier only (zero cost)
|
|
370
|
+
* const result = await client.smartChat('Hello!', { routingProfile: 'free' });
|
|
371
|
+
*
|
|
372
|
+
* // Eco mode (budget optimized)
|
|
373
|
+
* const result = await client.smartChat('Explain quantum computing', { routingProfile: 'eco' });
|
|
374
|
+
*
|
|
375
|
+
* // Premium mode (best quality)
|
|
376
|
+
* const result = await client.smartChat('Write a business plan', { routingProfile: 'premium' });
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
async smartChat(prompt, options) {
|
|
380
|
+
const modelPricing = await this.getModelPricing();
|
|
381
|
+
const maxOutputTokens = options?.maxOutputTokens || options?.maxTokens || 1024;
|
|
382
|
+
const decision = (0, import_clawrouter.route)(prompt, options?.system, maxOutputTokens, {
|
|
383
|
+
config: import_clawrouter.DEFAULT_ROUTING_CONFIG,
|
|
384
|
+
modelPricing,
|
|
385
|
+
routingProfile: options?.routingProfile
|
|
386
|
+
});
|
|
387
|
+
const response = await this.chat(decision.model, prompt, {
|
|
388
|
+
system: options?.system,
|
|
389
|
+
maxTokens: options?.maxTokens,
|
|
390
|
+
temperature: options?.temperature,
|
|
391
|
+
topP: options?.topP,
|
|
392
|
+
search: options?.search,
|
|
393
|
+
searchParameters: options?.searchParameters
|
|
394
|
+
});
|
|
395
|
+
return {
|
|
396
|
+
response,
|
|
397
|
+
model: decision.model,
|
|
398
|
+
routing: decision
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Get model pricing map (cached).
|
|
403
|
+
* Fetches from API on first call, then returns cached result.
|
|
404
|
+
*/
|
|
405
|
+
async getModelPricing() {
|
|
406
|
+
if (this.modelPricingCache) {
|
|
407
|
+
return this.modelPricingCache;
|
|
408
|
+
}
|
|
409
|
+
if (this.modelPricingPromise) {
|
|
410
|
+
return this.modelPricingPromise;
|
|
411
|
+
}
|
|
412
|
+
this.modelPricingPromise = this.fetchModelPricing();
|
|
413
|
+
try {
|
|
414
|
+
this.modelPricingCache = await this.modelPricingPromise;
|
|
415
|
+
return this.modelPricingCache;
|
|
416
|
+
} finally {
|
|
417
|
+
this.modelPricingPromise = null;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Fetch model pricing from API.
|
|
422
|
+
*/
|
|
423
|
+
async fetchModelPricing() {
|
|
424
|
+
const models = await this.listModels();
|
|
425
|
+
const pricing = /* @__PURE__ */ new Map();
|
|
426
|
+
for (const model of models) {
|
|
427
|
+
pricing.set(model.id, {
|
|
428
|
+
inputPrice: model.inputPrice,
|
|
429
|
+
outputPrice: model.outputPrice
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
return pricing;
|
|
433
|
+
}
|
|
344
434
|
/**
|
|
345
435
|
* Full chat completion interface (OpenAI-compatible).
|
|
346
436
|
*
|
package/dist/index.d.cts
CHANGED
|
@@ -179,6 +179,32 @@ interface ChatCompletionOptions {
|
|
|
179
179
|
/** Tool selection strategy */
|
|
180
180
|
toolChoice?: ToolChoice;
|
|
181
181
|
}
|
|
182
|
+
type RoutingProfile = "free" | "eco" | "auto" | "premium";
|
|
183
|
+
type RoutingTier = "SIMPLE" | "MEDIUM" | "COMPLEX" | "REASONING";
|
|
184
|
+
interface RoutingDecision {
|
|
185
|
+
model: string;
|
|
186
|
+
tier: RoutingTier;
|
|
187
|
+
confidence: number;
|
|
188
|
+
method: "rules" | "llm";
|
|
189
|
+
reasoning: string;
|
|
190
|
+
costEstimate: number;
|
|
191
|
+
baselineCost: number;
|
|
192
|
+
savings: number;
|
|
193
|
+
}
|
|
194
|
+
interface SmartChatOptions extends ChatOptions {
|
|
195
|
+
/** Routing profile: free (zero cost), eco (budget), auto (balanced), premium (best quality) */
|
|
196
|
+
routingProfile?: RoutingProfile;
|
|
197
|
+
/** Maximum output tokens (used for cost estimation) */
|
|
198
|
+
maxOutputTokens?: number;
|
|
199
|
+
}
|
|
200
|
+
interface SmartChatResponse {
|
|
201
|
+
/** The AI response text */
|
|
202
|
+
response: string;
|
|
203
|
+
/** Which model was selected by smart routing */
|
|
204
|
+
model: string;
|
|
205
|
+
/** Routing decision metadata */
|
|
206
|
+
routing: RoutingDecision;
|
|
207
|
+
}
|
|
182
208
|
declare class BlockrunError extends Error {
|
|
183
209
|
constructor(message: string);
|
|
184
210
|
}
|
|
@@ -240,6 +266,8 @@ declare class LLMClient {
|
|
|
240
266
|
private timeout;
|
|
241
267
|
private sessionTotalUsd;
|
|
242
268
|
private sessionCalls;
|
|
269
|
+
private modelPricingCache;
|
|
270
|
+
private modelPricingPromise;
|
|
243
271
|
/**
|
|
244
272
|
* Initialize the BlockRun LLM client.
|
|
245
273
|
*
|
|
@@ -259,6 +287,46 @@ declare class LLMClient {
|
|
|
259
287
|
* console.log(response); // 'The capital of France is Paris.'
|
|
260
288
|
*/
|
|
261
289
|
chat(model: string, prompt: string, options?: ChatOptions): Promise<string>;
|
|
290
|
+
/**
|
|
291
|
+
* Smart chat with automatic model routing.
|
|
292
|
+
*
|
|
293
|
+
* Uses ClawRouter's 14-dimension rule-based scoring algorithm (<1ms, 100% local)
|
|
294
|
+
* to select the cheapest model that can handle your request.
|
|
295
|
+
*
|
|
296
|
+
* @param prompt - User message
|
|
297
|
+
* @param options - Optional chat and routing parameters
|
|
298
|
+
* @returns SmartChatResponse with response text, selected model, and routing metadata
|
|
299
|
+
*
|
|
300
|
+
* @example Simple usage (auto profile)
|
|
301
|
+
* ```ts
|
|
302
|
+
* const result = await client.smartChat('What is 2+2?');
|
|
303
|
+
* console.log(result.response); // '4'
|
|
304
|
+
* console.log(result.model); // 'google/gemini-2.5-flash'
|
|
305
|
+
* console.log(result.routing.savings); // 0.78 (78% savings)
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* @example With routing profile
|
|
309
|
+
* ```ts
|
|
310
|
+
* // Free tier only (zero cost)
|
|
311
|
+
* const result = await client.smartChat('Hello!', { routingProfile: 'free' });
|
|
312
|
+
*
|
|
313
|
+
* // Eco mode (budget optimized)
|
|
314
|
+
* const result = await client.smartChat('Explain quantum computing', { routingProfile: 'eco' });
|
|
315
|
+
*
|
|
316
|
+
* // Premium mode (best quality)
|
|
317
|
+
* const result = await client.smartChat('Write a business plan', { routingProfile: 'premium' });
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
smartChat(prompt: string, options?: SmartChatOptions): Promise<SmartChatResponse>;
|
|
321
|
+
/**
|
|
322
|
+
* Get model pricing map (cached).
|
|
323
|
+
* Fetches from API on first call, then returns cached result.
|
|
324
|
+
*/
|
|
325
|
+
private getModelPricing;
|
|
326
|
+
/**
|
|
327
|
+
* Fetch model pricing from API.
|
|
328
|
+
*/
|
|
329
|
+
private fetchModelPricing;
|
|
262
330
|
/**
|
|
263
331
|
* Full chat completion interface (OpenAI-compatible).
|
|
264
332
|
*
|
|
@@ -673,4 +741,4 @@ declare class OpenAI {
|
|
|
673
741
|
getWalletAddress(): string;
|
|
674
742
|
}
|
|
675
743
|
|
|
676
|
-
export { APIError, BASE_CHAIN_ID, BlockrunError, type ChatChoice, type ChatCompletionOptions, type ChatMessage, type ChatOptions, type ChatResponse, type ChatUsage, type FunctionCall, type FunctionDefinition, ImageClient, type ImageClientOptions, type ImageData, type ImageGenerateOptions, type ImageModel, type ImageResponse, LLMClient, type LLMClientOptions, type Model, type NewsSearchSource, OpenAI, type OpenAIChatCompletionChoice, type OpenAIChatCompletionChunk, type OpenAIChatCompletionParams, type OpenAIChatCompletionResponse, type OpenAIClientOptions, PaymentError, type PaymentLinks, type RssSearchSource, type SearchParameters, type SearchSource, type Spending, type Tool, type ToolCall, type ToolChoice, USDC_BASE, USDC_BASE_CONTRACT, WALLET_DIR_PATH, WALLET_FILE_PATH, type WalletInfo, type WebSearchSource, type XSearchSource, createWallet, LLMClient as default, formatFundingMessageCompact, formatNeedsFundingMessage, formatWalletCreatedMessage, getEip681Uri, getOrCreateWallet, getPaymentLinks, getWalletAddress, loadWallet, saveWallet, testnetClient };
|
|
744
|
+
export { APIError, BASE_CHAIN_ID, BlockrunError, type ChatChoice, type ChatCompletionOptions, type ChatMessage, type ChatOptions, type ChatResponse, type ChatUsage, type FunctionCall, type FunctionDefinition, ImageClient, type ImageClientOptions, type ImageData, type ImageGenerateOptions, type ImageModel, type ImageResponse, LLMClient, type LLMClientOptions, type Model, type NewsSearchSource, OpenAI, type OpenAIChatCompletionChoice, type OpenAIChatCompletionChunk, type OpenAIChatCompletionParams, type OpenAIChatCompletionResponse, type OpenAIClientOptions, PaymentError, type PaymentLinks, type RoutingDecision, type RoutingProfile, type RoutingTier, type RssSearchSource, type SearchParameters, type SearchSource, type SmartChatOptions, type SmartChatResponse, type Spending, type Tool, type ToolCall, type ToolChoice, USDC_BASE, USDC_BASE_CONTRACT, WALLET_DIR_PATH, WALLET_FILE_PATH, type WalletInfo, type WebSearchSource, type XSearchSource, createWallet, LLMClient as default, formatFundingMessageCompact, formatNeedsFundingMessage, formatWalletCreatedMessage, getEip681Uri, getOrCreateWallet, getPaymentLinks, getWalletAddress, loadWallet, saveWallet, testnetClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -179,6 +179,32 @@ interface ChatCompletionOptions {
|
|
|
179
179
|
/** Tool selection strategy */
|
|
180
180
|
toolChoice?: ToolChoice;
|
|
181
181
|
}
|
|
182
|
+
type RoutingProfile = "free" | "eco" | "auto" | "premium";
|
|
183
|
+
type RoutingTier = "SIMPLE" | "MEDIUM" | "COMPLEX" | "REASONING";
|
|
184
|
+
interface RoutingDecision {
|
|
185
|
+
model: string;
|
|
186
|
+
tier: RoutingTier;
|
|
187
|
+
confidence: number;
|
|
188
|
+
method: "rules" | "llm";
|
|
189
|
+
reasoning: string;
|
|
190
|
+
costEstimate: number;
|
|
191
|
+
baselineCost: number;
|
|
192
|
+
savings: number;
|
|
193
|
+
}
|
|
194
|
+
interface SmartChatOptions extends ChatOptions {
|
|
195
|
+
/** Routing profile: free (zero cost), eco (budget), auto (balanced), premium (best quality) */
|
|
196
|
+
routingProfile?: RoutingProfile;
|
|
197
|
+
/** Maximum output tokens (used for cost estimation) */
|
|
198
|
+
maxOutputTokens?: number;
|
|
199
|
+
}
|
|
200
|
+
interface SmartChatResponse {
|
|
201
|
+
/** The AI response text */
|
|
202
|
+
response: string;
|
|
203
|
+
/** Which model was selected by smart routing */
|
|
204
|
+
model: string;
|
|
205
|
+
/** Routing decision metadata */
|
|
206
|
+
routing: RoutingDecision;
|
|
207
|
+
}
|
|
182
208
|
declare class BlockrunError extends Error {
|
|
183
209
|
constructor(message: string);
|
|
184
210
|
}
|
|
@@ -240,6 +266,8 @@ declare class LLMClient {
|
|
|
240
266
|
private timeout;
|
|
241
267
|
private sessionTotalUsd;
|
|
242
268
|
private sessionCalls;
|
|
269
|
+
private modelPricingCache;
|
|
270
|
+
private modelPricingPromise;
|
|
243
271
|
/**
|
|
244
272
|
* Initialize the BlockRun LLM client.
|
|
245
273
|
*
|
|
@@ -259,6 +287,46 @@ declare class LLMClient {
|
|
|
259
287
|
* console.log(response); // 'The capital of France is Paris.'
|
|
260
288
|
*/
|
|
261
289
|
chat(model: string, prompt: string, options?: ChatOptions): Promise<string>;
|
|
290
|
+
/**
|
|
291
|
+
* Smart chat with automatic model routing.
|
|
292
|
+
*
|
|
293
|
+
* Uses ClawRouter's 14-dimension rule-based scoring algorithm (<1ms, 100% local)
|
|
294
|
+
* to select the cheapest model that can handle your request.
|
|
295
|
+
*
|
|
296
|
+
* @param prompt - User message
|
|
297
|
+
* @param options - Optional chat and routing parameters
|
|
298
|
+
* @returns SmartChatResponse with response text, selected model, and routing metadata
|
|
299
|
+
*
|
|
300
|
+
* @example Simple usage (auto profile)
|
|
301
|
+
* ```ts
|
|
302
|
+
* const result = await client.smartChat('What is 2+2?');
|
|
303
|
+
* console.log(result.response); // '4'
|
|
304
|
+
* console.log(result.model); // 'google/gemini-2.5-flash'
|
|
305
|
+
* console.log(result.routing.savings); // 0.78 (78% savings)
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* @example With routing profile
|
|
309
|
+
* ```ts
|
|
310
|
+
* // Free tier only (zero cost)
|
|
311
|
+
* const result = await client.smartChat('Hello!', { routingProfile: 'free' });
|
|
312
|
+
*
|
|
313
|
+
* // Eco mode (budget optimized)
|
|
314
|
+
* const result = await client.smartChat('Explain quantum computing', { routingProfile: 'eco' });
|
|
315
|
+
*
|
|
316
|
+
* // Premium mode (best quality)
|
|
317
|
+
* const result = await client.smartChat('Write a business plan', { routingProfile: 'premium' });
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
smartChat(prompt: string, options?: SmartChatOptions): Promise<SmartChatResponse>;
|
|
321
|
+
/**
|
|
322
|
+
* Get model pricing map (cached).
|
|
323
|
+
* Fetches from API on first call, then returns cached result.
|
|
324
|
+
*/
|
|
325
|
+
private getModelPricing;
|
|
326
|
+
/**
|
|
327
|
+
* Fetch model pricing from API.
|
|
328
|
+
*/
|
|
329
|
+
private fetchModelPricing;
|
|
262
330
|
/**
|
|
263
331
|
* Full chat completion interface (OpenAI-compatible).
|
|
264
332
|
*
|
|
@@ -673,4 +741,4 @@ declare class OpenAI {
|
|
|
673
741
|
getWalletAddress(): string;
|
|
674
742
|
}
|
|
675
743
|
|
|
676
|
-
export { APIError, BASE_CHAIN_ID, BlockrunError, type ChatChoice, type ChatCompletionOptions, type ChatMessage, type ChatOptions, type ChatResponse, type ChatUsage, type FunctionCall, type FunctionDefinition, ImageClient, type ImageClientOptions, type ImageData, type ImageGenerateOptions, type ImageModel, type ImageResponse, LLMClient, type LLMClientOptions, type Model, type NewsSearchSource, OpenAI, type OpenAIChatCompletionChoice, type OpenAIChatCompletionChunk, type OpenAIChatCompletionParams, type OpenAIChatCompletionResponse, type OpenAIClientOptions, PaymentError, type PaymentLinks, type RssSearchSource, type SearchParameters, type SearchSource, type Spending, type Tool, type ToolCall, type ToolChoice, USDC_BASE, USDC_BASE_CONTRACT, WALLET_DIR_PATH, WALLET_FILE_PATH, type WalletInfo, type WebSearchSource, type XSearchSource, createWallet, LLMClient as default, formatFundingMessageCompact, formatNeedsFundingMessage, formatWalletCreatedMessage, getEip681Uri, getOrCreateWallet, getPaymentLinks, getWalletAddress, loadWallet, saveWallet, testnetClient };
|
|
744
|
+
export { APIError, BASE_CHAIN_ID, BlockrunError, type ChatChoice, type ChatCompletionOptions, type ChatMessage, type ChatOptions, type ChatResponse, type ChatUsage, type FunctionCall, type FunctionDefinition, ImageClient, type ImageClientOptions, type ImageData, type ImageGenerateOptions, type ImageModel, type ImageResponse, LLMClient, type LLMClientOptions, type Model, type NewsSearchSource, OpenAI, type OpenAIChatCompletionChoice, type OpenAIChatCompletionChunk, type OpenAIChatCompletionParams, type OpenAIChatCompletionResponse, type OpenAIClientOptions, PaymentError, type PaymentLinks, type RoutingDecision, type RoutingProfile, type RoutingTier, type RssSearchSource, type SearchParameters, type SearchSource, type SmartChatOptions, type SmartChatResponse, type Spending, type Tool, type ToolCall, type ToolChoice, USDC_BASE, USDC_BASE_CONTRACT, WALLET_DIR_PATH, WALLET_FILE_PATH, type WalletInfo, type WebSearchSource, type XSearchSource, createWallet, LLMClient as default, formatFundingMessageCompact, formatNeedsFundingMessage, formatWalletCreatedMessage, getEip681Uri, getOrCreateWallet, getPaymentLinks, getWalletAddress, loadWallet, saveWallet, testnetClient };
|
package/dist/index.js
CHANGED
|
@@ -27,6 +27,9 @@ var APIError = class extends BlockrunError {
|
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
+
// src/client.ts
|
|
31
|
+
import { route, DEFAULT_ROUTING_CONFIG } from "@blockrun/clawrouter";
|
|
32
|
+
|
|
30
33
|
// src/x402.ts
|
|
31
34
|
import { signTypedData } from "viem/accounts";
|
|
32
35
|
var BASE_CHAIN_ID = 8453;
|
|
@@ -237,6 +240,8 @@ var LLMClient = class {
|
|
|
237
240
|
timeout;
|
|
238
241
|
sessionTotalUsd = 0;
|
|
239
242
|
sessionCalls = 0;
|
|
243
|
+
modelPricingCache = null;
|
|
244
|
+
modelPricingPromise = null;
|
|
240
245
|
/**
|
|
241
246
|
* Initialize the BlockRun LLM client.
|
|
242
247
|
*
|
|
@@ -285,6 +290,91 @@ var LLMClient = class {
|
|
|
285
290
|
});
|
|
286
291
|
return result.choices[0].message.content || "";
|
|
287
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* Smart chat with automatic model routing.
|
|
295
|
+
*
|
|
296
|
+
* Uses ClawRouter's 14-dimension rule-based scoring algorithm (<1ms, 100% local)
|
|
297
|
+
* to select the cheapest model that can handle your request.
|
|
298
|
+
*
|
|
299
|
+
* @param prompt - User message
|
|
300
|
+
* @param options - Optional chat and routing parameters
|
|
301
|
+
* @returns SmartChatResponse with response text, selected model, and routing metadata
|
|
302
|
+
*
|
|
303
|
+
* @example Simple usage (auto profile)
|
|
304
|
+
* ```ts
|
|
305
|
+
* const result = await client.smartChat('What is 2+2?');
|
|
306
|
+
* console.log(result.response); // '4'
|
|
307
|
+
* console.log(result.model); // 'google/gemini-2.5-flash'
|
|
308
|
+
* console.log(result.routing.savings); // 0.78 (78% savings)
|
|
309
|
+
* ```
|
|
310
|
+
*
|
|
311
|
+
* @example With routing profile
|
|
312
|
+
* ```ts
|
|
313
|
+
* // Free tier only (zero cost)
|
|
314
|
+
* const result = await client.smartChat('Hello!', { routingProfile: 'free' });
|
|
315
|
+
*
|
|
316
|
+
* // Eco mode (budget optimized)
|
|
317
|
+
* const result = await client.smartChat('Explain quantum computing', { routingProfile: 'eco' });
|
|
318
|
+
*
|
|
319
|
+
* // Premium mode (best quality)
|
|
320
|
+
* const result = await client.smartChat('Write a business plan', { routingProfile: 'premium' });
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
async smartChat(prompt, options) {
|
|
324
|
+
const modelPricing = await this.getModelPricing();
|
|
325
|
+
const maxOutputTokens = options?.maxOutputTokens || options?.maxTokens || 1024;
|
|
326
|
+
const decision = route(prompt, options?.system, maxOutputTokens, {
|
|
327
|
+
config: DEFAULT_ROUTING_CONFIG,
|
|
328
|
+
modelPricing,
|
|
329
|
+
routingProfile: options?.routingProfile
|
|
330
|
+
});
|
|
331
|
+
const response = await this.chat(decision.model, prompt, {
|
|
332
|
+
system: options?.system,
|
|
333
|
+
maxTokens: options?.maxTokens,
|
|
334
|
+
temperature: options?.temperature,
|
|
335
|
+
topP: options?.topP,
|
|
336
|
+
search: options?.search,
|
|
337
|
+
searchParameters: options?.searchParameters
|
|
338
|
+
});
|
|
339
|
+
return {
|
|
340
|
+
response,
|
|
341
|
+
model: decision.model,
|
|
342
|
+
routing: decision
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Get model pricing map (cached).
|
|
347
|
+
* Fetches from API on first call, then returns cached result.
|
|
348
|
+
*/
|
|
349
|
+
async getModelPricing() {
|
|
350
|
+
if (this.modelPricingCache) {
|
|
351
|
+
return this.modelPricingCache;
|
|
352
|
+
}
|
|
353
|
+
if (this.modelPricingPromise) {
|
|
354
|
+
return this.modelPricingPromise;
|
|
355
|
+
}
|
|
356
|
+
this.modelPricingPromise = this.fetchModelPricing();
|
|
357
|
+
try {
|
|
358
|
+
this.modelPricingCache = await this.modelPricingPromise;
|
|
359
|
+
return this.modelPricingCache;
|
|
360
|
+
} finally {
|
|
361
|
+
this.modelPricingPromise = null;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Fetch model pricing from API.
|
|
366
|
+
*/
|
|
367
|
+
async fetchModelPricing() {
|
|
368
|
+
const models = await this.listModels();
|
|
369
|
+
const pricing = /* @__PURE__ */ new Map();
|
|
370
|
+
for (const model of models) {
|
|
371
|
+
pricing.set(model.id, {
|
|
372
|
+
inputPrice: model.inputPrice,
|
|
373
|
+
outputPrice: model.outputPrice
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
return pricing;
|
|
377
|
+
}
|
|
288
378
|
/**
|
|
289
379
|
* Full chat completion interface (OpenAI-compatible).
|
|
290
380
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blockrun/llm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "BlockRun LLM Gateway SDK - Pay-per-request AI via x402 on Base",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"url": "https://github.com/BlockRunAI/blockrun-llm-ts/issues"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
+
"@blockrun/clawrouter": "^0.9.3",
|
|
51
52
|
"viem": "^2.21.0"
|
|
52
53
|
},
|
|
53
54
|
"optionalDependencies": {
|