refactor(framework): make AbstractAgentModule sealed with class-based module contracts and streamline ActivateModel model access

This commit is contained in:
2026-02-20 16:12:35 +08:00
parent c3b0a9dd25
commit e00441faa8
2 changed files with 41 additions and 40 deletions

View File

@@ -3,12 +3,10 @@ package work.slhaf.partner.module.modules.core;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
import work.slhaf.partner.api.agent.factory.module.abstracts.AbstractAgentRunningModule;
import work.slhaf.partner.api.agent.factory.module.abstracts.AbstractAgentModule;
import work.slhaf.partner.api.agent.factory.module.abstracts.ActivateModel;
import work.slhaf.partner.api.agent.factory.module.annotation.CoreModule;
import work.slhaf.partner.api.agent.factory.module.annotation.Init;
import work.slhaf.partner.api.chat.ChatClient;
import work.slhaf.partner.api.chat.constant.ChatConstant;
@@ -29,9 +27,7 @@ import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
@EqualsAndHashCode(callSuper = true)
@Data
@Slf4j
@CoreModule
public class CoreModel extends AbstractAgentRunningModule<PartnerRunningFlowContext> implements ActivateModel {
public class CoreModel extends AbstractAgentModule.Running<PartnerRunningFlowContext> implements ActivateModel {
@InjectCapability
private CognationCapability cognationCapability;
@@ -252,4 +248,9 @@ public class CoreModel extends AbstractAgentRunningModule<PartnerRunningFlowCont
.build();
appendedMessages.add(startMessage);
}
@Override
public int order() {
return 5;
}
}

View File

@@ -1,5 +1,7 @@
package work.slhaf.partner.api.agent.factory.module.abstracts
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityHolder
import work.slhaf.partner.api.agent.factory.module.annotation.Init
import work.slhaf.partner.api.agent.runtime.config.AgentConfigManager
@@ -13,33 +15,46 @@ import work.slhaf.partner.api.chat.pojo.Message
* 模块基类
*/
@CapabilityHolder
abstract class AbstractAgentModule {
sealed class AbstractAgentModule {
var moduleName: String = javaClass.simpleName
interface Running<T : RunningFlowContext> {
@JvmField
val log: Logger = LoggerFactory.getLogger(javaClass.simpleName)
fun execute(context: T)
abstract class Running<T : RunningFlowContext> : AbstractAgentModule() {
fun order(): Int
abstract fun execute(context: T)
abstract fun order(): Int
}
interface Sub<I, O> {
fun execute(input: I): O
abstract class Sub<I, O> : AbstractAgentModule() {
abstract fun execute(input: I): O
}
interface Standalone
abstract class Standalone
// TODO 后续于此处扩展生命周期内容
}
interface ActivateModel {
val model: Model
get() = modelMap.computeIfAbsent(modelKey()) {
buildModel()
}
companion object {
val modelMap: MutableMap<String, Model> = mutableMapOf()
private val configManager: AgentConfigManager = AgentConfigManager.INSTANCE
}
fun getModel(): Model {
@Init(order = -1)
fun modelSettings() {
modelMap[modelKey()] = buildModel()
}
fun buildModel(): Model {
val modelConfig = configManager.loadModelConfig(modelKey())
val chatClient = ChatClient(modelConfig.baseUrl, modelConfig.apikey, modelConfig.model)
@@ -54,18 +69,6 @@ interface ActivateModel {
return model
}
val model = modelMap.computeIfAbsent(modelKey()) {
buildModel()
}
return model
}
@Init(order = -1)
fun modelSettings() {
val model = getModel()
modelMap[modelKey()] = model
}
private fun loadSpecificPromptAndBasicPrompt(modelKey: String): MutableList<Message> {
val messages: MutableList<Message> = ArrayList()
messages.addAll(configManager.loadModelPrompt("basic"))
@@ -74,7 +77,6 @@ interface ActivateModel {
}
fun chat(): ChatResponse {
val model = this.getModel()
val temp = ArrayList<Message?>()
temp.addAll(model.baseMessages)
temp.addAll(model.chatMessages)
@@ -82,14 +84,12 @@ interface ActivateModel {
}
fun singleChat(input: String): ChatResponse {
val model = this.getModel()
val temp = ArrayList<Message>(model.baseMessages)
temp.add(Message(ChatConstant.Character.USER, input))
return model.chatClient.runChat(temp)
}
fun updateChatClientSettings() {
val model = this.getModel()
model.chatClient.temperature = 0.4
model.chatClient.top_p = 0.8
}