@ai-slot/adapter-react 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-react
2
+
3
+ React renderer adapter for [ai-slot-component](https://github.com/iannil/ai-slot-component#readme): render validated component trees as real React components, plus an `<AiSlot>` wrapper around the Web Component.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @ai-slot/adapter-react
9
+ # peer deps: react >= 18, react-dom >= 18
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```tsx
15
+ import { AiSlot, treeToReact } from "@ai-slot/adapter-react";
16
+
17
+ // 1. Map component-tree nodes to your React components
18
+ const tree = treeToReact({ Banner }, modelOutput);
19
+
20
+ // 2. Or use the wrapper — plain markup inside 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,28 @@
1
+ import { ComponentNode, Registry } from '@ai-slot/registry';
2
+ import { ComponentType, ReactNode, ReactElement } from 'react';
3
+
4
+ type ReactComponentMap = Record<string, ComponentType<Record<string, unknown>>>;
5
+ /**
6
+ * 把 AI 组件树映射为 React 元素。未注册组件跳过并警告(渲染其余部分)。
7
+ * 默认槽位 children → 组件 children;命名槽位 → 同名 prop(渲染后的 ReactNode 数组)。
8
+ */
9
+ declare function treeToReact(components: ReactComponentMap, node: ComponentNode): ReactNode;
10
+
11
+ interface AiSlotProps {
12
+ src: string;
13
+ components: ReactComponentMap;
14
+ /** 提供时渲染前对 AI 输出再校验一次 */
15
+ registry?: Registry;
16
+ editable?: boolean;
17
+ /** 首屏与降级内容(对应 WC 的 light DOM 兜底) */
18
+ fallback: ReactNode;
19
+ /** 轮询间隔(秒),>0 时生效 */
20
+ refreshInterval?: number;
21
+ }
22
+ /**
23
+ * React 版 <ai-slot>:任何失败静默保留 fallback,语义与 Web Component 一致。
24
+ * `registry` 应为模块级常量或经 useMemo 稳定化——内联字面量会因引用变化触发重复加载。
25
+ */
26
+ declare function AiSlot(props: AiSlotProps): ReactElement;
27
+
28
+ export { AiSlot, type AiSlotProps, type ReactComponentMap, treeToReact };
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
1
+ // src/ai-slot.tsx
2
+ import { fetchComponentTree } from "@ai-slot/runtime";
3
+ import { useCallback, useEffect, useRef, useState } from "react";
4
+
5
+ // src/tree-to-react.ts
6
+ import { createElement, Fragment } from "react";
7
+ function treeToReact(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(
14
+ (child, i) => createElement(Fragment, { key: i }, treeToReact(components, child))
15
+ );
16
+ const props = { ...node.props ?? {} };
17
+ for (const [slotName, nodes] of Object.entries(node.slots ?? {})) {
18
+ props[slotName] = nodes.map(
19
+ (child, i) => createElement(Fragment, { key: i }, treeToReact(components, child))
20
+ );
21
+ }
22
+ return createElement(Comp, props, ...children);
23
+ }
24
+
25
+ // src/ai-slot.tsx
26
+ import { Fragment as Fragment2, jsx, jsxs } from "react/jsx-runtime";
27
+ function AiSlot(props) {
28
+ const [tree, setTree] = useState(null);
29
+ const [prompt, setPrompt] = useState("");
30
+ const seqRef = useRef(0);
31
+ const load = useCallback(
32
+ async (userPrompt) => {
33
+ const seq = ++seqRef.current;
34
+ const next = await fetchComponentTree({ src: props.src, userPrompt, registry: props.registry });
35
+ if (!next) return;
36
+ if (seq !== seqRef.current) return;
37
+ setTree(next);
38
+ },
39
+ [props.src, props.registry]
40
+ );
41
+ useEffect(() => {
42
+ void load();
43
+ if (props.refreshInterval && props.refreshInterval > 0) {
44
+ const timer = setInterval(() => void load(), props.refreshInterval * 1e3);
45
+ return () => clearInterval(timer);
46
+ }
47
+ }, [load, props.refreshInterval]);
48
+ const onSubmit = (event) => {
49
+ event.preventDefault();
50
+ const value = prompt.trim();
51
+ if (value) void load(value);
52
+ };
53
+ const restore = () => {
54
+ seqRef.current += 1;
55
+ setTree(null);
56
+ };
57
+ return /* @__PURE__ */ jsxs(Fragment2, { children: [
58
+ tree ? treeToReact(props.components, tree) : props.fallback,
59
+ props.editable ? /* @__PURE__ */ jsxs("form", { className: "ai-slot-editor", onSubmit, children: [
60
+ /* @__PURE__ */ jsx(
61
+ "input",
62
+ {
63
+ name: "prompt",
64
+ maxLength: 500,
65
+ placeholder: "\u7528\u4E00\u53E5\u8BDD\u8C03\u6574\u8FD9\u4E2A\u533A\u57DF\u2026",
66
+ value: prompt,
67
+ onChange: (e) => setPrompt(e.target.value)
68
+ }
69
+ ),
70
+ /* @__PURE__ */ jsx("button", { type: "submit", children: "\u5E94\u7528" }),
71
+ /* @__PURE__ */ jsx("button", { type: "button", onClick: restore, children: "\u6062\u590D\u9ED8\u8BA4" })
72
+ ] }) : null
73
+ ] });
74
+ }
75
+ export {
76
+ AiSlot,
77
+ treeToReact
78
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@ai-slot/adapter-react",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "description": "React renderer adapter for ai-slot: render validated component trees as real React components.",
6
+ "keywords": [
7
+ "react",
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-react"
26
+ },
27
+ "bugs": "https://github.com/iannil/ai-slot-component/issues",
28
+ "homepage": "https://github.com/iannil/ai-slot-component/tree/master/packages/adapter-react#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
+ "react": ">=18",
40
+ "react-dom": ">=18"
41
+ },
42
+ "devDependencies": {
43
+ "@types/react": "^18.3.0",
44
+ "@types/react-dom": "^18.3.0",
45
+ "jsdom": "^25.0.0",
46
+ "react": "^18.3.1",
47
+ "react-dom": "^18.3.1",
48
+ "tsup": "^8.3.0",
49
+ "typescript": "^5.6.0",
50
+ "vitest": "^2.1.0"
51
+ },
52
+ "scripts": {
53
+ "build": "tsup src/index.ts --format esm --dts --clean",
54
+ "test": "vitest run",
55
+ "typecheck": "tsc --noEmit"
56
+ }
57
+ }