载入中...
搜索中...
未找到
McpWorkshopTools.cpp
浏览该文件的文档.
3
10#include "devtools/PlayHost.h"
11
12#include <Poco/Dynamic/Var.h>
13#include <Poco/JSON/Array.h>
14#include <Poco/JSON/Object.h>
15#include <Poco/JSON/Parser.h>
16
17#include <mutex>
18#include <string>
19#include <string_view>
20
21namespace eve::dev {
22namespace {
23
24std::mutex g_modeMu;
25std::string g_mode{"play"};
26
27std::string textPayload(Poco::JSON::Object::Ptr object, bool isError = false) {
28 return textContentResult(mcpStringify(Poco::Dynamic::Var(object)), isError);
29}
30
31std::string errorPayload(const std::string& message, const std::string& hint = {}, const char* code = nullptr) {
32 Poco::JSON::Object::Ptr out = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
33 out->set("ok", false);
34 out->set("error", message);
35 if (code) out->set("errorCode", std::string(code));
36 if (!hint.empty()) out->set("hint", hint);
37 return textPayload(out, true);
38}
39
40Poco::JSON::Object::Ptr parseJsonObject(const std::string& text) {
41 try {
42 Poco::JSON::Parser parser;
43 auto root = parser.parse(text);
44 if (root.type() == typeid(Poco::JSON::Object::Ptr)) return root.extract<Poco::JSON::Object::Ptr>();
45 } catch (...) {
46 }
47 return {};
48}
49
50std::string workshopStatus() {
51 Poco::JSON::Object::Ptr out = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
52 out->set("ok", true);
53 out->set("schemaId", "eve.workshop.status");
54 out->set("schemaVersion", 1);
55 {
56 std::lock_guard lock(g_modeMu);
57 out->set("mode", g_mode);
58 }
59 out->set("processProfile", mcpProcessProfile());
60 out->set("sessionProfile", mcpSessionProfile());
61 out->set("sceneDirectorPending", sceneDirectorInstallPending());
62 Debugger& dbg = Debugger::instance();
63 out->set("vmAttached", dbg.vm() != nullptr);
64 Poco::JSON::Array::Ptr ops = Poco::JSON::Array::Ptr(new Poco::JSON::Array());
65 for (const char* op : {"status", "set_mode", "recipe.apply", "map.brush", "play.observe", "play.submit"})
66 ops->add(std::string(op));
67 out->set("ops", ops);
68 out->set("hint", "Use recipe.apply for content; play.observe/play.submit for verify evidence");
69 return textPayload(out);
70}
71
72std::string workshopSetMode(Poco::JSON::Object::Ptr args) {
73 const std::string mode = getArgString(args, "mode");
74 if (mode != "edit" && mode != "play" && mode != "battle") {
75 return errorPayload("mode must be edit|play|battle",
76 "Workshop mode gates input routing hints; it does not enable Godot toolsets");
77 }
78 {
79 std::lock_guard lock(g_modeMu);
80 g_mode = mode;
81 }
82 Poco::JSON::Object::Ptr out = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
83 out->set("ok", true);
84 out->set("mode", mode);
85 out->set("hint", "edit → content/recipe; play/battle → verify.play observe/submit");
86 return textPayload(out);
87}
88
89std::string workshopRecipeApply(Poco::JSON::Object::Ptr args) {
90 Poco::JSON::Object::Ptr recipeArgs = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
91 if (args) {
92 if (args->has("id")) recipeArgs->set("id", args->get("id"));
93 if (args->has("params")) recipeArgs->set("params", args->get("params"));
94 if (args->has("expectedRevision")) recipeArgs->set("expectedRevision", args->get("expectedRevision"));
95 if (args->has("stopOnError")) recipeArgs->set("stopOnError", args->get("stopOnError"));
96 }
97 return callMcpRecipeTool("eve_recipe_apply", recipeArgs);
98}
99
100std::string workshopMapBrush(Poco::JSON::Object::Ptr /*args*/) {
101 // Intentionally unsupported until a live TileBuffer authoring session is
102 // published through the editor capability (EA2). Agents must escalate to
103 // content.map / editor targets rather than inventing a parallel brush API.
104 return errorPayload("map.brush is unsupported without a live level-editor TileBuffer session",
105 "Escalate to content.map / eve_editor_* or sync TileBuffer→TileLayer in the "
106 "workshop example before calling this op",
107 "Unsupported");
108}
109
110std::string workshopPlay(const char* op, Poco::JSON::Object::Ptr args) {
111 Poco::JSON::Object::Ptr req = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
112 req->set("schemaId", "evengine.play-request");
113 req->set("schemaVersion", 1);
114 req->set("op", std::string(op));
115 if (args) {
116 if (args->has("id")) req->set("id", args->get("id"));
117 if (args->has("observation")) req->set("observation", args->get("observation"));
118 if (args->has("action")) req->set("action", args->get("action"));
119 if (args->has("params")) req->set("params", args->get("params"));
120 if (args->has("fields")) req->set("fields", args->get("fields"));
121 }
122 auto playResponse = executePlayJson(mcpStringify(Poco::Dynamic::Var(req)));
123 if (!playResponse) {
124 return errorPayload(playResponse.status().describe(),
125 "Play Host needs game.agent.json with paths.gameState / observations");
126 }
127 auto playObj = parseJsonObject(playResponse.value());
128 if (!playObj) {
129 Poco::JSON::Object::Ptr out = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
130 out->set("ok", true);
131 out->set("op", std::string(op));
132 out->set("raw", playResponse.value());
133 return textPayload(out);
134 }
135 playObj->set("workshopOp", std::string("play.") + op);
136 return textPayload(playObj, !playObj->optValue<bool>("ok", true));
137}
138
139std::string workshopDispatch(Poco::JSON::Object::Ptr args) {
140 if (!args || !args->has("op")) {
141 return errorPayload("op is required", "ops: status|set_mode|recipe.apply|map.brush|play.observe|play.submit");
142 }
143 const std::string op = getArgString(args, "op");
144 if (op == "status") return workshopStatus();
145 if (op == "set_mode") return workshopSetMode(args);
146 if (op == "recipe.apply") return workshopRecipeApply(args);
147 if (op == "map.brush") return workshopMapBrush(args);
148 if (op == "play.observe") return workshopPlay("observe", args);
149 if (op == "play.submit" || op == "play.act") return workshopPlay("act", args);
150 return errorPayload("unknown workshop op '" + op + "'",
151 "ops: status|set_mode|recipe.apply|map.brush|play.observe|play.submit");
152}
153
154} // namespace
155
156bool isMcpWorkshopTool(std::string_view name) { return name == "eve_workshop"; }
157
158std::string callMcpWorkshopTool(std::string_view name, Poco::JSON::Object::Ptr args) {
159 if (name == "eve_workshop") return workshopDispatch(args);
160 return errorPayload("unknown workshop tool '" + std::string(name) + "'");
161}
162
163std::string_view mcpWorkshopToolSchemas() {
164 return R"json({"name":"eve_workshop","description":"Narrow RPG workshop facade for workshop.rpg: status|set_mode|recipe.apply|map.brush|play.observe|play.submit. Prefer this over registering project-specific atelier_* tools.","inputSchema":{"type":"object","properties":{"op":{"type":"string","description":"status|set_mode|recipe.apply|map.brush|play.observe|play.submit"},"mode":{"type":"string","description":"For set_mode: edit|play|battle"},"id":{"type":"string","description":"Recipe id or observation id"},"params":{"type":"object"},"observation":{"type":"string"},"action":{"type":"string"},"expectedRevision":{"type":"integer"},"stopOnError":{"type":"boolean"}},"required":["op"]}})json";
165}
166
168 std::lock_guard lock(g_modeMu);
169 g_mode = "play";
170}
171
172} // namespace eve::dev
173
174namespace eve::dev {
176} // namespace eve::dev
int root
Definition AnimSmr.cpp:119
std::string name
Definition ECS.cpp:154
std::string message
DiagnosticCode code
Content recipe list/apply facade for agent task profiles.
Shared helpers for MCP tools that drive the embedded scene_director kit.
Named Agent task profiles that narrow the MCP tool surface.
#define EVE_MCP_TOOL_FAMILY(Id, Schemas, Handler, Payload)
Declare one family beside its implementation; CMake derives composition from compiled sources.
Narrow workshop facade (eve_workshop) for the workshop.rpg profile.
Versioned Play Host protocol for Agent pause / step / observe / capture.
LinkOps ops
std::string text
ScriptStructure out
static Debugger & instance()
Definition Debugger.cpp:387
std::string mcpProcessProfile()
Current process-level profile; empty when unset.
bool sceneDirectorInstallPending()
Whether a deferred install is waiting for the next poll.
Result< std::string > executePlayJson(std::string_view requestJson)
Parse, execute and serialize one Play JSON request against the live engine.
Definition PlayHost.cpp:898
bool isMcpWorkshopTool(std::string_view name)
Whether name is the workshop facade.
std::string mcpStringify(const Poco::Dynamic::Var &value)
Compact single-line JSON text for a newline-framed MCP payload.
Definition McpJson.hpp:24
std::string textContentResult(const std::string &text, bool isError=false)
MCP tools/call result object carrying one text content item.
Definition McpJson.hpp:84
void resetMcpWorkshopStateForTest()
Test helper: clear workshop mode overlay.
std::string callMcpWorkshopTool(std::string_view name, Poco::JSON::Object::Ptr args)
Execute eve_workshop with a narrow op.
std::string_view mcpWorkshopToolSchemas()
Comma-free MCP tool schema fragment for eve_workshop.
std::string mcpSessionProfile()
Current session-level profile; empty when no session overlay.
std::string callMcpRecipeTool(std::string_view name, Poco::JSON::Object::Ptr args)
Execute one recipe tool.
std::string getArgString(Poco::JSON::Object::Ptr args, const char *key, const std::string &def={})
String argument, or def when absent or not convertible.
Definition McpArgs.hpp:25