@dyyz1993/create-agent 2.1.0 → 2.1.2
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 +5 -4
- package/src/__tests__/cli.test.ts +2 -0
- package/src/__tests__/copy.test.ts +110 -122
- package/src/__tests__/create-args.test.ts +113 -0
- package/src/__tests__/templates.test.ts +29 -3
- package/src/__tests__/update.test.ts +129 -0
- package/src/cli.ts +30 -32
- package/src/commands/create.ts +83 -24
- package/src/commands/update.ts +174 -0
- package/src/commands/workspace.ts +26 -6
- package/src/lib/copy.ts +233 -93
- package/src/lib/templates.ts +12 -0
- package/templates/agent/.husky/commit-msg +4 -0
- package/templates/agent/.husky/pre-commit +11 -0
- package/templates/agent/.husky/pre-push +6 -0
- package/templates/agent/.prettierignore +6 -0
- package/templates/agent/.prettierrc +9 -0
- package/templates/agent/CHANGELOG.md +15 -0
- package/templates/agent/commitlint.config.js +8 -0
- package/templates/agent/package.json +19 -4
- package/templates/agent/src/gateway/__tests__/ws-handler-token.test.ts +53 -39
- package/templates/agent/src/mainview/App.tsx +14 -12
- package/templates/agent/src/mainview/__tests__/hooks/use-rpc-init.test.ts +194 -190
- package/templates/agent/src/mainview/components/bash/BashPanel.tsx +210 -189
- package/templates/agent/src/mainview/components/chat/ChatPanel.tsx +45 -20
- package/templates/agent/src/mainview/components/common/ThemeToggle.tsx +41 -21
- package/templates/agent/src/mainview/components/debug/DebugPanel.tsx +181 -114
- package/templates/agent/src/mainview/components/debug/__tests__/DebugPanel.test.tsx +80 -80
- package/templates/agent/src/mainview/components/diff/__tests__/DiffViewerPanel.test.tsx +61 -58
- package/templates/agent/src/mainview/components/feed/FeedPanel.tsx +43 -41
- package/templates/agent/src/mainview/components/file-preview/FilePreviewOverlay.tsx +69 -50
- package/templates/agent/src/mainview/components/file-preview/VirtualizedCodeView.tsx +31 -18
- package/templates/agent/src/mainview/components/git/GitPanel.tsx +731 -490
- package/templates/agent/src/mainview/components/git/__tests__/GitPanel.test.tsx +157 -139
- package/templates/agent/src/mainview/components/layout/AppLayout.tsx +210 -161
- package/templates/agent/src/mainview/components/layout/__tests__/AppLayout.test.tsx +130 -122
- package/templates/agent/src/mainview/components/rules/RulesPanel.tsx +119 -109
- package/templates/agent/src/mainview/components/search/SearchPanel.tsx +123 -117
- package/templates/agent/src/mainview/components/search/__tests__/SearchPanel.test.tsx +64 -44
- package/templates/agent/src/mainview/components/sidebar/__tests__/PinButton.test.tsx +38 -38
- package/templates/agent/src/mainview/components/todo/TodoPanel.tsx +40 -38
- package/templates/agent/src/mainview/components/todo/__tests__/TodoPanel.test.tsx +72 -72
- package/templates/agent/src/mainview/lib/i18n/locales/en.json +175 -155
- package/templates/agent/src/mainview/lib/i18n/locales/zh.json +173 -155
- package/templates/agent/src/mainview/stores/use-app-store.ts +96 -86
- package/templates/agent/src/mainview/stores/use-explorer-store.ts +292 -275
- package/templates/agent/src/mainview/types/index.ts +23 -22
- package/templates/agent/src/mainview/utils/file-icon.tsx +17 -21
- package/templates/agent/src/shared/handlers/chat.ts +53 -53
- package/templates/agent/src/shared/handlers/debug.ts +13 -3
- package/templates/browser-agent/.env.example +19 -0
- package/templates/browser-agent/.husky/commit-msg +4 -0
- package/templates/browser-agent/.husky/pre-commit +11 -0
- package/templates/browser-agent/.husky/pre-push +6 -0
- package/templates/browser-agent/.prettierignore +6 -0
- package/templates/browser-agent/.prettierrc +9 -0
- package/templates/browser-agent/AGENTS.md +396 -0
- package/templates/browser-agent/CHANGELOG.md +15 -0
- package/templates/browser-agent/LICENSE +21 -0
- package/templates/browser-agent/README.md +103 -0
- package/templates/browser-agent/commitlint.config.js +8 -0
- package/templates/browser-agent/electrobun.config.ts +27 -0
- package/templates/browser-agent/electron/main.js +46 -0
- package/templates/browser-agent/electron/preload.js +5 -0
- package/templates/browser-agent/electron-builder.json +40 -0
- package/templates/browser-agent/eslint.config.mjs +79 -0
- package/templates/browser-agent/llms.txt +24 -0
- package/templates/browser-agent/package.json +140 -0
- package/templates/browser-agent/postcss.config.js +6 -0
- package/templates/browser-agent/scripts/dev.ts +138 -0
- package/templates/browser-agent/src/__tests__/hybrid-mode.test.ts +126 -0
- package/templates/browser-agent/src/__tests__/server-config-security.test.ts +55 -0
- package/templates/browser-agent/src/__tests__/server-config.test.ts +59 -0
- package/templates/browser-agent/src/bun/index.ts +130 -0
- package/templates/browser-agent/src/bun/three.d.ts +1 -0
- package/templates/browser-agent/src/gateway/http-routes.ts +1 -0
- package/templates/browser-agent/src/gateway/ipc-transport.ts +68 -0
- package/templates/browser-agent/src/gateway/sse-transport.ts +221 -0
- package/templates/browser-agent/src/mainview/App.tsx +45 -0
- package/templates/browser-agent/src/mainview/__tests__/hooks/use-rpc-init.test.ts +202 -0
- package/templates/browser-agent/src/mainview/__tests__/hooks/use-sidebar-resize.test.ts +127 -0
- package/templates/browser-agent/src/mainview/__tests__/i18n/i18n.test.ts +49 -0
- package/templates/browser-agent/src/mainview/__tests__/setup.ts +40 -0
- package/templates/browser-agent/src/mainview/__tests__/stores/use-chat-store.test.ts +73 -0
- package/templates/browser-agent/src/mainview/__tests__/stores/use-sidebar-store.test.ts +61 -0
- package/templates/browser-agent/src/mainview/__tests__/theme-variables.test.ts +36 -0
- package/templates/browser-agent/src/mainview/components/assets/AssetsPanel.tsx +139 -0
- package/templates/browser-agent/src/mainview/components/chat/ChatPanel.tsx +219 -0
- package/templates/browser-agent/src/mainview/components/chat/CommandBar.tsx +184 -0
- package/templates/browser-agent/src/mainview/components/chat/MessageBubble.tsx +249 -0
- package/templates/browser-agent/src/mainview/components/chat/ToolPicker.tsx +115 -0
- package/templates/browser-agent/src/mainview/components/common/ErrorBoundary.tsx +1 -0
- package/templates/browser-agent/src/mainview/components/common/LanguageSwitcher.tsx +15 -0
- package/templates/browser-agent/src/mainview/components/common/ThemeToggle.tsx +45 -0
- package/templates/browser-agent/src/mainview/components/common/__tests__/ErrorBoundary.test.tsx +74 -0
- package/templates/browser-agent/src/mainview/components/common/__tests__/LanguageSwitcher.test.tsx +44 -0
- package/templates/browser-agent/src/mainview/components/common/__tests__/ThemeToggle.test.tsx +41 -0
- package/templates/browser-agent/src/mainview/components/dev/NetworkPanel.tsx +155 -0
- package/templates/browser-agent/src/mainview/components/layout/AppLayout.tsx +311 -0
- package/templates/browser-agent/src/mainview/components/onboarding/SetupWizard.tsx +310 -0
- package/templates/browser-agent/src/mainview/components/process/ProcessPanel.tsx +329 -0
- package/templates/browser-agent/src/mainview/components/sidebar/SessionSidebar.tsx +122 -0
- package/templates/browser-agent/src/mainview/components/sidebar/SkillSidebar.tsx +115 -0
- package/templates/browser-agent/src/mainview/components/topbar/TopBar.tsx +350 -0
- package/templates/browser-agent/src/mainview/global.d.ts +13 -0
- package/templates/browser-agent/src/mainview/hooks/__tests__/use-breakpoint.test.ts +151 -0
- package/templates/browser-agent/src/mainview/hooks/use-agent-chat.ts +157 -0
- package/templates/browser-agent/src/mainview/hooks/use-breakpoint.ts +68 -0
- package/templates/browser-agent/src/mainview/hooks/use-rpc-init.ts +71 -0
- package/templates/browser-agent/src/mainview/hooks/use-sidebar-resize.ts +40 -0
- package/templates/browser-agent/src/mainview/index.css +71 -0
- package/templates/browser-agent/src/mainview/index.html +12 -0
- package/templates/browser-agent/src/mainview/lib/api-client.ts +397 -0
- package/templates/browser-agent/src/mainview/lib/i18n/index.ts +30 -0
- package/templates/browser-agent/src/mainview/lib/i18n/locales/en.json +130 -0
- package/templates/browser-agent/src/mainview/lib/i18n/locales/zh.json +128 -0
- package/templates/browser-agent/src/mainview/lib/network-bus.ts +92 -0
- package/templates/browser-agent/src/mainview/lib/rpc-cache.ts +94 -0
- package/templates/browser-agent/src/mainview/main.tsx +18 -0
- package/templates/browser-agent/src/mainview/stores/__tests__/use-connection-store.test.ts +26 -0
- package/templates/browser-agent/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
- package/templates/browser-agent/src/mainview/stores/__tests__/use-log-store.test.ts +28 -0
- package/templates/browser-agent/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
- package/templates/browser-agent/src/mainview/stores/message-batcher.ts +25 -0
- package/templates/browser-agent/src/mainview/stores/use-asset-store.ts +65 -0
- package/templates/browser-agent/src/mainview/stores/use-chat-store.ts +200 -0
- package/templates/browser-agent/src/mainview/stores/use-connection-store.ts +173 -0
- package/templates/browser-agent/src/mainview/stores/use-locale-store.ts +27 -0
- package/templates/browser-agent/src/mainview/stores/use-log-store.ts +16 -0
- package/templates/browser-agent/src/mainview/stores/use-network-panel-store.ts +18 -0
- package/templates/browser-agent/src/mainview/stores/use-record-store.ts +147 -0
- package/templates/browser-agent/src/mainview/stores/use-session-store.ts +151 -0
- package/templates/browser-agent/src/mainview/stores/use-sidebar-store.ts +161 -0
- package/templates/browser-agent/src/mainview/stores/use-theme-store.ts +46 -0
- package/templates/browser-agent/src/mainview/stores/use-view-store.ts +20 -0
- package/templates/browser-agent/src/mainview/types/index.ts +6 -0
- package/templates/browser-agent/src/server-config.ts +45 -0
- package/templates/browser-agent/src/server.ts +78 -0
- package/templates/browser-agent/src/shared/handlers/__tests__/chat-concurrency.test.ts +127 -0
- package/templates/browser-agent/src/shared/handlers/__tests__/chat-handler.test.ts +77 -0
- package/templates/browser-agent/src/shared/handlers/__tests__/file-handler.test.ts +100 -0
- package/templates/browser-agent/src/shared/handlers/__tests__/system-handler.test.ts +55 -0
- package/templates/browser-agent/src/shared/handlers/__tests__/timer-handler.test.ts +77 -0
- package/templates/browser-agent/src/shared/handlers/browser.ts +596 -0
- package/templates/browser-agent/src/shared/handlers/chat.ts +213 -0
- package/templates/browser-agent/src/shared/handlers/file.ts +99 -0
- package/templates/browser-agent/src/shared/handlers/index.ts +7 -0
- package/templates/browser-agent/src/shared/handlers/session.ts +167 -0
- package/templates/browser-agent/src/shared/handlers/system.ts +27 -0
- package/templates/browser-agent/src/shared/handlers/timer.ts +34 -0
- package/templates/browser-agent/src/shared/http-routes.ts +437 -0
- package/templates/browser-agent/src/shared/lib/__tests__/logger.test.ts +92 -0
- package/templates/browser-agent/src/shared/lib/__tests__/path-security.test.ts +77 -0
- package/templates/browser-agent/src/shared/lib/__tests__/web-server.test.ts +203 -0
- package/templates/browser-agent/src/shared/lib/agent.ts +384 -0
- package/templates/browser-agent/src/shared/lib/cdp.ts +448 -0
- package/templates/browser-agent/src/shared/lib/generate.ts +111 -0
- package/templates/browser-agent/src/shared/lib/logger.ts +152 -0
- package/templates/browser-agent/src/shared/lib/mock-stream.ts +147 -0
- package/templates/browser-agent/src/shared/lib/path-security.ts +38 -0
- package/templates/browser-agent/src/shared/lib/port-registry.ts +103 -0
- package/templates/browser-agent/src/shared/lib/web-server.ts +127 -0
- package/templates/browser-agent/src/shared/lib/xhs-extract.ts +71 -0
- package/templates/browser-agent/src/shared/modules/browser.ts +86 -0
- package/templates/browser-agent/src/shared/modules/chat.ts +20 -0
- package/templates/browser-agent/src/shared/modules/file.ts +40 -0
- package/templates/browser-agent/src/shared/modules/session.ts +55 -0
- package/templates/browser-agent/src/shared/modules/system.ts +8 -0
- package/templates/browser-agent/src/shared/modules/timer.ts +11 -0
- package/templates/browser-agent/src/shared/register-all-handlers.ts +36 -0
- package/templates/browser-agent/src/shared/rpc-schema.ts +34 -0
- package/templates/browser-agent/tailwind.config.js +15 -0
- package/templates/browser-agent/tsconfig.ipc.json +14 -0
- package/templates/browser-agent/tsconfig.json +36 -0
- package/templates/browser-agent/vite.config.ts +3 -0
- package/templates/browser-agent/vitest.config.ts +3 -0
- package/templates/chat/.husky/commit-msg +4 -0
- package/templates/chat/.husky/pre-commit +11 -0
- package/templates/chat/.husky/pre-push +6 -0
- package/templates/chat/CHANGELOG.md +15 -0
- package/templates/chat/commitlint.config.js +8 -0
- package/templates/chat/package.json +19 -4
- package/templates/chat/src/mainview/__tests__/hooks/use-input-history.test.ts +125 -0
- package/templates/chat/src/mainview/__tests__/setup.ts +32 -29
- package/templates/chat/src/mainview/components/chat/ChatPanel.tsx +80 -55
- package/templates/chat/src/mainview/components/common/ThemeToggle.tsx +40 -20
- package/templates/chat/src/mainview/lib/i18n/locales/en.json +175 -155
- package/templates/chat/src/mainview/lib/i18n/locales/zh.json +173 -155
- package/templates/chat/src/shared/handlers/chat.ts +17 -19
- package/templates/chat/src/shared/handlers/debug.ts +12 -2
- package/templates/cowork/.env.example +8 -0
- package/templates/cowork/.prettierignore +6 -0
- package/templates/cowork/.prettierrc +9 -0
- package/templates/cowork/AGENTS.md +236 -0
- package/templates/cowork/CHANGELOG.md +15 -0
- package/templates/cowork/LICENSE +21 -0
- package/templates/cowork/README.md +103 -0
- package/templates/cowork/commitlint.config.js +8 -0
- package/templates/cowork/docs/CODE-VIEW-PLAN.md +637 -0
- package/templates/cowork/docs/CODE-VIEW-V2.md +196 -0
- package/templates/cowork/docs/CODE-VIEW-V4.md +150 -0
- package/templates/cowork/electrobun.config.ts +27 -0
- package/templates/cowork/eslint.config.mjs +79 -0
- package/templates/cowork/llms.txt +24 -0
- package/templates/cowork/package.json +87 -0
- package/templates/cowork/postcss.config.js +6 -0
- package/templates/cowork/scripts/dev.ts +138 -0
- package/templates/cowork/src/__tests__/hybrid-mode.test.ts +126 -0
- package/templates/cowork/src/__tests__/server-config-security.test.ts +55 -0
- package/templates/cowork/src/__tests__/server-config.test.ts +59 -0
- package/templates/cowork/src/bun/index.ts +130 -0
- package/templates/cowork/src/bun/three.d.ts +1 -0
- package/templates/cowork/src/gateway/http-routes.ts +1 -0
- package/templates/cowork/src/gateway/ipc-transport.ts +68 -0
- package/templates/cowork/src/gateway/sse-transport.ts +231 -0
- package/templates/cowork/src/mainview/App.tsx +45 -0
- package/templates/cowork/src/mainview/__tests__/hooks/use-rpc-init.test.ts +202 -0
- package/templates/cowork/src/mainview/__tests__/hooks/use-sidebar-resize.test.ts +127 -0
- package/templates/cowork/src/mainview/__tests__/i18n/i18n.test.ts +49 -0
- package/templates/cowork/src/mainview/__tests__/setup.ts +40 -0
- package/templates/cowork/src/mainview/__tests__/stores/use-chat-store.test.ts +73 -0
- package/templates/cowork/src/mainview/__tests__/stores/use-sidebar-store.test.ts +61 -0
- package/templates/cowork/src/mainview/__tests__/theme-variables.test.ts +36 -0
- package/templates/cowork/src/mainview/components/chat/TaskChat.tsx +311 -0
- package/templates/cowork/src/mainview/components/chat/__tests__/localhost-links.test.ts +76 -0
- package/templates/cowork/src/mainview/components/common/ErrorBoundary.tsx +1 -0
- package/templates/cowork/src/mainview/components/common/LanguageSwitcher.tsx +15 -0
- package/templates/cowork/src/mainview/components/common/ThemeToggle.tsx +45 -0
- package/templates/cowork/src/mainview/components/common/__tests__/ErrorBoundary.test.tsx +74 -0
- package/templates/cowork/src/mainview/components/common/__tests__/LanguageSwitcher.test.tsx +44 -0
- package/templates/cowork/src/mainview/components/common/__tests__/ThemeToggle.test.tsx +41 -0
- package/templates/cowork/src/mainview/components/dev/NetworkPanel.tsx +155 -0
- package/templates/cowork/src/mainview/components/layout/AppLayout.tsx +216 -0
- package/templates/cowork/src/mainview/components/right/ArtifactsPanel.tsx +54 -0
- package/templates/cowork/src/mainview/components/right/ContextPanel.tsx +133 -0
- package/templates/cowork/src/mainview/components/right/ElementPicker.tsx +206 -0
- package/templates/cowork/src/mainview/components/right/PreviewBlock.tsx +155 -0
- package/templates/cowork/src/mainview/components/right/ProgressPanel.tsx +85 -0
- package/templates/cowork/src/mainview/components/sidebar/TaskSidebar.tsx +211 -0
- package/templates/cowork/src/mainview/components/topbar/TopBar.tsx +86 -0
- package/templates/cowork/src/mainview/global.d.ts +13 -0
- package/templates/cowork/src/mainview/hooks/__tests__/use-breakpoint.test.ts +123 -0
- package/templates/cowork/src/mainview/hooks/use-breakpoint.ts +53 -0
- package/templates/cowork/src/mainview/hooks/use-rpc-init.ts +26 -0
- package/templates/cowork/src/mainview/hooks/use-sidebar-resize.ts +40 -0
- package/templates/cowork/src/mainview/index.css +89 -0
- package/templates/cowork/src/mainview/index.html +12 -0
- package/templates/cowork/src/mainview/lib/api-client.ts +404 -0
- package/templates/cowork/src/mainview/lib/i18n/index.ts +30 -0
- package/templates/cowork/src/mainview/lib/i18n/locales/en.json +130 -0
- package/templates/cowork/src/mainview/lib/i18n/locales/zh.json +128 -0
- package/templates/cowork/src/mainview/lib/network-bus.ts +92 -0
- package/templates/cowork/src/mainview/lib/rpc-cache.ts +94 -0
- package/templates/cowork/src/mainview/main.tsx +18 -0
- package/templates/cowork/src/mainview/stores/__tests__/use-connection-store.test.ts +26 -0
- package/templates/cowork/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
- package/templates/cowork/src/mainview/stores/__tests__/use-log-store.test.ts +28 -0
- package/templates/cowork/src/mainview/stores/__tests__/use-preview-store.test.ts +193 -0
- package/templates/cowork/src/mainview/stores/__tests__/use-sidebar-store.test.ts +81 -0
- package/templates/cowork/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
- package/templates/cowork/src/mainview/stores/message-batcher.ts +25 -0
- package/templates/cowork/src/mainview/stores/use-chat-store.ts +198 -0
- package/templates/cowork/src/mainview/stores/use-connection-store.ts +168 -0
- package/templates/cowork/src/mainview/stores/use-context-store.ts +68 -0
- package/templates/cowork/src/mainview/stores/use-locale-store.ts +27 -0
- package/templates/cowork/src/mainview/stores/use-log-store.ts +16 -0
- package/templates/cowork/src/mainview/stores/use-network-panel-store.ts +18 -0
- package/templates/cowork/src/mainview/stores/use-output-store.ts +47 -0
- package/templates/cowork/src/mainview/stores/use-preview-store.ts +97 -0
- package/templates/cowork/src/mainview/stores/use-sidebar-store.ts +272 -0
- package/templates/cowork/src/mainview/stores/use-task-store.ts +86 -0
- package/templates/cowork/src/mainview/stores/use-theme-store.ts +46 -0
- package/templates/cowork/src/mainview/stores/use-view-store.ts +29 -0
- package/templates/cowork/src/mainview/types/index.ts +6 -0
- package/templates/cowork/src/server-config.ts +44 -0
- package/templates/cowork/src/server.ts +78 -0
- package/templates/cowork/src/shared/handlers/__tests__/chat-concurrency.test.ts +127 -0
- package/templates/cowork/src/shared/handlers/__tests__/chat-handler.test.ts +77 -0
- package/templates/cowork/src/shared/handlers/__tests__/file-handler.test.ts +100 -0
- package/templates/cowork/src/shared/handlers/__tests__/preview-handler.test.ts +172 -0
- package/templates/cowork/src/shared/handlers/__tests__/system-handler.test.ts +55 -0
- package/templates/cowork/src/shared/handlers/__tests__/timer-handler.test.ts +77 -0
- package/templates/cowork/src/shared/handlers/chat.ts +213 -0
- package/templates/cowork/src/shared/handlers/context.ts +72 -0
- package/templates/cowork/src/shared/handlers/file.ts +99 -0
- package/templates/cowork/src/shared/handlers/index.ts +9 -0
- package/templates/cowork/src/shared/handlers/output.ts +59 -0
- package/templates/cowork/src/shared/handlers/preview.ts +81 -0
- package/templates/cowork/src/shared/handlers/system.ts +27 -0
- package/templates/cowork/src/shared/handlers/task.ts +101 -0
- package/templates/cowork/src/shared/handlers/timer.ts +34 -0
- package/templates/cowork/src/shared/http-routes.ts +444 -0
- package/templates/cowork/src/shared/lib/__tests__/logger.test.ts +92 -0
- package/templates/cowork/src/shared/lib/__tests__/path-security.test.ts +77 -0
- package/templates/cowork/src/shared/lib/__tests__/web-server.test.ts +203 -0
- package/templates/cowork/src/shared/lib/logger.ts +152 -0
- package/templates/cowork/src/shared/lib/path-security.ts +38 -0
- package/templates/cowork/src/shared/lib/port-registry.ts +103 -0
- package/templates/cowork/src/shared/lib/web-server.ts +127 -0
- package/templates/cowork/src/shared/modules/chat.ts +20 -0
- package/templates/cowork/src/shared/modules/context.ts +32 -0
- package/templates/cowork/src/shared/modules/file.ts +40 -0
- package/templates/cowork/src/shared/modules/output.ts +20 -0
- package/templates/cowork/src/shared/modules/preview.ts +44 -0
- package/templates/cowork/src/shared/modules/system.ts +8 -0
- package/templates/cowork/src/shared/modules/task.ts +31 -0
- package/templates/cowork/src/shared/modules/timer.ts +11 -0
- package/templates/cowork/src/shared/register-all-handlers.ts +36 -0
- package/templates/cowork/src/shared/rpc-schema.ts +38 -0
- package/templates/cowork/tailwind.config.js +15 -0
- package/templates/cowork/test-upload.txt +0 -0
- package/templates/cowork/tsconfig.ipc.json +14 -0
- package/templates/cowork/tsconfig.json +36 -0
- package/templates/cowork/vite.config.ts +3 -0
- package/templates/cowork/vitest.config.ts +3 -0
- package/templates/general/.husky/commit-msg +4 -0
- package/templates/general/.husky/pre-commit +11 -0
- package/templates/general/.husky/pre-push +6 -0
- package/templates/general/CHANGELOG.md +15 -0
- package/templates/general/commitlint.config.js +8 -0
- package/templates/general/package.json +19 -4
- package/templates/general/src/mainview/__tests__/hooks/use-input-history.test.ts +125 -0
- package/templates/general/src/mainview/__tests__/setup.ts +32 -29
- package/templates/general/src/mainview/components/chat/ChatPanel.tsx +80 -55
- package/templates/general/src/mainview/components/common/ThemeToggle.tsx +40 -20
- package/templates/general/src/mainview/components/debug/DebugPanel.tsx +170 -156
- package/templates/general/src/mainview/components/debug/__tests__/DebugPanel.test.tsx +169 -0
- package/templates/general/src/mainview/components/explorer/ConfirmDialog.tsx +42 -42
- package/templates/general/src/mainview/components/explorer/ContextMenu.tsx +72 -67
- package/templates/general/src/mainview/components/feed/FeedPanel.tsx +7 -5
- package/templates/general/src/mainview/components/file-preview/FilePreviewOverlay.tsx +64 -45
- package/templates/general/src/mainview/components/file-preview/VirtualizedCodeView.tsx +24 -7
- package/templates/general/src/mainview/components/git/GitPanel.tsx +700 -473
- package/templates/general/src/mainview/components/layout/AppLayout.tsx +123 -114
- package/templates/general/src/mainview/components/search/SearchPanel.tsx +15 -19
- package/templates/general/src/mainview/lib/i18n/locales/en.json +175 -155
- package/templates/general/src/mainview/lib/i18n/locales/zh.json +173 -155
- package/templates/general/src/mainview/stores/__tests__/use-explorer-store.test.ts +259 -0
- package/templates/general/src/mainview/stores/__tests__/use-feed-store.test.ts +160 -0
- package/templates/general/src/mainview/stores/__tests__/use-git-store.test.ts +313 -0
- package/templates/general/src/mainview/stores/__tests__/use-notification-store.test.ts +115 -0
- package/templates/general/src/mainview/stores/__tests__/use-sidebar-store.test.ts +120 -0
- package/templates/general/src/mainview/stores/use-explorer-store.ts +277 -260
- package/templates/general/src/mainview/types/index.ts +21 -20
- package/templates/general/src/shared/handlers/chat.ts +1 -1
- package/templates/general/src/shared/handlers/debug.ts +12 -2
- package/templates/shared/components/ErrorBoundary.tsx +0 -1
- package/templates/shared/vite-base.config.ts +8 -7
- /package/templates/{shared → browser-agent}/test-upload.txt +0 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Store — 会话管理
|
|
3
|
+
*
|
|
4
|
+
* 对应 PRD §8 数据模型:会话 CRUD、消息管理
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { create } from "zustand";
|
|
8
|
+
import { apiClient } from "../lib/api-client";
|
|
9
|
+
|
|
10
|
+
interface Message {
|
|
11
|
+
id: string;
|
|
12
|
+
role: "user" | "agent";
|
|
13
|
+
text: string;
|
|
14
|
+
steps?: { label: string; status: string; detail?: string }[];
|
|
15
|
+
toolCalls?: { tool: string; input: string; output: string; status: string }[];
|
|
16
|
+
thinking?: string;
|
|
17
|
+
turns?: any[];
|
|
18
|
+
summary?: any;
|
|
19
|
+
error?: string;
|
|
20
|
+
at: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface SessionSummary {
|
|
24
|
+
id: string;
|
|
25
|
+
title: string;
|
|
26
|
+
createdAt: number;
|
|
27
|
+
status: string;
|
|
28
|
+
messageCount: number;
|
|
29
|
+
assetCount: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface SessionDetail {
|
|
33
|
+
id: string;
|
|
34
|
+
title: string;
|
|
35
|
+
messages: Message[];
|
|
36
|
+
assets: any[];
|
|
37
|
+
createdAt: number;
|
|
38
|
+
status: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface SessionState {
|
|
42
|
+
// 会话列表
|
|
43
|
+
sessions: SessionSummary[];
|
|
44
|
+
currentSessionId: string | null;
|
|
45
|
+
currentSession: SessionDetail | null;
|
|
46
|
+
running: boolean;
|
|
47
|
+
|
|
48
|
+
// Actions
|
|
49
|
+
setRunning: (running: boolean) => void;
|
|
50
|
+
refreshSessions: () => Promise<void>;
|
|
51
|
+
createSession: (title?: string) => Promise<SessionSummary>;
|
|
52
|
+
loadSession: (id: string) => Promise<void>;
|
|
53
|
+
patchCurrentSession: (patch: Partial<SessionDetail>) => void;
|
|
54
|
+
patchLastAgentMessage: (patch: Partial<Message>) => void;
|
|
55
|
+
/** 确保 UI 有一个可用的会话(空会话或已有会话),用户能直接输入 */
|
|
56
|
+
ensureDefaultSession: () => Promise<string>;
|
|
57
|
+
/** 智能新建:当前空会话未被使用时复用,否则新建 */
|
|
58
|
+
newSessionSmart: () => Promise<string>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const useSessionStore = create<SessionState>((set, get) => ({
|
|
62
|
+
sessions: [],
|
|
63
|
+
currentSessionId: null,
|
|
64
|
+
currentSession: null,
|
|
65
|
+
running: false,
|
|
66
|
+
|
|
67
|
+
setRunning: (running) => set({ running }),
|
|
68
|
+
|
|
69
|
+
refreshSessions: async () => {
|
|
70
|
+
try {
|
|
71
|
+
const result = await apiClient.call("session.list", {});
|
|
72
|
+
set({ sessions: result.sessions });
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.error("Failed to load sessions", err);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
createSession: async (title) => {
|
|
79
|
+
const result = await apiClient.call("session.create", { title }) as SessionSummary;
|
|
80
|
+
await get().refreshSessions();
|
|
81
|
+
return result;
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
loadSession: async (id) => {
|
|
85
|
+
try {
|
|
86
|
+
const session = await apiClient.call("session.get", { id });
|
|
87
|
+
if (session) {
|
|
88
|
+
set({ currentSessionId: id, currentSession: session });
|
|
89
|
+
}
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error("Failed to load session", err);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
patchCurrentSession: (patch) => {
|
|
96
|
+
const cur = get().currentSession;
|
|
97
|
+
if (cur) {
|
|
98
|
+
set({ currentSession: { ...cur, ...patch } });
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
patchLastAgentMessage: (patch) => {
|
|
103
|
+
const cur = get().currentSession;
|
|
104
|
+
if (!cur) return;
|
|
105
|
+
const msgs = [...(cur.messages || [])];
|
|
106
|
+
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
107
|
+
const m = msgs[i];
|
|
108
|
+
if (!m || m.role !== "agent") continue;
|
|
109
|
+
msgs[i] = { ...m, ...patch };
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
set({ currentSession: { ...cur, messages: msgs } });
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
ensureDefaultSession: async () => {
|
|
116
|
+
const state = get();
|
|
117
|
+
// 已有选中的会话 → 不动
|
|
118
|
+
if (state.currentSessionId) return state.currentSessionId;
|
|
119
|
+
|
|
120
|
+
// 刷新列表
|
|
121
|
+
await get().refreshSessions();
|
|
122
|
+
const sessions = get().sessions;
|
|
123
|
+
|
|
124
|
+
if (sessions.length > 0) {
|
|
125
|
+
// 有历史会话 → 选中第一个
|
|
126
|
+
const first = sessions[0]!;
|
|
127
|
+
await get().loadSession(first.id);
|
|
128
|
+
return first.id;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 没有任何会话 → 创建空会话
|
|
132
|
+
const sess = await get().createSession("新会话");
|
|
133
|
+
await get().loadSession(sess.id);
|
|
134
|
+
return sess.id;
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
newSessionSmart: async () => {
|
|
138
|
+
const state = get();
|
|
139
|
+
// 如果当前会话是空的(没有消息),直接复用,不创建新的
|
|
140
|
+
if (state.currentSession) {
|
|
141
|
+
const msgCount = state.currentSession.messages?.length || 0;
|
|
142
|
+
if (msgCount === 0) {
|
|
143
|
+
return state.currentSessionId!;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// 否则创建新会话
|
|
147
|
+
const sess = await get().createSession("新会话");
|
|
148
|
+
await get().loadSession(sess.id);
|
|
149
|
+
return sess.id;
|
|
150
|
+
},
|
|
151
|
+
}));
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { create } from "zustand";
|
|
2
|
+
|
|
3
|
+
export type Breakpoint = "mobile" | "tablet" | "desktop" | "wide";
|
|
4
|
+
|
|
5
|
+
/** 侧边栏三态折叠模式 */
|
|
6
|
+
export type SidebarMode = "full" | "icon" | "hidden";
|
|
7
|
+
|
|
8
|
+
const PINNED_KEY = "sidebar-pinned";
|
|
9
|
+
const WIDTH_KEY = "sidebar-width";
|
|
10
|
+
const MODE_KEY = "sidebar-mode";
|
|
11
|
+
const COLLAPSED_SECTIONS_KEY = "sidebar-collapsed-sections";
|
|
12
|
+
|
|
13
|
+
export const SIDEBAR_MIN_WIDTH = 180;
|
|
14
|
+
export const SIDEBAR_MAX_WIDTH = 480;
|
|
15
|
+
export const SIDEBAR_ICON_WIDTH = 56;
|
|
16
|
+
const DEFAULT_WIDTH = 240;
|
|
17
|
+
|
|
18
|
+
/** 断点定义:mobile <768, tablet 768-1023, desktop 1024-1279, wide ≥1280 */
|
|
19
|
+
function getBreakpoint(width: number): Breakpoint {
|
|
20
|
+
if (width < 768) return "mobile";
|
|
21
|
+
if (width < 1024) return "tablet";
|
|
22
|
+
if (width < 1280) return "desktop";
|
|
23
|
+
return "wide";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface SidebarState {
|
|
27
|
+
breakpoint: Breakpoint;
|
|
28
|
+
isPinned: boolean;
|
|
29
|
+
drawerOpen: boolean;
|
|
30
|
+
sidebarWidth: number;
|
|
31
|
+
/** 三态折叠模式(替代 isPinned 在桌面端的主折叠语义) */
|
|
32
|
+
sidebarMode: SidebarMode;
|
|
33
|
+
/** 区块级折叠(section id 集合,如 "skills" / "sessions") */
|
|
34
|
+
collapsedSections: Set<string>;
|
|
35
|
+
setPinned: (pinned: boolean) => void;
|
|
36
|
+
setDrawerOpen: (open: boolean) => void;
|
|
37
|
+
setSidebarWidth: (width: number) => void;
|
|
38
|
+
setSidebarMode: (mode: SidebarMode) => void;
|
|
39
|
+
/** 循环切换:full → icon → hidden → full */
|
|
40
|
+
cycleSidebarMode: () => void;
|
|
41
|
+
/** 切换区块折叠状态 */
|
|
42
|
+
toggleSection: (id: string) => void;
|
|
43
|
+
_setBreakpoint: (bp: Breakpoint) => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function readPinned(): boolean {
|
|
47
|
+
try {
|
|
48
|
+
const stored = localStorage.getItem(PINNED_KEY);
|
|
49
|
+
if (stored !== null) return stored !== "false";
|
|
50
|
+
return window.innerWidth >= 1024;
|
|
51
|
+
} catch {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function readWidth(): number {
|
|
57
|
+
try {
|
|
58
|
+
const v = Number(localStorage.getItem(WIDTH_KEY));
|
|
59
|
+
if (Number.isNaN(v) || v < SIDEBAR_MIN_WIDTH || v > SIDEBAR_MAX_WIDTH) return DEFAULT_WIDTH;
|
|
60
|
+
return v;
|
|
61
|
+
} catch {
|
|
62
|
+
return DEFAULT_WIDTH;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function readMode(): SidebarMode {
|
|
67
|
+
try {
|
|
68
|
+
const v = localStorage.getItem(MODE_KEY);
|
|
69
|
+
if (v === "full" || v === "icon" || v === "hidden") return v;
|
|
70
|
+
} catch { /* ignore */ }
|
|
71
|
+
return "full";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function readCollapsedSections(): Set<string> {
|
|
75
|
+
try {
|
|
76
|
+
const v = localStorage.getItem(COLLAPSED_SECTIONS_KEY);
|
|
77
|
+
if (v) return new Set(JSON.parse(v) as string[]);
|
|
78
|
+
} catch { /* ignore */ }
|
|
79
|
+
return new Set();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function persistCollapsedSections(set: Set<string>): void {
|
|
83
|
+
try {
|
|
84
|
+
localStorage.setItem(COLLAPSED_SECTIONS_KEY, JSON.stringify([...set]));
|
|
85
|
+
} catch { /* ignore */ }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const initialWidth = typeof window !== "undefined" ? window.innerWidth : 1024;
|
|
89
|
+
|
|
90
|
+
export const useSidebarStore = create<SidebarState>((set, get) => ({
|
|
91
|
+
breakpoint: getBreakpoint(initialWidth),
|
|
92
|
+
isPinned: readPinned(),
|
|
93
|
+
drawerOpen: false,
|
|
94
|
+
sidebarWidth: readWidth(),
|
|
95
|
+
sidebarMode: readMode(),
|
|
96
|
+
collapsedSections: readCollapsedSections(),
|
|
97
|
+
|
|
98
|
+
setPinned: (pinned) => {
|
|
99
|
+
try {
|
|
100
|
+
localStorage.setItem(PINNED_KEY, String(pinned));
|
|
101
|
+
} catch { /* ignore */ }
|
|
102
|
+
set({ isPinned: pinned, drawerOpen: false });
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
setDrawerOpen: (open) => {
|
|
106
|
+
set({ drawerOpen: open });
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
setSidebarWidth: (width) => {
|
|
110
|
+
const clamped = Math.min(SIDEBAR_MAX_WIDTH, Math.max(SIDEBAR_MIN_WIDTH, width));
|
|
111
|
+
try {
|
|
112
|
+
localStorage.setItem(WIDTH_KEY, String(clamped));
|
|
113
|
+
} catch { /* ignore */ }
|
|
114
|
+
set({ sidebarWidth: clamped });
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
setSidebarMode: (mode) => {
|
|
118
|
+
try {
|
|
119
|
+
localStorage.setItem(MODE_KEY, mode);
|
|
120
|
+
} catch { /* ignore */ }
|
|
121
|
+
set({ sidebarMode: mode });
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
cycleSidebarMode: () => {
|
|
125
|
+
const order: SidebarMode[] = ["full", "icon", "hidden"];
|
|
126
|
+
const current = get().sidebarMode;
|
|
127
|
+
const next = order[(order.indexOf(current) + 1) % order.length] || "full";
|
|
128
|
+
try {
|
|
129
|
+
localStorage.setItem(MODE_KEY, next);
|
|
130
|
+
} catch { /* ignore */ }
|
|
131
|
+
set({ sidebarMode: next });
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
toggleSection: (id) => {
|
|
135
|
+
const sections = new Set(get().collapsedSections);
|
|
136
|
+
if (sections.has(id)) {
|
|
137
|
+
sections.delete(id);
|
|
138
|
+
} else {
|
|
139
|
+
sections.add(id);
|
|
140
|
+
}
|
|
141
|
+
persistCollapsedSections(sections);
|
|
142
|
+
set({ collapsedSections: sections });
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
_setBreakpoint: (bp) => set({ breakpoint: bp }),
|
|
146
|
+
}));
|
|
147
|
+
|
|
148
|
+
/** 资源面板(右侧)状态 */
|
|
149
|
+
interface PanelState {
|
|
150
|
+
assetsVisible: boolean;
|
|
151
|
+
assetsDrawerOpen: boolean;
|
|
152
|
+
setAssetsVisible: (v: boolean) => void;
|
|
153
|
+
setAssetsDrawerOpen: (open: boolean) => void;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export const useAssetsPanelStore = create<PanelState>((set) => ({
|
|
157
|
+
assetsVisible: true,
|
|
158
|
+
assetsDrawerOpen: false,
|
|
159
|
+
setAssetsVisible: (v) => set({ assetsVisible: v }),
|
|
160
|
+
setAssetsDrawerOpen: (open) => set({ assetsDrawerOpen: open }),
|
|
161
|
+
}));
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { create } from "zustand";
|
|
2
|
+
|
|
3
|
+
export type Theme = "light" | "dark";
|
|
4
|
+
|
|
5
|
+
interface ThemeState {
|
|
6
|
+
theme: Theme;
|
|
7
|
+
setTheme: (theme: Theme) => void;
|
|
8
|
+
toggleTheme: () => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getInitialTheme(): Theme {
|
|
12
|
+
if (typeof window === "undefined") return "dark";
|
|
13
|
+
const stored = localStorage.getItem("theme");
|
|
14
|
+
if (stored === "light" || stored === "dark") return stored;
|
|
15
|
+
return "dark";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function applyTheme(theme: Theme): void {
|
|
19
|
+
if (typeof document === "undefined") return;
|
|
20
|
+
if (theme === "dark") {
|
|
21
|
+
document.documentElement.classList.add("dark");
|
|
22
|
+
} else {
|
|
23
|
+
document.documentElement.classList.remove("dark");
|
|
24
|
+
}
|
|
25
|
+
localStorage.setItem("theme", theme);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const useThemeStore = create<ThemeState>((set) => {
|
|
29
|
+
const initial = getInitialTheme();
|
|
30
|
+
applyTheme(initial);
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
theme: initial,
|
|
34
|
+
setTheme: (theme) => {
|
|
35
|
+
applyTheme(theme);
|
|
36
|
+
set({ theme });
|
|
37
|
+
},
|
|
38
|
+
toggleTheme: () => {
|
|
39
|
+
set((state) => {
|
|
40
|
+
const next = state.theme === "dark" ? "light" : "dark";
|
|
41
|
+
applyTheme(next);
|
|
42
|
+
return { theme: next };
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* View Store — 全局视图状态
|
|
3
|
+
*
|
|
4
|
+
* 管理当前激活的视图 Tab(会话/加工)。
|
|
5
|
+
* TopBar、SidebarInner、AppLayout 都能访问,无需层层传递。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { create } from "zustand";
|
|
9
|
+
|
|
10
|
+
export type ViewTab = "chat" | "process";
|
|
11
|
+
|
|
12
|
+
interface ViewState {
|
|
13
|
+
activeView: ViewTab;
|
|
14
|
+
setActiveView: (view: ViewTab) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const useViewStore = create<ViewState>((set) => ({
|
|
18
|
+
activeView: "chat",
|
|
19
|
+
setActiveView: (view) => set({ activeView: view }),
|
|
20
|
+
}));
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web server configuration — single source of truth.
|
|
3
|
+
* Values are read from environment variables with sensible defaults.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { randomUUID } from 'crypto';
|
|
7
|
+
|
|
8
|
+
if (process.env.NODE_ENV === 'production' && !process.env.AUTH_TOKEN) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
'[FATAL] AUTH_TOKEN environment variable must be set in production mode. ' +
|
|
11
|
+
'Example: AUTH_TOKEN=$(openssl rand -hex 32) bun src/server.ts',
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function parseEnvInt(
|
|
16
|
+
key: string,
|
|
17
|
+
value: string | undefined,
|
|
18
|
+
defaultValue: number,
|
|
19
|
+
min: number,
|
|
20
|
+
max: number,
|
|
21
|
+
): number {
|
|
22
|
+
if (value === undefined || value === '') return defaultValue;
|
|
23
|
+
const parsed = parseInt(value, 10);
|
|
24
|
+
if (isNaN(parsed) || parsed < min || parsed > max) {
|
|
25
|
+
process.stderr.write(`[config] Invalid ${key}: "${value}", using default: ${defaultValue}\n`);
|
|
26
|
+
return defaultValue;
|
|
27
|
+
}
|
|
28
|
+
return parsed;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const config = {
|
|
32
|
+
port: parseEnvInt('PORT', process.env.PORT, 5200, 1024, 65535),
|
|
33
|
+
authToken: process.env.AUTH_TOKEN || `dev-${randomUUID()}`,
|
|
34
|
+
maxUploadSize: parseEnvInt(
|
|
35
|
+
'MAX_UPLOAD_SIZE',
|
|
36
|
+
process.env.MAX_UPLOAD_SIZE,
|
|
37
|
+
50 * 1024 * 1024,
|
|
38
|
+
0,
|
|
39
|
+
1024 * 1024 * 1024,
|
|
40
|
+
),
|
|
41
|
+
logDir: process.env.LOG_DIR || 'logs',
|
|
42
|
+
enableWebService: process.env.ENABLE_WEB_SERVICE === 'true',
|
|
43
|
+
enableMockStream: process.env.ENABLE_MOCK_STREAM === 'true',
|
|
44
|
+
corsOrigin: process.env.CORS_ORIGIN || 'http://localhost:7200',
|
|
45
|
+
} as const;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web server entry point — HTTP file endpoints + SSE/HTTP RPC gateway.
|
|
3
|
+
* Port auto-negotiation: tries config.port first, increments on EADDRINUSE.
|
|
4
|
+
* Writes actual port to .server-port for dev orchestration.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { writeFileSync, unlinkSync, existsSync } from "fs";
|
|
8
|
+
import { join, resolve, basename } from "path";
|
|
9
|
+
import { config } from "./server-config";
|
|
10
|
+
import { createWebServer, getLocalIP } from "./shared/lib/web-server";
|
|
11
|
+
import { createLogger, configureLogDir } from "./shared/lib/logger";
|
|
12
|
+
import { registerPort, unregisterPort, formatRegistryForOutput } from "./shared/lib/port-registry";
|
|
13
|
+
import { discoverMethodNames } from "./shared/register-all-handlers";
|
|
14
|
+
|
|
15
|
+
configureLogDir(config.logDir);
|
|
16
|
+
const log = createLogger("server");
|
|
17
|
+
|
|
18
|
+
const PORT_FILE = join(import.meta.dir, "..", ".server-port");
|
|
19
|
+
const PROJECT_ROOT = resolve(import.meta.dir, "..");
|
|
20
|
+
const PROJECT_NAME = basename(PROJECT_ROOT);
|
|
21
|
+
|
|
22
|
+
function cleanupPortFile() {
|
|
23
|
+
try {
|
|
24
|
+
if (existsSync(PORT_FILE)) unlinkSync(PORT_FILE);
|
|
25
|
+
} catch {}
|
|
26
|
+
unregisterPort(PROJECT_ROOT);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function writePortFile(port: number) {
|
|
30
|
+
writeFileSync(PORT_FILE, String(port), "utf-8");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
process.on("exit", cleanupPortFile);
|
|
34
|
+
process.on("SIGINT", () => {
|
|
35
|
+
cleanupPortFile();
|
|
36
|
+
process.exit(0);
|
|
37
|
+
});
|
|
38
|
+
process.on("SIGTERM", () => {
|
|
39
|
+
cleanupPortFile();
|
|
40
|
+
process.exit(0);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
async function start() {
|
|
44
|
+
const { port, close } = await createWebServer({
|
|
45
|
+
port: config.port,
|
|
46
|
+
authToken: config.authToken,
|
|
47
|
+
maxUploadSize: config.maxUploadSize,
|
|
48
|
+
corsOrigin: config.corsOrigin,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
process.on("SIGINT", () => {
|
|
52
|
+
close();
|
|
53
|
+
cleanupPortFile();
|
|
54
|
+
process.exit(0);
|
|
55
|
+
});
|
|
56
|
+
process.on("SIGTERM", () => {
|
|
57
|
+
close();
|
|
58
|
+
cleanupPortFile();
|
|
59
|
+
process.exit(0);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
writePortFile(port);
|
|
63
|
+
registerPort(PROJECT_ROOT, port, PROJECT_NAME);
|
|
64
|
+
|
|
65
|
+
const localIp = getLocalIP();
|
|
66
|
+
log.info(`HTTP + SSE server running on http://localhost:${port}`);
|
|
67
|
+
log.info(`Local network access: http://${localIp}:${port}`);
|
|
68
|
+
log.info(`RPC: POST http://localhost:${port}/api/rpc (auth required)`);
|
|
69
|
+
log.info(`SSE: GET http://localhost:${port}/api/events (auth required)`);
|
|
70
|
+
log.info(`Available RPC methods: ${discoverMethodNames().join(", ")}`);
|
|
71
|
+
log.info("File endpoints: GET /file/{path}, GET /info/{path}");
|
|
72
|
+
log.info(formatRegistryForOutput());
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
start().catch((err) => {
|
|
76
|
+
log.error("Server failed to start", { error: err.message });
|
|
77
|
+
process.exit(1);
|
|
78
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import { unlink } from "fs/promises";
|
|
5
|
+
import { existsSync } from "fs";
|
|
6
|
+
|
|
7
|
+
vi.mock("../../lib/logger", () => ({
|
|
8
|
+
createLogger: () => ({
|
|
9
|
+
info: vi.fn(),
|
|
10
|
+
error: vi.fn(),
|
|
11
|
+
warn: vi.fn(),
|
|
12
|
+
debug: vi.fn(),
|
|
13
|
+
}),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
function createMockServer() {
|
|
17
|
+
const handlers = new Map<string, (params: unknown) => Promise<unknown>>();
|
|
18
|
+
return {
|
|
19
|
+
register: vi.fn((method: string, handler: (params: unknown) => Promise<unknown>) => {
|
|
20
|
+
handlers.set(method, handler);
|
|
21
|
+
}),
|
|
22
|
+
emitEvent: vi.fn(),
|
|
23
|
+
handlers,
|
|
24
|
+
async call(method: string, params: unknown) {
|
|
25
|
+
const h = handlers.get(method);
|
|
26
|
+
if (!h) throw new Error(`No handler for ${method}`);
|
|
27
|
+
return h(params);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const DESKTOP_HISTORY_PATH = join(homedir(), ".pi-agent", "chat-history-desktop.json");
|
|
33
|
+
|
|
34
|
+
describe("Chat Handler 并发安全", () => {
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
vi.resetModules();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterEach(async () => {
|
|
40
|
+
if (existsSync(DESKTOP_HISTORY_PATH)) {
|
|
41
|
+
await unlink(DESKTOP_HISTORY_PATH).catch(() => {});
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("两个独立的 RPCServer 应注册各自的 handler", async () => {
|
|
46
|
+
const { register } = await import("../chat");
|
|
47
|
+
const serverA = createMockServer();
|
|
48
|
+
const serverB = createMockServer();
|
|
49
|
+
|
|
50
|
+
register(serverA as any, { platform: "desktop" });
|
|
51
|
+
register(serverB as any, { platform: "web" });
|
|
52
|
+
|
|
53
|
+
expect(serverA.register).toHaveBeenCalledWith("chat.list", expect.any(Function));
|
|
54
|
+
expect(serverA.register).toHaveBeenCalledWith("chat.send", expect.any(Function));
|
|
55
|
+
expect(serverB.register).toHaveBeenCalledWith("chat.list", expect.any(Function));
|
|
56
|
+
expect(serverB.register).toHaveBeenCalledWith("chat.send", expect.any(Function));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("server A 的消息不影响 server B 的消息列表", async () => {
|
|
60
|
+
const { register } = await import("../chat");
|
|
61
|
+
const serverA = createMockServer();
|
|
62
|
+
const serverB = createMockServer();
|
|
63
|
+
|
|
64
|
+
register(serverA as any, { platform: "desktop" });
|
|
65
|
+
register(serverB as any, { platform: "web" });
|
|
66
|
+
|
|
67
|
+
await serverA.call("chat.send", { content: "hello from desktop" });
|
|
68
|
+
await serverB.call("chat.send", { content: "hello from web" });
|
|
69
|
+
|
|
70
|
+
const resultA = (await serverA.call("chat.list", { limit: 50 })) as {
|
|
71
|
+
messages: { content: string }[];
|
|
72
|
+
};
|
|
73
|
+
const resultB = (await serverB.call("chat.list", { limit: 50 })) as {
|
|
74
|
+
messages: { content: string }[];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const desktopContents = resultA.messages.map((m) => m.content);
|
|
78
|
+
const webContents = resultB.messages.map((m) => m.content);
|
|
79
|
+
|
|
80
|
+
expect(desktopContents).toContain("hello from desktop");
|
|
81
|
+
expect(webContents).toContain("hello from web");
|
|
82
|
+
expect(desktopContents).not.toContain("hello from web");
|
|
83
|
+
expect(webContents).not.toContain("hello from desktop");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("同一 server 内的消息按顺序写入", async () => {
|
|
87
|
+
const { register } = await import("../chat");
|
|
88
|
+
const server = createMockServer();
|
|
89
|
+
register(server as any, { platform: "desktop" });
|
|
90
|
+
|
|
91
|
+
await server.call("chat.send", { content: "msg1" });
|
|
92
|
+
await server.call("chat.send", { content: "msg2" });
|
|
93
|
+
await server.call("chat.send", { content: "msg3" });
|
|
94
|
+
|
|
95
|
+
const result = (await server.call("chat.list", { limit: 50 })) as {
|
|
96
|
+
messages: { role: string; content: string }[];
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const userMsgs = result.messages.filter((m) => m.role === "user").map((m) => m.content);
|
|
100
|
+
expect(userMsgs).toEqual(["msg1", "msg2", "msg3"]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("存储路径应包含平台标识(desktop/web)或唯一 ID", async () => {
|
|
104
|
+
const { getStoragePathFor } = await import("../chat");
|
|
105
|
+
const desktopPath = getStoragePathFor("desktop");
|
|
106
|
+
const webPath = getStoragePathFor("web");
|
|
107
|
+
|
|
108
|
+
expect(desktopPath).toContain("desktop");
|
|
109
|
+
expect(webPath).not.toBe(desktopPath);
|
|
110
|
+
expect(webPath).toMatch(/web/);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("desktop 使用固定路径保留历史", async () => {
|
|
114
|
+
const { getStoragePathFor } = await import("../chat");
|
|
115
|
+
const path1 = getStoragePathFor("desktop");
|
|
116
|
+
const path2 = getStoragePathFor("desktop");
|
|
117
|
+
expect(path1).toBe(path2);
|
|
118
|
+
expect(path1).toContain("chat-history-desktop.json");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("web 每次生成不同的路径", async () => {
|
|
122
|
+
const { getStoragePathFor } = await import("../chat");
|
|
123
|
+
const path1 = getStoragePathFor("web");
|
|
124
|
+
const path2 = getStoragePathFor("web");
|
|
125
|
+
expect(path1).not.toBe(path2);
|
|
126
|
+
});
|
|
127
|
+
});
|