feat(llm): 添加 LLM 调用周期核心模块

新增 LLM 调用生命周期引擎,包含 Provider 抽象、OpenAI 兼容实现、
可重试机制及 Token 用量追踪。移除原有的占位测试代码。
添加所需的 Rust 依赖(tokio、reqwest、serde 等)。
This commit is contained in:
徐涛
2026-05-12 06:06:24 +08:00
parent b21e163be0
commit 91d32a6a82
10 changed files with 788 additions and 13 deletions
+37
View File
@@ -0,0 +1,37 @@
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),
}