Metadata-Version: 2.5
Name: roborouter
Version: 0.1.0
Summary: Run a robot on any model with a key and one call.
Author: Momentum
License: Apache-2.0
Keywords: models,momentum,robotics
Requires-Python: >=3.11
Requires-Dist: httpx>=0.28
Requires-Dist: msgpack>=1.0
Provides-Extra: arrays
Requires-Dist: numpy>=1.26; extra == 'arrays'
Requires-Dist: simplejpeg>=1.8; extra == 'arrays'
Provides-Extra: dev
Requires-Dist: mcap>=1.4; extra == 'dev'
Requires-Dist: numpy>=1.26; extra == 'dev'
Requires-Dist: protobuf>=5; extra == 'dev'
Requires-Dist: pytest>=9.1; extra == 'dev'
Requires-Dist: ruff>=0.16; extra == 'dev'
Requires-Dist: simplejpeg>=1.8; extra == 'dev'
Provides-Extra: record
Requires-Dist: mcap>=1.4; extra == 'record'
Description-Content-Type: text/markdown

# RoboRouter

Run a robot, in a simulator or the real world, on any model with a key and one call.

**Not ready for use yet.** The service isn't open to new users.

```bash
pip install roborouter
export MOMENTUM_API_KEY=fw_cli_...   # Settings, Workspace API keys
```

```python
import roborouter

config = {"model": "momentum/scripted"}

while True:
    state = {"joints": read_my_joints(), "images": {"top": read_my_camera_jpeg()}}
    reply = roborouter.control_robot(config, state, "pick up the fork")
    if reply.finished:
        break
    move_my_robot(reply.joints)
```

`joints` is a map of your joint names to their positions, for any number of arms. `images` is optional, and each frame is JPEG bytes. With `pip install roborouter[arrays]` a frame can also be a `uint8` height × width × 3 RGB array. OpenCV frames are BGR, so convert them first. `momentum/scripted` nudges every joint a little for a few moves and then finishes, so you can check the loop with no model provider involved.

**With no details from you, nothing checks the targets.** A bad reply moves the arm as far as your robot's own limits allow. `control_robot` raises `RoboRouterError` when it can't get an answer, so hold your robot on an exception. If the robot moved after a reply was lost, the error's code is `state_changed`, and the next call starts a new run. If no model answered, the code is `no_model_answered` and the run stays where it was, so you can send the same move again with another model.

## Choosing a model

| `config` | Asks |
|---|---|
| `{"model": "anthropic/claude-opus-5-5"}` | That model only |
| `{"model": "anthropic/claude-opus-5-5", "fallbacks": ["openai/gpt-6-astra"]}` | That model, then each fallback in turn, up to 4 |
| `{"model": "momentum/auto"}` | The model that last answered in this run, then the rest of Gemini 4 Argon, Claude Opus 5.5 and GPT-6 Astra |

The models you can name:

| Provider | Models |
|---|---|
| Anthropic | `anthropic/claude-opus-5-5`, `anthropic/claude-haiku-5-5` |
| OpenAI | `openai/gpt-6-astra`, `openai/gpt-6.1-sol`, `openai/gpt-6.1-terra`, `openai/gpt-6-luna` |
| Google | `google/gemini-4-argon`, `google/gemini-3.8-flash`, `google/gemini-3.5-flash-lite` |

Every call can change `config`, and the run carries on. The server keeps the run: the next model gets your instructions and the last 20 moves, with who answered each one, its targets and its note. It doesn't get earlier camera frames. `reply.message` has the note the model left for the next one, and `reply.answered_by` has the model that answered.

Add `"thinking": "more"` to a call to give the models more thinking and more time on that move. A move still has to answer within 50 seconds.

The same instructions carry on the last run until it finishes, whichever model each call names. Call `roborouter.new_run()` between trials.

`control_robot` drives one robot per process. For several robots, give each its own `roborouter.Router()` and call `router.control_robot(...)`.

## Moving by gripper poses

If a controller on your robot can move a gripper to a pose by itself, the model can choose gripper poses instead of joint targets:

