{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "43b3892e",
   "metadata": {},
   "source": [
    "# 4 - Discover the LeRobot API with `use_lerobot`\n",
    "\n",
    "`use_lerobot` lets an agent (or you) explore everything LeRobot exposes - registered robots, policies, teleoperators, cameras - and inspect any class, without hardcoding anything. It reads from LeRobot's own registries, so the answers track the version you have installed.\n",
    "\n",
    "Runs anywhere: no hardware, no GPU, no Hugging Face credentials. Part of the [examples/notebooks](./README.md) series.\n",
    "\n",
    "**Install:** `uv pip install -U \"strands-robots[sim-mujoco,lerobot]\"`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71f83af2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from strands_robots.tools import use_lerobot\n",
    "\n",
    "# use_lerobot is a Strands @tool. __wrapped__ is the original undecorated\n",
    "# function (preserved by functools.wraps), which we call directly in a notebook.\n",
    "call = use_lerobot.__wrapped__\n",
    "\n",
    "\n",
    "def text(result):\n",
    "    \"\"\"Pull the text content out of a tool result.\"\"\"\n",
    "    if isinstance(result, dict):\n",
    "        return \"\".join(c.get(\"text\", \"\") for c in result.get(\"content\", []) if isinstance(c, dict))\n",
    "    return str(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac7eef59",
   "metadata": {},
   "source": [
    "## Discover everything at once\n",
    "\n",
    "`module=\"__discovery__\"` returns the packages, modules, and every registered config in one view - the fastest way to see what you can work with."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ca826d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(text(call(module=\"__discovery__\", method=\"list_modules\"))[:900])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b82d87b5",
   "metadata": {},
   "source": [
    "## List a single registry\n",
    "\n",
    "The choices come from LeRobot, not from Strands, so they reflect the installed version. Here are the policy types you can train or run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04b85970",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(text(call(module=\"__registry__\", method=\"policies\")))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9197ac4",
   "metadata": {},
   "source": [
    "The same call works for `robots`, `teleoperators`, and `cameras`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30b878c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "for kind in (\"robots\", \"teleoperators\", \"cameras\"):\n",
    "    print(f\"--- {kind} ---\")\n",
    "    print(text(call(module=\"__registry__\", method=kind))[:300])\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7368e487",
   "metadata": {},
   "source": [
    "## Inspect a class without instantiating it\n",
    "\n",
    "`__describe__` returns a class's methods and signature - useful for finding the right call before you make it. No object is constructed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c0cf8ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(text(call(module=\"datasets.lerobot_dataset.LeRobotDataset\", method=\"__describe__\"))[:600])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f857466e",
   "metadata": {},
   "source": [
    "## Resolve a concrete object from a registry name\n",
    "\n",
    "This is the same lookup `create_policy()` does under the hood: the registry name `act` resolves to its policy class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71686beb",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(text(call(module=\"policies.factory\", method=\"get_policy_class\", parameters={\"name\": \"act\"}))[:400])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47046908",
   "metadata": {},
   "source": [
    "## In an agent\n",
    "\n",
    "When `use_lerobot` is handed to a Strands `Agent`, the model selects it from natural language - for example, *\"List the LeRobot policies I can use, then describe the ACT policy.\"* The agent makes the same calls shown above and reads back the results.\n",
    "\n",
    "## Next\n",
    "\n",
    "- **[1 - Getting started](./01_getting_started.ipynb)** - `Robot()` and policies.\n",
    "- **[3 - Record, train, deploy](./03_record_train_deploy.ipynb)** - the full loop."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
