@sluzen/mcp 0.0.0-stage → 0.1.2

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.
@@ -0,0 +1,119 @@
1
+ import { selectAccept, type AnnouncedRoute, type CallReceipt, type TaskCallBinding, type X402Challenge } from "@sluzen/sdk";
2
+ import { type McpRuntimeConfig } from "./config.js";
3
+ export type ToolOk<T> = {
4
+ ok: true;
5
+ data: T;
6
+ };
7
+ export type ToolErr = {
8
+ ok: false;
9
+ error: string;
10
+ cause?: string;
11
+ };
12
+ export type ToolResult<T> = ToolOk<T> | ToolErr;
13
+ export declare function toolHealth(runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
14
+ /**
15
+ * Find: public announced sellable proxy capabilities (no seed).
16
+ * Empty routes is valid. Binding price remains the 402 challenge.
17
+ */
18
+ export declare function toolListAnnouncedRoutes(runtime?: McpRuntimeConfig): Promise<ToolResult<{
19
+ routes: AnnouncedRoute[];
20
+ }>>;
21
+ export declare function toolFacilitatorSupported(runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
22
+ export declare function toolChallenge(args: {
23
+ surface: "proxy" | "facilitator";
24
+ path?: string;
25
+ resource?: string;
26
+ channelId?: string;
27
+ method?: string;
28
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
29
+ paymentRequired: string;
30
+ challenge: X402Challenge;
31
+ accept: ReturnType<typeof selectAccept>;
32
+ callId?: string;
33
+ status: number;
34
+ }>>;
35
+ export declare function toolSignPaymentXrp(args: {
36
+ challenge: X402Challenge | string;
37
+ walletSeed?: string;
38
+ requestUrl?: string;
39
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
40
+ paymentSignature: string;
41
+ }>>;
42
+ export declare function toolSignPaymentRlusd(args: {
43
+ challenge: X402Challenge | string;
44
+ walletSeed?: string;
45
+ requestUrl?: string;
46
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
47
+ paymentSignature: string;
48
+ }>>;
49
+ export declare function toolSignPaychanClaim(args: {
50
+ channelId: string;
51
+ cumulativeDrops: string;
52
+ walletSeed?: string;
53
+ challenge?: X402Challenge | string;
54
+ deltaDrops?: string;
55
+ requestUrl?: string;
56
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
57
+ channelId: string;
58
+ claimSignature: string;
59
+ cumulativeDrops: string;
60
+ paymentSignature: string;
61
+ publicKey: string;
62
+ account: string;
63
+ }>>;
64
+ export declare function toolProxyPay(args: {
65
+ path: string;
66
+ paymentSignature: string;
67
+ method?: string;
68
+ body?: string;
69
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
70
+ status: number;
71
+ data: unknown;
72
+ paymentResponse?: string;
73
+ callId?: string;
74
+ }>>;
75
+ export declare function toolFacilitatorVerify(args: {
76
+ resource: string;
77
+ paymentSignature?: string;
78
+ channelId?: string;
79
+ claimSignature?: string;
80
+ cumulativeDrops?: string;
81
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
82
+ export declare function toolFacilitatorSettle(args: {
83
+ resource: string;
84
+ paymentSignature: string;
85
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
86
+ export declare function toolFacilitatorAuthorize(args: {
87
+ resource: string;
88
+ channelId: string;
89
+ claimSignature: string;
90
+ cumulativeDrops: string;
91
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
92
+ /**
93
+ * Prove: public joined receipt for one callId (settle + delivery when recorded).
94
+ * Facilitator never invents a Sluzen delivery row.
95
+ */
96
+ export declare function toolGetCallReceipt(args: {
97
+ callId: string;
98
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<CallReceipt>>;
99
+ /**
100
+ * Local Prove bridge: bind orchestrator taskId/goalId to callId in process memory.
101
+ * Does not call Sluzen; does not store paid bodies.
102
+ */
103
+ export declare function toolBindTaskCall(args: {
104
+ callId: string;
105
+ taskId?: string;
106
+ goalId?: string;
107
+ resultRef?: string;
108
+ }, runtime?: McpRuntimeConfig): ToolResult<TaskCallBinding>;
109
+ /** Lookup a local task↔call binding (process memory only). */
110
+ export declare function toolLookupTaskCall(args: {
111
+ callId?: string;
112
+ taskId?: string;
113
+ goalId?: string;
114
+ }, runtime?: McpRuntimeConfig): ToolResult<TaskCallBinding>;
115
+ /** Assert accept is PayChan with usable channel params (fail-closed helper). */
116
+ export declare function assertPayChanParams(challenge: X402Challenge): ToolResult<{
117
+ channelId: string;
118
+ minCumulativeDrops?: string;
119
+ }>;
package/dist/tools.js ADDED
@@ -0,0 +1,509 @@
1
+ import { assertDiscoveryListed, assertMachinePolicyAllowsPayChanClaim, assertMachinePolicyAllowsSign, assertMachinePolicySessionAllows, commitSpendSession, decodeChallenge, encodePayChanPaymentSignature, facilitatorAuthorize, facilitatorChallenge, facilitatorSettle, facilitatorSupported, facilitatorVerify, getCallReceipt, isAllowlistedIou, isExactAccept, isPayChanAccept, isRlusdProductCurrency, isXrpDropsCurrency, listAnnouncedRoutes, PAYMENT_REQUIRED_HEADER, PAYMENT_RESPONSE_HEADER, PAYMENT_SIGNATURE_HEADER, CALL_ID_HEADER, selectAccept, signExactPayment, signPayChanClaim, } from "@sluzen/sdk";
2
+ import { createRuntime, requireSeed, } from "./config.js";
3
+ function fail(error, cause) {
4
+ return cause ? { ok: false, error, cause } : { ok: false, error };
5
+ }
6
+ function ok(data) {
7
+ return { ok: true, data };
8
+ }
9
+ function catchFail(err) {
10
+ if (err instanceof Error) {
11
+ const cause = "reason" in err && typeof err.reason === "string"
12
+ ? err.reason
13
+ : undefined;
14
+ return fail(err.message, cause);
15
+ }
16
+ return fail(String(err));
17
+ }
18
+ function policyRequestUrl(runtime, requestUrl) {
19
+ const override = requestUrl?.trim();
20
+ if (override && override.length > 0) {
21
+ return override;
22
+ }
23
+ return `${runtime.apiBase}/`;
24
+ }
25
+ async function enforceListingIfRequired(runtime, requestUrl) {
26
+ const policy = runtime.machinePolicy;
27
+ if (policy == null || policy.requireDiscoveryListed !== true) {
28
+ return;
29
+ }
30
+ const discoveryBase = policy.discoveryBaseUrl?.replace(/\/$/, "") || runtime.apiBase;
31
+ const routes = await listAnnouncedRoutes({
32
+ baseUrl: discoveryBase,
33
+ fetchFn: runtime.fetchFn,
34
+ });
35
+ assertDiscoveryListed({ routes, requestUrl });
36
+ }
37
+ function commitSessionDrops(runtime, addDrops) {
38
+ if (runtime.machinePolicy == null) {
39
+ return;
40
+ }
41
+ const next = commitSpendSession(runtime.spendSession, addDrops);
42
+ runtime.spendSession.spentDrops = next.spentDrops;
43
+ runtime.spendSession.callCount = next.callCount;
44
+ }
45
+ async function enforceExactSignPolicy(runtime, challenge, requestUrl) {
46
+ const policy = runtime.machinePolicy;
47
+ if (policy == null) {
48
+ return;
49
+ }
50
+ const url = policyRequestUrl(runtime, requestUrl);
51
+ await enforceListingIfRequired(runtime, url);
52
+ assertMachinePolicyAllowsSign({
53
+ policy,
54
+ challenge,
55
+ requestUrl: url,
56
+ });
57
+ const addDrops = isXrpDropsCurrency(challenge.currency) ? challenge.amount : 0;
58
+ assertMachinePolicySessionAllows({
59
+ policy,
60
+ state: runtime.spendSession,
61
+ addDrops,
62
+ });
63
+ }
64
+ async function enforcePayChanSignPolicy(runtime, args) {
65
+ const policy = runtime.machinePolicy;
66
+ if (policy == null) {
67
+ return { addDrops: 0 };
68
+ }
69
+ const url = policyRequestUrl(runtime, args.requestUrl);
70
+ await enforceListingIfRequired(runtime, url);
71
+ assertMachinePolicyAllowsPayChanClaim({
72
+ policy,
73
+ cumulativeDrops: args.cumulativeDrops,
74
+ deltaDrops: args.deltaDrops,
75
+ challenge: args.challenge,
76
+ requestUrl: url,
77
+ });
78
+ let addDrops = 0;
79
+ if (args.deltaDrops != null) {
80
+ addDrops = Number(args.deltaDrops);
81
+ }
82
+ else if (args.challenge != null) {
83
+ addDrops = args.challenge.amount;
84
+ }
85
+ if (!Number.isSafeInteger(addDrops) || addDrops < 0) {
86
+ addDrops = 0;
87
+ }
88
+ assertMachinePolicySessionAllows({
89
+ policy,
90
+ state: runtime.spendSession,
91
+ addDrops,
92
+ });
93
+ return { addDrops };
94
+ }
95
+ function parseChallengeJson(challenge) {
96
+ if (typeof challenge === "string") {
97
+ const trimmed = challenge.trim();
98
+ if (trimmed.startsWith("{")) {
99
+ return JSON.parse(trimmed);
100
+ }
101
+ return decodeChallenge(trimmed);
102
+ }
103
+ return challenge;
104
+ }
105
+ export async function toolHealth(runtime = createRuntime()) {
106
+ try {
107
+ const response = await runtime.fetchFn(`${runtime.apiBase}/health`);
108
+ const text = await response.text();
109
+ let body = text;
110
+ try {
111
+ body = text.length > 0 ? JSON.parse(text) : {};
112
+ }
113
+ catch {
114
+ /* keep text */
115
+ }
116
+ if (!response.ok) {
117
+ return fail(`health HTTP ${response.status}`);
118
+ }
119
+ return ok(body);
120
+ }
121
+ catch (err) {
122
+ return catchFail(err);
123
+ }
124
+ }
125
+ /**
126
+ * Find: public announced sellable proxy capabilities (no seed).
127
+ * Empty routes is valid. Binding price remains the 402 challenge.
128
+ */
129
+ export async function toolListAnnouncedRoutes(runtime = createRuntime()) {
130
+ try {
131
+ const discoveryBase = runtime.machinePolicy?.discoveryBaseUrl?.replace(/\/$/, "") ||
132
+ runtime.apiBase;
133
+ const routes = await listAnnouncedRoutes({
134
+ baseUrl: discoveryBase,
135
+ fetchFn: runtime.fetchFn,
136
+ });
137
+ return ok({ routes });
138
+ }
139
+ catch (err) {
140
+ return catchFail(err);
141
+ }
142
+ }
143
+ export async function toolFacilitatorSupported(runtime = createRuntime()) {
144
+ try {
145
+ const data = await facilitatorSupported({
146
+ apiBase: runtime.apiBase,
147
+ fetchFn: runtime.fetchFn,
148
+ });
149
+ return ok(data);
150
+ }
151
+ catch (err) {
152
+ return catchFail(err);
153
+ }
154
+ }
155
+ export async function toolChallenge(args, runtime = createRuntime()) {
156
+ try {
157
+ if (args.surface === "facilitator") {
158
+ const resource = args.resource?.trim();
159
+ if (!resource) {
160
+ return fail("resource is required for facilitator challenge");
161
+ }
162
+ const issued = await facilitatorChallenge({
163
+ apiBase: runtime.apiBase,
164
+ resource,
165
+ channelId: args.channelId,
166
+ fetchFn: runtime.fetchFn,
167
+ });
168
+ const challenge = decodeChallenge(issued.paymentRequired);
169
+ const accept = selectAccept(challenge);
170
+ return ok({
171
+ paymentRequired: issued.paymentRequired,
172
+ challenge,
173
+ accept,
174
+ callId: issued.callId ?? challenge.callId,
175
+ status: 402,
176
+ });
177
+ }
178
+ const path = args.path?.trim();
179
+ if (!path || !path.startsWith("/")) {
180
+ return fail("path starting with / is required for proxy challenge");
181
+ }
182
+ const method = (args.method ?? "GET").toUpperCase();
183
+ const response = await runtime.fetchFn(`${runtime.apiBase}${path}`, {
184
+ method,
185
+ });
186
+ const paymentRequired = response.headers.get(PAYMENT_REQUIRED_HEADER);
187
+ if (response.status !== 402 || !paymentRequired) {
188
+ return fail(response.status === 402
189
+ ? "402 without PAYMENT-REQUIRED (settlement/bind failure, not a quote)"
190
+ : `expected 402, got HTTP ${response.status}`, "challenge_failed");
191
+ }
192
+ const challenge = decodeChallenge(paymentRequired);
193
+ const accept = selectAccept(challenge);
194
+ const callId = response.headers.get(CALL_ID_HEADER) ?? challenge.callId ?? undefined;
195
+ return ok({
196
+ paymentRequired,
197
+ challenge,
198
+ accept,
199
+ callId,
200
+ status: 402,
201
+ });
202
+ }
203
+ catch (err) {
204
+ return catchFail(err);
205
+ }
206
+ }
207
+ export async function toolSignPaymentXrp(args, runtime = createRuntime()) {
208
+ try {
209
+ const seed = requireSeed(args.walletSeed ?? runtime.walletSeed);
210
+ const challenge = parseChallengeJson(args.challenge);
211
+ const accept = selectAccept(challenge);
212
+ if (!isExactAccept(accept) || accept.asset !== "XRP") {
213
+ return fail("challenge is not exact XRP Payment", "scheme_mismatch");
214
+ }
215
+ if (!isXrpDropsCurrency(challenge.currency)) {
216
+ return fail(`currency ${challenge.currency} is not XRP`, "iou_not_supported");
217
+ }
218
+ await enforceExactSignPolicy(runtime, challenge, args.requestUrl);
219
+ const paymentSignature = await signExactPayment({
220
+ challenge,
221
+ walletSeed: seed,
222
+ node: runtime.xrplNode,
223
+ });
224
+ commitSessionDrops(runtime, challenge.amount);
225
+ return ok({ paymentSignature });
226
+ }
227
+ catch (err) {
228
+ return catchFail(err);
229
+ }
230
+ }
231
+ export async function toolSignPaymentRlusd(args, runtime = createRuntime()) {
232
+ try {
233
+ const seed = requireSeed(args.walletSeed ?? runtime.walletSeed);
234
+ const challenge = parseChallengeJson(args.challenge);
235
+ const accept = selectAccept(challenge);
236
+ if (!isExactAccept(accept) || accept.asset !== "RLUSD") {
237
+ return fail("challenge is not exact allowlisted RLUSD Payment", "scheme_mismatch");
238
+ }
239
+ if (!isRlusdProductCurrency(challenge.currency) || challenge.iou == null) {
240
+ return fail("RLUSD challenge missing iou", "iou_not_supported");
241
+ }
242
+ if (!isAllowlistedIou({
243
+ currency: challenge.iou.currency,
244
+ issuer: challenge.iou.issuer,
245
+ network: challenge.network,
246
+ })) {
247
+ return fail("IOU issuer/currency not on Sluzen allowlist", "iou_not_supported");
248
+ }
249
+ await enforceExactSignPolicy(runtime, challenge, args.requestUrl);
250
+ const paymentSignature = await signExactPayment({
251
+ challenge,
252
+ walletSeed: seed,
253
+ node: runtime.xrplNode,
254
+ });
255
+ commitSessionDrops(runtime, 0);
256
+ return ok({ paymentSignature });
257
+ }
258
+ catch (err) {
259
+ return catchFail(err);
260
+ }
261
+ }
262
+ export async function toolSignPaychanClaim(args, runtime = createRuntime()) {
263
+ try {
264
+ const seed = requireSeed(args.walletSeed ?? runtime.walletSeed);
265
+ const channelId = args.channelId.trim().toUpperCase();
266
+ if (!/^[0-9A-F]{64}$/.test(channelId)) {
267
+ return fail("channelId must be 64 hex characters", "invalid_channel_id");
268
+ }
269
+ const challenge = args.challenge != null ? parseChallengeJson(args.challenge) : undefined;
270
+ const { addDrops } = await enforcePayChanSignPolicy(runtime, {
271
+ cumulativeDrops: args.cumulativeDrops,
272
+ deltaDrops: args.deltaDrops,
273
+ challenge,
274
+ requestUrl: args.requestUrl,
275
+ });
276
+ const claim = signPayChanClaim({
277
+ channelId,
278
+ cumulativeDrops: args.cumulativeDrops,
279
+ walletSeed: seed,
280
+ });
281
+ const paymentSignature = encodePayChanPaymentSignature(claim);
282
+ commitSessionDrops(runtime, addDrops);
283
+ return ok({
284
+ channelId: claim.channelId,
285
+ claimSignature: claim.claimSignature,
286
+ cumulativeDrops: claim.cumulativeDrops,
287
+ paymentSignature,
288
+ publicKey: claim.publicKey,
289
+ account: claim.account,
290
+ });
291
+ }
292
+ catch (err) {
293
+ return catchFail(err);
294
+ }
295
+ }
296
+ export async function toolProxyPay(args, runtime = createRuntime()) {
297
+ try {
298
+ const path = args.path.trim();
299
+ if (!path.startsWith("/")) {
300
+ return fail("path must start with /");
301
+ }
302
+ if (!args.paymentSignature || args.paymentSignature.trim().length === 0) {
303
+ return fail("paymentSignature is required");
304
+ }
305
+ const method = (args.method ?? "GET").toUpperCase();
306
+ const headers = new Headers({
307
+ [PAYMENT_SIGNATURE_HEADER]: args.paymentSignature,
308
+ });
309
+ const init = { method, headers };
310
+ if (args.body != null && method !== "GET" && method !== "HEAD") {
311
+ headers.set("content-type", "application/json");
312
+ init.body = args.body;
313
+ }
314
+ const response = await runtime.fetchFn(`${runtime.apiBase}${path}`, init);
315
+ const text = await response.text();
316
+ let data = text;
317
+ const ct = response.headers.get("content-type") ?? "";
318
+ if (ct.includes("application/json") && text.length > 0) {
319
+ try {
320
+ data = JSON.parse(text);
321
+ }
322
+ catch {
323
+ /* keep text */
324
+ }
325
+ }
326
+ return ok({
327
+ status: response.status,
328
+ data,
329
+ paymentResponse: response.headers.get(PAYMENT_RESPONSE_HEADER) ?? undefined,
330
+ callId: response.headers.get(CALL_ID_HEADER) ?? undefined,
331
+ });
332
+ }
333
+ catch (err) {
334
+ return catchFail(err);
335
+ }
336
+ }
337
+ export async function toolFacilitatorVerify(args, runtime = createRuntime()) {
338
+ try {
339
+ const resource = args.resource.trim();
340
+ if (!resource) {
341
+ return fail("resource is required");
342
+ }
343
+ if (args.paymentSignature != null && args.paymentSignature.length > 0) {
344
+ const verified = await facilitatorVerify({
345
+ apiBase: runtime.apiBase,
346
+ resource,
347
+ paymentSignature: args.paymentSignature,
348
+ fetchFn: runtime.fetchFn,
349
+ });
350
+ return ok(verified);
351
+ }
352
+ if (args.channelId &&
353
+ args.claimSignature &&
354
+ args.cumulativeDrops != null) {
355
+ const verified = await facilitatorVerify({
356
+ apiBase: runtime.apiBase,
357
+ resource,
358
+ channelId: args.channelId,
359
+ claimSignature: args.claimSignature,
360
+ cumulativeDrops: args.cumulativeDrops,
361
+ fetchFn: runtime.fetchFn,
362
+ });
363
+ return ok(verified);
364
+ }
365
+ return fail("provide paymentSignature (exact) or channelId+claimSignature+cumulativeDrops (PayChan)", "missing_payment_signature");
366
+ }
367
+ catch (err) {
368
+ return catchFail(err);
369
+ }
370
+ }
371
+ export async function toolFacilitatorSettle(args, runtime = createRuntime()) {
372
+ try {
373
+ if (!args.paymentSignature?.trim()) {
374
+ return fail("paymentSignature is required");
375
+ }
376
+ // Detect accidental PayChan JSON blob
377
+ const trimmed = args.paymentSignature.trim();
378
+ if (trimmed.startsWith("{")) {
379
+ try {
380
+ const parsed = JSON.parse(trimmed);
381
+ if (typeof parsed.channelId === "string") {
382
+ return fail("PayChan routes use facilitator_authorize, not settle", "use_authorize");
383
+ }
384
+ }
385
+ catch {
386
+ /* not JSON */
387
+ }
388
+ }
389
+ const settled = await facilitatorSettle({
390
+ apiBase: runtime.apiBase,
391
+ resource: args.resource,
392
+ paymentSignature: args.paymentSignature,
393
+ fetchFn: runtime.fetchFn,
394
+ handler: () => null,
395
+ });
396
+ return ok({
397
+ txHash: settled.txHash,
398
+ callId: settled.callId,
399
+ network: settled.network,
400
+ });
401
+ }
402
+ catch (err) {
403
+ return catchFail(err);
404
+ }
405
+ }
406
+ export async function toolFacilitatorAuthorize(args, runtime = createRuntime()) {
407
+ try {
408
+ const channelId = args.channelId.trim().toUpperCase();
409
+ if (!/^[0-9A-F]{64}$/.test(channelId)) {
410
+ return fail("channelId must be 64 hex characters", "invalid_channel_id");
411
+ }
412
+ if (!args.claimSignature?.trim()) {
413
+ return fail("claimSignature is required");
414
+ }
415
+ const authorized = await facilitatorAuthorize({
416
+ apiBase: runtime.apiBase,
417
+ resource: args.resource,
418
+ channelId,
419
+ claimSignature: args.claimSignature,
420
+ cumulativeDrops: args.cumulativeDrops,
421
+ fetchFn: runtime.fetchFn,
422
+ });
423
+ return ok(authorized);
424
+ }
425
+ catch (err) {
426
+ return catchFail(err);
427
+ }
428
+ }
429
+ /**
430
+ * Prove: public joined receipt for one callId (settle + delivery when recorded).
431
+ * Facilitator never invents a Sluzen delivery row.
432
+ */
433
+ export async function toolGetCallReceipt(args, runtime = createRuntime()) {
434
+ try {
435
+ const callId = args.callId?.trim();
436
+ if (!callId) {
437
+ return fail("callId is required", "invalid_call_id");
438
+ }
439
+ const receipt = await getCallReceipt({
440
+ baseUrl: runtime.apiBase,
441
+ callId,
442
+ fetchFn: runtime.fetchFn,
443
+ });
444
+ return ok(receipt);
445
+ }
446
+ catch (err) {
447
+ return catchFail(err);
448
+ }
449
+ }
450
+ /**
451
+ * Local Prove bridge: bind orchestrator taskId/goalId to callId in process memory.
452
+ * Does not call Sluzen; does not store paid bodies.
453
+ */
454
+ export function toolBindTaskCall(args, runtime = createRuntime()) {
455
+ try {
456
+ const binding = runtime.taskBindings.bind({
457
+ callId: args.callId,
458
+ taskId: args.taskId,
459
+ goalId: args.goalId,
460
+ resultRef: args.resultRef,
461
+ });
462
+ return ok(binding);
463
+ }
464
+ catch (err) {
465
+ return catchFail(err);
466
+ }
467
+ }
468
+ /** Lookup a local task↔call binding (process memory only). */
469
+ export function toolLookupTaskCall(args, runtime = createRuntime()) {
470
+ try {
471
+ const callId = args.callId?.trim();
472
+ const taskId = args.taskId?.trim();
473
+ const goalId = args.goalId?.trim();
474
+ const binding = callId
475
+ ? runtime.taskBindings.getByCallId(callId)
476
+ : taskId
477
+ ? runtime.taskBindings.getByTaskId(taskId)
478
+ : goalId
479
+ ? runtime.taskBindings.getByGoalId(goalId)
480
+ : undefined;
481
+ if (!binding) {
482
+ return fail("binding not found", "not_found");
483
+ }
484
+ return ok(binding);
485
+ }
486
+ catch (err) {
487
+ return catchFail(err);
488
+ }
489
+ }
490
+ /** Assert accept is PayChan with usable channel params (fail-closed helper). */
491
+ export function assertPayChanParams(challenge) {
492
+ try {
493
+ const accept = selectAccept(challenge);
494
+ if (!isPayChanAccept(accept)) {
495
+ return fail("challenge is not PayChan", "scheme_mismatch");
496
+ }
497
+ const channelId = accept.extra.channelId?.trim().toUpperCase();
498
+ if (!channelId || !/^[0-9A-F]{64}$/.test(channelId)) {
499
+ return fail("PayChan challenge missing valid channelId (open channel first on agent host)", "invalid_channel_id");
500
+ }
501
+ return ok({
502
+ channelId,
503
+ minCumulativeDrops: accept.extra.minCumulativeDrops,
504
+ });
505
+ }
506
+ catch (err) {
507
+ return catchFail(err);
508
+ }
509
+ }