cli-relay 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. cli_relay-0.1.0/.gitignore +15 -0
  2. cli_relay-0.1.0/CHANGELOG.md +35 -0
  3. cli_relay-0.1.0/LICENSE +202 -0
  4. cli_relay-0.1.0/PKG-INFO +227 -0
  5. cli_relay-0.1.0/README.md +201 -0
  6. cli_relay-0.1.0/README.ru.md +123 -0
  7. cli_relay-0.1.0/examples/cli-relay.toml +50 -0
  8. cli_relay-0.1.0/pyproject.toml +81 -0
  9. cli_relay-0.1.0/src/cli_relay/__init__.py +39 -0
  10. cli_relay-0.1.0/src/cli_relay/__main__.py +5 -0
  11. cli_relay-0.1.0/src/cli_relay/adapters/__init__.py +38 -0
  12. cli_relay-0.1.0/src/cli_relay/adapters/antigravity.py +109 -0
  13. cli_relay-0.1.0/src/cli_relay/adapters/base.py +153 -0
  14. cli_relay-0.1.0/src/cli_relay/adapters/claude.py +169 -0
  15. cli_relay-0.1.0/src/cli_relay/adapters/codex.py +120 -0
  16. cli_relay-0.1.0/src/cli_relay/adapters/openai_compat.py +144 -0
  17. cli_relay-0.1.0/src/cli_relay/cli.py +238 -0
  18. cli_relay-0.1.0/src/cli_relay/config.py +131 -0
  19. cli_relay-0.1.0/src/cli_relay/errors.py +85 -0
  20. cli_relay-0.1.0/src/cli_relay/events.py +115 -0
  21. cli_relay-0.1.0/src/cli_relay/handoff.py +68 -0
  22. cli_relay-0.1.0/src/cli_relay/mcp_server.py +102 -0
  23. cli_relay-0.1.0/src/cli_relay/models.py +67 -0
  24. cli_relay-0.1.0/src/cli_relay/ndjson.py +41 -0
  25. cli_relay-0.1.0/src/cli_relay/proc.py +184 -0
  26. cli_relay-0.1.0/src/cli_relay/py.typed +0 -0
  27. cli_relay-0.1.0/src/cli_relay/quota.py +123 -0
  28. cli_relay-0.1.0/src/cli_relay/relay.py +137 -0
  29. cli_relay-0.1.0/tests/__init__.py +0 -0
  30. cli_relay-0.1.0/tests/conftest.py +70 -0
  31. cli_relay-0.1.0/tests/fakes/fake_cli.py +41 -0
  32. cli_relay-0.1.0/tests/fixtures/agy_quota.ndjson +2 -0
  33. cli_relay-0.1.0/tests/fixtures/agy_success.ndjson +8 -0
  34. cli_relay-0.1.0/tests/fixtures/claude_rate_limited.ndjson +3 -0
  35. cli_relay-0.1.0/tests/fixtures/claude_success.ndjson +10 -0
  36. cli_relay-0.1.0/tests/fixtures/codex_failed.ndjson +4 -0
  37. cli_relay-0.1.0/tests/fixtures/codex_rate_limited.ndjson +6 -0
  38. cli_relay-0.1.0/tests/fixtures/codex_success.ndjson +6 -0
  39. cli_relay-0.1.0/tests/fixtures/garbled.ndjson +4 -0
  40. cli_relay-0.1.0/tests/test_adapters.py +170 -0
  41. cli_relay-0.1.0/tests/test_config_cli.py +138 -0
  42. cli_relay-0.1.0/tests/test_mcp.py +65 -0
  43. cli_relay-0.1.0/tests/test_openai_compat.py +90 -0
  44. cli_relay-0.1.0/tests/test_parsing.py +166 -0
  45. cli_relay-0.1.0/tests/test_relay.py +165 -0
