mayura 1.0.0-rc.1 → 1.0.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 (195) hide show
  1. package/README.md +4 -5
  2. package/docs/README.md +1 -0
  3. package/docs/cli/init.md +5 -5
  4. package/docs/cli/run.md +182 -150
  5. package/docs/concepts/agent.md +2 -4
  6. package/docs/concepts/costs-and-budgets.md +2 -4
  7. package/docs/concepts/permissions.md +1 -2
  8. package/docs/concepts/tools.md +2 -4
  9. package/docs/concepts/workflows.md +1 -2
  10. package/docs/guides/approvals-and-human-input.md +3 -4
  11. package/docs/guides/child-agents.md +1 -2
  12. package/docs/guides/code-mode.md +1 -2
  13. package/docs/guides/deployment.md +350 -3
  14. package/docs/guides/durable-workflows.md +8 -7
  15. package/docs/guides/guardrails.md +2 -4
  16. package/docs/guides/helpers.md +1 -1
  17. package/docs/guides/lifecycle-hooks.md +2 -4
  18. package/docs/guides/mcp.md +1 -2
  19. package/docs/guides/memory-and-context.md +1 -2
  20. package/docs/guides/model-providers.md +1 -2
  21. package/docs/guides/react.md +1 -1
  22. package/docs/guides/sagas-and-loops.md +2 -3
  23. package/docs/guides/server-and-client.md +30 -4
  24. package/docs/guides/storage.md +8 -2
  25. package/docs/guides/streaming.md +1 -2
  26. package/docs/guides/testing.md +2 -4
  27. package/docs/guides/vision.md +3 -6
  28. package/docs/guides/webhooks.md +1 -1
  29. package/docs/guides/workflow-composition.md +1 -2
  30. package/docs/guides/workflow-operations.md +4 -3
  31. package/docs/installation.md +4 -3
  32. package/docs/project/credits.md +78 -0
  33. package/docs/project/support.md +8 -2
  34. package/docs/project/versioning.md +3 -0
  35. package/docs/quickstart.md +4 -7
  36. package/docs/reference/entry-points.md +2 -1
  37. package/lib/adapter-code-docker/image/Dockerfile +1 -1
  38. package/lib/adapter-code-docker/package.json +1 -1
  39. package/lib/adapter-code-quickjs/package.json +1 -1
  40. package/lib/adapter-mcp/package.json +1 -1
  41. package/lib/artifacts/package.json +1 -1
  42. package/lib/cli/dist/agent-guide.d.ts.map +1 -1
  43. package/lib/cli/dist/agent-guide.js +1 -0
  44. package/lib/cli/dist/agent-guide.js.map +1 -1
  45. package/lib/cli/dist/application.d.ts +24 -0
  46. package/lib/cli/dist/application.d.ts.map +1 -1
  47. package/lib/cli/dist/application.js +21 -0
  48. package/lib/cli/dist/application.js.map +1 -1
  49. package/lib/cli/dist/bin.js +13 -2
  50. package/lib/cli/dist/bin.js.map +1 -1
  51. package/lib/cli/dist/index.d.ts +1 -1
  52. package/lib/cli/dist/index.d.ts.map +1 -1
  53. package/lib/cli/dist/index.js +7 -7
  54. package/lib/cli/dist/index.js.map +1 -1
  55. package/lib/cli/package.json +1 -1
  56. package/lib/cli/src/agent-guide.ts +1 -0
  57. package/lib/cli/src/application.ts +24 -0
  58. package/lib/cli/src/bin.ts +11 -2
  59. package/lib/cli/src/index.ts +8 -8
  60. package/lib/cli/starters/approval-workflow/package.json +1 -2
  61. package/lib/cli/starters/approval-workflow/src/config.ts +1 -1
  62. package/lib/cli/starters/approval-workflow/src/intake.ts +1 -2
  63. package/lib/cli/starters/approval-workflow/src/model.ts +1 -1
  64. package/lib/cli/starters/approval-workflow/src/workflow.ts +1 -2
  65. package/lib/cli/starters/cli-agent/package.json +1 -2
  66. package/lib/cli/starters/cli-agent/src/assistant.ts +1 -2
  67. package/lib/cli/starters/cli-agent/src/config.ts +1 -1
  68. package/lib/cli/starters/cli-agent/src/workspace.ts +1 -2
  69. package/lib/cli/starters/event-automation/package.json +1 -2
  70. package/lib/cli/starters/event-automation/src/config.ts +1 -1
  71. package/lib/cli/starters/event-automation/src/model.ts +1 -1
  72. package/lib/cli/starters/event-automation/src/send-sample.ts +1 -1
  73. package/lib/cli/starters/event-automation/src/tracker/main.ts +1 -1
  74. package/lib/cli/starters/event-automation/src/tracker/mcp-server.ts +1 -1
  75. package/lib/cli/starters/event-automation/src/tracker/tickets.ts +1 -1
  76. package/lib/cli/starters/event-automation/src/tracker-tools.ts +1 -1
  77. package/lib/cli/starters/event-automation/src/triage.ts +1 -2
  78. package/lib/cli/starters/event-automation/src/workflows.ts +1 -1
  79. package/lib/cli/starters/research-team/package.json +1 -2
  80. package/lib/cli/starters/research-team/src/config.ts +1 -1
  81. package/lib/cli/starters/research-team/src/desk.ts +1 -2
  82. package/lib/cli/starters/research-team/src/library/index.ts +1 -2
  83. package/lib/cli/starters/research-team/src/model.ts +1 -1
  84. package/lib/cli/starters/research-team/src/team.ts +1 -2
  85. package/lib/cli/starters/research-team/src/workflow.ts +1 -2
  86. package/lib/cli/starters/support-agent/package.json +1 -2
  87. package/lib/cli/starters/support-agent/src/assistant.ts +1 -2
  88. package/lib/cli/starters/support-agent/src/config.ts +1 -1
  89. package/lib/cli/starters/support-agent/src/model.ts +1 -1
  90. package/lib/cli/starters/support-agent/src/returns.ts +1 -2
  91. package/lib/cli/templates/basic-agent.ts +1 -2
  92. package/lib/cli/templates/capability-policy.ts +1 -2
  93. package/lib/cli/templates/durable-approval.ts +1 -1
  94. package/lib/cli/templates/parallel-research.ts +1 -2
  95. package/lib/cli/templates/typed-tool-runner.ts +1 -2
  96. package/lib/client/package.json +1 -1
  97. package/lib/client-react/package.json +1 -1
  98. package/lib/code-mode/package.json +1 -1
  99. package/lib/code-mode-workflows/package.json +1 -1
  100. package/lib/context/package.json +1 -1
  101. package/lib/core/package.json +1 -1
  102. package/lib/exporter-otlp/dist/otlp-http-json.js +1 -1
  103. package/lib/exporter-otlp/dist/otlp-http-json.js.map +1 -1
  104. package/lib/exporter-otlp/dist/signals.js +2 -2
  105. package/lib/exporter-otlp/dist/signals.js.map +1 -1
  106. package/lib/exporter-otlp/package.json +1 -1
  107. package/lib/exporter-otlp/src/otlp-http-json.ts +1 -1
  108. package/lib/exporter-otlp/src/signals.ts +2 -2
  109. package/lib/guardrails/package.json +1 -1
  110. package/lib/helpers/package.json +1 -1
  111. package/lib/memory/package.json +1 -1
  112. package/lib/memory-remote/package.json +1 -1
  113. package/lib/observability/package.json +1 -1
  114. package/lib/provider-anthropic/package.json +1 -1
  115. package/lib/provider-openai/package.json +1 -1
  116. package/lib/runtime/package.json +1 -1
  117. package/lib/sdk/dist/index.d.ts +9 -1
  118. package/lib/sdk/dist/index.d.ts.map +1 -1
  119. package/lib/sdk/dist/index.js +9 -1
  120. package/lib/sdk/dist/index.js.map +1 -1
  121. package/lib/sdk/package.json +1 -1
  122. package/lib/sdk/src/index.ts +9 -1
  123. package/lib/server/dist/index.d.ts +9 -0
  124. package/lib/server/dist/index.d.ts.map +1 -1
  125. package/lib/server/dist/index.js +15 -2
  126. package/lib/server/dist/index.js.map +1 -1
  127. package/lib/server/package.json +1 -1
  128. package/lib/server/src/index.ts +24 -3
  129. package/lib/server-node/package.json +1 -1
  130. package/lib/skills/package.json +1 -1
  131. package/lib/storage/dist/index.d.ts +1 -1
  132. package/lib/storage/dist/index.d.ts.map +1 -1
  133. package/lib/storage/dist/index.js.map +1 -1
  134. package/lib/storage/package.json +1 -1
  135. package/lib/storage/src/index.ts +1 -1
  136. package/lib/storage-contracts/package.json +1 -1
  137. package/lib/storage-postgres/dist/index.d.ts +1 -1
  138. package/lib/storage-postgres/dist/index.d.ts.map +1 -1
  139. package/lib/storage-postgres/dist/index.js.map +1 -1
  140. package/lib/storage-postgres/dist/postgres.d.ts +15 -0
  141. package/lib/storage-postgres/dist/postgres.d.ts.map +1 -1
  142. package/lib/storage-postgres/dist/postgres.js +12 -1
  143. package/lib/storage-postgres/dist/postgres.js.map +1 -1
  144. package/lib/storage-postgres/package.json +1 -1
  145. package/lib/storage-postgres/src/index.ts +1 -1
  146. package/lib/storage-postgres/src/postgres.ts +28 -2
  147. package/lib/storage-sql/package.json +1 -1
  148. package/lib/storage-sqlite/package.json +1 -1
  149. package/lib/terminal/package.json +1 -1
  150. package/lib/testing/package.json +1 -1
  151. package/lib/tools/package.json +1 -1
  152. package/lib/workflows/dist/abandoned.d.ts +15 -0
  153. package/lib/workflows/dist/abandoned.d.ts.map +1 -0
  154. package/lib/workflows/dist/abandoned.js +33 -0
  155. package/lib/workflows/dist/abandoned.js.map +1 -0
  156. package/lib/workflows/dist/agent-step.d.ts +3 -2
  157. package/lib/workflows/dist/agent-step.d.ts.map +1 -1
  158. package/lib/workflows/dist/agent-step.js.map +1 -1
  159. package/lib/workflows/dist/composite-host.d.ts.map +1 -1
  160. package/lib/workflows/dist/composite-host.js +26 -2
  161. package/lib/workflows/dist/composite-host.js.map +1 -1
  162. package/lib/workflows/dist/index.d.ts +1 -1
  163. package/lib/workflows/dist/index.d.ts.map +1 -1
  164. package/lib/workflows/dist/index.js.map +1 -1
  165. package/lib/workflows/dist/lifecycle-host.d.ts.map +1 -1
  166. package/lib/workflows/dist/lifecycle-host.js +26 -2
  167. package/lib/workflows/dist/lifecycle-host.js.map +1 -1
  168. package/lib/workflows/dist/lifecycle-runtime.d.ts.map +1 -1
  169. package/lib/workflows/dist/lifecycle-runtime.js +21 -0
  170. package/lib/workflows/dist/lifecycle-runtime.js.map +1 -1
  171. package/lib/workflows/dist/runtime.d.ts.map +1 -1
  172. package/lib/workflows/dist/runtime.js +22 -1
  173. package/lib/workflows/dist/runtime.js.map +1 -1
  174. package/lib/workflows/dist/sweep-position.d.ts +22 -0
  175. package/lib/workflows/dist/sweep-position.d.ts.map +1 -0
  176. package/lib/workflows/dist/sweep-position.js +45 -0
  177. package/lib/workflows/dist/sweep-position.js.map +1 -0
  178. package/lib/workflows/dist/worker.d.ts +31 -0
  179. package/lib/workflows/dist/worker.d.ts.map +1 -1
  180. package/lib/workflows/dist/worker.js +94 -0
  181. package/lib/workflows/dist/worker.js.map +1 -1
  182. package/lib/workflows/package.json +1 -1
  183. package/lib/workflows/src/abandoned.ts +32 -0
  184. package/lib/workflows/src/agent-step.ts +3 -2
  185. package/lib/workflows/src/composite-host.ts +21 -3
  186. package/lib/workflows/src/index.ts +2 -1
  187. package/lib/workflows/src/lifecycle-host.ts +21 -5
  188. package/lib/workflows/src/lifecycle-runtime.ts +16 -0
  189. package/lib/workflows/src/runtime.ts +16 -1
  190. package/lib/workflows/src/sweep-position.ts +52 -0
  191. package/lib/workflows/src/worker.ts +79 -1
  192. package/lib/workstream/package.json +1 -1
  193. package/llms-full.txt +572 -95
  194. package/llms.txt +3 -1
  195. package/package.json +3 -2
