luca 3.0.0 → 3.0.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 (372) hide show
  1. package/.github/workflows/release.yaml +1 -0
  2. package/CLAUDE.md +10 -2
  3. package/README.md +130 -112
  4. package/assistants/codingAssistant/CORE.md +6 -1
  5. package/assistants/codingAssistant/hooks.ts +1 -1
  6. package/assistants/inkbot/hooks.ts +1 -1
  7. package/assistants/inkbot/tools.ts +1 -1
  8. package/bun.lock +220 -322
  9. package/commands/audit-docs.ts +2 -2
  10. package/commands/build-bootstrap.ts +2 -3
  11. package/commands/build-python-bridge.ts +2 -3
  12. package/commands/build-scaffolds.ts +2 -3
  13. package/commands/bundle-consumer-project.ts +521 -0
  14. package/commands/generate-api-docs.ts +2 -2
  15. package/commands/inkbot.ts +2 -2
  16. package/commands/release.ts +2 -2
  17. package/commands/try-all-challenges.ts +3 -3
  18. package/commands/try-challenge.ts +3 -3
  19. package/dist/agi/container.server.d.ts +2 -2
  20. package/dist/agi/features/assistant.d.ts +2 -2
  21. package/dist/agi/features/assistants-manager.d.ts +1 -1
  22. package/dist/agi/features/autonomous-assistant.d.ts +1 -1
  23. package/dist/agi/features/browser-use.d.ts +1 -1
  24. package/dist/agi/features/claude-code.d.ts +1 -1
  25. package/dist/agi/features/conversation-history.d.ts +2 -2
  26. package/dist/agi/features/conversation.d.ts +1 -1
  27. package/dist/agi/features/docs-reader.d.ts +1 -1
  28. package/dist/agi/features/file-tools.d.ts +1 -1
  29. package/dist/agi/features/luca-coder.d.ts +1 -1
  30. package/dist/agi/features/openai-codex.d.ts +1 -1
  31. package/dist/agi/features/skills-library.d.ts +1 -1
  32. package/dist/clients/civitai/index.d.ts +4 -4
  33. package/dist/clients/client-template.d.ts +4 -4
  34. package/dist/clients/comfyui/index.d.ts +2 -2
  35. package/dist/clients/elevenlabs/index.d.ts +2 -2
  36. package/dist/clients/openai/index.d.ts +2 -2
  37. package/dist/clients/supabase/index.d.ts +3 -3
  38. package/dist/command.d.ts +1 -1
  39. package/dist/node/container.d.ts +1 -1
  40. package/dist/node/features/helpers.d.ts +3 -3
  41. package/dist/node/features/semantic-search.d.ts +1 -1
  42. package/dist/node/features/vm.d.ts +3 -3
  43. package/dist/node.d.ts +1 -1
  44. package/dist/scaffolds/generated.d.ts +1 -1
  45. package/dist/selector.d.ts +1 -1
  46. package/index.html +217 -190
  47. package/luca.console.ts +1 -1
  48. package/package.json +2 -2
  49. package/public/index.html +217 -190
  50. package/public/slides-ai-native.html +1 -1
  51. package/public/slides-intro.html +2 -2
  52. package/scripts/examples/ask-luca-expert.ts +1 -1
  53. package/scripts/examples/assistant-questions.ts +1 -1
  54. package/scripts/examples/excalidraw-expert.ts +1 -1
  55. package/scripts/examples/file-manager.ts +1 -1
  56. package/scripts/examples/ideas.ts +1 -1
  57. package/scripts/examples/interactive-chat.ts +1 -1
  58. package/scripts/examples/opening-a-web-browser.ts +1 -1
  59. package/scripts/examples/telegram-bot.ts +1 -1
  60. package/scripts/examples/using-assistant-with-mcp.ts +1 -1
  61. package/scripts/examples/using-claude-code.ts +1 -1
  62. package/scripts/examples/using-contentdb.ts +2 -2
  63. package/scripts/examples/using-conversations.ts +1 -1
  64. package/scripts/examples/using-disk-cache.ts +1 -1
  65. package/scripts/examples/using-docker-shell.ts +1 -1
  66. package/scripts/examples/using-elevenlabs.ts +1 -1
  67. package/scripts/examples/using-google-calendar.ts +1 -1
  68. package/scripts/examples/using-google-docs.ts +1 -1
  69. package/scripts/examples/using-google-drive.ts +1 -1
  70. package/scripts/examples/using-google-sheets.ts +1 -1
  71. package/scripts/examples/using-nlp.ts +1 -1
  72. package/scripts/examples/using-ollama.ts +1 -1
  73. package/scripts/examples/using-postgres.ts +1 -1
  74. package/scripts/examples/using-runpod.ts +1 -1
  75. package/scripts/examples/using-tts.ts +1 -1
  76. package/scripts/scaffold.ts +5 -5
  77. package/scripts/scratch.ts +1 -1
  78. package/scripts/test-assistant-hooks.ts +1 -1
  79. package/scripts/test-docs-reader.ts +1 -1
  80. package/src/agi/container.server.ts +6 -2
  81. package/src/agi/features/agent-memory.ts +25 -25
  82. package/src/agi/features/assistant.ts +34 -5
  83. package/src/agi/features/assistants-manager.ts +122 -6
  84. package/src/agi/features/autonomous-assistant.ts +1 -1
  85. package/src/agi/features/browser-use.ts +20 -1
  86. package/src/agi/features/claude-code.ts +51 -5
  87. package/src/agi/features/coding-tools.ts +1 -1
  88. package/src/agi/features/conversation-history.ts +181 -4
  89. package/src/agi/features/conversation.ts +186 -15
  90. package/src/agi/features/docs-reader.ts +2 -2
  91. package/src/agi/features/file-tools.ts +49 -2
  92. package/src/agi/features/luca-coder.ts +7 -5
  93. package/src/agi/features/mcp-bridge.ts +532 -0
  94. package/src/agi/features/openai-codex.ts +2 -2
  95. package/src/agi/features/skills-library.ts +131 -52
  96. package/src/agi/lib/token-counter.ts +80 -0
  97. package/src/bootstrap/generated.ts +56 -57
  98. package/src/browser.ts +1 -1
  99. package/src/cli/build-info.ts +2 -2
  100. package/src/cli/cli.ts +2 -2
  101. package/src/clients/civitai/index.ts +5 -5
  102. package/src/clients/client-template.ts +4 -4
  103. package/src/clients/comfyui/index.ts +4 -4
  104. package/src/clients/elevenlabs/index.ts +4 -4
  105. package/src/clients/openai/index.ts +7 -7
  106. package/src/clients/supabase/index.ts +4 -4
  107. package/src/clients/voicebox/index.ts +4 -4
  108. package/src/command.ts +2 -1
  109. package/src/commands/chat.ts +1 -0
  110. package/src/commands/eval.ts +2 -56
  111. package/src/commands/introspect.ts +1 -1
  112. package/src/commands/prompt.ts +41 -9
  113. package/src/container-describer.ts +8 -1
  114. package/src/container.ts +13 -0
  115. package/src/entity.ts +2 -2
  116. package/src/helper.ts +1 -1
  117. package/src/introspection/generated.agi.ts +28563 -27571
  118. package/src/introspection/generated.node.ts +20281 -20194
  119. package/src/introspection/generated.web.ts +605 -584
  120. package/src/introspection/scan.ts +11 -6
  121. package/src/node/container.ts +1 -1
  122. package/src/node/features/content-db.ts +39 -2
  123. package/src/node/features/display-result.ts +57 -0
  124. package/src/node/features/helpers.ts +42 -15
  125. package/src/node/features/python.ts +25 -19
  126. package/src/node/features/repl.ts +1 -1
  127. package/src/node/features/secure-shell.ts +11 -17
  128. package/src/node/features/semantic-search.ts +2 -2
  129. package/src/node/features/transpiler.ts +2 -3
  130. package/src/node/features/ui.ts +5 -0
  131. package/src/node/features/vm.ts +3 -3
  132. package/src/node.ts +3 -3
  133. package/src/python/generated.ts +0 -1
  134. package/src/scaffolds/generated.ts +82 -83
  135. package/src/selector.ts +1 -1
  136. package/src/servers/express.ts +1 -1
  137. package/src/web/features/helpers.ts +22 -0
  138. package/tsconfig.json +12 -12
  139. package/docs/CLI.md +0 -335
  140. package/docs/CNAME +0 -1
  141. package/docs/README.md +0 -60
  142. package/docs/TABLE-OF-CONTENTS.md +0 -183
  143. package/docs/apis/clients/elevenlabs.md +0 -308
  144. package/docs/apis/clients/graph.md +0 -107
  145. package/docs/apis/clients/openai.md +0 -429
  146. package/docs/apis/clients/rest.md +0 -161
  147. package/docs/apis/clients/websocket.md +0 -174
  148. package/docs/apis/features/agi/assistant.md +0 -625
  149. package/docs/apis/features/agi/assistants-manager.md +0 -282
  150. package/docs/apis/features/agi/auto-assistant.md +0 -279
  151. package/docs/apis/features/agi/browser-use.md +0 -802
  152. package/docs/apis/features/agi/claude-code.md +0 -884
  153. package/docs/apis/features/agi/conversation-history.md +0 -364
  154. package/docs/apis/features/agi/conversation.md +0 -548
  155. package/docs/apis/features/agi/docs-reader.md +0 -99
  156. package/docs/apis/features/agi/file-tools.md +0 -163
  157. package/docs/apis/features/agi/luca-coder.md +0 -407
  158. package/docs/apis/features/agi/openai-codex.md +0 -396
  159. package/docs/apis/features/agi/openapi.md +0 -138
  160. package/docs/apis/features/agi/semantic-search.md +0 -387
  161. package/docs/apis/features/agi/skills-library.md +0 -239
  162. package/docs/apis/features/node/container-link.md +0 -192
  163. package/docs/apis/features/node/content-db.md +0 -450
  164. package/docs/apis/features/node/disk-cache.md +0 -379
  165. package/docs/apis/features/node/dns.md +0 -652
  166. package/docs/apis/features/node/docker.md +0 -706
  167. package/docs/apis/features/node/downloader.md +0 -81
  168. package/docs/apis/features/node/esbuild.md +0 -60
  169. package/docs/apis/features/node/file-manager.md +0 -191
  170. package/docs/apis/features/node/fs.md +0 -1217
  171. package/docs/apis/features/node/git.md +0 -371
  172. package/docs/apis/features/node/google-auth.md +0 -193
  173. package/docs/apis/features/node/google-calendar.md +0 -202
  174. package/docs/apis/features/node/google-docs.md +0 -173
  175. package/docs/apis/features/node/google-drive.md +0 -246
  176. package/docs/apis/features/node/google-mail.md +0 -214
  177. package/docs/apis/features/node/google-sheets.md +0 -194
  178. package/docs/apis/features/node/grep.md +0 -292
  179. package/docs/apis/features/node/helpers.md +0 -164
  180. package/docs/apis/features/node/ink.md +0 -334
  181. package/docs/apis/features/node/ipc-socket.md +0 -249
  182. package/docs/apis/features/node/json-tree.md +0 -86
  183. package/docs/apis/features/node/networking.md +0 -316
  184. package/docs/apis/features/node/nlp.md +0 -133
  185. package/docs/apis/features/node/opener.md +0 -97
  186. package/docs/apis/features/node/os.md +0 -146
  187. package/docs/apis/features/node/package-finder.md +0 -392
  188. package/docs/apis/features/node/postgres.md +0 -234
  189. package/docs/apis/features/node/proc.md +0 -399
  190. package/docs/apis/features/node/process-manager.md +0 -305
  191. package/docs/apis/features/node/python.md +0 -604
  192. package/docs/apis/features/node/redis.md +0 -380
  193. package/docs/apis/features/node/repl.md +0 -88
  194. package/docs/apis/features/node/runpod.md +0 -674
  195. package/docs/apis/features/node/secure-shell.md +0 -176
  196. package/docs/apis/features/node/semantic-search.md +0 -408
  197. package/docs/apis/features/node/sqlite.md +0 -233
  198. package/docs/apis/features/node/telegram.md +0 -279
  199. package/docs/apis/features/node/transpiler.md +0 -74
  200. package/docs/apis/features/node/tts.md +0 -133
  201. package/docs/apis/features/node/ui.md +0 -701
  202. package/docs/apis/features/node/vault.md +0 -59
  203. package/docs/apis/features/node/vm.md +0 -75
  204. package/docs/apis/features/node/yaml-tree.md +0 -85
  205. package/docs/apis/features/node/yaml.md +0 -176
  206. package/docs/apis/features/web/asset-loader.md +0 -59
  207. package/docs/apis/features/web/container-link.md +0 -192
  208. package/docs/apis/features/web/esbuild.md +0 -54
  209. package/docs/apis/features/web/helpers.md +0 -164
  210. package/docs/apis/features/web/network.md +0 -44
  211. package/docs/apis/features/web/speech.md +0 -69
  212. package/docs/apis/features/web/vault.md +0 -59
  213. package/docs/apis/features/web/vm.md +0 -75
  214. package/docs/apis/features/web/voice.md +0 -84
  215. package/docs/apis/servers/express.md +0 -171
  216. package/docs/apis/servers/mcp.md +0 -238
  217. package/docs/apis/servers/websocket.md +0 -170
  218. package/docs/bootstrap/CLAUDE.md +0 -101
  219. package/docs/bootstrap/SKILL.md +0 -341
  220. package/docs/bootstrap/templates/about-command.ts +0 -41
  221. package/docs/bootstrap/templates/docs-models.ts +0 -22
  222. package/docs/bootstrap/templates/docs-readme.md +0 -43
  223. package/docs/bootstrap/templates/example-feature.ts +0 -53
  224. package/docs/bootstrap/templates/health-endpoint.ts +0 -15
  225. package/docs/bootstrap/templates/luca-cli.ts +0 -30
  226. package/docs/bootstrap/templates/runme.md +0 -54
  227. package/docs/challenges/caching-proxy.md +0 -16
  228. package/docs/challenges/content-db-round-trip.md +0 -14
  229. package/docs/challenges/custom-command.md +0 -9
  230. package/docs/challenges/file-watcher-pipeline.md +0 -11
  231. package/docs/challenges/grep-audit-report.md +0 -15
  232. package/docs/challenges/multi-feature-dashboard.md +0 -14
  233. package/docs/challenges/process-orchestrator.md +0 -17
  234. package/docs/challenges/rest-api-server-with-client.md +0 -12
  235. package/docs/challenges/script-runner-with-vm.md +0 -11
  236. package/docs/challenges/simple-rest-api.md +0 -15
  237. package/docs/challenges/websocket-serve-and-client.md +0 -11
  238. package/docs/challenges/yaml-config-system.md +0 -14
  239. package/docs/command-system-overhaul.md +0 -94
  240. package/docs/documentation-audit.md +0 -134
  241. package/docs/examples/assistant/CORE.md +0 -18
  242. package/docs/examples/assistant/hooks.ts +0 -3
  243. package/docs/examples/assistant/tools.ts +0 -10
  244. package/docs/examples/assistant-hooks-reference.ts +0 -171
  245. package/docs/examples/assistant-with-process-manager.md +0 -84
  246. package/docs/examples/content-db.md +0 -77
  247. package/docs/examples/disk-cache.md +0 -83
  248. package/docs/examples/docker.md +0 -101
  249. package/docs/examples/downloader.md +0 -70
  250. package/docs/examples/entity.md +0 -124
  251. package/docs/examples/esbuild.md +0 -80
  252. package/docs/examples/feature-as-tool-provider.md +0 -143
  253. package/docs/examples/file-manager.md +0 -82
  254. package/docs/examples/fs.md +0 -83
  255. package/docs/examples/git.md +0 -85
  256. package/docs/examples/google-auth.md +0 -88
  257. package/docs/examples/google-calendar.md +0 -94
  258. package/docs/examples/google-docs.md +0 -82
  259. package/docs/examples/google-drive.md +0 -96
  260. package/docs/examples/google-sheets.md +0 -95
  261. package/docs/examples/grep.md +0 -85
  262. package/docs/examples/ink-blocks.md +0 -75
  263. package/docs/examples/ink-renderer.md +0 -41
  264. package/docs/examples/ink.md +0 -103
  265. package/docs/examples/ipc-socket.md +0 -103
  266. package/docs/examples/json-tree.md +0 -91
  267. package/docs/examples/networking.md +0 -58
  268. package/docs/examples/nlp.md +0 -91
  269. package/docs/examples/opener.md +0 -78
  270. package/docs/examples/os.md +0 -72
  271. package/docs/examples/package-finder.md +0 -89
  272. package/docs/examples/postgres.md +0 -91
  273. package/docs/examples/proc.md +0 -81
  274. package/docs/examples/process-manager.md +0 -79
  275. package/docs/examples/python.md +0 -132
  276. package/docs/examples/repl.md +0 -93
  277. package/docs/examples/runpod.md +0 -119
  278. package/docs/examples/secure-shell.md +0 -92
  279. package/docs/examples/sqlite.md +0 -86
  280. package/docs/examples/structured-output-with-assistants.md +0 -144
  281. package/docs/examples/telegram.md +0 -77
  282. package/docs/examples/tts.md +0 -86
  283. package/docs/examples/ui.md +0 -80
  284. package/docs/examples/vault.md +0 -70
  285. package/docs/examples/vm.md +0 -86
  286. package/docs/examples/websocket-ask-and-reply-example.md +0 -128
  287. package/docs/examples/yaml-tree.md +0 -93
  288. package/docs/examples/yaml.md +0 -104
  289. package/docs/ideas/assistant-factory-pattern.md +0 -142
  290. package/docs/in-memory-fs.md +0 -4
  291. package/docs/introspection-audit.md +0 -49
  292. package/docs/introspection.md +0 -164
  293. package/docs/mcp/readme.md +0 -162
  294. package/docs/models.ts +0 -41
  295. package/docs/philosophy.md +0 -86
  296. package/docs/principles.md +0 -7
  297. package/docs/prompts/audit-codebase-for-failures-to-use-the-container.md +0 -34
  298. package/docs/prompts/check-for-undocumented-features.md +0 -27
  299. package/docs/prompts/mcp-test-easy-command.md +0 -27
  300. package/docs/scaffolds/client.md +0 -149
  301. package/docs/scaffolds/command.md +0 -120
  302. package/docs/scaffolds/endpoint.md +0 -171
  303. package/docs/scaffolds/feature.md +0 -158
  304. package/docs/scaffolds/selector.md +0 -91
  305. package/docs/scaffolds/server.md +0 -196
  306. package/docs/selectors.md +0 -115
  307. package/docs/sessions/custom-command/attempt-log-2.md +0 -195
  308. package/docs/sessions/file-watcher-pipeline/attempt-log-1.md +0 -728
  309. package/docs/sessions/file-watcher-pipeline/attempt-log-2.md +0 -555
  310. package/docs/sessions/grep-audit-report/attempt-log-1.md +0 -289
  311. package/docs/sessions/multi-feature-dashboard/attempt-log-2.md +0 -679
  312. package/docs/sessions/rest-api-server-with-client/attempt-log-1.md +0 -1
  313. package/docs/sessions/rest-api-server-with-client/attempt-log-3.md +0 -920
  314. package/docs/sessions/simple-rest-api/attempt-log-1.md +0 -593
  315. package/docs/sessions/websocket-serve-and-client/attempt-log-2.md +0 -995
  316. package/docs/tutorials/00-bootstrap.md +0 -166
  317. package/docs/tutorials/01-getting-started.md +0 -106
  318. package/docs/tutorials/02-container.md +0 -210
  319. package/docs/tutorials/03-scripts.md +0 -194
  320. package/docs/tutorials/04-features-overview.md +0 -196
  321. package/docs/tutorials/05-state-and-events.md +0 -171
  322. package/docs/tutorials/06-servers.md +0 -157
  323. package/docs/tutorials/07-endpoints.md +0 -198
  324. package/docs/tutorials/08-commands.md +0 -252
  325. package/docs/tutorials/09-clients.md +0 -162
  326. package/docs/tutorials/10-creating-features.md +0 -203
  327. package/docs/tutorials/11-contentbase.md +0 -191
  328. package/docs/tutorials/12-assistants.md +0 -215
  329. package/docs/tutorials/13-introspection.md +0 -157
  330. package/docs/tutorials/14-type-system.md +0 -174
  331. package/docs/tutorials/15-project-patterns.md +0 -222
  332. package/docs/tutorials/16-google-features.md +0 -534
  333. package/docs/tutorials/17-tui-blocks.md +0 -530
  334. package/docs/tutorials/18-semantic-search.md +0 -334
  335. package/docs/tutorials/19-python-sessions.md +0 -401
  336. package/docs/tutorials/20-browser-esm.md +0 -234
  337. package/src/agi/endpoints/ask.ts +0 -60
  338. package/src/agi/endpoints/conversations/[id].ts +0 -45
  339. package/src/agi/endpoints/conversations.ts +0 -31
  340. package/src/agi/endpoints/experts.ts +0 -37
  341. package/test/assistant-hooks.test.ts +0 -306
  342. package/test/assistant.test.ts +0 -81
  343. package/test/bus.test.ts +0 -134
  344. package/test/clients-servers.test.ts +0 -217
  345. package/test/command.test.ts +0 -267
  346. package/test/container-link.test.ts +0 -274
  347. package/test/conversation.test.ts +0 -220
  348. package/test/features.test.ts +0 -160
  349. package/test/fork-and-research.test.ts +0 -450
  350. package/test/integration.test.ts +0 -787
  351. package/test/interceptor-chain.test.ts +0 -61
  352. package/test/node-container.test.ts +0 -121
  353. package/test/python-session.test.ts +0 -105
  354. package/test/rate-limit.test.ts +0 -272
  355. package/test/semantic-search.test.ts +0 -550
  356. package/test/state.test.ts +0 -121
  357. package/test/vm-context.test.ts +0 -146
  358. package/test/vm-loadmodule.test.ts +0 -213
  359. package/test/websocket-ask.test.ts +0 -101
  360. package/test-integration/assistant.test.ts +0 -138
  361. package/test-integration/assistants-manager.test.ts +0 -113
  362. package/test-integration/claude-code.test.ts +0 -98
  363. package/test-integration/conversation-history.test.ts +0 -205
  364. package/test-integration/conversation.test.ts +0 -137
  365. package/test-integration/elevenlabs.test.ts +0 -55
  366. package/test-integration/google-services.test.ts +0 -80
  367. package/test-integration/helpers.ts +0 -89
  368. package/test-integration/memory.test.ts +0 -204
  369. package/test-integration/openai-codex.test.ts +0 -93
  370. package/test-integration/runpod.test.ts +0 -58
  371. package/test-integration/server-endpoints.test.ts +0 -97
  372. package/test-integration/telegram.test.ts +0 -46
