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.
@@ -0,0 +1,5 @@
1
+ export * from "./basic.js";
2
+ export * from "./ainui.js";
3
+ export * from "./payment.js";
4
+ export type { SkillResult } from "./types.js";
5
+ export * from "./agui.js";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./basic.js";
2
+ export * from "./ainui.js";
3
+ export * from "./payment.js";
4
+ export * from "./agui.js";
@@ -0,0 +1,13 @@
1
+ /** Browser-wallet payment surface. The host executes the action locally;
2
+ * signatures are submitted to the same authorized share resource via x402. */
3
+ import type { PaymentRequired } from "@x402/core/types";
4
+ import { type A2uiMessage } from "./basic.js";
5
+ export declare const X402_PAY_ACTION = "aindrive.x402.pay";
6
+ export declare function ainuiPayment(input: {
7
+ shareToken: string;
8
+ title: string;
9
+ required: PaymentRequired;
10
+ paymentRequired: string;
11
+ symbol: string;
12
+ decimals: number;
13
+ }): A2uiMessage[];
@@ -0,0 +1,23 @@
1
+ import { AINUI_CATALOG } from "./ainui.js";
2
+ import { A2UI_VERSION } from "./basic.js";
3
+ export const X402_PAY_ACTION = "aindrive.x402.pay";
4
+ export function ainuiPayment(input) {
5
+ const surfaceId = `aindrive-payment-${input.shareToken}`;
6
+ const req = input.required.accepts[0];
7
+ const digits = req.amount.padStart(input.decimals + 1, "0");
8
+ const amount = input.decimals ? `${digits.slice(0, -input.decimals)}.${digits.slice(-input.decimals)}`.replace(/\.?0+$/, "") : digits;
9
+ return [
10
+ { version: A2UI_VERSION, createSurface: { surfaceId, catalogId: AINUI_CATALOG } },
11
+ { version: A2UI_VERSION, updateComponents: { surfaceId, components: [
12
+ { id: "root", component: "Column", children: ["title", "payment"] },
13
+ { id: "title", component: "Text", variant: "h3", text: { path: "/title" } },
14
+ { id: "payment", component: "X402Payment", amount: { path: "/amount" }, currency: { path: "/currency" },
15
+ network: { path: "/network" }, payTo: { path: "/payTo" },
16
+ action: { event: { name: X402_PAY_ACTION, context: { share_token: { path: "/share_token" }, paymentRequired: { path: "/paymentRequired" } } } } },
17
+ ] } },
18
+ { version: A2UI_VERSION, updateDataModel: { surfaceId, path: "/", value: {
19
+ title: input.title, share_token: input.shareToken, paymentRequired: input.paymentRequired,
20
+ amount, currency: input.symbol, network: req.network, payTo: req.payTo,
21
+ } } },
22
+ ];
23
+ }
@@ -0,0 +1,13 @@
1
+ import React from "react";
2
+ import { Catalog } from "@a2ui/web_core/v0_9";
3
+ import { type AssetRef } from "./ainui.js";
4
+ import type { A2uiAction, A2uiMessage } from "./basic.js";
5
+ export type AssetResolver = (asset: AssetRef["$asset"], options?: {
6
+ download?: boolean;
7
+ }) => string;
8
+ export declare const ainuiCatalog: Catalog<import("@a2ui/react/v0_9").ReactComponentImplementation, import("@a2ui/web_core/v0_9").FunctionImplementation>;
9
+ export declare function AinuiSurface({ messages, onAction, resolveAsset }: {
10
+ messages: A2uiMessage[];
11
+ onAction: (action: A2uiAction) => void | Promise<void>;
12
+ resolveAsset?: AssetResolver;
13
+ }): React.JSX.Element;
package/dist/react.js ADDED
@@ -0,0 +1,173 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import React, { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
4
+ import { A2uiSurface, MarkdownContext, basicCatalog, createBinderlessComponentImplementation, useSignalValue } from "@a2ui/react/v0_9";
5
+ import { Catalog, CommonSchemas, MessageProcessor } from "@a2ui/web_core/v0_9";
6
+ import { z } from "zod";
7
+ import { renderMarkdown } from "@a2ui/markdown-it";
8
+ import { AINUI_CATALOG, AINUI_UPLOAD_MAX_BYTES } from "./ainui.js";
9
+ const Assets = createContext(() => "");
10
+ const box = { display: "flex", flexDirection: "column", gap: 8, minWidth: 0 };
11
+ const button = { padding: "8px 12px", border: "1px solid #8885", borderRadius: 8, color: "inherit", background: "transparent" };
12
+ function useValue(ctx, raw) {
13
+ const signal = useMemo(() => ctx.dataContext.resolveSignal(raw), [ctx, raw]);
14
+ return useSignalValue(signal);
15
+ }
16
+ function urlOf(value, resolver, download = false) {
17
+ const raw = value && typeof value === "object" && "$asset" in value
18
+ ? resolver(value.$asset, { download }) : value;
19
+ if (typeof raw !== "string")
20
+ return "";
21
+ if (/^\/(?!\/)/.test(raw) || /^https?:\/\//i.test(raw) || /^blob:/i.test(raw))
22
+ return raw;
23
+ return "";
24
+ }
25
+ async function fire(ctx, extra = {}) {
26
+ const raw = ctx.componentModel.properties.action;
27
+ if (!raw)
28
+ return;
29
+ const action = ctx.dataContext.resolveAction(raw);
30
+ if (action?.event)
31
+ await ctx.dispatchAction({ event: { ...action.event, context: { ...action.event.context, ...extra } } });
32
+ }
33
+ // A2UI owns state, bindings, action validation and surface lifecycle. These
34
+ // implementations add only AIN-UI's component views to the official catalog.
35
+ const api = (name, shape) => ({ name, schema: z.object(shape).passthrough() });
36
+ const dynamic = z.union([CommonSchemas.DynamicValue, z.object({ $asset: z.object({
37
+ drive_id: z.string(), path: z.string(), variant: z.enum(["thumb", "original"]), mime: z.string(), v: z.number().optional(),
38
+ }) }), z.null()]).optional();
39
+ const Grid = createBinderlessComponentImplementation(api("Grid", { children: CommonSchemas.ChildList, minItemWidth: z.number().optional(), gap: z.number().optional() }), ({ context, buildChild }) => {
40
+ const p = context.componentModel.properties;
41
+ const spec = p.children;
42
+ const rows = useValue(context, !Array.isArray(spec) && spec ? { path: spec.path } : []);
43
+ return _jsx("div", { style: { display: "grid", gridTemplateColumns: `repeat(auto-fit,minmax(min(100%,${Math.max(64, Number(p.minItemWidth) || 128)}px),1fr))`, gap: Number(p.gap) || 8 }, children: Array.isArray(spec) ? spec.map((id) => _jsx(React.Fragment, { children: buildChild(id) }, id)) :
44
+ (Array.isArray(rows) ? rows : []).map((_, i) => _jsx(React.Fragment, { children: buildChild(spec.componentId, `${spec.path}/${i}`) }, i)) });
45
+ });
46
+ const Tile = createBinderlessComponentImplementation(api("Tile", { media: dynamic, label: dynamic, caption: dynamic, kind: dynamic, action: CommonSchemas.Action.optional() }), ({ context }) => {
47
+ const p = context.componentModel.properties;
48
+ const media = useValue(context, p.media);
49
+ const label = useValue(context, p.label);
50
+ const caption = useValue(context, p.caption);
51
+ const kind = useValue(context, p.kind);
52
+ const resolver = useContext(Assets);
53
+ const src = urlOf(media, resolver);
54
+ const [failed, setFailed] = useState(false);
55
+ useEffect(() => setFailed(false), [src]);
56
+ return _jsxs("button", { style: { ...box, ...button, textAlign: "left" }, onClick: () => void fire(context), children: [src && !failed ? _jsx("img", { src: src, alt: "", loading: "lazy", onError: () => setFailed(true), style: { width: "100%", aspectRatio: "1", objectFit: "cover", borderRadius: 8 } }) :
57
+ _jsx("span", { style: { fontSize: 40, padding: 24 }, children: kind === "folder" ? "📁" : "📄" }), _jsx("span", { style: { overflowWrap: "anywhere" }, children: label }), _jsx("small", { children: caption })] });
58
+ });
59
+ const FileView = createBinderlessComponentImplementation(api("FileView", { src: dynamic, name: dynamic, mime: dynamic, size: dynamic }), ({ context }) => {
60
+ const p = context.componentModel.properties;
61
+ const asset = useValue(context, p.src);
62
+ const name = useValue(context, p.name);
63
+ const mime = useValue(context, p.mime) || "";
64
+ const resolver = useContext(Assets);
65
+ const src = urlOf(asset, resolver);
66
+ return _jsxs("div", { style: box, children: [src && (mime.startsWith("image/") ? _jsx("img", { src: src, alt: name, style: { maxHeight: "70vh", objectFit: "contain" } }) :
67
+ mime.startsWith("video/") ? _jsx("video", { controls: true, src: src }) : mime.startsWith("audio/") ? _jsx("audio", { controls: true, src: src }) :
68
+ mime === "application/pdf" ? _jsx("iframe", { title: name, src: src, style: { height: "70vh", border: 0 } }) : null), _jsx("strong", { children: name }), _jsx("a", { href: urlOf(asset, resolver, true), download: name, children: "Download" })] });
69
+ });
70
+ const Breadcrumbs = createBinderlessComponentImplementation(api("Breadcrumbs", { items: dynamic, action: CommonSchemas.Action.optional() }), ({ context }) => {
71
+ const items = useValue(context, context.componentModel.properties.items) || [];
72
+ return _jsx("nav", { "aria-label": "Folder path", style: { display: "flex", flexWrap: "wrap", gap: 4 }, children: items.map((item, i) => i === items.length - 1 ? _jsx("span", { "aria-current": "page", children: item.label }, i) :
73
+ _jsx("button", { style: button, onClick: () => void fire(context, { path: item.path }), children: item.label }, i)) });
74
+ });
75
+ const Segmented = createBinderlessComponentImplementation(api("Segmented", { options: z.array(z.object({ value: z.string(), label: z.string() })), value: dynamic, action: CommonSchemas.Action.optional() }), ({ context }) => {
76
+ const p = context.componentModel.properties;
77
+ const value = useValue(context, p.value);
78
+ return _jsx("div", { role: "group", style: { display: "flex", gap: 4 }, children: p.options.map((o) => _jsx("button", { style: { ...button, fontWeight: value === o.value ? "bold" : "normal" }, "aria-pressed": value === o.value, onClick: () => {
79
+ const binding = p.value;
80
+ if (binding?.path)
81
+ context.dataContext.set(binding.path, o.value);
82
+ void fire(context, { value: o.value });
83
+ }, children: o.label }, o.value)) });
84
+ });
85
+ const FileUpload = createBinderlessComponentImplementation(api("FileUpload", { label: dynamic, maxBytes: z.number().optional(), action: CommonSchemas.Action.optional() }), ({ context }) => {
86
+ const p = context.componentModel.properties;
87
+ const label = useValue(context, p.label) || "Upload file";
88
+ const [busy, setBusy] = useState(false);
89
+ const [error, setError] = useState("");
90
+ const limit = Math.min(Number(p.maxBytes) || AINUI_UPLOAD_MAX_BYTES, AINUI_UPLOAD_MAX_BYTES);
91
+ return _jsxs("label", { style: box, children: [label, _jsx("input", { type: "file", disabled: busy, onChange: async (e) => {
92
+ const file = e.target.files?.[0];
93
+ e.target.value = "";
94
+ if (!file)
95
+ return;
96
+ if (file.size > limit) {
97
+ setError(`Maximum file size: ${limit / 1024 / 1024} MiB`);
98
+ return;
99
+ }
100
+ setBusy(true);
101
+ setError("");
102
+ try {
103
+ const bytes = new Uint8Array(await file.arrayBuffer());
104
+ let binary = "";
105
+ for (let i = 0; i < bytes.length; i += 8192)
106
+ binary += String.fromCharCode(...bytes.subarray(i, i + 8192));
107
+ await fire(context, { name: file.name, content: btoa(binary), encoding: "base64" });
108
+ }
109
+ catch (e) {
110
+ setError(e.message);
111
+ }
112
+ finally {
113
+ setBusy(false);
114
+ }
115
+ } }), busy && _jsx("small", { children: "Uploading\u2026" }), error && _jsx("small", { role: "alert", children: error })] });
116
+ });
117
+ const X402Payment = createBinderlessComponentImplementation(api("X402Payment", { amount: dynamic, currency: dynamic, network: dynamic, payTo: dynamic, action: CommonSchemas.Action.optional() }), ({ context }) => {
118
+ const p = context.componentModel.properties;
119
+ const amount = useValue(context, p.amount);
120
+ const currency = useValue(context, p.currency);
121
+ const network = useValue(context, p.network);
122
+ const payTo = useValue(context, p.payTo);
123
+ const [busy, setBusy] = useState(false);
124
+ const [error, setError] = useState("");
125
+ return _jsxs("div", { style: box, children: [_jsxs("strong", { children: [amount, " ", currency] }), _jsxs("small", { children: ["Network: ", network] }), _jsxs("small", { style: { overflowWrap: "anywhere" }, children: ["To: ", payTo] }), _jsx("button", { style: button, disabled: busy, onClick: async () => {
126
+ if (busy)
127
+ return;
128
+ setBusy(true);
129
+ setError("");
130
+ try {
131
+ await fire(context);
132
+ }
133
+ catch (e) {
134
+ setError(e.message);
135
+ }
136
+ finally {
137
+ setBusy(false);
138
+ }
139
+ }, children: busy ? "Confirm in your wallet…" : `Pay ${amount} ${currency}` }), error && _jsx("small", { role: "alert", children: error })] });
140
+ });
141
+ // Preserve AIN-UI's confirmation extension on the basic Button.
142
+ const Button = createBinderlessComponentImplementation(api("Button", { child: CommonSchemas.ComponentId, action: CommonSchemas.Action.optional(), confirm: dynamic, variant: dynamic, tone: dynamic }), ({ context, buildChild }) => {
143
+ const p = context.componentModel.properties;
144
+ const confirm = useValue(context, p.confirm);
145
+ return _jsx("button", { style: button, onClick: () => { if (!confirm || window.confirm(confirm))
146
+ void fire(context); }, children: buildChild(String(p.child)) });
147
+ });
148
+ export const ainuiCatalog = new Catalog(AINUI_CATALOG, [...basicCatalog.components.values()].filter((c) => c.name !== "Button").concat([Button, Grid, Tile, FileView, Breadcrumbs, Segmented, FileUpload, X402Payment]), [...basicCatalog.functions.values()]);
149
+ export function AinuiSurface({ messages, onAction, resolveAsset }) {
150
+ const handler = useRef(onAction);
151
+ handler.current = onAction;
152
+ const [processor, setProcessor] = useState(null);
153
+ const [error, setError] = useState("");
154
+ useEffect(() => {
155
+ const p = new MessageProcessor([ainuiCatalog, basicCatalog], (action) => handler.current(action));
156
+ try {
157
+ p.processMessages(messages);
158
+ setProcessor(p);
159
+ setError("");
160
+ }
161
+ catch (e) {
162
+ setProcessor(null);
163
+ setError(e.message);
164
+ }
165
+ return () => { for (const surface of p.model.surfacesMap.values())
166
+ surface.dispose(); };
167
+ }, [messages]);
168
+ if (error)
169
+ return _jsx("div", { className: "ain-ui", role: "alert", children: error });
170
+ if (!processor)
171
+ return _jsx("div", { className: "ain-ui", "aria-busy": "true" });
172
+ return _jsx(MarkdownContext.Provider, { value: renderMarkdown, children: _jsx(Assets.Provider, { value: resolveAsset ?? (() => ""), children: _jsx("div", { className: "ain-ui", children: Array.from(processor.model.surfacesMap.values()).map((surface) => _jsx(A2uiSurface, { surface: surface }, surface.id)) }) }) });
173
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * aindrive's own byte routes for an AINUI asset (works where the viewer has the session cookie):
3
+ * thumb → fs/thumbnail, original → fs/stream (inline), or fs/download (attachment) with `{ download: true }`.
4
+ */
5
+ export function aindriveAssetUrl(asset: any, base?: string, { download }?: {
6
+ download?: boolean | undefined;
7
+ }): string;
8
+ /**
9
+ * @param {HTMLElement} container
10
+ * @param {{
11
+ * onAction?: (action: any) => unknown,
12
+ * resolveAsset?: (asset: { drive_id: string, path: string, variant: string, mime: string, v?: number }, opts?: { download?: boolean }) => string,
13
+ * assetBase?: string,
14
+ * }} [opts] resolveAsset: AINUI {$asset} → URL (default: aindrive's own fs routes under assetBase);
15
+ * `{ download: true }` asks for a URL that saves the original bytes (FileView's download link).
16
+ */
17
+ export function createA2uiRenderer(container: HTMLElement, { onAction, resolveAsset, assetBase }?: {
18
+ onAction?: (action: any) => unknown;
19
+ resolveAsset?: (asset: {
20
+ drive_id: string;
21
+ path: string;
22
+ variant: string;
23
+ mime: string;
24
+ v?: number;
25
+ }, opts?: {
26
+ download?: boolean;
27
+ }) => string;
28
+ assetBase?: string;
29
+ }): {
30
+ process: (messages: any) => void;
31
+ replace: (messages: any) => void;
32
+ surfaces: Map<any, any>;
33
+ };
34
+ export const A2UI_RENDERER_CSS: "\n.a2ui-surface{font:14px/1.5 system-ui,-apple-system,\"Segoe UI\",sans-serif;color:#0f1319}\n.a2ui-column,.a2ui-row,.a2ui-list{display:flex;gap:8px}\n.a2ui-list{gap:2px}\n.a2ui-card{border:1px solid #dce3ec;border-radius:12px;padding:12px;background:#fff;overflow:auto;max-height:480px}\n.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}\n.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}\n.a2ui-text-caption{font-size:12px;color:#54607a}.a2ui-text-caption p{margin:0}\n.a2ui-text pre{background:#f2f5f9;padding:8px;border-radius:8px;overflow:auto;white-space:pre-wrap;word-break:break-word}\n.a2ui-button{font:inherit;border:1px solid #dce3ec;background:#fff;border-radius:999px;padding:4px 12px;cursor:pointer}\n.a2ui-button:hover{background:#eaeef4}\n.a2ui-button-primary{background:#0b57d0;border-color:#0b57d0;color:#fff}.a2ui-button-primary:hover{background:#0842a0}\n.a2ui-button-borderless{border:0;background:none;padding:2px 4px;text-align:left}.a2ui-button-borderless:hover{background:#eaeef4}\n.a2ui-button .a2ui-text p{margin:0}\n.a2ui-textfield{display:flex;flex-direction:column;flex:1}.a2ui-textfield span{display:none}\n.a2ui-textfield input{font:inherit;border:1px solid #dce3ec;border-radius:999px;padding:4px 12px;min-width:120px}\n.a2ui-image{max-width:100%;max-height:420px;object-fit:contain}\n.a2ui-divider{border-top:1px solid #dce3ec;margin:4px 0}\n.a2ui-row .a2ui-text-caption{white-space:nowrap}\n.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}\n.a2ui-confirm{display:inline-flex;flex-wrap:wrap;align-items:center;gap:6px}\n.a2ui-text-mono{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:13px}\n.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}\n.a2ui-row{flex-wrap:wrap}\n.a2ui-grid{display:grid}\n.a2ui-tile-wrap{min-width:0}\n.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}\n.a2ui-tile:hover,.a2ui-tile:focus-visible{background:#eaeef4}\n.a2ui-tile-media{display:flex;align-items:center;justify-content:center;aspect-ratio:1;width:100%;border-radius:10px;overflow:hidden;background:#f2f5f9}\n.a2ui-tile-media img{width:100%;height:100%;object-fit:cover}\n.a2ui-tile-media .a2ui-kind-icon{font-size:40px}\n.a2ui-tile-label{font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}\n.a2ui-tile-caption{font-size:12px;color:#54607a;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}\n.a2ui-fileview{display:flex;flex-direction:column;gap:8px}\n.a2ui-fileview-img,.a2ui-fileview-video{max-width:100%;max-height:70vh;object-fit:contain;border-radius:10px;background:#f2f5f9}\n.a2ui-fileview-audio{width:100%}\n.a2ui-fileview-icon{display:flex;align-items:center;justify-content:center;height:160px;border-radius:10px;background:#f2f5f9;font-size:56px}\n.a2ui-fileview-info{display:flex;gap:8px;align-items:baseline;flex-wrap:wrap}.a2ui-fileview-name{font-weight:600;word-break:break-all}\n.a2ui-fileview-download{display:inline-flex;align-items:center;min-height:36px;box-sizing:border-box;margin-left:auto;color:inherit;text-decoration:none}\n.a2ui-breadcrumbs{display:flex;flex-wrap:wrap;align-items:center;gap:2px;font-size:13px}\n.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}\n.a2ui-crumb:hover{background:#eaeef4}.a2ui-crumb-current{color:inherit;font-weight:600;cursor:default}\n.a2ui-crumb-sep{color:#54607a}\n.a2ui-segmented{display:inline-flex;border:1px solid #dce3ec;border-radius:999px;overflow:hidden}\n.a2ui-segment{font:inherit;border:0;background:#fff;color:inherit;padding:4px 14px;cursor:pointer;min-height:36px}\n.a2ui-segment[aria-pressed=true]{background:#0b57d0;color:#fff}\n@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}}\n";