package/README.md CHANGED
@@ -14,8 +14,8 @@ with a CLI that scaffolds, runs and operates your project.
14
14
 
15
15
  ## Features
16
16
 
17
- - **Typed agents and tools.** Zod (or any Standard Schema validator) for inputs and outputs, validated at runtime and
18
- inferred in TypeScript.
17
+ - **Typed agents and tools.** Schemas for inputs and outputs with the built-in `z` (Zod), or any Standard Schema
18
+ validator, validated at runtime and inferred in TypeScript.
19
19
  - **Any model.** OpenAI, Anthropic, and OpenAI-compatible providers such as Groq, Gemini, Mistral, DeepSeek, xAI,
20
20
  OpenRouter, Together, Fireworks, Azure OpenAI and local servers. Route between providers with automatic failover.
21
21
  - **Explicit permissions.** A run can use only the models, tools and effects you grant; nothing is implied by
@@ -47,15 +47,14 @@ with a CLI that scaffolds, runs and operates your project.
47
47
  Mayura runs on Node.js 22 (22.12 or later) or 24 (24.14.1 or later).
48
48
 
49
49
  ```bash
50
- npm install mayura zod
50
+ npm install mayura
51
51
  ```
52
52
 
53
53
  An agent that answers weather questions with a tool, on OpenAI:
