madeonsol-x402 4.2.0 → 4.4.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.
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Framework-agnostic terminal snapshot/stream bridge (#534).
3
+ *
4
+ * A shared SDK socket supplies push frames. Snapshot reads happen once after
5
+ * the named subscription is ACKED, then only on coalesced, named invalidations.
6
+ * This deliberately NEVER patches REST module data from unrelated clocks.
7
+ *
8
+ * Instantiate once per selected token, reusing one existing stream connection.
9
+ * The controller never closes that shared stream on dispose().
10
+ */
11
+ import { type TerminalWatchPlan, type TerminalModule, type TerminalChain, type TerminalTier } from "./terminal-policy.js";
12
+ export interface TerminalStreamFrame {
13
+ channel?: string;
14
+ event?: string;
15
+ sub_id?: string;
16
+ id?: string;
17
+ data?: unknown;
18
+ ts?: number;
19
+ replayed?: boolean;
20
+ snapshot?: boolean;
21
+ }
22
+ /** Adaptable to the existing MadeOnSolStream / RobinhoodChainStream overloads. */
23
+ export interface TerminalStreamPort {
24
+ subscribe(opts: {
25
+ subId: string;
26
+ channels: string[];
27
+ filters: Record<string, unknown>;
28
+ }): unknown;
29
+ unsubscribe(subId: string): unknown;
30
+ on(event: string, listener: (...args: unknown[]) => unknown): unknown;
31
+ off(event: string, listener: (...args: unknown[]) => unknown): unknown;
32
+ }
33
+ export interface TerminalModuleResponse {
34
+ status: "ready" | "partial_history" | "unverified" | "unavailable" | "timeout";
35
+ reason: string | null;
36
+ as_of: string | null;
37
+ data?: Record<string, unknown>;
38
+ }
39
+ export interface TerminalSnapshotResponse {
40
+ chain: TerminalChain;
41
+ address: string;
42
+ generated_at: string;
43
+ modules: Partial<Record<TerminalModule, TerminalModuleResponse>>;
44
+ }
45
+ export type TerminalPhase = "idle" | "awaiting_subscribe" | "bootstrapping" | "live" | "degraded" | "stopped";
46
+ /**
47
+ * Stream delivery truth only: "replay_complete" establishes what the SDK
48
+ * replayed for THIS subscription, not full market/trade/archive coverage.
49
+ * "initial" means no reconnect has needed proof during this widget's life.
50
+ */
51
+ export type TerminalContinuityStatus = "initial" | "snapshot_only" | "replay_pending" | "replay_complete" | "replay_unverified" | "historical_gap" | "externally_reconciled";
52
+ export interface TerminalContinuity {
53
+ status: TerminalContinuityStatus;
54
+ /** Client monotonic/now() time at the latest socket close, if any. */
55
+ last_disconnected_at_ms: number | null;
56
+ /** Client receipt time for a completed replay of THIS named subscription. */
57
+ last_replay_at_ms: number | null;
58
+ /** An SDK replay ending for another widget is not proof for this one. */
59
+ this_subscription_replay_complete: boolean;
60
+ }
61
+ export interface TerminalView {
62
+ phase: TerminalPhase;
63
+ address: string;
64
+ chain: TerminalChain;
65
+ /** Untouched REST modules; use the latest live frame as a separate overlay. */
66
+ snapshot: TerminalSnapshotResponse | null;
67
+ live: Partial<Record<TerminalModule, TerminalStreamFrame>>;
68
+ /** A known event or gap may have made a cached module stale. */
69
+ stale: TerminalModule[];
70
+ /** A module without an adequate existing WS source is snapshot-only. */
71
+ no_push: TerminalModule[];
72
+ /** A stream interruption or refusal must be visible to the terminal. */
73
+ incomplete: boolean;
74
+ last_error: string | null;
75
+ /** Reconnect/replay diagnostics; never a historical coverage certification. */
76
+ continuity: TerminalContinuity;
77
+ }
78
+ export interface TerminalViewOptions {
79
+ chain: TerminalChain;
80
+ address: string;
81
+ tier: TerminalTier;
82
+ include: readonly TerminalModule[];
83
+ /** Full RHC DEX firehose (all tokens) is high-bandwidth; opt in explicitly. */
84
+ includeRhcFirehose?: boolean;
85
+ /** KOL channels are unscoped broadcasts; opt in only with bandwidth budget. */
86
+ includeKolBroadcast?: boolean;
87
+ subId: string;
88
+ stream: TerminalStreamPort;
89
+ /** The existing keyed REST .tokenIntelligence(address, {include}) call. */
90
+ read: (include: readonly TerminalModule[]) => Promise<TerminalSnapshotResponse>;
91
+ onChange?: (view: TerminalView) => void;
92
+ onLive?: (frame: TerminalStreamFrame, module: TerminalModule) => void;
93
+ /** Min seconds per module before another HTTP refresh (default 15s). */
94
+ minModuleRefreshMs?: number;
95
+ /** Debounce and coalesce changed modules (default 750ms). */
96
+ debounceMs?: number;
97
+ now?: () => number;
98
+ }
99
+ /** Bounded by nine modules and one frame per live overlay; never a trade queue. */
100
+ export declare class TerminalTokenView {
101
+ readonly plan: TerminalWatchPlan;
102
+ private readonly opts;
103
+ private readonly listeners;
104
+ private readonly dirty;
105
+ private readonly stale;
106
+ private readonly seen;
107
+ private readonly live;
108
+ private readonly gen;
109
+ private readonly lastRead;
110
+ private readonly now;
111
+ private timer;
112
+ private timerAt;
113
+ private busy;
114
+ /** If the socket ACK arrives while a prior REST read is still unresolved,
115
+ * the latest post-ACK snapshot must run after that read settles. */
116
+ private pendingBootstrap;
117
+ private subscribed;
118
+ private epoch;
119
+ private phase;
120
+ private incomplete;
121
+ /** An observed historical gap survives a new REST snapshot and reconnect. */
122
+ private gapUnresolved;
123
+ /** A named subscription ACK never proves that its resume finished. */
124
+ private replayPending;
125
+ private replayUnverified;
126
+ private replayComplete;
127
+ private continuityStatus;
128
+ private lastDisconnectedAt;
129
+ private lastReplayAt;
130
+ private lastError;
131
+ private snapshot;
132
+ /**
133
+ * Modules not yet successfully re-read since the latest ACK/disconnect. A
134
+ * replay_end or a targeted refresh of ONE module must not report "live"
135
+ * while another module still shows its pre-disconnect REST value.
136
+ */
137
+ private readonly baselineMissing;
138
+ constructor(options: TerminalViewOptions);
139
+ getView(): TerminalView;
140
+ /** Registers handlers BEFORE opening the stream; first REST read waits for ACK. */
141
+ start(): void;
142
+ /** Consumer-initiated refresh, useful after a known unrecoverable gap. */
143
+ refresh(): void;
144
+ /**
145
+ * The caller must independently reconcile an unrecoverable event interval
146
+ * BEFORE clearing the visible gap. A fresh snapshot is not that evidence.
147
+ */
148
+ acknowledgeHistoricalRecovery(): void;
149
+ dispose(): void;
150
+ private isStopped;
151
+ private requireBaseline;
152
+ /** Phase after a continuity change; never "live" on a partial baseline. */
153
+ private settledPhase;
154
+ private clearLive;
155
+ private emit;
156
+ private listen;
157
+ private onSubscribed;
158
+ /**
159
+ * A successful SDK aggregate replay is relevant only when its per-subscribe
160
+ * replay_end for OUR named subscription says complete=true. REST freshness
161
+ * alone cannot certify recovered KOL/trade/risk events.
162
+ */
163
+ private onReplay;
164
+ private onWarning;
165
+ private onGap;
166
+ private onFrame;
167
+ private markStale;
168
+ private clearTimer;
169
+ private schedule;
170
+ private flush;
171
+ private fetchModules;
172
+ }
173
+ /** A named subscription prevents overwriting an integrator's other filters. */
174
+ export declare function createTerminalTokenView(options: TerminalViewOptions): TerminalTokenView;
175
+ //# sourceMappingURL=terminal-watch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"terminal-watch.d.ts","sourceRoot":"","sources":["../src/terminal-watch.ts"],"names":[],"mappings":"AACA;;;;;;;;;GASG;AACH,OAAO,EAEL,KAAK,iBAAiB,EAAE,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,KAAK,YAAY,EACnF,MAAM,sBAAsB,CAAC;AAE9B,MAAM,WAAW,mBAAmB;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,kFAAkF;AAClF,MAAM,WAAW,kBAAkB;IACjC,SAAS,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,EAAE,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,GAAG,OAAO,CAAC;IAClG,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACpC,EAAE,CAAC,KAAK,EAA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