refactor(agent): normalize Agent launch flow

This commit is contained in:
2026-04-09 12:26:52 +08:00
parent d6593c10f9
commit 56daaf0b60

View File

@@ -1,5 +1,6 @@
package work.slhaf.partner.framework.agent;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import work.slhaf.partner.framework.agent.config.ConfigCenter;
import work.slhaf.partner.framework.agent.exception.AgentLaunchFailedException;
@@ -21,35 +22,44 @@ import java.util.Set;
@Slf4j
public final class Agent {
public static AgentStep newAgent(Class<?> clazz) {
if (clazz == null) {
throw new AgentLaunchFailedException("Agent class 和 interaction flow context 不能为 null");
}
public static AgentApp newAgent(@NonNull Class<?> clazz) {
return new AgentApp(clazz);
}
public interface AgentStep {
AgentStep addGatewayRegistration(AgentGatewayRegistration... registrations);
void launch();
}
public static class AgentApp implements AgentStep {
public static class AgentApp {
private final Class<?> applicationClass;
private final Set<AgentGatewayRegistration> gatewayRegistrations = new LinkedHashSet<>();
private final Set<LifecycleHook> preShutdownHooks = new LinkedHashSet<>();
private final Set<LifecycleHook> postShutdownHooks = new LinkedHashSet<>();
private AgentApp(Class<?> clazz) {
this.applicationClass = clazz;
}
@Override
public AgentStep addGatewayRegistration(AgentGatewayRegistration... registrations) {
public AgentApp addGatewayRegistration(AgentGatewayRegistration... registrations) {
this.gatewayRegistrations.addAll(Set.of(registrations));
return this;
}
@Override
public AgentApp addPreShutdownHook(String name, Runnable action) {
return addPreShutdownHook(name, 0, action);
}
public AgentApp addPreShutdownHook(String name, int order, Runnable action) {
this.preShutdownHooks.add(new LifecycleHook(name, order, action));
return this;
}
public AgentApp addPostShutdownHook(String name, Runnable action) {
return addPostShutdownHook(name, 0, action);
}
public AgentApp addPostShutdownHook(String name, int order, Runnable action) {
this.postShutdownHooks.add(new LifecycleHook(name, order, action));
return this;
}
public void launch() {
try {
// Keep startup order explicit so registries are ready before component scanning.
@@ -59,11 +69,26 @@ public final class Agent {
for (AgentGatewayRegistration registration : gatewayRegistrations) {
registration.register();
}
registerShutdownHooks();
Path externalModuleDir = ConfigCenter.INSTANCE.getPaths().getResourcesDir().resolve("module");
AgentRegisterFactory.addScanDir(externalModuleDir.toString());
AgentRegisterFactory.launch(applicationClass.getPackageName());
ConfigCenter.INSTANCE.initAll();
ConfigCenter.INSTANCE.start();
} catch (Exception e) {
throw new AgentLaunchFailedException("Agent 启动失败", e);
}
}
private void registerShutdownHooks() {
AgentContext.INSTANCE.addPreShutdownHook(
"agent-gateway-registry-close",
0,
AgentGatewayRegistry.INSTANCE::close
);
preShutdownHooks.forEach(hook ->
AgentContext.INSTANCE.addPreShutdownHook(hook.name(), hook.order(), hook.action())
);
AgentContext.INSTANCE.addPostShutdownHook(
"state-center-save",
0,
@@ -74,15 +99,13 @@ public final class Agent {
100,
ConfigCenter.INSTANCE::close
);
Path externalModuleDir = ConfigCenter.INSTANCE.getPaths().getResourcesDir().resolve("module");
AgentRegisterFactory.addScanDir(externalModuleDir.toString());
AgentRegisterFactory.launch(applicationClass.getPackageName());
ConfigCenter.INSTANCE.initAll();
ConfigCenter.INSTANCE.start();
} catch (Exception e) {
throw new AgentLaunchFailedException("Agent 启动失败", e);
}
postShutdownHooks.forEach(hook ->
AgentContext.INSTANCE.addPostShutdownHook(hook.name(), hook.order(), hook.action())
);
}
}
private record LifecycleHook(String name, int order, Runnable action) {
}
}