Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import io.agentscope.core.agent.Agent;
import io.agentscope.core.agent.AgentBase;
import io.agentscope.core.message.Msg;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -100,6 +102,7 @@
public class MsgHub implements AutoCloseable {

private static final Logger log = LoggerFactory.getLogger(MsgHub.class);
private static final Duration CLOSE_TIMEOUT = Duration.ofSeconds(10);

private final String name;
private final List<AgentBase> participants;
Expand All @@ -113,7 +116,7 @@ public class MsgHub implements AutoCloseable {
* @param builder Builder instance
*/
private MsgHub(Builder builder) {
this.name = builder.name != null ? builder.name : UUID.randomUUID().toString();
this.name = Objects.requireNonNullElse(builder.name, UUID.randomUUID().toString());
this.participants = new CopyOnWriteArrayList<>(builder.participants);
this.announcement = builder.announcement;
this.enableAutoBroadcast = builder.enableAutoBroadcast;
Expand Down Expand Up @@ -195,10 +198,11 @@ public Mono<Void> exit() {
/**
* Close the MsgHub and cleanup resources.
* This is the AutoCloseable implementation for try-with-resources support.
* Waits up to 10 seconds for exit to complete.
*/
@Override
public void close() {
exit().block();
exit().block(CLOSE_TIMEOUT);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import io.agentscope.core.rag.model.RetrieveConfig;
import io.agentscope.core.tool.Tool;
import io.agentscope.core.tool.ToolParam;
import java.time.Duration;
import java.util.List;
import java.util.Objects;

/**
* Knowledge retrieval tools for Agentic RAG mode.
Expand Down Expand Up @@ -51,19 +53,19 @@
*/
public class KnowledgeRetrievalTools {

private static final int DEFAULT_RETRIEVE_LIMIT = 5;
private static final Duration RETRIEVE_TIMEOUT = Duration.ofSeconds(60);

private final Knowledge knowledge;

/**
* Creates a new KnowledgeRetrievalTools instance.
*
* @param knowledge the knowledge base to retrieve from
* @throws IllegalArgumentException if knowledgeBase is null
* @throws NullPointerException if knowledge is null
*/
public KnowledgeRetrievalTools(Knowledge knowledge) {
if (knowledge == null) {
throw new IllegalArgumentException("Knowledge base cannot be null");
}
this.knowledge = knowledge;
this.knowledge = Objects.requireNonNull(knowledge, "knowledge");
}

/**
Expand Down Expand Up @@ -108,10 +110,7 @@ public String retrieveKnowledge(
Integer limit,
Agent agent) {

// Set default value
if (limit == null) {
limit = 5;
}
int effectiveLimit = Objects.requireNonNullElse(limit, DEFAULT_RETRIEVE_LIMIT);

// Extract conversation history from agent if available
List<Msg> conversationHistory = null;
Expand All @@ -122,7 +121,7 @@ public String retrieveKnowledge(
// Build retrieval config with conversation history
RetrieveConfig config =
RetrieveConfig.builder()
.limit(limit)
.limit(effectiveLimit)
.scoreThreshold(0.5)
.conversationHistory(conversationHistory)
.build();
Expand All @@ -131,7 +130,7 @@ public String retrieveKnowledge(
.retrieve(query, config)
.map(this::formatDocumentsForTool)
.onErrorReturn("Failed to retrieve knowledge for query: " + query)
.block(); // Convert to synchronous call to match Tool interface
.block(RETRIEVE_TIMEOUT);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.agentscope.core.tool.subagent.SubAgentProvider;
import io.agentscope.core.tool.subagent.SubAgentTool;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -935,7 +936,7 @@ public void apply() {
disableTools,
groupName,
presetParameters)
.block();
.block(Duration.ofSeconds(30));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and for consistency with other changes in this PR, please extract the magic number 30 into a named constant within the ToolRegistration class. For example: private static final Duration MCP_REGISTRATION_TIMEOUT = Duration.ofSeconds(30);

Suggested change
.block(Duration.ofSeconds(30));
.block(MCP_REGISTRATION_TIMEOUT);

} else if (subAgentProvider != null) {
SubAgentTool subAgentTool = new SubAgentTool(subAgentProvider, subAgentConfig);
toolkit.registerAgentTool(subAgentTool, groupName, extendedModel, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.modelcontextprotocol.client.McpAsyncClient;
import io.modelcontextprotocol.spec.McpSchema;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
Expand Down Expand Up @@ -167,7 +168,7 @@ public void close() {
client.closeGracefully()
.doOnSuccess(v -> logger.debug("MCP client '{}' closed", name))
.doOnError(e -> logger.error("Error closing MCP client '{}'", name, e))
.block();
.block(Duration.ofSeconds(10));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and for consistency with other changes in this PR, please extract the magic number 10 into a named constant at the top of the McpAsyncClientWrapper class. For example: private static final Duration CLOSE_GRACEFULLY_TIMEOUT = Duration.ofSeconds(10);

Suggested change
.block(Duration.ofSeconds(10));
.block(CLOSE_GRACEFULLY_TIMEOUT);

} catch (Exception e) {
logger.error("Exception during MCP client close", e);
client.close();
Expand Down
Loading