@wheatley-ai/cli 0.3.0 → 0.4.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/dist/cli/ask.js +8 -4
- package/dist/cli/chat.js +8 -4
- package/dist/cli/daemon.js +45 -0
- package/dist/cli/io.js +1 -0
- package/dist/cli/main.js +145 -163
- package/dist/cli/session.js +59 -3
- package/dist/cli/status.js +4 -4
- package/dist/cli/tui/app.js +14 -2
- package/dist/cli/tui/controller.js +80 -19
- package/dist/cli/tui/markdown.js +229 -104
- package/dist/cli/tui/model-picker.js +76 -0
- package/dist/cli/tui/picker-panel.js +39 -0
- package/dist/core/agent/agent.js +23 -12
- package/dist/core/config/config-store.js +11 -6
- package/dist/core/config/diagnostics.js +116 -0
- package/dist/core/config/secret-store.js +13 -9
- package/dist/core/deployment.js +19 -1
- package/dist/core/markdown-tokens.js +60 -0
- package/dist/core/model-catalog.js +54 -0
- package/dist/core/rpc/deployment-handlers.js +2 -1
- package/dist/core/server/control-server.js +19 -21
- package/dist/core/server/http-server.js +17 -0
- package/dist/core/server/static-files.js +40 -66
- package/dist/dashboard/assets/index-DGa2Ps9u.js +12 -0
- package/dist/dashboard/assets/index-DeERxJOJ.css +2 -0
- package/dist/dashboard/assets/{sheet-presentation-CkgiDsTg.js → sheet-presentation-CZ1dO2kr.js} +1 -1
- package/dist/dashboard/index.html +2 -2
- package/dist/frontends/discord/approvals.js +8 -43
- package/dist/frontends/discord/binding.js +81 -8
- package/dist/frontends/discord/json-file.js +57 -0
- package/dist/frontends/discord/model-picker.js +276 -0
- package/dist/frontends/slack/binding.js +33 -3
- package/dist/frontends/slack/inbound.js +21 -12
- package/dist/frontends/slack/model-picker-blocks.js +147 -0
- package/dist/frontends/slack/outbound.js +27 -2
- package/dist/frontends/slack/schemas.js +9 -2
- package/dist/frontends/telegram/approvals.js +13 -48
- package/dist/frontends/telegram/binding.js +5 -0
- package/dist/frontends/telegram/client.js +7 -0
- package/dist/frontends/telegram/delivery.js +19 -6
- package/dist/frontends/telegram/inbound.js +64 -8
- package/dist/frontends/telegram/markdown.js +120 -83
- package/dist/frontends/telegram/model-picker.js +160 -0
- package/dist/frontends/telegram/token-store.js +73 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/domain.d.ts +13 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/domain.js +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/index.d.ts +1 -1
- package/dist/node_modules/@wheatley-ai/protocol/dist/index.js +1 -1
- package/dist/node_modules/@wheatley-ai/protocol/dist/methods.d.ts +20 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/methods.js +9 -1
- package/dist/node_modules/@wheatley-ai/sdk/dist/commands.d.ts +2 -2
- package/dist/node_modules/@wheatley-ai/sdk/dist/commands.js +2 -2
- package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.d.ts +23 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/index.d.ts +1 -1
- package/dist/node_modules/@wheatley-ai/sdk/package.json +1 -1
- package/dist/ops/cli.js +92 -98
- package/dist/ops/service.js +6 -1
- package/dist/ops/update.js +14 -27
- package/dist/triggers/delivery-codec.js +13 -0
- package/dist/triggers/webhook-ingress.js +89 -82
- package/package.json +9 -2
- package/dist/dashboard/assets/index-C9Rul5vE.css +0 -2
- package/dist/dashboard/assets/index-DThc2x_d.js +0 -11
package/dist/ops/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { homedir } from "node:os";
|
|
2
|
-
import {
|
|
2
|
+
import { Argument, Option } from "commander";
|
|
3
3
|
import { systemClock } from "../contracts/module.js";
|
|
4
4
|
import { listBackups, restoreAgentBackup } from "./backup.js";
|
|
5
5
|
import { processRunner } from "./command-runner.js";
|
|
@@ -24,102 +24,70 @@ export const hostCliDeps = (isDaemonRunning, drainTimeoutMs) => ({
|
|
|
24
24
|
});
|
|
25
25
|
const EXIT_OK = 0;
|
|
26
26
|
const EXIT_FAILURE = 1;
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
/** Turns thrown errors into exit codes, so that a command never rejects. */
|
|
37
|
-
const guarded = (usage, body) => async (args, io) => {
|
|
27
|
+
const SERVICE_ACTIONS = [
|
|
28
|
+
"install",
|
|
29
|
+
"start",
|
|
30
|
+
"stop",
|
|
31
|
+
"restart",
|
|
32
|
+
"status",
|
|
33
|
+
];
|
|
34
|
+
/** Runs `body` and turns a thrown error into exit code 1 and a line on `io.err`, so that a command never rejects. */
|
|
35
|
+
const guarded = async (io, body) => {
|
|
38
36
|
try {
|
|
39
|
-
return await body(
|
|
37
|
+
return await body();
|
|
40
38
|
}
|
|
41
39
|
catch (error) {
|
|
42
|
-
|
|
43
|
-
io.err(failure.message);
|
|
44
|
-
if (isUsageError(failure)) {
|
|
45
|
-
io.err(usage);
|
|
46
|
-
return EXIT_USAGE;
|
|
47
|
-
}
|
|
40
|
+
io.err(error instanceof Error ? error.message : String(error));
|
|
48
41
|
return EXIT_FAILURE;
|
|
49
42
|
}
|
|
50
43
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
env: deps.env,
|
|
66
|
-
execPath: deps.execPath,
|
|
67
|
-
homeDir: deps.homeDir,
|
|
68
|
-
drainTimeoutMs: deps.drainTimeoutMs,
|
|
69
|
-
});
|
|
70
|
-
const result = await manager.install(spec);
|
|
71
|
-
io.out(`Installed the ${manager.kind} unit: ${result.unitPath}`);
|
|
72
|
-
for (const note of result.notes) {
|
|
73
|
-
io.out(note);
|
|
74
|
-
}
|
|
75
|
-
return EXIT_OK;
|
|
76
|
-
}
|
|
77
|
-
case "start": {
|
|
78
|
-
await manager.start();
|
|
79
|
-
io.out("Started the Wheatley service.");
|
|
80
|
-
return EXIT_OK;
|
|
81
|
-
}
|
|
82
|
-
case "stop": {
|
|
83
|
-
await manager.stop();
|
|
84
|
-
io.out("Stopped the Wheatley service.");
|
|
85
|
-
return EXIT_OK;
|
|
86
|
-
}
|
|
87
|
-
case "restart": {
|
|
88
|
-
await manager.restart();
|
|
89
|
-
io.out("Restarted the Wheatley service.");
|
|
90
|
-
return EXIT_OK;
|
|
91
|
-
}
|
|
92
|
-
case "status": {
|
|
93
|
-
const status = await manager.status();
|
|
94
|
-
io.out(`Manager: ${status.manager}`);
|
|
95
|
-
io.out(`Unit: ${status.unitPath}`);
|
|
96
|
-
io.out(`State: ${status.state}`);
|
|
97
|
-
io.out(`PID: ${status.pid ?? "-"}`);
|
|
98
|
-
return EXIT_OK;
|
|
99
|
-
}
|
|
100
|
-
default: {
|
|
101
|
-
throw new UsageError(`Unknown service action: ${action}`);
|
|
44
|
+
/** The steps of each `wheatley service` action. */
|
|
45
|
+
const SERVICE_STEPS = {
|
|
46
|
+
install: async (manager, deps, io) => {
|
|
47
|
+
const spec = await resolveServiceSpec({
|
|
48
|
+
binScript: packageBinScript(),
|
|
49
|
+
env: deps.env,
|
|
50
|
+
execPath: deps.execPath,
|
|
51
|
+
homeDir: deps.homeDir,
|
|
52
|
+
drainTimeoutMs: deps.drainTimeoutMs,
|
|
53
|
+
});
|
|
54
|
+
const result = await manager.install(spec);
|
|
55
|
+
io.out(`Installed the ${manager.kind} unit: ${result.unitPath}`);
|
|
56
|
+
for (const note of result.notes) {
|
|
57
|
+
io.out(note);
|
|
102
58
|
}
|
|
103
|
-
}
|
|
59
|
+
},
|
|
60
|
+
restart: async (manager, _deps, io) => {
|
|
61
|
+
await manager.restart();
|
|
62
|
+
io.out("Restarted the Wheatley service.");
|
|
63
|
+
},
|
|
64
|
+
start: async (manager, _deps, io) => {
|
|
65
|
+
await manager.start();
|
|
66
|
+
io.out("Started the Wheatley service.");
|
|
67
|
+
},
|
|
68
|
+
status: async (manager, _deps, io) => {
|
|
69
|
+
const status = await manager.status();
|
|
70
|
+
io.out(`Manager: ${status.manager}`);
|
|
71
|
+
io.out(`Unit: ${status.unitPath}`);
|
|
72
|
+
io.out(`State: ${status.state}`);
|
|
73
|
+
io.out(`PID: ${status.pid ?? "-"}`);
|
|
74
|
+
},
|
|
75
|
+
stop: async (manager, _deps, io) => {
|
|
76
|
+
await manager.stop();
|
|
77
|
+
io.out("Stopped the Wheatley service.");
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
const serviceCommand = async (deps, io, action) => {
|
|
81
|
+
await SERVICE_STEPS[action](createServiceManager(deps), deps, io);
|
|
82
|
+
return EXIT_OK;
|
|
104
83
|
};
|
|
105
|
-
const isUpdateMethod = (value) => UPDATE_METHODS.some((method) => method === value);
|
|
106
84
|
const newerMajorLines = (major) => {
|
|
107
85
|
const intro = `Wheatley ${major.version} is a new major version. wheatley update stays within the current major version. Read the release notes, then`;
|
|
108
86
|
return major.action.kind === "command"
|
|
109
87
|
? [`${intro} run: ${major.action.command.join(" ")}`]
|
|
110
88
|
: [`${intro}:`, ...major.action.lines];
|
|
111
89
|
};
|
|
112
|
-
const updateCommand =
|
|
113
|
-
const { values } = parseArgs({
|
|
114
|
-
args: [...args],
|
|
115
|
-
options: {
|
|
116
|
-
check: { type: "boolean" },
|
|
117
|
-
method: { type: "string" },
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
if (values.method !== undefined && !isUpdateMethod(values.method)) {
|
|
121
|
-
throw new UsageError(`Unknown install method: ${values.method}`);
|
|
122
|
-
}
|
|
90
|
+
const updateCommand = async (deps, io, values) => {
|
|
123
91
|
const installation = await resolveInstallation({
|
|
124
92
|
env: deps.env,
|
|
125
93
|
execPath: deps.execPath,
|
|
@@ -170,15 +138,7 @@ const updateCommand = (deps) => async (args, io) => {
|
|
|
170
138
|
}
|
|
171
139
|
return EXIT_OK;
|
|
172
140
|
};
|
|
173
|
-
const restoreCommand = (deps
|
|
174
|
-
const { positionals } = parseArgs({
|
|
175
|
-
allowPositionals: true,
|
|
176
|
-
args: [...args],
|
|
177
|
-
});
|
|
178
|
-
const [agent, backup, ...extra] = positionals;
|
|
179
|
-
if (agent === undefined || extra.length > 0) {
|
|
180
|
-
throw new UsageError("Pass an Agent name and at most one backup.");
|
|
181
|
-
}
|
|
141
|
+
const restoreCommand = async (deps, io, agent, backup) => {
|
|
182
142
|
const agentDir = agentDirFor(deps.homeDir, agent);
|
|
183
143
|
if (backup === undefined) {
|
|
184
144
|
const backups = await listBackups(agentDir);
|
|
@@ -205,9 +165,43 @@ const restoreCommand = (deps) => async (args, io) => {
|
|
|
205
165
|
}
|
|
206
166
|
return EXIT_OK;
|
|
207
167
|
};
|
|
208
|
-
/**
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Adds `service`, `update`, and `restore` to `program`. Call it after the root options and the output, help, and
|
|
170
|
+
* exit settings of `program`, because subcommands copy those settings when they are created.
|
|
171
|
+
*
|
|
172
|
+
* - `wheatley service <install|start|stop|restart|status>`
|
|
173
|
+
* - `wheatley update [--check] [--method npm|pnpm|yarn|bun|brew|curl]`
|
|
174
|
+
* - `wheatley restore <agent> [backup]`: without `backup`, it lists the Agent's backups on stdout. With `backup`, it
|
|
175
|
+
* restores that backup. A running daemon, an invalid Agent name, or a failed restore prints the error on stderr and
|
|
176
|
+
* exits 1.
|
|
177
|
+
*
|
|
178
|
+
* Commander reports usage errors, for example a missing Agent name or an unknown option, before an action runs.
|
|
179
|
+
*/
|
|
180
|
+
export const addOpsCommands = (program, host) => {
|
|
181
|
+
const run = async (command, body) => {
|
|
182
|
+
host.exit(await guarded(host.io, async () => await body(host.deps(command))));
|
|
183
|
+
};
|
|
184
|
+
program
|
|
185
|
+
.command("service")
|
|
186
|
+
.description("Install or control the Wheatley service: install, start, stop, restart, status")
|
|
187
|
+
.addArgument(new Argument("<action>", "The service action").choices(SERVICE_ACTIONS))
|
|
188
|
+
.action(async (action, _options, command) => {
|
|
189
|
+
await run(command, async (deps) => await serviceCommand(deps, host.io, action));
|
|
190
|
+
});
|
|
191
|
+
program
|
|
192
|
+
.command("update")
|
|
193
|
+
.description("Update Wheatley within its major version")
|
|
194
|
+
.option("--check", "Report an available update and install nothing")
|
|
195
|
+
.addOption(new Option("--method <method>", "The install method of this Wheatley").choices(UPDATE_METHODS))
|
|
196
|
+
.action(async (options, command) => {
|
|
197
|
+
await run(command, async (deps) => await updateCommand(deps, host.io, options));
|
|
198
|
+
});
|
|
199
|
+
program
|
|
200
|
+
.command("restore")
|
|
201
|
+
.description("List the backups of an Agent, or restore its agent.sqlite")
|
|
202
|
+
.argument("<agent>", "The Agent name")
|
|
203
|
+
.argument("[backup]", "The backup file to restore")
|
|
204
|
+
.action(async (agent, backup, _options, command) => {
|
|
205
|
+
await run(command, async (deps) => await restoreCommand(deps, host.io, agent, backup));
|
|
206
|
+
});
|
|
207
|
+
};
|
package/dist/ops/service.js
CHANGED
|
@@ -45,6 +45,11 @@ const systemdArg = (value) => {
|
|
|
45
45
|
const escaped = value.replaceAll(PERCENT, "%%").replaceAll(DOLLAR, "$$$$");
|
|
46
46
|
return SYSTEMD_BARE_WORD.test(escaped) ? escaped : quoteSystemd(escaped);
|
|
47
47
|
};
|
|
48
|
+
/** Escapes a systemd path directive such as `WorkingDirectory=`, which does not strip quotes. */
|
|
49
|
+
const systemdPath = (value) => {
|
|
50
|
+
assertSingleLine(value);
|
|
51
|
+
return value.replaceAll(PERCENT, "%%");
|
|
52
|
+
};
|
|
48
53
|
const systemdValue = (value) => {
|
|
49
54
|
assertSingleLine(value);
|
|
50
55
|
return quoteSystemd(value.replaceAll(PERCENT, "%%"));
|
|
@@ -64,7 +69,7 @@ export const renderSystemdUnit = (spec) => {
|
|
|
64
69
|
"[Service]",
|
|
65
70
|
"Type=exec",
|
|
66
71
|
`ExecStart=${spec.command.map(systemdArg).join(" ")}`,
|
|
67
|
-
`WorkingDirectory=${
|
|
72
|
+
`WorkingDirectory=${systemdPath(spec.workingDirectory)}`,
|
|
68
73
|
...environment,
|
|
69
74
|
"Restart=on-failure",
|
|
70
75
|
`RestartSec=${RESTART_DELAY_SECONDS}`,
|
package/dist/ops/update.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import semver from "semver";
|
|
3
4
|
import { Type } from "typebox";
|
|
4
5
|
import { Value } from "typebox/value";
|
|
5
6
|
import { describeFailure } from "./command-runner.js";
|
|
@@ -11,26 +12,14 @@ export const DOCKER_UPDATE_COMMANDS = [
|
|
|
11
12
|
];
|
|
12
13
|
/** The `curl` installer. Run again with `--version`, it replaces the app in its XDG folder. */
|
|
13
14
|
export const INSTALLER_URL = "https://raw.githubusercontent.com/SphericalKat/wheatley/main/scripts/install.sh";
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
minor: Number(groups.minor),
|
|
23
|
-
patch: Number(groups.patch),
|
|
24
|
-
prerelease: groups.prerelease !== undefined,
|
|
25
|
-
text,
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
/** Orders by major, minor, and patch. A release sorts after a prerelease of the same numbers. */
|
|
29
|
-
const compareVersions = (a, b) => a.major - b.major ||
|
|
30
|
-
a.minor - b.minor ||
|
|
31
|
-
a.patch - b.patch ||
|
|
32
|
-
Number(b.prerelease) - Number(a.prerelease);
|
|
33
|
-
const newest = (versions) => versions.toSorted(compareVersions).at(-1);
|
|
15
|
+
const BUILD_METADATA = /\+.*$/u;
|
|
16
|
+
/**
|
|
17
|
+
* Accepts only canonical semantic versions. `semver.valid` also accepts a `v` prefix and white space, and drops build
|
|
18
|
+
* metadata from its result, so the text without build metadata must equal the normalized version.
|
|
19
|
+
*/
|
|
20
|
+
const isVersion = (text) => semver.valid(text) === text.replace(BUILD_METADATA, "");
|
|
21
|
+
const isStable = (text) => semver.prerelease(text) === null;
|
|
22
|
+
const newest = (versions) => versions.toSorted(semver.rcompare).at(0);
|
|
34
23
|
/**
|
|
35
24
|
* Applies the major-version guard: picks the newest stable release with the same major version as `current` that is
|
|
36
25
|
* newer than `current`. It ignores prereleases and text that is not a version.
|
|
@@ -38,16 +27,14 @@ const newest = (versions) => versions.toSorted(compareVersions).at(-1);
|
|
|
38
27
|
* Errors: throws if `current` is not a semantic version.
|
|
39
28
|
*/
|
|
40
29
|
export const selectUpdate = (current, available) => {
|
|
41
|
-
|
|
42
|
-
if (installed === undefined) {
|
|
30
|
+
if (!isVersion(current)) {
|
|
43
31
|
throw new Error(`The installed version is not a semantic version: ${current}`);
|
|
44
32
|
}
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
.filter((version) => !version.prerelease && compareVersions(version, installed) > 0);
|
|
33
|
+
const installedMajor = semver.major(current);
|
|
34
|
+
const releases = available.filter((text) => isVersion(text) && isStable(text) && semver.gt(text, current));
|
|
48
35
|
return {
|
|
49
|
-
newerMajor: newest(releases.filter((
|
|
50
|
-
target: newest(releases.filter((
|
|
36
|
+
newerMajor: newest(releases.filter((text) => semver.major(text) > installedMajor)),
|
|
37
|
+
target: newest(releases.filter((text) => semver.major(text) === installedMajor)),
|
|
51
38
|
};
|
|
52
39
|
};
|
|
53
40
|
const command = (verify, ...parts) => ({ command: parts, kind: "command", verify });
|
|
@@ -16,12 +16,25 @@ const StoredAttachmentSchema = Type.Union([
|
|
|
16
16
|
sizeBytes: Type.Number(),
|
|
17
17
|
}),
|
|
18
18
|
]);
|
|
19
|
+
const StoredModelRefSchema = Type.Object({
|
|
20
|
+
id: Type.String(),
|
|
21
|
+
provider: Type.String(),
|
|
22
|
+
});
|
|
23
|
+
const StoredModelPickerSchema = Type.Object({
|
|
24
|
+
current: Type.Optional(StoredModelRefSchema),
|
|
25
|
+
models: Type.Array(Type.Object({
|
|
26
|
+
id: Type.String(),
|
|
27
|
+
name: Type.String(),
|
|
28
|
+
provider: Type.String(),
|
|
29
|
+
})),
|
|
30
|
+
});
|
|
19
31
|
const StoredDeliverySchema = Type.Union([
|
|
20
32
|
Type.Object({
|
|
21
33
|
attachments: Type.Optional(Type.Array(StoredAttachmentSchema)),
|
|
22
34
|
chatId: Type.String(),
|
|
23
35
|
kind: Type.Literal("message"),
|
|
24
36
|
messageId: Type.String(),
|
|
37
|
+
modelPicker: Type.Optional(StoredModelPickerSchema),
|
|
25
38
|
runId: Type.Optional(Type.String()),
|
|
26
39
|
source: Type.Union([
|
|
27
40
|
Type.Literal("reply"),
|
|
@@ -1,47 +1,59 @@
|
|
|
1
1
|
import { timingSafeEqual } from "node:crypto";
|
|
2
2
|
import { once } from "node:events";
|
|
3
|
-
import {
|
|
3
|
+
import { Hono } from "hono";
|
|
4
|
+
import { bodyLimit } from "hono/body-limit";
|
|
5
|
+
import { HTTPException } from "hono/http-exception";
|
|
4
6
|
import { SeamError } from "../contracts/errors.js";
|
|
5
7
|
import { MAX_WEBHOOK_BODY_BYTES } from "../contracts/webhook-ingress.js";
|
|
8
|
+
import { createHttpServer } from "../core/server/http-server.js";
|
|
6
9
|
import { errorText } from "./errors.js";
|
|
7
10
|
/** A request must arrive in full within this time. */
|
|
8
11
|
const REQUEST_TIMEOUT_MS = 30_000;
|
|
9
12
|
const BEARER = /^Bearer (?<token>\S+)$/u;
|
|
10
13
|
const SAFE_SUFFIX = /^[A-Za-z0-9._~\-/]+$/u;
|
|
14
|
+
/** The origin of the `Request` that `handle` builds. The Ingress routes on the path only. */
|
|
15
|
+
const INTERNAL_ORIGIN = "http://ingress.invalid";
|
|
16
|
+
const FRONTEND_PREFIX = /^\/frontends\/[^/]+\/[^/]+\//u;
|
|
11
17
|
const status = (code) => ({ status: code });
|
|
12
18
|
const tokensMatch = (given, expected) => {
|
|
13
19
|
const left = Buffer.from(given);
|
|
14
20
|
const right = Buffer.from(expected);
|
|
15
21
|
return left.length === right.length && timingSafeEqual(left, right);
|
|
16
22
|
};
|
|
17
|
-
const
|
|
18
|
-
?
|
|
19
|
-
:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return undefined;
|
|
28
|
-
}
|
|
29
|
-
const chunks = [];
|
|
30
|
-
let size = 0;
|
|
31
|
-
// The early return must not destroy the socket before the `413` answer.
|
|
32
|
-
for await (const chunk of request.iterator({ destroyOnReturn: false })) {
|
|
33
|
-
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
34
|
-
size += buffer.length;
|
|
35
|
-
if (size > MAX_WEBHOOK_BODY_BYTES) {
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
chunks.push(buffer);
|
|
39
|
-
}
|
|
40
|
-
return new Uint8Array(Buffer.concat(chunks));
|
|
23
|
+
const toResponse = (result) => new Response(result.body instanceof Uint8Array
|
|
24
|
+
? new Uint8Array(result.body)
|
|
25
|
+
: result.body, { headers: result.headers, status: result.status });
|
|
26
|
+
const fromResponse = async (response) => {
|
|
27
|
+
const body = new Uint8Array(await response.arrayBuffer());
|
|
28
|
+
return {
|
|
29
|
+
status: response.status,
|
|
30
|
+
headers: Object.fromEntries(response.headers),
|
|
31
|
+
...(body.byteLength > 0 && { body }),
|
|
32
|
+
};
|
|
41
33
|
};
|
|
42
34
|
const isAddressInfo = (address) => typeof address === "object" && address !== null;
|
|
35
|
+
const readRequest = (c) => ({
|
|
36
|
+
body: c.get("body"),
|
|
37
|
+
headers: Object.fromEntries(c.req.raw.headers),
|
|
38
|
+
path: new URL(c.req.url).pathname,
|
|
39
|
+
query: c.req.query(),
|
|
40
|
+
});
|
|
41
|
+
const noop = async () => {
|
|
42
|
+
await Promise.resolve();
|
|
43
|
+
};
|
|
44
|
+
/** An error that answers `code` with an empty body and closes the connection. */
|
|
45
|
+
const closingError = (code) => new HTTPException(code, {
|
|
46
|
+
res: new Response(null, { headers: { connection: "close" }, status: code }),
|
|
47
|
+
});
|
|
48
|
+
const limitBody = bodyLimit({
|
|
49
|
+
maxSize: MAX_WEBHOOK_BODY_BYTES,
|
|
50
|
+
onError: () => {
|
|
51
|
+
throw closingError(413);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
43
54
|
/**
|
|
44
|
-
* The Webhook Ingress (ADR 0010, ADR 0012): the routing of `WebhookIngress` and a `node:http` listener
|
|
55
|
+
* The Webhook Ingress (ADR 0010, ADR 0012): the routing of `WebhookIngress` as a Hono app and a `node:http` listener
|
|
56
|
+
* for it.
|
|
45
57
|
*
|
|
46
58
|
* The listener answers `413` for a body over 256 KiB as soon as the declared length or the streamed bytes pass the
|
|
47
59
|
* cap, and closes the connection without reading the rest.
|
|
@@ -49,9 +61,11 @@ const isAddressInfo = (address) => typeof address === "object" && address !== nu
|
|
|
49
61
|
export class HttpWebhookIngress {
|
|
50
62
|
#options;
|
|
51
63
|
#routes = new Map();
|
|
64
|
+
#app;
|
|
52
65
|
#server;
|
|
53
66
|
constructor(options) {
|
|
54
67
|
this.#options = options;
|
|
68
|
+
this.#app = this.#createApp();
|
|
55
69
|
}
|
|
56
70
|
registerFrontendRoute = (agent, bindingId, pathSuffix, handler) => {
|
|
57
71
|
const invalid = pathSuffix === "" ||
|
|
@@ -75,29 +89,12 @@ export class HttpWebhookIngress {
|
|
|
75
89
|
};
|
|
76
90
|
};
|
|
77
91
|
handle = async (request) => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const [empty, area, agentName = "", target = "", ...rest] = request.path.split("/");
|
|
82
|
-
if (empty !== "") {
|
|
83
|
-
return status(404);
|
|
84
|
-
}
|
|
85
|
-
try {
|
|
86
|
-
if (area === "webhooks" && rest.length === 0) {
|
|
87
|
-
return await this.#trigger(agentName, target, request);
|
|
88
|
-
}
|
|
89
|
-
if (area === "frontends") {
|
|
90
|
-
return await this.#frontend(agentName, rest.join("/"), request);
|
|
91
|
-
}
|
|
92
|
-
return status(404);
|
|
93
|
-
}
|
|
94
|
-
catch (error) {
|
|
95
|
-
this.#options.log.error("A webhook request failed", {
|
|
96
|
-
error: errorText(error),
|
|
97
|
-
path: request.path,
|
|
98
|
-
});
|
|
99
|
-
return status(500);
|
|
92
|
+
const url = new URL(request.path, INTERNAL_ORIGIN);
|
|
93
|
+
for (const [name, value] of Object.entries(request.query)) {
|
|
94
|
+
url.searchParams.set(name, value);
|
|
100
95
|
}
|
|
96
|
+
const response = await this.#app.fetch(new Request(url, { headers: request.headers, method: request.method }), { body: request.body });
|
|
97
|
+
return await fromResponse(response);
|
|
101
98
|
};
|
|
102
99
|
/** The bound address while the listener runs, else `undefined`. */
|
|
103
100
|
address = () => {
|
|
@@ -106,9 +103,7 @@ export class HttpWebhookIngress {
|
|
|
106
103
|
};
|
|
107
104
|
/** Starts the listener on `host` and `port`. Rejects if the port is in use. */
|
|
108
105
|
listen = async (host, port) => {
|
|
109
|
-
const server =
|
|
110
|
-
void this.#serve(request, response);
|
|
111
|
-
});
|
|
106
|
+
const server = createHttpServer(this.#app.fetch, "Webhook Ingress");
|
|
112
107
|
server.requestTimeout = REQUEST_TIMEOUT_MS;
|
|
113
108
|
server.listen(port, host);
|
|
114
109
|
await once(server, "listening");
|
|
@@ -126,6 +121,49 @@ export class HttpWebhookIngress {
|
|
|
126
121
|
server.closeAllConnections();
|
|
127
122
|
await closed;
|
|
128
123
|
};
|
|
124
|
+
#createApp = () => {
|
|
125
|
+
const app = new Hono();
|
|
126
|
+
app.post("*", async (c, proceed) => {
|
|
127
|
+
c.set("body", c.env.body ?? (await this.#readBody(c)));
|
|
128
|
+
await proceed();
|
|
129
|
+
});
|
|
130
|
+
app.post("/webhooks/:agent/:triggerId", async (c) => toResponse(await this.#trigger(c.req.param("agent"), c.req.param("triggerId"), readRequest(c))));
|
|
131
|
+
app.post("/frontends/:agent/:bindingId/*", async (c) => {
|
|
132
|
+
const request = readRequest(c);
|
|
133
|
+
return toResponse(await this.#frontend(c.req.param("agent"), request.path.replace(FRONTEND_PREFIX, ""), request));
|
|
134
|
+
});
|
|
135
|
+
app.all("*", (c) => c.body(null, c.req.method === "POST" ? 404 : 405));
|
|
136
|
+
app.onError((failure, c) => {
|
|
137
|
+
if (failure instanceof HTTPException) {
|
|
138
|
+
return failure.getResponse();
|
|
139
|
+
}
|
|
140
|
+
this.#options.log.error("A webhook request failed", {
|
|
141
|
+
error: errorText(failure),
|
|
142
|
+
path: c.req.path,
|
|
143
|
+
});
|
|
144
|
+
return c.body(null, 500);
|
|
145
|
+
});
|
|
146
|
+
return app;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Reads the body of a listener request. Throws an `HTTPException` that answers `413` past the cap, or `400` for a
|
|
150
|
+
* body that cannot be read, for example after the client aborts. Handler failures answer `500` in `onError`.
|
|
151
|
+
*/
|
|
152
|
+
#readBody = async (c) => {
|
|
153
|
+
try {
|
|
154
|
+
await limitBody(c, noop);
|
|
155
|
+
return new Uint8Array(await c.req.arrayBuffer());
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (error instanceof HTTPException) {
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
this.#options.log.warn("A webhook request was not read", {
|
|
162
|
+
error: errorText(error),
|
|
163
|
+
});
|
|
164
|
+
throw closingError(400);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
129
167
|
#trigger = async (agentName, triggerId, request) => {
|
|
130
168
|
const agent = this.#options.agent(agentName);
|
|
131
169
|
if (agent === undefined) {
|
|
@@ -178,35 +216,4 @@ export class HttpWebhookIngress {
|
|
|
178
216
|
query: request.query,
|
|
179
217
|
});
|
|
180
218
|
};
|
|
181
|
-
#serve = async (request, response) => {
|
|
182
|
-
const url = new URL(request.url ?? "/", "http://ingress.invalid");
|
|
183
|
-
try {
|
|
184
|
-
const body = request.method === "POST" ? await readBody(request) : new Uint8Array();
|
|
185
|
-
if (body === undefined) {
|
|
186
|
-
response.writeHead(413, { connection: "close" });
|
|
187
|
-
response.end(() => {
|
|
188
|
-
request.destroy();
|
|
189
|
-
});
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
const result = await this.handle({
|
|
193
|
-
body,
|
|
194
|
-
headers: headersOf(request),
|
|
195
|
-
method: request.method ?? "",
|
|
196
|
-
path: url.pathname,
|
|
197
|
-
query: Object.fromEntries(url.searchParams),
|
|
198
|
-
});
|
|
199
|
-
response.writeHead(result.status, result.headers);
|
|
200
|
-
response.end(result.body);
|
|
201
|
-
}
|
|
202
|
-
catch (error) {
|
|
203
|
-
this.#options.log.warn("A webhook request was not read", {
|
|
204
|
-
error: errorText(error),
|
|
205
|
-
});
|
|
206
|
-
if (!response.headersSent) {
|
|
207
|
-
response.writeHead(400, { connection: "close" });
|
|
208
|
-
}
|
|
209
|
-
response.end();
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
219
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wheatley-ai/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Wheatley daemon and CLI",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"repository": {
|
|
@@ -21,19 +21,25 @@
|
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
+
"@babel/code-frame": "8.0.6",
|
|
24
25
|
"@earendil-works/chord": "1.6.1",
|
|
25
26
|
"@earendil-works/pi-ai": "1.6.1",
|
|
26
27
|
"@earendil-works/pi-durable": "1.6.1",
|
|
27
28
|
"@earendil-works/pi-schema": "1.6.1",
|
|
29
|
+
"@hono/node-server": "2.1.11",
|
|
28
30
|
"@modelcontextprotocol/client": "2.4.0",
|
|
29
31
|
"@slack/socket-mode": "3.2.0",
|
|
30
32
|
"@slack/web-api": "8.5.0",
|
|
33
|
+
"commander": "15.1.0",
|
|
31
34
|
"croner": "11.0.1",
|
|
32
35
|
"discord.js": "14.28.1",
|
|
33
36
|
"grammy": "1.48.0",
|
|
37
|
+
"hono": "4.13.23",
|
|
34
38
|
"import-meta-resolve": "4.2.0",
|
|
35
39
|
"ink": "8.0.0",
|
|
40
|
+
"marked": "18.3.0",
|
|
36
41
|
"react": "19.3.1",
|
|
42
|
+
"semver": "7.8.6",
|
|
37
43
|
"string-width": "9.0.0",
|
|
38
44
|
"typebox": "1.4.7",
|
|
39
45
|
"wrap-ansi": "10.0.2",
|
|
@@ -43,10 +49,11 @@
|
|
|
43
49
|
"devDependencies": {
|
|
44
50
|
"@modelcontextprotocol/server": "2.4.0",
|
|
45
51
|
"@types/react": "19.3.3",
|
|
52
|
+
"@types/semver": "7.8.0",
|
|
46
53
|
"@types/ws": "8.18.2",
|
|
47
54
|
"@wheatley-ai/dashboard": "0.0.0",
|
|
48
55
|
"@wheatley-ai/protocol": "0.0.0",
|
|
49
|
-
"@wheatley-ai/sdk": "0.
|
|
56
|
+
"@wheatley-ai/sdk": "0.4.0",
|
|
50
57
|
"ink-testing-library": "4.0.0"
|
|
51
58
|
},
|
|
52
59
|
"engines": {
|