@conveno/cli 0.2.0-rc.10 → 0.2.0-rc.12

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,6 +1,6 @@
1
1
  import { readFile, readdir } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
- import { $t as uninstallLauncher, B as editJson, H as endTurnContinuation, Ht as sameJson, O as compareVersions, P as dataDir, W as existedBefore, _ as agentCommand, _t as pendingHookContext, dt as ownSession, et as isObject, lt as needsQuoting, ot as loadJson, pt as parseCliVersion, rt as late, v as askedMarker, yt as planLauncher, z as dropFromList } from "./dist-STNJjbNH.js";
3
+ import { Ct as pendingHookContext, Et as planLauncher, G as endTurnContinuation, H as dropFromList, Jt as sameJson, L as dataDir, U as editJson, _t as ownSession, an as uninstallLauncher, at as isObject, ct as late, dt as loadJson, j as compareVersions, mt as needsQuoting, q as existedBefore, v as agentCommand, x as askedMarker, yt as parseCliVersion } from "./dist-CRj_cuHc.js";
4
4
  // ../adapters/antigravity/dist/index.js
5
5
  const SERVER_NAME = "conveno";
6
6
  /** The name of our entry in hooks.json. Antigravity keys hooks by an arbitrary name. */
@@ -554,19 +554,25 @@ async function uninstallPlan(env, baseline) {
554
554
  }
555
555
  const UUID = /^[0-9a-fA-F-]{8,64}$/;
556
556
  /**
557
- * The session that owns this hook, after recording its conversation id as the session's native id
558
- * (`session.setNative`), which is how `conveno agy-relay` finds its session. Null when no session
559
- * owns the hook or on any failure. Never throws.
557
+ * The hook's context with its conversation id, which the daemon binds to the window's session
558
+ * (`session.bind`): that is how `conveno agy-relay` finds its session, and the window's session
559
+ * follows `/new` and `/resume`. A session start or a model call may move the window to the
560
+ * conversation; a Stop of any other conversation than the current one is ignored.
560
561
  */
