From dc2e2b4ed228f1c9223311f3b1b91853123ac962 Mon Sep 17 00:00:00 2001 From: Menno van Leeuwen Date: Sat, 22 Mar 2025 00:49:49 +0100 Subject: [PATCH] Add JSON loading and serialization methods to PromptBuilder --- lib/src/prompt_builder.dart | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/src/prompt_builder.dart b/lib/src/prompt_builder.dart index 64aa228..2c6bd43 100644 --- a/lib/src/prompt_builder.dart +++ b/lib/src/prompt_builder.dart @@ -391,4 +391,37 @@ class PromptBuilder { return []; } } + + /// Loads nodes from a JSON map + void loadFromJson(Map json) { + _nodes.clear(); + _outputToNode.clear(); + + json.forEach((nodeId, nodeData) { + _nodes[nodeId] = nodeData; + + // Register outputs of this node + final classType = nodeData["class_type"]; + final defaultOutputs = _getDefaultOutputs(classType); + for (var i = 0; i < defaultOutputs.length; i++) { + final outputTag = nodeData["_meta"]?["outputTags"] + ?[defaultOutputs[i]] ?? + defaultOutputs[i]; + _outputToNode[outputTag] = { + "nodeId": nodeId, + "outputIndex": i.toString() + }; + } + }); + + // Update the node ID counter to avoid collisions + _nodeIdCounter = + _nodes.keys.map(int.parse).fold(0, (max, id) => id > max ? id : max) + + 1; + } + + /// Converts the current nodes to a JSON map + Map toJson() { + return Map.from(_nodes); + } }