Skip to content

armgit / jan

Public Imported from GitHub
0

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

6.5 KB
Raw Blame
//! The built-in agent toolset, shared by the Jan desktop app and the headless
//! `jan` CLI.
//!
//! The toolset is Tauri-free: executing a tool needs nothing but
//! `(&BuiltinTool, &serde_json::Value, &ToolContext)`, never an `AppHandle`.
//! `tauri` is an optional dependency behind the `tauri` feature, so the CLI
//! builds this crate with `default-features = false` and links no GUI crates.
//! Everything under `permissions`, `skills`, `tools` and `workspace` is
//! available in both configurations; only `init()` and the IPC shims in
//! `commands` are gated.

pub mod artifact;
pub mod consent;
pub mod memory;
pub mod permissions;
pub mod preview;
pub mod screenshot_pages;
pub mod skills;
pub mod tools;
pub mod workspace;

#[cfg(feature = "tauri")]
mod commands;

/// Webview-backed `screenshot` renderer, so the tool works without Chrome, on
/// all three desktop platforms; other targets keep the Chrome path.
#[cfg(all(
    feature = "tauri",
    any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
mod webview_shot;

/// Label prefix of the hidden windows the webview `screenshot` renderer opens.
/// The app's navigation guard keys on it: those windows load their target as a
/// `file:` URL, which no other webview may, and only the files
/// [`screenshot_pages`] records for them.
pub const SCREENSHOT_WINDOW_LABEL_PREFIX: &str = "jan-shot-";

/// Runs the confined-spawn helper and exits, when this process was re-exec'd as
/// one by the Windows sandbox backend. A no-op on every other platform and on a
/// normal launch, but it must be called before anything else in `main`: the
/// helper's whole job is to spawn and wait, so starting the app first would run a
/// second copy of it per shell command.
pub use tools::appcontainer::run_helper_if_requested as run_sandbox_helper_if_requested;

#[cfg(feature = "tauri")]
pub use commands::{AgentToolsError, ToolResult};

/// Initializes the agent tools plugin.
#[cfg(feature = "tauri")]
pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
    tauri::plugin::Builder::new("agent-tools")
        .invoke_handler(tauri::generate_handler![
            commands::workspace_path,
            commands::thread_workspace_path,
            commands::thread_workspace_delete,
            commands::thread_workspace_sweep,
            commands::session_workspace_path,
            commands::session_workspace_copy_to_thread,
            commands::session_workspace_delete,
            commands::session_workspace_sweep,
            commands::skill_list,
            commands::skill_read,
            commands::skill_write,
            commands::skill_delete,
            commands::skill_invoke,
            commands::memory_list,
            commands::memory_read,
            commands::memory_write,
            commands::memory_catalog,
            commands::memory_delete,
            commands::tool_schemas,
            commands::sandbox_status,
            commands::subagent_result_reserve,
            commands::subagent_result_fill,
            commands::blackboard_write,
            commands::attachment_import,
            commands::execute_tool,
            commands::execute_tool_streaming,
            commands::start_monitor,
            commands::stop_monitor,
            commands::list_monitors,
            commands::session_monitor_ids,
            commands::stop_session_monitors,
            commands::cancel_thread_bash,
            commands::preview_register_root,
            commands::preview_unregister_root,
            commands::open_artifact,
            commands::consent_request,
            commands::consent_answer,
            commands::consent_consume,
            commands::consent_cancel_thread,
            commands::consent_saved,
            commands::consent_revoke,
            commands::consent_set_allow_all_mcp
        ])
        .setup(|app, _api| {
            use tauri::Manager;
            app.manage(preview::PreviewRoots::default());
            Ok(())
        })
        .register_asynchronous_uri_scheme_protocol("preview", |ctx, request, responder| {
            let app = ctx.app_handle().clone();
            // Off the webview thread: canonicalize and read touch the disk.
            std::thread::spawn(move || {
                responder.respond(commands::preview_response(&app, request.uri().path()));
            });
        })
        .build()
}

#[cfg(test)]
mod permission_tests {
    /// A command reaches the frontend only if it is BOTH in `generate_handler!`
    /// and in `build.rs`'s `COMMANDS` (which generates its permission). Missing
    /// the latter compiles and tests clean, then fails at runtime with
    /// "not allowed. Command not found" -- invisible to any suite that mocks
    /// `invoke`. Keep the two lists in lockstep.
    fn names_between<'a>(src: &'a str, start: &str, end: &str) -> Vec<&'a str> {
        let Some(rest) = src.split_once(start).map(|(_, r)| r) else {
            return Vec::new();
        };
        let Some(block) = rest.split_once(end).map(|(b, _)| b) else {
            return Vec::new();
        };
        block
            .split(',')
            .map(|s| s.trim().trim_matches('"'))
            .filter(|s| !s.is_empty())
            .map(|s| s.rsplit("::").next().unwrap_or(s))
            .collect()
    }

    #[test]
    fn every_registered_command_has_a_permission() {
        let handlers = names_between(include_str!("lib.rs"), "tauri::generate_handler![", "])");
        let declared = names_between(include_str!("../build.rs"), "COMMANDS: &[&str] = &[", "];");
        assert!(!handlers.is_empty(), "failed to parse generate_handler!");
        assert!(!declared.is_empty(), "failed to parse build.rs COMMANDS");
        let missing: Vec<_> = handlers.iter().filter(|c| !declared.contains(c)).collect();
        assert!(
            missing.is_empty(),
            "commands registered but absent from build.rs COMMANDS: {missing:?}"
        );
    }

    #[test]
    fn every_permission_is_in_the_default_set() {
        let declared = names_between(include_str!("../build.rs"), "COMMANDS: &[&str] = &[", "];");
        let default_toml = include_str!("../permissions/default.toml");
        let missing: Vec<_> = declared
            .iter()
            .filter(|c| {
                let permission = format!("allow-{}", c.replace('_', "-"));
                !default_toml.contains(&permission)
            })
            .collect();
        assert!(
            missing.is_empty(),
            "commands missing an allow-* entry in permissions/default.toml: {missing:?}"
        );
    }
}