@secondlayer/sdk 6.8.0 → 6.9.0

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/dist/index.d.ts CHANGED
@@ -31,6 +31,23 @@ interface SubgraphSource {
31
31
  reason?: string;
32
32
  updatedAt: string;
33
33
  }
34
+ /** Status of a tracked reindex/backfill operation (poll until terminal). */
35
+ interface SubgraphOperationStatus {
36
+ id: string;
37
+ subgraphName: string;
38
+ kind: "reindex" | "backfill";
39
+ status: "queued" | "running" | "completed" | "failed" | "cancelled";
40
+ fromBlock: number | null;
41
+ toBlock: number | null;
42
+ processedBlocks: number | null;
43
+ /** 0–1 fraction; null when no denominator is known yet. 1 when completed. */
44
+ progress: number | null;
45
+ error: string | null;
46
+ startedAt: string | null;
47
+ finishedAt: string | null;
48
+ createdAt: string;
49
+ updatedAt: string;
50
+ }
34
51
  interface BundleSubgraphResponse {
35
52
  ok: true;
36
53
  name: string;
@@ -73,6 +90,13 @@ declare class Subgraphs extends BaseClient {
73
90
  }): Promise<{
74
91
  message: string
75
92
  }>;
93
+ /** Recent reindex/backfill operations for a subgraph, newest first. */
94
+ operations(name: string): Promise<{
95
+ operations: SubgraphOperationStatus[]
96
+ }>;
97
+ /** Status of a single operation (poll the `operationId` returned by
98
+ * reindex/backfill/stop until `status` is terminal). */
99
+ getOperation(name: string, operationId: string): Promise<SubgraphOperationStatus>;
76
100
  deploy(data: DeploySubgraphRequest): Promise<DeploySubgraphResponse>;
77
101
  getSource(name: string): Promise<SubgraphSource>;
