mayura 0.1.0-dev.0 → 1.0.0-rc.2

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 (578) hide show
  1. package/README.md +152 -35
  2. package/docs/README.md +82 -0
  3. package/docs/ai-agents.md +73 -0
  4. package/docs/cli/dev.md +111 -0
  5. package/docs/cli/init.md +192 -0
  6. package/docs/cli/operations.md +235 -0
  7. package/docs/cli/overview.md +129 -0
  8. package/docs/cli/run.md +182 -0
  9. package/docs/concepts/agent.md +197 -0
  10. package/docs/concepts/costs-and-budgets.md +177 -0
  11. package/docs/concepts/outcomes.md +154 -0
  12. package/docs/concepts/permissions.md +155 -0
  13. package/docs/concepts/runtime.md +172 -0
  14. package/docs/concepts/tools.md +202 -0
  15. package/docs/concepts/workflows.md +134 -0
  16. package/docs/guides/approvals-and-human-input.md +278 -0
  17. package/docs/guides/artifacts.md +123 -0
  18. package/docs/guides/child-agents.md +158 -0
  19. package/docs/guides/code-mode.md +233 -0
  20. package/docs/guides/deployment.md +644 -0
  21. package/docs/guides/durable-workflows.md +463 -0
  22. package/docs/guides/guardrails.md +205 -0
  23. package/docs/guides/helpers.md +176 -0
  24. package/docs/guides/lifecycle-hooks.md +187 -0
  25. package/docs/guides/mcp.md +123 -0
  26. package/docs/guides/memory-and-context.md +293 -0
  27. package/docs/guides/model-providers.md +371 -0
  28. package/docs/guides/model-routing.md +138 -0
  29. package/docs/guides/observability.md +238 -0
  30. package/docs/guides/operator-console.md +140 -0
  31. package/docs/guides/react.md +276 -0
  32. package/docs/guides/sagas-and-loops.md +212 -0
  33. package/docs/guides/server-and-client.md +559 -0
  34. package/docs/guides/skills.md +152 -0
  35. package/docs/guides/storage.md +251 -0
  36. package/docs/guides/streaming.md +146 -0
  37. package/docs/guides/terminal.md +166 -0
  38. package/docs/guides/testing.md +220 -0
  39. package/docs/guides/vision.md +155 -0
  40. package/docs/guides/webhooks.md +172 -0
  41. package/docs/guides/workflow-composition.md +141 -0
  42. package/docs/guides/workflow-operations.md +269 -0
  43. package/docs/installation.md +90 -0
  44. package/docs/introduction.md +74 -0
  45. package/docs/project/credits.md +78 -0
  46. package/docs/project/security.md +178 -0
  47. package/docs/project/support.md +91 -0
  48. package/docs/project/versioning.md +122 -0
  49. package/docs/quickstart.md +190 -0
  50. package/docs/reference/entry-points.md +146 -0
  51. package/lib/adapter-code-docker/dist/index.d.ts +8 -1
  52. package/lib/adapter-code-docker/dist/index.d.ts.map +1 -1
  53. package/lib/adapter-code-docker/dist/index.js +64 -67
  54. package/lib/adapter-code-docker/dist/index.js.map +1 -1
  55. package/lib/adapter-code-docker/dist/options.d.ts +15 -0
  56. package/lib/adapter-code-docker/dist/options.d.ts.map +1 -0
  57. package/lib/adapter-code-docker/dist/options.js +56 -0
  58. package/lib/adapter-code-docker/dist/options.js.map +1 -0
  59. package/lib/adapter-code-docker/image/Dockerfile +4 -2
  60. package/lib/adapter-code-docker/package.json +1 -1
  61. package/lib/adapter-code-docker/src/index.ts +63 -65
  62. package/lib/adapter-code-docker/src/options.ts +51 -0
  63. package/lib/adapter-code-quickjs/dist/index.d.ts +16 -4
  64. package/lib/adapter-code-quickjs/dist/index.d.ts.map +1 -1
  65. package/lib/adapter-code-quickjs/dist/index.js +94 -50
  66. package/lib/adapter-code-quickjs/dist/index.js.map +1 -1
  67. package/lib/adapter-code-quickjs/dist/worker-command.d.ts +27 -0
  68. package/lib/adapter-code-quickjs/dist/worker-command.d.ts.map +1 -0
  69. package/lib/adapter-code-quickjs/dist/worker-command.js +66 -0
  70. package/lib/adapter-code-quickjs/dist/worker-command.js.map +1 -0
  71. package/lib/adapter-code-quickjs/dist/worker.js +421 -104
  72. package/lib/adapter-code-quickjs/dist/worker.js.map +1 -1
  73. package/lib/adapter-code-quickjs/package.json +1 -1
  74. package/lib/adapter-code-quickjs/src/index.ts +88 -44
  75. package/lib/adapter-code-quickjs/src/worker-command.ts +68 -0
  76. package/lib/adapter-code-quickjs/src/worker.ts +315 -89
  77. package/lib/adapter-mcp/package.json +1 -1
  78. package/lib/artifacts/dist/contracts.d.ts +6 -2
  79. package/lib/artifacts/dist/contracts.d.ts.map +1 -1
  80. package/lib/artifacts/dist/contracts.js.map +1 -1
  81. package/lib/artifacts/dist/index.d.ts +1 -0
  82. package/lib/artifacts/dist/index.d.ts.map +1 -1
  83. package/lib/artifacts/dist/index.js +1 -0
  84. package/lib/artifacts/dist/index.js.map +1 -1
  85. package/lib/artifacts/dist/local-store.d.ts.map +1 -1
  86. package/lib/artifacts/dist/local-store.js +4 -7
  87. package/lib/artifacts/dist/local-store.js.map +1 -1
  88. package/lib/artifacts/dist/media.d.ts +11 -0
  89. package/lib/artifacts/dist/media.d.ts.map +1 -0
  90. package/lib/artifacts/dist/media.js +18 -0
  91. package/lib/artifacts/dist/media.js.map +1 -0
  92. package/lib/artifacts/package.json +1 -1
  93. package/lib/artifacts/src/contracts.ts +6 -2
  94. package/lib/artifacts/src/index.ts +1 -0
  95. package/lib/artifacts/src/local-store.ts +5 -7
  96. package/lib/artifacts/src/media.ts +20 -0
  97. package/lib/cli/dist/agent-guide.d.ts +10 -0
  98. package/lib/cli/dist/agent-guide.d.ts.map +1 -0
  99. package/lib/cli/dist/agent-guide.js +48 -0
  100. package/lib/cli/dist/agent-guide.js.map +1 -0
  101. package/lib/cli/dist/application.d.ts +24 -0
  102. package/lib/cli/dist/application.d.ts.map +1 -1
  103. package/lib/cli/dist/application.js +21 -0
  104. package/lib/cli/dist/application.js.map +1 -1
  105. package/lib/cli/dist/bin.js +26 -6
  106. package/lib/cli/dist/bin.js.map +1 -1
  107. package/lib/cli/dist/dev.d.ts.map +1 -1
  108. package/lib/cli/dist/dev.js +7 -1
  109. package/lib/cli/dist/dev.js.map +1 -1
  110. package/lib/cli/dist/index.d.ts +3 -3
  111. package/lib/cli/dist/index.d.ts.map +1 -1
  112. package/lib/cli/dist/index.js +19 -9
  113. package/lib/cli/dist/index.js.map +1 -1
  114. package/lib/cli/dist/interactive.d.ts.map +1 -1
  115. package/lib/cli/dist/interactive.js +52 -11
  116. package/lib/cli/dist/interactive.js.map +1 -1
  117. package/lib/cli/dist/operations.d.ts +25 -2
  118. package/lib/cli/dist/operations.d.ts.map +1 -1
  119. package/lib/cli/dist/operations.js +125 -12
  120. package/lib/cli/dist/operations.js.map +1 -1
  121. package/lib/cli/dist/output.d.ts.map +1 -1
  122. package/lib/cli/dist/output.js +3 -0
  123. package/lib/cli/dist/output.js.map +1 -1
  124. package/lib/cli/dist/providers.d.ts +34 -4
  125. package/lib/cli/dist/providers.d.ts.map +1 -1
  126. package/lib/cli/dist/providers.js +26 -5
  127. package/lib/cli/dist/providers.js.map +1 -1
  128. package/lib/cli/package.json +1 -1
  129. package/lib/cli/src/agent-guide.ts +47 -0
  130. package/lib/cli/src/application.ts +24 -0
  131. package/lib/cli/src/bin.ts +22 -5
  132. package/lib/cli/src/dev.ts +5 -1
  133. package/lib/cli/src/index.ts +18 -10
  134. package/lib/cli/src/interactive.ts +44 -12
  135. package/lib/cli/src/operations.ts +82 -12
  136. package/lib/cli/src/output.ts +3 -0
  137. package/lib/cli/src/providers.ts +45 -10
  138. package/lib/cli/starters/approval-workflow/dot-env.example +11 -0
  139. package/lib/cli/starters/approval-workflow/package.json +1 -2
  140. package/lib/cli/starters/approval-workflow/src/config.ts +25 -7
  141. package/lib/cli/starters/approval-workflow/src/intake.ts +1 -2
  142. package/lib/cli/starters/approval-workflow/src/model.ts +11 -4
  143. package/lib/cli/starters/approval-workflow/src/workflow.ts +1 -2
  144. package/lib/cli/starters/cli-agent/README.md +121 -0
  145. package/lib/cli/starters/cli-agent/dot-env.example +37 -0
  146. package/lib/cli/starters/cli-agent/dot-github/workflows/ci.yml +13 -0
  147. package/lib/cli/starters/cli-agent/dot-gitignore +10 -0
  148. package/lib/cli/starters/cli-agent/mayura.project.json +15 -0
  149. package/lib/cli/starters/cli-agent/package.json +28 -0
  150. package/lib/cli/starters/cli-agent/skills/release-notes/SKILL.md +18 -0
  151. package/lib/cli/starters/cli-agent/skills/release-notes/references/example.md +12 -0
  152. package/lib/cli/starters/cli-agent/src/assistant.ts +124 -0
  153. package/lib/cli/starters/cli-agent/src/cli.ts +72 -0
  154. package/lib/cli/starters/cli-agent/src/config.ts +117 -0
  155. package/lib/cli/starters/cli-agent/src/model.ts +34 -0
  156. package/lib/cli/starters/cli-agent/src/workspace.ts +170 -0
  157. package/lib/cli/starters/cli-agent/test/assistant.test.ts +205 -0
  158. package/lib/cli/starters/cli-agent/tsconfig.json +20 -0
  159. package/lib/cli/starters/event-automation/README.md +4 -3
  160. package/lib/cli/starters/event-automation/dot-env.example +11 -0
  161. package/lib/cli/starters/event-automation/package.json +1 -2
  162. package/lib/cli/starters/event-automation/src/config.ts +25 -7
  163. package/lib/cli/starters/event-automation/src/ingress.ts +3 -5
  164. package/lib/cli/starters/event-automation/src/model.ts +11 -4
  165. package/lib/cli/starters/event-automation/src/send-sample.ts +1 -1
  166. package/lib/cli/starters/event-automation/src/tracker/main.ts +1 -1
  167. package/lib/cli/starters/event-automation/src/tracker/mcp-server.ts +1 -1
  168. package/lib/cli/starters/event-automation/src/tracker/tickets.ts +1 -1
  169. package/lib/cli/starters/event-automation/src/tracker-tools.ts +1 -1
  170. package/lib/cli/starters/event-automation/src/triage.ts +15 -22
  171. package/lib/cli/starters/event-automation/src/workflows.ts +1 -1
  172. package/lib/cli/starters/event-automation/test/automation.test.ts +3 -3
  173. package/lib/cli/starters/research-team/README.md +2 -2
  174. package/lib/cli/starters/research-team/dot-env.example +11 -0
  175. package/lib/cli/starters/research-team/package.json +1 -2
  176. package/lib/cli/starters/research-team/src/config.ts +25 -7
  177. package/lib/cli/starters/research-team/src/desk.ts +1 -2
  178. package/lib/cli/starters/research-team/src/library/index.ts +1 -2
  179. package/lib/cli/starters/research-team/src/model.ts +11 -4
  180. package/lib/cli/starters/research-team/src/services.ts +1 -1
  181. package/lib/cli/starters/research-team/src/team.ts +1 -2
  182. package/lib/cli/starters/research-team/src/workflow.ts +45 -59
  183. package/lib/cli/starters/support-agent/dot-env.example +11 -0
  184. package/lib/cli/starters/support-agent/package.json +1 -2
  185. package/lib/cli/starters/support-agent/src/assistant.ts +5 -5
  186. package/lib/cli/starters/support-agent/src/config.ts +25 -7
  187. package/lib/cli/starters/support-agent/src/guardrails.ts +3 -1
  188. package/lib/cli/starters/support-agent/src/model.ts +11 -4
  189. package/lib/cli/starters/support-agent/src/returns.ts +1 -2
  190. package/lib/cli/starters/support-agent/test/support.test.ts +25 -1
  191. package/lib/cli/templates/basic-agent.ts +1 -2
  192. package/lib/cli/templates/capability-policy.ts +1 -2
  193. package/lib/cli/templates/code-mode-workflow.ts +1 -1
  194. package/lib/cli/templates/durable-approval.ts +1 -1
  195. package/lib/cli/templates/parallel-research.ts +1 -2
  196. package/lib/cli/templates/typed-tool-runner.ts +1 -2
  197. package/lib/client/dist/forms.d.ts.map +1 -1
  198. package/lib/client/dist/forms.js +3 -4
  199. package/lib/client/dist/forms.js.map +1 -1
  200. package/lib/client/dist/headless.d.ts +3 -1
  201. package/lib/client/dist/headless.d.ts.map +1 -1
  202. package/lib/client/dist/headless.js +10 -6
  203. package/lib/client/dist/headless.js.map +1 -1
  204. package/lib/client/dist/index.d.ts +95 -6
  205. package/lib/client/dist/index.d.ts.map +1 -1
  206. package/lib/client/dist/index.js +397 -124
  207. package/lib/client/dist/index.js.map +1 -1
  208. package/lib/client/dist/workflows.d.ts +1 -1
  209. package/lib/client/dist/workflows.d.ts.map +1 -1
  210. package/lib/client/dist/workflows.js +7 -6
  211. package/lib/client/dist/workflows.js.map +1 -1
  212. package/lib/client/package.json +1 -1
  213. package/lib/client/src/forms.ts +3 -4
  214. package/lib/client/src/headless.ts +13 -7
  215. package/lib/client/src/index.ts +344 -74
  216. package/lib/client/src/workflows.ts +8 -7
  217. package/lib/client-react/package.json +1 -1
  218. package/lib/code-mode/dist/index.d.ts +32 -1
  219. package/lib/code-mode/dist/index.d.ts.map +1 -1
  220. package/lib/code-mode/dist/index.js +289 -221
  221. package/lib/code-mode/dist/index.js.map +1 -1
  222. package/lib/code-mode/package.json +1 -1
  223. package/lib/code-mode/src/index.ts +288 -184
  224. package/lib/code-mode-workflows/package.json +1 -1
  225. package/lib/context/package.json +1 -1
  226. package/lib/core/dist/budget.d.ts.map +1 -1
  227. package/lib/core/dist/budget.js +17 -2
  228. package/lib/core/dist/budget.js.map +1 -1
  229. package/lib/core/dist/contracts.d.ts +57 -0
  230. package/lib/core/dist/contracts.d.ts.map +1 -1
  231. package/lib/core/dist/contracts.js +36 -0
  232. package/lib/core/dist/contracts.js.map +1 -1
  233. package/lib/core/dist/destination.d.ts +12 -0
  234. package/lib/core/dist/destination.d.ts.map +1 -0
  235. package/lib/core/dist/destination.js +37 -0
  236. package/lib/core/dist/destination.js.map +1 -0
  237. package/lib/core/dist/event-stream.d.ts +1 -1
  238. package/lib/core/dist/event-stream.d.ts.map +1 -1
  239. package/lib/core/dist/event-stream.js +7 -0
  240. package/lib/core/dist/event-stream.js.map +1 -1
  241. package/lib/core/dist/host.d.ts +3 -0
  242. package/lib/core/dist/host.d.ts.map +1 -1
  243. package/lib/core/dist/host.js +3 -0
  244. package/lib/core/dist/host.js.map +1 -1
  245. package/lib/core/dist/index.d.ts +2 -0
  246. package/lib/core/dist/index.d.ts.map +1 -1
  247. package/lib/core/dist/index.js +2 -0
  248. package/lib/core/dist/index.js.map +1 -1
  249. package/lib/core/dist/media.d.ts +98 -0
  250. package/lib/core/dist/media.d.ts.map +1 -0
  251. package/lib/core/dist/media.js +227 -0
  252. package/lib/core/dist/media.js.map +1 -0
  253. package/lib/core/dist/model-schema.d.ts +37 -0
  254. package/lib/core/dist/model-schema.d.ts.map +1 -0
  255. package/lib/core/dist/model-schema.js +154 -0
  256. package/lib/core/dist/model-schema.js.map +1 -0
  257. package/lib/core/package.json +1 -1
  258. package/lib/core/src/budget.ts +17 -2
  259. package/lib/core/src/contracts.ts +71 -3
  260. package/lib/core/src/destination.ts +33 -0
  261. package/lib/core/src/event-stream.ts +5 -1
  262. package/lib/core/src/host.ts +3 -0
  263. package/lib/core/src/index.ts +2 -0
  264. package/lib/core/src/media.ts +228 -0
  265. package/lib/core/src/model-schema.ts +129 -0
  266. package/lib/exporter-otlp/dist/otlp-http-json.js +1 -1
  267. package/lib/exporter-otlp/dist/otlp-http-json.js.map +1 -1
  268. package/lib/exporter-otlp/dist/signals.js +2 -2
  269. package/lib/exporter-otlp/dist/signals.js.map +1 -1
  270. package/lib/exporter-otlp/package.json +1 -1
  271. package/lib/exporter-otlp/src/otlp-http-json.ts +1 -1
  272. package/lib/exporter-otlp/src/signals.ts +2 -2
  273. package/lib/guardrails/package.json +1 -1
  274. package/lib/helpers/package.json +1 -1
  275. package/lib/memory/dist/index.js +1 -1
  276. package/lib/memory/dist/index.js.map +1 -1
  277. package/lib/memory/dist/native.d.ts.map +1 -1
  278. package/lib/memory/dist/native.js +4 -3
  279. package/lib/memory/dist/native.js.map +1 -1
  280. package/lib/memory/package.json +1 -1
  281. package/lib/memory/src/index.ts +1 -1
  282. package/lib/memory/src/native.ts +4 -3
  283. package/lib/memory-remote/package.json +1 -1
  284. package/lib/observability/package.json +1 -1
  285. package/lib/provider-anthropic/dist/index.d.ts +36 -3
  286. package/lib/provider-anthropic/dist/index.d.ts.map +1 -1
  287. package/lib/provider-anthropic/dist/index.js +128 -78
  288. package/lib/provider-anthropic/dist/index.js.map +1 -1
  289. package/lib/provider-anthropic/package.json +1 -1
  290. package/lib/provider-anthropic/src/index.ts +152 -76
  291. package/lib/provider-openai/dist/index.d.ts +71 -4
  292. package/lib/provider-openai/dist/index.d.ts.map +1 -1
  293. package/lib/provider-openai/dist/index.js +259 -96
  294. package/lib/provider-openai/dist/index.js.map +1 -1
  295. package/lib/provider-openai/package.json +1 -1
  296. package/lib/provider-openai/src/index.ts +282 -84
  297. package/lib/runtime/dist/agent.d.ts +18 -1
  298. package/lib/runtime/dist/agent.d.ts.map +1 -1
  299. package/lib/runtime/dist/agent.js +59 -3
  300. package/lib/runtime/dist/agent.js.map +1 -1
  301. package/lib/runtime/dist/composition.d.ts.map +1 -1
  302. package/lib/runtime/dist/composition.js +2 -1
  303. package/lib/runtime/dist/composition.js.map +1 -1
  304. package/lib/runtime/dist/hooks.d.ts +15 -3
  305. package/lib/runtime/dist/hooks.d.ts.map +1 -1
  306. package/lib/runtime/dist/hooks.js.map +1 -1
  307. package/lib/runtime/dist/index.d.ts +1 -1
  308. package/lib/runtime/dist/index.d.ts.map +1 -1
  309. package/lib/runtime/dist/index.js.map +1 -1
  310. package/lib/runtime/dist/response.d.ts +7 -1
  311. package/lib/runtime/dist/response.d.ts.map +1 -1
  312. package/lib/runtime/dist/response.js +23 -2
  313. package/lib/runtime/dist/response.js.map +1 -1
  314. package/lib/runtime/dist/router.d.ts +10 -3
  315. package/lib/runtime/dist/router.d.ts.map +1 -1
  316. package/lib/runtime/dist/router.js +45 -8
  317. package/lib/runtime/dist/router.js.map +1 -1
  318. package/lib/runtime/dist/runtime.d.ts +9 -1
  319. package/lib/runtime/dist/runtime.d.ts.map +1 -1
  320. package/lib/runtime/dist/runtime.js +50 -22
  321. package/lib/runtime/dist/runtime.js.map +1 -1
  322. package/lib/runtime/package.json +1 -1
  323. package/lib/runtime/src/agent.ts +69 -3
  324. package/lib/runtime/src/composition.ts +2 -1
  325. package/lib/runtime/src/hooks.ts +10 -4
  326. package/lib/runtime/src/index.ts +1 -1
  327. package/lib/runtime/src/response.ts +20 -2
  328. package/lib/runtime/src/router.ts +43 -10
  329. package/lib/runtime/src/runtime.ts +58 -27
  330. package/lib/sdk/dist/index.d.ts +9 -1
  331. package/lib/sdk/dist/index.d.ts.map +1 -1
  332. package/lib/sdk/dist/index.js +9 -1
  333. package/lib/sdk/dist/index.js.map +1 -1
  334. package/lib/sdk/package.json +1 -1
  335. package/lib/sdk/src/index.ts +9 -1
  336. package/lib/server/dist/index.d.ts +201 -3
  337. package/lib/server/dist/index.d.ts.map +1 -1
  338. package/lib/server/dist/index.js +701 -149
  339. package/lib/server/dist/index.js.map +1 -1
  340. package/lib/server/dist/inspector-bundle.d.ts +1 -1
  341. package/lib/server/dist/inspector-bundle.d.ts.map +1 -1
  342. package/lib/server/dist/inspector-bundle.js +4 -4
  343. package/lib/server/dist/inspector-bundle.js.map +1 -1
  344. package/lib/server/package.json +1 -1
  345. package/lib/server/src/index.ts +611 -135
  346. package/lib/server/src/inspector-bundle.ts +4 -4
  347. package/lib/server-node/dist/hardening.d.ts +1 -1
  348. package/lib/server-node/dist/hardening.d.ts.map +1 -1
  349. package/lib/server-node/dist/hardening.js +4 -2
  350. package/lib/server-node/dist/hardening.js.map +1 -1
  351. package/lib/server-node/dist/index.d.ts +2 -1
  352. package/lib/server-node/dist/index.d.ts.map +1 -1
  353. package/lib/server-node/dist/index.js.map +1 -1
  354. package/lib/server-node/dist/probe.js +2 -2
  355. package/lib/server-node/dist/probe.js.map +1 -1
  356. package/lib/server-node/dist/production.d.ts +8 -1
  357. package/lib/server-node/dist/production.d.ts.map +1 -1
  358. package/lib/server-node/dist/production.js +15 -2
  359. package/lib/server-node/dist/production.js.map +1 -1
  360. package/lib/server-node/package.json +1 -1
  361. package/lib/server-node/src/hardening.ts +5 -3
  362. package/lib/server-node/src/index.ts +2 -1
  363. package/lib/server-node/src/probe.ts +2 -2
  364. package/lib/server-node/src/production.ts +23 -3
  365. package/lib/skills/dist/index.d.ts +81 -0
  366. package/lib/skills/dist/index.d.ts.map +1 -0
  367. package/lib/skills/dist/index.js +311 -0
  368. package/lib/skills/dist/index.js.map +1 -0
  369. package/lib/skills/package.json +5 -0
  370. package/lib/skills/src/index.ts +260 -0
  371. package/lib/storage/dist/index.d.ts +1 -1
  372. package/lib/storage/dist/index.d.ts.map +1 -1
  373. package/lib/storage/dist/index.js.map +1 -1
  374. package/lib/storage/package.json +1 -1
  375. package/lib/storage/src/index.ts +1 -1
  376. package/lib/storage-contracts/dist/contracts.d.ts +12 -5
  377. package/lib/storage-contracts/dist/contracts.d.ts.map +1 -1
  378. package/lib/storage-contracts/dist/contracts.js +24 -6
  379. package/lib/storage-contracts/dist/contracts.js.map +1 -1
  380. package/lib/storage-contracts/dist/index.d.ts +2 -0
  381. package/lib/storage-contracts/dist/index.d.ts.map +1 -1
  382. package/lib/storage-contracts/dist/index.js +2 -0
  383. package/lib/storage-contracts/dist/index.js.map +1 -1
  384. package/lib/storage-contracts/dist/memory-contracts.js +1 -1
  385. package/lib/storage-contracts/dist/memory-contracts.js.map +1 -1
  386. package/lib/storage-contracts/dist/run-records.d.ts +100 -0
  387. package/lib/storage-contracts/dist/run-records.d.ts.map +1 -0
  388. package/lib/storage-contracts/dist/run-records.js +464 -0
  389. package/lib/storage-contracts/dist/run-records.js.map +1 -0
  390. package/lib/storage-contracts/dist/schema-digest.d.ts +17 -0
  391. package/lib/storage-contracts/dist/schema-digest.d.ts.map +1 -0
  392. package/lib/storage-contracts/dist/schema-digest.js +94 -0
  393. package/lib/storage-contracts/dist/schema-digest.js.map +1 -0
  394. package/lib/storage-contracts/dist/submission-journal.js +1 -1
  395. package/lib/storage-contracts/dist/submission-journal.js.map +1 -1
  396. package/lib/storage-contracts/dist/workflow-format5.d.ts +30 -2
  397. package/lib/storage-contracts/dist/workflow-format5.d.ts.map +1 -1
  398. package/lib/storage-contracts/dist/workflow-format5.js +57 -5
  399. package/lib/storage-contracts/dist/workflow-format5.js.map +1 -1
  400. package/lib/storage-contracts/package.json +1 -1
  401. package/lib/storage-contracts/src/contracts.ts +29 -5
  402. package/lib/storage-contracts/src/index.ts +2 -0
  403. package/lib/storage-contracts/src/memory-contracts.ts +1 -1
  404. package/lib/storage-contracts/src/run-records.ts +365 -0
  405. package/lib/storage-contracts/src/schema-digest.ts +71 -0
  406. package/lib/storage-contracts/src/submission-journal.ts +1 -1
  407. package/lib/storage-contracts/src/workflow-format5.ts +70 -6
  408. package/lib/storage-postgres/dist/index.d.ts +1 -1
  409. package/lib/storage-postgres/dist/index.d.ts.map +1 -1
  410. package/lib/storage-postgres/dist/index.js.map +1 -1
  411. package/lib/storage-postgres/dist/postgres.d.ts +15 -0
  412. package/lib/storage-postgres/dist/postgres.d.ts.map +1 -1
  413. package/lib/storage-postgres/dist/postgres.js +18 -7
  414. package/lib/storage-postgres/dist/postgres.js.map +1 -1
  415. package/lib/storage-postgres/package.json +1 -1
  416. package/lib/storage-postgres/src/index.ts +1 -1
  417. package/lib/storage-postgres/src/postgres.ts +34 -8
  418. package/lib/storage-sql/dist/aggregate-session.js +1 -1
  419. package/lib/storage-sql/dist/aggregate-session.js.map +1 -1
  420. package/lib/storage-sql/dist/scheduled-database.js +1 -1
  421. package/lib/storage-sql/dist/scheduled-database.js.map +1 -1
  422. package/lib/storage-sql/dist/workflow-tree-validation.js +1 -1
  423. package/lib/storage-sql/dist/workflow-tree-validation.js.map +1 -1
  424. package/lib/storage-sql/package.json +1 -1
  425. package/lib/storage-sql/src/aggregate-session.ts +1 -1
  426. package/lib/storage-sql/src/scheduled-database.ts +1 -1
  427. package/lib/storage-sql/src/workflow-tree-validation.ts +1 -1
  428. package/lib/storage-sqlite/dist/sqlite-database.js +4 -4
  429. package/lib/storage-sqlite/dist/sqlite-database.js.map +1 -1
  430. package/lib/storage-sqlite/dist/sqlite-worker.js +2 -2
  431. package/lib/storage-sqlite/dist/sqlite-worker.js.map +1 -1
  432. package/lib/storage-sqlite/dist/sqlite.js +1 -1
  433. package/lib/storage-sqlite/dist/sqlite.js.map +1 -1
  434. package/lib/storage-sqlite/package.json +1 -1
  435. package/lib/storage-sqlite/src/sqlite-database.ts +4 -4
  436. package/lib/storage-sqlite/src/sqlite-worker.ts +2 -2
  437. package/lib/storage-sqlite/src/sqlite.ts +1 -1
  438. package/lib/terminal/dist/index.d.ts +92 -0
  439. package/lib/terminal/dist/index.d.ts.map +1 -0
  440. package/lib/terminal/dist/index.js +397 -0
  441. package/lib/terminal/dist/index.js.map +1 -0
  442. package/lib/terminal/package.json +5 -0
  443. package/lib/terminal/src/index.ts +273 -0
  444. package/lib/testing/dist/index.d.ts +44 -1
  445. package/lib/testing/dist/index.d.ts.map +1 -1
  446. package/lib/testing/dist/index.js +63 -10
  447. package/lib/testing/dist/index.js.map +1 -1
  448. package/lib/testing/package.json +1 -1
  449. package/lib/testing/src/index.ts +83 -10
  450. package/lib/tools/dist/index.d.ts +32 -4
  451. package/lib/tools/dist/index.d.ts.map +1 -1
  452. package/lib/tools/dist/index.js +64 -11
  453. package/lib/tools/dist/index.js.map +1 -1
  454. package/lib/tools/package.json +1 -1
  455. package/lib/tools/src/index.ts +74 -13
  456. package/lib/workflows/dist/abandoned.d.ts +15 -0
  457. package/lib/workflows/dist/abandoned.d.ts.map +1 -0
  458. package/lib/workflows/dist/abandoned.js +33 -0
  459. package/lib/workflows/dist/abandoned.js.map +1 -0
  460. package/lib/workflows/dist/agent-step.d.ts +83 -0
  461. package/lib/workflows/dist/agent-step.d.ts.map +1 -0
  462. package/lib/workflows/dist/agent-step.js +161 -0
  463. package/lib/workflows/dist/agent-step.js.map +1 -0
  464. package/lib/workflows/dist/children-runtime.js +8 -8
  465. package/lib/workflows/dist/children-runtime.js.map +1 -1
  466. package/lib/workflows/dist/composite-fleet.js +2 -2
  467. package/lib/workflows/dist/composite-fleet.js.map +1 -1
  468. package/lib/workflows/dist/composite-host.d.ts.map +1 -1
  469. package/lib/workflows/dist/composite-host.js +26 -2
  470. package/lib/workflows/dist/composite-host.js.map +1 -1
  471. package/lib/workflows/dist/hook-relay.js +2 -2
  472. package/lib/workflows/dist/hook-relay.js.map +1 -1
  473. package/lib/workflows/dist/index.d.ts +1 -1
  474. package/lib/workflows/dist/index.d.ts.map +1 -1
  475. package/lib/workflows/dist/index.js.map +1 -1
  476. package/lib/workflows/dist/leadership.js +3 -3
  477. package/lib/workflows/dist/leadership.js.map +1 -1
  478. package/lib/workflows/dist/lifecycle-definition.d.ts +26 -4
  479. package/lib/workflows/dist/lifecycle-definition.d.ts.map +1 -1
  480. package/lib/workflows/dist/lifecycle-definition.js +31 -6
  481. package/lib/workflows/dist/lifecycle-definition.js.map +1 -1
  482. package/lib/workflows/dist/lifecycle-fleet.d.ts.map +1 -1
  483. package/lib/workflows/dist/lifecycle-fleet.js +9 -11
  484. package/lib/workflows/dist/lifecycle-fleet.js.map +1 -1
  485. package/lib/workflows/dist/lifecycle-host.d.ts.map +1 -1
  486. package/lib/workflows/dist/lifecycle-host.js +26 -2
  487. package/lib/workflows/dist/lifecycle-host.js.map +1 -1
  488. package/lib/workflows/dist/lifecycle-human-transport.d.ts +22 -2
  489. package/lib/workflows/dist/lifecycle-human-transport.d.ts.map +1 -1
  490. package/lib/workflows/dist/lifecycle-human-transport.js +115 -33
  491. package/lib/workflows/dist/lifecycle-human-transport.js.map +1 -1
  492. package/lib/workflows/dist/lifecycle-runtime.d.ts +62 -1
  493. package/lib/workflows/dist/lifecycle-runtime.d.ts.map +1 -1
  494. package/lib/workflows/dist/lifecycle-runtime.js +268 -64
  495. package/lib/workflows/dist/lifecycle-runtime.js.map +1 -1
  496. package/lib/workflows/dist/lifecycle.d.ts +6 -4
  497. package/lib/workflows/dist/lifecycle.d.ts.map +1 -1
  498. package/lib/workflows/dist/lifecycle.js +2 -0
  499. package/lib/workflows/dist/lifecycle.js.map +1 -1
  500. package/lib/workflows/dist/loop-runtime.d.ts.map +1 -1
  501. package/lib/workflows/dist/loop-runtime.js +38 -26
  502. package/lib/workflows/dist/loop-runtime.js.map +1 -1
  503. package/lib/workflows/dist/migration.d.ts +1 -1
  504. package/lib/workflows/dist/migration.d.ts.map +1 -1
  505. package/lib/workflows/dist/migration.js +3 -3
  506. package/lib/workflows/dist/migration.js.map +1 -1
  507. package/lib/workflows/dist/operator-transports.d.ts +24 -2
  508. package/lib/workflows/dist/operator-transports.d.ts.map +1 -1
  509. package/lib/workflows/dist/operator-transports.js +49 -3
  510. package/lib/workflows/dist/operator-transports.js.map +1 -1
  511. package/lib/workflows/dist/runtime.d.ts.map +1 -1
  512. package/lib/workflows/dist/runtime.js +34 -22
  513. package/lib/workflows/dist/runtime.js.map +1 -1
  514. package/lib/workflows/dist/saga-runtime.d.ts.map +1 -1
  515. package/lib/workflows/dist/saga-runtime.js +44 -30
  516. package/lib/workflows/dist/saga-runtime.js.map +1 -1
  517. package/lib/workflows/dist/scheduled-helpers.d.ts.map +1 -1
  518. package/lib/workflows/dist/scheduled-helpers.js +3 -3
  519. package/lib/workflows/dist/scheduled-helpers.js.map +1 -1
  520. package/lib/workflows/dist/storage-failure.d.ts +16 -0
  521. package/lib/workflows/dist/storage-failure.d.ts.map +1 -0
  522. package/lib/workflows/dist/storage-failure.js +38 -0
  523. package/lib/workflows/dist/storage-failure.js.map +1 -0
  524. package/lib/workflows/dist/sweep-position.d.ts +22 -0
  525. package/lib/workflows/dist/sweep-position.d.ts.map +1 -0
  526. package/lib/workflows/dist/sweep-position.js +45 -0
  527. package/lib/workflows/dist/sweep-position.js.map +1 -0
  528. package/lib/workflows/dist/traces.js +2 -2
  529. package/lib/workflows/dist/traces.js.map +1 -1
  530. package/lib/workflows/dist/worker.d.ts +31 -0
  531. package/lib/workflows/dist/worker.d.ts.map +1 -1
  532. package/lib/workflows/dist/worker.js +94 -0
  533. package/lib/workflows/dist/worker.js.map +1 -1
  534. package/lib/workflows/package.json +1 -1
  535. package/lib/workflows/src/abandoned.ts +32 -0
  536. package/lib/workflows/src/agent-step.ts +199 -0
  537. package/lib/workflows/src/children-runtime.ts +8 -8
  538. package/lib/workflows/src/composite-fleet.ts +2 -2
  539. package/lib/workflows/src/composite-host.ts +21 -3
  540. package/lib/workflows/src/hook-relay.ts +2 -2
  541. package/lib/workflows/src/index.ts +2 -1
  542. package/lib/workflows/src/leadership.ts +3 -3
  543. package/lib/workflows/src/lifecycle-definition.ts +50 -9
  544. package/lib/workflows/src/lifecycle-fleet.ts +7 -8
  545. package/lib/workflows/src/lifecycle-host.ts +21 -5
  546. package/lib/workflows/src/lifecycle-human-transport.ts +110 -26
  547. package/lib/workflows/src/lifecycle-runtime.ts +270 -60
  548. package/lib/workflows/src/lifecycle.ts +7 -0
  549. package/lib/workflows/src/loop-runtime.ts +34 -25
  550. package/lib/workflows/src/migration.ts +2 -2
  551. package/lib/workflows/src/operator-transports.ts +48 -4
  552. package/lib/workflows/src/runtime.ts +28 -22
  553. package/lib/workflows/src/saga-runtime.ts +38 -28
  554. package/lib/workflows/src/scheduled-helpers.ts +3 -3
  555. package/lib/workflows/src/storage-failure.ts +37 -0
  556. package/lib/workflows/src/sweep-position.ts +52 -0
  557. package/lib/workflows/src/traces.ts +2 -2
  558. package/lib/workflows/src/worker.ts +79 -1
  559. package/lib/workstream/dist/humans.d.ts +7 -3
  560. package/lib/workstream/dist/humans.d.ts.map +1 -1
  561. package/lib/workstream/dist/humans.js +13 -2
  562. package/lib/workstream/dist/humans.js.map +1 -1
  563. package/lib/workstream/dist/index.js +2 -2
  564. package/lib/workstream/dist/index.js.map +1 -1
  565. package/lib/workstream/dist/timers.js +2 -2
  566. package/lib/workstream/dist/timers.js.map +1 -1
  567. package/lib/workstream/dist/webhooks.d.ts +6 -2
  568. package/lib/workstream/dist/webhooks.d.ts.map +1 -1
  569. package/lib/workstream/dist/webhooks.js +17 -7
  570. package/lib/workstream/dist/webhooks.js.map +1 -1
  571. package/lib/workstream/package.json +1 -1
  572. package/lib/workstream/src/humans.ts +15 -5
  573. package/lib/workstream/src/index.ts +2 -2
  574. package/lib/workstream/src/timers.ts +2 -2
  575. package/lib/workstream/src/webhooks.ts +21 -8
  576. package/llms-full.txt +9766 -0
  577. package/llms.txt +92 -0
  578. package/package.json +18 -6
