@lucid-agents/pi-xgate 0.1.5 → 0.1.6

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,94 @@
1
+ import {
2
+ XgateError,
3
+ type Invocation,
4
+ type PurchaseResult,
5
+ } from "@lucid-agents/xgate-client/types";
6
+ /** Response metadata safe to show Pi's `onResponse` hook: never payment or status credentials. */
7
+ export interface XgateResponseHead {
8
+ status: number;
9
+ headers: Record<string, string>;
10
+ }
11
+ /**
12
+ * Rail-neutral payment terms and status for display. Pi shows them; it never selects or
13
+ * configures a rail. Amounts are unsigned integer strings in the asset's smallest unit, and
14
+ * amounts of different assets are never combined.
15
+ */
16
+ export interface XgatePayment {
17
+ /** CAIP-2 network, e.g. `eip155:8453`. */
18
+ network: string;
19
+ /** Payment scheme as the payee advertised it, e.g. `batch-settlement`, `exact`, `session`. */
20
+ scheme: string;
21
+ asset: string;
22
+ payTo?: string;
23
+ /** Rail shape: `per-request` or `session` today; other values may appear. */
24
+ rail?: string;
25
+ /** Most this invocation could be charged. */
26
+ authorizedAtomic: string;
27
+ /** Verified charge, once known. */
28
+ chargedAtomic?: string;
29
+ /** `settled`, `pending`, `amount_unknown`, `released`, ...; anything but `settled` is unverified. */
30
+ status: string;
31
+ /** Settlement transaction or payee commitment, when one exists. */
32
+ reference?: string;
33
+ /** Session-rail balance after this payment, when the host reports one. */
34
+ channel?: XgateChannelBalance;
35
+ }
36
+ export interface XgateChannelBalance {
37
+ depositingAtomic: string;
38
+ lockedAtomic: string;
39
+ chargedAtomic: string;
40
+ pendingAtomic: string;
41
+ refundableAtomic: string;
42
+ }
43
+ export interface XgatePurchaseResult extends PurchaseResult {
44
+ payment?: XgatePayment;
45
+ }
46
+ /**
47
+ * A purchase that ended without a verified result. `pending` is true when money may have moved
48
+ * and the outcome is unknown: the invocation must be reconciled, never retried.
49
+ */
50
+ export class XgatePurchaseError extends XgateError {
51
+ readonly invocationId: string;
52
+ readonly pending: boolean;
53
+ readonly payment?: XgatePayment;
54
+ constructor(
55
+ code: string,
56
+ invocationId: string,
57
+ pending: boolean,
58
+ payment?: XgatePayment,
59
+ ) {
60
+ super(code);
61
+ this.name = "XgatePurchaseError";
62
+ this.message = pending
63
+ ? `Xgate: ${code}. The payment outcome is unknown; do not retry. Reconcile it before another purchase.`
64
+ : `Xgate: ${code}. Inspect the payment status before another purchase.`;
65
+ this.invocationId = invocationId;
66
+ this.pending = pending;
67
+ if (payment) this.payment = payment;
68
+ }
69
+ }
70
+ /**
71
+ * Moves one frozen Pi request to xgate and returns the verified result. The provider owns
72
+ * catalog qualification, codecs and the invocation ID; the transport owns payment.
73
+ *
74
+ * `purchase` sends at most one paid submission per `Invocation.id`, never retries a paid
75
+ * request, and never pays again for an ID it has seen: a repeated ID rejects. It resolves
76
+ * only with a verified result and rejects on every other outcome, preferably with an
77
+ * `XgatePurchaseError` stating whether the payment is pending. `BrokerClient` is the
78
+ * standalone implementation.
79
+ */
80
+ export interface XgateTransport {
81
+ /** Current router catalog rows. Called before every paid invocation. */
82
+ models(signal?: AbortSignal): Promise<unknown[]>;
83
+ purchase(
84
+ invocation: Invocation,
85
+ signal?: AbortSignal,
86
+ onResponse?: (response: XgateResponseHead) => Promise<void>,
87
+ ): Promise<XgatePurchaseResult>;
88
+ /** Optional `/xgate status` and `/xgate budget` view. */
89
+ status?(signal?: AbortSignal): Promise<unknown>;
90
+ /** Optional `/xgate receipts` view. */
91
+ receipts?(signal?: AbortSignal): Promise<unknown>;
92
+ /** Optional `/xgate reconcile <invocation ID>`. Never resends the request. */
93
+ reconcile?(invocationId: string, signal?: AbortSignal): Promise<unknown>;
94
+ }