xsync-cli 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,117 @@
1
+ """The reader of an OpenAI-compatible model endpoint.
2
+
3
+ This module knows nothing about a harness. It makes Model objects.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ import json
9
+ import urllib.error
10
+ import urllib.request
11
+ from typing import Any, Callable
12
+
13
+ from xsync_cli.core.model import Model
14
+
15
+
16
+ class SourceError(Exception):
17
+ """The endpoint answered, but the answer is wrong."""
18
+
19
+
20
+ class EndpointUnreachable(SourceError):
21
+ """The endpoint did not answer."""
22
+
23
+
24
+ def _as_bool(value: Any) -> bool:
25
+ return value is True
26
+
27
+
28
+ def _as_int(value: Any) -> int | None:
29
+ return value if isinstance(value, int) and not isinstance(value, bool) else None
30
+
31
+
32
+ def parse_models(payload: dict[str, Any]) -> list[Model]:
33
+ """Make Model objects from a `/models` payload.
34
+
35
+ The function accepts an endpoint that reports no capabilities. The
36
+ absent fields then hold None or False.
37
+ """
38
+ data = payload.get("data")
39
+ if not isinstance(data, list):
40
+ raise SourceError("the answer has no `data` list. Check the base URL.")
41
+
42
+ models: list[Model] = []
43
+ for entry in data:
44
+ if not isinstance(entry, dict):
45
+ continue
46
+ slug = entry.get("id")
47
+ if not isinstance(slug, str) or not slug:
48
+ continue
49
+ capabilities = entry.get("capabilities")
50
+ if not isinstance(capabilities, dict):
51
+ capabilities = {}
52
+ context_window = _as_int(capabilities.get("contextWindow")) or _as_int(
53
+ entry.get("context_length")
54
+ )
55
+ max_output = _as_int(capabilities.get("maxOutput")) or _as_int(
56
+ entry.get("max_completion_tokens")
57
+ )
58
+ models.append(
59
+ Model(
60
+ slug=slug,
61
+ context_window=context_window,
62
+ max_output=max_output,
63
+ vision=_as_bool(capabilities.get("vision")),
64
+ reasoning=_as_bool(capabilities.get("reasoning")),
65
+ tools=_as_bool(capabilities.get("tools")),
66
+ search=_as_bool(capabilities.get("search")),
67
+ owned_by=entry.get("owned_by"),
68
+ )
69
+ )
70
+ models.sort(key=lambda model: model.slug)
71
+ return models
72
+
73
+
74
+ def fetch_models(
75
+ base_url: str,
76
+ api_key: str | None,
77
+ timeout: float = 10.0,
78
+ retries: int = 2,
79
+ opener: Callable[..., Any] | None = None,
80
+ ) -> list[Model]:
81
+ """Read `{base_url}/models` and make Model objects.
82
+
83
+ The function tries the request `retries + 1` times. The `opener`
84
+ argument exists for the tests.
85
+ """
86
+ url = f"{base_url.rstrip('/')}/models"
87
+ headers = {"Accept": "application/json", "User-Agent": "xsync-cli"}
88
+ if api_key:
89
+ headers["Authorization"] = f"Bearer {api_key}"
90
+ request = urllib.request.Request(url, headers=headers)
91
+ open_url = opener or urllib.request.urlopen
92
+
93
+ last: Exception | None = None
94
+ for _ in range(retries + 1):
95
+ try:
96
+ with open_url(request, timeout=timeout) as response:
97
+ raw = response.read()
98
+ break
99
+ except urllib.error.HTTPError as error:
100
+ if error.code in (401, 403):
101
+ raise SourceError(
102
+ f"the endpoint refused the request with status {error.code}. "
103
+ "Check the API key."
104
+ ) from None
105
+ last = error
106
+ except OSError as error:
107
+ last = error
108
+ else:
109
+ raise EndpointUnreachable(f"no answer from {url}: {last}") from None
110
+
111
+ try:
112
+ payload = json.loads(raw)
113
+ except json.JSONDecodeError as error:
114
+ raise SourceError(f"the answer is not JSON: {error}") from None
115
+ if not isinstance(payload, dict):
116
+ raise SourceError("the answer is not a JSON object.")
117
+ return parse_models(payload)
@@ -0,0 +1,184 @@
1
+ Metadata-Version: 2.5
2
+ Name: xsync-cli
3
+ Version: 0.1.0
4
+ Summary: Sync the model list of an OpenAI-compatible endpoint into the Codex model catalog.
5
+ License-Expression: MIT
6
+ License-File: LICENSE
7
+ Requires-Python: >=3.11
8
+ Requires-Dist: tomlkit>=0.13
9
+ Provides-Extra: dev
10
+ Requires-Dist: pytest>=8.0; extra == 'dev'
11
+ Description-Content-Type: text/markdown
12
+
13
+ # xsync-cli
14
+
15
+ Sync the model list of an OpenAI-compatible endpoint into the Codex model
16
+ catalog.
17
+
18
+ Codex reads its model list from a large JSON catalog file. A person must
19
+ write that file by hand. The file becomes wrong when the endpoint adds a
20
+ model or drops a model. `xsync` writes the file for you.
21
+
22
+ xsync codex
23
+ wrote 35 models to /Users/you/.codex/9router-models.json
24
+
25
+ ## Install
26
+
27
+ uv tool install xsync-cli
28
+
29
+ Or:
30
+
31
+ pipx install xsync-cli
32
+
33
+ The package needs Python 3.11 or later. The command is `xsync`.
34
+
35
+ ## Start
36
+
37
+ ### 1. Make a profile
38
+
39
+ xsync setup
40
+
41
+ The command asks for the profile name, the base URL, and the API key. Leave
42
+ the key empty when the endpoint needs none. The command then reads the
43
+ endpoint and shows every model that it serves.
44
+
45
+ The last question asks for the wire API:
46
+
47
+ - `chat` — most OpenAI-compatible servers.
48
+ - `responses` — the OpenAI Responses API.
49
+
50
+ A wrong value breaks every request. Ask the operator of the endpoint when
51
+ you do not know.
52
+
53
+ ### 2. Connect Codex to the endpoint
54
+
55
+ xsync codex --init
56
+
57
+ The command adds `[model_providers.<profile>]` to `~/.codex/config.toml` and
58
+ points `model_catalog_json` at the catalog of the profile. Run this one time
59
+ for each endpoint.
60
+
61
+ ### 3. Sync
62
+
63
+ xsync codex
64
+
65
+ Run this command again when the endpoint changes.
66
+
67
+ ## Commands
68
+
69
+ | Command | Action |
70
+ |---|---|
71
+ | `xsync setup` | Make a profile. |
72
+ | `xsync list` | Show the profiles. The active profile has a star. |
73
+ | `xsync use <name>` | Set the active profile. |
74
+ | `xsync remove <name>` | Delete a profile. |
75
+ | `xsync codex` | Sync the active profile into the Codex catalog. |
76
+ | `xsync codex --dry-run` | Show the difference. Write nothing. |
77
+ | `xsync codex --profile <name>` | Use another profile for one run. |
78
+ | `xsync codex --init` | Connect Codex to the endpoint of the profile. |
79
+ | `xsync codex --reset` | Remove everything that xSync wrote. |
80
+ | `xsync codex --reset --force` | Reset when no state file exists. |
81
+
82
+ Exit codes: `0` for success, `1` for an error, `2` when the endpoint does not
83
+ answer.
84
+
85
+ ## Switch between endpoints
86
+
87
+ Codex holds one model provider. Therefore one profile is active at a time.
88
+
89
+ xsync codex --profile openrouter --init
90
+
91
+ This command points Codex at the other endpoint and fills the catalog in one
92
+ step.
93
+
94
+ `xsync codex --profile X` without `--init` fills the catalog from X while
95
+ Codex still routes somewhere else. Every model then fails at request time.
96
+ xSync finds this mismatch before it writes, and it stops:
97
+
98
+ catalog would come from "openrouter" (https://openrouter.ai/api/v1)
99
+ but Codex routes to "9router" (http://127.0.0.1:20128/v1)
100
+ run: xsync codex --profile openrouter --init
101
+
102
+ ## The profile file
103
+
104
+ Path: `~/.config/xsync/profiles.toml`. Mode: `0600`.
105
+
106
+ active = "9router"
107
+
108
+ [profiles.9router]
109
+ base_url = "http://127.0.0.1:20128/v1"
110
+ api_key = "sk-..."
111
+ wire_api = "responses"
112
+
113
+ [profiles.openrouter]
114
+ base_url = "https://openrouter.ai/api/v1"
115
+ api_key_env = "OPENROUTER_API_KEY"
116
+ wire_api = "chat"
117
+ exclude = ["*-embedding-*"]
118
+
119
+ A profile uses `api_key` or `api_key_env`, but not both. Use `api_key_env`
120
+ when you share the file. A profile with neither key sends no `Authorization`
121
+ header.
122
+
123
+ The `include` and `exclude` lists hold glob patterns. An empty `include`
124
+ list keeps every model. The `exclude` list always wins.
125
+
126
+ ## Safety
127
+
128
+ - The catalog write is atomic. xSync writes a temporary file, confirms the
129
+ JSON, and then replaces the target. It keeps one backup with the suffix
130
+ `.bak`.
131
+ - The everyday `xsync codex` command never opens `config.toml` for writing.
132
+ Only `--init` and `--reset` do.
133
+ - Codex writes `config.toml` while it runs. Therefore `--init` and `--reset`
134
+ hash the file before the edit and again before the replace. On a
135
+ difference they stop and write nothing.
136
+ - `--reset` removes only what the state file `~/.codex/.xsync-state.json`
137
+ records. It never restores an old copy of `config.toml`, because Codex
138
+ adds project entries to that file over time.
139
+ - With no state file, `--reset` prints the keys that it would remove and
140
+ then stops. Add `--force` to continue.
141
+ - xSync never prints an API key.
142
+
143
+ ## What xSync reads and what it writes
144
+
145
+ | File | Read | Write |
146
+ |---|---|---|
147
+ | `{base_url}/models` | yes | no |
148
+ | `~/.config/xsync/profiles.toml` | yes | yes |
149
+ | `~/.codex/<profile>-models.json` | yes | yes |
150
+ | `~/.codex/config.toml` | yes | only with `--init` or `--reset` |
151
+ | `~/.codex/.xsync-state.json` | yes | only with `--init` or `--reset` |
152
+
153
+ ## The catalog fields
154
+
155
+ The Codex catalog needs 33 fields for each model. The endpoint supplies
156
+ approximately 8 of them:
157
+
158
+ | Codex field | Source |
159
+ |---|---|
160
+ | `slug` | the model id |
161
+ | `context_window`, `max_context_window` | the context window |
162
+ | `input_modalities` | the vision flag |
163
+ | `supported_reasoning_levels` | the reasoning flag |
164
+ | `supports_parallel_tool_calls` | the tool flag |
165
+ | `supports_search_tool` | the search flag |
166
+
167
+ The other fields come from `adapters/rules/codex.toml`. The rules apply in
168
+ three layers. A later layer wins:
169
+
170
+ 1. `[defaults]`
171
+ 2. `[prefix.<first slug segment>]`
172
+ 3. `[slug."<exact slug>"]`
173
+
174
+ Every default value comes from a catalog that works with codex-cli 0.153.4.
175
+
176
+ ## Develop
177
+
178
+ uv run --extra dev pytest
179
+
180
+ No test opens a network connection.
181
+
182
+ ## License
183
+
184
+ MIT.
@@ -0,0 +1,28 @@
1
+ xsync_cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ xsync_cli/cli.py,sha256=dmqD5WwnQC1xlV_zdRJFOSvF6LxVKI2psm7HJz4V9dE,10021
3
+ xsync_cli/adapters/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ xsync_cli/adapters/codex.py,sha256=9bD2MN6QMzTY6XPOBfkcTnqbk5xS4DeTB-JsrY7QdEY,5443
5
+ xsync_cli/adapters/codex_config.py,sha256=wxCkKwdNV9Z0sYtc7RH_rSKFAxTmpd3OcNxPO5OdGSE,3908
6
+ xsync_cli/adapters/codex_wiring.py,sha256=QFVQ90gJdPHQ31SfOGbpg78GX5LfueChKB4eSadG1kQ,4129
7
+ xsync_cli/adapters/prompts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ xsync_cli/adapters/prompts/generic-codex.md,sha256=Wg2UOclZkU_zJ7OgmOGIXtkuxT1xl_osMrT_JoMzDXs,12982
9
+ xsync_cli/adapters/prompts/generic-codex.messages.json,sha256=dWa5MSS1b9aNil9HyrHeofKVOXOECBWX186ryeMbsgQ,15769
10
+ xsync_cli/adapters/prompts/gpt-5.4.md,sha256=mox33od1RakVvptw8c5GRRPXcSFTqHFpT8TVMjd2jwk,14763
11
+ xsync_cli/adapters/prompts/gpt-5.4.messages.json,sha256=cOCgSqsYuTSpZXWktlkZURHcoY6jC_ElRTynHsCv5vY,17554
12
+ xsync_cli/adapters/prompts/gpt-5.5.md,sha256=I1FjHfxWRNxaReqspBOUdb0CgQ7my3ktBYtVFVmzJC4,21473
13
+ xsync_cli/adapters/prompts/gpt-5.5.messages.json,sha256=kWoCWtWGasew-VbLEMvMixukP4OZ_V1umTSvMpVISBE,23437
14
+ xsync_cli/adapters/rules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
+ xsync_cli/adapters/rules/codex.toml,sha256=NU5aUBLt7omIlsGMWWaJ4-nr5h9Mu6Az2MveekBvNSE,1867
16
+ xsync_cli/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
+ xsync_cli/core/atomic.py,sha256=au8JLySKqDwx0anBf5qoTk_tFxEPM49fuwe9MZcSTVU,871
18
+ xsync_cli/core/diff.py,sha256=3Q0T8VH0pEu04Rf_xdTJQh0dRwGPatyIzVjwE7damVE,2730
19
+ xsync_cli/core/filters.py,sha256=0wj5WEoacTDqBrCqRUmT-lxtz1MnxLQj1t9wD9zDrBU,936
20
+ xsync_cli/core/model.py,sha256=nAabM8I5Sl2n3BYuSOtSH9saXKJ8EumKhEoLTew-UeM,901
21
+ xsync_cli/core/profiles.py,sha256=RJ4ywpyfPQdRbOb_ptH5oJzRn2p_IHZ65HLktGbM34E,5783
22
+ xsync_cli/sources/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
+ xsync_cli/sources/openai_compat.py,sha256=XrBOjIJvjbpLRCxzJTjFSsWVDVYceGDjpzilWZunWPQ,3747
24
+ xsync_cli-0.1.0.dist-info/METADATA,sha256=2OIUhmeSO5q_uuILs0EIYsIVFRl4RlkV96vZgfMduQ0,5655
25
+ xsync_cli-0.1.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
26
+ xsync_cli-0.1.0.dist-info/entry_points.txt,sha256=u2_0wRA3g5tfvI0hQGyzXcA7fAiOta0CcugQNHWEKj0,45
27
+ xsync_cli-0.1.0.dist-info/licenses/LICENSE,sha256=sTTr3_kpXclg0Q0D564eHLyDlerOLIZemRPA99ogQBs,1066
28
+ xsync_cli-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ xsync = xsync_cli.cli:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alienstro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.