7
7
import static org .opensearch .core .xcontent .XContentParserUtils .ensureExpectedToken ;
8
8
9
9
import java .io .IOException ;
10
+ import java .util .Map ;
10
11
12
+ import org .opensearch .Version ;
11
13
import org .opensearch .core .common .io .stream .StreamInput ;
12
14
import org .opensearch .core .common .io .stream .StreamOutput ;
13
15
import org .opensearch .core .common .io .stream .Writeable ;
@@ -25,6 +27,9 @@ public class ToolMetadata implements ToXContentObject, Writeable {
25
27
public static final String TOOL_DESCRIPTION_FIELD = "description" ;
26
28
public static final String TOOL_TYPE_FIELD = "type" ;
27
29
public static final String TOOL_VERSION_FIELD = "version" ;
30
+ public static final String TOOL_ATTRIBUTES_FIELD = "attributes" ;
31
+
32
+ private static final Version MINIMUM_VERSION_FOR_TOOL_ATTRIBUTES = Version .V_3_0_0 ;
28
33
29
34
@ Getter
30
35
private String name ;
@@ -34,27 +39,43 @@ public class ToolMetadata implements ToXContentObject, Writeable {
34
39
private String type ;
35
40
@ Getter
36
41
private String version ;
42
+ @ Getter
43
+ private Map <String , Object > attributes ;
37
44
38
45
@ Builder (toBuilder = true )
39
- public ToolMetadata (String name , String description , String type , String version ) {
46
+ public ToolMetadata (String name , String description , String type , String version , Map < String , Object > attributes ) {
40
47
this .name = name ;
41
48
this .description = description ;
42
49
this .type = type ;
43
50
this .version = version ;
51
+ this .attributes = attributes ;
44
52
}
45
53
46
54
public ToolMetadata (StreamInput input ) throws IOException {
55
+ Version byteStreamVersion = input .getVersion ();
47
56
name = input .readString ();
48
57
description = input .readString ();
49
58
type = input .readString ();
50
59
version = input .readOptionalString ();
60
+ if (byteStreamVersion .onOrAfter (MINIMUM_VERSION_FOR_TOOL_ATTRIBUTES ) && input .readBoolean ()) {
61
+ attributes = input .readMap (StreamInput ::readString , StreamInput ::readGenericValue );
62
+ }
51
63
}
52
64
53
65
public void writeTo (StreamOutput output ) throws IOException {
66
+ Version byteStreamVersion = output .getVersion ();
54
67
output .writeString (name );
55
68
output .writeString (description );
56
69
output .writeString (type );
57
70
output .writeOptionalString (version );
71
+ if (byteStreamVersion .onOrAfter (MINIMUM_VERSION_FOR_TOOL_ATTRIBUTES )) {
72
+ if (attributes != null ) {
73
+ output .writeBoolean (true );
74
+ output .writeMap (attributes , StreamOutput ::writeString , StreamOutput ::writeGenericValue );
75
+ } else {
76
+ output .writeBoolean (false );
77
+ }
78
+ }
58
79
}
59
80
60
81
@ Override
@@ -70,6 +91,9 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par
70
91
builder .field (TOOL_TYPE_FIELD , type );
71
92
}
72
93
builder .field (TOOL_VERSION_FIELD , version != null ? version : "undefined" );
94
+ if (attributes != null ) {
95
+ builder .field (TOOL_ATTRIBUTES_FIELD , attributes );
96
+ }
73
97
builder .endObject ();
74
98
return builder ;
75
99
}
@@ -79,6 +103,7 @@ public static ToolMetadata parse(XContentParser parser) throws IOException {
79
103
String description = null ;
80
104
String type = null ;
81
105
String version = null ;
106
+ Map <String , Object > attributes = null ;
82
107
83
108
ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .currentToken (), parser );
84
109
while (parser .nextToken () != XContentParser .Token .END_OBJECT ) {
@@ -97,12 +122,14 @@ public static ToolMetadata parse(XContentParser parser) throws IOException {
97
122
break ;
98
123
case TOOL_VERSION_FIELD :
99
124
version = parser .text ();
125
+ case TOOL_ATTRIBUTES_FIELD :
126
+ attributes = parser .map ();
100
127
default :
101
128
parser .skipChildren ();
102
129
break ;
103
130
}
104
131
}
105
- return ToolMetadata .builder ().name (name ).description (description ).type (type ).version (version ).build ();
132
+ return ToolMetadata .builder ().name (name ).description (description ).type (type ).version (version ).attributes ( attributes ). build ();
106
133
}
107
134
108
135
public static ToolMetadata fromStream (StreamInput in ) throws IOException {
0 commit comments