agency-lang 0.4.0 → 0.5.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 (539) hide show
  1. package/dist/lib/agents/agency-agent/agent.agency +416 -128
  2. package/dist/lib/agents/agency-agent/agent.js +1716 -521
  3. package/dist/lib/agents/agency-agent/code.js +17 -15
  4. package/dist/lib/agents/agency-agent/images/images.js +1 -1
  5. package/dist/lib/agents/agency-agent/lib/config.agency +72 -0
  6. package/dist/lib/agents/agency-agent/lib/config.js +258 -0
  7. package/dist/lib/agents/agency-agent/lib/defaultPolicy.agency +63 -0
  8. package/dist/lib/agents/agency-agent/lib/defaultPolicy.js +236 -0
  9. package/dist/lib/agents/agency-agent/lib/utils.agency +57 -0
  10. package/dist/lib/agents/agency-agent/lib/utils.js +1017 -0
  11. package/dist/lib/agents/agency-agent/prompts/codeSample.js +1 -1
  12. package/dist/lib/agents/agency-agent/research.js +9 -9
  13. package/dist/lib/agents/agency-agent/shared.js +3 -2
  14. package/dist/lib/agents/agency-agent/subagents/code.agency +370 -26
  15. package/dist/lib/agents/agency-agent/subagents/code.js +663 -184
  16. package/dist/lib/agents/agency-agent/subagents/explorer.agency +166 -0
  17. package/dist/lib/agents/agency-agent/subagents/explorer.js +530 -0
  18. package/dist/lib/agents/agency-agent/subagents/oneShot.agency +49 -0
  19. package/dist/lib/agents/agency-agent/subagents/{plan.js → oneShot.js} +27 -165
  20. package/dist/lib/agents/agency-agent/subagents/oracle.agency +128 -0
  21. package/dist/lib/agents/agency-agent/subagents/oracle.js +503 -0
  22. package/dist/lib/agents/agency-agent/{research.agency → subagents/research.agency} +41 -5
  23. package/dist/lib/agents/agency-agent/subagents/research.js +581 -0
  24. package/dist/lib/agents/agency-agent/subagents/review.agency +171 -0
  25. package/dist/lib/agents/agency-agent/subagents/review.js +1548 -0
  26. package/dist/lib/agents/docs/appendix/advanced-types.md +1 -1
  27. package/dist/lib/agents/docs/appendix/agency-stdlib.md +1 -1
  28. package/dist/lib/agents/docs/appendix/agency-vs-typescript.md +1 -1
  29. package/dist/lib/agents/docs/appendix/appendix/advanced-types.md +26 -0
  30. package/dist/lib/agents/docs/appendix/appendix/agency-stdlib.md +25 -0
  31. package/dist/lib/agents/docs/appendix/appendix/agency-vs-typescript.md +14 -0
  32. package/dist/lib/agents/docs/appendix/appendix/builtins.md +61 -0
  33. package/dist/lib/agents/docs/appendix/appendix/callbacks.md +147 -0
  34. package/dist/lib/agents/docs/appendix/appendix/schema-parameter-injection.md +101 -0
  35. package/dist/lib/agents/docs/appendix/appendix/ts-helpers.md +511 -0
  36. package/dist/lib/agents/docs/appendix/appendix/vscode-extension.md +11 -0
  37. package/dist/lib/agents/docs/appendix/builtins.md +1 -1
  38. package/dist/lib/agents/docs/appendix/callbacks.md +1 -1
  39. package/dist/lib/agents/docs/appendix/schema-parameter-injection.md +1 -1
  40. package/dist/lib/agents/docs/appendix/ts-helpers.md +23 -1
  41. package/dist/lib/agents/docs/appendix/vscode-extension.md +1 -1
  42. package/dist/lib/agents/docs/cli/cli/agent.md +16 -0
  43. package/dist/lib/agents/docs/cli/cli/compile.md +21 -0
  44. package/dist/lib/agents/docs/cli/cli/coverage.md +112 -0
  45. package/dist/lib/agents/docs/cli/cli/debug.md +41 -0
  46. package/dist/lib/agents/docs/cli/cli/doc.md +98 -0
  47. package/dist/lib/agents/docs/cli/cli/editor-integration.md +44 -0
  48. package/dist/lib/agents/docs/cli/cli/eval-judge.md +72 -0
  49. package/dist/lib/agents/docs/cli/cli/eval.md +259 -0
  50. package/dist/lib/agents/docs/cli/cli/format.md +23 -0
  51. package/dist/lib/agents/docs/cli/cli/index.md +7 -0
  52. package/dist/lib/agents/docs/cli/cli/pack.md +56 -0
  53. package/dist/lib/agents/docs/cli/cli/policy.md +23 -0
  54. package/dist/lib/agents/docs/cli/cli/preprocess-and-ast.md +21 -0
  55. package/dist/lib/agents/docs/cli/cli/review.md +16 -0
  56. package/dist/lib/agents/docs/cli/cli/run.md +108 -0
  57. package/dist/lib/agents/docs/cli/cli/schedule.md +89 -0
  58. package/dist/lib/agents/docs/cli/cli/serve.md +52 -0
  59. package/dist/lib/agents/docs/cli/cli/test.md +44 -0
  60. package/dist/lib/agents/docs/cli/cli/trace-and-bundle.md +75 -0
  61. package/dist/lib/agents/docs/cli/cli/typecheck.md +19 -0
  62. package/dist/lib/agents/docs/cli/eval-judge.md +72 -0
  63. package/dist/lib/agents/docs/cli/eval.md +259 -0
  64. package/dist/lib/agents/docs/cli/test.md +0 -9
  65. package/dist/lib/agents/docs/guide/agency-config-file.md +1 -1
  66. package/dist/lib/agents/docs/guide/agents-101.md +1 -1
  67. package/dist/lib/agents/docs/guide/basic-syntax.md +41 -1
  68. package/dist/lib/agents/docs/guide/blocks.md +1 -1
  69. package/dist/lib/agents/docs/guide/checkpointing.md +1 -1
  70. package/dist/lib/agents/docs/guide/cli-args.md +190 -0
  71. package/dist/lib/agents/docs/guide/concurrency.md +60 -1
  72. package/dist/lib/agents/docs/guide/cross-thread-context.md +29 -1
  73. package/dist/lib/agents/docs/guide/error-handling.md +1 -1
  74. package/dist/lib/agents/docs/guide/execution-model.md +14 -1
  75. package/dist/lib/agents/docs/guide/functions.md +1 -1
  76. package/dist/lib/agents/docs/guide/getting-started.md +1 -1
  77. package/dist/lib/agents/docs/guide/global-vs-static.md +28 -1
  78. package/dist/lib/agents/docs/guide/guards.md +1 -1
  79. package/dist/lib/agents/docs/guide/guide/agency-config-file.md +10 -0
  80. package/dist/lib/agents/docs/guide/guide/agents-101.md +212 -0
  81. package/dist/lib/agents/docs/guide/guide/basic-syntax.md +218 -0
  82. package/dist/lib/agents/docs/guide/guide/blocks.md +104 -0
  83. package/dist/lib/agents/docs/guide/guide/checkpointing.md +72 -0
  84. package/dist/lib/agents/docs/guide/guide/cli-args.md +190 -0
  85. package/dist/lib/agents/docs/guide/guide/concurrency.md +151 -0
  86. package/dist/lib/agents/docs/guide/guide/cross-thread-context.md +228 -0
  87. package/dist/lib/agents/docs/guide/guide/error-handling.md +149 -0
  88. package/dist/lib/agents/docs/guide/guide/execution-model.md +130 -0
  89. package/dist/lib/agents/docs/guide/guide/functions.md +33 -0
  90. package/dist/lib/agents/docs/guide/guide/getting-started.md +42 -0
  91. package/dist/lib/agents/docs/guide/guide/global-vs-static.md +81 -0
  92. package/dist/lib/agents/docs/guide/guide/guards.md +185 -0
  93. package/dist/lib/agents/docs/guide/guide/handlers.md +292 -0
  94. package/dist/lib/agents/docs/guide/guide/imports-and-packages.md +160 -0
  95. package/dist/lib/agents/docs/guide/guide/interrupts.md +99 -0
  96. package/dist/lib/agents/docs/guide/guide/llm.md +180 -0
  97. package/dist/lib/agents/docs/guide/guide/mcp.md +295 -0
  98. package/dist/lib/agents/docs/guide/guide/memory.md +388 -0
  99. package/dist/lib/agents/docs/guide/guide/message-history-and-threads.md +88 -0
  100. package/dist/lib/agents/docs/guide/guide/nodes.md +35 -0
  101. package/dist/lib/agents/docs/guide/guide/observability.md +86 -0
  102. package/dist/lib/agents/docs/guide/guide/odds-and-ends.md +41 -0
  103. package/dist/lib/agents/docs/guide/guide/partial-application.md +162 -0
  104. package/dist/lib/agents/docs/guide/guide/pattern-matching.md +193 -0
  105. package/dist/lib/agents/docs/guide/guide/policies.md +60 -0
  106. package/dist/lib/agents/docs/guide/guide/schemas.md +86 -0
  107. package/dist/lib/agents/docs/guide/guide/serving.md +165 -0
  108. package/dist/lib/agents/docs/guide/guide/structured-interrupts.md +49 -0
  109. package/dist/lib/agents/docs/guide/guide/testing.md +27 -0
  110. package/dist/lib/agents/docs/guide/guide/troubleshooting.md +82 -0
  111. package/dist/lib/agents/docs/guide/guide/ts-interop.md +185 -0
  112. package/dist/lib/agents/docs/guide/guide/type-validation.md +329 -0
  113. package/dist/lib/agents/docs/guide/guide/types.md +196 -0
  114. package/dist/lib/agents/docs/guide/handlers.md +1 -1
  115. package/dist/lib/agents/docs/guide/imports-and-packages.md +1 -1
  116. package/dist/lib/agents/docs/guide/interrupts.md +1 -1
  117. package/dist/lib/agents/docs/guide/llm.md +1 -1
  118. package/dist/lib/agents/docs/guide/mcp.md +1 -1
  119. package/dist/lib/agents/docs/guide/memory.md +1 -1
  120. package/dist/lib/agents/docs/guide/message-history-and-threads.md +1 -1
  121. package/dist/lib/agents/docs/guide/nodes.md +1 -1
  122. package/dist/lib/agents/docs/guide/observability.md +1 -1
  123. package/dist/lib/agents/docs/guide/odds-and-ends.md +1 -1
  124. package/dist/lib/agents/docs/guide/partial-application.md +1 -1
  125. package/dist/lib/agents/docs/guide/pattern-matching.md +1 -1
  126. package/dist/lib/agents/docs/guide/policies.md +1 -1
  127. package/dist/lib/agents/docs/guide/schemas.md +1 -1
  128. package/dist/lib/agents/docs/guide/serving.md +1 -1
  129. package/dist/lib/agents/docs/guide/structured-interrupts.md +1 -1
  130. package/dist/lib/agents/docs/guide/testing.md +1 -1
  131. package/dist/lib/agents/docs/guide/troubleshooting.md +1 -1
  132. package/dist/lib/agents/docs/guide/ts-interop.md +1 -1
  133. package/dist/lib/agents/docs/guide/type-validation.md +1 -1
  134. package/dist/lib/agents/docs/guide/types.md +1 -1
  135. package/dist/lib/agents/judge.js +1 -1
  136. package/dist/lib/agents/judgePairwise.agency +41 -0
  137. package/dist/lib/agents/mutatePrompt.agency +38 -0
  138. package/dist/lib/agents/mutatePrompt.js +301 -0
  139. package/dist/lib/agents/policy/agent.js +53 -39
  140. package/dist/lib/agents/review/agent.js +3 -2
  141. package/dist/lib/analysis/interrupts.d.ts +36 -0
  142. package/dist/lib/analysis/interrupts.js +197 -0
  143. package/dist/lib/analysis/interrupts.test.js +392 -0
  144. package/dist/lib/backends/agencyGenerator.d.ts +9 -0
  145. package/dist/lib/backends/agencyGenerator.js +126 -33
  146. package/dist/lib/backends/agencyGenerator.test.js +141 -0
  147. package/dist/lib/backends/toolSchemaContribution.test.d.ts +1 -0
  148. package/dist/lib/backends/toolSchemaContribution.test.js +107 -0
  149. package/dist/lib/backends/typescriptBuilder/namedArgsResolver.js +36 -7
  150. package/dist/lib/backends/typescriptBuilder/namedArgsResolver.test.js +19 -2
  151. package/dist/lib/backends/typescriptBuilder/sectionAssembler.js +8 -5
  152. package/dist/lib/backends/typescriptBuilder.d.ts +27 -0
  153. package/dist/lib/backends/typescriptBuilder.integration.test.js +18 -0
  154. package/dist/lib/backends/typescriptBuilder.js +132 -23
  155. package/dist/lib/backends/typescriptGenerator/typeToString.js +9 -2
  156. package/dist/lib/cli/agent.d.ts +1 -1
  157. package/dist/lib/cli/agent.js +2 -2
  158. package/dist/lib/cli/commands.d.ts +2 -0
  159. package/dist/lib/cli/commands.js +87 -51
  160. package/dist/lib/cli/doc.js +15 -4
  161. package/dist/lib/cli/doc.test.js +53 -7
  162. package/dist/lib/cli/eval/optimize.d.ts +27 -0
  163. package/dist/lib/cli/eval/optimize.js +96 -0
  164. package/dist/lib/cli/eval/optimize.test.d.ts +1 -0
  165. package/dist/lib/cli/eval/optimize.test.js +172 -0
  166. package/dist/lib/cli/eval/run.d.ts +51 -0
  167. package/dist/lib/cli/eval/run.js +192 -0
  168. package/dist/lib/cli/eval/run.test.d.ts +1 -0
  169. package/dist/lib/cli/eval/run.test.js +125 -0
  170. package/dist/lib/cli/evalExtract.d.ts +8 -0
  171. package/dist/lib/cli/evalExtract.js +28 -0
  172. package/dist/lib/cli/evalJudge.d.ts +10 -0
  173. package/dist/lib/cli/evalJudge.js +99 -0
  174. package/dist/lib/cli/evalJudge.test.d.ts +1 -0
  175. package/dist/lib/cli/evalJudge.test.js +161 -0
  176. package/dist/lib/cli/help.js +1 -0
  177. package/dist/lib/cli/interrupts.d.ts +9 -0
  178. package/dist/lib/cli/interrupts.js +48 -0
  179. package/dist/lib/cli/interrupts.test.d.ts +1 -0
  180. package/dist/lib/cli/interrupts.test.js +67 -0
  181. package/dist/lib/cli/runAgencyAgent.d.ts +33 -0
  182. package/dist/lib/cli/runAgencyAgent.js +112 -0
  183. package/dist/lib/cli/runAgencyAgent.test.d.ts +1 -0
  184. package/dist/lib/cli/runAgencyAgent.test.js +108 -0
  185. package/dist/lib/cli/runBundledAgent.js +1 -1
  186. package/dist/lib/cli/serve.js +1 -1
  187. package/dist/lib/cli/test.js +1 -0
  188. package/dist/lib/cli/util.d.ts +7 -3
  189. package/dist/lib/cli/util.js +44 -31
  190. package/dist/lib/compilationUnit.d.ts +17 -0
  191. package/dist/lib/compilationUnit.js +3 -0
  192. package/dist/lib/config.d.ts +9 -0
  193. package/dist/lib/config.js +6 -0
  194. package/dist/lib/config.test.js +9 -0
  195. package/dist/lib/eval/extract.d.ts +12 -0
  196. package/dist/lib/eval/extract.js +355 -0
  197. package/dist/lib/eval/extract.test.d.ts +1 -0
  198. package/dist/lib/eval/extract.test.js +366 -0
  199. package/dist/lib/eval/ids.d.ts +2 -0
  200. package/dist/lib/eval/ids.js +12 -0
  201. package/dist/lib/eval/judge/pairwise.d.ts +10 -0
  202. package/dist/lib/eval/judge/pairwise.js +113 -0
  203. package/dist/lib/eval/judge/pairwise.test.d.ts +1 -0
  204. package/dist/lib/eval/judge/pairwise.test.js +194 -0
  205. package/dist/lib/eval/judge/selectFinalResponse.d.ts +6 -0
  206. package/dist/lib/eval/judge/selectFinalResponse.js +20 -0
  207. package/dist/lib/eval/judge/selectFinalResponse.test.d.ts +1 -0
  208. package/dist/lib/eval/judge/selectFinalResponse.test.js +49 -0
  209. package/dist/lib/eval/judge/suite.d.ts +21 -0
  210. package/dist/lib/eval/judge/suite.js +154 -0
  211. package/dist/lib/eval/judge/suite.test.d.ts +1 -0
  212. package/dist/lib/eval/judge/suite.test.js +139 -0
  213. package/dist/lib/eval/judge/types.d.ts +65 -0
  214. package/dist/lib/eval/judge/types.js +1 -0
  215. package/dist/lib/eval/loadTasks.d.ts +6 -0
  216. package/dist/lib/eval/loadTasks.js +81 -0
  217. package/dist/lib/eval/loadTasks.test.d.ts +1 -0
  218. package/dist/lib/eval/loadTasks.test.js +60 -0
  219. package/dist/lib/eval/normalize.d.ts +40 -0
  220. package/dist/lib/eval/normalize.js +60 -0
  221. package/dist/lib/eval/normalize.test.d.ts +1 -0
  222. package/dist/lib/eval/normalize.test.js +125 -0
  223. package/dist/lib/eval/parseJsonl.d.ts +20 -0
  224. package/dist/lib/eval/parseJsonl.js +57 -0
  225. package/dist/lib/eval/parseJsonl.test.d.ts +1 -0
  226. package/dist/lib/eval/parseJsonl.test.js +72 -0
  227. package/dist/lib/eval/readRun.d.ts +13 -0
  228. package/dist/lib/eval/readRun.js +42 -0
  229. package/dist/lib/eval/readRun.test.d.ts +1 -0
  230. package/dist/lib/eval/readRun.test.js +82 -0
  231. package/dist/lib/eval/runArtifacts.d.ts +43 -0
  232. package/dist/lib/eval/runArtifacts.js +131 -0
  233. package/dist/lib/eval/runArtifacts.test.d.ts +1 -0
  234. package/dist/lib/eval/runArtifacts.test.js +131 -0
  235. package/dist/lib/eval/runEvalTask.d.ts +55 -0
  236. package/dist/lib/eval/runEvalTask.js +63 -0
  237. package/dist/lib/eval/runTypes.d.ts +36 -0
  238. package/dist/lib/eval/runTypes.js +1 -0
  239. package/dist/lib/eval/statelogParser.d.ts +26 -0
  240. package/dist/lib/eval/statelogParser.js +74 -0
  241. package/dist/lib/eval/statelogParser.test.d.ts +1 -0
  242. package/dist/lib/eval/statelogParser.test.js +78 -0
  243. package/dist/lib/eval/types.d.ts +193 -0
  244. package/dist/lib/eval/types.js +1 -0
  245. package/dist/lib/formatter.test.js +113 -0
  246. package/dist/lib/importPaths.d.ts +9 -0
  247. package/dist/lib/importPaths.js +31 -0
  248. package/dist/lib/importPaths.test.js +35 -1
  249. package/dist/lib/ir/builders.d.ts +24 -10
  250. package/dist/lib/ir/builders.js +33 -13
  251. package/dist/lib/ir/prettyPrint.js +15 -7
  252. package/dist/lib/ir/prettyPrint.test.js +1 -1
  253. package/dist/lib/logsViewer/conversation.js +3 -2
  254. package/dist/lib/logsViewer/conversation.test.js +14 -8
  255. package/dist/lib/logsViewer/render.js +13 -1
  256. package/dist/lib/logsViewer/render.test.js +2 -1
  257. package/dist/lib/logsViewer/summary.js +68 -8
  258. package/dist/lib/logsViewer/types.d.ts +2 -13
  259. package/dist/lib/mcp/server.js +1 -1
  260. package/dist/lib/mcp/server.test.js +1 -1
  261. package/dist/lib/optimize/artifacts.d.ts +46 -0
  262. package/dist/lib/optimize/artifacts.js +178 -0
  263. package/dist/lib/optimize/artifacts.test.d.ts +1 -0
  264. package/dist/lib/optimize/artifacts.test.js +174 -0
  265. package/dist/lib/optimize/history.d.ts +16 -0
  266. package/dist/lib/optimize/history.js +29 -0
  267. package/dist/lib/optimize/history.test.d.ts +1 -0
  268. package/dist/lib/optimize/history.test.js +37 -0
  269. package/dist/lib/optimize/loop.d.ts +24 -0
  270. package/dist/lib/optimize/loop.js +327 -0
  271. package/dist/lib/optimize/loop.test.d.ts +1 -0
  272. package/dist/lib/optimize/loop.test.js +377 -0
  273. package/dist/lib/optimize/mutator.d.ts +39 -0
  274. package/dist/lib/optimize/mutator.js +114 -0
  275. package/dist/lib/optimize/mutator.test.d.ts +1 -0
  276. package/dist/lib/optimize/mutator.test.js +129 -0
  277. package/dist/lib/optimize/reporter.d.ts +59 -0
  278. package/dist/lib/optimize/reporter.js +123 -0
  279. package/dist/lib/optimize/reporter.test.d.ts +1 -0
  280. package/dist/lib/optimize/reporter.test.js +207 -0
  281. package/dist/lib/optimize/sourceMutator.d.ts +127 -0
  282. package/dist/lib/optimize/sourceMutator.js +243 -0
  283. package/dist/lib/optimize/sourceMutator.test.d.ts +1 -0
  284. package/dist/lib/optimize/sourceMutator.test.js +524 -0
  285. package/dist/lib/optimize/targets.d.ts +56 -0
  286. package/dist/lib/optimize/targets.js +168 -0
  287. package/dist/lib/optimize/targets.test.d.ts +1 -0
  288. package/dist/lib/optimize/targets.test.js +255 -0
  289. package/dist/lib/optimize/tasks.d.ts +2 -0
  290. package/dist/lib/optimize/tasks.js +9 -0
  291. package/dist/lib/optimize/tasks.test.d.ts +1 -0
  292. package/dist/lib/optimize/tasks.test.js +17 -0
  293. package/dist/lib/optimize/types.d.ts +65 -0
  294. package/dist/lib/optimize/types.js +1 -0
  295. package/dist/lib/optimize/validation.d.ts +11 -0
  296. package/dist/lib/optimize/validation.js +64 -0
  297. package/dist/lib/optimize/validation.test.d.ts +1 -0
  298. package/dist/lib/optimize/validation.test.js +43 -0
  299. package/dist/lib/parser.d.ts +30 -1
  300. package/dist/lib/parser.js +102 -15
  301. package/dist/lib/parser.test.js +51 -0
  302. package/dist/lib/parsers/assignment.test.js +68 -0
  303. package/dist/lib/parsers/exportConst.test.js +11 -0
  304. package/dist/lib/parsers/forLoop.test.js +1 -0
  305. package/dist/lib/parsers/function.test.js +96 -1
  306. package/dist/lib/parsers/literals.test.js +161 -11
  307. package/dist/lib/parsers/objectTypeTrivia.test.d.ts +1 -0
  308. package/dist/lib/parsers/objectTypeTrivia.test.js +163 -0
  309. package/dist/lib/parsers/parsers.d.ts +1 -1
  310. package/dist/lib/parsers/parsers.js +322 -106
  311. package/dist/lib/parsers/typeHints.test.js +103 -0
  312. package/dist/lib/parsers/vitest.setup.js +11 -3
  313. package/dist/lib/preprocessors/parallelDesugar.js +14 -4
  314. package/dist/lib/runtime/__tests__/testHelpers.js +3 -0
  315. package/dist/lib/runtime/agency.d.ts +3 -1
  316. package/dist/lib/runtime/agency.js +40 -2
  317. package/dist/lib/runtime/agency.test.js +101 -1
  318. package/dist/lib/runtime/agencyFunction.d.ts +20 -0
  319. package/dist/lib/runtime/agencyFunction.js +61 -5
  320. package/dist/lib/runtime/agencyFunction.test.js +75 -2
  321. package/dist/lib/runtime/asyncContext.d.ts +30 -0
  322. package/dist/lib/runtime/asyncContext.js +27 -1
  323. package/dist/lib/runtime/asyncContext.test.js +5 -5
  324. package/dist/lib/runtime/configOverrides.d.ts +28 -0
  325. package/dist/lib/runtime/configOverrides.js +30 -0
  326. package/dist/lib/runtime/configOverrides.test.d.ts +1 -0
  327. package/dist/lib/runtime/configOverrides.test.js +26 -0
  328. package/dist/lib/runtime/deterministicClient.d.ts +21 -3
  329. package/dist/lib/runtime/deterministicClient.js +54 -9
  330. package/dist/lib/runtime/deterministicClient.test.js +76 -0
  331. package/dist/lib/runtime/index.d.ts +1 -1
  332. package/dist/lib/runtime/index.js +1 -1
  333. package/dist/lib/runtime/interrupts.js +20 -5
  334. package/dist/lib/runtime/ipc.configOverrides.test.d.ts +1 -0
  335. package/dist/lib/runtime/ipc.configOverrides.test.js +168 -0
  336. package/dist/lib/runtime/ipc.d.ts +54 -3
  337. package/dist/lib/runtime/ipc.js +203 -24
  338. package/dist/lib/runtime/lock.d.ts +11 -0
  339. package/dist/lib/runtime/lock.js +110 -0
  340. package/dist/lib/runtime/lock.test.d.ts +1 -0
  341. package/dist/lib/runtime/lock.test.js +85 -0
  342. package/dist/lib/runtime/node.js +14 -2
  343. package/dist/lib/runtime/prompt.js +37 -8
  344. package/dist/lib/runtime/promptRunner.js +10 -0
  345. package/dist/lib/runtime/resumableScope.js +7 -1
  346. package/dist/lib/runtime/runBatch.d.ts +25 -0
  347. package/dist/lib/runtime/runBatch.js +66 -18
  348. package/dist/lib/runtime/runner.d.ts +1 -1
  349. package/dist/lib/runtime/runner.js +47 -21
  350. package/dist/lib/runtime/state/context.d.ts +4 -0
  351. package/dist/lib/runtime/state/context.js +14 -0
  352. package/dist/lib/runtime/state/globalStore.d.ts +19 -0
  353. package/dist/lib/runtime/state/globalStore.js +21 -0
  354. package/dist/lib/runtime/state/messageThread.d.ts +6 -1
  355. package/dist/lib/runtime/state/messageThread.js +7 -1
  356. package/dist/lib/runtime/state/stateStack.d.ts +21 -1
  357. package/dist/lib/runtime/state/stateStack.js +21 -12
  358. package/dist/lib/runtime/state/threadStore.d.ts +88 -11
  359. package/dist/lib/runtime/state/threadStore.js +151 -24
  360. package/dist/lib/runtime/subprocess-bootstrap.js +8 -4
  361. package/dist/lib/runtime/toolBlockDiagnostics.d.ts +20 -0
  362. package/dist/lib/runtime/toolBlockDiagnostics.js +38 -0
  363. package/dist/lib/runtime/trace/traceWriter.js +1 -1
  364. package/dist/lib/runtime/validateToolForLLM.test.d.ts +1 -0
  365. package/dist/lib/runtime/validateToolForLLM.test.js +95 -0
  366. package/dist/lib/statelog/wireAccessors.d.ts +47 -0
  367. package/dist/lib/statelog/wireAccessors.js +113 -0
  368. package/dist/lib/statelog/wireAccessors.test.d.ts +1 -0
  369. package/dist/lib/statelog/wireAccessors.test.js +158 -0
  370. package/dist/lib/statelog/wireTypes.d.ts +13 -0
  371. package/dist/lib/statelog/wireTypes.js +1 -0
  372. package/dist/lib/statelogClient.d.ts +64 -5
  373. package/dist/lib/statelogClient.js +42 -5
  374. package/dist/lib/stdlib/agencyEval.d.ts +84 -0
  375. package/dist/lib/stdlib/agencyEval.js +154 -0
  376. package/dist/lib/stdlib/args.d.ts +115 -0
  377. package/dist/lib/stdlib/args.js +713 -0
  378. package/dist/lib/stdlib/cli.d.ts +2 -0
  379. package/dist/lib/stdlib/cli.js +94 -204
  380. package/dist/lib/stdlib/concurrency.d.ts +1 -0
  381. package/dist/lib/stdlib/concurrency.js +8 -0
  382. package/dist/lib/stdlib/layout/ansi.d.ts +15 -0
  383. package/dist/lib/stdlib/layout/ansi.js +167 -0
  384. package/dist/lib/stdlib/layout/axis.d.ts +4 -0
  385. package/dist/lib/stdlib/layout/axis.js +91 -0
  386. package/dist/lib/stdlib/layout/block.d.ts +16 -0
  387. package/dist/lib/stdlib/layout/block.js +103 -0
  388. package/dist/lib/stdlib/layout/border.d.ts +36 -0
  389. package/dist/lib/stdlib/layout/border.js +119 -0
  390. package/dist/lib/stdlib/layout/box.d.ts +3 -0
  391. package/dist/lib/stdlib/layout/box.js +25 -0
  392. package/dist/lib/stdlib/layout/nodes.d.ts +29 -0
  393. package/dist/lib/stdlib/layout/nodes.js +90 -0
  394. package/dist/lib/stdlib/layout/render.d.ts +18 -0
  395. package/dist/lib/stdlib/layout/render.js +174 -0
  396. package/dist/lib/stdlib/layout/table.d.ts +14 -0
  397. package/dist/lib/stdlib/layout/table.js +453 -0
  398. package/dist/lib/stdlib/layout.d.ts +21 -58
  399. package/dist/lib/stdlib/layout.js +27 -467
  400. package/dist/lib/stdlib/markdown.d.ts +2 -1
  401. package/dist/lib/stdlib/markdown.js +20 -10
  402. package/dist/lib/stdlib/statelog.d.ts +7 -0
  403. package/dist/lib/stdlib/statelog.js +62 -0
  404. package/dist/lib/stdlib/syntax.js +26 -10
  405. package/dist/lib/stdlib/system.d.ts +3 -0
  406. package/dist/lib/stdlib/system.js +13 -0
  407. package/dist/lib/stdlib/ui.d.ts +12 -10
  408. package/dist/lib/stdlib/ui.js +230 -62
  409. package/dist/lib/stdlib/version.d.ts +1 -0
  410. package/dist/lib/stdlib/version.js +1 -0
  411. package/dist/lib/templates/backends/typescriptGenerator/blockSetup.d.ts +1 -1
  412. package/dist/lib/templates/backends/typescriptGenerator/blockSetup.js +10 -1
  413. package/dist/lib/templates/backends/typescriptGenerator/imports.d.ts +1 -1
  414. package/dist/lib/templates/backends/typescriptGenerator/imports.js +4 -2
  415. package/dist/lib/typeChecker/builtinNamedArgs.test.d.ts +1 -0
  416. package/dist/lib/typeChecker/builtinNamedArgs.test.js +61 -0
  417. package/dist/lib/typeChecker/builtins.js +14 -2
  418. package/dist/lib/typeChecker/checker.d.ts +36 -0
  419. package/dist/lib/typeChecker/checker.js +166 -40
  420. package/dist/lib/typeChecker/index.d.ts +2 -0
  421. package/dist/lib/typeChecker/index.js +20 -1
  422. package/dist/lib/typeChecker/interruptAnalysis.d.ts +58 -0
  423. package/dist/lib/typeChecker/interruptAnalysis.js +115 -0
  424. package/dist/lib/typeChecker/interruptCallGraph.test.d.ts +1 -0
  425. package/dist/lib/typeChecker/interruptCallGraph.test.js +171 -0
  426. package/dist/lib/typeChecker/resolveCall.d.ts +3 -0
  427. package/dist/lib/typeChecker/resolveCall.js +12 -3
  428. package/dist/lib/typeChecker/scopes.js +7 -0
  429. package/dist/lib/typeChecker/synthesizer.js +98 -10
  430. package/dist/lib/typeChecker/toolBlockBinding.d.ts +27 -0
  431. package/dist/lib/typeChecker/toolBlockBinding.js +168 -0
  432. package/dist/lib/typeChecker/toolBlockBinding.test.d.ts +1 -0
  433. package/dist/lib/typeChecker/toolBlockBinding.test.js +286 -0
  434. package/dist/lib/typeChecker/toolBlockBindingHelpers.test.d.ts +1 -0
  435. package/dist/lib/typeChecker/toolBlockBindingHelpers.test.js +54 -0
  436. package/dist/lib/typeChecker/types.d.ts +29 -1
  437. package/dist/lib/typeChecker/utils.d.ts +19 -0
  438. package/dist/lib/typeChecker/utils.js +39 -0
  439. package/dist/lib/typeChecker/variadicNamedBinding.test.d.ts +1 -0
  440. package/dist/lib/typeChecker/variadicNamedBinding.test.js +176 -0
  441. package/dist/lib/typeChecker.test.js +32 -6
  442. package/dist/lib/types/literals.d.ts +1 -0
  443. package/dist/lib/types/parallelBlock.d.ts +5 -1
  444. package/dist/lib/types/typeHints.d.ts +28 -1
  445. package/dist/lib/types.d.ts +1 -0
  446. package/dist/lib/utils/diff.d.ts +8 -5
  447. package/dist/lib/utils/diff.js +11 -8
  448. package/dist/lib/utils/diff.test.js +10 -0
  449. package/dist/lib/utils/formatType.d.ts +11 -1
  450. package/dist/lib/utils/formatType.js +21 -5
  451. package/dist/lib/utils/termcolors.d.ts +2 -0
  452. package/dist/lib/utils/termcolors.js +1 -0
  453. package/dist/scripts/agency.d.ts +1 -0
  454. package/dist/scripts/agency.js +142 -28
  455. package/dist/scripts/agency.test.js +11 -1
  456. package/dist/scripts/regenerate-fixtures.js +36 -0
  457. package/package.json +3 -3
  458. package/stdlib/agency/eval.agency +422 -0
  459. package/stdlib/agency/eval.js +1347 -0
  460. package/stdlib/agency.agency +77 -31
  461. package/stdlib/agency.js +268 -75
  462. package/stdlib/agent.agency +48 -5
  463. package/stdlib/agent.js +396 -38
  464. package/stdlib/args.agency +118 -0
  465. package/stdlib/args.js +288 -0
  466. package/stdlib/array.agency +14 -14
  467. package/stdlib/array.js +69 -41
  468. package/stdlib/browser.js +17 -9
  469. package/stdlib/calendar.js +52 -29
  470. package/stdlib/cli.agency +16 -4
  471. package/stdlib/cli.js +275 -19
  472. package/stdlib/clipboard.js +6 -4
  473. package/stdlib/concurrency.agency +38 -0
  474. package/stdlib/concurrency.js +342 -0
  475. package/stdlib/date.js +73 -44
  476. package/stdlib/email.js +75 -39
  477. package/stdlib/fs.js +50 -29
  478. package/stdlib/http.js +29 -16
  479. package/stdlib/imessage.js +11 -6
  480. package/stdlib/index.agency +2 -0
  481. package/stdlib/index.js +89 -49
  482. package/stdlib/keyring.js +20 -12
  483. package/stdlib/layout.agency +308 -26
  484. package/stdlib/layout.js +1583 -134
  485. package/stdlib/markdown.agency +11 -12
  486. package/stdlib/markdown.js +22 -16
  487. package/stdlib/math.js +22 -13
  488. package/stdlib/memory.agency +46 -29
  489. package/stdlib/memory.js +163 -24
  490. package/stdlib/oauth.js +28 -16
  491. package/stdlib/object.agency +6 -6
  492. package/stdlib/object.js +27 -16
  493. package/stdlib/path.js +27 -17
  494. package/stdlib/policy.agency +127 -52
  495. package/stdlib/policy.js +627 -174
  496. package/stdlib/schemas.js +1 -1
  497. package/stdlib/search.agency +14 -3
  498. package/stdlib/search.js +52 -10
  499. package/stdlib/shell.agency +15 -11
  500. package/stdlib/shell.js +77 -51
  501. package/stdlib/skills.agency +247 -49
  502. package/stdlib/skills.js +1149 -271
  503. package/stdlib/sms.js +17 -9
  504. package/stdlib/speech.js +27 -15
  505. package/stdlib/statelog.agency +131 -0
  506. package/stdlib/statelog.js +1010 -0
  507. package/stdlib/strategy.agency +14 -6
  508. package/stdlib/strategy.js +36 -22
  509. package/stdlib/syntax.js +9 -5
  510. package/stdlib/system.agency +49 -7
  511. package/stdlib/system.js +379 -27
  512. package/stdlib/thread.agency +32 -25
  513. package/stdlib/thread.js +42 -32
  514. package/stdlib/threads.js +23 -14
  515. package/stdlib/types.js +1 -1
  516. package/stdlib/ui.agency +261 -44
  517. package/stdlib/ui.js +1619 -453
  518. package/stdlib/validators.js +45 -28
  519. package/stdlib/weather.agency +5 -0
  520. package/stdlib/weather.js +44 -9
  521. package/stdlib/wikipedia.agency +13 -0
  522. package/stdlib/wikipedia.js +96 -9
  523. package/dist/lib/agents/agency-agent/code.agency +0 -223
  524. package/dist/lib/agents/agency-agent/subagents/plan.agency +0 -34
  525. package/dist/lib/agents/agency-agent/subagents/task.agency +0 -37
  526. package/dist/lib/agents/agency-agent/subagents/task.js +0 -513
  527. package/dist/lib/agents/docs/cli/optimize.md +0 -18
  528. package/dist/lib/cli/evaluate.d.ts +0 -2
  529. package/dist/lib/cli/evaluate.js +0 -213
  530. package/dist/lib/cli/optimize.d.ts +0 -30
  531. package/dist/lib/cli/optimize.js +0 -225
  532. package/dist/lib/cli/optimize.test.js +0 -251
  533. package/dist/lib/cli/optimizerIO.d.ts +0 -28
  534. package/dist/lib/cli/optimizerIO.js +0 -84
  535. package/dist/lib/cli/testOptimizerIO.d.ts +0 -29
  536. package/dist/lib/cli/testOptimizerIO.js +0 -31
  537. package/dist/lib/version.d.ts +0 -1
  538. package/dist/lib/version.js +0 -1
  539. /package/dist/lib/{cli/optimize.test.d.ts → analysis/interrupts.test.d.ts} +0 -0
