|
4 | 4 | import static org.mockito.ArgumentMatchers.anyInt; |
5 | 5 | import static org.mockito.ArgumentMatchers.anyString; |
6 | 6 | import static org.mockito.ArgumentMatchers.nullable; |
| 7 | +import static org.mockito.Mockito.doReturn; |
7 | 8 | import static org.mockito.Mockito.mock; |
8 | 9 | import static org.mockito.Mockito.never; |
9 | 10 | import static org.mockito.Mockito.spy; |
|
17 | 18 | import com.google.adk.sessions.Session; |
18 | 19 | import com.google.common.collect.ImmutableList; |
19 | 20 | import com.google.common.collect.ImmutableMap; |
| 21 | +import com.google.genai.types.Blob; |
20 | 22 | import com.google.genai.types.Content; |
21 | 23 | import com.google.genai.types.FunctionDeclaration; |
22 | 24 | import com.google.genai.types.FunctionResponse; |
23 | 25 | import com.google.genai.types.Part; |
24 | 26 | import com.google.genai.types.Schema; |
25 | 27 | import io.reactivex.rxjava3.core.Maybe; |
26 | 28 | import io.reactivex.rxjava3.core.Single; |
| 29 | +import java.nio.charset.StandardCharsets; |
27 | 30 | import java.util.List; |
28 | 31 | import java.util.Map; |
29 | 32 | import java.util.Optional; |
@@ -218,4 +221,137 @@ public void processLlmRequest_artifactsInContext_withOtherFunctionCall_doesNotLo |
218 | 221 | .loadArtifact(anyString(), anyString(), anyString(), anyString(), anyInt()); |
219 | 222 | assertThat(finalRequest.contents()).containsExactly(functionCallContent); |
220 | 223 | } |
| 224 | + |
| 225 | + @Test |
| 226 | + public void processLlmRequest_unsupportedTextLikeMime_convertsToText() { |
| 227 | + String artifactName = "data.csv"; |
| 228 | + String csvContent = "col1,col2\n1,2\n"; |
| 229 | + Part artifactPart = |
| 230 | + processLoadArtifactRequest( |
| 231 | + artifactName, |
| 232 | + Part.fromBytes( |
| 233 | + csvContent.getBytes(StandardCharsets.UTF_8), "application/csv; charset=utf-8")); |
| 234 | + |
| 235 | + assertThat(artifactPart.inlineData()).isEmpty(); |
| 236 | + assertThat(artifactPart.text()).hasValue(csvContent); |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + public void processLlmRequest_supportedMime_keepsInlineData() { |
| 241 | + String artifactName = "file.pdf"; |
| 242 | + byte[] pdfBytes = "%PDF-1.4".getBytes(StandardCharsets.UTF_8); |
| 243 | + Part artifactPart = |
| 244 | + processLoadArtifactRequest(artifactName, Part.fromBytes(pdfBytes, "application/pdf")); |
| 245 | + |
| 246 | + assertThat(artifactPart.inlineData()).isPresent(); |
| 247 | + assertThat(artifactPart.inlineData().get().mimeType()).hasValue("application/pdf"); |
| 248 | + assertThat(artifactPart.inlineData().get().data().get()).isEqualTo(pdfBytes); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void processLlmRequest_unsupportedBinaryMime_convertsToPlaceholder() { |
| 253 | + String artifactName = "slides.pptx"; |
| 254 | + Part artifactPart = |
| 255 | + processLoadArtifactRequest( |
| 256 | + artifactName, |
| 257 | + Part.fromBytes( |
| 258 | + new byte[] {1, 2, 3}, |
| 259 | + "application/vnd.openxmlformats-officedocument.presentationml.presentation")); |
| 260 | + |
| 261 | + assertThat(artifactPart.inlineData()).isEmpty(); |
| 262 | + assertThat(artifactPart.text()) |
| 263 | + .hasValue( |
| 264 | + "[Binary artifact: slides.pptx, type:" |
| 265 | + + " application/vnd.openxmlformats-officedocument.presentationml.presentation," |
| 266 | + + " size: 0.0 KB. Content cannot be displayed inline.]"); |
| 267 | + } |
| 268 | + |
| 269 | + @Test |
| 270 | + public void processLlmRequest_unsupportedMimeWithoutInlineData_convertsToNoDataPlaceholder() { |
| 271 | + String artifactName = "empty.bin"; |
| 272 | + Part artifactPart = |
| 273 | + processLoadArtifactRequest( |
| 274 | + artifactName, |
| 275 | + Part.builder() |
| 276 | + .inlineData(Blob.builder().mimeType("application/octet-stream").build()) |
| 277 | + .build()); |
| 278 | + |
| 279 | + assertThat(artifactPart.inlineData()).isEmpty(); |
| 280 | + assertThat(artifactPart.text()) |
| 281 | + .hasValue( |
| 282 | + "[Artifact: empty.bin, type: application/octet-stream." |
| 283 | + + " No inline data was provided.]"); |
| 284 | + } |
| 285 | + |
| 286 | + @Test |
| 287 | + public void processLlmRequest_emptyMime_defaultsToOctetStream() { |
| 288 | + String artifactName = "unknown"; |
| 289 | + Part artifactPart = |
| 290 | + processLoadArtifactRequest( |
| 291 | + artifactName, |
| 292 | + Part.fromBytes(new byte[] {(byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xEF}, "")); |
| 293 | + |
| 294 | + assertThat(artifactPart.inlineData()).isEmpty(); |
| 295 | + assertThat(artifactPart.text()) |
| 296 | + .hasValue( |
| 297 | + "[Binary artifact: unknown, type: application/octet-stream," |
| 298 | + + " size: 0.0 KB. Content cannot be displayed inline.]"); |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + public void processLlmRequest_nullMime_defaultsToOctetStream() { |
| 303 | + String artifactName = "mystery"; |
| 304 | + Part artifactPart = |
| 305 | + processLoadArtifactRequest( |
| 306 | + artifactName, |
| 307 | + Part.builder() |
| 308 | + .inlineData( |
| 309 | + Blob.builder() |
| 310 | + .data(new byte[] {(byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xEF}) |
| 311 | + .build()) |
| 312 | + .build()); |
| 313 | + |
| 314 | + assertThat(artifactPart.inlineData()).isEmpty(); |
| 315 | + assertThat(artifactPart.text()) |
| 316 | + .hasValue( |
| 317 | + "[Binary artifact: mystery, type: application/octet-stream," |
| 318 | + + " size: 0.0 KB. Content cannot be displayed inline.]"); |
| 319 | + } |
| 320 | + |
| 321 | + private Part processLoadArtifactRequest(String artifactName, Part loadedArtifactPart) { |
| 322 | + ImmutableList<String> availableArtifacts = ImmutableList.of(artifactName); |
| 323 | + ImmutableList<String> artifactsToLoad = ImmutableList.of(artifactName); |
| 324 | + |
| 325 | + FunctionResponse functionResponse = |
| 326 | + FunctionResponse.builder() |
| 327 | + .name("load_artifacts") |
| 328 | + .response(ImmutableMap.of("artifact_names", artifactsToLoad)) |
| 329 | + .build(); |
| 330 | + Content functionCallContent = |
| 331 | + Content.builder() |
| 332 | + .role("model") |
| 333 | + .parts( |
| 334 | + ImmutableList.of( |
| 335 | + Part.fromFunctionResponse( |
| 336 | + functionResponse.name().get(), functionResponse.response().get()))) |
| 337 | + .build(); |
| 338 | + llmRequestBuilder.contents(ImmutableList.of(functionCallContent)); |
| 339 | + |
| 340 | + ToolContext spiedToolContext = spy(ToolContext.builder(mockInvocationContext).build()); |
| 341 | + doReturn(Single.just(availableArtifacts)).when(spiedToolContext).listArtifacts(); |
| 342 | + doReturn(Maybe.just(loadedArtifactPart)).when(spiedToolContext).loadArtifact(artifactName); |
| 343 | + |
| 344 | + loadArtifactsTool.processLlmRequest(llmRequestBuilder, spiedToolContext).blockingAwait(); |
| 345 | + verify(spiedToolContext).loadArtifact(artifactName); |
| 346 | + |
| 347 | + LlmRequest finalRequest = llmRequestBuilder.build(); |
| 348 | + assertThat(finalRequest.contents()).hasSize(2); |
| 349 | + Content appendedContent = finalRequest.contents().get(1); |
| 350 | + assertThat(appendedContent.role()).hasValue("user"); |
| 351 | + assertThat(appendedContent.parts()).isPresent(); |
| 352 | + assertThat(appendedContent.parts().get()).hasSize(2); |
| 353 | + assertThat(appendedContent.parts().get().get(0).text()) |
| 354 | + .hasValue("Artifact " + artifactName + " is:"); |
| 355 | + return appendedContent.parts().get().get(1); |
| 356 | + } |
221 | 357 | } |
0 commit comments