@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/dist/react.cjs CHANGED
@@ -14,13 +14,151 @@ var DEFAULT_REFRESH_MS = 6e4;
14
14
  async function fetchSageActivity(opts = {}) {
15
15
  const base = opts.apiBase ?? DEFAULT_API_BASE;
16
16
  const limit = Math.min(Math.max(opts.limit ?? DEFAULT_LIMIT, 1), 25);
17
- const url = `${base}/api/sage/activity?limit=${limit}`;
17
+ const url = `${trimSlash(base)}/api/sage/activity?limit=${limit}`;
18
18
  const res = await fetch(url, { signal: opts.signal });
19
19
  if (!res.ok) {
20
20
  throw new Error(`sage activity ${res.status}`);
21
21
  }
22
22
  return await res.json();
23
23
  }
24
+ async function fetchSageQuote(opts) {
25
+ const res = await fetch(`${apiBase(opts)}/api/sage/quote`, {
26
+ method: "POST",
27
+ headers: jsonHeaders(opts),
28
+ body: JSON.stringify({
29
+ question: opts.question,
30
+ history: opts.history ?? []
31
+ }),
32
+ signal: opts.signal
33
+ });
34
+ const body = await parseJson(res);
35
+ if (!res.ok) throw new Error(readError(body, `sage quote ${res.status}`));
36
+ return body;
37
+ }
38
+ async function verifySagePayment(opts) {
39
+ const res = await fetch(`${apiBase(opts)}/api/sage/verify-payment`, {
40
+ method: "POST",
41
+ headers: jsonHeaders(opts),
42
+ body: JSON.stringify({
43
+ quote: opts.quote,
44
+ question: opts.question,
45
+ noteBoxId: opts.noteBoxId
46
+ }),
47
+ signal: opts.signal
48
+ });
49
+ const body = await parseJson(res);
50
+ if (!res.ok) throw new Error(readError(body, `sage verify-payment ${res.status}`));
51
+ return body;
52
+ }
53
+ async function fetchSageReceipt(id, opts = {}) {
54
+ const res = await fetch(`${apiBase(opts)}/api/sage/receipt/${encodeURIComponent(id)}`, {
55
+ headers: requestHeaders(opts),
56
+ signal: opts.signal
57
+ });
58
+ const body = await parseJson(res);
59
+ if (!res.ok) throw new Error(readError(body, `sage receipt ${res.status}`));
60
+ return body;
61
+ }
62
+ function createSagePaymentIntent(opts) {
63
+ const base = trimSlash(opts.apiBase ?? DEFAULT_API_BASE);
64
+ return {
65
+ type: "sage.payment_intent.v1",
66
+ network: opts.network ?? "ergo-testnet",
67
+ createdAt: opts.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
68
+ question: opts.question,
69
+ ...opts.tenant?.id || opts.tenant?.label ? { tenant: { id: opts.tenant.id, label: opts.tenant.label } } : {},
70
+ quote: opts.quote,
71
+ amountErg: opts.quote.price,
72
+ receiverAddress: opts.quote.receiverAddress,
73
+ reserveBoxId: opts.quote.reserveBoxId,
74
+ taskHash: opts.quote.taskHash,
75
+ expiresAt: opts.quote.expiresAt,
76
+ deadline: opts.quote.deadline,
77
+ verifyEndpoint: `${base}/api/sage/verify-payment`,
78
+ receiptEndpointTemplate: `${base}/api/sage/receipt/{receiptId}`
79
+ };
80
+ }
81
+ function serializeSagePaymentIntent(intent) {
82
+ return JSON.stringify(intent, null, 2);
83
+ }
84
+ async function streamSageChat(opts) {
85
+ const res = await fetch(`${apiBase(opts)}/api/sage/chat`, {
86
+ method: "POST",
87
+ headers: jsonHeaders(opts),
88
+ body: JSON.stringify({
89
+ messages: opts.messages,
90
+ ...opts.paymentToken ? { paymentToken: opts.paymentToken } : {}
91
+ }),
92
+ signal: opts.signal
93
+ });
94
+ if (res.status === 402) {
95
+ const body = await parseJson(res);
96
+ return {
97
+ ok: false,
98
+ status: res.status,
99
+ text: "",
100
+ paymentRequired: body
101
+ };
102
+ }
103
+ if (!res.ok) {
104
+ const body = await parseJson(res);
105
+ return {
106
+ ok: false,
107
+ status: res.status,
108
+ text: "",
109
+ error: readError(body, `sage chat ${res.status}`)
110
+ };
111
+ }
112
+ if (!res.body) {
113
+ return { ok: false, status: res.status, text: "", error: "Sage chat stream missing body" };
114
+ }
115
+ const reader = res.body.getReader();
116
+ const decoder = new TextDecoder();
117
+ let buffer = "";
118
+ let text = "";
119
+ let tier;
120
+ while (true) {
121
+ const { value, done } = await reader.read();
122
+ if (done) break;
123
+ buffer += decoder.decode(value, { stream: true });
124
+ const parts = buffer.split("\n\n");
125
+ buffer = parts.pop() ?? "";
126
+ for (const part of parts) {
127
+ const event = parseSseEvent(part);
128
+ if (!event) continue;
129
+ if (event.type === "delta") text += event.text;
130
+ if (event.type === "tier") tier = event.tier;
131
+ opts.onEvent?.(event);
132
+ if (event.type === "error") {
133
+ return {
134
+ ok: false,
135
+ status: res.status,
136
+ text,
137
+ tier,
138
+ error: event.message
139
+ };
140
+ }
141
+ }
142
+ }
143
+ if (buffer.trim()) {
144
+ const event = parseSseEvent(buffer);
145
+ if (event) {
146
+ if (event.type === "delta") text += event.text;
147
+ if (event.type === "tier") tier = event.tier;
148
+ opts.onEvent?.(event);
149
+ if (event.type === "error") {
150
+ return {
151
+ ok: false,
152
+ status: res.status,
153
+ text,
154
+ tier,
155
+ error: event.message
156
+ };
157
+ }
158
+ }
159
+ }
160
+ return { ok: true, status: res.status, text, tier };
161
+ }
24
162
  function nanoToErg(nano) {
25
163
  if (!nano || nano <= 0) return "0";
26
164
  const erg = nano / 1e9;
@@ -37,16 +175,92 @@ function relativeTime(ms, now = Date.now()) {
37
175
  const day = Math.floor(hr / 24);
38
176
  return `${day}d ago`;
39
177
  }
40
- function receiptUrl(txId, apiBase = DEFAULT_API_BASE) {
41
- return `${apiBase}/r/sage/${txId}`;
178
+ function receiptUrl(txId, apiBase2 = DEFAULT_API_BASE) {
179
+ return `${trimSlash(apiBase2)}/r/sage/${txId}`;
42
180
  }
43
181
  function explorerUrl(txId, network = "testnet") {
44
182
  return network === "testnet" ? `https://testnet.ergoplatform.com/transactions/${txId}` : `https://explorer.ergoplatform.com/transactions/${txId}`;
45
183
  }
184
+ function apiBase(opts) {
185
+ return trimSlash(opts.apiBase ?? DEFAULT_API_BASE);
186
+ }
187
+ function trimSlash(value) {
188
+ return value.replace(/\/+$/, "");
189
+ }
190
+ function requestHeaders(opts) {
191
+ return {
192
+ ...opts.tenant?.id ? { "x-sage-tenant-id": opts.tenant.id } : {},
193
+ ...opts.tenant?.headers ?? {},
194
+ ...opts.headers ?? {}
195
+ };
196
+ }
197
+ function jsonHeaders(opts) {
198
+ return {
199
+ "content-type": "application/json",
200
+ ...requestHeaders(opts)
201
+ };
202
+ }
203
+ async function parseJson(res) {
204
+ const text = await res.text();
205
+ if (!text) return null;
206
+ try {
207
+ return JSON.parse(text);
208
+ } catch {
209
+ return { error: text };
210
+ }
211
+ }
212
+ function readError(body, fallback) {
213
+ if (body && typeof body === "object" && "error" in body) {
214
+ const error = body.error;
215
+ if (typeof error === "string") return error;
216
+ }
217
+ return fallback;
218
+ }
219
+ function parseSseEvent(raw) {
220
+ let eventName = "message";
221
+ let data = "";
222
+ for (const line of raw.split("\n")) {
223
+ if (line.startsWith("event:")) eventName = line.slice("event:".length).trim();
224
+ if (line.startsWith("data:")) data += line.slice("data:".length).trim();
225
+ }
226
+ if (!data) return null;
227
+ let parsed;
228
+ try {
229
+ parsed = JSON.parse(data);
230
+ } catch {
231
+ return null;
232
+ }
233
+ if (eventName === "tier") {
234
+ const tier = parsed.tier === "premium" ? "premium" : "free";
235
+ return {
236
+ type: "tier",
237
+ tier,
238
+ ...typeof parsed.model === "string" ? { model: parsed.model } : {}
239
+ };
240
+ }
241
+ if (eventName === "delta" && typeof parsed.text === "string") {
242
+ return { type: "delta", text: parsed.text };
243
+ }
244
+ if (eventName === "done") {
245
+ return {
246
+ type: "done",
247
+ ...typeof parsed.stopReason === "string" ? { stopReason: parsed.stopReason } : {},
248
+ ...typeof parsed.inputTokens === "number" ? { inputTokens: parsed.inputTokens } : {},
249
+ ...typeof parsed.outputTokens === "number" ? { outputTokens: parsed.outputTokens } : {}
250
+ };
251
+ }
252
+ if (eventName === "error") {
253
+ return {
254
+ type: "error",
255
+ message: typeof parsed.message === "string" ? parsed.message : "Sage stream error"
256
+ };
257
+ }
258
+ return null;
259
+ }
46
260
  var ONE_MIN = 6e4;
47
261
  function SageActivityFeed(props) {
48
262
  const {
49
- apiBase,
263
+ apiBase: apiBase2,
50
264
  limit = DEFAULT_LIMIT,
51
265
  refreshMs = DEFAULT_REFRESH_MS,
52
266
  onUpdate,
@@ -73,7 +287,7 @@ function SageActivityFeed(props) {
73
287
  async function load() {
74
288
  try {
75
289
  const data = await fetchSageActivity({
76
- apiBase,
290
+ apiBase: apiBase2,
77
291
  limit,
78
292
  signal: abort.signal
79
293
  });
@@ -98,7 +312,7 @@ function SageActivityFeed(props) {
98
312
  if (pollId) clearInterval(pollId);
99
313
  clearInterval(tickId);
100
314
  };
101
- }, [apiBase, limit, refreshMs]);
315
+ }, [apiBase2, limit, refreshMs]);
102
316
  if (children) {
103
317
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: children({ loading, response, error }) });
104
318
  }
@@ -110,7 +324,7 @@ function SageActivityFeed(props) {
110
324
  loading,
111
325
  events: response?.events ?? [],
112
326
  network: response?.network ?? "testnet",
113
- apiBase,
327
+ apiBase: apiBase2,
114
328
  now
115
329
  }
116
330
  ),