@@ -1,16 +1,40 @@
1
+ import { getVersion } from "std::agency"
1
2
  import { route } from "std::agent"
2
- import { clearMessages, pushMessage, repl } from "std::cli"
3
+ import { parseArgs } from "std::args"
4
+ import { clearMessages, pushMessage, repl, clearScreen } from "std::cli"
3
5
  import { today } from "std::date"
4
6
  import { box, render } from "std::layout"
5
- import { ScopedRuleFields, cliPolicyHandler } from "std::policy"
7
+ import { setMemoryId } from "std::memory"
8
+ import {
9
+ ScopedRuleFields,
10
+ cliPolicyHandler,
11
+ parsePolicyFile,
12
+ setPolicy,
13
+ } from "std::policy"
6
14
  import { exists } from "std::shell"
15
+ import { commandsDir, expandSlash } from "std::skills"
7
16
  import { highlight } from "std::syntax"
8
- import { cwd, env } from "std::system"
9
- import { getCost } from "std::thread"
17
+ import { cwd, env, isTTY, readStdin, setTitle } from "std::system"
18
+ import { getCost, getTokens, systemMessage } from "std::thread"
19
+ import { chooseOption, ChoiceItem } from "std::ui"
10
20
 
11
- import { codeSysPrompt, codeTools } from "./code.agency"
12
21
  import { figs, title } from "./images/images.agency"
