@absol-labs/agent 0.9.1 → 0.10.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.
Files changed (199) hide show
  1. package/README.md +77 -3
  2. package/dist/cli/amounts.d.ts +26 -0
  3. package/dist/cli/amounts.d.ts.map +1 -0
  4. package/dist/cli/amounts.js +71 -0
  5. package/dist/cli/amounts.js.map +1 -0
  6. package/dist/cli/args.d.ts +32 -0
  7. package/dist/cli/args.d.ts.map +1 -0
  8. package/dist/cli/args.js +119 -0
  9. package/dist/cli/args.js.map +1 -0
  10. package/dist/cli/attesters.d.ts +51 -0
  11. package/dist/cli/attesters.d.ts.map +1 -0
  12. package/dist/cli/attesters.js +58 -0
  13. package/dist/cli/attesters.js.map +1 -0
  14. package/dist/cli/bin.d.ts +3 -0
  15. package/dist/cli/bin.d.ts.map +1 -0
  16. package/dist/cli/bin.js +5 -0
  17. package/dist/cli/bin.js.map +1 -0
  18. package/dist/cli/commands/claim.d.ts +13 -0
  19. package/dist/cli/commands/claim.d.ts.map +1 -0
  20. package/dist/cli/commands/claim.js +86 -0
  21. package/dist/cli/commands/claim.js.map +1 -0
  22. package/dist/cli/commands/close.d.ts +3 -0
  23. package/dist/cli/commands/close.d.ts.map +1 -0
  24. package/dist/cli/commands/close.js +43 -0
  25. package/dist/cli/commands/close.js.map +1 -0
  26. package/dist/cli/commands/discover.d.ts +3 -0
  27. package/dist/cli/commands/discover.d.ts.map +1 -0
  28. package/dist/cli/commands/discover.js +183 -0
  29. package/dist/cli/commands/discover.js.map +1 -0
  30. package/dist/cli/commands/invoke.d.ts +3 -0
  31. package/dist/cli/commands/invoke.d.ts.map +1 -0
  32. package/dist/cli/commands/invoke.js +141 -0
  33. package/dist/cli/commands/invoke.js.map +1 -0
  34. package/dist/cli/commands/mcp.d.ts +3 -0
  35. package/dist/cli/commands/mcp.d.ts.map +1 -0
  36. package/dist/cli/commands/mcp.js +113 -0
  37. package/dist/cli/commands/mcp.js.map +1 -0
  38. package/dist/cli/commands/open.d.ts +3 -0
  39. package/dist/cli/commands/open.d.ts.map +1 -0
  40. package/dist/cli/commands/open.js +279 -0
  41. package/dist/cli/commands/open.js.map +1 -0
  42. package/dist/cli/commands/reclaim-unverified.d.ts +3 -0
  43. package/dist/cli/commands/reclaim-unverified.d.ts.map +1 -0
  44. package/dist/cli/commands/reclaim-unverified.js +164 -0
  45. package/dist/cli/commands/reclaim-unverified.js.map +1 -0
  46. package/dist/cli/commands/reclaim.d.ts +33 -0
  47. package/dist/cli/commands/reclaim.d.ts.map +1 -0
  48. package/dist/cli/commands/reclaim.js +297 -0
  49. package/dist/cli/commands/reclaim.js.map +1 -0
  50. package/dist/cli/commands/status.d.ts +3 -0
  51. package/dist/cli/commands/status.d.ts.map +1 -0
  52. package/dist/cli/commands/status.js +43 -0
  53. package/dist/cli/commands/status.js.map +1 -0
  54. package/dist/cli/commands/streams.d.ts +3 -0
  55. package/dist/cli/commands/streams.d.ts.map +1 -0
  56. package/dist/cli/commands/streams.js +257 -0
  57. package/dist/cli/commands/streams.js.map +1 -0
  58. package/dist/cli/commands/watch.d.ts +3 -0
  59. package/dist/cli/commands/watch.d.ts.map +1 -0
  60. package/dist/cli/commands/watch.js +55 -0
  61. package/dist/cli/commands/watch.js.map +1 -0
  62. package/dist/cli/config.d.ts +61 -0
  63. package/dist/cli/config.d.ts.map +1 -0
  64. package/dist/cli/config.js +140 -0
  65. package/dist/cli/config.js.map +1 -0
  66. package/dist/cli/context.d.ts +29 -0
  67. package/dist/cli/context.d.ts.map +1 -0
  68. package/dist/cli/context.js +2 -0
  69. package/dist/cli/context.js.map +1 -0
  70. package/dist/cli/deps.d.ts +145 -0
  71. package/dist/cli/deps.d.ts.map +1 -0
  72. package/dist/cli/deps.js +227 -0
  73. package/dist/cli/deps.js.map +1 -0
  74. package/dist/cli/descriptor.d.ts +61 -0
  75. package/dist/cli/descriptor.d.ts.map +1 -0
  76. package/dist/cli/descriptor.js +78 -0
  77. package/dist/cli/descriptor.js.map +1 -0
  78. package/dist/cli/errors.d.ts +48 -0
  79. package/dist/cli/errors.d.ts.map +1 -0
  80. package/dist/cli/errors.js +129 -0
  81. package/dist/cli/errors.js.map +1 -0
  82. package/dist/cli/escape-hatch.d.ts +50 -0
  83. package/dist/cli/escape-hatch.d.ts.map +1 -0
  84. package/dist/cli/escape-hatch.js +45 -0
  85. package/dist/cli/escape-hatch.js.map +1 -0
  86. package/dist/cli/index.d.ts +8 -0
  87. package/dist/cli/index.d.ts.map +1 -0
  88. package/dist/cli/index.js +7 -0
  89. package/dist/cli/index.js.map +1 -0
  90. package/dist/cli/listings.d.ts +15 -0
  91. package/dist/cli/listings.d.ts.map +1 -0
  92. package/dist/cli/listings.js +56 -0
  93. package/dist/cli/listings.js.map +1 -0
  94. package/dist/cli/mandate.d.ts +66 -0
  95. package/dist/cli/mandate.d.ts.map +1 -0
  96. package/dist/cli/mandate.js +61 -0
  97. package/dist/cli/mandate.js.map +1 -0
  98. package/dist/cli/output.d.ts +33 -0
  99. package/dist/cli/output.d.ts.map +1 -0
  100. package/dist/cli/output.js +34 -0
  101. package/dist/cli/output.js.map +1 -0
  102. package/dist/cli/preflight.d.ts +38 -0
  103. package/dist/cli/preflight.d.ts.map +1 -0
  104. package/dist/cli/preflight.js +72 -0
  105. package/dist/cli/preflight.js.map +1 -0
  106. package/dist/cli/progress.d.ts +36 -0
  107. package/dist/cli/progress.d.ts.map +1 -0
  108. package/dist/cli/progress.js +40 -0
  109. package/dist/cli/progress.js.map +1 -0
  110. package/dist/cli/run.d.ts +25 -0
  111. package/dist/cli/run.d.ts.map +1 -0
  112. package/dist/cli/run.js +173 -0
  113. package/dist/cli/run.js.map +1 -0
  114. package/dist/cli/scan-range.d.ts +55 -0
  115. package/dist/cli/scan-range.d.ts.map +1 -0
  116. package/dist/cli/scan-range.js +57 -0
  117. package/dist/cli/scan-range.js.map +1 -0
  118. package/dist/cli/status.d.ts +90 -0
  119. package/dist/cli/status.d.ts.map +1 -0
  120. package/dist/cli/status.js +252 -0
  121. package/dist/cli/status.js.map +1 -0
  122. package/dist/cli/timeout.d.ts +30 -0
  123. package/dist/cli/timeout.d.ts.map +1 -0
  124. package/dist/cli/timeout.js +55 -0
  125. package/dist/cli/timeout.js.map +1 -0
  126. package/dist/cli/version.d.ts +12 -0
  127. package/dist/cli/version.d.ts.map +1 -0
  128. package/dist/cli/version.js +22 -0
  129. package/dist/cli/version.js.map +1 -0
  130. package/dist/cli/wallet.d.ts +95 -0
  131. package/dist/cli/wallet.d.ts.map +1 -0
  132. package/dist/cli/wallet.js +169 -0
  133. package/dist/cli/wallet.js.map +1 -0
  134. package/dist/frameworks/crewai.d.ts +10 -0
  135. package/dist/frameworks/crewai.d.ts.map +1 -1
  136. package/dist/frameworks/crewai.js +12 -2
  137. package/dist/frameworks/crewai.js.map +1 -1
  138. package/dist/gateway/server-entry.d.ts +1 -0
  139. package/dist/gateway/server-entry.js +1 -0
  140. package/dist/gateway/server-entry.js.map +1 -1
  141. package/dist/mcp/http-server.d.ts +1 -0
  142. package/dist/mcp/http-server.js +1 -0
  143. package/dist/mcp/http-server.js.map +1 -1
  144. package/dist/mcp/server.d.ts.map +1 -1
  145. package/dist/mcp/server.js +20 -1
  146. package/dist/mcp/server.js.map +1 -1
  147. package/dist/mcp/stdio.d.ts +1 -0
  148. package/dist/mcp/stdio.js +1 -0
  149. package/dist/mcp/stdio.js.map +1 -1
  150. package/dist/sdk/client.d.ts +25 -1
  151. package/dist/sdk/client.d.ts.map +1 -1
  152. package/dist/sdk/client.js +20 -1
  153. package/dist/sdk/client.js.map +1 -1
  154. package/docs/agent-layer.md +2 -1
  155. package/docs/cli.md +569 -0
  156. package/docs/crewai.md +6 -2
  157. package/docs/eliza.md +6 -1
  158. package/docs/mcp-hosted.md +3 -2
  159. package/docs/quickstart.md +18 -5
  160. package/package.json +17 -5
  161. package/src/cli/amounts.ts +102 -0
  162. package/src/cli/args.ts +150 -0
  163. package/src/cli/attesters.ts +86 -0
  164. package/src/cli/bin.ts +5 -0
  165. package/src/cli/commands/claim.ts +100 -0
  166. package/src/cli/commands/close.ts +53 -0
  167. package/src/cli/commands/discover.ts +228 -0
  168. package/src/cli/commands/invoke.ts +177 -0
  169. package/src/cli/commands/mcp.ts +135 -0
  170. package/src/cli/commands/open.ts +331 -0
  171. package/src/cli/commands/reclaim-unverified.ts +206 -0
  172. package/src/cli/commands/reclaim.ts +360 -0
  173. package/src/cli/commands/status.ts +56 -0
  174. package/src/cli/commands/streams.ts +344 -0
  175. package/src/cli/commands/watch.ts +70 -0
  176. package/src/cli/config.ts +208 -0
  177. package/src/cli/context.ts +30 -0
  178. package/src/cli/deps.ts +437 -0
  179. package/src/cli/descriptor.ts +133 -0
  180. package/src/cli/errors.ts +162 -0
  181. package/src/cli/escape-hatch.ts +92 -0
  182. package/src/cli/index.ts +27 -0
  183. package/src/cli/listings.ts +77 -0
  184. package/src/cli/mandate.ts +126 -0
  185. package/src/cli/output.ts +63 -0
  186. package/src/cli/preflight.ts +88 -0
  187. package/src/cli/progress.ts +75 -0
  188. package/src/cli/run.ts +209 -0
  189. package/src/cli/scan-range.ts +76 -0
  190. package/src/cli/status.ts +381 -0
  191. package/src/cli/timeout.ts +64 -0
  192. package/src/cli/version.ts +21 -0
  193. package/src/cli/wallet.ts +241 -0
  194. package/src/frameworks/crewai.ts +13 -2
  195. package/src/gateway/server-entry.ts +1 -0
  196. package/src/mcp/http-server.ts +1 -0
  197. package/src/mcp/server.ts +25 -1
  198. package/src/mcp/stdio.ts +1 -0
  199. package/src/sdk/client.ts +33 -1
