@mohanscodex/spectra-code 0.4.5

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 (319) hide show
  1. package/README.md +275 -0
  2. package/dist/package.json +62 -0
  3. package/dist/src/agents/definitions.d.ts +16 -0
  4. package/dist/src/agents/definitions.d.ts.map +1 -0
  5. package/dist/src/agents/definitions.js +148 -0
  6. package/dist/src/agents/definitions.js.map +1 -0
  7. package/dist/src/agents/registry.d.ts +11 -0
  8. package/dist/src/agents/registry.d.ts.map +1 -0
  9. package/dist/src/agents/registry.js +13 -0
  10. package/dist/src/agents/registry.js.map +1 -0
  11. package/dist/src/cli.d.ts +3 -0
  12. package/dist/src/cli.d.ts.map +1 -0
  13. package/dist/src/cli.js +436 -0
  14. package/dist/src/cli.js.map +1 -0
  15. package/dist/src/commands/agent.d.ts +3 -0
  16. package/dist/src/commands/agent.d.ts.map +1 -0
  17. package/dist/src/commands/agent.js +28 -0
  18. package/dist/src/commands/agent.js.map +1 -0
  19. package/dist/src/commands/db.d.ts +3 -0
  20. package/dist/src/commands/db.d.ts.map +1 -0
  21. package/dist/src/commands/db.js +47 -0
  22. package/dist/src/commands/db.js.map +1 -0
  23. package/dist/src/commands/doctor.d.ts +12 -0
  24. package/dist/src/commands/doctor.d.ts.map +1 -0
  25. package/dist/src/commands/doctor.js +59 -0
  26. package/dist/src/commands/doctor.js.map +1 -0
  27. package/dist/src/commands/mcp.d.ts +9 -0
  28. package/dist/src/commands/mcp.d.ts.map +1 -0
  29. package/dist/src/commands/mcp.js +208 -0
  30. package/dist/src/commands/mcp.js.map +1 -0
  31. package/dist/src/commands/plugin.d.ts +3 -0
  32. package/dist/src/commands/plugin.d.ts.map +1 -0
  33. package/dist/src/commands/plugin.js +36 -0
  34. package/dist/src/commands/plugin.js.map +1 -0
  35. package/dist/src/commands/session.d.ts +3 -0
  36. package/dist/src/commands/session.d.ts.map +1 -0
  37. package/dist/src/commands/session.js +49 -0
  38. package/dist/src/commands/session.js.map +1 -0
  39. package/dist/src/index.d.ts +19 -0
  40. package/dist/src/index.d.ts.map +1 -0
  41. package/dist/src/index.js +17 -0
  42. package/dist/src/index.js.map +1 -0
  43. package/dist/src/integrations/acp/index.d.ts +2 -0
  44. package/dist/src/integrations/acp/index.d.ts.map +1 -0
  45. package/dist/src/integrations/acp/index.js +2 -0
  46. package/dist/src/integrations/acp/index.js.map +1 -0
  47. package/dist/src/integrations/acp/server.d.ts +11 -0
  48. package/dist/src/integrations/acp/server.d.ts.map +1 -0
  49. package/dist/src/integrations/acp/server.js +220 -0
  50. package/dist/src/integrations/acp/server.js.map +1 -0
  51. package/dist/src/integrations/custom-tools/index.d.ts +3 -0
  52. package/dist/src/integrations/custom-tools/index.d.ts.map +1 -0
  53. package/dist/src/integrations/custom-tools/index.js +2 -0
  54. package/dist/src/integrations/custom-tools/index.js.map +1 -0
  55. package/dist/src/integrations/custom-tools/loader.d.ts +16 -0
  56. package/dist/src/integrations/custom-tools/loader.d.ts.map +1 -0
  57. package/dist/src/integrations/custom-tools/loader.js +95 -0
  58. package/dist/src/integrations/custom-tools/loader.js.map +1 -0
  59. package/dist/src/integrations/mcp/client.d.ts +32 -0
  60. package/dist/src/integrations/mcp/client.d.ts.map +1 -0
  61. package/dist/src/integrations/mcp/client.js +117 -0
  62. package/dist/src/integrations/mcp/client.js.map +1 -0
  63. package/dist/src/integrations/mcp/index.d.ts +3 -0
  64. package/dist/src/integrations/mcp/index.d.ts.map +1 -0
  65. package/dist/src/integrations/mcp/index.js +2 -0
  66. package/dist/src/integrations/mcp/index.js.map +1 -0
  67. package/dist/src/security/doom-loop.d.ts +22 -0
  68. package/dist/src/security/doom-loop.d.ts.map +1 -0
  69. package/dist/src/security/doom-loop.js +72 -0
  70. package/dist/src/security/doom-loop.js.map +1 -0
  71. package/dist/src/security/index.d.ts +46 -0
  72. package/dist/src/security/index.d.ts.map +1 -0
  73. package/dist/src/security/index.js +362 -0
  74. package/dist/src/security/index.js.map +1 -0
  75. package/dist/src/security/path-safety.d.ts +15 -0
  76. package/dist/src/security/path-safety.d.ts.map +1 -0
  77. package/dist/src/security/path-safety.js +57 -0
  78. package/dist/src/security/path-safety.js.map +1 -0
  79. package/dist/src/security/permissions.d.ts +7 -0
  80. package/dist/src/security/permissions.d.ts.map +1 -0
  81. package/dist/src/security/permissions.js +41 -0
  82. package/dist/src/security/permissions.js.map +1 -0
  83. package/dist/src/security/read-tracker.d.ts +20 -0
  84. package/dist/src/security/read-tracker.d.ts.map +1 -0
  85. package/dist/src/security/read-tracker.js +73 -0
  86. package/dist/src/security/read-tracker.js.map +1 -0
  87. package/dist/src/security/ssrf-guard.d.ts +19 -0
  88. package/dist/src/security/ssrf-guard.d.ts.map +1 -0
  89. package/dist/src/security/ssrf-guard.js +98 -0
  90. package/dist/src/security/ssrf-guard.js.map +1 -0
  91. package/dist/src/security/types.d.ts +70 -0
  92. package/dist/src/security/types.d.ts.map +1 -0
  93. package/dist/src/security/types.js +2 -0
  94. package/dist/src/security/types.js.map +1 -0
  95. package/dist/src/security/wildcard.d.ts +5 -0
  96. package/dist/src/security/wildcard.d.ts.map +1 -0
  97. package/dist/src/security/wildcard.js +50 -0
  98. package/dist/src/security/wildcard.js.map +1 -0
  99. package/dist/src/services/auth-store.d.ts +25 -0
  100. package/dist/src/services/auth-store.d.ts.map +1 -0
  101. package/dist/src/services/auth-store.js +40 -0
  102. package/dist/src/services/auth-store.js.map +1 -0
  103. package/dist/src/services/config.d.ts +66 -0
  104. package/dist/src/services/config.d.ts.map +1 -0
  105. package/dist/src/services/config.js +83 -0
  106. package/dist/src/services/config.js.map +1 -0
  107. package/dist/src/services/context.d.ts +7 -0
  108. package/dist/src/services/context.d.ts.map +1 -0
  109. package/dist/src/services/context.js +23 -0
  110. package/dist/src/services/context.js.map +1 -0
  111. package/dist/src/services/custom-providers.d.ts +5 -0
  112. package/dist/src/services/custom-providers.d.ts.map +1 -0
  113. package/dist/src/services/custom-providers.js +213 -0
  114. package/dist/src/services/custom-providers.js.map +1 -0
  115. package/dist/src/services/session-store.d.ts +76 -0
  116. package/dist/src/services/session-store.d.ts.map +1 -0
  117. package/dist/src/services/session-store.js +199 -0
  118. package/dist/src/services/session-store.js.map +1 -0
  119. package/dist/src/services/snapshot-manager.d.ts +46 -0
  120. package/dist/src/services/snapshot-manager.d.ts.map +1 -0
  121. package/dist/src/services/snapshot-manager.js +183 -0
  122. package/dist/src/services/snapshot-manager.js.map +1 -0
  123. package/dist/src/tools/edit.d.ts +3 -0
  124. package/dist/src/tools/edit.d.ts.map +1 -0
  125. package/dist/src/tools/edit.js +57 -0
  126. package/dist/src/tools/edit.js.map +1 -0
  127. package/dist/src/tools/glob.d.ts +3 -0
  128. package/dist/src/tools/glob.d.ts.map +1 -0
  129. package/dist/src/tools/glob.js +46 -0
  130. package/dist/src/tools/glob.js.map +1 -0
  131. package/dist/src/tools/grep.d.ts +3 -0
  132. package/dist/src/tools/grep.d.ts.map +1 -0
  133. package/dist/src/tools/grep.js +50 -0
  134. package/dist/src/tools/grep.js.map +1 -0
  135. package/dist/src/tools/index.d.ts +26 -0
  136. package/dist/src/tools/index.d.ts.map +1 -0
  137. package/dist/src/tools/index.js +202 -0
  138. package/dist/src/tools/index.js.map +1 -0
  139. package/dist/src/tools/mcp-tool.d.ts +5 -0
  140. package/dist/src/tools/mcp-tool.d.ts.map +1 -0
  141. package/dist/src/tools/mcp-tool.js +82 -0
  142. package/dist/src/tools/mcp-tool.js.map +1 -0
  143. package/dist/src/tools/read.d.ts +3 -0
  144. package/dist/src/tools/read.d.ts.map +1 -0
  145. package/dist/src/tools/read.js +48 -0
  146. package/dist/src/tools/read.js.map +1 -0
  147. package/dist/src/tools/shell.d.ts +3 -0
  148. package/dist/src/tools/shell.d.ts.map +1 -0
  149. package/dist/src/tools/shell.js +266 -0
  150. package/dist/src/tools/shell.js.map +1 -0
  151. package/dist/src/tools/task.d.ts +3 -0
  152. package/dist/src/tools/task.d.ts.map +1 -0
  153. package/dist/src/tools/task.js +75 -0
  154. package/dist/src/tools/task.js.map +1 -0
  155. package/dist/src/tools/types.d.ts +18 -0
  156. package/dist/src/tools/types.d.ts.map +1 -0
  157. package/dist/src/tools/types.js +2 -0
  158. package/dist/src/tools/types.js.map +1 -0
  159. package/dist/src/tools/utils.d.ts +4 -0
  160. package/dist/src/tools/utils.d.ts.map +1 -0
  161. package/dist/src/tools/utils.js +10 -0
  162. package/dist/src/tools/utils.js.map +1 -0
  163. package/dist/src/tools/web-fetch.d.ts +3 -0
  164. package/dist/src/tools/web-fetch.d.ts.map +1 -0
  165. package/dist/src/tools/web-fetch.js +81 -0
  166. package/dist/src/tools/web-fetch.js.map +1 -0
  167. package/dist/src/tools/write.d.ts +3 -0
  168. package/dist/src/tools/write.d.ts.map +1 -0
  169. package/dist/src/tools/write.js +28 -0
  170. package/dist/src/tools/write.js.map +1 -0
  171. package/dist/src/tui/app-constants.d.ts +3 -0
  172. package/dist/src/tui/app-constants.d.ts.map +1 -0
  173. package/dist/src/tui/app-constants.js +4 -0
  174. package/dist/src/tui/app-constants.js.map +1 -0
  175. package/dist/src/tui/app.d.ts +5 -0
  176. package/dist/src/tui/app.d.ts.map +1 -0
  177. package/dist/src/tui/app.js +322 -0
  178. package/dist/src/tui/app.js.map +1 -0
  179. package/dist/src/tui/commands.d.ts +61 -0
  180. package/dist/src/tui/commands.d.ts.map +1 -0
  181. package/dist/src/tui/commands.js +95 -0
  182. package/dist/src/tui/commands.js.map +1 -0
  183. package/dist/src/tui/components/chat-area.d.ts +9 -0
  184. package/dist/src/tui/components/chat-area.d.ts.map +1 -0
  185. package/dist/src/tui/components/chat-area.js +12 -0
  186. package/dist/src/tui/components/chat-area.js.map +1 -0
  187. package/dist/src/tui/components/command-palette.d.ts +18 -0
  188. package/dist/src/tui/components/command-palette.d.ts.map +1 -0
  189. package/dist/src/tui/components/command-palette.js +50 -0
  190. package/dist/src/tui/components/command-palette.js.map +1 -0
  191. package/dist/src/tui/components/message.d.ts +9 -0
  192. package/dist/src/tui/components/message.d.ts.map +1 -0
  193. package/dist/src/tui/components/message.js +116 -0
  194. package/dist/src/tui/components/message.js.map +1 -0
  195. package/dist/src/tui/components/slash-autocomplete.d.ts +14 -0
  196. package/dist/src/tui/components/slash-autocomplete.d.ts.map +1 -0
  197. package/dist/src/tui/components/slash-autocomplete.js +29 -0
  198. package/dist/src/tui/components/slash-autocomplete.js.map +1 -0
  199. package/dist/src/tui/components/toast.d.ts +8 -0
  200. package/dist/src/tui/components/toast.d.ts.map +1 -0
  201. package/dist/src/tui/components/toast.js +29 -0
  202. package/dist/src/tui/components/toast.js.map +1 -0
  203. package/dist/src/tui/hooks/use-agent.d.ts +20 -0
  204. package/dist/src/tui/hooks/use-agent.d.ts.map +1 -0
  205. package/dist/src/tui/hooks/use-agent.js +129 -0
  206. package/dist/src/tui/hooks/use-agent.js.map +1 -0
  207. package/dist/src/tui/hooks/use-app-keyboard.d.ts +53 -0
  208. package/dist/src/tui/hooks/use-app-keyboard.d.ts.map +1 -0
  209. package/dist/src/tui/hooks/use-app-keyboard.js +155 -0
  210. package/dist/src/tui/hooks/use-app-keyboard.js.map +1 -0
  211. package/dist/src/tui/hooks/use-chat-submit.d.ts +61 -0
  212. package/dist/src/tui/hooks/use-chat-submit.d.ts.map +1 -0
  213. package/dist/src/tui/hooks/use-chat-submit.js +269 -0
  214. package/dist/src/tui/hooks/use-chat-submit.js.map +1 -0
  215. package/dist/src/tui/hooks/use-permission-queue.d.ts +10 -0
  216. package/dist/src/tui/hooks/use-permission-queue.d.ts.map +1 -0
  217. package/dist/src/tui/hooks/use-permission-queue.js +26 -0
  218. package/dist/src/tui/hooks/use-permission-queue.js.map +1 -0
  219. package/dist/src/tui/hooks/use-revert.d.ts +26 -0
  220. package/dist/src/tui/hooks/use-revert.d.ts.map +1 -0
  221. package/dist/src/tui/hooks/use-revert.js +88 -0
  222. package/dist/src/tui/hooks/use-revert.js.map +1 -0
  223. package/dist/src/tui/index.d.ts +5 -0
  224. package/dist/src/tui/index.d.ts.map +1 -0
  225. package/dist/src/tui/index.js +38 -0
  226. package/dist/src/tui/index.js.map +1 -0
  227. package/dist/src/tui/prompt-bar.d.ts +29 -0
  228. package/dist/src/tui/prompt-bar.d.ts.map +1 -0
  229. package/dist/src/tui/prompt-bar.js +41 -0
  230. package/dist/src/tui/prompt-bar.js.map +1 -0
  231. package/dist/src/tui/slash-commands.d.ts +20 -0
  232. package/dist/src/tui/slash-commands.d.ts.map +1 -0
  233. package/dist/src/tui/slash-commands.js +30 -0
  234. package/dist/src/tui/slash-commands.js.map +1 -0
  235. package/dist/src/tui/theme.d.ts +29 -0
  236. package/dist/src/tui/theme.d.ts.map +1 -0
  237. package/dist/src/tui/theme.js +54 -0
  238. package/dist/src/tui/theme.js.map +1 -0
  239. package/dist/src/tui/tips.d.ts +2 -0
  240. package/dist/src/tui/tips.d.ts.map +1 -0
  241. package/dist/src/tui/tips.js +40 -0
  242. package/dist/src/tui/tips.js.map +1 -0
  243. package/dist/src/tui/types.d.ts +34 -0
  244. package/dist/src/tui/types.d.ts.map +1 -0
  245. package/dist/src/tui/types.js +2 -0
  246. package/dist/src/tui/types.js.map +1 -0
  247. package/dist/src/tui/ui/about-dialog.d.ts +8 -0
  248. package/dist/src/tui/ui/about-dialog.d.ts.map +1 -0
  249. package/dist/src/tui/ui/about-dialog.js +21 -0
  250. package/dist/src/tui/ui/about-dialog.js.map +1 -0
  251. package/dist/src/tui/ui/agent-switcher.d.ts +10 -0
  252. package/dist/src/tui/ui/agent-switcher.d.ts.map +1 -0
  253. package/dist/src/tui/ui/agent-switcher.js +77 -0
  254. package/dist/src/tui/ui/agent-switcher.js.map +1 -0
  255. package/dist/src/tui/ui/debug-dialog.d.ts +15 -0
  256. package/dist/src/tui/ui/debug-dialog.d.ts.map +1 -0
  257. package/dist/src/tui/ui/debug-dialog.js +74 -0
  258. package/dist/src/tui/ui/debug-dialog.js.map +1 -0
  259. package/dist/src/tui/ui/doctor-dialog.d.ts +9 -0
  260. package/dist/src/tui/ui/doctor-dialog.d.ts.map +1 -0
  261. package/dist/src/tui/ui/doctor-dialog.js +45 -0
  262. package/dist/src/tui/ui/doctor-dialog.js.map +1 -0
  263. package/dist/src/tui/ui/manage-providers-dialog.d.ts +10 -0
  264. package/dist/src/tui/ui/manage-providers-dialog.d.ts.map +1 -0
  265. package/dist/src/tui/ui/manage-providers-dialog.js +257 -0
  266. package/dist/src/tui/ui/manage-providers-dialog.js.map +1 -0
  267. package/dist/src/tui/ui/mcp-toggle-dialog.d.ts +8 -0
  268. package/dist/src/tui/ui/mcp-toggle-dialog.d.ts.map +1 -0
  269. package/dist/src/tui/ui/mcp-toggle-dialog.js +89 -0
  270. package/dist/src/tui/ui/mcp-toggle-dialog.js.map +1 -0
  271. package/dist/src/tui/ui/message-controls.d.ts +16 -0
  272. package/dist/src/tui/ui/message-controls.d.ts.map +1 -0
  273. package/dist/src/tui/ui/message-controls.js +102 -0
  274. package/dist/src/tui/ui/message-controls.js.map +1 -0
  275. package/dist/src/tui/ui/model-switcher.d.ts +10 -0
  276. package/dist/src/tui/ui/model-switcher.d.ts.map +1 -0
  277. package/dist/src/tui/ui/model-switcher.js +139 -0
  278. package/dist/src/tui/ui/model-switcher.js.map +1 -0
  279. package/dist/src/tui/ui/permission-dialog.d.ts +12 -0
  280. package/dist/src/tui/ui/permission-dialog.d.ts.map +1 -0
  281. package/dist/src/tui/ui/permission-dialog.js +37 -0
  282. package/dist/src/tui/ui/permission-dialog.js.map +1 -0
  283. package/dist/src/tui/ui/provider-dialog.d.ts +11 -0
  284. package/dist/src/tui/ui/provider-dialog.d.ts.map +1 -0
  285. package/dist/src/tui/ui/provider-dialog.js +232 -0
  286. package/dist/src/tui/ui/provider-dialog.js.map +1 -0
  287. package/dist/src/tui/ui/session-list.d.ts +23 -0
  288. package/dist/src/tui/ui/session-list.d.ts.map +1 -0
  289. package/dist/src/tui/ui/session-list.js +147 -0
  290. package/dist/src/tui/ui/session-list.js.map +1 -0
  291. package/dist/src/tui/ui/thinking-effort-dialog.d.ts +11 -0
  292. package/dist/src/tui/ui/thinking-effort-dialog.d.ts.map +1 -0
  293. package/dist/src/tui/ui/thinking-effort-dialog.js +79 -0
  294. package/dist/src/tui/ui/thinking-effort-dialog.js.map +1 -0
  295. package/dist/src/tui/utils/model-config.d.ts +9 -0
  296. package/dist/src/tui/utils/model-config.d.ts.map +1 -0
  297. package/dist/src/tui/utils/model-config.js +63 -0
  298. package/dist/src/tui/utils/model-config.js.map +1 -0
  299. package/dist/src/tui/utils/session-messages.d.ts +22 -0
  300. package/dist/src/tui/utils/session-messages.d.ts.map +1 -0
  301. package/dist/src/tui/utils/session-messages.js +62 -0
  302. package/dist/src/tui/utils/session-messages.js.map +1 -0
  303. package/dist/src/tui/utils.d.ts +6 -0
  304. package/dist/src/tui/utils.d.ts.map +1 -0
  305. package/dist/src/tui/utils.js +23 -0
  306. package/dist/src/tui/utils.js.map +1 -0
  307. package/dist/src/tui/variant-cycle.d.ts +10 -0
  308. package/dist/src/tui/variant-cycle.d.ts.map +1 -0
  309. package/dist/src/tui/variant-cycle.js +64 -0
  310. package/dist/src/tui/variant-cycle.js.map +1 -0
  311. package/dist/src/utils/paths.d.ts +10 -0
  312. package/dist/src/utils/paths.d.ts.map +1 -0
  313. package/dist/src/utils/paths.js +106 -0
  314. package/dist/src/utils/paths.js.map +1 -0
  315. package/dist/src/utils/platform.d.ts +10 -0
  316. package/dist/src/utils/platform.d.ts.map +1 -0
  317. package/dist/src/utils/platform.js +133 -0
  318. package/dist/src/utils/platform.js.map +1 -0
  319. package/package.json +62 -0