78
102
  /**
@@ -105,6 +129,41 @@ declare class Subgraphs extends BaseClient {
105
129
  private createTableClient;
106
130
  }
107
131
  import { InferSubgraphClient as InferSubgraphClient2 } from "@secondlayer/subgraphs";
132
+ import { SubgraphSummary as SubgraphSummary2 } from "@secondlayer/shared/schemas";
133
+ /**
134
+ * Typed client for the agent-reachable key mint (`POST /v1/api-keys`).
135
+ *
136
+ * Lets a headless agent self-provision a SCOPED `streams`/`index` read key
137
+ * without the dashboard. Requires an owner credential — an account-level API
138
+ * key (or a dashboard session). The minted key is always scoped (never an
139
+ * account/superkey) and inherits the account plan's tier.
140
+ */
141
+ /** Scope of a minted read key. */
142
+ type ScopedKeyProduct = "streams" | "index";
143
+ interface CreateApiKeyParams {
144
+ /** Scope of the minted key. Defaults to "streams". */
145
+ product?: ScopedKeyProduct;
146
+ /** Optional human-readable label for the key. */
147
+ name?: string;
148
+ }
149
+ interface CreateApiKeyResponse {
150
+ /** Plaintext key — returned ONCE. Store it now; only its hash is persisted. */
151
+ key: string;
152
+ prefix: string;
153
+ id: string;
154
+ product: string;
155
+ tier: string | null;
156
+ createdAt: string;
157
+ }
158
+ declare class ApiKeys extends BaseClient {
159
+ constructor(options?: Partial<SecondLayerOptions>);
160
+ /**
161
+ * Mint a new scoped read key. The configured `apiKey` must be an
162
+ * account-level (owner) key; the plaintext `key` in the response is shown
163
+ * only once.
164
+ */
165
+ create(params?: CreateApiKeyParams): Promise<CreateApiKeyResponse>;
166
+ }
108
167
  /**
109
168
  * Typed client for the contract-discovery API (`GET /v1/contracts`).
110
169
  *
@@ -278,6 +337,17 @@ type IndexTip = {
278
337
  block_height: number
279
338
  lag_seconds: number
280
339
  };
340
+ type IndexUsage = {
341
+ product: "index"
342
+ tier: string
343
+ limits: {
344
+ rate_limit_per_second: number | null
345
+ }
346
+ usage: {
347
+ decoded_events_today: number
348
+ decoded_events_this_month: number
349
+ }
350
+ };
281
351
  type FtTransfer = {
282
352
  cursor: string
283
353
  block_height: number
@@ -737,6 +807,8 @@ type MempoolWalkParams = Omit<MempoolListParams, "limit"> & {
737
807
  };
738
808
  declare class Index extends BaseClient {
739
809
  constructor(options?: Partial<SecondLayerOptions>);
810
+ /** Your own Index consumption (decoded events today + this month) and tier limits. */
811
+ usage(): Promise<IndexUsage>;
740
812
  readonly ftTransfers: {
741
813
  list: (params?: FtTransfersListParams) => Promise<FtTransfersEnvelope>
742
814
  walk: (params?: FtTransfersWalkParams) => AsyncIterable<FtTransfer>
@@ -808,8 +880,7 @@ declare class Index extends BaseClient {
808
880
  private getMempoolTx;
809
881
  private walkMempool;
810
882
  }
811
- declare const STREAMS_EVENT_TYPES: readonly ["stx_transfer", "stx_mint", "stx_burn", "stx_lock", "ft_transfer", "ft_mint", "ft_burn", "nft_transfer", "nft_mint", "nft_burn", "print"];
812
- type StreamsEventType = (typeof STREAMS_EVENT_TYPES)[number];
883
+ import { StreamsEventType } from "@secondlayer/shared";
813
884
  /** A Clarity value as Streams serves it: the canonical hex string, a typed
814
885
  * object carrying that hex (`{ hex }`), or a decoded Clarity-JSON object.
815
886
  * Decode helpers (`decodeNftTransfer`, etc.) resolve it to a concrete value. */
@@ -1137,6 +1208,20 @@ type StreamsClient = {
1137
1208
  dumps: StreamsDumps
1138
1209
  canonical(height: number): Promise<StreamsCanonicalBlock>
1139
1210
  tip(): Promise<StreamsTip>
1211
+ /** Your own Streams consumption (events today + this month) and tier limits. */
1212
+ usage(): Promise<StreamsUsage>
1213
+ };
1214
+ type StreamsUsage = {
1215
+ product: "streams"
1216
+ tier: string
1217
+ limits: {
1218
+ rate_limit_per_second: number | null
1219
+ retention_days: number | null
1220
+ }
1221
+ usage: {
1222
+ events_today: number
1223
+ events_this_month: number
1224
+ }
1140
1225
  };
1141
1226
  import { CreateSubscriptionRequest, CreateSubscriptionResponse, DeadRow, DeliveryRow, ReplayResult, RotateSecretResponse, SubscriptionDetail, SubscriptionSummary, UpdateSubscriptionRequest } from "@secondlayer/shared/schemas/subscriptions";
1142
1227
  import { ChainTrigger, ChainTriggerType, CreateSubscriptionRequest as CreateSubscriptionRequest2, CreateSubscriptionResponse as CreateSubscriptionResponse2, DeadRow as DeadRow2, DeliveryRow as DeliveryRow2, ReplayResult as ReplayResult2, RotateSecretResponse as RotateSecretResponse2, SubscriptionDetail as SubscriptionDetail2, SubscriptionFormat, SubscriptionKind, SubscriptionRuntime, SubscriptionStatus, SubscriptionSummary as SubscriptionSummary2, UpdateSubscriptionRequest as UpdateSubscriptionRequest2 } from "@secondlayer/shared/schemas/subscriptions";
@@ -1168,6 +1253,34 @@ declare class Subscriptions extends BaseClient {
1168
1253
  ok: true
1169
1254
  }>;
1170
1255
  }