@@ -140,22 +354,22 @@ function List({
140
354
  loading,
141
355
  events,
142
356
  network,
143
- apiBase,
357
+ apiBase: apiBase2,
144
358
  now
145
359
  }) {
146
360
  if (loading) return /* @__PURE__ */ jsxRuntime.jsx("div", { style: emptyStyle, children: "Loading\u2026" });
147
361
  if (events.length === 0)
148
362
  return /* @__PURE__ */ jsxRuntime.jsx("div", { style: emptyStyle, children: "No activity yet \u2014 be the first to ask Sage a paid query." });
149
- return /* @__PURE__ */ jsxRuntime.jsx("div", { children: events.map((evt) => /* @__PURE__ */ jsxRuntime.jsx(Row, { evt, network, apiBase, now }, evt.txId)) });
363
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { children: events.map((evt) => /* @__PURE__ */ jsxRuntime.jsx(Row, { evt, network, apiBase: apiBase2, now }, evt.txId)) });
150
364
  }
151
365
  function Row({
152
366
  evt,
153
367
  network,
154
- apiBase,
368
+ apiBase: apiBase2,
155
369
  now
156
370
  }) {
157
371
  const isSettle = evt.type === "settlement";
158
- const href = isSettle ? receiptUrl(evt.txId, apiBase) : explorerUrl(evt.txId, network);
372
+ const href = isSettle ? receiptUrl(evt.txId, apiBase2) : explorerUrl(evt.txId, network);
159
373
  const amount = isSettle ? evt.paymentNanoErg ?? evt.inflowNanoErg : evt.inflowNanoErg;
160
374
  const chipStyle = isSettle ? chipSettleStyle : chipIssueStyle;
161
375
  const chipText = isSettle ? "Settled" : evt.type === "issuance" ? "Issued" : "Transfer";
@@ -293,10 +507,579 @@ var errorStyle = {
293
507
  ...emptyStyle,
294
508
  color: "#f87171"
295
509
  };
510
+ function SagePaymentWidget(props) {
511
+ const {
512
+ apiBase: apiBase2 = DEFAULT_API_BASE,
513
+ tenant,
514
+ initialMessages = [],
515
+ placeholder = "Ask Sage about Ergo or agent payments...",
516
+ paymentInstructions,
517
+ showPaymentIntent = true,
518
+ testnetWarning = "Testnet proof flow. The widget never signs funds; connect your own reviewed wallet layer before handling real value.",
519
+ className,
520
+ style,
521
+ title = "Ask Sage"
522
+ } = props;
523
+ const [messages, setMessages] = react.useState(initialMessages);
524
+ const [input, setInput] = react.useState("");
525
+ const [phase, setPhase] = react.useState("idle");
526
+ const [quoteResponse, setQuoteResponse] = react.useState(null);
527
+ const [paymentIntent, setPaymentIntent] = react.useState(null);
528
+ const [activeQuestion, setActiveQuestion] = react.useState("");
529
+ const [noteBoxId, setNoteBoxId] = react.useState("");
530
+ const [receipt, setReceipt] = react.useState(null);
531
+ const [receiptBundle, setReceiptBundle] = react.useState(null);
532
+ const [error, setError] = react.useState(null);
533
+ const [tier, setTier] = react.useState(null);
534
+ const mountedRef = react.useRef(true);
535
+ react.useEffect(() => {
536
+ mountedRef.current = true;
537
+ return () => {
538
+ mountedRef.current = false;
539
+ };
540
+ }, []);
541
+ const busy = phase === "quoting" || phase === "verifying" || phase === "streaming";
542
+ const apiOpts = react.useMemo(() => ({ apiBase: apiBase2, tenant }), [apiBase2, tenant]);
543
+ async function submit(e) {
544
+ e.preventDefault();
545
+ const question = input.trim();
546
+ if (!question || busy) return;
547
+ const userMessage = { role: "user", content: question };
548
+ const nextMessages = [...messages, userMessage];
549
+ setMessages(nextMessages);
550
+ props.onMessage?.(userMessage, nextMessages);
551
+ setInput("");
552
+ setError(null);
553
+ setReceipt(null);
554
+ setReceiptBundle(null);
555
+ setQuoteResponse(null);
556
+ setPaymentIntent(null);
557
+ setActiveQuestion(question);
558
+ setNoteBoxId("");
559
+ setTier(null);
560
+ transition("quoting");
561
+ try {
562
+ const quote2 = await fetchSageQuote({
563
+ ...apiOpts,
564
+ question,
565
+ history: messages
566
+ });
567
+ props.onQuote?.(quote2);
568
+ if (quote2.premium) {
569
+ if (!quote2.quote) throw new Error("Sage marked this question premium but did not return a quote.");
570
+ const intent = createSagePaymentIntent({
571
+ ...apiOpts,
572
+ question,
573
+ quote: quote2.quote
574
+ });
575
+ setPaymentIntent(intent);
576
+ setQuoteResponse(quote2);
577
+ props.onPaymentIntent?.(intent);
578
+ transition("payment_required", { quote: quote2.quote, paymentIntent: intent });
579
+ return;
580
+ }
581
+ await streamAnswer(nextMessages, void 0, question);
582
+ } catch (err) {
583
+ fail(err);
584
+ }
585
+ }
586
+ async function verifyAndContinue() {
587
+ const quote2 = quoteResponse?.quote;
588
+ const note = noteBoxId.trim();
589
+ if (!quote2 || !activeQuestion || !note || busy) return;
590
+ setError(null);
591
+ transition("verifying");
592
+ let verified;
593
+ try {
594
+ verified = await verifySagePayment({
595
+ ...apiOpts,
596
+ quote: quote2,
597
+ question: activeQuestion,
598
+ noteBoxId: note
599
+ });
600
+ } catch (err) {
601
+ transition("payment_required");
602
+ fail(err, false);
603
+ return;
604
+ }
605
+ if (!mountedRef.current) return;
606
+ setReceipt(verified);
607
+ props.onReceipt?.(verified);
608
+ setQuoteResponse(null);
609
+ setPaymentIntent(null);
610
+ setNoteBoxId("");
611
+ transition("streaming", { quote: null, paymentIntent: null, receipt: verified });
612
+ try {
613
+ const bundle = await fetchSageReceipt(verified.receiptId, apiOpts);
614
+ if (!mountedRef.current) return;
615
+ setReceiptBundle(bundle);
616
+ props.onReceiptBundle?.(bundle);
617
+ emitStatus("streaming", { quote: null, paymentIntent: null, receipt: verified, receiptBundle: bundle });
618
+ } catch (bundleErr) {
619
+ props.onError?.(bundleErr);
620
+ }
621
+ try {
622
+ await streamAnswer(messages, verified.paymentToken, activeQuestion);
623
+ } catch (err) {
624
+ fail(err);
625
+ }
626
+ }
627
+ async function streamAnswer(baseMessages, paymentToken, fallbackQuestion) {
628
+ transition("streaming");
629
+ let text = "";
630
+ const placeholderMessage = { role: "assistant", content: "" };
631
+ setMessages([...baseMessages, placeholderMessage]);
632
+ const result = await streamSageChat({
633
+ ...apiOpts,
634
+ messages: baseMessages,
635
+ paymentToken,
636
+ onEvent(event) {
637
+ if (!mountedRef.current) return;
638
+ if (event.type === "tier") {
639
+ setTier(event.tier);
640
+ props.onTier?.(event.tier);
641
+ emitStatus("streaming", { tier: event.tier, messages: baseMessages });
642
+ }
643
+ if (event.type === "delta") {
644
+ text += event.text;
645
+ setMessages([...baseMessages, { role: "assistant", content: text }]);
646
+ }
647
+ }
648
+ });
649
+ if (!result.ok) {
650
+ if (result.paymentRequired) {
651
+ const question = fallbackQuestion ?? lastUserQuestion(baseMessages) ?? activeQuestion;
652
+ const quote2 = await fetchSageQuote({
653
+ ...apiOpts,
654
+ question,
655
+ history: baseMessages.slice(0, -1)
656
+ });
657
+ if (!mountedRef.current) return;
658
+ props.onQuote?.(quote2);
659
+ setQuoteResponse(quote2);
660
+ setActiveQuestion(question);
661
+ const intent = quote2.quote ? createSagePaymentIntent({ ...apiOpts, question, quote: quote2.quote }) : null;
662
+ setPaymentIntent(intent);
663
+ if (intent) props.onPaymentIntent?.(intent);
664
+ transition("payment_required", { quote: quote2.quote ?? null, paymentIntent: intent });
665
+ return;
666
+ }
667
+ throw new Error(result.error ?? "Sage chat failed.");
668
+ }
669
+ if (!mountedRef.current) return;
670
+ if (result.text && result.text !== text) {
671
+ setMessages([...baseMessages, { role: "assistant", content: result.text }]);
672
+ }
673
+ transition("idle");
674
+ }
675
+ function fail(err, setErrorPhase = true) {
676
+ const message = err instanceof Error ? err.message : "Sage request failed.";
677
+ setError(message);
678
+ if (setErrorPhase) transition("error", { error: message });
679
+ else emitStatus(phase, { error: message });
680
+ props.onError?.(err);
681
+ }
682
+ function transition(next, overrides = {}) {
683
+ setPhase(next);
684
+ props.onPhase?.(next);
685
+ emitStatus(next, overrides);
686
+ }
687
+ function emitStatus(nextPhase, overrides = {}) {
688
+ const has = (key) => Object.prototype.hasOwnProperty.call(overrides, key);
689
+ props.onStatus?.({
690
+ phase: nextPhase,
691
+ tier: has("tier") ? overrides.tier ?? null : tier,
692
+ quote: has("quote") ? overrides.quote ?? null : quoteResponse?.quote ?? null,
693
+ paymentIntent: has("paymentIntent") ? overrides.paymentIntent ?? null : paymentIntent,
694
+ receipt: has("receipt") ? overrides.receipt ?? null : receipt,
695
+ receiptBundle: has("receiptBundle") ? overrides.receiptBundle ?? null : receiptBundle,
696
+ error: has("error") ? overrides.error ?? null : error,
697
+ messages: overrides.messages ?? messages,
698
+ activeQuestion: has("activeQuestion") ? overrides.activeQuestion ?? null : activeQuestion || null
699
+ });
700
+ }
701
+ const quote = quoteResponse?.quote;
702
+ const showPaymentPanel = quote && !receipt;
703
+ async function copyPaymentIntent() {
704
+ if (!paymentIntent) return;
705
+ await copyText(serializeSagePaymentIntent(paymentIntent));
706
+ }
707
+ async function launchWallet() {
708
+ if (!paymentIntent || !props.walletLauncher || busy) return;
709
+ setError(null);
710
+ try {
711
+ const result = await props.walletLauncher(paymentIntent);
712
+ if (result?.ok === false) {
713
+ throw new Error(result.error ?? "Wallet flow did not produce a Note.");
714
+ }
715
+ if (result?.noteBoxId) setNoteBoxId(result.noteBoxId);
716
+ } catch (err) {
717
+ fail(err, false);
718
+ }
719
+ }
720
+ return /* @__PURE__ */ jsxRuntime.jsxs("section", { className, style: { ...rootStyle2, ...style }, children: [
721
+ /* @__PURE__ */ jsxRuntime.jsxs("header", { style: headerStyle2, children: [
722
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
723
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: eyebrowStyle, children: tenant?.label ?? "Ergo agent economy" }),
724
+ /* @__PURE__ */ jsxRuntime.jsx("h2", { style: titleStyle, children: title })
725
+ ] }),
726
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: badgeStyle, children: tier ? tier.toUpperCase() : "SAGE" })
727
+ ] }),
728
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: messagesStyle, "aria-live": "polite", children: messages.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: emptyStyle2, children: "Free questions answer immediately. Premium questions return an Accord Note quote." }) : messages.map((message, index) => /* @__PURE__ */ jsxRuntime.jsx(
729
+ "div",
730
+ {
731
+ style: message.role === "user" ? userBubbleStyle : assistantBubbleStyle,
732
+ children: message.content || (message.role === "assistant" && phase === "streaming" ? "Thinking..." : "")
733
+ },
734
+ `${message.role}-${index}`
735
+ )) }),
736
+ showPaymentPanel ? /* @__PURE__ */ jsxRuntime.jsxs("div", { style: paymentStyle, children: [
737
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: paymentHeaderStyle, children: [
738
+ /* @__PURE__ */ jsxRuntime.jsx("strong", { children: "Payment required" }),
739
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
740
+ quote.price,
741
+ " ERG testnet"
742
+ ] })
743
+ ] }),
744
+ /* @__PURE__ */ jsxRuntime.jsxs("p", { style: helperStyle, children: [
745
+ paymentInstructions?.helperText ?? "Issue an Ergo testnet Note for this quote, then paste the created Note box id.",
746
+ paymentInstructions?.walletUrl ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
747
+ " ",
748
+ /* @__PURE__ */ jsxRuntime.jsx("a", { href: paymentInstructions.walletUrl, target: "_blank", rel: "noopener noreferrer", style: inlineLinkStyle, children: "Wallet guide" })
749
+ ] }) : null
750
+ ] }),
751
+ /* @__PURE__ */ jsxRuntime.jsx(Field, { label: "Quote", value: quote.quoteId }),
752
+ /* @__PURE__ */ jsxRuntime.jsx(Field, { label: "Receiver", value: quote.receiverAddress, copy: true }),
753
+ /* @__PURE__ */ jsxRuntime.jsx(Field, { label: "Reserve box", value: quote.reserveBoxId, copy: true }),
754
+ /* @__PURE__ */ jsxRuntime.jsx(Field, { label: "Task hash", value: quote.taskHash, copy: true }),
755
+ testnetWarning ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: warningStyle, children: testnetWarning }) : null,
756
+ showPaymentIntent && paymentIntent ? /* @__PURE__ */ jsxRuntime.jsxs("div", { style: intentStyle, children: [
757
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: intentHeaderStyle, children: [
758
+ /* @__PURE__ */ jsxRuntime.jsx("strong", { children: "Payment intent" }),
759
+ /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", style: copyButtonStyle, onClick: copyPaymentIntent, children: "Copy JSON" })
760
+ ] }),
761
+ /* @__PURE__ */ jsxRuntime.jsx("code", { style: intentCodeStyle, children: serializeSagePaymentIntent(paymentIntent) })
762
+ ] }) : null,
763
+ props.walletLauncher && paymentIntent ? /* @__PURE__ */ jsxRuntime.jsx(
764
+ "button",
765
+ {
766
+ type: "button",
767
+ onClick: launchWallet,
768
+ disabled: busy,
769
+ style: secondaryButtonStyle,
770
+ children: paymentInstructions?.walletLauncherLabel ?? "Open wallet flow"
771
+ }
772
+ ) : null,
773
+ /* @__PURE__ */ jsxRuntime.jsxs("label", { style: labelStyle2, children: [
774
+ paymentInstructions?.noteBoxLabel ?? "Note box id",
775
+ /* @__PURE__ */ jsxRuntime.jsx(
776
+ "input",
777
+ {
778
+ value: noteBoxId,
779
+ onChange: (e) => setNoteBoxId(e.currentTarget.value),
780
+ placeholder: "Paste 64-char Ergo box id",
781
+ style: inputStyle,
782
+ disabled: busy
783
+ }
784
+ )
785
+ ] }),
786
+ /* @__PURE__ */ jsxRuntime.jsx(
787
+ "button",
788
+ {
789
+ type: "button",
790
+ onClick: verifyAndContinue,
791
+ disabled: !noteBoxId.trim() || busy,
792
+ style: primaryButtonStyle,
793
+ children: phase === "verifying" ? "Verifying..." : "Verify payment"
794
+ }
795
+ )
796
+ ] }) : null,
797
+ receipt ? /* @__PURE__ */ jsxRuntime.jsxs("div", { style: receiptPanelStyle, children: [
798
+ /* @__PURE__ */ jsxRuntime.jsxs("a", { href: receipt.receiptUrl, target: "_blank", rel: "noopener noreferrer", style: receiptStyle, children: [
799
+ "Receipt: ",
800
+ shortId(receipt.receiptId),
801
+ receiptBundle ? ` \xB7 ${receiptBundle.completeness}` : ""
802
+ ] }),
803
+ /* @__PURE__ */ jsxRuntime.jsx("a", { href: receipt.receiptApiUrl, target: "_blank", rel: "noopener noreferrer", style: receiptApiStyle, children: "machine-readable JSON" })
804
+ ] }) : null,
805
+ error ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: errorStyle2, children: error }) : null,
806
+ /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: submit, style: formStyle, children: [
807
+ /* @__PURE__ */ jsxRuntime.jsx(
808
+ "input",
809
+ {
810
+ value: input,
811
+ onChange: (e) => setInput(e.currentTarget.value),
812
+ placeholder,
813
+ disabled: busy,
814
+ style: inputStyle
815
+ }
816
+ ),
817
+ /* @__PURE__ */ jsxRuntime.jsx("button", { type: "submit", disabled: !input.trim() || busy, style: sendButtonStyle, children: phase === "quoting" ? "Quoting..." : phase === "streaming" ? "Streaming..." : "Send" })
818
+ ] })
819
+ ] });
820
+ }
821
+ function Field({ label, value, copy = false }) {
822
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: fieldStyle, children: [
823
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: fieldLabelStyle, children: label }),
824
+ /* @__PURE__ */ jsxRuntime.jsx("code", { style: fieldValueStyle, children: value }),
825
+ copy ? /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", style: copyButtonStyle, onClick: () => copyText(value), children: "Copy" }) : null
826
+ ] });
827
+ }
828
+ async function copyText(value) {
829
+ if (typeof navigator !== "undefined" && navigator.clipboard) {
830
+ await navigator.clipboard.writeText(value);
831
+ }
832
+ }
833
+ function shortId(value) {
834
+ return value.length > 18 ? `${value.slice(0, 10)}...${value.slice(-8)}` : value;
835
+ }
836
+ function lastUserQuestion(messages) {
837
+ for (let i = messages.length - 1; i >= 0; i -= 1) {
838
+ const message = messages[i];
839
+ if (message?.role === "user" && message.content.trim()) return message.content.trim();
840
+ }
841
+ return null;
842
+ }
843
+ var rootStyle2 = {
844
+ background: "#070707",
845
+ color: "#f8fafc",
846
+ border: "1px solid rgba(255,255,255,.1)",
847
+ borderRadius: 8,
848
+ padding: 16,
849
+ fontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
850
+ boxSizing: "border-box",
851
+ width: "100%",
852
+ maxWidth: 520
853
+ };
854
+ var headerStyle2 = {
855
+ display: "flex",
856
+ justifyContent: "space-between",
857
+ alignItems: "flex-start",
858
+ gap: 12,
859
+ marginBottom: 12
860
+ };
861
+ var eyebrowStyle = {
862
+ color: "#fb923c",
863
+ fontSize: 11,
864
+ letterSpacing: ".16em",
865
+ textTransform: "uppercase",
866
+ marginBottom: 4
867
+ };
868
+ var titleStyle = {
869
+ fontSize: 18,
870
+ lineHeight: 1.2,
871
+ margin: 0
872
+ };
873
+ var badgeStyle = {
874
+ color: "#0f172a",
875
+ background: "#fdba74",
876
+ borderRadius: 4,
877
+ padding: "4px 7px",
878
+ fontSize: 10,
879
+ fontWeight: 800,
880
+ letterSpacing: ".12em"
881
+ };
882
+ var messagesStyle = {
883
+ minHeight: 180,
884
+ maxHeight: 360,
885
+ overflow: "auto",
886
+ display: "flex",
887
+ flexDirection: "column",
888
+ gap: 8,
889
+ padding: "12px 0"
890
+ };
891
+ var emptyStyle2 = {
892
+ color: "#94a3b8",
893
+ border: "1px dashed rgba(148,163,184,.28)",
894
+ borderRadius: 6,
895
+ padding: 12,
896
+ fontSize: 13
897
+ };
898
+ var bubbleBase = {
899
+ borderRadius: 6,
900
+ padding: "9px 10px",
901
+ fontSize: 14,
902
+ lineHeight: 1.45,
903
+ whiteSpace: "pre-wrap"
904
+ };
905
+ var userBubbleStyle = {
906
+ ...bubbleBase,
907
+ alignSelf: "flex-end",
908
+ maxWidth: "88%",
909
+ background: "#fb923c",
910
+ color: "#111827"
911
+ };
912
+ var assistantBubbleStyle = {
913
+ ...bubbleBase,
914
+ alignSelf: "flex-start",
915
+ maxWidth: "92%",
916
+ background: "rgba(255,255,255,.07)",
917
+ color: "#e5e7eb"
918
+ };
919
+ var paymentStyle = {
920
+ border: "1px solid rgba(251,146,60,.35)",
921
+ background: "rgba(251,146,60,.08)",
922
+ borderRadius: 8,
923
+ padding: 12,
924
+ display: "grid",
925
+ gap: 8
926
+ };
927
+ var paymentHeaderStyle = {
928
+ display: "flex",
929
+ justifyContent: "space-between",
930
+ color: "#fed7aa",
931
+ fontSize: 13
932
+ };
933
+ var helperStyle = {
934
+ color: "#fdba74",
935
+ fontSize: 12,
936
+ lineHeight: 1.45,
937
+ margin: 0
938
+ };
939
+ var inlineLinkStyle = {
940
+ color: "#67e8f9",
941
+ textDecoration: "none"
942
+ };
943
+ var labelStyle2 = {
944
+ display: "grid",
945
+ gap: 6,
946
+ color: "#cbd5e1",
947
+ fontSize: 12
948
+ };
949
+ var fieldStyle = {
950
+ display: "grid",
951
+ gridTemplateColumns: "minmax(70px, 88px) minmax(0, 1fr) auto",
952
+ alignItems: "center",
953
+ gap: 8,
954
+ fontSize: 12
955
+ };
956
+ var fieldLabelStyle = {
957
+ color: "#94a3b8"
958
+ };
959
+ var fieldValueStyle = {
960
+ color: "#f8fafc",
961
+ overflow: "hidden",
962
+ textOverflow: "ellipsis",
963
+ whiteSpace: "nowrap",
964
+ fontSize: 11
965
+ };
966
+ var copyButtonStyle = {
967
+ border: "1px solid rgba(255,255,255,.16)",
968
+ background: "rgba(255,255,255,.06)",
969
+ color: "#f8fafc",
970
+ borderRadius: 4,
971
+ padding: "4px 7px",
972
+ cursor: "pointer"
973
+ };
974
+ var formStyle = {
975
+ display: "grid",
976
+ gridTemplateColumns: "minmax(0, 1fr) auto",
977
+ gap: 8,
978
+ marginTop: 12
979
+ };
980
+ var inputStyle = {
981
+ width: "100%",
982
+ boxSizing: "border-box",
983
+ border: "1px solid rgba(255,255,255,.16)",
984
+ background: "#050505",
985
+ color: "#f8fafc",
986
+ borderRadius: 6,
987
+ padding: "10px 11px",
988
+ fontSize: 14
989
+ };
990
+ var sendButtonStyle = {
991
+ border: 0,
992
+ background: "#fb923c",
993
+ color: "#111827",
994
+ borderRadius: 6,
995
+ padding: "0 14px",
996
+ fontWeight: 800,
997
+ cursor: "pointer"
998
+ };
999
+ var primaryButtonStyle = {
1000
+ ...sendButtonStyle,
1001
+ padding: "10px 12px"
1002
+ };
1003
+ var secondaryButtonStyle = {
1004
+ border: "1px solid rgba(103,232,249,.28)",
1005
+ background: "rgba(103,232,249,.08)",
1006
+ color: "#cffafe",
1007
+ borderRadius: 6,
1008
+ padding: "10px 12px",
1009
+ fontWeight: 800,
1010
+ cursor: "pointer"
1011
+ };
1012
+ var warningStyle = {
1013
+ color: "#fde68a",
1014
+ background: "rgba(245,158,11,.1)",
1015
+ border: "1px solid rgba(245,158,11,.24)",
1016
+ borderRadius: 6,
1017
+ padding: "8px 9px",
1018
+ fontSize: 12,
1019
+ lineHeight: 1.45
1020
+ };
1021
+ var intentStyle = {
1022
+ border: "1px solid rgba(255,255,255,.12)",
1023
+ background: "rgba(255,255,255,.04)",
1024
+ borderRadius: 6,
1025
+ padding: 10,
1026
+ display: "grid",
1027
+ gap: 8
1028
+ };
1029
+ var intentHeaderStyle = {
1030
+ display: "flex",
1031
+ justifyContent: "space-between",
1032
+ alignItems: "center",
1033
+ gap: 8,
1034
+ color: "#e2e8f0",
1035
+ fontSize: 12
1036
+ };
1037
+ var intentCodeStyle = {
1038
+ display: "block",
1039
+ maxHeight: 130,
1040
+ overflow: "auto",
1041
+ whiteSpace: "pre-wrap",
1042
+ wordBreak: "break-word",
1043
+ color: "#cbd5e1",
1044
+ fontSize: 10,
1045
+ lineHeight: 1.45
1046
+ };
1047
+ var receiptStyle = {
1048
+ display: "inline-flex",
1049
+ color: "#67e8f9",
1050
+ fontSize: 13,
1051
+ textDecoration: "none"
1052
+ };
1053
+ var receiptPanelStyle = {
1054
+ display: "flex",
1055
+ alignItems: "center",
1056
+ flexWrap: "wrap",
1057
+ gap: 10,
1058
+ marginTop: 10,
1059
+ border: "1px solid rgba(103,232,249,.22)",
1060
+ background: "rgba(103,232,249,.07)",
1061
+ borderRadius: 6,
1062
+ padding: "9px 10px"
1063
+ };
1064
+ var receiptApiStyle = {
1065
+ color: "#cbd5e1",
1066
+ fontSize: 12,
1067
+ textDecoration: "none"
1068
+ };
1069
+ var errorStyle2 = {
1070
+ color: "#fecaca",
1071
+ background: "rgba(239,68,68,.12)",
1072
+ border: "1px solid rgba(239,68,68,.25)",
1073
+ borderRadius: 6,
1074
+ padding: 10,
1075
+ marginTop: 10,
1076
+ fontSize: 13
1077
+ };
296
1078
 
297
1079
  exports.DEFAULT_API_BASE = DEFAULT_API_BASE;
298
1080
  exports.DEFAULT_LIMIT = DEFAULT_LIMIT;
299
1081
  exports.DEFAULT_REFRESH_MS = DEFAULT_REFRESH_MS;
300
1082
  exports.SageActivityFeed = SageActivityFeed;
1083
+ exports.SagePaymentWidget = SagePaymentWidget;
301
1084
  //# sourceMappingURL=react.cjs.map
302
1085
  //# sourceMappingURL=react.cjs.map