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/ainui.js
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { A2UI_BASIC_CATALOG, A2UI_VERSION, a2uiForSkill, baseOf, humanSize, parentOf, } from "./basic.js";
|
|
2
|
+
export const AINUI_UPLOAD_MAX_BYTES = 8 * 1024 * 1024;
|
|
3
|
+
export const AINUI_VERSION = "v1";
|
|
4
|
+
export const AINUI_CATALOG = "https://aindrive.ainetwork.ai/ainui/v1/catalog.json";
|
|
5
|
+
/** MCP request header a client sends to get AINUI surfaces. */
|
|
6
|
+
export const AINUI_HEADER = "x-ainui";
|
|
7
|
+
/** Files larger than this open read-only (no edit button; aindrive.edit refuses). */
|
|
8
|
+
export const MAX_EDIT_BYTES = 1024 * 1024;
|
|
9
|
+
const NEW_FILE_BASE = "untitled";
|
|
10
|
+
const NEW_FILE_EXT = ".txt";
|
|
11
|
+
// ── mime / kind ─────────────────────────────────────────────────────────────
|
|
12
|
+
// Fallback used only when no mimeOf is injected (client code, e.g. the playground).
|
|
13
|
+
const EXT_MIME = {
|
|
14
|
+
png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp",
|
|
15
|
+
svg: "image/svg+xml", avif: "image/avif", heic: "image/heic", bmp: "image/bmp",
|
|
16
|
+
mp4: "video/mp4", m4v: "video/x-m4v", webm: "video/webm", mov: "video/quicktime", mkv: "video/x-matroska",
|
|
17
|
+
mp3: "audio/mpeg", m4a: "audio/mp4", wav: "audio/wav", ogg: "audio/ogg", flac: "audio/flac", aac: "audio/aac",
|
|
18
|
+
pdf: "application/pdf",
|
|
19
|
+
txt: "text/plain", md: "text/markdown", csv: "text/csv", html: "text/html", css: "text/css",
|
|
20
|
+
js: "application/javascript", ts: "application/typescript", json: "application/json", yaml: "application/yaml", yml: "application/yaml",
|
|
21
|
+
};
|
|
22
|
+
const TEXT_MIME_EXTRA = ["application/json", "application/xml", "application/yaml", "application/x-yaml", "application/javascript", "application/typescript", "application/x-sh"];
|
|
23
|
+
const OCTET = "application/octet-stream";
|
|
24
|
+
function fallbackMime(path) {
|
|
25
|
+
const dot = path.lastIndexOf(".");
|
|
26
|
+
return dot < 0 ? null : EXT_MIME[path.slice(dot + 1).toLowerCase()] ?? null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The agent's per-entry mime (cli/src/rpc.js, mobile RpcHandler) wins unless it
|
|
30
|
+
* is missing or generic; then the file name decides.
|
|
31
|
+
*/
|
|
32
|
+
export function resolveMime(path, agentMime, env = {}) {
|
|
33
|
+
if (typeof agentMime === "string" && agentMime && agentMime !== "folder" && agentMime !== OCTET)
|
|
34
|
+
return agentMime;
|
|
35
|
+
return (env.mimeOf ?? fallbackMime)(path) || OCTET;
|
|
36
|
+
}
|
|
37
|
+
export function kindOf(mime, isDir) {
|
|
38
|
+
if (isDir)
|
|
39
|
+
return "folder";
|
|
40
|
+
if (mime.startsWith("image/"))
|
|
41
|
+
return "image";
|
|
42
|
+
if (mime.startsWith("video/"))
|
|
43
|
+
return "video";
|
|
44
|
+
if (mime.startsWith("audio/"))
|
|
45
|
+
return "audio";
|
|
46
|
+
if (mime === "application/pdf")
|
|
47
|
+
return "pdf";
|
|
48
|
+
return "file";
|
|
49
|
+
}
|
|
50
|
+
function isText(path, mime, env) {
|
|
51
|
+
if (env.isText)
|
|
52
|
+
return env.isText(path, mime);
|
|
53
|
+
return mime.startsWith("text/") || TEXT_MIME_EXTRA.some((p) => mime === p || mime.startsWith(p + ";"));
|
|
54
|
+
}
|
|
55
|
+
const KIND_EMOJI = { folder: "📁", image: "🖼", video: "🎬", audio: "🎵", pdf: "📕", file: "📄" };
|
|
56
|
+
const THUMB_KINDS = ["image", "video", "pdf"];
|
|
57
|
+
export function assetRef(driveId, path, variant, mime, v) {
|
|
58
|
+
return { $asset: { drive_id: driveId, path, variant, mime, ...(typeof v === "number" && Number.isFinite(v) ? { v } : {}) } };
|
|
59
|
+
}
|
|
60
|
+
export function toItem(driveId, path, e, env, meta) {
|
|
61
|
+
const isDir = !!e.isDir;
|
|
62
|
+
const mime = isDir ? "inode/directory" : resolveMime(path, e.mime, env);
|
|
63
|
+
const kind = kindOf(mime, isDir);
|
|
64
|
+
const mtime = typeof e.mtimeMs === "number" && Number.isFinite(e.mtimeMs) ? e.mtimeMs : null;
|
|
65
|
+
const size = !isDir && typeof e.size === "number" && Number.isFinite(e.size) ? e.size : null;
|
|
66
|
+
const lock = e.locked ? "🔒 " : "";
|
|
67
|
+
return {
|
|
68
|
+
name: e.name,
|
|
69
|
+
path,
|
|
70
|
+
is_dir: isDir,
|
|
71
|
+
kind,
|
|
72
|
+
mime,
|
|
73
|
+
size,
|
|
74
|
+
mtime,
|
|
75
|
+
meta: lock + (meta ?? (isDir ? "folder" : humanSize(size))),
|
|
76
|
+
drive_id: driveId,
|
|
77
|
+
// Locked (paid, not bought) entries get no thumb: the carve-out 402s the bytes anyway.
|
|
78
|
+
thumb: THUMB_KINDS.includes(kind) && !e.locked ? assetRef(driveId, path, "thumb", mime, mtime) : null,
|
|
79
|
+
label: `${KIND_EMOJI[kind]} ${e.name}${e.locked ? " 🔒" : ""}`,
|
|
80
|
+
...(e.locked ? { locked: true } : {}),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** Grid when at least half of the folder's FILES are images/videos (folders don't count). */
|
|
84
|
+
export function defaultView(items) {
|
|
85
|
+
const files = items.filter((i) => !i.is_dir);
|
|
86
|
+
const media = files.filter((i) => i.kind === "image" || i.kind === "video").length;
|
|
87
|
+
return files.length > 0 && media * 2 >= files.length ? "grid" : "list";
|
|
88
|
+
}
|
|
89
|
+
const ev = (name, context) => ({ event: { name, context } });
|
|
90
|
+
const text = (id, t, variant) => ({ id, component: "Text", text: t, ...(variant ? { variant } : {}) });
|
|
91
|
+
const button = (id, child, name, context, extra = {}) => ({ id, component: "Button", child, action: ev(name, context), ...extra });
|
|
92
|
+
const row = (id, children, extra = {}) => ({ id, component: "Row", children, ...extra });
|
|
93
|
+
const column = (id, children) => ({ id, component: "Column", children });
|
|
94
|
+
const DRIVE = { path: "/drive_id" };
|
|
95
|
+
function ainuiSurface(prefix, components, data) {
|
|
96
|
+
const surfaceId = `aindrive-${prefix}-${Math.random().toString(36).slice(2, 10)}`;
|
|
97
|
+
return [
|
|
98
|
+
{ version: A2UI_VERSION, createSurface: { surfaceId, catalogId: AINUI_CATALOG } },
|
|
99
|
+
{ version: A2UI_VERSION, updateComponents: { surfaceId, components } },
|
|
100
|
+
{ version: A2UI_VERSION, updateDataModel: { surfaceId, path: "/", value: data } },
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
/** A basic-catalog surface is a valid AINUI surface: only the declared catalog changes. */
|
|
104
|
+
export function asAinui(messages) {
|
|
105
|
+
return messages.map((m) => ("createSurface" in m && m.createSurface.catalogId === A2UI_BASIC_CATALOG
|
|
106
|
+
? { ...m, createSurface: { ...m.createSurface, catalogId: AINUI_CATALOG } }
|
|
107
|
+
: m));
|
|
108
|
+
}
|
|
109
|
+
/** Root first; the last item is the current location (renderers draw it as plain text). */
|
|
110
|
+
export function crumbsFor(path, isDir, rootLabel = "/") {
|
|
111
|
+
const segs = path ? path.split("/") : [];
|
|
112
|
+
return [
|
|
113
|
+
{ label: rootLabel, path: "", is_dir: true },
|
|
114
|
+
...segs.map((s, i) => ({ label: s, path: segs.slice(0, i + 1).join("/"), is_dir: i < segs.length - 1 || isDir })),
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
const breadcrumbs = () => ({
|
|
118
|
+
id: "crumbs",
|
|
119
|
+
component: "Breadcrumbs",
|
|
120
|
+
items: { path: "/crumbs" },
|
|
121
|
+
// `path` binds to the tapped crumb (template-relative), per spec §3.
|
|
122
|
+
action: ev("aindrive.open", { drive_id: DRIVE, path: { path: "path" }, is_dir: true }),
|
|
123
|
+
});
|
|
124
|
+
const cleanPath = (p) => (typeof p === "string" ? p.replace(/^\/+|\/+$/g, "") : "");
|
|
125
|
+
// ── surfaces ───────────────────────────────────────────────────────────────
|
|
126
|
+
export function ainuiFolder(opts) {
|
|
127
|
+
const env = opts.env ?? {};
|
|
128
|
+
const view = env.view ?? defaultView(opts.items);
|
|
129
|
+
const toolbar = ["search_field", "search_btn", "view"];
|
|
130
|
+
if (env.canWrite)
|
|
131
|
+
toolbar.push("new_btn", "upload");
|
|
132
|
+
const body = opts.items.length ? (view === "grid" ? "grid" : "list") : "empty";
|
|
133
|
+
const rootChildren = ["crumbs", "toolbar", body, ...(opts.note ? ["note"] : [])];
|
|
134
|
+
const comps = [
|
|
135
|
+
column("root", rootChildren),
|
|
136
|
+
breadcrumbs(),
|
|
137
|
+
row("toolbar", toolbar, { align: "center" }),
|
|
138
|
+
{ id: "search_field", component: "TextField", label: "Search file names", value: { path: "/query" }, weight: 1 },
|
|
139
|
+
button("search_btn", "search_label", "aindrive.search", { drive_id: DRIVE, query: { path: "/query" }, path: { path: "/path" } }, { variant: "primary" }),
|
|
140
|
+
text("search_label", "Search"),
|
|
141
|
+
{
|
|
142
|
+
id: "view",
|
|
143
|
+
component: "Segmented",
|
|
144
|
+
options: [{ value: "list", label: "List" }, { value: "grid", label: "Grid" }],
|
|
145
|
+
value: { path: "/view" },
|
|
146
|
+
action: ev("aindrive.view", { drive_id: DRIVE, path: { path: "/path" }, value: { path: "/view" } }),
|
|
147
|
+
},
|
|
148
|
+
];
|
|
149
|
+
if (env.canWrite) {
|
|
150
|
+
comps.push(button("new_btn", "new_label", "aindrive.new_file", { drive_id: DRIVE, path: { path: "/path" }, name: { path: "/new_name" } }), text("new_label", "New file"), { id: "upload", component: "FileUpload", label: "Upload file", maxBytes: AINUI_UPLOAD_MAX_BYTES,
|
|
151
|
+
action: ev("aindrive.upload", { drive_id: DRIVE, path: { path: "/path" } }) });
|
|
152
|
+
}
|
|
153
|
+
const open = ev("aindrive.open", { drive_id: { path: "drive_id" }, path: { path: "path" }, is_dir: { path: "is_dir" } });
|
|
154
|
+
if (body === "grid") {
|
|
155
|
+
comps.push({ id: "grid", component: "Grid", children: { componentId: "tile", path: "/items" }, minItemWidth: 128, gap: 8 }, {
|
|
156
|
+
id: "tile", component: "Tile",
|
|
157
|
+
media: { path: "thumb" }, kind: { path: "kind" }, label: { path: "name" }, caption: { path: "meta" }, action: open,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else if (body === "list") {
|
|
161
|
+
comps.push({ id: "list", component: "List", children: { componentId: "row", path: "/items" } }, row("row", ["row_open", "row_meta"], { align: "center", justify: "spaceBetween" }), { id: "row_open", component: "Button", child: "row_label", action: open, variant: "borderless" }, text("row_label", { path: "label" }), text("row_meta", { path: "meta" }, "caption"));
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
comps.push(text("empty", "(empty)", "caption"));
|
|
165
|
+
}
|
|
166
|
+
if (opts.note)
|
|
167
|
+
comps.push(text("note", opts.note, "caption"));
|
|
168
|
+
return ainuiSurface("folder", comps, {
|
|
169
|
+
drive_id: opts.driveId,
|
|
170
|
+
path: opts.path,
|
|
171
|
+
parent: parentOf(opts.path),
|
|
172
|
+
title: opts.path ? baseOf(opts.path) : (env.rootLabel ?? "/"),
|
|
173
|
+
crumbs: crumbsFor(opts.path, true, env.rootLabel),
|
|
174
|
+
view,
|
|
175
|
+
query: opts.query ?? "",
|
|
176
|
+
new_name: "",
|
|
177
|
+
items: opts.items,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
export function ainuiFile(opts) {
|
|
181
|
+
const env = opts.env ?? {};
|
|
182
|
+
const name = baseOf(opts.path);
|
|
183
|
+
const editable = !!env.canWrite && isText(opts.path, opts.mime, env) && (opts.size === null || opts.size <= MAX_EDIT_BYTES);
|
|
184
|
+
const toolbar = ["back_btn", ...(editable ? ["edit_btn"] : []), ...(env.canDelete ? ["delete_btn"] : [])];
|
|
185
|
+
const comps = [
|
|
186
|
+
column("root", ["crumbs", "toolbar", "view"]),
|
|
187
|
+
breadcrumbs(),
|
|
188
|
+
row("toolbar", toolbar, { align: "center" }),
|
|
189
|
+
button("back_btn", "back_label", "aindrive.open", { drive_id: DRIVE, path: { path: "/parent" }, is_dir: true }),
|
|
190
|
+
text("back_label", "Back"),
|
|
191
|
+
];
|
|
192
|
+
if (editable) {
|
|
193
|
+
comps.push(button("edit_btn", "edit_label", "aindrive.edit", { drive_id: DRIVE, path: { path: "/path" } }), text("edit_label", "Edit"));
|
|
194
|
+
}
|
|
195
|
+
if (env.canDelete) {
|
|
196
|
+
comps.push(button("delete_btn", "delete_label", "aindrive.delete", { drive_id: DRIVE, path: { path: "/path" } }, { tone: "danger", confirm: `Delete ${name}?` }), text("delete_label", "Delete"));
|
|
197
|
+
}
|
|
198
|
+
comps.push({
|
|
199
|
+
id: "view", component: "FileView",
|
|
200
|
+
src: { path: "/src" }, name: { path: "/name" }, mime: { path: "/mime" }, size: { path: "/size" },
|
|
201
|
+
});
|
|
202
|
+
return ainuiSurface("file", comps, {
|
|
203
|
+
drive_id: opts.driveId,
|
|
204
|
+
path: opts.path,
|
|
205
|
+
parent: parentOf(opts.path),
|
|
206
|
+
title: name,
|
|
207
|
+
crumbs: crumbsFor(opts.path, false, env.rootLabel),
|
|
208
|
+
name,
|
|
209
|
+
mime: opts.mime,
|
|
210
|
+
kind: kindOf(opts.mime, false),
|
|
211
|
+
size: opts.size,
|
|
212
|
+
mtime: opts.mtime,
|
|
213
|
+
src: assetRef(opts.driveId, opts.path, "original", opts.mime, opts.mtime),
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
export function ainuiEditor(opts) {
|
|
217
|
+
const env = opts.env ?? {};
|
|
218
|
+
const name = baseOf(opts.path);
|
|
219
|
+
const comps = [
|
|
220
|
+
column("root", ["crumbs", "editor", "actions"]),
|
|
221
|
+
breadcrumbs(),
|
|
222
|
+
{ id: "editor", component: "TextField", label: name, value: { path: "/content" }, variant: "longText" },
|
|
223
|
+
row("actions", ["cancel_btn", "save_btn"], { align: "center", justify: "end" }),
|
|
224
|
+
button("cancel_btn", "cancel_label", "aindrive.open", { drive_id: DRIVE, path: { path: "/path" }, is_dir: false }),
|
|
225
|
+
text("cancel_label", "Cancel"),
|
|
226
|
+
button("save_btn", "save_label", "aindrive.save", { drive_id: DRIVE, path: { path: "/path" }, content: { path: "/content" } }, { variant: "primary" }),
|
|
227
|
+
text("save_label", "Save"),
|
|
228
|
+
];
|
|
229
|
+
return ainuiSurface("editor", comps, {
|
|
230
|
+
drive_id: opts.driveId,
|
|
231
|
+
path: opts.path,
|
|
232
|
+
parent: parentOf(opts.path),
|
|
233
|
+
title: name,
|
|
234
|
+
crumbs: crumbsFor(opts.path, false, env.rootLabel),
|
|
235
|
+
name,
|
|
236
|
+
content: opts.content,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function utf8Bytes(s) {
|
|
240
|
+
return typeof TextEncoder === "function" ? new TextEncoder().encode(s).length : s.length;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* The AINUI surface for one skill call (tool result, AG-UI run, A2A reply).
|
|
244
|
+
* Folder skills get the folder surface, file skills the file surface; the
|
|
245
|
+
* rest (drive list, write/delete results, errors) are the basic surfaces
|
|
246
|
+
* relabelled with the AINUI catalog — AINUI includes the basic catalog.
|
|
247
|
+
*/
|
|
248
|
+
export function ainuiForSkill(skill, args, result, driveId, env = {}) {
|
|
249
|
+
const drive = (typeof args.drive_id === "string" && args.drive_id) || driveId || "";
|
|
250
|
+
const path = cleanPath(args.path);
|
|
251
|
+
if (result.kind === "err")
|
|
252
|
+
return asAinui(a2uiForSkill(skill, args, result, driveId));
|
|
253
|
+
const s = (result.structured ?? {});
|
|
254
|
+
switch (skill) {
|
|
255
|
+
case "list_files": {
|
|
256
|
+
const entries = (s.entries ?? []);
|
|
257
|
+
return ainuiFolder({
|
|
258
|
+
driveId: drive, path, env,
|
|
259
|
+
items: entries.map((e) => toItem(drive, path ? `${path}/${e.name}` : e.name, e, env)),
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
case "search": {
|
|
263
|
+
const matches = (s.matches ?? []);
|
|
264
|
+
return ainuiFolder({
|
|
265
|
+
driveId: drive, path, env, query: typeof args.query === "string" ? args.query : "",
|
|
266
|
+
note: s.truncated ? "Showing the first results only — refine the query for more." : undefined,
|
|
267
|
+
// search reports no size/mtime: the caption is the match's folder, thumbs carry no `v`.
|
|
268
|
+
items: matches.map((m) => toItem(drive, m.path, { name: baseOf(m.path), isDir: m.isDir, locked: m.locked }, env, `/${parentOf(m.path)}`)),
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
case "stat": {
|
|
272
|
+
const e = s;
|
|
273
|
+
if (e.isDir || e.locked || !path)
|
|
274
|
+
return asAinui(a2uiForSkill(skill, args, result, driveId));
|
|
275
|
+
return ainuiFile({
|
|
276
|
+
driveId: drive, path, env, mime: resolveMime(path, e.mime, env),
|
|
277
|
+
size: typeof e.size === "number" ? e.size : null,
|
|
278
|
+
mtime: typeof e.mtimeMs === "number" ? e.mtimeMs : null,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
case "read_file": {
|
|
282
|
+
if (!path)
|
|
283
|
+
return asAinui(a2uiForSkill(skill, args, result, driveId));
|
|
284
|
+
const content = typeof s.content === "string" ? s.content : "";
|
|
285
|
+
const size = s.truncated
|
|
286
|
+
? null
|
|
287
|
+
: s.encoding === "base64" ? Math.floor(content.length * 3 / 4) - (content.endsWith("==") ? 2 : content.endsWith("=") ? 1 : 0)
|
|
288
|
+
: utf8Bytes(content);
|
|
289
|
+
return ainuiFile({ driveId: drive, path, env, mime: resolveMime(path, undefined, env), size, mtime: null });
|
|
290
|
+
}
|
|
291
|
+
default:
|
|
292
|
+
return asAinui(a2uiForSkill(skill, args, result, driveId));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
export const AINUI_ACTIONS = [
|
|
296
|
+
"aindrive.open_drive", "aindrive.open", "aindrive.search", "aindrive.view",
|
|
297
|
+
"aindrive.edit", "aindrive.new_file", "aindrive.save", "aindrive.delete", "aindrive.upload",
|
|
298
|
+
];
|
|
299
|
+
const err = (code, message) => ({ kind: "err", code, message });
|
|
300
|
+
class Refused extends Error {
|
|
301
|
+
skill;
|
|
302
|
+
constructor(skill) {
|
|
303
|
+
super(`refused: ${skill}`);
|
|
304
|
+
this.skill = skill;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Answer an AINUI action. Multi-step actions run their follow-up server-side:
|
|
309
|
+
* new_file → editor on the new file, save → file view, delete → parent folder.
|
|
310
|
+
* Every skill passes deps.allowed first (refused → nothing runs) and runs
|
|
311
|
+
* through deps.run (runSkill's own checks). A follow-up the caller may not run
|
|
312
|
+
* falls back to the first skill's surface.
|
|
313
|
+
*/
|
|
314
|
+
export async function dispatchAinuiAction(action, deps) {
|
|
315
|
+
const c = action.context ?? {};
|
|
316
|
+
const str = (v) => (typeof v === "string" ? v : "");
|
|
317
|
+
const drive_id = str(c.drive_id) || deps.driveId || "";
|
|
318
|
+
if (!AINUI_ACTIONS.includes(action.name))
|
|
319
|
+
return { kind: "invalid", error: `unknown action: ${action.name}` };
|
|
320
|
+
if (!drive_id)
|
|
321
|
+
return { kind: "invalid", error: `${action.name} needs context.drive_id` };
|
|
322
|
+
const path = cleanPath(c.path);
|
|
323
|
+
const steps = [];
|
|
324
|
+
const need = (...skills) => { for (const s of skills)
|
|
325
|
+
if (!deps.allowed(s))
|
|
326
|
+
throw new Refused(s); };
|
|
327
|
+
const run = async (skill, args) => {
|
|
328
|
+
need(skill);
|
|
329
|
+
const step = { skill, args, result: await deps.run(skill, args) };
|
|
330
|
+
steps.push(step);
|
|
331
|
+
return step;
|
|
332
|
+
};
|
|
333
|
+
const done = (final, surface) => ({ kind: "done", steps, final, surface });
|
|
334
|
+
/** Reply with a step's plain surface (an error card, or the result card of a write whose follow-up can't run). */
|
|
335
|
+
const basic = (skill, args, result) => done({ skill, args, result }, asAinui(a2uiForSkill(skill, args, result, drive_id)));
|
|
336
|
+
const folder = async (dir, view) => {
|
|
337
|
+
const s = await run("list_files", { drive_id, path: dir });
|
|
338
|
+
const env = await deps.env(drive_id, dir);
|
|
339
|
+
return done(s, ainuiForSkill(s.skill, s.args, s.result, drive_id, { ...env, ...(view ? { view } : {}) }));
|
|
340
|
+
};
|
|
341
|
+
const file = async (p) => {
|
|
342
|
+
const s = await run("stat", { drive_id, path: p });
|
|
343
|
+
if (s.result.kind === "ok") {
|
|
344
|
+
const e = s.result.structured;
|
|
345
|
+
if (e.isDir && deps.allowed("list_files"))
|
|
346
|
+
return folder(p);
|
|
347
|
+
// As read_file: a paid file nobody bought isn't opened (its bytes would 402 anyway).
|
|
348
|
+
if (e.locked)
|
|
349
|
+
return basic("stat", s.args, err("forbidden", `payment required for ${p}`));
|
|
350
|
+
}
|
|
351
|
+
return done(s, ainuiForSkill(s.skill, s.args, s.result, drive_id, await deps.env(drive_id, p)));
|
|
352
|
+
};
|
|
353
|
+
try {
|
|
354
|
+
switch (action.name) {
|
|
355
|
+
case "aindrive.open_drive":
|
|
356
|
+
return await folder("");
|
|
357
|
+
case "aindrive.open":
|
|
358
|
+
return c.is_dir === true || c.is_dir === "true" ? await folder(path) : await file(path);
|
|
359
|
+
case "aindrive.view": {
|
|
360
|
+
const v = str(c.value);
|
|
361
|
+
return await folder(path, v === "grid" || v === "list" ? v : undefined);
|
|
362
|
+
}
|
|
363
|
+
case "aindrive.search": {
|
|
364
|
+
const query = str(c.query).trim();
|
|
365
|
+
if (!query)
|
|
366
|
+
return await folder(path);
|
|
367
|
+
const s = await run("search", { drive_id, query, path });
|
|
368
|
+
return done(s, ainuiForSkill(s.skill, s.args, s.result, drive_id, await deps.env(drive_id, path)));
|
|
369
|
+
}
|
|
370
|
+
case "aindrive.edit": {
|
|
371
|
+
// Opening an editor the caller can't save from is refused like the save itself.
|
|
372
|
+
need("read_file", "write_file");
|
|
373
|
+
const args = { drive_id, path, encoding: "utf8" };
|
|
374
|
+
const env = await deps.env(drive_id, path);
|
|
375
|
+
if (!env.canWrite)
|
|
376
|
+
return basic("read_file", args, err("forbidden", "forbidden (no write access here)"));
|
|
377
|
+
const mime = resolveMime(path, undefined, env);
|
|
378
|
+
if (!isText(path, mime, env))
|
|
379
|
+
return basic("read_file", args, err("invalid_params", `not a text file: ${path}`));
|
|
380
|
+
const s = await run("read_file", args);
|
|
381
|
+
if (s.result.kind === "err")
|
|
382
|
+
return basic(s.skill, s.args, s.result);
|
|
383
|
+
const r = s.result.structured;
|
|
384
|
+
const content = typeof r.content === "string" ? r.content : "";
|
|
385
|
+
// Saving a truncated read would cut the file: too-large files stay read-only.
|
|
386
|
+
if (r.truncated || utf8Bytes(content) > MAX_EDIT_BYTES) {
|
|
387
|
+
return basic(s.skill, s.args, err("invalid_params", `file too large to edit here (limit ${MAX_EDIT_BYTES} bytes)`));
|
|
388
|
+
}
|
|
389
|
+
return done(s, ainuiEditor({ driveId: drive_id, path, content, env }));
|
|
390
|
+
}
|
|
391
|
+
case "aindrive.new_file": {
|
|
392
|
+
need("list_files", "write_file");
|
|
393
|
+
const name = str(c.name).trim();
|
|
394
|
+
if (/[/\\]/.test(name) || name === "." || name === "..") {
|
|
395
|
+
return basic("write_file", { drive_id, path }, err("invalid_params", `invalid file name: ${name}`));
|
|
396
|
+
}
|
|
397
|
+
const l = await run("list_files", { drive_id, path });
|
|
398
|
+
if (l.result.kind === "err")
|
|
399
|
+
return basic(l.skill, l.args, l.result);
|
|
400
|
+
const taken = new Set((l.result.structured.entries ?? []).map((e) => e.name));
|
|
401
|
+
let fileName = name;
|
|
402
|
+
if (!fileName) {
|
|
403
|
+
fileName = `${NEW_FILE_BASE}${NEW_FILE_EXT}`;
|
|
404
|
+
for (let i = 2; taken.has(fileName); i++)
|
|
405
|
+
fileName = `${NEW_FILE_BASE}-${i}${NEW_FILE_EXT}`;
|
|
406
|
+
}
|
|
407
|
+
else if (taken.has(fileName)) {
|
|
408
|
+
// Never clobber: new_file only creates.
|
|
409
|
+
return basic("write_file", { drive_id, path }, err("invalid_params", `already exists: ${fileName}`));
|
|
410
|
+
}
|
|
411
|
+
const target = path ? `${path}/${fileName}` : fileName;
|
|
412
|
+
const w = await run("write_file", { drive_id, path: target, content: "" });
|
|
413
|
+
if (w.result.kind === "err")
|
|
414
|
+
return basic(w.skill, w.args, w.result);
|
|
415
|
+
return done(w, ainuiEditor({ driveId: drive_id, path: target, content: "", env: await deps.env(drive_id, target) }));
|
|
416
|
+
}
|
|
417
|
+
case "aindrive.upload": {
|
|
418
|
+
need("write_file");
|
|
419
|
+
const name = str(c.name);
|
|
420
|
+
const content = str(c.content);
|
|
421
|
+
if (!name || /[/\\\x00-\x1f]/.test(name) || name === "." || name === ".." ||
|
|
422
|
+
c.encoding !== "base64" || !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(content) ||
|
|
423
|
+
(content.length / 4 * 3 - (content.endsWith("==") ? 2 : content.endsWith("=") ? 1 : 0)) > AINUI_UPLOAD_MAX_BYTES) {
|
|
424
|
+
return basic("write_file", { drive_id, path }, err("invalid_params", "Invalid upload (maximum 8 MiB)"));
|
|
425
|
+
}
|
|
426
|
+
const env = await deps.env(drive_id, path);
|
|
427
|
+
if (!env.canWrite)
|
|
428
|
+
return basic("write_file", { drive_id, path }, err("forbidden", "No upload permission here"));
|
|
429
|
+
const target = path ? `${path}/${name}` : name;
|
|
430
|
+
const w = await run("write_file", { drive_id, path: target, content, encoding: "base64" });
|
|
431
|
+
if (w.result.kind === "err" || !deps.allowed("list_files"))
|
|
432
|
+
return basic(w.skill, w.args, w.result);
|
|
433
|
+
return await folder(path);
|
|
434
|
+
}
|
|
435
|
+
case "aindrive.save": {
|
|
436
|
+
const w = await run("write_file", { drive_id, path, content: c.content });
|
|
437
|
+
if (w.result.kind === "err" || !deps.allowed("stat"))
|
|
438
|
+
return basic(w.skill, w.args, w.result);
|
|
439
|
+
const s = await run("stat", { drive_id, path });
|
|
440
|
+
if (s.result.kind === "err")
|
|
441
|
+
return basic(w.skill, w.args, w.result);
|
|
442
|
+
return done(s, ainuiForSkill(s.skill, s.args, s.result, drive_id, await deps.env(drive_id, path)));
|
|
443
|
+
}
|
|
444
|
+
case "aindrive.delete": {
|
|
445
|
+
const d = await run("delete_path", { drive_id, path });
|
|
446
|
+
if (d.result.kind === "err" || !deps.allowed("list_files"))
|
|
447
|
+
return basic(d.skill, d.args, d.result);
|
|
448
|
+
const parent = parentOf(path);
|
|
449
|
+
const l = await run("list_files", { drive_id, path: parent });
|
|
450
|
+
if (l.result.kind === "err")
|
|
451
|
+
return basic(d.skill, d.args, d.result);
|
|
452
|
+
return done(l, ainuiForSkill(l.skill, l.args, l.result, drive_id, await deps.env(drive_id, parent)));
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
catch (e) {
|
|
457
|
+
if (e instanceof Refused)
|
|
458
|
+
return { kind: "refused", skill: e.skill };
|
|
459
|
+
throw e;
|
|
460
|
+
}
|
|
461
|
+
return { kind: "invalid", error: `unknown action: ${action.name}` };
|
|
462
|
+
}
|
package/dist/basic.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A2UI (Agent-to-User Interface, https://a2ui.org) surfaces for aindrive —
|
|
3
|
+
* the ONE declarative UI definition every agent transport reuses:
|
|
4
|
+
*
|
|
5
|
+
* A2A DataPart { data: A2uiMessage[] }, metadata.mimeType A2UI_MIME
|
|
6
|
+
* AG-UI ACTIVITY_SNAPSHOT { activityType: "a2ui-surface", content: { a2ui_operations } }
|
|
7
|
+
* MCP tool result `_meta[A2UI_META_KEY]` (rendered by the MCP Apps view,
|
|
8
|
+
* app/mcp/ui) + an embedded `application/a2ui+json` resource on opt-in
|
|
9
|
+
*
|
|
10
|
+
* Spec: A2UI v0.9 (stable), basic catalog only — so any conforming renderer
|
|
11
|
+
* (@a2ui/lit, @a2ui/react, CopilotKit, …) can draw it; there is no custom
|
|
12
|
+
* catalog to install. Buttons emit `action.event`s named `aindrive.*`; the
|
|
13
|
+
* resulting client→server `action` maps back to a skill via actionToSkill().
|
|
14
|
+
*
|
|
15
|
+
* Pure: no IO, no Node APIs — safe to import from client code (docs playground).
|
|
16
|
+
*
|
|
17
|
+
* AINUI (docs/AINUI.md) — the same messages with aindrive's custom catalog
|
|
18
|
+
* (grid, thumbnail tiles, asset references, file viewer, write actions) — lives
|
|
19
|
+
* in ./ainui.ts and is only sent to clients that opt in.
|
|
20
|
+
*/
|
|
21
|
+
import type { SkillResult } from "./types.js";
|
|
22
|
+
export declare const A2UI_VERSION: "v0.9";
|
|
23
|
+
export declare const A2UI_MIME = "application/a2ui+json";
|
|
24
|
+
export declare const A2UI_BASIC_CATALOG = "https://a2ui.org/specification/v0_9/catalogs/basic/catalog.json";
|
|
25
|
+
/** A2A extension URI (A2UI over A2A), advertised in the agent card. */
|
|
26
|
+
export declare const A2UI_A2A_EXTENSION = "https://a2ui.org/a2a-extension/a2ui/v0.9";
|
|
27
|
+
/** AG-UI activity type used by @ag-ui/a2ui-middleware / CopilotKit. */
|
|
28
|
+
export declare const AGUI_A2UI_ACTIVITY = "a2ui-surface";
|
|
29
|
+
export declare const AGUI_A2UI_OPERATIONS_KEY = "a2ui_operations";
|
|
30
|
+
/** Where MCP tool results carry the surface for UI-only consumption (not shown to the model). */
|
|
31
|
+
export declare const A2UI_META_KEY = "ai.aindrive/a2ui";
|
|
32
|
+
export type A2uiComponent = {
|
|
33
|
+
id: string;
|
|
34
|
+
component: string;
|
|
35
|
+
} & Record<string, unknown>;
|
|
36
|
+
export type A2uiMessage = {
|
|
37
|
+
version: typeof A2UI_VERSION;
|
|
38
|
+
createSurface: {
|
|
39
|
+
surfaceId: string;
|
|
40
|
+
catalogId: string;
|
|
41
|
+
};
|
|
42
|
+
} | {
|
|
43
|
+
version: typeof A2UI_VERSION;
|
|
44
|
+
updateComponents: {
|
|
45
|
+
surfaceId: string;
|
|
46
|
+
components: A2uiComponent[];
|
|
47
|
+
};
|
|
48
|
+
} | {
|
|
49
|
+
version: typeof A2UI_VERSION;
|
|
50
|
+
updateDataModel: {
|
|
51
|
+
surfaceId: string;
|
|
52
|
+
path?: string;
|
|
53
|
+
value?: unknown;
|
|
54
|
+
};
|
|
55
|
+
} | {
|
|
56
|
+
version: typeof A2UI_VERSION;
|
|
57
|
+
deleteSurface: {
|
|
58
|
+
surfaceId: string;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
/** Client→server A2UI user action (v0.9 `action` object). */
|
|
62
|
+
export type A2uiAction = {
|
|
63
|
+
name: string;
|
|
64
|
+
surfaceId?: string;
|
|
65
|
+
sourceComponentId?: string;
|
|
66
|
+
timestamp?: string;
|
|
67
|
+
context?: Record<string, unknown>;
|
|
68
|
+
};
|
|
69
|
+
export declare function humanSize(n: unknown): string;
|
|
70
|
+
export declare const parentOf: (p: string) => string;
|
|
71
|
+
export declare const baseOf: (p: string) => string;
|
|
72
|
+
/**
|
|
73
|
+
* Build the A2UI surface for one skill call. `driveId` is the drive the call
|
|
74
|
+
* ran against (from args or the pinned ctx); `args` are the skill arguments.
|
|
75
|
+
*/
|
|
76
|
+
export declare function a2uiForSkill(skill: string, args: Record<string, unknown>, result: SkillResult, driveId?: string): A2uiMessage[];
|
|
77
|
+
/**
|
|
78
|
+
* Accept the shapes renderers actually send: a v0.9 client event
|
|
79
|
+
* `{version, action}`, a bare `action` object, `{action}`, or AG-UI/CopilotKit's
|
|
80
|
+
* `{userAction}` (forwardedProps.a2uiAction).
|
|
81
|
+
*/
|
|
82
|
+
export declare function parseA2uiAction(input: unknown): A2uiAction | null;
|
|
83
|
+
export type SkillCall = {
|
|
84
|
+
skill: string;
|
|
85
|
+
args: Record<string, unknown>;
|
|
86
|
+
};
|
|
87
|
+
/** Map an aindrive A2UI action to the skill call that answers it. */
|
|
88
|
+
export declare function actionToSkill(action: A2uiAction, fallbackDriveId?: string): SkillCall | {
|
|
89
|
+
error: string;
|
|
90
|
+
};
|
|
91
|
+
/** Tiny text-command grammar shared by A2A TextParts and AG-UI user messages. */
|
|
92
|
+
export declare function commandToSkill(textIn: string, driveId?: string): SkillCall;
|