@warlock.js/ai 4.3.0 → 4.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 (541) hide show
  1. package/CHANGELOG.md +52 -48
  2. package/cjs/index.cjs +221 -14144
  3. package/cjs/src-C02yzsLs.cjs +22991 -0
  4. package/cjs/src-C02yzsLs.cjs.map +1 -0
  5. package/cjs/src-DFibP2FQ.cjs +30 -0
  6. package/esm/agent/agent-config.type.d.mts +90 -1
  7. package/esm/agent/agent-config.type.d.mts.map +1 -1
  8. package/esm/agent/agent-input-builder.mjs +27 -6
  9. package/esm/agent/agent-input-builder.mjs.map +1 -1
  10. package/esm/agent/agent.d.mts +40 -1
  11. package/esm/agent/agent.d.mts.map +1 -1
  12. package/esm/agent/agent.mjs +203 -30
  13. package/esm/agent/agent.mjs.map +1 -1
  14. package/esm/agent/index.d.mts +2 -1
  15. package/esm/agent/index.mjs +1 -0
  16. package/esm/agent/judge-config.type.d.mts +33 -0
  17. package/esm/agent/judge-config.type.d.mts.map +1 -0
  18. package/esm/agent/judge-config.type.mjs +13 -0
  19. package/esm/agent/judge-config.type.mjs.map +1 -0
  20. package/esm/ai-openai/src/embedder.mjs +4 -0
  21. package/esm/ai-openai/src/index.mjs +4 -0
  22. package/esm/ai-openai/src/model.mjs +5 -0
  23. package/esm/ai-openai/src/sdk.mjs +6 -0
  24. package/esm/ai-openai/src/utils/index.mjs +4 -0
  25. package/esm/ai-openai/src/utils/to-openai-tools.mjs +3 -0
  26. package/esm/ai-openai/src/utils/wrap-openai-error.mjs +4 -0
  27. package/esm/ai.d.mts +119 -53
  28. package/esm/ai.d.mts.map +1 -1
  29. package/esm/ai.mjs +43 -8
  30. package/esm/ai.mjs.map +1 -1
  31. package/esm/batch/batch.d.mts.map +1 -1
  32. package/esm/batch/batch.mjs +21 -1
  33. package/esm/batch/batch.mjs.map +1 -1
  34. package/esm/batch/batch.type.d.mts +11 -4
  35. package/esm/batch/batch.type.d.mts.map +1 -1
  36. package/esm/config.d.mts +39 -3
  37. package/esm/config.d.mts.map +1 -1
  38. package/esm/config.mjs +26 -2
  39. package/esm/config.mjs.map +1 -1
  40. package/esm/contracts/agent/agent-options.type.d.mts +11 -3
  41. package/esm/contracts/agent/agent-options.type.d.mts.map +1 -1
  42. package/esm/contracts/agent/eval.type.d.mts +43 -2
  43. package/esm/contracts/agent/eval.type.d.mts.map +1 -1
  44. package/esm/contracts/attachment-policy.type.d.mts +51 -0
  45. package/esm/contracts/attachment-policy.type.d.mts.map +1 -0
  46. package/esm/contracts/attachment.type.d.mts +16 -7
  47. package/esm/contracts/attachment.type.d.mts.map +1 -1
  48. package/esm/contracts/content-part.type.d.mts +14 -5
  49. package/esm/contracts/content-part.type.d.mts.map +1 -1
  50. package/esm/contracts/events/supervisor-events.type.d.mts.map +1 -1
  51. package/esm/contracts/index.d.mts +6 -4
  52. package/esm/contracts/model.contract.d.mts +11 -8
  53. package/esm/contracts/model.contract.d.mts.map +1 -1
  54. package/esm/contracts/orchestrator/index.d.mts +1 -0
  55. package/esm/contracts/orchestrator/orchestrator-config.type.d.mts +31 -2
  56. package/esm/contracts/orchestrator/orchestrator-config.type.d.mts.map +1 -1
  57. package/esm/contracts/orchestrator/session-lock.contract.d.mts +47 -0
  58. package/esm/contracts/orchestrator/session-lock.contract.d.mts.map +1 -0
  59. package/esm/contracts/planner/index.d.mts +1 -1
  60. package/esm/contracts/planner/planner-config.type.d.mts +31 -0
  61. package/esm/contracts/planner/planner-config.type.d.mts.map +1 -1
  62. package/esm/contracts/planner/planner-execute-options.type.d.mts +48 -1
  63. package/esm/contracts/planner/planner-execute-options.type.d.mts.map +1 -1
  64. package/esm/contracts/planner/planner-result.type.d.mts +19 -5
  65. package/esm/contracts/planner/planner-result.type.d.mts.map +1 -1
  66. package/esm/contracts/planner/planner.contract.d.mts +1 -1
  67. package/esm/contracts/result/agent-result.type.d.mts +6 -4
  68. package/esm/contracts/result/agent-result.type.d.mts.map +1 -1
  69. package/esm/contracts/result/base-report.type.d.mts +21 -3
  70. package/esm/contracts/result/base-report.type.d.mts.map +1 -1
  71. package/esm/contracts/result/base-report.type.mjs.map +1 -1
  72. package/esm/contracts/result/execution-report.type.d.mts +53 -1
  73. package/esm/contracts/result/execution-report.type.d.mts.map +1 -1
  74. package/esm/contracts/result/orchestrator-result.type.d.mts +15 -11
  75. package/esm/contracts/result/orchestrator-result.type.d.mts.map +1 -1
  76. package/esm/contracts/result/supervisor-result.type.d.mts +13 -1
  77. package/esm/contracts/result/supervisor-result.type.d.mts.map +1 -1
  78. package/esm/contracts/result/tool-call.type.d.mts +2 -2
  79. package/esm/contracts/result/tool-call.type.d.mts.map +1 -1
  80. package/esm/contracts/result/workflow-result.type.d.mts +1 -0
  81. package/esm/contracts/result/workflow-result.type.d.mts.map +1 -1
  82. package/esm/contracts/supervisor/supervisor-config.type.d.mts +29 -0
  83. package/esm/contracts/supervisor/supervisor-config.type.d.mts.map +1 -1
  84. package/esm/contracts/system-prompt.contract.d.mts +103 -1
  85. package/esm/contracts/system-prompt.contract.d.mts.map +1 -1
  86. package/esm/contracts/team/index.d.mts +1 -0
  87. package/esm/contracts/team/team-config.type.d.mts +127 -0
  88. package/esm/contracts/team/team-config.type.d.mts.map +1 -0
  89. package/esm/contracts/tool.contract.d.mts +4 -2
  90. package/esm/contracts/tool.contract.d.mts.map +1 -1
  91. package/esm/contracts/workflow/step.contract.d.mts +30 -5
  92. package/esm/contracts/workflow/step.contract.d.mts.map +1 -1
  93. package/esm/contracts/workflow/workflow.contract.d.mts +16 -0
  94. package/esm/contracts/workflow/workflow.contract.d.mts.map +1 -1
  95. package/esm/errors/error-code.type.d.mts +1 -1
  96. package/esm/errors/index.d.mts +1 -0
  97. package/esm/errors/index.mjs +1 -0
  98. package/esm/errors/outbound-policy-error.d.mts +27 -0
  99. package/esm/errors/outbound-policy-error.d.mts.map +1 -0
  100. package/esm/errors/outbound-policy-error.mjs +32 -0
  101. package/esm/errors/outbound-policy-error.mjs.map +1 -0
  102. package/esm/eval/dataset.d.mts +28 -0
  103. package/esm/eval/dataset.d.mts.map +1 -0
  104. package/esm/eval/dataset.mjs +112 -0
  105. package/esm/eval/dataset.mjs.map +1 -0
  106. package/esm/eval/dataset.type.d.mts +53 -0
  107. package/esm/eval/dataset.type.d.mts.map +1 -0
  108. package/esm/eval/eval-runner.d.mts.map +1 -1
  109. package/esm/eval/eval-runner.mjs +16 -2
  110. package/esm/eval/eval-runner.mjs.map +1 -1
  111. package/esm/eval/index.d.mts +20 -1
  112. package/esm/eval/index.d.mts.map +1 -1
  113. package/esm/eval/index.mjs +20 -2
  114. package/esm/eval/index.mjs.map +1 -1
  115. package/esm/eval/regression.d.mts +30 -0
  116. package/esm/eval/regression.d.mts.map +1 -0
  117. package/esm/eval/regression.mjs +51 -0
  118. package/esm/eval/regression.mjs.map +1 -0
  119. package/esm/eval/report-json.d.mts +30 -0
  120. package/esm/eval/report-json.d.mts.map +1 -0
  121. package/esm/eval/report-json.mjs +33 -0
  122. package/esm/eval/report-json.mjs.map +1 -0
  123. package/esm/eval/report-junit.d.mts +22 -0
  124. package/esm/eval/report-junit.d.mts.map +1 -0
  125. package/esm/eval/report-junit.mjs +60 -0
  126. package/esm/eval/report-junit.mjs.map +1 -0
  127. package/esm/guard/contracts/guard-options.type.d.mts +165 -0
  128. package/esm/guard/contracts/guard-options.type.d.mts.map +1 -0
  129. package/esm/guard/contracts/guardrail.contract.d.mts +78 -0
  130. package/esm/guard/contracts/guardrail.contract.d.mts.map +1 -0
  131. package/esm/guard/contracts/index.d.mts +4 -0
  132. package/esm/guard/contracts/openai-client.contract.d.mts +51 -0
  133. package/esm/guard/contracts/openai-client.contract.d.mts.map +1 -0
  134. package/esm/guard/contracts/verdict.type.d.mts +76 -0
  135. package/esm/guard/contracts/verdict.type.d.mts.map +1 -0
  136. package/esm/guard/detectors/index.d.mts +4 -0
  137. package/esm/guard/detectors/index.mjs +6 -0
  138. package/esm/guard/detectors/injection.d.mts +34 -0
  139. package/esm/guard/detectors/injection.d.mts.map +1 -0
  140. package/esm/guard/detectors/injection.mjs +254 -0
  141. package/esm/guard/detectors/injection.mjs.map +1 -0
  142. package/esm/guard/detectors/moderation.d.mts +32 -0
  143. package/esm/guard/detectors/moderation.d.mts.map +1 -0
  144. package/esm/guard/detectors/moderation.mjs +134 -0
  145. package/esm/guard/detectors/moderation.mjs.map +1 -0
  146. package/esm/guard/detectors/pii.d.mts +41 -0
  147. package/esm/guard/detectors/pii.d.mts.map +1 -0
  148. package/esm/guard/detectors/pii.mjs +199 -0
  149. package/esm/guard/detectors/pii.mjs.map +1 -0
  150. package/esm/guard/detectors/topic.d.mts +29 -0
  151. package/esm/guard/detectors/topic.d.mts.map +1 -0
  152. package/esm/guard/detectors/topic.mjs +99 -0
  153. package/esm/guard/detectors/topic.mjs.map +1 -0
  154. package/esm/guard/errors.d.mts +12 -0
  155. package/esm/guard/errors.d.mts.map +1 -0
  156. package/esm/guard/errors.mjs +18 -0
  157. package/esm/guard/errors.mjs.map +1 -0
  158. package/esm/guard/guard.d.mts +67 -0
  159. package/esm/guard/guard.d.mts.map +1 -0
  160. package/esm/guard/guard.mjs +209 -0
  161. package/esm/guard/guard.mjs.map +1 -0
  162. package/esm/guard/guardrail.d.mts +39 -0
  163. package/esm/guard/guardrail.d.mts.map +1 -0
  164. package/esm/guard/guardrail.mjs +22 -0
  165. package/esm/guard/guardrail.mjs.map +1 -0
  166. package/esm/human/contracts/approval.type.d.mts +154 -0
  167. package/esm/human/contracts/approval.type.d.mts.map +1 -0
  168. package/esm/human/contracts/human-approval.type.d.mts +38 -0
  169. package/esm/human/contracts/human-approval.type.d.mts.map +1 -0
  170. package/esm/human/contracts/index.d.mts +4 -0
  171. package/esm/human/contracts/interrupt-store.contract.d.mts +112 -0
  172. package/esm/human/contracts/interrupt-store.contract.d.mts.map +1 -0
  173. package/esm/human/contracts/resume.type.d.mts +77 -0
  174. package/esm/human/contracts/resume.type.d.mts.map +1 -0
  175. package/esm/human/errors.d.mts +84 -0
  176. package/esm/human/errors.d.mts.map +1 -0
  177. package/esm/human/errors.mjs +60 -0
  178. package/esm/human/errors.mjs.map +1 -0
  179. package/esm/human/human-approval.d.mts +57 -0
  180. package/esm/human/human-approval.d.mts.map +1 -0
  181. package/esm/human/human-approval.mjs +170 -0
  182. package/esm/human/human-approval.mjs.map +1 -0
  183. package/esm/human/policy.d.mts +55 -0
  184. package/esm/human/policy.d.mts.map +1 -0
  185. package/esm/human/policy.mjs +67 -0
  186. package/esm/human/policy.mjs.map +1 -0
  187. package/esm/human/register.mjs +37 -0
  188. package/esm/human/register.mjs.map +1 -0
  189. package/esm/human/resume-seed.mjs +53 -0
  190. package/esm/human/resume-seed.mjs.map +1 -0
  191. package/esm/human/resume.d.mts +54 -0
  192. package/esm/human/resume.d.mts.map +1 -0
  193. package/esm/human/resume.mjs +121 -0
  194. package/esm/human/resume.mjs.map +1 -0
  195. package/esm/human/stores/index.mjs +5 -0
  196. package/esm/human/stores/memory.d.mts +30 -0
  197. package/esm/human/stores/memory.d.mts.map +1 -0
  198. package/esm/human/stores/memory.mjs +91 -0
  199. package/esm/human/stores/memory.mjs.map +1 -0
  200. package/esm/human/stores/pg.d.mts +59 -0
  201. package/esm/human/stores/pg.d.mts.map +1 -0
  202. package/esm/human/stores/pg.mjs +220 -0
  203. package/esm/human/stores/pg.mjs.map +1 -0
  204. package/esm/human/stores/redis.d.mts +56 -0
  205. package/esm/human/stores/redis.d.mts.map +1 -0
  206. package/esm/human/stores/redis.mjs +201 -0
  207. package/esm/human/stores/redis.mjs.map +1 -0
  208. package/esm/index.d.mts +113 -22
  209. package/esm/index.mjs +82 -11
  210. package/esm/middleware/builtins/budget.mjs +6 -2
  211. package/esm/middleware/builtins/budget.mjs.map +1 -1
  212. package/esm/middleware/utils/extract-user-text.d.mts +8 -1
  213. package/esm/middleware/utils/extract-user-text.d.mts.map +1 -1
  214. package/esm/middleware/utils/extract-user-text.mjs +8 -1
  215. package/esm/middleware/utils/extract-user-text.mjs.map +1 -1
  216. package/esm/object-stream/index.d.mts +2 -0
  217. package/esm/object-stream/index.mjs +4 -0
  218. package/esm/object-stream/parse-partial-json.d.mts +22 -0
  219. package/esm/object-stream/parse-partial-json.d.mts.map +1 -0
  220. package/esm/object-stream/parse-partial-json.mjs +78 -0
  221. package/esm/object-stream/parse-partial-json.mjs.map +1 -0
  222. package/esm/object-stream/stream-object.d.mts +68 -0
  223. package/esm/object-stream/stream-object.d.mts.map +1 -0
  224. package/esm/object-stream/stream-object.mjs +104 -0
  225. package/esm/object-stream/stream-object.mjs.map +1 -0
  226. package/esm/observe/index.mjs +4 -0
  227. package/esm/observe/observer-registry.d.mts +30 -0
  228. package/esm/observe/observer-registry.d.mts.map +1 -0
  229. package/esm/observe/observer-registry.mjs +51 -0
  230. package/esm/observe/observer-registry.mjs.map +1 -0
  231. package/esm/observe/observer.contract.d.mts +40 -0
  232. package/esm/observe/observer.contract.d.mts.map +1 -0
  233. package/esm/observe/resolve-observers.d.mts +40 -0
  234. package/esm/observe/resolve-observers.d.mts.map +1 -0
  235. package/esm/observe/resolve-observers.mjs +73 -0
  236. package/esm/observe/resolve-observers.mjs.map +1 -0
  237. package/esm/orchestrator/execution.d.mts.map +1 -1
  238. package/esm/orchestrator/execution.mjs +5 -2
  239. package/esm/orchestrator/execution.mjs.map +1 -1
  240. package/esm/orchestrator/index.d.mts +1 -0
  241. package/esm/orchestrator/index.mjs +1 -0
  242. package/esm/orchestrator/orchestrator.d.mts.map +1 -1
  243. package/esm/orchestrator/orchestrator.mjs +39 -6
  244. package/esm/orchestrator/orchestrator.mjs.map +1 -1
  245. package/esm/orchestrator/session-lock.d.mts +25 -0
  246. package/esm/orchestrator/session-lock.d.mts.map +1 -0
  247. package/esm/orchestrator/session-lock.mjs +83 -0
  248. package/esm/orchestrator/session-lock.mjs.map +1 -0
  249. package/esm/planner/dag-scheduler.mjs +97 -0
  250. package/esm/planner/dag-scheduler.mjs.map +1 -0
  251. package/esm/planner/plan-prompt.d.mts +1 -1
  252. package/esm/planner/plan-prompt.d.mts.map +1 -1
  253. package/esm/planner/plan-prompt.mjs +2 -1
  254. package/esm/planner/plan-prompt.mjs.map +1 -1
  255. package/esm/planner/plan-schema.mjs +17 -14
  256. package/esm/planner/plan-schema.mjs.map +1 -1
  257. package/esm/planner/planner-run.d.mts.map +1 -1
  258. package/esm/planner/planner-run.mjs +300 -29
  259. package/esm/planner/planner-run.mjs.map +1 -1
  260. package/esm/planner/planner.mjs +1 -1
  261. package/esm/planner/planner.mjs.map +1 -1
  262. package/esm/prompt/errors.d.mts +57 -0
  263. package/esm/prompt/errors.d.mts.map +1 -0
  264. package/esm/prompt/errors.mjs +73 -0
  265. package/esm/prompt/errors.mjs.map +1 -0
  266. package/esm/prompt/index.d.mts +3 -0
  267. package/esm/prompt/index.mjs +4 -0
  268. package/esm/prompt/prompt-langfuse-sync.mjs +104 -0
  269. package/esm/prompt/prompt-langfuse-sync.mjs.map +1 -0
  270. package/esm/prompt/prompt-langfuse-sync.type.d.mts +32 -0
  271. package/esm/prompt/prompt-langfuse-sync.type.d.mts.map +1 -0
  272. package/esm/prompt/prompt-validate.mjs +170 -0
  273. package/esm/prompt/prompt-validate.mjs.map +1 -0
  274. package/esm/prompt/prompt.d.mts +54 -0
  275. package/esm/prompt/prompt.d.mts.map +1 -0
  276. package/esm/prompt/prompt.mjs +218 -0
  277. package/esm/prompt/prompt.mjs.map +1 -0
  278. package/esm/prompt/prompt.type.d.mts +174 -0
  279. package/esm/prompt/prompt.type.d.mts.map +1 -0
  280. package/esm/prompts/index.d.mts +3 -0
  281. package/esm/prompts/index.mjs +3 -0
  282. package/esm/prompts/prompts-manager.contract.d.mts +154 -0
  283. package/esm/prompts/prompts-manager.contract.d.mts.map +1 -0
  284. package/esm/prompts/prompts-manager.d.mts +38 -0
  285. package/esm/prompts/prompts-manager.d.mts.map +1 -0
  286. package/esm/prompts/prompts-manager.mjs +410 -0
  287. package/esm/prompts/prompts-manager.mjs.map +1 -0
  288. package/esm/prompts/prompts-manager.type.d.mts +172 -0
  289. package/esm/prompts/prompts-manager.type.d.mts.map +1 -0
  290. package/esm/prompts/prompts-validate.mjs +200 -0
  291. package/esm/prompts/prompts-validate.mjs.map +1 -0
  292. package/esm/rag/as-tool.mjs +48 -0
  293. package/esm/rag/as-tool.mjs.map +1 -0
  294. package/esm/rag/chunk/chunk.d.mts +24 -0
  295. package/esm/rag/chunk/chunk.d.mts.map +1 -0
  296. package/esm/rag/chunk/chunk.mjs +44 -0
  297. package/esm/rag/chunk/chunk.mjs.map +1 -0
  298. package/esm/rag/chunk/fixed.mjs +32 -0
  299. package/esm/rag/chunk/fixed.mjs.map +1 -0
  300. package/esm/rag/chunk/markdown.mjs +75 -0
  301. package/esm/rag/chunk/markdown.mjs.map +1 -0
  302. package/esm/rag/chunk/recursive.mjs +132 -0
  303. package/esm/rag/chunk/recursive.mjs.map +1 -0
  304. package/esm/rag/chunk/sentence.mjs +73 -0
  305. package/esm/rag/chunk/sentence.mjs.map +1 -0
  306. package/esm/rag/contracts/chunk-options.type.d.mts +35 -0
  307. package/esm/rag/contracts/chunk-options.type.d.mts.map +1 -0
  308. package/esm/rag/contracts/citation.type.d.mts +35 -0
  309. package/esm/rag/contracts/citation.type.d.mts.map +1 -0
  310. package/esm/rag/contracts/index.d.mts +4 -0
  311. package/esm/rag/contracts/rag-config.type.d.mts +68 -0
  312. package/esm/rag/contracts/rag-config.type.d.mts.map +1 -0
  313. package/esm/rag/contracts/rag-document.type.d.mts +21 -0
  314. package/esm/rag/contracts/rag-document.type.d.mts.map +1 -0
  315. package/esm/rag/hybrid/bm25.d.mts +23 -0
  316. package/esm/rag/hybrid/bm25.d.mts.map +1 -0
  317. package/esm/rag/hybrid/bm25.mjs +51 -0
  318. package/esm/rag/hybrid/bm25.mjs.map +1 -0
  319. package/esm/rag/hybrid/hybrid-rank.d.mts +33 -0
  320. package/esm/rag/hybrid/hybrid-rank.d.mts.map +1 -0
  321. package/esm/rag/hybrid/hybrid-rank.mjs +29 -0
  322. package/esm/rag/hybrid/hybrid-rank.mjs.map +1 -0
  323. package/esm/rag/hybrid/rrf.d.mts +25 -0
  324. package/esm/rag/hybrid/rrf.d.mts.map +1 -0
  325. package/esm/rag/hybrid/rrf.mjs +30 -0
  326. package/esm/rag/hybrid/rrf.mjs.map +1 -0
  327. package/esm/rag/index.d.mts +15 -0
  328. package/esm/rag/index.mjs +11 -0
  329. package/esm/rag/rag.d.mts +38 -0
  330. package/esm/rag/rag.d.mts.map +1 -0
  331. package/esm/rag/rag.mjs +126 -0
  332. package/esm/rag/rag.mjs.map +1 -0
  333. package/esm/rag/rerank/keyword-reranker.d.mts +32 -0
  334. package/esm/rag/rerank/keyword-reranker.d.mts.map +1 -0
  335. package/esm/rag/rerank/keyword-reranker.mjs +58 -0
  336. package/esm/rag/rerank/keyword-reranker.mjs.map +1 -0
  337. package/esm/rag/rerank/llm-reranker.d.mts +36 -0
  338. package/esm/rag/rerank/llm-reranker.d.mts.map +1 -0
  339. package/esm/rag/rerank/llm-reranker.mjs +85 -0
  340. package/esm/rag/rerank/llm-reranker.mjs.map +1 -0
  341. package/esm/rag/rerank/reranker.contract.d.mts +28 -0
  342. package/esm/rag/rerank/reranker.contract.d.mts.map +1 -0
  343. package/esm/rag/retrieve.mjs +68 -0
  344. package/esm/rag/retrieve.mjs.map +1 -0
  345. package/esm/rag/store/cache-vector-store.d.mts +27 -0
  346. package/esm/rag/store/cache-vector-store.d.mts.map +1 -0
  347. package/esm/rag/store/cache-vector-store.mjs +48 -0
  348. package/esm/rag/store/cache-vector-store.mjs.map +1 -0
  349. package/esm/rag/store/vector-store.contract.d.mts +38 -0
  350. package/esm/rag/store/vector-store.contract.d.mts.map +1 -0
  351. package/esm/rag/transforms/multi-query.d.mts +27 -0
  352. package/esm/rag/transforms/multi-query.d.mts.map +1 -0
  353. package/esm/rag/transforms/multi-query.mjs +41 -0
  354. package/esm/rag/transforms/multi-query.mjs.map +1 -0
  355. package/esm/security/index.mjs +5 -0
  356. package/esm/security/outbound-policy.d.mts +46 -0
  357. package/esm/security/outbound-policy.d.mts.map +1 -0
  358. package/esm/security/outbound-policy.mjs +187 -0
  359. package/esm/security/outbound-policy.mjs.map +1 -0
  360. package/esm/security/outbound-policy.type.d.mts +74 -0
  361. package/esm/security/outbound-policy.type.d.mts.map +1 -0
  362. package/esm/security/private-ip.d.mts +15 -0
  363. package/esm/security/private-ip.d.mts.map +1 -0
  364. package/esm/security/private-ip.mjs +48 -0
  365. package/esm/security/private-ip.mjs.map +1 -0
  366. package/esm/security/redact.d.mts +59 -0
  367. package/esm/security/redact.d.mts.map +1 -0
  368. package/esm/security/redact.mjs +122 -0
  369. package/esm/security/redact.mjs.map +1 -0
  370. package/esm/serve/serve.d.mts +50 -0
  371. package/esm/serve/serve.d.mts.map +1 -0
  372. package/esm/serve/serve.mjs +90 -0
  373. package/esm/serve/serve.mjs.map +1 -0
  374. package/esm/serve/sse.d.mts +20 -0
  375. package/esm/serve/sse.d.mts.map +1 -0
  376. package/esm/serve/sse.mjs +25 -0
  377. package/esm/serve/sse.mjs.map +1 -0
  378. package/esm/serve/stream-to-sse.d.mts +29 -0
  379. package/esm/serve/stream-to-sse.d.mts.map +1 -0
  380. package/esm/serve/stream-to-sse.mjs +37 -0
  381. package/esm/serve/stream-to-sse.mjs.map +1 -0
  382. package/esm/skills/catalog.d.mts +49 -0
  383. package/esm/skills/catalog.d.mts.map +1 -0
  384. package/esm/skills/catalog.mjs +140 -0
  385. package/esm/skills/catalog.mjs.map +1 -0
  386. package/esm/skills/contracts/skill-record.type.d.mts +37 -0
  387. package/esm/skills/contracts/skill-record.type.d.mts.map +1 -0
  388. package/esm/skills/contracts/skills-config.type.d.mts +108 -0
  389. package/esm/skills/contracts/skills-config.type.d.mts.map +1 -0
  390. package/esm/skills/contracts/skills-store.contract.d.mts +28 -0
  391. package/esm/skills/contracts/skills-store.contract.d.mts.map +1 -0
  392. package/esm/skills/contracts/skills.contract.d.mts +43 -0
  393. package/esm/skills/contracts/skills.contract.d.mts.map +1 -0
  394. package/esm/skills/index.d.mts +16 -0
  395. package/esm/skills/index.mjs +14 -0
  396. package/esm/skills/load-skill-tool.d.mts +38 -0
  397. package/esm/skills/load-skill-tool.d.mts.map +1 -0
  398. package/esm/skills/load-skill-tool.mjs +65 -0
  399. package/esm/skills/load-skill-tool.mjs.map +1 -0
  400. package/esm/skills/review-gate.d.mts +33 -0
  401. package/esm/skills/review-gate.d.mts.map +1 -0
  402. package/esm/skills/review-gate.mjs +60 -0
  403. package/esm/skills/review-gate.mjs.map +1 -0
  404. package/esm/skills/save-skill-tool.d.mts +39 -0
  405. package/esm/skills/save-skill-tool.d.mts.map +1 -0
  406. package/esm/skills/save-skill-tool.mjs +65 -0
  407. package/esm/skills/save-skill-tool.mjs.map +1 -0
  408. package/esm/skills/skills.d.mts +33 -0
  409. package/esm/skills/skills.d.mts.map +1 -0
  410. package/esm/skills/skills.mjs +109 -0
  411. package/esm/skills/skills.mjs.map +1 -0
  412. package/esm/skills/sources/directory-source.d.mts +19 -0
  413. package/esm/skills/sources/directory-source.d.mts.map +1 -0
  414. package/esm/skills/sources/directory-source.mjs +108 -0
  415. package/esm/skills/sources/directory-source.mjs.map +1 -0
  416. package/esm/skills/sources/index.d.mts +18 -0
  417. package/esm/skills/sources/index.d.mts.map +1 -0
  418. package/esm/skills/sources/index.mjs +27 -0
  419. package/esm/skills/sources/index.mjs.map +1 -0
  420. package/esm/skills/sources/parse-frontmatter.d.mts +27 -0
  421. package/esm/skills/sources/parse-frontmatter.d.mts.map +1 -0
  422. package/esm/skills/sources/parse-frontmatter.mjs +46 -0
  423. package/esm/skills/sources/parse-frontmatter.mjs.map +1 -0
  424. package/esm/skills/sources/store-source.d.mts +14 -0
  425. package/esm/skills/sources/store-source.d.mts.map +1 -0
  426. package/esm/skills/sources/store-source.mjs +15 -0
  427. package/esm/skills/sources/store-source.mjs.map +1 -0
  428. package/esm/skills/sources/url-source.d.mts +29 -0
  429. package/esm/skills/sources/url-source.d.mts.map +1 -0
  430. package/esm/skills/sources/url-source.mjs +117 -0
  431. package/esm/skills/sources/url-source.mjs.map +1 -0
  432. package/esm/skills/store/mock-skills-store.d.mts +57 -0
  433. package/esm/skills/store/mock-skills-store.d.mts.map +1 -0
  434. package/esm/skills/store/mock-skills-store.mjs +100 -0
  435. package/esm/skills/store/mock-skills-store.mjs.map +1 -0
  436. package/esm/skills/store/procedural-skill-store.d.mts +30 -0
  437. package/esm/skills/store/procedural-skill-store.d.mts.map +1 -0
  438. package/esm/skills/store/procedural-skill-store.mjs +125 -0
  439. package/esm/skills/store/procedural-skill-store.mjs.map +1 -0
  440. package/esm/supervisor/as-tool.mjs +2 -2
  441. package/esm/supervisor/as-tool.mjs.map +1 -1
  442. package/esm/supervisor/execution.d.mts.map +1 -1
  443. package/esm/supervisor/execution.mjs +31 -28
  444. package/esm/supervisor/execution.mjs.map +1 -1
  445. package/esm/supervisor/supervisor.d.mts.map +1 -1
  446. package/esm/supervisor/supervisor.mjs +8 -3
  447. package/esm/supervisor/supervisor.mjs.map +1 -1
  448. package/esm/system-prompt/index.d.mts +4 -0
  449. package/esm/system-prompt/system-prompt.d.mts +68 -4
  450. package/esm/system-prompt/system-prompt.d.mts.map +1 -1
  451. package/esm/system-prompt/system-prompt.mjs +89 -5
  452. package/esm/system-prompt/system-prompt.mjs.map +1 -1
  453. package/esm/team/gates.mjs +48 -0
  454. package/esm/team/gates.mjs.map +1 -0
  455. package/esm/team/index.d.mts +1 -0
  456. package/esm/team/index.mjs +3 -0
  457. package/esm/team/team.d.mts +42 -0
  458. package/esm/team/team.d.mts.map +1 -0
  459. package/esm/team/team.mjs +94 -0
  460. package/esm/team/team.mjs.map +1 -0
  461. package/esm/tool/executable-as-tool.d.mts.map +1 -1
  462. package/esm/tool/executable-as-tool.mjs +2 -2
  463. package/esm/tool/executable-as-tool.mjs.map +1 -1
  464. package/esm/tool/tool.d.mts.map +1 -1
  465. package/esm/tool/tool.mjs +2 -2
  466. package/esm/tool/tool.mjs.map +1 -1
  467. package/esm/utils/compute-cost.d.mts +17 -1
  468. package/esm/utils/compute-cost.d.mts.map +1 -1
  469. package/esm/utils/compute-cost.mjs +26 -1
  470. package/esm/utils/compute-cost.mjs.map +1 -1
  471. package/esm/utils/extract-json-lenient.d.mts +42 -0
  472. package/esm/utils/extract-json-lenient.d.mts.map +1 -0
  473. package/esm/utils/extract-json-lenient.mjs +97 -0
  474. package/esm/utils/extract-json-lenient.mjs.map +1 -0
  475. package/esm/utils/index.d.mts +4 -2
  476. package/esm/utils/index.mjs +3 -1
  477. package/esm/utils/json-schema.d.mts +1 -1
  478. package/esm/utils/prepare-attachment-part.d.mts +10 -1
  479. package/esm/utils/prepare-attachment-part.d.mts.map +1 -1
  480. package/esm/utils/prepare-attachment-part.mjs +103 -11
  481. package/esm/utils/prepare-attachment-part.mjs.map +1 -1
  482. package/esm/utils/resolve-attachment.d.mts +4 -3
  483. package/esm/utils/resolve-attachment.d.mts.map +1 -1
  484. package/esm/utils/resolve-attachment.mjs +4 -3
  485. package/esm/utils/resolve-attachment.mjs.map +1 -1
  486. package/esm/utils/run-context.d.mts +94 -0
  487. package/esm/utils/run-context.d.mts.map +1 -0
  488. package/esm/utils/run-context.mjs +98 -0
  489. package/esm/utils/run-context.mjs.map +1 -0
  490. package/esm/vcr/cassette-io.mjs +57 -0
  491. package/esm/vcr/cassette-io.mjs.map +1 -0
  492. package/esm/vcr/errors.d.mts +42 -0
  493. package/esm/vcr/errors.d.mts.map +1 -0
  494. package/esm/vcr/errors.mjs +37 -0
  495. package/esm/vcr/errors.mjs.map +1 -0
  496. package/esm/vcr/hash-request.d.mts +28 -0
  497. package/esm/vcr/hash-request.d.mts.map +1 -0
  498. package/esm/vcr/hash-request.mjs +118 -0
  499. package/esm/vcr/hash-request.mjs.map +1 -0
  500. package/esm/vcr/index.d.mts +4 -0
  501. package/esm/vcr/index.mjs +5 -0
  502. package/esm/vcr/vcr.d.mts +32 -0
  503. package/esm/vcr/vcr.d.mts.map +1 -0
  504. package/esm/vcr/vcr.mjs +248 -0
  505. package/esm/vcr/vcr.mjs.map +1 -0
  506. package/esm/vcr/vcr.type.d.mts +118 -0
  507. package/esm/vcr/vcr.type.d.mts.map +1 -0
  508. package/esm/workflow/as-tool.mjs +2 -2
  509. package/esm/workflow/as-tool.mjs.map +1 -1
  510. package/esm/workflow/engine.mjs +1 -0
  511. package/esm/workflow/engine.mjs.map +1 -1
  512. package/esm/workflow/step-runner.mjs +19 -20
  513. package/esm/workflow/step-runner.mjs.map +1 -1
  514. package/esm/workflow/workflow.d.mts.map +1 -1
  515. package/esm/workflow/workflow.mjs +8 -3
  516. package/esm/workflow/workflow.mjs.map +1 -1
  517. package/llms-full.txt +1683 -68
  518. package/llms.txt +16 -4
  519. package/package.json +7 -3
  520. package/skills/README.md +40 -4
  521. package/skills/ai-dx-helpers/SKILL.md +2 -2
  522. package/skills/approve-tool-calls/SKILL.md +134 -0
  523. package/skills/attach-ai-middleware/SKILL.md +1 -1
  524. package/skills/detect-and-redact-pii/SKILL.md +104 -0
  525. package/skills/durable-resume/SKILL.md +128 -0
  526. package/skills/escalate-block-to-human/SKILL.md +85 -0
  527. package/skills/eval-datasets-and-ci/SKILL.md +117 -0
  528. package/skills/guard-input-output/SKILL.md +117 -0
  529. package/skills/manage-prompts/SKILL.md +186 -0
  530. package/skills/observe-ai-flows/SKILL.md +94 -0
  531. package/skills/record-replay-llm/SKILL.md +92 -0
  532. package/skills/run-ai-agent/SKILL.md +26 -1
  533. package/skills/run-ai-rag/SKILL.md +139 -0
  534. package/skills/run-ai-team/SKILL.md +107 -0
  535. package/skills/run-orchestrator/SKILL.md +2 -0
  536. package/skills/run-planner/SKILL.md +73 -8
  537. package/skills/run-supervisor/SKILL.md +20 -1
  538. package/skills/use-runtime-skills/SKILL.md +106 -0
  539. package/skills/write-system-prompt/SKILL.md +30 -1
  540. package/cjs/index.cjs.map +0 -1
  541. package/esm/contracts/result/index.d.mts +0 -15
