@ottocode/ai-sdk 0.1.6 → 0.1.7

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ottocode/ai-sdk",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
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
- return new Response(response.body.pipeThrough(transform), {
107
- status: response.status,
108
- statusText: response.statusText,
109
- headers: response.headers,
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: typeof fetch;
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: typeof fetch;
212
+ baseFetch: FetchFunction;
212
213
  maxAttempts: number;
213
214
  callbacks: PaymentCallbacks;
214
215
  }): Promise<{ attemptsUsed: number }> {
package/src/setu.ts CHANGED
@@ -62,6 +62,7 @@ export function createSetu(config: SetuConfig): SetuInstance {
62
62
  const setuFetch = createSetuFetch({
63
63
  wallet,
64
64
  baseURL,
65
+ fetch: config.fetch,
65
66
  rpcURL: config.rpcURL,
66
67
  callbacks: config.callbacks,
67
68
  cache: config.cache,
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>;