54
54
 
55
55
  ```ts
56
- import { createRuntime, defineAgent, defineTool } from 'mayura';
56
+ import { createRuntime, defineAgent, defineTool, z } from 'mayura';
57
57
  import { openAIResponses } from 'mayura/provider-openai';
58
- import { z } from 'zod';
59
58
 
60
59
  const getWeather = defineTool({
61
60
  id: 'weather.get', version: '1', description: 'Current weather for a city.',
package/docs/README.md CHANGED
@@ -79,3 +79,4 @@ Mayura is a TypeScript framework for building AI agents, typed tools and durable
79
79
  - [Versioning and stability](project/versioning.md): what each release promises.
80
80
  - [Supported platforms](project/support.md): Node.js versions, operating systems and getting help.
81
81
  - [Security model](project/security.md): what Mayura protects against, and reporting vulnerabilities.
82
+ - [Credits](project/credits.md): the open-source projects and standards Mayura is built on.
package/docs/cli/init.md CHANGED
@@ -137,14 +137,14 @@ Browse the source at
137
137
 
138
138
  | Template | What it shows | Extra packages |
139
139
  |---|---|---|
140
- | `typed-tool-runner` | An agent calling a typed tool | `zod` |
141
- | `basic-agent` | The smallest agent with a structured answer | `zod` |
142
- | `durable-approval` | A durable workflow step that waits for human approval, on SQLite | `better-sqlite3`, `zod` |
143
- | `parallel-research` | Two child agents run in parallel and joined | `zod` |
140
+ | `typed-tool-runner` | An agent calling a typed tool | none |
141
+ | `basic-agent` | The smallest agent with a structured answer | none |
142
+ | `durable-approval` | A durable workflow step that waits for human approval, on SQLite | `better-sqlite3` |
143
+ | `parallel-research` | Two child agents run in parallel and joined | none |
144
144
  | `native-memory` | Scoped memory with provenance, correction and deletion | `better-sqlite3` |
145
145
  | `guarded-streaming-app` | An authenticated local server, guarded output and the browser client | none |
146
146
  | `code-mode-workflow` | An approval-gated durable Code Mode workflow in the QuickJS sandbox | `better-sqlite3`, `quickjs-emscripten-core`, `@jitl/quickjs-wasmfile-release-sync` |
147
- | `capability-policy` | A tool that needs an explicit permission, granted and denied | `zod` |
147
+ | `capability-policy` | A tool that needs an explicit permission, granted and denied | none |
148
148
 
149
149
  Browse the source at
150
150
  [packages/cli/templates](https://github.com/KartikeyAI/mayura/blob/main/packages/cli/templates).
package/docs/cli/run.md CHANGED
@@ -1,150 +1,182 @@
1
- ---
2
- title: "Serve, worker and migrate"
3
- description: "Run your application in production with mayura serve, mayura worker and mayura migrate, and write the module they load."
4
- ---
5
-
6
- In production a Mayura application usually runs as separate processes: one or more **servers** that answer HTTP
7
- requests, one or more **workers** that advance durable workflows, and a **migration** step that updates the storage
8
- schema before new code starts. The CLI runs each of these from one application module that you write:
9
-
10
- ```bash
11
- mayura migrate --app dist/src/app.js
12
- mayura serve --app dist/src/app.js
13
- mayura worker --app dist/src/app.js --probe-host 0.0.0.0 --probe-port 9090
14
- ```
15
-
16
- The CLI owns only the process lifecycle: starting, handling Ctrl+C or `SIGTERM`, graceful shutdown and worker probes.
17
- Your module wires its own storage, agents, workflows and authentication with its own installed `mayura` package.
18
-
19
- ## The application module
20
-
21
- The module's default export is an object with up to four functions. Wrap it in `defineMayuraApplication` from
22
- `mayura/cli`, which checks the shape:
23
-
24
- ```ts
25
- import { defineMayuraApplication } from 'mayura/cli';
26
- import { listenProductionServer } from 'mayura/server-node';
27
- import { createWorkflowWorker } from 'mayura/workflows';
28
-
29
- // openServices, agents, authenticate and workerUnits stand for your own code: storage, agent
30
- // registrations, token verification and the workflow runtimes the worker drives.
31
- let services: Promise<Services> | undefined;
32
- const ready = () => (services ??= openServices());
33
-
34
- export default defineMayuraApplication({
35
- async server() {
36
- await ready();
37
- return listenProductionServer({
38
- publicOrigin: 'https://agents.example.com',
39
- hostname: '0.0.0.0',
40
- port: 8080,
41
- tls: { terminatedBy: 'proxy' },
42
- agents,
43
- authenticate,
44
- });
45
- },
46
- async worker() {
47
- return createWorkflowWorker({ units: workerUnits(await ready()) });
48
- },
49
- async migrate() {
50
- await ready();
51
- return { schemaVersion: 1 };
52
- },
53
- async shutdown() {
54
- if (services) await (await services).close();
55
- },
56
- });
57
- ```
58
-
59
- | Function | Used by | Must return |
60
- |---|---|---|
61
- | `server()` | `mayura serve` | A running server with `isAccepting()` and `close()`, such as the result of `listenProductionServer` from `mayura/server-node` |
62
- | `worker()` | `mayura worker` | A worker with `start()`, `isReady()` and `drain()`, such as the result of `createWorkflowWorker` from `mayura/workflows` |
63
- | `migrate()` | `mayura migrate` | Any JSON-serializable report, printed when the migration finishes |
64
- | `shutdown()` | all three | Nothing. Runs last, after the server closes, the worker drains or the migration ends, for example to close storage. |
65
-
66
- Define at least one function, and only these four. A command whose function is missing fails with `INVALID_CONFIG`,
67
- for example `mayura worker` on a module without `worker`.
68
-
69
- `--app` must name a single regular `.js` or `.mjs` file: build your TypeScript first. Directories, symbolic links and
70
- other file types are refused, and the CLI imports nothing else. For a complete, working module, see `src/app.ts` in
71
- any starter, such as
72
- [approval-workflow](https://github.com/KartikeyAI/mayura/blob/main/packages/cli/starters/approval-workflow/src/app.ts).
73
-
74
- ## mayura serve
75
-
76
- `mayura serve --app <module>` calls `server()` and keeps it running. On the first Ctrl+C or `SIGTERM` it closes the
77
- server gracefully, then calls `shutdown()`. A second signal during shutdown forces the process to exit.
78
-
79
- `listenProductionServer` stops accepting new requests, fails its readiness check so load balancers stop routing to
80
- it, and drains in-flight requests before it closes. See [Deployment](../guides/deployment.md).
81
-
82
- ## mayura worker
83
-
84
- `mayura worker --app <module>` calls `worker()`, then `start()`. On the first Ctrl+C or `SIGTERM` it drains the
85
- worker, waiting for in-flight work to finish, then calls `shutdown()`.
86
-
87
- | Option | Meaning |
88
- |---|---|
89
- | `--drain-timeout-ms <ms>` | How long draining may take, from 1 to 300000. Default 30000. |
90
- | `--probe-port <port>` | Serve HTTP health probes on this port. Off by default. |
91
- | `--probe-host <host>` | The address the probes listen on. Default `127.0.0.1`. |
92
-
93
- With `--probe-port`, the worker answers two probes for your orchestrator:
94
-
95
- - `GET /livez` returns 200 while the process is running, and 503 once it starts stopping.
96
- - `GET /readyz` returns 200 while the worker reports itself ready, and 503 when it does not, for example while it is
97
- draining. A worker with a leadership lease is ready only while it can confirm the lease in storage.
98
-
99
- The probe listener comes from `mayura/server-node` in your application's own installation, so `mayura` must be
100
- installed alongside the module. In a container, use `--probe-host 0.0.0.0` so the orchestrator can reach it.
101
-
102
- When the worker stops, it reports whether the drain finished in time, and how much work was interrupted if it did not.
103
-
104
- ## mayura migrate
105
-
106
- `mayura migrate --app <module>` calls `migrate()` once, then `shutdown()`, and exits. Nothing else starts. Run it
107
- before deploying new code, for example as a release step or an init container:
108
-
109
- ```bash
110
- mayura migrate --app dist/src/app.js --json
111
- ```
112
-
113
- Migrations are explicit and one-way. Mayura's storage refuses data in a format or schema version it does not
114
- support; see [Storage](../guides/storage.md).
115
-
116
- ## Output
117
-
118
- In a terminal each command prints one line per stage:
119
-
120
- ```text
121
- ● Worker started, probes on 0.0.0.0:9090. Press Ctrl+C to stop.
122
- ● Stopping… (press Ctrl+C again to force)
123
- ✔ Stopped
124
- ```
125
-
126
- When output is piped, as in most deployments, each stage is one JSON line, then the command's final JSON document:
127
-
128
- ```text
129
- {"event":"worker-started","probe":{"hostname":"0.0.0.0","port":9090}}
130
- {"event":"stopping"}
131
- {"event":"stopped","drained":true,"interrupted":0}
132
- ```
133
-
134
- `serve` prints `serving`, `stopping` and `stopped` events. `migrate` prints `{ "status": "migrated", "report": <report> }`,
135
- where the report is whatever your `migrate()` returned. See [CLI overview](overview.md) for exit codes and errors.
136
-
137
- ## Good to know
138
-
139
- - `serve` and `worker` do not load `.env`. Set configuration in the real process environment. Only
140
- [mayura dev](dev.md) reads `.env`.
141
- - Run servers and workers as separate processes. With a leadership lease (`createWorkflowLeadership` from
142
- `mayura/workflows`), several worker replicas can run without driving the same workflows twice.
143
-
144
- ## Related
145
-
146
- - [Deployment](../guides/deployment.md)
147
- - [Develop with mayura dev](dev.md)
148
- - [Server and client](../guides/server-and-client.md)
149
- - [Workflow operations](../guides/workflow-operations.md)
150
- - [Storage](../guides/storage.md)
1
+ ---
2
+ title: "Serve, worker and migrate"
3
+ description: "Run your application in production with mayura serve, mayura worker and mayura migrate, and write the module they load."
4
+ ---
5
+
6
+ In production a Mayura application usually runs as separate processes: one or more **servers** that answer HTTP
7
+ requests, one or more **workers** that advance durable workflows, and a **migration** step that updates the storage
8
+ schema before new code starts. The CLI runs each of these from one application module that you write:
9
+
10
+ ```bash
11
+ mayura migrate --app dist/src/app.js
12
+ mayura serve --app dist/src/app.js
13
+ mayura worker --app dist/src/app.js --probe-host 0.0.0.0 --probe-port 9090
14
+ ```
15
+
16
+ The CLI owns only the process lifecycle: starting, handling Ctrl+C or `SIGTERM`, graceful shutdown and worker probes.
17
+ Your module wires its own storage, agents, workflows and authentication with its own installed `mayura` package.
18
+
19
+ ## The application module
20
+
21
+ The module's default export is an object with up to four functions. Wrap it in `defineMayuraApplication` from
22
+ `mayura/cli`, which checks the shape:
23
+
24
+ ```ts
25
+ import { defineMayuraApplication } from 'mayura/cli';
26
+ import { listenProductionServer } from 'mayura/server-node';
27
+ import { createWorkflowWorker } from 'mayura/workflows';
28
+
29
+ // openServices, agents, authenticate and workerUnits stand for your own code: storage, agent
30
+ // registrations, token verification and the workflow runtimes the worker drives.
31
+ let services: Promise<Services> | undefined;
32
+ const ready = () => (services ??= openServices());
33
+
34
+ export default defineMayuraApplication({
35
+ async server() {
36
+ await ready();
37
+ return listenProductionServer({
38
+ publicOrigin: 'https://agents.example.com',
39
+ hostname: '0.0.0.0',
40
+ port: 8080,
41
+ tls: { terminatedBy: 'proxy' },
42
+ agents,
43
+ authenticate,
44
+ });
45
+ },
46
+ async worker() {
47
+ return createWorkflowWorker({ units: workerUnits(await ready()) });
48
+ },
49
+ async migrate() {
50
+ await ready();
51
+ return { schemaVersion: 1 };
52
+ },
53
+ async shutdown() {
54
+ if (services) await (await services).close();
55
+ },
56
+ });
57
+ ```
58
+
59
+ | Function | Used by | Must return |
60
+ |---|---|---|
61
+ | `server()` | `mayura serve` | A running server with `isAccepting()` and `close()`, such as the result of `listenProductionServer` from `mayura/server-node` |
62
+ | `worker()` | `mayura worker` | A worker with `start()`, `isReady()` and `drain()`, such as the result of `createWorkflowWorker` from `mayura/workflows` |
63
+ | `migrate()` | `mayura migrate` | Any JSON-serializable report, printed when the migration finishes |
64
+ | `shutdown()` | all three | Nothing. Runs last, after the server closes, the worker drains or the migration ends, for example to close storage. |
65
+
66
+ Define at least one function, and only these four. A command whose function is missing fails with `INVALID_CONFIG`,
67
+ for example `mayura worker` on a module without `worker`.
68
+
69
+ `--app` must name a single regular `.js` or `.mjs` file: build your TypeScript first. Directories, symbolic links and
70
+ other file types are refused, and the CLI imports nothing else. For a complete, working module, see `src/app.ts` in
71
+ any starter, such as
72
+ [approval-workflow](https://github.com/KartikeyAI/mayura/blob/main/packages/cli/starters/approval-workflow/src/app.ts).
73
+
74
+ ## mayura serve
75
+
76
+ `mayura serve --app <module>` calls `server()` and keeps it running. On the first Ctrl+C or `SIGTERM` it closes the
77
+ server gracefully, then calls `shutdown()`. A second signal during shutdown forces the process to exit.
78
+
79
+ `listenProductionServer` stops accepting new requests, fails its readiness check so load balancers stop routing to
80
+ it, and drains in-flight requests before it closes. See [Deployment](../guides/deployment.md).
81
+
82
+ ## mayura worker
83
+
84
+ `mayura worker --app <module>` calls `worker()`, then `start()`. On the first Ctrl+C or `SIGTERM` it drains the
85
+ worker, waiting for in-flight work to finish, then calls `shutdown()`.
86
+
87
+ | Option | Meaning |
88
+ |---|---|
89
+ | `--drain-timeout-ms <ms>` | How long draining may take, from 1 to 300000. Default 30000. |
90
+ | `--probe-port <port>` | Serve HTTP health probes on this port. Off by default. |
91
+ | `--probe-host <host>` | The address the probes listen on. Default `127.0.0.1`. |
92
+
93
+ With `--probe-port`, the worker answers two probes for your orchestrator:
94
+
95
+ - `GET /livez` returns 200 while the process is running, and 503 once it starts stopping.
96
+ - `GET /readyz` returns 200 while the worker reports itself ready, and 503 when it does not, for example while it is
97
+ draining. A worker with a leadership lease is ready only while it can confirm the lease in storage.
98
+
99
+ The probe listener comes from `mayura/server-node` in your application's own installation, so `mayura` must be
100
+ installed alongside the module. In a container, use `--probe-host 0.0.0.0` so the orchestrator can reach it.
101
+
102
+ When the worker stops, it reports whether the drain finished in time, and how much work was interrupted if it did not.
103
+
104
+ ### Run once
105
+
106
+ With `--once`, the worker advances everything that is due and exits, instead of running until it is stopped. Use it
107
+ from a scheduled job (a Cloud Run job, a Kubernetes CronJob) or a scheduler that starts a process every minute:
108
+
109
+ ```bash
110
+ mayura worker --app dist/src/app.js --once --budget-ms 50000
111
+ ```
112
+
113
+ It takes the leadership lease once, runs passes until every host has completed a sweep of its runs or the budget runs
114
+ out, then releases the lease and calls `shutdown()`. A pass that has started finishes, so keep the budget below your
115
+ platform's time limit by at least your longest tool's `timeoutMs`. Each host keeps its place in storage, so if a sweep
116
+ does not finish, the next run continues from there rather than starting over.
117
+
118
+ | Option | Meaning |
119
+ |---|---|
120
+ | `--once` | Advance what is due, then exit. Not with `--probe-port`, `--probe-host` or `--drain-timeout-ms`. |
121
+ | `--budget-ms <ms>` | Stop starting passes after this long, from 1000 to 3600000. Default 60000. |
122
+
123
+ It prints a report and its status:
124
+
125
+ | Status | Meaning | Exit code |
126
+ |---|---|---|
127
+ | `succeeded` | Every host completed a sweep. | 0 |
128
+ | `incomplete` | The budget ran out first; the next run continues. Run it more often or give it more time. | 0 |
129
+ | `standby` | Another replica holds the lease, so this run did nothing. | 0 |
130
+ | `held` | An operator holds the fleet, so nothing was driven. | 0 |
131
+ | `failed` | A pass failed, for example because storage was unavailable. | 1 |
132
+
133
+ The worker must come from `createWorkflowWorker`, whose `runOnce` this calls; in your own code, such as a scheduled
134
+ function, call `worker.runOnce({ budgetMs })` directly.
135
+
136
+ ## mayura migrate
137
+
138
+ `mayura migrate --app <module>` calls `migrate()` once, then `shutdown()`, and exits. Nothing else starts. Run it
139
+ before deploying new code, for example as a release step or an init container:
140
+
141
+ ```bash
142
+ mayura migrate --app dist/src/app.js --json
143
+ ```
144
+
145
+ Migrations are explicit and one-way. Mayura's storage refuses data in a format or schema version it does not
146
+ support; see [Storage](../guides/storage.md).
147
+
148
+ ## Output
149
+
150
+ In a terminal each command prints one line per stage:
151
+
152
+ ```text
153
+ ● Worker started, probes on 0.0.0.0:9090. Press Ctrl+C to stop.
154
+ ● Stopping… (press Ctrl+C again to force)
155
+ ✔ Stopped
156
+ ```
157
+
158
+ When output is piped, as in most deployments, each stage is one JSON line, then the command's final JSON document:
159
+
160
+ ```text
161
+ {"event":"worker-started","probe":{"hostname":"0.0.0.0","port":9090}}
162
+ {"event":"stopping"}
163
+ {"event":"stopped","drained":true,"interrupted":0}
164
+ ```
165
+
166
+ `serve` prints `serving`, `stopping` and `stopped` events. `migrate` prints `{ "status": "migrated", "report": <report> }`,
167
+ where the report is whatever your `migrate()` returned. See [CLI overview](overview.md) for exit codes and errors.
168
+
169
+ ## Good to know
170
+
171
+ - `serve` and `worker` do not load `.env`. Set configuration in the real process environment. Only
172
+ [mayura dev](dev.md) reads `.env`.
173
+ - Run servers and workers as separate processes. With a leadership lease (`createWorkflowLeadership` from
174
+ `mayura/workflows`), several worker replicas can run without driving the same workflows twice.
175
+
176
+ ## Related
177
+
178
+ - [Deployment](../guides/deployment.md)
179
+ - [Develop with mayura dev](dev.md)
180
+ - [Server and client](../guides/server-and-client.md)
181
+ - [Workflow operations](../guides/workflow-operations.md)
182
+ - [Storage](../guides/storage.md)
@@ -14,9 +14,8 @@ This example runs offline. `scriptedModel` from `mayura/testing` is a test model
14
14
  it, so you can see the whole loop without an API key. It does not read the instructions or reason.
15
15
 
16
16
  ```ts
