Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
perf(ui-server): gzip JSON API responses (#762)
Call White10af6a98819d
diff --git a/tests/test_ui_server_fastapi.py b/tests/test_ui_server_fastapi.pyindex 0fe0ff1fe..acb81b7bc 100644--- a/tests/test_ui_server_fastapi.py+++ b/tests/test_ui_server_fastapi.py@@ -19,6 +19,18 @@ from tests.test_api_save_config_merge import _full_config_payloadfrom tests.ui_server_test_helpers import csrf_headers+def _raw_client_get(client, path: str, *, headers: dict[str, str] | None = None):+ request_headers = {TEST_REMOTE_ADDR_HEADER: "127.0.0.1"}+ request_headers.update(headers or {})+ with client._client.stream(+ "GET",+ f"http://127.0.0.1{path}",+ headers=request_headers,+ ) as response:+ body = b"".join(response.iter_raw())+ return response, body++def test_websocket_echo_is_disabled_by_default(monkeypatch):monkeypatch.delenv("VIBE_UI_ENABLE_WS_ECHO", raising=False)@@ -715,6 +727,104 @@ def test_static_ui_asset_gzip_skips_small_and_binary_files(monkeypatch, tmp_pathassert "Vary" not in binary_response.headers+def test_json_api_gzip_uses_shared_response_rules(monkeypatch, tmp_path):+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path / "home"))++ client = app.test_client()+ identity_response = client.get("/api/config", headers={"Accept-Encoding": ""})+ original = identity_response.content++ assert identity_response.status_code == 200+ assert identity_response.is_json+ assert len(original) >= ui_server._SHOW_RUNTIME_COMPRESSIBLE_MIN_BYTES+ assert "Content-Encoding" not in identity_response.headers+ assert "Accept-Encoding" in identity_response.headers["Vary"]++ gzip_disabled_response = client.get("/api/config", headers={"Accept-Encoding": "br, gzip;q=0"})+ assert gzip_disabled_response.status_code == 200+ assert gzip_disabled_response.content == original+ assert "Content-Encoding" not in gzip_disabled_response.headers+ assert "Accept-Encoding" in gzip_disabled_response.headers["Vary"]++ gzip_client = app.test_client()+ gzip_response, compressed = _raw_client_get(+ gzip_client,+ "/api/config",+ headers={"Accept-Encoding": "gzip"},+ )++ assert gzip_response.status_code == 200+ assert gzip_response.headers["Content-Encoding"] == "gzip"+ assert gzip_response.headers["Content-Length"] == str(len(compressed))+ assert "Accept-Encoding" in gzip_response.headers["Vary"]+ assert gzip.decompress(compressed) == original+ assert any(header.startswith("vibe_csrf_token=") for header in gzip_response.headers.get_list("Set-Cookie"))+++def test_json_api_gzip_skips_small_responses(monkeypatch, tmp_path):+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path / "home"))++ response = app.test_client().get("/api/csrf-token", headers={"Accept-Encoding": "gzip"})++ assert response.status_code == 200+ assert response.is_json+ assert len(response.content) < ui_server._SHOW_RUNTIME_COMPRESSIBLE_MIN_BYTES+ assert "Content-Encoding" not in response.headers+++def test_json_api_gzip_skips_sse_streaming_response():+ from fastapi.responses import StreamingResponse++ async def generate():+ yield b": stream connected\n\n"++ response = StreamingResponse(generate(), media_type="text/event-stream")++ with app.test_request_context("/api/events", headers={"Accept-Encoding": "gzip"}):+ compressed_response = ui_server._compress_materialized_api_response(response)++ assert compressed_response is response+ assert "Content-Encoding" not in response.headers+ assert response.media_type == "text/event-stream"++ body = b"event: message\ndata: {}\n\n" * 100+ materialized = ui_server.Response(content=body, mimetype="text/event-stream")+ with app.test_request_context("/api/events", headers={"Accept-Encoding": "gzip"}):+ materialized_response = ui_server._compress_materialized_api_response(materialized)++ assert materialized_response is materialized+ assert materialized_response.body == body+ assert "Content-Encoding" not in materialized_response.headers+++def test_json_api_gzip_skips_attachments_and_existing_encoding():+ body = b'{"items":[' + (b'"payload",' * 300) + b'"end"]}'+ attachment = ui_server.Response(+ content=body,+ mimetype="application/json",+ headers={"Content-Disposition": "attachment; filename=data.json"},+ )+ with app.test_request_context("/api/export", headers={"Accept-Encoding": "gzip"}):+ attachment_response = ui_server._compress_materialized_api_response(attachment)++ assert attachment_response is attachment+ assert attachment_response.body == body+ assert "Content-Encoding" not in attachment_response.headers+ assert "Vary" not in attachment_response.headers++ encoded = ui_server.Response(+ content=body,+ mimetype="application/json",+ headers={"Content-Encoding": "br"},+ )+ with app.test_request_context("/api/precompressed", headers={"Accept-Encoding": "gzip"}):+ encoded_response = ui_server._compress_materialized_api_response(encoded)++ assert encoded_response is encoded+ assert encoded_response.body == body+ assert encoded_response.headers["Content-Encoding"] == "br"++def test_run_maybe_async_offloads_sync_handlers_without_losing_context():import asyncioimport threadingdiff --git a/vibe/ui_server.py b/vibe/ui_server.pyindex 5f9b4b53c..569182292 100644--- a/vibe/ui_server.py+++ b/vibe/ui_server.py@@ -1841,6 +1841,11 @@ def protect_mutating_ui_requests():return None[email protected]_request+def compress_materialized_api_response(response: Response) -> Response:+ return _compress_materialized_api_response(response)++@app.after_requestdef add_api_timing_headers(response: Response) -> Response:started_at = getattr(g, "api_request_started_at", None)@@ -7582,6 +7587,10 @@ def _compress_response_content(content: bytes, headers: dict[str, str], starlettreturn contentif _response_header(headers, "content-encoding"):return content+ if _show_response_is_attachment(_response_header(headers, "content-disposition")):+ return content+ if starlette_request.headers.get("upgrade", "").lower() == "websocket":+ return contentcontent_type = _response_header(headers, "content-type") or ""if not _show_response_is_compressible(content_type):return content@@ -7594,9 +7603,26 @@ def _compress_response_content(content: bytes, headers: dict[str, str], starlett_remove_response_header(headers, "content-length")_remove_response_header(headers, "etag")_set_response_header(headers, "Content-Encoding", "gzip")+ _set_response_header(headers, "Content-Length", str(len(compressed)))return compressed+def _compress_materialized_api_response(response: Response) -> Response:+ if not (request.path or "").startswith("/api/"):+ return response+ body = getattr(response, "body", None)+ if body is None:+ return response+ if not isinstance(body, bytes | bytearray | memoryview):+ return response+ content = bytes(body)+ compressed = _compress_response_content(content, response.headers, request._request)+ if compressed is content:+ return response+ response.body = compressed+ return response++def _accept_encoding_allows_gzip(accept_encoding: str | None) -> bool:if not accept_encoding:return False@@ -7633,6 +7659,8 @@ def _show_response_is_compressible(content_type: str | None) -> bool:if not content_type:return Falselowered = content_type.lower()+ if "text/event-stream" in lowered:+ return Falsereturn any(marker in loweredfor marker in (