feat(exception): support report necessary exception info into context

This commit is contained in:
2026-04-11 18:52:53 +08:00
parent 2aae1b1f14
commit fac6e24e49
2 changed files with 59 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
package work.slhaf.partner.runtime.exception;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import work.slhaf.partner.core.cognition.BlockContent;
import work.slhaf.partner.core.cognition.CognitionCapability;
import work.slhaf.partner.core.cognition.ContextBlock;
import work.slhaf.partner.framework.agent.exception.AgentException;
import work.slhaf.partner.framework.agent.exception.ExceptionReport;
import work.slhaf.partner.framework.agent.exception.ExceptionReporter;
import work.slhaf.partner.framework.agent.factory.capability.annotation.InjectCapability;
import work.slhaf.partner.framework.agent.factory.component.annotation.AgentComponent;
import work.slhaf.partner.framework.agent.factory.component.annotation.Init;
import java.util.Set;
@AgentComponent
public class ContextExceptionReporter implements ExceptionReporter {
public static final String REPORTER_NAME = "context-reporter";
@InjectCapability
private CognitionCapability cognitionCapability;
@Init
public void init() {
register();
}
@Override
@NotNull
public String reporterName() {
return REPORTER_NAME;
}
@Override
public void report(@NotNull AgentException exception) {
ExceptionReport report = exception.toReport();
cognitionCapability.contextWorkspace().register(new ContextBlock(
buildExceptionReportBlock(report),
Set.of(ContextBlock.VisibleDomain.COGNITION),
10,
10,
0
));
}
private @NotNull BlockContent buildExceptionReportBlock(ExceptionReport report) {
return new BlockContent("agent-runtime-exception", "context-exception-reporter") {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendTextElement(document, root, "exception_info", report.toString());
}
};
}
}