@dyyz1993/create-agent 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (358) hide show
  1. package/package.json +25 -0
  2. package/src/__tests__/cli.test.ts +85 -0
  3. package/src/__tests__/copy.test.ts +168 -0
  4. package/src/__tests__/templates.test.ts +87 -0
  5. package/src/cli.ts +65 -0
  6. package/src/commands/create.ts +49 -0
  7. package/src/commands/list.ts +12 -0
  8. package/src/commands/status.ts +85 -0
  9. package/src/commands/workspace.ts +129 -0
  10. package/src/lib/copy.ts +285 -0
  11. package/src/lib/templates.ts +57 -0
  12. package/src/lib/types.ts +6 -0
  13. package/templates/agent/.env.example +7 -0
  14. package/templates/agent/LICENSE +21 -0
  15. package/templates/agent/README.md +87 -0
  16. package/templates/agent/bun.lock +1279 -0
  17. package/templates/agent/electrobun.config.ts +27 -0
  18. package/templates/agent/eslint.config.mjs +63 -0
  19. package/templates/agent/llms.txt +24 -0
  20. package/templates/agent/package-lock.json +3670 -0
  21. package/templates/agent/package.json +59 -0
  22. package/templates/agent/postcss.config.js +6 -0
  23. package/templates/agent/scripts/dev.ts +138 -0
  24. package/templates/agent/src/__tests__/server-config.test.ts +59 -0
  25. package/templates/agent/src/bun/index.ts +73 -0
  26. package/templates/agent/src/gateway/__tests__/ws-handler.test.ts +48 -0
  27. package/templates/agent/src/gateway/http-routes.ts +1 -0
  28. package/templates/agent/src/gateway/ipc-transport.ts +68 -0
  29. package/templates/agent/src/gateway/ws-handler.ts +86 -0
  30. package/templates/agent/src/mainview/App.tsx +41 -0
  31. package/templates/agent/src/mainview/__tests__/components/ChatPanel.test.tsx +114 -0
  32. package/templates/agent/src/mainview/__tests__/components/ExplorerSidebar.test.tsx +141 -0
  33. package/templates/agent/src/mainview/__tests__/components/MessageBubble.test.tsx +72 -0
  34. package/templates/agent/src/mainview/__tests__/hooks/use-input-history.test.ts +125 -0
  35. package/templates/agent/src/mainview/__tests__/hooks/use-rpc-init.test.ts +217 -0
  36. package/templates/agent/src/mainview/__tests__/hooks/use-sidebar-resize.test.ts +127 -0
  37. package/templates/agent/src/mainview/__tests__/i18n/i18n.test.ts +48 -0
  38. package/templates/agent/src/mainview/__tests__/setup.ts +37 -0
  39. package/templates/agent/src/mainview/__tests__/stores/use-chat-store.test.ts +83 -0
  40. package/templates/agent/src/mainview/__tests__/stores/use-sidebar-store.test.ts +85 -0
  41. package/templates/agent/src/mainview/components/activity-bar/ActivityBar.tsx +36 -0
  42. package/templates/agent/src/mainview/components/activity-bar/MobileTabBar.tsx +36 -0
  43. package/templates/agent/src/mainview/components/activity-bar/__tests__/ActivityBar.test.tsx +44 -0
  44. package/templates/agent/src/mainview/components/activity-bar/__tests__/MobileTabBar.test.tsx +44 -0
  45. package/templates/agent/src/mainview/components/bash/BashPanel.tsx +206 -0
  46. package/templates/agent/src/mainview/components/bash/__tests__/BashPanel.test.tsx +103 -0
  47. package/templates/agent/src/mainview/components/chat/ChatPanel.tsx +102 -0
  48. package/templates/agent/src/mainview/components/chat/MessageBubble.tsx +86 -0
  49. package/templates/agent/src/mainview/components/common/ErrorBoundary.tsx +58 -0
  50. package/templates/agent/src/mainview/components/common/LanguageSwitcher.tsx +14 -0
  51. package/templates/agent/src/mainview/components/common/ThemeToggle.tsx +24 -0
  52. package/templates/agent/src/mainview/components/common/__tests__/ErrorBoundary.test.tsx +74 -0
  53. package/templates/agent/src/mainview/components/common/__tests__/LanguageSwitcher.test.tsx +44 -0
  54. package/templates/agent/src/mainview/components/common/__tests__/ThemeToggle.test.tsx +41 -0
  55. package/templates/agent/src/mainview/components/debug/DebugPanel.tsx +120 -0
  56. package/templates/agent/src/mainview/components/debug/__tests__/DebugPanel.test.tsx +102 -0
  57. package/templates/agent/src/mainview/components/diff/DiffViewerPanel.tsx +106 -0
  58. package/templates/agent/src/mainview/components/diff/__tests__/DiffViewerPanel.test.tsx +73 -0
  59. package/templates/agent/src/mainview/components/explorer/ConfirmDialog.tsx +53 -0
  60. package/templates/agent/src/mainview/components/explorer/ContextMenu.tsx +82 -0
  61. package/templates/agent/src/mainview/components/explorer/ExplorerSidebar.tsx +221 -0
  62. package/templates/agent/src/mainview/components/explorer/InlineInput.tsx +55 -0
  63. package/templates/agent/src/mainview/components/explorer/TreeNodeItem.tsx +121 -0
  64. package/templates/agent/src/mainview/components/explorer/__tests__/ConfirmDialog.test.tsx +37 -0
  65. package/templates/agent/src/mainview/components/explorer/__tests__/ContextMenu.test.tsx +49 -0
  66. package/templates/agent/src/mainview/components/explorer/__tests__/InlineInput.test.tsx +48 -0
  67. package/templates/agent/src/mainview/components/explorer/__tests__/TreeNodeItem.test.tsx +146 -0
  68. package/templates/agent/src/mainview/components/feed/FeedPanel.tsx +239 -0
  69. package/templates/agent/src/mainview/components/feed/__tests__/FeedPanel.test.tsx +108 -0
  70. package/templates/agent/src/mainview/components/file-preview/FilePreviewOverlay.tsx +56 -0
  71. package/templates/agent/src/mainview/components/file-preview/VirtualizedCodeView.tsx +99 -0
  72. package/templates/agent/src/mainview/components/file-preview/__tests__/FilePreviewOverlay.test.tsx +96 -0
  73. package/templates/agent/src/mainview/components/file-preview/__tests__/VirtualizedCodeView.test.tsx +58 -0
  74. package/templates/agent/src/mainview/components/git/GitBranchSelector.tsx +98 -0
  75. package/templates/agent/src/mainview/components/git/GitCommitInput.tsx +60 -0
  76. package/templates/agent/src/mainview/components/git/GitPanel.tsx +528 -0
  77. package/templates/agent/src/mainview/components/git/__tests__/GitBranchSelector.test.tsx +76 -0
  78. package/templates/agent/src/mainview/components/git/__tests__/GitCommitInput.test.tsx +91 -0
  79. package/templates/agent/src/mainview/components/git/__tests__/GitPanel.test.tsx +174 -0
  80. package/templates/agent/src/mainview/components/layout/AppLayout.tsx +170 -0
  81. package/templates/agent/src/mainview/components/layout/__tests__/AppLayout.test.tsx +163 -0
  82. package/templates/agent/src/mainview/components/rules/RulesPanel.tsx +117 -0
  83. package/templates/agent/src/mainview/components/rules/__tests__/RulesPanel.test.tsx +91 -0
  84. package/templates/agent/src/mainview/components/search/SearchPanel.tsx +404 -0
  85. package/templates/agent/src/mainview/components/search/__tests__/SearchPanel.test.tsx +50 -0
  86. package/templates/agent/src/mainview/components/sidebar/PinButton.tsx +20 -0
  87. package/templates/agent/src/mainview/components/sidebar/__tests__/PinButton.test.tsx +48 -0
  88. package/templates/agent/src/mainview/components/todo/TodoPanel.tsx +141 -0
  89. package/templates/agent/src/mainview/components/todo/__tests__/TodoPanel.test.tsx +91 -0
  90. package/templates/agent/src/mainview/global.d.ts +13 -0
  91. package/templates/agent/src/mainview/hooks/__tests__/use-breakpoint.test.ts +147 -0
  92. package/templates/agent/src/mainview/hooks/use-breakpoint.ts +34 -0
  93. package/templates/agent/src/mainview/hooks/use-input-history.ts +84 -0
  94. package/templates/agent/src/mainview/hooks/use-rpc-init.ts +61 -0
  95. package/templates/agent/src/mainview/hooks/use-sidebar-resize.ts +40 -0
  96. package/templates/agent/src/mainview/index.css +61 -0
  97. package/templates/agent/src/mainview/index.html +12 -0
  98. package/templates/agent/src/mainview/lib/api-client.ts +192 -0
  99. package/templates/agent/src/mainview/lib/i18n/index.ts +30 -0
  100. package/templates/agent/src/mainview/lib/i18n/locales/en.json +157 -0
  101. package/templates/agent/src/mainview/lib/i18n/locales/zh.json +157 -0
  102. package/templates/agent/src/mainview/lib/rpc-cache.ts +94 -0
  103. package/templates/agent/src/mainview/main.tsx +24 -0
  104. package/templates/agent/src/mainview/stores/__tests__/use-app-store.test.ts +86 -0
  105. package/templates/agent/src/mainview/stores/__tests__/use-bash-store.test.ts +99 -0
  106. package/templates/agent/src/mainview/stores/__tests__/use-connection-store.test.ts +26 -0
  107. package/templates/agent/src/mainview/stores/__tests__/use-explorer-store.test.ts +130 -0
  108. package/templates/agent/src/mainview/stores/__tests__/use-feed-store.test.ts +132 -0
  109. package/templates/agent/src/mainview/stores/__tests__/use-git-store.test.ts +161 -0
  110. package/templates/agent/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
  111. package/templates/agent/src/mainview/stores/__tests__/use-log-store.test.ts +28 -0
  112. package/templates/agent/src/mainview/stores/__tests__/use-notification-store.test.ts +102 -0
  113. package/templates/agent/src/mainview/stores/__tests__/use-rules-store.test.ts +75 -0
  114. package/templates/agent/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
  115. package/templates/agent/src/mainview/stores/__tests__/use-todo-store.test.ts +75 -0
  116. package/templates/agent/src/mainview/stores/message-batcher.ts +25 -0
  117. package/templates/agent/src/mainview/stores/use-app-store.ts +99 -0
  118. package/templates/agent/src/mainview/stores/use-bash-store.ts +93 -0
  119. package/templates/agent/src/mainview/stores/use-chat-store.ts +37 -0
  120. package/templates/agent/src/mainview/stores/use-connection-store.ts +42 -0
  121. package/templates/agent/src/mainview/stores/use-explorer-store.ts +320 -0
  122. package/templates/agent/src/mainview/stores/use-feed-store.ts +129 -0
  123. package/templates/agent/src/mainview/stores/use-git-store.ts +291 -0
  124. package/templates/agent/src/mainview/stores/use-locale-store.ts +27 -0
  125. package/templates/agent/src/mainview/stores/use-log-store.ts +16 -0
  126. package/templates/agent/src/mainview/stores/use-notification-store.ts +82 -0
  127. package/templates/agent/src/mainview/stores/use-rules-store.ts +54 -0
  128. package/templates/agent/src/mainview/stores/use-sidebar-store.ts +99 -0
  129. package/templates/agent/src/mainview/stores/use-theme-store.ts +46 -0
  130. package/templates/agent/src/mainview/stores/use-todo-store.ts +54 -0
  131. package/templates/agent/src/mainview/types/index.ts +33 -0
  132. package/templates/agent/src/mainview/utils/constants.ts +1 -0
  133. package/templates/agent/src/mainview/utils/drop-handler.ts +150 -0
  134. package/templates/agent/src/mainview/utils/file-icon.tsx +24 -0
  135. package/templates/agent/src/mainview/utils/file-utils.ts +35 -0
  136. package/templates/agent/src/server-config.ts +43 -0
  137. package/templates/agent/src/server.ts +89 -0
  138. package/templates/agent/src/shared/handlers/__tests__/bash-handler.test.ts +135 -0
  139. package/templates/agent/src/shared/handlers/__tests__/chat-handler.test.ts +77 -0
  140. package/templates/agent/src/shared/handlers/__tests__/feed-handler.test.ts +108 -0
  141. package/templates/agent/src/shared/handlers/__tests__/file-handler.test.ts +100 -0
  142. package/templates/agent/src/shared/handlers/__tests__/system-handler.test.ts +55 -0
  143. package/templates/agent/src/shared/handlers/__tests__/timer-handler.test.ts +77 -0
  144. package/templates/agent/src/shared/handlers/bash.ts +89 -0
  145. package/templates/agent/src/shared/handlers/chat.ts +179 -0
  146. package/templates/agent/src/shared/handlers/feed.ts +53 -0
  147. package/templates/agent/src/shared/handlers/file.ts +99 -0
  148. package/templates/agent/src/shared/handlers/git.ts +263 -0
  149. package/templates/agent/src/shared/handlers/index.ts +10 -0
  150. package/templates/agent/src/shared/handlers/rules.ts +45 -0
  151. package/templates/agent/src/shared/handlers/system.ts +27 -0
  152. package/templates/agent/src/shared/handlers/timer.ts +34 -0
  153. package/templates/agent/src/shared/handlers/todo.ts +45 -0
  154. package/templates/agent/src/shared/lib/__tests__/bash-security.test.ts +125 -0
  155. package/templates/agent/src/shared/lib/__tests__/logger.test.ts +92 -0
  156. package/templates/agent/src/shared/lib/__tests__/path-security.test.ts +77 -0
  157. package/templates/agent/src/shared/lib/bash-security.ts +64 -0
  158. package/templates/agent/src/shared/lib/logger.ts +130 -0
  159. package/templates/agent/src/shared/lib/path-security.ts +38 -0
  160. package/templates/agent/src/shared/lib/port-registry.ts +103 -0
  161. package/templates/agent/src/shared/modules/bash.ts +19 -0
  162. package/templates/agent/src/shared/modules/chat.ts +20 -0
  163. package/templates/agent/src/shared/modules/feed.ts +30 -0
  164. package/templates/agent/src/shared/modules/file.ts +40 -0
  165. package/templates/agent/src/shared/modules/git.ts +81 -0
  166. package/templates/agent/src/shared/modules/rules.ts +25 -0
  167. package/templates/agent/src/shared/modules/system.ts +8 -0
  168. package/templates/agent/src/shared/modules/timer.ts +11 -0
  169. package/templates/agent/src/shared/modules/todo.ts +27 -0
  170. package/templates/agent/src/shared/register-all-handlers.ts +36 -0
  171. package/templates/agent/src/shared/rpc-schema.ts +35 -0
  172. package/templates/agent/tailwind.config.js +15 -0
  173. package/templates/agent/tsconfig.json +26 -0
  174. package/templates/agent/vite.config.ts +3 -0
  175. package/templates/agent/vitest.config.ts +3 -0
  176. package/templates/chat/.env.example +6 -0
  177. package/templates/chat/LICENSE +21 -0
  178. package/templates/chat/README.md +56 -0
  179. package/templates/chat/bun.lock +1203 -0
  180. package/templates/chat/electrobun.config.ts +27 -0
  181. package/templates/chat/eslint.config.mjs +56 -0
  182. package/templates/chat/llms.txt +24 -0
  183. package/templates/chat/package-lock.json +3670 -0
  184. package/templates/chat/package.json +58 -0
  185. package/templates/chat/postcss.config.js +6 -0
  186. package/templates/chat/scripts/dev.ts +138 -0
  187. package/templates/chat/src/__tests__/server-config.test.ts +59 -0
  188. package/templates/chat/src/bun/index.ts +72 -0
  189. package/templates/chat/src/gateway/__tests__/ws-handler.test.ts +48 -0
  190. package/templates/chat/src/gateway/http-routes.ts +1 -0
  191. package/templates/chat/src/gateway/ipc-transport.ts +68 -0
  192. package/templates/chat/src/gateway/ws-handler.ts +86 -0
  193. package/templates/chat/src/mainview/App.tsx +108 -0
  194. package/templates/chat/src/mainview/__tests__/components/ChatPanel.test.tsx +76 -0
  195. package/templates/chat/src/mainview/__tests__/components/MessageBubble.test.tsx +48 -0
  196. package/templates/chat/src/mainview/__tests__/i18n/i18n.test.ts +48 -0
  197. package/templates/chat/src/mainview/__tests__/setup-verify.test.ts +8 -0
  198. package/templates/chat/src/mainview/__tests__/setup.ts +37 -0
  199. package/templates/chat/src/mainview/__tests__/stores/use-app-store.test.ts +46 -0
  200. package/templates/chat/src/mainview/__tests__/stores/use-chat-store.test.ts +104 -0
  201. package/templates/chat/src/mainview/__tests__/stores/use-connection-store.test.ts +35 -0
  202. package/templates/chat/src/mainview/__tests__/stores/use-log-store.test.ts +45 -0
  203. package/templates/chat/src/mainview/__tests__/stores/use-notification-store.test.ts +102 -0
  204. package/templates/chat/src/mainview/__tests__/stores/use-sidebar-store.test.ts +89 -0
  205. package/templates/chat/src/mainview/components/activity-bar/ActivityBar.tsx +26 -0
  206. package/templates/chat/src/mainview/components/activity-bar/MobileTabBar.tsx +20 -0
  207. package/templates/chat/src/mainview/components/chat/ChatPanel.tsx +66 -0
  208. package/templates/chat/src/mainview/components/chat/MessageBubble.tsx +85 -0
  209. package/templates/chat/src/mainview/components/common/LanguageSwitcher.tsx +15 -0
  210. package/templates/chat/src/mainview/components/common/ThemeToggle.tsx +24 -0
  211. package/templates/chat/src/mainview/components/sidebar/PinButton.tsx +20 -0
  212. package/templates/chat/src/mainview/global.d.ts +13 -0
  213. package/templates/chat/src/mainview/hooks/use-breakpoint.ts +34 -0
  214. package/templates/chat/src/mainview/hooks/use-input-history.ts +84 -0
  215. package/templates/chat/src/mainview/index.css +61 -0
  216. package/templates/chat/src/mainview/index.html +12 -0
  217. package/templates/chat/src/mainview/lib/api-client.ts +175 -0
  218. package/templates/chat/src/mainview/lib/i18n/index.ts +30 -0
  219. package/templates/chat/src/mainview/lib/i18n/locales/en.json +157 -0
  220. package/templates/chat/src/mainview/lib/i18n/locales/zh.json +157 -0
  221. package/templates/chat/src/mainview/main.tsx +20 -0
  222. package/templates/chat/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
  223. package/templates/chat/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
  224. package/templates/chat/src/mainview/stores/message-batcher.ts +25 -0
  225. package/templates/chat/src/mainview/stores/use-app-store.ts +50 -0
  226. package/templates/chat/src/mainview/stores/use-chat-store.ts +37 -0
  227. package/templates/chat/src/mainview/stores/use-connection-store.ts +42 -0
  228. package/templates/chat/src/mainview/stores/use-locale-store.ts +27 -0
  229. package/templates/chat/src/mainview/stores/use-log-store.ts +16 -0
  230. package/templates/chat/src/mainview/stores/use-notification-store.ts +82 -0
  231. package/templates/chat/src/mainview/stores/use-sidebar-store.ts +98 -0
  232. package/templates/chat/src/mainview/stores/use-theme-store.ts +46 -0
  233. package/templates/chat/src/mainview/types/index.ts +8 -0
  234. package/templates/chat/src/mainview/utils/constants.ts +1 -0
  235. package/templates/chat/src/server-config.ts +42 -0
  236. package/templates/chat/src/server.ts +89 -0
  237. package/templates/chat/src/shared/__tests__/chat-handler.test.ts +71 -0
  238. package/templates/chat/src/shared/__tests__/system-handler.test.ts +70 -0
  239. package/templates/chat/src/shared/handlers/chat.ts +186 -0
  240. package/templates/chat/src/shared/handlers/index.ts +2 -0
  241. package/templates/chat/src/shared/handlers/system.ts +27 -0
  242. package/templates/chat/src/shared/lib/logger.ts +130 -0
  243. package/templates/chat/src/shared/lib/path-security.ts +38 -0
  244. package/templates/chat/src/shared/lib/port-registry.ts +103 -0
  245. package/templates/chat/src/shared/modules/chat.ts +20 -0
  246. package/templates/chat/src/shared/modules/system.ts +8 -0
  247. package/templates/chat/src/shared/register-all-handlers.ts +36 -0
  248. package/templates/chat/src/shared/rpc-schema.ts +11 -0
  249. package/templates/chat/tailwind.config.js +15 -0
  250. package/templates/chat/tsconfig.json +26 -0
  251. package/templates/chat/vite.config.ts +3 -0
  252. package/templates/chat/vitest.config.ts +3 -0
  253. package/templates/general/.env.example +7 -0
  254. package/templates/general/LICENSE +21 -0
  255. package/templates/general/README.md +113 -0
  256. package/templates/general/bun.lock +1266 -0
  257. package/templates/general/electrobun.config.ts +27 -0
  258. package/templates/general/eslint.config.mjs +56 -0
  259. package/templates/general/llms.txt +24 -0
  260. package/templates/general/package-lock.json +3670 -0
  261. package/templates/general/package.json +59 -0
  262. package/templates/general/postcss.config.js +6 -0
  263. package/templates/general/scripts/dev.ts +138 -0
  264. package/templates/general/src/__tests__/server-config.test.ts +59 -0
  265. package/templates/general/src/bun/index.ts +72 -0
  266. package/templates/general/src/gateway/http-routes.ts +1 -0
  267. package/templates/general/src/gateway/ipc-transport.ts +68 -0
  268. package/templates/general/src/gateway/ws-handler.ts +86 -0
  269. package/templates/general/src/mainview/App.tsx +40 -0
  270. package/templates/general/src/mainview/__tests__/components/ChatPanel.test.tsx +82 -0
  271. package/templates/general/src/mainview/__tests__/components/FeedPanel.test.tsx +109 -0
  272. package/templates/general/src/mainview/__tests__/components/GitPanel.test.tsx +151 -0
  273. package/templates/general/src/mainview/__tests__/i18n/i18n.test.ts +48 -0
  274. package/templates/general/src/mainview/__tests__/setup-verify.test.ts +8 -0
  275. package/templates/general/src/mainview/__tests__/setup.ts +37 -0
  276. package/templates/general/src/mainview/components/activity-bar/ActivityBar.tsx +40 -0
  277. package/templates/general/src/mainview/components/activity-bar/MobileTabBar.tsx +29 -0
  278. package/templates/general/src/mainview/components/chat/ChatPanel.tsx +66 -0
  279. package/templates/general/src/mainview/components/chat/MessageBubble.tsx +85 -0
  280. package/templates/general/src/mainview/components/common/LanguageSwitcher.tsx +15 -0
  281. package/templates/general/src/mainview/components/common/ThemeToggle.tsx +24 -0
  282. package/templates/general/src/mainview/components/debug/DebugPanel.tsx +176 -0
  283. package/templates/general/src/mainview/components/diff/DiffViewerPanel.tsx +108 -0
  284. package/templates/general/src/mainview/components/explorer/ConfirmDialog.tsx +52 -0
  285. package/templates/general/src/mainview/components/explorer/ContextMenu.tsx +82 -0
  286. package/templates/general/src/mainview/components/explorer/ExplorerSidebar.tsx +225 -0
  287. package/templates/general/src/mainview/components/explorer/InlineInput.tsx +55 -0
  288. package/templates/general/src/mainview/components/explorer/TreeNodeItem.tsx +121 -0
  289. package/templates/general/src/mainview/components/feed/FeedPanel.tsx +248 -0
  290. package/templates/general/src/mainview/components/file-preview/FilePreviewOverlay.tsx +55 -0
  291. package/templates/general/src/mainview/components/file-preview/VirtualizedCodeView.tsx +99 -0
  292. package/templates/general/src/mainview/components/git/GitBranchSelector.tsx +93 -0
  293. package/templates/general/src/mainview/components/git/GitCommitInput.tsx +60 -0
  294. package/templates/general/src/mainview/components/git/GitPanel.tsx +533 -0
  295. package/templates/general/src/mainview/components/layout/AppLayout.tsx +146 -0
  296. package/templates/general/src/mainview/components/search/SearchPanel.tsx +405 -0
  297. package/templates/general/src/mainview/components/sidebar/PinButton.tsx +20 -0
  298. package/templates/general/src/mainview/global.d.ts +13 -0
  299. package/templates/general/src/mainview/hooks/use-breakpoint.ts +34 -0
  300. package/templates/general/src/mainview/hooks/use-input-history.ts +84 -0
  301. package/templates/general/src/mainview/hooks/use-rpc-init.ts +61 -0
  302. package/templates/general/src/mainview/hooks/use-sidebar-resize.ts +40 -0
  303. package/templates/general/src/mainview/index.css +61 -0
  304. package/templates/general/src/mainview/index.html +12 -0
  305. package/templates/general/src/mainview/lib/api-client.ts +175 -0
  306. package/templates/general/src/mainview/lib/i18n/index.ts +30 -0
  307. package/templates/general/src/mainview/lib/i18n/locales/en.json +157 -0
  308. package/templates/general/src/mainview/lib/i18n/locales/zh.json +157 -0
  309. package/templates/general/src/mainview/main.tsx +20 -0
  310. package/templates/general/src/mainview/stores/__tests__/use-chat-store.test.ts +104 -0
  311. package/templates/general/src/mainview/stores/__tests__/use-connection-store.test.ts +35 -0
  312. package/templates/general/src/mainview/stores/__tests__/use-locale-store.test.ts +32 -0
  313. package/templates/general/src/mainview/stores/__tests__/use-log-store.test.ts +45 -0
  314. package/templates/general/src/mainview/stores/__tests__/use-theme-store.test.ts +59 -0
  315. package/templates/general/src/mainview/stores/message-batcher.ts +25 -0
  316. package/templates/general/src/mainview/stores/use-app-store.ts +104 -0
  317. package/templates/general/src/mainview/stores/use-chat-store.ts +37 -0
  318. package/templates/general/src/mainview/stores/use-connection-store.ts +42 -0
  319. package/templates/general/src/mainview/stores/use-explorer-store.ts +315 -0
  320. package/templates/general/src/mainview/stores/use-feed-store.ts +129 -0
  321. package/templates/general/src/mainview/stores/use-git-store.ts +284 -0
  322. package/templates/general/src/mainview/stores/use-locale-store.ts +27 -0
  323. package/templates/general/src/mainview/stores/use-log-store.ts +16 -0
  324. package/templates/general/src/mainview/stores/use-notification-store.ts +82 -0
  325. package/templates/general/src/mainview/stores/use-sidebar-store.ts +99 -0
  326. package/templates/general/src/mainview/stores/use-theme-store.ts +46 -0
  327. package/templates/general/src/mainview/types/index.ts +33 -0
  328. package/templates/general/src/mainview/utils/constants.ts +1 -0
  329. package/templates/general/src/mainview/utils/drop-handler.ts +150 -0
  330. package/templates/general/src/mainview/utils/file-icon.tsx +24 -0
  331. package/templates/general/src/mainview/utils/file-utils.ts +35 -0
  332. package/templates/general/src/server-config.ts +42 -0
  333. package/templates/general/src/server.ts +89 -0
  334. package/templates/general/src/shared/handlers/__tests__/chat.test.ts +183 -0
  335. package/templates/general/src/shared/handlers/__tests__/system.test.ts +94 -0
  336. package/templates/general/src/shared/handlers/__tests__/timer.test.ts +113 -0
  337. package/templates/general/src/shared/handlers/chat.ts +179 -0
  338. package/templates/general/src/shared/handlers/feed.ts +53 -0
  339. package/templates/general/src/shared/handlers/file.ts +99 -0
  340. package/templates/general/src/shared/handlers/git.ts +263 -0
  341. package/templates/general/src/shared/handlers/index.ts +7 -0
  342. package/templates/general/src/shared/handlers/system.ts +27 -0
  343. package/templates/general/src/shared/handlers/timer.ts +34 -0
  344. package/templates/general/src/shared/lib/logger.ts +130 -0
  345. package/templates/general/src/shared/lib/path-security.ts +38 -0
  346. package/templates/general/src/shared/lib/port-registry.ts +103 -0
  347. package/templates/general/src/shared/modules/chat.ts +20 -0
  348. package/templates/general/src/shared/modules/feed.ts +30 -0
  349. package/templates/general/src/shared/modules/file.ts +40 -0
  350. package/templates/general/src/shared/modules/git.ts +81 -0
  351. package/templates/general/src/shared/modules/system.ts +8 -0
  352. package/templates/general/src/shared/modules/timer.ts +11 -0
  353. package/templates/general/src/shared/register-all-handlers.ts +36 -0
  354. package/templates/general/src/shared/rpc-schema.ts +31 -0
  355. package/templates/general/tailwind.config.js +15 -0
  356. package/templates/general/tsconfig.json +26 -0
  357. package/templates/general/vite.config.ts +3 -0
  358. package/templates/general/vitest.config.ts +3 -0
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "pi-chat-app",
3
+ "version": "1.0.0",
4
+ "description": "Chat-focused template",
5
+ "scripts": {
6
+ "start": "vite build && electrobun dev",
7
+ "dev": "electrobun dev --watch",
8
+ "dev:web": "bun scripts/dev.ts",
9
+ "dev:server": "bun src/server.ts",
10
+ "build": "vite build",
11
+ "build:canary": "vite build && electrobun build --env=canary",
12
+ "lint": "eslint .",
13
+ "lint:fix": "eslint . --fix",
14
+ "prepare": "husky",
15
+ "test": "vitest run",
16
+ "test:watch": "vitest",
17
+ "test:coverage": "vitest run --coverage"
18
+ },
19
+ "dependencies": {
20
+ "@dyyz1993/rpc-core": "^1.2.0",
21
+ "@tanstack/react-virtual": "^3.13.24",
22
+ "electrobun": "1.13.1",
23
+ "i18next": "^24.2.2",
24
+ "i18next-browser-languagedetector": "^8.0.4",
25
+ "lucide-react": "^1.9.0",
26
+ "prism-react-renderer": "^2.4.1",
27
+ "react": "^18.3.1",
28
+ "react-dom": "^18.3.1",
29
+ "react-i18next": "^15.4.1",
30
+ "react-markdown": "^10.1.0",
31
+ "remark-gfm": "^4.0.1",
32
+ "zustand": "^5.0.12"
33
+ },
34
+ "devDependencies": {
35
+ "@dyyz1993/eslint-plugin-rpc": "workspace:*",
36
+ "@eslint/js": "^9.0.0",
37
+ "@testing-library/jest-dom": "^6.9.1",
38
+ "@testing-library/react": "^16.3.2",
39
+ "@testing-library/user-event": "^14.6.1",
40
+ "@types/bun": "latest",
41
+ "@types/react": "^18.3.12",
42
+ "@types/react-dom": "^18.3.1",
43
+ "@types/ws": "^8.18.1",
44
+ "@vitejs/plugin-react": "^4.3.4",
45
+ "autoprefixer": "^10.4.20",
46
+ "concurrently": "^9.1.0",
47
+ "eslint": "^9.0.0",
48
+ "happy-dom": "^20.9.0",
49
+ "husky": "^9.1.0",
50
+ "jsdom": "^29.1.1",
51
+ "postcss": "^8.4.49",
52
+ "tailwindcss": "^3.4.16",
53
+ "typescript": "^5.7.2",
54
+ "typescript-eslint": "^8.0.0",
55
+ "vite": "^6.0.3",
56
+ "vitest": "^4.1.5"
57
+ }
58
+ }
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ plugins: {
3
+ tailwindcss: {},
4
+ autoprefixer: {},
5
+ },
6
+ };
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Dev orchestration script — single command to start backend + Vite.
3
+ *
4
+ * Usage: bun run dev:web
5
+ *
6
+ * 1. Starts backend server (dynamic port, writes to .server-port)
7
+ * 2. Waits for .server-port to appear
8
+ * 3. Starts Vite dev server (reads backend port for proxy)
9
+ * 4. On Ctrl+C / SIGTERM: kills both processes, cleans up .server-port
10
+ */
11
+
12
+ import { spawn } from "child_process";
13
+ import { existsSync, readFileSync, unlinkSync } from "fs";
14
+ import { resolve } from "path";
15
+
16
+ const ROOT = resolve(import.meta.dir, "..");
17
+ const PORT_FILE = resolve(ROOT, ".server-port");
18
+ const MAX_WAIT_MS = 10000;
19
+ const POLL_INTERVAL_MS = 100;
20
+
21
+ let backend: ReturnType<typeof spawn> | null = null;
22
+ let vite: ReturnType<typeof spawn> | null = null;
23
+ let shuttingDown = false;
24
+
25
+ function cleanup() {
26
+ if (shuttingDown) return;
27
+ shuttingDown = true;
28
+
29
+ if (vite && !vite.killed) {
30
+ vite.kill("SIGTERM");
31
+ }
32
+ if (backend && !backend.killed) {
33
+ backend.kill("SIGTERM");
34
+ }
35
+ try { if (existsSync(PORT_FILE)) unlinkSync(PORT_FILE); } catch { /* ignore cleanup errors */ }
36
+
37
+ setTimeout(() => {
38
+ if (backend && !backend.killed) backend.kill("SIGKILL");
39
+ if (vite && !vite.killed) vite.kill("SIGKILL");
40
+ process.exit(0);
41
+ }, 3000);
42
+ }
43
+
44
+ process.on("SIGINT", cleanup);
45
+ process.on("SIGTERM", cleanup);
46
+ process.on("exit", () => {
47
+ try { if (existsSync(PORT_FILE)) unlinkSync(PORT_FILE); } catch { /* ignore */ }
48
+ });
49
+
50
+ function log(tag: string, msg: string) {
51
+ console.log(`[${tag}] ${msg}`);
52
+ }
53
+
54
+ function waitForPortFile(): Promise<number> {
55
+ return new Promise((resolve, reject) => {
56
+ const start = Date.now();
57
+ const check = () => {
58
+ if (existsSync(PORT_FILE)) {
59
+ try {
60
+ const port = parseInt(readFileSync(PORT_FILE, "utf-8").trim());
61
+ if (port > 0) { resolve(port); return; }
62
+ } catch { /* ignore parse errors */ }
63
+ }
64
+ if (Date.now() - start > MAX_WAIT_MS) {
65
+ reject(new Error("Timed out waiting for backend to start"));
66
+ return;
67
+ }
68
+ setTimeout(check, POLL_INTERVAL_MS);
69
+ };
70
+ check();
71
+ });
72
+ }
73
+
74
+ async function main() {
75
+ log("dev", "Starting backend server...");
76
+
77
+ backend = spawn("bun", ["src/server.ts"], {
78
+ cwd: ROOT,
79
+ stdio: ["pipe", "pipe", "pipe"],
80
+ });
81
+
82
+ backend.stdout?.on("data", (data: Buffer) => {
83
+ data.toString().split("\n").filter(Boolean).forEach((line) => {
84
+ log("backend", line);
85
+ });
86
+ });
87
+
88
+ backend.stderr?.on("data", (data: Buffer) => {
89
+ data.toString().split("\n").filter(Boolean).forEach((line) => {
90
+ log("backend", line);
91
+ });
92
+ });
93
+
94
+ backend.on("exit", (code) => {
95
+ if (!shuttingDown) {
96
+ log("backend", `exited with code ${code}`);
97
+ cleanup();
98
+ }
99
+ });
100
+
101
+ const backendPort = await waitForPortFile();
102
+ log("dev", `Backend ready on port ${backendPort}`);
103
+
104
+ log("dev", "Starting Vite dev server...");
105
+ vite = spawn("vite", [], {
106
+ cwd: ROOT,
107
+ stdio: ["pipe", "pipe", "pipe"],
108
+ env: { ...process.env },
109
+ });
110
+
111
+ vite.stdout?.on("data", (data: Buffer) => {
112
+ data.toString().split("\n").filter(Boolean).forEach((line) => {
113
+ log("vite", line);
114
+ });
115
+ });
116
+
117
+ vite.stderr?.on("data", (data: Buffer) => {
118
+ data.toString().split("\n").filter(Boolean).forEach((line) => {
119
+ log("vite", line);
120
+ });
121
+ });
122
+
123
+ vite.on("exit", (code) => {
124
+ if (!shuttingDown) {
125
+ log("vite", `exited with code ${code}`);
126
+ cleanup();
127
+ }
128
+ });
129
+
130
+ log("dev", `Proxy: Vite → http://localhost:${backendPort}`);
131
+ log("dev", "Press Ctrl+C to stop both servers");
132
+ }
133
+
134
+ main().catch((err) => {
135
+ console.error("Dev startup failed:", err.message);
136
+ cleanup();
137
+ process.exit(1);
138
+ });
@@ -0,0 +1,59 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { parseEnvInt } from "../server-config";
3
+
4
+ describe("parseEnvInt", () => {
5
+ it("should return default when value is undefined", () => {
6
+ expect(parseEnvInt("TEST_KEY", undefined, 3100, 1024, 65535)).toBe(3100);
7
+ });
8
+
9
+ it("should return default when value is empty string", () => {
10
+ expect(parseEnvInt("TEST_KEY", "", 3100, 1024, 65535)).toBe(3100);
11
+ });
12
+
13
+ it("should parse valid number", () => {
14
+ expect(parseEnvInt("TEST_KEY", "8080", 3100, 1024, 65535)).toBe(8080);
15
+ });
16
+
17
+ it("should return default for NaN input", () => {
18
+ expect(parseEnvInt("TEST_KEY", "abc", 3100, 1024, 65535)).toBe(3100);
19
+ });
20
+
21
+ it("should return default for out-of-range value (too low)", () => {
22
+ expect(parseEnvInt("TEST_KEY", "80", 3100, 1024, 65535)).toBe(3100);
23
+ });
24
+
25
+ it("should return default for out-of-range value (too high)", () => {
26
+ expect(parseEnvInt("TEST_KEY", "99999", 3100, 1024, 65535)).toBe(3100);
27
+ });
28
+
29
+ it("should accept boundary min value", () => {
30
+ expect(parseEnvInt("TEST_KEY", "1024", 3100, 1024, 65535)).toBe(1024);
31
+ });
32
+
33
+ it("should accept boundary max value", () => {
34
+ expect(parseEnvInt("TEST_KEY", "65535", 3100, 1024, 65535)).toBe(65535);
35
+ });
36
+ });
37
+
38
+ describe("server-config exports", () => {
39
+ it("should export config with all required fields", async () => {
40
+ const { config } = await import("../server-config");
41
+ expect(config).toHaveProperty("port");
42
+ expect(config).toHaveProperty("authToken");
43
+ expect(config).toHaveProperty("maxUploadSize");
44
+ expect(config).toHaveProperty("logDir");
45
+ expect(config).toHaveProperty("corsOrigin");
46
+ expect(typeof config.port).toBe("number");
47
+ expect(typeof config.authToken).toBe("string");
48
+ expect(typeof config.maxUploadSize).toBe("number");
49
+ expect(typeof config.corsOrigin).toBe("string");
50
+ });
51
+
52
+ it("should have sensible defaults", async () => {
53
+ const { config } = await import("../server-config");
54
+ expect(config.port).toBeGreaterThanOrEqual(1024);
55
+ expect(config.port).toBeLessThanOrEqual(65535);
56
+ expect(config.maxUploadSize).toBeGreaterThan(0);
57
+ expect(config.corsOrigin).toBeTruthy();
58
+ });
59
+ });
@@ -0,0 +1,72 @@
1
+ import { BrowserWindow, BrowserView, Updater, ApplicationMenu } from "electrobun/bun";
2
+ import { RPCServer } from "@dyyz1993/rpc-core";
3
+ import { ElectrobunTransport } from "../gateway/ipc-transport";
4
+ import { registerAllHandlers } from "../shared/register-all-handlers";
5
+ import { createLogger, configureLogDir } from "../shared/lib/logger";
6
+
7
+ configureLogDir("logs");
8
+ const log = createLogger("server");
9
+
10
+ async function getMainViewUrl(): Promise<string> {
11
+ const channel = await Updater.localInfo.channel();
12
+ const DEV_SERVER_URL = "http://localhost:5173";
13
+ if (channel === "dev") {
14
+ try {
15
+ await fetch(DEV_SERVER_URL, { method: "HEAD" });
16
+ log.info(`HMR enabled: Using Vite dev server at ${DEV_SERVER_URL}`);
17
+ return DEV_SERVER_URL;
18
+ } catch {
19
+ log.info("Vite dev server not running.");
20
+ }
21
+ }
22
+ return "views://mainview/index.html";
23
+ }
24
+
25
+ const url = await getMainViewUrl();
26
+
27
+ const transport = new ElectrobunTransport();
28
+ const server = new RPCServer(transport);
29
+
30
+ // --- 注册 RPC handlers(自动导入 handlers barrel) ---
31
+ registerAllHandlers(server, { platform: "desktop" });
32
+
33
+ // --- 创建窗口 ---
34
+
35
+ const mainWindow = new BrowserWindow({
36
+ title: "Pi Agent",
37
+ url,
38
+ frame: {
39
+ width: 1200,
40
+ height: 800,
41
+ x: 200,
42
+ y: 200,
43
+ },
44
+ rpc: BrowserView.defineRPC({
45
+ maxRequestTime: 60000,
46
+ handlers: {
47
+ requests: {},
48
+ messages: {
49
+ // @ts-expect-error Electrobun's messages schema doesn't support custom keys at compile time
50
+ "rpc-message": (data: unknown) => {
51
+ try {
52
+ const message = typeof data === "string" ? JSON.parse(data) : data;
53
+ transport.handleMessage(message);
54
+ } catch (error) {
55
+ log.error("Failed to parse RPC message", { error });
56
+ }
57
+ },
58
+ },
59
+ },
60
+ }),
61
+ });
62
+
63
+ transport.setBrowserView(mainWindow.webview);
64
+
65
+ log.info("Pi Agent desktop app started!");
66
+
67
+ ApplicationMenu.setApplicationMenu([
68
+ { label: "Pi Agent", submenu: [{ role: "about" }, { type: "separator" }, { role: "hide" }, { role: "hideOthers" }, { role: "showAll" }, { type: "separator" }, { role: "quit" }] },
69
+ { label: "Edit", submenu: [{ role: "undo" }, { role: "redo" }, { type: "separator" }, { role: "cut" }, { role: "copy" }, { role: "paste" }, { role: "selectAll" }] },
70
+ { label: "View", submenu: [{ role: "toggleFullScreen" }] },
71
+ { label: "Window", submenu: [{ role: "minimize" }, { role: "zoom" }] },
72
+ ]);
@@ -0,0 +1,48 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+
3
+ vi.mock("../../server-config", () => ({
4
+ config: {
5
+ authToken: "test-token",
6
+ port: 3100,
7
+ corsOrigin: "http://localhost:5173",
8
+ },
9
+ }));
10
+
11
+ vi.mock("../../shared/lib/logger", () => ({
12
+ createLogger: () => ({
13
+ info: vi.fn(),
14
+ error: vi.fn(),
15
+ warn: vi.fn(),
16
+ debug: vi.fn(),
17
+ }),
18
+ }));
19
+
20
+ describe("WebSocket Handler", () => {
21
+ beforeEach(() => {
22
+ vi.resetModules();
23
+ });
24
+
25
+ it("should reject connections without token", async () => {
26
+ const { config } = await import("../../server-config");
27
+
28
+ const token = null;
29
+ const isValid = token === config.authToken;
30
+ expect(isValid).toBe(false);
31
+ });
32
+
33
+ it("should reject wrong token", async () => {
34
+ const { config } = await import("../../server-config");
35
+
36
+ const token = "wrong-token";
37
+ const isValid = token === config.authToken;
38
+ expect(isValid).toBe(false);
39
+ });
40
+
41
+ it("should accept correct token", async () => {
42
+ const { config } = await import("../../server-config");
43
+
44
+ const token = "test-token";
45
+ const isValid = token === config.authToken;
46
+ expect(isValid).toBe(true);
47
+ });
48
+ });
@@ -0,0 +1 @@
1
+ export { createHttpHandler, type HttpRouteDeps } from "@shared/http-routes";
@@ -0,0 +1,68 @@
1
+ import type { Transport, MessageHandler, ErrorHandler } from "@dyyz1993/rpc-core";
2
+
3
+ /**
4
+ * Transport that bridges between Bun and Webview via:
5
+ * - Bun → Browser: executeJavascript calls window.__piAgentIPC()
6
+ * - Browser → Bun: __electrobunBunBridge.postMessage with Electrobun message format
7
+ */
8
+ export class ElectrobunTransport implements Transport {
9
+ private messageHandlers: Set<MessageHandler> = new Set();
10
+ private errorHandlers: Set<ErrorHandler> = new Set();
11
+ private browserView: { executeJavascript: (js: string) => void } | null = null;
12
+ private closed = false;
13
+
14
+ setBrowserView(view: { executeJavascript: (js: string) => void }): void {
15
+ this.browserView = view;
16
+ }
17
+
18
+ async send(message: unknown): Promise<void> {
19
+ if (this.closed) {
20
+ throw new Error("Transport is closed");
21
+ }
22
+ if (!this.browserView) {
23
+ throw new Error("BrowserView not set");
24
+ }
25
+
26
+ // Pass JSON directly as a JS expression — JSON is valid JavaScript
27
+ const msgJson = JSON.stringify(message);
28
+ this.browserView.executeJavascript(
29
+ `if(typeof window.__piAgentIPC==="function"){window.__piAgentIPC(${msgJson})}`
30
+ );
31
+ }
32
+
33
+ onMessage(handler: MessageHandler): () => void {
34
+ this.messageHandlers.add(handler);
35
+ return () => {
36
+ this.messageHandlers.delete(handler);
37
+ };
38
+ }
39
+
40
+ onError(handler: ErrorHandler): () => void {
41
+ this.errorHandlers.add(handler);
42
+ return () => {
43
+ this.errorHandlers.delete(handler);
44
+ };
45
+ }
46
+
47
+ onDisconnect(): () => void {
48
+ return () => {};
49
+ }
50
+
51
+ isConnected(): boolean {
52
+ return this.browserView !== null && !this.closed;
53
+ }
54
+
55
+ close(): void {
56
+ this.closed = true;
57
+ this.messageHandlers.clear();
58
+ this.errorHandlers.clear();
59
+ this.browserView = null;
60
+ }
61
+
62
+ handleMessage(message: unknown): void {
63
+ if (this.closed) return;
64
+ for (const handler of this.messageHandlers) {
65
+ handler(message);
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * WebSocket handler for the web gateway.
3
+ * Handles WS connections, auth, and bridges to RPCServer.
4
+ */
5
+
6
+ import type { Server } from "http";
7
+ import { WebSocketServer, WebSocket } from "ws";
8
+ import { timingSafeEqual } from "crypto";
9
+ import { RPCServer, type Transport } from "@dyyz1993/rpc-core";
10
+ import { registerAllHandlers } from "../shared/register-all-handlers";
11
+ import { createLogger } from "../shared/lib/logger";
12
+
13
+ const log = createLogger("gateway");
14
+
15
+ function safeEqual(a: string, b: string): boolean {
16
+ const bufA = Buffer.from(a);
17
+ const bufB = Buffer.from(b);
18
+ if (bufA.length !== bufB.length) return false;
19
+ return timingSafeEqual(bufA, bufB);
20
+ }
21
+
22
+ export interface WsHandlerDeps {
23
+ config: { readonly port: number; readonly authToken: string; readonly maxUploadSize: number };
24
+ }
25
+
26
+ export function createWsHandler(httpServer: Server, deps: WsHandlerDeps): WebSocketServer {
27
+ const { config: cfg } = deps;
28
+ const wss = new WebSocketServer({ server: httpServer, path: "/ws" });
29
+
30
+ wss.on("connection", (ws: WebSocket, req) => {
31
+ // Token 验证
32
+ const url = req.url ? new URL(req.url, "http://localhost") : null;
33
+ const token = url?.searchParams.get("token");
34
+ if (!safeEqual(token || "", cfg.authToken)) {
35
+ log.warn("Connection rejected: invalid token");
36
+ ws.close(4001, "Unauthorized");
37
+ return;
38
+ }
39
+
40
+ log.info("Client connected", { total: wss.clients.size });
41
+
42
+ const wsTransport = {
43
+ send: async (message: unknown): Promise<void> => {
44
+ if (ws.readyState === WebSocket.OPEN) {
45
+ ws.send(JSON.stringify(message));
46
+ }
47
+ },
48
+ onMessage: (handler: (message: unknown) => void): (() => void) => {
49
+ const listener = (data: Buffer) => {
50
+ try {
51
+ const msg = JSON.parse(data.toString());
52
+ handler(msg);
53
+ } catch (err) {
54
+ log.error("Failed to parse message", { error: err instanceof Error ? err.message : String(err) });
55
+ }
56
+ };
57
+ ws.on("message", listener);
58
+ return () => ws.off("message", listener);
59
+ },
60
+ onError: (): (() => void) => {
61
+ return () => {};
62
+ },
63
+ onDisconnect: (): (() => void) => {
64
+ return () => {};
65
+ },
66
+ isConnected: (): boolean => ws.readyState === WebSocket.OPEN,
67
+ close: (): void => {},
68
+ };
69
+
70
+ const rpcServer = new RPCServer(wsTransport as Transport);
71
+
72
+ // 自动导入并注册所有 handlers
73
+ registerAllHandlers(rpcServer, { platform: "web" });
74
+
75
+ ws.on("close", () => {
76
+ log.info("Client disconnected", { total: wss.clients.size });
77
+ rpcServer.close();
78
+ });
79
+
80
+ ws.on("error", (err: Error) => {
81
+ log.error("Client error", { error: err.message });
82
+ });
83
+ });
84
+
85
+ return wss;
86
+ }
@@ -0,0 +1,108 @@
1
+ import { useEffect } from "react";
2
+ import { Wifi, Monitor } from "lucide-react";
3
+ import { useTranslation } from "react-i18next";
4
+ import { apiClient } from "./lib/api-client";
5
+ import { useConnectionStore } from "./stores/use-connection-store";
6
+ import { useLogStore } from "./stores/use-log-store";
7
+ import { useChatStore } from "./stores/use-chat-store";
8
+ import { useSidebarStore } from "./stores/use-sidebar-store";
9
+ import { useBreakpointSync } from "./hooks/use-breakpoint";
10
+ import { ActivityBar } from "./components/activity-bar/ActivityBar";
11
+ import { MobileTabBar } from "./components/activity-bar/MobileTabBar";
12
+ import { ChatPanel } from "./components/chat/ChatPanel";
13
+
14
+ function App() {
15
+ const { t } = useTranslation();
16
+ const mode = useConnectionStore((s) => s.mode);
17
+ const ready = useConnectionStore((s) => s.ready);
18
+ const initializeConnection = useConnectionStore((s) => s.initializeConnection);
19
+ const addLog = useLogStore((s) => s.addLog);
20
+
21
+ const breakpoint = useSidebarStore((s) => s.breakpoint);
22
+
23
+ useBreakpointSync();
24
+
25
+ const isMobile = breakpoint === "mobile";
26
+
27
+ useEffect(() => {
28
+ initializeConnection();
29
+ }, [initializeConnection]);
30
+
31
+ useEffect(() => {
32
+ if (!ready) return;
33
+
34
+ let subscriptionId: string | null = null;
35
+
36
+ const setup = async () => {
37
+ subscriptionId = await apiClient.subscribe("chat.message", (payload) => {
38
+ useChatStore.getState().addMessage({
39
+ id: payload.id,
40
+ role: payload.role,
41
+ content: payload.content,
42
+ timestamp: payload.timestamp,
43
+ });
44
+ }, {});
45
+ addLog("Subscribed to chat.message");
46
+
47
+ try {
48
+ const history = await apiClient.call("chat.list", { limit: 100 });
49
+ if (history.messages.length > 0) {
50
+ useChatStore.getState().setMessages(
51
+ history.messages.map((m: { id: string; role: "user" | "assistant"; content: string; timestamp: number }) => ({
52
+ id: m.id,
53
+ role: m.role,
54
+ content: m.content,
55
+ timestamp: m.timestamp,
56
+ }))
57
+ );
58
+ addLog(`Loaded ${history.messages.length} history messages`);
59
+ }
60
+ } catch (err) {
61
+ addLog(`Failed to load history: ${err instanceof Error ? err.message : String(err)}`);
62
+ }
63
+ };
64
+ setup();
65
+
66
+ return () => {
67
+ if (subscriptionId) {
68
+ apiClient.unsubscribe(subscriptionId);
69
+ }
70
+ };
71
+ }, [ready, addLog]);
72
+
73
+ if (!ready) {
74
+ return (
75
+ <div className="h-screen bg-[var(--color-bg-primary)] flex items-center justify-center">
76
+ <div className="text-center">
77
+ <div className="inline-block w-8 h-8 border-2 border-[var(--color-text-accent)] border-t-transparent rounded-full animate-spin mb-4" />
78
+ <div className="text-[var(--color-text-tertiary)] text-sm">{t("app.connecting")}</div>
79
+ </div>
80
+ </div>
81
+ );
82
+ }
83
+
84
+ return (
85
+ <div className="h-screen bg-[var(--color-bg-primary)] text-[var(--color-text-primary)] flex flex-col overflow-hidden">
86
+ {!isMobile && (
87
+ <div className="h-8 bg-[var(--color-bg-secondary)] flex items-center px-3 text-xs border-b border-[var(--color-border-primary)] flex-shrink-0">
88
+ <span className={`px-2 py-0.5 rounded flex items-center gap-1 ${mode === "desktop" ? "bg-green-600" : "bg-blue-600"}`}>
89
+ {mode === "desktop" ? <Monitor className="w-3 h-3" /> : <Wifi className="w-3 h-3" />}
90
+ {mode === "desktop" ? t("app.mode.desktop") : t("app.mode.web")}
91
+ </span>
92
+ <span className="ml-3 text-[var(--color-text-tertiary)]">{t("app.title")}</span>
93
+ </div>
94
+ )}
95
+
96
+ <div className="flex-1 flex overflow-hidden relative">
97
+ {!isMobile && <ActivityBar />}
98
+ <div className="flex-1 flex flex-col overflow-hidden">
99
+ <ChatPanel />
100
+ </div>
101
+ </div>
102
+
103
+ {isMobile && <MobileTabBar />}
104
+ </div>
105
+ );
106
+ }
107
+
108
+ export default App;