foldrun 21.1.0 → 21.2.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.
package/bin/foldrun.mjs CHANGED
@@ -309,6 +309,7 @@ Signing in
309
309
  foldrun keys create <label> --gateway --cap <usd> [--daily <usd>] a key for a CODING agent (Claude Code: ANTHROPIC_BASE_URL + this key) — it spends the account's credits on foldrun's models through the gateway, up to its own limits, and opens no /api route; an admin of every workspace; shown once
310
310
  foldrun keys create laptop --gateway --cap 20 --claude-code the same, and Claude Code is set up on this machine: the URL and key go into its settings, and its status line shows the key's spend
311
311
  foldrun gateway status-line one line for Claude Code's status line: what the gateway key in ANTHROPIC_AUTH_TOKEN has spent, of its limits (asks the gateway; kept 10 s, --cache <s>)
312
+ foldrun talk speak a prompt: records the microphone until you stop talking, foldrun's own Whisper turns it into text (Persian with the Persian model), prints the words — Claude Code's /talk runs it (--lang auto|fa|en…, --max <s> up to 120, --silence <s>, --file <audio or video> instead of the mic, --mic <name>, --plain, --json; the gateway key from ANTHROPIC_AUTH_TOKEN or --url/--key)
312
313
  foldrun a2a ls --to <ws> what a workspace serves over A2A: each flow's a2a:, card and endpoint URLs, its A2A keys (admins), recent tasks and the key that asked (--json)
