BaseAgent only adds the completion to the conversation if a request was made by the current agent;

This commit is contained in:
extio-de
2026-03-21 18:01:51 +01:00
parent 531fd88588
commit 8869ed8d62
3 changed files with 20 additions and 7 deletions

View File

@@ -4,7 +4,7 @@
<artifactId>lm-lib</artifactId> <artifactId>lm-lib</artifactId>
<groupId>de.extio</groupId> <groupId>de.extio</groupId>
<name>lm-lib</name> <name>lm-lib</name>
<version>2.4.1</version> <version>2.4.2</version>
<properties> <properties>
<java.version>25</java.version> <java.version>25</java.version>

View File

@@ -42,6 +42,8 @@ public final class AgentContext {
private final Map<String, ? extends BaseAgent> agents; private final Map<String, ? extends BaseAgent> agents;
private volatile String currentAgentName;
private volatile AgentNext nextAgent; private volatile AgentNext nextAgent;
private volatile Completion lastCompletion; private volatile Completion lastCompletion;
@@ -260,4 +262,12 @@ public final class AgentContext {
public void setStreaming(final boolean streaming) { public void setStreaming(final boolean streaming) {
this.streaming = streaming; this.streaming = streaming;
} }
public String getCurrentAgentName() {
return currentAgentName;
}
public void setCurrentAgentName(final String currentAgentName) {
this.currentAgentName = currentAgentName;
}
} }

View File

@@ -80,6 +80,7 @@ public interface BaseAgent {
default List<AgentContext> execute(final Client client, final ExecutorService agentExecutorService, final AgentContext context) { default List<AgentContext> execute(final Client client, final ExecutorService agentExecutorService, final AgentContext context) {
final var splits = new ArrayList<Split>(); final var splits = new ArrayList<Split>();
context.setCurrentAgentName(this.name());
context.setNextAgent(null); context.setNextAgent(null);
context.clearError(); context.clearError();
@@ -94,6 +95,7 @@ public interface BaseAgent {
context.setError(AgentErrorType.GENERAL, ex); context.setError(AgentErrorType.GENERAL, ex);
return List.of(context); return List.of(context);
} }
final boolean skipCompletion = context.isSkipNextCompletion();
splits.addAll(this.applyTemplate(context)); splits.addAll(this.applyTemplate(context));
final var tasks = new ArrayList<CompletableFuture<?>>(splits.size()); final var tasks = new ArrayList<CompletableFuture<?>>(splits.size());
@@ -101,8 +103,8 @@ public interface BaseAgent {
tasks.add(CompletableFuture.runAsync(() -> { tasks.add(CompletableFuture.runAsync(() -> {
try { try {
split.context().clearError(); split.context().clearError();
final boolean skipCompletion = split.context().isSkipNextCompletion() || this.agentType(split.context()) == AgentType.PROCESSING_ONLY; final boolean skipCompletionInSplit = skipCompletion || this.agentType(split.context()) == AgentType.PROCESSING_ONLY;
if (skipCompletion) { if (skipCompletionInSplit) {
split.context().setSkipNextCompletion(false); split.context().setSkipNextCompletion(false);
split.context().getGraph().add("○"); split.context().getGraph().add("○");
} }
@@ -112,7 +114,7 @@ public interface BaseAgent {
} }
split.context().getGraph().add(this.name()); split.context().getGraph().add(this.name());
if (!skipCompletion) { if (!skipCompletionInSplit) {
final boolean skipCache = split.context().isSkipCache() || split.context().isAlwaysSkipCache(); final boolean skipCache = split.context().isSkipCache() || split.context().isAlwaysSkipCache();
split.context().setSkipCache(false); split.context().setSkipCache(false);
final var conversation = this.setupConversation(split); final var conversation = this.setupConversation(split);
@@ -218,7 +220,7 @@ public interface BaseAgent {
} }
} }
retContexts.forEach(this::nextAgent); retContexts.forEach(ctx -> this.nextAgent(ctx, skipCompletion));
} }
retContexts.addAll(splits.stream().filter(s -> s.context().isError()).map(s -> s.context()).toList()); retContexts.addAll(splits.stream().filter(s -> s.context().isError()).map(s -> s.context()).toList());
@@ -357,7 +359,7 @@ public interface BaseAgent {
return conversation; return conversation;
} }
private void nextAgent(final AgentContext context) { private void nextAgent(final AgentContext context, final boolean skipCompletion) {
context.setNextAgent(this.chooseNextAgent(context)); context.setNextAgent(this.chooseNextAgent(context));
if (context.getNextAgent() != null) { if (context.getNextAgent() != null) {
@@ -400,7 +402,8 @@ public interface BaseAgent {
} }
else { else {
context.getGraph().add("↴"); context.getGraph().add("↴");
if (context.getConversation().getConversation().getLast().type() != TurnType.ASSISTANT) { final var currentAgent = context.getAgents().get(context.getCurrentAgentName());
if (! skipCompletion && currentAgent.agentType(context) != AgentType.PROCESSING_ONLY && context.getConversation().getConversation().getLast().type() != TurnType.ASSISTANT) {
context.getConversation().addTurn(new Conversation.Turn(TurnType.ASSISTANT, context.getLastCompletion().response())); context.getConversation().addTurn(new Conversation.Turn(TurnType.ASSISTANT, context.getLastCompletion().response()));
} }
} }