codex-api-proxy 0.1.2__tar.gz → 0.1.4__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 (25) hide show
  1. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/PKG-INFO +42 -1
  2. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/README.md +41 -0
  3. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/pyproject.toml +1 -1
  4. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy/__init__.py +1 -1
  5. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy/chat_completions.py +64 -1
  6. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy/config.py +9 -0
  7. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy.egg-info/PKG-INFO +42 -1
  8. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/tests/test_api.py +52 -0
  9. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/tests/test_chat_completions.py +78 -0
  10. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/tests/test_config.py +50 -0
  11. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/setup.cfg +0 -0
  12. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy/auth.py +0 -0
  13. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy/cli.py +0 -0
  14. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy/main.py +0 -0
  15. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy/models.py +0 -0
  16. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy/sse_utils.py +0 -0
  17. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy.egg-info/SOURCES.txt +0 -0
  18. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy.egg-info/dependency_links.txt +0 -0
  19. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy.egg-info/entry_points.txt +0 -0
  20. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy.egg-info/requires.txt +0 -0
  21. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/src/codex_api_proxy.egg-info/top_level.txt +0 -0
  22. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/tests/test_auth.py +0 -0
  23. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/tests/test_cli.py +0 -0
  24. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/tests/test_models.py +0 -0
  25. {codex_api_proxy-0.1.2 → codex_api_proxy-0.1.4}/tests/test_release_version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: codex-api-proxy
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Summary: Local OpenAI-compatible HTTP proxy backed by Codex/OpenAI credentials
5
5
  Author: codex-api-proxy contributors
6
6
  License-Expression: MIT
@@ -102,6 +102,20 @@ You can also set the upstream proxy explicitly at startup:
102
102
  codex-api-proxy start --proxy=http://127.0.0.1:8118
103
103
  ```
104
104
 
105
+ If the upstream connection passes through a corporate TLS proxy or another endpoint
106
+ using a private/self-signed certificate chain, point the proxy at a CA bundle that
107
+ trusts that chain:
108
+
109
+ - `CODEX_API_PROXY_CA_BUNDLE`
110
+ - `REQUESTS_CA_BUNDLE`
111
+ - `SSL_CERT_FILE`
112
+
113
+ For example:
114
+
115
+ ```bash
116
+ CODEX_API_PROXY_CA_BUNDLE=/path/to/internal-ca-bundle.pem codex-api-proxy start
117
+ ```
118
+
105
119
  ## Run
106
120
 
107
121
  Start in the background:
@@ -164,6 +178,7 @@ Environment variables for the local server:
164
178
  - `CODEX_PROXY_PORT`
165
179
  - `CODEX_PROXY_API_KEY`
166
180
  - `CODEX_API_PROXY_HTTPS_PROXY`
181
+ - `CODEX_API_PROXY_CA_BUNDLE`
167
182
  - `CODEX_PROXY_LOG_LEVEL`
168
183
 
169
184
  Token refresh compatibility variables:
@@ -209,6 +224,32 @@ curl -sS http://127.0.0.1:8765/v1/responses \
209
224
  -d '{"model":"gpt-5.5","input":"Reply with exactly: pong"}'
210
225
  ```
211
226
 
227
+ Image input through Chat Completions:
228
+
229
+ ```bash
230
+ BASE64_IMAGE=$(base64 < image.jpg)
231
+ curl -sS http://127.0.0.1:8765/v1/chat/completions \
232
+ -H 'Content-Type: application/json' \
233
+ -d '{
234
+ "model": "gpt-5.5",
235
+ "messages": [{
236
+ "role": "user",
237
+ "content": [
238
+ {"type": "text", "text": "What is in this image?"},
239
+ {
240
+ "type": "image_url",
241
+ "image_url": {
242
+ "url": "data:image/jpeg;base64,'"$BASE64_IMAGE"'",
243
+ "detail": "high"
244
+ }
245
+ }
246
+ ]
247
+ }]
248
+ }'
249
+ ```
250
+
251
+ When using ChatGPT Codex credentials, Chat Completions image parts are converted to Responses API `input_image` parts. `/v1/responses` requests are passed through unchanged.
252
+
212
253
  When `--api-key` is configured:
213
254
 
214
255
  ```bash
@@ -78,6 +78,20 @@ You can also set the upstream proxy explicitly at startup:
78
78
  codex-api-proxy start --proxy=http://127.0.0.1:8118
79
79
  ```
