Tin

Examples · agents · 42 of 339

13-voice

Talking agents: a voice agent holds a live, two-way conversation with a speech-to-speech model.

examples/agents/13-voice.tintin run examples/agents/13-voice.tin --mic question.wav --speaker reply.wav
// Talking agents: a `voice agent` holds a live, two-way conversation with a speech-to-speech
// model. Audio and text go in while audio, transcripts and tool calls come out.
//
// This file uses `model: "mock"`, an offline stand-in that answers with a tone and a
// transcript. For a real voice, set the model to one of:
//   "openai-realtime:gpt-realtime-mini"   (OPENAI_API_KEY; works in the browser too)
//   "gemini-live:gemini-live-2.5-flash-preview"   (GEMINI_API_KEY)
//   "bedrock-sonic:amazon.nova-sonic-v1:0"   (access keys; Node only)

/// The weather in a city.
tool fn weather(city: string): string => "18C and clear in {city}"

voice agent Concierge {
  model: "mock"
  system: "You are a hotel concierge. Answer in one short sentence."
  voice: "marin"
  tools: [weather]
  // handlers run as the conversation goes
  on toolCall(name, input) => print("  [tool] {name} {input}")
  on bargeIn() => print("  [the guest talked over the reply]")
}

fn main() {
  // a session is a live connection; say() sends text, send() sends audio
  let s = Concierge.open()
  s.say("What is the weather in 'Lisbon'?")
  var heard = 0.0
  loop {
    // every event is a VoiceEvent; match is checked for every case you name
    match s.next() {
      Connected(model) => print("connected to {model}")
      Speech(chunk) => { heard = heard + chunk.seconds() }
      Said(text) => print("concierge: {text}")
      // a tool call is a turn of its own: hang up after the spoken answer
      TurnDone => if heard > 0.0 { s.stop() }
      HungUp(reason) => break
      _ => {}
    }
  }
  print("the reply was {heard.fixed(1)} s of audio")
  // a whole call with a microphone and a speaker is one line:
  //   Concierge.talk(mic(), speaker())
  // on the CLI: tin run 13-voice.tin --mic question.wav --speaker reply.wav
}
connected to mock
  [tool] weather {"city":"Lisbon"}
concierge: mock: 18C and clear in Lisbon
the reply was 1.7 s of audio

This output is from a test microphone: 0.6 s of speech, then quiet. In the playground it listens to yours.