@tenonai/yew 0.0.0-stage → 1.3.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/CHANGELOG.md +273 -0
- package/LICENSE +21 -0
- package/README.md +64 -2
- package/THIRD-PARTY-NOTICES.md +122 -0
- package/bin/yew.js +9 -0
- package/dist/cli/app.js +211 -0
- package/dist/cli/export.js +57 -0
- package/dist/cli/io.js +65 -0
- package/dist/cli/key.js +113 -0
- package/dist/cli/main.js +58 -0
- package/dist/cli/mcp.js +76 -0
- package/dist/cli/remote.js +116 -0
- package/dist/cli/run.js +72 -0
- package/dist/cli/schedule.js +123 -0
- package/dist/cli/send.js +127 -0
- package/dist/cli/serve.js +301 -0
- package/dist/cli/service.js +268 -0
- package/dist/cli/system.js +44 -0
- package/dist/remote/access.js +107 -0
- package/dist/remote/connector.js +278 -0
- package/dist/remote/identity.js +71 -0
- package/dist/remote/noise.js +287 -0
- package/dist/remote/protocol.js +51 -0
- package/dist/remote/push.js +37 -0
- package/dist/server/agent/compaction.js +242 -0
- package/dist/server/agent/context.js +120 -0
- package/dist/server/agent/held.js +48 -0
- package/dist/server/agent/helper.js +122 -0
- package/dist/server/agent/mailroom.js +510 -0
- package/dist/server/agent/prompt.js +268 -0
- package/dist/server/agent/runner.js +1159 -0
- package/dist/server/artifacts.js +216 -0
- package/dist/server/budget.js +88 -0
- package/dist/server/data-export.js +105 -0
- package/dist/server/data-usage.js +144 -0
- package/dist/server/db/database.js +49 -0
- package/dist/server/db/migrations.js +343 -0
- package/dist/server/errors.js +16 -0
- package/dist/server/events.js +17 -0
- package/dist/server/export.js +378 -0
- package/dist/server/folder-picker.js +32 -0
- package/dist/server/from-jig.js +98 -0
- package/dist/server/http/agents.js +78 -0
- package/dist/server/http/api.js +465 -0
- package/dist/server/http/app.js +111 -0
- package/dist/server/http/artifacts.js +53 -0
- package/dist/server/http/data.js +54 -0
- package/dist/server/http/devices.js +47 -0
- package/dist/server/http/events.js +134 -0
- package/dist/server/http/idempotency.js +53 -0
- package/dist/server/http/input.js +106 -0
- package/dist/server/http/mail.js +52 -0
- package/dist/server/http/mcp.js +134 -0
- package/dist/server/http/notes.js +91 -0
- package/dist/server/http/problem.js +55 -0
- package/dist/server/http/providers.js +131 -0
- package/dist/server/http/schedules.js +58 -0
- package/dist/server/http/search.js +42 -0
- package/dist/server/http/security.js +168 -0
- package/dist/server/http/skills.js +53 -0
- package/dist/server/http/system.js +26 -0
- package/dist/server/ids.js +30 -0
- package/dist/server/keys.js +70 -0
- package/dist/server/mail/envelope.js +152 -0
- package/dist/server/mail/post.js +603 -0
- package/dist/server/mail/store.js +279 -0
- package/dist/server/manage.js +601 -0
- package/dist/server/mcp-builtin.js +180 -0
- package/dist/server/mcp-config.js +305 -0
- package/dist/server/mcp-oauth.js +448 -0
- package/dist/server/mcp.js +668 -0
- package/dist/server/model/gateway.js +410 -0
- package/dist/server/paths.js +43 -0
- package/dist/server/permissions/engine.js +249 -0
- package/dist/server/permissions/paths.js +93 -0
- package/dist/server/permissions/shell.js +635 -0
- package/dist/server/push.js +345 -0
- package/dist/server/reveal.js +36 -0
- package/dist/server/runtime.js +17 -0
- package/dist/server/schedules.js +356 -0
- package/dist/server/settings.js +77 -0
- package/dist/server/skills.js +263 -0
- package/dist/server/store.js +1077 -0
- package/dist/server/tools/files.js +318 -0
- package/dist/server/tools/html.js +221 -0
- package/dist/server/tools/jobs.js +38 -0
- package/dist/server/tools/mail.js +680 -0
- package/dist/server/tools/match.js +81 -0
- package/dist/server/tools/misc.js +253 -0
- package/dist/server/tools/process.js +128 -0
- package/dist/server/tools/read-state.js +40 -0
- package/dist/server/tools/registry.js +43 -0
- package/dist/server/tools/search.js +175 -0
- package/dist/server/tools/shell.js +124 -0
- package/dist/server/tools/types.js +43 -0
- package/dist/server/tools/web-search.js +198 -0
- package/dist/server/tools/web.js +240 -0
- package/dist/shared/cron.js +105 -0
- package/dist/shared/mcp-presets.js +170 -0
- package/dist/shared/messages.js +45 -0
- package/dist/shared/policy.js +42 -0
- package/dist/shared/types.js +34 -0
- package/dist/shared/version.js +8 -0
- package/mcp-builtin/package-lock.json +3398 -0
- package/mcp-builtin/package.json +14 -0
- package/mcp-builtin/presets/gitee/package-lock.json +105 -0
- package/mcp-builtin/presets/gitee/package.json +10 -0
- package/mcp-builtin/presets/notion/package-lock.json +2243 -0
- package/mcp-builtin/presets/notion/package.json +10 -0
- package/mcp-builtin/presets/sentry/package-lock.json +28 -0
- package/mcp-builtin/presets/sentry/package.json +10 -0
- package/package.json +75 -5
- package/web/dist/THIRD-PARTY-NOTICES.txt +1944 -0
- package/web/dist/assets/index-BtK_SzpF.css +1 -0
- package/web/dist/assets/index-C0rnUFI-.js +68 -0
- package/web/dist/icons/app-icon.png +0 -0
- package/web/dist/icons/apple-touch-icon.png +0 -0
- package/web/dist/icons/favicon-16.png +0 -0
- package/web/dist/icons/favicon-32.png +0 -0
- package/web/dist/icons/favicon-64.png +0 -0
- package/web/dist/icons/tenon-mark.svg +1 -0
- package/web/dist/icons/yew-mark.svg +1 -0
- package/web/dist/index.html +18 -0
- package/web/dist/mascot/gift.webp +0 -0
- package/web/dist/mascot/greeting.webp +0 -0
- package/web/dist/mascot/happy.webp +0 -0
- package/web/dist/mascot/lost.webp +0 -0
- package/web/dist/mascot/shush.webp +0 -0
- package/web/dist/mascot/sleepy.webp +0 -0
- package/web/dist/mascot/sorry.webp +0 -0
- package/web/dist/mascot/thinking.webp +0 -0
- package/web/dist/mascot/waking.webp +0 -0
- package/web/dist/mascot/writing.webp +0 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { RELAY_URL } from "../remote/protocol.js";
|
|
3
|
+
import { EXIT_ERROR, EXIT_OK, UsageError } from "./io.js";
|
|
4
|
+
import { serviceRunning } from "./serve.js";
|
|
5
|
+
const HELP = `Usage: yew remote <on|off|status|pair> [options]
|
|
6
|
+
|
|
7
|
+
Let your phone reach Yew on this Mac through Yew's relay. Both sides connect
|
|
8
|
+
out to the relay, so this Mac opens no port; everything between them is
|
|
9
|
+
end-to-end encrypted, and the relay sees only ciphertext.
|
|
10
|
+
|
|
11
|
+
Commands:
|
|
12
|
+
on Turn on phone access
|
|
13
|
+
off Turn it off; paired phones stay paired
|
|
14
|
+
status Show whether it is on, the relay and the paired phones
|
|
15
|
+
pair Show a QR code to pair a phone (valid for 5 minutes, once)
|
|
16
|
+
|
|
17
|
+
Options:
|
|
18
|
+
--relay <url> With on: the relay to use (default wss://relay.tenonai.cn)
|
|
19
|
+
--json Print as JSON
|
|
20
|
+
-h, --help Show this help
|
|
21
|
+
`;
|
|
22
|
+
export function remoteCommand() {
|
|
23
|
+
return {
|
|
24
|
+
name: "remote",
|
|
25
|
+
summary: "Let your phone reach this Mac",
|
|
26
|
+
async run(args, io) {
|
|
27
|
+
const { values, positionals } = parseArgs({
|
|
28
|
+
args,
|
|
29
|
+
options: {
|
|
30
|
+
relay: { type: "string" },
|
|
31
|
+
json: { type: "boolean" },
|
|
32
|
+
help: { type: "boolean", short: "h" },
|
|
33
|
+
},
|
|
34
|
+
allowPositionals: true,
|
|
35
|
+
strict: true,
|
|
36
|
+
});
|
|
37
|
+
if (values.help) {
|
|
38
|
+
io.stdout.write(HELP);
|
|
39
|
+
return EXIT_OK;
|
|
40
|
+
}
|
|
41
|
+
const action = positionals[0];
|
|
42
|
+
if (positionals.length !== 1 || !["on", "off", "status", "pair"].includes(action ?? "")) {
|
|
43
|
+
throw new UsageError("yew remote needs one of on, off, status or pair");
|
|
44
|
+
}
|
|
45
|
+
const relay = values.relay;
|
|
46
|
+
if (relay !== undefined && !RELAY_URL.test(relay)) {
|
|
47
|
+
throw new UsageError("--relay must be a wss:// address (ws:// only inside this network)");
|
|
48
|
+
}
|
|
49
|
+
const { yewHome, dbPath } = await import("../server/paths.js");
|
|
50
|
+
const { openDatabase } = await import("../server/db/database.js");
|
|
51
|
+
const { EventBus } = await import("../server/events.js");
|
|
52
|
+
const { Store } = await import("../server/store.js");
|
|
53
|
+
const { readSettings } = await import("../server/settings.js");
|
|
54
|
+
const home = yewHome(io.env);
|
|
55
|
+
const db = openDatabase(dbPath(home));
|
|
56
|
+
try {
|
|
57
|
+
const store = new Store(db, new EventBus());
|
|
58
|
+
const settings = readSettings(store).remote;
|
|
59
|
+
const running = serviceRunning(home);
|
|
60
|
+
if (action === "on" || action === "off") {
|
|
61
|
+
const next = {
|
|
62
|
+
...settings,
|
|
63
|
+
enabled: action === "on",
|
|
64
|
+
...(relay ? { relayUrl: relay } : {}),
|
|
65
|
+
};
|
|
66
|
+
store.setSetting("remote", next);
|
|
67
|
+
if (values.json)
|
|
68
|
+
io.stdout.write(`${JSON.stringify(next)}\n`);
|
|
69
|
+
else
|
|
70
|
+
io.stdout.write(`Phone access is ${next.enabled ? "on" : "off"} (${next.relayUrl}).\n`);
|
|
71
|
+
// The running service picks the setting up within a few seconds.
|
|
72
|
+
if (!running)
|
|
73
|
+
io.stderr.write("yew: Yew is not running; start it with 'yew serve'.\n");
|
|
74
|
+
return EXIT_OK;
|
|
75
|
+
}
|
|
76
|
+
if (action === "status") {
|
|
77
|
+
const devices = store.listDevices();
|
|
78
|
+
if (values.json) {
|
|
79
|
+
io.stdout.write(`${JSON.stringify({ ...settings, running, devices })}\n`);
|
|
80
|
+
return EXIT_OK;
|
|
81
|
+
}
|
|
82
|
+
io.stdout.write(`Phone access: ${settings.enabled ? "on" : "off"}\nRelay: ${settings.relayUrl}\nYew service: ${running ? "running" : "not running"}\nPaired phones: ${devices.length}\n`);
|
|
83
|
+
for (const device of devices) {
|
|
84
|
+
const seen = device.lastSeenAt ? new Date(device.lastSeenAt).toISOString() : "never";
|
|
85
|
+
io.stdout.write(` ${device.name} (last seen ${seen})\n`);
|
|
86
|
+
}
|
|
87
|
+
return EXIT_OK;
|
|
88
|
+
}
|
|
89
|
+
if (!settings.enabled) {
|
|
90
|
+
io.stderr.write("yew: turn on phone access first: yew remote on\n");
|
|
91
|
+
return EXIT_ERROR;
|
|
92
|
+
}
|
|
93
|
+
const { systemKeys } = await import("../server/keys.js");
|
|
94
|
+
const { loadIdentity, createPairing } = await import("../remote/identity.js");
|
|
95
|
+
const { desktopName } = await import("../remote/access.js");
|
|
96
|
+
const identity = await loadIdentity(systemKeys("remote"));
|
|
97
|
+
const { link, expiresAt } = createPairing(store, identity, {
|
|
98
|
+
relay: settings.relayUrl,
|
|
99
|
+
name: desktopName(),
|
|
100
|
+
});
|
|
101
|
+
if (values.json) {
|
|
102
|
+
io.stdout.write(`${JSON.stringify({ link, expiresAt })}\n`);
|
|
103
|
+
return EXIT_OK;
|
|
104
|
+
}
|
|
105
|
+
const { renderUnicodeCompact } = await import("uqr");
|
|
106
|
+
io.stdout.write(`${renderUnicodeCompact(link)}\nScan this with Yew on your phone within 5 minutes.\n`);
|
|
107
|
+
if (!running)
|
|
108
|
+
io.stderr.write("yew: Yew is not running; start it with 'yew serve'.\n");
|
|
109
|
+
return EXIT_OK;
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
db.close();
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
package/dist/cli/run.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { NAME, PUBLISHER, VERSION, WEBSITE } from "../shared/version.js";
|
|
3
|
+
import { EXIT_ERROR, EXIT_OK, EXIT_USAGE, paint, UsageError } from "./io.js";
|
|
4
|
+
function helpText(io, commands) {
|
|
5
|
+
const bold = (s) => paint("bold", s, io.stdout, io.env);
|
|
6
|
+
const lines = [
|
|
7
|
+
`${NAME} ${VERSION}`,
|
|
8
|
+
"An AI-native multi-agent workbench.",
|
|
9
|
+
"",
|
|
10
|
+
`${bold("Usage:")} ${NAME} <command> [options]`,
|
|
11
|
+
];
|
|
12
|
+
if (commands.length > 0) {
|
|
13
|
+
lines.push("", bold("Commands:"));
|
|
14
|
+
const width = Math.max(...commands.map((c) => c.name.length));
|
|
15
|
+
for (const c of commands)
|
|
16
|
+
lines.push(` ${c.name.padEnd(width)} ${c.summary}`);
|
|
17
|
+
}
|
|
18
|
+
lines.push("", bold("Options:"), " -h, --help Show this help", " -v, --version Show the version, publisher and website", " --json With --version, print JSON", "", `Run '${NAME} <command> --help' for help on a command.`, "");
|
|
19
|
+
return lines.join("\n");
|
|
20
|
+
}
|
|
21
|
+
function versionText(json) {
|
|
22
|
+
if (json) {
|
|
23
|
+
return `${JSON.stringify({ name: NAME, version: VERSION, publisher: PUBLISHER, website: WEBSITE })}\n`;
|
|
24
|
+
}
|
|
25
|
+
return `${NAME} ${VERSION}\n${PUBLISHER} · ${WEBSITE}\n`;
|
|
26
|
+
}
|
|
27
|
+
export async function run(argv, io, commands = []) {
|
|
28
|
+
const first = argv[0];
|
|
29
|
+
const command = first && !first.startsWith("-") ? commands.find((c) => c.name === first) : undefined;
|
|
30
|
+
try {
|
|
31
|
+
if (command)
|
|
32
|
+
return await command.run(argv.slice(1), io);
|
|
33
|
+
if (first && !first.startsWith("-")) {
|
|
34
|
+
throw new UsageError(`unknown command '${first}'`);
|
|
35
|
+
}
|
|
36
|
+
const { values } = parseArgs({
|
|
37
|
+
args: argv,
|
|
38
|
+
options: {
|
|
39
|
+
help: { type: "boolean", short: "h" },
|
|
40
|
+
version: { type: "boolean", short: "v" },
|
|
41
|
+
json: { type: "boolean" },
|
|
42
|
+
},
|
|
43
|
+
strict: true,
|
|
44
|
+
allowPositionals: false,
|
|
45
|
+
});
|
|
46
|
+
if (values.version) {
|
|
47
|
+
io.stdout.write(versionText(values.json === true));
|
|
48
|
+
return EXIT_OK;
|
|
49
|
+
}
|
|
50
|
+
if (values.json)
|
|
51
|
+
throw new UsageError("--json needs --version or a command");
|
|
52
|
+
io.stdout.write(helpText(io, commands));
|
|
53
|
+
return EXIT_OK;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
return report(error, io);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function report(error, io) {
|
|
60
|
+
const prefix = paint("red", `${NAME}:`, io.stderr, io.env);
|
|
61
|
+
if (error instanceof UsageError || isParseArgsError(error)) {
|
|
62
|
+
io.stderr.write(`${prefix} ${error.message}\nRun '${NAME} --help' for usage.\n`);
|
|
63
|
+
return EXIT_USAGE;
|
|
64
|
+
}
|
|
65
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
66
|
+
io.stderr.write(`${prefix} ${message}\n`);
|
|
67
|
+
return EXIT_ERROR;
|
|
68
|
+
}
|
|
69
|
+
function isParseArgsError(error) {
|
|
70
|
+
const code = error?.code;
|
|
71
|
+
return typeof code === "string" && code.startsWith("ERR_PARSE_ARGS_");
|
|
72
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { EXIT_ERROR, EXIT_OK, UsageError } from "./io.js";
|
|
3
|
+
import { serviceRunning } from "./serve.js";
|
|
4
|
+
const HELP = `Usage: yew schedule <list|run> [options] [<task>]
|
|
5
|
+
|
|
6
|
+
Scheduled tasks send a standing session a letter at set times; the session's
|
|
7
|
+
last reply goes to the main conversation as the report. Set them up in Yew's
|
|
8
|
+
web interface, or ask the main conversation.
|
|
9
|
+
|
|
10
|
+
Commands:
|
|
11
|
+
list List the scheduled tasks with their next and last runs
|
|
12
|
+
run <task> Run a task now, by name or id; its next run stays as it was
|
|
13
|
+
|
|
14
|
+
Options:
|
|
15
|
+
--json Print as JSON
|
|
16
|
+
-h, --help Show this help
|
|
17
|
+
`;
|
|
18
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
19
|
+
function time(at) {
|
|
20
|
+
if (at === null)
|
|
21
|
+
return "-";
|
|
22
|
+
const date = new Date(at);
|
|
23
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
|
|
24
|
+
}
|
|
25
|
+
function lastRun(schedule) {
|
|
26
|
+
if (!schedule.lastStatus)
|
|
27
|
+
return "never run";
|
|
28
|
+
const detail = schedule.lastStatus === "skipped"
|
|
29
|
+
? ` (${{ busy: "the last run was not finished", archived: "the session is archived", missed: "missed" }[schedule.lastDetail] ?? schedule.lastDetail})`
|
|
30
|
+
: "";
|
|
31
|
+
return `${schedule.lastStatus}${detail} ${time(schedule.lastRunAt)}`;
|
|
32
|
+
}
|
|
33
|
+
export function scheduleCommand() {
|
|
34
|
+
return {
|
|
35
|
+
name: "schedule",
|
|
36
|
+
summary: "List scheduled tasks, or run one now",
|
|
37
|
+
async run(args, io) {
|
|
38
|
+
const { values, positionals } = parseArgs({
|
|
39
|
+
args,
|
|
40
|
+
options: { json: { type: "boolean" }, help: { type: "boolean", short: "h" } },
|
|
41
|
+
allowPositionals: true,
|
|
42
|
+
strict: true,
|
|
43
|
+
});
|
|
44
|
+
if (values.help) {
|
|
45
|
+
io.stdout.write(HELP);
|
|
46
|
+
return EXIT_OK;
|
|
47
|
+
}
|
|
48
|
+
const [action, ref] = positionals;
|
|
49
|
+
if (action === "list" ? positionals.length !== 1 : action !== "run" || !ref) {
|
|
50
|
+
throw new UsageError(action === "run" && !ref
|
|
51
|
+
? "yew schedule run needs a task's name or id"
|
|
52
|
+
: "yew schedule needs list or run");
|
|
53
|
+
}
|
|
54
|
+
if (positionals.length > 2)
|
|
55
|
+
throw new UsageError("too many arguments");
|
|
56
|
+
const { yewHome, dbPath } = await import("../server/paths.js");
|
|
57
|
+
const { openDatabase } = await import("../server/db/database.js");
|
|
58
|
+
const { EventBus } = await import("../server/events.js");
|
|
59
|
+
const { Store } = await import("../server/store.js");
|
|
60
|
+
const { PostOffice } = await import("../server/mail/post.js");
|
|
61
|
+
const { ScheduleBook, Scheduler } = await import("../server/schedules.js");
|
|
62
|
+
const home = yewHome(io.env);
|
|
63
|
+
const db = openDatabase(dbPath(home));
|
|
64
|
+
try {
|
|
65
|
+
const store = new Store(db, new EventBus());
|
|
66
|
+
const book = new ScheduleBook(store);
|
|
67
|
+
const sessionName = (id) => {
|
|
68
|
+
try {
|
|
69
|
+
return store.getSession(id).name;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return id;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
if (action === "list") {
|
|
76
|
+
const schedules = book.list();
|
|
77
|
+
if (values.json) {
|
|
78
|
+
const items = schedules.map((schedule) => ({
|
|
79
|
+
...schedule,
|
|
80
|
+
session: { id: schedule.sessionId, name: sessionName(schedule.sessionId) },
|
|
81
|
+
}));
|
|
82
|
+
io.stdout.write(`${JSON.stringify(items)}\n`);
|
|
83
|
+
return EXIT_OK;
|
|
84
|
+
}
|
|
85
|
+
if (schedules.length === 0) {
|
|
86
|
+
io.stdout.write("No scheduled tasks yet.\n");
|
|
87
|
+
return EXIT_OK;
|
|
88
|
+
}
|
|
89
|
+
for (const schedule of schedules) {
|
|
90
|
+
io.stdout.write(`${schedule.name} ${schedule.cron} to ${sessionName(schedule.sessionId)}${schedule.enabled ? "" : " (off)"}\n next: ${time(schedule.nextRunAt)} last: ${lastRun(schedule)}\n`);
|
|
91
|
+
}
|
|
92
|
+
return EXIT_OK;
|
|
93
|
+
}
|
|
94
|
+
const schedule = book.find(ref);
|
|
95
|
+
if (!schedule) {
|
|
96
|
+
io.stderr.write(`yew: there is no scheduled task with the name or id ${ref}\n`);
|
|
97
|
+
return EXIT_ERROR;
|
|
98
|
+
}
|
|
99
|
+
const scheduler = new Scheduler(store, new PostOffice(store), book, () => { });
|
|
100
|
+
const result = scheduler.run(schedule.id);
|
|
101
|
+
if ("skipped" in result) {
|
|
102
|
+
io.stderr.write(result.skipped === "busy"
|
|
103
|
+
? `yew: the last run of ${schedule.name} is not finished yet\n`
|
|
104
|
+
: `yew: ${sessionName(schedule.sessionId)} is archived\n`);
|
|
105
|
+
return EXIT_ERROR;
|
|
106
|
+
}
|
|
107
|
+
if (values.json) {
|
|
108
|
+
io.stdout.write(`${JSON.stringify({ schedule: book.get(schedule.id), letter: result.letter })}\n`);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
io.stdout.write(`Sent ${schedule.name} to ${sessionName(schedule.sessionId)} (letter ${result.letter.id}).\n`);
|
|
112
|
+
}
|
|
113
|
+
if (!serviceRunning(home)) {
|
|
114
|
+
io.stderr.write("Yew is not running; the letter is delivered when it starts.\n");
|
|
115
|
+
}
|
|
116
|
+
return EXIT_OK;
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
db.close();
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
package/dist/cli/send.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { EXIT_ERROR, EXIT_OK, UsageError } from "./io.js";
|
|
3
|
+
import { serviceRunning } from "./serve.js";
|
|
4
|
+
const HELP = `Usage: yew send [options] <session> <message>
|
|
5
|
+
|
|
6
|
+
Send a letter to a session, from a script, CI or a git hook. <session> is the
|
|
7
|
+
session's id or name. Use - as the message to read it from standard input; the
|
|
8
|
+
first line is the summary. The letter is marked as coming from the command
|
|
9
|
+
line, not from you; run inside a session's shell, it comes from that session
|
|
10
|
+
and counts against its letters. A running Yew delivers it within a few
|
|
11
|
+
seconds; otherwise it waits until Yew starts.
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
-k, --kind <kind> notice, request or report (default: notice)
|
|
15
|
+
--json Print the letter as JSON
|
|
16
|
+
-h, --help Show this help
|
|
17
|
+
`;
|
|
18
|
+
const KINDS = ["notice", "request", "report"];
|
|
19
|
+
export function sendCommand() {
|
|
20
|
+
return {
|
|
21
|
+
name: "send",
|
|
22
|
+
summary: "Send a letter to a session",
|
|
23
|
+
async run(args, io) {
|
|
24
|
+
const { values, positionals } = parseArgs({
|
|
25
|
+
args,
|
|
26
|
+
options: {
|
|
27
|
+
kind: { type: "string", short: "k" },
|
|
28
|
+
json: { type: "boolean" },
|
|
29
|
+
help: { type: "boolean", short: "h" },
|
|
30
|
+
},
|
|
31
|
+
allowPositionals: true,
|
|
32
|
+
strict: true,
|
|
33
|
+
});
|
|
34
|
+
if (values.help) {
|
|
35
|
+
io.stdout.write(HELP);
|
|
36
|
+
return EXIT_OK;
|
|
37
|
+
}
|
|
38
|
+
if (positionals.length !== 2) {
|
|
39
|
+
throw new UsageError("yew send needs a session and a message");
|
|
40
|
+
}
|
|
41
|
+
const kind = (values.kind ?? "notice");
|
|
42
|
+
if (!KINDS.includes(kind)) {
|
|
43
|
+
throw new UsageError("--kind must be notice, request or report");
|
|
44
|
+
}
|
|
45
|
+
const [target, raw] = positionals;
|
|
46
|
+
let message = raw;
|
|
47
|
+
if (raw === "-") {
|
|
48
|
+
message = "";
|
|
49
|
+
for await (const chunk of io.stdin)
|
|
50
|
+
message += chunk.toString();
|
|
51
|
+
}
|
|
52
|
+
message = message.trim();
|
|
53
|
+
if (!message)
|
|
54
|
+
throw new UsageError("the message is empty");
|
|
55
|
+
const { yewHome, dbPath } = await import("../server/paths.js");
|
|
56
|
+
const { openDatabase } = await import("../server/db/database.js");
|
|
57
|
+
const { EventBus } = await import("../server/events.js");
|
|
58
|
+
const { Store } = await import("../server/store.js");
|
|
59
|
+
const { PostOffice, SUMMARY_MAX, reasonText } = await import("../server/mail/post.js");
|
|
60
|
+
const { YewError } = await import("../server/errors.js");
|
|
61
|
+
const home = yewHome(io.env);
|
|
62
|
+
const db = openDatabase(dbPath(home));
|
|
63
|
+
try {
|
|
64
|
+
const store = new Store(db, new EventBus());
|
|
65
|
+
let session = store.findSessionByName(target);
|
|
66
|
+
if (!session) {
|
|
67
|
+
try {
|
|
68
|
+
session = store.getSession(target);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
io.stderr.write(`yew: there is no session with the name or id ${target}\n`);
|
|
72
|
+
return EXIT_ERROR;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Inside a session's shell the letter is that session's, with its quota, limits and hold.
|
|
76
|
+
const { fromJigEnv } = await import("../server/from-jig.js");
|
|
77
|
+
const fromId = fromJigEnv(io.env, "SESSION")?.trim();
|
|
78
|
+
let sender = { kind: "cli" };
|
|
79
|
+
if (fromId) {
|
|
80
|
+
try {
|
|
81
|
+
sender = { kind: "session", session: store.getSession(fromId) };
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
io.stderr.write(`yew: YEW_SESSION names no session (${fromId})\n`);
|
|
85
|
+
return EXIT_ERROR;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const [first = "", ...rest] = message.split("\n");
|
|
89
|
+
const line = first.trim();
|
|
90
|
+
const summary = [...line].length > SUMMARY_MAX
|
|
91
|
+
? `${[...line].slice(0, SUMMARY_MAX - 1).join("")}…`
|
|
92
|
+
: line;
|
|
93
|
+
const body = summary === line ? rest.join("\n").trim() : message;
|
|
94
|
+
let letter;
|
|
95
|
+
try {
|
|
96
|
+
letter = new PostOffice(store).send(sender, { to: session.id, kind, summary, body });
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (!(error instanceof YewError))
|
|
100
|
+
throw error;
|
|
101
|
+
io.stderr.write(`yew: ${error.message}\n`);
|
|
102
|
+
return EXIT_ERROR;
|
|
103
|
+
}
|
|
104
|
+
if (letter.status === "refused" || letter.status === "dropped") {
|
|
105
|
+
io.stderr.write(`yew: ${session.name} did not take the letter: ${reasonText(letter.statusReason)}\n`);
|
|
106
|
+
return EXIT_ERROR;
|
|
107
|
+
}
|
|
108
|
+
if (values.json) {
|
|
109
|
+
io.stdout.write(`${JSON.stringify({ id: letter.id, to: { id: session.id, name: session.name }, kind, status: letter.status })}\n`);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
io.stdout.write(`Sent letter ${letter.id} to ${session.name}.\n`);
|
|
113
|
+
}
|
|
114
|
+
if (letter.status === "held") {
|
|
115
|
+
io.stderr.write("It is held until you release it in Yew's approval center.\n");
|
|
116
|
+
}
|
|
117
|
+
else if (!serviceRunning(home)) {
|
|
118
|
+
io.stderr.write("Yew is not running; the letter is delivered when it starts.\n");
|
|
119
|
+
}
|
|
120
|
+
return EXIT_OK;
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
db.close();
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|