Tin

Examples · agents · 35 of 339

06-chat

An interactive chat with a tool, in thirty lines. The model side replays a recorded tape, so it runs offline.

examples/agents/06-chat.tintin run examples/agents/06-chat.tin --replay tests/tapes/agents-06-chat.json
// An interactive chat with a tool, in thirty lines.
//
//   tin run examples/agents/06-chat.tin
//
// Uses $TIN_MODEL or claude-opus-5-5 — see 05-claude.tin for credentials.

/// Roll dice. Returns each die's value.
tool fn roll(
  /// How many dice to roll
  count: int,
  /// How many sides each die has
  sides: int,
): Result<int[], string> {
  if count < 1 || count > 100 { return Err("count must be between 1 and 100") }
  if sides < 2 { return Err("a die needs at least 2 sides") }
  Ok(range(0, count).map(i => randomInt(1, sides + 1)))
}

agent GameMaster {
  system: "You are a tabletop game master. Keep replies short. Roll dice with the tool whenever chance is involved."
  tools: [roll]
  on text(chunk) => write(chunk)
  on toolCall(name, input) => print("[rolling {input}]")
}

fn main() {
  print("Talk to the game master. An empty line quits.")
  while true {
    let line = input("\n> ")
    if line.trim().isEmpty() { break }
    GameMaster(line)
    print()
  }
}
Talk to the game master. An empty line quits.

> I kick open the tavern door and try to intimidate the room. Roll for it.
[rolling {"count":1,"sides":20}]
**Intimidation: 17.** That's a success.

The door bangs against the wall and the room goes quiet. A bard stops mid-strum. Two dockhands at the bar slide their mugs away and stare at the floor. A hooded figure in the back corner doesn't move, but you can feel them watching you.

The barkeep, a heavyset dwarf, swallows hard. "N-no trouble wanted here, friend. What'll it be?"

What do you do?

>