@blockrun/llm 4.2.1 → 4.3.1
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 +105 -36
- package/dist/index.cjs +188 -10
- package/dist/index.d.cts +222 -2
- package/dist/index.d.ts +222 -2
- package/dist/index.js +185 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -593,6 +593,51 @@ function withCost(body, response, bookedUsd, scheme) {
|
|
|
593
593
|
return body;
|
|
594
594
|
}
|
|
595
595
|
|
|
596
|
+
// src/decisions.ts
|
|
597
|
+
var DECISIONS_DEFAULT_MODEL = "gpt-6-luna";
|
|
598
|
+
var QUESTION_TYPES = /* @__PURE__ */ new Set(["predicate", "choice", "score"]);
|
|
599
|
+
var GEMINI_DECISIONS_MODEL = "gemini-decision-1";
|
|
600
|
+
function isGemini(model) {
|
|
601
|
+
return typeof model === "string" && model.trim().replace(/^google\//, "") === GEMINI_DECISIONS_MODEL;
|
|
602
|
+
}
|
|
603
|
+
function invalid(message) {
|
|
604
|
+
throw withDisposition(new Error(`decisions: ${message}`), "unpaid");
|
|
605
|
+
}
|
|
606
|
+
function buildDecisionsBody(request) {
|
|
607
|
+
if (!request || typeof request !== "object") invalid("request must be an object");
|
|
608
|
+
const { input, questions } = request;
|
|
609
|
+
const emptyInput = typeof input === "string" ? input.length === 0 : !Array.isArray(input) || input.length === 0;
|
|
610
|
+
if (emptyInput) invalid("input must be a non-empty string or a non-empty array of messages");
|
|
611
|
+
if (!Array.isArray(questions) || questions.length === 0) invalid("questions must be a non-empty array");
|
|
612
|
+
const names = /* @__PURE__ */ new Set();
|
|
613
|
+
questions.forEach((q, i) => {
|
|
614
|
+
if (!q || typeof q !== "object") invalid(`questions[${i}] must be an object`);
|
|
615
|
+
if (!QUESTION_TYPES.has(q.type)) invalid(`questions[${i}].type must be "predicate", "choice" or "score"`);
|
|
616
|
+
if (typeof q.name !== "string" || q.name.length === 0) invalid(`questions[${i}].name must be a non-empty string`);
|
|
617
|
+
if (typeof q.instructions !== "string" || q.instructions.length === 0) {
|
|
618
|
+
invalid(`questions[${i}].instructions must be a non-empty string`);
|
|
619
|
+
}
|
|
620
|
+
if (names.has(q.name)) invalid(`questions[${i}].name "${q.name}" is used twice; answers are keyed by name`);
|
|
621
|
+
names.add(q.name);
|
|
622
|
+
if (q.type === "choice" && (!Array.isArray(q.choices) || q.choices.length === 0)) {
|
|
623
|
+
invalid(`questions[${i}] (choice) needs a non-empty choices array`);
|
|
624
|
+
}
|
|
625
|
+
if (q.type === "score" && (!Array.isArray(q.levels) || q.levels.length === 0)) {
|
|
626
|
+
invalid(`questions[${i}] (score) needs a non-empty levels array, lowest first`);
|
|
627
|
+
}
|
|
628
|
+
if (isGemini(request.model) && (q.type === "choice" || q.type === "score")) {
|
|
629
|
+
const key = q.type === "choice" ? "choices" : "levels";
|
|
630
|
+
const items = (q.type === "choice" ? q.choices : q.levels) ?? [];
|
|
631
|
+
items.forEach((item, j) => {
|
|
632
|
+
if (!item || typeof item !== "object" || typeof item.description !== "string") {
|
|
633
|
+
invalid(`questions[${i}].${key}[${j}].description must be a string for ${GEMINI_DECISIONS_MODEL} (it may be empty: "")`);
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
return { ...request, model: request.model ?? DECISIONS_DEFAULT_MODEL };
|
|
639
|
+
}
|
|
640
|
+
|
|
596
641
|
// src/client.ts
|
|
597
642
|
import { privateKeyToAccount } from "viem/accounts";
|
|
598
643
|
|
|
@@ -5179,7 +5224,7 @@ function getCostSummary() {
|
|
|
5179
5224
|
}
|
|
5180
5225
|
|
|
5181
5226
|
// src/version.ts
|
|
5182
|
-
var SDK_VERSION = "4.
|
|
5227
|
+
var SDK_VERSION = "4.3.1";
|
|
5183
5228
|
var USER_AGENT = `blockrun-ts/${SDK_VERSION}`;
|
|
5184
5229
|
|
|
5185
5230
|
// src/client.ts
|
|
@@ -6399,6 +6444,36 @@ var LLMClient = class _LLMClient {
|
|
|
6399
6444
|
const data = await this.requestWithPaymentRaw("/v1/search", body);
|
|
6400
6445
|
return data;
|
|
6401
6446
|
}
|
|
6447
|
+
/**
|
|
6448
|
+
* Typed judgments (OpenAI Decisions-compatible, `gpt-6-luna`): send
|
|
6449
|
+
* evidence and questions with a fixed answer space, get one typed answer
|
|
6450
|
+
* per question instead of text.
|
|
6451
|
+
*
|
|
6452
|
+
* Priced on input tokens only. With a wallet the 402 quotes an upper-bound
|
|
6453
|
+
* estimate of them plus the $0.001 transaction fee, and that quote settles;
|
|
6454
|
+
* with an API key the account is billed on the tokens the provider reports.
|
|
6455
|
+
* A request over the gateway's limits is refused with a 400 before payment.
|
|
6456
|
+
*
|
|
6457
|
+
* A `choice` always picks one of your values, so give it a `none` value for
|
|
6458
|
+
* inputs that fit nothing.
|
|
6459
|
+
*
|
|
6460
|
+
* @example
|
|
6461
|
+
* const { answers } = await client.decisions({
|
|
6462
|
+
* input: 'My card was charged twice for one order.',
|
|
6463
|
+
* questions: [
|
|
6464
|
+
* { type: 'predicate', name: 'urgent', instructions: 'Does this need a reply today?' },
|
|
6465
|
+
* { type: 'choice', name: 'team', instructions: 'Which team owns this?',
|
|
6466
|
+
* choices: [{ value: 'billing', description: 'payments and refunds' },
|
|
6467
|
+
* { value: 'shipping', description: 'delivery' },
|
|
6468
|
+
* { value: 'none', description: 'fits no team' }] },
|
|
6469
|
+
* ],
|
|
6470
|
+
* });
|
|
6471
|
+
*/
|
|
6472
|
+
async decisions(request) {
|
|
6473
|
+
const body = buildDecisionsBody(request);
|
|
6474
|
+
const response = await this.requestWithPayment("/v1/decisions", body);
|
|
6475
|
+
return response;
|
|
6476
|
+
}
|
|
6402
6477
|
/**
|
|
6403
6478
|
* Generic Exa endpoint proxy (POST). Useful when you need an Exa API
|
|
6404
6479
|
* surface that the typed wrappers below don't expose.
|
|
@@ -8789,7 +8864,9 @@ var PriceClient = class {
|
|
|
8789
8864
|
assetType: data.assetType,
|
|
8790
8865
|
category: data.category,
|
|
8791
8866
|
source: data.source,
|
|
8792
|
-
free: data.free
|
|
8867
|
+
free: data.free,
|
|
8868
|
+
realtime: data.realtime,
|
|
8869
|
+
note: data.note
|
|
8793
8870
|
};
|
|
8794
8871
|
}
|
|
8795
8872
|
async history(category, symbol, options) {
|
|
@@ -10447,6 +10524,8 @@ var DEFAULT_BATCH_RATE_LIMIT = { maxAttempts: 3, maxWaitMs: 6e4 };
|
|
|
10447
10524
|
var DEFAULT_BATCH_IN_DOUBT = { maxWaitMs: 3e5 };
|
|
10448
10525
|
var DEFAULT_BATCH_LOCK_WAIT_MS = 6e4;
|
|
10449
10526
|
var LOCK_POLL_MS = 500;
|
|
10527
|
+
var DEFAULT_BATCH_BUSY_WAIT_MS = 6e4;
|
|
10528
|
+
var BUSY_POLL_MS = 100;
|
|
10450
10529
|
var BatchPaymentUnresolvedError = class extends PaymentError {
|
|
10451
10530
|
/** The {@link SolanaBatchEvent.reason} of the `unresolved` event reported with it. */
|
|
10452
10531
|
reason;
|
|
@@ -10508,6 +10587,21 @@ var BatchChannelLockedError = class extends BlockrunError {
|
|
|
10508
10587
|
withDisposition(this, "unpaid");
|
|
10509
10588
|
}
|
|
10510
10589
|
};
|
|
10590
|
+
var BatchChannelBusyError = class extends BlockrunError {
|
|
10591
|
+
/** The paying wallet's address. */
|
|
10592
|
+
wallet;
|
|
10593
|
+
/** How long this call waited for the wallet, in ms (0 unless `onBusy` is `"wait"`). */
|
|
10594
|
+
waitedMs;
|
|
10595
|
+
constructor(init) {
|
|
10596
|
+
super(
|
|
10597
|
+
`batch-settlement busy for wallet ${init.wallet}: another batch call is in flight` + (init.waitedMs > 0 ? ` (waited ${init.waitedMs} ms)` : "")
|
|
10598
|
+
);
|
|
10599
|
+
this.name = "BatchChannelBusyError";
|
|
10600
|
+
this.wallet = init.wallet;
|
|
10601
|
+
this.waitedMs = init.waitedMs;
|
|
10602
|
+
withDisposition(this, "unpaid");
|
|
10603
|
+
}
|
|
10604
|
+
};
|
|
10511
10605
|
var INSTALL_HINT2 = "npm install @x402/core@~2.30.0 @x402/svm@~2.30.0 @solana/kit@^8";
|
|
10512
10606
|
async function load(pkg, importer) {
|
|
10513
10607
|
try {
|
|
@@ -10687,20 +10781,20 @@ var FileIntentJournal = class _FileIntentJournal {
|
|
|
10687
10781
|
if (err.code === "ENOENT") return {};
|
|
10688
10782
|
throw err;
|
|
10689
10783
|
}
|
|
10690
|
-
const
|
|
10784
|
+
const invalid2 = (why) => new Error(`deposit-intent journal ${this.file} is not valid (${why})`);
|
|
10691
10785
|
let parsed;
|
|
10692
10786
|
try {
|
|
10693
10787
|
parsed = JSON.parse(text);
|
|
10694
10788
|
} catch (err) {
|
|
10695
|
-
throw
|
|
10789
|
+
throw invalid2(err instanceof Error ? err.message : String(err));
|
|
10696
10790
|
}
|
|
10697
|
-
if (!isPlainObject(parsed)) throw
|
|
10698
|
-
if (parsed.version !== 1) throw
|
|
10791
|
+
if (!isPlainObject(parsed)) throw invalid2("not a JSON object");
|
|
10792
|
+
if (parsed.version !== 1) throw invalid2(`unsupported version ${JSON.stringify(parsed.version)}`);
|
|
10699
10793
|
const { intents } = parsed;
|
|
10700
|
-
if (!isPlainObject(intents)) throw
|
|
10794
|
+
if (!isPlainObject(intents)) throw invalid2("intents is not an object");
|
|
10701
10795
|
for (const [key, intent] of Object.entries(intents)) {
|
|
10702
10796
|
const why = intentProblem(key, intent);
|
|
10703
|
-
if (why) throw
|
|
10797
|
+
if (why) throw invalid2(`intent ${JSON.stringify(key)}: ${why}`);
|
|
10704
10798
|
}
|
|
10705
10799
|
return intents;
|
|
10706
10800
|
}
|
|
@@ -11580,6 +11674,14 @@ var SolanaBatchPayer = class {
|
|
|
11580
11674
|
if (!Number.isFinite(this.lockWaitMs) || this.lockWaitMs < 0) {
|
|
11581
11675
|
throw new Error("batch.lockWaitMs must be a finite number of ms >= 0");
|
|
11582
11676
|
}
|
|
11677
|
+
this.onBusy = init.options.onBusy ?? "exact";
|
|
11678
|
+
if (this.onBusy !== "exact" && this.onBusy !== "wait" && this.onBusy !== "throw") {
|
|
11679
|
+
throw new Error('batch.onBusy must be "exact", "wait" or "throw"');
|
|
11680
|
+
}
|
|
11681
|
+
this.busyWaitMs = init.options.busyWaitMs ?? DEFAULT_BATCH_BUSY_WAIT_MS;
|
|
11682
|
+
if (!Number.isFinite(this.busyWaitMs) || this.busyWaitMs < 0) {
|
|
11683
|
+
throw new Error("batch.busyWaitMs must be a finite number of ms >= 0");
|
|
11684
|
+
}
|
|
11583
11685
|
}
|
|
11584
11686
|
init;
|
|
11585
11687
|
state;
|
|
@@ -11588,6 +11690,8 @@ var SolanaBatchPayer = class {
|
|
|
11588
11690
|
inDoubtMaxWaitMs;
|
|
11589
11691
|
onLocked;
|
|
11590
11692
|
lockWaitMs;
|
|
11693
|
+
onBusy;
|
|
11694
|
+
busyWaitMs;
|
|
11591
11695
|
counters = {
|
|
11592
11696
|
fallbacks: 0,
|
|
11593
11697
|
fallbacksByReason: {},
|
|
@@ -12049,6 +12153,22 @@ var SolanaBatchPayer = class {
|
|
|
12049
12153
|
});
|
|
12050
12154
|
return { kind: "failed", error };
|
|
12051
12155
|
}
|
|
12156
|
+
/**
|
|
12157
|
+
* `onBusy: "wait"`: look at the wallet every {@link BUSY_POLL_MS} until no
|
|
12158
|
+
* batch call holds it or `deadline` passes. True when it is free.
|
|
12159
|
+
*/
|
|
12160
|
+
async waitWhileBusy(wallet, deadline) {
|
|
12161
|
+
while (wallet.busy) {
|
|
12162
|
+
const left = deadline - Date.now();
|
|
12163
|
+
if (left <= 0) return false;
|
|
12164
|
+
await sleep2(Math.min(BUSY_POLL_MS, left));
|
|
12165
|
+
}
|
|
12166
|
+
return true;
|
|
12167
|
+
}
|
|
12168
|
+
/** `onBusy` other than `"exact"`: the call's `"unpaid"` error, nothing signed. */
|
|
12169
|
+
async busyOut(waitedMs) {
|
|
12170
|
+
return { kind: "failed", error: new BatchChannelBusyError({ wallet: await this.init.address(), waitedMs }) };
|
|
12171
|
+
}
|
|
12052
12172
|
/**
|
|
12053
12173
|
* The close fence for a call about to send its first request: how many
|
|
12054
12174
|
* closes this wallet has completed so far, to hand to {@link pay}.
|
|
@@ -12074,6 +12194,8 @@ var SolanaBatchPayer = class {
|
|
|
12074
12194
|
const budget = { attempt: 1, waited: 0 };
|
|
12075
12195
|
let waitedUntil = 0;
|
|
12076
12196
|
let backedOff;
|
|
12197
|
+
let busySince;
|
|
12198
|
+
let rechallengeAfterBusy = false;
|
|
12077
12199
|
let holding = false;
|
|
12078
12200
|
let sent;
|
|
12079
12201
|
const release = () => {
|
|
@@ -12109,7 +12231,8 @@ var SolanaBatchPayer = class {
|
|
|
12109
12231
|
await sleep2(wait);
|
|
12110
12232
|
continue;
|
|
12111
12233
|
}
|
|
12112
|
-
if (budget.attempt > 1 || budget.waited > 0) {
|
|
12234
|
+
if (budget.attempt > 1 || budget.waited > 0 || rechallengeAfterBusy) {
|
|
12235
|
+
rechallengeAfterBusy = false;
|
|
12113
12236
|
let fresh;
|
|
12114
12237
|
try {
|
|
12115
12238
|
fresh = await rechallenge();
|
|
@@ -12137,7 +12260,28 @@ var SolanaBatchPayer = class {
|
|
|
12137
12260
|
detail: "closeBatchChannel() closed this wallet's channel after this call started; it opens no channel"
|
|
12138
12261
|
});
|
|
12139
12262
|
}
|
|
12140
|
-
if (wallet.busy)
|
|
12263
|
+
if (wallet.busy) {
|
|
12264
|
+
if (this.onBusy === "exact") return fallback({ reason: "channel_busy", attempt: budget.attempt });
|
|
12265
|
+
const waited = busySince === void 0 ? 0 : Date.now() - busySince;
|
|
12266
|
+
if (this.onBusy === "throw") return this.busyOut(waited);
|
|
12267
|
+
if (busySince === void 0) {
|
|
12268
|
+
busySince = Date.now();
|
|
12269
|
+
if (this.busyWaitMs > 0) {
|
|
12270
|
+
await this.report({
|
|
12271
|
+
type: "backoff",
|
|
12272
|
+
reason: "channel_busy",
|
|
12273
|
+
retryAfterMs: this.busyWaitMs,
|
|
12274
|
+
attempt: budget.attempt,
|
|
12275
|
+
detail: 'another batch call for this wallet is in flight; waiting for it (onBusy: "wait")'
|
|
12276
|
+
});
|
|
12277
|
+
}
|
|
12278
|
+
}
|
|
12279
|
+
if (!await this.waitWhileBusy(wallet, busySince + this.busyWaitMs)) {
|
|
12280
|
+
return this.busyOut(Date.now() - busySince);
|
|
12281
|
+
}
|
|
12282
|
+
rechallengeAfterBusy = true;
|
|
12283
|
+
continue;
|
|
12284
|
+
}
|
|
12141
12285
|
if (wallet.file && !holdsChannelFile(wallet.file)) {
|
|
12142
12286
|
const detail = `this process no longer holds the lock on ${wallet.file}; another owner may be paying into its channels`;
|
|
12143
12287
|
if (this.onLocked !== "exact") return this.lockedOut(detail, void 0, 0);
|
|
@@ -13762,6 +13906,22 @@ var SolanaLLMClient = class {
|
|
|
13762
13906
|
const data = await this.requestWithPaymentRaw("/v1/search", body);
|
|
13763
13907
|
return data;
|
|
13764
13908
|
}
|
|
13909
|
+
/**
|
|
13910
|
+
* Typed judgments (OpenAI Decisions-compatible, `gpt-6-luna`), paid on
|
|
13911
|
+
* Solana: one typed answer per question instead of text. See
|
|
13912
|
+
* {@link LLMClient.decisions} for the request shape.
|
|
13913
|
+
*
|
|
13914
|
+
* Under `exact` the 402's quote settles (an upper-bound estimate of the
|
|
13915
|
+
* input tokens plus the $0.001 fee). With the `batch` option the call pays
|
|
13916
|
+
* with a channel voucher and is charged the input tokens the provider
|
|
13917
|
+
* reports, with no per-call fee; `costUsd` is that metered charge. It shares
|
|
13918
|
+
* the wallet's channel with chat, one batch call at a time.
|
|
13919
|
+
*/
|
|
13920
|
+
async decisions(request) {
|
|
13921
|
+
const body = buildDecisionsBody(request);
|
|
13922
|
+
const response = await this.requestWithPayment("/v1/decisions", body);
|
|
13923
|
+
return response;
|
|
13924
|
+
}
|
|
13765
13925
|
// ── Prediction Markets (Powered by Predexon) ────────────────────────────
|
|
13766
13926
|
async pm(path7, params) {
|
|
13767
13927
|
return this.getWithPaymentRaw(`/v1/pm/${path7}`, params);
|
|
@@ -14925,8 +15085,19 @@ var Chat = class {
|
|
|
14925
15085
|
this.completions = new ChatCompletions(client, apiUrl, timeout);
|
|
14926
15086
|
}
|
|
14927
15087
|
};
|
|
15088
|
+
var Decisions = class {
|
|
15089
|
+
constructor(client) {
|
|
15090
|
+
this.client = client;
|
|
15091
|
+
}
|
|
15092
|
+
client;
|
|
15093
|
+
/** Same as {@link LLMClient.decisions}. */
|
|
15094
|
+
async create(params) {
|
|
15095
|
+
return this.client.decisions(params);
|
|
15096
|
+
}
|
|
15097
|
+
};
|
|
14928
15098
|
var OpenAI = class {
|
|
14929
15099
|
chat;
|
|
15100
|
+
decisions;
|
|
14930
15101
|
get authMode() {
|
|
14931
15102
|
return this.client.authMode;
|
|
14932
15103
|
}
|
|
@@ -14943,6 +15114,7 @@ var OpenAI = class {
|
|
|
14943
15114
|
timeout
|
|
14944
15115
|
});
|
|
14945
15116
|
this.chat = new Chat(this.client, apiUrl, timeout);
|
|
15117
|
+
this.decisions = new Decisions(this.client);
|
|
14946
15118
|
}
|
|
14947
15119
|
/**
|
|
14948
15120
|
* Get the wallet address being used for payments.
|
|
@@ -15090,11 +15262,14 @@ export {
|
|
|
15090
15262
|
AnthropicClient,
|
|
15091
15263
|
BASE_CHAIN_ID,
|
|
15092
15264
|
BLOCKRUN_SOL_OPERATOR,
|
|
15265
|
+
BatchChannelBusyError,
|
|
15093
15266
|
BatchChannelLockedError,
|
|
15094
15267
|
BatchCloseDeferredError,
|
|
15095
15268
|
BatchPaymentUnresolvedError,
|
|
15096
15269
|
BlockrunClient,
|
|
15097
15270
|
BlockrunError,
|
|
15271
|
+
DECISIONS_DEFAULT_MODEL,
|
|
15272
|
+
DEFAULT_BATCH_BUSY_WAIT_MS,
|
|
15098
15273
|
DEFAULT_BATCH_IN_DOUBT,
|
|
15099
15274
|
DEFAULT_BATCH_LOCK_WAIT_MS,
|
|
15100
15275
|
DEFAULT_BATCH_RATE_LIMIT,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blockrun/llm",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.1",
|
|
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",
|