@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.
Files changed (349) hide show
  1. package/package.json +5 -4
  2. package/src/__tests__/cli.test.ts +2 -0
  3. package/src/__tests__/copy.test.ts +110 -122
  4. package/src/__tests__/create-args.test.ts +113 -0
  5. package/src/__tests__/templates.test.ts +29 -3
  6. package/src/__tests__/update.test.ts +129 -0
  7. package/src/cli.ts +30 -32
  8. package/src/commands/create.ts +83 -24
  9. package/src/commands/update.ts +174 -0
  10. package/src/commands/workspace.ts +26 -6
  11. package/src/lib/copy.ts +233 -93
  12. package/src/lib/templates.ts +12 -0
  13. package/templates/agent/.husky/commit-msg +4 -0
  14. package/templates/agent/.husky/pre-commit +11 -0
  15. package/templates/agent/.husky/pre-push +6 -0
  16. package/templates/agent/.prettierignore +6 -0
  17. package/templates/agent/.prettierrc +9 -0
  18. package/templates/agent/CHANGELOG.md +15 -0
  19. package/templates/agent/commitlint.config.js +8 -0
  20. package/templates/agent/package.json +19 -4
  21. package/templates/agent/src/gateway/__tests__/ws-handler-token.test.ts +53 -39
  22. package/templates/agent/src/mainview/App.tsx +14 -12
  23. package/templates/agent/src/mainview/__tests__/hooks/use-rpc-init.test.ts +194 -190
  24. package/templates/agent/src/mainview/components/bash/BashPanel.tsx +210 -189
  25. package/templates/agent/src/mainview/components/chat/ChatPanel.tsx +45 -20
  26. package/templates/agent/src/mainview/components/common/ThemeToggle.tsx +41 -21
  27. package/templates/agent/src/mainview/components/debug/DebugPanel.tsx +181 -114
  28. package/templates/agent/src/mainview/components/debug/__tests__/DebugPanel.test.tsx +80 -80
  29. package/templates/agent/src/mainview/components/diff/__tests__/DiffViewerPanel.test.tsx +61 -58
  30. package/templates/agent/src/mainview/components/feed/FeedPanel.tsx +43 -41
  31. package/templates/agent/src/mainview/components/file-preview/FilePreviewOverlay.tsx +69 -50
  32. package/templates/agent/src/mainview/components/file-preview/VirtualizedCodeView.tsx +31 -18
  33. package/templates/agent/src/mainview/components/git/GitPanel.tsx +731 -490
  34. package/templates/agent/src/mainview/components/git/__tests__/GitPanel.test.tsx +157 -139
  35. package/templates/agent/src/mainview/components/layout/AppLayout.tsx +210 -161
  36. package/templates/agent/src/mainview/components/layout/__tests__/AppLayout.test.tsx +130 -122
  37. package/templates/agent/src/mainview/components/rules/RulesPanel.tsx +119 -109
  38. package/templates/agent/src/mainview/components/search/SearchPanel.tsx +123 -117
  39. package/templates/agent/src/mainview/components/search/__tests__/SearchPanel.test.tsx +64 -44
  40. package/templates/agent/src/mainview/components/sidebar/__tests__/PinButton.test.tsx +38 -38
  41. package/templates/agent/src/mainview/components/todo/TodoPanel.tsx +40 -38
  42. package/templates/agent/src/mainview/components/todo/__tests__/TodoPanel.test.tsx +72 -72
  43. package/templates/agent/src/mainview/lib/i18n/locales/en.json +175 -155
  44. package/templates/agent/src/mainview/lib/i18n/locales/zh.json +173 -155
  45. package/templates/agent/src/mainview/stores/use-app-store.ts +96 -86
  46. package/templates/agent/src/mainview/stores/use-explorer-store.ts +292 -275
  47. package/templates/agent/src/mainview/types/index.ts +23 -22
  48. package/templates/agent/src/mainview/utils/file-icon.tsx +17 -21
  49. package/templates/agent/src/shared/handlers/chat.ts +53 -53
  50. package/templates/agent/src/shared/handlers/debug.ts +13 -3
  51. package/templates/browser-agent/.env.example +19 -0
  52. package/templates/browser-agent/.husky/commit-msg +4 -0
  53. package/templates/browser-agent/.husky/pre-commit +11 -0
  54. package/templates/browser-agent/.husky/pre-push +6 -0
  55. package/templates/browser-agent/.prettierignore +6 -0
  56. package/templates/browser-agent/.prettierrc +9 -0
  57. package/templates/browser-agent/AGENTS.md +396 -0
  58. package/templates/browser-agent/CHANGELOG.md +15 -0
  59. package/templates/browser-agent/LICENSE +21 -0
  60. package/templates/browser-agent/README.md +103 -0
  61. package/templates/browser-agent/commitlint.config.js +8 -0
  62. package/templates/browser-agent/electrobun.config.ts +27 -0
  63. package/templates/browser-agent/electron/main.js +46 -0
  64. package/templates/browser-agent/electron/preload.js +5 -0
  65. package/templates/browser-agent/electron-builder.json +40 -0
  66. package/templates/browser-agent/eslint.config.mjs +79 -0
  67. package/templates/browser-agent/llms.txt +24 -0
  68. package/templates/browser-agent/package.json +140 -0
  69. package/templates/browser-agent/postcss.config.js +6 -0
  70. package/templates/browser-agent/scripts/dev.ts +138 -0
  71. package/templates/browser-agent/src/__tests__/hybrid-mode.test.ts +126 -0
  72. package/templates/browser-agent/src/__tests__/server-config-security.test.ts +55 -0
  73. package/templates/browser-agent/src/__tests__/server-config.test.ts +59 -0
  74. package/templates/browser-agent/src/bun/index.ts +130 -0
  75. package/templates/browser-agent/src/bun/three.d.ts +1 -0
  76. package/templates/browser-agent/src/gateway/http-routes.ts +1 -0
  77. package/templates/browser-agent/src/gateway/ipc-transport.ts +68 -0
  78. package/templates/browser-agent/src/gateway/sse-transport.ts +221 -0
  79. package/templates/browser-agent/src/mainview/App.tsx +45 -0
  80. package/templates/browser-agent/src/mainview/__tests__/hooks/use-rpc-init.test.ts +202 -0
  81. package/templates/browser-agent/src/mainview/__tests__/hooks/use-sidebar-resize.test.ts +127 -0
  82. package/templates/browser-agent/src/mainview/__tests__/i18n/i18n.test.ts +49 -0
  83. package/templates/browser-agent/src/mainview/__tests__/setup.ts +40 -0
  84. package/templates/browser-agent/src/mainview/__tests__/stores/use-chat-store.test.ts +73 -0
  85. package/templates/browser-agent/src/mainview/__tests__/stores/use-sidebar-store.test.ts +61 -0
  86. package/templates/browser-agent/src/mainview/__tests__/theme-variables.test.ts +36 -0
  87. package/templates/browser-agent/src/mainview/components/assets/AssetsPanel.tsx +139 -0
  88. package/templates/browser-agent/src/mainview/components/chat/ChatPanel.tsx +219 -0
  89. package/templates/browser-agent/src/mainview/components/chat/CommandBar.tsx +184 -0
  90. package/templates/browser-agent/src/mainview/components/chat/MessageBubble.tsx +249 -0
  91. package/templates/browser-agent/src/mainview/components/chat/ToolPicker.tsx +115 -0
  92. package/templates/browser-agent/src/mainview/components/common/ErrorBoundary.tsx +1 -0
  93. package/templates/browser-agent/src/mainview/components/common/LanguageSwitcher.tsx +15 -0
  94. package/templates/browser-agent/src/mainview/components/common/ThemeToggle.tsx +45 -0
  95. package/templates/browser-agent/src/mainview/components/common/__tests__/ErrorBoundary.test.tsx +74 -0
  96. package/templates/browser-agent/src/mainview/components/common/__tests__/LanguageSwitcher.test.tsx +44 -0
  97. package/templates/browser-agent/src/mainview/components/common/__tests__/ThemeToggle.test.tsx +41 -0
  98. package/templates/browser-agent/src/mainview/components/dev/NetworkPanel.tsx +155 -0
  99. package/templates/browser-agent/src/mainview/components/layout/AppLayout.tsx +311 -0
  100. package/templates/browser-agent/src/mainview/components/onboarding/SetupWizard.tsx +310 -0
  101. package/templates/browser-agent/src/mainview/components/process/ProcessPanel.tsx +329 -0
  102. package/templates/browser-agent/src/mainview/components/sidebar/SessionSidebar.tsx +122 -0
  103. package/templates/browser-agent/src/mainview/components/sidebar/SkillSidebar.tsx +115 -0
  104. package/templates/browser-agent/src/mainview/components/topbar/TopBar.tsx +350 -0
  105. package/templates/browser-agent/src/mainview/global.d.ts +13 -0
  106. package/templates/browser-agent/src/mainview/hooks/__tests__/use-breakpoint.test.ts +151 -0
  107. package/templates/browser-agent/src/mainview/hooks/use-agent-chat.ts +157 -0
  108. package/templates/browser-agent/src/mainview/hooks/use-breakpoint.ts +68 -0
  109. package/templates/browser-agent/src/mainview/hooks/use-rpc-init.ts +71 -0
  110. package/templates/browser-agent/src/mainview/hooks/use-sidebar-resize.ts +40 -0
  111. package/templates/browser-agent/src/mainview/index.css +71 -0
  112. package/templates/browser-agent/src/mainview/index.html +12 -0
  113. package/templates/browser-agent/src/mainview/lib/api-client.ts +397 -0
  114. package/templates/browser-agent/src/mainview/lib/i18n/index.ts +30 -0
  115. package/templates/browser-agent/src/mainview/lib/i18n/locales/en.json +130 -0
  116. package/templates/browser-agent/src/mainview/lib/i18n/locales/zh.json +128 -0
  117. package/templates/browser-agent/src/mainview/lib/network-bus.ts +92 -0
  118. package/templates/browser-agent/src/mainview/lib/rpc-cache.ts +94 -0
  119. package/templates/browser-agent/src/mainview/main.tsx +18 -0
  120. package/templates/browser-agent/src/mainview/stores/__tests__/use-connection-store.test.ts +26 -0
  121. package/templates/browser-agent/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
  122. package/templates/browser-agent/src/mainview/stores/__tests__/use-log-store.test.ts +28 -0
  123. package/templates/browser-agent/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
  124. package/templates/browser-agent/src/mainview/stores/message-batcher.ts +25 -0
  125. package/templates/browser-agent/src/mainview/stores/use-asset-store.ts +65 -0
  126. package/templates/browser-agent/src/mainview/stores/use-chat-store.ts +200 -0
  127. package/templates/browser-agent/src/mainview/stores/use-connection-store.ts +173 -0
  128. package/templates/browser-agent/src/mainview/stores/use-locale-store.ts +27 -0
  129. package/templates/browser-agent/src/mainview/stores/use-log-store.ts +16 -0
  130. package/templates/browser-agent/src/mainview/stores/use-network-panel-store.ts +18 -0
  131. package/templates/browser-agent/src/mainview/stores/use-record-store.ts +147 -0
  132. package/templates/browser-agent/src/mainview/stores/use-session-store.ts +151 -0
  133. package/templates/browser-agent/src/mainview/stores/use-sidebar-store.ts +161 -0
  134. package/templates/browser-agent/src/mainview/stores/use-theme-store.ts +46 -0
  135. package/templates/browser-agent/src/mainview/stores/use-view-store.ts +20 -0
  136. package/templates/browser-agent/src/mainview/types/index.ts +6 -0
  137. package/templates/browser-agent/src/server-config.ts +45 -0
  138. package/templates/browser-agent/src/server.ts +78 -0
  139. package/templates/browser-agent/src/shared/handlers/__tests__/chat-concurrency.test.ts +127 -0
  140. package/templates/browser-agent/src/shared/handlers/__tests__/chat-handler.test.ts +77 -0
  141. package/templates/browser-agent/src/shared/handlers/__tests__/file-handler.test.ts +100 -0
  142. package/templates/browser-agent/src/shared/handlers/__tests__/system-handler.test.ts +55 -0
  143. package/templates/browser-agent/src/shared/handlers/__tests__/timer-handler.test.ts +77 -0
  144. package/templates/browser-agent/src/shared/handlers/browser.ts +596 -0
  145. package/templates/browser-agent/src/shared/handlers/chat.ts +213 -0
  146. package/templates/browser-agent/src/shared/handlers/file.ts +99 -0
  147. package/templates/browser-agent/src/shared/handlers/index.ts +7 -0
  148. package/templates/browser-agent/src/shared/handlers/session.ts +167 -0
  149. package/templates/browser-agent/src/shared/handlers/system.ts +27 -0
  150. package/templates/browser-agent/src/shared/handlers/timer.ts +34 -0
  151. package/templates/browser-agent/src/shared/http-routes.ts +437 -0
  152. package/templates/browser-agent/src/shared/lib/__tests__/logger.test.ts +92 -0
  153. package/templates/browser-agent/src/shared/lib/__tests__/path-security.test.ts +77 -0
  154. package/templates/browser-agent/src/shared/lib/__tests__/web-server.test.ts +203 -0
  155. package/templates/browser-agent/src/shared/lib/agent.ts +384 -0
  156. package/templates/browser-agent/src/shared/lib/cdp.ts +448 -0
  157. package/templates/browser-agent/src/shared/lib/generate.ts +111 -0
  158. package/templates/browser-agent/src/shared/lib/logger.ts +152 -0
  159. package/templates/browser-agent/src/shared/lib/mock-stream.ts +147 -0
  160. package/templates/browser-agent/src/shared/lib/path-security.ts +38 -0
  161. package/templates/browser-agent/src/shared/lib/port-registry.ts +103 -0
  162. package/templates/browser-agent/src/shared/lib/web-server.ts +127 -0
  163. package/templates/browser-agent/src/shared/lib/xhs-extract.ts +71 -0
  164. package/templates/browser-agent/src/shared/modules/browser.ts +86 -0
  165. package/templates/browser-agent/src/shared/modules/chat.ts +20 -0
  166. package/templates/browser-agent/src/shared/modules/file.ts +40 -0
  167. package/templates/browser-agent/src/shared/modules/session.ts +55 -0
  168. package/templates/browser-agent/src/shared/modules/system.ts +8 -0
  169. package/templates/browser-agent/src/shared/modules/timer.ts +11 -0
  170. package/templates/browser-agent/src/shared/register-all-handlers.ts +36 -0
  171. package/templates/browser-agent/src/shared/rpc-schema.ts +34 -0
  172. package/templates/browser-agent/tailwind.config.js +15 -0
  173. package/templates/browser-agent/tsconfig.ipc.json +14 -0
  174. package/templates/browser-agent/tsconfig.json +36 -0
  175. package/templates/browser-agent/vite.config.ts +3 -0
  176. package/templates/browser-agent/vitest.config.ts +3 -0
  177. package/templates/chat/.husky/commit-msg +4 -0
  178. package/templates/chat/.husky/pre-commit +11 -0
  179. package/templates/chat/.husky/pre-push +6 -0
  180. package/templates/chat/CHANGELOG.md +15 -0
  181. package/templates/chat/commitlint.config.js +8 -0
  182. package/templates/chat/package.json +19 -4
  183. package/templates/chat/src/mainview/__tests__/hooks/use-input-history.test.ts +125 -0
  184. package/templates/chat/src/mainview/__tests__/setup.ts +32 -29
  185. package/templates/chat/src/mainview/components/chat/ChatPanel.tsx +80 -55
  186. package/templates/chat/src/mainview/components/common/ThemeToggle.tsx +40 -20
  187. package/templates/chat/src/mainview/lib/i18n/locales/en.json +175 -155
  188. package/templates/chat/src/mainview/lib/i18n/locales/zh.json +173 -155
  189. package/templates/chat/src/shared/handlers/chat.ts +17 -19
  190. package/templates/chat/src/shared/handlers/debug.ts +12 -2
  191. package/templates/cowork/.env.example +8 -0
  192. package/templates/cowork/.prettierignore +6 -0
  193. package/templates/cowork/.prettierrc +9 -0
  194. package/templates/cowork/AGENTS.md +236 -0
  195. package/templates/cowork/CHANGELOG.md +15 -0
  196. package/templates/cowork/LICENSE +21 -0
  197. package/templates/cowork/README.md +103 -0
  198. package/templates/cowork/commitlint.config.js +8 -0
  199. package/templates/cowork/docs/CODE-VIEW-PLAN.md +637 -0
  200. package/templates/cowork/docs/CODE-VIEW-V2.md +196 -0
  201. package/templates/cowork/docs/CODE-VIEW-V4.md +150 -0
  202. package/templates/cowork/electrobun.config.ts +27 -0
  203. package/templates/cowork/eslint.config.mjs +79 -0
  204. package/templates/cowork/llms.txt +24 -0
  205. package/templates/cowork/package.json +87 -0
  206. package/templates/cowork/postcss.config.js +6 -0
  207. package/templates/cowork/scripts/dev.ts +138 -0
  208. package/templates/cowork/src/__tests__/hybrid-mode.test.ts +126 -0
  209. package/templates/cowork/src/__tests__/server-config-security.test.ts +55 -0
  210. package/templates/cowork/src/__tests__/server-config.test.ts +59 -0
  211. package/templates/cowork/src/bun/index.ts +130 -0
  212. package/templates/cowork/src/bun/three.d.ts +1 -0
  213. package/templates/cowork/src/gateway/http-routes.ts +1 -0
  214. package/templates/cowork/src/gateway/ipc-transport.ts +68 -0
  215. package/templates/cowork/src/gateway/sse-transport.ts +231 -0
  216. package/templates/cowork/src/mainview/App.tsx +45 -0
  217. package/templates/cowork/src/mainview/__tests__/hooks/use-rpc-init.test.ts +202 -0
  218. package/templates/cowork/src/mainview/__tests__/hooks/use-sidebar-resize.test.ts +127 -0
  219. package/templates/cowork/src/mainview/__tests__/i18n/i18n.test.ts +49 -0
  220. package/templates/cowork/src/mainview/__tests__/setup.ts +40 -0
  221. package/templates/cowork/src/mainview/__tests__/stores/use-chat-store.test.ts +73 -0
  222. package/templates/cowork/src/mainview/__tests__/stores/use-sidebar-store.test.ts +61 -0
  223. package/templates/cowork/src/mainview/__tests__/theme-variables.test.ts +36 -0
  224. package/templates/cowork/src/mainview/components/chat/TaskChat.tsx +311 -0
  225. package/templates/cowork/src/mainview/components/chat/__tests__/localhost-links.test.ts +76 -0
  226. package/templates/cowork/src/mainview/components/common/ErrorBoundary.tsx +1 -0
  227. package/templates/cowork/src/mainview/components/common/LanguageSwitcher.tsx +15 -0
  228. package/templates/cowork/src/mainview/components/common/ThemeToggle.tsx +45 -0
  229. package/templates/cowork/src/mainview/components/common/__tests__/ErrorBoundary.test.tsx +74 -0
  230. package/templates/cowork/src/mainview/components/common/__tests__/LanguageSwitcher.test.tsx +44 -0
  231. package/templates/cowork/src/mainview/components/common/__tests__/ThemeToggle.test.tsx +41 -0
  232. package/templates/cowork/src/mainview/components/dev/NetworkPanel.tsx +155 -0
  233. package/templates/cowork/src/mainview/components/layout/AppLayout.tsx +216 -0
  234. package/templates/cowork/src/mainview/components/right/ArtifactsPanel.tsx +54 -0
  235. package/templates/cowork/src/mainview/components/right/ContextPanel.tsx +133 -0
  236. package/templates/cowork/src/mainview/components/right/ElementPicker.tsx +206 -0
  237. package/templates/cowork/src/mainview/components/right/PreviewBlock.tsx +155 -0
  238. package/templates/cowork/src/mainview/components/right/ProgressPanel.tsx +85 -0
  239. package/templates/cowork/src/mainview/components/sidebar/TaskSidebar.tsx +211 -0
  240. package/templates/cowork/src/mainview/components/topbar/TopBar.tsx +86 -0
  241. package/templates/cowork/src/mainview/global.d.ts +13 -0
  242. package/templates/cowork/src/mainview/hooks/__tests__/use-breakpoint.test.ts +123 -0
  243. package/templates/cowork/src/mainview/hooks/use-breakpoint.ts +53 -0
  244. package/templates/cowork/src/mainview/hooks/use-rpc-init.ts +26 -0
  245. package/templates/cowork/src/mainview/hooks/use-sidebar-resize.ts +40 -0
  246. package/templates/cowork/src/mainview/index.css +89 -0
  247. package/templates/cowork/src/mainview/index.html +12 -0
  248. package/templates/cowork/src/mainview/lib/api-client.ts +404 -0
  249. package/templates/cowork/src/mainview/lib/i18n/index.ts +30 -0
  250. package/templates/cowork/src/mainview/lib/i18n/locales/en.json +130 -0
  251. package/templates/cowork/src/mainview/lib/i18n/locales/zh.json +128 -0
  252. package/templates/cowork/src/mainview/lib/network-bus.ts +92 -0
  253. package/templates/cowork/src/mainview/lib/rpc-cache.ts +94 -0
  254. package/templates/cowork/src/mainview/main.tsx +18 -0
  255. package/templates/cowork/src/mainview/stores/__tests__/use-connection-store.test.ts +26 -0
  256. package/templates/cowork/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
  257. package/templates/cowork/src/mainview/stores/__tests__/use-log-store.test.ts +28 -0
  258. package/templates/cowork/src/mainview/stores/__tests__/use-preview-store.test.ts +193 -0
  259. package/templates/cowork/src/mainview/stores/__tests__/use-sidebar-store.test.ts +81 -0
  260. package/templates/cowork/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
  261. package/templates/cowork/src/mainview/stores/message-batcher.ts +25 -0
  262. package/templates/cowork/src/mainview/stores/use-chat-store.ts +198 -0
  263. package/templates/cowork/src/mainview/stores/use-connection-store.ts +168 -0
  264. package/templates/cowork/src/mainview/stores/use-context-store.ts +68 -0
  265. package/templates/cowork/src/mainview/stores/use-locale-store.ts +27 -0
  266. package/templates/cowork/src/mainview/stores/use-log-store.ts +16 -0
  267. package/templates/cowork/src/mainview/stores/use-network-panel-store.ts +18 -0
  268. package/templates/cowork/src/mainview/stores/use-output-store.ts +47 -0
  269. package/templates/cowork/src/mainview/stores/use-preview-store.ts +97 -0
  270. package/templates/cowork/src/mainview/stores/use-sidebar-store.ts +272 -0
  271. package/templates/cowork/src/mainview/stores/use-task-store.ts +86 -0
  272. package/templates/cowork/src/mainview/stores/use-theme-store.ts +46 -0
  273. package/templates/cowork/src/mainview/stores/use-view-store.ts +29 -0
  274. package/templates/cowork/src/mainview/types/index.ts +6 -0
  275. package/templates/cowork/src/server-config.ts +44 -0
  276. package/templates/cowork/src/server.ts +78 -0
  277. package/templates/cowork/src/shared/handlers/__tests__/chat-concurrency.test.ts +127 -0
  278. package/templates/cowork/src/shared/handlers/__tests__/chat-handler.test.ts +77 -0
  279. package/templates/cowork/src/shared/handlers/__tests__/file-handler.test.ts +100 -0
  280. package/templates/cowork/src/shared/handlers/__tests__/preview-handler.test.ts +172 -0
  281. package/templates/cowork/src/shared/handlers/__tests__/system-handler.test.ts +55 -0
  282. package/templates/cowork/src/shared/handlers/__tests__/timer-handler.test.ts +77 -0
  283. package/templates/cowork/src/shared/handlers/chat.ts +213 -0
  284. package/templates/cowork/src/shared/handlers/context.ts +72 -0
  285. package/templates/cowork/src/shared/handlers/file.ts +99 -0
  286. package/templates/cowork/src/shared/handlers/index.ts +9 -0
  287. package/templates/cowork/src/shared/handlers/output.ts +59 -0
  288. package/templates/cowork/src/shared/handlers/preview.ts +81 -0
  289. package/templates/cowork/src/shared/handlers/system.ts +27 -0
  290. package/templates/cowork/src/shared/handlers/task.ts +101 -0
  291. package/templates/cowork/src/shared/handlers/timer.ts +34 -0
  292. package/templates/cowork/src/shared/http-routes.ts +444 -0
  293. package/templates/cowork/src/shared/lib/__tests__/logger.test.ts +92 -0
  294. package/templates/cowork/src/shared/lib/__tests__/path-security.test.ts +77 -0
  295. package/templates/cowork/src/shared/lib/__tests__/web-server.test.ts +203 -0
  296. package/templates/cowork/src/shared/lib/logger.ts +152 -0
  297. package/templates/cowork/src/shared/lib/path-security.ts +38 -0
  298. package/templates/cowork/src/shared/lib/port-registry.ts +103 -0
  299. package/templates/cowork/src/shared/lib/web-server.ts +127 -0
  300. package/templates/cowork/src/shared/modules/chat.ts +20 -0
  301. package/templates/cowork/src/shared/modules/context.ts +32 -0
  302. package/templates/cowork/src/shared/modules/file.ts +40 -0
  303. package/templates/cowork/src/shared/modules/output.ts +20 -0
  304. package/templates/cowork/src/shared/modules/preview.ts +44 -0
  305. package/templates/cowork/src/shared/modules/system.ts +8 -0
  306. package/templates/cowork/src/shared/modules/task.ts +31 -0
  307. package/templates/cowork/src/shared/modules/timer.ts +11 -0
  308. package/templates/cowork/src/shared/register-all-handlers.ts +36 -0
  309. package/templates/cowork/src/shared/rpc-schema.ts +38 -0
  310. package/templates/cowork/tailwind.config.js +15 -0
  311. package/templates/cowork/test-upload.txt +0 -0
  312. package/templates/cowork/tsconfig.ipc.json +14 -0
  313. package/templates/cowork/tsconfig.json +36 -0
  314. package/templates/cowork/vite.config.ts +3 -0
  315. package/templates/cowork/vitest.config.ts +3 -0
  316. package/templates/general/.husky/commit-msg +4 -0
  317. package/templates/general/.husky/pre-commit +11 -0
  318. package/templates/general/.husky/pre-push +6 -0
  319. package/templates/general/CHANGELOG.md +15 -0
  320. package/templates/general/commitlint.config.js +8 -0
  321. package/templates/general/package.json +19 -4
  322. package/templates/general/src/mainview/__tests__/hooks/use-input-history.test.ts +125 -0
  323. package/templates/general/src/mainview/__tests__/setup.ts +32 -29
  324. package/templates/general/src/mainview/components/chat/ChatPanel.tsx +80 -55
  325. package/templates/general/src/mainview/components/common/ThemeToggle.tsx +40 -20
  326. package/templates/general/src/mainview/components/debug/DebugPanel.tsx +170 -156
  327. package/templates/general/src/mainview/components/debug/__tests__/DebugPanel.test.tsx +169 -0
  328. package/templates/general/src/mainview/components/explorer/ConfirmDialog.tsx +42 -42
  329. package/templates/general/src/mainview/components/explorer/ContextMenu.tsx +72 -67
  330. package/templates/general/src/mainview/components/feed/FeedPanel.tsx +7 -5
  331. package/templates/general/src/mainview/components/file-preview/FilePreviewOverlay.tsx +64 -45
  332. package/templates/general/src/mainview/components/file-preview/VirtualizedCodeView.tsx +24 -7
  333. package/templates/general/src/mainview/components/git/GitPanel.tsx +700 -473
  334. package/templates/general/src/mainview/components/layout/AppLayout.tsx +123 -114
  335. package/templates/general/src/mainview/components/search/SearchPanel.tsx +15 -19
  336. package/templates/general/src/mainview/lib/i18n/locales/en.json +175 -155
  337. package/templates/general/src/mainview/lib/i18n/locales/zh.json +173 -155
  338. package/templates/general/src/mainview/stores/__tests__/use-explorer-store.test.ts +259 -0
  339. package/templates/general/src/mainview/stores/__tests__/use-feed-store.test.ts +160 -0
  340. package/templates/general/src/mainview/stores/__tests__/use-git-store.test.ts +313 -0
  341. package/templates/general/src/mainview/stores/__tests__/use-notification-store.test.ts +115 -0
  342. package/templates/general/src/mainview/stores/__tests__/use-sidebar-store.test.ts +120 -0
  343. package/templates/general/src/mainview/stores/use-explorer-store.ts +277 -260
  344. package/templates/general/src/mainview/types/index.ts +21 -20
  345. package/templates/general/src/shared/handlers/chat.ts +1 -1
  346. package/templates/general/src/shared/handlers/debug.ts +12 -2
  347. package/templates/shared/components/ErrorBoundary.tsx +0 -1
  348. package/templates/shared/vite-base.config.ts +8 -7
  349. /package/templates/{shared → browser-agent}/test-upload.txt +0 -0