17
- import { createRuntime, defineAgent, defineTool } from 'mayura';
17
+ import { createRuntime, defineAgent, defineTool, z } from 'mayura';
18
18
  import { scriptedModel } from 'mayura/testing';
19
- import { z } from 'zod';
20
19
 
21
20
  const add = defineTool({
22
21
  id: 'math.add',
@@ -121,9 +120,8 @@ enforces on the answer.
121
120
  `model:<adapter id>`: `model:openai.responses`, `model:anthropic.messages`, `model:scripted`.
122
121
 
123
122
  ```ts
124
- import { defineAgent } from 'mayura';
123
+ import { defineAgent, z } from 'mayura';
125
124
  import { openAIResponses } from 'mayura/provider-openai';
126
- import { z } from 'zod';
127
125
 
128
126
  const answerer = defineAgent({
129
127
  id: 'answerer',
@@ -13,9 +13,8 @@ The run budget comes from the runtime's `limits.maxCostMicros`, and its default
13
13
  ## A budgeted agent
14
14
 
15
15
  ```ts
16
- import { createRuntime, defineAgent } from 'mayura';
16
+ import { createRuntime, defineAgent, z } from 'mayura';
17
17
  import { openAIResponses } from 'mayura/provider-openai';
18
- import { z } from 'zod';
19
18
 
20
19
  const agent = defineAgent({
21
20
  id: 'summarizer',
@@ -95,8 +94,7 @@ SMS, a search query. It is reserved before the tool runs. If the tool does not s
95
94
  charged the full amount. To charge less, report the real cost once from inside `execute`:
96
95
 
97
96
  ```ts
98
- import { defineTool } from 'mayura';
99
- import { z } from 'zod';
97
+ import { defineTool, z } from 'mayura';
100
98
 
101
99
  export const sendSms = defineTool({
102
100
  id: 'sms.send',
@@ -74,8 +74,7 @@ to every tool (`context.scope`), guard and hook, so your code can limit what it
74
74
  project:
75
75
 
76
76
  ```ts
77
- import { defineTool } from 'mayura';
78
- import { z } from 'zod';
77
+ import { defineTool, z } from 'mayura';
79
78
 
80
79
  export const myOrder = defineTool({
81
80
  id: 'orders.mine',
@@ -11,8 +11,7 @@ allowed to use it, its input is validated before your code runs, and its output
11
11
  ## A read-only tool
12
12
 
13
13
  ```ts
14
- import { defineTool } from 'mayura';
15
- import { z } from 'zod';
14
+ import { defineTool, z } from 'mayura';
16
15
 
17
16
  export const lookupOrder = defineTool({
18
17
  id: 'orders.lookup',
@@ -129,8 +128,7 @@ tool with the same id; `extraTimeoutMs` lengthens its timeout for checks that wa
129
128
  description.
130
129
 
131
130
  ```ts
132
- import { ToolRefusal, defineTool, withPreflight } from 'mayura';
133
- import { z } from 'zod';
131
+ import { ToolRefusal, defineTool, withPreflight, z } from 'mayura';
134
132
 
135
133
  const refundOrder = defineTool({
136
134
  id: 'orders.refund',
@@ -23,9 +23,8 @@ as a tool.
23
23
  ## A workflow at a glance
24
24
 
25
25
  ```ts
26
- import { defineTool } from 'mayura';
26
+ import { defineTool, z } from 'mayura';
27
27
  import { defineWorkflowLifecycle } from 'mayura/workflows/lifecycle';
28
- import { z } from 'zod';
29
28
 
30
29
  const invoice = z.object({ invoiceId: z.string(), amountCents: z.number().int() });
31
30
 
@@ -17,10 +17,9 @@ need information only a person has: a corrected address, a choice between two pl
17
17
  This workflow checks a refund and waits for approval before paying it. The approver is verified by `verifyHuman`.
18
18
 
19
19
  ```ts
20
- import { defineTool } from 'mayura';
20
+ import { defineTool, z } from 'mayura';
21
21
  import { createSqliteStore } from 'mayura/storage-sqlite';
22
22
  import { createWorkflowLifecycleRuntime, defineWorkflowLifecycle } from 'mayura/workflows/lifecycle';
23
- import { z } from 'zod';
24
23
 
25
24
  const refund = z.object({ refundId: z.string(), amountCents: z.number().int().positive() });
26
25
 
@@ -110,7 +109,7 @@ the step's output, which later steps can bind to.
110
109
 
111
110
  ```ts
112
111
  import type { WorkflowLifecycleNode } from 'mayura/workflows/lifecycle';
113
- import { z } from 'zod';
112
+ import { z } from 'mayura';
114
113
 
115
114
  const pickPlan: WorkflowLifecycleNode = {
116
115
  kind: 'human', id: 'pick-plan', dependsOn: ['propose'],
@@ -233,7 +232,7 @@ agent tool needs a person's input that your own code will act on later.
233
232
 
234
233
  ```ts
235
234
  import { createHumanWorkStream } from 'mayura/workstream/humans';
236
- import { z } from 'zod';
235
+ import { z } from 'mayura';
237
236
 
238
237
  const humans = createHumanWorkStream({
239
238
  store, scope, streamId: 'address-checks',
@@ -14,9 +14,8 @@ There are three ways to start one:
14
14
  - **`runtime.speculate`**: your code runs a few alternative children and keeps at most one verified answer.
15
15
 
16
16
  ```ts
17
- import { agentAsTool, createRuntime, defineAgent } from 'mayura';
17
+ import { agentAsTool, createRuntime, defineAgent, z } from 'mayura';
18
18
  import { openAIResponses } from 'mayura/provider-openai';
19
- import { z } from 'zod';
20
19
 
21
20
  const openai = openAIResponses({
22
21
  apiKey: process.env.OPENAI_API_KEY!, model: process.env.OPENAI_MODEL!,
@@ -12,8 +12,7 @@ tool broker, with its permissions, budget and guards.
12
12
  ```ts
13
13
  import { createQuickJsSandboxAdapter } from 'mayura/adapter-code-quickjs';
14
14
  import { createCodeMode, defineCodeProgram } from 'mayura/code-mode';
15
- import { Budget, defineTool, invokeTool, type JsonValue, type Outcome } from 'mayura';
16
- import { z } from 'zod';
15
+ import { Budget, defineTool, invokeTool, type JsonValue, type Outcome, z } from 'mayura';
17
16
 
18
17
  const price = defineTool({
19
18
  id: 'catalog.price', version: '1', description: 'Price of one product in cents.',