@ai-gui/vue 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 +42 -0
- package/dist/index.cjs +210 -0
- package/dist/index.d.cts +38 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +183 -0
- package/package.json +58 -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,42 @@
|
|
|
1
|
+
# @ai-gui/vue
|
|
2
|
+
|
|
3
|
+
Vue adapter for [AIGUI](../../README.md) — renders a streaming LLM response into Vue, with app-defined cards and plugins.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @ai-gui/core @ai-gui/vue
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```vue
|
|
14
|
+
<script setup lang="ts">
|
|
15
|
+
import { ref } from "vue"
|
|
16
|
+
import { CardRegistry } from "@ai-gui/core"
|
|
17
|
+
import { AIRenderer } from "@ai-gui/vue"
|
|
18
|
+
|
|
19
|
+
const registry = new CardRegistry()
|
|
20
|
+
// A card render component receives props `data` and emits `action`.
|
|
21
|
+
registry.register({ type: "weather", description: "Weather summary", render: WeatherCard })
|
|
22
|
+
|
|
23
|
+
const r = ref<InstanceType<typeof AIRenderer>>()
|
|
24
|
+
|
|
25
|
+
async function ask() {
|
|
26
|
+
const res = await fetch("/api/chat")
|
|
27
|
+
r.value?.reset()
|
|
28
|
+
await r.value?.feed(res.body!) // also: r.value?.push(chunk)
|
|
29
|
+
}
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<template>
|
|
33
|
+
<AIRenderer ref="r" :registry="registry" @card-action="(a) => {/* app makes the real request */}" />
|
|
34
|
+
</template>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Exports
|
|
38
|
+
|
|
39
|
+
- `<AIRenderer :registry :plugins :sanitize @card-action />` — a render-function component; imperative `push` / `feed` / `reset` via a template ref (exposed).
|
|
40
|
+
- `useAIRenderer()` — composable returning `{ nodes, push, feed, reset }`.
|
|
41
|
+
|
|
42
|
+
Card component contract: props `data`, `emits: ['action']`. See the [root README](../../README.md) for cards, plugins, and `buildSystemPrompt`.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
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 vue = __toESM(require("vue"));
|
|
26
|
+
const __ai_gui_core = __toESM(require("@ai-gui/core"));
|
|
27
|
+
|
|
28
|
+
//#region src/use-ai-renderer.ts
|
|
29
|
+
function useAIRenderer(options = {}) {
|
|
30
|
+
const nodes = (0, vue.shallowRef)([]);
|
|
31
|
+
const renderer = new __ai_gui_core.Renderer({
|
|
32
|
+
...options,
|
|
33
|
+
plugins: options.plugins,
|
|
34
|
+
onPatch: (_patches, snapshot) => {
|
|
35
|
+
nodes.value = snapshot;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
nodes,
|
|
40
|
+
push: (c) => renderer.push(c),
|
|
41
|
+
feed: (s) => renderer.feed(s),
|
|
42
|
+
reset: () => {
|
|
43
|
+
renderer.reset();
|
|
44
|
+
nodes.value = [];
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/render-output.ts
|
|
51
|
+
/** Host element for imperative `mount` outputs, wired to Vue's lifecycle. */
|
|
52
|
+
const MountHost = (0, vue.defineComponent)({
|
|
53
|
+
name: "MountHost",
|
|
54
|
+
props: { mount: {
|
|
55
|
+
type: Function,
|
|
56
|
+
required: true
|
|
57
|
+
} },
|
|
58
|
+
setup(props) {
|
|
59
|
+
const elRef = (0, vue.ref)(null);
|
|
60
|
+
let cleanup;
|
|
61
|
+
(0, vue.onMounted)(() => {
|
|
62
|
+
if (elRef.value) cleanup = props.mount(elRef.value);
|
|
63
|
+
});
|
|
64
|
+
(0, vue.onBeforeUnmount)(() => {
|
|
65
|
+
if (typeof cleanup === "function") cleanup();
|
|
66
|
+
});
|
|
67
|
+
return () => (0, vue.h)("div", {
|
|
68
|
+
ref: elRef,
|
|
69
|
+
"data-aigui-mount": ""
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
/** Translate a framework-neutral RenderOutput into a Vue VNode. */
|
|
74
|
+
function renderOutput(out) {
|
|
75
|
+
switch (out.kind) {
|
|
76
|
+
case "html": return (0, vue.h)("div", { innerHTML: (0, __ai_gui_core.sanitizeHtml)(out.html) });
|
|
77
|
+
case "element": return (0, vue.h)(out.tag, out.props, (out.children ?? []).map(renderOutput));
|
|
78
|
+
case "mount": return (0, vue.h)(MountHost, { mount: out.mount });
|
|
79
|
+
case "card": return (0, vue.h)("pre", { "data-aigui-card-fallback": "" }, [(0, vue.h)("code", JSON.stringify(out.data, null, 2))]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Await an async RenderOutput, rendering a placeholder until it resolves. */
|
|
83
|
+
const AsyncOutput = (0, vue.defineComponent)({
|
|
84
|
+
name: "AsyncOutput",
|
|
85
|
+
props: { promise: {
|
|
86
|
+
type: Object,
|
|
87
|
+
required: true
|
|
88
|
+
} },
|
|
89
|
+
setup(props) {
|
|
90
|
+
const resolved = (0, vue.ref)(null);
|
|
91
|
+
props.promise.then((v) => {
|
|
92
|
+
resolved.value = v;
|
|
93
|
+
});
|
|
94
|
+
return () => resolved.value ? renderOutput(resolved.value) : (0, vue.h)("span", { "data-aigui-async-pending": "" });
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/render-node.ts
|
|
100
|
+
function renderNode(node, ctx) {
|
|
101
|
+
const r = (0, __ai_gui_core.collectNodeRenderers)(ctx.plugins)[node.type];
|
|
102
|
+
if (r) {
|
|
103
|
+
const out = r(node);
|
|
104
|
+
if (out && typeof out.then === "function") return (0, vue.h)(AsyncOutput, {
|
|
105
|
+
key: node.key,
|
|
106
|
+
promise: out
|
|
107
|
+
});
|
|
108
|
+
return renderOutput(out);
|
|
109
|
+
}
|
|
110
|
+
switch (node.type) {
|
|
111
|
+
case "heading": return (0, vue.h)(node.tag ?? "h1", {
|
|
112
|
+
key: node.key,
|
|
113
|
+
innerHTML: node.html ?? ""
|
|
114
|
+
});
|
|
115
|
+
case "paragraph": return (0, vue.h)("p", {
|
|
116
|
+
key: node.key,
|
|
117
|
+
innerHTML: node.html ?? ""
|
|
118
|
+
});
|
|
119
|
+
case "code": return (0, vue.h)("pre", {
|
|
120
|
+
key: node.key,
|
|
121
|
+
"data-lang": node.attrs?.lang
|
|
122
|
+
}, [(0, vue.h)("code", node.content ?? "")]);
|
|
123
|
+
case "hr": return (0, vue.h)("hr", { key: node.key });
|
|
124
|
+
case "html": return (0, vue.h)("div", {
|
|
125
|
+
key: node.key,
|
|
126
|
+
innerHTML: node.content ?? ""
|
|
127
|
+
});
|
|
128
|
+
case "card": return renderCard(node, ctx);
|
|
129
|
+
default: return (0, vue.h)("div", {
|
|
130
|
+
key: node.key,
|
|
131
|
+
innerHTML: node.html ?? (0, __ai_gui_core.sanitizeHtml)(node.content ?? "")
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function renderCard(node, ctx) {
|
|
136
|
+
const card = node.card;
|
|
137
|
+
if (!card) return (0, vue.h)("div", { key: node.key });
|
|
138
|
+
if (!card.complete) return (0, vue.h)("div", {
|
|
139
|
+
key: node.key,
|
|
140
|
+
"data-aigui-card-loading": "",
|
|
141
|
+
"data-card-type": card.type
|
|
142
|
+
});
|
|
143
|
+
if (!card.valid) return (0, vue.h)("pre", {
|
|
144
|
+
key: node.key,
|
|
145
|
+
"data-aigui-card-invalid": ""
|
|
146
|
+
}, [(0, vue.h)("code", JSON.stringify(card.data, null, 2))]);
|
|
147
|
+
const Comp = ctx.registry?.getRender(card.type);
|
|
148
|
+
if (!Comp) return (0, vue.h)("pre", {
|
|
149
|
+
key: node.key,
|
|
150
|
+
"data-aigui-card-fallback": ""
|
|
151
|
+
}, [(0, vue.h)("code", JSON.stringify(card.data, null, 2))]);
|
|
152
|
+
return (0, vue.h)(Comp, {
|
|
153
|
+
key: node.key,
|
|
154
|
+
data: card.data,
|
|
155
|
+
onAction: (a) => ctx.onCardAction?.({
|
|
156
|
+
...a,
|
|
157
|
+
cardType: card.type
|
|
158
|
+
})
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/ai-renderer.ts
|
|
164
|
+
const AIRenderer = (0, vue.defineComponent)({
|
|
165
|
+
name: "AIRenderer",
|
|
166
|
+
props: {
|
|
167
|
+
registry: {
|
|
168
|
+
type: Object,
|
|
169
|
+
default: void 0
|
|
170
|
+
},
|
|
171
|
+
plugins: {
|
|
172
|
+
type: Array,
|
|
173
|
+
default: void 0
|
|
174
|
+
},
|
|
175
|
+
sanitize: {
|
|
176
|
+
type: Boolean,
|
|
177
|
+
default: void 0
|
|
178
|
+
},
|
|
179
|
+
onCardAction: {
|
|
180
|
+
type: Function,
|
|
181
|
+
default: void 0
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
setup(props, { expose }) {
|
|
185
|
+
const { nodes, push, feed, reset } = useAIRenderer({
|
|
186
|
+
registry: props.registry,
|
|
187
|
+
sanitize: props.sanitize,
|
|
188
|
+
plugins: props.plugins
|
|
189
|
+
});
|
|
190
|
+
expose({
|
|
191
|
+
push,
|
|
192
|
+
feed,
|
|
193
|
+
reset
|
|
194
|
+
});
|
|
195
|
+
return () => {
|
|
196
|
+
const ctx = {
|
|
197
|
+
registry: props.registry,
|
|
198
|
+
plugins: props.plugins,
|
|
199
|
+
onCardAction: props.onCardAction
|
|
200
|
+
};
|
|
201
|
+
return (0, vue.h)("div", { "data-aigui-renderer": "" }, nodes.value.map((n) => renderNode(n, ctx)));
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
//#endregion
|
|
207
|
+
exports.AIRenderer = AIRenderer
|
|
208
|
+
exports.renderNode = renderNode
|
|
209
|
+
exports.renderOutput = renderOutput
|
|
210
|
+
exports.useAIRenderer = useAIRenderer
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ShallowRef, VNode } from "vue";
|
|
2
|
+
import { AIGuiPlugin, ASTNode, CardRegistry, RenderOutput, RendererOptions } from "@ai-gui/core";
|
|
3
|
+
|
|
4
|
+
//#region src/use-ai-renderer.d.ts
|
|
5
|
+
interface UseAIRendererResult {
|
|
6
|
+
nodes: ShallowRef<ASTNode[]>;
|
|
7
|
+
push: (chunk: string) => void;
|
|
8
|
+
feed: (source: AsyncIterable<string> | ReadableStream) => Promise<void>;
|
|
9
|
+
reset: () => void;
|
|
10
|
+
}
|
|
11
|
+
declare function useAIRenderer(options?: Omit<RendererOptions, "onPatch">): UseAIRendererResult;
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/ai-renderer.d.ts
|
|
15
|
+
declare const AIRenderer: any;
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/render-node.d.ts
|
|
19
|
+
interface RenderContext {
|
|
20
|
+
registry?: CardRegistry;
|
|
21
|
+
plugins?: AIGuiPlugin[];
|
|
22
|
+
onCardAction?: (action: {
|
|
23
|
+
type: string;
|
|
24
|
+
params?: unknown;
|
|
25
|
+
cardType: string;
|
|
26
|
+
}) => void;
|
|
27
|
+
}
|
|
28
|
+
declare function renderNode(node: ASTNode, ctx: RenderContext): VNode;
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/render-output.d.ts
|
|
32
|
+
/** Translate a framework-neutral RenderOutput into a Vue VNode. */
|
|
33
|
+
declare function renderOutput(out: RenderOutput): VNode;
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
/** Await an async RenderOutput, rendering a placeholder until it resolves. */
|
|
37
|
+
|
|
38
|
+
export { AIRenderer, RenderContext, UseAIRendererResult, renderNode, renderOutput, useAIRenderer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ShallowRef, VNode } from "vue";
|
|
2
|
+
import { AIGuiPlugin, ASTNode, CardRegistry, RenderOutput, RendererOptions } from "@ai-gui/core";
|
|
3
|
+
|
|
4
|
+
//#region src/use-ai-renderer.d.ts
|
|
5
|
+
interface UseAIRendererResult {
|
|
6
|
+
nodes: ShallowRef<ASTNode[]>;
|
|
7
|
+
push: (chunk: string) => void;
|
|
8
|
+
feed: (source: AsyncIterable<string> | ReadableStream) => Promise<void>;
|
|
9
|
+
reset: () => void;
|
|
10
|
+
}
|
|
11
|
+
declare function useAIRenderer(options?: Omit<RendererOptions, "onPatch">): UseAIRendererResult;
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/ai-renderer.d.ts
|
|
15
|
+
declare const AIRenderer: any;
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/render-node.d.ts
|
|
19
|
+
interface RenderContext {
|
|
20
|
+
registry?: CardRegistry;
|
|
21
|
+
plugins?: AIGuiPlugin[];
|
|
22
|
+
onCardAction?: (action: {
|
|
23
|
+
type: string;
|
|
24
|
+
params?: unknown;
|
|
25
|
+
cardType: string;
|
|
26
|
+
}) => void;
|
|
27
|
+
}
|
|
28
|
+
declare function renderNode(node: ASTNode, ctx: RenderContext): VNode;
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/render-output.d.ts
|
|
32
|
+
/** Translate a framework-neutral RenderOutput into a Vue VNode. */
|
|
33
|
+
declare function renderOutput(out: RenderOutput): VNode;
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
/** Await an async RenderOutput, rendering a placeholder until it resolves. */
|
|
37
|
+
|
|
38
|
+
export { AIRenderer, RenderContext, UseAIRendererResult, renderNode, renderOutput, useAIRenderer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { defineComponent, h, onBeforeUnmount, onMounted, ref, shallowRef } from "vue";
|
|
2
|
+
import { Renderer, collectNodeRenderers, sanitizeHtml } from "@ai-gui/core";
|
|
3
|
+
|
|
4
|
+
//#region src/use-ai-renderer.ts
|
|
5
|
+
function useAIRenderer(options = {}) {
|
|
6
|
+
const nodes = shallowRef([]);
|
|
7
|
+
const renderer = new Renderer({
|
|
8
|
+
...options,
|
|
9
|
+
plugins: options.plugins,
|
|
10
|
+
onPatch: (_patches, snapshot) => {
|
|
11
|
+
nodes.value = snapshot;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
nodes,
|
|
16
|
+
push: (c) => renderer.push(c),
|
|
17
|
+
feed: (s) => renderer.feed(s),
|
|
18
|
+
reset: () => {
|
|
19
|
+
renderer.reset();
|
|
20
|
+
nodes.value = [];
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/render-output.ts
|
|
27
|
+
/** Host element for imperative `mount` outputs, wired to Vue's lifecycle. */
|
|
28
|
+
const MountHost = defineComponent({
|
|
29
|
+
name: "MountHost",
|
|
30
|
+
props: { mount: {
|
|
31
|
+
type: Function,
|
|
32
|
+
required: true
|
|
33
|
+
} },
|
|
34
|
+
setup(props) {
|
|
35
|
+
const elRef = ref(null);
|
|
36
|
+
let cleanup;
|
|
37
|
+
onMounted(() => {
|
|
38
|
+
if (elRef.value) cleanup = props.mount(elRef.value);
|
|
39
|
+
});
|
|
40
|
+
onBeforeUnmount(() => {
|
|
41
|
+
if (typeof cleanup === "function") cleanup();
|
|
42
|
+
});
|
|
43
|
+
return () => h("div", {
|
|
44
|
+
ref: elRef,
|
|
45
|
+
"data-aigui-mount": ""
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
/** Translate a framework-neutral RenderOutput into a Vue VNode. */
|
|
50
|
+
function renderOutput(out) {
|
|
51
|
+
switch (out.kind) {
|
|
52
|
+
case "html": return h("div", { innerHTML: sanitizeHtml(out.html) });
|
|
53
|
+
case "element": return h(out.tag, out.props, (out.children ?? []).map(renderOutput));
|
|
54
|
+
case "mount": return h(MountHost, { mount: out.mount });
|
|
55
|
+
case "card": return h("pre", { "data-aigui-card-fallback": "" }, [h("code", JSON.stringify(out.data, null, 2))]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** Await an async RenderOutput, rendering a placeholder until it resolves. */
|
|
59
|
+
const AsyncOutput = defineComponent({
|
|
60
|
+
name: "AsyncOutput",
|
|
61
|
+
props: { promise: {
|
|
62
|
+
type: Object,
|
|
63
|
+
required: true
|
|
64
|
+
} },
|
|
65
|
+
setup(props) {
|
|
66
|
+
const resolved = ref(null);
|
|
67
|
+
props.promise.then((v) => {
|
|
68
|
+
resolved.value = v;
|
|
69
|
+
});
|
|
70
|
+
return () => resolved.value ? renderOutput(resolved.value) : h("span", { "data-aigui-async-pending": "" });
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/render-node.ts
|
|
76
|
+
function renderNode(node, ctx) {
|
|
77
|
+
const r = collectNodeRenderers(ctx.plugins)[node.type];
|
|
78
|
+
if (r) {
|
|
79
|
+
const out = r(node);
|
|
80
|
+
if (out && typeof out.then === "function") return h(AsyncOutput, {
|
|
81
|
+
key: node.key,
|
|
82
|
+
promise: out
|
|
83
|
+
});
|
|
84
|
+
return renderOutput(out);
|
|
85
|
+
}
|
|
86
|
+
switch (node.type) {
|
|
87
|
+
case "heading": return h(node.tag ?? "h1", {
|
|
88
|
+
key: node.key,
|
|
89
|
+
innerHTML: node.html ?? ""
|
|
90
|
+
});
|
|
91
|
+
case "paragraph": return h("p", {
|
|
92
|
+
key: node.key,
|
|
93
|
+
innerHTML: node.html ?? ""
|
|
94
|
+
});
|
|
95
|
+
case "code": return h("pre", {
|
|
96
|
+
key: node.key,
|
|
97
|
+
"data-lang": node.attrs?.lang
|
|
98
|
+
}, [h("code", node.content ?? "")]);
|
|
99
|
+
case "hr": return h("hr", { key: node.key });
|
|
100
|
+
case "html": return h("div", {
|
|
101
|
+
key: node.key,
|
|
102
|
+
innerHTML: node.content ?? ""
|
|
103
|
+
});
|
|
104
|
+
case "card": return renderCard(node, ctx);
|
|
105
|
+
default: return h("div", {
|
|
106
|
+
key: node.key,
|
|
107
|
+
innerHTML: node.html ?? sanitizeHtml(node.content ?? "")
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function renderCard(node, ctx) {
|
|
112
|
+
const card = node.card;
|
|
113
|
+
if (!card) return h("div", { key: node.key });
|
|
114
|
+
if (!card.complete) return h("div", {
|
|
115
|
+
key: node.key,
|
|
116
|
+
"data-aigui-card-loading": "",
|
|
117
|
+
"data-card-type": card.type
|
|
118
|
+
});
|
|
119
|
+
if (!card.valid) return h("pre", {
|
|
120
|
+
key: node.key,
|
|
121
|
+
"data-aigui-card-invalid": ""
|
|
122
|
+
}, [h("code", JSON.stringify(card.data, null, 2))]);
|
|
123
|
+
const Comp = ctx.registry?.getRender(card.type);
|
|
124
|
+
if (!Comp) return h("pre", {
|
|
125
|
+
key: node.key,
|
|
126
|
+
"data-aigui-card-fallback": ""
|
|
127
|
+
}, [h("code", JSON.stringify(card.data, null, 2))]);
|
|
128
|
+
return h(Comp, {
|
|
129
|
+
key: node.key,
|
|
130
|
+
data: card.data,
|
|
131
|
+
onAction: (a) => ctx.onCardAction?.({
|
|
132
|
+
...a,
|
|
133
|
+
cardType: card.type
|
|
134
|
+
})
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/ai-renderer.ts
|
|
140
|
+
const AIRenderer = defineComponent({
|
|
141
|
+
name: "AIRenderer",
|
|
142
|
+
props: {
|
|
143
|
+
registry: {
|
|
144
|
+
type: Object,
|
|
145
|
+
default: void 0
|
|
146
|
+
},
|
|
147
|
+
plugins: {
|
|
148
|
+
type: Array,
|
|
149
|
+
default: void 0
|
|
150
|
+
},
|
|
151
|
+
sanitize: {
|
|
152
|
+
type: Boolean,
|
|
153
|
+
default: void 0
|
|
154
|
+
},
|
|
155
|
+
onCardAction: {
|
|
156
|
+
type: Function,
|
|
157
|
+
default: void 0
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
setup(props, { expose }) {
|
|
161
|
+
const { nodes, push, feed, reset } = useAIRenderer({
|
|
162
|
+
registry: props.registry,
|
|
163
|
+
sanitize: props.sanitize,
|
|
164
|
+
plugins: props.plugins
|
|
165
|
+
});
|
|
166
|
+
expose({
|
|
167
|
+
push,
|
|
168
|
+
feed,
|
|
169
|
+
reset
|
|
170
|
+
});
|
|
171
|
+
return () => {
|
|
172
|
+
const ctx = {
|
|
173
|
+
registry: props.registry,
|
|
174
|
+
plugins: props.plugins,
|
|
175
|
+
onCardAction: props.onCardAction
|
|
176
|
+
};
|
|
177
|
+
return h("div", { "data-aigui-renderer": "" }, nodes.value.map((n) => renderNode(n, ctx)));
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
export { AIRenderer, renderNode, renderOutput, useAIRenderer };
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ai-gui/vue",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vue 3 adapter for AIGUI — stream LLM output into live Vue components with cards and plugins.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"llm",
|
|
7
|
+
"ai",
|
|
8
|
+
"streaming",
|
|
9
|
+
"markdown",
|
|
10
|
+
"vue",
|
|
11
|
+
"vue3",
|
|
12
|
+
"components",
|
|
13
|
+
"cards",
|
|
14
|
+
"aigui"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "Liang Li <ll_faw@hotmail.com>",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/liliang-cn/aigui.git",
|
|
21
|
+
"directory": "packages/vue"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/liliang-cn/aigui#readme",
|
|
24
|
+
"bugs": "https://github.com/liliang-cn/aigui/issues",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.js",
|
|
33
|
+
"require": "./dist/index.cjs"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"README.md",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@ai-gui/core": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"vue": ">=3.3"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"vue": "^3.5.12",
|
|
52
|
+
"@vue/test-utils": "^2.4.6"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsdown",
|
|
56
|
+
"typecheck": "tsc --noEmit"
|
|
57
|
+
}
|
|
58
|
+
}
|