mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-30 18:24:11 +01:00
Merge pull request #2761 from storytold/feat/2534-compact-command-list
MCP, #2534: list_commands {brief} lists commands without their parameter docs
This commit is contained in:
3 files changed
+31
-10
No files matched your search
+21
-9
@@ -164,8 +164,14 @@ fn tools() -> Vec<Value> {
|
||||
tool(
|
||||
"list_commands",
|
||||
"List commands",
|
||||
"List FigCraft commands: id, label, menu path, shortcut, whether it is undoable, and a parameter description. Every menu item, panel action and shortcut is a command; run one with run_command (or several as one undo step with batch). Most commands act on `ids` when given, else on the selection. Use `filter` to narrow (e.g. \"align\", \"layout\", \"variable\", \"style\", \"component\", \"proto\").",
|
||||
obj(json!({"filter": string("Case-insensitive substring matched against id, label, menu path and parameter docs")}), &[]),
|
||||
"List FigCraft commands: id, label, menu path, shortcut, whether it is undoable, and a parameter description. Every menu item, panel action and shortcut is a command; run one with run_command (or several as one undo step with batch). Most commands act on `ids` when given, else on the selection. Use `filter` to narrow (e.g. \"align\", \"layout\", \"variable\", \"style\", \"component\", \"proto\"). `brief: true` leaves the parameter docs out (a fraction of the size) and matches `filter` against the id, label and menu path only; read one command's parameters from the figcraft://command/{id} resource or by filtering on its id.",
|
||||
obj(
|
||||
json!({
|
||||
"filter": string("Case-insensitive substring matched against id, label, menu path and (unless brief) parameter docs"),
|
||||
"brief": {"type": "boolean", "description": "One line per command without parameter docs"}
|
||||
}),
|
||||
&[],
|
||||
),
|
||||
true,
|
||||
),
|
||||
tool(
|
||||
@@ -763,16 +769,19 @@ pub fn call_tool(b: &mut dyn Backend, name: &str, args: &Value) -> ToolResult {
|
||||
|
||||
fn list_commands(b: &mut dyn Backend, a: &Map<String, Value>) -> Result<ToolResult, String> {
|
||||
let filter = s_arg(a, "filter")?.map(str::to_lowercase).unwrap_or_default();
|
||||
let brief = match a.get("brief") {
|
||||
None | Some(Value::Null) => false,
|
||||
Some(Value::Bool(v)) => *v,
|
||||
Some(_) => return Err("`brief` must be true or false".into()),
|
||||
};
|
||||
let fields: &[&str] = if brief { &["id", "label", "menu"] } else { &["id", "label", "menu", "params"] };
|
||||
let all = b.call("engine.commands", json!({}))?;
|
||||
let list: Vec<Value> = all
|
||||
.as_array()
|
||||
.map(|v| {
|
||||
v.iter()
|
||||
.filter(|c| {
|
||||
filter.is_empty()
|
||||
|| ["id", "label", "menu", "params"]
|
||||
.iter()
|
||||
.any(|k| c.get(*k).and_then(Value::as_str).is_some_and(|s| s.to_lowercase().contains(&filter)))
|
||||
filter.is_empty() || fields.iter().any(|k| c.get(*k).and_then(Value::as_str).is_some_and(|s| s.to_lowercase().contains(&filter)))
|
||||
})
|
||||
.cloned()
|
||||
.collect()
|
||||
@@ -782,7 +791,8 @@ fn list_commands(b: &mut dyn Backend, a: &Map<String, Value>) -> Result<ToolResu
|
||||
return Ok(ToolResult::text(format!("no command matches `{filter}`")));
|
||||
}
|
||||
// One compact line per command keeps this readable for agents.
|
||||
let mut out = format!("{} commands (id — label [menu] (shortcut) params; * = undoable):\n", list.len());
|
||||
let head = if brief { "id — label [menu] (shortcut); * = undoable" } else { "id — label [menu] (shortcut) params; * = undoable" };
|
||||
let mut out = format!("{} commands ({head}):\n", list.len());
|
||||
for c in &list {
|
||||
let g = |k: &str| c.get(k).and_then(Value::as_str).unwrap_or("");
|
||||
out.push_str(g("id"));
|
||||
@@ -797,8 +807,10 @@ fn list_commands(b: &mut dyn Backend, a: &Map<String, Value>) -> Result<ToolResu
|
||||
if !g("shortcut").is_empty() {
|
||||
out.push_str(&format!(" ({})", g("shortcut")));
|
||||
}
|
||||
out.push(' ');
|
||||
out.push_str(g("params"));
|
||||
if !brief {
|
||||
out.push(' ');
|
||||
out.push_str(g("params"));
|
||||
}
|
||||
out.push('\n');
|
||||
}
|
||||
Ok(ToolResult::text(out))
|
||||
|
||||
@@ -272,6 +272,15 @@ fn list_commands_filters() {
|
||||
let some = some.as_str().unwrap();
|
||||
assert!(some.contains("align.left") && !some.contains("file.save"));
|
||||
assert!(c.ok("list_commands", json!({"filter": "zzzzzz"})).as_str().unwrap().contains("no command"));
|
||||
// Brief (#2534): every command, a fraction of the size; the filter skips parameter docs.
|
||||
let brief = c.ok("list_commands", json!({"brief": true}));
|
||||
let brief = brief.as_str().unwrap();
|
||||
assert_eq!(brief.lines().count(), all.lines().count(), "the same commands");
|
||||
assert!(brief.len() * 2 < all.len(), "much smaller: {} vs {}", brief.len(), all.len());
|
||||
let by_params = c.ok("list_commands", json!({"filter": "rootFontSize"}));
|
||||
assert!(by_params.as_str().unwrap().contains("inspect.code"), "a full listing matches parameter docs");
|
||||
assert!(c.ok("list_commands", json!({"filter": "rootFontSize", "brief": true})).as_str().unwrap().contains("no command"));
|
||||
assert!(c.err("list_commands", json!({"brief": "yes"})).contains("brief"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+1
-1
@@ -457,7 +457,7 @@ a later step can use `{"$step": 0, "field": "ids/0"}` to target the first match.
|
||||
|
||||
| Tool | What it does |
|
||||
|---|---|
|
||||
| `list_commands {filter?}` | Every command: id, label, menu, shortcut, undoable, params doc. |
|
||||
| `list_commands {filter?, brief?}` | Every command: id, label, menu, shortcut, undoable, params doc. `brief: true` leaves the params docs out (a fraction of the size, for scanning all commands) and matches `filter` against the id, label and menu only; one command's params come from `figcraft://command/{id}` (#2534). |
|
||||
| `run_command {command, params}` | Run any command (e.g. `align.left`, `layout.add`, `component.create`, `instance.place` (drop an instance at a page point into the frame or auto layout slot under it), `component.exposeInstance` / `instance.exposed` (nested instance properties on instances), `component.makeSlot` / `instance.slotInsert` / `instance.resetSlot` (slots, below), `inspect.code` (CSS, HTML + CSS for a whole frame, React, Leptos, Vue, SwiftUI, Jetpack Compose, ArkUI or Flutter for a layer; components take their properties as parameters and instances are calls, see Developer Mode below), `inspect.properties` (layout, auto layout, typography, fills, strokes and effects with their style and variable names; `colorFormat`: hex, rgb, css, hsl or hsb), `inspect.assets` (layers under the selection with export settings, icons and images, each with its `kind`), `inspect.code` takes `unit: rem` and `rootFontSize` for CSS, HTML, React, Leptos and Vue, `style.create`, `variable.bind`, `variable.update` (rename, describe, scope), `variable.set` with `{alias}` or a colour calculation (`{alpha}`, `{mix}`), `variable.renameMode` / `variable.removeMode`, `variable.deleteCollection`, `proto.interaction.add`, `codeConnect.set` (components linked to real code, see Developer Mode below)). |
|
||||
| `batch {commands: [{command, params}], label?}` | Several commands as one undo step (`command.batch`), which keeps one copy of the document however many steps there are (an `edit.undo` step undoes the batch's edits so far together); all-or-nothing for open documents and their document state. Failed inner commands are removed from the journal; preferences, clipboard, tool state and UI requests are not rolled back. A step can use what an earlier step returned: name it with `"as": "card"` and `{"$ref": "card"}` anywhere in a later step's params becomes the `id` in its result (`{"$step": n}` does the same by zero-based position); `"field"` takes another key or a slash path such as `ids/0` (see Step references below). |
|
||||
| `create_node {type, x, y, width, height, parent, index, name, text, path, props, fields?, outline?}` | Create a frame, rectangle, ellipse, polygon, star, line, arrow, text, vector, section, component or slice; returns `{id, node}`. `warnings` reports a fontStyle that draws in another face. `fields` / `outline` shrink `node` as they do for `get_node`; a bad option is an error and creates nothing. |
|
||||
|
||||
Reference in new issue
Block a user