@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,99 @@
1
+ import type { RPCServer } from "@dyyz1993/rpc-core";
2
+ import type { MethodParams, MethodResult } from "@dyyz1993/rpc-core";
3
+ import type { RPCMethods, HandlerOptions } from "../rpc-schema";
4
+ import { readdir, stat, writeFile, readFile, mkdir, rename, rm, cp } from "fs/promises";
5
+ import { existsSync } from "fs";
6
+ import { join, dirname } from "path";
7
+ import { createLogger } from "../lib/logger";
8
+ import { validatePath } from "../lib/path-security";
9
+
10
+ const log = createLogger("file");
11
+
12
+ type RegisterFn = <K extends keyof RPCMethods & string>(
13
+ method: K,
14
+ handler: (params: MethodParams<RPCMethods, K>) => Promise<MethodResult<RPCMethods, K>>,
15
+ ) => void;
16
+
17
+ export function register(server: RPCServer, _options: HandlerOptions): void {
18
+ const r: RegisterFn = (method, handler) => {
19
+ server.register(method, handler as (params: unknown) => Promise<unknown>);
20
+ };
21
+
22
+ r("file.findProjectRoot", async () => {
23
+ let dir = process.cwd();
24
+ for (let i = 0; i < 20; i++) {
25
+ if (existsSync(join(dir, "package.json"))) return { path: dir };
26
+ const parent = dirname(dir);
27
+ if (parent === dir) break;
28
+ dir = parent;
29
+ }
30
+ return { path: process.cwd() };
31
+ });
32
+
33
+ r("file.listDir", async (params) => {
34
+ const basePath = validatePath(params.path || process.cwd());
35
+ const entries: { name: string; path: string; type: "file" | "directory"; size?: number }[] = [];
36
+ try {
37
+ const files = await readdir(basePath);
38
+ for (const name of files) {
39
+ const fullPath = join(basePath, name);
40
+ try {
41
+ const s = await stat(fullPath);
42
+ entries.push({
43
+ name,
44
+ path: fullPath,
45
+ type: s.isDirectory() ? "directory" : "file",
46
+ size: s.size,
47
+ });
48
+ } catch {
49
+ entries.push({ name, path: fullPath, type: "file" });
50
+ }
51
+ }
52
+ } catch (err) {
53
+ log.error("listDir error", { error: err });
54
+ }
55
+ return { entries, basePath };
56
+ });
57
+
58
+ r("file.createFile", async (params) => {
59
+ const dirPath = validatePath(params.dirPath);
60
+ const filePath = join(dirPath, params.name);
61
+ await writeFile(filePath, "");
62
+ return { path: filePath };
63
+ });
64
+
65
+ r("file.createDir", async (params) => {
66
+ const dirPath = validatePath(params.dirPath);
67
+ const fullDirPath = join(dirPath, params.name);
68
+ await mkdir(fullDirPath, { recursive: true });
69
+ return { path: fullDirPath };
70
+ });
71
+
72
+ r("file.rename", async (params) => {
73
+ const oldPath = validatePath(params.oldPath);
74
+ const newPath = join(dirname(oldPath), params.newName);
75
+ await rename(oldPath, newPath);
76
+ return { newPath };
77
+ });
78
+
79
+ r("file.delete", async (params) => {
80
+ const safePath = validatePath(params.path);
81
+ await rm(safePath, { recursive: true, force: true });
82
+ return { ok: true };
83
+ });
84
+
85
+ r("file.copy", async (params) => {
86
+ const srcPath = validatePath(params.srcPath);
87
+ const destDir = validatePath(params.destDir);
88
+ const name = srcPath.split("/").pop() || srcPath;
89
+ const destPath = join(destDir, name);
90
+ await cp(srcPath, destPath, { recursive: true });
91
+ return { path: destPath };
92
+ });
93
+
94
+ r("file.readFile", async (params) => {
95
+ const filePath = validatePath(params.path);
96
+ const content = await readFile(filePath);
97
+ return { content: content.toString(), size: content.length };
98
+ });
99
+ }
@@ -0,0 +1,263 @@
1
+ import type { RPCServer } from "@dyyz1993/rpc-core";
2
+ import type { MethodParams, MethodResult } from "@dyyz1993/rpc-core";
3
+ import type { RPCMethods, HandlerOptions } from "../rpc-schema";
4
+ import type { GitFileChange } from "../modules/git";
5
+
6
+ type RegisterFn = <K extends keyof RPCMethods & string>(
7
+ method: K,
8
+ handler: (params: MethodParams<RPCMethods, K>) => Promise<MethodResult<RPCMethods, K>>,
9
+ ) => void;
10
+
11
+ function execGit(args: string[], cwd: string, allowNonZero = false): string {
12
+ const proc = Bun.spawnSync(["git", ...args], { cwd, stdout: "pipe", stderr: "pipe" });
13
+ if (proc.exitCode !== 0 && !allowNonZero) {
14
+ throw new Error(proc.stderr.toString().trim() || `git ${args[0]} failed`);
15
+ }
16
+ return proc.stdout.toString();
17
+ }
18
+
19
+ function getRepoRoot(cwd: string): string {
20
+ return execGit(["rev-parse", "--show-toplevel"], cwd).trim();
21
+ }
22
+
23
+ function parseStatus(output: string): {
24
+ staged: GitFileChange[];
25
+ changed: GitFileChange[];
26
+ untracked: string[];
27
+ } {
28
+ const staged: GitFileChange[] = [];
29
+ const changed: GitFileChange[] = [];
30
+ const untracked: string[] = [];
31
+
32
+ for (const line of output.split("\n")) {
33
+ if (!line.trim()) continue;
34
+ const xy = line.slice(0, 2);
35
+ const filePath = line.slice(3).trim();
36
+
37
+ const statusMap: Record<string, "modified" | "added" | "deleted" | "renamed" | "copied"> = {
38
+ M: "modified", A: "added", D: "deleted", R: "renamed", C: "copied",
39
+ };
40
+
41
+ // Index (staged) - first char
42
+ const indexStatus = xy[0];
43
+ if (indexStatus !== " " && indexStatus !== "?" && statusMap[indexStatus]) {
44
+ const path = indexStatus === "R" ? filePath.split(" -> ")[1] : filePath;
45
+ staged.push({ path, status: statusMap[indexStatus] });
46
+ }
47
+
48
+ // Working tree - second char
49
+ const wtStatus = xy[1];
50
+ if (wtStatus !== " " && wtStatus !== "?" && statusMap[wtStatus]) {
51
+ changed.push({ path: filePath, status: statusMap[wtStatus] });
52
+ }
53
+
54
+ // Untracked
55
+ if (xy === "??") {
56
+ untracked.push(filePath);
57
+ }
58
+ }
59
+
60
+ return { staged, changed, untracked };
61
+ }
62
+
63
+ export function register(server: RPCServer, _options: HandlerOptions): void {
64
+ const r: RegisterFn = (method, handler) => {
65
+ server.register(method, handler as (params: unknown) => Promise<unknown>);
66
+ };
67
+
68
+ r("git.status", async (params) => {
69
+ const repoRoot = getRepoRoot(params.repoPath);
70
+ const output = execGit(["status", "--porcelain=v1", "--branch"], repoRoot);
71
+ const lines = output.split("\n");
72
+
73
+ // Parse branch info from first line
74
+ const branchLine = lines[0] || "";
75
+ const branchMatch = branchLine.match(/^## (.+?)(?:\.\.\.(\S+))?(?:\s+\[(ahead\s+(\d+))?(?:,\s*)?(behind\s+(\d+))?\])?$/);
76
+ const branch = branchMatch?.[1]?.replace("HEAD detached", "").replace(/[()]/g, "").trim() || "unknown";
77
+ const ahead = branchMatch?.[3] ? parseInt(branchMatch[3]) : 0;
78
+ const behind = branchMatch?.[5] ? parseInt(branchMatch[5]) : 0;
79
+
80
+ const { staged, changed, untracked } = parseStatus(lines.slice(1).join("\n"));
81
+
82
+ return { staged, changed, untracked, branch, ahead, behind };
83
+ });
84
+
85
+ r("git.diff", async (params) => {
86
+ const repoRoot = getRepoRoot(params.repoPath);
87
+ let diff = "";
88
+ if (params.staged) {
89
+ diff = execGit(["diff", "--cached", "--", params.filePath], repoRoot);
90
+ } else {
91
+ diff = execGit(["diff", "--", params.filePath], repoRoot);
92
+ if (!diff) {
93
+ try {
94
+ diff = execGit(["diff", "--no-index", "/dev/null", params.filePath], repoRoot, true);
95
+ } catch {
96
+ // ignore
97
+ }
98
+ }
99
+ }
100
+
101
+ // Get old content (HEAD version) and new content (working tree)
102
+ let oldContent = "";
103
+ let newContent = "";
104
+ try {
105
+ oldContent = execGit(["show", `HEAD:${params.filePath}`], repoRoot);
106
+ } catch {
107
+ // New file — no old content
108
+ }
109
+ try {
110
+ const { readFile } = await import("fs/promises");
111
+ const { join } = await import("path");
112
+ newContent = (await readFile(join(repoRoot, params.filePath))).toString();
113
+ } catch {
114
+ // Deleted file — no new content
115
+ }
116
+
117
+ return { filePath: params.filePath, diff, oldContent, newContent };
118
+ });
119
+
120
+ r("git.log", async (params) => {
121
+ const repoRoot = getRepoRoot(params.repoPath);
122
+ const count = params.maxCount || 50;
123
+ const output = execGit([
124
+ "log", `--max-count=${count}`,
125
+ "--pretty=format:%H|%h|%s|%an|%aI",
126
+ ], repoRoot);
127
+
128
+ const commits = output.split("\n").filter(Boolean).map((line) => {
129
+ const [hash, shortHash, message, author, date] = line.split("|");
130
+ return { hash, shortHash, message, author, date };
131
+ });
132
+
133
+ return { commits };
134
+ });
135
+
136
+ r("git.commitFiles", async (params) => {
137
+ const repoRoot = getRepoRoot(params.repoPath);
138
+ const output = execGit([
139
+ "diff-tree", "--no-commit-id", "--name-status", "-r", params.hash,
140
+ ], repoRoot);
141
+
142
+ const statusMap: Record<string, GitFileChange["status"]> = {
143
+ M: "modified", A: "added", D: "deleted", R: "renamed", C: "copied",
144
+ };
145
+
146
+ const files: GitFileChange[] = output.split("\n").filter(Boolean).map((line) => {
147
+ const [status, ...pathParts] = line.split("\t");
148
+ const path = pathParts.join("\t"); // handle paths with tabs (renames: old\tnew)
149
+ return { path: status === "R" ? path.split("\t").pop()! : path, status: statusMap[status] || "modified" };
150
+ });
151
+
152
+ return { files };
153
+ });
154
+
155
+ r("git.commitFileDiff", async (params) => {
156
+ const repoRoot = getRepoRoot(params.repoPath);
157
+ const { hash, filePath } = params;
158
+
159
+ // Get the diff for this file in this commit
160
+ const diff = execGit(["diff", `${hash}^..${hash}`, "--", filePath], repoRoot, true);
161
+
162
+ // Get old content (parent commit version)
163
+ let oldContent = "";
164
+ try {
165
+ oldContent = execGit(["show", `${hash}^:${filePath}`], repoRoot);
166
+ } catch {
167
+ // File was added in this commit — no old content
168
+ }
169
+
170
+ // Get new content (this commit version)
171
+ let newContent = "";
172
+ try {
173
+ newContent = execGit(["show", `${hash}:${filePath}`], repoRoot);
174
+ } catch {
175
+ // File was deleted in this commit — no new content
176
+ }
177
+
178
+ return { filePath, diff, oldContent, newContent };
179
+ });
180
+
181
+ r("git.branches", async (params) => {
182
+ const repoRoot = getRepoRoot(params.repoPath);
183
+ const output = execGit(["branch", "-a", "--no-color"], repoRoot);
184
+ const branches = output.split("\n").filter(Boolean).map((line) => {
185
+ const isCurrent = line.startsWith("*");
186
+ const name = line.replace(/^\*?\s+/, "").trim();
187
+ const isRemote = name.startsWith("remotes/");
188
+ return { name, isCurrent, isRemote };
189
+ });
190
+ return { branches };
191
+ });
192
+
193
+ r("git.checkout", async (params) => {
194
+ const repoRoot = getRepoRoot(params.repoPath);
195
+ execGit(["checkout", params.branch], repoRoot);
196
+ return { ok: true };
197
+ });
198
+
199
+ r("git.add", async (params) => {
200
+ const repoRoot = getRepoRoot(params.repoPath);
201
+ execGit(["add", ...params.paths], repoRoot);
202
+ return { ok: true };
203
+ });
204
+
205
+ r("git.reset", async (params) => {
206
+ const repoRoot = getRepoRoot(params.repoPath);
207
+ execGit(["reset", "HEAD", "--", ...params.paths], repoRoot);
208
+ return { ok: true };
209
+ });
210
+
211
+ r("git.commit", async (params) => {
212
+ const repoRoot = getRepoRoot(params.repoPath);
213
+ const output = execGit(["commit", "-m", params.message], repoRoot);
214
+ // Extract hash from output like "[main abc1234] message"
215
+ const hashMatch = output.match(/\[[\w\-/.]+\s+([0-9a-f]{7,40})\]/);
216
+ const shortHash = hashMatch?.[1] || "";
217
+ let hash = "";
218
+ if (shortHash) {
219
+ hash = execGit(["rev-parse", shortHash], repoRoot).trim();
220
+ }
221
+ return { hash, shortHash };
222
+ });
223
+
224
+ r("git.push", async (params) => {
225
+ const repoRoot = getRepoRoot(params.repoPath);
226
+ execGit(["push"], repoRoot);
227
+ return { ok: true };
228
+ });
229
+
230
+ r("git.pull", async (params) => {
231
+ const repoRoot = getRepoRoot(params.repoPath);
232
+ execGit(["pull"], repoRoot);
233
+ return { ok: true };
234
+ });
235
+
236
+ r("git.worktreeList", async (params) => {
237
+ const repoRoot = getRepoRoot(params.repoPath);
238
+ const output = execGit(["worktree", "list", "--porcelain"], repoRoot);
239
+ const worktrees: { path: string; branch: string; isMain: boolean }[] = [];
240
+ let current: Partial<typeof worktrees[0]> = {};
241
+
242
+ for (const line of output.split("\n")) {
243
+ if (line.startsWith("worktree ")) {
244
+ if (current.path) {
245
+ worktrees.push({ path: current.path!, branch: current.branch || "", isMain: !!current.isMain });
246
+ }
247
+ current = { path: line.slice(9), isMain: false };
248
+ } else if (line.startsWith("branch ")) {
249
+ current.branch = line.slice(7).replace("refs/heads/", "");
250
+ } else if (line === "bare") {
251
+ current.isMain = false;
252
+ } else if (line === "" && current.path) {
253
+ // first worktree is main
254
+ if (worktrees.length === 0) current.isMain = true;
255
+ }
256
+ }
257
+ if (current.path) {
258
+ worktrees.push({ path: current.path!, branch: current.branch || "", isMain: !!current.isMain });
259
+ }
260
+
261
+ return { worktrees };
262
+ });
263
+ }
@@ -0,0 +1,10 @@
1
+ // Handler barrel — 新增模块在此加一行 export
2
+ export { register as system } from "./system";
3
+ export { register as file } from "./file";
4
+ export { register as timer } from "./timer";
5
+ export { register as chat } from "./chat";
6
+ export { register as git } from "./git";
7
+ export { register as feed } from "./feed";
8
+ export { register as bash } from "./bash";
9
+ export { register as rules } from "./rules";
10
+ export { register as todo } from "./todo";
@@ -0,0 +1,45 @@
1
+ import type { RPCServer } from "@dyyz1993/rpc-core";
2
+ import type { MethodParams, MethodResult } from "@dyyz1993/rpc-core";
3
+ import type { RPCMethods, HandlerOptions } from "../rpc-schema";
4
+ import type { Rule } from "../modules/rules";
5
+
6
+ type RegisterFn = <K extends keyof RPCMethods & string>(
7
+ method: K,
8
+ handler: (params: MethodParams<RPCMethods, K>) => Promise<MethodResult<RPCMethods, K>>,
9
+ ) => void;
10
+
11
+ const rules: Rule[] = [];
12
+ let ruleIdCounter = 1;
13
+
14
+ export function register(server: RPCServer, _options: HandlerOptions): void {
15
+ const r: RegisterFn = (method, handler) => {
16
+ server.register(method, handler as (params: unknown) => Promise<unknown>);
17
+ };
18
+
19
+ r("rules.list", async () => ({ rules }));
20
+
21
+ r("rules.add", async (params) => {
22
+ const rule: Rule = {
23
+ id: `rule-${ruleIdCounter++}`,
24
+ name: params.name,
25
+ pattern: params.pattern,
26
+ enabled: true,
27
+ };
28
+ rules.push(rule);
29
+ return { rule };
30
+ });
31
+
32
+ r("rules.toggle", async (params) => {
33
+ const rule = rules.find((r) => r.id === params.id);
34
+ if (!rule) throw new Error(`Rule ${params.id} not found`);
35
+ rule.enabled = !rule.enabled;
36
+ return { rule };
37
+ });
38
+
39
+ r("rules.remove", async (params) => {
40
+ const idx = rules.findIndex((r) => r.id === params.id);
41
+ if (idx === -1) return { success: false };
42
+ rules.splice(idx, 1);
43
+ return { success: true };
44
+ });
45
+ }
@@ -0,0 +1,27 @@
1
+ import type { RPCServer } from "@dyyz1993/rpc-core";
2
+ import type { MethodParams, MethodResult } from "@dyyz1993/rpc-core";
3
+ import type { RPCMethods, HandlerOptions } from "../rpc-schema";
4
+
5
+ type RegisterFn = <K extends keyof RPCMethods & string>(
6
+ method: K,
7
+ handler: (params: MethodParams<RPCMethods, K>) => Promise<MethodResult<RPCMethods, K>>,
8
+ ) => void;
9
+
10
+ export function register(server: RPCServer, options: HandlerOptions): void {
11
+ const r: RegisterFn = (method, handler) => {
12
+ server.register(method, handler as (params: unknown) => Promise<unknown>);
13
+ };
14
+
15
+ r("system.ping", async () => ({
16
+ pong: true,
17
+ timestamp: Date.now(),
18
+ platform: options.platform,
19
+ }));
20
+
21
+ r("system.hello", async (params) => ({
22
+ message: `Hello ${params.name || "World"}!`,
23
+ timestamp: Date.now(),
24
+ }));
25
+
26
+ r("system.echo", async (params) => params);
27
+ }
@@ -0,0 +1,34 @@
1
+ import type { RPCServer } from "@dyyz1993/rpc-core";
2
+ import type { MethodParams, MethodResult } from "@dyyz1993/rpc-core";
3
+ import type { RPCMethods, HandlerOptions } from "../rpc-schema";
4
+
5
+ type RegisterFn = <K extends keyof RPCMethods & string>(
6
+ method: K,
7
+ handler: (params: MethodParams<RPCMethods, K>) => Promise<MethodResult<RPCMethods, K>>,
8
+ ) => void;
9
+
10
+ export function register(server: RPCServer, _options: HandlerOptions): void {
11
+ const r: RegisterFn = (method, handler) => {
12
+ server.register(method, handler as (params: unknown) => Promise<unknown>);
13
+ };
14
+
15
+ let timerId: ReturnType<typeof setInterval> | null = null;
16
+
17
+ r("timer.start", async () => {
18
+ if (timerId !== null) return { alreadyRunning: true };
19
+ let count = 0;
20
+ timerId = setInterval(() => {
21
+ count++;
22
+ server.emitEvent("timer.tick", { count, timestamp: Date.now() }, { channel: "default" });
23
+ }, 1000);
24
+ return { started: true };
25
+ });
26
+
27
+ r("timer.stop", async () => {
28
+ if (timerId !== null) {
29
+ clearInterval(timerId);
30
+ timerId = null;
31
+ }
32
+ return { stopped: true };
33
+ });
34
+ }
@@ -0,0 +1,45 @@
1
+ import type { RPCServer } from "@dyyz1993/rpc-core";
2
+ import type { MethodParams, MethodResult } from "@dyyz1993/rpc-core";
3
+ import type { RPCMethods, HandlerOptions } from "../rpc-schema";
4
+ import type { TodoItem, TodoStatus } from "../modules/todo";
5
+
6
+ type RegisterFn = <K extends keyof RPCMethods & string>(
7
+ method: K,
8
+ handler: (params: MethodParams<RPCMethods, K>) => Promise<MethodResult<RPCMethods, K>>,
9
+ ) => void;
10
+
11
+ const items: TodoItem[] = [];
12
+ let todoIdCounter = 1;
13
+
14
+ export function register(server: RPCServer, _options: HandlerOptions): void {
15
+ const r: RegisterFn = (method, handler) => {
16
+ server.register(method, handler as (params: unknown) => Promise<unknown>);
17
+ };
18
+
19
+ r("todo.list", async () => ({ items }));
20
+
21
+ r("todo.add", async (params) => {
22
+ const item: TodoItem = {
23
+ id: `todo-${todoIdCounter++}`,
24
+ content: params.content,
25
+ status: "pending",
26
+ createdAt: Date.now(),
27
+ };
28
+ items.push(item);
29
+ return { item };
30
+ });
31
+
32
+ r("todo.update", async (params) => {
33
+ const item = items.find((i) => i.id === params.id);
34
+ if (!item) throw new Error(`Todo ${params.id} not found`);
35
+ item.status = params.status as TodoStatus;
36
+ return { item };
37
+ });
38
+
39
+ r("todo.remove", async (params) => {
40
+ const idx = items.findIndex((i) => i.id === params.id);
41
+ if (idx === -1) return { success: false };
42
+ items.splice(idx, 1);
43
+ return { success: true };
44
+ });
45
+ }
@@ -0,0 +1,125 @@
1
+ import { describe, it, expect, beforeEach } from "vitest";
2
+ import {
3
+ isCommandAllowed,
4
+ validateCommand,
5
+ setCommandPolicy,
6
+ } from "../bash-security";
7
+
8
+ describe("bash-security", () => {
9
+ describe("default policy (enabled with blocked list)", () => {
10
+ beforeEach(() => {
11
+ setCommandPolicy({
12
+ enabled: true,
13
+ blockedPatterns: [
14
+ /rm\s+-rf\s+(.*\s)?\/($|\s)/,
15
+ /rm\s+-rf\s+--no-preserve-root/,
16
+ /mkfs/,
17
+ /dd\s+if=/,
18
+ />\s*\/dev\//,
19
+ /:()\s*\{.*\|.*&\s*\}/,
20
+ /shutdown/,
21
+ /reboot/,
22
+ ],
23
+ allowedCommands: null,
24
+ });
25
+ });
26
+
27
+ it("should allow normal commands", () => {
28
+ expect(isCommandAllowed("ls -la")).toBe(true);
29
+ expect(isCommandAllowed("git status")).toBe(true);
30
+ expect(isCommandAllowed("npm install")).toBe(true);
31
+ expect(isCommandAllowed("echo hello")).toBe(true);
32
+ });
33
+
34
+ it("should block dangerous rm -rf /", () => {
35
+ expect(isCommandAllowed("rm -rf /")).toBe(false);
36
+ expect(isCommandAllowed("rm -rf --no-preserve-root /")).toBe(false);
37
+ });
38
+
39
+ it("should block mkfs commands", () => {
40
+ expect(isCommandAllowed("mkfs.ext4 /dev/sda1")).toBe(false);
41
+ });
42
+
43
+ it("should block dd write commands", () => {
44
+ expect(isCommandAllowed("dd if=/dev/zero of=/dev/sda")).toBe(false);
45
+ });
46
+
47
+ it("should block redirect to device files", () => {
48
+ expect(isCommandAllowed("echo data > /dev/sda")).toBe(false);
49
+ });
50
+
51
+ it("should block shutdown and reboot", () => {
52
+ expect(isCommandAllowed("shutdown now")).toBe(false);
53
+ expect(isCommandAllowed("reboot")).toBe(false);
54
+ });
55
+
56
+ it("should allow rm in project directory", () => {
57
+ expect(isCommandAllowed("rm -rf ./node_modules")).toBe(true);
58
+ expect(isCommandAllowed("rm -rf ./dist")).toBe(true);
59
+ });
60
+ });
61
+
62
+ describe("disabled policy", () => {
63
+ it("should reject all commands when disabled", () => {
64
+ setCommandPolicy({
65
+ enabled: false,
66
+ blockedPatterns: [],
67
+ allowedCommands: null,
68
+ });
69
+ expect(isCommandAllowed("ls")).toBe(false);
70
+ expect(isCommandAllowed("echo hello")).toBe(false);
71
+ });
72
+ });
73
+
74
+ describe("whitelist policy", () => {
75
+ it("should only allow whitelisted commands", () => {
76
+ setCommandPolicy({
77
+ enabled: true,
78
+ blockedPatterns: [],
79
+ allowedCommands: ["git", "npm", "node", "ls", "cat", "echo"],
80
+ });
81
+ expect(isCommandAllowed("git status")).toBe(true);
82
+ expect(isCommandAllowed("npm test")).toBe(true);
83
+ expect(isCommandAllowed("python script.py")).toBe(false);
84
+ });
85
+ });
86
+
87
+ describe("validateCommand", () => {
88
+ it("should return trimmed command for allowed commands", () => {
89
+ setCommandPolicy({
90
+ enabled: true,
91
+ blockedPatterns: [],
92
+ allowedCommands: null,
93
+ });
94
+ expect(validateCommand(" ls -la ")).toBe("ls -la");
95
+ });
96
+
97
+ it("should throw for blocked commands", () => {
98
+ setCommandPolicy({
99
+ enabled: true,
100
+ blockedPatterns: [/rm\s+-rf\s+\//],
101
+ allowedCommands: null,
102
+ });
103
+ expect(() => validateCommand("rm -rf /")).toThrow("blocked");
104
+ });
105
+
106
+ it("should throw when bash is disabled", () => {
107
+ setCommandPolicy({
108
+ enabled: false,
109
+ blockedPatterns: [],
110
+ allowedCommands: null,
111
+ });
112
+ expect(() => validateCommand("ls")).toThrow("disabled");
113
+ });
114
+
115
+ it("should throw for empty commands", () => {
116
+ setCommandPolicy({
117
+ enabled: true,
118
+ blockedPatterns: [],
119
+ allowedCommands: null,
120
+ });
121
+ expect(() => validateCommand("")).toThrow("empty");
122
+ expect(() => validateCommand(" ")).toThrow("empty");
123
+ });
124
+ });
125
+ });