free-claude-code/smoke/lib/http.py
Alishahryar1 f3a7528d49
Some checks are pending
CI / checks (push) Waiting to run
Major refactor: API, providers, messaging, and Anthropic protocol
Consolidates the incremental refactor work into a single change set: modular web tools (api/web_tools), native Anthropic request building and SSE block policy, OpenAI conversion and error handling, provider transports and rate limiting, messaging handler and tree queue, safe logging, smoke tests, and broad test coverage.
2026-04-26 03:01:14 -07:00

71 lines
1.8 KiB
Python

"""HTTP helpers for live smoke requests."""
from __future__ import annotations
from typing import Any
import httpx
from core.anthropic.stream_contracts import SSEEvent, parse_sse_lines
from .config import SmokeConfig, auth_headers, redacted
from .server import RunningServer
def message_payload(
text: str,
*,
model: str = "claude-3-5-sonnet-20241022",
max_tokens: int = 128,
extra: dict[str, Any] | None = None,
) -> dict[str, Any]:
payload: dict[str, Any] = {
"model": model,
"max_tokens": max_tokens,
"messages": [{"role": "user", "content": text}],
}
if extra:
payload.update(extra)
return payload
def post_json(
server: RunningServer,
path: str,
payload: dict[str, Any],
config: SmokeConfig,
*,
headers: dict[str, str] | None = None,
) -> httpx.Response:
request_headers = headers or auth_headers()
response = httpx.post(
f"{server.base_url}{path}",
headers=request_headers,
json=payload,
timeout=config.timeout_s,
)
return response
def collect_message_stream(
server: RunningServer,
payload: dict[str, Any],
config: SmokeConfig,
*,
headers: dict[str, str] | None = None,
) -> list[SSEEvent]:
request_headers = headers or auth_headers()
with httpx.stream(
"POST",
f"{server.base_url}/v1/messages",
headers=request_headers,
json=payload,
timeout=config.timeout_s,
) as response:
if response.status_code != 200:
body = response.read().decode("utf-8", errors="replace")
raise AssertionError(
f"stream request failed: HTTP {response.status_code} "
f"{redacted(body[:1000])}"
)
return parse_sse_lines(response.iter_lines())