openmsg 0.4.2 → 0.4.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openmsg",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Messages between AI coding agents of different vendors, on one machine or between two people, sealed end to end.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -2,14 +2,31 @@
2
2
  // The OpenCode user interface is a client of a local HTTP server. Any program
3
3
  // can send a prompt to a session with one request.
4
4
  // Source: https://opencode.ai/docs/server/
5
- import { render } from "../envelope.mjs";
5
+ //
6
+ // OpenCode draws the text of a user message in one color. It shows no escape
7
+ // sequence and no markdown there. It shows only the first text part that is not
8
+ // `synthetic`, and the model reads every part, in order. So the header, the text
9
+ // of the sender, and the line of dashes after it go in one part that the person
10
+ // sees. The notice and the answer command go in a synthetic part: the model
11
+ // reads them, and the screen does not show them.
12
+ // Source: packages/opencode/src/cli/cmd/tui/routes/session/index.tsx (v1.1.11),
13
+ // packages/opencode/src/session/message-v2.ts.
14
+ import { render, RULE } from "../envelope.mjs";
6
15
 
7
- export async function deliver(agent, message, { text = null } = {}) {
16
+ export function partsOf({ head, body, foot }) {
17
+ const cut = foot.indexOf(RULE) + 1;
18
+ const shown = { type: "text", text: [...head, body, ...foot.slice(0, cut)].join("\n") };
19
+ const rest = foot.slice(cut);
20
+ return rest.length ? [shown, { type: "text", synthetic: true, text: rest.join("\n") }] : [shown];
21
+ }
22
+
23
+ export async function deliver(agent, message, { text = null, layout = null } = {}) {
8
24
  const url = `${agent.transport.base}/session/${agent.id}/prompt_async`;
25
+ const parts = layout ? partsOf(layout) : [{ type: "text", text: text ?? render(message) }];
9
26
  const res = await fetch(url, {
10
27
  method: "POST",
11
28
  headers: { "content-type": "application/json" },
12
- body: JSON.stringify({ parts: [{ type: "text", text: text ?? render(message) }] }),
29
+ body: JSON.stringify({ parts }),
13
30
  signal: AbortSignal.timeout(5000),
14
31
  });
15
32
  if (!res.ok) throw new Error(`opencode ${res.status}: ${(await res.text()).slice(0, 200)}`);
package/src/deliver.mjs CHANGED
@@ -12,7 +12,8 @@ import { layoutOf, compose, bodyRange } from "./envelope.mjs";
12
12
  // and the text of the sender is blue.
13
13
  // Codex removes each escape sequence, but it draws a byte range in its accent
14
14
  // color. The text of the sender gets that color, and the model never sees it.
15
- // OpenCode gets the plain text. Its display is not tested.
15
+ // OpenCode shows no color. The person sees the header and the text of the
16
+ // sender, and the notice after them goes in a part that only the model reads.
16
17
  // `text` alone, with no layout, goes to the adapter as it is.
17
18
  export async function deliverLocal(agent, message, { text = null, parts = null } = {}) {
18
19
  if (!parts && !text) parts = layoutOf(message);
@@ -25,7 +26,7 @@ export async function deliverLocal(agent, message, { text = null, parts = null }
25
26
  : undefined;
26
27
  return claude.deliver(agent, message, { token, text: parts ? compose(parts, { ansi: true }) : text });
27
28
  }
28
- if (agent.vendor === "opencode") return opencode.deliver(agent, message, { text: plain });
29
+ if (agent.vendor === "opencode") return opencode.deliver(agent, message, parts ? { layout: parts } : { text });
29
30
  if (agent.vendor === "codex") return codex.deliver(agent, message, { text: plain, highlight: parts ? bodyRange(parts) : null });
30
31
  // An agent with no push entry point, such as Gemini CLI or Cursor CLI, reads
31
32
  // its mailbox at the end of a turn. The message waits there.
package/src/registry.mjs CHANGED
@@ -51,6 +51,16 @@ export function claudeAgents() {
51
51
  return out;
52
52
  }
53
53
 
54
+ // The pids of the programs with this exact name. On macOS, pgrep leaves out
55
+ // the parents of its own process, so an agent that runs this command does not
56
+ // find itself. `-a` puts them back. Linux pgrep leaves out only itself, and
57
+ // there `-a` means another thing.
58
+ async function pgrep(name) {
59
+ const args = process.platform === "darwin" ? ["-a", "-x", name] : ["-x", name];
60
+ const { stdout } = await run("pgrep", args);
61
+ return stdout.trim().split("\n").filter(Boolean);
62
+ }
63
+
54
64
  // OpenCode runs an HTTP server for each user interface. The port is in the
55
65
  // listening sockets of the process.
56
66
  export async function opencodeAgents({ maxAgeMs = 24 * 3600 * 1000 } = {}) {
@@ -59,8 +69,7 @@ export async function opencodeAgents({ maxAgeMs = 24 * 3600 * 1000 } = {}) {
59
69
  // `-x` matches the name of the program. `-f` matches the whole command
60
70
  // line, and that also matches a program that only holds the word in its
61
71
  // environment.
62
- const { stdout } = await run("pgrep", ["-x", "opencode"]);
63
- pids = stdout.trim().split("\n").filter(Boolean);
72
+ pids = await pgrep("opencode");
64
73
  } catch {
65
74
  return [];
66
75
  }
@@ -117,8 +126,7 @@ export async function opencodeAgents({ maxAgeMs = 24 * 3600 * 1000 } = {}) {
117
126
  export async function codexAgents() {
118
127
  let pids = [];
119
128
  try {
120
- const { stdout } = await run("pgrep", ["-x", "codex"]);
121
- pids = stdout.trim().split("\n").filter(Boolean);
129
+ pids = await pgrep("codex");
122
130
  } catch {
123
131
  return [];
124
132
  }
@@ -268,6 +276,41 @@ async function ancestors(pid, depth = 8) {
268
276
  return out;
269
277
  }
270
278
 
279
+ // The agents that can run a command of their own. Each one is a sender apart
280
+ // from the Claude session that started it.
281
+ const OTHER_AGENTS = new Set(["codex", "opencode", "cursor-agent"]);
282
+
283
+ // A program that a Claude session starts, such as an OpenCode server, gets the
284
+ // environment of that session, with its socket. That socket names the sender
285
+ // only if the session is a parent of this command, and no other agent runs
286
+ // between the two. One `ps` call gives the whole tree, so the walk has no
287
+ // limit of depth.
288
+ export async function runsUnder(sessionPid) {
289
+ let table;
290
+ try {
291
+ const { stdout } = await run("ps", ["-A", "-o", "pid=,ppid=,comm="]);
292
+ table = new Map(
293
+ stdout.trim().split("\n").map((l) => {
294
+ const [pid, ppid, ...comm] = l.trim().split(/\s+/);
295
+ return [Number(pid), { ppid: Number(ppid), name: path.basename(comm.join(" ")) }];
296
+ }),
297
+ );
298
+ } catch {
299
+ table = new Map();
300
+ }
301
+ // In a sandbox, `ps` can give nothing. Then the socket is the only evidence.
302
+ if (!table.has(process.ppid)) return true;
303
+ const seen = new Set();
304
+ for (let pid = process.ppid; pid > 1 && !seen.has(pid); ) {
305
+ if (pid === sessionPid) return true;
306
+ seen.add(pid);
307
+ const p = table.get(pid);
308
+ if (!p || OTHER_AGENTS.has(p.name)) return false;
309
+ pid = p.ppid;
310
+ }
311
+ return false;
312
+ }
313
+
271
314
  export async function self() {
272
315
  // OPENMSG_SELF can hold an alias, such as "codex:openmsg-33ce". An alias is
273
316
  // not a session id, so openmsg looks the session up. Without this step, a
@@ -291,7 +334,7 @@ export async function self() {
291
334
  }
292
335
  if (process.env.CLAUDE_CODE_MESSAGING_SOCKET) {
293
336
  const me = claudeAgents().find((a) => a.transport.path === process.env.CLAUDE_CODE_MESSAGING_SOCKET);
294
- if (me) return me;
337
+ if (me && (await runsUnder(me.pid))) return me;
295
338
  }
296
339
  const line = await ancestors(process.ppid);
297
340
  let [codex, opencode] = await Promise.all([codexAgents(), opencodeAgents()]);