From d3ce3078550eea9d7b26941973048f1cc12a272f Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:58:51 +0800 Subject: [PATCH] Add File --- .../agentsflex/core/chain/ChainHolder.java | 239 ++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 agents-flex-core/src/main/java/com/agentsflex/core/chain/ChainHolder.java diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/chain/ChainHolder.java b/agents-flex-core/src/main/java/com/agentsflex/core/chain/ChainHolder.java new file mode 100644 index 0000000..d9ba437 --- /dev/null +++ b/agents-flex-core/src/main/java/com/agentsflex/core/chain/ChainHolder.java @@ -0,0 +1,239 @@ +/* + * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.agentsflex.core.chain; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.parser.DefaultJSONParser; +import com.alibaba.fastjson.parser.Feature; +import com.alibaba.fastjson.parser.ParserConfig; +import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer; +import com.alibaba.fastjson.serializer.JSONSerializer; +import com.alibaba.fastjson.serializer.ObjectSerializer; +import com.alibaba.fastjson.serializer.SerializeConfig; +import com.alibaba.fastjson.serializer.SerializerFeature; + +import java.io.IOException; +import java.io.Serializable; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class ChainHolder implements Serializable { + + private String id; + private String name; + private String description; + + private List nodes; + private List edges; + + private Map executeResult; + + private Map nodeContexts; + + protected Map suspendNodes = new ConcurrentHashMap<>(); + private List suspendForParameters; + private ChainStatus status; + private String message; + + public ChainHolder() { + } + + + public static ChainHolder fromChain(Chain chain) { + ChainHolder holder = new ChainHolder(); + holder.id = chain.getId(); + holder.name = chain.getName(); + holder.description = chain.getDescription(); + + holder.nodes = chain.getNodes(); + holder.edges = chain.getEdges(); + + holder.executeResult = chain.getExecuteResult(); + holder.nodeContexts = chain.getNodeContexts(); + + holder.suspendNodes = chain.getSuspendNodes(); + holder.suspendForParameters = chain.getSuspendForParameters(); + holder.status = chain.getStatus(); + + holder.message = chain.getMessage(); + + return holder; + } + + + public static ChainHolder fromJSON(String jsonString) { + ParserConfig config = new ParserConfig(); + config.putDeserializer(Chain.class, new ChainDeserializer()); + return JSON.parseObject(jsonString, ChainHolder.class, config, Feature.SupportAutoType); + } + + public String toJSON() { + SerializeConfig config = new SerializeConfig(); + config.put(Chain.class, new ChainSerializer()); + return JSON.toJSONString(this, config, SerializerFeature.WriteClassName); + } + + public Chain toChain() { + return new Chain(this); + } + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +// public ChainHolder getParent() { +// return parent; +// } +// +// public void setParent(ChainHolder parent) { +// this.parent = parent; +// } +// +// public List getChildren() { +// return children; +// } +// +// public void setChildren(List children) { +// this.children = children; +// } + + public List getNodes() { + return nodes; + } + + public void setNodes(List nodes) { + this.nodes = nodes; + } + + public List getEdges() { + return edges; + } + + public void setEdges(List edges) { + this.edges = edges; + } + + public Map getExecuteResult() { + return executeResult; + } + + public void setExecuteResult(Map executeResult) { + this.executeResult = executeResult; + } + + public Map getNodeContexts() { + return nodeContexts; + } + + public void setNodeContexts(Map nodeContexts) { + this.nodeContexts = nodeContexts; + } + + public Map getSuspendNodes() { + return suspendNodes; + } + + public void setSuspendNodes(Map suspendNodes) { + this.suspendNodes = suspendNodes; + } + + public List getSuspendForParameters() { + return suspendForParameters; + } + + public void setSuspendForParameters(List suspendForParameters) { + this.suspendForParameters = suspendForParameters; + } + + public ChainStatus getStatus() { + return status; + } + + public void setStatus(ChainStatus status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public static class ChainSerializer implements ObjectSerializer { + @Override + public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { + if (object == null) { + serializer.writeNull(); + return; + } + Chain chain = (Chain) object; + serializer.write(chain.toJSON()); + } + } + + public static class ChainDeserializer implements ObjectDeserializer { + @Override + public T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { + String value = parser.parseObject(String.class); + //noinspection unchecked + return (T) Chain.fromJSON(value); + } + } + + @Override + public String toString() { + return "ChainHolder{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", description='" + description + '\'' + +// ", parent=" + parent + +// ", children=" + children + + ", nodes=" + nodes + + ", edges=" + edges + + ", executeResult=" + executeResult + + ", nodeContexts=" + nodeContexts + + ", suspendNodes=" + suspendNodes + + ", suspendForParameters=" + suspendForParameters + + ", status=" + status + + ", message='" + message + '\'' + + '}'; + } +}