55package org .opensearch .neuralsearch .processor ;
66
77import com .google .gson .Gson ;
8+ import lombok .AllArgsConstructor ;
89import lombok .extern .log4j .Log4j2 ;
910import org .opensearch .action .search .SearchRequest ;
1011import org .opensearch .common .xcontent .XContentType ;
2021import org .opensearch .neuralsearch .stats .events .EventStatsManager ;
2122import org .opensearch .neuralsearch .util .NeuralSearchClusterUtil ;
2223import org .opensearch .search .builder .SearchSourceBuilder ;
23- import org .opensearch .search .pipeline .AbstractProcessor ;
24+ import org .opensearch .search .pipeline .SystemGeneratedProcessor ;
25+ import org .opensearch .search .pipeline .ProcessorGenerationContext ;
2426import org .opensearch .search .pipeline .Processor ;
2527import org .opensearch .search .pipeline .SearchRequestProcessor ;
2628import org .opensearch .search .pipeline .PipelineProcessingContext ;
3133import java .util .Locale ;
3234import java .util .Map ;
3335
34- import static org .opensearch .ingest .ConfigurationUtils .readStringProperty ;
35-
3636@ Log4j2
37- public class AgenticQueryTranslatorProcessor extends AbstractProcessor implements SearchRequestProcessor {
37+ public class AgenticQueryTranslatorProcessor implements SearchRequestProcessor , SystemGeneratedProcessor {
3838
3939 public static final String TYPE = "agentic_query_translator" ;
4040 private static final int MAX_AGENT_RESPONSE_SIZE = 10_000 ;
4141 private final MLCommonsClientAccessor mlClient ;
42- private final String agentId ;
4342 private final NamedXContentRegistry xContentRegistry ;
44- private static final Gson gson = new Gson ();;
43+ private final String tag ;
44+ private final String description ;
45+ private final boolean ignoreFailure ;
46+ private static final Gson gson = new Gson ();
4547
4648 AgenticQueryTranslatorProcessor (
4749 String tag ,
4850 String description ,
4951 boolean ignoreFailure ,
5052 MLCommonsClientAccessor mlClient ,
51- String agentId ,
5253 NamedXContentRegistry xContentRegistry
5354 ) {
54- super (tag , description , ignoreFailure );
55+ this .tag = tag ;
56+ this .description = description ;
57+ this .ignoreFailure = ignoreFailure ;
5558 this .mlClient = mlClient ;
56- this .agentId = agentId ;
5759 this .xContentRegistry = xContentRegistry ;
5860 }
5961
@@ -80,12 +82,7 @@ public void processRequestAsync(
8082
8183 // Validate that agentic query is used alone without other search features
8284 if (hasOtherSearchFeatures (sourceBuilder )) {
83- String errorMessage = String .format (
84- Locale .ROOT ,
85- "Agentic search blocked - Invalid usage with other search features - Agent ID: [%s], Query: [%s]" ,
86- agentId ,
87- agenticQuery .getQueryText ()
88- );
85+ String errorMessage = "Agentic search blocked - Invalid usage with other search features" ;
8986 requestListener .onFailure (new IllegalArgumentException (errorMessage ));
9087 return ;
9188 }
@@ -109,6 +106,7 @@ private void executeAgentAsync(
109106 ActionListener <SearchRequest > requestListener
110107 ) {
111108 Map <String , String > parameters = new HashMap <>();
109+ String agentId = agenticQuery .getAgentId ();
112110 parameters .put ("query_text" , agenticQuery .getQueryText ());
113111
114112 // Get index mapping from the search request
@@ -131,22 +129,14 @@ private void executeAgentAsync(
131129
132130 // Validate response size to prevent memory exhaustion
133131 if (agentResponse == null ) {
134- String errorMessage = String .format (
135- Locale .ROOT ,
136- "Agentic search failed - Null response from agent - Agent ID: [%s], Query: [%s]" ,
137- agentId ,
138- agenticQuery .getQueryText ()
139- );
140- throw new IllegalArgumentException (errorMessage );
132+ throw new IllegalArgumentException ("Agentic search failed - Null response from agent" );
141133 }
142134
143135 if (agentResponse .length () > MAX_AGENT_RESPONSE_SIZE ) {
144136 String errorMessage = String .format (
145137 Locale .ROOT ,
146- "Agentic search blocked - Response size exceeded limit - Agent ID: [%s], Size: [%d], Query: [%s]. Maximum allowed size is %d characters." ,
147- agentId ,
138+ "Agentic search blocked - Response size exceeded limit. Size: [%d], Maximum allowed size is %d characters." ,
148139 agentResponse .length (),
149- agenticQuery .getQueryText (),
150140 MAX_AGENT_RESPONSE_SIZE
151141 );
152142 throw new IllegalArgumentException (errorMessage );
@@ -161,22 +151,11 @@ private void executeAgentAsync(
161151
162152 requestListener .onResponse (request );
163153 } catch (IOException e ) {
164- String errorMessage = String .format (
165- Locale .ROOT ,
166- "Agentic search failed - Parse error - Agent ID: [%s], Error: [%s]" ,
167- agentId ,
168- e .getMessage ()
169- );
154+ String errorMessage = String .format (Locale .ROOT , "Agentic search failed - Parse error: [%s]" , e .getMessage ());
170155 requestListener .onFailure (new IOException (errorMessage , e ));
171156 }
172157 }, e -> {
173- String errorMessage = String .format (
174- Locale .ROOT ,
175- "Agentic search failed - Agent execution error - Agent ID: [%s], Query: [%s], Error: [%s]" ,
176- agentId ,
177- agenticQuery .getQueryText (),
178- e .getMessage ()
179- );
158+ String errorMessage = String .format (Locale .ROOT , "Agentic search failed - Agent execution error: [%s]" , e .getMessage ());
180159 requestListener .onFailure (new RuntimeException (errorMessage , e ));
181160 }));
182161 }
@@ -191,19 +170,44 @@ public String getType() {
191170 return TYPE ;
192171 }
193172
194- public static class Factory implements Processor .Factory <SearchRequestProcessor > {
173+ @ Override
174+ public String getTag () {
175+ return this .tag ;
176+ }
177+
178+ @ Override
179+ public String getDescription () {
180+ return this .description ;
181+ }
182+
183+ @ Override
184+ public boolean isIgnoreFailure () {
185+ return this .ignoreFailure ;
186+ }
187+
188+ @ Override
189+ public ExecutionStage getExecutionStage () {
190+ // Execute before user-defined processors as agentic query would be replaced by the new DSL
191+ return ExecutionStage .PRE_USER_DEFINED ;
192+ }
193+
194+ @ AllArgsConstructor
195+ public static class Factory implements SystemGeneratedProcessor .SystemGeneratedFactory <SearchRequestProcessor > {
195196 private final MLCommonsClientAccessor mlClient ;
196197 private final NamedXContentRegistry xContentRegistry ;
197198 private final NeuralSearchSettingsAccessor settingsAccessor ;
198199
199- public Factory (
200- MLCommonsClientAccessor mlClient ,
201- NamedXContentRegistry xContentRegistry ,
202- NeuralSearchSettingsAccessor settingsAccessor
203- ) {
204- this .mlClient = mlClient ;
205- this .xContentRegistry = xContentRegistry ;
206- this .settingsAccessor = settingsAccessor ;
200+ @ Override
201+ public boolean shouldGenerate (ProcessorGenerationContext context ) {
202+ SearchRequest searchRequest = context .searchRequest ();
203+ if (searchRequest == null || searchRequest .source () == null ) {
204+ return false ;
205+ }
206+
207+ boolean hasAgenticQuery = searchRequest .source ().query () instanceof AgenticSearchQueryBuilder ;
208+ log .debug ("Query type: {}, hasAgenticQuery: {}" , searchRequest .source ().query ().getClass ().getSimpleName (), hasAgenticQuery );
209+
210+ return hasAgenticQuery ;
207211 }
208212
209213 @ Override
@@ -221,11 +225,7 @@ public AgenticQueryTranslatorProcessor create(
221225 "Agentic search is currently disabled. Enable it using the 'plugins.neural_search.agentic_search_enabled' setting."
222226 );
223227 }
224- String agentId = readStringProperty (TYPE , tag , config , "agent_id" );
225- if (agentId == null || agentId .trim ().isEmpty ()) {
226- throw new IllegalArgumentException ("agent_id is required for agentic_query_translator processor" );
227- }
228- return new AgenticQueryTranslatorProcessor (tag , description , ignoreFailure , mlClient , agentId , xContentRegistry );
228+ return new AgenticQueryTranslatorProcessor (tag , description , ignoreFailure , mlClient , xContentRegistry );
229229 }
230230 }
231231}
0 commit comments