reef-client 0.2.0__tar.gz → 0.2.1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reef-client
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Stdlib-only client SDK for the Reef protocol
5
5
  Author: Human-Agent-Society
6
6
  License-Expression: Apache-2.0
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
6
6
 
7
7
  [project]
8
8
  name = "reef-client"
9
- version = "0.2.0"
9
+ version = "0.2.1"
10
10
  description = "Stdlib-only client SDK for the Reef protocol"
11
11
  readme = "README.md"
12
12
  license = "Apache-2.0"
@@ -172,6 +172,8 @@ class ServeConfig:
172
172
  def __post_init__(self) -> None:
173
173
  if not self.upstream:
174
174
  raise ValueError("upstream must be non-empty")
175
+ if urlsplit(self.upstream).scheme not in ("http", "https"):
176
+ raise ValueError(f"upstream must be an http or https URL, got {self.upstream!r}")
175
177
 
176
178
 
177
179
  def build_handler(config: ServeConfig, store: CaptureStore) -> type[BaseHTTPRequestHandler]:
@@ -368,7 +370,14 @@ def build_handler(config: ServeConfig, store: CaptureStore) -> type[BaseHTTPRequ
368
370
  if client_stream:
369
371
  self._begin_sse()
370
372
 
371
- conn = http.client.HTTPConnection(parsed.hostname, parsed.port, timeout=config.timeout_s)
373
+ # The upstream's own scheme: an https upstream (the API platform) over a plain connection is
374
+ # answered with a redirect and an empty body, which the agent sees as an empty completion.
375
+ if parsed.scheme == "https":
376
+ conn: http.client.HTTPConnection = http.client.HTTPSConnection(
377
+ parsed.hostname, parsed.port, timeout=config.timeout_s
378
+ )
379
+ else:
380
+ conn = http.client.HTTPConnection(parsed.hostname, parsed.port, timeout=config.timeout_s)
372
381
  started = time.monotonic()
373
382
  try:
374
383
  conn.request(self.command, forward_path, body=forward_body, headers=headers)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reef-client
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Stdlib-only client SDK for the Reef protocol
5
5
  Author: Human-Agent-Society
6
6
  License-Expression: Apache-2.0
@@ -11,5 +11,6 @@ reef_client.egg-info/SOURCES.txt
11
11
  reef_client.egg-info/dependency_links.txt
12
12
  reef_client.egg-info/top_level.txt
13
13
  tests/test_client_disconnects.py
14
+ tests/test_https_upstream.py
14
15
  tests/test_sse_receipt.py
15
16
  tests/test_streaming_head.py
@@ -0,0 +1,91 @@
1
+ """An https upstream is reached over TLS, not over a plain connection to its host.
2
+
3
+ The API platform serves reef behind https. A plain connection to that host is
4
+ answered with a redirect to https and no body, which serve relayed as an empty
5
+ completion; the agent then reported an empty upstream response for every call.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import http.client
11
+ import json
12
+ import socket
13
+ import threading
14
+ from http.server import ThreadingHTTPServer
15
+
16
+ import pytest
17
+
18
+ from reef_client.serve import CaptureStore, ServeConfig, build_handler
19
+
20
+
21
+ def _serve(upstream_url: str) -> ThreadingHTTPServer:
22
+ proxy = ThreadingHTTPServer(("127.0.0.1", 0), build_handler(ServeConfig(upstream=upstream_url, listen_port=0), CaptureStore()))
23
+ threading.Thread(target=proxy.serve_forever, daemon=True).start()
24
+ return proxy
25
+
26
+
27
+ def _post(port: int) -> bytes:
28
+ body = json.dumps({"model": "m", "messages": [{"role": "user", "content": "hi"}]})
29
+ raw = socket.create_connection(("127.0.0.1", port))
30
+ raw.sendall(
31
+ f"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\n"
32
+ f"Content-Length: {len(body)}\r\nConnection: close\r\n\r\n".encode()
33
+ + body.encode()
34
+ )
35
+ raw.settimeout(10)
36
+ chunks = []
37
+ while True:
38
+ try:
39
+ chunk = raw.recv(65536)
40
+ except socket.timeout:
41
+ break
42
+ if not chunk:
43
+ break
44
+ chunks.append(chunk)
45
+ raw.close()
46
+ return b"".join(chunks)
47
+
48
+
49
+ def test_an_https_upstream_opens_a_tls_connection(monkeypatch):
50
+ """The connection class follows the upstream's scheme: https opens HTTPSConnection at the upstream's host."""
51
+ opened: list[tuple[str, tuple]] = []
52
+
53
+ class _Recorder:
54
+ def __init__(self, host, port=None, timeout=None, **_kwargs):
55
+ opened.append(("https", (host, port)))
56
+
57
+ def request(self, *args, **kwargs):
58
+ raise OSError("no network in this test")
59
+
60
+ monkeypatch.setattr(http.client, "HTTPSConnection", _Recorder)
61
+ proxy = _serve("https://api.example.test")
62
+ try:
63
+ answer = _post(proxy.server_address[1])
64
+ finally:
65
+ proxy.shutdown()
66
+ assert opened == [("https", ("api.example.test", None))]
67
+ assert answer.startswith(b"HTTP/1.1 502")
68
+
69
+
70
+ def test_an_http_upstream_keeps_a_plain_connection(monkeypatch):
71
+ opened: list[str] = []
72
+
73
+ class _Recorder:
74
+ def __init__(self, host, port=None, timeout=None, **_kwargs):
75
+ opened.append("http")
76
+
77
+ def request(self, *args, **kwargs):
78
+ raise OSError("no network in this test")
79
+
80
+ monkeypatch.setattr(http.client, "HTTPConnection", _Recorder)
81
+ proxy = _serve("http://127.0.0.1:1")
82
+ try:
83
+ _post(proxy.server_address[1])
84
+ finally:
85
+ proxy.shutdown()
86
+ assert opened == ["http"]
87
+
88
+
89
+ def test_a_config_refuses_an_upstream_without_a_scheme():
90
+ with pytest.raises(ValueError, match="http or https"):
91
+ ServeConfig(upstream="api.example.test", listen_port=0)
File without changes
File without changes
File without changes