@@ -0,0 +1,437 @@
1
+ import { streamEscrowV2Abi } from "@absol-labs/shared";
2
+ import {
3
+ MetrikClient,
4
+ type CheckpointProof,
5
+ type HireComputeInput,
6
+ type HireComputeResult,
7
+ type HireTxPreview,
8
+ type StreamV2,
9
+ type StreamProofTransactionResult,
10
+ } from "@absol-labs/sdk";
11
+ import { erc20Abi, getAbiItem, http, type LocalAccount } from "viem";
12
+
13
+ import {
14
+ discoverServicesDetailed,
15
+ type DiscoverResult,
16
+ type DiscoverServicesOptions,
17
+ type ServiceListing,
18
+ } from "../discovery/registry.js";
19
+ import {
20
+ invoke as sdkInvoke,
21
+ type InvokeOptions,
22
+ type InvokeResult,
23
+ type InvokeServiceInput,
24
+ type InvokeStreamReader,
25
+ } from "../sdk/invoke.js";
26
+ import {
27
+ VerifiedStreamAgentClient,
28
+ type MandateAuthorizedStreamActionInput,
29
+ type OpenVerifiedStreamInput,
30
+ type ReclaimAuthorizedStreamInput,
31
+ type ReclaimVerifiedStreamResult,
32
+ } from "../sdk/client.js";
33
+
34
+ import type { CliConfig } from "./config.js";
35
+ import { RPC_RETRY_COUNT, RPC_TIMEOUT_MS, withReadTimeout } from "./timeout.js";
36
+
37
+ /**
38
+ * Everything the CLI touches that is not pure computation, behind one
39
+ * injectable surface. Commands are written against these interfaces so the test
40
+ * suite exercises the real argument parsing, config resolution, testnet guard,
41
+ * retry logic and rendering against doubles — without a key, an RPC, or a
42
+ * single transaction.
43
+ */
44
+ export interface CliReadClient {
45
+ /** The chain id the RPC endpoint actually serves (half of the testnet guard). */
46
+ getChainId(): Promise<number>;
47
+ getStreamV2(streamId: `0x${string}`): Promise<StreamV2>;
48
+ /**
49
+ * The ONLY live reading of accrual. `StreamEscrowV2.getStream` reports
50
+ * `claimedCumulative = 0` and `settledCumulative = 0` for a stream's entire
51
+ * active life — `settledCumulative` is written by `reclaim`, not by accrual —
52
+ * so a status command that reads a stream field would always print zero. The
53
+ * oracle's checkpoint proof carries the real earned-so-far figure.
54
+ */
55
+ checkpointProof(streamId: `0x${string}`): Promise<CheckpointProof>;
56
+ claimableForV2(
57
+ streamId: `0x${string}`,
58
+ cumulativeAmount: bigint,
59
+ ): Promise<bigint>;
60
+ reclaimableForV2(
61
+ streamId: `0x${string}`,
62
+ cumulativeAmount: bigint,
63
+ ): Promise<bigint>;
64
+ /** Live protocol fee, read from the escrow rather than hardcoded. */
65
+ protocolFeeBps(): Promise<number>;
66
+ /**
67
+ * `StreamEscrowV2.escapeGraceSeconds` (public immutable). The buyer's no-proof
68
+ * escape hatch unlocks at `expiresAt + escapeGraceSeconds`, so this is half of
69
+ * every readiness answer the CLI gives about it.
70
+ */
71
+ escapeGraceSeconds(): Promise<number>;
72
+ /**
73
+ * `StreamEscrowV2.latestCheckpointCheckedAt` — how far the oracle has
74
+ * published. A buyer reading "the last checkpoint is two hours old" is reading
75
+ * the reason their proof-based reclaim is blocked.
76
+ */
77
+ latestCheckpointCheckedAt(): Promise<number>;
78
+ /**
79
+ * `StreamEscrowV2.getOracleSigners` — every address whose signature can carry
80
+ * a checkpoint into the escrow.
81
+ *
82
+ * Read so the CLI can say who attests. A buyer told a listing is
83
+ * "cryptographically verified" is entitled to know whether the operator they
84
+ * are about to pay is ALSO one of the parties attesting that the delivery
85
+ * happened; on the live deployment that overlap exists. Disclosure only — the
86
+ * CLI does not refuse such a listing, it names the fact.
87
+ */
88
+ oracleSigners(): Promise<readonly `0x${string}`[]>;
89
+ /**
90
+ * The timestamp of the latest block. The escape hatch is gated on
91
+ * `block.timestamp`, not on the caller's clock, so readiness is answered
92
+ * against the chain rather than against a laptop that may be skewed.
93
+ */
94
+ latestBlockTimestamp(): Promise<number>;
95
+ /**
96
+ * Native balance of an address, in wei. Read before a fund-moving command so
97
+ * an unfunded wallet is reported as an unfunded wallet, rather than as the
98
+ * raw node error `gas required exceeds allowance (0)`.
99
+ */
100
+ nativeBalance(address: `0x${string}`): Promise<bigint>;
101
+ /**
102
+ * USDC balance of an address, in base units. Gas and deposit are SEPARATE
103
+ * balances, so `assertCanPayGas` passing says nothing about whether the hire
104
+ * can be funded — see `assertCanFundStream`.
105
+ */
106
+ usdcBalance(address: `0x${string}`): Promise<bigint>;
107
+ /** Current chain head, the upper bound of every log scan. */
108
+ blockNumber(): Promise<bigint>;
109
+ /**
110
+ * `StreamOpened` logs for one indexed party over one inclusive block window.
111
+ *
112
+ * `buyer` and `operator` are INDEXED on the event, so `role` becomes an
113
+ * `eth_getLogs` topic filter the NODE evaluates: the response carries only
114
+ * that wallet's streams regardless of how wide the window is. What bounds a
115
+ * window is the endpoint's per-call BLOCK RANGE cap, not the response size —
116
+ * see `./scan-range.ts`.
117
+ */
118
+ streamOpenedLogs(query: StreamOpenedQuery): Promise<readonly OpenedStream[]>;
119
+ /**
120
+ * Count of ANY escrow log in a single block. Used as a known-answer retention
121
+ * canary: a node that has pruned its log index answers a range it no longer
122
+ * retains with a SUCCESSFUL EMPTY result, so an empty scan is only honest
123
+ * from an endpoint that demonstrably still serves the oldest block in range.
124
+ */
125
+ escrowLogCountAt(block: bigint): Promise<number>;
126
+ }
127
+
128
+ /** One indexed party filter over one inclusive block window. */
129
+ export interface StreamOpenedQuery {
130
+ readonly role: "buyer" | "operator";
131
+ readonly address: `0x${string}`;
132
+ readonly fromBlock: bigint;
133
+ readonly toBlock: bigint;
134
+ }
135
+
136
+ /** The immutable facts `StreamOpened` carries, which later state cannot restate. */
137
+ export interface OpenedStream {
138
+ readonly streamId: `0x${string}`;
139
+ readonly buyer: `0x${string}`;
140
+ readonly operator: `0x${string}`;
141
+ readonly serviceRef: `0x${string}`;
142
+ /**
143
+ * The amount originally escrowed. V2 MUTATES `Stream.deposit` on reclaim, so
144
+ * the event is the only place the opening figure survives.
145
+ */
146
+ readonly deposit: bigint;
147
+ readonly ratePerSecond: bigint;
148
+ readonly expiresAt: number;
149
+ readonly blockNumber: bigint;
150
+ }
151
+
152
+ export interface CliWriteClient {
153
+ /** Build + best-effort simulate the hire WITHOUT broadcasting (`--dry-run`). */
154
+ buildHireTx(input: HireComputeInput): Promise<HireTxPreview>;
155
+ openVerifiedStream(
156
+ input: OpenVerifiedStreamInput,
157
+ ): Promise<HireComputeResult>;
158
+ closeStream(
159
+ input: MandateAuthorizedStreamActionInput,
160
+ ): Promise<StreamProofTransactionResult>;
161
+ reclaimStream(
162
+ input: ReclaimAuthorizedStreamInput,
163
+ ): Promise<ReclaimVerifiedStreamResult>;
164
+ /**
165
+ * The no-proof escape hatch. Separate from {@link reclaimStream} because it is
166
+ * a different transaction with a different outcome, not a mode of the same one
167
+ * — see `VerifiedStreamAgentClient.reclaimUnverifiedStream`.
168
+ */
169
+ reclaimUnverifiedStream(
170
+ input: MandateAuthorizedStreamActionInput,
171
+ ): Promise<StreamProofTransactionResult>;
172
+ /** Operator-only: redeem the verified-earned balance for a stream. */
173
+ claimStream(
174
+ input: MandateAuthorizedStreamActionInput,
175
+ ): Promise<StreamProofTransactionResult>;
176
+ }
177
+
178
+ export interface CliDependencies {
179
+ readonly createReadClient: (config: CliConfig) => CliReadClient;
180
+ /**
181
+ * `account` is nullable because `open --dry-run` broadcasts nothing and must
182
+ * work for someone who has no key yet. Without one the SDK still builds the
183
+ * calldata and simply reports that the allowance read and simulation were
184
+ * skipped; every fund-moving method throws rather than inventing a signer.
185
+ */
186
+ readonly createWriteClient: (
187
+ config: CliConfig,
188
+ account: LocalAccount | null,
189
+ ) => CliWriteClient;
190
+ readonly discover: (
191
+ options: DiscoverServicesOptions,
192
+ ) => Promise<DiscoverResult>;
193
+ readonly invoke: (
194
+ streamId: `0x${string}`,
195
+ request: InvokeServiceInput,
196
+ options: InvokeOptions,
197
+ ) => Promise<InvokeResult>;
198
+ readonly createInvokeStreamReader: (config: CliConfig) => InvokeStreamReader;
199
+ readonly nowSeconds: () => number;
200
+ readonly sleep: (ms: number) => Promise<void>;
201
+ }
202
+
203
+ /**
204
+ * The `StreamOpened` ABI entry, resolved once. Taken from the shared V2 ABI
205
+ * rather than re-declared: a hand-written copy that drifted by one indexed
206
+ * argument would silently match nothing.
207
+ */
208
+ const STREAM_OPENED_EVENT = getAbiItem({
209
+ abi: streamEscrowV2Abi,
210
+ name: "StreamOpened",
211
+ });
212
+
213
+ /**
214
+ * The RPC transport, with an explicit per-call budget.
215
+ *
216
+ * viem's defaults leave a single `eth_call` able to hang for a long time behind
217
+ * three backing-off retries, which is how a `status` run reached two minutes
218
+ * with nothing on screen. Bounding it here bounds every read and every
219
+ * simulation the SDK performs, in one place.
220
+ */
221
+ function rpcTransport(config: CliConfig) {
222
+ return http(config.rpcUrl, {
223
+ timeout: RPC_TIMEOUT_MS,
224
+ retryCount: RPC_RETRY_COUNT,
225
+ });
226
+ }
227
+
228
+ /** Shared `MetrikClient` construction: one escrow, declared as V2 on both keys. */
229
+ function createMetrikClient(
230
+ config: CliConfig,
231
+ account?: LocalAccount,
232
+ ): MetrikClient {
233
+ return new MetrikClient({
234
+ chain: config.chain,
235
+ transport: rpcTransport(config),
236
+ escrow: config.escrow,
237
+ // Declared on BOTH keys: without `escrowV2` the SDK routes reads and
238
+ // settlement through the legacy V1 ABI, which reverts against
239
+ // StreamEscrowV2 (its one-argument views were replaced by proof-carrying
240
+ // ones, and `reclaim` becomes `reclaimWithProof`).
241
+ escrowV2: config.escrow,
242
+ usdc: config.usdc,
243
+ oracleBaseUrl: config.oracleBaseUrl,
244
+ settlementTarget: { chainId: config.chainId, escrow: config.escrow },
245
+ ...(account === undefined ? {} : { account }),
246
+ });
247
+ }
248
+
249
+ export const defaultCliDependencies: CliDependencies = {
250
+ createReadClient(config) {
251
+ const metrik = createMetrikClient(config);
252
+ return {
253
+ async getChainId() {
254
+ return await metrik.publicClient.getChainId();
255
+ },
256
+ async getStreamV2(streamId) {
257
+ return await metrik.getStreamV2(streamId);
258
+ },
259
+ async checkpointProof(streamId) {
260
+ // Not an RPC call: this is an HTTP fetch against the oracle, so the
261
+ // transport budget above does not cover it.
262
+ return await withReadTimeout(
263
+ async () => await metrik.checkpointProof(streamId),
264
+ `the oracle at ${config.oracleBaseUrl}`,
265
+ );
266
+ },
267
+ async claimableForV2(streamId, cumulativeAmount) {
268
+ return await metrik.claimableForV2(streamId, cumulativeAmount);
269
+ },
270
+ async reclaimableForV2(streamId, cumulativeAmount) {
271
+ return await metrik.reclaimableForV2(streamId, cumulativeAmount);
272
+ },
273
+ async nativeBalance(address) {
274
+ return await metrik.publicClient.getBalance({ address });
275
+ },
276
+ async usdcBalance(address) {
277
+ return await metrik.publicClient.readContract({
278
+ address: config.usdc,
279
+ abi: erc20Abi,
280
+ functionName: "balanceOf",
281
+ args: [address],
282
+ });
283
+ },
284
+ async blockNumber() {
285
+ return await metrik.publicClient.getBlockNumber();
286
+ },
287
+ async streamOpenedLogs({ role, address, fromBlock, toBlock }) {
288
+ const logs = await metrik.publicClient.getLogs({
289
+ address: config.escrow,
290
+ event: STREAM_OPENED_EVENT,
291
+ args: { [role]: address },
292
+ fromBlock,
293
+ toBlock,
294
+ });
295
+ // Returned as-is: the topic filter is evaluated by the node, so
296
+ // re-checking `log.args.buyer === address` here would imply the filter
297
+ // is decorative and would hide a mis-built filter behind a second pass.
298
+ // A wrong filter should produce a visibly wrong result, not a quietly
299
+ // correct one.
300
+ return logs.flatMap((log) =>
301
+ log.args.streamId === undefined ||
302
+ log.args.buyer === undefined ||
303
+ log.args.operator === undefined ||
304
+ log.args.serviceRef === undefined ||
305
+ log.args.deposit === undefined ||
306
+ log.args.ratePerSecond === undefined ||
307
+ log.args.expiresAt === undefined
308
+ ? []
309
+ : [
310
+ {
311
+ streamId: log.args.streamId,
312
+ buyer: log.args.buyer,
313
+ operator: log.args.operator,
314
+ serviceRef: log.args.serviceRef,
315
+ deposit: log.args.deposit,
316
+ ratePerSecond: log.args.ratePerSecond,
317
+ expiresAt: Number(log.args.expiresAt),
318
+ blockNumber: log.blockNumber ?? 0n,
319
+ },
320
+ ],
321
+ );
322
+ },
323
+ async escrowLogCountAt(block) {
324
+ const logs = await metrik.publicClient.getLogs({
325
+ address: config.escrow,
326
+ fromBlock: block,
327
+ toBlock: block,
328
+ });
329
+ return logs.length;
330
+ },
331
+ async protocolFeeBps() {
332
+ const bps = await metrik.publicClient.readContract({
333
+ address: config.escrow,
334
+ abi: streamEscrowV2Abi,
335
+ functionName: "protocolFeeBps",
336
+ });
337
+ return Number(bps);
338
+ },
339
+ async escapeGraceSeconds() {
340
+ const seconds = await metrik.publicClient.readContract({
341
+ address: config.escrow,
342
+ abi: streamEscrowV2Abi,
343
+ functionName: "escapeGraceSeconds",
344
+ });
345
+ return Number(seconds);
346
+ },
347
+ async latestCheckpointCheckedAt() {
348
+ const checkedAt = await metrik.publicClient.readContract({
349
+ address: config.escrow,
350
+ abi: streamEscrowV2Abi,
351
+ functionName: "latestCheckpointCheckedAt",
352
+ });
353
+ return Number(checkedAt);
354
+ },
355
+ async latestBlockTimestamp() {
356
+ const block = await metrik.publicClient.getBlock();
357
+ return Number(block.timestamp);
358
+ },
359
+ async oracleSigners() {
360
+ return await metrik.publicClient.readContract({
361
+ address: config.escrow,
362
+ abi: streamEscrowV2Abi,
363
+ functionName: "getOracleSigners",
364
+ });
365
+ },
366
+ };
367
+ },
368
+ createWriteClient(config, account) {
369
+ const clientConfig = {
370
+ chain: config.chain,
371
+ transport: rpcTransport(config),
372
+ escrow: config.escrow,
373
+ escrowV2: config.escrow,
374
+ usdc: config.usdc,
375
+ oracleBaseUrl: config.oracleBaseUrl,
376
+ settlementTarget: { chainId: config.chainId, escrow: config.escrow },
377
+ ...(account === null ? {} : { account }),
378
+ };
379
+ // The mandate-gated agent client owns every fund-moving call; the raw SDK
380
+ // client is used only for the build-only `--dry-run` preview, which
381
+ // broadcasts nothing.
382
+ const agentClient = new VerifiedStreamAgentClient(clientConfig);
383
+ const metrik = createMetrikClient(
384
+ config,
385
+ ...(account === null ? [] : ([account] as const)),
386
+ );
387
+ return {
388
+ async buildHireTx(input) {
389
+ return await metrik.buildHireTx(input);
390
+ },
391
+ async openVerifiedStream(input) {
392
+ return await agentClient.openVerifiedStream(input);
393
+ },
394
+ async closeStream(input) {
395
+ return await agentClient.closeStream(input);
396
+ },
397
+ async reclaimStream(input) {
398
+ return await agentClient.reclaimStream(input);
399
+ },
400
+ async reclaimUnverifiedStream(input) {
401
+ return await agentClient.reclaimUnverifiedStream(input);
402
+ },
403
+ async claimStream(input) {
404
+ return await agentClient.claimStream(input);
405
+ },
406
+ };
407
+ },
408
+ async discover(options) {
409
+ return await discoverServicesDetailed(options);
410
+ },
411
+ async invoke(streamId, request, options) {
412
+ return await sdkInvoke(streamId, request, options);
413
+ },
414
+ createInvokeStreamReader(config) {
415
+ const metrik = createMetrikClient(config);
416
+ return {
417
+ async getStreamV2(streamId) {
418
+ const stream = await metrik.getStreamV2(streamId);
419
+ return {
420
+ buyer: stream.buyer,
421
+ operator: stream.operator,
422
+ serviceRef: stream.serviceRef,
423
+ status: stream.status,
424
+ expiresAt: stream.expiresAt,
425
+ };
426
+ },
427
+ };
428
+ },
429
+ nowSeconds() {
430
+ return Math.floor(Date.now() / 1000);
431
+ },
432
+ async sleep(ms) {
433
+ await new Promise((resolve) => setTimeout(resolve, ms));
434
+ },
435
+ };
436
+
437
+ export type { DiscoverResult, ServiceListing };
@@ -0,0 +1,133 @@
1
+ import type {
2
+ SignedServiceBinding,
3
+ SignedServiceDescriptor,
4
+ } from "@absol-labs/shared";
5
+
6
+ import type { DiscoveredService } from "../discovery/registry.js";
7
+
8
+ /**
9
+ * What a listing's SIGNED descriptor already tells a newcomer — surfaced rather
10
+ * than discarded.
11
+ *
12
+ * The registry's signed records carry the callable endpoints, one worked example
13
+ * call, and the operator's accepted rate band. `metrik discover` used to project
14
+ * them away, so the CLI could show a `serviceRef` but never what the service
15
+ * DOES, what path to call, or what `--rate` the operator will accept. Everything
16
+ * here is read from the record whose signature was already recovered and
17
+ * matched, so nothing unsigned is ever presented as fact.
18
+ */
19
+ export interface ListingEndpoint {
20
+ readonly name: string;
21
+ readonly path: string;
22
+ readonly method: string;
23
+ readonly description: string | null;
24
+ }
25
+
26
+ export interface ListingExample {
27
+ readonly endpoint: string;
28
+ readonly description: string | null;
29
+ readonly request: unknown;
30
+ readonly response: unknown;
31
+ }
32
+
33
+ export interface ListingRates {
34
+ /** USDC base units per second, as signed (decimal strings, never floats). */
35
+ readonly minRatePerSecond: string | null;
36
+ readonly maxRatePerSecond: string | null;
37
+ readonly currency: string | null;
38
+ readonly slaDescription: string | null;
39
+ readonly refundPolicy: string | null;
40
+ }
41
+
42
+ export interface ListingDetail {
43
+ readonly endpoints: readonly ListingEndpoint[];
44
+ readonly example: ListingExample | null;
45
+ readonly rates: ListingRates | null;
46
+ readonly authModel: string | null;
47
+ }
48
+
49
+ const EMPTY_DETAIL: ListingDetail = {
50
+ endpoints: [],
51
+ example: null,
52
+ rates: null,
53
+ authModel: null,
54
+ };
55
+
56
+ /**
57
+ * Project the signed record onto the presentable detail. A legacy binding (which
58
+ * carries no interface at all) yields empty detail rather than an invented one.
59
+ */
60
+ export function listingDetail(
61
+ signed: SignedServiceBinding | SignedServiceDescriptor | undefined,
62
+ ): ListingDetail {
63
+ const descriptor = (signed as SignedServiceDescriptor | undefined)
64
+ ?.descriptor;
65
+ if (descriptor === undefined) return EMPTY_DETAIL;
66
+
67
+ const iface = descriptor.interface;
68
+ const commercial = descriptor.commercial;
69
+ return {
70
+ endpoints: (iface?.endpoints ?? []).map((endpoint) => ({
71
+ name: endpoint.name,
72
+ path: endpoint.path,
73
+ method: endpoint.method,
74
+ description: endpoint.description ?? null,
75
+ })),
76
+ example:
77
+ iface?.example === undefined
78
+ ? null
79
+ : {
80
+ endpoint: iface.example.endpoint,
81
+ description: iface.example.description ?? null,
82
+ request: iface.example.request ?? null,
83
+ response: iface.example.response ?? null,
84
+ },
85
+ rates:
86
+ commercial === undefined
87
+ ? null
88
+ : {
89
+ minRatePerSecond: commercial.minRatePerSecond ?? null,
90
+ maxRatePerSecond: commercial.maxRatePerSecond ?? null,
91
+ currency: commercial.currency ?? null,
92
+ slaDescription: commercial.slaDescription ?? null,
93
+ refundPolicy: commercial.refundPolicy ?? null,
94
+ },
95
+ authModel: iface?.authModel ?? null,
96
+ };
97
+ }
98
+
99
+ /**
100
+ * How much verification a listing actually declares. Used to order `discover`
101
+ * strongest-first so `--limit 1` returns the best-attested listing rather than
102
+ * whatever the registry happened to serve first — which is how a newcomer's very
103
+ * first result ended up being a deliberately-failing test fixture.
104
+ *
105
+ * This ranks the DECLARED verification spec, which is the only thing the signed
106
+ * record can tell us. It is a presentation order, not a trust score.
107
+ */
108
+ export function verificationStrength(service: DiscoveredService): number {
109
+ const summary = service.verification;
110
+ if (summary === null) return 0;
111
+ return (
112
+ (summary.hasReexec ? 8 : 0) +
113
+ (summary.hasSchema ? 4 : 0) +
114
+ (summary.hasSla ? 2 : 0) +
115
+ Math.min(summary.canaries, 1)
116
+ );
117
+ }
118
+
119
+ /**
120
+ * Deterministic, strongest-verification-first order. Ties break on `serviceRef`
121
+ * so the same registry always renders in the same order and `--limit N` is
122
+ * reproducible between runs.
123
+ */
124
+ export function orderListings<T extends DiscoveredService>(
125
+ services: readonly T[],
126
+ ): T[] {
127
+ return [...services].sort((left, right) => {
128
+ if (left.tier !== right.tier) return right.tier - left.tier;
129
+ const strength = verificationStrength(right) - verificationStrength(left);
130
+ if (strength !== 0) return strength;
131
+ return left.serviceRef.localeCompare(right.serviceRef);
132
+ });
133
+ }