@@ -1,11 +1,13 @@
1
- import { describe, it, expect, vi, beforeEach } from "vitest";
2
- import { EventEmitter } from "events";
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { EventEmitter } from 'events';
3
+ import type { Server } from 'http';
4
+ import type { WebSocketServer } from 'ws';
3
5
 
4
- vi.mock("../../shared/register-all-handlers", () => ({
6
+ vi.mock('../../shared/register-all-handlers', () => ({
5
7
  registerAllHandlers: vi.fn(),
6
8
  }));
7
9
 
8
- vi.mock("../../shared/lib/logger", () => ({
10
+ vi.mock('../../shared/lib/logger', () => ({
9
11
  createLogger: () => ({
10
12
  info: vi.fn(),
11
13
  error: vi.fn(),
@@ -14,14 +16,22 @@ vi.mock("../../shared/lib/logger", () => ({
14
16
  }),
15
17
  }));
16
18
 
17
- const MOCK_AUTH_TOKEN = "test-secure-token-123456";
19
+ const MOCK_AUTH_TOKEN = 'test-secure-token-123456';
18
20
 
19
- function createMockWs() {
20
- const ws = new EventEmitter();
21
- (ws as any).readyState = 1;
22
- (ws as any).close = vi.fn();
23
- (ws as any).send = vi.fn();
24
- (ws as any).ping = vi.fn();
21
+ interface MockWs extends EventEmitter {
22
+ readyState: number;
23
+ close: ReturnType<typeof vi.fn>;
24
+ send: ReturnType<typeof vi.fn>;
25
+ ping: ReturnType<typeof vi.fn>;
26
+ }
27
+
28
+ function createMockWs(): MockWs {
29
+ const ws = Object.assign(new EventEmitter(), {
30
+ readyState: 1,
31
+ close: vi.fn(),
32
+ send: vi.fn(),
33
+ ping: vi.fn(),
34
+ }) as MockWs;
25
35
  return ws;
26
36
  }
27
37
 
@@ -29,19 +39,19 @@ function createMockReq(url: string, headers: Record<string, string> = {}) {
29
39
  return { url, headers };
30
40
  }
31
41
 
32
- describe("WS Handler Token 安全", () => {
33
- let _wssInstance: EventEmitter & { clients: Set<any> };
42
+ describe('WS Handler Token 安全', () => {
43
+ let _wssInstance: EventEmitter & { clients: Set<unknown> };
34
44
 
35
45
  beforeEach(async () => {
36
46
  vi.resetModules();
37
47
 
38
- const { WebSocketServer } = await import("ws");
48
+ const { WebSocketServer } = await import('ws');
39
49
  const _originalWSS = WebSocketServer;
40
50
 
41
- vi.doMock("ws", () => {
51
+ vi.doMock('ws', () => {
42
52
  class MockWSS extends EventEmitter {
43
- clients = new Set();
44
- constructor(_opts: any) {
53
+ clients = new Set<unknown>();
54
+ constructor(_opts: unknown) {
45
55
  super();
46
56
  // eslint-disable-next-line @typescript-eslint/no-this-alias
47
57
  _wssInstance = this;
@@ -52,67 +62,71 @@ describe("WS Handler Token 安全", () => {
52
62
  });
53
63
 
54
64
  async function setupTest() {
55
- const { createWsHandler } = await import("../ws-handler");
65
+ const { createWsHandler } = await import('../ws-handler');
56
66
  const httpServer = new EventEmitter();
57
- const wss = createWsHandler(httpServer as any, {
67
+ const wss = createWsHandler(httpServer as unknown as Server, {
58
68
  config: { port: 3100, authToken: MOCK_AUTH_TOKEN, maxUploadSize: 50 * 1024 * 1024 },
59
69
  });
60
70
  return wss;
61
71
  }
62
72
 
63
- function emitConnection(wss: any, ws: any, req: any) {
64
- wss.emit("connection", ws, req);
73
+ function emitConnection(
74
+ wss: WebSocketServer,
75
+ ws: MockWs,
76
+ req: { url: string; headers: Record<string, string> },
77
+ ) {
78
+ wss.emit('connection', ws, req);
65
79
  }
66
80
 
67
- it("无 token 应被拒绝 (4001)", async () => {
81
+ it('无 token 应被拒绝 (4001)', async () => {
68
82
  const wss = await setupTest();
69
83
  const ws = createMockWs();
70
- const req = createMockReq("/ws");
84
+ const req = createMockReq('/ws');
71
85
  emitConnection(wss, ws, req);
72
- expect((ws as any).close).toHaveBeenCalledWith(4001, "Unauthorized");
86
+ expect(ws.close).toHaveBeenCalledWith(4001, 'Unauthorized');
73
87
  });
74
88
 
75
- it("错误 token 应被拒绝 (4001)", async () => {
89
+ it('错误 token 应被拒绝 (4001)', async () => {
76
90
  const wss = await setupTest();
77
91
  const ws = createMockWs();
78
- const req = createMockReq("/ws?token=wrong-token");
92
+ const req = createMockReq('/ws?token=wrong-token');
79
93
  emitConnection(wss, ws, req);
80
- expect((ws as any).close).toHaveBeenCalledWith(4001, "Unauthorized");
94
+ expect(ws.close).toHaveBeenCalledWith(4001, 'Unauthorized');
81
95
  });
82
96
 
83
- it("正确 token 通过 URL query 应被接受", async () => {
97
+ it('正确 token 通过 URL query 应被接受', async () => {
84
98
  const wss = await setupTest();
85
99
  const ws = createMockWs();
86
100
  const req = createMockReq(`/ws?token=${MOCK_AUTH_TOKEN}`);
87
101
  emitConnection(wss, ws, req);
88
- expect((ws as any).close).not.toHaveBeenCalled();
102
+ expect(ws.close).not.toHaveBeenCalled();
89
103
  });
90
104
 
91
- it("正确 token 通过 Sec-WebSocket-Protocol header 应被接受", async () => {
105
+ it('正确 token 通过 Sec-WebSocket-Protocol header 应被接受', async () => {
92
106
  const wss = await setupTest();
93
107
  const ws = createMockWs();
94
- const req = createMockReq("/ws", {
95
- "sec-websocket-protocol": MOCK_AUTH_TOKEN,
108
+ const req = createMockReq('/ws', {
109
+ 'sec-websocket-protocol': MOCK_AUTH_TOKEN,
96
110
  });
97
111
  emitConnection(wss, ws, req);
98
- expect((ws as any).close).not.toHaveBeenCalled();
112
+ expect(ws.close).not.toHaveBeenCalled();
99
113
  });
100
114
 
101
- it("header 方式优先于 query 方式 (header 正确, query 错误)", async () => {
115
+ it('header 方式优先于 query 方式 (header 正确, query 错误)', async () => {
102
116
  const wss = await setupTest();
103
117
  const ws = createMockWs();
104
- const req = createMockReq("/ws?token=wrong-token", {
105
- "sec-websocket-protocol": MOCK_AUTH_TOKEN,
118
+ const req = createMockReq('/ws?token=wrong-token', {
119
+ 'sec-websocket-protocol': MOCK_AUTH_TOKEN,
106
120
  });
107
121
  emitConnection(wss, ws, req);
108
- expect((ws as any).close).not.toHaveBeenCalled();
122
+ expect(ws.close).not.toHaveBeenCalled();
109
123
  });
110
124
 
111
- it("token 验证使用 timingSafeEqual(长度不等返回 false)", async () => {
125
+ it('token 验证使用 timingSafeEqual(长度不等返回 false)', async () => {
112
126
  const wss = await setupTest();
113
127
  const ws = createMockWs();
114
128
  const req = createMockReq(`/ws?token=short`);
115
129
  emitConnection(wss, ws, req);
116
- expect((ws as any).close).toHaveBeenCalledWith(4001, "Unauthorized");
130
+ expect(ws.close).toHaveBeenCalledWith(4001, 'Unauthorized');
117
131
  });
118
132
  });
@@ -1,17 +1,19 @@
1
- import type { ReactNode } from "react";
2
- import { useState } from "react";
3
- import { useConnectionStore } from "./stores/use-connection-store";
4
- import { useBreakpointSync } from "./hooks/use-breakpoint";
5
- import { useRpcInit } from "./hooks/use-rpc-init";
6
- import { useSidebarResize } from "./hooks/use-sidebar-resize";
7
- import { AppLayout, type CenterTab } from "./components/layout/AppLayout";
8
- import { ErrorBoundary as _EB } from "./components/common/ErrorBoundary";
1
+ import type { ReactNode } from 'react';
2
+ import { useState } from 'react';
3
+ import { useTranslation } from 'react-i18next';
4
+ import { useConnectionStore } from './stores/use-connection-store';
5
+ import { useBreakpointSync } from './hooks/use-breakpoint';
6
+ import { useRpcInit } from './hooks/use-rpc-init';
7
+ import { useSidebarResize } from './hooks/use-sidebar-resize';
8
+ import { AppLayout, type CenterTab } from './components/layout/AppLayout';
9
+ import { ErrorBoundary as _EB } from './components/common/ErrorBoundary';
9
10
 
10
11
  const ErrorBoundary = _EB as unknown as React.FC<{ children: ReactNode }>;
11
12
 
12
13
  function App() {
14
+ const { t } = useTranslation();
13
15
  const ready = useConnectionStore((s) => s.ready);
14
- const [centerTab, setCenterTab] = useState<CenterTab>("chat");
16
+ const [centerTab, setCenterTab] = useState<CenterTab>('chat');
15
17
 
16
18
  useBreakpointSync();
17
19
  useRpcInit();
@@ -20,10 +22,10 @@ function App() {
20
22
 
21
23
  if (!ready) {
22
24
  return (
23
- <div className="h-screen bg-gray-900 flex items-center justify-center">
25
+ <div className="h-screen bg-[var(--color-bg-primary)] flex items-center justify-center">
24
26
  <div className="text-center">
25
- <div className="inline-block w-8 h-8 border-2 border-indigo-400 border-t-transparent rounded-full animate-spin mb-4" />
26
- <div className="text-gray-400 text-sm">Connecting to RPC server...</div>
27
+ <div className="inline-block w-8 h-8 border-2 border-[var(--color-text-accent)] border-t-transparent rounded-full animate-spin mb-4" />
28
+ <div className="text-[var(--color-text-tertiary)] text-sm">{t('app.connecting')}</div>
27
29
  </div>
28
30
  </div>
29
31
  );
@@ -1,5 +1,5 @@
1
- import { describe, it, expect, vi, beforeEach } from "vitest";
2
- import { renderHook, waitFor } from "@testing-library/react";
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { renderHook, waitFor } from '@testing-library/react';
3
3
 
4
4
  const mockInitializeConnection = vi.fn();
5
5
  const mockAddLog = vi.fn();
@@ -11,207 +11,211 @@ const mockCall = vi.fn();
11
11
  const mockChatAddMessage = vi.fn();
12
12
  const mockChatSetMessages = vi.fn();
13
13
 
14
- let mockConnectionState: Record<string, any>;
15
- let mockLogState: Record<string, any>;
14
+ let mockConnectionState: Record<string, unknown>;
15
+ let mockLogState: Record<string, unknown>;
16
16
 
17
- function resetMockConnectionState(overrides?: Partial<Record<string, any>>) {
18
- mockConnectionState = {
19
- ready: false,
20
- initializeConnection: mockInitializeConnection,
21
- ...overrides,
22
- };
17
+ function resetMockConnectionState(overrides?: Partial<Record<string, unknown>>) {
18
+ mockConnectionState = {
19
+ ready: false,
20
+ initializeConnection: mockInitializeConnection,
21
+ ...overrides,
22
+ };
23
23
  }
24
24
 
25
- function resetMockLogState(overrides?: Partial<Record<string, any>>) {
26
- mockLogState = {
27
- addLog: mockAddLog,
28
- ...overrides,
29
- };
25
+ function resetMockLogState(overrides?: Partial<Record<string, unknown>>) {
26
+ mockLogState = {
27
+ addLog: mockAddLog,
28
+ ...overrides,
29
+ };
30
30
  }
31
31
 
32
- vi.mock("../../stores/use-connection-store", () => {
33
- return {
34
- useConnectionStore: Object.assign(
35
- (selector: (s: any) => any) => selector(mockConnectionState),
36
- {
37
- getState: () => mockConnectionState,
38
- setState: (partial: any) => {
39
- const next = typeof partial === "function" ? partial(mockConnectionState) : partial;
40
- mockConnectionState = { ...mockConnectionState, ...next };
41
- },
42
- }
43
- ),
44
- };
32
+ vi.mock('../../stores/use-connection-store', () => {
33
+ return {
34
+ useConnectionStore: Object.assign(
35
+ (selector: (s: Record<string, unknown>) => unknown) => selector(mockConnectionState),
36
+ {
37
+ getState: () => mockConnectionState,
38
+ setState: (
39
+ partial:
40
+ | Partial<Record<string, unknown>>
41
+ | ((prev: Record<string, unknown>) => Partial<Record<string, unknown>>),
42
+ ) => {
43
+ const next = typeof partial === 'function' ? partial(mockConnectionState) : partial;
44
+ mockConnectionState = { ...mockConnectionState, ...next };
45
+ },
46
+ },
47
+ ),
48
+ };
45
49
  });
46
50
 
47
- vi.mock("../../stores/use-log-store", () => {
48
- return {
49
- useLogStore: Object.assign(
50
- (selector: (s: any) => any) => selector(mockLogState),
51
- {
52
- getState: () => mockLogState,
53
- setState: (partial: any) => {
54
- const next = typeof partial === "function" ? partial(mockLogState) : partial;
55
- mockLogState = { ...mockLogState, ...next };
56
- },
57
- }
58
- ),
59
- };
51
+ vi.mock('../../stores/use-log-store', () => {
52
+ return {
53
+ useLogStore: Object.assign(
54
+ (selector: (s: Record<string, unknown>) => unknown) => selector(mockLogState),
55
+ {
56
+ getState: () => mockLogState,
57
+ setState: (
58
+ partial:
59
+ | Partial<Record<string, unknown>>
60
+ | ((prev: Record<string, unknown>) => Partial<Record<string, unknown>>),
61
+ ) => {
62
+ const next = typeof partial === 'function' ? partial(mockLogState) : partial;
63
+ mockLogState = { ...mockLogState, ...next };
64
+ },
65
+ },
66
+ ),
67
+ };
60
68
  });
61
69
 
62
- vi.mock("../../stores/use-explorer-store", () => ({
63
- useExplorerStore: Object.assign(
64
- (selector: (s: any) => any) =>
65
- selector({ listRootDir: mockListRootDir }),
66
- { getState: () => ({ listRootDir: mockListRootDir }) }
67
- ),
70
+ vi.mock('../../stores/use-explorer-store', () => ({
71
+ useExplorerStore: Object.assign(
72
+ (selector: (s: Record<string, unknown>) => unknown) =>
73
+ selector({ listRootDir: mockListRootDir }),
74
+ { getState: () => ({ listRootDir: mockListRootDir }) },
75
+ ),
68
76
  }));
69
77
 
70
- vi.mock("../../stores/use-chat-store", () => ({
71
- useChatStore: Object.assign(
72
- (selector: (s: any) => any) =>
73
- selector({ addMessage: mockChatAddMessage, setMessages: mockChatSetMessages }),
74
- {
75
- getState: () => ({
76
- addMessage: mockChatAddMessage,
77
- setMessages: mockChatSetMessages,
78
- }),
79
- }
80
- ),
78
+ vi.mock('../../stores/use-chat-store', () => ({
79
+ useChatStore: Object.assign(
80
+ (selector: (s: Record<string, unknown>) => unknown) =>
81
+ selector({ addMessage: mockChatAddMessage, setMessages: mockChatSetMessages }),
82
+ {
83
+ getState: () => ({
84
+ addMessage: mockChatAddMessage,
85
+ setMessages: mockChatSetMessages,
86
+ }),
87
+ },
88
+ ),
81
89
  }));
82
90
 
83
- vi.mock("../../lib/api-client", () => ({
84
- apiClient: {
85
- subscribe: mockSubscribe,
86
- unsubscribe: mockUnsubscribe,
87
- call: mockCall,
88
- },
91
+ vi.mock('../../lib/api-client', () => ({
92
+ apiClient: {
93
+ subscribe: mockSubscribe,
94
+ unsubscribe: mockUnsubscribe,
95
+ call: mockCall,
96
+ },
89
97
  }));
90
98
 
91
- describe("useRpcInit", () => {
92
- beforeEach(() => {
93
- vi.clearAllMocks();
94
- mockSubscribe.mockResolvedValue("sub-1");
95
- mockCall.mockResolvedValue({ messages: [] });
96
- resetMockLogState();
97
- });
98
-
99
- it("should call initializeConnection on mount", async () => {
100
- resetMockConnectionState({ ready: false });
101
- const { useRpcInit } = await import("../../hooks/use-rpc-init");
102
- renderHook(() => useRpcInit());
103
- expect(mockInitializeConnection).toHaveBeenCalledTimes(1);
104
- });
105
-
106
- it("should not subscribe when not ready", async () => {
107
- resetMockConnectionState({ ready: false });
108
- const { useRpcInit } = await import("../../hooks/use-rpc-init");
109
- renderHook(() => useRpcInit());
110
- expect(mockSubscribe).not.toHaveBeenCalled();
111
- expect(mockListRootDir).not.toHaveBeenCalled();
112
- expect(mockCall).not.toHaveBeenCalled();
113
- });
114
-
115
- it("should subscribe, load history and init explorer when ready", async () => {
116
- resetMockConnectionState({ ready: true });
117
- const { useRpcInit } = await import("../../hooks/use-rpc-init");
118
- renderHook(() => useRpcInit());
119
-
120
- await waitFor(() => {
121
- expect(mockListRootDir).toHaveBeenCalledTimes(1);
122
- });
123
-
124
- await waitFor(() => {
125
- expect(mockSubscribe).toHaveBeenCalledWith(
126
- "chat.message",
127
- expect.any(Function),
128
- {}
129
- );
130
- });
131
-
132
- await waitFor(() => {
133
- expect(mockCall).toHaveBeenCalledWith("chat.list", { limit: 100 });
134
- });
135
- });
136
-
137
- it("should load history messages into chat store", async () => {
138
- const messages = [
139
- { id: "1", role: "user" as const, content: "hello", timestamp: 1000 },
140
- { id: "2", role: "assistant" as const, content: "world", timestamp: 2000 },
141
- ];
142
- mockCall.mockResolvedValue({ messages });
143
-
144
- resetMockConnectionState({ ready: true });
145
- const { useRpcInit } = await import("../../hooks/use-rpc-init");
146
- renderHook(() => useRpcInit());
147
-
148
- await waitFor(() => {
149
- expect(mockChatSetMessages).toHaveBeenCalledWith(
150
- messages.map((m) => ({
151
- id: m.id,
152
- role: m.role,
153
- content: m.content,
154
- timestamp: m.timestamp,
155
- }))
156
- );
157
- });
158
-
159
- expect(mockAddLog).toHaveBeenCalledWith("Loaded 2 history messages");
160
- });
161
-
162
- it("should unsubscribe on cleanup when ready", async () => {
163
- resetMockConnectionState({ ready: true });
164
- const { useRpcInit } = await import("../../hooks/use-rpc-init");
165
- const { unmount } = renderHook(() => useRpcInit());
166
-
167
- await waitFor(() => {
168
- expect(mockSubscribe).toHaveBeenCalled();
169
- });
170
-
171
- unmount();
172
- expect(mockUnsubscribe).toHaveBeenCalledWith("sub-1");
173
- });
174
-
175
- it("should handle history load failure gracefully", async () => {
176
- mockCall.mockRejectedValue(new Error("Network error"));
177
- resetMockConnectionState({ ready: true });
178
- const { useRpcInit } = await import("../../hooks/use-rpc-init");
179
- renderHook(() => useRpcInit());
180
-
181
- await waitFor(() => {
182
- expect(mockAddLog).toHaveBeenCalledWith(
183
- expect.stringContaining("Failed to load history")
184
- );
185
- });
186
- });
187
-
188
- it("should forward chat.message payloads to chat store", async () => {
189
- let subscriptionHandler: ((payload: any) => void) | undefined;
190
- mockSubscribe.mockImplementation(async (_event: string, handler: (p: any) => void) => {
191
- subscriptionHandler = handler;
192
- return "sub-1";
193
- });
194
-
195
- resetMockConnectionState({ ready: true });
196
- const { useRpcInit } = await import("../../hooks/use-rpc-init");
197
- renderHook(() => useRpcInit());
198
-
199
- await waitFor(() => {
200
- expect(mockSubscribe).toHaveBeenCalled();
201
- });
202
-
203
- subscriptionHandler!({
204
- id: "msg-1",
205
- role: "user",
206
- content: "test message",
207
- timestamp: 12345,
208
- });
209
-
210
- expect(mockChatAddMessage).toHaveBeenCalledWith({
211
- id: "msg-1",
212
- role: "user",
213
- content: "test message",
214
- timestamp: 12345,
215
- });
216
- });
99
+ describe('useRpcInit', () => {
100
+ beforeEach(() => {
101
+ vi.clearAllMocks();
102
+ mockSubscribe.mockResolvedValue('sub-1');
103
+ mockCall.mockResolvedValue({ messages: [] });
104
+ resetMockLogState();
105
+ });
106
+
107
+ it('should call initializeConnection on mount', async () => {
108
+ resetMockConnectionState({ ready: false });
109
+ const { useRpcInit } = await import('../../hooks/use-rpc-init');
110
+ renderHook(() => useRpcInit());
111
+ expect(mockInitializeConnection).toHaveBeenCalledTimes(1);
112
+ });
113
+
114
+ it('should not subscribe when not ready', async () => {
115
+ resetMockConnectionState({ ready: false });
116
+ const { useRpcInit } = await import('../../hooks/use-rpc-init');
117
+ renderHook(() => useRpcInit());
118
+ expect(mockSubscribe).not.toHaveBeenCalled();
119
+ expect(mockListRootDir).not.toHaveBeenCalled();
120
+ expect(mockCall).not.toHaveBeenCalled();
121
+ });
122
+
123
+ it('should subscribe, load history and init explorer when ready', async () => {
124
+ resetMockConnectionState({ ready: true });
125
+ const { useRpcInit } = await import('../../hooks/use-rpc-init');
126
+ renderHook(() => useRpcInit());
127
+
128
+ await waitFor(() => {
129
+ expect(mockListRootDir).toHaveBeenCalledTimes(1);
130
+ });
131
+
132
+ await waitFor(() => {
133
+ expect(mockSubscribe).toHaveBeenCalledWith('chat.message', expect.any(Function), {});
134
+ });
135
+
136
+ await waitFor(() => {
137
+ expect(mockCall).toHaveBeenCalledWith('chat.list', { limit: 100 });
138
+ });
139
+ });
140
+
141
+ it('should load history messages into chat store', async () => {
142
+ const messages = [
143
+ { id: '1', role: 'user' as const, content: 'hello', timestamp: 1000 },
144
+ { id: '2', role: 'assistant' as const, content: 'world', timestamp: 2000 },
145
+ ];
146
+ mockCall.mockResolvedValue({ messages });
147
+
148
+ resetMockConnectionState({ ready: true });
149
+ const { useRpcInit } = await import('../../hooks/use-rpc-init');
150
+ renderHook(() => useRpcInit());
151
+
152
+ await waitFor(() => {
153
+ expect(mockChatSetMessages).toHaveBeenCalledWith(
154
+ messages.map((m) => ({
155
+ id: m.id,
156
+ role: m.role,
157
+ content: m.content,
158
+ timestamp: m.timestamp,
159
+ })),
160
+ );
161
+ });
162
+
163
+ expect(mockAddLog).toHaveBeenCalledWith('Loaded 2 history messages');
164
+ });
165
+
166
+ it('should unsubscribe on cleanup when ready', async () => {
167
+ resetMockConnectionState({ ready: true });
168
+ const { useRpcInit } = await import('../../hooks/use-rpc-init');
169
+ const { unmount } = renderHook(() => useRpcInit());
170
+
171
+ await waitFor(() => {
172
+ expect(mockSubscribe).toHaveBeenCalled();
173
+ });
174
+
175
+ unmount();
176
+ expect(mockUnsubscribe).toHaveBeenCalledWith('sub-1');
177
+ });
178
+
179
+ it('should handle history load failure gracefully', async () => {
180
+ mockCall.mockRejectedValue(new Error('Network error'));
181
+ resetMockConnectionState({ ready: true });
182
+ const { useRpcInit } = await import('../../hooks/use-rpc-init');
183
+ renderHook(() => useRpcInit());
184
+
185
+ await waitFor(() => {
186
+ expect(mockAddLog).toHaveBeenCalledWith(expect.stringContaining('Failed to load history'));
187
+ });
188
+ });
189
+
190
+ it('should forward chat.message payloads to chat store', async () => {
191
+ let subscriptionHandler: ((payload: Record<string, unknown>) => void) | undefined;
192
+ mockSubscribe.mockImplementation(
193
+ async (_event: string, handler: (p: Record<string, unknown>) => void) => {
194
+ subscriptionHandler = handler;
195
+ return 'sub-1';
196
+ },
197
+ );
198
+
199
+ resetMockConnectionState({ ready: true });
200
+ const { useRpcInit } = await import('../../hooks/use-rpc-init');
201
+ renderHook(() => useRpcInit());
202
+
203
+ await waitFor(() => {
204
+ expect(mockSubscribe).toHaveBeenCalled();
205
+ });
206
+
207
+ subscriptionHandler!({
208
+ id: 'msg-1',
209
+ role: 'user',
210
+ content: 'test message',
211
+ timestamp: 12345,
212
+ });
213
+
214
+ expect(mockChatAddMessage).toHaveBeenCalledWith({
215
+ id: 'msg-1',
216
+ role: 'user',
217
+ content: 'test message',
218
+ timestamp: 12345,
219
+ });
220
+ });
217
221
  });