Isolate entity image edits and retain shared fallbacks (by Codex)
This commit is contained in:
1 parent
f128a46104
commit
fcf6092063
6 files changed
+76
-15
No files matched your search
+8
-2
@@ -199,8 +199,14 @@ Json McpServer::tools() {
|
||||
{"file"});
|
||||
add("editor_image_edit",
|
||||
"Re-edit an existing image using its preserved original. No repeated "
|
||||
"resampling.",
|
||||
{{"key", id}, {"transform", transform}, {"expected_revision", revision}}, {"key", "transform"});
|
||||
"resampling. Optional source_key clones that source into key; target assigns it to one entity without changing "
|
||||
"other users of a shared fallback.",
|
||||
{{"key", id},
|
||||
{"source_key", id},
|
||||
{"target", field("object")},
|
||||
{"transform", transform},
|
||||
{"expected_revision", revision}},
|
||||
{"key", "transform"});
|
||||
add("editor_image_preview", "Return a PNG preview as an MCP image content block.",
|
||||
{{"key", id}, {"transform", transform}}, {"key"}, true);
|
||||
add("editor_image_export", "Export an image as PNG to an explicit filesystem path.",
|
||||
|
||||
+24
-4
@@ -259,17 +259,23 @@ Json Session::apply(const Json &args, std::vector<FileDelta> files, const std::s
|
||||
Json Session::import_image(const Json &args) {
|
||||
auto &w = world();
|
||||
std::string original, extension, original_name;
|
||||
const auto *existing = w.find("assets", str(args, "key"));
|
||||
const auto source_key = str(args, "source_key", str(args, "key"));
|
||||
const auto *existing = w.find("assets", source_key);
|
||||
if (args.contains("file")) {
|
||||
original = read_file(path(str(args, "file")));
|
||||
extension = folded(utf8(path(str(args, "file")).extension()));
|
||||
original_name = utf8(path(str(args, "file")).filename());
|
||||
} else if (source_key.starts_with("portrait:") && w.find("players", source_key.substr(9))) {
|
||||
auto value = asset_bytes(source_key);
|
||||
original = std::move(value.first);
|
||||
extension = value.second;
|
||||
original_name = w.title(*w.find("players", source_key.substr(9)));
|
||||
} else {
|
||||
if (!existing)
|
||||
throw Error("record_missing");
|
||||
auto source = str(existing->value("x-editor", Json::object()), "original");
|
||||
if (source.empty())
|
||||
source = w.asset_file(str(args, "key"));
|
||||
source = w.asset_file(source_key);
|
||||
original = w.store->read(source);
|
||||
extension = folded(utf8(path(source).extension()));
|
||||
original_name = text(existing->value("name", Json(str(args, "key"))));
|
||||
@@ -280,14 +286,26 @@ Json Session::import_image(const Json &args) {
|
||||
auto result = images::transform(original, extension, args.value("transform", Json::object()));
|
||||
auto key = str(args, "key");
|
||||
const auto target = args.value("target", Json::object());
|
||||
const auto target_table = str(target, "table");
|
||||
const auto target_field = str(target, "field",
|
||||
target_table == "players" || target_table == "staff" ? "photo"
|
||||
: target_table == "competitions" ? "trophy"
|
||||
: "crest");
|
||||
if (key.empty()) {
|
||||
if (!target.empty()) {
|
||||
auto id = str(target, "id");
|
||||
auto table = str(target, "table");
|
||||
key = "asset:" + table + '/' + id + '/' + str(target, "field", table == "players" ? "photo" : "crest");
|
||||
key = "asset:" + table + '/' + id + '/' + target_field;
|
||||
} else
|
||||
key = new_id("asset:uploaded/");
|
||||
}
|
||||
if (!target.empty() && w.find("assets", key)) {
|
||||
for (const auto &[table, id] : w.references(key))
|
||||
if (table != str(target, "table") || id != str(target, "id")) {
|
||||
key = "asset:editor/" + target_table + '/' + str(target, "id") + '/' + target_field;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const auto original_hash = content::sha256(original), edited_hash = content::sha256(result.bytes);
|
||||
auto source_file = "x-editor/originals/" + original_hash + extension;
|
||||
auto output_file = "assets/edited/" + edited_hash + result.extension;
|
||||
@@ -308,6 +326,8 @@ Json Session::import_image(const Json &args) {
|
||||
{"license", "user-supplied"},
|
||||
{"kind", result.extension == ".svg" ? "vector" : "raster"},
|
||||
{"x-editor", {{"original", source_file}, {"original_sha256", original_hash}, {"transform", result.recipe}}}};
|
||||
if (!args.contains("file") && !source_key.empty() && source_key != key)
|
||||
row["x-editor"]["source_asset"] = source_key;
|
||||
Json changes = Json::array();
|
||||
if (const auto *before = w.find("assets", key)) {
|
||||
auto after = *before;
|
||||
@@ -323,7 +343,7 @@ Json Session::import_image(const Json &args) {
|
||||
changes.push_back({{"op", "update"},
|
||||
{"table", str(target, "table")},
|
||||
{"id", str(target, "id")},
|
||||
{"set", {{'/' + str(target, "field", "crest"), key}}}});
|
||||
{"set", {{'/' + target_field, key}}}});
|
||||
Json command{{"changes", changes}};
|
||||
if (args.contains("expected_revision"))
|
||||
command["expected_revision"] = args["expected_revision"];
|
||||
|
||||
+19
-4
@@ -152,7 +152,7 @@ class EditorUi final : public Rml::EventListener {
|
||||
std::future<Json> work_;
|
||||
std::function<void(const Json &)> completed_;
|
||||
std::function<void()> modal_accept_, discard_continuation_;
|
||||
std::string image_bytes_, image_extension_, image_file_, image_key_;
|
||||
std::string image_bytes_, image_extension_, image_file_, image_key_, image_source_key_;
|
||||
Json image_target_, image_recipe_;
|
||||
double drag_x_ = 0, drag_y_ = 0;
|
||||
bool dragging_ = false;
|
||||
@@ -842,17 +842,22 @@ class EditorUi final : public Rml::EventListener {
|
||||
void edit_image(bool existing, bool fresh = false) {
|
||||
image_file_.clear();
|
||||
image_key_ = fresh ? "" : image_key(selected_);
|
||||
image_source_key_ = image_key_;
|
||||
image_target_ = Json::object();
|
||||
image_recipe_ = {{"angle", 0},
|
||||
{"flip_x", false},
|
||||
{"flip_y", false},
|
||||
{"crop", {{"x", 0}, {"y", 0}, {"width", 1}, {"height", 1}}}};
|
||||
if (table() != "assets")
|
||||
if (table() != "assets") {
|
||||
image_target_ = {{"table", table()},
|
||||
{"id", selected_id_},
|
||||
{"field", table() == "players" ? "photo"
|
||||
: table() == "competitions" ? "trophy"
|
||||
: "crest"}};
|
||||
// Editing one person/club must not overwrite a shared fallback or
|
||||
// a template image also referenced by another record.
|
||||
image_key_ = "asset:editor/" + table() + '/' + selected_id_ + '/' + str(image_target_, "field");
|
||||
}
|
||||
if (!existing) {
|
||||
image_file_ = native_dialog("image");
|
||||
if (image_file_.empty())
|
||||
@@ -862,10 +867,18 @@ class EditorUi final : public Rml::EventListener {
|
||||
if (table() == "assets" && selected_id_.empty())
|
||||
image_key_.clear();
|
||||
} else {
|
||||
auto row = session_.execute("get", {{"table", "assets"}, {"id", image_key_}})["record"];
|
||||
Json row;
|
||||
try {
|
||||
row = session_.execute("get", {{"table", "assets"}, {"id", image_source_key_}})["record"];
|
||||
} catch (const Error &error) {
|
||||
if (error.code != "record_missing" || table() != "players" || !selected_.contains("appearance"))
|
||||
throw;
|
||||
image_source_key_ = "portrait:" + selected_id_;
|
||||
row = Json::object();
|
||||
}
|
||||
auto metadata = row.value("x-editor", Json::object());
|
||||
auto source = str(metadata, "original");
|
||||
auto pair = session_.asset_bytes(source.empty() ? image_key_ : source);
|
||||
auto pair = session_.asset_bytes(source.empty() ? image_source_key_ : source);
|
||||
image_bytes_ = std::move(pair.first);
|
||||
image_extension_ = pair.second;
|
||||
if (metadata.contains("transform"))
|
||||
@@ -906,6 +919,8 @@ class EditorUi final : public Rml::EventListener {
|
||||
Json args{{"key", image_key_}, {"transform", image_recipe_}, {"expected_revision", revision_}};
|
||||
if (!image_file_.empty())
|
||||
args["file"] = image_file_;
|
||||
else
|
||||
args["source_key"] = image_source_key_;
|
||||
if (!image_target_.empty())
|
||||
args["target"] = image_target_;
|
||||
session_.execute(image_file_.empty() ? "asset.edit" : "asset.import", args);
|
||||
|
||||
Reference in new issue
Block a user