|
| 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 | +}) |
0 commit comments