@zhin.js/client 1.0.0 → 1.0.1

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.
Files changed (43) hide show
  1. package/README.md +334 -67
  2. package/app/index.html +4 -3
  3. package/app/postcss.config.js +5 -0
  4. package/app/src/components/ThemeToggle.tsx +21 -0
  5. package/app/src/hooks/useTheme.ts +17 -0
  6. package/app/src/layouts/dashboard.tsx +259 -0
  7. package/app/src/main.tsx +121 -0
  8. package/app/src/pages/dashboard-bots.tsx +198 -0
  9. package/app/src/pages/dashboard-home.tsx +301 -0
  10. package/app/src/pages/dashboard-logs.tsx +298 -0
  11. package/app/src/pages/dashboard-plugin-detail.tsx +360 -0
  12. package/app/src/pages/dashboard-plugins.tsx +166 -0
  13. package/app/src/style.css +1105 -0
  14. package/app/src/theme/index.ts +92 -0
  15. package/app/tailwind.config.js +70 -0
  16. package/app/tsconfig.json +5 -0
  17. package/dist/index.js +15 -3
  18. package/package.json +20 -7
  19. package/src/index.ts +19 -3
  20. package/src/router/index.tsx +55 -0
  21. package/src/store/index.ts +111 -0
  22. package/src/store/reducers/index.ts +16 -0
  23. package/src/store/reducers/route.ts +122 -0
  24. package/src/store/reducers/script.ts +103 -0
  25. package/src/store/reducers/ui.ts +31 -0
  26. package/src/types.ts +11 -17
  27. package/src/websocket/index.ts +193 -0
  28. package/src/websocket/useWebSocket.ts +42 -0
  29. package/app/components.d.ts +0 -33
  30. package/app/src/App.vue +0 -7
  31. package/app/src/main.ts +0 -127
  32. package/app/src/pages/$.vue +0 -899
  33. package/app/src/pages/404.vue +0 -11
  34. package/app/src/pages/contexts/overview.vue +0 -177
  35. package/app/src/pages/dashboard.vue +0 -323
  36. package/app/src/pages/plugins/installed.vue +0 -734
  37. package/app/src/pages/system/status.vue +0 -241
  38. package/app/src/services/api.ts +0 -155
  39. package/app/src/styles/README.md +0 -202
  40. package/app/src/styles/common.css +0 -0
  41. package/global.d.ts +0 -19
  42. package/src/router.ts +0 -44
  43. package/src/store.ts +0 -53
