Skip to content

Commit 8652687

Browse files
committed
Fix things
Signed-off-by: sirivarma <[email protected]>
1 parent 4793e69 commit 8652687

14 files changed

+895
-154
lines changed

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

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package io.dapr.client;
1515

16+
import com.fasterxml.jackson.databind.ObjectMapper;
1617
import com.google.common.base.Strings;
1718
import com.google.protobuf.Any;
1819
import com.google.protobuf.ByteString;
@@ -21,14 +22,15 @@
2122
import io.dapr.client.domain.ActorMetadata;
2223
import io.dapr.client.domain.AppConnectionPropertiesHealthMetadata;
2324
import io.dapr.client.domain.AppConnectionPropertiesMetadata;
25+
import io.dapr.client.domain.AssistantMessage;
2426
import io.dapr.client.domain.BulkPublishEntry;
2527
import io.dapr.client.domain.BulkPublishRequest;
2628
import io.dapr.client.domain.BulkPublishResponse;
2729
import io.dapr.client.domain.BulkPublishResponseFailedEntry;
2830
import io.dapr.client.domain.CloudEvent;
2931
import io.dapr.client.domain.ComponentMetadata;
3032
import io.dapr.client.domain.ConfigurationItem;
31-
import io.dapr.client.domain.ConversationFunction;
33+
import io.dapr.client.domain.ConversationToolsFunction;
3234
import io.dapr.client.domain.ConversationInput;
3335
import io.dapr.client.domain.ConversationInputAlpha2;
3436
import io.dapr.client.domain.ConversationMessage;
@@ -42,7 +44,7 @@
4244
import io.dapr.client.domain.ConversationResultChoices;
4345
import io.dapr.client.domain.ConversationResultMessage;
4446
import io.dapr.client.domain.ConversationToolCalls;
45-
import io.dapr.client.domain.ConversationToolCallsFunction;
47+
import io.dapr.client.domain.ConversationToolCallsOfFunction;
4648
import io.dapr.client.domain.ConversationTools;
4749
import io.dapr.client.domain.DaprMetadata;
4850
import io.dapr.client.domain.DeleteJobRequest;
@@ -73,6 +75,7 @@
7375
import io.dapr.client.domain.SubscribeConfigurationRequest;
7476
import io.dapr.client.domain.SubscribeConfigurationResponse;
7577
import io.dapr.client.domain.SubscriptionMetadata;
78+
import io.dapr.client.domain.ToolMessage;
7679
import io.dapr.client.domain.TransactionalStateOperation;
7780
import io.dapr.client.domain.UnlockRequest;
7881
import io.dapr.client.domain.UnlockResponseStatus;
@@ -1572,6 +1575,7 @@ public Mono<DaprMetadata> getMetadata() {
15721575
/**
15731576
* {@inheritDoc}
15741577
*/
1578+
@Deprecated(forRemoval = true)
15751579
@Override
15761580
public Mono<ConversationResponse> converse(ConversationRequest conversationRequest) {
15771581

@@ -1685,7 +1689,7 @@ public Mono<ConversationResponseAlpha2> converseAlpha2(ConversationRequestAlpha2
16851689
}
16861690

16871691
private DaprProtos.ConversationRequestAlpha2 buildConversationRequestProto(ConversationRequestAlpha2 request,
1688-
DaprProtos.ConversationRequestAlpha2.Builder builder) {
1692+
DaprProtos.ConversationRequestAlpha2.Builder builder) {
16891693
if (request.getTools() != null) {
16901694
buildConversationTools(request.getTools(), builder);
16911695
}
@@ -1694,13 +1698,21 @@ private DaprProtos.ConversationRequestAlpha2 buildConversationRequestProto(Conve
16941698
builder.putAllMetadata(request.getMetadata());
16951699
}
16961700

1701+
16971702
if (request.getParameters() != null) {
16981703
Map<String, Any> parameters = request.getParameters()
16991704
.entrySet().stream()
17001705
.collect(Collectors.toMap(
17011706
Map.Entry::getKey,
1702-
e -> Any.pack((Message) e.getValue())
1703-
));
1707+
e -> {
1708+
try {
1709+
return Any.newBuilder().setValue(ByteString.copyFrom(objectSerializer.serialize(e.getValue())))
1710+
.build();
1711+
} catch (IOException ex) {
1712+
throw new RuntimeException(ex);
1713+
}
1714+
})
1715+
);
17041716
builder.putAllParameters(parameters);
17051717
}
17061718

@@ -1727,7 +1739,7 @@ private DaprProtos.ConversationRequestAlpha2 buildConversationRequestProto(Conve
17271739
private void buildConversationTools(List<ConversationTools> tools,
17281740
DaprProtos.ConversationRequestAlpha2.Builder builder) {
17291741
for (ConversationTools tool : tools) {
1730-
ConversationFunction function = tool.getFunction();
1742+
ConversationToolsFunction function = tool.getFunction();
17311743

17321744
DaprProtos.ConversationToolsFunction.Builder protoFunction = DaprProtos.ConversationToolsFunction.newBuilder()
17331745
.setName(function.getName());
@@ -1741,7 +1753,14 @@ private void buildConversationTools(List<ConversationTools> tools,
17411753
.entrySet().stream()
17421754
.collect(Collectors.toMap(
17431755
Map.Entry::getKey,
1744-
e -> Any.pack((Message) e.getValue())
1756+
e -> {
1757+
try {
1758+
return Any.newBuilder().setValue(ByteString.copyFrom(objectSerializer.serialize(e.getValue())))
1759+
.build();
1760+
} catch (IOException ex) {
1761+
throw new RuntimeException(ex);
1762+
}
1763+
}
17451764
));
17461765

17471766
protoFunction.putAllParameters(functionParams);
@@ -1766,8 +1785,8 @@ private DaprProtos.ConversationMessage buildConversationMessage(ConversationMess
17661785
if (message.getContent() != null) {
17671786
toolMessage.addAllContent(getConversationMessageContent(message));
17681787
}
1769-
if (message.getToolId() != null) {
1770-
toolMessage.setToolId(message.getToolId());
1788+
if (((ToolMessage)message).getToolId() != null) {
1789+
toolMessage.setToolId(((ToolMessage)message).getToolId());
17711790
}
17721791
messageBuilder.setOfTool(toolMessage);
17731792
break;
@@ -1785,14 +1804,15 @@ private DaprProtos.ConversationMessage buildConversationMessage(ConversationMess
17851804
case ASSISTANT:
17861805
DaprProtos.ConversationMessageOfAssistant.Builder assistantMessage =
17871806
DaprProtos.ConversationMessageOfAssistant.newBuilder();
1807+
17881808
if (message.getName() != null) {
17891809
assistantMessage.setName(message.getName());
17901810
}
17911811
if (message.getContent() != null) {
17921812
assistantMessage.addAllContent(getConversationMessageContent(message));
17931813
}
1794-
if (message.getToolCalls() != null) {
1795-
assistantMessage.addAllToolCalls(getConversationToolCalls(message));
1814+
if (((AssistantMessage)message).getToolCalls() != null) {
1815+
assistantMessage.addAllToolCalls(getConversationToolCalls((AssistantMessage)message));
17961816
}
17971817
messageBuilder.setOfAssistant(assistantMessage);
17981818
break;
@@ -1851,15 +1871,18 @@ private ConversationResultMessage buildConversationResultMessage(DaprProtos.Conv
18511871
List<ConversationToolCalls> toolCalls = new ArrayList<>();
18521872

18531873
for (DaprProtos.ConversationToolCalls protoToolCall : protoChoice.getMessage().getToolCallsList()) {
1854-
ConversationToolCallsFunction function = null;
1874+
ConversationToolCallsOfFunction function = null;
18551875
if (protoToolCall.hasFunction()) {
1856-
function = new ConversationToolCallsFunction(
1876+
function = new ConversationToolCallsOfFunction(
18571877
protoToolCall.getFunction().getName(),
18581878
protoToolCall.getFunction().getArguments()
18591879
);
18601880
}
1861-
1862-
toolCalls.add(new ConversationToolCalls(protoToolCall.getId(), function));
1881+
1882+
ConversationToolCalls conversationToolCalls = new ConversationToolCalls(function);
1883+
conversationToolCalls.setId(protoToolCall.getId());
1884+
1885+
toolCalls.add(conversationToolCalls);
18631886
}
18641887

18651888
return new ConversationResultMessage(
@@ -1882,16 +1905,19 @@ private List<DaprProtos.ConversationMessageContent> getConversationMessageConten
18821905
}
18831906

18841907
private List<DaprProtos.ConversationToolCalls> getConversationToolCalls(
1885-
ConversationMessage conversationMessage) {
1908+
AssistantMessage assistantMessage) {
18861909
List<DaprProtos.ConversationToolCalls> conversationToolCalls = new ArrayList<>();
1887-
for (ConversationToolCalls conversationToolCall: conversationMessage.getToolCalls()) {
1888-
conversationToolCalls.add(DaprProtos.ConversationToolCalls.newBuilder()
1889-
.setId(conversationToolCall.getId())
1910+
for (ConversationToolCalls conversationToolCall: assistantMessage.getToolCalls()) {
1911+
DaprProtos.ConversationToolCalls.Builder toolCallsBuilder = DaprProtos.ConversationToolCalls.newBuilder()
18901912
.setFunction(DaprProtos.ConversationToolCallsOfFunction.newBuilder()
1891-
.setName(conversationToolCall.getFunction().getName())
1892-
.setArguments(conversationToolCall.getFunction().getArguments())
1893-
.build())
1894-
.build());
1913+
.setName(conversationToolCall.getFunction().getName())
1914+
.setArguments(conversationToolCall.getFunction().getArguments())
1915+
.build());
1916+
if (conversationToolCall.getId() != null) {
1917+
toolCallsBuilder.setId(conversationToolCall.getId());
1918+
}
1919+
1920+
conversationToolCalls.add(toolCallsBuilder.build());
18951921
}
18961922

18971923
return conversationToolCalls;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ <T> Subscription subscribeToEvents(
315315
* @param conversationRequest request to be passed to the LLM.
316316
* @return {@link ConversationResponse}.
317317
*/
318+
@Deprecated
318319
public Mono<ConversationResponse> converse(ConversationRequest conversationRequest);
319320

320321
/*
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2025 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.domain;
15+
16+
import java.util.List;
17+
18+
/**
19+
* Assistant message containing responses from the AI model.
20+
* Can include regular content and/or tool calls that the model wants to make.
21+
*/
22+
public class AssistantMessage implements ConversationMessage {
23+
24+
private String name;
25+
private final List<ConversationMessageContent> content;
26+
private final List<ConversationToolCalls> toolCalls;
27+
28+
/**
29+
* Creates an assistant message with content and optional tool calls.
30+
* @param content the content of the assistant message.
31+
* @param toolCalls the tool calls requested by the assistant.
32+
*/
33+
public AssistantMessage(List<ConversationMessageContent> content, List<ConversationToolCalls> toolCalls) {
34+
this.content = content != null ? List.copyOf(content) : null;
35+
this.toolCalls = toolCalls != null ? List.copyOf(toolCalls) : null;
36+
}
37+
38+
@Override
39+
public ConversationMessageRole getRole() {
40+
return ConversationMessageRole.ASSISTANT;
41+
}
42+
43+
@Override
44+
public String getName() {
45+
return name;
46+
}
47+
48+
/**
49+
* Sets the name of the assistant participant.
50+
*
51+
* @param name the name to set
52+
*/
53+
public void setName(String name) {
54+
this.name = name;
55+
}
56+
57+
@Override
58+
public List<ConversationMessageContent> getContent() {
59+
return content;
60+
}
61+
62+
public List<ConversationToolCalls> getToolCalls() {
63+
return toolCalls;
64+
}
65+
}

sdk/src/main/java/io/dapr/client/domain/ConversationMessage.java

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -16,118 +16,29 @@
1616
import java.util.List;
1717

1818
/**
19-
* Represents a conversation message with role-specific content.
19+
* Interface representing a conversation message with role-specific content.
2020
* Supports different message types: system, user, assistant, developer, and tool.
2121
*/
22-
public class ConversationMessage {
23-
24-
/**
25-
* Enum representing the different roles a message can have.
26-
*/
27-
public enum Role {
28-
SYSTEM,
29-
USER,
30-
ASSISTANT,
31-
DEVELOPER,
32-
TOOL
33-
}
34-
35-
private final Role role;
36-
private final String name;
37-
private final List<ConversationMessageContent> content;
38-
private final List<ConversationToolCalls> toolCalls;
39-
private final String toolId;
40-
41-
/**
42-
* Constructor for creating a message with basic content.
43-
*
44-
* @param role the role of the message sender
45-
* @param content the content of the message
46-
*/
47-
public ConversationMessage(Role role, List<ConversationMessageContent> content) {
48-
this(role, null, content, null, null);
49-
}
50-
51-
/**
52-
* Constructor for creating a message with name and content.
53-
*
54-
* @param role the role of the message sender
55-
* @param name the name of the participant (optional)
56-
* @param content the content of the message
57-
*/
58-
public ConversationMessage(Role role, String name, List<ConversationMessageContent> content) {
59-
this(role, name, content, null, null);
60-
}
61-
62-
/**
63-
* Full constructor for creating a message with all properties.
64-
*
65-
* @param role the role of the message sender
66-
* @param name the name of the participant (optional)
67-
* @param content the content of the message
68-
* @param toolCalls tool calls for assistant messages (optional)
69-
* @param toolId tool ID for tool messages (optional)
70-
*/
71-
public ConversationMessage(Role role, String name, List<ConversationMessageContent> content,
72-
List<ConversationToolCalls> toolCalls, String toolId) {
73-
this.role = role;
74-
this.name = name;
75-
this.content = content != null ? List.copyOf(content) : null;
76-
this.toolCalls = toolCalls != null ? List.copyOf(toolCalls) : null;
77-
this.toolId = toolId;
78-
}
22+
public interface ConversationMessage {
7923

8024
/**
8125
* Gets the role of the message sender.
8226
*
8327
* @return the message role
8428
*/
85-
public Role getRole() {
86-
return role;
87-
}
29+
ConversationMessageRole getRole();
8830

8931
/**
9032
* Gets the name of the participant in the message.
9133
*
9234
* @return the participant name, or null if not specified
9335
*/
94-
public String getName() {
95-
return name;
96-
}
36+
String getName();
9737

9838
/**
9939
* Gets the content of the message.
10040
*
10141
* @return the message content
10242
*/
103-
public List<ConversationMessageContent> getContent() {
104-
return content;
105-
}
106-
107-
/**
108-
* Gets the tool calls generated by the model (for assistant messages).
109-
*
110-
* @return the tool calls, or null if none
111-
*/
112-
public List<ConversationToolCalls> getToolCalls() {
113-
return toolCalls;
114-
}
115-
116-
/**
117-
* Gets the tool ID (for tool messages).
118-
*
119-
* @return the tool ID, or null if not a tool message
120-
*/
121-
public String getToolId() {
122-
return toolId;
123-
}
124-
125-
/**
126-
* Checks if this message has tool calls.
127-
*
128-
* @return true if the message has tool calls, false otherwise
129-
*/
130-
public boolean hasToolCalls() {
131-
return toolCalls != null && !toolCalls.isEmpty();
132-
}
43+
List<ConversationMessageContent> getContent();
13344
}

0 commit comments

Comments
 (0)