@blockrun/llm 3.26.0 → 3.26.3
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/dist/index.cjs +104 -14
- package/dist/index.d.cts +15 -2
- package/dist/index.d.ts +15 -2
- package/dist/index.js +104 -14
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -269,6 +269,18 @@ function validateApiUrl(url) {
|
|
|
269
269
|
);
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
|
+
var PAYMENT_FACT_FIELDS = ["payment_status", "tx_hash", "payment_note"];
|
|
273
|
+
function paymentFactsNote(errorBody) {
|
|
274
|
+
if (typeof errorBody !== "object" || errorBody === null) return "";
|
|
275
|
+
const { payment_status: status2, tx_hash: txHash } = errorBody;
|
|
276
|
+
if (status2 === "settled") {
|
|
277
|
+
const tx = typeof txHash === "string" && txHash ? ` (tx ${txHash})` : "";
|
|
278
|
+
return `. Payment settled${tx}. Do not pay for this job again.`;
|
|
279
|
+
}
|
|
280
|
+
if (status2 === "unknown") return ". Payment state unknown. Check your wallet before paying again.";
|
|
281
|
+
if (status2 === "not_charged" || status2 === "settlement_failed") return ". No payment was taken.";
|
|
282
|
+
return "";
|
|
283
|
+
}
|
|
272
284
|
function sanitizeErrorResponse(errorBody) {
|
|
273
285
|
if (typeof errorBody !== "object" || errorBody === null) {
|
|
274
286
|
return { message: "API request failed" };
|
|
@@ -279,7 +291,10 @@ function sanitizeErrorResponse(errorBody) {
|
|
|
279
291
|
message: typeof body.error === "string" ? body.error : "API request failed",
|
|
280
292
|
code: typeof body.code === "string" ? body.code : void 0,
|
|
281
293
|
...detail ? { detail } : {},
|
|
282
|
-
...typeof body.hint === "string" ? { hint: body.hint } : {}
|
|
294
|
+
...typeof body.hint === "string" ? { hint: body.hint } : {},
|
|
295
|
+
...Object.fromEntries(
|
|
296
|
+
PAYMENT_FACT_FIELDS.filter((field) => typeof body[field] === "string").map((field) => [field, body[field]])
|
|
297
|
+
)
|
|
283
298
|
};
|
|
284
299
|
}
|
|
285
300
|
function validateResourceUrl(url, baseUrl) {
|
|
@@ -5049,10 +5064,16 @@ function parsePaymentRequired(headerValue) {
|
|
|
5049
5064
|
}
|
|
5050
5065
|
}
|
|
5051
5066
|
function extractPaymentDetails(paymentRequired, preferredNetwork) {
|
|
5052
|
-
const
|
|
5053
|
-
if (
|
|
5067
|
+
const offered = paymentRequired.accepts || [];
|
|
5068
|
+
if (offered.length === 0) {
|
|
5054
5069
|
throw new Error("No payment options in payment required response");
|
|
5055
5070
|
}
|
|
5071
|
+
const accepts = offered.filter((opt) => !opt.scheme || opt.scheme === "exact");
|
|
5072
|
+
if (accepts.length === 0) {
|
|
5073
|
+
throw new Error(
|
|
5074
|
+
`This client signs the exact scheme, but the 402 only offers ${offered.map((opt) => JSON.stringify(opt.scheme)).join(", ")}`
|
|
5075
|
+
);
|
|
5076
|
+
}
|
|
5056
5077
|
let option = null;
|
|
5057
5078
|
if (preferredNetwork) {
|
|
5058
5079
|
option = accepts.find((opt) => opt.network === preferredNetwork) || null;
|
|
@@ -5122,7 +5143,7 @@ function getCostSummary() {
|
|
|
5122
5143
|
}
|
|
5123
5144
|
|
|
5124
5145
|
// src/version.ts
|
|
5125
|
-
var SDK_VERSION = "3.26.
|
|
5146
|
+
var SDK_VERSION = "3.26.3";
|
|
5126
5147
|
var USER_AGENT = `blockrun-ts/${SDK_VERSION}`;
|
|
5127
5148
|
|
|
5128
5149
|
// src/client.ts
|
|
@@ -7078,6 +7099,9 @@ var import_accounts4 = require("viem/accounts");
|
|
|
7078
7099
|
var DEFAULT_API_URL3 = "https://blockrun.ai/api";
|
|
7079
7100
|
var DEFAULT_MODEL2 = "minimax/music-2.5+";
|
|
7080
7101
|
var DEFAULT_TIMEOUT3 = 21e4;
|
|
7102
|
+
var POLL_INTERVAL_MS2 = 5e3;
|
|
7103
|
+
var POLL_MAX_DURATION_MS2 = 3e5;
|
|
7104
|
+
var TRANSIENT_POLL_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
|
|
7081
7105
|
var MusicClient = class {
|
|
7082
7106
|
_account;
|
|
7083
7107
|
get account() {
|
|
@@ -7219,15 +7243,79 @@ var MusicClient = class {
|
|
|
7219
7243
|
}
|
|
7220
7244
|
throw new APIError(`API error after payment: ${retryResponse.status}`, retryResponse.status, sanitizeErrorResponse(errorBody));
|
|
7221
7245
|
}
|
|
7246
|
+
const costUsd = Number(details.amount) / 1e6;
|
|
7222
7247
|
const data = await retryResponse.json();
|
|
7223
|
-
if (
|
|
7248
|
+
if (retryResponse.status === 202 || data.status === "queued" || data.status === "in_progress") {
|
|
7249
|
+
if (!data.poll_url) {
|
|
7250
|
+
throw new APIError(
|
|
7251
|
+
`Server returned ${retryResponse.status} but no poll_url to follow.`,
|
|
7252
|
+
retryResponse.status,
|
|
7253
|
+
sanitizeErrorResponse(data)
|
|
7254
|
+
);
|
|
7255
|
+
}
|
|
7256
|
+
return this.pollMusicJob(data.poll_url, paymentPayload, costUsd);
|
|
7257
|
+
}
|
|
7258
|
+
return this.finishPaidResponse(retryResponse, data, costUsd);
|
|
7259
|
+
}
|
|
7260
|
+
finishPaidResponse(response, data, costUsd) {
|
|
7261
|
+
if (!settlementSkipped(response)) {
|
|
7224
7262
|
this.sessionCalls++;
|
|
7225
|
-
this.sessionTotalUsd +=
|
|
7263
|
+
this.sessionTotalUsd += costUsd;
|
|
7226
7264
|
}
|
|
7227
|
-
const txHash =
|
|
7265
|
+
const txHash = response.headers.get("x-payment-receipt");
|
|
7228
7266
|
if (txHash) data.txHash = txHash;
|
|
7229
7267
|
return data;
|
|
7230
7268
|
}
|
|
7269
|
+
/**
|
|
7270
|
+
* Poll a slow track until it completes, sending the SAME payment header each
|
|
7271
|
+
* time: the server binds the payer to the job and settles on the first poll
|
|
7272
|
+
* that sees it completed.
|
|
7273
|
+
*
|
|
7274
|
+
* Network errors and 502/503/504 keep polling. A response lost on the settling
|
|
7275
|
+
* poll would otherwise drop a track that was paid for, and polling again returns
|
|
7276
|
+
* it as already settled, with no second charge.
|
|
7277
|
+
*/
|
|
7278
|
+
async pollMusicJob(pollPath, paymentHeader, costUsd) {
|
|
7279
|
+
const pollUrl = pollPath.startsWith("/api/") ? `${this.apiUrl.replace(/\/api$/, "")}${pollPath}` : `${this.apiUrl}${pollPath.startsWith("/") ? "" : "/"}${pollPath}`;
|
|
7280
|
+
const startedAt = Date.now();
|
|
7281
|
+
while (Date.now() - startedAt < POLL_MAX_DURATION_MS2) {
|
|
7282
|
+
await new Promise((resolve2) => setTimeout(resolve2, POLL_INTERVAL_MS2));
|
|
7283
|
+
let pollResp;
|
|
7284
|
+
try {
|
|
7285
|
+
pollResp = await this.fetchWithTimeout(pollUrl, {
|
|
7286
|
+
method: "GET",
|
|
7287
|
+
headers: { "PAYMENT-SIGNATURE": paymentHeader }
|
|
7288
|
+
});
|
|
7289
|
+
} catch {
|
|
7290
|
+
continue;
|
|
7291
|
+
}
|
|
7292
|
+
if (pollResp.status === 202 || TRANSIENT_POLL_STATUSES.has(pollResp.status)) continue;
|
|
7293
|
+
const pollBody = await pollResp.json().catch(() => ({}));
|
|
7294
|
+
if (!pollResp.ok) {
|
|
7295
|
+
throw new APIError(
|
|
7296
|
+
`API error during poll of ${pollPath}: ${pollResp.status}`,
|
|
7297
|
+
pollResp.status,
|
|
7298
|
+
sanitizeErrorResponse(pollBody)
|
|
7299
|
+
);
|
|
7300
|
+
}
|
|
7301
|
+
if (pollBody.status === "failed") {
|
|
7302
|
+
throw new APIError(
|
|
7303
|
+
pollBody.error || "Upstream music generation failed",
|
|
7304
|
+
200,
|
|
7305
|
+
sanitizeErrorResponse(pollBody)
|
|
7306
|
+
);
|
|
7307
|
+
}
|
|
7308
|
+
if (pollBody.data) {
|
|
7309
|
+
if (!pollBody.txHash && pollBody.payment?.tx_hash) pollBody.txHash = pollBody.payment.tx_hash;
|
|
7310
|
+
return this.finishPaidResponse(pollResp, pollBody, costUsd);
|
|
7311
|
+
}
|
|
7312
|
+
}
|
|
7313
|
+
throw new APIError(
|
|
7314
|
+
`Music generation did not finish within ${POLL_MAX_DURATION_MS2 / 1e3}s. The job may still complete: poll poll_url with the same payment header before paying again.`,
|
|
7315
|
+
504,
|
|
7316
|
+
{ error: "poll_timeout", poll_url: pollPath }
|
|
7317
|
+
);
|
|
7318
|
+
}
|
|
7231
7319
|
async fetchWithTimeout(url, options) {
|
|
7232
7320
|
const controller = new AbortController();
|
|
7233
7321
|
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
@@ -7467,7 +7555,7 @@ var import_accounts6 = require("viem/accounts");
|
|
|
7467
7555
|
var DEFAULT_API_URL5 = "https://blockrun.ai/api";
|
|
7468
7556
|
var DEFAULT_MODEL4 = "xai/grok-imagine-video";
|
|
7469
7557
|
var DEFAULT_TIMEOUT5 = 12e4;
|
|
7470
|
-
var
|
|
7558
|
+
var POLL_INTERVAL_MS3 = 5e3;
|
|
7471
7559
|
var DEFAULT_GENERATE_BUDGET_MS = 9e5;
|
|
7472
7560
|
var MAX_TIMEOUT_SECONDS = 600;
|
|
7473
7561
|
var MAX_POLL_RESIGNS = 2;
|
|
@@ -7678,7 +7766,7 @@ var VideoClient = class {
|
|
|
7678
7766
|
headers: { "Content-Type": "application/json" },
|
|
7679
7767
|
body: JSON.stringify(body)
|
|
7680
7768
|
});
|
|
7681
|
-
if (this.apiAuth) return this.apiAuth.poll(resp402, budgetMs,
|
|
7769
|
+
if (this.apiAuth) return this.apiAuth.poll(resp402, budgetMs, POLL_INTERVAL_MS3);
|
|
7682
7770
|
if (resp402.status !== 402) {
|
|
7683
7771
|
await this.throwApiError(resp402, "Expected 402 on first POST");
|
|
7684
7772
|
}
|
|
@@ -7713,7 +7801,7 @@ var VideoClient = class {
|
|
|
7713
7801
|
let lastStatus = submitData.status || "queued";
|
|
7714
7802
|
let resignsLeft = MAX_POLL_RESIGNS;
|
|
7715
7803
|
while (Date.now() < deadline) {
|
|
7716
|
-
await new Promise((r) => setTimeout(r,
|
|
7804
|
+
await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS3));
|
|
7717
7805
|
const pollResp = await this.fetchWithTimeout(pollUrl, {
|
|
7718
7806
|
method: "GET",
|
|
7719
7807
|
headers: { "PAYMENT-SIGNATURE": paymentPayload }
|
|
@@ -9446,7 +9534,7 @@ var BlockrunClient = class {
|
|
|
9446
9534
|
}
|
|
9447
9535
|
if (lastStatus === "failed") {
|
|
9448
9536
|
throw new APIError(
|
|
9449
|
-
`Upstream job failed: ${pollData.error || "unknown"}`,
|
|
9537
|
+
`Upstream job failed: ${pollData.error || "unknown"}${paymentFactsNote(pollData)}`,
|
|
9450
9538
|
pollResp.status,
|
|
9451
9539
|
sanitizeErrorResponse(pollData)
|
|
9452
9540
|
);
|
|
@@ -13418,7 +13506,8 @@ var SolanaLLMClient = class {
|
|
|
13418
13506
|
*
|
|
13419
13507
|
* The POST is verified, not charged. The job is then polled with a wallet
|
|
13420
13508
|
* signature (no payment) until it finishes, and ONE payment is signed for
|
|
13421
|
-
* the finished video; a
|
|
13509
|
+
* the finished video; a job that fails before completing is not charged. A paid
|
|
13510
|
+
* job can still fail, and its APIError then carries `payment_status`. If the gateway does not
|
|
13422
13511
|
* accept poll signatures for the job, it is polled with a payment per poll
|
|
13423
13512
|
* instead, which is likewise only charged on completion.
|
|
13424
13513
|
*
|
|
@@ -13786,7 +13875,8 @@ var SolanaLLMClient = class {
|
|
|
13786
13875
|
* cost at POST; this loop only fetches the result and never signs a payment.
|
|
13787
13876
|
* A batch-billed image job still answers its proof poll with 402: that job
|
|
13788
13877
|
* falls back to the per-poll payment below.
|
|
13789
|
-
* - video: settles on completion
|
|
13878
|
+
* - video: settles on completion. A job that fails before completing is not
|
|
13879
|
+
* charged; one that fails after payment says so in `payment_status`. A
|
|
13790
13880
|
* finished, unpaid job answers the proof poll with 402 `{status:"completed"}`
|
|
13791
13881
|
* and a normal x402 challenge; ONE payment is signed from that challenge and
|
|
13792
13882
|
* the same poll is re-sent with it. The cost is recorded here, on delivery.
|
|
@@ -13833,7 +13923,7 @@ var SolanaLLMClient = class {
|
|
|
13833
13923
|
const failIfFailed = (data, status2) => {
|
|
13834
13924
|
if (lastStatus === "failed") {
|
|
13835
13925
|
throw new APIError(
|
|
13836
|
-
`Upstream job failed: ${data.error || "unknown"}`,
|
|
13926
|
+
`Upstream job failed: ${data.error || "unknown"}${paymentFactsNote(data)}`,
|
|
13837
13927
|
status2,
|
|
13838
13928
|
sanitizeErrorResponse(data)
|
|
13839
13929
|
);
|
package/dist/index.d.cts
CHANGED
|
@@ -1998,6 +1998,17 @@ declare class MusicClient {
|
|
|
1998
1998
|
generate(prompt: string, options?: MusicGenerateOptions): Promise<MusicResponse>;
|
|
1999
1999
|
private requestWithPayment;
|
|
2000
2000
|
private handlePaymentAndRetry;
|
|
2001
|
+
private finishPaidResponse;
|
|
2002
|
+
/**
|
|
2003
|
+
* Poll a slow track until it completes, sending the SAME payment header each
|
|
2004
|
+
* time: the server binds the payer to the job and settles on the first poll
|
|
2005
|
+
* that sees it completed.
|
|
2006
|
+
*
|
|
2007
|
+
* Network errors and 502/503/504 keep polling. A response lost on the settling
|
|
2008
|
+
* poll would otherwise drop a track that was paid for, and polling again returns
|
|
2009
|
+
* it as already settled, with no second charge.
|
|
2010
|
+
*/
|
|
2011
|
+
private pollMusicJob;
|
|
2001
2012
|
private fetchWithTimeout;
|
|
2002
2013
|
getWalletAddress(): string;
|
|
2003
2014
|
getSpending(): Spending;
|
|
@@ -3901,7 +3912,8 @@ declare class SolanaLLMClient {
|
|
|
3901
3912
|
*
|
|
3902
3913
|
* The POST is verified, not charged. The job is then polled with a wallet
|
|
3903
3914
|
* signature (no payment) until it finishes, and ONE payment is signed for
|
|
3904
|
-
* the finished video; a
|
|
3915
|
+
* the finished video; a job that fails before completing is not charged. A paid
|
|
3916
|
+
* job can still fail, and its APIError then carries `payment_status`. If the gateway does not
|
|
3905
3917
|
* accept poll signatures for the job, it is polled with a payment per poll
|
|
3906
3918
|
* instead, which is likewise only charged on completion.
|
|
3907
3919
|
*
|
|
@@ -4086,7 +4098,8 @@ declare class SolanaLLMClient {
|
|
|
4086
4098
|
* cost at POST; this loop only fetches the result and never signs a payment.
|
|
4087
4099
|
* A batch-billed image job still answers its proof poll with 402: that job
|
|
4088
4100
|
* falls back to the per-poll payment below.
|
|
4089
|
-
* - video: settles on completion
|
|
4101
|
+
* - video: settles on completion. A job that fails before completing is not
|
|
4102
|
+
* charged; one that fails after payment says so in `payment_status`. A
|
|
4090
4103
|
* finished, unpaid job answers the proof poll with 402 `{status:"completed"}`
|
|
4091
4104
|
* and a normal x402 challenge; ONE payment is signed from that challenge and
|
|
4092
4105
|
* the same poll is re-sent with it. The cost is recorded here, on delivery.
|
package/dist/index.d.ts
CHANGED
|
@@ -1998,6 +1998,17 @@ declare class MusicClient {
|
|
|
1998
1998
|
generate(prompt: string, options?: MusicGenerateOptions): Promise<MusicResponse>;
|
|
1999
1999
|
private requestWithPayment;
|
|
2000
2000
|
private handlePaymentAndRetry;
|
|
2001
|
+
private finishPaidResponse;
|
|
2002
|
+
/**
|
|
2003
|
+
* Poll a slow track until it completes, sending the SAME payment header each
|
|
2004
|
+
* time: the server binds the payer to the job and settles on the first poll
|
|
2005
|
+
* that sees it completed.
|
|
2006
|
+
*
|
|
2007
|
+
* Network errors and 502/503/504 keep polling. A response lost on the settling
|
|
2008
|
+
* poll would otherwise drop a track that was paid for, and polling again returns
|
|
2009
|
+
* it as already settled, with no second charge.
|
|
2010
|
+
*/
|
|
2011
|
+
private pollMusicJob;
|
|
2001
2012
|
private fetchWithTimeout;
|
|
2002
2013
|
getWalletAddress(): string;
|
|
2003
2014
|
getSpending(): Spending;
|
|
@@ -3901,7 +3912,8 @@ declare class SolanaLLMClient {
|
|
|
3901
3912
|
*
|
|
3902
3913
|
* The POST is verified, not charged. The job is then polled with a wallet
|
|
3903
3914
|
* signature (no payment) until it finishes, and ONE payment is signed for
|
|
3904
|
-
* the finished video; a
|
|
3915
|
+
* the finished video; a job that fails before completing is not charged. A paid
|
|
3916
|
+
* job can still fail, and its APIError then carries `payment_status`. If the gateway does not
|
|
3905
3917
|
* accept poll signatures for the job, it is polled with a payment per poll
|
|
3906
3918
|
* instead, which is likewise only charged on completion.
|
|
3907
3919
|
*
|
|
@@ -4086,7 +4098,8 @@ declare class SolanaLLMClient {
|
|
|
4086
4098
|
* cost at POST; this loop only fetches the result and never signs a payment.
|
|
4087
4099
|
* A batch-billed image job still answers its proof poll with 402: that job
|
|
4088
4100
|
* falls back to the per-poll payment below.
|
|
4089
|
-
* - video: settles on completion
|
|
4101
|
+
* - video: settles on completion. A job that fails before completing is not
|
|
4102
|
+
* charged; one that fails after payment says so in `payment_status`. A
|
|
4090
4103
|
* finished, unpaid job answers the proof poll with 402 `{status:"completed"}`
|
|
4091
4104
|
* and a normal x402 challenge; ONE payment is signed from that challenge and
|
|
4092
4105
|
* the same poll is re-sent with it. The cost is recorded here, on delivery.
|
package/dist/index.js
CHANGED
|
@@ -136,6 +136,18 @@ function validateApiUrl(url) {
|
|
|
136
136
|
);
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
+
var PAYMENT_FACT_FIELDS = ["payment_status", "tx_hash", "payment_note"];
|
|
140
|
+
function paymentFactsNote(errorBody) {
|
|
141
|
+
if (typeof errorBody !== "object" || errorBody === null) return "";
|
|
142
|
+
const { payment_status: status2, tx_hash: txHash } = errorBody;
|
|
143
|
+
if (status2 === "settled") {
|
|
144
|
+
const tx = typeof txHash === "string" && txHash ? ` (tx ${txHash})` : "";
|
|
145
|
+
return `. Payment settled${tx}. Do not pay for this job again.`;
|
|
146
|
+
}
|
|
147
|
+
if (status2 === "unknown") return ". Payment state unknown. Check your wallet before paying again.";
|
|
148
|
+
if (status2 === "not_charged" || status2 === "settlement_failed") return ". No payment was taken.";
|
|
149
|
+
return "";
|
|
150
|
+
}
|
|
139
151
|
function sanitizeErrorResponse(errorBody) {
|
|
140
152
|
if (typeof errorBody !== "object" || errorBody === null) {
|
|
141
153
|
return { message: "API request failed" };
|
|
@@ -146,7 +158,10 @@ function sanitizeErrorResponse(errorBody) {
|
|
|
146
158
|
message: typeof body.error === "string" ? body.error : "API request failed",
|
|
147
159
|
code: typeof body.code === "string" ? body.code : void 0,
|
|
148
160
|
...detail ? { detail } : {},
|
|
149
|
-
...typeof body.hint === "string" ? { hint: body.hint } : {}
|
|
161
|
+
...typeof body.hint === "string" ? { hint: body.hint } : {},
|
|
162
|
+
...Object.fromEntries(
|
|
163
|
+
PAYMENT_FACT_FIELDS.filter((field) => typeof body[field] === "string").map((field) => [field, body[field]])
|
|
164
|
+
)
|
|
150
165
|
};
|
|
151
166
|
}
|
|
152
167
|
function validateResourceUrl(url, baseUrl) {
|
|
@@ -4916,10 +4931,16 @@ function parsePaymentRequired(headerValue) {
|
|
|
4916
4931
|
}
|
|
4917
4932
|
}
|
|
4918
4933
|
function extractPaymentDetails(paymentRequired, preferredNetwork) {
|
|
4919
|
-
const
|
|
4920
|
-
if (
|
|
4934
|
+
const offered = paymentRequired.accepts || [];
|
|
4935
|
+
if (offered.length === 0) {
|
|
4921
4936
|
throw new Error("No payment options in payment required response");
|
|
4922
4937
|
}
|
|
4938
|
+
const accepts = offered.filter((opt) => !opt.scheme || opt.scheme === "exact");
|
|
4939
|
+
if (accepts.length === 0) {
|
|
4940
|
+
throw new Error(
|
|
4941
|
+
`This client signs the exact scheme, but the 402 only offers ${offered.map((opt) => JSON.stringify(opt.scheme)).join(", ")}`
|
|
4942
|
+
);
|
|
4943
|
+
}
|
|
4923
4944
|
let option = null;
|
|
4924
4945
|
if (preferredNetwork) {
|
|
4925
4946
|
option = accepts.find((opt) => opt.network === preferredNetwork) || null;
|
|
@@ -4989,7 +5010,7 @@ function getCostSummary() {
|
|
|
4989
5010
|
}
|
|
4990
5011
|
|
|
4991
5012
|
// src/version.ts
|
|
4992
|
-
var SDK_VERSION = "3.26.
|
|
5013
|
+
var SDK_VERSION = "3.26.3";
|
|
4993
5014
|
var USER_AGENT = `blockrun-ts/${SDK_VERSION}`;
|
|
4994
5015
|
|
|
4995
5016
|
// src/client.ts
|
|
@@ -6945,6 +6966,9 @@ import { privateKeyToAccount as privateKeyToAccount3 } from "viem/accounts";
|
|
|
6945
6966
|
var DEFAULT_API_URL3 = "https://blockrun.ai/api";
|
|
6946
6967
|
var DEFAULT_MODEL2 = "minimax/music-2.5+";
|
|
6947
6968
|
var DEFAULT_TIMEOUT3 = 21e4;
|
|
6969
|
+
var POLL_INTERVAL_MS2 = 5e3;
|
|
6970
|
+
var POLL_MAX_DURATION_MS2 = 3e5;
|
|
6971
|
+
var TRANSIENT_POLL_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
|
|
6948
6972
|
var MusicClient = class {
|
|
6949
6973
|
_account;
|
|
6950
6974
|
get account() {
|
|
@@ -7086,15 +7110,79 @@ var MusicClient = class {
|
|
|
7086
7110
|
}
|
|
7087
7111
|
throw new APIError(`API error after payment: ${retryResponse.status}`, retryResponse.status, sanitizeErrorResponse(errorBody));
|
|
7088
7112
|
}
|
|
7113
|
+
const costUsd = Number(details.amount) / 1e6;
|
|
7089
7114
|
const data = await retryResponse.json();
|
|
7090
|
-
if (
|
|
7115
|
+
if (retryResponse.status === 202 || data.status === "queued" || data.status === "in_progress") {
|
|
7116
|
+
if (!data.poll_url) {
|
|
7117
|
+
throw new APIError(
|
|
7118
|
+
`Server returned ${retryResponse.status} but no poll_url to follow.`,
|
|
7119
|
+
retryResponse.status,
|
|
7120
|
+
sanitizeErrorResponse(data)
|
|
7121
|
+
);
|
|
7122
|
+
}
|
|
7123
|
+
return this.pollMusicJob(data.poll_url, paymentPayload, costUsd);
|
|
7124
|
+
}
|
|
7125
|
+
return this.finishPaidResponse(retryResponse, data, costUsd);
|
|
7126
|
+
}
|
|
7127
|
+
finishPaidResponse(response, data, costUsd) {
|
|
7128
|
+
if (!settlementSkipped(response)) {
|
|
7091
7129
|
this.sessionCalls++;
|
|
7092
|
-
this.sessionTotalUsd +=
|
|
7130
|
+
this.sessionTotalUsd += costUsd;
|
|
7093
7131
|
}
|
|
7094
|
-
const txHash =
|
|
7132
|
+
const txHash = response.headers.get("x-payment-receipt");
|
|
7095
7133
|
if (txHash) data.txHash = txHash;
|
|
7096
7134
|
return data;
|
|
7097
7135
|
}
|
|
7136
|
+
/**
|
|
7137
|
+
* Poll a slow track until it completes, sending the SAME payment header each
|
|
7138
|
+
* time: the server binds the payer to the job and settles on the first poll
|
|
7139
|
+
* that sees it completed.
|
|
7140
|
+
*
|
|
7141
|
+
* Network errors and 502/503/504 keep polling. A response lost on the settling
|
|
7142
|
+
* poll would otherwise drop a track that was paid for, and polling again returns
|
|
7143
|
+
* it as already settled, with no second charge.
|
|
7144
|
+
*/
|
|
7145
|
+
async pollMusicJob(pollPath, paymentHeader, costUsd) {
|
|
7146
|
+
const pollUrl = pollPath.startsWith("/api/") ? `${this.apiUrl.replace(/\/api$/, "")}${pollPath}` : `${this.apiUrl}${pollPath.startsWith("/") ? "" : "/"}${pollPath}`;
|
|
7147
|
+
const startedAt = Date.now();
|
|
7148
|
+
while (Date.now() - startedAt < POLL_MAX_DURATION_MS2) {
|
|
7149
|
+
await new Promise((resolve2) => setTimeout(resolve2, POLL_INTERVAL_MS2));
|
|
7150
|
+
let pollResp;
|
|
7151
|
+
try {
|
|
7152
|
+
pollResp = await this.fetchWithTimeout(pollUrl, {
|
|
7153
|
+
method: "GET",
|
|
7154
|
+
headers: { "PAYMENT-SIGNATURE": paymentHeader }
|
|
7155
|
+
});
|
|
7156
|
+
} catch {
|
|
7157
|
+
continue;
|
|
7158
|
+
}
|
|
7159
|
+
if (pollResp.status === 202 || TRANSIENT_POLL_STATUSES.has(pollResp.status)) continue;
|
|
7160
|
+
const pollBody = await pollResp.json().catch(() => ({}));
|
|
7161
|
+
if (!pollResp.ok) {
|
|
7162
|
+
throw new APIError(
|
|
7163
|
+
`API error during poll of ${pollPath}: ${pollResp.status}`,
|
|
7164
|
+
pollResp.status,
|
|
7165
|
+
sanitizeErrorResponse(pollBody)
|
|
7166
|
+
);
|
|
7167
|
+
}
|
|
7168
|
+
if (pollBody.status === "failed") {
|
|
7169
|
+
throw new APIError(
|
|
7170
|
+
pollBody.error || "Upstream music generation failed",
|
|
7171
|
+
200,
|
|
7172
|
+
sanitizeErrorResponse(pollBody)
|
|
7173
|
+
);
|
|
7174
|
+
}
|
|
7175
|
+
if (pollBody.data) {
|
|
7176
|
+
if (!pollBody.txHash && pollBody.payment?.tx_hash) pollBody.txHash = pollBody.payment.tx_hash;
|
|
7177
|
+
return this.finishPaidResponse(pollResp, pollBody, costUsd);
|
|
7178
|
+
}
|
|
7179
|
+
}
|
|
7180
|
+
throw new APIError(
|
|
7181
|
+
`Music generation did not finish within ${POLL_MAX_DURATION_MS2 / 1e3}s. The job may still complete: poll poll_url with the same payment header before paying again.`,
|
|
7182
|
+
504,
|
|
7183
|
+
{ error: "poll_timeout", poll_url: pollPath }
|
|
7184
|
+
);
|
|
7185
|
+
}
|
|
7098
7186
|
async fetchWithTimeout(url, options) {
|
|
7099
7187
|
const controller = new AbortController();
|
|
7100
7188
|
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
@@ -7334,7 +7422,7 @@ import { privateKeyToAccount as privateKeyToAccount5 } from "viem/accounts";
|
|
|
7334
7422
|
var DEFAULT_API_URL5 = "https://blockrun.ai/api";
|
|
7335
7423
|
var DEFAULT_MODEL4 = "xai/grok-imagine-video";
|
|
7336
7424
|
var DEFAULT_TIMEOUT5 = 12e4;
|
|
7337
|
-
var
|
|
7425
|
+
var POLL_INTERVAL_MS3 = 5e3;
|
|
7338
7426
|
var DEFAULT_GENERATE_BUDGET_MS = 9e5;
|
|
7339
7427
|
var MAX_TIMEOUT_SECONDS = 600;
|
|
7340
7428
|
var MAX_POLL_RESIGNS = 2;
|
|
@@ -7545,7 +7633,7 @@ var VideoClient = class {
|
|
|
7545
7633
|
headers: { "Content-Type": "application/json" },
|
|
7546
7634
|
body: JSON.stringify(body)
|
|
7547
7635
|
});
|
|
7548
|
-
if (this.apiAuth) return this.apiAuth.poll(resp402, budgetMs,
|
|
7636
|
+
if (this.apiAuth) return this.apiAuth.poll(resp402, budgetMs, POLL_INTERVAL_MS3);
|
|
7549
7637
|
if (resp402.status !== 402) {
|
|
7550
7638
|
await this.throwApiError(resp402, "Expected 402 on first POST");
|
|
7551
7639
|
}
|
|
@@ -7580,7 +7668,7 @@ var VideoClient = class {
|
|
|
7580
7668
|
let lastStatus = submitData.status || "queued";
|
|
7581
7669
|
let resignsLeft = MAX_POLL_RESIGNS;
|
|
7582
7670
|
while (Date.now() < deadline) {
|
|
7583
|
-
await new Promise((r) => setTimeout(r,
|
|
7671
|
+
await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS3));
|
|
7584
7672
|
const pollResp = await this.fetchWithTimeout(pollUrl, {
|
|
7585
7673
|
method: "GET",
|
|
7586
7674
|
headers: { "PAYMENT-SIGNATURE": paymentPayload }
|
|
@@ -9313,7 +9401,7 @@ var BlockrunClient = class {
|
|
|
9313
9401
|
}
|
|
9314
9402
|
if (lastStatus === "failed") {
|
|
9315
9403
|
throw new APIError(
|
|
9316
|
-
`Upstream job failed: ${pollData.error || "unknown"}`,
|
|
9404
|
+
`Upstream job failed: ${pollData.error || "unknown"}${paymentFactsNote(pollData)}`,
|
|
9317
9405
|
pollResp.status,
|
|
9318
9406
|
sanitizeErrorResponse(pollData)
|
|
9319
9407
|
);
|
|
@@ -13285,7 +13373,8 @@ var SolanaLLMClient = class {
|
|
|
13285
13373
|
*
|
|
13286
13374
|
* The POST is verified, not charged. The job is then polled with a wallet
|
|
13287
13375
|
* signature (no payment) until it finishes, and ONE payment is signed for
|
|
13288
|
-
* the finished video; a
|
|
13376
|
+
* the finished video; a job that fails before completing is not charged. A paid
|
|
13377
|
+
* job can still fail, and its APIError then carries `payment_status`. If the gateway does not
|
|
13289
13378
|
* accept poll signatures for the job, it is polled with a payment per poll
|
|
13290
13379
|
* instead, which is likewise only charged on completion.
|
|
13291
13380
|
*
|
|
@@ -13653,7 +13742,8 @@ var SolanaLLMClient = class {
|
|
|
13653
13742
|
* cost at POST; this loop only fetches the result and never signs a payment.
|
|
13654
13743
|
* A batch-billed image job still answers its proof poll with 402: that job
|
|
13655
13744
|
* falls back to the per-poll payment below.
|
|
13656
|
-
* - video: settles on completion
|
|
13745
|
+
* - video: settles on completion. A job that fails before completing is not
|
|
13746
|
+
* charged; one that fails after payment says so in `payment_status`. A
|
|
13657
13747
|
* finished, unpaid job answers the proof poll with 402 `{status:"completed"}`
|
|
13658
13748
|
* and a normal x402 challenge; ONE payment is signed from that challenge and
|
|
13659
13749
|
* the same poll is re-sent with it. The cost is recorded here, on delivery.
|
|
@@ -13700,7 +13790,7 @@ var SolanaLLMClient = class {
|
|
|
13700
13790
|
const failIfFailed = (data, status2) => {
|
|
13701
13791
|
if (lastStatus === "failed") {
|
|
13702
13792
|
throw new APIError(
|
|
13703
|
-
`Upstream job failed: ${data.error || "unknown"}`,
|
|
13793
|
+
`Upstream job failed: ${data.error || "unknown"}${paymentFactsNote(data)}`,
|
|
13704
13794
|
status2,
|
|
13705
13795
|
sanitizeErrorResponse(data)
|
|
13706
13796
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blockrun/llm",
|
|
3
|
-
"version": "3.26.
|
|
3
|
+
"version": "3.26.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "TypeScript SDK for BlockRun - every frontier model behind one OpenAI-compatible client. Pay with a BlockRun API key or per-call in USDC. Smart routing picks the cheapest capable model. No rate limits.",
|
|
6
6
|
"main": "dist/index.cjs",
|