ain-ui 0.1.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/LICENSE +21 -0
- package/README.md +107 -0
- package/dist/agui.d.ts +11 -0
- package/dist/agui.js +11 -0
- package/dist/ainui.d.ts +173 -0
- package/dist/ainui.js +462 -0
- package/dist/basic.d.ts +92 -0
- package/dist/basic.js +247 -0
- package/dist/catalog.json +2268 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/payment.d.ts +13 -0
- package/dist/payment.js +23 -0
- package/dist/react.d.ts +13 -0
- package/dist/react.js +173 -0
- package/dist/renderer.d.ts +34 -0
- package/dist/renderer.js +602 -0
- package/dist/styles.css +6 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +1 -0
- package/dist/x402.d.ts +8 -0
- package/dist/x402.js +16 -0
- package/package.json +74 -0
package/dist/renderer.js
ADDED
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal, dependency-free A2UI v0.9 renderer for the basic-catalog subset
|
|
3
|
+
* aindrive emits (shared/a2ui/index.ts): Column, Row, List, Card, Text, Button,
|
|
4
|
+
* TextField, Image, Divider, Icon — with JSON-Pointer data binding, templated
|
|
5
|
+
* children and action events.
|
|
6
|
+
*
|
|
7
|
+
* Used by the MCP Apps view (app/mcp/ui — inlined into the sandboxed iframe)
|
|
8
|
+
* and the /docs playground. Third parties should use an official renderer
|
|
9
|
+
* (@a2ui/lit, @a2ui/react, CopilotKit); this one exists so aindrive's own
|
|
10
|
+
* surfaces render anywhere without a build step.
|
|
11
|
+
*
|
|
12
|
+
* AINUI (docs/AINUI.md, catalog AINUI_CATALOG in ./ainui.ts) is understood too:
|
|
13
|
+
* Grid, Tile, FileView, Breadcrumbs, Segmented, Button `tone`/`confirm`,
|
|
14
|
+
* Text `mono`, TextField `longText`, and `{$asset}` references, resolved by
|
|
15
|
+
* `opts.resolveAsset(asset)` or — by default — through aindrive's own routes
|
|
16
|
+
* (`{assetBase}/api/drives/{drive_id}/fs/thumbnail|stream?path=…`), which only
|
|
17
|
+
* work where the viewer has aindrive's session. FileView's download link asks
|
|
18
|
+
* `opts.resolveAsset({...asset, variant: "original"}, { download: true })`
|
|
19
|
+
* (default: `fs/download`). Unknown components draw their children, else nothing.
|
|
20
|
+
*
|
|
21
|
+
* Plain ES module, browser-only APIs, no imports. Everything user-supplied is
|
|
22
|
+
* written via textContent / escaped HTML; media URLs are limited to data:,
|
|
23
|
+
* https:, blob: and same-origin paths.
|
|
24
|
+
*/
|
|
25
|
+
const KIND_EMOJI = { folder: "📁", image: "🖼", video: "🎬", audio: "🎵", pdf: "📕", file: "📄" };
|
|
26
|
+
/** Media URLs we let into src attributes. */
|
|
27
|
+
function safeMediaUrl(u) {
|
|
28
|
+
const s = typeof u === "string" ? u : "";
|
|
29
|
+
return /^(data:(image|video|audio)\/|https:\/\/|blob:)/.test(s) || /^\/(?!\/)/.test(s) ? s : "";
|
|
30
|
+
}
|
|
31
|
+
function humanSize(n) {
|
|
32
|
+
if (typeof n !== "number" || !Number.isFinite(n))
|
|
33
|
+
return "";
|
|
34
|
+
const u = ["B", "KB", "MB", "GB", "TB"];
|
|
35
|
+
let i = 0;
|
|
36
|
+
let v = n;
|
|
37
|
+
while (v >= 1024 && i < u.length - 1) {
|
|
38
|
+
v /= 1024;
|
|
39
|
+
i++;
|
|
40
|
+
}
|
|
41
|
+
return `${i === 0 ? v : v.toFixed(1)} ${u[i]}`;
|
|
42
|
+
}
|
|
43
|
+
/** URLs we let into a download link: the media set, plus data: types a browser saves rather than runs. */
|
|
44
|
+
function safeDownloadUrl(u) {
|
|
45
|
+
const s = typeof u === "string" ? u : "";
|
|
46
|
+
return /^data:(text\/plain|application\/(pdf|octet-stream))[;,]/.test(s) ? s : safeMediaUrl(s);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* aindrive's own byte routes for an AINUI asset (works where the viewer has the session cookie):
|
|
50
|
+
* thumb → fs/thumbnail, original → fs/stream (inline), or fs/download (attachment) with `{ download: true }`.
|
|
51
|
+
*/
|
|
52
|
+
export function aindriveAssetUrl(asset, base = "", { download = false } = {}) {
|
|
53
|
+
if (!asset || typeof asset.drive_id !== "string" || typeof asset.path !== "string")
|
|
54
|
+
return "";
|
|
55
|
+
const route = download ? "download" : asset.variant === "thumb" ? "thumbnail" : "stream";
|
|
56
|
+
const v = typeof asset.v === "number" && !download ? `&v=${asset.v}` : "";
|
|
57
|
+
return `${base}/api/drives/${encodeURIComponent(asset.drive_id)}/fs/${route}?path=${encodeURIComponent(asset.path)}${v}`;
|
|
58
|
+
}
|
|
59
|
+
const ICON_EMOJI = { folder: "📁", search: "🔍", arrowBack: "⬅", home: "🏠", delete: "🗑", download: "⬇", share: "🔗", lock: "🔒", info: "ℹ️", warning: "⚠️", check: "✅" };
|
|
60
|
+
function escapeHtml(s) {
|
|
61
|
+
return String(s).replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
|
|
62
|
+
}
|
|
63
|
+
/** Tiny safe markdown: fenced code, headings, bold, inline code, paragraphs. */
|
|
64
|
+
function miniMarkdown(src) {
|
|
65
|
+
const parts = String(src).split(/```[^\n]*\n?/);
|
|
66
|
+
return parts.map((chunk, i) => {
|
|
67
|
+
if (i % 2 === 1)
|
|
68
|
+
return `<pre><code>${escapeHtml(chunk.replace(/\n$/, ""))}</code></pre>`;
|
|
69
|
+
return chunk.split(/\n{2,}/).filter((p) => p.trim()).map((p) => {
|
|
70
|
+
const inline = (t) => escapeHtml(t).replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>").replace(/`([^`]+)`/g, "<code>$1</code>");
|
|
71
|
+
const block = p.trim();
|
|
72
|
+
const m = block.match(/^(#{1,6})\s+([^\n]*)(?:\n([\s\S]*))?$/);
|
|
73
|
+
if (m)
|
|
74
|
+
return `<h${m[1].length}>${inline(m[2])}</h${m[1].length}>` + (m[3] ? miniMarkdown(m[3]) : "");
|
|
75
|
+
const lines = block.split("\n");
|
|
76
|
+
if (lines.every((l) => /^\s*[-*]\s+/.test(l)))
|
|
77
|
+
return `<ul>${lines.map((l) => `<li>${inline(l.replace(/^\s*[-*]\s+/, ""))}</li>`).join("")}</ul>`;
|
|
78
|
+
if (lines.every((l) => /^\s*\d+[.)]\s+/.test(l)))
|
|
79
|
+
return `<ol>${lines.map((l) => `<li>${inline(l.replace(/^\s*\d+[.)]\s+/, ""))}</li>`).join("")}</ol>`;
|
|
80
|
+
return `<p>${lines.map(inline).join("<br>")}</p>`;
|
|
81
|
+
}).join("");
|
|
82
|
+
}).join("");
|
|
83
|
+
}
|
|
84
|
+
function pointerGet(obj, pointer) {
|
|
85
|
+
if (!pointer || pointer === "/")
|
|
86
|
+
return obj;
|
|
87
|
+
return pointer.replace(/^\//, "").split("/").reduce((o, k) => (o == null ? undefined : o[k.replace(/~1/g, "/").replace(/~0/g, "~")]), obj);
|
|
88
|
+
}
|
|
89
|
+
const UNSAFE_KEYS = new Set(["__proto__", "prototype", "constructor"]);
|
|
90
|
+
function pointerSet(obj, pointer, value) {
|
|
91
|
+
const keys = pointer.replace(/^\//, "").split("/").filter(Boolean);
|
|
92
|
+
if (keys.some((k) => UNSAFE_KEYS.has(k)))
|
|
93
|
+
return obj;
|
|
94
|
+
if (!keys.length)
|
|
95
|
+
return value;
|
|
96
|
+
let o = obj;
|
|
97
|
+
keys.slice(0, -1).forEach((k) => { if (o[k] == null || typeof o[k] !== "object")
|
|
98
|
+
o[k] = {}; o = o[k]; });
|
|
99
|
+
o[keys[keys.length - 1]] = value;
|
|
100
|
+
return obj;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* @param {HTMLElement} container
|
|
104
|
+
* @param {{
|
|
105
|
+
* onAction?: (action: any) => unknown,
|
|
106
|
+
* resolveAsset?: (asset: { drive_id: string, path: string, variant: string, mime: string, v?: number }, opts?: { download?: boolean }) => string,
|
|
107
|
+
* assetBase?: string,
|
|
108
|
+
* }} [opts] resolveAsset: AINUI {$asset} → URL (default: aindrive's own fs routes under assetBase);
|
|
109
|
+
* `{ download: true }` asks for a URL that saves the original bytes (FileView's download link).
|
|
110
|
+
*/
|
|
111
|
+
export function createA2uiRenderer(container, { onAction, resolveAsset, assetBase = "" } = {}) {
|
|
112
|
+
/** surfaceId → { components: Map, data: object } */
|
|
113
|
+
const surfaces = new Map();
|
|
114
|
+
/** A `Media` value (URL string or {$asset}) → a safe URL, or "". */
|
|
115
|
+
function mediaUrl(v) {
|
|
116
|
+
if (v && typeof v === "object" && v.$asset) {
|
|
117
|
+
return safeMediaUrl(resolveAsset ? resolveAsset(v.$asset) : aindriveAssetUrl(v.$asset, assetBase));
|
|
118
|
+
}
|
|
119
|
+
return safeMediaUrl(v);
|
|
120
|
+
}
|
|
121
|
+
/** A `Media` value → a safe URL that saves the original bytes, or "". */
|
|
122
|
+
function downloadUrl(v) {
|
|
123
|
+
if (v && typeof v === "object" && v.$asset) {
|
|
124
|
+
const a = { ...v.$asset, variant: "original" };
|
|
125
|
+
return safeDownloadUrl(resolveAsset ? resolveAsset(a, { download: true }) : aindriveAssetUrl(a, assetBase, { download: true }));
|
|
126
|
+
}
|
|
127
|
+
return safeDownloadUrl(v);
|
|
128
|
+
}
|
|
129
|
+
function kindIcon(kind) {
|
|
130
|
+
const i = document.createElement("span");
|
|
131
|
+
i.className = "a2ui-kind-icon";
|
|
132
|
+
i.textContent = KIND_EMOJI[kind] || KIND_EMOJI.file;
|
|
133
|
+
return i;
|
|
134
|
+
}
|
|
135
|
+
function resolve(v, surf, scope) {
|
|
136
|
+
if (v && typeof v === "object" && typeof v.path === "string") {
|
|
137
|
+
return v.path.startsWith("/") ? pointerGet(surf.data, v.path) : pointerGet(scope ?? {}, "/" + v.path);
|
|
138
|
+
}
|
|
139
|
+
if (v && typeof v === "object" && typeof v.call === "string")
|
|
140
|
+
return "";
|
|
141
|
+
return v;
|
|
142
|
+
}
|
|
143
|
+
function childIds(spec, surf, scope) {
|
|
144
|
+
if (Array.isArray(spec))
|
|
145
|
+
return spec.map((id) => ({ id, scope }));
|
|
146
|
+
if (spec && typeof spec === "object" && spec.componentId) {
|
|
147
|
+
const list = resolve({ path: spec.path }, surf, scope);
|
|
148
|
+
return Array.isArray(list) ? list.map((item) => ({ id: spec.componentId, scope: item })) : [];
|
|
149
|
+
}
|
|
150
|
+
return [];
|
|
151
|
+
}
|
|
152
|
+
function renderNode(surfaceId, id, scope) {
|
|
153
|
+
const surf = surfaces.get(surfaceId);
|
|
154
|
+
const c = surf?.components.get(id);
|
|
155
|
+
const el = document.createElement("div");
|
|
156
|
+
if (!c) {
|
|
157
|
+
el.className = "a2ui-missing";
|
|
158
|
+
return el;
|
|
159
|
+
}
|
|
160
|
+
const kids = (spec, into) => childIds(spec, surf, scope).forEach((k) => into.appendChild(renderNode(surfaceId, k.id, k.scope)));
|
|
161
|
+
switch (c.component) {
|
|
162
|
+
case "Column":
|
|
163
|
+
case "Row":
|
|
164
|
+
case "List": {
|
|
165
|
+
const dir = c.component === "Row" || c.direction === "horizontal" ? "row" : "column";
|
|
166
|
+
el.className = `a2ui-${c.component.toLowerCase()}`;
|
|
167
|
+
el.style.flexDirection = dir;
|
|
168
|
+
if (c.align)
|
|
169
|
+
el.style.alignItems = { start: "flex-start", end: "flex-end", center: "center", stretch: "stretch" }[c.align] || "";
|
|
170
|
+
if (c.justify)
|
|
171
|
+
el.style.justifyContent = { start: "flex-start", end: "flex-end", center: "center", spaceBetween: "space-between", spaceAround: "space-around", spaceEvenly: "space-evenly" }[c.justify] || "";
|
|
172
|
+
kids(c.children, el);
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case "Card":
|
|
176
|
+
el.className = "a2ui-card";
|
|
177
|
+
el.appendChild(renderNode(surfaceId, c.child, scope));
|
|
178
|
+
break;
|
|
179
|
+
case "Text": {
|
|
180
|
+
const v = c.variant || "body";
|
|
181
|
+
const t = resolve(c.text, surf, scope) ?? "";
|
|
182
|
+
el.className = `a2ui-text a2ui-text-${v}`;
|
|
183
|
+
if (/^h[1-5]$/.test(v) || v === "mono")
|
|
184
|
+
el.textContent = String(t);
|
|
185
|
+
else
|
|
186
|
+
el.innerHTML = miniMarkdown(t);
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
case "Icon":
|
|
190
|
+
el.className = "a2ui-icon";
|
|
191
|
+
el.textContent = typeof c.name === "string" ? (ICON_EMOJI[c.name] || "•") : "•";
|
|
192
|
+
break;
|
|
193
|
+
case "Divider":
|
|
194
|
+
el.className = "a2ui-divider";
|
|
195
|
+
break;
|
|
196
|
+
case "Image": {
|
|
197
|
+
const img = document.createElement("img");
|
|
198
|
+
const safe = mediaUrl(resolve(c.url, surf, scope));
|
|
199
|
+
if (safe && !/^data:(video|audio)/.test(safe))
|
|
200
|
+
img.src = safe;
|
|
201
|
+
img.alt = String(resolve(c.description, surf, scope) ?? "");
|
|
202
|
+
img.className = "a2ui-image";
|
|
203
|
+
el.appendChild(img);
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
case "TextField": {
|
|
207
|
+
const label = document.createElement("label");
|
|
208
|
+
label.className = "a2ui-textfield";
|
|
209
|
+
const span = document.createElement("span");
|
|
210
|
+
span.textContent = String(resolve(c.label, surf, scope) ?? "");
|
|
211
|
+
const long = c.variant === "longText";
|
|
212
|
+
const input = document.createElement(long ? "textarea" : "input");
|
|
213
|
+
if (long) {
|
|
214
|
+
input.rows = 14;
|
|
215
|
+
label.classList.add("a2ui-textfield-long");
|
|
216
|
+
}
|
|
217
|
+
input.value = String(resolve(c.value, surf, scope) ?? "");
|
|
218
|
+
input.placeholder = span.textContent;
|
|
219
|
+
if (c.value && typeof c.value.path === "string") {
|
|
220
|
+
input.addEventListener("input", () => {
|
|
221
|
+
if (c.value.path.startsWith("/"))
|
|
222
|
+
pointerSet(surf.data, c.value.path, input.value);
|
|
223
|
+
else if (scope)
|
|
224
|
+
pointerSet(scope, "/" + c.value.path, input.value);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
input.addEventListener("keydown", (e) => {
|
|
228
|
+
if (e.key !== "Enter" || long)
|
|
229
|
+
return;
|
|
230
|
+
// Enter submits the nearest primary button's action, if any.
|
|
231
|
+
const primary = [...surf.components.values()].find((x) => x.component === "Button" && x.variant === "primary");
|
|
232
|
+
if (primary)
|
|
233
|
+
fire(surfaceId, primary, scope);
|
|
234
|
+
});
|
|
235
|
+
label.append(span, input);
|
|
236
|
+
el.appendChild(label);
|
|
237
|
+
if (typeof c.weight === "number")
|
|
238
|
+
el.style.flex = String(c.weight);
|
|
239
|
+
return el;
|
|
240
|
+
}
|
|
241
|
+
case "Button": {
|
|
242
|
+
const b = document.createElement("button");
|
|
243
|
+
b.type = "button";
|
|
244
|
+
b.className = `a2ui-button a2ui-button-${c.variant || "default"}${c.tone === "danger" ? " a2ui-button-danger" : ""}`;
|
|
245
|
+
b.appendChild(renderNode(surfaceId, c.child, scope));
|
|
246
|
+
const question = c.confirm !== undefined ? String(resolve(c.confirm, surf, scope) ?? "") : "";
|
|
247
|
+
b.addEventListener("click", () => {
|
|
248
|
+
if (!question)
|
|
249
|
+
return fire(surfaceId, c, scope);
|
|
250
|
+
// Inline confirmation: sandboxed iframes (MCP Apps) block window.confirm.
|
|
251
|
+
const ask = document.createElement("span");
|
|
252
|
+
ask.className = "a2ui-confirm";
|
|
253
|
+
const q = document.createElement("span");
|
|
254
|
+
q.textContent = question;
|
|
255
|
+
const no = document.createElement("button");
|
|
256
|
+
no.type = "button";
|
|
257
|
+
no.className = "a2ui-button";
|
|
258
|
+
no.textContent = "Cancel";
|
|
259
|
+
const yes = document.createElement("button");
|
|
260
|
+
yes.type = "button";
|
|
261
|
+
yes.className = `a2ui-button${c.tone === "danger" ? " a2ui-button-danger" : " a2ui-button-primary"}`;
|
|
262
|
+
yes.textContent = "OK";
|
|
263
|
+
no.addEventListener("click", () => el.replaceChildren(b));
|
|
264
|
+
yes.addEventListener("click", () => { el.replaceChildren(b); fire(surfaceId, c, scope); });
|
|
265
|
+
ask.append(q, no, yes);
|
|
266
|
+
el.replaceChildren(ask);
|
|
267
|
+
no.focus();
|
|
268
|
+
});
|
|
269
|
+
el.appendChild(b);
|
|
270
|
+
el.className = "a2ui-button-wrap";
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
// ── AINUI components ──
|
|
274
|
+
case "FileUpload": {
|
|
275
|
+
const label = document.createElement("label");
|
|
276
|
+
label.textContent = String(resolve(c.label, surf, scope) || "Upload file");
|
|
277
|
+
const input = document.createElement("input");
|
|
278
|
+
input.type = "file";
|
|
279
|
+
const status = document.createElement("span");
|
|
280
|
+
status.setAttribute("role", "status");
|
|
281
|
+
input.addEventListener("change", async () => {
|
|
282
|
+
const file = input.files?.[0];
|
|
283
|
+
input.value = "";
|
|
284
|
+
if (!file)
|
|
285
|
+
return;
|
|
286
|
+
const limit = Math.min(Number(c.maxBytes) || 8388608, 8388608);
|
|
287
|
+
if (file.size > limit) {
|
|
288
|
+
status.textContent = "Maximum file size: 8 MiB";
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
input.disabled = true;
|
|
292
|
+
status.textContent = "Uploading…";
|
|
293
|
+
try {
|
|
294
|
+
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
295
|
+
let binary = "";
|
|
296
|
+
for (let i = 0; i < bytes.length; i += 8192)
|
|
297
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + 8192));
|
|
298
|
+
await fire(surfaceId, c, scope, { name: file.name, content: btoa(binary), encoding: "base64" });
|
|
299
|
+
status.textContent = "Uploaded";
|
|
300
|
+
}
|
|
301
|
+
catch (e) {
|
|
302
|
+
status.textContent = e.message;
|
|
303
|
+
}
|
|
304
|
+
finally {
|
|
305
|
+
input.disabled = false;
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
label.appendChild(input);
|
|
309
|
+
el.appendChild(label);
|
|
310
|
+
el.appendChild(status);
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
case "X402Payment": {
|
|
314
|
+
el.className = "a2ui-column";
|
|
315
|
+
const terms = document.createElement("p");
|
|
316
|
+
const amount = String(resolve(c.amount, surf, scope) ?? "");
|
|
317
|
+
const currency = String(resolve(c.currency, surf, scope) ?? "");
|
|
318
|
+
terms.textContent = `${amount} ${currency} · ${resolve(c.network, surf, scope)} → ${resolve(c.payTo, surf, scope)}`;
|
|
319
|
+
const pay = document.createElement("button");
|
|
320
|
+
pay.type = "button";
|
|
321
|
+
pay.className = "a2ui-button a2ui-button-primary";
|
|
322
|
+
pay.textContent = `Pay ${amount} ${currency}`;
|
|
323
|
+
const status = document.createElement("span");
|
|
324
|
+
status.setAttribute("role", "status");
|
|
325
|
+
pay.addEventListener("click", async () => {
|
|
326
|
+
pay.disabled = true;
|
|
327
|
+
status.textContent = "Confirm in your wallet…";
|
|
328
|
+
try {
|
|
329
|
+
await fire(surfaceId, c, scope);
|
|
330
|
+
status.textContent = "";
|
|
331
|
+
}
|
|
332
|
+
catch (e) {
|
|
333
|
+
status.textContent = e.message;
|
|
334
|
+
}
|
|
335
|
+
finally {
|
|
336
|
+
pay.disabled = false;
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
el.appendChild(terms);
|
|
340
|
+
el.appendChild(pay);
|
|
341
|
+
el.appendChild(status);
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
case "Grid": {
|
|
345
|
+
el.className = "a2ui-grid";
|
|
346
|
+
const min = typeof c.minItemWidth === "number" && c.minItemWidth > 0 ? c.minItemWidth : 128;
|
|
347
|
+
el.style.gridTemplateColumns = `repeat(auto-fill, minmax(min(${min}px, 100%), 1fr))`;
|
|
348
|
+
el.style.gap = `${typeof c.gap === "number" && c.gap >= 0 ? c.gap : 8}px`;
|
|
349
|
+
kids(c.children, el);
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
case "Tile": {
|
|
353
|
+
const kind = String(resolve(c.kind, surf, scope) ?? "file");
|
|
354
|
+
const b = document.createElement("button");
|
|
355
|
+
b.type = "button";
|
|
356
|
+
b.className = "a2ui-tile";
|
|
357
|
+
const media = document.createElement("span");
|
|
358
|
+
media.className = "a2ui-tile-media";
|
|
359
|
+
const src = mediaUrl(resolve(c.media, surf, scope));
|
|
360
|
+
if (src && !/^data:(video|audio)/.test(src)) {
|
|
361
|
+
const img = document.createElement("img");
|
|
362
|
+
img.loading = "lazy";
|
|
363
|
+
img.alt = "";
|
|
364
|
+
img.addEventListener("error", () => media.replaceChildren(kindIcon(kind)));
|
|
365
|
+
img.src = src;
|
|
366
|
+
media.appendChild(img);
|
|
367
|
+
}
|
|
368
|
+
else
|
|
369
|
+
media.appendChild(kindIcon(kind));
|
|
370
|
+
const label = document.createElement("span");
|
|
371
|
+
label.className = "a2ui-tile-label";
|
|
372
|
+
label.textContent = String(resolve(c.label, surf, scope) ?? "");
|
|
373
|
+
b.title = label.textContent;
|
|
374
|
+
b.append(media, label);
|
|
375
|
+
const captionText = c.caption !== undefined ? String(resolve(c.caption, surf, scope) ?? "") : "";
|
|
376
|
+
if (captionText) {
|
|
377
|
+
const cap = document.createElement("span");
|
|
378
|
+
cap.className = "a2ui-tile-caption";
|
|
379
|
+
cap.textContent = captionText;
|
|
380
|
+
b.appendChild(cap);
|
|
381
|
+
}
|
|
382
|
+
b.addEventListener("click", () => fire(surfaceId, c, scope));
|
|
383
|
+
el.className = "a2ui-tile-wrap";
|
|
384
|
+
el.appendChild(b);
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
case "FileView": {
|
|
388
|
+
el.className = "a2ui-fileview";
|
|
389
|
+
const name = String(resolve(c.name, surf, scope) ?? "");
|
|
390
|
+
const mime = String(resolve(c.mime, surf, scope) ?? "");
|
|
391
|
+
const size = resolve(c.size, surf, scope);
|
|
392
|
+
const rawSrc = resolve(c.src, surf, scope);
|
|
393
|
+
const src = mediaUrl(rawSrc);
|
|
394
|
+
const tag = !src ? "" : mime.startsWith("image/") ? "img" : mime.startsWith("video/") ? "video" : mime.startsWith("audio/") ? "audio" : "";
|
|
395
|
+
if (tag) {
|
|
396
|
+
const m = document.createElement(tag);
|
|
397
|
+
m.className = `a2ui-fileview-${tag}`;
|
|
398
|
+
if (tag === "img")
|
|
399
|
+
m.alt = name;
|
|
400
|
+
else {
|
|
401
|
+
m.controls = true;
|
|
402
|
+
m.preload = "metadata";
|
|
403
|
+
}
|
|
404
|
+
m.src = src;
|
|
405
|
+
el.appendChild(m);
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
const box = document.createElement("div");
|
|
409
|
+
box.className = "a2ui-fileview-icon";
|
|
410
|
+
box.appendChild(kindIcon(mime === "application/pdf" ? "pdf" : "file"));
|
|
411
|
+
el.appendChild(box);
|
|
412
|
+
}
|
|
413
|
+
const info = document.createElement("div");
|
|
414
|
+
info.className = "a2ui-fileview-info";
|
|
415
|
+
const n = document.createElement("span");
|
|
416
|
+
n.className = "a2ui-fileview-name";
|
|
417
|
+
n.textContent = name;
|
|
418
|
+
const sz = document.createElement("span");
|
|
419
|
+
sz.className = "a2ui-text-caption";
|
|
420
|
+
sz.textContent = humanSize(size);
|
|
421
|
+
info.append(n, sz);
|
|
422
|
+
// Download affordance (AINUI §3) for every kind — the only way to reach a PDF's / archive's bytes.
|
|
423
|
+
const href = downloadUrl(rawSrc);
|
|
424
|
+
if (href) {
|
|
425
|
+
const a = document.createElement("a");
|
|
426
|
+
a.className = "a2ui-button a2ui-fileview-download";
|
|
427
|
+
a.href = href;
|
|
428
|
+
a.download = name;
|
|
429
|
+
// Sandboxed iframes (MCP Apps) may block downloads; a new tab still reaches the bytes.
|
|
430
|
+
a.target = "_blank";
|
|
431
|
+
a.rel = "noopener noreferrer";
|
|
432
|
+
a.textContent = "⬇ Download";
|
|
433
|
+
a.setAttribute("aria-label", name ? `Download ${name}` : "Download");
|
|
434
|
+
info.appendChild(a);
|
|
435
|
+
}
|
|
436
|
+
el.appendChild(info);
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
case "Breadcrumbs": {
|
|
440
|
+
el.className = "a2ui-breadcrumbs";
|
|
441
|
+
el.setAttribute("role", "navigation");
|
|
442
|
+
const items = resolve(c.items, surf, scope);
|
|
443
|
+
(Array.isArray(items) ? items : []).forEach((item, i, all) => {
|
|
444
|
+
if (i > 0) {
|
|
445
|
+
const sep = document.createElement("span");
|
|
446
|
+
sep.className = "a2ui-crumb-sep";
|
|
447
|
+
sep.textContent = "›";
|
|
448
|
+
el.appendChild(sep);
|
|
449
|
+
}
|
|
450
|
+
const label = String(item && item.label != null ? item.label : "");
|
|
451
|
+
if (i === all.length - 1) {
|
|
452
|
+
// The last crumb is where we are: plain text.
|
|
453
|
+
const cur = document.createElement("span");
|
|
454
|
+
cur.className = "a2ui-crumb a2ui-crumb-current";
|
|
455
|
+
cur.textContent = label;
|
|
456
|
+
el.appendChild(cur);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const b = document.createElement("button");
|
|
460
|
+
b.type = "button";
|
|
461
|
+
b.className = "a2ui-crumb";
|
|
462
|
+
b.textContent = label;
|
|
463
|
+
b.addEventListener("click", () => fire(surfaceId, c, item, { path: item && item.path }));
|
|
464
|
+
el.appendChild(b);
|
|
465
|
+
});
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
case "Segmented": {
|
|
469
|
+
el.className = "a2ui-segmented";
|
|
470
|
+
el.setAttribute("role", "group");
|
|
471
|
+
const current = String(resolve(c.value, surf, scope) ?? "");
|
|
472
|
+
for (const opt of Array.isArray(c.options) ? c.options : []) {
|
|
473
|
+
const b = document.createElement("button");
|
|
474
|
+
b.type = "button";
|
|
475
|
+
b.className = "a2ui-segment";
|
|
476
|
+
b.textContent = String(opt && opt.label != null ? opt.label : opt && opt.value);
|
|
477
|
+
b.setAttribute("aria-pressed", String(opt && opt.value === current));
|
|
478
|
+
b.addEventListener("click", () => {
|
|
479
|
+
if (c.value && typeof c.value.path === "string") {
|
|
480
|
+
if (c.value.path.startsWith("/"))
|
|
481
|
+
pointerSet(surf.data, c.value.path, opt.value);
|
|
482
|
+
else if (scope)
|
|
483
|
+
pointerSet(scope, "/" + c.value.path, opt.value);
|
|
484
|
+
}
|
|
485
|
+
fire(surfaceId, c, scope, { value: opt.value });
|
|
486
|
+
});
|
|
487
|
+
el.appendChild(b);
|
|
488
|
+
}
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
default:
|
|
492
|
+
// Unknown component: draw its children if it has any, else nothing (AINUI §3).
|
|
493
|
+
el.className = "a2ui-unknown";
|
|
494
|
+
if (c.children)
|
|
495
|
+
kids(c.children, el);
|
|
496
|
+
else if (typeof c.child === "string")
|
|
497
|
+
el.appendChild(renderNode(surfaceId, c.child, scope));
|
|
498
|
+
}
|
|
499
|
+
if (typeof c.weight === "number")
|
|
500
|
+
el.style.flex = String(c.weight);
|
|
501
|
+
return el;
|
|
502
|
+
}
|
|
503
|
+
function fire(surfaceId, c, scope, extra) {
|
|
504
|
+
const ev = c.action && c.action.event;
|
|
505
|
+
if (!ev || !onAction)
|
|
506
|
+
return;
|
|
507
|
+
const surf = surfaces.get(surfaceId);
|
|
508
|
+
const context = {};
|
|
509
|
+
for (const [k, v] of Object.entries(ev.context || {}))
|
|
510
|
+
context[k] = resolve(v, surf, scope);
|
|
511
|
+
Object.assign(context, extra || {});
|
|
512
|
+
return onAction({ name: ev.name, surfaceId, sourceComponentId: c.id, timestamp: new Date().toISOString(), context });
|
|
513
|
+
}
|
|
514
|
+
function render() {
|
|
515
|
+
container.replaceChildren();
|
|
516
|
+
for (const [surfaceId, surf] of surfaces) {
|
|
517
|
+
if (!surf.components.has("root"))
|
|
518
|
+
continue;
|
|
519
|
+
const wrap = document.createElement("section");
|
|
520
|
+
wrap.className = "a2ui-surface";
|
|
521
|
+
wrap.appendChild(renderNode(surfaceId, "root", undefined));
|
|
522
|
+
container.appendChild(wrap);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
/** Apply a batch of server→client messages, then re-render. */
|
|
526
|
+
function process(messages) {
|
|
527
|
+
for (const m of messages || []) {
|
|
528
|
+
if (m.createSurface)
|
|
529
|
+
surfaces.set(m.createSurface.surfaceId, { components: new Map(), data: {} });
|
|
530
|
+
else if (m.updateComponents) {
|
|
531
|
+
const s = surfaces.get(m.updateComponents.surfaceId);
|
|
532
|
+
if (s)
|
|
533
|
+
for (const c of m.updateComponents.components)
|
|
534
|
+
s.components.set(c.id, c);
|
|
535
|
+
}
|
|
536
|
+
else if (m.updateDataModel) {
|
|
537
|
+
const s = surfaces.get(m.updateDataModel.surfaceId);
|
|
538
|
+
if (s) {
|
|
539
|
+
const p = m.updateDataModel.path || "/";
|
|
540
|
+
if (p === "/")
|
|
541
|
+
s.data = m.updateDataModel.value ?? {};
|
|
542
|
+
else
|
|
543
|
+
pointerSet(s.data, p, m.updateDataModel.value);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
else if (m.deleteSurface)
|
|
547
|
+
surfaces.delete(m.deleteSurface.surfaceId);
|
|
548
|
+
}
|
|
549
|
+
render();
|
|
550
|
+
}
|
|
551
|
+
/** Drop every surface and show just these messages (one result = one view). */
|
|
552
|
+
function replace(messages) { surfaces.clear(); process(messages); }
|
|
553
|
+
return { process, replace, surfaces };
|
|
554
|
+
}
|
|
555
|
+
export const A2UI_RENDERER_CSS = `
|
|
556
|
+
.a2ui-surface{font:14px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif;color:#0f1319}
|
|
557
|
+
.a2ui-column,.a2ui-row,.a2ui-list{display:flex;gap:8px}
|
|
558
|
+
.a2ui-list{gap:2px}
|
|
559
|
+
.a2ui-card{border:1px solid #dce3ec;border-radius:12px;padding:12px;background:#fff;overflow:auto;max-height:480px}
|
|
560
|
+
.a2ui-text p,.a2ui-text ul,.a2ui-text ol{margin:0 0 6px}.a2ui-text ul{list-style:disc;padding-left:20px}.a2ui-text ol{list-style:decimal;padding-left:20px}.a2ui-text h1,.a2ui-text h2,.a2ui-text h3{margin:4px 0}
|
|
561
|
+
.a2ui-text-h1{font-size:24px;font-weight:600}.a2ui-text-h2{font-size:20px;font-weight:600}.a2ui-text-h3{font-size:16px;font-weight:600}
|
|
562
|
+
.a2ui-text-caption{font-size:12px;color:#54607a}.a2ui-text-caption p{margin:0}
|
|
563
|
+
.a2ui-text pre{background:#f2f5f9;padding:8px;border-radius:8px;overflow:auto;white-space:pre-wrap;word-break:break-word}
|
|
564
|
+
.a2ui-button{font:inherit;border:1px solid #dce3ec;background:#fff;border-radius:999px;padding:4px 12px;cursor:pointer}
|
|
565
|
+
.a2ui-button:hover{background:#eaeef4}
|
|
566
|
+
.a2ui-button-primary{background:#0b57d0;border-color:#0b57d0;color:#fff}.a2ui-button-primary:hover{background:#0842a0}
|
|
567
|
+
.a2ui-button-borderless{border:0;background:none;padding:2px 4px;text-align:left}.a2ui-button-borderless:hover{background:#eaeef4}
|
|
568
|
+
.a2ui-button .a2ui-text p{margin:0}
|
|
569
|
+
.a2ui-textfield{display:flex;flex-direction:column;flex:1}.a2ui-textfield span{display:none}
|
|
570
|
+
.a2ui-textfield input{font:inherit;border:1px solid #dce3ec;border-radius:999px;padding:4px 12px;min-width:120px}
|
|
571
|
+
.a2ui-image{max-width:100%;max-height:420px;object-fit:contain}
|
|
572
|
+
.a2ui-divider{border-top:1px solid #dce3ec;margin:4px 0}
|
|
573
|
+
.a2ui-row .a2ui-text-caption{white-space:nowrap}
|
|
574
|
+
.a2ui-button-danger{color:#b3261e;border-color:#f2b8b5}.a2ui-button-danger.a2ui-button-primary,.a2ui-confirm .a2ui-button-danger{background:#b3261e;border-color:#b3261e;color:#fff}
|
|
575
|
+
.a2ui-confirm{display:inline-flex;flex-wrap:wrap;align-items:center;gap:6px}
|
|
576
|
+
.a2ui-text-mono{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:13px}
|
|
577
|
+
.a2ui-textfield-long{min-width:0}.a2ui-textfield-long textarea{font:13px/1.5 ui-monospace,SFMono-Regular,Menlo,monospace;border:1px solid #dce3ec;border-radius:12px;padding:8px 12px;resize:vertical;min-height:240px;width:100%;box-sizing:border-box}
|
|
578
|
+
.a2ui-row{flex-wrap:wrap}
|
|
579
|
+
.a2ui-grid{display:grid}
|
|
580
|
+
.a2ui-tile-wrap{min-width:0}
|
|
581
|
+
.a2ui-tile{display:flex;flex-direction:column;gap:4px;width:100%;font:inherit;color:inherit;text-align:left;border:0;background:none;padding:4px;border-radius:12px;cursor:pointer}
|
|
582
|
+
.a2ui-tile:hover,.a2ui-tile:focus-visible{background:#eaeef4}
|
|
583
|
+
.a2ui-tile-media{display:flex;align-items:center;justify-content:center;aspect-ratio:1;width:100%;border-radius:10px;overflow:hidden;background:#f2f5f9}
|
|
584
|
+
.a2ui-tile-media img{width:100%;height:100%;object-fit:cover}
|
|
585
|
+
.a2ui-tile-media .a2ui-kind-icon{font-size:40px}
|
|
586
|
+
.a2ui-tile-label{font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
587
|
+
.a2ui-tile-caption{font-size:12px;color:#54607a;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
588
|
+
.a2ui-fileview{display:flex;flex-direction:column;gap:8px}
|
|
589
|
+
.a2ui-fileview-img,.a2ui-fileview-video{max-width:100%;max-height:70vh;object-fit:contain;border-radius:10px;background:#f2f5f9}
|
|
590
|
+
.a2ui-fileview-audio{width:100%}
|
|
591
|
+
.a2ui-fileview-icon{display:flex;align-items:center;justify-content:center;height:160px;border-radius:10px;background:#f2f5f9;font-size:56px}
|
|
592
|
+
.a2ui-fileview-info{display:flex;gap:8px;align-items:baseline;flex-wrap:wrap}.a2ui-fileview-name{font-weight:600;word-break:break-all}
|
|
593
|
+
.a2ui-fileview-download{display:inline-flex;align-items:center;min-height:36px;box-sizing:border-box;margin-left:auto;color:inherit;text-decoration:none}
|
|
594
|
+
.a2ui-breadcrumbs{display:flex;flex-wrap:wrap;align-items:center;gap:2px;font-size:13px}
|
|
595
|
+
.a2ui-crumb{display:inline-flex;align-items:center;font:inherit;color:#0b57d0;border:0;background:none;padding:0 6px;border-radius:8px;cursor:pointer;min-height:36px;box-sizing:border-box}
|
|
596
|
+
.a2ui-crumb:hover{background:#eaeef4}.a2ui-crumb-current{color:inherit;font-weight:600;cursor:default}
|
|
597
|
+
.a2ui-crumb-sep{color:#54607a}
|
|
598
|
+
.a2ui-segmented{display:inline-flex;border:1px solid #dce3ec;border-radius:999px;overflow:hidden}
|
|
599
|
+
.a2ui-segment{font:inherit;border:0;background:#fff;color:inherit;padding:4px 14px;cursor:pointer;min-height:36px}
|
|
600
|
+
.a2ui-segment[aria-pressed=true]{background:#0b57d0;color:#fff}
|
|
601
|
+
@media (prefers-color-scheme:dark){.a2ui-surface{color:#e6e9ef}.a2ui-card,.a2ui-button,.a2ui-textfield input,.a2ui-textfield textarea,.a2ui-segment{background:#1b1f27;border-color:#343b48;color:inherit}.a2ui-segmented{border-color:#343b48}.a2ui-segment[aria-pressed=true]{background:#0b57d0;color:#fff}.a2ui-button:hover,.a2ui-button-borderless:hover,.a2ui-tile:hover,.a2ui-crumb:hover{background:#262c36}.a2ui-text pre,.a2ui-tile-media,.a2ui-fileview-icon,.a2ui-fileview-img,.a2ui-fileview-video{background:#262c36}.a2ui-text-caption,.a2ui-tile-caption,.a2ui-crumb-sep{color:#9aa4b8}.a2ui-crumb{color:#a8c7fa}.a2ui-button-danger{color:#f2b8b5;border-color:#8c1d18}}
|
|
602
|
+
`;
|
package/dist/styles.css
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
.ain-ui { color: inherit; font: inherit; }
|
|
2
|
+
.ain-ui button, .ain-ui input { font: inherit; }
|
|
3
|
+
.ain-ui button { cursor:pointer; }
|
|
4
|
+
.ain-ui button:disabled { cursor:default; opacity:.5; }
|
|
5
|
+
.ain-ui img, .ain-ui video { max-width:100%; }
|
|
6
|
+
.ain-ui fieldset { border:0; padding:0; margin:0; min-width:0; }
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/x402.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ClientEvmSigner } from "@x402/evm";
|
|
2
|
+
/** Sign only after a host has shown the quote and the user has clicked Pay.
|
|
3
|
+
* The official x402 SDK owns authorization construction and header encoding.
|
|
4
|
+
* Permit2 approval is a separate host flow, so this helper admits EIP-3009 only. */
|
|
5
|
+
export declare function signX402Payment(paymentRequired: string, signer: ClientEvmSigner): Promise<{
|
|
6
|
+
header: string;
|
|
7
|
+
from: `0x${string}`;
|
|
8
|
+
}>;
|
package/dist/x402.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { x402Client } from "@x402/core/client";
|
|
2
|
+
import { decodePaymentRequiredHeader, encodePaymentSignatureHeader } from "@x402/core/http";
|
|
3
|
+
import { ExactEvmScheme } from "@x402/evm/exact/client";
|
|
4
|
+
/** Sign only after a host has shown the quote and the user has clicked Pay.
|
|
5
|
+
* The official x402 SDK owns authorization construction and header encoding.
|
|
6
|
+
* Permit2 approval is a separate host flow, so this helper admits EIP-3009 only. */
|
|
7
|
+
export async function signX402Payment(paymentRequired, signer) {
|
|
8
|
+
const required = decodePaymentRequiredHeader(paymentRequired);
|
|
9
|
+
const accepts = required.accepts.filter((r) => r.scheme === "exact" && r.network.startsWith("eip155:") && r.extra?.assetTransferMethod !== "permit2");
|
|
10
|
+
if (!accepts.length)
|
|
11
|
+
throw new Error("This payment requires a different wallet approval flow");
|
|
12
|
+
const client = new x402Client();
|
|
13
|
+
client.register("eip155:*", new ExactEvmScheme(signer));
|
|
14
|
+
const payload = await client.createPaymentPayload({ ...required, accepts });
|
|
15
|
+
return { header: encodePaymentSignatureHeader(payload), from: signer.address };
|
|
16
|
+
}
|