Skip to content

Commit 9e29d97

Browse files
committed
Fix tool calling
Signed-off-by: sirivarma <[email protected]>
1 parent 8e508e4 commit 9e29d97

File tree

3 files changed

+504
-4
lines changed

3 files changed

+504
-4
lines changed

sdk/src/main/java/io/dapr/client/DaprClientImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.google.protobuf.Any;
1818
import com.google.protobuf.ByteString;
1919
import com.google.protobuf.Empty;
20+
import com.google.protobuf.Struct;
21+
import com.google.protobuf.Value;
2022
import io.dapr.client.domain.ActorMetadata;
2123
import io.dapr.client.domain.AppConnectionPropertiesHealthMetadata;
2224
import io.dapr.client.domain.AppConnectionPropertiesMetadata;
@@ -1811,21 +1813,20 @@ private void buildConversationTools(List<ConversationTools> tools,
18111813
}
18121814

18131815
if (function.getParameters() != null) {
1814-
Map<String, Any> functionParams = function.getParameters()
1816+
Map<String, Value> functionParams = function.getParameters()
18151817
.entrySet().stream()
18161818
.collect(Collectors.toMap(
18171819
Map.Entry::getKey,
18181820
e -> {
18191821
try {
1820-
return Any.newBuilder().setValue(ByteString.copyFrom(objectSerializer.serialize(e.getValue())))
1821-
.build();
1822+
return ProtobufValueHelper.toProtobufValue(e.getValue());
18221823
} catch (IOException ex) {
18231824
throw new RuntimeException(ex);
18241825
}
18251826
}
18261827
));
18271828

1828-
protoFunction.putAllParameters(functionParams);
1829+
protoFunction.setParameters(Struct.newBuilder().putAllFields(functionParams).build());
18291830
}
18301831

18311832
builder.addTools(DaprProtos.ConversationTools.newBuilder()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2021 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.client;
15+
16+
import com.google.protobuf.ListValue;
17+
import com.google.protobuf.NullValue;
18+
import com.google.protobuf.Struct;
19+
import com.google.protobuf.Value;
20+
import io.dapr.serializer.DaprObjectSerializer;
21+
22+
import java.io.IOException;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
/**
27+
* Helper class to convert Java objects to Google Protobuf Value types.
28+
*/
29+
public class ProtobufValueHelper {
30+
31+
/**
32+
* Converts a Java object to a Google Protobuf Value.
33+
*
34+
* @param obj the Java object to convert
35+
* @return the corresponding Protobuf Value
36+
* @throws IOException if serialization fails
37+
*/
38+
public static Value toProtobufValue(Object obj) throws IOException {
39+
if (obj == null) {
40+
return Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
41+
}
42+
43+
if (obj instanceof Boolean) {
44+
return Value.newBuilder().setBoolValue((Boolean) obj).build();
45+
}
46+
47+
if (obj instanceof String) {
48+
return Value.newBuilder().setStringValue((String) obj).build();
49+
}
50+
51+
if (obj instanceof Number) {
52+
return Value.newBuilder().setNumberValue(((Number) obj).doubleValue()).build();
53+
}
54+
55+
if (obj instanceof List) {
56+
ListValue.Builder listBuilder = ListValue.newBuilder();
57+
for (Object item : (List<?>) obj) {
58+
listBuilder.addValues(toProtobufValue(item));
59+
}
60+
return Value.newBuilder().setListValue(listBuilder.build()).build();
61+
}
62+
63+
if (obj instanceof Map) {
64+
Struct.Builder structBuilder = Struct.newBuilder();
65+
for (Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
66+
String key = entry.getKey().toString();
67+
Value value = toProtobufValue(entry.getValue());
68+
structBuilder.putFields(key, value);
69+
}
70+
return Value.newBuilder().setStructValue(structBuilder.build()).build();
71+
}
72+
73+
// Fallback: convert to string
74+
return Value.newBuilder().setStringValue(obj.toString()).build();
75+
}
76+
}

0 commit comments

Comments
 (0)