@@ -0,0 +1,15 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ build/
5
+ dist/
6
+ *.egg-info/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ htmlcov/
12
+ /cli-relay.toml
13
+ /HANDOFF.md
14
+ .env
15
+ .venv-*/
@@ -0,0 +1,35 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
5
+ [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2026-11-15
10
+
11
+ ### Added
12
+ - `Relay`: runs a task on the first available provider and falls back on rate limits,
13
+ auth errors, missing CLIs and generic errors (configurable).
14
+ - Adapters for Claude Code (`claude -p --output-format stream-json`), Google Antigravity
15
+ (`agy -p --output-format stream-json`) and any OpenAI-compatible chat endpoint.
16
+ - Adapter for OpenAI Codex (`codex exec --json`); on Windows it also finds the `codex.exe`
17
+ bundled with the Codex desktop app. Command layout and the rate-limit path are verified
18
+ against codex-cli 0.167.
19
+ - Reset times are parsed from "in 2h 30m", wall-clock ("try again at 11:05 AM",
20
+ "resets 3pm") and epoch formats.
21
+ - Normalised event stream: `started`, `text_delta`, `tool_activity`, `quota_info`,
22
+ `completed`, `failed`, `skipped`, `waiting`, `switched`.
23
+ - Persistent quota tracker: remembers limits and reset times; skips a provider
24
+ pre-emptively when a reported usage window is nearly exhausted.
25
+ - Mid-task handoff: the next provider receives the original task, actions taken and the
26
+ last output, plus an instruction to inspect the working tree first.
27
+ - Permission levels (`read_only`, `edit`, `full`) mapped to each CLI's own flags, with a
28
+ per-provider upper bound.
29
+ - `cli-relay` command (`run`, `status`, `reset`, `mcp`) with text and NDJSON output.
30
+ - MCP server (`cli-relay[mcp]`) with `relay_run` and `relay_status` tools. Permissions
31
+ requested over MCP are capped by `[mcp] max_permission` (default `read_only`).
32
+ - TOML configuration.
33
+
34
+ [Unreleased]: https://github.com/mustikon/cli-relay/compare/v0.1.0...HEAD
35
+ [0.1.0]: https://github.com/mustikon/cli-relay/releases/tag/v0.1.0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,227 @@
1
+ Metadata-Version: 2.5
2
+ Name: cli-relay
3
+ Version: 0.1.0
4
+ Summary: Run coding-agent CLIs (Claude Code, Antigravity, Codex, any OpenAI-compatible API) as one worker with automatic fallback when a subscription hits its limit.
5
+ Project-URL: Homepage, https://github.com/mustikon/cli-relay
6
+ Project-URL: Issues, https://github.com/mustikon/cli-relay/issues
7
+ Project-URL: Changelog, https://github.com/mustikon/cli-relay/blob/main/CHANGELOG.md
8
+ Author: mustikon
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: agents,antigravity,claude-code,codex,fallback,gemini,llm,mcp,rate-limit
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Topic :: Software Development
21
+ Classifier: Typing :: Typed
22
+ Requires-Python: >=3.11
23
+ Provides-Extra: mcp
24
+ Requires-Dist: mcp<2,>=1.10; extra == 'mcp'
25
+ Description-Content-Type: text/markdown
26
+
27
+ # cli-relay
28
+
29
+ [![CI](https://github.com/mustikon/cli-relay/actions/workflows/ci.yml/badge.svg)](https://github.com/mustikon/cli-relay/actions/workflows/ci.yml)
30
+ [![PyPI](https://img.shields.io/pypi/v/cli-relay.svg)](https://pypi.org/project/cli-relay/)
31
+ [![Python](https://img.shields.io/pypi/pyversions/cli-relay.svg)](https://pypi.org/project/cli-relay/)
32
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
33
+
34
+ **One worker interface over several coding-agent CLIs, with automatic fallback when a
35
+ subscription hits its limit.**
36
+
37
+ [Русская версия](README.ru.md)
38
+
39
+ You pay for Claude, ChatGPT and Google subscriptions, and each comes with its own agent CLI
40
+ and its own usage window. `cli-relay` runs a task on the first provider that can take it.
41
+ If that provider runs out of quota mid-task, the relay hands the work to the next one with
42
+ a briefing on what was already done.
43
+
44
+ ```
45
+ cli-relay run "fix the failing test in tests/test_api.py" --cwd ~/code/app --permission edit
46
+
47
+ [cli-relay] claude started (claude-opus-5-5)
48
+ ...
49
+ [cli-relay] claude failed [rate_limit]: You've hit your limit · resets 3pm
50
+ [cli-relay] switching claude -> antigravity (rate_limit)
51
+ [cli-relay] antigravity started
52
+ ...
53
+ [cli-relay] done by antigravity
54
+ ```
55
+
56
+ ## Why
57
+
58
+ * **The official CLIs, unmodified.** `cli-relay` launches `claude`, `agy` and `codex`
59
+ exactly as you would, under your own login. It never extracts, proxies or reuses OAuth
60
+ tokens. You keep each tool's own permissions, hooks, `CLAUDE.md` / `AGENTS.md`, skills
61
+ and MCP servers.
62
+ * **Limit-aware.** Claude Code reports its five-hour and weekly windows in its event
63
+ stream. `cli-relay` records them and skips a provider *before* it fails when a window is
64
+ nearly used up. A provider that failed on a limit is skipped until its reset time.
65
+ * **Handoff, not restart.** When it switches providers mid-task, the next agent gets the
66
+ original task, the actions already taken and the last output. It is also told to check
67
+ `git status` / `git diff` before continuing.
68
+ * **One event stream.** Every provider is normalised to the same events (`started`,
69
+ `text_delta`, `tool_activity`, `quota_info`, `completed`, `failed`, plus the relay's
70
+ `skipped` / `waiting` / `switched`). Use `--format ndjson` to drive it from another
71
+ program, such as a voice assistant.
72
+ * **MCP server.** `cli-relay mcp` exposes `relay_run` and `relay_status`, so any MCP client
73
+ can delegate work and get fallback for free.
74
+ * **Small and dependency-free.** The core uses only the Python standard library. That
75
+ matters for a tool that runs next to all your credentials.
76
+
77
+ ## Providers
78
+
79
+ | Type | CLI / endpoint | Status | Session resume | Quota telemetry |
80
+ |---|---|---|---|---|
81
+ | `claude` | Claude Code `claude -p --output-format stream-json` | verified live | `--resume` | five-hour / weekly windows |
82
+ | `antigravity` | Google Antigravity `agy -p --output-format stream-json` | verified live | `--conversation` | from error text |
83
+ | `codex` | OpenAI Codex `codex exec --json` (also finds the copy bundled with the Codex desktop app on Windows) | command layout and limit path verified live on 0.167; success path from docs | `exec resume` | from error text, incl. "try again at 11:05 AM" |
84
+ | `openai` | any OpenAI-compatible `/chat/completions` (OpenRouter, vLLM, Ollama, LM Studio…) | tested with a local server | — | HTTP 429 + `Retry-After` |
85
+
86
+ The `openai` type is a **text-only** fallback: it has no tools and cannot edit files.
87
+
88
+ ## Install
89
+
90
+ ```bash
91
+ pipx install cli-relay # or: uv tool install cli-relay
92
+ pipx install "cli-relay[mcp]" # with the MCP server
93
+ ```
94
+
95
+ Requires Python 3.11+. Install and log in to the agent CLIs you want to use first.
96
+
97
+ ## Usage
98
+
99
+ ```bash
100
+ cli-relay status # what is installed / limited
101
+ cli-relay run "explain src/auth.py" # read-only by default
102
+ cli-relay run - < task.md --cwd ./repo --permission edit
103
+ cli-relay run "continue" --resume claude=SESSION_ID
104
+ cli-relay run "..." --providers antigravity,claude --format ndjson
105
+ cli-relay reset [provider] # forget recorded limits
106
+ ```
107
+
108
+ ### Permissions
109
+
110
+ `--permission` is mapped to each CLI's own permission system. Non-interactive runs never
111
+ wait for a prompt: anything that would ask is denied.
112
+
113
+ | `--permission` | Claude Code | Antigravity | Codex |
114
+ |---|---|---|---|
115
+ | `read_only` (default) | `--permission-mode dontAsk` | `--mode plan` | `--sandbox read-only` |
116
+ | `edit` | `--permission-mode acceptEdits` | `--mode accept-edits` | `--sandbox workspace-write` |
117
+ | `full` | `--permission-mode auto` | `--mode auto` | `workspace-write` + `--approve-for-me` |
118
+
119
+ A provider's `permission` setting is an upper bound, so a task never gets more than that.
120
+ `cli-relay` never uses "skip all permission checks" flags.
121
+
122
+ ## Configuration
123
+
124
+ `cli-relay` looks for its config file in this order:
125
+
126
+ 1. `--config PATH`
127
+ 2. `$CLI_RELAY_CONFIG`
128
+ 3. `./cli-relay.toml`
129
+ 4. `%APPDATA%\cli-relay\config.toml` on Windows, `~/.config/cli-relay/config.toml` elsewhere
130
+
131
+ Without a file the chain is `claude → antigravity → codex`. See
132
+ [`examples/cli-relay.toml`](examples/cli-relay.toml):
133
+
134
+ ```toml
135
+ order = ["claude", "antigravity", "codex", "openrouter", "local"]
136
+
137
+ [relay]
138
+ wait_if_reset_within_s = 900 # wait for a provider that resets within 15 min
139
+ skip_above_utilization = 0.97 # skip Claude when a usage window is ≥ 97 %
140
+ default_cooldown_s = 1800 # when a limit error carries no reset time
141
+ write_handoff_file = false # also write HANDOFF.md into --cwd on a switch
142
+
143
+ [providers.claude]
144
+ model = "opus"
145
+ permission = "edit" # upper bound for this provider
146
+
147
+ [providers.openrouter]
148
+ type = "openai"
149
+ base_url = "https://openrouter.ai/api/v1"
150
+ model = "vendor/model-id" # any OpenRouter model id
151
+ api_key_env = "OPENROUTER_API_KEY"
152
+
153
+ [providers.local]
154
+ type = "openai"
155
+ base_url = "http://127.0.0.1:8000/v1" # vLLM / Ollama / LM Studio
156
+ model = "your-served-model-name"
157
+ ```
158
+
159
+ Recorded limits live in `%LOCALAPPDATA%\cli-relay\state.json` on Windows and
160
+ `~/.local/state/cli-relay/state.json` elsewhere. Override the path with `$CLI_RELAY_STATE`.
161
+
162
+ ## As an MCP server
163
+
164
+ ```bash
165
+ claude mcp add cli-relay -- cli-relay mcp
166
+ ```
167
+
168
+ The server exposes two tools:
169
+ * `relay_run(prompt, cwd?, providers?, permission?)` returns `{ok, provider, text, session_id, switches, skipped}`.
170
+ * `relay_status()` returns installed providers and their known limits.
171
+
172
+ By default an MCP client can only request `read_only` work, whatever it asks for.
173
+ Otherwise a sandboxed agent could escalate by delegating to another one. To allow more,
174
+ set this in the config:
175
+
176
+ ```toml
177
+ [mcp]
178
+ max_permission = "edit"
179
+ ```
180
+
181
+ ## As a library
182
+
183
+ ```python
184
+ import asyncio
185
+ from cli_relay import Relay, Task, TextDelta, Completed
186
+ from cli_relay.config import load
187
+
188
+ async def main() -> None:
189
+ cfg = load()
190
+ relay = Relay(cfg.adapters(), cfg.quota(), cfg.policy)
191
+ async for event in relay.run(Task(prompt="summarise README.md")):
192
+ if isinstance(event, TextDelta):
193
+ print(event.text, end="")
194
+ elif isinstance(event, Completed):
195
+ print(f"\n-- by {event.provider}")
196
+
197
+ asyncio.run(main())
198
+ ```
199
+
200
+ ## Terms of service
201
+
202
+ `cli-relay` is meant for **your own** subscriptions on **your own** machine. It runs each
203
+ vendor's official CLI as a normal user would, and the vendors' terms still apply. For
204
+ unattended automation, the vendors recommend API keys over subscription logins. Do not
205
+ use `cli-relay` to share one subscription between several people.
206
+
207
+ ## Security notes
208
+
209
+ * Agent processes are started from an argument list, never through a shell.
210
+ * On Windows, `claude` and `codex` are npm `.cmd` shims, which `cmd.exe` re-parses. Prompts
211
+ and system prompts are therefore always sent through stdin or a temp file. `cli-relay`
212
+ refuses to put `cmd.exe` metacharacters on such a command line.
213
+ * Corrupted or interleaved NDJSON lines are salvaged rather than crashing a long run.
214
+
215
+ See [SECURITY.md](SECURITY.md) for how to report a vulnerability.
216
+
217
+ ## Roadmap
218
+
219
+ - Voice and push approvals for permission prompts (Claude Code `PermissionRequest` hook,
220
+ Codex app-server approvals)
221
+ - Codex app-server adapter with first-class rate-limit telemetry
222
+ - Git worktree per task with automatic WIP checkpoints
223
+ - Same-harness fallback (Claude Code against an Anthropic-compatible endpoint)
224
+
225
+ ## License
226
+
227
+ [Apache-2.0](LICENSE)
@@ -0,0 +1,201 @@
1
+ # cli-relay
2
+
3
+ [![CI](https://github.com/mustikon/cli-relay/actions/workflows/ci.yml/badge.svg)](https://github.com/mustikon/cli-relay/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/cli-relay.svg)](https://pypi.org/project/cli-relay/)
5
+ [![Python](https://img.shields.io/pypi/pyversions/cli-relay.svg)](https://pypi.org/project/cli-relay/)
6
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
7
+
8
+ **One worker interface over several coding-agent CLIs, with automatic fallback when a
9
+ subscription hits its limit.**
10
+
11
+ [Русская версия](README.ru.md)
12
+
13
+ You pay for Claude, ChatGPT and Google subscriptions, and each comes with its own agent CLI
14
+ and its own usage window. `cli-relay` runs a task on the first provider that can take it.
15
+ If that provider runs out of quota mid-task, the relay hands the work to the next one with
16
+ a briefing on what was already done.
17
+
18
+ ```
19
+ cli-relay run "fix the failing test in tests/test_api.py" --cwd ~/code/app --permission edit
20
+
21
+ [cli-relay] claude started (claude-opus-5-5)
22
+ ...
23
+ [cli-relay] claude failed [rate_limit]: You've hit your limit · resets 3pm
24
+ [cli-relay] switching claude -> antigravity (rate_limit)
25
+ [cli-relay] antigravity started
26
+ ...
27
+ [cli-relay] done by antigravity
28
+ ```
29
+
30
+ ## Why
31
+
32
+ * **The official CLIs, unmodified.** `cli-relay` launches `claude`, `agy` and `codex`
33
+ exactly as you would, under your own login. It never extracts, proxies or reuses OAuth
34
+ tokens. You keep each tool's own permissions, hooks, `CLAUDE.md` / `AGENTS.md`, skills
35
+ and MCP servers.
36
+ * **Limit-aware.** Claude Code reports its five-hour and weekly windows in its event
37
+ stream. `cli-relay` records them and skips a provider *before* it fails when a window is
38
+ nearly used up. A provider that failed on a limit is skipped until its reset time.
39
+ * **Handoff, not restart.** When it switches providers mid-task, the next agent gets the
40
+ original task, the actions already taken and the last output. It is also told to check
41
+ `git status` / `git diff` before continuing.
42
+ * **One event stream.** Every provider is normalised to the same events (`started`,
43
+ `text_delta`, `tool_activity`, `quota_info`, `completed`, `failed`, plus the relay's
44
+ `skipped` / `waiting` / `switched`). Use `--format ndjson` to drive it from another
45
+ program, such as a voice assistant.
46
+ * **MCP server.** `cli-relay mcp` exposes `relay_run` and `relay_status`, so any MCP client
47
+ can delegate work and get fallback for free.
48
+ * **Small and dependency-free.** The core uses only the Python standard library. That
49
+ matters for a tool that runs next to all your credentials.
50
+
51
+ ## Providers
52
+
53
+ | Type | CLI / endpoint | Status | Session resume | Quota telemetry |
54
+ |---|---|---|---|---|
55
+ | `claude` | Claude Code `claude -p --output-format stream-json` | verified live | `--resume` | five-hour / weekly windows |
56
+ | `antigravity` | Google Antigravity `agy -p --output-format stream-json` | verified live | `--conversation` | from error text |
57
+ | `codex` | OpenAI Codex `codex exec --json` (also finds the copy bundled with the Codex desktop app on Windows) | command layout and limit path verified live on 0.167; success path from docs | `exec resume` | from error text, incl. "try again at 11:05 AM" |
58
+ | `openai` | any OpenAI-compatible `/chat/completions` (OpenRouter, vLLM, Ollama, LM Studio…) | tested with a local server | — | HTTP 429 + `Retry-After` |
59
+
60
+ The `openai` type is a **text-only** fallback: it has no tools and cannot edit files.
61
+
62
+ ## Install
63
+
64
+ ```bash
65
+ pipx install cli-relay # or: uv tool install cli-relay
66
+ pipx install "cli-relay[mcp]" # with the MCP server
67
+ ```
68
+
69
+ Requires Python 3.11+. Install and log in to the agent CLIs you want to use first.
70
+
71
+ ## Usage
72
+
73
+ ```bash
74
+ cli-relay status # what is installed / limited
75
+ cli-relay run "explain src/auth.py" # read-only by default
76
+ cli-relay run - < task.md --cwd ./repo --permission edit
77
+ cli-relay run "continue" --resume claude=SESSION_ID
78
+ cli-relay run "..." --providers antigravity,claude --format ndjson
79
+ cli-relay reset [provider] # forget recorded limits
80
+ ```
81
+
82
+ ### Permissions
83
+
84
+ `--permission` is mapped to each CLI's own permission system. Non-interactive runs never
85
+ wait for a prompt: anything that would ask is denied.
86
+
87
+ | `--permission` | Claude Code | Antigravity | Codex |
88
+ |---|---|---|---|
89
+ | `read_only` (default) | `--permission-mode dontAsk` | `--mode plan` | `--sandbox read-only` |
90
+ | `edit` | `--permission-mode acceptEdits` | `--mode accept-edits` | `--sandbox workspace-write` |
91
+ | `full` | `--permission-mode auto` | `--mode auto` | `workspace-write` + `--approve-for-me` |
92
+
93
+ A provider's `permission` setting is an upper bound, so a task never gets more than that.
94
+ `cli-relay` never uses "skip all permission checks" flags.
95
+
96
+ ## Configuration
97
+
98
+ `cli-relay` looks for its config file in this order:
99
+
100
+ 1. `--config PATH`
101
+ 2. `$CLI_RELAY_CONFIG`
102
+ 3. `./cli-relay.toml`
103
+ 4. `%APPDATA%\cli-relay\config.toml` on Windows, `~/.config/cli-relay/config.toml` elsewhere
104
+
105
+ Without a file the chain is `claude → antigravity → codex`. See
106
+ [`examples/cli-relay.toml`](examples/cli-relay.toml):
107
+
108
+ ```toml
109
+ order = ["claude", "antigravity", "codex", "openrouter", "local"]
110
+
111
+ [relay]
112
+ wait_if_reset_within_s = 900 # wait for a provider that resets within 15 min
113
+ skip_above_utilization = 0.97 # skip Claude when a usage window is ≥ 97 %
114
+ default_cooldown_s = 1800 # when a limit error carries no reset time
115
+ write_handoff_file = false # also write HANDOFF.md into --cwd on a switch
116
+
117
+ [providers.claude]
118
+ model = "opus"
119
+ permission = "edit" # upper bound for this provider
120
+
121
+ [providers.openrouter]
122
+ type = "openai"
123
+ base_url = "https://openrouter.ai/api/v1"
124
+ model = "vendor/model-id" # any OpenRouter model id
125
+ api_key_env = "OPENROUTER_API_KEY"
126
+
127
+ [providers.local]
128
+ type = "openai"
129
+ base_url = "http://127.0.0.1:8000/v1" # vLLM / Ollama / LM Studio
130
+ model = "your-served-model-name"
131
+ ```
132
+
133
+ Recorded limits live in `%LOCALAPPDATA%\cli-relay\state.json` on Windows and
134
+ `~/.local/state/cli-relay/state.json` elsewhere. Override the path with `$CLI_RELAY_STATE`.
135
+
136
+ ## As an MCP server
137
+
138
+ ```bash
139
+ claude mcp add cli-relay -- cli-relay mcp
140
+ ```
141
+
142
+ The server exposes two tools:
143
+ * `relay_run(prompt, cwd?, providers?, permission?)` returns `{ok, provider, text, session_id, switches, skipped}`.
144
+ * `relay_status()` returns installed providers and their known limits.
145
+
146
+ By default an MCP client can only request `read_only` work, whatever it asks for.
147
+ Otherwise a sandboxed agent could escalate by delegating to another one. To allow more,
148
+ set this in the config:
149
+
150
+ ```toml
151
+ [mcp]
152
+ max_permission = "edit"
153
+ ```
154
+
155
+ ## As a library
156
+
157
+ ```python
158
+ import asyncio
159
+ from cli_relay import Relay, Task, TextDelta, Completed
160
+ from cli_relay.config import load
161
+
162
+ async def main() -> None:
163
+ cfg = load()
164
+ relay = Relay(cfg.adapters(), cfg.quota(), cfg.policy)
165
+ async for event in relay.run(Task(prompt="summarise README.md")):
166
+ if isinstance(event, TextDelta):
167
+ print(event.text, end="")
168
+ elif isinstance(event, Completed):
169
+ print(f"\n-- by {event.provider}")
170
+
171
+ asyncio.run(main())
172
+ ```
173
+
174
+ ## Terms of service
175
+
176
+ `cli-relay` is meant for **your own** subscriptions on **your own** machine. It runs each
177
+ vendor's official CLI as a normal user would, and the vendors' terms still apply. For
178
+ unattended automation, the vendors recommend API keys over subscription logins. Do not
179
+ use `cli-relay` to share one subscription between several people.
180
+
181
+ ## Security notes
182
+
183
+ * Agent processes are started from an argument list, never through a shell.
184
+ * On Windows, `claude` and `codex` are npm `.cmd` shims, which `cmd.exe` re-parses. Prompts
185
+ and system prompts are therefore always sent through stdin or a temp file. `cli-relay`
186
+ refuses to put `cmd.exe` metacharacters on such a command line.
187
+ * Corrupted or interleaved NDJSON lines are salvaged rather than crashing a long run.
188
+
189
+ See [SECURITY.md](SECURITY.md) for how to report a vulnerability.
190
+
191
+ ## Roadmap
192
+
193
+ - Voice and push approvals for permission prompts (Claude Code `PermissionRequest` hook,
194
+ Codex app-server approvals)
195
+ - Codex app-server adapter with first-class rate-limit telemetry
196
+ - Git worktree per task with automatic WIP checkpoints
197
+ - Same-harness fallback (Claude Code against an Anthropic-compatible endpoint)
198
+
199
+ ## License
200
+
201
+ [Apache-2.0](LICENSE)