claude-code-workflow 6.1.4 → 6.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (437) hide show
  1. package/.claude/CLAUDE.md +10 -0
  2. package/.claude/agents/action-planning-agent.md +857 -778
  3. package/.claude/agents/cli-execution-agent.md +266 -269
  4. package/.claude/agents/cli-explore-agent.md +2 -2
  5. package/.claude/agents/cli-lite-planning-agent.md +142 -92
  6. package/.claude/agents/cli-planning-agent.md +4 -4
  7. package/.claude/agents/code-developer.md +7 -6
  8. package/.claude/agents/conceptual-planning-agent.md +2 -2
  9. package/.claude/agents/context-search-agent.md +31 -32
  10. package/.claude/agents/doc-generator.md +4 -4
  11. package/.claude/agents/memory-bridge.md +93 -93
  12. package/.claude/agents/test-context-search-agent.md +8 -7
  13. package/.claude/agents/test-fix-agent.md +7 -6
  14. package/.claude/commands/clean.md +516 -0
  15. package/.claude/commands/memory/compact.md +383 -0
  16. package/.claude/commands/memory/docs-full-cli.md +471 -471
  17. package/.claude/commands/memory/docs-related-cli.md +386 -386
  18. package/.claude/commands/memory/docs.md +615 -615
  19. package/.claude/commands/memory/load.md +5 -5
  20. package/.claude/commands/memory/tech-research-rules.md +310 -0
  21. package/.claude/commands/memory/update-full.md +332 -332
  22. package/.claude/commands/memory/workflow-skill-memory.md +4 -4
  23. package/.claude/commands/task/create.md +151 -151
  24. package/.claude/commands/version.md +254 -254
  25. package/.claude/commands/workflow/brainstorm/api-designer.md +587 -585
  26. package/.claude/commands/workflow/brainstorm/artifacts.md +1 -0
  27. package/.claude/commands/workflow/brainstorm/auto-parallel.md +443 -443
  28. package/.claude/commands/workflow/brainstorm/data-architect.md +220 -220
  29. package/.claude/commands/workflow/brainstorm/product-manager.md +200 -200
  30. package/.claude/commands/workflow/brainstorm/product-owner.md +200 -200
  31. package/.claude/commands/workflow/brainstorm/scrum-master.md +200 -200
  32. package/.claude/commands/workflow/brainstorm/subject-matter-expert.md +200 -200
  33. package/.claude/commands/workflow/brainstorm/system-architect.md +389 -387
  34. package/.claude/commands/workflow/brainstorm/ui-designer.md +221 -221
  35. package/.claude/commands/workflow/brainstorm/ux-expert.md +221 -221
  36. package/.claude/commands/workflow/debug.md +321 -0
  37. package/.claude/commands/workflow/execute.md +13 -0
  38. package/.claude/commands/workflow/init.md +165 -164
  39. package/.claude/commands/workflow/lite-execute.md +119 -13
  40. package/.claude/commands/workflow/lite-fix.md +623 -621
  41. package/.claude/commands/workflow/lite-plan.md +610 -592
  42. package/.claude/commands/workflow/plan.md +5 -5
  43. package/.claude/commands/workflow/review-module-cycle.md +2 -0
  44. package/.claude/commands/workflow/review-session-cycle.md +2 -0
  45. package/.claude/commands/workflow/review.md +297 -291
  46. package/.claude/commands/workflow/session/complete.md +153 -500
  47. package/.claude/commands/workflow/session/list.md +95 -95
  48. package/.claude/commands/workflow/session/resume.md +60 -60
  49. package/.claude/commands/workflow/session/start.md +199 -199
  50. package/.claude/commands/workflow/tdd-plan.md +3 -3
  51. package/.claude/commands/workflow/tdd-verify.md +23 -9
  52. package/.claude/commands/workflow/test-cycle-execute.md +2 -0
  53. package/.claude/commands/workflow/test-fix-gen.md +699 -699
  54. package/.claude/commands/workflow/tools/conflict-resolution.md +104 -18
  55. package/.claude/commands/workflow/tools/context-gather.md +436 -434
  56. package/.claude/commands/workflow/tools/task-generate-agent.md +490 -291
  57. package/.claude/commands/workflow/tools/task-generate-tdd.md +18 -10
  58. package/.claude/commands/workflow/tools/test-concept-enhanced.md +2 -1
  59. package/.claude/commands/workflow/tools/test-context-gather.md +1 -0
  60. package/.claude/commands/workflow/tools/test-task-generate.md +1 -0
  61. package/.claude/commands/workflow/ui-design/import-from-code.md +9 -6
  62. package/.claude/skills/command-guide/SKILL.md +5 -5
  63. package/.claude/skills/command-guide/index/all-commands.json +1 -1
  64. package/.claude/skills/command-guide/index/by-category.json +1 -1
  65. package/.claude/skills/command-guide/index/by-use-case.json +1 -1
  66. package/.claude/skills/command-guide/reference/agents/action-planning-agent.md +857 -778
  67. package/.claude/skills/command-guide/reference/agents/cli-execution-agent.md +266 -269
  68. package/.claude/skills/command-guide/reference/agents/cli-explore-agent.md +2 -2
  69. package/.claude/skills/command-guide/reference/agents/cli-lite-planning-agent.md +142 -92
  70. package/.claude/skills/command-guide/reference/agents/cli-planning-agent.md +4 -4
  71. package/.claude/skills/command-guide/reference/agents/code-developer.md +7 -6
  72. package/.claude/skills/command-guide/reference/agents/conceptual-planning-agent.md +2 -2
  73. package/.claude/skills/command-guide/reference/agents/context-search-agent.md +31 -32
  74. package/.claude/skills/command-guide/reference/agents/doc-generator.md +4 -4
  75. package/.claude/skills/command-guide/reference/agents/memory-bridge.md +93 -93
  76. package/.claude/skills/command-guide/reference/agents/test-context-search-agent.md +8 -7
  77. package/.claude/skills/command-guide/reference/agents/test-fix-agent.md +7 -6
  78. package/.claude/skills/command-guide/reference/commands/memory/docs-full-cli.md +471 -471
  79. package/.claude/skills/command-guide/reference/commands/memory/docs-related-cli.md +386 -386
  80. package/.claude/skills/command-guide/reference/commands/memory/docs.md +17 -16
  81. package/.claude/skills/command-guide/reference/commands/memory/load.md +5 -5
  82. package/.claude/skills/command-guide/reference/commands/memory/tech-research.md +194 -357
  83. package/.claude/skills/command-guide/reference/commands/memory/update-full.md +332 -332
  84. package/.claude/skills/command-guide/reference/commands/memory/workflow-skill-memory.md +4 -4
  85. package/.claude/skills/command-guide/reference/commands/task/create.md +151 -151
  86. package/.claude/skills/command-guide/reference/commands/version.md +254 -254
  87. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/api-designer.md +585 -585
  88. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/auto-parallel.md +443 -443
  89. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/data-architect.md +220 -220
  90. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/product-manager.md +200 -200
  91. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/product-owner.md +200 -200
  92. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/scrum-master.md +200 -200
  93. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/subject-matter-expert.md +200 -200
  94. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/system-architect.md +387 -387
  95. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/ui-designer.md +221 -221
  96. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/ux-expert.md +221 -221
  97. package/.claude/skills/command-guide/reference/commands/workflow/execute.md +25 -20
  98. package/.claude/skills/command-guide/reference/commands/workflow/init.md +164 -164
  99. package/.claude/skills/command-guide/reference/commands/workflow/lite-execute.md +748 -686
  100. package/.claude/skills/command-guide/reference/commands/workflow/lite-fix.md +664 -621
  101. package/.claude/skills/command-guide/reference/commands/workflow/lite-plan.md +645 -592
  102. package/.claude/skills/command-guide/reference/commands/workflow/plan.md +5 -5
  103. package/.claude/skills/command-guide/reference/commands/workflow/review.md +25 -18
  104. package/.claude/skills/command-guide/reference/commands/workflow/session/complete.md +547 -500
  105. package/.claude/skills/command-guide/reference/commands/workflow/session/list.md +45 -27
  106. package/.claude/skills/command-guide/reference/commands/workflow/session/resume.md +35 -19
  107. package/.claude/skills/command-guide/reference/commands/workflow/session/start.md +90 -33
  108. package/.claude/skills/command-guide/reference/commands/workflow/tdd-plan.md +3 -3
  109. package/.claude/skills/command-guide/reference/commands/workflow/tdd-verify.md +23 -9
  110. package/.claude/skills/command-guide/reference/commands/workflow/test-fix-gen.md +699 -699
  111. package/.claude/skills/command-guide/reference/commands/workflow/tools/conflict-resolution.md +103 -17
  112. package/.claude/skills/command-guide/reference/commands/workflow/tools/context-gather.md +434 -434
  113. package/.claude/skills/command-guide/reference/commands/workflow/tools/task-generate-agent.md +487 -291
  114. package/.claude/skills/command-guide/reference/commands/workflow/tools/task-generate-tdd.md +17 -10
  115. package/.claude/skills/command-guide/reference/commands/workflow/tools/test-concept-enhanced.md +1 -1
  116. package/.claude/skills/command-guide/reference/commands/workflow/ui-design/import-from-code.md +6 -6
  117. package/.claude/workflows/chinese-response.md +38 -0
  118. package/.claude/workflows/cli-templates/prompts/rules/rule-api.txt +122 -0
  119. package/.claude/workflows/cli-templates/prompts/rules/rule-components.txt +122 -0
  120. package/.claude/workflows/cli-templates/prompts/rules/rule-config.txt +89 -0
  121. package/.claude/workflows/cli-templates/prompts/rules/rule-core.txt +60 -0
  122. package/.claude/workflows/cli-templates/prompts/rules/rule-patterns.txt +70 -0
  123. package/.claude/workflows/cli-templates/prompts/rules/rule-testing.txt +81 -0
  124. package/.claude/workflows/cli-templates/prompts/rules/tech-rules-agent-prompt.txt +89 -0
  125. package/.claude/workflows/cli-templates/prompts/workflow/gemini-solution-design.txt +131 -131
  126. package/.claude/workflows/cli-templates/prompts/workflow/skill-conflict-patterns.txt +5 -9
  127. package/.claude/workflows/cli-templates/prompts/workflow/skill-lessons-learned.txt +5 -9
  128. package/.claude/workflows/cli-templates/protocols/analysis-protocol.md +112 -0
  129. package/.claude/workflows/cli-templates/protocols/write-protocol.md +201 -0
  130. package/.claude/workflows/cli-templates/schemas/conflict-resolution-schema.json +137 -0
  131. package/.claude/workflows/cli-templates/schemas/debug-log-json-schema.json +127 -0
  132. package/.claude/workflows/cli-templates/schemas/fix-plan-json-schema.json +25 -0
  133. package/.claude/workflows/cli-templates/schemas/plan-json-schema.json +25 -0
  134. package/.claude/workflows/cli-tools-usage.md +526 -0
  135. package/{CLAUDE.md → .claude/workflows/coding-philosophy.md} +24 -45
  136. package/.claude/workflows/context-tools.md +84 -0
  137. package/.claude/workflows/file-modification.md +64 -0
  138. package/.claude/workflows/tool-strategy.md +216 -79
  139. package/.claude/workflows/windows-platform.md +16 -0
  140. package/.claude/workflows/workflow-architecture.md +942 -942
  141. package/.codex/AGENTS.md +63 -330
  142. package/.codex/prompts/debug.md +318 -0
  143. package/.codex/prompts/execute.md +273 -0
  144. package/.codex/prompts/lite-execute.md +164 -0
  145. package/.codex/prompts/lite-plan.md +469 -0
  146. package/.codex/prompts.zip +0 -0
  147. package/.gemini/GEMINI.md +25 -164
  148. package/.qwen/QWEN.md +0 -139
  149. package/README.md +29 -9
  150. package/ccw/README.md +30 -6
  151. package/ccw/bin/ccw-mcp.js +7 -0
  152. package/ccw/bin/ccw.js +9 -9
  153. package/ccw/package.json +65 -47
  154. package/ccw/src/.workflow/.cli-history/history.db +0 -0
  155. package/ccw/src/.workflow/.cli-history/history.db-shm +0 -0
  156. package/ccw/src/.workflow/.cli-history/history.db-wal +0 -0
  157. package/ccw/src/cli.ts +244 -0
  158. package/ccw/src/commands/cli.ts +740 -0
  159. package/ccw/src/commands/core-memory.ts +770 -0
  160. package/ccw/src/commands/hook.ts +315 -0
  161. package/ccw/src/commands/install.ts +519 -0
  162. package/ccw/src/commands/{list.js → list.ts} +1 -1
  163. package/ccw/src/commands/memory.ts +1090 -0
  164. package/ccw/src/commands/{serve.js → serve.ts} +14 -5
  165. package/ccw/src/commands/session-path-resolver.ts +372 -0
  166. package/ccw/src/commands/session.ts +1141 -0
  167. package/ccw/src/commands/{stop.js → stop.ts} +16 -6
  168. package/ccw/src/commands/tool.ts +201 -0
  169. package/ccw/src/commands/{uninstall.js → uninstall.ts} +89 -40
  170. package/ccw/src/commands/{upgrade.js → upgrade.ts} +68 -23
  171. package/ccw/src/commands/{view.js → view.ts} +22 -8
  172. package/ccw/src/config/storage-paths.ts +670 -0
  173. package/ccw/src/core/cache-manager.ts +294 -0
  174. package/ccw/src/core/claude-freshness.ts +319 -0
  175. package/ccw/src/core/core-memory-store.ts +1528 -0
  176. package/ccw/src/core/{dashboard-generator-patch.js → dashboard-generator-patch.ts} +18 -0
  177. package/ccw/src/core/{dashboard-generator.js → dashboard-generator.ts} +69 -12
  178. package/ccw/src/core/data-aggregator.ts +584 -0
  179. package/ccw/src/core/history-importer.ts +625 -0
  180. package/ccw/src/core/{lite-scanner.js → lite-scanner-complete.ts} +162 -66
  181. package/ccw/src/core/lite-scanner.ts +469 -0
  182. package/ccw/src/core/{manifest.js → manifest.ts} +104 -34
  183. package/ccw/src/core/memory-embedder-bridge.ts +262 -0
  184. package/ccw/src/core/memory-store.ts +978 -0
  185. package/ccw/src/core/routes/ccw-routes.ts +96 -0
  186. package/ccw/src/core/routes/claude-routes.ts +1183 -0
  187. package/ccw/src/core/routes/cli-routes.ts +561 -0
  188. package/ccw/src/core/routes/codexlens-routes.ts +806 -0
  189. package/ccw/src/core/routes/core-memory-routes.ts +605 -0
  190. package/ccw/src/core/routes/files-routes.ts +428 -0
  191. package/ccw/src/core/routes/graph-routes.md +164 -0
  192. package/ccw/src/core/routes/graph-routes.ts +626 -0
  193. package/ccw/src/core/routes/help-routes.ts +308 -0
  194. package/ccw/src/core/routes/hooks-routes.ts +405 -0
  195. package/ccw/src/core/routes/mcp-routes.ts +1271 -0
  196. package/ccw/src/core/routes/mcp-routes.ts.backup +550 -0
  197. package/ccw/src/core/routes/mcp-templates-db.ts +268 -0
  198. package/ccw/src/core/routes/memory-routes.ts +1206 -0
  199. package/ccw/src/core/routes/rules-routes.ts +526 -0
  200. package/ccw/src/core/routes/session-routes.ts +467 -0
  201. package/ccw/src/core/routes/skills-routes.ts +599 -0
  202. package/ccw/src/core/routes/status-routes.ts +57 -0
  203. package/ccw/src/core/routes/system-routes.ts +427 -0
  204. package/ccw/src/core/server.ts +431 -0
  205. package/ccw/src/core/session-clustering-service.ts +1258 -0
  206. package/ccw/src/core/session-scanner.ts +283 -0
  207. package/ccw/src/core/websocket.ts +190 -0
  208. package/ccw/src/{index.js → index.ts} +1 -0
  209. package/ccw/src/mcp-server/index.ts +186 -0
  210. package/ccw/src/templates/assets/css/github-dark.min.css +10 -0
  211. package/ccw/src/templates/assets/css/github.min.css +10 -0
  212. package/ccw/src/templates/assets/js/cytoscape.min.js +32 -0
  213. package/ccw/src/templates/assets/js/d3.min.js +2 -0
  214. package/ccw/src/templates/assets/js/highlight.min.js +1244 -0
  215. package/ccw/src/templates/assets/js/lucide.min.js +12 -0
  216. package/ccw/src/templates/assets/js/marked.min.js +69 -0
  217. package/ccw/src/templates/assets/js/tailwind.js +83 -0
  218. package/ccw/src/templates/dashboard-css/01-base.css +11 -0
  219. package/ccw/src/templates/dashboard-css/02-session.css +22 -0
  220. package/ccw/src/templates/dashboard-css/04-lite-tasks.css +10 -0
  221. package/ccw/src/templates/dashboard-css/06-cards.css +10 -4
  222. package/ccw/src/templates/dashboard-css/07-managers.css +1178 -7
  223. package/ccw/src/templates/dashboard-css/09-explorer.css +23 -12
  224. package/ccw/src/templates/dashboard-css/10-cli-status.css +337 -0
  225. package/ccw/src/templates/dashboard-css/11-cli-history.css +271 -0
  226. package/ccw/src/templates/dashboard-css/12-cli-legacy.css +796 -0
  227. package/ccw/src/templates/dashboard-css/13-cli-ccw.css +199 -0
  228. package/ccw/src/templates/dashboard-css/14-cli-modals.css +258 -0
  229. package/ccw/src/templates/dashboard-css/15-cli-endpoints.css +305 -0
  230. package/ccw/src/templates/dashboard-css/16-cli-session.css +241 -0
  231. package/ccw/src/templates/dashboard-css/17-cli-conversation.css +283 -0
  232. package/ccw/src/templates/dashboard-css/18-cli-settings.css +160 -0
  233. package/ccw/src/templates/dashboard-css/19-cli-native-session.css +496 -0
  234. package/ccw/src/templates/dashboard-css/20-cli-taskqueue.css +188 -0
  235. package/ccw/src/templates/dashboard-css/21-cli-toolmgmt.css +310 -0
  236. package/ccw/src/templates/dashboard-css/22-cli-semantic.css +240 -0
  237. package/ccw/src/templates/dashboard-css/23-memory.css +2390 -0
  238. package/ccw/src/templates/dashboard-css/24-prompt-history.css +1089 -0
  239. package/ccw/src/templates/dashboard-css/25-skills-rules.css +326 -0
  240. package/ccw/src/templates/dashboard-css/26-claude-manager.css +908 -0
  241. package/ccw/src/templates/dashboard-css/27-graph-explorer.css +1678 -0
  242. package/ccw/src/templates/dashboard-css/28-mcp-manager.css +748 -0
  243. package/ccw/src/templates/dashboard-css/29-help.css +264 -0
  244. package/ccw/src/templates/dashboard-css/30-core-memory.css +1700 -0
  245. package/ccw/src/templates/dashboard-js/api.js +162 -142
  246. package/ccw/src/templates/dashboard-js/components/carousel.js +4 -4
  247. package/ccw/src/templates/dashboard-js/components/cli-history.js +876 -0
  248. package/ccw/src/templates/dashboard-js/components/cli-status.js +978 -0
  249. package/ccw/src/templates/dashboard-js/components/global-notifications.js +508 -219
  250. package/ccw/src/templates/dashboard-js/components/hook-manager.js +1277 -282
  251. package/ccw/src/templates/dashboard-js/components/index-manager.js +302 -0
  252. package/ccw/src/templates/dashboard-js/components/mcp-manager.js +718 -27
  253. package/ccw/src/templates/dashboard-js/components/modals.js +66 -0
  254. package/ccw/src/templates/dashboard-js/components/navigation.js +80 -12
  255. package/ccw/src/templates/dashboard-js/components/notifications.js +758 -194
  256. package/ccw/src/templates/dashboard-js/components/storage-manager.js +478 -0
  257. package/ccw/src/templates/dashboard-js/components/tabs-other.js +157 -6
  258. package/ccw/src/templates/dashboard-js/components/task-queue-sidebar.js +716 -0
  259. package/ccw/src/templates/dashboard-js/help-i18n.js +272 -0
  260. package/ccw/src/templates/dashboard-js/i18n.js +2807 -0
  261. package/ccw/src/templates/dashboard-js/main.js +15 -0
  262. package/ccw/src/templates/dashboard-js/state.js +243 -42
  263. package/ccw/src/templates/dashboard-js/utils.js +47 -1
  264. package/ccw/src/templates/dashboard-js/views/claude-manager.js +912 -0
  265. package/ccw/src/templates/dashboard-js/views/cli-manager.js +2272 -0
  266. package/ccw/src/templates/dashboard-js/views/codexlens-manager.js +964 -0
  267. package/ccw/src/templates/dashboard-js/views/core-memory-clusters.js +503 -0
  268. package/ccw/src/templates/dashboard-js/views/core-memory.js +782 -0
  269. package/ccw/src/templates/dashboard-js/views/explorer.js +888 -852
  270. package/ccw/src/templates/dashboard-js/views/graph-explorer.js +1157 -0
  271. package/ccw/src/templates/dashboard-js/views/help.js +856 -0
  272. package/ccw/src/templates/dashboard-js/views/history.js +337 -0
  273. package/ccw/src/templates/dashboard-js/views/home.js +61 -15
  274. package/ccw/src/templates/dashboard-js/views/hook-manager.js +311 -43
  275. package/ccw/src/templates/dashboard-js/views/lite-tasks.js +204 -28
  276. package/ccw/src/templates/dashboard-js/views/mcp-manager.js +2187 -411
  277. package/ccw/src/templates/dashboard-js/views/mcp-manager.js.backup +1729 -0
  278. package/ccw/src/templates/dashboard-js/views/mcp-manager.js.new +928 -0
  279. package/ccw/src/templates/dashboard-js/views/memory.js +1221 -0
  280. package/ccw/src/templates/dashboard-js/views/prompt-history.js +713 -0
  281. package/ccw/src/templates/dashboard-js/views/rules-manager.js +828 -0
  282. package/ccw/src/templates/dashboard-js/views/session-detail.js +54 -53
  283. package/ccw/src/templates/dashboard-js/views/skills-manager.js +819 -0
  284. package/ccw/src/templates/dashboard.html +185 -85
  285. package/ccw/src/templates/hooks-config-example.json +60 -0
  286. package/ccw/src/tools/classify-folders.ts +245 -0
  287. package/ccw/src/tools/cli-config-manager.ts +268 -0
  288. package/ccw/src/tools/cli-executor.ts +2014 -0
  289. package/ccw/src/tools/cli-history-store.ts +1195 -0
  290. package/ccw/src/tools/codex-lens.ts +1141 -0
  291. package/ccw/src/tools/{convert-tokens-to-css.js → convert-tokens-to-css.ts} +73 -23
  292. package/ccw/src/tools/core-memory.ts +444 -0
  293. package/ccw/src/tools/detect-changed-modules.ts +325 -0
  294. package/ccw/src/tools/{discover-design-files.js → discover-design-files.ts} +74 -24
  295. package/ccw/src/tools/edit-file.ts +568 -0
  296. package/ccw/src/tools/{generate-module-docs.js → generate-module-docs.ts} +207 -185
  297. package/ccw/src/tools/{get-modules-by-depth.js → get-modules-by-depth.ts} +120 -79
  298. package/ccw/src/tools/index.ts +370 -0
  299. package/ccw/src/tools/native-session-discovery.ts +795 -0
  300. package/ccw/src/tools/notifier.ts +129 -0
  301. package/ccw/src/tools/read-file.ts +410 -0
  302. package/ccw/src/tools/resume-strategy.ts +345 -0
  303. package/ccw/src/tools/session-content-parser.ts +619 -0
  304. package/ccw/src/tools/session-manager.ts +1026 -0
  305. package/ccw/src/tools/smart-context.ts +228 -0
  306. package/ccw/src/tools/smart-search.ts +2065 -0
  307. package/ccw/src/tools/smart-search.ts.backup +1233 -0
  308. package/ccw/src/tools/storage-manager.ts +455 -0
  309. package/ccw/src/tools/write-file.ts +222 -0
  310. package/ccw/src/types/config.ts +11 -0
  311. package/ccw/src/types/index.ts +3 -0
  312. package/ccw/src/types/session.ts +25 -0
  313. package/ccw/src/types/tool.ts +41 -0
  314. package/ccw/src/utils/{browser-launcher.js → browser-launcher.ts} +10 -8
  315. package/ccw/src/utils/file-utils.ts +48 -0
  316. package/ccw/src/utils/{path-resolver.js → path-resolver.ts} +114 -78
  317. package/ccw/src/utils/path-validator.ts +153 -0
  318. package/ccw/src/utils/{ui.js → ui.ts} +32 -25
  319. package/codex-lens/pyproject.toml +48 -0
  320. package/codex-lens/src/codexlens/.workflow/.cli-history/history.db +0 -0
  321. package/codex-lens/src/codexlens/__init__.py +28 -0
  322. package/codex-lens/src/codexlens/__main__.py +14 -0
  323. package/codex-lens/src/codexlens/__pycache__/__init__.cpython-313.pyc +0 -0
  324. package/codex-lens/src/codexlens/__pycache__/__main__.cpython-313.pyc +0 -0
  325. package/codex-lens/src/codexlens/__pycache__/config.cpython-313.pyc +0 -0
  326. package/codex-lens/src/codexlens/__pycache__/entities.cpython-313.pyc +0 -0
  327. package/codex-lens/src/codexlens/__pycache__/errors.cpython-313.pyc +0 -0
  328. package/codex-lens/src/codexlens/cli/__init__.py +27 -0
  329. package/codex-lens/src/codexlens/cli/__pycache__/__init__.cpython-313.pyc +0 -0
  330. package/codex-lens/src/codexlens/cli/__pycache__/commands.cpython-313.pyc +0 -0
  331. package/codex-lens/src/codexlens/cli/__pycache__/embedding_manager.cpython-313.pyc +0 -0
  332. package/codex-lens/src/codexlens/cli/__pycache__/model_manager.cpython-313.pyc +0 -0
  333. package/codex-lens/src/codexlens/cli/__pycache__/output.cpython-313.pyc +0 -0
  334. package/codex-lens/src/codexlens/cli/commands.py +1931 -0
  335. package/codex-lens/src/codexlens/cli/embedding_manager.py +620 -0
  336. package/codex-lens/src/codexlens/cli/model_manager.py +289 -0
  337. package/codex-lens/src/codexlens/cli/output.py +124 -0
  338. package/codex-lens/src/codexlens/config.py +201 -0
  339. package/codex-lens/src/codexlens/entities.py +121 -0
  340. package/codex-lens/src/codexlens/errors.py +55 -0
  341. package/codex-lens/src/codexlens/indexing/README.md +77 -0
  342. package/codex-lens/src/codexlens/indexing/__init__.py +4 -0
  343. package/codex-lens/src/codexlens/indexing/__pycache__/__init__.cpython-313.pyc +0 -0
  344. package/codex-lens/src/codexlens/indexing/__pycache__/symbol_extractor.cpython-313.pyc +0 -0
  345. package/codex-lens/src/codexlens/indexing/symbol_extractor.py +243 -0
  346. package/codex-lens/src/codexlens/parsers/__init__.py +8 -0
  347. package/codex-lens/src/codexlens/parsers/__pycache__/__init__.cpython-313.pyc +0 -0
  348. package/codex-lens/src/codexlens/parsers/__pycache__/encoding.cpython-313.pyc +0 -0
  349. package/codex-lens/src/codexlens/parsers/__pycache__/factory.cpython-313.pyc +0 -0
  350. package/codex-lens/src/codexlens/parsers/__pycache__/tokenizer.cpython-313.pyc +0 -0
  351. package/codex-lens/src/codexlens/parsers/__pycache__/treesitter_parser.cpython-313.pyc +0 -0
  352. package/codex-lens/src/codexlens/parsers/encoding.py +202 -0
  353. package/codex-lens/src/codexlens/parsers/factory.py +256 -0
  354. package/codex-lens/src/codexlens/parsers/tokenizer.py +98 -0
  355. package/codex-lens/src/codexlens/parsers/treesitter_parser.py +335 -0
  356. package/codex-lens/src/codexlens/search/__init__.py +15 -0
  357. package/codex-lens/src/codexlens/search/__pycache__/__init__.cpython-313.pyc +0 -0
  358. package/codex-lens/src/codexlens/search/__pycache__/chain_search.cpython-313.pyc +0 -0
  359. package/codex-lens/src/codexlens/search/__pycache__/enrichment.cpython-313.pyc +0 -0
  360. package/codex-lens/src/codexlens/search/__pycache__/hybrid_search.cpython-313.pyc +0 -0
  361. package/codex-lens/src/codexlens/search/__pycache__/query_parser.cpython-313.pyc +0 -0
  362. package/codex-lens/src/codexlens/search/__pycache__/ranking.cpython-313.pyc +0 -0
  363. package/codex-lens/src/codexlens/search/chain_search.py +647 -0
  364. package/codex-lens/src/codexlens/search/enrichment.py +150 -0
  365. package/codex-lens/src/codexlens/search/hybrid_search.py +313 -0
  366. package/codex-lens/src/codexlens/search/query_parser.py +242 -0
  367. package/codex-lens/src/codexlens/search/ranking.py +274 -0
  368. package/codex-lens/src/codexlens/semantic/__init__.py +39 -0
  369. package/codex-lens/src/codexlens/semantic/__pycache__/__init__.cpython-313.pyc +0 -0
  370. package/codex-lens/src/codexlens/semantic/__pycache__/ann_index.cpython-313.pyc +0 -0
  371. package/codex-lens/src/codexlens/semantic/__pycache__/chunker.cpython-313.pyc +0 -0
  372. package/codex-lens/src/codexlens/semantic/__pycache__/code_extractor.cpython-313.pyc +0 -0
  373. package/codex-lens/src/codexlens/semantic/__pycache__/embedder.cpython-313.pyc +0 -0
  374. package/codex-lens/src/codexlens/semantic/__pycache__/graph_analyzer.cpython-313.pyc +0 -0
  375. package/codex-lens/src/codexlens/semantic/__pycache__/llm_enhancer.cpython-313.pyc +0 -0
  376. package/codex-lens/src/codexlens/semantic/__pycache__/vector_store.cpython-313.pyc +0 -0
  377. package/codex-lens/src/codexlens/semantic/ann_index.py +414 -0
  378. package/codex-lens/src/codexlens/semantic/chunker.py +448 -0
  379. package/codex-lens/src/codexlens/semantic/code_extractor.py +274 -0
  380. package/codex-lens/src/codexlens/semantic/embedder.py +185 -0
  381. package/codex-lens/src/codexlens/semantic/vector_store.py +955 -0
  382. package/codex-lens/src/codexlens/storage/__init__.py +29 -0
  383. package/codex-lens/src/codexlens/storage/__pycache__/__init__.cpython-313.pyc +0 -0
  384. package/codex-lens/src/codexlens/storage/__pycache__/dir_index.cpython-313.pyc +0 -0
  385. package/codex-lens/src/codexlens/storage/__pycache__/file_cache.cpython-313.pyc +0 -0
  386. package/codex-lens/src/codexlens/storage/__pycache__/index_tree.cpython-313.pyc +0 -0
  387. package/codex-lens/src/codexlens/storage/__pycache__/migration_manager.cpython-313.pyc +0 -0
  388. package/codex-lens/src/codexlens/storage/__pycache__/path_mapper.cpython-313.pyc +0 -0
  389. package/codex-lens/src/codexlens/storage/__pycache__/registry.cpython-313.pyc +0 -0
  390. package/codex-lens/src/codexlens/storage/__pycache__/sqlite_store.cpython-313.pyc +0 -0
  391. package/codex-lens/src/codexlens/storage/__pycache__/sqlite_utils.cpython-313.pyc +0 -0
  392. package/codex-lens/src/codexlens/storage/dir_index.py +1850 -0
  393. package/codex-lens/src/codexlens/storage/file_cache.py +32 -0
  394. package/codex-lens/src/codexlens/storage/index_tree.py +776 -0
  395. package/codex-lens/src/codexlens/storage/migration_manager.py +154 -0
  396. package/codex-lens/src/codexlens/storage/migrations/__init__.py +1 -0
  397. package/codex-lens/src/codexlens/storage/migrations/__pycache__/__init__.cpython-313.pyc +0 -0
  398. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_001_normalize_keywords.cpython-313.pyc +0 -0
  399. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_002_add_token_metadata.cpython-313.pyc +0 -0
  400. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_003_code_relationships.cpython-313.pyc +0 -0
  401. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_004_dual_fts.cpython-313.pyc +0 -0
  402. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_005_cleanup_unused_fields.cpython-313.pyc +0 -0
  403. package/codex-lens/src/codexlens/storage/migrations/migration_001_normalize_keywords.py +123 -0
  404. package/codex-lens/src/codexlens/storage/migrations/migration_002_add_token_metadata.py +48 -0
  405. package/codex-lens/src/codexlens/storage/migrations/migration_004_dual_fts.py +232 -0
  406. package/codex-lens/src/codexlens/storage/migrations/migration_005_cleanup_unused_fields.py +196 -0
  407. package/codex-lens/src/codexlens/storage/path_mapper.py +274 -0
  408. package/codex-lens/src/codexlens/storage/registry.py +670 -0
  409. package/codex-lens/src/codexlens/storage/sqlite_store.py +576 -0
  410. package/codex-lens/src/codexlens/storage/sqlite_utils.py +64 -0
  411. package/package.json +4 -1
  412. package/.claude/commands/memory/tech-research.md +0 -477
  413. package/.claude/scripts/classify-folders.sh +0 -39
  414. package/.claude/scripts/convert_tokens_to_css.sh +0 -229
  415. package/.claude/scripts/detect_changed_modules.sh +0 -161
  416. package/.claude/scripts/discover-design-files.sh +0 -87
  417. package/.claude/scripts/extract-animations.js +0 -243
  418. package/.claude/scripts/extract-computed-styles.js +0 -118
  419. package/.claude/scripts/extract-layout-structure.js +0 -411
  420. package/.claude/scripts/generate_module_docs.sh +0 -717
  421. package/.claude/scripts/get_modules_by_depth.sh +0 -170
  422. package/.claude/scripts/ui-generate-preview.sh +0 -395
  423. package/.claude/scripts/ui-instantiate-prototypes.sh +0 -815
  424. package/.claude/scripts/update_module_claude.sh +0 -337
  425. package/.claude/workflows/context-search-strategy.md +0 -77
  426. package/.claude/workflows/intelligent-tools-strategy.md +0 -662
  427. package/ccw/src/cli.js +0 -119
  428. package/ccw/src/commands/install.js +0 -324
  429. package/ccw/src/commands/tool.js +0 -138
  430. package/ccw/src/core/data-aggregator.js +0 -409
  431. package/ccw/src/core/server.js +0 -2063
  432. package/ccw/src/core/session-scanner.js +0 -235
  433. package/ccw/src/tools/classify-folders.js +0 -204
  434. package/ccw/src/tools/detect-changed-modules.js +0 -288
  435. package/ccw/src/tools/edit-file.js +0 -266
  436. package/ccw/src/tools/index.js +0 -176
  437. package/ccw/src/utils/file-utils.js +0 -48
