@jboltai/tokui-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 jboltai.com
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,52 @@
1
+ # @jboltai/tokui-vue
2
+
3
+ > TokUI 的 Vue 3 适配器:`<TokUIView>` 组件 + `useTokUIStream()` 组合式。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @jboltai/tokui-vue vue
9
+ ```
10
+
11
+ ## 声明式渲染
12
+
13
+ ```vue
14
+ <script setup>
15
+ import { TokUIView } from '@jboltai/tokui-vue';
16
+ const dsl = '[card tt:你好 TokUI][p 流式 UI,零依赖][/card]';
17
+ </script>
18
+
19
+ <template>
20
+ <TokUIView :dsl="dsl" theme="default" />
21
+ </template>
22
+ ```
23
+
24
+ `dsl` 变化时自动重新渲染;组件卸载时自动断开清理。
25
+
26
+ ## 流式 / SSE
27
+
28
+ ```vue
29
+ <script setup>
30
+ import { useTokUIStream } from '@jboltai/tokui-vue';
31
+ const { container, start, feed, end, connect } = useTokUIStream();
32
+
33
+ function render() {
34
+ start();
35
+ feed('[card tt:');
36
+ feed('流式卡片]');
37
+ end();
38
+ }
39
+ </script>
40
+
41
+ <template>
42
+ <div ref="container" />
43
+ <button @click="render">渲染</button>
44
+ <button @click="connect('/api/chat', { prompt: '画登录卡片' })">SSE 连接</button>
45
+ </template>
46
+ ```
47
+
48
+ ## CSS
49
+
50
+ 样式随 `@jboltai/tokui` 自动注入,无需手动引 CSS。
51
+
52
+ License: MIT
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@jboltai/tokui-vue",
3
+ "version": "0.1.0",
4
+ "description": "TokUI Vue 3 adapter — <TokUIView> component and useTokUIStream() composable for streaming DSL UI.",
5
+ "license": "MIT",
6
+ "author": "sdxiaomu (https://jboltai.com)",
7
+ "homepage": "https://github.com/jboltai/tokui",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/jboltai/tokui.git",
11
+ "directory": "packages/vue"
12
+ },
13
+ "main": "src/index.js",
14
+ "module": "src/index.js",
15
+ "types": "src/index.d.ts",
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./src/index.d.ts",
20
+ "import": "./src/index.js",
21
+ "require": "./src/index.js",
22
+ "default": "./src/index.js"
23
+ },
24
+ "./package.json": "./package.json"
25
+ },
26
+ "dependencies": {
27
+ "@jboltai/tokui": "^0.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "vue": ">=3.0.0"
31
+ },
32
+ "peerDependenciesMeta": {
33
+ "vue": {
34
+ "optional": false
35
+ }
36
+ },
37
+ "sideEffects": [
38
+ "**/*.css"
39
+ ],
40
+ "files": [
41
+ "src",
42
+ "README.md"
43
+ ],
44
+ "keywords": [
45
+ "tokui",
46
+ "vue",
47
+ "vue3",
48
+ "streaming-ui",
49
+ "dsl",
50
+ "composable"
51
+ ],
52
+ "publishConfig": {
53
+ "access": "public"
54
+ }
55
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { Component, Ref } from 'vue';
2
+ import type { TokUI as TokUIClass, TokUIEventCallback } from '@jboltai/tokui';
3
+
4
+ export interface TokUIViewProps {
5
+ dsl?: string;
6
+ theme?: string;
7
+ onEvent?: TokUIEventCallback;
8
+ }
9
+
10
+ export declare const TokUIView: Component;
11
+
12
+ export interface UseTokUIStreamReturn {
13
+ container: Ref<HTMLDivElement | null>;
14
+ start: (container?: HTMLDivElement) => void;
15
+ feed: (chunk: string) => void;
16
+ end: () => void;
17
+ connect: (url: string, body?: Record<string, any>) => Promise<void>;
18
+ disconnect: () => void;
19
+ }
20
+
21
+ export declare function useTokUIStream(options?: {
22
+ theme?: string;
23
+ onEvent?: TokUIEventCallback;
24
+ }): UseTokUIStreamReturn;
25
+
26
+ export default TokUIView;
package/src/index.js ADDED
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ // TokUI Vue 3 适配器:<TokUIView> 组件 + useTokUIStream() 组合式。
4
+ // 用 h() 渲染函数(ref 对象直接传入),避免模板编译依赖,兼容任意 Vue 3 项目。
5
+ import { defineComponent, h, ref, onMounted, onBeforeUnmount, watch } from 'vue';
6
+ import { TokUI, setTheme } from '@jboltai/tokui';
7
+
8
+ /**
9
+ * 声明式渲染一段 TokUI DSL。
10
+ * @param {object} props
11
+ * @param {string} [props.dsl] DSL 文本(变化时重新渲染)
12
+ * @param {string} [props.theme] 主题名
13
+ * @param {function} [props.onEvent] 事件回调 (name, data)
14
+ */
15
+ export const TokUIView = defineComponent({
16
+ name: 'TokUIView',
17
+ props: {
18
+ dsl: { type: String, default: '' },
19
+ theme: { type: String, default: '' },
20
+ onEvent: { type: Function, default: null }
21
+ },
22
+ setup(props) {
23
+ const container = ref(null);
24
+ let ui = null;
25
+
26
+ onMounted(() => {
27
+ if (!container.value) return;
28
+ ui = new TokUI({
29
+ container: container.value,
30
+ theme: props.theme || undefined,
31
+ onEvent: props.onEvent || undefined
32
+ });
33
+ if (props.theme) { try { setTheme(props.theme); } catch (e) { /* ignore */ } }
34
+ if (props.dsl) ui.render(props.dsl);
35
+ });
36
+
37
+ watch(() => props.dsl, (v) => { if (ui && v) ui.render(v); });
38
+
39
+ onBeforeUnmount(() => {
40
+ if (ui && ui.disconnect) ui.disconnect();
41
+ ui = null;
42
+ });
43
+
44
+ // Vue 3 渲染函数:把 ref 对象直接作为 ref 传入,元素挂载后自动绑定到 container。
45
+ return () => h('div', { ref: container, class: 'tokui-vue-view' });
46
+ }
47
+ });
48
+
49
+ /**
50
+ * 流式渲染组合式(手动 feed / SSE connect)。
51
+ * @param {object} [options] TokUI 构造选项(theme/onEvent 等)
52
+ * @returns {{container, start, feed, end, connect, disconnect}}
53
+ */
54
+ export function useTokUIStream(options) {
55
+ const container = ref(null);
56
+ let ui = null;
57
+
58
+ const ensure = () => {
59
+ if (!ui && container.value) {
60
+ ui = new TokUI({ container: container.value, ...(options || {}) });
61
+ }
62
+ return ui;
63
+ };
64
+ const start = (c) => { const u = ensure(); if (u) u.startStream(c || container.value); };
65
+ const feed = (chunk) => { const u = ensure(); if (u) u.feed(chunk); };
66
+ const end = () => { if (ui) ui.endStream(); };
67
+ const connect = (url, body) => { const u = ensure(); return u ? u.connect(url, body) : Promise.resolve(); };
68
+ const disconnect = () => { if (ui && ui.disconnect) ui.disconnect(); };
69
+
70
+ onBeforeUnmount(() => disconnect());
71
+
72
+ return { container, start, feed, end, connect, disconnect };
73
+ }
74
+
75
+ export default TokUIView;