313
314
  foldrun a2a publish <flow> set a2a: publish: true in the flow (--to <ws>: the deployed copy, an admin's) — --name, --description, --example "…" (repeatable), --public-card | --private-card; diff, checked, --dry-run
314
315
  foldrun a2a publish <flow> --clear-name | --clear-description | --clear-examples drop the card's name, description or examples (also on unpublish)
@@ -511,7 +512,7 @@ if (!command || command === "--help" || command === "-h") {
511
512
  // `--value` as the account's argument and stored an empty secret; `--force
512
513
  // ./dir` swallowed the directory. A flag followed by another flag is also
513
514
  // boolean, so an unlisted switch at least does not eat its neighbour.
514
- const BOOLEAN_FLAGS = new Set(["read-only", "account", "follow", "force", "oauth2", "wait", "watch", "print", "dry-run", "help", "no-browser", "local", "test", "flat", "yes", "platform", "json", "forever", "all", "check", "new-client", "quiet", "off", "can-ask", "preview", "csv", "failed", "starter", "history", "list", "library", "clear", "security", "changed", "write-baseline", "no-baseline", "rules", "calls", "tree", "tool-error", "stop", "last", "no-pkce", "generate-key", "device", "write", "dpop", "export", "isolated", "no-wait", "dismissed", "unread", "team", "started", "mine", "none", "a2a", "gateway", "claude-code", "login", "unreachable", "public-card", "private-card", "return-immediately", "online", "overwrite", "no-send", "event-wait", "with-client", "use", "clear-name", "clear-description", "clear-examples", "clear-accept-risk", "clear-address", "clear-email", "clear-abn", "no-task", "empty"]);
515
+ const BOOLEAN_FLAGS = new Set(["read-only", "account", "follow", "force", "oauth2", "wait", "watch", "print", "dry-run", "help", "no-browser", "local", "test", "flat", "yes", "platform", "json", "forever", "all", "check", "new-client", "quiet", "off", "can-ask", "preview", "csv", "failed", "starter", "history", "list", "library", "clear", "security", "changed", "write-baseline", "no-baseline", "rules", "calls", "tree", "tool-error", "stop", "last", "no-pkce", "generate-key", "device", "write", "dpop", "export", "isolated", "no-wait", "dismissed", "unread", "team", "started", "mine", "none", "a2a", "gateway", "claude-code", "login", "unreachable", "public-card", "private-card", "return-immediately", "online", "overwrite", "no-send", "event-wait", "with-client", "use", "clear-name", "clear-description", "clear-examples", "clear-accept-risk", "clear-address", "clear-email", "clear-abn", "no-task", "empty", "plain", "no-beep"]);
515
516
  // Flags said more than once collect into a list: `--tag a --tag b`.
516
517
  const REPEATABLE = new Set(["tag", "expect", "header", "site", "add", "remove", "rule", "authorize-param", "example", "accept-risk"]);
517
518
  /** Flags that become a list only when given more than once (FOL-163 `--file`). */
package/docs/providers.md CHANGED
@@ -215,6 +215,11 @@ model on Chat Completions a PDF's text, read it with a script. Whether a given
215
215
  model can see images at all is the provider's; one that cannot refuses or
216
216
  ignores the image.
217
217
 
218
+ Audio and video are never model input: no endpoint above is sent a sound or
219
+ a video, whatever its provider supports. A voice note stays a file in the
220
+ sandbox; to use it, transcribe it with a script (`runtime:` packages, or an
221
+ API the agent declares in `apis:`) and give the model the text.
222
+
218
223
  ### Persian prompts, by tier (checked 11 Nov 2026)
219
224
 
220
225
  An eval of eight Persian tasks, compared on the three tiers with **Compare
package/docs/runs.md CHANGED
@@ -592,6 +592,13 @@ the model as they are, where the model can be shown them (see
592
592
  [images and PDFs](providers#images-and-pdfs-an-agent-reads)). Scripts can read
593
593
  every file.
594
594
 
595
+ Which model is shown an image or a PDF is the one the step runs on — Claude
596
+ only when the step's provider is Anthropic (your key, or foldrun's models
597
+ set to Anthropic); OpenAI, Gemini or another provider otherwise. Audio and
598
+ video (a voice note, an `.mp3`, an `.mp4`) are never sent to a model: they
599
+ stay files in the sandbox, for a script or tool to work on. A script that
600
+ transcribes one with an outside API sends it to that API, and nowhere else.
601
+
595
602
  ```sh
596
603
  foldrun storage put brief.pdf --to sales --into briefs/
597
604
  foldrun agent run analyst --to sales --task "summarise the brief" --file briefs/brief.pdf
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foldrun",
3
- "version": "21.1.0",
3
+ "version": "21.2.0",
4
4
  "description": "Agents are just folders. Write them, check them, run them — on your machine.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "type": "module",
@@ -11,7 +11,7 @@
11
11
  "node": ">=22"
12
12
  },
13
13
  "dependencies": {
14
- "@foldrun/core": "^19.0.0"
14
+ "@foldrun/core": "^19.1.0"
15
15
  },
16
16
  "files": [
17
17
  "bin",
@@ -22,7 +22,7 @@
22
22
  ],
23
23
  "scripts": {
24
24
  "e2e": "FOLDRUN_E2E=1 FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-e2e-XXXXXX) node --test --test-timeout=300000 tests/e2e.test.ts",
25
- "test": "FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-test-XXXXXX) node --test tests/cli-account.test.ts tests/model-credential.test.ts tests/cli-approvals.test.ts tests/cli-issues.test.ts tests/cli-capture.test.ts tests/cli-stop.test.ts tests/cli-account-settings.test.ts tests/cli-team-me.test.ts tests/cli-schedule.test.ts tests/cli-billing.test.ts tests/cli-storage.test.ts tests/cli-scaffold.test.ts tests/cli-secrets.test.ts tests/cli-site-login.test.ts tests/cli-check-library.test.ts tests/cli-check-timezone.test.ts tests/cli-check-frontmatter.test.ts tests/cli-agent-import.test.ts tests/cli-check-subagents.test.ts tests/cli-connect.test.ts tests/cli-accounts.test.ts tests/cli-remote.test.ts tests/cli-stream.test.ts tests/cli-doctor.test.ts tests/cli-tool-test.test.ts tests/cli-agent-run.test.ts tests/cli-check-remote.test.ts tests/cli-shares.test.ts tests/cli-triggers.test.ts tests/cli-providers.test.ts tests/cli-rerun-put.test.ts tests/cli-guide.test.ts tests/cli-runtimes.test.ts tests/cli-deploy-safety.test.ts tests/cli-operator.test.ts tests/cli-flow-patterns.test.ts tests/cli-flow-rm-step.test.ts tests/cli-flow-dup-step.test.ts tests/cli-flow-write-stale.test.ts tests/cli-agent-link-stale.test.ts tests/cli-agent-link-isolated.test.ts tests/cli-parity.test.ts tests/cli-invoke-inputs.test.ts tests/cli-audit-keys.test.ts tests/cli-version.test.ts tests/cli-check-web-warnings.test.ts tests/cli-report-image.test.ts tests/cli-report-cache.test.ts tests/cli-check-workspace-paths.test.ts tests/cli-parity-dashboard.test.ts tests/cli-api-standards.test.ts tests/cli-backups-onboarding.test.ts tests/cli-check-limits.test.ts tests/cli-gallery.test.ts tests/cli-status-platform.test.ts tests/cli-webhooks-notifications.test.ts tests/cli-flags.test.ts tests/cli-api-coverage.test.ts tests/cli-hooks.test.ts tests/cli-release.test.ts tests/cli-terminal.test.ts tests/cli-network.test.ts tests/cli-assistant.test.ts tests/cli-check-scan.test.ts tests/cli-published.test.ts tests/cli-init-target.test.ts tests/cli-flow-filter.test.ts tests/cli-eval-grades.test.ts tests/cli-eval-compare.test.ts tests/cli-proposals.test.ts tests/cli-inbox.test.ts tests/cli-usage-compute.test.ts tests/cli-fol105.test.ts tests/cli-key-renew.test.ts tests/cli-wait-failed.test.ts tests/cli-mcp.test.ts tests/cli-a2a.test.ts tests/cli-gateway.test.ts tests/cli-switches.test.ts tests/cli-parity-runs.test.ts tests/cli-parity-b.test.ts tests/cli-parity-clears.test.ts tests/cli-parity-c.test.ts tests/cli-runs-paging.test.ts tests/cli-budget-invoices.test.ts tests/cli-observe-analysis.test.ts tests/cli-failure-retention.test.ts tests/cli-snapshot.test.ts tests/cli-run-money.test.ts",
25
+ "test": "FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-test-XXXXXX) node --test tests/cli-account.test.ts tests/model-credential.test.ts tests/cli-approvals.test.ts tests/cli-issues.test.ts tests/cli-capture.test.ts tests/cli-stop.test.ts tests/cli-account-settings.test.ts tests/cli-team-me.test.ts tests/cli-schedule.test.ts tests/cli-billing.test.ts tests/cli-storage.test.ts tests/cli-scaffold.test.ts tests/cli-secrets.test.ts tests/cli-site-login.test.ts tests/cli-check-library.test.ts tests/cli-check-timezone.test.ts tests/cli-check-frontmatter.test.ts tests/cli-agent-import.test.ts tests/cli-check-subagents.test.ts tests/cli-connect.test.ts tests/cli-accounts.test.ts tests/cli-remote.test.ts tests/cli-stream.test.ts tests/cli-doctor.test.ts tests/cli-tool-test.test.ts tests/cli-agent-run.test.ts tests/cli-check-remote.test.ts tests/cli-shares.test.ts tests/cli-triggers.test.ts tests/cli-providers.test.ts tests/cli-rerun-put.test.ts tests/cli-guide.test.ts tests/cli-runtimes.test.ts tests/cli-deploy-safety.test.ts tests/cli-operator.test.ts tests/cli-flow-patterns.test.ts tests/cli-flow-rm-step.test.ts tests/cli-flow-dup-step.test.ts tests/cli-flow-write-stale.test.ts tests/cli-agent-link-stale.test.ts tests/cli-agent-link-isolated.test.ts tests/cli-parity.test.ts tests/cli-invoke-inputs.test.ts tests/cli-audit-keys.test.ts tests/cli-version.test.ts tests/cli-check-web-warnings.test.ts tests/cli-report-image.test.ts tests/cli-report-cache.test.ts tests/cli-check-workspace-paths.test.ts tests/cli-parity-dashboard.test.ts tests/cli-api-standards.test.ts tests/cli-backups-onboarding.test.ts tests/cli-check-limits.test.ts tests/cli-gallery.test.ts tests/cli-status-platform.test.ts tests/cli-webhooks-notifications.test.ts tests/cli-flags.test.ts tests/cli-api-coverage.test.ts tests/cli-hooks.test.ts tests/cli-release.test.ts tests/cli-terminal.test.ts tests/cli-network.test.ts tests/cli-assistant.test.ts tests/cli-check-scan.test.ts tests/cli-published.test.ts tests/cli-init-target.test.ts tests/cli-flow-filter.test.ts tests/cli-eval-grades.test.ts tests/cli-eval-compare.test.ts tests/cli-proposals.test.ts tests/cli-inbox.test.ts tests/cli-usage-compute.test.ts tests/cli-fol105.test.ts tests/cli-key-renew.test.ts tests/cli-wait-failed.test.ts tests/cli-mcp.test.ts tests/cli-a2a.test.ts tests/cli-gateway.test.ts tests/cli-switches.test.ts tests/cli-parity-runs.test.ts tests/cli-parity-b.test.ts tests/cli-parity-clears.test.ts tests/cli-parity-c.test.ts tests/cli-runs-paging.test.ts tests/cli-budget-invoices.test.ts tests/cli-observe-analysis.test.ts tests/cli-failure-retention.test.ts tests/cli-snapshot.test.ts tests/cli-run-money.test.ts tests/cli-talk.test.ts",
26
26
  "release": "node scripts/release.mjs",
27
27
  "typecheck": "tsc -p tsconfig.json",
28
28
  "docs:sync": "node scripts/sync-docs.mjs",
package/src/commands.mjs CHANGED
@@ -5101,6 +5101,235 @@ export async function gatewayStatusLine(env = process.env, flags = {}, now = Dat
5101
5101
  return line;
5102
5102
  }
5103
5103
 
5104
+ /**
5105
+ * `foldrun talk` — speak a prompt. Records from the microphone until you stop
5106
+ * talking (2 s of quiet) or `--max` seconds, sends the recording to the
5107
+ * foldrun gateway's POST /v1/foldrun/transcribe, where foldrun's own Whisper
5108
+ * pods turn it into text (the Persian model for Persian), and prints the
5109
+ * words. `--file <audio>` transcribes a file instead.
5110
+ *
5111
+ * Inside Claude Code it is the `/talk` skill's one command, `foldrun talk
5112
+ * --plain`: only the words reach stdout and nothing reaches stderr unless it
5113
+ * fails, because the skill pastes the output as the prompt. Claude Code's own
5114
+ * /voice needs a claude.ai login, is off with a gateway key, sends audio to
5115
+ * Anthropic only and has no Persian.
5116
+ *
5117
+ * The gateway and key come from --url / --key, FOLDRUN_GATEWAY_URL /
5118
+ * FOLDRUN_GATEWAY_KEY, ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN (what Claude
5119
+ * Code hands its commands), or the env block of ~/.claude/settings.json that
5120
+ * `keys create --gateway --claude-code` wrote.
5121
+ */
5122
+ async function talkCmd(flags) {
5123
+ const started = Date.now();
5124
+ const plain = flags.plain === true;
5125
+ const lang = typeof flags.lang === "string" ? flags.lang.toLowerCase() : "auto";
5126
+ if (lang !== "auto" && !/^[a-z]{2,3}$/.test(lang)) throw new Error("--lang is auto, or a language code such as fa or en");
5127
+ const max = flags.max === undefined ? 60 : Number(flags.max);
5128
+ if (!Number.isFinite(max) || max < 1 || max > TALK_MAX_SECONDS) throw new Error(`--max is a number of seconds, 1 to ${TALK_MAX_SECONDS}`);
5129
+ const quiet = flags.silence === undefined ? 2 : Number(flags.silence);
5130
+ if (!Number.isFinite(quiet) || quiet < 0.5 || quiet > 10) throw new Error("--silence is the seconds of quiet that end a recording, 0.5 to 10");
5131
+ const { base, key } = talkEndpoint(process.env, flags);
5132
+ const say = (s) => { if (!plain) process.stderr.write(s); };
5133
+ let wav;
5134
+ if (typeof flags.file === "string") {
5135
+ wav = await talkFileWav(flags.file);
5136
+ } else {
5137
+ say(` ${c.bold("listening…")} ${c.dim(`speak, then stop for ${quiet} s (at most ${max} s; Ctrl+C to cancel)`)}\n`);
5138
+ wav = await talkRecord({ max, quiet, mic: typeof flags.mic === "string" ? flags.mic : null, beep: flags["no-beep"] !== true, until: started + TALK_DEADLINE_MS - 20_000 });
5139
+ }
5140
+ say(` ${c.dim(`transcribing ${(Math.max(0, wav.length - 44) / 32000).toFixed(1)} s…`)}\n`);
5141
+ let res;
5142
+ try {
5143
+ // Claude Code kills a skill's command at 2 minutes and drops the prompt:
5144
+ // the whole of `talk` stays inside 110 s, recording included.
5145
+ const wait = Math.max(10_000, TALK_DEADLINE_MS - (Date.now() - started));
5146
+ res = await fetch(`${base}/v1/foldrun/transcribe?lang=${lang}`, { method: "POST", headers: { authorization: `Bearer ${key}`, "content-type": "audio/wav" }, body: wav, signal: AbortSignal.timeout(wait) });
5147
+ } catch (err) {
5148
+ throw new Error(`the gateway at ${base} did not answer (${err.name === "TimeoutError" ? "no transcript in time — try a shorter recording" : err.message})`);
5149
+ }
5150
+ const text = await res.text();
5151
+ /** @type {any} */
5152
+ let j = null;
5153
+ try { j = JSON.parse(text); } catch { /* not JSON: said below */ }
5154
+ if (!res.ok) throw new Error(j?.error?.message ? `the gateway refused it (${res.status}): ${j.error.message}` : res.status === 404 ? `${base} is not a foldrun gateway with transcription (404)` : `the gateway answered ${res.status}`);
5155
+ const words = talkClean(typeof j?.text === "string" ? j.text : "");
5156
+ if (!words) throw new Error("no speech was heard — check the microphone, and that this terminal may use it");
5157
+ if (flags.json === true) console.log(JSON.stringify({ ...j, text: words }));
5158
+ else process.stdout.write(`${words}\n`);
5159
+ say(` ${c.dim(`${j.language ?? "?"} · ${j.model ?? "?"} · ${j.seconds ?? "?"} s`)}\n`);
5160
+ return 0;
5161
+ }
5162
+
5163
+ const TALK_MAX_SECONDS = 120;
5164
+ /** All of `foldrun talk`, recording to words: under Claude Code's 2-minute
5165
+ * limit on a skill's command. */
5166
+ const TALK_DEADLINE_MS = 110_000;
5167
+
5168
+ /** The gateway and key `foldrun talk` uses, or why there is none. */
5169
+ export function talkEndpoint(env = process.env, flags = {}, settingsFile = path.join(os.homedir(), ".claude", "settings.json")) {
5170
+ let settings = {};
5171
+ try { settings = JSON.parse(fs.readFileSync(settingsFile, "utf8"))?.env ?? {}; } catch { /* no Claude Code settings */ }
5172
+ const pick = (...vals) => vals.map((v) => (typeof v === "string" ? v.trim() : "")).find(Boolean) ?? "";
5173
+ const base = pick(flags.url, env.FOLDRUN_GATEWAY_URL, env.ANTHROPIC_BASE_URL, settings.ANTHROPIC_BASE_URL).replace(/\/+$/, "");
5174
+ const key = pick(flags.key, env.FOLDRUN_GATEWAY_KEY, env.ANTHROPIC_AUTH_TOKEN, env.ANTHROPIC_API_KEY, settings.ANTHROPIC_AUTH_TOKEN, settings.ANTHROPIC_API_KEY);
5175
+ if (!base) throw new Error("no foldrun gateway set — make a key with `foldrun keys create <label> --gateway --cap <usd> --claude-code`, or pass --url and --key");
5176
+ if (!/^https?:\/\//.test(base)) throw new Error(`the gateway URL must start with https:// (got ${base})`);
5177
+ if (/(^|\.)anthropic\.com$/.test(new URL(base).hostname)) throw new Error("ANTHROPIC_BASE_URL points at Anthropic, not a foldrun gateway — /talk sends recordings to foldrun");
5178
+ if (!key) throw new Error("no gateway key set (ANTHROPIC_AUTH_TOKEN) — `foldrun keys create <label> --gateway --cap <usd> --claude-code` makes one");
5179
+ return { base, key };
5180
+ }
5181
+
5182
+ /** Whisper's notes for no words ([BLANK_AUDIO], (music)…) are not words. */
5183
+ export function talkClean(text) {
5184
+ const t = text.replace(/\s+/g, " ").trim();
5185
+ return /^([[(][^\])]*[\])]\s*)+$/.test(t) ? "" : t;
5186
+ }
5187
+
5188
+ /** ffmpeg, which records and converts: FOLDRUN_FFMPEG, else on the PATH. */
5189
+ function talkFfmpeg() {
5190
+ const bin = process.env.FOLDRUN_FFMPEG || "ffmpeg";
5191
+ const probe = spawnSync(bin, ["-hide_banner", "-version"], { stdio: "ignore" });
5192
+ if (probe.error || probe.status !== 0) throw new Error(`/talk needs ffmpeg to record — install it (${process.platform === "darwin" ? "brew install ffmpeg" : process.platform === "win32" ? "winget install ffmpeg" : "sudo apt install ffmpeg"})`);
5193
+ return bin;
5194
+ }
5195
+
5196
+ /** A file as the 16 kHz mono WAV the gateway takes: sent as it is when it
5197
+ * already is one, else converted by ffmpeg (any audio, or a video's sound). */
5198
+ async function talkFileWav(file) {
5199
+ const buf = fs.readFileSync(file);
5200
+ // Already the right shape and not too long: sent as it is. A longer one is
5201
+ // cut to TALK_MAX_SECONDS by ffmpeg below, as any other file is.
5202
+ if (talkIsWav16k(buf) && buf.length <= 44 + TALK_MAX_SECONDS * 32000) return buf;
5203
+ const out = spawnSync(talkFfmpeg(), ["-hide_banner", "-nostdin", "-loglevel", "error", "-i", file, "-vn", "-ac", "1", "-ar", "16000", "-t", String(TALK_MAX_SECONDS), "-f", "s16le", "pipe:1"], { maxBuffer: 64 * 1024 * 1024 });
5204
+ if (out.status !== 0 || !out.stdout?.length) throw new Error(`ffmpeg could not read ${file}: ${String(out.stderr ?? "").trim().split("\n").pop() || "no audio in it"}`);
5205
+ return talkWav(out.stdout);
5206
+ }
5207
+
5208
+ /** Is this already PCM 16-bit, 16 kHz, mono WAV? */
5209
+ export function talkIsWav16k(b) {
5210
+ return b.length >= 44 && b.toString("ascii", 0, 4) === "RIFF" && b.toString("ascii", 8, 12) === "WAVE" && b.toString("ascii", 12, 16) === "fmt " && b.readUInt16LE(20) === 1 && b.readUInt16LE(22) === 1 && b.readUInt32LE(24) === 16000 && b.readUInt16LE(34) === 16;
5211
+ }
5212
+
5213
+ /** Raw PCM 16-bit 16 kHz mono, with a WAV header in front. */
5214
+ export function talkWav(pcm) {
5215
+ const h = Buffer.alloc(44);
5216
+ h.write("RIFF", 0, "ascii");
5217
+ h.writeUInt32LE(36 + pcm.length, 4);
5218
+ h.write("WAVE", 8, "ascii");
5219
+ h.write("fmt ", 12, "ascii");
5220
+ h.writeUInt32LE(16, 16);
5221
+ h.writeUInt16LE(1, 20);
5222
+ h.writeUInt16LE(1, 22);
5223
+ h.writeUInt32LE(16000, 24);
5224
+ h.writeUInt32LE(32000, 28);
5225
+ h.writeUInt16LE(2, 32);
5226
+ h.writeUInt16LE(16, 34);
5227
+ h.write("data", 36, "ascii");
5228
+ h.writeUInt32LE(pcm.length, 40);
5229
+ return Buffer.concat([h, pcm]);
5230
+ }
5231
+
5232
+ /** ffmpeg's input for the default microphone on this system. macOS: the
5233
+ * device System Settings has chosen (by name — index 0 may be an iPhone). */
5234
+ function talkInput(device) {
5235
+ if (process.platform === "darwin") {
5236
+ let name = device;
5237
+ if (!name) {
5238
+ const sp = spawnSync("system_profiler", ["SPAudioDataType", "-json"], { encoding: "utf8", timeout: 10_000 });
5239
+ try {
5240
+ const items = JSON.parse(sp.stdout).SPAudioDataType.flatMap((g) => g._items ?? []);
5241
+ name = items.find((i) => i.coreaudio_default_audio_input_device === "spaudio_yes")?._name ?? null;
5242
+ } catch { /* the first device below */ }
5243
+ }
5244
+ return ["-f", "avfoundation", "-i", `:${name ?? "0"}`];
5245
+ }
5246
+ if (process.platform === "win32") {
5247
+ let name = device;
5248
+ if (!name) {
5249
+ const ls = spawnSync(talkFfmpeg(), ["-hide_banner", "-list_devices", "true", "-f", "dshow", "-i", "dummy"], { encoding: "utf8" });
5250
+ name = /"([^"]+)"\s+\(audio\)/.exec(`${ls.stderr}`)?.[1] ?? null;
5251
+ if (!name) throw new Error("no microphone found — name one with --mic \"<name>\" (ffmpeg -list_devices true -f dshow -i dummy)");
5252
+ }
5253
+ return ["-f", "dshow", "-i", `audio=${name}`];
5254
+ }
5255
+ const pulse = spawnSync("pactl", ["info"], { stdio: "ignore" });
5256
+ return pulse.status === 0 ? ["-f", "pulse", "-i", device ?? "default"] : ["-f", "alsa", "-i", device ?? "default"];
5257
+ }
5258
+
5259
+ /** A short sound at the start and end of recording, where the system has
5260
+ * one: inside Claude Code nothing is drawn while a command runs. */
5261
+ function talkBeep(which) {
5262
+ if (process.platform !== "darwin") return;
5263
+ const f = `/System/Library/Sounds/${which === "start" ? "Tink" : "Pop"}.aiff`;
5264
+ if (fs.existsSync(f)) spawnSync("afplay", [f], { stdio: "ignore", timeout: 2000 });
5265
+ }
5266
+
5267
+ /**
5268
+ * Record until `quiet` seconds of silence after speech, or `max` seconds.
5269
+ * ffmpeg's silencedetect reports `silence_start: <t>` on stderr once a quiet
5270
+ * spell has lasted `quiet` seconds; one that starts after the first tenth of a second is
5271
+ * the end of speech (one at 0 is quiet before speaking, and is waited out
5272
+ * up to 15 s). The samples come back raw on stdout and get a WAV header here.
5273
+ */
5274
+ async function talkRecord({ max, quiet, mic, beep, until }) {
5275
+ const bin = talkFfmpeg();
5276
+ const input = talkInput(mic);
5277
+ if (beep) talkBeep("start");
5278
+ const child = (await import("node:child_process")).spawn(bin, ["-hide_banner", "-nostdin", "-loglevel", "info", ...input, "-ac", "1", "-ar", "16000", "-af", `silencedetect=noise=-35dB:d=${quiet}`, "-t", String(max), "-f", "s16le", "pipe:1"], { stdio: ["ignore", "pipe", "pipe"] });
5279
+ const chunks = [];
5280
+ let bytes = 0;
5281
+ let err = "";
5282
+ let stopped = null;
5283
+ let heard = false;
5284
+ let quietAtStart = false;
5285
+ let partial = "";
5286
+ const stop = (why) => {
5287
+ if (stopped) return;
5288
+ stopped = why;
5289
+ child.kill("SIGTERM");
5290
+ };
5291
+ child.stdout.on("data", (b) => {
5292
+ chunks.push(b);
5293
+ bytes += b.length;
5294
+ if (bytes / 32000 > 15 && !heard) stop("nothing");
5295
+ });
5296
+ child.stderr.on("data", (b) => {
5297
+ err = (err + b.toString()).slice(-4000);
5298
+ // Whole lines only: a report split across two chunks is read once it
5299
+ // is complete (ffmpeg never repeats one).
5300
+ const lines = (partial + b.toString()).split(/\r?\n|\r/);
5301
+ partial = lines.pop() ?? "";
5302
+ for (const m of lines.join("\n").matchAll(/silence_(start|end): (-?[\d.]+)/g)) {
5303
+ // A quiet spell that began at 0 is quiet before speaking; one that
5304
+ // began any later followed speech, however short ("yes").
5305
+ if (m[1] === "end") heard = true;
5306
+ else if (Number(m[2]) > 0.1) { heard = true; stop("quiet"); }
5307
+ else quietAtStart = true;
5308
+ }
5309
+ // Speaking from the first moment: no quiet spell at 0 was reported once
5310
+ // one could have been.
5311
+ if (!heard && !quietAtStart && bytes / 32000 > quiet + 0.5) heard = true;
5312
+ });
5313
+ const onSig = () => stop("cancel");
5314
+ process.once("SIGINT", onSig);
5315
+ // ffmpeg's -t counts audio, not time: a microphone that never starts (a
5316
+ // permission prompt nobody answers) would hold it for ever. A clock of our
5317
+ // own, inside the whole command's deadline.
5318
+ const watchdog = setTimeout(() => stop("clock"), Math.max(1000, Math.min(max * 1000 + 5000, until - Date.now())));
5319
+ const code = await new Promise((resolve) => child.on("close", resolve));
5320
+ clearTimeout(watchdog);
5321
+ process.removeListener("SIGINT", onSig);
5322
+ if (beep) talkBeep("end");
5323
+ if (stopped === "cancel") throw new Error("cancelled");
5324
+ if (stopped === "nothing") throw new Error("no speech was heard in 15 s — check the microphone, and that this terminal may use it");
5325
+ if (stopped === "clock" && !bytes) throw new Error("the microphone did not start — allow this terminal in System Settings → Privacy & Security → Microphone, or name one with --mic");
5326
+ if (!bytes) {
5327
+ const last = err.trim().split("\n").filter((l) => !/silence_/.test(l)).pop() ?? "";
5328
+ throw new Error(/permission|not authorized|denied/i.test(err) ? "this terminal may not use the microphone — allow it in System Settings → Privacy & Security → Microphone" : `ffmpeg recorded nothing (exit ${code}): ${last}`);
5329
+ }
5330
+ return talkWav(Buffer.concat(chunks));
5331
+ }
5332
+
5104
5333
  /** A retried mint or rotate is answered from the first call, and the server
5105
5334
  * never stores a key's secret — so a retry after a lost answer has none. */
5106
5335
  function lostSecret(id) {
@@ -15361,6 +15590,8 @@ export async function run(command, positional, flags, workspace, layout) {
15361
15590
  return workspacesCmd(positional, flags, layout);
15362
15591
  case "extract":
15363
15592
  return extract(workspace, flags);
15593
+ case "talk":
15594
+ return talkCmd(flags);
15364
15595
  case "deploy":
15365
15596
  return deploy(positional[0] ?? ".", flags, layout);
15366
15597
  case "run":