@blockrun/llm 1.7.0 → 1.8.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 +66 -1
- package/dist/index.cjs +561 -12
- package/dist/index.d.cts +211 -1
- package/dist/index.d.ts +211 -1
- package/dist/index.js +557 -11
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1143,7 +1143,10 @@ var LLMClient = class _LLMClient {
|
|
|
1143
1143
|
contextWindow: m.contextWindow ?? m.context_window ?? 0,
|
|
1144
1144
|
maxOutput: m.maxOutput ?? m.max_output ?? 0,
|
|
1145
1145
|
categories: m.categories || [],
|
|
1146
|
-
available: true
|
|
1146
|
+
available: true,
|
|
1147
|
+
billingMode: m.billingMode ?? m.billing_mode,
|
|
1148
|
+
flatPrice: m.flatPrice ?? m.flat_price ?? m.pricing?.flat,
|
|
1149
|
+
hidden: m.hidden
|
|
1147
1150
|
}));
|
|
1148
1151
|
}
|
|
1149
1152
|
/**
|
|
@@ -1968,8 +1971,548 @@ var MusicClient = class {
|
|
|
1968
1971
|
}
|
|
1969
1972
|
};
|
|
1970
1973
|
|
|
1974
|
+
// src/search.ts
|
|
1975
|
+
import { privateKeyToAccount as privateKeyToAccount4 } from "viem/accounts";
|
|
1976
|
+
var DEFAULT_API_URL4 = "https://blockrun.ai/api";
|
|
1977
|
+
var DEFAULT_TIMEOUT4 = 6e4;
|
|
1978
|
+
var SearchClient = class {
|
|
1979
|
+
account;
|
|
1980
|
+
privateKey;
|
|
1981
|
+
apiUrl;
|
|
1982
|
+
timeout;
|
|
1983
|
+
constructor(options = {}) {
|
|
1984
|
+
const envKey = typeof process !== "undefined" && process.env ? process.env.BLOCKRUN_WALLET_KEY || process.env.BASE_CHAIN_WALLET_KEY : void 0;
|
|
1985
|
+
const privateKey = options.privateKey || envKey;
|
|
1986
|
+
if (!privateKey) {
|
|
1987
|
+
throw new Error(
|
|
1988
|
+
"Private key required. Pass privateKey in options or set BLOCKRUN_WALLET_KEY environment variable."
|
|
1989
|
+
);
|
|
1990
|
+
}
|
|
1991
|
+
validatePrivateKey(privateKey);
|
|
1992
|
+
this.privateKey = privateKey;
|
|
1993
|
+
this.account = privateKeyToAccount4(privateKey);
|
|
1994
|
+
const apiUrl = options.apiUrl || DEFAULT_API_URL4;
|
|
1995
|
+
validateApiUrl(apiUrl);
|
|
1996
|
+
this.apiUrl = apiUrl.replace(/\/$/, "");
|
|
1997
|
+
this.timeout = options.timeout || DEFAULT_TIMEOUT4;
|
|
1998
|
+
}
|
|
1999
|
+
async search(query, options) {
|
|
2000
|
+
if (!query || query.length > 1e3) {
|
|
2001
|
+
throw new Error("query must be 1-1000 characters");
|
|
2002
|
+
}
|
|
2003
|
+
const maxResults = options?.maxResults ?? 10;
|
|
2004
|
+
if (maxResults < 1 || maxResults > 50) {
|
|
2005
|
+
throw new Error("maxResults must be between 1 and 50");
|
|
2006
|
+
}
|
|
2007
|
+
const body = {
|
|
2008
|
+
query,
|
|
2009
|
+
max_results: maxResults
|
|
2010
|
+
};
|
|
2011
|
+
if (options?.sources) body.sources = options.sources;
|
|
2012
|
+
if (options?.fromDate) body.from_date = options.fromDate;
|
|
2013
|
+
if (options?.toDate) body.to_date = options.toDate;
|
|
2014
|
+
return this.requestWithPayment("/v1/search", body);
|
|
2015
|
+
}
|
|
2016
|
+
async requestWithPayment(endpoint, body) {
|
|
2017
|
+
const url = `${this.apiUrl}${endpoint}`;
|
|
2018
|
+
const response = await this.fetchWithTimeout(url, {
|
|
2019
|
+
method: "POST",
|
|
2020
|
+
headers: { "Content-Type": "application/json" },
|
|
2021
|
+
body: JSON.stringify(body)
|
|
2022
|
+
});
|
|
2023
|
+
if (response.status === 402) {
|
|
2024
|
+
return this.handlePaymentAndRetry(url, body, response);
|
|
2025
|
+
}
|
|
2026
|
+
if (!response.ok) {
|
|
2027
|
+
let errorBody;
|
|
2028
|
+
try {
|
|
2029
|
+
errorBody = await response.json();
|
|
2030
|
+
} catch {
|
|
2031
|
+
errorBody = { error: "Request failed" };
|
|
2032
|
+
}
|
|
2033
|
+
throw new APIError(
|
|
2034
|
+
`API error: ${response.status}`,
|
|
2035
|
+
response.status,
|
|
2036
|
+
sanitizeErrorResponse(errorBody)
|
|
2037
|
+
);
|
|
2038
|
+
}
|
|
2039
|
+
return response.json();
|
|
2040
|
+
}
|
|
2041
|
+
async handlePaymentAndRetry(url, body, response) {
|
|
2042
|
+
let paymentHeader = response.headers.get("payment-required");
|
|
2043
|
+
if (!paymentHeader) {
|
|
2044
|
+
try {
|
|
2045
|
+
const respBody = await response.json();
|
|
2046
|
+
if (respBody.x402 || respBody.accepts) {
|
|
2047
|
+
paymentHeader = btoa(JSON.stringify(respBody));
|
|
2048
|
+
}
|
|
2049
|
+
} catch {
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
if (!paymentHeader) {
|
|
2053
|
+
throw new PaymentError("402 response but no payment requirements found");
|
|
2054
|
+
}
|
|
2055
|
+
const paymentRequired = parsePaymentRequired(paymentHeader);
|
|
2056
|
+
const details = extractPaymentDetails(paymentRequired);
|
|
2057
|
+
const paymentPayload = await createPaymentPayload(
|
|
2058
|
+
this.privateKey,
|
|
2059
|
+
this.account.address,
|
|
2060
|
+
details.recipient,
|
|
2061
|
+
details.amount,
|
|
2062
|
+
details.network || "eip155:8453",
|
|
2063
|
+
{
|
|
2064
|
+
resourceUrl: details.resource?.url || url,
|
|
2065
|
+
resourceDescription: details.resource?.description || "BlockRun Search",
|
|
2066
|
+
maxTimeoutSeconds: details.maxTimeoutSeconds || 300,
|
|
2067
|
+
extra: details.extra
|
|
2068
|
+
}
|
|
2069
|
+
);
|
|
2070
|
+
const retry = await this.fetchWithTimeout(url, {
|
|
2071
|
+
method: "POST",
|
|
2072
|
+
headers: {
|
|
2073
|
+
"Content-Type": "application/json",
|
|
2074
|
+
"PAYMENT-SIGNATURE": paymentPayload
|
|
2075
|
+
},
|
|
2076
|
+
body: JSON.stringify(body)
|
|
2077
|
+
});
|
|
2078
|
+
if (retry.status === 402) {
|
|
2079
|
+
throw new PaymentError("Payment was rejected. Check your wallet balance.");
|
|
2080
|
+
}
|
|
2081
|
+
if (!retry.ok) {
|
|
2082
|
+
let errorBody;
|
|
2083
|
+
try {
|
|
2084
|
+
errorBody = await retry.json();
|
|
2085
|
+
} catch {
|
|
2086
|
+
errorBody = { error: "Request failed" };
|
|
2087
|
+
}
|
|
2088
|
+
throw new APIError(
|
|
2089
|
+
`API error after payment: ${retry.status}`,
|
|
2090
|
+
retry.status,
|
|
2091
|
+
sanitizeErrorResponse(errorBody)
|
|
2092
|
+
);
|
|
2093
|
+
}
|
|
2094
|
+
return retry.json();
|
|
2095
|
+
}
|
|
2096
|
+
async fetchWithTimeout(url, init) {
|
|
2097
|
+
const controller = new AbortController();
|
|
2098
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
2099
|
+
try {
|
|
2100
|
+
return await fetch(url, { ...init, signal: controller.signal });
|
|
2101
|
+
} finally {
|
|
2102
|
+
clearTimeout(timeoutId);
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
getWalletAddress() {
|
|
2106
|
+
return this.account.address;
|
|
2107
|
+
}
|
|
2108
|
+
};
|
|
2109
|
+
|
|
2110
|
+
// src/x-client.ts
|
|
2111
|
+
import { privateKeyToAccount as privateKeyToAccount5 } from "viem/accounts";
|
|
2112
|
+
var DEFAULT_API_URL5 = "https://blockrun.ai/api";
|
|
2113
|
+
var DEFAULT_TIMEOUT5 = 6e4;
|
|
2114
|
+
var XClient = class {
|
|
2115
|
+
account;
|
|
2116
|
+
privateKey;
|
|
2117
|
+
apiUrl;
|
|
2118
|
+
timeout;
|
|
2119
|
+
constructor(options = {}) {
|
|
2120
|
+
const envKey = typeof process !== "undefined" && process.env ? process.env.BLOCKRUN_WALLET_KEY || process.env.BASE_CHAIN_WALLET_KEY : void 0;
|
|
2121
|
+
const privateKey = options.privateKey || envKey;
|
|
2122
|
+
if (!privateKey) {
|
|
2123
|
+
throw new Error(
|
|
2124
|
+
"Private key required. Pass privateKey in options or set BLOCKRUN_WALLET_KEY environment variable."
|
|
2125
|
+
);
|
|
2126
|
+
}
|
|
2127
|
+
validatePrivateKey(privateKey);
|
|
2128
|
+
this.privateKey = privateKey;
|
|
2129
|
+
this.account = privateKeyToAccount5(privateKey);
|
|
2130
|
+
const apiUrl = options.apiUrl || DEFAULT_API_URL5;
|
|
2131
|
+
validateApiUrl(apiUrl);
|
|
2132
|
+
this.apiUrl = apiUrl.replace(/\/$/, "");
|
|
2133
|
+
this.timeout = options.timeout || DEFAULT_TIMEOUT5;
|
|
2134
|
+
}
|
|
2135
|
+
// ───────── User endpoints ─────────
|
|
2136
|
+
userLookup(usernames) {
|
|
2137
|
+
return this.post("/v1/x/users/lookup", { usernames });
|
|
2138
|
+
}
|
|
2139
|
+
userInfo(username) {
|
|
2140
|
+
return this.post("/v1/x/users/info", { username });
|
|
2141
|
+
}
|
|
2142
|
+
followers(username, cursor) {
|
|
2143
|
+
const body = { username };
|
|
2144
|
+
if (cursor) body.cursor = cursor;
|
|
2145
|
+
return this.post("/v1/x/users/followers", body);
|
|
2146
|
+
}
|
|
2147
|
+
/** `/v1/x/users/following` (singular path variant). */
|
|
2148
|
+
following(username, cursor) {
|
|
2149
|
+
const body = { username };
|
|
2150
|
+
if (cursor) body.cursor = cursor;
|
|
2151
|
+
return this.post("/v1/x/users/following", body);
|
|
2152
|
+
}
|
|
2153
|
+
/** `/v1/x/users/followings` (plural path variant). */
|
|
2154
|
+
followings(username, cursor) {
|
|
2155
|
+
const body = { username };
|
|
2156
|
+
if (cursor) body.cursor = cursor;
|
|
2157
|
+
return this.post("/v1/x/users/followings", body);
|
|
2158
|
+
}
|
|
2159
|
+
verifiedFollowers(userId, cursor) {
|
|
2160
|
+
const body = { userId };
|
|
2161
|
+
if (cursor) body.cursor = cursor;
|
|
2162
|
+
return this.post(
|
|
2163
|
+
"/v1/x/users/verified-followers",
|
|
2164
|
+
body
|
|
2165
|
+
);
|
|
2166
|
+
}
|
|
2167
|
+
userTweets(options) {
|
|
2168
|
+
if (!options.username && !options.userId) {
|
|
2169
|
+
throw new Error("Either username or userId is required");
|
|
2170
|
+
}
|
|
2171
|
+
const body = {};
|
|
2172
|
+
if (options.username) body.username = options.username;
|
|
2173
|
+
if (options.userId) body.userId = options.userId;
|
|
2174
|
+
if (options.cursor) body.cursor = options.cursor;
|
|
2175
|
+
if (options.includeReplies !== void 0) body.includeReplies = options.includeReplies;
|
|
2176
|
+
return this.post("/v1/x/users/tweets", body);
|
|
2177
|
+
}
|
|
2178
|
+
mentions(username, options) {
|
|
2179
|
+
const body = { username };
|
|
2180
|
+
if (options?.sinceTime) body.sinceTime = options.sinceTime;
|
|
2181
|
+
if (options?.untilTime) body.untilTime = options.untilTime;
|
|
2182
|
+
if (options?.cursor) body.cursor = options.cursor;
|
|
2183
|
+
return this.post("/v1/x/users/mentions", body);
|
|
2184
|
+
}
|
|
2185
|
+
// ───────── Tweet endpoints ─────────
|
|
2186
|
+
tweetLookup(tweetIds) {
|
|
2187
|
+
return this.post("/v1/x/tweets/lookup", {
|
|
2188
|
+
tweet_ids: tweetIds
|
|
2189
|
+
});
|
|
2190
|
+
}
|
|
2191
|
+
tweetReplies(tweetId, options) {
|
|
2192
|
+
const body = { tweetId };
|
|
2193
|
+
if (options?.cursor) body.cursor = options.cursor;
|
|
2194
|
+
if (options?.queryType) body.queryType = options.queryType;
|
|
2195
|
+
return this.post("/v1/x/tweets/replies", body);
|
|
2196
|
+
}
|
|
2197
|
+
tweetThread(tweetId, cursor) {
|
|
2198
|
+
const body = { tweetId };
|
|
2199
|
+
if (cursor) body.cursor = cursor;
|
|
2200
|
+
return this.post("/v1/x/tweets/thread", body);
|
|
2201
|
+
}
|
|
2202
|
+
// ───────── Search & discovery ─────────
|
|
2203
|
+
search(query, options) {
|
|
2204
|
+
const body = { query };
|
|
2205
|
+
if (options?.queryType) body.queryType = options.queryType;
|
|
2206
|
+
if (options?.cursor) body.cursor = options.cursor;
|
|
2207
|
+
return this.post("/v1/x/search", body);
|
|
2208
|
+
}
|
|
2209
|
+
trending() {
|
|
2210
|
+
return this.post("/v1/x/trending", {});
|
|
2211
|
+
}
|
|
2212
|
+
articlesRising() {
|
|
2213
|
+
return this.post("/v1/x/articles/rising", {});
|
|
2214
|
+
}
|
|
2215
|
+
// ───────── Internals ─────────
|
|
2216
|
+
async post(endpoint, body) {
|
|
2217
|
+
const url = `${this.apiUrl}${endpoint}`;
|
|
2218
|
+
const response = await this.fetchWithTimeout(url, {
|
|
2219
|
+
method: "POST",
|
|
2220
|
+
headers: { "Content-Type": "application/json" },
|
|
2221
|
+
body: JSON.stringify(body)
|
|
2222
|
+
});
|
|
2223
|
+
if (response.status === 402) {
|
|
2224
|
+
return this.payAndRetry(url, body, response);
|
|
2225
|
+
}
|
|
2226
|
+
if (!response.ok) {
|
|
2227
|
+
let errorBody;
|
|
2228
|
+
try {
|
|
2229
|
+
errorBody = await response.json();
|
|
2230
|
+
} catch {
|
|
2231
|
+
errorBody = { error: "Request failed" };
|
|
2232
|
+
}
|
|
2233
|
+
throw new APIError(
|
|
2234
|
+
`API error: ${response.status}`,
|
|
2235
|
+
response.status,
|
|
2236
|
+
sanitizeErrorResponse(errorBody)
|
|
2237
|
+
);
|
|
2238
|
+
}
|
|
2239
|
+
return response.json();
|
|
2240
|
+
}
|
|
2241
|
+
async payAndRetry(url, body, response) {
|
|
2242
|
+
let paymentHeader = response.headers.get("payment-required");
|
|
2243
|
+
if (!paymentHeader) {
|
|
2244
|
+
try {
|
|
2245
|
+
const respBody = await response.json();
|
|
2246
|
+
if (respBody.x402 || respBody.accepts) {
|
|
2247
|
+
paymentHeader = btoa(JSON.stringify(respBody));
|
|
2248
|
+
}
|
|
2249
|
+
} catch {
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
if (!paymentHeader) {
|
|
2253
|
+
throw new PaymentError("402 response but no payment requirements found");
|
|
2254
|
+
}
|
|
2255
|
+
const paymentRequired = parsePaymentRequired(paymentHeader);
|
|
2256
|
+
const details = extractPaymentDetails(paymentRequired);
|
|
2257
|
+
const paymentPayload = await createPaymentPayload(
|
|
2258
|
+
this.privateKey,
|
|
2259
|
+
this.account.address,
|
|
2260
|
+
details.recipient,
|
|
2261
|
+
details.amount,
|
|
2262
|
+
details.network || "eip155:8453",
|
|
2263
|
+
{
|
|
2264
|
+
resourceUrl: details.resource?.url || url,
|
|
2265
|
+
resourceDescription: details.resource?.description || "BlockRun X API",
|
|
2266
|
+
maxTimeoutSeconds: details.maxTimeoutSeconds || 300,
|
|
2267
|
+
extra: details.extra
|
|
2268
|
+
}
|
|
2269
|
+
);
|
|
2270
|
+
const retry = await this.fetchWithTimeout(url, {
|
|
2271
|
+
method: "POST",
|
|
2272
|
+
headers: {
|
|
2273
|
+
"Content-Type": "application/json",
|
|
2274
|
+
"PAYMENT-SIGNATURE": paymentPayload
|
|
2275
|
+
},
|
|
2276
|
+
body: JSON.stringify(body)
|
|
2277
|
+
});
|
|
2278
|
+
if (retry.status === 402) {
|
|
2279
|
+
throw new PaymentError("Payment was rejected. Check your wallet balance.");
|
|
2280
|
+
}
|
|
2281
|
+
if (!retry.ok) {
|
|
2282
|
+
let errorBody;
|
|
2283
|
+
try {
|
|
2284
|
+
errorBody = await retry.json();
|
|
2285
|
+
} catch {
|
|
2286
|
+
errorBody = { error: "Request failed" };
|
|
2287
|
+
}
|
|
2288
|
+
throw new APIError(
|
|
2289
|
+
`API error after payment: ${retry.status}`,
|
|
2290
|
+
retry.status,
|
|
2291
|
+
sanitizeErrorResponse(errorBody)
|
|
2292
|
+
);
|
|
2293
|
+
}
|
|
2294
|
+
return retry.json();
|
|
2295
|
+
}
|
|
2296
|
+
async fetchWithTimeout(url, init) {
|
|
2297
|
+
const controller = new AbortController();
|
|
2298
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
2299
|
+
try {
|
|
2300
|
+
return await fetch(url, { ...init, signal: controller.signal });
|
|
2301
|
+
} finally {
|
|
2302
|
+
clearTimeout(timeoutId);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
getWalletAddress() {
|
|
2306
|
+
return this.account.address;
|
|
2307
|
+
}
|
|
2308
|
+
};
|
|
2309
|
+
|
|
2310
|
+
// src/price.ts
|
|
2311
|
+
import { privateKeyToAccount as privateKeyToAccount6 } from "viem/accounts";
|
|
2312
|
+
var DEFAULT_API_URL6 = "https://blockrun.ai/api";
|
|
2313
|
+
var DEFAULT_TIMEOUT6 = 3e4;
|
|
2314
|
+
var PriceClient = class {
|
|
2315
|
+
account = null;
|
|
2316
|
+
privateKey = null;
|
|
2317
|
+
apiUrl;
|
|
2318
|
+
timeout;
|
|
2319
|
+
constructor(options = {}) {
|
|
2320
|
+
const envKey = typeof process !== "undefined" && process.env ? process.env.BLOCKRUN_WALLET_KEY || process.env.BASE_CHAIN_WALLET_KEY : void 0;
|
|
2321
|
+
const privateKey = options.privateKey || envKey;
|
|
2322
|
+
const requireWallet = options.requireWallet ?? true;
|
|
2323
|
+
if (!privateKey && requireWallet) {
|
|
2324
|
+
throw new Error(
|
|
2325
|
+
"Private key required for paid endpoints. Pass privateKey in options, set BLOCKRUN_WALLET_KEY, or pass requireWallet: false for free-only usage."
|
|
2326
|
+
);
|
|
2327
|
+
}
|
|
2328
|
+
if (privateKey) {
|
|
2329
|
+
validatePrivateKey(privateKey);
|
|
2330
|
+
this.privateKey = privateKey;
|
|
2331
|
+
this.account = privateKeyToAccount6(privateKey);
|
|
2332
|
+
}
|
|
2333
|
+
const apiUrl = options.apiUrl || DEFAULT_API_URL6;
|
|
2334
|
+
validateApiUrl(apiUrl);
|
|
2335
|
+
this.apiUrl = apiUrl.replace(/\/$/, "");
|
|
2336
|
+
this.timeout = options.timeout || DEFAULT_TIMEOUT6;
|
|
2337
|
+
}
|
|
2338
|
+
async price(category, symbol, options) {
|
|
2339
|
+
if (!symbol) throw new Error("symbol is required");
|
|
2340
|
+
const path5 = categoryPath(category, options?.market, "price", symbol);
|
|
2341
|
+
const query = {};
|
|
2342
|
+
if (options?.session) query.session = options.session;
|
|
2343
|
+
const data = await this.getWithPayment(path5, query);
|
|
2344
|
+
return {
|
|
2345
|
+
symbol: data.symbol ?? symbol.toUpperCase(),
|
|
2346
|
+
price: data.price,
|
|
2347
|
+
publishTime: data.publishTime,
|
|
2348
|
+
confidence: data.confidence,
|
|
2349
|
+
feedId: data.feedId,
|
|
2350
|
+
timestamp: data.timestamp,
|
|
2351
|
+
assetType: data.assetType,
|
|
2352
|
+
category: data.category,
|
|
2353
|
+
source: data.source,
|
|
2354
|
+
free: data.free
|
|
2355
|
+
};
|
|
2356
|
+
}
|
|
2357
|
+
async history(category, symbol, options) {
|
|
2358
|
+
if (!symbol) throw new Error("symbol is required");
|
|
2359
|
+
if (!options.from || options.from <= 0) {
|
|
2360
|
+
throw new Error("history requires options.from (unix seconds)");
|
|
2361
|
+
}
|
|
2362
|
+
const path5 = categoryPath(category, options.market, "history", symbol);
|
|
2363
|
+
const query = {
|
|
2364
|
+
resolution: options.resolution ?? "D",
|
|
2365
|
+
from: String(options.from)
|
|
2366
|
+
};
|
|
2367
|
+
if (options.to) query.to = String(options.to);
|
|
2368
|
+
if (options.session) query.session = options.session;
|
|
2369
|
+
const data = await this.getWithPayment(path5, query);
|
|
2370
|
+
return {
|
|
2371
|
+
symbol: data.symbol ?? symbol.toUpperCase(),
|
|
2372
|
+
resolution: data.resolution ?? (options.resolution ?? "D"),
|
|
2373
|
+
from: data.from,
|
|
2374
|
+
to: data.to,
|
|
2375
|
+
bars: data.bars ?? [],
|
|
2376
|
+
source: data.source,
|
|
2377
|
+
category: data.category
|
|
2378
|
+
};
|
|
2379
|
+
}
|
|
2380
|
+
async listSymbols(category, options) {
|
|
2381
|
+
const path5 = categoryPath(category, options?.market, "list");
|
|
2382
|
+
const query = {
|
|
2383
|
+
limit: String(options?.limit && options.limit > 0 ? options.limit : 100)
|
|
2384
|
+
};
|
|
2385
|
+
if (options?.query) query.q = options.query;
|
|
2386
|
+
const data = await this.getWithPayment(path5, query);
|
|
2387
|
+
if (Array.isArray(data)) {
|
|
2388
|
+
return { symbols: data, count: data.length };
|
|
2389
|
+
}
|
|
2390
|
+
const obj = data;
|
|
2391
|
+
const symbols = obj.symbols ?? obj.feeds ?? [];
|
|
2392
|
+
return {
|
|
2393
|
+
symbols,
|
|
2394
|
+
count: obj.count ?? symbols.length
|
|
2395
|
+
};
|
|
2396
|
+
}
|
|
2397
|
+
getWalletAddress() {
|
|
2398
|
+
return this.account?.address ?? null;
|
|
2399
|
+
}
|
|
2400
|
+
async getWithPayment(endpoint, query) {
|
|
2401
|
+
const url = buildUrl(`${this.apiUrl}${endpoint}`, query);
|
|
2402
|
+
const response = await this.fetchWithTimeout(url, { method: "GET" });
|
|
2403
|
+
if (response.status === 402) {
|
|
2404
|
+
if (!this.privateKey || !this.account) {
|
|
2405
|
+
throw new PaymentError(
|
|
2406
|
+
`${endpoint} returned 402 Payment Required but no wallet is configured.`
|
|
2407
|
+
);
|
|
2408
|
+
}
|
|
2409
|
+
return this.payAndRetry(url, response);
|
|
2410
|
+
}
|
|
2411
|
+
if (!response.ok) {
|
|
2412
|
+
let errorBody;
|
|
2413
|
+
try {
|
|
2414
|
+
errorBody = await response.json();
|
|
2415
|
+
} catch {
|
|
2416
|
+
errorBody = { error: "Request failed" };
|
|
2417
|
+
}
|
|
2418
|
+
throw new APIError(
|
|
2419
|
+
`API error: ${response.status}`,
|
|
2420
|
+
response.status,
|
|
2421
|
+
sanitizeErrorResponse(errorBody)
|
|
2422
|
+
);
|
|
2423
|
+
}
|
|
2424
|
+
return response.json();
|
|
2425
|
+
}
|
|
2426
|
+
async payAndRetry(url, response) {
|
|
2427
|
+
if (!this.privateKey || !this.account) {
|
|
2428
|
+
throw new PaymentError("Wallet required to sign payment.");
|
|
2429
|
+
}
|
|
2430
|
+
let paymentHeader = response.headers.get("payment-required");
|
|
2431
|
+
if (!paymentHeader) {
|
|
2432
|
+
try {
|
|
2433
|
+
const respBody = await response.json();
|
|
2434
|
+
if (respBody.x402) {
|
|
2435
|
+
paymentHeader = btoa(JSON.stringify(respBody.x402));
|
|
2436
|
+
} else if (respBody.accepts) {
|
|
2437
|
+
paymentHeader = btoa(JSON.stringify(respBody));
|
|
2438
|
+
}
|
|
2439
|
+
} catch {
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
if (!paymentHeader) {
|
|
2443
|
+
throw new PaymentError("402 response but no payment requirements found");
|
|
2444
|
+
}
|
|
2445
|
+
const paymentRequired = parsePaymentRequired(paymentHeader);
|
|
2446
|
+
const details = extractPaymentDetails(paymentRequired);
|
|
2447
|
+
const paymentPayload = await createPaymentPayload(
|
|
2448
|
+
this.privateKey,
|
|
2449
|
+
this.account.address,
|
|
2450
|
+
details.recipient,
|
|
2451
|
+
details.amount,
|
|
2452
|
+
details.network || "eip155:8453",
|
|
2453
|
+
{
|
|
2454
|
+
resourceUrl: details.resource?.url || url,
|
|
2455
|
+
resourceDescription: details.resource?.description || "BlockRun Price Data",
|
|
2456
|
+
maxTimeoutSeconds: details.maxTimeoutSeconds || 300,
|
|
2457
|
+
extra: details.extra
|
|
2458
|
+
}
|
|
2459
|
+
);
|
|
2460
|
+
const retry = await this.fetchWithTimeout(url, {
|
|
2461
|
+
method: "GET",
|
|
2462
|
+
headers: { "PAYMENT-SIGNATURE": paymentPayload }
|
|
2463
|
+
});
|
|
2464
|
+
if (retry.status === 402) {
|
|
2465
|
+
throw new PaymentError("Payment was rejected. Check your wallet balance.");
|
|
2466
|
+
}
|
|
2467
|
+
if (!retry.ok) {
|
|
2468
|
+
let errorBody;
|
|
2469
|
+
try {
|
|
2470
|
+
errorBody = await retry.json();
|
|
2471
|
+
} catch {
|
|
2472
|
+
errorBody = { error: "Request failed" };
|
|
2473
|
+
}
|
|
2474
|
+
throw new APIError(
|
|
2475
|
+
`API error after payment: ${retry.status}`,
|
|
2476
|
+
retry.status,
|
|
2477
|
+
sanitizeErrorResponse(errorBody)
|
|
2478
|
+
);
|
|
2479
|
+
}
|
|
2480
|
+
return retry.json();
|
|
2481
|
+
}
|
|
2482
|
+
async fetchWithTimeout(url, init) {
|
|
2483
|
+
const controller = new AbortController();
|
|
2484
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
2485
|
+
try {
|
|
2486
|
+
return await fetch(url, { ...init, signal: controller.signal });
|
|
2487
|
+
} finally {
|
|
2488
|
+
clearTimeout(timeoutId);
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
};
|
|
2492
|
+
function categoryPath(category, market, kind, symbol) {
|
|
2493
|
+
let base;
|
|
2494
|
+
if (category === "stocks") {
|
|
2495
|
+
if (!market) {
|
|
2496
|
+
throw new Error("market is required when category === 'stocks'");
|
|
2497
|
+
}
|
|
2498
|
+
base = `/v1/stocks/${market}`;
|
|
2499
|
+
} else if (["crypto", "fx", "commodity", "usstock"].includes(category)) {
|
|
2500
|
+
base = `/v1/${category}`;
|
|
2501
|
+
} else {
|
|
2502
|
+
throw new Error(`unknown category: ${category}`);
|
|
2503
|
+
}
|
|
2504
|
+
if (!symbol) return `${base}/${kind}`;
|
|
2505
|
+
return `${base}/${kind}/${encodeURIComponent(symbol.toUpperCase())}`;
|
|
2506
|
+
}
|
|
2507
|
+
function buildUrl(base, query) {
|
|
2508
|
+
const params = Object.entries(query);
|
|
2509
|
+
if (params.length === 0) return base;
|
|
2510
|
+
const qs = params.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join("&");
|
|
2511
|
+
return `${base}?${qs}`;
|
|
2512
|
+
}
|
|
2513
|
+
|
|
1971
2514
|
// src/wallet.ts
|
|
1972
|
-
import { privateKeyToAccount as
|
|
2515
|
+
import { privateKeyToAccount as privateKeyToAccount7, generatePrivateKey } from "viem/accounts";
|
|
1973
2516
|
import * as fs from "fs";
|
|
1974
2517
|
import * as path from "path";
|
|
1975
2518
|
import * as os from "os";
|
|
@@ -1979,7 +2522,7 @@ var WALLET_DIR = path.join(os.homedir(), ".blockrun");
|
|
|
1979
2522
|
var WALLET_FILE = path.join(WALLET_DIR, ".session");
|
|
1980
2523
|
function createWallet() {
|
|
1981
2524
|
const privateKey = generatePrivateKey();
|
|
1982
|
-
const account =
|
|
2525
|
+
const account = privateKeyToAccount7(privateKey);
|
|
1983
2526
|
return {
|
|
1984
2527
|
address: account.address,
|
|
1985
2528
|
privateKey
|
|
@@ -2035,12 +2578,12 @@ function loadWallet() {
|
|
|
2035
2578
|
function getOrCreateWallet() {
|
|
2036
2579
|
const envKey = typeof process !== "undefined" && process.env ? process.env.BLOCKRUN_WALLET_KEY || process.env.BASE_CHAIN_WALLET_KEY : void 0;
|
|
2037
2580
|
if (envKey) {
|
|
2038
|
-
const account =
|
|
2581
|
+
const account = privateKeyToAccount7(envKey);
|
|
2039
2582
|
return { address: account.address, privateKey: envKey, isNew: false };
|
|
2040
2583
|
}
|
|
2041
2584
|
const fileKey = loadWallet();
|
|
2042
2585
|
if (fileKey) {
|
|
2043
|
-
const account =
|
|
2586
|
+
const account = privateKeyToAccount7(fileKey);
|
|
2044
2587
|
return { address: account.address, privateKey: fileKey, isNew: false };
|
|
2045
2588
|
}
|
|
2046
2589
|
const { address, privateKey } = createWallet();
|
|
@@ -2050,11 +2593,11 @@ function getOrCreateWallet() {
|
|
|
2050
2593
|
function getWalletAddress() {
|
|
2051
2594
|
const envKey = typeof process !== "undefined" && process.env ? process.env.BLOCKRUN_WALLET_KEY || process.env.BASE_CHAIN_WALLET_KEY : void 0;
|
|
2052
2595
|
if (envKey) {
|
|
2053
|
-
return
|
|
2596
|
+
return privateKeyToAccount7(envKey).address;
|
|
2054
2597
|
}
|
|
2055
2598
|
const fileKey = loadWallet();
|
|
2056
2599
|
if (fileKey) {
|
|
2057
|
-
return
|
|
2600
|
+
return privateKeyToAccount7(fileKey).address;
|
|
2058
2601
|
}
|
|
2059
2602
|
return null;
|
|
2060
2603
|
}
|
|
@@ -2234,7 +2777,7 @@ async function getOrCreateSolanaWallet() {
|
|
|
2234
2777
|
// src/solana-client.ts
|
|
2235
2778
|
var SOLANA_API_URL = "https://sol.blockrun.ai/api";
|
|
2236
2779
|
var DEFAULT_MAX_TOKENS2 = 1024;
|
|
2237
|
-
var
|
|
2780
|
+
var DEFAULT_TIMEOUT7 = 6e4;
|
|
2238
2781
|
var SDK_VERSION2 = "0.3.0";
|
|
2239
2782
|
var USER_AGENT2 = `blockrun-ts/${SDK_VERSION2}`;
|
|
2240
2783
|
var SolanaLLMClient = class {
|
|
@@ -2259,7 +2802,7 @@ var SolanaLLMClient = class {
|
|
|
2259
2802
|
validateApiUrl(apiUrl);
|
|
2260
2803
|
this.apiUrl = apiUrl.replace(/\/$/, "");
|
|
2261
2804
|
this.rpcUrl = options.rpcUrl || "https://api.mainnet-beta.solana.com";
|
|
2262
|
-
this.timeout = options.timeout ||
|
|
2805
|
+
this.timeout = options.timeout || DEFAULT_TIMEOUT7;
|
|
2263
2806
|
}
|
|
2264
2807
|
/** Get Solana wallet address (public key in base58). */
|
|
2265
2808
|
async getWalletAddress() {
|
|
@@ -3204,7 +3747,7 @@ var OpenAI = class {
|
|
|
3204
3747
|
};
|
|
3205
3748
|
|
|
3206
3749
|
// src/anthropic-compat.ts
|
|
3207
|
-
import { privateKeyToAccount as
|
|
3750
|
+
import { privateKeyToAccount as privateKeyToAccount8 } from "viem/accounts";
|
|
3208
3751
|
var AnthropicClient = class {
|
|
3209
3752
|
_client = null;
|
|
3210
3753
|
_clientPromise = null;
|
|
@@ -3217,7 +3760,7 @@ var AnthropicClient = class {
|
|
|
3217
3760
|
const key = options.privateKey ?? wallet.privateKey;
|
|
3218
3761
|
validatePrivateKey(key);
|
|
3219
3762
|
this._privateKey = key;
|
|
3220
|
-
this._account =
|
|
3763
|
+
this._account = privateKeyToAccount8(this._privateKey);
|
|
3221
3764
|
const apiUrl = options.apiUrl ?? "https://blockrun.ai/api";
|
|
3222
3765
|
validateApiUrl(apiUrl);
|
|
3223
3766
|
this._apiUrl = apiUrl.replace(/\/$/, "");
|
|
@@ -3322,14 +3865,17 @@ export {
|
|
|
3322
3865
|
MusicClient,
|
|
3323
3866
|
OpenAI,
|
|
3324
3867
|
PaymentError,
|
|
3868
|
+
PriceClient,
|
|
3325
3869
|
SOLANA_NETWORK,
|
|
3326
3870
|
SOLANA_WALLET_FILE as SOLANA_WALLET_FILE_PATH,
|
|
3871
|
+
SearchClient,
|
|
3327
3872
|
SolanaLLMClient,
|
|
3328
3873
|
USDC_BASE,
|
|
3329
3874
|
USDC_BASE_CONTRACT,
|
|
3330
3875
|
USDC_SOLANA,
|
|
3331
3876
|
WALLET_DIR_PATH,
|
|
3332
3877
|
WALLET_FILE_PATH,
|
|
3878
|
+
XClient,
|
|
3333
3879
|
clearCache,
|
|
3334
3880
|
createPaymentPayload,
|
|
3335
3881
|
createSolanaPaymentPayload,
|