@wheatley-ai/cli 0.0.0-stage → 0.1.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 (210) hide show
  1. package/LICENSE +15 -0
  2. package/bin/wheatley.js +2 -0
  3. package/dist/approvals/gate.js +278 -0
  4. package/dist/approvals/module.js +21 -0
  5. package/dist/approvals/policy.js +30 -0
  6. package/dist/approvals/preview.js +17 -0
  7. package/dist/approvals/prompt.js +98 -0
  8. package/dist/approvals/rpc.js +16 -0
  9. package/dist/approvals/store.js +159 -0
  10. package/dist/cli/ask.js +94 -0
  11. package/dist/cli/chat.js +178 -0
  12. package/dist/cli/client.js +104 -0
  13. package/dist/cli/daemon.js +35 -0
  14. package/dist/cli/init.js +72 -0
  15. package/dist/cli/io.js +45 -0
  16. package/dist/cli/main.js +206 -0
  17. package/dist/cli/session.js +86 -0
  18. package/dist/cli/status.js +46 -0
  19. package/dist/contracts/approval-gate.js +1 -0
  20. package/dist/contracts/config-store.js +1 -0
  21. package/dist/contracts/delivery-outbox.js +1 -0
  22. package/dist/contracts/errors.js +28 -0
  23. package/dist/contracts/extension-catalog.js +1 -0
  24. package/dist/contracts/maintenance-runner.js +1 -0
  25. package/dist/contracts/memory-store.js +1 -0
  26. package/dist/contracts/module.js +7 -0
  27. package/dist/contracts/tool-names.js +43 -0
  28. package/dist/contracts/trigger-runtime.js +1 -0
  29. package/dist/contracts/webhook-ingress.js +2 -0
  30. package/dist/core/agent/agent-store.js +300 -0
  31. package/dist/core/agent/agent.js +1033 -0
  32. package/dist/core/agent/gated-models.js +89 -0
  33. package/dist/core/agent/pi-adapter.js +401 -0
  34. package/dist/core/agent/policy-gate.js +44 -0
  35. package/dist/core/builtin-catalog.js +20 -0
  36. package/dist/core/concurrency-gate.js +87 -0
  37. package/dist/core/config/atomic-file.js +72 -0
  38. package/dist/core/config/config-store.js +138 -0
  39. package/dist/core/config/pending-restart.js +53 -0
  40. package/dist/core/config/secret-references.js +69 -0
  41. package/dist/core/config/secret-store.js +147 -0
  42. package/dist/core/config/validate.js +55 -0
  43. package/dist/core/config/yaml-patch.js +57 -0
  44. package/dist/core/daemon-socket.js +96 -0
  45. package/dist/core/daemon.js +97 -0
  46. package/dist/core/deployment.js +379 -0
  47. package/dist/core/error-message.js +4 -0
  48. package/dist/core/events.js +25 -0
  49. package/dist/core/logger.js +81 -0
  50. package/dist/core/models.js +27 -0
  51. package/dist/core/module.js +42 -0
  52. package/dist/core/rpc/agent-handlers.js +82 -0
  53. package/dist/core/rpc/deployment-handlers.js +63 -0
  54. package/dist/core/rpc/router.js +96 -0
  55. package/dist/core/server/connection.js +170 -0
  56. package/dist/core/server/control-server.js +152 -0
  57. package/dist/core/server/static-files.js +79 -0
  58. package/dist/core/supervisor.js +215 -0
  59. package/dist/core/tool-routing.js +41 -0
  60. package/dist/dashboard/assets/index-C9Rul5vE.css +2 -0
  61. package/dist/dashboard/assets/index-DXzGGAxQ.js +11 -0
  62. package/dist/dashboard/assets/sheet-presentation-DMljN0LW.js +1 -0
  63. package/dist/dashboard/index.html +18 -0
  64. package/dist/db/database.js +16 -0
  65. package/dist/db/migrations.js +184 -0
  66. package/dist/frontends/discord/approvals.js +199 -0
  67. package/dist/frontends/discord/attachments.js +115 -0
  68. package/dist/frontends/discord/binding.js +489 -0
  69. package/dist/frontends/discord/config.js +107 -0
  70. package/dist/frontends/discord/discord-runtime.js +52 -0
  71. package/dist/frontends/discord/errors.js +61 -0
  72. package/dist/frontends/discord/live-runs.js +189 -0
  73. package/dist/frontends/discord/module.js +39 -0
  74. package/dist/frontends/discord/recent-ids.js +20 -0
  75. package/dist/frontends/slack/approval-blocks.js +98 -0
  76. package/dist/frontends/slack/attachments.js +102 -0
  77. package/dist/frontends/slack/background-tasks.js +20 -0
  78. package/dist/frontends/slack/binding.js +405 -0
  79. package/dist/frontends/slack/bounded.js +9 -0
  80. package/dist/frontends/slack/chat-id.js +17 -0
  81. package/dist/frontends/slack/config.js +80 -0
  82. package/dist/frontends/slack/delivery-errors.js +81 -0
  83. package/dist/frontends/slack/inbound.js +76 -0
  84. package/dist/frontends/slack/live-progress.js +190 -0
  85. package/dist/frontends/slack/manifest.js +66 -0
  86. package/dist/frontends/slack/outbound.js +160 -0
  87. package/dist/frontends/slack/schemas.js +99 -0
  88. package/dist/frontends/slack/signature.js +30 -0
  89. package/dist/frontends/slack/slack-client.js +60 -0
  90. package/dist/frontends/slack/slack-frontend.js +38 -0
  91. package/dist/frontends/slack/state-store.js +112 -0
  92. package/dist/frontends/telegram/approvals.js +134 -0
  93. package/dist/frontends/telegram/attachments.js +126 -0
  94. package/dist/frontends/telegram/binding.js +144 -0
  95. package/dist/frontends/telegram/chat-key.js +43 -0
  96. package/dist/frontends/telegram/client.js +88 -0
  97. package/dist/frontends/telegram/config.js +93 -0
  98. package/dist/frontends/telegram/delivery.js +215 -0
  99. package/dist/frontends/telegram/frontend.js +42 -0
  100. package/dist/frontends/telegram/grammy-client.js +122 -0
  101. package/dist/frontends/telegram/http-status.js +6 -0
  102. package/dist/frontends/telegram/inbound.js +149 -0
  103. package/dist/frontends/telegram/live.js +182 -0
  104. package/dist/frontends/telegram/markdown.js +103 -0
  105. package/dist/frontends/telegram/recent-set.js +18 -0
  106. package/dist/frontends/telegram/serial-queue.js +23 -0
  107. package/dist/index.js +7 -0
  108. package/dist/mcp/content.js +61 -0
  109. package/dist/mcp/mcp-module.js +35 -0
  110. package/dist/mcp/mcp-tool-provider.js +93 -0
  111. package/dist/mcp/server-connection.js +297 -0
  112. package/dist/memory/consolidation.js +260 -0
  113. package/dist/memory/format.js +5 -0
  114. package/dist/memory/migrations.js +76 -0
  115. package/dist/memory/module.js +69 -0
  116. package/dist/memory/names.js +20 -0
  117. package/dist/memory/rows.js +22 -0
  118. package/dist/memory/rpc.js +49 -0
  119. package/dist/memory/snapshots.js +12 -0
  120. package/dist/memory/store.js +281 -0
  121. package/dist/memory/tools.js +158 -0
  122. package/dist/node_modules/@wheatley-ai/protocol/dist/config.d.ts +262 -0
  123. package/dist/node_modules/@wheatley-ai/protocol/dist/config.js +120 -0
  124. package/dist/node_modules/@wheatley-ai/protocol/dist/domain.d.ts +512 -0
  125. package/dist/node_modules/@wheatley-ai/protocol/dist/domain.js +441 -0
  126. package/dist/node_modules/@wheatley-ai/protocol/dist/duration.d.ts +8 -0
  127. package/dist/node_modules/@wheatley-ai/protocol/dist/duration.js +28 -0
  128. package/dist/node_modules/@wheatley-ai/protocol/dist/events.d.ts +234 -0
  129. package/dist/node_modules/@wheatley-ai/protocol/dist/events.js +124 -0
  130. package/dist/node_modules/@wheatley-ai/protocol/dist/index.d.ts +11 -0
  131. package/dist/node_modules/@wheatley-ai/protocol/dist/index.js +11 -0
  132. package/dist/node_modules/@wheatley-ai/protocol/dist/json-rpc.d.ts +81 -0
  133. package/dist/node_modules/@wheatley-ai/protocol/dist/json-rpc.js +157 -0
  134. package/dist/node_modules/@wheatley-ai/protocol/dist/json.d.ts +14 -0
  135. package/dist/node_modules/@wheatley-ai/protocol/dist/json.js +23 -0
  136. package/dist/node_modules/@wheatley-ai/protocol/dist/limits.d.ts +17 -0
  137. package/dist/node_modules/@wheatley-ai/protocol/dist/limits.js +17 -0
  138. package/dist/node_modules/@wheatley-ai/protocol/dist/methods.d.ts +1107 -0
  139. package/dist/node_modules/@wheatley-ai/protocol/dist/methods.js +201 -0
  140. package/dist/node_modules/@wheatley-ai/protocol/dist/rpc-error.d.ts +45 -0
  141. package/dist/node_modules/@wheatley-ai/protocol/dist/rpc-error.js +44 -0
  142. package/dist/node_modules/@wheatley-ai/protocol/dist/validation.d.ts +20 -0
  143. package/dist/node_modules/@wheatley-ai/protocol/dist/validation.js +11 -0
  144. package/dist/node_modules/@wheatley-ai/protocol/package.json +14 -0
  145. package/dist/node_modules/@wheatley-ai/sdk/dist/chunk.d.ts +11 -0
  146. package/dist/node_modules/@wheatley-ai/sdk/dist/chunk.js +162 -0
  147. package/dist/node_modules/@wheatley-ai/sdk/dist/commands.d.ts +48 -0
  148. package/dist/node_modules/@wheatley-ai/sdk/dist/commands.js +39 -0
  149. package/dist/node_modules/@wheatley-ai/sdk/dist/delivery-error.d.ts +29 -0
  150. package/dist/node_modules/@wheatley-ai/sdk/dist/delivery-error.js +27 -0
  151. package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.d.ts +236 -0
  152. package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.js +2 -0
  153. package/dist/node_modules/@wheatley-ai/sdk/dist/index.d.ts +10 -0
  154. package/dist/node_modules/@wheatley-ai/sdk/dist/index.js +7 -0
  155. package/dist/node_modules/@wheatley-ai/sdk/dist/json.d.ts +9 -0
  156. package/dist/node_modules/@wheatley-ai/sdk/dist/json.js +2 -0
  157. package/dist/node_modules/@wheatley-ai/sdk/dist/logger.d.ts +18 -0
  158. package/dist/node_modules/@wheatley-ai/sdk/dist/logger.js +2 -0
  159. package/dist/node_modules/@wheatley-ai/sdk/dist/plugin.d.ts +48 -0
  160. package/dist/node_modules/@wheatley-ai/sdk/dist/plugin.js +5 -0
  161. package/dist/node_modules/@wheatley-ai/sdk/dist/schema.d.ts +69 -0
  162. package/dist/node_modules/@wheatley-ai/sdk/dist/schema.js +3 -0
  163. package/dist/node_modules/@wheatley-ai/sdk/dist/tool.d.ts +69 -0
  164. package/dist/node_modules/@wheatley-ai/sdk/dist/tool.js +3 -0
  165. package/dist/node_modules/@wheatley-ai/sdk/package.json +11 -0
  166. package/dist/ops/backup.js +185 -0
  167. package/dist/ops/cli.js +194 -0
  168. package/dist/ops/command-runner.js +34 -0
  169. package/dist/ops/dashboard-assets.js +37 -0
  170. package/dist/ops/migrate.js +63 -0
  171. package/dist/ops/module.js +47 -0
  172. package/dist/ops/paths.js +55 -0
  173. package/dist/ops/service.js +376 -0
  174. package/dist/ops/update.js +145 -0
  175. package/dist/plugins/contributions.js +89 -0
  176. package/dist/plugins/guards.js +111 -0
  177. package/dist/plugins/load.js +76 -0
  178. package/dist/plugins/module.js +31 -0
  179. package/dist/plugins/provider.js +349 -0
  180. package/dist/plugins/schema.js +69 -0
  181. package/dist/skills/disclosure.js +142 -0
  182. package/dist/skills/discovery.js +120 -0
  183. package/dist/skills/fs-errors.js +8 -0
  184. package/dist/skills/read-skill-tool.js +3 -0
  185. package/dist/skills/skill-file.js +90 -0
  186. package/dist/skills/skills.js +140 -0
  187. package/dist/tools/builtin-tools.js +61 -0
  188. package/dist/tools/input.js +45 -0
  189. package/dist/tools/paths.js +6 -0
  190. package/dist/tools/read-file.js +65 -0
  191. package/dist/tools/shell.js +156 -0
  192. package/dist/tools/write-file.js +58 -0
  193. package/dist/triggers/delivery-codec.js +99 -0
  194. package/dist/triggers/delivery-outbox.js +313 -0
  195. package/dist/triggers/due-timer.js +14 -0
  196. package/dist/triggers/errors.js +2 -0
  197. package/dist/triggers/migrations.js +91 -0
  198. package/dist/triggers/module.js +106 -0
  199. package/dist/triggers/rpc.js +35 -0
  200. package/dist/triggers/schedule.js +52 -0
  201. package/dist/triggers/sql-row.js +59 -0
  202. package/dist/triggers/trigger-runtime.js +445 -0
  203. package/dist/triggers/trigger-tools.js +210 -0
  204. package/dist/triggers/webhook-ingress.js +212 -0
  205. package/dist/wiring/agent-services.js +151 -0
  206. package/dist/wiring/catalog.js +81 -0
  207. package/dist/wiring/daemon.js +83 -0
  208. package/dist/wiring/frontend-host.js +268 -0
  209. package/package.json +50 -3
  210. package/README.md +0 -3