@@ -1,550 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach } from 'bun:test'
2
- import { SemanticSearch } from '../src/node/features/semantic-search'
3
- import type { DocumentInput, Chunk } from '../src/node/features/semantic-search'
4
- import { mkdtempSync, rmSync, existsSync } from 'node:fs'
5
- import { join } from 'node:path'
6
- import { tmpdir } from 'node:os'
7
- import { AGIContainer } from '../src/agi/container.server'
8
-
9
- function makeTmpDir(): string {
10
- return mkdtempSync(join(tmpdir(), 'semantic-search-test-'))
11
- }
12
-
13
- function makeContainer(): AGIContainer {
14
- return new AGIContainer().use(SemanticSearch) as AGIContainer
15
- }
16
-
17
- function makeTestDoc(overrides: Partial<DocumentInput> = {}): DocumentInput {
18
- return {
19
- pathId: 'test/doc-1',
20
- model: 'Plan',
21
- title: 'Test Document',
22
- slug: 'test-doc-1',
23
- meta: { status: 'approved', priority: 'high' },
24
- content: 'This is a test document about authentication and login flows.',
25
- sections: [
26
- { heading: 'Overview', headingPath: 'Overview', content: 'This section covers authentication basics.', level: 2 },
27
- { heading: 'Implementation', headingPath: 'Overview > Implementation', content: 'We implement login using OAuth2 tokens and session management.', level: 3 },
28
- ],
29
- ...overrides,
30
- }
31
- }
32
-
33
- function fakeEmbedding(dims: number, seed = 0.5): Float32Array {
34
- const vec = new Float32Array(dims)
35
- for (let i = 0; i < dims; i++) {
36
- vec[i] = Math.sin(seed * (i + 1)) * 0.5
37
- }
38
- return vec
39
- }
40
-
41
- describe('SemanticSearch', () => {
42
- let tmpDir: string
43
- let container: AGIContainer
44
- let search: SemanticSearch
45
-
46
- beforeEach(() => {
47
- tmpDir = makeTmpDir()
48
- container = makeContainer()
49
- search = container.feature('semanticSearch', {
50
- dbPath: join(tmpDir, 'search.sqlite'),
51
- embeddingProvider: 'local',
52
- embeddingModel: 'embedding-gemma-300M-Q8_0',
53
- chunkStrategy: 'section',
54
- }) as unknown as SemanticSearch
55
- })
56
-
57
- afterEach(async () => {
58
- try { await search.close() } catch {}
59
- try { rmSync(tmpDir, { recursive: true, force: true }) } catch {}
60
- })
61
-
62
- describe('Feature scaffolding', () => {
63
- it('registers in the container', () => {
64
- expect(container.features.available).toContain('semanticSearch')
65
- })
66
-
67
- it('has correct shortcut', () => {
68
- expect(SemanticSearch.shortcut).toBe('features.semanticSearch')
69
- })
70
-
71
- it('initializes with default state', () => {
72
- expect(search.state.get('indexed')).toBe(0)
73
- expect(search.state.get('embedded')).toBe(0)
74
- expect(search.state.get('lastIndexedAt')).toBeNull()
75
- expect(search.state.get('dbReady')).toBe(false)
76
- })
77
- })
78
-
79
- describe('Database layer (2.2)', () => {
80
- it('initializes database and creates tables', async () => {
81
- await search.initDb()
82
- expect(search.state.get('dbReady')).toBe(true)
83
-
84
- const tables = search.db.query(
85
- "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
86
- ).all() as any[]
87
- const tableNames = tables.map((t: any) => t.name)
88
-
89
- expect(tableNames).toContain('search_meta')
90
- expect(tableNames).toContain('documents')
91
- expect(tableNames).toContain('chunks')
92
- })
93
-
94
- it('writes search_meta with provider, model, dims on init', async () => {
95
- await search.initDb()
96
-
97
- const getMeta = (key: string) =>
98
- (search.db.query('SELECT value FROM search_meta WHERE key = ?').get(key) as any)?.value
99
-
100
- expect(getMeta('provider')).toBe('local')
101
- expect(getMeta('model')).toBe('embedding-gemma-300M-Q8_0')
102
- expect(getMeta('dims')).toBe('768')
103
- expect(getMeta('createdAt')).toBeTruthy()
104
- })
105
-
106
- it('refuses to load with mismatched provider/model/dims', async () => {
107
- await search.initDb()
108
- // Get the actual db file path that was created
109
- const dbFile = join(tmpDir, 'search.local-embedding-gemma-300M-Q8_0.sqlite')
110
- expect(existsSync(dbFile)).toBe(true)
111
- await search.close()
112
-
113
- // Create a new instance that resolves to the SAME db file but expects different meta
114
- // We achieve this by directly constructing with options that resolve to the same path
115
- const search2 = container.feature('semanticSearch', {
116
- dbPath: join(tmpDir, 'search.sqlite'),
117
- embeddingProvider: 'local',
118
- embeddingModel: 'embedding-gemma-300M-Q8_0',
119
- }) as unknown as SemanticSearch
120
-
121
- // Manually tamper with the meta to simulate a mismatch
122
- await search2.initDb() // should work since same config
123
-
124
- // Now manually update meta to simulate mismatch
125
- search2.db.run("UPDATE search_meta SET value = 'openai' WHERE key = 'provider'")
126
- await search2.close()
127
-
128
- // Re-open with original config — should fail
129
- const search3 = container.feature('semanticSearch', {
130
- dbPath: join(tmpDir, 'search.sqlite'),
131
- embeddingProvider: 'local',
132
- embeddingModel: 'embedding-gemma-300M-Q8_0',
133
- }) as unknown as SemanticSearch
134
-
135
- try {
136
- await search3.initDb()
137
- expect(true).toBe(false)
138
- } catch (err: any) {
139
- expect(err.message).toContain('mismatch')
140
- }
141
- })
142
-
143
- it('creates FTS5 virtual table', async () => {
144
- await search.initDb()
145
-
146
- // FTS5 tables appear as regular tables in sqlite_master
147
- const vtables = search.db.query(
148
- "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '%fts%'"
149
- ).all() as any[]
150
- const names = vtables.map((t: any) => t.name)
151
- expect(names.some((n: string) => n.includes('documents_fts'))).toBe(true)
152
- })
153
-
154
- it('dimensions are 768 for local gemma model', () => {
155
- expect(search.dimensions).toBe(768)
156
- })
157
-
158
- it('insertDocument stores document with correct metadata', async () => {
159
- await search.initDb()
160
- const doc = makeTestDoc()
161
- search.insertDocument(doc)
162
-
163
- const row = search.db.query('SELECT * FROM documents WHERE path_id = ?').get('test/doc-1') as any
164
- expect(row).toBeTruthy()
165
- expect(row.model).toBe('Plan')
166
- expect(row.title).toBe('Test Document')
167
- expect(row.content_hash).toBeTruthy()
168
- expect(row.indexed_at).toBeTruthy()
169
- expect(JSON.parse(row.meta_json)).toEqual({ status: 'approved', priority: 'high' })
170
- })
171
-
172
- it('insertDocument creates FTS5 entry', async () => {
173
- await search.initDb()
174
- search.insertDocument(makeTestDoc())
175
-
176
- const results = search.db.query(
177
- "SELECT * FROM documents_fts WHERE documents_fts MATCH 'authentication'"
178
- ).all() as any[]
179
- expect(results.length).toBeGreaterThan(0)
180
- })
181
-
182
- it('removeDocument removes document, chunks, and FTS entry', async () => {
183
- await search.initDb()
184
- search.insertDocument(makeTestDoc())
185
-
186
- const chunk: Chunk = {
187
- pathId: 'test/doc-1',
188
- section: 'Overview',
189
- headingPath: 'Overview',
190
- seq: 0,
191
- content: 'test content',
192
- contentHash: 'abc123',
193
- }
194
- search.insertChunk(chunk, fakeEmbedding(768))
195
-
196
- expect((search.db.query('SELECT COUNT(*) as c FROM chunks').get() as any).c).toBe(1)
197
-
198
- search.removeDocument('test/doc-1')
199
-
200
- expect((search.db.query('SELECT COUNT(*) as c FROM documents').get() as any).c).toBe(0)
201
- expect((search.db.query('SELECT COUNT(*) as c FROM chunks').get() as any).c).toBe(0)
202
- expect((search.db.query("SELECT COUNT(*) as c FROM documents_fts WHERE documents_fts MATCH 'authentication'").get() as any).c).toBe(0)
203
- })
204
-
205
- it('getStats returns accurate counts', async () => {
206
- await search.initDb()
207
- search.insertDocument(makeTestDoc())
208
-
209
- const stats = search.getStats()
210
- expect(stats.documentCount).toBe(1)
211
- expect(stats.provider).toBe('local')
212
- expect(stats.model).toBe('embedding-gemma-300M-Q8_0')
213
- expect(stats.dimensions).toBe(768)
214
- })
215
-
216
- it('insertChunk stores chunk with embedding BLOB', async () => {
217
- await search.initDb()
218
- search.insertDocument(makeTestDoc())
219
-
220
- const chunk: Chunk = {
221
- pathId: 'test/doc-1',
222
- section: 'Overview',
223
- headingPath: 'Overview',
224
- seq: 0,
225
- content: 'test chunk content',
226
- contentHash: 'hash123',
227
- }
228
-
229
- const vec = fakeEmbedding(768, 0.7)
230
- search.insertChunk(chunk, vec)
231
-
232
- const chunkRow = search.db.query('SELECT * FROM chunks WHERE path_id = ?').get('test/doc-1') as any
233
- expect(chunkRow).toBeTruthy()
234
- expect(chunkRow.section).toBe('Overview')
235
- expect(chunkRow.heading_path).toBe('Overview')
236
- expect(chunkRow.content).toBe('test chunk content')
237
- expect(chunkRow.embedding).toBeTruthy()
238
-
239
- // Verify embedding roundtrip
240
- const restored = new Float32Array(
241
- new Uint8Array(chunkRow.embedding).buffer,
242
- chunkRow.embedding.byteOffset ?? 0,
243
- 768,
244
- )
245
- expect(Math.abs(restored[0]! - vec[0]!)).toBeLessThan(0.0001)
246
- })
247
- })
248
-
249
- describe('Document chunking (2.4)', () => {
250
- it('section strategy splits at h2/h3 boundaries', () => {
251
- const doc = makeTestDoc()
252
- const chunks = search.chunkDocument(doc, 'section')
253
-
254
- expect(chunks.length).toBe(2)
255
- expect(chunks[0]!.section).toBe('Overview')
256
- expect(chunks[1]!.section).toBe('Implementation')
257
- })
258
-
259
- it('section strategy stores headingPath for each chunk', () => {
260
- const doc = makeTestDoc()
261
- const chunks = search.chunkDocument(doc, 'section')
262
-
263
- expect(chunks[0]!.headingPath).toBe('Overview')
264
- expect(chunks[1]!.headingPath).toBe('Overview > Implementation')
265
- })
266
-
267
- it('fixed strategy respects token limit and overlap', () => {
268
- const longContent = Array(1000).fill('word').join(' ')
269
- const doc = makeTestDoc({ content: longContent, sections: undefined })
270
-
271
- const chunks = search.chunkDocument(doc, 'fixed')
272
- expect(chunks.length).toBeGreaterThan(1)
273
-
274
- for (const chunk of chunks) {
275
- const wordCount = chunk.content.split(/\s+/).length
276
- expect(wordCount).toBeLessThanOrEqual(900 * 0.75 + 1)
277
- }
278
- })
279
-
280
- it('document strategy produces one chunk per doc', () => {
281
- const doc = makeTestDoc()
282
- const chunks = search.chunkDocument(doc, 'document')
283
- expect(chunks.length).toBe(1)
284
- expect(chunks[0]!.content).toBe(doc.content)
285
- })
286
-
287
- it('section strategy falls back to fixed for docs without sections', () => {
288
- const doc = makeTestDoc({ sections: undefined })
289
- const chunks = search.chunkDocument(doc, 'section')
290
- expect(chunks.length).toBeGreaterThan(0)
291
- expect(chunks[0]!.section).toBeUndefined()
292
- })
293
-
294
- it('pathId is used consistently in chunks', () => {
295
- const doc = makeTestDoc({ pathId: 'projects/my-project' })
296
- const chunks = search.chunkDocument(doc)
297
- for (const chunk of chunks) {
298
- expect(chunk.pathId).toBe('projects/my-project')
299
- }
300
- })
301
-
302
- it('chunks have content hashes', () => {
303
- const doc = makeTestDoc()
304
- const chunks = search.chunkDocument(doc)
305
- for (const chunk of chunks) {
306
- expect(chunk.contentHash).toBeTruthy()
307
- expect(chunk.contentHash.length).toBe(16)
308
- }
309
- })
310
- })
311
-
312
- describe('Search engine (2.5)', () => {
313
- it('BM25 search returns results for keyword queries', async () => {
314
- await search.initDb()
315
- search.insertDocument(makeTestDoc())
316
- search.insertDocument(makeTestDoc({
317
- pathId: 'test/doc-2',
318
- title: 'Deployment Guide',
319
- content: 'This document covers deployment and CI/CD pipelines.',
320
- sections: [
321
- { heading: 'Pipelines', headingPath: 'Pipelines', content: 'CI/CD deployment pipelines.', level: 2 },
322
- ],
323
- }))
324
-
325
- const results = await search.search('authentication')
326
- expect(results.length).toBeGreaterThan(0)
327
- expect(results[0]!.pathId).toBe('test/doc-1')
328
- })
329
-
330
- it('BM25 search with model filter', async () => {
331
- await search.initDb()
332
- search.insertDocument(makeTestDoc({ model: 'Plan' }))
333
- search.insertDocument(makeTestDoc({
334
- pathId: 'test/doc-2',
335
- model: 'Task',
336
- title: 'Auth Task',
337
- content: 'Authentication task for the sprint.',
338
- sections: [],
339
- }))
340
-
341
- const results = await search.search('authentication', { model: 'Plan' })
342
- for (const r of results) {
343
- expect(r.model).toBe('Plan')
344
- }
345
- })
346
-
347
- it('BM25 search with where filter applies json_extract', async () => {
348
- await search.initDb()
349
- search.insertDocument(makeTestDoc({ meta: { status: 'approved' } }))
350
- search.insertDocument(makeTestDoc({
351
- pathId: 'test/doc-2',
352
- meta: { status: 'draft' },
353
- content: 'Authentication draft document.',
354
- sections: [],
355
- }))
356
-
357
- const results = await search.search('authentication', { where: { status: 'approved' } })
358
- expect(results.length).toBe(1)
359
- expect(results[0]!.meta.status).toBe('approved')
360
- })
361
-
362
- it('vector search returns results ranked by similarity', async () => {
363
- await search.initDb()
364
- search.insertDocument(makeTestDoc())
365
-
366
- // Insert chunks with known embeddings
367
- const authVec = fakeEmbedding(768, 1.0)
368
- const deployVec = fakeEmbedding(768, 5.0)
369
-
370
- search.insertChunk({
371
- pathId: 'test/doc-1',
372
- section: 'Overview',
373
- headingPath: 'Overview',
374
- seq: 0,
375
- content: 'authentication and login flows',
376
- contentHash: 'hash1',
377
- }, authVec)
378
-
379
- search.insertDocument(makeTestDoc({
380
- pathId: 'test/doc-2',
381
- title: 'Deploy Guide',
382
- content: 'deployment pipelines',
383
- sections: [],
384
- }))
385
-
386
- search.insertChunk({
387
- pathId: 'test/doc-2',
388
- seq: 0,
389
- content: 'deployment and CI/CD',
390
- contentHash: 'hash2',
391
- }, deployVec)
392
-
393
- // Mock embed to return a vector similar to authVec
394
- const origEmbed = search.embed.bind(search)
395
- search.embed = async () => [Array.from(fakeEmbedding(768, 1.1))]
396
-
397
- const results = await search.vectorSearch('auth')
398
- expect(results.length).toBe(2)
399
- // First result should be closer to authVec (seed 1.0 vs 5.0)
400
- expect(results[0]!.pathId).toBe('test/doc-1')
401
- expect(results[0]!.score).toBeGreaterThan(results[1]!.score)
402
-
403
- search.embed = origEmbed
404
- })
405
-
406
- it('hybrid search combines BM25 and vector results via RRF', async () => {
407
- await search.initDb()
408
- search.insertDocument(makeTestDoc())
409
- search.insertChunk({
410
- pathId: 'test/doc-1',
411
- section: 'Overview',
412
- headingPath: 'Overview',
413
- seq: 0,
414
- content: 'authentication and login flows',
415
- contentHash: 'hash1',
416
- }, fakeEmbedding(768, 1.0))
417
-
418
- // Mock embed
419
- search.embed = async () => [Array.from(fakeEmbedding(768, 1.1))]
420
-
421
- const results = await search.hybridSearch('authentication')
422
- expect(results.length).toBeGreaterThan(0)
423
- expect(results[0]!.pathId).toBe('test/doc-1')
424
- // RRF score should be > single source score
425
- expect(results[0]!.score).toBeGreaterThan(0)
426
- })
427
-
428
- it('deepSearch throws v2 message', async () => {
429
- await search.initDb()
430
- try {
431
- await search.deepSearch('test')
432
- expect(true).toBe(false)
433
- } catch (err: any) {
434
- expect(err.message).toContain('v2')
435
- }
436
- })
437
-
438
- it('SearchResult includes citation fields', async () => {
439
- await search.initDb()
440
- search.insertDocument(makeTestDoc())
441
-
442
- const results = await search.search('authentication')
443
- expect(results.length).toBeGreaterThan(0)
444
- const r = results[0]!
445
- expect(r).toHaveProperty('pathId')
446
- expect(r).toHaveProperty('snippet')
447
- expect(r).toHaveProperty('matchedSection')
448
- expect(r).toHaveProperty('headingPath')
449
- })
450
- })
451
-
452
- describe('Index management (2.6)', () => {
453
- it('needsReindex detects changed documents', async () => {
454
- await search.initDb()
455
- const doc = makeTestDoc()
456
- search.insertDocument(doc)
457
-
458
- expect(search.needsReindex(doc)).toBe(false)
459
-
460
- const changed = { ...doc, content: 'completely new content' }
461
- expect(search.needsReindex(changed)).toBe(true)
462
- })
463
-
464
- it('needsReindex returns true for new documents', async () => {
465
- await search.initDb()
466
- expect(search.needsReindex(makeTestDoc())).toBe(true)
467
- })
468
-
469
- it('removeStale deletes documents no longer in collection', async () => {
470
- await search.initDb()
471
- search.insertDocument(makeTestDoc({ pathId: 'keep-this' }))
472
- search.insertDocument(makeTestDoc({ pathId: 'remove-this' }))
473
-
474
- search.removeStale(['keep-this'])
475
-
476
- const count = (search.db.query('SELECT COUNT(*) as c FROM documents').get() as any).c
477
- expect(count).toBe(1)
478
- const remaining = search.db.query('SELECT path_id FROM documents').get() as any
479
- expect(remaining.path_id).toBe('keep-this')
480
- })
481
-
482
- it('status returns accurate health information', async () => {
483
- await search.initDb()
484
- search.insertDocument(makeTestDoc())
485
-
486
- const status = search.status()
487
- expect(status.documentCount).toBe(1)
488
- expect(status.provider).toBe('local')
489
- expect(status.dimensions).toBe(768)
490
- expect(status.dbSizeBytes).toBeGreaterThan(0)
491
- })
492
-
493
- it('reindex clears all data when called without args', async () => {
494
- await search.initDb()
495
- search.insertDocument(makeTestDoc())
496
-
497
- await search.reindex()
498
-
499
- expect((search.db.query('SELECT COUNT(*) as c FROM documents').get() as any).c).toBe(0)
500
- expect((search.db.query('SELECT COUNT(*) as c FROM chunks').get() as any).c).toBe(0)
501
- })
502
- })
503
-
504
- describe('Embedding engine (2.3)', () => {
505
- it('getDimensions returns correct value for local provider', () => {
506
- expect(search.getDimensions()).toBe(768)
507
- })
508
-
509
- it('getDimensions returns 1536 for openai text-embedding-3-small', () => {
510
- const search2 = container.feature('semanticSearch', {
511
- dbPath: join(tmpDir, 'search-openai.sqlite'),
512
- embeddingProvider: 'openai',
513
- embeddingModel: 'text-embedding-3-small',
514
- }) as unknown as SemanticSearch
515
- expect(search2.getDimensions()).toBe(1536)
516
- })
517
-
518
- it('disposeModel can be called safely even without loaded model', async () => {
519
- await search.disposeModel()
520
- // Should not throw
521
- })
522
- })
523
-
524
- describe('Database path scoping', () => {
525
- it('scopes db path by provider and model', async () => {
526
- await search.initDb()
527
- const dbPath = join(tmpDir, 'search.local-embedding-gemma-300M-Q8_0.sqlite')
528
- expect(existsSync(dbPath)).toBe(true)
529
- })
530
- })
531
-
532
- describe('pathId consistency', () => {
533
- it('uses pathId consistently in all operations', async () => {
534
- await search.initDb()
535
- const pathId = 'projects/semantic-search/plan-1'
536
- const doc = makeTestDoc({ pathId })
537
- search.insertDocument(doc)
538
-
539
- const row = search.db.query('SELECT path_id FROM documents').get() as any
540
- expect(row.path_id).toBe(pathId)
541
-
542
- const chunks = search.chunkDocument(doc)
543
- for (const chunk of chunks) {
544
- expect(chunk.pathId).toBe(pathId)
545
- }
546
-
547
- expect(search.needsReindex(doc)).toBe(false)
548
- })
549
- })
550
- })
@@ -1,121 +0,0 @@
1
- import { describe, it, expect, mock } from 'bun:test'
2
- import { State } from '../src/state'
3
-
4
- describe('State', () => {
5
- it('initializes with empty state by default', () => {
6
- const state = new State()
7
- expect(state.current).toEqual({})
8
- expect(state.version).toBe(0)
9
- })
10
-
11
- it('initializes with provided initial state', () => {
12
- const state = new State({ initialState: { count: 0, name: 'test' } })
13
- expect(state.get('count')).toBe(0)
14
- expect(state.get('name')).toBe('test')
15
- })
16
-
17
- it('set and get', () => {
18
- const state = new State<{ x: number; y: string }>()
19
- state.set('x', 42)
20
- state.set('y', 'hello')
21
- expect(state.get('x')).toBe(42)
22
- expect(state.get('y')).toBe('hello')
23
- })
24
-
25
- it('increments version on set', () => {
26
- const state = new State<{ a: number }>()
27
- expect(state.version).toBe(0)
28
- state.set('a', 1)
29
- expect(state.version).toBe(1)
30
- state.set('a', 2)
31
- expect(state.version).toBe(2)
32
- })
33
-
34
- it('does not increment version when setting same value', () => {
35
- const state = new State<{ a: number }>()
36
- state.set('a', 1)
37
- expect(state.version).toBe(1)
38
- state.set('a', 1)
39
- expect(state.version).toBe(1)
40
- })
41
-
42
- it('has', () => {
43
- const state = new State<{ a: number }>()
44
- expect(state.has('a')).toBe(false)
45
- state.set('a', 1)
46
- expect(state.has('a')).toBe(true)
47
- })
48
-
49
- it('delete', () => {
50
- const state = new State<{ a: number }>()
51
- state.set('a', 1)
52
- expect(state.has('a')).toBe(true)
53
- state.delete('a')
54
- expect(state.has('a')).toBe(false)
55
- expect(state.get('a')).toBeUndefined()
56
- })
57
-
58
- it('keys, entries, values', () => {
59
- const state = new State<{ a: number; b: string }>()
60
- state.set('a', 1).set('b', 'two')
61
- expect(state.keys()).toEqual(['a', 'b'])
62
- expect(state.entries()).toEqual([['a', 1], ['b', 'two']])
63
- expect(state.values()).toEqual([1, 'two'])
64
- })
65
-
66
- it('clear', () => {
67
- const state = new State<{ a: number; b: string }>()
68
- state.set('a', 1).set('b', 'two')
69
- state.clear()
70
- expect(state.keys()).toEqual([])
71
- expect(state.current).toEqual({})
72
- })
73
-
74
- it('setState with partial object', () => {
75
- const state = new State<{ a: number; b: string }>({ initialState: { a: 0, b: '' } })
76
- state.setState({ a: 10, b: 'updated' })
77
- expect(state.get('a')).toBe(10)
78
- expect(state.get('b')).toBe('updated')
79
- })
80
-
81
- it('setState with function', () => {
82
- const state = new State<{ count: number }>({ initialState: { count: 5 } })
83
- state.setState((current) => ({ count: current.count + 1 }))
84
- expect(state.get('count')).toBe(6)
85
- })
86
-
87
- it('observers are notified on add', () => {
88
- const state = new State<{ a: number }>()
89
- const observer = mock()
90
- state.observe(observer)
91
- state.set('a', 1)
92
- expect(observer).toHaveBeenCalledWith('add', 'a', 1)
93
- })
94
-
95
- it('observers are notified on update', () => {
96
- const state = new State<{ a: number }>({ initialState: { a: 0 } })
97
- const observer = mock()
98
- state.observe(observer)
99
- state.set('a', 5)
100
- expect(observer).toHaveBeenCalledWith('update', 'a', 5)
101
- })
102
-
103
- it('observers are notified on delete', () => {
104
- const state = new State<{ a: number }>({ initialState: { a: 1 } })
105
- const observer = mock()
106
- state.observe(observer)
107
- state.delete('a')
108
- expect(observer).toHaveBeenCalledWith('delete', 'a', 1)
109
- })
110
-
111
- it('unsubscribe stops notifications', () => {
112
- const state = new State<{ a: number }>()
113
- const observer = mock()
114
- const unsubscribe = state.observe(observer)
115
- state.set('a', 1)
116
- expect(observer).toHaveBeenCalledTimes(1)
117
- unsubscribe()
118
- state.set('a', 2)
119
- expect(observer).toHaveBeenCalledTimes(1)
120
- })
121
- })