@@ -0,0 +1,154 @@
1
+ import { MayuraError } from './errors.js';
2
+ import { freezeJson, jsonValue } from './json.js';
3
+ import { ModelProviderError } from './contracts.js';
4
+ const nested = ['items', 'additionalItems', 'not', 'if', 'then', 'else', 'contains'];
5
+ const lists = ['anyOf', 'allOf', 'oneOf', 'prefixItems'];
6
+ const maps = ['$defs', 'definitions', 'properties', 'patternProperties'];
7
+ const isObject = (value) => !!value && typeof value === 'object' && !Array.isArray(value);
8
+ const describesObject = (node) => node['type'] === 'object' || (Array.isArray(node['type']) && node['type'].includes('object')) || (node['type'] === undefined && isObject(node['properties']));
9
+ /** Visit every subschema of a JSON Schema, with its JSON Pointer. */
10
+ function walk(node, path, visit) {
11
+ visit(node, path);
12
+ for (const key of nested) {
13
+ const child = node[key];
14
+ if (isObject(child))
15
+ walk(child, `${path}/${key}`, visit);
16
+ }
17
+ if (isObject(node['additionalProperties']))
18
+ walk(node['additionalProperties'], `${path}/additionalProperties`, visit);
19
+ for (const key of lists) {
20
+ const children = node[key];
21
+ if (Array.isArray(children))
22
+ children.forEach((child, index) => { if (isObject(child))
23
+ walk(child, `${path}/${key}/${index}`, visit); });
24
+ }
25
+ for (const key of maps) {
26
+ const children = node[key];
27
+ if (isObject(children))
28
+ for (const [name, child] of Object.entries(children))
29
+ if (isObject(child))
30
+ walk(child, `${path}/${key}/${name.replaceAll('~', '~0').replaceAll('/', '~1')}`, visit);
31
+ }
32
+ }
33
+ /**
34
+ * The JSON Schema of the values a validator accepts, when the validator can describe itself through the Standard JSON
35
+ * Schema interface (`~standard.jsonSchema`, which Zod 4.2 and later implement). That is what a model has to produce:
36
+ * a tool's input, or an agent's output. The `$schema` dialect marker is removed, and an object that says nothing about
37
+ * other keys gets `additionalProperties: false`, because a model should never add keys the validator would drop.
38
+ * Returns undefined when the validator cannot describe itself, or its description is not plain bounded JSON.
39
+ */
40
+ export function jsonSchemaOf(schema) {
41
+ let described;
42
+ try {
43
+ const standard = schema['~standard'];
44
+ const generator = standard.jsonSchema?.input;
45
+ if (typeof generator !== 'function')
46
+ return undefined;
47
+ described = generator.call(standard.jsonSchema, { target: 'draft-2020-12' });
48
+ // A plain copy: generators may attach non-enumerable metadata that is not JSON.
49
+ const copy = jsonValue(JSON.parse(JSON.stringify(described)), { maxBytes: 262_144, maxDepth: 64 });
50
+ if (!isObject(copy))
51
+ return undefined;
52
+ delete copy['$schema'];
53
+ walk(copy, '', node => { if (describesObject(node) && node['additionalProperties'] === undefined)
54
+ node['additionalProperties'] = false; });
55
+ return freezeJson(copy);
56
+ }
57
+ catch {
58
+ return undefined;
59
+ }
60
+ }
61
+ /**
62
+ * Check a JSON Schema against the strict rules model providers apply to tool inputs and structured output (OpenAI and
63
+ * Anthropic strict mode, and OpenAI-compatible servers): the root is an object, and every object lists all of its
64
+ * properties in `required` and sets `additionalProperties: false`. Returns a frozen copy to send; an object with no
65
+ * properties may leave out `properties` and `required`, which the copy states. Throws INVALID_CONFIG naming `subject`,
66
+ * the place in the schema and how to fix it.
67
+ */
68
+ export function strictJsonSchema(schema, subject) {
69
+ let copy;
70
+ try {
71
+ copy = jsonValue(schema, { maxBytes: 262_144, maxDepth: 64 });
72
+ }
73
+ catch {
74
+ throw new MayuraError('INVALID_CONFIG', `${subject} is not a plain JSON Schema object.`);
75
+ }
76
+ if (!isObject(copy) || copy['type'] !== 'object')
77
+ throw new MayuraError('INVALID_CONFIG', `${subject} must be a JSON Schema whose root has type "object".`);
78
+ const at = (path) => (path ? ` at ${path}` : '');
79
+ walk(copy, '', (node, path) => {
80
+ if (!describesObject(node))
81
+ return;
82
+ if (node['properties'] === undefined && node['required'] === undefined)
83
+ node['properties'] = {};
84
+ const properties = node['properties'];
85
+ if (!isObject(properties))
86
+ throw new MayuraError('INVALID_CONFIG', `${subject}: "properties"${at(path)} must be an object.`);
87
+ if (node['required'] === undefined && Object.keys(properties).length === 0)
88
+ node['required'] = [];
89
+ // Generators leave `required` out when nothing is required; then every property is optional.
90
+ const required = node['required'] ?? [];
91
+ if (!Array.isArray(required) || required.some(key => typeof key !== 'string') || new Set(required).size !== required.length) {
92
+ throw new MayuraError('INVALID_CONFIG', `${subject}: "required"${at(path)} must list property names once each.`);
93
+ }
94
+ const optional = Object.keys(properties).find(key => !required.includes(key));
95
+ if (optional !== undefined) {
96
+ throw new MayuraError('INVALID_CONFIG', `${subject}: property "${optional}"${at(path)} is optional, but model providers require every property. Make it nullable instead (with Zod, .nullable() rather than .optional() or .default()).`);
97
+ }
98
+ const unknown = required.find(key => !Object.hasOwn(properties, key));
99
+ if (unknown !== undefined)
100
+ throw new MayuraError('INVALID_CONFIG', `${subject}: "required"${at(path)} names "${String(unknown)}", which is not a property.`);
101
+ if (node['additionalProperties'] !== false) {
102
+ throw new MayuraError('INVALID_CONFIG', `${subject}: the object${at(path)} must set additionalProperties to false; model providers accept no open objects, records or maps.`);
103
+ }
104
+ });
105
+ return freezeJson(copy);
106
+ }
107
+ /**
108
+ * The failure for a provider's HTTP error status: 401 and 403 are credentials or model access, 402 and 429 a rate limit
109
+ * or quota, 408 and 5xx the provider being unavailable, and any other status a rejected request. `costMicros` is usage
110
+ * the provider confirmed before the failure, if any.
111
+ */
112
+ export function providerHttpFailure(status, costMicros) {
113
+ const reason = status === 401 || status === 403 ? 'authentication' : status === 402 || status === 429 ? 'rate_limited'
114
+ : status === 408 || (status >= 500 && status <= 599) ? 'unavailable' : 'rejected';
115
+ const httpStatus = Number.isSafeInteger(status) && status >= 100 && status <= 599 ? status : undefined;
116
+ return new ModelProviderError(reason, { ...(httpStatus === undefined ? {} : { httpStatus }), ...(costMicros === undefined ? {} : { costMicros }) });
117
+ }
118
+ /**
119
+ * `ModelAdapter.checkDefinition` for a provider with strict schemas: every tool needs an input schema and the agent an
120
+ * output schema (unless the adapter was given its own, `fixedOutput`), and each must follow the strict rules.
121
+ */
122
+ export function checkStrictDefinition(definition, fixedOutput) {
123
+ for (const tool of definition.tools) {
124
+ if (tool.inputJsonSchema === undefined) {
125
+ throw new MayuraError('INVALID_CONFIG', `Tool "${tool.id}" has no inputJsonSchema, and its input validator cannot describe itself. Give the tool an inputJsonSchema.`);
126
+ }
127
+ strictJsonSchema(tool.inputJsonSchema, `Tool "${tool.id}" input schema`);
128
+ }
129
+ if (fixedOutput !== undefined)
130
+ return;
131
+ if (definition.outputJsonSchema === undefined) {
132
+ throw new MayuraError('INVALID_CONFIG', 'There is no output JSON Schema: the output validator cannot describe itself. Give outputJsonSchema to defineAgent or to the model adapter.');
133
+ }
134
+ strictJsonSchema(definition.outputJsonSchema, 'The output schema');
135
+ }
136
+ /**
137
+ * The names a provider sees for tools: each Mayura tool id made safe for provider function names (letters, digits,
138
+ * `_` and `-`, at most 64 characters), so `orders.list` becomes `orders_list` and the model can match the names in
139
+ * the instructions. Two ids that sanitize alike get `_2`, `_3`, ... so every name is unique. The map is id -> name.
140
+ */
141
+ export function modelToolNames(ids) {
142
+ const names = new Map();
143
+ const taken = new Set();
144
+ for (const id of ids) {
145
+ const base = (id.replace(/[^A-Za-z0-9_-]/gu, '_') || 'tool').slice(0, 64);
146
+ let name = base;
147
+ for (let suffix = 2; taken.has(name); suffix++)
148
+ name = `${base.slice(0, 64 - String(suffix).length - 1)}_${suffix}`;
149
+ taken.add(name);
150
+ names.set(id, name);
151
+ }
152
+ return names;
153
+ }
154
+ //# sourceMappingURL=model-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-schema.js","sourceRoot":"","sources":["../src/model-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAmC,MAAM,WAAW,CAAC;AAEnF,OAAO,EAAE,kBAAkB,EAAsD,MAAM,gBAAgB,CAAC;AAExG,MAAM,MAAM,GAAG,CAAC,OAAO,EAAE,iBAAiB,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAU,CAAC;AAC9F,MAAM,KAAK,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,CAAU,CAAC;AAClE,MAAM,IAAI,GAAG,CAAC,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,mBAAmB,CAAU,CAAC;AAClF,MAAM,QAAQ,GAAG,CAAC,KAA4B,EAAuB,EAAE,CAAC,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;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AAC,MAAM,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC;QACpH,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IACvC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { MayuraError } from './errors.js';\nimport { freezeJson, jsonValue, type JsonObject, type JsonValue } from './json.js';\nimport type { Schema } from './schema.js';\nimport { ModelProviderError, type ModelDefinitionCheck, type ModelFailureReason } from './contracts.js';\n\nconst nested = ['items', 'additionalItems', 'not', 'if', 'then', 'else', 'contains'] as const;\nconst lists = ['anyOf', 'allOf', 'oneOf', 'prefixItems'] as const;\nconst maps = ['$defs', 'definitions', 'properties', 'patternProperties'] as const;\nconst isObject = (value: JsonValue | undefined): value is JsonObject => !!value && typeof value === 'object' && !Array.isArray(value);\nconst describesObject = (node: JsonObject): boolean =>\n node['type'] === 'object' || (Array.isArray(node['type']) && node['type'].includes('object')) || (node['type'] === undefined && isObject(node['properties']));\n\n/** Visit every subschema of a JSON Schema, with its JSON Pointer. */\nfunction walk(node: JsonObject, path: string, visit: (node: JsonObject, path: string) => void): void {\n visit(node, path);\n for (const key of nested) { const child = node[key]; if (isObject(child)) walk(child, `${path}/${key}`, visit); }\n if (isObject(node['additionalProperties'])) walk(node['additionalProperties'], `${path}/additionalProperties`, visit);\n for (const key of lists) { const children = node[key]; if (Array.isArray(children)) children.forEach((child, index) => { if (isObject(child)) walk(child, `${path}/${key}/${index}`, visit); }); }\n for (const key of maps) {\n const children = node[key];\n if (isObject(children)) for (const [name, child] of Object.entries(children)) if (isObject(child)) walk(child, `${path}/${key}/${name.replaceAll('~', '~0').replaceAll('/', '~1')}`, visit);\n }\n}\n\n/**\n * The JSON Schema of the values a validator accepts, when the validator can describe itself through the Standard JSON\n * Schema interface (`~standard.jsonSchema`, which Zod 4.2 and later implement). That is what a model has to produce:\n * a tool's input, or an agent's output. The `$schema` dialect marker is removed, and an object that says nothing about\n * other keys gets `additionalProperties: false`, because a model should never add keys the validator would drop.\n * Returns undefined when the validator cannot describe itself, or its description is not plain bounded JSON.\n */\nexport function jsonSchemaOf(schema: Schema): JsonObject | undefined {\n let described: unknown;\n try {\n const standard = schema['~standard'] as { readonly jsonSchema?: { readonly input?: unknown } };\n const generator = standard.jsonSchema?.input;\n if (typeof generator !== 'function') return undefined;\n described = generator.call(standard.jsonSchema, { target: 'draft-2020-12' });\n // A plain copy: generators may attach non-enumerable metadata that is not JSON.\n const copy = jsonValue(JSON.parse(JSON.stringify(described)) as unknown, { maxBytes: 262_144, maxDepth: 64 });\n if (!isObject(copy)) return undefined;\n delete copy['$schema'];\n walk(copy, '', node => { if (describesObject(node) && node['additionalProperties'] === undefined) node['additionalProperties'] = false; });\n return freezeJson(copy) as JsonObject;\n } catch { return undefined; }\n}\n\n/**\n * Check a JSON Schema against the strict rules model providers apply to tool inputs and structured output (OpenAI and\n * Anthropic strict mode, and OpenAI-compatible servers): the root is an object, and every object lists all of its\n * properties in `required` and sets `additionalProperties: false`. Returns a frozen copy to send; an object with no\n * properties may leave out `properties` and `required`, which the copy states. Throws INVALID_CONFIG naming `subject`,\n * the place in the schema and how to fix it.\n */\nexport function strictJsonSchema(schema: unknown, subject: string): JsonObject {\n let copy: JsonValue;\n try { copy = jsonValue(schema, { maxBytes: 262_144, maxDepth: 64 }); }\n catch { throw new MayuraError('INVALID_CONFIG', `${subject} is not a plain JSON Schema object.`); }\n if (!isObject(copy) || copy['type'] !== 'object') throw new MayuraError('INVALID_CONFIG', `${subject} must be a JSON Schema whose root has type \"object\".`);\n const at = (path: string): string => (path ? ` at ${path}` : '');\n walk(copy, '', (node, path) => {\n if (!describesObject(node)) return;\n if (node['properties'] === undefined && node['required'] === undefined) node['properties'] = {};\n const properties = node['properties'];\n if (!isObject(properties)) throw new MayuraError('INVALID_CONFIG', `${subject}: \"properties\"${at(path)} must be an object.`);\n if (node['required'] === undefined && Object.keys(properties).length === 0) node['required'] = [];\n // Generators leave `required` out when nothing is required; then every property is optional.\n const required = node['required'] ?? [];\n if (!Array.isArray(required) || required.some(key => typeof key !== 'string') || new Set(required).size !== required.length) {\n throw new MayuraError('INVALID_CONFIG', `${subject}: \"required\"${at(path)} must list property names once each.`);\n }\n const optional = Object.keys(properties).find(key => !required.includes(key));\n if (optional !== undefined) {\n throw new MayuraError('INVALID_CONFIG', `${subject}: property \"${optional}\"${at(path)} is optional, but model providers require every property. Make it nullable instead (with Zod, .nullable() rather than .optional() or .default()).`);\n }\n const unknown = required.find(key => !Object.hasOwn(properties, key as string));\n if (unknown !== undefined) throw new MayuraError('INVALID_CONFIG', `${subject}: \"required\"${at(path)} names \"${String(unknown)}\", which is not a property.`);\n if (node['additionalProperties'] !== false) {\n throw new MayuraError('INVALID_CONFIG', `${subject}: the object${at(path)} must set additionalProperties to false; model providers accept no open objects, records or maps.`);\n }\n });\n return freezeJson(copy) as JsonObject;\n}\n\n/**\n * The failure for a provider's HTTP error status: 401 and 403 are credentials or model access, 402 and 429 a rate limit\n * or quota, 408 and 5xx the provider being unavailable, and any other status a rejected request. `costMicros` is usage\n * the provider confirmed before the failure, if any.\n */\nexport function providerHttpFailure(status: number, costMicros?: number): ModelProviderError {\n const reason: ModelFailureReason = status === 401 || status === 403 ? 'authentication' : status === 402 || status === 429 ? 'rate_limited'\n : status === 408 || (status >= 500 && status <= 599) ? 'unavailable' : 'rejected';\n const httpStatus = Number.isSafeInteger(status) && status >= 100 && status <= 599 ? status : undefined;\n return new ModelProviderError(reason, { ...(httpStatus === undefined ? {} : { httpStatus }), ...(costMicros === undefined ? {} : { costMicros }) });\n}\n\n/**\n * `ModelAdapter.checkDefinition` for a provider with strict schemas: every tool needs an input schema and the agent an\n * output schema (unless the adapter was given its own, `fixedOutput`), and each must follow the strict rules.\n */\nexport function checkStrictDefinition(definition: ModelDefinitionCheck, fixedOutput?: JsonObject): void {\n for (const tool of definition.tools) {\n if (tool.inputJsonSchema === undefined) {\n throw new MayuraError('INVALID_CONFIG', `Tool \"${tool.id}\" has no inputJsonSchema, and its input validator cannot describe itself. Give the tool an inputJsonSchema.`);\n }\n strictJsonSchema(tool.inputJsonSchema, `Tool \"${tool.id}\" input schema`);\n }\n if (fixedOutput !== undefined) return;\n if (definition.outputJsonSchema === undefined) {\n throw new MayuraError('INVALID_CONFIG', 'There is no output JSON Schema: the output validator cannot describe itself. Give outputJsonSchema to defineAgent or to the model adapter.');\n }\n strictJsonSchema(definition.outputJsonSchema, 'The output schema');\n}\n\n/**\n * The names a provider sees for tools: each Mayura tool id made safe for provider function names (letters, digits,\n * `_` and `-`, at most 64 characters), so `orders.list` becomes `orders_list` and the model can match the names in\n * the instructions. Two ids that sanitize alike get `_2`, `_3`, ... so every name is unique. The map is id -> name.\n */\nexport function modelToolNames(ids: readonly string[]): ReadonlyMap<string, string> {\n const names = new Map<string, string>(); const taken = new Set<string>();\n for (const id of ids) {\n const base = (id.replace(/[^A-Za-z0-9_-]/gu, '_') || 'tool').slice(0, 64);\n let name = base;\n for (let suffix = 2; taken.has(name); suffix++) name = `${base.slice(0, 64 - String(suffix).length - 1)}_${suffix}`;\n taken.add(name); names.set(id, name);\n }\n return names;\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "0.1.0-dev.0",
3
+ "version": "1.0.0-rc.2",
4
4
  "private": true
