Skip to content

后端开发指南

IfAI Rust 后端与 Tauri 2.0 开发指南。

快速开始

前置要求

  • Rust 1.70+
  • Tauri CLI
  • 异步 Rust 经验

开发设置

bash
# 安装 Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 启动开发
cd ifai
npm run tauri:dev

Tauri 命令

创建命令

rust
#[tauri::command]
pub async fn example_command(input: String) -> Result<String, String> {
    Ok(format!("处理: {}", input))
}

注册命令

rust
// lib.rs
tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![
        example_command
    ])
    .run()

AI 集成

提供商抽象

rust
#[async_trait]
pub trait AIProvider {
    async fn chat(&self, messages: Vec<Message>) -> Result<String>;
}

错误处理

rust
use thiserror::Error;

#[derive(Error, Debug)]
pub enum IfAIError {
    #[error("IO 错误: {0}")]
    Io(#[from] std::io::Error),
}

测试

bash
cargo test

下一步

基于 MIT 许可发布