80
80
 
81
+ If the upstream connection passes through a corporate TLS proxy or another endpoint
82
+ using a private/self-signed certificate chain, point the proxy at a CA bundle that
83
+ trusts that chain:
84
+
85
+ - `CODEX_API_PROXY_CA_BUNDLE`
86
+ - `REQUESTS_CA_BUNDLE`
87
+ - `SSL_CERT_FILE`
88
+
89
+ For example:
90
+
91
+ ```bash
92
+ CODEX_API_PROXY_CA_BUNDLE=/path/to/internal-ca-bundle.pem codex-api-proxy start
93
+ ```
94
+
81
95
  ## Run
82
96
 
83
97
  Start in the background:
@@ -140,6 +154,7 @@ Environment variables for the local server:
140
154
  - `CODEX_PROXY_PORT`
141
155
  - `CODEX_PROXY_API_KEY`
142
156
  - `CODEX_API_PROXY_HTTPS_PROXY`
157
+ - `CODEX_API_PROXY_CA_BUNDLE`
143
158
  - `CODEX_PROXY_LOG_LEVEL`
144
159
 
145
160
  Token refresh compatibility variables:
@@ -185,6 +200,32 @@ curl -sS http://127.0.0.1:8765/v1/responses \
185
200
  -d '{"model":"gpt-5.5","input":"Reply with exactly: pong"}'
186
201
  ```
187
202
 
203
+ Image input through Chat Completions:
204
+
205
+ ```bash
206
+ BASE64_IMAGE=$(base64 < image.jpg)
207
+ curl -sS http://127.0.0.1:8765/v1/chat/completions \
208
+ -H 'Content-Type: application/json' \
209
+ -d '{
210
+ "model": "gpt-5.5",
211
+ "messages": [{
212
+ "role": "user",
213
+ "content": [
214
+ {"type": "text", "text": "What is in this image?"},
215
+ {
216
+ "type": "image_url",
217
+ "image_url": {
218
+ "url": "data:image/jpeg;base64,'"$BASE64_IMAGE"'",
219
+ "detail": "high"
220
+ }
221
+ }
222
+ ]
223
+ }]
224
+ }'
225
+ ```
226
+
227
+ When using ChatGPT Codex credentials, Chat Completions image parts are converted to Responses API `input_image` parts. `/v1/responses` requests are passed through unchanged.
228
+
188
229
  When `--api-key` is configured:
189
230
 
190
231
  ```bash
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "codex-api-proxy"
3
- version = "0.1.2"
3
+ version = "0.1.4"
4
4
  description = "Local OpenAI-compatible HTTP proxy backed by Codex/OpenAI credentials"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -1,3 +1,3 @@
1
1
  """OpenAI-compatible HTTP proxy backed by Codex/OpenAI credentials."""
2
2
 
3
- __version__ = "0.1.2"
3
+ __version__ = "0.1.4"
@@ -46,7 +46,7 @@ def chat_to_responses_request(
46
46
  {
47
47
  "type": "message",
48
48
  "role": "user",
49
- "content": [{"type": "input_text", "text": text}],
49
+ "content": message_content_to_response_content(message.get("content", "")),
50
50
  }
51
51
  )
52
52
  continue
@@ -147,6 +147,69 @@ def message_content_to_text(content: object) -> str:
147
147
  return "".join(parts)
148
148
 
149
149
 
