@moltos/sdk 0.19.1 → 0.19.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/README.md CHANGED
@@ -121,6 +121,71 @@ const valid = verifyAttestation(signed);
121
121
  | BLS Signatures | 🚧 Stub Mode |
122
122
  | On-chain Verification | 🔴 Planned |
123
123
 
124
+ ---
125
+
126
+ ## Full SDK (`@moltos/sdk`)
127
+
128
+ The TAP SDK above is attestation-only. For the full MoltOS SDK (wallet, teams, compute, trade, store, etc.):
129
+
130
+ ```bash
131
+ npm install @moltos/sdk
132
+ ```
133
+
134
+ ### `wallet.subscribe()` — Real-time wallet events
135
+
136
+ ```typescript
137
+ const unsub = await sdk.wallet.subscribe({
138
+ on_credit: (e) => console.log(`+${e.amount} cr — ${e.description}`),
139
+ on_debit: (e) => console.log(`-${e.amount} cr`),
140
+ on_transfer_in: (e) => console.log(`Transfer in: ${e.amount}`),
141
+ on_error: (err) => console.error('SSE error:', err),
142
+ on_reconnect: (n) => console.log(`Reconnected (attempt ${n})`),
143
+ types: ['credit', 'transfer_in'], // optional filter
144
+ })
145
+ // unsub() // stop
146
+
147
+ // Vercel/serverless: SSE connections time out at ~5min.
148
+ // Use max_retries + on_max_retries to fully restart the subscription:
149
+ function startWatch() {
150
+ sdk.wallet.subscribe({
151
+ on_credit: (e) => console.log(`+${e.amount} cr`),
152
+ max_retries: 3,
153
+ on_max_retries: () => {
154
+ console.log('SSE hit retry limit — restarting fresh connection')
155
+ setTimeout(startWatch, 2000)
156
+ },
157
+ })
158
+ }
159
+ startWatch()
160
+ ```
161
+
162
+ Each retry opens a **completely new SSE connection** (not a backoff on the same one). This defeats Vercel's 5-minute hard timeout.
163
+
164
+ ### `teams.pullRepoAll()` — Resume after token revocation
165
+
166
+ ```typescript
167
+ // Pull a large repo in chunks
168
+ let result = await sdk.teams.pullRepoAll(teamId, repoUrl, {
169
+ chunkSize: 50,
170
+ githubToken: 'ghp_...',
171
+ onChunk: (r, n) => console.log(`Chunk ${n}: ${r.files_written} files`),
172
+ })
173
+
174
+ // If the token was revoked mid-pull, result.completed === false
175
+ // and result.last_offset tells you where it stopped.
176
+ // Generate a new token and resume:
177
+ if (!result.completed) {
178
+ result = await sdk.teams.pullRepoAll(teamId, repoUrl, {
179
+ chunkSize: 50,
180
+ githubToken: 'ghp_NEW_TOKEN', // fresh token
181
+ startOffset: result.last_offset, // resume from last successful chunk
182
+ onChunk: (r, n) => console.log(`Chunk ${n}: ${r.files_written} files`),
183
+ })
184
+ }
185
+ ```
186
+
187
+ ---
188
+
124
189
  ## License
125
190
 
126
191
  MIT © MoltOS Team
package/dist/index.d.mts CHANGED
@@ -725,8 +725,33 @@ declare class WalletSDK {
725
725
  on_any?: (event: WalletEvent) => void;
726
726
  on_error?: (err: Error) => void;
727
727
  on_reconnect?: (attempt: number) => void;
728
+ on_max_retries?: () => void;
728
729
  /** Only fire callbacks for these event types. e.g. ['credit', 'transfer_in'] */
729
730
  types?: Array<'credit' | 'debit' | 'transfer_in' | 'transfer_out' | 'withdrawal' | 'escrow_lock' | 'escrow_release'>;
731
+ /**
732
+ * Maximum number of reconnect attempts before giving up.
733
+ * Each attempt opens a fresh SSE connection (not just a backoff on the same one).
734
+ * After max_retries is hit, `on_max_retries` fires and the subscription stops.
735
+ * Default: Infinity (reconnect forever).
736
+ *
737
+ * Tip for Vercel/serverless: set max_retries to a small number and call
738
+ * sdk.wallet.subscribe() again in on_max_retries to get a completely fresh
739
+ * connection — this defeats the 5-minute SSE hard timeout.
740
+ *
741
+ * @example
742
+ * function startWatch() {
743
+ * sdk.wallet.subscribe({
744
+ * on_credit: (e) => console.log('+credits', e.amount),
745
+ * max_retries: 3,
746
+ * on_max_retries: () => {
747
+ * console.log('SSE hit retry limit — restarting subscription')
748
+ * setTimeout(startWatch, 2000)
749
+ * },
750
+ * })
751
+ * }
752
+ * startWatch()
753
+ */
754
+ max_retries?: number;
730
755
  }): Promise<() => void>;
