pub struct AgentLinkClient { /* private fields */ }Expand description
AgentLink SDK 主客户端
这是 SDK 的主要入口点,提供对所有服务的访问。
§Example
ⓘ
use agentlink_sdk::{AgentLinkClient, ClientConfig};
async fn example() {
let config = ClientConfig::default();
let client = AgentLinkClient::new(config);
// 登录
let response = client.auth()
.login_with_email_code("user@example.com", "123456")
.await
.unwrap();
// 连接 MQTT(自动启动事件循环)
client.connect_mqtt(&response.token, &response.user.id)
.await
.unwrap();
}§使用 API Key(类似 OpenAI SDK)
ⓘ
// 方式 1:从环境变量自动读取 API Key
// 环境变量: AGENTLINK_API_KEY=your-api-key
let client = AgentLinkClient::from_env().expect("Missing AGENTLINK_API_KEY");
// 方式 2:直接传入 API Key
let client = AgentLinkClient::from_api_key("your-api-key");
// 连接 MQTT 并启动事件循环
client.connect_and_start().await.expect("Connection failed");
// 注册事件处理器
client.on(EVENT_MESSAGE_RECEIVED, |event| async move {
println!("收到消息: {:?}", event);
}).await;Implementations§
Source§impl AgentLinkClient
impl AgentLinkClient
Sourcepub fn new(config: ClientConfig) -> Self
pub fn new(config: ClientConfig) -> Self
创建新的 SDK 客户端
Sourcepub fn from_env() -> Result<Self, SdkError>
pub fn from_env() -> Result<Self, SdkError>
从环境变量创建客户端(类似 OpenAI SDK 风格)
§环境变量
AGENTLINK_API_KEY: API Key(必需)AGENTLINK_API_URL: API 基础 URL(可选)AGENTLINK_MQTT_URL: MQTT Broker URL(可选)
§Example
ⓘ
// 设置环境变量
std::env::set_var("AGENTLINK_API_KEY", "your-api-key");
// 创建客户端
let client = AgentLinkClient::from_env().expect("Missing AGENTLINK_API_KEY");Sourcepub fn try_from_env() -> Option<Self>
pub fn try_from_env() -> Option<Self>
尝试从环境变量创建客户端,如果环境变量不存在则返回 None
Sourcepub fn from_api_key(api_key: &str) -> Self
pub fn from_api_key(api_key: &str) -> Self
Sourcepub fn from_api_key_with_urls(
api_key: &str,
api_url: &str,
mqtt_url: &str,
) -> Self
pub fn from_api_key_with_urls( api_key: &str, api_url: &str, mqtt_url: &str, ) -> Self
Sourcepub fn config(&self) -> &ClientConfig
pub fn config(&self) -> &ClientConfig
获取当前配置
Sourcepub fn update_config(&mut self, config: ClientConfig)
pub fn update_config(&mut self, config: ClientConfig)
更新配置
Sourcepub fn auth(&self) -> AuthService
pub fn auth(&self) -> AuthService
获取认证服务
不需要认证即可使用
Sourcepub fn users(&self) -> UserService
pub fn users(&self) -> UserService
获取用户服务
需要认证 Token 才能调用具体方法
Sourcepub fn messages(&self) -> MessageService
pub fn messages(&self) -> MessageService
获取消息服务
需要认证 Token 才能调用具体方法
Sourcepub fn conversations(&self) -> ConversationService
pub fn conversations(&self) -> ConversationService
获取会话服务
需要认证 Token 才能调用具体方法
Sourcepub fn friends(&self) -> FriendService
pub fn friends(&self) -> FriendService
获取好友服务
需要认证 Token 才能调用具体方法
Sourcepub async fn disconnect_mqtt(&self) -> SdkResult<()>
pub async fn disconnect_mqtt(&self) -> SdkResult<()>
断开 MQTT 连接
Sourcepub async fn mqtt_connection_state(&self) -> MqttConnectionState
pub async fn mqtt_connection_state(&self) -> MqttConnectionState
获取 MQTT 连接状态
Sourcepub async fn is_mqtt_connected(&self) -> bool
pub async fn is_mqtt_connected(&self) -> bool
检查 MQTT 是否已连接
Sourcepub async fn unsubscribe(&self, topic: &str) -> SdkResult<()>
pub async fn unsubscribe(&self, topic: &str) -> SdkResult<()>
Sourcepub async fn on<T, F, Fut>(&self, event_type: &str, callback: F)where
T: DeserializeOwned + Send + Sync + 'static,
F: Fn(ServerEvent<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
pub async fn on<T, F, Fut>(&self, event_type: &str, callback: F)where
T: DeserializeOwned + Send + Sync + 'static,
F: Fn(ServerEvent<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
注册事件回调
§Example
ⓘ
use agentlink_sdk::{AgentLinkClient, ClientConfig, EVENT_MESSAGE_RECEIVED, ServerEvent, MessageReceivedData};
let client = AgentLinkClient::new(ClientConfig::default());
client.on(EVENT_MESSAGE_RECEIVED, |event: ServerEvent<MessageReceivedData>| async move {
println!("收到新消息: {:?}", event.data.content);
}).await;Sourcepub async fn clear_callbacks(&self)
pub async fn clear_callbacks(&self)
Sourcepub fn event_loop(&self) -> Arc<RwLock<EventLoop>>
pub fn event_loop(&self) -> Arc<RwLock<EventLoop>>
获取事件循环的引用(高级用法)
大多数情况下,应该使用 client.on() 方法注册回调,
而不是直接访问 event_loop。
Sourcepub async fn start_event_loop(&self) -> SdkResult<()>
pub async fn start_event_loop(&self) -> SdkResult<()>
Sourcepub async fn stop_event_loop(&self)
pub async fn stop_event_loop(&self)
停止事件循环
Sourcepub async fn mqtt_subscriptions(&self) -> Vec<String>
pub async fn mqtt_subscriptions(&self) -> Vec<String>
获取所有订阅的主题
Sourcepub async fn connect_and_start(&self) -> SdkResult<()>
pub async fn connect_and_start(&self) -> SdkResult<()>
使用 API Key 连接 MQTT 并启动事件循环
这是一个便捷方法,适合使用 API Key 认证的场景(类似 OpenAI SDK)。
§流程
- 使用 API Key 作为 MQTT 密码连接
- 启动事件循环
§Example
ⓘ
// 从环境变量创建客户端
let client = AgentLinkClient::from_env().expect("Missing AGENTLINK_API_KEY");
// 连接并启动事件循环
client.connect_and_start().await.expect("Connection failed");
// 注册事件处理器
client.on(EVENT_MESSAGE_RECEIVED, |event| async move {
println!("收到消息: {:?}", event);
}).await;Sourcepub async fn connect_mqtt_with_api_key(&self, api_key: &str) -> SdkResult<()>
pub async fn connect_mqtt_with_api_key(&self, api_key: &str) -> SdkResult<()>
Sourcepub fn with_token(&self, token: &str) -> &Self
pub fn with_token(&self, token: &str) -> &Self
Sourcepub fn clear_token(&self)
pub fn clear_token(&self)
清除 Token(用于登出)
Auto Trait Implementations§
impl Freeze for AgentLinkClient
impl !RefUnwindSafe for AgentLinkClient
impl Send for AgentLinkClient
impl Sync for AgentLinkClient
impl Unpin for AgentLinkClient
impl UnsafeUnpin for AgentLinkClient
impl !UnwindSafe for AgentLinkClient
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