Skip to content

Commit f85e25e

Browse files
committed
chore: licenses and pre-commit
1 parent 2464578 commit f85e25e

8 files changed

Lines changed: 99 additions & 33 deletions

File tree

src/rai_core/rai/communication/http/api.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
# limitations under the License.
1414

1515
import asyncio
16+
import logging
1617
import threading
1718
from enum import IntFlag
18-
from typing import Callable, Optional, Any
19-
import json
20-
import logging
19+
from typing import Callable, Optional
2120

2221
import aiohttp
2322
from aiohttp import ClientSession, ClientTimeout, web
@@ -153,4 +152,3 @@ def shutdown(self):
153152
except Exception as e:
154153
logging.warning(f"Background request failed or timed out: {e}")
155154
self.unresolved_futures.clear()
156-

src/rai_core/rai/communication/http/connectors/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import time
16-
from functools import partial
1715
from typing import Any, Callable, Dict, Optional, TypeVar
1816

1917
from aiohttp import web
@@ -76,7 +74,9 @@ def service_call(
7674
payload=message.payload,
7775
headers=message.metadata.get("headers"),
7876
)
79-
return self.T_class(payload=ret, method=message.method, metadata={"return_code": code}) # type: ignore[call-arg]
77+
return self.T_class(
78+
payload=ret, method=message.method, metadata={"return_code": code}
79+
) # type: ignore[call-arg]
8080

