Merge main into fix/open-dwg-files

This commit is contained in:
dotCooCoo committed 2026-12-04 06:36:17 -08:00
commit 2e030ec46d
34 files changed
+3799 -243

No files matched your search

+3
View File
@@ -25,6 +25,8 @@ vectorcraft-render = { workspace = true }
vectorcraft-text = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
# The agent settings and the desktop app to start for plain `mcp` (the shared `src/agent_settings.rs`).
craft = { workspace = true, features = ["automation-agents"] }
# `perf` on Windows (src/perf.rs) reads how busy the processors are (GetSystemTimes) and this
# process's working set (GetProcessMemoryInfo) through winsafe's safe wrappers over Win32 (MIT),
@@ -54,6 +56,7 @@ serde_json = { workspace = true }
# The files of other crates that this crate compiles or its tests read, for CI's test selection.
[package.metadata.craft]
reads = [
"apps/vectorcraft/src/agent_settings.rs",
"apps/vectorcraft/src/mac_fonts.rs",
"apps/vectorcraft/src/prefs_dir.rs",
"apps/vectorcraft/src/profile.rs",
+64 -12
View File
@@ -2,7 +2,7 @@
//!
//! ```text
//! vectorcraft-cli --safe-mode SUBCOMMAND ...
//! vectorcraft-cli mcp [--connect 127.0.0.1:7979 | --headless] [--automation-read-root DIR]... [--automation-write-root DIR]...
//! vectorcraft-cli mcp [--headless] [--when-closed launch|headless|error] [--automation-read-root DIR]... [--automation-write-root DIR]...
//! vectorcraft-cli run [--in FILE] [--cmd id [--params '{json}'] | --commands FILE|-]... [--export out.svg]... [--scale 2] [--ppi 300] [--width 1920] [--height 1080] [--background transparent]
//! vectorcraft-cli commands
//! vectorcraft-cli convert IN OUT [--scale 2] [--ppi 300] [--width 1920] [--height 1080] [--background transparent] [--artboard 0 | --range 1-3,5] [--outline-text] [--decimals N] [--replace-lossy] [--editing-data] [--legacy-name-encoding ENC] [--preserve-editing] [--ai-version 7|pdf] [--hidden-objects layer|omit|visible] [--transparent-images flatten|omit]
@@ -39,6 +39,10 @@ macro_rules! out {
}};
}
/// The desktop app's agent settings (Preferences › AI Integrations), shared: plain `mcp` reads the
/// closed-app choice and the agent folders the app saved.
#[path = "../../vectorcraft/src/agent_settings.rs"]
mod agent_settings;
/// The desktop app's CoreText font lister, shared: exports and MCP find the same fonts.
#[cfg(target_os = "macos")]
#[path = "../../vectorcraft/src/mac_fonts.rs"]
@@ -56,9 +60,11 @@ mod system_fonts;
#[path = "../../vectorcraft/src/windows_cores.rs"]
mod windows_cores;
use craft::automation::agents::WhenAppClosed;
use serde_json::{Value, json};
use vectorcraft_engine::file_access::{self, AutomationRoots};
use vectorcraft_mcp::{Backend, DEFAULT_ADDR, Headless, Remote, Server};
use vectorcraft_mcp::agents::{LiveBackend, LiveConfig};
use vectorcraft_mcp::{Backend, Headless, Remote, Server};
/// stdout went away. A reader that stopped early (a closed pipe) ends the program quietly, as
/// ripgrep does; any other write error is reported.
@@ -79,9 +85,18 @@ USAGE:
Safe Mode is on. Plug-ins stay installed. `--safe-mode mcp` serves a headless session and
does not take --connect.
vectorcraft-cli mcp [--connect ADDR | --headless] [--automation-read-root DIR]... [--automation-write-root DIR]... [--allow-plugin-grants]
Run the MCP server on stdio. Default: connect to a running app at 127.0.0.1:7979
(vectorcraft --control 7979), falling back to a headless in-process session.
vectorcraft-cli mcp [--headless] [--when-closed launch|headless|error] [--automation-read-root DIR]... [--automation-write-root DIR]... [--allow-plugin-grants]
Run the MCP server on stdio. Every tool call goes to the open VectorCraft window while AI
agents may use it (ArtCraft Launcher › Settings › AI agents), found through the connection
file the window publishes in the settings folder: no port or token to configure, and an app
opened after the session started is found too. Otherwise the call runs in a session without
a window. While agents may use VectorCraft but it is closed, the choice in Preferences › AI
Integrations applies (open VectorCraft, a session without a window in the agent folders, or
an error); --when-closed overrides it. Files of the open window's agent requests stay in the
agent folders set there (Documents/ArtCraft/Agents/VectorCraft when none is set); the
agent_folders tool names them. --headless always runs without a window.
--connect ADDR (deprecated, removed in a later release) drives an app started with
vectorcraft --control PORT and its VECTORCRAFT_CONTROL_TOKEN.
--automation-read-root / --automation-write-root confine the files the agent's commands read
(open, place, relink, library loads…) and write (save, export, package, library saves…) to
DIR, links followed; each may be given more than once, a path inside any of its folders being
@@ -286,6 +301,20 @@ fn with_stores(mut h: Headless) -> Headless {
h
}
/// Plain `mcp`'s window lookup: the agent settings in `dir`, the closed-app choice (`--when-closed`
/// overrides the saved one), the desktop app beside this CLI to start, and headless sessions with
/// the user's stores, or confined to the agent folders without them.
fn live_config(dir: std::path::PathBuf, when_closed: Option<WhenAppClosed>) -> LiveConfig {
let mut cfg = LiveConfig::new(dir, agent_settings::read_settings);
cfg.when_closed = when_closed;
cfg.launch = std::env::current_exe().ok().and_then(|cli| craft::automation::agents::launcher(&cli, vectorcraft_mcp::agents::APP_EXE));
cfg.headless = Box::new(|roots| match roots {
Some(roots) => Headless::with_document().with_automation_roots(Some(roots)),
None => with_stores(Headless::with_document()),
});
cfg
}
/// Makes `h`'s `plugin.install` accept `grant` and `allowHeavy` when `allow` (`--allow-plugin-grants`).
fn accepting_grants(mut h: Headless, allow: bool) -> Headless {
if allow {
@@ -294,26 +323,34 @@ fn accepting_grants(mut h: Headless, allow: bool) -> Headless {
h
}
/// `mcp`. With `--safe-mode` (`safe_mode`) it serves a headless session: a running app loads its own
/// plug-ins, so `--connect` is refused and the default does not connect.
/// The stderr line `mcp --connect` prints: the flag works for one more release.
const CONNECT_DEPRECATED: &str = "vectorcraft-cli: --connect is deprecated and will be removed in a later release; plain `vectorcraft-cli mcp` finds the open VectorCraft window while AI agents are on in ArtCraft Launcher › Settings › AI agents";
/// `mcp`: the open VectorCraft window while the person lets agents use it (the ArtCraft Launcher's
/// switch), found through its connection file on every tool call, otherwise a headless session
/// (`vectorcraft_mcp::agents`). `--headless`, the automation roots, `--allow-plugin-grants` and
/// `--safe-mode` (`safe_mode`: a running app loads its own plug-ins) always run headless.
/// `--connect ADDR` drives an app started with `vectorcraft --control PORT` (deprecated).
fn mcp(args: &[String], safe_mode: bool) -> Result<(), String> {
if help_requested(args, &["--connect", "--automation-read-root", "--automation-write-root"]) {
if help_requested(args, &["--connect", "--when-closed", "--automation-read-root", "--automation-write-root"]) {
return help();
}
let mut connect: Option<String> = None;
let mut headless = false;
let mut when_closed: Option<String> = None;
let mut allow_grants = false;
let (mut read_roots, mut write_roots): (Vec<String>, Vec<String>) = (vec![], vec![]);
let mut it = args.iter();
while let Some(a) = it.next() {
// The roots also come as `--flag=DIR`, as the other Craft apps take them.
let (flag, inline) = match a.split_once('=') {
Some((f, v)) if f.starts_with("--automation-") => (f, Some(v.to_string())),
Some((f, v)) if f.starts_with("--automation-") || f == "--when-closed" => (f, Some(v.to_string())),
_ => (a.as_str(), None),
};
match flag {
"--connect" => connect = Some(it.next().cloned().ok_or("--connect needs an address")?),
"--headless" => headless = true,
"--when-closed" => when_closed = Some(inline.or_else(|| it.next().cloned()).ok_or("--when-closed needs launch, headless or error")?),
"--allow-plugin-grants" => allow_grants = true,
"--automation-read-root" | "--automation-write-root" => {
let dir = inline.or_else(|| it.next().cloned()).ok_or_else(|| format!("{flag} needs a folder"))?;
@@ -334,6 +371,16 @@ fn mcp(args: &[String], safe_mode: bool) -> Result<(), String> {
if safe_mode && connect.is_some() {
return Err("--safe-mode runs a headless session without plug-ins; a running app loads its own: start it with `vectorcraft --safe-mode --control <port>`".into());
}
let when_closed = match when_closed.as_deref() {
None => None,
Some(choice) => {
let parsed = WhenAppClosed::parse(choice).ok_or_else(|| format!("--when-closed must be launch, headless or error, not `{choice}`"))?;
if connect.is_some() || headless || safe_mode || allow_grants || !(read_roots.is_empty() && write_roots.is_empty()) {
return Err("--when-closed is for the open window of plain `mcp`; --connect, --headless, --safe-mode, --allow-plugin-grants and the automation roots never use it".into());
}
Some(parsed)
}
};
let read: Vec<&Path> = read_roots.iter().map(Path::new).collect();
let write: Vec<&Path> = write_roots.iter().map(Path::new).collect();
let roots = AutomationRoots::new(&read, &write)?;
@@ -349,13 +396,18 @@ fn mcp(args: &[String], safe_mode: bool) -> Result<(), String> {
} else if headless || safe_mode || allow_grants {
Box::new(accepting_grants(with_stores(Headless::with_document()), allow_grants))
} else if let Some(addr) = connect {
eprintln!("{CONNECT_DEPRECATED}");
// Explicit address: fail loudly if the app isn't there.
let token = vectorcraft_mcp::control_token()?;
Box::new(Remote::connect(&addr, &token).map_err(|e| format!("cannot connect to {addr}: {e}"))?)
} else {
// No token or no app listening: the headless session answers instead.
match vectorcraft_mcp::control_token().ok().and_then(|token| Remote::connect(DEFAULT_ADDR, &token).ok()) {
Some(r) => Box::new(r),
match prefs_dir::prefs_dir() {
Some(dir) => Box::new(LiveBackend::new(live_config(dir, when_closed))),
// No settings folder (no home folder): no window can be found either, so `--when-closed error`
// refuses rather than answering from a session the agent didn't ask for.
None if when_closed == Some(WhenAppClosed::Error) => {
return Err("VectorCraft's settings folder can't be found (no home folder), so its window can't be either: open VectorCraft, or run `mcp` without `--when-closed error`".into());
}
None => Box::new(with_stores(Headless::with_document())),
}
};
+17
View File
@@ -836,3 +836,20 @@ fn runs_without_preferences_remember_nothing() {
let r = run_in(&config, &[("pdf.preset.list", json!({}))]);
assert!(r[0]["presets"].as_array().unwrap().iter().all(|p| p["builtIn"] == true));
}
/// Plain `mcp` takes `--when-closed` for the open window only, and `--connect` still works for one
/// release but says it is deprecated before it connects.
#[test]
fn mcp_when_closed_and_the_deprecated_connect() {
let fails = |args: &[&str], why: &str| {
let out = Command::new(crate::bin()).args(args).env("VECTORCRAFT_NO_PREFS", "1").stdin(Stdio::null()).output().unwrap();
let err = String::from_utf8_lossy(&out.stderr);
assert!(!out.status.success() && err.contains(why), "{args:?}: {err}");
err.into_owned()
};
fails(&["mcp", "--when-closed", "later"], "--when-closed must be launch, headless or error");
fails(&["mcp", "--when-closed", "launch", "--headless"], "--when-closed is for the open window");
fails(&["mcp", "--when-closed=error", "--connect", "127.0.0.1:1"], "--when-closed is for the open window");
let err = fails(&["mcp", "--connect", "127.0.0.1:1"], "--connect is deprecated");
assert!(err.contains("cannot connect") || err.contains("no control token"), "{err}");
}