Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

feat: ERNIE-Image, Codex streaming, path fix, mobile native tools, continue capability

PurpleDoubleD2c0c224b4323
diff --git a/src-tauri/src/commands/agent.rs b/src-tauri/src/commands/agent.rs
index cc324f05..903cee2e 100644
--- a/src-tauri/src/commands/agent.rs
+++ b/src-tauri/src/commands/agent.rs
@@ -13,16 +13,127 @@ use crate::state::AppState;
#[cfg(target_os = "windows")]
const CREATE_NO_WINDOW: u32 = 0x08000000;
-fn agent_workspace() -> PathBuf {
+/// Base directory for all agent workspaces. Per-chat subfolders are
+/// created lazily by `agent_workspace(chat_id)` on the first write.
+fn agent_workspace_root() -> PathBuf {
dirs::home_dir().unwrap_or_default().join("agent-workspace")
}
-fn resolve_agent_path(path: &str) -> PathBuf {
- let p = std::path::Path::new(path);
+/// Per-chat workspace directory. Each LU chat / Remote chat / Codex chat
+/// gets its own isolated subfolder so writes from one agent don't clobber
+/// another's files. If `chat_id` is None (legacy callers, CLI, etc.),
+/// we fall back to `agent-workspace/default/` so nobody pollutes the
+/// top-level folder with orphan files.
+///
+/// `chat_id` is sanitised to prevent path traversal — anything outside
+/// `[A-Za-z0-9_\-\.]` is replaced with `_` and the string is capped at
+/// 64 chars. The original id is kept in the chat UI; only the filesystem
+/// form is sanitised.
+fn agent_workspace(chat_id: Option<&str>) -> PathBuf {
+ let root = agent_workspace_root();
+ let id = chat_id.unwrap_or("default");
+ let safe: String = id
+ .chars()
+ .take(64)
+ .map(|c| if c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '.' { c } else { '_' })
+ .collect();
+ let slug = if safe.is_empty() { "default".to_string() } else { safe };
+ root.join(slug)
+}
+
+/// Defensive normalization that strips duplicate drive-letter prefixes.
+///
+/// The caller (desktop useCodex.ts or the model itself) can end up with paths
+/// like `D:/Pictures/foo/D:/Pictures/foo/index.html` when:
+/// 1. `useCodex.ts` used to only treat `C:` as absolute and prepended workDir
+/// in front of any `D:/…` path (now fixed there, but belt-and-suspenders).
+/// 2. The model hallucinated a doubled prefix after seeing an earlier error.
+///
+/// If the path contains more than one drive-letter `X:/` or `X:\` pattern, we
+/// keep only the substring starting at the LAST one. A single drive prefix at
+/// the start is untouched.
+fn normalize_duplicate_drive_prefix(path: &str) -> String {
+ let bytes = path.as_bytes();
+ if bytes.len() < 3 { return path.to_string(); }
+ let mut last_drive_idx: Option<usize> = None;
+ let mut i = 1;
+ while i + 1 < bytes.len() {
+ if bytes[i] == b':'
+ && bytes[i - 1].is_ascii_alphabetic()
+ && (bytes[i + 1] == b'/' || bytes[i + 1] == b'\\')
+ {
+ last_drive_idx = Some(i - 1);
+ }
+ i += 1;
+ }
+ match last_drive_idx {
+ Some(idx) if idx > 0 => path[idx..].to_string(),
+ _ => path.to_string(),
+ }
+}
+
+fn resolve_agent_path(path: &str, chat_id: Option<&str>) -> PathBuf {
+ let cleaned = normalize_duplicate_drive_prefix(path);
+ let p = std::path::Path::new(&cleaned);
if p.is_absolute() {
p.to_path_buf()
} else {
- agent_workspace().join(path)
+ agent_workspace(chat_id).join(&cleaned)
+ }
+}
+
+#[cfg(test)]
+mod path_tests {
+ use super::normalize_duplicate_drive_prefix as n;
+
+ #[test]
+ fn single_drive_prefix_untouched() {
+ assert_eq!(n("C:/foo/bar.txt"), "C:/foo/bar.txt");
+ assert_eq!(n("D:\\foo\\bar.txt"), "D:\\foo\\bar.txt");
+ }
+
+ #[test]
+ fn duplicate_drive_prefix_trimmed() {
+ assert_eq!(
+ n("D:/Pictures/foo/D:/Pictures/foo/index.html"),
+ "D:/Pictures/foo/index.html"
+ );
+ assert_eq!(n("D:\\x\\D:\\x\\y.txt"), "D:\\x\\y.txt");
+ }
+
+ #[test]
+ fn triple_drive_prefix_trimmed_to_last() {
+ assert_eq!(n("D:/a/D:/a/D:/a/file.html"), "D:/a/file.html");
+ }
+
+ #[test]
+ fn different_drives_trims_to_last() {
+ assert_eq!(n("C:/temp/D:/real/x.txt"), "D:/real/x.txt");
+ }
+
+ #[test]
+ fn relative_path_untouched() {
+ assert_eq!(n("./foo.txt"), "./foo.txt");
+ assert_eq!(n("foo/bar.txt"), "foo/bar.txt");
+ }
+
+ #[test]
+ fn unix_absolute_untouched() {
+ assert_eq!(n("/etc/passwd"), "/etc/passwd");
+ assert_eq!(n("/home/user/x.txt"), "/home/user/x.txt");
+ }
+
+ #[test]
+ fn short_path_untouched() {
+ assert_eq!(n(""), "");
+ assert_eq!(n("a"), "a");
+ assert_eq!(n("ab"), "ab");
+ }
+
+ #[test]
+ fn path_that_looks_like_drive_but_is_not() {
+ assert_eq!(n("label:value"), "label:value");
+ assert_eq!(n("key:val/x"), "key:val/x");
}
}
@@ -30,6 +141,7 @@ fn resolve_agent_path(path: &str) -> PathBuf {
pub fn execute_code(
code: String,
timeout: Option<u64>,
+ #[allow(non_snake_case)] chatId: Option<String>,
state: State<'_, AppState>,
) -> Result<serde_json::Value, String> {
let timeout_ms = timeout.unwrap_or(30000);
@@ -41,7 +153,10 @@ pub fn execute_code(
fs::write(&script_path, &code)
.map_err(|e| format!("Write temp script: {}", e))?;
- let workspace = agent_workspace();
+ // cwd = per-chat workspace (auto-created). Python scripts that do
+ // relative file I/O land in the same isolated folder as file_read /
+ // file_write for this chat.
+ let workspace = agent_workspace(chatId.as_deref());
let _ = fs::create_dir_all(&workspace);
let mut cmd = Command::new(&state.python_bin);
@@ -101,8 +216,9 @@ pub fn execute_code(
}
#[tauri::command]
-pub fn file_read(path: String) -> Result<serde_json::Value, String> {
- let full_path = resolve_agent_path(&path);
+#[allow(non_snake_case)]
+pub fn file_read(path: String, chatId: Option<String>) -> Result<serde_json::Value, String> {
+ let full_path = resolve_agent_path(&path, chatId.as_deref());
if !full_path.exists() {
return Err(format!("File not found: {}", full_path.display()));
}
@@ -112,8 +228,9 @@ pub fn file_read(path: String) -> Result<serde_json::Value, String> {
}
#[tauri::command]
-pub fn file_write(path: String, content: String) -> Result<serde_json::Value, String> {
- let full_path = resolve_agent_path(&path);
+#[allow(non_snake_case)]
+pub fn file_write(path: String, content: String, chatId: Option<String>) -> Result<serde_json::Value, String> {
+ let full_path = resolve_agent_path(&path, chatId.as_deref());
if let Some(parent) = full_path.parent() {
fs::create_dir_all(parent).map_err(|e| format!("Create dir: {}", e))?;
}
diff --git a/src-tauri/src/commands/filesystem.rs b/src-tauri/src/commands/filesystem.rs
index eda10469..7b2ae39e 100644
--- a/src-tauri/src/commands/filesystem.rs
+++ b/src-tauri/src/commands/filesystem.rs
@@ -7,16 +7,52 @@ use glob::glob as glob_match;
use regex::Regex;
use walkdir::WalkDir;
-/// Resolve path — supports absolute and relative (relative to home/agent-workspace)
-fn resolve_path(path: &str) -> PathBuf {
- let p = Path::new(path);
+/// Strip duplicate drive-letter prefixes, e.g.
+/// `D:/a/D:/a/file.txt` → `D:/a/file.txt`. See commands/agent.rs for
+/// the full rationale — same bug surface for fs_list / fs_search.
+fn normalize_duplicate_drive_prefix(path: &str) -> String {
+ let bytes = path.as_bytes();
+ if bytes.len() < 3 { return path.to_string(); }
+ let mut last_drive_idx: Option<usize> = None;
+ let mut i = 1;
+ while i + 1 < bytes.len() {
+ if bytes[i] == b':'
+ && bytes[i - 1].is_ascii_alphabetic()
+ && (bytes[i + 1] == b'/' || bytes[i + 1] == b'\\')
+ {
+ last_drive_idx = Some(i - 1);
+ }
+ i += 1;
+ }
+ match last_drive_idx {
+ Some(idx) if idx > 0 => path[idx..].to_string(),
+ _ => path.to_string(),
+ }
+}
+
+/// Resolve path — supports absolute and relative (relative to the
+/// per-chat workspace `~/agent-workspace/<chat_id>/`). Mirrors the
+/// pattern in agent.rs. `chat_id` is sanitised to the charset
+/// `[A-Za-z0-9_\-\.]` (anything else → `_`) and capped at 64 chars to
+/// prevent path traversal. Missing chat_id falls back to `default`.
+fn resolve_path(path: &str, chat_id: Option<&str>) -> PathBuf {
+ let cleaned = normalize_duplicate_drive_prefix(path);
+ let p = Path::new(&cleaned);
if p.is_absolute() {
p.to_path_buf()
} else {
+ let id = chat_id.unwrap_or("default");
+ let safe: String = id
+ .chars()
+ .take(64)
+ .map(|c| if c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '.' { c } else { '_' })
+ .collect();
+ let slug = if safe.is_empty() { "default".to_string() } else { safe };
dirs::home_dir()
.unwrap_or_default()
.join("agent-workspace")
- .join(path)
+ .join(slug)
+ .join(&cleaned)
}
}
@@ -44,8 +80,9 @@ fn file_meta(path: &Path) -> serde_json::Value {
}
#[tauri::command]
-pub fn fs_read(path: String) -> Result<serde_json::Value, String> {
- let full = resolve_path(&path);
+#[allow(non_snake_case)]
+pub fn fs_read(path: String, chatId: Option<String>) -> Result<serde_json::Value, String> {
+ let full = resolve_path(&path, chatId.as_deref());
if !full.exists() {
return Err(format!("File not found: {}", full.display()));
}
@@ -62,8 +99,9 @@ pub fn fs_read(path: String) -> Result<serde_json::Value, String> {
}
#[tauri::command]
-pub fn fs_write(path: String, content: String) -> Result<serde_json::Value, String> {
- let full = resolve_path(&path);
+#[allow(non_snake_case)]
+pub fn fs_write(path: String, content: String, chatId: Option<String>) -> Result<serde_json::Value, String> {
+ let full = resolve_path(&path, chatId.as_deref());
if let Some(parent) = full.parent() {
fs::create_dir_all(parent).map_err(|e| format!("Create dir: {}", e))?;
}
@@ -72,12 +110,14 @@ pub fn fs_write(path: String, content: String) -> Result<serde_json::Value, Stri
}
#[tauri::command]
+#[allow(non_snake_case)]
pub fn fs_list(
path: String,
recursive: Option<bool>,
pattern: Option<String>,
+ chatId: Option<String>,
) -> Result<serde_json::Value, String> {
- let dir = resolve_path(&path);
+ let dir = resolve_path(&path, chatId.as_deref());
if !dir.is_dir() {
return Err(format!("Not a directory: {}", dir.display()));
}
@@ -117,12 +157,14 @@ pub fn fs_list(
}
#[tauri::command]
+#[allow(non_snake_case)]
pub fn fs_search(
path: String,
pattern: String,
max_results: Option<u32>,
+ chatId: Option<String>,
) -> Result<serde_json::Value, String> {
- let dir = resolve_path(&path);
+ let dir = resolve_path(&path, chatId.as_deref());
if !dir.is_dir() {
return Err(format!("Not a directory: {}", dir.display()));
}
@@ -174,8 +216,9 @@ pub fn fs_search(
}
#[tauri::command]
-pub fn fs_info(path: String) -> Result<serde_json::Value, String> {
- let full = resolve_path(&path);
+#[allow(non_snake_case)]
+pub fn fs_info(path: String, chatId: Option<String>) -> Result<serde_json::Value, String> {
+ let full = resolve_path(&path, chatId.as_deref());
if !full.exists() {
return Err(format!("Path not found: {}", full.display()));
}
@@ -236,3 +279,46 @@ pub async fn save_text_file_dialog(
None => Ok(None),
}
}
+
+/// Binary counterpart to `save_text_file_dialog`. Used by the Download
+/// buttons in the Create view's Gallery / OutputDisplay / MediaViewer.
+///
+/// Why a dedicated Rust command instead of the JS `<a download>` trick?
+/// In Tauri's Webview2 the blob-URL anchor-click pattern is unreliable
+/// — most of the time the webview simply navigates to the blob URL
+/// instead of saving it, so the user saw "nothing happens". Going
+/// through a native Save As dialog guarantees the bytes hit the disk.
+///
+/// `bytes` is expected as a raw number[] over the Tauri IPC (the JS
+/// side passes `Array.from(new Uint8Array(blob))`). Returns the chosen
+/// path, or null when the user cancelled.
+#[tauri::command]
+#[allow(non_snake_case)]
+pub async fn save_binary_file_dialog(
+ bytes: Vec<u8>,
+ defaultName: Option<String>,
+ extension: Option<String>,
+ ext_label: Option<String>,
+) -> Result<Option<String>, String> {
+ let default_name = defaultName.unwrap_or_else(|| "download.bin".to_string());
+ let ext = extension.unwrap_or_else(|| {
+ // Infer from defaultName if caller didn't tell us — cheap split.
+ default_name.rsplit_once('.').map(|(_, e)| e.to_string()).unwrap_or_else(|| "bin".to_string())
+ });
+ let label = ext_label.unwrap_or_else(|| format!("{} file", ext.to_uppercase()));
+
+ let file = rfd::AsyncFileDialog::new()
+ .set_file_name(&default_name)
+ .add_filter(&label, &[ext.as_str()])
+ .save_file()
+ .await;
+
+ match file {
+ Some(handle) => {
+ let path = handle.path().to_path_buf();
+ std::fs::write(&path, &bytes).map_err(|e| format!("Write failed: {}", e))?;
+ Ok(Some(path.to_string_lossy().into_owned()))
+ }
+ None => Ok(None),
+ }
+}
diff --git a/src-tauri/src/commands/remote.rs b/src-tauri/src/commands/remote.rs
index 94610d3d..d3b872f0 100644
--- a/src-tauri/src/commands/remote.rs
+++ b/src-tauri/src/commands/remote.rs
@@ -41,9 +41,9 @@ pub struct RemotePermissions {
impl Default for RemotePermissions {
fn default() -> Self {
Self {
- filesystem: false,
- downloads: false,
- process_control: false,
+ filesystem: true,
+ downloads: true,
+ process_control: true,
}
}
}
@@ -374,6 +374,11 @@ struct AgentToolPayload {
tool: String,
#[serde(default)]
args: serde_json::Value,
+ /// Per-chat workspace slug. When provided, all file tools resolve
+ /// relative paths against `~/agent-workspace/<chat_id>/`. Missing /
+ /// empty falls back to `default` so legacy clients still work.
+ #[serde(default, rename = "chatId", alias = "chat_id")]
+ chat_id: Option<String>,
}
/// Run a single agent tool on behalf of an authenticated mobile client.
@@ -383,41 +388,67 @@ struct AgentToolPayload {
/// - code_execute → requires `filesystem`
/// - image_generate → requires `process_control`
/// - web_search → no permission required
+///
+/// Bug fix (mobile agent HTTP 500): all tool failures (missing arg,
+/// permission denied, underlying tool error) are returned as HTTP 200
+/// with `{ "error": "<msg>" }`. The mobile JS treats a 200-with-error
+/// as a normal observation that the model can read and recover from,
+/// instead of bubbling a scary HTTP 500 back up to the chat. The Rust
+/// server still logs the failure to stderr so devs can diagnose.
async fn handle_agent_tool(
AxumState(state): AxumState<RemoteState>,
Json(body): Json<AgentToolPayload>,
) -> Response {
use tauri::Manager;
+ let tool_name = body.tool.clone();
+
let app_state = match state.app_handle.try_state::<crate::state::AppState>() {
Some(s) => s,
- None => return (StatusCode::INTERNAL_SERVER_ERROR, "AppState unavailable").into_response(),
+ None => {
+ eprintln!("[Remote agent] AppState not registered — cannot dispatch tool {}", tool_name);
+ return graceful_error("AppState unavailable on the desktop side.");
+ }
};
let perms = state.permissions.lock().await.clone();
- let result: Result<serde_json::Value, String> = match body.tool.as_str() {
+ // Permission gate up-front. Returns a graceful 200 + {error,permission}
+ // so the mobile UI can render a single-line hint instead of "HTTP 403".
+ let needs = match tool_name.as_str() {
+ "file_read" | "file_write" | "code_execute" | "file_list" | "file_search" | "shell_execute" | "screenshot"
+ => Some(("filesystem", perms.filesystem)),
+ "image_generate"
+ => Some(("process_control", perms.process_control)),
+ _ => None,
+ };
+ if let Some((perm, on)) = needs {
+ if !on {
+ return graceful_perm_error(&tool_name, perm);
+ }
+ }
+
+ // Per-chat workspace slug — threaded into every file tool so each
+ // mobile chat gets its own isolated `~/agent-workspace/<slug>/` and
+ // agents running in different chats can't clobber each other.
+ let chat_id = body.chat_id.clone();
+
+ let result: Result<serde_json::Value, String> = match tool_name.as_str() {
"file_read" => {
- if !perms.filesystem {
- return forbidden("filesystem permission disabled for remote clients");
- }
let path = body.args.get("path").and_then(|v| v.as_str()).unwrap_or("").to_string();
- crate::commands::agent::file_read(path)
+ if path.is_empty() { Err("file_read needs a non-empty `path` argument.".into()) }
+ else { crate::commands::agent::file_read(path, chat_id.clone()) }
}
"file_write" => {
- if !perms.filesystem {
- return forbidden("filesystem permission disabled for remote clients");
- }
let path = body.args.get("path").and_then(|v| v.as_str()).unwrap_or("").to_string();
let content = body.args.get("content").and_then(|v| v.as_str()).unwrap_or("").to_string();
- crate::commands::agent::file_write(path, content)
+ if path.is_empty() { Err("file_write needs a non-empty `path` argument.".into()) }
+ else { crate::commands::agent::file_write(path, content, chat_id.clone()) }
}
"code_execute" => {
- if !perms.filesystem {
- return forbidden("filesystem permission disabled for remote clients");
- }
let code = body.args.get("code").and_then(|v| v.as_str()).unwrap_or("").to_string();
let timeout = body.args.get("timeout").and_then(|v| v.as_u64());
- crate::commands::agent::execute_code(code, timeout, app_state)
+ if code.is_empty() { Err("code_execute needs a non-empty `code` argument.".into()) }
+ else { crate::commands::agent::execute_code(code, timeout, chat_id.clone(), app_state) }
}
"web_search" => {
let query = body.args.get("query").and_then(|v| v.as_str()).unwrap_or("").to_string();
@@ -425,80 +456,101 @@ async fn handle_agent_tool(
.or_else(|| body.args.get("count"))
.and_then(|v| v.as_u64())
.map(|n| n as usize);
- crate::commands::search::web_search(query, count, app_state).await
+ if query.is_empty() { Err("web_search needs a non-empty `query` argument.".into()) }
+ else { crate::commands::search::web_search(query, count, app_state).await }
}
"web_fetch" => {
let url = body.args.get("url").and_then(|v| v.as_str()).unwrap_or("").to_string();
- if url.is_empty() {
- return (StatusCode::BAD_REQUEST, "web_fetch requires a `url` argument").into_response();
- }
- crate::commands::search::web_fetch(url).await
+ if url.is_empty() { Err("web_fetch needs a non-empty `url` argument.".into()) }
+ else { crate::commands::search::web_fetch(url).await }
}
"file_list" => {
- if !perms.filesystem {
- return forbidden("filesystem permission disabled for remote clients");
- }
let path = body.args.get("path").and_then(|v| v.as_str()).unwrap_or("").to_string();
let recursive = body.args.get("recursive").and_then(|v| v.as_bool());
let pattern = body.args.get("pattern").and_then(|v| v.as_str()).map(String::from);
- crate::commands::filesystem::fs_list(path, recursive, pattern)
+ if path.is_empty() { Err("file_list needs a non-empty `path` argument.".into()) }
+ else { crate::commands::filesystem::fs_list(path, recursive, pattern, chat_id.clone()) }
}
"file_search" => {
- if !perms.filesystem {
- return forbidden("filesystem permission disabled for remote clients");
- }
let path = body.args.get("path").and_then(|v| v.as_str()).unwrap_or("").to_string();
let pattern = body.args.get("query")
.or_else(|| body.args.get("pattern"))
.and_then(|v| v.as_str()).unwrap_or("").to_string();
- let max = body.args.get("max_results").and_then(|v| v.as_u64()).map(|n| n as u32);
- crate::commands::filesystem::fs_search(path, pattern, max)
+ // Bug: AGENT_TOOLS sends `maxResults` (camelCase); also accept
+ // snake-case for older clients.
+ let max = body.args.get("maxResults")
+ .or_else(|| body.args.get("max_results"))
+ .and_then(|v| v.as_u64())
+ .map(|n| n as u32);
+ if path.is_empty() || pattern.is_empty() {
+ Err("file_search needs both `path` and `pattern` arguments.".into())
+ } else {
+ crate::commands::filesystem::fs_search(path, pattern, max, chat_id.clone())
+ }
}
"shell_execute" => {
- if !perms.filesystem {
- return forbidden("filesystem permission disabled for remote clients");
- }
let command = body.args.get("command").and_then(|v| v.as_str()).unwrap_or("").to_string();
- if command.is_empty() {
- return (StatusCode::BAD_REQUEST, "shell_execute requires a `command` argument").into_response();
- }
- let cwd = body.args.get("cwd").and_then(|v| v.as_str()).map(String::from);
- let timeout = body.args.get("timeout").and_then(|v| v.as_u64());
- let shell = body.args.get("shell").and_then(|v| v.as_str()).map(String::from);
- crate::commands::shell::shell_execute(command, None, cwd, timeout, shell).await
- }
- "system_info" => {
- crate::commands::system::system_info()
- }
- "process_list" => {
- crate::commands::system::process_list()
- }
- "screenshot" => {
- if !perms.filesystem {
- return forbidden("filesystem permission disabled (screenshot reads your desktop)");
+ if command.is_empty() { Err("shell_execute needs a non-empty `command` argument.".into()) }
+ else {
+ let cwd = body.args.get("cwd").and_then(|v| v.as_str()).map(String::from);
+ let timeout = body.args.get("timeout").and_then(|v| v.as_u64());
+ let shell = body.args.get("shell").and_then(|v| v.as_str()).map(String::from);
+ crate::commands::shell::shell_execute(command, None, cwd, timeout, shell, chat_id.clone()).await
}
- crate::commands::system::screenshot()
- }
- "get_current_time" => {
- crate::commands::system::get_current_time()
}
+ "system_info" => crate::commands::system::system_info(),
+ "process_list" => crate::commands::system::process_list(),
+ "screenshot" => crate::commands::system::screenshot(),
+ "get_current_time" => crate::commands::system::get_current_time(),
"image_generate" => {
- if !perms.process_control {
- return forbidden("ComfyUI access disabled (enable Process Control)");
- }
// Image generation requires the desktop Agent path — too much
- // plumbing for the remote bridge. Fall back with a clear message.
- Err("image_generate is desktop-only for now. Use the Create tab.".into())
+ // plumbing for the remote bridge. Return a clean structured
+ // observation rather than HTTP 500 so the mobile UI shows it
+ // as a single line, not a red HTTP error.
+ Ok(serde_json::json!({
+ "error": "image_generate is only available on the desktop app for now. Open the Create tab there."
+ }))
}
other => Err(format!("Unknown tool: {}", other)),
};
match result {
Ok(v) => Json(v).into_response(),
- Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(),
+ Err(e) => {
+ // Log so a dev tailing the desktop console sees what failed.
+ // The mobile gets a graceful 200+{error} so the agent loop
+ // observation reads cleanly ("Error: file not found …").
+ eprintln!("[Remote agent] tool `{}` failed: {}", tool_name, e);
+ graceful_error(&e)
+ }
}
}
+/// Wrap a tool failure as a 200-OK JSON payload so the mobile parser
+/// doesn't treat it as an HTTP transport error. Mobile JS reads
+/// `{error}` and surfaces it as a normal observation.
+fn graceful_error(msg: &str) -> Response {
+ let body = serde_json::json!({ "error": msg });
+ Json(body).into_response()
+}
+
+/// Permission-denied responder: still 200, but flagged so the mobile UI
+/// can render a 1-tap "Open Settings → Permissions" hint instead of a
+/// generic error.
+fn graceful_perm_error(tool: &str, permission: &str) -> Response {
+ let msg = format!(
+ "Tool `{}` is gated behind the `{}` permission. Open Settings (gear icon) and toggle it on.",
+ tool, permission
+ );
+ eprintln!("[Remote agent] tool `{}` blocked: missing permission `{}`", tool, permission);
+ let body = serde_json::json!({
+ "error": msg,
+ "permission": permission,
+ "needs_permission": true,
+ });
+ Json(body).into_response()
+}
+
// ─── Mobile chat event (mirror messages to desktop) ───
/// Cap on chat-event content to prevent an authenticated mobile from DoS'ing
@@ -783,7 +835,15 @@ async fn mobile_landing() -> Html<String> {
--radius:2px;--radius-md:6px;--radius-lg:10px;
--safe-top:env(safe-area-inset-top,0px);--safe-bottom:env(safe-area-inset-bottom,0px);
}
-html,body{height:100%;overflow:hidden}
+/* Viewport units: `dvh` (dynamic viewport height) tracks iOS / Android
+ keyboard open/close automatically. `svh` stays at the small state
+ (keyboard open) permanently as a fallback if dvh isn't supported.
+ Without this the old `height:100%` left the keyboard pushing the
+ whole chat up past the top edge. */
+html,body{height:100dvh;overflow:hidden}
+@supports not (height: 100dvh){
+ html,body{height:100vh}
+}
body{font-family:system-ui,-apple-system,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;background:var(--surface);color:var(--text-primary);display:flex;flex-direction:column}
#app{flex:1;display:flex;flex-direction:column;min-height:0;overflow:hidden;position:relative}
/* Bug #5: inline SVG icons in place of Material Symbols font. The span
@@ -994,6 +1054,13 @@ button{-webkit-appearance:none;appearance:none}
.agent-step-summary{font-size:0.66rem;color:rgba(255,255,255,0.55);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;flex:2;min-width:0}
.agent-step-chev{font-size:15px;transition:transform 0.2s;color:var(--text-tertiary);flex-shrink:0}
.agent-step.open .agent-step-chev{transform:rotate(180deg)}
+/* Live-running step: subtle pulsing accent stripe on the left edge so the
+ user can see that this step is still in flight (model thinking, tool
+ running, etc.). Disappears the moment the step finalises. */
+.agent-step.live{position:relative}
+.agent-step.live::before{content:'';position:absolute;left:0;top:0;bottom:0;width:2px;background:linear-gradient(180deg,var(--accent),transparent);animation:agent-live-pulse 1.2s ease-in-out infinite}
+@keyframes agent-live-pulse{0%,100%{opacity:0.35}50%{opacity:1}}
+.agent-step.live .agent-step-icon{animation:agent-live-pulse 1.2s ease-in-out infinite}
.agent-step-content{display:none;padding:4px 12px 10px;line-height:1.5;white-space:pre-wrap;word-wrap:break-word;overflow-wrap:anywhere;font-size:0.7rem;border-top:1px solid rgba(255,255,255,0.04)}
.agent-step.open .agent-step-content{display:block}
.agent-step-content code{background:rgba(0,0,0,0.35);padding:1px 4px;border-radius:var(--radius);font-size:0.66rem;font-family:ui-monospace,Menlo,monospace}
@@ -1024,7 +1091,11 @@ button{-webkit-appearance:none;appearance:none}
@keyframes cursor-blink{0%,100%{opacity:1}50%{opacity:0}}
/* ── Input bar (enlarged per request) ── */
-.input-bar{flex-shrink:0;padding:10px 12px 14px;padding-bottom:max(14px,var(--safe-bottom));background:var(--surface);border-top:1px solid rgba(255,255,255,0.04)}
+/* `--kb-height` is 0 when the keyboard is closed, > 0 when open. The
+ input bar lifts above the keyboard automatically; the chat area's
+ flex:1 makes it naturally shrink to fill the remaining space, so the
+ latest message stays visible instead of hiding behind the keys. */
+.input-bar{flex-shrink:0;padding:10px 12px 14px;padding-bottom:calc(max(14px, var(--safe-bottom)) + var(--kb-height, 0px));background:var(--surface);border-top:1px solid rgba(255,255,255,0.04)}
.img-preview-row{display:flex;flex-wrap:wrap;gap:6px;padding:0 0 8px}
.img-preview{position:relative;width:52px;height:52px;border-radius:var(--radius-md);overflow:hidden;background:var(--container-high);flex-shrink:0}
.img-preview img{width:100%;height:100%;object-fit:cover;display:block}
@@ -1044,6 +1115,42 @@ button{-webkit-appearance:none;appearance:none}
.send-btn:disabled{opacity:0.3}
.send-btn:active{opacity:0.85}
.send-btn .material-symbols-outlined{font-size:20px}
+/* Cancel state: while a stream or agent run is active the same button
+ switches to a muted red so the user can kill the request with one tap. */
+.send-btn.cancel{background:rgba(239,68,68,0.18);color:#fca5a5;border:1px solid rgba(239,68,68,0.35)}
+.send-btn.cancel:active{background:rgba(239,68,68,0.28)}
+
+/* ── Code block (Bug #4: collapsed by default + parity with desktop CodeBlock.tsx) ── */
+.cb-wrap{margin:8px 0;border:1px solid rgba(255,255,255,0.08);border-radius:var(--radius-md);background:rgba(0,0,0,0.4);overflow:hidden;max-width:100%}
+.cb-head{display:flex;align-items:center;justify-content:space-between;gap:6px;padding:5px 10px;background:rgba(255,255,255,0.04);border-bottom:1px solid rgba(255,255,255,0.06)}
+.cb-lang{font-size:0.58rem;color:var(--text-tertiary);font-family:ui-monospace,Menlo,monospace;letter-spacing:0.04em;text-transform:uppercase}
+.cb-actions{display:flex;align-items:center;gap:4px}
+.cb-action{display:flex;align-items:center;gap:3px;background:none;border:none;color:var(--text-tertiary);cursor:pointer;padding:3px 6px;border-radius:var(--radius);font-family:inherit;font-size:0.55rem;letter-spacing:0.06em;text-transform:uppercase;font-weight:600;transition:color 0.15s,background 0.15s}
+.cb-action:active{color:var(--primary);background:rgba(255,255,255,0.06)}
+.cb-action .material-symbols-outlined{font-size:13px}
+.cb-pre{margin:0;padding:8px 10px;overflow-x:auto;font-size:0.74rem;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;color:rgba(255,255,255,0.86);line-height:1.55;background:transparent;white-space:pre;border:none;border-radius:0}
+.cb-pre code{background:none;padding:0;border:none;font-size:inherit;color:inherit}
+.cb-toggle{width:100%;display:flex;align-items:center;justify-content:center;gap:5px;padding:5px;background:rgba(255,255,255,0.04);border:none;border-top:1px solid rgba(255,255,255,0.06);color:var(--text-tertiary);cursor:pointer;font-family:inherit;font-size:0.55rem;letter-spacing:0.08em;text-transform:uppercase;font-weight:600;transition:color 0.15s,background 0.15s}
+.cb-toggle:active{color:var(--primary);background:rgba(255,255,255,0.08)}
+.cb-toggle .cb-chev{font-size:13px;transition:transform 0.2s}
+.cb-wrap.open .cb-toggle .cb-chev{transform:rotate(180deg)}
+
+/* ── HTML preview overlay (Feature #8) ── */
+.html-preview-overlay{position:fixed;inset:0;z-index:300;background:rgba(0,0,0,0.7);-webkit-backdrop-filter:blur(4px);backdrop-filter:blur(4px);display:flex;flex-direction:column;animation:fadeIn 0.15s ease-out;padding-top:var(--safe-top);padding-bottom:var(--safe-bottom)}
+.html-preview-shell{margin:auto;width:min(96vw,820px);height:min(82vh,720px);background:var(--container);border:1px solid rgba(255,255,255,0.08);border-radius:var(--radius-md);display:flex;flex-direction:column;overflow:hidden;animation:slideUp 0.22s cubic-bezier(0.16,1,0.3,1)}
+.html-preview-header{display:flex;align-items:center;justify-content:space-between;gap:8px;padding:10px 14px;background:var(--container-low);border-bottom:1px solid rgba(255,255,255,0.06);flex-shrink:0}
+.html-preview-title{display:flex;align-items:center;gap:6px;font-size:0.66rem;font-weight:600;letter-spacing:0.12em;text-transform:uppercase;color:var(--text-secondary)}
+.html-preview-title .material-symbols-outlined{font-size:14px;color:var(--accent)}
+.html-preview-actions{display:flex;align-items:center;gap:4px}
+.html-preview-action{background:none;border:none;color:var(--text-secondary);cursor:pointer;width:32px;height:32px;display:flex;align-items:center;justify-content:center;border-radius:var(--radius);transition:background 0.15s}
+.html-preview-action:active{background:var(--container-high);color:var(--primary)}
+.html-preview-action .material-symbols-outlined{font-size:18px}
+.html-preview-frame{flex:1;width:100%;border:none;background:#ffffff;display:block}
+
+/* Settings button "permission needs attention" pulse — Bug #1 hint. */
+.settings-btn.perm-gap{border-color:var(--accent);color:var(--accent)}
+.settings-btn.perm-gap::before{content:'';display:inline-block;width:6px;height:6px;border-radius:50%;background:var(--accent);margin-right:6px;animation:perm-pulse 1.4s ease-in-out infinite}
+@keyframes perm-pulse{0%,100%{opacity:0.4}50%{opacity:1}}
</style>
</head>
<body>
@@ -1073,15 +1180,30 @@ button{-webkit-appearance:none;appearance:none}
logout:'<path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/>',
extension:'<path d="M20 12V8h-4a2 2 0 10-4 0H8v4a2 2 0 110 4v4h4a2 2 0 104 0h4v-4a2 2 0 110-4z"/>',
auto_awesome:'<path d="M12 3l1.8 4.2L18 9l-4.2 1.8L12 15l-1.8-4.2L6 9l4.2-1.8z"/><path d="M19 13l.9 2.1L22 16l-2.1.9L19 19l-.9-2.1L16 16l2.1-.9z"/>',
- psychology:'<path d="M9 21c0-2 1-3 1-5 0-1-1-2-2-2a4 4 0 01-4-4V8a5 5 0 0110 0v1c1 0 2 1 2 2 0 1-1 2-1 3 1 0 2 1 2 2v2h-2v3"/>',
- psychology_alt:'<path d="M9 21c0-2 1-3 1-5 0-1-1-2-2-2a4 4 0 01-4-4V8a5 5 0 0110 0v1c1 0 2 1 2 2 0 1-1 2-1 3 1 0 2 1 2 2v2h-2v3"/><circle cx="12" cy="9" r="0.8" fill="currentColor"/>',
+ // Bug #7: redesigned thinking icon — proper Lucide Brain glyph (parity
+ // with desktop ThinkingBlock.tsx which imports Brain from lucide-react).
+ // The old psychology / psychology_alt SVGs looked like a half-drawn head;
+ // the new Brain has two clear hemispheres so the meaning reads at 20px.
+ brain:'<path d="M12 5a3 3 0 1 0-5.997.125 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .556 6.588A4 4 0 1 0 12 18Z"/><path d="M12 5a3 3 0 1 1 5.997.125 4 4 0 0 1 2.526 5.77 4 4 0 0 1-.556 6.588A4 4 0 1 1 12 18Z"/><path d="M9 13a4.5 4.5 0 0 0 3-4 4.5 4.5 0 0 0 3 4"/>',
+ // Aliases — old call sites (psychology / psychology_alt) keep working
+ // by mapping to the same Brain glyph so we don't have to rewrite every
+ // string template that uses them. The redesign comment above stays
+ // accurate: there is now exactly one mental-icon shape on mobile.
+ psychology:'<path d="M12 5a3 3 0 1 0-5.997.125 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .556 6.588A4 4 0 1 0 12 18Z"/><path d="M12 5a3 3 0 1 1 5.997.125 4 4 0 0 1 2.526 5.77 4 4 0 0 1-.556 6.588A4 4 0 1 1 12 18Z"/><path d="M9 13a4.5 4.5 0 0 0 3-4 4.5 4.5 0 0 0 3 4"/>',
+ psychology_alt:'<path d="M12 5a3 3 0 1 0-5.997.125 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .556 6.588A4 4 0 1 0 12 18Z"/><path d="M12 5a3 3 0 1 1 5.997.125 4 4 0 0 1 2.526 5.77 4 4 0 0 1-.556 6.588A4 4 0 1 1 12 18Z"/><path d="M9 13a4.5 4.5 0 0 0 3-4 4.5 4.5 0 0 0 3 4"/>',
+ // smart_toy = our agent icon (kept across all states — see Bug #6).
smart_toy:'<rect x="3" y="11" width="18" height="10" rx="2"/><circle cx="12" cy="5" r="2"/><line x1="12" y1="7" x2="12" y2="11"/><circle cx="8.5" cy="16" r="1" fill="currentColor"/><circle cx="15.5" cy="16" r="1" fill="currentColor"/>',
stop:'<rect x="6" y="6" width="12" height="12" rx="1"/>',
pencil:'<path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 013 3L7 19l-4 1 1-4L16.5 3.5z"/>',
refresh:'<polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0114.85-3.36L23 10"/><path d="M20.49 15A9 9 0 015.64 18.36L1 14"/>',
tune:'<line x1="4" y1="21" x2="4" y2="14"/><line x1="4" y1="10" x2="4" y2="3"/><line x1="12" y1="21" x2="12" y2="12"/><line x1="12" y1="8" x2="12" y2="3"/><line x1="20" y1="21" x2="20" y2="16"/><line x1="20" y1="12" x2="20" y2="3"/><line x1="1" y1="14" x2="7" y2="14"/><line x1="9" y1="8" x2="15" y2="8"/><line x1="17" y1="16" x2="23" y2="16"/>',
trash:'<polyline points="3 6 5 6 21 6"/><path d="M19 6l-2 14a2 2 0 01-2 2H9a2 2 0 01-2-2L5 6"/><path d="M10 11v6"/><path d="M14 11v6"/><path d="M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2"/>',
- delete_sweep:'<polyline points="3 6 5 6 21 6"/><path d="M19 6l-2 14a2 2 0 01-2 2H9a2 2 0 01-2-2L5 6"/>'
+ delete_sweep:'<polyline points="3 6 5 6 21 6"/><path d="M19 6l-2 14a2 2 0 01-2 2H9a2 2 0 01-2-2L5 6"/>',
+ // Feature #8: HTML preview chip glyphs.
+ play_arrow:'<polygon points="6 3 20 12 6 21 6 3" fill="currentColor"/>',
+ open_in_new:'<path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/>',
+ code_brackets:'<polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/>',
+ eye:'<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/>'
};
function svgIcon(name){return ICONS[name] ? ICON_SVG_OPEN + ICONS[name] + ICON_SVG_CLOSE : '';}
// Expose for inline handlers (rare path, but keeps symmetry with prev API)
@@ -1105,12 +1227,65 @@ button{-webkit-appearance:none;appearance:none}
// are NOT exposed here — user explicitly asked for permissions only.
var remotePerms = { filesystem: false, downloads: false, process_control: false };
- // ── Codex prompt (mobile-adapted, no tool execution) ──
- var CODEX_PROMPT = 'You are Codex, a coding-focused assistant. Provide clean, efficient, well-commented code and clear explanations. Prefer showing code over prose. Focus on correctness, readability, and best practices. Be concise. When the user asks about files, explain what changes you would make without claiming to have executed them — the mobile client cannot run tools.';
+ // ── Codex prompt (agentic on mobile) ──
+ // Codex on mobile now runs the same ReAct agent loop as desktop Codex:
+ // tool calls via the per-chat `~/agent-workspace/<chatId>/` folder,
+ // with live Thought/Action/Observation cards streamed into the chat.
+ // The old "you can't run tools" text was from v2.3.3 before the mobile
+ // bridge could actually execute them.
+ var CODEX_PROMPT = 'You are Codex, an agentic coding assistant running on a phone that bridges to a desktop machine via a per-chat workspace at `~/agent-workspace/<this chat>/`. You CAN run tools: file_read, file_write, file_list, file_search, shell_execute, code_execute, web_search, web_fetch, system_info, get_current_time. Relative paths resolve inside the workspace so multiple chats never overwrite each other. Default to Python for computation (code_execute) and POSIX/PowerShell shell for system tasks. When implementing features, READ before you WRITE. Keep responses concise and action-oriented. Verify your work by reading back the file or running a quick check.';
// ── Thinking-compatible prefixes (parity with desktop) ──
var THINKING_COMPATIBLE = ['qwq','deepseek-r1','qwen3','qwen3.5','qwen3-coder','gemma3','gemma4'];
+ // ── Plain-text planner models — Gemma 3/4 ──
+ // Bug fix parity with desktop entry #80: Gemma 3/4 with `think:false`
+ // emits PLAIN-TEXT structured planning ("Plan:" / "Constraint Checklist:"
+ // / "Confidence Score:" / "Self-Correction during drafting:") that no
+ // tag-stripper can clean. The working escape hatch is to NOT send the
+ // explicit `think:false` to Ollama for these — let Ollama's default
+ // tagged thinking kick in, then strip the tags via stripNonCanonicalTags
+ // below. Same trade-off as desktop: hidden token spend, clean answer.
+ var PLAIN_TEXT_PLANNER_PREFIXES = ['gemma3','gemma4'];
+ function isPlainTextPlanner(modelName){
+ if(!modelName) return false;
+ var n = String(modelName).toLowerCase()
+ .replace(/^[^/]+\//,'')
+ .replace(/:.*$/,'')
+ .replace(/-abliterated/g,'')
+ .replace(/-uncensored/g,'')
+ .replace(/-heretic/g,'');
+ for(var i=0;i<PLAIN_TEXT_PLANNER_PREFIXES.length;i++){
+ if(n.indexOf(PLAIN_TEXT_PLANNER_PREFIXES[i])===0) return true;
+ }
+ return false;
+ }
+
+ // ── Universal thinking-tag stripper (mobile port of
+ // src/lib/thinking-stripper.ts). The canonical char-state-machine
+ // in pushChunkContent only handles `<think>…</think>`. This catches
+ // the non-canonical formats Gemma / GPT-OSS / DeepSeek-distill emit:
+ // <|channel|>thought, <thought>, <reasoning>, <reflect>, <deepthink>.
+ // Stripping happens AFTER content is in the bubble so the user never
+ // sees a "Plan:" preamble. ──
+ function stripNonCanonicalTags(text){
+ if(!text) return '';
+ var out = text;
+ // Gemma channel marker: <|channel|>thought OR <|channel|>reasoning…
+ out = out.replace(/<\|channel\|>[\s\S]*?<\|message\|>/gi, '');
+ // Wrapped variants (closing tags use the same name).
+ out = out.replace(/<thought>[\s\S]*?<\/thought>/gi, '');
+ out = out.replace(/<reasoning>[\s\S]*?<\/reasoning>/gi, '');
+ out = out.replace(/<reflect>[\s\S]*?<\/reflect>/gi, '');
+ out = out.replace(/<deepthink>[\s\S]*?<\/deepthink>/gi, '');
+ out = out.replace(/<analysis>[\s\S]*?<\/analysis>/gi, '');
+ // Orphan opener (model emits opening tag but never closes — e.g. cut
+ // off mid-thought). Treat the rest of the buffer as discarded thought.
+ var openMatch = /<thought>|<reasoning>|<reflect>|<deepthink>|<analysis>|<\|channel\|>/i.exec(out);
+ if(openMatch){ out = out.slice(0, openMatch.index); }
+ return out;
+ }
+
// ── Built-in personas (mobile parity) ──
var PERSONAS = [
{id:'unrestricted',name:'No Filter',prompt:''},
@@ -1147,6 +1322,9 @@ button{-webkit-appearance:none;appearance:none}
var streaming = false;
var abortCtrl = null;
var pendingImages = []; // [{data: base64, mimeType, name}]
+ // Thinking toggle removed from mobile — see renderShell() comment.
+ // Hardcoded false so every "is thinking on?" call site sees the same
+ // answer and the stripper drops reasoning tokens silently.
var thinking = false;
var drawerOpen = false;
// Agent mode is per-chat; toggled via the brain icon next to Plugins.
@@ -1199,73 +1377,161 @@ button{-webkit-appearance:none;appearance:none}
parameters:[]}
];
- function buildReActPrompt(goal, history){
- var toolDescs = AGENT_TOOLS.map(function(t){
- var params = t.parameters.map(function(p){
- return ' - '+p.name+' ('+p.type+(p.required?', required':', optional')+'): '+p.description;
- }).join('\n');
- return ' '+t.name+': '+t.description+'\n Parameters:\n'+params;
- }).join('\n\n');
- var historyText = history.map(function(e){
- switch(e.type){
- case 'thought': return 'Thought: '+e.content;
- case 'action': return 'Action: '+e.content;
- case 'observation': return 'Observation: '+e.content;
- case 'error': return 'Error: '+e.content;
- case 'user_input': return 'User: '+e.content;
- default: return e.content;
+ // ── Tool-set constants ──
+ var CODEX_TOOLS = ['file_read','file_write','file_list','file_search','shell_execute','code_execute','system_info','get_current_time','web_search','web_fetch'];
+ var AGENT_ALL_TOOLS = AGENT_TOOLS.map(function(t){return t.name;});
+
+ // Convert the flat AGENT_TOOLS array into Ollama's native tools schema.
+ // `toolNames` is a whitelist — only tools whose name appears in it are
+ // included. Returns [{type:'function', function:{name, description,
+ // parameters:{type:'object', properties:{...}, required:[...]}}}].
+ function buildToolDefs(toolNames){
+ var out = [];
+ for(var i=0;i<AGENT_TOOLS.length;i++){
+ var t = AGENT_TOOLS[i];
+ if(toolNames.indexOf(t.name) === -1) continue;
+ var props = {};
+ var req = [];
+ for(var j=0;j<t.parameters.length;j++){
+ var p = t.parameters[j];
+ props[p.name] = {type: p.type, description: p.description};
+ if(p.required) req.push(p.name);
}
- }).join('\n');
- var open = '```' + 'json';
- var close = '```';
- return 'You are an autonomous AI agent. Your goal is: '+goal+'\n\n'+
- 'You have access to the following tools:\n\n'+toolDescs+'\n\n'+
- 'You must respond with a JSON object in one of these two formats:\n\n'+
- 'To use a tool:\n'+open+'\n{"thought": "your reasoning about what to do next", "action": "tool_name", "args": {"param1": "value1"}}\n'+close+'\n\n'+
- 'To finish the task:\n'+open+'\n{"thought": "your final reasoning", "action": "finish", "answer": "your final answer or summary of what was accomplished"}\n'+close+'\n\n'+
- 'Rules:\n- Always include a "thought" explaining your reasoning\n- Use exactly one action per response\n- Only use tools from the list above\n- If a tool returns an error, try a different approach\n- When the goal is accomplished, use the "finish" action\n- Be concise and efficient\n\n'+
- (historyText ? '\nPrevious steps:\n'+historyText+'\n\nContinue from where you left off.' : 'Begin working on the goal now.');
+ out.push({
+ type: 'function',
+ function: {
+ name: t.name,
+ description: t.description,
+ parameters: {type:'object', properties: props, required: req}
+ }
+ });
+ }
+ return out;
}
- function parseAgentResponse(response){
- var codeBlockMatch = response.match(/```(?:json)?\s*\n?([\s\S]*?)\n?```/);
- var candidate = codeBlockMatch ? codeBlockMatch[1] : response;
- var jsonMatch = candidate.match(/\{[\s\S]*\}/);
- if(jsonMatch){
- try{
- var p = JSON.parse(jsonMatch[0]);
- return {
- thought: p.thought || p.thinking || p.reasoning || '',
- action: String(p.action || p.tool || 'continue').toLowerCase().trim(),
- args: p.args || p.arguments || p.parameters || p.input || {},
- answer: p.answer || p.final_answer || p.response
- };
- }catch(_){}
+ // Strip <think>...</think> tags from content. If keepThinking is true,
+ // capture inner text into the returned `thinking` field; otherwise
+ // discard it. Also handles Ollama's native `thinking` field.
+ function stripThinkTags(content, keepThinking){
+ var think = '';
+ // Handle inline <think>...</think> tags (may be multiple)
+ var cleaned = String(content || '').replace(/<think>([\s\S]*?)<\/think>/g, function(_, inner){
+ if(keepThinking && inner) think = think ? think+'\n'+inner : inner;
+ return '';
+ });
+ // Strip non-canonical reasoning tags too
+ cleaned = stripNonCanonicalTags(cleaned).trim();
+ return {content: cleaned, thinking: think};
+ }
+
+ // Non-streaming POST to /api/chat with native `tools` array.
+ // Returns a Promise that resolves to {content, thinking, toolCalls}.
+ // `apiMessages` = [{role, content, ...}], `tools` = Ollama tool defs.
+ function nativeToolChat(apiMessages, tools){
+ var body = {
+ model: currentModel,
+ messages: apiMessages,
+ tools: tools,
+ stream: false,
+ options: {num_gpu: 99, num_predict: 16384}
+ };
+ // Tri-state think flag — same logic as _doSend.
+ if(isThinkingCompatible(currentModel)){
+ if(thinking){
+ body.think = true;
+ } else if(!isPlainTextPlanner(currentModel)){
+ body.think = false;
+ }
}
- return {thought: String(response).slice(0,500), action: 'continue', args: {}};
+
+ function doPost(b){
+ return fetch('/api/chat',{
+ method:'POST',
+ headers:{'Content-Type':'application/json','Authorization':'Bearer '+TOKEN},
+ body: JSON.stringify(b),
+ signal: abortCtrl ? abortCtrl.signal : undefined
+ });
+ }
+
+ return doPost(body).then(function(r){
+ if(r.status===401){ clearAuthAndReload(); throw new Error('401'); }
+ if(!r.ok){
+ if(r.status===400 && ('think' in body)){
+ var retry = {}; for(var k in body) retry[k]=body[k]; delete retry.think;
+ return doPost(retry).then(function(rr){
+ if(!rr.ok) return rr.text().then(function(t){throw new Error('HTTP '+rr.status+': '+t);});
+ return rr.json();
+ });
+ }
+ return r.text().then(function(t){
+ // Detect "model does not support tools" errors
+ if(t.indexOf('does not support tools') >= 0 || t.indexOf('does not support tool') >= 0){
+ throw new Error('TOOLS_NOT_SUPPORTED');
+ }
+ throw new Error('HTTP '+r.status+': '+t);
+ });
+ }
+ return r.json();
+ }).then(function(data){
+ var msg = data.message || {};
+ var rawContent = msg.content || '';
+ var rawThinking = msg.thinking || '';
+ var toolCalls = [];
+ if(Array.isArray(msg.tool_calls)){
+ for(var i=0;i<msg.tool_calls.length;i++){
+ var tc = msg.tool_calls[i];
+ if(tc && tc.function){
+ toolCalls.push({function:{name:tc.function.name, arguments:tc.function.arguments || {}}});
+ }
+ }
+ }
+ return {content: rawContent, thinking: rawThinking, toolCalls: toolCalls};
+ });
}
// Run a single tool against the desktop via /remote-api/agent-tool.
- // Returns a stringified observation suitable for the next ReAct turn.
+ // Returns a stringified observation suitable for the next loop turn.
+ // The Rust bridge returns 200 OK with {error:"..."} for every failure
+ // (missing arg, permission, underlying tool error). We surface those
+ // as clean observations instead of "HTTP 500: ..." red errors.
function runAgentTool(tool, args){
return fetch('/remote-api/agent-tool',{
method:'POST',
headers:{'Content-Type':'application/json','Authorization':'Bearer '+TOKEN},
- body: JSON.stringify({tool:tool, args:args||{}})
+ // `chatId` → per-chat workspace on the desktop side. Each mobile
+ // chat gets its own isolated `~/agent-workspace/<chatId>/` folder
+ // so agents across chats don't trample each other's files.
+ body: JSON.stringify({tool:tool, args:args||{}, chatId: currentChatId || ''})
}).then(function(r){
if(r.status===401){ clearAuthAndReload(); return 'Auth required'; }
- if(r.status===403){ return r.text().then(function(t){return 'Permission denied: '+t;}); }
return r.text().then(function(text){
- if(!r.ok) return 'Error '+r.status+': '+text;
- try{
- var data = JSON.parse(text);
+ // Try to parse JSON regardless of status — the new bridge always
+ // responds with a JSON body.
+ var data = null;
+ try{ data = JSON.parse(text); }catch(_){ /* leave null */ }
+
+ // Old-style HTTP errors (e.g. 400 from a malformed payload) — fall
+ // back to plain text so the agent still sees something useful.
+ if(!r.ok && !data){ return 'Error '+r.status+': '+text; }
+
+ if(data){
+ // Surface permission-denied with an actionable hint to enable it.
+ if(data.needs_permission){
+ // Give the user a one-tap path: bump the header plugins icon
+ // pulse so they know to open Settings → Permissions.
+ try{ window._flagPermissionGap && window._flagPermissionGap(data.permission); }catch(_){}
+ return 'Permission denied: '+data.error;
+ }
+ // Generic tool error — clean observation.
+ if(typeof data.error === 'string') return 'Error: '+data.error;
if(typeof data === 'string') return data;
// web_search returns {results:[{title,url,snippet},...]}
- if(data && Array.isArray(data.results)){
+ if(Array.isArray(data.results)){
+ if(!data.results.length) return 'No results.';
return data.results.map(function(it,i){return (i+1)+'. '+(it.title||'')+'\n '+(it.url||'')+'\n '+(it.snippet||'');}).join('\n\n');
}
// web_fetch returns {url, status, contentType, title, text, truncated}
- if(data && typeof data.text === 'string' && (data.url || data.status !== undefined)){
+ if(typeof data.text === 'string' && (data.url || data.status !== undefined)){
var parts = [];
if(data.title) parts.push('Title: '+data.title);
if(data.url) parts.push('URL: '+data.url);
@@ -1276,11 +1542,11 @@ button{-webkit-appearance:none;appearance:none}
return parts.join('\n');
}
// file_read returns {content:"..."}
- if(data && typeof data.content === 'string') return data.content;
+ if(typeof data.content === 'string') return data.content;
// file_write returns {status:"saved", path:"..."}
- if(data && data.status==='saved') return 'File saved: '+(data.path||args.path||'');
- // code_execute returns {stdout, stderr, exitCode, timedOut}
- if(data && (data.exitCode!==undefined || data.stdout!==undefined)){
+ if(data.status==='saved') return 'File saved: '+(data.path||args.path||'');
+ // code_execute / shell_execute returns {stdout, stderr, exitCode, timedOut}
+ if(data.exitCode!==undefined || data.stdout!==undefined){
var out = data.stdout || '';
var err = data.stderr || '';
if(data.timedOut) return 'Execution timed out.';
@@ -1288,11 +1554,20 @@ button{-webkit-appearance:none;appearance:none}
return out || (err ? 'stderr: '+err : 'Done.');
}
return JSON.stringify(data);
- }catch(_){ return text; }
+ }
+ return text || 'Done.';
});
}).catch(function(e){ return 'Network error: '+(e && e.message || e); });
}
+ // Highlights the Settings (cog) icon in the drawer when an agent tool
+ // got blocked by a permission. Best-effort — we just toggle a class so
+ // the user notices the dot. No-op if the drawer isn't mounted yet.
+ window._flagPermissionGap = function(_perm){
+ var sb = document.querySelector('.settings-btn');
+ if(sb){ sb.classList.add('perm-gap'); }
+ };
+
function H(t){return String(t==null?'':t).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');}
function el(id){return document.getElementById(id);}
function uid(){return 'c-'+Date.now()+'-'+Math.random().toString(36).slice(2,7);}
@@ -1336,7 +1611,8 @@ button{-webkit-appearance:none;appearance:none}
}
}
currentChatId = localStorage.getItem('lu-mobile-current-chat') || '';
- thinking = localStorage.getItem('lu-mobile-thinking') === '1';
+ // `thinking` is always false now (UI toggle removed).
+ thinking = false;
}catch(_){chats=[];currentChatId='';thinking=false;}
}
function persistChats(){
@@ -1345,7 +1621,6 @@ button{-webkit-appearance:none;appearance:none}
function persistState(){
try{
localStorage.setItem('lu-mobile-current-chat', currentChatId);
- localStorage.setItem('lu-mobile-thinking', thinking?'1':'0');
}catch(_){}
}
function getCaveman(){var c=findChat(currentChatId); return c && c.caveman ? c.caveman : 'off';}
@@ -1466,6 +1741,30 @@ button{-webkit-appearance:none;appearance:none}
}
loadPersisted();
+
+ // ── Keyboard / viewport tracking ────────────────────────────────
+ // iOS Safari + some Android browsers do NOT resize `window.innerHeight`
+ // when the software keyboard opens. The page sits at full height behind
+ // the keyboard and the top of the chat scrolls off-screen. `dvh` CSS
+ // units fix most cases; for the holdouts we sync an explicit
+ // `--kb-height` custom prop and apply it as bottom-padding on the
+ // input bar so it never hides behind the keyboard.
+ (function setupViewportTracking(){
+ if(typeof window.visualViewport === 'undefined') return;
+ var vv = window.visualViewport;
+ function sync(){
+ // Height difference = keyboard + safe-area chrome on the bottom.
+ var kb = Math.max(0, (window.innerHeight || 0) - vv.height - vv.offsetTop);
+ document.documentElement.style.setProperty('--kb-height', kb + 'px');
+ // Also pin the scroll position to the bottom of the current chat
+ // when the keyboard animates in, so the latest message stays visible.
+ var cm = document.getElementById('chat-msgs');
+ if(cm && kb > 0){ cm.scrollTop = cm.scrollHeight; }
+ }
+ vv.addEventListener('resize', sync);
+ vv.addEventListener('scroll', sync);
+ sync();
+ })();
Promise.all([authJson('/remote-api/config'), authJson('/api/tags')]).then(function(res){
var cfg = res[0] || {};
var tags = res[1] || {};
@@ -1487,20 +1786,35 @@ button{-webkit-appearance:none;appearance:none}
msgs = Array.isArray(c.msgs) ? c.msgs.slice() : [];
}
- // Turn off thinking if incompatible
- if(thinking && !isThinkingCompatible(currentModel)){ thinking = false; persistState(); }
-
renderShell();
});
function renderShell(){
var mode = getCurrentMode();
- var modeTag = mode==='codex' ? '<span class="header-mode-tag">Codex</span>' :
+ var isCodex = mode === 'codex';
+ var modeTag = isCodex ? '<span class="header-mode-tag">Codex</span>' :
(getAgentEnabled() ? '<span class="header-mode-tag">Agent</span>' : '');
- var thinkCls = !isThinkingCompatible(currentModel) ? 'disabled' : (thinking ? 'active' : '');
- var thinkIcon = thinking ? 'psychology' : 'psychology_alt';
var pluginsActive = (getCaveman()!=='off' || getPersonaEnabled()) ? ' active' : '';
var agentActive = getAgentEnabled() ? ' active' : '';
+ // Agent icon STAYS smart_toy regardless of state. The button class
+ // flips to `active` (purple) while running, click routes to _stopAgent.
+ var agentClickHandler = agentRunning ? 'window._stopAgent()' : 'window._toggleAgent()';
+ var agentLabel = agentRunning ? 'Stop agent' : 'Agent';
+ var agentTitle = agentRunning ? 'Stop agent' : 'Agent mode (native tool calling)';
+ var agentBtnCls = agentRunning ? 'active' : agentActive.trim();
+ // Hidden in Codex chats (Codex on mobile is a coding-focused plain
+ // chat, no phone-side tool execution).
+ var agentBtn = isCodex ? '' :
+ ('<button class="icon-btn '+agentBtnCls+'" id="agent-btn" onclick="'+agentClickHandler+'" aria-label="'+agentLabel+'" title="'+agentTitle+'">'+
+ '<span class="material-symbols-outlined">'+svgIcon('smart_toy')+'</span>'+
+ '</button>');
+
+ // Thinking-toggle button REMOVED from mobile (user request): wasn't
+ // working reliably and confused users. Thinking is now fully handled
+ // under-the-hood: stripper silently drops any reasoning tokens regardless
+ // of whether the model emitted canonical <think> or non-canonical tags.
+ // The `thinking` variable stays as a constant `false` internally for the
+ // few code paths that still reference it.
el('app').innerHTML =
'<div class="app-shell">' +
@@ -1515,19 +1829,10 @@ button{-webkit-appearance:none;appearance:none}
'<span class="model-name">'+H(currentModel || 'Select model')+'</span>' +
'<span class="material-symbols-outlined chev">'+svgIcon('expand_more')+'</span>' +
'</button>' +
- (agentRunning
- ? '<button class="icon-btn active" id="agent-btn" onclick="window._stopAgent()" aria-label="Stop agent" title="Stop agent">'+
- '<span class="material-symbols-outlined">'+svgIcon('stop')+'</span>'+
- '</button>'
- : '<button class="icon-btn'+agentActive+'" id="agent-btn" onclick="window._toggleAgent()" aria-label="Agent" title="Agent mode (ReAct + tools)">'+
- '<span class="material-symbols-outlined">'+svgIcon('smart_toy')+'</span>'+
- '</button>') +
+ agentBtn +
'<button class="icon-btn'+pluginsActive+'" id="plugins-btn" onclick="window._openPluginsPicker()" aria-label="Plugins">' +
'<span class="material-symbols-outlined">'+svgIcon('extension')+'</span>' +
'</button>' +
- '<button class="icon-btn '+thinkCls+'" id="think-btn" onclick="window._toggleThinking()" aria-label="Thinking">' +
- '<span class="material-symbols-outlined">'+svgIcon(thinkIcon)+'</span>' +
- '</button>' +
'</div>' +
'<div class="chat-area" id="chat-area"></div>' +
'<div class="input-bar">' +
@@ -1536,7 +1841,19 @@ button{-webkit-appearance:none;appearance:none}
'<button class="attach-btn" id="attach-btn" onclick="window._triggerAttach()" aria-label="Attach file"><span class="material-symbols-outlined">'+svgIcon('attach_file')+'</span></button>' +
'<input type="file" id="file-input" accept="image/*" multiple style="display:none">' +
'<textarea id="msg-input" rows="1" placeholder="'+(getAgentEnabled()?'Give the agent a goal…':'Message...')+'"></textarea>' +
- '<button class="send-btn" id="send-btn" onclick="window._doSend()" aria-label="Send"><span class="material-symbols-outlined">'+svgIcon('arrow_upward')+'</span></button>' +
+ // Send-Button flips to a red Stop chip while a response is
+ // in flight (streaming chat OR native tool loop). Without
+ // this the only "stop" control was hidden in the header,
+ // which users kept missing. Single id so CSS + handler both
+ // route to the right action.
+ (function(){
+ var busy = streaming || agentRunning;
+ var btnCls = busy ? 'send-btn cancel' : 'send-btn';
+ var handler = busy ? 'window._cancelSend()' : 'window._doSend()';
+ var label = busy ? 'Stop' : 'Send';
+ var icon = busy ? 'stop' : 'arrow_upward';
+ return '<button class="'+btnCls+'" id="send-btn" onclick="'+handler+'" aria-label="'+label+'" title="'+label+'"><span class="material-symbols-outlined">'+svgIcon(icon)+'</span></button>';
+ })() +
'</div>' +
'</div>' +
'</div>' +
@@ -1627,7 +1944,6 @@ button{-webkit-appearance:none;appearance:none}
if(name){
currentModel = name;
try{localStorage.setItem('lu-mobile-model', name);}catch(_){}
- if(thinking && !isThinkingCompatible(currentModel)){ thinking=false; persistState(); }
renderShell();
}
document.body.removeChild(overlay);
@@ -1713,6 +2029,9 @@ button{-webkit-appearance:none;appearance:none}
var html = '<div class="chat-messages" id="chat-msgs">';
for(var i=0;i<msgs.length;i++){
var m = msgs[i];
+ // Skip hidden tool-call history (persisted for continue capability
+ // but not user-visible). The model sees them on the next turn.
+ if(m.hidden) continue;
var isUser = m.role==='user';
var isLast = i===msgs.length-1;
var typingCls = (streaming && isLast && !isUser) ? ' msg-typing' : '';
@@ -1725,18 +2044,11 @@ button{-webkit-appearance:none;appearance:none}
}
html += '</div>';
}
- // Thinking block (assistant, collapsible) — rendered ABOVE the bubble
- if(!isUser && m.thinking){
- var openCls = m.thinkingOpen ? ' open' : '';
- html += '<div class="think-block'+openCls+'">' +
- '<button class="think-toggle" onclick="window._toggleThink(\''+m.id+'\')">' +
- '<span class="material-symbols-outlined think-icon">'+svgIcon('psychology')+'</span>' +
- '<span class="think-label">Thinking</span>' +
- '<span class="material-symbols-outlined think-chev">'+svgIcon('expand_more')+'</span>' +
- '</button>' +
- '<div class="think-body">'+renderMd(m.thinking)+'</div>' +
- '</div>';
- }
+ // Thinking block never rendered on mobile — the toggle was removed
+ // (user request: "thinking toggle im Mobile ersetzt / rausgenommen").
+ // Any accidentally captured reasoning text lives in `m.thinking` but
+ // is NOT shown; the stripper drops incoming reasoning bytes at the
+ // source so `m.thinking` usually stays empty anyway.
// Agent steps (transient, during / after a run). These stay visible
// but they are NOT part of msg.content — so the next user turn does
// not see the ReAct scaffolding and cannot drift into that style.
@@ -1746,16 +2058,19 @@ button{-webkit-appearance:none;appearance:none}
html += '<div class="agent-steps">';
for(var si=0; si<m.agentSteps.length; si++){
var st = m.agentSteps[si];
- var stIcon = st.type==='thought' ? 'psychology'
+ var stIcon = st.type==='thought' ? 'brain'
: st.type==='action' ? 'smart_toy'
: st.type==='observation' ? 'check'
: st.type==='error' ? 'close'
: 'auto_awesome';
var openCls = st.open ? ' open' : '';
+ // Live = step is still in flight (model streaming its JSON, or
+ // the tool hasn't returned yet). Triggers the pulsing stripe CSS.
+ var liveCls = st.live ? ' live' : '';
var summary = String(st.content||'').replace(/\s+/g,' ').slice(0, 80);
if((st.content||'').length > 80) summary += '…';
var stepKey = m.id + ':' + si;
- html += '<div class="agent-step agent-'+H(st.type||'info')+openCls+'">' +
+ html += '<div class="agent-step agent-'+H(st.type||'info')+openCls+liveCls+'">' +
'<button class="agent-step-toggle" onclick="window._toggleAgentStep(\''+H(stepKey)+'\')">' +
'<span class="material-symbols-outlined agent-step-icon">'+svgIcon(stIcon)+'</span>' +
'<span class="agent-step-label">'+H(st.type||'info')+'</span>' +
@@ -1795,16 +2110,140 @@ button{-webkit-appearance:none;appearance:none}
if(cm) cm.scrollTop = cm.scrollHeight;
}
+ // Persistent expand/collapse state for individual code blocks. Keyed by
+ // a stable hash of (language + content) so the state survives re-renders
+ // of the chat. Same hash is used for the HTML-preview blob lookup.
+ var codeBlockOpen = {};
+ // Cache of code payloads — Preview button reads from here without having
+ // to re-extract from the rendered DOM. Keyed by the same djb2 hash.
+ var codeBlockSource = {};
+
+ function djb2(str){
+ var h = 5381;
+ for(var i=0;i<str.length;i++){ h = ((h << 5) + h) ^ str.charCodeAt(i); h |= 0; }
+ // Force unsigned + base36 for short stable ID
+ return (h >>> 0).toString(36);
+ }
+ // Heuristic: this code block IS a renderable HTML document.
+ function isHtmlSnippet(lang, raw){
+ var l = (lang || '').toLowerCase();
+ if(l === 'html' || l === 'htm' || l === 'xhtml' || l === 'svg') return true;
+ var t = raw.trim().toLowerCase();
+ if(!t) return false;
+ if(t.indexOf('<!doctype html') === 0) return true;
+ if(t.indexOf('<html') === 0) return true;
+ if(t.indexOf('<svg') === 0 && t.indexOf('xmlns') > 0) return true;
+ return false;
+ }
+ // Bug #4 / Feature #8: code blocks are collapsed by default if longer
+ // than COLLAPSE_THRESHOLD lines (parity with desktop CodeBlock.tsx).
+ // Renderable HTML/SVG also gets a "Preview" chip → opens a sandboxed
+ // iframe overlay in `_openHtmlPreview`.
+ var COLLAPSE_THRESHOLD = 4;
+
function renderMd(text){
var s = H(text);
- s = s.replace(/```(\w*)\n([\s\S]*?)```/g, function(_, lang, code){
- return '<pre><button class="copy-btn" onclick="window._copyCode(this)"><span class="material-symbols-outlined">'+svgIcon('content_copy')+'</span></button><code>'+code+'</code></pre>';
+ s = s.replace(/```(\w*)\n?([\s\S]*?)```/g, function(_, lang, code){
+ var rawCode = code.replace(/\n$/, ''); // trim trailing newline
+ var rawHtmlEscaped = H(rawCode);
+ var lines = rawCode.split('\n');
+ var lineCount = lines.length;
+ var langLabel = (lang || 'code').toLowerCase();
+ var key = djb2(langLabel + '\n' + rawCode);
+ // Cache the original source so the Preview button can read it back.
+ codeBlockSource[key] = { lang: langLabel, code: rawCode };
+ var isLong = lineCount > COLLAPSE_THRESHOLD;
+ var expanded = !isLong || codeBlockOpen[key] === true;
+ var displayCode = expanded
+ ? rawHtmlEscaped
+ : H(lines.slice(0, COLLAPSE_THRESHOLD).join('\n'));
+ var htmlPreview = isHtmlSnippet(langLabel, rawCode);
+ var previewBtn = htmlPreview
+ ? '<button class="cb-action" onclick="window._openHtmlPreview(\''+key+'\')" aria-label="Preview HTML"><span class="material-symbols-outlined">'+svgIcon('eye')+'</span><span class="cb-action-label">Preview</span></button>'
+ : '';
+ var toggleBtn = isLong
+ ? '<button class="cb-toggle" onclick="window._toggleCodeBlock(\''+key+'\')"><span class="material-symbols-outlined cb-chev">'+svgIcon('expand_more')+'</span>'+(expanded?'Collapse':('Show all '+lineCount+' lines'))+'</button>'
+ : '';
+ return ''+
+ '<div class="cb-wrap'+(expanded?' open':'')+'" data-cb-id="'+key+'">'+
+ '<div class="cb-head">'+
+ '<span class="cb-lang">'+H(langLabel)+'</span>'+
+ '<div class="cb-actions">'+
+ previewBtn +
+ '<button class="cb-action" onclick="window._copyCodeKey(\''+key+'\')" aria-label="Copy"><span class="material-symbols-outlined">'+svgIcon('content_copy')+'</span><span class="cb-action-label">Copy</span></button>'+
+ '</div>'+
+ '</div>'+
+ '<pre class="cb-pre"><code>'+displayCode+'</code></pre>'+
+ toggleBtn +
+ '</div>';
});
s = s.replace(/`([^`]+)`/g,'<code>$1</code>');
s = s.replace(/\*\*(.+?)\*\*/g,'<b>$1</b>');
return s;
}
+ // Toggle handler for the "Show all N lines" / "Collapse" button.
+ window._toggleCodeBlock = function(key){
+ codeBlockOpen[key] = !codeBlockOpen[key];
+ renderChat();
+ };
+ // Copy handler that reads from our cache (no DOM scraping → handles
+ // collapsed blocks correctly).
+ window._copyCodeKey = function(key){
+ var src = codeBlockSource[key];
+ if(src && src.code) navigator.clipboard.writeText(src.code).catch(function(){});
+ };
+ // Feature #8: full-screen sandboxed HTML preview. Opens an iframe with
+ // `srcdoc` + sandbox flags so user-supplied JS can't reach the parent
+ // page. "Open in new tab" button hands the data: URL to the host
+ // browser for full inspection. Tap backdrop or close icon to dismiss.
+ window._openHtmlPreview = function(key){
+ var src = codeBlockSource[key];
+ if(!src) return;
+ var raw = src.code || '';
+ var lang = (src.lang || '').toLowerCase();
+ var doc = raw;
+ // Bare SVG → wrap so the iframe centres it.
+ if(lang === 'svg' || (raw.trim().toLowerCase().indexOf('<svg') === 0 && raw.indexOf('xmlns') > 0)){
+ doc = '<!doctype html><html><head><meta charset="utf-8"><title>SVG Preview</title>' +
+ '<style>html,body{margin:0;padding:0;background:#0e0e0e;color:#ffffff;height:100%;display:flex;align-items:center;justify-content:center;font-family:system-ui,sans-serif}svg{max-width:100%;max-height:100%}</style>' +
+ '</head><body>'+raw+'</body></html>';
+ } else if(raw.trim().toLowerCase().indexOf('<!doctype') !== 0 &&
+ raw.trim().toLowerCase().indexOf('<html') !== 0){
+ // Snippet-only HTML → wrap in a minimal doc so meta-charset is set.
+ doc = '<!doctype html><html><head><meta charset="utf-8"><title>HTML Preview</title>' +
+ '<style>body{margin:0;padding:16px;font-family:system-ui,-apple-system,sans-serif}</style>' +
+ '</head><body>'+raw+'</body></html>';
+ }
+ var overlay = document.createElement('div');
+ overlay.className = 'html-preview-overlay';
+ overlay.onclick = function(e){ if(e.target === overlay) document.body.removeChild(overlay); };
+ var dataUrl = 'data:text/html;charset=utf-8;base64,' + btoa(unescape(encodeURIComponent(doc)));
+ overlay.innerHTML =
+ '<div class="html-preview-shell">' +
+ '<div class="html-preview-header">' +
+ '<span class="html-preview-title"><span class="material-symbols-outlined">'+svgIcon('eye')+'</span>HTML Preview</span>' +
+ '<div class="html-preview-actions">' +
+ '<button class="html-preview-action" id="hpv-open" aria-label="Open in new tab"><span class="material-symbols-outlined">'+svgIcon('open_in_new')+'</span></button>' +
+ '<button class="html-preview-action" id="hpv-close" aria-label="Close"><span class="material-symbols-outlined">'+svgIcon('close')+'</span></button>' +
+ '</div>' +
+ '</div>' +
+ '<iframe class="html-preview-frame" sandbox="allow-scripts" referrerpolicy="no-referrer"></iframe>' +
+ '</div>';
+ document.body.appendChild(overlay);
+ var iframe = overlay.querySelector('.html-preview-frame');
+ if(iframe){
+ // srcdoc is the most reliable cross-mobile way to push HTML in
+ // without escaping pain. Falls back to data URL if browser blocks.
+ try{ iframe.srcdoc = doc; }
+ catch(_){ iframe.src = dataUrl; }
+ }
+ var openBtn = overlay.querySelector('#hpv-open');
+ if(openBtn) openBtn.onclick = function(){ window.open(dataUrl, '_blank'); };
+ var closeBtn = overlay.querySelector('#hpv-close');
+ if(closeBtn) closeBtn.onclick = function(){ document.body.removeChild(overlay); };
+ };
+
// ── Exposed handlers ──
window._toggleDrawer = function(){
drawerOpen = !drawerOpen;
@@ -1831,18 +2270,30 @@ button{-webkit-appearance:none;appearance:none}
var bd=document.querySelector('.drawer-backdrop'); if(bd) bd.classList.add('open');
drawerOpen = true;
};
- window._toggleThinking = function(){
- if(!isThinkingCompatible(currentModel)) return;
- thinking = !thinking;
- persistState();
- renderShell();
- };
+ // _toggleThinking removed — mobile has no thinking toggle anymore.
+ // Kept as a no-op stub so stale handlers (any lingering cached page)
+ // don't throw when clicked.
+ window._toggleThinking = function(){};
window._toggleAgent = function(){
if(streaming || agentRunning) return;
setAgentEnabled(!getAgentEnabled());
renderShell();
};
window._stopAgent = function(){ agentAbort = true; };
+
+ // Single-button cancel: stops whichever mode is currently running.
+ // Streaming chat → abort the fetch. Agent loop → flip the agentAbort
+ // flag so the next iteration bails cleanly. User can hit this from
+ // the input-bar Send (which flips to Stop while busy) OR from the
+ // header Agent icon (also flips to Stop while an agent is running).
+ window._cancelSend = function(){
+ if(streaming && abortCtrl){
+ try{ abortCtrl.abort(); }catch(_){}
+ }
+ if(agentRunning){
+ agentAbort = true;
+ }
+ };
window._triggerAttach = function(){
var f = el('file-input'); if(f) f.click();
};
@@ -2159,6 +2610,9 @@ button{-webkit-appearance:none;appearance:none}
// content" (user request).
function postChatEvent(role, content){
if(!content) return;
+ // Safety filter: never mirror bare "Continue." to the desktop.
+ // Legacy from the old ReAct loop; kept as defense in depth.
+ if(role === 'user' && /^\s*continue\.?\s*$/i.test(content)) return;
var c = findChat(currentChatId);
if(!c) return;
var mode = c.mode === 'codex' ? 'codex' : 'lu';
@@ -2198,14 +2652,24 @@ button{-webkit-appearance:none;appearance:none}
// Mirror user message to desktop (text only)
postChatEvent('user', text);
- // Agent mode? Spin up ReAct loop instead of plain chat.
- if(getAgentEnabled()){
- runAgentLoop(text);
+ // Agent / Codex mode? Use native Ollama tool calling instead of
+ // plain streaming chat. Codex chats ALWAYS run tools (no toggle),
+ // matching desktop Codex which is agentic by design. Plain LU chats
+ // only run tools when the user toggled Agent on in the header.
+ var isCodexChat = getCurrentMode() === 'codex';
+ if(getAgentEnabled() || isCodexChat){
+ var toolNames = isCodexChat ? CODEX_TOOLS : AGENT_ALL_TOOLS;
+ var sysPrompt = buildSystemPrompt();
+ var kindLabel = isCodexChat ? 'Codex' : 'Agent';
+ runToolLoop(sysPrompt, toolNames, kindLabel);
return;
}
streaming=true;
- el('send-btn').disabled=true;
+ // Re-render the input bar so the Send-Button flips to the red Stop
+ // chip. Without this the user has no visible way to cancel a chat
+ // that hangs on an over-eager thinking model.
+ renderShell();
renderChat();
// Build API messages
@@ -2229,12 +2693,32 @@ button{-webkit-appearance:none;appearance:none}
apiMsgs.push(apiMsg);
}
- var body = {model:currentModel, messages:apiMsgs, stream:true, options:{num_gpu:99}};
- // Tri-state: for thinking-capable models we send explicit true|false.
- // Explicit `false` tells Ollama to SKIP thinking (saves tokens) instead
- // of silently letting the model emit <think> tags we'd then have to
- // hide. Non-thinking models: omit the field entirely (undefined).
- if(isThinkingCompatible(currentModel)) body.think = !!thinking;
+ // Token budget: 16 384 is a balance between "no truncation of the
+ // assistant's visible message" (previous default of 4000 was too low)
+ // and "don't let tagged-thinking models loop forever" (true `-1`
+ // caused Gemma 3/4 to think without ever emitting an answer). 16 k is
+ // enough for the longest reasonable agent reply plus deep thinking.
+ var body = {model:currentModel, messages:apiMsgs, stream:true, options:{num_gpu:99, num_predict:16384}};
+ // Tri-state: for thinking-capable models we normally send explicit
+ // true|false. Explicit `false` tells Ollama to SKIP thinking (saves
+ // tokens) instead of silently letting the model emit <think> tags we'd
+ // then have to hide. Non-thinking models: omit the field entirely.
+ //
+ // Bug #80 parity: Gemma 3/4 with `think:false` drops into PLAIN-TEXT
+ // structured planning ("Plan:" / "Constraint Checklist:" …) that no
+ // tag-stripper can clean. For these models with the toggle OFF we
+ // instead OMIT `think`, which makes Ollama emit tagged thinking that
+ // our stripper handles cleanly. UX is the same (clean answer) — the
+ // trade-off is hidden token spend on internal reasoning.
+ if(isThinkingCompatible(currentModel)){
+ if(thinking){
+ body.think = true;
+ } else if(!isPlainTextPlanner(currentModel)){
+ body.think = false;
+ }
+ // else: leave body.think undefined → Ollama default = tagged thinking,
+ // stripped at render time.
+ }
abortCtrl = new AbortController();
fetch('/api/chat',{
@@ -2264,152 +2748,274 @@ button{-webkit-appearance:none;appearance:none}
});
};
- // ── Agent ReAct loop ────────────────────────────────────────────────
- // Runs a non-streaming chat → parse JSON action → execute tool →
- // observation → next chat. Same shape as desktop useAgentChat.ts.
- // Parity with desktop useAgentChat.ts: pass `think: true` on the
- // `/api/chat` body when the user toggled thinking on AND the model is
- // thinking-compatible. Falls back to non-thinking retry on HTTP 400.
- function agentChatNonStream(messages){
- var body = {model:currentModel, messages:messages, stream:false, options:{num_gpu:99}};
- // Same tri-state as _doSend — explicit true|false for thinking-capable
- // models, field omitted for the rest.
- if(isThinkingCompatible(currentModel)) body.think = !!thinking;
- function go(bodyToSend){
- return fetch('/api/chat',{
- method:'POST',
- headers:{'Content-Type':'application/json','Authorization':'Bearer '+TOKEN},
- body:JSON.stringify(bodyToSend)
- }).then(function(r){
- if(r.status===401){ clearAuthAndReload(); throw new Error('401'); }
- if(!r.ok){
- if(r.status===400 && ('think' in bodyToSend)){
- var retry = {}; for(var k in bodyToSend) retry[k]=bodyToSend[k]; delete retry.think;
- return go(retry);
- }
- return r.text().then(function(t){ throw new Error('HTTP '+r.status+': '+t); });
- }
- return r.json();
- }).then(function(j){
- // Non-stream returns { message: { role, content, thinking? } }
- var content = (j && j.message && typeof j.message.content === 'string') ? j.message.content : '';
- var think = (j && j.message && typeof j.message.thinking === 'string') ? j.message.thinking : '';
- if(!content && j && typeof j.response === 'string') content = j.response;
- // Inline <think>...</think> fallback. Strip from content regardless,
- // but keep the captured text only when the toggle asked for it.
- if(content.indexOf('<think>') >= 0 && content.indexOf('</think>') > content.indexOf('<think>')){
- var s = content.indexOf('<think>'), e = content.indexOf('</think>');
- var inline = content.slice(s+7, e);
- if(inline) think = (think ? think+'\n' : '') + inline;
- content = (content.slice(0,s) + content.slice(e+8)).trim();
- }
- // Thinking visibility is gated by the header toggle — drop it when off.
- var keep = thinking && isThinkingCompatible(currentModel);
- if(!keep) think = '';
- return {content: content, thinking: think};
- });
- }
- return go(body);
- }
-
+ // ── Native tool-calling loop ─────────────────────────────────────
+ //
+ // Replaces the old ReAct JSON loop with Ollama's native /api/chat
+ // `tools` parameter. The model returns structured `tool_calls[]`
+ // instead of freeform JSON we had to parse. Reliability goes from
+ // ~60% to ~99% because the model uses its trained tool-call format
+ // instead of trying to emit valid JSON in its content.
// Append a structured agent step to the current assistant message.
// Steps render as small colored cards ABOVE the bubble; they are NOT
- // part of msg.content, so the next user turn does NOT see ReAct
+ // part of msg.content, so the next user turn does NOT see tool-call
// scaffolding and the model cannot drift into that style.
function appendAgentStep(type, content, meta){
var idx = msgs.length-1;
if(idx < 0 || msgs[idx].role !== 'assistant') return;
if(!Array.isArray(msgs[idx].agentSteps)) msgs[idx].agentSteps = [];
- // All previous steps collapse as a new one arrives; new steps start
- // collapsed too (parity with "tool calls always collapsed by default").
+ // ALL steps always start collapsed (user request). Pulsing CSS stripe
+ // on `.live` rows shows in-flight status; the content itself stays
+ // behind the chevron until the user taps to expand.
for(var p=0; p<msgs[idx].agentSteps.length; p++){ msgs[idx].agentSteps[p].open = false; }
- var step = {type:type, content:content, ts:Date.now(), open:false};
+ var step = {type:type, content:content, ts:Date.now(), open:false, live:false};
if(meta && typeof meta === 'object'){ for(var k in meta) if(Object.prototype.hasOwnProperty.call(meta,k)) step[k]=meta[k]; }
msgs[idx].agentSteps.push(step);
renderChat();
}
- function runAgentLoop(goal){
+ // ── Native tool-calling loop ──
+ //
+ // Uses Ollama's /api/chat with `tools` parameter (non-streaming,
+ // stream:false). The model returns structured `tool_calls[]` that we
+ // execute via the existing runAgentTool() bridge, then feed results
+ // back as `role:'tool'` messages. Loop until the model responds with
+ // no tool_calls (= final answer) or we hit maxIter.
+ //
+ // Why non-streaming? Ollama's tool calling requires stream:false to
+ // return structured tool_calls[]. The trade-off vs the old streaming
+ // ReAct loop: no live token-by-token display, but near-100% tool-call
+ // reliability instead of ~60% JSON parse success.
+ function runToolLoop(systemPrompt, toolNames, kindLabel){
agentRunning = true;
agentAbort = false;
- el('send-btn').disabled = true;
- renderShell(); // show stop button via header state
+ abortCtrl = new AbortController();
+ renderShell(); // flips Send button to Stop
renderChat();
- var history = [];
- var maxIter = 12;
- var i = 0;
- var target = msgs[msgs.length-1]; // the empty assistant slot we pushed
+ var maxIter = 50; // matches desktop v24 AgentBudget default
+ var iter = 0;
+ var target = msgs[msgs.length-1]; // the empty assistant slot
+ var tools = buildToolDefs(toolNames);
+
+ // Build the LLM message history from the conversation.
+ // Include system prompt + all msgs except the last empty assistant.
+ // Hidden messages (tool-call history from previous turns) are included
+ // so the model sees what it did before (continue capability, parity
+ // with original Codex CLI).
+ var apiMessages = [];
+ var apiStartLen = 0; // track where loop-generated messages begin
+ if(systemPrompt) apiMessages.push({role:'system', content:systemPrompt});
+ var cm = getCaveman();
+ for(var k=0; k<msgs.length-1; k++){
+ var m = msgs[k];
+ var content = m.content;
+ if(m.role==='user' && cm!=='off' && CAVEMAN_REMINDERS[cm]){
+ content = CAVEMAN_REMINDERS[cm] + '\n' + content;
+ }
+ var apiMsg = {role: m.role, content: content};
+ // Carry over tool_calls on assistant messages so Ollama sees the
+ // full native tool-call history (assistant→tool pairs).
+ if(m.tool_calls) apiMsg.tool_calls = m.tool_calls;
+ if(m.images && m.images.length){ apiMsg.images = m.images.map(function(im){return im.data;}); }
+ apiMessages.push(apiMsg);
+ }
+
+ apiStartLen = apiMessages.length; // everything after this was added during the loop
+
+ // Show a "thinking" step while the first call is in flight
+ appendAgentStep('thought', kindLabel+' is thinking...', {live:true});
+ var thinkingStepIdx = (target.agentSteps || []).length - 1;
function step(){
if(agentAbort){
- appendAgentStep('error', 'Agent stopped by user.');
- finishAgent(null);
+ appendAgentStep('error', kindLabel+' stopped by user.');
+ finishToolLoop(null);
return;
}
- if(i >= maxIter){
- appendAgentStep('error', 'Agent stopped: max iterations reached.');
- finishAgent(null);
+ if(iter >= maxIter){
+ appendAgentStep('error', kindLabel+' stopped: max iterations reached.');
+ finishToolLoop(null);
return;
}
- i++;
-
- var prompt = buildReActPrompt(goal, history);
- var apiMsgs = [{role:'system', content:prompt}, {role:'user', content: i===1 ? goal : 'Continue.'}];
+ iter++;
+
+ nativeToolChat(apiMessages, tools).then(function(res){
+ // Remove the initial "thinking" placeholder on the first
+ // successful response (only matters for iter === 1).
+ if(thinkingStepIdx >= 0 && target.agentSteps && target.agentSteps[thinkingStepIdx]){
+ target.agentSteps.splice(thinkingStepIdx, 1);
+ thinkingStepIdx = -1;
+ renderChat();
+ }
- agentChatNonStream(apiMsgs).then(function(res){
- // Native/inline thinking goes into the message's thinking field
+ // Handle thinking content (native Ollama field + inline tags)
+ var keepThinking = thinking && isThinkingCompatible(currentModel);
+ var stripped = stripThinkTags(res.content, keepThinking);
+ var turnContent = stripped.content;
+ var turnThinking = stripped.thinking;
+ // Also pick up the native thinking field from Ollama
if(res.thinking){
- if(target){ target.thinking = (target.thinking ? target.thinking+'\n' : '') + res.thinking; target.thinkingOpen = true; }
- renderChat();
+ if(keepThinking){
+ turnThinking = turnThinking ? turnThinking+'\n'+res.thinking : res.thinking;
+ }
}
- var parsed = parseAgentResponse(res.content);
- if(parsed.thought){
- history.push({type:'thought', content:parsed.thought});
- appendAgentStep('thought', parsed.thought);
+ if(turnThinking && target){
+ target.thinking = (target.thinking ? target.thinking+'\n' : '') + turnThinking;
}
- if(parsed.action === 'finish'){
- var ans = parsed.answer || parsed.thought || '(done)';
- history.push({type:'action', content:'finish'});
- finishAgent(ans);
- return;
- }
- if(parsed.action === 'continue' || !parsed.action){
- step();
+
+ // No tool calls → model is done, set final content
+ if(!res.toolCalls || res.toolCalls.length === 0){
+ finishToolLoop(turnContent || '(done)');
return;
}
- var argsPretty = '';
- try{ argsPretty = JSON.stringify(parsed.args||{}); }catch(_){ argsPretty = '{}'; }
- history.push({type:'action', content:parsed.action+' '+argsPretty});
- appendAgentStep('action', '`'+parsed.action+'` '+argsPretty, {toolName:parsed.action, args:parsed.args});
-
- runAgentTool(parsed.action, parsed.args || {}).then(function(observation){
- var obsTrim = String(observation || '');
- if(obsTrim.length > 4000) obsTrim = obsTrim.slice(0,4000)+'\n…(truncated)';
- history.push({type:'observation', content:obsTrim});
- appendAgentStep('observation', obsTrim);
- step();
- }).catch(function(e){
- history.push({type:'error', content:String(e)});
- appendAgentStep('error', String(e));
- step();
+
+ // Push the assistant message with tool_calls into the history
+ // so Ollama sees the proper assistant→tool message pairs.
+ apiMessages.push({
+ role: 'assistant',
+ content: turnContent || '',
+ tool_calls: res.toolCalls.map(function(tc){
+ return {function:{name:tc.function.name, arguments:tc.function.arguments}};
+ })
});
+
+ // Execute each tool call sequentially, show steps in UI
+ var tcIndex = 0;
+ function execNext(){
+ if(agentAbort){
+ appendAgentStep('error', kindLabel+' stopped by user.');
+ finishToolLoop(null);
+ return;
+ }
+ if(tcIndex >= res.toolCalls.length){
+ // All tools executed — loop back for next model turn
+ renderChat();
+ step();
+ return;
+ }
+
+ var tc = res.toolCalls[tcIndex];
+ var toolName = tc.function.name;
+ var toolArgs = tc.function.arguments || {};
+ var argsPretty = '';
+ try{ argsPretty = JSON.stringify(toolArgs); }catch(_){ argsPretty = '{}'; }
+
+ // Show "running" action step
+ appendAgentStep('action', '`'+toolName+'` '+argsPretty, {toolName:toolName, args:toolArgs, live:true});
+ var actionIdx = (target.agentSteps || []).length - 1;
+
+ runAgentTool(toolName, toolArgs).then(function(observation){
+ var obs = String(observation || '');
+
+ // Mark action as completed
+ if(target.agentSteps && target.agentSteps[actionIdx]){
+ target.agentSteps[actionIdx].live = false;
+ }
+ appendAgentStep('observation', obs);
+
+ // Push tool result into the LLM history
+ apiMessages.push({role:'tool', content:obs});
+
+ tcIndex++;
+ execNext();
+ }).catch(function(e){
+ var errMsg = String(e && e.message || e);
+ if(target.agentSteps && target.agentSteps[actionIdx]){
+ target.agentSteps[actionIdx].live = false;
+ }
+ appendAgentStep('error', errMsg);
+
+ // Push error as tool result so the model can adapt
+ apiMessages.push({role:'tool', content:'Error: '+errMsg});
+
+ tcIndex++;
+ execNext();
+ });
+ }
+
+ execNext();
}).catch(function(e){
- appendAgentStep('error', 'Agent chat error: '+(e && e.message || e));
- finishAgent(null);
+ // Remove the thinking placeholder if still present
+ if(thinkingStepIdx >= 0 && target.agentSteps && target.agentSteps[thinkingStepIdx]){
+ target.agentSteps.splice(thinkingStepIdx, 1);
+ thinkingStepIdx = -1;
+ }
+
+ if(e && e.name === 'AbortError'){
+ appendAgentStep('error', 'Stopped.');
+ finishToolLoop(null);
+ return;
+ }
+ var errMsg = (e && e.message) || String(e);
+ if(errMsg === 'TOOLS_NOT_SUPPORTED'){
+ appendAgentStep('error', 'This model does not support tool calling. Pick a tool-capable model (Qwen 3, Llama 3.1+, Gemma 4).');
+ finishToolLoop(null);
+ return;
+ }
+ appendAgentStep('error', kindLabel+' error: '+errMsg);
+ finishToolLoop(null);
});
}
- function finishAgent(finalAnswer){
+ function finishToolLoop(finalAnswer){
agentRunning = false;
agentAbort = false;
- el('send-btn').disabled = false;
- // CRITICAL: only the final answer goes into msg.content. The
- // ReAct scaffolding stays in agentSteps[] where the next user
- // turn won't send it back to the model. This prevents the
- // "style drift" bug where the model keeps emitting JSON
- // thought/action blocks after Agent Mode is toggled off.
- if(target){ target.content = finalAnswer || target.content || ''; }
+ abortCtrl = null;
+
+ // ── Continue capability (parity with original Codex CLI) ────────
+ // Persist the tool-call history from THIS turn as hidden messages
+ // in msgs[]. On the NEXT turn, the history builder (line ~2802)
+ // includes them in apiMessages so the model sees what it did before.
+ // Hidden messages are skipped by renderChat() — the user only sees
+ // the final answer, but the model sees the full tool-call chain.
+ var toolHistory = [];
+ for(var hi = apiStartLen; hi < apiMessages.length; hi++){
+ var am = apiMessages[hi];
+ toolHistory.push(mkMsg(am.role, am.content || '', {
+ hidden: true,
+ tool_calls: am.tool_calls || undefined
+ }));
+ }
+ if(toolHistory.length > 0){
+ // Splice BEFORE target (the visible final-answer message) so
+ // the conversation order is: user → hidden tool chain → answer.
+ var targetIdx = msgs.indexOf(target);
+ if(targetIdx >= 0){
+ // splice(targetIdx, 0, ...items) — Array.prototype.splice.apply
+ // for ES5 compat (mobile JS can't use spread in all engines).
+ var spliceArgs = [targetIdx, 0];
+ for(var si = 0; si < toolHistory.length; si++) spliceArgs.push(toolHistory[si]);
+ Array.prototype.splice.apply(msgs, spliceArgs);
+ }
+ }
+
+ // The visible answer — if finalAnswer is null/empty and no prior
+ // content, build a fallback summary from agent steps so the bubble
+ // is never blank (parity with desktop useCodex.ts fix).
+ var answer = finalAnswer || '';
+ if(!answer && target && !target.content){
+ var steps = target.agentSteps || [];
+ var writes = 0, reads = 0, otherOk = 0, fails = 0;
+ for(var si2 = 0; si2 < steps.length; si2++){
+ var s = steps[si2];
+ if(s.type === 'action'){
+ if(s.toolName === 'file_write') writes++;
+ else if(s.toolName === 'file_read') reads++;
+ else otherOk++;
+ } else if(s.type === 'error' && s.content && s.content.indexOf('stopped') < 0){
+ fails++;
+ }
+ }
+ var summaryParts = [];
+ if(writes) summaryParts.push(writes + ' file(s) written');
+ if(reads) summaryParts.push(reads + ' file(s) read');
+ if(otherOk) summaryParts.push(otherOk + ' other operation(s) completed');
+ if(fails) summaryParts.push(fails + ' operation(s) failed');
+ answer = summaryParts.length > 0
+ ? 'Task completed: ' + summaryParts.join(', ') + '.'
+ : 'Task completed.';
+ }
+ if(target){ target.content = answer || target.content || ''; }
renderShell();
renderChat();
var finalText = target ? (target.content || '') : '';
@@ -2466,14 +3072,20 @@ button{-webkit-appearance:none;appearance:none}
discardedThinkBuf = '';
var target = msgs[msgs.length-1];
// Thinking visibility is driven strictly by the toggle. If the toggle
- // is OFF, ALL thinking tokens (native field AND inline <think> tags)
- // are silently dropped so the UI never shows a think block the user
- // didn't ask for. If the toggle turns ON later, subsequent tokens
- // appear live.
+ // is OFF, ALL thinking tokens (native field AND inline <think> tags
+ // AND non-canonical tags via stripNonCanonicalTags) are silently
+ // dropped so the UI never shows a think block the user didn't ask
+ // for. If the toggle turns ON later, subsequent tokens appear live.
function keepThinkingNow(){ return thinking && isThinkingCompatible(currentModel); }
function pump(){
reader.read().then(function(result){
if(result.done){
+ // Final pass: scrub any non-canonical thinking markers that
+ // slipped past the streaming state-machine (Gemma's channel
+ // marker, orphan <thought>, etc.). Bug #3+#5.
+ if(target){
+ target.content = stripNonCanonicalTags(target.content).trim();
+ }
var finalText = target ? target.content : '';
postChatEvent('assistant', finalText);
finishStream();
@@ -2504,6 +3116,12 @@ button{-webkit-appearance:none;appearance:none}
}
}catch(_){ }
}
+ // Live partial scrub of non-canonical tags (Gemma <|channel|>…).
+ // We re-strip on every chunk so the user never sees a "Plan:"
+ // preamble flash up while the model is still writing the answer.
+ if(target){
+ target.content = stripNonCanonicalTags(target.content);
+ }
renderChat();
pump();
}).catch(function(){finishStream();});
@@ -2513,8 +3131,11 @@ button{-webkit-appearance:none;appearance:none}
function finishStream(){
streaming=false;abortCtrl=null;
- var sb=el('send-btn');if(sb)sb.disabled=false;
syncCurrentChat();
+ // renderShell flips the Send-button back from Stop to Send. Must run
+ // BEFORE renderChat so the keyboard-on-iOS doesn't momentarily see a
+ // disabled button.
+ renderShell();
renderChat();
}
})();
diff --git a/src/api/__tests__/comfyui-integration.test.ts b/src/api/__tests__/comfyui-integration.test.ts
index 7e5210e8..24dfe019 100644
--- a/src/api/__tests__/comfyui-integration.test.ts
+++ b/src/api/__tests__/comfyui-integration.test.ts
@@ -4,7 +4,7 @@
* WITHOUT requiring any real ComfyUI instance or model downloads.
*/
-import { getVideoBundles, CUSTOM_NODE_REGISTRY, type ModelBundle } from '../discover'
+import { getVideoBundles, getImageBundles, CUSTOM_NODE_REGISTRY, type ModelBundle } from '../discover'
import { classifyModel, MODEL_TYPE_DEFAULTS, COMPONENT_REGISTRY as COMFYUI_REGISTRY, isVideoModelType } from '../comfyui'
import { determineStrategy } from '../dynamic-workflow'
import type { CategorizedNodes, AvailableModels } from '../comfyui-nodes'
@@ -182,4 +182,41 @@ describe('COMPONENT_REGISTRY coverage', () => {
const fpVae = COMFYUI_REGISTRY.framepack.vae!
expect(fpVae.matchPatterns).toContain('hunyuan')
})
+
+ it('ERNIE-Image uses flux2 CLIP type and separate VAE/CLIP', () => {
+ const entry = COMFYUI_REGISTRY.ernie_image
+ expect(entry).toBeDefined()
+ expect(entry.loader).toBe('UNETLoader')
+ expect(entry.needsSeparateVAE).toBe(true)
+ expect(entry.needsSeparateCLIP).toBe(true)
+ expect(entry.clipType).toBe('flux2')
+ expect(entry.vae!.matchPatterns).toContain('flux2')
+ expect(entry.clip!.matchPatterns).toContain('ernie-image-prompt-enhancer')
+ })
+})
+
+describe('Full Pipeline: Image bundle → Strategy for ERNIE-Image', () => {
+ const bundles = getImageBundles()
+ const ernieBundles = bundles.filter(b => b.workflow === 'ernie_image')
+ const ernieBundle = ernieBundles.find(b => b.name.includes('Turbo'))
+ const ernieBaseBundle = ernieBundles.find(b => b.name.includes('Base'))
+
+ it('ERNIE-Image has Turbo + Base bundles', () => {
+ expect(ernieBundles).toHaveLength(2)
+ expect(ernieBundle).toBeDefined()
+ expect(ernieBaseBundle).toBeDefined()
+ })
+
+ it('ERNIE-Image Turbo has 4 files (model + ministral + enhancer + vae)', () => {
+ expect(ernieBundle!.files.length).toBe(4)
+ })
+
+ it('ERNIE-Image Base has 4 files (model + ministral + enhancer + vae)', () => {
+ expect(ernieBaseBundle!.files.length).toBe(4)
+ })
+
+ it('ERNIE-Image Turbo has valid strategy when all nodes available', () => {
+ const result = determineStrategy('ernie_image' as any, false, allNodesAvailable(), defaultModels)
+ expect(result.strategy).toBe('unet_ernie_image')
+ })
})
diff --git a/src/api/__tests__/comfyui-models.test.ts b/src/api/__tests__/comfyui-models.test.ts
index c8ebd0e7..a344b22f 100644
--- a/src/api/__tests__/comfyui-models.test.ts
+++ b/src/api/__tests__/comfyui-models.test.ts
@@ -82,6 +82,11 @@ describe('classifyModel', () => {
expect(classifyModel('zimage_base.safetensors')).toBe('zimage')
})
+ it('classifies ERNIE-Image models', () => {
+ expect(classifyModel('ernie-image-turbo.safetensors')).toBe('ernie_image')
+ expect(classifyModel('ernie_image_turbo_bf16.safetensors')).toBe('ernie_image')
+ })
+
it('returns unknown for unrecognized models', () => {
expect(classifyModel('totally_custom_model.safetensors')).toBe('unknown')
})
@@ -91,7 +96,7 @@ describe('classifyModel', () => {
describe('isVideoModelType', () => {
const videoTypes = ['wan', 'hunyuan', 'ltx', 'mochi', 'cosmos', 'cogvideo', 'svd', 'framepack', 'pyramidflow', 'allegro'] as const
- const imageTypes = ['flux', 'flux2', 'zimage', 'sdxl', 'sd15', 'unknown'] as const
+ const imageTypes = ['flux', 'flux2', 'zimage', 'ernie_image', 'sdxl', 'sd15', 'unknown'] as const
for (const t of videoTypes) {
it(`${t} is a video model type`, () => {
@@ -139,7 +144,7 @@ describe('MODEL_TYPE_DEFAULTS', () => {
// ─── COMPONENT_REGISTRY ───
describe('COMPONENT_REGISTRY', () => {
- const allTypes = ['sd15', 'sdxl', 'flux', 'flux2', 'zimage', 'wan', 'hunyuan', 'ltx', 'mochi', 'cosmos', 'cogvideo', 'svd', 'framepack', 'pyramidflow', 'allegro', 'unknown']
+ const allTypes = ['sd15', 'sdxl', 'flux', 'flux2', 'zimage', 'ernie_image', 'wan', 'hunyuan', 'ltx', 'mochi', 'cosmos', 'cogvideo', 'svd', 'framepack', 'pyramidflow', 'allegro', 'unknown']
for (const t of allTypes) {
it(`${t} has a registry entry`, () => {
diff --git a/src/api/__tests__/comfyui-workflows.test.ts b/src/api/__tests__/comfyui-workflows.test.ts
index c46ed9f8..9ed12a81 100644
--- a/src/api/__tests__/comfyui-workflows.test.ts
+++ b/src/api/__tests__/comfyui-workflows.test.ts
@@ -58,6 +58,7 @@ describe('determineStrategy — all 15 model types', () => {
['flux', 'unet_flux'],
['flux2', 'unet_flux2'],
['zimage', 'unet_zimage'],
+ ['ernie_image', 'unet_ernie_image'],
]
for (const [modelType, expectedStrategy] of strategyMap) {
diff --git a/src/api/__tests__/discover-registry.test.ts b/src/api/__tests__/discover-registry.test.ts
index 746a1ce5..bd4cd172 100644
--- a/src/api/__tests__/discover-registry.test.ts
+++ b/src/api/__tests__/discover-registry.test.ts
@@ -275,7 +275,7 @@ describe('discover — data validation', () => {
describe('COMPONENT_REGISTRY', () => {
it('has entries for all expected model types', () => {
const expectedTypes = [
- 'sd15', 'sdxl', 'flux', 'flux2', 'zimage', 'wan', 'hunyuan',
+ 'sd15', 'sdxl', 'flux', 'flux2', 'zimage', 'ernie_image', 'wan', 'hunyuan',
'ltx', 'mochi', 'cosmos', 'cogvideo', 'svd', 'framepack',
'pyramidflow', 'allegro', 'unknown',
]
@@ -298,7 +298,7 @@ describe('discover — data validation', () => {
})
it('UNET-based types that need separate VAE have vae spec with downloadUrl', () => {
- const typesWithVAE = ['flux', 'flux2', 'zimage', 'wan', 'hunyuan', 'mochi', 'cosmos', 'cogvideo', 'framepack', 'pyramidflow']
+ const typesWithVAE = ['flux', 'flux2', 'zimage', 'ernie_image', 'wan', 'hunyuan', 'mochi', 'cosmos', 'cogvideo', 'framepack', 'pyramidflow']
for (const t of typesWithVAE) {
const spec = COMPONENT_REGISTRY[t]
if (spec.needsSeparateVAE && spec.vae) {
@@ -311,7 +311,7 @@ describe('discover — data validation', () => {
})
it('UNET-based types that need separate CLIP have clip spec with downloadUrl', () => {
- const typesWithCLIP = ['flux', 'flux2', 'zimage', 'wan', 'hunyuan', 'ltx', 'mochi', 'cosmos', 'cogvideo', 'framepack']
+ const typesWithCLIP = ['flux', 'flux2', 'zimage', 'ernie_image', 'wan', 'hunyuan', 'ltx', 'mochi', 'cosmos', 'cogvideo', 'framepack']
for (const t of typesWithCLIP) {
const spec = COMPONENT_REGISTRY[t]
if (spec.needsSeparateCLIP && spec.clip) {
diff --git a/src/api/comfyui-nodes.ts b/src/api/comfyui-nodes.ts
index 0d0bd52c..0e906f68 100644
--- a/src/api/comfyui-nodes.ts
+++ b/src/api/comfyui-nodes.ts
@@ -112,6 +112,8 @@ export function categorizeNodes(allNodes: Record<string, NodeMetadata>): Categor
EmptyMochiLatentVideo: 'latentInit',
EmptyCosmosLatentVideo: 'latentInit',
CogVideoXEmptyLatents: 'latentInit',
+ // Conditioning
+ ConditioningZeroOut: 'textEncoders',
// Text encoding
CLIPTextEncode: 'textEncoders',
CLIPTextEncodeSDXL: 'textEncoders',
diff --git a/src/api/comfyui.ts b/src/api/comfyui.ts
index 7834fbd5..296bb2b8 100644
--- a/src/api/comfyui.ts
+++ b/src/api/comfyui.ts
@@ -30,7 +30,7 @@ export interface ComfyUIOutput {
type: string
}
-export type ModelType = 'flux' | 'flux2' | 'zimage' | 'sdxl' | 'sd15' | 'wan' | 'hunyuan' | 'ltx' | 'mochi' | 'cosmos' | 'cogvideo' | 'svd' | 'framepack' | 'pyramidflow' | 'allegro' | 'unknown'
+export type ModelType = 'flux' | 'flux2' | 'zimage' | 'ernie_image' | 'sdxl' | 'sd15' | 'wan' | 'hunyuan' | 'ltx' | 'mochi' | 'cosmos' | 'cogvideo' | 'svd' | 'framepack' | 'pyramidflow' | 'allegro' | 'unknown'
export type VideoBackend = 'wan' | 'animatediff' | 'none'
export interface ClassifiedModel {
@@ -75,6 +75,9 @@ export function classifyModel(name: string): ModelType {
if (lower.includes('hunyuan')) return 'hunyuan'
if (lower.includes('ltx')) return 'ltx'
+ // ERNIE-Image (Baidu, uses flux2 CLIP type + ConditioningZeroOut for negative)
+ if (lower.includes('ernie-image') || lower.includes('ernie_image')) return 'ernie_image'
+
// Z-Image (uses qwen_image CLIP type, NOT flux2 — different embedding dimensions)
if (lower.includes('z_image') || lower.includes('z-image') || lower.includes('zimage')) return 'zimage'
if (lower.includes('flux-2') || lower.includes('flux2')) return 'flux2'
@@ -99,7 +102,7 @@ export function classifyModel(name: string): ModelType {
}
export function isImageModelType(type: ModelType): boolean {
- return type === 'flux' || type === 'flux2' || type === 'zimage' || type === 'sdxl' || type === 'sd15' || type === 'unknown'
+ return type === 'flux' || type === 'flux2' || type === 'zimage' || type === 'ernie_image' || type === 'sdxl' || type === 'sd15' || type === 'unknown'
}
export function isVideoModelType(type: ModelType): boolean {
@@ -140,6 +143,8 @@ export const MODEL_TYPE_DEFAULTS: Record<string, ModelTypeDefaults> = {
animatediff: { steps: 20, cfg: 7.5, sampler: 'euler_ancestral', scheduler: 'normal', width: 512, height: 512, frames: 16, fps: 8 },
// AnimateDiff Lightning override (4 steps only)
animatediff_lightning: { steps: 4, cfg: 1.0, sampler: 'euler', scheduler: 'sgm_uniform', width: 512, height: 512, frames: 16, fps: 8 },
+ // ERNIE-Image Turbo (Baidu 8B DiT)
+ ernie_image: { steps: 8, cfg: 1, sampler: 'euler', scheduler: 'simple', width: 1024, height: 1024, frames: 1, fps: 1 },
}
// ─── Component Requirements per model type ───
@@ -177,6 +182,11 @@ export const COMPONENT_REGISTRY: Record<string, ComponentRequirements> = {
vae: { matchPatterns: ['ae', 'flux'], downloadFilename: 'ae.safetensors' },
clip: { matchPatterns: ['qwen_3_4b', 'qwen3'], downloadFilename: 'qwen_3_4b.safetensors' },
},
+ ernie_image: {
+ loader: 'UNETLoader', needsSeparateVAE: true, needsSeparateCLIP: true, clipType: 'flux2',
+ vae: { matchPatterns: ['flux2-vae', 'flux2', 'flux'], downloadFilename: 'flux2-vae.safetensors' },
+ clip: { matchPatterns: ['ministral-3-3b', 'ministral', 'ernie-image-prompt-enhancer'], downloadFilename: 'ministral-3-3b.safetensors' },
+ },
wan: {
loader: 'UNETLoader', needsSeparateVAE: true, needsSeparateCLIP: true, clipType: 'wan',
vae: { matchPatterns: ['wan', 'hunyuan'], downloadFilename: 'wan_2.1_vae.safetensors' },
@@ -494,7 +504,7 @@ export async function findMatchingVAE(modelType: ModelType): Promise<string> {
if (match) return match
throw new Error(`No Z-Image VAE found. Download "ae.safetensors" from the Model Manager.`)
}
- if (modelType === 'flux' || modelType === 'flux2') {
+ if (modelType === 'flux' || modelType === 'flux2' || modelType === 'ernie_image') {
const match = vaes.find(v => lower(v).includes('flux2') || lower(v).includes('flux'))
|| vaes.find(v => lower(v).includes('ae'))
if (match) return match
@@ -548,10 +558,23 @@ export async function findMatchingVAE(modelType: ModelType): Promise<string> {
return vaes[0]
}
-export async function findMatchingCLIP(modelType: ModelType): Promise<string> {
+/**
+ * Pick the right text encoder for a model.
+ *
+ * @param modelType — ModelType from `classifyModel`.
+ * @param activeModelName — optional filename of the UNet/checkpoint the
+ * user selected. Enables quantisation-aware pairing: e.g. fp4 FLUX 2
+ * models get the fp4-matched Qwen encoder, fp8/bf16 FLUX 2 models get
+ * the full-precision Qwen encoder. When omitted (legacy callers), we
+ * fall back to the full-precision variant, which is what most users
+ * want.
+ */
+export async function findMatchingCLIP(modelType: ModelType, activeModelName?: string): Promise<string> {
const clips = await getCLIPModels()
if (clips.length === 0) throw new Error('No text encoder models found. Download a CLIP/T5 model for your model type from the Model Manager.')
const lower = (s: string) => s.toLowerCase()
+ const modelLc = activeModelName ? lower(activeModelName) : ''
+ const modelIsFp4 = /fp4|nf4/.test(modelLc)
if (modelType === 'zimage') {
// Z-Image uses qwen_3_4b.safetensors (NOT the fp4_flux2 variant — different embedding dimensions!)
@@ -562,11 +585,31 @@ export async function findMatchingCLIP(modelType: ModelType): Promise<string> {
throw new Error(`No Z-Image text encoder found. Download "qwen_3_4b.safetensors" from the Model Manager.`)
}
if (modelType === 'flux2') {
- // FLUX 2 uses Qwen or Mistral text encoders, NOT T5
- const match = clips.find(c => lower(c).includes('qwen') && !lower(c).includes('qwen_2.5_vl'))
- || clips.find(c => lower(c).includes('mistral'))
+ // FLUX 2 uses Qwen 3 4B (NOT T5). The encoder file comes in two
+ // quantisation tiers that ARE NOT interchangeable:
+ // - `qwen_3_4b.safetensors` → normal / bf16 / fp8 models
+ // - `qwen_3_4b_fp4_flux2.safetensors` → fp4 / nf4 quantised models
+ // Using the wrong one can work (same embedding dim) but noticeably
+ // degrades prompt adherence, so we pair them by inspecting the
+ // filename of the active UNet (see `modelIsFp4` above). Fallback
+ // order ensures we never hard-fail when the "ideal" encoder isn't
+ // installed: we try the paired one first, then the other.
+ const qwenFp4 = clips.find(c => lower(c).includes('qwen') && (lower(c).includes('fp4') || lower(c).includes('nf4')) && !lower(c).includes('qwen_2.5_vl'))
+ const qwenFull = clips.find(c => lower(c).includes('qwen_3_4b') && !lower(c).includes('fp4') && !lower(c).includes('nf4') && !lower(c).includes('vl'))
+ const qwenAny = clips.find(c => lower(c).includes('qwen') && !lower(c).includes('qwen_2.5_vl'))
+ const mistral = clips.find(c => lower(c).includes('mistral'))
+ const match = modelIsFp4
+ ? (qwenFp4 || qwenFull || qwenAny || mistral)
+ : (qwenFull || qwenAny || qwenFp4 || mistral)
+ if (match) return match
+ const wanted = modelIsFp4 ? 'qwen_3_4b_fp4_flux2.safetensors (fp4 FLUX 2)' : 'qwen_3_4b.safetensors (fp8/bf16 FLUX 2)'
+ throw new Error(`No FLUX 2 text encoder found. Download "${wanted}" from the Model Manager.`)
+ }
+ if (modelType === 'ernie_image') {
+ // ERNIE-Image uses its own prompt enhancer text encoder
+ const match = clips.find(c => lower(c).includes('ernie-image-prompt-enhancer') || lower(c).includes('ernie'))
if (match) return match
- throw new Error(`No FLUX 2 text encoder found. Download "qwen_3_4b_fp4_flux2.safetensors" from the Model Manager.`)
+ throw new Error(`No ERNIE-Image text encoder found. Download "ernie-image-prompt-enhancer.safetensors" from the Model Manager.`)
}
if (modelType === 'flux') {
const match = clips.find(c => lower(c).includes('t5') && !lower(c).includes('umt5'))
diff --git a/src/api/discover.ts b/src/api/discover.ts
index 134b4617..240de066 100644
--- a/src/api/discover.ts
+++ b/src/api/discover.ts
@@ -290,6 +290,12 @@ export const COMPONENT_REGISTRY: Record<string, ComponentRequirements> = {
clip: { patterns: ['qwen_3_4b', 'qwen3'], downloadName: 'qwen_3_4b.safetensors', downloadUrl: 'https://huggingface.co/Comfy-Org/z_image_turbo/resolve/main/split_files/text_encoders/qwen_3_4b.safetensors', subfolder: 'text_encoders' },
needsSeparateVAE: true, needsSeparateCLIP: true,
},
+ ernie_image: {
+ loader: 'UNETLoader',
+ vae: { patterns: ['flux2-vae', 'flux2', 'flux'], downloadName: 'flux2-vae.safetensors', downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/vae/flux2-vae.safetensors', subfolder: 'vae' },
+ clip: { patterns: ['ministral-3-3b', 'ministral', 'ernie-image-prompt-enhancer'], downloadName: 'ministral-3-3b.safetensors', downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/text_encoders/ministral-3-3b.safetensors', subfolder: 'text_encoders' },
+ needsSeparateVAE: true, needsSeparateCLIP: true,
+ },
wan: {
loader: 'UNETLoader',
vae: { patterns: ['wan'], downloadName: 'wan_2.1_vae.safetensors', downloadUrl: 'https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/resolve/main/split_files/vae/wan_2.1_vae.safetensors', subfolder: 'vae' },
@@ -839,6 +845,88 @@ export function getImageBundles(): ModelBundle[] {
},
],
},
+ {
+ name: 'ERNIE-Image Turbo',
+ description: 'Baidu ERNIE-Image Turbo — 8B DiT, 8 steps, 1024x1024. Fastest ERNIE variant with Ministral-3B encoder + Prompt Enhancer.',
+ tags: ['ernie_image', 'Image', '1024x1024'],
+ uncensored: false,
+ verified: true,
+ totalSizeGB: 28.9,
+ vramRequired: '24 GB',
+ workflow: 'ernie_image',
+ url: 'https://huggingface.co/Comfy-Org/ERNIE-Image',
+ files: [
+ {
+ name: 'ERNIE-Image Turbo (DiT 8B)',
+ description: 'Baidu ERNIE-Image Turbo diffusion model. 8 steps, fast inference.',
+ pulls: '', tags: ['Diffusion Model', '15.0 GB'], updated: 'New',
+ downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/diffusion_models/ernie-image-turbo.safetensors',
+ filename: 'ernie-image-turbo.safetensors', subfolder: 'diffusion_models', sizeGB: 15.0,
+ },
+ {
+ name: 'Ministral-3-3B Text Encoder',
+ description: 'Main text encoder (Ministral-3B) for ERNIE-Image prompt understanding.',
+ pulls: '', tags: ['Text Encoder', '7.2 GB'], updated: 'New',
+ downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/text_encoders/ministral-3-3b.safetensors',
+ filename: 'ministral-3-3b.safetensors', subfolder: 'text_encoders', sizeGB: 7.2,
+ },
+ {
+ name: 'ERNIE Prompt Enhancer',
+ description: 'Optional prompt enhancer that expands short prompts into richer descriptions.',
+ pulls: '', tags: ['Text Encoder', '6.4 GB'], updated: 'New',
+ downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/text_encoders/ernie-image-prompt-enhancer.safetensors',
+ filename: 'ernie-image-prompt-enhancer.safetensors', subfolder: 'text_encoders', sizeGB: 6.4,
+ },
+ {
+ name: 'FLUX 2 VAE',
+ description: 'Required autoencoder — shared with FLUX 2.',
+ pulls: '', tags: ['VAE', '335 MB'], updated: '',
+ downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/vae/flux2-vae.safetensors',
+ filename: 'flux2-vae.safetensors', subfolder: 'vae', sizeGB: 0.3,
+ },
+ ],
+ },
+ {
+ name: 'ERNIE-Image Base',
+ description: 'Baidu ERNIE-Image Base — 8B DiT, 50 steps, 1024x1024. Highest quality ERNIE variant.',
+ tags: ['ernie_image', 'Image', '1024x1024'],
+ uncensored: false,
+ verified: true,
+ totalSizeGB: 28.9,
+ vramRequired: '24 GB',
+ workflow: 'ernie_image',
+ url: 'https://huggingface.co/Comfy-Org/ERNIE-Image',
+ files: [
+ {
+ name: 'ERNIE-Image Base (DiT 8B)',
+ description: 'Baidu ERNIE-Image Base diffusion model. 50 steps, highest quality.',
+ pulls: '', tags: ['Diffusion Model', '15.0 GB'], updated: 'New',
+ downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/diffusion_models/ernie-image.safetensors',
+ filename: 'ernie-image.safetensors', subfolder: 'diffusion_models', sizeGB: 15.0,
+ },
+ {
+ name: 'Ministral-3-3B Text Encoder',
+ description: 'Main text encoder (Ministral-3B) for ERNIE-Image prompt understanding.',
+ pulls: '', tags: ['Text Encoder', '7.2 GB'], updated: 'New',
+ downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/text_encoders/ministral-3-3b.safetensors',
+ filename: 'ministral-3-3b.safetensors', subfolder: 'text_encoders', sizeGB: 7.2,
+ },
+ {
+ name: 'ERNIE Prompt Enhancer',
+ description: 'Optional prompt enhancer that expands short prompts into richer descriptions.',
+ pulls: '', tags: ['Text Encoder', '6.4 GB'], updated: 'New',
+ downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/text_encoders/ernie-image-prompt-enhancer.safetensors',
+ filename: 'ernie-image-prompt-enhancer.safetensors', subfolder: 'text_encoders', sizeGB: 6.4,
+ },
+ {
+ name: 'FLUX 2 VAE',
+ description: 'Required autoencoder — shared with FLUX 2.',
+ pulls: '', tags: ['VAE', '335 MB'], updated: '',
+ downloadUrl: 'https://huggingface.co/Comfy-Org/ERNIE-Image/resolve/main/vae/flux2-vae.safetensors',
+ filename: 'flux2-vae.safetensors', subfolder: 'vae', sizeGB: 0.3,
+ },
+ ],
+ },
]
}
diff --git a/src/api/dynamic-workflow.ts b/src/api/dynamic-workflow.ts
index c3cf0e42..759c7ae1 100644
--- a/src/api/dynamic-workflow.ts
+++ b/src/api/dynamic-workflow.ts
@@ -15,6 +15,7 @@ export type WorkflowStrategy =
| 'unet_flux' // FLUX 1: UNETLoader + CLIPLoader + VAELoader + EmptySD3LatentImage
| 'unet_flux2' // FLUX 2: UNETLoader + CLIPLoader + VAELoader + EmptyFlux2LatentImage
| 'unet_zimage' // Z-Image: UNETLoader + CLIPLoader(qwen_image) + VAELoader + EmptySD3LatentImage
+ | 'unet_ernie_image' // ERNIE-Image: UNETLoader + CLIPLoader(flux2) + VAELoader + EmptyFlux2LatentImage + ConditioningZeroOut
| 'unet_video' // Wan/Hunyuan: UNETLoader + CLIPLoader + VAELoader + EmptyHunyuanLatentVideo
| 'unet_ltx' // LTX Video: UNETLoader + CLIPLoader + EmptyLTXVLatentVideo
| 'unet_mochi' // Mochi: UNETLoader + CLIPLoader + VAELoader + EmptyMochiLatentVideo
@@ -45,6 +46,14 @@ export function determineStrategy(
const hasVAELoader = nodes.loaders.includes('VAELoader')
const hasAnimateDiff = nodes.motion.includes('ADE_LoadAnimateDiffModel')
+ // ERNIE-Image → UNET + CLIPLoader(flux2) + VAE + Flux2LatentImage + ConditioningZeroOut
+ if (modelType === 'ernie_image') {
+ if (hasUNET && hasCLIPLoader && hasVAELoader) {
+ return { strategy: 'unet_ernie_image', reason: 'ERNIE-Image model → UNETLoader + CLIPLoader(flux2) + ConditioningZeroOut' }
+ }
+ return { strategy: 'unavailable', reason: 'ERNIE-Image requires UNETLoader + CLIPLoader + VAELoader nodes' }
+ }
+
// Z-Image → UNET + CLIPLoader(qwen_image) + VAE + SD3LatentImage
if (modelType === 'zimage') {
if (hasUNET && hasCLIPLoader && hasVAELoader) {
@@ -232,13 +241,14 @@ export async function buildDynamicWorkflow(
vaeOutputSlot = 2
samplerModelId = modelNodeId
- } else if (strategy === 'unet_flux' || strategy === 'unet_flux2' || strategy === 'unet_zimage' || strategy === 'unet_video' || strategy === 'unet_ltx'
+ } else if (strategy === 'unet_flux' || strategy === 'unet_flux2' || strategy === 'unet_zimage' || strategy === 'unet_ernie_image' || strategy === 'unet_video' || strategy === 'unet_ltx'
|| strategy === 'unet_mochi' || strategy === 'unet_cosmos') {
// Separate loaders
const unetId = String(n++)
const clipId = String(n++)
const clipType = type === 'zimage' ? 'qwen_image'
+ : type === 'ernie_image' ? 'flux2'
: type === 'flux2' ? 'flux2'
: type === 'flux' ? 'flux'
: type === 'ltx' ? 'ltxv'
@@ -249,7 +259,10 @@ export async function buildDynamicWorkflow(
let vae: string, clip: string
try { vae = await findMatchingVAE(type) } catch { vae = models.vaes[0] || '' }
- try { clip = await findMatchingCLIP(type) } catch { clip = models.clips[0] || '' }
+ // Pass the active UNet filename so the resolver can prefer the matching
+ // quantisation tier (fp4 model → fp4 encoder; fp8/bf16 model → full-
+ // precision encoder). See findMatchingCLIP's flux2 branch.
+ try { clip = await findMatchingCLIP(type, params.model) } catch { clip = models.clips[0] || '' }
workflow[unetId] = {
class_type: 'UNETLoader',
@@ -325,12 +338,21 @@ export async function buildDynamicWorkflow(
class_type: 'CLIPTextEncode',
inputs: { text: params.prompt, clip: [clipSourceId, clipOutputSlot] },
}
- workflow[negId] = {
- class_type: 'CLIPTextEncode',
- inputs: {
- text: params.negativePrompt || '',
- clip: [clipSourceId, clipOutputSlot],
- },
+
+ if (strategy === 'unet_ernie_image') {
+ // ERNIE-Image uses ConditioningZeroOut for negative (NOT CLIPTextEncode)
+ workflow[negId] = {
+ class_type: 'ConditioningZeroOut',
+ inputs: { conditioning: [posId, 0] },
+ }
+ } else {
+ workflow[negId] = {
+ class_type: 'CLIPTextEncode',
+ inputs: {
+ text: params.negativePrompt || '',
+ clip: [clipSourceId, clipOutputSlot],
+ },
+ }
}
// ─── Phase 3: Latent Initialization ───
@@ -381,8 +403,8 @@ export async function buildDynamicWorkflow(
class_type: 'EmptyLTXVLatentVideo',
inputs: { width: params.width, height: params.height, length: videoParams.frames, batch_size: 1 },
}
- } else if (strategy === 'unet_flux2') {
- // FLUX 2 uses its own latent node
+ } else if (strategy === 'unet_flux2' || strategy === 'unet_ernie_image') {
+ // FLUX 2 / ERNIE-Image use Flux2 latent node
const latentNode = nodes.latentInit.includes('EmptyFlux2LatentImage')
? 'EmptyFlux2LatentImage'
: 'EmptySD3LatentImage'
diff --git a/src/api/preflight.ts b/src/api/preflight.ts
index ab9032c2..f241978f 100644
--- a/src/api/preflight.ts
+++ b/src/api/preflight.ts
@@ -94,7 +94,7 @@ export async function preflightCheck(
}
}
- const needsUnet = modelType === 'flux' || modelType === 'flux2' || modelType === 'zimage' || modelType === 'wan' || modelType === 'hunyuan'
+ const needsUnet = modelType === 'flux' || modelType === 'flux2' || modelType === 'zimage' || modelType === 'ernie_image' || modelType === 'wan' || modelType === 'hunyuan'
|| modelType === 'ltx' || modelType === 'mochi' || modelType === 'cosmos'
|| modelType === 'framepack'
if (needsUnet) {
diff --git a/src/components/chat/CodexView.tsx b/src/components/chat/CodexView.tsx
index d0d5ea83..9f7940e7 100644
--- a/src/components/chat/CodexView.tsx
+++ b/src/components/chat/CodexView.tsx
@@ -71,7 +71,7 @@ export function CodexView() {
</div>
) : (
<div className="py-1">
- {messages.map((msg) => {
+ {messages.filter(msg => !msg.hidden).map((msg) => {
const cleanContent = msg.content ? stripChannelTags(msg.content) : ''
return (
<div
diff --git a/src/components/chat/MessageList.tsx b/src/components/chat/MessageList.tsx
index 9f24b8bc..82bc97a7 100644
--- a/src/components/chat/MessageList.tsx
+++ b/src/components/chat/MessageList.tsx
@@ -24,7 +24,7 @@ export function MessageList({ isGenerating, isLoadingModel, onRegenerate, onEdit
return (
<div ref={scrollRef} className="flex-1 overflow-y-auto scrollbar-thin py-4">
{conversation.messages
- .filter((m) => m.role !== 'system')
+ .filter((m) => m.role !== 'system' && !m.hidden)
.map((message) => (
<MessageBubble
key={message.id}
diff --git a/src/hooks/__tests__/useCodex-path-resolution.test.ts b/src/hooks/__tests__/useCodex-path-resolution.test.ts
new file mode 100644
index 00000000..fc9926b1
--- /dev/null
+++ b/src/hooks/__tests__/useCodex-path-resolution.test.ts
@@ -0,0 +1,133 @@
+/**
+ * Regression tests for the Codex path-resolution bug.
+ *
+ * Before the fix in useCodex.ts, the absolute-path check was:
+ * !p.startsWith('/') && !p.startsWith('C:') && !p.startsWith('\\\\')
+ * which incorrectly treated `D:/foo`, `E:/bar` (any non-C: drive) as RELATIVE
+ * and prepended workDir, producing the doubled-path bug:
+ * workDir=D:/Pictures/foo, p=D:/Pictures/foo/x.html →
+ * D:/Pictures/foo/D:/Pictures/foo/x.html
+ *
+ * After the fix the check is:
+ * /^[a-zA-Z]:[/\\]/.test(p) || p.startsWith('/') || p.startsWith('\\\\')
+ * which recognises ALL drive letters as absolute.
+ *
+ * These tests exercise the absolute-path detection logic in isolation so we
+ * never regress into the broken C:-only check again.
+ */
+import { describe, it, expect } from 'vitest'
+
+// Standalone copy of the fixed absolute-path predicate — must stay in sync
+// with useCodex.ts. The drift-detection test at the bottom re-reads the hook
+// source to confirm parity.
+function isAbsolutePath(p: string): boolean {
+ return /^[a-zA-Z]:[/\\]/.test(p) || p.startsWith('/') || p.startsWith('\\\\')
+}
+
+// Mirrors the workDir-prepend block in useCodex.ts so we can assert on
+// end-to-end behaviour (given workDir + path, what is the resolved path?).
+function resolveCodexPath(p: string, workDir: string): string {
+ if (isAbsolutePath(p) || !workDir) return p
+ return workDir.replace(/\\/g, '/') + '/' + p
+}
+
+describe('useCodex absolute-path detection', () => {
+ it('treats C: drive as absolute', () => {
+ expect(isAbsolutePath('C:/foo/bar.txt')).toBe(true)
+ expect(isAbsolutePath('C:\\foo\\bar.txt')).toBe(true)
+ })
+
+ it('treats D: drive as absolute (regression — previously broken)', () => {
+ expect(isAbsolutePath('D:/Pictures/foo/bar.txt')).toBe(true)
+ expect(isAbsolutePath('D:\\Pictures\\foo\\bar.txt')).toBe(true)
+ })
+
+ it('treats all other drive letters as absolute', () => {
+ expect(isAbsolutePath('E:/foo')).toBe(true)
+ expect(isAbsolutePath('F:/foo')).toBe(true)
+ expect(isAbsolutePath('Z:/foo')).toBe(true)
+ })
+
+ it('treats lowercase drive letters as absolute', () => {
+ expect(isAbsolutePath('d:/foo/bar.txt')).toBe(true)
+ expect(isAbsolutePath('e:\\foo')).toBe(true)
+ })
+
+ it('treats Unix-absolute paths as absolute', () => {
+ expect(isAbsolutePath('/etc/passwd')).toBe(true)
+ expect(isAbsolutePath('/home/user/x.txt')).toBe(true)
+ })
+
+ it('treats UNC paths as absolute', () => {
+ expect(isAbsolutePath('\\\\server\\share\\file.txt')).toBe(true)
+ })
+
+ it('treats relative paths as NOT absolute', () => {
+ expect(isAbsolutePath('./foo.txt')).toBe(false)
+ expect(isAbsolutePath('foo/bar.txt')).toBe(false)
+ expect(isAbsolutePath('subdir/file.txt')).toBe(false)
+ expect(isAbsolutePath('../up/x.txt')).toBe(false)
+ })
+
+ it('treats strings that LOOK like drive refs but are not, as relative', () => {
+ // `X:filename` (no slash after colon) is ambiguous; we require a slash
+ expect(isAbsolutePath('C:foo')).toBe(false)
+ expect(isAbsolutePath('label:value')).toBe(false)
+ })
+})
+
+describe('useCodex resolveCodexPath (end-to-end path doubling regression)', () => {
+ const workDir = 'D:/Pictures/UbisoftConnect'
+
+ it('does NOT double a D:/ path under a D:/ workDir (the user bug)', () => {
+ const resolved = resolveCodexPath('D:/Pictures/UbisoftConnect/index.html', workDir)
+ expect(resolved).toBe('D:/Pictures/UbisoftConnect/index.html')
+ // The broken behaviour would have produced:
+ // D:/Pictures/UbisoftConnect/D:/Pictures/UbisoftConnect/index.html
+ expect(resolved).not.toContain('UbisoftConnect/D:')
+ })
+
+ it('does NOT double a C:/ path under a D:/ workDir', () => {
+ const resolved = resolveCodexPath('C:/Windows/x.dll', workDir)
+ expect(resolved).toBe('C:/Windows/x.dll')
+ })
+
+ it('still prepends workDir for relative paths', () => {
+ expect(resolveCodexPath('index.html', workDir)).toBe('D:/Pictures/UbisoftConnect/index.html')
+ expect(resolveCodexPath('sub/file.txt', workDir)).toBe('D:/Pictures/UbisoftConnect/sub/file.txt')
+ })
+
+ it('leaves absolute Unix paths alone even with workDir set', () => {
+ expect(resolveCodexPath('/tmp/x.txt', workDir)).toBe('/tmp/x.txt')
+ })
+
+ it('leaves UNC paths alone even with workDir set', () => {
+ expect(resolveCodexPath('\\\\server\\share\\x.txt', workDir)).toBe('\\\\server\\share\\x.txt')
+ })
+
+ it('with no workDir, returns path as-is', () => {
+ expect(resolveCodexPath('foo.txt', '')).toBe('foo.txt')
+ expect(resolveCodexPath('D:/foo.txt', '')).toBe('D:/foo.txt')
+ })
+})
+
+// ────────────────────────────────────────────────────────────────────────
+// Drift detection — re-read useCodex.ts and assert it contains the updated
+// regex (not the old C:-only check).
+// ────────────────────────────────────────────────────────────────────────
+import { readFileSync } from 'fs'
+import { fileURLToPath } from 'url'
+import { dirname, join } from 'path'
+
+describe('useCodex path-resolution drift detection', () => {
+ it('matches the absolute-path regex currently in useCodex.ts', () => {
+ const __filename = fileURLToPath(import.meta.url)
+ const __dirname = dirname(__filename)
+ const src = readFileSync(join(__dirname, '../useCodex.ts'), 'utf8')
+
+ // The hook must contain the updated regex (not the old C:-only check)
+ expect(src).toMatch(/\/\^\[a-zA-Z\]:\[\/\\\\\]\//)
+ // And must NOT contain the old broken C:-only check
+ expect(src).not.toContain("!p.startsWith('C:') && !p.startsWith('\\\\\\\\')")
+ })
+})
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index f5b43e9f..c2e851f7 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -10,6 +10,7 @@ import { usePermissionStore } from '../stores/permissionStore'
import { getToolCallingStrategy } from '../lib/model-compatibility'
import { buildHermesToolPrompt, buildHermesToolResult, parseHermesToolCalls, stripToolCallTags, hasToolCallTags } from '../api/hermes-tool-calling'
import { chatNonStreaming } from '../api/agents'
+import { setActiveChatId, clearActiveChatId } from '../api/agent-context'
import type { CodexEvent } from '../types/codex'
import type { AgentBlock, AgentToolCall } from '../types/agent-mode'
import { selectRelevantTools } from '../lib/tool-selection'
@@ -21,6 +22,7 @@ import { makeInTurnCacheLookup } from '../api/agents/in-turn-cache'
import { explainError as explainToolError } from '../api/agents/error-hints'
import { budgetFromSettings } from '../api/agents/budget'
import { finalStripThinkingTags } from '../lib/thinking-stripper'
+import { ollamaUrl, localFetchStream } from '../api/backend'
const CODEX_SYSTEM_PROMPT = `You are Codex, an autonomous coding agent inside Locally Uncensored. You execute coding tasks end-to-end by reading files, writing code, and running shell commands. You MUST use tools — never guess file contents.
@@ -46,6 +48,131 @@ Rules:
- If a command fails, diagnose and retry with a different approach — don't hand back to the user unless truly stuck
- Be concise in text. All the work happens in tool calls.`
+// ── Streaming tool-call helper for Ollama ──────────────────────────────
+// Replaces the non-streaming chatWithTools() so the user sees live
+// content/thinking tokens while Gemma 4 generates (2+ minutes).
+async function streamWithTools(
+ modelId: string,
+ messages: ChatMessage[],
+ tools: ToolDefinition[],
+ options: { temperature?: number; thinking?: boolean; maxTokens?: number; signal?: AbortSignal },
+ onContent: (content: string) => void,
+ onThinking: (thinking: string) => void,
+): Promise<{ content: string; toolCalls: ToolCall[]; thinking: string }> {
+ const ollamaMessages = messages.map(m => {
+ const msg: Record<string, any> = { role: m.role, content: m.content }
+ if (m.tool_calls) msg.tool_calls = m.tool_calls
+ if ((m as any).images?.length) msg.images = (m as any).images.map((img: any) => img.data)
+ return msg
+ })
+
+ const body: Record<string, any> = {
+ model: modelId,
+ messages: ollamaMessages,
+ tools,
+ stream: true,
+ options: { num_gpu: 99 },
+ }
+ if (options.temperature !== undefined) body.options.temperature = options.temperature
+ if (options.maxTokens) body.options.num_predict = options.maxTokens
+ if (options.thinking === true) body.think = true
+ else if (options.thinking === false) body.think = false
+
+ const url = ollamaUrl('/chat')
+ // Use localFetchStream which tries direct fetch first, then falls back
+ // to the Tauri Rust proxy (proxy_localhost_stream). Plain `fetch` fails
+ // in the Tauri .exe because WebView can't reach localhost:11434 directly.
+ let response: Response
+ try {
+ response = await localFetchStream(url, {
+ method: 'POST',
+ body: JSON.stringify(body),
+ signal: options.signal,
+ })
+ } catch (fetchErr) {
+ throw fetchErr
+ }
+
+ // Retry without think on 400 (old Ollama builds / non-thinking models)
+ if (!response.ok && response.status === 400 && 'think' in body) {
+ delete body.think
+ response = await localFetchStream(url, {
+ method: 'POST',
+ body: JSON.stringify(body),
+ signal: options.signal,
+ })
+ }
+
+ if (!response.ok) {
+ const text = await response.text()
+ const err = new Error(`HTTP ${response.status}: ${text}`) as any
+ err.statusCode = response.status
+ throw err
+ }
+
+ const reader = response.body!.getReader()
+ const decoder = new TextDecoder()
+ let buf = ''
+ let content = ''
+ let thinking = ''
+ let toolCalls: ToolCall[] = []
+
+ while (true) {
+ const { done, value } = await reader.read()
+ if (done) break
+
+ buf += decoder.decode(value, { stream: true })
+ const lines = buf.split('\n')
+ buf = lines.pop() || ''
+
+ for (const line of lines) {
+ const trimmed = line.trim()
+ if (!trimmed) continue
+ try {
+ const j = JSON.parse(trimmed)
+ if (j.message) {
+ if (j.message.content) {
+ content += j.message.content
+ onContent(content)
+ }
+ if (j.message.thinking) {
+ thinking += j.message.thinking
+ onThinking(thinking)
+ }
+ // Ollama sends tool_calls in the last chunk where done=true
+ if (j.message.tool_calls && Array.isArray(j.message.tool_calls)) {
+ toolCalls = j.message.tool_calls.map((tc: any) => ({
+ function: { name: tc.function.name, arguments: tc.function.arguments },
+ }))
+ }
+ }
+ } catch { /* partial JSON line — skip */ }
+ }
+ }
+
+ // Process any remaining buffer
+ if (buf.trim()) {
+ try {
+ const j = JSON.parse(buf.trim())
+ if (j.message?.tool_calls && Array.isArray(j.message.tool_calls)) {
+ toolCalls = j.message.tool_calls.map((tc: any) => ({
+ function: { name: tc.function.name, arguments: tc.function.arguments },
+ }))
+ }
+ if (j.message?.content) {
+ content += j.message.content
+ onContent(content)
+ }
+ if (j.message?.thinking) {
+ thinking += j.message.thinking
+ onThinking(thinking)
+ }
+ } catch { /* ignore */ }
+ }
+
+ return { content, toolCalls, thinking }
+}
+
// Coding-relevant tool categories
const CODEX_CATEGORIES = ['filesystem', 'terminal', 'system', 'web'] as const
@@ -69,6 +196,11 @@ export function useCodex() {
convId = store.createConversation(activeModel, persona?.systemPrompt || '', 'codex')
}
+ // Per-chat agent workspace → `~/agent-workspace/<convId>/`.
+ // Cleared in the finally block so standalone tool calls elsewhere
+ // don't leak into this conversation's folder.
+ setActiveChatId(convId)
+
// Init codex thread if needed
if (!codexStore.getThread(convId)) {
codexStore.initThread(convId, codexStore.workingDirectory || '.')
@@ -146,14 +278,22 @@ export function useCodex() {
let messages: ChatMessage[] = [
{ role: 'system', content: systemPrompt },
...conv.messages
- .filter(m => m.role !== 'system' && m.content.trim())
- .map(m => ({
- role: m.role as 'user' | 'assistant' | 'tool',
- content: m.role === 'user' && cavemanReminder
- ? `${cavemanReminder}\n${m.content}`
- : m.content,
- })),
+ .filter(m => m.role !== 'system' && (m.content.trim() || m.hidden))
+ .map(m => {
+ const msg: ChatMessage = {
+ role: m.role as 'user' | 'assistant' | 'tool',
+ content: m.role === 'user' && cavemanReminder
+ ? `${cavemanReminder}\n${m.content}`
+ : m.content,
+ }
+ // Carry over tool_calls from hidden assistant messages so the
+ // model sees the full tool-call chain from previous turns
+ // (continue capability, parity with original Codex CLI).
+ if (m.tool_calls) msg.tool_calls = m.tool_calls as any
+ return msg
+ }),
]
+ const messagesStartLen = messages.length
// Setup
const abort = new AbortController()
@@ -215,29 +355,61 @@ export function useCodex() {
function: { name: t.name, description: t.description, parameters: t.inputSchema },
}))
- let turn: { content: string; toolCalls: ToolCall[] }
- try {
- turn = await provider.chatWithTools(modelToUse, messages, tools, chatOptions)
- } catch (thinkErr: any) {
- if (thinkErr?.message?.includes('does not support thinking') || thinkErr?.statusCode === 400) {
- // Retry with `undefined` so the provider omits the think
- // field entirely (old Ollama builds reject both `think: true`
- // and `think: false`).
- turn = await provider.chatWithTools(modelToUse, messages, tools, { ...chatOptions, thinking: undefined as unknown as boolean })
- } else {
- throw thinkErr
- }
- }
-
- toolCalls = turn.toolCalls
- turnContent = turn.content || ''
- // Codex parity with Agent / Chat: Thinking visibility is driven
- // by the toggle. Native Ollama `thinking` field is only surfaced
- // when the user asked for it. Drop it silently otherwise.
const keepThinking = settings.thinkingEnabled === true && isThinkingCompatible(activeModel)
- if (keepThinking && (turn as any).thinking) {
- thinkingContent += (thinkingContent ? '\n\n' : '') + (turn as any).thinking
- useChatStore.getState().updateMessageThinking(convId!, assistantMsg.id, thinkingContent)
+
+ if (providerId === 'ollama') {
+ // ── Streaming path for Ollama ──────────────────────────────
+ // Shows live content/thinking tokens so the user isn't staring
+ // at an empty bubble for 2+ minutes while the model generates.
+ let turn: { content: string; toolCalls: ToolCall[]; thinking: string }
+ try {
+ turn = await streamWithTools(
+ modelToUse, messages, tools,
+ { temperature: 0.1, thinking: chatOptions.thinking, maxTokens: chatOptions.maxTokens, signal: abort.signal },
+ (c) => useChatStore.getState().updateMessageContent(convId!, assistantMsg.id, fullContent ? fullContent + '\n\n' + c : c),
+ (t) => {
+ if (keepThinking) {
+ const combined = thinkingContent ? thinkingContent + '\n\n' + t : t
+ useChatStore.getState().updateMessageThinking(convId!, assistantMsg.id, combined)
+ }
+ },
+ )
+ } catch (thinkErr: any) {
+ if (thinkErr?.statusCode === 400 || thinkErr?.message?.includes('does not support thinking')) {
+ turn = await streamWithTools(
+ modelToUse, messages, tools,
+ { temperature: 0.1, thinking: undefined, maxTokens: chatOptions.maxTokens, signal: abort.signal },
+ (c) => useChatStore.getState().updateMessageContent(convId!, assistantMsg.id, fullContent ? fullContent + '\n\n' + c : c),
+ () => {},
+ )
+ } else {
+ throw thinkErr
+ }
+ }
+ toolCalls = turn.toolCalls
+ turnContent = turn.content || ''
+ if (keepThinking && turn.thinking) {
+ thinkingContent += (thinkingContent ? '\n\n' : '') + turn.thinking
+ useChatStore.getState().updateMessageThinking(convId!, assistantMsg.id, thinkingContent)
+ }
+ } else {
+ // ── Non-streaming fallback for OpenAI/Anthropic providers ──
+ let turn: { content: string; toolCalls: ToolCall[] }
+ try {
+ turn = await provider.chatWithTools(modelToUse, messages, tools, chatOptions)
+ } catch (thinkErr: any) {
+ if (thinkErr?.message?.includes('does not support thinking') || thinkErr?.statusCode === 400) {
+ turn = await provider.chatWithTools(modelToUse, messages, tools, { ...chatOptions, thinking: undefined as unknown as boolean })
+ } else {
+ throw thinkErr
+ }
+ }
+ toolCalls = turn.toolCalls
+ turnContent = turn.content || ''
+ if (keepThinking && (turn as any).thinking) {
+ thinkingContent += (thinkingContent ? '\n\n' : '') + (turn as any).thinking
+ useChatStore.getState().updateMessageThinking(convId!, assistantMsg.id, thinkingContent)
+ }
}
} else {
const hermesTools = toolRegistry.toHermesToolDefs(permissions)
@@ -302,10 +474,21 @@ export function useCodex() {
if (hasValidWorkDir) toolArgs.cwd = workDir
if (!toolArgs.timeout) toolArgs.timeout = 30000
}
- // Resolve relative file paths against working directory
+ // Resolve relative file paths against working directory.
+ // Absolute-path detection must accept ANY drive letter (C:, D:, E:, …),
+ // not just C:. Previously `!p.startsWith('C:')` classified
+ // `D:/Pictures/foo/bar.html` as relative and prepended workDir,
+ // producing the "doubled path" bug:
+ // workDir=D:/Pictures/foo, p=D:/Pictures/foo/bar.html →
+ // D:/Pictures/foo/D:/Pictures/foo/bar.html
+ // which then grew further on retry as the model re-emitted the path.
if ((toolName === 'file_read' || toolName === 'file_write' || toolName === 'file_list' || toolName === 'file_search') && toolArgs.path) {
- const p = toolArgs.path
- if (!p.startsWith('/') && !p.startsWith('C:') && !p.startsWith('\\\\') && workDir) {
+ const p: string = toolArgs.path
+ const isAbsolute =
+ /^[a-zA-Z]:[/\\]/.test(p) || // Windows drive letter: C:/ D:\ etc.
+ p.startsWith('/') || // Unix absolute
+ p.startsWith('\\\\') // UNC path: \\server\share
+ if (!isAbsolute && workDir) {
toolArgs.path = workDir.replace(/\\/g, '/') + '/' + p
}
}
@@ -451,6 +634,28 @@ export function useCodex() {
}
}
+ // Bug fix: when the model's final turn returns empty content (all
+ // work happened via tool calls), build a fallback summary from the
+ // blocks array so the assistant bubble is never blank.
+ if (!fullContent.trim()) {
+ const completed = blocks.filter(b => b.phase === 'tool_call' && b.toolCall?.status === 'completed')
+ const failed = blocks.filter(b => b.phase === 'tool_call' && b.toolCall?.status === 'failed')
+ const writes = completed.filter(b => b.toolCall?.toolName === 'file_write')
+ const reads = completed.filter(b => b.toolCall?.toolName === 'file_read')
+
+ const parts: string[] = []
+ if (writes.length) parts.push(`${writes.length} file(s) written`)
+ if (reads.length) parts.push(`${reads.length} file(s) read`)
+ const otherCompleted = completed.length - writes.length - reads.length
+ if (otherCompleted > 0) parts.push(`${otherCompleted} other operation(s) completed`)
+ if (failed.length) parts.push(`${failed.length} operation(s) failed`)
+
+ fullContent = parts.length > 0
+ ? `Task completed: ${parts.join(', ')}.`
+ : 'Task completed.'
+ useChatStore.getState().updateMessageContent(convId, assistantMsg.id, fullContent)
+ }
+
// Final update
codexStore.addEvent(convId, {
id: uuid(), type: 'done', content: 'Task completed.', timestamp: Date.now(),
@@ -482,9 +687,35 @@ export function useCodex() {
})
}
} finally {
+ // ── Continue capability (parity with original Codex CLI) ────────
+ // Persist the tool-call chain from this turn as hidden messages in
+ // the chat store. On the next turn, the history builder includes
+ // them in the API payload so the model sees what it did before.
+ // Hidden messages are filtered out by MessageBubble rendering.
+ const toolHistory = messages.slice(messagesStartLen)
+ if (toolHistory.length > 0 && convId) {
+ const store = useChatStore.getState()
+ // Find the assistant message we just filled so we can insert BEFORE it
+ const convNow = store.conversations.find(c => c.id === convId)
+ const assistantIdx = convNow?.messages.findIndex(m => m.id === assistantMsg.id) ?? -1
+ if (assistantIdx > 0) {
+ for (const tm of toolHistory) {
+ store.insertMessageBefore(convId, assistantMsg.id, {
+ id: uuid(),
+ role: tm.role as 'assistant' | 'tool',
+ content: tm.content || '',
+ timestamp: Date.now(),
+ hidden: true,
+ tool_calls: tm.tool_calls as any,
+ })
+ }
+ }
+ }
+
setIsRunning(false)
runningRef.current = false
abortRef.current = null
+ clearActiveChatId()
codexStore.setThreadStatus(convId, 'idle')
}
}, [])
diff --git a/src/stores/chatStore.ts b/src/stores/chatStore.ts
index 52ff255e..3dd526c2 100644
--- a/src/stores/chatStore.ts
+++ b/src/stores/chatStore.ts
@@ -34,6 +34,7 @@ interface ChatState {
renameConversation: (id: string, title: string) => void
setActiveConversation: (id: string | null) => void
addMessage: (conversationId: string, message: Message) => void
+ insertMessageBefore: (conversationId: string, beforeId: string, message: Message) => void
updateMessageContent: (conversationId: string, messageId: string, content: string) => void
updateMessageThinking: (conversationId: string, messageId: string, thinking: string) => void
updateMessageAgentBlocks: (conversationId: string, messageId: string, blocks: AgentBlock[]) => void
@@ -109,6 +110,18 @@ export const useChatStore = create<ChatState>()(
),
})),
+ insertMessageBefore: (conversationId, beforeId, message) =>
+ set((state) => ({
+ conversations: state.conversations.map((c) => {
+ if (c.id !== conversationId) return c
+ const idx = c.messages.findIndex((m) => m.id === beforeId)
+ if (idx < 0) return { ...c, messages: [...c.messages, message], updatedAt: Date.now() }
+ const msgs = [...c.messages]
+ msgs.splice(idx, 0, message)
+ return { ...c, messages: msgs, updatedAt: Date.now() }
+ }),
+ })),
+
updateMessageContent: (conversationId, messageId, content) =>
set((state) => ({
conversations: state.conversations.map((c) =>
diff --git a/src/types/chat.ts b/src/types/chat.ts
index 0623a912..9dadfaf5 100644
--- a/src/types/chat.ts
+++ b/src/types/chat.ts
@@ -19,6 +19,11 @@ export interface Message {
// Agent Mode fields
agentBlocks?: AgentBlock[]
toolCallSummary?: string
+ // Continue capability — tool-call history persisted between turns so
+ // the model sees what it did before (parity with original Codex CLI).
+ // Hidden messages are included in the API payload but not rendered.
+ hidden?: boolean
+ tool_calls?: { function: { name: string; arguments: Record<string, unknown> } }[]
}
export interface Conversation {