mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(framework): migrate AgentRegisterContext to Kotlin and store validated capability scan results in context
This commit is contained in:
@@ -20,11 +20,13 @@ class CapabilityAnnotationValidatorFactory : AgentBaseFactory() {
|
||||
val reflections = context.reflections
|
||||
val cores = loadCores(reflections)
|
||||
val capabilities = loadCapabilities(reflections)
|
||||
val methods = loadCapabilityMethods(reflections)
|
||||
|
||||
checkCapabilityUniqueByValue(capabilities)
|
||||
checkCapabilityMethodLocation(reflections)
|
||||
checkCapabilityMethodLocation(methods)
|
||||
checkCapabilityMethodsImplementedUniquely(cores, capabilities)
|
||||
checkInjectCapability(reflections)
|
||||
storeValidatedScanResult(context, cores, capabilities, methods)
|
||||
}
|
||||
|
||||
private fun loadCores(reflections: Reflections): Set<Class<*>> {
|
||||
@@ -37,6 +39,10 @@ class CapabilityAnnotationValidatorFactory : AgentBaseFactory() {
|
||||
return reflections.getTypesAnnotatedWith(Capability::class.java).toSet()
|
||||
}
|
||||
|
||||
private fun loadCapabilityMethods(reflections: Reflections): Set<java.lang.reflect.Method> {
|
||||
return reflections.getMethodsAnnotatedWith(CapabilityMethod::class.java).toSet()
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则1: @Capability 按 value 唯一
|
||||
*/
|
||||
@@ -54,17 +60,16 @@ class CapabilityAnnotationValidatorFactory : AgentBaseFactory() {
|
||||
/**
|
||||
* 规则3.1: @CapabilityMethod 仅能用于 @CapabilityCore 类
|
||||
*/
|
||||
private fun checkCapabilityMethodLocation(reflections: Reflections) {
|
||||
reflections.getMethodsAnnotatedWith(CapabilityMethod::class.java)
|
||||
.forEach { method ->
|
||||
val declaringClass = method.declaringClass
|
||||
if (!declaringClass.isAnnotationPresent(CapabilityCore::class.java)) {
|
||||
throw UnMatchedCapabilityException(
|
||||
"@CapabilityMethod 仅能用于 @CapabilityCore 所标注类中: " +
|
||||
"${declaringClass.name}#${method.name}"
|
||||
)
|
||||
}
|
||||
private fun checkCapabilityMethodLocation(methods: Set<java.lang.reflect.Method>) {
|
||||
methods.forEach { method ->
|
||||
val declaringClass = method.declaringClass
|
||||
if (!declaringClass.isAnnotationPresent(CapabilityCore::class.java)) {
|
||||
throw UnMatchedCapabilityException(
|
||||
"@CapabilityMethod 仅能用于 @CapabilityCore 所标注类中: " +
|
||||
"${declaringClass.name}#${method.name}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,4 +126,19 @@ class CapabilityAnnotationValidatorFactory : AgentBaseFactory() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun storeValidatedScanResult(
|
||||
context: AgentRegisterContext,
|
||||
cores: Set<Class<*>>,
|
||||
capabilities: Set<Class<*>>,
|
||||
methods: Set<java.lang.reflect.Method>
|
||||
) {
|
||||
val capabilityFactoryContext = context.capabilityFactoryContext
|
||||
capabilityFactoryContext.cores.clear()
|
||||
capabilityFactoryContext.capabilities.clear()
|
||||
capabilityFactoryContext.methods.clear()
|
||||
capabilityFactoryContext.cores.addAll(cores)
|
||||
capabilityFactoryContext.capabilities.addAll(capabilities)
|
||||
capabilityFactoryContext.methods.addAll(methods)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package work.slhaf.partner.api.agent.factory.context;
|
||||
|
||||
import lombok.Data;
|
||||
import org.reflections.Reflections;
|
||||
import org.reflections.scanners.Scanners;
|
||||
import org.reflections.util.ConfigurationBuilder;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class AgentRegisterContext {
|
||||
private Reflections reflections;
|
||||
private ConfigFactoryContext configFactoryContext = new ConfigFactoryContext();
|
||||
private AgentContext agentContext = AgentContext.INSTANCE;
|
||||
|
||||
public AgentRegisterContext(List<URL> urls) {
|
||||
reflections = new Reflections(new ConfigurationBuilder().setScanners(
|
||||
Scanners.FieldsAnnotated,
|
||||
Scanners.SubTypes,
|
||||
Scanners.MethodsAnnotated,
|
||||
Scanners.TypesAnnotated
|
||||
)
|
||||
.setUrls(urls)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package work.slhaf.partner.api.agent.factory.context
|
||||
|
||||
import org.reflections.Reflections
|
||||
import org.reflections.scanners.Scanners
|
||||
import org.reflections.util.ConfigurationBuilder
|
||||
import work.slhaf.partner.api.agent.factory.config.pojo.ModelConfig
|
||||
import work.slhaf.partner.api.chat.pojo.Message
|
||||
import java.lang.reflect.Method
|
||||
import java.net.URL
|
||||
|
||||
class AgentRegisterContext(urls: List<URL>) {
|
||||
val reflections: Reflections = Reflections(
|
||||
ConfigurationBuilder().setScanners(
|
||||
Scanners.FieldsAnnotated,
|
||||
Scanners.SubTypes,
|
||||
Scanners.MethodsAnnotated,
|
||||
Scanners.TypesAnnotated
|
||||
).setUrls(urls)
|
||||
)
|
||||
|
||||
val configFactoryContext: ConfigFactoryContext = ConfigFactoryContext()
|
||||
val capabilityFactoryContext: CapabilityFactoryContext = CapabilityFactoryContext()
|
||||
val agentContext: AgentContext = AgentContext
|
||||
}
|
||||
|
||||
class ConfigFactoryContext {
|
||||
val modelPromptMap: HashMap<String, List<Message>> = HashMap()
|
||||
val modelConfigMap: HashMap<String, ModelConfig> = HashMap()
|
||||
}
|
||||
|
||||
class CapabilityFactoryContext {
|
||||
val cores: MutableSet<Class<*>> = LinkedHashSet()
|
||||
val capabilities: MutableSet<Class<*>> = LinkedHashSet()
|
||||
val methods: MutableSet<Method> = LinkedHashSet()
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package work.slhaf.partner.api.agent.factory.context;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.agent.factory.config.pojo.ModelConfig;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ConfigFactoryContext {
|
||||
private HashMap<String, List<Message>> modelPromptMap = new HashMap<>();
|
||||
private HashMap<String, ModelConfig> modelConfigMap = new HashMap<>();
|
||||
}
|
||||
Reference in New Issue
Block a user