33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- package org .opensearch .ml .engine . tools ;
6+ package org .opensearch .ml .common . utils ;
77
88import static org .opensearch .ml .common .CommonValue .TENANT_ID_FIELD ;
99import static org .opensearch .ml .common .utils .StringUtils .gson ;
1010
11- import java .util .ArrayList ;
1211import java .util .HashMap ;
1312import java .util .List ;
1413import java .util .Map ;
1918import org .opensearch .ml .common .output .model .ModelTensor ;
2019import org .opensearch .ml .common .output .model .ModelTensorOutput ;
2120import org .opensearch .ml .common .output .model .ModelTensors ;
22- import org .opensearch .ml .common .spi .tools .Tool ;
23- import org .opensearch .ml .common .utils .StringUtils ;
2421
2522import com .google .gson .reflect .TypeToken ;
2623import com .jayway .jsonpath .JsonPath ;
@@ -38,6 +35,19 @@ public class ToolUtils {
3835 public static final String TOOL_OUTPUT_FILTERS_FIELD = "output_filter" ;
3936 public static final String TOOL_REQUIRED_PARAMS = "required_parameters" ;
4037
38+ /**
39+ * Extracts required parameters based on tool attributes specification.
40+ * <p>
41+ * The method performs the following:
42+ * <ul>
43+ * <li>If required parameters are specified in attributes, only those parameters are extracted</li>
44+ * <li>If no required parameters are specified, all parameters are returned</li>
45+ * </ul>
46+ *
47+ * @param parameters The input parameters map to extract from
48+ * @param attributes The attributes map containing required parameter specifications
49+ * @return Map containing only the required parameters
50+ */
4151 public static Map <String , String > extractRequiredParameters (Map <String , String > parameters , Map <String , ?> attributes ) {
4252 Map <String , String > extractedParameters = new HashMap <>();
4353 if (parameters == null ) {
@@ -56,6 +66,26 @@ public static Map<String, String> extractRequiredParameters(Map<String, String>
5666 return extractedParameters ;
5767 }
5868
69+ /**
70+ * Extracts and processes input parameters, including handling "input" parameter.
71+ * <p>
72+ * The method performs the following steps:
73+ * <ol>
74+ * <li>Extracts required parameters based on tool attributes specification</li>
75+ * <li>If an "input" parameter exists:
76+ * <ul>
77+ * <li>Substitutes any parameter placeholders</li>
78+ * <li>Parses it as a JSON map</li>
79+ * <li>Merges the parsed values with other parameters</li>
80+ * </ul>
81+ * </li>
82+ * </ol>
83+ *
84+ * @param parameters The raw input parameters
85+ * @param attributes The tool attributes containing parameter specifications
86+ * @return Map of processed input parameters
87+ * @throws IllegalArgumentException if input JSON parsing fails
88+ */
5989 public static Map <String , String > extractInputParameters (Map <String , String > parameters , Map <String , ?> attributes ) {
6090 Map <String , String > extractedParameters = ToolUtils .extractRequiredParameters (parameters , attributes );
6191 if (extractedParameters .containsKey ("input" )) {
@@ -73,6 +103,22 @@ public static Map<String, String> extractInputParameters(Map<String, String> par
73103 return extractedParameters ;
74104 }
75105
106+ /**
107+ * Builds the final parameter map for tool execution.
108+ * <p>
109+ * The method performs the following steps:
110+ * <ol>
111+ * <li>Combines tool specification parameters with input parameters</li>
112+ * <li>Processes tool-specific parameter prefixes</li>
113+ * <li>Applies configuration overrides from tool specification</li>
114+ * <li>Adds tenant identification</li>
115+ * </ol>
116+ *
117+ * @param parameters The input parameters to process
118+ * @param toolSpec The tool specification containing default parameters and configuration
119+ * @param tenantId The identifier for the tenant
120+ * @return Map of processed parameters ready for tool execution
121+ */
76122 public static Map <String , String > buildToolParameters (Map <String , String > parameters , MLToolSpec toolSpec , String tenantId ) {
77123 Map <String , String > executeParams = new HashMap <>();
78124 if (toolSpec .getParameters () != null ) {
@@ -102,30 +148,14 @@ public static Map<String, String> buildToolParameters(Map<String, String> parame
102148 return executeParams ;
103149 }
104150
105- public static Tool createTool (Map <String , Tool .Factory > toolFactories , Map <String , String > executeParams , MLToolSpec toolSpec ) {
106- if (!toolFactories .containsKey (toolSpec .getType ())) {
107- throw new IllegalArgumentException ("Tool not found: " + toolSpec .getType ());
108- }
109- Map <String , Object > toolParams = new HashMap <>();
110- toolParams .putAll (executeParams );
111- Map <String , Object > runtimeResources = toolSpec .getRuntimeResources ();
112- if (runtimeResources != null ) {
113- toolParams .putAll (runtimeResources );
114- }
115- Tool tool = toolFactories .get (toolSpec .getType ()).create (toolParams );
116- String toolName = getToolName (toolSpec );
117- tool .setName (toolName );
118-
119- if (toolSpec .getDescription () != null ) {
120- tool .setDescription (toolSpec .getDescription ());
121- }
122- if (executeParams .containsKey (toolName + ".description" )) {
123- tool .setDescription (executeParams .get (toolName + ".description" ));
124- }
125-
126- return tool ;
127- }
128-
151+ /**
152+ * Filters tool output based on specified output filters in tool parameters.
153+ * Uses JSONPath expressions to extract specific portions of the response.
154+ *
155+ * @param toolParams The tool parameters containing output filter specifications
156+ * @param response The raw tool response to filter
157+ * @return Filtered output if successful, original response if filtering fails
158+ */
129159 public static Object filterToolOutput (Map <String , String > toolParams , Object response ) {
130160 if (toolParams != null && toolParams .containsKey (TOOL_OUTPUT_FILTERS_FIELD )) {
131161 try {
@@ -142,6 +172,20 @@ public static Object filterToolOutput(Map<String, String> toolParams, Object res
142172 return response ;
143173 }
144174
175+ /**
176+ * Parses different types of tool responses into a JSON string representation.
177+ * <p>
178+ * Handles the following special cases:
179+ * <ul>
180+ * <li>ModelTensors - converts to XContent JSON representation</li>
181+ * <li>ModelTensor - converts to XContent JSON representation</li>
182+ * <li>ModelTensorOutput - converts to XContent JSON representation</li>
183+ * <li>Other types - converts to generic JSON string</li>
184+ * </ul>
185+ *
186+ * @param output The tool output object to parse
187+ * @return JSON string representation of the output
188+ */
145189 public static String parseResponse (Object output ) {
146190 try {
147191 if (output instanceof List && !((List ) output ).isEmpty () && ((List ) output ).get (0 ) instanceof ModelTensors ) {
@@ -159,16 +203,15 @@ public static String parseResponse(Object output) {
159203 }
160204 }
161205
162- public static List <String > getToolNames (Map <String , Tool > tools ) {
163- final List <String > inputTools = new ArrayList <>();
164- for (Map .Entry <String , Tool > entry : tools .entrySet ()) {
165- String toolName = entry .getValue ().getName ();
166- inputTools .add (toolName );
167- }
168- return inputTools ;
169- }
170-
206+ /**
207+ * Gets the tool name from a tool specification.
208+ * Returns the specified name if available, otherwise returns the tool type.
209+ *
210+ * @param toolSpec The tool specification
211+ * @return The name of the tool
212+ */
171213 public static String getToolName (MLToolSpec toolSpec ) {
172214 return toolSpec .getName () != null ? toolSpec .getName () : toolSpec .getType ();
173215 }
216+
174217}
0 commit comments