From 1af185e77ab7bd4056485d47247560078667adab Mon Sep 17 00:00:00 2001 From: extio-de Date: Sat, 2 May 2026 14:03:39 +0200 Subject: [PATCH] feat: Refactor conversation handling in BaseAgent and add metadata support in Conversation class --- .../java/de/extio/lmlib/agent/BaseAgent.java | 19 +++++++++++++------ .../de/extio/lmlib/client/Conversation.java | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/extio/lmlib/agent/BaseAgent.java b/src/main/java/de/extio/lmlib/agent/BaseAgent.java index aad0a3c..35672c4 100644 --- a/src/main/java/de/extio/lmlib/agent/BaseAgent.java +++ b/src/main/java/de/extio/lmlib/agent/BaseAgent.java @@ -384,20 +384,21 @@ public interface BaseAgent { private Conversation setupConversation(final Split split) { var conversation = split.context().getConversation(); if (conversation == null || conversation.getConversation().isEmpty()) { - conversation = Conversation.create(this.systemPrompt(), split.text()); + conversation = this.createConversation(this.systemPrompt(), split.text()); split.context().setConversation(conversation); } else if (this.agentType(split.context()) == AgentType.START_CONVERSATION) { - if (this.systemPrompt() == null || this.systemPrompt().isEmpty()) { - conversation = Conversation.create(split.text()); + final var system = this.systemPrompt(); + if (system == null || system.isEmpty()) { + conversation = this.createConversation(null, split.text()); } else { - conversation = Conversation.create(this.systemPrompt() + "\n" + split.text()); + conversation = this.createConversation(null, system + "\n" + split.text()); } split.context().setConversation(conversation); } else if (this.agentType(split.context()) == AgentType.START_CONVERSATION_WITH_SYSTEM_PROMPT) { - conversation = Conversation.create(this.systemPrompt(), split.text()); + conversation = this.createConversation(this.systemPrompt(), split.text()); split.context().setConversation(conversation); } else { @@ -409,7 +410,7 @@ public interface BaseAgent { mergedSysAndUser = conversation.getConversation().get(0).text(); } - final var newConversation = Conversation.create(mergedSysAndUser); + final var newConversation = this.createConversation(null, mergedSysAndUser); for (int n = 2; n < conversation.getConversation().size(); n++) { newConversation.addTurn(conversation.getConversation().get(n)); } @@ -429,6 +430,12 @@ public interface BaseAgent { return conversation; } + private Conversation createConversation(String system, String user) { + var conversation = Conversation.create(system, user); + conversation.setMetadata("Agent: " + this.name()); + return conversation; + } + private void appendLastCompletionToConversation(final AgentContext context, final boolean skipCompletion) { if (skipCompletion || context.getConversation() == null || context.getLastCompletion() == null) { return; diff --git a/src/main/java/de/extio/lmlib/client/Conversation.java b/src/main/java/de/extio/lmlib/client/Conversation.java index dfd8c48..d195b93 100755 --- a/src/main/java/de/extio/lmlib/client/Conversation.java +++ b/src/main/java/de/extio/lmlib/client/Conversation.java @@ -36,6 +36,8 @@ public final class Conversation { } private final List conversation = Collections.synchronizedList(new ArrayList<>()); + + private String metadata; // Optional metadata that can be used to store additional information about the conversation, e.g. conversation id, user id, etc. private Conversation() { } @@ -92,9 +94,21 @@ public final class Conversation { final StringBuilder builder = new StringBuilder(); builder.append("Conversation [conversation="); builder.append(this.conversation); + if (this.metadata != null) { + builder.append(", metadata="); + builder.append(this.metadata); + } builder.append("]"); return builder.toString(); } + + public String getMetadata() { + return metadata; + } + + public void setMetadata(final String metadata) { + this.metadata = metadata; + } public enum TurnType { SYSTEM, USER, ASSISTANT, TOOL