5
5
  }
@@ -72,6 +72,21 @@ interface Account {
72
72
  closed: boolean;
73
73
  }
74
74
 
75
+ /**
76
+ * Say which limit stopped a call, with the numbers (costs and counts, never content), and how to raise a budget of 0,
77
+ * the default for an agent run.
78
+ */
79
+ function exhausted(path: readonly Account[], calls: number, bound: bigint): MayuraError {
80
+ const full = path.find(ancestor => calls > ancestor.maxCalls - ancestor.calls - ancestor.heldCalls);
81
+ if (full) return new MayuraError('BUDGET_EXCEEDED', `The budget's call limit of ${full.maxCalls} is used up; no new call was started.`);
82
+ const short = path.find(ancestor => bound > BigInt(ancestor.maxCostMicros) - ancestor.spent - ancestor.reserved) ?? path[0]!;
83
+ const left = BigInt(short.maxCostMicros) - short.spent - short.reserved;
84
+ const hint = short.maxCostMicros === 0
85
+ ? ' The budget is 0, so nothing that costs money can run: for an agent run, set limits.maxCostMicros in createRuntime; for a workflow, its runtime\'s maxCostMicros.'
86
+ : '';
87
+ return new MayuraError('BUDGET_EXCEEDED', `The call may cost up to ${bound} micros, but only ${left < 0n ? 0n : left} of the budget's ${short.maxCostMicros} micros are left; no new call was started.${hint}`);
88
+ }
89
+
75
90
  interface TicketState {
76
91
  readonly account: Account;
77
92
  readonly path: readonly Account[];
@@ -298,7 +313,7 @@ export class Budget {
298
313
  assertOpen(account, path);
299
314
  const bound = BigInt(maxMicros);
300
315
  if (path.some(ancestor => ancestor.heldCalls >= ancestor.maxCalls - ancestor.calls || bound > BigInt(ancestor.maxCostMicros) - ancestor.spent - ancestor.reserved)) {
301
- throw new MayuraError('BUDGET_EXCEEDED', 'Execution budget exhausted; no new call was dispatched.');
316
+ throw exhausted(path, 1, bound);
302
317
  }
303
318
  // No callback or await can interleave ancestor validation and commit.
304
319
  for (const ancestor of path) { ancestor.calls++; ancestor.reserved += bound; }
@@ -328,7 +343,7 @@ export class Budget {
328
343
  }
329
344
  if (path.some(ancestor => config.length > ancestor.maxCalls - ancestor.calls - ancestor.heldCalls
330
345
  || bound > BigInt(ancestor.maxCostMicros) - ancestor.spent - ancestor.reserved)) {
331
- throw new MayuraError('BUDGET_EXCEEDED', 'Execution budget exhausted; no future call was admitted.');
346
+ throw exhausted(path, config.length, bound);
332
347
  }
333
348
  const states: TicketState[] = [];
334
349
  const handles = config.map(operation => {
@@ -1,6 +1,7 @@
1
1
  import type { PublicError } from './errors.js';
2
2
  import { MayuraError } from './errors.js';
3
3
  import type { JsonValue, JsonObject } from './json.js';
4
+ import type { Media, MediaType } from './media.js';
4
5
 
5
6
  export interface Scope { readonly principalId: string; readonly projectId: string }
6
7
  export type Effect = 'none' | 'read' | 'write' | 'host';
@@ -62,10 +63,14 @@ export interface ModelTool {
62
63
  readonly description: string;
63
64
  readonly inputJsonSchema?: JsonObject;
64
65
  }
66
+ /**
67
+ * One message of a model conversation. `media` holds images or PDFs the model should see: with the user's input, or
68
+ * returned by a tool. It is present only when there is some, and only for adapters that declare `capabilities.media`.
69
+ */
65
70
  export type ModelMessage =
66
- | { readonly role: 'user'; readonly content: JsonValue }
71
+ | { readonly role: 'user'; readonly content: JsonValue; readonly media?: readonly Media[] }
67
72
  | { readonly role: 'assistant'; readonly calls: readonly ModelToolCall[] }
68
- | { readonly role: 'tool'; readonly callId: string; readonly toolId: string; readonly result: JsonValue };
73
+ | { readonly role: 'tool'; readonly callId: string; readonly toolId: string; readonly result: JsonValue; readonly media?: readonly Media[] };
69
74
  export interface ModelToolCall { readonly id: string; readonly toolId: string; readonly input: JsonValue }
70
75
  export interface ModelRequest {
71
76
  readonly instructions: string;
@@ -75,7 +80,23 @@ export interface ModelRequest {
75
80
  readonly maxOutputTokens: number;
76
81
  /** Opaque bounded provider protocol state; private to this run and never a public output. */
77
82
  readonly continuation?: JsonValue;
83
+ /**
84
+ * The agent's output as JSON Schema: what the final answer must look like. The runtime sends it when the agent has
85
+ * one (given, or generated from its output validator); an adapter configured with its own output schema uses that.
86
+ */
87
+ readonly outputJsonSchema?: JsonObject;
78
88
  }
89
+ /**
90
+ * What `defineAgent` asks an adapter to check once, before any run: the tools and output schema it would be sent, and
91
+ * the media the agent accepts with its input or its tools may return.
92
+ */
93
+ export interface ModelDefinitionCheck {
94
+ readonly tools: readonly ModelTool[];
95
+ readonly outputJsonSchema?: JsonObject;
96
+ readonly media?: { readonly types: readonly MediaType[]; readonly urls: boolean };
97
+ }
98
+ /** The media a model can see: which types, and whether it can take a URL (which its provider then fetches). */
99
+ export interface ModelMediaCapability { readonly types: readonly MediaType[]; readonly urls: boolean }
79
100
  export interface ModelUsage { readonly costMicros: number }
80
101
  /** A failed provider invocation may still have confirmed billable usage. No raw failure text is accepted. */
81
102
  export class ModelInvocationError extends MayuraError {
@@ -85,6 +106,46 @@ export class ModelInvocationError extends MayuraError {
85
106
  Object.freeze(this);
86
107
  }
87
108
  }
109
+ /**
110
+ * Why a model call failed, in words Mayura can show safely. An adapter picks the reason; the message is Mayura's own,
111
+ * so no provider text or credential can reach an outcome through it.
112
+ */
113
+ export type ModelFailureReason = 'authentication' | 'rate_limited' | 'unavailable' | 'timeout' | 'rejected' | 'invalid_response' | 'refused' | 'configuration';
114
+ const modelFailureReasons: readonly ModelFailureReason[] = ['authentication', 'rate_limited', 'unavailable', 'timeout', 'rejected', 'invalid_response', 'refused', 'configuration'];
115
+ export function isModelFailureReason(value: unknown): value is ModelFailureReason { return modelFailureReasons.includes(value as ModelFailureReason); }
116
+ /** The fixed public message for a failure reason, with the provider's HTTP status when there is one. */
117
+ export function modelFailureMessage(reason: ModelFailureReason, httpStatus?: number): string {
118
+ const status = httpStatus === undefined ? '' : ` (HTTP ${httpStatus})`;
119
+ switch (reason) {
120
+ case 'authentication': return `The model provider refused the credentials or access to this model${status}. Check the API key and that it may use this model.`;
121
+ case 'rate_limited': return `The model provider's rate limit or quota was reached${status}. Try again later, or raise the limit with the provider.`;
122
+ case 'unavailable': return `The model provider was unavailable${status}. Try again later.`;
123
+ case 'timeout': return `The model provider did not answer in time. Try again, or raise the adapter's timeoutMs.`;
124
+ case 'rejected': return `The model provider rejected the request${status}. Check the model name and the adapter's settings.`;
125
+ case 'invalid_response': return 'The model provider returned a response Mayura could not use, for example an answer that is not the required JSON or no token usage.';
126
+ case 'refused': return 'The model refused to answer, or stopped before finishing (for example at its token limit).';
127
+ case 'configuration': return `The model adapter could not send this request: a tool's input schema or the output schema breaks the provider's rules, no output schema was given, or the request holds media this provider cannot take.`;
128
+ }
129
+ }
130
+ /** A model call that failed for a known reason. `costMicros`, when given, is usage the provider confirmed before failing. */
131
+ export class ModelProviderError extends MayuraError {
132
+ readonly reason: ModelFailureReason;
133
+ /** Present only when the provider answered with an HTTP error status. */
134
+ declare readonly httpStatus?: number;
135
+ /** Present only when the provider confirmed usage before the failure. */
136
+ declare readonly costMicros?: number;
137
+ constructor(reason: ModelFailureReason, options: { readonly httpStatus?: number; readonly costMicros?: number } = {}) {
138
+ if (!isModelFailureReason(reason)) throw new MayuraError('INVALID_CONFIG', 'Unknown model failure reason.');
139
+ const { httpStatus, costMicros } = options;
140
+ if (httpStatus !== undefined && (!Number.isSafeInteger(httpStatus) || httpStatus < 100 || httpStatus > 599)) throw new MayuraError('INVALID_CONFIG', 'An HTTP status must be between 100 and 599.');
141
+ if (costMicros !== undefined && (!Number.isSafeInteger(costMicros) || costMicros < 0)) throw new MayuraError('INVALID_CONFIG', 'Known model usage must be a nonnegative safe integer.');
142
+ super(reason === 'configuration' ? 'INVALID_CONFIG' : 'MODEL_FAILED', modelFailureMessage(reason, httpStatus));
143
+ this.reason = reason;
144
+ if (httpStatus !== undefined) Object.defineProperty(this, 'httpStatus', { value: httpStatus, enumerable: true });
145
+ if (costMicros !== undefined) Object.defineProperty(this, 'costMicros', { value: costMicros, enumerable: true });
146
+ Object.freeze(this);
147
+ }
148
+ }
88
149
  export type ModelResponse =
89
150
  | { readonly type: 'final'; readonly output: JsonValue; readonly usage: ModelUsage; readonly continuation?: JsonValue }
90
151
  | { readonly type: 'tool_calls'; readonly calls: readonly ModelToolCall[]; readonly usage: ModelUsage; readonly continuation?: JsonValue };
@@ -99,9 +160,16 @@ export type ModelStreamEvent =
99
160
  | { readonly type: 'response'; readonly response: ModelResponse };
100
161
  export interface ModelAdapter {
101
162
  readonly id: string;
102
- readonly capabilities: { readonly tools: boolean; readonly structuredOutput: boolean };
163
+ /** What the model can do. Without `media`, it cannot see images or PDFs, and agents that accept media refuse it. */
164
+ readonly capabilities: { readonly tools: boolean; readonly structuredOutput: boolean; readonly media?: ModelMediaCapability };
103
165
  readonly maxCostMicros: number;
104
166
  generate(request: ModelRequest): Promise<ModelResponse>;
105
167
  /** Optional streamed form of `generate`, used only for agents that opt into streaming. */
106
168
  stream?(request: ModelRequest): AsyncIterable<ModelStreamEvent>;
169
+ /**
170
+ * Optional: called by `defineAgent` with the agent's tools and output schema. Throw INVALID_CONFIG, with a message
171
+ * that says what to fix, when this adapter could never send them (for example a schema its provider refuses), so the
172
+ * mistake shows when the agent is defined instead of on its first call.
173
+ */
174
+ checkDefinition?(definition: ModelDefinitionCheck): void;
107
175
  }
@@ -0,0 +1,33 @@
1
+ import { MayuraError } from './errors.js';
2
+
3
+ // Where a provider adapter sends requests, when it is not the provider's own API: a gateway or proxy such as Cloudflare
4
+ // AI Gateway. Choosing one is an explicit data-egress decision in code; nothing here is read from the environment.
5
+
6
+ /**
7
+ * Check a provider endpoint given in place of the default: an https URL ending in `path` (such as `/responses`), with no
8
+ * credentials, query or fragment in it. Returns the normalized URL. `where` names the adapter in the error.
9
+ */
10
+ export function providerEndpoint(value: unknown, path: string, where: string): string {
11
+ let url: URL;
12
+ try { url = new URL(typeof value === 'string' ? value : ''); } catch { throw new MayuraError('INVALID_CONFIG', `${where}: endpoint must be an absolute https:// URL.`); }
13
+ if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash || !url.pathname.endsWith(path) || url.href.length > 2_048) {
14
+ throw new MayuraError('INVALID_CONFIG', `${where}: endpoint must be an https:// URL ending in ${path}, without credentials, query or fragment.`);
15
+ }
16
+ return url.href;
17
+ }
18
+
19
+ /**
20
+ * Check extra request headers, such as a gateway's credential (`cf-aig-authorization`): at most 8, each a bounded
21
+ * value without line breaks. They cannot replace the headers the adapter owns (`owned`, in any case), cookies or
22
+ * transport headers. Treated as credentials by the caller: never logged or reported.
23
+ */
24
+ export function providerHeaders(value: unknown, owned: readonly string[], where: string): Readonly<Record<string, string>> {
25
+ if (value === undefined) return Object.freeze({});
26
+ const reserved = new Set([...owned.map(name => name.toLowerCase()), 'content-type', 'content-length', 'host', 'cookie', 'transfer-encoding', 'connection']);
27
+ const entries = value && typeof value === 'object' && !Array.isArray(value) ? Object.entries(value) : undefined;
28
+ if (!entries || entries.length > 8 || entries.some(([name, text]) => !/^[A-Za-z0-9-]{1,64}$/u.test(name) || reserved.has(name.toLowerCase())
29
+ || typeof text !== 'string' || !text || /[\r\n]/u.test(text) || text.length > 8_192)) {
30
+ throw new MayuraError('INVALID_CONFIG', `${where}: headers must be at most 8 extra header values; they cannot replace ${owned.join(', ')}, Content-Type, Host or cookies.`);
31
+ }
32
+ return Object.freeze(Object.fromEntries(entries as [string, string][]));
33
+ }
@@ -1,5 +1,6 @@
1
1
  import { MayuraError } from './errors.js';
2
- import type { ModelResponse, ModelStreamEvent } from './contracts.js';
2
+ import { ModelProviderError, type ModelResponse, type ModelStreamEvent } from './contracts.js';
3
+ import { providerHttpFailure } from './model-schema.js';
3
4
 
4
5
  export interface ServerSentEvent {
5
6
  /** The `event:` field, or `message` when absent. */
@@ -28,6 +29,9 @@ export async function* readServerSentEvents(response: Response, limits: ServerSe
28
29
  }
29
30
  if (!response.ok || response.redirected || !response.body || !/^text\/event-stream(?:\s*;|$)/iu.test(response.headers.get('content-type') ?? '')) {
30
31
  void response.body?.cancel().catch(() => undefined);
32
+ // A refused stream fails for the provider's reason (credentials, rate limit, ...), as a refused call does.
33
+ if (response.redirected) throw new ModelProviderError('rejected');
34
+ if (!response.ok) throw providerHttpFailure(response.status);
31
35
  throw failure();
32
36
  }
33
37
  const reader = response.body.getReader(); const decoder = new TextDecoder('utf-8', { fatal: true }); const encoder = new TextEncoder();
@@ -4,3 +4,6 @@ export { snapshotLocalGuards } from './local-guards.js';
4
4
  export { registerManagedGuardDefinition, readManagedGuardDefinition, type ManagedGuardDescriptor, type ManagedGuardLimits } from './managed-guards.js';
5
5
  export { evaluateLifecycleControl, evaluateLifecycleObserver, lifecycleHookTimeout, frozenView, snapshotHookOptions } from './lifecycle.js';
6
6
  export { readServerSentEvents, streamModelCall, type ServerSentEvent, type ServerSentEventLimits } from './event-stream.js';
7
+ export { admitMedia, base64ToBytes, bytesToBase64, encodedMediaBytes, mediaDataUrl, mediaPolicy, readMediaResult, type ResolvedMediaPolicy } from './media.js';
8
+ export { checkStrictDefinition, modelToolNames, providerHttpFailure, strictJsonSchema } from './model-schema.js';
9
+ export { providerEndpoint, providerHeaders } from './destination.js';
@@ -1,7 +1,9 @@
1
1
  export * from './errors.js';
2
2
  export * from './json.js';
3
3
  export * from './schema.js';
4
+ export { jsonSchemaOf } from './model-schema.js';
4
5
  export * from './contracts.js';
6
+ export { MEDIA_TYPES, media, mediaUrl, mediaFromBase64, mediaSummary, sniffMediaType, withMedia, type Media, type MediaType, type MediaPolicy, type MediaSummary } from './media.js';
5
7
  export * from './budget.js';
6
8
  export type { ManagedGuardDefinition, ManagedModerationVerdict } from './managed-guards.js';
7
9
  export { LIFECYCLE_STAGES, type LifecycleStage, type LifecycleDecision, type LifecycleHookContext, type LifecycleControl, type LifecycleObserver } from './lifecycle.js';