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/basic.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
export const A2UI_VERSION = "v0.9";
|
|
2
|
+
export const A2UI_MIME = "application/a2ui+json";
|
|
3
|
+
export const A2UI_BASIC_CATALOG = "https://a2ui.org/specification/v0_9/catalogs/basic/catalog.json";
|
|
4
|
+
/** A2A extension URI (A2UI over A2A), advertised in the agent card. */
|
|
5
|
+
export const A2UI_A2A_EXTENSION = "https://a2ui.org/a2a-extension/a2ui/v0.9";
|
|
6
|
+
/** AG-UI activity type used by @ag-ui/a2ui-middleware / CopilotKit. */
|
|
7
|
+
export const AGUI_A2UI_ACTIVITY = "a2ui-surface";
|
|
8
|
+
export const AGUI_A2UI_OPERATIONS_KEY = "a2ui_operations";
|
|
9
|
+
/** Where MCP tool results carry the surface for UI-only consumption (not shown to the model). */
|
|
10
|
+
export const A2UI_META_KEY = "ai.aindrive/a2ui";
|
|
11
|
+
// ── component helpers ──────────────────────────────────────────────────────
|
|
12
|
+
const text = (id, t, variant) => ({ id, component: "Text", text: t, ...(variant ? { variant } : {}) });
|
|
13
|
+
const button = (id, child, name, context, variant) => ({ id, component: "Button", child, action: { event: { name, context } }, ...(variant ? { variant } : {}) });
|
|
14
|
+
const row = (id, children, extra = {}) => ({ id, component: "Row", children, ...extra });
|
|
15
|
+
const column = (id, children) => ({ id, component: "Column", children });
|
|
16
|
+
function surface(prefix, components, data) {
|
|
17
|
+
const surfaceId = `aindrive-${prefix}-${Math.random().toString(36).slice(2, 10)}`;
|
|
18
|
+
return [
|
|
19
|
+
{ version: A2UI_VERSION, createSurface: { surfaceId, catalogId: A2UI_BASIC_CATALOG } },
|
|
20
|
+
{ version: A2UI_VERSION, updateComponents: { surfaceId, components } },
|
|
21
|
+
{ version: A2UI_VERSION, updateDataModel: { surfaceId, path: "/", value: data } },
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
export function humanSize(n) {
|
|
25
|
+
if (typeof n !== "number" || !Number.isFinite(n))
|
|
26
|
+
return "";
|
|
27
|
+
const u = ["B", "KB", "MB", "GB", "TB"];
|
|
28
|
+
let i = 0;
|
|
29
|
+
let v = n;
|
|
30
|
+
while (v >= 1024 && i < u.length - 1) {
|
|
31
|
+
v /= 1024;
|
|
32
|
+
i++;
|
|
33
|
+
}
|
|
34
|
+
return `${i === 0 ? v : v.toFixed(1)} ${u[i]}`;
|
|
35
|
+
}
|
|
36
|
+
export const parentOf = (p) => (p.includes("/") ? p.slice(0, p.lastIndexOf("/")) : "");
|
|
37
|
+
export const baseOf = (p) => (p.includes("/") ? p.slice(p.lastIndexOf("/") + 1) : p);
|
|
38
|
+
/**
|
|
39
|
+
* A browsable list: title, optional toolbar (up + search box), and a templated
|
|
40
|
+
* List whose rows are borderless buttons firing `aindrive.open`
|
|
41
|
+
* (or `aindrive.open_drive` for the drive picker).
|
|
42
|
+
*/
|
|
43
|
+
function listSurface(prefix, opts) {
|
|
44
|
+
const open = opts.openAction ?? "aindrive.open";
|
|
45
|
+
const rootChildren = ["title"];
|
|
46
|
+
const comps = [text("title", { path: "/title" }, "h3")];
|
|
47
|
+
if (opts.toolbar) {
|
|
48
|
+
rootChildren.push("toolbar");
|
|
49
|
+
const tb = ["search_field", "search_btn"];
|
|
50
|
+
if (opts.path)
|
|
51
|
+
tb.unshift("up_btn");
|
|
52
|
+
comps.push(row("toolbar", tb, { align: "center" }), button("up_btn", "up_label", "aindrive.open", { drive_id: { path: "/drive_id" }, path: { path: "/parent" }, is_dir: true }), text("up_label", "⬆ Up"), { id: "search_field", component: "TextField", label: "Search file names", value: { path: "/query" }, weight: 1 }, button("search_btn", "search_label", "aindrive.search", { drive_id: { path: "/drive_id" }, query: { path: "/query" }, path: { path: "/path" } }, "primary"), text("search_label", "Search"));
|
|
53
|
+
}
|
|
54
|
+
rootChildren.push(opts.items.length ? "list" : "empty");
|
|
55
|
+
comps.push({ id: "list", component: "List", children: { componentId: "item", path: "/items" } }, row("item", ["item_open", "item_meta"], { align: "center", justify: "spaceBetween" }), button("item_open", "item_label", open, open === "aindrive.open_drive"
|
|
56
|
+
? { drive_id: { path: "drive_id" } }
|
|
57
|
+
: { drive_id: { path: "drive_id" }, path: { path: "path" }, is_dir: { path: "is_dir" } }, "borderless"), text("item_label", { path: "label" }), text("item_meta", { path: "meta" }, "caption"), text("empty", "(empty)", "caption"));
|
|
58
|
+
if (opts.note) {
|
|
59
|
+
rootChildren.push("note");
|
|
60
|
+
comps.push(text("note", opts.note, "caption"));
|
|
61
|
+
}
|
|
62
|
+
comps.unshift(column("root", rootChildren));
|
|
63
|
+
return surface(prefix, comps, {
|
|
64
|
+
title: opts.title, drive_id: opts.driveId, path: opts.path, parent: parentOf(opts.path), query: "", items: opts.items,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function cardSurface(prefix, title, body, driveId, folder) {
|
|
68
|
+
const rootChildren = ["title", "card"];
|
|
69
|
+
const comps = [
|
|
70
|
+
text("title", { path: "/title" }, "h3"),
|
|
71
|
+
{ id: "card", component: "Card", child: "body" },
|
|
72
|
+
text("body", { path: "/body" }),
|
|
73
|
+
];
|
|
74
|
+
if (driveId !== undefined && folder !== undefined) {
|
|
75
|
+
rootChildren.push("open_btn");
|
|
76
|
+
comps.push(button("open_btn", "open_label", "aindrive.open", { drive_id: { path: "/drive_id" }, path: { path: "/folder" }, is_dir: true }), text("open_label", "📁 Open folder"));
|
|
77
|
+
}
|
|
78
|
+
comps.unshift(column("root", rootChildren));
|
|
79
|
+
return surface(prefix, comps, { title, body, drive_id: driveId ?? "", folder: folder ?? "" });
|
|
80
|
+
}
|
|
81
|
+
const MAX_PREVIEW_CHARS = 20_000;
|
|
82
|
+
/** Inline image cap (base64 chars ≈ 1.5 MB of image) — larger ones get a note, not a data URL. */
|
|
83
|
+
const MAX_INLINE_IMAGE_B64 = 2_000_000;
|
|
84
|
+
const IMAGE_EXT = /\.(png|jpe?g|gif|webp|svg)$/i;
|
|
85
|
+
function previewSurface(driveId, path, r) {
|
|
86
|
+
let content = typeof r.content === "string" ? r.content : "";
|
|
87
|
+
let encoding = r.encoding;
|
|
88
|
+
// An SVG read as text is still an image.
|
|
89
|
+
if (encoding !== "base64" && /\.svg$/i.test(path) && content.trimStart().startsWith("<")) {
|
|
90
|
+
content = typeof btoa === "function" ? btoa(unescape(encodeURIComponent(content))) : Buffer.from(content, "utf8").toString("base64");
|
|
91
|
+
encoding = "base64";
|
|
92
|
+
}
|
|
93
|
+
const isImage = encoding === "base64" && IMAGE_EXT.test(path) && content.length <= MAX_INLINE_IMAGE_B64;
|
|
94
|
+
const comps = [
|
|
95
|
+
column("root", ["title", "toolbar", "card"]),
|
|
96
|
+
text("title", { path: "/title" }, "h3"),
|
|
97
|
+
row("toolbar", ["back_btn"]),
|
|
98
|
+
button("back_btn", "back_label", "aindrive.open", { drive_id: { path: "/drive_id" }, path: { path: "/parent" }, is_dir: true }),
|
|
99
|
+
text("back_label", "⬅ Back to folder"),
|
|
100
|
+
{ id: "card", component: "Card", child: "body" },
|
|
101
|
+
];
|
|
102
|
+
const data = { title: `📄 ${baseOf(path)}`, drive_id: driveId, parent: parentOf(path) };
|
|
103
|
+
if (isImage) {
|
|
104
|
+
const ext = path.slice(path.lastIndexOf(".") + 1).toLowerCase();
|
|
105
|
+
const mime = ext === "svg" ? "image/svg+xml" : `image/${ext === "jpg" ? "jpeg" : ext}`;
|
|
106
|
+
comps.push({ id: "body", component: "Image", url: { path: "/image_url" }, fit: "contain", variant: "largeFeature" });
|
|
107
|
+
data.image_url = `data:${mime};base64,${content}`;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
comps.push(text("body", { path: "/content" }));
|
|
111
|
+
const shown = encoding === "base64"
|
|
112
|
+
? `(${IMAGE_EXT.test(path) ? "image too large to preview" : "binary file"}, ${humanSize(Math.floor(content.length * 3 / 4))})`
|
|
113
|
+
: content;
|
|
114
|
+
const clipped = shown.length > MAX_PREVIEW_CHARS ? `${shown.slice(0, MAX_PREVIEW_CHARS)}\n…(truncated)` : shown;
|
|
115
|
+
data.content = /\.(md|markdown)$/i.test(path) || encoding === "base64" ? clipped : "```\n" + clipped + "\n```";
|
|
116
|
+
}
|
|
117
|
+
return surface("preview", comps, data);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Build the A2UI surface for one skill call. `driveId` is the drive the call
|
|
121
|
+
* ran against (from args or the pinned ctx); `args` are the skill arguments.
|
|
122
|
+
*/
|
|
123
|
+
export function a2uiForSkill(skill, args, result, driveId) {
|
|
124
|
+
const drive = (typeof args.drive_id === "string" && args.drive_id) || driveId || "";
|
|
125
|
+
const path = typeof args.path === "string" ? args.path.replace(/^\/+|\/+$/g, "") : "";
|
|
126
|
+
if (result.kind === "err") {
|
|
127
|
+
return cardSurface("error", "⚠️ Something went wrong", `[${result.code}] ${result.message}`);
|
|
128
|
+
}
|
|
129
|
+
const s = (result.structured ?? {});
|
|
130
|
+
switch (skill) {
|
|
131
|
+
case "list_drives": {
|
|
132
|
+
const drives = (s.drives ?? []);
|
|
133
|
+
return listSurface("drives", {
|
|
134
|
+
title: "Your drives", driveId: "", path: "", toolbar: false, openAction: "aindrive.open_drive",
|
|
135
|
+
items: drives.map((d) => ({ label: `💽 ${d.name}`, meta: d.id, path: "", is_dir: true, drive_id: d.id })),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
case "list_files": {
|
|
139
|
+
const entries = (s.entries ?? []);
|
|
140
|
+
return listSurface("browser", {
|
|
141
|
+
title: `📁 /${path}`, driveId: drive, path, toolbar: true,
|
|
142
|
+
items: entries.map((e) => ({
|
|
143
|
+
label: `${e.isDir ? "📁" : "📄"} ${e.name}${e.locked ? " 🔒" : ""}`,
|
|
144
|
+
meta: e.isDir ? "folder" : humanSize(e.size),
|
|
145
|
+
path: path ? `${path}/${e.name}` : e.name,
|
|
146
|
+
is_dir: !!e.isDir,
|
|
147
|
+
drive_id: drive,
|
|
148
|
+
})),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
case "search": {
|
|
152
|
+
const matches = (s.matches ?? []);
|
|
153
|
+
return listSurface("search", {
|
|
154
|
+
title: `🔍 "${String(args.query ?? "")}"`, driveId: drive, path, toolbar: true,
|
|
155
|
+
note: s.truncated ? "Showing the first results only — refine the query for more." : undefined,
|
|
156
|
+
items: matches.map((m) => ({
|
|
157
|
+
label: `${m.isDir ? "📁" : "📄"} ${m.path}${m.locked ? " 🔒" : ""}`,
|
|
158
|
+
meta: m.isDir ? "folder" : "", path: m.path, is_dir: !!m.isDir, drive_id: drive,
|
|
159
|
+
})),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
case "read_file":
|
|
163
|
+
return previewSurface(drive, path, s);
|
|
164
|
+
case "write_file":
|
|
165
|
+
return cardSurface("result", "✅ Saved", `Wrote /${path}`, drive, parentOf(path));
|
|
166
|
+
case "delete_path":
|
|
167
|
+
return cardSurface("result", "🗑 Deleted", `Deleted ${s.kind === "folder" ? "folder" : "file"} /${path}`, drive, parentOf(path));
|
|
168
|
+
case "stat": {
|
|
169
|
+
const e = s;
|
|
170
|
+
const lines = [
|
|
171
|
+
`Type: ${e.isDir ? "folder" : "file"}`,
|
|
172
|
+
e.isDir ? "" : `Size: ${humanSize(e.size)}`,
|
|
173
|
+
e.mtimeMs ? `Modified: ${new Date(e.mtimeMs).toISOString()}` : "",
|
|
174
|
+
].filter(Boolean).join("\n\n");
|
|
175
|
+
return cardSurface("stat", `ℹ️ /${path}`, lines, drive, e.isDir ? path : parentOf(path));
|
|
176
|
+
}
|
|
177
|
+
default:
|
|
178
|
+
return cardSurface("result", skill, result.text);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// ── actions (renderer → agent) ─────────────────────────────────────────────
|
|
182
|
+
/**
|
|
183
|
+
* Accept the shapes renderers actually send: a v0.9 client event
|
|
184
|
+
* `{version, action}`, a bare `action` object, `{action}`, or AG-UI/CopilotKit's
|
|
185
|
+
* `{userAction}` (forwardedProps.a2uiAction).
|
|
186
|
+
*/
|
|
187
|
+
export function parseA2uiAction(input) {
|
|
188
|
+
if (!input || typeof input !== "object")
|
|
189
|
+
return null;
|
|
190
|
+
const o = input;
|
|
191
|
+
const candidate = (o.action ?? o.userAction ?? o);
|
|
192
|
+
if (typeof candidate?.name !== "string")
|
|
193
|
+
return null;
|
|
194
|
+
const context = candidate.context && typeof candidate.context === "object" ? candidate.context : {};
|
|
195
|
+
return { ...candidate, name: candidate.name, context };
|
|
196
|
+
}
|
|
197
|
+
/** Map an aindrive A2UI action to the skill call that answers it. */
|
|
198
|
+
export function actionToSkill(action, fallbackDriveId) {
|
|
199
|
+
const c = action.context ?? {};
|
|
200
|
+
const drive_id = (typeof c.drive_id === "string" && c.drive_id) || fallbackDriveId;
|
|
201
|
+
const str = (v) => (typeof v === "string" ? v : "");
|
|
202
|
+
switch (action.name) {
|
|
203
|
+
case "aindrive.open_drive":
|
|
204
|
+
if (!drive_id)
|
|
205
|
+
return { error: "aindrive.open_drive needs context.drive_id" };
|
|
206
|
+
return { skill: "list_files", args: { drive_id, path: "" } };
|
|
207
|
+
case "aindrive.open": {
|
|
208
|
+
if (!drive_id)
|
|
209
|
+
return { error: "aindrive.open needs context.drive_id" };
|
|
210
|
+
const isDir = c.is_dir === true || c.is_dir === "true";
|
|
211
|
+
if (isDir)
|
|
212
|
+
return { skill: "list_files", args: { drive_id, path: str(c.path) } };
|
|
213
|
+
// Images are previewed inline, so fetch their bytes.
|
|
214
|
+
return { skill: "read_file", args: { drive_id, path: str(c.path), ...(IMAGE_EXT.test(str(c.path)) ? { encoding: "base64" } : {}) } };
|
|
215
|
+
}
|
|
216
|
+
case "aindrive.search": {
|
|
217
|
+
if (!drive_id)
|
|
218
|
+
return { error: "aindrive.search needs context.drive_id" };
|
|
219
|
+
const query = str(c.query).trim();
|
|
220
|
+
if (!query)
|
|
221
|
+
return { skill: "list_files", args: { drive_id, path: str(c.path) } };
|
|
222
|
+
return { skill: "search", args: { drive_id, query, path: str(c.path) } };
|
|
223
|
+
}
|
|
224
|
+
default:
|
|
225
|
+
return { error: `unknown action: ${action.name}` };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/** Tiny text-command grammar shared by A2A TextParts and AG-UI user messages. */
|
|
229
|
+
export function commandToSkill(textIn, driveId) {
|
|
230
|
+
const t = textIn.trim();
|
|
231
|
+
const [cmd, ...rest] = t.split(/\s+/);
|
|
232
|
+
const arg = rest.join(" ");
|
|
233
|
+
const withDrive = (args) => (driveId ? { drive_id: driveId, ...args } : args);
|
|
234
|
+
switch ((cmd ?? "").toLowerCase()) {
|
|
235
|
+
case "drives": return { skill: "list_drives", args: {} };
|
|
236
|
+
case "ls": return { skill: "list_files", args: withDrive({ path: arg }) };
|
|
237
|
+
case "cat":
|
|
238
|
+
case "open": return { skill: "read_file", args: withDrive({ path: arg }) };
|
|
239
|
+
case "stat": return { skill: "stat", args: withDrive({ path: arg }) };
|
|
240
|
+
case "find":
|
|
241
|
+
case "search": return { skill: "search", args: withDrive({ query: arg }) };
|
|
242
|
+
default:
|
|
243
|
+
if (!t)
|
|
244
|
+
return driveId ? { skill: "list_files", args: withDrive({ path: "" }) } : { skill: "list_drives", args: {} };
|
|
245
|
+
return driveId ? { skill: "search", args: withDrive({ query: t }) } : { skill: "list_drives", args: {} };
|
|
246
|
+
}
|
|
247
|
+
}
|