@agi-cli/sdk 0.1.128 → 0.1.129
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/package.json
CHANGED
|
@@ -72,9 +72,8 @@ function buildSolforgeEntry(base: CatalogMap): ProviderCatalogEntry | null {
|
|
|
72
72
|
id: SOLFORGE_ID,
|
|
73
73
|
label: 'Solforge',
|
|
74
74
|
env: ['SOLFORGE_PRIVATE_KEY'],
|
|
75
|
-
api: 'https://
|
|
76
|
-
doc: 'https://
|
|
77
|
-
npm: '@ai-sdk/openai-compatible',
|
|
75
|
+
api: 'https://router.solforge.sh/v1',
|
|
76
|
+
doc: 'https://router.solforge.sh/docs',
|
|
78
77
|
models: solforgeModels,
|
|
79
78
|
};
|
|
80
79
|
}
|
|
@@ -5,14 +5,22 @@ import { createPaymentHeader } from 'x402/client';
|
|
|
5
5
|
import type { PaymentRequirements } from 'x402/types';
|
|
6
6
|
import { svm } from 'x402/shared';
|
|
7
7
|
import nacl from 'tweetnacl';
|
|
8
|
-
import {
|
|
8
|
+
import { createOpenAI } from '@ai-sdk/openai';
|
|
9
|
+
import { createAnthropic } from '@ai-sdk/anthropic';
|
|
9
10
|
|
|
10
|
-
const DEFAULT_BASE_URL = 'https://
|
|
11
|
-
const DEFAULT_RPC_URL = 'https://api.
|
|
12
|
-
const DEFAULT_TOPUP_AMOUNT = '
|
|
11
|
+
const DEFAULT_BASE_URL = 'https://router.solforge.sh';
|
|
12
|
+
const DEFAULT_RPC_URL = 'https://api.devnet.solana.com';
|
|
13
|
+
const DEFAULT_TOPUP_AMOUNT = '5000000'; // $5.00
|
|
13
14
|
const DEFAULT_MAX_ATTEMPTS = 3;
|
|
14
15
|
const DEFAULT_MAX_PAYMENT_ATTEMPTS = 20;
|
|
15
16
|
|
|
17
|
+
export type SolforgePaymentCallbacks = {
|
|
18
|
+
onPaymentRequired?: (amountUsd: number) => void;
|
|
19
|
+
onPaymentSigning?: () => void;
|
|
20
|
+
onPaymentComplete?: (data: { amountUsd: number; newBalance: number }) => void;
|
|
21
|
+
onPaymentError?: (error: string) => void;
|
|
22
|
+
};
|
|
23
|
+
|
|
16
24
|
export type SolforgeProviderOptions = {
|
|
17
25
|
baseURL?: string;
|
|
18
26
|
rpcURL?: string;
|
|
@@ -20,6 +28,8 @@ export type SolforgeProviderOptions = {
|
|
|
20
28
|
topupAmountMicroUsdc?: string;
|
|
21
29
|
maxRequestAttempts?: number;
|
|
22
30
|
maxPaymentAttempts?: number;
|
|
31
|
+
callbacks?: SolforgePaymentCallbacks;
|
|
32
|
+
providerNpm?: string;
|
|
23
33
|
};
|
|
24
34
|
|
|
25
35
|
export type SolforgeAuth = {
|
|
@@ -46,8 +56,10 @@ type PaymentPayload = {
|
|
|
46
56
|
};
|
|
47
57
|
|
|
48
58
|
type PaymentResponse = {
|
|
49
|
-
amount_usd
|
|
50
|
-
new_balance
|
|
59
|
+
amount_usd?: number | string;
|
|
60
|
+
new_balance?: number | string;
|
|
61
|
+
amount?: number;
|
|
62
|
+
balance?: number;
|
|
51
63
|
};
|
|
52
64
|
|
|
53
65
|
export function createSolforgeFetch(
|
|
@@ -63,6 +75,7 @@ export function createSolforgeFetch(
|
|
|
63
75
|
const maxAttempts = options.maxRequestAttempts ?? DEFAULT_MAX_ATTEMPTS;
|
|
64
76
|
const maxPaymentAttempts =
|
|
65
77
|
options.maxPaymentAttempts ?? DEFAULT_MAX_PAYMENT_ATTEMPTS;
|
|
78
|
+
const callbacks = options.callbacks ?? {};
|
|
66
79
|
|
|
67
80
|
const baseFetch = globalThis.fetch.bind(globalThis);
|
|
68
81
|
let paymentAttempts = 0;
|
|
@@ -99,19 +112,25 @@ export function createSolforgeFetch(
|
|
|
99
112
|
const payload = await response.json().catch(() => ({}));
|
|
100
113
|
const requirement = pickPaymentRequirement(payload, targetTopup);
|
|
101
114
|
if (!requirement) {
|
|
115
|
+
callbacks.onPaymentError?.('Unsupported payment requirement');
|
|
102
116
|
throw new Error('Solforge: unsupported payment requirement');
|
|
103
117
|
}
|
|
104
118
|
if (attempt >= maxAttempts) {
|
|
119
|
+
callbacks.onPaymentError?.('Payment failed after multiple attempts');
|
|
105
120
|
throw new Error('Solforge: payment failed after multiple attempts');
|
|
106
121
|
}
|
|
107
122
|
|
|
108
123
|
const remainingPayments = maxPaymentAttempts - paymentAttempts;
|
|
109
124
|
if (remainingPayments <= 0) {
|
|
125
|
+
callbacks.onPaymentError?.('Maximum payment attempts exceeded');
|
|
110
126
|
throw new Error(
|
|
111
127
|
'Solforge: payment failed after maximum payment attempts.',
|
|
112
128
|
);
|
|
113
129
|
}
|
|
114
130
|
|
|
131
|
+
const amountUsd = parseInt(requirement.maxAmountRequired, 10) / 1_000_000;
|
|
132
|
+
callbacks.onPaymentRequired?.(amountUsd);
|
|
133
|
+
|
|
115
134
|
const outcome = await handlePayment({
|
|
116
135
|
requirement,
|
|
117
136
|
keypair,
|
|
@@ -120,6 +139,7 @@ export function createSolforgeFetch(
|
|
|
120
139
|
baseFetch,
|
|
121
140
|
buildWalletHeaders,
|
|
122
141
|
maxAttempts: remainingPayments,
|
|
142
|
+
callbacks,
|
|
123
143
|
});
|
|
124
144
|
paymentAttempts += outcome.attemptsUsed;
|
|
125
145
|
}
|
|
@@ -128,6 +148,15 @@ export function createSolforgeFetch(
|
|
|
128
148
|
};
|
|
129
149
|
}
|
|
130
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Create a Solforge-backed AI model.
|
|
153
|
+
*
|
|
154
|
+
* Uses native AI SDK providers:
|
|
155
|
+
* - OpenAI models → /v1/responses (via @ai-sdk/openai)
|
|
156
|
+
* - Anthropic models → /v1/messages (via @ai-sdk/anthropic)
|
|
157
|
+
*
|
|
158
|
+
* Provider is determined by options.providerNpm from catalog.
|
|
159
|
+
*/
|
|
131
160
|
export function createSolforgeModel(
|
|
132
161
|
model: string,
|
|
133
162
|
auth: SolforgeAuth,
|
|
@@ -136,14 +165,25 @@ export function createSolforgeModel(
|
|
|
136
165
|
const baseURL = `${trimTrailingSlash(
|
|
137
166
|
options.baseURL ?? DEFAULT_BASE_URL,
|
|
138
167
|
)}/v1`;
|
|
139
|
-
const
|
|
140
|
-
const
|
|
141
|
-
|
|
168
|
+
const customFetch = createSolforgeFetch(auth, options);
|
|
169
|
+
const providerNpm = options.providerNpm ?? '@ai-sdk/openai';
|
|
170
|
+
|
|
171
|
+
if (providerNpm === '@ai-sdk/anthropic') {
|
|
172
|
+
const anthropic = createAnthropic({
|
|
173
|
+
baseURL,
|
|
174
|
+
apiKey: 'solforge-wallet-auth',
|
|
175
|
+
fetch: customFetch,
|
|
176
|
+
});
|
|
177
|
+
return anthropic(model);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Default to OpenAI
|
|
181
|
+
const openai = createOpenAI({
|
|
142
182
|
baseURL,
|
|
143
|
-
|
|
144
|
-
fetch,
|
|
183
|
+
apiKey: 'solforge-wallet-auth',
|
|
184
|
+
fetch: customFetch,
|
|
145
185
|
});
|
|
146
|
-
return
|
|
186
|
+
return openai(model);
|
|
147
187
|
}
|
|
148
188
|
|
|
149
189
|
function trimTrailingSlash(url: string) {
|
|
@@ -192,6 +232,7 @@ async function handlePayment(args: {
|
|
|
192
232
|
baseFetch: typeof fetch;
|
|
193
233
|
buildWalletHeaders: () => Record<string, string>;
|
|
194
234
|
maxAttempts: number;
|
|
235
|
+
callbacks: SolforgePaymentCallbacks;
|
|
195
236
|
}): Promise<{ attemptsUsed: number }> {
|
|
196
237
|
let attempts = 0;
|
|
197
238
|
while (attempts < args.maxAttempts) {
|
|
@@ -226,7 +267,10 @@ async function processSinglePayment(args: {
|
|
|
226
267
|
baseURL: string;
|
|
227
268
|
baseFetch: typeof fetch;
|
|
228
269
|
buildWalletHeaders: () => Record<string, string>;
|
|
270
|
+
callbacks: SolforgePaymentCallbacks;
|
|
229
271
|
}): Promise<{ attempts: number; balance?: number | string }> {
|
|
272
|
+
args.callbacks.onPaymentSigning?.();
|
|
273
|
+
|
|
230
274
|
const paymentPayload = await createPaymentPayload(args);
|
|
231
275
|
const walletHeaders = args.buildWalletHeaders();
|
|
232
276
|
const headers = {
|
|
@@ -251,6 +295,7 @@ async function processSinglePayment(args: {
|
|
|
251
295
|
console.log('Solforge payment already processed; continuing.');
|
|
252
296
|
return { attempts: 1 };
|
|
253
297
|
}
|
|
298
|
+
args.callbacks.onPaymentError?.(`Topup failed: ${response.status}`);
|
|
254
299
|
throw new Error(`Solforge topup failed (${response.status}): ${rawBody}`);
|
|
255
300
|
}
|
|
256
301
|
|
|
@@ -262,10 +307,22 @@ async function processSinglePayment(args: {
|
|
|
262
307
|
}
|
|
263
308
|
|
|
264
309
|
if (parsed) {
|
|
310
|
+
const amountUsd =
|
|
311
|
+
typeof parsed.amount_usd === 'string'
|
|
312
|
+
? parseFloat(parsed.amount_usd)
|
|
313
|
+
: (parsed.amount_usd ?? parsed.amount ?? 0);
|
|
314
|
+
const newBalance =
|
|
315
|
+
typeof parsed.new_balance === 'string'
|
|
316
|
+
? parseFloat(parsed.new_balance)
|
|
317
|
+
: (parsed.new_balance ?? parsed.balance ?? 0);
|
|
318
|
+
args.callbacks.onPaymentComplete?.({
|
|
319
|
+
amountUsd,
|
|
320
|
+
newBalance,
|
|
321
|
+
});
|
|
265
322
|
console.log(
|
|
266
|
-
`Solforge payment complete: +$${
|
|
323
|
+
`Solforge payment complete: +$${amountUsd} (balance: $${newBalance})`,
|
|
267
324
|
);
|
|
268
|
-
return { attempts: 1, balance:
|
|
325
|
+
return { attempts: 1, balance: newBalance };
|
|
269
326
|
}
|
|
270
327
|
console.log('Solforge payment complete.');
|
|
271
328
|
return { attempts: 1 };
|