gen-tool-catalog.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /**
  2. * Generate `docs/tool-catalog.md` from schemas collected by booting each tool
  3. * plugin. Runtime registration is the source of truth for computed schemas;
  4. * the manifest is checked against every on-disk `tool-*` package. `--check`
  5. * verifies the committed artifact. Rationale and ownership live in
  6. * `.agents/notes/implemented/process/2026-07-02-tool-schema-catalog.md`.
  7. */
  8. import { globSync, readFileSync, writeFileSync } from 'node:fs'
  9. import { basename, resolve } from 'node:path'
  10. import { Context } from 'cordis'
  11. import type { ToolSchema } from '@deepseek-ai/dsh-llm'
  12. import AgentRegistry from '@deepseek-ai/dsh-agent'
  13. import SessionStore from '@deepseek-ai/dsh-session'
  14. import SessionQuerySqlite from '@deepseek-ai/dsh-session-query-sqlite'
  15. import GoalService from '@deepseek-ai/dsh-goal'
  16. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  17. import ToolRegistry, { type Config as ToolsConfig } from '@deepseek-ai/dsh-tools'
  18. import LocalBashExecutor from '@deepseek-ai/dsh-bash-local'
  19. import LocalSubprocessService from '@deepseek-ai/dsh-subprocess-local'
  20. import LocalFileSystem from '@deepseek-ai/dsh-fs-local'
  21. import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
  22. import PlanModeService from '@deepseek-ai/dsh-plan-mode'
  23. import WebService from '@deepseek-ai/dsh-web'
  24. import * as WebSearchExa from '@deepseek-ai/dsh-web-search-exa'
  25. import * as WebFetchLocal from '@deepseek-ai/dsh-web-fetch-local'
  26. import SubagentService from '@deepseek-ai/dsh-subagent'
  27. import type { SubagentProvider } from '@deepseek-ai/dsh-subagent'
  28. import SkillService from '@deepseek-ai/dsh-skill'
  29. import * as SkillLocal from '@deepseek-ai/dsh-skill-local'
  30. import LocalTaskService from '@deepseek-ai/dsh-tasks-local'
  31. import * as ToolAskUser from '@deepseek-ai/dsh-tool-ask-user'
  32. import * as ToolBash from '@deepseek-ai/dsh-tool-bash'
  33. import * as ToolBashPersistent from '@deepseek-ai/dsh-tool-bash-persistent'
  34. import * as ToolCordis from '@deepseek-ai/dsh-tool-cordis'
  35. import * as ToolFs from '@deepseek-ai/dsh-tool-fs'
  36. import * as ToolFsSearch from '@deepseek-ai/dsh-tool-fs-search'
  37. import * as ToolStrReplaceEditor from '@deepseek-ai/dsh-tool-str-replace-editor'
  38. import PtyService from '@deepseek-ai/dsh-pty'
  39. import * as ToolPty from '@deepseek-ai/dsh-tool-pty'
  40. import * as ToolGoal from '@deepseek-ai/dsh-tool-goal'
  41. import Lsp from '@deepseek-ai/dsh-lsp'
  42. import * as ToolLsp from '@deepseek-ai/dsh-tool-lsp'
  43. import * as ToolSkill from '@deepseek-ai/dsh-tool-skill'
  44. import * as ToolSessionQuery from '@deepseek-ai/dsh-tool-session-query'
  45. import * as ToolTasks from '@deepseek-ai/dsh-tool-tasks'
  46. import * as ToolTodo from '@deepseek-ai/dsh-tool-todo'
  47. import * as ToolSubagent from '@deepseek-ai/dsh-tool-subagent'
  48. import * as ToolWeb from '@deepseek-ai/dsh-tool-web'
  49. import VmWorkflowEngine from '@deepseek-ai/dsh-workflow-workerthread'
  50. import * as ToolRalph from '@deepseek-ai/dsh-tool-ralph'
  51. import * as ToolWorkflow from '@deepseek-ai/dsh-tool-workflow'
  52. const root = resolve(import.meta.dirname, '..')
  53. const OUT = 'docs/tool-catalog.md'
  54. /**
  55. * Register the descriptor needed to mount schema-producing consumers. Declares
  56. * the full capability set of the shipped in-process providers so consumers
  57. * mount under their shipped defaults (tool-subagent's default numeric maxDepth
  58. * requires `depthLimit`).
  59. */
  60. function registerCatalogSubagentProvider(ctx: Context, name: string): void {
  61. const provider: SubagentProvider = {
  62. name,
  63. capabilities: { outputSchema: true, depthLimit: true, toolFilter: true, persona: true },
  64. inheritsParentContext: false,
  65. start: () => Promise.reject(new Error('tool-catalog provider cannot start a child')),
  66. }
  67. ctx.subagents.registerProvider(provider)
  68. }
  69. /**
  70. * Tool package plus its hand-maintained boot recipe. The caller mounts the
  71. * prompt and registry; each recipe supplies only package-specific seams and
  72. * config, while `dir` participates in the completeness check.
  73. */
  74. interface ToolPackage {
  75. /** The npm package name, used as the catalog section heading. */
  76. pkg: string
  77. /** The `packages/<group>/<dir>` leaf name — matched by the completeness guard. */
  78. dir: string
  79. /** Repo-relative source path linked from the catalog entry. */
  80. source: string
  81. /** Services or owning runtime surfaces the package requires at execution time. */
  82. requires: string[]
  83. /** Session events or other visible state the tools write or affect. */
  84. writes: string[]
  85. /** Additional model-visible names shipped by example/app config. */
  86. shippedNames?: string[]
  87. /** Plug the injected seams + the tool plugin onto a context that already
  88. * carries `systemPrompt` + `tools`. */
  89. mount: (ctx: Context) => Promise<void>
  90. /**
  91. * Config for the caller's `ToolRegistry` mount. The registry itself ships a
  92. * model-facing tool (`run_code`, registered under a non-native `mode`), so
  93. * ITS catalog entry boots the registry in the mode that surfaces it;
  94. * every other entry uses the default (native) registry.
  95. */
  96. toolsConfig?: ToolsConfig
  97. /**
  98. * A deployment note rendered after the package's tools, for a fact that
  99. * booting the package alone cannot show. The registered tool NAME can be a
  100. * load-time config (`tool-subagent`'s `toolName`), so one package may surface
  101. * under several names across deployments — the boot yields the package
  102. * DEFAULT, and this note records the shipped alternatives the model sees.
  103. */
  104. note?: string
  105. }
  106. /**
  107. * The boot manifest: every shipped tool package (a `tool-*` leaf under
  108. * `packages/`). Ordered by package name (the render order); the completeness
  109. * guard proves it is exhaustive against the on-disk glob.
  110. */
  111. const TOOL_PACKAGES: ToolPackage[] = [
  112. {
  113. pkg: '@deepseek-ai/dsh-tool-ask-user',
  114. dir: 'tool-ask-user',
  115. source: 'packages/ui/tool-ask-user/src/index.ts',
  116. requires: ['ctx.tools', 'ctx.userInteraction'],
  117. writes: ['tool/call', 'tool/result after a UI/provider answers the question'],
  118. async mount(ctx) {
  119. await ctx.plugin(UserInteractionService)
  120. await ctx.plugin(ToolAskUser)
  121. },
  122. note:
  123. 'ask_user_question pauses the tool call until the active UI provider returns a human answer.',
  124. },
  125. {
  126. pkg: '@deepseek-ai/dsh-tools',
  127. dir: 'tools',
  128. source: 'packages/core/tools/src/code-mode.ts',
  129. requires: ['ctx.tools', 'ctx.codeRuntime (execution time)', 'ctx.systemPrompt'],
  130. writes: ['tool/call', 'one tool/code-dispatch-start + tool/code-dispatch pair per bridged sub-call', 'tool/result'],
  131. // The registry's OWN tool: run_code exists only under a non-native mode
  132. // (the registry registers it in its constructor; the code runtime is read
  133. // at assembly/execution time, so the schema harvest needs none mounted).
  134. toolsConfig: { mode: 'code' },
  135. async mount() {},
  136. note:
  137. 'Owned by the tool registry as a reserved transport outside filterable capability layers under `mode: code` / `mode: both` (see the Code Mode Agent Note). Under `code` it is the registry\'s only wire contribution; the other visible capabilities are declared in a generated TypeScript SDK section, and a program calls them through bindings scheduled under the native concurrency contract (submission-ordered starts and policy; concurrency-safe bodies overlap up to `maxParallelSubCalls`) that re-enter the complete guarded tool pipeline and link each nested execution to this outer result.',
  138. },
  139. {
  140. pkg: '@deepseek-ai/dsh-plan-mode',
  141. dir: 'plan-mode',
  142. source: 'packages/plan/plan-mode/src/index.ts',
  143. requires: ['ctx.tools', 'ctx.systemPrompt', 'ctx.userInteraction (execution time, opportunistic)'],
  144. writes: ['tool/call', 'plan/mode inactive on an approved review', 'tool/result'],
  145. async mount(ctx) {
  146. await ctx.plugin(PlanModeService, { section: 'Tool catalog schema harvest.' })
  147. },
  148. note:
  149. 'exit_plan_mode stays in the model-facing schema while planning is inactive so transitions add no tool-catalog churn on top of the plan-policy change. Its execute path rejects calls outside plan mode; in plan mode it presents the plan over the user-interaction seam (approve / keep planning with feedback), and approval logs plan mode inactive at the step boundary.',
  150. },
  151. {
  152. pkg: '@deepseek-ai/dsh-tool-bash',
  153. dir: 'tool-bash',
  154. source: 'packages/bash/tool-bash/src/index.ts',
  155. requires: ['ctx.tools', 'ctx.bash', 'ctx.tasks at call time for run_in_background'],
  156. writes: ['tool/call', 'tool/result'],
  157. async mount(ctx) {
  158. await ctx.plugin(LocalSubprocessService)
  159. await ctx.plugin(LocalBashExecutor)
  160. await ctx.plugin(ToolBash)
  161. },
  162. note:
  163. 'The bash tool is the model-facing consumer of the bash executor seam. A `run_in_background` run registers with the generic `ctx.tasks` runtime and is collected/stopped through the `task_*` tools from `@deepseek-ai/dsh-tool-tasks`; the `enableRunInBackground` config (default true) removes the parameter entirely when disabled.',
  164. },
  165. {
  166. pkg: '@deepseek-ai/dsh-tool-cordis',
  167. dir: 'tool-cordis',
  168. source: 'packages/cordis/tool-cordis/src/index.ts',
  169. requires: ['ctx.tools'],
  170. writes: ['tool/call', 'tool/result', 'process-local temporary Plugin lifecycle'],
  171. async mount(ctx) {
  172. await ctx.plugin(ToolCordis)
  173. },
  174. note:
  175. 'Not in any shipped tree (a deliberate opt-in — temporary Plugin code reaches the real runtime, see .agents/notes/implemented/feature/2026-07-08-self-referential-cordis-toolset.md). Plugins created by cordis_mount may register ADDITIONAL model-visible tools until unmounted or DSH restarts; a full changed request header logs those tool-set changes.',
  176. },
  177. {
  178. pkg: '@deepseek-ai/dsh-tool-bash-persistent',
  179. dir: 'tool-bash-persistent',
  180. source: 'packages/pty/tool-bash-persistent/src/index.ts',
  181. requires: ['ctx.tools', 'ctx.pty', 'an owning Agent at execution time'],
  182. writes: ['tool/call', 'PTY shell state', 'tool/result'],
  183. async mount(ctx) {
  184. await ctx.plugin(PtyService)
  185. await ctx.plugin(ToolBashPersistent)
  186. },
  187. note:
  188. 'One owner-isolated persistent bash tool; deployment composition supplies the PTY backend and may override the model-facing environment description.',
  189. },
  190. {
  191. pkg: '@deepseek-ai/dsh-tool-str-replace-editor',
  192. dir: 'tool-str-replace-editor',
  193. source: 'packages/fs/tool-str-replace-editor/src/index.ts',
  194. requires: ['ctx.tools', 'ctx.fs'],
  195. writes: ['tool/call', 'fs/observed after successful file operations', 'tool/result'],
  196. async mount(ctx) {
  197. await ctx.plugin(LocalFileSystem)
  198. await ctx.plugin(ToolStrReplaceEditor)
  199. },
  200. note:
  201. 'Standalone view/create/unique literal replace/line insert tool over the filesystem seam; it composes with any shell or terminal surface.',
  202. },
  203. {
  204. pkg: '@deepseek-ai/dsh-tool-fs',
  205. dir: 'tool-fs',
  206. source: 'packages/fs/tool-fs/src/index.ts',
  207. requires: ['ctx.tools', 'ctx.fs', 'ctx.systemPrompt'],
  208. writes: ['tool/call', 'fs/write-intent or fs/edit-intent for mutations', 'fs/observed after successful file operations', 'tool/result'],
  209. async mount(ctx) {
  210. // The tool needs `fs`; the bare provider is sufficient because policy
  211. // changes behavior, not schema shape.
  212. await ctx.plugin(LocalFileSystem)
  213. await ctx.plugin(ToolFs)
  214. },
  215. note:
  216. 'The read-before-write/edit policy is added by `@deepseek-ai/dsh-fs-policy` (an `fs/*` event-gate plugin, no schema change); a deployment that loads these tools is expected to also load it. The tool schemas above are identical with or without the policy plugin.',
  217. },
  218. {
  219. pkg: '@deepseek-ai/dsh-tool-fs-search',
  220. dir: 'tool-fs-search',
  221. source: 'packages/fs/tool-fs-search/src/index.ts',
  222. requires: ['ctx.tools', 'ctx.subprocess', 'ctx.systemPrompt'],
  223. writes: ['tool/call', 'tool/result'],
  224. async mount(ctx) {
  225. // The tools inject `subprocess` (search spawns the packaged ripgrep
  226. // binary through the seam, not ctx.fs); registration itself never
  227. // spawns, so the real local service is inert here. `ctx.spillStore` is
  228. // optional (read via ctx.get) and does not affect the schemas, so no
  229. // spill backend is mounted.
  230. await ctx.plugin(LocalSubprocessService)
  231. await ctx.plugin(ToolFsSearch, { sampleOverCapGlobResults: true })
  232. },
  233. note:
  234. 'glob and grep are unconditional discovery tools that spawn the packaged ripgrep binary (`@vscode/ripgrep`) through ctx.subprocess as ordinary foreground calls (never background tasks) — no host `rg` install and no shell layer. The catalog uses `sampleOverCapGlobResults: true`; deployments must choose that behavior explicitly. Capped results save the complete formatted list through the optional ctx.spillStore backend; returned locators are follow-up-readable/searchable when the backend exposes local paths in co-located deployments.',
  235. },
  236. {
  237. pkg: '@deepseek-ai/dsh-tool-pty',
  238. dir: 'tool-pty',
  239. source: 'packages/pty/tool-pty/src/index.ts',
  240. requires: ['ctx.tools', 'ctx.pty', 'ctx.systemPrompt', 'ctx.tasks at call time for run_in_background'],
  241. writes: ['tool/call', 'tool/result'],
  242. async mount(ctx) {
  243. await ctx.plugin(PtyService)
  244. await ctx.plugin(ToolPty)
  245. },
  246. note:
  247. 'The six terminal tools are opt-in and complement one-shot bash/filesystem tools. `terminal_send(run_in_background: true)` registers with `ctx.tasks`; TUI, named key sequences, BEL, resize, auto-start, and cross-agent sharing are absent from the schema.',
  248. },
  249. {
  250. pkg: '@deepseek-ai/dsh-tool-goal',
  251. dir: 'tool-goal',
  252. source: 'packages/goal/tool-goal/src/index.ts',
  253. requires: ['ctx.tools', 'ctx.agents', 'ctx.goals', 'ctx.systemPrompt', 'a calling Agent in an authorized open turn'],
  254. writes: ['tool/call', 'user/message goal snapshot for mutations', 'tool/result'],
  255. async mount(ctx) {
  256. await ctx.plugin(AgentRegistry)
  257. await ctx.plugin(GoalService)
  258. await ctx.plugin(ToolGoal)
  259. },
  260. note:
  261. 'create, edit, pause, and resume require direct-human root authority; complete and blocked also accept the exact current goal round. The default blocked lower bound is three admitted rounds.',
  262. },
  263. {
  264. pkg: '@deepseek-ai/dsh-tool-lsp',
  265. dir: 'tool-lsp',
  266. source: 'packages/lsp/tool-lsp/src/index.ts',
  267. requires: ['ctx.tools', 'ctx.lsp', 'ctx.systemPrompt'],
  268. writes: ['tool/call', 'tool/result'],
  269. async mount(ctx) {
  270. // The tool registers from the seam alone; the schema does not depend on any provider.
  271. await ctx.plugin(Lsp)
  272. await ctx.plugin(ToolLsp)
  273. },
  274. note:
  275. 'The lsp tool keeps provider selection and language-server subprocesses behind ctx.lsp, so its model-visible schema stays stable across providers. Requires a registered provider (e.g. `@deepseek-ai/dsh-lsp-local`) at runtime; without one, a query returns the structured `LSP_UNAVAILABLE` error rather than changing the schema.',
  276. },
  277. {
  278. pkg: '@deepseek-ai/dsh-tool-ralph',
  279. dir: 'tool-ralph',
  280. source: 'packages/workflow/tool-ralph/src/index.ts',
  281. requires: ['ctx.tools', 'ctx.workflows', 'ctx.subagents', 'ctx.systemPrompt', 'a calling Agent (exec.agent parents every fresh round)'],
  282. writes: ['tool/call', 'tool/result', 'workflow and child session events during execution'],
  283. async mount(ctx) {
  284. await ctx.plugin(SubagentService)
  285. registerCatalogSubagentProvider(ctx, 'mock')
  286. await ctx.plugin(VmWorkflowEngine, { provider: 'mock' })
  287. await ctx.plugin(ToolRalph, { subagentProvider: 'mock' })
  288. },
  289. note:
  290. 'A fixed foreground workflow starts one fresh structured child per round; the model selects only the immutable objective and an optional round cap.',
  291. },
  292. {
  293. pkg: '@deepseek-ai/dsh-tool-skill',
  294. dir: 'tool-skill',
  295. source: 'packages/skill/tool-skill/src/index.ts',
  296. requires: ['ctx.tools', 'ctx.agents', 'ctx.skills'],
  297. writes: ['tool/call', 'tool/result', 'user/message replacement catalogs via agent.inject()'],
  298. async mount(ctx) {
  299. await ctx.plugin(AgentRegistry)
  300. await ctx.plugin(SkillService)
  301. await ctx.plugin(SkillLocal, {
  302. dshHome: resolve(root, '.tmp/tool-catalog/.dsh'),
  303. agentsHome: resolve(root, '.tmp/tool-catalog/.agents'),
  304. })
  305. await ctx.plugin(ToolSkill)
  306. },
  307. },
  308. {
  309. pkg: '@deepseek-ai/dsh-tool-session-query',
  310. dir: 'tool-session-query',
  311. source: 'packages/session-query/tool-session-query/src/index.ts',
  312. requires: ['ctx.tools', 'ctx.systemPrompt', 'ctx.sessionQuery', 'a calling Agent for workspace authority'],
  313. writes: ['tool/call', 'tool/result'],
  314. async mount(ctx) {
  315. await ctx.plugin(SessionStore)
  316. await ctx.plugin(SessionQuerySqlite, { path: ':memory:' })
  317. await ctx.plugin(ToolSessionQuery)
  318. },
  319. note:
  320. 'The five read-only tools hide provider cursors and authorize every result from the immutable calling agent session. The package is opt-in; compositions that need enforced deadlines or bounded inline output also mount the generic timeout or spill policies.',
  321. },
  322. {
  323. pkg: '@deepseek-ai/dsh-tool-subagent',
  324. dir: 'tool-subagent',
  325. source: 'packages/subagent/tool-subagent/src/index.ts',
  326. requires: ['ctx.tools', 'ctx.subagents'],
  327. writes: ['tool/call', 'tool/result', 'child session events through the chosen provider'],
  328. shippedNames: ['subagent', 'subagent_fork'],
  329. async mount(ctx) {
  330. await ctx.plugin(SubagentService)
  331. registerCatalogSubagentProvider(ctx, 'mock')
  332. await ctx.plugin(ToolSubagent, { provider: 'mock' })
  333. },
  334. note:
  335. 'The registered tool name is the load-time `toolName` config (default `subagent`); the schema above is that default. The shipped example agents load this package once per subagent backend, so the model additionally sees `subagent_fork` (bound to the fork backend) with an identical schema — see `apps/cli/config/base.cordis.yml` and `examples/acp-agent/cordis.yml`.',
  336. },
  337. {
  338. pkg: '@deepseek-ai/dsh-tool-tasks',
  339. dir: 'tool-tasks',
  340. source: 'packages/tasks/tool-tasks/src/index.ts',
  341. requires: ['ctx.tools', 'ctx.tasks', 'ctx.systemPrompt'],
  342. writes: ['tool/call', 'tool/result', 'user/message via agent.inject() for background completion notices'],
  343. async mount(ctx) {
  344. await ctx.plugin(LocalTaskService)
  345. await ctx.plugin(ToolTasks)
  346. },
  347. note:
  348. 'The kind-agnostic background-task control surface: background bash commands, PTY sends, and subagents are read, listed, and killed through the same three tools. Loading the plugin attaches the control surface that arms producers\' `ctx.tasks.start()`.',
  349. },
  350. {
  351. pkg: '@deepseek-ai/dsh-tool-todo',
  352. dir: 'tool-todo',
  353. source: 'packages/todo/tool-todo/src/index.ts',
  354. requires: ['ctx.tools', 'owning Agent session'],
  355. writes: ['tool/call', 'todo/write', 'tool/result'],
  356. async mount(ctx) {
  357. await ctx.plugin(ToolTodo)
  358. },
  359. note:
  360. 'todo_write is session-owned state; UIs render the latest todo/write event as a checklist.',
  361. },
  362. {
  363. pkg: '@deepseek-ai/dsh-tool-workflow',
  364. dir: 'tool-workflow',
  365. source: 'packages/workflow/tool-workflow/src/index.ts',
  366. requires: ['ctx.tools', 'ctx.workflows', 'ctx.systemPrompt', 'a calling Agent (exec.agent parents the script children)'],
  367. writes: ['tool/call', 'tool/result'],
  368. async mount(ctx) {
  369. // The tool injects `workflows`; boot the vm engine over a scripted
  370. // subagent provider to satisfy it. The schema does not depend on which
  371. // provider backs the engine.
  372. await ctx.plugin(SubagentService)
  373. registerCatalogSubagentProvider(ctx, 'mock')
  374. await ctx.plugin(VmWorkflowEngine, { provider: 'mock' })
  375. await ctx.plugin(ToolWorkflow)
  376. },
  377. },
  378. {
  379. pkg: '@deepseek-ai/dsh-tool-web',
  380. dir: 'tool-web',
  381. source: 'packages/web/tool-web/src/index.ts',
  382. requires: ['ctx.tools', 'ctx.web', 'ctx.systemPrompt'],
  383. writes: ['tool/call', 'tool/result'],
  384. async mount(ctx) {
  385. // Mount search and fetch providers so both tools register. Their schemas
  386. // do not depend on provider identity or availability.
  387. await ctx.plugin(WebService)
  388. await ctx.plugin(WebSearchExa)
  389. await ctx.plugin(WebFetchLocal)
  390. await ctx.plugin(ToolWeb)
  391. },
  392. note:
  393. 'web_search and web_fetch keep provider selection behind ctx.web so model-visible schemas stay stable across backend swaps.',
  394. },
  395. ]
  396. /** One package's contribution to the catalog: its schemas plus attribution. */
  397. interface CatalogPackage {
  398. pkg: string
  399. source: string
  400. requires: string[]
  401. writes: string[]
  402. shippedNames?: string[]
  403. schemas: ToolSchema[]
  404. /** A deployment note (see {@link ToolPackage.note}), rendered after the tools. */
  405. note?: string
  406. }
  407. /** The whole catalog: one entry per booted tool package, in manifest order. */
  408. export type ToolCatalog = CatalogPackage[]
  409. /**
  410. * Assert the boot manifest covers every shipped tool package on disk (a
  411. * `tool-*` leaf under `packages/`).
  412. * Booting has no source declaration to enumerate, so this glob restores the
  413. * "a new tool cannot be silently undocumented" guarantee: an unlisted package
  414. * fails the generator (and the freshness gate) until it is added to
  415. * {@link TOOL_PACKAGES}. Exported for a direct negative test.
  416. *
  417. * `scanRoot` defaults to the repo root; a test may point it at a fixture tree.
  418. */
  419. export function assertManifestComplete(packages: ToolPackage[] = TOOL_PACKAGES, scanRoot: string = root): void {
  420. const onDisk = globSync('packages/*/tool-*', { cwd: scanRoot }).map(p => basename(p)).sort()
  421. const listed = new Set(packages.map(p => p.dir))
  422. const missing = onDisk.filter(dir => !listed.has(dir))
  423. if (missing.length > 0) {
  424. throw new Error(
  425. `gen-tool-catalog: ${missing.length} tool package(s) not in the boot manifest: ${missing.join(', ')}. `
  426. + 'Add each to TOOL_PACKAGES in scripts/gen-tool-catalog.ts so its schema is catalogued.',
  427. )
  428. }
  429. }
  430. /**
  431. * Boot each tool package on a fresh Context and harvest its model-facing
  432. * schemas. A fresh Context per package keeps attribution clean (each entry's
  433. * schemas come from exactly that package) and isolates a boot failure to its
  434. * own entry. Disposed after harvest so no executor/provider outlives the run.
  435. */
  436. export async function collectToolCatalog(packages: ToolPackage[] = TOOL_PACKAGES): Promise<ToolCatalog> {
  437. assertManifestComplete(packages)
  438. const catalog: ToolCatalog = []
  439. for (const entry of packages) {
  440. const ctx = new Context()
  441. // Dispose in `finally` so a throw from `mount`/`schemas()` after earlier
  442. // plugins mounted still tears the context down (no leaked executor/provider
  443. // fiber) — the repo's "dispose must reach quiescence" rule.
  444. try {
  445. await ctx.plugin(SystemPrompt)
  446. await ctx.plugin(ToolRegistry, entry.toolsConfig ?? {})
  447. await entry.mount(ctx)
  448. const schemas = ctx.tools.schemas().sort((a, b) => a.name.localeCompare(b.name))
  449. catalog.push({
  450. pkg: entry.pkg,
  451. source: entry.source,
  452. requires: entry.requires,
  453. writes: entry.writes,
  454. schemas,
  455. ...entry.shippedNames !== undefined ? { shippedNames: entry.shippedNames } : {},
  456. ...entry.note !== undefined ? { note: entry.note } : {},
  457. })
  458. } finally {
  459. await ctx.fiber.dispose()
  460. }
  461. }
  462. return catalog
  463. }
  464. /** Render one tool's entry: name, description, JSON-Schema parameters, source. */
  465. function renderTool(schema: ToolSchema, source: string): string[] {
  466. const out = [`### \`${schema.name}\``, '']
  467. if (schema.description) out.push(schema.description, '')
  468. out.push('```json', JSON.stringify(schema.parameters, null, 2), '```', '')
  469. out.push(`Source: [\`${source}\`](../${source})`, '')
  470. return out
  471. }
  472. function codeList(values: string[] | undefined): string {
  473. return values?.length ? values.map(value => `\`${value}\``).join(', ') : '-'
  474. }
  475. function tableCell(value: string | undefined): string {
  476. return value ? value.replace(/\|/g, '\\|').replace(/\n/g, '<br>') : '-'
  477. }
  478. /** Render the full catalog (pure, deterministic given the manifest-ordered input). */
  479. export function render(catalog: ToolCatalog): string {
  480. const lines: string[] = [
  481. '<!-- Generated by scripts/gen-tool-catalog.ts — do not edit by hand.',
  482. ' Run `pnpm run gen-tool-catalog` to regenerate. -->',
  483. '',
  484. '# Tool Schema Catalog',
  485. '',
  486. 'Every model-facing tool a shipped plugin contributes to `ctx.tools`: the `name`, `description`, and JSON-Schema `parameters` the model receives via the system-prompt assembly. It complements the cordis [events](cordis-catalog/events.md) & [services](cordis-catalog/services.md) catalogs (the wiring a plugin listens to and calls) and [core-data-structures/](core-data-structures/core.md) (the types those signatures move) — this page is the *tools* the agent is offered.',
  487. '',
  488. 'This file is GENERATED and verified fresh by `pnpm run verify-tool-catalog` (part of `doc-sync`) — do not edit it by hand. Unlike the cordis catalog (a pure source-AST pass), this generator BOOTS each tool plugin on a real context and reads `ctx.tools.schemas()`, because a tool schema is not statically knowable (runtime-spread enums, concatenated descriptions, config-driven names, raw-JSON-Schema MCP tools). A completeness guard globs `packages/*/tool-*` and fails if any package is missing from the generator\'s boot manifest, so a new tool cannot be silently undocumented. See [the tool-schema-catalog Agent Note](../.agents/notes/implemented/process/2026-07-02-tool-schema-catalog.md).',
  489. '',
  490. 'Scope: shipped product tools under `packages/*/tool-*`, each booted with its DEFAULT config. The registered tool NAME can be a load-time config (e.g. `tool-subagent`\'s `toolName`), so a deployment may surface a package under a different or additional name — a per-package note records those shipped aliases where they exist. The `examples/` demo tools (e.g. `echo`) are excluded, matching the cordis catalog\'s packages-only scope.',
  491. '',
  492. '## Tool Package Map',
  493. '',
  494. 'This table connects model-visible tool names to the plugin package and service seams behind them. Exact JSON Schemas follow in the package sections below.',
  495. '',
  496. '| Tool package | Model-visible names | Requires | Writes / affects | Shipped aliases | Deployment note |',
  497. '| --- | --- | --- | --- | --- | --- |',
  498. ...catalog.map(entry => `| \`${entry.pkg}\` | ${codeList(entry.schemas.map(schema => schema.name))} | ${codeList(entry.requires)} | ${codeList(entry.writes)} | ${codeList(entry.shippedNames)} | ${tableCell(entry.note)} |`),
  499. '',
  500. ]
  501. for (const entry of catalog) {
  502. lines.push(`## \`${entry.pkg}\``, '')
  503. for (const schema of entry.schemas) lines.push(...renderTool(schema, entry.source))
  504. if (entry.note) lines.push(entry.note, '')
  505. }
  506. return lines.join('\n')
  507. }
  508. /** CLI entry: default writes the catalog, `--check` fails if the committed copy
  509. * is stale. Guarded behind an entry-point check so importing this module for
  510. * tests neither regenerates the committed file nor calls process.exit. */
  511. async function main(): Promise<void> {
  512. const content = render(await collectToolCatalog())
  513. if (process.argv.includes('--check')) {
  514. let committed: string | null = null
  515. try {
  516. committed = readFileSync(resolve(root, OUT), 'utf8')
  517. } catch {
  518. // Only ENOENT (not yet generated) is expected; a present-but-unreadable
  519. // file is not a state this repo produces. Either way the remedy is the
  520. // same — regenerate — so treat a read failure as "stale".
  521. committed = null
  522. }
  523. if (committed === content) {
  524. console.log(`gen-tool-catalog: ${OUT} is up to date.`)
  525. process.exit(0)
  526. }
  527. console.error(`gen-tool-catalog: ${OUT} is stale. Run \`pnpm run gen-tool-catalog\` and commit ${OUT}.`)
  528. process.exit(1)
  529. }
  530. writeFileSync(resolve(root, OUT), content)
  531. console.log(`gen-tool-catalog: wrote ${OUT}.`)
  532. }
  533. // Run only when invoked as a script, not when imported by a test.
  534. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  535. await main()
  536. }