@ottocode/ai-sdk 0.1.6 → 0.1.8
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 +1 -1
- package/src/balance.ts +70 -1
- package/src/fetch.ts +34 -24
- package/src/payment.ts +3 -2
- package/src/setu.ts +1 -0
- package/src/types.ts +28 -0
package/package.json
CHANGED
package/src/balance.ts
CHANGED
|
@@ -45,9 +45,36 @@ export async function fetchBalance(
|
|
|
45
45
|
request_count: number;
|
|
46
46
|
created_at?: string;
|
|
47
47
|
last_request?: string;
|
|
48
|
+
scope?: 'wallet' | 'account';
|
|
49
|
+
payg?: {
|
|
50
|
+
wallet_balance_usd: number;
|
|
51
|
+
account_balance_usd: number;
|
|
52
|
+
raw_pool_usd: number;
|
|
53
|
+
effective_spendable_usd: number;
|
|
54
|
+
};
|
|
55
|
+
limits?: {
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
daily_limit_usd: number | null;
|
|
58
|
+
daily_spent_usd: number;
|
|
59
|
+
daily_remaining_usd: number | null;
|
|
60
|
+
monthly_limit_usd: number | null;
|
|
61
|
+
monthly_spent_usd: number;
|
|
62
|
+
monthly_remaining_usd: number | null;
|
|
63
|
+
cap_remaining_usd: number | null;
|
|
64
|
+
} | null;
|
|
65
|
+
subscription?: {
|
|
66
|
+
active: boolean;
|
|
67
|
+
tier_id?: string;
|
|
68
|
+
tier_name?: string;
|
|
69
|
+
credits_included?: number;
|
|
70
|
+
credits_used?: number;
|
|
71
|
+
credits_remaining?: number;
|
|
72
|
+
period_start?: string;
|
|
73
|
+
period_end?: string;
|
|
74
|
+
} | null;
|
|
48
75
|
};
|
|
49
76
|
|
|
50
|
-
|
|
77
|
+
const result: BalanceResponse = {
|
|
51
78
|
walletAddress: data.wallet_address,
|
|
52
79
|
balance: data.balance_usd,
|
|
53
80
|
totalSpent: data.total_spent,
|
|
@@ -55,7 +82,49 @@ export async function fetchBalance(
|
|
|
55
82
|
requestCount: data.request_count,
|
|
56
83
|
createdAt: data.created_at,
|
|
57
84
|
lastRequest: data.last_request,
|
|
85
|
+
scope: data.scope,
|
|
58
86
|
};
|
|
87
|
+
|
|
88
|
+
if (data.payg) {
|
|
89
|
+
result.payg = {
|
|
90
|
+
walletBalanceUsd: data.payg.wallet_balance_usd,
|
|
91
|
+
accountBalanceUsd: data.payg.account_balance_usd,
|
|
92
|
+
rawPoolUsd: data.payg.raw_pool_usd,
|
|
93
|
+
effectiveSpendableUsd: data.payg.effective_spendable_usd,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (data.limits !== undefined) {
|
|
98
|
+
result.limits = data.limits
|
|
99
|
+
? {
|
|
100
|
+
enabled: data.limits.enabled,
|
|
101
|
+
dailyLimitUsd: data.limits.daily_limit_usd,
|
|
102
|
+
dailySpentUsd: data.limits.daily_spent_usd,
|
|
103
|
+
dailyRemainingUsd: data.limits.daily_remaining_usd,
|
|
104
|
+
monthlyLimitUsd: data.limits.monthly_limit_usd,
|
|
105
|
+
monthlySpentUsd: data.limits.monthly_spent_usd,
|
|
106
|
+
monthlyRemainingUsd: data.limits.monthly_remaining_usd,
|
|
107
|
+
capRemainingUsd: data.limits.cap_remaining_usd,
|
|
108
|
+
}
|
|
109
|
+
: null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (data.subscription !== undefined) {
|
|
113
|
+
result.subscription = data.subscription
|
|
114
|
+
? {
|
|
115
|
+
active: data.subscription.active,
|
|
116
|
+
tierId: data.subscription.tier_id,
|
|
117
|
+
tierName: data.subscription.tier_name,
|
|
118
|
+
creditsIncluded: data.subscription.credits_included,
|
|
119
|
+
creditsUsed: data.subscription.credits_used,
|
|
120
|
+
creditsRemaining: data.subscription.credits_remaining,
|
|
121
|
+
periodStart: data.subscription.period_start,
|
|
122
|
+
periodEnd: data.subscription.period_end,
|
|
123
|
+
}
|
|
124
|
+
: null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return result;
|
|
59
128
|
} catch {
|
|
60
129
|
return null;
|
|
61
130
|
}
|
package/src/fetch.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
PaymentOptions,
|
|
5
5
|
CacheOptions,
|
|
6
6
|
BalanceUpdate,
|
|
7
|
+
FetchFunction,
|
|
7
8
|
} from './types.ts';
|
|
8
9
|
import { pickPaymentRequirement, handlePayment } from './payment.ts';
|
|
9
10
|
import { addAnthropicCacheControl } from './cache.ts';
|
|
@@ -80,34 +81,41 @@ function wrapResponseWithBalanceSniffing(
|
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
if (!response.body) return response;
|
|
84
|
+
if (typeof TransformStream === 'undefined') return response;
|
|
85
|
+
if (typeof response.body.pipeThrough !== 'function') return response;
|
|
83
86
|
|
|
84
87
|
const onBalanceUpdate = callbacks.onBalanceUpdate;
|
|
85
88
|
let partial = '';
|
|
86
89
|
const decoder = new TextDecoder();
|
|
87
|
-
const transform = new TransformStream<Uint8Array, Uint8Array>({
|
|
88
|
-
transform(chunk, controller) {
|
|
89
|
-
controller.enqueue(chunk);
|
|
90
|
-
partial += decoder.decode(chunk, { stream: true });
|
|
91
|
-
let nlIndex = partial.indexOf('\n');
|
|
92
|
-
while (nlIndex !== -1) {
|
|
93
|
-
const line = partial.slice(0, nlIndex);
|
|
94
|
-
partial = partial.slice(nlIndex + 1);
|
|
95
|
-
tryParseSetuComment(line, onBalanceUpdate);
|
|
96
|
-
nlIndex = partial.indexOf('\n');
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
flush() {
|
|
100
|
-
if (partial.trim()) {
|
|
101
|
-
tryParseSetuComment(partial, onBalanceUpdate);
|
|
102
|
-
}
|
|
103
|
-
},
|
|
104
|
-
});
|
|
105
90
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
91
|
+
try {
|
|
92
|
+
const transform = new TransformStream<Uint8Array, Uint8Array>({
|
|
93
|
+
transform(chunk, controller) {
|
|
94
|
+
controller.enqueue(chunk);
|
|
95
|
+
partial += decoder.decode(chunk, { stream: true });
|
|
96
|
+
let nlIndex = partial.indexOf('\n');
|
|
97
|
+
while (nlIndex !== -1) {
|
|
98
|
+
const line = partial.slice(0, nlIndex);
|
|
99
|
+
partial = partial.slice(nlIndex + 1);
|
|
100
|
+
tryParseSetuComment(line, onBalanceUpdate);
|
|
101
|
+
nlIndex = partial.indexOf('\n');
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
flush() {
|
|
105
|
+
if (partial.trim()) {
|
|
106
|
+
tryParseSetuComment(partial, onBalanceUpdate);
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return new Response(response.body.pipeThrough(transform), {
|
|
112
|
+
status: response.status,
|
|
113
|
+
statusText: response.statusText,
|
|
114
|
+
headers: response.headers,
|
|
115
|
+
});
|
|
116
|
+
} catch {
|
|
117
|
+
return response;
|
|
118
|
+
}
|
|
111
119
|
}
|
|
112
120
|
|
|
113
121
|
async function getWalletUsdcBalance(
|
|
@@ -154,6 +162,7 @@ async function getWalletUsdcBalance(
|
|
|
154
162
|
export interface CreateSetuFetchOptions {
|
|
155
163
|
wallet: WalletContext;
|
|
156
164
|
baseURL: string;
|
|
165
|
+
fetch?: FetchFunction;
|
|
157
166
|
rpcURL?: string;
|
|
158
167
|
callbacks?: PaymentCallbacks;
|
|
159
168
|
cache?: CacheOptions;
|
|
@@ -164,6 +173,7 @@ export function createSetuFetch(options: CreateSetuFetchOptions) {
|
|
|
164
173
|
const {
|
|
165
174
|
wallet,
|
|
166
175
|
baseURL,
|
|
176
|
+
fetch: customFetch,
|
|
167
177
|
rpcURL = DEFAULT_RPC_URL,
|
|
168
178
|
callbacks = {},
|
|
169
179
|
cache,
|
|
@@ -175,7 +185,7 @@ export function createSetuFetch(options: CreateSetuFetchOptions) {
|
|
|
175
185
|
payment?.maxPaymentAttempts ?? DEFAULT_MAX_PAYMENT_ATTEMPTS;
|
|
176
186
|
const topupApprovalMode = payment?.topupApprovalMode ?? 'auto';
|
|
177
187
|
const autoPayThresholdUsd = payment?.autoPayThresholdUsd ?? 0;
|
|
178
|
-
const baseFetch = globalThis.fetch.bind(globalThis);
|
|
188
|
+
const baseFetch = customFetch ?? globalThis.fetch.bind(globalThis);
|
|
179
189
|
|
|
180
190
|
return async (
|
|
181
191
|
input: Parameters<typeof fetch>[0],
|
package/src/payment.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
ExactPaymentRequirement,
|
|
8
8
|
PaymentPayload,
|
|
9
9
|
PaymentCallbacks,
|
|
10
|
+
FetchFunction,
|
|
10
11
|
} from './types.ts';
|
|
11
12
|
import {
|
|
12
13
|
address,
|
|
@@ -136,7 +137,7 @@ export async function processSinglePayment(args: {
|
|
|
136
137
|
wallet: WalletContext;
|
|
137
138
|
rpcURL: string;
|
|
138
139
|
baseURL: string;
|
|
139
|
-
baseFetch:
|
|
140
|
+
baseFetch: FetchFunction;
|
|
140
141
|
callbacks: PaymentCallbacks;
|
|
141
142
|
}): Promise<{ attempts: number; balance?: number | string }> {
|
|
142
143
|
args.callbacks.onPaymentSigning?.();
|
|
@@ -208,7 +209,7 @@ export async function handlePayment(args: {
|
|
|
208
209
|
wallet: WalletContext;
|
|
209
210
|
rpcURL: string;
|
|
210
211
|
baseURL: string;
|
|
211
|
-
baseFetch:
|
|
212
|
+
baseFetch: FetchFunction;
|
|
212
213
|
maxAttempts: number;
|
|
213
214
|
callbacks: PaymentCallbacks;
|
|
214
215
|
}): Promise<{ attemptsUsed: number }> {
|
package/src/setu.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -91,6 +91,7 @@ export interface PaymentOptions {
|
|
|
91
91
|
export interface SetuConfig {
|
|
92
92
|
auth: SetuAuth;
|
|
93
93
|
baseURL?: string;
|
|
94
|
+
fetch?: FetchFunction;
|
|
94
95
|
rpcURL?: string;
|
|
95
96
|
providers?: ProviderConfig[];
|
|
96
97
|
modelMap?: Record<string, ProviderId>;
|
|
@@ -127,6 +128,33 @@ export interface BalanceResponse {
|
|
|
127
128
|
requestCount: number;
|
|
128
129
|
createdAt?: string;
|
|
129
130
|
lastRequest?: string;
|
|
131
|
+
scope?: 'wallet' | 'account';
|
|
132
|
+
payg?: {
|
|
133
|
+
walletBalanceUsd: number;
|
|
134
|
+
accountBalanceUsd: number;
|
|
135
|
+
rawPoolUsd: number;
|
|
136
|
+
effectiveSpendableUsd: number;
|
|
137
|
+
};
|
|
138
|
+
limits?: {
|
|
139
|
+
enabled: boolean;
|
|
140
|
+
dailyLimitUsd: number | null;
|
|
141
|
+
dailySpentUsd: number;
|
|
142
|
+
dailyRemainingUsd: number | null;
|
|
143
|
+
monthlyLimitUsd: number | null;
|
|
144
|
+
monthlySpentUsd: number;
|
|
145
|
+
monthlyRemainingUsd: number | null;
|
|
146
|
+
capRemainingUsd: number | null;
|
|
147
|
+
} | null;
|
|
148
|
+
subscription?: {
|
|
149
|
+
active: boolean;
|
|
150
|
+
tierId?: string;
|
|
151
|
+
tierName?: string;
|
|
152
|
+
creditsIncluded?: number;
|
|
153
|
+
creditsUsed?: number;
|
|
154
|
+
creditsRemaining?: number;
|
|
155
|
+
periodStart?: string;
|
|
156
|
+
periodEnd?: string;
|
|
157
|
+
} | null;
|
|
130
158
|
}
|
|
131
159
|
|
|
132
160
|
export interface WalletUsdcBalance {
|