@phren/cli 0.3.0 → 0.3.2

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.
Files changed (64) hide show
  1. package/dist/bridge/agent-hooks.d.ts +20 -6
  2. package/dist/bridge/agent-hooks.js +74 -24
  3. package/dist/bridge/claude-question-dialog.d.ts +65 -0
  4. package/dist/bridge/claude-question-dialog.js +210 -0
  5. package/dist/bridge/command.d.ts +3 -2
  6. package/dist/bridge/command.js +97 -5
  7. package/dist/bridge/computers.d.ts +5 -0
  8. package/dist/bridge/computers.js +10 -4
  9. package/dist/bridge/conductor-group.d.ts +23 -0
  10. package/dist/bridge/conductor-group.js +25 -0
  11. package/dist/bridge/hand-off.js +5 -1
  12. package/dist/bridge/herdr.d.ts +7 -0
  13. package/dist/bridge/herdr.js +53 -2
  14. package/dist/bridge/link.d.ts +48 -0
  15. package/dist/bridge/link.js +136 -0
  16. package/dist/bridge/memory-store.d.ts +23 -0
  17. package/dist/bridge/memory-store.js +152 -0
  18. package/dist/bridge/pair.d.ts +46 -0
  19. package/dist/bridge/pair.js +240 -0
  20. package/dist/bridge/peers.d.ts +9 -0
  21. package/dist/bridge/peers.js +25 -2
  22. package/dist/bridge/push.d.ts +42 -3
  23. package/dist/bridge/push.js +90 -20
  24. package/dist/bridge/server-launch.d.ts +9 -0
  25. package/dist/bridge/server-launch.js +34 -11
  26. package/dist/bridge/server-pane-routes.js +45 -8
  27. package/dist/bridge/server-routes.d.ts +1 -0
  28. package/dist/bridge/server-routes.js +11 -3
  29. package/dist/bridge/server-stream.js +4 -1
  30. package/dist/bridge/speech-key.d.ts +33 -0
  31. package/dist/bridge/speech-key.js +106 -0
  32. package/dist/bridge/speech-transcribe.js +1 -1
  33. package/dist/bridge/speech.d.ts +7 -3
  34. package/dist/bridge/speech.js +36 -17
  35. package/dist/bridge-hook.mjs +1356 -514
  36. package/dist/cli/namespaces-store.js +21 -0
  37. package/dist/cli-handlers.js +6 -0
  38. package/dist/cli-registry.js +14 -2
  39. package/dist/content/learning.js +1 -1
  40. package/dist/data/tasks.d.ts +26 -0
  41. package/dist/data/tasks.js +72 -1
  42. package/dist/entrypoint.js +7 -0
  43. package/dist/governance/rbac.d.ts +1 -1
  44. package/dist/governance/rbac.js +1 -0
  45. package/dist/init/init-walkthrough.d.ts +10 -0
  46. package/dist/init/init-walkthrough.js +25 -2
  47. package/dist/init/init.d.ts +5 -0
  48. package/dist/init/init.js +54 -32
  49. package/dist/link/doctor.js +10 -0
  50. package/dist/link/link.js +9 -3
  51. package/dist/link/refresh.js +11 -10
  52. package/dist/mcp/profile.js +2 -1
  53. package/dist/modules/config.d.ts +7 -0
  54. package/dist/modules/config.js +17 -0
  55. package/dist/modules/registry.js +5 -4
  56. package/dist/sync/conflict-resolve.d.ts +1 -1
  57. package/dist/sync/conflict-resolve.js +44 -7
  58. package/dist/sync/task-claim.d.ts +22 -0
  59. package/dist/sync/task-claim.js +45 -0
  60. package/dist/tools/dispatch.js +2 -2
  61. package/dist/tools/search.js +1 -1
  62. package/dist/tools/tasks.js +40 -1
  63. package/package.json +2 -1
  64. package/starter/global/skills/conductor/SKILL.md +41 -9
