Agent Skills: Tauri Development Skill

当用户要求"创建 Tauri 命令"、"添加后端命令"、"集成 Rust 和 React"、"设置 specta 类型"、"从前端调用 Tauri"、"优化 Tauri 性能"、"处理 Tauri 错误"、"前后端类型不一致"、"更新 bindings.ts"、"specta 类型问题"、"跨平台桌面开发"、"窗口管理"、"文件系统访问"、"系统托盘",或者提到"Tauri"、"桌面应用"、"命令通信"、"类型绑定"、"桌面 UI"时使用此技能。用于饲料配方系统的 Tauri 特定模式、Rust 后端集成、前后端通信、类型安全保障或使用 Tauri 2.0 开发跨平台桌面应用。

UncategorizedID: cacr92/wereply/tauri-development

Install this agent skill to your local

pnpm dlx add-skill https://github.com/cacr92/WeReply/tree/HEAD/.trae/skills/tauri-development

Skill Files

Browse the full folder contents for tauri-development.

Download Skill

Loading file tree…

.trae/skills/tauri-development/SKILL.md

Skill Metadata

Name
tauri-development
Description
当用户要求Tauri命令、Rust后端/前端联调、tauri-specta类型导出、bindings.ts调用或提到Tauri时使用。

Tauri Development Skill

适用范围

  • 新增/修改 Tauri 命令或 State 服务访问器
  • Rust 后端与前端命令联调、类型导出
  • 需要通过 frontend/src/bindings.ts 调用 Tauri 命令

关键规则(Critical Rules)

  • 每个命令必须同时包含 #[tauri::command]#[specta::specta]
  • 命令入参/出参类型必须实现 specta::Type,必要时加 #[specta(inline)]
  • 统一返回 Result<ApiResponse<T>, String>,使用 api_ok / api_err / with_service
  • 前端只使用 frontend/src/bindings.tscommands,禁止直接 invoke
  • 数据库访问优先 sqlx::query_as! 且显式列名,写操作使用事务

快速模板

命令实现(基于现有服务)

use tauri::State;
use crate::tauri_app::TauriAppState;
use crate::utils::error::{ApiResponse, with_service};

#[tauri::command]
#[specta::specta]
pub async fn get_material_nutritions(
    state: State<'_, TauriAppState>,
    factory_code: String,
    material_code: String,
) -> Result<ApiResponse<Vec<crate::material::material_nutrition::MaterialNutrition>>, String> {
    let material_svc = state.get_material_service().await?;
    with_service(
        Ok(material_svc),
        |svc| async move {
            svc.get_material_nutritions(&material_code, &factory_code).await
        },
        "获取营养指标失败",
    )
    .await
}

DTO 类型

#[derive(serde::Serialize, specta::Type, Clone)]
#[specta(inline)]
pub struct InitializationStatus {
    pub initialized: bool,
    pub message: String,
}

前端调用

import { commands } from '../bindings';
import { message } from 'antd';

export async function loadFactories(keyword: string | null) {
  const res = await commands.getAllFactories(keyword, null);
  if (res.status === 'error') {
    message.error(String(res.error));
    return [] as const;
  }
  const { success, data, error } = res.data;
  if (!success || !data) {
    message.error(error ?? '加载失败');
    return [] as const;
  }
  return data;
}

工作流程

  1. 设计命令签名与 DTO(Rust/TS 字段一致)
  2. 实现命令并返回 ApiResponse
  3. 按项目既有流程更新 frontend/src/bindings.ts
  4. 前端接入 commands 并补齐错误提示

检查清单

  • [ ] 命令含 #[tauri::command]#[specta::specta]
  • [ ] 入参/出参实现 specta::Type
  • [ ] 统一 ApiResponse,错误信息可直达用户
  • [ ] 前端仅使用 commands