-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathMessageAdapter.java
More file actions
289 lines (271 loc) · 10.9 KB
/
Copy pathMessageAdapter.java
File metadata and controls
289 lines (271 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.common;
import com.alibaba.dashscope.tools.ToolCallBase;
import com.alibaba.dashscope.tools.ToolCallFunction;
import com.alibaba.dashscope.tools.ToolCallFunction.CallFunction;
import com.alibaba.dashscope.utils.ApiKeywords;
import com.alibaba.dashscope.utils.JsonUtils;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MessageAdapter extends TypeAdapter<Message> {
@Override
public void write(JsonWriter out, Message value) throws IOException {
out.beginObject();
out.name(ApiKeywords.ROLE);
out.value(value.getRole());
if (value.getContent() != null) {
out.name(ApiKeywords.CONTENT);
out.value(value.getContent());
}
// array of content
if (value.getContents() != null) {
out.name(ApiKeywords.CONTENT).beginArray();
for (MessageContentBase content : value.getContents()) {
if (content.getType().equals(ApiKeywords.CONTENT_TYPE_TEXT)) {
// text content
MessageContentText textContent = (MessageContentText) content;
out.beginObject();
out.name("type");
out.value(ApiKeywords.CONTENT_TYPE_TEXT);
out.name(ApiKeywords.CONTENT_TYPE_TEXT);
out.value(textContent.getText());
// Write cache_control field if present
if (textContent.getCacheControl() != null) {
out.name(ApiKeywords.CONTENT_TYPE_CACHE_CONTROL);
out.beginObject();
out.name("type");
out.value(textContent.getCacheControl().getType());
out.name("ttl");
out.value(textContent.getCacheControl().getTtl());
out.endObject();
}
out.endObject();
} else if (content.getType().equals(ApiKeywords.CONTENT_TYPE_IMAGE_URL)) {
out.beginObject();
out.name("type");
out.value(ApiKeywords.CONTENT_TYPE_IMAGE_URL);
out.name(ApiKeywords.CONTENT_TYPE_IMAGE_URL);
out.beginObject();
out.name("url");
ImageURL imageURL = ((MessageContentImageURL) content).getImageURL();
out.value(imageURL.getUrl());
if (imageURL.getDetail() != null) {
out.name("detail");
out.value(imageURL.getDetail());
}
out.endObject();
out.endObject(); // image url
} else if (content.getType().equals(ApiKeywords.CONTENT_TYPE_DOC_URL)) {
DocURL docURL = ((MessageContentDocURL) content).getDocURL();
if (docURL != null) {
out.beginObject();
out.name("type");
out.value(ApiKeywords.CONTENT_TYPE_DOC_URL);
out.name(ApiKeywords.CONTENT_TYPE_DOC_URL);
out.beginObject();
out.name("url");
out.value(docURL.getUrl());
if (docURL.getFileParsingStrategy() != null) {
out.name("file_parsing_strategy");
out.value(docURL.getFileParsingStrategy());
}
out.endObject();
out.endObject();
}
}
}
out.endArray();
}
if (value.getReasoningContent() != null) {
out.name(ApiKeywords.REASONING_CONTENT);
out.value(value.getReasoningContent());
}
if (value.getToolCalls() != null) {
out.name(ApiKeywords.TOOL_CALLS).beginArray();
for (ToolCallBase toolCall : value.getToolCalls()) {
out.beginObject();
String type = toolCall.getType();
out.name("type");
out.value(type);
out.name("id");
out.value(toolCall.getId());
out.name("index");
out.value(toolCall.getIndex());
out.name("function");
if (type.equals("function")) {
writeCallFunction(out, (ToolCallFunction) toolCall);
}
out.endObject();
}
out.endArray();
}
if (value.getToolCallId() != null) {
out.name(ApiKeywords.TOOL_CALL_ID);
out.value(value.getToolCallId());
}
if (value.getName() != null) {
out.name(ApiKeywords.NAME);
out.value(value.getName());
}
if (value.getPartial() != null) {
out.name(ApiKeywords.PARTIAL);
out.value(value.getPartial());
}
out.endObject();
}
private void writeCallFunction(JsonWriter out, ToolCallFunction toolCall) throws IOException {
out.beginObject();
CallFunction callFunction = toolCall.getFunction();
out.name("name");
out.value(callFunction.getName());
out.name("arguments");
out.value(callFunction.getArguments());
out.endObject();
}
// Parse array of content objects
@SuppressWarnings({"unchecked", "rawtypes"})
private List<MessageContentBase> parseContentList(List<LinkedTreeMap> contentList) {
List<MessageContentBase> contents = new ArrayList<>();
for (LinkedTreeMap<String, Object> contentItem : contentList) {
String type = (String) contentItem.get("type");
if (ApiKeywords.CONTENT_TYPE_TEXT.equals(type)) {
contents.add(parseTextContent(contentItem));
} else if (ApiKeywords.CONTENT_TYPE_IMAGE_URL.equals(type)) {
contents.add(parseImageContent(contentItem));
} else if (ApiKeywords.CONTENT_TYPE_DOC_URL.equals(type)) {
contents.add(parseDocContent(contentItem));
}
}
return contents;
}
// Parse text content with optional cache_control
@SuppressWarnings({"unchecked", "rawtypes"})
private MessageContentText parseTextContent(LinkedTreeMap<String, Object> contentItem) {
MessageContentText.MessageContentTextBuilder textBuilder =
MessageContentText.builder()
.type((String) contentItem.get("type"))
.text((String) contentItem.get(ApiKeywords.CONTENT_TYPE_TEXT));
// Parse cache_control if present
if (contentItem.containsKey(ApiKeywords.CONTENT_TYPE_CACHE_CONTROL)) {
LinkedTreeMap<String, Object> cacheControlMap =
(LinkedTreeMap<String, Object>) contentItem.get(ApiKeywords.CONTENT_TYPE_CACHE_CONTROL);
MessageContentText.CacheControl.CacheControlBuilder cacheBuilder =
MessageContentText.CacheControl.builder().type((String) cacheControlMap.get("type"));
// Handle ttl field - convert to String regardless of input type
if (cacheControlMap.containsKey("ttl")) {
Object ttlObj = cacheControlMap.get("ttl");
cacheBuilder.ttl(
ttlObj instanceof Number
? String.valueOf(((Number) ttlObj).intValue())
: String.valueOf(ttlObj));
}
textBuilder.cacheControl(cacheBuilder.build());
}
return textBuilder.build();
}
// Parse image_url content
@SuppressWarnings({"unchecked", "rawtypes"})
private MessageContentImageURL parseImageContent(LinkedTreeMap<String, Object> contentItem) {
LinkedTreeMap<String, Object> imageUrlMap =
(LinkedTreeMap<String, Object>) contentItem.get(ApiKeywords.CONTENT_TYPE_IMAGE_URL);
ImageURL.ImageURLBuilder imageBuilder = ImageURL.builder().url((String) imageUrlMap.get("url"));
if (imageUrlMap.containsKey("detail")) {
imageBuilder.detail((String) imageUrlMap.get("detail"));
}
return MessageContentImageURL.builder()
.type((String) contentItem.get("type"))
.imageURL(imageBuilder.build())
.build();
}
// Parse doc_url content
@SuppressWarnings({"unchecked", "rawtypes"})
private MessageContentDocURL parseDocContent(LinkedTreeMap<String, Object> contentItem) {
LinkedTreeMap<String, Object> docUrlMap =
(LinkedTreeMap<String, Object>) contentItem.get(ApiKeywords.CONTENT_TYPE_DOC_URL);
DocURL docURL = null;
if (docUrlMap != null) {
DocURL.DocURLBuilder docBuilder = DocURL.builder().url((String) docUrlMap.get("url"));
if (docUrlMap.containsKey("file_parsing_strategy")) {
docBuilder.fileParsingStrategy((String) docUrlMap.get("file_parsing_strategy"));
}
docURL = docBuilder.build();
}
return MessageContentDocURL.builder()
.type((String) contentItem.get("type"))
.docURL(docURL)
.build();
}
@SuppressWarnings("unchecked")
private ToolCallFunction convertToCallFunction(LinkedTreeMap<String, Object> toolCall) {
ToolCallFunction functionCall = new ToolCallFunction();
if (toolCall.containsKey("function")) {
ToolCallFunction.CallFunction callFunction = functionCall.new CallFunction();
LinkedTreeMap<String, Object> fc = (LinkedTreeMap<String, Object>) toolCall.get("function");
if (fc.containsKey("name")) {
callFunction.setName(fc.get("name").toString());
}
if (fc.containsKey("arguments")) {
callFunction.setArguments(fc.get("arguments").toString());
}
functionCall.setFunction(callFunction);
}
functionCall.setType(toolCall.get("type").toString());
if (toolCall.containsKey("id")) {
functionCall.setId(toolCall.get("id").toString());
}
// Fix: Set index field if it exists in the JSON
if (toolCall.containsKey("index")) {
Object indexObj = toolCall.get("index");
if (indexObj instanceof Number) {
functionCall.setIndex(((Number) indexObj).intValue());
}
}
return functionCall;
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Message read(JsonReader in) throws IOException {
Map<String, Object> objectMap = JsonUtils.gson.fromJson(in, Map.class);
Message msg = new Message();
if (objectMap.containsKey(ApiKeywords.ROLE)) {
msg.setRole((String) objectMap.get(ApiKeywords.ROLE));
objectMap.remove(ApiKeywords.ROLE);
}
if (objectMap.containsKey(ApiKeywords.CONTENT)) {
Object contentObj = objectMap.get(ApiKeywords.CONTENT);
// Handle both string and array content types
if (contentObj instanceof String) {
msg.setContent((String) contentObj);
} else if (contentObj instanceof List) {
msg.setContents(parseContentList((List<LinkedTreeMap>) contentObj));
}
objectMap.remove(ApiKeywords.CONTENT);
}
if (objectMap.containsKey(ApiKeywords.REASONING_CONTENT)) {
msg.setReasoningContent((String) objectMap.get(ApiKeywords.REASONING_CONTENT));
objectMap.remove(ApiKeywords.REASONING_CONTENT);
}
if (objectMap.containsKey(ApiKeywords.TOOL_CALLS)) {
msg.toolCalls = new ArrayList<ToolCallBase>();
List<LinkedTreeMap> toolCalls = (List<LinkedTreeMap>) objectMap.get(ApiKeywords.TOOL_CALLS);
if (toolCalls == null) {
toolCalls = new ArrayList<>();
}
for (LinkedTreeMap<String, Object> toolCall : toolCalls) {
// convert to toolCall
String type = toolCall.get("type").toString();
if (type.equals("function")) {
msg.toolCalls.add(convertToCallFunction(toolCall));
}
}
objectMap.remove(ApiKeywords.TOOL_CALLS);
}
return msg;
}
}