8181
def create_service(
8282
self,

src/rai_core/rai/communication/http/connectors/hri_connector.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any, Callable, Optional
1615

1716
from rai.communication import HRIConnector
1817
from rai.communication.http.api import HTTPConnectorMode
@@ -66,12 +65,10 @@ def payload_to_message(data: dict) -> HTTPHRIMessage:
6665
return HTTPHRIMessage(
6766
text=data.get("text", ""),
6867
images=[
69-
HTTPHRIMessage._base64_to_image(img)
70-
for img in data.get("images", [])
68+
HTTPHRIMessage._base64_to_image(img) for img in data.get("images", [])
7169
],
7270
audios=[
73-
HTTPHRIMessage._base64_to_audio(aud)
74-
for aud in data.get("audios", [])
71+
HTTPHRIMessage._base64_to_audio(aud) for aud in data.get("audios", [])
7572
],
7673
message_author=data.get("message_author", "human"),
7774
communication_id=data.get("communication_id"),

src/rai_core/rai/communication/http/messages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Optional, Literal
15+
from typing import Literal, Optional
16+
1617
from rai.communication.base_connector import BaseMessage
1718
from rai.communication.hri_connector import HRIMessage
1819

tests/communication/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2026 Kajetan Rachwał
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2026 Kajetan Rachwał
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

tests/communication/http/test_api.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
# Copyright (C) 2026 Kajetan Rachwał
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import asyncio
2-
import threading
316
import time
4-
import pytest
517
from unittest.mock import AsyncMock, MagicMock, patch
6-
from aiohttp import web
718

19+
import pytest
20+
from aiohttp import web
821
from rai.communication.http.api import HTTPAPI, HTTPAPIError, HTTPConnectorMode
922

10-
1123
# ── Fixtures ──────────────────────────────────────────────────────────────────
1224

25+
1326
@pytest.fixture
1427
def client_api():
1528
api = HTTPAPI(mode=HTTPConnectorMode.client)
@@ -36,6 +49,7 @@ def client_server_api():
3649

3750
# ── HTTPConnectorMode ─────────────────────────────────────────────────────────
3851

52+
3953
class TestHTTPConnectorMode:
4054
def test_client_value(self):
4155
assert HTTPConnectorMode.client == 1
@@ -58,11 +72,15 @@ def test_server_flag_in_client_server(self):
5872
assert HTTPConnectorMode.server & HTTPConnectorMode.client_server
5973

6074
def test_client_flag_not_in_server_only(self):
61-
assert not (HTTPConnectorMode.client & HTTPConnectorMode.server == HTTPConnectorMode.client_server)
75+
assert not (
76+
HTTPConnectorMode.client & HTTPConnectorMode.server
77+
== HTTPConnectorMode.client_server
78+
)
6279

6380

6481
# ── Initialisation ────────────────────────────────────────────────────────────
6582

83+
6684
class TestHTTPAPIInit:
6785
def test_default_attributes(self):
6886
api = HTTPAPI()
@@ -91,6 +109,7 @@ def test_started_event_initially_unset(self):
91109

92110
# ── run() / stop() lifecycle ──────────────────────────────────────────────────
93111

112+
94113
class TestLifecycle:
95114
def test_run_sets_started_event(self, client_api):
96115
assert client_api._started_event.is_set()
@@ -118,6 +137,7 @@ def test_double_stop_does_not_raise(self, client_api):
118137

119138
# ── add_route() ───────────────────────────────────────────────────────────────
120139

140+
121141
class TestAddRoute:
122142
def test_add_route_ignored_in_client_only_mode(self, client_api):
123143
client_api.add_route("GET", "/ignored", AsyncMock())
@@ -163,21 +183,20 @@ async def handler(request):
163183

164184
# ── send_request() ────────────────────────────────────────────────────────────
165185

186+
166187
class TestSendRequest:
167188
def test_raises_in_server_only_mode(self, server_api):
168189
with pytest.raises(HTTPAPIError, match="client mode disabled"):
169190
server_api.send_request(
170-
"GET", "http://example.com", timeout=1.0,
171-
payload=None, headers=None
191+
"GET", "http://example.com", timeout=1.0, payload=None, headers=None
172192
)
173193

174194
def test_returns_empty_string_and_200_when_no_timeout(self, client_api):
175195
"""Fire-and-forget path (timeout=None) returns immediately."""
176196
with patch.object(client_api, "_request", new_callable=AsyncMock) as mock_req:
177197
mock_req.return_value = ("", 200)
178198
body, status = client_api.send_request(
179-
"GET", "http://example.com", timeout=None,
180-
payload=None, headers=None
199+
"GET", "http://example.com", timeout=None, payload=None, headers=None
181200
)
182201
assert body == ""
183202
assert status == 200
@@ -244,8 +263,7 @@ def test_future_added_to_unresolved(self, client_api):
244263
mock_req.return_value = ("", 200)
245264
before = len(client_api.unresolved_futures)
246265
client_api.send_request(
247-
"GET", "http://example.com", timeout=None,
248-
payload=None, headers=None
266+
"GET", "http://example.com", timeout=None, payload=None, headers=None
249267
)
250268
time.sleep(0.05)
251269
# future is appended even for fire-and-forget
@@ -264,6 +282,7 @@ def test_404_response(self, client_server_api):
264282

265283
# ── shutdown() ────────────────────────────────────────────────────────────────
266284

285+
267286
class TestShutdown:
268287
def test_shutdown_clears_unresolved_futures(self, client_api):
269288
mock_future = MagicMock()
@@ -299,6 +318,7 @@ def test_shutdown_with_no_futures_is_noop(self, client_api):
299318

300319
# ── _request() (internal async helper) ───────────────────────────────────────
301320

321+
302322
class TestInternalRequest:
303323
def test_request_returns_text_and_status(self, client_server_api):
304324
async def handler(request):

tests/communication/http/test_connectors.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1+
# Copyright (C) 2026 Kajetan Rachwał
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import base64
2-
import threading
316
import time
417
from io import BytesIO
5-
from typing import Any
6-
from unittest.mock import MagicMock, patch
718

819
import pytest
920
from aiohttp import web
1021
from PIL import Image
11-
1222
from rai.communication.http.api import HTTPConnectorMode
1323
from rai.communication.http.connectors import HTTPConnector, HTTPHRIConnector
1424
from rai.communication.http.messages import HTTPHRIMessage, HTTPMessage
1525

16-
1726
# ── Helpers ───────────────────────────────────────────────────────────────────
1827

28+
1929
def _make_image() -> Image.Image:
2030
img = Image.new("RGB", (4, 4), color=(255, 0, 0))
2131
return img
@@ -29,6 +39,7 @@ def _image_b64(img: Image.Image) -> str:
2939

3040
# ── Fixtures ──────────────────────────────────────────────────────────────────
3141

42+
3243
@pytest.fixture
3344
def client_connector():
3445
c = HTTPConnector(host="localhost", port=19080, mode=HTTPConnectorMode.client)
@@ -46,7 +57,9 @@ def server_connector():
4657
@pytest.fixture
4758
def cs_connector():
4859
"""Client-server connector, useful for end-to-end tests."""
49-
c = HTTPConnector(host="localhost", port=19082, mode=HTTPConnectorMode.client_server)
60+
c = HTTPConnector(
61+
host="localhost", port=19082, mode=HTTPConnectorMode.client_server
62+
)
5063
yield c
5164
c.shutdown()
5265

@@ -60,13 +73,16 @@ def hri_client_connector():
6073

6174
@pytest.fixture
6275
def hri_cs_connector():
63-
c = HTTPHRIConnector(host="localhost", port=19084, mode=HTTPConnectorMode.client_server)
76+
c = HTTPHRIConnector(
77+
host="localhost", port=19084, mode=HTTPConnectorMode.client_server
78+
)
6479
yield c
6580
c.shutdown()
6681

6782

6883
# ── HTTPConnector – initialisation ────────────────────────────────────────────
6984

85+
7086
class TestHTTPConnectorInit:
7187
def test_api_started(self, client_connector):
7288
assert client_connector.api._started_event.is_set()
@@ -80,6 +96,7 @@ def test_thread_alive(self, client_connector):
8096

8197
# ── HTTPConnector – service_call (request-response) ───────────────────────────
8298

99+
83100
class TestHTTPConnectorServiceCall:
84101
def test_service_call_returns_http_message(self, cs_connector):
85102
async def handler(request):
@@ -89,7 +106,9 @@ async def handler(request):
89106
time.sleep(0.1)
90107

91108
msg = HTTPMessage(method="GET", payload=None)
92-
response = cs_connector.service_call(msg, "http://localhost:19082/ping", timeout_sec=5.0)
109+
response = cs_connector.service_call(
110+
msg, "http://localhost:19082/ping", timeout_sec=5.0
111+
)
93112

94113
assert isinstance(response, HTTPMessage)
95114
assert response.payload == "pong"
@@ -119,12 +138,15 @@ async def handler(request):
119138
time.sleep(0.1)
120139

121140
msg = HTTPMessage(method="PUT", payload=None)
122-
response = cs_connector.service_call(msg, "http://localhost:19082/put", timeout_sec=5.0)
141+
response = cs_connector.service_call(
142+
msg, "http://localhost:19082/put", timeout_sec=5.0
143+
)
123144
assert response.method == "PUT"
124145

125146

126147
# ── HTTPConnector – create_service ────────────────────────────────────────────
127148

149+
128150
class TestHTTPConnectorCreateService:
129151
def test_create_service_returns_path(self, cs_connector):
130152
handle = cs_connector.create_service("/svc", lambda data: None)
@@ -183,6 +205,7 @@ def test_create_service_none_result_returns_200(self, cs_connector):
183205

184206
# ── HTTPConnector – shutdown ───────────────────────────────────────────────────
185207

208+
186209
class TestHTTPConnectorShutdown:
187210
def test_shutdown_stops_event_loop(self):
188211
c = HTTPConnector(host="localhost", port=19090, mode=HTTPConnectorMode.client)
@@ -193,6 +216,7 @@ def test_shutdown_stops_event_loop(self):
193216

194217
# ── HTTPHRIConnector – build_message (HRIConnector mixin) ────────────────────
195218

219+
196220
class TestHTTPHRIConnectorBuildMessage:
197221
def test_build_message_from_human_message(self, hri_client_connector):
198222
from langchain_core.messages import HumanMessage

0 commit comments

Comments
 (0)