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

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.
@@ -1,8 +1,3 @@
1
- import { execFile } from "node:child_process";
2
- import { readFileSync, statSync } from "node:fs";
3
- import { readFile } from "node:fs/promises";
4
- import { join } from "node:path";
5
- import { It as startTurnContext, U as findOnPath, at as parseCliVersion, dt as planProgram, lt as pendingHookContext, rt as ownSession, z as endTurnContinuation } from "./dist-BTLBO1Rp.js";
6
1
  // ../../node_modules/.pnpm/@decimalturn+toml-patch@3.3.3/node_modules/@decimalturn/toml-patch/dist/toml-patch.js
7
2
  //! @decimalturn/toml-patch v3.3.3 - https://github.com/DecimalTurn/toml-patch - @license: MIT
8
3
  function e(e) {
@@ -3098,7 +3093,7 @@ function wi(e, t) {
3098
3093
  };
3099
3094
  n(e);
3100
3095
  }
3101
- function q$1(e, t = R.default(), n = 0, r = !1) {
3096
+ function q(e, t = R.default(), n = 0, r = !1) {
3102
3097
  e = Ai(e);
3103
3098
  let i = Nr();
3104
3099
  $n(i), Zn(i, t.indentWidth);
@@ -4775,7 +4770,7 @@ function eo(e) {
4775
4770
  }
4776
4771
  function to(e, t, n, r) {
4777
4772
  if (!F(t)) return t;
4778
- let i = Vi(q$1(e, L({
4773
+ let i = Vi(q(e, L({
4779
4774
  ...n,
4780
4775
  inlineTableStart: 0
4781
4776
  }, n)).items, ``, { temporal: r });
@@ -4831,7 +4826,7 @@ function ro(e, t, n, r = !1, i = !0, a = !1) {
4831
4826
  let f = n.inlineTableStart != null && n.inlineTableStart > 1 ? 1 : n.inlineTableStart, p = L({
4832
4827
  ...n,
4833
4828
  inlineTableStart: f
4834
- }, n), m = q$1(t, p), h = Vi(m.items, ``, { temporal: s }), g = n.updateOrder ? to(t, h, p, s) : h;
4829
+ }, n), m = q(t, p), h = Vi(m.items, ``, { temporal: s }), g = n.updateOrder ? to(t, h, p, s) : h;
4835
4830
  if (!n.updateOrder && io(u, g)) return {
4836
4831
  tomlString: Mi(o, n),
4837
4832
  document: d
@@ -5305,7 +5300,7 @@ function So(n, i, s, c, l = !1, g = /* @__PURE__ */ new WeakSet(), _ = void 0, v
5305
5300
  trailingComma: y ? c.trailingComma : h,
5306
5301
  bracketSpacing: b ?? c.bracketSpacing,
5307
5302
  indentWidth: l
5308
- }, c), w = q$1({ tmp: i }, C, o + 1, s).items[0];
5303
+ }, c), w = q({ tmp: i }, C, o + 1, s).items[0];
5309
5304
  if (!w || !a(w)) return r;
5310
5305
  let T = w.value;
5311
5306
  if (x && (u(T) || f(T))) {
@@ -5332,7 +5327,7 @@ function So(n, i, s, c, l = !1, g = /* @__PURE__ */ new WeakSet(), _ = void 0, v
5332
5327
  inlineTableStart: 0,
5333
5328
  [C]: !0,
5334
5329
  trailingComma: y ? c.trailingComma : !1
5335
- }, c), T = q$1({ tmp: o }, w, l + 1, p).items[0];
5330
+ }, c), T = q({ tmp: o }, w, l + 1, p).items[0];
5336
5331
  if (!T || !a(T)) return t;
5337
5332
  let E = T.value;
5338
5333
  if (u(E) || f(E)) {
@@ -5422,7 +5417,7 @@ function So(n, i, s, c, l = !1, g = /* @__PURE__ */ new WeakSet(), _ = void 0, v
5422
5417
  let r = L({
5423
5418
  ...c,
5424
5419
  inlineTableStart: 0
5425
- }, c), i = e === void 0 ? void 0 : q$1({ tmp: e }, r), l = i == null ? void 0 : i.items[0];
5420
+ }, c), i = e === void 0 ? void 0 : q({ tmp: e }, r), l = i == null ? void 0 : i.items[0];
5426
5421
  if (l && a(l) && (o = K(l.value), _ = !1, d(o) && f(o.item) && o.item.items.length > 0)) {
5427
5422
  let e = o.item;
5428
5423
  if (!(e.items[0].loc.start.column - e.loc.start.column > 1)) {
@@ -5445,7 +5440,7 @@ function So(n, i, s, c, l = !1, g = /* @__PURE__ */ new WeakSet(), _ = void 0, v
5445
5440
  let e = Ir(x, c.escapeSequenceUpperCase), n = O ? L({
5446
5441
  ...c,
5447
5442
  inlineTableStart: 0
5448
- }, c) : c, i = q$1(T, n);
5443
+ }, c) : c, i = q(T, n);
5449
5444
  for (let t of i.items) H(e, e, t, void 0);
5450
5445
  if (w && S) {
5451
5446
  let n = (i, o) => {
@@ -5544,7 +5539,7 @@ function So(n, i, s, c, l = !1, g = /* @__PURE__ */ new WeakSet(), _ = void 0, v
5544
5539
  ...c,
5545
5540
  inlineTableStart: 0,
5546
5541
  trailingComma: e
5547
- }, c), i = t === void 0 ? void 0 : q$1({ tmp: t }, r).items[0];
5542
+ }, c), i = t === void 0 ? void 0 : q({ tmp: t }, r).items[0];
5548
5543
  if (i && a(i)) {
5549
5544
  let e = K(i.value);
5550
5545
  e.comma = n.comma, o = e;
@@ -5640,7 +5635,7 @@ function So(n, i, s, c, l = !1, g = /* @__PURE__ */ new WeakSet(), _ = void 0, v
5640
5635
  let t = L({
5641
5636
  ...c,
5642
5637
  inlineTableStart: 0
5643
- }, c), n = q$1({ tmp: e }, t), r = n == null ? void 0 : n.items[0];
5638
+ }, c), n = q({ tmp: e }, t), r = n == null ? void 0 : n.items[0];
5644
5639
  r && a(r) && (h = K(r.value));
5645
5640
  }
5646
5641
  }