1256
+ interface ContextAccount {
1257
+ email: string;
1258
+ plan: string;
1259
+ }
1260
+ interface ActiveSubgraphOperation {
1261
+ subgraph: string;
1262
+ operationId: string;
1263
+ kind: SubgraphOperationStatus["kind"];
1264
+ status: SubgraphOperationStatus["status"];
1265
+ progress: number | null;
1266
+ }
1267
+ /**
1268
+ * A point-in-time orientation snapshot for an agent: who you are, the live tips,
1269
+ * and what you own. Each field is `null` when it couldn't be read (e.g. no key,
1270
+ * or a free-tier key for a Build+ surface) so the snapshot never throws.
1271
+ */
1272
+ interface ContextSnapshot {
1273
+ account: ContextAccount | null;
1274
+ streamsTip: StreamsTip | null;
1275
+ indexTip: IndexTip | null;
1276
+ subgraphs: SubgraphSummary2[] | null;
1277
+ subscriptions: {
1278
+ count: number
1279
+ byStatus: Record<string, number>
1280
+ } | null;
1281
+ /** In-flight reindex operations (bounded to subgraphs reporting `reindexing`). */
1282
+ activeOperations: ActiveSubgraphOperation[] | null;
1283
+ }
1171
1284
  declare class SecondLayer extends BaseClient {
1172
1285
  readonly streams: StreamsClient;
1173
1286
  readonly index: Index;
@@ -1175,7 +1288,14 @@ declare class SecondLayer extends BaseClient {
1175
1288
  readonly contracts: Contracts;
1176
1289
  readonly subgraphs: Subgraphs;
1177
1290
  readonly subscriptions: Subscriptions;
1291
+ readonly apiKeys: ApiKeys;
1178
1292
  constructor(options?: Partial<SecondLayerOptions>);
1293
+ /**
1294
+ * Assemble a {@link ContextSnapshot} — the same orientation an MCP agent reads
1295
+ * from `secondlayer://context`, but available to any SDK/CLI consumer. Reads
1296
+ * run concurrently and degrade to `null` per field on failure.
1297
+ */
1298
+ context(): Promise<ContextSnapshot>;
1179
1299
  }
1180
1300
  /**
1181
1301
  * Returns a typed client for a subgraph defined with `defineSubgraph()`.
@@ -1625,4 +1745,4 @@ declare function toJsonSafe(value: unknown): unknown;
1625
1745
  /** Decode a hex-encoded Clarity value to JSON-safe JS (uints as strings,
1626
1746
  * buffers as `0x…` hex, tuples as objects). Returns the input hex on failure. */
1627
1747
  declare function decodeClarityValue(hex: string): unknown;
1628
- export { verifyWebhookSignature, trigger, toJsonSafe, isStxTransfer, isStxMint, isStxLock, isStxBurn, isPrint, isNftTransfer, isNftMint, isNftBurn, isFtTransfer, isFtMint, isFtBurn, getSubgraph, decodeStxTransfer, decodeStxMint, decodeStxLock, decodeStxBurn, decodePrint, decodeNftTransfer, decodeNftMint, decodeNftBurn, decodeFtTransfer, decodeFtMint, decodeFtBurn, decodeClarityValue, createStreamsClient, VersionConflictError, ValidationError, UpdateSubscriptionRequest2 as UpdateSubscriptionRequest, TransactionsWalkParams, TransactionsListParams, TransactionsEnvelope, TransactionEnvelope, Subscriptions, SubscriptionSummary2 as SubscriptionSummary, SubscriptionStatus, SubscriptionRuntime, SubscriptionKind, SubscriptionFormat, SubscriptionDetail2 as SubscriptionDetail, Subgraphs, SubgraphSpecOptions3 as SubgraphSpecOptions, SubgraphSpecFormat2 as SubgraphSpecFormat, SubgraphAgentSchema3 as SubgraphAgentSchema, StreamsTip, StreamsSignatureError, StreamsServerError, StreamsReorgsListParams, StreamsReorgsListEnvelope, StreamsReorgContext, StreamsReorg, StreamsEventsStreamParams, StreamsEventsListParams, StreamsEventsListEnvelope, StreamsEventsEnvelope, StreamsEventsConsumeResult, StreamsEventsConsumeParams, StreamsEventType, StreamsEventPayload, StreamsEvent, StreamsDumpsManifest, StreamsDumps, StreamsDumpFile, StreamsClient, StreamsCanonicalBlock, StreamsBatchContext, StackingWalkParams, StackingListParams, StackingEnvelope, SecondLayerOptions, SecondLayer, RotateSecretResponse2 as RotateSecretResponse, ReplayResult2 as ReplayResult, RateLimitError, Pox4CallsParams, NftTransfersWalkParams, NftTransfersListParams, NftTransfersEnvelope, NftTransferPayload, NftTransferEvent, NftTransfer, MempoolWalkParams, MempoolTransactionEnvelope, MempoolListParams, MempoolEnvelope, IndexTransaction, IndexTip, IndexStackingAction, IndexPostCondition, IndexMempoolTransaction, IndexEventType, IndexEvent, IndexContractCall, IndexCanonicalBlock, IndexBlock, Index, FtTransfersWalkParams, FtTransfersListParams, FtTransfersEnvelope, FtTransferPayload, FtTransferEvent, FtTransfer, FetchLike2 as FetchLike, EventsWalkParams, EventsListParams, EventsEnvelope, DeliveryRow2 as DeliveryRow, DecodedStxTransferPayload, DecodedStxTransfer, DecodedStxMintPayload, DecodedStxMint, DecodedStxLockPayload, DecodedStxLock, DecodedStxBurnPayload, DecodedStxBurn, DecodedPrintValue, DecodedPrintPayload, DecodedPrint, DecodedNftTransferPayload, DecodedNftTransfer, DecodedNftMintPayload, DecodedNftMint, DecodedNftBurnPayload, DecodedNftBurn, DecodedFtTransferPayload, DecodedFtTransfer, DecodedFtMintPayload, DecodedFtMint, DecodedFtBurnPayload, DecodedFtBurn, DecodedEventRow, DecodedEventColumns, DeadRow2 as DeadRow, Datasets, DatasetRow, CursorListParams, CursorEnvelope, Cursor, CreateSubscriptionResponse2 as CreateSubscriptionResponse, CreateSubscriptionRequest2 as CreateSubscriptionRequest, ContractsListParams, ContractsEnvelope, Contracts, ContractSummary, ContractConformance, ContractCallsWalkParams, ContractCallsListParams, ContractCallsEnvelope, ChainTriggerType, ChainTrigger, CanonicalWalkParams, CanonicalListParams, CanonicalEnvelope, CURSOR_SLUGS, BlocksWalkParams, BlocksListParams, BlocksEnvelope, BlockEnvelope, AuthError, ApiError };
1748
+ export { verifyWebhookSignature, trigger, toJsonSafe, isStxTransfer, isStxMint, isStxLock, isStxBurn, isPrint, isNftTransfer, isNftMint, isNftBurn, isFtTransfer, isFtMint, isFtBurn, getSubgraph, decodeStxTransfer, decodeStxMint, decodeStxLock, decodeStxBurn, decodePrint, decodeNftTransfer, decodeNftMint, decodeNftBurn, decodeFtTransfer, decodeFtMint, decodeFtBurn, decodeClarityValue, createStreamsClient, VersionConflictError, ValidationError, UpdateSubscriptionRequest2 as UpdateSubscriptionRequest, TransactionsWalkParams, TransactionsListParams, TransactionsEnvelope, TransactionEnvelope, Subscriptions, SubscriptionSummary2 as SubscriptionSummary, SubscriptionStatus, SubscriptionRuntime, SubscriptionKind, SubscriptionFormat, SubscriptionDetail2 as SubscriptionDetail, Subgraphs, SubgraphSpecOptions3 as SubgraphSpecOptions, SubgraphSpecFormat2 as SubgraphSpecFormat, SubgraphOperationStatus, SubgraphAgentSchema3 as SubgraphAgentSchema, StreamsUsage, StreamsTip, StreamsSignatureError, StreamsServerError, StreamsReorgsListParams, StreamsReorgsListEnvelope, StreamsReorgContext, StreamsReorg, StreamsEventsStreamParams, StreamsEventsListParams, StreamsEventsListEnvelope, StreamsEventsEnvelope, StreamsEventsConsumeResult, StreamsEventsConsumeParams, StreamsEventType, StreamsEventPayload, StreamsEvent, StreamsDumpsManifest, StreamsDumps, StreamsDumpFile, StreamsClient, StreamsCanonicalBlock, StreamsBatchContext, StackingWalkParams, StackingListParams, StackingEnvelope, SecondLayerOptions, SecondLayer, ScopedKeyProduct, RotateSecretResponse2 as RotateSecretResponse, ReplayResult2 as ReplayResult, RateLimitError, Pox4CallsParams, NftTransfersWalkParams, NftTransfersListParams, NftTransfersEnvelope, NftTransferPayload, NftTransferEvent, NftTransfer, MempoolWalkParams, MempoolTransactionEnvelope, MempoolListParams, MempoolEnvelope, IndexUsage, IndexTransaction, IndexTip, IndexStackingAction, IndexPostCondition, IndexMempoolTransaction, IndexEventType, IndexEvent, IndexContractCall, IndexCanonicalBlock, IndexBlock, Index, FtTransfersWalkParams, FtTransfersListParams, FtTransfersEnvelope, FtTransferPayload, FtTransferEvent, FtTransfer, FetchLike2 as FetchLike, EventsWalkParams, EventsListParams, EventsEnvelope, DeliveryRow2 as DeliveryRow, DecodedStxTransferPayload, DecodedStxTransfer, DecodedStxMintPayload, DecodedStxMint, DecodedStxLockPayload, DecodedStxLock, DecodedStxBurnPayload, DecodedStxBurn, DecodedPrintValue, DecodedPrintPayload, DecodedPrint, DecodedNftTransferPayload, DecodedNftTransfer, DecodedNftMintPayload, DecodedNftMint, DecodedNftBurnPayload, DecodedNftBurn, DecodedFtTransferPayload, DecodedFtTransfer, DecodedFtMintPayload, DecodedFtMint, DecodedFtBurnPayload, DecodedFtBurn, DecodedEventRow, DecodedEventColumns, DeadRow2 as DeadRow, Datasets, DatasetRow, CursorListParams, CursorEnvelope, Cursor, CreateSubscriptionResponse2 as CreateSubscriptionResponse, CreateSubscriptionRequest2 as CreateSubscriptionRequest, CreateApiKeyResponse, CreateApiKeyParams, ContractsListParams, ContractsEnvelope, Contracts, ContractSummary, ContractConformance, ContractCallsWalkParams, ContractCallsListParams, ContractCallsEnvelope, ContextSnapshot, ContextAccount, ChainTriggerType, ChainTrigger, CanonicalWalkParams, CanonicalListParams, CanonicalEnvelope, CURSOR_SLUGS, BlocksWalkParams, BlocksListParams, BlocksEnvelope, BlockEnvelope, AuthError, ApiKeys, ApiError, ActiveSubgraphOperation };
package/dist/index.js CHANGED
@@ -242,6 +242,12 @@ class Subgraphs extends BaseClient {
242
242
  const qs = options?.force ? "?force=true" : "";
243
243
  return this.request("DELETE", `/api/subgraphs/${name}${qs}`);
244
244
  }
245
+ async operations(name) {
246
+ return this.request("GET", `/api/subgraphs/${name}/operations`);
247
+ }
248
+ async getOperation(name, operationId) {
249
+ return this.request("GET", `/api/subgraphs/${name}/operations/${operationId}`);
250
+ }
245
251
  async deploy(data) {
246
252
  return this.request("POST", "/api/subgraphs", data);
247
253
  }
@@ -324,6 +330,19 @@ class Subgraphs extends BaseClient {
324
330
  };
325
331
  }
