Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

4.0 KB
Raw Blame
use crate::error::AppError;
use indexmap::IndexMap;
use std::str::FromStr;

use tauri::State;

use crate::app_config::AppType;
use crate::prompt::Prompt;
use crate::services::pi_prompt_files::{
    PiPromptFileKind, PiPromptFileService, PiPromptFileSnapshot, PiPromptTemplate,
    PiPromptTemplateService,
};
use crate::services::prompt::PromptService;
use crate::store::AppState;

#[tauri::command]
pub async fn get_prompts(
    app: String,
    state: State<'_, AppState>,
) -> Result<IndexMap<String, Prompt>, AppError> {
    let app_type = AppType::from_str(&app)?;
    PromptService::get_prompts(&state, app_type)
}

#[tauri::command]
pub async fn upsert_prompt(
    app: String,
    id: String,
    prompt: Prompt,
    state: State<'_, AppState>,
) -> Result<(), AppError> {
    let app_type = AppType::from_str(&app)?;
    PromptService::upsert_prompt(&state, app_type, &id, prompt)
}

#[tauri::command]
pub async fn delete_prompt(
    app: String,
    id: String,
    state: State<'_, AppState>,
) -> Result<(), AppError> {
    let app_type = AppType::from_str(&app)?;
    PromptService::delete_prompt(&state, app_type, &id)
}

#[tauri::command]
pub async fn enable_prompt(
    app: String,
    id: String,
    state: State<'_, AppState>,
) -> Result<(), AppError> {
    let app_type = AppType::from_str(&app)?;
    PromptService::enable_prompt(&state, app_type, &id)
}

#[tauri::command]
pub async fn import_prompt_from_file(
    app: String,
    state: State<'_, AppState>,
) -> Result<String, AppError> {
    let app_type = AppType::from_str(&app)?;
    PromptService::import_from_file(&state, app_type)
}

#[tauri::command]
pub async fn get_current_prompt_file_content(app: String) -> Result<Option<String>, AppError> {
    let app_type = AppType::from_str(&app)?;
    PromptService::get_current_file_content(app_type)
}

/// 提示词目标文件的位置:`path` 是完整路径(复制用),`displayPath` 把主目录写成 `~`(显示用)。
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PromptFileLocation {
    pub path: String,
    pub display_path: String,
}

#[tauri::command]
pub async fn get_prompt_file_location(app: String) -> Result<PromptFileLocation, String> {
    let app_type = AppType::from_str(&app)?;
    let path = crate::prompt_files::prompt_file_path(&app_type)?;
    Ok(PromptFileLocation {
        path: path.to_string_lossy().to_string(),
        display_path: crate::prompt_files::display_path(&path),
    })
}

#[tauri::command]
pub async fn get_pi_prompt_file(kind: PiPromptFileKind) -> Result<PiPromptFileSnapshot, String> {
    PiPromptFileService::read(kind).map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn replace_pi_prompt_file(
    kind: PiPromptFileKind,
    #[allow(non_snake_case)] expectedRevision: String,
    content: String,
) -> Result<PiPromptFileSnapshot, String> {
    PiPromptFileService::replace(kind, &expectedRevision, &content)
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn delete_pi_prompt_file(
    kind: PiPromptFileKind,
    #[allow(non_snake_case)] expectedRevision: String,
) -> Result<bool, String> {
    PiPromptFileService::delete(kind, &expectedRevision).map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn list_pi_prompt_templates() -> Result<Vec<PiPromptTemplate>, String> {
    PiPromptTemplateService::list().map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn upsert_pi_prompt_template(
    slug: String,
    #[allow(non_snake_case)] originalSlug: Option<String>,
    #[allow(non_snake_case)] expectedRevision: String,
    content: String,
) -> Result<PiPromptTemplate, String> {
    PiPromptTemplateService::upsert(&slug, originalSlug.as_deref(), &expectedRevision, &content)
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn delete_pi_prompt_template(
    slug: String,
    #[allow(non_snake_case)] expectedRevision: String,
) -> Result<bool, String> {
    PiPromptTemplateService::delete(&slug, &expectedRevision).map_err(|error| error.to_string())
}