viber-channel 0.7.2 → 0.8.0

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.
@@ -841,14 +841,23 @@ export function buildAgentIdentityInstructions(opts: {
841
841
 
842
842
  /**
843
843
  * Read the agent identity from the spawn env and build the identity line.
844
- * `VIBER_AGENT_NAME_EXPLICIT === "1"` gates whether `VIBER_CODEX_BRIDGE_LABEL`
845
- * is treated as an explicit name (the label is always set — even for auto ids —
846
- * so the separate flag is the only safe signal). Shared by every bridge.
844
+ * `VIBER_AGENT_NAME_EXPLICIT === "1"` gates whether the spawn label is treated as
845
+ * an explicit name (the label is always set — even for auto ids — so the separate
846
+ * flag is the only safe signal). Shared by every runtime:
847
+ * - the codex/gemma bridges set `VIBER_CODEX_BRIDGE_LABEL`;
848
+ * - a Claude terminal session (#328) has no bridge — its label is
849
+ * `VIBER_CHANNEL_LABEL` (set by buildClaudeLaunchSpec) — so fall back to it.
847
850
  */
848
851
  export function agentIdentityFromEnv(): string {
849
852
  const explicit = process.env.VIBER_AGENT_NAME_EXPLICIT === "1";
853
+ // A BLANK bridge label counts as absent (defense in depth, #328): the terminal
854
+ // launcher wipes an inherited VIBER_CODEX_BRIDGE_LABEL to "", so a Claude
855
+ // session falls through to its own VIBER_CHANNEL_LABEL instead of a leaked
856
+ // parent bridge name.
857
+ const bridgeLabel = process.env.VIBER_CODEX_BRIDGE_LABEL?.trim();
858
+ const label = bridgeLabel || process.env.VIBER_CHANNEL_LABEL;
850
859
  return buildAgentIdentityInstructions({
851
- explicitName: explicit ? process.env.VIBER_CODEX_BRIDGE_LABEL : undefined,
860
+ explicitName: explicit ? label : undefined,
852
861
  role: process.env.VIBER_AGENT_ROLE,
853
862
  });
854
863
  }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Control-plane capabilities help (#332).
3
+ *
4
+ * An agent connected via the channel does not otherwise know that a `vibe-master`
5
+ * control plane exists on the machine, nor how to spawn other agents with it. Per
6
+ * the issue + JP: this must be DISCOVERABLE ON DEMAND, never injected into every
7
+ * conversation. So the channel exposes a `capabilities` MCP tool that returns
8
+ * this text only when the agent chooses to call it; the channel instructions
9
+ * carry just a one-line pointer to the tool, not its contents.
10
+ *
11
+ * Pure (no I/O) so it is trivially testable and identical across hosts.
12
+ */
13
+
14
+ export function capabilitiesText(): string {
15
+ return [
16
+ "Viber control plane (vibe-master) — orchestrate other agents from this machine.",
17
+ "",
18
+ "vibe-master is a CLI + TUI (installed on the operator machine) that spawns and",
19
+ "supervises agents which appear in the Viber web UI and answer in their own",
20
+ "conversations. Use it when you want to delegate to another agent (e.g. a Codex",
21
+ "reviewer, a second Claude, or a Gemma chat).",
22
+ "",
23
+ "Runtimes: codex (background, via vctl) · gemma (background) · claude (terminal).",
24
+ "(--runtime defaults to codex. Gemma is conversational/read-only only — it",
25
+ " rejects --permission read-write.)",
26
+ "",
27
+ "Spawn one agent (CLI, non-interactive):",
28
+ " vibe-master spawn --permission <read-only|read-write> \\",
29
+ " [--runtime codex|claude|gemma] [--name <id>]",
30
+ " → only --permission is required; --runtime and --name are optional",
31
+ " (--name auto-generated if omitted). Prints the agent id; it registers",
32
+ " in Viber and comes online.",
33
+ "",
34
+ "Interactive: `vibe-master tui` (menu: + Add agent → runtime → permission → name).",
35
+ "Inspect / stop: `vibe-master list` · `vibe-master kill <id>`.",
36
+ "",
37
+ "Talk to a spawned agent from here with list_agents + message_agent.",
38
+ "Requires vibe-master installed on the machine (see the Install page on the web).",
39
+ "Run `vibe-master --help` for the full command reference.",
40
+ ].join("\n");
41
+ }
package/lib/lockfile.ts CHANGED
@@ -14,8 +14,8 @@
14
14
  * axis that isolates them for a normal bunx client, where sessionId is always
15
15
  * "" (the #259 sessionId axis below provides no isolation there).
16
16
  *
17
- * #259 adds the per-instance axis: when the launcher (e.g. viber-dev.ps1)
18
- * supplies a per-launch session id via VIBER_CHANNEL_SESSION_ID, the lock is
17
+ * #259 adds the per-instance axis: when a launcher supplies a per-launch session
18
+ * id via VIBER_CHANNEL_SESSION_ID, the lock is
19
19
  * additionally namespaced by it, so two concurrent launches of the *same*
20
20
  * project each get their own lock. Channel respawns within one launch inherit
21
21
  * the same env var and reuse the same lock — accidental duplicates are still
@@ -0,0 +1,124 @@
1
+ /**
2
+ * viber-channel stale-version guard (#330).
3
+ *
4
+ * A globally-installed `viber-channel` that a user placed by hand (old docs) can
5
+ * fall far behind npm `latest`. When a later refactor removes/renames a lib file,
6
+ * that stale install crashes with a raw `Cannot find module './lib/…'` instead of
7
+ * anything actionable. We can't fix an ALREADY-broken old install from here (its
8
+ * code predates this guard), so the useful move is PREVENTION: on every startup
9
+ * that still runs, check whether the installed version is behind `latest` and, if
10
+ * so, print a clear one-line nudge to update — BEFORE the user ever hits a
11
+ * breaking refactor. Best-effort and non-fatal: any error (offline, timeout, odd
12
+ * registry response) is swallowed so the channel starts normally.
13
+ */
14
+
15
+ import { readFileSync } from "node:fs";
16
+ import { join } from "node:path";
17
+
18
+ const NPM_LATEST_URL = "https://registry.npmjs.org/viber-channel/latest";
19
+ const DEFAULT_TIMEOUT_MS = 1500;
20
+
21
+ /** Parse a `major.minor.patch` string to a numeric tuple; non-numeric → 0. */
22
+ function parseVersion(v: string): [number, number, number] {
23
+ const parts = v
24
+ .trim()
25
+ .replace(/^v/, "")
26
+ .split(".")
27
+ .map((p) => Number.parseInt(p, 10));
28
+ return [parts[0] || 0, parts[1] || 0, parts[2] || 0];
29
+ }
30
+
31
+ /** True iff `local` is strictly older than `latest` (major.minor.patch order). */
32
+ export function isOlder(local: string, latest: string): boolean {
33
+ const a = parseVersion(local);
34
+ const b = parseVersion(latest);
35
+ for (let i = 0; i < 3; i++) {
36
+ if (a[i] < b[i]) return true;
37
+ if (a[i] > b[i]) return false;
38
+ }
39
+ return false;
40
+ }
41
+
42
+ /** Read this package's own version from its package.json (best-effort → null). */
43
+ export function readLocalVersion(
44
+ readFile: (p: string) => string = (p) => readFileSync(p, "utf-8"),
45
+ dir: string = import.meta.dir,
46
+ ): string | null {
47
+ try {
48
+ const pkg = JSON.parse(readFile(join(dir, "..", "package.json")));
49
+ return typeof pkg.version === "string" ? pkg.version : null;
50
+ } catch {
51
+ return null;
52
+ }
53
+ }
54
+
55
+ /** Fetch npm `latest` version with a short timeout; null on any failure. */
56
+ export async function fetchLatestVersion(
57
+ timeoutMs: number = DEFAULT_TIMEOUT_MS,
58
+ fetchImpl: typeof fetch = fetch,
59
+ ): Promise<string | null> {
60
+ const ctrl = new AbortController();
61
+ const timer = setTimeout(() => ctrl.abort(), timeoutMs);
62
+ try {
63
+ const res = await fetchImpl(NPM_LATEST_URL, { signal: ctrl.signal });
64
+ if (!res.ok) return null;
65
+ const body = (await res.json()) as { version?: unknown };
66
+ return typeof body.version === "string" ? body.version : null;
67
+ } catch {
68
+ return null;
69
+ } finally {
70
+ clearTimeout(timer);
71
+ }
72
+ }
73
+
74
+ export interface StaleCheck {
75
+ local: string;
76
+ latest: string;
77
+ stale: boolean;
78
+ }
79
+
80
+ /** Compare the installed version against npm latest. Null when either is unknown. */
81
+ export async function checkStaleVersion(opts?: {
82
+ localVersion?: string | null;
83
+ fetchLatest?: () => Promise<string | null>;
84
+ }): Promise<StaleCheck | null> {
85
+ // Honor an EXPLICIT localVersion (incl. null = "unknown"); only read the real
86
+ // package.json when the caller didn't pass the key at all.
87
+ const local =
88
+ opts && "localVersion" in opts ? opts.localVersion : readLocalVersion();
89
+ if (!local) return null;
90
+ const latest = opts?.fetchLatest
91
+ ? await opts.fetchLatest()
92
+ : await fetchLatestVersion();
93
+ if (!latest) return null;
94
+ return { local, latest, stale: isOlder(local, latest) };
95
+ }
96
+
97
+ /** The one-line nudge printed to stderr when the install is behind. */
98
+ export function staleWarning(local: string, latest: string): string {
99
+ return (
100
+ `[viber-channel] version ${local} is behind latest ${latest}. ` +
101
+ `Update to avoid a broken install: bun add -g viber-channel@latest`
102
+ );
103
+ }
104
+
105
+ /**
106
+ * Best-effort: warn on stderr when this install is behind npm latest. Never
107
+ * throws, never blocks longer than the fetch timeout. Called at startup.
108
+ */
109
+ export async function warnIfStale(opts?: {
110
+ localVersion?: string | null;
111
+ fetchLatest?: () => Promise<string | null>;
112
+ write?: (msg: string) => void;
113
+ }): Promise<void> {
114
+ try {
115
+ const check = await checkStaleVersion(opts);
116
+ if (check?.stale) {
117
+ (opts?.write ?? ((m) => process.stderr.write(`${m}\n`)))(
118
+ staleWarning(check.local, check.latest),
119
+ );
120
+ }
121
+ } catch {
122
+ /* best-effort — never break startup on a version check */
123
+ }
124
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "viber-channel",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "description": "Voice + text MCP channel between a Claude Code session and the Viber UI (https://viber.dgypx.dev). Push transcripts to Claude; send_message tool delivers text back to the UI.",
5
5
  "type": "module",
6
6
  "bin": {
package/viber-channel.ts CHANGED
@@ -16,9 +16,9 @@
16
16
  *
17
17
  * claude --dangerously-load-development-channels server:<name>
18
18
  *
19
- * Two PowerShell launchers wrap the right flag:
20
- * - viber.ps1 → server:viber-channel (staging)
21
- * - viber-dev.ps1 → server:viber-dev-channel (this repo, dev)
19
+ * For dev in this repo, vibe-master-dev.ps1 (the multi-agent TUI launcher) wraps
20
+ * the flag: `server:viber-dev-channel` against the dev backend. Staging sessions
21
+ * use the plain user-scoped `viber-channel` registration with no launcher flag.
22
22
  *
23
23
  * CLI subcommand: `bunx viber-channel connect <claim_url>` runs the one-shot
24
24
  * claim flow that writes .viber/auth.json without requiring any Claude Code
@@ -54,6 +54,9 @@ import {
54
54
  type TokenRefreshScheduler,
55
55
  } from "./lib/token_refresh.ts";
56
56
  import { awaitStableStartup } from "./lib/startup_gate.ts";
57
+ import { agentIdentityFromEnv } from "./lib/bridge_core.ts";
58
+ import { warnIfStale } from "./lib/version_check.ts";
59
+ import { capabilitiesText } from "./lib/capabilities.ts";
57
60
 
58
61
  // ---- CLI subcommand dispatch (must happen before lock acquire + loadAuth) ----
59
62
  //
@@ -98,6 +101,11 @@ import { awaitStableStartup } from "./lib/startup_gate.ts";
98
101
  );
99
102
  process.exit(1);
100
103
  }
104
+ // #330: this is the exact path that crashed on a stale hand-placed install
105
+ // (`bunx viber-channel connect` → missing ./lib/peers.ts). Await the check
106
+ // here so the "you're behind, update" nudge shows BEFORE the connect work
107
+ // (best-effort — never throws, short timeout).
108
+ await warnIfStale();
101
109
  try {
102
110
  await runConnect(claimUrl);
103
111
  process.exit(0);
@@ -108,6 +116,11 @@ import { awaitStableStartup } from "./lib/startup_gate.ts";
108
116
  }
109
117
  }
110
118
 
119
+ // Server path (fell through the dispatch block): warn if this install is behind
120
+ // npm latest, but fire-and-forget so it never delays mcp.connect — the nudge
121
+ // lands on stderr shortly after startup (#330).
122
+ void warnIfStale();
123
+
111
124
  // Lock file path: %APPDATA%/viber/ (Windows) or ~/.config/viber/ (Linux/Mac).
112
125
  // Namespaced by (VIBER_BASE_URL, client_fingerprint, VIBER_CHANNEL_SESSION_ID) so:
113
126
  // - channels at different backends (staging + dev) coexist (different base_url);
@@ -340,18 +353,32 @@ const mcp = new Server(
340
353
  experimental: { "claude/channel": {} },
341
354
  tools: {},
342
355
  },
356
+ // #369 truncation fix: Claude Code truncates long MCP `instructions`
357
+ // ("…[truncated]") and this blob was ~3 KB → the TAIL was dropped before the
358
+ // agent saw it (that hid the agent's name #328, and threatened the #332
359
+ // pointer + exit-intent). So: CRITICAL lines are FRONT-LOADED (identity →
360
+ // core reply → actions/questions → anti-deadlock → capabilities → exit) and
361
+ // the nice-to-have style notes live at the tail where a cut is harmless.
362
+ // Condensed vs the old prose (same substance — reviewed). Identity is "" for
363
+ // JP's own session (no explicit-name flag) → the identity LINE is filtered
364
+ // out for him; the rest of the (now shorter, reordered) blob applies to every
365
+ // session including his.
343
366
  instructions: [
344
- 'Voice transcripts arrive as <channel source="viber-channel"> events carrying the user\'s microphone speech.',
345
- "Reply with send_message. You are talking to a human who both LISTENS (the `text` is read aloud by TTS) AND READS it on screen (the `text` is ALSO rendered as Markdown in the UI). So aim for both: it must sound natural read aloud AND be easy to read. Lead with the answer, no preamble or recap.",
346
- "USE light Markdown in `text` when it makes the message easier to read: short bullet lists (`- `), numbered steps (`1.`), checklists, **bold** for key terms. A short list reads aloud just fine — a spoken list is natural. Don't cram several points into one dense paragraph; break them out. Avoid heavy/awkward-to-speak markup (big tables, code blocks, long fenced snippets) in `text` — that belongs in the artifact.",
347
- "ACTIONS and QUESTIONS belong in `text`, made visible — never bury them in the artifact. If you need a decision, ask it clearly in `text` (a short bullet list of the options is good); the artifact may hold supporting detail but the question itself must be in `text` where it jumps out.",
348
- "The `artifact` is for HEAVY or LONG content only: big code, large tables, long multi-point analyses, review dumps, JSON, file lists. When you use an artifact, keep `text` a brief spoken summary that points to it ('details on the side') — don't read the heavy content aloud.",
349
- "Write identifiers LITERALLY with their real characters (288, auth.json, viber-dev.dgypx.dev, CONVERSATION_TOKEN) — never spell them out as 'point'/'dash' and never as awkward digit-by-digit text. A lone long token or path is better placed in the artifact than spoken.",
350
- "Artifact formats: `markdown` (default), `code`, `json`, `html`. Use `html` only when the rendering needs structural HTML Markdown can't express (styled cards, badges, mixed layouts). Scripts and event handlers are stripped server-side; don't send executable JS.",
351
- "NO EMOJI in `text` — it is read aloud, so an emoji becomes spoken noise (a check mark is read as 'check mark'). Use plain Markdown (bullets, bold) for structure instead. Emoji are tolerable only inside an artifact (not spoken).",
352
- "Rule of thumb: short and conversational, but readable — break points into a list rather than a wall of text; reserve the artifact for what is genuinely long or heavy.",
367
+ agentIdentityFromEnv(),
368
+ // --- critical: front-loaded so truncation can never drop them ---
369
+ 'Voice transcripts arrive as <channel source="viber-channel"> events (the user\'s microphone speech). Reply with send_message. The `text` is BOTH read aloud (TTS) AND shown as Markdown — make it natural aloud AND easy to read; lead with the answer, no preamble.',
370
+ "Put ACTIONS and QUESTIONS in `text`, visibly — never bury them in the artifact. Use light Markdown (short bullet/numbered lists, **bold**) to stay readable; a short spoken list is fine.",
371
+ // #343 anti-deadlock (hands-free: the user is NOT watching the terminal).
372
+ "CRITICAL — while this channel is active, NEVER block on a terminal prompt or AskUserQuestion: the user is hands-free and cannot see the terminal, so it deadlocks. Put EVERY question or choice in send_message `text` and take the answer from the next voice transcript.",
373
+ // #332 discoverability pointer (the how-to lives in the capabilities tool).
374
+ "To orchestrate or spawn OTHER agents (Codex, Claude, Gemma) on this machine, call the `capabilities` tool for how — only when relevant.",
353
375
  "On exit intent (bye, au revoir, stop) call stop_conversation(), speak a brief farewell, and stop.",
354
- ].join(" "),
376
+ // --- nice-to-have style notes (safe near the tail) ---
377
+ "The `artifact` (format: markdown default, or code/json/html) is for HEAVY/LONG content (big code, large tables, long analyses, JSON, file lists); keep `text` a brief spoken summary that points to it ('details on the side'). Scripts/event handlers are stripped server-side.",
378
+ "NO EMOJI in `text` (read aloud — an emoji becomes spoken noise). Write identifiers LITERALLY (288, auth.json, viber-dev.dgypx.dev) — never spell out dots/dashes; a lone long token or path is better placed in the artifact.",
379
+ ]
380
+ .filter((line) => line.length > 0)
381
+ .join(" "),
355
382
  }
356
383
  );