561
- async function sessionOf(input, ctx) {
562
+ function withConversation(input, ctx, turnStart) {
563
+ const id = isObject(input) ? input.conversationId : void 0;
564
+ if (typeof id !== "string" || !UUID.test(id)) return ctx;
565
+ return {
566
+ ...ctx,
567
+ cli: ctx.cli ?? "antigravity",
568
+ nativeId: id,
569
+ turnStart
570
+ };
571
+ }
572
+ /** The session that owns this hook, or null when none does or on any failure. Never throws. */
573
+ async function sessionOf(ctx) {
562
574
  try {
563
- const session = await ownSession(ctx);
564
- const id = isObject(input) ? input.conversationId : void 0;
565
- if (!session || typeof id !== "string" || !UUID.test(id) || session.nativeId === id) return session;
566
- return (await ctx.client["session.setNative"]({
567
- sessionId: session.id,
568
- nativeId: id
569
- })).session;
575
+ return await ownSession(ctx);
570
576
  } catch {
571
577
  return null;
572
578
  }
@@ -592,7 +598,8 @@ const isHookEvent = (e) => HOOK_EVENTS.includes(e);
592
598
  */
593
599
  async function hook(event, input, ctx) {
594
600
  if (!isHookEvent(event)) return EMPTY;
595
- const session = await sessionOf(input, ctx);
601
+ ctx = withConversation(input, ctx, event !== "Stop");
602
+ const session = await sessionOf(ctx);
596
603
  if (event === "Stop") {
597
604
  const stop = isObject(input) ? input : {};
598
605
  const completed = (stop.terminationReason === void 0 || stop.terminationReason === "NO_TOOL_CALL") && (stop.error === void 0 || stop.error === "");
@@ -1,4 +1,5 @@
1
1
  import { appendFileSync } from "node:fs";
2
+ import { tt as hookLogLine, y as appendHookLog } from "./dist-CRj_cuHc.js";
2
3
  import { n as connectOpts } from "./ctx-tYftvhz_.js";
3
4
  // src/hook.ts
4
5
  /**
@@ -39,6 +40,49 @@ async function findHostPid(ctx, client, cli, local) {
39
40
  function timingLine(cli, event, ms, procMs, timedOut) {
40
41
  return `${(/* @__PURE__ */ new Date()).toISOString()} cli=${cli} event=${event} ms=${ms}${procMs === void 0 ? "" : ` proc_ms=${procMs}`} timeout=${timedOut ? "yes" : "no"}\n`;
41
42
  }
43
+ /**
44
+ * The client with its answers noted in `trace`: the session a lookup resolved to, the messages
45
+ * handed over, the turn state recorded. The hook sees the same client.
46
+ */
47
+ function traced(client, trace) {
48
+ return new Proxy(client, { get(target, prop, receiver) {
49
+ const value = Reflect.get(target, prop, receiver);
50
+ if (typeof prop !== "string" || !prop.includes(".") || typeof value !== "function") return value;
51
+ return async (...args) => {
52
+ let r;
53
+ try {
54
+ r = await value.apply(target, args);
55
+ } catch (e) {
56
+ const code = e?.code;
57
+ trace.error = `error ${typeof code === "number" || typeof code === "string" ? code : "unknown"}`;
58
+ throw e;
59
+ }
60
+ const out = r;
61
+ const count = Array.isArray(out?.messages) ? out.messages.length : 0;
62
+ if ((prop === "session.bind" || prop === "session.byHost") && out?.session?.name) trace.session = out.session.name;
63
+ else if (prop === "turn.start") trace.turn = "busy";
64
+ else if (prop === "turn.end") {
65
+ trace.delivered += count;
66
+ trace.turn = count > 0 ? "busy" : "idle";
67
+ } else if (prop === "message.deliver" || prop === "turn.wake") trace.delivered += count;
68
+ return r;
69
+ };
70
+ } });
71
+ }
72
+ /** The CLI's own conversation id in a hook's input, whichever way that CLI spells it. */
73
+ function conversationIn(input) {
74
+ if (typeof input !== "object" || input === null) return void 0;
75
+ const o = input;
76
+ for (const key of [
77
+ "session_id",
78
+ "sessionId",
79
+ "sessionID",
80
+ "conversationId"
81
+ ]) {
82
+ const v = o[key];
83
+ if (typeof v === "string" && v !== "") return v;
84
+ }
85
+ }
42
86
  /** After a timeout, the hook gets this many more timeouts' worth of time to finish recording. */
43
87
  const FINISH_GRACE_FACTOR = 2;
44
88
  /** How long before the hook's timeout its messages count as too late to print. */
@@ -60,6 +104,8 @@ async function runHook(ctx, argv) {
60
104
  const listener = adapter.listenEvents?.includes(event) === true;
61
105
  const deadline = listener ? void 0 : Date.now() + ctx.hookTimeoutMs - DEADLINE_MARGIN_MS;
62
106
  const t0 = ctx.timingLog && !listener ? Date.now() : void 0;
107
+ const trace = { delivered: 0 };
108
+ let conversation;
63
109
  let timedOut = false;
64
110
  let client;
65
111
  let timer;
@@ -74,9 +120,13 @@ async function runHook(ctx, argv) {
74
120
  }
75
121
  };
76
122
  const [input, connected] = await Promise.all([readInput(), ctx.connect(connectOpts(ctx, false))]);
77
- client = connected;
123
+ client = ctx.hookLog ? traced(connected, trace) : connected;
124
+ if (ctx.hookLog) conversation = conversationIn(input);
78
125
  const hostPid = await findHostPid(ctx, connected, adapter.cli, () => early ?? ctx.ancestry(1200));
79
- if (hostPid === void 0) return NOTHING;
126
+ if (hostPid === void 0) {
127
+ await adapter.hookWithoutSession?.(event, input).catch(() => {});
128
+ return NOTHING;
129
+ }
80
130
  return adapter.hook(event, input, {
81
131
  client,
82
132
  hostPid,
@@ -99,6 +149,16 @@ async function runHook(ctx, argv) {
99
149
  if (listener && out.stderr) ctx.err(out.stderr.endsWith("\n") ? out.stderr : `${out.stderr}\n`);
100
150
  if (listener) code = out.exitCode;
101
151
  } catch {} finally {
152
+ if (ctx.hookLog) {
153
+ const result = timedOut ? "timeout" : trace.error ?? (trace.delivered > 0 ? `delivered ${trace.delivered}` : trace.turn ?? "nothing");
154
+ appendHookLog(ctx.hookLog, hookLogLine({
155
+ cli: adapter.cli,
156
+ event,
157
+ conversationId: conversation,
158
+ session: trace.session,
159
+ result
160
+ }));
161
+ }
102
162
  if (t0 !== void 0 && ctx.timingLog) {
103
163
  const up = ctx.uptimeMs?.();
104
164
  try {