Tin

Examples · learn/26-fleet · 283 of 339

fleet

run: --replay tests/tapes/agents-08-fleet.json --yes

examples/learn/26-fleet/fleet.tintin run examples/learn/26-fleet/fleet.tin
// run: --replay tests/tapes/agents-08-fleet.json --yes
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
}