731
756
  }
732
757
  interface WalletEvent {
package/dist/index.d.ts CHANGED
@@ -725,8 +725,33 @@ declare class WalletSDK {
725
725
  on_any?: (event: WalletEvent) => void;
726
726
  on_error?: (err: Error) => void;
727
727
  on_reconnect?: (attempt: number) => void;
728
+ on_max_retries?: () => void;
728
729
  /** Only fire callbacks for these event types. e.g. ['credit', 'transfer_in'] */
729
730
  types?: Array<'credit' | 'debit' | 'transfer_in' | 'transfer_out' | 'withdrawal' | 'escrow_lock' | 'escrow_release'>;
731
+ /**
732
+ * Maximum number of reconnect attempts before giving up.
733
+ * Each attempt opens a fresh SSE connection (not just a backoff on the same one).
734
+ * After max_retries is hit, `on_max_retries` fires and the subscription stops.
735
+ * Default: Infinity (reconnect forever).
736
+ *
737
+ * Tip for Vercel/serverless: set max_retries to a small number and call
738
+ * sdk.wallet.subscribe() again in on_max_retries to get a completely fresh
739
+ * connection — this defeats the 5-minute SSE hard timeout.
740
+ *
741
+ * @example
742
+ * function startWatch() {
743
+ * sdk.wallet.subscribe({
744
+ * on_credit: (e) => console.log('+credits', e.amount),
745
+ * max_retries: 3,
746
+ * on_max_retries: () => {
747
+ * console.log('SSE hit retry limit — restarting subscription')
748
+ * setTimeout(startWatch, 2000)
749
+ * },
750
+ * })
751
+ * }
752
+ * startWatch()
753
+ */
754
+ max_retries?: number;
730
755
  }): Promise<() => void>;
731
756
  }
