@conveno/cli 0.1.1 → 0.2.0-rc.2

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.
@@ -0,0 +1,105 @@
1
+ import { nt as isPermissionHook } from "./dist-_sq6DmrE.js";
2
+ // src/ctx.ts
3
+ const adapterEnv = (ctx, opts = {}) => ({
4
+ home: ctx.home,
5
+ platform: ctx.platform,
6
+ cwd: ctx.cwd,
7
+ exec: ctx.exec,
8
+ conveno: ctx.conveno,
9
+ env: ctx.env,
10
+ ...opts.permissionRelay ? { permissionRelay: true } : {}
11
+ });
12
+ const connectOpts = (ctx, autoStart) => ({
13
+ kind: "cli",
14
+ dataDir: ctx.dataDir,
15
+ autoStart,
16
+ ...ctx.socketPath ? { socketPath: ctx.socketPath } : {},
17
+ ...ctx.daemonEntry ? { daemonEntry: ctx.daemonEntry } : {}
18
+ });
19
+ // src/hook.ts
20
+ /**
21
+ * Time allowed for the hook's own process-table query. Only the fallback runs it: the usual way to
22
+ * find the host CLI is to ask the daemon (`session.hostOf`). When the query fails or runs out, the
23
+ * hook falls back to its direct parent as the host.
24
+ */
25
+ const HOOK_ANCESTRY_TIMEOUT_MS = 1200;
26
+ const NOTHING = {
27
+ stdout: "",
28
+ exitCode: 0
29
+ };
30
+ /**
31
+ * The pid of the CLI that runs this hook, or undefined when there is nothing to deliver to.
32
+ * The daemon walks the process chain (it keeps a warm query helper on Windows) and answers at once
33
+ * when no session of this CLI is online, so the hook reads no process table in either case. When
34
+ * the daemon cannot answer (an older daemon, a table it may not read), the hook reads the table
35
+ * itself, as it always did, and `complete: false` keeps the direct-parent fallback.
36
+ */
37
+ async function findHostPid(ctx, client, cli, local) {
38
+ if (ctx.hostLookup) {
39
+ const { ppid, launcherPaths } = ctx.hostLookup;
40
+ const r = await client["session.hostOf"]({
41
+ cli,
42
+ ppid,
43
+ launcherPaths
44
+ }).catch(() => void 0);
45
+ if (r && !r.online) return void 0;
46
+ if (r?.complete) return r.hostPid;
47
+ }
48
+ return (await local()).hostPid;
49
+ }
50
+ /**
51
+ * `conveno hook <cli> <event>`: reads the CLI's hook JSON on stdin and prints what the adapter
52
+ * returns. Never starts a daemon. A hook that fails or stalls must not hold the CLI up, so an
53
+ * ordinary hook gives up after the context's timeout (2 s), never writes to stderr and exits 0.
54
+ *
55
+ * Listener events (the adapter's `listenEvents`) are the exception: they run in the background,
56
+ * wait without the 2 s budget until a message arrives or their CLI goes away, and may exit 2 with
57
+ * the message on stderr (Claude Code's `asyncRewake`). They stop on their own when the host CLI
58
+ * exits, the session goes offline, or a newer listener replaces them.
59
+ */
60
+ async function runHook(ctx, argv) {
61
+ const [cli, event] = argv;
62
+ const adapter = ctx.adapters.find((a) => a.cli === cli);
63
+ if (!adapter || !event) return 0;
64
+ const listener = adapter.listenEvents?.includes(event) === true;
65
+ let client;
66
+ let timer;
67
+ const early = ctx.hostLookup ? void 0 : ctx.ancestry(HOOK_ANCESTRY_TIMEOUT_MS);
68
+ const work = (async () => {
69
+ const readInput = async () => {
70
+ try {
71
+ const raw = await ctx.readStdin();
72
+ return raw.trim() === "" ? {} : JSON.parse(raw);
73
+ } catch {
74
+ return {};
75
+ }
76
+ };
77
+ const [input, connected] = await Promise.all([readInput(), ctx.connect(connectOpts(ctx, false))]);
78
+ client = connected;
79
+ const hostPid = await findHostPid(ctx, connected, adapter.cli, () => early ?? ctx.ancestry(1200));
80
+ if (hostPid === void 0) return NOTHING;
81
+ return adapter.hook(event, input, {
82
+ client,
83
+ hostPid,
84
+ cli: adapter.cli,
85
+ isAlive: ctx.isAlive
86
+ });
87
+ })();
88
+ const budget = isPermissionHook(cli, event) ? ctx.permissionHookTimeoutMs ?? 9e4 : ctx.hookTimeoutMs;
89
+ const timeout = listener ? new Promise(() => void 0) : new Promise((resolve) => {
90
+ timer = setTimeout(() => resolve(NOTHING), budget);
91
+ });
92
+ let code = 0;
93
+ try {
94
+ const out = await Promise.race([work, timeout]);
95
+ if (out.stdout) ctx.out(out.stdout.endsWith("\n") ? out.stdout : `${out.stdout}\n`);
96
+ if (listener && out.stderr) ctx.err(out.stderr.endsWith("\n") ? out.stderr : `${out.stderr}\n`);
97
+ if (listener) code = out.exitCode;
98
+ } catch {} finally {
99
+ if (timer) clearTimeout(timer);
100
+ work.catch(() => void 0);
101
+ await Promise.race([client?.close().catch(() => void 0), new Promise((r) => setTimeout(r, 100))]);
102
+ }
103
+ return code;
104
+ }
105
+ export { adapterEnv as n, connectOpts as r, runHook as t };