@ai-gui/vanilla 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 +41 -0
- package/dist/index.cjs +257 -0
- package/dist/index.d.cts +34 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +231 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Liang Li
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @ai-gui/vanilla
|
|
2
|
+
|
|
3
|
+
Vanilla-DOM adapter for [AIGUI](../../README.md) — renders a streaming LLM response into a plain DOM element, no framework required.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @ai-gui/core @ai-gui/vanilla
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { CardRegistry } from "@ai-gui/core"
|
|
15
|
+
import { createRenderer } from "@ai-gui/vanilla"
|
|
16
|
+
|
|
17
|
+
const registry = new CardRegistry()
|
|
18
|
+
registry.register({
|
|
19
|
+
type: "weather",
|
|
20
|
+
description: "Weather summary",
|
|
21
|
+
// card render = (data, { onAction }) => HTMLElement
|
|
22
|
+
render: (data: any, { onAction }: any) => {
|
|
23
|
+
const el = document.createElement("div")
|
|
24
|
+
el.textContent = `${data.city} — ${data.tempC}°C`
|
|
25
|
+
return el
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const r = createRenderer(document.getElementById("out")!, {
|
|
30
|
+
registry,
|
|
31
|
+
onCardAction: ({ type, params, cardType }) => {/* app makes the real request */},
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
await r.feed(response.body!) // r.push(chunk) / r.reset() / r.destroy()
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Exports
|
|
38
|
+
|
|
39
|
+
- `createRenderer(el, { registry?, plugins?, sanitize?, onCardAction? })` → `{ push, feed, reset, destroy }`.
|
|
40
|
+
|
|
41
|
+
`destroy()` tears down the host and cleans up any mounted interactive widgets. See the [root README](../../README.md) for cards, plugins, and `buildSystemPrompt`.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
const __ai_gui_core = __toESM(require("@ai-gui/core"));
|
|
26
|
+
|
|
27
|
+
//#region src/render-output.ts
|
|
28
|
+
/** Translate a framework-neutral RenderOutput (from a plugin node renderer) into a DOM element. */
|
|
29
|
+
function renderOutputToElement(out) {
|
|
30
|
+
switch (out.kind) {
|
|
31
|
+
case "html": {
|
|
32
|
+
const el = document.createElement("div");
|
|
33
|
+
el.innerHTML = (0, __ai_gui_core.sanitizeHtml)(out.html);
|
|
34
|
+
return el;
|
|
35
|
+
}
|
|
36
|
+
case "element": {
|
|
37
|
+
const el = document.createElement(out.tag);
|
|
38
|
+
for (const [key, value] of Object.entries(out.props ?? {})) if (key === "class" || key === "className") el.className = String(value);
|
|
39
|
+
else el.setAttribute(key, String(value));
|
|
40
|
+
for (const child of out.children ?? []) el.appendChild(renderOutputToElement(child));
|
|
41
|
+
return el;
|
|
42
|
+
}
|
|
43
|
+
case "card": {
|
|
44
|
+
const pre = document.createElement("pre");
|
|
45
|
+
pre.setAttribute("data-aigui-card-fallback", "");
|
|
46
|
+
const code = document.createElement("code");
|
|
47
|
+
code.textContent = JSON.stringify(out.data, null, 2);
|
|
48
|
+
pre.appendChild(code);
|
|
49
|
+
return pre;
|
|
50
|
+
}
|
|
51
|
+
case "mount": {
|
|
52
|
+
const el = document.createElement("div");
|
|
53
|
+
el.setAttribute("data-aigui-mount", "");
|
|
54
|
+
queueMicrotask(() => {
|
|
55
|
+
const c = out.mount(el);
|
|
56
|
+
if (typeof c === "function") el.__aiguiCleanup = c;
|
|
57
|
+
});
|
|
58
|
+
return el;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/render-node-dom.ts
|
|
65
|
+
function renderNodeToElement(node, ctx) {
|
|
66
|
+
const r = (0, __ai_gui_core.collectNodeRenderers)(ctx.plugins)[node.type];
|
|
67
|
+
if (r) {
|
|
68
|
+
const out = r(node);
|
|
69
|
+
if (typeof out?.then === "function") {
|
|
70
|
+
const ph = document.createElement("div");
|
|
71
|
+
ph.setAttribute("data-aigui-async-pending", "");
|
|
72
|
+
out.then((res) => ph.replaceWith(renderOutputToElement(res)));
|
|
73
|
+
return ph;
|
|
74
|
+
}
|
|
75
|
+
return renderOutputToElement(out);
|
|
76
|
+
}
|
|
77
|
+
switch (node.type) {
|
|
78
|
+
case "heading": {
|
|
79
|
+
const el = document.createElement(node.tag ?? "h1");
|
|
80
|
+
el.innerHTML = node.html ?? "";
|
|
81
|
+
return el;
|
|
82
|
+
}
|
|
83
|
+
case "paragraph": {
|
|
84
|
+
const el = document.createElement("p");
|
|
85
|
+
el.innerHTML = node.html ?? "";
|
|
86
|
+
return el;
|
|
87
|
+
}
|
|
88
|
+
case "code": {
|
|
89
|
+
const pre = document.createElement("pre");
|
|
90
|
+
if (node.attrs?.lang) pre.setAttribute("data-lang", node.attrs.lang);
|
|
91
|
+
const code = document.createElement("code");
|
|
92
|
+
code.textContent = node.content ?? "";
|
|
93
|
+
pre.appendChild(code);
|
|
94
|
+
return pre;
|
|
95
|
+
}
|
|
96
|
+
case "hr": return document.createElement("hr");
|
|
97
|
+
case "html": {
|
|
98
|
+
const el = document.createElement("div");
|
|
99
|
+
el.innerHTML = node.content ?? "";
|
|
100
|
+
return el;
|
|
101
|
+
}
|
|
102
|
+
case "card": return renderCardElement(node, ctx);
|
|
103
|
+
default: {
|
|
104
|
+
const el = document.createElement("div");
|
|
105
|
+
el.innerHTML = node.html ?? (0, __ai_gui_core.sanitizeHtml)(node.content ?? "");
|
|
106
|
+
return el;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function renderCardElement(node, ctx) {
|
|
111
|
+
const card = node.card;
|
|
112
|
+
if (!card) return document.createElement("div");
|
|
113
|
+
if (!card.complete) {
|
|
114
|
+
const el = document.createElement("div");
|
|
115
|
+
el.setAttribute("data-aigui-card-loading", "");
|
|
116
|
+
el.setAttribute("data-card-type", card.type);
|
|
117
|
+
return el;
|
|
118
|
+
}
|
|
119
|
+
if (!card.valid) {
|
|
120
|
+
const pre = document.createElement("pre");
|
|
121
|
+
pre.setAttribute("data-aigui-card-invalid", "");
|
|
122
|
+
const c = document.createElement("code");
|
|
123
|
+
c.textContent = JSON.stringify(card.data, null, 2);
|
|
124
|
+
pre.appendChild(c);
|
|
125
|
+
return pre;
|
|
126
|
+
}
|
|
127
|
+
const factory = ctx.registry?.getRender(card.type);
|
|
128
|
+
if (!factory) {
|
|
129
|
+
const pre = document.createElement("pre");
|
|
130
|
+
pre.setAttribute("data-aigui-card-fallback", "");
|
|
131
|
+
const c = document.createElement("code");
|
|
132
|
+
c.textContent = JSON.stringify(card.data, null, 2);
|
|
133
|
+
pre.appendChild(c);
|
|
134
|
+
return pre;
|
|
135
|
+
}
|
|
136
|
+
const host = document.createElement("div");
|
|
137
|
+
host.setAttribute("data-aigui-card", card.type);
|
|
138
|
+
host.appendChild(factory(card.data, { onAction: (a) => ctx.onCardAction?.({
|
|
139
|
+
...a,
|
|
140
|
+
cardType: card.type
|
|
141
|
+
}) }));
|
|
142
|
+
return host;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/reconcile.ts
|
|
147
|
+
function createReconcileState() {
|
|
148
|
+
return { els: new Map() };
|
|
149
|
+
}
|
|
150
|
+
/** Run a mounted widget's cleanup (if any) before the element leaves the DOM. */
|
|
151
|
+
function disposeEl(el) {
|
|
152
|
+
el.__aiguiCleanup?.();
|
|
153
|
+
}
|
|
154
|
+
function reconcile(container, nodes, ctx, state) {
|
|
155
|
+
const nextKeys = new Set(nodes.map((n) => n.key));
|
|
156
|
+
for (const [key, entry] of state.els) if (!nextKeys.has(key)) {
|
|
157
|
+
disposeEl(entry.el);
|
|
158
|
+
entry.el.remove();
|
|
159
|
+
state.els.delete(key);
|
|
160
|
+
}
|
|
161
|
+
let prev = null;
|
|
162
|
+
for (const node of nodes) {
|
|
163
|
+
const hash = JSON.stringify(node);
|
|
164
|
+
let entry = state.els.get(node.key);
|
|
165
|
+
if (!entry) {
|
|
166
|
+
const el = renderNodeToElement(node, ctx);
|
|
167
|
+
entry = {
|
|
168
|
+
el,
|
|
169
|
+
hash
|
|
170
|
+
};
|
|
171
|
+
state.els.set(node.key, entry);
|
|
172
|
+
} else if (entry.hash !== hash) if (updateElementInPlace(entry.el, node, ctx)) entry.hash = hash;
|
|
173
|
+
else {
|
|
174
|
+
disposeEl(entry.el);
|
|
175
|
+
const el = renderNodeToElement(node, ctx);
|
|
176
|
+
entry.el.replaceWith(el);
|
|
177
|
+
entry.el = el;
|
|
178
|
+
entry.hash = hash;
|
|
179
|
+
}
|
|
180
|
+
const ref = prev ? prev.nextSibling : container.firstChild;
|
|
181
|
+
if (entry.el !== ref) container.insertBefore(entry.el, ref);
|
|
182
|
+
prev = entry.el;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/** Mutate an existing element to reflect the node without replacing it. Returns false when a fresh element is required. */
|
|
186
|
+
function updateElementInPlace(el, node, ctx) {
|
|
187
|
+
switch (node.type) {
|
|
188
|
+
case "heading":
|
|
189
|
+
if (el.tagName !== (node.tag ?? "h1").toUpperCase()) return false;
|
|
190
|
+
el.innerHTML = node.html ?? "";
|
|
191
|
+
return true;
|
|
192
|
+
case "paragraph":
|
|
193
|
+
if (el.tagName !== "P") return false;
|
|
194
|
+
el.innerHTML = node.html ?? "";
|
|
195
|
+
return true;
|
|
196
|
+
case "code": {
|
|
197
|
+
if (el.tagName !== "PRE") return false;
|
|
198
|
+
if (node.attrs?.lang) el.setAttribute("data-lang", node.attrs.lang);
|
|
199
|
+
let code = el.querySelector("code");
|
|
200
|
+
if (!code) {
|
|
201
|
+
code = document.createElement("code");
|
|
202
|
+
el.appendChild(code);
|
|
203
|
+
}
|
|
204
|
+
code.textContent = node.content ?? "";
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
case "hr": return el.tagName === "HR";
|
|
208
|
+
case "html":
|
|
209
|
+
if (el.tagName !== "DIV") return false;
|
|
210
|
+
el.innerHTML = node.content ?? "";
|
|
211
|
+
return true;
|
|
212
|
+
case "card": return false;
|
|
213
|
+
default:
|
|
214
|
+
if (el.tagName !== "DIV") return false;
|
|
215
|
+
el.innerHTML = node.html ?? (0, __ai_gui_core.sanitizeHtml)(node.content ?? "");
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/create-renderer.ts
|
|
222
|
+
function createRenderer(el, options = {}) {
|
|
223
|
+
const { onCardAction,...rendererOpts } = options;
|
|
224
|
+
const ctx = {
|
|
225
|
+
registry: options.registry,
|
|
226
|
+
onCardAction,
|
|
227
|
+
plugins: options.plugins
|
|
228
|
+
};
|
|
229
|
+
const state = createReconcileState();
|
|
230
|
+
const renderer = new __ai_gui_core.Renderer({
|
|
231
|
+
...rendererOpts,
|
|
232
|
+
onPatch: (_patches, nodes) => reconcile(el, nodes, ctx, state)
|
|
233
|
+
});
|
|
234
|
+
const disposeAll = () => {
|
|
235
|
+
for (const entry of state.els.values()) disposeEl(entry.el);
|
|
236
|
+
};
|
|
237
|
+
return {
|
|
238
|
+
push: (c) => renderer.push(c),
|
|
239
|
+
feed: (s) => renderer.feed(s),
|
|
240
|
+
reset: () => {
|
|
241
|
+
disposeAll();
|
|
242
|
+
renderer.reset();
|
|
243
|
+
state.els.clear();
|
|
244
|
+
el.replaceChildren();
|
|
245
|
+
},
|
|
246
|
+
destroy: () => {
|
|
247
|
+
disposeAll();
|
|
248
|
+
state.els.clear();
|
|
249
|
+
el.replaceChildren();
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
//#endregion
|
|
255
|
+
exports.createRenderer = createRenderer
|
|
256
|
+
exports.renderNodeToElement = renderNodeToElement
|
|
257
|
+
exports.renderOutputToElement = renderOutputToElement
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AIGuiPlugin, ASTNode, CardRegistry, RenderOutput, RendererOptions } from "@ai-gui/core";
|
|
2
|
+
|
|
3
|
+
//#region src/render-node-dom.d.ts
|
|
4
|
+
interface DomRenderContext {
|
|
5
|
+
registry?: CardRegistry;
|
|
6
|
+
onCardAction?: (action: {
|
|
7
|
+
type: string;
|
|
8
|
+
params?: unknown;
|
|
9
|
+
cardType: string;
|
|
10
|
+
}) => void;
|
|
11
|
+
plugins?: AIGuiPlugin[];
|
|
12
|
+
}
|
|
13
|
+
declare function renderNodeToElement(node: ASTNode, ctx: DomRenderContext): HTMLElement;
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/create-renderer.d.ts
|
|
17
|
+
interface CreateRendererOptions extends Omit<RendererOptions, "onPatch"> {
|
|
18
|
+
onCardAction?: DomRenderContext["onCardAction"];
|
|
19
|
+
}
|
|
20
|
+
interface VanillaRenderer {
|
|
21
|
+
push: (chunk: string) => void;
|
|
22
|
+
feed: (source: AsyncIterable<string> | ReadableStream) => Promise<void>;
|
|
23
|
+
reset: () => void;
|
|
24
|
+
destroy: () => void;
|
|
25
|
+
}
|
|
26
|
+
declare function createRenderer(el: HTMLElement, options?: CreateRendererOptions): VanillaRenderer;
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/render-output.d.ts
|
|
30
|
+
/** Translate a framework-neutral RenderOutput (from a plugin node renderer) into a DOM element. */
|
|
31
|
+
declare function renderOutputToElement(out: RenderOutput): HTMLElement;
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { CreateRendererOptions, DomRenderContext, VanillaRenderer, createRenderer, renderNodeToElement, renderOutputToElement };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AIGuiPlugin, ASTNode, CardRegistry, RenderOutput, RendererOptions } from "@ai-gui/core";
|
|
2
|
+
|
|
3
|
+
//#region src/render-node-dom.d.ts
|
|
4
|
+
interface DomRenderContext {
|
|
5
|
+
registry?: CardRegistry;
|
|
6
|
+
onCardAction?: (action: {
|
|
7
|
+
type: string;
|
|
8
|
+
params?: unknown;
|
|
9
|
+
cardType: string;
|
|
10
|
+
}) => void;
|
|
11
|
+
plugins?: AIGuiPlugin[];
|
|
12
|
+
}
|
|
13
|
+
declare function renderNodeToElement(node: ASTNode, ctx: DomRenderContext): HTMLElement;
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/create-renderer.d.ts
|
|
17
|
+
interface CreateRendererOptions extends Omit<RendererOptions, "onPatch"> {
|
|
18
|
+
onCardAction?: DomRenderContext["onCardAction"];
|
|
19
|
+
}
|
|
20
|
+
interface VanillaRenderer {
|
|
21
|
+
push: (chunk: string) => void;
|
|
22
|
+
feed: (source: AsyncIterable<string> | ReadableStream) => Promise<void>;
|
|
23
|
+
reset: () => void;
|
|
24
|
+
destroy: () => void;
|
|
25
|
+
}
|
|
26
|
+
declare function createRenderer(el: HTMLElement, options?: CreateRendererOptions): VanillaRenderer;
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/render-output.d.ts
|
|
30
|
+
/** Translate a framework-neutral RenderOutput (from a plugin node renderer) into a DOM element. */
|
|
31
|
+
declare function renderOutputToElement(out: RenderOutput): HTMLElement;
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { CreateRendererOptions, DomRenderContext, VanillaRenderer, createRenderer, renderNodeToElement, renderOutputToElement };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { Renderer, collectNodeRenderers, sanitizeHtml } from "@ai-gui/core";
|
|
2
|
+
|
|
3
|
+
//#region src/render-output.ts
|
|
4
|
+
/** Translate a framework-neutral RenderOutput (from a plugin node renderer) into a DOM element. */
|
|
5
|
+
function renderOutputToElement(out) {
|
|
6
|
+
switch (out.kind) {
|
|
7
|
+
case "html": {
|
|
8
|
+
const el = document.createElement("div");
|
|
9
|
+
el.innerHTML = sanitizeHtml(out.html);
|
|
10
|
+
return el;
|
|
11
|
+
}
|
|
12
|
+
case "element": {
|
|
13
|
+
const el = document.createElement(out.tag);
|
|
14
|
+
for (const [key, value] of Object.entries(out.props ?? {})) if (key === "class" || key === "className") el.className = String(value);
|
|
15
|
+
else el.setAttribute(key, String(value));
|
|
16
|
+
for (const child of out.children ?? []) el.appendChild(renderOutputToElement(child));
|
|
17
|
+
return el;
|
|
18
|
+
}
|
|
19
|
+
case "card": {
|
|
20
|
+
const pre = document.createElement("pre");
|
|
21
|
+
pre.setAttribute("data-aigui-card-fallback", "");
|
|
22
|
+
const code = document.createElement("code");
|
|
23
|
+
code.textContent = JSON.stringify(out.data, null, 2);
|
|
24
|
+
pre.appendChild(code);
|
|
25
|
+
return pre;
|
|
26
|
+
}
|
|
27
|
+
case "mount": {
|
|
28
|
+
const el = document.createElement("div");
|
|
29
|
+
el.setAttribute("data-aigui-mount", "");
|
|
30
|
+
queueMicrotask(() => {
|
|
31
|
+
const c = out.mount(el);
|
|
32
|
+
if (typeof c === "function") el.__aiguiCleanup = c;
|
|
33
|
+
});
|
|
34
|
+
return el;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/render-node-dom.ts
|
|
41
|
+
function renderNodeToElement(node, ctx) {
|
|
42
|
+
const r = collectNodeRenderers(ctx.plugins)[node.type];
|
|
43
|
+
if (r) {
|
|
44
|
+
const out = r(node);
|
|
45
|
+
if (typeof out?.then === "function") {
|
|
46
|
+
const ph = document.createElement("div");
|
|
47
|
+
ph.setAttribute("data-aigui-async-pending", "");
|
|
48
|
+
out.then((res) => ph.replaceWith(renderOutputToElement(res)));
|
|
49
|
+
return ph;
|
|
50
|
+
}
|
|
51
|
+
return renderOutputToElement(out);
|
|
52
|
+
}
|
|
53
|
+
switch (node.type) {
|
|
54
|
+
case "heading": {
|
|
55
|
+
const el = document.createElement(node.tag ?? "h1");
|
|
56
|
+
el.innerHTML = node.html ?? "";
|
|
57
|
+
return el;
|
|
58
|
+
}
|
|
59
|
+
case "paragraph": {
|
|
60
|
+
const el = document.createElement("p");
|
|
61
|
+
el.innerHTML = node.html ?? "";
|
|
62
|
+
return el;
|
|
63
|
+
}
|
|
64
|
+
case "code": {
|
|
65
|
+
const pre = document.createElement("pre");
|
|
66
|
+
if (node.attrs?.lang) pre.setAttribute("data-lang", node.attrs.lang);
|
|
67
|
+
const code = document.createElement("code");
|
|
68
|
+
code.textContent = node.content ?? "";
|
|
69
|
+
pre.appendChild(code);
|
|
70
|
+
return pre;
|
|
71
|
+
}
|
|
72
|
+
case "hr": return document.createElement("hr");
|
|
73
|
+
case "html": {
|
|
74
|
+
const el = document.createElement("div");
|
|
75
|
+
el.innerHTML = node.content ?? "";
|
|
76
|
+
return el;
|
|
77
|
+
}
|
|
78
|
+
case "card": return renderCardElement(node, ctx);
|
|
79
|
+
default: {
|
|
80
|
+
const el = document.createElement("div");
|
|
81
|
+
el.innerHTML = node.html ?? sanitizeHtml(node.content ?? "");
|
|
82
|
+
return el;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function renderCardElement(node, ctx) {
|
|
87
|
+
const card = node.card;
|
|
88
|
+
if (!card) return document.createElement("div");
|
|
89
|
+
if (!card.complete) {
|
|
90
|
+
const el = document.createElement("div");
|
|
91
|
+
el.setAttribute("data-aigui-card-loading", "");
|
|
92
|
+
el.setAttribute("data-card-type", card.type);
|
|
93
|
+
return el;
|
|
94
|
+
}
|
|
95
|
+
if (!card.valid) {
|
|
96
|
+
const pre = document.createElement("pre");
|
|
97
|
+
pre.setAttribute("data-aigui-card-invalid", "");
|
|
98
|
+
const c = document.createElement("code");
|
|
99
|
+
c.textContent = JSON.stringify(card.data, null, 2);
|
|
100
|
+
pre.appendChild(c);
|
|
101
|
+
return pre;
|
|
102
|
+
}
|
|
103
|
+
const factory = ctx.registry?.getRender(card.type);
|
|
104
|
+
if (!factory) {
|
|
105
|
+
const pre = document.createElement("pre");
|
|
106
|
+
pre.setAttribute("data-aigui-card-fallback", "");
|
|
107
|
+
const c = document.createElement("code");
|
|
108
|
+
c.textContent = JSON.stringify(card.data, null, 2);
|
|
109
|
+
pre.appendChild(c);
|
|
110
|
+
return pre;
|
|
111
|
+
}
|
|
112
|
+
const host = document.createElement("div");
|
|
113
|
+
host.setAttribute("data-aigui-card", card.type);
|
|
114
|
+
host.appendChild(factory(card.data, { onAction: (a) => ctx.onCardAction?.({
|
|
115
|
+
...a,
|
|
116
|
+
cardType: card.type
|
|
117
|
+
}) }));
|
|
118
|
+
return host;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/reconcile.ts
|
|
123
|
+
function createReconcileState() {
|
|
124
|
+
return { els: new Map() };
|
|
125
|
+
}
|
|
126
|
+
/** Run a mounted widget's cleanup (if any) before the element leaves the DOM. */
|
|
127
|
+
function disposeEl(el) {
|
|
128
|
+
el.__aiguiCleanup?.();
|
|
129
|
+
}
|
|
130
|
+
function reconcile(container, nodes, ctx, state) {
|
|
131
|
+
const nextKeys = new Set(nodes.map((n) => n.key));
|
|
132
|
+
for (const [key, entry] of state.els) if (!nextKeys.has(key)) {
|
|
133
|
+
disposeEl(entry.el);
|
|
134
|
+
entry.el.remove();
|
|
135
|
+
state.els.delete(key);
|
|
136
|
+
}
|
|
137
|
+
let prev = null;
|
|
138
|
+
for (const node of nodes) {
|
|
139
|
+
const hash = JSON.stringify(node);
|
|
140
|
+
let entry = state.els.get(node.key);
|
|
141
|
+
if (!entry) {
|
|
142
|
+
const el = renderNodeToElement(node, ctx);
|
|
143
|
+
entry = {
|
|
144
|
+
el,
|
|
145
|
+
hash
|
|
146
|
+
};
|
|
147
|
+
state.els.set(node.key, entry);
|
|
148
|
+
} else if (entry.hash !== hash) if (updateElementInPlace(entry.el, node, ctx)) entry.hash = hash;
|
|
149
|
+
else {
|
|
150
|
+
disposeEl(entry.el);
|
|
151
|
+
const el = renderNodeToElement(node, ctx);
|
|
152
|
+
entry.el.replaceWith(el);
|
|
153
|
+
entry.el = el;
|
|
154
|
+
entry.hash = hash;
|
|
155
|
+
}
|
|
156
|
+
const ref = prev ? prev.nextSibling : container.firstChild;
|
|
157
|
+
if (entry.el !== ref) container.insertBefore(entry.el, ref);
|
|
158
|
+
prev = entry.el;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Mutate an existing element to reflect the node without replacing it. Returns false when a fresh element is required. */
|
|
162
|
+
function updateElementInPlace(el, node, ctx) {
|
|
163
|
+
switch (node.type) {
|
|
164
|
+
case "heading":
|
|
165
|
+
if (el.tagName !== (node.tag ?? "h1").toUpperCase()) return false;
|
|
166
|
+
el.innerHTML = node.html ?? "";
|
|
167
|
+
return true;
|
|
168
|
+
case "paragraph":
|
|
169
|
+
if (el.tagName !== "P") return false;
|
|
170
|
+
el.innerHTML = node.html ?? "";
|
|
171
|
+
return true;
|
|
172
|
+
case "code": {
|
|
173
|
+
if (el.tagName !== "PRE") return false;
|
|
174
|
+
if (node.attrs?.lang) el.setAttribute("data-lang", node.attrs.lang);
|
|
175
|
+
let code = el.querySelector("code");
|
|
176
|
+
if (!code) {
|
|
177
|
+
code = document.createElement("code");
|
|
178
|
+
el.appendChild(code);
|
|
179
|
+
}
|
|
180
|
+
code.textContent = node.content ?? "";
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
case "hr": return el.tagName === "HR";
|
|
184
|
+
case "html":
|
|
185
|
+
if (el.tagName !== "DIV") return false;
|
|
186
|
+
el.innerHTML = node.content ?? "";
|
|
187
|
+
return true;
|
|
188
|
+
case "card": return false;
|
|
189
|
+
default:
|
|
190
|
+
if (el.tagName !== "DIV") return false;
|
|
191
|
+
el.innerHTML = node.html ?? sanitizeHtml(node.content ?? "");
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/create-renderer.ts
|
|
198
|
+
function createRenderer(el, options = {}) {
|
|
199
|
+
const { onCardAction,...rendererOpts } = options;
|
|
200
|
+
const ctx = {
|
|
201
|
+
registry: options.registry,
|
|
202
|
+
onCardAction,
|
|
203
|
+
plugins: options.plugins
|
|
204
|
+
};
|
|
205
|
+
const state = createReconcileState();
|
|
206
|
+
const renderer = new Renderer({
|
|
207
|
+
...rendererOpts,
|
|
208
|
+
onPatch: (_patches, nodes) => reconcile(el, nodes, ctx, state)
|
|
209
|
+
});
|
|
210
|
+
const disposeAll = () => {
|
|
211
|
+
for (const entry of state.els.values()) disposeEl(entry.el);
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
push: (c) => renderer.push(c),
|
|
215
|
+
feed: (s) => renderer.feed(s),
|
|
216
|
+
reset: () => {
|
|
217
|
+
disposeAll();
|
|
218
|
+
renderer.reset();
|
|
219
|
+
state.els.clear();
|
|
220
|
+
el.replaceChildren();
|
|
221
|
+
},
|
|
222
|
+
destroy: () => {
|
|
223
|
+
disposeAll();
|
|
224
|
+
state.els.clear();
|
|
225
|
+
el.replaceChildren();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
//#endregion
|
|
231
|
+
export { createRenderer, renderNodeToElement, renderOutputToElement };
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ai-gui/vanilla",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-free DOM adapter for AIGUI — stream LLM output straight into the DOM.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"llm",
|
|
7
|
+
"ai",
|
|
8
|
+
"streaming",
|
|
9
|
+
"markdown",
|
|
10
|
+
"vanilla",
|
|
11
|
+
"dom",
|
|
12
|
+
"framework-free",
|
|
13
|
+
"aigui"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Liang Li <ll_faw@hotmail.com>",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/liliang-cn/aigui.git",
|
|
20
|
+
"directory": "packages/vanilla"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/liliang-cn/aigui#readme",
|
|
23
|
+
"bugs": "https://github.com/liliang-cn/aigui/issues",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@ai-gui/core": "0.1.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsdown",
|
|
48
|
+
"typecheck": "tsc --noEmit"
|
|
49
|
+
}
|
|
50
|
+
}
|