326
332
  }
333
+ // src/api-keys/client.ts
334
+ class ApiKeys extends BaseClient {
335
+ constructor(options = {}) {
336
+ super(options);
337
+ }
338
+ create(params = {}) {
339
+ return this.request("POST", "/v1/api-keys", {
340
+ product: params.product,
341
+ name: params.name
342
+ });
343
+ }
344
+ }
345
+
327
346
  // src/contracts/client.ts
328
347
  class Contracts extends BaseClient {
329
348
  constructor(options = {}) {
@@ -473,6 +492,9 @@ class Index extends BaseClient {
473
492
  constructor(options = {}) {
474
493
  super(options);
475
494
  }
495
+ usage() {
496
+ return this.request("GET", "/v1/index/usage");
497
+ }
476
498
  ftTransfers = {
477
499
  list: (params = {}) => this.listFtTransfers(params),
478
500
  walk: (params = {}) => this.walkFtTransfers(params)
@@ -1331,6 +1353,9 @@ function createStreamsClient(options) {
1331
1353
  },
1332
1354
  tip() {
1333
1355
  return request("/v1/streams/tip");
1356
+ },
1357
+ usage() {
1358
+ return request("/v1/streams/usage");
1334
1359
  }
1335
1360
  };
1336
1361
  }
@@ -1385,6 +1410,7 @@ class SecondLayer extends BaseClient {
1385
1410
  contracts;
1386
1411
  subgraphs;
1387
1412
  subscriptions;
1413
+ apiKeys;
1388
1414
  constructor(options = {}) {
1389
1415
  super(options);
1390
1416
  this.streams = createStreamsClient({
@@ -1397,6 +1423,49 @@ class SecondLayer extends BaseClient {
1397
1423
  this.contracts = new Contracts(options);
1398
1424
  this.subgraphs = new Subgraphs(options);
1399
1425
  this.subscriptions = new Subscriptions(options);
1426
+ this.apiKeys = new ApiKeys(options);
1427
+ }
1428
+ async context() {
1429
+ const safe = (p) => p.then((v) => v).catch(() => null);
1430
+ const [account, streamsTip, indexEnv, subgraphsRes, subscriptionsRes] = await Promise.all([
1431
+ safe(this.request("GET", "/api/accounts/me")),
1432
+ safe(this.streams.tip()),
1433
+ safe(this.index.canonical.list({ limit: 1 })),
1434
+ safe(this.subgraphs.list()),
1435
+ safe(this.subscriptions.list())
1436
+ ]);
1437
+ const subgraphs = subgraphsRes?.data ?? null;
1438
+ let subscriptions = null;
1439
+ if (subscriptionsRes) {
1440
+ const byStatus = {};
1441
+ for (const s of subscriptionsRes.data) {
1442
+ byStatus[s.status] = (byStatus[s.status] ?? 0) + 1;
1443
+ }
1444
+ subscriptions = { count: subscriptionsRes.data.length, byStatus };
1445
+ }
1446
+ let activeOperations = null;
1447
+ if (subgraphs) {
1448
+ const probed = await Promise.all(subgraphs.filter((s) => s.status === "reindexing").map(async (s) => {
1449
+ const res = await safe(this.subgraphs.operations(s.name));
1450
+ const op = res?.operations.find((o) => o.status === "queued" || o.status === "running");
1451
+ return op ? {
1452
+ subgraph: s.name,
1453
+ operationId: op.id,
1454
+ kind: op.kind,
1455
+ status: op.status,
1456
+ progress: op.progress
1457
+ } : null;
1458
+ }));
1459
+ activeOperations = probed.filter((o) => o !== null);
1460
+ }
1461
+ return {
1462
+ account,
1463
+ streamsTip,
1464
+ indexTip: indexEnv?.tip ?? null,
1465
+ subgraphs,
1466
+ subscriptions,
1467
+ activeOperations
1468
+ };
1400
1469
  }
1401
1470
  }
1402
1471
 
@@ -1690,19 +1759,9 @@ function decodePrint(event) {
1690
1759
  });
1691
1760
  }
1692
1761
  // src/streams/types.ts
1693
- var STREAMS_EVENT_TYPES = [
1694
- "stx_transfer",
1695
- "stx_mint",
1696
- "stx_burn",
1697
- "stx_lock",
1698
- "ft_transfer",
1699
- "ft_mint",
1700
- "ft_burn",
1701
- "nft_transfer",
1702
- "nft_mint",
1703
- "nft_burn",
1704
- "print"
1705
- ];
1762
+ import {
1763
+ STREAMS_EVENT_TYPES
1764
+ } from "@secondlayer/shared";
1706
1765
  // src/webhooks.ts
1707
1766
  import {
1708
1767
  verify
@@ -1789,8 +1848,9 @@ export {
1789
1848
  Contracts,
1790
1849
  CURSOR_SLUGS,
1791
1850
  AuthError,
1851
+ ApiKeys,
1792
1852
  ApiError
1793
1853
  };
1794
1854
 
1795
- //# debugId=9CB0E1549AAB87D864756E2164756E21
1855
+ //# debugId=209F8CDE1560C10F64756E2164756E21
1796
1856
  //# sourceMappingURL=index.js.map