13
- import { researchSysPrompt, researchTools } from "./research.agency"
22
+ import { POLICY_PATH, HISTORY_PATH, ALWAYS_FIELDS } from "./lib/config.agency"
23
+ import {
24
+ minimalAutoApprovePolicy,
25
+ recommendedAutoApprovePolicy,
26
+ } from "./lib/defaultPolicy.agency"
27
+ import { truncate, formatArgs, formatToolResponse } from "./lib/utils.agency"
28
+ import { codeSysPrompt, codeTools, codeAgent } from "./subagents/code.agency"
29
+ import { explorerAgent } from "./subagents/explorer.agency"
30
+ import { oneShotAgent } from "./subagents/oneShot.agency"
31
+ import { oracleAgent } from "./subagents/oracle.agency"
32
+ import {
33
+ researchSysPrompt,
34
+ researchTools,
35
+ researchAgent,
36
+ } from "./subagents/research.agency"
37
+ import { reviewAgent } from "./subagents/review.agency"
14
38
 
15
39
 
16
40
  // UI mode select — one-line swap between the alt-screen TUI and the
@@ -57,124 +81,68 @@ import { researchSysPrompt, researchTools } from "./research.agency"
57
81
  * editing `.agency` files.
58
82
  */
59
83
 
60
- // Verbose tool-call tracing, opt-in via `AGENT_DEBUG=1`.
61
- const AGENT_DEBUG = env("AGENT_DEBUG") == "1"
84
+ // Verbose tool-call tracing. Enabled either by `AGENT_DEBUG=1` (legacy)
85
+ // or by passing `--debug` / `--verbose` on the command line (set in main()).
86
+ let AGENT_DEBUG: boolean = env("AGENT_DEBUG") == "1"
87
+ let VERBOSE: boolean = false
88
+
89
+ // Whether the agent is running in interactive (TTY) mode. Set in main()
90
+ // before any tool calls happen, so the callbacks below can suppress
91
+ // scroll-output writes when stdout is destined for a Unix pipe.
92
+ let _isInteractive: boolean = true
62
93
 
