载入中...
搜索中...
未找到
McpEntityInspection.cpp
浏览该文件的文档.
2#include <Poco/Base64Encoder.h>
3#include <future>
4#include <set>
5#include "common/Capability.h"
10
11namespace eve::dev {
12namespace {
13using Object = Poco::JSON::Object;
14using Array = Poco::JSON::Array;
15std::future<std::string> encoding;
16std::string encodingToken, encoded;
17Object::Ptr metadata(std::string_view phase) {
18 Object::Ptr value = new Object;
19 value->set("schemaId", "eve.entityViews");
20 value->set("schemaVersion", 1);
21 value->set("status", std::string(phase));
22 return value;
23}
24std::string error(std::string message) {
25 auto value = metadata("failed");
26 value->set("message", std::move(message));
28}
29std::string pending(std::string_view phase, std::string_view token) {
30 auto value = metadata(phase);
31 value->set("token", std::string(token));
33}
34bool workerBusy() {
35 return encoding.valid() && encoding.wait_for(std::chrono::seconds(0)) != std::future_status::ready;
36}
37bool integer(Object::Ptr args, const char* key, uint32_t& output) {
38 if (!args->has(key)) return false;
39 const auto value = args->get(key);
40 if (!value.isInteger()) return false;
41 const double number = value.convert<double>();
42 if (number < 0 || number > UINT32_MAX) return false;
43 output = value.convert<uint32_t>();
44 return true;
45}
46} // namespace
47
48std::string callMcpEntityInspectionTool(std::string_view name, Object::Ptr args) try {
49 if (name != "eve_entity_views" && name != "eve_entity_views_status" && name != "eve_entity_views_release")
50 return error("Unknown entity inspection tool");
51 if (!args) return error("Arguments are required");
52 const bool request = name == "eve_entity_views";
53 const std::set<std::string> allowed =
54 request ? std::set<std::string>{"domain", "entityId", "generation", "mode", "cellSize"}
55 : std::set<std::string>{"token"};
56 for (const auto& [key, value] : *args) {
57 (void)value;
58 if (!allowed.contains(key)) return error("Unknown argument: " + key);
59 }
60 auto* provider = cap::query<IEntityInspection>();
61 if (!provider)
62 return error("Entity inspection provider unavailable; requires a running supported graphics backend");
63 if (request) {
65 if (!args->has("domain") || !args->get("domain").isString() ||
66 args->getValue<std::string>("domain") != "renderable3d" || !args->has("mode") ||
67 !args->get("mode").isString() || args->getValue<std::string>("mode") != "geometry" ||
68 !integer(args, "entityId", input.entityId) || !integer(args, "generation", input.generation) ||
69 (args->has("cellSize") && !integer(args, "cellSize", input.cellSize)) || input.cellSize < 64 ||
70 input.cellSize > 1024)
71 return error("Expected domain=renderable3d, mode=geometry, uint32 entityId/generation, cellSize=64..1024");
72 if (workerBusy()) return error("Image encoder busy; poll or release the previous request");
73 auto result = provider->request(input);
74 if (!result.ok()) return error(result.status().describe());
75 encoding = {};
76 encodingToken.clear();
77 encoded.clear();
78 return pending("queued", result.value());
79 }
80 if (!args->has("token") || !args->get("token").isString()) return error("token must be a string");
81 const auto token = args->getValue<std::string>("token");
82 if (token.empty() || token.size() > 128) return error("Invalid token length");
83 if (name == "eve_entity_views_release") {
84 auto result = provider->release(token);
85 if (!result.ok()) return error(result.status().describe());
86 return pending(frameCapturePhaseName(result.value()), token);
87 }
88 // Validate provider token before consulting cached encoding; cancelled/restarted jobs cannot publish stale images.
89 auto result = provider->snapshot(token);
90 if (!result.ok()) return error(result.status().describe());
91 const auto& snapshot = result.value();
92 if (snapshot.capture.phase == FrameCapturePhase::Failed) return error(snapshot.capture.reason);
93 if (snapshot.capture.phase != FrameCapturePhase::Ready)
94 return pending(frameCapturePhaseName(snapshot.capture.phase), token);
95 if (encodingToken == token) {
96 if (!encoded.empty()) return encoded;
97 if (workerBusy()) return pending("encoding", token);
98 encoded = encoding.get();
99 return encoded;
100 }
101 if (workerBusy()) return error("Image encoder busy");
102 if (snapshot.capture.buffers.size() != 1 || !snapshot.capture.buffers[0].bytes)
103 return error("Incomplete inspection snapshot");
104 auto info = metadata("ready");
105 info->set("token", token);
106 info->set("mode", "geometry");
107 info->set("scope", "isolated-entity");
108 info->set("surfaceEncoding", "world-normal-rgb");
109 info->set("gameMaterialsIncluded", false);
110 info->set("sameFrozenGeometry", true);
111 info->set("worldUnitsPerPixel", snapshot.worldUnitsPerPixel);
112 info->set("entityId", snapshot.request.entityId);
113 info->set("generation", snapshot.request.generation);
114 info->set("renderFrameId", static_cast<Poco::UInt64>(snapshot.capture.frameId));
115 info->set("width", snapshot.capture.buffers[0].width);
116 info->set("height", snapshot.capture.buffers[0].height);
117 info->set("matrixConvention", "column-major RH zero-to-one depth Y-down");
118 Array::Ptr bounds = new Array;
119 for (float number : snapshot.bounds) bounds->add(number);
120 info->set("worldBounds", bounds);
121 Array::Ptr views = new Array;
122 const std::array<const char*, 4> names{"front +Z", "right +X", "top +Y up -Z", "game camera"};
123 for (size_t i = 0; i < 4; ++i) {
124 Object::Ptr view = new Object;
125 const auto& camera = snapshot.cameras[i];
126 view->set("name", names[i]);
127 view->set("x", camera.x);
128 view->set("y", camera.y);
129 view->set("width", camera.width);
130 view->set("height", camera.height);
131 Array::Ptr v = new Array, p = new Array;
132 for (float n : camera.view) v->add(n);
133 for (float n : camera.projection) p->add(n);
134 view->set("view", v);
135 view->set("projection", p);
136 views->add(view);
137 }
138 info->set("views", views);
139 const auto text = mcpStringify(info);
140 encodingToken = token;
141 encoded.clear();
142 encoding = std::async(std::launch::async, [buffer = snapshot.capture.buffers[0], text] {
143 try {
144 FramePreviewOptions options;
145 options.maxWidth = options.maxHeight = 2048;
146 auto image = frameCapturePreview(buffer, options);
147 if (!image.ok()) return error(image.status().describe());
148 std::ostringstream stream;
149 Poco::Base64Encoder encoder(stream);
150 encoder.rdbuf()->setLineLength(0);
151 encoder.write(reinterpret_cast<const char*>(image.value().png.data()),
152 std::streamsize(image.value().png.size()));
153 encoder.close();
154 return imageContentResult(stream.str(), "image/png", text);
155 } catch (const std::exception& exception) {
156 return error(exception.what());
157 }
158 });
159 return pending("encoding", token);
160} catch (const std::exception& exception) {
161 return error(exception.what());
162}
163
164std::string_view mcpEntityInspectionSchemas() {
165 return R"JSON(
166{"name":"eve_entity_views","description":"Queue four isolated geometry diagnostic views of a generation-qualified Renderable3D: front/right/top plus original active camera, same frozen geometry. World-normal colors, no materials/lighting/scene occluders. Static, already-baked CPU morph and current four-influence skin poses supported; custom shaders/LOD/instances unsupported. Skin palettes must be affine, with valid joints and nonnegative weights. Returns token; poll eve_entity_views_status. Does not advance gameplay or change the camera.","inputSchema":{"type":"object","additionalProperties":false,"required":["domain","entityId","generation","mode"],"properties":{"domain":{"type":"string","enum":["renderable3d"]},"mode":{"type":"string","enum":["geometry"]},"entityId":{"type":"integer","minimum":0,"maximum":4294967295},"generation":{"type":"integer","minimum":0,"maximum":4294967295},"cellSize":{"type":"integer","minimum":64,"maximum":1024,"default":512}}}},
167{"name":"eve_entity_views_status","description":"Poll entity view token. Ready returns a PNG four-view atlas and source camera/bounds metadata; GPU readback and PNG encoding never block this call. Layout: front/right on top, top/game camera below.","inputSchema":{"type":"object","additionalProperties":false,"required":["token"],"properties":{"token":{"type":"string","minLength":1,"maxLength":128}}}},
168{"name":"eve_entity_views_release","description":"Release or cancel entity inspection; in-flight GPU resources retire after completion. Release before requesting another target. Ready snapshots expire after 60 seconds.","inputSchema":{"type":"object","additionalProperties":false,"required":["token"],"properties":{"token":{"type":"string","minLength":1,"maxLength":128}}}}
169)JSON";
170}
171EVE_MCP_TOOL_FAMILY(EntityInspection, mcpEntityInspectionSchemas, callMcpEntityInspectionTool, ContentEnvelope)
172} // namespace eve::dev
std::string output
std::vector< QuestEvent > pending
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
EvpackChunkInput input
Definition Evpack.cpp:170
std::string message
const GltfImportRequest & request
float camera[2]
glm::vec3 n
Definition Grass.cpp:63
float v
std::string encoding
Poco::JSON::Object::Ptr result
Snapshot snapshot
#define EVE_MCP_TOOL_FAMILY(Id, Schemas, Handler, Payload)
Declare one family beside its implementation; CMake derives composition from compiled sources.
OceanGpuFrameToken token
OceanSprayView view
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
std::string error
Definition Package.cpp:174
Object::Ptr value
double number
std::string text
float phase
std::string callMcpEntityInspectionTool(std::string_view name, Object::Ptr args)
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
std::string_view mcpEntityInspectionSchemas()
Source-owned schemas for the optional entity-inspection capability.
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
std::string_view frameCapturePhaseName(FrameCapturePhase p)
Stable protocol spelling of a progress state.
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Definition Runtime.h:120
Generation-qualified Renderable3D request; geometry diagnostics, world axes, isolated entity.
glm::vec4 bounds
glm::uvec4 info