@ai-slot/adapter-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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 iannil
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,32 @@
1
+ # @ai-slot/adapter-vue
2
+
3
+ Vue 3 renderer adapter for [ai-slot-component](https://github.com/iannil/ai-slot-component#readme): render validated component trees as real Vue components, plus an `<AiSlot>` wrapper around the Web Component.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @ai-slot/adapter-vue
9
+ # peer dep: vue >= 3.4
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ts
15
+ import { AiSlot, treeToVue } from "@ai-slot/adapter-vue";
16
+
17
+ // 1. Map component-tree nodes to your Vue components
18
+ const vnode = treeToVue({ Banner }, modelOutput);
19
+
20
+ // 2. Or use the wrapper — the default slot is the fallback
21
+ // <AiSlot name="hero" src="/ai-render/hero" stream>
22
+ // <h1>Original content</h1>
23
+ // </AiSlot>
24
+ ```
25
+
26
+ ## Docs
27
+
28
+ - [Root README](https://github.com/iannil/ai-slot-component#readme)
29
+
30
+ ## License
31
+
32
+ MIT
@@ -0,0 +1,63 @@
1
+ import * as vue from 'vue';
2
+ import { Component, VNode, PropType } from 'vue';
3
+ import { ComponentNode, Registry } from '@ai-slot/registry';
4
+
5
+ type VueComponentMap = Record<string, Component>;
6
+ /**
7
+ * 把 AI 组件树映射为 Vue VNode。未注册组件跳过并警告(渲染其余部分)。
8
+ * 默认槽位 children → 默认 slot;命名槽位 → 同名 prop(渲染后的 VNode 数组)。
9
+ */
10
+ declare function treeToVue(components: VueComponentMap, node: ComponentNode): VNode | null;
11
+
12
+ /** Vue 版 <ai-slot>:任何失败静默保留默认 slot(fallback),语义与 Web Component 一致。 */
13
+ declare const AiSlot: vue.DefineComponent<vue.ExtractPropTypes<{
14
+ src: {
15
+ type: StringConstructor;
16
+ required: true;
17
+ };
18
+ components: {
19
+ type: PropType<VueComponentMap>;
20
+ required: true;
21
+ };
22
+ registry: {
23
+ type: PropType<Registry | undefined>;
24
+ default: undefined;
25
+ };
26
+ editable: {
27
+ type: BooleanConstructor;
28
+ default: boolean;
29
+ };
30
+ refreshInterval: {
31
+ type: NumberConstructor;
32
+ default: number;
33
+ };
34
+ }>, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
35
+ [key: string]: any;
36
+ }>, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
37
+ src: {
38
+ type: StringConstructor;
39
+ required: true;
40
+ };
41
+ components: {
42
+ type: PropType<VueComponentMap>;
43
+ required: true;
44
+ };
45
+ registry: {
46
+ type: PropType<Registry | undefined>;
47
+ default: undefined;
48
+ };
49
+ editable: {
50
+ type: BooleanConstructor;
51
+ default: boolean;
52
+ };
53
+ refreshInterval: {
54
+ type: NumberConstructor;
55
+ default: number;
56
+ };
57
+ }>> & Readonly<{}>, {
58
+ registry: Registry | undefined;
59
+ editable: boolean;
60
+ refreshInterval: number;
61
+ }, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
62
+
63
+ export { AiSlot, type VueComponentMap, treeToVue };
package/dist/index.js ADDED
@@ -0,0 +1,80 @@
1
+ // src/ai-slot.ts
2
+ import { fetchComponentTree } from "@ai-slot/runtime";
3
+ import { defineComponent, h as h2, onMounted, onUnmounted, ref } from "vue";
4
+
5
+ // src/tree-to-vue.ts
6
+ import { h } from "vue";
7
+ function treeToVue(components, node) {
8
+ const Comp = Object.hasOwn(components, node.component) ? components[node.component] : void 0;
9
+ if (!Comp) {
10
+ console.warn(`[ai-slot] \u7EC4\u4EF6\u672A\u5728\u5BA2\u6237\u7AEF\u6CE8\u518C\uFF0C\u5DF2\u8DF3\u8FC7: ${node.component}`);
11
+ return null;
12
+ }
13
+ const children = (node.children ?? []).map((child) => treeToVue(components, child)).filter((v) => v !== null);
14
+ const props = { ...node.props ?? {} };
15
+ for (const [slotName, nodes] of Object.entries(node.slots ?? {})) {
16
+ props[slotName] = nodes.map((child) => treeToVue(components, child)).filter((v) => v !== null);
17
+ }
18
+ return h(Comp, props, () => children);
19
+ }
20
+
21
+ // src/ai-slot.ts
22
+ var AiSlot = defineComponent({
23
+ name: "AiSlot",
24
+ props: {
25
+ src: { type: String, required: true },
26
+ components: { type: Object, required: true },
27
+ registry: { type: Object, default: void 0 },
28
+ editable: { type: Boolean, default: false },
29
+ refreshInterval: { type: Number, default: 0 }
30
+ },
31
+ setup(props, { slots }) {
32
+ const tree = ref(null);
33
+ const prompt = ref("");
34
+ let seq = 0;
35
+ let timer;
36
+ async function load(userPrompt) {
37
+ const mySeq = ++seq;
38
+ const next = await fetchComponentTree({ src: props.src, userPrompt, registry: props.registry });
39
+ if (!next) return;
40
+ if (mySeq !== seq) return;
41
+ tree.value = next;
42
+ }
43
+ function restore() {
44
+ seq += 1;
45
+ tree.value = null;
46
+ }
47
+ onMounted(() => {
48
+ void load();
49
+ if (props.refreshInterval > 0) {
50
+ timer = setInterval(() => void load(), props.refreshInterval * 1e3);
51
+ }
52
+ });
53
+ onUnmounted(() => clearInterval(timer));
54
+ function onSubmit(event) {
55
+ event.preventDefault();
56
+ const value = prompt.value.trim();
57
+ if (value) void load(value);
58
+ }
59
+ return () => h2("span", { style: "display: contents" }, [
60
+ tree.value ? treeToVue(props.components, tree.value) : slots.default?.() ?? null,
61
+ props.editable ? h2("form", { class: "ai-slot-editor", onSubmit }, [
62
+ h2("input", {
63
+ name: "prompt",
64
+ maxlength: 500,
65
+ placeholder: "\u7528\u4E00\u53E5\u8BDD\u8C03\u6574\u8FD9\u4E2A\u533A\u57DF\u2026",
66
+ value: prompt.value,
67
+ onInput: (e) => {
68
+ prompt.value = e.target.value;
69
+ }
70
+ }),
71
+ h2("button", { type: "submit" }, "\u5E94\u7528"),
72
+ h2("button", { type: "button", onClick: restore }, "\u6062\u590D\u9ED8\u8BA4")
73
+ ]) : null
74
+ ]);
75
+ }
76
+ });
77
+ export {
78
+ AiSlot,
79
+ treeToVue
80
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@ai-slot/adapter-vue",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "description": "Vue 3 renderer adapter for ai-slot: render validated component trees as real Vue components.",
6
+ "keywords": [
7
+ "vue",
8
+ "ai",
9
+ "renderer",
10
+ "adapter"
11
+ ],
12
+ "type": "module",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/iannil/ai-slot-component.git",
25
+ "directory": "packages/adapter-vue"
26
+ },
27
+ "bugs": "https://github.com/iannil/ai-slot-component/issues",
28
+ "homepage": "https://github.com/iannil/ai-slot-component/tree/master/packages/adapter-vue#readme",
29
+ "publishConfig": {
30
+ "access": "public",
31
+ "registry": "https://registry.npmjs.org/"
32
+ },
33
+ "sideEffects": false,
34
+ "dependencies": {
35
+ "@ai-slot/registry": "0.1.0",
36
+ "@ai-slot/runtime": "0.1.0"
37
+ },
38
+ "peerDependencies": {
39
+ "vue": ">=3.4"
40
+ },
41
+ "devDependencies": {
42
+ "@vue/server-renderer": "^3.5.0",
43
+ "jsdom": "^25.0.0",
44
+ "tsup": "^8.3.0",
45
+ "typescript": "^5.6.0",
46
+ "vitest": "^2.1.0",
47
+ "vue": "^3.5.0"
48
+ },
49
+ "scripts": {
50
+ "build": "tsup src/index.ts --format esm --dts --clean",
51
+ "test": "vitest run",
52
+ "typecheck": "tsc --noEmit"
53
+ }
54
+ }