63
94
  callback("onToolCallStart") as data {
64
- //if (AGENT_DEBUG) {
65
95
  pushMessage(color.yellow("⏺ ${data.toolName}(${formatArgs(data.args)})"))
66
- //}
67
96
  }
68
97
 
69
- def truncate(str: string, maxLength: number): string {
70
- if (str.length <= maxLength) {
71
- return str
98
+ callback("onToolCallEnd") as data {
99
+ if (data.result is success(_result)) {
100
+ pushMessage(color.dim.green("${formatToolResponse(_result)}"))
101
+ } else if (data.result is failure(_error)) {
102
+ pushMessage(color.red(" ⎿ Error: ${_error}"))
103
+ } else {
104
+ pushMessage(color.dim("${formatToolResponse(data.result)}"))
72
105
  }
73
- return str.slice(0, maxLength) + "..."
74
106
  }
75
107
 
76
- def formatArgs(args: Record<string, any>): string {
77
- const parts = []
78
- for (key in args) {
79
- const value = args[key]
80
- parts.push("${key}: ${truncate(JSON.stringify(value), 50)}")
108
+ callback("onLLMCallStart") as data {
109
+ if (AGENT_DEBUG || VERBOSE) {
110
+ pushMessage(color.green.dim("💬 [${data.model}] ${data.prompt}"))
81
111
  }
82
- return parts.join(", ")
83
112
  }
84
113
 
85
- callback("onToolCallEnd") as data {
86
- if (AGENT_DEBUG) {
114
+ callback("onLLMCallEnd") as data {
115
+ if (AGENT_DEBUG || VERBOSE) {
87
116
  pushMessage(
88
- color.yellow(
89
- "tool return: ${data.toolName} -> ${JSON.stringify(data.result)} ${data.timeTaken}ms",
90
- ),
117
+ color.green.dim("💬 [${data.model}] ${renderLLMCallResponse(data.result)}"),
91
118
  )
92
119
  }
93
120
  }
94
121
 
95
- // On-disk policy at `$HOME/.agency-agent/policy.json`. We duplicate the
96
- // home-dir computation here instead of importing `AGENCY_AGENT_DIR` from
97
- // `shared.agency` because Agency doesn't guarantee imported static-const
98
- // init runs before the importer reads it (upstream #232).
99
- const POLICY_FILE = "policy.json"
100
- const HISTORY_FILE = "history"
101
-
102
- def policyDir(): string {
103
- const home = env("HOME")
104
- if (home == null) {
105
- return "./.agency-agent"
106
- }
107
- return "${home}/.agency-agent"
122
+ type ToolCallData = {
123
+ name: string;
124
+ arguments: any
108
125
  }
109
126
 
110
- def policyPath(): string {
111
- return "${policyDir()}/${POLICY_FILE}"
127
+ type LLMResponse = {
128
+ output: string | null;
129
+ toolCalls: ToolCallData[];
130
+ usage: { inputTokens: number; outputTokens: number; totalTokens: number; cachedInputTokens: number };
131
+ cost: { inputCost: number; outputCost: number; cachedInputCost: number; totalCost: number; currency: string };
132
+ model: string
112
133
  }
113
134
 
114
- def historyPath(): string {
115
- return "${policyDir()}/${HISTORY_FILE}"
116
- }
117
-
118
- // Per-kind config driving the "approve-always-here" option. For each
119
- // kind we list the data fields the scoped rule should pin, plus a
120
- // flag that turns a directory value into a brace-expanded glob so the
121
- // rule matches `dir` AND `dir/**`. Kinds not present in this map
122
- // don't offer the option — the user falls back to (a)/(r)/(aa)/(rr).
123
- const ALWAYS_FIELDS: ScopedRuleFields = {
124
- "std::read": [{
125
- field: "dir",
126
- matchSubpaths: true
127
- }],
128
- "std::write": [{
129
- field: "dir",
130
- matchSubpaths: true
131
- }],
132
- "std::edit": [{
133
- field: "dir",
134
- matchSubpaths: true
135
- }],
136
- "std::ls": [{
137
- field: "dir",
138
- matchSubpaths: true
139
- }],
140
- "std::glob": [{
141
- field: "dir",
142
- matchSubpaths: true
143
- }],
144
- "std::grep": [{
145
- field: "dir",
146
- matchSubpaths: true
147
- }],
148
- "std::copy": [
149
- {
150
- field: "src",
151
- matchSubpaths: true
152
- },
153
- {
154
- field: "dest",
155
- matchSubpaths: true
156
- },
157
- ],
158
- "std::move": [
159
- {
160
- field: "src",
161
- matchSubpaths: true
162
- },
163
- {
164
- field: "dest",
165
- matchSubpaths: true
166
- },
167
- ],
168
- "std::exec": [
169
- {
170
- field: "command",
171
- matchSubpaths: false
172
- },
173
- {
174
- field: "subcommand",
175
- matchSubpaths: false
176
- },
177
- ]
135
+ def renderLLMCallResponse(data: LLMResponse): string {
136
+ let response = []
137
+ if (data.output != "" && data.output != null && data.output != undefined) {
138
+ response.push(data.output)
139
+ }
140
+ if (data.toolCalls && data.toolCalls.length > 0) {
141
+ for (toolCall in data.toolCalls) {
142
+ response.push(`${toolCall.name}(${formatArgs(toolCall.arguments)})`)
143
+ }
144
+ }
145
+ return response.join("\n")
178
146
  }
179
147
 
180
148
  def loadAgentsMd(dir: string): string {
@@ -199,45 +167,280 @@ def loadAgentsMd(dir: string): string {
199
167
  // LLM doesn't have to re-ground each turn. Initialized in `main()`.
200
168
  let _context: string = ""
201
169
 
170
+ // Project-local slash commands. `cwd()` works at module-init time so
171
+ // we anchor at the user's project root (not the agent's source dir).
172
+ // `with approve` auto-approves the `glob`/`read` during init — the
173
+ // user implicitly opted in by running the agent in this directory.
174
+ static const projectCommands = commandsDir("${cwd()}/.claude/commands") with approve
175
+
176
+ def builtinPalette(): Record<string, string> {
177
+ return {
178
+ "/exit": "Exit the agent",
179
+ "/clear": "Clear the conversation transcript",
180
+ "/cost": "Show cumulative LLM cost and tokens",
181
+ "/help": "Show available slash commands"
182
+ }
183
+ }
184
+
185
+ // Built-ins win over file commands so a user `/clear.md` can't shadow
186
+ // the built-in `/clear`. Build file entries first, then overlay builtins.
187
+ def mergedPalette(): Record<string, string> {
188
+ let out: Record<string, string> = {}
189
+ for (cmd in projectCommands) {
190
+ let label = cmd.description
191
+ if (cmd.argHint != "") {
192
+ label = "${cmd.description} ${cmd.argHint}"
193
+ }
194
+ out["/${cmd.name}"] = label
195
+ }
196
+ const builtins = builtinPalette()
197
+ for (key in builtins) {
198
+ out[key] = builtins[key]
199
+ }
200
+ return out
201
+ }
202
+
202
203
  // Slash-command + user-message dispatcher invoked by `repl()` for
203
204
  // every Enter keystroke. Return `false` to terminate the loop.
204
205
  def _runTurn(msg: string): boolean {
205
- if (msg == "") {
206
+ // Trim once for built-in matching so `"/help\n"` (piped) or
207
+ // `" /clear"` behave the same as `"/clear"`. `expandSlash` is
208
+ // whitespace-tolerant in its own right, but it still receives the
209
+ // raw `msg` so the LLM sees the exact text the user typed when
210
+ // nothing matches.
211
+ const trimmed = msg.trim()
212
+ if (trimmed == "") {
206
213
  return true
207
214
  }
208
- if (msg == "/exit" || msg == "/quit") {
215
+ if (trimmed == "/exit" || trimmed == "/quit") {
209
216
  return false
210
217
  }
211
- if (msg == "/clear") {
218
+ if (trimmed == "/clear") {
212
219
  clearMessages()
213
220
  return true
214
221
  }
215
- if (msg == "/help") {
216
- pushMessage("Commands: /exit, /clear, /help")
222
+ if (trimmed == "/help") {
223
+ pushMessage("Commands: /exit, /clear, /cost, /help")
217
224
  return true
218
225
  }
226
+ if (trimmed == "/cost") {
227
+ pushMessage("Cost: $${getCost().toFixed(4)}")
228
+ pushMessage("Tokens: ${getTokens()}")
229
+ return true
230
+ }
231
+ // Expand `/foo args` to the rendered command body. Unknown `/foo`
232
+ // inputs fall through unchanged — matches Claude Code.
233
+ const prompt = expandSlash(msg, projectCommands)
234
+ const reply = mainAgent(prompt)
235
+ if (reply != "" && reply != null && reply != undefined) {
236
+ pushMessage(highlight("${reply}\n", language: "markdown"))
237
+ } else {
238
+ pushMessage(color.red("No reply generated."))
239
+ }
240
+ return true
241
+ }
242
+
243
+ let first = true
244
+ static const mainAgentSystemPrompt = """
245
+ You are the top-level coordinator of an Agency-language assistant. You
246
+ receive every user message and decide how to respond.
247
+
248
+ You have five subagent tools, each running in its own isolated
249
+ context:
250
+
251
+ - `codeAgent(userMsg)` — anything that touches code or the filesystem:
252
+ reading, writing, editing, typechecking, running shell commands,
253
+ and answering Agency syntax / CLI questions. Use this for any task
254
+ that involves inspecting or modifying source code.
255
+ - `researchAgent(userMsg)` — web search, URL fetches, Wikipedia,
256
+ external API lookups, summarizing external content.
257
+ - `reviewAgent(userMsg)` — reviews Agency code for syntax and type
258
+ errors. Call this after `codeAgent` produces non-trivial new or
259
+ modified Agency code, passing the code to be reviewed.
260
+ - `oracleAgent(userMsg)` — a read-only senior reviewer on a stronger
261
+ reasoning model. Read the **Oracle** section below — you are
262
+ expected to use this tool **frequently**.
263
+ - `explorerAgent(userMsg)` — a read-only researcher that produces
264
+ broad, synthesizing answers about the codebase or bundled docs.
265
+ Read the **Explorer** section below.
266
+
267
+ **Answer directly (no tool call) when** the message is conversational,
268
+ a clarifying question, or something you can answer from context alone.
269
+
270
+ **Delegate when** the message clearly needs one of the subagents'
271
+ capabilities. Pick one based on what the message actually needs —
272
+ don't pre-emptively call multiple subagents.
273
+
274
+ ## Picking between oracle, explorer, and code agent
275
+
276
+ These three overlap on "reads the codebase," but they're for
277
+ different jobs. Pick by **the shape of the answer the user wants**:
278
+
279
+ - **oracle** → sharp verdict on a specific plan, diff, or bug.
280
+ Output: short, decisive ("this plan won't work because X").
281
+ - **explorer** → broad synthesis from reading many files.
282
+ Output: structured overview ("Agency's five main features
283
+ are..."). Use for "summarize", "tour", "what are the main",
284
+ "how does X work across...".
285
+ - **codeAgent** → targeted action: edit, run, typecheck, or
286
+ answer a focused Agency-syntax question. Output: the edit
287
+ itself, or a concise factual reply.
288
+
289
+ Default: if the user asks a broad/synthesizing question,
290
+ `explorerAgent` beats `codeAgent` every time. The code agent is
291
+ optimized for terse action and will under-read on broad asks.
292
+
293
+ Subagents return summary text. Surface that result to the user (in
294
+ your own words if you're combining multiple results), formatted as
295
+ Markdown.
296
+
297
+ ## Oracle
298
+
299
+ The oracle is the most powerful tool you have. Use it **FREQUENTLY**.
300
+ It is a read-only senior reviewer running on a stronger reasoning
301
+ model than you. It can read the codebase and the Agency docs but
302
+ cannot write, edit, or run anything. Its job is to **think hard so
303
+ you don't have to guess**.
304
+
305
+ Call the oracle:
306
+
307
+ - **Before** dispatching `codeAgent` for any non-trivial task — ask
308
+ the oracle to sanity-check the plan, find existing code that
309
+ already solves the problem, or suggest a simpler approach.
310
+ - **After** `codeAgent` produces a non-trivial diff — ask the oracle
311
+ to review the work for correctness, missed edge cases, and
312
+ better alternatives.
313
+ - When `codeAgent` reports it's stuck on a bug after one or two
314
+ attempts — ask the oracle what's actually going on.
315
+ - When the user proposes a plan or approach — ask the oracle
316
+ whether the plan is sound, **before** you act on it. If the
317
+ oracle finds a flaw, surface that to the user *before* taking
318
+ any action.
319
+ - Whenever you're tempted to guess about the codebase ("I think
320
+ there's probably already a helper for X") — ask the oracle to
321
+ look.
322
+
323
+ **Tell the user when you're consulting the oracle.** Say something
324
+ like "Let me ask the oracle to review this plan before we start"
325
+ or "I'll have the oracle look for an existing implementation." The
326
+ visibility is half the value — the user benefits from knowing a
327
+ second opinion is being sought.
328
+
329
+ Pass the oracle a self-contained question with full context. It
330
+ does not see your conversation. Include the user's request, the
331
+ plan or diff under review, relevant file paths, and the specific
332
+ question you want answered.
333
+
334
+ Default bias: when in doubt, **consult the oracle**. The cost of
335
+ asking is small; the cost of executing a flawed plan is large.
336
+
337
+ ## Explorer
338
+
339
+ The explorer is your go-to for **broad, synthesizing questions**
340
+ about the codebase or the bundled Agency docs. It is read-only and
341
+ runs on a stronger reasoning model with extended thinking. Its job
342
+ is **coverage** — it reads widely and returns a structured synthesis.
343
+
344
+ Call the explorer when the user asks:
345
+
346
+ - "Summarize the Agency docs" / "What are the main features?"
347
+ - "Give me a tour of `lib/X/`" / "What's in this module?"
348
+ - "How does X work across the codebase?"
349
+ - "Walk me through the compilation pipeline"
350
+ - Anything where a good answer requires reading 5+ files and
351
+ organizing the findings by theme.
352
+
353
+ Do NOT use the explorer for:
354
+ - Specific factual lookups ("what does function X return?") — use
355
+ `codeAgent`.
356
+ - Plan critique or bug diagnosis — use `oracleAgent`.
357
+ - External / web research — use `researchAgent`.
358
+
359
+ **Tell the user when you're consulting the explorer.** "Let me have
360
+ the explorer go through the docs and put together an overview." Like
361
+ with the oracle, the visibility is part of the value — the user
362
+ benefits from knowing breadth is being applied.
363
+
364
+ Pass the explorer a self-contained question with explicit scope
365
+ ("all of `docs/site/guide/`", "the `lib/parsers/` module"). It does
366
+ not see your conversation. Be clear about the level of detail
367
+ expected.
368
+
369
+ ## Style
370
+
371
+ Never start a response by calling the user's question or idea good,
372
+ great, fascinating, profound, excellent, or perfect. Skip flattery
373
+ and respond directly. Don't pad replies with "happy to help",
374
+ "certainly", or trailing summaries the user can read in the diff.
375
+
376
+ **Use ASCII diagrams when they clarify.** For control flow, state
377
+ machines, pipelines, module relationships, or any "how do the parts
378
+ fit together" answer, draw a small ASCII diagram in a fenced
379
+ ```text block. Boxes, arrows, and trees beat paragraphs for
380
+ structural explanations:
381
+
382
+ ```text
383
+ parse → SymbolTable.build → preprocess → TypeScriptBuilder → printTs
384
+ ```
385
+
386
+ Keep diagrams small. Skip them where prose or code is clearer —
387
+ diagrams earn their space by showing **relationships** or **flow**.
388
+
389
+ ## Answer before action
390
+
391
+ When the user asks a question, asks for an opinion, or asks how to
392
+ plan or approach something, **answer the question first**. Don't
393
+ jump straight into delegating to a subagent or making tool calls
394
+ unless the user has clearly asked for an action ("do X", "fix Y",
395
+ "build Z"). If the user is exploring or thinking out loud, think
396
+ with them — don't sprint to implementation.
397
+ """
398
+
399
+ static const mainAgentTools = [
400
+ researchAgent.partial(allowHandoff: false),
401
+ codeAgent.partial(allowHandoff: false),
402
+ reviewAgent.partial(allowHandoff: false),
403
+ oracleAgent.partial(allowHandoff: false),
404
+ explorerAgent.partial(allowHandoff: false),
405
+ ]
406
+
407
+ def mainAgent(prompt: string): string {
408
+ thread(label: "main", summarize: true, session: "main") {
409
+ setMemoryId("main")
410
+ if (first) {
411
+ systemMessage(mainAgentSystemPrompt)
412
+ first = false
413
+ }
414
+ const result = llm(prompt, {
415
+ memory: true,
416
+ tools: mainAgentTools
417
+ })
418
+ }
419
+ return result
420
+ }
421
+
422
+ def routeAgent(prompt: string): string {
219
423
  const reply = route(
220
424
  {
221
425
  start: "code",
222
426
  agents: {
223
427
  code: {
224
- systemPrompt: codeSysPrompt,
225
- tools: codeTools,
226
- memory: true
428
+ type: "agent",
429
+ name: "code",
430
+ agent: codeAgent
227
431
  },
228
432
  research: {
229
- systemPrompt: researchSysPrompt,
230
- tools: researchTools,
231
- memory: true
433
+ type: "agent",
434
+ name: "research",
435
+ agent: researchAgent
232
436
  }
233
437
  },
234
438
  maxHops: 3,
235
439
  context: _context
236
440
  },
237
- msg,
441
+ prompt,
238
442
  )
239
- pushMessage(highlight("${reply}\n", language: "markdown"))
240
- return true
443
+ return reply
241
444
  }
242
445
 
243
446
  def roundedCost(): string {
@@ -263,6 +466,7 @@ def printHeader() {
263
466
  r.column() as left {
264
467
  left.text("Welcome to the Agency Agent!", bold: true)
265
468
  left.text("Ask me to write code, look up docs, or just chat.")
469
+ left.text("All costs are estimates. Actual costs may be higher.", dim: true)
266
470
  left.hline()
267
471
  left.text("Getting Started", bold: true, fgColor: "cyan")
268
472
  left.text("/help for commands · /exit to quit", dim: true)
@@ -276,7 +480,83 @@ def printHeader() {
276
480
  print(render(data, color: true))
277
481
  }
278
482
 
483
+ def givePolicyChoice() {
484
+ const title = "Welcome to the agency agent. Please pick a policy to start."
485
+ const body = "Don't worry, you can change this later. The policy just controls how the agent asks for your approval when it wants to do something. You can also create your own custom policy file at ${POLICY_PATH} and the agent will use it automatically."
486
+ let items: ChoiceItem[] = [
487
+ {
488
+ key: "minimal",
489
+ label: "minimal default policy, you do most approvals manually"
490
+ },
491
+ {
492
+ key: "recommended",
493
+ label: "recommended default policy, allow reading files and browsing the web, no writes"
494
+ },
495
+ ]
496
+
497
+ const answer = chooseOption(title, body, items, allowFreeText: true)
498
+ match(answer) {
499
+ "minimal" => return minimalAutoApprovePolicy
500
+ "recommended" => return recommendedAutoApprovePolicy
501
+ }
502
+ }
503
+
279
504
  node main() {
505
+ // Parse CLI flags first — `parseArgs` exits on --help / --version /
506
+ // usage errors, before any handlers or the TUI are installed.
507
+ const args = parseArgs(
508
+ {
509
+ programName: "agency agent",
510
+ description: "Agency language assistant — interactive REPL by default; one-shot when piped or passed a query / --print.",
511
+ version: getVersion(),
512
+ flags: {
513
+ print: {
514
+ type: "boolean",
515
+ short: "p",
516
+ description: "Run one-shot: print the reply to stdout and exit (no REPL)"
517
+ },
518
+ debug: {
519
+ type: "boolean",
520
+ description: "Log tool returns (toolName, result, timing). Same as AGENT_DEBUG=1"
521
+ },
522
+ verbose: {
523
+ type: "boolean",
524
+ description: "Echo tool-call starts to stdout in non-interactive mode"
525
+ }
526
+ }
527
+ },
528
+ )
529
+
530
+ if (args.flags.debug) {
531
+ AGENT_DEBUG = true
532
+ }
533
+ if (args.flags.verbose) {
534
+ VERBOSE = true
535
+ }
536
+
537
+ setTitle("Agency Agent")
538
+ clearScreen()
539
+ // Positional args (everything after flags) are joined with spaces to
540
+ // form the one-shot prompt. `--` ends flag parsing if a positional
541
+ // would otherwise look like a flag.
542
+ const positionalQuery = args.positionals.join(" ")
543
+ const hasQuery = positionalQuery != ""
544
+
545
+ // Non-interactive (piped) invocation: read the whole prompt from
546
+ // stdin, run one turn of `route()`, write the reply to stdout, and
547
+ // exit. No banner, no REPL, no slash commands. This lets the agent
548
+ // behave like a normal Unix filter: `echo "..." | pnpm run agency agent`.
549
+ //
550
+ // Also entered when `--print` / `-p` is passed, or when a positional
551
+ // query is given. In those cases stdin isn't read — the prompt comes
552
+ // from the positional.
553
+ const forceOneShot = args.flags.print || hasQuery
554
+ if (forceOneShot || !isTTY()) {
555
+ const result = oneShotAgent()
556
+ print(result)
557
+ process.exit(0)
558
+ }
559
+
280
560
  printHeader()
281
561
 
282
562
  // Grounding: the LLM should not have to ask the user where it is
@@ -289,12 +569,24 @@ node main() {
289
569
  const projectContext = loadAgentsMd(cwd()) with approve
290
570
  _context = "\n\nCurrent date: ${today()}\nCurrent working directory: ${cwd()}${projectContext}"
291
571
 
572
+ const policy = parsePolicyFile(POLICY_PATH)
573
+ if (policy is failure(f)) {
574
+ const { status, error } = f
575
+ print(f, status, error)
576
+ if (status == "doesnt-exist") {
577
+ print(color.yellow("No existing policy found at ${POLICY_PATH}."))
578
+ const choice = givePolicyChoice()
579
+ setPolicy(POLICY_PATH, choice)
580
+ } else {
581
+ print(color.red("Failed to load policy: ${JSON.stringify(error)}"))
582
+ process.exit(1)
583
+ }
584
+ } else {
585
+ setPolicy(POLICY_PATH, policy.value)
586
+ }
292
587
  // Bind the handler to a local var so `with handler` parses (the
293
588
  // `with` clause only accepts an identifier, not a call expression).
294
- const handler = cliPolicyHandler({
295
- file: policyPath(),
296
- fields: ALWAYS_FIELDS
297
- })
589
+ const handler = cliPolicyHandler(file: POLICY_PATH, fields: ALWAYS_FIELDS)
298
590
 
299
591
  // The `repl()` widget owns the runloop; every Enter dispatches into
300
592
  // `_runTurn` which calls `route()`. Interrupts raised inside
@@ -305,13 +597,9 @@ node main() {
305
597
  status: _buildStatus,
306
598
  onSubmit: _runTurn,
307
599
  prompt: "> ",
308
- historyFile: historyPath(),
600
+ historyFile: HISTORY_PATH,
309
601
  historyMax: 1000,
310
- paletteCommands: {
311
- "/exit": "Exit the agent",
312
- "/clear": "Clear the conversation transcript",
313
- "/help": "Show available slash commands"
314
- },
602
+ paletteCommands: mergedPalette(),
315
603
  )
316
604
  } with handler
317
605