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