Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import com.google.adk.skills.SkillSource;
import com.google.adk.tools.BaseTool;
import com.google.adk.tools.ToolContext;
import com.google.common.base.Ascii;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.genai.types.Content;
import com.google.genai.types.FunctionDeclaration;
import com.google.genai.types.FunctionResponse;
Expand All @@ -42,6 +44,9 @@
import io.reactivex.rxjava3.core.Single;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -213,26 +218,48 @@ private Stream<Part> processPart(Part part) {
private ImmutableMap<String, Object> createResult(
String skillName, String resourcePath, ByteSource contentSource) throws IOException {
byte[] bytes = contentSource.read();
// Special handling of shell script as the guessContentTypeFromName would return
// application/x-shar
String contentType =
resourcePath.endsWith(".sh") || resourcePath.endsWith(".bash")
? "application/x-sh"
: guessContentTypeFromName(resourcePath);
String extension = Ascii.toLowerCase(Files.getFileExtension(resourcePath));
String contentType;
if (extension.equals("sh") || extension.equals("bash")) {
// guessContentTypeFromName would return application/x-shar
contentType = "application/x-sh";
} else if (extension.equals("yml") || extension.equals("yaml")) {
// The JDK has no MIME mapping for YAML.
contentType = "application/yaml";
} else {
contentType = guessContentTypeFromName(resourcePath);
}
Comment on lines +222 to +231

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we fall back to strict UTF-8 decoding before application/octet-stream when both guesses return null?

Handling only .yml and .yaml still leaves other text formats missing from the JDK's MIME table (.toml, .ini, .properties, .sql, .ts, .kt, etc.) falling back to application/octet-stream and hitting the same Vertex AI 400. ADK Python and ADK Kotlin instead try a strict UTF-8 decode and treat valid UTF-8 as text.

Note that references/binary.dat in the test uses {0, 1, 2, 3}, which is valid UTF-8, so it would need non-UTF-8 bytes (or a NUL check) if we go that route. If you prefer to keep this PR scoped to YAML, that's fine too; we can leave #1893 open for the rest.

if (contentType == null) {
contentType = guessContentTypeFromStream(new ByteArrayInputStream(bytes));
}
String utf8Text = null;
if (contentType == null) {
contentType = "application/octet-stream";
// Treat valid UTF-8 as text when the JDK has no MIME mapping for the extension.
utf8Text = decodeStrictUtf8(bytes);
contentType = utf8Text != null ? "text/plain" : "application/octet-stream";
}
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put(SKILL_NAME, skillName).put(FILE_PATH, resourcePath).put(MIME_TYPE, contentType);

if (contentType.startsWith("text/") || EXTRA_TEXT_MIME_TYPES.contains(contentType)) {
builder.put(CONTENT, new String(bytes, UTF_8));
builder.put(CONTENT, utf8Text != null ? utf8Text : new String(bytes, UTF_8));
} else {
builder.put(CONTENT, bytes);
}
return builder.buildOrThrow();
}

/** Returns the decoded string if {@code bytes} is strict UTF-8, or {@code null} otherwise. */
private static String decodeStrictUtf8(byte[] bytes) {
try {
return UTF_8
.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT)
.decode(ByteBuffer.wrap(bytes))
.toString();
} catch (CharacterCodingException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,82 @@ public void call_loadSkillResourceTool_script_success() {
"content", "echo hello");
}

@Test
public void call_loadSkillResourceTool_ymlAsset_success() {
LoadSkillResourceTool loadSkillResourceTool =
new LoadSkillResourceTool(createTestSkillSource());
Map<String, Object> response =
loadSkillResourceTool
.runAsync(
ImmutableMap.of("skill_name", "test-skill", "file_path", "assets/config.yml"),
createToolContext())
.blockingGet();

assertThat(response)
.containsExactly(
"skill_name", "test-skill",
"file_path", "assets/config.yml",
"mime_type", "application/yaml",
"content", "id: a\nname: b\n");
}

@Test
public void call_loadSkillResourceTool_yamlAsset_success() {
LoadSkillResourceTool loadSkillResourceTool =
new LoadSkillResourceTool(createTestSkillSource());
Map<String, Object> response =
loadSkillResourceTool
.runAsync(
ImmutableMap.of("skill_name", "test-skill", "file_path", "assets/config.yaml"),
createToolContext())
.blockingGet();

assertThat(response)
.containsExactly(
"skill_name", "test-skill",
"file_path", "assets/config.yaml",
"mime_type", "application/yaml",
"content", "id: a\nname: b\n");
}

@Test
public void call_loadSkillResourceTool_uppercaseYamlAsset_success() {
LoadSkillResourceTool loadSkillResourceTool =
new LoadSkillResourceTool(createTestSkillSource());
Map<String, Object> response =
loadSkillResourceTool
.runAsync(
ImmutableMap.of("skill_name", "test-skill", "file_path", "assets/CONFIG.YAML"),
createToolContext())
.blockingGet();

assertThat(response)
.containsExactly(
"skill_name", "test-skill",
"file_path", "assets/CONFIG.YAML",
"mime_type", "application/yaml",
"content", "id: a\nname: b\n");
}

@Test
public void call_loadSkillResourceTool_unknownTextExtension_success() {
LoadSkillResourceTool loadSkillResourceTool =
new LoadSkillResourceTool(createTestSkillSource());
Map<String, Object> response =
loadSkillResourceTool
.runAsync(
ImmutableMap.of("skill_name", "test-skill", "file_path", "assets/config.toml"),
createToolContext())
.blockingGet();

assertThat(response)
.containsExactly(
"skill_name", "test-skill",
"file_path", "assets/config.toml",
"mime_type", "text/plain",
"content", "name = \"caf\u00e9\"\n");
}
Comment on lines +161 to +178

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assets/config.toml is plain 7-bit ASCII and references/binary.dat fails on byte 0, so swapping UTF_8 for US_ASCII would still pass every test. Could you include multi-byte UTF-8 characters (for example, "name = \"café\"\n") in the .toml test, and add a binary case with a valid UTF-8 prefix followed by a truncated sequence (for example, {'a', (byte) 0xC3})?


@Test
public void call_loadSkillResourceTool_streamDetection_success() {
LoadSkillResourceTool loadSkillResourceTool =
Expand Down Expand Up @@ -168,7 +244,28 @@ public void call_loadSkillResourceTool_binaryReference_detected() {

Part binaryPart = parts.get(1);
assertThat(binaryPart.inlineData().get().mimeType()).hasValue("application/octet-stream");
assertThat(binaryPart.inlineData().get().data().get()).isEqualTo(new byte[] {0, 1, 2, 3});
assertThat(binaryPart.inlineData().get().data().get())
.isEqualTo(new byte[] {(byte) 0xFF, (byte) 0xFE, (byte) 0x80});
}

@Test
public void call_loadSkillResourceTool_truncatedUtf8_detectedAsBinary() {
LoadSkillResourceTool loadSkillResourceTool =
new LoadSkillResourceTool(createTestSkillSource());
Map<String, Object> response =
loadSkillResourceTool
.runAsync(
ImmutableMap.of(
"skill_name", "test-skill", "file_path", "references/truncated.dat"),
createToolContext())
.blockingGet();

assertThat(response)
.containsAtLeast(
"skill_name", "test-skill",
"file_path", "references/truncated.dat",
"mime_type", "application/octet-stream");
assertThat((byte[]) response.get("content")).isEqualTo(new byte[] {'a', (byte) 0xC3});
}

@Test
Expand Down Expand Up @@ -321,9 +418,14 @@ private SkillSource createTestSkillSource() {
.frontmatter(Frontmatter.builder().name("test-skill").description("test skill").build())
.instructions("Test instructions")
.addResource("references/my_doc.md", "doc content".getBytes(UTF_8))
.addResource("references/binary.dat", new byte[] {0, 1, 2, 3})
.addResource("references/binary.dat", new byte[] {(byte) 0xFF, (byte) 0xFE, (byte) 0x80})
.addResource("references/truncated.dat", new byte[] {'a', (byte) 0xC3})
.addResource("assets/template.txt", "asset content".getBytes(UTF_8))
.addResource("scripts/setup.sh", "echo hello".getBytes(UTF_8))
.addResource("assets/config.yml", "id: a\nname: b\n".getBytes(UTF_8))
.addResource("assets/config.yaml", "id: a\nname: b\n".getBytes(UTF_8))
.addResource("assets/CONFIG.YAML", "id: a\nname: b\n".getBytes(UTF_8))
.addResource("assets/config.toml", "name = \"caf\u00e9\"\n".getBytes(UTF_8))
.addResource("assets/data_no_ext", "<?xml version=\"1.0\"?><root/>".getBytes(UTF_8))
.build();
}
Expand Down
Loading