@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,206 @@
1
+ import { createConnection } from "node:net";
2
+ import { parseArgs } from "node:util";
3
+ import { daemonSocket } from "../core/daemon-socket.js";
4
+ import { errorMessage } from "../core/error-message.js";
5
+ import { createOpsCommands, hostCliDeps } from "../ops/cli.js";
6
+ import { runAsk } from "./ask.js";
7
+ import { runChat } from "./chat.js";
8
+ import { runDaemon } from "./daemon.js";
9
+ import { runInit } from "./init.js";
10
+ import { nextLine, packageVersion, resolveHome } from "./io.js";
11
+ import { runStatus } from "./status.js";
12
+ /** The usage text of the CLI. */
13
+ export const USAGE = `Usage: wheatley <command> [options]
14
+
15
+ Commands:
16
+ init Create config.yaml and secrets.yaml in the Deployment home
17
+ daemon Run the Deployment and its Control Server in the foreground
18
+ chat Chat with an Agent in its CLI Chat
19
+ ask Send one prompt to an Agent and print the reply
20
+ status Show the health of the Deployment
21
+ service Install or control the Wheatley service: install, start, stop, restart, status
22
+ update Update Wheatley within its major version (--check, --manager npm|pnpm)
23
+ restore List the backups of an Agent, or restore agent.sqlite: restore <agent> [backup]
24
+
25
+ Options:
26
+ -h, --help Show this help
27
+ --home <dir> The Deployment home (default: $WHEATLEY_HOME or ~/.wheatley)
28
+ --url <ws-url> Connect over TCP instead of the Unix socket in the home folder
29
+ --agent <name> The Agent of chat and ask
30
+ --provider <name> init: the model provider of the first Agent
31
+ --model <id> init: the model of the first Agent
32
+ --host <host> daemon: the TCP host
33
+ --port <port> daemon: the TCP port
34
+ `;
35
+ /**
36
+ * Resolves `true` if a daemon accepts connections on the Unix socket of `home` (see `daemonSocket`). Resolves `false`
37
+ * if the connection fails for any reason, for example a missing socket file. It does not reject.
38
+ */
39
+ export const isDaemonRunning = async (home) => {
40
+ const { promise, resolve } = Promise.withResolvers();
41
+ const socket = createConnection(daemonSocket(home).path);
42
+ socket.once("connect", () => {
43
+ socket.destroy();
44
+ resolve(true);
45
+ });
46
+ socket.once("error", () => {
47
+ resolve(false);
48
+ });
49
+ return await promise;
50
+ };
51
+ const OPS_COMMANDS = ["service", "update", "restore"];
52
+ const isOpsCommand = (command) => OPS_COMMANDS.some((name) => name === command);
53
+ /** Runs `service`, `update`, or `restore` with the ops line-based output. */
54
+ const runOpsCommand = async (command, argv, io, home, overrides) => {
55
+ const commands = createOpsCommands({
56
+ ...hostCliDeps(async () => await isDaemonRunning(home)),
57
+ homeDir: home,
58
+ ...overrides,
59
+ });
60
+ return await commands[command](argv, {
61
+ err: (line) => {
62
+ io.stderr(`${line}\n`);
63
+ },
64
+ out: (line) => {
65
+ io.stdout(`${line}\n`);
66
+ },
67
+ });
68
+ };
69
+ const HOME_FLAG = "--home";
70
+ /**
71
+ * Takes `--home <dir>` and `--home=<dir>` out of ops arguments, which the ops commands do not parse themselves.
72
+ *
73
+ * Errors: throws if `--home` has no value.
74
+ */
75
+ const takeHomeOption = (argv) => {
76
+ const rest = [];
77
+ let home;
78
+ for (let index = 0; index < argv.length; index += 1) {
79
+ const arg = argv[index] ?? "";
80
+ if (arg.startsWith(`${HOME_FLAG}=`)) {
81
+ home = arg.slice(HOME_FLAG.length + 1);
82
+ }
83
+ else if (arg === HOME_FLAG) {
84
+ home = argv[index + 1];
85
+ index += 1;
86
+ if (home === undefined || home.startsWith("-")) {
87
+ throw new Error(`Option '${HOME_FLAG} <dir>' argument missing`);
88
+ }
89
+ }
90
+ else {
91
+ rest.push(arg);
92
+ }
93
+ }
94
+ return { home, rest };
95
+ };
96
+ const readPrompt = async (io, positionals) => {
97
+ if (positionals.length > 0) {
98
+ return positionals.join(" ");
99
+ }
100
+ const lines = [];
101
+ for (;;) {
102
+ // oxlint-disable-next-line no-await-in-loop -- Standard input arrives one line at a time.
103
+ const line = await nextLine(io);
104
+ if (line === undefined) {
105
+ return lines.join("\n");
106
+ }
107
+ lines.push(line);
108
+ }
109
+ };
110
+ /**
111
+ * Runs one CLI command and resolves with its exit code. `argv` excludes the program name, for example
112
+ * `["ask", "--agent", "home", "What", "now?"]`.
113
+ */
114
+ export const runCli = async (argv, io, overrides = {}) => {
115
+ let ops;
116
+ try {
117
+ ops = takeHomeOption(argv);
118
+ }
119
+ catch (error) {
120
+ io.stderr(`${errorMessage(error)}\n${USAGE}`);
121
+ return 2;
122
+ }
123
+ const [first, ...opsArgs] = ops.rest;
124
+ // The ops commands parse their own flags, so they skip the root parser.
125
+ if (isOpsCommand(first)) {
126
+ return await runOpsCommand(first, opsArgs, io, resolveHome(ops.home, io.env), overrides.ops);
127
+ }
128
+ let parsed;
129
+ try {
130
+ parsed = parseCliArgs(argv);
131
+ }
132
+ catch (error) {
133
+ io.stderr(`${errorMessage(error)}\n${USAGE}`);
134
+ return 2;
135
+ }
136
+ const { values, positionals } = parsed;
137
+ if (values.help === true) {
138
+ io.stdout(USAGE);
139
+ return 0;
140
+ }
141
+ const [command, ...rest] = positionals;
142
+ const home = resolveHome(values.home, io.env);
143
+ const version = await packageVersion();
144
+ const client = { agent: values.agent, home, url: values.url };
145
+ switch (command) {
146
+ case "init": {
147
+ return await runInit(io, {
148
+ agent: values.agent,
149
+ home,
150
+ model: values.model,
151
+ provider: values.provider,
152
+ });
153
+ }
154
+ case "daemon": {
155
+ const port = values.port === undefined ? undefined : Number(values.port);
156
+ if (port !== undefined && !Number.isInteger(port)) {
157
+ io.stderr(`Invalid port: ${values.port}\n`);
158
+ return 2;
159
+ }
160
+ return await runDaemon(io, {
161
+ home,
162
+ host: values.host,
163
+ overrides: overrides.daemon,
164
+ port,
165
+ version,
166
+ });
167
+ }
168
+ case "chat": {
169
+ return await runChat(io, client, version);
170
+ }
171
+ case "ask": {
172
+ const prompt = await readPrompt(io, rest);
173
+ if (prompt.trim() === "") {
174
+ io.stderr("ask needs a prompt as arguments or on standard input.\n");
175
+ return 2;
176
+ }
177
+ return await runAsk(io, client, prompt, version);
178
+ }
179
+ case "status": {
180
+ return await runStatus(io, client, version);
181
+ }
182
+ case undefined: {
183
+ io.stderr(USAGE);
184
+ return 0;
185
+ }
186
+ default: {
187
+ io.stderr(`Unknown command: ${command}\n${USAGE}`);
188
+ return 2;
189
+ }
190
+ }
191
+ };
192
+ const parseCliArgs = (argv) => parseArgs({
193
+ allowPositionals: true,
194
+ args: [...argv],
195
+ options: {
196
+ agent: { type: "string" },
197
+ help: { short: "h", type: "boolean" },
198
+ home: { type: "string" },
199
+ host: { type: "string" },
200
+ model: { type: "string" },
201
+ port: { type: "string" },
202
+ provider: { type: "string" },
203
+ url: { type: "string" },
204
+ },
205
+ strict: true,
206
+ });
@@ -0,0 +1,86 @@
1
+ import { daemonSocket } from "../core/daemon-socket.js";
2
+ import { errorMessage } from "../core/error-message.js";
3
+ import { DaemonClient } from "./client.js";
4
+ /** The daemon address of the flags. */
5
+ export const targetOf = (flags) => flags.url === undefined
6
+ ? { socketPath: daemonSocket(flags.home).path }
7
+ : { url: flags.url };
8
+ /**
9
+ * The Agent that a command talks to: `--agent`, else the only Agent, else the Agent named `default`. Errors: rejects if
10
+ * the choice is ambiguous.
11
+ */
12
+ export const chooseAgent = async (client, requested) => {
13
+ if (requested !== undefined) {
14
+ return requested;
15
+ }
16
+ const { agents } = await client.request("agents.list", {});
17
+ const names = agents.map((agent) => agent.name);
18
+ if (names.length === 1 && names[0] !== undefined) {
19
+ return names[0];
20
+ }
21
+ if (names.includes("default")) {
22
+ return "default";
23
+ }
24
+ throw new Error(`Choose an Agent with --agent: ${names.join(", ") || "no Agents exist"}`);
25
+ };
26
+ const DECISIONS = {
27
+ a: "allow_always",
28
+ always: "allow_always",
29
+ c: "allow_conversation",
30
+ conversation: "allow_conversation",
31
+ d: "deny",
32
+ deny: "deny",
33
+ o: "allow_once",
34
+ once: "allow_once",
35
+ };
36
+ const isDecisionWord = (word) => Object.hasOwn(DECISIONS, word);
37
+ /** The question that the CLI shows for an Approval. */
38
+ export const approvalQuestion = (approval) => `Approval needed for ${approval.toolName}\n input: ${approval.inputPreview}\nAllow? [o]nce, [c]onversation, [a]lways, [d]eny <reason>: `;
39
+ /** Parses an answer to `approvalQuestion`, or resolves `undefined` for an answer that is not a decision. */
40
+ export const parseApprovalAnswer = (answer) => {
41
+ const [word = "", ...rest] = answer.trim().split(/\s+/u);
42
+ const key = word.toLowerCase();
43
+ if (!isDecisionWord(key)) {
44
+ return undefined;
45
+ }
46
+ const decision = DECISIONS[key];
47
+ const reason = rest.join(" ");
48
+ return {
49
+ decision,
50
+ reason: decision === "deny" && reason !== "" ? reason : undefined,
51
+ };
52
+ };
53
+ /** Sends an answer to an Approval and reports the outcome. */
54
+ export const decideApproval = async (io, client, agent, approval, answer) => {
55
+ const parsed = parseApprovalAnswer(answer);
56
+ if (parsed === undefined) {
57
+ io.stdout("Answer o, c, a, or d.\n");
58
+ return false;
59
+ }
60
+ try {
61
+ const { accepted } = await client.request("approvals.decide", {
62
+ agent,
63
+ approvalId: approval.approvalId,
64
+ resolution: {
65
+ decision: parsed.decision,
66
+ reason: parsed.reason,
67
+ resolvedBy: "cli",
68
+ },
69
+ });
70
+ io.stdout(accepted ? "" : "The Approval already ended.\n");
71
+ }
72
+ catch (error) {
73
+ io.stderr(`Cannot decide the Approval: ${errorMessage(error)}\n`);
74
+ }
75
+ return true;
76
+ };
77
+ /** Connects to the daemon, or explains how to start it. */
78
+ export const connect = async (io, flags, version) => {
79
+ try {
80
+ return await DaemonClient.connect(targetOf(flags), version);
81
+ }
82
+ catch (error) {
83
+ io.stderr(`Cannot reach the Wheatley daemon (${errorMessage(error)}). Start it with "wheatley daemon".\n`);
84
+ return undefined;
85
+ }
86
+ };
@@ -0,0 +1,46 @@
1
+ import { errorMessage } from "../core/error-message.js";
2
+ import { connect } from "./session.js";
3
+ const MS_PER_SECOND = 1000;
4
+ /** Formats `system.status` as plain text lines. */
5
+ export const formatStatus = (status) => {
6
+ const lines = [
7
+ `Wheatley ${status.version} (protocol ${status.protocolVersion}), up ${Math.round(status.uptimeMs / MS_PER_SECOND)}s`,
8
+ "Agents:",
9
+ ...status.agents.map((agent) => {
10
+ const notes = [
11
+ `${agent.activeRuns} active Runs`,
12
+ agent.pendingRestart ? "restart pending" : undefined,
13
+ agent.lastError === null ? undefined : `error: ${agent.lastError}`,
14
+ agent.restartBackoffMs === null
15
+ ? undefined
16
+ : `retry in ${Math.round(agent.restartBackoffMs / MS_PER_SECOND)}s`,
17
+ ].filter((note) => note !== undefined);
18
+ return ` ${agent.name}: ${agent.state} (${notes.join(", ")})`;
19
+ }),
20
+ ];
21
+ if (status.agents.length === 0) {
22
+ lines.push(" none");
23
+ }
24
+ if (status.concurrency.length > 0) {
25
+ lines.push("Model slots:", ...status.concurrency.map((gate) => ` ${gate.credential}: ${gate.inUse}/${gate.limit} in use, ${gate.waiting} waiting`));
26
+ }
27
+ return `${lines.join("\n")}\n`;
28
+ };
29
+ /** `wheatley status`: prints Deployment health. Resolves with the exit code. */
30
+ export const runStatus = async (io, flags, version) => {
31
+ const client = await connect(io, flags, version);
32
+ if (client === undefined) {
33
+ return 1;
34
+ }
35
+ try {
36
+ io.stdout(formatStatus(await client.request("system.status", {})));
37
+ return 0;
38
+ }
39
+ catch (error) {
40
+ io.stderr(`${errorMessage(error)}\n`);
41
+ return 1;
42
+ }
43
+ finally {
44
+ await client.close();
45
+ }
46
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ import { RpcError, RpcErrorCode } from "@wheatley-ai/protocol";
2
+ /**
3
+ * The error that seam methods throw for expected failures. Seam methods throw other errors only for defects and I/O
4
+ * failures. The JSON-RPC router reports those as `InternalError`.
5
+ */
6
+ export class SeamError extends Error {
7
+ name = "SeamError";
8
+ code;
9
+ details;
10
+ constructor(code, message, details) {
11
+ super(message);
12
+ this.code = code;
13
+ this.details = details;
14
+ }
15
+ }
16
+ const RPC_CODES = {
17
+ agent_not_found: RpcErrorCode.AgentNotFound,
18
+ agent_unavailable: RpcErrorCode.AgentUnavailable,
19
+ config_invalid: RpcErrorCode.ConfigInvalid,
20
+ conflict: RpcErrorCode.Conflict,
21
+ forbidden: RpcErrorCode.InvalidParams,
22
+ invalid_name: RpcErrorCode.InvalidParams,
23
+ memory_budget_exceeded: RpcErrorCode.MemoryBudgetExceeded,
24
+ not_found: RpcErrorCode.NotFound,
25
+ revision_conflict: RpcErrorCode.RevisionConflict,
26
+ };
27
+ /** Converts a `SeamError` into the `RpcError` that a JSON-RPC handler throws. */
28
+ export const toRpcError = (error) => new RpcError(RPC_CODES[error.code], error.message, error.details);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ /** The system clock. */
2
+ export const systemClock = { now: () => Date.now() };
3
+ /** Returns the migration namespace of an Agent module for `runMigrations`. */
4
+ export const migrationNamespace = (module) => ({
5
+ migrations: module.migrations,
6
+ namespace: module.name,
7
+ });
@@ -0,0 +1,43 @@
1
+ const TOOL_PART = /^[a-z][a-z0-9_]*$/u;
2
+ /** The MCP Tool name character set, which allows names such as `resolve-library-id`, `getIssue`, and `browser.navigate`. */
3
+ const MCP_TOOL_PART = /^[A-Za-z0-9_.-]{1,128}$/u;
4
+ const OWNER_PART = /^[a-z0-9][a-z0-9_-]*$/u;
5
+ const REGEX_SPECIAL = /[.*+?^${}()|[\]\\]/gu;
6
+ const ESCAPED_WILDCARD = /\\\*/gu;
7
+ /** The Plugin name that Wheatley reserves for MCP Tool names. */
8
+ export const RESERVED_PLUGIN_NAME = "mcp";
9
+ /**
10
+ * Builds the canonical Tool name (ADR 0006): `<tool>` for built-ins, `<plugin>:<tool>` for Plugins, and
11
+ * `mcp:<server>:<tool>` for MCP Servers. The Permission Policy matches these names.
12
+ *
13
+ * Built-in and Plugin Tool names match `^[a-z][a-z0-9_]*$`. MCP Tool names keep the name that the server reports and
14
+ * match `^[A-Za-z0-9_.-]{1,128}$`. Throws a `RangeError` if a part breaks its pattern, or for a Plugin named `mcp`.
15
+ */
16
+ export const canonicalToolName = (origin, tool) => {
17
+ const toolPattern = origin.kind === "mcp" ? MCP_TOOL_PART : TOOL_PART;
18
+ if (!toolPattern.test(tool)) {
19
+ throw new RangeError(`Invalid Tool name: ${tool}`);
20
+ }
21
+ if (origin.kind === "builtin") {
22
+ return tool;
23
+ }
24
+ const owner = origin.kind === "plugin" ? origin.plugin : origin.server;
25
+ if (!OWNER_PART.test(owner) ||
26
+ (origin.kind === "plugin" && owner === RESERVED_PLUGIN_NAME)) {
27
+ throw new RangeError(`Invalid ${origin.kind} name: ${owner}`);
28
+ }
29
+ return origin.kind === "plugin"
30
+ ? `${owner}:${tool}`
31
+ : `${RESERVED_PLUGIN_NAME}:${owner}:${tool}`;
32
+ };
33
+ /**
34
+ * Returns true if a canonical Tool name matches a Permission Policy pattern. `*` matches any run of characters,
35
+ * including `:`. Every other character matches itself, case-sensitively. For example, `notes:*` matches `notes:add`,
36
+ * and `mcp:github:*` matches every Tool of the `github` MCP Server.
37
+ */
38
+ export const matchesToolPattern = (pattern, toolName) => {
39
+ const source = pattern
40
+ .replaceAll(REGEX_SPECIAL, String.raw `\$&`)
41
+ .replaceAll(ESCAPED_WILDCARD, ".*");
42
+ return new RegExp(`^${source}$`, "u").test(toolName);
43
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ /** The request body cap of the Webhook Ingress: 256 KiB (ADR 0010). */
2
+ export const MAX_WEBHOOK_BODY_BYTES = 262_144;