357
384
 
@@ -438,6 +465,20 @@ mcp.setRequestHandler(ListToolsRequestSchema, async () => ({
438
465
  additionalProperties: false,
439
466
  },
440
467
  },
468
+ {
469
+ name: "capabilities",
470
+ description:
471
+ "On-demand: learn what the Viber control plane (vibe-master) can do on this " +
472
+ "machine — how to spawn and supervise OTHER agents (Codex, Claude, Gemma). " +
473
+ "Call this only when orchestrating other agents is relevant; it is not part " +
474
+ "of the default context. Returns a short usage reference.",
475
+ inputSchema: {
476
+ type: "object" as const,
477
+ properties: {},
478
+ required: [],
479
+ additionalProperties: false,
480
+ },
481
+ },
441
482
  ],
442
483
  }));
443
484
 
@@ -478,6 +519,11 @@ mcp.setRequestHandler(CallToolRequestSchema, async (request) => {
478
519
  return libMessageAgent(channelToolsCtx, args);
479
520
  }
480
521
 
522
+ // #332: on-demand control-plane discovery — static help, no runtime state.
523
+ if (request.params.name === "capabilities") {
524
+ return { content: [{ type: "text" as const, text: capabilitiesText() }] };
525
+ }
526
+
481
527
  if (request.params.name !== "send_message") {
482
528
  return {
483
529
  isError: true,
@@ -829,7 +875,7 @@ try {
829
875
  // ---- Channel ready banner ----
830
876
 
831
877
  process.stderr.write(
832
- `[viber-channel] Channel ready: 3 tools (send_message, list_agents, message_agent), SSE on /api/conversations/${CONVERSATION_ID}/events\n`
878
+ `[viber-channel] Channel ready: 4 tools (send_message, list_agents, message_agent, capabilities), SSE on /api/conversations/${CONVERSATION_ID}/events\n`
833
879
  );
834
880
 
835
881
  // Channel liveness is proven by the INSTANCE heartbeat (#311, started above), not