mirror of
https://github.com/slhaf/Partner.git
synced 2026-06-27 17:49:16 +08:00
Compare commits
46 Commits
release-ct
...
9f9f7247f0
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f9f7247f0 | |||
| 15c24154f8 | |||
| a23657ec0c | |||
| 371b4a01d7 | |||
| 0567837dfe | |||
| 6dad6fdd6f | |||
| e583276938 | |||
| 42407567b1 | |||
| e5d19f31ca | |||
| ddf7f8da98 | |||
| 9269d4f678 | |||
| 03087fb259 | |||
| b73696cc24 | |||
| 96817d84fe | |||
| dd64599154 | |||
| 4b638b756e | |||
| 23a1b7093e | |||
| 9de46f3589 | |||
| fd8a0642b3 | |||
| cffb369aef | |||
| a929b3e0e6 | |||
| fe6895d10b | |||
| 8323f8ed13 | |||
| 0e1201253d | |||
| f3213675ff | |||
| 26ef5d875d | |||
| 047d1b56fe | |||
| 11aae1a353 | |||
| e5dcb49028 | |||
| 70a94d9c30 | |||
| ef096e76b3 | |||
| ed743521ec | |||
| cb8ddfe4e2 | |||
| 756c0a12ad | |||
| 8a5b844a4a | |||
|
|
8d29ea4c9e | ||
|
|
4770eaf42f | ||
| 8bb266a1c3 | |||
| 9054a9b4ad | |||
|
|
c8d5f577a1 | ||
| 7c82c4aea5 | |||
| 5491ad1747 | |||
| 1be6ed0198 | |||
| 01bfc3ee18 | |||
| 2d45adf8c3 | |||
| 707fddda79 |
16
.codegraph/.gitignore
vendored
Normal file
16
.codegraph/.gitignore
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
# CodeGraph data files
|
||||
# These are local to each machine and should not be committed
|
||||
|
||||
# Database
|
||||
*.db
|
||||
*.db-wal
|
||||
*.db-shm
|
||||
|
||||
# Cache
|
||||
cache/
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Hook markers
|
||||
.dirty
|
||||
5
.github/workflows/release-ctl.yml
vendored
5
.github/workflows/release-ctl.yml
vendored
@@ -130,6 +130,11 @@ jobs:
|
||||
echo "Ctl release tag: ${TAG}"
|
||||
echo "Ctl release version: ${VERSION}"
|
||||
|
||||
- name: Checkout release source
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ steps.release.outputs.tag }}
|
||||
|
||||
- name: Download release artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -60,4 +60,5 @@ build/
|
||||
/.codex
|
||||
|
||||
# Maven / build outputs
|
||||
dependency-reduced-pom.xml
|
||||
dependency-reduced-pom.xml
|
||||
/.backup/
|
||||
|
||||
37
.idea/misc.xml
generated
37
.idea/misc.xml
generated
@@ -1,28 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="EntryPointsManager">
|
||||
<list size="21">
|
||||
<list size="22">
|
||||
<item index="0" class="java.lang.String" itemvalue="lombok.Data" />
|
||||
<item index="1" class="java.lang.String" itemvalue="net.bytebuddy.implementation.bind.annotation.RuntimeType" />
|
||||
<item index="2" class="java.lang.String" itemvalue="picocli.CommandLine.Command" />
|
||||
<item index="3" class="java.lang.String" itemvalue="picocli.CommandLine.Mixin" />
|
||||
<item index="4" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.Capability" />
|
||||
<item index="5" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityCore" />
|
||||
<item index="6" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityMethod" />
|
||||
<item index="7" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.CoordinateManager" />
|
||||
<item index="8" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.Coordinated" />
|
||||
<item index="9" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.component.annotation.Init" />
|
||||
<item index="10" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.AfterExecute" />
|
||||
<item index="11" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.AgentRunningModule" />
|
||||
<item index="12" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.AgentSubModule" />
|
||||
<item index="13" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.BeforeExecute" />
|
||||
<item index="14" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.Init" />
|
||||
<item index="15" class="java.lang.String" itemvalue="work.slhaf.partner.api.capability.annotation.CapabilityMethod" />
|
||||
<item index="16" class="java.lang.String" itemvalue="work.slhaf.partner.api.capability.annotation.CoordinateManager" />
|
||||
<item index="17" class="java.lang.String" itemvalue="work.slhaf.partner.api.register.capability.annotation.Capability" />
|
||||
<item index="18" class="java.lang.String" itemvalue="work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityCore" />
|
||||
<item index="19" class="java.lang.String" itemvalue="work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityMethod" />
|
||||
<item index="20" class="java.lang.String" itemvalue="work.slhaf.partner.framework.agent.factory.component.annotation.AgentComponent" />
|
||||
<item index="4" class="java.lang.String" itemvalue="picocli.CommandLine.Option" />
|
||||
<item index="5" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.Capability" />
|
||||
<item index="6" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityCore" />
|
||||
<item index="7" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityMethod" />
|
||||
<item index="8" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.CoordinateManager" />
|
||||
<item index="9" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.capability.annotation.Coordinated" />
|
||||
<item index="10" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.component.annotation.Init" />
|
||||
<item index="11" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.AfterExecute" />
|
||||
<item index="12" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.AgentRunningModule" />
|
||||
<item index="13" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.AgentSubModule" />
|
||||
<item index="14" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.BeforeExecute" />
|
||||
<item index="15" class="java.lang.String" itemvalue="work.slhaf.partner.api.agent.factory.module.annotation.Init" />
|
||||
<item index="16" class="java.lang.String" itemvalue="work.slhaf.partner.api.capability.annotation.CapabilityMethod" />
|
||||
<item index="17" class="java.lang.String" itemvalue="work.slhaf.partner.api.capability.annotation.CoordinateManager" />
|
||||
<item index="18" class="java.lang.String" itemvalue="work.slhaf.partner.api.register.capability.annotation.Capability" />
|
||||
<item index="19" class="java.lang.String" itemvalue="work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityCore" />
|
||||
<item index="20" class="java.lang.String" itemvalue="work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityMethod" />
|
||||
<item index="21" class="java.lang.String" itemvalue="work.slhaf.partner.framework.agent.factory.component.annotation.AgentComponent" />
|
||||
</list>
|
||||
<writeAnnotations>
|
||||
<writeAnnotation name="work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability" />
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
<parent>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>partner-core</artifactId>
|
||||
<version>0.9.0-preview</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -20,7 +21,7 @@
|
||||
<dependency>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner-framework</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>${partner.runtime.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.nd4j/nd4j-api -->
|
||||
<dependency>
|
||||
@@ -64,6 +65,11 @@
|
||||
<artifactId>cron-utils</artifactId>
|
||||
<version>9.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.huaban</groupId>
|
||||
<artifactId>jieba-analysis</artifactId>
|
||||
<version>1.0.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -12,6 +12,7 @@ public abstract class VectorClient {
|
||||
|
||||
public static boolean status = false;
|
||||
public static VectorClient INSTANCE;
|
||||
public static String VECTOR_MODEL_ID;
|
||||
|
||||
public static void startClient(VectorConfig config) {
|
||||
try {
|
||||
@@ -23,6 +24,7 @@ public abstract class VectorClient {
|
||||
return;
|
||||
}
|
||||
status = true;
|
||||
VECTOR_MODEL_ID = config.modelId;
|
||||
} catch (VectorClientStartupException e) {
|
||||
throw e;
|
||||
} catch (VectorClientExecutionException e) {
|
||||
|
||||
@@ -39,7 +39,7 @@ public class VectorClientRegistry implements Configurable, ConfigRegistration<Ve
|
||||
@Nullable
|
||||
@Override
|
||||
public VectorConfig defaultConfig() {
|
||||
return new VectorConfig(false, null);
|
||||
return new VectorConfig(false, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,10 +5,16 @@ import work.slhaf.partner.framework.agent.config.Config;
|
||||
public sealed class VectorConfig extends Config permits VectorConfig.Ollama, VectorConfig.Onnx {
|
||||
final boolean enabled;
|
||||
final Type type;
|
||||
final String modelId;
|
||||
|
||||
public VectorConfig(boolean enabled, Type type) {
|
||||
public VectorConfig(boolean enabled, Type type, String modelId) {
|
||||
this.enabled = enabled;
|
||||
this.type = type;
|
||||
this.modelId = modelId;
|
||||
}
|
||||
|
||||
protected static String fallbackModelId(String modelId, String fallback) {
|
||||
return modelId == null || modelId.isBlank() ? fallback : modelId;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
@@ -21,8 +27,8 @@ public sealed class VectorConfig extends Config permits VectorConfig.Ollama, Vec
|
||||
final String tokenizerPath;
|
||||
final String embeddingModelPath;
|
||||
|
||||
public Onnx(boolean enabled, Type type, String tokenizerPath, String embeddingModelPath) {
|
||||
super(enabled, type);
|
||||
public Onnx(boolean enabled, Type type, String tokenizerPath, String embeddingModelPath, String modelId) {
|
||||
super(enabled, type, fallbackModelId(modelId, embeddingModelPath));
|
||||
this.tokenizerPath = tokenizerPath;
|
||||
this.embeddingModelPath = embeddingModelPath;
|
||||
}
|
||||
@@ -33,12 +39,10 @@ public sealed class VectorConfig extends Config permits VectorConfig.Ollama, Vec
|
||||
final String ollamaEmbeddingUrl;
|
||||
final String ollamaEmbeddingModel;
|
||||
|
||||
public Ollama(boolean enabled, Type type, String ollamaEmbeddingUrl, String ollamaEmbeddingModel) {
|
||||
super(enabled, type);
|
||||
public Ollama(boolean enabled, Type type, String ollamaEmbeddingUrl, String ollamaEmbeddingModel, String modelId) {
|
||||
super(enabled, type, fallbackModelId(modelId, ollamaEmbeddingModel));
|
||||
this.ollamaEmbeddingUrl = ollamaEmbeddingUrl;
|
||||
this.ollamaEmbeddingModel = ollamaEmbeddingModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
package work.slhaf.partner.core.cognition;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.cognition.context.ContextWorkspace;
|
||||
import work.slhaf.partner.core.cognition.impression.ActiveEntity;
|
||||
import work.slhaf.partner.core.cognition.impression.Entity;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.Capability;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
@Capability("cognition")
|
||||
@@ -26,4 +31,55 @@ public interface CognitionCapability {
|
||||
|
||||
Lock getMessageLock();
|
||||
|
||||
/**
|
||||
* Project user input onto known or currently active entities and append the input as runtime evidence.
|
||||
*/
|
||||
Set<ActiveEntity> projectEntity(String input);
|
||||
|
||||
/**
|
||||
* Return current active entities with their bound known entities when available.
|
||||
* ActiveEntity values are snapshots; Entity values are live known-entity references and should be updated through this capability.
|
||||
*/
|
||||
Map<ActiveEntity, Entity> showEntities();
|
||||
|
||||
/**
|
||||
* Create and register a new known entity by subject, then refresh search indexes for it.
|
||||
*/
|
||||
Entity createEntity(String subject);
|
||||
|
||||
/**
|
||||
* Return a known entity by uuid, or null when it does not exist.
|
||||
*/
|
||||
Entity getEntity(String uuid);
|
||||
|
||||
/**
|
||||
* Bind an active runtime entity to a known entity and refresh the active-entity search document.
|
||||
*/
|
||||
boolean bindActiveEntity(String runtimeId, String entityUuid);
|
||||
|
||||
/**
|
||||
* Rename the canonical subject of a known entity and refresh entity/active-entity indexes.
|
||||
*/
|
||||
boolean renameEntitySubject(String entityUuid, String newSubject);
|
||||
|
||||
/**
|
||||
* Add an alias or mention form for a known entity and refresh entity indexes.
|
||||
*/
|
||||
boolean addEntityAlias(String entityUuid, String alias, boolean deprecated);
|
||||
|
||||
/**
|
||||
* Add or replace an impression on a known entity and refresh all entity indexes.
|
||||
*/
|
||||
boolean updateEntityImpression(String entityUuid, String impression, String newImpression, double confidence);
|
||||
|
||||
/**
|
||||
* Add or replace a stable feature on a known entity and refresh all entity indexes.
|
||||
*/
|
||||
boolean updateEntityFeature(String entityUuid, String feature, String newFeature, double confidence);
|
||||
|
||||
/**
|
||||
* Add or update a relation from one known entity to another target and refresh all entity indexes.
|
||||
*/
|
||||
boolean updateEntityRelation(String entityUuid, String target, String relation, double strength);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package work.slhaf.partner.core.cognition;
|
||||
package work.slhaf.partner.core.cognition.context;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
@@ -27,7 +27,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@Slf4j
|
||||
@CapabilityCore(value = "cognition")
|
||||
public class CognitionCore implements StateSerializable {
|
||||
public class ContextCore implements StateSerializable {
|
||||
|
||||
private static final String RECENT_CHAT_MESSAGE_NOTES = """
|
||||
消息格式:
|
||||
@@ -58,7 +58,7 @@ public class CognitionCore implements StateSerializable {
|
||||
|
||||
private final ContextWorkspace contextWorkspace = new ContextWorkspace();
|
||||
|
||||
public CognitionCore() {
|
||||
public ContextCore() {
|
||||
register();
|
||||
}
|
||||
|
||||
@@ -200,13 +200,12 @@ public class CognitionCore 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;
|
||||
@@ -1,4 +1,4 @@
|
||||
package work.slhaf.partner.core.cognition
|
||||
package work.slhaf.partner.core.cognition.context
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject
|
||||
import org.w3c.dom.Document
|
||||
@@ -1,4 +1,4 @@
|
||||
package work.slhaf.partner.core.cognition
|
||||
package work.slhaf.partner.core.cognition.context
|
||||
|
||||
import org.w3c.dom.Document
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message
|
||||
@@ -0,0 +1,145 @@
|
||||
package work.slhaf.partner.core.cognition.impression
|
||||
|
||||
import org.w3c.dom.Document
|
||||
import org.w3c.dom.Element
|
||||
import work.slhaf.partner.core.cognition.context.BlockContent
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
class ActiveEntity @JvmOverloads constructor(
|
||||
val runtimeId: String = newActiveEntityRuntimeId(),
|
||||
val createdAt: Instant = Instant.now(),
|
||||
boundEntityUuid: String? = null,
|
||||
private val _evidences: MutableList<EntityEvidence> = mutableListOf(),
|
||||
) : BlockContent("active_entity_$runtimeId", "impression") {
|
||||
val evidences: List<EntityEvidence>
|
||||
get() = synchronized(_evidences) { _evidences.toList() }
|
||||
|
||||
@Volatile
|
||||
var lastMentionedAt: Instant = createdAt
|
||||
private set
|
||||
|
||||
private val _subject = AtomicReference("UNKNOWN")
|
||||
val subject: String get() = _subject.get()
|
||||
|
||||
private val _boundEntityUuid = AtomicReference<String?>(boundEntityUuid)
|
||||
val boundEntityUuid: String? get() = _boundEntityUuid.get()
|
||||
|
||||
private val _projectedFeatures: MutableMap<String, Double> = mutableMapOf()
|
||||
val projectedFeatures: Map<String, Double>
|
||||
get() = synchronized(_projectedFeatures) { _projectedFeatures.toMap() }
|
||||
|
||||
private val _projectedImpressions: MutableMap<String, Double> = mutableMapOf()
|
||||
val projectedImpressions: Map<String, Double>
|
||||
get() = synchronized(_projectedImpressions) { _projectedImpressions.toMap() }
|
||||
|
||||
@JvmOverloads
|
||||
fun addEvidence(
|
||||
content: String,
|
||||
associationConfidence: Double = 1.0,
|
||||
source: EntityEvidence.Source = EntityEvidence.Source.USER_INPUT,
|
||||
timestamp: Long = System.currentTimeMillis(),
|
||||
) = addEvidence(EntityEvidence(content, associationConfidence, source, timestamp))
|
||||
|
||||
fun addEvidence(evidence: EntityEvidence) = synchronized(_evidences) {
|
||||
_evidences.add(evidence)
|
||||
touch(Instant.ofEpochMilli(evidence.timestamp))
|
||||
}
|
||||
|
||||
fun updateSubject(subject: String) = _subject.set(subject)
|
||||
|
||||
fun bindEntity(uuid: String?) = _boundEntityUuid.set(uuid)
|
||||
|
||||
fun touch(time: Instant = Instant.now()) {
|
||||
lastMentionedAt = time
|
||||
}
|
||||
|
||||
fun addProjectedFeatures(vararg features: Pair<String, Double>) = synchronized(_projectedFeatures) {
|
||||
features.forEach { _projectedFeatures[it.first] = it.second }
|
||||
}
|
||||
|
||||
fun addProjectedImpressions(vararg impressions: Pair<String, Double>) = synchronized(_projectedImpressions) {
|
||||
impressions.forEach { _projectedImpressions[it.first] = it.second }
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a detached runtime snapshot for external inspection without exposing mutable internal collections.
|
||||
*/
|
||||
fun snapshot(): ActiveEntity {
|
||||
val copied = ActiveEntity(
|
||||
runtimeId = runtimeId,
|
||||
createdAt = createdAt,
|
||||
boundEntityUuid = boundEntityUuid,
|
||||
_evidences = synchronized(_evidences) { _evidences.toMutableList() },
|
||||
)
|
||||
copied.updateSubject(subject)
|
||||
copied.touch(lastMentionedAt)
|
||||
copied.addProjectedFeatures(*projectedFeatures.entries.map { it.key to it.value }.toTypedArray())
|
||||
copied.addProjectedImpressions(*projectedImpressions.entries.map { it.key to it.value }.toTypedArray())
|
||||
return copied
|
||||
}
|
||||
|
||||
override fun fillXml(document: Document, root: Element) {
|
||||
root.setAttribute("runtime_id", runtimeId)
|
||||
boundEntityUuid?.let { root.setAttribute("bound_entity_uuid", it) }
|
||||
root.setAttribute("created_at", modelTime(createdAt))
|
||||
root.setAttribute("last_mentioned_at", modelTime(lastMentionedAt))
|
||||
|
||||
appendTextElement(document, root, "subject", subject)
|
||||
|
||||
appendListElement(
|
||||
document,
|
||||
root,
|
||||
"evidences",
|
||||
"evidence",
|
||||
synchronized(_evidences) { _evidences.toList() }
|
||||
) { evidence ->
|
||||
setAttribute("association_confidence", evidence.associationConfidence.toString())
|
||||
setAttribute("source", evidence.source.name)
|
||||
setAttribute("timestamp", evidence.timestamp.toString())
|
||||
setAttribute("truncated", evidence.isContentTruncated().toString())
|
||||
setAttribute("original_length", evidence.content.length.toString())
|
||||
textContent = evidence.contentForContext()
|
||||
}
|
||||
|
||||
appendListElement(
|
||||
document,
|
||||
root,
|
||||
"projected_features",
|
||||
"feature",
|
||||
synchronized(_projectedFeatures) { _projectedFeatures.entries.toList() }
|
||||
) { entry ->
|
||||
setAttribute("confidence", entry.value.toString())
|
||||
textContent = entry.key
|
||||
}
|
||||
|
||||
appendListElement(
|
||||
document,
|
||||
root,
|
||||
"projected_impressions",
|
||||
"impression",
|
||||
synchronized(_projectedImpressions) { _projectedImpressions.entries.toList() }
|
||||
) { entry ->
|
||||
setAttribute("confidence", entry.value.toString())
|
||||
textContent = entry.key
|
||||
}
|
||||
}
|
||||
|
||||
private fun modelTime(time: Instant): String =
|
||||
time.atZone(ZoneId.systemDefault()).toString()
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is ActiveEntity) return false
|
||||
return runtimeId == other.runtimeId
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return runtimeId.hashCode()
|
||||
}
|
||||
}
|
||||
|
||||
private fun newActiveEntityRuntimeId(): String =
|
||||
UUID.randomUUID().toString().replace("-", "").take(12)
|
||||
@@ -0,0 +1,399 @@
|
||||
package work.slhaf.partner.core.cognition.impression
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray
|
||||
import com.alibaba.fastjson2.JSONObject
|
||||
import work.slhaf.partner.framework.agent.state.State
|
||||
import work.slhaf.partner.framework.agent.state.StateSerializable
|
||||
import work.slhaf.partner.framework.agent.state.StateValue
|
||||
import java.nio.file.Path
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import kotlin.concurrent.withLock
|
||||
|
||||
class Entity @JvmOverloads constructor(
|
||||
val uuid: String = UUID.randomUUID().toString(),
|
||||
subject: String,
|
||||
private val relations: MutableMap<String, MutableMap<String, Double>> = mutableMapOf(),
|
||||
private val impressions: MutableMap<String, IndexableData> = mutableMapOf(),
|
||||
private val features: MutableMap<String, IndexableData> = mutableMapOf(),
|
||||
private val aliases: MutableMap<String, AliasMetadata> = mutableMapOf()
|
||||
) : StateSerializable {
|
||||
|
||||
private var _subject: String = normalizeIdentityText(subject)
|
||||
|
||||
private val impressionLock = ReentrantLock()
|
||||
private val relationLock = ReentrantLock()
|
||||
private val featureLock = ReentrantLock()
|
||||
private val identityLock = ReentrantLock()
|
||||
|
||||
val subject: String
|
||||
get() = identityLock.withLock { _subject }
|
||||
|
||||
@JvmOverloads
|
||||
fun renameSubject(newSubject: String, keepOldSubjectAsAlias: Boolean = true): Boolean = identityLock.withLock {
|
||||
val normalizedSubject = normalizeIdentityText(newSubject)
|
||||
if (normalizedSubject.isBlank() || normalizedSubject == _subject) {
|
||||
return@withLock false
|
||||
}
|
||||
|
||||
val previousSubject = _subject
|
||||
if (keepOldSubjectAsAlias && previousSubject.isNotBlank()) {
|
||||
aliases[previousSubject] = aliases[previousSubject]?.copy(deprecated = true)
|
||||
?: AliasMetadata(Instant.now(), deprecated = true)
|
||||
}
|
||||
|
||||
aliases.remove(normalizedSubject)
|
||||
_subject = normalizedSubject
|
||||
true
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun addAlias(alias: String, deprecated: Boolean = false): Boolean = identityLock.withLock {
|
||||
val normalizedAlias = normalizeIdentityText(alias)
|
||||
if (normalizedAlias.isBlank() || normalizedAlias == _subject) {
|
||||
return@withLock false
|
||||
}
|
||||
|
||||
aliases[normalizedAlias] = aliases[normalizedAlias]?.copy(deprecated = deprecated)
|
||||
?: AliasMetadata(Instant.now(), deprecated)
|
||||
true
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun showAliases(includeDeprecated: Boolean = false): Set<AliasView> = identityLock.withLock {
|
||||
aliases.asSequence()
|
||||
.filter { (_, metadata) -> includeDeprecated || !metadata.deprecated }
|
||||
.map { (alias, metadata) ->
|
||||
AliasView(alias, metadata.instant, metadata.deprecated)
|
||||
}
|
||||
.sortedWith(compareBy<AliasView> { it.createdAt }.thenBy { it.alias })
|
||||
.toCollection(LinkedHashSet())
|
||||
}
|
||||
|
||||
fun snapshotAliases(): Map<String, AliasMetadata> = identityLock.withLock {
|
||||
aliases.mapValues { (_, metadata) -> metadata.copy() }
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun updateRelation(
|
||||
target: String,
|
||||
relation: String,
|
||||
strength: Double = 1.0
|
||||
) = relationLock.withLock {
|
||||
relations.computeIfAbsent(target) { mutableMapOf() }[relation] = strength
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun updateImpression(
|
||||
impression: String,
|
||||
newImpression: String? = null,
|
||||
confidence: Double = 1.0
|
||||
): IndexableData = impressionLock.withLock {
|
||||
if (newImpression == null) {
|
||||
impressions.computeIfAbsent(impression) { IndexableData(confidence) }
|
||||
.also {
|
||||
it.confidence = confidence
|
||||
if (it.confidence >= 0.9) {
|
||||
featureLock.withLock { features[impression] = it }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
impressions.remove(impression)
|
||||
IndexableData(confidence).also {
|
||||
impressions[newImpression] = it
|
||||
if (it.confidence >= 0.9) {
|
||||
featureLock.withLock { features[newImpression] = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateFeature(feature: String, newFeature: String? = null, confidence: Double = 1.0) = featureLock.withLock {
|
||||
if (newFeature == null) {
|
||||
features.computeIfAbsent(feature) { IndexableData(confidence) }
|
||||
.also { it.confidence = confidence }
|
||||
} else {
|
||||
features.remove(feature)
|
||||
IndexableData(confidence).also {
|
||||
features[newFeature] = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun removeFeature(feature: String) = featureLock.withLock {
|
||||
features.remove(feature)
|
||||
}
|
||||
|
||||
fun removeImpression(impression: String) = impressionLock.withLock {
|
||||
impressions.remove(impression)
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun removeRelation(
|
||||
target: String,
|
||||
relation: String? = null
|
||||
) = relationLock.withLock {
|
||||
if (relation == null) {
|
||||
relations.remove(target)
|
||||
} else {
|
||||
relations[target]?.remove(relation)
|
||||
if (relations[target].isNullOrEmpty()) {
|
||||
relations.remove(target)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun showRelations(): Set<RelationView> = relationLock.withLock {
|
||||
relations.map {
|
||||
RelationView(
|
||||
it.key,
|
||||
it.value.toMap()
|
||||
)
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
fun showImpressions(embeddingModel: String): Set<ImpressionView> = impressionLock.withLock {
|
||||
impressions.map {
|
||||
ImpressionView(
|
||||
it.key,
|
||||
it.value.confidence,
|
||||
it.value.getVector(embeddingModel)
|
||||
)
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
fun showFeatures(): Set<FeatureView> = featureLock.withLock {
|
||||
features.map {
|
||||
FeatureView(
|
||||
it.key,
|
||||
it.value.confidence
|
||||
)
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
fun snapshotImpressions(): Map<String, IndexableData> = impressionLock.withLock {
|
||||
impressions.toMap()
|
||||
}
|
||||
|
||||
fun snapshotFeatures(): Map<String, IndexableData> = featureLock.withLock {
|
||||
features.toMap()
|
||||
}
|
||||
|
||||
override fun statePath(): Path = Path.of("core", "impression", "entity-$uuid.json")
|
||||
|
||||
override fun load(state: JSONObject) {
|
||||
state.getJSONObject("relations")?.let { loadedRelations ->
|
||||
relationLock.withLock {
|
||||
relations.clear()
|
||||
loadedRelations.forEach { (target, relationValue) ->
|
||||
val relationObject = relationValue as? JSONObject ?: return@forEach
|
||||
val relationMap = mutableMapOf<String, Double>()
|
||||
relationObject.forEach { (relation, strengthValue) ->
|
||||
doubleValue(strengthValue)?.let { relationMap[relation] = it }
|
||||
}
|
||||
if (relationMap.isNotEmpty()) {
|
||||
relations[target] = relationMap
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state.getJSONObject("impressions")?.let { loadedImpressions ->
|
||||
impressionLock.withLock {
|
||||
impressions.clear()
|
||||
impressions.putAll(loadIndexableDataMap(loadedImpressions))
|
||||
}
|
||||
}
|
||||
|
||||
identityLock.withLock {
|
||||
state.getString("subject")
|
||||
?.let(::normalizeIdentityText)
|
||||
?.takeIf(String::isNotBlank)
|
||||
?.let { _subject = it }
|
||||
}
|
||||
|
||||
state.getJSONObject("features")?.let { loadedFeatures ->
|
||||
featureLock.withLock {
|
||||
features.clear()
|
||||
features.putAll(loadIndexableDataMap(loadedFeatures))
|
||||
}
|
||||
}
|
||||
|
||||
state.getJSONObject("aliases")?.let { loadedAliases ->
|
||||
identityLock.withLock {
|
||||
aliases.clear()
|
||||
loadedAliases.forEach { (alias, metadataValue) ->
|
||||
val normalizedAlias = normalizeIdentityText(alias)
|
||||
if (normalizedAlias.isBlank() || normalizedAlias == _subject) {
|
||||
return@forEach
|
||||
}
|
||||
|
||||
val metadata = when (metadataValue) {
|
||||
is JSONObject -> loadAliasMetadata(metadataValue)
|
||||
else -> AliasMetadata(Instant.now(), deprecated = false)
|
||||
}
|
||||
aliases[normalizedAlias] = metadata
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun convert(): State {
|
||||
val state = State()
|
||||
state.append("uuid", StateValue.str(uuid))
|
||||
|
||||
val identityState = identityLock.withLock {
|
||||
IdentityState(
|
||||
subject = _subject,
|
||||
aliases = aliases.mapValues { (_, metadata) ->
|
||||
mapOf(
|
||||
"timestamp" to metadata.instant.toEpochMilli(),
|
||||
"deprecated" to metadata.deprecated
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
state.append("subject", StateValue.str(identityState.subject))
|
||||
state.append("aliases", StateValue.obj(identityState.aliases))
|
||||
|
||||
val relationState = relationLock.withLock {
|
||||
relations.mapValues { (_, relationMap) -> relationMap.toMap() }
|
||||
}
|
||||
state.append("relations", StateValue.obj(relationState))
|
||||
|
||||
val impressionState = impressionLock.withLock {
|
||||
indexableDataState(impressions)
|
||||
}
|
||||
state.append("impressions", StateValue.obj(impressionState))
|
||||
|
||||
val featureState = featureLock.withLock {
|
||||
indexableDataState(features)
|
||||
}
|
||||
state.append("features", StateValue.obj(featureState))
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
override fun autoLoadOnRegister(): Boolean = false
|
||||
|
||||
private fun normalizeIdentityText(value: String): String =
|
||||
value.replace(IDENTITY_WHITESPACE_REGEX, " ").trim()
|
||||
|
||||
private fun loadAliasMetadata(state: JSONObject): AliasMetadata {
|
||||
val instant = state.getLong("timestamp")
|
||||
?.let(Instant::ofEpochMilli)
|
||||
?: state.getString("instant")
|
||||
?.let { runCatching { Instant.parse(it) }.getOrNull() }
|
||||
?: Instant.now()
|
||||
|
||||
return AliasMetadata(
|
||||
instant = instant,
|
||||
deprecated = state.getBoolean("deprecated") ?: false
|
||||
)
|
||||
}
|
||||
|
||||
private fun loadIndexableDataMap(state: JSONObject): Map<String, IndexableData> {
|
||||
val loaded = mutableMapOf<String, IndexableData>()
|
||||
state.forEach { (key, value) ->
|
||||
val data = when (value) {
|
||||
is JSONObject -> loadIndexableData(value)
|
||||
else -> IndexableData(doubleValue(value) ?: return@forEach)
|
||||
}
|
||||
loaded[key] = data
|
||||
}
|
||||
return loaded
|
||||
}
|
||||
|
||||
private fun loadIndexableData(state: JSONObject): IndexableData {
|
||||
val data = IndexableData(state.getDouble("confidence") ?: 1.0)
|
||||
state.getJSONObject("vectors")?.forEach { (embeddingModel, vectorValue) ->
|
||||
val vectorArray = vectorValue as? JSONArray ?: return@forEach
|
||||
val vector = FloatArray(vectorArray.size)
|
||||
for (index in vectorArray.indices) {
|
||||
vector[index] = floatValue(vectorArray[index]) ?: return@forEach
|
||||
}
|
||||
data.updateVector(embeddingModel, vector)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
private fun indexableDataState(source: Map<String, IndexableData>): Map<String, Map<String, Any>> =
|
||||
source.mapValues { (_, data) ->
|
||||
mapOf(
|
||||
"confidence" to data.confidence,
|
||||
"vectors" to data.snapshotVectors().mapValues { (_, vector) -> vector.toList() }
|
||||
)
|
||||
}
|
||||
|
||||
private fun doubleValue(value: Any?): Double? = when (value) {
|
||||
is Number -> value.toDouble()
|
||||
is String -> value.toDoubleOrNull()
|
||||
else -> null
|
||||
}
|
||||
|
||||
private fun floatValue(value: Any?): Float? = when (value) {
|
||||
is Number -> value.toFloat()
|
||||
is String -> value.toFloatOrNull()
|
||||
else -> null
|
||||
}
|
||||
|
||||
data class IndexableData(
|
||||
var confidence: Double
|
||||
) {
|
||||
private val vectors: ConcurrentHashMap<String, FloatArray> = ConcurrentHashMap()
|
||||
|
||||
fun updateVector(
|
||||
embeddingModel: String,
|
||||
vector: FloatArray
|
||||
) {
|
||||
vectors[embeddingModel] = vector.copyOf()
|
||||
}
|
||||
|
||||
fun getVector(embeddingModel: String): FloatArray? {
|
||||
return vectors[embeddingModel]?.copyOf()
|
||||
}
|
||||
|
||||
fun snapshotVectors(): Map<String, FloatArray> {
|
||||
return vectors.mapValues { (_, vector) -> vector.copyOf() }
|
||||
}
|
||||
}
|
||||
|
||||
data class RelationView(
|
||||
val target: String,
|
||||
val relations: Map<String, Double>
|
||||
)
|
||||
|
||||
data class FeatureView(
|
||||
val feature: String,
|
||||
val confidence: Double
|
||||
)
|
||||
|
||||
@Suppress("ArrayInDataClass")
|
||||
data class ImpressionView(
|
||||
val impression: String,
|
||||
val confidence: Double,
|
||||
val vector: FloatArray?
|
||||
)
|
||||
|
||||
private data class IdentityState(
|
||||
val subject: String,
|
||||
val aliases: Map<String, Map<String, Any>>
|
||||
)
|
||||
|
||||
data class AliasView(
|
||||
val alias: String,
|
||||
val createdAt: Instant,
|
||||
val deprecated: Boolean
|
||||
)
|
||||
|
||||
data class AliasMetadata(
|
||||
val instant: Instant,
|
||||
val deprecated: Boolean
|
||||
)
|
||||
|
||||
companion object {
|
||||
private val IDENTITY_WHITESPACE_REGEX = Regex("\\s+")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package work.slhaf.partner.core.cognition.impression
|
||||
|
||||
/**
|
||||
* Runtime evidence associated with an active entity.
|
||||
*
|
||||
* The confidence describes how strongly this evidence is associated with the
|
||||
* current active entity, not whether the evidence content itself is true.
|
||||
*/
|
||||
data class EntityEvidence @JvmOverloads constructor(
|
||||
val content: String,
|
||||
val associationConfidence: Double = 1.0,
|
||||
val source: Source = Source.USER_INPUT,
|
||||
val timestamp: Long = System.currentTimeMillis(),
|
||||
) {
|
||||
enum class Source {
|
||||
USER_INPUT,
|
||||
ASSISTANT_REPLY
|
||||
}
|
||||
|
||||
fun isContentTruncated(maxLength: Int = CONTEXT_CONTENT_MAX_LENGTH): Boolean =
|
||||
content.length > maxLength
|
||||
|
||||
fun contentForContext(maxLength: Int = CONTEXT_CONTENT_MAX_LENGTH): String {
|
||||
if (content.length <= maxLength) {
|
||||
return content
|
||||
}
|
||||
|
||||
val available = maxLength - OMITTED_MARKER.length
|
||||
if (available <= 0) {
|
||||
return content.take(maxLength)
|
||||
}
|
||||
|
||||
val headBudget = available / 2
|
||||
val tailBudget = available - headBudget
|
||||
val headEnd = adjustHeadEnd(content, headBudget)
|
||||
val tailStart = adjustTailStart(content, content.length - tailBudget)
|
||||
|
||||
if (tailStart <= headEnd) {
|
||||
return content.take(maxLength).trimEnd()
|
||||
}
|
||||
|
||||
return content.substring(0, headEnd).trimEnd() +
|
||||
OMITTED_MARKER +
|
||||
content.substring(tailStart).trimStart()
|
||||
}
|
||||
|
||||
private fun adjustHeadEnd(source: String, preferredEnd: Int): Int {
|
||||
val safePreferredEnd = preferredEnd.coerceIn(0, source.length)
|
||||
findForwardBoundary(source, safePreferredEnd, STRONG_BOUNDARY_SEARCH_WINDOW, ::isStrongBoundary)?.let {
|
||||
return it + 1
|
||||
}
|
||||
findForwardBoundary(source, safePreferredEnd, SOFT_BOUNDARY_SEARCH_WINDOW, ::isSoftBoundary)?.let {
|
||||
return it + 1
|
||||
}
|
||||
return safePreferredEnd
|
||||
}
|
||||
|
||||
private fun adjustTailStart(source: String, preferredStart: Int): Int {
|
||||
val safePreferredStart = preferredStart.coerceIn(0, source.length)
|
||||
findBackwardBoundary(source, safePreferredStart, STRONG_BOUNDARY_SEARCH_WINDOW, ::isStrongBoundary)?.let {
|
||||
return it
|
||||
}
|
||||
findBackwardBoundary(source, safePreferredStart, SOFT_BOUNDARY_SEARCH_WINDOW, ::isSoftBoundary)?.let {
|
||||
return it
|
||||
}
|
||||
return safePreferredStart
|
||||
}
|
||||
|
||||
private fun findForwardBoundary(
|
||||
source: String,
|
||||
start: Int,
|
||||
window: Int,
|
||||
predicate: (Char) -> Boolean,
|
||||
): Int? {
|
||||
val end = (start + window).coerceAtMost(source.length)
|
||||
for (index in start until end) {
|
||||
if (predicate(source[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun findBackwardBoundary(
|
||||
source: String,
|
||||
start: Int,
|
||||
window: Int,
|
||||
predicate: (Char) -> Boolean,
|
||||
): Int? {
|
||||
val end = (start - window).coerceAtLeast(0)
|
||||
for (index in start downTo end + 1) {
|
||||
if (predicate(source[index - 1])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun isStrongBoundary(char: Char): Boolean = char == '\n'
|
||||
|
||||
private fun isSoftBoundary(char: Char): Boolean = when (char) {
|
||||
'。', '!', '?', ';', ';', '.' -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val CONTEXT_CONTENT_MAX_LENGTH = 480
|
||||
private const val OMITTED_MARKER = "\n...[omitted]...\n"
|
||||
private const val STRONG_BOUNDARY_SEARCH_WINDOW = 120
|
||||
private const val SOFT_BOUNDARY_SEARCH_WINDOW = 80
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,486 @@
|
||||
package work.slhaf.partner.core.cognition.impression;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import work.slhaf.partner.core.cognition.impression.search.*;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.framework.agent.state.State;
|
||||
import work.slhaf.partner.framework.agent.state.StateSerializable;
|
||||
import work.slhaf.partner.framework.agent.state.StateValue;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@CapabilityCore(value = "cognition")
|
||||
public class ImpressionCore implements StateSerializable {
|
||||
|
||||
/**
|
||||
* Keyed by entity uuid. Subject can be revised or merged later, so it should not be used as the stable key.
|
||||
*/
|
||||
private final ConcurrentHashMap<String, Entity> knownEntitiesByUuid = new ConcurrentHashMap<>();
|
||||
private final ImpressionVectorIndex vectorIndex = new ImpressionVectorIndex();
|
||||
private final Set<ActiveEntity> activeEntities = new HashSet<>();
|
||||
private final ImpressionTextSearch textSearch = new SimpleTextSearch();
|
||||
|
||||
private static final int TEXT_SEARCH_LIMIT = 20;
|
||||
private static final int ASSOCIATION_MATCH_LIMIT = 8;
|
||||
private static final double SUPPORTING_HIT_FACTOR = 0.3;
|
||||
private static final double ASSOCIATION_CONFIDENCE_DIVISOR = 5.0;
|
||||
|
||||
/**
|
||||
* 根据新的 Input 召回相关的实体,如果实体已重复,则将输入追加到 ActiveEntity 的证据中。
|
||||
*
|
||||
* @param input 本次输入内容
|
||||
* @return 本次被召回的活跃实体(包括重复的实体)
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public Set<ActiveEntity> projectEntity(String input) {
|
||||
if (input == null || input.isBlank()) {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
List<ImpressionSearchHit> textSearchHits = textSearch.search(input, TEXT_SEARCH_LIMIT);
|
||||
List<EntityAssociationMatch> associationMatches = aggregateMatches(textSearchHits, ASSOCIATION_MATCH_LIMIT);
|
||||
if (associationMatches.isEmpty()) {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
Set<ActiveEntity> projected = new HashSet<>();
|
||||
for (EntityAssociationMatch match : associationMatches) {
|
||||
Optional<ActiveEntity> activeEntity = resolveActiveEntity(match.getTarget());
|
||||
if (activeEntity.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ActiveEntity entity = activeEntity.get();
|
||||
entity.addEvidence(
|
||||
input,
|
||||
associationConfidence(match),
|
||||
EntityEvidence.Source.USER_INPUT
|
||||
);
|
||||
refreshActiveEntityTextSearch(entity);
|
||||
projected.add(entity);
|
||||
}
|
||||
|
||||
return projected;
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出当前已存在的 ActiveEntity 以及对应的 Entity。ActiveEntity 返回快照,Entity 返回当前已知实体引用。
|
||||
*
|
||||
* 注意:外部模块不要直接修改返回的 Entity,否则文本索引 / 向量索引不会刷新。
|
||||
* Impression 更新应走 updateEntity* 系列接口。
|
||||
*
|
||||
* @return ActiveEntity 快照与已绑定 Entity 的映射
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public Map<ActiveEntity, Entity> showEntities() {
|
||||
Map<ActiveEntity, Entity> result = new LinkedHashMap<>();
|
||||
List<ActiveEntity> entities;
|
||||
synchronized (activeEntities) {
|
||||
entities = activeEntities.stream()
|
||||
.sorted(Comparator
|
||||
.comparing(ActiveEntity::getLastMentionedAt)
|
||||
.reversed()
|
||||
.thenComparing(ActiveEntity::getRuntimeId))
|
||||
.toList();
|
||||
}
|
||||
|
||||
for (ActiveEntity activeEntity : entities) {
|
||||
Entity boundEntity = Optional.ofNullable(activeEntity.getBoundEntityUuid())
|
||||
.map(knownEntitiesByUuid::get)
|
||||
.orElse(null);
|
||||
result.put(activeEntity.snapshot(), boundEntity);
|
||||
}
|
||||
return Collections.unmodifiableMap(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new known entity and make it visible to recall/update indexes immediately.
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public Entity createEntity(String subject) {
|
||||
if (subject == null || subject.isBlank()) {
|
||||
throw new IllegalArgumentException("subject must not be blank");
|
||||
}
|
||||
|
||||
Entity entity = new Entity(UUID.randomUUID().toString(), subject.trim());
|
||||
entity.register();
|
||||
knownEntitiesByUuid.put(entity.getUuid(), entity);
|
||||
refreshKnownEntityIndexes(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up a known entity by stable uuid.
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public Entity getEntity(String uuid) {
|
||||
if (uuid == null || uuid.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return knownEntitiesByUuid.get(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind a runtime active entity to a known entity.
|
||||
* This keeps the active entity in current context while giving later updates a stable storage target.
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public boolean bindActiveEntity(String runtimeId, String entityUuid) {
|
||||
if (runtimeId == null || runtimeId.isBlank() || entityUuid == null || entityUuid.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Entity entity = knownEntitiesByUuid.get(entityUuid);
|
||||
if (entity == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Optional<ActiveEntity> activeEntity = findActiveEntityByRuntimeId(runtimeId);
|
||||
if (activeEntity.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ActiveEntity active = activeEntity.get();
|
||||
active.bindEntity(entityUuid);
|
||||
active.updateSubject(entity.getSubject());
|
||||
refreshActiveEntityTextSearch(active);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename the canonical subject of a known entity and keep its previous subject as a historical alias.
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public boolean renameEntitySubject(String entityUuid, String newSubject) {
|
||||
Entity entity = knownEntitiesByUuid.get(entityUuid);
|
||||
if (entity == null || newSubject == null || newSubject.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean renamed = entity.renameSubject(newSubject.trim());
|
||||
if (!renamed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
refreshKnownEntityIndexes(entity);
|
||||
syncBoundActiveEntitySubjects(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an alias or mention form for a known entity and refresh search indexes.
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public boolean addEntityAlias(String entityUuid, String alias, boolean deprecated) {
|
||||
Entity entity = knownEntitiesByUuid.get(entityUuid);
|
||||
if (entity == null || alias == null || alias.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean added = entity.addAlias(alias.trim(), deprecated);
|
||||
if (!added) {
|
||||
return false;
|
||||
}
|
||||
|
||||
refreshKnownEntityIndexes(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a known entity impression through the core so text/vector indexes stay consistent.
|
||||
* newImpression can be null or blank to update the existing impression in place.
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public boolean updateEntityImpression(
|
||||
String entityUuid,
|
||||
String impression,
|
||||
String newImpression,
|
||||
double confidence
|
||||
) {
|
||||
Entity entity = knownEntitiesByUuid.get(entityUuid);
|
||||
if (entity == null || impression == null || impression.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
entity.updateImpression(
|
||||
impression.trim(),
|
||||
normalizeNullableText(newImpression),
|
||||
confidence
|
||||
);
|
||||
refreshKnownEntityIndexes(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a known entity feature through the core so text/vector indexes stay consistent.
|
||||
* newFeature can be null or blank to update the existing feature in place.
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public boolean updateEntityFeature(
|
||||
String entityUuid,
|
||||
String feature,
|
||||
String newFeature,
|
||||
double confidence
|
||||
) {
|
||||
Entity entity = knownEntitiesByUuid.get(entityUuid);
|
||||
if (entity == null || feature == null || feature.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
entity.updateFeature(
|
||||
feature.trim(),
|
||||
normalizeNullableText(newFeature),
|
||||
confidence
|
||||
);
|
||||
refreshKnownEntityIndexes(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a known entity relation through the core so search documents reflect the changed relation.
|
||||
*/
|
||||
@CapabilityMethod
|
||||
public boolean updateEntityRelation(
|
||||
String entityUuid,
|
||||
String target,
|
||||
String relation,
|
||||
double strength
|
||||
) {
|
||||
Entity entity = knownEntitiesByUuid.get(entityUuid);
|
||||
if (entity == null || target == null || target.isBlank() || relation == null || relation.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
entity.updateRelation(target.trim(), relation.trim(), strength);
|
||||
refreshKnownEntityIndexes(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize optional replacement text used by update methods.
|
||||
*/
|
||||
private String normalizeNullableText(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
private List<EntityAssociationMatch> aggregateMatches(
|
||||
List<ImpressionSearchHit> hits,
|
||||
int limit
|
||||
) {
|
||||
if (hits == null || hits.isEmpty() || limit <= 0) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
return hits.stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
hit -> hit.getDocument().getTarget(),
|
||||
LinkedHashMap::new,
|
||||
Collectors.toList()
|
||||
))
|
||||
.entrySet()
|
||||
.stream()
|
||||
.map(entry -> {
|
||||
List<ImpressionSearchHit> sortedHits = entry.getValue()
|
||||
.stream()
|
||||
.sorted(Comparator
|
||||
.comparingDouble(ImpressionSearchHit::getScore)
|
||||
.reversed()
|
||||
.thenComparing(hit -> hit.getDocument().getId()))
|
||||
.toList();
|
||||
return new EntityAssociationMatch(
|
||||
entry.getKey(),
|
||||
aggregateScore(sortedHits),
|
||||
sortedHits
|
||||
);
|
||||
})
|
||||
.sorted(Comparator
|
||||
.comparingDouble(EntityAssociationMatch::getScore)
|
||||
.reversed()
|
||||
.thenComparing(match -> match.getTarget().getType().name())
|
||||
.thenComparing(match -> match.getTarget().getId()))
|
||||
.limit(limit)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private double aggregateScore(List<ImpressionSearchHit> sortedHits) {
|
||||
if (sortedHits.isEmpty()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
double bestHitScore = sortedHits.getFirst().getScore();
|
||||
double supportingScore = sortedHits.stream()
|
||||
.skip(1)
|
||||
.limit(2)
|
||||
.mapToDouble(hit -> hit.getScore() * SUPPORTING_HIT_FACTOR)
|
||||
.sum();
|
||||
return bestHitScore + supportingScore;
|
||||
}
|
||||
|
||||
private Optional<ActiveEntity> resolveActiveEntity(ImpressionSearchTarget target) {
|
||||
return switch (target.getType()) {
|
||||
case ACTIVE_ENTITY -> findActiveEntityByRuntimeId(target.getId());
|
||||
case ENTITY -> activateKnownEntity(target.getId());
|
||||
};
|
||||
}
|
||||
|
||||
private Optional<ActiveEntity> findActiveEntityByRuntimeId(String runtimeId) {
|
||||
synchronized (activeEntities) {
|
||||
return activeEntities.stream()
|
||||
.filter(activeEntity -> activeEntity.getRuntimeId().equals(runtimeId))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<ActiveEntity> findActiveEntityByBoundEntityUuid(String uuid) {
|
||||
synchronized (activeEntities) {
|
||||
return activeEntities.stream()
|
||||
.filter(activeEntity -> uuid.equals(activeEntity.getBoundEntityUuid()))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<ActiveEntity> activateKnownEntity(String uuid) {
|
||||
Entity knownEntity = knownEntitiesByUuid.get(uuid);
|
||||
if (knownEntity == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Optional<ActiveEntity> existing = findActiveEntityByBoundEntityUuid(uuid);
|
||||
if (existing.isPresent()) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
ActiveEntity activeEntity = new ActiveEntity();
|
||||
activeEntity.updateSubject(knownEntity.getSubject());
|
||||
activeEntity.bindEntity(uuid);
|
||||
|
||||
synchronized (activeEntities) {
|
||||
activeEntities.add(activeEntity);
|
||||
}
|
||||
refreshActiveEntityTextSearch(activeEntity);
|
||||
return Optional.of(activeEntity);
|
||||
}
|
||||
|
||||
private double associationConfidence(EntityAssociationMatch match) {
|
||||
double normalized = match.getScore() / ASSOCIATION_CONFIDENCE_DIVISOR;
|
||||
return Math.clamp(normalized, 0.05, 1.0);
|
||||
}
|
||||
|
||||
private void refreshActiveEntityTextSearch(ActiveEntity activeEntity) {
|
||||
ImpressionSearchTarget target = new ImpressionSearchTarget(
|
||||
ImpressionSearchTarget.Type.ACTIVE_ENTITY,
|
||||
activeEntity.getRuntimeId()
|
||||
);
|
||||
textSearch.removeByTarget(target);
|
||||
for (ImpressionSearchDocument document : ImpressionSearchDocuments.INSTANCE.fromActiveEntity(activeEntity)) {
|
||||
textSearch.upsert(document);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh every index derived from a known entity after mutation.
|
||||
*/
|
||||
private void refreshKnownEntityIndexes(Entity entity) {
|
||||
vectorIndex.sync(entity);
|
||||
refreshKnownEntityTextSearch(entity);
|
||||
}
|
||||
|
||||
private void syncBoundActiveEntitySubjects(Entity entity) {
|
||||
List<ActiveEntity> boundEntities;
|
||||
synchronized (activeEntities) {
|
||||
boundEntities = activeEntities.stream()
|
||||
.filter(activeEntity -> entity.getUuid().equals(activeEntity.getBoundEntityUuid()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
boundEntities.forEach(activeEntity -> {
|
||||
activeEntity.updateSubject(entity.getSubject());
|
||||
refreshActiveEntityTextSearch(activeEntity);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace text-search documents for one known entity.
|
||||
*/
|
||||
private void refreshKnownEntityTextSearch(Entity entity) {
|
||||
ImpressionSearchTarget target = new ImpressionSearchTarget(
|
||||
ImpressionSearchTarget.Type.ENTITY,
|
||||
entity.getUuid()
|
||||
);
|
||||
textSearch.removeByTarget(target);
|
||||
for (ImpressionSearchDocument document : ImpressionSearchDocuments.INSTANCE.fromEntity(entity)) {
|
||||
textSearch.upsert(document);
|
||||
}
|
||||
}
|
||||
|
||||
private void rebuildTextSearch() {
|
||||
List<ImpressionSearchDocument> documents = new ArrayList<>();
|
||||
knownEntitiesByUuid.values().forEach(entity ->
|
||||
documents.addAll(ImpressionSearchDocuments.INSTANCE.fromEntity(entity))
|
||||
);
|
||||
synchronized (activeEntities) {
|
||||
activeEntities.forEach(activeEntity ->
|
||||
documents.addAll(ImpressionSearchDocuments.INSTANCE.fromActiveEntity(activeEntity))
|
||||
);
|
||||
}
|
||||
textSearch.rebuild(documents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Path statePath() {
|
||||
return Path.of("core", "impression.json");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(@NotNull JSONObject state) {
|
||||
JSONArray entityArray = state.getJSONArray("entities");
|
||||
if (entityArray == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
knownEntitiesByUuid.clear();
|
||||
for (int i = 0; i < entityArray.size(); i++) {
|
||||
JSONObject entityObject = entityArray.getJSONObject(i);
|
||||
if (entityObject == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String uuid = entityObject.getString("uuid");
|
||||
String subject = entityObject.getString("subject");
|
||||
if (uuid == null || uuid.isBlank() || subject == null || subject.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Entity entity = new Entity(uuid, subject);
|
||||
entity.register();
|
||||
entity.load();
|
||||
vectorIndex.sync(entity);
|
||||
knownEntitiesByUuid.put(uuid, entity);
|
||||
}
|
||||
rebuildTextSearch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull State convert() {
|
||||
State state = new State();
|
||||
|
||||
List<StateValue.Obj> entities = knownEntitiesByUuid.values().stream()
|
||||
.map(entity -> StateValue.obj(Map.of(
|
||||
"uuid", entity.getUuid(),
|
||||
"subject", entity.getSubject()
|
||||
)))
|
||||
.toList();
|
||||
|
||||
state.append("entities", StateValue.arr(entities));
|
||||
return state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package work.slhaf.partner.core.cognition.impression;
|
||||
|
||||
import work.slhaf.partner.common.vector.VectorClient;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class ImpressionVectorIndex {
|
||||
|
||||
private final Executor executor = Executors.newFixedThreadPool(2, r -> {
|
||||
Thread thread = new Thread(r, "impression-vector-index");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
|
||||
public void sync(Entity entity) {
|
||||
if (!VectorClient.status){
|
||||
return;
|
||||
}
|
||||
entity.snapshotFeatures().forEach(this::upsert);
|
||||
entity.snapshotImpressions().forEach(this::upsert);
|
||||
}
|
||||
|
||||
public void upsert(String text, Entity.IndexableData indexableData){
|
||||
if (!VectorClient.status){
|
||||
return;
|
||||
}
|
||||
String modelId = VectorClient.VECTOR_MODEL_ID;
|
||||
if (indexableData.getVector(modelId) != null) {
|
||||
return;
|
||||
}
|
||||
executor.execute(() -> {
|
||||
float[] vector = VectorClient.INSTANCE.compute(text);
|
||||
indexableData.updateVector(modelId,vector);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
data class EntityAssociationMatch(
|
||||
val target: ImpressionSearchTarget,
|
||||
val score: Double,
|
||||
val hits: List<ImpressionSearchHit> = emptyList(),
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
data class ImpressionSearchDocument(
|
||||
val id: String,
|
||||
val target: ImpressionSearchTarget,
|
||||
val field: ImpressionSearchField,
|
||||
val text: String,
|
||||
val weight: Double = 1.0,
|
||||
val metadata: Map<String, String> = emptyMap(),
|
||||
)
|
||||
@@ -0,0 +1,151 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
import work.slhaf.partner.core.cognition.impression.ActiveEntity
|
||||
import work.slhaf.partner.core.cognition.impression.Entity
|
||||
|
||||
object ImpressionSearchDocuments {
|
||||
|
||||
fun fromActiveEntity(activeEntity: ActiveEntity): List<ImpressionSearchDocument> {
|
||||
val target = ImpressionSearchTarget(
|
||||
ImpressionSearchTarget.Type.ACTIVE_ENTITY,
|
||||
activeEntity.runtimeId
|
||||
)
|
||||
val metadata = activeEntity.boundEntityUuid
|
||||
?.let { mapOf("boundEntityUuid" to it) }
|
||||
.orEmpty()
|
||||
|
||||
return buildList {
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "active:${activeEntity.runtimeId}:subject",
|
||||
target = target,
|
||||
field = ImpressionSearchField.SUBJECT,
|
||||
text = activeEntity.subject,
|
||||
weight = SUBJECT_WEIGHT,
|
||||
metadata = metadata,
|
||||
)
|
||||
)
|
||||
|
||||
activeEntity.evidences.forEachIndexed { index, evidence ->
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "active:${activeEntity.runtimeId}:evidence:$index",
|
||||
target = target,
|
||||
field = ImpressionSearchField.EVIDENCE,
|
||||
text = evidence.contentForContext(),
|
||||
weight = EVIDENCE_WEIGHT * evidence.associationConfidence,
|
||||
metadata = metadata,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
activeEntity.projectedFeatures.entries.forEachIndexed { index, entry ->
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "active:${activeEntity.runtimeId}:feature:$index",
|
||||
target = target,
|
||||
field = ImpressionSearchField.FEATURE,
|
||||
text = entry.key,
|
||||
weight = FEATURE_WEIGHT * entry.value,
|
||||
metadata = metadata,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
activeEntity.projectedImpressions.entries.forEachIndexed { index, entry ->
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "active:${activeEntity.runtimeId}:impression:$index",
|
||||
target = target,
|
||||
field = ImpressionSearchField.IMPRESSION,
|
||||
text = entry.key,
|
||||
weight = IMPRESSION_WEIGHT * entry.value,
|
||||
metadata = metadata,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun fromEntity(entity: Entity): List<ImpressionSearchDocument> {
|
||||
val target = ImpressionSearchTarget(
|
||||
ImpressionSearchTarget.Type.ENTITY,
|
||||
entity.uuid
|
||||
)
|
||||
|
||||
return buildList {
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "entity:${entity.uuid}:subject",
|
||||
target = target,
|
||||
field = ImpressionSearchField.SUBJECT,
|
||||
text = entity.subject,
|
||||
weight = SUBJECT_WEIGHT,
|
||||
)
|
||||
)
|
||||
|
||||
entity.showAliases(includeDeprecated = true).forEachIndexed { index, alias ->
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "entity:${entity.uuid}:alias:$index",
|
||||
target = target,
|
||||
field = ImpressionSearchField.SUBJECT,
|
||||
text = alias.alias,
|
||||
weight = SUBJECT_WEIGHT * ALIAS_WEIGHT_FACTOR,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
entity.snapshotFeatures().keys.forEachIndexed { index, feature ->
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "entity:${entity.uuid}:feature:$index",
|
||||
target = target,
|
||||
field = ImpressionSearchField.FEATURE,
|
||||
text = feature,
|
||||
weight = FEATURE_WEIGHT,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
entity.snapshotImpressions().keys.forEachIndexed { index, impression ->
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "entity:${entity.uuid}:impression:$index",
|
||||
target = target,
|
||||
field = ImpressionSearchField.IMPRESSION,
|
||||
text = impression,
|
||||
weight = IMPRESSION_WEIGHT,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
entity.showRelations().forEachIndexed { index, relation ->
|
||||
val relationText = buildString {
|
||||
append(relation.target)
|
||||
relation.relations.keys.forEach { name ->
|
||||
append(' ')
|
||||
append(name)
|
||||
}
|
||||
}
|
||||
|
||||
add(
|
||||
ImpressionSearchDocument(
|
||||
id = "entity:${entity.uuid}:relation:$index",
|
||||
target = target,
|
||||
field = ImpressionSearchField.RELATION,
|
||||
text = relationText,
|
||||
weight = RELATION_WEIGHT,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const val SUBJECT_WEIGHT = 1.0
|
||||
private const val ALIAS_WEIGHT_FACTOR = 0.9
|
||||
private const val FEATURE_WEIGHT = 0.85
|
||||
private const val IMPRESSION_WEIGHT = 0.75
|
||||
private const val RELATION_WEIGHT = 0.65
|
||||
private const val EVIDENCE_WEIGHT = 0.8
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
enum class ImpressionSearchField {
|
||||
SUBJECT,
|
||||
FEATURE,
|
||||
IMPRESSION,
|
||||
RELATION,
|
||||
EVIDENCE
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
data class ImpressionSearchHit(
|
||||
val document: ImpressionSearchDocument,
|
||||
val score: Double,
|
||||
val matchedTerms: Set<String> = emptySet(),
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
data class ImpressionSearchTarget(
|
||||
val type: Type,
|
||||
val id: String,
|
||||
) {
|
||||
enum class Type {
|
||||
ACTIVE_ENTITY,
|
||||
ENTITY
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
interface ImpressionTextSearch {
|
||||
|
||||
fun rebuild(documents: Collection<ImpressionSearchDocument>)
|
||||
|
||||
fun upsert(document: ImpressionSearchDocument)
|
||||
|
||||
fun removeByTarget(target: ImpressionSearchTarget)
|
||||
|
||||
fun search(
|
||||
query: String,
|
||||
limit: Int = 20,
|
||||
): List<ImpressionSearchHit>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
interface ImpressionTokenizer {
|
||||
fun tokenize(text: String): Set<String>
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
import com.huaban.analysis.jieba.JiebaSegmenter
|
||||
|
||||
class JiebaImpressionTokenizer(
|
||||
private val segmenter: JiebaSegmenter = JiebaSegmenter(),
|
||||
private val mode: JiebaSegmenter.SegMode = JiebaSegmenter.SegMode.SEARCH,
|
||||
) : ImpressionTokenizer {
|
||||
|
||||
override fun tokenize(text: String): Set<String> {
|
||||
val normalized = normalize(text)
|
||||
if (normalized.isBlank()) {
|
||||
return emptySet()
|
||||
}
|
||||
|
||||
val jiebaTerms = segmenter.process(normalized, mode)
|
||||
.asSequence()
|
||||
.map { it.word }
|
||||
.map(::normalize)
|
||||
.filter { it.isNotBlank() }
|
||||
|
||||
return (jiebaTerms + alphaNumericTerms(normalized)).toSet()
|
||||
}
|
||||
|
||||
private fun alphaNumericTerms(text: String): Sequence<String> =
|
||||
ALPHA_NUMERIC_REGEX.findAll(text).map { it.value }
|
||||
|
||||
private fun normalize(text: String): String =
|
||||
text.lowercase()
|
||||
.replace(WHITESPACE_REGEX, " ")
|
||||
.trim()
|
||||
|
||||
companion object {
|
||||
private val WHITESPACE_REGEX = Regex("\\s+")
|
||||
private val ALPHA_NUMERIC_REGEX = Regex("[a-z0-9]+(?:[-_./][a-z0-9]+)*")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
class SimpleTextSearch(
|
||||
private val tokenizer: ImpressionTokenizer = JiebaImpressionTokenizer(),
|
||||
) : ImpressionTextSearch {
|
||||
|
||||
private val documents = linkedMapOf<String, IndexedDocument>()
|
||||
private val invertedIndex = linkedMapOf<String, MutableSet<String>>()
|
||||
|
||||
@Synchronized
|
||||
override fun rebuild(documents: Collection<ImpressionSearchDocument>) {
|
||||
this.documents.clear()
|
||||
invertedIndex.clear()
|
||||
documents.forEach(::upsertInternal)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun upsert(document: ImpressionSearchDocument) {
|
||||
removeByDocumentId(document.id)
|
||||
upsertInternal(document)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun removeByTarget(target: ImpressionSearchTarget) {
|
||||
documents.values
|
||||
.asSequence()
|
||||
.filter { it.document.target == target }
|
||||
.map { it.document.id }
|
||||
.toList()
|
||||
.forEach(::removeByDocumentId)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun search(query: String, limit: Int): List<ImpressionSearchHit> {
|
||||
if (limit <= 0) {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
val normalizedQuery = normalize(query)
|
||||
if (normalizedQuery.isBlank()) {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
val queryTerms = tokenizer.tokenize(normalizedQuery)
|
||||
val candidateIds = if (queryTerms.isEmpty()) {
|
||||
documents.keys.toSet()
|
||||
} else {
|
||||
queryTerms
|
||||
.asSequence()
|
||||
.flatMap { invertedIndex[it].orEmpty().asSequence() }
|
||||
.toSet()
|
||||
}
|
||||
|
||||
return candidateIds
|
||||
.asSequence()
|
||||
.mapNotNull { documentId -> scoreDocument(documents[documentId] ?: return@mapNotNull null, normalizedQuery, queryTerms) }
|
||||
.filter { it.score > 0.0 }
|
||||
.sortedWith(compareByDescending<ImpressionSearchHit> { it.score }.thenBy { it.document.id })
|
||||
.take(limit)
|
||||
.toList()
|
||||
}
|
||||
|
||||
private fun upsertInternal(document: ImpressionSearchDocument) {
|
||||
val normalizedText = normalize(document.text)
|
||||
val terms = tokenizer.tokenize(normalizedText)
|
||||
val indexedDocument = IndexedDocument(document, normalizedText, terms)
|
||||
documents[document.id] = indexedDocument
|
||||
terms.forEach { term ->
|
||||
invertedIndex.getOrPut(term) { linkedSetOf() }.add(document.id)
|
||||
}
|
||||
}
|
||||
|
||||
private fun removeByDocumentId(documentId: String) {
|
||||
val indexedDocument = documents.remove(documentId) ?: return
|
||||
indexedDocument.terms.forEach { term ->
|
||||
val ids = invertedIndex[term] ?: return@forEach
|
||||
ids.remove(documentId)
|
||||
if (ids.isEmpty()) {
|
||||
invertedIndex.remove(term)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun scoreDocument(
|
||||
indexedDocument: IndexedDocument,
|
||||
normalizedQuery: String,
|
||||
queryTerms: Set<String>,
|
||||
): ImpressionSearchHit? {
|
||||
val matchedTerms = if (queryTerms.isEmpty()) {
|
||||
emptySet()
|
||||
} else {
|
||||
queryTerms.intersect(indexedDocument.terms)
|
||||
}
|
||||
val exactPhraseMatched = indexedDocument.normalizedText.contains(normalizedQuery)
|
||||
|
||||
if (matchedTerms.isEmpty() && !exactPhraseMatched) {
|
||||
return null
|
||||
}
|
||||
|
||||
val coverage = if (queryTerms.isEmpty()) 0.0 else matchedTerms.size.toDouble() / queryTerms.size.toDouble()
|
||||
val termScore = matchedTerms.size.toDouble()
|
||||
val exactPhraseBonus = if (exactPhraseMatched) EXACT_PHRASE_BONUS else 0.0
|
||||
val fieldBonus = fieldBonus(indexedDocument.document.field)
|
||||
val score = (termScore + coverage + exactPhraseBonus + fieldBonus) * indexedDocument.document.weight
|
||||
|
||||
return ImpressionSearchHit(
|
||||
document = indexedDocument.document,
|
||||
score = score,
|
||||
matchedTerms = matchedTerms,
|
||||
)
|
||||
}
|
||||
|
||||
private fun fieldBonus(field: ImpressionSearchField): Double = when (field) {
|
||||
ImpressionSearchField.SUBJECT -> 0.8
|
||||
ImpressionSearchField.FEATURE -> 0.35
|
||||
ImpressionSearchField.IMPRESSION -> 0.25
|
||||
ImpressionSearchField.RELATION -> 0.15
|
||||
ImpressionSearchField.EVIDENCE -> 0.0
|
||||
}
|
||||
|
||||
private fun normalize(text: String): String =
|
||||
text.lowercase()
|
||||
.replace(WHITESPACE_REGEX, " ")
|
||||
.trim()
|
||||
|
||||
private data class IndexedDocument(
|
||||
val document: ImpressionSearchDocument,
|
||||
val normalizedText: String,
|
||||
val terms: Set<String>,
|
||||
)
|
||||
|
||||
companion object {
|
||||
private const val EXACT_PHRASE_BONUS = 1.5
|
||||
private val WHITESPACE_REGEX = Regex("\\s+")
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
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.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.core.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.core.memory.pojo.MemoryUnit;
|
||||
|
||||
@@ -12,7 +12,11 @@ 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.*;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.CommunicationBlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextWorkspace;
|
||||
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;
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
|
||||
@@ -6,9 +6,9 @@ import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.action.entity.*;
|
||||
import work.slhaf.partner.core.action.entity.intervention.MetaIntervention;
|
||||
import work.slhaf.partner.core.cognition.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.ContextWorkspace;
|
||||
import work.slhaf.partner.core.cognition.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextWorkspace;
|
||||
import work.slhaf.partner.module.StateHintContent;
|
||||
import work.slhaf.partner.module.action.executor.entity.HistoryAction;
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
|
||||
@@ -8,9 +8,9 @@ import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.action.entity.*;
|
||||
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.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
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;
|
||||
|
||||
@@ -6,10 +6,10 @@ import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
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.ResolvedContext;
|
||||
import work.slhaf.partner.core.cognition.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ResolvedContext;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.Init;
|
||||
|
||||
@@ -2,7 +2,7 @@ package work.slhaf.partner.module.action.planner.extractor;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException;
|
||||
import work.slhaf.partner.framework.agent.exception.ModuleExecutionException;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
|
||||
@@ -3,8 +3,8 @@ package work.slhaf.partner.module
|
||||
import org.w3c.dom.Document
|
||||
import org.w3c.dom.Element
|
||||
import work.slhaf.partner.common.base.Block
|
||||
import work.slhaf.partner.core.cognition.CommunicationBlockContent
|
||||
import work.slhaf.partner.core.cognition.ContextBlock
|
||||
import work.slhaf.partner.core.cognition.context.CommunicationBlockContent
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message
|
||||
|
||||
abstract class TaskBlock @JvmOverloads constructor(
|
||||
|
||||
@@ -5,7 +5,11 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import work.slhaf.partner.core.cognition.*;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.CommunicationBlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ResolvedContext;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.Init;
|
||||
|
||||
@@ -9,9 +9,9 @@ import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.action.entity.Schedulable;
|
||||
import work.slhaf.partner.core.action.entity.StateAction;
|
||||
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.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.core.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.core.memory.pojo.MemoryUnit;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package work.slhaf.partner.module.impression;
|
||||
|
||||
import lombok.val;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.runtime.PartnerRunningFlowContext;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ImpressionRecaller extends AbstractAgentModule.Running<PartnerRunningFlowContext> {
|
||||
|
||||
@InjectCapability
|
||||
private CognitionCapability cognitionCapability;
|
||||
|
||||
/**
|
||||
* 从交互中积累谈论的内容的特征(证据),基于证据创建 ActiveEntity,然后交给 CognitionCapability 进行投影并更新上下文
|
||||
*/
|
||||
@Override
|
||||
protected void doExecute(@NotNull PartnerRunningFlowContext context) {
|
||||
val contextWorkspace = cognitionCapability.contextWorkspace();
|
||||
context.getInputs()
|
||||
.stream()
|
||||
.map(inputEntry -> {
|
||||
val content = inputEntry.getContent();
|
||||
return cognitionCapability.projectEntity(content);
|
||||
})
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toSet())
|
||||
.forEach(activeEntity -> {
|
||||
contextWorkspace.register(new ContextBlock(
|
||||
activeEntity,
|
||||
activeEntity,
|
||||
activeEntity,
|
||||
Set.of(
|
||||
ContextBlock.FocusedDomain.COGNITION,
|
||||
ContextBlock.FocusedDomain.MEMORY
|
||||
),
|
||||
100,
|
||||
0.5,
|
||||
20
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package work.slhaf.partner.module.impression;
|
||||
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.module.communication.AfterRolling;
|
||||
import work.slhaf.partner.module.communication.RollingResult;
|
||||
|
||||
public class ImpressionUpdater extends AbstractAgentModule.Standalone implements AfterRolling {
|
||||
|
||||
@InjectCapability
|
||||
private CognitionCapability cognitionCapability;
|
||||
|
||||
@Override
|
||||
public void consume(RollingResult result) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
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.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.InjectModule;
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.Init;
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.InjectModule;
|
||||
|
||||
@@ -3,10 +3,10 @@ package work.slhaf.partner.module.perceive;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.cognition.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.CommunicationBlockContent;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.CommunicationBlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
|
||||
@@ -4,9 +4,9 @@ import kotlin.Unit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
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.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.runtime.PartnerRunningFlowContext;
|
||||
|
||||
@@ -3,9 +3,9 @@ package work.slhaf.partner.runtime.exception;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
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.context.BlockContent;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.framework.agent.exception.AgentException;
|
||||
import work.slhaf.partner.framework.agent.exception.ExceptionReport;
|
||||
import work.slhaf.partner.framework.agent.exception.ExceptionReporter;
|
||||
|
||||
@@ -3,6 +3,8 @@ package work.slhaf.partner.core.cognition;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock;
|
||||
import work.slhaf.partner.core.cognition.context.ContextCore;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
|
||||
import java.nio.file.Path;
|
||||
@@ -20,15 +22,15 @@ class CognitionCoreTest {
|
||||
|
||||
@Test
|
||||
void shouldRenderRecentChatMessagesWithWrapperAndNotes() {
|
||||
CognitionCore cognitionCore = new CognitionCore();
|
||||
cognitionCore.getChatMessages().addAll(List.of(
|
||||
ContextCore contextCore = new ContextCore();
|
||||
contextCore.getChatMessages().addAll(List.of(
|
||||
new Message(Message.Character.USER, "[[USER]: user-1]: hello"),
|
||||
new Message(Message.Character.ASSISTANT, "[NOT_REPLIED]: wait"),
|
||||
new Message(Message.Character.ASSISTANT, "latest message")
|
||||
));
|
||||
|
||||
cognitionCore.refreshRecentChatMessagesContext();
|
||||
String content = cognitionCore.contextWorkspace()
|
||||
contextCore.refreshRecentChatMessagesContext();
|
||||
String content = contextCore.contextWorkspace()
|
||||
.resolve(List.of(ContextBlock.FocusedDomain.COMMUNICATION))
|
||||
.encodeToMessage()
|
||||
.getContent();
|
||||
|
||||
@@ -2,6 +2,9 @@ package work.slhaf.partner.core.cognition
|
||||
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.Test
|
||||
import work.slhaf.partner.core.cognition.context.BlockContent
|
||||
import work.slhaf.partner.core.cognition.context.ContextBlock
|
||||
import work.slhaf.partner.core.cognition.context.ContextWorkspace
|
||||
|
||||
class ContextWorkspaceTest {
|
||||
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
package work.slhaf.partner.core.cognition.impression.search
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import work.slhaf.partner.core.cognition.impression.ActiveEntity
|
||||
import work.slhaf.partner.core.cognition.impression.Entity
|
||||
|
||||
class SimpleTextSearchTest {
|
||||
|
||||
@Test
|
||||
fun `search ranks subject hit before evidence hit when both match similar terms`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val targetA = activeTarget("a")
|
||||
val targetB = activeTarget("b")
|
||||
|
||||
search.rebuild(
|
||||
listOf(
|
||||
document("a-subject", targetA, ImpressionSearchField.SUBJECT, "城南旧书店老板", 1.0),
|
||||
document("b-evidence", targetB, ImpressionSearchField.EVIDENCE, "用户提到城南旧书店附近有一家打印店", 0.8),
|
||||
)
|
||||
)
|
||||
|
||||
val hits = search.search("城南旧书店", limit = 10)
|
||||
|
||||
assertEquals(listOf("a-subject", "b-evidence"), hits.map { it.document.id })
|
||||
assertTrue(hits.first().score > hits[1].score)
|
||||
assertTrue(hits.first().matchedTerms.containsAll(setOf("城南", "旧书店")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `exact phrase match can beat partial subject match`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val partialSubject = activeTarget("partial")
|
||||
val exactEvidence = activeTarget("exact")
|
||||
|
||||
search.rebuild(
|
||||
listOf(
|
||||
document("partial-subject", partialSubject, ImpressionSearchField.SUBJECT, "工程教材", 1.0),
|
||||
document("exact-evidence", exactEvidence, ImpressionSearchField.EVIDENCE, "旧书店老板推荐过工程教材", 0.8),
|
||||
)
|
||||
)
|
||||
|
||||
val hits = search.search("旧书店老板推荐过工程教材", limit = 10)
|
||||
|
||||
assertEquals("exact-evidence", hits.first().document.id)
|
||||
assertTrue(hits.first().matchedTerms.containsAll(setOf("旧书店", "老板", "推荐", "工程", "教材")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `search recalls bookstore owner from generated active entity documents`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val bookstoreOwner = activeEntity("bookstore", "城南旧书店老板") {
|
||||
addEvidence("用户上周提到城南旧书店老板推荐过一本水利工程教材")
|
||||
addProjectedFeatures("熟悉工程类旧书" to 0.9)
|
||||
}
|
||||
val technicalPartner = activeEntity("technical", "Java 技术搭子") {
|
||||
addEvidence("用户正在讨论 Jieba 分词、SimpleTextSearch 和倒排索引")
|
||||
addProjectedFeatures("熟悉 Kotlin 与检索实现" to 0.9)
|
||||
}
|
||||
val reportRoommate = activeEntity("report", "实验报告室友") {
|
||||
addEvidence("用户帮室友整理 Vivado 进阶仿真实验报告模板和 docx 文件")
|
||||
}
|
||||
|
||||
search.rebuild(
|
||||
listOf(bookstoreOwner, technicalPartner, reportRoommate)
|
||||
.flatMap(ImpressionSearchDocuments::fromActiveEntity)
|
||||
)
|
||||
|
||||
val hits = search.search("旧书店老板推荐的工程教材", limit = 10)
|
||||
|
||||
assertFalse(hits.isEmpty())
|
||||
assertEquals("bookstore", hits.first().document.target.id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `search recalls technical active entity from implementation terms`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val technicalPartner = activeEntity("technical", "Java 技术搭子") {
|
||||
addEvidence("用户正在讨论 Jieba 分词、SimpleTextSearch 和倒排索引")
|
||||
addProjectedImpressions("需要补充搜索召回测试" to 0.8)
|
||||
}
|
||||
val reportRoommate = activeEntity("report", "实验报告室友") {
|
||||
addEvidence("用户帮室友整理 Vivado 进阶仿真实验报告模板和 docx 文件")
|
||||
}
|
||||
|
||||
search.rebuild(
|
||||
listOf(technicalPartner, reportRoommate)
|
||||
.flatMap(ImpressionSearchDocuments::fromActiveEntity)
|
||||
)
|
||||
|
||||
val hits = search.search("jieba 分词 SimpleTextSearch 倒排索引", limit = 10)
|
||||
|
||||
assertFalse(hits.isEmpty())
|
||||
assertEquals("technical", hits.first().document.target.id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `search recalls report active entity from document task terms`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val technicalPartner = activeEntity("technical", "Java 技术搭子") {
|
||||
addEvidence("用户正在讨论 Kotlin、Jieba 分词和 SimpleTextSearch")
|
||||
}
|
||||
val reportRoommate = activeEntity("report", "实验报告室友") {
|
||||
addEvidence("用户帮室友整理 Vivado 进阶仿真实验报告模板和 docx 文件")
|
||||
}
|
||||
|
||||
search.rebuild(
|
||||
listOf(technicalPartner, reportRoommate)
|
||||
.flatMap(ImpressionSearchDocuments::fromActiveEntity)
|
||||
)
|
||||
|
||||
val hits = search.search("Vivado 实验报告模板", limit = 10)
|
||||
|
||||
assertFalse(hits.isEmpty())
|
||||
assertEquals("report", hits.first().document.target.id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `search recalls known entity by alias documents`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val entity = Entity("entity-1", "Partner")
|
||||
entity.addAlias("智能体项目")
|
||||
|
||||
search.rebuild(ImpressionSearchDocuments.fromEntity(entity))
|
||||
|
||||
val hits = search.search("智能体项目", limit = 10)
|
||||
|
||||
assertFalse(hits.isEmpty())
|
||||
assertEquals(ImpressionSearchTarget.Type.ENTITY, hits.first().document.target.type)
|
||||
assertEquals("entity-1", hits.first().document.target.id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `upsert replaces previous index terms for the same document id`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val target = activeTarget("entity")
|
||||
|
||||
search.upsert(document("doc", target, ImpressionSearchField.EVIDENCE, "旧书店老板", 1.0))
|
||||
assertEquals(listOf("doc"), search.search("老板", limit = 10).map { it.document.id })
|
||||
|
||||
search.upsert(document("doc", target, ImpressionSearchField.EVIDENCE, "实验报告模板", 1.0))
|
||||
|
||||
assertTrue(search.search("老板", limit = 10).isEmpty())
|
||||
assertEquals(listOf("doc"), search.search("实验报告", limit = 10).map { it.document.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `removeByTarget removes all documents belonging to that target`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val removed = activeTarget("removed")
|
||||
val kept = activeTarget("kept")
|
||||
|
||||
search.rebuild(
|
||||
listOf(
|
||||
document("removed-subject", removed, ImpressionSearchField.SUBJECT, "旧书店老板", 1.0),
|
||||
document("removed-evidence", removed, ImpressionSearchField.EVIDENCE, "工程教材", 0.8),
|
||||
document("kept-evidence", kept, ImpressionSearchField.EVIDENCE, "实验报告模板", 0.8),
|
||||
)
|
||||
)
|
||||
|
||||
search.removeByTarget(removed)
|
||||
|
||||
val hits = search.search("实验报告", limit = 10)
|
||||
assertEquals(listOf("kept-evidence"), hits.map { it.document.id })
|
||||
assertFalse(hits.any { it.document.target == removed })
|
||||
assertTrue(search.search("旧书店", limit = 10).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rebuild clears previous documents and index terms`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val target = activeTarget("entity")
|
||||
|
||||
search.rebuild(listOf(document("old", target, ImpressionSearchField.SUBJECT, "旧书店老板", 1.0)))
|
||||
assertEquals(listOf("old"), search.search("老板", limit = 10).map { it.document.id })
|
||||
|
||||
search.rebuild(listOf(document("new", target, ImpressionSearchField.SUBJECT, "实验报告模板", 1.0)))
|
||||
|
||||
assertTrue(search.search("老板", limit = 10).isEmpty())
|
||||
assertEquals(listOf("new"), search.search("实验报告", limit = 10).map { it.document.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `blank unmatched and zero limit queries return empty hits`() {
|
||||
val search = SimpleTextSearch(TestTokenizer())
|
||||
val target = activeTarget("entity")
|
||||
search.rebuild(listOf(document("doc", target, ImpressionSearchField.SUBJECT, "旧书店老板", 1.0)))
|
||||
|
||||
assertTrue(search.search(" ", limit = 10).isEmpty())
|
||||
assertTrue(search.search("完全不存在", limit = 10).isEmpty())
|
||||
assertTrue(search.search("旧书店", limit = 0).isEmpty())
|
||||
}
|
||||
|
||||
private fun activeTarget(id: String) =
|
||||
ImpressionSearchTarget(ImpressionSearchTarget.Type.ACTIVE_ENTITY, id)
|
||||
|
||||
private fun activeEntity(
|
||||
runtimeId: String,
|
||||
subject: String,
|
||||
configure: ActiveEntity.() -> Unit,
|
||||
): ActiveEntity = ActiveEntity(runtimeId = runtimeId).apply {
|
||||
updateSubject(subject)
|
||||
configure()
|
||||
}
|
||||
|
||||
private fun document(
|
||||
id: String,
|
||||
target: ImpressionSearchTarget,
|
||||
field: ImpressionSearchField,
|
||||
text: String,
|
||||
weight: Double,
|
||||
) = ImpressionSearchDocument(
|
||||
id = id,
|
||||
target = target,
|
||||
field = field,
|
||||
text = text,
|
||||
weight = weight,
|
||||
)
|
||||
|
||||
private class TestTokenizer : ImpressionTokenizer {
|
||||
private val dictionary = listOf(
|
||||
"城南", "旧书店", "老板", "推荐", "工程", "教材", "水利", "熟悉", "旧书",
|
||||
"java", "kotlin", "jieba", "分词", "simpletextsearch", "倒排", "索引", "检索", "测试", "召回",
|
||||
"vivado", "实验报告", "实验", "报告", "模板", "docx", "室友", "整理", "文件",
|
||||
"智能体", "项目", "智能体项目"
|
||||
)
|
||||
private val alphaNumericRegex = Regex("[a-z0-9]+(?:[-_./][a-z0-9]+)*")
|
||||
|
||||
override fun tokenize(text: String): Set<String> {
|
||||
val normalized = text.lowercase().trim()
|
||||
if (normalized.isBlank()) {
|
||||
return emptySet()
|
||||
}
|
||||
|
||||
return buildSet {
|
||||
dictionary.filterTo(this) { normalized.contains(it) }
|
||||
alphaNumericRegex.findAll(normalized).mapTo(this) { it.value }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.action.entity.*;
|
||||
import work.slhaf.partner.core.action.runner.RunnerClient;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextWorkspace;
|
||||
import work.slhaf.partner.core.cognition.context.ContextWorkspace;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
import work.slhaf.partner.module.action.executor.entity.ExtractorResult;
|
||||
import work.slhaf.partner.module.action.executor.entity.HistoryAction;
|
||||
|
||||
@@ -3,7 +3,7 @@ package work.slhaf.partner.module.communication;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextWorkspace;
|
||||
import work.slhaf.partner.core.cognition.context.ContextWorkspace;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
import work.slhaf.partner.runtime.PartnerRunningFlowContext;
|
||||
|
||||
@@ -145,5 +145,58 @@ class CommunicationProducerTest {
|
||||
public Lock getMessageLock() {
|
||||
return lock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.util.Set<work.slhaf.partner.core.cognition.impression.ActiveEntity> projectEntity(String input) {
|
||||
return java.util.Set.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.util.Map<
|
||||
work.slhaf.partner.core.cognition.impression.ActiveEntity,
|
||||
work.slhaf.partner.core.cognition.impression.Entity
|
||||
> showEntities() {
|
||||
return java.util.Map.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public work.slhaf.partner.core.cognition.impression.Entity createEntity(String subject) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public work.slhaf.partner.core.cognition.impression.Entity getEntity(String uuid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean bindActiveEntity(String runtimeId, String entityUuid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean renameEntitySubject(String entityUuid, String newSubject) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addEntityAlias(String entityUuid, String alias, boolean deprecated) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEntityImpression(String entityUuid, String impression, String newImpression, double confidence) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEntityFeature(String entityUuid, String feature, String newFeature, double confidence) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEntityRelation(String entityUuid, String target, String relation, double strength) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.context.ContextWorkspace;
|
||||
import work.slhaf.partner.core.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.core.memory.pojo.MemoryUnit;
|
||||
@@ -65,8 +66,8 @@ class MemoryRuntimeTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public work.slhaf.partner.core.cognition.ContextWorkspace contextWorkspace() {
|
||||
return new work.slhaf.partner.core.cognition.ContextWorkspace();
|
||||
public ContextWorkspace contextWorkspace() {
|
||||
return new ContextWorkspace();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,6 +98,59 @@ class MemoryRuntimeTest {
|
||||
public Lock getMessageLock() {
|
||||
return lock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.util.Set<work.slhaf.partner.core.cognition.impression.ActiveEntity> projectEntity(String input) {
|
||||
return java.util.Set.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.util.Map<
|
||||
work.slhaf.partner.core.cognition.impression.ActiveEntity,
|
||||
work.slhaf.partner.core.cognition.impression.Entity
|
||||
> showEntities() {
|
||||
return java.util.Map.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public work.slhaf.partner.core.cognition.impression.Entity createEntity(String subject) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public work.slhaf.partner.core.cognition.impression.Entity getEntity(String uuid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean bindActiveEntity(String runtimeId, String entityUuid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean renameEntitySubject(String entityUuid, String newSubject) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addEntityAlias(String entityUuid, String alias, boolean deprecated) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEntityImpression(String entityUuid, String impression, String newImpression, double confidence) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEntityFeature(String entityUuid, String feature, String newFeature, double confidence) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEntityRelation(String entityUuid, String target, String relation, double strength) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,22 +6,23 @@
|
||||
<parent>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner-external-modules</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>partner-onebot-adapter</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<version>${partner.runtime.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner-framework</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<version>${partner.runtime.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
<parent>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>partner-external-modules</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
<parent>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>partner-framework</artifactId>
|
||||
<version>0.9.0-preview</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -86,7 +87,7 @@
|
||||
<dependency>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner-interaction-api</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>${partner.interaction-api.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
<parent>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>partner-interaction-api</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
<parent>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>partnerctl</artifactId>
|
||||
<version>1.0.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
@@ -44,7 +45,7 @@
|
||||
<dependency>
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner-interaction-api</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>${partner.interaction-api.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@@ -122,6 +123,7 @@
|
||||
<buildArg>-H:+ReportExceptionStackTraces</buildArg>
|
||||
<buildArg>--initialize-at-build-time=kotlin.DeprecationLevel</buildArg>
|
||||
<buildArg>-H:IncludeResourceBundles=i18n.messages</buildArg>
|
||||
<buildArg>-H:IncludeLocales=zh-CN</buildArg>
|
||||
</buildArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@@ -2,13 +2,12 @@ package work.slhaf.partner.ctl.commands
|
||||
|
||||
import kotlinx.serialization.json.*
|
||||
import picocli.CommandLine
|
||||
import work.slhaf.partner.ctl.commands.InitCommand.InstallChoice.BUILD_FROM_SOURCE
|
||||
import work.slhaf.partner.ctl.commands.InitCommand.InstallChoice.DOWNLOAD_JAR
|
||||
import work.slhaf.partner.ctl.commands.data.GatewayConfig
|
||||
import work.slhaf.partner.ctl.commands.data.OpenAiCompatible
|
||||
import work.slhaf.partner.ctl.commands.data.ProviderConfig
|
||||
import work.slhaf.partner.ctl.commands.init.buildFromSource
|
||||
import work.slhaf.partner.ctl.commands.init.configureExternalGateway
|
||||
import work.slhaf.partner.ctl.commands.init.configureOpenAiCompatible
|
||||
import work.slhaf.partner.ctl.commands.init.configureWebSocketGateway
|
||||
import work.slhaf.partner.ctl.commands.init.*
|
||||
import work.slhaf.partner.ctl.i18n.I18n.text
|
||||
import work.slhaf.partner.ctl.support.CommandInterrupted
|
||||
import work.slhaf.partner.ctl.support.inheritCommand
|
||||
@@ -176,11 +175,15 @@ class InitCommand : Runnable {
|
||||
|
||||
val installChoice = prompt.select(
|
||||
label = text("init.install.method.label"),
|
||||
choices = listOf(Choice(text("init.install.method.buildFromSource"), InstallChoice.BUILD_FROM_SOURCE))
|
||||
choices = listOf(
|
||||
Choice(text("init.install.method.buildFromSource"), BUILD_FROM_SOURCE),
|
||||
Choice(text("init.install.method.downloadFromRelease"), DOWNLOAD_JAR)
|
||||
)
|
||||
)
|
||||
|
||||
when (installChoice) {
|
||||
InstallChoice.BUILD_FROM_SOURCE -> buildFromSource(home, prompt)
|
||||
BUILD_FROM_SOURCE -> buildFromSource(home, prompt)
|
||||
DOWNLOAD_JAR -> downloadFromRelease(home, prompt)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -348,7 +351,8 @@ class InitCommand : Runnable {
|
||||
}
|
||||
|
||||
private enum class InstallChoice {
|
||||
BUILD_FROM_SOURCE
|
||||
BUILD_FROM_SOURCE,
|
||||
DOWNLOAD_JAR
|
||||
}
|
||||
|
||||
private enum class ModelProviderChoice(val display: String) {
|
||||
|
||||
@@ -64,7 +64,8 @@ fun configureExternalGateway(home: Path, prompt: Prompt, manifest: ModuleManifes
|
||||
text("configure.gateway.external.details.buildCommand") to manifest.source.buildCommand.joinToString(" "),
|
||||
text("configure.gateway.external.details.artifact") to "${manifest.source.artifactDirectory}/${manifest.source.artifactPattern}",
|
||||
text("configure.gateway.external.details.installTarget") to manifest.install.target,
|
||||
text("configure.gateway.external.details.configTarget") to (manifest.config?.target ?: text("configure.gateway.external.details.noConfig")),
|
||||
text("configure.gateway.external.details.configTarget") to (manifest.config?.target
|
||||
?: text("configure.gateway.external.details.noConfig")),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -138,18 +139,43 @@ private fun askField(prompt: Prompt, field: Field): JsonElement? {
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("KotlinConstantConditions")
|
||||
private fun validateFieldValue(field: Field, value: String): String? {
|
||||
if (value.isBlank() && !field.required) return null
|
||||
|
||||
return when (field.type) {
|
||||
FieldType.STRING -> null
|
||||
FieldType.INT -> value.toIntOrNull()?.let { null } ?: text("configure.field.error.int", field.label)
|
||||
FieldType.NUMBER -> value.toDoubleOrNull()?.let { null } ?: text("configure.field.error.number", field.label)
|
||||
FieldType.BOOLEAN -> value.toBooleanStrictOrNull()?.let { null } ?: text("configure.field.error.boolean", field.label)
|
||||
FieldType.RAW_JSON -> runCatching { Json.parseToJsonElement(value) }
|
||||
.exceptionOrNull()
|
||||
?.let { text("configure.field.error.rawJson", field.label) }
|
||||
FieldType.INT -> {
|
||||
if (value.toIntOrNull() == null) {
|
||||
text("configure.field.error.int", field.label)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
FieldType.NUMBER -> {
|
||||
if (value.toDoubleOrNull() == null) {
|
||||
text("configure.field.error.number", field.label)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
FieldType.BOOLEAN -> {
|
||||
if (value.toBooleanStrictOrNull() == null) {
|
||||
text("configure.field.error.boolean", field.label)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
FieldType.RAW_JSON -> {
|
||||
val result = runCatching { Json.parseToJsonElement(value) }.exceptionOrNull()
|
||||
if (result == null) {
|
||||
text("configure.field.error.rawJson", field.label)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
package work.slhaf.partner.ctl.commands.init
|
||||
|
||||
import work.slhaf.partner.ctl.i18n.I18n.text
|
||||
import work.slhaf.partner.ctl.support.SourceBuildInstallSpec
|
||||
import work.slhaf.partner.ctl.support.buildAndInstallFromSource
|
||||
import work.slhaf.partner.ctl.support.downloadTo
|
||||
import work.slhaf.partner.ctl.support.registryIndex
|
||||
import work.slhaf.partner.ctl.ui.Prompt
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import kotlin.io.path.exists
|
||||
import kotlin.io.path.isDirectory
|
||||
import kotlin.io.path.name
|
||||
|
||||
private const val PARTNER_REPO_URL = "https://gitea.slhaf.work/slhaf/Partner.git"
|
||||
private const val PARTNER_REPO_URL = "https://github.com/slhaf/Partner.git"
|
||||
|
||||
fun buildFromSource(home: Path, prompt: Prompt) {
|
||||
buildAndInstallFromSource(
|
||||
@@ -40,3 +44,41 @@ private fun findLargestJar(directory: Path): Path? {
|
||||
.orElse(null)
|
||||
}
|
||||
}
|
||||
|
||||
fun downloadFromRelease(home: Path, prompt: Prompt) {
|
||||
prompt.info(text("init.install.method.downloadFromRelease.startDownloading"))
|
||||
val path = home.resolve("resources/partner-core.jar").toAbsolutePath().normalize()
|
||||
downloadTo(registryIndex.partner.latestRelease.url, path) { downloaded, total ->
|
||||
if (total != null && total > 0) {
|
||||
val percent = downloaded * 100 / total
|
||||
updateLine(
|
||||
text(
|
||||
"init.install.method.downloadFromRelease.progress.percent",
|
||||
percent
|
||||
)
|
||||
)
|
||||
} else {
|
||||
updateLine(
|
||||
text(
|
||||
"init.install.method.downloadFromRelease.progress.size",
|
||||
downloaded / 1024
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
finishLine(text("init.install.method.downloadFromRelease.done"))
|
||||
if (!path.exists()) {
|
||||
throw IllegalStateException("Unable to find downloaded partner release at $path")
|
||||
}
|
||||
prompt.success(text("init.install.method.downloadFromRelease.success"))
|
||||
}
|
||||
|
||||
fun updateLine(text: String) {
|
||||
print("\r\u001B[2K$text")
|
||||
System.out.flush()
|
||||
}
|
||||
|
||||
fun finishLine(text: String) {
|
||||
updateLine(text)
|
||||
println()
|
||||
}
|
||||
@@ -1,10 +1,20 @@
|
||||
package work.slhaf.partner.ctl.support
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
private const val registryUrl = "https://raw.githubusercontent.com/slhaf/Partner/refs/heads/master/registry"
|
||||
private const val indexUrl = "$registryUrl/index.json"
|
||||
|
||||
val registryIndex = run {
|
||||
Json.decodeFromString<RegistryIndex>(fetchText(indexUrl))
|
||||
}
|
||||
|
||||
private fun loadModules(): Set<ModuleManifest> {
|
||||
// TODO: 待实现具体加载逻辑
|
||||
return emptySet()
|
||||
return registryIndex.externalModules.map { indexItem ->
|
||||
val manifestStr = fetchText("$registryUrl/${indexItem.registryRef}")
|
||||
return@map Json.decodeFromString<ModuleManifest>(manifestStr)
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
fun loadAvailableGateway(): Set<ModuleManifest> {
|
||||
@@ -91,4 +101,36 @@ enum class FieldType {
|
||||
NUMBER,
|
||||
BOOLEAN,
|
||||
RAW_JSON,
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class RegistryIndex(
|
||||
val partner: PartnerIndex,
|
||||
val externalModules: List<ModulesIndexItem>
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class PartnerIndex(
|
||||
val latestBuildable: Buildable,
|
||||
val latestRelease: Release
|
||||
) {
|
||||
@Serializable
|
||||
data class Buildable(
|
||||
val url: String,
|
||||
val ref: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Release(
|
||||
val url: String,
|
||||
val version: String
|
||||
)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class ModulesIndexItem(
|
||||
val name: String,
|
||||
val version: String,
|
||||
val withGateway: Boolean,
|
||||
val registryRef: String
|
||||
)
|
||||
@@ -0,0 +1,135 @@
|
||||
package work.slhaf.partner.ctl.support
|
||||
|
||||
import java.io.IOException
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.ProxySelector
|
||||
import java.net.URI
|
||||
import java.net.http.*
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.StandardCopyOption
|
||||
import java.time.Duration
|
||||
import kotlin.io.path.isDirectory
|
||||
|
||||
private val httpClient: HttpClient = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.apply {
|
||||
proxySelectorFromEnv()?.let(::proxy)
|
||||
}
|
||||
.build()
|
||||
|
||||
private fun proxySelectorFromEnv(): ProxySelector? {
|
||||
val proxyText = System.getenv("HTTPS_PROXY")
|
||||
?: System.getenv("https_proxy")
|
||||
?: return null
|
||||
|
||||
val proxyUri = URI.create(proxyText)
|
||||
val host = proxyUri.host
|
||||
?: throw IllegalArgumentException("Invalid HTTPS_PROXY host: $proxyText")
|
||||
|
||||
val port = proxyUri.port
|
||||
if (port == -1) {
|
||||
throw IllegalArgumentException("HTTPS_PROXY must include port: $proxyText")
|
||||
}
|
||||
|
||||
return ProxySelector.of(InetSocketAddress(host, port))
|
||||
}
|
||||
|
||||
fun fetchText(url: String): String {
|
||||
var lastError: Exception? = null
|
||||
|
||||
repeat(3) { attempt ->
|
||||
try {
|
||||
val request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.timeout(Duration.ofSeconds(60))
|
||||
.header("User-Agent", "partnerctl")
|
||||
.GET()
|
||||
.build()
|
||||
|
||||
val response = httpClient.send(
|
||||
request,
|
||||
HttpResponse.BodyHandlers.ofString()
|
||||
)
|
||||
|
||||
if (response.statusCode() !in 200..299) {
|
||||
throw IOException("Failed to fetch $url: HTTP ${response.statusCode()}")
|
||||
}
|
||||
|
||||
return response.body()
|
||||
} catch (e: HttpTimeoutException) {
|
||||
lastError = e
|
||||
} catch (e: HttpConnectTimeoutException) {
|
||||
lastError = e
|
||||
} catch (e: IOException) {
|
||||
lastError = e
|
||||
} catch (e: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
throw IOException("Interrupted while fetching $url", e)
|
||||
}
|
||||
|
||||
if (attempt < 2) {
|
||||
Thread.sleep(500L * (attempt + 1))
|
||||
}
|
||||
}
|
||||
|
||||
throw IOException("Failed to fetch $url after retries", lastError)
|
||||
}
|
||||
|
||||
fun downloadTo(
|
||||
url: String,
|
||||
targetPath: Path,
|
||||
onProgress: (downloaded: Long, total: Long?) -> Unit = { _, _ -> }
|
||||
) {
|
||||
if (targetPath.isDirectory()) {
|
||||
throw IllegalArgumentException("Target path must be a file")
|
||||
}
|
||||
val targetPath = targetPath.toAbsolutePath().normalize()
|
||||
val targetFile = targetPath.toFile()
|
||||
val temp = Files.createTempFile(
|
||||
"${targetFile.name}-${System.currentTimeMillis()}", ".${targetFile.extension}.download"
|
||||
)
|
||||
|
||||
try {
|
||||
val request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.GET()
|
||||
.build()
|
||||
|
||||
val response = httpClient.send(
|
||||
request,
|
||||
HttpResponse.BodyHandlers.ofInputStream()
|
||||
)
|
||||
|
||||
if (response.statusCode() !in 200..299) {
|
||||
throw IllegalStateException("Failed to download from $url: HTTP ${response.statusCode()}")
|
||||
}
|
||||
|
||||
val totalBytes = response.headers()
|
||||
.firstValue("Content-Length")
|
||||
.orElse(null)
|
||||
?.toLongOrNull()
|
||||
|
||||
response.body().use { input ->
|
||||
Files.newOutputStream(temp).use { output ->
|
||||
val buffer = ByteArray(8192)
|
||||
var downloaded = 0L
|
||||
|
||||
while (true) {
|
||||
val read = input.read(buffer)
|
||||
if (read < 0) break
|
||||
|
||||
output.write(buffer, 0, read)
|
||||
downloaded += read
|
||||
onProgress(downloaded, totalBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Files.move(temp, targetPath, StandardCopyOption.REPLACE_EXISTING)
|
||||
} catch (e: Exception) {
|
||||
Files.deleteIfExists(temp)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,12 @@ init.home.overwrite.refuseBroadDirectory=Refuse to overwrite suspiciously broad
|
||||
init.install.section=Install Partner
|
||||
init.install.method.label=Choose an installation method
|
||||
init.install.method.buildFromSource=Build Partner from source
|
||||
init.install.method.downloadFromRelease=Download Partner release
|
||||
init.install.method.downloadFromRelease.startDownloading=Downloading Partner release...
|
||||
init.install.method.downloadFromRelease.success=Partner release downloaded successfully.
|
||||
init.install.method.downloadFromRelease.progress.percent=Downloading Partner release... {0}%
|
||||
init.install.method.downloadFromRelease.progress.size=Downloading Partner release... {0} KB
|
||||
init.install.method.downloadFromRelease.done=Downloading Partner release... Done
|
||||
init.gateway.section=Configure Gateway
|
||||
init.gateway.select.label=Select gateway
|
||||
init.gateway.websocket.choice=WebSocket Gateway
|
||||
|
||||
@@ -31,6 +31,12 @@ init.home.overwrite.refuseBroadDirectory=拒绝覆盖范围过大的目录:{0}
|
||||
init.install.section=安装 Partner
|
||||
init.install.method.label=选择安装方式
|
||||
init.install.method.buildFromSource=从源码构建 Partner
|
||||
init.install.method.downloadFromRelease=下载 Partner 发布包
|
||||
init.install.method.downloadFromRelease.startDownloading=正在下载 Partner 发布包...
|
||||
init.install.method.downloadFromRelease.success=Partner 发布包下载完成。
|
||||
init.install.method.downloadFromRelease.progress.percent=正在下载 Partner 发布包... {0}%
|
||||
init.install.method.downloadFromRelease.progress.size=正在下载 Partner 发布包... {0} KB
|
||||
init.install.method.downloadFromRelease.done=正在下载 Partner 发布包... 完成
|
||||
init.gateway.section=配置网关
|
||||
init.gateway.select.label=选择网关
|
||||
init.gateway.websocket.choice=WebSocket Gateway
|
||||
|
||||
12
PartnerCtl/src/test/java/experimental/WebFetchTest.kt
Normal file
12
PartnerCtl/src/test/java/experimental/WebFetchTest.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package experimental
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
import work.slhaf.partner.ctl.support.RegistryIndex
|
||||
import work.slhaf.partner.ctl.support.fetchText
|
||||
|
||||
|
||||
fun main() {
|
||||
val str = fetchText("https://raw.githubusercontent.com/slhaf/Partner/refs/heads/master/registry/index.json")
|
||||
val index = Json.decodeFromString<RegistryIndex>(str)
|
||||
println(index)
|
||||
}
|
||||
51
README.md
51
README.md
@@ -25,12 +25,54 @@ Partner 分为 `Partner-Framework` 与 `Partner-Core` 两层。前者提供配
|
||||
|
||||
## 项目启动
|
||||
|
||||
**环境要求**
|
||||
### 环境要求
|
||||
|
||||
**基础运行要求**
|
||||
|
||||
- JDK 21
|
||||
- Maven 3.x
|
||||
|
||||
### 手动准备环境并启动
|
||||
**仅在从源码构建 Partner Runtime 或外部模块时需要**
|
||||
|
||||
- Maven 3.x
|
||||
- Git
|
||||
|
||||
### 推荐方式:PartnerCtl
|
||||
|
||||
`PartnerCtl` 用于完成 Partner 的首次初始化、运行时安装与启动管理。相比手动准备运行目录和配置文件,使用它可以更快完成最小可运行环境的搭建。
|
||||
|
||||
#### 初始化
|
||||
|
||||
```bash
|
||||
partnerctl init
|
||||
```
|
||||
|
||||
初始化流程会引导完成:
|
||||
|
||||
- 选择 `PARTNER_HOME`
|
||||
- 安装 Partner Runtime
|
||||
- 从源码构建
|
||||
- 下载发布版 jar
|
||||
- 配置 Gateway
|
||||
- 配置模型 Provider
|
||||
- 可选立即启动 Partner
|
||||
|
||||
#### 启动
|
||||
|
||||
如果初始化完成后未选择立即启动,可执行:
|
||||
|
||||
```bash
|
||||
partnerctl run
|
||||
```
|
||||
|
||||
如需后台运行:
|
||||
|
||||
```bash
|
||||
partnerctl run -d
|
||||
```
|
||||
|
||||
PartnerCtl 默认读取 `PARTNER_HOME` 指定的运行目录;若未设置,则使用 `~/.partner`。
|
||||
|
||||
### 手动方式:从源码构建并启动
|
||||
|
||||
#### 克隆项目并构建
|
||||
|
||||
@@ -161,5 +203,4 @@ Partner/
|
||||
|
||||
## License
|
||||
|
||||
暂未指定。
|
||||
|
||||
暂未选择开源许可证。当前仓库主要作为个人项目展示与学习研究记录,未经授权不建议复制、分发或商用。
|
||||
281
doc/design/first-encounter-module.md
Normal file
281
doc/design/first-encounter-module.md
Normal file
@@ -0,0 +1,281 @@
|
||||
# First Encounter Module / 初见模块设计草案
|
||||
|
||||
## 背景
|
||||
|
||||
Partner 当前已经不是“不能跑”的项目,但用户面对一个新的 agent 时,仍然会有明显的启动成本。
|
||||
|
||||
这个启动成本不完全来自工程状态,而来自互动预期的不确定:
|
||||
|
||||
- 不知道该怎么和它说话;
|
||||
- 不知道它知道什么、不知道什么;
|
||||
- 不知道它会不会误解用户;
|
||||
- 不知道它能不能被纠正;
|
||||
- 不知道纠正之后会不会真正改变后续行为。
|
||||
|
||||
因此,Partner 需要一个“初见模块”。
|
||||
|
||||
它解决的不是程序启动问题,而是关系和预期建立问题。
|
||||
|
||||
## 定位
|
||||
|
||||
初见模块不应该只是 `InitModule`。
|
||||
|
||||
`InitModule` 更像加载配置、初始化资源、检查运行状态;而初见模块面对的是用户第一次或重新面对 Partner 时的交互问题。
|
||||
|
||||
因此,代码层可以命名为:
|
||||
|
||||
```text
|
||||
FirstEncounterModule
|
||||
```
|
||||
|
||||
产品/概念层称为:
|
||||
|
||||
```text
|
||||
初见模块
|
||||
```
|
||||
|
||||
它的职责是:
|
||||
|
||||
> 在新用户、长时间未使用、上下文断裂、版本升级,或用户主动询问“你现在知道我什么”时,组织一次清醒、温和、可校准的开场。
|
||||
|
||||
## 与 Impression 模块的关系
|
||||
|
||||
初见模块应当依托 Impression,但不属于 ImpressionCore。
|
||||
|
||||
边界如下:
|
||||
|
||||
```text
|
||||
ImpressionCore
|
||||
负责存储、召回、更新关于用户、agent 自身、关系契约、项目上下文等印象。
|
||||
|
||||
FirstEncounterModule
|
||||
负责判断是否进入初见/重逢模式,并将召回的印象组织成本轮对话可用的 EncounterFrame。
|
||||
|
||||
EncounterState
|
||||
负责记录初见流程是否已经完成,以及哪些环节已经向用户公开。
|
||||
```
|
||||
|
||||
也就是说:
|
||||
|
||||
> Impression 负责“我对你有什么印象”。
|
||||
> FirstEncounterModule 负责“第一次见面时,我该如何使用这些印象”。
|
||||
|
||||
不应把开场策略、纠错协议、对话引导逻辑直接塞进 ImpressionCore,否则记忆模块会被迫承担表达和流程控制职责。
|
||||
|
||||
## 触发场景
|
||||
|
||||
初见模块可以在以下场景触发:
|
||||
|
||||
- 新用户第一次进入;
|
||||
- 当前 session 没有足够上下文;
|
||||
- 长时间未使用后重新进入;
|
||||
- Partner 发生较大版本升级;
|
||||
- Impression 召回结果置信度较低;
|
||||
- 用户主动询问:
|
||||
- “你知道我什么?”
|
||||
- “你现在能做什么?”
|
||||
- “我该怎么和你说话?”
|
||||
- “你是不是还记得之前的事?”
|
||||
- 系统检测到当前对话存在明显预期不稳定,例如用户多次纠正 agent 的语气、事实或任务边界。
|
||||
|
||||
## 核心流程
|
||||
|
||||
推荐流程:
|
||||
|
||||
```text
|
||||
User Input
|
||||
↓
|
||||
InteractionHub
|
||||
↓
|
||||
EncounterDetector
|
||||
↓
|
||||
ImpressionRecaller
|
||||
↓
|
||||
FirstEncounterModule
|
||||
↓
|
||||
EncounterFrame
|
||||
↓
|
||||
PromptContributor / AppendPrompt
|
||||
↓
|
||||
CoreModel Reply
|
||||
↓
|
||||
ImpressionUpdater
|
||||
```
|
||||
|
||||
其中:
|
||||
|
||||
1. `EncounterDetector` 判断是否需要进入初见/重逢模式;
|
||||
2. `ImpressionRecaller` 召回相关印象;
|
||||
3. `FirstEncounterModule` 将召回结果整理成 EncounterFrame;
|
||||
4. `PromptContributor` 将 EncounterFrame 注入模型上下文;
|
||||
5. 对话结束后,`ImpressionUpdater` 根据用户反馈更新印象。
|
||||
|
||||
## EncounterFrame
|
||||
|
||||
`EncounterFrame` 是初见模块的核心输出。它不是长期记忆,而是本轮对话使用的临时认知框架。
|
||||
|
||||
示例结构:
|
||||
|
||||
```kotlin
|
||||
data class EncounterFrame(
|
||||
val mode: EncounterMode,
|
||||
val knownAboutUser: List<ImpressionProjection>,
|
||||
val knownAboutSelf: List<ImpressionProjection>,
|
||||
val knownAboutRelationship: List<ImpressionProjection>,
|
||||
val uncertainty: List<String>,
|
||||
val correctionProtocol: CorrectionProtocol,
|
||||
val openingStrategy: OpeningStrategy
|
||||
)
|
||||
```
|
||||
|
||||
其中:
|
||||
|
||||
- `mode`:当前是初见、重逢、版本升级后再介绍,还是用户主动询问;
|
||||
- `knownAboutUser`:关于用户的可靠印象;
|
||||
- `knownAboutSelf`:Partner 对自身能力和边界的描述;
|
||||
- `knownAboutRelationship`:关于互动方式、纠错方式、语气偏好等印象;
|
||||
- `uncertainty`:当前不能确定的部分;
|
||||
- `correctionProtocol`:用户如何纠正 Partner;
|
||||
- `openingStrategy`:本次开场应采用的表达策略。
|
||||
|
||||
## Impression Subject 建议
|
||||
|
||||
为了支持初见模块,Impression 可以支持一些特殊 subject:
|
||||
|
||||
```text
|
||||
user
|
||||
agent_self
|
||||
relationship_contract
|
||||
interaction_preference
|
||||
project_context
|
||||
```
|
||||
|
||||
例如:
|
||||
|
||||
```text
|
||||
user:
|
||||
- 用户偏好技术回答直接,不喜欢客服腔。
|
||||
- 用户面对陌生 agent 时会在意互动预期是否稳定。
|
||||
- 用户更容易接受从一个小切口开始推进。
|
||||
|
||||
agent_self:
|
||||
- Partner 当前不是完全成熟的 agent。
|
||||
- Partner 应公开自己的已知、未知和不确定。
|
||||
- Partner 不应该在缺少依据时假装熟悉用户。
|
||||
|
||||
relationship_contract:
|
||||
- 用户可以直接纠正 Partner。
|
||||
- Partner 需要区分事实错误、语气偏差、理解偏差和任务边界偏差。
|
||||
- 纠正应作为后续 impression 更新的重要信号。
|
||||
```
|
||||
|
||||
## 初见开场策略
|
||||
|
||||
初见模块不应一上来问很多问题,也不应假装已经充分了解用户。
|
||||
|
||||
更合适的开场结构是:
|
||||
|
||||
```text
|
||||
我现在对你还没有足够稳定的了解。
|
||||
|
||||
我会先说明:
|
||||
- 我目前知道什么;
|
||||
- 我不知道什么;
|
||||
- 你可以怎么纠正我;
|
||||
- 我会如何处理这些纠正。
|
||||
|
||||
接下来我们可以从一个很小的任务开始。
|
||||
```
|
||||
|
||||
在 prompt 中可组织为:
|
||||
|
||||
```text
|
||||
你正在与用户进行初见/重逢式对话。
|
||||
|
||||
你目前可靠知道:
|
||||
- 用户希望技术讨论直接、少废话;
|
||||
- 用户对陌生 agent 的互动预期尚未建立;
|
||||
- 用户不喜欢 agent 在缺少依据时假装熟悉。
|
||||
|
||||
你应该主动说明:
|
||||
- 你知道什么;
|
||||
- 你不知道什么;
|
||||
- 用户可以如何纠正你;
|
||||
- 你会如何处理纠正。
|
||||
|
||||
不要一次性问很多问题。
|
||||
不要假装亲近。
|
||||
先从一个很小的任务或对话入口开始。
|
||||
```
|
||||
|
||||
## EncounterState
|
||||
|
||||
初见模块需要少量流程状态,但这些状态不一定属于 Impression。
|
||||
|
||||
示例:
|
||||
|
||||
```kotlin
|
||||
data class EncounterState(
|
||||
val hasIntroducedSelf: Boolean,
|
||||
val hasShownKnownUnknown: Boolean,
|
||||
val hasExplainedCorrectionProtocol: Boolean,
|
||||
val firstEncounterCompleted: Boolean,
|
||||
val lastEncounterVersion: String?
|
||||
)
|
||||
```
|
||||
|
||||
这些状态表示流程是否完成,而不是关于用户的长期印象。
|
||||
|
||||
真正应该进入 Impression 的,是对用户、关系、互动方式的理解,例如:
|
||||
|
||||
```text
|
||||
用户面对新的 agent 时,会担心互动预期不稳定。
|
||||
用户希望 agent 明确边界,而不是一上来装熟。
|
||||
用户能接受通过纠正来校准 agent。
|
||||
```
|
||||
|
||||
## 最小实现方案
|
||||
|
||||
第一版可以很轻,不需要完整工程化。
|
||||
|
||||
建议步骤:
|
||||
|
||||
1. 新增 `FirstEncounterPromptContributor`;
|
||||
2. 新增 `EncounterDetector`,先用简单规则判断是否触发;
|
||||
3. 从 `ImpressionRecaller` 召回 `user`、`agent_self`、`relationship_contract`、`interaction_preference`、`project_context` 相关印象;
|
||||
4. 生成一个简化版 `EncounterFrame`;
|
||||
5. 将 EncounterFrame 注入 AppendPrompt;
|
||||
6. 用户纠正后,将纠正内容作为 evidence 交给 ImpressionUpdater。
|
||||
|
||||
第一版不需要复杂策略模型,规则足够:
|
||||
|
||||
```text
|
||||
新 session + 低熟悉度 → 初见模式
|
||||
长时间未使用 + 有历史 impression → 重逢模式
|
||||
用户主动询问已知/未知 → 自我公开模式
|
||||
多次纠正 → 关系校准模式
|
||||
```
|
||||
|
||||
## 不做什么
|
||||
|
||||
初见模块第一版不做以下内容:
|
||||
|
||||
- 不做完整 onboarding 表单;
|
||||
- 不一次性询问大量偏好;
|
||||
- 不把用户画像写死;
|
||||
- 不假装已经理解用户;
|
||||
- 不替代 ImpressionCore;
|
||||
- 不直接负责长期记忆写入;
|
||||
- 不在每轮对话中重复自我介绍。
|
||||
|
||||
它只负责在关系尚未稳定时,提供一个清醒、可纠正、可继续的开场。
|
||||
|
||||
## 价值
|
||||
|
||||
初见模块的价值不只是“第一次使用体验更好”。
|
||||
|
||||
它实际上补上了 Partner 作为 agent 的一个关键能力:
|
||||
|
||||
> 在上下文断裂、长期未见、版本变化或记忆不确定时,仍然能让用户知道该如何继续与它相处。
|
||||
|
||||
这使 Partner 不只是一个能运行的程序,而是一个能够建立互动预期、暴露不确定性、接受校准,并逐步形成稳定关系的 agent。
|
||||
210
doc/design/impression-vector-fusion.md
Normal file
210
doc/design/impression-vector-fusion.md
Normal file
@@ -0,0 +1,210 @@
|
||||
# Impression Vector Fusion Plan
|
||||
|
||||
## Context
|
||||
|
||||
Current `ImpressionCore.projectEntity` already connects text recall to active entity projection:
|
||||
|
||||
```text
|
||||
input
|
||||
-> SimpleTextSearch.search(input)
|
||||
-> group document hits by ImpressionSearchTarget
|
||||
-> aggregate into EntityAssociationMatch
|
||||
-> resolve ACTIVE_ENTITY or ENTITY target
|
||||
-> append EntityEvidence
|
||||
-> refresh active entity text-search documents
|
||||
```
|
||||
|
||||
This gives the Impression module a first explainable recall path. Vector recall should not replace this path. It should become another recall signal that is fused with text recall before projection.
|
||||
|
||||
## Why not implement vector fusion immediately
|
||||
|
||||
Vector fusion is a recall-source enhancement, not the next foundation step.
|
||||
|
||||
Before adding more recall sources, the module still needs a clearer organization pipeline:
|
||||
|
||||
- how an unmatched input becomes a new `ActiveEntity`;
|
||||
- how runtime evidence is accumulated, merged, or decayed;
|
||||
- how an `ActiveEntity` is rolled into a long-term `Entity`;
|
||||
- how extracted features and impressions update known entities;
|
||||
- when `textSearch` and `vectorIndex` are refreshed after entity updates.
|
||||
|
||||
Unmatched entity creation and `ActiveEntity` rolling are closely related: both decide how temporary evidence becomes a stable entity-level impression. They should be considered as one organization chain rather than two unrelated features.
|
||||
|
||||
## Target shape
|
||||
|
||||
Future `projectEntity` should have this shape:
|
||||
|
||||
```text
|
||||
input
|
||||
-> text recall signals
|
||||
-> vector recall signals
|
||||
-> normalize scores
|
||||
-> fuse signals by ImpressionSearchTarget
|
||||
-> resolve or create ActiveEntity
|
||||
-> append evidence
|
||||
-> refresh runtime indexes
|
||||
```
|
||||
|
||||
The later half should stay shared. Text recall, vector recall, relation recall, and recency recall should all produce association signals. Projection should not depend on which recall source produced a match.
|
||||
|
||||
## First vector scope
|
||||
|
||||
The first vector implementation should only recall long-term `ENTITY` targets.
|
||||
|
||||
Reason:
|
||||
|
||||
- `ImpressionVectorIndex` already syncs known `Entity` data.
|
||||
- Known entities have relatively stable features and impressions.
|
||||
- Active entity evidence changes frequently; embedding every new evidence item would add update cost and lifecycle complexity too early.
|
||||
|
||||
So the first vector target should be:
|
||||
|
||||
```text
|
||||
Entity feature / impression vector
|
||||
-> ImpressionSearchTarget(Type.ENTITY, entityUuid)
|
||||
```
|
||||
|
||||
Later, after the active entity organization chain is stable, active evidence vectors can be added as:
|
||||
|
||||
```text
|
||||
ActiveEntity evidence / projected feature / projected impression vector
|
||||
-> ImpressionSearchTarget(Type.ACTIVE_ENTITY, runtimeId)
|
||||
```
|
||||
|
||||
## Signal model
|
||||
|
||||
`EntityAssociationMatch` is currently text-oriented because it stores `List<ImpressionSearchHit>`.
|
||||
|
||||
For fusion, introduce a source-neutral signal model:
|
||||
|
||||
```kotlin
|
||||
data class EntityAssociationSignal(
|
||||
val target: ImpressionSearchTarget,
|
||||
val source: Source,
|
||||
val score: Double,
|
||||
val reason: String,
|
||||
val textHit: ImpressionSearchHit? = null,
|
||||
val vectorHit: ImpressionVectorHit? = null,
|
||||
) {
|
||||
enum class Source {
|
||||
TEXT,
|
||||
VECTOR,
|
||||
RELATION,
|
||||
RECENCY
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then change or extend `EntityAssociationMatch` toward:
|
||||
|
||||
```kotlin
|
||||
data class EntityAssociationMatch(
|
||||
val target: ImpressionSearchTarget,
|
||||
val score: Double,
|
||||
val signals: List<EntityAssociationSignal> = emptyList(),
|
||||
)
|
||||
```
|
||||
|
||||
This keeps fusion explainable. A match can still tell the model or logs why an entity was recalled.
|
||||
|
||||
## Score normalization
|
||||
|
||||
Text search score and vector similarity should not be added directly.
|
||||
|
||||
Text search currently produces an internal score based on token hits, coverage, exact phrase bonus, field bonus, and document weight. Vector search is usually cosine-like similarity. Normalize both into association-strength-like values before fusion.
|
||||
|
||||
Possible first normalization:
|
||||
|
||||
```text
|
||||
textScore01 = clamp(textScore / 5.0, 0.0, 1.0)
|
||||
|
||||
vectorScore01 =
|
||||
similarity < 0.55 -> 0.0
|
||||
otherwise -> clamp((similarity - 0.55) / 0.35, 0.0, 1.0)
|
||||
```
|
||||
|
||||
The constants are placeholders. They should be tuned with tests and logs.
|
||||
|
||||
## Fusion rule
|
||||
|
||||
Use strong-hit priority with multi-source support, not simple averaging.
|
||||
|
||||
A first rule can be:
|
||||
|
||||
```text
|
||||
targetScore =
|
||||
max(bestTextScore, bestVectorScore * 0.9)
|
||||
+ sameTargetCrossSourceBonus
|
||||
+ supportingSignalBonus
|
||||
```
|
||||
|
||||
Suggested behavior:
|
||||
|
||||
- direct subject or phrase text match should beat vague vector similarity;
|
||||
- vector recall should recover semantically related entities when text recall is weak or empty;
|
||||
- if text and vector both hit the same target, the target should receive a small confidence boost;
|
||||
- long documents or many weak signals should not dominate a single strong subject/evidence hit.
|
||||
|
||||
## Execution strategy
|
||||
|
||||
First implementation can be conservative:
|
||||
|
||||
```text
|
||||
always run TextSearch
|
||||
run VectorSearch only when:
|
||||
- text recall is empty; or
|
||||
- top text match confidence is low; or
|
||||
- input is long and semantic rather than name-like
|
||||
```
|
||||
|
||||
If the embedding model is local and cheap enough, this can later become parallel text + vector recall.
|
||||
|
||||
## Implementation phases
|
||||
|
||||
### Phase 1: organization chain first
|
||||
|
||||
Implement before vector fusion:
|
||||
|
||||
- unmatched input -> new `ActiveEntity` candidate;
|
||||
- active evidence update and dedup/merge rules;
|
||||
- active entity rolling into known `Entity`;
|
||||
- known entity feature/impression update;
|
||||
- index refresh after entity updates.
|
||||
|
||||
### Phase 2: signal abstraction
|
||||
|
||||
Introduce `EntityAssociationSignal` and make text hits convert into signals.
|
||||
|
||||
Keep current behavior equivalent after refactor.
|
||||
|
||||
### Phase 3: long-term entity vector recall
|
||||
|
||||
Add vector recall only for known `Entity` targets:
|
||||
|
||||
```text
|
||||
input embedding
|
||||
-> ImpressionVectorIndex.search(...)
|
||||
-> vector hits
|
||||
-> EntityAssociationSignal(source = VECTOR)
|
||||
-> fuse with text signals
|
||||
```
|
||||
|
||||
### Phase 4: active entity vector recall
|
||||
|
||||
Only after active entity lifecycle is stable:
|
||||
|
||||
- vectorize active evidence or projected features;
|
||||
- update active vector index when evidence changes;
|
||||
- fuse `ACTIVE_ENTITY` vector hits with text hits.
|
||||
|
||||
## Non-goals for first vector pass
|
||||
|
||||
Do not start with:
|
||||
|
||||
- vectorizing every raw evidence item immediately;
|
||||
- replacing text search ranking;
|
||||
- using vector score as direct `associationConfidence` without normalization;
|
||||
- adding opaque fusion that cannot explain why an entity was recalled;
|
||||
- expanding `projectEntity` into a large source-specific method.
|
||||
|
||||
The intended direction is: multiple recall sources produce explainable signals, then `ImpressionCore` performs one shared entity projection flow.
|
||||
5
pom.xml
5
pom.xml
@@ -5,7 +5,7 @@
|
||||
|
||||
<groupId>work.slhaf.partner</groupId>
|
||||
<artifactId>partner</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>1.0.0</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
@@ -26,6 +26,9 @@
|
||||
|
||||
<!-- 推荐仓库默认不跳测试;本地需要时再 -DskipTests=true -->
|
||||
<skipTests>false</skipTests>
|
||||
|
||||
<partner.runtime.version>0.9.0-preview</partner.runtime.version>
|
||||
<partner.interaction-api.version>1.0.0</partner.interaction-api.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -2,17 +2,17 @@
|
||||
"partner": {
|
||||
"latestBuildable": {
|
||||
"url": "https://github.com/slhaf/Partner.git",
|
||||
"ref": "buildable/0.5.0"
|
||||
"ref": "buildable/0.9.0-preview"
|
||||
},
|
||||
"latestRelease": {
|
||||
"url": "https://github.com/slhaf/Partner/releases/download/release-core%2F0.5.0/partner-core-0.5.0.jar",
|
||||
"version": "0.5.0"
|
||||
"url": "https://github.com/slhaf/Partner/releases/download/release-core%2F0.9.0-preview/partner-core-0.9.0-preview.jar",
|
||||
"version": "0.9.0-preview"
|
||||
}
|
||||
},
|
||||
"externalModules": [
|
||||
{
|
||||
"name": "OneBot Adapter",
|
||||
"version": "0.5.0",
|
||||
"version": "1.0.0",
|
||||
"withGateway": true,
|
||||
"registryRef": "modules/onebot-adapter.json"
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"id": "onebot_channel",
|
||||
"name": "OneBot Adapter",
|
||||
"version": "0.5.0",
|
||||
"version": "1.0.0",
|
||||
"withGateway": true,
|
||||
"description": "OneBot v11 reverse WebSocket gateway adapter for Partner. It accepts reverse WebSocket connections from a OneBot implementation and converts private message events into Partner input events.",
|
||||
"source": {
|
||||
|
||||
Reference in New Issue
Block a user