@@ -0,0 +1,5 @@
1
+ import type { AgentTool } from "@mohanscodex/spectra-agent";
2
+ import type { Tool as McpToolDefinition } from "@modelcontextprotocol/sdk/types.js";
3
+ export declare function createMcpAgentTool(serverName: string, mcpTool: McpToolDefinition): AgentTool;
4
+ export declare function createMcpAgentTools(serverName: string, tools: McpToolDefinition[]): AgentTool[];
5
+ //# sourceMappingURL=mcp-tool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-tool.d.ts","sourceRoot":"","sources":["../../../src/tools/mcp-tool.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAc,MAAM,4BAA4B,CAAC;AAExE,OAAO,KAAK,EAAE,IAAI,IAAI,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AA6EpF,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,iBAAiB,GACzB,SAAS,CAmBX;AAED,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,iBAAiB,EAAE,GAAG,SAAS,EAAE,CAE/F"}
@@ -0,0 +1,82 @@
1
+ import { z } from "zod";
2
+ import { defineTool } from "@mohanscodex/spectra-agent";
3
+ import { callMcpTool, formatMcpToolName } from "../integrations/mcp/index.js";
4
+ import { textResult, errorResult } from "./utils.js";
5
+ function mcpSchemaToZod(schema) {
6
+ const jsonSchema = schema;
7
+ const properties = jsonSchema.properties ?? {};
8
+ const required = jsonSchema.required ?? [];
9
+ const zodShape = {};
10
+ for (const [key, prop] of Object.entries(properties)) {
11
+ let zodType;
12
+ const propType = prop.type ?? "string";
13
+ switch (propType) {
14
+ case "string":
15
+ zodType = z.string().describe(prop.description ?? "");
16
+ if (prop.enum) {
17
+ zodType = z.enum(prop.enum).describe(prop.description ?? "");
18
+ }
19
+ break;
20
+ case "number":
21
+ zodType = z.number().describe(prop.description ?? "");
22
+ break;
23
+ case "integer":
24
+ zodType = z.number().int().describe(prop.description ?? "");
25
+ break;
26
+ case "boolean":
27
+ zodType = z.boolean().describe(prop.description ?? "");
28
+ break;
29
+ case "array":
30
+ zodType = z.array(z.unknown()).describe(prop.description ?? "");
31
+ break;
32
+ case "object":
33
+ zodType = z.record(z.unknown()).describe(prop.description ?? "");
34
+ break;
35
+ default:
36
+ zodType = z.unknown().describe(prop.description ?? "");
37
+ }
38
+ if (!required.includes(key)) {
39
+ zodType = zodType.optional();
40
+ }
41
+ zodShape[key] = zodType;
42
+ }
43
+ return z.object(zodShape);
44
+ }
45
+ function extractResultText(result) {
46
+ if (result.isError) {
47
+ const text = result.content
48
+ ?.filter((b) => b.type === "text")
49
+ .map((b) => b.text ?? "")
50
+ .join("\n") || "Tool execution failed";
51
+ return errorResult(text);
52
+ }
53
+ const text = result.content
54
+ ?.filter((b) => b.type === "text")
55
+ .map((b) => b.text ?? "")
56
+ .join("\n") || "(no output)";
57
+ return textResult(text);
58
+ }
59
+ export function createMcpAgentTool(serverName, mcpTool) {
60
+ const toolName = formatMcpToolName(serverName, mcpTool.name);
61
+ const inputSchema = mcpTool.inputSchema ?? {};
62
+ const zodSchema = mcpSchemaToZod(inputSchema);
63
+ return defineTool({
64
+ name: toolName,
65
+ description: mcpTool.description ?? `MCP tool from server "${serverName}"`,
66
+ parameters: zodSchema,
67
+ execute: async (args) => {
68
+ try {
69
+ const result = await callMcpTool(serverName, mcpTool.name, args);
70
+ return extractResultText(result);
71
+ }
72
+ catch (err) {
73
+ const message = err instanceof Error ? err.message : String(err);
74
+ return errorResult(`MCP tool "${toolName}" failed: ${message}`);
75
+ }
76
+ },
77
+ });
78
+ }
79
+ export function createMcpAgentTools(serverName, tools) {
80
+ return tools.map((tool) => createMcpAgentTool(serverName, tool));
81
+ }
82
+ //# sourceMappingURL=mcp-tool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-tool.js","sourceRoot":"","sources":["../../../src/tools/mcp-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAExD,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAErD,SAAS,cAAc,CAAC,MAA+B;IACrD,MAAM,UAAU,GAAG,MASlB,CAAC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readTool: SpectraTool;
3
+ //# sourceMappingURL=read.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.d.ts","sourceRoot":"","sources":["../../../src/tools/read.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAK9C,eAAO,MAAM,QAAQ,EAAE,WA+CtB,CAAC"}
@@ -0,0 +1,48 @@
1
+ import { z } from "zod";
2
+ import { errorResult, textResult } from "./utils.js";
3
+ import { readFileSync, statSync, readdirSync, existsSync } from "fs";
4
+ import { resolve, relative } from "path";
5
+ export const readTool = {
6
+ name: "read",
7
+ capabilities: { reads: true, writes: false },
8
+ description: `Read the contents of a file or directory.
9
+ Supports reading files with optional line ranges.
10
+ When reading a directory, lists entries with types.
11
+ For large files, use the offset and limit parameters to read specific sections.`,
12
+ displayName: (args) => relative(process.cwd(), resolve(args.path)),
13
+ parameters: z.object({
14
+ path: z.string().describe("Absolute or relative path to the file or directory"),
15
+ offset: z.number().optional().describe("Starting line number (1-indexed)"),
16
+ limit: z.number().optional().describe("Maximum number of lines to read"),
17
+ }),
18
+ execute: async ({ path, offset, limit }) => {
19
+ const resolved = resolve(process.cwd(), path);
20
+ if (!existsSync(resolved)) {
21
+ return errorResult(`File not found: ${resolved}`);
22
+ }
23
+ const stat = statSync(resolved);
24
+ if (stat.isDirectory()) {
25
+ const entries = readdirSync(resolved, { withFileTypes: true });
26
+ const listing = entries.map(e => `${e.isDirectory() ? "📁" : "📄"} ${e.name}`).join("\n");
27
+ return textResult(`Directory: ${resolved}\n${listing}`);
28
+ }
29
+ if (stat.size > 1024 * 1024) {
30
+ return errorResult(`File too large (${(stat.size / 1024 / 1024).toFixed(1)} MB). Use offset/limit or grep instead.`);
31
+ }
32
+ const content = readFileSync(resolved, "utf-8");
33
+ const lines = content.split("\n");
34
+ const start = offset ? Math.max(0, offset - 1) : 0;
35
+ const end = limit ? Math.min(lines.length, start + limit) : lines.length;
36
+ const slice = lines.slice(start, end);
37
+ const result = slice.map((line, i) => {
38
+ const lineNum = start + i + 1;
39
+ return `${String(lineNum).padStart(4)} │ ${line}`;
40
+ }).join("\n");
41
+ const header = `${resolved} (${lines.length} lines)`;
42
+ if (start > 0 || end < lines.length) {
43
+ return textResult(`${header} [lines ${start + 1}-${end}]\n${result}`);
44
+ }
45
+ return textResult(`${header}\n${result}`);
46
+ },
47
+ };
48
+ //# sourceMappingURL=read.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.js","sourceRoot":"","sources":["../../../src/tools/read.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AACrE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAEzC,MAAM,CAAC,MAAM,QAAQ,GAAgB;IACnC,IAAI,EAAE,MAAM;IACZ,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE;IAC5C,WAAW,EAAE;;;gFAGiE;IAC9E,WAAW,EAAE,CAAC,IAAsB,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpF,UAAU,EAAE,CAAC,CAAC,MAAM,CAAC;QACnB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,oDAAoD,CAAC;QAC/E,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,kCAAkC,CAAC;QAC1E,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,iCAAiC,CAAC;KACzE,CAAC;I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,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACnD,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC;QACzE,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAEtC,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE;YACnC,MAAM,OAAO,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC;YAC9B,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC;QACpD,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEd,MAAM,MAAM,GAAG,GAAG,QAAQ,KAAK,KAAK,CAAC,MAAM,SAAS,CAAC;QACrD,IAAI,KAAK,GAAG,CAAC,IAAI,GAAG,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;YACpC,OAAO,UAAU,CAAC,GAAG,MAAM,WAAW,KAAK,GAAG,CAAC,IAAI,GAAG,MAAM,MAAM,EAAE,CAAC,CAAC;QACxE,CAAC;QACD,OAAO,UAAU,CAAC,GAAG,MAAM,KAAK,MAAM,EAAE,CAAC,CAAC;IAC5C,CAAC;CACF,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { SpectraTool } from "./types.js";
2
+ export declare const shellTool: SpectraTool;
3
+ //# sourceMappingURL=shell.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell.d.ts","sourceRoot":"","sources":["../../../src/tools/shell.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,YAAY,CAAC;AAyF3D,eAAO,MAAM,SAAS,EAAE,WAgMvB,CAAC"}
@@ -0,0 +1,266 @@
1
+ import { z } from "zod";
2
+ import { spawn } from "child_process";
3
+ import { getPlatformInfo } from "../utils/platform.js";
4
+ import { tmpdir } from "os";
5
+ import { join } from "path";
6
+ import { writeFileSync, mkdirSync, existsSync } from "fs";
7
+ const DEFAULT_TIMEOUT = 2 * 60 * 1000;
8
+ const MAX_OUTPUT_LINES = 2000;
9
+ const MAX_OUTPUT_BYTES = 50 * 1024;
10
+ const SIGKILL_GRACE_MS = 200;
11
+ const UPDATE_MIN_INTERVAL_MS = 100;
12
+ const PREVIEW_BYTES = 30_000;
13
+ function killProcess(pid) {
14
+ return new Promise((resolve) => {
15
+ if (process.platform === "win32") {
16
+ const killer = spawn("taskkill", ["/pid", String(pid), "/f", "/t"], {
17
+ stdio: "ignore",
18
+ windowsHide: true,
19
+ });
20
+ killer.once("exit", () => resolve());
21
+ killer.once("error", () => resolve());
22
+ return;
23
+ }
24
+ try {
25
+ process.kill(-pid, "SIGTERM");
26
+ }
27
+ catch {
28
+ // process already gone
29
+ }
30
+ const timer = setTimeout(() => {
31
+ try {
32
+ process.kill(-pid, "SIGKILL");
33
+ }
34
+ catch {
35
+ // process already gone
36
+ }
37
+ resolve();
38
+ }, SIGKILL_GRACE_MS);
39
+ timer.unref();
40
+ });
41
+ }
42
+ function tailText(text, maxLines, maxBytes) {
43
+ const lines = text.split("\n");
44
+ if (lines.length <= maxLines && Buffer.byteLength(text, "utf-8") <= maxBytes) {
45
+ return { text, cut: false };
46
+ }
47
+ const out = [];
48
+ let bytes = 0;
49
+ for (let i = lines.length - 1; i >= 0 && out.length < maxLines; i--) {
50
+ const size = Buffer.byteLength(lines[i], "utf-8") + (out.length > 0 ? 1 : 0);
51
+ if (bytes + size > maxBytes) {
52
+ if (out.length === 0) {
53
+ const buf = Buffer.from(lines[i], "utf-8");
54
+ let start = Math.max(0, buf.length - maxBytes);
55
+ while (start < buf.length && (buf[start] & 0xc0) === 0x80)
56
+ start++;
57
+ out.unshift(buf.subarray(start).toString("utf-8"));
58
+ }
59
+ break;
60
+ }
61
+ out.unshift(lines[i]);
62
+ bytes += size;
63
+ }
64
+ return { text: out.join("\n"), cut: true };
65
+ }
66
+ function previewText(text) {
67
+ if (text.length <= PREVIEW_BYTES)
68
+ return text;
69
+ return "...\n\n" + text.slice(-PREVIEW_BYTES);
70
+ }
71
+ function truncationDir() {
72
+ const dir = join(tmpdir(), "spectra-truncation");
73
+ if (!existsSync(dir)) {
74
+ mkdirSync(dir, { recursive: true });
75
+ }
76
+ return dir;
77
+ }
78
+ export const shellTool = {
79
+ name: "bash",
80
+ capabilities: { reads: true, writes: true },
81
+ description: `Execute shell commands on the user's system.
82
+ Supports any command available in the system shell.
83
+ Output streams in realtime while the command runs.
84
+ Returns stdout, stderr, exit code. Long output is truncated with the full content saved to a temp file.
85
+ Be careful with destructive commands — seek permission for rm -rf, sudo, etc.`,
86
+ displayName: (args) => args.command.split("\n")[0].slice(0, 60),
87
+ parameters: z.object({
88
+ command: z.string().describe("The shell command to execute"),
89
+ description: z.string().optional().describe("Brief description of what this command does"),
90
+ timeout: z.number().optional().describe("Timeout in milliseconds (default: 2 min, max: 10 min)"),
91
+ workdir: z.string().optional().describe("Working directory for the command"),
92
+ }),
93
+ execute: async ({ command, description, timeout, workdir }, ctx) => {
94
+ return new Promise((resolve) => {
95
+ const info = getPlatformInfo();
96
+ const isWindows = info.os === "windows";
97
+ const shell = info.shell;
98
+ const isPwsh = /^pwsh(\.exe)?$/i.test(shell) || /^powershell(\.exe)?$/i.test(shell);
99
+ const effectiveTimeout = Math.min(timeout ?? DEFAULT_TIMEOUT, 10 * 60 * 1000);
100
+ const onUpdate = ctx.onUpdate;
101
+ let proc;
102
+ if (isPwsh) {
103
+ proc = spawn(shell, ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", command], {
104
+ cwd: workdir || process.cwd(),
105
+ env: process.env,
106
+ stdio: ["ignore", "pipe", "pipe"],
107
+ windowsHide: true,
108
+ detached: false,
109
+ });
110
+ }
111
+ else {
112
+ proc = spawn(command, [], {
113
+ cwd: workdir || process.cwd(),
114
+ env: process.env,
115
+ stdio: ["ignore", "pipe", "pipe"],
116
+ windowsHide: true,
117
+ shell: isWindows ? shell : true,
118
+ detached: !isWindows,
119
+ });
120
+ }
121
+ let stdout = "";
122
+ let stderr = "";
123
+ let killed = false;
124
+ let resolved = false;
125
+ let forceKillTimer;
126
+ let lastUpdate = 0;
127
+ const finalize = (result) => {
128
+ if (resolved)
129
+ return;
130
+ resolved = true;
131
+ if (forceKillTimer)
132
+ clearTimeout(forceKillTimer);
133
+ resolve(result);
134
+ };
135
+ const kill = () => {
136
+ if (killed)
137
+ return;
138
+ killed = true;
139
+ if (!proc.pid)
140
+ return;
141
+ killProcess(proc.pid);
142
+ forceKillTimer = setTimeout(() => {
143
+ // If the process still hasn't exited after grace, force-resolve
144
+ if (!resolved && (proc.exitCode === null && proc.signalCode === null)) {
145
+ try {
146
+ proc.kill("SIGKILL");
147
+ }
148
+ catch { /* ignore */ }
149
+ }
150
+ // Give a bit more time for the exit event to fire
151
+ setTimeout(() => {
152
+ if (!resolved) {
153
+ const combined = `${stdout}${stderr ? "\n" + stderr : ""}`;
154
+ const truncated = tailText(combined, MAX_OUTPUT_LINES, MAX_OUTPUT_BYTES);
155
+ finalize({
156
+ content: [{
157
+ type: "text",
158
+ text: truncated.text + "\n\n<shell_metadata>\nshell tool terminated command after exceeding timeout " + effectiveTimeout + " ms. If this command is expected to take longer, retry with a larger timeout value.\n</shell_metadata>",
159
+ }],
160
+ details: { exitCode: -1, command, truncated: truncated.cut },
161
+ isError: true,
162
+ });
163
+ }
164
+ }, 500);
165
+ }, 3100);
166
+ };
167
+ // Timeout
168
+ const timeoutId = setTimeout(() => kill(), effectiveTimeout);
169
+ // Abort signal
170
+ if (ctx.signal) {
171
+ if (ctx.signal.aborted) {
172
+ kill();
173
+ }
174
+ else {
175
+ ctx.signal.addEventListener("abort", kill, { once: true });
176
+ }
177
+ }
178
+ // Streams may not exist for some shell configurations
179
+ if (proc.stdout) {
180
+ proc.stdout.on("data", (chunk) => {
181
+ stdout += chunk.toString("utf-8");
182
+ if (onUpdate) {
183
+ const now = Date.now();
184
+ if (now - lastUpdate >= UPDATE_MIN_INTERVAL_MS) {
185
+ lastUpdate = now;
186
+ onUpdate({
187
+ content: [{ type: "text", text: previewText(stdout + (stderr ? "\n" + stderr : "")) }],
188
+ details: { exitCode: 0, command },
189
+ });
190
+ }
191
+ }
192
+ });
193
+ }
194
+ if (proc.stderr) {
195
+ proc.stderr.on("data", (chunk) => {
196
+ stderr += chunk.toString("utf-8");
197
+ if (onUpdate) {
198
+ const now = Date.now();
199
+ if (now - lastUpdate >= UPDATE_MIN_INTERVAL_MS) {
200
+ lastUpdate = now;
201
+ onUpdate({
202
+ content: [{ type: "text", text: previewText(stdout + (stderr ? "\n" + stderr : "")) }],
203
+ details: { exitCode: 0, command },
204
+ });
205
+ }
206
+ }
207
+ });
208
+ }
209
+ proc.once("exit", (code, signal) => {
210
+ clearTimeout(timeoutId);
211
+ if (ctx.signal) {
212
+ ctx.signal.removeEventListener("abort", kill);
213
+ }
214
+ if (onUpdate) {
215
+ const combined = stdout + (stderr ? "\n" + stderr : "");
216
+ onUpdate({
217
+ content: [{ type: "text", text: previewText(combined) }],
218
+ details: { exitCode: code ?? (signal ? 1 : 0), command },
219
+ });
220
+ }
221
+ const exitCode = code ?? (signal ? 1 : 0);
222
+ let output = stdout;
223
+ if (stderr)
224
+ output = output ? `${output}\n${stderr}` : stderr;
225
+ const truncated = tailText(output, MAX_OUTPUT_LINES, MAX_OUTPUT_BYTES);
226
+ let finalText = truncated.text || "(no output)";
227
+ if (truncated.cut) {
228
+ const dir = truncationDir();
229
+ const outputPath = join(dir, `shell_${Date.now()}_${Math.random().toString(36).slice(2, 8)}.txt`);
230
+ writeFileSync(outputPath, output, "utf-8");
231
+ finalText = `...output truncated...\n\nFull output saved to: ${outputPath}\n\n` + finalText;
232
+ finalize({
233
+ content: [{ type: "text", text: finalText }],
234
+ details: { exitCode, command, truncated: true, outputPath },
235
+ isError: exitCode !== 0,
236
+ });
237
+ }
238
+ else {
239
+ finalize({
240
+ content: [{ type: "text", text: finalText }],
241
+ details: { exitCode, command, truncated: false },
242
+ isError: exitCode !== 0,
243
+ });
244
+ }
245
+ });
246
+ proc.once("error", (err) => {
247
+ clearTimeout(timeoutId);
248
+ if (ctx.signal) {
249
+ ctx.signal.removeEventListener("abort", kill);
250
+ }
251
+ const msg = err.message || "";
252
+ const prefix = `Command failed: ${command}\n`;
253
+ const prefixCr = `Command failed: ${command}\r\n`;
254
+ let clean = msg.startsWith(prefix) ? msg.slice(prefix.length)
255
+ : msg.startsWith(prefixCr) ? msg.slice(prefixCr.length)
256
+ : msg;
257
+ finalize({
258
+ content: [{ type: "text", text: clean }],
259
+ details: { exitCode: 1, command },
260
+ isError: true,
261
+ });
262
+ });
263
+ });
264
+ },
265
+ };
266
+ //# sourceMappingURL=shell.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell.js","sourceRoot":"","sources":["../../../src/tools/shell.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,KAAK,EAAqB,MAAM,eAAe,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAE1D,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AACtC,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,gBAAgB,GAAG,EAAE,GAAG,IAAI,CAAC;AACnC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAC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AG,mBAAmB,OAAO,MAAM,CAAC;gBAClD,IAAI,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;oBAC3D,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC;wBACvD,CAAC,CAAC,GAAG,CAAC;gBAER,QAAQ,CAAC;oBACP,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;oBACxC,OAAO,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAE;oBACjC,OAAO,EAAE,IAAI;iBACd,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { SpectraTool } from "./types.js";
2
+ export declare const taskTool: SpectraTool;
3
+ //# sourceMappingURL=task.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task.d.ts","sourceRoot":"","sources":["../../../src/tools/task.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAyB9C,eAAO,MAAM,QAAQ,EAAE,WA8DtB,CAAC"}
@@ -0,0 +1,75 @@
1
+ import { z } from "zod";
2
+ import { textResult, errorResult } from "./utils.js";
3
+ import { AGENT_DEFINITIONS, SUBAGENTS, filterToolsByAgent } from "../agents/definitions.js";
4
+ import { AgentRegistry } from "../agents/registry.js";
5
+ import { getSystemPrompt } from "../utils/platform.js";
6
+ function descriptionForTaskTool() {
7
+ const subagentList = SUBAGENTS.map((name) => {
8
+ const def = AGENT_DEFINITIONS[name];
9
+ return `- **${name}**: ${def.description}`;
10
+ }).join("\n");
11
+ return `Launch a new agent to handle complex, multistep tasks autonomously.
12
+
13
+ When to use the Task tool:
14
+ ${subagentList}
15
+
16
+ Usage notes:
17
+ 1. Launch multiple agents concurrently whenever possible, to maximize performance
18
+ 2. Each agent invocation starts with a fresh context
19
+ 3. The agent's outputs should generally be trusted
20
+ 4. Clearly tell the agent whether you expect it to write code or just to do research`;
21
+ }
22
+ export const taskTool = {
23
+ name: "task",
24
+ capabilities: { reads: false, writes: false },
25
+ description: descriptionForTaskTool(),
26
+ displayName: (args) => `@${args.subagent_type || "subagent"} ${args.description || ""}`.slice(0, 60),
27
+ parameters: z.object({
28
+ description: z.string().describe("A short (3-5 words) description of the task"),
29
+ prompt: z.string().describe("The task for the agent to perform"),
30
+ subagent_type: z.string().describe("The type of specialized agent to use for this task"),
31
+ }),
32
+ execute: async ({ description, prompt, subagent_type }, ctx) => {
33
+ const def = AGENT_DEFINITIONS[subagent_type];
34
+ if (!def) {
35
+ const available = SUBAGENTS.join(", ");
36
+ return errorResult(`Unknown subagent "${subagent_type}". Available: ${available}`);
37
+ }
38
+ if (def.mode !== "subagent") {
39
+ return errorResult(`"${subagent_type}" is a primary agent, not a subagent. Available subagents: ${SUBAGENTS.join(", ")}`);
40
+ }
41
+ const config = AgentRegistry.getConfig();
42
+ if (!config) {
43
+ return errorResult("Task tool: no agent config available");
44
+ }
45
+ try {
46
+ const { Agent } = await import("@mohanscodex/spectra-agent");
47
+ const { createAllTools, spectraToolToAgentTool } = await import("./index.js");
48
+ const allTools = createAllTools().map((t) => spectraToolToAgentTool(t));
49
+ const tools = filterToolsByAgent(allTools, subagent_type);
50
+ const subagent = new Agent({
51
+ model: config.model,
52
+ systemPrompt: [getSystemPrompt(), def.prompt].filter(Boolean).join("\n\n"),
53
+ tools,
54
+ maxTurns: def.maxTurns,
55
+ getApiKey: config.getApiKey,
56
+ });
57
+ let finalText = "";
58
+ for await (const ev of subagent.run(prompt)) {
59
+ if (ev.type === "message_update" && ev.message.role === "assistant") {
60
+ const textBlocks = ev.message.content.filter((c) => c.type === "text");
61
+ finalText = textBlocks.map((b) => b.text).join("");
62
+ }
63
+ }
64
+ const resultText = finalText.trim();
65
+ if (!resultText) {
66
+ return textResult(`Subagent @${subagent_type} completed with no text output.`);
67
+ }
68
+ return textResult(resultText);
69
+ }
70
+ catch (err) {
71
+ return errorResult(`Subagent @${subagent_type} failed: ${err instanceof Error ? err.message : String(err)}`);
72
+ }
73
+ },
74
+ };
75
+ //# sourceMappingURL=task.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task.js","sourceRoot":"","sources":["../../../src/tools/task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC5F,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,SAAS,sBAAsB;IAC7B,MAAM,YAAY,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QAC1C,MAAM,GAAG,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;QACpC,OAAO,OAAO,IAAI,OAAO,GAAG,CAAC,WAAW,EAAE,CAAC;IAC7C,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAEd,OAAO;;;EAGP,YAAY;;;;;;qFAMuE,CAAC;AACtF,CAAC;AAED,MAAM,CAAC,MAAM,QAAQ,GAAgB;IACnC,IAAI,EAAE,MAAM;IACZ,YAAY,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE;IAC7C,WAAW,EAAE,sBAAsB,EAAE;IACrC,WAAW,EAAE,CAAC,IAA6B,EAAE,EAAE,CAC7C,IAAK,IAAY,CAAC,aAAa,IAAI,UAAU,IAAI,I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AO,GAAG,EAAE,CAAC;YACb,OAAO,WAAW,CAChB,aAAa,aAAa,YAAY,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CACzF,CAAC;QACJ,CAAC;IACH,CAAC;CACF,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { z } from "zod";
2
+ import type { ToolResult, ToolUpdateCallback } from "@mohanscodex/spectra-agent";
3
+ import type { ToolCapabilities } from "../security/types.js";
4
+ export interface SpectraTool<TArgs extends z.ZodType = z.ZodType> {
5
+ name: string;
6
+ description: string;
7
+ displayName?: string | ((args: z.infer<TArgs>, result: ToolResult) => string);
8
+ parameters: TArgs;
9
+ promptGuidelines?: string[];
10
+ capabilities?: ToolCapabilities;
11
+ execute: (args: z.infer<TArgs>, context: ToolContext) => Promise<ToolResult>;
12
+ }
13
+ export type ToolContext = {
14
+ toolCallId: string;
15
+ signal?: AbortSignal;
16
+ onUpdate?: ToolUpdateCallback;
17
+ };
18
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/tools/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE7D,MAAM,WAAW,WAAW,CAAC,KAAK,SAAS,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO;IAC9D,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,UAAU,KAAK,MAAM,CAAC,CAAC;IAC9E,UAAU,EAAE,KAAK,CAAC;IAClB,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,YAAY,CAAC,EAAE,gBAAgB,CAAC;IAChC,OAAO,EAAE,CACP,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EACpB,OAAO,EAAE,WAAW,KACjB,OAAO,CAAC,UAAU,CAAC,CAAC;CAC1B;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,QAAQ,CAAC,EAAE,kBAAkB,CAAC;CAC/B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/tools/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import type { ToolResult } from "@mohanscodex/spectra-agent";
2
+ export declare function textResult(text: string): ToolResult;
3
+ export declare function errorResult(message: string): ToolResult;
4
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/tools/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAE7D,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAEnD;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAKvD"}
@@ -0,0 +1,10 @@
1
+ export function textResult(text) {
2
+ return { content: [{ type: "text", text }] };
3
+ }
4
+ export function errorResult(message) {
5
+ return {
6
+ content: [{ type: "text", text: message }],
7
+ isError: true,
8
+ };
9
+ }
10
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/tools/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAoC,CAAC,EAAE,CAAC;AACjF,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,OAAe;IACzC,OAAO;QACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAoC,CAAC;QAC5E,OAAO,EAAE,IAAI;KACd,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { SpectraTool } from "./types.js";
2
+ export declare const webFetchTool: SpectraTool;
3
+ //# sourceMappingURL=web-fetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-fetch.d.ts","sourceRoot":"","sources":["../../../src/tools/web-fetch.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG9C,eAAO,MAAM,YAAY,EAAE,WAuD1B,CAAC"}