```python
config = {"model": "anthropic/claude-opus-5-5", "actions": "gripper_poses",
          "robot": "Two arms, left and right. Base frame at the torso: x forward, y left, z up. At orientation "
                   "[1, 0, 0, 0] a gripper points along +x and its jaws close along y; the tool point is between "
                   "the fingertips. ..."}
report = None

while True:
    state = {"joints": read_my_joints(), "images": {"top": read_my_camera_jpeg()},
             "poses": {"left": {"position": [0.42, 0.18, 0.25], "orientation": [1.0, 0.0, 0.0, 0.0],
                                "gripper_opening_m": 0.08}}}
    if report is not None:
        state["report"] = report
    reply = roborouter.control_robot(config, state, "put the cup in the bowl")
    if reply.finished:
        break
    report = my_controller.move_to(reply.pose)
```

`poses` has each arm the model may move: where its gripper's tool point is, in metres, and its orientation as a unit quaternion `[w, x, y, z]`, both in your robot's base frame, and the distance between the jaws in metres, or `None` if that arm has no working gripper. Put w first: scipy's `Rotation.as_quat()` and ROS's `Quaternion` put it last. `joints` is still needed on every call.

`reply.pose` is `{"arm": ..., "position": [x, y, z], "orientation": [w, x, y, z], "gripper": ...}`, where `gripper` is `"open"` or `"close"`, and `reply.joints` is `None`. The model is told that your controller moves the tool point there in a straight line at a capped speed, then opens or closes the gripper, and refuses a target it can't reach, saying why. Send how the move ended with the next call, as `report`: `{"outcome": ..., "detail": ..., "position_error_mm": ..., "orientation_error_deg": ...}`, where `outcome` is `"reached"`, `"blocked"`, `"timed_out"` or `"refused"`, `detail` is up to 500 characters, and an error you don't know is `None`.

`robot` is optional, up to 12,000 characters, and the model sees it on every move. Say where the base frame's axes point, which way each gripper points and which way its jaws close at orientation `[1, 0, 0, 0]`, where its tool point is, what each arm can reach, what the gripper is and where the cameras are. A run keeps the `actions` and `robot` of its first call; call `roborouter.new_run()` to change them. A call whose `actions` differ from its run's raises `RoboRouterError` with the code `run_mismatch`, and the next call starts a new run.

## Turning the head, raising the torso and driving

A run that moves by gripper poses can offer the model more than `move_to`. List the tools in `config`:

```python
config = {"model": "anthropic/claude-opus-5-5", "actions": "gripper_poses",
          "tools": ["move_to", "look_at", "set_torso_height", "drive"], "robot": "..."}
```

| Tool | Arguments | What your controller does |
|---|---|---|
| `move_to` | `arm`, `position`, `orientation`, `gripper` | Moves one gripper's tool point to the pose, as above |
| `look_at` | `x`, `y`, `z` | Turns the head so the camera looks straight at the point, which puts it in the middle of the image |
| `set_torso_height` | `height_m` | Moves the torso to this height, in metres above its lowest point |
| `drive` | `forward_m`, `left_m`, `turn_deg` | Drives the base straight by `forward_m` along the base frame's x axis and `left_m` along its y axis, from where it is now, then turns it in place by `turn_deg`, counter-clockwise seen from above |
| `ask_person` | `question` | Shows the question to the person at the robot and waits for them to type an answer; nothing moves. Report `"reached"` with the `detail` `"The person answered: <answer>"`, within its 500 characters, or say they gave none |

Positions are in metres in the base frame. The base frame moves with the robot: after a drive, every position you send and every position the model gives is relative to where the base is then, and the model is told so. Leave `tools` out and the run offers `move_to` alone, as before. `poses` is needed only while `move_to` is listed. The order of the list doesn't matter. Like `actions`, the tools go with the run's first call and the run keeps them.

