mirror of
https://github.com/slhaf/Partner.git
synced 2026-06-27 17:49:16 +08:00
refactor(state): optimize StateValue building methods
This commit is contained in:
@@ -50,70 +50,70 @@ final class ActionPoolStateCodec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static StateValue.Obj encodeExecutableAction(ExecutableAction action) {
|
private static StateValue.Obj encodeExecutableAction(ExecutableAction action) {
|
||||||
Map<String, StateValue> actionMap = new LinkedHashMap<>();
|
Map<String, Object> actionMap = new LinkedHashMap<>();
|
||||||
actionMap.put("kind", StateValue.str(action instanceof SchedulableExecutableAction ? "schedulable" : "immediate"));
|
actionMap.put("kind", action instanceof SchedulableExecutableAction ? "schedulable" : "immediate");
|
||||||
actionMap.put("uuid", StateValue.str(action.getUuid()));
|
actionMap.put("uuid", action.getUuid());
|
||||||
actionMap.put("source", StateValue.str(action.getSource()));
|
actionMap.put("source", action.getSource());
|
||||||
actionMap.put("reason", StateValue.str(action.getReason()));
|
actionMap.put("reason", action.getReason());
|
||||||
actionMap.put("description", StateValue.str(action.getDescription()));
|
actionMap.put("description", action.getDescription());
|
||||||
actionMap.put("status", StateValue.str(action.getStatus().name()));
|
actionMap.put("status", action.getStatus().name());
|
||||||
actionMap.put("tendency", StateValue.str(action.getTendency()));
|
actionMap.put("tendency", action.getTendency());
|
||||||
actionMap.put("executing_stage", StateValue.num(action.getExecutingStage()));
|
actionMap.put("executing_stage", action.getExecutingStage());
|
||||||
|
|
||||||
String result = resolveExecutableResult(action);
|
String result = resolveExecutableResult(action);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
actionMap.put("result", StateValue.str(result));
|
actionMap.put("result", result);
|
||||||
}
|
}
|
||||||
if (action instanceof SchedulableExecutableAction schedulableAction) {
|
if (action instanceof SchedulableExecutableAction schedulableAction) {
|
||||||
actionMap.put("schedule_type", StateValue.str(schedulableAction.getScheduleType().name()));
|
actionMap.put("schedule_type", schedulableAction.getScheduleType().name());
|
||||||
actionMap.put("schedule_content", StateValue.str(schedulableAction.getScheduleContent()));
|
actionMap.put("schedule_content", schedulableAction.getScheduleContent());
|
||||||
actionMap.put("enabled", StateValue.bool(schedulableAction.getEnabled()));
|
actionMap.put("enabled", schedulableAction.getEnabled());
|
||||||
actionMap.put("schedule_histories", StateValue.arr(encodeScheduleHistories(schedulableAction)));
|
actionMap.put("schedule_histories", encodeScheduleHistories(schedulableAction));
|
||||||
}
|
}
|
||||||
|
|
||||||
List<StateValue> chainStates = action.getActionChain().entrySet().stream()
|
List<StateValue.Obj> chainStates = action.getActionChain().entrySet().stream()
|
||||||
.sorted(Map.Entry.comparingByKey())
|
.sorted(Map.Entry.comparingByKey())
|
||||||
.<StateValue>map(entry -> {
|
.map(entry -> {
|
||||||
Map<String, StateValue> stageMap = new LinkedHashMap<>();
|
Map<String, Object> stageMap = new LinkedHashMap<>();
|
||||||
stageMap.put("stage", StateValue.num(entry.getKey()));
|
stageMap.put("stage", entry.getKey());
|
||||||
String stageDescription = action.getStageDescriptions().get(entry.getKey());
|
String stageDescription = action.getStageDescriptions().get(entry.getKey());
|
||||||
if (stageDescription != null && !stageDescription.isBlank()) {
|
if (stageDescription != null && !stageDescription.isBlank()) {
|
||||||
stageMap.put("description", StateValue.str(stageDescription));
|
stageMap.put("description", stageDescription);
|
||||||
}
|
}
|
||||||
stageMap.put("actions", StateValue.arr(entry.getValue().stream()
|
stageMap.put("actions", entry.getValue().stream()
|
||||||
.map(metaAction -> (StateValue) encodeMetaAction(metaAction))
|
.map(ActionPoolStateCodec::encodeMetaAction)
|
||||||
.toList()));
|
.toList());
|
||||||
return StateValue.obj(stageMap);
|
return StateValue.obj(stageMap);
|
||||||
}).toList();
|
}).toList();
|
||||||
actionMap.put("action_chain", StateValue.arr(chainStates));
|
actionMap.put("action_chain", chainStates);
|
||||||
|
|
||||||
actionMap.put("history", StateValue.arr(encodeHistoryStages(action.getHistory())));
|
actionMap.put("history", encodeHistoryStages(action.getHistory()));
|
||||||
|
|
||||||
return StateValue.obj(actionMap);
|
return StateValue.obj(actionMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static StateValue.Obj encodeMetaAction(MetaAction metaAction) {
|
private static StateValue.Obj encodeMetaAction(MetaAction metaAction) {
|
||||||
Map<String, StateValue> metaMap = new LinkedHashMap<>();
|
Map<String, Object> metaMap = new LinkedHashMap<>();
|
||||||
metaMap.put("name", StateValue.str(metaAction.getName()));
|
metaMap.put("name", metaAction.getName());
|
||||||
metaMap.put("io", StateValue.bool(metaAction.getIo()));
|
metaMap.put("io", metaAction.getIo());
|
||||||
if (metaAction.getLauncher() != null) {
|
if (metaAction.getLauncher() != null) {
|
||||||
metaMap.put("launcher", StateValue.str(metaAction.getLauncher()));
|
metaMap.put("launcher", metaAction.getLauncher());
|
||||||
}
|
}
|
||||||
metaMap.put("type", StateValue.str(metaAction.getType().name()));
|
metaMap.put("type", metaAction.getType().name());
|
||||||
metaMap.put("location", StateValue.str(metaAction.getLocation()));
|
metaMap.put("location", metaAction.getLocation());
|
||||||
metaMap.put("params_json", StateValue.str(JSONObject.toJSONString(metaAction.getParams())));
|
metaMap.put("params_json", JSONObject.toJSONString(metaAction.getParams()));
|
||||||
metaMap.put("result_status", StateValue.str(metaAction.getResult().getStatus().name()));
|
metaMap.put("result_status", metaAction.getResult().getStatus().name());
|
||||||
if (metaAction.getResult().getData() != null) {
|
if (metaAction.getResult().getData() != null) {
|
||||||
metaMap.put("result_data", StateValue.str(metaAction.getResult().getData()));
|
metaMap.put("result_data", metaAction.getResult().getData());
|
||||||
}
|
}
|
||||||
return StateValue.obj(metaMap);
|
return StateValue.obj(metaMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static StateValue.Obj encodeHistoryAction(HistoryAction historyAction) {
|
private static StateValue.Obj encodeHistoryAction(HistoryAction historyAction) {
|
||||||
Map<String, StateValue> historyMap = new LinkedHashMap<>();
|
Map<String, Object> historyMap = new LinkedHashMap<>();
|
||||||
historyMap.put("action_key", StateValue.str(historyAction.actionKey()));
|
historyMap.put("action_key", historyAction.actionKey());
|
||||||
historyMap.put("description", StateValue.str(historyAction.description()));
|
historyMap.put("description", historyAction.description());
|
||||||
historyMap.put("result", StateValue.str(historyAction.result()));
|
historyMap.put("result", historyAction.result());
|
||||||
return StateValue.obj(historyMap);
|
return StateValue.obj(historyMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,26 +288,26 @@ final class ActionPoolStateCodec {
|
|||||||
return restored;
|
return restored;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<StateValue> encodeHistoryStages(Map<Integer, ? extends List<HistoryAction>> historyMap) {
|
private static List<StateValue.Obj> encodeHistoryStages(Map<Integer, ? extends List<HistoryAction>> historyMap) {
|
||||||
return historyMap.entrySet().stream()
|
return historyMap.entrySet().stream()
|
||||||
.sorted(Map.Entry.comparingByKey())
|
.sorted(Map.Entry.comparingByKey())
|
||||||
.<StateValue>map(entry -> {
|
.map(entry -> {
|
||||||
Map<String, StateValue> stageMap = new LinkedHashMap<>();
|
Map<String, Object> stageMap = new LinkedHashMap<>();
|
||||||
stageMap.put("stage", StateValue.num(entry.getKey()));
|
stageMap.put("stage", entry.getKey());
|
||||||
stageMap.put("actions", StateValue.arr(entry.getValue().stream()
|
stageMap.put("actions", entry.getValue().stream()
|
||||||
.map(historyAction -> (StateValue) encodeHistoryAction(historyAction))
|
.map(ActionPoolStateCodec::encodeHistoryAction)
|
||||||
.toList()));
|
.toList());
|
||||||
return StateValue.obj(stageMap);
|
return StateValue.obj(stageMap);
|
||||||
}).toList();
|
}).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<StateValue> encodeScheduleHistories(SchedulableExecutableAction schedulableAction) {
|
private static List<StateValue.Obj> encodeScheduleHistories(SchedulableExecutableAction schedulableAction) {
|
||||||
return schedulableAction.getScheduleHistories().stream()
|
return schedulableAction.getScheduleHistories().stream()
|
||||||
.<StateValue>map(scheduleHistory -> {
|
.map(scheduleHistory -> {
|
||||||
Map<String, StateValue> historyMap = new LinkedHashMap<>();
|
Map<String, Object> historyMap = new LinkedHashMap<>();
|
||||||
historyMap.put("end_time", StateValue.str(scheduleHistory.getEndTime().toString()));
|
historyMap.put("end_time", scheduleHistory.getEndTime().toString());
|
||||||
historyMap.put("result", StateValue.str(scheduleHistory.getResult()));
|
historyMap.put("result", scheduleHistory.getResult());
|
||||||
historyMap.put("history", StateValue.arr(encodeHistoryStages(scheduleHistory.getHistory())));
|
historyMap.put("history", encodeHistoryStages(scheduleHistory.getHistory()));
|
||||||
return StateValue.obj(historyMap);
|
return StateValue.obj(historyMap);
|
||||||
})
|
})
|
||||||
.toList();
|
.toList();
|
||||||
|
|||||||
@@ -200,13 +200,12 @@ public class ContextCore implements StateSerializable {
|
|||||||
public @NotNull State convert() {
|
public @NotNull State convert() {
|
||||||
State state = new State();
|
State state = new State();
|
||||||
|
|
||||||
List<StateValue.Obj> convertedMessageList = chatMessages.stream().map(message -> {
|
List<StateValue.Obj> convertedMessageList = chatMessages.stream()
|
||||||
Map<String, StateValue> convertedMap = Map.of(
|
.map(message -> StateValue.obj(Map.of(
|
||||||
"role", StateValue.str(message.roleValue()),
|
"role", message.roleValue(),
|
||||||
"content", StateValue.str(message.getContent())
|
"content", message.getContent()
|
||||||
);
|
)))
|
||||||
return StateValue.obj(convertedMap);
|
.toList();
|
||||||
}).toList();
|
|
||||||
state.append("chat_messages", StateValue.arr(convertedMessageList));
|
state.append("chat_messages", StateValue.arr(convertedMessageList));
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
|
|||||||
@@ -175,8 +175,7 @@ public class MemoryCore implements StateSerializable {
|
|||||||
State state = new State();
|
State state = new State();
|
||||||
state.append("memory_session_id", StateValue.str(memorySessionId));
|
state.append("memory_session_id", StateValue.str(memorySessionId));
|
||||||
|
|
||||||
List<StateValue.Str> unitOverview = memoryUnits.keySet().stream()
|
List<String> unitOverview = memoryUnits.keySet().stream()
|
||||||
.map(StateValue::str)
|
|
||||||
.toList();
|
.toList();
|
||||||
state.append("memory_unit_uuid_set", StateValue.arr(unitOverview));
|
state.append("memory_unit_uuid_set", StateValue.arr(unitOverview));
|
||||||
return state;
|
return state;
|
||||||
|
|||||||
@@ -95,25 +95,23 @@ public class MemoryUnit implements StateSerializable {
|
|||||||
state.append("id", StateValue.str(id));
|
state.append("id", StateValue.str(id));
|
||||||
state.append("update_timestamp", StateValue.num(timestamp));
|
state.append("update_timestamp", StateValue.num(timestamp));
|
||||||
|
|
||||||
List<StateValue.Obj> convertedMessageList = conversationMessages.stream().map(message -> {
|
List<StateValue.Obj> convertedMessageList = conversationMessages.stream()
|
||||||
Map<String, StateValue> convertedMap = Map.of(
|
.map(message -> StateValue.obj(Map.of(
|
||||||
"role", StateValue.str(message.roleValue()),
|
"role", message.roleValue(),
|
||||||
"content", StateValue.str(message.getContent())
|
"content", message.getContent()
|
||||||
);
|
)))
|
||||||
return StateValue.obj(convertedMap);
|
.toList();
|
||||||
}).toList();
|
|
||||||
state.append("conversation_messages", StateValue.arr(convertedMessageList));
|
state.append("conversation_messages", StateValue.arr(convertedMessageList));
|
||||||
|
|
||||||
List<StateValue.Obj> convertedSliceList = slices.stream().map(slice -> {
|
List<StateValue.Obj> convertedSliceList = slices.stream()
|
||||||
Map<String, StateValue> convertedMap = Map.of(
|
.map(slice -> StateValue.obj(Map.of(
|
||||||
"id", StateValue.str(slice.getId()),
|
"id", slice.getId(),
|
||||||
"start_index", StateValue.num(slice.getStartIndex()),
|
"start_index", slice.getStartIndex(),
|
||||||
"end_index", StateValue.num(slice.getEndIndex()),
|
"end_index", slice.getEndIndex(),
|
||||||
"summary", StateValue.str(slice.getSummary()),
|
"summary", slice.getSummary(),
|
||||||
"created_timestamp", StateValue.num(slice.getTimestamp())
|
"created_timestamp", slice.getTimestamp()
|
||||||
);
|
)))
|
||||||
return StateValue.obj(convertedMap);
|
.toList();
|
||||||
}).toList();
|
|
||||||
state.append("memory_slices", StateValue.arr(convertedSliceList));
|
state.append("memory_slices", StateValue.arr(convertedSliceList));
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ final class MemoryRuntimeStateCodec {
|
|||||||
List<StateValue.Obj> dateIndexStates = dateIndex.entries().entrySet().stream()
|
List<StateValue.Obj> dateIndexStates = dateIndex.entries().entrySet().stream()
|
||||||
.sorted(Map.Entry.comparingByKey())
|
.sorted(Map.Entry.comparingByKey())
|
||||||
.map(entry -> StateValue.obj(Map.of(
|
.map(entry -> StateValue.obj(Map.of(
|
||||||
"date", StateValue.str(entry.getKey().toString()),
|
"date", entry.getKey().toString(),
|
||||||
"refs", StateValue.arr(encodeSliceRefs(entry.getValue()))
|
"refs", encodeSliceRefs(entry.getValue())
|
||||||
)))
|
)))
|
||||||
.toList();
|
.toList();
|
||||||
state.append("date_index", StateValue.arr(dateIndexStates));
|
state.append("date_index", StateValue.arr(dateIndexStates));
|
||||||
@@ -82,8 +82,8 @@ final class MemoryRuntimeStateCodec {
|
|||||||
TopicMemoryIndex.TopicTreeNode topicNode,
|
TopicMemoryIndex.TopicTreeNode topicNode,
|
||||||
List<StateValue.Obj> topicStates) {
|
List<StateValue.Obj> topicStates) {
|
||||||
topicStates.add(StateValue.obj(Map.of(
|
topicStates.add(StateValue.obj(Map.of(
|
||||||
"topic_path", StateValue.str(path),
|
"topic_path", path,
|
||||||
"bindings", StateValue.arr(encodeTopicBindings(topicNode.bindings()))
|
"bindings", encodeTopicBindings(topicNode.bindings())
|
||||||
)));
|
)));
|
||||||
for (Map.Entry<String, TopicMemoryIndex.TopicTreeNode> childEntry : topicNode.children().entrySet()) {
|
for (Map.Entry<String, TopicMemoryIndex.TopicTreeNode> childEntry : topicNode.children().entrySet()) {
|
||||||
collectTopicStates(path + "->" + childEntry.getKey(), childEntry.getValue(), topicStates);
|
collectTopicStates(path + "->" + childEntry.getKey(), childEntry.getValue(), topicStates);
|
||||||
@@ -93,18 +93,16 @@ final class MemoryRuntimeStateCodec {
|
|||||||
private List<StateValue> encodeTopicBindings(List<TopicMemoryIndex.TopicBinding> bindings) {
|
private List<StateValue> encodeTopicBindings(List<TopicMemoryIndex.TopicBinding> bindings) {
|
||||||
return bindings.stream()
|
return bindings.stream()
|
||||||
.map(binding -> (StateValue) StateValue.obj(Map.of(
|
.map(binding -> (StateValue) StateValue.obj(Map.of(
|
||||||
"unit_id", StateValue.str(binding.sliceRef().getUnitId()),
|
"unit_id", binding.sliceRef().getUnitId(),
|
||||||
"slice_id", StateValue.str(binding.sliceRef().getSliceId()),
|
"slice_id", binding.sliceRef().getSliceId(),
|
||||||
"timestamp", StateValue.num(binding.timestamp()),
|
"timestamp", binding.timestamp(),
|
||||||
"activation_profile", StateValue.obj(Map.of(
|
"activation_profile", StateValue.obj(Map.of(
|
||||||
"activation_weight", StateValue.num(binding.activationProfile().getActivationWeight()),
|
"activation_weight", binding.activationProfile().getActivationWeight(),
|
||||||
"diffusion_weight", StateValue.num(binding.activationProfile().getDiffusionWeight()),
|
"diffusion_weight", binding.activationProfile().getDiffusionWeight(),
|
||||||
"context_independence_weight",
|
"context_independence_weight",
|
||||||
StateValue.num(binding.activationProfile().getContextIndependenceWeight())
|
binding.activationProfile().getContextIndependenceWeight()
|
||||||
)),
|
)),
|
||||||
"related_topic_paths", StateValue.arr(binding.relatedTopicPaths().stream()
|
"related_topic_paths", binding.relatedTopicPaths()
|
||||||
.map(StateValue::str)
|
|
||||||
.toList())
|
|
||||||
)))
|
)))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
@@ -156,8 +154,8 @@ final class MemoryRuntimeStateCodec {
|
|||||||
private List<StateValue> encodeSliceRefs(List<SliceRef> refs) {
|
private List<StateValue> encodeSliceRefs(List<SliceRef> refs) {
|
||||||
return refs.stream()
|
return refs.stream()
|
||||||
.map(ref -> (StateValue) StateValue.obj(Map.of(
|
.map(ref -> (StateValue) StateValue.obj(Map.of(
|
||||||
"unit_id", StateValue.str(ref.getUnitId()),
|
"unit_id", ref.getUnitId(),
|
||||||
"slice_id", StateValue.str(ref.getSliceId())
|
"slice_id", ref.getSliceId()
|
||||||
)))
|
)))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -148,10 +148,130 @@ sealed interface StateValue {
|
|||||||
fun str(value: String) = Str(value)
|
fun str(value: String) = Str(value)
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun arr(value: List<StateValue>) = Arr(value)
|
fun arr(value: List<*>): Arr {
|
||||||
|
val visiting = java.util.IdentityHashMap<Any, Unit>()
|
||||||
|
return Arr(convertList(value, visiting))
|
||||||
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun obj(value: Map<String, StateValue>) = Obj(value)
|
fun obj(value: Map<String, *>): Obj {
|
||||||
|
val visiting = java.util.IdentityHashMap<Any, Unit>()
|
||||||
|
return Obj(convertMap(value, visiting))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun convertValue(
|
||||||
|
value: Any?,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
): StateValue {
|
||||||
|
return when (value) {
|
||||||
|
null -> error("StateValue does not support null")
|
||||||
|
is StateValue -> normalizeStateValue(value, visiting)
|
||||||
|
is String -> Str(value)
|
||||||
|
is Number -> Num(value)
|
||||||
|
is Boolean -> Bool(value)
|
||||||
|
is List<*> -> Arr(convertList(value, visiting))
|
||||||
|
is Map<*, *> -> Obj(convertGenericMap(value, visiting))
|
||||||
|
else -> error("Unsupported state value type: ${value::class.qualifiedName}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun normalizeStateValue(
|
||||||
|
value: StateValue,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
): StateValue {
|
||||||
|
return when (value) {
|
||||||
|
is Num -> value
|
||||||
|
is Bool -> value
|
||||||
|
is Str -> value
|
||||||
|
is Arr -> Arr(convertStateValueList(value.value, visiting))
|
||||||
|
is Obj -> Obj(convertStateValueMap(value.value, visiting))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun convertList(
|
||||||
|
value: List<*>,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
): List<StateValue> {
|
||||||
|
enterContainer(value, visiting)
|
||||||
|
try {
|
||||||
|
return value.map { convertValue(it, visiting) }
|
||||||
|
} finally {
|
||||||
|
leaveContainer(value, visiting)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun convertMap(
|
||||||
|
value: Map<String, *>,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
): Map<String, StateValue> {
|
||||||
|
enterContainer(value, visiting)
|
||||||
|
try {
|
||||||
|
return value.entries.associateTo(LinkedHashMap()) { (key, mapValue) ->
|
||||||
|
key to convertValue(mapValue, visiting)
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
leaveContainer(value, visiting)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun convertGenericMap(
|
||||||
|
value: Map<*, *>,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
): Map<String, StateValue> {
|
||||||
|
enterContainer(value, visiting)
|
||||||
|
try {
|
||||||
|
return value.entries.associateTo(LinkedHashMap()) { (key, mapValue) ->
|
||||||
|
check(key is String) {
|
||||||
|
"StateValue object key must be String, but got: ${key?.let { it::class.qualifiedName }}"
|
||||||
|
}
|
||||||
|
key to convertValue(mapValue, visiting)
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
leaveContainer(value, visiting)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun convertStateValueList(
|
||||||
|
value: List<StateValue>,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
): List<StateValue> {
|
||||||
|
enterContainer(value, visiting)
|
||||||
|
try {
|
||||||
|
return value.map { normalizeStateValue(it, visiting) }
|
||||||
|
} finally {
|
||||||
|
leaveContainer(value, visiting)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun convertStateValueMap(
|
||||||
|
value: Map<String, StateValue>,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
): Map<String, StateValue> {
|
||||||
|
enterContainer(value, visiting)
|
||||||
|
try {
|
||||||
|
return value.entries.associateTo(LinkedHashMap()) { (key, mapValue) ->
|
||||||
|
key to normalizeStateValue(mapValue, visiting)
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
leaveContainer(value, visiting)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun enterContainer(
|
||||||
|
container: Any,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
) {
|
||||||
|
check(visiting.put(container, Unit) == null) {
|
||||||
|
"Circular reference detected while constructing StateValue"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun leaveContainer(
|
||||||
|
container: Any,
|
||||||
|
visiting: java.util.IdentityHashMap<Any, Unit>
|
||||||
|
) {
|
||||||
|
visiting.remove(container)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package work.slhaf.partner.framework.agent.state
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
testNormalStateJson()
|
||||||
|
println()
|
||||||
|
testCircularReference()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun testNormalStateJson() {
|
||||||
|
val nestedMap = linkedMapOf(
|
||||||
|
"name" to "partner",
|
||||||
|
"enabled" to true,
|
||||||
|
"count" to 3,
|
||||||
|
"tags" to listOf("agent", "runtime", "state-center"),
|
||||||
|
"meta" to linkedMapOf(
|
||||||
|
"version" to "0.1.0",
|
||||||
|
"experimental" to false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
val state = State()
|
||||||
|
state.append("root", StateValue.obj(nestedMap))
|
||||||
|
state.append(
|
||||||
|
"arr",
|
||||||
|
StateValue.arr(
|
||||||
|
listOf(
|
||||||
|
"hello",
|
||||||
|
123,
|
||||||
|
true,
|
||||||
|
linkedMapOf(
|
||||||
|
"nested" to "value"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
println("=== normal state ===")
|
||||||
|
println(state.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun testCircularReference() {
|
||||||
|
val cyclicMap = linkedMapOf<String, Any>()
|
||||||
|
cyclicMap["name"] = "cyclic"
|
||||||
|
cyclicMap["self"] = cyclicMap
|
||||||
|
|
||||||
|
println("=== circular reference ===")
|
||||||
|
|
||||||
|
try {
|
||||||
|
val state = State()
|
||||||
|
state.append("cyclic", StateValue.obj(cyclicMap))
|
||||||
|
|
||||||
|
// 如果前面没有抛错,这里再触发最终 JSON 输出
|
||||||
|
println(state.toString())
|
||||||
|
error("Expected circular reference detection, but no exception was thrown.")
|
||||||
|
} catch (e: IllegalStateException) {
|
||||||
|
println("circular reference detected as expected:")
|
||||||
|
println(e.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user