pub enum ClientAdapter {
Standard,
Zed,
OpenAI,
}Expand description
客户端适配器类型
用于识别不同的客户端并应用相应的响应转换。
§工作流程
- 检测客户端类型(通过 HTTP 头、User-Agent、配置等)
- 确定偏好的流式格式(SSE/NDJSON/JSON)
- 应用客户端特定的响应适配(字段添加、格式调整等)
§使用位置
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
impl ClientAdapter
Sourcepub fn preferred_format(&self) -> StreamFormat
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 // 使用客户端指定的格式
};Sourcepub fn apply_response_adaptations(&self, config: &Settings, data: &mut Value)
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
impl Clone for ClientAdapter
Source§fn clone(&self) -> ClientAdapter
fn clone(&self) -> ClientAdapter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ClientAdapter
impl Debug for ClientAdapter
Source§impl PartialEq for ClientAdapter
impl PartialEq for ClientAdapter
impl StructuralPartialEq for ClientAdapter
Auto Trait Implementations§
impl Freeze for ClientAdapter
impl RefUnwindSafe for ClientAdapter
impl Send for ClientAdapter
impl Sync for ClientAdapter
impl Unpin for ClientAdapter
impl UnwindSafe for ClientAdapter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more