@blockrun/franklin 3.45.0 → 3.47.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 +32 -11
- package/dist/agent/context.js +58 -10
- package/dist/agent/llm.js +14 -6
- package/dist/agent/loop.js +34 -6
- package/dist/agent/optimize.d.ts +1 -1
- package/dist/agent/optimize.js +17 -7
- package/dist/agent/secret-redact.d.ts +26 -0
- package/dist/agent/secret-redact.js +48 -0
- package/dist/agent/streaming-executor.js +15 -0
- package/dist/commands/doctor.js +18 -0
- package/dist/commands/start.js +4 -0
- package/dist/commands/usage.d.ts +14 -0
- package/dist/commands/usage.js +86 -0
- package/dist/commands/wallet-adopt.d.ts +12 -0
- package/dist/commands/wallet-adopt.js +49 -0
- package/dist/free-models.js +2 -1
- package/dist/gateway-models.js +34 -66
- package/dist/index.js +16 -0
- package/dist/model-catalog.d.ts +10 -0
- package/dist/model-catalog.js +77 -0
- package/dist/payments/auth-mode.d.ts +5 -19
- package/dist/payments/auth-mode.js +15 -17
- package/dist/payments/billing-copy.d.ts +37 -0
- package/dist/payments/billing-copy.js +57 -0
- package/dist/payments/post-with-payment.js +8 -15
- package/dist/payments/price-catalog.d.ts +9 -0
- package/dist/payments/price-catalog.js +13 -0
- package/dist/payments/usage.d.ts +106 -0
- package/dist/payments/usage.js +168 -0
- package/dist/pricing.d.ts +2 -6
- package/dist/pricing.js +16 -3
- package/dist/router/vision.js +9 -1
- package/dist/stats/tracker.d.ts +9 -1
- package/dist/stats/tracker.js +6 -1
- package/dist/tools/bash.js +17 -7
- package/dist/tools/blockrun.js +1 -1
- package/dist/tools/defillama.js +2 -2
- package/dist/tools/imagegen.js +5 -4
- package/dist/tools/modal.js +12 -6
- package/dist/tools/musicgen.js +4 -3
- package/dist/tools/phone.js +16 -15
- package/dist/tools/prediction.js +12 -11
- package/dist/tools/realface.js +2 -2
- package/dist/tools/rpc.js +2 -2
- package/dist/tools/sensitive-paths.d.ts +1 -1
- package/dist/tools/sensitive-paths.js +13 -4
- package/dist/tools/surf.js +2 -2
- package/dist/tools/videogen.js +6 -5
- package/dist/tools/voice.d.ts +1 -1
- package/dist/tools/voice.js +5 -4
- package/dist/ui/model-picker.d.ts +1 -2
- package/dist/ui/model-picker.js +34 -117
- package/dist/wallet/reservation.d.ts +24 -0
- package/dist/wallet/reservation.js +50 -4
- package/dist/wallet/solana-migration.d.ts +48 -0
- package/dist/wallet/solana-migration.js +81 -0
- package/package.json +6 -4
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { getOrCreateWallet, getOrCreateSolanaWallet, createPaymentPayload, createSolanaPaymentPayload, parsePaymentRequired, extractPaymentDetails, solanaKeyToBytes, SOLANA_NETWORK, } from '@blockrun/llm';
|
|
15
15
|
import { loadChain, USER_AGENT } from '../config.js';
|
|
16
|
-
import {
|
|
16
|
+
import { gatewayHeaders, resolvePayMode, } from './auth-mode.js';
|
|
17
17
|
async function extractPaymentReq(response) {
|
|
18
18
|
let header = response.headers.get('payment-required');
|
|
19
19
|
if (!header) {
|
|
@@ -83,21 +83,14 @@ export async function postWithPayment(endpoint, body, resourceDescription, timeo
|
|
|
83
83
|
body: payload,
|
|
84
84
|
});
|
|
85
85
|
const keyRaw = await keyResponse.text().catch(() => '');
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
let parsedKey = {};
|
|
92
|
-
try {
|
|
93
|
-
parsedKey = keyRaw ? JSON.parse(keyRaw) : {};
|
|
94
|
-
}
|
|
95
|
-
catch { /* leave as {} */ }
|
|
96
|
-
return { ok: keyResponse.ok, status: keyResponse.status, body: parsedKey, raw: keyRaw, settled: false };
|
|
86
|
+
// An account failure is never permission to spend from a wallet.
|
|
87
|
+
// The user can explicitly select --wallet and retry the command.
|
|
88
|
+
let parsedKey = {};
|
|
89
|
+
try {
|
|
90
|
+
parsedKey = keyRaw ? JSON.parse(keyRaw) : {};
|
|
97
91
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
endpoint = toWalletUrl(endpoint, chain);
|
|
92
|
+
catch { /* leave as {} */ }
|
|
93
|
+
return { ok: keyResponse.ok, status: keyResponse.status, body: parsedKey, raw: keyRaw, settled: false };
|
|
101
94
|
}
|
|
102
95
|
const headers = {
|
|
103
96
|
'Content-Type': 'application/json',
|
|
@@ -92,6 +92,15 @@ export declare function basePriceForPath(apiPath: string): number | null;
|
|
|
92
92
|
* in JS, which would book $0 against a billed call.
|
|
93
93
|
*/
|
|
94
94
|
export declare function chargeFromResponse(res: HeaderBag): number | null;
|
|
95
|
+
/**
|
|
96
|
+
* The gateway's id for this request, from `x-blockrun-request-id`.
|
|
97
|
+
*
|
|
98
|
+
* Recorded locally so `franklin usage` can join Franklin's journal to the
|
|
99
|
+
* account ledger row by row. Comparing totals is not the same check: two
|
|
100
|
+
* errors of opposite sign produce a total that matches and a ledger that is
|
|
101
|
+
* wrong in two places.
|
|
102
|
+
*/
|
|
103
|
+
export declare function requestIdFromResponse(res: HeaderBag): string | null;
|
|
95
104
|
/**
|
|
96
105
|
* Credit left on the account after this call, from
|
|
97
106
|
* `x-blockrun-credit-remaining-usd`. Absent on ungated accounts, which have no
|
|
@@ -397,6 +397,19 @@ export function basePriceForPath(apiPath) {
|
|
|
397
397
|
export function chargeFromResponse(res) {
|
|
398
398
|
return usdHeader(res, 'x-blockrun-cost-usd');
|
|
399
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* The gateway's id for this request, from `x-blockrun-request-id`.
|
|
402
|
+
*
|
|
403
|
+
* Recorded locally so `franklin usage` can join Franklin's journal to the
|
|
404
|
+
* account ledger row by row. Comparing totals is not the same check: two
|
|
405
|
+
* errors of opposite sign produce a total that matches and a ledger that is
|
|
406
|
+
* wrong in two places.
|
|
407
|
+
*/
|
|
408
|
+
export function requestIdFromResponse(res) {
|
|
409
|
+
const raw = res.headers?.get?.('x-blockrun-request-id');
|
|
410
|
+
const id = typeof raw === 'string' ? raw.trim() : '';
|
|
411
|
+
return id.length > 0 && id.length <= 128 ? id : null;
|
|
412
|
+
}
|
|
400
413
|
/**
|
|
401
414
|
* Credit left on the account after this call, from
|
|
402
415
|
* `x-blockrun-credit-remaining-usd`. Absent on ungated accounts, which have no
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gateway's own per-request ledger, and how Franklin reconciles to it.
|
|
3
|
+
*
|
|
4
|
+
* Franklin's local numbers in key mode are catalog estimates: the account
|
|
5
|
+
* gateway settles against a prepaid balance and returns no charge on the
|
|
6
|
+
* response, so `franklin stats` reports what a call SHOULD have cost. This
|
|
7
|
+
* module fetches what it DID cost.
|
|
8
|
+
*
|
|
9
|
+
* `GET /v1/usage` returns one row per request with a `request_id` that matches
|
|
10
|
+
* the `x-blockrun-request-id` header the gateway already sends, so the join is
|
|
11
|
+
* line-by-line rather than a comparison of two totals — a total that matches
|
|
12
|
+
* can still be two errors cancelling out.
|
|
13
|
+
*
|
|
14
|
+
* Four properties of this feed change how it must be read, and getting any of
|
|
15
|
+
* them wrong produces a confident wrong answer:
|
|
16
|
+
*
|
|
17
|
+
* `cost_state: 'pending'` means the usage exists and its charge does not yet.
|
|
18
|
+
* Those rows can still be repriced. Summing them as zero understates spend
|
|
19
|
+
* and, worse, looks settled.
|
|
20
|
+
*
|
|
21
|
+
* Zero-cost rows are INCLUDED on purpose. "You were not charged for this" is
|
|
22
|
+
* an answer; a row that is simply absent is indistinguishable from one the
|
|
23
|
+
* client dropped. Never filter them.
|
|
24
|
+
*
|
|
25
|
+
* `unavailable_days` names days the gateway could not list rather than
|
|
26
|
+
* silently returning a short page. Swallowing it makes two correct ledgers
|
|
27
|
+
* look like they disagree.
|
|
28
|
+
*
|
|
29
|
+
* `kind` says whether a row is checkable locally at all. A `chat` charge can
|
|
30
|
+
* be rebuilt from tokens and a published rate; a `service` charge is a
|
|
31
|
+
* per-call figure only the gateway holds, so a local estimate for one is a
|
|
32
|
+
* guess by construction and a mismatch is not evidence of a bug.
|
|
33
|
+
*
|
|
34
|
+
* The cursor is opaque. Do not parse it, and do not construct one.
|
|
35
|
+
*/
|
|
36
|
+
export type CostState = 'priced' | 'pending' | 'free';
|
|
37
|
+
export type UsageKind = 'chat' | 'service';
|
|
38
|
+
export interface UsageRow {
|
|
39
|
+
requestId: string;
|
|
40
|
+
timestamp: string;
|
|
41
|
+
endpoint: string;
|
|
42
|
+
model: string | null;
|
|
43
|
+
kind: UsageKind;
|
|
44
|
+
inputTokens: number;
|
|
45
|
+
outputTokens: number;
|
|
46
|
+
costUsd: number;
|
|
47
|
+
costState: CostState;
|
|
48
|
+
status: number;
|
|
49
|
+
}
|
|
50
|
+
export interface UsagePage {
|
|
51
|
+
rows: UsageRow[];
|
|
52
|
+
/** Days the gateway could not list. Surface these; never treat as empty. */
|
|
53
|
+
unavailableDays: string[];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Fetch usage rows, following the cursor. Returns null when there is no key or
|
|
57
|
+
* the gateway is unreachable — callers show what they do know rather than
|
|
58
|
+
* inventing a ledger.
|
|
59
|
+
*/
|
|
60
|
+
export declare function fetchUsage(opts?: {
|
|
61
|
+
from?: string;
|
|
62
|
+
to?: string;
|
|
63
|
+
limit?: number;
|
|
64
|
+
timeoutMs?: number;
|
|
65
|
+
}): Promise<UsagePage | null>;
|
|
66
|
+
export interface UsageTotals {
|
|
67
|
+
/** Rows whose charge has settled. This is the number you can trust. */
|
|
68
|
+
pricedUsd: number;
|
|
69
|
+
pricedCount: number;
|
|
70
|
+
/** Usage that exists with no charge yet. Not a zero — it can still be priced. */
|
|
71
|
+
pendingCount: number;
|
|
72
|
+
/** Explicitly not chargeable. Counted, never hidden. */
|
|
73
|
+
freeCount: number;
|
|
74
|
+
chatCount: number;
|
|
75
|
+
serviceCount: number;
|
|
76
|
+
}
|
|
77
|
+
export declare function summarize(rows: readonly UsageRow[]): UsageTotals;
|
|
78
|
+
export interface Reconciliation {
|
|
79
|
+
/** Ledger rows Franklin also has locally, and whether the numbers agree. */
|
|
80
|
+
matched: Array<{
|
|
81
|
+
row: UsageRow;
|
|
82
|
+
localUsd: number;
|
|
83
|
+
deltaUsd: number;
|
|
84
|
+
}>;
|
|
85
|
+
/** Charged by the gateway with no local row. Franklin under-counted. */
|
|
86
|
+
missingLocally: UsageRow[];
|
|
87
|
+
/** Rows Franklin recorded that carry no id to join on. */
|
|
88
|
+
unjoinable: number;
|
|
89
|
+
totals: UsageTotals;
|
|
90
|
+
unavailableDays: string[];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Join the gateway ledger to Franklin's own journal on `request_id`.
|
|
94
|
+
*
|
|
95
|
+
* Only `priced` rows are compared. A `pending` row has no charge to disagree
|
|
96
|
+
* with yet, and a `free` row's zero is an answer rather than a discrepancy.
|
|
97
|
+
*
|
|
98
|
+
* `unjoinable` counts local rows with no id — wallet-mode calls, free-path
|
|
99
|
+
* calls, and anything recorded before the id was captured. They are reported
|
|
100
|
+
* rather than hidden, because "0 mismatches" from a journal that could not be
|
|
101
|
+
* joined is not the same statement as "0 mismatches".
|
|
102
|
+
*/
|
|
103
|
+
export declare function reconcile(ledger: UsagePage, local: ReadonlyArray<{
|
|
104
|
+
requestId?: string;
|
|
105
|
+
costUsd: number;
|
|
106
|
+
}>): Reconciliation;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gateway's own per-request ledger, and how Franklin reconciles to it.
|
|
3
|
+
*
|
|
4
|
+
* Franklin's local numbers in key mode are catalog estimates: the account
|
|
5
|
+
* gateway settles against a prepaid balance and returns no charge on the
|
|
6
|
+
* response, so `franklin stats` reports what a call SHOULD have cost. This
|
|
7
|
+
* module fetches what it DID cost.
|
|
8
|
+
*
|
|
9
|
+
* `GET /v1/usage` returns one row per request with a `request_id` that matches
|
|
10
|
+
* the `x-blockrun-request-id` header the gateway already sends, so the join is
|
|
11
|
+
* line-by-line rather than a comparison of two totals — a total that matches
|
|
12
|
+
* can still be two errors cancelling out.
|
|
13
|
+
*
|
|
14
|
+
* Four properties of this feed change how it must be read, and getting any of
|
|
15
|
+
* them wrong produces a confident wrong answer:
|
|
16
|
+
*
|
|
17
|
+
* `cost_state: 'pending'` means the usage exists and its charge does not yet.
|
|
18
|
+
* Those rows can still be repriced. Summing them as zero understates spend
|
|
19
|
+
* and, worse, looks settled.
|
|
20
|
+
*
|
|
21
|
+
* Zero-cost rows are INCLUDED on purpose. "You were not charged for this" is
|
|
22
|
+
* an answer; a row that is simply absent is indistinguishable from one the
|
|
23
|
+
* client dropped. Never filter them.
|
|
24
|
+
*
|
|
25
|
+
* `unavailable_days` names days the gateway could not list rather than
|
|
26
|
+
* silently returning a short page. Swallowing it makes two correct ledgers
|
|
27
|
+
* look like they disagree.
|
|
28
|
+
*
|
|
29
|
+
* `kind` says whether a row is checkable locally at all. A `chat` charge can
|
|
30
|
+
* be rebuilt from tokens and a published rate; a `service` charge is a
|
|
31
|
+
* per-call figure only the gateway holds, so a local estimate for one is a
|
|
32
|
+
* guess by construction and a mismatch is not evidence of a bug.
|
|
33
|
+
*
|
|
34
|
+
* The cursor is opaque. Do not parse it, and do not construct one.
|
|
35
|
+
*/
|
|
36
|
+
import { KEY_API_URL, USER_AGENT } from '../config.js';
|
|
37
|
+
import { loadApiKey } from './auth-mode.js';
|
|
38
|
+
/** Max rows the endpoint will return per page. */
|
|
39
|
+
const MAX_LIMIT = 500;
|
|
40
|
+
/** Stop paging rather than walk an unbounded history. */
|
|
41
|
+
const MAX_PAGES = 40;
|
|
42
|
+
function toRow(raw) {
|
|
43
|
+
const requestId = typeof raw.request_id === 'string' ? raw.request_id : '';
|
|
44
|
+
if (!requestId)
|
|
45
|
+
return null; // without the join key the row is not usable here
|
|
46
|
+
const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : 0);
|
|
47
|
+
const state = raw.cost_state;
|
|
48
|
+
return {
|
|
49
|
+
requestId,
|
|
50
|
+
timestamp: typeof raw.timestamp === 'string' ? raw.timestamp : '',
|
|
51
|
+
endpoint: typeof raw.endpoint === 'string' ? raw.endpoint : '',
|
|
52
|
+
model: typeof raw.model === 'string' ? raw.model : null,
|
|
53
|
+
kind: raw.kind === 'chat' ? 'chat' : 'service',
|
|
54
|
+
inputTokens: num(raw.input_tokens),
|
|
55
|
+
outputTokens: num(raw.output_tokens),
|
|
56
|
+
costUsd: num(raw.cost_usd),
|
|
57
|
+
// An unrecognised state is treated as pending, not priced: the safe error
|
|
58
|
+
// is "we do not know yet", never "settled at whatever number came through".
|
|
59
|
+
costState: state === 'priced' || state === 'free' ? state : 'pending',
|
|
60
|
+
status: num(raw.status),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Fetch usage rows, following the cursor. Returns null when there is no key or
|
|
65
|
+
* the gateway is unreachable — callers show what they do know rather than
|
|
66
|
+
* inventing a ledger.
|
|
67
|
+
*/
|
|
68
|
+
export async function fetchUsage(opts = {}) {
|
|
69
|
+
const key = loadApiKey();
|
|
70
|
+
if (!key)
|
|
71
|
+
return null;
|
|
72
|
+
const rows = [];
|
|
73
|
+
const unavailableDays = new Set();
|
|
74
|
+
let cursor = null;
|
|
75
|
+
for (let page = 0; page < MAX_PAGES; page++) {
|
|
76
|
+
const url = new URL(`${KEY_API_URL}/v1/usage`);
|
|
77
|
+
if (opts.from)
|
|
78
|
+
url.searchParams.set('from', opts.from);
|
|
79
|
+
if (opts.to)
|
|
80
|
+
url.searchParams.set('to', opts.to);
|
|
81
|
+
url.searchParams.set('limit', String(Math.min(opts.limit ?? MAX_LIMIT, MAX_LIMIT)));
|
|
82
|
+
if (cursor)
|
|
83
|
+
url.searchParams.set('cursor', cursor);
|
|
84
|
+
let body;
|
|
85
|
+
try {
|
|
86
|
+
const res = await fetch(url, {
|
|
87
|
+
headers: { Authorization: `Bearer ${key}`, 'User-Agent': USER_AGENT },
|
|
88
|
+
signal: AbortSignal.timeout(opts.timeoutMs ?? 20_000),
|
|
89
|
+
});
|
|
90
|
+
if (!res.ok)
|
|
91
|
+
return null;
|
|
92
|
+
body = (await res.json());
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
for (const raw of Array.isArray(body.data) ? body.data : []) {
|
|
98
|
+
const row = toRow(raw);
|
|
99
|
+
if (row)
|
|
100
|
+
rows.push(row);
|
|
101
|
+
}
|
|
102
|
+
for (const d of Array.isArray(body.unavailable_days) ? body.unavailable_days : []) {
|
|
103
|
+
if (typeof d === 'string')
|
|
104
|
+
unavailableDays.add(d);
|
|
105
|
+
}
|
|
106
|
+
cursor = typeof body.next_cursor === 'string' && body.next_cursor ? body.next_cursor : null;
|
|
107
|
+
if (!cursor)
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
return { rows, unavailableDays: [...unavailableDays] };
|
|
111
|
+
}
|
|
112
|
+
export function summarize(rows) {
|
|
113
|
+
const t = {
|
|
114
|
+
pricedUsd: 0, pricedCount: 0, pendingCount: 0, freeCount: 0, chatCount: 0, serviceCount: 0,
|
|
115
|
+
};
|
|
116
|
+
for (const r of rows) {
|
|
117
|
+
if (r.costState === 'priced') {
|
|
118
|
+
t.pricedUsd += r.costUsd;
|
|
119
|
+
t.pricedCount++;
|
|
120
|
+
}
|
|
121
|
+
else if (r.costState === 'pending')
|
|
122
|
+
t.pendingCount++;
|
|
123
|
+
else
|
|
124
|
+
t.freeCount++;
|
|
125
|
+
if (r.kind === 'chat')
|
|
126
|
+
t.chatCount++;
|
|
127
|
+
else
|
|
128
|
+
t.serviceCount++;
|
|
129
|
+
}
|
|
130
|
+
return t;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Join the gateway ledger to Franklin's own journal on `request_id`.
|
|
134
|
+
*
|
|
135
|
+
* Only `priced` rows are compared. A `pending` row has no charge to disagree
|
|
136
|
+
* with yet, and a `free` row's zero is an answer rather than a discrepancy.
|
|
137
|
+
*
|
|
138
|
+
* `unjoinable` counts local rows with no id — wallet-mode calls, free-path
|
|
139
|
+
* calls, and anything recorded before the id was captured. They are reported
|
|
140
|
+
* rather than hidden, because "0 mismatches" from a journal that could not be
|
|
141
|
+
* joined is not the same statement as "0 mismatches".
|
|
142
|
+
*/
|
|
143
|
+
export function reconcile(ledger, local) {
|
|
144
|
+
const byId = new Map();
|
|
145
|
+
let unjoinable = 0;
|
|
146
|
+
for (const r of local) {
|
|
147
|
+
if (r.requestId)
|
|
148
|
+
byId.set(r.requestId, (byId.get(r.requestId) ?? 0) + r.costUsd);
|
|
149
|
+
else
|
|
150
|
+
unjoinable++;
|
|
151
|
+
}
|
|
152
|
+
const matched = [];
|
|
153
|
+
const missingLocally = [];
|
|
154
|
+
for (const row of ledger.rows) {
|
|
155
|
+
if (row.costState !== 'priced')
|
|
156
|
+
continue;
|
|
157
|
+
const localUsd = byId.get(row.requestId);
|
|
158
|
+
if (localUsd === undefined) {
|
|
159
|
+
// A charge with no local row: real spend Franklin never counted, so it
|
|
160
|
+
// never reached --max-spend either.
|
|
161
|
+
if (row.costUsd > 0)
|
|
162
|
+
missingLocally.push(row);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
matched.push({ row, localUsd, deltaUsd: localUsd - row.costUsd });
|
|
166
|
+
}
|
|
167
|
+
return { matched, missingLocally, unjoinable, totals: summarize(ledger.rows), unavailableDays: ledger.unavailableDays };
|
|
168
|
+
}
|
package/dist/pricing.d.ts
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* Used by agent loop, proxy server, stats tracker, and router.
|
|
4
|
-
*/
|
|
1
|
+
export declare function hasHistoricalPrice(model: string): boolean;
|
|
2
|
+
/** Gateway prices override historical fallbacks without replacing map identities. */
|
|
5
3
|
export declare const MODEL_PRICING: Record<string, {
|
|
6
4
|
input: number;
|
|
7
5
|
output: number;
|
|
8
6
|
perCall?: number;
|
|
9
7
|
}>;
|
|
10
|
-
/** Opus pricing for savings calculations — tracks the current flagship. */
|
|
11
8
|
export declare const OPUS_PRICING: {
|
|
12
9
|
input: number;
|
|
13
10
|
output: number;
|
|
14
|
-
perCall?: number;
|
|
15
11
|
};
|
|
16
12
|
/**
|
|
17
13
|
* Estimate cost in USD for a request.
|
package/dist/pricing.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { getModelCatalog, onModelCatalogChange } from './model-catalog.js';
|
|
1
2
|
/**
|
|
2
3
|
* Single source of truth for model pricing (per 1M tokens).
|
|
3
4
|
* Used by agent loop, proxy server, stats tracker, and router.
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
|
+
const LEGACY_PRICING = {
|
|
6
7
|
// Routing profiles (blended averages). Auto + Free are the only profiles
|
|
7
8
|
// surfaced after the 2026-05-03 collapse; eco/premium were retired and
|
|
8
9
|
// their parser mapping promotes them to Auto upstream of cost estimation.
|
|
@@ -177,8 +178,20 @@ export const MODEL_PRICING = {
|
|
|
177
178
|
'nvidia/kimi-k2.5': { input: 0.55, output: 2.5 },
|
|
178
179
|
'zai/glm-5.1-turbo': { input: 1.2, output: 4.0 }, // was a client alias for zai/glm-5-turbo
|
|
179
180
|
};
|
|
180
|
-
|
|
181
|
-
|
|
181
|
+
export function hasHistoricalPrice(model) { return model in LEGACY_PRICING; }
|
|
182
|
+
/** Gateway prices override historical fallbacks without replacing map identities. */
|
|
183
|
+
export const MODEL_PRICING = {};
|
|
184
|
+
export const OPUS_PRICING = { input: 5, output: 25 };
|
|
185
|
+
function updatePrices(state) {
|
|
186
|
+
for (const key of Object.keys(MODEL_PRICING))
|
|
187
|
+
delete MODEL_PRICING[key];
|
|
188
|
+
Object.assign(MODEL_PRICING, LEGACY_PRICING, state.pricing);
|
|
189
|
+
const opus = MODEL_PRICING['anthropic/claude-opus-5'];
|
|
190
|
+
if (opus)
|
|
191
|
+
Object.assign(OPUS_PRICING, opus);
|
|
192
|
+
}
|
|
193
|
+
updatePrices(getModelCatalog());
|
|
194
|
+
onModelCatalogChange(updatePrices);
|
|
182
195
|
/**
|
|
183
196
|
* Estimate cost in USD for a request.
|
|
184
197
|
* Falls back to $2/$10 per 1M for unknown models.
|
package/dist/router/vision.js
CHANGED
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
* model — having no vision pathway — would hallucinate based on the
|
|
19
19
|
* `Image file: <path>` description string. Expensive AND wrong.
|
|
20
20
|
*/
|
|
21
|
+
import { hasHistoricalPrice } from '../pricing.js';
|
|
22
|
+
import { getModelCatalog } from '../model-catalog.js';
|
|
21
23
|
import { freeVisionModel } from '../free-models.js';
|
|
22
24
|
const VISION_MODELS = new Set([
|
|
23
25
|
// Anthropic — native vision across the line
|
|
@@ -120,7 +122,13 @@ export function isVisionModel(modelId) {
|
|
|
120
122
|
// on gateway deploy state and on FRANKLIN_FREE_VISION_MODEL. freeVisionModel()
|
|
121
123
|
// returns null when no free model can be trusted with an image, and this
|
|
122
124
|
// stays false there.
|
|
123
|
-
|
|
125
|
+
if (modelId === freeVisionModel())
|
|
126
|
+
return true;
|
|
127
|
+
// Preserve vetted exclusions for existing models; use metadata for new IDs.
|
|
128
|
+
if (hasHistoricalPrice(modelId))
|
|
129
|
+
return false;
|
|
130
|
+
const model = getModelCatalog().models.find(entry => entry.id === modelId);
|
|
131
|
+
return model?.billing_mode !== 'free' && model?.categories.includes('vision') === true;
|
|
124
132
|
}
|
|
125
133
|
/** Lower-cased copy used for prefix family matching in pickVisionSibling. */
|
|
126
134
|
const VISION_MODELS_LIST = Array.from(VISION_MODELS);
|
package/dist/stats/tracker.d.ts
CHANGED
|
@@ -11,6 +11,12 @@ export interface UsageRecord {
|
|
|
11
11
|
costUsd: number;
|
|
12
12
|
latencyMs: number;
|
|
13
13
|
fallback?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* The gateway's `x-blockrun-request-id` for this call, when it sent one.
|
|
16
|
+
* The join key `franklin usage` uses to line this row up against the
|
|
17
|
+
* account ledger. Absent on wallet-mode and free-path calls.
|
|
18
|
+
*/
|
|
19
|
+
requestId?: string;
|
|
14
20
|
}
|
|
15
21
|
export interface ModelStats {
|
|
16
22
|
requests: number;
|
|
@@ -60,7 +66,9 @@ export declare function recordUsage(model: string, inputTokens: number, outputTo
|
|
|
60
66
|
* key-mode row is priced locally. Surfaced by `franklin stats` so the ledger
|
|
61
67
|
* never claims more precision than it has.
|
|
62
68
|
*/
|
|
63
|
-
estimated?: boolean
|
|
69
|
+
estimated?: boolean,
|
|
70
|
+
/** Gateway request id, from `requestIdFromResponse`. Enables reconciliation. */
|
|
71
|
+
requestId?: string | null): void;
|
|
64
72
|
/**
|
|
65
73
|
* Get stats summary for display
|
|
66
74
|
*/
|
package/dist/stats/tracker.js
CHANGED
|
@@ -200,7 +200,9 @@ export function recordUsage(model, inputTokens, outputTokens, costUsd, latencyMs
|
|
|
200
200
|
* key-mode row is priced locally. Surfaced by `franklin stats` so the ledger
|
|
201
201
|
* never claims more precision than it has.
|
|
202
202
|
*/
|
|
203
|
-
estimated = isKeyMode()
|
|
203
|
+
estimated = isKeyMode(),
|
|
204
|
+
/** Gateway request id, from `requestIdFromResponse`. Enables reconciliation. */
|
|
205
|
+
requestId) {
|
|
204
206
|
// Count real spend BEFORE the test/audit gates — the --max-spend ceiling must
|
|
205
207
|
// see every paid tool call even when history persistence is suppressed.
|
|
206
208
|
if (Number.isFinite(costUsd) && costUsd > 0)
|
|
@@ -262,6 +264,9 @@ estimated = isKeyMode()) {
|
|
|
262
264
|
costUsd,
|
|
263
265
|
latencyMs,
|
|
264
266
|
fallback,
|
|
267
|
+
// Omit rather than store undefined: history is JSON round-tripped, and an
|
|
268
|
+
// explicit `requestId: undefined` survives as a key with no value.
|
|
269
|
+
...(requestId ? { requestId } : {}),
|
|
265
270
|
});
|
|
266
271
|
scheduleSave();
|
|
267
272
|
}
|
package/dist/tools/bash.js
CHANGED
|
@@ -289,15 +289,25 @@ function executeCommand(command, timeoutMs, ctx) {
|
|
|
289
289
|
const shell = fs.existsSync('/bin/bash') ? '/bin/bash' : (process.env.SHELL || '/bin/sh');
|
|
290
290
|
let child;
|
|
291
291
|
try {
|
|
292
|
+
// The account bearer key is deliberately withheld from the subprocess.
|
|
293
|
+
// bash-guard auto-approves `printenv` and `echo` as safe, so
|
|
294
|
+
// `printenv BLOCKRUN_API_KEY` ran with no confirmation and returned the
|
|
295
|
+
// key as tool output — into the model prompt and the saved transcript.
|
|
296
|
+
// Wallet private keys are never in the environment at all; this gives
|
|
297
|
+
// the key the same treatment. Franklin's own gateway calls read
|
|
298
|
+
// process.env in-process and are unaffected, and a user who genuinely
|
|
299
|
+
// needs the key in a shell can export it there themselves.
|
|
300
|
+
const childEnv = {
|
|
301
|
+
...process.env,
|
|
302
|
+
FRANKLIN: '1', // Let scripts detect they're running inside Franklin
|
|
303
|
+
FRANKLIN_WORKDIR: ctx.workingDir,
|
|
304
|
+
RUNCODE: '1', // Backwards compat
|
|
305
|
+
RUNCODE_WORKDIR: ctx.workingDir,
|
|
306
|
+
};
|
|
307
|
+
delete childEnv.BLOCKRUN_API_KEY;
|
|
292
308
|
child = spawn(shell, ['-c', command], {
|
|
293
309
|
cwd: ctx.workingDir,
|
|
294
|
-
env:
|
|
295
|
-
...process.env,
|
|
296
|
-
FRANKLIN: '1', // Let scripts detect they're running inside Franklin
|
|
297
|
-
FRANKLIN_WORKDIR: ctx.workingDir,
|
|
298
|
-
RUNCODE: '1', // Backwards compat
|
|
299
|
-
RUNCODE_WORKDIR: ctx.workingDir,
|
|
300
|
-
},
|
|
310
|
+
env: childEnv,
|
|
301
311
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
302
312
|
// Put the shell in its own process group (pgid = pid) so a timeout
|
|
303
313
|
// can SIGTERM the entire tree. Without this, signalling only the
|
package/dist/tools/blockrun.js
CHANGED
|
@@ -176,7 +176,7 @@ function fmtUsd(n) {
|
|
|
176
176
|
export const blockrunCapability = {
|
|
177
177
|
spec: {
|
|
178
178
|
name: 'BlockRun',
|
|
179
|
-
description: 'Call any BlockRun gateway endpoint.
|
|
179
|
+
description: 'Call any BlockRun gateway endpoint. Billed to account credits or, in wallet mode, by signing an x402 USDC payment and retrying on HTTP 402. ' +
|
|
180
180
|
'Use this for crypto data (Surf — markets, on-chain, social), AI inference (chat / image / video / music), prediction markets, DeFi data, and any other API exposed under https://blockrun.ai/marketplace. ' +
|
|
181
181
|
'For phone and voice, prefer the typed tools (ListPhoneNumbers, BuyPhoneNumber, RenewPhoneNumber, ReleasePhoneNumber, PhoneLookup, PhoneFraudCheck, VoiceCall, VoiceStatus) — they spell out cost, required fields, and the buy-number-first requirement. ' +
|
|
182
182
|
'The path must start with "/v1/" or "/.well-known/". ' +
|
package/dist/tools/defillama.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
import { getOrCreateWallet, getOrCreateSolanaWallet, createPaymentPayload, createSolanaPaymentPayload, parsePaymentRequired, extractPaymentDetails, solanaKeyToBytes, SOLANA_NETWORK, } from '@blockrun/llm';
|
|
19
19
|
import { loadChain, VERSION } from '../config.js';
|
|
20
|
-
import { chargeFromResponse, resolveCharge } from '../payments/price-catalog.js';
|
|
20
|
+
import { chargeFromResponse, requestIdFromResponse, resolveCharge } from '../payments/price-catalog.js';
|
|
21
21
|
import { gatewayBase, gatewayHeaders } from '../payments/auth-mode.js';
|
|
22
22
|
import { logger } from '../logger.js';
|
|
23
23
|
import { recordUsage } from '../stats/tracker.js';
|
|
@@ -67,7 +67,7 @@ async function getWithPayment(path, ctx) {
|
|
|
67
67
|
// call in API-key mode.
|
|
68
68
|
const charge = resolveCharge({ apiPath: endpoint, chargedUsd: chargeFromResponse(response), settledUsd: paidUsd });
|
|
69
69
|
try {
|
|
70
|
-
recordUsage(`DeFiLlama:${path}`, 0, 0, charge.usd, Date.now() - startedAt, false, charge.estimated);
|
|
70
|
+
recordUsage(`DeFiLlama:${path}`, 0, 0, charge.usd, Date.now() - startedAt, false, charge.estimated, requestIdFromResponse(response));
|
|
71
71
|
}
|
|
72
72
|
catch { /* best-effort */ }
|
|
73
73
|
return (await response.json());
|
package/dist/tools/imagegen.js
CHANGED
|
@@ -14,6 +14,7 @@ import { findModel, estimateCostUsd, GATEWAY_MARGIN } from '../gateway-models.js
|
|
|
14
14
|
import { logger } from '../logger.js';
|
|
15
15
|
import { ssrfSafeFetch } from './ssrf.js';
|
|
16
16
|
import { isWalletKeyPath } from './sensitive-paths.js';
|
|
17
|
+
import { noChargeNote, cancelHint } from '../payments/billing-copy.js';
|
|
17
18
|
/**
|
|
18
19
|
* Models that accept a reference image via /v1/images/image2image. Mirrors the
|
|
19
20
|
* gateway's EDIT_SUPPORTED_MODELS (src/app/api/v1/images/image2image/route.ts):
|
|
@@ -253,7 +254,7 @@ function buildExecute(deps) {
|
|
|
253
254
|
return {
|
|
254
255
|
output: `## Image generation skipped\n` +
|
|
255
256
|
`- ${decision.reason}\n\n` +
|
|
256
|
-
|
|
257
|
+
`${noChargeNote()} Choose a cheaper model/size or raise the ` +
|
|
257
258
|
`content budget before trying again.`,
|
|
258
259
|
};
|
|
259
260
|
}
|
|
@@ -267,9 +268,9 @@ function buildExecute(deps) {
|
|
|
267
268
|
const est = perImageUsd * n;
|
|
268
269
|
const priceNote = est > 0 ? ` for ~$${est.toFixed(2)}` : '';
|
|
269
270
|
const countNote = n > 1 ? `${n} images` : 'an image';
|
|
270
|
-
const answer = await ctx.onAskUser(`Generate ${countNote} with ${imageModel}${priceNote}?
|
|
271
|
+
const answer = await ctx.onAskUser(`Generate ${countNote} with ${imageModel}${priceNote}? ${cancelHint()}`, ['Generate', 'Cancel']);
|
|
271
272
|
if (answer !== 'Generate') {
|
|
272
|
-
return { output: `## Image generation cancelled\n\
|
|
273
|
+
return { output: `## Image generation cancelled\n\n${noChargeNote()}` };
|
|
273
274
|
}
|
|
274
275
|
}
|
|
275
276
|
// Resolve all reference images + the mask into base64 data URIs now, right
|
|
@@ -648,7 +649,7 @@ export function createImageGenCapability(deps = {}) {
|
|
|
648
649
|
description: "Generate or edit an image. Text-to-image from a prompt, or " +
|
|
649
650
|
"image-to-image when you pass a reference image (style transfer, " +
|
|
650
651
|
"character consistency, edits). Supports mask-based inpainting and " +
|
|
651
|
-
"multi-image fusion.
|
|
652
|
+
"multi-image fusion. Billed per image — confirm " +
|
|
652
653
|
"before generating. Saves to local file(s). Default size: 1024x1024. " +
|
|
653
654
|
"Do NOT call repeatedly to iterate on style — ask the user first. " +
|
|
654
655
|
"Pass contentId to attach the result to an existing Content piece: " +
|
package/dist/tools/modal.js
CHANGED
|
@@ -26,11 +26,12 @@
|
|
|
26
26
|
* the SessionSandboxTracker registry.
|
|
27
27
|
*/
|
|
28
28
|
import { getOrCreateWallet, getOrCreateSolanaWallet, createPaymentPayload, createSolanaPaymentPayload, parsePaymentRequired, extractPaymentDetails, solanaKeyToBytes, SOLANA_NETWORK, } from '@blockrun/llm';
|
|
29
|
-
import { loadChain, VERSION } from '../config.js';
|
|
30
|
-
import { gatewayBase, gatewayHeaders } from '../payments/auth-mode.js';
|
|
29
|
+
import { loadChain, VERSION, DASHBOARD_URL } from '../config.js';
|
|
30
|
+
import { gatewayBase, gatewayHeaders, isKeyMode } from '../payments/auth-mode.js';
|
|
31
31
|
import { walletReservation, AMBIGUOUS_GRACE_MS } from '../wallet/reservation.js';
|
|
32
32
|
import { recordUsage } from '../stats/tracker.js';
|
|
33
33
|
import { logger } from '../logger.js';
|
|
34
|
+
import { noChargeNote } from '../payments/billing-copy.js';
|
|
34
35
|
// ─── Pricing table (probed from /.well-known/x402 + 402 responses) ─────────
|
|
35
36
|
const CREATE_PRICE_USD = {
|
|
36
37
|
cpu: 0.01,
|
|
@@ -214,9 +215,14 @@ function describeHeadroom() {
|
|
|
214
215
|
parts.push(`${fmtUsd(snap.ambiguousUsd)} is held for ${snap.ambiguousCount} recent payment(s) whose settlement is ` +
|
|
215
216
|
`unconfirmed (released automatically after the on-chain balance refreshes)`);
|
|
216
217
|
}
|
|
218
|
+
// In key mode there is no wallet to fund — the ceiling is the prepaid
|
|
219
|
+
// balance, so send the user to the right place.
|
|
220
|
+
const topUp = isKeyMode()
|
|
221
|
+
? `Top up account credits at ${DASHBOARD_URL}.`
|
|
222
|
+
: 'Fund the wallet.';
|
|
217
223
|
if (parts.length === 0)
|
|
218
|
-
return
|
|
219
|
-
return parts.join('; ') +
|
|
224
|
+
return topUp;
|
|
225
|
+
return parts.join('; ') + ` — wait for those to clear, or ${topUp[0].toLowerCase()}${topUp.slice(1)}`;
|
|
220
226
|
}
|
|
221
227
|
function signalError(signal) {
|
|
222
228
|
const reason = signal.reason;
|
|
@@ -373,7 +379,7 @@ export const modalCreateCapability = {
|
|
|
373
379
|
try {
|
|
374
380
|
const answer = await ctx.onAskUser(lines.join('\n'), ['Approve', 'Cancel']);
|
|
375
381
|
if (answer !== 'Approve') {
|
|
376
|
-
return { output:
|
|
382
|
+
return { output: `## Sandbox creation cancelled\n\n${noChargeNote()}` };
|
|
377
383
|
}
|
|
378
384
|
}
|
|
379
385
|
catch {
|
|
@@ -387,7 +393,7 @@ export const modalCreateCapability = {
|
|
|
387
393
|
reservation = await walletReservation.hold(price);
|
|
388
394
|
if (!reservation) {
|
|
389
395
|
return {
|
|
390
|
-
output: `Insufficient USDC for ModalCreate (${tier}, ~${fmtUsd(price)}). ` +
|
|
396
|
+
output: `Insufficient ${isKeyMode() ? 'account credit' : 'USDC'} for ModalCreate (${tier}, ~${fmtUsd(price)}). ` +
|
|
391
397
|
describeHeadroom(),
|
|
392
398
|
isError: true,
|
|
393
399
|
};
|