@ergoblockchain/sage-widget 0.1.0 → 0.2.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 +111 -5
- package/dist/index.cjs +203 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -3
- package/dist/index.d.ts +28 -3
- package/dist/index.js +199 -4
- package/dist/index.js.map +1 -1
- package/dist/react.cjs +677 -11
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.cts +10 -3
- package/dist/react.d.ts +10 -3
- package/dist/react.js +678 -13
- package/dist/react.js.map +1 -1
- package/dist/types-yv1THHVz.d.cts +229 -0
- package/dist/types-yv1THHVz.d.ts +229 -0
- package/dist/vanilla.cjs +720 -6
- package/dist/vanilla.cjs.map +1 -1
- package/dist/vanilla.d.cts +20 -3
- package/dist/vanilla.d.ts +20 -3
- package/dist/vanilla.js +720 -7
- package/dist/vanilla.js.map +1 -1
- package/package.json +3 -2
- package/dist/types-uzMz6_mP.d.cts +0 -77
- package/dist/types-uzMz6_mP.d.ts +0 -77
package/dist/react.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState, useRef, useEffect } from 'react';
|
|
1
|
+
import { useState, useRef, useEffect, useMemo } from 'react';
|
|
2
2
|
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
3
3
|
|
|
4
4
|
// src/react/SageActivityFeed.tsx
|
|
@@ -12,13 +12,129 @@ var DEFAULT_REFRESH_MS = 6e4;
|
|
|
12
12
|
async function fetchSageActivity(opts = {}) {
|
|
13
13
|
const base = opts.apiBase ?? DEFAULT_API_BASE;
|
|
14
14
|
const limit = Math.min(Math.max(opts.limit ?? DEFAULT_LIMIT, 1), 25);
|
|
15
|
-
const url = `${base}/api/sage/activity?limit=${limit}`;
|
|
15
|
+
const url = `${trimSlash(base)}/api/sage/activity?limit=${limit}`;
|
|
16
16
|
const res = await fetch(url, { signal: opts.signal });
|
|
17
17
|
if (!res.ok) {
|
|
18
18
|
throw new Error(`sage activity ${res.status}`);
|
|
19
19
|
}
|
|
20
20
|
return await res.json();
|
|
21
21
|
}
|
|
22
|
+
async function fetchSageQuote(opts) {
|
|
23
|
+
const res = await fetch(`${apiBase(opts)}/api/sage/quote`, {
|
|
24
|
+
method: "POST",
|
|
25
|
+
headers: jsonHeaders(opts),
|
|
26
|
+
body: JSON.stringify({
|
|
27
|
+
question: opts.question,
|
|
28
|
+
history: opts.history ?? []
|
|
29
|
+
}),
|
|
30
|
+
signal: opts.signal
|
|
31
|
+
});
|
|
32
|
+
const body = await parseJson(res);
|
|
33
|
+
if (!res.ok) throw new Error(readError(body, `sage quote ${res.status}`));
|
|
34
|
+
return body;
|
|
35
|
+
}
|
|
36
|
+
async function verifySagePayment(opts) {
|
|
37
|
+
const res = await fetch(`${apiBase(opts)}/api/sage/verify-payment`, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: jsonHeaders(opts),
|
|
40
|
+
body: JSON.stringify({
|
|
41
|
+
quote: opts.quote,
|
|
42
|
+
question: opts.question,
|
|
43
|
+
noteBoxId: opts.noteBoxId
|
|
44
|
+
}),
|
|
45
|
+
signal: opts.signal
|
|
46
|
+
});
|
|
47
|
+
const body = await parseJson(res);
|
|
48
|
+
if (!res.ok) throw new Error(readError(body, `sage verify-payment ${res.status}`));
|
|
49
|
+
return body;
|
|
50
|
+
}
|
|
51
|
+
async function fetchSageReceipt(id, opts = {}) {
|
|
52
|
+
const res = await fetch(`${apiBase(opts)}/api/sage/receipt/${encodeURIComponent(id)}`, {
|
|
53
|
+
headers: requestHeaders(opts),
|
|
54
|
+
signal: opts.signal
|
|
55
|
+
});
|
|
56
|
+
const body = await parseJson(res);
|
|
57
|
+
if (!res.ok) throw new Error(readError(body, `sage receipt ${res.status}`));
|
|
58
|
+
return body;
|
|
59
|
+
}
|
|
60
|
+
async function streamSageChat(opts) {
|
|
61
|
+
const res = await fetch(`${apiBase(opts)}/api/sage/chat`, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: jsonHeaders(opts),
|
|
64
|
+
body: JSON.stringify({
|
|
65
|
+
messages: opts.messages,
|
|
66
|
+
...opts.paymentToken ? { paymentToken: opts.paymentToken } : {}
|
|
67
|
+
}),
|
|
68
|
+
signal: opts.signal
|
|
69
|
+
});
|
|
70
|
+
if (res.status === 402) {
|
|
71
|
+
const body = await parseJson(res);
|
|
72
|
+
return {
|
|
73
|
+
ok: false,
|
|
74
|
+
status: res.status,
|
|
75
|
+
text: "",
|
|
76
|
+
paymentRequired: body
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (!res.ok) {
|
|
80
|
+
const body = await parseJson(res);
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
status: res.status,
|
|
84
|
+
text: "",
|
|
85
|
+
error: readError(body, `sage chat ${res.status}`)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (!res.body) {
|
|
89
|
+
return { ok: false, status: res.status, text: "", error: "Sage chat stream missing body" };
|
|
90
|
+
}
|
|
91
|
+
const reader = res.body.getReader();
|
|
92
|
+
const decoder = new TextDecoder();
|
|
93
|
+
let buffer = "";
|
|
94
|
+
let text = "";
|
|
95
|
+
let tier;
|
|
96
|
+
while (true) {
|
|
97
|
+
const { value, done } = await reader.read();
|
|
98
|
+
if (done) break;
|
|
99
|
+
buffer += decoder.decode(value, { stream: true });
|
|
100
|
+
const parts = buffer.split("\n\n");
|
|
101
|
+
buffer = parts.pop() ?? "";
|
|
102
|
+
for (const part of parts) {
|
|
103
|
+
const event = parseSseEvent(part);
|
|
104
|
+
if (!event) continue;
|
|
105
|
+
if (event.type === "delta") text += event.text;
|
|
106
|
+
if (event.type === "tier") tier = event.tier;
|
|
107
|
+
opts.onEvent?.(event);
|
|
108
|
+
if (event.type === "error") {
|
|
109
|
+
return {
|
|
110
|
+
ok: false,
|
|
111
|
+
status: res.status,
|
|
112
|
+
text,
|
|
113
|
+
tier,
|
|
114
|
+
error: event.message
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (buffer.trim()) {
|
|
120
|
+
const event = parseSseEvent(buffer);
|
|
121
|
+
if (event) {
|
|
122
|
+
if (event.type === "delta") text += event.text;
|
|
123
|
+
if (event.type === "tier") tier = event.tier;
|
|
124
|
+
opts.onEvent?.(event);
|
|
125
|
+
if (event.type === "error") {
|
|
126
|
+
return {
|
|
127
|
+
ok: false,
|
|
128
|
+
status: res.status,
|
|
129
|
+
text,
|
|
130
|
+
tier,
|
|
131
|
+
error: event.message
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return { ok: true, status: res.status, text, tier };
|
|
137
|
+
}
|
|
22
138
|
function nanoToErg(nano) {
|
|
23
139
|
if (!nano || nano <= 0) return "0";
|
|
24
140
|
const erg = nano / 1e9;
|
|
@@ -35,16 +151,92 @@ function relativeTime(ms, now = Date.now()) {
|
|
|
35
151
|
const day = Math.floor(hr / 24);
|
|
36
152
|
return `${day}d ago`;
|
|
37
153
|
}
|
|
38
|
-
function receiptUrl(txId,
|
|
39
|
-
return `${
|
|
154
|
+
function receiptUrl(txId, apiBase2 = DEFAULT_API_BASE) {
|
|
155
|
+
return `${trimSlash(apiBase2)}/r/sage/${txId}`;
|
|
40
156
|
}
|
|
41
157
|
function explorerUrl(txId, network = "testnet") {
|
|
42
158
|
return network === "testnet" ? `https://testnet.ergoplatform.com/transactions/${txId}` : `https://explorer.ergoplatform.com/transactions/${txId}`;
|
|
43
159
|
}
|
|
160
|
+
function apiBase(opts) {
|
|
161
|
+
return trimSlash(opts.apiBase ?? DEFAULT_API_BASE);
|
|
162
|
+
}
|
|
163
|
+
function trimSlash(value) {
|
|
164
|
+
return value.replace(/\/+$/, "");
|
|
165
|
+
}
|
|
166
|
+
function requestHeaders(opts) {
|
|
167
|
+
return {
|
|
168
|
+
...opts.tenant?.id ? { "x-sage-tenant-id": opts.tenant.id } : {},
|
|
169
|
+
...opts.tenant?.headers ?? {},
|
|
170
|
+
...opts.headers ?? {}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function jsonHeaders(opts) {
|
|
174
|
+
return {
|
|
175
|
+
"content-type": "application/json",
|
|
176
|
+
...requestHeaders(opts)
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
async function parseJson(res) {
|
|
180
|
+
const text = await res.text();
|
|
181
|
+
if (!text) return null;
|
|
182
|
+
try {
|
|
183
|
+
return JSON.parse(text);
|
|
184
|
+
} catch {
|
|
185
|
+
return { error: text };
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function readError(body, fallback) {
|
|
189
|
+
if (body && typeof body === "object" && "error" in body) {
|
|
190
|
+
const error = body.error;
|
|
191
|
+
if (typeof error === "string") return error;
|
|
192
|
+
}
|
|
193
|
+
return fallback;
|
|
194
|
+
}
|
|
195
|
+
function parseSseEvent(raw) {
|
|
196
|
+
let eventName = "message";
|
|
197
|
+
let data = "";
|
|
198
|
+
for (const line of raw.split("\n")) {
|
|
199
|
+
if (line.startsWith("event:")) eventName = line.slice("event:".length).trim();
|
|
200
|
+
if (line.startsWith("data:")) data += line.slice("data:".length).trim();
|
|
201
|
+
}
|
|
202
|
+
if (!data) return null;
|
|
203
|
+
let parsed;
|
|
204
|
+
try {
|
|
205
|
+
parsed = JSON.parse(data);
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
if (eventName === "tier") {
|
|
210
|
+
const tier = parsed.tier === "premium" ? "premium" : "free";
|
|
211
|
+
return {
|
|
212
|
+
type: "tier",
|
|
213
|
+
tier,
|
|
214
|
+
...typeof parsed.model === "string" ? { model: parsed.model } : {}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (eventName === "delta" && typeof parsed.text === "string") {
|
|
218
|
+
return { type: "delta", text: parsed.text };
|
|
219
|
+
}
|
|
220
|
+
if (eventName === "done") {
|
|
221
|
+
return {
|
|
222
|
+
type: "done",
|
|
223
|
+
...typeof parsed.stopReason === "string" ? { stopReason: parsed.stopReason } : {},
|
|
224
|
+
...typeof parsed.inputTokens === "number" ? { inputTokens: parsed.inputTokens } : {},
|
|
225
|
+
...typeof parsed.outputTokens === "number" ? { outputTokens: parsed.outputTokens } : {}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (eventName === "error") {
|
|
229
|
+
return {
|
|
230
|
+
type: "error",
|
|
231
|
+
message: typeof parsed.message === "string" ? parsed.message : "Sage stream error"
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
44
236
|
var ONE_MIN = 6e4;
|
|
45
237
|
function SageActivityFeed(props) {
|
|
46
238
|
const {
|
|
47
|
-
apiBase,
|
|
239
|
+
apiBase: apiBase2,
|
|
48
240
|
limit = DEFAULT_LIMIT,
|
|
49
241
|
refreshMs = DEFAULT_REFRESH_MS,
|
|
50
242
|
onUpdate,
|
|
@@ -71,7 +263,7 @@ function SageActivityFeed(props) {
|
|
|
71
263
|
async function load() {
|
|
72
264
|
try {
|
|
73
265
|
const data = await fetchSageActivity({
|
|
74
|
-
apiBase,
|
|
266
|
+
apiBase: apiBase2,
|
|
75
267
|
limit,
|
|
76
268
|
signal: abort.signal
|
|
77
269
|
});
|
|
@@ -96,7 +288,7 @@ function SageActivityFeed(props) {
|
|
|
96
288
|
if (pollId) clearInterval(pollId);
|
|
97
289
|
clearInterval(tickId);
|
|
98
290
|
};
|
|
99
|
-
}, [
|
|
291
|
+
}, [apiBase2, limit, refreshMs]);
|
|
100
292
|
if (children) {
|
|
101
293
|
return /* @__PURE__ */ jsx(Fragment, { children: children({ loading, response, error }) });
|
|
102
294
|
}
|
|
@@ -108,7 +300,7 @@ function SageActivityFeed(props) {
|
|
|
108
300
|
loading,
|
|
109
301
|
events: response?.events ?? [],
|
|
110
302
|
network: response?.network ?? "testnet",
|
|
111
|
-
apiBase,
|
|
303
|
+
apiBase: apiBase2,
|
|
112
304
|
now
|
|
113
305
|
}
|
|
114
306
|
),
|
|
@@ -138,22 +330,22 @@ function List({
|
|
|
138
330
|
loading,
|
|
139
331
|
events,
|
|
140
332
|
network,
|
|
141
|
-
apiBase,
|
|
333
|
+
apiBase: apiBase2,
|
|
142
334
|
now
|
|
143
335
|
}) {
|
|
144
336
|
if (loading) return /* @__PURE__ */ jsx("div", { style: emptyStyle, children: "Loading\u2026" });
|
|
145
337
|
if (events.length === 0)
|
|
146
338
|
return /* @__PURE__ */ jsx("div", { style: emptyStyle, children: "No activity yet \u2014 be the first to ask Sage a paid query." });
|
|
147
|
-
return /* @__PURE__ */ jsx("div", { children: events.map((evt) => /* @__PURE__ */ jsx(Row, { evt, network, apiBase, now }, evt.txId)) });
|
|
339
|
+
return /* @__PURE__ */ jsx("div", { children: events.map((evt) => /* @__PURE__ */ jsx(Row, { evt, network, apiBase: apiBase2, now }, evt.txId)) });
|
|
148
340
|
}
|
|
149
341
|
function Row({
|
|
150
342
|
evt,
|
|
151
343
|
network,
|
|
152
|
-
apiBase,
|
|
344
|
+
apiBase: apiBase2,
|
|
153
345
|
now
|
|
154
346
|
}) {
|
|
155
347
|
const isSettle = evt.type === "settlement";
|
|
156
|
-
const href = isSettle ? receiptUrl(evt.txId,
|
|
348
|
+
const href = isSettle ? receiptUrl(evt.txId, apiBase2) : explorerUrl(evt.txId, network);
|
|
157
349
|
const amount = isSettle ? evt.paymentNanoErg ?? evt.inflowNanoErg : evt.inflowNanoErg;
|
|
158
350
|
const chipStyle = isSettle ? chipSettleStyle : chipIssueStyle;
|
|
159
351
|
const chipText = isSettle ? "Settled" : evt.type === "issuance" ? "Issued" : "Transfer";
|
|
@@ -291,7 +483,480 @@ var errorStyle = {
|
|
|
291
483
|
...emptyStyle,
|
|
292
484
|
color: "#f87171"
|
|
293
485
|
};
|
|
486
|
+
function SagePaymentWidget(props) {
|
|
487
|
+
const {
|
|
488
|
+
apiBase: apiBase2 = DEFAULT_API_BASE,
|
|
489
|
+
tenant,
|
|
490
|
+
initialMessages = [],
|
|
491
|
+
placeholder = "Ask Sage about Ergo or agent payments...",
|
|
492
|
+
paymentInstructions,
|
|
493
|
+
className,
|
|
494
|
+
style,
|
|
495
|
+
title = "Ask Sage"
|
|
496
|
+
} = props;
|
|
497
|
+
const [messages, setMessages] = useState(initialMessages);
|
|
498
|
+
const [input, setInput] = useState("");
|
|
499
|
+
const [phase, setPhase] = useState("idle");
|
|
500
|
+
const [quoteResponse, setQuoteResponse] = useState(null);
|
|
501
|
+
const [activeQuestion, setActiveQuestion] = useState("");
|
|
502
|
+
const [noteBoxId, setNoteBoxId] = useState("");
|
|
503
|
+
const [receipt, setReceipt] = useState(null);
|
|
504
|
+
const [receiptBundle, setReceiptBundle] = useState(null);
|
|
505
|
+
const [error, setError] = useState(null);
|
|
506
|
+
const [tier, setTier] = useState(null);
|
|
507
|
+
const mountedRef = useRef(true);
|
|
508
|
+
useEffect(() => {
|
|
509
|
+
mountedRef.current = true;
|
|
510
|
+
return () => {
|
|
511
|
+
mountedRef.current = false;
|
|
512
|
+
};
|
|
513
|
+
}, []);
|
|
514
|
+
const busy = phase === "quoting" || phase === "verifying" || phase === "streaming";
|
|
515
|
+
const apiOpts = useMemo(() => ({ apiBase: apiBase2, tenant }), [apiBase2, tenant]);
|
|
516
|
+
async function submit(e) {
|
|
517
|
+
e.preventDefault();
|
|
518
|
+
const question = input.trim();
|
|
519
|
+
if (!question || busy) return;
|
|
520
|
+
const userMessage = { role: "user", content: question };
|
|
521
|
+
const nextMessages = [...messages, userMessage];
|
|
522
|
+
setMessages(nextMessages);
|
|
523
|
+
props.onMessage?.(userMessage, nextMessages);
|
|
524
|
+
setInput("");
|
|
525
|
+
setError(null);
|
|
526
|
+
setReceipt(null);
|
|
527
|
+
setReceiptBundle(null);
|
|
528
|
+
setQuoteResponse(null);
|
|
529
|
+
setActiveQuestion(question);
|
|
530
|
+
setNoteBoxId("");
|
|
531
|
+
setTier(null);
|
|
532
|
+
transition("quoting");
|
|
533
|
+
try {
|
|
534
|
+
const quote2 = await fetchSageQuote({
|
|
535
|
+
...apiOpts,
|
|
536
|
+
question,
|
|
537
|
+
history: messages
|
|
538
|
+
});
|
|
539
|
+
props.onQuote?.(quote2);
|
|
540
|
+
if (quote2.premium) {
|
|
541
|
+
if (!quote2.quote) throw new Error("Sage marked this question premium but did not return a quote.");
|
|
542
|
+
setQuoteResponse(quote2);
|
|
543
|
+
transition("payment_required", { quote: quote2.quote });
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
await streamAnswer(nextMessages, void 0, question);
|
|
547
|
+
} catch (err) {
|
|
548
|
+
fail(err);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
async function verifyAndContinue() {
|
|
552
|
+
const quote2 = quoteResponse?.quote;
|
|
553
|
+
const note = noteBoxId.trim();
|
|
554
|
+
if (!quote2 || !activeQuestion || !note || busy) return;
|
|
555
|
+
setError(null);
|
|
556
|
+
transition("verifying");
|
|
557
|
+
let verified;
|
|
558
|
+
try {
|
|
559
|
+
verified = await verifySagePayment({
|
|
560
|
+
...apiOpts,
|
|
561
|
+
quote: quote2,
|
|
562
|
+
question: activeQuestion,
|
|
563
|
+
noteBoxId: note
|
|
564
|
+
});
|
|
565
|
+
} catch (err) {
|
|
566
|
+
transition("payment_required");
|
|
567
|
+
fail(err, false);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
if (!mountedRef.current) return;
|
|
571
|
+
setReceipt(verified);
|
|
572
|
+
props.onReceipt?.(verified);
|
|
573
|
+
setQuoteResponse(null);
|
|
574
|
+
setNoteBoxId("");
|
|
575
|
+
transition("streaming", { quote: null, receipt: verified });
|
|
576
|
+
try {
|
|
577
|
+
const bundle = await fetchSageReceipt(verified.receiptId, apiOpts);
|
|
578
|
+
if (!mountedRef.current) return;
|
|
579
|
+
setReceiptBundle(bundle);
|
|
580
|
+
props.onReceiptBundle?.(bundle);
|
|
581
|
+
emitStatus("streaming", { quote: null, receipt: verified, receiptBundle: bundle });
|
|
582
|
+
} catch (bundleErr) {
|
|
583
|
+
props.onError?.(bundleErr);
|
|
584
|
+
}
|
|
585
|
+
try {
|
|
586
|
+
await streamAnswer(messages, verified.paymentToken, activeQuestion);
|
|
587
|
+
} catch (err) {
|
|
588
|
+
fail(err);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
async function streamAnswer(baseMessages, paymentToken, fallbackQuestion) {
|
|
592
|
+
transition("streaming");
|
|
593
|
+
let text = "";
|
|
594
|
+
const placeholderMessage = { role: "assistant", content: "" };
|
|
595
|
+
setMessages([...baseMessages, placeholderMessage]);
|
|
596
|
+
const result = await streamSageChat({
|
|
597
|
+
...apiOpts,
|
|
598
|
+
messages: baseMessages,
|
|
599
|
+
paymentToken,
|
|
600
|
+
onEvent(event) {
|
|
601
|
+
if (!mountedRef.current) return;
|
|
602
|
+
if (event.type === "tier") {
|
|
603
|
+
setTier(event.tier);
|
|
604
|
+
props.onTier?.(event.tier);
|
|
605
|
+
emitStatus("streaming", { tier: event.tier, messages: baseMessages });
|
|
606
|
+
}
|
|
607
|
+
if (event.type === "delta") {
|
|
608
|
+
text += event.text;
|
|
609
|
+
setMessages([...baseMessages, { role: "assistant", content: text }]);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
if (!result.ok) {
|
|
614
|
+
if (result.paymentRequired) {
|
|
615
|
+
const question = fallbackQuestion ?? lastUserQuestion(baseMessages) ?? activeQuestion;
|
|
616
|
+
const quote2 = await fetchSageQuote({
|
|
617
|
+
...apiOpts,
|
|
618
|
+
question,
|
|
619
|
+
history: baseMessages.slice(0, -1)
|
|
620
|
+
});
|
|
621
|
+
if (!mountedRef.current) return;
|
|
622
|
+
props.onQuote?.(quote2);
|
|
623
|
+
setQuoteResponse(quote2);
|
|
624
|
+
setActiveQuestion(question);
|
|
625
|
+
transition("payment_required", { quote: quote2.quote ?? null });
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
throw new Error(result.error ?? "Sage chat failed.");
|
|
629
|
+
}
|
|
630
|
+
if (!mountedRef.current) return;
|
|
631
|
+
if (result.text && result.text !== text) {
|
|
632
|
+
setMessages([...baseMessages, { role: "assistant", content: result.text }]);
|
|
633
|
+
}
|
|
634
|
+
transition("idle");
|
|
635
|
+
}
|
|
636
|
+
function fail(err, setErrorPhase = true) {
|
|
637
|
+
const message = err instanceof Error ? err.message : "Sage request failed.";
|
|
638
|
+
setError(message);
|
|
639
|
+
if (setErrorPhase) transition("error", { error: message });
|
|
640
|
+
else emitStatus(phase, { error: message });
|
|
641
|
+
props.onError?.(err);
|
|
642
|
+
}
|
|
643
|
+
function transition(next, overrides = {}) {
|
|
644
|
+
setPhase(next);
|
|
645
|
+
props.onPhase?.(next);
|
|
646
|
+
emitStatus(next, overrides);
|
|
647
|
+
}
|
|
648
|
+
function emitStatus(nextPhase, overrides = {}) {
|
|
649
|
+
const has = (key) => Object.prototype.hasOwnProperty.call(overrides, key);
|
|
650
|
+
props.onStatus?.({
|
|
651
|
+
phase: nextPhase,
|
|
652
|
+
tier: has("tier") ? overrides.tier ?? null : tier,
|
|
653
|
+
quote: has("quote") ? overrides.quote ?? null : quoteResponse?.quote ?? null,
|
|
654
|
+
receipt: has("receipt") ? overrides.receipt ?? null : receipt,
|
|
655
|
+
receiptBundle: has("receiptBundle") ? overrides.receiptBundle ?? null : receiptBundle,
|
|
656
|
+
error: has("error") ? overrides.error ?? null : error,
|
|
657
|
+
messages: overrides.messages ?? messages,
|
|
658
|
+
activeQuestion: has("activeQuestion") ? overrides.activeQuestion ?? null : activeQuestion || null
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
const quote = quoteResponse?.quote;
|
|
662
|
+
const showPaymentPanel = quote && !receipt;
|
|
663
|
+
return /* @__PURE__ */ jsxs("section", { className, style: { ...rootStyle2, ...style }, children: [
|
|
664
|
+
/* @__PURE__ */ jsxs("header", { style: headerStyle2, children: [
|
|
665
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
666
|
+
/* @__PURE__ */ jsx("div", { style: eyebrowStyle, children: tenant?.label ?? "Ergo agent economy" }),
|
|
667
|
+
/* @__PURE__ */ jsx("h2", { style: titleStyle, children: title })
|
|
668
|
+
] }),
|
|
669
|
+
/* @__PURE__ */ jsx("span", { style: badgeStyle, children: tier ? tier.toUpperCase() : "SAGE" })
|
|
670
|
+
] }),
|
|
671
|
+
/* @__PURE__ */ jsx("div", { style: messagesStyle, "aria-live": "polite", children: messages.length === 0 ? /* @__PURE__ */ jsx("div", { style: emptyStyle2, children: "Free questions answer immediately. Premium questions return an Accord Note quote." }) : messages.map((message, index) => /* @__PURE__ */ jsx(
|
|
672
|
+
"div",
|
|
673
|
+
{
|
|
674
|
+
style: message.role === "user" ? userBubbleStyle : assistantBubbleStyle,
|
|
675
|
+
children: message.content || (message.role === "assistant" && phase === "streaming" ? "Thinking..." : "")
|
|
676
|
+
},
|
|
677
|
+
`${message.role}-${index}`
|
|
678
|
+
)) }),
|
|
679
|
+
showPaymentPanel ? /* @__PURE__ */ jsxs("div", { style: paymentStyle, children: [
|
|
680
|
+
/* @__PURE__ */ jsxs("div", { style: paymentHeaderStyle, children: [
|
|
681
|
+
/* @__PURE__ */ jsx("strong", { children: "Payment required" }),
|
|
682
|
+
/* @__PURE__ */ jsxs("span", { children: [
|
|
683
|
+
quote.price,
|
|
684
|
+
" ERG testnet"
|
|
685
|
+
] })
|
|
686
|
+
] }),
|
|
687
|
+
/* @__PURE__ */ jsxs("p", { style: helperStyle, children: [
|
|
688
|
+
paymentInstructions?.helperText ?? "Issue an Ergo testnet Note for this quote, then paste the created Note box id.",
|
|
689
|
+
paymentInstructions?.walletUrl ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
690
|
+
" ",
|
|
691
|
+
/* @__PURE__ */ jsx("a", { href: paymentInstructions.walletUrl, target: "_blank", rel: "noopener noreferrer", style: inlineLinkStyle, children: "Wallet guide" })
|
|
692
|
+
] }) : null
|
|
693
|
+
] }),
|
|
694
|
+
/* @__PURE__ */ jsx(Field, { label: "Quote", value: quote.quoteId }),
|
|
695
|
+
/* @__PURE__ */ jsx(Field, { label: "Receiver", value: quote.receiverAddress, copy: true }),
|
|
696
|
+
/* @__PURE__ */ jsx(Field, { label: "Reserve box", value: quote.reserveBoxId, copy: true }),
|
|
697
|
+
/* @__PURE__ */ jsx(Field, { label: "Task hash", value: quote.taskHash, copy: true }),
|
|
698
|
+
/* @__PURE__ */ jsxs("label", { style: labelStyle2, children: [
|
|
699
|
+
paymentInstructions?.noteBoxLabel ?? "Note box id",
|
|
700
|
+
/* @__PURE__ */ jsx(
|
|
701
|
+
"input",
|
|
702
|
+
{
|
|
703
|
+
value: noteBoxId,
|
|
704
|
+
onChange: (e) => setNoteBoxId(e.currentTarget.value),
|
|
705
|
+
placeholder: "Paste 64-char Ergo box id",
|
|
706
|
+
style: inputStyle,
|
|
707
|
+
disabled: busy
|
|
708
|
+
}
|
|
709
|
+
)
|
|
710
|
+
] }),
|
|
711
|
+
/* @__PURE__ */ jsx(
|
|
712
|
+
"button",
|
|
713
|
+
{
|
|
714
|
+
type: "button",
|
|
715
|
+
onClick: verifyAndContinue,
|
|
716
|
+
disabled: !noteBoxId.trim() || busy,
|
|
717
|
+
style: primaryButtonStyle,
|
|
718
|
+
children: phase === "verifying" ? "Verifying..." : "Verify payment"
|
|
719
|
+
}
|
|
720
|
+
)
|
|
721
|
+
] }) : null,
|
|
722
|
+
receipt ? /* @__PURE__ */ jsxs("div", { style: receiptPanelStyle, children: [
|
|
723
|
+
/* @__PURE__ */ jsxs("a", { href: receipt.receiptUrl, target: "_blank", rel: "noopener noreferrer", style: receiptStyle, children: [
|
|
724
|
+
"Receipt: ",
|
|
725
|
+
shortId(receipt.receiptId),
|
|
726
|
+
receiptBundle ? ` \xB7 ${receiptBundle.completeness}` : ""
|
|
727
|
+
] }),
|
|
728
|
+
/* @__PURE__ */ jsx("a", { href: receipt.receiptApiUrl, target: "_blank", rel: "noopener noreferrer", style: receiptApiStyle, children: "machine-readable JSON" })
|
|
729
|
+
] }) : null,
|
|
730
|
+
error ? /* @__PURE__ */ jsx("div", { style: errorStyle2, children: error }) : null,
|
|
731
|
+
/* @__PURE__ */ jsxs("form", { onSubmit: submit, style: formStyle, children: [
|
|
732
|
+
/* @__PURE__ */ jsx(
|
|
733
|
+
"input",
|
|
734
|
+
{
|
|
735
|
+
value: input,
|
|
736
|
+
onChange: (e) => setInput(e.currentTarget.value),
|
|
737
|
+
placeholder,
|
|
738
|
+
disabled: busy,
|
|
739
|
+
style: inputStyle
|
|
740
|
+
}
|
|
741
|
+
),
|
|
742
|
+
/* @__PURE__ */ jsx("button", { type: "submit", disabled: !input.trim() || busy, style: sendButtonStyle, children: phase === "quoting" ? "Quoting..." : phase === "streaming" ? "Streaming..." : "Send" })
|
|
743
|
+
] })
|
|
744
|
+
] });
|
|
745
|
+
}
|
|
746
|
+
function Field({ label, value, copy = false }) {
|
|
747
|
+
return /* @__PURE__ */ jsxs("div", { style: fieldStyle, children: [
|
|
748
|
+
/* @__PURE__ */ jsx("span", { style: fieldLabelStyle, children: label }),
|
|
749
|
+
/* @__PURE__ */ jsx("code", { style: fieldValueStyle, children: value }),
|
|
750
|
+
copy ? /* @__PURE__ */ jsx("button", { type: "button", style: copyButtonStyle, onClick: () => copyText(value), children: "Copy" }) : null
|
|
751
|
+
] });
|
|
752
|
+
}
|
|
753
|
+
function copyText(value) {
|
|
754
|
+
if (typeof navigator !== "undefined" && navigator.clipboard) {
|
|
755
|
+
void navigator.clipboard.writeText(value);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
function shortId(value) {
|
|
759
|
+
return value.length > 18 ? `${value.slice(0, 10)}...${value.slice(-8)}` : value;
|
|
760
|
+
}
|
|
761
|
+
function lastUserQuestion(messages) {
|
|
762
|
+
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
|
763
|
+
const message = messages[i];
|
|
764
|
+
if (message?.role === "user" && message.content.trim()) return message.content.trim();
|
|
765
|
+
}
|
|
766
|
+
return null;
|
|
767
|
+
}
|
|
768
|
+
var rootStyle2 = {
|
|
769
|
+
background: "#070707",
|
|
770
|
+
color: "#f8fafc",
|
|
771
|
+
border: "1px solid rgba(255,255,255,.1)",
|
|
772
|
+
borderRadius: 8,
|
|
773
|
+
padding: 16,
|
|
774
|
+
fontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
|
|
775
|
+
boxSizing: "border-box",
|
|
776
|
+
width: "100%",
|
|
777
|
+
maxWidth: 520
|
|
778
|
+
};
|
|
779
|
+
var headerStyle2 = {
|
|
780
|
+
display: "flex",
|
|
781
|
+
justifyContent: "space-between",
|
|
782
|
+
alignItems: "flex-start",
|
|
783
|
+
gap: 12,
|
|
784
|
+
marginBottom: 12
|
|
785
|
+
};
|
|
786
|
+
var eyebrowStyle = {
|
|
787
|
+
color: "#fb923c",
|
|
788
|
+
fontSize: 11,
|
|
789
|
+
letterSpacing: ".16em",
|
|
790
|
+
textTransform: "uppercase",
|
|
791
|
+
marginBottom: 4
|
|
792
|
+
};
|
|
793
|
+
var titleStyle = {
|
|
794
|
+
fontSize: 18,
|
|
795
|
+
lineHeight: 1.2,
|
|
796
|
+
margin: 0
|
|
797
|
+
};
|
|
798
|
+
var badgeStyle = {
|
|
799
|
+
color: "#0f172a",
|
|
800
|
+
background: "#fdba74",
|
|
801
|
+
borderRadius: 4,
|
|
802
|
+
padding: "4px 7px",
|
|
803
|
+
fontSize: 10,
|
|
804
|
+
fontWeight: 800,
|
|
805
|
+
letterSpacing: ".12em"
|
|
806
|
+
};
|
|
807
|
+
var messagesStyle = {
|
|
808
|
+
minHeight: 180,
|
|
809
|
+
maxHeight: 360,
|
|
810
|
+
overflow: "auto",
|
|
811
|
+
display: "flex",
|
|
812
|
+
flexDirection: "column",
|
|
813
|
+
gap: 8,
|
|
814
|
+
padding: "12px 0"
|
|
815
|
+
};
|
|
816
|
+
var emptyStyle2 = {
|
|
817
|
+
color: "#94a3b8",
|
|
818
|
+
border: "1px dashed rgba(148,163,184,.28)",
|
|
819
|
+
borderRadius: 6,
|
|
820
|
+
padding: 12,
|
|
821
|
+
fontSize: 13
|
|
822
|
+
};
|
|
823
|
+
var bubbleBase = {
|
|
824
|
+
borderRadius: 6,
|
|
825
|
+
padding: "9px 10px",
|
|
826
|
+
fontSize: 14,
|
|
827
|
+
lineHeight: 1.45,
|
|
828
|
+
whiteSpace: "pre-wrap"
|
|
829
|
+
};
|
|
830
|
+
var userBubbleStyle = {
|
|
831
|
+
...bubbleBase,
|
|
832
|
+
alignSelf: "flex-end",
|
|
833
|
+
maxWidth: "88%",
|
|
834
|
+
background: "#fb923c",
|
|
835
|
+
color: "#111827"
|
|
836
|
+
};
|
|
837
|
+
var assistantBubbleStyle = {
|
|
838
|
+
...bubbleBase,
|
|
839
|
+
alignSelf: "flex-start",
|
|
840
|
+
maxWidth: "92%",
|
|
841
|
+
background: "rgba(255,255,255,.07)",
|
|
842
|
+
color: "#e5e7eb"
|
|
843
|
+
};
|
|
844
|
+
var paymentStyle = {
|
|
845
|
+
border: "1px solid rgba(251,146,60,.35)",
|
|
846
|
+
background: "rgba(251,146,60,.08)",
|
|
847
|
+
borderRadius: 8,
|
|
848
|
+
padding: 12,
|
|
849
|
+
display: "grid",
|
|
850
|
+
gap: 8
|
|
851
|
+
};
|
|
852
|
+
var paymentHeaderStyle = {
|
|
853
|
+
display: "flex",
|
|
854
|
+
justifyContent: "space-between",
|
|
855
|
+
color: "#fed7aa",
|
|
856
|
+
fontSize: 13
|
|
857
|
+
};
|
|
858
|
+
var helperStyle = {
|
|
859
|
+
color: "#fdba74",
|
|
860
|
+
fontSize: 12,
|
|
861
|
+
lineHeight: 1.45,
|
|
862
|
+
margin: 0
|
|
863
|
+
};
|
|
864
|
+
var inlineLinkStyle = {
|
|
865
|
+
color: "#67e8f9",
|
|
866
|
+
textDecoration: "none"
|
|
867
|
+
};
|
|
868
|
+
var labelStyle2 = {
|
|
869
|
+
display: "grid",
|
|
870
|
+
gap: 6,
|
|
871
|
+
color: "#cbd5e1",
|
|
872
|
+
fontSize: 12
|
|
873
|
+
};
|
|
874
|
+
var fieldStyle = {
|
|
875
|
+
display: "grid",
|
|
876
|
+
gridTemplateColumns: "minmax(70px, 88px) minmax(0, 1fr) auto",
|
|
877
|
+
alignItems: "center",
|
|
878
|
+
gap: 8,
|
|
879
|
+
fontSize: 12
|
|
880
|
+
};
|
|
881
|
+
var fieldLabelStyle = {
|
|
882
|
+
color: "#94a3b8"
|
|
883
|
+
};
|
|
884
|
+
var fieldValueStyle = {
|
|
885
|
+
color: "#f8fafc",
|
|
886
|
+
overflow: "hidden",
|
|
887
|
+
textOverflow: "ellipsis",
|
|
888
|
+
whiteSpace: "nowrap",
|
|
889
|
+
fontSize: 11
|
|
890
|
+
};
|
|
891
|
+
var copyButtonStyle = {
|
|
892
|
+
border: "1px solid rgba(255,255,255,.16)",
|
|
893
|
+
background: "rgba(255,255,255,.06)",
|
|
894
|
+
color: "#f8fafc",
|
|
895
|
+
borderRadius: 4,
|
|
896
|
+
padding: "4px 7px",
|
|
897
|
+
cursor: "pointer"
|
|
898
|
+
};
|
|
899
|
+
var formStyle = {
|
|
900
|
+
display: "grid",
|
|
901
|
+
gridTemplateColumns: "minmax(0, 1fr) auto",
|
|
902
|
+
gap: 8,
|
|
903
|
+
marginTop: 12
|
|
904
|
+
};
|
|
905
|
+
var inputStyle = {
|
|
906
|
+
width: "100%",
|
|
907
|
+
boxSizing: "border-box",
|
|
908
|
+
border: "1px solid rgba(255,255,255,.16)",
|
|
909
|
+
background: "#050505",
|
|
910
|
+
color: "#f8fafc",
|
|
911
|
+
borderRadius: 6,
|
|
912
|
+
padding: "10px 11px",
|
|
913
|
+
fontSize: 14
|
|
914
|
+
};
|
|
915
|
+
var sendButtonStyle = {
|
|
916
|
+
border: 0,
|
|
917
|
+
background: "#fb923c",
|
|
918
|
+
color: "#111827",
|
|
919
|
+
borderRadius: 6,
|
|
920
|
+
padding: "0 14px",
|
|
921
|
+
fontWeight: 800,
|
|
922
|
+
cursor: "pointer"
|
|
923
|
+
};
|
|
924
|
+
var primaryButtonStyle = {
|
|
925
|
+
...sendButtonStyle,
|
|
926
|
+
padding: "10px 12px"
|
|
927
|
+
};
|
|
928
|
+
var receiptStyle = {
|
|
929
|
+
display: "inline-flex",
|
|
930
|
+
color: "#67e8f9",
|
|
931
|
+
fontSize: 13,
|
|
932
|
+
textDecoration: "none"
|
|
933
|
+
};
|
|
934
|
+
var receiptPanelStyle = {
|
|
935
|
+
display: "flex",
|
|
936
|
+
alignItems: "center",
|
|
937
|
+
flexWrap: "wrap",
|
|
938
|
+
gap: 10,
|
|
939
|
+
marginTop: 10,
|
|
940
|
+
border: "1px solid rgba(103,232,249,.22)",
|
|
941
|
+
background: "rgba(103,232,249,.07)",
|
|
942
|
+
borderRadius: 6,
|
|
943
|
+
padding: "9px 10px"
|
|
944
|
+
};
|
|
945
|
+
var receiptApiStyle = {
|
|
946
|
+
color: "#cbd5e1",
|
|
947
|
+
fontSize: 12,
|
|
948
|
+
textDecoration: "none"
|
|
949
|
+
};
|
|
950
|
+
var errorStyle2 = {
|
|
951
|
+
color: "#fecaca",
|
|
952
|
+
background: "rgba(239,68,68,.12)",
|
|
953
|
+
border: "1px solid rgba(239,68,68,.25)",
|
|
954
|
+
borderRadius: 6,
|
|
955
|
+
padding: 10,
|
|
956
|
+
marginTop: 10,
|
|
957
|
+
fontSize: 13
|
|
958
|
+
};
|
|
294
959
|
|
|
295
|
-
export { DEFAULT_API_BASE, DEFAULT_LIMIT, DEFAULT_REFRESH_MS, SageActivityFeed };
|
|
960
|
+
export { DEFAULT_API_BASE, DEFAULT_LIMIT, DEFAULT_REFRESH_MS, SageActivityFeed, SagePaymentWidget };
|
|
296
961
|
//# sourceMappingURL=react.js.map
|
|
297
962
|
//# sourceMappingURL=react.js.map
|