@ergoblockchain/sage-widget 0.1.0 → 0.3.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/README.md CHANGED
@@ -3,10 +3,23 @@
3
3
  [![npm](https://img.shields.io/npm/v/@ergoblockchain/sage-widget.svg)](https://www.npmjs.com/package/@ergoblockchain/sage-widget)
4
4
  [![license](https://img.shields.io/npm/l/@ergoblockchain/sage-widget.svg)](./LICENSE)
5
5
 
6
- Embeddable live-activity feed for **Sage** — the agent-economy concierge on [ergoblockchain.org](https://www.ergoblockchain.org). Drop a typed React component into your app, or call a vanilla DOM mount function from any framework. Every settled paid query Sage takes appears in the feed within a minute, with a clickable link to the on-chain receipt.
6
+ Embeddable widgets for **Sage** — the agent-economy concierge on [ergoblockchain.org](https://www.ergoblockchain.org). Use the read-only activity feed, or embed the paid Sage chat flow with quote, manual Note verification, streaming answer, receipt JSON, and public receipt link.
7
7
 
8
8
  > _Why this matters:_ Sage settles real paid AI queries on Ergo testnet. The feed makes the "agent-economy" thesis visibly provable wherever you embed it — not a marketing claim, a list of public on-chain receipts that update live.
9
9
 
10
+ ## What v0.3 ships
11
+
12
+ - `<SagePaymentWidget />` React component.
13
+ - `mountSagePaymentWidget(target, opts)` vanilla DOM mount.
14
+ - Typed clients for quote, verify, chat stream, receipt bundle, and activity feed.
15
+ - Tenant metadata and host-provided payment instructions.
16
+ - Portable `SagePaymentIntent` JSON for host-owned wallet flows.
17
+ - Optional `walletLauncher(intent)` callback so your app can open a reviewed wallet flow without letting the widget sign funds.
18
+ - Payment lifecycle callbacks: quote, receipt, receipt bundle, tier, phase, status.
19
+ - Public receipt links plus machine-readable `/api/sage/receipt/<id>` links.
20
+
21
+ The canonical Sage host is **testnet live proof**. It can produce `full_receipt_bundle` receipts with Agreement JSON, Verification Receipt JSON, and Settlement Receipt JSON. Mainnet readiness remains audit-gated.
22
+
10
23
  ## Install
11
24
 
12
25
  ```bash
@@ -21,7 +34,7 @@ yarn add @ergoblockchain/sage-widget
21
34
 
22
35
  ## Use it
23
36
 
24
- ### React
37
+ ### React activity feed
25
38
 
26
39
  ```tsx
27
40
  import { SageActivityFeed } from "@ergoblockchain/sage-widget/react"
@@ -33,7 +46,38 @@ export function Footer() {
33
46
 
34
47
  The component renders into your DOM (no iframe), styles itself with inline styles to avoid host-CSS conflicts, and polls `/api/sage/activity` on the host you point it at.
35
48
 
36
- ### Vanilla / non-React
49
+ ### React paid chat
50
+
51
+ ```tsx
52
+ import { SagePaymentWidget } from "@ergoblockchain/sage-widget/react"
53
+
54
+ export function PaidSageBox() {
55
+ return (
56
+ <SagePaymentWidget
57
+ tenant={{ id: "my-site", label: "My Ergo app" }}
58
+ paymentInstructions={{
59
+ helperText: "Issue the quoted Accord Note from your testnet wallet, then paste the Note box id.",
60
+ walletUrl: "https://www.ergoblockchain.org/build/agent-payments",
61
+ walletLauncherLabel: "Open my testnet wallet",
62
+ }}
63
+ onQuote={(quote) => console.log("Sage quote", quote)}
64
+ onPaymentIntent={(intent) => console.log("Wallet intent", intent)}
65
+ onReceipt={(receipt) => console.log("Sage receipt", receipt.receiptUrl)}
66
+ onReceiptBundle={(bundle) => console.log("Receipt completeness", bundle.completeness)}
67
+ walletLauncher={async (intent) => {
68
+ // Your app owns wallet policy, ErgoPay/Fleet integration and signing.
69
+ // Return a Note box id when the wallet creates the testnet Note.
70
+ console.log("Create a Note for", intent.amountErg, intent.receiverAddress)
71
+ return { ok: true, noteBoxId: "" }
72
+ }}
73
+ />
74
+ )
75
+ }
76
+ ```
77
+
78
+ The paid widget calls `/api/sage/quote`, shows the Accord Note payment fields, accepts a `noteBoxId`, calls `/api/sage/verify-payment`, streams `/api/sage/chat`, then surfaces the receipt link from `/api/sage/receipt/<id>`.
79
+
80
+ ### Vanilla / non-React activity feed
37
81
 
38
82
  ```ts
39
83
  import { mountSageFeed } from "@ergoblockchain/sage-widget/vanilla"
@@ -59,6 +103,23 @@ Or use the canonical hosted CDN drop-in (no install needed):
59
103
 
60
104
  That last form is an iframe variant served straight from ergoblockchain.org — total style isolation, zero install. Use the npm package when you want the markup inline (themable, accessibility-friendly, tree-shakeable).
61
105
 
106
+ ### Vanilla / non-React paid chat
107
+
108
+ ```ts
109
+ import { mountSagePaymentWidget } from "@ergoblockchain/sage-widget/vanilla"
110
+
111
+ const handle = mountSagePaymentWidget(document.getElementById("sage-chat")!, {
112
+ tenant: { id: "docs-footer", label: "Docs footer" },
113
+ onPaymentIntent: (intent) => console.log("[sage] payment intent", intent),
114
+ })
115
+
116
+ // You can also start a question programmatically:
117
+ await handle.send("/research explain Ergo Notes")
118
+
119
+ // Inspect embed state:
120
+ console.log(handle.status().receipt?.receiptUrl)
121
+ ```
122
+
62
123
  ## Just the types
63
124
 
64
125
  If you want to render your own UI over Sage's API and skip the bundled components:
@@ -66,6 +127,9 @@ If you want to render your own UI over Sage's API and skip the bundled component
66
127
  ```ts
67
128
  import {
68
129
  fetchSageActivity,
130
+ fetchSageQuote,
131
+ streamSageChat,
132
+ verifySagePayment,
69
133
  type SageActivityEvent,
70
134
  type SageActivityResponse,
71
135
  } from "@ergoblockchain/sage-widget"
@@ -74,6 +138,39 @@ const data: SageActivityResponse = await fetchSageActivity({ limit: 10 })
74
138
  const settlements = data.events.filter((e) => e.type === "settlement")
75
139
  ```
76
140
 
141
+ Useful helpers:
142
+
143
+ ```ts
144
+ import { fetchSageReceipt, isFullSageReceiptBundle } from "@ergoblockchain/sage-widget"
145
+
146
+ const receipt = await fetchSageReceipt("f8752d10a2ece92fbc88065c3b92b94da621ec65943098f43c9e084deb763d81")
147
+
148
+ if (isFullSageReceiptBundle(receipt)) {
149
+ console.log("Agreement JSON is present", receipt.accord?.agreement_json)
150
+ }
151
+ ```
152
+
153
+ ### Payment intent bridge
154
+
155
+ v0.3 adds a small but important contract between the widget and your wallet layer. The widget still does **not** sign transactions. Instead it emits a portable intent:
156
+
157
+ ```ts
158
+ import {
159
+ createSagePaymentIntent,
160
+ serializeSagePaymentIntent,
161
+ } from "@ergoblockchain/sage-widget"
162
+
163
+ const intent = createSagePaymentIntent({
164
+ question: "/deep explain Ergo Notes",
165
+ quote,
166
+ tenant: { id: "my-wallet", label: "My Wallet" },
167
+ })
168
+
169
+ console.log(serializeSagePaymentIntent(intent))
170
+ ```
171
+
172
+ The intent includes network, receiver, amount, reserve box, task hash, expiry, verification endpoint, and receipt endpoint template. A wallet integration can turn it into an Ergo testnet Note, then hand the Note box id back to the widget for verification.
173
+
77
174
  ## Render-prop / bring-your-own-design
78
175
 
79
176
  ```tsx
@@ -97,6 +194,41 @@ const settlements = data.events.filter((e) => e.type === "settlement")
97
194
  | `style` | `CSSProperties` | — | Inline style merged onto the root container (React only). |
98
195
  | `children` | render-prop | — | Pass a function for full UI control; the component will skip the default markup. |
99
196
 
197
+ `SagePaymentWidget` also accepts:
198
+
199
+ | Prop | Type | Notes |
200
+ | ------------------- | ----------------------------------------- | ----------------------------------------------------------- |
201
+ | `tenant` | `{ id?: string; label?: string; headers?: Record<string,string> }` | Stable embed metadata and optional request headers. |
202
+ | `initialMessages` | `SageChatMessage[]` | Preloaded chat context. |
203
+ | `title` | `string` | Widget heading. |
204
+ | `placeholder` | `string` | Input placeholder. |
205
+ | `paymentInstructions` | `{ helperText?: string; walletUrl?: string; noteBoxLabel?: string }` | Host-specific payment copy and guide link. |
206
+ | `onMessage` | `(message, messages) => void` | Fired when the widget appends a chat message. |
207
+ | `onQuote` | `(quote) => void` | Fired after Sage returns a premium quote. |
208
+ | `onReceipt` | `(receipt) => void` | Fired after payment verifies and a receipt URL exists. |
209
+ | `onReceiptBundle` | `(bundle) => void` | Fired after the widget fetches `/api/sage/receipt/<id>`. |
210
+ | `onTier` | `("free" \| "premium") => void` | Fired when the stream reports model tier. |
211
+ | `onPhase` | `(phase) => void` | Fired on widget phase changes: idle, quoting, payment_required, verifying, streaming, error. |
212
+ | `onPaymentIntent` | `(intent) => void` | Fired when a premium quote becomes a structured wallet intent. |
213
+ | `walletLauncher` | `(intent) => Promise<{ ok; noteBoxId? }>` | Optional host-owned wallet flow. The widget never signs itself. |
214
+ | `showPaymentIntent` | `boolean` | Shows or hides the default payment-intent JSON panel. Default `true`. |
215
+ | `testnetWarning` | `string \| false` | Default testnet safety copy. Set `false` to hide. |
216
+ | `onStatus` | `(status) => void` | Fired with a compact snapshot of phase, tier, quote, payment intent, receipt, receipt bundle, and error. |
217
+
218
+ `onStatus` and the vanilla `handle.status()` also include `paymentIntent`, `messages`, and `activeQuestion`, so hosts can mirror widget state into their own telemetry or UI.
219
+
220
+ ## Payment model
221
+
222
+ v0.3 deliberately does **not** sign wallet transactions inside the widget. The widget shows the quote fields, emits a portable payment intent, accepts a Note box id, verifies it through Sage, then streams the answer and exposes the receipt. Hosts can pair it with their own wallet flow, Accord tooling, ErgoPay/Fleet integration, or a manual testnet Note issuer.
223
+
224
+ For the canonical site, a successful paid flow looks like:
225
+
226
+ ```text
227
+ question -> quote -> Ergo testnet Note -> verify-payment -> receipt bundle -> Sage answer
228
+ ```
229
+
230
+ The public receipt API is the source of truth. Articles, dashboards, and registry entries should link to it instead of duplicating receipt fields.
231
+
100
232
  ## Event shape
101
233
 
102
234
  Each event in `response.events` is a typed object:
@@ -117,17 +249,28 @@ For settlements, use `paymentNanoErg` (the value of the consumed Note) for "amou
117
249
 
118
250
  ## Related
119
251
 
120
- - **Live demo**: [ergoblockchain.org/agent-economy#sage-activity](https://www.ergoblockchain.org/agent-economy#sage-activity)
252
+ - **Live demo**: [ergoblockchain.org/agent-economy/sage-widget](https://www.ergoblockchain.org/agent-economy/sage-widget)
121
253
  - **API directly**: [`/api/sage/activity`](https://www.ergoblockchain.org/api/sage/activity) — JSON, no auth, cached 30s
122
254
  - **Receipt format**: [`/r/sage/<settlement_tx_id>`](https://www.ergoblockchain.org/r/sage/f697e4841dd9a0c689d0b83a311130b85a0cfbab123230a6c40284b44c4cafef)
123
255
  - **Accord Protocol**: [github.com/accord-protocol/accord-protocol](https://github.com/accord-protocol/accord-protocol)
124
256
  - **Sage in the registry**: [accord-protocol/registry/providers/sage.json](https://github.com/accord-protocol/accord-protocol/blob/main/registry/providers/sage.json)
125
257
 
258
+ ## Release checks
259
+
260
+ Before publishing:
261
+
262
+ ```bash
263
+ npm run typecheck
264
+ npm run smoke
265
+ npm pack --dry-run
266
+ ```
267
+
126
268
  ## Roadmap
127
269
 
128
- - **v0.1.x** _(current)_ — read-only activity feed (React + vanilla + types).
129
- - **v0.2** — `<SagePaymentWidget />` full chat with 402 + payment + Sonnet answer.
130
- - **v0.3** generic `<AccordActivityFeed providerId="..." />` that works for any provider in the registry, not just Sage.
270
+ - **v0.1.x** — read-only activity feed (React + vanilla + types).
271
+ - **v0.2** — `<SagePaymentWidget />` paid chat with quote, manual Note verify, streaming answer, receipt link, tenant config.
272
+ - **v0.3** _(current source)_ payment-intent bridge, wallet launcher callback, stronger host telemetry, clearer testnet safety copy.
273
+ - **v0.4** — generic `<AccordActivityFeed providerId="..." />` that works for any provider in the registry, not just Sage.
131
274
 
132
275
  ## License
133
276
 
package/dist/index.cjs CHANGED
@@ -9,13 +9,154 @@ var DEFAULT_REFRESH_MS = 6e4;
9
9
  async function fetchSageActivity(opts = {}) {
10
10
  const base = opts.apiBase ?? DEFAULT_API_BASE;
11
11
  const limit = Math.min(Math.max(opts.limit ?? DEFAULT_LIMIT, 1), 25);
12
- const url = `${base}/api/sage/activity?limit=${limit}`;
12
+ const url = `${trimSlash(base)}/api/sage/activity?limit=${limit}`;
13
13
  const res = await fetch(url, { signal: opts.signal });
14
14
  if (!res.ok) {
15
15
  throw new Error(`sage activity ${res.status}`);
16
16
  }
17
17
  return await res.json();
18
18
  }
19
+ async function fetchSageQuote(opts) {
20
+ const res = await fetch(`${apiBase(opts)}/api/sage/quote`, {
21
+ method: "POST",
22
+ headers: jsonHeaders(opts),
23
+ body: JSON.stringify({
24
+ question: opts.question,
25
+ history: opts.history ?? []
26
+ }),
27
+ signal: opts.signal
28
+ });
29
+ const body = await parseJson(res);
30
+ if (!res.ok) throw new Error(readError(body, `sage quote ${res.status}`));
31
+ return body;
32
+ }
33
+ async function verifySagePayment(opts) {
34
+ const res = await fetch(`${apiBase(opts)}/api/sage/verify-payment`, {
35
+ method: "POST",
36
+ headers: jsonHeaders(opts),
37
+ body: JSON.stringify({
38
+ quote: opts.quote,
39
+ question: opts.question,
40
+ noteBoxId: opts.noteBoxId
41
+ }),
42
+ signal: opts.signal
43
+ });
44
+ const body = await parseJson(res);
45
+ if (!res.ok) throw new Error(readError(body, `sage verify-payment ${res.status}`));
46
+ return body;
47
+ }
48
+ async function fetchSageReceipt(id, opts = {}) {
49
+ const res = await fetch(`${apiBase(opts)}/api/sage/receipt/${encodeURIComponent(id)}`, {
50
+ headers: requestHeaders(opts),
51
+ signal: opts.signal
52
+ });
53
+ const body = await parseJson(res);
54
+ if (!res.ok) throw new Error(readError(body, `sage receipt ${res.status}`));
55
+ return body;
56
+ }
57
+ function isFullSageReceiptBundle(value) {
58
+ return value?.ok === true && value.completeness === "full_receipt_bundle";
59
+ }
60
+ function createSagePaymentIntent(opts) {
61
+ const base = trimSlash(opts.apiBase ?? DEFAULT_API_BASE);
62
+ return {
63
+ type: "sage.payment_intent.v1",
64
+ network: opts.network ?? "ergo-testnet",
65
+ createdAt: opts.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
66
+ question: opts.question,
67
+ ...opts.tenant?.id || opts.tenant?.label ? { tenant: { id: opts.tenant.id, label: opts.tenant.label } } : {},
68
+ quote: opts.quote,
69
+ amountErg: opts.quote.price,
70
+ receiverAddress: opts.quote.receiverAddress,
71
+ reserveBoxId: opts.quote.reserveBoxId,
72
+ taskHash: opts.quote.taskHash,
73
+ expiresAt: opts.quote.expiresAt,
74
+ deadline: opts.quote.deadline,
75
+ verifyEndpoint: `${base}/api/sage/verify-payment`,
76
+ receiptEndpointTemplate: `${base}/api/sage/receipt/{receiptId}`
77
+ };
78
+ }
79
+ function serializeSagePaymentIntent(intent) {
80
+ return JSON.stringify(intent, null, 2);
81
+ }
82
+ async function streamSageChat(opts) {
83
+ const res = await fetch(`${apiBase(opts)}/api/sage/chat`, {
84
+ method: "POST",
85
+ headers: jsonHeaders(opts),
86
+ body: JSON.stringify({
87
+ messages: opts.messages,
88
+ ...opts.paymentToken ? { paymentToken: opts.paymentToken } : {}
89
+ }),
90
+ signal: opts.signal
91
+ });
92
+ if (res.status === 402) {
93
+ const body = await parseJson(res);
94
+ return {
95
+ ok: false,
96
+ status: res.status,
97
+ text: "",
98
+ paymentRequired: body
99
+ };
100
+ }
101
+ if (!res.ok) {
102
+ const body = await parseJson(res);
103
+ return {
104
+ ok: false,
105
+ status: res.status,
106
+ text: "",
107
+ error: readError(body, `sage chat ${res.status}`)
108
+ };
109
+ }
110
+ if (!res.body) {
111
+ return { ok: false, status: res.status, text: "", error: "Sage chat stream missing body" };
112
+ }
113
+ const reader = res.body.getReader();
114
+ const decoder = new TextDecoder();
115
+ let buffer = "";
116
+ let text = "";
117
+ let tier;
118
+ while (true) {
119
+ const { value, done } = await reader.read();
120
+ if (done) break;
121
+ buffer += decoder.decode(value, { stream: true });
122
+ const parts = buffer.split("\n\n");
123
+ buffer = parts.pop() ?? "";
124
+ for (const part of parts) {
125
+ const event = parseSseEvent(part);
126
+ if (!event) continue;
127
+ if (event.type === "delta") text += event.text;
128
+ if (event.type === "tier") tier = event.tier;
129
+ opts.onEvent?.(event);
130
+ if (event.type === "error") {
131
+ return {
132
+ ok: false,
133
+ status: res.status,
134
+ text,
135
+ tier,
136
+ error: event.message
137
+ };
138
+ }
139
+ }
140
+ }
141
+ if (buffer.trim()) {
142
+ const event = parseSseEvent(buffer);
143
+ if (event) {
144
+ if (event.type === "delta") text += event.text;
145
+ if (event.type === "tier") tier = event.tier;
146
+ opts.onEvent?.(event);
147
+ if (event.type === "error") {
148
+ return {
149
+ ok: false,
150
+ status: res.status,
151
+ text,
152
+ tier,
153
+ error: event.message
154
+ };
155
+ }
156
+ }
157
+ }
158
+ return { ok: true, status: res.status, text, tier };
159
+ }
19
160
  function nanoToErg(nano) {
20
161
  if (!nano || nano <= 0) return "0";
21
162
  const erg = nano / 1e9;
@@ -32,20 +173,103 @@ function relativeTime(ms, now = Date.now()) {
32
173
  const day = Math.floor(hr / 24);
33
174
  return `${day}d ago`;
34
175
  }
35
- function receiptUrl(txId, apiBase = DEFAULT_API_BASE) {
36
- return `${apiBase}/r/sage/${txId}`;
176
+ function receiptUrl(txId, apiBase2 = DEFAULT_API_BASE) {
177
+ return `${trimSlash(apiBase2)}/r/sage/${txId}`;
37
178
  }
38
179
  function explorerUrl(txId, network = "testnet") {
39
180
  return network === "testnet" ? `https://testnet.ergoplatform.com/transactions/${txId}` : `https://explorer.ergoplatform.com/transactions/${txId}`;
40
181
  }
182
+ function apiBase(opts) {
183
+ return trimSlash(opts.apiBase ?? DEFAULT_API_BASE);
184
+ }
185
+ function trimSlash(value) {
186
+ return value.replace(/\/+$/, "");
187
+ }
188
+ function requestHeaders(opts) {
189
+ return {
190
+ ...opts.tenant?.id ? { "x-sage-tenant-id": opts.tenant.id } : {},
191
+ ...opts.tenant?.headers ?? {},
192
+ ...opts.headers ?? {}
193
+ };
194
+ }
195
+ function jsonHeaders(opts) {
196
+ return {
197
+ "content-type": "application/json",
198
+ ...requestHeaders(opts)
199
+ };
200
+ }
201
+ async function parseJson(res) {
202
+ const text = await res.text();
203
+ if (!text) return null;
204
+ try {
205
+ return JSON.parse(text);
206
+ } catch {
207
+ return { error: text };
208
+ }
209
+ }
210
+ function readError(body, fallback) {
211
+ if (body && typeof body === "object" && "error" in body) {
212
+ const error = body.error;
213
+ if (typeof error === "string") return error;
214
+ }
215
+ return fallback;
216
+ }
217
+ function parseSseEvent(raw) {
218
+ let eventName = "message";
219
+ let data = "";
220
+ for (const line of raw.split("\n")) {
221
+ if (line.startsWith("event:")) eventName = line.slice("event:".length).trim();
222
+ if (line.startsWith("data:")) data += line.slice("data:".length).trim();
223
+ }
224
+ if (!data) return null;
225
+ let parsed;
226
+ try {
227
+ parsed = JSON.parse(data);
228
+ } catch {
229
+ return null;
230
+ }
231
+ if (eventName === "tier") {
232
+ const tier = parsed.tier === "premium" ? "premium" : "free";
233
+ return {
234
+ type: "tier",
235
+ tier,
236
+ ...typeof parsed.model === "string" ? { model: parsed.model } : {}
237
+ };
238
+ }
239
+ if (eventName === "delta" && typeof parsed.text === "string") {
240
+ return { type: "delta", text: parsed.text };
241
+ }
242
+ if (eventName === "done") {
243
+ return {
244
+ type: "done",
245
+ ...typeof parsed.stopReason === "string" ? { stopReason: parsed.stopReason } : {},
246
+ ...typeof parsed.inputTokens === "number" ? { inputTokens: parsed.inputTokens } : {},
247
+ ...typeof parsed.outputTokens === "number" ? { outputTokens: parsed.outputTokens } : {}
248
+ };
249
+ }
250
+ if (eventName === "error") {
251
+ return {
252
+ type: "error",
253
+ message: typeof parsed.message === "string" ? parsed.message : "Sage stream error"
254
+ };
255
+ }
256
+ return null;
257
+ }
41
258
 
42
259
  exports.DEFAULT_API_BASE = DEFAULT_API_BASE;
43
260
  exports.DEFAULT_LIMIT = DEFAULT_LIMIT;
44
261
  exports.DEFAULT_REFRESH_MS = DEFAULT_REFRESH_MS;
262
+ exports.createSagePaymentIntent = createSagePaymentIntent;
45
263
  exports.explorerUrl = explorerUrl;
46
264
  exports.fetchSageActivity = fetchSageActivity;
265
+ exports.fetchSageQuote = fetchSageQuote;
266
+ exports.fetchSageReceipt = fetchSageReceipt;
267
+ exports.isFullSageReceiptBundle = isFullSageReceiptBundle;
47
268
  exports.nanoToErg = nanoToErg;
48
269
  exports.receiptUrl = receiptUrl;
49
270
  exports.relativeTime = relativeTime;
271
+ exports.serializeSagePaymentIntent = serializeSagePaymentIntent;
272
+ exports.streamSageChat = streamSageChat;
273
+ exports.verifySagePayment = verifySagePayment;
50
274
  //# sourceMappingURL=index.cjs.map
51
275
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts","../src/api.ts"],"names":[],"mappings":";;;AA6EO,IAAM,gBA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,EAAG,GAAG,CAAA,KAAA,CAAA;AAC3B,EAAA,MAAM,GAAA,GAAM,IAAA,CAAK,KAAA,CAAM,GAAA,GAAM,EAAE,CAAA;AAC/B,EAAA,IAAI,GAAA,GAAM,EAAA,EAAI,OAAO,CAAA,EAAG,GAAG,CAAA,KAAA,CAAA;AAC3B,EAAA,MAAM,EAAA,GAAK,IAAA,CAAK,KAAA,CAAM,GAAA,GAAM,EAAE,CAAA;AAC9B,EAAA,IAAI,EAAA,GAAK,EAAA,EAAI,OAAO,CAAA,EAAG,EAAE,CAAA,KAAA,CAAA;AACzB,EAAA,MAAM,GAAA,GAAM,IAAA,CAAK,KAAA,CAAM,EAAA,GAAK,EAAE,CAAA;AAC9B,EAAA,OAAO,GAAG,GAAG,CAAA,KAAA,CAAA;AACf;AAGO,SAAS,UAAA,CAAW,IAAA,EAAc,OAAA,GAAkB,gBAAA,EAA0B;AACnF,EAAA,OAAO,CAAA,EAAG,OAAO,CAAA,QAAA,EAAW,IAAI,CAAA,CAAA;AAClC;AAGO,SAAS,WAAA,CACd,IAAA,EACA,OAAA,GAAiC,SAAA,EACzB;AACR,EAAA,OAAO,YAAY,SAAA,GACf,CAAA,8CAAA,EAAiD,IAAI,CAAA,CAAA,GACrD,kDAAkD,IAAI,CAAA,CAAA;AAC5D","file":"index.cjs","sourcesContent":["/**\n * Public types — mirror of /api/sage/activity response shape.\n *\n * The source of truth is the API at https://www.ergoblockchain.org/api/sage/activity;\n * this file re-states the schema so consumers don't have to depend on the\n * server-side fetcher.\n */\n\nexport type SageActivityType = \"settlement\" | \"issuance\" | \"transfer\"\n\nexport interface SageActivityEvent {\n /** 64-char hex transaction id. */\n txId: string\n /** Block height at inclusion. */\n blockHeight: number\n /** Block timestamp (ms epoch). */\n timestamp: number\n /** Heuristic classification of the tx. */\n type: SageActivityType\n /** nanoERG flowing into the seller wallet from this tx (sum of outputs). */\n inflowNanoErg: number\n /**\n * For settlements: value of the redeemed Note (= what the buyer paid).\n * For other event types: undefined.\n *\n * Use this — not `inflowNanoErg` — when displaying \"amount paid for a\n * settled query\". `inflowNanoErg` includes change boxes in test setups\n * where the buyer and seller share an address.\n */\n paymentNanoErg?: number\n /** First input box that carries Note-shape registers, if any. */\n noteBoxId?: string\n}\n\nexport interface SageActivityResponse {\n ok: boolean\n network: \"testnet\" | \"mainnet\"\n /** Sage seller wallet address. */\n receiver: string\n /** Total number of txs ever touching the wallet, per the explorer. */\n total: number\n events: SageActivityEvent[]\n error?: string\n}\n\n/**\n * Configuration accepted by every entry point (React component +\n * vanilla mount fn). Defaults below are sensible for the canonical\n * ergoblockchain.org deployment.\n */\nexport interface SageWidgetOptions {\n /**\n * Base URL of the Sage host. Override if you run your own Sage\n * deployment behind a custom domain. Default: ergoblockchain.org.\n */\n apiBase?: string\n /**\n * Number of events to display (max 25). Default: 5.\n */\n limit?: number\n /**\n * Polling interval in ms. Default: 60000 (60s). Set to 0 to disable\n * polling — the widget will fetch once on mount and never refresh.\n */\n refreshMs?: number\n /**\n * Optional callback fired every time a fresh response arrives. Useful\n * for analytics or for triggering host-side animations on new\n * settlements.\n */\n onUpdate?: (response: SageActivityResponse) => void\n /**\n * Optional callback fired on fetch errors. Default: console.warn.\n */\n onError?: (error: unknown) => void\n}\n\nexport const DEFAULT_API_BASE = \"https://www.ergoblockchain.org\"\nexport const DEFAULT_LIMIT = 5\nexport const DEFAULT_REFRESH_MS = 60_000\n","/**\n * Thin client over /api/sage/activity.\n *\n * Pure fetch + shape — no rendering, no DOM. React + vanilla mounts\n * both call into here so the API contract lives in one place.\n */\n\nimport {\n DEFAULT_API_BASE,\n DEFAULT_LIMIT,\n type SageActivityResponse,\n} from \"./types\"\n\nexport interface FetchActivityOptions {\n apiBase?: string\n limit?: number\n signal?: AbortSignal\n}\n\nexport async function fetchSageActivity(\n opts: FetchActivityOptions = {},\n): Promise<SageActivityResponse> {\n const base = opts.apiBase ?? DEFAULT_API_BASE\n const limit = Math.min(Math.max(opts.limit ?? DEFAULT_LIMIT, 1), 25)\n const url = `${base}/api/sage/activity?limit=${limit}`\n const res = await fetch(url, { signal: opts.signal })\n if (!res.ok) {\n throw new Error(`sage activity ${res.status}`)\n }\n return (await res.json()) as SageActivityResponse\n}\n\n/** nanoERG → \"0.001\" (trims trailing zeros, max 9 decimals). */\nexport function nanoToErg(nano: number | undefined): string {\n if (!nano || nano <= 0) return \"0\"\n const erg = nano / 1e9\n return erg.toFixed(9).replace(/\\.?0+$/, \"\")\n}\n\n/** Cheap relative-time formatter, ASCII-only, no Intl deps. */\nexport function relativeTime(ms: number, now: number = Date.now()): string {\n const diff = Math.max(0, now - ms)\n const sec = Math.floor(diff / 1000)\n if (sec < 60) return `${sec}s ago`\n const min = Math.floor(sec / 60)\n if (min < 60) return `${min}m ago`\n const hr = Math.floor(min / 60)\n if (hr < 24) return `${hr}h ago`\n const day = Math.floor(hr / 24)\n return `${day}d ago`\n}\n\n/** Receipt URL for a settled tx, given the host base. */\nexport function receiptUrl(txId: string, apiBase: string = DEFAULT_API_BASE): string {\n return `${apiBase}/r/sage/${txId}`\n}\n\n/** Explorer URL for a tx on the given network. */\nexport function explorerUrl(\n txId: string,\n network: \"testnet\" | \"mainnet\" = \"testnet\",\n): string {\n return network === \"testnet\"\n ? `https://testnet.ergoplatform.com/transactions/${txId}`\n : `https://explorer.ergoplatform.com/transactions/${txId}`\n}\n"]}
1
+ {"version":3,"sources":["../src/types.ts","../src/api.ts"],"names":["apiBase"],"mappings":";;;AAySO,IAAM,gBAAA,GAAmB;AACzB,IAAM,aAAA,GAAgB;AACtB,IAAM,kBAAA,GAAqB;;;AC7OlC,eAAsB,iBAAA,CACpB,IAAA,GAA6B,EAAC,EACC;AAC/B,EAAA,MAAM,IAAA,GAAO,KAAK,OAAA,IAAW,gBAAA;AAC7B,EAAA,MAAM,KAAA,GAAQ,IAAA,CAAK,GAAA,CAAI,IAAA,CAAK,GAAA,CAAI,KAAK,KAAA,IAAS,aAAA,EAAe,CAAC,CAAA,EAAG,EAAE,CAAA;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MACA,GAAI,OAAO,MAAA,CAAO,KAAA,KAAU,QAAA,GAAW,EAAE,KAAA,EAAO,MAAA,CAAO,KAAA,EAAM,GAAI;AAAC,KACpE;AAAA,EACF;AACA,EAAA,IAAI,SAAA,KAAc,OAAA,IAAW,OAAO,MAAA,CAAO,SAAS,QAAA,EAAU;AAC5D,IAAA,OAAO,EAAE,IAAA,EAAM,OAAA,EAAS,IAAA,EAAM,OAAO,IAAA,EAAK;AAAA,EAC5C;AACA,EAAA,IAAI,cAAc,MAAA,EAAQ;AACxB,IAAA,OAAO;AAAA,MACL,IAAA,EAAM,MAAA;AAAA,MACN,GAAI,OAAO,MAAA,CAAO,UAAA,KAAe,QAAA,GAAW,EAAE,UAAA,EAAY,MAAA,CAAO,UAAA,EAAW,GAAI,EAAC;AAAA,MACjF,GAAI,OAAO,MAAA,CAAO,WAAA,KAAgB,QAAA,GAAW,EAAE,WAAA,EAAa,MAAA,CAAO,WAAA,EAAY,GAAI,EAAC;AAAA,MACpF,GAAI,OAAO,MAAA,CAAO,YAAA,KAAiB,QAAA,GAAW,EAAE,YAAA,EAAc,MAAA,CAAO,YAAA,EAAa,GAAI;AAAC,KACzF;AAAA,EACF;AACA,EAAA,IAAI,cAAc,OAAA,EAAS;AACzB,IAAA,OAAO;AAAA,MACL,IAAA,EAAM,OAAA;AAAA,MACN,SAAS,OAAO,MAAA,CAAO,OAAA,KAAY,QAAA,GAAW,OAAO,OAAA,GAAU;AAAA,KACjE;AAAA,EACF;AACA,EAAA,OAAO,IAAA;AACT","file":"index.cjs","sourcesContent":["/**\n * Public types for the Sage activity feed and paid chat flow.\n *\n * The source of truth is the API at https://www.ergoblockchain.org/api/sage/activity;\n * this file re-states the schema so consumers don't have to depend on the\n * server-side fetcher.\n */\n\nexport type SageActivityType = \"settlement\" | \"issuance\" | \"transfer\"\n\nexport interface SageActivityEvent {\n /** 64-char hex transaction id. */\n txId: string\n /** Block height at inclusion. */\n blockHeight: number\n /** Block timestamp (ms epoch). */\n timestamp: number\n /** Heuristic classification of the tx. */\n type: SageActivityType\n /** nanoERG flowing into the seller wallet from this tx (sum of outputs). */\n inflowNanoErg: number\n /**\n * For settlements: value of the redeemed Note (= what the buyer paid).\n * For other event types: undefined.\n *\n * Use this — not `inflowNanoErg` — when displaying \"amount paid for a\n * settled query\". `inflowNanoErg` includes change boxes in test setups\n * where the buyer and seller share an address.\n */\n paymentNanoErg?: number\n /** First input box that carries Note-shape registers, if any. */\n noteBoxId?: string\n}\n\nexport interface SageActivityResponse {\n ok: boolean\n network: \"testnet\" | \"mainnet\"\n /** Sage seller wallet address. */\n receiver: string\n /** Total number of txs ever touching the wallet, per the explorer. */\n total: number\n events: SageActivityEvent[]\n error?: string\n}\n\n/**\n * Configuration accepted by every entry point (React component +\n * vanilla mount fn). Defaults below are sensible for the canonical\n * ergoblockchain.org deployment.\n */\nexport interface SageWidgetOptions {\n /**\n * Base URL of the Sage host. Override if you run your own Sage\n * deployment behind a custom domain. Default: ergoblockchain.org.\n */\n apiBase?: string\n /**\n * Number of events to display (max 25). Default: 5.\n */\n limit?: number\n /**\n * Polling interval in ms. Default: 60000 (60s). Set to 0 to disable\n * polling — the widget will fetch once on mount and never refresh.\n */\n refreshMs?: number\n /**\n * Optional callback fired every time a fresh response arrives. Useful\n * for analytics or for triggering host-side animations on new\n * settlements.\n */\n onUpdate?: (response: SageActivityResponse) => void\n /**\n * Optional callback fired on fetch errors. Default: console.warn.\n */\n onError?: (error: unknown) => void\n}\n\nexport type SageChatRole = \"user\" | \"assistant\"\n\nexport interface SageChatMessage {\n role: SageChatRole\n content: string\n}\n\nexport interface SageTenantConfig {\n /** Stable tenant id for analytics, logs, or future multi-tenant routing. */\n id?: string\n /** Human-facing label shown in the default widgets. */\n label?: string\n /** Extra headers attached to Sage API requests. */\n headers?: Record<string, string>\n}\n\nexport type SagePaymentNetwork = \"ergo-testnet\" | \"ergo-mainnet\"\n\nexport interface SageQuote {\n quoteId: string\n taskHash: string\n price: string\n issuedAt?: string\n expiresAt: string\n receiverAddress: string\n reserveBoxId: string\n deadline: `+${number} blocks`\n}\n\nexport type SagePremiumReason =\n | \"explicit_command\"\n | \"code_request\"\n | \"long_answer\"\n | \"deep_research\"\n | \"multi_turn_followup\"\n\nexport interface SageQuoteResponse {\n premium: boolean\n reason?: SagePremiumReason\n rationale?: string\n quote?: SageQuote\n}\n\nexport interface SagePaymentIntent {\n type: \"sage.payment_intent.v1\"\n network: SagePaymentNetwork\n createdAt: string\n question: string\n tenant?: Pick<SageTenantConfig, \"id\" | \"label\">\n quote: SageQuote\n amountErg: string\n receiverAddress: string\n reserveBoxId: string\n taskHash: string\n expiresAt: string\n deadline: `+${number} blocks`\n verifyEndpoint: string\n receiptEndpointTemplate: string\n}\n\nexport interface SageWalletLaunchResult {\n ok: boolean\n /** Note box id produced by a host wallet flow. If present, the widget can prefill verification. */\n noteBoxId?: string\n /** Optional transaction id for host telemetry. Sage verifies by Note box id, not by tx id. */\n txId?: string\n error?: string\n}\n\nexport type SageWalletLauncher = (\n intent: SagePaymentIntent,\n) => Promise<SageWalletLaunchResult | void> | SageWalletLaunchResult | void\n\nexport interface SageVerifyPaymentResponse {\n ok: true\n paymentToken: string\n receiptId: string\n receiptUrl: string\n receiptApiUrl: string\n settlementTxId?: string | null\n accordSettlementId?: string\n receiptStorage?: {\n ok: boolean\n skipped?: boolean\n reason?: string\n path?: string\n aliases?: string[]\n error?: string\n }\n}\n\nexport interface SagePremiumPaymentRequired {\n error: \"premium_payment_required\"\n reason?: SagePremiumReason\n rationale?: string\n}\n\nexport type SageChatTier = \"free\" | \"premium\"\n\nexport type SageChatStreamEvent =\n | { type: \"tier\"; tier: SageChatTier; model?: string }\n | { type: \"delta\"; text: string }\n | { type: \"done\"; stopReason?: string; inputTokens?: number; outputTokens?: number }\n | { type: \"error\"; message: string }\n\nexport interface SageChatStreamResult {\n ok: boolean\n status: number\n text: string\n tier?: SageChatTier\n paymentRequired?: SagePremiumPaymentRequired\n error?: string\n}\n\nexport interface SageReceiptBundle {\n ok: true\n type: \"sage.receipt_bundle.v1\"\n version: \"v1\"\n id: string\n status: \"settled_on_chain\" | \"verified_pending_redemption\"\n completeness: \"full_receipt_bundle\" | \"full\" | \"chain_proof_only\"\n public_receipt_url: string\n api_receipt_url: string\n explorer_url: string | null\n accord?: {\n agreement_hash?: string | null\n verification_receipt_hash?: string | null\n settlement_receipt_hash?: string | null\n agreement_json?: unknown\n verification_receipt_json?: unknown\n settlement_receipt_json?: unknown\n }\n}\n\nexport interface SagePaymentWidgetOptions {\n /**\n * Base URL of the Sage host. Default: ergoblockchain.org.\n */\n apiBase?: string\n /**\n * Optional tenant metadata. Current public Sage ignores tenant routing,\n * but the widget keeps this shape stable for multi-tenant deployments.\n */\n tenant?: SageTenantConfig\n /** Initial chat messages, useful for preloaded context. */\n initialMessages?: SageChatMessage[]\n /** Placeholder text for the default input. */\n placeholder?: string\n /** Widget heading. React also accepts this through component props. */\n title?: string\n /** Optional host-specific payment copy and links. */\n paymentInstructions?: SagePaymentInstructions\n /**\n * Optional wallet launcher supplied by the host app. The package does not\n * sign transactions itself; this callback receives a structured intent that\n * your wallet layer can transform into a testnet Note.\n */\n walletLauncher?: SageWalletLauncher\n /**\n * Show the portable payment intent JSON in the default UI. Default: true.\n * Hosts with a fully custom wallet flow can set this to false.\n */\n showPaymentIntent?: boolean\n /**\n * Testnet safety copy for the default widget. Set to false to hide.\n */\n testnetWarning?: string | false\n /** Called whenever a message is appended by the widget. */\n onMessage?: (message: SageChatMessage, messages: SageChatMessage[]) => void\n /** Called after Sage returns a premium quote. */\n onQuote?: (quote: SageQuoteResponse) => void\n /** Called when a structured payment intent is produced for the active quote. */\n onPaymentIntent?: (intent: SagePaymentIntent) => void\n /** Called after a payment verifies and Sage returns a receipt link. */\n onReceipt?: (receipt: SageVerifyPaymentResponse) => void\n /** Called after the widget fetches the full machine-readable receipt bundle. */\n onReceiptBundle?: (receipt: SageReceiptBundle) => void\n /** Called when the chat stream reports free vs premium tier. */\n onTier?: (tier: SageChatTier) => void\n /** Called when the widget phase changes. */\n onPhase?: (phase: SagePaymentPhase) => void\n /** Called with a compact state snapshot after important widget events. */\n onStatus?: (status: SagePaymentWidgetStatus) => void\n /** Optional callback fired on fetch or stream errors. */\n onError?: (error: unknown) => void\n}\n\nexport type SagePaymentPhase =\n | \"idle\"\n | \"quoting\"\n | \"payment_required\"\n | \"verifying\"\n | \"streaming\"\n | \"error\"\n\nexport interface SagePaymentInstructions {\n /** Short copy displayed above the Note box input. */\n helperText?: string\n /** Optional link to host payment/wallet instructions. */\n walletUrl?: string\n /** Custom label for the wallet launcher button. */\n walletLauncherLabel?: string\n /** Optional custom label for the Note box input. */\n noteBoxLabel?: string\n}\n\nexport interface SagePaymentWidgetStatus {\n phase: SagePaymentPhase\n tier: SageChatTier | null\n quote: SageQuoteResponse[\"quote\"] | null\n paymentIntent: SagePaymentIntent | null\n receipt: SageVerifyPaymentResponse | null\n receiptBundle: SageReceiptBundle | null\n error: string | null\n /** Latest chat transcript known to the widget. */\n messages: SageChatMessage[]\n /** Question currently tied to the active quote/payment cycle. */\n activeQuestion: string | null\n}\n\nexport const DEFAULT_API_BASE = \"https://www.ergoblockchain.org\"\nexport const DEFAULT_LIMIT = 5\nexport const DEFAULT_REFRESH_MS = 60_000\n","/**\n * Thin client over /api/sage/activity.\n *\n * Pure fetch + shape — no rendering, no DOM. React + vanilla mounts\n * both call into here so the API contract lives in one place.\n */\n\nimport {\n DEFAULT_API_BASE,\n DEFAULT_LIMIT,\n type SageChatMessage,\n type SageChatStreamEvent,\n type SageChatStreamResult,\n type SagePaymentIntent,\n type SagePaymentNetwork,\n type SageQuote,\n type SageQuoteResponse,\n type SageReceiptBundle,\n type SageTenantConfig,\n type SageVerifyPaymentResponse,\n type SageActivityResponse,\n} from \"./types\"\n\nexport interface FetchActivityOptions {\n apiBase?: string\n limit?: number\n signal?: AbortSignal\n}\n\nexport interface SageRequestOptions {\n apiBase?: string\n tenant?: SageTenantConfig\n headers?: Record<string, string>\n signal?: AbortSignal\n}\n\nexport interface FetchSageQuoteOptions extends SageRequestOptions {\n question: string\n history?: SageChatMessage[]\n}\n\nexport interface VerifySagePaymentOptions extends SageRequestOptions {\n quote: SageQuote\n question: string\n noteBoxId: string\n}\n\nexport interface CreateSagePaymentIntentOptions {\n quote: SageQuote\n question: string\n apiBase?: string\n tenant?: SageTenantConfig\n network?: SagePaymentNetwork\n createdAt?: string\n}\n\nexport interface StreamSageChatOptions extends SageRequestOptions {\n messages: SageChatMessage[]\n paymentToken?: string\n onEvent?: (event: SageChatStreamEvent) => void\n}\n\nexport async function fetchSageActivity(\n opts: FetchActivityOptions = {},\n): Promise<SageActivityResponse> {\n const base = opts.apiBase ?? DEFAULT_API_BASE\n const limit = Math.min(Math.max(opts.limit ?? DEFAULT_LIMIT, 1), 25)\n const url = `${trimSlash(base)}/api/sage/activity?limit=${limit}`\n const res = await fetch(url, { signal: opts.signal })\n if (!res.ok) {\n throw new Error(`sage activity ${res.status}`)\n }\n return (await res.json()) as SageActivityResponse\n}\n\nexport async function fetchSageQuote(\n opts: FetchSageQuoteOptions,\n): Promise<SageQuoteResponse> {\n const res = await fetch(`${apiBase(opts)}/api/sage/quote`, {\n method: \"POST\",\n headers: jsonHeaders(opts),\n body: JSON.stringify({\n question: opts.question,\n history: opts.history ?? [],\n }),\n signal: opts.signal,\n })\n const body = await parseJson(res)\n if (!res.ok) throw new Error(readError(body, `sage quote ${res.status}`))\n return body as SageQuoteResponse\n}\n\nexport async function verifySagePayment(\n opts: VerifySagePaymentOptions,\n): Promise<SageVerifyPaymentResponse> {\n const res = await fetch(`${apiBase(opts)}/api/sage/verify-payment`, {\n method: \"POST\",\n headers: jsonHeaders(opts),\n body: JSON.stringify({\n quote: opts.quote,\n question: opts.question,\n noteBoxId: opts.noteBoxId,\n }),\n signal: opts.signal,\n })\n const body = await parseJson(res)\n if (!res.ok) throw new Error(readError(body, `sage verify-payment ${res.status}`))\n return body as SageVerifyPaymentResponse\n}\n\nexport async function fetchSageReceipt(\n id: string,\n opts: SageRequestOptions = {},\n): Promise<SageReceiptBundle> {\n const res = await fetch(`${apiBase(opts)}/api/sage/receipt/${encodeURIComponent(id)}`, {\n headers: requestHeaders(opts),\n signal: opts.signal,\n })\n const body = await parseJson(res)\n if (!res.ok) throw new Error(readError(body, `sage receipt ${res.status}`))\n return body as SageReceiptBundle\n}\n\nexport function isFullSageReceiptBundle(value: SageReceiptBundle | null | undefined): boolean {\n return value?.ok === true && value.completeness === \"full_receipt_bundle\"\n}\n\nexport function createSagePaymentIntent(\n opts: CreateSagePaymentIntentOptions,\n): SagePaymentIntent {\n const base = trimSlash(opts.apiBase ?? DEFAULT_API_BASE)\n return {\n type: \"sage.payment_intent.v1\",\n network: opts.network ?? \"ergo-testnet\",\n createdAt: opts.createdAt ?? new Date().toISOString(),\n question: opts.question,\n ...(opts.tenant?.id || opts.tenant?.label\n ? { tenant: { id: opts.tenant.id, label: opts.tenant.label } }\n : {}),\n quote: opts.quote,\n amountErg: opts.quote.price,\n receiverAddress: opts.quote.receiverAddress,\n reserveBoxId: opts.quote.reserveBoxId,\n taskHash: opts.quote.taskHash,\n expiresAt: opts.quote.expiresAt,\n deadline: opts.quote.deadline,\n verifyEndpoint: `${base}/api/sage/verify-payment`,\n receiptEndpointTemplate: `${base}/api/sage/receipt/{receiptId}`,\n }\n}\n\nexport function serializeSagePaymentIntent(intent: SagePaymentIntent): string {\n return JSON.stringify(intent, null, 2)\n}\n\nexport async function streamSageChat(\n opts: StreamSageChatOptions,\n): Promise<SageChatStreamResult> {\n const res = await fetch(`${apiBase(opts)}/api/sage/chat`, {\n method: \"POST\",\n headers: jsonHeaders(opts),\n body: JSON.stringify({\n messages: opts.messages,\n ...(opts.paymentToken ? { paymentToken: opts.paymentToken } : {}),\n }),\n signal: opts.signal,\n })\n\n if (res.status === 402) {\n const body = await parseJson(res)\n return {\n ok: false,\n status: res.status,\n text: \"\",\n paymentRequired: body as SageChatStreamResult[\"paymentRequired\"],\n }\n }\n\n if (!res.ok) {\n const body = await parseJson(res)\n return {\n ok: false,\n status: res.status,\n text: \"\",\n error: readError(body, `sage chat ${res.status}`),\n }\n }\n\n if (!res.body) {\n return { ok: false, status: res.status, text: \"\", error: \"Sage chat stream missing body\" }\n }\n\n const reader = res.body.getReader()\n const decoder = new TextDecoder()\n let buffer = \"\"\n let text = \"\"\n let tier: SageChatStreamResult[\"tier\"]\n\n while (true) {\n const { value, done } = await reader.read()\n if (done) break\n buffer += decoder.decode(value, { stream: true })\n const parts = buffer.split(\"\\n\\n\")\n buffer = parts.pop() ?? \"\"\n for (const part of parts) {\n const event = parseSseEvent(part)\n if (!event) continue\n if (event.type === \"delta\") text += event.text\n if (event.type === \"tier\") tier = event.tier\n opts.onEvent?.(event)\n if (event.type === \"error\") {\n return {\n ok: false,\n status: res.status,\n text,\n tier,\n error: event.message,\n }\n }\n }\n }\n\n if (buffer.trim()) {\n const event = parseSseEvent(buffer)\n if (event) {\n if (event.type === \"delta\") text += event.text\n if (event.type === \"tier\") tier = event.tier\n opts.onEvent?.(event)\n if (event.type === \"error\") {\n return {\n ok: false,\n status: res.status,\n text,\n tier,\n error: event.message,\n }\n }\n }\n }\n\n return { ok: true, status: res.status, text, tier }\n}\n\n/** nanoERG → \"0.001\" (trims trailing zeros, max 9 decimals). */\nexport function nanoToErg(nano: number | undefined): string {\n if (!nano || nano <= 0) return \"0\"\n const erg = nano / 1e9\n return erg.toFixed(9).replace(/\\.?0+$/, \"\")\n}\n\n/** Cheap relative-time formatter, ASCII-only, no Intl deps. */\nexport function relativeTime(ms: number, now: number = Date.now()): string {\n const diff = Math.max(0, now - ms)\n const sec = Math.floor(diff / 1000)\n if (sec < 60) return `${sec}s ago`\n const min = Math.floor(sec / 60)\n if (min < 60) return `${min}m ago`\n const hr = Math.floor(min / 60)\n if (hr < 24) return `${hr}h ago`\n const day = Math.floor(hr / 24)\n return `${day}d ago`\n}\n\n/** Receipt URL for a settled tx, given the host base. */\nexport function receiptUrl(txId: string, apiBase: string = DEFAULT_API_BASE): string {\n return `${trimSlash(apiBase)}/r/sage/${txId}`\n}\n\n/** Explorer URL for a tx on the given network. */\nexport function explorerUrl(\n txId: string,\n network: \"testnet\" | \"mainnet\" = \"testnet\",\n): string {\n return network === \"testnet\"\n ? `https://testnet.ergoplatform.com/transactions/${txId}`\n : `https://explorer.ergoplatform.com/transactions/${txId}`\n}\n\nfunction apiBase(opts: SageRequestOptions): string {\n return trimSlash(opts.apiBase ?? DEFAULT_API_BASE)\n}\n\nfunction trimSlash(value: string): string {\n return value.replace(/\\/+$/, \"\")\n}\n\nfunction requestHeaders(opts: SageRequestOptions): Record<string, string> {\n return {\n ...(opts.tenant?.id ? { \"x-sage-tenant-id\": opts.tenant.id } : {}),\n ...(opts.tenant?.headers ?? {}),\n ...(opts.headers ?? {}),\n }\n}\n\nfunction jsonHeaders(opts: SageRequestOptions): Record<string, string> {\n return {\n \"content-type\": \"application/json\",\n ...requestHeaders(opts),\n }\n}\n\nasync function parseJson(res: Response): Promise<unknown> {\n const text = await res.text()\n if (!text) return null\n try {\n return JSON.parse(text)\n } catch {\n return { error: text }\n }\n}\n\nfunction readError(body: unknown, fallback: string): string {\n if (body && typeof body === \"object\" && \"error\" in body) {\n const error = (body as { error?: unknown }).error\n if (typeof error === \"string\") return error\n }\n return fallback\n}\n\nfunction parseSseEvent(raw: string): SageChatStreamEvent | null {\n let eventName = \"message\"\n let data = \"\"\n for (const line of raw.split(\"\\n\")) {\n if (line.startsWith(\"event:\")) eventName = line.slice(\"event:\".length).trim()\n if (line.startsWith(\"data:\")) data += line.slice(\"data:\".length).trim()\n }\n if (!data) return null\n let parsed: Record<string, unknown>\n try {\n parsed = JSON.parse(data) as Record<string, unknown>\n } catch {\n return null\n }\n if (eventName === \"tier\") {\n const tier = parsed.tier === \"premium\" ? \"premium\" : \"free\"\n return {\n type: \"tier\",\n tier,\n ...(typeof parsed.model === \"string\" ? { model: parsed.model } : {}),\n }\n }\n if (eventName === \"delta\" && typeof parsed.text === \"string\") {\n return { type: \"delta\", text: parsed.text }\n }\n if (eventName === \"done\") {\n return {\n type: \"done\",\n ...(typeof parsed.stopReason === \"string\" ? { stopReason: parsed.stopReason } : {}),\n ...(typeof parsed.inputTokens === \"number\" ? { inputTokens: parsed.inputTokens } : {}),\n ...(typeof parsed.outputTokens === \"number\" ? { outputTokens: parsed.outputTokens } : {}),\n }\n }\n if (eventName === \"error\") {\n return {\n type: \"error\",\n message: typeof parsed.message === \"string\" ? parsed.message : \"Sage stream error\",\n }\n }\n return null\n}\n"]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { c as SageActivityResponse } from './types-uzMz6_mP.cjs';
2
- export { D as DEFAULT_API_BASE, a as DEFAULT_LIMIT, b as DEFAULT_REFRESH_MS, S as SageActivityEvent, d as SageActivityType, e as SageWidgetOptions } from './types-uzMz6_mP.cjs';
1
+ import { r as SageQuote, u as SageTenantConfig, l as SagePaymentNetwork, k as SagePaymentIntent, c as SageActivityResponse, e as SageChatMessage, s as SageQuoteResponse, t as SageReceiptBundle, g as SageChatStreamEvent, h as SageChatStreamResult, v as SageVerifyPaymentResponse } from './types-B5V2rsYn.cjs';
2
+ export { D as DEFAULT_API_BASE, a as DEFAULT_LIMIT, b as DEFAULT_REFRESH_MS, S as SageActivityEvent, d as SageActivityType, f as SageChatRole, i as SageChatTier, j as SagePaymentInstructions, m as SagePaymentPhase, n as SagePaymentWidgetOptions, o as SagePaymentWidgetStatus, p as SagePremiumPaymentRequired, q as SagePremiumReason, w as SageWalletLaunchResult, x as SageWalletLauncher, y as SageWidgetOptions } from './types-B5V2rsYn.cjs';
3
3
 
4
4
  /**
5
5
  * Thin client over /api/sage/activity.
@@ -13,7 +13,42 @@ interface FetchActivityOptions {
13
13
  limit?: number;
14
14
  signal?: AbortSignal;
15
15
  }
16
+ interface SageRequestOptions {
17
+ apiBase?: string;
18
+ tenant?: SageTenantConfig;
19
+ headers?: Record<string, string>;
20
+ signal?: AbortSignal;
21
+ }
22
+ interface FetchSageQuoteOptions extends SageRequestOptions {
23
+ question: string;
24
+ history?: SageChatMessage[];
25
+ }
26
+ interface VerifySagePaymentOptions extends SageRequestOptions {
27
+ quote: SageQuote;
28
+ question: string;
29
+ noteBoxId: string;
30
+ }
31
+ interface CreateSagePaymentIntentOptions {
32
+ quote: SageQuote;
33
+ question: string;
34
+ apiBase?: string;
35
+ tenant?: SageTenantConfig;
36
+ network?: SagePaymentNetwork;
37
+ createdAt?: string;
38
+ }
39
+ interface StreamSageChatOptions extends SageRequestOptions {
40
+ messages: SageChatMessage[];
41
+ paymentToken?: string;
42
+ onEvent?: (event: SageChatStreamEvent) => void;
43
+ }
16
44
  declare function fetchSageActivity(opts?: FetchActivityOptions): Promise<SageActivityResponse>;
45
+ declare function fetchSageQuote(opts: FetchSageQuoteOptions): Promise<SageQuoteResponse>;
46
+ declare function verifySagePayment(opts: VerifySagePaymentOptions): Promise<SageVerifyPaymentResponse>;
47
+ declare function fetchSageReceipt(id: string, opts?: SageRequestOptions): Promise<SageReceiptBundle>;
48
+ declare function isFullSageReceiptBundle(value: SageReceiptBundle | null | undefined): boolean;
49
+ declare function createSagePaymentIntent(opts: CreateSagePaymentIntentOptions): SagePaymentIntent;
50
+ declare function serializeSagePaymentIntent(intent: SagePaymentIntent): string;
51
+ declare function streamSageChat(opts: StreamSageChatOptions): Promise<SageChatStreamResult>;
17
52
  /** nanoERG → "0.001" (trims trailing zeros, max 9 decimals). */
18
53
  declare function nanoToErg(nano: number | undefined): string;
19
54
  /** Cheap relative-time formatter, ASCII-only, no Intl deps. */
@@ -23,4 +58,4 @@ declare function receiptUrl(txId: string, apiBase?: string): string;
23
58
  /** Explorer URL for a tx on the given network. */
24
59
  declare function explorerUrl(txId: string, network?: "testnet" | "mainnet"): string;
25
60
 
26
- export { SageActivityResponse, explorerUrl, fetchSageActivity, nanoToErg, receiptUrl, relativeTime };
61
+ export { SageActivityResponse, SageChatMessage, SageChatStreamEvent, SageChatStreamResult, SagePaymentIntent, SagePaymentNetwork, SageQuote, SageQuoteResponse, SageReceiptBundle, SageTenantConfig, SageVerifyPaymentResponse, createSagePaymentIntent, explorerUrl, fetchSageActivity, fetchSageQuote, fetchSageReceipt, isFullSageReceiptBundle, nanoToErg, receiptUrl, relativeTime, serializeSagePaymentIntent, streamSageChat, verifySagePayment };