@@ -78,6 +78,31 @@ export function herdrWorktreeWorkers(s) {
78
78
  * queue, after which the option key (or typed text) is the answer. */
79
79
  const ANSWER_KEYS = ["Escape", "Enter", "Up", "Down", "Tab", "AltUp", "y", "n", "p", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
80
80
  const HERDR_KEYS = { Escape: "esc", Enter: "enter", Up: "up", Down: "down", Tab: "tab", AltUp: "alt+Up" };
81
+ // A question or label may wrap onto several lines; the pane shows them joined.
82
+ const questionText = z.string().trim().min(1).max(4000).refine(t => !/[\x00-\x08\x0b-\x1f\x7f]/.test(t));
83
+ const claudeQuestionBody = z.object({
84
+ questions: z.array(z.object({ question: questionText, multiSelect: z.boolean().optional(), options: z.array(questionText).min(1).max(8) })).min(1).max(8),
85
+ answers: z.array(z.object({ optionIndexes: z.array(z.number().int().nonnegative()).max(8),
86
+ text: z.string().max(1000).refine(t => !/[\x00-\x1f\x7f]/.test(t)).optional() })).min(1).max(8),
87
+ });
88
+ /** The phone's answer to Claude's AskUserQuestion: one answer per question,
89
+ * a single option or a typed answer for a single-select question, any
90
+ * options plus an optional typed answer for a multi-select one. */
91
+ function claudeQuestionAnswer(data) {
92
+ const body = claudeQuestionBody.parse(data);
93
+ if (body.answers.length !== body.questions.length)
94
+ throw new BridgeError(400, "Answer every question.");
95
+ const answers = body.answers.map((answer, index) => {
96
+ const question = body.questions[index], options = [...new Set(answer.optionIndexes)], text = answer.text?.trim() ?? "";
97
+ if (options.some(option => option >= question.options.length))
98
+ throw new BridgeError(400, "Choose an available answer.");
99
+ if (question.multiSelect ? !options.length && !text : options.length + (text ? 1 : 0) !== 1) {
100
+ throw new BridgeError(400, question.multiSelect ? "Choose at least one answer." : "Choose one answer for each question.");
101
+ }
102
+ return { options, ...(text ? { text } : {}) };
103
+ });
104
+ return { questions: body.questions.map(q => ({ question: q.question, multiSelect: q.multiSelect, options: q.options.map(label => ({ label })) })), answers };
105
+ }
81
106
  /** A secret typed into a terminal prompt: printable, bounded, never logged. */
82
107
  const secretText = z.string().min(1).max(256).refine(t => !/[\x00-\x1f\x7f]/.test(t));
83
108
  /** An idle agent that has not taken typed text within the first wait
@@ -339,17 +364,21 @@ export async function paneRoute(ctx, url, data, response) {
339
364
  // not hold) is being answered even when Herdr reads the pane as
340
365
  // working or idle; Herdr's status lags the agent's own dialog.
341
366
  const holding = menu || !!agentHooks.terminalPrompt(target);
367
+ // Esc on a question the Hook still holds declines it at once, whatever
368
+ // Herdr reads the pane as: an esc typed into the held pane does nothing.
369
+ const cancelled = keys.every(key => key === "Escape") && agentHooks.cancelHeldQuestion(target);
342
370
  // Escape interrupts a working agent. Everything else answers a
343
371
  // prompt the agent is holding: a menu, a y/n, a trust question.
344
- if (!holding && (keys.every(key => key === "Escape") ? !["working", "blocked", "waiting", "unknown"].includes(status)
372
+ if (!cancelled && !holding && (keys.every(key => key === "Escape") ? !["working", "blocked", "waiting", "unknown"].includes(status)
345
373
  : !["blocked", "waiting", "unknown"].includes(status)))
346
374
  throw new BridgeError(409, keys.every(key => key === "Escape") ? "This agent is no longer working." : "This agent is not waiting for an answer.");
347
- // A released AskUserQuestion is answered one question at a time:
348
- // the Hook sends the chosen digit, then Tab to advance or Enter
349
- // after the last, and clears the prompt when the set is done.
350
- const question = agentHooks.questionAnswerKeys(target, keys);
351
- if (question) {
352
- await rpc(target.server, "agent.send_keys", { target: target.pane, keys: question.map(key => HERDR_KEYS[key] ?? key) });
375
+ if (cancelled) {
376
+ result = { ok: true };
377
+ // An older phone answers a released AskUserQuestion one question at
378
+ // a time with its digits; the Hook walks that question in the pane
379
+ // and submits the set after the last.
380
+ }
381
+ else if (await agentHooks.answerReleasedQuestion(target, keys)) {
353
382
  result = { ok: true };
354
383
  }
355
384
  else {
@@ -469,7 +498,15 @@ export async function paneRoute(ctx, url, data, response) {
469
498
  result = { ok: true };
470
499
  }
471
500
  else if (url.pathname === "/v1/questions/answer") {
472
- await codexQuestions.answer(target, data);
501
+ if (target.source === "claude") {
502
+ // Claude's AskUserQuestion, answered in its terminal dialog: the
503
+ // phone names the questions it shows, and the Hook answers only a
504
+ // pane that is drawing exactly those.
505
+ const { questions, answers } = claudeQuestionAnswer(data);
506
+ await agentHooks.answerClaudeQuestions(target, questions, answers);
507
+ }
508
+ else
509
+ await codexQuestions.answer(target, data);
473
510
  result = { ok: true };
474
511
  }
475
512
  else
@@ -93,6 +93,7 @@ export declare const capabilities: {
93
93
  overviewStream: boolean;
94
94
  speech: boolean;
95
95
  transcribe: boolean;
96
+ memoryStore: boolean;
96
97
  };
97
98
  export declare function capabilitiesForModules(snapshot: ModuleSnapshot): Record<string, unknown>;
98
99
  export declare function requireRoute(snapshot: ModuleSnapshot, method: string, route: string): void;
@@ -13,6 +13,7 @@ import { optionalHookPeers, peerRequest } from "./peers.js";
13
13
  import { candidateRepos, enrollProject } from "./enroll.js";
14
14
  import { browseFiles } from "./files.js";
15
15
  import { MAX_FILE_RANGE, rangeInteger, readFileRange } from "./file-range.js";
16
+ import { storeRoute } from "./memory-store.js";
16
17
  import { paneChatState, panes, servers, snapshot, validateTarget, workspaceSnapshot } from "./herdr.js";
17
18
  import { locateProject } from "./locate.js";
18
19
  import { gitRoot, launchDirectory, repositoryBranch, webServers } from "./projects.js";
@@ -25,7 +26,7 @@ import { healthDetails, listsCaller } from "./health.js";
25
26
  import { defaultPhrenPath } from "../shared.js";
26
27
  import { loadCodePackage, loadedFrom } from "../modules/code-package.js";
27
28
  import { gitRepository, paneRoute, uploadBody } from "./server-pane-routes.js";
28
- import { launchSession, workspaceAction } from "./server-launch.js";
29
+ import { launchSession, localConductor, workspaceAction } from "./server-launch.js";
29
30
  import { hookMetrics } from "./metrics.js";
30
31
  import { streamSpeech } from "./speech.js";
31
32
  const CHILD_ACTIVITY_CACHE_MS = 5_000;
@@ -50,7 +51,7 @@ async function childActivity(source, session) {
50
51
  export const capabilities = { transcript: true, progress: true, images: true, prompt: true, stop: true,
51
52
  terminal: "ssh-pty", shell: "ssh-pty", herdr: true, diff: true, webServers: true, webPreview: "ssh-exec", activity: true,
52
53
  approvals: true, questions: false, accountUsage: true, providers: ["codex", "claude", "copilot", "opencode"],
53
- files: true, repositoryFiles: true, subagents: true, sideQuestions: true, dispatch: true, approvalPush: "direct-apns", simulators: process.platform === "darwin", code: true, overviewStream: true, speech: true, transcribe: true };
54
+ files: true, repositoryFiles: true, subagents: true, sideQuestions: true, dispatch: true, approvalPush: "direct-apns", simulators: process.platform === "darwin", code: true, overviewStream: true, speech: true, transcribe: true, memoryStore: true };
54
55
  export function capabilitiesForModules(snapshot) {
55
56
  const allowed = new Set(snapshot.modules.flatMap(module => module.capabilities));
56
57
  const result = Object.fromEntries(Object.entries(capabilities).filter(([name]) => allowed.has(name)));
@@ -182,7 +183,10 @@ export function createRouteHandler(ctx) {
182
183
  throw new BridgeError(400, "Invalid request origin.");
183
184
  requireRoute(modules, request.method ?? "", url.pathname);
184
185
  let result;
185
- if (request.method === "GET") {
186
+ if (url.pathname.startsWith("/v1/store/")) {
187
+ result = await storeRoute(modules.store, request.method ?? "", url, request.method === "POST" ? await body(request) : undefined);
188
+ }
189
+ else if (request.method === "GET") {
186
190
  switch (url.pathname) {
187
191
  case "/v1/health":
188
192
  result = { ...info, codePackage: await codePackageStatus(scheduleStore, modules.has("code")) };
@@ -207,6 +211,10 @@ export function createRouteHandler(ctx) {
207
211
  case "/v1/conductor/grants":
208
212
  result = { grants: await listGrants() };
209
213
  break;
214
+ // Asked by linked peers before they start a conductor: one per connected group.
215
+ case "/v1/conductor":
216
+ result = { computer: info.computer, conductor: await localConductor() ?? null };
217
+ break;
210
218
  case "/v1/dispatch/capacity": {
211
219
  const live = await servers();
212
220
  const snapshots = await Promise.all(live.map(server => snapshot(String(server.session))));
@@ -175,7 +175,10 @@ export function transcriptStreams(ctx) {
175
175
  compacting: agentHooks.compacting(target),
176
176
  ...(historyHealth.stalled ? { historyStalled: true, historyStalledSince: historyHealth.since } : {}),
177
177
  modules: info.modules, store: info.store, profile: info.profile, generation: info.generation,
178
- capabilities: { ...activeCapabilities, asyncQuestions: target.source === "codex" && codexQuestions.available }, branch } });
178
+ capabilities: { ...activeCapabilities, asyncQuestions: target.source === "codex" && codexQuestions.available,
179
+ // Claude's AskUserQuestion is answered in its terminal dialog
180
+ // through /v1/questions/answer, whether or not a hold caught it.
181
+ ...(target.source === "claude" ? { questions: true } : {}) }, branch } });
179
182
  }
180
183
  if (sideAnswers && sideQuestions) {
181
184
  for (const { revision, ...side } of sideQuestions.list(target)) {
@@ -0,0 +1,33 @@
1
+ /** The ElevenLabs key for spoken replies and Scribe dictation. It is machine
2
+ * config, like apns.json: it lives in the Hook's own directory, mode 600, and
3
+ * never in the synced store. ELEVENLABS_API_KEY, ElevenLabs' own convention
4
+ * that its SDKs and MCP server read, wins when set. The Hook runs as a service
5
+ * that doesn't see shell env, so the file is the durable source. */
6
+ export declare const SPEECH_KEY_ENV = "ELEVENLABS_API_KEY";
7
+ export type SpeechKeySource = "environment" | "file";
8
+ export declare function speechKeyFile(): string;
9
+ /** Where the key lived before phren kept its own: the mina trailer config. It
10
+ * is read once to migrate and never written or deleted. */
11
+ export declare function legacySpeechKeyFile(env?: NodeJS.ProcessEnv): string;
12
+ export declare function writeSpeechKey(key: string, file?: string): Promise<void>;
13
+ export interface SpeechKeyPaths {
14
+ env?: NodeJS.ProcessEnv;
15
+ file?: string;
16
+ legacy?: string;
17
+ }
18
+ /** ELEVENLABS_API_KEY, else the stored key. When nothing is stored yet and the
19
+ * mina trailer config has a key, it is copied here once and read from here
20
+ * after. */
21
+ export declare function resolveSpeechKey(paths?: SpeechKeyPaths): Promise<{
22
+ key: string;
23
+ source: SpeechKeySource;
24
+ } | undefined>;
25
+ export declare function readSpeechKey(): Promise<string | undefined>;
26
+ export interface SpeechKeyStatus {
27
+ configured: boolean;
28
+ detail: string;
29
+ problem?: boolean;
30
+ }
31
+ /** Whether this computer has a key, for doctor. Never the key itself, and it
32
+ * doesn't migrate: a read-only check. */
33
+ export declare function speechKeyStatus(paths?: SpeechKeyPaths): Promise<SpeechKeyStatus>;
@@ -0,0 +1,106 @@
1
+ import { mkdir, readFile, stat } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { homeDir } from "../home-paths.js";
4
+ import { atomic, bridgeRoot } from "./protocol.js";
5
+ /** The ElevenLabs key for spoken replies and Scribe dictation. It is machine
6
+ * config, like apns.json: it lives in the Hook's own directory, mode 600, and
7
+ * never in the synced store. ELEVENLABS_API_KEY, ElevenLabs' own convention
8
+ * that its SDKs and MCP server read, wins when set. The Hook runs as a service
9
+ * that doesn't see shell env, so the file is the durable source. */
10
+ export const SPEECH_KEY_ENV = "ELEVENLABS_API_KEY";
11
+ export function speechKeyFile() {
12
+ return path.join(bridgeRoot(), "elevenlabs.json");
13
+ }
14
+ /** Where the key lived before phren kept its own: the mina trailer config. It
15
+ * is read once to migrate and never written or deleted. */
16
+ export function legacySpeechKeyFile(env = process.env) {
17
+ return path.join(homeDir(env), ".config", "mina-trailer.json");
18
+ }
19
+ function clean(value) {
20
+ return typeof value === "string" && value.trim() ? value.trim() : undefined;
21
+ }
22
+ /** The stored key. A file other users can read is refused, as apns.json is. */
23
+ async function readStored(file) {
24
+ let info;
25
+ try {
26
+ info = await stat(file);
27
+ }
28
+ catch {
29
+ return { state: "missing" };
30
+ }
31
+ if (!info.isFile())
32
+ return { state: "invalid" };
33
+ if (process.platform !== "win32" && ((info.mode & 0o077) !== 0 || (process.getuid && info.uid !== process.getuid())))
34
+ return { state: "unsafe" };
35
+ try {
36
+ const key = clean(JSON.parse(await readFile(file, "utf8")).apiKey);
37
+ return key ? { state: "ok", key } : { state: "invalid" };
38
+ }
39
+ catch {
40
+ return { state: "invalid" };
41
+ }
42
+ }
43
+ async function readLegacy(file) {
44
+ try {
45
+ return clean(JSON.parse(await readFile(file, "utf8")).elevenlabs_api_key);
46
+ }
47
+ catch {
48
+ return undefined;
49
+ }
50
+ }
51
+ export async function writeSpeechKey(key, file = speechKeyFile()) {
52
+ const value = clean(key);
53
+ if (!value)
54
+ throw new Error("The ElevenLabs key is empty.");
55
+ await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
56
+ await atomic(file, JSON.stringify({ apiKey: value }) + "\n", 0o600);
57
+ }
58
+ /** ELEVENLABS_API_KEY, else the stored key. When nothing is stored yet and the
59
+ * mina trailer config has a key, it is copied here once and read from here
60
+ * after. */
61
+ export async function resolveSpeechKey(paths = {}) {
62
+ const env = paths.env ?? process.env;
63
+ const fromEnv = clean(env[SPEECH_KEY_ENV]);
64
+ if (fromEnv)
65
+ return { key: fromEnv, source: "environment" };
66
+ const file = paths.file ?? speechKeyFile();
67
+ const stored = await readStored(file);
68
+ if (stored.state === "ok")
69
+ return { key: stored.key, source: "file" };
70
+ if (stored.state !== "missing")
71
+ return undefined;
72
+ const legacy = await readLegacy(paths.legacy ?? legacySpeechKeyFile(env));
73
+ if (!legacy)
74
+ return undefined;
75
+ try {
76
+ await writeSpeechKey(legacy, file);
77
+ }
78
+ catch { /* use it this once; the next call tries again */ }
79
+ return { key: legacy, source: "file" };
80
+ }
81
+ export async function readSpeechKey() {
82
+ return (await resolveSpeechKey())?.key;
83
+ }
84
+ /** Whether this computer has a key, for doctor. Never the key itself, and it
85
+ * doesn't migrate: a read-only check. */
86
+ export async function speechKeyStatus(paths = {}) {
87
+ const env = paths.env ?? process.env;
88
+ const file = paths.file ?? speechKeyFile();
89
+ const stored = await readStored(file);
90
+ const setup = "run `phren bridge speech-key set` and paste the key";
91
+ if (clean(env[SPEECH_KEY_ENV])) {
92
+ return stored.state === "ok"
93
+ ? { configured: true, detail: `${SPEECH_KEY_ENV} is set and a key is stored in ${file}` }
94
+ : { configured: true, detail: `${SPEECH_KEY_ENV} is set in this shell; the Hook service doesn't see shell env, so ${setup} to store it` };
95
+ }
96
+ switch (stored.state) {
97
+ case "ok": return { configured: true, detail: `stored in ${file}` };
98
+ case "unsafe": return { configured: false, problem: true, detail: `${file} is readable by other users; run chmod 600 on it` };
99
+ case "invalid": return { configured: false, problem: true, detail: `${file} has no apiKey; ${setup}` };
100
+ }
101
+ const legacy = paths.legacy ?? legacySpeechKeyFile(env);
102
+ if (await readLegacy(legacy)) {
103
+ return { configured: true, detail: `not stored yet; the Hook copies it from ${legacy} on first use` };
104
+ }
105
+ return { configured: false, detail: `no ElevenLabs key, so spoken replies and Scribe dictation are off; ${setup}` };
106
+ }
@@ -1,5 +1,5 @@
1
1
  import { WebSocket } from "ws";
2
- import { readSpeechKey } from "./speech.js";
2
+ import { readSpeechKey } from "./speech-key.js";
3
3
  /** Dictation through ElevenLabs Scribe v2 Realtime, for the phone's opt-in
4
4
  * Scribe input. The phone streams 16 kHz mono PCM as binary frames; this
5
5
  * computer forwards it to ElevenLabs with its own key, which never leaves
@@ -17,15 +17,19 @@ export declare const speechRequest: z.ZodObject<{
17
17
  }, z.core.$strip>;
18
18
  export interface SpeechOptions {
19
19
  fetch?: typeof fetch;
20
- /** Resolves the ElevenLabs key; defaults to ~/.config/mina-trailer.json. */
20
+ /** Resolves the ElevenLabs key; defaults to ELEVENLABS_API_KEY, then the stored key (speech-key.ts). */
21
21
  key?: () => Promise<string | undefined>;
22
22
  voice?: string;
23
23
  }
24
- export declare function speechKeyFile(env?: NodeJS.ProcessEnv): string;
25
- export declare function readSpeechKey(file?: string): Promise<string | undefined>;
26
24
  /** A fixed message per ElevenLabs failure: its own response text is never
27
25
  * passed on. */
28
26
  export declare function speechError(upstream: Response): Promise<BridgeError>;
27
+ /** A reply as it should sound: markdown the agent wrote for the chat bubble
28
+ * (emphasis, headings, bullets, links, inline code, tables) is read aloud
29
+ * literally by ElevenLabs, so it is reduced to its words. A code block is
30
+ * skipped, a link keeps its text and a bare URL becomes "a link". Empty when
31
+ * nothing speakable is left. */
32
+ export declare function speakableText(text: string): string;
29
33
  /** Starts ElevenLabs' streaming synthesis and returns its audio body. */
30
34
  export declare function synthesizeSpeech(text: string, signal: AbortSignal, options?: SpeechOptions): Promise<ReadableStream<Uint8Array>>;
31
35
  /** POST /v1/speech: writes the audio to the phone as ElevenLabs produces it.
@@ -1,9 +1,7 @@
1
1
  import { once } from "node:events";
2
- import { readFile } from "node:fs/promises";
3
- import path from "node:path";
4
2
  import { z } from "zod";
5
- import { homeDir } from "../home-paths.js";
6
3
  import { BridgeError } from "./protocol.js";
4
+ import { readSpeechKey } from "./speech-key.js";
7
5
  /** Spoken replies for the phone's talk mode. The phone sends a sentence; the
8
6
  * Hook voices it with ElevenLabs and streams the audio back. The API key is
9
7
  * read on this computer, used only in the request to ElevenLabs, and never
@@ -18,18 +16,6 @@ export const SPEECH_MODEL = "eleven_flash_v2_5";
18
16
  export const DEFAULT_SPEECH_VOICE = "SAz9YHcvj6GT2YYXdXww";
19
17
  const MAX_TEXT = 2_000;
20
18
  export const speechRequest = z.object({ text: z.string().trim().min(1).max(MAX_TEXT) });
21
- export function speechKeyFile(env = process.env) {
22
- return path.join(homeDir(env), ".config", "mina-trailer.json");
23
- }
24
- export async function readSpeechKey(file = speechKeyFile()) {
25
- try {
26
- const key = JSON.parse(await readFile(file, "utf8")).elevenlabs_api_key;
27
- return typeof key === "string" && key.trim() ? key.trim() : undefined;
28
- }
29
- catch {
30
- return undefined;
31
- }
32
- }
33
19
  /** A fixed message per ElevenLabs failure: its own response text is never
34
20
  * passed on. */
35
21
  export async function speechError(upstream) {
@@ -50,9 +36,40 @@ export async function speechError(upstream) {
50
36
  default: return new BridgeError(502, `ElevenLabs failed (HTTP ${upstream.status}).`, { code: "speech-failed" });
51
37
  }
52
38
  }
39
+ /** A reply as it should sound: markdown the agent wrote for the chat bubble
40
+ * (emphasis, headings, bullets, links, inline code, tables) is read aloud
41
+ * literally by ElevenLabs, so it is reduced to its words. A code block is
42
+ * skipped, a link keeps its text and a bare URL becomes "a link". Empty when
43
+ * nothing speakable is left. */
44
+ export function speakableText(text) {
45
+ return text
46
+ .replace(/```[\s\S]*?(?:```|$)/g, " ")
47
+ .replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1")
48
+ .replace(/\[([^\]]+)\]\([^)]*\)/g, "$1")
49
+ .replace(/https?:\/\/\S+/g, "a link")
50
+ .replace(/`([^`]*)`/g, "$1")
51
+ .split(/\r?\n/)
52
+ .map(line => {
53
+ // A heading, bullet or table row ends where its line does; a
54
+ // paragraph's soft-wrapped lines run on.
55
+ const block = /^\s{0,3}(?:#{1,6}\s|[-*+]\s|\d+[.)]\s|\|)/.test(line);
56
+ const words = line
57
+ .replace(/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/, "")
58
+ .replace(/^\s{0,3}(?:#{1,6}\s+|>\s?|[-*+]\s+(?:\[[ xX]\]\s+)?)/, "")
59
+ .replace(/\s*\|\s*/g, (bar, offset, whole) => offset === 0 || offset + bar.length === whole.length ? "" : ", ")
60
+ .replace(/(\*\*|__|~~)(.+?)\1/g, "$2")
61
+ .replace(/(^|[^\w*])[*_](\S(?:.*?\S)?)[*_](?![\w*])/g, "$1$2")
62
+ .trim();
63
+ return block && words && !/[.!?:;,]$/.test(words) ? `${words}.` : words;
64
+ })
65
+ .filter(Boolean)
66
+ .join(" ")
67
+ .replace(/\s+/g, " ")
68
+ .trim();
69
+ }
53
70
  /** Starts ElevenLabs' streaming synthesis and returns its audio body. */
54
71
  export async function synthesizeSpeech(text, signal, options = {}) {
55
- const key = await (options.key ?? (() => readSpeechKey()))();
72
+ const key = await (options.key ?? readSpeechKey)();
56
73
  if (!key)
57
74
  throw new BridgeError(503, "Spoken replies aren't set up on this computer: it has no ElevenLabs key.", { code: "speech-unconfigured" });
58
75
  const voice = options.voice ?? process.env.PHREN_SPEECH_VOICE ?? DEFAULT_SPEECH_VOICE;
@@ -77,7 +94,9 @@ export async function synthesizeSpeech(text, signal, options = {}) {
77
94
  * failure mid-stream cuts the response off, which the phone treats as an
78
95
  * error. The phone hanging up cancels the ElevenLabs request. */
79
96
  export async function streamSpeech(data, response, options = {}) {
80
- const { text } = speechRequest.parse(data);
97
+ const text = speakableText(speechRequest.parse(data).text);
98
+ if (!text)
99
+ throw new BridgeError(400, "There is nothing to say in this reply.", { code: "speech-invalid" });
81
100
  const abort = new AbortController();
82
101
  response.once("close", () => { if (!response.writableEnded)
83
102
  abort.abort(); });