refactor(state): optimize StateValue building methods

This commit is contained in:
2026-05-15 23:07:40 +08:00
parent ed743521ec
commit ef096e76b3
7 changed files with 267 additions and 93 deletions

View File

@@ -50,70 +50,70 @@ final class ActionPoolStateCodec {
}
private static StateValue.Obj encodeExecutableAction(ExecutableAction action) {
Map<String, StateValue> actionMap = new LinkedHashMap<>();
actionMap.put("kind", StateValue.str(action instanceof SchedulableExecutableAction ? "schedulable" : "immediate"));
actionMap.put("uuid", StateValue.str(action.getUuid()));
actionMap.put("source", StateValue.str(action.getSource()));
actionMap.put("reason", StateValue.str(action.getReason()));
actionMap.put("description", StateValue.str(action.getDescription()));
actionMap.put("status", StateValue.str(action.getStatus().name()));
actionMap.put("tendency", StateValue.str(action.getTendency()));
actionMap.put("executing_stage", StateValue.num(action.getExecutingStage()));
Map<String, Object> actionMap = new LinkedHashMap<>();
actionMap.put("kind", action instanceof SchedulableExecutableAction ? "schedulable" : "immediate");
actionMap.put("uuid", action.getUuid());
actionMap.put("source", action.getSource());
actionMap.put("reason", action.getReason());
actionMap.put("description", action.getDescription());
actionMap.put("status", action.getStatus().name());
actionMap.put("tendency", action.getTendency());
actionMap.put("executing_stage", action.getExecutingStage());
String result = resolveExecutableResult(action);
if (result != null) {
actionMap.put("result", StateValue.str(result));
actionMap.put("result", result);
}
if (action instanceof SchedulableExecutableAction schedulableAction) {
actionMap.put("schedule_type", StateValue.str(schedulableAction.getScheduleType().name()));
actionMap.put("schedule_content", StateValue.str(schedulableAction.getScheduleContent()));
actionMap.put("enabled", StateValue.bool(schedulableAction.getEnabled()));
actionMap.put("schedule_histories", StateValue.arr(encodeScheduleHistories(schedulableAction)));
actionMap.put("schedule_type", schedulableAction.getScheduleType().name());
actionMap.put("schedule_content", schedulableAction.getScheduleContent());
actionMap.put("enabled", schedulableAction.getEnabled());
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())
.<StateValue>map(entry -> {
Map<String, StateValue> stageMap = new LinkedHashMap<>();
stageMap.put("stage", StateValue.num(entry.getKey()));
.map(entry -> {
Map<String, Object> stageMap = new LinkedHashMap<>();
stageMap.put("stage", entry.getKey());
String stageDescription = action.getStageDescriptions().get(entry.getKey());
if (stageDescription != null && !stageDescription.isBlank()) {
stageMap.put("description", StateValue.str(stageDescription));
stageMap.put("description", stageDescription);
}
stageMap.put("actions", StateValue.arr(entry.getValue().stream()
.map(metaAction -> (StateValue) encodeMetaAction(metaAction))
.toList()));
stageMap.put("actions", entry.getValue().stream()
.map(ActionPoolStateCodec::encodeMetaAction)
.toList());
return StateValue.obj(stageMap);
}).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);
}
private static StateValue.Obj encodeMetaAction(MetaAction metaAction) {
Map<String, StateValue> metaMap = new LinkedHashMap<>();
metaMap.put("name", StateValue.str(metaAction.getName()));
metaMap.put("io", StateValue.bool(metaAction.getIo()));
Map<String, Object> metaMap = new LinkedHashMap<>();
metaMap.put("name", metaAction.getName());
metaMap.put("io", metaAction.getIo());
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("location", StateValue.str(metaAction.getLocation()));
metaMap.put("params_json", StateValue.str(JSONObject.toJSONString(metaAction.getParams())));
metaMap.put("result_status", StateValue.str(metaAction.getResult().getStatus().name()));
metaMap.put("type", metaAction.getType().name());
metaMap.put("location", metaAction.getLocation());
metaMap.put("params_json", JSONObject.toJSONString(metaAction.getParams()));
metaMap.put("result_status", metaAction.getResult().getStatus().name());
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);
}
private static StateValue.Obj encodeHistoryAction(HistoryAction historyAction) {
Map<String, StateValue> historyMap = new LinkedHashMap<>();
historyMap.put("action_key", StateValue.str(historyAction.actionKey()));
historyMap.put("description", StateValue.str(historyAction.description()));
historyMap.put("result", StateValue.str(historyAction.result()));
Map<String, Object> historyMap = new LinkedHashMap<>();
historyMap.put("action_key", historyAction.actionKey());
historyMap.put("description", historyAction.description());
historyMap.put("result", historyAction.result());
return StateValue.obj(historyMap);
}
@@ -288,26 +288,26 @@ final class ActionPoolStateCodec {
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()
.sorted(Map.Entry.comparingByKey())
.<StateValue>map(entry -> {
Map<String, StateValue> stageMap = new LinkedHashMap<>();
stageMap.put("stage", StateValue.num(entry.getKey()));
stageMap.put("actions", StateValue.arr(entry.getValue().stream()
.map(historyAction -> (StateValue) encodeHistoryAction(historyAction))
.toList()));
.map(entry -> {
Map<String, Object> stageMap = new LinkedHashMap<>();
stageMap.put("stage", entry.getKey());
stageMap.put("actions", entry.getValue().stream()
.map(ActionPoolStateCodec::encodeHistoryAction)
.toList());
return StateValue.obj(stageMap);
}).toList();
}
private static List<StateValue> encodeScheduleHistories(SchedulableExecutableAction schedulableAction) {
private static List<StateValue.Obj> encodeScheduleHistories(SchedulableExecutableAction schedulableAction) {
return schedulableAction.getScheduleHistories().stream()
.<StateValue>map(scheduleHistory -> {
Map<String, StateValue> historyMap = new LinkedHashMap<>();
historyMap.put("end_time", StateValue.str(scheduleHistory.getEndTime().toString()));
historyMap.put("result", StateValue.str(scheduleHistory.getResult()));
historyMap.put("history", StateValue.arr(encodeHistoryStages(scheduleHistory.getHistory())));
.map(scheduleHistory -> {
Map<String, Object> historyMap = new LinkedHashMap<>();
historyMap.put("end_time", scheduleHistory.getEndTime().toString());
historyMap.put("result", scheduleHistory.getResult());
historyMap.put("history", encodeHistoryStages(scheduleHistory.getHistory()));
return StateValue.obj(historyMap);
})
.toList();

View File

@@ -200,13 +200,12 @@ public class ContextCore implements StateSerializable {
public @NotNull State convert() {
State state = new State();
List<StateValue.Obj> convertedMessageList = chatMessages.stream().map(message -> {
Map<String, StateValue> convertedMap = Map.of(
"role", StateValue.str(message.roleValue()),
"content", StateValue.str(message.getContent())
);
return StateValue.obj(convertedMap);
}).toList();
List<StateValue.Obj> convertedMessageList = chatMessages.stream()
.map(message -> StateValue.obj(Map.of(
"role", message.roleValue(),
"content", message.getContent()
)))
.toList();
state.append("chat_messages", StateValue.arr(convertedMessageList));
return state;

View File

@@ -175,8 +175,7 @@ public class MemoryCore implements StateSerializable {
State state = new State();
state.append("memory_session_id", StateValue.str(memorySessionId));
List<StateValue.Str> unitOverview = memoryUnits.keySet().stream()
.map(StateValue::str)
List<String> unitOverview = memoryUnits.keySet().stream()
.toList();
state.append("memory_unit_uuid_set", StateValue.arr(unitOverview));
return state;

View File

@@ -95,25 +95,23 @@ public class MemoryUnit implements StateSerializable {
state.append("id", StateValue.str(id));
state.append("update_timestamp", StateValue.num(timestamp));
List<StateValue.Obj> convertedMessageList = conversationMessages.stream().map(message -> {
Map<String, StateValue> convertedMap = Map.of(
"role", StateValue.str(message.roleValue()),
"content", StateValue.str(message.getContent())
);
return StateValue.obj(convertedMap);
}).toList();
List<StateValue.Obj> convertedMessageList = conversationMessages.stream()
.map(message -> StateValue.obj(Map.of(
"role", message.roleValue(),
"content", message.getContent()
)))
.toList();
state.append("conversation_messages", StateValue.arr(convertedMessageList));
List<StateValue.Obj> convertedSliceList = slices.stream().map(slice -> {
Map<String, StateValue> convertedMap = Map.of(
"id", StateValue.str(slice.getId()),
"start_index", StateValue.num(slice.getStartIndex()),
"end_index", StateValue.num(slice.getEndIndex()),
"summary", StateValue.str(slice.getSummary()),
"created_timestamp", StateValue.num(slice.getTimestamp())
);
return StateValue.obj(convertedMap);
}).toList();
List<StateValue.Obj> convertedSliceList = slices.stream()
.map(slice -> StateValue.obj(Map.of(
"id", slice.getId(),
"start_index", slice.getStartIndex(),
"end_index", slice.getEndIndex(),
"summary", slice.getSummary(),
"created_timestamp", slice.getTimestamp()
)))
.toList();
state.append("memory_slices", StateValue.arr(convertedSliceList));
return state;
}

View File

@@ -69,8 +69,8 @@ final class MemoryRuntimeStateCodec {
List<StateValue.Obj> dateIndexStates = dateIndex.entries().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(entry -> StateValue.obj(Map.of(
"date", StateValue.str(entry.getKey().toString()),
"refs", StateValue.arr(encodeSliceRefs(entry.getValue()))
"date", entry.getKey().toString(),
"refs", encodeSliceRefs(entry.getValue())
)))
.toList();
state.append("date_index", StateValue.arr(dateIndexStates));
@@ -82,8 +82,8 @@ final class MemoryRuntimeStateCodec {
TopicMemoryIndex.TopicTreeNode topicNode,
List<StateValue.Obj> topicStates) {
topicStates.add(StateValue.obj(Map.of(
"topic_path", StateValue.str(path),
"bindings", StateValue.arr(encodeTopicBindings(topicNode.bindings()))
"topic_path", path,
"bindings", encodeTopicBindings(topicNode.bindings())
)));
for (Map.Entry<String, TopicMemoryIndex.TopicTreeNode> childEntry : topicNode.children().entrySet()) {
collectTopicStates(path + "->" + childEntry.getKey(), childEntry.getValue(), topicStates);
@@ -93,18 +93,16 @@ final class MemoryRuntimeStateCodec {
private List<StateValue> encodeTopicBindings(List<TopicMemoryIndex.TopicBinding> bindings) {
return bindings.stream()
.map(binding -> (StateValue) StateValue.obj(Map.of(
"unit_id", StateValue.str(binding.sliceRef().getUnitId()),
"slice_id", StateValue.str(binding.sliceRef().getSliceId()),
"timestamp", StateValue.num(binding.timestamp()),
"unit_id", binding.sliceRef().getUnitId(),
"slice_id", binding.sliceRef().getSliceId(),
"timestamp", binding.timestamp(),
"activation_profile", StateValue.obj(Map.of(
"activation_weight", StateValue.num(binding.activationProfile().getActivationWeight()),
"diffusion_weight", StateValue.num(binding.activationProfile().getDiffusionWeight()),
"activation_weight", binding.activationProfile().getActivationWeight(),
"diffusion_weight", binding.activationProfile().getDiffusionWeight(),
"context_independence_weight",
StateValue.num(binding.activationProfile().getContextIndependenceWeight())
binding.activationProfile().getContextIndependenceWeight()
)),
"related_topic_paths", StateValue.arr(binding.relatedTopicPaths().stream()
.map(StateValue::str)
.toList())
"related_topic_paths", binding.relatedTopicPaths()
)))
.toList();
}
@@ -156,8 +154,8 @@ final class MemoryRuntimeStateCodec {
private List<StateValue> encodeSliceRefs(List<SliceRef> refs) {
return refs.stream()
.map(ref -> (StateValue) StateValue.obj(Map.of(
"unit_id", StateValue.str(ref.getUnitId()),
"slice_id", StateValue.str(ref.getSliceId())
"unit_id", ref.getUnitId(),
"slice_id", ref.getSliceId()
)))
.toList();
}

View File

@@ -148,10 +148,130 @@ sealed interface StateValue {
fun str(value: String) = Str(value)
@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
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)
}
}
}

View File

@@ -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)
}
}