ain-ui 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/dist/catalog.json +71 -0
- package/dist/chat.d.ts +54 -0
- package/dist/chat.js +186 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/react.d.ts +24 -1
- package/dist/react.js +72 -4
- package/dist/renderer.js +43 -0
- package/dist/styles.css +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -105,3 +105,27 @@ npm pack --dry-run
|
|
|
105
105
|
|
|
106
106
|
Source extracted from aindrive's existing AINUI implementation. aindrive and
|
|
107
107
|
ainmem consume this package; protocol and component changes belong here.
|
|
108
|
+
|
|
109
|
+
### Host file previews
|
|
110
|
+
|
|
111
|
+
`AinuiSurface` accepts `renderFile({ src, download, name, mime, size })` to
|
|
112
|
+
render a `FileView` with the host's document, spreadsheet or other rich preview.
|
|
113
|
+
The URLs have already passed the renderer's URL checks and asset resolver.
|
|
114
|
+
Keep authentication and file permissions in the host's byte-serving routes.
|
|
115
|
+
Omit this callback to use the built-in media preview and download view.
|
|
116
|
+
|
|
117
|
+
## Folder chat
|
|
118
|
+
|
|
119
|
+
`ainuiFolderChat(state)` produces the `FolderChat` A2UI catalog component.
|
|
120
|
+
`AinuiFolderChat` from `ain-ui/react` hosts agent selection, progressive replies,
|
|
121
|
+
stop, errors and per-folder/per-agent conversation ids. Supply `onSend` to connect
|
|
122
|
+
the host's authenticated API; it receives an AbortSignal and `onUpdate`.
|
|
123
|
+
`readChatStream` reads AG-UI SSE, including `ainui.chat.snapshot` custom events;
|
|
124
|
+
`ainuiActivity` wraps full folder-chat surfaces for other A2UI hosts.
|
|
125
|
+
`A2aChatAccumulator` combines A2A status/task/artifact events without replaying
|
|
126
|
+
a failed request or duplicating snapshot text. Native agents without streaming
|
|
127
|
+
continue to return one final response.
|
|
128
|
+
|
|
129
|
+
`listFolderTree` lists descendants within the selected root, with entry, depth
|
|
130
|
+
and directory limits. It reports partial/error results instead of treating them
|
|
131
|
+
as an empty or complete folder. No host credentials belong in UI messages.
|
package/dist/catalog.json
CHANGED
|
@@ -1755,6 +1755,77 @@
|
|
|
1755
1755
|
]
|
|
1756
1756
|
}
|
|
1757
1757
|
]
|
|
1758
|
+
},
|
|
1759
|
+
"FolderChat": {
|
|
1760
|
+
"allOf": [
|
|
1761
|
+
{
|
|
1762
|
+
"$ref": "common_types.json#/$defs/ComponentCommon"
|
|
1763
|
+
},
|
|
1764
|
+
{
|
|
1765
|
+
"properties": {
|
|
1766
|
+
"component": {
|
|
1767
|
+
"const": "FolderChat"
|
|
1768
|
+
},
|
|
1769
|
+
"value": {
|
|
1770
|
+
"anyOf": [
|
|
1771
|
+
{
|
|
1772
|
+
"$ref": "common_types.json#/$defs/DynamicValue",
|
|
1773
|
+
"description": "A value that can be a literal, a path, or a function call returning any type."
|
|
1774
|
+
},
|
|
1775
|
+
{
|
|
1776
|
+
"type": "object",
|
|
1777
|
+
"properties": {
|
|
1778
|
+
"$asset": {
|
|
1779
|
+
"type": "object",
|
|
1780
|
+
"properties": {
|
|
1781
|
+
"drive_id": {
|
|
1782
|
+
"type": "string"
|
|
1783
|
+
},
|
|
1784
|
+
"path": {
|
|
1785
|
+
"type": "string"
|
|
1786
|
+
},
|
|
1787
|
+
"variant": {
|
|
1788
|
+
"type": "string",
|
|
1789
|
+
"enum": [
|
|
1790
|
+
"thumb",
|
|
1791
|
+
"original"
|
|
1792
|
+
]
|
|
1793
|
+
},
|
|
1794
|
+
"mime": {
|
|
1795
|
+
"type": "string"
|
|
1796
|
+
},
|
|
1797
|
+
"v": {
|
|
1798
|
+
"type": "number"
|
|
1799
|
+
}
|
|
1800
|
+
},
|
|
1801
|
+
"required": [
|
|
1802
|
+
"drive_id",
|
|
1803
|
+
"path",
|
|
1804
|
+
"variant",
|
|
1805
|
+
"mime"
|
|
1806
|
+
],
|
|
1807
|
+
"additionalProperties": false
|
|
1808
|
+
}
|
|
1809
|
+
},
|
|
1810
|
+
"required": [
|
|
1811
|
+
"$asset"
|
|
1812
|
+
],
|
|
1813
|
+
"additionalProperties": false
|
|
1814
|
+
},
|
|
1815
|
+
{
|
|
1816
|
+
"type": "null"
|
|
1817
|
+
}
|
|
1818
|
+
]
|
|
1819
|
+
},
|
|
1820
|
+
"action": {
|
|
1821
|
+
"$ref": "common_types.json#/$defs/Action"
|
|
1822
|
+
}
|
|
1823
|
+
},
|
|
1824
|
+
"required": [
|
|
1825
|
+
"component"
|
|
1826
|
+
]
|
|
1827
|
+
}
|
|
1828
|
+
]
|
|
1758
1829
|
}
|
|
1759
1830
|
},
|
|
1760
1831
|
"functions": [
|
package/dist/chat.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type A2uiMessage } from './basic.js';
|
|
2
|
+
export type FolderChatMessage = {
|
|
3
|
+
role: 'user' | 'agent' | 'error';
|
|
4
|
+
text: string;
|
|
5
|
+
};
|
|
6
|
+
export type FolderChatAgent = {
|
|
7
|
+
id: string;
|
|
8
|
+
label: string;
|
|
9
|
+
remote?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export type ChatUpdate = {
|
|
12
|
+
text: string;
|
|
13
|
+
contextId?: string;
|
|
14
|
+
state?: string;
|
|
15
|
+
};
|
|
16
|
+
export type FolderChatState = {
|
|
17
|
+
driveId: string;
|
|
18
|
+
path: string;
|
|
19
|
+
agents: FolderChatAgent[];
|
|
20
|
+
agentId: string;
|
|
21
|
+
messages: FolderChatMessage[];
|
|
22
|
+
busy: boolean;
|
|
23
|
+
};
|
|
24
|
+
export declare function ainuiFolderChat(state: FolderChatState): A2uiMessage[];
|
|
25
|
+
/** A2A snapshots replace; artifact append events append to their own artifact only. */
|
|
26
|
+
export declare class A2aChatAccumulator {
|
|
27
|
+
private statusText;
|
|
28
|
+
private artifacts;
|
|
29
|
+
private contextId?;
|
|
30
|
+
private state?;
|
|
31
|
+
received: boolean;
|
|
32
|
+
push(raw: unknown): ChatUpdate;
|
|
33
|
+
value(): ChatUpdate;
|
|
34
|
+
}
|
|
35
|
+
/** Consume AG-UI events, including full AIN-UI surface snapshots. Handles split UTF-8/CRLF frames. */
|
|
36
|
+
export declare function readChatStream(response: Response, onUpdate: (update: ChatUpdate) => void, signal?: AbortSignal): Promise<ChatUpdate>;
|
|
37
|
+
export interface FolderTreeEntry {
|
|
38
|
+
name: string;
|
|
39
|
+
path: string;
|
|
40
|
+
isDir: boolean;
|
|
41
|
+
}
|
|
42
|
+
/** Bounded breadth-first traversal. Paths returned by a provider may never escape the selected root. */
|
|
43
|
+
export declare function listFolderTree<T extends FolderTreeEntry>(list: (path: string) => Promise<T[]>, root?: string, options?: {
|
|
44
|
+
maxEntries?: number;
|
|
45
|
+
maxDepth?: number;
|
|
46
|
+
maxDirectories?: number;
|
|
47
|
+
signal?: AbortSignal;
|
|
48
|
+
}): Promise<{
|
|
49
|
+
entries: T[];
|
|
50
|
+
directories: number;
|
|
51
|
+
observedEntries: number;
|
|
52
|
+
truncated: boolean;
|
|
53
|
+
errors: string[];
|
|
54
|
+
}>;
|
package/dist/chat.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { A2UI_VERSION } from './basic.js';
|
|
2
|
+
import { AINUI_CATALOG } from './ainui.js';
|
|
3
|
+
export function ainuiFolderChat(state) {
|
|
4
|
+
const surfaceId = 'ainui-folder-chat';
|
|
5
|
+
return [
|
|
6
|
+
{ version: A2UI_VERSION, createSurface: { surfaceId, catalogId: AINUI_CATALOG } },
|
|
7
|
+
{ version: A2UI_VERSION, updateComponents: { surfaceId, components: [{ id: 'root', component: 'FolderChat', value: { path: '/chat' }, action: { event: { name: 'aindrive.chat', context: { drive_id: state.driveId, path: state.path } } } }] } },
|
|
8
|
+
{ version: A2UI_VERSION, updateDataModel: { surfaceId, path: '/', value: { chat: state } } },
|
|
9
|
+
];
|
|
10
|
+
}
|
|
11
|
+
function textOf(parts) {
|
|
12
|
+
if (!Array.isArray(parts))
|
|
13
|
+
return '';
|
|
14
|
+
return parts.filter(p => p && (p.kind === 'text' || p.type === 'text') && typeof p.text === 'string').map(p => p.text).join('\n');
|
|
15
|
+
}
|
|
16
|
+
/** A2A snapshots replace; artifact append events append to their own artifact only. */
|
|
17
|
+
export class A2aChatAccumulator {
|
|
18
|
+
statusText = '';
|
|
19
|
+
artifacts = new Map();
|
|
20
|
+
contextId;
|
|
21
|
+
state;
|
|
22
|
+
received = false;
|
|
23
|
+
push(raw) {
|
|
24
|
+
if (!raw || typeof raw !== 'object')
|
|
25
|
+
throw new Error('Invalid A2A event');
|
|
26
|
+
const e = raw;
|
|
27
|
+
this.received = true;
|
|
28
|
+
if (typeof e.contextId === 'string')
|
|
29
|
+
this.contextId = e.contextId;
|
|
30
|
+
if (e.kind === 'message')
|
|
31
|
+
this.statusText = textOf(e.parts);
|
|
32
|
+
if (e.kind === 'task') {
|
|
33
|
+
this.state = e.status?.state;
|
|
34
|
+
if (e.status?.message)
|
|
35
|
+
this.statusText = textOf(e.status.message.parts);
|
|
36
|
+
if (Array.isArray(e.artifacts)) {
|
|
37
|
+
this.artifacts.clear();
|
|
38
|
+
for (const a of e.artifacts)
|
|
39
|
+
this.artifacts.set(a.artifactId, textOf(a.parts));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (e.kind === 'status-update') {
|
|
43
|
+
this.state = e.status?.state;
|
|
44
|
+
if (e.status?.message)
|
|
45
|
+
this.statusText = textOf(e.status.message.parts);
|
|
46
|
+
}
|
|
47
|
+
if (e.kind === 'artifact-update' && e.artifact) {
|
|
48
|
+
const id = e.artifact.artifactId;
|
|
49
|
+
if (typeof id !== 'string')
|
|
50
|
+
throw new Error('A2A artifact has no id');
|
|
51
|
+
const text = textOf(e.artifact.parts);
|
|
52
|
+
this.artifacts.set(id, e.append ? (this.artifacts.get(id) ?? '') + text : text);
|
|
53
|
+
}
|
|
54
|
+
if (this.state === 'failed' || this.state === 'rejected' || this.state === 'canceled')
|
|
55
|
+
throw new Error(this.statusText || `Agent task ${this.state}`);
|
|
56
|
+
return this.value();
|
|
57
|
+
}
|
|
58
|
+
value() {
|
|
59
|
+
// Artifacts are the deliverable; status text often repeats the same answer.
|
|
60
|
+
const artifacts = [...this.artifacts.values()].filter(Boolean).join('\n\n');
|
|
61
|
+
return { text: artifacts || this.statusText, contextId: this.contextId, state: this.state };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Consume AG-UI events, including full AIN-UI surface snapshots. Handles split UTF-8/CRLF frames. */
|
|
65
|
+
export async function readChatStream(response, onUpdate, signal) {
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
const data = await response.json().catch(() => ({}));
|
|
68
|
+
throw new Error(data.error || `HTTP ${response.status}`);
|
|
69
|
+
}
|
|
70
|
+
if (!response.headers.get('content-type')?.includes('text/event-stream')) {
|
|
71
|
+
const data = await response.json();
|
|
72
|
+
const update = { text: data.answer ?? data.text ?? '', contextId: data.contextId ?? undefined };
|
|
73
|
+
onUpdate(update);
|
|
74
|
+
return update;
|
|
75
|
+
}
|
|
76
|
+
const reader = response.body?.getReader();
|
|
77
|
+
if (!reader)
|
|
78
|
+
throw new Error('Empty chat stream');
|
|
79
|
+
const decoder = new TextDecoder();
|
|
80
|
+
let buffer = '', complete = false, value = { text: '' };
|
|
81
|
+
const abort = () => { void reader.cancel(); };
|
|
82
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
83
|
+
const frame = (raw) => {
|
|
84
|
+
const data = raw.split('\n').filter(l => l.startsWith('data:')).map(l => l.slice(5).trimStart()).join('\n');
|
|
85
|
+
if (!data || data === '[DONE]')
|
|
86
|
+
return;
|
|
87
|
+
const e = JSON.parse(data);
|
|
88
|
+
if (e.type === 'RUN_ERROR')
|
|
89
|
+
throw new Error(e.message || 'Agent failed');
|
|
90
|
+
if (e.type === 'TEXT_MESSAGE_CONTENT') {
|
|
91
|
+
value.text += e.delta ?? '';
|
|
92
|
+
onUpdate({ ...value });
|
|
93
|
+
}
|
|
94
|
+
if (e.type === 'CUSTOM' && e.name === 'ainui.chat.snapshot') {
|
|
95
|
+
value = e.value;
|
|
96
|
+
onUpdate({ ...value });
|
|
97
|
+
}
|
|
98
|
+
if (e.type === 'RUN_FINISHED') {
|
|
99
|
+
complete = true;
|
|
100
|
+
if (e.result?.contextId)
|
|
101
|
+
value.contextId = e.result.contextId;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
try {
|
|
105
|
+
signal?.throwIfAborted();
|
|
106
|
+
while (true) {
|
|
107
|
+
const { value: chunk, done } = await reader.read();
|
|
108
|
+
buffer += done ? decoder.decode() : decoder.decode(chunk, { stream: true });
|
|
109
|
+
buffer = buffer.replace(/\r\n/g, '\n');
|
|
110
|
+
let i;
|
|
111
|
+
while ((i = buffer.indexOf('\n\n')) >= 0) {
|
|
112
|
+
frame(buffer.slice(0, i));
|
|
113
|
+
buffer = buffer.slice(i + 2);
|
|
114
|
+
}
|
|
115
|
+
if (done)
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
signal?.throwIfAborted();
|
|
119
|
+
if (buffer.trim())
|
|
120
|
+
frame(buffer);
|
|
121
|
+
if (!complete)
|
|
122
|
+
throw new Error('Chat stream ended before completion');
|
|
123
|
+
return value;
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
signal?.removeEventListener('abort', abort);
|
|
127
|
+
await reader.cancel().catch(() => { });
|
|
128
|
+
reader.releaseLock();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** Bounded breadth-first traversal. Paths returned by a provider may never escape the selected root. */
|
|
132
|
+
export async function listFolderTree(list, root = '', options = {}) {
|
|
133
|
+
const clean = (p) => p.split('/').filter(Boolean).join('/');
|
|
134
|
+
const valid = (p) => !p.includes('\\') && !p.includes('\0') && !p.split('/').some(s => s === '.' || s === '..');
|
|
135
|
+
if (!valid(root))
|
|
136
|
+
throw new Error('Invalid folder root');
|
|
137
|
+
root = clean(root);
|
|
138
|
+
const max = options.maxEntries ?? 200, depthLimit = options.maxDepth ?? 8, dirLimit = options.maxDirectories ?? 64;
|
|
139
|
+
const queue = [{ path: root, depth: 0 }], seen = new Set();
|
|
140
|
+
const entries = [], errors = [];
|
|
141
|
+
let directories = 0, observedEntries = 0, truncated = false;
|
|
142
|
+
while (queue.length) {
|
|
143
|
+
options.signal?.throwIfAborted();
|
|
144
|
+
if (directories >= dirLimit || entries.length >= max) {
|
|
145
|
+
truncated = true;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
const dir = queue.shift();
|
|
149
|
+
let children;
|
|
150
|
+
try {
|
|
151
|
+
children = await list(dir.path);
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
if (dir.path === root)
|
|
155
|
+
throw e;
|
|
156
|
+
errors.push(dir.path);
|
|
157
|
+
truncated = true;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
directories++;
|
|
161
|
+
for (const entry of children) {
|
|
162
|
+
const path = clean(entry.path);
|
|
163
|
+
if (!valid(entry.path) || (dir.path ? !path.startsWith(dir.path + '/') : false) || path.slice(dir.path ? dir.path.length + 1 : 0).includes('/')) {
|
|
164
|
+
errors.push(dir.path);
|
|
165
|
+
truncated = true;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (!path || entry.name.startsWith('.') || seen.has(path))
|
|
169
|
+
continue;
|
|
170
|
+
seen.add(path);
|
|
171
|
+
observedEntries++;
|
|
172
|
+
if (entries.length >= max) {
|
|
173
|
+
truncated = true;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
entries.push({ ...entry, path });
|
|
177
|
+
if (entry.isDir) {
|
|
178
|
+
if (dir.depth >= depthLimit)
|
|
179
|
+
truncated = true;
|
|
180
|
+
else
|
|
181
|
+
queue.push({ path, depth: dir.depth + 1 });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return { entries, directories, observedEntries, truncated, errors };
|
|
186
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/react.d.ts
CHANGED
|
@@ -1,13 +1,36 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { Catalog } from "@a2ui/web_core/v0_9";
|
|
3
3
|
import { type AssetRef } from "./ainui.js";
|
|
4
|
+
import { type FolderChatAgent, type ChatUpdate } from "./chat.js";
|
|
4
5
|
import type { A2uiAction, A2uiMessage } from "./basic.js";
|
|
5
6
|
export type AssetResolver = (asset: AssetRef["$asset"], options?: {
|
|
6
7
|
download?: boolean;
|
|
7
8
|
}) => string;
|
|
9
|
+
export type FileViewRenderer = (file: {
|
|
10
|
+
src: string;
|
|
11
|
+
download: string;
|
|
12
|
+
name: string;
|
|
13
|
+
mime: string;
|
|
14
|
+
size?: number;
|
|
15
|
+
}) => React.ReactNode;
|
|
8
16
|
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 }: {
|
|
17
|
+
export declare function AinuiSurface({ messages, onAction, resolveAsset, renderFile }: {
|
|
10
18
|
messages: A2uiMessage[];
|
|
11
19
|
onAction: (action: A2uiAction) => void | Promise<void>;
|
|
12
20
|
resolveAsset?: AssetResolver;
|
|
21
|
+
renderFile?: FileViewRenderer;
|
|
22
|
+
}): React.JSX.Element;
|
|
23
|
+
export type FolderChatSend = (turn: {
|
|
24
|
+
q: string;
|
|
25
|
+
agentId: string;
|
|
26
|
+
contextId?: string;
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
onUpdate: (update: ChatUpdate) => void;
|
|
29
|
+
}) => Promise<ChatUpdate>;
|
|
30
|
+
/** Scope changes unmount the old chat, abort its request and drop its context ids. */
|
|
31
|
+
export declare function AinuiFolderChat(props: {
|
|
32
|
+
driveId: string;
|
|
33
|
+
path: string;
|
|
34
|
+
agents: FolderChatAgent[];
|
|
35
|
+
onSend: FolderChatSend;
|
|
13
36
|
}): React.JSX.Element;
|
package/dist/react.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
3
|
import React, { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { A2uiSurface, MarkdownContext, basicCatalog, createBinderlessComponentImplementation, useSignalValue } from "@a2ui/react/v0_9";
|
|
5
5
|
import { Catalog, CommonSchemas, MessageProcessor } from "@a2ui/web_core/v0_9";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import { renderMarkdown } from "@a2ui/markdown-it";
|
|
8
8
|
import { AINUI_CATALOG, AINUI_UPLOAD_MAX_BYTES } from "./ainui.js";
|
|
9
|
+
import { ainuiFolderChat } from "./chat.js";
|
|
10
|
+
const FileRenderer = createContext(undefined);
|
|
9
11
|
const Assets = createContext(() => "");
|
|
10
12
|
const box = { display: "flex", flexDirection: "column", gap: 8, minWidth: 0 };
|
|
11
13
|
const button = { padding: "8px 12px", border: "1px solid #8885", borderRadius: 8, color: "inherit", background: "transparent" };
|
|
@@ -63,6 +65,10 @@ const FileView = createBinderlessComponentImplementation(api("FileView", { src:
|
|
|
63
65
|
const mime = useValue(context, p.mime) || "";
|
|
64
66
|
const resolver = useContext(Assets);
|
|
65
67
|
const src = urlOf(asset, resolver);
|
|
68
|
+
const render = useContext(FileRenderer);
|
|
69
|
+
const size = useValue(context, p.size);
|
|
70
|
+
if (render && src)
|
|
71
|
+
return _jsx(_Fragment, { children: render({ src, download: urlOf(asset, resolver, true), name, mime, size }) });
|
|
66
72
|
return _jsxs("div", { style: box, children: [src && (mime.startsWith("image/") ? _jsx("img", { src: src, alt: name, style: { maxHeight: "70vh", objectFit: "contain" } }) :
|
|
67
73
|
mime.startsWith("video/") ? _jsx("video", { controls: true, src: src }) : mime.startsWith("audio/") ? _jsx("audio", { controls: true, src: src }) :
|
|
68
74
|
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" })] });
|
|
@@ -138,6 +144,16 @@ const X402Payment = createBinderlessComponentImplementation(api("X402Payment", {
|
|
|
138
144
|
}
|
|
139
145
|
}, children: busy ? "Confirm in your wallet…" : `Pay ${amount} ${currency}` }), error && _jsx("small", { role: "alert", children: error })] });
|
|
140
146
|
});
|
|
147
|
+
const FolderChat = createBinderlessComponentImplementation(api("FolderChat", { value: dynamic, action: CommonSchemas.Action.optional() }), ({ context }) => {
|
|
148
|
+
const state = useValue(context, context.componentModel.properties.value);
|
|
149
|
+
const [input, setInput] = useState("");
|
|
150
|
+
if (!state)
|
|
151
|
+
return null;
|
|
152
|
+
return _jsxs("section", { "aria-label": "Folder chat", style: box, children: [_jsxs("strong", { children: ["Folder chat \u00B7 ", state.path || "/"] }), _jsxs("label", { children: ["Agent ", _jsx("select", { "aria-label": "Chat agent", disabled: state.busy, value: state.agentId, onChange: e => void fire(context, { operation: "select", agentId: e.target.value }), children: state.agents.map(a => _jsx("option", { value: a.id, children: a.label }, a.id)) })] }), _jsx("div", { role: "log", "aria-live": "polite", style: { maxHeight: 400, overflowY: "auto" }, children: state.messages.map((m, i) => _jsxs("div", { role: m.role === "error" ? "alert" : undefined, style: { whiteSpace: "pre-wrap", overflowWrap: "anywhere", paddingBlock: 8 }, children: [_jsx("strong", { children: m.role === "user" ? "You" : m.role === "error" ? "Error" : state.agents.find(a => a.id === state.agentId)?.label || "Agent" }), _jsx("div", { children: m.text })] }, i)) }), _jsxs("form", { onSubmit: e => { e.preventDefault(); if (input.trim() && !state.busy) {
|
|
153
|
+
void fire(context, { operation: "send", q: input.trim(), agentId: state.agentId });
|
|
154
|
+
setInput("");
|
|
155
|
+
} }, style: box, children: [_jsx("textarea", { "aria-label": "Message", disabled: state.busy, value: input, onChange: e => setInput(e.target.value) }), state.busy ? _jsx("button", { type: "button", style: button, onClick: () => void fire(context, { operation: "cancel" }), children: "Stop" }) : _jsx("button", { style: button, disabled: !input.trim() || !state.agentId, children: "Send" })] })] });
|
|
156
|
+
});
|
|
141
157
|
// Preserve AIN-UI's confirmation extension on the basic Button.
|
|
142
158
|
const Button = createBinderlessComponentImplementation(api("Button", { child: CommonSchemas.ComponentId, action: CommonSchemas.Action.optional(), confirm: dynamic, variant: dynamic, tone: dynamic }), ({ context, buildChild }) => {
|
|
143
159
|
const p = context.componentModel.properties;
|
|
@@ -145,8 +161,8 @@ const Button = createBinderlessComponentImplementation(api("Button", { child: Co
|
|
|
145
161
|
return _jsx("button", { style: button, onClick: () => { if (!confirm || window.confirm(confirm))
|
|
146
162
|
void fire(context); }, children: buildChild(String(p.child)) });
|
|
147
163
|
});
|
|
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 }) {
|
|
164
|
+
export const ainuiCatalog = new Catalog(AINUI_CATALOG, [...basicCatalog.components.values()].filter((c) => c.name !== "Button").concat([Button, Grid, Tile, FileView, Breadcrumbs, Segmented, FileUpload, X402Payment, FolderChat]), [...basicCatalog.functions.values()]);
|
|
165
|
+
export function AinuiSurface({ messages, onAction, resolveAsset, renderFile }) {
|
|
150
166
|
const handler = useRef(onAction);
|
|
151
167
|
handler.current = onAction;
|
|
152
168
|
const [processor, setProcessor] = useState(null);
|
|
@@ -169,5 +185,57 @@ export function AinuiSurface({ messages, onAction, resolveAsset }) {
|
|
|
169
185
|
return _jsx("div", { className: "ain-ui", role: "alert", children: error });
|
|
170
186
|
if (!processor)
|
|
171
187
|
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)) }) }) });
|
|
188
|
+
return _jsx(MarkdownContext.Provider, { value: renderMarkdown, children: _jsx(Assets.Provider, { value: resolveAsset ?? (() => ""), children: _jsx(FileRenderer.Provider, { value: renderFile, children: _jsx("div", { className: "ain-ui", children: Array.from(processor.model.surfacesMap.values()).map((surface) => _jsx(A2uiSurface, { surface: surface }, surface.id)) }) }) }) });
|
|
189
|
+
}
|
|
190
|
+
/** Scope changes unmount the old chat, abort its request and drop its context ids. */
|
|
191
|
+
export function AinuiFolderChat(props) {
|
|
192
|
+
return _jsx(FolderChatSession, { ...props }, `${props.driveId}:${props.path}`);
|
|
193
|
+
}
|
|
194
|
+
function FolderChatSession({ driveId, path, agents, onSend }) {
|
|
195
|
+
const [agentId, select] = useState(agents[0]?.id ?? "");
|
|
196
|
+
const [messages, setMessages] = useState([]);
|
|
197
|
+
const [busy, setBusy] = useState(false);
|
|
198
|
+
const pending = useRef(null);
|
|
199
|
+
const contexts = useRef(new Map());
|
|
200
|
+
useEffect(() => () => { pending.current?.abort(); pending.current = null; }, []);
|
|
201
|
+
const selected = agents.some(a => a.id === agentId) ? agentId : agents[0]?.id ?? "";
|
|
202
|
+
const surface = ainuiFolderChat({ driveId, path, agents, agentId: selected, messages, busy });
|
|
203
|
+
return _jsx(AinuiSurface, { messages: surface, onAction: async (action) => {
|
|
204
|
+
const c = action.context ?? {};
|
|
205
|
+
if (c.operation === "cancel") {
|
|
206
|
+
pending.current?.abort();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (c.operation === "select" && !pending.current) {
|
|
210
|
+
select(String(c.agentId));
|
|
211
|
+
setMessages([]);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (c.operation !== "send" || pending.current || typeof c.q !== "string" || !selected)
|
|
215
|
+
return;
|
|
216
|
+
const controller = new AbortController();
|
|
217
|
+
pending.current = controller;
|
|
218
|
+
setBusy(true);
|
|
219
|
+
setMessages(m => [...m.slice(-98), { role: "user", text: c.q }, { role: "agent", text: "" }]);
|
|
220
|
+
const onUpdate = (u) => {
|
|
221
|
+
if (pending.current !== controller || controller.signal.aborted)
|
|
222
|
+
return;
|
|
223
|
+
if (u.contextId)
|
|
224
|
+
contexts.current.set(selected, u.contextId);
|
|
225
|
+
setMessages(m => [...m.slice(0, -1), { role: "agent", text: u.text }]);
|
|
226
|
+
};
|
|
227
|
+
try {
|
|
228
|
+
onUpdate(await onSend({ q: c.q, agentId: selected, contextId: contexts.current.get(selected), signal: controller.signal, onUpdate }));
|
|
229
|
+
}
|
|
230
|
+
catch (e) {
|
|
231
|
+
if (pending.current === controller)
|
|
232
|
+
setMessages(m => [...m, { role: "error", text: controller.signal.aborted ? "Stopped" : e.message }]);
|
|
233
|
+
}
|
|
234
|
+
finally {
|
|
235
|
+
if (pending.current === controller) {
|
|
236
|
+
pending.current = null;
|
|
237
|
+
setBusy(false);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} });
|
|
173
241
|
}
|
package/dist/renderer.js
CHANGED
|
@@ -271,6 +271,49 @@ export function createA2uiRenderer(container, { onAction, resolveAsset, assetBas
|
|
|
271
271
|
break;
|
|
272
272
|
}
|
|
273
273
|
// ── AINUI components ──
|
|
274
|
+
case "FolderChat": {
|
|
275
|
+
const chat = resolve(c.value, surf, scope) || {};
|
|
276
|
+
el.className = "a2ui-column";
|
|
277
|
+
el.setAttribute("aria-label", "Folder chat");
|
|
278
|
+
const title = document.createElement("strong");
|
|
279
|
+
title.textContent = `Folder chat · ${chat.path || "/"}`;
|
|
280
|
+
const select = document.createElement("select");
|
|
281
|
+
select.setAttribute("aria-label", "Chat agent");
|
|
282
|
+
select.disabled = !!chat.busy;
|
|
283
|
+
for (const agent of chat.agents || []) {
|
|
284
|
+
const option = document.createElement("option");
|
|
285
|
+
option.value = agent.id;
|
|
286
|
+
option.textContent = agent.label;
|
|
287
|
+
select.appendChild(option);
|
|
288
|
+
}
|
|
289
|
+
select.value = chat.agentId || "";
|
|
290
|
+
select.addEventListener("change", () => fire(surfaceId, c, scope, { operation: "select", agentId: select.value }));
|
|
291
|
+
const log = document.createElement("div");
|
|
292
|
+
log.setAttribute("role", "log");
|
|
293
|
+
log.setAttribute("aria-live", "polite");
|
|
294
|
+
for (const message of chat.messages || []) {
|
|
295
|
+
const line = document.createElement("p");
|
|
296
|
+
line.style.whiteSpace = "pre-wrap";
|
|
297
|
+
line.textContent = `${message.role}: ${message.text}`;
|
|
298
|
+
if (message.role === "error")
|
|
299
|
+
line.setAttribute("role", "alert");
|
|
300
|
+
log.appendChild(line);
|
|
301
|
+
}
|
|
302
|
+
const form = document.createElement("form");
|
|
303
|
+
const input = document.createElement("textarea");
|
|
304
|
+
input.setAttribute("aria-label", "Message");
|
|
305
|
+
input.disabled = !!chat.busy;
|
|
306
|
+
const submit = document.createElement("button");
|
|
307
|
+
submit.type = chat.busy ? "button" : "submit";
|
|
308
|
+
submit.textContent = chat.busy ? "Stop" : "Send";
|
|
309
|
+
submit.addEventListener("click", () => { if (chat.busy)
|
|
310
|
+
void fire(surfaceId, c, scope, { operation: "cancel" }); });
|
|
311
|
+
form.addEventListener("submit", event => { event.preventDefault(); if (input.value.trim() && !chat.busy)
|
|
312
|
+
void fire(surfaceId, c, scope, { operation: "send", agentId: select.value, q: input.value.trim() }); });
|
|
313
|
+
form.append(input, submit);
|
|
314
|
+
el.append(title, select, log, form);
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
274
317
|
case "FileUpload": {
|
|
275
318
|
const label = document.createElement("label");
|
|
276
319
|
label.textContent = String(resolve(c.label, surf, scope) || "Upload file");
|
package/dist/styles.css
CHANGED
|
@@ -4,3 +4,10 @@
|
|
|
4
4
|
.ain-ui button:disabled { cursor:default; opacity:.5; }
|
|
5
5
|
.ain-ui img, .ain-ui video { max-width:100%; }
|
|
6
6
|
.ain-ui fieldset { border:0; padding:0; margin:0; min-width:0; }
|
|
7
|
+
.ain-ui label { display:block; margin-block:6px; }
|
|
8
|
+
.ain-ui label input[type=checkbox], .ain-ui label input[type=radio] { margin-inline-end:8px; }
|
|
9
|
+
.ain-ui input[type=text], .ain-ui input[type=number], .ain-ui input[type=password], .ain-ui textarea {
|
|
10
|
+
width:100%; box-sizing:border-box; padding:8px 10px; border:1px solid #8886; border-radius:6px; color:inherit; background:transparent;
|
|
11
|
+
}
|
|
12
|
+
.ain-ui button:focus-visible, .ain-ui input:focus-visible, .ain-ui textarea:focus-visible { outline:2px solid currentColor; outline-offset:2px; }
|
|
13
|
+
.ain-ui p { margin-block:6px; }
|