Files
wehub-resource-sync 0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:03:20 +08:00

170 lines
4.6 KiB
Python

"""Base backend interface for Headroom.
Backends translate between the canonical Anthropic Messages API format
(used by the proxy) and provider-specific APIs.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import AsyncIterator
from dataclasses import dataclass, field
from typing import Any
@dataclass
class BackendResponse:
"""Standardized response from a backend."""
# Response body (Anthropic Messages API format)
body: dict[str, Any]
# HTTP status code
status_code: int = 200
# Response headers to forward
headers: dict[str, str] = field(default_factory=dict)
# Error message if any
error: str | None = None
@dataclass
class StreamEvent:
"""A single event from a streaming response."""
# Event type (message_start, content_block_delta, etc.)
event_type: str
# Event data (Anthropic SSE format)
data: dict[str, Any]
# Raw SSE line to forward (if available)
raw_sse: str | None = None
class Backend(ABC):
"""Abstract base class for LLM API backends.
Backends are responsible for:
- Translating requests from Anthropic format to provider format
- Making API calls to the provider
- Translating responses back to Anthropic format
- Handling streaming
"""
@property
@abstractmethod
def name(self) -> str:
"""Backend name (e.g., 'anthropic', 'bedrock')."""
...
@abstractmethod
async def send_message(
self,
body: dict[str, Any],
headers: dict[str, str],
) -> BackendResponse:
"""Send a non-streaming message request.
Args:
body: Request body in Anthropic Messages API format.
headers: Request headers (may include API keys, etc.).
Returns:
BackendResponse with body in Anthropic Messages API format.
"""
...
@abstractmethod
def stream_message(
self,
body: dict[str, Any],
headers: dict[str, str],
) -> AsyncIterator[StreamEvent]:
"""Stream a message request.
Args:
body: Request body in Anthropic Messages API format.
headers: Request headers.
Yields:
StreamEvent objects in Anthropic SSE format.
"""
...
@abstractmethod
def map_model_id(self, anthropic_model: str) -> str:
"""Map Anthropic model ID to provider model ID.
Args:
anthropic_model: Model ID in Anthropic format (e.g., 'claude-3-opus-20240229').
Returns:
Model ID in provider format.
"""
...
@abstractmethod
def supports_model(self, model: str) -> bool:
"""Check if this backend supports a model.
Args:
model: Model ID (can be Anthropic or provider format).
Returns:
True if model is supported.
"""
...
async def send_openai_message(
self,
body: dict[str, Any],
headers: dict[str, str],
) -> BackendResponse:
"""Send an OpenAI-format message request.
Unlike send_message(), this takes OpenAI-format input and returns
OpenAI-format output (no Anthropic conversion). Optional - only
implemented by backends that support OpenAI-compatible APIs.
Args:
body: Request body in OpenAI chat completion format.
headers: Request headers.
Returns:
BackendResponse with body in OpenAI chat completion format.
Raises:
NotImplementedError: If backend doesn't support OpenAI format.
"""
raise NotImplementedError(f"{self.name} backend does not support OpenAI format")
async def stream_openai_message(
self,
body: dict[str, Any],
headers: dict[str, str],
) -> AsyncIterator[str]:
"""Stream an OpenAI-format chat completion.
Yields SSE-formatted strings: 'data: {...}\\n\\n' for each chunk,
ending with 'data: [DONE]\\n\\n'.
Args:
body: Request body in OpenAI chat completion format (stream: true).
headers: Request headers.
Yields:
SSE-formatted strings ready to send to client.
Raises:
NotImplementedError: If backend doesn't support OpenAI streaming.
"""
raise NotImplementedError(f"{self.name} backend does not support OpenAI streaming")
# Make this an async generator (yield never reached but needed for type)
yield "" # type: ignore[misc] # pragma: no cover
async def close(self) -> None: # noqa: B027
"""Clean up resources (e.g., close HTTP clients)."""
pass