ain-ui 0.1.1 → 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 +16 -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 +15 -0
- package/dist/react.js +64 -1
- package/dist/renderer.js +43 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -113,3 +113,19 @@ render a `FileView` with the host's document, spreadsheet or other rich preview.
|
|
|
113
113
|
The URLs have already passed the renderer's URL checks and asset resolver.
|
|
114
114
|
Keep authentication and file permissions in the host's byte-serving routes.
|
|
115
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,6 +1,7 @@
|
|
|
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;
|
|
@@ -19,3 +20,17 @@ export declare function AinuiSurface({ messages, onAction, resolveAsset, renderF
|
|
|
19
20
|
resolveAsset?: AssetResolver;
|
|
20
21
|
renderFile?: FileViewRenderer;
|
|
21
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;
|
|
36
|
+
}): React.JSX.Element;
|
package/dist/react.js
CHANGED
|
@@ -6,6 +6,7 @@ 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";
|
|
9
10
|
const FileRenderer = createContext(undefined);
|
|
10
11
|
const Assets = createContext(() => "");
|
|
11
12
|
const box = { display: "flex", flexDirection: "column", gap: 8, minWidth: 0 };
|
|
@@ -143,6 +144,16 @@ const X402Payment = createBinderlessComponentImplementation(api("X402Payment", {
|
|
|
143
144
|
}
|
|
144
145
|
}, children: busy ? "Confirm in your wallet…" : `Pay ${amount} ${currency}` }), error && _jsx("small", { role: "alert", children: error })] });
|
|
145
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
|
+
});
|
|
146
157
|
// Preserve AIN-UI's confirmation extension on the basic Button.
|
|
147
158
|
const Button = createBinderlessComponentImplementation(api("Button", { child: CommonSchemas.ComponentId, action: CommonSchemas.Action.optional(), confirm: dynamic, variant: dynamic, tone: dynamic }), ({ context, buildChild }) => {
|
|
148
159
|
const p = context.componentModel.properties;
|
|
@@ -150,7 +161,7 @@ const Button = createBinderlessComponentImplementation(api("Button", { child: Co
|
|
|
150
161
|
return _jsx("button", { style: button, onClick: () => { if (!confirm || window.confirm(confirm))
|
|
151
162
|
void fire(context); }, children: buildChild(String(p.child)) });
|
|
152
163
|
});
|
|
153
|
-
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()]);
|
|
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()]);
|
|
154
165
|
export function AinuiSurface({ messages, onAction, resolveAsset, renderFile }) {
|
|
155
166
|
const handler = useRef(onAction);
|
|
156
167
|
handler.current = onAction;
|
|
@@ -176,3 +187,55 @@ export function AinuiSurface({ messages, onAction, resolveAsset, renderFile }) {
|
|
|
176
187
|
return _jsx("div", { className: "ain-ui", "aria-busy": "true" });
|
|
177
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)) }) }) }) });
|
|
178
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
|
+
} });
|
|
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");
|