@conveno/cli 0.2.0-rc.4 → 0.2.0-rc.6

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.
@@ -2,7 +2,7 @@ import { randomBytes as randomBytes$1 } from "@noble/ciphers/utils.js";
2
2
  import { equalBytes } from "@noble/curves/utils.js";
3
3
  import { sha256 } from "@noble/hashes/sha2.js";
4
4
  import { bytesToHex, concatBytes, utf8ToBytes } from "@noble/hashes/utils.js";
5
- import { $n as string, Kn as _enum, Qn as object, Xn as literal, Yn as discriminatedUnion, Zn as number, cn as DeviceId, qn as array } from "./dist-DUqXHTRg.js";
5
+ import { An as string, Cn as _enum, Dn as literal, En as discriminatedUnion, On as number, Yt as DeviceId, kn as object, wn as array } from "./dist-BTLBO1Rp.js";
6
6
  import { ed25519, x25519 } from "@noble/curves/ed25519.js";
7
7
  import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
8
8
  import { expand, extract } from "@noble/hashes/hkdf.js";
@@ -0,0 +1,449 @@
1
+ import { join } from "node:path";
2
+ import { $ as loadJson, I as dropFromList, L as editJson, Mt as sameJson, V as existedBefore, X as isObject, at as parseCliVersion, j as dataDir, lt as pendingHookContext, rt as ownSession, z as endTurnContinuation } from "./dist-BTLBO1Rp.js";
3
+ import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
4
+ import { homedir } from "node:os";
5
+ // ../adapters/antigravity/dist/index.js
6
+ /**
7
+ * Antigravity tells a hook its `conversationId`; the sidecar that wakes an idle conversation
8
+ * (`conveno sidecar antigravity`) has no hook and no host pid, only Conveno sessions. The hook
9
+ * writes one small file per session, `<data dir>/antigravity/conversations/<session id>.json`,
10
+ * and the sidecar reads it.
11
+ */
12
+ const conversationsDir = (base = dataDir(process.platform, process.env, homedir())) => join(base, "antigravity", "conversations");
13
+ const fileOf = (base, sessionId) => join(conversationsDir(base), `${sessionId}.json`);
14
+ const SAFE = /^[A-Za-z0-9]{1,64}$/;
15
+ const UUID = /^[0-9a-fA-F-]{8,64}$/;
16
+ /** Remember which conversation a session is. Writes only when the answer changed. Never throws. */
17
+ async function recordConversation(sessionId, conversationId, base) {
18
+ if (!SAFE.test(sessionId) || !UUID.test(conversationId)) return;
19
+ try {
20
+ const dataBase = base ?? dataDir(process.platform, process.env, homedir());
21
+ if (await conversationOf(sessionId, dataBase) === conversationId) return;
22
+ await mkdir(conversationsDir(dataBase), { recursive: true });
23
+ const path = fileOf(dataBase, sessionId);
24
+ const tmp = `${path}.${process.pid}.tmp`;
25
+ await writeFile(tmp, `${JSON.stringify({ conversationId })}\n`, "utf8");
26
+ await rename(tmp, path);
27
+ } catch {}
28
+ }
29
+ /** The conversation a session was last seen in, or null. */
30
+ async function conversationOf(sessionId, base) {
31
+ if (!SAFE.test(sessionId)) return null;
32
+ try {
33
+ const dataBase = base ?? dataDir(process.platform, process.env, homedir());
34
+ const raw = JSON.parse(await readFile(fileOf(dataBase, sessionId), "utf8"));
35
+ return typeof raw.conversationId === "string" && UUID.test(raw.conversationId) ? raw.conversationId : null;
36
+ } catch {
37
+ return null;
38
+ }
39
+ }
40
+ const SERVER_NAME = "conveno";
41
+ /** The name of our entry in hooks.json. Antigravity keys hooks by an arbitrary name. */
42
+ const HOOK_NAME = "conveno";
43
+ /**
44
+ * SessionStart fires when a conversation starts; PreInvocation before every model call; Stop when
45
+ * the agent loop is about to stop, where a waiting message continues it.
46
+ */
47
+ const HOOK_EVENTS = [
48
+ "SessionStart",
49
+ "PreInvocation",
50
+ "Stop"
51
+ ];
52
+ /**
53
+ * Antigravity runs a sidecar (a background process it starts, restarts and stops with itself) from
54
+ * `~/.gemini/config/sidecars/<id>/sidecar.json`, but only when `~/.gemini/config/config.json`
55
+ * enables the id. Ours wakes idle conversations: `conveno sidecar antigravity`.
56
+ */
57
+ const SIDECAR_ID = "conveno";
58
+ /** The permission rule that lets the agent call our tools without asking. */
59
+ const PERMISSION_RULE = `mcp(${SERVER_NAME}/*)`;
60
+ const EMPTY = {
61
+ stdout: "",
62
+ exitCode: 0
63
+ };
64
+ /** Everything Antigravity CLI keeps for the user lives under `~/.gemini`. */
65
+ function paths(env) {
66
+ const root = join(env.home, ".gemini");
67
+ return {
68
+ sidecar: join(root, "config", "sidecars", SIDECAR_ID, "sidecar.json"),
69
+ config: join(root, "config", "config.json"),
70
+ mcp: join(root, "config", "mcp_config.json"),
71
+ settings: join(root, "antigravity-cli", "settings.json"),
72
+ hooks: join(root, "config", "hooks.json")
73
+ };
74
+ }
75
+ /**
76
+ * Quote one word of a hook command. Antigravity's docs do not say which shell runs a hook, so the
77
+ * word is written to mean the same in bash, cmd.exe and POSIX sh: double quotes only when the word
78
+ * needs them, and on Windows forward slashes, which every Windows API Node uses accepts, so no
79
+ * backslash has to be escaped.
80
+ */
81
+ function quoteHookWord(arg, platform) {
82
+ const word = platform === "win32" ? arg.replace(/\\/g, "/") : arg;
83
+ return /^[A-Za-z0-9_@%+=:,./\\-]+$/.test(word) ? word : `"${word.replace(/(["\\$`])/g, "\\$1")}"`;
84
+ }
85
+ const hookCommand = (env, event) => [
86
+ ...env.conveno.command,
87
+ "hook",
88
+ "antigravity",
89
+ event
90
+ ].map((w) => quoteHookWord(w, env.platform)).join(" ");
91
+ const mcpArgs = (env) => [
92
+ ...env.conveno.command,
93
+ "mcp",
94
+ "--cli",
95
+ "antigravity"
96
+ ];
97
+ /** The sidecar manifest. Antigravity starts it from the sidecar's folder with its own environment. */
98
+ const desiredSidecar = (env) => {
99
+ const [command = "conveno", ...args] = [
100
+ ...env.conveno.command,
101
+ "sidecar",
102
+ "antigravity"
103
+ ];
104
+ return {
105
+ display_name: "Conveno",
106
+ description: "Wakes idle Antigravity conversations when another session sends them a message.",
107
+ command,
108
+ args,
109
+ restart_policy: "always"
110
+ };
111
+ };
112
+ /** The `sidecars` object of config.json, or an empty one. */
113
+ function sidecarsOf(config, path) {
114
+ const s = config.sidecars;
115
+ if (s === void 0) return {};
116
+ if (!isObject(s)) throw new Error(`"sidecars" in ${path} must be an object. Fix the file, then run the install again. Nothing was changed.`);
117
+ return s;
118
+ }
119
+ const desiredServer = (env) => {
120
+ const [command = "conveno", ...args] = mcpArgs(env);
121
+ return {
122
+ command,
123
+ args
124
+ };
125
+ };
126
+ const desiredHandlers = (env, event) => [{
127
+ type: "command",
128
+ command: hookCommand(env, event),
129
+ timeout: 5
130
+ }];
131
+ /** Recognizes our hook command even if the conveno path changed since install. */
132
+ const isOurHookCommand = (cmd) => typeof cmd === "string" && /(^|\s)hook antigravity (SessionStart|PreInvocation|Stop)$/.test(cmd.trim());
133
+ const listOf = (names) => `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`;
134
+ const handlersOf = (entry, event) => {
135
+ const h = entry[event];
136
+ return Array.isArray(h) ? h : [];
137
+ };
138
+ const hasOurHandler = (entry) => HOOK_EVENTS.some((e) => handlersOf(entry, e).some((h) => isObject(h) && isOurHookCommand(h.command)));
139
+ function mcpServers(config, path) {
140
+ const s = config.mcpServers;
141
+ if (s === void 0) return {};
142
+ if (!isObject(s)) throw new Error(`"mcpServers" in ${path} must be an object. Fix the file, then run the install again. Nothing was changed.`);
143
+ return s;
144
+ }
145
+ /** The `permissions.allow` list, or undefined when there is none. */
146
+ function allowList(settings, path) {
147
+ const p = settings.permissions;
148
+ if (p === void 0) return void 0;
149
+ if (!isObject(p)) throw new Error(`"permissions" in ${path} must be an object. Fix the file, then run the install again. Nothing was changed.`);
150
+ const a = p.allow;
151
+ if (a === void 0) return void 0;
152
+ if (!Array.isArray(a)) throw new Error(`"permissions.allow" in ${path} must be a list. Fix the file, then run the install again. Nothing was changed.`);
153
+ return a;
154
+ }
155
+ /** Our entry in hooks.json, or undefined. A hook of that name that is not ours is the user's: refuse. */
156
+ function ourHookEntry(hooksFile, path) {
157
+ const e = hooksFile[HOOK_NAME];
158
+ if (e === void 0) return void 0;
159
+ if (!isObject(e) || !hasOurHandler(e)) throw new Error(`${path} already has a hook named "${HOOK_NAME}" that is not Conveno's. Rename it, then run the install again. Nothing was changed.`);
160
+ return e;
161
+ }
162
+ async function detect(env) {
163
+ const p = paths(env);
164
+ const configPaths = [
165
+ p.mcp,
166
+ p.settings,
167
+ p.hooks
168
+ ];
169
+ try {
170
+ const r = await env.exec("agy", ["--version"]);
171
+ if (r.code !== 0) return {
172
+ installed: false,
173
+ configPaths
174
+ };
175
+ const version = parseCliVersion(r.stdout);
176
+ return {
177
+ installed: true,
178
+ binary: "agy",
179
+ ...version ? { version } : {},
180
+ configPaths
181
+ };
182
+ } catch {
183
+ return {
184
+ installed: false,
185
+ configPaths
186
+ };
187
+ }
188
+ }
189
+ async function plan(env) {
190
+ const p = paths(env);
191
+ const changes = [];
192
+ const mcpFile = await loadJson(p.mcp);
193
+ const servers = mcpServers(mcpFile.value, p.mcp);
194
+ const wantServer = desiredServer(env);
195
+ if (!sameJson(servers["conveno"], wantServer)) changes.push({
196
+ component: "mcp",
197
+ kind: servers["conveno"] === void 0 ? "add" : "update",
198
+ parts: [{
199
+ name: "MCP server",
200
+ kind: servers["conveno"] === void 0 ? "add" : "update"
201
+ }],
202
+ path: p.mcp,
203
+ before: mcpFile.raw,
204
+ after: editJson(mcpFile, [{
205
+ path: ["mcpServers", SERVER_NAME],
206
+ value: wantServer
207
+ }]),
208
+ description: `Register the "${SERVER_NAME}" MCP server (conveno mcp --cli antigravity).`
209
+ });
210
+ const setFile = await loadJson(p.settings);
211
+ const allow = allowList(setFile.value, p.settings);
212
+ if (!allow?.includes("mcp(conveno/*)")) {
213
+ const edit = setFile.value.permissions === void 0 ? {
214
+ path: ["permissions"],
215
+ value: { allow: [PERMISSION_RULE] }
216
+ } : allow === void 0 ? {
217
+ path: ["permissions", "allow"],
218
+ value: [PERMISSION_RULE]
219
+ } : {
220
+ path: [
221
+ "permissions",
222
+ "allow",
223
+ allow.length
224
+ ],
225
+ value: PERMISSION_RULE,
226
+ insert: true
227
+ };
228
+ changes.push({
229
+ component: "mcp",
230
+ kind: "add",
231
+ parts: [{
232
+ name: "tools permission",
233
+ kind: "add"
234
+ }],
235
+ path: p.settings,
236
+ before: setFile.raw,
237
+ after: editJson(setFile, [edit]),
238
+ description: `Allow the agent to call Conveno's tools without asking (${PERMISSION_RULE}). They only exchange messages between your own sessions.`
239
+ });
240
+ }
241
+ const hooksFile = await loadJson(p.hooks);
242
+ const entry = ourHookEntry(hooksFile.value, p.hooks);
243
+ const edits = [];
244
+ const parts = [];
245
+ for (const e of HOOK_EVENTS) {
246
+ const want = desiredHandlers(env, e);
247
+ const have = entry ? handlersOf(entry, e) : void 0;
248
+ if (!sameJson(have, want)) {
249
+ edits.push({
250
+ path: [HOOK_NAME, e],
251
+ value: want
252
+ });
253
+ parts.push({
254
+ name: `${e} hook`,
255
+ kind: have === void 0 ? "add" : "update"
256
+ });
257
+ }
258
+ }
259
+ if (edits.length > 0) changes.push({
260
+ component: "hooks",
261
+ kind: entry ? "update" : "add",
262
+ parts,
263
+ path: p.hooks,
264
+ before: hooksFile.raw,
265
+ after: editJson(hooksFile, edits),
266
+ description: `Add ${listOf(HOOK_EVENTS)} hooks that deliver Conveno messages to Antigravity on the next prompt and at the end of a turn.`
267
+ });
268
+ const sidecarFile = await loadJson(p.sidecar);
269
+ const wantSidecar = desiredSidecar(env);
270
+ if (!sameJson(sidecarFile.value, wantSidecar)) {
271
+ const had = sidecarFile.raw !== null;
272
+ changes.push({
273
+ component: "hooks",
274
+ kind: had ? "update" : "add",
275
+ parts: [{
276
+ name: "wake sidecar",
277
+ kind: had ? "update" : "add"
278
+ }],
279
+ path: p.sidecar,
280
+ before: sidecarFile.raw,
281
+ after: `${JSON.stringify(wantSidecar, null, 2)}\n`,
282
+ description: `Add the Conveno sidecar, which wakes an idle Antigravity conversation when a message arrives (conveno sidecar antigravity).`
283
+ });
284
+ }
285
+ const cfgFile = await loadJson(p.config);
286
+ const ours = sidecarsOf(cfgFile.value, p.config)[SIDECAR_ID];
287
+ if (!isObject(ours) || ours.enabled !== true) {
288
+ const edit = cfgFile.value.sidecars === void 0 ? {
289
+ path: ["sidecars"],
290
+ value: { [SIDECAR_ID]: { enabled: true } }
291
+ } : isObject(ours) ? {
292
+ path: [
293
+ "sidecars",
294
+ SIDECAR_ID,
295
+ "enabled"
296
+ ],
297
+ value: true
298
+ } : {
299
+ path: ["sidecars", SIDECAR_ID],
300
+ value: { enabled: true }
301
+ };
302
+ changes.push({
303
+ component: "hooks",
304
+ kind: ours === void 0 ? "add" : "update",
305
+ parts: [{
306
+ name: "enable wake sidecar",
307
+ kind: ours === void 0 ? "add" : "update"
308
+ }],
309
+ path: p.config,
310
+ before: cfgFile.raw,
311
+ after: editJson(cfgFile, [edit]),
312
+ description: `Turn on the Conveno sidecar in Antigravity's config (sidecars.${SIDECAR_ID}.enabled).`
313
+ });
314
+ }
315
+ return changes;
316
+ }
317
+ async function uninstallPlan(env, baseline) {
318
+ const p = paths(env);
319
+ const changes = [];
320
+ const mcpFile = await loadJson(p.mcp);
321
+ if ("conveno" in mcpServers(mcpFile.value, p.mcp)) changes.push({
322
+ component: "mcp",
323
+ kind: "remove",
324
+ path: p.mcp,
325
+ before: mcpFile.raw,
326
+ after: editJson(mcpFile, [{
327
+ path: ["mcpServers", SERVER_NAME],
328
+ value: void 0
329
+ }]),
330
+ description: `Remove the "${SERVER_NAME}" MCP server.`
331
+ });
332
+ const setFile = await loadJson(p.settings);
333
+ const drops = dropFromList(allowList(setFile.value, p.settings), ["permissions", "allow"], (r) => r === PERMISSION_RULE, { keepEmpty: existedBefore(baseline?.get(p.settings), ["permissions", "allow"]) });
334
+ if (drops.length > 0) {
335
+ const alone = drops.length === 1 && drops[0]?.path.length === 2;
336
+ const only = Object.keys(setFile.value.permissions).length === 1 && !existedBefore(baseline?.get(p.settings), ["permissions"]);
337
+ changes.push({
338
+ component: "mcp",
339
+ kind: "remove",
340
+ path: p.settings,
341
+ before: setFile.raw,
342
+ after: editJson(setFile, alone && only ? [{
343
+ path: ["permissions"],
344
+ value: void 0
345
+ }] : drops),
346
+ description: `Remove the ${PERMISSION_RULE} permission.`
347
+ });
348
+ }
349
+ const hooksFile = await loadJson(p.hooks);
350
+ if (ourHookEntry(hooksFile.value, p.hooks)) changes.push({
351
+ component: "hooks",
352
+ kind: "remove",
353
+ path: p.hooks,
354
+ before: hooksFile.raw,
355
+ after: editJson(hooksFile, [{
356
+ path: [HOOK_NAME],
357
+ value: void 0
358
+ }]),
359
+ description: `Remove the Conveno ${listOf(HOOK_EVENTS)} hooks.`
360
+ });
361
+ const sidecarFile = await loadJson(p.sidecar);
362
+ if (sidecarFile.raw !== null) changes.push({
363
+ component: "hooks",
364
+ kind: "remove",
365
+ path: p.sidecar,
366
+ before: sidecarFile.raw,
367
+ after: "",
368
+ deleteFile: true,
369
+ description: "Remove the Conveno wake sidecar."
370
+ });
371
+ const cfgFile = await loadJson(p.config);
372
+ const sidecars = sidecarsOf(cfgFile.value, p.config);
373
+ if ("conveno" in sidecars) {
374
+ const onlyOurs = Object.keys(sidecars).length === 1 && !existedBefore(baseline?.get(p.config), ["sidecars"]);
375
+ changes.push({
376
+ component: "hooks",
377
+ kind: "remove",
378
+ path: p.config,
379
+ before: cfgFile.raw,
380
+ after: editJson(cfgFile, [{
381
+ path: onlyOurs ? ["sidecars"] : ["sidecars", SIDECAR_ID],
382
+ value: void 0
383
+ }]),
384
+ description: "Turn off the Conveno sidecar in Antigravity's config."
385
+ });
386
+ }
387
+ return changes;
388
+ }
389
+ /** Tell the wake sidecar which conversation this session is. Never throws. */
390
+ async function noteConversation(input, ctx) {
391
+ try {
392
+ const id = isObject(input) ? input.conversationId : void 0;
393
+ if (typeof id !== "string") return;
394
+ const session = await ownSession(ctx);
395
+ if (session) await recordConversation(session.id, id);
396
+ } catch {}
397
+ }
398
+ const isHookEvent = (e) => HOOK_EVENTS.includes(e);
399
+ /**
400
+ * - SessionStart, PreInvocation: both accept `injectSteps`. An injected system message stays in the
401
+ * conversation, so a step is added only when a message is waiting.
402
+ * - Stop: records the turn end. With messages waiting, after a turn that ended on its
403
+ * own (`terminationReason` `model_stop`, no `error`), `{"decision":"continue","reason":"<text>"}`
404
+ * re-enters the loop and `reason` becomes a system message. The input has no loop-guard flag and
405
+ * Antigravity has no idle wake, so a message that arrives during a continued turn is delivered
406
+ * at its end; Conveno's auto-mode limit bounds the chain. While background tasks still run
407
+ * (`fullyIdle` false) the agent is not done, so nothing is recorded or delivered yet: another
408
+ * Stop follows when they finish.
409
+ * Never throws and never writes to stderr: any failure means "deliver nothing this time".
410
+ */
411
+ async function hook(event, input, ctx) {
412
+ if (!isHookEvent(event)) return EMPTY;
413
+ await noteConversation(input, ctx);
414
+ if (event === "Stop") {
415
+ const stop = isObject(input) ? input : {};
416
+ if (stop.fullyIdle === false) return EMPTY;
417
+ const completed = (stop.terminationReason === void 0 || stop.terminationReason === "model_stop") && (stop.error === void 0 || stop.error === "");
418
+ const reason = await endTurnContinuation(ctx, { allowContinue: completed });
419
+ if (reason === null) return EMPTY;
420
+ return {
421
+ stdout: JSON.stringify({
422
+ decision: "continue",
423
+ reason
424
+ }),
425
+ exitCode: 0
426
+ };
427
+ }
428
+ const text = await pendingHookContext(ctx);
429
+ if (text === null) return EMPTY;
430
+ return {
431
+ stdout: JSON.stringify({ injectSteps: [{ systemMessage: { systemMessage: text } }] }),
432
+ exitCode: 0
433
+ };
434
+ }
435
+ const antigravityAdapter = {
436
+ cli: "antigravity",
437
+ displayName: "Antigravity CLI",
438
+ deliveryTier: "next_turn",
439
+ delivery: () => ({
440
+ nextTurn: true,
441
+ endOfTurn: true,
442
+ idleWake: true
443
+ }),
444
+ detect,
445
+ plan,
446
+ uninstallPlan,
447
+ hook
448
+ };
449
+ export { conversationOf as n, antigravityAdapter as t };