Integrate unfinished server-rs refactor worklists
This commit is contained in:
@@ -109,6 +109,20 @@ pub enum LlmError {
|
||||
Deserialize(String),
|
||||
}
|
||||
|
||||
// 平台层只暴露稳定错误分类,HTTP status 和业务文案由 api-server 再映射。
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum LlmErrorKind {
|
||||
InvalidConfig,
|
||||
InvalidRequest,
|
||||
Timeout,
|
||||
Connectivity,
|
||||
Upstream,
|
||||
StreamUnavailable,
|
||||
EmptyResponse,
|
||||
Transport,
|
||||
Deserialize,
|
||||
}
|
||||
|
||||
// 统一 OpenAI 兼容文本网关 client。
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LlmClient {
|
||||
@@ -397,6 +411,22 @@ impl fmt::Display for LlmError {
|
||||
|
||||
impl Error for LlmError {}
|
||||
|
||||
impl LlmError {
|
||||
pub fn kind(&self) -> LlmErrorKind {
|
||||
match self {
|
||||
Self::InvalidConfig(_) => LlmErrorKind::InvalidConfig,
|
||||
Self::InvalidRequest(_) => LlmErrorKind::InvalidRequest,
|
||||
Self::Timeout { .. } => LlmErrorKind::Timeout,
|
||||
Self::Connectivity { .. } => LlmErrorKind::Connectivity,
|
||||
Self::Upstream { .. } => LlmErrorKind::Upstream,
|
||||
Self::StreamUnavailable => LlmErrorKind::StreamUnavailable,
|
||||
Self::EmptyResponse => LlmErrorKind::EmptyResponse,
|
||||
Self::Transport(_) => LlmErrorKind::Transport,
|
||||
Self::Deserialize(_) => LlmErrorKind::Deserialize,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LlmClient {
|
||||
pub fn new(config: LlmConfig) -> Result<Self, LlmError> {
|
||||
let http_client = Client::builder().build().map_err(|error| {
|
||||
@@ -1108,6 +1138,23 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn llm_error_kind_is_stable_for_adapter_mapping() {
|
||||
assert_eq!(
|
||||
LlmError::InvalidConfig("bad config".to_string()).kind(),
|
||||
LlmErrorKind::InvalidConfig
|
||||
);
|
||||
assert_eq!(
|
||||
LlmError::Upstream {
|
||||
status_code: 429,
|
||||
message: "too many requests".to_string(),
|
||||
}
|
||||
.kind(),
|
||||
LlmErrorKind::Upstream
|
||||
);
|
||||
assert_eq!(LlmError::EmptyResponse.kind(), LlmErrorKind::EmptyResponse);
|
||||
}
|
||||
|
||||
struct MockResponse {
|
||||
status_line: &'static str,
|
||||
content_type: &'static str,
|
||||
|
||||
Reference in New Issue
Block a user