12
12
import static org .opensearch .ml .common .utils .StringUtils .getParameterMap ;
13
13
14
14
import java .io .IOException ;
15
+ import java .util .HashMap ;
15
16
import java .util .Map ;
16
17
17
18
import org .opensearch .Version ;
@@ -39,6 +40,7 @@ public class MLToolSpec implements ToXContentObject {
39
40
public static final String ATTRIBUTES_FIELD = "attributes" ;
40
41
public static final String INCLUDE_OUTPUT_IN_AGENT_RESPONSE = "include_output_in_agent_response" ;
41
42
public static final String CONFIG_FIELD = "config" ;
43
+ public static final String RUN_TIME_RESOURCES_FIELD = "runtime_resources" ;
42
44
43
45
private String type ;
44
46
private String name ;
@@ -49,6 +51,7 @@ public class MLToolSpec implements ToXContentObject {
49
51
private Map <String , String > configMap ;
50
52
@ Setter
51
53
private String tenantId ;
54
+ private Map <String , Object > runtimeResources ;
52
55
53
56
@ Builder (toBuilder = true )
54
57
public MLToolSpec (
@@ -59,7 +62,8 @@ public MLToolSpec(
59
62
Map <String , String > attributes ,
60
63
boolean includeOutputInAgentResponse ,
61
64
Map <String , String > configMap ,
62
- String tenantId
65
+ String tenantId ,
66
+ Map <String , Object > runtimeResources
63
67
) {
64
68
if (type == null ) {
65
69
throw new IllegalArgumentException ("tool type is null" );
@@ -72,6 +76,7 @@ public MLToolSpec(
72
76
this .includeOutputInAgentResponse = includeOutputInAgentResponse ;
73
77
this .configMap = configMap ;
74
78
this .tenantId = tenantId ;
79
+ this .runtimeResources = runtimeResources ;
75
80
}
76
81
77
82
public MLToolSpec (StreamInput input ) throws IOException {
@@ -87,8 +92,13 @@ public MLToolSpec(StreamInput input) throws IOException {
87
92
configMap = input .readMap (StreamInput ::readString , StreamInput ::readOptionalString );
88
93
}
89
94
this .tenantId = streamInputVersion .onOrAfter (VERSION_2_19_0 ) ? input .readOptionalString () : null ;
90
- if (input .getVersion ().onOrAfter (VERSION_3_0_0 ) && input .available () > 0 && input .readBoolean ()) {
91
- attributes = input .readMap (StreamInput ::readString , StreamInput ::readOptionalString );
95
+ if (input .getVersion ().onOrAfter (VERSION_3_0_0 )) {
96
+ if (input .available () > 0 && input .readBoolean ()) {
97
+ attributes = input .readMap (StreamInput ::readString , StreamInput ::readOptionalString );
98
+ }
99
+ if (input .available () > 0 && input .readBoolean ()) {
100
+ runtimeResources = input .readMap (StreamInput ::readString , StreamInput ::readGenericValue );
101
+ }
92
102
}
93
103
}
94
104
@@ -122,6 +132,12 @@ public void writeTo(StreamOutput out) throws IOException {
122
132
} else {
123
133
out .writeBoolean (false );
124
134
}
135
+ if (runtimeResources != null && !runtimeResources .isEmpty ()) {
136
+ out .writeBoolean (true );
137
+ out .writeMap (runtimeResources , StreamOutput ::writeString , StreamOutput ::writeGenericValue );
138
+ } else {
139
+ out .writeBoolean (false );
140
+ }
125
141
}
126
142
}
127
143
@@ -150,6 +166,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
150
166
if (tenantId != null ) {
151
167
builder .field (TENANT_ID_FIELD , tenantId );
152
168
}
169
+ if (runtimeResources != null && !runtimeResources .isEmpty ()) {
170
+ builder .field (RUN_TIME_RESOURCES_FIELD , runtimeResources );
171
+ }
153
172
builder .endObject ();
154
173
return builder ;
155
174
}
@@ -163,6 +182,7 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
163
182
boolean includeOutputInAgentResponse = false ;
164
183
Map <String , String > configMap = null ;
165
184
String tenantId = null ;
185
+ Map <String , Object > runtimeResources = null ;
166
186
167
187
ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .currentToken (), parser );
168
188
while (parser .nextToken () != XContentParser .Token .END_OBJECT ) {
@@ -194,6 +214,8 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
194
214
case TENANT_ID_FIELD :
195
215
tenantId = parser .textOrNull ();
196
216
break ;
217
+ case RUN_TIME_RESOURCES_FIELD :
218
+ runtimeResources = parser .map ();
197
219
default :
198
220
parser .skipChildren ();
199
221
break ;
@@ -209,10 +231,22 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
209
231
.includeOutputInAgentResponse (includeOutputInAgentResponse )
210
232
.configMap (configMap )
211
233
.tenantId (tenantId )
234
+ .runtimeResources (runtimeResources )
212
235
.build ();
213
236
}
214
237
215
238
public static MLToolSpec fromStream (StreamInput in ) throws IOException {
216
239
return new MLToolSpec (in );
217
240
}
241
+
242
+ public void addRuntimeResource (String key , Object value ) {
243
+ if (this .runtimeResources == null ) {
244
+ this .runtimeResources = new HashMap <>();
245
+ }
246
+ this .runtimeResources .put (key , value );
247
+ }
248
+
249
+ public Object getRuntimeResource (String key ) {
250
+ return this .runtimeResources .get (key );
251
+ }
218
252
}
0 commit comments