@@ -5781,7 +5776,7 @@ function So(n, i, s, c, l = !1, g = /* @__PURE__ */ new WeakSet(), _ = void 0, v
5781
5776
  let e = (f ? $(n, o, f) : Z(n, o)).filter((e) => e !== p && (t(e) || r(e)));
5782
5777
  for (let t of e) X(n, d, t);
5783
5778
  }
5784
- let h = q$1({ [l[0]]: i }, c), _ = h.items[0];
5779
+ let h = q({ [l[0]]: i }, c), _ = h.items[0];
5785
5780
  if (a(_) && f && Q(u, f.key.item.value)) {
5786
5781
  let e = I(u, f.key.item.value) ? f : $(n, u, f).find((e) => t(e) && I(e.key.item.value, u));
5787
5782
  if (e) {
@@ -5822,7 +5817,7 @@ function So(n, i, s, c, l = !1, g = /* @__PURE__ */ new WeakSet(), _ = void 0, v
5822
5817
  let i = C, o = p.key.item.value, l = i, u = typeof s.path[s.path.length - 1] == `number` ? s.path.slice(0, -1) : s.path;
5823
5818
  for (let e of u) l = l == null ? void 0 : l[e];
5824
5819
  if (l !== void 0) {
5825
- let i = o.slice(-1), d = o.slice(0, -1), f = te(n, p) ?? la(n, s.path), m = Vo(n, u), h = q$1({ [i[0]]: l }, c).items[0];
5820
+ let i = o.slice(-1), d = o.slice(0, -1), f = te(n, p) ?? la(n, s.path), m = Vo(n, u), h = q({ [i[0]]: l }, c).items[0];
5826
5821
  if (Array.isArray(l) && !l.every((e) => F(e))) {
5827
5822
  let e = (m ? $(n, o, m) : Z(n, o)).filter((e) => e !== p && (t(e) || r(e)));
5828
5823
  for (let t of e) X(n, n, t);
@@ -6373,7 +6368,7 @@ function Oo(e, n, i, o, s, c, l) {
6373
6368
  _ = u;
6374
6369
  for (let e = i.path.length - 1; e > n; e--) _ = { [i.path[e]]: _ };
6375
6370
  }
6376
- let v = Mi(q$1(_, o).items, o), y = Array.from(N(v)), b = h >= 0 ? h : qo(e, l, Bo(e, l)), x = (t) => {
6371
+ let v = Mi(q(_, o).items, o), y = Array.from(N(v)), b = h >= 0 ? h : qo(e, l, Bo(e, l)), x = (t) => {
6377
6372
  let n = l.items.find((e) => a(e) && I(e.key.value, t.key.value));
6378
6373
  n ? V(e, l, n, t) : H(e, l, t, void 0);
6379
6374
  };
@@ -6404,7 +6399,7 @@ function Oo(e, n, i, o, s, c, l) {
6404
6399
  for (let e = i.path.length - 1; e >= 0; e--) p = { [i.path[e]]: p };
6405
6400
  let m = Z(e, i.path);
6406
6401
  for (let t of m) U(e, e, t);
6407
- let h = Mi(q$1(p, o).items, o), g = Array.from(N(h)), _ = To(e);
6402
+ let h = Mi(q(p, o).items, o), g = Array.from(N(h)), _ = To(e);
6408
6403
  _ !== void 0 && W(e);
6409
6404
  let v;
6410
6405
  for (let n = 0; n < g.length; n++) {
@@ -6545,7 +6540,7 @@ function Po(e, n, i) {
6545
6540
  if (o && r(o)) {
6546
6541
  let t = a[a.length - 1];
6547
6542
  if (typeof t != `string`) continue;
6548
- let n = q$1({ [t]: [] }, i), r = Rr([t], n.items[0].value);
6543
+ let n = q({ [t]: [] }, i), r = Rr([t], n.items[0].value);
6549
6544
  W(e), H(e, o, r);
6550
6545
  continue;
6551
6546
  }
@@ -6558,12 +6553,12 @@ function Po(e, n, i) {
6558
6553
  }
6559
6554
  }
6560
6555
  if (s) {
6561
- let t = a.filter((e) => typeof e == `string`).slice(s.key.item.value.length), n = Rr(t, q$1({ [t[t.length - 1]]: [] }, i).items[0].value);
6556
+ let t = a.filter((e) => typeof e == `string`).slice(s.key.item.value.length), n = Rr(t, q({ [t[t.length - 1]]: [] }, i).items[0].value);
6562
6557
  W(e), H(e, s, n);
6563
6558
  continue;
6564
6559
  }
6565
6560
  }
6566
- let n = a.filter((e) => typeof e == `string`), o = Rr(n, q$1({ [n[n.length - 1]]: [] }, i).items[0].value);
6561
+ let n = a.filter((e) => typeof e == `string`), o = Rr(n, q({ [n[n.length - 1]]: [] }, i).items[0].value);
6567
6562
  W(e), H(e, e, o, To(e));
6568
6563
  }
6569
6564
  W(e);
@@ -6596,7 +6591,7 @@ function Lo(e, n) {
6596
6591
  let r = L({
6597
6592
  ...n,
6598
6593
  inlineTableStart: 0
6599
- }, n), i = Mi(q$1({ __tmp__: e }, r).items, r), o = Array.from(N(i))[0];
6594
+ }, n), i = Mi(q({ __tmp__: e }, r).items, r), o = Array.from(N(i))[0];
6600
6595
  if (a(o)) return o.value;
6601
6596
  if (t(o) && o.items.length > 0 && a(o.items[0])) return o.items[0].value;
6602
6597
  }
@@ -6667,7 +6662,7 @@ function Wo(e, t, n) {
6667
6662
  let r = L({
6668
6663
  ...n,
6669
6664
  inlineTableStart: 0
6670
- }, n), i = q$1({ [e[e.length - 1]]: t }, r), o = Array.from(N(Mi(i.items, n)));
6665
+ }, n), i = q({ [e[e.length - 1]]: t }, r), o = Array.from(N(Mi(i.items, n)));
6671
6666
  if (o.length !== 1 || !a(o[0])) return;
6672
6667
  let s = o[0];
6673
6668
  return e.length > 1 && Go(s, e, n), s;
@@ -6705,7 +6700,7 @@ function Yo(e, n, i, o, s, c, l) {
6705
6700
  if (Ho(o)) {
6706
6701
  let d = n.items.filter((e) => a(e) && e.key.value[0] === u[0]);
6707
6702
  if (d.some((e) => !Q(e.key.value, u))) return !1;
6708
- let p = q$1({ [i[i.length - 1]]: o }, s), m = Array.from(N(Mi(p.items, s)));
6703
+ let p = q({ [i[i.length - 1]]: o }, s), m = Array.from(N(Mi(p.items, s)));
6709
6704
  if (!m.every((e) => t(e) || r(e))) return !1;
6710
6705
  let h = $(e, i, l), g = h.length > 0 ? e.items.indexOf(h[0]) : -1;
6711
6706
  for (let t of h) X(e, e, t);
@@ -6758,476 +6753,4 @@ function cs(e, t) {
6758
6753
  temporal: (t == null ? void 0 : t.temporal) ?? !1
6759
6754
  });
6760
6755
  }
6761
- // ../adapters/codex/dist/index.js
6762
- const SERVER_NAME = "conveno";
6763
- var ConfigParseError = class extends Error {};
6764
- function parseToml(path, text) {
6765
- try {
6766
- return cs(text);
6767
- } catch (err) {
6768
- throw new ConfigParseError(`Cannot parse ${path} as TOML (${err instanceof Error ? err.message : String(err)}). Fix or move the file, then run conveno again. Conveno did not change it.`);
6769
- }
6770
- }
6771
- /** The approval mode Conveno asks for. A mode the user already set on the server is kept instead. */
6772
- const APPROVAL_MODE = "approve";
6773
- function serverOf(doc) {
6774
- const servers = doc.mcp_servers;
6775
- if (servers && typeof servers === "object") {
6776
- const ours = servers[SERVER_NAME];
6777
- if (ours && typeof ours === "object") return ours;
6778
- }
6779
- }
6780
- function matchesSpec(server, spec) {
6781
- return server !== void 0 && server.command === spec.command && JSON.stringify(server.args) === JSON.stringify(spec.args) && server.default_tools_approval_mode === spec.approval;
6782
- }
6783
- const eolOf = (text) => text.includes("\r\n") ? "\r\n" : "\n";
6784
- /** TOML basic strings use the same escapes as JSON for everything we write. */
6785
- const q = (s) => JSON.stringify(s);
6786
- function tableText(spec, eol) {
6787
- return [
6788
- `[mcp_servers.${SERVER_NAME}]`,
6789
- `command = ${q(spec.command)}`,
6790
- `args = [${spec.args.map(q).join(", ")}]`,
6791
- `default_tools_approval_mode = ${q(spec.approval)}`,
6792
- ""
6793
- ].join(eol);
6794
- }
6795
- /** Compare parsed data, treating a missing `mcp_servers` and an empty one as equal. */
6796
- const sameData = (a, b) => {
6797
- const norm = (t) => JSON.stringify(t, (k, v) => k === "mcp_servers" && v && Object.keys(v).length === 0 ? void 0 : v);
6798
- return norm(a) === norm(b);
6799
- };
6800
- /**
6801
- * Add our `[mcp_servers.conveno]` table. The file is left byte-for-byte intact: the table is
6802
- * appended as text. If the result does not parse to the expected data (for example the user
6803
- * already defines `mcp_servers.conveno` inline), fall back to a comment-preserving patch.
6804
- */
6805
- function addServer(path, before, spec) {
6806
- const doc = parseToml(path, before);
6807
- const expected = structuredClone(doc);
6808
- const servers = expected.mcp_servers ?? {};
6809
- servers[SERVER_NAME] = {
6810
- command: spec.command,
6811
- args: spec.args,
6812
- default_tools_approval_mode: spec.approval
6813
- };
6814
- expected.mcp_servers = servers;
6815
- const eol = eolOf(before);
6816
- const appended = before + (before === "" ? "" : before.endsWith("\n") ? eol : eol + eol) + tableText(spec, eol);
6817
- try {
6818
- if (sameData(cs(appended), expected)) return appended;
6819
- } catch {}
6820
- return Za(before, expected);
6821
- }
6822
- /** Remove our table and any `[mcp_servers.conveno.*]` subtables. */
6823
- function removeServer(path, before) {
6824
- const doc = parseToml(path, before);
6825
- const expected = structuredClone(doc);
6826
- const servers = expected.mcp_servers;
6827
- if (servers) delete servers[SERVER_NAME];
6828
- const eol = eolOf(before);
6829
- const lines = before.split(/\r?\n/);
6830
- const out = [];
6831
- let skipping = false;
6832
- for (const line of lines) {
6833
- const header = /^\s*\[\[?\s*([^\]]+?)\s*\]\]?\s*(?:#.*)?$/.exec(line);
6834
- if (header) {
6835
- const key = header[1].replace(/["'\s]/g, "");
6836
- const key0 = `mcp_servers.${SERVER_NAME}`;
6837
- skipping = key === key0 || key.startsWith(`${key0}.`);
6838
- }
6839
- if (!skipping) out.push(line);
6840
- }
6841
- let text = out.join(eol);
6842
- if (skipping) text = text.replace(/(\r?\n)+$/, "") + (text.trim() === "" ? "" : eol);
6843
- try {
6844
- if (sameData(cs(text), expected)) return text;
6845
- } catch {}
6846
- return Za(before, expected);
6847
- }
6848
- const HOOK_EVENTS = [
6849
- "SessionStart",
6850
- "UserPromptSubmit",
6851
- "Stop"
6852
- ];
6853
- /** Older versions also installed a PermissionRequest hook; uninstall still removes it. */
6854
- const LEGACY_HOOK_EVENTS = ["PermissionRequest"];
6855
- /** Seconds. The hook gives up after 2 s on its own; this leaves room for Node startup. Same as Claude. */
6856
- const HOOK_TIMEOUT_SECONDS = 5;
6857
- const codexDir = (env) => env.env.CODEX_HOME || join(env.home, ".codex");
6858
- const configPath = (env) => join(codexDir(env), "config.toml");
6859
- const hooksPath = (env) => join(codexDir(env), "hooks.json");
6860
- async function readIfExists(path) {
6861
- try {
6862
- return await readFile(path, "utf8");
6863
- } catch (err) {
6864
- if (err.code === "ENOENT") return null;
6865
- throw err;
6866
- }
6867
- }
6868
- /**
6869
- * Quote one word of a hook command. Codex runs the command line with `$SHELL -lc` on Unix and with
6870
- * `%COMSPEC% /C "<line>"` (cmd.exe) on Windows, so Windows words are written for cmd.exe: double
6871
- * quotes, and forward slashes in paths. cmd.exe and every Windows API accept `/`, and it avoids
6872
- * any question of how `\` meets a quote.
6873
- */
6874
- function quoteArg(arg, platform) {
6875
- if (platform === "win32") {
6876
- const word = arg.replace(/\\/g, "/");
6877
- return /^[A-Za-z0-9_./:=@+,-]+$/.test(word) ? word : `"${word.replace(/"/g, "\"\"")}"`;
6878
- }
6879
- if (/^[A-Za-z0-9_./:=@%+,-]+$/.test(arg)) return arg;
6880
- return `'${arg.replace(/'/g, `'\\''`)}'`;
6881
- }
6882
- function hookCommand(env, event) {
6883
- return [
6884
- ...env.conveno.command,
6885
- "hook",
6886
- "codex",
6887
- event
6888
- ].map((a) => quoteArg(a, env.platform)).join(" ");
6889
- }
6890
- /** A handler is ours when it runs `... hook codex <event>`; this lets us find it after the command path changes. */
6891
- const isOurs = (command, event) => typeof command === "string" && new RegExp(`\\bhook codex ${event}\\s*$`).test(command);
6892
- function parseHooksJson(path, text) {
6893
- if (text === null || text.trim() === "") return {};
6894
- try {
6895
- const value = JSON.parse(text);
6896
- if (value && typeof value === "object" && !Array.isArray(value)) return value;
6897
- } catch {}
6898
- throw new ConfigParseError(`Cannot parse ${path} as a JSON object. Fix or move the file, then run conveno again. Conveno did not change it.`);
6899
- }
6900
- const asArray = (v) => Array.isArray(v) ? v : [];
6901
- const asObject = (v) => v && typeof v === "object" && !Array.isArray(v) ? v : {};
6902
- function handlersOf(group) {
6903
- return asArray(asObject(group).hooks);
6904
- }
6905
- /** Hook entries for one event with our handlers removed; empty groups are dropped. */
6906
- function withoutOurs(groups, event) {
6907
- const out = [];
6908
- for (const group of groups) {
6909
- const handlers = handlersOf(group);
6910
- const kept = handlers.filter((h) => !isOurs(asObject(h).command, event));
6911
- if (kept.length === handlers.length) out.push(group);
6912
- else if (kept.length > 0) out.push({
6913
- ...asObject(group),
6914
- hooks: kept
6915
- });
6916
- }
6917
- return out;
6918
- }
6919
- function hasOurs(groups, command) {
6920
- return groups.some((g) => handlersOf(g).some((h) => asObject(h).command === command));
6921
- }
6922
- const json = (value) => `${JSON.stringify(value, null, 2)}\n`;
6923
- async function planHooks(env) {
6924
- const path = hooksPath(env);
6925
- const before = await readIfExists(path);
6926
- const doc = parseHooksJson(path, before);
6927
- const hooks = asObject(doc.hooks);
6928
- let changed = false;
6929
- const next = { ...hooks };
6930
- for (const event of HOOK_EVENTS) {
6931
- const groups = asArray(hooks[event]);
6932
- const command = hookCommand(env, event);
6933
- if (hasOurs(groups, command)) continue;
6934
- changed = true;
6935
- next[event] = [...withoutOurs(groups, event), { hooks: [{
6936
- type: "command",
6937
- command,
6938
- timeout: HOOK_TIMEOUT_SECONDS
6939
- }] }];
6940
- }
6941
- if (!changed) return [];
6942
- const partKind = (event) => asArray(hooks[event]).some((g) => handlersOf(g).some((h) => isOurs(asObject(h).command, event))) ? "update" : "add";
6943
- const parts = [];
6944
- for (const event of HOOK_EVENTS) if (!hasOurs(asArray(hooks[event]), hookCommand(env, event))) parts.push({
6945
- name: `${event} hook`,
6946
- kind: partKind(event)
6947
- });
6948
- return [{
6949
- component: "hooks",
6950
- kind: [...HOOK_EVENTS, ...LEGACY_HOOK_EVENTS].some((event) => asArray(hooks[event]).some((g) => handlersOf(g).some((h) => isOurs(asObject(h).command, event)))) ? "update" : "add",
6951
- parts,
6952
- path,
6953
- before,
6954
- after: json({
6955
- ...doc,
6956
- hooks: next
6957
- }),
6958
- description: `Add SessionStart, UserPromptSubmit and Stop hooks that deliver Conveno messages on the next turn and at the end of a turn. Codex asks you to trust new hooks: review them with /hooks.`
6959
- }];
6960
- }
6961
- async function uninstallHooks(env) {
6962
- const path = hooksPath(env);
6963
- const before = await readIfExists(path);
6964
- if (before === null) return [];
6965
- const doc = parseHooksJson(path, before);
6966
- const hooks = asObject(doc.hooks);
6967
- const next = { ...hooks };
6968
- let changed = false;
6969
- for (const event of [...HOOK_EVENTS, ...LEGACY_HOOK_EVENTS]) {
6970
- const groups = asArray(hooks[event]);
6971
- const kept = withoutOurs(groups, event);
6972
- if (JSON.stringify(kept) === JSON.stringify(groups)) continue;
6973
- changed = true;
6974
- if (kept.length > 0) next[event] = kept;
6975
- else delete next[event];
6976
- }
6977
- if (!changed) return [];
6978
- const { hooks: _drop, ...rest } = doc;
6979
- const after = Object.keys(next).length > 0 ? {
6980
- ...rest,
6981
- hooks: next
6982
- } : rest;
6983
- return [{
6984
- component: "hooks",
6985
- kind: "remove",
6986
- path,
6987
- before,
6988
- after: json(after),
6989
- description: "Remove the Conveno SessionStart, UserPromptSubmit, and Stop hooks."
6990
- }];
6991
- }
6992
- function serverSpec(env, existing) {
6993
- const [command, ...rest] = env.conveno.command;
6994
- if (!command) throw new Error("conveno command is empty; cannot build the Codex MCP entry.");
6995
- const chosen = existing?.default_tools_approval_mode;
6996
- return {
6997
- command,
6998
- args: [
6999
- ...rest,
7000
- "mcp",
7001
- "--cli",
7002
- "codex"
7003
- ],
7004
- approval: typeof chosen === "string" ? chosen : APPROVAL_MODE
7005
- };
7006
- }
7007
- async function planMcp(env) {
7008
- const path = configPath(env);
7009
- const before = await readIfExists(path);
7010
- const existing = serverOf(parseToml(path, before ?? ""));
7011
- const spec = serverSpec(env, existing);
7012
- if (matchesSpec(existing, spec)) return [];
7013
- return [{
7014
- component: "mcp",
7015
- kind: existing ? "update" : "add",
7016
- parts: [{
7017
- name: "MCP server",
7018
- kind: existing ? "update" : "add"
7019
- }],
7020
- path,
7021
- before,
7022
- after: addServer(path, before ?? "", spec),
7023
- description: `${existing ? "Update" : "Add"} the "${SERVER_NAME}" MCP server in Codex config (mcp_servers.${SERVER_NAME}). It lets Codex call Conveno's own tools without asking each time (default_tools_approval_mode = "${spec.approval}", this server only).`
7024
- }];
7025
- }
7026
- async function uninstallMcp(env) {
7027
- const path = configPath(env);
7028
- const before = await readIfExists(path);
7029
- if (before === null || !serverOf(parseToml(path, before))) return [];
7030
- return [{
7031
- component: "mcp",
7032
- kind: "remove",
7033
- path,
7034
- before,
7035
- after: removeServer(path, before),
7036
- description: `Remove the "${SERVER_NAME}" MCP server from Codex config.`
7037
- }];
7038
- }
7039
- async function detect(env) {
7040
- const configPaths = [configPath(env), hooksPath(env)];
7041
- try {
7042
- const { code, stdout } = await env.exec("codex", ["--version"]);
7043
- if (code !== 0) return {
7044
- installed: false,
7045
- configPaths
7046
- };
7047
- const version = parseCliVersion(stdout);
7048
- return {
7049
- installed: true,
7050
- binary: "codex",
7051
- ...version ? { version } : {},
7052
- configPaths
7053
- };
7054
- } catch {
7055
- return {
7056
- installed: false,
7057
- configPaths
7058
- };
7059
- }
7060
- }
7061
- const EMPTY = {
7062
- stdout: "",
7063
- exitCode: 0
7064
- };
7065
- /**
7066
- * - SessionStart, UserPromptSubmit: waiting messages as `additionalContext` (UserPromptSubmit also
7067
- * marks the turn started).
7068
- * - Stop: with messages waiting, `decision: "block"` with the framed messages as
7069
- * `reason`, which Codex turns into a continuation prompt in the same turn. This also happens when
7070
- * `stop_hook_active` is set: a message that arrives during a
7071
- * continuation would otherwise wait for the user. Conveno's wake limit bounds the chain.
7072
- */
7073
- async function hook(event, input, ctx) {
7074
- await recordThreadId(input, ctx);
7075
- if (event === "Stop") {
7076
- const reason = await endTurnContinuation(ctx);
7077
- if (reason === null) return EMPTY;
7078
- return {
7079
- stdout: `${JSON.stringify({
7080
- decision: "block",
7081
- reason
7082
- })}\n`,
7083
- exitCode: 0
7084
- };
7085
- }
7086
- if (event !== "SessionStart" && event !== "UserPromptSubmit") return EMPTY;
7087
- const additionalContext = await (event === "SessionStart" ? pendingHookContext(ctx) : startTurnContext(ctx));
7088
- if (additionalContext === null) return EMPTY;
7089
- return {
7090
- stdout: `${JSON.stringify({ hookSpecificOutput: {
7091
- hookEventName: event,
7092
- additionalContext
7093
- } })}\n`,
7094
- exitCode: 0
7095
- };
7096
- }
7097
- /**
7098
- * The hook's `session_id` is the Codex thread id, the id `codex queue --thread` takes. Codex
7099
- * sessions can share one app-server process, so the host pid alone may not tell them apart: tell
7100
- * the daemon the thread id so it can wake this session by it. Best effort; an older daemon
7101
- * without `session.setNative` just keeps the pid lookup.
7102
- */
7103
- async function recordThreadId(input, ctx) {
7104
- const threadId = asObject(input).session_id;
7105
- if (typeof threadId !== "string" || threadId === "") return;
7106
- try {
7107
- const session = await ownSession(ctx);
7108
- if (!session) return;
7109
- const call = ctx.client["session.setNative"];
7110
- if (typeof call === "function") await call({
7111
- sessionId: session.id,
7112
- nativeId: threadId
7113
- });
7114
- } catch {}
7115
- }
7116
- /** How long `codex queue` may take (it starts or finds the shared app-server) before we give up. */
7117
- const QUEUE_TIMEOUT_MS = 2e4;
7118
- const CUT_NOTE = "\n\n[Cut to fit a command line. Read the full messages with the conveno inbox tool.]";
7119
- const runProcess = (file, args, opts) => new Promise((resolve) => {
7120
- try {
7121
- execFile(file, args, {
7122
- timeout: QUEUE_TIMEOUT_MS,
7123
- windowsHide: true,
7124
- ...opts
7125
- }, (err) => {
7126
- if (!err) return resolve(0);
7127
- const e = err;
7128
- if (e.code === "ENOENT") return resolve("missing");
7129
- if (e.killed) return resolve("timeout");
7130
- resolve(typeof e.code === "number" ? e.code : 1);
7131
- });
7132
- } catch {
7133
- resolve(1);
7134
- }
7135
- });
7136
- const readText = (path) => {
7137
- try {
7138
- return readFileSync(path, "utf8");
7139
- } catch {
7140
- return;
7141
- }
7142
- };
7143
- const isFileSync = (path) => {
7144
- try {
7145
- return statSync(path).isFile();
7146
- } catch {
7147
- return false;
7148
- }
7149
- };
7150
- /** `text`, cut to `MAX_QUEUE_TEXT` with a note when longer. */
7151
- function fitQueueText(text) {
7152
- if (text.length <= 24e3) return text;
7153
- let head = text.slice(0, 23918);
7154
- if (/[\uD800-\uDBFF]$/.test(head)) head = head.slice(0, -1);
7155
- return head + CUT_NOTE;
7156
- }
7157
- /**
7158
- * Wake an idle Codex session: `codex queue --thread <thread id> --message <text>`. Codex starts a
7159
- * queued message as a turn when the thread is idle and keeps it until the turn ends when it is
7160
- * busy, so the message lands like one the user typed. No `--resume`: a thread nobody has open
7161
- * stays queued and runs when it is resumed.
7162
- *
7163
- * `codex queue` takes the text only as an argument (no stdin, no file), so on Windows the text
7164
- * must never reach a cmd.exe command line, where `"`, `&`, `%VAR%` and line breaks are parsed
7165
- * again by the shim. A `codex.exe` runs directly; an npm `codex.cmd` shim is bypassed by running
7166
- * the script it names with node. Any other `.cmd` gives `unsupported`.
7167
- *
7168
- * `queued`: codex took it. `unknown`: it timed out, so it may have been queued. `unsupported`:
7169
- * no codex, or one too old for `queue` (a usage error). `failed`: anything else.
7170
- */
7171
- async function wakeCodexThread(args) {
7172
- if (!args.nativeId) return "unsupported";
7173
- if (args.text.trim() === "") return "failed";
7174
- const platform = args.platform ?? process.platform;
7175
- const env = args.env ?? process.env;
7176
- const program = findOnPath("codex", env, platform, args.isFile);
7177
- if (!program) return "unsupported";
7178
- const argv = [
7179
- "queue",
7180
- "--thread",
7181
- args.nativeId,
7182
- "--message",
7183
- fitQueueText(args.text)
7184
- ];
7185
- let plan;
7186
- try {
7187
- plan = planProgram(program, argv, {
7188
- platform,
7189
- env,
7190
- io: {
7191
- isFile: args.isFile ?? isFileSync,
7192
- readFile: args.readFile ?? readText
7193
- }
7194
- });
7195
- } catch {
7196
- return "unsupported";
7197
- }
7198
- if (plan.windowsVerbatimArguments) return "unsupported";
7199
- const result = await (args.run ?? runProcess)(plan.file, plan.args, { env: { ...env } });
7200
- if (result === 0) return "queued";
7201
- if (result === "timeout") return "unknown";
7202
- if (result === "missing" || result === 2) return "unsupported";
7203
- return "failed";
7204
- }
7205
- /** The daemon calls this when a message arrives for an idle Codex session (see WAKE-NOTES). */
7206
- async function daemonWake(a) {
7207
- return wakeCodexThread({
7208
- nativeId: a.nativeId,
7209
- text: a.text
7210
- });
7211
- }
7212
- /** Idle wake goes through `codex queue`; on a Codex without it the message waits for the next hook. */
7213
- const delivery = () => ({
7214
- nextTurn: true,
7215
- endOfTurn: true,
7216
- idleWake: true
7217
- });
7218
- const codexAdapter = {
7219
- cli: "codex",
7220
- displayName: "Codex",
7221
- deliveryTier: "next_turn",
7222
- detect,
7223
- async plan(env) {
7224
- return [...await planMcp(env), ...await planHooks(env)];
7225
- },
7226
- async uninstallPlan(env) {
7227
- return [...await uninstallMcp(env), ...await uninstallHooks(env)];
7228
- },
7229
- hook,
7230
- delivery,
7231
- daemonWake
7232
- };
7233
- export { Za as n, cs as r, codexAdapter as t };
6756
+ export { cs as n, Za as t };