Each reply's `tool` has the model's call, such as `{"name": "drive", "arguments": {"forward_m": 0.3, "left_m": 0.0, "turn_deg": 15.0}}`, and is `None` when the run has finished. For `move_to` the arguments are the pose, which is in `reply.pose` too; for the other tools `reply.pose` is `None`. If you list tools and a reply comes back without `tool`, the server is older than them, and `control_robot` raises `RoboRouterError` with the code `bad_reply`.

Send how each call ended with the next call, as `report`, in the same four fields. For `look_at`, `orientation_error_deg` is the angle between where the camera looks and the point, and `position_error_mm` is `None`. For `set_torso_height`, `position_error_mm` is how far the torso ended from the height. For `drive`, `position_error_mm` is how far the base ended from where it was to go, and `orientation_error_deg` how far its heading is from the one it was to end at.

`state` can also say where the robot's other parts are. Each key is optional, and the model is shown whatever you send:

```python
state = {"joints": read_my_joints(), "images": {"head": read_my_camera_jpeg()},
         "cameras": {"head": {"position": [0.11, 0.07, 1.80], "orientation": [0.5, -0.5, 0.5, -0.5],
                              "width": 1920, "height": 1200, "fx": 734.5, "fy": 734.6, "cx": 953.9, "cy": 607.0}},
         "neck": {"yaw_deg": 0.0, "pitch_deg": 30.0},
         "torso_height_m": 0.6,
         "base": {"x_m": 0.0, "y_m": 0.0, "yaw_deg": 0.0}}
```

- `cameras` has the pose of each camera's lens in the base frame: `position` in metres, and `orientation`, a quaternion `[w, x, y, z]`, of a frame whose z axis points out through the middle of the image, x toward the image's right and y toward its bottom. Then its pinhole numbers in pixels for the image as you send it, so scale them if you shrink the image: `width`, `height`, `fx`, `fy`, `cx` and `cy`, all or none. Name each camera as its image is named in `images`. If a camera's `width` and `height` aren't those of the image sent under its name, `control_robot` raises `RoboRouterError` with the code `invalid_state`. The model is given the frame's three axes as directions and, in a run that lists more than `move_to`, how to turn a place in the image into a direction, worked in fractions of the image so that it holds at whatever size the model is shown the image.
- `neck` has `yaw_deg`, positive turning the head left, and `pitch_deg`, positive tilting it down. Both are 0 with the head facing straight ahead and level.
- `torso_height_m` is in the same units `set_torso_height` takes.
- `base` has `x_m`, `y_m` and `yaw_deg`, counter-clockwise, relative to where the base was when the run started, such as from wheel odometry. The model also sees, for each earlier call, where the base was when that call was made.

`momentum/scripted` only calls `move_to`, so in a run that doesn't list it, it finishes on the first move.

## Recording a run

With `pip install roborouter[record]`, a run is written to an MCAP file on your machine as it happens:

```python
with roborouter.record("runs/") as recording:
    while True:
        state = {"joints": read_my_joints(), "images": {"top": read_my_camera_jpeg()}}
        reply = roborouter.control_robot(config, state, "pick up the fork")
        if reply.finished:
            break
        move_my_robot(reply.joints)
        recording.joints("/my_robot/joints", read_my_joints())
```

Each `control_robot` call writes `/roborouter/turn`, with the run and its actions, robot text and tools, the step, the joints and any poses, report and other state it sent (`cameras`, `neck`, `torso_height_m` and `base`, under `state_extra`), and the reply or the error, and `/roborouter/images/<camera>`, with the exact JPEG it sent. Add anything else your robot reads, from any thread, with `recording.joints(topic, positions, velocities, efforts)`, `recording.image(topic, frame)` and `recording.log(topic, data)`. Each takes an optional `timestamp`, in seconds since the epoch, for a reading that carries its own time.

A background thread does the writing, so no call waits on the disk. If it falls behind, or the disk has less than 10 GB free, frames from `recording.image` are dropped and counted in the file and in `recording.dropped`. Joints, turns and the frames a turn sent are kept. Frames are `foxglove.CompressedImage` messages; everything else is JSON.

`reply.usage` has tokens and cost, including any models that failed before one answered. `reply.timings` has the time each stage took, in milliseconds.
