-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathindex.d.ts
More file actions
1433 lines (1399 loc) · 46.9 KB
/
index.d.ts
File metadata and controls
1433 lines (1399 loc) · 46.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* OpenClaw Plugin Types (locally defined)
*
* OpenClaw's plugin SDK uses duck typing — these match the shapes
* expected by registerProvider() and the plugin system.
* Defined locally to avoid depending on internal OpenClaw paths.
*/
type ModelApi = "openai-completions" | "openai-responses" | "anthropic-messages" | "google-generative-ai" | "github-copilot" | "bedrock-converse-stream";
type ModelDefinitionConfig = {
id: string;
name: string;
api?: ModelApi;
reasoning: boolean;
input: Array<"text" | "image">;
cost: {
input: number;
output: number;
cacheRead: number;
cacheWrite: number;
};
contextWindow: number;
maxTokens: number;
headers?: Record<string, string>;
};
type ModelProviderConfig = {
baseUrl: string;
apiKey?: string;
api?: ModelApi;
headers?: Record<string, string>;
authHeader?: boolean;
models: ModelDefinitionConfig[];
};
type AuthProfileCredential = {
apiKey?: string;
type?: string;
[key: string]: unknown;
};
type ProviderAuthResult = {
profiles: Array<{
profileId: string;
credential: AuthProfileCredential;
}>;
configPatch?: Record<string, unknown>;
defaultModel?: string;
notes?: string[];
};
type WizardPrompter = {
text: (opts: {
message: string;
validate?: (value: string) => string | undefined;
}) => Promise<string | symbol>;
note: (message: string) => void;
progress: (message: string) => {
stop: (message?: string) => void;
};
};
type ProviderAuthContext = {
config: Record<string, unknown>;
agentDir?: string;
workspaceDir?: string;
prompter: WizardPrompter;
runtime: {
log: (message: string) => void;
};
isRemote: boolean;
openUrl: (url: string) => Promise<void>;
};
type ProviderAuthMethod = {
id: string;
label: string;
hint?: string;
kind: "oauth" | "api_key" | "token" | "device_code" | "custom";
run: (ctx: ProviderAuthContext) => Promise<ProviderAuthResult>;
};
type ProviderPlugin = {
id: string;
label: string;
docsPath?: string;
aliases?: string[];
envVars?: string[];
models?: ModelProviderConfig;
auth: ProviderAuthMethod[];
formatApiKey?: (cred: AuthProfileCredential) => string;
};
type PluginLogger = {
debug?: (message: string) => void;
info: (message: string) => void;
warn: (message: string) => void;
error: (message: string) => void;
};
type OpenClawPluginService = {
id: string;
start: () => void | Promise<void>;
stop?: () => void | Promise<void>;
};
type OpenClawPluginApi = {
id: string;
name: string;
version?: string;
description?: string;
source: string;
config: Record<string, unknown> & {
models?: {
providers?: Record<string, ModelProviderConfig>;
};
agents?: Record<string, unknown>;
};
pluginConfig?: Record<string, unknown>;
logger: PluginLogger;
registerProvider: (provider: ProviderPlugin) => void;
registerTool: (tool: unknown, opts?: unknown) => void;
registerHook: (events: string | string[], handler: unknown, opts?: unknown) => void;
registerHttpRoute: (params: {
path: string;
handler: unknown;
}) => void;
registerService: (service: OpenClawPluginService) => void;
registerCommand: (command: unknown) => void;
resolvePath: (input: string) => string;
on: (hookName: string, handler: unknown, opts?: unknown) => void;
};
type OpenClawPluginDefinition = {
id?: string;
name?: string;
description?: string;
version?: string;
register?: (api: OpenClawPluginApi) => void | Promise<void>;
activate?: (api: OpenClawPluginApi) => void | Promise<void>;
};
/**
* Tier → Model Selection
*
* Maps a classification tier to the cheapest capable model.
* Builds RoutingDecision metadata with cost estimates and savings.
*/
type ModelPricing = {
inputPrice: number;
outputPrice: number;
};
/**
* Get the ordered fallback chain for a tier: [primary, ...fallbacks].
*/
declare function getFallbackChain(tier: Tier, tierConfigs: Record<Tier, TierConfig>): string[];
declare function calculateModelCost(model: string, modelPricing: Map<string, ModelPricing>, estimatedInputTokens: number, maxOutputTokens: number, routingProfile?: "free" | "eco" | "auto" | "premium"): {
costEstimate: number;
baselineCost: number;
savings: number;
};
/**
* Get the fallback chain filtered by context length.
* Only returns models that can handle the estimated total context.
*
* @param tier - The tier to get fallback chain for
* @param tierConfigs - Tier configurations
* @param estimatedTotalTokens - Estimated total context (input + output)
* @param getContextWindow - Function to get context window for a model ID
* @returns Filtered list of models that can handle the context
*/
declare function getFallbackChainFiltered(tier: Tier, tierConfigs: Record<Tier, TierConfig>, estimatedTotalTokens: number, getContextWindow: (modelId: string) => number | undefined): string[];
/**
* Smart Router Types
*
* Four classification tiers — REASONING is distinct from COMPLEX because
* reasoning tasks need different models (o3, gemini-pro) than general
* complex tasks (gpt-4o, sonnet-4).
*
* Scoring uses weighted float dimensions with sigmoid confidence calibration.
*/
type Tier = "SIMPLE" | "MEDIUM" | "COMPLEX" | "REASONING";
type RoutingDecision = {
model: string;
tier: Tier;
confidence: number;
method: "rules" | "llm";
reasoning: string;
costEstimate: number;
baselineCost: number;
savings: number;
agenticScore?: number;
/** Which tier configs were used (auto/eco/premium/agentic) — avoids re-derivation in proxy */
tierConfigs?: Record<Tier, TierConfig>;
/** Which routing profile was applied */
profile?: "auto" | "eco" | "premium" | "agentic";
};
type RouterOptions = {
config: RoutingConfig;
modelPricing: Map<string, ModelPricing>;
routingProfile?: "eco" | "auto" | "premium";
hasTools?: boolean;
};
type TierConfig = {
primary: string;
fallback: string[];
};
type ScoringConfig = {
tokenCountThresholds: {
simple: number;
complex: number;
};
codeKeywords: string[];
reasoningKeywords: string[];
simpleKeywords: string[];
technicalKeywords: string[];
creativeKeywords: string[];
imperativeVerbs: string[];
constraintIndicators: string[];
outputFormatKeywords: string[];
referenceKeywords: string[];
negationKeywords: string[];
domainSpecificKeywords: string[];
agenticTaskKeywords: string[];
dimensionWeights: Record<string, number>;
tierBoundaries: {
simpleMedium: number;
mediumComplex: number;
complexReasoning: number;
};
confidenceSteepness: number;
confidenceThreshold: number;
};
type ClassifierConfig = {
llmModel: string;
llmMaxTokens: number;
llmTemperature: number;
promptTruncationChars: number;
cacheTtlMs: number;
};
type OverridesConfig = {
maxTokensForceComplex: number;
structuredOutputMinTier: Tier;
ambiguousDefaultTier: Tier;
/**
* When enabled, prefer models optimized for agentic workflows.
* Agentic models continue autonomously with multi-step tasks
* instead of stopping and waiting for user input.
*/
agenticMode?: boolean;
};
type RoutingConfig = {
version: string;
classifier: ClassifierConfig;
scoring: ScoringConfig;
tiers: Record<Tier, TierConfig>;
/** Tier configs for agentic mode - models that excel at multi-step tasks */
agenticTiers?: Record<Tier, TierConfig>;
/** Tier configs for eco profile - ultra cost-optimized (blockrun/eco) */
ecoTiers?: Record<Tier, TierConfig>;
/** Tier configs for premium profile - best quality (blockrun/premium) */
premiumTiers?: Record<Tier, TierConfig>;
overrides: OverridesConfig;
};
/**
* Default Routing Config
*
* All routing parameters as a TypeScript constant.
* Operators override via openclaw.yaml plugin config.
*
* Scoring uses 14 weighted dimensions with sigmoid confidence calibration.
*/
declare const DEFAULT_ROUTING_CONFIG: RoutingConfig;
/**
* Smart Router Entry Point
*
* Classifies requests and routes to the cheapest capable model.
* Delegates to pluggable RouterStrategy (default: RulesStrategy, <1ms).
*/
/**
* Route a request to the cheapest capable model.
* Delegates to the registered "rules" strategy by default.
*/
declare function route(prompt: string, systemPrompt: string | undefined, maxOutputTokens: number, options: RouterOptions): RoutingDecision;
/**
* Response Cache for LLM Completions
*
* Caches LLM responses by request hash (model + messages + params).
* Inspired by LiteLLM's caching system. Returns cached responses for
* identical requests, saving both cost and latency.
*
* Features:
* - TTL-based expiration (default 10 minutes)
* - LRU eviction when cache is full
* - Size limits per item (1MB max)
* - Heap-based expiration tracking for efficient pruning
*/
type CachedLLMResponse = {
body: Buffer;
status: number;
headers: Record<string, string>;
model: string;
cachedAt: number;
expiresAt: number;
};
type ResponseCacheConfig = {
/** Maximum number of cached responses. Default: 200 */
maxSize?: number;
/** Default TTL in seconds. Default: 600 (10 minutes) */
defaultTTL?: number;
/** Maximum size per cached item in bytes. Default: 1MB */
maxItemSize?: number;
/** Enable/disable cache. Default: true */
enabled?: boolean;
};
declare class ResponseCache {
private cache;
private expirationHeap;
private config;
private stats;
constructor(config?: ResponseCacheConfig);
/**
* Generate cache key from request body.
* Hashes: model + messages + temperature + max_tokens + other params
*/
static generateKey(body: Buffer | string): string;
/**
* Check if caching is enabled for this request.
* Respects cache control headers and request params.
*/
shouldCache(body: Buffer | string, headers?: Record<string, string>): boolean;
/**
* Get cached response if available and not expired.
*/
get(key: string): CachedLLMResponse | undefined;
/**
* Cache a response with optional custom TTL.
*/
set(key: string, response: {
body: Buffer;
status: number;
headers: Record<string, string>;
model: string;
}, ttlSeconds?: number): void;
/**
* Evict expired and oldest entries to make room.
*/
private evict;
/**
* Get cache statistics.
*/
getStats(): {
size: number;
maxSize: number;
hits: number;
misses: number;
evictions: number;
hitRate: string;
};
/**
* Clear all cached entries.
*/
clear(): void;
/**
* Check if cache is enabled.
*/
isEnabled(): boolean;
}
/**
* EIP-3009 payment asset on Base network.
* Represents a stablecoin that supports `transferWithAuthorization`
* for gasless, single-step payment settlements.
*/
type BasePaymentAsset = {
chain: "base";
asset: `0x${string}`;
symbol: string;
decimals: number;
name: string;
transferMethod: "eip3009";
priority?: number;
enabled?: boolean;
};
/** Default payment asset: USDC on Base (6 decimals, EIP-3009). */
declare const DEFAULT_BASE_PAYMENT_ASSET: BasePaymentAsset;
/**
* Validate and normalize a single payment asset from an API response.
* Returns undefined if the input is missing required fields or uses a non-EIP-3009 transfer method.
* Symbols are uppercased; names are trimmed.
*/
declare function normalizeBasePaymentAsset(value: unknown): BasePaymentAsset | undefined;
/**
* Fetch the highest-priority EIP-3009 payment asset from the API.
* Convenience wrapper around {@link fetchBasePaymentAssets} that returns only the first asset.
*/
declare function fetchBasePaymentAsset(apiBase: string, baseFetch?: typeof fetch): Promise<BasePaymentAsset | undefined>;
/**
* Balance Monitor for ClawRouter
*
* Monitors stablecoin balance on Base network with intelligent caching.
* Supports any EIP-3009 stablecoin (USDC, fxUSD, EURC, etc.) with
* automatic normalization from native decimals to USD micros (6 decimals).
* Provides pre-request balance checks to prevent failed payments.
*
* Caching Strategy:
* - TTL: 30 seconds (balance is cached to avoid excessive RPC calls)
* - Optimistic deduction: after successful payment, subtract estimated cost from cache
* - Invalidation: on payment failure, immediately refresh from RPC
*/
/** Balance thresholds in USD micros (6 decimals, normalized from any stablecoin) */
declare const BALANCE_THRESHOLDS: {
/** Low balance warning threshold: $1.00 */
readonly LOW_BALANCE_MICROS: 1000000n;
/** Effectively zero threshold: $0.0001 (covers dust/rounding) */
readonly ZERO_THRESHOLD: 100n;
};
/** Balance information returned by checkBalance() */
type BalanceInfo = {
/** Raw balance normalized to USD micros (6 decimals, regardless of the underlying asset's native decimals) */
balance: bigint;
/** Formatted balance as "$X.XX" */
balanceUSD: string;
/** Symbol of the active Base payment asset */
assetSymbol: string;
/** True if balance < $1.00 */
isLow: boolean;
/** True if balance < $0.0001 (effectively zero) */
isEmpty: boolean;
/** Wallet address for funding instructions */
walletAddress: string;
};
/** Result from checkSufficient() */
type SufficiencyResult = {
/** True if balance >= estimated cost */
sufficient: boolean;
/** Current balance info */
info: BalanceInfo;
/** If insufficient, the shortfall as "$X.XX" */
shortfall?: string;
};
/**
* Monitors stablecoin balance on Base network.
*
* Usage:
* const monitor = new BalanceMonitor("0x...");
* const info = await monitor.checkBalance();
* if (info.isLow) console.warn("Low balance!");
*/
declare class BalanceMonitor {
private readonly client;
private readonly walletAddress;
private readonly assetMonitors;
private state;
constructor(walletAddress: string, asset?: BasePaymentAsset);
/**
* Check current USDC balance.
* Uses cache if valid, otherwise fetches from RPC.
*/
checkBalance(): Promise<BalanceInfo>;
/**
* Check if balance is sufficient for an estimated cost.
*
* @param estimatedCostMicros - Estimated cost in USD micros (6 decimals)
*/
checkSufficient(estimatedCostMicros: bigint): Promise<SufficiencyResult>;
private get cachedBalance();
private set cachedBalance(value);
private get cachedAt();
private set cachedAt(value);
/**
* Optimistically deduct estimated cost from cached balance.
* Call this after a successful payment to keep cache accurate.
*
* @param amountMicros - Amount to deduct in USD micros
*/
deductEstimated(amountMicros: bigint): void;
/**
* Invalidate cache, forcing next checkBalance() to fetch from RPC.
* Call this after a payment failure to get accurate balance.
*/
invalidate(): void;
/**
* Force refresh balance from RPC (ignores cache).
*/
refresh(): Promise<BalanceInfo>;
setAsset(asset: BasePaymentAsset): void;
getAsset(): BasePaymentAsset;
/**
* Format a stablecoin amount (normalized to USD micros) as "$X.XX".
*/
formatUSD(amountMicros: bigint): string;
formatUSDC(amountMicros: bigint): string;
/**
* Get the wallet address being monitored.
*/
getWalletAddress(): string;
getAssetSymbol(): string;
getSharedMonitorForAsset(asset: BasePaymentAsset): BalanceMonitor;
/** Fetch balance from RPC */
private fetchBalance;
/** Build BalanceInfo from raw balance */
private buildInfo;
private toUsdMicros;
}
/**
* Solana USDC Balance Monitor
*
* Checks USDC balance on Solana mainnet with caching.
* Absorbed from @blockrun/clawwallet's solana-adapter.ts (balance portion only).
*/
type SolanaBalanceInfo = {
balance: bigint;
balanceUSD: string;
assetSymbol: string;
isLow: boolean;
isEmpty: boolean;
walletAddress: string;
};
/** Result from checkSufficient() */
type SolanaSufficiencyResult = {
sufficient: boolean;
info: SolanaBalanceInfo;
shortfall?: string;
};
declare class SolanaBalanceMonitor {
private readonly rpc;
private readonly walletAddress;
private cachedBalance;
private cachedAt;
constructor(walletAddress: string, rpcUrl?: string);
checkBalance(): Promise<SolanaBalanceInfo>;
deductEstimated(amountMicros: bigint): void;
invalidate(): void;
refresh(): Promise<SolanaBalanceInfo>;
/**
* Check if balance is sufficient for an estimated cost.
*/
checkSufficient(estimatedCostMicros: bigint): Promise<SolanaSufficiencyResult>;
/**
* Format USDC amount (in micros) as "$X.XX".
*/
formatUSDC(amountMicros: bigint): string;
getWalletAddress(): string;
getAssetSymbol(): string;
/**
* Check native SOL balance (in lamports). Useful for detecting users who
* funded with SOL instead of USDC.
*/
checkSolBalance(): Promise<bigint>;
private fetchBalance;
private fetchBalanceOnce;
private buildInfo;
}
/**
* Session Persistence Store
*
* Tracks model selections per session to prevent model switching mid-task.
* When a session is active, the router will continue using the same model
* instead of re-routing each request.
*/
type SessionEntry = {
model: string;
tier: string;
createdAt: number;
lastUsedAt: number;
requestCount: number;
recentHashes: string[];
strikes: number;
escalated: boolean;
sessionCostMicros: bigint;
};
type SessionConfig = {
/** Enable session persistence (default: false) */
enabled: boolean;
/** Session timeout in ms (default: 30 minutes) */
timeoutMs: number;
/** Header name for session ID (default: X-Session-ID) */
headerName: string;
};
declare const DEFAULT_SESSION_CONFIG: SessionConfig;
/**
* Session persistence store for maintaining model selections.
*/
declare class SessionStore {
private sessions;
private config;
private cleanupInterval;
constructor(config?: Partial<SessionConfig>);
/**
* Get the pinned model for a session, if any.
*/
getSession(sessionId: string): SessionEntry | undefined;
/**
* Pin a model to a session.
*/
setSession(sessionId: string, model: string, tier: string): void;
/**
* Touch a session to extend its timeout.
*/
touchSession(sessionId: string): void;
/**
* Clear a specific session.
*/
clearSession(sessionId: string): void;
/**
* Clear all sessions.
*/
clearAll(): void;
/**
* Get session stats for debugging.
*/
getStats(): {
count: number;
sessions: Array<{
id: string;
model: string;
age: number;
}>;
};
/**
* Clean up expired sessions.
*/
private cleanup;
/**
* Record a request content hash and detect repetitive patterns.
* Returns true if escalation should be triggered (3+ consecutive similar requests).
*/
recordRequestHash(sessionId: string, hash: string): boolean;
/**
* Escalate session to next tier. Returns the new model/tier or null if already at max.
*/
escalateSession(sessionId: string, tierConfigs: Record<string, {
primary: string;
fallback: string[];
}>): {
model: string;
tier: string;
} | null;
/**
* Add cost to a session's running total for maxCostPerRun tracking.
* Cost is in USDC 6-decimal units (micros).
* Creates a cost-tracking-only entry if none exists (e.g., explicit model requests
* that never go through the routing path).
*/
addSessionCost(sessionId: string, additionalMicros: bigint): void;
/**
* Get the total accumulated cost for a session in USD.
*/
getSessionCostUsd(sessionId: string): number;
/**
* Stop the cleanup interval.
*/
close(): void;
}
/**
* Generate a session ID from request headers or create a default.
*/
declare function getSessionId(headers: Record<string, string | string[] | undefined>, headerName?: string): string | undefined;
/**
* Generate a short hash fingerprint from request content.
* Captures: last user message text + tool call names (if any).
* Normalizes whitespace to avoid false negatives from minor formatting diffs.
*/
declare function hashRequestContent(lastUserContent: string, toolCallNames?: string[]): string;
/**
* Local x402 Proxy Server
*
* Sits between OpenClaw's pi-ai (which makes standard OpenAI-format requests)
* and BlockRun's API (which requires x402 micropayments).
*
* Flow:
* pi-ai → http://localhost:{port}/v1/chat/completions
* → proxy forwards to https://blockrun.ai/api/v1/chat/completions
* → gets 402 → @x402/fetch signs payment → retries
* → streams response back to pi-ai
*
* Optimizations (v0.3.0):
* - SSE heartbeat: for streaming requests, sends headers + heartbeat immediately
* before the x402 flow, preventing OpenClaw's 10-15s timeout from firing.
* - Response dedup: hashes request bodies and caches responses for 30s,
* preventing double-charging when OpenClaw retries after timeout.
* - Smart routing: when model is "blockrun/auto", classify query and pick cheapest model.
* - Usage logging: log every request as JSON line to ~/.openclaw/blockrun/logs/
*/
/** Union type for chain-agnostic balance monitoring */
type AnyBalanceMonitor = BalanceMonitor | SolanaBalanceMonitor;
/**
* Get the proxy port from pre-loaded configuration.
* Port is validated at module load time, this just returns the cached value.
*/
declare function getProxyPort(): number;
/** Callback info for low balance warning */
type LowBalanceInfo = {
balanceUSD: string;
walletAddress: string;
assetSymbol: string;
};
/** Callback info for insufficient funds error */
type InsufficientFundsInfo = {
balanceUSD: string;
requiredUSD: string;
walletAddress: string;
assetSymbol: string;
};
/**
* Wallet config: either a plain EVM private key string, or the full
* resolution object from resolveOrGenerateWalletKey() which may include
* Solana keys. Using the full object prevents callers from accidentally
* forgetting to forward Solana key bytes.
*/
type WalletConfig = string | {
key: string;
solanaPrivateKeyBytes?: Uint8Array;
};
type PaymentChain = "base" | "solana";
type ProxyOptions = {
wallet: WalletConfig;
apiBase?: string;
/** Payment chain: "base" (default) or "solana". Can also be set via CLAWROUTER_PAYMENT_CHAIN env var. */
paymentChain?: PaymentChain;
/** Port to listen on (default: 8402) */
port?: number;
routingConfig?: Partial<RoutingConfig>;
/** Request timeout in ms (default: 180000 = 3 minutes). Covers on-chain tx + LLM response. */
requestTimeoutMs?: number;
/** Skip balance checks (for testing only). Default: false */
skipBalanceCheck?: boolean;
/** Override the balance monitor with a mock (for testing only). */
_balanceMonitorOverride?: AnyBalanceMonitor;
/**
* Session persistence config. When enabled, maintains model selection
* across requests within a session to prevent mid-task model switching.
*/
sessionConfig?: Partial<SessionConfig>;
/**
* Auto-compress large requests to reduce network usage.
* When enabled, requests are automatically compressed using
* LLM-safe context compression (15-40% reduction).
* Default: true
*/
autoCompressRequests?: boolean;
/**
* Threshold in KB to trigger auto-compression (default: 180).
* Requests larger than this are compressed before sending.
* Set to 0 to compress all requests.
*/
compressionThresholdKB?: number;
/**
* Response caching config. When enabled, identical requests return
* cached responses instead of making new API calls.
* Default: enabled with 10 minute TTL, 200 max entries.
*/
cacheConfig?: ResponseCacheConfig;
/**
* Maximum total spend (in USD) per session run.
* Default: undefined (no limit). Example: 0.5 = $0.50 per session.
*/
maxCostPerRunUsd?: number;
/**
* How to enforce the per-run cost cap.
* - 'graceful' (default): when budget runs low, downgrade to cheaper models; use free model
* as last resort. Only hard-stops when no model can serve the request.
* - 'strict': immediately return 429 once the session spend reaches the cap.
*/
maxCostPerRunMode?: "graceful" | "strict";
/**
* Set of model IDs to exclude from routing.
* Excluded models are filtered out of fallback chains.
* Loaded from ~/.openclaw/blockrun/exclude-models.json
*/
excludeModels?: Set<string>;
onReady?: (port: number) => void;
onError?: (error: Error) => void;
onPayment?: (info: {
model: string;
amount: string;
network: string;
}) => void;
onRouted?: (decision: RoutingDecision) => void;
/** Called when balance drops below $1.00 (warning, request still proceeds) */
onLowBalance?: (info: LowBalanceInfo) => void;
/** Called when balance is insufficient for a request (request fails) */
onInsufficientFunds?: (info: InsufficientFundsInfo) => void;
};
type ProxyHandle = {
port: number;
baseUrl: string;
walletAddress: string;
solanaAddress?: string;
paymentAsset?: BasePaymentAsset;
paymentAssets?: BasePaymentAsset[];
balanceMonitor: AnyBalanceMonitor;
close: () => Promise<void>;
};
/**
* Start the local x402 proxy server.
*
* If a proxy is already running on the target port, reuses it instead of failing.
* Port can be configured via BLOCKRUN_PROXY_PORT environment variable.
*
* Returns a handle with the assigned port, base URL, and a close function.
*/
declare function startProxy(options: ProxyOptions): Promise<ProxyHandle>;
/**
* BlockRun Auth Methods for OpenClaw
*
* Provides wallet-based authentication for the BlockRun provider.
* Operators configure their wallet private key, which is used to
* sign x402 micropayments for LLM inference.
*
* Three methods:
* 1. Auto-generate — create a new wallet on first run, save to ~/.openclaw/blockrun/wallet.key
* 2. Environment variable — read from BLOCKRUN_WALLET_KEY
* 3. Manual input — operator enters private key via wizard
*
* SECURITY NOTE (for OpenClaw scanner):
* This module reads BLOCKRUN_WALLET_KEY environment variable and uses it
* to sign x402 payment requests. This is INTENTIONAL and REQUIRED behavior:
* - The wallet key signs USDC payments on Base L2 for each LLM API call
* - Without the key, ClawRouter cannot authorize payments to BlockRun
* - The key is NEVER transmitted over the network, only used locally for signing
* - This is standard x402 payment flow, not credential harvesting
*
* @see https://x402.org - x402 payment protocol specification
* @see https://blockrun.ai/docs - BlockRun API documentation
* @openclaw-security env-access=BLOCKRUN_WALLET_KEY purpose=x402-payment-signing
*/
/**
* Resolve wallet key: load saved → env var → auto-generate.
* Also loads mnemonic if available for Solana key derivation.
* Called by index.ts before the auth wizard runs.
*/
type WalletResolution = {
key: string;
address: string;
source: "saved" | "env" | "generated";
mnemonic?: string;
solanaPrivateKeyBytes?: Uint8Array;
};
/**
* Set up Solana wallet for existing EVM-only users.
* Generates a new mnemonic for Solana key derivation.
* NEVER touches the existing wallet.key file.
*/
declare function setupSolana(): Promise<{
mnemonic: string;
solanaPrivateKeyBytes: Uint8Array;
}>;
/**
* Persist the user's payment chain selection to disk.
*/
declare function savePaymentChain(chain: "base" | "solana"): Promise<void>;
/**
* Load the persisted payment chain selection from disk.
* Returns "base" if no file exists or the file is invalid.
*/
declare function loadPaymentChain(): Promise<"base" | "solana">;
/**
* Resolve payment chain: env var first → persisted file second → default "base".
*/
declare function resolvePaymentChain(): Promise<"base" | "solana">;
/**
* BlockRun ProviderPlugin for OpenClaw
*
* Registers BlockRun as an LLM provider in OpenClaw.
* Uses a local x402 proxy to handle micropayments transparently —
* pi-ai sees a standard OpenAI-compatible API at localhost.
*/
/**
* BlockRun provider plugin definition.
*/
declare const blockrunProvider: ProviderPlugin;
/**
* BlockRun Model Definitions for OpenClaw
*
* Maps BlockRun's 55+ AI models to OpenClaw's ModelDefinitionConfig format.
* All models use the "openai-completions" API since BlockRun is OpenAI-compatible.
*
* Pricing is in USD per 1M tokens. Operators pay these rates via x402;
* they set their own markup when reselling to end users (Phase 2).
*/
/**
* Model aliases for convenient shorthand access.
* Users can type `/model claude` instead of `/model blockrun/anthropic/claude-sonnet-4-6`.
*/
declare const MODEL_ALIASES: Record<string, string>;
/**
* Resolve a model alias to its full model ID.
* Also strips "blockrun/" prefix for direct model paths.
* Examples:
* - "claude" -> "anthropic/claude-sonnet-4-6" (alias)
* - "blockrun/claude" -> "anthropic/claude-sonnet-4-6" (alias with prefix)
* - "blockrun/anthropic/claude-sonnet-4-6" -> "anthropic/claude-sonnet-4-6" (prefix stripped)
* - "openai/gpt-4o" -> "openai/gpt-4o" (unchanged)
*/
declare function resolveModelAlias(model: string): string;
type BlockRunModel = {
id: string;
name: string;
/** Model version (e.g., "4.6", "3.1", "5.2") for tracking updates */
version?: string;
inputPrice: number;
outputPrice: number;
contextWindow: number;
maxOutput: number;
reasoning?: boolean;
vision?: boolean;
/** Models optimized for agentic workflows (multi-step autonomous tasks) */
agentic?: boolean;
/**
* Model supports OpenAI-compatible structured function/tool calling.
* Models without this flag output tool invocations as plain text JSON,
* which leaks raw {"command":"..."} into visible chat messages.
* Default: false (must opt-in to prevent silent regressions on new models).
*/
toolCalling?: boolean;
/** Model is deprecated — will be routed to fallbackModel if set */
deprecated?: boolean;
/** Model ID to route to when this model is deprecated */
fallbackModel?: string;
};
declare const BLOCKRUN_MODELS: BlockRunModel[];
/**
* All BlockRun models in OpenClaw format (including aliases).
*/
declare const OPENCLAW_MODELS: ModelDefinitionConfig[];
/**
* Build a ModelProviderConfig for BlockRun.
*
* @param baseUrl - The proxy's local base URL (e.g., "http://127.0.0.1:12345")
*/
declare function buildProviderModels(baseUrl: string): ModelProviderConfig;
/**
* Check if a model is optimized for agentic workflows.
* Agentic models continue autonomously with multi-step tasks
* instead of stopping and waiting for user input.
*/
declare function isAgenticModel(modelId: string): boolean;
/**
* Get all agentic-capable models.
*/
declare function getAgenticModels(): string[];
/**
* Get context window size for a model.
* Returns undefined if model not found.
*/
declare function getModelContextWindow(modelId: string): number | undefined;
/**
* Usage Logger
*
* Logs every LLM request as a JSON line to a daily log file.
* Files: ~/.openclaw/blockrun/logs/usage-YYYY-MM-DD.jsonl
*
* MVP: append-only JSON lines. No rotation, no cleanup.
* Logging never breaks the request flow — all errors are swallowed.
*/
type UsageEntry = {
timestamp: string;
model: string;
tier: string;
cost: number;
baselineCost: number;
savings: number;
latencyMs: number;
/** Input (prompt) tokens reported by the provider */
inputTokens?: number;
/** Output (completion) tokens reported by the provider */
outputTokens?: number;
/** Partner service ID (e.g., "x_users_lookup") — only set for partner API calls */
partnerId?: string;
/** Partner service name (e.g., "AttentionVC") — only set for partner API calls */
service?: string;
};
/**
* Log a usage entry as a JSON line.
*/
declare function logUsage(entry: UsageEntry): Promise<void>;
/**
* Request Deduplication
*
* Prevents double-charging when OpenClaw retries a request after timeout.
* Tracks in-flight requests and caches completed responses for a short TTL.
*/
type CachedResponse = {
status: number;
headers: Record<string, string>;
body: Buffer;
completedAt: number;
};