ClientAdapter

Enum ClientAdapter 

Source
pub enum ClientAdapter {
    Standard,
    Zed,
    OpenAI,
}
Expand description

客户端适配器类型

用于识别不同的客户端并应用相应的响应转换。

§工作流程

  1. 检测客户端类型(通过 HTTP 头、User-Agent、配置等)
  2. 确定偏好的流式格式(SSE/NDJSON/JSON)
  3. 应用客户端特定的响应适配(字段添加、格式调整等)

§使用位置

  • src/api/ollama.rs::detect_ollama_client() - Ollama API 客户端检测
  • src/api/openai.rs::detect_openai_client() - OpenAI API 客户端检测

§示例

let adapter = detect_client(&headers, &config);
let format = adapter.preferred_format();
adapter.apply_response_adaptations(&config, &mut response_data);

Variants§

§

Standard

标准 Ollama 客户端

  • 偏好格式: NDJSON
  • 特殊处理: 无
§

Zed

Zed 编辑器适配

  • 偏好格式: NDJSON
  • 特殊处理: 添加 images 字段
§

OpenAI

OpenAI API 客户端适配(包括 Codex CLI)

  • 偏好格式: SSE
  • 特殊处理: finish_reason 修正(在 llm/stream.rs 中处理)

Implementations§

Source§

impl ClientAdapter

Source

pub fn preferred_format(&self) -> StreamFormat

获取该客户端的首选流式格式

当客户端没有明确指定 Accept 头(或使用 */*)时, 使用此方法返回的格式。

§返回值
  • StreamFormat::SSE - Server-Sent Events (OpenAI/Codex 偏好)
  • StreamFormat::NDJSON - Newline Delimited JSON (Ollama/Zed 偏好)
§使用场景
let format = if headers.get("accept").contains("*/*") {
    adapter.preferred_format()  // 使用偏好格式
} else {
    detected_format  // 使用客户端指定的格式
};
Source

pub fn apply_response_adaptations(&self, config: &Settings, data: &mut Value)

应用客户端特定的响应处理

根据客户端类型,对 LLM 返回的响应数据进行适配转换。

§参数
  • config: 全局配置
  • data: 响应数据(可变引用),会被就地修改
§适配内容
§Standard
  • 无特殊处理
§Zed
  • 添加 images: null 字段(Zed 要求)
§OpenAI
  • finish_reason 修正(在 client.rs 中处理)
§调用位置
  • src/handlers/ollama.rs - 在流式响应的每个 chunk 中调用
  • src/handlers/openai.rs - 在流式响应的每个 chunk 中调用
§示例
let mut response_data = serde_json::from_str(&chunk)?;
adapter.apply_response_adaptations(&config, &mut response_data);
// response_data 已被适配

Trait Implementations§

Source§

impl Clone for ClientAdapter

Source§

fn clone(&self) -> ClientAdapter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ClientAdapter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for ClientAdapter

Source§

fn eq(&self, other: &ClientAdapter) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ClientAdapter

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,