Add JSON loading and serialization methods to PromptBuilder

This commit is contained in:
Menno van Leeuwen 2025-03-22 00:49:49 +01:00
parent 9b50cf1575
commit dc2e2b4ed2
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -391,4 +391,37 @@ class PromptBuilder {
return [];
}
}
/// Loads nodes from a JSON map
void loadFromJson(Map<String, dynamic> 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<String, dynamic> toJson() {
return Map<String, dynamic>.from(_nodes);
}
}