Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
build(mlx): build the MLX plugin and extension on macOS only
Akarshan Biswas8f74bb7cc9a9
diff --git a/package.json b/package.jsonindex e6d44a736..8132fa107 100644--- a/package.json+++ b/package.json@@ -39,7 +39,10 @@"build:tauri:linux": "yarn download:bin && yarn build:cli && NO_STRIP=1 ./src-tauri/build-utils/shim-linuxdeploy.sh yarn tauri build && ./src-tauri/build-utils/buildAppImage.sh","build:tauri:darwin": "yarn download:bin && yarn build:mlx-server && yarn build:cli && yarn tauri build --target universal-apple-darwin","build:tauri": "yarn build:icon && yarn copy:assets:tauri && run-script-os",- "build:tauri:plugin:api": "cd src-tauri/plugins && yarn install && yarn workspaces foreach -Apt run build",+ "build:tauri:plugin:api": "cd src-tauri/plugins && yarn install && cd ../.. && run-script-os",+ "build:tauri:plugin:api:darwin": "cd src-tauri/plugins && yarn workspaces foreach -Apt run build",+ "build:tauri:plugin:api:win32": "cd src-tauri/plugins && yarn workspaces foreach -Apt --exclude @janhq/tauri-plugin-mlx-api run build",+ "build:tauri:plugin:api:linux": "cd src-tauri/plugins && yarn workspaces foreach -Apt --exclude @janhq/tauri-plugin-mlx-api run build","build:icon": "tauri icon ./src-tauri/icons/icon.png","build:core": "cd core && yarn build && yarn pack","build:web": "yarn workspace @janhq/web-app build",diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex d88b6fc39..687c4b158 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -33,11 +33,11 @@ default = []hardware = ["dep:tauri-plugin-hardware"]deep-link = ["dep:tauri-plugin-deep-link"]-mlx = ["dep:tauri-plugin-mlx"]+# MLX (tauri-plugin-mlx) is Apple-Silicon-only; it's a macOS-target dependency+# (see [target.'cfg(target_os = "macos")'.dependencies]) rather than a feature.desktop = ["deep-link","hardware",- "mlx",]mobile = ["tauri/protocol-asset",@@ -54,7 +54,6 @@ test-tauri = ["tauri/test","deep-link","hardware",- "mlx",]cli = ["dep:env_logger", "dep:dialoguer", "dep:indicatif", "dep:console"]@@ -95,7 +94,6 @@ zip = "0.6"tauri-plugin-deep-link = { version = "2", optional = true }tauri-plugin-hardware = { path = "./plugins/tauri-plugin-hardware", optional = true }tauri-plugin-llamacpp = { path = "./plugins/tauri-plugin-llamacpp" }-tauri-plugin-mlx = { path = "./plugins/tauri-plugin-mlx", optional = true }tauri-plugin-vector-db = { path = "./plugins/tauri-plugin-vector-db" }tauri-plugin-rag = { path = "./plugins/tauri-plugin-rag" }# `default-features = false` drops the plugin's default `rustls-tls` (bundled@@ -159,6 +157,10 @@ gtk = "0.18"libc = "0.2.172"windows-sys = { version = "0.60.2", features = ["Win32_Storage_FileSystem"] }+# MLX runs only on Apple Silicon, so the plugin is built solely for macOS.+[target.'cfg(target_os = "macos")'.dependencies]+tauri-plugin-mlx = { path = "./plugins/tauri-plugin-mlx" }+[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]reqwest = { version = "0.11", features = ["json", "blocking", "stream", "native-tls"] }tauri-plugin-updater = "2"diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.jsonindex b6a4eba7f..24f119885 100644--- a/src-tauri/capabilities/default.json+++ b/src-tauri/capabilities/default.json@@ -29,7 +29,6 @@"hardware:default","deep-link:default","llamacpp:default",- "mlx:default","updater:default","updater:allow-check",{diff --git a/src-tauri/capabilities/desktop.json b/src-tauri/capabilities/desktop.jsonindex cc49c6687..f23b76b59 100644--- a/src-tauri/capabilities/desktop.json+++ b/src-tauri/capabilities/desktop.json@@ -27,7 +27,6 @@"vector-db:default","rag:default","llamacpp:default",- "mlx:default","deep-link:default","hardware:default",{diff --git a/src-tauri/capabilities/mlx.json b/src-tauri/capabilities/mlx.jsonnew file mode 100644index 000000000..33acdeee4--- /dev/null+++ b/src-tauri/capabilities/mlx.json@@ -0,0 +1,8 @@+{+ "$schema": "../gen/schemas/desktop-schema.json",+ "identifier": "mlx",+ "description": "MLX plugin permissions (Apple Silicon / macOS only)",+ "windows": ["main"],+ "platforms": ["macOS"],+ "permissions": ["mlx:default"]+}diff --git a/src-tauri/src/bin/jan-cli.rs b/src-tauri/src/bin/jan-cli.rsindex df46ce161..a89ba5d0d 100644--- a/src-tauri/src/bin/jan-cli.rs+++ b/src-tauri/src/bin/jan-cli.rs@@ -15,10 +15,13 @@ use indicatif::{ProgressBar, ProgressStyle};use app_lib::core::cli::{cli_delete_thread, cli_get_data_folder, cli_get_thread,cli_list_messages, cli_list_threads, discover_llamacpp_binary,- discover_mlx_binary, download_hf_model, fetch_hf_gguf_files, init_llamacpp_state,- init_mlx_state, list_models, load_mlx_model_impl,- looks_like_hf_repo, resolve_model_by_id, resolve_model_engine, HfFileInfo,- MlxConfig,+ download_hf_model, fetch_hf_gguf_files, init_llamacpp_state,+ list_models, looks_like_hf_repo, resolve_model_engine, HfFileInfo,+};+// MLX is macOS-only; these CLI symbols don't exist on other platforms.+#[cfg(target_os = "macos")]+use app_lib::core::cli::{+ discover_mlx_binary, init_mlx_state, load_mlx_model_impl, resolve_model_by_id, MlxConfig,};use tauri_plugin_llamacpp::router as llamacpp_router;use tauri_plugin_llamacpp::state::LlamacppState;@@ -201,6 +204,7 @@ enum ModelsCommands {args: ServeArgs,},/// Load an MLX model directly (macOS / Apple Silicon only)+ #[cfg(target_os = "macos")]LoadMlx {/// Model ID as shown by `jan models list --engine mlx`#[arg(long)]@@ -364,6 +368,7 @@ async fn handle_models(cmd: ModelsCommands) {ModelsCommands::Load { args } => handle_serve(args).await,+ #[cfg(target_os = "macos")]ModelsCommands::LoadMlx {model_id,model_path,@@ -867,6 +872,14 @@ async fn handle_serve(args: ServeArgs) {let pb = start_progress(verbose, format!("Loading {} ({engine})…", model_id));if engine == "mlx" {+ #[cfg(not(target_os = "macos"))]+ {+ finish_progress(pb, "✗ MLX is only supported on macOS");+ eprintln!("MLX models require macOS / Apple Silicon. Use a llama.cpp (GGUF) model instead.");+ std::process::exit(1);+ }+ #[cfg(target_os = "macos")]+ {use std::path::Path;let bin_path = match bin {@@ -918,6 +931,7 @@ async fn handle_serve(args: ServeArgs) {std::process::exit(1);}}+ }} else {// LlamaCPP pathlet bin_path = match bin {@@ -1314,6 +1328,14 @@ async fn start_model_server(let pb = start_progress(verbose, format!("Loading {} ({engine})…", model_id));if engine == "mlx" {+ #[cfg(not(target_os = "macos"))]+ {+ finish_progress(pb, "✗ MLX is only supported on macOS");+ eprintln!("MLX models require macOS / Apple Silicon. Use a llama.cpp (GGUF) model instead.");+ std::process::exit(1)+ }+ #[cfg(target_os = "macos")]+ {use std::path::Path;let bin_path = match bin.or_else(|| discover_mlx_binary().map(|p| p.to_string_lossy().into_owned())) {Some(p) => p,@@ -1348,6 +1370,7 @@ async fn start_model_server(let url = format!("http://127.0.0.1:{}", info.port);finish_progress(pb, format!("✓ {model_id} ready · {url}"));(info.pid, info.port as u16)+ }} else {let bin_path = match bin.or_else(|| discover_llamacpp_binary().map(|p| p.to_string_lossy().into_owned())) {Some(p) => p,diff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rsindex 6080bede4..d9c750112 100644--- a/src-tauri/src/core/cli/mod.rs+++ b/src-tauri/src/core/cli/mod.rs@@ -14,9 +14,12 @@ use crate::core::threads::{utils::{ensure_data_dirs, get_data_dir, get_thread_dir, get_thread_metadata_path},};use tauri_plugin_llamacpp::state::LlamacppState;+#[cfg(target_os = "macos")]use tauri_plugin_mlx::state::MlxState;+#[cfg(target_os = "macos")]pub use tauri_plugin_mlx::{load_mlx_model_impl, MlxConfig};+#[cfg(target_os = "macos")]pub use tauri_plugin_mlx::state::SessionInfo;// ── State constructors ─────────────────────────────────────────────────────@@ -25,6 +28,7 @@ pub fn init_llamacpp_state() -> LlamacppState {LlamacppState::new()}+#[cfg(target_os = "macos")]pub fn init_mlx_state() -> MlxState {MlxState::new()}@@ -92,38 +96,6 @@ pub fn cli_get_thread(thread_id: &str) -> Result<serde_json::Value, String> {// ── Server operations ──────────────────────────────────────────────────────-/// Start the OpenAI-compatible proxy server. Returns the port it's listening on.-#[allow(clippy::too_many_arguments)]-pub async fn cli_start_server(- app_state: Arc<AppState>,- llama_state: Arc<LlamacppState>,- mlx_state: Arc<MlxState>,- host: String,- port: u16,- prefix: String,- api_key: String,- proxy_timeout: u64,-) -> Result<u16, String> {- proxy::start_server(- app_state.server_handle.clone(),- llama_state.clone(),- mlx_state.mlx_server_process.clone(),- host,- port,- prefix,- api_key,- vec![vec![]],- proxy_timeout,- app_state.provider_configs.clone(),- app_state.mcp_servers.clone(),- app_state.mcp_settings.clone(),- resolve_jan_data_folder().to_string_lossy().into_owned(),- false,- )- .await- .map_err(|e| e.to_string())-}-/// Stop the running proxy server.pub async fn cli_stop_server(app_state: Arc<AppState>) -> Result<(), String> {proxy::stop_server(app_state.server_handle.clone())@@ -333,6 +305,7 @@ pub fn discover_llamacpp_binary() -> Option<PathBuf> {/// Checks standard locations in order:/// 1. `/Applications/Jan.app/Contents/Resources/bin/mlx-server` (installed app)/// 2. Next to the running binary (for dev/custom installs)+#[cfg(target_os = "macos")]pub fn discover_mlx_binary() -> Option<PathBuf> {// 1. Standard macOS app bundle locations (try both path variants)for candidate in &[@@ -673,6 +646,7 @@ mod tests {#[test]fn state_constructors_do_not_panic() {let _ = init_llamacpp_state();+ #[cfg(target_os = "macos")]let _ = init_mlx_state();}diff --git a/src-tauri/src/core/server/commands.rs b/src-tauri/src/core/server/commands.rsindex 232901c3f..6c7d5bf90 100644--- a/src-tauri/src/core/server/commands.rs+++ b/src-tauri/src/core/server/commands.rs@@ -2,7 +2,6 @@ use std::sync::Arc;use tauri::{AppHandle, Manager, Runtime, State};use tauri_plugin_llamacpp::state::LlamacppState;-use tauri_plugin_mlx::state::MlxState;use crate::core::server::proxy;use crate::core::app::commands::get_jan_data_folder_path;@@ -39,8 +38,18 @@ pub async fn start_server<R: Runtime>(let llama_state: State<Arc<LlamacppState>> = app_handle.state();let llama_state_arc = llama_state.inner().clone();- let mlx_state: State<MlxState> = app_handle.state();- let mlx_sessions = mlx_state.mlx_server_process.clone();+ // MLX is macOS-only; elsewhere the session map is permanently empty.+ #[cfg(target_os = "macos")]+ let mlx_sessions = {+ let mlx_state: State<tauri_plugin_mlx::state::MlxState> = app_handle.state();+ mlx_state.mlx_server_process.clone()+ };+ #[cfg(not(target_os = "macos"))]+ let mlx_sessions: Arc<+ tokio::sync::Mutex<+ std::collections::HashMap<i32, crate::core::server::MlxBackendSession>,+ >,+ > = Arc::new(tokio::sync::Mutex::new(std::collections::HashMap::new()));let actual_port = proxy::start_server(server_handle,diff --git a/src-tauri/src/core/server/mod.rs b/src-tauri/src/core/server/mod.rsindex b1c9baf59..53a3318dc 100644--- a/src-tauri/src/core/server/mod.rs+++ b/src-tauri/src/core/server/mod.rs@@ -3,3 +3,29 @@ pub mod proxy;pub mod remote_provider_commands;#[cfg(test)]pub mod tests;++// MLX session types used by the proxy. MLX is macOS-only, so on other platforms+// we expose a field-compatible stub: the session map is always empty there, so+// the proxy's MLX branches are dead but still compile.+#[cfg(target_os = "macos")]+pub use tauri_plugin_mlx::state::{MlxBackendSession, SessionInfo};++#[cfg(not(target_os = "macos"))]+mod mlx_stub {+ #[derive(Debug, Clone)]+ pub struct SessionInfo {+ pub pid: i32,+ pub port: i32,+ pub model_id: String,+ pub model_path: String,+ pub is_embedding: bool,+ pub api_key: String,+ }++ pub struct MlxBackendSession {+ pub info: SessionInfo,+ }+}++#[cfg(not(target_os = "macos"))]+pub use mlx_stub::{MlxBackendSession, SessionInfo};diff --git a/src-tauri/src/core/server/proxy.rs b/src-tauri/src/core/server/proxy.rsindex 5839a12a5..075168b9c 100644--- a/src-tauri/src/core/server/proxy.rs+++ b/src-tauri/src/core/server/proxy.rs@@ -591,7 +591,7 @@ pub fn get_destination_path(original_path: &str, prefix: &str) -> String {remove_prefix(original_path, prefix)}-use tauri_plugin_mlx::state::MlxBackendSession;+use crate::core::server::MlxBackendSession;use rmcp::model::{CallToolRequestParam, CallToolResult};diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex 802349286..85ec8dbe0 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -225,7 +225,7 @@ pub fn run() {app_builder = app_builder.plugin(tauri_plugin_deep_link::init());}- #[cfg(feature = "mlx")]+ #[cfg(target_os = "macos")]{app_builder = app_builder.plugin(tauri_plugin_mlx::init());}@@ -439,7 +439,7 @@ pub fn run() {log::info!("Llama-server router shut down successfully");}- #[cfg(feature = "mlx")]+ #[cfg(target_os = "macos")]{use tauri_plugin_mlx::cleanup_mlx_processes;if let Err(e) = cleanup_mlx_processes(app_handle.clone()).await {