@dyyz1993/create-agent 1.9.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/package.json +25 -0
- package/src/__tests__/cli.test.ts +85 -0
- package/src/__tests__/copy.test.ts +168 -0
- package/src/__tests__/templates.test.ts +87 -0
- package/src/cli.ts +65 -0
- package/src/commands/create.ts +49 -0
- package/src/commands/list.ts +12 -0
- package/src/commands/status.ts +85 -0
- package/src/commands/workspace.ts +129 -0
- package/src/lib/copy.ts +285 -0
- package/src/lib/templates.ts +57 -0
- package/src/lib/types.ts +6 -0
- package/templates/agent/.env.example +7 -0
- package/templates/agent/LICENSE +21 -0
- package/templates/agent/README.md +87 -0
- package/templates/agent/bun.lock +1279 -0
- package/templates/agent/electrobun.config.ts +27 -0
- package/templates/agent/eslint.config.mjs +63 -0
- package/templates/agent/llms.txt +24 -0
- package/templates/agent/package-lock.json +3670 -0
- package/templates/agent/package.json +59 -0
- package/templates/agent/postcss.config.js +6 -0
- package/templates/agent/scripts/dev.ts +138 -0
- package/templates/agent/src/__tests__/server-config.test.ts +59 -0
- package/templates/agent/src/bun/index.ts +73 -0
- package/templates/agent/src/gateway/__tests__/ws-handler.test.ts +48 -0
- package/templates/agent/src/gateway/http-routes.ts +1 -0
- package/templates/agent/src/gateway/ipc-transport.ts +68 -0
- package/templates/agent/src/gateway/ws-handler.ts +86 -0
- package/templates/agent/src/mainview/App.tsx +41 -0
- package/templates/agent/src/mainview/__tests__/components/ChatPanel.test.tsx +114 -0
- package/templates/agent/src/mainview/__tests__/components/ExplorerSidebar.test.tsx +141 -0
- package/templates/agent/src/mainview/__tests__/components/MessageBubble.test.tsx +72 -0
- package/templates/agent/src/mainview/__tests__/hooks/use-input-history.test.ts +125 -0
- package/templates/agent/src/mainview/__tests__/hooks/use-rpc-init.test.ts +217 -0
- package/templates/agent/src/mainview/__tests__/hooks/use-sidebar-resize.test.ts +127 -0
- package/templates/agent/src/mainview/__tests__/i18n/i18n.test.ts +48 -0
- package/templates/agent/src/mainview/__tests__/setup.ts +37 -0
- package/templates/agent/src/mainview/__tests__/stores/use-chat-store.test.ts +83 -0
- package/templates/agent/src/mainview/__tests__/stores/use-sidebar-store.test.ts +85 -0
- package/templates/agent/src/mainview/components/activity-bar/ActivityBar.tsx +36 -0
- package/templates/agent/src/mainview/components/activity-bar/MobileTabBar.tsx +36 -0
- package/templates/agent/src/mainview/components/activity-bar/__tests__/ActivityBar.test.tsx +44 -0
- package/templates/agent/src/mainview/components/activity-bar/__tests__/MobileTabBar.test.tsx +44 -0
- package/templates/agent/src/mainview/components/bash/BashPanel.tsx +206 -0
- package/templates/agent/src/mainview/components/bash/__tests__/BashPanel.test.tsx +103 -0
- package/templates/agent/src/mainview/components/chat/ChatPanel.tsx +102 -0
- package/templates/agent/src/mainview/components/chat/MessageBubble.tsx +86 -0
- package/templates/agent/src/mainview/components/common/ErrorBoundary.tsx +58 -0
- package/templates/agent/src/mainview/components/common/LanguageSwitcher.tsx +14 -0
- package/templates/agent/src/mainview/components/common/ThemeToggle.tsx +24 -0
- package/templates/agent/src/mainview/components/common/__tests__/ErrorBoundary.test.tsx +74 -0
- package/templates/agent/src/mainview/components/common/__tests__/LanguageSwitcher.test.tsx +44 -0
- package/templates/agent/src/mainview/components/common/__tests__/ThemeToggle.test.tsx +41 -0
- package/templates/agent/src/mainview/components/debug/DebugPanel.tsx +120 -0
- package/templates/agent/src/mainview/components/debug/__tests__/DebugPanel.test.tsx +102 -0
- package/templates/agent/src/mainview/components/diff/DiffViewerPanel.tsx +106 -0
- package/templates/agent/src/mainview/components/diff/__tests__/DiffViewerPanel.test.tsx +73 -0
- package/templates/agent/src/mainview/components/explorer/ConfirmDialog.tsx +53 -0
- package/templates/agent/src/mainview/components/explorer/ContextMenu.tsx +82 -0
- package/templates/agent/src/mainview/components/explorer/ExplorerSidebar.tsx +221 -0
- package/templates/agent/src/mainview/components/explorer/InlineInput.tsx +55 -0
- package/templates/agent/src/mainview/components/explorer/TreeNodeItem.tsx +121 -0
- package/templates/agent/src/mainview/components/explorer/__tests__/ConfirmDialog.test.tsx +37 -0
- package/templates/agent/src/mainview/components/explorer/__tests__/ContextMenu.test.tsx +49 -0
- package/templates/agent/src/mainview/components/explorer/__tests__/InlineInput.test.tsx +48 -0
- package/templates/agent/src/mainview/components/explorer/__tests__/TreeNodeItem.test.tsx +146 -0
- package/templates/agent/src/mainview/components/feed/FeedPanel.tsx +239 -0
- package/templates/agent/src/mainview/components/feed/__tests__/FeedPanel.test.tsx +108 -0
- package/templates/agent/src/mainview/components/file-preview/FilePreviewOverlay.tsx +56 -0
- package/templates/agent/src/mainview/components/file-preview/VirtualizedCodeView.tsx +99 -0
- package/templates/agent/src/mainview/components/file-preview/__tests__/FilePreviewOverlay.test.tsx +96 -0
- package/templates/agent/src/mainview/components/file-preview/__tests__/VirtualizedCodeView.test.tsx +58 -0
- package/templates/agent/src/mainview/components/git/GitBranchSelector.tsx +98 -0
- package/templates/agent/src/mainview/components/git/GitCommitInput.tsx +60 -0
- package/templates/agent/src/mainview/components/git/GitPanel.tsx +528 -0
- package/templates/agent/src/mainview/components/git/__tests__/GitBranchSelector.test.tsx +76 -0
- package/templates/agent/src/mainview/components/git/__tests__/GitCommitInput.test.tsx +91 -0
- package/templates/agent/src/mainview/components/git/__tests__/GitPanel.test.tsx +174 -0
- package/templates/agent/src/mainview/components/layout/AppLayout.tsx +170 -0
- package/templates/agent/src/mainview/components/layout/__tests__/AppLayout.test.tsx +163 -0
- package/templates/agent/src/mainview/components/rules/RulesPanel.tsx +117 -0
- package/templates/agent/src/mainview/components/rules/__tests__/RulesPanel.test.tsx +91 -0
- package/templates/agent/src/mainview/components/search/SearchPanel.tsx +404 -0
- package/templates/agent/src/mainview/components/search/__tests__/SearchPanel.test.tsx +50 -0
- package/templates/agent/src/mainview/components/sidebar/PinButton.tsx +20 -0
- package/templates/agent/src/mainview/components/sidebar/__tests__/PinButton.test.tsx +48 -0
- package/templates/agent/src/mainview/components/todo/TodoPanel.tsx +141 -0
- package/templates/agent/src/mainview/components/todo/__tests__/TodoPanel.test.tsx +91 -0
- package/templates/agent/src/mainview/global.d.ts +13 -0
- package/templates/agent/src/mainview/hooks/__tests__/use-breakpoint.test.ts +147 -0
- package/templates/agent/src/mainview/hooks/use-breakpoint.ts +34 -0
- package/templates/agent/src/mainview/hooks/use-input-history.ts +84 -0
- package/templates/agent/src/mainview/hooks/use-rpc-init.ts +61 -0
- package/templates/agent/src/mainview/hooks/use-sidebar-resize.ts +40 -0
- package/templates/agent/src/mainview/index.css +61 -0
- package/templates/agent/src/mainview/index.html +12 -0
- package/templates/agent/src/mainview/lib/api-client.ts +192 -0
- package/templates/agent/src/mainview/lib/i18n/index.ts +30 -0
- package/templates/agent/src/mainview/lib/i18n/locales/en.json +157 -0
- package/templates/agent/src/mainview/lib/i18n/locales/zh.json +157 -0
- package/templates/agent/src/mainview/lib/rpc-cache.ts +94 -0
- package/templates/agent/src/mainview/main.tsx +24 -0
- package/templates/agent/src/mainview/stores/__tests__/use-app-store.test.ts +86 -0
- package/templates/agent/src/mainview/stores/__tests__/use-bash-store.test.ts +99 -0
- package/templates/agent/src/mainview/stores/__tests__/use-connection-store.test.ts +26 -0
- package/templates/agent/src/mainview/stores/__tests__/use-explorer-store.test.ts +130 -0
- package/templates/agent/src/mainview/stores/__tests__/use-feed-store.test.ts +132 -0
- package/templates/agent/src/mainview/stores/__tests__/use-git-store.test.ts +161 -0
- package/templates/agent/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
- package/templates/agent/src/mainview/stores/__tests__/use-log-store.test.ts +28 -0
- package/templates/agent/src/mainview/stores/__tests__/use-notification-store.test.ts +102 -0
- package/templates/agent/src/mainview/stores/__tests__/use-rules-store.test.ts +75 -0
- package/templates/agent/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
- package/templates/agent/src/mainview/stores/__tests__/use-todo-store.test.ts +75 -0
- package/templates/agent/src/mainview/stores/message-batcher.ts +25 -0
- package/templates/agent/src/mainview/stores/use-app-store.ts +99 -0
- package/templates/agent/src/mainview/stores/use-bash-store.ts +93 -0
- package/templates/agent/src/mainview/stores/use-chat-store.ts +37 -0
- package/templates/agent/src/mainview/stores/use-connection-store.ts +42 -0
- package/templates/agent/src/mainview/stores/use-explorer-store.ts +320 -0
- package/templates/agent/src/mainview/stores/use-feed-store.ts +129 -0
- package/templates/agent/src/mainview/stores/use-git-store.ts +291 -0
- package/templates/agent/src/mainview/stores/use-locale-store.ts +27 -0
- package/templates/agent/src/mainview/stores/use-log-store.ts +16 -0
- package/templates/agent/src/mainview/stores/use-notification-store.ts +82 -0
- package/templates/agent/src/mainview/stores/use-rules-store.ts +54 -0
- package/templates/agent/src/mainview/stores/use-sidebar-store.ts +99 -0
- package/templates/agent/src/mainview/stores/use-theme-store.ts +46 -0
- package/templates/agent/src/mainview/stores/use-todo-store.ts +54 -0
- package/templates/agent/src/mainview/types/index.ts +33 -0
- package/templates/agent/src/mainview/utils/constants.ts +1 -0
- package/templates/agent/src/mainview/utils/drop-handler.ts +150 -0
- package/templates/agent/src/mainview/utils/file-icon.tsx +24 -0
- package/templates/agent/src/mainview/utils/file-utils.ts +35 -0
- package/templates/agent/src/server-config.ts +43 -0
- package/templates/agent/src/server.ts +89 -0
- package/templates/agent/src/shared/handlers/__tests__/bash-handler.test.ts +135 -0
- package/templates/agent/src/shared/handlers/__tests__/chat-handler.test.ts +77 -0
- package/templates/agent/src/shared/handlers/__tests__/feed-handler.test.ts +108 -0
- package/templates/agent/src/shared/handlers/__tests__/file-handler.test.ts +100 -0
- package/templates/agent/src/shared/handlers/__tests__/system-handler.test.ts +55 -0
- package/templates/agent/src/shared/handlers/__tests__/timer-handler.test.ts +77 -0
- package/templates/agent/src/shared/handlers/bash.ts +89 -0
- package/templates/agent/src/shared/handlers/chat.ts +179 -0
- package/templates/agent/src/shared/handlers/feed.ts +53 -0
- package/templates/agent/src/shared/handlers/file.ts +99 -0
- package/templates/agent/src/shared/handlers/git.ts +263 -0
- package/templates/agent/src/shared/handlers/index.ts +10 -0
- package/templates/agent/src/shared/handlers/rules.ts +45 -0
- package/templates/agent/src/shared/handlers/system.ts +27 -0
- package/templates/agent/src/shared/handlers/timer.ts +34 -0
- package/templates/agent/src/shared/handlers/todo.ts +45 -0
- package/templates/agent/src/shared/lib/__tests__/bash-security.test.ts +125 -0
- package/templates/agent/src/shared/lib/__tests__/logger.test.ts +92 -0
- package/templates/agent/src/shared/lib/__tests__/path-security.test.ts +77 -0
- package/templates/agent/src/shared/lib/bash-security.ts +64 -0
- package/templates/agent/src/shared/lib/logger.ts +130 -0
- package/templates/agent/src/shared/lib/path-security.ts +38 -0
- package/templates/agent/src/shared/lib/port-registry.ts +103 -0
- package/templates/agent/src/shared/modules/bash.ts +19 -0
- package/templates/agent/src/shared/modules/chat.ts +20 -0
- package/templates/agent/src/shared/modules/feed.ts +30 -0
- package/templates/agent/src/shared/modules/file.ts +40 -0
- package/templates/agent/src/shared/modules/git.ts +81 -0
- package/templates/agent/src/shared/modules/rules.ts +25 -0
- package/templates/agent/src/shared/modules/system.ts +8 -0
- package/templates/agent/src/shared/modules/timer.ts +11 -0
- package/templates/agent/src/shared/modules/todo.ts +27 -0
- package/templates/agent/src/shared/register-all-handlers.ts +36 -0
- package/templates/agent/src/shared/rpc-schema.ts +35 -0
- package/templates/agent/tailwind.config.js +15 -0
- package/templates/agent/tsconfig.json +26 -0
- package/templates/agent/vite.config.ts +3 -0
- package/templates/agent/vitest.config.ts +3 -0
- package/templates/chat/.env.example +6 -0
- package/templates/chat/LICENSE +21 -0
- package/templates/chat/README.md +56 -0
- package/templates/chat/bun.lock +1203 -0
- package/templates/chat/electrobun.config.ts +27 -0
- package/templates/chat/eslint.config.mjs +56 -0
- package/templates/chat/llms.txt +24 -0
- package/templates/chat/package-lock.json +3670 -0
- package/templates/chat/package.json +58 -0
- package/templates/chat/postcss.config.js +6 -0
- package/templates/chat/scripts/dev.ts +138 -0
- package/templates/chat/src/__tests__/server-config.test.ts +59 -0
- package/templates/chat/src/bun/index.ts +72 -0
- package/templates/chat/src/gateway/__tests__/ws-handler.test.ts +48 -0
- package/templates/chat/src/gateway/http-routes.ts +1 -0
- package/templates/chat/src/gateway/ipc-transport.ts +68 -0
- package/templates/chat/src/gateway/ws-handler.ts +86 -0
- package/templates/chat/src/mainview/App.tsx +108 -0
- package/templates/chat/src/mainview/__tests__/components/ChatPanel.test.tsx +76 -0
- package/templates/chat/src/mainview/__tests__/components/MessageBubble.test.tsx +48 -0
- package/templates/chat/src/mainview/__tests__/i18n/i18n.test.ts +48 -0
- package/templates/chat/src/mainview/__tests__/setup-verify.test.ts +8 -0
- package/templates/chat/src/mainview/__tests__/setup.ts +37 -0
- package/templates/chat/src/mainview/__tests__/stores/use-app-store.test.ts +46 -0
- package/templates/chat/src/mainview/__tests__/stores/use-chat-store.test.ts +104 -0
- package/templates/chat/src/mainview/__tests__/stores/use-connection-store.test.ts +35 -0
- package/templates/chat/src/mainview/__tests__/stores/use-log-store.test.ts +45 -0
- package/templates/chat/src/mainview/__tests__/stores/use-notification-store.test.ts +102 -0
- package/templates/chat/src/mainview/__tests__/stores/use-sidebar-store.test.ts +89 -0
- package/templates/chat/src/mainview/components/activity-bar/ActivityBar.tsx +26 -0
- package/templates/chat/src/mainview/components/activity-bar/MobileTabBar.tsx +20 -0
- package/templates/chat/src/mainview/components/chat/ChatPanel.tsx +66 -0
- package/templates/chat/src/mainview/components/chat/MessageBubble.tsx +85 -0
- package/templates/chat/src/mainview/components/common/LanguageSwitcher.tsx +15 -0
- package/templates/chat/src/mainview/components/common/ThemeToggle.tsx +24 -0
- package/templates/chat/src/mainview/components/sidebar/PinButton.tsx +20 -0
- package/templates/chat/src/mainview/global.d.ts +13 -0
- package/templates/chat/src/mainview/hooks/use-breakpoint.ts +34 -0
- package/templates/chat/src/mainview/hooks/use-input-history.ts +84 -0
- package/templates/chat/src/mainview/index.css +61 -0
- package/templates/chat/src/mainview/index.html +12 -0
- package/templates/chat/src/mainview/lib/api-client.ts +175 -0
- package/templates/chat/src/mainview/lib/i18n/index.ts +30 -0
- package/templates/chat/src/mainview/lib/i18n/locales/en.json +157 -0
- package/templates/chat/src/mainview/lib/i18n/locales/zh.json +157 -0
- package/templates/chat/src/mainview/main.tsx +20 -0
- package/templates/chat/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
- package/templates/chat/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
- package/templates/chat/src/mainview/stores/message-batcher.ts +25 -0
- package/templates/chat/src/mainview/stores/use-app-store.ts +50 -0
- package/templates/chat/src/mainview/stores/use-chat-store.ts +37 -0
- package/templates/chat/src/mainview/stores/use-connection-store.ts +42 -0
- package/templates/chat/src/mainview/stores/use-locale-store.ts +27 -0
- package/templates/chat/src/mainview/stores/use-log-store.ts +16 -0
- package/templates/chat/src/mainview/stores/use-notification-store.ts +82 -0
- package/templates/chat/src/mainview/stores/use-sidebar-store.ts +98 -0
- package/templates/chat/src/mainview/stores/use-theme-store.ts +46 -0
- package/templates/chat/src/mainview/types/index.ts +8 -0
- package/templates/chat/src/mainview/utils/constants.ts +1 -0
- package/templates/chat/src/server-config.ts +42 -0
- package/templates/chat/src/server.ts +89 -0
- package/templates/chat/src/shared/__tests__/chat-handler.test.ts +71 -0
- package/templates/chat/src/shared/__tests__/system-handler.test.ts +70 -0
- package/templates/chat/src/shared/handlers/chat.ts +186 -0
- package/templates/chat/src/shared/handlers/index.ts +2 -0
- package/templates/chat/src/shared/handlers/system.ts +27 -0
- package/templates/chat/src/shared/lib/logger.ts +130 -0
- package/templates/chat/src/shared/lib/path-security.ts +38 -0
- package/templates/chat/src/shared/lib/port-registry.ts +103 -0
- package/templates/chat/src/shared/modules/chat.ts +20 -0
- package/templates/chat/src/shared/modules/system.ts +8 -0
- package/templates/chat/src/shared/register-all-handlers.ts +36 -0
- package/templates/chat/src/shared/rpc-schema.ts +11 -0
- package/templates/chat/tailwind.config.js +15 -0
- package/templates/chat/tsconfig.json +26 -0
- package/templates/chat/vite.config.ts +3 -0
- package/templates/chat/vitest.config.ts +3 -0
- package/templates/general/.env.example +7 -0
- package/templates/general/LICENSE +21 -0
- package/templates/general/README.md +113 -0
- package/templates/general/bun.lock +1266 -0
- package/templates/general/electrobun.config.ts +27 -0
- package/templates/general/eslint.config.mjs +56 -0
- package/templates/general/llms.txt +24 -0
- package/templates/general/package-lock.json +3670 -0
- package/templates/general/package.json +59 -0
- package/templates/general/postcss.config.js +6 -0
- package/templates/general/scripts/dev.ts +138 -0
- package/templates/general/src/__tests__/server-config.test.ts +59 -0
- package/templates/general/src/bun/index.ts +72 -0
- package/templates/general/src/gateway/http-routes.ts +1 -0
- package/templates/general/src/gateway/ipc-transport.ts +68 -0
- package/templates/general/src/gateway/ws-handler.ts +86 -0
- package/templates/general/src/mainview/App.tsx +40 -0
- package/templates/general/src/mainview/__tests__/components/ChatPanel.test.tsx +82 -0
- package/templates/general/src/mainview/__tests__/components/FeedPanel.test.tsx +109 -0
- package/templates/general/src/mainview/__tests__/components/GitPanel.test.tsx +151 -0
- package/templates/general/src/mainview/__tests__/i18n/i18n.test.ts +48 -0
- package/templates/general/src/mainview/__tests__/setup-verify.test.ts +8 -0
- package/templates/general/src/mainview/__tests__/setup.ts +37 -0
- package/templates/general/src/mainview/components/activity-bar/ActivityBar.tsx +40 -0
- package/templates/general/src/mainview/components/activity-bar/MobileTabBar.tsx +29 -0
- package/templates/general/src/mainview/components/chat/ChatPanel.tsx +66 -0
- package/templates/general/src/mainview/components/chat/MessageBubble.tsx +85 -0
- package/templates/general/src/mainview/components/common/LanguageSwitcher.tsx +15 -0
- package/templates/general/src/mainview/components/common/ThemeToggle.tsx +24 -0
- package/templates/general/src/mainview/components/debug/DebugPanel.tsx +176 -0
- package/templates/general/src/mainview/components/diff/DiffViewerPanel.tsx +108 -0
- package/templates/general/src/mainview/components/explorer/ConfirmDialog.tsx +52 -0
- package/templates/general/src/mainview/components/explorer/ContextMenu.tsx +82 -0
- package/templates/general/src/mainview/components/explorer/ExplorerSidebar.tsx +225 -0
- package/templates/general/src/mainview/components/explorer/InlineInput.tsx +55 -0
- package/templates/general/src/mainview/components/explorer/TreeNodeItem.tsx +121 -0
- package/templates/general/src/mainview/components/feed/FeedPanel.tsx +248 -0
- package/templates/general/src/mainview/components/file-preview/FilePreviewOverlay.tsx +55 -0
- package/templates/general/src/mainview/components/file-preview/VirtualizedCodeView.tsx +99 -0
- package/templates/general/src/mainview/components/git/GitBranchSelector.tsx +93 -0
- package/templates/general/src/mainview/components/git/GitCommitInput.tsx +60 -0
- package/templates/general/src/mainview/components/git/GitPanel.tsx +533 -0
- package/templates/general/src/mainview/components/layout/AppLayout.tsx +146 -0
- package/templates/general/src/mainview/components/search/SearchPanel.tsx +405 -0
- package/templates/general/src/mainview/components/sidebar/PinButton.tsx +20 -0
- package/templates/general/src/mainview/global.d.ts +13 -0
- package/templates/general/src/mainview/hooks/use-breakpoint.ts +34 -0
- package/templates/general/src/mainview/hooks/use-input-history.ts +84 -0
- package/templates/general/src/mainview/hooks/use-rpc-init.ts +61 -0
- package/templates/general/src/mainview/hooks/use-sidebar-resize.ts +40 -0
- package/templates/general/src/mainview/index.css +61 -0
- package/templates/general/src/mainview/index.html +12 -0
- package/templates/general/src/mainview/lib/api-client.ts +175 -0
- package/templates/general/src/mainview/lib/i18n/index.ts +30 -0
- package/templates/general/src/mainview/lib/i18n/locales/en.json +157 -0
- package/templates/general/src/mainview/lib/i18n/locales/zh.json +157 -0
- package/templates/general/src/mainview/main.tsx +20 -0
- package/templates/general/src/mainview/stores/__tests__/use-chat-store.test.ts +104 -0
- package/templates/general/src/mainview/stores/__tests__/use-connection-store.test.ts +35 -0
- package/templates/general/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
- package/templates/general/src/mainview/stores/__tests__/use-log-store.test.ts +45 -0
- package/templates/general/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
- package/templates/general/src/mainview/stores/message-batcher.ts +25 -0
- package/templates/general/src/mainview/stores/use-app-store.ts +104 -0
- package/templates/general/src/mainview/stores/use-chat-store.ts +37 -0
- package/templates/general/src/mainview/stores/use-connection-store.ts +42 -0
- package/templates/general/src/mainview/stores/use-explorer-store.ts +315 -0
- package/templates/general/src/mainview/stores/use-feed-store.ts +129 -0
- package/templates/general/src/mainview/stores/use-git-store.ts +284 -0
- package/templates/general/src/mainview/stores/use-locale-store.ts +27 -0
- package/templates/general/src/mainview/stores/use-log-store.ts +16 -0
- package/templates/general/src/mainview/stores/use-notification-store.ts +82 -0
- package/templates/general/src/mainview/stores/use-sidebar-store.ts +99 -0
- package/templates/general/src/mainview/stores/use-theme-store.ts +46 -0
- package/templates/general/src/mainview/types/index.ts +33 -0
- package/templates/general/src/mainview/utils/constants.ts +1 -0
- package/templates/general/src/mainview/utils/drop-handler.ts +150 -0
- package/templates/general/src/mainview/utils/file-icon.tsx +24 -0
- package/templates/general/src/mainview/utils/file-utils.ts +35 -0
- package/templates/general/src/server-config.ts +42 -0
- package/templates/general/src/server.ts +89 -0
- package/templates/general/src/shared/handlers/__tests__/chat.test.ts +183 -0
- package/templates/general/src/shared/handlers/__tests__/system.test.ts +94 -0
- package/templates/general/src/shared/handlers/__tests__/timer.test.ts +113 -0
- package/templates/general/src/shared/handlers/chat.ts +179 -0
- package/templates/general/src/shared/handlers/feed.ts +53 -0
- package/templates/general/src/shared/handlers/file.ts +99 -0
- package/templates/general/src/shared/handlers/git.ts +263 -0
- package/templates/general/src/shared/handlers/index.ts +7 -0
- package/templates/general/src/shared/handlers/system.ts +27 -0
- package/templates/general/src/shared/handlers/timer.ts +34 -0
- package/templates/general/src/shared/lib/logger.ts +130 -0
- package/templates/general/src/shared/lib/path-security.ts +38 -0
- package/templates/general/src/shared/lib/port-registry.ts +103 -0
- package/templates/general/src/shared/modules/chat.ts +20 -0
- package/templates/general/src/shared/modules/feed.ts +30 -0
- package/templates/general/src/shared/modules/file.ts +40 -0
- package/templates/general/src/shared/modules/git.ts +81 -0
- package/templates/general/src/shared/modules/system.ts +8 -0
- package/templates/general/src/shared/modules/timer.ts +11 -0
- package/templates/general/src/shared/register-all-handlers.ts +36 -0
- package/templates/general/src/shared/rpc-schema.ts +31 -0
- package/templates/general/tailwind.config.js +15 -0
- package/templates/general/tsconfig.json +26 -0
- package/templates/general/vite.config.ts +3 -0
- package/templates/general/vitest.config.ts +3 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-agent-app",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Coding agent template with Bash, Rules, and Todo management",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "vite build && electrobun dev",
|
|
7
|
+
"dev": "electrobun dev --watch",
|
|
8
|
+
"dev:web": "bun scripts/dev.ts",
|
|
9
|
+
"dev:server": "bun src/server.ts",
|
|
10
|
+
"build": "vite build",
|
|
11
|
+
"build:canary": "vite build && electrobun build --env=canary",
|
|
12
|
+
"lint": "eslint .",
|
|
13
|
+
"lint:fix": "eslint . --fix",
|
|
14
|
+
"test": "vitest run",
|
|
15
|
+
"test:watch": "vitest",
|
|
16
|
+
"test:coverage": "vitest run --coverage",
|
|
17
|
+
"prepare": "husky"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@dyyz1993/rpc-core": "^1.2.0",
|
|
21
|
+
"@tanstack/react-virtual": "^3.13.24",
|
|
22
|
+
"electrobun": "1.13.1",
|
|
23
|
+
"lucide-react": "^1.9.0",
|
|
24
|
+
"prism-react-renderer": "^2.4.1",
|
|
25
|
+
"react": "^18.3.1",
|
|
26
|
+
"react-diff-viewer-continued": "^4.2.2",
|
|
27
|
+
"i18next": "^24.2.2",
|
|
28
|
+
"i18next-browser-languagedetector": "^8.0.4",
|
|
29
|
+
"react-dom": "^18.3.1",
|
|
30
|
+
"react-i18next": "^15.4.1",
|
|
31
|
+
"react-markdown": "^10.1.0",
|
|
32
|
+
"remark-gfm": "^4.0.1",
|
|
33
|
+
"zustand": "^5.0.12"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@dyyz1993/eslint-plugin-rpc": "workspace:*",
|
|
37
|
+
"@eslint/js": "^9.0.0",
|
|
38
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
39
|
+
"@testing-library/react": "^16.3.2",
|
|
40
|
+
"@testing-library/user-event": "^14.6.1",
|
|
41
|
+
"@types/bun": "latest",
|
|
42
|
+
"@types/react": "^18.3.12",
|
|
43
|
+
"@types/react-dom": "^18.3.1",
|
|
44
|
+
"@types/ws": "^8.18.1",
|
|
45
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
46
|
+
"autoprefixer": "^10.4.20",
|
|
47
|
+
"concurrently": "^9.1.0",
|
|
48
|
+
"eslint": "^9.0.0",
|
|
49
|
+
"happy-dom": "^20.9.0",
|
|
50
|
+
"husky": "^9.1.0",
|
|
51
|
+
"jsdom": "^29.1.1",
|
|
52
|
+
"postcss": "^8.4.49",
|
|
53
|
+
"tailwindcss": "^3.4.16",
|
|
54
|
+
"typescript": "^5.7.2",
|
|
55
|
+
"typescript-eslint": "^8.0.0",
|
|
56
|
+
"vite": "^6.0.3",
|
|
57
|
+
"vitest": "^4.1.5"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev orchestration script — single command to start backend + Vite.
|
|
3
|
+
*
|
|
4
|
+
* Usage: bun run dev:web
|
|
5
|
+
*
|
|
6
|
+
* 1. Starts backend server (dynamic port, writes to .server-port)
|
|
7
|
+
* 2. Waits for .server-port to appear
|
|
8
|
+
* 3. Starts Vite dev server (reads backend port for proxy)
|
|
9
|
+
* 4. On Ctrl+C / SIGTERM: kills both processes, cleans up .server-port
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { spawn } from "child_process";
|
|
13
|
+
import { existsSync, readFileSync, unlinkSync } from "fs";
|
|
14
|
+
import { resolve } from "path";
|
|
15
|
+
|
|
16
|
+
const ROOT = resolve(import.meta.dir, "..");
|
|
17
|
+
const PORT_FILE = resolve(ROOT, ".server-port");
|
|
18
|
+
const MAX_WAIT_MS = 10000;
|
|
19
|
+
const POLL_INTERVAL_MS = 100;
|
|
20
|
+
|
|
21
|
+
let backend: ReturnType<typeof spawn> | null = null;
|
|
22
|
+
let vite: ReturnType<typeof spawn> | null = null;
|
|
23
|
+
let shuttingDown = false;
|
|
24
|
+
|
|
25
|
+
function cleanup() {
|
|
26
|
+
if (shuttingDown) return;
|
|
27
|
+
shuttingDown = true;
|
|
28
|
+
|
|
29
|
+
if (vite && !vite.killed) {
|
|
30
|
+
vite.kill("SIGTERM");
|
|
31
|
+
}
|
|
32
|
+
if (backend && !backend.killed) {
|
|
33
|
+
backend.kill("SIGTERM");
|
|
34
|
+
}
|
|
35
|
+
try { if (existsSync(PORT_FILE)) unlinkSync(PORT_FILE); } catch { /* ignore cleanup errors */ }
|
|
36
|
+
|
|
37
|
+
setTimeout(() => {
|
|
38
|
+
if (backend && !backend.killed) backend.kill("SIGKILL");
|
|
39
|
+
if (vite && !vite.killed) vite.kill("SIGKILL");
|
|
40
|
+
process.exit(0);
|
|
41
|
+
}, 3000);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
process.on("SIGINT", cleanup);
|
|
45
|
+
process.on("SIGTERM", cleanup);
|
|
46
|
+
process.on("exit", () => {
|
|
47
|
+
try { if (existsSync(PORT_FILE)) unlinkSync(PORT_FILE); } catch { /* ignore */ }
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
function log(tag: string, msg: string) {
|
|
51
|
+
console.log(`[${tag}] ${msg}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function waitForPortFile(): Promise<number> {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const start = Date.now();
|
|
57
|
+
const check = () => {
|
|
58
|
+
if (existsSync(PORT_FILE)) {
|
|
59
|
+
try {
|
|
60
|
+
const port = parseInt(readFileSync(PORT_FILE, "utf-8").trim());
|
|
61
|
+
if (port > 0) { resolve(port); return; }
|
|
62
|
+
} catch { /* ignore parse errors */ }
|
|
63
|
+
}
|
|
64
|
+
if (Date.now() - start > MAX_WAIT_MS) {
|
|
65
|
+
reject(new Error("Timed out waiting for backend to start"));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
setTimeout(check, POLL_INTERVAL_MS);
|
|
69
|
+
};
|
|
70
|
+
check();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function main() {
|
|
75
|
+
log("dev", "Starting backend server...");
|
|
76
|
+
|
|
77
|
+
backend = spawn("bun", ["src/server.ts"], {
|
|
78
|
+
cwd: ROOT,
|
|
79
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
backend.stdout?.on("data", (data: Buffer) => {
|
|
83
|
+
data.toString().split("\n").filter(Boolean).forEach((line) => {
|
|
84
|
+
log("backend", line);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
backend.stderr?.on("data", (data: Buffer) => {
|
|
89
|
+
data.toString().split("\n").filter(Boolean).forEach((line) => {
|
|
90
|
+
log("backend", line);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
backend.on("exit", (code) => {
|
|
95
|
+
if (!shuttingDown) {
|
|
96
|
+
log("backend", `exited with code ${code}`);
|
|
97
|
+
cleanup();
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const backendPort = await waitForPortFile();
|
|
102
|
+
log("dev", `Backend ready on port ${backendPort}`);
|
|
103
|
+
|
|
104
|
+
log("dev", "Starting Vite dev server...");
|
|
105
|
+
vite = spawn("vite", [], {
|
|
106
|
+
cwd: ROOT,
|
|
107
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
108
|
+
env: { ...process.env },
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
vite.stdout?.on("data", (data: Buffer) => {
|
|
112
|
+
data.toString().split("\n").filter(Boolean).forEach((line) => {
|
|
113
|
+
log("vite", line);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
vite.stderr?.on("data", (data: Buffer) => {
|
|
118
|
+
data.toString().split("\n").filter(Boolean).forEach((line) => {
|
|
119
|
+
log("vite", line);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
vite.on("exit", (code) => {
|
|
124
|
+
if (!shuttingDown) {
|
|
125
|
+
log("vite", `exited with code ${code}`);
|
|
126
|
+
cleanup();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
log("dev", `Proxy: Vite → http://localhost:${backendPort}`);
|
|
131
|
+
log("dev", "Press Ctrl+C to stop both servers");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
main().catch((err) => {
|
|
135
|
+
console.error("Dev startup failed:", err.message);
|
|
136
|
+
cleanup();
|
|
137
|
+
process.exit(1);
|
|
138
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { parseEnvInt } from "../server-config";
|
|
3
|
+
|
|
4
|
+
describe("parseEnvInt", () => {
|
|
5
|
+
it("should return default when value is undefined", () => {
|
|
6
|
+
expect(parseEnvInt("TEST_KEY", undefined, 3100, 1024, 65535)).toBe(3100);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("should return default when value is empty string", () => {
|
|
10
|
+
expect(parseEnvInt("TEST_KEY", "", 3100, 1024, 65535)).toBe(3100);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("should parse valid number", () => {
|
|
14
|
+
expect(parseEnvInt("TEST_KEY", "8080", 3100, 1024, 65535)).toBe(8080);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return default for NaN input", () => {
|
|
18
|
+
expect(parseEnvInt("TEST_KEY", "abc", 3100, 1024, 65535)).toBe(3100);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should return default for out-of-range value (too low)", () => {
|
|
22
|
+
expect(parseEnvInt("TEST_KEY", "80", 3100, 1024, 65535)).toBe(3100);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should return default for out-of-range value (too high)", () => {
|
|
26
|
+
expect(parseEnvInt("TEST_KEY", "99999", 3100, 1024, 65535)).toBe(3100);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should accept boundary min value", () => {
|
|
30
|
+
expect(parseEnvInt("TEST_KEY", "1024", 3100, 1024, 65535)).toBe(1024);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should accept boundary max value", () => {
|
|
34
|
+
expect(parseEnvInt("TEST_KEY", "65535", 3100, 1024, 65535)).toBe(65535);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("server-config exports", () => {
|
|
39
|
+
it("should export config with all required fields", async () => {
|
|
40
|
+
const { config } = await import("../server-config");
|
|
41
|
+
expect(config).toHaveProperty("port");
|
|
42
|
+
expect(config).toHaveProperty("authToken");
|
|
43
|
+
expect(config).toHaveProperty("maxUploadSize");
|
|
44
|
+
expect(config).toHaveProperty("logDir");
|
|
45
|
+
expect(config).toHaveProperty("corsOrigin");
|
|
46
|
+
expect(typeof config.port).toBe("number");
|
|
47
|
+
expect(typeof config.authToken).toBe("string");
|
|
48
|
+
expect(typeof config.maxUploadSize).toBe("number");
|
|
49
|
+
expect(typeof config.corsOrigin).toBe("string");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should have sensible defaults", async () => {
|
|
53
|
+
const { config } = await import("../server-config");
|
|
54
|
+
expect(config.port).toBeGreaterThanOrEqual(1024);
|
|
55
|
+
expect(config.port).toBeLessThanOrEqual(65535);
|
|
56
|
+
expect(config.maxUploadSize).toBeGreaterThan(0);
|
|
57
|
+
expect(config.corsOrigin).toBeTruthy();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { BrowserWindow, BrowserView, Updater, ApplicationMenu } from "electrobun/bun";
|
|
2
|
+
import { RPCServer } from "@dyyz1993/rpc-core";
|
|
3
|
+
import { ElectrobunTransport } from "../gateway/ipc-transport";
|
|
4
|
+
import { registerAllHandlers } from "../shared/register-all-handlers";
|
|
5
|
+
import { createLogger, configureLogDir } from "../shared/lib/logger";
|
|
6
|
+
import { config } from "../server-config";
|
|
7
|
+
|
|
8
|
+
configureLogDir("logs");
|
|
9
|
+
const log = createLogger("server");
|
|
10
|
+
|
|
11
|
+
async function getMainViewUrl(): Promise<string> {
|
|
12
|
+
const channel = await Updater.localInfo.channel();
|
|
13
|
+
const DEV_SERVER_URL = "http://localhost:5173";
|
|
14
|
+
if (channel === "dev") {
|
|
15
|
+
try {
|
|
16
|
+
await fetch(DEV_SERVER_URL, { method: "HEAD" });
|
|
17
|
+
log.info(`HMR enabled: Using Vite dev server at ${DEV_SERVER_URL}`);
|
|
18
|
+
return DEV_SERVER_URL;
|
|
19
|
+
} catch {
|
|
20
|
+
log.info("Vite dev server not running.");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return "views://mainview/index.html";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const url = await getMainViewUrl();
|
|
27
|
+
|
|
28
|
+
const transport = new ElectrobunTransport();
|
|
29
|
+
const server = new RPCServer(transport);
|
|
30
|
+
|
|
31
|
+
// --- 注册 RPC handlers(自动导入 handlers barrel) ---
|
|
32
|
+
registerAllHandlers(server, { platform: "desktop", enableBash: config.enableBash });
|
|
33
|
+
|
|
34
|
+
// --- 创建窗口 ---
|
|
35
|
+
|
|
36
|
+
const mainWindow = new BrowserWindow({
|
|
37
|
+
title: "Pi Agent",
|
|
38
|
+
url,
|
|
39
|
+
frame: {
|
|
40
|
+
width: 1200,
|
|
41
|
+
height: 800,
|
|
42
|
+
x: 200,
|
|
43
|
+
y: 200,
|
|
44
|
+
},
|
|
45
|
+
rpc: BrowserView.defineRPC({
|
|
46
|
+
maxRequestTime: 60000,
|
|
47
|
+
handlers: {
|
|
48
|
+
requests: {},
|
|
49
|
+
messages: {
|
|
50
|
+
// @ts-expect-error Electrobun's messages schema doesn't support custom keys at compile time
|
|
51
|
+
"rpc-message": (data: unknown) => {
|
|
52
|
+
try {
|
|
53
|
+
const message = typeof data === "string" ? JSON.parse(data) : data;
|
|
54
|
+
transport.handleMessage(message);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
log.error("Failed to parse RPC message", { error });
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
transport.setBrowserView(mainWindow.webview);
|
|
65
|
+
|
|
66
|
+
log.info("Pi Agent desktop app started!");
|
|
67
|
+
|
|
68
|
+
ApplicationMenu.setApplicationMenu([
|
|
69
|
+
{ label: "Pi Agent", submenu: [{ role: "about" }, { type: "separator" }, { role: "hide" }, { role: "hideOthers" }, { role: "showAll" }, { type: "separator" }, { role: "quit" }] },
|
|
70
|
+
{ label: "Edit", submenu: [{ role: "undo" }, { role: "redo" }, { type: "separator" }, { role: "cut" }, { role: "copy" }, { role: "paste" }, { role: "selectAll" }] },
|
|
71
|
+
{ label: "View", submenu: [{ role: "toggleFullScreen" }] },
|
|
72
|
+
{ label: "Window", submenu: [{ role: "minimize" }, { role: "zoom" }] },
|
|
73
|
+
]);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("../../server-config", () => ({
|
|
4
|
+
config: {
|
|
5
|
+
authToken: "test-token",
|
|
6
|
+
port: 3100,
|
|
7
|
+
corsOrigin: "http://localhost:5173",
|
|
8
|
+
},
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
vi.mock("../../shared/lib/logger", () => ({
|
|
12
|
+
createLogger: () => ({
|
|
13
|
+
info: vi.fn(),
|
|
14
|
+
error: vi.fn(),
|
|
15
|
+
warn: vi.fn(),
|
|
16
|
+
debug: vi.fn(),
|
|
17
|
+
}),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
describe("WebSocket Handler", () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.resetModules();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should reject connections without token", async () => {
|
|
26
|
+
const { config } = await import("../../server-config");
|
|
27
|
+
|
|
28
|
+
const token = null;
|
|
29
|
+
const isValid = token === config.authToken;
|
|
30
|
+
expect(isValid).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should reject wrong token", async () => {
|
|
34
|
+
const { config } = await import("../../server-config");
|
|
35
|
+
|
|
36
|
+
const token = "wrong-token";
|
|
37
|
+
const isValid = token === config.authToken;
|
|
38
|
+
expect(isValid).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should accept correct token", async () => {
|
|
42
|
+
const { config } = await import("../../server-config");
|
|
43
|
+
|
|
44
|
+
const token = "test-token";
|
|
45
|
+
const isValid = token === config.authToken;
|
|
46
|
+
expect(isValid).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createHttpHandler, type HttpRouteDeps } from "@shared/http-routes";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Transport, MessageHandler, ErrorHandler } from "@dyyz1993/rpc-core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Transport that bridges between Bun and Webview via:
|
|
5
|
+
* - Bun → Browser: executeJavascript calls window.__piAgentIPC()
|
|
6
|
+
* - Browser → Bun: __electrobunBunBridge.postMessage with Electrobun message format
|
|
7
|
+
*/
|
|
8
|
+
export class ElectrobunTransport implements Transport {
|
|
9
|
+
private messageHandlers: Set<MessageHandler> = new Set();
|
|
10
|
+
private errorHandlers: Set<ErrorHandler> = new Set();
|
|
11
|
+
private browserView: { executeJavascript: (js: string) => void } | null = null;
|
|
12
|
+
private closed = false;
|
|
13
|
+
|
|
14
|
+
setBrowserView(view: { executeJavascript: (js: string) => void }): void {
|
|
15
|
+
this.browserView = view;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async send(message: unknown): Promise<void> {
|
|
19
|
+
if (this.closed) {
|
|
20
|
+
throw new Error("Transport is closed");
|
|
21
|
+
}
|
|
22
|
+
if (!this.browserView) {
|
|
23
|
+
throw new Error("BrowserView not set");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Pass JSON directly as a JS expression — JSON is valid JavaScript
|
|
27
|
+
const msgJson = JSON.stringify(message);
|
|
28
|
+
this.browserView.executeJavascript(
|
|
29
|
+
`if(typeof window.__piAgentIPC==="function"){window.__piAgentIPC(${msgJson})}`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
onMessage(handler: MessageHandler): () => void {
|
|
34
|
+
this.messageHandlers.add(handler);
|
|
35
|
+
return () => {
|
|
36
|
+
this.messageHandlers.delete(handler);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
onError(handler: ErrorHandler): () => void {
|
|
41
|
+
this.errorHandlers.add(handler);
|
|
42
|
+
return () => {
|
|
43
|
+
this.errorHandlers.delete(handler);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
onDisconnect(): () => void {
|
|
48
|
+
return () => {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
isConnected(): boolean {
|
|
52
|
+
return this.browserView !== null && !this.closed;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
close(): void {
|
|
56
|
+
this.closed = true;
|
|
57
|
+
this.messageHandlers.clear();
|
|
58
|
+
this.errorHandlers.clear();
|
|
59
|
+
this.browserView = null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
handleMessage(message: unknown): void {
|
|
63
|
+
if (this.closed) return;
|
|
64
|
+
for (const handler of this.messageHandlers) {
|
|
65
|
+
handler(message);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket handler for the web gateway.
|
|
3
|
+
* Handles WS connections, auth, and bridges to RPCServer.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Server } from "http";
|
|
7
|
+
import { WebSocketServer, WebSocket } from "ws";
|
|
8
|
+
import { timingSafeEqual } from "crypto";
|
|
9
|
+
import { RPCServer, type Transport } from "@dyyz1993/rpc-core";
|
|
10
|
+
import { registerAllHandlers } from "../shared/register-all-handlers";
|
|
11
|
+
import { createLogger } from "../shared/lib/logger";
|
|
12
|
+
|
|
13
|
+
const log = createLogger("gateway");
|
|
14
|
+
|
|
15
|
+
function safeEqual(a: string, b: string): boolean {
|
|
16
|
+
const bufA = Buffer.from(a);
|
|
17
|
+
const bufB = Buffer.from(b);
|
|
18
|
+
if (bufA.length !== bufB.length) return false;
|
|
19
|
+
return timingSafeEqual(bufA, bufB);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface WsHandlerDeps {
|
|
23
|
+
config: { readonly port: number; readonly authToken: string; readonly maxUploadSize: number };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createWsHandler(httpServer: Server, deps: WsHandlerDeps): WebSocketServer {
|
|
27
|
+
const { config: cfg } = deps;
|
|
28
|
+
const wss = new WebSocketServer({ server: httpServer, path: "/ws" });
|
|
29
|
+
|
|
30
|
+
wss.on("connection", (ws: WebSocket, req) => {
|
|
31
|
+
// Token 验证
|
|
32
|
+
const url = req.url ? new URL(req.url, "http://localhost") : null;
|
|
33
|
+
const token = url?.searchParams.get("token");
|
|
34
|
+
if (!safeEqual(token || "", cfg.authToken)) {
|
|
35
|
+
log.warn("Connection rejected: invalid token");
|
|
36
|
+
ws.close(4001, "Unauthorized");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
log.info("Client connected", { total: wss.clients.size });
|
|
41
|
+
|
|
42
|
+
const wsTransport = {
|
|
43
|
+
send: async (message: unknown): Promise<void> => {
|
|
44
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
45
|
+
ws.send(JSON.stringify(message));
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
onMessage: (handler: (message: unknown) => void): (() => void) => {
|
|
49
|
+
const listener = (data: Buffer) => {
|
|
50
|
+
try {
|
|
51
|
+
const msg = JSON.parse(data.toString());
|
|
52
|
+
handler(msg);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
log.error("Failed to parse message", { error: err instanceof Error ? err.message : String(err) });
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
ws.on("message", listener);
|
|
58
|
+
return () => ws.off("message", listener);
|
|
59
|
+
},
|
|
60
|
+
onError: (): (() => void) => {
|
|
61
|
+
return () => {};
|
|
62
|
+
},
|
|
63
|
+
onDisconnect: (): (() => void) => {
|
|
64
|
+
return () => {};
|
|
65
|
+
},
|
|
66
|
+
isConnected: (): boolean => ws.readyState === WebSocket.OPEN,
|
|
67
|
+
close: (): void => {},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const rpcServer = new RPCServer(wsTransport as Transport);
|
|
71
|
+
|
|
72
|
+
// 自动导入并注册所有 handlers
|
|
73
|
+
registerAllHandlers(rpcServer, { platform: "web" });
|
|
74
|
+
|
|
75
|
+
ws.on("close", () => {
|
|
76
|
+
log.info("Client disconnected", { total: wss.clients.size });
|
|
77
|
+
rpcServer.close();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
ws.on("error", (err: Error) => {
|
|
81
|
+
log.error("Client error", { error: err.message });
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return wss;
|
|
86
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { useConnectionStore } from "./stores/use-connection-store";
|
|
3
|
+
import { useBreakpointSync } from "./hooks/use-breakpoint";
|
|
4
|
+
import { useRpcInit } from "./hooks/use-rpc-init";
|
|
5
|
+
import { useSidebarResize } from "./hooks/use-sidebar-resize";
|
|
6
|
+
import { AppLayout, type CenterTab } from "./components/layout/AppLayout";
|
|
7
|
+
import { ErrorBoundary } from "./components/common/ErrorBoundary";
|
|
8
|
+
|
|
9
|
+
function App() {
|
|
10
|
+
const ready = useConnectionStore((s) => s.ready);
|
|
11
|
+
const [centerTab, setCenterTab] = useState<CenterTab>("chat");
|
|
12
|
+
|
|
13
|
+
useBreakpointSync();
|
|
14
|
+
useRpcInit();
|
|
15
|
+
|
|
16
|
+
const { sidebarWidth, handleResizeStart } = useSidebarResize();
|
|
17
|
+
|
|
18
|
+
if (!ready) {
|
|
19
|
+
return (
|
|
20
|
+
<div className="h-screen bg-gray-900 flex items-center justify-center">
|
|
21
|
+
<div className="text-center">
|
|
22
|
+
<div className="inline-block w-8 h-8 border-2 border-indigo-400 border-t-transparent rounded-full animate-spin mb-4" />
|
|
23
|
+
<div className="text-gray-400 text-sm">Connecting to RPC server...</div>
|
|
24
|
+
</div>
|
|
25
|
+
</div>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<ErrorBoundary>
|
|
31
|
+
<AppLayout
|
|
32
|
+
centerTab={centerTab}
|
|
33
|
+
setCenterTab={setCenterTab}
|
|
34
|
+
sidebarWidth={sidebarWidth}
|
|
35
|
+
handleResizeStart={handleResizeStart}
|
|
36
|
+
/>
|
|
37
|
+
</ErrorBoundary>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default App;
|