visionai-sdk-python 2.2.0.dev6__tar.gz → 2.3.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.
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/.github/workflows/ci.yml +2 -0
- {visionai_sdk_python-2.2.0.dev6/src/visionai_sdk_python.egg-info → visionai_sdk_python-2.3.0}/PKG-INFO +35 -7
- visionai_sdk_python-2.2.0.dev6/PKG-INFO → visionai_sdk_python-2.3.0/README.md +32 -24
- visionai_sdk_python-2.3.0/docs/relay-protocol.md +284 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/pyproject.toml +2 -0
- visionai_sdk_python-2.3.0/src/visionai_sdk_python/exceptions.py +150 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/instrumentation.py +12 -4
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/__init__.py +3 -2
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/__init__.py +2 -2
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/protocol.py +3 -4
- visionai_sdk_python-2.2.0.dev6/README.md → visionai_sdk_python-2.3.0/src/visionai_sdk_python.egg-info/PKG-INFO +52 -6
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python.egg-info/SOURCES.txt +2 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python.egg-info/requires.txt +3 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python.egg-info/scm_file_list.json +2 -0
- visionai_sdk_python-2.3.0/src/visionai_sdk_python.egg-info/scm_version.json +8 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/constants.py +7 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/relay/test_protocol.py +6 -7
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/test_client.py +17 -2
- visionai_sdk_python-2.3.0/tests/test_exceptions.py +129 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/test_instrumentation.py +25 -16
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/uv.lock +59 -5
- visionai_sdk_python-2.2.0.dev6/src/visionai_sdk_python/exceptions.py +0 -97
- visionai_sdk_python-2.2.0.dev6/src/visionai_sdk_python.egg-info/scm_version.json +0 -8
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/.github/dependabot.yml +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/.github/workflows/publish.yml +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/.gitignore +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/setup.cfg +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/__init__.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/_base.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/_jwt_verifier.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/_request_attribution.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/_source_header.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/async_client.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/auth/__init__.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/auth/_mixin.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/auth/async_resource.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/auth/credentials.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/auth/models.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/auth/resource.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/client.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/constants.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/endpoints.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/py.typed +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/error_connection.json +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/error_request.json +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/heartbeat.json +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/register.json +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/registered.json +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/request.json +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/response.json +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/relay/examples/response_text.json +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/vlm/__init__.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/vlm/_mixin.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/vlm/async_resource.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/vlm/models.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/vlm/preprocess.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python/vlm/resource.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python.egg-info/dependency_links.txt +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/src/visionai_sdk_python.egg-info/top_level.txt +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/__init__.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/relay/__init__.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/test__base.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/test_async_client.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/test_credentials.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/test_request.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/test_transport_options.py +0 -0
- {visionai_sdk_python-2.2.0.dev6 → visionai_sdk_python-2.3.0}/tests/test_vlm_preprocess.py +0 -0
|
@@ -72,6 +72,7 @@ jobs:
|
|
|
72
72
|
run: >-
|
|
73
73
|
uv pip install
|
|
74
74
|
"httpx==0.28.1"
|
|
75
|
+
"httpx2==2.13.*"
|
|
75
76
|
"requests==2.31.*"
|
|
76
77
|
"aiohttp==3.9.*"
|
|
77
78
|
"wrapt==1.16.*"
|
|
@@ -81,6 +82,7 @@ jobs:
|
|
|
81
82
|
run: >-
|
|
82
83
|
uv pip install --upgrade
|
|
83
84
|
"httpx>=0.28.1"
|
|
85
|
+
"httpx2>=2.13,<3"
|
|
84
86
|
"requests>=2.31,<3"
|
|
85
87
|
"aiohttp>=3.9,<4"
|
|
86
88
|
"wrapt>=1.16,<3"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: visionai-sdk-python
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.3.0
|
|
4
4
|
Summary: VisionAI SDK for Python
|
|
5
5
|
Author-email: Tony Yang <tonyyang@linkervision.com>
|
|
6
6
|
Requires-Python: >=3.11
|
|
@@ -13,6 +13,8 @@ Provides-Extra: requests
|
|
|
13
13
|
Requires-Dist: requests<3,>=2.31; extra == "requests"
|
|
14
14
|
Provides-Extra: aiohttp
|
|
15
15
|
Requires-Dist: aiohttp<4,>=3.9; extra == "aiohttp"
|
|
16
|
+
Provides-Extra: httpx2
|
|
17
|
+
Requires-Dist: httpx2<3,>=2.13; extra == "httpx2"
|
|
16
18
|
Provides-Extra: instrumentation
|
|
17
19
|
Requires-Dist: wrapt<3,>=1.16; extra == "instrumentation"
|
|
18
20
|
|
|
@@ -43,6 +45,7 @@ Attributing direct `requests`/`aiohttp` calls, or using `instrumentation.instrum
|
|
|
43
45
|
```bash
|
|
44
46
|
pip install visionai-sdk-python[requests]
|
|
45
47
|
pip install visionai-sdk-python[aiohttp]
|
|
48
|
+
pip install visionai-sdk-python[httpx2]
|
|
46
49
|
pip install visionai-sdk-python[instrumentation] # needed for instrument() itself
|
|
47
50
|
```
|
|
48
51
|
|
|
@@ -177,11 +180,13 @@ authentication wins. `vlm_url` is optional for clients that only authenticate.
|
|
|
177
180
|
`visionai_sdk_python.relay` holds the wire format between platform-relay and
|
|
178
181
|
platform-edge-agent: the frame models, the constants and `parse_agent_frame()` /
|
|
179
182
|
`parse_relay_frame()` (text frames only: `bytes` or `None` is an `InvalidFrame`) /
|
|
180
|
-
`dump_frame()` (which refuses a frame over `MAX_FRAME_BYTES`). The spec is
|
|
181
|
-
`docs/
|
|
182
|
-
copy
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
`dump_frame()` (which refuses a frame over `MAX_FRAME_BYTES`). The spec is
|
|
184
|
+
[`docs/relay-protocol.md`](docs/relay-protocol.md); both sides import this module instead of
|
|
185
|
+
keeping a copy, and a protocol change edits the spec and the module in one PR. The spec's
|
|
186
|
+
JSON examples ship with it (`visionai_sdk_python.relay.examples`: `example(name)` /
|
|
187
|
+
`example_text(name)`), so every implementation tests against the same frames. The decisions
|
|
188
|
+
behind the protocol are platform-service's `docs/adr/0001-relay-protocol.md`. It is internal
|
|
189
|
+
to the platform and not a public API.
|
|
185
190
|
|
|
186
191
|
## VLM Inference
|
|
187
192
|
|
|
@@ -338,6 +343,25 @@ except VisionaiSDKError as e:
|
|
|
338
343
|
- `NetworkError`: Connection/timeout errors
|
|
339
344
|
- `JwksDiscoveryError`: OIDC discovery failures
|
|
340
345
|
|
|
346
|
+
Every HTTP error (`ClientError`, `ServerError` and their subclasses) carries
|
|
347
|
+
`status_code`, `code` and `body`; `body` is the whole JSON body. `code` is set for two
|
|
348
|
+
bodies only: a VS chat result (its `error`) and the relay's own errors (their `code`).
|
|
349
|
+
It is `None` for every other body, including the ones that list their errors under
|
|
350
|
+
`errors` (VS's `invalid_credentials` or `workspace_mismatch`, Platform's errors): read
|
|
351
|
+
`body` there. Where `code` is set, branch on it, not on the message text:
|
|
352
|
+
|
|
353
|
+
```python
|
|
354
|
+
try:
|
|
355
|
+
result = client.vlm.chat(request)
|
|
356
|
+
except ClientError as e:
|
|
357
|
+
# [400 Bad Request] postprocess_error (message='Yes')
|
|
358
|
+
if e.code == "postprocess_error":
|
|
359
|
+
raw_output = e.body["message"]
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
VS answers a failed chat with 200 and `status: "failed"` (read `result.error`), except
|
|
363
|
+
`postprocess_error` (400) and a timeout (408), which raise `ClientError`.
|
|
364
|
+
|
|
341
365
|
## Context Manager Usage
|
|
342
366
|
|
|
343
367
|
Recommended for automatic resource cleanup:
|
|
@@ -422,7 +446,10 @@ instrumentation.instrument(
|
|
|
422
446
|
```
|
|
423
447
|
|
|
424
448
|
This wraps a request-dispatch method of `requests.Session`, `httpx.Client`/
|
|
425
|
-
`AsyncClient
|
|
449
|
+
`AsyncClient`, `httpx2.Client`/`AsyncClient` (the httpx fork Starlette prefers
|
|
450
|
+
from 1.2.0 on, see
|
|
451
|
+
[Kludex/starlette#3291](https://github.com/Kludex/starlette/pull/3291)) and
|
|
452
|
+
`aiohttp.ClientSession` in place, so it also covers calls made
|
|
426
453
|
by third-party packages you cannot edit, module-level one-shots like
|
|
427
454
|
`requests.get()`, and requests sent through a client instance that already
|
|
428
455
|
existed before `instrument()` ran (see "Call it any time" below).
|
|
@@ -434,6 +461,7 @@ it patches are separately optional, and whichever is not installed is skipped:
|
|
|
434
461
|
pip install visionai-sdk-python[instrumentation]
|
|
435
462
|
pip install visionai-sdk-python[requests] # httpx is already a core dependency
|
|
436
463
|
pip install visionai-sdk-python[aiohttp]
|
|
464
|
+
pip install visionai-sdk-python[httpx2]
|
|
437
465
|
```
|
|
438
466
|
|
|
439
467
|
### Destination scoping
|
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: visionai-sdk-python
|
|
3
|
-
Version: 2.2.0.dev6
|
|
4
|
-
Summary: VisionAI SDK for Python
|
|
5
|
-
Author-email: Tony Yang <tonyyang@linkervision.com>
|
|
6
|
-
Requires-Python: >=3.11
|
|
7
|
-
Description-Content-Type: text/markdown
|
|
8
|
-
Requires-Dist: httpx<1,>=0.28.1
|
|
9
|
-
Requires-Dist: pydantic>=2.12.5
|
|
10
|
-
Requires-Dist: cryptography>=46.0.5
|
|
11
|
-
Requires-Dist: PyJWT[cryptography]>=2.8.0
|
|
12
|
-
Provides-Extra: requests
|
|
13
|
-
Requires-Dist: requests<3,>=2.31; extra == "requests"
|
|
14
|
-
Provides-Extra: aiohttp
|
|
15
|
-
Requires-Dist: aiohttp<4,>=3.9; extra == "aiohttp"
|
|
16
|
-
Provides-Extra: instrumentation
|
|
17
|
-
Requires-Dist: wrapt<3,>=1.16; extra == "instrumentation"
|
|
18
|
-
|
|
19
1
|
# VisionAI SDK for Python
|
|
20
2
|
|
|
21
3
|
Python client library for VisionAI authentication and Vision Language Model (VLM) inference services.
|
|
@@ -43,6 +25,7 @@ Attributing direct `requests`/`aiohttp` calls, or using `instrumentation.instrum
|
|
|
43
25
|
```bash
|
|
44
26
|
pip install visionai-sdk-python[requests]
|
|
45
27
|
pip install visionai-sdk-python[aiohttp]
|
|
28
|
+
pip install visionai-sdk-python[httpx2]
|
|
46
29
|
pip install visionai-sdk-python[instrumentation] # needed for instrument() itself
|
|
47
30
|
```
|
|
48
31
|
|
|
@@ -177,11 +160,13 @@ authentication wins. `vlm_url` is optional for clients that only authenticate.
|
|
|
177
160
|
`visionai_sdk_python.relay` holds the wire format between platform-relay and
|
|
178
161
|
platform-edge-agent: the frame models, the constants and `parse_agent_frame()` /
|
|
179
162
|
`parse_relay_frame()` (text frames only: `bytes` or `None` is an `InvalidFrame`) /
|
|
180
|
-
`dump_frame()` (which refuses a frame over `MAX_FRAME_BYTES`). The spec is
|
|
181
|
-
`docs/
|
|
182
|
-
copy
|
|
183
|
-
|
|
184
|
-
|
|
163
|
+
`dump_frame()` (which refuses a frame over `MAX_FRAME_BYTES`). The spec is
|
|
164
|
+
[`docs/relay-protocol.md`](docs/relay-protocol.md); both sides import this module instead of
|
|
165
|
+
keeping a copy, and a protocol change edits the spec and the module in one PR. The spec's
|
|
166
|
+
JSON examples ship with it (`visionai_sdk_python.relay.examples`: `example(name)` /
|
|
167
|
+
`example_text(name)`), so every implementation tests against the same frames. The decisions
|
|
168
|
+
behind the protocol are platform-service's `docs/adr/0001-relay-protocol.md`. It is internal
|
|
169
|
+
to the platform and not a public API.
|
|
185
170
|
|
|
186
171
|
## VLM Inference
|
|
187
172
|
|
|
@@ -338,6 +323,25 @@ except VisionaiSDKError as e:
|
|
|
338
323
|
- `NetworkError`: Connection/timeout errors
|
|
339
324
|
- `JwksDiscoveryError`: OIDC discovery failures
|
|
340
325
|
|
|
326
|
+
Every HTTP error (`ClientError`, `ServerError` and their subclasses) carries
|
|
327
|
+
`status_code`, `code` and `body`; `body` is the whole JSON body. `code` is set for two
|
|
328
|
+
bodies only: a VS chat result (its `error`) and the relay's own errors (their `code`).
|
|
329
|
+
It is `None` for every other body, including the ones that list their errors under
|
|
330
|
+
`errors` (VS's `invalid_credentials` or `workspace_mismatch`, Platform's errors): read
|
|
331
|
+
`body` there. Where `code` is set, branch on it, not on the message text:
|
|
332
|
+
|
|
333
|
+
```python
|
|
334
|
+
try:
|
|
335
|
+
result = client.vlm.chat(request)
|
|
336
|
+
except ClientError as e:
|
|
337
|
+
# [400 Bad Request] postprocess_error (message='Yes')
|
|
338
|
+
if e.code == "postprocess_error":
|
|
339
|
+
raw_output = e.body["message"]
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
VS answers a failed chat with 200 and `status: "failed"` (read `result.error`), except
|
|
343
|
+
`postprocess_error` (400) and a timeout (408), which raise `ClientError`.
|
|
344
|
+
|
|
341
345
|
## Context Manager Usage
|
|
342
346
|
|
|
343
347
|
Recommended for automatic resource cleanup:
|
|
@@ -422,7 +426,10 @@ instrumentation.instrument(
|
|
|
422
426
|
```
|
|
423
427
|
|
|
424
428
|
This wraps a request-dispatch method of `requests.Session`, `httpx.Client`/
|
|
425
|
-
`AsyncClient
|
|
429
|
+
`AsyncClient`, `httpx2.Client`/`AsyncClient` (the httpx fork Starlette prefers
|
|
430
|
+
from 1.2.0 on, see
|
|
431
|
+
[Kludex/starlette#3291](https://github.com/Kludex/starlette/pull/3291)) and
|
|
432
|
+
`aiohttp.ClientSession` in place, so it also covers calls made
|
|
426
433
|
by third-party packages you cannot edit, module-level one-shots like
|
|
427
434
|
`requests.get()`, and requests sent through a client instance that already
|
|
428
435
|
existed before `instrument()` ran (see "Call it any time" below).
|
|
@@ -434,6 +441,7 @@ it patches are separately optional, and whichever is not installed is skipped:
|
|
|
434
441
|
pip install visionai-sdk-python[instrumentation]
|
|
435
442
|
pip install visionai-sdk-python[requests] # httpx is already a core dependency
|
|
436
443
|
pip install visionai-sdk-python[aiohttp]
|
|
444
|
+
pip install visionai-sdk-python[httpx2]
|
|
437
445
|
```
|
|
438
446
|
|
|
439
447
|
### Destination scoping
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# platform-relay ↔ platform-edge-agent wire format, `platform-relay.v1`
|
|
2
|
+
|
|
3
|
+
What goes over the WebSocket between platform-relay (platform-service) and platform-edge-agent
|
|
4
|
+
(visionai-vlm-scheduling-service). `visionai_sdk_python.relay` implements it, the models, the
|
|
5
|
+
constants and the parsers, and both sides import that module instead of keeping a copy. The JSON
|
|
6
|
+
examples below ship as `visionai_sdk_python.relay.examples` and are the fixtures of the SDK's, the
|
|
7
|
+
relay's and the agent's tests. A protocol change edits this file and the module in one PR; the two
|
|
8
|
+
consumers then bump the SDK.
|
|
9
|
+
|
|
10
|
+
Why the protocol looks like this is platform-service's `docs/adr/0001-relay-protocol.md`. How the
|
|
11
|
+
relay implements it (the Platform check at `register`, routing, its HTTP API and status mapping) is
|
|
12
|
+
`relay/README.md` in that repository; the agent's side is in visionai-vlm-scheduling-service.
|
|
13
|
+
|
|
14
|
+
## 1. Connection
|
|
15
|
+
|
|
16
|
+
The agent connects to `wss://<platform_domain>/relay/ws`. The HTTP upgrade carries the agent's
|
|
17
|
+
identity and the protocol version:
|
|
18
|
+
|
|
19
|
+
```http
|
|
20
|
+
GET /relay/ws HTTP/1.1
|
|
21
|
+
Host: <platform_domain>
|
|
22
|
+
Upgrade: websocket
|
|
23
|
+
Connection: Upgrade
|
|
24
|
+
Sec-WebSocket-Version: 13
|
|
25
|
+
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
|
|
26
|
+
Sec-WebSocket-Protocol: platform-relay.v1
|
|
27
|
+
Authorization: Bearer eyJhbGciOi…
|
|
28
|
+
|
|
29
|
+
HTTP/1.1 101 Switching Protocols
|
|
30
|
+
Upgrade: websocket
|
|
31
|
+
Connection: Upgrade
|
|
32
|
+
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
|
33
|
+
Sec-WebSocket-Protocol: platform-relay.v1
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- `Authorization` is a platform M2M token (client credentials). A refused upgrade is an HTTP
|
|
37
|
+
status, not a close code, with a JSON body `{"detail": "<why>"}`: 400 when the relay knows none
|
|
38
|
+
of the offered subprotocols; 401 when Platform does not accept the token (the agent fetches a new
|
|
39
|
+
token, once, and reconnects); 403 when the token is valid but the relay does not serve that
|
|
40
|
+
identity, a user token or a client outside its allowlist (a new token would not help: the agent
|
|
41
|
+
keeps it and retries after its backoff); 503 when the relay got no answer from Platform, a 429 of
|
|
42
|
+
its auth-failure throttle included (the agent retries with the same token).
|
|
43
|
+
- `Sec-WebSocket-Protocol` carries the protocol version (§6).
|
|
44
|
+
- After `101` the connection is the identity. Messages carry no agent or tenant fields.
|
|
45
|
+
|
|
46
|
+
Transport:
|
|
47
|
+
|
|
48
|
+
- Text frames only, one JSON object per frame, UTF-8. A binary frame is answered with
|
|
49
|
+
`error invalid_message`.
|
|
50
|
+
- Maximum frame size 16 MiB in both directions; both sides check the encoded frame before sending
|
|
51
|
+
(`dump_frame()` refuses a larger one).
|
|
52
|
+
- WebSocket ping every 20 s, pong timeout 60 s, on both sides. Large frames on slow links delay
|
|
53
|
+
pongs, so the timeout is not the library default.
|
|
54
|
+
|
|
55
|
+
## 2. Messages
|
|
56
|
+
|
|
57
|
+
Six message types, told apart by `type`. Optional fields may be omitted or `null`. Receivers ignore
|
|
58
|
+
unknown fields.
|
|
59
|
+
|
|
60
|
+
| `type` | Sender | Fields |
|
|
61
|
+
|---|---|---|
|
|
62
|
+
| `register` | agent | `agent_id`, `cluster_id`, `workspaces` |
|
|
63
|
+
| `registered` | relay | `accepted`, `rejected` |
|
|
64
|
+
| `heartbeat` | agent | none |
|
|
65
|
+
| `request` | relay | `request_id`, `workspace_id`, `method`, `path`, `headers`, `body`, `timeout_s` |
|
|
66
|
+
| `response` | agent | `request_id`, `status`, `headers`, `body` |
|
|
67
|
+
| `error` | either | `request_id` (optional), `code`, `message` |
|
|
68
|
+
|
|
69
|
+
| Field | Type | Meaning |
|
|
70
|
+
|---|---|---|
|
|
71
|
+
| `agent_id` | string | The agent's pod name. With `cluster_id` it identifies a reconnecting agent (§3). |
|
|
72
|
+
| `cluster_id` | string | The agent's site, from its own configuration: the value vlm-mgmt reports as `VLM_CLUSTER_NAME` and Platform stores in `Node.cluster`. Required. |
|
|
73
|
+
| `workspaces` | array of object | `{"id", "models"}` per workspace the agent serves. `id` is the workspace's Platform id, a lowercase hyphenated UUID. Complete list; replaces the previous one. |
|
|
74
|
+
| `models` | array of object | `{"name", "version", "replicas"}` per model the workspace's VS lists as available (§3). Empty when it lists none. |
|
|
75
|
+
| `replicas` | integer | Required; a `register` without it is invalid (§5). Available nodes listing that model and version (§3); weights the relay's routing. |
|
|
76
|
+
| `accepted` | array of string | Workspace ids from `workspaces` the relay registered. |
|
|
77
|
+
| `rejected` | array of object | `{"id", "reason"}` per refused workspace: the workspace id and a reason from §5. |
|
|
78
|
+
| `request_id` | string | UUID v4 minted by the relay. `response` and `error` echo it. |
|
|
79
|
+
| `workspace_id` | string | Target workspace of a `request`. |
|
|
80
|
+
| `method`, `path` | string | The caller's HTTP method and path below `/<workspace_id>`, query string included. |
|
|
81
|
+
| `headers` | object | `request`: `Authorization`, `X-Request-Source`, `Content-Type` only. `response`: `Content-Type` only. Names are case-insensitive. |
|
|
82
|
+
| `body` | object, string or null | The HTTP body: JSON as an object, anything else as a string, `null` when there is none. |
|
|
83
|
+
| `timeout_s` | integer | Seconds the agent waits for VS. |
|
|
84
|
+
| `status` | integer | VS's HTTP status code, unchanged. |
|
|
85
|
+
| `code`, `message` | string | Error code (§5) and a human-readable detail, at most 200 characters. |
|
|
86
|
+
|
|
87
|
+
Examples. Image data and tokens are truncated; the `request` body is what Dataverse sends today.
|
|
88
|
+
|
|
89
|
+
`register`, agent → relay, first frame after `101`:
|
|
90
|
+
|
|
91
|
+
```json
|
|
92
|
+
{"type": "register",
|
|
93
|
+
"agent_id": "platform-edge-agent-7d9f4b6c8-xk2pq",
|
|
94
|
+
"cluster_id": "aws-offline-edge",
|
|
95
|
+
"workspaces": [{"id": "31a6638a-5c2e-4f1b-9a7d-0e3c6b8d2f41",
|
|
96
|
+
"models": [{"name": "cr2-8b", "version": "4.5.0", "replicas": 2},
|
|
97
|
+
{"name": "cr2-8b", "version": "4.6.0", "replicas": 1}]},
|
|
98
|
+
{"id": "7589d018-2b4c-4e6f-8a1d-3c5e7f9b1d23", "models": []},
|
|
99
|
+
{"id": "QA Workspace", "models": []}]}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`registered`, relay → agent. Platform lists no node of `aws-offline-edge` for `7589…`; `QA Workspace`
|
|
103
|
+
is not a UUID:
|
|
104
|
+
|
|
105
|
+
```json
|
|
106
|
+
{"type": "registered",
|
|
107
|
+
"accepted": ["31a6638a-5c2e-4f1b-9a7d-0e3c6b8d2f41"],
|
|
108
|
+
"rejected": [{"id": "7589d018-2b4c-4e6f-8a1d-3c5e7f9b1d23", "reason": "cluster_mismatch"},
|
|
109
|
+
{"id": "QA Workspace", "reason": "invalid_id"}]}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`heartbeat`, agent → relay, every 30 s:
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{"type": "heartbeat"}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
`request`, relay → agent:
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
{"type": "request",
|
|
122
|
+
"request_id": "7f3c2a1e-9d4b-4c8a-b2e6-1f5d3a7c9e0b",
|
|
123
|
+
"workspace_id": "31a6638a-5c2e-4f1b-9a7d-0e3c6b8d2f41",
|
|
124
|
+
"method": "POST",
|
|
125
|
+
"path": "/api/chat",
|
|
126
|
+
"headers": {"Authorization": "Bearer eyJhbGciOi…",
|
|
127
|
+
"X-Request-Source": "dataverse",
|
|
128
|
+
"Content-Type": "application/json"},
|
|
129
|
+
"body": {"img": "data:image/jpeg;base64,/9j/4AAQ…",
|
|
130
|
+
"prompt": "Describe the scene.",
|
|
131
|
+
"temperature": 0.2, "max_tokens": 500, "top_p": 0.7,
|
|
132
|
+
"stream": false, "use_cache": true, "num_beams": 1,
|
|
133
|
+
"model": "cr2-8b", "model_version": null,
|
|
134
|
+
"api_endpoint": null, "hook": null,
|
|
135
|
+
"keep_alive": true, "use_response_postprocess": true,
|
|
136
|
+
"workspace_id": "31a6638a-5c2e-4f1b-9a7d-0e3c6b8d2f41"},
|
|
137
|
+
"timeout_s": 100}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`response`, agent → relay. The same shape carries VS's 408, 422 and 500 answers:
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
{"type": "response",
|
|
144
|
+
"request_id": "7f3c2a1e-9d4b-4c8a-b2e6-1f5d3a7c9e0b",
|
|
145
|
+
"status": 200,
|
|
146
|
+
"headers": {"Content-Type": "application/json"},
|
|
147
|
+
"body": {"chat_id": "high:1760424000123-3f9a1c7e",
|
|
148
|
+
"status": "completed",
|
|
149
|
+
"message": "{\"description\": \"A forklift in a warehouse aisle.\"}",
|
|
150
|
+
"dispatch": {"model_name": "cr2-8b", "model_version": "4.5.0",
|
|
151
|
+
"api_endpoint": "http://cr2-8b:8000"}}}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`response` with a non-JSON body, for example an unhandled 500 from VS:
|
|
155
|
+
|
|
156
|
+
```json
|
|
157
|
+
{"type": "response",
|
|
158
|
+
"request_id": "7f3c2a1e-9d4b-4c8a-b2e6-1f5d3a7c9e0b",
|
|
159
|
+
"status": 500,
|
|
160
|
+
"headers": {"Content-Type": "text/plain; charset=utf-8"},
|
|
161
|
+
"body": "Internal Server Error"}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
`error` about one request, agent → relay:
|
|
165
|
+
|
|
166
|
+
```json
|
|
167
|
+
{"type": "error",
|
|
168
|
+
"request_id": "7f3c2a1e-9d4b-4c8a-b2e6-1f5d3a7c9e0b",
|
|
169
|
+
"code": "upstream_unreachable",
|
|
170
|
+
"message": "connect to vlm-scheduling-service-backend.31a6638a-5c2e-4f1b-9a7d-0e3c6b8d2f41.svc.cluster.local:80 failed"}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
`error` about the connection, relay → agent, no `request_id`:
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{"type": "error",
|
|
177
|
+
"code": "invalid_message",
|
|
178
|
+
"message": "unknown type 'ping'"}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## 3. Registration and liveness
|
|
182
|
+
|
|
183
|
+
- The first frame after `101` MUST be `register`, within 10 s. Otherwise the relay closes with 1008.
|
|
184
|
+
- `workspaces` is every workspace the agent serves; an empty list is valid. `models` is what that
|
|
185
|
+
workspace's VS reports on `GET /mgmt/nodes`: the `model_name` / `model_version` pairs of nodes
|
|
186
|
+
with `status: available`, and in `replicas` how many such nodes list each pair.
|
|
187
|
+
- The relay answers every `register` with `registered`, checking each workspace on its own (reasons
|
|
188
|
+
in §5). The connection stays open whatever the outcome.
|
|
189
|
+
- The agent re-sends `register` when its list changes, and on its next poll after a
|
|
190
|
+
`platform_unavailable` rejection. The new list replaces the previous one; an agent shutting down
|
|
191
|
+
may send an empty list first so no new `request` reaches it while it drains.
|
|
192
|
+
- A new connection with the same `cluster_id` and `agent_id` replaces the old one: the relay closes
|
|
193
|
+
the old connection with 4000.
|
|
194
|
+
- The agent sends `heartbeat` every 30 s; the relay does not reply. No frame for 90 s: the relay
|
|
195
|
+
closes with 4001.
|
|
196
|
+
- Shutdown: on SIGTERM the agent finishes in-flight requests, 10 s at most, then closes with 1001.
|
|
197
|
+
The relay fails the requests still open on a connection that closes, whatever the code.
|
|
198
|
+
|
|
199
|
+
## 4. Request and response
|
|
200
|
+
|
|
201
|
+
The relay wraps the caller's HTTP request; the agent replays it against VS. The shape is generic
|
|
202
|
+
HTTP so further paths can be added without a protocol change.
|
|
203
|
+
|
|
204
|
+
- Several requests may be in flight on one connection. Responses may arrive in any order; the
|
|
205
|
+
`request_id` pairs them.
|
|
206
|
+
- The agent forwards the three allowed headers unchanged and adds `X-Request-ID: <request_id>`.
|
|
207
|
+
Nothing else is added.
|
|
208
|
+
- `timeout_s` is the agent's HTTP timeout for the call. Chat uses 100.
|
|
209
|
+
- Paths allowed in v1: `POST /api/chat`. Anything else: `error path_not_allowed`.
|
|
210
|
+
- The agent answers 422 without calling VS when the body has `stream: true`: a response is one
|
|
211
|
+
frame, so SSE cannot be carried.
|
|
212
|
+
- The agent always answers: VS unreachable → `error upstream_unreachable`; no answer within
|
|
213
|
+
`timeout_s` → `error upstream_timeout`; a `response` that would not fit a frame →
|
|
214
|
+
`error upstream_too_large`; any other body → `response` with the body as a string.
|
|
215
|
+
- Neither side logs `Authorization` or `body.img` (linker-convention `conventions/logging.md` §6).
|
|
216
|
+
|
|
217
|
+
One chat, end to end:
|
|
218
|
+
|
|
219
|
+
```text
|
|
220
|
+
agent → relay HTTP upgrade, Authorization + Sec-WebSocket-Protocol: platform-relay.v1
|
|
221
|
+
relay → agent 101 Switching Protocols
|
|
222
|
+
agent → relay {"type": "register", "agent_id": "…", "cluster_id": "aws-offline-edge", "workspaces": [{"id": "31a6…", "models": [{"name": "cr2-8b", "version": "4.5.0", "replicas": 2}]}, {"id": "7589…", "models": []}]}
|
|
223
|
+
relay → agent {"type": "registered", "accepted": ["31a6…", "7589…"], "rejected": []}
|
|
224
|
+
… Dataverse: POST http://platform-relay/31a6…/api/chat
|
|
225
|
+
relay → agent {"type": "request", "request_id": "7f3c…", "workspace_id": "31a6…", "method": "POST", "path": "/api/chat", …}
|
|
226
|
+
… agent: POST http://vlm-scheduling-service-backend.31a6….svc.cluster.local/api/chat
|
|
227
|
+
agent → relay {"type": "response", "request_id": "7f3c…", "status": 200, …}
|
|
228
|
+
… relay: 200 with VS's body back to Dataverse
|
|
229
|
+
agent → relay {"type": "heartbeat"} every 30 s throughout
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## 5. Errors and close codes
|
|
233
|
+
|
|
234
|
+
An `error` that carries a `request_id` ends that request at once. An `error` without
|
|
235
|
+
`request_id` is logged and the connection stays open.
|
|
236
|
+
|
|
237
|
+
| `code` | Sender | When |
|
|
238
|
+
|---|---|---|
|
|
239
|
+
| `invalid_message` | either | Frame is binary, not JSON, has no `type`, an unknown `type`, or fails validation |
|
|
240
|
+
| `unknown_workspace` | agent | `workspace_id` is not in the agent's current list |
|
|
241
|
+
| `path_not_allowed` | agent | `method` + `path` outside §4 |
|
|
242
|
+
| `upstream_unreachable` | agent | Connection to VS failed, or VS's answer could not be read |
|
|
243
|
+
| `upstream_timeout` | agent | VS did not answer within `timeout_s` |
|
|
244
|
+
| `upstream_too_large` | agent | VS answered, but the `response` frame would be over 16 MiB |
|
|
245
|
+
| any other value | either | Added by a later version |
|
|
246
|
+
|
|
247
|
+
`reason` values in `registered.rejected`:
|
|
248
|
+
|
|
249
|
+
| `reason` | When |
|
|
250
|
+
|---|---|
|
|
251
|
+
| `invalid_id` | `id` is not a lowercase hyphenated UUID, or appears twice |
|
|
252
|
+
| `cluster_mismatch` | Platform lists no node of `cluster_id` for that workspace |
|
|
253
|
+
| `platform_unavailable` | The relay could not ask Platform; the agent's next `register` retries |
|
|
254
|
+
|
|
255
|
+
Close codes:
|
|
256
|
+
|
|
257
|
+
| Code | Sender | When |
|
|
258
|
+
|---|---|---|
|
|
259
|
+
| 1000 | either | Normal close |
|
|
260
|
+
| 1001 | either | Shutting down. Agents reconnect after a relay 1001; the relay drops the entry after an agent 1001 |
|
|
261
|
+
| 1008 | relay | Protocol violation: no `register` within 10 s, or a first frame that is not `register` |
|
|
262
|
+
| 1009 | either | Frame over 16 MiB. Both sides enforce the limit before sending, so this is a bug |
|
|
263
|
+
| 4000 | relay | Replaced by a new connection with the same `cluster_id` and `agent_id` |
|
|
264
|
+
| 4001 | relay | No frame for 90 s |
|
|
265
|
+
|
|
266
|
+
HTTP 400, 401 and 503 at the upgrade (§1) are not close codes; the connection never opens.
|
|
267
|
+
|
|
268
|
+
## 6. Versioning
|
|
269
|
+
|
|
270
|
+
- The version is the WebSocket subprotocol name, `platform-relay.v1`. Messages carry no version field.
|
|
271
|
+
- Adding an optional field is not a new version. Receivers ignore unknown fields.
|
|
272
|
+
- Changing a field's meaning, removing one, or changing the message flow is `platform-relay.v2`.
|
|
273
|
+
- An agent may offer several versions, newest first: `platform-relay.v2, platform-relay.v1`. The
|
|
274
|
+
relay answers with the one it picked. An agent refused at the upgrade backs off and retries; it
|
|
275
|
+
does not fall back to an unversioned connection.
|
|
276
|
+
|
|
277
|
+
## 7. Limits
|
|
278
|
+
|
|
279
|
+
- 16 MiB per frame, both directions (`MAX_FRAME_BYTES`); the relay refuses a request body over
|
|
280
|
+
15 MiB before anything is sent (`MAX_BODY_BYTES`).
|
|
281
|
+
- `error.message` is cut at 200 characters (`MAX_MESSAGE_CHARS`): it quotes parts of the peer's
|
|
282
|
+
frame, and the error must fit a frame itself.
|
|
283
|
+
- Timers: `register` within 10 s of the upgrade, `heartbeat` every 30 s, stale after 90 s; the
|
|
284
|
+
agent waits `timeout_s` (100) for VS and the relay 110 s for the agent.
|
|
@@ -24,6 +24,7 @@ dependencies = [
|
|
|
24
24
|
[project.optional-dependencies]
|
|
25
25
|
requests = ["requests>=2.31,<3"]
|
|
26
26
|
aiohttp = ["aiohttp>=3.9,<4"]
|
|
27
|
+
httpx2 = ["httpx2>=2.13,<3"]
|
|
27
28
|
instrumentation = ["wrapt>=1.16,<3"]
|
|
28
29
|
|
|
29
30
|
[build-system]
|
|
@@ -57,6 +58,7 @@ dev = [
|
|
|
57
58
|
"ruff>=0.15.11",
|
|
58
59
|
"requests>=2.31,<3",
|
|
59
60
|
"aiohttp>=3.9,<4",
|
|
61
|
+
"httpx2>=2.13,<3",
|
|
60
62
|
"wrapt>=1.16,<3",
|
|
61
63
|
]
|
|
62
64
|
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
from http import HTTPStatus
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
import httpx
|
|
5
|
+
|
|
6
|
+
# The longest `message` text an exception quotes; `APIError.body` keeps all of it.
|
|
7
|
+
_MESSAGE_LIMIT = 200
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class VisionaiSDKError(Exception):
|
|
11
|
+
"""Base exception for all SDK errors."""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class APIError(VisionaiSDKError):
|
|
15
|
+
"""Base exception for all HTTP API errors.
|
|
16
|
+
|
|
17
|
+
Attributes:
|
|
18
|
+
status_code: The response's HTTP status.
|
|
19
|
+
code: The error code in the body (e.g. ``postprocess_error``), None without one.
|
|
20
|
+
body: The response's JSON object, None when it is not one.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
status_code: int,
|
|
26
|
+
message: str,
|
|
27
|
+
*,
|
|
28
|
+
code: str | None = None,
|
|
29
|
+
body: dict[str, Any] | None = None,
|
|
30
|
+
) -> None:
|
|
31
|
+
self.status_code = status_code
|
|
32
|
+
self.code = code
|
|
33
|
+
self.body = body
|
|
34
|
+
super().__init__(f"[{_status_text(status_code)}] {message}")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ClientError(APIError):
|
|
38
|
+
"""4xx - Client-side error."""
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AuthenticationError(ClientError):
|
|
42
|
+
"""401 - Invalid credentials/password."""
|
|
43
|
+
|
|
44
|
+
def __init__(self, message: str, **kwargs: Any) -> None:
|
|
45
|
+
super().__init__(401, message, **kwargs)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class PermissionDeniedError(ClientError):
|
|
49
|
+
"""403 - Insufficient permissions."""
|
|
50
|
+
|
|
51
|
+
def __init__(self, message: str, **kwargs: Any) -> None:
|
|
52
|
+
super().__init__(403, message, **kwargs)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class RateLimitedError(ClientError):
|
|
56
|
+
"""429 - Too many requests; on client-token, Platform is rate limiting mints."""
|
|
57
|
+
|
|
58
|
+
def __init__(self, message: str, **kwargs: Any) -> None:
|
|
59
|
+
super().__init__(429, message, **kwargs)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class ServerError(APIError):
|
|
63
|
+
"""5xx - Server-side failure, consider retry."""
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class NetworkError(VisionaiSDKError):
|
|
67
|
+
"""Connection or timeout failure."""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class JwksDiscoveryError(VisionaiSDKError):
|
|
71
|
+
"""Failed to fetch or parse the OIDC discovery document or JWKS endpoint."""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def from_transport_error(error: httpx.RequestError) -> VisionaiSDKError:
|
|
75
|
+
"""The SDK exception for an httpx request that never got a response."""
|
|
76
|
+
if isinstance(error, httpx.TimeoutException):
|
|
77
|
+
return NetworkError("Request timed out")
|
|
78
|
+
if isinstance(error, httpx.NetworkError):
|
|
79
|
+
return NetworkError(f"Network error: {error}")
|
|
80
|
+
return VisionaiSDKError(f"Request failed: {error}")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def raise_for_status(response: httpx.Response) -> httpx.Response:
|
|
84
|
+
"""Raise SDK-specific exceptions for non-2xx responses.
|
|
85
|
+
|
|
86
|
+
Raises:
|
|
87
|
+
AuthenticationError: If the server returns 401
|
|
88
|
+
PermissionDeniedError: If the server returns 403
|
|
89
|
+
ClientError: If the server returns any other 4xx
|
|
90
|
+
ServerError: If the server returns 5xx
|
|
91
|
+
APIError: If the server returns any other non-2xx status
|
|
92
|
+
"""
|
|
93
|
+
try:
|
|
94
|
+
response.raise_for_status()
|
|
95
|
+
except httpx.HTTPStatusError as e:
|
|
96
|
+
body = None
|
|
97
|
+
if e.response.content:
|
|
98
|
+
try:
|
|
99
|
+
body = e.response.json()
|
|
100
|
+
except Exception:
|
|
101
|
+
pass
|
|
102
|
+
code = None
|
|
103
|
+
if isinstance(body, dict):
|
|
104
|
+
detail: str = body.get("detail") or body.get("message") or str(e)
|
|
105
|
+
# A VS chat result puts the error code in `error`, the relay in `code`.
|
|
106
|
+
# An `errors` list (VS's exceptions, Platform) is not read.
|
|
107
|
+
# TODO: read one shape once the services agree on an error body.
|
|
108
|
+
code = next(
|
|
109
|
+
(v for k in ("error", "code") if isinstance(v := body.get(k), str)),
|
|
110
|
+
None,
|
|
111
|
+
)
|
|
112
|
+
if code:
|
|
113
|
+
detail = _coded_detail(code, body)
|
|
114
|
+
else:
|
|
115
|
+
body = None
|
|
116
|
+
detail = str(e)
|
|
117
|
+
status = e.response.status_code
|
|
118
|
+
if status == 401:
|
|
119
|
+
raise AuthenticationError(detail, code=code, body=body) from e
|
|
120
|
+
if status == 403:
|
|
121
|
+
raise PermissionDeniedError(detail, code=code, body=body) from e
|
|
122
|
+
if status == 429:
|
|
123
|
+
raise RateLimitedError(detail, code=code, body=body) from e
|
|
124
|
+
if 400 <= status < 500:
|
|
125
|
+
raise ClientError(status, detail, code=code, body=body) from e
|
|
126
|
+
if 500 <= status < 600:
|
|
127
|
+
raise ServerError(status, detail, code=code, body=body) from e
|
|
128
|
+
raise APIError(status, detail, code=code, body=body) from e
|
|
129
|
+
return response
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _status_text(status_code: int) -> str:
|
|
133
|
+
"""The status with its standard phrase (400 Bad Request); the number alone without one."""
|
|
134
|
+
try:
|
|
135
|
+
return f"{status_code} {HTTPStatus(status_code).phrase}"
|
|
136
|
+
except ValueError:
|
|
137
|
+
return str(status_code)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _coded_detail(code: str, body: dict[str, Any]) -> str:
|
|
141
|
+
"""The message of an error with a code: the code, then what the body says about it."""
|
|
142
|
+
if body.get("detail"):
|
|
143
|
+
return f"{code}: {body['detail']}"
|
|
144
|
+
if body.get("message"):
|
|
145
|
+
# VS's `message` is the model's output, not a description: quote it, cut short.
|
|
146
|
+
text = str(body["message"])
|
|
147
|
+
if len(text) > _MESSAGE_LIMIT:
|
|
148
|
+
text = f"{text[:_MESSAGE_LIMIT]}..."
|
|
149
|
+
return f"{code} (message={text!r})"
|
|
150
|
+
return code
|