2 Commits

Author SHA1 Message Date
a929b3e0e6 feat(impression): add vector index skeleton 2026-05-27 23:27:51 +08:00
fe6895d10b feat(impression): serialize entity state 2026-05-27 23:11:41 +08:00
3 changed files with 87 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ package work.slhaf.partner.core.cognition.impression
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.util.*
import java.util.concurrent.ConcurrentHashMap
@@ -122,15 +123,83 @@ class Entity @JvmOverloads constructor(
override fun statePath(): Path = Path.of("core", "impression", "entity-$uuid.json")
override fun load(state: JSONObject) {
TODO("Not yet implemented")
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) ->
numberValue(strengthValue)?.let { relationMap[relation] = it }
}
if (relationMap.isNotEmpty()) {
relations[target] = relationMap
}
}
}
}
state.getJSONObject("impressions")?.let { loadedImpressions ->
impressionLock.withLock {
impressions.clear()
impressions.putAll(loadIndexableDataMap(loadedImpressions))
}
}
state.getJSONObject("features")?.let { loadedFeatures ->
featureLock.withLock {
features.clear()
features.putAll(loadIndexableDataMap(loadedFeatures))
}
}
}
override fun convert(): State {
TODO("Not yet implemented")
val state = State()
state.append("uuid", StateValue.str(uuid))
state.append("subject", StateValue.str(subject))
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 loadIndexableDataMap(state: JSONObject): Map<String, IndexableData> {
val loaded = mutableMapOf<String, IndexableData>()
state.forEach { (key, value) ->
val confidence = when (value) {
is JSONObject -> value.getDouble("confidence")?.toDouble() ?: 1.0
else -> numberValue(value) ?: return@forEach
}
loaded[key] = IndexableData(confidence)
}
return loaded
}
private fun indexableDataState(source: Map<String, IndexableData>): Map<String, Map<String, Double>> =
source.mapValues { (_, data) -> mapOf("confidence" to data.confidence) }
private fun numberValue(value: Any?): Double? = when (value) {
is Number -> value.toDouble()
is String -> value.toDoubleOrNull()
else -> null
}
data class IndexableData(
var confidence: Double
) {
@@ -164,4 +233,4 @@ class Entity @JvmOverloads constructor(
val confidence: Double,
val vector: DoubleArray?
)
}
}

View File

@@ -22,6 +22,7 @@ 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();
@CapabilityMethod
public void updateRelation() {
@@ -66,6 +67,7 @@ public class ImpressionCore implements StateSerializable {
Entity entity = new Entity(uuid, subject);
entity.load();
vectorIndex.sync(entity);
knownEntitiesByUuid.put(uuid, entity);
}
}

View File

@@ -0,0 +1,13 @@
package work.slhaf.partner.core.cognition.impression;
public class ImpressionVectorIndex {
public void sync(Entity entity){
// TODO sync entity impressions/features with vector index.
}
public void upsert(String content, Entity.IndexableData indexableData){
// TODO update vector for content when embedding/vector client boundary is finalized.
}
}