@@ -2,6 +2,8 @@ import { cmd } from "./cmd"
22import * as prompts from "./clack"
33import { UI } from "../ui"
44import { irisFetch , requireAuth , handleApiError , printDivider , dim , bold , FL_API , IRIS_API , resolveUserId , streamAgentChat } from "./iris-api"
5+ import { captureMic , speak , listMics } from "../lib/voice"
6+ import { transcribeLocal , which } from "../lib/transcription"
57
68// ============================================================================
79// Polling helper
@@ -231,6 +233,137 @@ export async function executeChat(args: {
231233 }
232234}
233235
236+ // ============================================================================
237+ // Voice chat — local, free, real-time conversation loop.
238+ //
239+ // mic (ffmpeg) → transcribeLocal (whisper.cpp) → streamAgentChat → speak (say).
240+ // Push-to-talk turn-taking; multi-turn via conversation_history (no server
241+ // session, so no cross-turn poisoning — same guarantee as text chat). All STT
242+ // + TTS runs on-device; only the agent call leaves the machine. (#158044/#158045)
243+ // ============================================================================
244+
245+ export async function runVoiceChat ( args : {
246+ agent ?: number
247+ bloq ?: number
248+ timeout : number
249+ "no-rag" : boolean
250+ model ?: string
251+ "max-iterations" ?: number
252+ mic ?: string
253+ tts ?: string
254+ "tts-voice" ?: string
255+ } ) : Promise < void > {
256+ UI . empty ( )
257+ prompts . intro ( "◈ IRIS Voice Chat" )
258+
259+ const token = await requireAuth ( )
260+ if ( ! token ) { prompts . outro ( "Done" ) ; return }
261+
262+ const agentId = args . agent
263+ if ( ! agentId ) {
264+ prompts . log . warn ( "Voice chat needs an explicit agent. Use --agent <id>." )
265+ prompts . log . info ( `Try: ${ dim ( "iris agents list" ) } to find one (e.g. --agent 642 for TOBI)` )
266+ prompts . outro ( "Done" )
267+ return
268+ }
269+
270+ if ( ! which ( "ffmpeg" ) || ( ! which ( "whisper-cli" ) && ! which ( "whisper-cpp" ) ) ) {
271+ prompts . log . error ( "Local voice needs ffmpeg + whisper-cpp." )
272+ prompts . log . info ( `Install: ${ dim ( "brew install ffmpeg whisper-cpp" ) } ` )
273+ prompts . outro ( "Done" )
274+ return
275+ }
276+
277+ const userId = await resolveUserId ( )
278+ const mics = listMics ( )
279+ const micLabel = args . mic
280+ ? mics . find ( ( m ) => m . index === args . mic ) ?. name ?? `device :${ args . mic } `
281+ : "system default"
282+ const ttsLabel = args . tts ?? ( process . platform === "darwin" ? "say" : "piper" )
283+
284+ prompts . log . info ( `${ bold ( `Agent #${ agentId } ` ) } ${ dim ( `· mic: ${ micLabel } · tts: ${ ttsLabel } ` ) } ` )
285+ prompts . log . info ( dim ( "ENTER = speak · ENTER again = stop · type q + ENTER to quit" ) )
286+
287+ const history : Array < { role : string ; content : string } > = [ ]
288+
289+ while ( true ) {
290+ process . stderr . write ( `\n ${ dim ( "▶︎ press ENTER to speak (q to quit)…" ) } ` )
291+ const key = await new Promise < string > ( ( resolve ) => {
292+ process . stdin . resume ( )
293+ const onData = ( d : Buffer ) => {
294+ process . stdin . removeListener ( "data" , onData )
295+ process . stdin . pause ( )
296+ resolve ( d . toString ( ) . trim ( ) )
297+ }
298+ process . stdin . once ( "data" , onData )
299+ } )
300+ if ( key . toLowerCase ( ) === "q" ) break
301+
302+ // Capture + transcribe (both on-device).
303+ let text = ""
304+ try {
305+ const wav = await captureMic ( { mic : args . mic } )
306+ process . stderr . write ( "\r" + " " . repeat ( 48 ) + "\r" )
307+ text = await transcribeLocal ( wav )
308+ } catch ( err ) {
309+ prompts . log . error ( err instanceof Error ? err . message : String ( err ) )
310+ continue
311+ }
312+ if ( ! text ) { prompts . log . info ( dim ( "(heard nothing — try again)" ) ) ; continue }
313+ console . log ( ` ${ bold ( "You:" ) } ${ text } ` )
314+
315+ // Ask the agent (same faithful V6 ReactLoop path as text chat).
316+ history . push ( { role : "user" , content : text } )
317+ const startTime = Date . now ( )
318+ const spinner = prompts . spinner ( )
319+ spinner . start ( "Thinking…" )
320+ let lastActivity = "Thinking…"
321+ const heartbeat = setInterval ( ( ) => {
322+ spinner . message ( `${ lastActivity } (${ Math . floor ( ( Date . now ( ) - startTime ) / 1000 ) } s)` )
323+ } , 1000 )
324+
325+ try {
326+ const result = await streamAgentChat ( {
327+ agentId,
328+ message : text ,
329+ userId,
330+ bloqId : args . bloq ,
331+ overrideModel : args . model ,
332+ maxIterations : args [ "max-iterations" ] ,
333+ timeoutSecs : args . timeout ,
334+ enableRag : ! args [ "no-rag" ] ,
335+ conversationHistory : history . slice ( 0 , - 1 ) ,
336+ onEvent : ( evt ) => {
337+ if ( evt . type === "tool_call" && evt . tool ) lastActivity = `Using ${ evt . tool } …`
338+ else if ( evt . type === "tool_result" && evt . tool ) lastActivity = `${ evt . tool } ✓`
339+ else if ( evt . type === "thinking" ) lastActivity = "Thinking…"
340+ } ,
341+ } )
342+ clearInterval ( heartbeat )
343+
344+ if ( ! result . ok ) {
345+ spinner . stop ( result . timedOut ? "Timed out" : "Failed" , 1 )
346+ prompts . log . error ( result . error ?? "Chat failed — no answer delivered." )
347+ history . pop ( ) // drop the unanswered turn so history stays consistent
348+ continue
349+ }
350+
351+ spinner . stop ( dim ( `${ result . iterations ?? 0 } iter · ${ ( ( Date . now ( ) - startTime ) / 1000 ) . toFixed ( 1 ) } s` ) )
352+ const reply = result . content || "(no response)"
353+ history . push ( { role : "assistant" , content : reply } )
354+ console . log ( ` ${ bold ( "Agent:" ) } ${ reply . split ( "\n" ) . join ( "\n " ) } ` )
355+ await speak ( reply , { tts : args . tts , voice : args [ "tts-voice" ] } )
356+ } catch ( err ) {
357+ clearInterval ( heartbeat )
358+ spinner . stop ( "Error" , 1 )
359+ prompts . log . error ( err instanceof Error ? err . message : String ( err ) )
360+ history . pop ( )
361+ }
362+ }
363+
364+ prompts . outro ( "Voice chat ended 👋" )
365+ }
366+
234367function outputResult ( run : WorkflowRun , workflowId : string , agentId : number | undefined , isJson : boolean , toolsUsed : string [ ] = [ ] ) : void {
235368 const response = run . summary ?? run . response ?? run . output ?? "(no response)"
236369
@@ -427,9 +560,60 @@ export const PlatformChatCommand = cmd({
427560 describe : "cap ReactLoop iterations" ,
428561 type : "number" ,
429562 } )
563+ . option ( "voice" , {
564+ describe : "voice mode — talk to the agent via mic + local speech (free, on-device)" ,
565+ type : "boolean" ,
566+ default : false ,
567+ } )
568+ . option ( "mic" , {
569+ describe : "input device (macOS index from --list-mics, or ALSA name); default = system mic" ,
570+ type : "string" ,
571+ } )
572+ . option ( "tts" , {
573+ describe : "speech backend for replies: say | piper | none" ,
574+ type : "string" ,
575+ choices : [ "say" , "piper" , "none" ] ,
576+ } )
577+ . option ( "tts-voice" , {
578+ describe : "TTS voice name (e.g. macOS `say -v` voice)" ,
579+ type : "string" ,
580+ } )
581+ . option ( "list-mics" , {
582+ describe : "list available input devices and exit" ,
583+ type : "boolean" ,
584+ default : false ,
585+ } )
430586 . command ( ChatApproveCommand ) ,
431587
432588 async handler ( args ) {
589+ if ( args [ "list-mics" ] ) {
590+ UI . empty ( )
591+ prompts . intro ( "◈ IRIS Voice — Input Devices" )
592+ const mics = listMics ( )
593+ if ( mics . length === 0 ) {
594+ prompts . log . info ( "No devices enumerated (device listing is macOS-only; on Linux pass --mic <alsa-name>)." )
595+ } else {
596+ for ( const m of mics ) console . log ( ` ${ bold ( `:${ m . index } ` ) } ${ m . name } ` )
597+ }
598+ prompts . outro ( `Use: ${ dim ( "iris chat --voice --agent <id> --mic <index>" ) } ` )
599+ return
600+ }
601+
602+ if ( args . voice ) {
603+ await runVoiceChat ( {
604+ agent : args . agent ,
605+ bloq : args . bloq ,
606+ timeout : args . timeout ,
607+ "no-rag" : args [ "no-rag" ] ,
608+ model : args . model ,
609+ "max-iterations" : args [ "max-iterations" ] ,
610+ mic : args . mic ,
611+ tts : args . tts ,
612+ "tts-voice" : args [ "tts-voice" ] ,
613+ } )
614+ return
615+ }
616+
433617 if ( ! args . message && ! args . continue ) {
434618 UI . empty ( )
435619 prompts . intro ( "◈ IRIS Chat" )
0 commit comments