From 7f8af3beeb4da90499f0dfabcc2a97a239bd52b5 Mon Sep 17 00:00:00 2001 From: SoftFever Date: Sun, 22 Nov 2026 18:19:51 +0800 Subject: [PATCH] Browse settings by page with settings_list_pages, and read them by key settings_get and preferences_get matched an agent's words, so a goal such as "add a brim" found the wrong settings. Agents now list a settings tab's pages and groups with settings_list_pages, read a page's settings and what each does with settings_list, and read named settings with settings_get and preferences by tab with preferences_get. --- docs/HLSD/mcp-server.md | 39 +++--- docs/HLSD/mcp-tools.md | 11 +- src/slic3r/GUI/Mcp/McpArguments.cpp | 71 +--------- src/slic3r/GUI/Mcp/McpArguments.hpp | 11 -- src/slic3r/GUI/Mcp/McpServerInfo.cpp | 5 +- src/slic3r/GUI/Mcp/McpTools.hpp | 11 +- src/slic3r/GUI/Mcp/McpToolsApi.cpp | 2 +- src/slic3r/GUI/Mcp/McpToolsConfig.cpp | 153 ++++++++++++--------- src/slic3r/GUI/Mcp/McpToolsPreferences.cpp | 29 +--- tests/data/command_catalog.json | 55 ++++++-- tests/slic3rutils/test_command_catalog.cpp | 38 +++-- tests/slic3rutils/test_mcp_config.cpp | 65 ++++----- tests/slic3rutils/test_mcp_tools.cpp | 25 +--- tools/mcp/test-entries/presets.md | 6 +- tools/mcp/test_presets.py | 35 +++-- 15 files changed, 257 insertions(+), 299 deletions(-) diff --git a/docs/HLSD/mcp-server.md b/docs/HLSD/mcp-server.md index cbc765383d..13ba78b766 100644 --- a/docs/HLSD/mcp-server.md +++ b/docs/HLSD/mcp-server.md @@ -102,7 +102,8 @@ which an agent should prefer, and driving the GUI itself. They say to prefer the commands that act on project data over clicks and never retry a call that returned a job, that commands are named place first, then verb, and `tools/list` gives them place by place, that a call with exactly a Speed Dial action's arguments may ask as a person's pick does while any other runs from its -arguments, point to `settings_get {query}` and its meta for what a setting does, say how a gizmo opens and is +arguments, point to `settings_list_pages`, `settings_list {type, where}` and the meta of the settings they list +for what a setting does, say how a gizmo opens and is driven (and that `gizmo_pick_file` only stands in for a file picker inside one, since its name draws agents to it for any gizmo work), that the camera turns with `view_set` rather than a drag, and which indices are 0-based. They also say that `tool_list` lists the commands `tools/list` never shows (the three menu items that take @@ -1099,7 +1100,7 @@ edits in place. The other targets refuse `reset_all`; a null value removes an ov ### Reading, comparing and changing preset values -`settings_get` reads the settings named (`keys`) or searched for (`query`), or with `compare` those that +`settings_get` reads the settings named (`keys`), or with `compare` those that differ from another preset, or what a target other than `global` overrides; a call with none of these is refused, since `settings_list` lists all of a type's settings, so no call of `settings_get` is a second way to list them. On the target `global` it reads the preset being edited (for a filament, the slot's @@ -1107,7 +1108,7 @@ preset, edited or not), or with `preset` any saved preset of the type, selected it: the named preset is read as saved, without the edits the tab holds. `preset` and `filament_slot` are exclusive, and a name that matches no preset is refused with the closest names. `compare` lists only the keys, among -those named or matched, that differ from another preset: a saved preset by name, `:saved` (the read +those named, that differ from another preset: a saved preset by name, `:saved` (the read preset as saved, so the list is its unsaved changes, what the Unsaved Changes dialog shows) or `:parent` (the preset it inherits from, a system preset itself, what the tab's lock icons compare against). `:` cannot be part of a preset name, so the two never clash with one. The key list is @@ -1141,23 +1142,21 @@ where agents look: in the bench, agents read settings with the settings reads th as they called a separate explanation tool, and with the tooltips out of the meta they set a draft shield without skirt loops and a support threshold measured from the wrong side, mistakes the same agents avoided when the tooltip came with the value. The size stays bounded by the rule above: only -keys that are named, searched for or listed with their group carry meta. +keys that are named or listed with their group carry meta. -`settings_list`'s `where` lists one group ("Strength > Walls") or a whole page of the type with values -and meta; a name the type has no page or group for is an error that lists them all, which is the map of -the tabs. `settings_get`'s `query` serves an agent that knows what a setting should do but not its name -(`rank_settings()`). A query word matches a word of a key, label or description that starts with it, -so "retract" finds "retraction" while "to" does not match inside "toolchange"; words under three -letters count only when the query has nothing longer, since most help texts have them. Settings -matching more of the query's words come first, then those matching more of them in the key or label. -With no weighting by how rare a word is, a generic word such as "reduce" counts as much as -"stringing", so the tool asks for a few distinctive words. A search returns the best 20 unless `limit` -asks for more. +`settings_list_pages {type}` lists the pages and groups of the type's settings tab, both in alphabetical order +(the settings index the places come from keeps no tab order), each group with how many settings it holds +(`settings_pages()`), and `settings_list`'s `where` lists one group ("Strength > Walls") or a whole page of +the type with values and meta; a name the type has no page or group for is an error that lists them all. +Both read the same keys (`listed_settings()`), so a group's count is what `where` lists there. An agent +that knows what a setting should do but not its name reads the pages, picks the one that would hold it and +reads that page's settings with their descriptions. There is no search by words: a goal's words ("add a +brim", "stop the ooze") are seldom a setting's, and the agent matches what it wants against what each +setting does better than a match of words. -Without a `type`, a search and named keys look through the process, filament and printer settings -being edited, since a question about an effect rarely knows which tab answers it (stringing is -retraction on the printer tab and its overrides on the filament's), and an agent naming a key seldom -knows which tab holds it; each item names its `type`, the first of the three that has the key. Every +Without a `type`, named keys look through the process, filament and printer settings being edited, +since an agent naming a key seldom knows which tab holds it (retraction is on the printer tab, and its +overrides on the filament's); each item names its `type`, the first of the three that has the key. Every other read without a type (`preset`, `compare`) reads the process, and `settings_set` writes it: a write takes one type, whose values apply together, so it never spreads over several by itself. A key the type lacks is listed in `unknown_keys`, and `type_of` names where it is a setting (another type, or `project` @@ -1385,7 +1384,7 @@ once and changes nothing before then), and `refused` for a key `preferences_set` switch and the port of this server read as their controls show them (`Ops::shown_texts()`, which the dialog's rows read too): the switch is on while the server runs and the port is the one it is on, also where the session's `--mcp` and `--mcp-port` started it without storing either, so their `stored` says -only whether the configuration holds a value. `keys`, `query` and `tab` narrow the list together. A named key the +only whether the configuration holds a value. `keys` and `tab` narrow the list together. A named key the dialog does not show is read as the app configuration stores it, and one that is neither is listed under `unknown_keys`: the section also holds account and device state, which is read only by name. @@ -4799,7 +4798,7 @@ against one with more, what `tool_list` lists and its groups, every command's ma parameter, the tool names of `retired_names.hpp` that are no command, and which arguments the journal leaves out; `test_mcp_config` the keys and effective values of the settings commands' targets, the names and places `preset_save` accepts, enum labels and descriptions, the -settings search, per-variant entries and their writes, preset comparison, `changed`, which unsaved changes a +settings pages' groups, per-variant entries and their writes, preset comparison, `changed`, which unsaved changes a call discarded, which presets `preset_delete` refuses or deletes with a custom printer, the project's slot colours, flushing multipliers and matrix, a plate's filament orders, layer ranges and filament map and `plate_list`' layer numbers and layer G-codes, `preferences_get`'s list and `preferences_set`'s checks, the diff --git a/docs/HLSD/mcp-tools.md b/docs/HLSD/mcp-tools.md index 2b79dd4989..4ff863e7fc 100644 --- a/docs/HLSD/mcp-tools.md +++ b/docs/HLSD/mcp-tools.md @@ -120,7 +120,7 @@ them and `tool_call` runs them. | `app_check_for_updates` | Check for a newer OrcaSlicer | | | `app_show_config_folder` | Open the configuration folder in the file manager | | | `app_benchmark_3d_scene` | Time the 3D views on a test model: it asks first, then replaces the project as New Project does | | -| `preferences_get` | The preferences the Preferences dialog shows, as it shows them, with each one's kind, choices or range | `keys`, `query`, `tab` | +| `preferences_get` | The preferences the Preferences dialog shows, as it shows them, with each one's kind, choices or range | `keys`, `tab` | | `preferences_set` | Change preferences as their controls do, all checked first and written together | `values` | | `job_cancel` | Stop the running slice or export, or one job an MCP call started | `job` | @@ -209,8 +209,9 @@ them and `tool_call` runs them. | `preset_select` | Select a preset, as its selector does; a filament for a slot | `type`, `name`; `filament_slot`; `preset_changes`: transfer, discard | | `preset_save` | Save the preset being edited, with its unsaved changes, under a name, as the tab's Save does | `type`, `name`; `filament_slot`, `overwrite`, `inside_project`, `detach` | | `preset_delete` | Delete a user preset or one inside the project, its file and its cloud copy | `type`, `name`; `delete_attached` | +| `settings_list_pages` | The pages of a type's settings tab and the groups on each, with how many settings each holds: the places `settings_list` takes as `where` | `type`: process, printer, filament, object, region | | `settings_list` | Every setting of a type: the edited preset's, a saved one's or a slot's; or those an object, a part or a height range takes | `type`: process, printer, filament, object, region; `preset`, `filament_slot`, `where` | -| `settings_get` | The settings named or searched for, with what each does; or what an object, a part or a height range overrides, and where each value comes from | `keys`, `query`, `compare`; `type`; `target`: global, object, part, height_range | +| `settings_get` | The settings named, with what each does; or what an object, a part or a height range overrides, and where each value comes from | `keys`, `compare`; `type`; `target`: global, object, part, height_range | | `settings_set` | Change settings as typing them into their tab does: a preset's, or the overrides an object, a part or a height range carries | `values`; `type`, `target`; `reset_all`, `reset_to`: saved, parent | | `preset_import` | Import presets from a preset file, an archive or a bundle, or the settings a G-code was sliced with | `path`; `on_conflict`: skip, overwrite | | `preset_export` | Open the Export Preset Bundle dialog, which the caller answers | `path`, the folder | @@ -321,7 +322,7 @@ them and `tool_call` runs them. | `part_change_type` | `subtype` | center, left, right, enforced, blocked, neutral, beside `type` precise_seam or alone | | `calibration_generate_test` | `test` | temperature, max_volumetric, pressure_advance, flow_ratio, retraction, vfa, cornering, input_shaping_freq, input_shaping_damp | | `preset_list`, `preset_select`, `preset_save`, `preset_delete` | `type` | printer, process, filament | -| `settings_list` | `type` | process, printer, filament, object, region | +| `settings_list`, `settings_list_pages` | `type` | process, printer, filament, object, region | | `settings_get`, `settings_set` | `type`; `target` | printer, process, filament; global, object, part, height_range | | `settings_set_mode` | `mode` | simple, advanced, expert | | `filament_sync_ams` | `mode` | map, overwrite | @@ -654,9 +655,9 @@ Each command runs under a guard (mcp-server.md, Threading): Access labels, which `tools/list` sends as MCP's annotations (mcp-server.md, Tool catalog, says why each is set as it is): -- **read_only**, 24 commands: `tool_list`, `tool_describe`, `action_list`, `app_state`, `app_wait`, `app_log`, +- **read_only**, 25 commands: `tool_list`, `tool_describe`, `action_list`, `app_state`, `app_wait`, `app_log`, `preferences_get`, `project_get_settings`, `filament_list`, `object_list`, `plate_get_settings`, `plate_list`, - `plate_slice_stats`, `preset_list`, `settings_list`, `settings_get`, `printer_list_models`, `printer_status`, + `plate_slice_stats`, `preset_list`, `settings_list`, `settings_list_pages`, `settings_get`, `printer_list_models`, `printer_status`, `plugin_list`, `window_list`, `window_screenshot`, `script_list_api`, `script_describe_api` and `widget_list`. These are idempotent too, and so is `dialog_auto_answer`. - **destructive**, 3: `preset_delete` and `plugin_uninstall`, whose every call removes what a person made, diff --git a/src/slic3r/GUI/Mcp/McpArguments.cpp b/src/slic3r/GUI/Mcp/McpArguments.cpp index c2b2c2c54a..8819384b3d 100644 --- a/src/slic3r/GUI/Mcp/McpArguments.cpp +++ b/src/slic3r/GUI/Mcp/McpArguments.cpp @@ -1,7 +1,7 @@ // Argument checking and name matching for the command catalog: the schema validator, the normalizer that // reads what agents send in place of a schema's form, what tells a command's person's form from its silent -// one, the words and scores of the searches, and the edit distance every "did you mean" ranks with. It -// includes no wx header: it is a unit of the library libslic3r_automation, which is built without wx. +// one, and the edit distance every "did you mean" ranks with. It includes no wx header: it is a unit of the +// library libslic3r_automation, which is built without wx. #include "McpArguments.hpp" @@ -10,11 +10,9 @@ #include #include -#include #include #include #include -#include #include #include #include @@ -199,49 +197,6 @@ void coerce_value(const json& schema, json& value) } } -// The words of `text` in lower case, split at anything but a letter or a digit. With `camel`, a word -// with a capital after a lower-case letter also counts as its parts: TriangleMesh as triangle and mesh. -std::vector text_words(const std::string& text, bool camel) -{ - std::vector words; - std::string word; - std::vector parts; // where each CamelCase part after the first starts in `word` - const auto flush = [&] { - if (word.empty()) - return; - std::size_t from = 0; - for (const std::size_t at : parts) { - words.push_back(word.substr(from, at - from)); - from = at; - } - if (!parts.empty()) - words.push_back(word.substr(from)); - words.push_back(std::exchange(word, {})); - parts.clear(); - }; - unsigned char previous = 0; - for (const char c : text) { - const auto u = static_cast(c); - if (!std::isalnum(u)) { - flush(); - previous = 0; - continue; - } - if (camel && std::isupper(u) && std::islower(previous)) - parts.push_back(word.size()); - word += char(std::tolower(u)); - previous = u; - } - flush(); - return words; -} - -// Whether a word of `words` starts with `w`. -bool has_word(const std::vector& words, const std::string& w) -{ - return std::any_of(words.begin(), words.end(), [&w](const std::string& word) { return word.compare(0, w.size(), w) == 0; }); -} - } // namespace std::size_t edit_distance(const std::string& a, const std::string& b) @@ -416,28 +371,6 @@ ToolFailure argument_refusal(const std::string& name, const std::string& error) {{"code", "invalid_argument"}, {"hint", "tool_describe {tool: \"" + name + "\"} gives its parameters"}}); } -std::vector query_words(const std::string& query) -{ - std::vector words = text_words(query, false); - std::vector longer; - std::copy_if(words.begin(), words.end(), std::back_inserter(longer), [](const std::string& w) { return w.size() >= 3; }); - return longer.empty() ? words : longer; -} - -std::pair match_score(const std::vector& words, const std::string& name, const std::string& text) -{ - const std::vector in_name = text_words(name, true); - const std::vector in_text = text_words(text, true); - std::pair score; - for (const std::string& w : words) - if (has_word(in_name, w)) { - ++score.first; - ++score.second; - } else if (has_word(in_text, w)) - ++score.first; - return score; -} - std::string first_sentence(const std::string& text) { std::string line = text; diff --git a/src/slic3r/GUI/Mcp/McpArguments.hpp b/src/slic3r/GUI/Mcp/McpArguments.hpp index fb029f2f4a..9d12b2db34 100644 --- a/src/slic3r/GUI/Mcp/McpArguments.hpp +++ b/src/slic3r/GUI/Mcp/McpArguments.hpp @@ -80,17 +80,6 @@ void normalize_arguments(const nlohmann::json& schema, nlohmann::json& args); // each lookup with its own threshold. std::size_t edit_distance(const std::string& a, const std::string& b); -// The words of a search query: lower case, split at anything but a letter or a digit. "to" or "of" -// starts a word of most help texts, so words of fewer than three letters are left out when the query -// has longer ones. -std::vector query_words(const std::string& query); - -// How well an entry matches the words of query_words(): {words found in `name` or `text`, of them those -// found in `name`}, {0, 0} when none is. A word is found where a word of the text starts with it, so -// "retract" finds "retraction"; the text's words are split at underscores, dots and the like and also -// at a capital in a CamelCase name, so "mesh" finds TriangleMesh. -std::pair match_score(const std::vector& words, const std::string& name, const std::string& text); - // The first sentence of `text` on one line, for a listing: up to the first full stop followed by a space // or the end, "e.g." and "i.e." aside; the whole text when it has none. std::string first_sentence(const std::string& text); diff --git a/src/slic3r/GUI/Mcp/McpServerInfo.cpp b/src/slic3r/GUI/Mcp/McpServerInfo.cpp index 8501ea4056..2aeebbab66 100644 --- a/src/slic3r/GUI/Mcp/McpServerInfo.cpp +++ b/src/slic3r/GUI/Mcp/McpServerInfo.cpp @@ -35,8 +35,9 @@ const char* kInstructions = "act on the selection, as a person's pick does. Any other call, with silent: true where a " "schema takes it, runs from its arguments; a dialog it still opens (preset_export's always) is in its " "description.\n" - "- Which setting does what: settings_get {query} (a few words, no type) searches the settings being edited; each " - "item names its type, which settings_set needs, and its meta its description and page.\n" + "- Which setting does what: settings_list_pages {type} lists a settings tab's pages, settings_list {type, where} a " + "page's settings with their descriptions, settings_get {keys} named ones (with no type, each names the type " + "settings_set needs).\n" "- Objects, parts and plates are 0-based, as object_list and plate_list number them; filament slots and Preview " "layers are 1-based.\n" "- Lists come in pages: {items, offset, total}, next_offset while more remain (call again with it); object_list " diff --git a/src/slic3r/GUI/Mcp/McpTools.hpp b/src/slic3r/GUI/Mcp/McpTools.hpp index 33c37a7d03..fa36bd24d5 100644 --- a/src/slic3r/GUI/Mcp/McpTools.hpp +++ b/src/slic3r/GUI/Mcp/McpTools.hpp @@ -387,13 +387,10 @@ bool save_preset_inside_project(const PresetCollection& presets, const std::stri // (the untranslated texts the GUI shows), in the same order. nlohmann::json option_meta(const ConfigOptionDef& def); -// settings_get's search: the keys of `within` that `query` describes, best first, at most `limit`. -// A query word matches a word of a key (split at _), its label or its tooltip that starts with it, so -// "retract" finds "retraction". Settings matching more of the query's words come first, then those -// matching more of them in the key or label, so of two that match alike, the one the query names -// outranks one whose help text only mentions it. Words of fewer than three letters are left out when -// the query has longer ones. -std::vector rank_settings(const std::string& query, const std::vector& within, std::size_t limit); +// settings_list_pages' reply: those of `keys`, in the order settings_list lists them, that have a place ("Page > Group", as +// the settings tab's rows give it) in `places`, as {pages: [{page, groups: [{group, settings}]}]}, pages and groups in +// alphabetical order, each group's settings being how many of `keys` it holds. Any thread. +nlohmann::json settings_pages(const std::map& places, const std::vector& keys); // For a per-variant or per-extruder vector `key` of a preset's `config` that has more than one entry: diff --git a/src/slic3r/GUI/Mcp/McpToolsApi.cpp b/src/slic3r/GUI/Mcp/McpToolsApi.cpp index 47766a4702..499ed18abe 100644 --- a/src/slic3r/GUI/Mcp/McpToolsApi.cpp +++ b/src/slic3r/GUI/Mcp/McpToolsApi.cpp @@ -190,7 +190,7 @@ const std::map& classes_by_path() // Settings and preferences: the keys a tool takes are those its reader lists. {"settings_get.keys", source({"settings_list"})}, {"settings_set.values", source({"settings_list"})}, - {"settings_list.where", kDescribed}, + {"settings_list.where", source({"settings_list_pages"})}, {"settings_get.min_z", source({"object_list"})}, {"settings_get.max_z", source({"object_list"})}, {"settings_set.min_z", source({"object_list"})}, diff --git a/src/slic3r/GUI/Mcp/McpToolsConfig.cpp b/src/slic3r/GUI/Mcp/McpToolsConfig.cpp index c5480ced15..3bc364b020 100644 --- a/src/slic3r/GUI/Mcp/McpToolsConfig.cpp +++ b/src/slic3r/GUI/Mcp/McpToolsConfig.cpp @@ -1942,25 +1942,22 @@ json option_meta(const ConfigOptionDef& def) return meta; } -std::vector rank_settings(const std::string& query, const std::vector& within, std::size_t limit) +json settings_pages(const std::map& places, const std::vector& keys) { - const std::vector words = query_words(query); - // (query words matched, those matched in the key or label) per key. - std::vector, std::string>> scored; - std::set seen; // a type's options can list a key twice - for (const std::string& key : within) { - const ConfigOptionDef* def = print_config_def.get(key); - if (def == nullptr || !seen.insert(key).second) - continue; - const std::pair score = match_score(words, key + ' ' + def->label + ' ' + def->full_label, def->tooltip); - if (score.first > 0) - scored.emplace_back(score, key); + // page -> group -> how many of `keys` it holds; the maps keep both in alphabetical order. + std::map> counted; + for (const std::string& key : keys) + if (const auto place = places.find(key); place != places.end()) + if (const std::size_t sep = place->second.find(" > "); sep != std::string::npos) + ++counted[place->second.substr(0, sep)][place->second.substr(sep + 3)]; + json pages = json::array(); + for (const auto& [page, groups] : counted) { + json listed = json::array(); + for (const auto& [group, settings] : groups) + listed.push_back({{"group", group}, {"settings", settings}}); + pages.push_back({{"page", page}, {"groups", std::move(listed)}}); } - std::stable_sort(scored.begin(), scored.end(), [](const auto& a, const auto& b) { return a.first > b.first; }); - std::vector out; - for (std::size_t i = 0; i < scored.size() && i < limit; ++i) - out.push_back(scored[i].second); - return out; + return {{"pages", std::move(pages)}}; } json variant_entries(const std::string& key, const DynamicPrintConfig& config, const std::vector& extruder_types, @@ -2611,25 +2608,20 @@ json target_reply(const ConfigTarget& t) // settings_get: the settings `args` asks for, read on the UI thread. json read_settings(const json& args) { - if (int(args.contains("keys")) + int(args.contains("query")) + int(args.contains("where")) > 1) - throw ToolFailure("Give one of keys, query or where"); - // A search or named keys with no type look through kSearchTypes; each item names its type. - if ((args.contains("query") || args.contains("keys")) && !args.contains("type") && arg_string(args, "target", "global") == "global" && + // Named keys with no type look through kSearchTypes; each item names its type. + if (args.contains("keys") && !args.contains("type") && arg_string(args, "target", "global") == "global" && !args.contains("preset") && !args.contains("compare")) { std::map type_of; const std::vector options = search_type_options(type_of); std::vector wanted; json unknown = json::array(); - if (args.contains("query")) - wanted = rank_settings(arg_string(args, "query"), options, 200); - else - for (const json& k : args["keys"]) { - const std::string key = k.get(); - if (type_of.count(key) == 0) - unknown.push_back(key); - else if (std::find(wanted.begin(), wanted.end(), key) == wanted.end()) - wanted.push_back(key); - } + for (const json& k : args["keys"]) { + const std::string key = k.get(); + if (type_of.count(key) == 0) + unknown.push_back(key); + else if (std::find(wanted.begin(), wanted.end(), key) == wanted.end()) + wanted.push_back(key); + } std::map found; for (const char* type : kSearchTypes) { json keys = json::array(); @@ -2639,7 +2631,6 @@ json read_settings(const json& args) if (keys.empty()) continue; json typed = args; - typed.erase("query"); typed.erase("offset"); typed.update({{"type", type}, {"keys", std::move(keys)}, {"limit", 500}}); json read = read_settings(typed); @@ -2653,22 +2644,22 @@ json read_settings(const json& args) for (const std::string& key : wanted) if (const auto it = found.find(key); it != found.end()) items.push_back(std::move(it->second)); - json out = page(items, args, args.contains("query") ? 20 : 100); + json out = page(items, args, 100); add_unknown_keys(out, unknown, options); return out; } return read_target(args, resolve_target(args, false)); } -// The settings of `t` that `args` asks for: its keys, query or where, its whole listing without them (for a target +// The settings of `t` that `args` asks for: its keys or where, its whole listing without them (for a target // other than global, what it overrides), or with compare what differs from another preset. `listing`: settings_list's, // paged by settings_list_page(). A key a target does not take is refused with where it is set. UI thread. json read_target(const json& args, const ConfigTarget& t, bool listing) { const bool compare = args.contains("compare"); // A preset's whole listing, hundreds of keys, is for scanning values; what one key takes - // comes with it when it is named, searched for or listed with its group. - const bool whole = !args.contains("keys") && !args.contains("query") && !args.contains("where") && t.object == nullptr && !t.project; + // comes with it when it is named or listed with its group. + const bool whole = !args.contains("keys") && !args.contains("where") && t.object == nullptr && !t.project; const bool meta = listing ? listed_with_meta(args) : arg_bool(args, "include_meta", !compare && !whole); if (t.object != nullptr && (compare || args.contains("preset"))) throw ToolFailure("preset and compare read a type's presets, which the targets object, part and height_range are not"); @@ -2679,8 +2670,6 @@ json read_target(const json& args, const ConfigTarget& t, bool listing) if (args.contains("keys")) { for (const json& k : args["keys"]) keys.push_back(k.get()); - } else if (args.contains("query")) { - keys = rank_settings(arg_string(args, "query"), t.options, 200); } else if (args.contains("where")) { const std::string where = lower(arg_string(args, "where")); std::set groups; @@ -2701,7 +2690,7 @@ json read_target(const json& args, const ConfigTarget& t, bool listing) keys = t.options; } // A target's overrides are listed whatever they are; a key named for a target is one it takes. - const bool overrides = t.object != nullptr && !args.contains("keys") && !args.contains("query"); + const bool overrides = t.object != nullptr && !args.contains("keys"); const std::vector& process = preset_options(Preset::TYPE_PRINT); std::vector known; json unknown = json::array(); @@ -2764,9 +2753,7 @@ json read_target(const json& args, const ConfigTarget& t, bool listing) add_meta(item, key); items.push_back(std::move(item)); } - // A search's matches carry their descriptions, and those past the best few rarely - // fit what was asked. - out = listing ? settings_list_page(items, args) : page(items, args, args.contains("query") ? 20 : 100); + out = listing ? settings_list_page(items, args) : page(items, args, 100); std::string note; if (part_only) note = "A part's value (source \"part\", or in parts) applies only inside that part's volume."; @@ -2787,13 +2774,15 @@ json read_target(const json& args, const ConfigTarget& t, bool listing) return out; } -// settings_list: a type's settings, a preset's or those an object or a region takes, which read the process -// preset's values. UI thread. -json list_settings(const json& args) +// The settings settings_list lists for `args`' type, and the arguments it reads them with: a type's preset (process, +// printer, filament) as `args` names it, or for object and region the process preset, its options cut to those an object +// or a region takes, whose values they print with unless they set their own. settings_list_pages groups the same +// settings, so that a group's count is what settings_list lists there. UI thread. +std::pair listed_settings(const json& args) { const std::string type = arg_string(args, "type"); if (type != "object" && type != "region") - return read_target(args, resolve_target(args, false), true); + return {resolve_target(args, false), args}; json process = args; process["type"] = "process"; ConfigTarget t = resolve_target(process, false); @@ -2802,14 +2791,33 @@ json list_settings(const json& args) t.options.erase(std::remove_if(t.options.begin(), t.options.end(), [&taken](const std::string& key) { return std::find(taken.begin(), taken.end(), key) == taken.end(); }), t.options.end()); - json out = read_target(process, t, true); - if (type == "region") + return {std::move(t), std::move(process)}; +} + +// settings_list: a type's settings, a preset's or those an object or a region takes. UI thread. +json list_settings(const json& args) +{ + const auto [t, read] = listed_settings(args); + json out = read_target(read, t, true); + if (arg_string(args, "type") == "region") for (json& item : out["items"]) if (item["key"] == "layer_height") item["targets"] = json::array({"height_range"}); return out; } +// settings_list_pages: the pages and groups of the settings settings_list lists for `args`' type. UI thread. +json list_setting_pages(const json& args) +{ + const std::pair listed = listed_settings(args); + // The keys settings_list lists: those of the target's options that the config defines (read_target()). + std::vector keys; + for (const std::string& key : listed.first.options) + if (print_config_def.get(key) != nullptr) + keys.push_back(key); + return settings_pages(setting_places(listed.first.type), keys); +} + } // namespace std::string settings_request_error(const json& args, bool writes) @@ -2833,8 +2841,8 @@ std::string settings_request_error(const json& args, bool writes) if (target == "height_range" && !(args.contains("min_z") && args.contains("max_z"))) return "target height_range needs min_z and max_z, the range's heights as object_list lists them"; } - // settings_set writes values, or takes a preset back to its saved state; settings_get reads what keys, query or - // compare names, or a target's overrides. + // settings_set writes values, or takes a preset back to its saved state; settings_get reads what keys or compare + // names, or a target's overrides. if (writes) { if (!args.contains("values") && !args.contains("reset_all")) return "Nothing is named: give values to change settings, or reset_all to discard the unsaved changes of a type's " @@ -2846,8 +2854,8 @@ std::string settings_request_error(const json& args, bool writes) return "extruders_count is set alone, with type printer: the extruders it adds take their settings in a following " "call"; } else if (target == "global" && !args.contains("keys") && !args.contains("query") && !args.contains("compare")) { - return "Nothing is named: give keys, query or compare to read settings, or a target other than global to read what " - "it overrides; settings_list lists a type's settings"; + return "Nothing is named: give keys or compare to read settings, or a target other than global to read what it " + "overrides; settings_list lists a type's settings, and settings_list_pages their pages"; } return {}; } @@ -3115,9 +3123,9 @@ void register_config_tools(CommandCatalog& catalog) "filament_slot a slot's. object: the process settings an object or an assembly takes; region: those a part, a " "modifier or a height range takes, whose layer_height item has targets [\"height_range\"], a height range alone " "taking it; in both, each with the process preset's value, which an object or a region prints with unless it " - "sets its own. With where, the settings of one settings page or \"Page > Group\" (an unknown one is refused with " - "the list of them). Items with meta, which where gives them, come 100 a page. settings_get reads the settings " - "named or searched for, and what an object, a part or a height range overrides.", + "sets its own. With where, the settings of one settings page or \"Page > Group\", as settings_list_pages lists them " + "(an unknown one is refused with the list of them). Items with meta, which where gives them, come 100 a page. " + "settings_get reads settings by key, and what an object, a part or a height range overrides.", object_schema({{"type", enum_prop({"process", "printer", "filament", "object", "region"}, "Which settings: a preset's, or those an object or a region takes.")}, {"where", prop("string", "A settings page, or \"Page > Group\" as meta.where gives it, e.g. " @@ -3139,22 +3147,39 @@ void register_config_tools(CommandCatalog& catalog) })); })); + catalog.add(inspect_tool( + "settings_list_pages", L("List settings pages"), + "The pages of a type's settings tab and the groups on each, pages and groups in alphabetical order, each group with " + "how many of the settings settings_list lists for the same type it holds: settings_list {type, where} lists that " + "many there. A page and a \"Page > Group\" are each a where settings_list takes, which lists those settings with " + "their values and what each does. A setting with no row on a page is in no group; settings_list without where " + "lists it.", + object_schema({{"type", enum_prop({"process", "printer", "filament", "object", "region"}, + "Which settings, as settings_list takes them: a preset's, or those an object or a " + "region takes.")}}, + {"type"}), + [](const json& args, ToolContext& ctx) { + return ToolResult::ok(ctx.run_ui([&] { + ui_plater(); // the presets and the settings pages exist once the main window does + return list_setting_pages(args); + })); + })); + catalog.add(inspect_tool( "settings_get", L("Read settings"), - "Read the settings named or searched for: each item has its value, as OrcaSlicer writes it, and meta: label, " + "Read the settings named: each item has its value, as OrcaSlicer writes it, and meta: label, " "description (what the setting does, when to change it), where (its settings page and group), type, unit, range, " - "allowed values and mode. Choose them by keys, or by query, a few words matched in keys, labels and descriptions, " - "best matches first. Keys the settings lack are listed in unknown_keys, type_of gives where each of those is a " + "allowed values and mode. Keys the settings lack are listed in unknown_keys, type_of gives where each of those is a " "setting elsewhere (a type, or project for the project's own settings), and similar the keys like each of the others. " - "It needs keys, query, compare or a target " - "other than global; settings_list lists all of a type's settings.\n" - "- No type: keys and query look through the process, filament and printer settings, and each item names its " + "It needs keys, compare or a target other than global; settings_list lists all of a type's settings, a page " + "at a time with where, and settings_list_pages the pages.\n" + "- No type: keys look through the process, filament and printer settings, and each item names its " "type, the one settings_set takes; otherwise the type is the process.\n" "- type printer, process or filament (target global): the preset being edited, or with preset a saved one; " "compare lists only the keys that differ from another preset (value and other). A per-variant value lists in meta " "its variants (what each entry is for) and active (the entry each extruder prints with).\n" "- target object (object), part (object and part) or height_range (object, min_z and max_z), the object or part " - "selected in Prepare where none is named: without keys, what it overrides; keys and query read the process " + "selected in Prepare where none is named: without keys, what it overrides; keys read the process " "settings it takes (settings_list type object or region lists them) and extruder, its filament, and a key it does " "not take is refused with where it is set. Each has value (its own, null where it has none), effective (the " "value it prints with) and source, in the order the slicer applies them: height_range, part, object, global or " @@ -3170,8 +3195,6 @@ void register_config_tools(CommandCatalog& catalog) object_schema({{"type", type_prop}, {"target", target_prop}, {"keys", array_prop({{"type", "string"}}, "Option keys, e.g. [\"layer_height\", \"wall_loops\"].")}, - {"query", prop("string", "A few distinctive words, e.g. \"stringing\" or \"ooze travel\"; the best 20 " - "matches unless limit says otherwise.")}, {"object", object_prop}, {"part", part_prop}, {"min_z", min_z_prop}, @@ -3183,7 +3206,7 @@ void register_config_tools(CommandCatalog& catalog) {"compare", prop("string", "Target global: a preset name, \":saved\" (the unsaved changes) or \":parent\" " "(the preset it inherits from).")}, {"include_meta", prop("boolean", "Include labels, types and ranges. Default true, false with compare.")}, - {"limit", {{"type", "integer"}, {"minimum", 1}, {"maximum", 500}, {"description", "Default 100, or 20 with query."}}}, + {"limit", {{"type", "integer"}, {"minimum", 1}, {"maximum", 500}, {"description", "Default 100."}}}, {"offset", {{"type", "integer"}, {"minimum", 0}}}}), [](const json& args, ToolContext& ctx) { // Refused before the UI is asked for anything: what the arguments alone decide. diff --git a/src/slic3r/GUI/Mcp/McpToolsPreferences.cpp b/src/slic3r/GUI/Mcp/McpToolsPreferences.cpp index 6716252b9e..9fc0908f31 100644 --- a/src/slic3r/GUI/Mcp/McpToolsPreferences.cpp +++ b/src/slic3r/GUI/Mcp/McpToolsPreferences.cpp @@ -15,11 +15,9 @@ #include #include -#include #include #include #include -#include #include #include #include @@ -41,12 +39,6 @@ const char* kind_name(Ops::PreferenceKind kind) return "text"; } -std::string lower(std::string s) -{ - std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return char(std::tolower(c)); }); - return s; -} - // A bound of a number's range, whole where the row takes whole numbers. json bound(const Ops::Preference& p, double value) { @@ -134,21 +126,14 @@ json changes_json(const Ops::PreferenceChanges& changes, const std::vector& shown, const json& args, const std::map& shown_texts) { - const std::string tab = arg_string(args, "tab"); - std::vector words; - std::istringstream query(lower(arg_string(args, "query"))); - for (std::string word; query >> word;) - words.push_back(word); + const std::string tab = arg_string(args, "tab"); std::vector asked; if (args.contains("keys")) for (const json& key : args["keys"]) asked.push_back(key.get()); const auto named = [&asked](const std::string& key) { return std::find(asked.begin(), asked.end(), key) != asked.end(); }; const auto matches = [&](const Ops::Preference& p) { - if ((!tab.empty() && p.tab != tab) || (args.contains("keys") && !named(p.key))) - return false; - const std::string text = lower(p.key + " " + p.label + " " + p.section); - return std::all_of(words.begin(), words.end(), [&text](const std::string& word) { return text.find(word) != std::string::npos; }); + return (tab.empty() || p.tab == tab) && (!args.contains("keys") || named(p.key)); }; json items = json::array(), unknown = json::array(); @@ -186,14 +171,12 @@ void register_preference_tools(CommandCatalog& catalog) "restart_needed: true for a preference read at the next start, which a write changes nothing before, and " "refused, the reason, for a preference preferences_set does not write. The MCP server's switch and port read as " "the dialog shows them: on while the server runs, and the port it is on, also where this session's launch " - "arguments set them without storing them; their stored says whether a value is stored. keys, query and tab " - "narrow the list together; a " + "arguments set them without storing them; their stored says whether a value is stored. keys and tab narrow " + "the list together; a " "named key the dialog does not show is read as the app configuration stores it ({key, value}), and one that is " "neither is listed under unknown_keys. The app configuration also holds account and device state, which is read " "only by name.", object_schema({{"keys", array_prop({{"type", "string"}}, "Preference keys, e.g. [\"project_load_behaviour\"].")}, - {"query", prop("string", "Words to find in the keys, labels and section titles, in any case, e.g. " - "\"auto slice\".")}, {"tab", enum_prop({"general", "files", "behaviour", "control", "graphics", "online", "developer"}, "The dialog's tab, as window_open names it.")}}), [](const json& args, ToolContext& ctx) { @@ -244,8 +227,8 @@ void register_preference_tools(CommandCatalog& catalog) return ToolResult::ok(result); })); write.examples = json::parse(R"json([ - {"title": "Find a preference and turn it off", "calls": [ - {"tool": "preferences_get", "args": {"query": "arrange"}}, + {"title": "Read the Behaviour tab's preferences and turn one off", "calls": [ + {"tool": "preferences_get", "args": {"tab": "behaviour"}}, {"tool": "preferences_set", "args": {"values": {"auto_arrange": false}}}]}, {"title": "Set a choice and a number", "calls": [ {"tool": "preferences_set", "args": {"values": {"auto_calculate_flush": "color change", "opengl_fps_cap": 60}}}]}])json"); diff --git a/tests/data/command_catalog.json b/tests/data/command_catalog.json index 56d600d1d5..feaf1f7f41 100644 --- a/tests/data/command_catalog.json +++ b/tests/data/command_catalog.json @@ -622,7 +622,7 @@ }, { "completion": "immediate", - "description": "Read the app's preferences as the Preferences dialog shows them. With no argument, every preference it shows, tab by tab in its order, each with key (its control is named after it), tab, section and label (the row's title, in English), kind (checkbox, choice, number or text), value (what the dialog shows: true or false for a checkbox, a number for a number field), a choice's choices ({value, label}; preferences_set takes the value), a number's min and max, stored: false where nothing is stored and the value is the one the dialog shows then, restart_needed: true for a preference read at the next start, which a write changes nothing before, and refused, the reason, for a preference preferences_set does not write. The MCP server's switch and port read as the dialog shows them: on while the server runs, and the port it is on, also where this session's launch arguments set them without storing them; their stored says whether a value is stored. keys, query and tab narrow the list together; a named key the dialog does not show is read as the app configuration stores it ({key, value}), and one that is neither is listed under unknown_keys. The app configuration also holds account and device state, which is read only by name.", + "description": "Read the app's preferences as the Preferences dialog shows them. With no argument, every preference it shows, tab by tab in its order, each with key (its control is named after it), tab, section and label (the row's title, in English), kind (checkbox, choice, number or text), value (what the dialog shows: true or false for a checkbox, a number for a number field), a choice's choices ({value, label}; preferences_set takes the value), a number's min and max, stored: false where nothing is stored and the value is the one the dialog shows then, restart_needed: true for a preference read at the next start, which a write changes nothing before, and refused, the reason, for a preference preferences_set does not write. The MCP server's switch and port read as the dialog shows them: on while the server runs, and the port it is on, also where this session's launch arguments set them without storing them; their stored says whether a value is stored. keys and tab narrow the list together; a named key the dialog does not show is read as the app configuration stores it ({key, value}), and one that is neither is listed under unknown_keys. The app configuration also holds account and device state, which is read only by name.", "destructive": false, "group": "The app", "guard": "inspect", @@ -638,10 +638,6 @@ }, "type": "array" }, - "query": { - "description": "Words to find in the keys, labels and section titles, in any case, e.g. \"auto slice\".", - "type": "string" - }, "tab": { "description": "The dialog's tab, as window_open names it.", "enum": [ @@ -674,7 +670,7 @@ "calls": [ { "args": { - "query": "arrange" + "tab": "behaviour" }, "tool": "preferences_get" }, @@ -687,7 +683,7 @@ "tool": "preferences_set" } ], - "title": "Find a preference and turn it off" + "title": "Read the Behaviour tab's preferences and turn one off" }, { "calls": [ @@ -5340,7 +5336,7 @@ }, { "completion": "immediate", - "description": "All the settings of a type in one reply, each item with its value as OrcaSlicer writes it, and without meta unless include_meta. process, printer and filament: the preset being edited, or with preset a saved one, or with filament_slot a slot's. object: the process settings an object or an assembly takes; region: those a part, a modifier or a height range takes, whose layer_height item has targets [\"height_range\"], a height range alone taking it; in both, each with the process preset's value, which an object or a region prints with unless it sets its own. With where, the settings of one settings page or \"Page > Group\" (an unknown one is refused with the list of them). Items with meta, which where gives them, come 100 a page. settings_get reads the settings named or searched for, and what an object, a part or a height range overrides.", + "description": "All the settings of a type in one reply, each item with its value as OrcaSlicer writes it, and without meta unless include_meta. process, printer and filament: the preset being edited, or with preset a saved one, or with filament_slot a slot's. object: the process settings an object or an assembly takes; region: those a part, a modifier or a height range takes, whose layer_height item has targets [\"height_range\"], a height range alone taking it; in both, each with the process preset's value, which an object or a region prints with unless it sets its own. With where, the settings of one settings page or \"Page > Group\", as settings_list_pages lists them (an unknown one is refused with the list of them). Items with meta, which where gives them, come 100 a page. settings_get reads settings by key, and what an object, a part or a height range overrides.", "destructive": false, "group": "Presets and settings", "guard": "inspect", @@ -5402,7 +5398,42 @@ }, { "completion": "immediate", - "description": "Read the settings named or searched for: each item has its value, as OrcaSlicer writes it, and meta: label, description (what the setting does, when to change it), where (its settings page and group), type, unit, range, allowed values and mode. Choose them by keys, or by query, a few words matched in keys, labels and descriptions, best matches first. Keys the settings lack are listed in unknown_keys, type_of gives where each of those is a setting elsewhere (a type, or project for the project's own settings), and similar the keys like each of the others. It needs keys, query, compare or a target other than global; settings_list lists all of a type's settings.\n- No type: keys and query look through the process, filament and printer settings, and each item names its type, the one settings_set takes; otherwise the type is the process.\n- type printer, process or filament (target global): the preset being edited, or with preset a saved one; compare lists only the keys that differ from another preset (value and other). A per-variant value lists in meta its variants (what each entry is for) and active (the entry each extruder prints with).\n- target object (object), part (object and part) or height_range (object, min_z and max_z), the object or part selected in Prepare where none is named: without keys, what it overrides; keys and query read the process settings it takes (settings_list type object or region lists them) and extruder, its filament, and a key it does not take is refused with where it is set. Each has value (its own, null where it has none), effective (the value it prints with) and source, in the order the slicer applies them: height_range, part, object, global or default; an object's item lists in parts the parts and modifiers that override the key inside their own volume, and an object's or a part's item lists in height_ranges the ranges whose value applies to it between their heights. Inside a modifier, mesh or painted, that sets none of a key, the value of the part it overlaps applies, which the command does not work out: where a part (or a modifier of either kind before it) sets the key, the modifier's item has effective null, source overlapped_part, object_effective and object_source (the object's value), and in parts those that set it. These settings are process settings. The reply names the target (objects or parts, and height_range).\npreferences_get reads the app's preferences, plate_get_settings a plate's settings and project_get_settings the project's own.", + "description": "The pages of a type's settings tab and the groups on each, pages and groups in alphabetical order, each group with how many of the settings settings_list lists for the same type it holds: settings_list {type, where} lists that many there. A page and a \"Page > Group\" are each a where settings_list takes, which lists those settings with their values and what each does. A setting with no row on a page is in no group; settings_list without where lists it.", + "destructive": false, + "group": "Presets and settings", + "guard": "inspect", + "has_own_action": false, + "idempotent": true, + "input_schema": { + "additionalProperties": false, + "properties": { + "type": { + "description": "Which settings, as settings_list takes them: a preset's, or those an object or a region takes.", + "enum": [ + "process", + "printer", + "filament", + "object", + "region" + ], + "type": "string" + } + }, + "required": [ + "type" + ], + "type": "object" + }, + "key": "settings_list_pages", + "listed": true, + "open_world": false, + "operations": [], + "read_only": true, + "title": "List settings pages" + }, + { + "completion": "immediate", + "description": "Read the settings named: each item has its value, as OrcaSlicer writes it, and meta: label, description (what the setting does, when to change it), where (its settings page and group), type, unit, range, allowed values and mode. Keys the settings lack are listed in unknown_keys, type_of gives where each of those is a setting elsewhere (a type, or project for the project's own settings), and similar the keys like each of the others. It needs keys, compare or a target other than global; settings_list lists all of a type's settings, a page at a time with where, and settings_list_pages the pages.\n- No type: keys look through the process, filament and printer settings, and each item names its type, the one settings_set takes; otherwise the type is the process.\n- type printer, process or filament (target global): the preset being edited, or with preset a saved one; compare lists only the keys that differ from another preset (value and other). A per-variant value lists in meta its variants (what each entry is for) and active (the entry each extruder prints with).\n- target object (object), part (object and part) or height_range (object, min_z and max_z), the object or part selected in Prepare where none is named: without keys, what it overrides; keys read the process settings it takes (settings_list type object or region lists them) and extruder, its filament, and a key it does not take is refused with where it is set. Each has value (its own, null where it has none), effective (the value it prints with) and source, in the order the slicer applies them: height_range, part, object, global or default; an object's item lists in parts the parts and modifiers that override the key inside their own volume, and an object's or a part's item lists in height_ranges the ranges whose value applies to it between their heights. Inside a modifier, mesh or painted, that sets none of a key, the value of the part it overlaps applies, which the command does not work out: where a part (or a modifier of either kind before it) sets the key, the modifier's item has effective null, source overlapped_part, object_effective and object_source (the object's value), and in parts those that set it. These settings are process settings. The reply names the target (objects or parts, and height_range).\npreferences_get reads the app's preferences, plate_get_settings a plate's settings and project_get_settings the project's own.", "destructive": false, "group": "Presets and settings", "guard": "inspect", @@ -5432,7 +5463,7 @@ "type": "array" }, "limit": { - "description": "Default 100, or 20 with query.", + "description": "Default 100.", "maximum": 500, "minimum": 1, "type": "integer" @@ -5465,10 +5496,6 @@ "description": "Read this saved preset of the type (a name from preset_list) instead of the one being edited.", "type": "string" }, - "query": { - "description": "A few distinctive words, e.g. \"stringing\" or \"ooze travel\"; the best 20 matches unless limit says otherwise.", - "type": "string" - }, "target": { "description": "Where they apply: global (default), the type's preset; object, part or height_range, the overrides an object, one of its parts or modifiers, or one of its height ranges carries, which are process settings.", "enum": [ diff --git a/tests/slic3rutils/test_command_catalog.cpp b/tests/slic3rutils/test_command_catalog.cpp index 10ddf6c234..df82bd6d46 100644 --- a/tests/slic3rutils/test_command_catalog.cpp +++ b/tests/slic3rutils/test_command_catalog.cpp @@ -144,7 +144,7 @@ std::string first_difference(const std::string& actual, const std::string& expec } // The design's command set outside the Design tab, heading by heading in the order tools/list gives them, Driving the -// GUI last, after the Design tab's commands: the 137 commands of the catalog, its typed tools, the commands made from +// GUI last, after the Design tab's commands: the 138 commands of the catalog, its typed tools, the commands made from // menu items, the seven Parts 4a to 4c add (Printable, Fix Model, Auto Drop, Clone, Reload from disk, Replace 3D file, // Add Mixed Filament), the three of Part 5a (the Printer Selection dialog's list, install and uninstall), the two of // Part 5b (the System Filaments dialog's install and uninstall), Part 5c's write of the Preferences dialog's preferences @@ -171,7 +171,8 @@ const std::vector>> kPlaces{ {"plate_list", "plate_get_settings", "plate_set_settings", "plate_add", "plate_delete", "plate_duplicate", "plate_rename", "plate_lock", "plate_select", "plate_slice", "plate_slice_stats", "plate_set_custom_gcode", "calibration_generate_test"}}, {"Presets and settings", - {"preset_list", "preset_select", "preset_save", "preset_delete", "settings_list", "settings_get", "settings_set", "preset_import", + {"preset_list", "preset_select", "preset_save", "preset_delete", "settings_list", "settings_list_pages", "settings_get", + "settings_set", "preset_import", "preset_export", "preset_sync", "settings_set_mode", "preset_migrate_filaments", "settings_toggle_developer_mode"}}, {"Filaments", {"filament_list", "filament_add", "filament_delete", "filament_sync_ams", "filament_add_mixed", "filament_edit_mixed", @@ -456,8 +457,8 @@ TEST_CASE("outside the Design tab the catalog has its commands and 129 actions", // The commands outside the Design tab: the typed tools, the commands made from menu items, the seven Parts 4a to 4c // add, Part 5a's three, Part 5b's two, Part 5c's one, Part 5d's four, the seven gizmo commands, the six printing // commands, the four of the filament list (filament_list, filament_edit_mixed, filament_set_colour and - // filament_set_flushing), tool_describe and script_describe_api. - CHECK(commands == 137); + // filament_set_flushing), tool_describe, script_describe_api and settings_list_pages. + CHECK(commands == 138); // The eight commands named as their menu items keep an action of their own; the other 121 actions are items. CHECK(own == std::set{"plate_add", "plate_delete", "plate_duplicate", "plate_rename", "filament_add", "filament_delete", "undo", "redo"}); @@ -484,7 +485,7 @@ TEST_CASE("the commands outside the Design tab are the design's, under its headi commands += keys.size(); } CHECK(by_heading.size() == kPlaces.size()); - CHECK(commands == 137); + CHECK(commands == 138); // tool_list's group says which there are. const NativeCommand* list = NativeCommands::find("tool_list"); @@ -525,8 +526,9 @@ TEST_CASE("every command that runs from arguments is in the tool list, by place" // The three menu items with no argument form, which tool_list lists and tool_call runs. CHECK(unlisted == std::set{"window_save_screenshot", "script_toggle_panel", "settings_toggle_developer_mode"}); // Task 3's 98, the seven Parts 4a to 4c add, Part 5a's three, Part 5b's two, Part 5c's one, Part 5d's four, the - // seven gizmo commands, the six printing commands, the filament list's four, tool_describe and script_describe_api. - CHECK(listed.size() == 134); + // seven gizmo commands, the six printing commands, the filament list's four, tool_describe, script_describe_api and + // settings_list_pages. + CHECK(listed.size() == 135); // The listing of the other commands, the manual, the generic call and the app's state first; then each heading's // commands together, in the design's order, Driving the GUI last. @@ -548,11 +550,31 @@ TEST_CASE("every command that runs from arguments is in the tool list, by place" CHECK(group_runs(with_design, std::vector(keys.begin() + 4, keys.end())) == with_design_headings); } +TEST_CASE("no command searches by words, and none sends an agent to a search", "[CommandCatalog]") +{ + // An agent reads lists and picks from them: the query a command takes is a name filter, which keeps the entries + // holding the text as written. + std::set with_query; + for (const NativeCommand& c : NativeCommands::instance().commands()) + if (c.input_schema.contains("properties") && c.input_schema["properties"].contains("query")) + with_query.insert(c.key); + CHECK(with_query == std::set{"preset_list", "printer_list_models", "widget_list"}); + // No description, the parameters' included, names a search that is gone. + for (const NativeCommand& c : NativeCommands::instance().commands()) { + INFO(c.key); + const std::string text = c.description + ' ' + c.input_schema.dump(); + for (const char* gone : {"tool_search", "action_search", "script_search_api"}) + CHECK(text.find(gone) == std::string::npos); + if (c.key == "settings_get" || c.key == "settings_list" || c.key == "preferences_get") + CHECK(text.find("query") == std::string::npos); + } +} + TEST_CASE("only deleting a preset, uninstalling a plugin and stopping a print are labelled destructive", "[CommandCatalog]") { // The reads: they change nothing in the app or the project. const std::set reads{"app_state", "app_wait", "app_log", "tool_list", "tool_describe", - "preset_list", "settings_list", "settings_get", "preferences_get", + "preset_list", "settings_list", "settings_list_pages", "settings_get", "preferences_get", "plate_get_settings", "project_get_settings", "plate_list", "object_list", "window_screenshot", "plate_slice_stats", "script_list_api", "script_describe_api", "action_list", "window_list", "widget_list", "printer_list_models", "plugin_list", diff --git a/tests/slic3rutils/test_mcp_config.cpp b/tests/slic3rutils/test_mcp_config.cpp index 0f2dfdd394..e698253844 100644 --- a/tests/slic3rutils/test_mcp_config.cpp +++ b/tests/slic3rutils/test_mcp_config.cpp @@ -457,37 +457,25 @@ TEST_CASE("a setting's meta describes it with its whole tooltip", "[Mcp]") CHECK(option_meta(def)["description"] == def.tooltip); } -TEST_CASE("a settings search finds settings by their description, those named by the query first", "[Mcp]") +TEST_CASE("settings_list_pages groups a type's settings by page and group, in alphabetical order", "[Mcp]") { - // seam_gap and seam_slope_type have "seam" in their key, wipe_on_loops only in its description, and - // only retraction_length's description speaks of ooze during travel. - const std::vector within{"wipe_on_loops", "retraction_length", "seam_gap", "seam_slope_type"}; - CHECK(rank_settings("seam", within, 10) == std::vector{"seam_gap", "seam_slope_type", "wipe_on_loops"}); - CHECK(rank_settings("seam", within, 1) == std::vector{"seam_gap"}); - CHECK(rank_settings("Ooze during travel", within, 10) == std::vector{"retraction_length"}); - CHECK(rank_settings("bridge", within, 10).empty()); - // A word matches from the start of a word: "lope" is inside "slope", not the start of one. - CHECK(rank_settings("lope", within, 10).empty()); - // A key listed twice is one match. - CHECK(rank_settings("seam gap", {"seam_gap", "seam_gap"}, 10) == std::vector{"seam_gap"}); -} - -TEST_CASE("a settings search ranks the setting matching more of the query's words first", "[Mcp]") -{ - // retraction_length's description has all four words; long_retractions_when_cut has "long" in its key - // and "during" in its description, two words. - CHECK(rank_settings("ooze during long travel", {"long_retractions_when_cut", "retraction_length"}, 10) == - std::vector{"retraction_length", "long_retractions_when_cut"}); -} - -TEST_CASE("a settings search counts a short query word only when the query has no longer one", "[Mcp]") -{ - // z_hop's key splits into "z" and "hop"; seam_gap has no word starting with "z" or "hop". - const std::vector within{"seam_gap", "z_hop"}; - CHECK(rank_settings("z", within, 10) == std::vector{"z_hop"}); - CHECK(rank_settings("z hop", within, 10) == std::vector{"z_hop"}); - // "to" would start "toolchange" and many description words; with "gap" in the query it counts for nothing. - CHECK(rank_settings("gap to", within, 10) == std::vector{"seam_gap"}); + const std::map places{{"wall_loops", "Strength > Walls"}, + {"top_shell_layers", "Strength > Top/bottom shells"}, + {"brim_type", "Others > Brim"}, + {"brim_width", "Others > Brim"}, + {"skirt_loops", "Others > Skirt"}, + {"seam_position", "Quality > Seam"}}; + // The keys in the order the type lists them, one with no row on a page; seam_position the type does not list. + const json pages = settings_pages(places, {"wall_loops", "brim_width", "brim_type", "top_shell_layers", "skirt_loops", "no_row"}); + const json expected{{"pages", json::array({ + {{"page", "Others"}, + {"groups", json::array({{{"group", "Brim"}, {"settings", 2}}, {{"group", "Skirt"}, {"settings", 1}}})}}, + {{"page", "Strength"}, + {"groups", json::array({{{"group", "Top/bottom shells"}, {"settings", 1}}, + {{"group", "Walls"}, {"settings", 1}}})}}, + })}}; + CHECK(pages == expected); + CHECK(settings_pages(places, {}) == json{{"pages", json::array()}}); } TEST_CASE("a per-variant value names its entries and the one the nozzle prints with", "[Mcp]") @@ -931,15 +919,12 @@ TEST_CASE("preferences_get lists the dialog's preferences, typed, and reads a na CHECK(all["items"][2]["refused"] == "opengl_fps_cap: a reason a call may not write it."); CHECK_FALSE(all.contains("unknown_keys")); - // tab and query narrow the list: query by a key's, a label's or a section's words, in any case. + // tab and keys narrow the list together. const json graphics = preferences_settings(config, table, json{{"tab", "graphics"}}); REQUIRE(graphics["items"].size() == 1); CHECK(graphics["items"][0]["key"] == "opengl_fps_cap"); - const json flush = preferences_settings(config, table, json{{"query", "FLUSH after"}}); - REQUIRE(flush["items"].size() == 1); - CHECK(flush["items"][0]["key"] == "auto_calculate_flush"); - CHECK(preferences_settings(config, table, json{{"query", "prepare"}})["items"].size() == 2); - CHECK(preferences_settings(config, table, json{{"query", "prepare"}, {"tab", "graphics"}})["items"].empty()); + CHECK(preferences_settings(config, table, json{{"tab", "behaviour"}})["items"].size() == 2); + CHECK(preferences_settings(config, table, json{{"tab", "graphics"}, {"keys", {"auto_arrange"}}})["items"].empty()); // Named keys: those of the table as listed, another key of the app section as stored, and the others unknown. const json named = @@ -1078,7 +1063,8 @@ TEST_CASE("the settings tools take a type and a target", "[Mcp]") const std::string read_nothing = settings_request_error(nothing); const std::string write_nothing = settings_request_error(nothing, true); CHECK_THAT(read_nothing, ContainsSubstring("Nothing is named")); - CHECK_THAT(read_nothing, ContainsSubstring("keys, query or compare")); + CHECK_THAT(read_nothing, ContainsSubstring("keys or compare")); + CHECK_THAT(read_nothing, ContainsSubstring("settings_list_pages")); CHECK_THAT(write_nothing, ContainsSubstring("Nothing is named")); CHECK_THAT(write_nothing, ContainsSubstring("values")); CHECK_THAT(write_nothing, !ContainsSubstring("query")); @@ -1256,11 +1242,12 @@ TEST_CASE("the preferences, a plate's settings and the project's settings are co const NativeCommand* preferences = NativeCommands::find("preferences_get"); REQUIRE(preferences != nullptr); CHECK(preferences->listed); - // It takes keys, query and tab, and needs none: with none it lists the Preferences dialog's preferences. - for (const char* arg : {"keys", "query", "tab"}) { + // It takes keys and tab, and needs none: with none it lists the Preferences dialog's preferences. + for (const char* arg : {"keys", "tab"}) { INFO(arg); CHECK(preferences->input_schema["properties"].contains(arg)); } + CHECK_FALSE(preferences->input_schema["properties"].contains("query")); CHECK(preferences->input_schema.value("required", json::array()).empty()); const NativeCommand* write = NativeCommands::find("preferences_set"); REQUIRE(write != nullptr); diff --git a/tests/slic3rutils/test_mcp_tools.cpp b/tests/slic3rutils/test_mcp_tools.cpp index efe964c826..65aa6a913c 100644 --- a/tests/slic3rutils/test_mcp_tools.cpp +++ b/tests/slic3rutils/test_mcp_tools.cpp @@ -144,23 +144,6 @@ TEST_CASE("setting values serialize the way their options read them back", "[Mcp CHECK_THROWS_AS(to_config_string(json::object()), ToolFailure); } -TEST_CASE("a search matches query words at the start of a text's words, CamelCase parts included", "[Mcp]") -{ - // Short words drop out once the query has a longer one. - CHECK(query_words("Add a Brim to it") == std::vector{"add", "brim"}); - CHECK(query_words("z") == std::vector{"z"}); - - const std::vector words = query_words("mesh normals"); - // "mesh" starts the second part of TriangleMesh and "normals" a word of the name: both in the name. - CHECK(match_score(words, "orca.host.TriangleMesh.face_normals", "Per-triangle normals.") == std::pair{2, 2}); - // A word only the text has counts, but not as a name match. - CHECK(match_score(words, "orca.host.ModelVolume.mesh", "Face normals are not cached.") == std::pair{2, 1}); - CHECK(match_score(words, "orca.host.Model", "The project's objects.") == std::pair{0, 0}); - // A word is matched at the start of a word, never inside one. - CHECK(match_score(query_words("range"), "arrange", "") == std::pair{0, 0}); - CHECK(match_score(query_words("retract"), "retraction_length", "") == std::pair{1, 1}); -} - TEST_CASE("action_run refuses arguments for an action that takes none", "[Mcp][SpeedDial]") { // A plate's, a setting's, a recent project's and a plugin's action run what they are, whatever args says. @@ -678,15 +661,15 @@ TEST_CASE("a tool of the old vocabulary is no command", "[Mcp]") CHECK(first_sentence(command->description) == r.first_sentence); } - // Outside the Design group 134 commands are listed: every one that runs from its arguments, all but the three menu + // Outside the Design group 135 commands are listed: every one that runs from its arguments, all but the three menu // items that take none (Task 3's 98, the seven Parts 4a to 4c add, Part 5a's three, Part 5b's two, Part 5c's one, - // Part 5d's four, the seven gizmo commands, the six printing commands, the filament list's four, tool_describe and - // script_describe_api). + // Part 5d's four, the seven gizmo commands, the six printing commands, the filament list's four, tool_describe, + // script_describe_api and settings_list_pages). std::size_t listed = 0; for (const NativeCommand& c : catalog.commands()) if (c.listed && c.group != Slic3r::GUI::kDesignGroup) ++listed; - CHECK(listed == 134); + CHECK(listed == 135); } TEST_CASE("opening a project and importing models are two commands", "[Mcp]") diff --git a/tools/mcp/test-entries/presets.md b/tools/mcp/test-entries/presets.md index 58dc494b89..90d1f74836 100644 --- a/tools/mcp/test-entries/presets.md +++ b/tools/mcp/test-entries/presets.md @@ -4,7 +4,7 @@ The design of this area's sweep tests. The rules every entry follows, the grades in [TESTS.md](../TESTS.md). ## `test_presets` -`test_presets.py` · Python, 772 checks · in `sweep.json` as `{"path": "test_presets.py", "timeout_s": 1500}` +`test_presets.py` · Python, 805 checks · in `sweep.json` as `{"path": "test_presets.py", "timeout_s": 1500}` **Tests:** The presets of the three types, through the tools a person's preset work maps to: selecting a preset by the name its selector shows, hidden ones included (`preset_select`); listing presets with filters (`preset_list`); @@ -150,8 +150,8 @@ The selection stays `Klipper | Std | PLA` throughout; each step checks it. | # | Step | Checked | |---|---|---| | 1 | `settings_get {type: process, keys: [seam_slope_type, no_such_key]}` | one item, `seam_slope_type`, whose `meta.description` holds "scarf joint", `meta.where` is "Quality > Seam" and `meta.labels[1]` "Contour"; `unknown_keys` is `[no_such_key]` | -| 2 | `settings_get {type: printer, query: "ooze during long travel"}` | the first item is `retraction_length`, with `meta.where` "Extruder > Retraction" | -| 3 | `settings_get {query: "ooze during long travel"}` | the first item is `filament_retraction_length` of type `filament`, value `nil×6`, and the second `retraction_length` of type `printer`, value `0.8×3`: the two share their label and description, and the search keeps the order of the types it looks through, the filament's before the printer's | +| 2 | `settings_list_pages {type: printer}`, then `settings_list {type: printer, where: "Extruder > Retraction"}` | `pages` holds a page "Extruder" with a group "Retraction" whose `settings` is the number of items the listing gives, at least 1; the listing holds `retraction_length`, with `meta.where` "Extruder > Retraction" | +| 3 | `settings_list_pages {type: process}`, then `settings_list {type: process, where: "Others > Brim"}`, then `settings_get {query: "seam"}` | `pages` holds a page "Others" with a group "Brim"; the listing holds `brim_type`; the `settings_get` is refused with code `invalid_argument`, as it takes no query | | 4 | `settings_get {keys: [nozzle_temperature, layer_height, nozzle_diameter, curr_bed_type, no_such_key], include_meta: false}` | items, in this order: `nozzle_temperature` type `filament` `220×6`, `layer_height` type `process` "0.2", `nozzle_diameter` type `printer` "0.4"; `unknown_keys` `[curr_bed_type, no_such_key]`; `type_of` `{curr_bed_type: project}` | | 5 | `settings_get {type: process, keys: [nozzle_temperature]}` | no items; `type_of` `{nozzle_temperature: filament}` | | 6 | `settings_set {values: {nozzle_temperature: 215}}` | refused, `"nozzle_temperature" is a filament setting: settings_set takes it with type "filament"`; `value(filament, nozzle_temperature)` is `220×6` | diff --git a/tools/mcp/test_presets.py b/tools/mcp/test_presets.py index 395625acd6..6c274fddc4 100644 --- a/tools/mcp/test_presets.py +++ b/tools/mcp/test_presets.py @@ -78,6 +78,16 @@ def times(value, n): return ",".join([str(value)] * n) +def group_size(pages, page, group): + """How many settings settings_list_pages' `pages` gives `group` of `page`, None where it has no such group.""" + for listed in pages: + if listed["page"] == page: + for g in listed["groups"]: + if g["group"] == group: + return g["settings"] + return None + + def same_number(a, b): try: return float(a) == float(b) @@ -530,18 +540,21 @@ class PresetsTest: (meta.get("where"), (meta.get("labels") or [None, None])[1]), ("Quality > Seam", "Contour")) self.t.equal(f"{s} 1: the unknown keys", reply.get("unknown_keys"), ["no_such_key"]) self.selection_is(f"{s} 1", "Klipper | Std | PLA") - items = self.c.call("settings_get", type="printer", query="ooze during long travel")["items"] - self.t.equal(f"{s} 2: the first match", (items[0]["key"], items[0]["meta"].get("where")) if items else None, - ("retraction_length", "Extruder > Retraction")) + pages = self.c.call("settings_list_pages", type="printer")["pages"] + retraction = self.settings_pages(type="printer", where="Extruder > Retraction") + size = group_size(pages, "Extruder", "Retraction") + self.t.check(f"{s} 2: the Extruder page's Retraction group counts the settings listed there", + len(retraction) >= 1 and size == len(retraction), (size, len(retraction))) + self.t.equal(f"{s} 2: retraction_length is listed there", + {i["key"]: i.get("meta", {}).get("where") for i in retraction}.get("retraction_length"), + "Extruder > Retraction") self.selection_is(f"{s} 2", "Klipper | Std | PLA") - items = self.c.call("settings_get", query="ooze during long travel")["items"] - self.t.equal(f"{s} 3: the first two matches, the filament's first", - [(i["key"], i["type"], i["value"]) for i in items[:2]], - [("filament_retraction_length", "filament", times("nil", 6)), - ("retraction_length", "printer", times(0.8, 3))]) - metas = [(i["meta"].get("label"), i["meta"].get("description")) for i in items[:2]] - self.t.check(f"{s} 3: the two share their label and description", len(metas) == 2 and metas[0] == metas[1], - metas) + pages = self.c.call("settings_list_pages", type="process")["pages"] + self.t.check(f"{s} 3: the Others page has a Brim group", group_size(pages, "Others", "Brim") is not None, pages) + brim = [i["key"] for i in self.settings_pages(type="process", where="Others > Brim")] + self.t.check(f"{s} 3: brim_type is listed there", "brim_type" in brim, brim) + self.t.refused(f"{s} 3: settings_get takes no query", lambda: self.c.call("settings_get", query="seam"), + code="invalid_argument") self.selection_is(f"{s} 3", "Klipper | Std | PLA") reply = self.c.call("settings_get", keys=["nozzle_temperature", "layer_height", "nozzle_diameter", "curr_bed_type", "no_such_key"], include_meta=False)