@animus-labs/cortex 0.2.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 (293) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +73 -0
  3. package/dist/budget-guard.d.ts +75 -0
  4. package/dist/budget-guard.d.ts.map +1 -0
  5. package/dist/budget-guard.js +142 -0
  6. package/dist/budget-guard.js.map +1 -0
  7. package/dist/compaction/compaction.d.ts +99 -0
  8. package/dist/compaction/compaction.d.ts.map +1 -0
  9. package/dist/compaction/compaction.js +302 -0
  10. package/dist/compaction/compaction.js.map +1 -0
  11. package/dist/compaction/failsafe.d.ts +57 -0
  12. package/dist/compaction/failsafe.d.ts.map +1 -0
  13. package/dist/compaction/failsafe.js +135 -0
  14. package/dist/compaction/failsafe.js.map +1 -0
  15. package/dist/compaction/index.d.ts +381 -0
  16. package/dist/compaction/index.d.ts.map +1 -0
  17. package/dist/compaction/index.js +979 -0
  18. package/dist/compaction/index.js.map +1 -0
  19. package/dist/compaction/microcompaction.d.ts +219 -0
  20. package/dist/compaction/microcompaction.d.ts.map +1 -0
  21. package/dist/compaction/microcompaction.js +536 -0
  22. package/dist/compaction/microcompaction.js.map +1 -0
  23. package/dist/compaction/observational/buffering.d.ts +225 -0
  24. package/dist/compaction/observational/buffering.d.ts.map +1 -0
  25. package/dist/compaction/observational/buffering.js +354 -0
  26. package/dist/compaction/observational/buffering.js.map +1 -0
  27. package/dist/compaction/observational/constants.d.ts +70 -0
  28. package/dist/compaction/observational/constants.d.ts.map +1 -0
  29. package/dist/compaction/observational/constants.js +507 -0
  30. package/dist/compaction/observational/constants.js.map +1 -0
  31. package/dist/compaction/observational/index.d.ts +219 -0
  32. package/dist/compaction/observational/index.d.ts.map +1 -0
  33. package/dist/compaction/observational/index.js +641 -0
  34. package/dist/compaction/observational/index.js.map +1 -0
  35. package/dist/compaction/observational/observer.d.ts +97 -0
  36. package/dist/compaction/observational/observer.d.ts.map +1 -0
  37. package/dist/compaction/observational/observer.js +424 -0
  38. package/dist/compaction/observational/observer.js.map +1 -0
  39. package/dist/compaction/observational/recall-tool.d.ts +27 -0
  40. package/dist/compaction/observational/recall-tool.d.ts.map +1 -0
  41. package/dist/compaction/observational/recall-tool.js +93 -0
  42. package/dist/compaction/observational/recall-tool.js.map +1 -0
  43. package/dist/compaction/observational/reflector.d.ts +94 -0
  44. package/dist/compaction/observational/reflector.d.ts.map +1 -0
  45. package/dist/compaction/observational/reflector.js +167 -0
  46. package/dist/compaction/observational/reflector.js.map +1 -0
  47. package/dist/compaction/observational/types.d.ts +271 -0
  48. package/dist/compaction/observational/types.d.ts.map +1 -0
  49. package/dist/compaction/observational/types.js +15 -0
  50. package/dist/compaction/observational/types.js.map +1 -0
  51. package/dist/context-manager.d.ts +134 -0
  52. package/dist/context-manager.d.ts.map +1 -0
  53. package/dist/context-manager.js +170 -0
  54. package/dist/context-manager.js.map +1 -0
  55. package/dist/cortex-agent.d.ts +1020 -0
  56. package/dist/cortex-agent.d.ts.map +1 -0
  57. package/dist/cortex-agent.js +3589 -0
  58. package/dist/cortex-agent.js.map +1 -0
  59. package/dist/error-classifier.d.ts +48 -0
  60. package/dist/error-classifier.d.ts.map +1 -0
  61. package/dist/error-classifier.js +152 -0
  62. package/dist/error-classifier.js.map +1 -0
  63. package/dist/event-bridge.d.ts +166 -0
  64. package/dist/event-bridge.d.ts.map +1 -0
  65. package/dist/event-bridge.js +381 -0
  66. package/dist/event-bridge.js.map +1 -0
  67. package/dist/index.d.ts +55 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/index.js +57 -0
  70. package/dist/index.js.map +1 -0
  71. package/dist/mcp-client.d.ts +119 -0
  72. package/dist/mcp-client.d.ts.map +1 -0
  73. package/dist/mcp-client.js +474 -0
  74. package/dist/mcp-client.js.map +1 -0
  75. package/dist/model-wrapper.d.ts +58 -0
  76. package/dist/model-wrapper.d.ts.map +1 -0
  77. package/dist/model-wrapper.js +86 -0
  78. package/dist/model-wrapper.js.map +1 -0
  79. package/dist/noop-logger.d.ts +4 -0
  80. package/dist/noop-logger.d.ts.map +1 -0
  81. package/dist/noop-logger.js +8 -0
  82. package/dist/noop-logger.js.map +1 -0
  83. package/dist/prompt-diagnostics.d.ts +47 -0
  84. package/dist/prompt-diagnostics.d.ts.map +1 -0
  85. package/dist/prompt-diagnostics.js +230 -0
  86. package/dist/prompt-diagnostics.js.map +1 -0
  87. package/dist/provider-manager.d.ts +224 -0
  88. package/dist/provider-manager.d.ts.map +1 -0
  89. package/dist/provider-manager.js +563 -0
  90. package/dist/provider-manager.js.map +1 -0
  91. package/dist/provider-registry.d.ts +115 -0
  92. package/dist/provider-registry.d.ts.map +1 -0
  93. package/dist/provider-registry.js +305 -0
  94. package/dist/provider-registry.js.map +1 -0
  95. package/dist/schema-converter.d.ts +20 -0
  96. package/dist/schema-converter.d.ts.map +1 -0
  97. package/dist/schema-converter.js +48 -0
  98. package/dist/schema-converter.js.map +1 -0
  99. package/dist/skill-preprocessor.d.ts +46 -0
  100. package/dist/skill-preprocessor.d.ts.map +1 -0
  101. package/dist/skill-preprocessor.js +237 -0
  102. package/dist/skill-preprocessor.js.map +1 -0
  103. package/dist/skill-registry.d.ts +107 -0
  104. package/dist/skill-registry.d.ts.map +1 -0
  105. package/dist/skill-registry.js +330 -0
  106. package/dist/skill-registry.js.map +1 -0
  107. package/dist/skill-tool.d.ts +54 -0
  108. package/dist/skill-tool.d.ts.map +1 -0
  109. package/dist/skill-tool.js +88 -0
  110. package/dist/skill-tool.js.map +1 -0
  111. package/dist/sub-agent-manager.d.ts +90 -0
  112. package/dist/sub-agent-manager.d.ts.map +1 -0
  113. package/dist/sub-agent-manager.js +192 -0
  114. package/dist/sub-agent-manager.js.map +1 -0
  115. package/dist/token-estimator.d.ts +23 -0
  116. package/dist/token-estimator.d.ts.map +1 -0
  117. package/dist/token-estimator.js +27 -0
  118. package/dist/token-estimator.js.map +1 -0
  119. package/dist/tool-contract.d.ts +68 -0
  120. package/dist/tool-contract.d.ts.map +1 -0
  121. package/dist/tool-contract.js +35 -0
  122. package/dist/tool-contract.js.map +1 -0
  123. package/dist/tool-result-persistence.d.ts +89 -0
  124. package/dist/tool-result-persistence.d.ts.map +1 -0
  125. package/dist/tool-result-persistence.js +152 -0
  126. package/dist/tool-result-persistence.js.map +1 -0
  127. package/dist/tools/bash/index.d.ts +71 -0
  128. package/dist/tools/bash/index.d.ts.map +1 -0
  129. package/dist/tools/bash/index.js +485 -0
  130. package/dist/tools/bash/index.js.map +1 -0
  131. package/dist/tools/bash/interactive.d.ts +47 -0
  132. package/dist/tools/bash/interactive.d.ts.map +1 -0
  133. package/dist/tools/bash/interactive.js +262 -0
  134. package/dist/tools/bash/interactive.js.map +1 -0
  135. package/dist/tools/bash/safety.d.ts +149 -0
  136. package/dist/tools/bash/safety.d.ts.map +1 -0
  137. package/dist/tools/bash/safety.js +1116 -0
  138. package/dist/tools/bash/safety.js.map +1 -0
  139. package/dist/tools/edit.d.ts +57 -0
  140. package/dist/tools/edit.d.ts.map +1 -0
  141. package/dist/tools/edit.js +310 -0
  142. package/dist/tools/edit.js.map +1 -0
  143. package/dist/tools/glob.d.ts +34 -0
  144. package/dist/tools/glob.d.ts.map +1 -0
  145. package/dist/tools/glob.js +268 -0
  146. package/dist/tools/glob.js.map +1 -0
  147. package/dist/tools/grep.d.ts +53 -0
  148. package/dist/tools/grep.d.ts.map +1 -0
  149. package/dist/tools/grep.js +673 -0
  150. package/dist/tools/grep.js.map +1 -0
  151. package/dist/tools/index.d.ts +62 -0
  152. package/dist/tools/index.d.ts.map +1 -0
  153. package/dist/tools/index.js +52 -0
  154. package/dist/tools/index.js.map +1 -0
  155. package/dist/tools/read.d.ts +43 -0
  156. package/dist/tools/read.d.ts.map +1 -0
  157. package/dist/tools/read.js +459 -0
  158. package/dist/tools/read.js.map +1 -0
  159. package/dist/tools/runtime.d.ts +62 -0
  160. package/dist/tools/runtime.d.ts.map +1 -0
  161. package/dist/tools/runtime.js +116 -0
  162. package/dist/tools/runtime.js.map +1 -0
  163. package/dist/tools/shared/cwd-tracker.d.ts +32 -0
  164. package/dist/tools/shared/cwd-tracker.d.ts.map +1 -0
  165. package/dist/tools/shared/cwd-tracker.js +44 -0
  166. package/dist/tools/shared/cwd-tracker.js.map +1 -0
  167. package/dist/tools/shared/edit-history.d.ts +55 -0
  168. package/dist/tools/shared/edit-history.d.ts.map +1 -0
  169. package/dist/tools/shared/edit-history.js +72 -0
  170. package/dist/tools/shared/edit-history.js.map +1 -0
  171. package/dist/tools/shared/edit-matcher.d.ts +83 -0
  172. package/dist/tools/shared/edit-matcher.d.ts.map +1 -0
  173. package/dist/tools/shared/edit-matcher.js +359 -0
  174. package/dist/tools/shared/edit-matcher.js.map +1 -0
  175. package/dist/tools/shared/file-mutation-lock.d.ts +22 -0
  176. package/dist/tools/shared/file-mutation-lock.d.ts.map +1 -0
  177. package/dist/tools/shared/file-mutation-lock.js +35 -0
  178. package/dist/tools/shared/file-mutation-lock.js.map +1 -0
  179. package/dist/tools/shared/gitignore.d.ts +17 -0
  180. package/dist/tools/shared/gitignore.d.ts.map +1 -0
  181. package/dist/tools/shared/gitignore.js +59 -0
  182. package/dist/tools/shared/gitignore.js.map +1 -0
  183. package/dist/tools/shared/pdf-extractor.d.ts +96 -0
  184. package/dist/tools/shared/pdf-extractor.d.ts.map +1 -0
  185. package/dist/tools/shared/pdf-extractor.js +196 -0
  186. package/dist/tools/shared/pdf-extractor.js.map +1 -0
  187. package/dist/tools/shared/read-registry.d.ts +66 -0
  188. package/dist/tools/shared/read-registry.d.ts.map +1 -0
  189. package/dist/tools/shared/read-registry.js +65 -0
  190. package/dist/tools/shared/read-registry.js.map +1 -0
  191. package/dist/tools/shared/safe-env.d.ts +18 -0
  192. package/dist/tools/shared/safe-env.d.ts.map +1 -0
  193. package/dist/tools/shared/safe-env.js +70 -0
  194. package/dist/tools/shared/safe-env.js.map +1 -0
  195. package/dist/tools/sub-agent.d.ts +91 -0
  196. package/dist/tools/sub-agent.d.ts.map +1 -0
  197. package/dist/tools/sub-agent.js +89 -0
  198. package/dist/tools/sub-agent.js.map +1 -0
  199. package/dist/tools/task-output.d.ts +38 -0
  200. package/dist/tools/task-output.d.ts.map +1 -0
  201. package/dist/tools/task-output.js +186 -0
  202. package/dist/tools/task-output.js.map +1 -0
  203. package/dist/tools/tool-search/index.d.ts +40 -0
  204. package/dist/tools/tool-search/index.d.ts.map +1 -0
  205. package/dist/tools/tool-search/index.js +110 -0
  206. package/dist/tools/tool-search/index.js.map +1 -0
  207. package/dist/tools/tool-search/registry.d.ts +82 -0
  208. package/dist/tools/tool-search/registry.d.ts.map +1 -0
  209. package/dist/tools/tool-search/registry.js +238 -0
  210. package/dist/tools/tool-search/registry.js.map +1 -0
  211. package/dist/tools/undo-edit.d.ts +51 -0
  212. package/dist/tools/undo-edit.d.ts.map +1 -0
  213. package/dist/tools/undo-edit.js +231 -0
  214. package/dist/tools/undo-edit.js.map +1 -0
  215. package/dist/tools/web-fetch/cache.d.ts +49 -0
  216. package/dist/tools/web-fetch/cache.d.ts.map +1 -0
  217. package/dist/tools/web-fetch/cache.js +89 -0
  218. package/dist/tools/web-fetch/cache.js.map +1 -0
  219. package/dist/tools/web-fetch/index.d.ts +53 -0
  220. package/dist/tools/web-fetch/index.d.ts.map +1 -0
  221. package/dist/tools/web-fetch/index.js +513 -0
  222. package/dist/tools/web-fetch/index.js.map +1 -0
  223. package/dist/tools/write.d.ts +59 -0
  224. package/dist/tools/write.d.ts.map +1 -0
  225. package/dist/tools/write.js +316 -0
  226. package/dist/tools/write.js.map +1 -0
  227. package/dist/types.d.ts +881 -0
  228. package/dist/types.d.ts.map +1 -0
  229. package/dist/types.js +16 -0
  230. package/dist/types.js.map +1 -0
  231. package/dist/working-tags.d.ts +44 -0
  232. package/dist/working-tags.d.ts.map +1 -0
  233. package/dist/working-tags.js +103 -0
  234. package/dist/working-tags.js.map +1 -0
  235. package/package.json +87 -0
  236. package/src/budget-guard.ts +170 -0
  237. package/src/compaction/compaction.ts +386 -0
  238. package/src/compaction/failsafe.ts +185 -0
  239. package/src/compaction/index.ts +1199 -0
  240. package/src/compaction/microcompaction.ts +709 -0
  241. package/src/compaction/observational/buffering.ts +430 -0
  242. package/src/compaction/observational/constants.ts +532 -0
  243. package/src/compaction/observational/index.ts +837 -0
  244. package/src/compaction/observational/observer.ts +510 -0
  245. package/src/compaction/observational/recall-tool.ts +130 -0
  246. package/src/compaction/observational/reflector.ts +221 -0
  247. package/src/compaction/observational/types.ts +343 -0
  248. package/src/context-manager.ts +237 -0
  249. package/src/cortex-agent.ts +4297 -0
  250. package/src/error-classifier.ts +199 -0
  251. package/src/event-bridge.ts +508 -0
  252. package/src/index.ts +292 -0
  253. package/src/mcp-client.ts +582 -0
  254. package/src/model-wrapper.ts +128 -0
  255. package/src/noop-logger.ts +9 -0
  256. package/src/prompt-diagnostics.ts +296 -0
  257. package/src/provider-manager.ts +823 -0
  258. package/src/provider-registry.ts +386 -0
  259. package/src/schema-converter.ts +51 -0
  260. package/src/skill-preprocessor.ts +314 -0
  261. package/src/skill-registry.ts +378 -0
  262. package/src/skill-tool.ts +130 -0
  263. package/src/sub-agent-manager.ts +236 -0
  264. package/src/token-estimator.ts +26 -0
  265. package/src/tool-contract.ts +113 -0
  266. package/src/tool-result-persistence.ts +197 -0
  267. package/src/tools/bash/index.ts +633 -0
  268. package/src/tools/bash/interactive.ts +302 -0
  269. package/src/tools/bash/safety.ts +1297 -0
  270. package/src/tools/edit.ts +422 -0
  271. package/src/tools/glob.ts +330 -0
  272. package/src/tools/grep.ts +819 -0
  273. package/src/tools/index.ts +110 -0
  274. package/src/tools/read.ts +580 -0
  275. package/src/tools/runtime.ts +173 -0
  276. package/src/tools/shared/cwd-tracker.ts +50 -0
  277. package/src/tools/shared/edit-history.ts +96 -0
  278. package/src/tools/shared/edit-matcher.ts +457 -0
  279. package/src/tools/shared/file-mutation-lock.ts +40 -0
  280. package/src/tools/shared/gitignore.ts +61 -0
  281. package/src/tools/shared/pdf-extractor.ts +290 -0
  282. package/src/tools/shared/read-registry.ts +93 -0
  283. package/src/tools/shared/safe-env.ts +82 -0
  284. package/src/tools/sub-agent.ts +171 -0
  285. package/src/tools/task-output.ts +236 -0
  286. package/src/tools/tool-search/index.ts +167 -0
  287. package/src/tools/tool-search/registry.ts +278 -0
  288. package/src/tools/undo-edit.ts +314 -0
  289. package/src/tools/web-fetch/cache.ts +112 -0
  290. package/src/tools/web-fetch/index.ts +604 -0
  291. package/src/tools/write.ts +385 -0
  292. package/src/types.ts +1057 -0
  293. package/src/working-tags.ts +118 -0
