Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix: passing auth key to the config

Vanalite3f7ed098c14f
diff --git a/src-tauri/src/core/openclaw/commands.rs b/src-tauri/src/core/openclaw/commands.rs
index d37dc683a..3996196be 100644
--- a/src-tauri/src/core/openclaw/commands.rs
+++ b/src-tauri/src/core/openclaw/commands.rs
@@ -1153,9 +1153,69 @@ pub async fn openclaw_configure(config_input: Option<OpenClawConfigInput>) -> Re
.map_err(|e| format!("Failed to write config file: {}", e))?;
log::info!("OpenClaw configuration written to {:?}", config_path);
+
+ // Ensure the agent directory has auth-profiles.json and that models.json
+ // carries the jan apiKey. Without these, agents fail with
+ // "No API key found for provider".
+ ensure_agent_auth_files()?;
+
Ok(config)
}
+/// Ensure the agent directory has the files OpenClaw's agent system needs:
+/// 1. `auth-profiles.json` — the credential store (empty is fine).
+/// 2. `models.json` — must have `apiKey` on the jan provider so the agent
+/// can resolve credentials via `getCustomProviderApiKey`.
+///
+/// OpenClaw regenerates `models.json` from `openclaw.json` on gateway start,
+/// but if the gateway is already running (hot-reload) the stale file is used.
+/// We patch it directly so the fix takes effect immediately.
+fn ensure_agent_auth_files() -> Result<(), String> {
+ let config_dir = get_openclaw_config_dir()?;
+ let agent_dir = config_dir.join("agents").join("main").join("agent");
+
+ std::fs::create_dir_all(&agent_dir)
+ .map_err(|e| format!("Failed to create agent directory: {}", e))?;
+
+ // 1. auth-profiles.json
+ let auth_path = agent_dir.join("auth-profiles.json");
+ if !auth_path.exists() {
+ let empty_store = serde_json::json!({
+ "version": 1,
+ "profiles": {}
+ });
+ let json = serde_json::to_string_pretty(&empty_store)
+ .map_err(|e| format!("Failed to serialize auth profiles: {}", e))?;
+ std::fs::write(&auth_path, json)
+ .map_err(|e| format!("Failed to write auth-profiles.json: {}", e))?;
+ log::info!("Created empty auth-profiles.json at {:?}", auth_path);
+ }
+
+ // 2. Patch models.json — ensure jan provider has apiKey
+ let models_path = agent_dir.join("models.json");
+ if models_path.exists() {
+ let content = std::fs::read_to_string(&models_path)
+ .map_err(|e| format!("Failed to read models.json: {}", e))?;
+ if let Ok(mut models) = serde_json::from_str::<serde_json::Value>(&content) {
+ let needs_patch = models
+ .pointer("/providers/jan")
+ .map(|jan| jan.get("apiKey").and_then(|v| v.as_str()).unwrap_or("").is_empty())
+ .unwrap_or(false);
+
+ if needs_patch {
+ models["providers"]["jan"]["apiKey"] = serde_json::json!(DEFAULT_JAN_API_KEY);
+ let json = serde_json::to_string_pretty(&models)
+ .map_err(|e| format!("Failed to serialize models.json: {}", e))?;
+ std::fs::write(&models_path, json)
+ .map_err(|e| format!("Failed to write models.json: {}", e))?;
+ log::info!("Patched models.json with jan apiKey");
+ }
+ }
+ }
+
+ Ok(())
+}
+
/// Sanitise an OpenClaw config value before writing it to disk.
/// 1. Strips known-invalid keys (OpenClaw uses strict Zod validation).
/// 2. Ensures required fields are present (e.g., gateway.mode = "local").
@@ -1264,6 +1324,7 @@ pub async fn openclaw_sync_model(
config["models"]["providers"]["jan"] = serde_json::json!({
"baseUrl": current_base_url,
"api": DEFAULT_JAN_API_TYPE,
+ "apiKey": DEFAULT_JAN_API_KEY,
"models": [model_def]
});
} else {
@@ -1346,10 +1407,18 @@ pub async fn openclaw_sync_all_models(
.unwrap_or(DEFAULT_JAN_BASE_URL)
.to_string();
+ // Preserve the current apiKey (may have been customised by the user)
+ let current_api_key = config
+ .pointer("/models/providers/jan/apiKey")
+ .and_then(|v| v.as_str())
+ .unwrap_or(DEFAULT_JAN_API_KEY)
+ .to_string();
+
// Build the full jan provider config with all models as objects
config["models"]["providers"]["jan"] = serde_json::json!({
"baseUrl": current_base_url,
"api": DEFAULT_JAN_API_TYPE,
+ "apiKey": current_api_key,
"models": model_defs
});
@@ -1364,6 +1433,10 @@ pub async fn openclaw_sync_all_models(
write_openclaw_config(&config)?;
+ // Also patch the agent-level models.json so the running gateway picks up
+ // the apiKey immediately (without needing a full restart).
+ ensure_agent_auth_files()?;
+
log::info!("Successfully bulk synced {} models to OpenClaw", synced_count);
Ok(BulkSyncResult {
synced_count,
diff --git a/src-tauri/src/core/openclaw/constants.rs b/src-tauri/src/core/openclaw/constants.rs
index c5f709b45..3c241b38f 100644
--- a/src-tauri/src/core/openclaw/constants.rs
+++ b/src-tauri/src/core/openclaw/constants.rs
@@ -23,5 +23,11 @@ pub const DOCKER_BIND_MODE: &str = "lan";
/// Default API type for Jan
pub const DEFAULT_JAN_API_TYPE: &str = "openai-completions";
+/// Default API key placeholder for the Jan provider.
+/// Jan's local server doesn't require authentication, but OpenClaw's agent
+/// system requires every provider to have a resolvable API key. This
+/// placeholder satisfies that requirement for local models.
+pub const DEFAULT_JAN_API_KEY: &str = "jan-local";
+
/// Default model ID for agents
pub const DEFAULT_MODEL_ID: &str = "llama-3.2-3b";
\ No newline at end of file
diff --git a/src-tauri/src/core/openclaw/models.rs b/src-tauri/src/core/openclaw/models.rs
index b8e7324b2..53614d0d3 100644
--- a/src-tauri/src/core/openclaw/models.rs
+++ b/src-tauri/src/core/openclaw/models.rs
@@ -145,6 +145,11 @@ pub struct JanProviderConfig {
pub base_url: String,
/// API type (openai-completions, etc.)
pub api: String,
+ /// API key for authentication.
+ /// For local models this is a placeholder (Jan doesn't require auth).
+ /// For remote providers routed through Jan, this carries the real key.
+ #[serde(rename = "apiKey")]
+ pub api_key: String,
/// List of model definitions (objects with at least { id, name })
pub models: Vec<ModelDefinition>,
}
@@ -154,6 +159,7 @@ impl Default for JanProviderConfig {
Self {
base_url: constants::DEFAULT_JAN_BASE_URL.to_string(),
api: constants::DEFAULT_JAN_API_TYPE.to_string(),
+ api_key: constants::DEFAULT_JAN_API_KEY.to_string(),
// Start with the pinned default model; openclaw_sync_all_models
// replaces this list with the full catalog from Jan.
models: vec![ModelDefinition {
diff --git a/src-tauri/src/core/openclaw/sandbox_docker.rs b/src-tauri/src/core/openclaw/sandbox_docker.rs
index 8335d5d1f..b84fbe2ec 100644
--- a/src-tauri/src/core/openclaw/sandbox_docker.rs
+++ b/src-tauri/src/core/openclaw/sandbox_docker.rs
@@ -35,12 +35,19 @@ use futures::StreamExt;
#[cfg(feature = "docker")]
use super::sandbox::{IsolationTier, Sandbox, SandboxConfig, SandboxHandle, SandboxStatus};
+#[cfg(feature = "docker")]
+use super::constants::OPENCLAW_VERSION;
#[cfg(feature = "docker")]
const CONTAINER_NAME: &str = "jan-openclaw";
#[cfg(feature = "docker")]
-const IMAGE_NAME: &str = "ghcr.io/openclaw/openclaw:2026.3.1";
+const IMAGE_REPO: &str = "ghcr.io/openclaw/openclaw";
+
+#[cfg(feature = "docker")]
+fn image_name() -> String {
+ format!("{}:{}", IMAGE_REPO, OPENCLAW_VERSION)
+}
#[cfg(feature = "docker")]
pub struct DockerSandbox;
@@ -93,7 +100,7 @@ impl DockerSandbox {
/// Check if the image exists locally (internal).
async fn image_exists(client: &bollard::Docker) -> bool {
- client.inspect_image(IMAGE_NAME).await.is_ok()
+ client.inspect_image(&image_name()).await.is_ok()
}
/// Check if the Docker image is available locally (public).
@@ -143,10 +150,11 @@ impl DockerSandbox {
/// Pull the OpenClaw image (internal, no progress events).
async fn pull_image(client: &bollard::Docker) -> Result<(), String> {
- log::info!("DockerSandbox: pulling image {}", IMAGE_NAME);
+ let img = image_name();
+ log::info!("DockerSandbox: pulling image {}", img);
let options = CreateImageOptions {
- from_image: IMAGE_NAME,
+ from_image: img.as_str(),
..Default::default()
};
@@ -160,7 +168,7 @@ impl DockerSandbox {
}
}
Err(e) => {
- return Err(format!("Failed to pull image {}: {}", IMAGE_NAME, e));
+ return Err(format!("Failed to pull image {}: {}", img, e));
}
}
}
@@ -181,9 +189,11 @@ impl DockerSandbox {
let client = Self::get_client().await?;
+ let img = image_name();
+
// Check if image already exists locally
if Self::image_exists(&client).await {
- log::info!("DockerSandbox: image {} already exists locally", IMAGE_NAME);
+ log::info!("DockerSandbox: image {} already exists locally", img);
let _ = app.emit(
"openclaw-enable-progress",
EnableProgressEvent {
@@ -196,10 +206,10 @@ impl DockerSandbox {
return Ok(());
}
- log::info!("DockerSandbox: pulling image {} with progress", IMAGE_NAME);
+ log::info!("DockerSandbox: pulling image {} with progress", img);
let options = CreateImageOptions {
- from_image: IMAGE_NAME,
+ from_image: img.as_str(),
..Default::default()
};
@@ -252,7 +262,7 @@ impl DockerSandbox {
log::debug!("DockerSandbox: pull: {:?}", info.status);
}
Err(e) => {
- return Err(format!("Failed to pull image {}: {}", IMAGE_NAME, e));
+ return Err(format!("Failed to pull image {}: {}", img, e));
}
}
}
@@ -394,7 +404,7 @@ impl Sandbox for DockerSandbox {
exposed_ports.insert(port_key, HashMap::new());
let container_config = Config {
- image: Some(IMAGE_NAME.to_string()),
+ image: Some(image_name()),
env: Some(env_vars),
exposed_ports: Some(exposed_ports),
host_config: Some(host_config),