@@ -4,16 +4,22 @@ import { AgentCancelledError } from "../errors/agent-cancelled-error.mjs";
4
4
  import { AgentMaxTripsError } from "../errors/agent-max-trips-error.mjs";
5
5
  import { SchemaValidationError } from "../errors/schema-validation-error.mjs";
6
6
  import "../errors/index.mjs";
7
- import { accumulateCost, computeCost } from "../utils/compute-cost.mjs";
7
+ import { computeCost, mergeUsage } from "../utils/compute-cost.mjs";
8
8
  import { extractJsonPayload } from "../utils/extract-json-payload.mjs";
9
+ import { extractJsonLenient } from "../utils/extract-json-lenient.mjs";
9
10
  import { generateRunId } from "../utils/generate-run-id.mjs";
11
+ import { captureChildReport } from "../utils/run-context.mjs";
10
12
  import { safeJsonParse } from "../utils/safe-json-parse.mjs";
11
13
  import { stampReportLineage } from "../utils/stamp-report-lineage.mjs";
12
14
  import "../utils/index.mjs";
13
15
  import { runEval } from "../eval/eval-runner.mjs";
14
16
  import { runPipeline } from "../middleware/pipeline.mjs";
15
17
  import "../middleware/index.mjs";
18
+ import { notifyObservers } from "../observe/resolve-observers.mjs";
19
+ import { skills } from "../skills/skills.mjs";
20
+ import "../skills/index.mjs";
16
21
  import { normalizeAgentTools } from "../tool/executable-as-tool.mjs";
