Tin

Examples · agents · 33 of 339

04-approval

guarded tool fn puts a human in the loop.

examples/agents/04-approval.tintin run examples/agents/04-approval.tin
// `guarded tool fn` puts a human in the loop.
//
// Before a guarded tool runs, the agent loop pauses and the runtime asks for
// approval. If the answer is no, the tool never runs: the model is told the
// call was denied and carries on without it.
//
//   tin run examples/agents/04-approval.tin          asks you (or denies, if not a terminal)
//   tin run examples/agents/04-approval.tin --yes    approves everything

/// List the files in the project.
tool fn listFiles(): string[] => ["notes.txt", "draft.md", "old-backup.zip"]

/// Permanently delete a file.
guarded tool fn deleteFile(path: string): string => "deleted {path}"

agent Janitor {
  model: "mock"
  system: "You tidy up the project. Ask before deleting anything important."
  tools: [listFiles, deleteFile]
  on toolResult(name, output, ok) => print("  {name} -> {output}")
}

fn main() {
  print(Janitor("use listFiles"))
  print(Janitor("deleteFile 'old-backup.zip'"))

  // Plain code can call a guarded tool too, but only through `approve`, which
  // shows a person the exact call first. Calling deleteFile(...) directly is a
  // compile error.
  match approve deleteFile("draft.md") {
    Ok(msg) => print(msg)
    Err(why) => print("not done: {why}")
  }
}
  listFiles -> ["notes.txt","draft.md","old-backup.zip"]
mock: ["notes.txt","draft.md","old-backup.zip"]
  deleteFile -> Tool call denied by the user. Do not retry it; continue without it.
mock: error: Tool call denied by the user. Do not retry it; continue without it.
not done: denied: deleteFile