后端代码优化

This commit is contained in:
xintaofei
2026-03-29 18:55:47 +08:00
parent 080a16f26c
commit a7f628ee21
10 changed files with 39 additions and 7 deletions

View File

@@ -8,13 +8,16 @@ use tauri::State;
use crate::acp::binary_cache; use crate::acp::binary_cache;
use crate::acp::error::AcpError; use crate::acp::error::AcpError;
#[cfg(feature = "tauri-runtime")]
use crate::acp::manager::ConnectionManager; use crate::acp::manager::ConnectionManager;
use crate::acp::preflight::{self, PreflightResult}; use crate::acp::preflight::{self, PreflightResult};
use crate::acp::registry; use crate::acp::registry;
use crate::acp::types::{ use crate::acp::types::{
AcpAgentInfo, AgentSkillContent, AgentSkillItem, AgentSkillLayout, AgentSkillLocation, AcpAgentInfo, AgentSkillContent, AgentSkillItem, AgentSkillLayout, AgentSkillLocation,
AgentSkillScope, AgentSkillsListResult, ConnectionInfo, ForkResultInfo, PromptInputBlock, AgentSkillScope, AgentSkillsListResult,
}; };
#[cfg(feature = "tauri-runtime")]
use crate::acp::types::{ConnectionInfo, ForkResultInfo, PromptInputBlock};
use crate::db::service::agent_setting_service; use crate::db::service::agent_setting_service;
use crate::db::AppDatabase; use crate::db::AppDatabase;
use crate::models::agent::AgentType; use crate::models::agent::AgentType;

View File

@@ -1,8 +1,12 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use crate::app_error::AppCommandError; use crate::app_error::AppCommandError;
#[cfg(feature = "tauri-runtime")]
use crate::db::entities::conversation; use crate::db::entities::conversation;
use crate::db::service::{conversation_service, folder_service, import_service}; use crate::db::service::{conversation_service, folder_service};
#[cfg(feature = "tauri-runtime")]
use crate::db::service::import_service;
#[cfg(feature = "tauri-runtime")]
use crate::db::AppDatabase; use crate::db::AppDatabase;
use crate::models::*; use crate::models::*;
use crate::parsers::claude::ClaudeParser; use crate::parsers::claude::ClaudeParser;

View File

