mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(framework): unify model invocation result and exception handling
This commit is contained in:
@@ -30,7 +30,7 @@ public class ActionCorrectionRecognizer extends AbstractAgentModule.Sub<Correcti
|
||||
resolveContextMessage(),
|
||||
resolveTaskMessage(input)
|
||||
);
|
||||
return formattedChat(messages, CorrectionRecognizerResult.class);
|
||||
return formattedChat(messages, CorrectionRecognizerResult.class).getOrThrow();
|
||||
}
|
||||
|
||||
private Message resolveTaskMessage(CorrectionRecognizerInput input) {
|
||||
|
||||
@@ -30,7 +30,7 @@ public class ActionCorrector extends AbstractAgentModule.Sub<CorrectorInput, Cor
|
||||
resolveContextMessage(),
|
||||
resolveTaskMessage(input)
|
||||
);
|
||||
return formattedChat(messages, CorrectorResult.class);
|
||||
return formattedChat(messages, CorrectorResult.class).getOrThrow();
|
||||
}
|
||||
|
||||
private Message resolveTaskMessage(CorrectorInput input) {
|
||||
|
||||
@@ -11,6 +11,7 @@ import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCa
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
import work.slhaf.partner.module.TaskBlock;
|
||||
import work.slhaf.partner.module.action.executor.entity.ExtractorInput;
|
||||
import work.slhaf.partner.module.action.executor.entity.ExtractorResult;
|
||||
@@ -28,20 +29,19 @@ public class ParamsExtractor extends AbstractAgentModule.Sub<ExtractorInput, Ext
|
||||
|
||||
@Override
|
||||
public ExtractorResult execute(ExtractorInput input) {
|
||||
ExtractorResult result;
|
||||
try {
|
||||
List<Message> messages = List.of(
|
||||
resolveContextMessage(),
|
||||
resolveTaskMessage(input)
|
||||
);
|
||||
result = formattedChat(messages, ExtractorResult.class);
|
||||
} catch (Exception e) {
|
||||
log.error("ParamsExtractor解析结果失败", e);
|
||||
result = new ExtractorResult();
|
||||
result.setOk(false);
|
||||
result.setParams(new HashMap<>());
|
||||
List<Message> messages = List.of(
|
||||
resolveContextMessage(),
|
||||
resolveTaskMessage(input)
|
||||
);
|
||||
Result<ExtractorResult> result = formattedChat(messages, ExtractorResult.class);
|
||||
if (result.isFailure()) {
|
||||
log.error("ParamsExtractor解析结果失败", result.exceptionOrNull());
|
||||
ExtractorResult fallback = new ExtractorResult();
|
||||
fallback.setOk(false);
|
||||
fallback.setParams(new HashMap<>());
|
||||
return fallback;
|
||||
}
|
||||
return result;
|
||||
return result.getOrThrow();
|
||||
}
|
||||
|
||||
private Message resolveTaskMessage(ExtractorInput input) {
|
||||
|
||||
@@ -15,6 +15,7 @@ import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAg
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
import work.slhaf.partner.module.action.planner.evaluator.entity.EvaluatorInput;
|
||||
import work.slhaf.partner.module.action.planner.evaluator.entity.EvaluatorResult;
|
||||
|
||||
@@ -61,10 +62,15 @@ public class ActionEvaluator extends AbstractAgentModule.Sub<EvaluatorInput, Lis
|
||||
availableMetaActionContext(),
|
||||
new Message(Message.Character.USER, tendency)
|
||||
);
|
||||
EvaluatorResult evaluatorResult = formattedChat(
|
||||
Result<EvaluatorResult> result = formattedChat(
|
||||
messages,
|
||||
EvaluatorResult.class
|
||||
);
|
||||
if (result.isFailure()) {
|
||||
log.error("ActionEvaluator评估失败: {}", tendency, result.exceptionOrNull());
|
||||
return;
|
||||
}
|
||||
EvaluatorResult evaluatorResult = result.getOrThrow();
|
||||
evaluatorResult.setTendency(tendency);
|
||||
synchronized (evaluatorResults) {
|
||||
evaluatorResults.add(evaluatorResult);
|
||||
|
||||
@@ -7,6 +7,7 @@ import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCa
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
import work.slhaf.partner.module.action.planner.extractor.entity.ExtractorResult;
|
||||
|
||||
import java.util.List;
|
||||
@@ -18,23 +19,18 @@ public class ActionExtractor extends AbstractAgentModule.Sub<String, ExtractorRe
|
||||
|
||||
@Override
|
||||
public ExtractorResult execute(String input) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
List<Message> messages = List.of(
|
||||
cognitionCapability.contextWorkspace().resolve(List.of(
|
||||
ContextBlock.VisibleDomain.COGNITION,
|
||||
ContextBlock.VisibleDomain.ACTION
|
||||
)).encodeToMessage(),
|
||||
new Message(Message.Character.USER, input)
|
||||
);
|
||||
return formattedChat(
|
||||
messages,
|
||||
ExtractorResult.class
|
||||
);
|
||||
} catch (Exception e) {
|
||||
log.error("提取信息出错", e);
|
||||
}
|
||||
List<Message> messages = List.of(
|
||||
cognitionCapability.contextWorkspace().resolve(List.of(
|
||||
ContextBlock.VisibleDomain.COGNITION,
|
||||
ContextBlock.VisibleDomain.ACTION
|
||||
)).encodeToMessage(),
|
||||
new Message(Message.Character.USER, input)
|
||||
);
|
||||
Result<ExtractorResult> result = formattedChat(messages, ExtractorResult.class);
|
||||
if (result.isSuccess()) {
|
||||
return result.getOrThrow();
|
||||
}
|
||||
log.error("提取信息出错", result.exceptionOrNull());
|
||||
return new ExtractorResult();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import work.slhaf.partner.framework.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
import work.slhaf.partner.framework.agent.model.StreamChatMessageConsumer;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
import work.slhaf.partner.runtime.PartnerRunningFlowContext;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
@@ -28,6 +29,8 @@ import java.util.stream.Collectors;
|
||||
@Data
|
||||
public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRunningFlowContext> implements ActivateModel {
|
||||
|
||||
private static final String INTERRUPTED_MARKER = " [response interrupted due to internal exception]";
|
||||
|
||||
private static final String MODULE_PROMPT = """
|
||||
你是 Partner 的表达模块。
|
||||
你接下来收到的消息固定分为三个区段:
|
||||
@@ -64,7 +67,11 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
|
||||
private void executeChat(PartnerRunningFlowContext runningFlowContext) {
|
||||
StreamChatMessageConsumer consumer = ReplyDispatcher.INSTANCE.createConsumer(runningFlowContext.getTarget());
|
||||
this.streamChat(buildChatMessages(runningFlowContext), consumer);
|
||||
Result<kotlin.Unit> result = this.streamChat(buildChatMessages(runningFlowContext), consumer);
|
||||
if (result.isFailure()) {
|
||||
log.error("Streaming response failed", result.exceptionOrNull());
|
||||
consumer.onDelta(INTERRUPTED_MARKER);
|
||||
}
|
||||
updateChatMessages(runningFlowContext, consumer.collectResponse());
|
||||
updateContext();
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class DialogRollingService extends AbstractAgentModule.Standalone impleme
|
||||
List<Message> messages = List.of(
|
||||
resolveTaskBlock(snapshotMessages)
|
||||
);
|
||||
return chat(messages);
|
||||
return chat(messages).getOrThrow();
|
||||
}
|
||||
|
||||
private @NotNull BlockContent buildDialogAbstractBlock(String summary, @Nullable String unitId, @Nullable String sliceId) {
|
||||
|
||||
@@ -15,6 +15,7 @@ import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAg
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
import work.slhaf.partner.module.TaskBlock;
|
||||
import work.slhaf.partner.module.memory.selector.ActivatedMemorySlice;
|
||||
import work.slhaf.partner.module.memory.selector.evaluator.entity.EvaluatorBatchInput;
|
||||
@@ -64,13 +65,16 @@ public class SliceSelectEvaluator extends AbstractAgentModule.Sub<EvaluatorInput
|
||||
contextMessage,
|
||||
resolveTaskMessage(batchInput)
|
||||
);
|
||||
EvaluatorBatchResult batchResult = formattedChat(messages, EvaluatorBatchResult.class);
|
||||
if (batchResult.isPassed()) {
|
||||
Result<EvaluatorBatchResult> batchResult = formattedChat(messages, EvaluatorBatchResult.class);
|
||||
if (batchResult.isFailure()) {
|
||||
log.debug("切片评估失败,已跳过当前切片", batchResult.exceptionOrNull());
|
||||
return;
|
||||
}
|
||||
if (batchResult.getOrThrow().isPassed()) {
|
||||
synchronized (result) {
|
||||
result.add(slice);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAg
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.InjectModule;
|
||||
import work.slhaf.partner.framework.agent.model.ActivateModel;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
import work.slhaf.partner.module.TaskBlock;
|
||||
import work.slhaf.partner.module.memory.runtime.MemoryRuntime;
|
||||
import work.slhaf.partner.module.memory.selector.extractor.entity.ExtractorInput;
|
||||
@@ -34,18 +35,19 @@ public class MemorySelectExtractor extends AbstractAgentModule.Sub<ExtractorInpu
|
||||
public ExtractorResult execute(ExtractorInput input) {
|
||||
log.debug("[MemorySelectExtractor] 主题提取模块开始...");
|
||||
ExtractorResult extractorResult;
|
||||
try {
|
||||
List<Message> messages = List.of(
|
||||
resolveContextMessage(),
|
||||
resolveTaskMessage(input)
|
||||
);
|
||||
extractorResult = formattedChat(
|
||||
messages,
|
||||
ExtractorResult.class
|
||||
);
|
||||
List<Message> messages = List.of(
|
||||
resolveContextMessage(),
|
||||
resolveTaskMessage(input)
|
||||
);
|
||||
Result<ExtractorResult> result = formattedChat(
|
||||
messages,
|
||||
ExtractorResult.class
|
||||
);
|
||||
if (result.isSuccess()) {
|
||||
extractorResult = result.getOrThrow();
|
||||
log.debug("[MemorySelectExtractor] 主题提取结果: {}", extractorResult);
|
||||
} catch (Exception e) {
|
||||
log.error("[MemorySelectExtractor] 主题提取出错: ", e);
|
||||
} else {
|
||||
log.error("[MemorySelectExtractor] 主题提取出错: ", result.exceptionOrNull());
|
||||
extractorResult = new ExtractorResult();
|
||||
extractorResult.setRecall(false);
|
||||
extractorResult.setMatches(List.of());
|
||||
|
||||
@@ -28,7 +28,7 @@ public class MultiSummarizer extends AbstractAgentModule.Sub<SummarizeInput, Sum
|
||||
SummarizeResult result = formattedChat(
|
||||
List.of(new Message(Message.Character.USER, JSONUtil.toJsonPrettyStr(input))),
|
||||
SummarizeResult.class
|
||||
);
|
||||
).getOrThrow();
|
||||
log.debug("[MemorySummarizer] 整体摘要结果: {}", JSONObject.toJSONString(result));
|
||||
return fix(result);
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class SingleSummarizer extends AbstractAgentModule.Sub<List<Message>, Voi
|
||||
|
||||
private String singleExecute(String primaryContent) {
|
||||
try {
|
||||
return chat(List.of(new Message(Message.Character.USER, primaryContent)));
|
||||
return chat(List.of(new Message(Message.Character.USER, primaryContent))).getOrThrow();
|
||||
} catch (Exception e) {
|
||||
log.error("[SingleSummarizer] 单消息总结出错: ", e);
|
||||
return primaryContent;
|
||||
|
||||
@@ -17,7 +17,7 @@ public class TotalSummarizer extends AbstractAgentModule.Sub<HashMap<String, Str
|
||||
return formattedChat(
|
||||
List.of(new Message(Message.Character.USER, JSONUtil.toJsonPrettyStr(singleMemorySummary))),
|
||||
SummaryContent.class
|
||||
).getContent();
|
||||
).getOrThrow().getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user