@ai.ntellect/core 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (435) hide show
  1. package/.mocharc.json +6 -0
  2. package/AGENTS.md +50 -0
  3. package/ARCHITECTURE.md +58 -0
  4. package/BENCHMARK.md +38 -0
  5. package/CHANGELOG.md +224 -0
  6. package/README.md +411 -142
  7. package/agent/agent-workflow.ts +569 -0
  8. package/agent/agent.ts +183 -0
  9. package/agent/base/executor.ts +103 -0
  10. package/agent/base/index.ts +87 -0
  11. package/agent/entity-resolver/confidence.ts +54 -0
  12. package/agent/entity-resolver/entity-index.ts +241 -0
  13. package/agent/entity-resolver/explore-strategy.ts +82 -0
  14. package/agent/entity-resolver/index.ts +22 -0
  15. package/agent/entity-resolver/resolver.ts +193 -0
  16. package/agent/entity-resolver/scorer.ts +211 -0
  17. package/agent/entity-resolver/types.ts +98 -0
  18. package/agent/generic-executor.ts +713 -0
  19. package/agent/handlers/cognitive-handler.ts +197 -0
  20. package/agent/handlers/index.ts +1 -0
  21. package/agent/handoff.ts +58 -0
  22. package/agent/llm-factory.ts +343 -0
  23. package/agent/orchestrator.ts +45 -0
  24. package/agent/prompt-builder.ts +78 -0
  25. package/agent/registry.ts +64 -0
  26. package/agent/tools/file-system.ts +471 -0
  27. package/agent/tools/index.ts +23 -0
  28. package/agent/tools/logger.ts +105 -0
  29. package/agent/tools/tool-resolver.ts +104 -0
  30. package/benchmark/cortexflow-workflow.ts +182 -0
  31. package/benchmark/langgraph-workflow.ts +201 -0
  32. package/benchmark/llm-client.ts +76 -0
  33. package/benchmark/run-benchmark.ts +222 -0
  34. package/cli-dev.ts +419 -0
  35. package/dist/agent/agent-workflow.d.ts +41 -0
  36. package/dist/agent/agent-workflow.d.ts.map +1 -0
  37. package/dist/agent/agent-workflow.js +560 -0
  38. package/dist/agent/agent-workflow.js.map +1 -0
  39. package/dist/agent/agent.d.ts +38 -0
  40. package/dist/agent/agent.d.ts.map +1 -0
  41. package/dist/agent/agent.js +149 -0
  42. package/dist/agent/agent.js.map +1 -0
  43. package/dist/agent/base/executor.d.ts +51 -0
  44. package/dist/agent/base/executor.d.ts.map +1 -0
  45. package/dist/agent/base/executor.js +69 -0
  46. package/dist/agent/base/executor.js.map +1 -0
  47. package/dist/agent/base/index.d.ts +30 -0
  48. package/dist/agent/base/index.d.ts.map +1 -0
  49. package/dist/agent/base/index.js +88 -0
  50. package/dist/agent/base/index.js.map +1 -0
  51. package/dist/agent/entity-resolver/confidence.d.ts +4 -0
  52. package/dist/agent/entity-resolver/confidence.d.ts.map +1 -0
  53. package/dist/agent/entity-resolver/confidence.js +47 -0
  54. package/dist/agent/entity-resolver/confidence.js.map +1 -0
  55. package/dist/agent/entity-resolver/entity-index.d.ts +40 -0
  56. package/dist/agent/entity-resolver/entity-index.d.ts.map +1 -0
  57. package/dist/agent/entity-resolver/entity-index.js +244 -0
  58. package/dist/agent/entity-resolver/entity-index.js.map +1 -0
  59. package/dist/agent/entity-resolver/explore-strategy.d.ts +13 -0
  60. package/dist/agent/entity-resolver/explore-strategy.d.ts.map +1 -0
  61. package/dist/agent/entity-resolver/explore-strategy.js +109 -0
  62. package/dist/agent/entity-resolver/explore-strategy.js.map +1 -0
  63. package/dist/agent/entity-resolver/index.d.ts +8 -0
  64. package/dist/agent/entity-resolver/index.d.ts.map +1 -0
  65. package/dist/agent/entity-resolver/index.js +16 -0
  66. package/dist/agent/entity-resolver/index.js.map +1 -0
  67. package/dist/agent/entity-resolver/resolver.d.ts +18 -0
  68. package/dist/agent/entity-resolver/resolver.d.ts.map +1 -0
  69. package/dist/agent/entity-resolver/resolver.js +158 -0
  70. package/dist/agent/entity-resolver/resolver.js.map +1 -0
  71. package/dist/agent/entity-resolver/scorer.d.ts +3 -0
  72. package/dist/agent/entity-resolver/scorer.d.ts.map +1 -0
  73. package/dist/agent/entity-resolver/scorer.js +182 -0
  74. package/dist/agent/entity-resolver/scorer.js.map +1 -0
  75. package/dist/agent/entity-resolver/types.d.ts +89 -0
  76. package/dist/agent/entity-resolver/types.d.ts.map +1 -0
  77. package/dist/agent/entity-resolver/types.js +3 -0
  78. package/dist/agent/entity-resolver/types.js.map +1 -0
  79. package/dist/agent/generic-executor.d.ts +68 -0
  80. package/dist/agent/generic-executor.d.ts.map +1 -0
  81. package/dist/agent/generic-executor.js +620 -0
  82. package/dist/agent/generic-executor.js.map +1 -0
  83. package/dist/agent/handlers/cognitive-handler.d.ts +44 -0
  84. package/dist/agent/handlers/cognitive-handler.d.ts.map +1 -0
  85. package/dist/agent/handlers/cognitive-handler.js +172 -0
  86. package/dist/agent/handlers/cognitive-handler.js.map +1 -0
  87. package/dist/agent/handlers/index.d.ts +2 -0
  88. package/dist/agent/handlers/index.d.ts.map +1 -0
  89. package/dist/agent/handlers/index.js +7 -0
  90. package/dist/agent/handlers/index.js.map +1 -0
  91. package/dist/agent/handoff.d.ts +12 -0
  92. package/dist/agent/handoff.d.ts.map +1 -0
  93. package/dist/agent/handoff.js +62 -0
  94. package/dist/agent/handoff.js.map +1 -0
  95. package/dist/agent/llm-factory.d.ts +19 -0
  96. package/dist/agent/llm-factory.d.ts.map +1 -0
  97. package/dist/agent/llm-factory.js +297 -0
  98. package/dist/agent/llm-factory.js.map +1 -0
  99. package/dist/agent/orchestrator.d.ts +18 -0
  100. package/dist/agent/orchestrator.d.ts.map +1 -0
  101. package/dist/agent/orchestrator.js +44 -0
  102. package/dist/agent/orchestrator.js.map +1 -0
  103. package/dist/agent/prompt-builder.d.ts +35 -0
  104. package/dist/agent/prompt-builder.d.ts.map +1 -0
  105. package/dist/agent/prompt-builder.js +76 -0
  106. package/dist/agent/prompt-builder.js.map +1 -0
  107. package/dist/agent/registry.d.ts +19 -0
  108. package/dist/agent/registry.d.ts.map +1 -0
  109. package/dist/agent/registry.js +48 -0
  110. package/dist/agent/registry.js.map +1 -0
  111. package/dist/agent/tools/file-system.d.ts +11 -0
  112. package/dist/agent/tools/file-system.d.ts.map +1 -0
  113. package/dist/agent/tools/file-system.js +528 -0
  114. package/dist/agent/tools/file-system.js.map +1 -0
  115. package/dist/agent/tools/index.d.ts +4 -0
  116. package/dist/agent/tools/index.d.ts.map +1 -0
  117. package/dist/agent/tools/index.js +20 -0
  118. package/dist/agent/tools/index.js.map +1 -0
  119. package/dist/agent/tools/logger.d.ts +33 -0
  120. package/dist/agent/tools/logger.d.ts.map +1 -0
  121. package/dist/agent/tools/logger.js +86 -0
  122. package/dist/agent/tools/logger.js.map +1 -0
  123. package/dist/agent/tools/tool-resolver.d.ts +7 -0
  124. package/dist/agent/tools/tool-resolver.d.ts.map +1 -0
  125. package/dist/agent/tools/tool-resolver.js +122 -0
  126. package/dist/agent/tools/tool-resolver.js.map +1 -0
  127. package/dist/execution/adapters/in-memory-checkpoint.d.ts +11 -0
  128. package/dist/execution/adapters/in-memory-checkpoint.d.ts.map +1 -0
  129. package/dist/execution/adapters/in-memory-checkpoint.js +54 -0
  130. package/dist/execution/adapters/in-memory-checkpoint.js.map +1 -0
  131. package/dist/execution/compiler.d.ts +8 -0
  132. package/dist/execution/compiler.d.ts.map +1 -0
  133. package/dist/execution/compiler.js +50 -0
  134. package/dist/execution/compiler.js.map +1 -0
  135. package/dist/execution/controller.d.ts +30 -0
  136. package/dist/execution/controller.d.ts.map +1 -0
  137. package/dist/execution/controller.js +80 -0
  138. package/dist/execution/controller.js.map +1 -0
  139. package/dist/execution/event-manager.d.ts +29 -0
  140. package/dist/execution/event-manager.d.ts.map +1 -0
  141. package/dist/execution/event-manager.js +264 -0
  142. package/dist/execution/event-manager.js.map +1 -0
  143. package/dist/execution/index.d.ts +221 -0
  144. package/dist/execution/index.d.ts.map +1 -0
  145. package/dist/execution/index.js +638 -0
  146. package/dist/execution/index.js.map +1 -0
  147. package/dist/execution/logger.d.ts +46 -0
  148. package/dist/execution/logger.d.ts.map +1 -0
  149. package/dist/execution/logger.js +69 -0
  150. package/dist/execution/logger.js.map +1 -0
  151. package/dist/execution/node.d.ts +65 -0
  152. package/dist/execution/node.d.ts.map +1 -0
  153. package/dist/execution/node.js +250 -0
  154. package/dist/execution/node.js.map +1 -0
  155. package/dist/execution/observer.d.ts +38 -0
  156. package/dist/execution/observer.d.ts.map +1 -0
  157. package/dist/execution/observer.js +111 -0
  158. package/dist/execution/observer.js.map +1 -0
  159. package/dist/execution/planner.d.ts +14 -0
  160. package/dist/execution/planner.d.ts.map +1 -0
  161. package/dist/execution/planner.js +43 -0
  162. package/dist/execution/planner.js.map +1 -0
  163. package/dist/execution/reducer.d.ts +19 -0
  164. package/dist/execution/reducer.d.ts.map +1 -0
  165. package/dist/execution/reducer.js +61 -0
  166. package/dist/execution/reducer.js.map +1 -0
  167. package/dist/execution/registry.d.ts +38 -0
  168. package/dist/execution/registry.d.ts.map +1 -0
  169. package/dist/execution/registry.js +63 -0
  170. package/dist/execution/registry.js.map +1 -0
  171. package/dist/execution/send-api.d.ts +24 -0
  172. package/dist/execution/send-api.d.ts.map +1 -0
  173. package/dist/execution/send-api.js +52 -0
  174. package/dist/execution/send-api.js.map +1 -0
  175. package/dist/execution/types.parallel.d.ts +61 -0
  176. package/dist/execution/types.parallel.d.ts.map +1 -0
  177. package/dist/execution/types.parallel.js +3 -0
  178. package/dist/execution/types.parallel.js.map +1 -0
  179. package/dist/execution/visualizer.d.ts +35 -0
  180. package/dist/execution/visualizer.d.ts.map +1 -0
  181. package/dist/execution/visualizer.js +131 -0
  182. package/dist/execution/visualizer.js.map +1 -0
  183. package/dist/index.d.ts +47 -0
  184. package/dist/index.d.ts.map +1 -0
  185. package/dist/index.js +78 -0
  186. package/dist/index.js.map +1 -0
  187. package/dist/interfaces/index.d.ts +487 -0
  188. package/dist/interfaces/index.d.ts.map +1 -0
  189. package/dist/interfaces/index.js +75 -0
  190. package/dist/interfaces/index.js.map +1 -0
  191. package/dist/modules/agenda/adapters/node-cron/index.d.ts +17 -0
  192. package/dist/modules/agenda/adapters/node-cron/index.d.ts.map +1 -0
  193. package/dist/modules/agenda/adapters/node-cron/index.js +30 -0
  194. package/dist/modules/agenda/adapters/node-cron/index.js.map +1 -0
  195. package/dist/modules/agenda/index.d.ts +63 -0
  196. package/dist/modules/agenda/index.d.ts.map +1 -0
  197. package/dist/modules/agenda/index.js +141 -0
  198. package/dist/modules/agenda/index.js.map +1 -0
  199. package/dist/modules/cli/index.d.ts +13 -0
  200. package/dist/modules/cli/index.d.ts.map +1 -0
  201. package/dist/modules/cli/index.js +672 -0
  202. package/dist/modules/cli/index.js.map +1 -0
  203. package/dist/modules/embedding/adapters/ai/index.d.ts +29 -0
  204. package/dist/modules/embedding/adapters/ai/index.d.ts.map +1 -0
  205. package/dist/modules/embedding/adapters/ai/index.js +58 -0
  206. package/dist/modules/embedding/adapters/ai/index.js.map +1 -0
  207. package/dist/modules/embedding/index.d.ts +36 -0
  208. package/dist/modules/embedding/index.d.ts.map +1 -0
  209. package/dist/modules/embedding/index.js +60 -0
  210. package/dist/modules/embedding/index.js.map +1 -0
  211. package/dist/modules/nlp/engine.d.ts +126 -0
  212. package/dist/modules/nlp/engine.d.ts.map +1 -0
  213. package/dist/modules/nlp/engine.js +299 -0
  214. package/dist/modules/nlp/engine.js.map +1 -0
  215. package/dist/modules/nlp/index.d.ts +27 -0
  216. package/dist/modules/nlp/index.d.ts.map +1 -0
  217. package/dist/modules/nlp/index.js +56 -0
  218. package/dist/modules/nlp/index.js.map +1 -0
  219. package/dist/persistence/index.d.ts +12 -0
  220. package/dist/persistence/index.d.ts.map +1 -0
  221. package/dist/persistence/index.js +24 -0
  222. package/dist/persistence/index.js.map +1 -0
  223. package/dist/persistence/neo4j/driver.d.ts +26 -0
  224. package/dist/persistence/neo4j/driver.d.ts.map +1 -0
  225. package/dist/persistence/neo4j/driver.js +64 -0
  226. package/dist/persistence/neo4j/driver.js.map +1 -0
  227. package/dist/persistence/neo4j/entity-store.d.ts +15 -0
  228. package/dist/persistence/neo4j/entity-store.d.ts.map +1 -0
  229. package/dist/persistence/neo4j/entity-store.js +136 -0
  230. package/dist/persistence/neo4j/entity-store.js.map +1 -0
  231. package/dist/persistence/neo4j/execution-tracer.d.ts +19 -0
  232. package/dist/persistence/neo4j/execution-tracer.d.ts.map +1 -0
  233. package/dist/persistence/neo4j/execution-tracer.js +166 -0
  234. package/dist/persistence/neo4j/execution-tracer.js.map +1 -0
  235. package/dist/persistence/neo4j/memory-adapter.d.ts +35 -0
  236. package/dist/persistence/neo4j/memory-adapter.d.ts.map +1 -0
  237. package/dist/persistence/neo4j/memory-adapter.js +252 -0
  238. package/dist/persistence/neo4j/memory-adapter.js.map +1 -0
  239. package/dist/persistence/neo4j/petri-checkpoint-adapter.d.ts +22 -0
  240. package/dist/persistence/neo4j/petri-checkpoint-adapter.d.ts.map +1 -0
  241. package/dist/persistence/neo4j/petri-checkpoint-adapter.js +141 -0
  242. package/dist/persistence/neo4j/petri-checkpoint-adapter.js.map +1 -0
  243. package/dist/pipeline/agent-pipeline.d.ts +99 -0
  244. package/dist/pipeline/agent-pipeline.d.ts.map +1 -0
  245. package/dist/pipeline/agent-pipeline.js +356 -0
  246. package/dist/pipeline/agent-pipeline.js.map +1 -0
  247. package/dist/routing/checkpoint-adapter.d.ts +30 -0
  248. package/dist/routing/checkpoint-adapter.d.ts.map +1 -0
  249. package/dist/routing/checkpoint-adapter.js +83 -0
  250. package/dist/routing/checkpoint-adapter.js.map +1 -0
  251. package/dist/routing/documentation-generator.d.ts +47 -0
  252. package/dist/routing/documentation-generator.d.ts.map +1 -0
  253. package/dist/routing/documentation-generator.js +320 -0
  254. package/dist/routing/documentation-generator.js.map +1 -0
  255. package/dist/routing/index.d.ts +38 -0
  256. package/dist/routing/index.d.ts.map +1 -0
  257. package/dist/routing/index.js +375 -0
  258. package/dist/routing/index.js.map +1 -0
  259. package/dist/routing/intent-classifier.d.ts +206 -0
  260. package/dist/routing/intent-classifier.d.ts.map +1 -0
  261. package/dist/routing/intent-classifier.js +267 -0
  262. package/dist/routing/intent-classifier.js.map +1 -0
  263. package/dist/routing/matrix.d.ts +6 -0
  264. package/dist/routing/matrix.d.ts.map +1 -0
  265. package/dist/routing/matrix.js +131 -0
  266. package/dist/routing/matrix.js.map +1 -0
  267. package/dist/routing/orchestrator.d.ts +87 -0
  268. package/dist/routing/orchestrator.d.ts.map +1 -0
  269. package/dist/routing/orchestrator.js +425 -0
  270. package/dist/routing/orchestrator.js.map +1 -0
  271. package/dist/routing/postgres-checkpoint-adapter.d.ts +32 -0
  272. package/dist/routing/postgres-checkpoint-adapter.d.ts.map +1 -0
  273. package/dist/routing/postgres-checkpoint-adapter.js +167 -0
  274. package/dist/routing/postgres-checkpoint-adapter.js.map +1 -0
  275. package/dist/routing/redis-checkpoint-adapter.d.ts +34 -0
  276. package/dist/routing/redis-checkpoint-adapter.d.ts.map +1 -0
  277. package/dist/routing/redis-checkpoint-adapter.js +170 -0
  278. package/dist/routing/redis-checkpoint-adapter.js.map +1 -0
  279. package/dist/routing/types.d.ts +49 -0
  280. package/dist/routing/types.d.ts.map +1 -0
  281. package/dist/routing/types.js +3 -0
  282. package/dist/routing/types.js.map +1 -0
  283. package/dist/types/agent.d.ts +289 -0
  284. package/dist/types/agent.d.ts.map +1 -0
  285. package/dist/types/agent.js +78 -0
  286. package/dist/types/agent.js.map +1 -0
  287. package/dist/types/index.d.ts +343 -0
  288. package/dist/types/index.d.ts.map +1 -0
  289. package/dist/types/index.js +3 -0
  290. package/dist/types/index.js.map +1 -0
  291. package/dist/utils/generate-action-schema.d.ts +4 -0
  292. package/dist/utils/generate-action-schema.d.ts.map +1 -0
  293. package/dist/utils/generate-action-schema.js +46 -0
  294. package/dist/utils/generate-action-schema.js.map +1 -0
  295. package/dist/utils/header-builder.d.ts +12 -0
  296. package/dist/utils/header-builder.d.ts.map +1 -0
  297. package/dist/utils/header-builder.js +35 -0
  298. package/dist/utils/header-builder.js.map +1 -0
  299. package/dist/utils/logger.d.ts +27 -0
  300. package/dist/utils/logger.d.ts.map +1 -0
  301. package/dist/utils/logger.js +45 -0
  302. package/dist/utils/logger.js.map +1 -0
  303. package/docs/.gitbook/assets/image (1).png +0 -0
  304. package/docs/.gitbook/assets/image (2).png +0 -0
  305. package/docs/.gitbook/assets/image (3).png +0 -0
  306. package/docs/.gitbook/assets/image (4).png +0 -0
  307. package/docs/.gitbook/assets/image (5).png +0 -0
  308. package/docs/.gitbook/assets/image (6).png +0 -0
  309. package/docs/.gitbook/assets/image.png +0 -0
  310. package/docs/README.md +57 -0
  311. package/docs/SUMMARY.md +34 -0
  312. package/docs/cas-dusages.md +69 -0
  313. package/docs/cli/README.md +65 -0
  314. package/docs/concepts-cles.md +50 -0
  315. package/docs/core/architecture.md +87 -0
  316. package/docs/core/benchmark.md +59 -0
  317. package/docs/core/checkpoint.md +72 -0
  318. package/docs/core/documentation.md +55 -0
  319. package/docs/core/graphcontroller.md +63 -0
  320. package/docs/core/graphflow.md +137 -0
  321. package/docs/core/introduction.md +41 -0
  322. package/docs/core/les-evenements.md +95 -0
  323. package/docs/modules/agenda/README.md +63 -0
  324. package/docs/modules/agenda/interface-iagenda.md +170 -0
  325. package/docs/modules/agenda/les-adaptateurs/README.md +237 -0
  326. package/docs/modules/agenda/les-adaptateurs/nodecronadapter.md +91 -0
  327. package/docs/modules/introduction.md +55 -0
  328. package/docs/modules/les-adaptateurs.md +52 -0
  329. package/docs/modules/memoire/README.md +68 -0
  330. package/docs/modules/memoire/interface-imemory.md +183 -0
  331. package/docs/modules/memoire/les-adaptateurs/README.md +209 -0
  332. package/docs/modules/memoire/les-adaptateurs/inmemoryadapter.md +110 -0
  333. package/docs/modules/memoire/les-adaptateurs/meilisearchadapter.md +147 -0
  334. package/docs/modules/memoire/les-adaptateurs/redisadapter.md +212 -0
  335. package/docs/modules/nlp/README.md +44 -0
  336. package/docs/philosophie.md +51 -0
  337. package/docs/tutoriels/ajouter-des-conditions.md +150 -0
  338. package/docs/tutoriels/branching.md +194 -0
  339. package/docs/tutoriels/checkpoint-usage.md +99 -0
  340. package/docs/tutoriels/creer-agent-onchain.md +1041 -0
  341. package/docs/tutoriels/creer-un-agent.md +108 -0
  342. package/docs/tutoriels/creer-un-graphe-simple.md +92 -0
  343. package/docs/tutoriels/gerer-les-erreurs.md +124 -0
  344. package/docs/tutoriels/pour-commencer.md +73 -0
  345. package/docs/tutoriels/retry.md +166 -0
  346. package/execution/adapters/in-memory-checkpoint.ts +35 -0
  347. package/execution/compiler.ts +47 -0
  348. package/execution/controller.ts +84 -0
  349. package/execution/event-manager.ts +331 -0
  350. package/execution/index.ts +800 -0
  351. package/execution/logger.ts +70 -0
  352. package/execution/node.ts +315 -0
  353. package/execution/observer.ts +192 -0
  354. package/execution/planner.ts +40 -0
  355. package/execution/reducer.ts +73 -0
  356. package/execution/registry.ts +86 -0
  357. package/execution/send-api.ts +58 -0
  358. package/execution/types.parallel.ts +81 -0
  359. package/execution/visualizer.ts +158 -0
  360. package/index.ts +55 -465
  361. package/interfaces/index.ts +597 -0
  362. package/modules/agenda/adapters/node-cron/index.ts +25 -0
  363. package/modules/agenda/index.ts +146 -0
  364. package/modules/cli/index.ts +580 -0
  365. package/modules/embedding/adapters/ai/index.ts +42 -0
  366. package/modules/embedding/index.ts +45 -0
  367. package/modules/nlp/engine.ts +324 -0
  368. package/modules/nlp/index.ts +45 -0
  369. package/package.json +81 -9
  370. package/persistence/index.ts +27 -0
  371. package/persistence/neo4j/driver.ts +34 -0
  372. package/persistence/neo4j/entity-store.ts +141 -0
  373. package/persistence/neo4j/execution-tracer.ts +194 -0
  374. package/persistence/neo4j/memory-adapter.ts +281 -0
  375. package/persistence/neo4j/petri-checkpoint-adapter.ts +153 -0
  376. package/pipeline/agent-pipeline.ts +426 -0
  377. package/routing/checkpoint-adapter.ts +79 -0
  378. package/routing/documentation-generator.ts +358 -0
  379. package/routing/index.ts +459 -0
  380. package/routing/intent-classifier.ts +360 -0
  381. package/routing/matrix.ts +138 -0
  382. package/routing/orchestrator.ts +498 -0
  383. package/routing/patterns/data-extraction.json +79 -0
  384. package/routing/patterns/human-approval.json +64 -0
  385. package/routing/patterns/rag-search.json +68 -0
  386. package/routing/postgres-checkpoint-adapter.ts +172 -0
  387. package/routing/redis-checkpoint-adapter.ts +187 -0
  388. package/routing/types.ts +59 -0
  389. package/routing/web-server.ts +260 -0
  390. package/scripts/generate-petri-docs.ts +70 -0
  391. package/scripts/get-gmail-token.js +65 -0
  392. package/scripts/get-gmail-token.ts +65 -0
  393. package/test/agent/agent.test.ts +92 -0
  394. package/test/agent/clone.test.ts +143 -0
  395. package/test/agent/cognitive-handler.test.ts +78 -0
  396. package/test/agent/entity-store.test.ts +80 -0
  397. package/test/agent/generic-executor.test.ts +230 -0
  398. package/test/agent/handoff.test.ts +163 -0
  399. package/test/agent/llm-factory.test.ts +40 -0
  400. package/test/agent/orchestrator.test.ts +156 -0
  401. package/test/agent/registry.test.ts +97 -0
  402. package/test/agent/tools.test.ts +267 -0
  403. package/test/execution/checkpoint.test.ts +811 -0
  404. package/test/execution/controller.test.ts +236 -0
  405. package/test/execution/event-manager.test.ts +118 -0
  406. package/test/execution/index.test.ts +690 -0
  407. package/test/execution/node.test.ts +464 -0
  408. package/test/execution/observer.test.ts +393 -0
  409. package/test/execution/parallel.test.ts +135 -0
  410. package/test/execution/plan-llm-integration.test.ts +290 -0
  411. package/test/execution/plan-real-onchain.test.ts +226 -0
  412. package/test/execution/send-api.test.ts +121 -0
  413. package/test/modules/agenda/node-cron.test.ts +307 -0
  414. package/test/modules/cli/index.test.ts +125 -0
  415. package/test/persistence/neo4j-execution-tracer.test.ts +96 -0
  416. package/test/persistence/neo4j-memory-adapter.test.ts +107 -0
  417. package/test/persistence/neo4j-petri-checkpoint.test.ts +89 -0
  418. package/test/pipeline/agent-pipeline.test.ts +118 -0
  419. package/test/routing/checkpoint-persistence.test.ts +58 -0
  420. package/test/routing/documentation-generator.test.ts +76 -0
  421. package/test/routing/integration.test.ts +261 -0
  422. package/test/routing/intent-classifier.test.ts +102 -0
  423. package/test/routing/petri.test.ts +156 -0
  424. package/test/routing/real-llm.test.ts +260 -0
  425. package/test-petri-features.ts +218 -0
  426. package/test-pipeline-api.ts +163 -0
  427. package/tsconfig.json +30 -108
  428. package/types/agent.ts +296 -0
  429. package/types/index.ts +387 -0
  430. package/utils/generate-action-schema.ts +47 -0
  431. package/utils/header-builder.ts +40 -0
  432. package/utils/logger.ts +40 -0
  433. package/package copy.json +0 -21
  434. package/types.ts +0 -62
  435. package/utils/executor.ts +0 -42
