6
6
package org .opensearch .ml .common .agent ;
7
7
8
8
import static org .opensearch .core .xcontent .XContentParserUtils .ensureExpectedToken ;
9
+ import static org .opensearch .ml .common .CommonValue .TENANT_ID_FIELD ;
10
+ import static org .opensearch .ml .common .CommonValue .VERSION_2_19_0 ;
9
11
import static org .opensearch .ml .common .utils .StringUtils .getParameterMap ;
10
12
11
13
import java .io .IOException ;
22
24
import lombok .Builder ;
23
25
import lombok .EqualsAndHashCode ;
24
26
import lombok .Getter ;
27
+ import lombok .Setter ;
25
28
26
29
@ EqualsAndHashCode
27
30
@ Getter
@@ -41,6 +44,8 @@ public class MLToolSpec implements ToXContentObject {
41
44
private Map <String , String > parameters ;
42
45
private boolean includeOutputInAgentResponse ;
43
46
private Map <String , String > configMap ;
47
+ @ Setter
48
+ private String tenantId ;
44
49
45
50
@ Builder (toBuilder = true )
46
51
public MLToolSpec (
@@ -49,7 +54,8 @@ public MLToolSpec(
49
54
String description ,
50
55
Map <String , String > parameters ,
51
56
boolean includeOutputInAgentResponse ,
52
- Map <String , String > configMap
57
+ Map <String , String > configMap ,
58
+ String tenantId
53
59
) {
54
60
if (type == null ) {
55
61
throw new IllegalArgumentException ("tool type is null" );
@@ -60,9 +66,11 @@ public MLToolSpec(
60
66
this .parameters = parameters ;
61
67
this .includeOutputInAgentResponse = includeOutputInAgentResponse ;
62
68
this .configMap = configMap ;
69
+ this .tenantId = tenantId ;
63
70
}
64
71
65
72
public MLToolSpec (StreamInput input ) throws IOException {
73
+ Version streamInputVersion = input .getVersion ();
66
74
type = input .readString ();
67
75
name = input .readOptionalString ();
68
76
description = input .readOptionalString ();
@@ -73,13 +81,15 @@ public MLToolSpec(StreamInput input) throws IOException {
73
81
if (input .getVersion ().onOrAfter (MINIMAL_SUPPORTED_VERSION_FOR_TOOL_CONFIG ) && input .readBoolean ()) {
74
82
configMap = input .readMap (StreamInput ::readString , StreamInput ::readOptionalString );
75
83
}
84
+ this .tenantId = streamInputVersion .onOrAfter (VERSION_2_19_0 ) ? input .readOptionalString () : null ;
76
85
}
77
86
78
87
public void writeTo (StreamOutput out ) throws IOException {
88
+ Version streamOutputVersion = out .getVersion ();
79
89
out .writeString (type );
80
90
out .writeOptionalString (name );
81
91
out .writeOptionalString (description );
82
- if (parameters != null && parameters .size () > 0 ) {
92
+ if (parameters != null && ! parameters .isEmpty () ) {
83
93
out .writeBoolean (true );
84
94
out .writeMap (parameters , StreamOutput ::writeString , StreamOutput ::writeOptionalString );
85
95
} else {
@@ -94,6 +104,9 @@ public void writeTo(StreamOutput out) throws IOException {
94
104
out .writeBoolean (false );
95
105
}
96
106
}
107
+ if (streamOutputVersion .onOrAfter (VERSION_2_19_0 )) {
108
+ out .writeOptionalString (tenantId );
109
+ }
97
110
}
98
111
99
112
@ Override
@@ -108,13 +121,16 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
108
121
if (description != null ) {
109
122
builder .field (DESCRIPTION_FIELD , description );
110
123
}
111
- if (parameters != null && parameters .size () > 0 ) {
124
+ if (parameters != null && ! parameters .isEmpty () ) {
112
125
builder .field (PARAMETERS_FIELD , parameters );
113
126
}
114
127
builder .field (INCLUDE_OUTPUT_IN_AGENT_RESPONSE , includeOutputInAgentResponse );
115
128
if (configMap != null && !configMap .isEmpty ()) {
116
129
builder .field (CONFIG_FIELD , configMap );
117
130
}
131
+ if (tenantId != null ) {
132
+ builder .field (TENANT_ID_FIELD , tenantId );
133
+ }
118
134
builder .endObject ();
119
135
return builder ;
120
136
}
@@ -126,6 +142,7 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
126
142
Map <String , String > parameters = null ;
127
143
boolean includeOutputInAgentResponse = false ;
128
144
Map <String , String > configMap = null ;
145
+ String tenantId = null ;
129
146
130
147
ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .currentToken (), parser );
131
148
while (parser .nextToken () != XContentParser .Token .END_OBJECT ) {
@@ -151,6 +168,9 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
151
168
case CONFIG_FIELD :
152
169
configMap = getParameterMap (parser .map ());
153
170
break ;
171
+ case TENANT_ID_FIELD :
172
+ tenantId = parser .textOrNull ();
173
+ break ;
154
174
default :
155
175
parser .skipChildren ();
156
176
break ;
@@ -164,11 +184,11 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
164
184
.parameters (parameters )
165
185
.includeOutputInAgentResponse (includeOutputInAgentResponse )
166
186
.configMap (configMap )
187
+ .tenantId (tenantId )
167
188
.build ();
168
189
}
169
190
170
191
public static MLToolSpec fromStream (StreamInput in ) throws IOException {
171
- MLToolSpec toolSpec = new MLToolSpec (in );
172
- return toolSpec ;
192
+ return new MLToolSpec (in );
173
193
}
174
194
}
0 commit comments