"""适配器接口与中间数据形(ARCH §4.2/§4.4)。 适配器把 `LlmRequest` 翻译成目标厂商请求,并把响应/流/usage 翻译回统一中间形。 """ from __future__ import annotations from collections.abc import AsyncIterator from typing import Protocol, runtime_checkable from pydantic import BaseModel, ConfigDict from ..types import LlmRequest class Capabilities(BaseModel): structured_output: bool = False prefix_cache: bool = False thinking: bool = False class ProviderUsage(BaseModel): input_tokens: int output_tokens: int cache_read_tokens: int = 0 class ProviderResult(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) text: str usage: ProviderUsage parsed: BaseModel | None = None # output_schema 命中时的结构化结果(§4.4) class StreamChunk(BaseModel): """流式块:文本增量(usage=None),或末尾用量块(text="")。""" text: str = "" usage: ProviderUsage | None = None @runtime_checkable class ProviderAdapter(Protocol): provider: str def capabilities(self) -> Capabilities: ... async def complete(self, req: LlmRequest, model: str) -> ProviderResult: ... def stream(self, req: LlmRequest, model: str) -> AsyncIterator[StreamChunk]: ...