22
+ import { JUDGE_DEFAULT_REPAIR_ATTEMPTS } from "./judge-config.type.mjs";
17
23
  import { buildAgentInputMessages } from "./agent-input-builder.mjs";
18
24
  import { logAgentEvent } from "./agent-log-event.mjs";
19
25
  import { createAgentStream } from "./agent-stream.mjs";
@@ -24,6 +30,14 @@ import { log } from "@warlock.js/logger";
24
30
  //#region ../@warlock.js/ai/src/agent/agent.ts
25
31
  const LOG_MODULE = "ai.agent";
26
32
  /**
33
+ * Duck-type a value as a {@link SkillsContract} (vs a raw `SkillsConfig`).
34
+ * A contract exposes the agent-facing methods; a config is a plain spec.
35
+ * Checking `catalogPrompt` is sufficient to discriminate the two shapes.
36
+ */
37
+ function isSkillsContract(value) {
38
+ return typeof value === "object" && value !== null && typeof value.catalogPrompt === "function";
39
+ }
40
+ /**
27
41
  * Detect abort-flavored errors surfaced by SDK HTTP layers — the
28
42
  * DOM `AbortError`, axios `ERR_CANCELED`, node-fetch's own
29
43
  * `AbortError`. Used to classify them as cancellation rather than
@@ -93,6 +107,16 @@ function validateMiddleware(middleware) {
93
107
  }
94
108
  }
95
109
  /**
110
+ * Normalize the public `judge` flag (`boolean | JudgeConfig | undefined`)
111
+ * into a resolved {@link JudgeConfig} or `undefined` when the preset is
112
+ * off. `true` ⇒ all defaults (`{}`); a partial config fills missing fields
113
+ * from the defaults; `false` / absent ⇒ `undefined` (judge mode off).
114
+ */
115
+ function resolveJudgeConfig(judge) {
116
+ if (!judge) return;
117
+ return { repairAttempts: (judge === true ? {} : judge).repairAttempts ?? 2 };
118
+ }
119
+ /**
96
120
  * Creates an executable AI agent from the given configuration.
97
121
  *
98
122
  * The agent runs a bounded trip loop: each trip calls the model, dispatches
@@ -130,10 +154,12 @@ function agent(config) {
130
154
  ...config,
131
155
  tools
132
156
  }) : config.name;
157
+ const skillsLib = config.skills ? isSkillsContract(config.skills) ? config.skills : skills(config.skills) : void 0;
133
158
  const resolvedConfig = {
134
159
  ...config,
135
160
  name,
136
- tools
161
+ tools,
162
+ skillsLib
137
163
  };
138
164
  const instanceHandlers = /* @__PURE__ */ new Map();