@@ -1,10 +1,16 @@
#[cfg(feature = "tauri-runtime")]
use crate::db::error::DbError; use crate::db::error::DbError;
#[cfg(feature = "tauri-runtime")]
use crate::db::service::folder_command_service; use crate::db::service::folder_command_service;
#[cfg(feature = "tauri-runtime")]
use crate::db::AppDatabase; use crate::db::AppDatabase;
#[cfg(feature = "tauri-runtime")]
use crate::models::FolderCommandInfo; use crate::models::FolderCommandInfo;
use std::path::Path; use std::path::Path;
#[cfg(feature = "tauri-runtime")]
use tokio::sync::Mutex; use tokio::sync::Mutex;
#[cfg(feature = "tauri-runtime")]
static BOOTSTRAP_FOLDER_COMMANDS_LOCK: Mutex<()> = Mutex::const_new(()); static BOOTSTRAP_FOLDER_COMMANDS_LOCK: Mutex<()> = Mutex::const_new(());
pub(crate) fn load_package_scripts_as_commands(folder_path: &str) -> Vec<(String, String)> { pub(crate) fn load_package_scripts_as_commands(folder_path: &str) -> Vec<(String, String)> {

View File

@@ -18,10 +18,13 @@ use walkdir::WalkDir;
use tauri::Manager; use tauri::Manager;
use crate::app_error::AppCommandError; use crate::app_error::AppCommandError;
#[cfg(feature = "tauri-runtime")]
use crate::db::error::DbError; use crate::db::error::DbError;
use crate::db::service::folder_service; use crate::db::service::folder_service;
use crate::db::AppDatabase; use crate::db::AppDatabase;
use crate::models::{FolderDetail, FolderHistoryEntry, GitCredentials, OpenedConversation}; use crate::models::GitCredentials;
#[cfg(feature = "tauri-runtime")]
use crate::models::{FolderDetail, FolderHistoryEntry, OpenedConversation};
use crate::web::event_bridge::EventEmitter; use crate::web::event_bridge::EventEmitter;
/// Configure a git command for remote operations: /// Configure a git command for remote operations:

View File

@@ -4,12 +4,15 @@ use tauri::State;
use crate::app_error::AppCommandError; use crate::app_error::AppCommandError;
use crate::db::service::app_metadata_service; use crate::db::service::app_metadata_service;
#[cfg(feature = "tauri-runtime")]
use crate::db::AppDatabase; use crate::db::AppDatabase;
use crate::models::{SystemLanguageSettings, SystemProxySettings}; use crate::models::{SystemLanguageSettings, SystemProxySettings};
#[cfg(feature = "tauri-runtime")]
use crate::network::proxy; use crate::network::proxy;
const SYSTEM_PROXY_SETTINGS_KEY: &str = "system_proxy_settings"; const SYSTEM_PROXY_SETTINGS_KEY: &str = "system_proxy_settings";
const SYSTEM_LANGUAGE_SETTINGS_KEY: &str = "system_language_settings"; const SYSTEM_LANGUAGE_SETTINGS_KEY: &str = "system_language_settings";
#[cfg(feature = "tauri-runtime")]
const LANGUAGE_SETTINGS_UPDATED_EVENT: &str = "app://language-settings-updated"; const LANGUAGE_SETTINGS_UPDATED_EVENT: &str = "app://language-settings-updated";
fn normalize_proxy_settings( fn normalize_proxy_settings(

View File

@@ -6,9 +6,13 @@ use tauri::Manager;
use tauri::State; use tauri::State;
use crate::git_credential; use crate::git_credential;
#[cfg(feature = "tauri-runtime")]
use crate::terminal::error::TerminalError; use crate::terminal::error::TerminalError;
#[cfg(feature = "tauri-runtime")]
use crate::terminal::manager::{SpawnOptions, TerminalManager}; use crate::terminal::manager::{SpawnOptions, TerminalManager};
#[cfg(feature = "tauri-runtime")]
use crate::terminal::types::TerminalInfo; use crate::terminal::types::TerminalInfo;
#[cfg(feature = "tauri-runtime")]
use crate::web::event_bridge::EventEmitter; use crate::web::event_bridge::EventEmitter;
/// Build extra env vars for the terminal session. /// Build extra env vars for the terminal session.

View File

@@ -4,12 +4,15 @@ use tauri::State;
use crate::app_error::AppCommandError; use crate::app_error::AppCommandError;
use crate::db::service::app_metadata_service; use crate::db::service::app_metadata_service;
#[cfg(feature = "tauri-runtime")]
use crate::db::AppDatabase; use crate::db::AppDatabase;
use crate::models::{ use crate::models::GitDetectResult;
GitDetectResult, GitHubAccountsSettings, GitHubTokenValidation, GitSettings, #[cfg(feature = "tauri-runtime")]
}; use crate::models::GitHubAccountsSettings;
use crate::models::{GitHubTokenValidation, GitSettings};
const GIT_SETTINGS_KEY: &str = "git_settings"; const GIT_SETTINGS_KEY: &str = "git_settings";
#[cfg(feature = "tauri-runtime")]
const GITHUB_ACCOUNTS_KEY: &str = "github_accounts"; const GITHUB_ACCOUNTS_KEY: &str = "github_accounts";
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -170,6 +173,7 @@ pub async fn update_git_settings(
// GitHub accounts // GitHub accounts
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
#[cfg(feature = "tauri-runtime")]
async fn load_github_accounts( async fn load_github_accounts(
conn: &sea_orm::DatabaseConnection, conn: &sea_orm::DatabaseConnection,
) -> Result<GitHubAccountsSettings, AppCommandError> { ) -> Result<GitHubAccountsSettings, AppCommandError> {

View File

@@ -1,4 +1,5 @@
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
#[cfg(feature = "tauri-runtime")]
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
@@ -88,6 +89,7 @@ where
/// child processes) can find node/npm/npx without any special handling. /// child processes) can find node/npm/npx without any special handling.
/// ///
/// Call once at startup, after `fix_path_env::fix()`. /// Call once at startup, after `fix_path_env::fix()`.
#[cfg(feature = "tauri-runtime")]
pub fn ensure_node_in_path() { pub fn ensure_node_in_path() {
// Already reachable — nothing to do. // Already reachable — nothing to do.
if which::which("node").is_ok() { if which::which("node").is_ok() {
@@ -107,6 +109,7 @@ pub fn ensure_node_in_path() {
/// Search common Node.js version manager directories for a `node` binary and /// Search common Node.js version manager directories for a `node` binary and
/// return the containing bin directory. /// return the containing bin directory.
#[cfg(feature = "tauri-runtime")]
fn find_node_bin_dir(home: &std::path::Path) -> Option<PathBuf> { fn find_node_bin_dir(home: &std::path::Path) -> Option<PathBuf> {
let mut candidates: Vec<PathBuf> = Vec::new(); let mut candidates: Vec<PathBuf> = Vec::new();
@@ -175,6 +178,7 @@ fn find_node_bin_dir(home: &std::path::Path) -> Option<PathBuf> {
} }
/// Prepend a directory to the process `PATH` environment variable. /// Prepend a directory to the process `PATH` environment variable.
#[cfg(feature = "tauri-runtime")]
fn prepend_to_path(dir: &std::path::Path) { fn prepend_to_path(dir: &std::path::Path) {
let sep = if cfg!(windows) { ";" } else { ":" }; let sep = if cfg!(windows) { ";" } else { ":" };
let current = std::env::var_os("PATH").unwrap_or_default(); let current = std::env::var_os("PATH").unwrap_or_default();

View File

@@ -22,7 +22,7 @@ pub struct StartWebServerParams {
pub async fn start_web_server( pub async fn start_web_server(
Extension(state): Extension<Arc<AppState>>, Extension(state): Extension<Arc<AppState>>,
Json(params): Json<StartWebServerParams>, Json(_params): Json<StartWebServerParams>,
) -> Result<Json<WebServerInfo>, AppCommandError> { ) -> Result<Json<WebServerInfo>, AppCommandError> {
// In web mode, the server is already running (this handler itself is served by it). // In web mode, the server is already running (this handler itself is served by it).
// This endpoint is mainly useful in Tauri mode. Return current status as a noop. // This endpoint is mainly useful in Tauri mode. Return current status as a noop.

View File

@@ -122,6 +122,7 @@ pub fn get_local_addresses(port: u16) -> Vec<String> {
// ── Core logic (shared by Tauri commands and web handlers) ── // ── Core logic (shared by Tauri commands and web handlers) ──
#[allow(dead_code)]
pub(crate) async fn do_start_web_server_with_state( pub(crate) async fn do_start_web_server_with_state(
app_state: Arc<AppState>, app_state: Arc<AppState>,
static_dir: PathBuf, static_dir: PathBuf,