Skip to content

Commit 2d79257

Browse files
mayoalexanderclaude
andcommitted
feat(cli): transcription seam (lib) + iris ideas capture (anomalyco#1089)
Layer 2 client side: new cli/lib/transcription.ts with transcribeLocal() (on-device whisper.cpp, extracted from the transcribe command) and transcribeAudio() — the single seam that routes by provider (flag → IRIS_TRANSCRIPTION_PROVIDER → default whisper-local; cloud providers POST to the unified /api/v1/transcribe). Refactored 'iris transcribe' to use it (behavior-preserving). New 'iris ideas capture' (anomalyco#1089): audio (--audio, default on-device) OR text (--text/stdin, e.g. WhisperFlow) → gpt-4.1-nano splits into titled ideas → posted as notes to a lead (default anomalyco#28307). --dry-run/--json/--provider/--lead. Verified live: dictation → 3 clean titled ideas. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent ca9459d commit 2d79257

5 files changed

Lines changed: 296 additions & 88 deletions

File tree

‎packages/opencode/src/cli/cmd/command-groups.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export const COMMAND_CATEGORY_MAP: Record<string, string> = {
190190
claude: "system",
191191
doctor: "system",
192192
"system:apps-scan": "system",
193+
ideas: "crm",
193194
"sdk:call": "system",
194195
eval: "system",
195196
diary: "system",
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import { cmd } from "./cmd"
2+
import * as prompts from "./clack"
3+
import { UI } from "../ui"
4+
import { irisFetch, requireAuth, dim, bold, success, IRIS_API } from "./iris-api"
5+
import { transcribeAudio } from "../lib/transcription"
6+
7+
// ============================================================================
8+
// iris ideas capture — voice/text idea capture (#1089)
9+
//
10+
// audio (or already-transcribed text) → transcription seam (Layer 2, default
11+
// on-device whisper.cpp) → nano structuring into titled ideas → each posted as
12+
// a note on a lead. Input is pluggable: --audio transcribes; --text/stdin lets
13+
// WhisperFlow / any dictation tool feed the SAME pipeline.
14+
// ============================================================================
15+
16+
const DEFAULT_LEAD = 28307 // David Baker
17+
const DEFAULT_MODEL = "iris/gpt-4.1-nano" // nano-only per global rule
18+
19+
interface Idea {
20+
title: string
21+
body: string
22+
}
23+
24+
async function readStdin(): Promise<string> {
25+
if (process.stdin.isTTY) return ""
26+
const chunks: Buffer[] = []
27+
for await (const c of process.stdin) chunks.push(c as Buffer)
28+
return Buffer.concat(chunks).toString("utf8").trim()
29+
}
30+
31+
/** nano pass: split raw dictation into discrete, titled ideas. */
32+
async function structureIdeas(transcript: string, model: string): Promise<Idea[]> {
33+
const sys =
34+
"You turn a person's raw dictated thoughts into a clean list of discrete ideas. "
35+
+ "Split the input into self-contained ideas (one idea = one thing they want to do/remember/explore). "
36+
+ "Clean up filler and false starts but keep their meaning and voice. "
37+
+ 'Return ONLY a JSON array, each item {"title": "<=8 words", "body": "1-3 cleaned sentences"}. No prose, no code fences.'
38+
const res = await irisFetch("/api/v6/openai/chat/completions", {
39+
method: "POST",
40+
body: JSON.stringify({
41+
model,
42+
messages: [
43+
{ role: "system", content: sys },
44+
{ role: "user", content: transcript },
45+
],
46+
temperature: 0.3,
47+
max_tokens: 1500,
48+
}),
49+
}, IRIS_API)
50+
if (!res.ok) {
51+
throw new Error(`Idea structuring failed (HTTP ${res.status})`)
52+
}
53+
const data = (await res.json()) as any
54+
let content = String(data?.choices?.[0]?.message?.content ?? "").trim()
55+
const m = content.match(/\[[\s\S]*\]/)
56+
if (m) content = m[0]
57+
let parsed: any
58+
try {
59+
parsed = JSON.parse(content)
60+
} catch {
61+
// Fail soft: treat the whole transcript as one idea rather than losing it.
62+
return [{ title: "Captured idea", body: transcript.slice(0, 500) }]
63+
}
64+
if (!Array.isArray(parsed)) return [{ title: "Captured idea", body: transcript.slice(0, 500) }]
65+
return parsed
66+
.map((i: any) => ({ title: String(i?.title ?? "").trim() || "Idea", body: String(i?.body ?? "").trim() }))
67+
.filter((i: Idea) => i.body)
68+
}
69+
70+
const IdeasCaptureCommand = cmd({
71+
command: "capture",
72+
aliases: ["add"],
73+
describe: "capture voice/text ideas → structured → posted to a lead's notes",
74+
builder: (y) =>
75+
y
76+
.option("audio", { type: "string", describe: "audio file to transcribe (m4a/mp3/wav)" })
77+
.option("text", { type: "string", describe: "already-transcribed text (e.g. from WhisperFlow); skips transcription" })
78+
.option("lead", { type: "number", default: DEFAULT_LEAD, describe: "lead ID to attach ideas to" })
79+
.option("provider", { type: "string", describe: "transcription provider (default: whisper-local / on-device)" })
80+
.option("language", { type: "string", describe: "ISO 639-1 hint for transcription (e.g. 'en')" })
81+
.option("model", { type: "string", default: DEFAULT_MODEL, describe: "LLM for structuring (nano only)" })
82+
.option("dry-run", { type: "boolean", default: false, describe: "show parsed ideas without posting" })
83+
.option("json", { type: "boolean", default: false }),
84+
async handler(args) {
85+
UI.empty()
86+
prompts.intro("◈ Ideas Capture")
87+
88+
// 1. Get the transcript text.
89+
let transcript = ""
90+
let source = "text"
91+
if (args.text) {
92+
transcript = String(args.text).trim()
93+
} else if (args.audio) {
94+
const sp = prompts.spinner()
95+
sp.start(`Transcribing (${args.provider || "whisper-local"})…`)
96+
try {
97+
const r = await transcribeAudio(String(args.audio), { provider: args.provider, language: args.language })
98+
transcript = r.text.trim()
99+
source = r.provider
100+
sp.stop(`Transcribed via ${r.provider}`)
101+
} catch (e) {
102+
sp.stop("Transcription failed", 1)
103+
prompts.log.error(e instanceof Error ? e.message : String(e))
104+
prompts.outro("Done")
105+
return
106+
}
107+
} else {
108+
transcript = await readStdin()
109+
}
110+
111+
if (!transcript) {
112+
prompts.log.error("No input. Provide --audio <file>, --text \"…\", or pipe text via stdin.")
113+
prompts.outro("Done")
114+
return
115+
}
116+
117+
// 2. Structure into ideas (nano).
118+
const sp2 = prompts.spinner()
119+
sp2.start("Structuring ideas…")
120+
let ideas: Idea[]
121+
try {
122+
const token = await requireAuth()
123+
if (!token) { sp2.stop("Not authenticated", 1); prompts.outro("Done"); return }
124+
ideas = await structureIdeas(transcript, String(args.model))
125+
} catch (e) {
126+
sp2.stop("Failed", 1)
127+
prompts.log.error(e instanceof Error ? e.message : String(e))
128+
prompts.outro("Done")
129+
return
130+
}
131+
sp2.stop(`${ideas.length} idea(s)`)
132+
133+
if (args.json) {
134+
console.log(JSON.stringify({ source, lead: args.lead, dry_run: !!args["dry-run"], ideas }, null, 2))
135+
} else {
136+
console.log(dim(" " + "─".repeat(60)))
137+
for (const i of ideas) console.log(` 💡 ${bold(i.title)}\n ${dim(i.body)}`)
138+
console.log(dim(" " + "─".repeat(60)))
139+
}
140+
141+
if (args["dry-run"]) {
142+
prompts.outro(`Dry run — ${ideas.length} idea(s), nothing posted.`)
143+
return
144+
}
145+
146+
// 3. Post each idea as a lead note.
147+
const sp3 = prompts.spinner()
148+
sp3.start(`Posting ${ideas.length} idea(s) to lead ${args.lead}…`)
149+
let posted = 0
150+
for (const i of ideas) {
151+
const res = await irisFetch(`/api/v1/leads/${args.lead}/notes`, {
152+
method: "POST",
153+
body: JSON.stringify({ message: `💡 ${i.title}\n\n${i.body}`, type: "note" }),
154+
})
155+
if (res.ok) posted++
156+
}
157+
sp3.stop(success(`posted ${posted}/${ideas.length} idea(s) to lead ${args.lead}`))
158+
prompts.outro("Done")
159+
},
160+
})
161+
162+
export const PlatformIdeasCommand = cmd({
163+
command: "ideas",
164+
describe: "capture and manage ideas (voice/text → lead notes)",
165+
builder: (y) => y.command(IdeasCaptureCommand).demandCommand(),
166+
async handler() {},
167+
})

‎packages/opencode/src/cli/cmd/transcribe.ts‎

Lines changed: 14 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
highlight,
1313
} from "./iris-api"
1414
import { spawnSync } from "child_process"
15-
import { existsSync, mkdirSync, statSync } from "fs"
15+
import { existsSync, mkdirSync, statSync, writeFileSync } from "fs"
16+
import { transcribeLocal } from "../lib/transcription"
1617
import { homedir, tmpdir } from "os"
1718
import { join, basename, extname, resolve } from "path"
1819

@@ -32,92 +33,23 @@ async function runLocalWhisper(
3233
sourceUrl?: string,
3334
) {
3435
const abs = resolve(filePath)
35-
if (!existsSync(abs)) {
36-
prompts.log.error(`File not found: ${abs}`)
37-
return
38-
}
39-
40-
// 1. Check deps
41-
const ffmpeg = which("ffmpeg")
42-
const whisper = which("whisper-cli") || which("whisper-cpp")
43-
if (!ffmpeg) {
44-
prompts.log.error("ffmpeg not found. Install: brew install ffmpeg")
45-
return
46-
}
47-
if (!whisper) {
48-
prompts.log.error("Local transcription requires whisper-cpp.")
49-
prompts.log.info("Install: brew install whisper-cpp")
50-
prompts.log.info("Or use a YouTube URL — server-side transcription doesn't need local tools.")
51-
return
52-
}
53-
54-
// 2. Ensure model
55-
const modelDir = join(homedir(), ".whisper")
56-
const modelPath = join(modelDir, "ggml-base.en.bin")
57-
if (!existsSync(modelPath)) {
58-
mkdirSync(modelDir, { recursive: true })
59-
const sp = prompts.spinner()
60-
sp.start("Downloading whisper model (~141 MB)…")
61-
const dl = spawnSync("curl", ["-L", "-o", modelPath, WHISPER_MODEL_URL], {
62-
stdio: "ignore",
63-
})
64-
if (dl.status !== 0) {
65-
sp.stop("Failed", 1)
66-
prompts.log.error("Model download failed")
67-
return
68-
}
69-
sp.stop("Model ready")
70-
}
71-
72-
// 3. Convert → 16kHz mono WAV in tmp
73-
const wavPath = join(
74-
tmpdir(),
75-
`iris-transcribe-${Date.now()}-${basename(abs, extname(abs))}.wav`,
76-
)
7736
const sp = prompts.spinner()
78-
sp.start("Converting audio…")
79-
const conv = spawnSync(
80-
ffmpeg,
81-
["-y", "-i", abs, "-ar", "16000", "-ac", "1", "-c:a", "pcm_s16le", wavPath],
82-
{ stdio: "ignore" },
83-
)
84-
if (conv.status !== 0 || !existsSync(wavPath)) {
37+
sp.start("Transcribing locally (whisper.cpp)…")
38+
let text: string
39+
try {
40+
text = await transcribeLocal(abs, { language })
41+
} catch (e) {
8542
sp.stop("Failed", 1)
86-
prompts.log.error("ffmpeg conversion failed")
87-
return
88-
}
89-
const wavSize = (statSync(wavPath).size / 1024 / 1024).toFixed(1)
90-
sp.stop(`Converted (${wavSize} MB)`)
91-
92-
// 4. Run whisper-cli
93-
const outBase = join(
94-
process.cwd(),
95-
`${basename(abs, extname(abs))}-transcript`,
96-
)
97-
const args = ["-m", modelPath, "-otxt", "-of", outBase]
98-
if (language) args.push("-l", language)
99-
args.push(wavPath)
100-
101-
const sp2 = prompts.spinner()
102-
sp2.start("Transcribing locally (whisper.cpp)…")
103-
const res = spawnSync(whisper, args, { encoding: "utf8" })
104-
// cleanup wav regardless
105-
spawnSync("rm", ["-f", wavPath])
106-
107-
if (res.status !== 0) {
108-
sp2.stop("Failed", 1)
109-
prompts.log.error(res.stderr?.slice(-500) || "whisper-cli failed")
43+
prompts.log.error(e instanceof Error ? e.message : String(e))
11044
return
11145
}
112-
sp2.stop("Done")
46+
sp.stop("Done")
11347

114-
const txtPath = `${outBase}.txt`
115-
const text = existsSync(txtPath)
116-
? require("fs").readFileSync(txtPath, "utf8")
117-
: ""
48+
// Persist next to the source (preserves prior UX).
49+
const txtPath = join(process.cwd(), `${basename(abs, extname(abs))}-transcript.txt`)
50+
writeFileSync(txtPath, text)
11851

119-
// Sync to server so the transcript is searchable in the user's knowledge base.
120-
// Best-effort — silent on failure.
52+
// Best-effort server sync so it's searchable in the knowledge base.
12153
const estimatedDuration = Math.round((text.split(/\s+/).length / 150) * 60)
12254
const syncUrl = sourceUrl ?? (/^https?:\/\//i.test(filePath) ? filePath : undefined)
12355
try {
@@ -137,13 +69,7 @@ async function runLocalWhisper(
13769
}
13870

13971
if (asJson) {
140-
console.log(
141-
JSON.stringify(
142-
{ provider: "whisper.cpp (local)", file: abs, transcript_path: txtPath, text },
143-
null,
144-
2,
145-
),
146-
)
72+
console.log(JSON.stringify({ provider: "whisper.cpp (local)", file: abs, transcript_path: txtPath, text }, null, 2))
14773
return
14874
}
14975

0 commit comments

Comments
 (0)