91d32a6a82
新增 LLM 调用生命周期引擎,包含 Provider 抽象、OpenAI 兼容实现、 可重试机制及 Token 用量追踪。移除原有的占位测试代码。 添加所需的 Rust 依赖(tokio、reqwest、serde 等)。
38 lines
1.2 KiB
Rust
38 lines
1.2 KiB
Rust
use std::time::Duration;
|
||
|
||
/// LLM 调用过程中可能发生的所有错误。
|
||
///
|
||
/// 错误按可重试性分为两类:
|
||
/// - **可重试**:`RateLimit`、`Timeout`、状态码 >= 500
|
||
/// - **不可重试**:`Authentication`、`ContextLength`、状态码 4xx(除 429)
|
||
#[derive(thiserror::Error, Debug)]
|
||
pub enum LlmError {
|
||
/// API 认证失败(如 API key 无效)。
|
||
#[error("认证失败: {0}")]
|
||
Authentication(String),
|
||
|
||
/// 请求被限流,可选地附带重试等待时间。
|
||
#[error("限流(retry_after={retry_after:?})")]
|
||
RateLimit { retry_after: Option<Duration> },
|
||
|
||
/// HTTP 请求失败,包含状态码和响应体。
|
||
#[error("请求失败(status={status}): {body}")]
|
||
Request { status: u16, body: String },
|
||
|
||
/// 请求超时。
|
||
#[error("请求超时(duration={duration:?})")]
|
||
Timeout { duration: Duration },
|
||
|
||
/// 流式响应处理错误(预留)。
|
||
#[error("流式响应错误: {0}")]
|
||
Stream(String),
|
||
|
||
/// 上下文长度超限。
|
||
#[error("上下文超限(actual={actual}, limit={limit})")]
|
||
ContextLength { actual: u32, limit: u32 },
|
||
|
||
/// 其他未分类的 LLM 调用失败。
|
||
#[error("LLM 调用失败: {0}")]
|
||
Other(String),
|
||
}
|