@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.
- package/LICENSE +15 -0
- package/bin/wheatley.js +2 -0
- package/dist/approvals/gate.js +278 -0
- package/dist/approvals/module.js +21 -0
- package/dist/approvals/policy.js +30 -0
- package/dist/approvals/preview.js +17 -0
- package/dist/approvals/prompt.js +98 -0
- package/dist/approvals/rpc.js +16 -0
- package/dist/approvals/store.js +159 -0
- package/dist/cli/ask.js +94 -0
- package/dist/cli/chat.js +178 -0
- package/dist/cli/client.js +104 -0
- package/dist/cli/daemon.js +35 -0
- package/dist/cli/init.js +72 -0
- package/dist/cli/io.js +45 -0
- package/dist/cli/main.js +206 -0
- package/dist/cli/session.js +86 -0
- package/dist/cli/status.js +46 -0
- package/dist/contracts/approval-gate.js +1 -0
- package/dist/contracts/config-store.js +1 -0
- package/dist/contracts/delivery-outbox.js +1 -0
- package/dist/contracts/errors.js +28 -0
- package/dist/contracts/extension-catalog.js +1 -0
- package/dist/contracts/maintenance-runner.js +1 -0
- package/dist/contracts/memory-store.js +1 -0
- package/dist/contracts/module.js +7 -0
- package/dist/contracts/tool-names.js +43 -0
- package/dist/contracts/trigger-runtime.js +1 -0
- package/dist/contracts/webhook-ingress.js +2 -0
- package/dist/core/agent/agent-store.js +300 -0
- package/dist/core/agent/agent.js +1033 -0
- package/dist/core/agent/gated-models.js +89 -0
- package/dist/core/agent/pi-adapter.js +401 -0
- package/dist/core/agent/policy-gate.js +44 -0
- package/dist/core/builtin-catalog.js +20 -0
- package/dist/core/concurrency-gate.js +87 -0
- package/dist/core/config/atomic-file.js +72 -0
- package/dist/core/config/config-store.js +138 -0
- package/dist/core/config/pending-restart.js +53 -0
- package/dist/core/config/secret-references.js +69 -0
- package/dist/core/config/secret-store.js +147 -0
- package/dist/core/config/validate.js +55 -0
- package/dist/core/config/yaml-patch.js +57 -0
- package/dist/core/daemon-socket.js +96 -0
- package/dist/core/daemon.js +97 -0
- package/dist/core/deployment.js +379 -0
- package/dist/core/error-message.js +4 -0
- package/dist/core/events.js +25 -0
- package/dist/core/logger.js +81 -0
- package/dist/core/models.js +27 -0
- package/dist/core/module.js +42 -0
- package/dist/core/rpc/agent-handlers.js +82 -0
- package/dist/core/rpc/deployment-handlers.js +63 -0
- package/dist/core/rpc/router.js +96 -0
- package/dist/core/server/connection.js +170 -0
- package/dist/core/server/control-server.js +152 -0
- package/dist/core/server/static-files.js +79 -0
- package/dist/core/supervisor.js +215 -0
- package/dist/core/tool-routing.js +41 -0
- package/dist/dashboard/assets/index-C9Rul5vE.css +2 -0
- package/dist/dashboard/assets/index-DXzGGAxQ.js +11 -0
- package/dist/dashboard/assets/sheet-presentation-DMljN0LW.js +1 -0
- package/dist/dashboard/index.html +18 -0
- package/dist/db/database.js +16 -0
- package/dist/db/migrations.js +184 -0
- package/dist/frontends/discord/approvals.js +199 -0
- package/dist/frontends/discord/attachments.js +115 -0
- package/dist/frontends/discord/binding.js +489 -0
- package/dist/frontends/discord/config.js +107 -0
- package/dist/frontends/discord/discord-runtime.js +52 -0
- package/dist/frontends/discord/errors.js +61 -0
- package/dist/frontends/discord/live-runs.js +189 -0
- package/dist/frontends/discord/module.js +39 -0
- package/dist/frontends/discord/recent-ids.js +20 -0
- package/dist/frontends/slack/approval-blocks.js +98 -0
- package/dist/frontends/slack/attachments.js +102 -0
- package/dist/frontends/slack/background-tasks.js +20 -0
- package/dist/frontends/slack/binding.js +405 -0
- package/dist/frontends/slack/bounded.js +9 -0
- package/dist/frontends/slack/chat-id.js +17 -0
- package/dist/frontends/slack/config.js +80 -0
- package/dist/frontends/slack/delivery-errors.js +81 -0
- package/dist/frontends/slack/inbound.js +76 -0
- package/dist/frontends/slack/live-progress.js +190 -0
- package/dist/frontends/slack/manifest.js +66 -0
- package/dist/frontends/slack/outbound.js +160 -0
- package/dist/frontends/slack/schemas.js +99 -0
- package/dist/frontends/slack/signature.js +30 -0
- package/dist/frontends/slack/slack-client.js +60 -0
- package/dist/frontends/slack/slack-frontend.js +38 -0
- package/dist/frontends/slack/state-store.js +112 -0
- package/dist/frontends/telegram/approvals.js +134 -0
- package/dist/frontends/telegram/attachments.js +126 -0
- package/dist/frontends/telegram/binding.js +144 -0
- package/dist/frontends/telegram/chat-key.js +43 -0
- package/dist/frontends/telegram/client.js +88 -0
- package/dist/frontends/telegram/config.js +93 -0
- package/dist/frontends/telegram/delivery.js +215 -0
- package/dist/frontends/telegram/frontend.js +42 -0
- package/dist/frontends/telegram/grammy-client.js +122 -0
- package/dist/frontends/telegram/http-status.js +6 -0
- package/dist/frontends/telegram/inbound.js +149 -0
- package/dist/frontends/telegram/live.js +182 -0
- package/dist/frontends/telegram/markdown.js +103 -0
- package/dist/frontends/telegram/recent-set.js +18 -0
- package/dist/frontends/telegram/serial-queue.js +23 -0
- package/dist/index.js +7 -0
- package/dist/mcp/content.js +61 -0
- package/dist/mcp/mcp-module.js +35 -0
- package/dist/mcp/mcp-tool-provider.js +93 -0
- package/dist/mcp/server-connection.js +297 -0
- package/dist/memory/consolidation.js +260 -0
- package/dist/memory/format.js +5 -0
- package/dist/memory/migrations.js +76 -0
- package/dist/memory/module.js +69 -0
- package/dist/memory/names.js +20 -0
- package/dist/memory/rows.js +22 -0
- package/dist/memory/rpc.js +49 -0
- package/dist/memory/snapshots.js +12 -0
- package/dist/memory/store.js +281 -0
- package/dist/memory/tools.js +158 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/config.d.ts +262 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/config.js +120 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/domain.d.ts +512 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/domain.js +441 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/duration.d.ts +8 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/duration.js +28 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/events.d.ts +234 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/events.js +124 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/index.d.ts +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/index.js +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json-rpc.d.ts +81 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json-rpc.js +157 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json.d.ts +14 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json.js +23 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/limits.d.ts +17 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/limits.js +17 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/methods.d.ts +1107 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/methods.js +201 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/rpc-error.d.ts +45 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/rpc-error.js +44 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/validation.d.ts +20 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/validation.js +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/package.json +14 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/chunk.d.ts +11 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/chunk.js +162 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/commands.d.ts +48 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/commands.js +39 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/delivery-error.d.ts +29 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/delivery-error.js +27 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.d.ts +236 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/index.d.ts +10 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/index.js +7 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/json.d.ts +9 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/json.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/logger.d.ts +18 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/logger.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/plugin.d.ts +48 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/plugin.js +5 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/schema.d.ts +69 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/schema.js +3 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/tool.d.ts +69 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/tool.js +3 -0
- package/dist/node_modules/@wheatley-ai/sdk/package.json +11 -0
- package/dist/ops/backup.js +185 -0
- package/dist/ops/cli.js +194 -0
- package/dist/ops/command-runner.js +34 -0
- package/dist/ops/dashboard-assets.js +37 -0
- package/dist/ops/migrate.js +63 -0
- package/dist/ops/module.js +47 -0
- package/dist/ops/paths.js +55 -0
- package/dist/ops/service.js +376 -0
- package/dist/ops/update.js +145 -0
- package/dist/plugins/contributions.js +89 -0
- package/dist/plugins/guards.js +111 -0
- package/dist/plugins/load.js +76 -0
- package/dist/plugins/module.js +31 -0
- package/dist/plugins/provider.js +349 -0
- package/dist/plugins/schema.js +69 -0
- package/dist/skills/disclosure.js +142 -0
- package/dist/skills/discovery.js +120 -0
- package/dist/skills/fs-errors.js +8 -0
- package/dist/skills/read-skill-tool.js +3 -0
- package/dist/skills/skill-file.js +90 -0
- package/dist/skills/skills.js +140 -0
- package/dist/tools/builtin-tools.js +61 -0
- package/dist/tools/input.js +45 -0
- package/dist/tools/paths.js +6 -0
- package/dist/tools/read-file.js +65 -0
- package/dist/tools/shell.js +156 -0
- package/dist/tools/write-file.js +58 -0
- package/dist/triggers/delivery-codec.js +99 -0
- package/dist/triggers/delivery-outbox.js +313 -0
- package/dist/triggers/due-timer.js +14 -0
- package/dist/triggers/errors.js +2 -0
- package/dist/triggers/migrations.js +91 -0
- package/dist/triggers/module.js +106 -0
- package/dist/triggers/rpc.js +35 -0
- package/dist/triggers/schedule.js +52 -0
- package/dist/triggers/sql-row.js +59 -0
- package/dist/triggers/trigger-runtime.js +445 -0
- package/dist/triggers/trigger-tools.js +210 -0
- package/dist/triggers/webhook-ingress.js +212 -0
- package/dist/wiring/agent-services.js +151 -0
- package/dist/wiring/catalog.js +81 -0
- package/dist/wiring/daemon.js +83 -0
- package/dist/wiring/frontend-host.js +268 -0
- package/package.json +50 -3
- package/README.md +0 -3
package/dist/cli/ask.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { errorMessage } from "../core/error-message.js";
|
|
2
|
+
import { nextLine } from "./io.js";
|
|
3
|
+
import { approvalQuestion, chooseAgent, connect, decideApproval, } from "./session.js";
|
|
4
|
+
/**
|
|
5
|
+
* `wheatley ask <prompt>`: sends one prompt to the Agent's CLI Chat, prints the reply, and exits when the Run ends
|
|
6
|
+
* (ADR 0005). Without a terminal, the Run is non-interactive and the gate denies every `ask` Tool (ADR 0011).
|
|
7
|
+
*
|
|
8
|
+
* Resolves with the exit code: 0 for a completed Run, 1 otherwise.
|
|
9
|
+
*/
|
|
10
|
+
export const runAsk = async (io, flags, prompt, version) => {
|
|
11
|
+
const client = await connect(io, flags, version);
|
|
12
|
+
if (client === undefined) {
|
|
13
|
+
return 1;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const agent = await chooseAgent(client, flags.agent);
|
|
17
|
+
const queue = [];
|
|
18
|
+
let wake = Promise.withResolvers();
|
|
19
|
+
const state = { runId: undefined };
|
|
20
|
+
const push = (event) => {
|
|
21
|
+
queue.push(event);
|
|
22
|
+
wake.resolve(undefined);
|
|
23
|
+
};
|
|
24
|
+
const backlog = [];
|
|
25
|
+
const route = (event) => {
|
|
26
|
+
if (event.name === "chat.message" &&
|
|
27
|
+
event.payload.message.runId === state.runId &&
|
|
28
|
+
event.payload.message.role === "assistant") {
|
|
29
|
+
push({ kind: "reply", text: event.payload.message.text });
|
|
30
|
+
}
|
|
31
|
+
else if (event.name === "approval.requested" &&
|
|
32
|
+
event.payload.approval.runId === state.runId) {
|
|
33
|
+
push({ approval: event.payload.approval, kind: "approval" });
|
|
34
|
+
}
|
|
35
|
+
else if (event.name === "run.finished" &&
|
|
36
|
+
event.payload.run.runId === state.runId) {
|
|
37
|
+
push({ kind: "finished", run: event.payload.run });
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
client.onEvent((event) => {
|
|
41
|
+
if (state.runId === undefined) {
|
|
42
|
+
backlog.push(event);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
route(event);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
await client.request("events.subscribe", {
|
|
49
|
+
agents: [agent],
|
|
50
|
+
topics: ["chat", "run", "approval"],
|
|
51
|
+
});
|
|
52
|
+
const sent = await client.request("chat.ask", {
|
|
53
|
+
agent,
|
|
54
|
+
interactive: io.isTTY,
|
|
55
|
+
text: prompt,
|
|
56
|
+
});
|
|
57
|
+
state.runId = sent.runId;
|
|
58
|
+
for (const event of backlog.splice(0)) {
|
|
59
|
+
route(event);
|
|
60
|
+
}
|
|
61
|
+
for (;;) {
|
|
62
|
+
const event = queue.shift();
|
|
63
|
+
if (event === undefined) {
|
|
64
|
+
// oxlint-disable-next-line no-await-in-loop -- The loop waits for the next event of the Run.
|
|
65
|
+
await wake.promise;
|
|
66
|
+
wake = Promise.withResolvers();
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (event.kind === "reply") {
|
|
70
|
+
io.stdout(`${event.text}\n`);
|
|
71
|
+
}
|
|
72
|
+
else if (event.kind === "approval") {
|
|
73
|
+
io.stdout(approvalQuestion(event.approval));
|
|
74
|
+
// oxlint-disable-next-line no-await-in-loop -- The answer must arrive before the Run continues.
|
|
75
|
+
const answer = await nextLine(io);
|
|
76
|
+
// oxlint-disable-next-line no-await-in-loop -- The decision goes out before the next event is read.
|
|
77
|
+
await decideApproval(io, client, agent, event.approval, answer ?? "deny");
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
if (event.run.status !== "completed") {
|
|
81
|
+
io.stderr(`The Run ended ${event.run.status}${event.run.error === undefined ? "" : `: ${event.run.error}`}\n`);
|
|
82
|
+
}
|
|
83
|
+
return event.run.status === "completed" ? 0 : 1;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
io.stderr(`${errorMessage(error)}\n`);
|
|
89
|
+
return 1;
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
await client.close();
|
|
93
|
+
}
|
|
94
|
+
};
|
package/dist/cli/chat.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { BUILTIN_COMMANDS, parseCommandText } from "@wheatley-ai/sdk";
|
|
2
|
+
import { CLI_CHAT_ID } from "../core/agent/agent.js";
|
|
3
|
+
import { CLI_BINDING_ID } from "../core/config/validate.js";
|
|
4
|
+
import { errorMessage } from "../core/error-message.js";
|
|
5
|
+
import { nextLine } from "./io.js";
|
|
6
|
+
import { approvalQuestion, chooseAgent, connect, decideApproval, } from "./session.js";
|
|
7
|
+
/** Lines that end the chat. */
|
|
8
|
+
const EXIT_LINES = new Set(["/exit", "/quit"]);
|
|
9
|
+
const isCliChatRun = (event) => (event.name === "run.started" || event.name === "run.finished") &&
|
|
10
|
+
event.payload.run.origin.kind === "chat" &&
|
|
11
|
+
event.payload.run.origin.bindingId === CLI_BINDING_ID &&
|
|
12
|
+
event.payload.run.origin.chatId === CLI_CHAT_ID;
|
|
13
|
+
/**
|
|
14
|
+
* Tracks the Runs of the CLI Chat and its open Approvals while the chat runs. A sent message counts as busy until the
|
|
15
|
+
* Agent's echo of it names its Run. The echo and the Run events can arrive before or after the `chat.send` response
|
|
16
|
+
* is handled, so every step works in any order.
|
|
17
|
+
*/
|
|
18
|
+
class ChatState {
|
|
19
|
+
approvals = [];
|
|
20
|
+
#waiting = new Set();
|
|
21
|
+
#echoed = new Set();
|
|
22
|
+
#active = new Set();
|
|
23
|
+
#finished = new Set();
|
|
24
|
+
#idle = Promise.withResolvers();
|
|
25
|
+
constructor() {
|
|
26
|
+
this.#idle.resolve(undefined);
|
|
27
|
+
}
|
|
28
|
+
removeApproval = (approvalId) => {
|
|
29
|
+
const index = this.approvals.findIndex((approval) => approval.approvalId === approvalId);
|
|
30
|
+
if (index !== -1) {
|
|
31
|
+
this.approvals.splice(index, 1);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
/** This chat sent a message. */
|
|
35
|
+
sent = (messageId) => {
|
|
36
|
+
if (this.#echoed.delete(messageId)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
this.#busy();
|
|
40
|
+
this.#waiting.add(messageId);
|
|
41
|
+
};
|
|
42
|
+
/** The Agent stored a user message of this Chat for a Run. */
|
|
43
|
+
echoed = (messageId, runId) => {
|
|
44
|
+
if (!this.#waiting.delete(messageId)) {
|
|
45
|
+
this.#echoed.add(messageId);
|
|
46
|
+
}
|
|
47
|
+
if (runId !== undefined) {
|
|
48
|
+
this.started(runId);
|
|
49
|
+
}
|
|
50
|
+
this.#settle();
|
|
51
|
+
};
|
|
52
|
+
started = (runId) => {
|
|
53
|
+
if (this.#finished.has(runId)) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
this.#busy();
|
|
57
|
+
this.#active.add(runId);
|
|
58
|
+
};
|
|
59
|
+
finished = (runId) => {
|
|
60
|
+
this.#finished.add(runId);
|
|
61
|
+
this.#active.delete(runId);
|
|
62
|
+
this.#settle();
|
|
63
|
+
};
|
|
64
|
+
/** Resolves when no message waits for its Run and no Run of the CLI Chat is active. */
|
|
65
|
+
get idle() {
|
|
66
|
+
return this.#idle.promise;
|
|
67
|
+
}
|
|
68
|
+
#busy = () => {
|
|
69
|
+
if (this.#active.size === 0 && this.#waiting.size === 0) {
|
|
70
|
+
this.#idle = Promise.withResolvers();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
#settle = () => {
|
|
74
|
+
if (this.#active.size === 0 && this.#waiting.size === 0) {
|
|
75
|
+
this.#idle.resolve(undefined);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const render = (io, agent, state, event) => {
|
|
80
|
+
if (event.name === "chat.message" && event.payload.message.role === "user") {
|
|
81
|
+
state.echoed(event.payload.message.messageId, event.payload.message.runId);
|
|
82
|
+
}
|
|
83
|
+
else if (event.name === "chat.message") {
|
|
84
|
+
io.stdout(`${agent}> ${event.payload.message.text}\n`);
|
|
85
|
+
}
|
|
86
|
+
else if (event.name === "run.toolStarted") {
|
|
87
|
+
io.stdout(` [${event.payload.toolName}] ${event.payload.inputPreview}\n`);
|
|
88
|
+
}
|
|
89
|
+
else if (event.name === "run.toolFinished" && event.payload.isError) {
|
|
90
|
+
io.stdout(` [${event.payload.toolName} failed] ${event.payload.outputPreview}\n`);
|
|
91
|
+
}
|
|
92
|
+
else if (event.name === "approval.requested") {
|
|
93
|
+
state.approvals.push(event.payload.approval);
|
|
94
|
+
io.stdout(approvalQuestion(event.payload.approval));
|
|
95
|
+
}
|
|
96
|
+
else if (event.name === "approval.resolved") {
|
|
97
|
+
state.removeApproval(event.payload.approvalId);
|
|
98
|
+
}
|
|
99
|
+
else if (isCliChatRun(event) && event.name === "run.started") {
|
|
100
|
+
state.started(event.payload.run.runId);
|
|
101
|
+
}
|
|
102
|
+
else if (isCliChatRun(event) && event.name === "run.finished") {
|
|
103
|
+
if (event.payload.run.status === "failed") {
|
|
104
|
+
io.stderr(`The Run failed: ${event.payload.run.error ?? "unknown error"}\n`);
|
|
105
|
+
}
|
|
106
|
+
state.finished(event.payload.run.runId);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
const handleLine = async (io, client, agent, state, line) => {
|
|
110
|
+
const [approval] = state.approvals;
|
|
111
|
+
if (approval !== undefined) {
|
|
112
|
+
// Events that arrive during the request can already remove this Approval and queue the next one.
|
|
113
|
+
if (await decideApproval(io, client, agent, approval, line)) {
|
|
114
|
+
state.removeApproval(approval.approvalId);
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const command = parseCommandText(line);
|
|
119
|
+
if (command === undefined) {
|
|
120
|
+
const { messageId } = await client.request("chat.send", {
|
|
121
|
+
agent,
|
|
122
|
+
text: line,
|
|
123
|
+
});
|
|
124
|
+
state.sent(messageId);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
// `Agent.command` sends the reply for a command that cannot run, so the CLI ignores `accepted`.
|
|
128
|
+
await client.request("chat.command", {
|
|
129
|
+
agent,
|
|
130
|
+
args: command.args,
|
|
131
|
+
name: command.name,
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* `wheatley chat`: an interactive session in the Agent's CLI Chat (`cli` binding, Chat `default`), which the dashboard
|
|
136
|
+
* shares (ADR 0007). Lines that start with a built-in command run it; `/exit` or the end of input ends the session
|
|
137
|
+
* after the active Runs end. While an Approval waits, the next line answers it.
|
|
138
|
+
*
|
|
139
|
+
* Resolves with the exit code.
|
|
140
|
+
*/
|
|
141
|
+
export const runChat = async (io, flags, version) => {
|
|
142
|
+
const client = await connect(io, flags, version);
|
|
143
|
+
if (client === undefined) {
|
|
144
|
+
return 1;
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
const agent = await chooseAgent(client, flags.agent);
|
|
148
|
+
const state = new ChatState();
|
|
149
|
+
client.onEvent((event) => {
|
|
150
|
+
render(io, agent, state, event);
|
|
151
|
+
});
|
|
152
|
+
await client.request("events.subscribe", {
|
|
153
|
+
agents: [agent],
|
|
154
|
+
topics: ["chat", "run", "approval"],
|
|
155
|
+
});
|
|
156
|
+
io.stdout(`Chat with ${agent}. Commands: ${BUILTIN_COMMANDS.map((command) => `/${command.name}`).join(" ")} /exit\n`);
|
|
157
|
+
for (;;) {
|
|
158
|
+
// oxlint-disable-next-line no-await-in-loop -- The chat reads one line at a time.
|
|
159
|
+
const line = await nextLine(io);
|
|
160
|
+
if (line === undefined || EXIT_LINES.has(line.trim())) {
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
if (line.trim() !== "") {
|
|
164
|
+
// oxlint-disable-next-line no-await-in-loop -- Each line is handled before the next one is read.
|
|
165
|
+
await handleLine(io, client, agent, state, line.trim());
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
await state.idle;
|
|
169
|
+
return 0;
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
io.stderr(`${errorMessage(error)}\n`);
|
|
173
|
+
return 1;
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
await client.close();
|
|
177
|
+
}
|
|
178
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { once } from "node:events";
|
|
2
|
+
import { decodeServerMessage, encodeRequest, PROTOCOL_VERSION, RpcError, } from "@wheatley-ai/protocol";
|
|
3
|
+
import { WebSocket } from "ws";
|
|
4
|
+
/** The client name in `system.hello`. */
|
|
5
|
+
export const CLIENT_NAME = "wheatley-cli";
|
|
6
|
+
const rawText = (data) => {
|
|
7
|
+
if (Array.isArray(data)) {
|
|
8
|
+
return Buffer.concat(data).toString("utf-8");
|
|
9
|
+
}
|
|
10
|
+
return Buffer.from(data instanceof ArrayBuffer ? new Uint8Array(data) : data).toString("utf-8");
|
|
11
|
+
};
|
|
12
|
+
const isResultOf = (response, method) => response.method === method;
|
|
13
|
+
/**
|
|
14
|
+
* A JSON-RPC client of the Control Server (ADR 0005). One connection carries requests, responses, and events in order.
|
|
15
|
+
* `connect` sends `system.hello` before it resolves.
|
|
16
|
+
*/
|
|
17
|
+
export class DaemonClient {
|
|
18
|
+
#socket;
|
|
19
|
+
#pending = new Map();
|
|
20
|
+
#listeners = new Set();
|
|
21
|
+
#nextId = 1;
|
|
22
|
+
constructor(socket) {
|
|
23
|
+
this.#socket = socket;
|
|
24
|
+
socket.on("message", (data) => {
|
|
25
|
+
this.#onMessage(rawText(data));
|
|
26
|
+
});
|
|
27
|
+
socket.on("close", () => {
|
|
28
|
+
for (const pending of this.#pending.values()) {
|
|
29
|
+
pending.settle(new RpcError(-32_603, "The connection to the daemon closed"));
|
|
30
|
+
}
|
|
31
|
+
this.#pending.clear();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Opens a connection and greets the daemon. Errors: rejects if the daemon does not answer, and with `RpcError`
|
|
36
|
+
* `ProtocolMismatch` if it speaks another protocol version.
|
|
37
|
+
*/
|
|
38
|
+
static async connect(target, version) {
|
|
39
|
+
const socket = "url" in target
|
|
40
|
+
? new WebSocket(target.url)
|
|
41
|
+
: new WebSocket(`ws+unix://${target.socketPath}:/ws`);
|
|
42
|
+
// `once` rejects if the socket emits `error` first, for example when no daemon listens.
|
|
43
|
+
await once(socket, "open");
|
|
44
|
+
const client = new DaemonClient(socket);
|
|
45
|
+
await client.request("system.hello", {
|
|
46
|
+
client: { name: CLIENT_NAME, version },
|
|
47
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
48
|
+
});
|
|
49
|
+
return client;
|
|
50
|
+
}
|
|
51
|
+
/** Sends one request and resolves with its result. Errors: rejects with the daemon's `RpcError`. */
|
|
52
|
+
request = async (method, params) => {
|
|
53
|
+
const id = this.#nextId;
|
|
54
|
+
this.#nextId += 1;
|
|
55
|
+
const response = Promise.withResolvers();
|
|
56
|
+
this.#pending.set(id, { method, settle: response.resolve });
|
|
57
|
+
this.#socket.send(encodeRequest(id, method, params));
|
|
58
|
+
const settled = await response.promise;
|
|
59
|
+
if (settled instanceof RpcError) {
|
|
60
|
+
throw settled;
|
|
61
|
+
}
|
|
62
|
+
if (!isResultOf(settled, method)) {
|
|
63
|
+
throw new RpcError(-32_603, `The daemon answered ${settled.method} for ${method}`);
|
|
64
|
+
}
|
|
65
|
+
return settled.result;
|
|
66
|
+
};
|
|
67
|
+
/** Adds an event listener and returns the function that removes it. */
|
|
68
|
+
onEvent = (listener) => {
|
|
69
|
+
this.#listeners.add(listener);
|
|
70
|
+
return () => {
|
|
71
|
+
this.#listeners.delete(listener);
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
/** Closes the connection. */
|
|
75
|
+
close = async () => {
|
|
76
|
+
if (this.#socket.readyState === WebSocket.CLOSED) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const closed = once(this.#socket, "close");
|
|
80
|
+
this.#socket.close();
|
|
81
|
+
await closed;
|
|
82
|
+
};
|
|
83
|
+
#settle = (id, response) => {
|
|
84
|
+
const pending = this.#pending.get(id);
|
|
85
|
+
this.#pending.delete(id);
|
|
86
|
+
pending?.settle(response);
|
|
87
|
+
};
|
|
88
|
+
#onMessage = (text) => {
|
|
89
|
+
const message = decodeServerMessage(text, (id) => this.#pending.get(id)?.method);
|
|
90
|
+
if (message.kind === "event") {
|
|
91
|
+
for (const listener of this.#listeners) {
|
|
92
|
+
listener(message.event);
|
|
93
|
+
}
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (message.kind === "result") {
|
|
97
|
+
this.#settle(message.response.id, message.response);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (message.kind === "error" && message.id !== null) {
|
|
101
|
+
this.#settle(message.id, new RpcError(message.error.code, message.error.message, message.error.data));
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { once } from "node:events";
|
|
2
|
+
import { errorMessage } from "../core/error-message.js";
|
|
3
|
+
import { startWheatley } from "../wiring/daemon.js";
|
|
4
|
+
/**
|
|
5
|
+
* `wheatley daemon`: runs the Deployment with every module, the Webhook Ingress, and the Control Server in the
|
|
6
|
+
* foreground until `SIGINT` or `SIGTERM`, then drains and stops. Log records go to stderr as JSON lines. Resolves with
|
|
7
|
+
* the exit code.
|
|
8
|
+
*/
|
|
9
|
+
export const runDaemon = async (io, options) => {
|
|
10
|
+
let daemon;
|
|
11
|
+
try {
|
|
12
|
+
daemon = await startWheatley({
|
|
13
|
+
env: io.env,
|
|
14
|
+
homeDir: options.home,
|
|
15
|
+
host: options.host,
|
|
16
|
+
port: options.port,
|
|
17
|
+
version: options.version,
|
|
18
|
+
writeLog: (record) => {
|
|
19
|
+
io.stderr(`${JSON.stringify(record)}\n`);
|
|
20
|
+
},
|
|
21
|
+
...options.overrides,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
io.stderr(`Cannot start the daemon: ${errorMessage(error)}\n`);
|
|
26
|
+
return 1;
|
|
27
|
+
}
|
|
28
|
+
io.stdout(`Wheatley ${options.version} listens on ${daemon.server.url}\n`);
|
|
29
|
+
if (!io.shutdown.aborted) {
|
|
30
|
+
await once(io.shutdown, "abort");
|
|
31
|
+
}
|
|
32
|
+
io.stdout("Stopping...\n");
|
|
33
|
+
await daemon.close();
|
|
34
|
+
return 0;
|
|
35
|
+
};
|
package/dist/cli/init.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { mkdir } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { AgentNameSchema, decode } from "@wheatley-ai/protocol";
|
|
4
|
+
import { readTextIfExists, writeFileAtomic, } from "../core/config/atomic-file.js";
|
|
5
|
+
import { CONFIG_FILE_MODE, CONFIG_FILE_NAME, SECRETS_FILE_NAME, } from "../core/config/config-store.js";
|
|
6
|
+
import { SECRETS_FILE_MODE } from "../core/config/secret-store.js";
|
|
7
|
+
import { parseConfigText } from "../core/config/validate.js";
|
|
8
|
+
import { AGENTS_DIR } from "../core/deployment.js";
|
|
9
|
+
/** The model of the first Agent when `init` gets no `--provider` and `--model`. */
|
|
10
|
+
export const DEFAULT_PROVIDER = "anthropic";
|
|
11
|
+
export const DEFAULT_MODEL = "claude-sonnet-5-5";
|
|
12
|
+
/** The name of the first Agent when `init` gets no `--agent`. */
|
|
13
|
+
export const DEFAULT_AGENT = "default";
|
|
14
|
+
/** The environment variable name that holds the API key of a provider, for example `ANTHROPIC_API_KEY`. */
|
|
15
|
+
const apiKeySecret = (provider) => `${provider.toUpperCase().replaceAll(/[^A-Z0-9]/gu, "_")}_API_KEY`;
|
|
16
|
+
const configTemplate = (agent, provider, model) => `# Wheatley Deployment settings (ADR 0004).
|
|
17
|
+
# Save changes, then restart the changed Agent: wheatley restarts no Agent on its own.
|
|
18
|
+
version: 1
|
|
19
|
+
|
|
20
|
+
providers:
|
|
21
|
+
${provider}:
|
|
22
|
+
# The API key is a secret: set it in secrets.yaml or as an environment variable.
|
|
23
|
+
apiKey:
|
|
24
|
+
secret: ${apiKeySecret(provider)}
|
|
25
|
+
|
|
26
|
+
agents:
|
|
27
|
+
${agent}:
|
|
28
|
+
model:
|
|
29
|
+
provider: ${provider}
|
|
30
|
+
id: ${model}
|
|
31
|
+
# Rules match canonical Tool names. The first match wins. Without a match, read_file is allowed, and
|
|
32
|
+
# write_file and shell ask for an Approval.
|
|
33
|
+
permissionPolicy:
|
|
34
|
+
- tool: read_file
|
|
35
|
+
action: allow
|
|
36
|
+
`;
|
|
37
|
+
const secretsTemplate = (provider) => `# Wheatley secrets. Keep this file private (mode 0600).
|
|
38
|
+
# An environment variable with the same name wins over a value here.
|
|
39
|
+
# ${apiKeySecret(provider)}: <your API key>
|
|
40
|
+
`;
|
|
41
|
+
/**
|
|
42
|
+
* `wheatley init`: creates the Deployment home with `config.yaml`, an owner-only `secrets.yaml`, and the first Agent's
|
|
43
|
+
* folder. It never overwrites an existing `config.yaml`. Resolves with the exit code.
|
|
44
|
+
*/
|
|
45
|
+
export const runInit = async (io, options) => {
|
|
46
|
+
const agent = options.agent ?? DEFAULT_AGENT;
|
|
47
|
+
const provider = options.provider ?? DEFAULT_PROVIDER;
|
|
48
|
+
const model = options.model ?? DEFAULT_MODEL;
|
|
49
|
+
const configFile = path.join(options.home, CONFIG_FILE_NAME);
|
|
50
|
+
const secretsFile = path.join(options.home, SECRETS_FILE_NAME);
|
|
51
|
+
if (!decode(AgentNameSchema, agent).ok) {
|
|
52
|
+
io.stderr(`Invalid Agent name: ${agent}. Use lowercase letters, digits, and hyphens.\n`);
|
|
53
|
+
return 1;
|
|
54
|
+
}
|
|
55
|
+
if ((await readTextIfExists(configFile)) !== undefined) {
|
|
56
|
+
io.stderr(`${configFile} already exists. Edit it, or remove it to start again.\n`);
|
|
57
|
+
return 1;
|
|
58
|
+
}
|
|
59
|
+
const config = configTemplate(agent, provider, model);
|
|
60
|
+
const parsed = parseConfigText(config);
|
|
61
|
+
if (!parsed.ok) {
|
|
62
|
+
io.stderr(`The generated config is invalid: ${parsed.issues.map((issue) => issue.message).join("; ")}\n`);
|
|
63
|
+
return 1;
|
|
64
|
+
}
|
|
65
|
+
await mkdir(path.join(options.home, AGENTS_DIR, agent), { recursive: true });
|
|
66
|
+
await writeFileAtomic(configFile, config, CONFIG_FILE_MODE);
|
|
67
|
+
if ((await readTextIfExists(secretsFile)) === undefined) {
|
|
68
|
+
await writeFileAtomic(secretsFile, secretsTemplate(provider), SECRETS_FILE_MODE);
|
|
69
|
+
}
|
|
70
|
+
io.stdout(`Created ${configFile} with the Agent "${agent}" (${provider}/${model}).\nSet ${apiKeySecret(provider)} in ${secretsFile} or the environment, then run "wheatley daemon".\n`);
|
|
71
|
+
return 0;
|
|
72
|
+
};
|
package/dist/cli/io.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { createInterface } from "node:readline";
|
|
5
|
+
import { isatty } from "node:tty";
|
|
6
|
+
/** The terminal of the running process. */
|
|
7
|
+
export const processIo = () => {
|
|
8
|
+
const shutdown = new AbortController();
|
|
9
|
+
const stop = () => {
|
|
10
|
+
shutdown.abort(new Error("The process received a stop signal"));
|
|
11
|
+
};
|
|
12
|
+
process.once("SIGINT", stop);
|
|
13
|
+
process.once("SIGTERM", stop);
|
|
14
|
+
const readline = createInterface({ input: process.stdin, terminal: false });
|
|
15
|
+
return {
|
|
16
|
+
env: process.env,
|
|
17
|
+
isTTY: isatty(process.stdin.fd),
|
|
18
|
+
lines: readline[Symbol.asyncIterator](),
|
|
19
|
+
shutdown: shutdown.signal,
|
|
20
|
+
stderr: (text) => {
|
|
21
|
+
process.stderr.write(text);
|
|
22
|
+
},
|
|
23
|
+
stdout: (text) => {
|
|
24
|
+
process.stdout.write(text);
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/** Reads the next line, or resolves `undefined` at the end of input. */
|
|
29
|
+
export const nextLine = async (io) => {
|
|
30
|
+
const result = await io.lines.next();
|
|
31
|
+
return result.done === true ? undefined : result.value;
|
|
32
|
+
};
|
|
33
|
+
/** The Deployment home: `--home`, else `WHEATLEY_HOME`, else `~/.wheatley`. */
|
|
34
|
+
export const resolveHome = (flag, env) => path.resolve(flag ?? env.WHEATLEY_HOME ?? path.join(homedir(), ".wheatley"));
|
|
35
|
+
const isVersionedPackage = (value) => typeof value === "object" &&
|
|
36
|
+
value !== null &&
|
|
37
|
+
"version" in value &&
|
|
38
|
+
typeof value.version === "string";
|
|
39
|
+
/** The version of the `wheatley` package, read from its `package.json`. */
|
|
40
|
+
export const packageVersion = async () => {
|
|
41
|
+
// The file is two folders up from both `src/cli/` and `dist/cli/`.
|
|
42
|
+
const text = await readFile(new URL("../../package.json", import.meta.url), "utf-8");
|
|
43
|
+
const value = JSON.parse(text);
|
|
44
|
+
return isVersionedPackage(value) ? value.version : "0.0.0";
|
|
45
|
+
};
|