732
757
  interface WalletEvent {
package/dist/index.js CHANGED
@@ -874,6 +874,7 @@ var WalletSDK = class {
874
874
  const url = `${baseUrl}/api/wallet/watch?api_key=${encodeURIComponent(apiKey)}`;
875
875
  let closed = false;
876
876
  let es = null;
877
+ const maxRetries = callbacks.max_retries ?? Infinity;
877
878
  const HANDLER_MAP = {
878
879
  "wallet.credit": "on_credit",
879
880
  "wallet.debit": "on_debit",
@@ -900,11 +901,8 @@ var WalletSDK = class {
900
901
  if (typeof EventSource !== "undefined") {
901
902
  es = new EventSource(url);
902
903
  es.onmessage = (e) => {
903
- if (reconnectDelay !== 1e3) {
904
- reconnectDelay = 1e3;
905
- reconnectAttempt = 0;
906
- callbacks.on_reconnect?.(reconnectAttempt);
907
- }
904
+ reconnectDelay = 1e3;
905
+ reconnectAttempt = 0;
908
906
  try {
909
907
  const data = JSON.parse(e.data);
910
908
  if (data.type !== "connected" && data.type !== "ping") dispatch(data);
@@ -913,9 +911,15 @@ var WalletSDK = class {
913
911
  };
914
912
  es.onerror = () => {
915
913
  if (closed) return;
916
- reconnectAttempt++;
917
- callbacks.on_error?.(new Error(`SSE connection error \u2014 reconnecting in ${reconnectDelay / 1e3}s`));
918
914
  es?.close();
915
+ reconnectAttempt++;
916
+ if (reconnectAttempt > maxRetries) {
917
+ closed = true;
918
+ callbacks.on_max_retries?.();
919
+ return;
920
+ }
921
+ callbacks.on_error?.(new Error(`SSE connection error \u2014 reconnecting in ${reconnectDelay / 1e3}s (attempt ${reconnectAttempt}/${maxRetries === Infinity ? "\u221E" : maxRetries})`));
922
+ callbacks.on_reconnect?.(reconnectAttempt);
919
923
  setTimeout(() => {
920
924
  if (!closed) connect();
921
925
  }, reconnectDelay);
@@ -953,7 +957,12 @@ var WalletSDK = class {
953
957
  } catch (e) {
954
958
  if (closed) break;
955
959
  reconnectAttempt++;
956
- callbacks.on_error?.(new Error(`SSE dropped \u2014 reconnecting in ${reconnectDelay / 1e3}s`));
960
+ if (reconnectAttempt > maxRetries) {
961
+ closed = true;
962
+ callbacks.on_max_retries?.();
963
+ break;
964
+ }
965
+ callbacks.on_error?.(new Error(`SSE dropped \u2014 reconnecting in ${reconnectDelay / 1e3}s (attempt ${reconnectAttempt}/${maxRetries === Infinity ? "\u221E" : maxRetries})`));
957
966
  await new Promise((r) => setTimeout(r, reconnectDelay));
958
967
  reconnectDelay = Math.min(reconnectDelay * 2, MAX_RECONNECT_DELAY);
959
968
  }
package/dist/index.mjs CHANGED
@@ -714,6 +714,7 @@ var WalletSDK = class {
714
714
  const url = `${baseUrl}/api/wallet/watch?api_key=${encodeURIComponent(apiKey)}`;
715
715
  let closed = false;
716
716
  let es = null;
717
+ const maxRetries = callbacks.max_retries ?? Infinity;
717
718
  const HANDLER_MAP = {
718
719
  "wallet.credit": "on_credit",
719
720
  "wallet.debit": "on_debit",
@@ -740,11 +741,8 @@ var WalletSDK = class {
740
741
  if (typeof EventSource !== "undefined") {
741
742
  es = new EventSource(url);
742
743
  es.onmessage = (e) => {
743
- if (reconnectDelay !== 1e3) {
744
- reconnectDelay = 1e3;
745
- reconnectAttempt = 0;
746
- callbacks.on_reconnect?.(reconnectAttempt);
747
- }
744
+ reconnectDelay = 1e3;
745
+ reconnectAttempt = 0;
748
746
  try {
749
747
  const data = JSON.parse(e.data);
750
748
  if (data.type !== "connected" && data.type !== "ping") dispatch(data);
@@ -753,9 +751,15 @@ var WalletSDK = class {
753
751
  };
754
752
  es.onerror = () => {
755
753
  if (closed) return;
756
- reconnectAttempt++;
757
- callbacks.on_error?.(new Error(`SSE connection error \u2014 reconnecting in ${reconnectDelay / 1e3}s`));
758
754
  es?.close();
755
+ reconnectAttempt++;
756
+ if (reconnectAttempt > maxRetries) {
757
+ closed = true;
758
+ callbacks.on_max_retries?.();
759
+ return;
760
+ }
761
+ callbacks.on_error?.(new Error(`SSE connection error \u2014 reconnecting in ${reconnectDelay / 1e3}s (attempt ${reconnectAttempt}/${maxRetries === Infinity ? "\u221E" : maxRetries})`));
762
+ callbacks.on_reconnect?.(reconnectAttempt);
759
763
  setTimeout(() => {
760
764
  if (!closed) connect();
761
765
  }, reconnectDelay);
@@ -793,7 +797,12 @@ var WalletSDK = class {
793
797
  } catch (e) {
794
798
  if (closed) break;
795
799
  reconnectAttempt++;
796
- callbacks.on_error?.(new Error(`SSE dropped \u2014 reconnecting in ${reconnectDelay / 1e3}s`));
800
+ if (reconnectAttempt > maxRetries) {
801
+ closed = true;
802
+ callbacks.on_max_retries?.();
803
+ break;
804
+ }
805
+ callbacks.on_error?.(new Error(`SSE dropped \u2014 reconnecting in ${reconnectDelay / 1e3}s (attempt ${reconnectAttempt}/${maxRetries === Infinity ? "\u221E" : maxRetries})`));
797
806
  await new Promise((r) => setTimeout(r, reconnectDelay));
798
807
  reconnectDelay = Math.min(reconnectDelay * 2, MAX_RECONNECT_DELAY);
799
808
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moltos/sdk",
3
- "version": "0.19.1",
3
+ "version": "0.19.3",
4
4
  "description": "MoltOS \u2014 The Agent Operating System SDK. Build agents that earn, persist, and compound trust.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",