Add ext/agent layout framework, ext CLI, and docs catch-up.

Ship ABI agent plugin helpers, marqdo ext list/add/remove, and document the agent development roadmap (compose LLM, match/assign).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-06 13:31:25 +08:00
1 parent b0c777c480
commit 3a7e11c995
45 files changed
+1632 -204

No files matched your search

+303
View File
@@ -0,0 +1,303 @@
//! Official extension installer (`marqdo ext list|add|remove`).
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
/// Official extension catalog entry.
#[derive(Debug, Clone)]
pub struct ExtPackage {
pub id: &'static str,
pub description: &'static str,
/// Files relative to a source `ext/` directory (UTF-8 paths).
pub mq_files: &'static [&'static str],
/// If set, also install native plugin named by [`native_lib_filename`].
pub native_crate: Option<&'static str>,
}
pub const CATALOG: &[ExtPackage] = &[
ExtPackage {
id: "llm",
description: "OpenAI-compatible chat object (ext/llm)",
mq_files: &["llm.mq.md", "大模型.mq.md"],
native_crate: None,
},
ExtPackage {
id: "agent",
description: "Agent development framework (ext/agent + ABI plugin)",
mq_files: &["agent.mq.md", "智能体.mq.md"],
native_crate: Some("marqdo_plugin_agent"),
},
];
pub fn find_package(id: &str) -> Option<&'static ExtPackage> {
let id = id.trim();
CATALOG.iter().find(|p| p.id == id)
}
/// Default install root: `MARQDO_EXT` if set, else `~/.marqdo/ext`.
pub fn install_root() -> Result<PathBuf> {
if let Ok(h) = env::var("MARQDO_EXT") {
let p = PathBuf::from(h);
if !p.as_os_str().is_empty() {
return Ok(p);
}
}
Ok(default_user_ext_dir())
}
pub fn default_user_ext_dir() -> PathBuf {
dirs_next_home()
.unwrap_or_else(|| PathBuf::from("."))
.join(".marqdo")
.join("ext")
}
fn dirs_next_home() -> Option<PathBuf> {
env::var_os("USERPROFILE")
.or_else(|| env::var_os("HOME"))
.map(PathBuf::from)
}
/// Trusted roots for native plugins (sandbox bypass for `host_plugin_load`).
pub fn trusted_plugin_roots() -> Vec<PathBuf> {
let mut roots = Vec::new();
if let Ok(h) = env::var("MARQDO_EXT") {
roots.push(PathBuf::from(h));
}
roots.push(default_user_ext_dir());
if let Ok(exe) = env::current_exe() {
if let Some(dir) = exe.parent() {
roots.push(dir.join("ext"));
}
}
roots
}
pub fn native_lib_filename() -> &'static str {
if cfg!(windows) {
"agent.dll"
} else if cfg!(target_os = "macos") {
"libagent.dylib"
} else {
"libagent.so"
}
}
pub fn is_installed(root: &Path, pkg: &ExtPackage) -> bool {
pkg.mq_files
.iter()
.all(|f| root.join(f).is_file())
}
pub fn list_ext() -> Result<()> {
let root = install_root()?;
println!("Install root: {}", root.display());
println!();
println!(
"{:<10} {:<8} {}",
"ID", "STATUS", "DESCRIPTION"
);
println!("{:-<10} {:-<8} {:-<40}", "", "", "");
for pkg in CATALOG {
let status = if is_installed(&root, pkg) {
"yes"
} else {
"no"
};
println!("{:<10} {:<8} {}", pkg.id, status, pkg.description);
}
Ok(())
}
/// Directories that may contain source `ext/*.mq.md` for `add` (dev / release layout).
pub fn source_ext_roots() -> Vec<PathBuf> {
let mut roots = Vec::new();
if let Ok(h) = env::var("MARQDO_EXT_SOURCE") {
roots.push(PathBuf::from(h));
}
if let Ok(cwd) = env::current_dir() {
roots.push(cwd.join("ext"));
// cargo test / run from repo root
roots.push(cwd.join("..").join("ext"));
}
if let Ok(exe) = env::current_exe() {
if let Some(dir) = exe.parent() {
roots.push(dir.join("ext"));
roots.push(dir.join("..").join("ext"));
roots.push(dir.join("..").join("..").join("ext"));
roots.push(dir.join("..").join("..").join("..").join("ext"));
// target/debug → repo ext
roots.push(dir.join("..").join("..").join("..").join("ext"));
}
}
roots
}
fn find_source_file(name: &str) -> Result<PathBuf> {
for root in source_ext_roots() {
let p = root.join(name);
if p.is_file() {
return Ok(p);
}
}
bail!(
"cannot find source `{name}` for install (set MARQDO_EXT_SOURCE to an ext/ directory, or run from the Marqdo repo)"
);
}
fn find_native_plugin() -> Result<PathBuf> {
let name = native_lib_filename();
let mut candidates = Vec::new();
if let Ok(h) = env::var("MARQDO_EXT_SOURCE") {
let base = PathBuf::from(h);
candidates.push(base.join("native").join(name));
candidates.push(base.join(name));
}
if let Ok(cwd) = env::current_dir() {
candidates.push(cwd.join("target").join("debug").join(name));
candidates.push(cwd.join("target").join("release").join(name));
candidates.push(cwd.join("ext").join("native").join(name));
}
if let Ok(exe) = env::current_exe() {
if let Some(dir) = exe.parent() {
candidates.push(dir.join("ext").join("native").join(name));
candidates.push(dir.join(name));
// target/debug/deps → target/debug
candidates.push(dir.join(name));
candidates.push(dir.join("..").join(name));
}
}
for p in candidates {
if p.is_file() {
return Ok(p);
}
}
bail!(
"cannot find native plugin `{name}` (run `cargo build -p marqdo_plugin_agent`, or place it under MARQDO_EXT_SOURCE/native/)"
);
}
pub fn add_ext(id: &str) -> Result<()> {
let pkg = find_package(id).with_context(|| {
format!(
"unknown extension `{id}` (official: {})",
CATALOG
.iter()
.map(|p| p.id)
.collect::<Vec<_>>()
.join(", ")
)
})?;
let root = install_root()?;
fs::create_dir_all(&root)
.with_context(|| format!("create install root {}", root.display()))?;
for f in pkg.mq_files {
let src = find_source_file(f)?;
let dest = root.join(f);
fs::copy(&src, &dest)
.with_context(|| format!("copy {} → {}", src.display(), dest.display()))?;
println!("installed {}", dest.display());
}
if pkg.native_crate.is_some() {
let src = find_native_plugin()?;
let native_dir = root.join("native");
fs::create_dir_all(&native_dir)?;
let dest = native_dir.join(native_lib_filename());
fs::copy(&src, &dest)
.with_context(|| format!("copy {} → {}", src.display(), dest.display()))?;
println!("installed {}", dest.display());
// Absolute path hint for loaders / env
let abs = dest
.canonicalize()
.unwrap_or_else(|_| dest.clone());
let hint = root.join("agent.plugin");
fs::write(&hint, abs.to_string_lossy().as_bytes())?;
println!("wrote {}", hint.display());
println!(
"hint: import works via install root; set MARQDO_AGENT_PLUGIN={} if load_native needs it",
abs.display()
);
}
if env::var_os("MARQDO_EXT").is_none() {
println!(
"hint: imports resolve via {} automatically; or set MARQDO_EXT to this directory",
root.display()
);
}
Ok(())
}
pub fn remove_ext(id: &str) -> Result<()> {
let pkg = find_package(id).with_context(|| format!("unknown extension `{id}`"))?;
let root = install_root()?;
let mut removed = 0usize;
for f in pkg.mq_files {
let dest = root.join(f);
if dest.is_file() {
fs::remove_file(&dest)
.with_context(|| format!("remove {}", dest.display()))?;
println!("removed {}", dest.display());
removed += 1;
}
}
if pkg.native_crate.is_some() {
let dest = root.join("native").join(native_lib_filename());
if dest.is_file() {
fs::remove_file(&dest)?;
println!("removed {}", dest.display());
removed += 1;
}
let hint = root.join("agent.plugin");
if hint.is_file() {
fs::remove_file(&hint)?;
removed += 1;
}
}
if removed == 0 {
println!("nothing to remove for `{id}` under {}", root.display());
}
Ok(())
}
/// Resolve installed native plugin path for `name` (currently only `agent`).
pub fn installed_native_path(name: &str) -> Option<PathBuf> {
if name != "agent" {
return None;
}
for root in trusted_plugin_roots() {
let hint = root.join("agent.plugin");
if let Ok(s) = fs::read_to_string(&hint) {
let p = PathBuf::from(s.trim());
if p.is_file() {
return Some(p);
}
}
let p = root.join("native").join(native_lib_filename());
if p.is_file() {
return Some(p);
}
}
None
}
pub fn path_is_trusted_plugin(path: &Path) -> bool {
let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
for root in trusted_plugin_roots() {
let root = root.canonicalize().unwrap_or(root);
if crate::host::path_under_root(&path, &root) {
return true;
}
let native = root.join("native");
let native = native.canonicalize().unwrap_or(native);
if crate::host::path_under_root(&path, &native) {
return true;
}
}
false
}
+16
View File
@@ -80,6 +80,7 @@ pub enum HostFn {
PluginLoad = 69,
PluginUnload = 70,
PluginList = 71,
ExtNativePath = 72,
}
impl HostFn {
@@ -156,6 +157,7 @@ impl HostFn {
69 => Self::PluginLoad,
70 => Self::PluginUnload,
71 => Self::PluginList,
72 => Self::ExtNativePath,
_ => return None,
})
}
@@ -236,6 +238,7 @@ impl HostFn {
"host_plugin_load" | "plugin_load" => Self::PluginLoad,
"host_plugin_unload" | "plugin_unload" => Self::PluginUnload,
"host_plugin_list" | "plugin_list" => Self::PluginList,
"host_ext_native_path" | "ext_native_path" => Self::ExtNativePath,
_ => return None,
})
}
@@ -317,6 +320,7 @@ impl HostFn {
Self::PluginLoad => "host_plugin_load",
Self::PluginUnload => "host_plugin_unload",
Self::PluginList => "host_plugin_list",
Self::ExtNativePath => "host_ext_native_path",
}
}
@@ -361,6 +365,7 @@ impl HostFn {
Self::ForeignLangs => &[],
Self::PluginLoad => &["path"],
Self::PluginUnload | Self::PluginList => &[],
Self::ExtNativePath => &["name"],
}
}
@@ -539,6 +544,17 @@ pub fn call_host(
HostFn::PluginLoad => plugin::load(ctx, require(bound, "path")?),
HostFn::PluginUnload => plugin::unload(ctx),
HostFn::PluginList => plugin::list(ctx),
HostFn::ExtNativePath => {
let name = require(bound, "name")?;
let s = match name {
Value::Text(t) => t.as_str(),
_ => return Err("name must be text".into()),
};
Ok(match crate::ext_cli::installed_native_path(s) {
Some(p) => Value::Text(p.to_string_lossy().into_owned()),
None => Value::None,
})
}
}
}
+16 -1
View File
@@ -172,7 +172,22 @@ pub fn load(ctx: &mut HostContext, path: &Value) -> Result<Value, String> {
return Err("plugin_load denied by host policy".into());
}
let rel = as_text(path, "path")?;
let file = ctx.resolve_path(rel)?;
let file = match ctx.resolve_path(rel) {
Ok(p) => p,
Err(e) => {
let p = std::path::Path::new(rel);
let abs = if p.is_absolute() {
p.to_path_buf()
} else {
ctx.cwd.join(p)
};
if abs.is_file() && crate::ext_cli::path_is_trusted_plugin(&abs) {
abs
} else {
return Err(e);
}
}
};
if !file.is_file() {
return Err(format!("plugin_load: not a file: {}", file.display()));
}
+1
View File
@@ -11,6 +11,7 @@ pub mod capture;
pub mod catalog;
pub mod debug;
pub mod diagnostics;
pub mod ext_cli;
pub mod foreign;
pub mod formula;
pub mod host;
+2
View File
@@ -116,6 +116,8 @@ fn ext_search_roots() -> Vec<PathBuf> {
if let Ok(h) = env::var("MARQDO_EXT") {
roots.push(PathBuf::from(h));
}
// Official installer default (`marqdo ext add`)
roots.push(crate::ext_cli::default_user_ext_dir());
if let Ok(cwd) = env::current_dir() {
roots.push(cwd.join("ext"));
roots.push(cwd);
+31 -1
View File
@@ -5,12 +5,13 @@ use anyhow::Result;
use clap::{Parser, Subcommand, ValueEnum};
use marqdo::catalog::{write_catalog, CatalogOptions};
use marqdo::ext_cli::{add_ext, list_ext, remove_ext};
use marqdo::input_feed::load_stdin_file;
use marqdo::view::{serve, serve_debug, write_static, DebugOptions, OutputOptions, ViewOptions};
use marqdo::{Backend, RunOptions};
#[derive(Parser, Debug)]
#[command(name = "marqdo", version, about = "Marqdo interpreter — run, view, debug, and catalog .mq.md")]
#[command(name = "marqdo", version, about = "Marqdo interpreter — run, view, debug, catalog, and ext")]
struct Cli {
#[command(subcommand)]
command: Commands,
@@ -109,6 +110,27 @@ enum Commands {
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
out: PathBuf,
},
/// Official extension installer (`list` / `add` / `remove`)
Ext {
#[command(subcommand)]
action: ExtAction,
},
}
#[derive(Subcommand, Debug)]
enum ExtAction {
/// List official extensions and install status
List,
/// Install an official extension into the local ext root
Add {
#[arg(value_name = "NAME")]
name: String,
},
/// Remove an installed official extension
Remove {
#[arg(value_name = "NAME")]
name: String,
},
}
#[derive(Subcommand, Debug)]
@@ -232,5 +254,13 @@ fn try_main() -> Result<i32> {
})?;
Ok(0)
}
Commands::Ext { action } => {
match action {
ExtAction::List => list_ext()?,
ExtAction::Add { name } => add_ext(&name)?,
ExtAction::Remove { name } => remove_ext(&name)?,
}
Ok(0)
}
}
}