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.

feat(cowork): one subagent directory for the desktop

Akarshan Biswasf273c2111bd5
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts b/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts
index bfb29556b..248e447cf 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts
@@ -1,7 +1,19 @@
import { invoke, Channel } from '@tauri-apps/api/core'
-import { SkillMeta, ToolResult, ToolSchema } from './types'
+import {
+ SkillMeta,
+ ToolOutputChunk,
+ ToolResult,
+ ToolSchema,
+ WorkspaceScope,
+} from './types'
-export { SkillMeta, ToolResult, ToolSchema } from './types'
+export {
+ SkillMeta,
+ ToolOutputChunk,
+ ToolResult,
+ ToolSchema,
+ WorkspaceScope,
+} from './types'
/**
* Every call takes the Jan data folder, because the plugin derives its
diff --git a/src-tauri/src/core/agent/commands.rs b/src-tauri/src/core/agent/commands.rs
index 95983683e..5857ce2ac 100644
--- a/src-tauri/src/core/agent/commands.rs
+++ b/src-tauri/src/core/agent/commands.rs
@@ -13,6 +13,8 @@ use crate::core::agent::project::{
};
use crate::core::agent::skill_hub;
use crate::core::agent::skills as agent_skills;
+use crate::core::agent::subagent;
+use crate::core::app::commands::get_jan_data_folder_path;
use tauri_plugin_agent_tools::skills::{self, SkillMeta};
use tauri_plugin_agent_tools::workspace;
@@ -152,3 +154,44 @@ pub async fn agent_plugin_search(
pub fn agent_git_branch(project: String) -> Option<String> {
git::current_branch(std::path::Path::new(&project))
}
+
+/// A saved subagent definition, for the `task` tool's advertised name list and
+/// the Cowork subagents panel.
+#[derive(serde::Serialize)]
+pub struct SubagentDefinitionDto {
+ pub name: String,
+ pub description: String,
+ pub system_prompt: String,
+ /// When set, the child's toolset is this list intersected with the parent's;
+ /// it never widens. `None` inherits the parent's set.
+ pub allowed_tools: Option<Vec<String>>,
+ pub model: Option<String>,
+}
+
+/// Every subagent saved for the desktop, from the single
+/// `<jan_data>/agent-workspace/subagents/` directory.
+///
+/// Deliberately not the CLI's plugin/user/project merge: Cowork has no project
+/// root in a default session, and an attached folder is mounted read-only, so
+/// scanning it would let a cloned repo inject a system prompt and a tool
+/// allowlist into the agent. Malformed files are skipped, so a bad TOML costs one
+/// definition rather than the whole list.
+#[tauri::command]
+pub async fn agent_subagent_list<R: tauri::Runtime>(
+ app_handle: tauri::AppHandle<R>,
+) -> Result<Vec<SubagentDefinitionDto>, String> {
+ let dir = subagent::desktop_subagents_dir(&get_jan_data_folder_path(app_handle));
+ Ok(
+ subagent::SubagentRegistry::load_one(&dir, subagent::SubagentScope::User)
+ .list()
+ .into_iter()
+ .map(|d| SubagentDefinitionDto {
+ name: d.name.clone(),
+ description: d.description.clone(),
+ system_prompt: d.system_prompt.clone(),
+ allowed_tools: d.allowed_tools.clone(),
+ model: d.model.clone(),
+ })
+ .collect(),
+ )
+}
diff --git a/src-tauri/src/core/agent/subagent.rs b/src-tauri/src/core/agent/subagent.rs
index 74a6564d3..e4eba4f30 100644
--- a/src-tauri/src/core/agent/subagent.rs
+++ b/src-tauri/src/core/agent/subagent.rs
@@ -10,6 +10,11 @@ use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use tauri_plugin_agent_tools::permissions::ToolPermissions;
+use tauri_plugin_agent_tools::workspace;
+
+/// Directory name holding `<name>.toml` definitions, under both a project's
+/// `.jan/agent/` and the desktop's permanent store.
+const SUBAGENTS: &str = "subagents";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
@@ -76,15 +81,25 @@ impl std::fmt::Display for SubagentError {
/// `~/.jan/agent/subagents/`. `None` when the home directory can't be resolved.
pub fn user_subagents_dir() -> Option<PathBuf> {
- dirs::home_dir().map(|h| h.join(".jan").join("agent").join("subagents"))
+ dirs::home_dir().map(|h| h.join(".jan").join("agent").join(SUBAGENTS))
}
/// `<project_root>/.jan/agent/subagents/`.
pub fn project_subagents_dir(project_root: &Path) -> PathBuf {
- project_root
- .join(".jan")
- .join("agent")
- .join("subagents")
+ project_root.join(".jan").join("agent").join(SUBAGENTS)
+}
+
+/// The desktop's single subagent directory:
+/// `<jan_data_folder>/agent-workspace/subagents/`.
+///
+/// Cowork has no project root in a default session and mounts an attached folder
+/// read-only, so two of the three CLI scopes (project, and plugin -- which is
+/// also project-relative) are unreachable or unwritable there. Desktop keeps one
+/// directory instead, a sibling of `memory/` and `skills/` in the permanent
+/// store. That placement also puts it outside every tool's project root, so the
+/// agent cannot rewrite its own definitions with `write`.
+pub fn desktop_subagents_dir(jan_data_folder: &Path) -> PathBuf {
+ workspace::store_dir(&workspace::permanent_store(jan_data_folder), SUBAGENTS)
}
/// A subagent name is used to build a filename, so it must be a single path
@@ -131,6 +146,15 @@ impl SubagentRegistry {
Self { defs }
}
+ /// Load exactly one directory as one scope, for the desktop's flat layout
+ /// (see [`desktop_subagents_dir`]). No merge, so no shadowing: `get` and
+ /// `list` both see the same set.
+ pub fn load_one(dir: &Path, scope: SubagentScope) -> Self {
+ let mut defs = Vec::new();
+ load_dir(dir, scope, &mut defs);
+ Self { defs }
+ }
+
/// The winning definition for `name`: the project-scoped entry shadows a
/// user-scoped one of the same name.
pub fn get(&self, name: &str) -> Option<&SubagentDefinition> {
@@ -1168,6 +1192,48 @@ mod tests {
let _ = std::fs::remove_dir_all(&root);
}
+ #[test]
+ fn desktop_dir_is_a_sibling_of_the_store_kinds() {
+ let data = unique_root("desktopdir");
+ let dir = desktop_subagents_dir(&data);
+ assert_eq!(dir, data.join("agent-workspace").join("subagents"));
+ // A sibling of memory/ and skills/, never inside a thread or session
+ // sandbox -- that is what keeps `write` away from these definitions.
+ assert_eq!(
+ dir.parent(),
+ Some(workspace::permanent_store(&data).as_path())
+ );
+ assert_ne!(dir, workspace::threads_dir(&data));
+ assert_ne!(dir, workspace::sessions_dir(&data));
+ let _ = std::fs::remove_dir_all(&data);
+ }
+
+ #[test]
+ fn load_one_reads_only_the_directory_it_is_given() {
+ let root = unique_root("loadone");
+ let flat = root.join("flat");
+ write_def(&flat, "desktop-agent", "");
+ // A project-scoped definition under the same root must stay invisible:
+ // Cowork mounts an attached folder read-only and must not pick up
+ // definitions that ship inside it.
+ write_def(&project_subagents_dir(&root), "project-agent", "");
+
+ let reg = SubagentRegistry::load_one(&flat, SubagentScope::User);
+ assert_eq!(reg.list().len(), 1);
+ let def = reg.get("desktop-agent").expect("loaded");
+ assert_eq!(def.scope, SubagentScope::User);
+ assert!(reg.get("project-agent").is_none());
+ let _ = std::fs::remove_dir_all(&root);
+ }
+
+ #[test]
+ fn load_one_on_a_missing_directory_is_empty_not_an_error() {
+ let root = unique_root("loadone_missing");
+ let reg = SubagentRegistry::load_one(&root.join("nope"), SubagentScope::User);
+ assert!(reg.list().is_empty());
+ let _ = std::fs::remove_dir_all(&root);
+ }
+
#[test]
fn malformed_file_is_skipped_not_fatal() {
let root = unique_root("malformed");
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index a26fd9d7f..e4f1e79a5 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -97,6 +97,7 @@ macro_rules! invoke_commands_with_extras {
core::agent::commands::agent_plugin_remove,
core::agent::commands::agent_plugin_search,
core::agent::commands::agent_git_branch,
+ core::agent::commands::agent_subagent_list,
// Remote provider commands
core::server::remote_provider_commands::register_provider_config,
core::server::remote_provider_commands::unregister_provider_config,