refactor(context): rename VisibleDomain into FocusedDomain, and optimize related context usage

This commit is contained in:
2026-04-15 22:16:33 +08:00
parent 999a6a8d7e
commit 347560d979
20 changed files with 73 additions and 75 deletions

View File

@@ -108,7 +108,7 @@ public class CognitionCore implements StateSerializable {
appendRepeatedElements(document, root, "chat_message", resolveRecentChatMessages());
}
},
Set.of(ContextBlock.VisibleDomain.COGNITION),
Set.of(ContextBlock.FocusedDomain.COMMUNICATION),
100,
10,
4

View File

@@ -27,10 +27,10 @@ class ContextWorkspace {
private val lock = ReentrantReadWriteLock()
/**
* 根据传入的 [ContextBlock.VisibleDomain] 列表,获取上下文块
* 根据传入的 [ContextBlock.FocusedDomain] 列表,获取上下文块
* @param domains 需要获取上下文的域列表,顺序将决定权重优先级,按照列表排序将具备线性权重分层,最终反映到 blockContent 列表的排序上
*/
fun resolve(domains: List<ContextBlock.VisibleDomain>): ResolvedContext = lock.write {
fun resolve(domains: List<ContextBlock.FocusedDomain>): ResolvedContext = lock.write {
if (domains.isEmpty()) {
return@write ResolvedContext(emptyList())
}
@@ -51,7 +51,7 @@ class ContextWorkspace {
continue
}
val matchedDomains = block.visibleTo.intersect(domainWeights.keys)
val matchedDomains = block.focusedOn.intersect(domainWeights.keys)
if (matchedDomains.isEmpty()) {
continue
}
@@ -158,7 +158,7 @@ class ContextWorkspace {
val payload = JSONObject()
payload["blockName"] = block.sourceKey.blockName
payload["source"] = block.sourceKey.source
payload["visibleTo"] = block.visibleTo.map { it.name }
payload["focusedOn"] = block.focusedOn.map { it.name }
payload["fullRendered"] = block.blockContent.encodeToXmlString()
payload["compactRendered"] = block.compactBlock.encodeToXmlString()
payload["abstractRendered"] = block.abstractBlock.encodeToXmlString()
@@ -179,9 +179,9 @@ data class ContextBlock @JvmOverloads constructor(
val compactBlock: BlockContent = blockContent,
val abstractBlock: BlockContent = blockContent,
/**
* 哪些域可见
* 该 block 集中在哪些域
*/
val visibleTo: Set<VisibleDomain>,
val focusedOn: Set<FocusedDomain>,
/**
* 新的 [blockContent] 属性与其相同时,发生的衰退步长
@@ -216,7 +216,7 @@ data class ContextBlock @JvmOverloads constructor(
private var activationScore = 100.0
private var lastTouchedAt = Instant.now()
enum class VisibleDomain {
enum class FocusedDomain {
ACTION,
MEMORY,
PERCEIVE,

View File

@@ -79,7 +79,7 @@ class BuiltinCapabilityActionProvider implements BuiltinActionProvider {
MemoryUnit unit = memoryCapability.getMemoryUnit(unitId);
cognitionCapability.contextWorkspace().register(new ContextBlock(
buildMemoryRecallFullBlock(unit, slice),
Set.of(ContextBlock.VisibleDomain.MEMORY),
Set.of(ContextBlock.FocusedDomain.MEMORY),
60,
16,
28

View File

@@ -12,10 +12,7 @@ import work.slhaf.partner.core.action.entity.MetaAction;
import work.slhaf.partner.core.action.entity.MetaActionInfo;
import work.slhaf.partner.core.action.entity.intervention.InterventionType;
import work.slhaf.partner.core.action.entity.intervention.MetaIntervention;
import work.slhaf.partner.core.cognition.BlockContent;
import work.slhaf.partner.core.cognition.CognitionCapability;
import work.slhaf.partner.core.cognition.ContextBlock;
import work.slhaf.partner.core.cognition.ContextWorkspace;
import work.slhaf.partner.core.cognition.*;
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException;
import work.slhaf.partner.framework.agent.exception.ExceptionReporterHandler;
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
@@ -133,7 +130,7 @@ class BuiltinInterventionActionProvider implements BuiltinActionProvider {
String blockName = "acquire_intervention-" + actionId;
String source = "action_executor";
contextWorkspace.register(new ContextBlock(
new BlockContent(blockName, source) {
new CommunicationBlockContent(blockName, source, BlockContent.Urgency.HIGH, CommunicationBlockContent.Projection.SUPPLY) {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendTextElement(document, root, "action_id", actionId);
@@ -141,7 +138,7 @@ class BuiltinInterventionActionProvider implements BuiltinActionProvider {
appendTextElement(document, root, "demand", demand);
}
},
Set.of(ContextBlock.VisibleDomain.ACTION, ContextBlock.VisibleDomain.COMMUNICATION),
Set.of(ContextBlock.FocusedDomain.ACTION),
10,
10,
20

View File

@@ -53,9 +53,9 @@ public class ActionCorrectionRecognizer extends AbstractAgentModule.Sub<Correcti
private Message resolveContextMessage() {
return cognitionCapability.contextWorkspace().resolve(List.of(
ContextBlock.VisibleDomain.ACTION,
ContextBlock.VisibleDomain.COGNITION,
ContextBlock.VisibleDomain.MEMORY
ContextBlock.FocusedDomain.ACTION,
ContextBlock.FocusedDomain.COGNITION,
ContextBlock.FocusedDomain.MEMORY
)).encodeToMessage();
}

View File

@@ -63,9 +63,10 @@ public class ActionCorrector extends AbstractAgentModule.Sub<CorrectorInput, Res
private Message resolveContextMessage() {
return cognitionCapability.contextWorkspace().resolve(List.of(
ContextBlock.VisibleDomain.ACTION,
ContextBlock.VisibleDomain.COGNITION,
ContextBlock.VisibleDomain.MEMORY
ContextBlock.FocusedDomain.ACTION,
ContextBlock.FocusedDomain.COMMUNICATION,
ContextBlock.FocusedDomain.COGNITION,
ContextBlock.FocusedDomain.MEMORY
)).encodeToMessage();
}

View File

@@ -39,7 +39,7 @@ class ExecutingActionBlockManager {
buildExecutingActionRecoveredFullBlock(snapshots, blockName, emittedAt, event),
buildExecutingActionRecoveredCompactBlock(snapshots, blockName, emittedAt, event),
buildExecutingActionRecoveredAbstractBlock(snapshots, blockName, event),
Set.of(ContextBlock.VisibleDomain.ACTION),
Set.of(ContextBlock.FocusedDomain.ACTION),
100,
12,
1
@@ -97,7 +97,7 @@ class ExecutingActionBlockManager {
buildStateActionFullBlock(snapshot, blockName, emittedAt, event),
buildStateActionCompactBlock(snapshot, blockName, emittedAt, event),
buildStateActionAbstractBlock(snapshot, blockName, event),
Set.of(ContextBlock.VisibleDomain.ACTION),
Set.of(ContextBlock.FocusedDomain.ACTION),
70,
18,
10
@@ -151,7 +151,7 @@ class ExecutingActionBlockManager {
buildActionLaunchedFullBlock(snapshot, blockName, event, emittedAt),
buildActionCompactBlock(snapshot, blockName, event, emittedAt),
buildActionAbstractBlock(snapshot, blockName, event),
Set.of(ContextBlock.VisibleDomain.ACTION),
Set.of(ContextBlock.FocusedDomain.ACTION),
28,
6,
18
@@ -230,7 +230,7 @@ class ExecutingActionBlockManager {
buildActionStageFullBlock(snapshot, blockName, emittedAt, event),
buildActionStageCompactBlock(snapshot, blockName, emittedAt, event),
buildActionStageAbstractBlock(snapshot, blockName, event),
Set.of(ContextBlock.VisibleDomain.ACTION),
Set.of(ContextBlock.FocusedDomain.ACTION),
55,
10,
12
@@ -305,7 +305,7 @@ class ExecutingActionBlockManager {
buildActionCorrectionFullBlock(snapshot, reason, interventions, blockName, emittedAt, event),
buildActionCorrectionCompactBlock(snapshot, reason, interventions, blockName, emittedAt, event),
buildActionCorrectionAbstractBlock(snapshot, interventions, blockName, event),
Set.of(ContextBlock.VisibleDomain.ACTION),
Set.of(ContextBlock.FocusedDomain.ACTION),
22,
5,
22
@@ -372,7 +372,7 @@ class ExecutingActionBlockManager {
buildActionFinishedFullBlock(snapshot, blockName, emittedAt, event),
buildActionFinishedFullBlock(snapshot, blockName, emittedAt, event),
buildActionFinishedAbstractBlock(snapshot, blockName, event),
Set.of(ContextBlock.VisibleDomain.ACTION),
Set.of(ContextBlock.FocusedDomain.ACTION),
35,
14,
24

View File

@@ -62,9 +62,9 @@ public class ParamsExtractor extends AbstractAgentModule.Sub<ExtractorInput, Res
private Message resolveContextMessage() {
return cognitionCapability.contextWorkspace()
.resolve(List.of(
ContextBlock.VisibleDomain.ACTION,
ContextBlock.VisibleDomain.COGNITION,
ContextBlock.VisibleDomain.MEMORY
ContextBlock.FocusedDomain.ACTION,
ContextBlock.FocusedDomain.COMMUNICATION,
ContextBlock.FocusedDomain.MEMORY
))
.encodeToMessage();
}

View File

@@ -93,7 +93,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
buildTendenciesEvaluatingFullBlock(tendencies),
buildTendenciesEvaluatingCompactBlock(tendencies, datetime, input),
buildTendenciesEvaluatingAbstractBlock(tendencies, datetime, input),
Set.of(ContextBlock.VisibleDomain.ACTION, ContextBlock.VisibleDomain.COMMUNICATION),
Set.of(ContextBlock.FocusedDomain.ACTION),
60,
18,
4
@@ -200,7 +200,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
buildPendingBlock(blockName, executableAction, evaluatorResult),
buildPendingCompactBlock(blockName, executableAction, evaluatorResult, input),
buildPendingAbstractBlock(blockName, executableAction, evaluatorResult, input),
Set.of(ContextBlock.VisibleDomain.ACTION),
Set.of(ContextBlock.FocusedDomain.ACTION),
30,
10,
5

View File

@@ -55,9 +55,10 @@ public class ActionEvaluator extends AbstractAgentModule.Sub<EvaluatorInput, Lis
try {
List<Message> messages = List.of(
cognitionCapability.contextWorkspace().resolve(List.of(
ContextBlock.VisibleDomain.ACTION,
ContextBlock.VisibleDomain.COGNITION,
ContextBlock.VisibleDomain.MEMORY
ContextBlock.FocusedDomain.ACTION,
ContextBlock.FocusedDomain.COMMUNICATION,
ContextBlock.FocusedDomain.COGNITION,
ContextBlock.FocusedDomain.MEMORY
)).encodeToMessage(),
availableMetaActionContext(),
new Message(Message.Character.USER, tendency)

View File

@@ -23,8 +23,8 @@ public class ActionExtractor extends AbstractAgentModule.Sub<String, Result<Extr
protected @NotNull Result<ExtractorResult> doExecute(String input) {
List<Message> messages = List.of(
cognitionCapability.contextWorkspace().resolve(List.of(
ContextBlock.VisibleDomain.COGNITION,
ContextBlock.VisibleDomain.ACTION
ContextBlock.FocusedDomain.COMMUNICATION,
ContextBlock.FocusedDomain.ACTION
)).encodeToMessage(),
new Message(Message.Character.USER, input)
);

View File

@@ -79,7 +79,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
private List<Message> buildChatMessages(PartnerRunningFlowContext runningFlowContext) {
ResolvedContext resolvedContext = cognitionCapability.contextWorkspace()
.resolve(List.of(ContextBlock.VisibleDomain.COMMUNICATION, ContextBlock.VisibleDomain.MEMORY, ContextBlock.VisibleDomain.PERCEIVE, ContextBlock.VisibleDomain.ACTION));
.resolve(List.of(ContextBlock.FocusedDomain.COGNITION, ContextBlock.FocusedDomain.ACTION, ContextBlock.FocusedDomain.MEMORY, ContextBlock.FocusedDomain.PERCEIVE));
List<BlockContent> communicationBlocks = resolvedContext.getBlocks();
List<Message> historyMessages = snapshotConversationMessages();
List<Message> temp = new ArrayList<>(historyMessages.size() + 2);

View File

@@ -175,7 +175,7 @@ public class DialogRolling extends AbstractAgentModule.Running<PartnerRunningFlo
private void applyRolling(RollingResult result) {
cognitionCapability.contextWorkspace().register(new ContextBlock(
buildDialogAbstractBlock(result.summary(), result.memoryUnit().getId(), result.memorySlice().getId()),
Set.of(ContextBlock.VisibleDomain.MEMORY, ContextBlock.VisibleDomain.COMMUNICATION),
Set.of(ContextBlock.FocusedDomain.MEMORY, ContextBlock.FocusedDomain.COMMUNICATION),
20,
5,
10

View File

@@ -66,7 +66,7 @@ public class MemorySelector extends AbstractAgentModule.Running<PartnerRunningFl
buildMemoryFullBlock(activatedSlices),
buildMemoryCompactBlock(activatedSlices),
buildMemoryAbstractBlock(activatedSlices),
Set.of(ContextBlock.VisibleDomain.MEMORY),
Set.of(ContextBlock.FocusedDomain.MEMORY),
18,
8,
16

View File

@@ -50,8 +50,7 @@ public class SliceSelectEvaluator extends AbstractAgentModule.Sub<EvaluatorInput
CountDownLatch latch = new CountDownLatch(preparedSlices.size());
Message contextMessage = cognitionCapability.contextWorkspace().resolve(List.of(
ContextBlock.VisibleDomain.COGNITION,
ContextBlock.VisibleDomain.MEMORY
ContextBlock.FocusedDomain.MEMORY
)).encodeToMessage();
for (ActivatedMemorySlice slice : preparedSlices) {

View File

@@ -73,7 +73,7 @@ public class MemorySelectExtractor extends AbstractAgentModule.Sub<ExtractorInpu
private Message resolveContextMessage() {
return cognitionCapability.contextWorkspace().resolve(List.of(
ContextBlock.VisibleDomain.COGNITION, ContextBlock.VisibleDomain.MEMORY
ContextBlock.FocusedDomain.COMMUNICATION, ContextBlock.FocusedDomain.MEMORY
)).encodeToMessage();
}

View File

@@ -45,8 +45,8 @@ public class MemoryUpdater extends AbstractAgentModule.Standalone implements Aft
Result<MemoryTopicResult> extractResult = formattedChat(
List.of(
cognitionCapability.contextWorkspace().resolve(List.of(
ContextBlock.VisibleDomain.COGNITION,
ContextBlock.VisibleDomain.MEMORY
ContextBlock.FocusedDomain.COGNITION,
ContextBlock.FocusedDomain.MEMORY
)).encodeToMessage(),
resolveTopicTaskMessage(result, slicedMessages)
),

View File

@@ -38,7 +38,7 @@ public class PerceiveMonitor extends AbstractAgentModule.Running<PartnerRunningF
appendTextElement(document, root, "current_time", ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
},
Set.of(ContextBlock.VisibleDomain.COMMUNICATION),
Set.of(ContextBlock.FocusedDomain.PERCEIVE),
100,
30,
0

View File

@@ -39,7 +39,7 @@ public class ContextExceptionReporter implements ExceptionReporter {
ExceptionReport report = exception.toReport();
cognitionCapability.contextWorkspace().register(new ContextBlock(
buildExceptionReportBlock(report),
Set.of(ContextBlock.VisibleDomain.COGNITION),
Set.of(ContextBlock.FocusedDomain.COGNITION),
10,
10,
0