mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
需继续为上层模块构建注册体系以适应完整的加载逻辑。
- 移除了 BaseCoordinateManager 抽象类,而是添加了 @CoordinateManager 注解 - 移除了 CapabilityHolder 抽象类,换成 @CapabilityHolder 注解 - 新增了适应新注册机制的部分类,仍需进一步推进
This commit is contained in:
4
.idea/encodings.xml
generated
4
.idea/encodings.xml
generated
@@ -3,12 +3,12 @@
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/Partner-Api/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Api/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Capability-Demo/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Capability-Demo/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Main/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Main/src/main/java/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Main/src/main/java/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Main/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Test-Demo/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Test-Demo/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
|
||||
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@@ -1,8 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="EntryPointsManager">
|
||||
<list size="1">
|
||||
<list size="2">
|
||||
<item index="0" class="java.lang.String" itemvalue="work.slhaf.partner.api.capability.annotation.CapabilityMethod" />
|
||||
<item index="1" class="java.lang.String" itemvalue="work.slhaf.partner.api.capability.annotation.CoordinateManager" />
|
||||
</list>
|
||||
</component>
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package work.slhaf.partner.api;
|
||||
|
||||
public class Agent {
|
||||
public static void run(Class<?> clazz) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package work.slhaf.partner.api;
|
||||
|
||||
import work.slhaf.partner.api.common.entity.AgentRegisterContext;
|
||||
|
||||
public class AgentRegisterFactory {
|
||||
|
||||
private AgentRegisterContext context = new AgentRegisterContext();
|
||||
|
||||
private AgentRegisterFactory(){}
|
||||
|
||||
public static void launch(){
|
||||
//TODO 通过调用module与capability的注册逻辑,完成完整的注册过程,需要考虑hook机制
|
||||
AgentRegisterFactory factory = new AgentRegisterFactory();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package work.slhaf.partner.api.capability;
|
||||
|
||||
|
||||
public class BaseCoordinateManager {
|
||||
}
|
||||
@@ -6,8 +6,7 @@ import org.reflections.util.ClasspathHelper;
|
||||
import org.reflections.util.ConfigurationBuilder;
|
||||
import work.slhaf.partner.api.capability.annotation.*;
|
||||
import work.slhaf.partner.api.capability.exception.*;
|
||||
import work.slhaf.partner.api.capability.module.CapabilityHolder;
|
||||
import work.slhaf.partner.api.capability.util.CapabilityUtil;
|
||||
import work.slhaf.partner.api.common.util.AgentUtil;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.net.URL;
|
||||
@@ -15,13 +14,11 @@ import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static work.slhaf.partner.api.capability.util.CapabilityUtil.methodSignature;
|
||||
import static work.slhaf.partner.api.common.util.AgentUtil.methodSignature;
|
||||
|
||||
|
||||
public final class CapabilityRegisterFactory {
|
||||
|
||||
public static volatile CapabilityRegisterFactory capabilityRegisterFactory;
|
||||
|
||||
private Reflections reflections;
|
||||
private final HashMap<String, Function<Object[], Object>> methodsRouterTable = new HashMap<>();
|
||||
private final HashMap<String, Function<Object[], Object>> coordinatedMethodsRouterTable = new HashMap<>();
|
||||
@@ -33,26 +30,20 @@ public final class CapabilityRegisterFactory {
|
||||
private CapabilityRegisterFactory() {
|
||||
}
|
||||
|
||||
public static CapabilityRegisterFactory getInstance() {
|
||||
if (capabilityRegisterFactory == null) {
|
||||
synchronized (CapabilityRegisterFactory.class) {
|
||||
if (capabilityRegisterFactory == null) {
|
||||
capabilityRegisterFactory = new CapabilityRegisterFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
return capabilityRegisterFactory;
|
||||
}
|
||||
|
||||
|
||||
//TODO 需决定是否分离检查与路由表生成、注入逻辑,如果分离,可进一步添加hook点,但目前似乎并非必要
|
||||
public void registerCapabilities(String scannerPath) {
|
||||
setBasicVariable(scannerPath);
|
||||
//检查可注册能力是否正常
|
||||
statusCheck();
|
||||
//扫描现有Capability, value为键,返回函数路由表, 函数路由表内部通过反射调用对应core的方法
|
||||
generateRouterTable();
|
||||
//通过动态代理注入能力
|
||||
injectCapability();
|
||||
}
|
||||
|
||||
private void generateRouterTable() {
|
||||
generateMethodsRouterTable();
|
||||
generateCoordinatedMethodsRouterTable();
|
||||
//扫描现有Capability, value为键,返回函数路由表, 函数路由表内部通过反射调用对应core的方法
|
||||
injectCapability();
|
||||
}
|
||||
|
||||
private void generateCoordinatedMethodsRouterTable() {
|
||||
@@ -82,9 +73,9 @@ public final class CapabilityRegisterFactory {
|
||||
|
||||
private HashMap<String, Object> getCognationManagerInstances() throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
for (Class<? extends BaseCoordinateManager> c : reflections.getSubTypesOf(BaseCoordinateManager.class)) {
|
||||
Constructor<? extends BaseCoordinateManager> constructor = c.getDeclaredConstructor();
|
||||
BaseCoordinateManager instance = constructor.newInstance();
|
||||
for (Class<?> c : reflections.getTypesAnnotatedWith(CoordinateManager.class)) {
|
||||
Constructor<?> constructor = c.getDeclaredConstructor();
|
||||
Object instance = constructor.newInstance();
|
||||
|
||||
Arrays.stream(c.getMethods())
|
||||
.filter(method -> method.isAnnotationPresent(Coordinated.class))
|
||||
@@ -186,8 +177,8 @@ public final class CapabilityRegisterFactory {
|
||||
|
||||
private void checkInjectCapability() {
|
||||
reflections.getFieldsAnnotatedWith(InjectCapability.class).forEach(field -> {
|
||||
if (!CapabilityHolder.class.isAssignableFrom(field.getDeclaringClass())) {
|
||||
throw new UnMatchedCapabilityException("InjectCapability 注解只能用于CapabilityHolder的子类");
|
||||
if (!field.getDeclaringClass().isAssignableFrom(CapabilityHolder.class)) {
|
||||
throw new UnMatchedCapabilityException("InjectCapability 注解只能用于 CapabilityHolder 注解所在类");
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -223,7 +214,7 @@ public final class CapabilityRegisterFactory {
|
||||
})
|
||||
.collect(Collectors.toSet());
|
||||
if (!methodsToCoordinated.isEmpty()) {
|
||||
Set<Class<? extends BaseCoordinateManager>> subTypesOfAbsCM = reflections.getSubTypesOf(BaseCoordinateManager.class);
|
||||
Set<Class<?>> subTypesOfAbsCM = reflections.getTypesAnnotatedWith(CoordinateManager.class);
|
||||
Set<String> methodsCoordinated = getMethodsCoordinated(subTypesOfAbsCM);
|
||||
if (!methodsCoordinated.equals(methodsToCoordinated)) {
|
||||
// 找出缺少的协调方法
|
||||
@@ -245,9 +236,9 @@ public final class CapabilityRegisterFactory {
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> getMethodsCoordinated(Set<Class<? extends BaseCoordinateManager>> subTypesOfAbsCM) {
|
||||
private Set<String> getMethodsCoordinated(Set<Class<?>> classes) {
|
||||
Set<String> methodsCoordinated = new HashSet<>();
|
||||
for (Class<? extends BaseCoordinateManager> cm : subTypesOfAbsCM) {
|
||||
for (Class<?> cm : classes) {
|
||||
Method[] methods = cm.getMethods();
|
||||
for (Method method : methods) {
|
||||
if (method.isAnnotationPresent(Coordinated.class)) {
|
||||
@@ -280,11 +271,11 @@ public final class CapabilityRegisterFactory {
|
||||
private LackRecord checkMethodsMatched(List<Method> methodsWithAnnotation, List<Method> capabilityMethods) {
|
||||
Set<String> collectedMethodsWithAnnotation = methodsWithAnnotation.stream()
|
||||
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
||||
.map(CapabilityUtil::methodSignature)
|
||||
.map(AgentUtil::methodSignature)
|
||||
.collect(Collectors.toSet());
|
||||
Set<String> collectedCapabilityMethods = capabilityMethods.stream()
|
||||
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
||||
.map(CapabilityUtil::methodSignature)
|
||||
.map(AgentUtil::methodSignature)
|
||||
.collect(Collectors.toSet());
|
||||
return checkMethodsMatched(collectedMethodsWithAnnotation, collectedCapabilityMethods);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package work.slhaf.partner.api.capability.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface CapabilityHolder {
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package work.slhaf.demo.capability.annotation;
|
||||
package work.slhaf.partner.api.capability.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface CapabilityMethod {
|
||||
public @interface CoordinateManager {
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package work.slhaf.partner.api.capability.module;
|
||||
|
||||
import work.slhaf.partner.api.capability.CapabilityRegisterFactory;
|
||||
|
||||
public abstract class CapabilityHolder {
|
||||
protected CapabilityHolder() {
|
||||
CapabilityRegisterFactory.getInstance().registerModule(this);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package work.slhaf.partner.api.capability.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public final class CapabilityUtil {
|
||||
public static String methodSignature(Method method) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("(");
|
||||
sb.append(method.getReturnType().getName()).append(" ");
|
||||
sb.append(method.getName()).append("(");
|
||||
Class<?>[] paramTypes = method.getParameterTypes();
|
||||
for (int i = 0; i < paramTypes.length; i++) {
|
||||
sb.append(paramTypes[i].getName());
|
||||
if (i < paramTypes.length - 1) sb.append(",");
|
||||
}
|
||||
sb.append(")").append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package work.slhaf.partner.api.common.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
@Data
|
||||
public class AgentRegisterContext {
|
||||
//TODO 抽取出必要的注册工厂共用的上下文
|
||||
private Reflections reflections;
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package work.slhaf.demo.capability.util;
|
||||
package work.slhaf.partner.api.common.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public final class CapabilityUtil {
|
||||
public final class AgentUtil {
|
||||
public static String methodSignature(Method method) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("(");
|
||||
@@ -0,0 +1,4 @@
|
||||
package work.slhaf.partner.api.module;
|
||||
|
||||
public class ModuleRegisterFactory {
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package work.slhaf;
|
||||
|
||||
import work.slhaf.demo.TestModule;
|
||||
import work.slhaf.demo.capability.CapabilityRegisterFactory;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws ClassNotFoundException {
|
||||
TestModule testModule = new TestModule();
|
||||
CapabilityRegisterFactory.getInstance().registerCapabilities(Main.class.getPackage().getName());
|
||||
testModule.execute();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package work.slhaf.demo.capability;
|
||||
|
||||
public class BaseCognationManager {
|
||||
}
|
||||
@@ -1,373 +0,0 @@
|
||||
package work.slhaf.demo.capability;
|
||||
|
||||
import org.reflections.Reflections;
|
||||
import org.reflections.scanners.Scanners;
|
||||
import org.reflections.util.ClasspathHelper;
|
||||
import org.reflections.util.ConfigurationBuilder;
|
||||
import work.slhaf.demo.capability.annotation.*;
|
||||
import work.slhaf.demo.capability.exception.*;
|
||||
import work.slhaf.demo.capability.module.CapabilityHolder;
|
||||
import work.slhaf.demo.capability.util.CapabilityUtil;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static work.slhaf.demo.capability.util.CapabilityUtil.methodSignature;
|
||||
|
||||
public final class CapabilityRegisterFactory {
|
||||
|
||||
public static volatile CapabilityRegisterFactory capabilityRegisterFactory;
|
||||
|
||||
private Reflections reflections;
|
||||
private final HashMap<String, Function<Object[], Object>> methodsRouterTable = new HashMap<>();
|
||||
private final HashMap<String, Function<Object[], Object>> coordinatedMethodsRouterTable = new HashMap<>();
|
||||
private final HashMap<Class<?>, Object> capabilityCoreInstances = new HashMap<>();
|
||||
private final HashMap<Class<?>, Object> capabilityHolderInstances = new HashMap<>();
|
||||
private Set<Class<?>> cores;
|
||||
private Set<Class<?>> capabilities;
|
||||
|
||||
private CapabilityRegisterFactory() {
|
||||
}
|
||||
|
||||
public static CapabilityRegisterFactory getInstance() {
|
||||
if (capabilityRegisterFactory == null) {
|
||||
synchronized (CapabilityRegisterFactory.class) {
|
||||
if (capabilityRegisterFactory == null) {
|
||||
capabilityRegisterFactory = new CapabilityRegisterFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
return capabilityRegisterFactory;
|
||||
}
|
||||
|
||||
|
||||
public void registerCapabilities(String scannerPath) {
|
||||
setBasicVariable(scannerPath);
|
||||
//检查可注册能力是否正常
|
||||
statusCheck();
|
||||
generateMethodsRouterTable();
|
||||
generateCoordinatedMethodsRouterTable();
|
||||
//扫描现有Capability, value为键,返回函数路由表, 函数路由表内部通过反射调用对应core的方法
|
||||
injectCapability();
|
||||
}
|
||||
|
||||
private void generateCoordinatedMethodsRouterTable() {
|
||||
Set<Method> methodsAnnotatedWith = reflections.getMethodsAnnotatedWith(Coordinated.class);
|
||||
if (methodsAnnotatedWith.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
//获取所有CM实例
|
||||
HashMap<String, Object> cognationManagerInstances = getCognationManagerInstances();
|
||||
methodsAnnotatedWith.forEach(method -> {
|
||||
String key = method.getAnnotation(Coordinated.class).capability() + "." + methodSignature(method);
|
||||
Function<Object[], Object> function = args -> {
|
||||
try {
|
||||
return method.invoke(cognationManagerInstances.get(key), args);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
coordinatedMethodsRouterTable.put(key, function);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
throw new FactoryExecuteFailedException("创建协调方法路由表出错", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private HashMap<String, Object> getCognationManagerInstances() throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
for (Class<? extends BaseCognationManager> c : reflections.getSubTypesOf(BaseCognationManager.class)) {
|
||||
Constructor<? extends BaseCognationManager> constructor = c.getDeclaredConstructor();
|
||||
BaseCognationManager instance = constructor.newInstance();
|
||||
|
||||
Arrays.stream(c.getMethods())
|
||||
.filter(method -> method.isAnnotationPresent(Coordinated.class))
|
||||
.forEach(method -> {
|
||||
String key = method.getAnnotation(Coordinated.class).capability() + "." + methodSignature(method);
|
||||
map.put(key, instance);
|
||||
});
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private void setBasicVariable(String scannerPath) {
|
||||
setReflections(scannerPath);
|
||||
setAnnotatedClasses();
|
||||
}
|
||||
|
||||
private void setAnnotatedClasses() {
|
||||
cores = reflections.getTypesAnnotatedWith(CapabilityCore.class);
|
||||
capabilities = reflections.getTypesAnnotatedWith(Capability.class);
|
||||
}
|
||||
|
||||
private void setReflections(String scannerPath) {
|
||||
//后续可替换为根据传入的启动类获取路径
|
||||
Collection<URL> urls = ClasspathHelper.forPackage(scannerPath);
|
||||
reflections = new Reflections(
|
||||
new ConfigurationBuilder()
|
||||
.setUrls(urls)
|
||||
.setScanners(
|
||||
Scanners.TypesAnnotated,
|
||||
Scanners.MethodsAnnotated,
|
||||
Scanners.SubTypes,
|
||||
Scanners.FieldsAnnotated
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private void generateMethodsRouterTable() {
|
||||
//扫描`@Capability`与`@CapabilityMethod`注解的类与方法
|
||||
//将`capabilityValue.methodSignature`作为key,函数对象为通过反射拿到的core实例对应的方法
|
||||
cores.forEach(core -> Arrays.stream(core.getMethods())
|
||||
.filter(method -> method.isAnnotationPresent(CapabilityMethod.class))
|
||||
.forEach(method -> {
|
||||
Function<Object[], Object> function = args -> {
|
||||
try {
|
||||
return method.invoke(capabilityCoreInstances.get(core), args);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
String key = core.getAnnotation(CapabilityCore.class).value() + "." + methodSignature(method);
|
||||
if (methodsRouterTable.containsKey(key)) {
|
||||
throw new DuplicateMethodException("重复注册能力方法: " + core.getPackage().getName() + "." + core.getSimpleName() + "#" + method.getName());
|
||||
}
|
||||
methodsRouterTable.put(key, function);
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
private void injectCapability() {
|
||||
//获取现有的`@InjectCapability`注解所在字段,并获取对应的类,通过动态代理注入对象
|
||||
Set<Field> fields = reflections.getFieldsAnnotatedWith(InjectCapability.class);
|
||||
//在动态代理内部,通过函数路由表调用对应的方法
|
||||
createProxy(fields);
|
||||
}
|
||||
|
||||
private void createProxy(Set<Field> fields) {
|
||||
try {
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
Class<?> fieldType = field.getType();
|
||||
Object instance = Proxy.newProxyInstance(
|
||||
fieldType.getClassLoader(),
|
||||
new Class[]{fieldType},
|
||||
(proxy, method, objects) -> {
|
||||
if (method.isAnnotationPresent(ToCoordinated.class)) {
|
||||
String key = method.getDeclaringClass().getAnnotation(Capability.class).value() + "." + methodSignature(method);
|
||||
return coordinatedMethodsRouterTable.get(key).apply(objects);
|
||||
}
|
||||
String key = fieldType.getAnnotation(Capability.class).value() + "." + methodSignature(method);
|
||||
return methodsRouterTable.get(key).apply(objects);
|
||||
}
|
||||
);
|
||||
field.set(capabilityHolderInstances.get(field.getDeclaringClass()), instance);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new ProxySetFailedException("代理设置失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void statusCheck() {
|
||||
capabilityHolderCheck();
|
||||
checkCountAndCapabilities();
|
||||
checkCapabilityMethods();
|
||||
checkCoordinatedMethods();
|
||||
checkInjectCapability();
|
||||
//检查完毕,设置core的实例类
|
||||
setCapabilityCoreInstances();
|
||||
}
|
||||
|
||||
private void checkInjectCapability() {
|
||||
reflections.getFieldsAnnotatedWith(InjectCapability.class).forEach(field -> {
|
||||
if (!CapabilityHolder.class.isAssignableFrom(field.getDeclaringClass())) {
|
||||
throw new UnMatchedCapabilityException("InjectCapability 注解只能用于CapabilityHolder的子类");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void capabilityHolderCheck() {
|
||||
if (capabilityHolderInstances.isEmpty()) {
|
||||
throw new EmptyCapabilityHolderException("Capability 持有者实例为空");
|
||||
}
|
||||
}
|
||||
|
||||
private void setCapabilityCoreInstances() {
|
||||
try {
|
||||
for (Class<?> core : cores) {
|
||||
Constructor<?> constructor = core.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
capabilityCoreInstances.put(core, constructor.newInstance());
|
||||
}
|
||||
} catch (InvocationTargetException | NoSuchMethodException | InstantiationException |
|
||||
IllegalAccessException e) {
|
||||
throw new CoreInstancesCreateFailedException("core实例创建失败");
|
||||
}
|
||||
}
|
||||
|
||||
private void checkCoordinatedMethods() {
|
||||
//检查各个capability中是否含有ToCoordinated注解
|
||||
//如果含有,则需要查找AbstractCognationManager的子类,看这里是否有对应的Coordinated注解所在方法
|
||||
Set<String> methodsToCoordinated = capabilities.stream()
|
||||
.flatMap(capability -> Arrays.stream(capability.getDeclaredMethods()))
|
||||
.filter(method -> method.isAnnotationPresent(ToCoordinated.class))
|
||||
.map(method -> {
|
||||
String capabilityValue = method.getDeclaringClass().getAnnotation(Capability.class).value();
|
||||
return capabilityValue + "." + methodSignature(method);
|
||||
})
|
||||
.collect(Collectors.toSet());
|
||||
if (!methodsToCoordinated.isEmpty()) {
|
||||
Set<Class<? extends BaseCognationManager>> subTypesOfAbsCM = reflections.getSubTypesOf(BaseCognationManager.class);
|
||||
Set<String> methodsCoordinated = getMethodsCoordinated(subTypesOfAbsCM);
|
||||
if (!methodsCoordinated.equals(methodsToCoordinated)) {
|
||||
// 找出缺少的协调方法
|
||||
Set<String> missingMethods = new HashSet<>(methodsToCoordinated);
|
||||
missingMethods.removeAll(methodsCoordinated);
|
||||
|
||||
// 找出多余的协调方法
|
||||
Set<String> extraMethods = new HashSet<>(methodsCoordinated);
|
||||
extraMethods.removeAll(methodsToCoordinated);
|
||||
|
||||
// 抛出异常或记录错误
|
||||
if (!missingMethods.isEmpty()) {
|
||||
throw new UnMatchedCoordinatedMethodException("缺少协调方法: " + String.join(", ", missingMethods));
|
||||
}
|
||||
if (!extraMethods.isEmpty()) {
|
||||
throw new UnMatchedCoordinatedMethodException("发现多余的协调方法: " + String.join(", ", extraMethods));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> getMethodsCoordinated(Set<Class<? extends BaseCognationManager>> subTypesOfAbsCM) {
|
||||
Set<String> methodsCoordinated = new HashSet<>();
|
||||
for (Class<? extends BaseCognationManager> cm : subTypesOfAbsCM) {
|
||||
Method[] methods = cm.getMethods();
|
||||
for (Method method : methods) {
|
||||
if (method.isAnnotationPresent(Coordinated.class)) {
|
||||
methodsCoordinated.add(method.getAnnotation(Coordinated.class).capability() + "." + methodSignature(method));
|
||||
}
|
||||
}
|
||||
}
|
||||
return methodsCoordinated;
|
||||
}
|
||||
|
||||
|
||||
private void checkCapabilityMethods() {
|
||||
HashMap<String, List<Method>> capabilitiesMethods = getCapabilityMethods(capabilities);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Class<?> core : cores) {
|
||||
List<Method> methodsWithAnnotation = Arrays.stream(core.getMethods())
|
||||
.filter(method -> method.isAnnotationPresent(CapabilityMethod.class))
|
||||
.toList();
|
||||
List<Method> capabilityMethods = capabilitiesMethods.get(core.getAnnotation(CapabilityCore.class).value());
|
||||
LackRecord lackRecord = checkMethodsMatched(methodsWithAnnotation, capabilityMethods);
|
||||
if (lackRecord.hasNotEmptyRecord()) {
|
||||
sb.append(lackRecord.toLackErrorMsg(core.getAnnotation(CapabilityCore.class).value()));
|
||||
}
|
||||
}
|
||||
if (!sb.isEmpty()) {
|
||||
throw new UnMatchedCapabilityMethodException(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private LackRecord checkMethodsMatched(List<Method> methodsWithAnnotation, List<Method> capabilityMethods) {
|
||||
Set<String> collectedMethodsWithAnnotation = methodsWithAnnotation.stream()
|
||||
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
||||
.map(CapabilityUtil::methodSignature)
|
||||
.collect(Collectors.toSet());
|
||||
Set<String> collectedCapabilityMethods = capabilityMethods.stream()
|
||||
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
||||
.map(CapabilityUtil::methodSignature)
|
||||
.collect(Collectors.toSet());
|
||||
return checkMethodsMatched(collectedMethodsWithAnnotation, collectedCapabilityMethods);
|
||||
}
|
||||
|
||||
private LackRecord checkMethodsMatched(Set<String> collectedMethodsWithAnnotation, Set<String> collectedCapabilityMethods) {
|
||||
List<String> coreLack = new ArrayList<>();
|
||||
List<String> capLack = new ArrayList<>();
|
||||
// 找出 core 中多余的方法
|
||||
for (String coreSig : collectedMethodsWithAnnotation) {
|
||||
if (!collectedCapabilityMethods.contains(coreSig)) {
|
||||
capLack.add(coreSig);
|
||||
}
|
||||
}
|
||||
// 找出 capability 中多余的方法
|
||||
for (String capSig : collectedCapabilityMethods) {
|
||||
if (!collectedMethodsWithAnnotation.contains(capSig)) {
|
||||
coreLack.add(capSig);
|
||||
}
|
||||
}
|
||||
return new LackRecord(coreLack, capLack);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private HashMap<String, List<Method>> getCapabilityMethods(Set<Class<?>> capabilities) {
|
||||
HashMap<String, List<Method>> capabilityMethods = new HashMap<>();
|
||||
capabilities.forEach(capability -> {
|
||||
capabilityMethods.put(capability.getAnnotation(Capability.class).value(), Arrays.stream(capability.getMethods()).toList());
|
||||
});
|
||||
return capabilityMethods;
|
||||
}
|
||||
|
||||
private void checkCountAndCapabilities() {
|
||||
if (cores.size() != capabilities.size()) {
|
||||
throw new UnMatchedCapabilityException("Capability 注册异常: 已存在的CapabilityCore与Capability数量不匹配!");
|
||||
}
|
||||
if (!checkValuesMatched(cores, capabilities)) {
|
||||
throw new UnMatchedCapabilityException("Capability 注册异常: 已存在的CapabilityCore与Capability不匹配!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean checkValuesMatched(Set<Class<?>> cores, Set<Class<?>> capabilities) {
|
||||
Set<String> coresValues = new HashSet<>();
|
||||
Set<String> capabilitiesValues = new HashSet<>();
|
||||
for (Class<?> core : cores) {
|
||||
CapabilityCore annotation = core.getAnnotation(CapabilityCore.class);
|
||||
if (annotation != null) {
|
||||
if (coresValues.contains(annotation.value())) {
|
||||
throw new DuplicateCapabilityException(String.format("Capability 注册异常: 重复的Capability核心: %s", annotation.value()));
|
||||
}
|
||||
coresValues.add(annotation.value());
|
||||
}
|
||||
}
|
||||
for (Class<?> capability : capabilities) {
|
||||
Capability annotation = capability.getAnnotation(Capability.class);
|
||||
if (annotation != null) {
|
||||
if (capabilitiesValues.contains(annotation.value())) {
|
||||
throw new DuplicateCapabilityException(String.format("Capability 注册异常: 重复的Capability接口: %s", annotation.value()));
|
||||
}
|
||||
capabilitiesValues.add(annotation.value());
|
||||
}
|
||||
}
|
||||
return coresValues.equals(capabilitiesValues);
|
||||
}
|
||||
|
||||
public void registerModule(CapabilityHolder capabilityHolder) {
|
||||
capabilityHolderInstances.put(capabilityHolder.getClass(), capabilityHolder);
|
||||
}
|
||||
|
||||
record LackRecord(List<String> coreLack, List<String> capLack) {
|
||||
public boolean hasNotEmptyRecord() {
|
||||
return !coreLack.isEmpty() || !capLack.isEmpty();
|
||||
}
|
||||
|
||||
public String toLackErrorMsg(String capabilityName) {
|
||||
StringBuilder sb = new StringBuilder("\n").append(capabilityName).append("\n");
|
||||
if (!coreLack.isEmpty()) {
|
||||
sb.append("缺少Core方法:").append("\n").append(coreLack).append("\n");
|
||||
}
|
||||
if (!capLack.isEmpty()) {
|
||||
sb.append("缺少Capability方法:").append("\n").append(capLack).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package work.slhaf.demo.capability.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 用于注解能力接口,需要与`@CapabilityCore`对应的`value`一致
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface Capability {
|
||||
String value();
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package work.slhaf.demo.capability.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 用于注解Core服务,需标识一个value致用于核心服务发现
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface CapabilityCore {
|
||||
String value();
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package work.slhaf.demo.capability.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 用于标注协调方法,`value`值需与对应的`@ToCoordinated`保持一致
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Coordinated {
|
||||
String capability();
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package work.slhaf.demo.capability.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 用于注入`Capability`
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface InjectCapability {
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package work.slhaf.demo.capability.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 当`@Capability`所注接口中,如果存在方法需要协调多个Core服务的调用,可以通过该注解进行排除
|
||||
* value值为方法对应标识,需与协调实现处的方法标识保持一致
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ToCoordinated {
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class CapabilityCheckFailedException extends RuntimeException {
|
||||
public CapabilityCheckFailedException(String message) {
|
||||
super("Capability注册失败: " + message);
|
||||
}
|
||||
|
||||
public CapabilityCheckFailedException(String message, Throwable cause) {
|
||||
super("Capability注册失败: " + message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class CoreInstancesCreateFailedException extends FactoryExecuteFailedException{
|
||||
public CoreInstancesCreateFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CoreInstancesCreateFailedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class DuplicateCapabilityException extends CapabilityCheckFailedException{
|
||||
public DuplicateCapabilityException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DuplicateCapabilityException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class DuplicateMethodException extends CapabilityCheckFailedException{
|
||||
public DuplicateMethodException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DuplicateMethodException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class EmptyCapabilityHolderException extends CapabilityCheckFailedException{
|
||||
public EmptyCapabilityHolderException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public EmptyCapabilityHolderException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class FactoryExecuteFailedException extends RuntimeException {
|
||||
public FactoryExecuteFailedException(String message) {
|
||||
super("CapabilityRegisterFactory 执行失败: " + message);
|
||||
}
|
||||
|
||||
public FactoryExecuteFailedException(String message, Throwable cause) {
|
||||
super("CapabilityRegisterFactory 执行失败: " + message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class ProxySetFailedException extends FactoryExecuteFailedException{
|
||||
public ProxySetFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ProxySetFailedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class UnMatchedCapabilityException extends CapabilityCheckFailedException{
|
||||
public UnMatchedCapabilityException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public UnMatchedCapabilityException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class UnMatchedCapabilityMethodException extends CapabilityCheckFailedException{
|
||||
public UnMatchedCapabilityMethodException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public UnMatchedCapabilityMethodException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package work.slhaf.demo.capability.exception;
|
||||
|
||||
public class UnMatchedCoordinatedMethodException extends CapabilityCheckFailedException{
|
||||
public UnMatchedCoordinatedMethodException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public UnMatchedCoordinatedMethodException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package work.slhaf.demo.capability.module;
|
||||
|
||||
import work.slhaf.demo.capability.CapabilityRegisterFactory;
|
||||
|
||||
public abstract class CapabilityHolder {
|
||||
protected CapabilityHolder(){
|
||||
CapabilityRegisterFactory.getInstance().registerModule(this);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package work.slhaf.partner.common.exception_handler.pojo;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCore;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.core.session.SessionManager;
|
||||
|
||||
@@ -20,7 +20,7 @@ public class GlobalException extends RuntimeException {
|
||||
this.data = new GlobalExceptionData();
|
||||
this.data.setExceptionTime(System.currentTimeMillis());
|
||||
this.data.setSessionManager(SessionManager.getInstance());
|
||||
this.data.setCognationManager(CognationManager.getInstance());
|
||||
this.data.setCognationCore(CognationCore.getInstance());
|
||||
this.data.setContext(InteractionContext.getInstance());
|
||||
} catch (Exception e) {
|
||||
log.error("[GlobalException] 捕获异常, 获取数据失败");
|
||||
|
||||
@@ -3,7 +3,7 @@ package work.slhaf.partner.common.exception_handler.pojo;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.common.serialize.PersistableObject;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCore;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.core.session.SessionManager;
|
||||
|
||||
@@ -21,6 +21,6 @@ public class GlobalExceptionData extends PersistableObject {
|
||||
|
||||
protected HashMap<String, InteractionContext> context;
|
||||
protected SessionManager sessionManager;
|
||||
protected CognationManager cognationManager;
|
||||
protected CognationCore cognationCore;
|
||||
protected Long exceptionTime;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package work.slhaf.partner.core;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.capability.CapabilityRegisterFactory;
|
||||
import work.slhaf.partner.common.exception_handler.GlobalExceptionHandler;
|
||||
import work.slhaf.partner.common.exception_handler.pojo.GlobalException;
|
||||
import work.slhaf.partner.core.interaction.agent_interface.TaskCallback;
|
||||
@@ -33,7 +32,6 @@ public class InteractionHub {
|
||||
interactionHub = new InteractionHub();
|
||||
//加载模块
|
||||
interactionHub.setInteractionModules(InteractionModulesLoader.getInstance().registerInteractionModules());
|
||||
CapabilityRegisterFactory.getInstance().registerCapabilities("work.slhaf.partner");
|
||||
log.info("InteractionHub注册完毕...");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package work.slhaf.partner.core.cognation;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.capability.BaseCoordinateManager;
|
||||
import work.slhaf.partner.api.capability.annotation.CoordinateManager;
|
||||
import work.slhaf.partner.api.capability.annotation.Coordinated;
|
||||
import work.slhaf.partner.common.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.common.exception_handler.GlobalExceptionHandler;
|
||||
@@ -28,15 +27,15 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import static work.slhaf.partner.common.util.ExtractUtil.extractUserId;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Slf4j
|
||||
public class CognationManager extends BaseCoordinateManager implements Serializable {
|
||||
@CoordinateManager
|
||||
public class CoordinatedManager implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static volatile CognationManager cognationManager;
|
||||
private static volatile CoordinatedManager coordinatedManager;
|
||||
private final Lock sliceInsertLock = new ReentrantLock();
|
||||
|
||||
private CognationCore cognationCore;
|
||||
@@ -45,22 +44,22 @@ public class CognationManager extends BaseCoordinateManager implements Serializa
|
||||
private PerceiveCore perceiveCore;
|
||||
private DispatchCore dispatchCore;
|
||||
|
||||
private CognationManager() {
|
||||
private CoordinatedManager() {
|
||||
}
|
||||
|
||||
|
||||
public static CognationManager getInstance() throws IOException, ClassNotFoundException {
|
||||
if (cognationManager == null) {
|
||||
synchronized (CognationManager.class) {
|
||||
if (cognationManager == null) {
|
||||
cognationManager = new CognationManager();
|
||||
cognationManager.setCognationCore(CognationCore.getInstance());
|
||||
cognationManager.setCores();
|
||||
log.info("[CognationManager] MemoryManager注册完毕...");
|
||||
public static CoordinatedManager getInstance() throws IOException, ClassNotFoundException {
|
||||
if (coordinatedManager == null) {
|
||||
synchronized (CoordinatedManager.class) {
|
||||
if (coordinatedManager == null) {
|
||||
coordinatedManager = new CoordinatedManager();
|
||||
coordinatedManager.setCognationCore(CognationCore.getInstance());
|
||||
coordinatedManager.setCores();
|
||||
log.info("[CoordinatedManager] MemoryManager注册完毕...");
|
||||
}
|
||||
}
|
||||
}
|
||||
return cognationManager;
|
||||
return coordinatedManager;
|
||||
}
|
||||
|
||||
private void setCores() {
|
||||
@@ -88,9 +87,9 @@ public class CognationManager extends BaseCoordinateManager implements Serializa
|
||||
//尝试更新缓存
|
||||
cacheCore.updateCache(topicPath, memoryResult);
|
||||
} catch (Exception e) {
|
||||
log.error("[CognationManager] selectMemory error: ", e);
|
||||
log.error("[CognationManager] 路径: {}", topicPathStr);
|
||||
log.error("[CognationManager] 主题树: {}", memoryCore.getTopicTree());
|
||||
log.error("[CoordinatedManager] selectMemory error: ", e);
|
||||
log.error("[CoordinatedManager] 路径: {}", topicPathStr);
|
||||
log.error("[CoordinatedManager] 主题树: {}", memoryCore.getTopicTree());
|
||||
memoryResult = new MemoryResult();
|
||||
memoryResult.setRelatedMemorySliceResult(new ArrayList<>());
|
||||
memoryResult.setMemorySliceResult(new CopyOnWriteArrayList<>());
|
||||
@@ -131,10 +130,10 @@ public class CognationManager extends BaseCoordinateManager implements Serializa
|
||||
cacheCore.updateUserDialogMap(memorySlice);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[CognationManager] 插入记忆时出错: ", e);
|
||||
log.error("[CoordinatedManager] 插入记忆时出错: ", e);
|
||||
GlobalExceptionHandler.writeExceptionState(new GlobalException("插入记忆时出错: " + e.getLocalizedMessage()));
|
||||
}
|
||||
log.debug("[CognationManager] 插入切片: {}, 路径: {}", memorySlice, topicPath);
|
||||
log.debug("[CoordinatedManager] 插入切片: {}, 路径: {}", memorySlice, topicPath);
|
||||
sliceInsertLock.unlock();
|
||||
}
|
||||
|
||||
@@ -62,6 +62,8 @@ public class CognationCore extends PersistableObject {
|
||||
|
||||
private void connectCores(CognationCore temp) {
|
||||
temp.setCacheCore(CacheCore.getInstance());
|
||||
temp.setMemoryCore(MemoryCore.getInstance());
|
||||
temp.setPerceiveCore(PerceiveCore.getInstance());
|
||||
}
|
||||
|
||||
private void setupHook(CognationCore temp) {
|
||||
@@ -138,7 +140,7 @@ public class CognationCore extends PersistableObject {
|
||||
|
||||
public void updateActivatedSlices(String userId, List<EvaluatedSlice> memorySlices) {
|
||||
activeData.updateActivatedSlices(userId, memorySlices);
|
||||
log.debug("[CognationManager] 已更新激活切片, userId: {}", userId);
|
||||
log.debug("[CoordinatedManager] 已更新激活切片, userId: {}", userId);
|
||||
}
|
||||
|
||||
public String getActivatedSlicesStr(String userId) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package work.slhaf.partner.core.cognation.submodule.cache;
|
||||
|
||||
import work.slhaf.partner.api.capability.annotation.Capability;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityMethod;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package work.slhaf.partner.core.cognation.submodule.memory;
|
||||
|
||||
import work.slhaf.partner.api.capability.annotation.Capability;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.api.capability.annotation.ToCoordinated;
|
||||
import work.slhaf.partner.core.cognation.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package work.slhaf.partner.core.cognation.submodule.perceive;
|
||||
|
||||
import work.slhaf.partner.api.capability.annotation.Capability;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.pojo.User;
|
||||
|
||||
@Capability(value = "perceive")
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.util.List;
|
||||
public interface ActivateModel {
|
||||
|
||||
default void modelSettings() {
|
||||
Model model = getModel();
|
||||
Model model = new Model();
|
||||
ModelConfig modelConfig = ModelConfig.load(modelKey());
|
||||
model.setBaseMessages(withAwareness() ? ResourcesUtil.Prompt.loadPromptWithSelfAwareness(modelKey(), promptModule()) : ResourcesUtil.Prompt.loadPrompt(modelKey(), promptModule()));
|
||||
model.setChatClient(new ChatClient(modelConfig.getBaseUrl(), modelConfig.getApikey(), modelConfig.getModel()));
|
||||
|
||||
@@ -2,17 +2,12 @@ package work.slhaf.partner.module.common.model;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.common.chat.ChatClient;
|
||||
import work.slhaf.partner.common.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.common.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.common.chat.pojo.Message;
|
||||
import work.slhaf.partner.common.config.ModelConfig;
|
||||
import work.slhaf.partner.common.util.ResourcesUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public abstract class Model {
|
||||
public class Model {
|
||||
|
||||
protected ChatClient chatClient;
|
||||
protected List<Message> chatMessages;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package work.slhaf.partner.module.common.module;
|
||||
|
||||
import work.slhaf.partner.core.interaction.module.InteractionFlow;
|
||||
|
||||
public abstract class CoreModule extends InteractionModule {
|
||||
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ package work.slhaf.partner.module.common.module;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import work.slhaf.partner.api.capability.module.CapabilityHolder;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityHolder;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
|
||||
public abstract class Module extends CapabilityHolder {
|
||||
@CapabilityHolder
|
||||
public abstract class Module {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package work.slhaf.partner.module.common.module;
|
||||
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.core.interaction.module.InteractionFlow;
|
||||
import work.slhaf.partner.module.common.entity.AppendPromptData;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -5,7 +5,6 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.cognation.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.cognation.submodule.cache.CacheCapability;
|
||||
|
||||
@@ -10,14 +10,12 @@ import work.slhaf.partner.common.chat.pojo.Message;
|
||||
import work.slhaf.partner.common.chat.pojo.MetaMessage;
|
||||
import work.slhaf.partner.common.exception_handler.GlobalExceptionHandler;
|
||||
import work.slhaf.partner.common.exception_handler.pojo.GlobalException;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.memory.pojo.EvaluatedSlice;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.core.session.SessionManager;
|
||||
import work.slhaf.partner.module.common.model.ActivateModel;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.common.module.SubModule;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.data.ExtractorInput;
|
||||
|
||||
@@ -8,14 +8,12 @@ import work.slhaf.partner.api.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.common.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.common.chat.pojo.Message;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.cache.CacheCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.core.interaction.module.InteractionFlow;
|
||||
import work.slhaf.partner.core.session.SessionManager;
|
||||
import work.slhaf.partner.module.common.module.PostModule;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.MemorySelectExtractor;
|
||||
|
||||
@@ -7,7 +7,6 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.common.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.module.common.model.ActivateModel;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.common.module.SubModule;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.data.SummarizeInput;
|
||||
|
||||
@@ -9,7 +9,6 @@ import work.slhaf.partner.common.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.common.chat.pojo.Message;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.module.common.model.ActivateModel;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.common.module.SubModule;
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.common.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.module.common.model.ActivateModel;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.common.module.SubModule;
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package work.slhaf.partner.module.modules.perceive.selector;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.pojo.User;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
|
||||
@@ -5,14 +5,10 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.pojo.User;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.core.interaction.module.InteractionFlow;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.common.module.PostModule;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.RelationExtractor;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.pojo.RelationExtractResult;
|
||||
|
||||
@@ -5,13 +5,11 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.common.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.common.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.pojo.User;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.module.common.model.ActivateModel;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.common.module.SubModule;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.pojo.RelationExtractInput;
|
||||
|
||||
@@ -6,12 +6,10 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.common.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.module.common.model.ActivateModel;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.common.module.SubModule;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.static_extractor.data.StaticMemoryExtractInput;
|
||||
|
||||
@@ -4,10 +4,8 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.core.cognation.CognationManager;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.interaction.data.context.InteractionContext;
|
||||
import work.slhaf.partner.core.interaction.module.InteractionFlow;
|
||||
import work.slhaf.partner.module.common.module.PostModule;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package work.slhaf.partner.module.modules.process;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityHolder;
|
||||
import work.slhaf.partner.api.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.capability.module.CapabilityHolder;
|
||||
import work.slhaf.partner.core.cognation.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.cognation.submodule.perceive.pojo.User;
|
||||
@@ -18,10 +17,10 @@ import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Slf4j
|
||||
public class PreprocessExecutor extends CapabilityHolder {
|
||||
@CapabilityHolder
|
||||
public class PreprocessExecutor {
|
||||
|
||||
private static volatile PreprocessExecutor preprocessExecutor;
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package work.slhaf.partner.module.modules.task;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.module.common.model.Model;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
|
||||
@Data
|
||||
public class TaskEvaluator {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<version>0.5.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>Partner-Capability-Demo</artifactId>
|
||||
<artifactId>Partner-Test-Demo</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -17,6 +17,11 @@
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.10.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>work.slhaf</groupId>
|
||||
<artifactId>Partner-Api</artifactId>
|
||||
<version>0.5.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
10
Partner-Test-Demo/src/main/java/work/slhaf/Main.java
Normal file
10
Partner-Test-Demo/src/main/java/work/slhaf/Main.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package work.slhaf;
|
||||
|
||||
import work.slhaf.demo.TestModule;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
TestModule testModule = new TestModule();
|
||||
testModule.execute();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package work.slhaf.demo;
|
||||
|
||||
import work.slhaf.demo.capability.BaseCognationManager;
|
||||
import work.slhaf.demo.capability.annotation.Coordinated;
|
||||
|
||||
import work.slhaf.partner.api.capability.annotation.CoordinateManager;
|
||||
import work.slhaf.partner.api.capability.annotation.Coordinated;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyCognationManager extends BaseCognationManager {
|
||||
@CoordinateManager
|
||||
public class MyCognationManager {
|
||||
|
||||
@Coordinated(capability = "memory")
|
||||
public List<String> selectMemory(String path) {
|
||||
@@ -1,10 +1,11 @@
|
||||
package work.slhaf.demo;
|
||||
|
||||
import work.slhaf.demo.ability.MemoryCapability;
|
||||
import work.slhaf.demo.capability.annotation.InjectCapability;
|
||||
import work.slhaf.demo.capability.module.CapabilityHolder;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityHolder;
|
||||
import work.slhaf.partner.api.capability.annotation.InjectCapability;
|
||||
|
||||
public class TestModule extends CapabilityHolder {
|
||||
@CapabilityHolder
|
||||
public class TestModule {
|
||||
@InjectCapability
|
||||
private MemoryCapability capability;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package work.slhaf.demo.ability;
|
||||
|
||||
|
||||
import work.slhaf.demo.capability.annotation.Capability;
|
||||
|
||||
import work.slhaf.partner.api.capability.annotation.Capability;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
@@ -1,7 +1,8 @@
|
||||
package work.slhaf.demo.ability;
|
||||
|
||||
import work.slhaf.demo.capability.annotation.Capability;
|
||||
import work.slhaf.demo.capability.annotation.ToCoordinated;
|
||||
|
||||
import work.slhaf.partner.api.capability.annotation.Capability;
|
||||
import work.slhaf.partner.api.capability.annotation.ToCoordinated;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package work.slhaf.demo.ability;
|
||||
|
||||
import work.slhaf.demo.capability.annotation.Capability;
|
||||
|
||||
import work.slhaf.partner.api.capability.annotation.Capability;
|
||||
|
||||
@Capability(value = "perceive")
|
||||
public interface PerceiveCapability {
|
||||
@@ -1,8 +1,8 @@
|
||||
package work.slhaf.demo.core;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.demo.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.demo.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityMethod;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
@@ -1,8 +1,8 @@
|
||||
package work.slhaf.demo.core;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.demo.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.demo.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityMethod;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -1,8 +1,8 @@
|
||||
package work.slhaf.demo.core;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.demo.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.demo.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.partner.api.capability.annotation.CapabilityMethod;
|
||||
|
||||
@CapabilityCore(value = "perceive")
|
||||
@Slf4j
|
||||
Reference in New Issue
Block a user