@@ -0,0 +1,110 @@
1
+ /**
2
+ * ToolSearch tool: load full tool schemas on demand.
3
+ *
4
+ * Auto-registered when `deferredTools.enabled` is true. The model uses this
5
+ * tool to discover and load tools that appear by name in the
6
+ * `_available_tools` slot but whose schemas are not yet in the agent's
7
+ * tools array.
8
+ *
9
+ * Once a tool is loaded, it persists in the agent's tools array for the
10
+ * rest of the session and can be called normally.
11
+ */
12
+ import { Type } from 'typebox';
13
+ // ---------------------------------------------------------------------------
14
+ // Constants
15
+ // ---------------------------------------------------------------------------
16
+ export const TOOL_SEARCH_TOOL_NAME = 'ToolSearch';
17
+ const DEFAULT_MAX_RESULTS = 5;
18
+ const QUERY_DESCRIPTION = [
19
+ 'Query for the tool(s) you want to load. Supported formats:',
20
+ '- "select:NameA,NameB" to load specific tools by exact name (preferred when you already know the names from the available-tools list)',
21
+ '- "exact_tool_name" to load a single tool by its exact name',
22
+ '- "prefix" to load all tools starting with the given prefix (e.g., "mcp__obsidian")',
23
+ '- "keyword another keyword" for keyword search across tool names and descriptions',
24
+ '- prefix any keyword with "+" to require it (e.g., "+slack send")',
25
+ ].join('\n');
26
+ // ---------------------------------------------------------------------------
27
+ // Schema
28
+ // ---------------------------------------------------------------------------
29
+ export const ToolSearchParams = Type.Object({
30
+ query: Type.String({ description: QUERY_DESCRIPTION }),
31
+ max_results: Type.Optional(Type.Number({
32
+ description: `Maximum number of tools to load when using keyword/prefix search. Default ${DEFAULT_MAX_RESULTS}. Ignored for select: queries (which always load every requested tool).`,
33
+ default: DEFAULT_MAX_RESULTS,
34
+ })),
35
+ });
36
+ export function createToolSearchTool(config) {
37
+ return {
38
+ name: TOOL_SEARCH_TOOL_NAME,
39
+ description: [
40
+ 'Load tool schemas on demand. Some tools are not loaded by default to save context tokens; their names appear in the "Available Tools" section but their parameters are not visible to you yet.',
41
+ '',
42
+ 'Call this tool to load specific tools before using them. Once loaded, tools become callable on the next turn.',
43
+ '',
44
+ 'Use a "select:Name1,Name2" query when you already know the tool names you need (most common). Use keyword queries when you need to discover tools by capability.',
45
+ ].join('\n'),
46
+ parameters: ToolSearchParams,
47
+ alwaysLoad: true, // ToolSearch itself must never be deferred
48
+ executionMode: 'sequential',
49
+ async execute(params) {
50
+ const max = params.max_results ?? DEFAULT_MAX_RESULTS;
51
+ const result = config.registry.resolveQuery(params.query, max);
52
+ const alreadyAvailable = result.resolved
53
+ .map((t) => t.name)
54
+ .filter((n) => !result.newlyDiscovered.includes(n));
55
+ const totalDeferred = config.registry.getUndiscoveredNames().length;
56
+ // Only trigger downstream refresh when something actually changed.
57
+ if (result.newlyDiscovered.length > 0) {
58
+ config.onAfterDiscovery();
59
+ }
60
+ const text = formatResultText(params.query, result, alreadyAvailable, totalDeferred);
61
+ return {
62
+ content: [{ type: 'text', text }],
63
+ details: {
64
+ query: params.query,
65
+ loaded: result.newlyDiscovered,
66
+ alreadyAvailable,
67
+ notFound: result.notFound,
68
+ totalDeferred,
69
+ },
70
+ };
71
+ },
72
+ };
73
+ }
74
+ // ---------------------------------------------------------------------------
75
+ // Result formatting
76
+ // ---------------------------------------------------------------------------
77
+ function formatResultText(query, result, alreadyAvailable, totalDeferredAfter) {
78
+ const lines = [];
79
+ if (result.newlyDiscovered.length > 0) {
80
+ lines.push('Loaded the following tools (callable on the next turn):');
81
+ for (const name of result.newlyDiscovered) {
82
+ lines.push(`- ${name}`);
83
+ }
84
+ }
85
+ if (alreadyAvailable.length > 0) {
86
+ if (lines.length > 0)
87
+ lines.push('');
88
+ lines.push('Already loaded (no action needed):');
89
+ for (const name of alreadyAvailable) {
90
+ lines.push(`- ${name}`);
91
+ }
92
+ }
93
+ if (result.notFound.length > 0) {
94
+ if (lines.length > 0)
95
+ lines.push('');
96
+ lines.push('Not found in the deferred tool list:');
97
+ for (const name of result.notFound) {
98
+ lines.push(`- ${name}`);
99
+ }
100
+ }
101
+ if (result.resolved.length === 0 && result.notFound.length === 0) {
102
+ lines.push(`No tools matched the query: "${query}"`);
103
+ if (totalDeferredAfter > 0) {
104
+ lines.push('');
105
+ lines.push(`There are ${totalDeferredAfter} tools still available. Refer to the "Available Tools" section for the full list.`);
106
+ }
107
+ }
108
+ return lines.join('\n');
109
+ }
110
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/tools/tool-search/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,IAAI,EAAe,MAAM,SAAS,CAAC;AAK5C,8EAA8E;AAC9E,YAAY;AACZ,8EAA8E;AAE9E,MAAM,CAAC,MAAM,qBAAqB,GAAG,YAAY,CAAC;AAElD,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAC9B,MAAM,iBAAiB,GAAG;IACxB,4DAA4D;IAC5D,uIAAuI;IACvI,6DAA6D;IAC7D,qFAAqF;IACrF,mFAAmF;IACnF,mEAAmE;CACpE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEb,8EAA8E;AAC9E,SAAS;AACT,8EAA8E;AAE9E,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAC,MAAM,CAAC;IAC1C,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,iBAAiB,EAAE,CAAC;IACtD,WAAW,EAAE,IAAI,CAAC,QAAQ,CACxB,IAAI,CAAC,MAAM,CAAC;QACV,WAAW,EAAE,6EAA6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K,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjE,KAAK,CAAC,IAAI,CAAC,gCAAgC,KAAK,GAAG,CAAC,CAAC;QACrD,IAAI,kBAAkB,GAAG,CAAC,EAAE,CAAC;YAC3B,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACf,KAAK,CAAC,IAAI,CAAC,aAAa,kBAAkB,mFAAmF,CAAC,CAAC;QACjI,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
@@ -0,0 +1,82 @@
1
+ /**
2
+ * DeferredToolRegistry: tracks the deferred tool pool and which tools the
3
+ * agent has discovered (loaded) during this session.
4
+ *
5
+ * Lives on the CortexAgent instance. `refreshTools()` populates the deferred
6
+ * pool from the union of registered + MCP tools (filtered by deferral
7
+ * criteria), and `ToolSearch` updates the discovered set when the agent
8
+ * resolves a query.
9
+ *
10
+ * The slot content (`formatSlotContent`) is the canonical text that goes
11
+ * into the `_available_tools` slot. It is byte-stable for any given pool +
12
+ * discovered set so the prompt cache hits cleanly.
13
+ */
14
+ import type { CortexTool } from '../../tool-contract.js';
15
+ /**
16
+ * Result of resolving a ToolSearch query.
17
+ */
18
+ export interface ToolSearchResult {
19
+ /** Tools that were resolved by the query (newly loaded + already loaded). */
20
+ resolved: CortexTool[];
21
+ /** Names of tools newly added to the discovered set by this query. */
22
+ newlyDiscovered: string[];
23
+ /** Names of tools the query referenced that were not found in the pool. */
24
+ notFound: string[];
25
+ }
26
+ export declare class DeferredToolRegistry {
27
+ /** Tools currently eligible for deferral, keyed by name. */
28
+ private deferredPool;
29
+ /** Names the model has loaded via ToolSearch. Persists for the session. */
30
+ private discovered;
31
+ /**
32
+ * Replace the deferred pool. Called by `refreshTools()` whenever the
33
+ * underlying tool set changes (MCP server connect/disconnect, etc).
34
+ *
35
+ * Tools that were previously discovered remain in the discovered set even
36
+ * if they're temporarily missing from the pool (e.g., MCP server briefly
37
+ * disconnected). The next `refreshTools()` call will see them again.
38
+ */
39
+ setDeferredPool(tools: readonly CortexTool[]): void;
40
+ /**
41
+ * Mark tool names as discovered. Subsequent `refreshTools()` calls will
42
+ * include their full schemas in the agent's tools array.
43
+ *
44
+ * Returns the subset that were newly added (i.e., were not already
45
+ * discovered). Used by ToolSearch to report what changed.
46
+ */
47
+ markDiscovered(names: readonly string[]): string[];
48
+ /**
49
+ * The set of tool names the agent has loaded so far.
50
+ */
51
+ getDiscovered(): ReadonlySet<string>;
52
+ /**
53
+ * Names currently in the deferred pool that have NOT yet been discovered.
54
+ * These are the names that should appear in the `_available_tools` slot.
55
+ * Returned sorted alphabetically for deterministic, cache-stable output.
56
+ */
57
+ getUndiscoveredNames(): string[];
58
+ /**
59
+ * Format the `_available_tools` slot content. Names only (no descriptions),
60
+ * sorted, in a fixed format. Returns an empty string when there are no
61
+ * undiscovered tools (the slot will be set to empty content, which still
62
+ * occupies a slot index but contributes no tokens).
63
+ */
64
+ formatSlotContent(): string;
65
+ /**
66
+ * Resolve a ToolSearch query against the deferred pool.
67
+ *
68
+ * Query formats:
69
+ * - "select:NameA,NameB" Direct load by name. Bypasses scoring.
70
+ * - "ExactToolName" Exact name match if it exists in the pool.
71
+ * - "prefix__" Returns all tools starting with the prefix.
72
+ * - "keyword another" Keyword search; scored by name + description.
73
+ *
74
+ * Discovered tools that are referenced by the query are still returned
75
+ * (harmless no-op so the model gets confirmation), but they don't count
76
+ * as "newly discovered".
77
+ */
78
+ resolveQuery(query: string, maxResults: number): ToolSearchResult;
79
+ private resolveByNames;
80
+ private scoreKeywordQuery;
81
+ }
82
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../../src/tools/tool-search/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAMzD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,6EAA6E;IAC7E,QAAQ,EAAE,UAAU,EAAE,CAAC;IACvB,sEAAsE;IACtE,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,2EAA2E;IAC3E,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAMD,qBAAa,oBAAoB;IAC/B,4DAA4D;IAC5D,OAAO,CAAC,YAAY,CAAiC;IACrD,2EAA2E;IAC3E,OAAO,CAAC,UAAU,CAAqB;IAEvC;;;;;;;OAOG;IACH,eAAe,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,GAAG,IAAI;IAOnD;;;;;;OAMG;IACH,cAAc,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE;IAWlD;;OAEG;IACH,aAAa,IAAI,WAAW,CAAC,MAAM,CAAC;IAIpC;;;;OAIG;IACH,oBAAoB,IAAI,MAAM,EAAE;IAUhC;;;;;OAKG;IACH,iBAAiB,IAAI,MAAM;IAkB3B;;;;;;;;;;;;OAYG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,gBAAgB;IAuDjE,OAAO,CAAC,cAAc;IAmBtB,OAAO,CAAC,iBAAiB;CAqE1B"}
@@ -0,0 +1,238 @@
1
+ /**
2
+ * DeferredToolRegistry: tracks the deferred tool pool and which tools the
3
+ * agent has discovered (loaded) during this session.
4
+ *
5
+ * Lives on the CortexAgent instance. `refreshTools()` populates the deferred
6
+ * pool from the union of registered + MCP tools (filtered by deferral
7
+ * criteria), and `ToolSearch` updates the discovered set when the agent
8
+ * resolves a query.
9
+ *
10
+ * The slot content (`formatSlotContent`) is the canonical text that goes
11
+ * into the `_available_tools` slot. It is byte-stable for any given pool +
12
+ * discovered set so the prompt cache hits cleanly.
13
+ */
14
+ // ---------------------------------------------------------------------------
15
+ // Registry
16
+ // ---------------------------------------------------------------------------
17
+ export class DeferredToolRegistry {
18
+ /** Tools currently eligible for deferral, keyed by name. */
19
+ deferredPool = new Map();
20
+ /** Names the model has loaded via ToolSearch. Persists for the session. */
21
+ discovered = new Set();
22
+ /**
23
+ * Replace the deferred pool. Called by `refreshTools()` whenever the
24
+ * underlying tool set changes (MCP server connect/disconnect, etc).
25
+ *
26
+ * Tools that were previously discovered remain in the discovered set even
27
+ * if they're temporarily missing from the pool (e.g., MCP server briefly
28
+ * disconnected). The next `refreshTools()` call will see them again.
29
+ */
30
+ setDeferredPool(tools) {
31
+ this.deferredPool.clear();
32
+ for (const tool of tools) {
33
+ this.deferredPool.set(tool.name, tool);
34
+ }
35
+ }
36
+ /**
37
+ * Mark tool names as discovered. Subsequent `refreshTools()` calls will
38
+ * include their full schemas in the agent's tools array.
39
+ *
40
+ * Returns the subset that were newly added (i.e., were not already
41
+ * discovered). Used by ToolSearch to report what changed.
42
+ */
43
+ markDiscovered(names) {
44
+ const added = [];
45
+ for (const name of names) {
46
+ if (!this.discovered.has(name)) {
47
+ this.discovered.add(name);
48
+ added.push(name);
49
+ }
50
+ }
51
+ return added;
52
+ }
53
+ /**
54
+ * The set of tool names the agent has loaded so far.
55
+ */
56
+ getDiscovered() {
57
+ return this.discovered;
58
+ }
59
+ /**
60
+ * Names currently in the deferred pool that have NOT yet been discovered.
61
+ * These are the names that should appear in the `_available_tools` slot.
62
+ * Returned sorted alphabetically for deterministic, cache-stable output.
63
+ */
64
+ getUndiscoveredNames() {
65
+ const names = [];
66
+ for (const name of this.deferredPool.keys()) {
67
+ if (!this.discovered.has(name)) {
68
+ names.push(name);
69
+ }
70
+ }
71
+ return names.sort();
72
+ }
73
+ /**
74
+ * Format the `_available_tools` slot content. Names only (no descriptions),
75
+ * sorted, in a fixed format. Returns an empty string when there are no
76
+ * undiscovered tools (the slot will be set to empty content, which still
77
+ * occupies a slot index but contributes no tokens).
78
+ */
79
+ formatSlotContent() {
80
+ const names = this.getUndiscoveredNames();
81
+ if (names.length === 0) {
82
+ return '';
83
+ }
84
+ const list = names.map((n) => `- ${n}`).join('\n');
85
+ return [
86
+ '# Available Tools',
87
+ '',
88
+ 'The following tools are available but their schemas have not been loaded.',
89
+ 'Use the ToolSearch tool to load a tool before calling it.',
90
+ '',
91
+ 'Calling any of these tools directly (without loading first) will fail.',
92
+ '',
93
+ list,
94
+ ].join('\n');
95
+ }
96
+ /**
97
+ * Resolve a ToolSearch query against the deferred pool.
98
+ *
99
+ * Query formats:
100
+ * - "select:NameA,NameB" Direct load by name. Bypasses scoring.
101
+ * - "ExactToolName" Exact name match if it exists in the pool.
102
+ * - "prefix__" Returns all tools starting with the prefix.
103
+ * - "keyword another" Keyword search; scored by name + description.
104
+ *
105
+ * Discovered tools that are referenced by the query are still returned
106
+ * (harmless no-op so the model gets confirmation), but they don't count
107
+ * as "newly discovered".
108
+ */
109
+ resolveQuery(query, maxResults) {
110
+ const trimmed = query.trim();
111
+ // Direct select format
112
+ if (trimmed.startsWith('select:')) {
113
+ const requested = trimmed
114
+ .slice('select:'.length)
115
+ .split(',')
116
+ .map((s) => s.trim())
117
+ .filter((s) => s.length > 0);
118
+ return this.resolveByNames(requested);
119
+ }
120
+ // Empty or whitespace-only query: return nothing useful
121
+ if (trimmed.length === 0) {
122
+ return { resolved: [], newlyDiscovered: [], notFound: [] };
123
+ }
124
+ // Exact name match (single token, exists in pool)
125
+ if (!/\s/.test(trimmed) && this.deferredPool.has(trimmed)) {
126
+ return this.resolveByNames([trimmed]);
127
+ }
128
+ // Prefix match (single token, no spaces, ends with __ or matches a prefix)
129
+ if (!/\s/.test(trimmed)) {
130
+ const prefixMatches = [];
131
+ for (const name of this.deferredPool.keys()) {
132
+ if (name.startsWith(trimmed)) {
133
+ prefixMatches.push(name);
134
+ }
135
+ }
136
+ if (prefixMatches.length > 0) {
137
+ prefixMatches.sort();
138
+ return this.resolveByNames(prefixMatches.slice(0, maxResults));
139
+ }
140
+ }
141
+ // Keyword search with scoring
142
+ const scored = this.scoreKeywordQuery(trimmed);
143
+ const top = scored
144
+ .sort((a, b) => b.score - a.score)
145
+ .slice(0, maxResults)
146
+ .filter((entry) => entry.score > 0)
147
+ .map((entry) => entry.name);
148
+ if (top.length === 0) {
149
+ return { resolved: [], newlyDiscovered: [], notFound: [] };
150
+ }
151
+ return this.resolveByNames(top);
152
+ }
153
+ // -----------------------------------------------------------------------
154
+ // Internal helpers
155
+ // -----------------------------------------------------------------------
156
+ resolveByNames(names) {
157
+ const resolved = [];
158
+ const notFound = [];
159
+ const toMarkDiscovered = [];
160
+ for (const name of names) {
161
+ const tool = this.deferredPool.get(name);
162
+ if (tool) {
163
+ resolved.push(tool);
164
+ toMarkDiscovered.push(name);
165
+ }
166
+ else {
167
+ notFound.push(name);
168
+ }
169
+ }
170
+ const newlyDiscovered = this.markDiscovered(toMarkDiscovered);
171
+ return { resolved, newlyDiscovered, notFound };
172
+ }
173
+ scoreKeywordQuery(query) {
174
+ // Split into terms; "+term" means required.
175
+ const tokens = query
176
+ .toLowerCase()
177
+ .split(/\s+/)
178
+ .filter((t) => t.length > 0);
179
+ const required = new Set();
180
+ const optional = [];
181
+ for (const token of tokens) {
182
+ if (token.startsWith('+') && token.length > 1) {
183
+ required.add(token.slice(1));
184
+ }
185
+ else {
186
+ optional.push(token);
187
+ }
188
+ }
189
+ const allTerms = [...required, ...optional];
190
+ if (allTerms.length === 0)
191
+ return [];
192
+ const results = [];
193
+ for (const [name, tool] of this.deferredPool.entries()) {
194
+ const lowerName = name.toLowerCase();
195
+ const lowerDesc = (tool.description ?? '').toLowerCase();
196
+ // Tool name parts: split by __ (MCP namespacing) and underscore for general matching
197
+ const nameParts = lowerName.split(/__|_/).filter((p) => p.length > 0);
198
+ // Required terms must all match somewhere
199
+ let satisfiesRequired = true;
200
+ for (const req of required) {
201
+ if (!lowerName.includes(req) && !lowerDesc.includes(req)) {
202
+ satisfiesRequired = false;
203
+ break;
204
+ }
205
+ }
206
+ if (!satisfiesRequired)
207
+ continue;
208
+ const isMcp = tool.isMcp === true;
209
+ let score = 0;
210
+ for (const term of allTerms) {
211
+ // Exact part match in name (highest weight)
212
+ if (nameParts.includes(term)) {
213
+ score += isMcp ? 12 : 10;
214
+ continue;
215
+ }
216
+ // Partial part match
217
+ if (nameParts.some((p) => p.includes(term))) {
218
+ score += isMcp ? 6 : 5;
219
+ continue;
220
+ }
221
+ // Full-name substring fallback
222
+ if (lowerName.includes(term)) {
223
+ score += 3;
224
+ continue;
225
+ }
226
+ // Description substring
227
+ if (lowerDesc.includes(term)) {
228
+ score += 2;
229
+ }
230
+ }
231
+ if (score > 0) {
232
+ results.push({ name, score });
233
+ }
234
+ }
235
+ return results;
236
+ }
237
+ }
238
+ //# sourceMappingURL=registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../../src/tools/tool-search/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAoBH,8EAA8E;AAC9E,WAAW;AACX,8EAA8E;AAE9E,MAAM,OAAO,oBAAoB;IAC/B,4DAA4D;IACpD,YAAY,GAAG,IAAI,GAAG,EAAsB,CAAC;IACrD,2EAA2E;IACnE,UAAU,GAAG,IAAI,GAAG,EAAU,CAAC;IAEvC;;;;;;;OAOG;IACH,eAAe,CAAC,KAA4B;QAC1C,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;QAC1B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACH,cAAc,CAAC,KAAwB;QACrC,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,CAA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AAK,IAAI,CAAC,CAAC;oBACX,SAAS;gBACX,CAAC;gBACD,wBAAwB;gBACxB,IAAI,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;oBAC7B,KAAK,IAAI,CAAC,CAAC;gBACb,CAAC;YACH,CAAC;YAED,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACd,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * UndoEdit tool: revert the most recent Edit or Write on a single file.
3
+ *
4
+ * Uses the per-runtime `EditHistory` stack: every successful Edit /
5
+ * Write push a pre-mutation snapshot, and `UndoEdit` pops the top
6
+ * snapshot to restore the prior state.
7
+ *
8
+ * The undo is guarded: we verify the current on-disk state still
9
+ * matches what we recorded at the moment the mutation completed (via
10
+ * mtime + SHA-256 hash). If anything has diverged since — another
11
+ * Edit/Write that wasn't history-tracked, a formatter, cloud sync —
12
+ * we refuse rather than overwrite unrelated changes.
13
+ *
14
+ * Reference: docs/cortex/tools/undo-edit.md
15
+ */
16
+ import { Type, type Static } from 'typebox';
17
+ import type { EditHistory } from './shared/edit-history.js';
18
+ import type { FileMutationLock } from './shared/file-mutation-lock.js';
19
+ import type { ReadRegistry } from './shared/read-registry.js';
20
+ import type { ToolContentDetails } from '../types.js';
21
+ import type { CortexToolRuntime } from './runtime.js';
22
+ export declare const UndoEditParams: Type.TObject<{
23
+ file_path: Type.TString;
24
+ }>;
25
+ export type UndoEditParamsType = Static<typeof UndoEditParams>;
26
+ export interface UndoEditDetails {
27
+ filePath: string;
28
+ /** The tool whose mutation was reverted (when the undo succeeds). */
29
+ revertedSource?: 'Edit' | 'Write';
30
+ /** True when the undo removed a file that Write had created. */
31
+ deleted?: boolean;
32
+ /** True when the undo restored prior content to an existing file. */
33
+ restored?: boolean;
34
+ /** True when the undo was rejected (no history, stale state, etc.). */
35
+ rejected?: boolean;
36
+ /** Remaining history depth for this file after the operation. */
37
+ remainingDepth?: number;
38
+ }
39
+ export interface UndoEditToolConfig {
40
+ runtime?: CortexToolRuntime | undefined;
41
+ editHistory?: EditHistory | undefined;
42
+ readRegistry?: ReadRegistry | undefined;
43
+ fileMutationLock?: FileMutationLock | undefined;
44
+ }
45
+ export declare function createUndoEditTool(config: UndoEditToolConfig): {
46
+ name: string;
47
+ description: string;
48
+ parameters: typeof UndoEditParams;
49
+ execute: (params: UndoEditParamsType) => Promise<ToolContentDetails<UndoEditDetails>>;
50
+ };
51
+ //# sourceMappingURL=undo-edit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"undo-edit.d.ts","sourceRoot":"","sources":["../../src/tools/undo-edit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,EAAE,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAoB,MAAM,0BAA0B,CAAC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AACvE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAOtD,eAAO,MAAM,cAAc;;EAIzB,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC,OAAO,cAAc,CAAC,CAAC;AAM/D,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,qEAAqE;IACrE,cAAc,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IAClC,gEAAgE;IAChE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,OAAO,CAAC;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