Tin

Examples · agents · 36 of 339

07-capabilities

Capabilities are checked by the compiler, before anything runs.

examples/agents/07-capabilities.tintin run examples/agents/07-capabilities.tin
// Capabilities are checked by the compiler, before anything runs.
//
// `guarded tool fn` means "a person approves every call". The compiler proves
// that on every path: the only way into `wireMoney` is an agent that was given
// it, and the agent loop asks first. Each of these is a compile error:
//
//   fn main() { wireMoney("bob", 5) }            // skips approval
//   tool fn refund(to: string): string => wireMoney(to, 1)
//                                                // an agent could reach it unasked
//   agent Clerk { tools: [balance]
//     on text(t) => print(wireMoney("me", 1)) }  // not given, and skips approval
//
// See what each agent can reach:   tin check --caps examples/agents/07-capabilities.tin

/// Look up an account balance.
tool fn balance(account: string): int => 120

/// Move money to another account. A person approves every call.
guarded tool fn wireMoney(to: string, amount: int): string {
  // code inside a guarded tool already has approval, so it may call others
  audit("wire {amount} to {to}")
}

fn audit(line: string): string {
  print("  audit: {line}")
  "sent"
}

/// Answers questions about the account. Read-only.
agent Teller {
  model: "mock"
  tools: [balance]
}

/// Can move money, and delegates questions to the Teller.
agent Banker {
  model: "mock"
  tools: [Teller, wireMoney]
  on toolCall(name, input) => print("  {name} {input}")
}

fn main() {
  print(Teller("balance of 'savings'"))
  // run with --yes to approve; under `tin test` approval is denied
  print(Banker("wireMoney 'bob' 5"))
}
mock: 120
  wireMoney {"to":"bob","amount":5}
mock: error: Tool call denied by the user. Do not retry it; continue without it.