@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,437 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP route handlers for the web gateway.
|
|
3
|
+
* Handles: /health, /info/{path}, /file/{path}, /file/upload
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { IncomingMessage, ServerResponse } from "http";
|
|
7
|
+
import { stat, readFile, writeFile, mkdir } from "fs/promises";
|
|
8
|
+
import { existsSync } from "fs";
|
|
9
|
+
import { extname, basename, dirname, resolve, join } from "path";
|
|
10
|
+
import { tmpdir } from "os";
|
|
11
|
+
import { createLogger } from "./lib/logger";
|
|
12
|
+
import { verifyToken as verifySseToken } from "../gateway/sse-transport";
|
|
13
|
+
import type { SseHandler } from "../gateway/sse-transport";
|
|
14
|
+
|
|
15
|
+
const log = createLogger("gateway");
|
|
16
|
+
|
|
17
|
+
const MIME_TYPES: Record<string, string> = {
|
|
18
|
+
".html": "text/html",
|
|
19
|
+
".css": "text/css",
|
|
20
|
+
".js": "application/javascript",
|
|
21
|
+
".json": "application/json",
|
|
22
|
+
".png": "image/png",
|
|
23
|
+
".jpg": "image/jpeg",
|
|
24
|
+
".jpeg": "image/jpeg",
|
|
25
|
+
".gif": "image/gif",
|
|
26
|
+
".svg": "image/svg+xml",
|
|
27
|
+
".ico": "image/x-icon",
|
|
28
|
+
".txt": "text/plain",
|
|
29
|
+
".md": "text/markdown",
|
|
30
|
+
".ts": "text/plain",
|
|
31
|
+
".tsx": "text/plain",
|
|
32
|
+
".py": "text/plain",
|
|
33
|
+
".pdf": "application/pdf",
|
|
34
|
+
".zip": "application/zip",
|
|
35
|
+
".mp4": "video/mp4",
|
|
36
|
+
".mp3": "audio/mpeg",
|
|
37
|
+
".wav": "audio/wav",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const ALLOWED_ROOTS = [resolve(process.cwd())];
|
|
41
|
+
function isPathAllowed(requestedPath: string): boolean {
|
|
42
|
+
const resolved = resolve(requestedPath);
|
|
43
|
+
return ALLOWED_ROOTS.some((root) => resolved === root || resolved.startsWith(root + "/"));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function verifyToken(req: IncomingMessage, authToken: string): boolean {
|
|
47
|
+
return verifySseToken(req, authToken);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface HttpRouteDeps {
|
|
51
|
+
config: {
|
|
52
|
+
readonly port: number;
|
|
53
|
+
readonly authToken: string;
|
|
54
|
+
readonly maxUploadSize: number;
|
|
55
|
+
readonly corsOrigin: string;
|
|
56
|
+
};
|
|
57
|
+
getSseClientCount: () => number;
|
|
58
|
+
sse: SseHandler;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function createHttpHandler(
|
|
62
|
+
deps: HttpRouteDeps
|
|
63
|
+
): (req: IncomingMessage, res: ServerResponse) => void {
|
|
64
|
+
const { config: cfg, getSseClientCount, sse } = deps;
|
|
65
|
+
|
|
66
|
+
return async (req, res) => {
|
|
67
|
+
res.setHeader("Access-Control-Allow-Origin", cfg.corsOrigin);
|
|
68
|
+
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
69
|
+
res.setHeader("Access-Control-Allow-Headers", "Authorization, Range, Content-Type");
|
|
70
|
+
if (req.method === "OPTIONS") {
|
|
71
|
+
res.writeHead(204).end();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!req.url) {
|
|
76
|
+
res.writeHead(400).end();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const url = new URL(req.url, "http://localhost");
|
|
81
|
+
|
|
82
|
+
// ── SSE + RPC routes (require auth) ──
|
|
83
|
+
if (url.pathname === "/api/events" && req.method === "GET") {
|
|
84
|
+
if (!verifyToken(req, cfg.authToken)) {
|
|
85
|
+
res.writeHead(401, { "Content-Type": "application/json" });
|
|
86
|
+
res.end(JSON.stringify({ error: "Unauthorized" }));
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
sse.handleSseConnect(req, res);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (url.pathname === "/api/rpc" && req.method === "POST") {
|
|
94
|
+
if (!verifyToken(req, cfg.authToken)) {
|
|
95
|
+
res.writeHead(401, { "Content-Type": "application/json" });
|
|
96
|
+
res.end(JSON.stringify({ error: "Unauthorized" }));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
await sse.handleRpcPost(req, res);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ── 扩展安装引导端点(用户视角,不暴露 CDP/tunnel 概念)──
|
|
104
|
+
if (url.pathname === "/api/create-browser" && req.method === "POST") {
|
|
105
|
+
if (!verifyToken(req, cfg.authToken)) {
|
|
106
|
+
res.writeHead(401, { "Content-Type": "application/json" });
|
|
107
|
+
res.end(JSON.stringify({ error: "Unauthorized" }));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
await handleCreateBrowser(req, res);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (url.pathname === "/api/pack-extension" && req.method === "POST") {
|
|
115
|
+
if (!verifyToken(req, cfg.authToken)) {
|
|
116
|
+
res.writeHead(401, { "Content-Type": "application/json" });
|
|
117
|
+
res.end(JSON.stringify({ error: "Unauthorized" }));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
await handlePackExtension(req, res);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Browser Agent: resolve asset path from ID (stored as JSON index)
|
|
125
|
+
const assetMatch = url.pathname.match(/^\/api\/assets\/([\w]+)\/download$/);
|
|
126
|
+
if (assetMatch) {
|
|
127
|
+
await handleAssetDownload(assetMatch[1] || '', res);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const assetMetaMatch = url.pathname.match(/^\/api\/assets\/([\w]+)$/);
|
|
131
|
+
if (assetMetaMatch) {
|
|
132
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
133
|
+
res.end(JSON.stringify({ id: assetMetaMatch[1], note: "Use download endpoint for file" }));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (url.pathname === "/health") {
|
|
138
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
139
|
+
res.end(JSON.stringify({ status: "ok", clients: getSseClientCount() }));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!verifyToken(req, cfg.authToken)) {
|
|
144
|
+
log.warn("Auth failed", { path: url.pathname });
|
|
145
|
+
res.writeHead(401, { "Content-Type": "application/json" });
|
|
146
|
+
res.end(JSON.stringify({ error: "Unauthorized" }));
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (url.pathname.startsWith("/info/")) {
|
|
151
|
+
await handleFileInfo(url.pathname.slice(6), res);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (url.pathname.startsWith("/file/")) {
|
|
156
|
+
if (url.pathname === "/file/upload" && req.method === "POST") {
|
|
157
|
+
await handleFileUpload(req, url.searchParams.get("path"), res, cfg.maxUploadSize);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
await handleFileContent(url.pathname.slice(6), req, res);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
res.writeHead(404);
|
|
165
|
+
res.end();
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function handleFileInfo(encodedPath: string, res: ServerResponse): Promise<void> {
|
|
170
|
+
const filePath = decodeURIComponent(encodedPath);
|
|
171
|
+
if (!isPathAllowed(filePath)) {
|
|
172
|
+
res.writeHead(403, { "Content-Type": "application/json" });
|
|
173
|
+
res.end(JSON.stringify({ error: "Path not allowed" }));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
const s = await stat(filePath);
|
|
178
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
179
|
+
res.end(
|
|
180
|
+
JSON.stringify({
|
|
181
|
+
name: basename(filePath),
|
|
182
|
+
path: filePath,
|
|
183
|
+
size: s.size,
|
|
184
|
+
isDirectory: s.isDirectory(),
|
|
185
|
+
modified: s.mtime.toISOString(),
|
|
186
|
+
mimeType: s.isFile()
|
|
187
|
+
? MIME_TYPES[extname(filePath)] || "application/octet-stream"
|
|
188
|
+
: undefined,
|
|
189
|
+
})
|
|
190
|
+
);
|
|
191
|
+
} catch {
|
|
192
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
193
|
+
res.end(JSON.stringify({ error: "File not found" }));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function handleFileContent(
|
|
198
|
+
encodedPath: string,
|
|
199
|
+
req: IncomingMessage,
|
|
200
|
+
res: ServerResponse
|
|
201
|
+
): Promise<void> {
|
|
202
|
+
const filePath = decodeURIComponent(encodedPath);
|
|
203
|
+
if (!isPathAllowed(filePath)) {
|
|
204
|
+
res.writeHead(403, { "Content-Type": "application/json" });
|
|
205
|
+
res.end(JSON.stringify({ error: "Path not allowed" }));
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
if (!existsSync(filePath)) {
|
|
210
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
211
|
+
res.end(JSON.stringify({ error: "File not found" }));
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const s = await stat(filePath);
|
|
215
|
+
const mimeType = MIME_TYPES[extname(filePath)] || "application/octet-stream";
|
|
216
|
+
|
|
217
|
+
const range = req.headers["range"];
|
|
218
|
+
if (range) {
|
|
219
|
+
const parts = range.replace(/bytes=/, "").split("-");
|
|
220
|
+
const start = parseInt(parts[0]!, 10);
|
|
221
|
+
const end = parts[1] ? parseInt(parts[1], 10) : s.size - 1;
|
|
222
|
+
const chunkSize = end - start + 1;
|
|
223
|
+
|
|
224
|
+
res.writeHead(206, {
|
|
225
|
+
"Content-Range": `bytes ${start}-${end}/${s.size}`,
|
|
226
|
+
"Accept-Ranges": "bytes",
|
|
227
|
+
"Content-Length": chunkSize,
|
|
228
|
+
"Content-Type": mimeType,
|
|
229
|
+
});
|
|
230
|
+
const buffer = await readFile(filePath);
|
|
231
|
+
res.end(buffer.subarray(start, end + 1));
|
|
232
|
+
} else {
|
|
233
|
+
res.writeHead(200, {
|
|
234
|
+
"Content-Length": s.size,
|
|
235
|
+
"Content-Type": mimeType,
|
|
236
|
+
"Accept-Ranges": "bytes",
|
|
237
|
+
});
|
|
238
|
+
const buffer = await readFile(filePath);
|
|
239
|
+
res.end(buffer);
|
|
240
|
+
}
|
|
241
|
+
log.info("File served", { path: filePath });
|
|
242
|
+
} catch {
|
|
243
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
244
|
+
res.end(JSON.stringify({ error: "Failed to read file" }));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async function handleFileUpload(
|
|
249
|
+
req: IncomingMessage,
|
|
250
|
+
destPath: string | null,
|
|
251
|
+
res: ServerResponse,
|
|
252
|
+
maxUploadSize: number
|
|
253
|
+
): Promise<void> {
|
|
254
|
+
if (!destPath) {
|
|
255
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
256
|
+
res.end(JSON.stringify({ error: "Missing path parameter" }));
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (!isPathAllowed(destPath)) {
|
|
260
|
+
res.writeHead(403, { "Content-Type": "application/json" });
|
|
261
|
+
res.end(JSON.stringify({ error: "Path not allowed" }));
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const contentLength = parseInt(req.headers["content-length"] || "0", 10);
|
|
265
|
+
if (contentLength > maxUploadSize) {
|
|
266
|
+
res.writeHead(413, { "Content-Type": "application/json" });
|
|
267
|
+
res.end(JSON.stringify({ error: `File too large, max ${maxUploadSize / 1024 / 1024}MB` }));
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
const chunks: Uint8Array[] = [];
|
|
272
|
+
for await (const chunk of req) {
|
|
273
|
+
const bytes =
|
|
274
|
+
typeof chunk === "string"
|
|
275
|
+
? new TextEncoder().encode(chunk)
|
|
276
|
+
: new Uint8Array(chunk as ArrayBuffer);
|
|
277
|
+
chunks.push(bytes);
|
|
278
|
+
}
|
|
279
|
+
const body = Buffer.concat(chunks as any);
|
|
280
|
+
await mkdir(dirname(destPath), { recursive: true });
|
|
281
|
+
await writeFile(destPath, body as any);
|
|
282
|
+
log.info("File uploaded", { path: destPath, size: body.length });
|
|
283
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
284
|
+
res.end(JSON.stringify({ ok: true, path: destPath, size: body.length }));
|
|
285
|
+
} catch (err) {
|
|
286
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
287
|
+
res.end(JSON.stringify({ error: err instanceof Error ? err.message : "Upload failed" }));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* 查找并下载资源文件(搜索 os.tmpdir/browser-agent-assets/)
|
|
293
|
+
*/
|
|
294
|
+
async function handleAssetDownload(assetId: string, res: ServerResponse): Promise<void> {
|
|
295
|
+
try {
|
|
296
|
+
const assetsDir = join(tmpdir(), "browser-agent-assets");
|
|
297
|
+
if (!existsSync(assetsDir)) {
|
|
298
|
+
res.writeHead(404).end();
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const { readdir, readFile: readFileP } = await import("fs/promises");
|
|
302
|
+
const sessionDirs = await readdir(assetsDir);
|
|
303
|
+
for (const sessionDir of sessionDirs) {
|
|
304
|
+
const sessionPath = join(assetsDir, sessionDir);
|
|
305
|
+
const entries = await readdir(sessionPath);
|
|
306
|
+
for (const entry of entries) {
|
|
307
|
+
if (entry.includes(assetId)) {
|
|
308
|
+
const filePath = join(sessionPath, entry);
|
|
309
|
+
const s = await stat(filePath);
|
|
310
|
+
const mimeType = MIME_TYPES[extname(filePath)] || "application/octet-stream";
|
|
311
|
+
const buffer = await readFileP(filePath);
|
|
312
|
+
res.writeHead(200, {
|
|
313
|
+
"Content-Type": mimeType,
|
|
314
|
+
"Content-Length": s.size,
|
|
315
|
+
"Content-Disposition": `attachment; filename="${encodeURIComponent(entry)}"`,
|
|
316
|
+
});
|
|
317
|
+
res.end(buffer);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
res.writeHead(404).end();
|
|
323
|
+
} catch {
|
|
324
|
+
res.writeHead(500).end();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ===== 扩展安装引导(用户视角)=====
|
|
329
|
+
|
|
330
|
+
const CDP_TUNNEL_API = process.env.CLOUD_PROXY_URL || process.env.CDP_ENDPOINT || "http://localhost:9221";
|
|
331
|
+
const CDP_TUNNEL_EXT = process.env.CDP_TUNNEL_EXT || "";
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* 创建连接 Key — 调用 cdp-tunnel admin API
|
|
335
|
+
* 用户视角:"创建连接",实际是生成一个 cdp-tunnel 的 plugin key
|
|
336
|
+
*/
|
|
337
|
+
async function handleCreateBrowser(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
338
|
+
try {
|
|
339
|
+
let body = "";
|
|
340
|
+
for await (const chunk of req) {
|
|
341
|
+
body += typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8");
|
|
342
|
+
}
|
|
343
|
+
const parsed = body ? JSON.parse(body) : {};
|
|
344
|
+
const name = parsed.name || `用户-${Date.now().toString(36)}`;
|
|
345
|
+
|
|
346
|
+
const apiRes = await fetch(`${CDP_TUNNEL_API}/admin/api/keys`, {
|
|
347
|
+
method: "POST",
|
|
348
|
+
headers: { "Content-Type": "application/json" },
|
|
349
|
+
body: JSON.stringify({ name }),
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
if (!apiRes.ok) {
|
|
353
|
+
const errText = await apiRes.text();
|
|
354
|
+
log.error("create-browser failed", { status: apiRes.status, body: errText });
|
|
355
|
+
res.writeHead(502, { "Content-Type": "application/json" });
|
|
356
|
+
res.end(JSON.stringify({ error: "无法创建连接,请确认 cdp-tunnel 服务正在运行" }));
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const data = await apiRes.json() as { key: string; pluginUrl?: string };
|
|
361
|
+
const wsUrl = CDP_TUNNEL_API.replace(/^https/, "wss").replace(/^http/, "ws");
|
|
362
|
+
const pluginUrl = `${wsUrl}/plugin?key=${data.key}`;
|
|
363
|
+
|
|
364
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
365
|
+
res.end(JSON.stringify({ key: data.key, pluginUrl }));
|
|
366
|
+
} catch (err) {
|
|
367
|
+
log.error("create-browser error", { error: err instanceof Error ? err.message : String(err) });
|
|
368
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
369
|
+
res.end(JSON.stringify({ error: "创建连接失败,cdp-tunnel 服务可能未运行" }));
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* 打包 Chrome 扩展 ZIP — 将 Key 注入扩展配置后打包
|
|
375
|
+
* 用户视角:"下载扩展",实际是生成一个配置好连接地址的 ZIP
|
|
376
|
+
*/
|
|
377
|
+
async function handlePackExtension(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
378
|
+
try {
|
|
379
|
+
let body = "";
|
|
380
|
+
for await (const chunk of req) {
|
|
381
|
+
body += typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8");
|
|
382
|
+
}
|
|
383
|
+
const { key } = JSON.parse(body);
|
|
384
|
+
if (!key) {
|
|
385
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
386
|
+
res.end(JSON.stringify({ error: "缺少 key 参数" }));
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (!CDP_TUNNEL_EXT || !existsSync(CDP_TUNNEL_EXT)) {
|
|
391
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
392
|
+
res.end(JSON.stringify({
|
|
393
|
+
error: "扩展源码未找到,请设置 CDP_TUNNEL_EXT 环境变量指向 cdp-tunnel 扩展目录",
|
|
394
|
+
}));
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const { execSync } = await import("child_process");
|
|
399
|
+
const tmpDir = join(tmpdir(), `cdp-ext-${Date.now()}`);
|
|
400
|
+
const wsUrl = CDP_TUNNEL_API.replace(/^https/, "wss").replace(/^http/, "ws");
|
|
401
|
+
const pluginUrl = `${wsUrl}/plugin?key=${key}`;
|
|
402
|
+
|
|
403
|
+
// 复制扩展源码到临时目录
|
|
404
|
+
execSync(`cp -r "${CDP_TUNNEL_EXT}" "${tmpDir}"`);
|
|
405
|
+
|
|
406
|
+
// 注入连接地址到 config.js
|
|
407
|
+
const configPath = join(tmpDir, "utils", "config.js");
|
|
408
|
+
if (existsSync(configPath)) {
|
|
409
|
+
const { readFile, writeFile } = await import("fs/promises");
|
|
410
|
+
let config = await readFile(configPath, "utf8");
|
|
411
|
+
config = config.replace(/WS_URL:\s*'[^']*'/, `WS_URL: '${pluginUrl}'`);
|
|
412
|
+
await writeFile(configPath, config);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// 打包 ZIP
|
|
416
|
+
const zipPath = `${tmpDir}.zip`;
|
|
417
|
+
execSync(`cd "${tmpdir()}" && zip -rq "${basename(zipPath)}" "${basename(tmpDir)}/"`);
|
|
418
|
+
|
|
419
|
+
// 读取并返回 ZIP
|
|
420
|
+
const { readFile: readZip } = await import("fs/promises");
|
|
421
|
+
const zipBuffer = await readZip(zipPath);
|
|
422
|
+
|
|
423
|
+
res.writeHead(200, {
|
|
424
|
+
"Content-Type": "application/zip",
|
|
425
|
+
"Content-Disposition": `attachment; filename="browser-agent-extension.zip"`,
|
|
426
|
+
"Content-Length": zipBuffer.length,
|
|
427
|
+
});
|
|
428
|
+
res.end(zipBuffer);
|
|
429
|
+
|
|
430
|
+
// 清理临时文件
|
|
431
|
+
execSync(`rm -rf "${tmpDir}" "${zipPath}"`);
|
|
432
|
+
} catch (err) {
|
|
433
|
+
log.error("pack-extension error", { error: err instanceof Error ? err.message : String(err) });
|
|
434
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
435
|
+
res.end(JSON.stringify({ error: "打包扩展失败: " + (err instanceof Error ? err.message : String(err)) }));
|
|
436
|
+
}
|
|
437
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { mkdirSync, existsSync, readdirSync, readFileSync, writeFileSync, rmSync, utimesSync } from "fs";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
|
|
5
|
+
const TEST_LOG_DIR = join(process.cwd(), "test-logs");
|
|
6
|
+
|
|
7
|
+
describe("Logger", () => {
|
|
8
|
+
beforeEach(async () => {
|
|
9
|
+
if (existsSync(TEST_LOG_DIR)) {
|
|
10
|
+
rmSync(TEST_LOG_DIR, { recursive: true, force: true });
|
|
11
|
+
}
|
|
12
|
+
mkdirSync(TEST_LOG_DIR, { recursive: true });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
if (existsSync(TEST_LOG_DIR)) {
|
|
17
|
+
rmSync(TEST_LOG_DIR, { recursive: true, force: true });
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should write logs asynchronously", async () => {
|
|
22
|
+
const { createLogger, configureLogDir, flushLogs } = await import("../logger");
|
|
23
|
+
await configureLogDir(TEST_LOG_DIR);
|
|
24
|
+
|
|
25
|
+
const logger = createLogger("test");
|
|
26
|
+
logger.info("test message");
|
|
27
|
+
|
|
28
|
+
await flushLogs();
|
|
29
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
30
|
+
|
|
31
|
+
const files = readdirSync(TEST_LOG_DIR);
|
|
32
|
+
expect(files.length).toBeGreaterThan(0);
|
|
33
|
+
|
|
34
|
+
const content = readFileSync(join(TEST_LOG_DIR, files[0]), "utf-8");
|
|
35
|
+
expect(content).toContain("test message");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should support multiple log levels", async () => {
|
|
39
|
+
const { createLogger, configureLogDir, flushLogs } = await import("../logger");
|
|
40
|
+
await configureLogDir(TEST_LOG_DIR);
|
|
41
|
+
|
|
42
|
+
const logger = createLogger("test");
|
|
43
|
+
logger.debug("debug msg");
|
|
44
|
+
logger.info("info msg");
|
|
45
|
+
logger.warn("warn msg");
|
|
46
|
+
logger.error("error msg");
|
|
47
|
+
|
|
48
|
+
await flushLogs();
|
|
49
|
+
|
|
50
|
+
const files = readdirSync(TEST_LOG_DIR);
|
|
51
|
+
const content = readFileSync(join(TEST_LOG_DIR, files[0]), "utf-8");
|
|
52
|
+
expect(content).toContain("info msg");
|
|
53
|
+
expect(content).toContain("warn msg");
|
|
54
|
+
expect(content).toContain("error msg");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should clean up old log files based on maxAgeDays", async () => {
|
|
58
|
+
const { configureLogDir } = await import("../logger");
|
|
59
|
+
|
|
60
|
+
const oldDate = new Date(Date.now() - 10 * 86400000).toISOString().slice(0, 10);
|
|
61
|
+
const oldPath = join(TEST_LOG_DIR, `${oldDate}.log`);
|
|
62
|
+
writeFileSync(oldPath, "old log");
|
|
63
|
+
const oldTime = new Date(Date.now() - 10 * 86400000);
|
|
64
|
+
utimesSync(oldPath, oldTime, oldTime);
|
|
65
|
+
|
|
66
|
+
const todayFile = new Date().toISOString().slice(0, 10);
|
|
67
|
+
writeFileSync(join(TEST_LOG_DIR, `${todayFile}.log`), "today log");
|
|
68
|
+
|
|
69
|
+
await configureLogDir(TEST_LOG_DIR, { maxAgeDays: 5 });
|
|
70
|
+
|
|
71
|
+
const files = readdirSync(TEST_LOG_DIR);
|
|
72
|
+
expect(files.find((f) => f.includes(oldDate))).toBeUndefined();
|
|
73
|
+
expect(files.find((f) => f.includes(todayFile))).toBeDefined();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should flush all pending writes", async () => {
|
|
77
|
+
const { createLogger, configureLogDir, flushLogs } = await import("../logger");
|
|
78
|
+
await configureLogDir(TEST_LOG_DIR);
|
|
79
|
+
|
|
80
|
+
const logger = createLogger("test");
|
|
81
|
+
for (let i = 0; i < 100; i++) {
|
|
82
|
+
logger.info(`msg ${i}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
await flushLogs();
|
|
86
|
+
|
|
87
|
+
const files = readdirSync(TEST_LOG_DIR);
|
|
88
|
+
const content = readFileSync(join(TEST_LOG_DIR, files[0]), "utf-8");
|
|
89
|
+
expect(content).toContain("msg 0");
|
|
90
|
+
expect(content).toContain("msg 99");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { validatePath, isRpcPathAllowed, setAllowedRoots } from "../path-security";
|
|
3
|
+
|
|
4
|
+
describe("path-security", () => {
|
|
5
|
+
const originalCwd = process.cwd();
|
|
6
|
+
const originalEnv = process.env.NODE_ENV;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
setAllowedRoots([process.cwd()]);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
process.env.NODE_ENV = originalEnv;
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe("isRpcPathAllowed", () => {
|
|
17
|
+
it("should allow paths within project root", () => {
|
|
18
|
+
expect(isRpcPathAllowed(`${originalCwd}/src/main.ts`)).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should allow project root itself", () => {
|
|
22
|
+
expect(isRpcPathAllowed(originalCwd)).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should reject paths outside project root", () => {
|
|
26
|
+
expect(isRpcPathAllowed("/etc/passwd")).toBe(false);
|
|
27
|
+
expect(isRpcPathAllowed("/etc/shadow")).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should reject path traversal attempts", () => {
|
|
31
|
+
expect(isRpcPathAllowed(`${originalCwd}/../../../etc/passwd`)).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should reject paths with encoded traversal", () => {
|
|
35
|
+
expect(isRpcPathAllowed(`${originalCwd}/%2e%2e/etc/passwd`)).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should handle relative paths", () => {
|
|
39
|
+
expect(isRpcPathAllowed("./src/main.ts")).toBe(true);
|
|
40
|
+
expect(isRpcPathAllowed("../../etc/passwd")).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should handle null bytes", () => {
|
|
44
|
+
expect(isRpcPathAllowed(`${originalCwd}/file.txt\0/etc/passwd`)).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should work with multiple allowed roots", () => {
|
|
48
|
+
setAllowedRoots([originalCwd, "/tmp/test"]);
|
|
49
|
+
expect(isRpcPathAllowed("/tmp/test/file.txt")).toBe(true);
|
|
50
|
+
expect(isRpcPathAllowed("/tmp/other/file.txt")).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("validatePath", () => {
|
|
55
|
+
it("should return resolved path for allowed paths", () => {
|
|
56
|
+
const result = validatePath("./src/main.ts");
|
|
57
|
+
expect(result).toContain("src/main.ts");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should throw for paths outside allowed roots", () => {
|
|
61
|
+
expect(() => validatePath("/etc/passwd")).toThrow("Access denied");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should throw for path traversal attempts", () => {
|
|
65
|
+
expect(() => validatePath("../../../etc/passwd")).toThrow("Access denied");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should throw for null byte injection", () => {
|
|
69
|
+
expect(() => validatePath("file.txt\0/evil")).toThrow("Access denied");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should normalize the returned path", () => {
|
|
73
|
+
const result = validatePath("./src/../src/./main.ts");
|
|
74
|
+
expect(result).toBe(`${originalCwd}/src/main.ts`);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|