@@ -0,0 +1,14 @@
1
+ /** A timer never sleeps longer than this, so that a scheduler notices a changed system clock within a minute. */
2
+ const MAX_TIMER_MS = 60_000;
3
+ /**
4
+ * Arms an unref'd timer that calls `onDue` at `due` (epoch milliseconds), or after `MAX_TIMER_MS` if `due` is later.
5
+ * A `due` in the past calls `onDue` on the next timer turn. Returns `undefined` and arms nothing if `due` is `null`.
6
+ */
7
+ export const armDueTimer = (due, now, onDue) => {
8
+ if (due === null) {
9
+ return undefined;
10
+ }
11
+ const timer = setTimeout(onDue, Math.min(Math.max(due - now, 0), MAX_TIMER_MS));
12
+ timer.unref();
13
+ return timer;
14
+ };
@@ -0,0 +1,2 @@
1
+ /** The message of a caught value, for logs and Tool results. */
2
+ export const errorText = (cause) => cause instanceof Error ? cause.message : String(cause);
@@ -0,0 +1,91 @@
1
+ /** The migration namespace and table prefix of the Trigger runtime. */
2
+ export const TRIGGERS_NAMESPACE = "triggers";
3
+ /** The migration namespace and table prefix of the Delivery Outbox. */
4
+ export const OUTBOX_NAMESPACE = "outbox";
5
+ /**
6
+ * Tables of the Trigger runtime in `agent.sqlite` (ADR 0010):
7
+ * - `triggers_definitions`: every Trigger with its schedule state. `seq` keeps the creation order.
8
+ * - `triggers_webhook_events`: accepted webhook events that wait for their firing, in arrival order.
9
+ * - `triggers_runs`: Runs that a Trigger started, with the `suppress_delivery` flag.
10
+ */
11
+ export const triggersMigrations = [
12
+ {
13
+ name: "create triggers, webhook events, and trigger runs",
14
+ up: (db) => {
15
+ db.exec(`
16
+ CREATE TABLE triggers_definitions (
17
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
18
+ id TEXT NOT NULL UNIQUE,
19
+ name TEXT NOT NULL,
20
+ prompt TEXT NOT NULL,
21
+ schedule_json TEXT NOT NULL,
22
+ target_binding_id TEXT NOT NULL,
23
+ target_chat_id TEXT NOT NULL,
24
+ conversation_mode TEXT NOT NULL CHECK (conversation_mode IN ('per_firing', 'shared')),
25
+ paused INTEGER NOT NULL CHECK (paused IN (0, 1)),
26
+ created_by TEXT NOT NULL CHECK (created_by IN ('agent', 'operator')),
27
+ created_at INTEGER NOT NULL,
28
+ last_fired_at INTEGER,
29
+ next_fire_at INTEGER
30
+ ) STRICT;
31
+
32
+ CREATE INDEX triggers_definitions_due ON triggers_definitions (next_fire_at)
33
+ WHERE next_fire_at IS NOT NULL AND paused = 0;
34
+
35
+ CREATE TABLE triggers_webhook_events (
36
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
37
+ id TEXT NOT NULL UNIQUE,
38
+ trigger_id TEXT NOT NULL REFERENCES triggers_definitions (id) ON DELETE CASCADE,
39
+ received_at INTEGER NOT NULL,
40
+ headers_json TEXT NOT NULL,
41
+ body BLOB NOT NULL
42
+ ) STRICT;
43
+
44
+ CREATE INDEX triggers_webhook_events_trigger ON triggers_webhook_events (trigger_id);
45
+
46
+ CREATE TABLE triggers_runs (
47
+ run_id TEXT PRIMARY KEY,
48
+ trigger_id TEXT NOT NULL,
49
+ firing_id TEXT NOT NULL,
50
+ started_at INTEGER NOT NULL,
51
+ suppressed INTEGER NOT NULL DEFAULT 0 CHECK (suppressed IN (0, 1))
52
+ ) STRICT;
53
+
54
+ CREATE INDEX triggers_runs_started ON triggers_runs (started_at);
55
+ `);
56
+ },
57
+ version: 1,
58
+ },
59
+ ];
60
+ /**
61
+ * Tables of the Delivery Outbox in `agent.sqlite` (ADR 0007, ADR 0010). `seq` keeps the FIFO order per
62
+ * `(binding_id, chat_id)`.
63
+ */
64
+ export const outboxMigrations = [
65
+ {
66
+ name: "create outbox entries",
67
+ up: (db) => {
68
+ db.exec(`
69
+ CREATE TABLE outbox_entries (
70
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
71
+ delivery_id TEXT NOT NULL UNIQUE,
72
+ binding_id TEXT NOT NULL,
73
+ chat_id TEXT NOT NULL,
74
+ delivery_json TEXT NOT NULL,
75
+ source_conversation_id TEXT,
76
+ status TEXT NOT NULL CHECK (status IN ('pending', 'delivered', 'failed', 'dismissed')),
77
+ attempts INTEGER NOT NULL,
78
+ budget_started_at INTEGER NOT NULL,
79
+ next_attempt_at INTEGER,
80
+ last_error TEXT,
81
+ permanent INTEGER NOT NULL CHECK (permanent IN (0, 1)),
82
+ created_at INTEGER NOT NULL,
83
+ finished_at INTEGER
84
+ ) STRICT;
85
+
86
+ CREATE INDEX outbox_entries_chat ON outbox_entries (status, binding_id, chat_id, seq);
87
+ `);
88
+ },
89
+ version: 1,
90
+ },
91
+ ];
@@ -0,0 +1,106 @@
1
+ import { DEFAULT_WEBHOOK_HOST, DEFAULT_WEBHOOK_PORT, } from "@wheatley-ai/protocol";
2
+ import { SqliteDeliveryOutbox } from "./delivery-outbox.js";
3
+ import { OUTBOX_NAMESPACE, outboxMigrations, TRIGGERS_NAMESPACE, triggersMigrations, } from "./migrations.js";
4
+ import { createOutboxRpc, createTriggerRpc } from "./rpc.js";
5
+ import { SqliteTriggerRuntime } from "./trigger-runtime.js";
6
+ import { createTriggerToolProvider } from "./trigger-tools.js";
7
+ import { HttpWebhookIngress } from "./webhook-ingress.js";
8
+ export { OUTBOX_NAMESPACE, outboxMigrations, TRIGGERS_NAMESPACE, triggersMigrations, } from "./migrations.js";
9
+ export { createOutboxRpc, createTriggerRpc } from "./rpc.js";
10
+ /**
11
+ * The Triggers module of an Agent (ADR 0010): the Trigger runtime and scheduler over `agent.sqlite`, the Trigger
12
+ * Tools, and the `triggers.*` JSON-RPC handlers. `start` starts the scheduler, and `stop` stops it.
13
+ */
14
+ export const triggersModule = {
15
+ create: (ctx, deps) => {
16
+ const runtime = new SqliteTriggerRuntime({
17
+ agentName: ctx.agentName,
18
+ clock: ctx.clock,
19
+ db: ctx.db,
20
+ events: ctx.events,
21
+ log: ctx.log,
22
+ runner: deps.runner,
23
+ });
24
+ return {
25
+ api: {
26
+ runtime,
27
+ tools: createTriggerToolProvider({
28
+ bindingIds: deps.bindingIds,
29
+ clock: ctx.clock,
30
+ runtime,
31
+ suppressDelivery: runtime.suppressDelivery,
32
+ }),
33
+ },
34
+ rpc: createTriggerRpc(runtime),
35
+ start: runtime.start,
36
+ stop: runtime.stop,
37
+ };
38
+ },
39
+ migrations: triggersMigrations,
40
+ name: TRIGGERS_NAMESPACE,
41
+ scope: "agent",
42
+ };
43
+ /**
44
+ * The Delivery Outbox module of an Agent (ADR 0007, ADR 0010): the durable FIFO outbox over `agent.sqlite` and the
45
+ * `deliveries.*` JSON-RPC handlers. `start` starts deliveries, and `stop` stops them.
46
+ */
47
+ export const deliveryOutboxModule = {
48
+ create: (ctx, deps) => {
49
+ const outbox = new SqliteDeliveryOutbox({
50
+ agentName: ctx.agentName,
51
+ clock: ctx.clock,
52
+ db: ctx.db,
53
+ deps,
54
+ events: ctx.events,
55
+ log: ctx.log,
56
+ random: deps.random ?? Math.random,
57
+ });
58
+ return {
59
+ api: outbox,
60
+ rpc: createOutboxRpc(outbox),
61
+ start: outbox.start,
62
+ stop: outbox.stop,
63
+ };
64
+ },
65
+ migrations: outboxMigrations,
66
+ name: OUTBOX_NAMESPACE,
67
+ scope: "agent",
68
+ };
69
+ /**
70
+ * The Webhook Ingress module of the Deployment (ADR 0010, ADR 0012). `start` opens the HTTP listener on
71
+ * `webhooks.host` (default `127.0.0.1`) and `webhooks.port` (default `8788`) if `webhooks.enabled` is true. It has no
72
+ * JSON-RPC handlers.
73
+ */
74
+ export const webhookIngressModule = {
75
+ create: (ctx, deps) => {
76
+ const ingress = new HttpWebhookIngress({
77
+ agent: deps.agent,
78
+ clock: ctx.clock,
79
+ log: ctx.log,
80
+ secrets: deps.secrets,
81
+ });
82
+ const enabled = deps.config?.enabled === true;
83
+ return {
84
+ api: {
85
+ address: () => {
86
+ const address = ingress.address();
87
+ return address === undefined
88
+ ? undefined
89
+ : { host: address.address, port: address.port };
90
+ },
91
+ enabled,
92
+ handle: ingress.handle,
93
+ registerFrontendRoute: ingress.registerFrontendRoute,
94
+ },
95
+ rpc: {},
96
+ start: async () => {
97
+ if (enabled) {
98
+ await ingress.listen(deps.config?.host ?? DEFAULT_WEBHOOK_HOST, deps.config?.port ?? DEFAULT_WEBHOOK_PORT);
99
+ }
100
+ },
101
+ stop: ingress.close,
102
+ };
103
+ },
104
+ name: "webhook_ingress",
105
+ scope: "deployment",
106
+ };
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The Operator JSON-RPC handlers for the Triggers of one Agent: `triggers.list`, `triggers.create`, `triggers.update`,
3
+ * `triggers.delete`, and `triggers.fire`. The Operator manages every Trigger kind, including `webhook`.
4
+ *
5
+ * Errors: the handlers pass on the `SeamError` values of `TriggerRuntime`, for example `not_found` for an unknown
6
+ * Trigger and `invalid_name` for an invalid cron expression. The router converts them.
7
+ */
8
+ export const createTriggerRpc = (runtime) => ({
9
+ "triggers.create": async ({ trigger }) => ({
10
+ trigger: await runtime.create(trigger, "operator"),
11
+ }),
12
+ "triggers.delete": async ({ triggerId }) => ({
13
+ deleted: await runtime.delete(triggerId, "operator"),
14
+ }),
15
+ "triggers.fire": async ({ triggerId }) => await runtime.fire(triggerId),
16
+ "triggers.list": async () => ({ triggers: [...(await runtime.list())] }),
17
+ "triggers.update": async ({ patch, triggerId }) => ({
18
+ trigger: await runtime.update(triggerId, patch, "operator"),
19
+ }),
20
+ });
21
+ /**
22
+ * The Operator JSON-RPC handlers for the failed deliveries of one Agent: `deliveries.listFailed`, `deliveries.retry`,
23
+ * and `deliveries.dismiss`. Retry and dismiss resolve `false` for an entry that is not `failed`.
24
+ */
25
+ export const createOutboxRpc = (outbox) => ({
26
+ "deliveries.dismiss": async ({ deliveryId }) => ({
27
+ dismissed: await outbox.dismiss(deliveryId),
28
+ }),
29
+ "deliveries.listFailed": async () => ({
30
+ deliveries: [...(await outbox.listFailed())],
31
+ }),
32
+ "deliveries.retry": async ({ deliveryId }) => ({
33
+ requeued: await outbox.retry(deliveryId),
34
+ }),
35
+ });
@@ -0,0 +1,52 @@
1
+ import { Cron } from "croner";
2
+ import { SeamError } from "../contracts/errors.js";
3
+ import { errorText } from "./errors.js";
4
+ const isTimeZone = (timeZone) => {
5
+ try {
6
+ new Intl.DateTimeFormat("en-US", { timeZone }).format(0);
7
+ return true;
8
+ }
9
+ catch {
10
+ return false;
11
+ }
12
+ };
13
+ /**
14
+ * Parses a 5-field cron expression in an IANA time zone. Throws `SeamError("invalid_name")` for an invalid expression,
15
+ * an unknown time zone, or an expression that never matches, for example `0 0 30 2 *`.
16
+ */
17
+ export const parseCron = (expression, timezone) => {
18
+ if (!isTimeZone(timezone)) {
19
+ throw new SeamError("invalid_name", `Invalid time zone: ${timezone}`);
20
+ }
21
+ let cron;
22
+ try {
23
+ cron = new Cron(expression.trim(), { mode: "5-part", timezone });
24
+ }
25
+ catch (error) {
26
+ throw new SeamError("invalid_name", `Invalid cron expression: ${expression} (${errorText(error)})`);
27
+ }
28
+ if (cron.nextRun() === null) {
29
+ throw new SeamError("invalid_name", `Cron expression never matches: ${expression}`);
30
+ }
31
+ return cron;
32
+ };
33
+ /** Throws `SeamError("invalid_name")` if `schedule` is a `cron` schedule with an invalid expression or time zone. */
34
+ export const assertValidSchedule = (schedule) => {
35
+ if (schedule.kind === "cron") {
36
+ parseCron(schedule.schedule, schedule.timezone);
37
+ }
38
+ };
39
+ /** The first `cron` tick strictly after `after` (epoch milliseconds), or `null` if no tick follows. */
40
+ export const nextCronTick = (schedule, after) => parseCron(schedule.schedule, schedule.timezone)
41
+ .nextRun(new Date(after))
42
+ ?.getTime() ?? null;
43
+ /**
44
+ * The first fire time of a new or rescheduled Trigger: the next future tick for `cron`, the `at` time for `once` (also
45
+ * if it is in the past, so that an overdue `once` catches up one time), and `null` for `webhook`.
46
+ */
47
+ export const initialFireAt = (schedule, now) => {
48
+ if (schedule.kind === "cron") {
49
+ return nextCronTick(schedule, now);
50
+ }
51
+ return schedule.kind === "once" ? Date.parse(schedule.at) : null;
52
+ };
@@ -0,0 +1,59 @@
1
+ const columnError = (column, expected) => new TypeError(`Column ${column} is not ${expected}`);
2
+ const isText = (value) => typeof value === "string";
3
+ const isNumber = (value) => typeof value === "number";
4
+ const isBigint = (value) => typeof value === "bigint";
5
+ /** Reads a `TEXT NOT NULL` column. Throws a `TypeError` if the stored value has another type. */
6
+ export const textColumn = (row, column) => {
7
+ const value = row[column];
8
+ if (!isText(value)) {
9
+ throw columnError(column, "text");
10
+ }
11
+ return value;
12
+ };
13
+ /** Reads a `TEXT NOT NULL` column that holds one of `values`. Throws a `TypeError` for any other value. */
14
+ export const enumColumn = (row, column, values) => {
15
+ const stored = textColumn(row, column);
16
+ const value = values.find((candidate) => candidate === stored);
17
+ if (value === undefined) {
18
+ throw new TypeError(`Column ${column} has an unknown value: ${stored}`);
19
+ }
20
+ return value;
21
+ };
22
+ /** Reads a nullable `TEXT` column. */
23
+ export const optionalTextColumn = (row, column) => {
24
+ const value = row[column];
25
+ if (value === null || value === undefined) {
26
+ return null;
27
+ }
28
+ if (!isText(value)) {
29
+ throw columnError(column, "text");
30
+ }
31
+ return value;
32
+ };
33
+ /** Reads an `INTEGER NOT NULL` column as a number. */
34
+ export const integerColumn = (row, column) => {
35
+ const value = row[column];
36
+ if (isBigint(value)) {
37
+ return Number(value);
38
+ }
39
+ if (!isNumber(value)) {
40
+ throw columnError(column, "an integer");
41
+ }
42
+ return value;
43
+ };
44
+ /** Reads a nullable `INTEGER` column as a number. */
45
+ export const optionalIntegerColumn = (row, column) => {
46
+ const value = row[column];
47
+ if (value === null || value === undefined) {
48
+ return null;
49
+ }
50
+ return integerColumn(row, column);
51
+ };
52
+ /** Reads a `BLOB NOT NULL` column. */
53
+ export const blobColumn = (row, column) => {
54
+ const value = row[column];
55
+ if (!(value instanceof Uint8Array)) {
56
+ throw columnError(column, "a blob");
57
+ }
58
+ return value;
59
+ };