Skip to main content

rucora_core/
lib.rs

1//! rucora-core - rucora 核心抽象层
2//!
3//! # 概述
4//!
5//! `rucora-core` 是 rucora 框架的核心抽象层,只包含 trait、类型定义、错误类型和事件模型。
6//! 它不包含任何具体实现(如 Provider、Tool、Runtime 的具体实现),便于第三方实现与长期兼容。
7//!
8//! # 设计目标
9//!
10//! - **接口与实现解耦**: 只定义 trait 与核心类型,第三方可以只依赖 core 自己实现 Provider/Tool/VectorStore 等
11//! - **稳定抽象**: 提供稳定的接口,避免频繁变更影响上层实现
12//! - **最小依赖**: 不绑定具体实现,保持轻量级
13//!
14//! # 核心模块
15//!
16//! ## Agent(智能体)
17//!
18//! Agent 是智能体的核心,负责:
19//! - 思考、决策、规划
20//! - 接收输入(消息、任务、上下文)
21//! - 返回决策结果
22//!
23//! 相关类型:
24//! - [`agent::types::AgentInput`][]: Agent 输入类型
25//! - [`agent::types::AgentOutput`][]: Agent 输出类型
26//! - [`agent::Agent`][]: Agent trait
27//! - [`agent::AgentExecutor`][]: Agent 执行器 trait
28//!
29//! ## Channel(通信渠道)
30//!
31//! 统一的事件模型,用于在运行时、工具、技能之间传递消息。
32//!
33//! 相关类型:
34//! - [`channel::types::ChannelEvent`][]: 统一事件类型
35//! - [`channel::types::TokenDeltaEvent`][]: Token 流式输出事件
36//! - [`channel::types::DebugEvent`][]: 调试事件
37//! - [`channel::types::ErrorEvent`][]: 错误事件
38//!
39//! ## Provider(LLM 提供者)
40//!
41//! LLM Provider 抽象,定义与大型语言模型交互的接口。
42//!
43//! 相关 trait:
44//! - [`provider::LlmProvider`][]: LLM Provider trait
45//!
46//! 相关类型:
47//! - [`provider::types::ChatRequest`][]: 聊天请求
48//! - [`provider::types::ChatResponse`][]: 聊天响应
49//! - [`provider::types::ChatMessage`][]: 聊天消息
50//! - [`provider::types::Role`][]: 消息角色(System/User/Assistant/Tool)
51//!
52//! ## Tool(工具)
53//!
54//! 工具是可以被 Agent 调用的"可执行能力",例如:读取文件、访问网页、查询数据库等。
55//!
56//! 相关 trait:
57//! - [`tool::Tool`][]: Tool trait
58//!
59//! 相关类型:
60//! - [`tool::types::ToolDefinition`][]: 工具定义
61//! - [`tool::types::ToolCall`][]: 工具调用
62//! - [`tool::types::ToolResult`][]: 工具结果
63//! - [`tool::ToolCategory`][]: 工具分类
64//!
65//! ## Skill(技能)
66//!
67//! 技能是对 Tool/Provider/Memory 的组合封装,提供更高层次的抽象。
68//!
69//! 相关 trait:
70//! - [`skill::Skill`][]: Skill trait
71//!
72//! 相关类型:
73//! - [`skill::types::SkillContext`][]: 技能上下文
74//! - [`skill::types::SkillOutput`][]: 技能输出
75//!
76//! ## Memory(记忆)
77//!
78//! 记忆抽象,用于添加和检索长期记忆。
79//!
80//! 相关 trait:
81//! - [`memory::Memory`][]: Memory trait
82//!
83//! 相关类型:
84//! - [`memory::types::MemoryItem`][]: 记忆项
85//! - [`memory::types::MemoryQuery`][]: 记忆查询
86//!
87//! ## Embedding(向量嵌入)
88//!
89//! 向量嵌入抽象,用于文本向量化。
90//!
91//! 相关 trait:
92//! - [`embed::EmbeddingProvider`][]: Embedding Provider trait
93//!
94//! ## Retrieval(语义检索)
95//!
96//! 向量存储与相似度搜索抽象。
97//!
98//! 相关 trait:
99//! - [`retrieval::VectorStore`][]: VectorStore trait
100//!
101//! 相关类型:
102//! - [`retrieval::VectorRecord`][]: 向量记录
103//! - [`retrieval::VectorQuery`][]: 向量查询
104//! - [`retrieval::SearchResult`][]: 搜索结果
105//!
106//! # 使用示例
107//!
108//! ## 实现自定义 Provider
109//!
110//! ```rust,ignore
111//! use rucora_core::provider::{LlmProvider, types::{ChatRequest, ChatMessage, Role, ChatResponse, ChatStreamChunk}};
112//! use rucora_core::error::ProviderError;
113//! use async_trait::async_trait;
114//! use futures_util::stream::BoxStream;
115//!
116//! struct MyProvider;
117//!
118//! #[async_trait]
119//! impl LlmProvider for MyProvider {
120//!     async fn chat(&self, request: ChatRequest) -> Result<ChatResponse, ProviderError> {
121//!         let msg = ChatMessage { role: Role::Assistant, content: "Hello!".to_string(), name: None };
122//!         Ok(ChatResponse { message: msg, tool_calls: vec![], usage: None, finish_reason: None })
123//!     }
124//!     fn stream_chat(&self, request: ChatRequest) -> Result<BoxStream<'static, Result<ChatStreamChunk, ProviderError>>, ProviderError> {
125//!         Err(ProviderError::Message("not implemented".to_string()))
126//!     }
127//! }
128//! ```
129//!
130//! ## 实现自定义 Tool
131//!
132//! ```rust,ignore
133//! use rucora_core::tool::{Tool, ToolCategory};
134//! use rucora_core::error::ToolError;
135//! use async_trait::async_trait;
136//! use serde_json::{Value, json};
137//!
138//! struct EchoTool;
139//!
140//! #[async_trait]
141//! impl Tool for EchoTool {
142//!     fn name(&self) -> &str { "echo" }
143//!     fn description(&self) -> Option<&str> { Some("回显输入内容") }
144//!     fn categories(&self) -> &'static [ToolCategory] { &[ToolCategory::Basic] }
145//!     fn input_schema(&self) -> Value {
146//!         json!({"type": "object", "properties": {"text": {"type": "string"}}})
147//!     }
148//!     async fn call(&self, input: Value) -> Result<Value, ToolError> {
149//!         let text = input.get("text").and_then(|v| v.as_str()).unwrap_or("");
150//!         Ok(json!({"echo": text}))
151//!     }
152//! }
153//! ```
154//!
155//! # 错误处理
156//!
157//! 统一的错误类型定义:
158//!
159//! - [`ProviderError`][]: Provider 错误
160//! - [`ToolError`][]: Tool 错误
161//! - [`SkillError`][]: Skill 错误
162//! - [`AgentError`][]: Agent/Runtime 错误
163//! - [`MemoryError`][]: Memory 错误
164//! - [`ChannelError`][]: Channel 错误
165//!
166//! 所有错误类型都实现了 [`error::DiagnosticError`] trait,提供结构化诊断信息。
167//!
168//! # 事件模型
169//!
170//! [`channel::types::ChannelEvent`] 是统一的事件类型,支持:
171//!
172//! - `Message`: 对话消息事件
173//! - `TokenDelta`: Token 流式输出事件
174//! - `ToolCall`: 工具调用事件
175//! - `ToolResult`: 工具结果事件
176//! - `Skill`: 技能相关事件
177//! - `Memory`: 记忆相关事件
178//! - `Debug`: 调试事件
179//! - `Error`: 错误事件
180//! - `Raw`: 原始事件(用于透传)
181
182/// Agent 核心抽象(运行入口)
183pub mod agent;
184
185/// 通信渠道抽象(事件发送与订阅)
186pub mod channel;
187
188/// 向量嵌入抽象(文本向量化)
189pub mod embed;
190
191/// 记忆抽象(添加与检索)
192pub mod memory;
193
194/// LLM 提供者抽象(对话/流式对话等)
195pub mod provider;
196
197/// 语义检索抽象(向量存储与相似度搜索)
198pub mod retrieval;
199
200/// 技能抽象(更高层的可复用能力单元)
201pub mod skill;
202
203/// 工具抽象(名称、输入 schema、执行)
204pub mod tool;
205
206/// 统一错误类型定义
207pub mod error;
208
209/// 结构化错误分类器 trait(纯接口)
210pub mod error_classifier_trait;
211
212/// Prompt 注入防护扫描器 trait(纯接口)
213pub mod injection_guard_trait;
214
215/// Graceful Shutdown(优雅关闭)支持
216pub mod graceful_shutdown;
217
218/// RetryPolicy(重试策略)接口
219pub mod retry;
220
221// 重新导出常用类型
222pub use agent::types::{AgentInput, AgentOutput};
223pub use channel::types::ChannelEvent;
224pub use error::{AgentError, ChannelError, MemoryError, ProviderError, SkillError, ToolError};
225
226// 重新导出错误分类器 trait
227pub use error_classifier_trait::{
228    ClassifiedError, ErrorClassifier, ErrorContext, FailoverReason, ProviderErrorExt,
229};
230
231// 重新导出注入防护 trait
232pub use injection_guard_trait::{ContentScannable, InjectionGuard, ScanResult, Threat, ThreatType};
233
234pub use provider::LlmProvider;
235pub use provider::types::LlmParams;
236pub use tool::Tool;
237pub use graceful_shutdown::{GracefulShutdown, ShutdownHandle, ShutdownState, ShutdownToken};
238pub use retry::{ExponentialBackoff, FixedDelay, NoRetry, RetryPolicy, RetryPolicyExt};