@@ -0,0 +1,31 @@
1
+ import { createSlice, PayloadAction } from "@reduxjs/toolkit"
2
+
3
+ export interface UIState {
4
+ sidebarOpen: boolean
5
+ activeMenu: string
6
+ }
7
+
8
+ const initialState: UIState = {
9
+ sidebarOpen: true,
10
+ activeMenu: 'home'
11
+ }
12
+
13
+ export const uiSlice = createSlice({
14
+ name: 'ui',
15
+ initialState,
16
+ reducers: {
17
+ toggleSidebar: (state) => {
18
+ state.sidebarOpen = !state.sidebarOpen
19
+ },
20
+ setSidebarOpen: (state, action: PayloadAction<boolean>) => {
21
+ state.sidebarOpen = action.payload
22
+ },
23
+ setActiveMenu: (state, action: PayloadAction<string>) => {
24
+ state.activeMenu = action.payload
25
+ }
26
+ }
27
+ })
28
+
29
+ export const { toggleSidebar, setSidebarOpen, setActiveMenu } = uiSlice.actions
30
+ export default uiSlice.reducer
31
+
package/src/types.ts CHANGED
@@ -1,18 +1,12 @@
1
- import { RouteComponent } from 'vue-router';
2
-
3
- export interface MenuInfo {
4
- icon?: string;
5
- parentName?: string;
6
- children?: MenuInfo[];
7
- path: string;
8
- name: string;
1
+ // 消息段类型定义 - 客户端版本
2
+ export interface MessageSegment {
3
+ type: 'text' | 'image' | 'at' | 'face' | 'video' | 'audio' | 'file' | 'reply'
4
+ data: Record<string, any>
9
5
  }
10
- export type MenuWithComponent = {
11
- component: RouteComponent;
12
- children?: MenuWithComponent[];
13
- } & MenuInfo;
14
- export type ToolInfo = {
15
- name?: string;
16
- icon?: string;
17
- component: RouteComponent;
18
- };
6
+
7
+ // 消息元素别名(兼容不同命名习惯)
8
+ export type MessageElement = MessageSegment
9
+
10
+ // 发送内容类型
11
+ export type SendContent = MessageSegment | MessageSegment[] | string
12
+
@@ -0,0 +1,193 @@
1
+ import { store, loadScripts, loadScript, unloadScript } from '../store'
2
+ /**
3
+ * WebSocket 客户端管理
4
+ * 用于连接服务器并接收动态入口脚本
5
+ * 脚本加载状态由 Redux store 管理
6
+ */
7
+
8
+ export interface WebSocketMessage {
9
+ type: 'sync' | 'add' | 'delete' | 'init-data' | 'data-update'
10
+ data?: {
11
+ key: string
12
+ value: any
13
+ }
14
+ timestamp?: number
15
+ }
16
+
17
+ export interface WebSocketManagerOptions {
18
+ url?: string
19
+ reconnectInterval?: number
20
+ maxReconnectAttempts?: number
21
+ onConnect?: () => void
22
+ onDisconnect?: () => void
23
+ onError?: (error: Event) => void
24
+ onMessage?: (message: WebSocketMessage) => void
25
+ }
26
+
27
+ export class WebSocketManager {
28
+ private ws: WebSocket | null = null
29
+ private url: string
30
+ private reconnectInterval: number
31
+ private maxReconnectAttempts: number
32
+ private reconnectAttempts = 0
33
+ private reconnectTimer: NodeJS.Timeout | null = null
34
+ private options: WebSocketManagerOptions
35
+
36
+ constructor(options: WebSocketManagerOptions = {}) {
37
+ const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
38
+ const host = window.location.host
39
+
40
+ this.url = options.url || `${protocol}//${host}/server`
41
+ this.reconnectInterval = options.reconnectInterval || 3000
42
+ this.maxReconnectAttempts = options.maxReconnectAttempts || 10
43
+ this.options = options
44
+ }
45
+
46
+ /**
47
+ * 连接 WebSocket
48
+ */
49
+ connect() {
50
+ if (this.ws?.readyState === WebSocket.OPEN) return
51
+
52
+ try {
53
+ this.ws = new WebSocket(this.url)
54
+
55
+ this.ws.onopen = () => {
56
+ this.reconnectAttempts = 0
57
+ this.options.onConnect?.()
58
+ }
59
+
60
+ this.ws.onmessage = (event) => {
61
+ try {
62
+ const message: WebSocketMessage = JSON.parse(event.data)
63
+ this.handleMessage(message)
64
+ this.options.onMessage?.(message)
65
+ } catch (error) {
66
+ console.error('[WS] Parse error:', error)
67
+ }
68
+ }
69
+
70
+ this.ws.onclose = () => {
71
+ this.options.onDisconnect?.()
72
+ this.attemptReconnect()
73
+ }
74
+
75
+ this.ws.onerror = () => {
76
+ this.options.onError?.(new Event('error'))
77
+ }
78
+ } catch (error) {
79
+ this.attemptReconnect()
80
+ }
81
+ }
82
+
83
+ /**
84
+ * 处理服务器消息
85
+ */
86
+ private handleMessage(message: WebSocketMessage) {
87
+ switch (message.type) {
88
+ case 'sync':
89
+ // 同步所有入口 - dispatch 到 Redux
90
+ if (message.data?.key === 'entries') {
91
+ const entries = Array.isArray(message.data.value)
92
+ ? message.data.value
93
+ : [message.data.value]
94
+ store.dispatch({ type: 'script/syncEntries', payload: entries })
95
+ // 使用 AsyncThunk 加载脚本
96
+ store.dispatch(loadScripts(entries))
97
+ }
98
+ break
99
+
100
+ case 'add':
101
+ // 添加新入口 - dispatch 到 Redux
102
+ if (message.data?.key === 'entries') {
103
+ store.dispatch({ type: 'script/addEntry', payload: message.data.value })
104
+ // 使用 AsyncThunk 加载脚本
105
+ store.dispatch(loadScript(message.data.value))
106
+ }
107
+ break
108
+
109
+ case 'delete':
110
+ // 删除入口 - dispatch 到 Redux
111
+ if (message.data?.key === 'entries') {
112
+ store.dispatch({ type: 'script/removeEntry', payload: message.data.value })
113
+ // 使用 AsyncThunk 卸载脚本
114
+ store.dispatch(unloadScript(message.data.value))
115
+ }
116
+ break
117
+
118
+ case 'init-data':
119
+ case 'data-update':
120
+ break
121
+
122
+ default:
123
+ console.warn('[WS] Unknown type:', message.type)
124
+ }
125
+ }
126
+
127
+ /**
128
+ * 尝试重新连接
129
+ */
130
+ private attemptReconnect() {
131
+ if (this.reconnectAttempts >= this.maxReconnectAttempts) return
132
+
133
+ this.reconnectAttempts++
134
+ this.reconnectTimer = setTimeout(() => {
135
+ this.connect()
136
+ }, this.reconnectInterval)
137
+ }
138
+
139
+ /**
140
+ * 断开连接
141
+ */
142
+ disconnect() {
143
+ if (this.reconnectTimer) {
144
+ clearTimeout(this.reconnectTimer)
145
+ this.reconnectTimer = null
146
+ }
147
+
148
+ if (this.ws) {
149
+ this.ws.close()
150
+ this.ws = null
151
+ }
152
+ }
153
+
154
+ /**
155
+ * 发送消息
156
+ */
157
+ send(message: any) {
158
+ if (this.ws?.readyState === WebSocket.OPEN) {
159
+ this.ws.send(JSON.stringify(message))
160
+ }
161
+ }
162
+
163
+ /**
164
+ * 检查是否已连接
165
+ */
166
+ isConnected() {
167
+ return this.ws?.readyState === WebSocket.OPEN
168
+ }
169
+ }
170
+
171
+ // 创建全局单例并自动连接
172
+ const globalWebSocketManager = new WebSocketManager()
173
+
174
+ // 模块加载时自动连接
175
+ if (typeof window !== 'undefined') {
176
+ globalWebSocketManager.connect()
177
+ }
178
+
179
+ /**
180
+ * 获取全局 WebSocket 管理器
181
+ */
182
+ export function getWebSocketManager(): WebSocketManager {
183
+ return globalWebSocketManager
184
+ }
185
+
186
+ /**
187
+ * 销毁全局 WebSocket 管理器
188
+ */
189
+ export function destroyWebSocketManager() {
190
+ globalWebSocketManager.disconnect()
191
+ }
192
+
193
+ export default WebSocketManager
@@ -0,0 +1,42 @@
1
+ import { useCallback } from 'react'
2
+ import { useSelector } from '../store'
3
+ import { getWebSocketManager } from './index'
4
+
5
+ /**
6
+ * WebSocket Hook
7
+ * 用于在 React 组件中访问 WebSocket 状态和方法
8
+ * WebSocket 在模块加载时已自动连接,无需手动初始化
9
+ * 状态由 Redux store 管理
10
+ */
11
+ export function useWebSocket() {
12
+ // 从 Redux 读取状态
13
+ const entries = useSelector((state: any) => state.script.entries || [])
14
+ const loadedScripts = useSelector((state: any) => state.script.loadedScripts || [])
15
+
16
+ const connect = useCallback(() => {
17
+ getWebSocketManager().connect()
18
+ }, [])
19
+
20
+ const disconnect = useCallback(() => {
21
+ getWebSocketManager().disconnect()
22
+ }, [])
23
+
24
+ const send = useCallback((message: any) => {
25
+ getWebSocketManager().send(message)
26
+ }, [])
27
+
28
+ const isConnected = useCallback(() => {
29
+ return getWebSocketManager().isConnected()
30
+ }, [])
31
+
32
+ return {
33
+ entries,
34
+ loadedScripts,
35
+ connect,
36
+ disconnect,
37
+ send,
38
+ isConnected
39
+ }
40
+ }
41
+
42
+ export default useWebSocket
@@ -1,33 +0,0 @@
1
- /* eslint-disable */
2
- // @ts-nocheck
3
- // Generated by unplugin-vue-components
4
- // Read more: https://github.com/vuejs/core/pull/3399
5
- // biome-ignore lint: disable
6
- export {}
7
-
8
- /* prettier-ignore */
9
- declare module 'vue' {
10
- export interface GlobalComponents {
11
- Badge: typeof import('primevue/badge')['default']
12
- Breadcrumb: typeof import('primevue/breadcrumb')['default']
13
- Button: typeof import('primevue/button')['default']
14
- Card: typeof import('primevue/card')['default']
15
- Dialog: typeof import('primevue/dialog')['default']
16
- Divider: typeof import('primevue/divider')['default']
17
- Dropdown: typeof import('primevue/dropdown')['default']
18
- InputText: typeof import('primevue/inputtext')['default']
19
- OverlayPanel: typeof import('primevue/overlaypanel')['default']
20
- PanelMenu: typeof import('primevue/panelmenu')['default']
21
- Password: typeof import('primevue/password')['default']
22
- ProgressBar: typeof import('primevue/progressbar')['default']
23
- RouterLink: typeof import('vue-router')['RouterLink']
24
- RouterView: typeof import('vue-router')['RouterView']
25
- Sidebar: typeof import('primevue/sidebar')['default']
26
- StyleGuide: typeof import('./src/components/StyleGuide.vue')['default']
27
- Tag: typeof import('primevue/tag')['default']
28
- }
29
- export interface GlobalDirectives {
30
- Ripple: typeof import('primevue/ripple')['default']
31
- Tooltip: typeof import('primevue/tooltip')['default']
32
- }
33
- }
package/app/src/App.vue DELETED
@@ -1,7 +0,0 @@
1
- <template>
2
- <router-view/>
3
- </template>
4
-
5
- <script setup lang="ts">
6
- </script>
7
- <style></style>
package/app/src/main.ts DELETED
@@ -1,127 +0,0 @@
1
- import { createApp } from 'vue';
2
- import { createPinia } from 'pinia';
3
- import PrimeVue from "primevue/config";
4
- import { addPage, router, useCommonStore } from '@zhin.js/client';
5
- import { updateAllData, DataService } from './services/api';
6
- import App from './App.vue';
7
- // 引入通用样式
8
- import './styles/common.css';
9
-
10
- // 全局暴露数据管理方法,供外部模块使用
11
- declare global {
12
- interface Window {
13
- ZhinDataAPI: {
14
- updateAllData: () => Promise<void>
15
- getSystemStatus: () => Promise<any>
16
- getPlugins: () => Promise<any>
17
- getAdapters: () => Promise<any>
18
- reloadPlugin: (pluginName: string) => Promise<any>
19
- sendMessage: (payload: any) => Promise<any>
20
- }
21
- ZhinStore: {
22
- getCommonStore: () => any
23
- }
24
- }
25
- }
26
-
27
- const pinia = createPinia();
28
-
29
- // 暴露全局API,供外部模块和适配器使用
30
- window.ZhinDataAPI = {
31
- updateAllData: () => updateAllData().then(() => Promise.resolve()),
32
- getSystemStatus: DataService.getSystemStatus,
33
- getPlugins: DataService.getPlugins,
34
- getAdapters: DataService.getAdapters,
35
- reloadPlugin: DataService.reloadPlugin,
36
- sendMessage: DataService.sendMessage,
37
- }
38
-
39
- // 暴露全局Store访问器
40
- window.ZhinStore = {
41
- getCommonStore: () => useCommonStore(pinia)
42
- }
43
-
44
- // 🌍 Zhin 全局API已暴露到 window 对象
45
-
46
- const wsUrl = `${window.location.protocol.replace(/^http?/, 'ws')}${window.location.host}/server`;
47
- const ws = new WebSocket(wsUrl);
48
-
49
- ws.onopen = () => {
50
- // WebSocket连接已建立
51
- };
52
-
53
- ws.onmessage = message => {
54
- const payload = JSON.parse(message.data || '{}');
55
- const commonStore = useCommonStore(pinia);
56
-
57
- switch (payload.type) {
58
- case 'sync':
59
- return commonStore.syncData(payload.data);
60
- case 'add':
61
- return commonStore.addData(payload.data);
62
- case 'delete':
63
- return commonStore.deleteData(payload.data);
64
- case 'init-data':
65
- // 初始化时获取数据
66
- // 🚀 收到初始化数据通知
67
- updateAllData();
68
- break;
69
- case 'data-update':
70
- // 收到更新通知时获取最新数据
71
- // 🔄 收到数据更新通知
72
- updateAllData();
73
- break;
74
- default:
75
- return;
76
- }
77
- };
78
-
79
- ws.onclose = () => {
80
- // WebSocket连接已关闭
81
- };
82
- const app = createApp(App);
83
- app.use(pinia).use(router).use(PrimeVue, {
84
- // 临时移除主题配置以修复类型错误
85
- });
86
- app.config.globalProperties.$ws = ws;
87
- // 注册主布局路由
88
- router.addRoute({
89
- path: '/',
90
- name: 'Zhin',
91
- component: () => import('./pages/$.vue'),
92
- });
93
-
94
- // 注册所有内置页面(addPage 会自动添加路由和菜单)
95
- addPage({
96
- parentName: 'Zhin',
97
- path: '/dashboard',
98
- name: 'Dashboard',
99
- component: () => import('./pages/dashboard.vue'),
100
- });
101
-
102
- addPage({
103
- parentName: 'Zhin',
104
- path: '/system/status',
105
- name: 'Status',
106
- component: () => import('./pages/system/status.vue'),
107
- });
108
-
109
- addPage({
110
- parentName: 'Zhin',
111
- path: '/contexts/overview',
112
- name: 'Overview',
113
- component: () => import('./pages/contexts/overview.vue'),
114
- });
115
-
116
-
117
-
118
- addPage({
119
- parentName: 'Zhin',
120
- path: '/plugins/installed',
121
- name: 'Installed',
122
- component: () => import('./pages/plugins/installed.vue'),
123
- });
124
-
125
- // 📝 所有内置页面已通过 addPage 注册
126
-
127
- app.mount('#app');