refactor(memory): move memory id refresh into runtime init

This commit is contained in:
2026-03-10 20:17:34 +08:00
parent 0903b8482b
commit 027e8bddc0
2 changed files with 16 additions and 7 deletions

View File

@@ -4,11 +4,14 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule; import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
import work.slhaf.partner.api.agent.factory.component.annotation.Init; import work.slhaf.partner.api.agent.factory.component.annotation.Init;
import work.slhaf.partner.api.agent.runtime.config.AgentConfigLoader; import work.slhaf.partner.api.agent.runtime.config.AgentConfigLoader;
import work.slhaf.partner.api.common.entity.PersistableObject; import work.slhaf.partner.api.common.entity.PersistableObject;
import work.slhaf.partner.common.config.PartnerAgentConfigLoader; import work.slhaf.partner.common.config.PartnerAgentConfigLoader;
import work.slhaf.partner.core.cognation.CognationCapability;
import work.slhaf.partner.core.memory.MemoryCapability;
import work.slhaf.partner.core.memory.exception.UnExistedDateIndexException; import work.slhaf.partner.core.memory.exception.UnExistedDateIndexException;
import work.slhaf.partner.core.memory.exception.UnExistedTopicException; import work.slhaf.partner.core.memory.exception.UnExistedTopicException;
import work.slhaf.partner.core.memory.pojo.MemorySlice; import work.slhaf.partner.core.memory.pojo.MemorySlice;
@@ -36,6 +39,11 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
private static final String RUNTIME_KEY = "memory-runtime"; private static final String RUNTIME_KEY = "memory-runtime";
@InjectCapability
private MemoryCapability memoryCapability;
@InjectCapability
private CognationCapability cognationCapability;
private final ReentrantLock runtimeLock = new ReentrantLock(); private final ReentrantLock runtimeLock = new ReentrantLock();
private Map<String, CopyOnWriteArrayList<SliceRef>> topicSlices = new HashMap<>(); private Map<String, CopyOnWriteArrayList<SliceRef>> topicSlices = new HashMap<>();
private Map<LocalDate, CopyOnWriteArrayList<SliceRef>> dateIndex = new HashMap<>(); private Map<LocalDate, CopyOnWriteArrayList<SliceRef>> dateIndex = new HashMap<>();
@@ -44,9 +52,17 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone {
@Init @Init
public void init() { public void init() {
loadState(); loadState();
checkAndSetMemoryId();
Runtime.getRuntime().addShutdownHook(new Thread(this::saveStateSafely)); Runtime.getRuntime().addShutdownHook(new Thread(this::saveStateSafely));
} }
private void checkAndSetMemoryId() {
String currentMemoryId = memoryCapability.getCurrentMemoryId();
if (currentMemoryId == null || cognationCapability.getChatMessages().isEmpty()) {
memoryCapability.refreshMemoryId();
}
}
public void bindTopic(String topicPath, SliceRef sliceRef) { public void bindTopic(String topicPath, SliceRef sliceRef) {
String normalizedPath = normalizeTopicPath(topicPath); String normalizedPath = normalizeTopicPath(topicPath);
runtimeLock.lock(); runtimeLock.lock();

View File

@@ -25,16 +25,9 @@ public class PreprocessExecutor extends AbstractAgentModule.Running<PartnerRunni
@Override @Override
public void execute(PartnerRunningFlowContext context) { public void execute(PartnerRunningFlowContext context) {
checkAndSetMemoryId();
getInteractionContext(context); getInteractionContext(context);
} }
private void checkAndSetMemoryId() {
String currentMemoryId = memoryCapability.getCurrentMemoryId();
if (currentMemoryId == null || cognationCapability.getChatMessages().isEmpty()) {
memoryCapability.refreshMemoryId();
}
}
private void getInteractionContext(PartnerRunningFlowContext context) { private void getInteractionContext(PartnerRunningFlowContext context) {
log.debug("[PreprocessExecutor] 预处理原始输入: {}", context); log.debug("[PreprocessExecutor] 预处理原始输入: {}", context);