139
165
  function on(event, handler) {
@@ -170,6 +196,41 @@ function agent(config) {
170
196
  return agentContract;
171
197
  }
172
198
  /**
199
+ * Build a judge-safe agent — sugar for `agent({ ...config, judge })`.
200
+ *
201
+ * Use this for LLM-as-judge graders and verdict classifiers running on
202
+ * models that may emit corrupted structured output (e.g. the Amazon Nova
203
+ * family). The returned agent parses verdicts leniently (tolerates fenced
204
+ * ` ```json ` blocks + surrounding prose), auto-enables a couple of repair
205
+ * re-asks, and never throws on a parse miss — surfacing `result.error` with
206
+ * `result.data` left undefined so a flaky judge degrades gracefully.
207
+ *
208
+ * See {@link AgentConfig.judge} for the full behavior + the resilience-over-
209
+ * strictness trade-off.
210
+ *
211
+ * @param config - Any agent config (model, system prompt, output schema, …).
212
+ * @param judge - Optional fine-tuning ({@link JudgeConfig}); defaults to `true`.
213
+ *
214
+ * @example
215
+ * const grader = ai.agent.judge({
216
+ * model: nova.model({ name: "amazon.nova-pro-v1:0" }),
217
+ * systemPrompt: "Grade the answer. Respond with JSON only.",
218
+ * output: verdictSchema,
219
+ * });
220
+ *
221
+ * const result = await grader.execute(prompt);
222
+ * if (result.error) {
223
+ * // graceful default — the judge couldn't produce a clean verdict
224
+ * }
225
+ */
226
+ function judgeAgent(config, judge = true) {
227
+ return agent({
228
+ ...config,
229
+ judge
230
+ });
231
+ }
232
+ agent.judge = judgeAgent;
233
+ /**
173
234
  * Per-call driver that owns the full lifecycle of a single
174
235
  * `agent.execute()` or `agent.stream()` invocation.
175
236
  *
@@ -230,9 +291,13 @@ var Execution = class {
230
291
  this.start = performance.now();
231
292
  this.runId = generateRunId("agent");
232
293
  this.logger = log;
294
+ this.warnedHandlerEvents = /* @__PURE__ */ new Set();
233
295
  this.middlewareState = /* @__PURE__ */ new Map();
234
296
  this.maxTrips = config.maxTrips ?? 10;
235
297
  this.middleware = config.middleware ?? [];
298
+ this.judgeConfig = resolveJudgeConfig(config.judge);
299
+ const skillTools = config.skillsLib ? normalizeAgentTools(config.skillsLib.tools(this.runId)) ?? [] : [];
300
+ this.effectiveTools = [...config.tools ?? [], ...skillTools];
236
301
  }
