|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.skills; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static java.nio.channels.Channels.newReader; |
| 21 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 25 | +import com.google.common.collect.ImmutableMap; |
| 26 | +import com.google.common.io.ByteSource; |
| 27 | +import java.io.BufferedReader; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.UncheckedIOException; |
| 31 | +import java.nio.channels.Channels; |
| 32 | +import java.nio.channels.ReadableByteChannel; |
| 33 | +import java.util.function.BiConsumer; |
| 34 | + |
| 35 | +/** |
| 36 | + * Abstract base class for SkillSource implementations that load skills from path like object. |
| 37 | + * |
| 38 | + * @param <PathT> the type of path object |
| 39 | + */ |
| 40 | +public abstract class AbstractSkillSource<PathT> implements SkillSource { |
| 41 | + |
| 42 | + private static final String THREE_DASHES = "---"; |
| 43 | + private static final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); |
| 44 | + |
| 45 | + @Override |
| 46 | + public ImmutableMap<String, Frontmatter> listFrontmatters() { |
| 47 | + ImmutableMap.Builder<String, Frontmatter> builder = ImmutableMap.builder(); |
| 48 | + iterateSkills((name, path) -> builder.put(name, loadFrontmatter(name, path))); |
| 49 | + return builder.buildOrThrow(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Frontmatter loadFrontmatter(String skillName) { |
| 54 | + PathT skillMdPath = findSkillMdPath(skillName); |
| 55 | + return loadFrontmatter(skillName, skillMdPath); |
| 56 | + } |
| 57 | + |
| 58 | + private Frontmatter loadFrontmatter(String skillName, PathT skillMdPath) { |
| 59 | + try (BufferedReader reader = openReader(skillMdPath)) { |
| 60 | + String yaml = readFrontmatterYaml(reader); |
| 61 | + Frontmatter frontmatter = parseFrontmatter(yaml); |
| 62 | + checkArgument( |
| 63 | + frontmatter.name().equals(skillName), |
| 64 | + "Skill name '%s' does not match directory name '%s'.", |
| 65 | + frontmatter.name(), |
| 66 | + skillName); |
| 67 | + return frontmatter; |
| 68 | + } catch (IOException e) { |
| 69 | + throw new UncheckedIOException(e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String loadInstructions(String skillName) { |
| 75 | + PathT skillMdPath = findSkillMdPath(skillName); |
| 76 | + |
| 77 | + try (BufferedReader reader = openReader(skillMdPath)) { |
| 78 | + return readInstructions(reader); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new UncheckedIOException(e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public ByteSource loadResource(String skillName, String resourcePath) { |
| 86 | + PathT path = findResourcePath(skillName, resourcePath); |
| 87 | + return new ByteSource() { |
| 88 | + @Override |
| 89 | + public InputStream openStream() throws IOException { |
| 90 | + return Channels.newInputStream(AbstractSkillSource.this.openChannel(path)); |
| 91 | + } |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + /** Iterates through SKILL.md files for all the supported skills. */ |
| 96 | + protected abstract void iterateSkills(BiConsumer<String, PathT> skillMdConsumer); |
| 97 | + |
| 98 | + /** Returns the path to the SKILL.md file for the given skill. */ |
| 99 | + protected abstract PathT findSkillMdPath(String skillName); |
| 100 | + |
| 101 | + /** Returns the path to the resource for the given skill. */ |
| 102 | + protected abstract PathT findResourcePath(String skillName, String resourcePath); |
| 103 | + |
| 104 | + /** Opens a {@link InputStream} for reading the content of the given path. */ |
| 105 | + protected abstract ReadableByteChannel openChannel(PathT path) throws IOException; |
| 106 | + |
| 107 | + private BufferedReader openReader(PathT path) throws IOException { |
| 108 | + return new BufferedReader(newReader(openChannel(path), UTF_8)); |
| 109 | + } |
| 110 | + |
| 111 | + private String readFrontmatterYaml(BufferedReader reader) throws IOException { |
| 112 | + String line = reader.readLine(); |
| 113 | + checkArgument( |
| 114 | + line != null && line.trim().equals(THREE_DASHES), |
| 115 | + "Skill file must start with %s", |
| 116 | + THREE_DASHES); |
| 117 | + |
| 118 | + StringBuilder sb = new StringBuilder(); |
| 119 | + while ((line = reader.readLine()) != null) { |
| 120 | + if (line.trim().equals(THREE_DASHES)) { |
| 121 | + return sb.toString(); |
| 122 | + } |
| 123 | + sb.append(line).append("\n"); |
| 124 | + } |
| 125 | + throw new IllegalArgumentException( |
| 126 | + "Skill file frontmatter not properly closed with " + THREE_DASHES); |
| 127 | + } |
| 128 | + |
| 129 | + private String readInstructions(BufferedReader reader) throws IOException { |
| 130 | + // Skip the frontmatter block |
| 131 | + String line = reader.readLine(); |
| 132 | + checkArgument( |
| 133 | + line != null && line.trim().equals(THREE_DASHES), |
| 134 | + "Skill file must start with %s", |
| 135 | + THREE_DASHES); |
| 136 | + boolean dashClosed = false; |
| 137 | + while ((line = reader.readLine()) != null) { |
| 138 | + if (line.trim().equals(THREE_DASHES)) { |
| 139 | + dashClosed = true; |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + checkArgument(dashClosed, "Skill file frontmatter not properly closed with %s", THREE_DASHES); |
| 144 | + |
| 145 | + // Read the instructions till the end of the file |
| 146 | + StringBuilder sb = new StringBuilder(); |
| 147 | + while ((line = reader.readLine()) != null) { |
| 148 | + sb.append(line).append("\n"); |
| 149 | + } |
| 150 | + return sb.toString().trim(); |
| 151 | + } |
| 152 | + |
| 153 | + private Frontmatter parseFrontmatter(String yaml) { |
| 154 | + try { |
| 155 | + return yamlMapper.readValue(yaml, Frontmatter.class); |
| 156 | + } catch (IOException e) { |
| 157 | + throw new UncheckedIOException(e); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments