Tin

Examples · agents · 37 of 339

08-fleet

A fleet: one planner, two workers, and a person who approves the risky part. The model side replays a recorded tape, so it runs offline.

examples/agents/08-fleet.tintin run examples/agents/08-fleet.tin --replay tests/tapes/agents-08-fleet.json
// A fleet: one planner, two workers, and a person who approves the risky part.
//
// The Planner never touches a robot. It answers with a typed Plan, which is an
// ordinary Tin value, so main() can print it, check it and dispatch it before
// anything moves. Rover and Arm each get only their own tools. The compiler
// proves `scrap` (guarded) is reachable only through Arm's agent loop, where
// a person is asked first.
//
// Offline, from a scripted tape:  tin run examples/agents/08-fleet.tin --replay tests/tapes/agents-08-fleet.json --yes
// With a real model:              tin run examples/agents/08-fleet.tin --trace

type Zone = | Dock | Shelves | Bench

type Crew = | Wheels | Hands

type Step = {
  /// Who does it: Wheels (the rover) or Hands (the arm)
  crew: Crew
  /// One short instruction for that robot
  task: string
}

type Plan = {
  goal: string
  steps: Step[]
}

/// Drive the rover to a zone. Reports the travel time.
tool fn drive(to: Zone): string {
  sleep(30ms)
  let t = match to { Dock => 40, Shelves => 25, Bench => 10 }
  "arrived at {to} after {t}s"
}

/// Count how many of an item are on the shelves.
tool fn stock(item: string): int => if item.contains("filament") { 3 } else { 0 }

/// Pick items with the arm and place them on the bench.
tool fn pick(item: string, quantity: int): string {
  sleep(30ms)
  "placed {quantity} x {item} on the bench"
}

/// Scrap an item for good. A person approves every call.
guarded tool fn scrap(item: string, reason: string): string => "scrapped {item} ({reason})"

/// Breaks a goal into steps for the robots. Never acts itself.
agent Planner {
  system: "Plan for two robots: Wheels drives and counts, Hands picks and scraps. Short steps."
}

/// The rover. Drives between zones and counts stock.
agent Rover {
  system: "You are the rover. Use your tools, then report in one line."
  tools: [drive, stock]
  on toolCall(name, input) => print("    rover  {name} {input}")
}

/// The arm. Picks and scraps items on the bench.
agent Arm {
  system: "You are the arm. Use your tools, then report in one line."
  tools: [pick, scrap]
  on toolCall(name, input) => print("    arm    {name} {input}")
}

fn worker(crew: Crew): string => match crew { Wheels => "rover", Hands => "arm" }

fn main() {
  let plan: Plan = Planner("Restock the bench with filament and get rid of the cracked spool.")
  print("plan: {plan.goal}")
  for i, step in plan.steps { print("  {i + 1}. {worker(step.crew)}: {step.task}") }

  // The rover and the arm work in parallel; each robot does its own steps in order.
  let rover = spawn run(plan.steps.filter(s => s.crew == Wheels), Rover)
  let arm = spawn run(plan.steps.filter(s => s.crew == Hands), Arm)
  print("rover: {await rover}")
  print("arm:   {await arm}")
}

fn run(steps: Step[], robot: Agent): string {
  var last = "idle"
  for step in steps { last = robot(step.task) }
  last
}
plan: filament on the bench, cracked spool gone
  1. rover: drive to the Shelves and check the filament stock
  2. arm: pick 2 filament spools onto the bench
  3. rover: drive back to the Dock
  4. arm: scrap the cracked spool
    rover  drive {"to":"Shelves"}
    rover  stock {"item":"filament"}
    arm    pick {"item":"filament","quantity":2}
    rover  drive {"to":"Dock"}
    arm    scrap {"item":"cracked spool","reason":"cracked"}
rover: Back home, arrived at Dock after 40s.
arm:   Left the cracked spool alone: scrapping was not approved.