@blockrun/llm 0.1.1 → 0.2.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.
@@ -0,0 +1,587 @@
1
+ /**
2
+ * Type definitions for BlockRun LLM SDK
3
+ */
4
+ interface ChatMessage {
5
+ role: "system" | "user" | "assistant";
6
+ content: string;
7
+ }
8
+ interface ChatChoice {
9
+ index: number;
10
+ message: ChatMessage;
11
+ finish_reason?: string;
12
+ }
13
+ interface ChatUsage {
14
+ prompt_tokens: number;
15
+ completion_tokens: number;
16
+ total_tokens: number;
17
+ num_sources_used?: number;
18
+ }
19
+ interface ChatResponse {
20
+ id: string;
21
+ object: string;
22
+ created: number;
23
+ model: string;
24
+ choices: ChatChoice[];
25
+ usage?: ChatUsage;
26
+ citations?: string[];
27
+ }
28
+ interface Model {
29
+ id: string;
30
+ name: string;
31
+ provider: string;
32
+ description: string;
33
+ inputPrice: number;
34
+ outputPrice: number;
35
+ contextWindow: number;
36
+ maxOutput: number;
37
+ available: boolean;
38
+ type?: "llm" | "image";
39
+ }
40
+ interface ImageData {
41
+ url: string;
42
+ revised_prompt?: string;
43
+ b64_json?: string;
44
+ }
45
+ interface ImageResponse {
46
+ created: number;
47
+ data: ImageData[];
48
+ }
49
+ interface ImageModel {
50
+ id: string;
51
+ name: string;
52
+ provider: string;
53
+ description: string;
54
+ pricePerImage: number;
55
+ supportedSizes?: string[];
56
+ maxPromptLength?: number;
57
+ available: boolean;
58
+ type?: "llm" | "image";
59
+ }
60
+ interface ImageClientOptions {
61
+ /** EVM wallet private key (hex string starting with 0x) */
62
+ privateKey?: `0x${string}` | string;
63
+ /** API endpoint URL (default: https://blockrun.ai/api) */
64
+ apiUrl?: string;
65
+ /** Request timeout in milliseconds (default: 120000 for images) */
66
+ timeout?: number;
67
+ }
68
+ interface ImageGenerateOptions {
69
+ /** Model ID (default: "google/nano-banana") */
70
+ model?: string;
71
+ /** Image size (default: "1024x1024") */
72
+ size?: string;
73
+ /** Number of images to generate (default: 1) */
74
+ n?: number;
75
+ /** Image quality (for supported models) */
76
+ quality?: "standard" | "hd";
77
+ }
78
+ interface WebSearchSource {
79
+ type: "web";
80
+ country?: string;
81
+ excludedWebsites?: string[];
82
+ allowedWebsites?: string[];
83
+ safeSearch?: boolean;
84
+ }
85
+ interface XSearchSource {
86
+ type: "x";
87
+ includedXHandles?: string[];
88
+ excludedXHandles?: string[];
89
+ postFavoriteCount?: number;
90
+ postViewCount?: number;
91
+ }
92
+ interface NewsSearchSource {
93
+ type: "news";
94
+ country?: string;
95
+ excludedWebsites?: string[];
96
+ allowedWebsites?: string[];
97
+ safeSearch?: boolean;
98
+ }
99
+ interface RssSearchSource {
100
+ type: "rss";
101
+ links: string[];
102
+ }
103
+ type SearchSource = WebSearchSource | XSearchSource | NewsSearchSource | RssSearchSource;
104
+ interface SearchParameters {
105
+ mode?: "off" | "auto" | "on";
106
+ sources?: SearchSource[];
107
+ returnCitations?: boolean;
108
+ fromDate?: string;
109
+ toDate?: string;
110
+ maxSearchResults?: number;
111
+ }
112
+ interface Spending {
113
+ totalUsd: number;
114
+ calls: number;
115
+ }
116
+ interface LLMClientOptions {
117
+ /** EVM wallet private key (hex string starting with 0x). Optional if BASE_CHAIN_WALLET_KEY env var is set. */
118
+ privateKey?: `0x${string}` | string;
119
+ /** API endpoint URL (default: https://blockrun.ai/api) */
120
+ apiUrl?: string;
121
+ /** Request timeout in milliseconds (default: 60000) */
122
+ timeout?: number;
123
+ }
124
+ interface ChatOptions {
125
+ /** System prompt */
126
+ system?: string;
127
+ /** Max tokens to generate */
128
+ maxTokens?: number;
129
+ /** Sampling temperature */
130
+ temperature?: number;
131
+ /** Nucleus sampling parameter */
132
+ topP?: number;
133
+ /** Enable xAI Live Search (shortcut for searchParameters.mode = "on") */
134
+ search?: boolean;
135
+ /** Full xAI Live Search configuration (for Grok models) */
136
+ searchParameters?: SearchParameters;
137
+ }
138
+ interface ChatCompletionOptions {
139
+ /** Max tokens to generate */
140
+ maxTokens?: number;
141
+ /** Sampling temperature */
142
+ temperature?: number;
143
+ /** Nucleus sampling parameter */
144
+ topP?: number;
145
+ /** Enable xAI Live Search (shortcut for searchParameters.mode = "on") */
146
+ search?: boolean;
147
+ /** Full xAI Live Search configuration (for Grok models) */
148
+ searchParameters?: SearchParameters;
149
+ }
150
+ declare class BlockrunError extends Error {
151
+ constructor(message: string);
152
+ }
153
+ declare class PaymentError extends BlockrunError {
154
+ constructor(message: string);
155
+ }
156
+ declare class APIError extends BlockrunError {
157
+ statusCode: number;
158
+ response?: unknown;
159
+ constructor(message: string, statusCode: number, response?: unknown);
160
+ }
161
+
162
+ /**
163
+ * BlockRun LLM Client - Main SDK entry point.
164
+ *
165
+ * Usage:
166
+ * import { LLMClient } from '@blockrun/llm';
167
+ *
168
+ * // Option 1: Use BASE_CHAIN_WALLET_KEY env var
169
+ * const client = new LLMClient();
170
+ *
171
+ * // Option 2: Pass private key directly
172
+ * const client = new LLMClient({ privateKey: '0x...' });
173
+ *
174
+ * const response = await client.chat('openai/gpt-4o', 'Hello!');
175
+ * console.log(response);
176
+ */
177
+
178
+ /**
179
+ * BlockRun LLM Gateway Client.
180
+ *
181
+ * Provides access to multiple LLM providers (OpenAI, Anthropic, Google, etc.)
182
+ * with automatic x402 micropayments on Base chain.
183
+ */
184
+ declare class LLMClient {
185
+ private account;
186
+ private privateKey;
187
+ private apiUrl;
188
+ private timeout;
189
+ private sessionTotalUsd;
190
+ private sessionCalls;
191
+ /**
192
+ * Initialize the BlockRun LLM client.
193
+ *
194
+ * @param options - Client configuration options (optional if BASE_CHAIN_WALLET_KEY env var is set)
195
+ */
196
+ constructor(options?: LLMClientOptions);
197
+ /**
198
+ * Simple 1-line chat interface.
199
+ *
200
+ * @param model - Model ID (e.g., 'openai/gpt-4o', 'anthropic/claude-sonnet-4')
201
+ * @param prompt - User message
202
+ * @param options - Optional chat parameters
203
+ * @returns Assistant's response text
204
+ *
205
+ * @example
206
+ * const response = await client.chat('gpt-4o', 'What is the capital of France?');
207
+ * console.log(response); // 'The capital of France is Paris.'
208
+ */
209
+ chat(model: string, prompt: string, options?: ChatOptions): Promise<string>;
210
+ /**
211
+ * Full chat completion interface (OpenAI-compatible).
212
+ *
213
+ * @param model - Model ID
214
+ * @param messages - Array of messages with role and content
215
+ * @param options - Optional completion parameters
216
+ * @returns ChatResponse object with choices and usage
217
+ */
218
+ chatCompletion(model: string, messages: ChatMessage[], options?: ChatCompletionOptions): Promise<ChatResponse>;
219
+ /**
220
+ * Make a request with automatic x402 payment handling.
221
+ */
222
+ private requestWithPayment;
223
+ /**
224
+ * Handle 402 response: parse requirements, sign payment, retry.
225
+ */
226
+ private handlePaymentAndRetry;
227
+ /**
228
+ * Fetch with timeout.
229
+ */
230
+ private fetchWithTimeout;
231
+ /**
232
+ * List available LLM models with pricing.
233
+ */
234
+ listModels(): Promise<Model[]>;
235
+ /**
236
+ * List available image generation models with pricing.
237
+ */
238
+ listImageModels(): Promise<ImageModel[]>;
239
+ /**
240
+ * List all available models (both LLM and image) with pricing.
241
+ *
242
+ * @returns Array of all models with 'type' field ('llm' or 'image')
243
+ *
244
+ * @example
245
+ * const models = await client.listAllModels();
246
+ * for (const model of models) {
247
+ * if (model.type === 'llm') {
248
+ * console.log(`LLM: ${model.id} - $${model.inputPrice}/M input`);
249
+ * } else {
250
+ * console.log(`Image: ${model.id} - $${model.pricePerImage}/image`);
251
+ * }
252
+ * }
253
+ */
254
+ listAllModels(): Promise<(Model | ImageModel)[]>;
255
+ /**
256
+ * Get current session spending.
257
+ *
258
+ * @returns Object with totalUsd and calls count
259
+ *
260
+ * @example
261
+ * const spending = client.getSpending();
262
+ * console.log(`Spent $${spending.totalUsd.toFixed(4)} across ${spending.calls} calls`);
263
+ */
264
+ getSpending(): Spending;
265
+ /**
266
+ * Get the wallet address being used for payments.
267
+ */
268
+ getWalletAddress(): string;
269
+ }
270
+
271
+ /**
272
+ * BlockRun Image Client - Generate images via x402 micropayments.
273
+ *
274
+ * SECURITY NOTE - Private Key Handling:
275
+ * Your private key NEVER leaves your machine. Here's what happens:
276
+ * 1. Key stays local - only used to sign an EIP-712 typed data message
277
+ * 2. Only the SIGNATURE is sent in the PAYMENT-SIGNATURE header
278
+ * 3. BlockRun verifies the signature on-chain via Coinbase CDP facilitator
279
+ *
280
+ * Usage:
281
+ * import { ImageClient } from '@blockrun/llm';
282
+ *
283
+ * const client = new ImageClient({ privateKey: '0x...' });
284
+ * const result = await client.generate('A cute cat in space');
285
+ * console.log(result.data[0].url);
286
+ */
287
+
288
+ /**
289
+ * BlockRun Image Generation Client.
290
+ *
291
+ * Generate images using Nano Banana (Google Gemini), DALL-E 3, or GPT Image
292
+ * with automatic x402 micropayments on Base chain.
293
+ */
294
+ declare class ImageClient {
295
+ private account;
296
+ private privateKey;
297
+ private apiUrl;
298
+ private timeout;
299
+ private sessionTotalUsd;
300
+ private sessionCalls;
301
+ /**
302
+ * Initialize the BlockRun Image client.
303
+ *
304
+ * @param options - Client configuration options
305
+ */
306
+ constructor(options?: ImageClientOptions);
307
+ /**
308
+ * Generate an image from a text prompt.
309
+ *
310
+ * @param prompt - Text description of the image to generate
311
+ * @param options - Optional generation parameters
312
+ * @returns ImageResponse with generated image URLs
313
+ *
314
+ * @example
315
+ * const result = await client.generate('A sunset over mountains');
316
+ * console.log(result.data[0].url);
317
+ */
318
+ generate(prompt: string, options?: ImageGenerateOptions): Promise<ImageResponse>;
319
+ /**
320
+ * List available image generation models with pricing.
321
+ */
322
+ listImageModels(): Promise<ImageModel[]>;
323
+ /**
324
+ * Make a request with automatic x402 payment handling.
325
+ */
326
+ private requestWithPayment;
327
+ /**
328
+ * Handle 402 response: parse requirements, sign payment, retry.
329
+ */
330
+ private handlePaymentAndRetry;
331
+ /**
332
+ * Fetch with timeout.
333
+ */
334
+ private fetchWithTimeout;
335
+ /**
336
+ * Get the wallet address being used for payments.
337
+ */
338
+ getWalletAddress(): string;
339
+ /**
340
+ * Get session spending information.
341
+ */
342
+ getSpending(): Spending;
343
+ }
344
+
345
+ /**
346
+ * x402 Payment Protocol v2 Implementation for BlockRun.
347
+ *
348
+ * This module handles creating signed payment payloads for the x402 v2 protocol.
349
+ * The private key is used ONLY for local signing and NEVER leaves the client.
350
+ */
351
+
352
+ declare const BASE_CHAIN_ID = 8453;
353
+ declare const USDC_BASE: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
354
+
355
+ /**
356
+ * BlockRun Wallet Management - Auto-create and manage wallets.
357
+ *
358
+ * Provides frictionless wallet setup for new users:
359
+ * - Auto-creates wallet if none exists
360
+ * - Stores key securely at ~/.blockrun/.session
361
+ * - Generates EIP-681 URIs for easy MetaMask funding
362
+ */
363
+ declare const USDC_BASE_CONTRACT = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
364
+ interface WalletInfo {
365
+ privateKey: string;
366
+ address: string;
367
+ isNew: boolean;
368
+ }
369
+ interface PaymentLinks {
370
+ basescan: string;
371
+ walletLink: string;
372
+ ethereum: string;
373
+ blockrun: string;
374
+ }
375
+ /**
376
+ * Create a new Ethereum wallet.
377
+ *
378
+ * @returns Object with address and privateKey
379
+ */
380
+ declare function createWallet(): {
381
+ address: string;
382
+ privateKey: string;
383
+ };
384
+ /**
385
+ * Save wallet private key to ~/.blockrun/.session
386
+ *
387
+ * @param privateKey - Private key string (with 0x prefix)
388
+ * @returns Path to saved wallet file
389
+ */
390
+ declare function saveWallet(privateKey: string): string;
391
+ /**
392
+ * Load wallet private key from file.
393
+ *
394
+ * @returns Private key string or null if not found
395
+ */
396
+ declare function loadWallet(): string | null;
397
+ /**
398
+ * Get existing wallet or create new one.
399
+ *
400
+ * Priority:
401
+ * 1. BLOCKRUN_WALLET_KEY environment variable
402
+ * 2. ~/.blockrun/.session file
403
+ * 3. ~/.blockrun/wallet.key file (legacy)
404
+ * 4. Create new wallet
405
+ *
406
+ * @returns WalletInfo with address, privateKey, and isNew flag
407
+ */
408
+ declare function getOrCreateWallet(): WalletInfo;
409
+ /**
410
+ * Get wallet address without exposing private key.
411
+ *
412
+ * @returns Wallet address or null if no wallet configured
413
+ */
414
+ declare function getWalletAddress(): string | null;
415
+ /**
416
+ * Generate EIP-681 URI for USDC transfer on Base.
417
+ *
418
+ * @param address - Recipient Ethereum address
419
+ * @param amountUsdc - Amount in USDC (default 1.0)
420
+ * @returns EIP-681 URI string for MetaMask/wallet scanning
421
+ */
422
+ declare function getEip681Uri(address: string, amountUsdc?: number): string;
423
+ /**
424
+ * Generate payment links for the wallet address.
425
+ *
426
+ * @param address - Ethereum address
427
+ * @returns Object with various payment links
428
+ */
429
+ declare function getPaymentLinks(address: string): PaymentLinks;
430
+ /**
431
+ * Format the message shown when a new wallet is created.
432
+ *
433
+ * @param address - New wallet address
434
+ * @returns Formatted message string
435
+ */
436
+ declare function formatWalletCreatedMessage(address: string): string;
437
+ /**
438
+ * Format the message shown when wallet needs more funds.
439
+ *
440
+ * @param address - Wallet address
441
+ * @returns Formatted message string
442
+ */
443
+ declare function formatNeedsFundingMessage(address: string): string;
444
+ /**
445
+ * Compact funding message (no QR) for repeated displays.
446
+ *
447
+ * @param address - Wallet address
448
+ * @returns Short formatted message string
449
+ */
450
+ declare function formatFundingMessageCompact(address: string): string;
451
+ declare const WALLET_FILE_PATH: string;
452
+ declare const WALLET_DIR_PATH: string;
453
+
454
+ /**
455
+ * OpenAI-compatible API wrapper for BlockRun LLM SDK.
456
+ *
457
+ * Drop-in replacement for OpenAI SDK - just change the import and use walletKey instead of apiKey.
458
+ *
459
+ * @example
460
+ * // Before (OpenAI)
461
+ * import OpenAI from 'openai';
462
+ * const client = new OpenAI({ apiKey: 'sk-...' });
463
+ *
464
+ * // After (BlockRun)
465
+ * import { OpenAI } from '@blockrun/llm';
466
+ * const client = new OpenAI({ walletKey: '0x...' });
467
+ *
468
+ * // Rest of your code stays exactly the same!
469
+ * const response = await client.chat.completions.create({
470
+ * model: 'gpt-4o',
471
+ * messages: [{ role: 'user', content: 'Hello!' }]
472
+ * });
473
+ */
474
+
475
+ interface OpenAIClientOptions {
476
+ /** EVM wallet private key (replaces apiKey) */
477
+ walletKey?: `0x${string}` | string;
478
+ /** Alternative: use privateKey like LLMClient */
479
+ privateKey?: `0x${string}` | string;
480
+ /** API endpoint URL (default: https://blockrun.ai/api) */
481
+ baseURL?: string;
482
+ /** Request timeout in milliseconds */
483
+ timeout?: number;
484
+ }
485
+ interface OpenAIChatCompletionParams {
486
+ model: string;
487
+ messages: Array<{
488
+ role: "system" | "user" | "assistant";
489
+ content: string;
490
+ }>;
491
+ max_tokens?: number;
492
+ temperature?: number;
493
+ top_p?: number;
494
+ stream?: boolean;
495
+ n?: number;
496
+ stop?: string | string[];
497
+ presence_penalty?: number;
498
+ frequency_penalty?: number;
499
+ user?: string;
500
+ }
501
+ interface OpenAIChatCompletionChoice {
502
+ index: number;
503
+ message: {
504
+ role: "assistant";
505
+ content: string;
506
+ };
507
+ finish_reason: string | null;
508
+ }
509
+ interface OpenAIChatCompletionResponse {
510
+ id: string;
511
+ object: "chat.completion";
512
+ created: number;
513
+ model: string;
514
+ choices: OpenAIChatCompletionChoice[];
515
+ usage?: {
516
+ prompt_tokens: number;
517
+ completion_tokens: number;
518
+ total_tokens: number;
519
+ };
520
+ }
521
+ interface OpenAIChatCompletionChunk {
522
+ id: string;
523
+ object: "chat.completion.chunk";
524
+ created: number;
525
+ model: string;
526
+ choices: Array<{
527
+ index: number;
528
+ delta: {
529
+ role?: "assistant";
530
+ content?: string;
531
+ };
532
+ finish_reason: string | null;
533
+ }>;
534
+ }
535
+ /**
536
+ * Chat completions API (OpenAI-compatible)
537
+ */
538
+ declare class ChatCompletions {
539
+ private client;
540
+ private apiUrl;
541
+ private timeout;
542
+ constructor(client: LLMClient, apiUrl: string, timeout: number);
543
+ /**
544
+ * Create a chat completion (OpenAI-compatible).
545
+ */
546
+ create(params: OpenAIChatCompletionParams): Promise<OpenAIChatCompletionResponse>;
547
+ create(params: OpenAIChatCompletionParams & {
548
+ stream: true;
549
+ }): Promise<AsyncIterable<OpenAIChatCompletionChunk>>;
550
+ private createStream;
551
+ private transformResponse;
552
+ }
553
+ /**
554
+ * Chat API namespace
555
+ */
556
+ declare class Chat {
557
+ completions: ChatCompletions;
558
+ constructor(client: LLMClient, apiUrl: string, timeout: number);
559
+ }
560
+ /**
561
+ * OpenAI-compatible client for BlockRun.
562
+ *
563
+ * Drop-in replacement for the OpenAI SDK.
564
+ *
565
+ * @example
566
+ * import { OpenAI } from '@blockrun/llm';
567
+ *
568
+ * const client = new OpenAI({ walletKey: '0x...' });
569
+ *
570
+ * const response = await client.chat.completions.create({
571
+ * model: 'gpt-4o',
572
+ * messages: [{ role: 'user', content: 'Hello!' }]
573
+ * });
574
+ *
575
+ * console.log(response.choices[0].message.content);
576
+ */
577
+ declare class OpenAI {
578
+ chat: Chat;
579
+ private client;
580
+ constructor(options?: OpenAIClientOptions);
581
+ /**
582
+ * Get the wallet address being used for payments.
583
+ */
584
+ getWalletAddress(): string;
585
+ }
586
+
587
+ export { APIError, BASE_CHAIN_ID, BlockrunError, type ChatChoice, type ChatCompletionOptions, type ChatMessage, type ChatOptions, type ChatResponse, type ChatUsage, 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, 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 };