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 @@ -62,6 +62,7 @@ public class SearchIndexTool implements Tool {
public static final String INPUT_FIELD = "input";
public static final String INDEX_FIELD = "index";
public static final String QUERY_FIELD = "query";
public static final String SIZE_FIELD = "size";
public static final String INPUT_SCHEMA_FIELD = "input_schema";
public static final String STRICT_FIELD = "strict";

Expand Down Expand Up @@ -131,10 +132,13 @@ public boolean validate(Map<String, String> parameters) {
return true;
}

private SearchRequest getSearchRequest(String index, String query) throws IOException {
private SearchRequest getSearchRequest(String index, String query, Integer size) throws IOException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
XContentParser queryParser = XContentType.JSON.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, query);
searchSourceBuilder.parseXContent(queryParser);
if (size != null && size > 0) {
searchSourceBuilder.size(size);
}
return new SearchRequest().source(searchSourceBuilder).indices(index);
}

Expand Down Expand Up @@ -176,6 +180,7 @@ public <T> void run(Map<String, String> originalParameters, ActionListener<T> li
String input = parameters.get(INPUT_FIELD);
String index = null;
String query = null;
Integer size = null;
boolean returnFullResponse = Boolean.parseBoolean(parameters.getOrDefault(RETURN_RAW_RESPONSE, "false"));
if (!StringUtils.isEmpty(input)) {
try {
Expand All @@ -188,6 +193,10 @@ public <T> void run(Map<String, String> originalParameters, ActionListener<T> li
Object queryObject = PLAIN_NUMBER_GSON.fromJson(queryElement, Object.class);
query = PLAIN_NUMBER_GSON.toJson(queryObject);
}

if (jsonObject.has(SIZE_FIELD)) {
size = jsonObject.get(SIZE_FIELD).getAsInt();
}
}
} catch (JsonSyntaxException e) {
log.error("Invalid JSON input: {}", input, e);
Expand All @@ -202,6 +211,14 @@ public <T> void run(Map<String, String> originalParameters, ActionListener<T> li
query = parameters.get(QUERY_FIELD);
}

if (StringUtils.isNotEmpty(parameters.get(SIZE_FIELD))) {
try {
size = Math.min(size == null ? 100 : size, Integer.parseInt(parameters.get(SIZE_FIELD)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

The size here is also generated by LLM, right? Only when LLM doesn't generate anything we will use the default size 100 in PER agent.

} catch (NumberFormatException e) {
log.warn("Invalid size parameter: {}", parameters.get(SIZE_FIELD));
}
}

if (StringUtils.isEmpty(index) || StringUtils.isEmpty(query)) {
listener
.onFailure(
Expand All @@ -212,7 +229,7 @@ public <T> void run(Map<String, String> originalParameters, ActionListener<T> li
return;
}

SearchRequest searchRequest = getSearchRequest(index, query);
SearchRequest searchRequest = getSearchRequest(index, query, size);

ActionListener<SearchResponse> actionListener = ActionListener.<SearchResponse>wrap(r -> {
SearchHit[] hits = r.getHits().getHits();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,44 @@ public void testRun_withRangeQuery_triggersPlainDoubleGson() {

assertArrayEquals(new String[] { "test-index" }, cap.getValue().indices());
}

@Test
public void testRunWithSizeInInput() {
String inputString = "{\"index\": \"test-index\", \"query\": {\"query\": {\"match_all\": {}}}, \"size\": 5}";
Map<String, String> parameters = Map.of("input", inputString);
mockedSearchIndexTool.run(parameters, null);

ArgumentCaptor<SearchRequest> cap = ArgumentCaptor.forClass(SearchRequest.class);
verify(client, times(1)).search(cap.capture(), any());
assertEquals(5, cap.getValue().source().size());
}

@Test
public void testRunWithSizeAsParameter() {
Map<String, String> parameters = Map.of(
"index", "test-index",
"query", "{\"query\": {\"match_all\": {}}}",
"size", "3"
);
mockedSearchIndexTool.run(parameters, null);

ArgumentCaptor<SearchRequest> cap = ArgumentCaptor.forClass(SearchRequest.class);
verify(client, times(1)).search(cap.capture(), any());
assertEquals(3, cap.getValue().source().size());
}

@Test
public void testRunWithInvalidSizeParameter() {
Map<String, String> parameters = Map.of(
"index", "test-index",
"query", "{\"query\": {\"match_all\": {}}}",
"size", "invalid"
);
mockedSearchIndexTool.run(parameters, null);

ArgumentCaptor<SearchRequest> cap = ArgumentCaptor.forClass(SearchRequest.class);
verify(client, times(1)).search(cap.capture(), any());
// Size should not be set when invalid
assertEquals(-1, cap.getValue().source().size());
}
}
Loading