@@ -0,0 +1,1528 @@
1
+ /**
2
+ * Core Memory Store - Independent storage system for core memories
3
+ * Provides persistent storage for high-level architectural and strategic context
4
+ */
5
+
6
+ import Database from 'better-sqlite3';
7
+ import { existsSync, mkdirSync } from 'fs';
8
+ import { join } from 'path';
9
+ import { StoragePaths, ensureStorageDir } from '../config/storage-paths.js';
10
+
11
+ // Types
12
+ export interface CoreMemory {
13
+ id: string; // Format: CMEM-YYYYMMDD-HHMMSS
14
+ content: string;
15
+ summary: string;
16
+ raw_output?: string;
17
+ created_at: string;
18
+ updated_at: string;
19
+ archived: boolean;
20
+ metadata?: string; // JSON string
21
+ }
22
+
23
+ export interface SessionCluster {
24
+ id: string; // Format: CLST-YYYYMMDD-HHMMSS
25
+ name: string;
26
+ description?: string;
27
+ intent?: string;
28
+ created_at: string;
29
+ updated_at: string;
30
+ status: 'active' | 'archived' | 'merged';
31
+ metadata?: string;
32
+ }
33
+
34
+ export interface ClusterMember {
35
+ cluster_id: string;
36
+ session_id: string;
37
+ session_type: 'core_memory' | 'workflow' | 'cli_history' | 'native';
38
+ sequence_order: number;
39
+ added_at: string;
40
+ relevance_score: number;
41
+ }
42
+
43
+ export interface ClusterRelation {
44
+ source_cluster_id: string;
45
+ target_cluster_id: string;
46
+ relation_type: 'depends_on' | 'extends' | 'conflicts_with' | 'related_to';
47
+ created_at: string;
48
+ }
49
+
50
+ export interface SessionMetadataCache {
51
+ session_id: string;
52
+ session_type: string;
53
+ title?: string;
54
+ summary?: string;
55
+ keywords?: string[]; // stored as JSON
56
+ token_estimate?: number;
57
+ file_patterns?: string[]; // stored as JSON
58
+ created_at?: string;
59
+ last_accessed?: string;
60
+ access_count: number;
61
+ }
62
+
63
+ export interface MemoryChunk {
64
+ id?: number;
65
+ source_id: string;
66
+ source_type: 'core_memory' | 'workflow' | 'cli_history';
67
+ chunk_index: number;
68
+ content: string;
69
+ embedding?: Buffer;
70
+ metadata?: string;
71
+ created_at: string;
72
+ }
73
+
74
+ export interface ClaudeUpdateRecord {
75
+ id?: number;
76
+ file_path: string;
77
+ file_level: 'user' | 'project' | 'module';
78
+ module_path?: string;
79
+ updated_at: string;
80
+ update_source: 'manual' | 'cli_sync' | 'dashboard' | 'api';
81
+ git_commit_hash?: string;
82
+ files_changed_before_update: number;
83
+ metadata?: string;
84
+ }
85
+
86
+ /**
87
+ * Core Memory Store using SQLite
88
+ */
89
+ export class CoreMemoryStore {
90
+ private db: Database.Database;
91
+ private dbPath: string;
92
+ private projectPath: string;
93
+
94
+ constructor(projectPath: string) {
95
+ this.projectPath = projectPath;
96
+ // Use centralized storage path
97
+ const paths = StoragePaths.project(projectPath);
98
+ const coreMemoryDir = join(paths.root, 'core-memory');
99
+ ensureStorageDir(coreMemoryDir);
100
+
101
+ this.dbPath = join(coreMemoryDir, 'core_memory.db');
102
+ this.db = new Database(this.dbPath);
103
+ this.db.pragma('journal_mode = WAL');
104
+ this.db.pragma('synchronous = NORMAL');
105
+
106
+ this.initDatabase();
107
+ }
108
+
109
+ /**
110
+ * Initialize database schema
111
+ */
112
+ private initDatabase(): void {
113
+ // Migrate old tables
114
+ this.migrateDatabase();
115
+
116
+ this.db.exec(`
117
+ -- Core memories table
118
+ CREATE TABLE IF NOT EXISTS memories (
119
+ id TEXT PRIMARY KEY,
120
+ content TEXT NOT NULL,
121
+ summary TEXT,
122
+ raw_output TEXT,
123
+ created_at TEXT NOT NULL,
124
+ updated_at TEXT NOT NULL,
125
+ archived INTEGER DEFAULT 0,
126
+ metadata TEXT
127
+ );
128
+
129
+ -- Session clusters table
130
+ CREATE TABLE IF NOT EXISTS session_clusters (
131
+ id TEXT PRIMARY KEY,
132
+ name TEXT NOT NULL,
133
+ description TEXT,
134
+ intent TEXT,
135
+ created_at TEXT NOT NULL,
136
+ updated_at TEXT NOT NULL,
137
+ status TEXT DEFAULT 'active',
138
+ metadata TEXT
139
+ );
140
+
141
+ -- Cluster members table
142
+ CREATE TABLE IF NOT EXISTS cluster_members (
143
+ cluster_id TEXT NOT NULL,
144
+ session_id TEXT NOT NULL,
145
+ session_type TEXT NOT NULL,
146
+ sequence_order INTEGER NOT NULL,
147
+ added_at TEXT NOT NULL,
148
+ relevance_score REAL DEFAULT 1.0,
149
+ PRIMARY KEY (cluster_id, session_id),
150
+ FOREIGN KEY (cluster_id) REFERENCES session_clusters(id) ON DELETE CASCADE
151
+ );
152
+
153
+ -- Cluster relations table
154
+ CREATE TABLE IF NOT EXISTS cluster_relations (
155
+ source_cluster_id TEXT NOT NULL,
156
+ target_cluster_id TEXT NOT NULL,
157
+ relation_type TEXT NOT NULL,
158
+ created_at TEXT NOT NULL,
159
+ PRIMARY KEY (source_cluster_id, target_cluster_id),
160
+ FOREIGN KEY (source_cluster_id) REFERENCES session_clusters(id) ON DELETE CASCADE,
161
+ FOREIGN KEY (target_cluster_id) REFERENCES session_clusters(id) ON DELETE CASCADE
162
+ );
163
+
164
+ -- Session metadata cache table
165
+ CREATE TABLE IF NOT EXISTS session_metadata_cache (
166
+ session_id TEXT PRIMARY KEY,
167
+ session_type TEXT NOT NULL,
168
+ title TEXT,
169
+ summary TEXT,
170
+ keywords TEXT,
171
+ token_estimate INTEGER,
172
+ file_patterns TEXT,
173
+ created_at TEXT,
174
+ last_accessed TEXT,
175
+ access_count INTEGER DEFAULT 0
176
+ );
177
+
178
+ -- Memory chunks table for embeddings
179
+ CREATE TABLE IF NOT EXISTS memory_chunks (
180
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
181
+ source_id TEXT NOT NULL,
182
+ source_type TEXT NOT NULL,
183
+ chunk_index INTEGER NOT NULL,
184
+ content TEXT NOT NULL,
185
+ embedding BLOB,
186
+ metadata TEXT,
187
+ created_at TEXT NOT NULL,
188
+ UNIQUE(source_id, chunk_index)
189
+ );
190
+
191
+ -- CLAUDE.md update history table
192
+ CREATE TABLE IF NOT EXISTS claude_update_history (
193
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
194
+ file_path TEXT NOT NULL,
195
+ file_level TEXT NOT NULL CHECK(file_level IN ('user', 'project', 'module')),
196
+ module_path TEXT,
197
+ updated_at TEXT NOT NULL,
198
+ update_source TEXT NOT NULL CHECK(update_source IN ('manual', 'cli_sync', 'dashboard', 'api')),
199
+ git_commit_hash TEXT,
200
+ files_changed_before_update INTEGER DEFAULT 0,
201
+ metadata TEXT,
202
+ UNIQUE(file_path, updated_at)
203
+ );
204
+
205
+ -- Indexes for efficient queries
206
+ CREATE INDEX IF NOT EXISTS idx_memories_created ON memories(created_at DESC);
207
+ CREATE INDEX IF NOT EXISTS idx_memories_updated ON memories(updated_at DESC);
208
+ CREATE INDEX IF NOT EXISTS idx_memories_archived ON memories(archived);
209
+ CREATE INDEX IF NOT EXISTS idx_session_clusters_status ON session_clusters(status);
210
+ CREATE INDEX IF NOT EXISTS idx_cluster_members_cluster ON cluster_members(cluster_id);
211
+ CREATE INDEX IF NOT EXISTS idx_cluster_members_session ON cluster_members(session_id);
212
+ CREATE INDEX IF NOT EXISTS idx_session_metadata_type ON session_metadata_cache(session_type);
213
+ CREATE INDEX IF NOT EXISTS idx_memory_chunks_source ON memory_chunks(source_id, source_type);
214
+ CREATE INDEX IF NOT EXISTS idx_memory_chunks_embedded ON memory_chunks(embedding IS NOT NULL);
215
+ CREATE INDEX IF NOT EXISTS idx_claude_history_path ON claude_update_history(file_path);
216
+ CREATE INDEX IF NOT EXISTS idx_claude_history_updated ON claude_update_history(updated_at DESC);
217
+ CREATE INDEX IF NOT EXISTS idx_claude_history_module ON claude_update_history(module_path);
218
+ `);
219
+ }
220
+
221
+ /**
222
+ * Migrate database by removing old tables, views, and triggers
223
+ */
224
+ private migrateDatabase(): void {
225
+ const oldTables = ['knowledge_graph', 'knowledge_graph_edges', 'evolution_history'];
226
+
227
+ try {
228
+ // Disable foreign key constraints during migration
229
+ this.db.pragma('foreign_keys = OFF');
230
+
231
+ // Drop any triggers that might reference old tables
232
+ const triggers = this.db.prepare(
233
+ `SELECT name FROM sqlite_master WHERE type='trigger'`
234
+ ).all() as { name: string }[];
235
+
236
+ for (const trigger of triggers) {
237
+ try {
238
+ this.db.exec(`DROP TRIGGER IF EXISTS "${trigger.name}"`);
239
+ } catch (e) {
240
+ // Ignore trigger drop errors
241
+ }
242
+ }
243
+
244
+ // Drop any views that might reference old tables
245
+ const views = this.db.prepare(
246
+ `SELECT name FROM sqlite_master WHERE type='view'`
247
+ ).all() as { name: string }[];
248
+
249
+ for (const view of views) {
250
+ try {
251
+ this.db.exec(`DROP VIEW IF EXISTS "${view.name}"`);
252
+ } catch (e) {
253
+ // Ignore view drop errors
254
+ }
255
+ }
256
+
257
+ // Now drop the old tables
258
+ for (const table of oldTables) {
259
+ try {
260
+ this.db.exec(`DROP TABLE IF EXISTS "${table}"`);
261
+ } catch (e) {
262
+ // Ignore if table doesn't exist
263
+ }
264
+ }
265
+
266
+ // Re-enable foreign key constraints
267
+ this.db.pragma('foreign_keys = ON');
268
+ } catch (e) {
269
+ // If migration fails, continue - tables may not exist
270
+ try {
271
+ this.db.pragma('foreign_keys = ON');
272
+ } catch (_) {
273
+ // Ignore
274
+ }
275
+ }
276
+ }
277
+
278
+ /**
279
+ * Generate timestamp-based ID for core memory
280
+ */
281
+ private generateId(): string {
282
+ const now = new Date();
283
+ const year = now.getFullYear();
284
+ const month = String(now.getMonth() + 1).padStart(2, '0');
285
+ const day = String(now.getDate()).padStart(2, '0');
286
+ const hours = String(now.getHours()).padStart(2, '0');
287
+ const minutes = String(now.getMinutes()).padStart(2, '0');
288
+ const seconds = String(now.getSeconds()).padStart(2, '0');
289
+ return `CMEM-${year}${month}${day}-${hours}${minutes}${seconds}`;
290
+ }
291
+
292
+ /**
293
+ * Generate cluster ID
294
+ */
295
+ generateClusterId(): string {
296
+ const now = new Date();
297
+ const year = now.getFullYear();
298
+ const month = String(now.getMonth() + 1).padStart(2, '0');
299
+ const day = String(now.getDate()).padStart(2, '0');
300
+ const hours = String(now.getHours()).padStart(2, '0');
301
+ const minutes = String(now.getMinutes()).padStart(2, '0');
302
+ const seconds = String(now.getSeconds()).padStart(2, '0');
303
+ const ms = String(now.getMilliseconds()).padStart(3, '0');
304
+ // Add random 4-digit suffix to ensure uniqueness (10000 combinations)
305
+ const random = String(Math.floor(Math.random() * 10000)).padStart(4, '0');
306
+ return `CLST-${year}${month}${day}-${hours}${minutes}${seconds}${ms}${random}`;
307
+ }
308
+
309
+ /**
310
+ * Upsert a core memory
311
+ */
312
+ upsertMemory(memory: Partial<CoreMemory> & { content: string }): CoreMemory {
313
+ const now = new Date().toISOString();
314
+ const id = memory.id || this.generateId();
315
+
316
+ // Check if memory exists
317
+ const existingMemory = this.getMemory(id);
318
+
319
+ if (existingMemory) {
320
+ // Update existing memory
321
+ const stmt = this.db.prepare(`
322
+ UPDATE memories
323
+ SET content = ?, summary = ?, raw_output = ?, updated_at = ?, archived = ?, metadata = ?
324
+ WHERE id = ?
325
+ `);
326
+
327
+ stmt.run(
328
+ memory.content,
329
+ memory.summary || existingMemory.summary,
330
+ memory.raw_output || existingMemory.raw_output,
331
+ now,
332
+ memory.archived !== undefined ? (memory.archived ? 1 : 0) : existingMemory.archived ? 1 : 0,
333
+ memory.metadata || existingMemory.metadata,
334
+ id
335
+ );
336
+
337
+ return this.getMemory(id)!;
338
+ } else {
339
+ // Insert new memory
340
+ const stmt = this.db.prepare(`
341
+ INSERT INTO memories (id, content, summary, raw_output, created_at, updated_at, archived, metadata)
342
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
343
+ `);
344
+
345
+ stmt.run(
346
+ id,
347
+ memory.content,
348
+ memory.summary || '',
349
+ memory.raw_output || null,
350
+ now,
351
+ now,
352
+ memory.archived ? 1 : 0,
353
+ memory.metadata || null
354
+ );
355
+
356
+ return this.getMemory(id)!;
357
+ }
358
+ }
359
+
360
+ /**
361
+ * Get memory by ID
362
+ */
363
+ getMemory(id: string): CoreMemory | null {
364
+ const stmt = this.db.prepare(`SELECT * FROM memories WHERE id = ?`);
365
+ const row = stmt.get(id) as any;
366
+ if (!row) return null;
367
+
368
+ return {
369
+ id: row.id,
370
+ content: row.content,
371
+ summary: row.summary,
372
+ raw_output: row.raw_output,
373
+ created_at: row.created_at,
374
+ updated_at: row.updated_at,
375
+ archived: Boolean(row.archived),
376
+ metadata: row.metadata
377
+ };
378
+ }
379
+
380
+ /**
381
+ * Get all memories
382
+ */
383
+ getMemories(options: { archived?: boolean; limit?: number; offset?: number } = {}): CoreMemory[] {
384
+ const { archived = false, limit = 50, offset = 0 } = options;
385
+
386
+ const stmt = this.db.prepare(`
387
+ SELECT * FROM memories
388
+ WHERE archived = ?
389
+ ORDER BY updated_at DESC
390
+ LIMIT ? OFFSET ?
391
+ `);
392
+
393
+ const rows = stmt.all(archived ? 1 : 0, limit, offset) as any[];
394
+ return rows.map(row => ({
395
+ id: row.id,
396
+ content: row.content,
397
+ summary: row.summary,
398
+ raw_output: row.raw_output,
399
+ created_at: row.created_at,
400
+ updated_at: row.updated_at,
401
+ archived: Boolean(row.archived),
402
+ metadata: row.metadata
403
+ }));
404
+ }
405
+
406
+ /**
407
+ * Archive a memory
408
+ */
409
+ archiveMemory(id: string): void {
410
+ const stmt = this.db.prepare(`
411
+ UPDATE memories
412
+ SET archived = 1, updated_at = ?
413
+ WHERE id = ?
414
+ `);
415
+ stmt.run(new Date().toISOString(), id);
416
+ }
417
+
418
+ /**
419
+ * Delete a memory
420
+ */
421
+ deleteMemory(id: string): void {
422
+ const stmt = this.db.prepare(`DELETE FROM memories WHERE id = ?`);
423
+ stmt.run(id);
424
+ }
425
+
426
+ /**
427
+ * Generate summary for a memory using CLI tool
428
+ */
429
+ async generateSummary(memoryId: string, tool: 'gemini' | 'qwen' = 'gemini'): Promise<string> {
430
+ const memory = this.getMemory(memoryId);
431
+ if (!memory) throw new Error('Memory not found');
432
+
433
+ // Import CLI executor
434
+ const { executeCliTool } = await import('../tools/cli-executor.js');
435
+
436
+ const prompt = `
437
+ PURPOSE: Generate a concise summary (2-3 sentences) of the following core memory content
438
+ TASK: Extract key architectural decisions, strategic insights, and important context
439
+ MODE: analysis
440
+ EXPECTED: Plain text summary without markdown or formatting
441
+ RULES: Be concise. Focus on high-level understanding. No technical jargon unless essential.
442
+
443
+ CONTENT:
444
+ ${memory.content}
445
+ `;
446
+
447
+ const result = await executeCliTool({
448
+ tool,
449
+ prompt,
450
+ mode: 'analysis',
451
+ timeout: 60000,
452
+ cd: this.projectPath,
453
+ category: 'internal'
454
+ });
455
+
456
+ const summary = result.stdout?.trim() || 'Failed to generate summary';
457
+
458
+ // Update memory with summary
459
+ const stmt = this.db.prepare(`
460
+ UPDATE memories
461
+ SET summary = ?, updated_at = ?
462
+ WHERE id = ?
463
+ `);
464
+ stmt.run(summary, new Date().toISOString(), memoryId);
465
+
466
+ return summary;
467
+ }
468
+
469
+ /**
470
+ * Create a new session cluster
471
+ */
472
+ createCluster(cluster: Partial<SessionCluster> & { name: string }): SessionCluster {
473
+ const now = new Date().toISOString();
474
+ const id = cluster.id || this.generateClusterId();
475
+
476
+ const stmt = this.db.prepare(`
477
+ INSERT INTO session_clusters (id, name, description, intent, created_at, updated_at, status, metadata)
478
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
479
+ `);
480
+
481
+ stmt.run(
482
+ id,
483
+ cluster.name,
484
+ cluster.description || null,
485
+ cluster.intent || null,
486
+ now,
487
+ now,
488
+ cluster.status || 'active',
489
+ cluster.metadata || null
490
+ );
491
+
492
+ return this.getCluster(id)!;
493
+ }
494
+
495
+ /**
496
+ * Get cluster by ID
497
+ */
498
+ getCluster(id: string): SessionCluster | null {
499
+ const stmt = this.db.prepare(`SELECT * FROM session_clusters WHERE id = ?`);
500
+ const row = stmt.get(id) as any;
501
+ if (!row) return null;
502
+
503
+ return {
504
+ id: row.id,
505
+ name: row.name,
506
+ description: row.description,
507
+ intent: row.intent,
508
+ created_at: row.created_at,
509
+ updated_at: row.updated_at,
510
+ status: row.status,
511
+ metadata: row.metadata
512
+ };
513
+ }
514
+
515
+ /**
516
+ * List all clusters
517
+ */
518
+ listClusters(status?: string): SessionCluster[] {
519
+ let query = 'SELECT * FROM session_clusters';
520
+ const params: any[] = [];
521
+
522
+ if (status) {
523
+ query += ' WHERE status = ?';
524
+ params.push(status);
525
+ }
526
+
527
+ query += ' ORDER BY updated_at DESC';
528
+
529
+ const stmt = this.db.prepare(query);
530
+ const rows = stmt.all(...params) as any[];
531
+
532
+ return rows.map(row => ({
533
+ id: row.id,
534
+ name: row.name,
535
+ description: row.description,
536
+ intent: row.intent,
537
+ created_at: row.created_at,
538
+ updated_at: row.updated_at,
539
+ status: row.status,
540
+ metadata: row.metadata
541
+ }));
542
+ }
543
+
544
+ /**
545
+ * Update cluster
546
+ */
547
+ updateCluster(id: string, updates: Partial<SessionCluster>): SessionCluster | null {
548
+ const existing = this.getCluster(id);
549
+ if (!existing) return null;
550
+
551
+ const now = new Date().toISOString();
552
+ const stmt = this.db.prepare(`
553
+ UPDATE session_clusters
554
+ SET name = ?, description = ?, intent = ?, updated_at = ?, status = ?, metadata = ?
555
+ WHERE id = ?
556
+ `);
557
+
558
+ stmt.run(
559
+ updates.name || existing.name,
560
+ updates.description !== undefined ? updates.description : existing.description,
561
+ updates.intent !== undefined ? updates.intent : existing.intent,
562
+ now,
563
+ updates.status || existing.status,
564
+ updates.metadata !== undefined ? updates.metadata : existing.metadata,
565
+ id
566
+ );
567
+
568
+ return this.getCluster(id);
569
+ }
570
+
571
+ /**
572
+ * Delete cluster
573
+ */
574
+ deleteCluster(id: string): boolean {
575
+ const stmt = this.db.prepare(`DELETE FROM session_clusters WHERE id = ?`);
576
+ const result = stmt.run(id);
577
+ return result.changes > 0;
578
+ }
579
+
580
+ /**
581
+ * Merge multiple clusters into one
582
+ * Keeps the first cluster and moves all members from others into it
583
+ * @param targetClusterId The cluster to keep
584
+ * @param sourceClusterIds The clusters to merge into target (will be deleted)
585
+ * @returns Number of members moved
586
+ */
587
+ mergeClusters(targetClusterId: string, sourceClusterIds: string[]): number {
588
+ const targetCluster = this.getCluster(targetClusterId);
589
+ if (!targetCluster) {
590
+ throw new Error(`Target cluster not found: ${targetClusterId}`);
591
+ }
592
+
593
+ let membersMoved = 0;
594
+ const existingMembers = new Set(
595
+ this.getClusterMembers(targetClusterId).map(m => m.session_id)
596
+ );
597
+
598
+ for (const sourceId of sourceClusterIds) {
599
+ if (sourceId === targetClusterId) continue;
600
+
601
+ const sourceMembers = this.getClusterMembers(sourceId);
602
+ const maxOrder = this.getClusterMembers(targetClusterId).length;
603
+
604
+ for (const member of sourceMembers) {
605
+ // Skip if already exists in target
606
+ if (existingMembers.has(member.session_id)) continue;
607
+
608
+ // Move member to target cluster
609
+ this.addClusterMember({
610
+ cluster_id: targetClusterId,
611
+ session_id: member.session_id,
612
+ session_type: member.session_type,
613
+ sequence_order: maxOrder + membersMoved + 1,
614
+ relevance_score: member.relevance_score
615
+ });
616
+
617
+ existingMembers.add(member.session_id);
618
+ membersMoved++;
619
+ }
620
+
621
+ // Delete source cluster
622
+ this.deleteCluster(sourceId);
623
+ }
624
+
625
+ // Update target cluster description
626
+ const finalMembers = this.getClusterMembers(targetClusterId);
627
+ this.updateCluster(targetClusterId, {
628
+ description: `Merged cluster with ${finalMembers.length} sessions`
629
+ });
630
+
631
+ return membersMoved;
632
+ }
633
+
634
+ /**
635
+ * Add member to cluster
636
+ */
637
+ addClusterMember(member: Omit<ClusterMember, 'added_at'>): ClusterMember {
638
+ const now = new Date().toISOString();
639
+
640
+ const stmt = this.db.prepare(`
641
+ INSERT INTO cluster_members (cluster_id, session_id, session_type, sequence_order, added_at, relevance_score)
642
+ VALUES (?, ?, ?, ?, ?, ?)
643
+ `);
644
+
645
+ stmt.run(
646
+ member.cluster_id,
647
+ member.session_id,
648
+ member.session_type,
649
+ member.sequence_order,
650
+ now,
651
+ member.relevance_score
652
+ );
653
+
654
+ return {
655
+ ...member,
656
+ added_at: now
657
+ };
658
+ }
659
+
660
+ /**
661
+ * Remove member from cluster
662
+ */
663
+ removeClusterMember(clusterId: string, sessionId: string): boolean {
664
+ const stmt = this.db.prepare(`
665
+ DELETE FROM cluster_members
666
+ WHERE cluster_id = ? AND session_id = ?
667
+ `);
668
+ const result = stmt.run(clusterId, sessionId);
669
+ return result.changes > 0;
670
+ }
671
+
672
+ /**
673
+ * Get all members of a cluster
674
+ */
675
+ getClusterMembers(clusterId: string): ClusterMember[] {
676
+ const stmt = this.db.prepare(`
677
+ SELECT * FROM cluster_members
678
+ WHERE cluster_id = ?
679
+ ORDER BY sequence_order ASC
680
+ `);
681
+
682
+ const rows = stmt.all(clusterId) as any[];
683
+ return rows.map(row => ({
684
+ cluster_id: row.cluster_id,
685
+ session_id: row.session_id,
686
+ session_type: row.session_type,
687
+ sequence_order: row.sequence_order,
688
+ added_at: row.added_at,
689
+ relevance_score: row.relevance_score
690
+ }));
691
+ }
692
+
693
+ /**
694
+ * Get all clusters that contain a session
695
+ */
696
+ getSessionClusters(sessionId: string): SessionCluster[] {
697
+ const stmt = this.db.prepare(`
698
+ SELECT sc.*
699
+ FROM session_clusters sc
700
+ INNER JOIN cluster_members cm ON sc.id = cm.cluster_id
701
+ WHERE cm.session_id = ?
702
+ ORDER BY sc.updated_at DESC
703
+ `);
704
+
705
+ const rows = stmt.all(sessionId) as any[];
706
+ return rows.map(row => ({
707
+ id: row.id,
708
+ name: row.name,
709
+ description: row.description,
710
+ intent: row.intent,
711
+ created_at: row.created_at,
712
+ updated_at: row.updated_at,
713
+ status: row.status,
714
+ metadata: row.metadata
715
+ }));
716
+ }
717
+
718
+ /**
719
+ * Add relation between clusters
720
+ */
721
+ addClusterRelation(relation: Omit<ClusterRelation, 'created_at'>): ClusterRelation {
722
+ const now = new Date().toISOString();
723
+
724
+ const stmt = this.db.prepare(`
725
+ INSERT INTO cluster_relations (source_cluster_id, target_cluster_id, relation_type, created_at)
726
+ VALUES (?, ?, ?, ?)
727
+ `);
728
+
729
+ stmt.run(
730
+ relation.source_cluster_id,
731
+ relation.target_cluster_id,
732
+ relation.relation_type,
733
+ now
734
+ );
735
+
736
+ return {
737
+ ...relation,
738
+ created_at: now
739
+ };
740
+ }
741
+
742
+ /**
743
+ * Remove relation between clusters
744
+ */
745
+ removeClusterRelation(sourceId: string, targetId: string): boolean {
746
+ const stmt = this.db.prepare(`
747
+ DELETE FROM cluster_relations
748
+ WHERE source_cluster_id = ? AND target_cluster_id = ?
749
+ `);
750
+ const result = stmt.run(sourceId, targetId);
751
+ return result.changes > 0;
752
+ }
753
+
754
+ /**
755
+ * Get all relations for a cluster
756
+ */
757
+ getClusterRelations(clusterId: string): ClusterRelation[] {
758
+ const stmt = this.db.prepare(`
759
+ SELECT * FROM cluster_relations
760
+ WHERE source_cluster_id = ? OR target_cluster_id = ?
761
+ ORDER BY created_at DESC
762
+ `);
763
+
764
+ const rows = stmt.all(clusterId, clusterId) as any[];
765
+ return rows.map(row => ({
766
+ source_cluster_id: row.source_cluster_id,
767
+ target_cluster_id: row.target_cluster_id,
768
+ relation_type: row.relation_type,
769
+ created_at: row.created_at
770
+ }));
771
+ }
772
+
773
+ /**
774
+ * Upsert session metadata
775
+ */
776
+ upsertSessionMetadata(metadata: SessionMetadataCache): SessionMetadataCache {
777
+ const now = new Date().toISOString();
778
+
779
+ const existing = this.getSessionMetadata(metadata.session_id);
780
+
781
+ if (existing) {
782
+ const stmt = this.db.prepare(`
783
+ UPDATE session_metadata_cache
784
+ SET session_type = ?, title = ?, summary = ?, keywords = ?, token_estimate = ?,
785
+ file_patterns = ?, last_accessed = ?, access_count = ?
786
+ WHERE session_id = ?
787
+ `);
788
+
789
+ stmt.run(
790
+ metadata.session_type,
791
+ metadata.title || null,
792
+ metadata.summary || null,
793
+ metadata.keywords ? JSON.stringify(metadata.keywords) : null,
794
+ metadata.token_estimate || null,
795
+ metadata.file_patterns ? JSON.stringify(metadata.file_patterns) : null,
796
+ now,
797
+ existing.access_count + 1,
798
+ metadata.session_id
799
+ );
800
+ } else {
801
+ const stmt = this.db.prepare(`
802
+ INSERT INTO session_metadata_cache
803
+ (session_id, session_type, title, summary, keywords, token_estimate, file_patterns, created_at, last_accessed, access_count)
804
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
805
+ `);
806
+
807
+ stmt.run(
808
+ metadata.session_id,
809
+ metadata.session_type,
810
+ metadata.title || null,
811
+ metadata.summary || null,
812
+ metadata.keywords ? JSON.stringify(metadata.keywords) : null,
813
+ metadata.token_estimate || null,
814
+ metadata.file_patterns ? JSON.stringify(metadata.file_patterns) : null,
815
+ metadata.created_at || now,
816
+ now,
817
+ metadata.access_count || 1
818
+ );
819
+ }
820
+
821
+ return this.getSessionMetadata(metadata.session_id)!;
822
+ }
823
+
824
+ /**
825
+ * Get session metadata
826
+ */
827
+ getSessionMetadata(sessionId: string): SessionMetadataCache | null {
828
+ const stmt = this.db.prepare(`SELECT * FROM session_metadata_cache WHERE session_id = ?`);
829
+ const row = stmt.get(sessionId) as any;
830
+ if (!row) return null;
831
+
832
+ return {
833
+ session_id: row.session_id,
834
+ session_type: row.session_type,
835
+ title: row.title,
836
+ summary: row.summary,
837
+ keywords: row.keywords ? JSON.parse(row.keywords) : undefined,
838
+ token_estimate: row.token_estimate,
839
+ file_patterns: row.file_patterns ? JSON.parse(row.file_patterns) : undefined,
840
+ created_at: row.created_at,
841
+ last_accessed: row.last_accessed,
842
+ access_count: row.access_count
843
+ };
844
+ }
845
+
846
+ /**
847
+ * Search sessions by keyword
848
+ */
849
+ searchSessionsByKeyword(keyword: string): SessionMetadataCache[] {
850
+ const stmt = this.db.prepare(`
851
+ SELECT * FROM session_metadata_cache
852
+ WHERE title LIKE ? OR summary LIKE ? OR keywords LIKE ?
853
+ ORDER BY access_count DESC, last_accessed DESC
854
+ `);
855
+
856
+ const pattern = `%${keyword}%`;
857
+ const rows = stmt.all(pattern, pattern, pattern) as any[];
858
+
859
+ return rows.map(row => ({
860
+ session_id: row.session_id,
861
+ session_type: row.session_type,
862
+ title: row.title,
863
+ summary: row.summary,
864
+ keywords: row.keywords ? JSON.parse(row.keywords) : undefined,
865
+ token_estimate: row.token_estimate,
866
+ file_patterns: row.file_patterns ? JSON.parse(row.file_patterns) : undefined,
867
+ created_at: row.created_at,
868
+ last_accessed: row.last_accessed,
869
+ access_count: row.access_count
870
+ }));
871
+ }
872
+
873
+ // ============================================================================
874
+ // Memory Chunks CRUD Operations
875
+ // ============================================================================
876
+
877
+ /**
878
+ * Chunk content into smaller pieces for embedding
879
+ * @param content Content to chunk
880
+ * @param sourceId Source identifier (e.g., memory ID)
881
+ * @param sourceType Type of source
882
+ * @returns Array of chunk content strings
883
+ */
884
+ chunkContent(content: string, sourceId: string, sourceType: string): string[] {
885
+ const CHUNK_SIZE = 1500;
886
+ const OVERLAP = 200;
887
+ const chunks: string[] = [];
888
+
889
+ // Split by paragraph boundaries first
890
+ const paragraphs = content.split(/\n\n+/);
891
+ let currentChunk = '';
892
+
893
+ for (const paragraph of paragraphs) {
894
+ // If adding this paragraph would exceed chunk size
895
+ if (currentChunk.length + paragraph.length > CHUNK_SIZE && currentChunk.length > 0) {
896
+ // Save current chunk
897
+ chunks.push(currentChunk.trim());
898
+
899
+ // Start new chunk with overlap
900
+ const overlapText = currentChunk.slice(-OVERLAP);
901
+ currentChunk = overlapText + '\n\n' + paragraph;
902
+ } else {
903
+ // Add paragraph to current chunk
904
+ currentChunk += (currentChunk ? '\n\n' : '') + paragraph;
905
+ }
906
+ }
907
+
908
+ // Add remaining chunk
909
+ if (currentChunk.trim()) {
910
+ chunks.push(currentChunk.trim());
911
+ }
912
+
913
+ // If no paragraphs or chunks are still too large, split by sentences
914
+ const finalChunks: string[] = [];
915
+ for (const chunk of chunks) {
916
+ if (chunk.length <= CHUNK_SIZE) {
917
+ finalChunks.push(chunk);
918
+ } else {
919
+ // Split by sentence boundaries
920
+ const sentences = chunk.split(/\. +/);
921
+ let sentenceChunk = '';
922
+
923
+ for (const sentence of sentences) {
924
+ const sentenceWithPeriod = sentence + '. ';
925
+ if (sentenceChunk.length + sentenceWithPeriod.length > CHUNK_SIZE && sentenceChunk.length > 0) {
926
+ finalChunks.push(sentenceChunk.trim());
927
+ const overlapText = sentenceChunk.slice(-OVERLAP);
928
+ sentenceChunk = overlapText + sentenceWithPeriod;
929
+ } else {
930
+ sentenceChunk += sentenceWithPeriod;
931
+ }
932
+ }
933
+
934
+ if (sentenceChunk.trim()) {
935
+ finalChunks.push(sentenceChunk.trim());
936
+ }
937
+ }
938
+ }
939
+
940
+ return finalChunks.length > 0 ? finalChunks : [content];
941
+ }
942
+
943
+ /**
944
+ * Insert a single chunk
945
+ */
946
+ insertChunk(chunk: Omit<MemoryChunk, 'id'>): number {
947
+ const now = new Date().toISOString();
948
+
949
+ const stmt = this.db.prepare(`
950
+ INSERT INTO memory_chunks (source_id, source_type, chunk_index, content, embedding, metadata, created_at)
951
+ VALUES (?, ?, ?, ?, ?, ?, ?)
952
+ `);
953
+
954
+ const result = stmt.run(
955
+ chunk.source_id,
956
+ chunk.source_type,
957
+ chunk.chunk_index,
958
+ chunk.content,
959
+ chunk.embedding || null,
960
+ chunk.metadata || null,
961
+ chunk.created_at || now
962
+ );
963
+
964
+ return result.lastInsertRowid as number;
965
+ }
966
+
967
+ /**
968
+ * Insert multiple chunks in a batch
969
+ */
970
+ insertChunksBatch(chunks: Omit<MemoryChunk, 'id'>[]): void {
971
+ const now = new Date().toISOString();
972
+ const insert = this.db.prepare(`
973
+ INSERT INTO memory_chunks (source_id, source_type, chunk_index, content, embedding, metadata, created_at)
974
+ VALUES (?, ?, ?, ?, ?, ?, ?)
975
+ `);
976
+
977
+ const transaction = this.db.transaction((chunks: Omit<MemoryChunk, 'id'>[]) => {
978
+ for (const chunk of chunks) {
979
+ insert.run(
980
+ chunk.source_id,
981
+ chunk.source_type,
982
+ chunk.chunk_index,
983
+ chunk.content,
984
+ chunk.embedding || null,
985
+ chunk.metadata || null,
986
+ chunk.created_at || now
987
+ );
988
+ }
989
+ });
990
+
991
+ transaction(chunks);
992
+ }
993
+
994
+ /**
995
+ * Get all chunks for a source
996
+ */
997
+ getChunks(sourceId: string): MemoryChunk[] {
998
+ const stmt = this.db.prepare(`
999
+ SELECT * FROM memory_chunks
1000
+ WHERE source_id = ?
1001
+ ORDER BY chunk_index ASC
1002
+ `);
1003
+
1004
+ const rows = stmt.all(sourceId) as any[];
1005
+ return rows.map(row => ({
1006
+ id: row.id,
1007
+ source_id: row.source_id,
1008
+ source_type: row.source_type,
1009
+ chunk_index: row.chunk_index,
1010
+ content: row.content,
1011
+ embedding: row.embedding,
1012
+ metadata: row.metadata,
1013
+ created_at: row.created_at
1014
+ }));
1015
+ }
1016
+
1017
+ /**
1018
+ * Get chunks by source type
1019
+ */
1020
+ getChunksByType(sourceType: string): MemoryChunk[] {
1021
+ const stmt = this.db.prepare(`
1022
+ SELECT * FROM memory_chunks
1023
+ WHERE source_type = ?
1024
+ ORDER BY source_id, chunk_index ASC
1025
+ `);
1026
+
1027
+ const rows = stmt.all(sourceType) as any[];
1028
+ return rows.map(row => ({
1029
+ id: row.id,
1030
+ source_id: row.source_id,
1031
+ source_type: row.source_type,
1032
+ chunk_index: row.chunk_index,
1033
+ content: row.content,
1034
+ embedding: row.embedding,
1035
+ metadata: row.metadata,
1036
+ created_at: row.created_at
1037
+ }));
1038
+ }
1039
+
1040
+ /**
1041
+ * Get chunks without embeddings
1042
+ */
1043
+ getUnembeddedChunks(limit?: number): MemoryChunk[] {
1044
+ const query = `
1045
+ SELECT * FROM memory_chunks
1046
+ WHERE embedding IS NULL
1047
+ ORDER BY created_at ASC
1048
+ ${limit ? 'LIMIT ?' : ''}
1049
+ `;
1050
+
1051
+ const stmt = this.db.prepare(query);
1052
+ const rows = (limit ? stmt.all(limit) : stmt.all()) as any[];
1053
+
1054
+ return rows.map(row => ({
1055
+ id: row.id,
1056
+ source_id: row.source_id,
1057
+ source_type: row.source_type,
1058
+ chunk_index: row.chunk_index,
1059
+ content: row.content,
1060
+ embedding: row.embedding,
1061
+ metadata: row.metadata,
1062
+ created_at: row.created_at
1063
+ }));
1064
+ }
1065
+
1066
+ /**
1067
+ * Update embedding for a chunk
1068
+ */
1069
+ updateChunkEmbedding(chunkId: number, embedding: Buffer): void {
1070
+ const stmt = this.db.prepare(`
1071
+ UPDATE memory_chunks
1072
+ SET embedding = ?
1073
+ WHERE id = ?
1074
+ `);
1075
+
1076
+ stmt.run(embedding, chunkId);
1077
+ }
1078
+
1079
+ /**
1080
+ * Update embeddings for multiple chunks in a batch
1081
+ */
1082
+ updateChunkEmbeddingsBatch(updates: { id: number; embedding: Buffer }[]): void {
1083
+ const update = this.db.prepare(`
1084
+ UPDATE memory_chunks
1085
+ SET embedding = ?
1086
+ WHERE id = ?
1087
+ `);
1088
+
1089
+ const transaction = this.db.transaction((updates: { id: number; embedding: Buffer }[]) => {
1090
+ for (const { id, embedding } of updates) {
1091
+ update.run(embedding, id);
1092
+ }
1093
+ });
1094
+
1095
+ transaction(updates);
1096
+ }
1097
+
1098
+ /**
1099
+ * Delete all chunks for a source
1100
+ */
1101
+ deleteChunks(sourceId: string): void {
1102
+ const stmt = this.db.prepare(`
1103
+ DELETE FROM memory_chunks
1104
+ WHERE source_id = ?
1105
+ `);
1106
+
1107
+ stmt.run(sourceId);
1108
+ }
1109
+
1110
+ // ============================================================================
1111
+ // CLAUDE.md Update History CRUD Operations
1112
+ // ============================================================================
1113
+
1114
+ /**
1115
+ * Insert a CLAUDE.md update record
1116
+ */
1117
+ insertClaudeUpdateRecord(record: Omit<ClaudeUpdateRecord, 'id'>): ClaudeUpdateRecord {
1118
+ const stmt = this.db.prepare(`
1119
+ INSERT INTO claude_update_history
1120
+ (file_path, file_level, module_path, updated_at, update_source, git_commit_hash, files_changed_before_update, metadata)
1121
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
1122
+ `);
1123
+
1124
+ const result = stmt.run(
1125
+ record.file_path,
1126
+ record.file_level,
1127
+ record.module_path || null,
1128
+ record.updated_at,
1129
+ record.update_source,
1130
+ record.git_commit_hash || null,
1131
+ record.files_changed_before_update,
1132
+ record.metadata || null
1133
+ );
1134
+
1135
+ return {
1136
+ id: result.lastInsertRowid as number,
1137
+ ...record
1138
+ };
1139
+ }
1140
+
1141
+ /**
1142
+ * Get the last update record for a file
1143
+ */
1144
+ getLastClaudeUpdate(filePath: string): ClaudeUpdateRecord | null {
1145
+ const stmt = this.db.prepare(`
1146
+ SELECT * FROM claude_update_history
1147
+ WHERE file_path = ?
1148
+ ORDER BY updated_at DESC
1149
+ LIMIT 1
1150
+ `);
1151
+
1152
+ const row = stmt.get(filePath) as any;
1153
+ if (!row) return null;
1154
+
1155
+ return {
1156
+ id: row.id,
1157
+ file_path: row.file_path,
1158
+ file_level: row.file_level,
1159
+ module_path: row.module_path,
1160
+ updated_at: row.updated_at,
1161
+ update_source: row.update_source,
1162
+ git_commit_hash: row.git_commit_hash,
1163
+ files_changed_before_update: row.files_changed_before_update,
1164
+ metadata: row.metadata
1165
+ };
1166
+ }
1167
+
1168
+ /**
1169
+ * Get update history for a file
1170
+ */
1171
+ getClaudeUpdateHistory(filePath: string, limit: number = 50): ClaudeUpdateRecord[] {
1172
+ const stmt = this.db.prepare(`
1173
+ SELECT * FROM claude_update_history
1174
+ WHERE file_path = ?
1175
+ ORDER BY updated_at DESC
1176
+ LIMIT ?
1177
+ `);
1178
+
1179
+ const rows = stmt.all(filePath, limit) as any[];
1180
+ return rows.map(row => ({
1181
+ id: row.id,
1182
+ file_path: row.file_path,
1183
+ file_level: row.file_level,
1184
+ module_path: row.module_path,
1185
+ updated_at: row.updated_at,
1186
+ update_source: row.update_source,
1187
+ git_commit_hash: row.git_commit_hash,
1188
+ files_changed_before_update: row.files_changed_before_update,
1189
+ metadata: row.metadata
1190
+ }));
1191
+ }
1192
+
1193
+ /**
1194
+ * Get all CLAUDE.md update records for freshness calculation
1195
+ */
1196
+ getAllClaudeUpdateRecords(): ClaudeUpdateRecord[] {
1197
+ const stmt = this.db.prepare(`
1198
+ SELECT * FROM claude_update_history
1199
+ WHERE id IN (
1200
+ SELECT MAX(id) FROM claude_update_history
1201
+ GROUP BY file_path
1202
+ )
1203
+ ORDER BY updated_at DESC
1204
+ `);
1205
+
1206
+ const rows = stmt.all() as any[];
1207
+ return rows.map(row => ({
1208
+ id: row.id,
1209
+ file_path: row.file_path,
1210
+ file_level: row.file_level,
1211
+ module_path: row.module_path,
1212
+ updated_at: row.updated_at,
1213
+ update_source: row.update_source,
1214
+ git_commit_hash: row.git_commit_hash,
1215
+ files_changed_before_update: row.files_changed_before_update,
1216
+ metadata: row.metadata
1217
+ }));
1218
+ }
1219
+
1220
+ /**
1221
+ * Delete update records for a file
1222
+ */
1223
+ deleteClaudeUpdateRecords(filePath: string): number {
1224
+ const stmt = this.db.prepare(`
1225
+ DELETE FROM claude_update_history
1226
+ WHERE file_path = ?
1227
+ `);
1228
+ const result = stmt.run(filePath);
1229
+ return result.changes;
1230
+ }
1231
+
1232
+ /**
1233
+ * Close database connection
1234
+ */
1235
+ close(): void {
1236
+ this.db.close();
1237
+ }
1238
+ }
1239
+
1240
+ // Singleton instance cache
1241
+ const storeCache = new Map<string, CoreMemoryStore>();
1242
+
1243
+ /**
1244
+ * Get or create a store instance for a project
1245
+ */
1246
+ export function getCoreMemoryStore(projectPath: string): CoreMemoryStore {
1247
+ const normalizedPath = projectPath.toLowerCase().replace(/\\/g, '/');
1248
+
1249
+ if (!storeCache.has(normalizedPath)) {
1250
+ storeCache.set(normalizedPath, new CoreMemoryStore(projectPath));
1251
+ }
1252
+ return storeCache.get(normalizedPath)!;
1253
+ }
1254
+
1255
+ // ============================================================================
1256
+ // Cross-workspace management functions
1257
+ // ============================================================================
1258
+
1259
+ import { readdirSync, writeFileSync, readFileSync } from 'fs';
1260
+ import { homedir } from 'os';
1261
+
1262
+ export interface ProjectInfo {
1263
+ id: string;
1264
+ path: string;
1265
+ memoriesCount: number;
1266
+ clustersCount: number;
1267
+ lastUpdated?: string;
1268
+ }
1269
+
1270
+ export interface ExportedMemory {
1271
+ version: string;
1272
+ exportedAt: string;
1273
+ sourceProject: string;
1274
+ memories: CoreMemory[];
1275
+ }
1276
+
1277
+ /**
1278
+ * Get CCW home directory
1279
+ */
1280
+ function getCCWHome(): string {
1281
+ return process.env.CCW_DATA_DIR || join(homedir(), '.ccw');
1282
+ }
1283
+
1284
+ /**
1285
+ * List all projects with their memory counts
1286
+ */
1287
+ export function listAllProjects(): ProjectInfo[] {
1288
+ const projectsDir = join(getCCWHome(), 'projects');
1289
+
1290
+ if (!existsSync(projectsDir)) {
1291
+ return [];
1292
+ }
1293
+
1294
+ const projects: ProjectInfo[] = [];
1295
+ const entries = readdirSync(projectsDir, { withFileTypes: true });
1296
+
1297
+ for (const entry of entries) {
1298
+ if (!entry.isDirectory()) continue;
1299
+
1300
+ const projectId = entry.name;
1301
+ const coreMemoryDb = join(projectsDir, projectId, 'core-memory', 'core_memory.db');
1302
+
1303
+ let memoriesCount = 0;
1304
+ let clustersCount = 0;
1305
+ let lastUpdated: string | undefined;
1306
+
1307
+ if (existsSync(coreMemoryDb)) {
1308
+ try {
1309
+ const db = new Database(coreMemoryDb, { readonly: true });
1310
+
1311
+ // Count memories
1312
+ const memResult = db.prepare('SELECT COUNT(*) as count FROM memories').get() as { count: number };
1313
+ memoriesCount = memResult?.count || 0;
1314
+
1315
+ // Count clusters
1316
+ try {
1317
+ const clusterResult = db.prepare('SELECT COUNT(*) as count FROM session_clusters').get() as { count: number };
1318
+ clustersCount = clusterResult?.count || 0;
1319
+ } catch {
1320
+ // Table might not exist
1321
+ }
1322
+
1323
+ // Get last update time
1324
+ const lastMemory = db.prepare('SELECT MAX(updated_at) as last FROM memories').get() as { last: string };
1325
+ lastUpdated = lastMemory?.last;
1326
+
1327
+ db.close();
1328
+ } catch {
1329
+ // Database might be locked or corrupted
1330
+ }
1331
+ }
1332
+
1333
+ // Convert project ID back to approximate path
1334
+ const approximatePath = projectId
1335
+ .replace(/^([a-z])--/, '$1:/') // d-- -> d:/
1336
+ .replace(/--/g, '/')
1337
+ .replace(/-/g, ' ');
1338
+
1339
+ projects.push({
1340
+ id: projectId,
1341
+ path: approximatePath,
1342
+ memoriesCount,
1343
+ clustersCount,
1344
+ lastUpdated
1345
+ });
1346
+ }
1347
+
1348
+ // Sort by last updated (most recent first)
1349
+ return projects.sort((a, b) => {
1350
+ if (!a.lastUpdated) return 1;
1351
+ if (!b.lastUpdated) return -1;
1352
+ return b.lastUpdated.localeCompare(a.lastUpdated);
1353
+ });
1354
+ }
1355
+
1356
+ /**
1357
+ * Get memories from another project by ID
1358
+ */
1359
+ export function getMemoriesFromProject(projectId: string): CoreMemory[] {
1360
+ const projectsDir = join(getCCWHome(), 'projects');
1361
+ const coreMemoryDb = join(projectsDir, projectId, 'core-memory', 'core_memory.db');
1362
+
1363
+ if (!existsSync(coreMemoryDb)) {
1364
+ throw new Error(`Project not found: ${projectId}`);
1365
+ }
1366
+
1367
+ const db = new Database(coreMemoryDb, { readonly: true });
1368
+
1369
+ const stmt = db.prepare('SELECT * FROM memories ORDER BY updated_at DESC');
1370
+ const rows = stmt.all() as any[];
1371
+
1372
+ db.close();
1373
+
1374
+ return rows.map(row => ({
1375
+ id: row.id,
1376
+ content: row.content,
1377
+ summary: row.summary || '',
1378
+ raw_output: row.raw_output,
1379
+ created_at: row.created_at,
1380
+ updated_at: row.updated_at,
1381
+ archived: Boolean(row.archived),
1382
+ metadata: row.metadata
1383
+ }));
1384
+ }
1385
+
1386
+ /**
1387
+ * Find a memory by ID across all projects
1388
+ * Searches through all project databases to locate a specific memory
1389
+ */
1390
+ export function findMemoryAcrossProjects(memoryId: string): { memory: CoreMemory; projectId: string } | null {
1391
+ const projectsDir = join(getCCWHome(), 'projects');
1392
+
1393
+ if (!existsSync(projectsDir)) {
1394
+ return null;
1395
+ }
1396
+
1397
+ const entries = readdirSync(projectsDir, { withFileTypes: true });
1398
+
1399
+ for (const entry of entries) {
1400
+ if (!entry.isDirectory()) continue;
1401
+
1402
+ const projectId = entry.name;
1403
+ const coreMemoryDb = join(projectsDir, projectId, 'core-memory', 'core_memory.db');
1404
+
1405
+ if (!existsSync(coreMemoryDb)) continue;
1406
+
1407
+ try {
1408
+ const db = new Database(coreMemoryDb, { readonly: true });
1409
+ const row = db.prepare('SELECT * FROM memories WHERE id = ?').get(memoryId) as any;
1410
+ db.close();
1411
+
1412
+ if (row) {
1413
+ return {
1414
+ memory: {
1415
+ id: row.id,
1416
+ content: row.content,
1417
+ summary: row.summary || '',
1418
+ raw_output: row.raw_output,
1419
+ created_at: row.created_at,
1420
+ updated_at: row.updated_at,
1421
+ archived: Boolean(row.archived),
1422
+ metadata: row.metadata
1423
+ },
1424
+ projectId
1425
+ };
1426
+ }
1427
+ } catch {
1428
+ // Database might be locked or corrupted, skip
1429
+ }
1430
+ }
1431
+
1432
+ return null;
1433
+ }
1434
+
1435
+ /**
1436
+ * Export memories to a JSON file
1437
+ */
1438
+ export function exportMemories(
1439
+ projectPath: string,
1440
+ outputPath: string,
1441
+ options?: { ids?: string[]; includeArchived?: boolean }
1442
+ ): number {
1443
+ const store = getCoreMemoryStore(projectPath);
1444
+ let memories = store.getMemories({ archived: options?.includeArchived || false, limit: 10000 });
1445
+
1446
+ // Filter by IDs if specified
1447
+ if (options?.ids && options.ids.length > 0) {
1448
+ const idSet = new Set(options.ids);
1449
+ memories = memories.filter(m => idSet.has(m.id));
1450
+ }
1451
+
1452
+ const exportData: ExportedMemory = {
1453
+ version: '1.0',
1454
+ exportedAt: new Date().toISOString(),
1455
+ sourceProject: projectPath,
1456
+ memories
1457
+ };
1458
+
1459
+ writeFileSync(outputPath, JSON.stringify(exportData, null, 2), 'utf-8');
1460
+ return memories.length;
1461
+ }
1462
+
1463
+ /**
1464
+ * Import memories from a JSON file or another project
1465
+ */
1466
+ export function importMemories(
1467
+ targetProjectPath: string,
1468
+ source: string, // File path or project ID
1469
+ options?: { overwrite?: boolean; prefix?: string }
1470
+ ): { imported: number; skipped: number } {
1471
+ const store = getCoreMemoryStore(targetProjectPath);
1472
+ let memories: CoreMemory[];
1473
+
1474
+ // Check if source is a file or project ID
1475
+ if (existsSync(source) && source.endsWith('.json')) {
1476
+ // Import from file
1477
+ const content = readFileSync(source, 'utf-8');
1478
+ const data = JSON.parse(content) as ExportedMemory;
1479
+ memories = data.memories;
1480
+ } else {
1481
+ // Import from project ID
1482
+ memories = getMemoriesFromProject(source);
1483
+ }
1484
+
1485
+ let imported = 0;
1486
+ let skipped = 0;
1487
+
1488
+ for (const memory of memories) {
1489
+ // Generate new ID with optional prefix
1490
+ let newId = memory.id;
1491
+ if (options?.prefix) {
1492
+ newId = `${options.prefix}-${memory.id}`;
1493
+ }
1494
+
1495
+ // Check if already exists
1496
+ const existing = store.getMemory(newId);
1497
+ if (existing && !options?.overwrite) {
1498
+ skipped++;
1499
+ continue;
1500
+ }
1501
+
1502
+ // Import memory
1503
+ store.upsertMemory({
1504
+ id: newId,
1505
+ content: memory.content,
1506
+ summary: memory.summary,
1507
+ raw_output: memory.raw_output,
1508
+ metadata: memory.metadata
1509
+ });
1510
+
1511
+ imported++;
1512
+ }
1513
+
1514
+ return { imported, skipped };
1515
+ }
1516
+
1517
+ /**
1518
+ * Close all store instances
1519
+ */
1520
+ export function closeAllStores(): void {
1521
+ const stores = Array.from(storeCache.values());
1522
+ for (const store of stores) {
1523
+ store.close();
1524
+ }
1525
+ storeCache.clear();
1526
+ }
1527
+
1528
+ export default CoreMemoryStore;