@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,70 @@
1
+ /**
2
+ * Shared environment sanitization for child processes.
3
+ *
4
+ * Used by both the Bash tool (safety.ts) and MCP client (mcp-client.ts)
5
+ * to strip dangerous environment variables before spawning subprocesses.
6
+ */
7
+ // ---------------------------------------------------------------------------
8
+ // Blocked variables
9
+ // ---------------------------------------------------------------------------
10
+ const BLOCKED_ENV_PREFIXES = ['LD_', 'DYLD_', 'BASH_FUNC_'];
11
+ const BLOCKED_ENV_VARS = new Set([
12
+ // Runtime loaders
13
+ 'NODE_OPTIONS', 'NODE_PATH',
14
+ 'PYTHONPATH', 'PYTHONHOME',
15
+ 'PERL5LIB', 'PERL5OPT',
16
+ 'RUBYLIB', 'RUBYOPT',
17
+ // Shell startup injection
18
+ 'BASH_ENV', 'ENV', 'SHELLOPTS', 'PS4', 'IFS', 'PROMPT_COMMAND', 'ZDOTDIR',
19
+ // Git execution
20
+ 'GIT_EXTERNAL_DIFF', 'GIT_EXEC_PATH', 'GIT_SSH_COMMAND',
21
+ // Security-sensitive
22
+ 'SSLKEYLOGFILE', 'GCONV_PATH', 'OPENSSL_CONF', 'CURL_HOME', 'WGETRC',
23
+ ]);
24
+ // ---------------------------------------------------------------------------
25
+ // Public API
26
+ // ---------------------------------------------------------------------------
27
+ /**
28
+ * Build a safe environment for child processes by stripping dangerous variables.
29
+ *
30
+ * @param parentEnv - The source environment (typically process.env or a consumer-supplied map)
31
+ * @param marker - Optional context marker added as CORTEX_SHELL. Pass undefined to skip.
32
+ * @param overrides - Optional key-value pairs merged ON TOP of the sanitized env, bypassing
33
+ * the blocklist. Used for consumer-set variables that must propagate (e.g., macOS dock
34
+ * icon suppression vars like DYLD_INSERT_LIBRARIES).
35
+ * @returns A new object with dangerous variables removed and overrides applied
36
+ */
37
+ export function buildSafeEnv(parentEnv, marker, overrides) {
38
+ const env = {};
39
+ for (const [key, value] of Object.entries(parentEnv)) {
40
+ if (value === undefined)
41
+ continue;
42
+ // Check exact match
43
+ if (BLOCKED_ENV_VARS.has(key))
44
+ continue;
45
+ // Check prefix match
46
+ let blocked = false;
47
+ for (const prefix of BLOCKED_ENV_PREFIXES) {
48
+ if (key.startsWith(prefix)) {
49
+ blocked = true;
50
+ break;
51
+ }
52
+ }
53
+ if (blocked)
54
+ continue;
55
+ env[key] = value;
56
+ }
57
+ if (marker !== undefined) {
58
+ env['CORTEX_SHELL'] = marker;
59
+ }
60
+ // Merge overrides ON TOP of the sanitized env, bypassing the blocklist.
61
+ // This allows consumers to restore specific blocked variables (e.g.,
62
+ // DYLD_INSERT_LIBRARIES for macOS dock icon suppression).
63
+ if (overrides) {
64
+ for (const [key, value] of Object.entries(overrides)) {
65
+ env[key] = value;
66
+ }
67
+ }
68
+ return env;
69
+ }
70
+ //# sourceMappingURL=safe-env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safe-env.js","sourceRoot":"","sources":["../../../src/tools/shared/safe-env.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,8EAA8E;AAC9E,oBAAoB;AACpB,8EAA8E;AAE9E,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;AAE5D,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC;IAC/B,kBAAkB;IAClB,cAAc,EAAE,WAAW;IAC3B,YAAY,EAAE,YAAY;IAC1B,UAAU,EAAE,UAAU;IACtB,SAAS,EAAE,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AE,CAAC;YACrD,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACnB,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * SubAgent tool: spawn independent cortex-based sub-agents for delegated work.
3
+ *
4
+ * Supports foreground (blocking) and background (async) execution modes.
5
+ * Each sub-agent is an independent CortexAgent with its own message array
6
+ * and empty context slots.
7
+ *
8
+ * The SubAgent tool is ALWAYS excluded from child agents to prevent
9
+ * recursive spawning.
10
+ *
11
+ * References:
12
+ * - docs/cortex/tools/sub-agent.md
13
+ * - docs/cortex/plans/phase-4-sub-agents-and-skills.md
14
+ */
15
+ import { Type, type Static } from 'typebox';
16
+ export declare const SubAgentParams: Type.TObject<{
17
+ instructions: Type.TString;
18
+ tools: Type.TOptional<Type.TArray<Type.TString>>;
19
+ systemPrompt: Type.TOptional<Type.TString>;
20
+ maxTurns: Type.TOptional<Type.TNumber>;
21
+ maxCost: Type.TOptional<Type.TNumber>;
22
+ background: Type.TOptional<Type.TBoolean>;
23
+ }>;
24
+ export type SubAgentParamsType = Static<typeof SubAgentParams>;
25
+ export interface SubAgentDetails {
26
+ taskId: string;
27
+ background: boolean;
28
+ status: string;
29
+ durationMs: number | null;
30
+ turns: number | null;
31
+ cost: number | null;
32
+ /** Model ID used by the sub-agent (inherited from parent). */
33
+ modelId?: string;
34
+ }
35
+ /**
36
+ * Configuration passed to the SubAgent tool factory.
37
+ * The CortexAgent provides all of these at tool registration time.
38
+ */
39
+ export interface SubAgentToolConfig {
40
+ /**
41
+ * Spawn a sub-agent and run it. Returns the result when complete.
42
+ * The factory function handles CortexAgent creation, budget guard
43
+ * inheritance, tool filtering, and lifecycle management.
44
+ */
45
+ spawnSubAgent: (params: SubAgentParamsType) => Promise<{
46
+ taskId: string;
47
+ output: string;
48
+ status: string;
49
+ usage: {
50
+ turns: number;
51
+ cost: number;
52
+ durationMs: number;
53
+ };
54
+ }>;
55
+ /**
56
+ * Spawn a background sub-agent. Returns the task ID immediately.
57
+ */
58
+ spawnBackgroundSubAgent: (params: SubAgentParamsType) => Promise<{
59
+ taskId: string;
60
+ }>;
61
+ /**
62
+ * Check if another sub-agent can be spawned.
63
+ */
64
+ canSpawn: () => boolean;
65
+ /**
66
+ * Get concurrency info for error messages.
67
+ */
68
+ getConcurrencyInfo: () => {
69
+ active: number;
70
+ limit: number;
71
+ };
72
+ /**
73
+ * Get the model ID for the child agent.
74
+ * Child agents inherit the parent's primary model.
75
+ */
76
+ getModelId: () => string;
77
+ }
78
+ export declare const SUB_AGENT_TOOL_NAME = "SubAgent";
79
+ /**
80
+ * Create the SubAgent tool.
81
+ *
82
+ * Returns a Cortex-native tool. CortexAgent adapts it to pi-agent-core's
83
+ * execute signature when synchronizing the tool inventory.
84
+ */
85
+ export declare function createSubAgentTool(config: SubAgentToolConfig): {
86
+ name: string;
87
+ description: string;
88
+ parameters: typeof SubAgentParams;
89
+ execute: (args: unknown) => Promise<unknown>;
90
+ };
91
+ //# sourceMappingURL=sub-agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sub-agent.d.ts","sourceRoot":"","sources":["../../src/tools/sub-agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,SAAS,CAAC;AAM5C,eAAO,MAAM,cAAc;;;;;;;EAmBzB,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC,OAAO,cAAc,CAAC,CAAC;AAM/D,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,OAAO,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,8DAA8D;IAC9D,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAMD;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,aAAa,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,OAAO,CAAC;QACrD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE;YAAE,KAAK,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAC;YAAC,UAAU,EAAE,MAAM,CAAA;SAAE,CAAC;KAC5D,CAAC,CAAC;IAEH;;OAEG;IACH,uBAAuB,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,OAAO,CAAC;QAC/D,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC,CAAC;IAEH;;OAEG;IACH,QAAQ,EAAE,MAAM,OAAO,CAAC;IAExB;;OAEG;IACH,kBAAkB,EAAE,MAAM;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAE5D;;;OAGG;IACH,UAAU,EAAE,MAAM,MAAM,CAAC;CAC1B;AAMD,eAAO,MAAM,mBAAmB,aAAa,CAAC;AAM9C;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,kBAAkB,GAAG;IAC9D,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,OAAO,cAAc,CAAC;IAClC,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9C,CA6CA"}
@@ -0,0 +1,89 @@
1
+ /**
2
+ * SubAgent tool: spawn independent cortex-based sub-agents for delegated work.
3
+ *
4
+ * Supports foreground (blocking) and background (async) execution modes.
5
+ * Each sub-agent is an independent CortexAgent with its own message array
6
+ * and empty context slots.
7
+ *
8
+ * The SubAgent tool is ALWAYS excluded from child agents to prevent
9
+ * recursive spawning.
10
+ *
11
+ * References:
12
+ * - docs/cortex/tools/sub-agent.md
13
+ * - docs/cortex/plans/phase-4-sub-agents-and-skills.md
14
+ */
15
+ import { Type } from 'typebox';
16
+ // ---------------------------------------------------------------------------
17
+ // Schema
18
+ // ---------------------------------------------------------------------------
19
+ export const SubAgentParams = Type.Object({
20
+ instructions: Type.String({
21
+ description: 'What the sub-agent should do. This becomes the sub-agent\'s initial prompt.',
22
+ }),
23
+ tools: Type.Optional(Type.Array(Type.String(), {
24
+ description: 'Tool names to make available. Default: inherits parent\'s registered tools.',
25
+ })),
26
+ systemPrompt: Type.Optional(Type.String({
27
+ description: 'Custom system prompt. Default: inherits parent\'s full system prompt.',
28
+ })),
29
+ maxTurns: Type.Optional(Type.Number({
30
+ description: 'Maximum LLM turns. Default: inherits parent\'s budget guard config.',
31
+ })),
32
+ maxCost: Type.Optional(Type.Number({
33
+ description: 'Maximum cost in USD. Default: inherits parent\'s budget guard config.',
34
+ })),
35
+ background: Type.Optional(Type.Boolean({
36
+ description: 'Run asynchronously. Default: false (blocks until complete).',
37
+ })),
38
+ });
39
+ // ---------------------------------------------------------------------------
40
+ // Tool name constant
41
+ // ---------------------------------------------------------------------------
42
+ export const SUB_AGENT_TOOL_NAME = 'SubAgent';
43
+ // ---------------------------------------------------------------------------
44
+ // Factory
45
+ // ---------------------------------------------------------------------------
46
+ /**
47
+ * Create the SubAgent tool.
48
+ *
49
+ * Returns a Cortex-native tool. CortexAgent adapts it to pi-agent-core's
50
+ * execute signature when synchronizing the tool inventory.
51
+ */
52
+ export function createSubAgentTool(config) {
53
+ return {
54
+ name: SUB_AGENT_TOOL_NAME,
55
+ description: `Spawn a sub-agent to handle a delegated task independently. Use for tasks that are complex, long-running, or can proceed in parallel with your main work.
56
+
57
+ Foreground mode (default): Blocks until the sub-agent completes and returns its result directly. Use for quick, focused tasks where you need the result to continue.
58
+
59
+ Background mode (background: true): Returns a task ID immediately. The sub-agent runs independently. You will be notified when it completes. Use for long-running research, analysis, or multi-step work.
60
+
61
+ Sub-agents are independent: they have their own conversation, do not share your context, and cannot spawn further sub-agents. Give them clear, self-contained instructions.`,
62
+ parameters: SubAgentParams,
63
+ execute: async (args) => {
64
+ const params = args;
65
+ // Check concurrency limit
66
+ if (!config.canSpawn()) {
67
+ const info = config.getConcurrencyInfo();
68
+ return `Cannot spawn sub-agent: concurrency limit reached (${info.active}/${info.limit} active). Wait for a running sub-agent to complete or cancel one to free a slot.`;
69
+ }
70
+ // Background mode: spawn and return immediately
71
+ if (params.background) {
72
+ const { taskId } = await config.spawnBackgroundSubAgent(params);
73
+ return `Sub-agent spawned in background. Task ID: ${taskId}\nYou will be notified when it completes. Continue with other work.`;
74
+ }
75
+ // Foreground mode: block until complete
76
+ const result = await config.spawnSubAgent(params);
77
+ // Format result for the parent agent
78
+ const statusLine = result.status === 'completed'
79
+ ? 'Sub-agent completed successfully.'
80
+ : `Sub-agent finished with status: ${result.status}`;
81
+ const usageLine = `(${result.usage.turns} turns, $${result.usage.cost.toFixed(4)}, ${(result.usage.durationMs / 1000).toFixed(1)}s)`;
82
+ if (result.output) {
83
+ return `${statusLine} ${usageLine}\n\n${result.output}`;
84
+ }
85
+ return `${statusLine} ${usageLine}\n\nNo output was produced.`;
86
+ },
87
+ };
88
+ }
89
+ //# sourceMappingURL=sub-agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sub-agent.js","sourceRoot":"","sources":["../../src/tools/sub-agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,IAAI,EAAe,MAAM,SAAS,CAAC;AAE5C,8EAA8E;AAC9E,SAAS;AACT,8EAA8E;AAE9E,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC;IACxC,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC;QACxB,WAAW,EAAE,6EAA6E;KAC3F,CAAC;IACF,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAA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AM,EAAE,GAAG,MAAM,MAAM,CAAC,uBAAuB,CAAC,MAAM,CAAC,CAAC;gBAChE,OAAO,6CAA6C,MAAM,qEAAqE,CAAC;YAClI,CAAC;YAED,wCAAwC;YACxC,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;YAElD,qCAAqC;YACrC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,KAAK,WAAW;gBAC9C,CAAC,CAAC,mCAAmC;gBACrC,CAAC,CAAC,mCAAmC,MAAM,CAAC,MAAM,EAAE,CAAC;YAEvD,MAAM,SAAS,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,YAAY,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;YAErI,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;gBAClB,OAAO,GAAG,UAAU,IAAI,SAAS,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC;YAC1D,CAAC;YAED,OAAO,GAAG,UAAU,IAAI,SAAS,6BAA6B,CAAC;QACjE,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * TaskOutput tool: companion tool for polling backgrounded processes.
3
+ *
4
+ * Auto-registered alongside the Bash tool. Provides three actions:
5
+ * - poll: get latest output and status
6
+ * - send: send input to process stdin
7
+ * - kill: send a signal to the process
8
+ *
9
+ * Reference: docs/cortex/tools/bash.md (Background Execution)
10
+ */
11
+ import { Type, type Static } from 'typebox';
12
+ import type { ToolContentDetails } from '../types.js';
13
+ import type { CortexToolRuntime } from './runtime.js';
14
+ export declare const TaskOutputParams: Type.TObject<{
15
+ task_id: Type.TString;
16
+ action: Type.TUnion<[Type.TLiteral<"poll">, Type.TLiteral<"send">, Type.TLiteral<"kill">]>;
17
+ input: Type.TOptional<Type.TString>;
18
+ signal: Type.TOptional<Type.TString>;
19
+ }>;
20
+ export type TaskOutputParamsType = Static<typeof TaskOutputParams>;
21
+ export interface TaskOutputDetails {
22
+ taskId: string;
23
+ action: string;
24
+ status: 'running' | 'completed' | 'failed' | 'not_found';
25
+ exitCode: number | null;
26
+ stdout: string;
27
+ stderr: string;
28
+ }
29
+ export interface TaskOutputToolConfig {
30
+ runtime?: CortexToolRuntime | undefined;
31
+ }
32
+ export declare function createTaskOutputTool(config?: TaskOutputToolConfig): {
33
+ name: string;
34
+ description: string;
35
+ parameters: typeof TaskOutputParams;
36
+ execute: (params: TaskOutputParamsType) => Promise<ToolContentDetails<TaskOutputDetails>>;
37
+ };
38
+ //# sourceMappingURL=task-output.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task-output.d.ts","sourceRoot":"","sources":["../../src/tools/task-output.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAUtD,eAAO,MAAM,gBAAgB;;;;;EAa3B,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAMnE,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,SAAS,GAAG,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;IACzD,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB;AAMD,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAC;CACzC;AAED,wBAAgB,oBAAoB,CAAC,MAAM,CAAC,EAAE,oBAAoB,GAAG;IACnE,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,OAAO,gBAAgB,CAAC;IACpC,OAAO,EAAE,CAAC,MAAM,EAAE,oBAAoB,KAAK,OAAO,CAAC,kBAAkB,CAAC,iBAAiB,CAAC,CAAC,CAAC;CAC3F,CAyKA"}
@@ -0,0 +1,186 @@
1
+ /**
2
+ * TaskOutput tool: companion tool for polling backgrounded processes.
3
+ *
4
+ * Auto-registered alongside the Bash tool. Provides three actions:
5
+ * - poll: get latest output and status
6
+ * - send: send input to process stdin
7
+ * - kill: send a signal to the process
8
+ *
9
+ * Reference: docs/cortex/tools/bash.md (Background Execution)
10
+ */
11
+ import { Type } from 'typebox';
12
+ import { attachRuntimeAwareTool, globalBackgroundTaskStore, } from './runtime.js';
13
+ // ---------------------------------------------------------------------------
14
+ // Schema
15
+ // ---------------------------------------------------------------------------
16
+ export const TaskOutputParams = Type.Object({
17
+ task_id: Type.String({ description: 'The task ID returned by a backgrounded Bash command' }),
18
+ action: Type.Union([
19
+ Type.Literal('poll'),
20
+ Type.Literal('send'),
21
+ Type.Literal('kill'),
22
+ ], { description: 'Action to perform: poll (get output), send (send input), or kill (terminate)' }),
23
+ input: Type.Optional(Type.String({ description: 'Input to send to the process stdin (only for "send" action)' })),
24
+ signal: Type.Optional(Type.String({ description: 'Signal to send (only for "kill" action). Default: SIGTERM. Options: SIGINT, SIGTERM, SIGKILL' })),
25
+ });
26
+ export function createTaskOutputTool(config) {
27
+ const backgroundTasks = config?.runtime?.backgroundTasks ?? globalBackgroundTaskStore;
28
+ const tool = {
29
+ name: 'TaskOutput',
30
+ description: 'Poll, send input to, or kill a backgrounded process.',
31
+ parameters: TaskOutputParams,
32
+ async execute(params) {
33
+ const { task_id: taskId, action } = params;
34
+ const task = backgroundTasks.get(taskId);
35
+ if (!task) {
36
+ return {
37
+ content: [{ type: 'text', text: `Task not found: ${taskId}` }],
38
+ details: {
39
+ taskId,
40
+ action,
41
+ status: 'not_found',
42
+ exitCode: null,
43
+ stdout: '',
44
+ stderr: '',
45
+ },
46
+ };
47
+ }
48
+ switch (action) {
49
+ case 'poll': {
50
+ const status = task.completed
51
+ ? (task.exitCode === 0 ? 'completed' : 'failed')
52
+ : 'running';
53
+ let text = `Status: ${status}`;
54
+ if (task.completed && task.exitCode !== null) {
55
+ text += ` (exit code: ${task.exitCode})`;
56
+ }
57
+ if (task.stdout) {
58
+ const output = task.stdout.length > 30000
59
+ ? task.stdout.slice(-30000)
60
+ : task.stdout;
61
+ text += `\n\nOutput:\n${output}`;
62
+ }
63
+ if (task.stderr) {
64
+ text += `\n\nStderr:\n${task.stderr}`;
65
+ }
66
+ return {
67
+ content: [{ type: 'text', text }],
68
+ details: {
69
+ taskId,
70
+ action,
71
+ status: status,
72
+ exitCode: task.exitCode,
73
+ stdout: task.stdout,
74
+ stderr: task.stderr,
75
+ },
76
+ };
77
+ }
78
+ case 'send': {
79
+ if (task.completed) {
80
+ return {
81
+ content: [{ type: 'text', text: `Task ${taskId} has already completed. Cannot send input.` }],
82
+ details: {
83
+ taskId,
84
+ action,
85
+ status: task.exitCode === 0 ? 'completed' : 'failed',
86
+ exitCode: task.exitCode,
87
+ stdout: task.stdout,
88
+ stderr: task.stderr,
89
+ },
90
+ };
91
+ }
92
+ const input = params.input ?? '';
93
+ try {
94
+ task.process.stdin?.write(input + '\n');
95
+ return {
96
+ content: [{ type: 'text', text: `Sent input to task ${taskId}.` }],
97
+ details: {
98
+ taskId,
99
+ action,
100
+ status: 'running',
101
+ exitCode: null,
102
+ stdout: task.stdout,
103
+ stderr: task.stderr,
104
+ },
105
+ };
106
+ }
107
+ catch (err) {
108
+ const msg = err instanceof Error ? err.message : String(err);
109
+ return {
110
+ content: [{ type: 'text', text: `Failed to send input to task ${taskId}: ${msg}` }],
111
+ details: {
112
+ taskId,
113
+ action,
114
+ status: 'running',
115
+ exitCode: null,
116
+ stdout: task.stdout,
117
+ stderr: task.stderr,
118
+ },
119
+ };
120
+ }
121
+ }
122
+ case 'kill': {
123
+ if (task.completed) {
124
+ return {
125
+ content: [{ type: 'text', text: `Task ${taskId} has already completed.` }],
126
+ details: {
127
+ taskId,
128
+ action,
129
+ status: task.exitCode === 0 ? 'completed' : 'failed',
130
+ exitCode: task.exitCode,
131
+ stdout: task.stdout,
132
+ stderr: task.stderr,
133
+ },
134
+ };
135
+ }
136
+ const signal = (params.signal ?? 'SIGTERM');
137
+ try {
138
+ task.process.kill(signal);
139
+ return {
140
+ content: [{ type: 'text', text: `Sent ${signal} to task ${taskId}.` }],
141
+ details: {
142
+ taskId,
143
+ action,
144
+ status: 'running',
145
+ exitCode: null,
146
+ stdout: task.stdout,
147
+ stderr: task.stderr,
148
+ },
149
+ };
150
+ }
151
+ catch (err) {
152
+ const msg = err instanceof Error ? err.message : String(err);
153
+ return {
154
+ content: [{ type: 'text', text: `Failed to kill task ${taskId}: ${msg}` }],
155
+ details: {
156
+ taskId,
157
+ action,
158
+ status: 'running',
159
+ exitCode: null,
160
+ stdout: task.stdout,
161
+ stderr: task.stderr,
162
+ },
163
+ };
164
+ }
165
+ }
166
+ default:
167
+ return {
168
+ content: [{ type: 'text', text: `Unknown action: ${action}` }],
169
+ details: {
170
+ taskId,
171
+ action,
172
+ status: 'not_found',
173
+ exitCode: null,
174
+ stdout: '',
175
+ stderr: '',
176
+ },
177
+ };
178
+ }
179
+ },
180
+ };
181
+ return attachRuntimeAwareTool(tool, {
182
+ toolKind: 'TaskOutput',
183
+ cloneForRuntime: (runtime) => createTaskOutputTool({ runtime }),
184
+ });
185
+ }
186
+ //# sourceMappingURL=task-output.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task-output.js","sourceRoot":"","sources":["../../src/tools/task-output.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,IAAI,EAAe,MAAM,SAAS,CAAC;AAG5C,OAAO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MAAM,EAAE,IAAI,CAAC,MAAM;6BACpB;yBACF,CAAC;oBACJ,CAAC;oBAAC,OAAO,GAAG,EAAE,CAAC;wBACb,MAAM,GAAG,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;wBAC7D,OAAO;4BACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,uBAAuB,MAAM,KAAK,GAAG,EAAE,EAAE,CAAC;4BAC1E,OAAO,EAAE;gCACP,MAAM;gCACN,MAAM;gCACN,MAAM,EAAE,SAAS;gCACjB,QAAQ,EAAE,IAAI;gCACd,MAAM,EAAE,IAAI,CAAC,MAAM;gCACnB,MAAM,EAAE,IAAI,CAAC,MAAM;6BACpB;yBACF,CAAC;oBACJ,CAAC;gBACH,CAAC;gBAED;oBACE,OAAO;wBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,mBAAmB,MAAM,EAAE,EAAE,CAAC;wBAC9D,OAAO,EAAE;4BACP,MAAM;4BACN,MAAM;4BACN,MAAM,EAAE,WAAW;4BACnB,QAAQ,EAAE,IAAI;4BACd,MAAM,EAAE,EAAE;4BACV,MAAM,EAAE,EAAE;yBACX;qBACF,CAAC;YACN,CAAC;QACH,CAAC;KACF,CAAC;IAEF,OAAO,sBAAsB,CAAC,IAAI,EAAE;QAClC,QAAQ,EAAE,YAAY;QACtB,eAAe,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,oBAAoB,CAAC,EAAE,OAAO,EAAE,CAAC;KAChE,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,40 @@
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, type Static } from 'typebox';
13
+ import type { CortexTool } from '../../tool-contract.js';
14
+ import type { ToolContentDetails } from '../../types.js';
15
+ import type { DeferredToolRegistry } from './registry.js';
16
+ export declare const TOOL_SEARCH_TOOL_NAME = "ToolSearch";
17
+ export declare const ToolSearchParams: Type.TObject<{
18
+ query: Type.TString;
19
+ max_results: Type.TOptional<Type.TNumber>;
20
+ }>;
21
+ export type ToolSearchParamsType = Static<typeof ToolSearchParams>;
22
+ export interface ToolSearchDetails {
23
+ query: string;
24
+ loaded: string[];
25
+ alreadyAvailable: string[];
26
+ notFound: string[];
27
+ totalDeferred: number;
28
+ }
29
+ export interface ToolSearchToolConfig {
30
+ /** The deferred tool registry shared with CortexAgent. */
31
+ registry: DeferredToolRegistry;
32
+ /**
33
+ * Called after the registry is updated. The agent uses this to refresh
34
+ * its tools array (so the newly discovered tools appear in the next API
35
+ * call) and update the `_available_tools` slot.
36
+ */
37
+ onAfterDiscovery: () => void;
38
+ }
39
+ export declare function createToolSearchTool(config: ToolSearchToolConfig): CortexTool<ToolSearchParamsType, ToolContentDetails<ToolSearchDetails>>;
40
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/tools/tool-search/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,KAAK,EAAE,oBAAoB,EAAoB,MAAM,eAAe,CAAC;AAM5E,eAAO,MAAM,qBAAqB,eAAe,CAAC;AAgBlD,eAAO,MAAM,gBAAgB;;;EAQ3B,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAMnE,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC3B,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;CACvB;AAMD,MAAM,WAAW,oBAAoB;IACnC,0DAA0D;IAC1D,QAAQ,EAAE,oBAAoB,CAAC;IAC/B;;;;OAIG;IACH,gBAAgB,EAAE,MAAM,IAAI,CAAC;CAC9B;AAED,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,oBAAoB,GAC3B,UAAU,CAAC,oBAAoB,EAAE,kBAAkB,CAAC,iBAAiB,CAAC,CAAC,CA0CzE"}