@@ -0,0 +1,324 @@
1
+ import { ActionHandler, NLPConfig } from "../../types";
2
+
3
+ const { dockStart } = require("@nlpjs/basic");
4
+
5
+ /**
6
+ * NLP Engine for processing natural language commands
7
+ * @example
8
+ * const engine = await NLPEngine.create({
9
+ * corpus: require('./corpus.json'),
10
+ * responses: require('./responses.json'),
11
+ * entities: require('./entities.json')
12
+ * });
13
+ *
14
+ * engine.addAction('transferTokens', async (data) => {
15
+ * // Handle transfer logic
16
+ * });
17
+ *
18
+ * const response = await engine.process('transfer 5 ETH');
19
+ */
20
+ export class NLPEngine {
21
+ private manager: any;
22
+ private responses: Record<string, any>;
23
+ private intentHandlers: Record<string, ActionHandler> = {};
24
+
25
+ constructor() {
26
+ this.responses = {};
27
+ }
28
+
29
+ /**
30
+ * Creates and initializes a new NLP Engine
31
+ */
32
+ static async create(config: NLPConfig): Promise<NLPEngine> {
33
+ const engine = new NLPEngine();
34
+ await engine.init(config);
35
+ return engine;
36
+ }
37
+
38
+ /**
39
+ * Initializes the NLP engine with configuration
40
+ */
41
+ private async init({
42
+ corpus,
43
+ responses,
44
+ entities,
45
+ language = "en",
46
+ threshold = 0.5,
47
+ }: NLPConfig) {
48
+ const dock = await dockStart({
49
+ settings: {
50
+ nlp: {
51
+ forceNER: true,
52
+ languages: [language],
53
+ corpora: corpus ? [corpus] : undefined,
54
+ },
55
+ ner: { threshold },
56
+ },
57
+ use: ["Basic", "LangEn"],
58
+ });
59
+
60
+ this.manager = dock.get("nlp");
61
+ this.responses = responses || {};
62
+
63
+ if (corpus) {
64
+ await this.manager.addCorpus(corpus);
65
+ }
66
+
67
+ if (entities) {
68
+ this.registerEntities(entities);
69
+ }
70
+
71
+ await this.manager.train();
72
+ }
73
+
74
+ /**
75
+ * Registers entities for Named Entity Recognition (NER)
76
+ * @private
77
+ * @param {Record<string, any>} entities - Entity definitions
78
+ */
79
+ private registerEntities(entities: Record<string, any>) {
80
+ Object.entries(entities).forEach(([name, data]: [string, any]) => {
81
+ if (data.options) {
82
+ Object.entries(data.options).forEach(([option, texts]) => {
83
+ this.manager.addNerRuleOptionTexts(
84
+ "en",
85
+ name,
86
+ option,
87
+ texts as string[]
88
+ );
89
+ });
90
+ } else if (data.regex) {
91
+ this.manager.addNerRegexRule("en", name, data.regex);
92
+ } else if (data.trim) {
93
+ data.trim.forEach((trimRule: any) => {
94
+ if (trimRule.position === "afterLast") {
95
+ trimRule.words.forEach((word: string) => {
96
+ this.manager.addNerAfterLastCondition("en", name, word);
97
+ });
98
+ } else if (trimRule.position === "betweenLast") {
99
+ this.manager.addNerBetweenLastCondition(
100
+ "en",
101
+ name,
102
+ trimRule.leftWords,
103
+ trimRule.rightWords
104
+ );
105
+ }
106
+ });
107
+ }
108
+ });
109
+ }
110
+
111
+ /**
112
+ * Adds an action handler for a specific intent
113
+ * @param {string} name - Intent name
114
+ * @param {ActionHandler} handler - Action handler function
115
+ */
116
+ addAction(name: string, handler: ActionHandler): void {
117
+ this.intentHandlers[name] = handler;
118
+ }
119
+
120
+ /**
121
+ * Creates and loads a pre-trained NLP Engine
122
+ */
123
+ static async loadFromModel(
124
+ modelPath: string,
125
+ responses?: Record<string, any>
126
+ ): Promise<NLPEngine> {
127
+ const engine = new NLPEngine();
128
+ engine.responses = responses || {};
129
+ await engine.loadModel(modelPath);
130
+ return engine;
131
+ }
132
+
133
+ /**
134
+ * Loads a pre-trained model and configures default actions
135
+ */
136
+ private async loadModel(modelPath: string) {
137
+ const dock = await dockStart({
138
+ settings: {
139
+ nlp: {
140
+ forceNER: true,
141
+ languages: ["en"],
142
+ },
143
+ },
144
+ use: ["Basic", "LangEn"],
145
+ });
146
+
147
+ this.manager = dock.get("nlp");
148
+ await this.manager.load(modelPath);
149
+
150
+ // Configure default actions handler
151
+ this.setupDefaultActions();
152
+ }
153
+
154
+ /**
155
+ * Setup default actions to handle responses
156
+ */
157
+ private setupDefaultActions() {
158
+ this.manager.onIntent = async (data: any) => {
159
+ // Execute registered action if exists
160
+ if (data.actions?.length > 0) {
161
+ for (const actionData of data.actions) {
162
+ const result = await this.executeAction(actionData, data);
163
+ if (result) {
164
+ data.actionResult = result;
165
+ }
166
+ }
167
+ }
168
+
169
+ // Format response if answer exists
170
+ if (data.answer && this.responses[data.answer]) {
171
+ data.jsonResponse = this.formatResponse(
172
+ this.responses[data.answer],
173
+ data.actionResult || {}
174
+ );
175
+ }
176
+
177
+ return data;
178
+ };
179
+ }
180
+
181
+ /**
182
+ * Execute a single action
183
+ */
184
+ private async executeAction(actionData: any, context: any): Promise<any> {
185
+ const { action, parameters } = actionData;
186
+ const handler = this.manager.actions[action];
187
+ if (handler) {
188
+ return handler(context);
189
+ }
190
+ return null;
191
+ }
192
+
193
+ /**
194
+ * Processes natural language input text
195
+ * @param {string} input - Input text to process
196
+ * @returns {Promise<any>} Processing result with intent, entities and response
197
+ */
198
+ async process(input: string) {
199
+ const result = await this.manager.process("en", input);
200
+ console.log("NLP Engine Result:", result);
201
+
202
+ if (result.intent) {
203
+ const handler = this.intentHandlers[result.intent.split(".")[1]];
204
+ if (handler) {
205
+ const handlerResult = await handler(result);
206
+ return {
207
+ ...result,
208
+ ...handlerResult,
209
+ };
210
+ }
211
+ }
212
+
213
+ return result;
214
+ }
215
+
216
+ /**
217
+ * Trains the NLP model with current corpus
218
+ * @returns {Promise<void>}
219
+ */
220
+ public async train(): Promise<void> {
221
+ await this.manager.train();
222
+ }
223
+
224
+ /**
225
+ * Exports the trained model
226
+ * @param {boolean} [minified=true] - Whether to minify the exported model
227
+ * @returns {string} Exported model as string
228
+ */
229
+ public export(minified = true): string {
230
+ return this.manager.export(minified);
231
+ }
232
+
233
+ /**
234
+ * Imports a previously trained model
235
+ * @param {string} data - Model data to import
236
+ */
237
+ public import(data: string): void {
238
+ this.manager.import(data);
239
+ }
240
+
241
+ /**
242
+ * Saves the current model to a file
243
+ * @param {string} [path='./model.json'] - Path to save the model
244
+ * @returns {Promise<void>}
245
+ */
246
+ public async saveModel(path = "./model.json"): Promise<void> {
247
+ this.manager.save();
248
+ this.manager.save("./model.json");
249
+ }
250
+
251
+ /**
252
+ * Formats response using template and context
253
+ */
254
+ private formatResponse(template: any, context: any): any {
255
+ const response = JSON.parse(JSON.stringify(template));
256
+
257
+ // Format data fields
258
+ if (response.data) {
259
+ Object.keys(response.data).forEach((key) => {
260
+ if (context[key] !== undefined) {
261
+ response.data[key] = context[key];
262
+ }
263
+ });
264
+ }
265
+
266
+ return response;
267
+ }
268
+
269
+ /**
270
+ * Gets entity value from NLP result
271
+ * @param {any} data - NLP processing result
272
+ * @param {string} entityName - Name of the entity to extract
273
+ * @returns {string|undefined} Entity value if found
274
+ */
275
+ getEntity(data: any, entityName: string): string | undefined {
276
+ const entity = data.entities?.find((e: any) => e.entity === entityName);
277
+ return entity ? entity.utteranceText : undefined;
278
+ }
279
+
280
+ /**
281
+ * Gets entity option from NLP result
282
+ * @param {any} data - NLP processing result
283
+ * @param {string} entityName - Name of the entity to extract
284
+ * @returns {string|undefined} Entity option if found
285
+ */
286
+ getEntityOption(data: any, entityName: string): string | undefined {
287
+ const entity = data.entities?.find((e: any) => e.entity === entityName);
288
+ return entity ? entity.option : undefined;
289
+ }
290
+
291
+ /**
292
+ * Gets recipient entity from NLP result
293
+ * @param {any} data - NLP processing result
294
+ * @returns {string|undefined} Recipient value if found
295
+ */
296
+ getRecipient(data: any): string | undefined {
297
+ const toEntity = data.entities?.find((e: any) => e.entity === "to");
298
+ return toEntity ? toEntity.utteranceText : undefined;
299
+ }
300
+
301
+ /**
302
+ * Gets current responses configuration
303
+ * @returns {Record<string, any>} Current responses
304
+ */
305
+ public getResponses(): Record<string, any> {
306
+ return this.responses;
307
+ }
308
+
309
+ /**
310
+ * Gets current corpus configuration
311
+ * @returns {any} Current corpus
312
+ */
313
+ public getCorpus(): any {
314
+ return this.manager.corpora;
315
+ }
316
+
317
+ /**
318
+ * Gets current entities configuration
319
+ * @returns {any} Current entities configuration
320
+ */
321
+ public getEntities(): any {
322
+ return this.manager.nerManager?.rules || {};
323
+ }
324
+ }
@@ -0,0 +1,45 @@
1
+ import { NLPNodeConfig } from "@/interfaces";
2
+ import { ZodSchema } from "zod";
3
+ import { NLPEngine } from "./engine";
4
+
5
+ /**
6
+ * NLPNode class handles natural language processing operations with type-safe schema validation
7
+ * @template T - Zod schema type for validation
8
+ */
9
+ export class NLPNode<T extends ZodSchema> {
10
+ private engine!: NLPEngine;
11
+
12
+ /**
13
+ * Creates an instance of NLPNode
14
+ * @param {NLPNodeConfig<T>} config - Configuration object for the NLP node
15
+ */
16
+ constructor(private config: NLPNodeConfig<T>) {}
17
+
18
+ /**
19
+ * Initializes the NLP engine and sets up intent handlers
20
+ * @returns {Promise<void>}
21
+ */
22
+ async initialize() {
23
+ this.engine = await NLPEngine.create(this.config.nlpConfig);
24
+ console.log("Loaded engine");
25
+ if (this.config.intentHandlers) {
26
+ Object.entries(this.config.intentHandlers).forEach(
27
+ ([intent, handler]) => {
28
+ this.engine.addAction(intent, handler);
29
+ }
30
+ );
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Processes the input text through the NLP engine
36
+ * @param {string} input - Text to be processed
37
+ * @returns {Promise<any>} - Processing result from the engine
38
+ */
39
+ async process(input: string) {
40
+ console.log("NLP Node processing:", input);
41
+ const result = await this.engine.process(input);
42
+ console.log("NLP Node result:", result);
43
+ return result;
44
+ }
45
+ }
package/package.json CHANGED
@@ -1,19 +1,91 @@
1
1
  {
2
2
  "name": "@ai.ntellect/core",
3
- "version": "1.0.0",
4
- "description": "A robust framework for managing and executing workflows, supporting dynamic decision-making, conditional paths, event-driven execution, and state persistence.",
5
- "main": "index.js",
3
+ "version": "1.1.0",
4
+ "description": "In-process workflow engine with typed graphs, events, and LLM agent support",
5
+ "main": "dist/index.js",
6
+ "packageManager": "pnpm@10.33.0",
6
7
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
+ "build": "rimraf dist && tsc",
9
+ "prepare": "pnpm run build",
10
+ "prepublishOnly": "pnpm run build",
11
+ "cli": "node -r ts-node/register modules/cli/index.ts",
12
+ "example:hello": "ts-node examples/hello-graph.ts",
13
+ "example:events": "ts-node examples/event-triggered-workflow.ts",
14
+ "example:agent": "ts-node examples/agent-tools.ts",
15
+ "example:agent-events": "ts-node examples/agent-events.ts",
16
+ "example:agent-project": "ts-node examples/agent-project-creator.ts",
17
+ "example:agent-complex": "ts-node examples/agent-complex-projects.ts",
18
+ "example:native-tools": "ts-node examples/native-tools-demo.ts",
19
+ "example:wallet": "ts-node examples/hl-wallet-assistant/index.ts",
20
+ "example:onchain": "ts-node examples/onchain-agent/index.ts",
21
+ "example:trading": "ts-node examples/trading-system/index.ts",
22
+ "test": "mocha -r ./node_modules/ts-node/register",
23
+ "test:all": "mocha -r ./node_modules/ts-node/register 'test/**/*.test.ts'",
24
+ "test:coverage": "nyc --reporter=text --reporter=html pnpm test",
25
+ "test:watch": "mocha --require ./node_modules/ts-node/register 'test/**/*.test.ts' --watch --watch-files ./**/*.ts,test/**/*.ts",
26
+ "test:watch:execution": "mocha --require ./node_modules/ts-node/register 'test/execution/**/*.test.ts' --watch --watch-files ./execution/**/*.ts test/**/*.ts",
27
+ "test:petri": "mocha --require ./node_modules/ts-node/register 'test/routing/**/*.test.ts'",
28
+ "dev:cli": "ts-node cli-dev.ts",
29
+ "benchmark": "ts-node benchmark/run-benchmark.ts"
30
+ },
31
+ "keywords": [],
32
+ "author": "Lorcann Rauzduel",
33
+ "license": "MIT",
34
+ "dependencies": {
35
+ "@ai-sdk/openai": "^1.1.15",
36
+ "@nlpjs/basic": "5.0.0-alpha.5",
37
+ "@types/node-cron": "^3.0.11",
38
+ "@types/pg": "^8.20.0",
39
+ "ai": "^4.1.46",
40
+ "chalk": "^5.4.1",
41
+ "dotenv": "^17.4.2",
42
+ "node-cron": "^3.0.3",
43
+ "pg": "^8.20.0",
44
+ "redis": "^4.7.0",
45
+ "rxjs": "^7.8.1",
46
+ "zod": "^4.4.2"
47
+ },
48
+ "devDependencies": {
49
+ "@langchain/core": "^1.1.43",
50
+ "@langchain/langgraph": "^1.2.9",
51
+ "@langchain/ollama": "^1.2.7",
52
+ "@nktkas/hyperliquid": "^0.32.2",
53
+ "@types/chai": "^4.3.20",
54
+ "@types/chai-as-promised": "^7.1.8",
55
+ "@types/mocha": "^10.0.10",
56
+ "@types/node": "^20",
57
+ "@types/open": "^6.2.1",
58
+ "@types/sinon": "^17.0.3",
59
+ "chai": "^4.5.0",
60
+ "chai-as-promised": "^7.1.2",
61
+ "googleapis": "^171.4.0",
62
+ "mocha": "^10.0.0",
63
+ "neo4j-driver": "^6.1.0",
64
+ "nyc": "^17.1.0",
65
+ "open": "^11.0.0",
66
+ "pino": "^10.3.1",
67
+ "pino-pretty": "^13.1.3",
68
+ "redis": "^4.6.13",
69
+ "rimraf": "^5.0.10",
70
+ "sinon": "^19.0.2",
71
+ "ts-node": "10.9.1",
72
+ "typescript": "^5.7.2",
73
+ "viem": "^2.47.17"
74
+ },
75
+ "peerDependencies": {
76
+ "neo4j-driver": "^5.28.0"
77
+ },
78
+ "peerDependenciesMeta": {
79
+ "neo4j-driver": {
80
+ "optional": true
81
+ }
8
82
  },
9
83
  "repository": {
10
84
  "type": "git",
11
- "url": "git+https://github.com/ai-ntellect/workflow.git"
85
+ "url": "git+https://github.com/ai-ntellect/core.git"
12
86
  },
13
- "author": "",
14
- "license": "ISC",
15
87
  "bugs": {
16
- "url": "https://github.com/ai-ntellect/workflow/issues"
88
+ "url": "https://github.com/ai-ntellect/core/issues"
17
89
  },
18
- "homepage": "https://github.com/ai-ntellect/workflow#readme"
90
+ "homepage": "https://github.com/ai-ntellect/core#readme"
19
91
  }
@@ -0,0 +1,27 @@
1
+ export { Memory } from "../modules/memory";
2
+ export { InMemoryCheckpointAdapter } from "../execution/adapters/in-memory-checkpoint";
3
+ export {
4
+ IPetriCheckpointAdapter,
5
+ InMemoryPetriCheckpointAdapter,
6
+ } from "../routing/checkpoint-adapter";
7
+ export { RedisPetriCheckpointAdapter } from "../routing/redis-checkpoint-adapter";
8
+ export { PostgresPetriCheckpointAdapter } from "../routing/postgres-checkpoint-adapter";
9
+ export {
10
+ Neo4jExecutionTracer,
11
+ } from "./neo4j/execution-tracer";
12
+ export {
13
+ Neo4jPetriCheckpointAdapter,
14
+ } from "./neo4j/petri-checkpoint-adapter";
15
+ export {
16
+ Neo4jMemoryAdapter,
17
+ } from "./neo4j/memory-adapter";
18
+ export {
19
+ Neo4jEntityStore,
20
+ IEntityStore,
21
+ } from "./neo4j/entity-store";
22
+ export type {
23
+ Neo4jConfig,
24
+ Neo4jDriverLike,
25
+ Neo4jSessionLike,
26
+ } from "./neo4j/driver";
27
+ export { createDriver } from "./neo4j/driver";
@@ -0,0 +1,34 @@
1
+ export interface Neo4jSessionLike {
2
+ run(query: string, params?: Record<string, any>): Promise<{ records: any[] }>;
3
+ close(): Promise<void>;
4
+ }
5
+
6
+ export interface Neo4jDriverLike {
7
+ session(config?: { database?: string }): Neo4jSessionLike;
8
+ close(): Promise<void>;
9
+ }
10
+
11
+ export type Neo4jConfig =
12
+ | { driver: Neo4jDriverLike; database?: string }
13
+ | { url: string; username: string; password: string; database?: string };
14
+
15
+ export async function createDriver(config: Neo4jConfig): Promise<{ driver: Neo4jDriverLike; database?: string }> {
16
+ if ("driver" in config) {
17
+ return { driver: config.driver, database: config.database };
18
+ }
19
+
20
+ let neo4j: any;
21
+ try {
22
+ neo4j = await import("neo4j-driver");
23
+ } catch {
24
+ throw new Error(
25
+ "neo4j-driver is not installed. Install it with: npm install neo4j-driver"
26
+ );
27
+ }
28
+
29
+ const driver = neo4j.default.auth
30
+ ? neo4j.default.driver(config.url, neo4j.default.auth.basic(config.username, config.password))
31
+ : neo4j.default.driver(config.url, neo4j.default.auth.basic(config.username, config.password));
32
+
33
+ return { driver, database: config.database };
34
+ }
@@ -0,0 +1,141 @@
1
+ import { Entity } from "../../agent/entity-resolver/types";
2
+ import { IEntityStore } from "../../agent/entity-resolver/entity-index";
3
+ import { Neo4jDriverLike, Neo4jSessionLike } from "./driver";
4
+
5
+ export { IEntityStore };
6
+
7
+ export class Neo4jEntityStore implements IEntityStore {
8
+ constructor(
9
+ private driver: Neo4jDriverLike,
10
+ private database?: string
11
+ ) {}
12
+
13
+ private session(): Neo4jSessionLike {
14
+ return this.driver.session(this.database ? { database: this.database } : undefined);
15
+ }
16
+
17
+ async upsertMany(entities: Entity[]): Promise<void> {
18
+ const s = this.session();
19
+ try {
20
+ for (const entity of entities) {
21
+ await s.run(
22
+ `MERGE (e:Entity {id: $id})
23
+ SET e.category = $category,
24
+ e.filename = $filename,
25
+ e.stem = $stem,
26
+ e.extension = $extension,
27
+ e.path = $path,
28
+ e.scope = $scope
29
+ WITH e
30
+ UNWIND $tokens AS tokenValue
31
+ MERGE (t:Token {value: tokenValue})
32
+ MERGE (e)-[:HAS_TOKEN]->(t)`,
33
+ {
34
+ id: entity.id,
35
+ category: entity.category,
36
+ filename: entity.filename,
37
+ stem: entity.stem,
38
+ extension: entity.extension,
39
+ path: entity.path,
40
+ scope: entity.path.split("/")[0] || "root",
41
+ tokens: entity.tokens,
42
+ }
43
+ );
44
+
45
+ const dir = entity.path.substring(0, entity.path.lastIndexOf("/")) || "/";
46
+ await s.run(
47
+ `MATCH (e:Entity {id: $id})
48
+ MERGE (d:Dir {path: $dir})
49
+ MERGE (e)-[:IN_DIR]->(d)`,
50
+ { id: entity.id, dir }
51
+ );
52
+ }
53
+ } finally {
54
+ await s.close();
55
+ }
56
+ }
57
+
58
+ async loadAll(scope?: string): Promise<Entity[]> {
59
+ const s = this.session();
60
+ try {
61
+ let query: string;
62
+ let params: Record<string, any> = {};
63
+
64
+ if (scope) {
65
+ query = `MATCH (e:Entity)
66
+ WHERE e.scope = $scope
67
+ OPTIONAL MATCH (e)-[:HAS_TOKEN]->(t:Token)
68
+ RETURN e, collect(t.value) AS tokens
69
+ ORDER BY e.filename`;
70
+ params.scope = scope;
71
+ } else {
72
+ query = `MATCH (e:Entity)
73
+ OPTIONAL MATCH (e)-[:HAS_TOKEN]->(t:Token)
74
+ RETURN e, collect(t.value) AS tokens
75
+ ORDER BY e.filename`;
76
+ }
77
+
78
+ const result = await s.run(query, params);
79
+
80
+ return result.records.map((r: any) => {
81
+ const entity = r.get("e");
82
+ const props = entity.properties ? (typeof entity.properties === "function" ? entity.properties() : entity.properties) : entity;
83
+ return {
84
+ id: props.id,
85
+ category: props.category || "file",
86
+ filename: props.filename,
87
+ stem: props.stem,
88
+ extension: props.extension || null,
89
+ tokens: r.get("tokens") || [],
90
+ path: props.path,
91
+ } as Entity;
92
+ });
93
+ } finally {
94
+ await s.close();
95
+ }
96
+ }
97
+
98
+ async recordResolution(
99
+ entityId: string,
100
+ query: string,
101
+ confidence: number
102
+ ): Promise<void> {
103
+ const s = this.session();
104
+ try {
105
+ await s.run(
106
+ `MATCH (e:Entity {id: $entityId})
107
+ CREATE (r:Resolution {id: $resolutionId, query: $query, confidence: $confidence, at: $at})
108
+ MERGE (e)-[:RESOLVED_BY]->(r)`,
109
+ {
110
+ entityId,
111
+ resolutionId: `res-${entityId}-${Date.now()}`,
112
+ query,
113
+ confidence,
114
+ at: new Date().toISOString(),
115
+ }
116
+ );
117
+ } finally {
118
+ await s.close();
119
+ }
120
+ }
121
+
122
+ async clear(scope?: string): Promise<void> {
123
+ const s = this.session();
124
+ try {
125
+ if (scope) {
126
+ await s.run(
127
+ `MATCH (e:Entity {scope: $scope})
128
+ DETACH DELETE e`,
129
+ { scope }
130
+ );
131
+ } else {
132
+ await s.run(
133
+ `MATCH (e:Entity)
134
+ DETACH DELETE e`
135
+ );
136
+ }
137
+ } finally {
138
+ await s.close();
139
+ }
140
+ }
141
+ }