237
302
  /**
238
303
  * Base middleware context shared by every level. `state` is the
@@ -280,6 +345,8 @@ var Execution = class {
280
345
  if (result.error) this.emit("agent.error", { error: result.error });
281
346
  this.emit("agent.completed", { result });
282
347
  await this.fireCompleteHook(result);
348
+ await notifyObservers(this.config.observe, result.report);
349
+ captureChildReport(result.report);
283
350
  this.streamController?.end(result);
284
351
  return result;
285
352
  }
@@ -296,7 +363,7 @@ var Execution = class {
296
363
  await this.buildInitialMessages();
297
364
  this.emit("agent.starting", { input: this.input });
298
365
  await this.runTripLoop();
299
- if (await this.parseOutput() === "failed" && this.options?.repair) await this.runRepairLoop();
366
+ if (await this.parseOutput() === "failed" && this.resolveRepairAttempts() > 0) await this.runRepairLoop();
300
367
  } catch (thrown) {
301
368
  this.error = this.toAIError(thrown);
302
369
  }
@@ -311,13 +378,50 @@ var Execution = class {
311
378
  * to the model. Runs exactly once per execution.
312
379
  */
313
380
  async buildInitialMessages() {
314
- const { messages, responseSchema } = await buildAgentInputMessages({
381
+ const { messages, responseSchema, systemPrompt, promptName, promptVersion } = await buildAgentInputMessages({
315
382
  config: this.config,
316
383
  input: this.input,
317
384
  options: this.options
318
385
  });
319
386
  this.messages.push(...messages);
320
387
  this.responseSchema = responseSchema;
388
+ this.systemPrompt = systemPrompt;
389
+ this.promptName = promptName;
390
+ this.promptVersion = promptVersion;
391
+ await this.injectSkills();
392
+ }
393
+ /**
394
+ * Prepend the skills library's contribution to the system prompt — the
395
+ * always-injected metadata catalog first, then (only under `inject`) the
396
+ * preloaded skill bodies, then the developer's resolved system prompt.
397
+ * Never replaces the developer prompt.
398
+ *
399
+ * No-op when no skills library is attached. `catalogPrompt` returns `""`
400
+ * when nothing is in scope and `preload` returns `[]` when `inject` is
401
+ * omitted (the default), so the prepend is a no-op in those cases too.
402
+ *
403
+ * Awaited inside `buildInitialMessages`, which runs inside `runCore`'s
404
+ * try/catch — a source/embedder failure funnels into `this.error` like
405
+ * any other build failure, no new error handling needed.
406
+ */
407
+ async injectSkills() {
408
+ const lib = this.config.skillsLib;
409
+ if (!lib) return;
410
+ const catalogBlock = await lib.catalogPrompt(this.input);
411
+ const preloaded = await lib.preload(this.input);
412
+ const blocks = [];
413
+ if (catalogBlock) blocks.push(catalogBlock);
414
+ for (const record of preloaded) if (record.body) blocks.push(record.body);
415
+ if (blocks.length === 0) return;
416
+ const prefix = blocks.join("\n\n");
417
+ const merged = this.systemPrompt ? `${prefix}\n\n${this.systemPrompt}` : prefix;
418
+ this.systemPrompt = merged;
419
+ const firstMessage = this.messages[0];
420
+ if (firstMessage?.role === "system") firstMessage.content = merged;
421
+ else this.messages.unshift({
422
+ role: "system",
423
+ content: merged
424
+ });
321
425
  }
322
426
  /**
323
427
  * Drive sequential trips up to `maxTrips`. Each trip may stop the loop
@@ -378,11 +482,7 @@ var Execution = class {
378
482
  return "error";
379
483
  }
380
484
  if (response.usage.cost === void 0) response.usage.cost = computeCost(response.usage, this.config.model.pricing);
381
- this.usage.input += response.usage.input;
382
- this.usage.output += response.usage.output;
383
- this.usage.total += response.usage.total;
384
- if (response.usage.cachedTokens !== void 0) this.usage.cachedTokens = (this.usage.cachedTokens ?? 0) + response.usage.cachedTokens;
385
- this.usage.cost = accumulateCost(this.usage.cost, response.usage.cost);
485
+ mergeUsage(this.usage, response.usage);
386
486
  await this.fireUsageHook(tripIndex, response.usage);
387
487
  const isToolCallTrip = response.finishReason === "tool_calls" && response.toolCalls !== void 0 && response.toolCalls.length > 0;
388
488
  const tripToolCalls = [];
@@ -412,7 +512,7 @@ var Execution = class {
412
512
  this.emit("agent.trip.completed", { trip });
413
513
  if (!isToolCallTrip) return "stop";
414
514
  return response.toolCalls.every((request) => {
415
- return (this.config.tools?.find((tool) => tool.name === request.name))?.mode === "silent";
515
+ return this.effectiveTools.find((tool) => tool.name === request.name)?.mode === "silent";
416
516
  }) ? "stop" : "continue";
417
517
  }
418
518
  /**
@@ -442,7 +542,7 @@ var Execution = class {
442
542
  async getModelResponse(tripIndex) {
443
543
  const callOptions = {
444
544
  ...this.config.modelOptions,
445
- tools: this.config.tools ?? [],
545
+ tools: this.effectiveTools,
446
546
  ...this.responseSchema ? { responseSchema: this.responseSchema } : {},
447
547
  ...this.options?.signal ? { signal: this.options.signal } : {}
448
548
  };
@@ -458,7 +558,7 @@ var Execution = class {
458
558
  const recoveredCalls = [];
459
559
  const guardConfig = this.resolveStreamingToolGuard();
460
560
  const guard = guardConfig ? new JsonStreamGuard({
461
- tools: this.config.tools ?? [],
561
+ tools: this.effectiveTools,
462
562
  maxBufferBytes: guardConfig.maxBufferBytes,
463
563
  onSafeDelta: (delta) => {
464
564
  content += delta;
@@ -527,7 +627,7 @@ var Execution = class {
527
627
  * an error). Never throws — always returns a `ToolCall` record.
528
628
  */
529
629
  async dispatchToolCall(toolCallRequest, tripIndex) {
530
- const registeredTool = this.config.tools?.find((tool) => tool.name === toolCallRequest.name);
630
+ const registeredTool = this.effectiveTools.find((tool) => tool.name === toolCallRequest.name);
531
631
  if (!registeredTool) {
532
632
  const error = new AgentExecutionError(`Tool not registered: ${toolCallRequest.name}`, { context: {
533
633
  toolName: toolCallRequest.name,
@@ -592,9 +692,15 @@ var Execution = class {
592
692
  },
593
693
  request: toolCallRequest
594
694
  };
695
+ const runSignal = this.options?.signal;
696
+ const dispatchToolCtx = runSignal ? {
697
+ artifacts: this.options?.toolCtx?.artifacts ?? {},
698
+ ...this.options?.toolCtx,
699
+ signal: runSignal
700
+ } : this.options?.toolCtx;
595
701
  let invokeResult;
596
702
  try {
597
- invokeResult = await runPipeline(this.middleware, "tool", toolContext, () => registeredTool.invoke(toolCallRequest.input, this.options?.toolCtx), this.logger);
703
+ invokeResult = await runPipeline(this.middleware, "tool", toolContext, () => registeredTool.invoke(toolCallRequest.input, dispatchToolCtx), this.logger);
598
704
  } catch (thrown) {
599
705
  const error = this.toAIError(thrown);
600
706
  const nowIso = (/* @__PURE__ */ new Date()).toISOString();
@@ -643,10 +749,7 @@ var Execution = class {
643
749
  ...toolCallRequest.recoveredFrom ? { recoveredFrom: toolCallRequest.recoveredFrom } : {}
644
750
  };
645
751
  this.toolCalls.push(record);
646
- this.usage.input += invokeResult.usage.input;
647
- this.usage.output += invokeResult.usage.output;
648
- this.usage.total += invokeResult.usage.total;
649
- this.usage.cost = accumulateCost(this.usage.cost, invokeResult.usage.cost);
752
+ mergeUsage(this.usage, invokeResult.usage);
650
753
  this.messages.push({
651
754
  role: "tool",
652
755
  toolCallId: toolCallRequest.id,
@@ -675,13 +778,20 @@ var Execution = class {
675
778
  * - `"failed"` — schema present, output text either failed JSON.parse
676
779
  * or failed `~standard.validate`. Repairable via `runRepairLoop`.
677
780
  * - `"success"` — parsed and validated; `this.data` populated.
781
+ *
782
+ * Under the judge-safe preset (`judge: true`) the JSON extraction is
783
+ * lenient — it tolerates fenced ` ```json ` blocks plus leading /
784
+ * trailing prose by slicing the first balanced object / array out of the
785
+ * response. Never throws regardless of preset: a parse / validation miss
786
+ * sets `this.error` and returns `"failed"`, leaving `this.data`
787
+ * undefined for the graceful-default path.
678
788
  */
679
789
  async parseOutput() {
680
790
  const schema = this.options?.output ?? this.config.output;
681
791
  if (!schema || this.error) return "skipped";
682
792
  const text = this.trips[this.trips.length - 1]?.output ?? "";
683
793
  if (!text) return "skipped";
684
- const payload = extractJsonPayload(text);
794
+ const payload = this.judgeConfig ? extractJsonLenient(text) : extractJsonPayload(text);
685
795
  const sentinel = Symbol("parse-failed");
686
796
  const parsed = safeJsonParse(payload, sentinel);
687
797
  if (parsed === sentinel) {
@@ -698,8 +808,22 @@ var Execution = class {
698
808
  return "success";
699
809
  }
700
810
  /**
811
+ * Resolve how many repair re-asks this run should perform after a parse
812
+ * failure. Per-call `options.repair` wins when explicitly set (preserving
813
+ * the existing surface). Otherwise the judge-safe preset supplies its
814
+ * default attempt count — so `judge: true` enables repair without the
815
+ * caller also having to pass `repair`. Returns `0` when neither applies,
816
+ * which leaves the historical "no repair unless asked" behavior intact.
817
+ */
818
+ resolveRepairAttempts() {
819
+ if (this.options?.repair) return this.options.repair.maxAttempts ?? 1;
820
+ if (this.judgeConfig) return this.judgeConfig.repairAttempts ?? 2;
821
+ return 0;
822
+ }
823
+ /**
701
824
  * Opt-in self-repair loop for `output` schema failures. Triggered only
702
- * when `options.repair` is set and `parseOutput()` returned `"failed"`.
825
+ * when repair attempts remain (`resolveRepairAttempts() > 0`) and
826
+ * `parseOutput()` returned `"failed"`.
703
827
  *
704
828
  * Each attempt:
705
829
  * 1. Pushes the bad assistant response into `this.messages` (so the
@@ -714,7 +838,7 @@ var Execution = class {
714
838
  * outcome (success or last failure) is what surfaces to the caller.
715
839
  */
716
840
  async runRepairLoop() {
717
- const maxAttempts = this.options?.repair?.maxAttempts ?? 1;
841
+ const maxAttempts = this.resolveRepairAttempts();
718
842
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
719
843
  if (this.trips.length >= this.maxTrips) return;
720
844
  const badResponse = this.trips[this.trips.length - 1]?.output ?? "";
@@ -764,6 +888,7 @@ var Execution = class {
764
888
  version: this.config.version,
765
889
  type: "agent",
766
890
  status,
891
+ ...this.error ? { error: this.error } : {},
767
892
  startedAt: this.startedAt.toISOString(),
768
893
  endedAt: endedAt.toISOString(),
769
894
  duration: performance.now() - this.start,
@@ -773,7 +898,13 @@ var Execution = class {
773
898
  name: this.config.model.name,
774
899
  provider: this.config.model.provider
775
900
  },
776
- trips: this.trips
901
+ trips: this.trips,
902
+ systemPrompt: this.systemPrompt,
903
+ ...this.promptName ? {
904
+ promptName: this.promptName,
905
+ promptVersion: this.promptVersion
906
+ } : {},
907
+ ...this.config.captureMessages ? { messages: this.captureMessages() } : {}
777
908
  };
778
909
  stampReportLineage(report, {
779
910
  rootRunId: this.runId,
@@ -789,6 +920,25 @@ var Execution = class {
789
920
  };
790
921
  }
791
922
  /**
923
+ * Normalize the accumulated runtime `Message[]` into the JSON-safe
924
+ * {@link CapturedMessage}[] persisted on `AgentReport.messages` (F2).
925
+ * Flattens `ContentPart[]` content to a string, and forwards
926
+ * `toolCalls` (assistant turns) / `toolCallId` (tool-result turns)
927
+ * only when present so the captured shape stays lean. Called only when
928
+ * `captureMessages` is enabled.
929
+ */
930
+ captureMessages() {
931
+ return this.messages.map((message) => {
932
+ const captured = {
933
+ role: message.role,
934
+ content: typeof message.content === "string" ? message.content : JSON.stringify(message.content)
935
+ };
936
+ if (message.toolCalls !== void 0) captured.toolCalls = message.toolCalls;
937
+ if (message.toolCallId !== void 0) captured.toolCallId = message.toolCallId;
938
+ return captured;
939
+ });
940
+ }
941
+ /**
792
942
  * Normalize any thrown value into an `AIError`. `AIError` instances
793
943
  * pass through untouched; provider-adapter SDK errors are caught by
794
944
  * the adapter and already arrive typed, so this branch mainly
@@ -832,12 +982,13 @@ var Execution = class {
832
982
  rootRunId: this.runId
833
983
  };
834
984
  this.logEvent(event, fullPayload);
985
+ const onError = (error) => this.surfaceHandlerError(event, error);
835
986
  const factoryHandler = this.config.on?.[event];
836
- if (factoryHandler) safeCall(factoryHandler, fullPayload);
987
+ if (factoryHandler) safeCall(factoryHandler, fullPayload, onError);
837
988
  const bucket = this.instanceHandlers?.get(event);
838
- if (bucket) for (const handler of bucket) safeCall(handler, fullPayload);
989
+ if (bucket) for (const handler of bucket) safeCall(handler, fullPayload, onError);
839
990
  const perCallHandler = this.options?.on?.[event];
840
- if (perCallHandler) safeCall(perCallHandler, fullPayload);
991
+ if (perCallHandler) safeCall(perCallHandler, fullPayload, onError);
841
992
  if (this.streamController) {
842
993
  const body = this.toStreamEvent(event, fullPayload);
843
994
  if (body) this.streamController.push({
@@ -848,6 +999,23 @@ var Execution = class {
848
999
  }
849
1000
  }
850
1001
  /**
1002
+ * Surface an isolated event-handler failure (C5). A throwing user
1003
+ * handler never crashes the agent — that isolation is preserved — but
1004
+ * total silence is the wrong default: a broken `on` handler would
1005
+ * otherwise disappear from production with no signal. Routed to the
1006
+ * structured logger (matching the `onUsage` / `onComplete` policy) and
1007
+ * warned at most once per event type so a hot event can't spam the log.
1008
+ */
1009
+ surfaceHandlerError(event, error) {
1010
+ if (this.warnedHandlerEvents.has(event)) return;
1011
+ this.warnedHandlerEvents.add(event);
1012
+ this.logger.warn(LOG_MODULE, "event.handler.error", "an event handler threw and was isolated", {
1013
+ runId: this.runId,
1014
+ event,
1015
+ error: error instanceof Error ? error.message : String(error)
1016
+ });
1017
+ }
1018
+ /**
851
1019
  * Emit a structured log line for a lifecycle event. The action
852
1020
  * string mirrors the event name with the `agent.` prefix stripped
853
1021
  * (`agent.trip.started` → `trip.started`) so log grep filters and
@@ -953,18 +1121,23 @@ function stableStringify(value) {
953
1121
  }
954
1122
  /**
955
1123
  * Invoke a user-supplied event handler without letting exceptions
956
- * escape the agent. Thrown errors are swallowed structured logging
957
- * attaches here in Phase 0.5 so operators still see the failure.
1124
+ * escape the agent. A throw is isolated (it never crashes the agent)
1125
+ * but no longer silent: the optional `onError` surfaces it the agent
1126
+ * routes it to its structured logger, matching the swallow-and-log
1127
+ * policy of the `onUsage` / `onComplete` hooks (C5).
958
1128
  */
959
- function safeCall(handler, payload) {
1129
+ function safeCall(handler, payload, onError) {
960
1130
  try {
961
1131
  handler(payload);
962
- } catch {}
1132
+ } catch (error) {
1133
+ onError?.(error);
1134
+ }
963
1135
  }
964
1136
  /**
965
1137
  * Resolve a tool's `action` declaration into a plain string for
966
1138
  * inclusion in `ToolEventMeta`. Static strings pass through;
967
- * function-shaped actions are invoked with the validated input.
1139
+ * function-shaped actions are invoked with the model's raw,
1140
+ * pre-validation input (before `execute`'s schema validation runs).
968
1141
  *
969
1142
  * Defensive: if the user's callback throws, swallow and return
970
1143
  * `undefined` rather than crashing the agent — UI strings are not