150
+ def message_content_to_response_content(content: object) -> list[dict[str, str]]:
151
+ if isinstance(content, str):
152
+ return [{"type": "input_text", "text": content}]
153
+ if not isinstance(content, list):
154
+ return [{"type": "input_text", "text": ""}]
155
+
156
+ parts: list[dict[str, str]] = []
157
+ for item in content:
158
+ if isinstance(item, str):
159
+ parts.append({"type": "input_text", "text": item})
160
+ continue
161
+ if not isinstance(item, dict):
162
+ continue
163
+
164
+ part_type = item.get("type")
165
+ text = item.get("text")
166
+ if part_type in {"text", "input_text"} and isinstance(text, str):
167
+ parts.append({"type": "input_text", "text": text})
168
+ continue
169
+
170
+ image_part = _image_content_part(item)
171
+ if image_part:
172
+ parts.append(image_part)
173
+
174
+ return parts or [{"type": "input_text", "text": ""}]
175
+
176
+
177
+ def _image_content_part(item: dict) -> dict[str, str] | None:
178
+ part_type = item.get("type")
179
+ if part_type == "image_url":
180
+ image_url = item.get("image_url")
181
+ url: object
182
+ detail: object = item.get("detail")
183
+ if isinstance(image_url, dict):
184
+ url = image_url.get("url")
185
+ detail = image_url.get("detail", detail)
186
+ else:
187
+ url = image_url
188
+ if not isinstance(url, str):
189
+ return None
190
+ part = {"type": "input_image", "image_url": url}
191
+ if isinstance(detail, str):
192
+ part["detail"] = detail
193
+ return part
194
+
195
+ if part_type == "input_image":
196
+ part = {"type": "input_image"}
197
+ image_url = item.get("image_url")
198
+ file_id = item.get("file_id")
199
+ detail = item.get("detail")
200
+ if isinstance(image_url, str):
201
+ part["image_url"] = image_url
202
+ if isinstance(file_id, str):
203
+ part["file_id"] = file_id
204
+ if isinstance(detail, str):
205
+ part["detail"] = detail
206
+ if "image_url" not in part and "file_id" not in part:
207
+ return None
208
+ return part
209
+
210
+ return None
211
+
212
+
150
213
  def extract_output_text(response: dict) -> str:
151
214
  chunks: list[str] = []
152
215
  for item in response.get("output", []):
@@ -76,6 +76,14 @@ def upstream_https_proxy(explicit_proxy: str | None = None) -> str | None:
76
76
  return DEFAULT_HTTPS_PROXY
77
77
 
78
78
 
79
+ def upstream_ssl_verify() -> bool | str:
80
+ for key in ("CODEX_API_PROXY_CA_BUNDLE", "REQUESTS_CA_BUNDLE", "SSL_CERT_FILE"):
81
+ value = os.environ.get(key, "").strip()
82
+ if value:
83
+ return value
84
+ return True
85
+
86
+
79
87
  def upstream_originator() -> str:
80
88
  override = os.environ.get("CODEX_INTERNAL_ORIGINATOR_OVERRIDE", "").strip()
81
89
  return override or DEFAULT_ORIGINATOR
@@ -95,6 +103,7 @@ def upstream_client(explicit_proxy: str | None = None) -> httpx.AsyncClient:
95
103
  return httpx.AsyncClient(
96
104
  timeout=DEFAULT_TIMEOUT,
97
105
  proxy=upstream_https_proxy(explicit_proxy),
106
+ verify=upstream_ssl_verify(),
98
107
  trust_env=False,
99
108
  )
100
109
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: codex-api-proxy
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Summary: Local OpenAI-compatible HTTP proxy backed by Codex/OpenAI credentials
5
5
  Author: codex-api-proxy contributors
6
6
  License-Expression: MIT
@@ -102,6 +102,20 @@ You can also set the upstream proxy explicitly at startup:
102
102
  codex-api-proxy start --proxy=http://127.0.0.1:8118
103
103
  ```
104
104
 
105
+ If the upstream connection passes through a corporate TLS proxy or another endpoint
106
+ using a private/self-signed certificate chain, point the proxy at a CA bundle that
107
+ trusts that chain:
108
+
109
+ - `CODEX_API_PROXY_CA_BUNDLE`
110
+ - `REQUESTS_CA_BUNDLE`
111
+ - `SSL_CERT_FILE`
112
+
113
+ For example:
114
+
115
+ ```bash
116
+ CODEX_API_PROXY_CA_BUNDLE=/path/to/internal-ca-bundle.pem codex-api-proxy start
117
+ ```
118
+
105
119
  ## Run
106
120
 
107
121
  Start in the background:
@@ -164,6 +178,7 @@ Environment variables for the local server:
164
178
  - `CODEX_PROXY_PORT`
165
179
  - `CODEX_PROXY_API_KEY`
166
180
  - `CODEX_API_PROXY_HTTPS_PROXY`
181
+ - `CODEX_API_PROXY_CA_BUNDLE`
167
182
  - `CODEX_PROXY_LOG_LEVEL`
168
183
 
169
184
  Token refresh compatibility variables:
@@ -209,6 +224,32 @@ curl -sS http://127.0.0.1:8765/v1/responses \
209
224
  -d '{"model":"gpt-5.5","input":"Reply with exactly: pong"}'
210
225
  ```
211
226
 
227
+ Image input through Chat Completions:
228
+
229
+ ```bash
230
+ BASE64_IMAGE=$(base64 < image.jpg)
231
+ curl -sS http://127.0.0.1:8765/v1/chat/completions \
232
+ -H 'Content-Type: application/json' \
233
+ -d '{
234
+ "model": "gpt-5.5",
235
+ "messages": [{
236
+ "role": "user",
237
+ "content": [
238
+ {"type": "text", "text": "What is in this image?"},
239
+ {
240
+ "type": "image_url",
241
+ "image_url": {
242
+ "url": "data:image/jpeg;base64,'"$BASE64_IMAGE"'",
243
+ "detail": "high"
244
+ }
245
+ }
246
+ ]
247
+ }]
248
+ }'
249
+ ```
250
+
251
+ When using ChatGPT Codex credentials, Chat Completions image parts are converted to Responses API `input_image` parts. `/v1/responses` requests are passed through unchanged.
252
+
212
253
  When `--api-key` is configured:
213
254
 
214
255
  ```bash
@@ -170,6 +170,58 @@ def test_chatgpt_mode_converts_chat_completion_to_responses(tmp_path: Path, monk
170
170
  assert seen["body"]["store"] is False
171
171
 
172
172
 
173
+ def test_chatgpt_mode_converts_chat_completion_images_to_responses(tmp_path: Path, monkeypatch) -> None:
174
+ seen = {}
175
+
176
+ def handler(request: httpx.Request) -> httpx.Response:
177
+ seen["body"] = json.loads(request.content)
178
+ body = "\n\n".join(
179
+ [
180
+ 'data: {"type":"response.output_text.delta","delta":"a cat"}',
181
+ 'data: {"type":"response.completed","response":{"output":[]}}',
182
+ "",
183
+ ]
184
+ )
185
+ return httpx.Response(200, content=body, headers={"content-type": "text/event-stream"})
186
+
187
+ mock_auto_auth(monkeypatch, chatgpt_auth(tmp_path))
188
+ mock_upstream(monkeypatch, handler)
189
+ app = create_app(Settings())
190
+ client = TestClient(app)
191
+
192
+ response = client.post(
193
+ "/v1/chat/completions",
194
+ json={
195
+ "model": "gpt-5.5",
196
+ "messages": [
197
+ {
198
+ "role": "user",
199
+ "content": [
200
+ {"type": "text", "text": "what is in this image?"},
201
+ {
202
+ "type": "image_url",
203
+ "image_url": {
204
+ "url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ==",
205
+ "detail": "high",
206
+ },
207
+ },
208
+ ],
209
+ }
210
+ ],
211
+ },
212
+ )
213
+
214
+ assert response.status_code == 200
215
+ assert seen["body"]["input"][0]["content"] == [
216
+ {"type": "input_text", "text": "what is in this image?"},
217
+ {
218
+ "type": "input_image",
219
+ "image_url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ==",
220
+ "detail": "high",
221
+ },
222
+ ]
223
+
224
+
173
225
  def test_chatgpt_mode_applies_default_latency_controls(tmp_path: Path, monkeypatch) -> None:
174
226
  seen = {}
175
227
 
@@ -27,6 +27,84 @@ def test_chat_to_responses_request_maps_messages() -> None:
27
27
  ]
28
28
 
29
29
 
30
+ def test_chat_to_responses_request_maps_image_content_parts() -> None:
31
+ payload = chat_to_responses_request(
32
+ {
33
+ "model": "gpt-5.5",
34
+ "messages": [
35
+ {
36
+ "role": "user",
37
+ "content": [
38
+ {"type": "text", "text": "what is in this image?"},
39
+ {
40
+ "type": "image_url",
41
+ "image_url": {
42
+ "url": "https://example.com/cat.jpg",
43
+ "detail": "high",
44
+ },
45
+ },
46
+ {
47
+ "type": "image_url",
48
+ "image_url": {
49
+ "url": "data:image/png;base64,iVBORw0KGgo=",
50
+ },
51
+ },
52
+ ],
53
+ }
54
+ ],
55
+ }
56
+ )
57
+
58
+ assert payload["input"] == [
59
+ {
60
+ "type": "message",
61
+ "role": "user",
62
+ "content": [
63
+ {"type": "input_text", "text": "what is in this image?"},
64
+ {
65
+ "type": "input_image",
66
+ "image_url": "https://example.com/cat.jpg",
67
+ "detail": "high",
68
+ },
69
+ {
70
+ "type": "input_image",
71
+ "image_url": "data:image/png;base64,iVBORw0KGgo=",
72
+ },
73
+ ],
74
+ }
75
+ ]
76
+
77
+
78
+ def test_chat_to_responses_request_preserves_responses_image_content_parts() -> None:
79
+ payload = chat_to_responses_request(
80
+ {
81
+ "model": "gpt-5.5",
82
+ "messages": [
83
+ {
84
+ "role": "user",
85
+ "content": [
86
+ {"type": "input_text", "text": "compare these images"},
87
+ {
88
+ "type": "input_image",
89
+ "image_url": "https://example.com/a.jpg",
90
+ "detail": "low",
91
+ },
92
+ ],
93
+ }
94
+ ],
95
+ }
96
+ )
97
+
98
+ assert payload["input"][0]["content"] == [
99
+ {"type": "input_text", "text": "compare these images"},
100
+ {
101
+ "type": "input_image",
102
+ "image_url": "https://example.com/a.jpg",
103
+ "detail": "low",
104
+ },
105
+ ]
106
+
107
+
30
108
  def test_chat_to_responses_request_applies_default_latency_controls() -> None:
31
109
  payload = chat_to_responses_request(
32
110
  {
@@ -1,5 +1,6 @@
1
1
  from codex_api_proxy.config import DEFAULT_HTTPS_PROXY
2
2
  from codex_api_proxy.config import Settings
3
+ from codex_api_proxy.config import upstream_client
3
4
  from codex_api_proxy.config import upstream_https_proxy
4
5
 
5
6
 
@@ -64,3 +65,52 @@ def test_upstream_https_proxy_defaults_to_privoxy(monkeypatch) -> None:
64
65
  def test_upstream_https_proxy_env_override(monkeypatch) -> None:
65
66
  monkeypatch.setenv("CODEX_API_PROXY_HTTPS_PROXY", "http://127.0.0.1:9999")
66
67
  assert upstream_https_proxy() == "http://127.0.0.1:9999"
68
+
69
+
70
+ def test_upstream_client_defaults_to_system_tls_trust(monkeypatch) -> None:
71
+ for key in ("CODEX_API_PROXY_CA_BUNDLE", "SSL_CERT_FILE", "REQUESTS_CA_BUNDLE"):
72
+ monkeypatch.delenv(key, raising=False)
73
+ captured = {}
74
+
75
+ def fake_async_client(**kwargs):
76
+ captured.update(kwargs)
77
+ return object()
78
+
79
+ monkeypatch.setattr("codex_api_proxy.config.httpx.AsyncClient", fake_async_client)
80
+
81
+ upstream_client()
82
+
83
+ assert captured["verify"] is True
84
+
85
+
86
+ def test_upstream_client_uses_proxy_ca_bundle(monkeypatch) -> None:
87
+ monkeypatch.setenv("CODEX_API_PROXY_CA_BUNDLE", "/etc/ssl/certs/internal-ca.pem")
88
+ monkeypatch.setenv("SSL_CERT_FILE", "/etc/ssl/certs/ignored.pem")
89
+ captured = {}
90
+
91
+ def fake_async_client(**kwargs):
92
+ captured.update(kwargs)
93
+ return object()
94
+
95
+ monkeypatch.setattr("codex_api_proxy.config.httpx.AsyncClient", fake_async_client)
96
+
97
+ upstream_client()
98
+
99
+ assert captured["verify"] == "/etc/ssl/certs/internal-ca.pem"
100
+
101
+
102
+ def test_upstream_client_uses_common_ca_bundle_env(monkeypatch) -> None:
103
+ monkeypatch.delenv("CODEX_API_PROXY_CA_BUNDLE", raising=False)
104
+ monkeypatch.setenv("REQUESTS_CA_BUNDLE", "/etc/ssl/certs/requests-ca.pem")
105
+ monkeypatch.setenv("SSL_CERT_FILE", "/etc/ssl/certs/ssl-ca.pem")
106
+ captured = {}
107
+
108
+ def fake_async_client(**kwargs):
109
+ captured.update(kwargs)
110
+ return object()
111
+
112
+ monkeypatch.setattr("codex_api_proxy.config.httpx.AsyncClient", fake_async_client)
113
+
114
+ upstream_client()
115
+
116
+ assert captured["verify"] == "/etc/ssl/certs/requests-ca.pem"