|
| 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.adk.skills.SkillSourceException.RESOURCE_NOT_FOUND; |
| 20 | +import static com.google.adk.skills.SkillSourceException.SKILL_LOAD_ERROR; |
| 21 | +import static com.google.adk.skills.SkillSourceException.SKILL_NOT_FOUND; |
| 22 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 23 | + |
| 24 | +import com.google.common.base.Ascii; |
| 25 | +import com.google.common.base.Splitter; |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | +import com.google.common.collect.ImmutableMap; |
| 28 | +import com.google.common.reflect.ClassPath; |
| 29 | +import com.google.common.reflect.ClassPath.ResourceInfo; |
| 30 | +import io.reactivex.rxjava3.core.Flowable; |
| 31 | +import io.reactivex.rxjava3.core.Single; |
| 32 | +import java.io.IOException; |
| 33 | +import java.nio.channels.Channels; |
| 34 | +import java.nio.channels.ReadableByteChannel; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Objects; |
| 39 | +import java.util.Optional; |
| 40 | + |
| 41 | +/** Loads skills from the classpath. */ |
| 42 | +public final class ClassPathSkillSource extends AbstractSkillSource<ResourceInfo> { |
| 43 | + |
| 44 | + private static final Splitter PATH_SPLITTER = Splitter.on('/'); |
| 45 | + |
| 46 | + private final String baseResourcePath; |
| 47 | + private final ClassLoader classLoader; |
| 48 | + private final Single<ImmutableMap<String, ResourceInfo>> skillMdsSingle; |
| 49 | + private final Single<ImmutableList<ResourceInfo>> allResourcesSingle; |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a new {@link ClassPathSkillSource} that loads skills from the given base resource path |
| 53 | + * using the current thread's context class loader. |
| 54 | + * |
| 55 | + * @param baseResourcePath the base classpath path to scan for skills (e.g., "skills/") |
| 56 | + */ |
| 57 | + public ClassPathSkillSource(String baseResourcePath) { |
| 58 | + this( |
| 59 | + baseResourcePath, |
| 60 | + Objects.requireNonNullElse( |
| 61 | + Thread.currentThread().getContextClassLoader(), |
| 62 | + ClassPathSkillSource.class.getClassLoader())); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new {@link ClassPathSkillSource} that loads skills from the given base resource path |
| 67 | + * using the specified {@link ClassLoader}. |
| 68 | + * |
| 69 | + * @param baseResourcePath the base classpath path to scan for skills |
| 70 | + * @param classLoader the class loader to use for scanning resources |
| 71 | + */ |
| 72 | + public ClassPathSkillSource(String baseResourcePath, ClassLoader classLoader) { |
| 73 | + this.baseResourcePath = normalizePath(baseResourcePath); |
| 74 | + this.classLoader = classLoader; |
| 75 | + |
| 76 | + // Scan classpath once (lazily) |
| 77 | + Single<ImmutableList<ResourceInfo>> scanned = Single.fromCallable(this::scanClassPath).cache(); |
| 78 | + this.allResourcesSingle = scanned; |
| 79 | + this.skillMdsSingle = scanned.map(this::extractSkillMds).cache(); |
| 80 | + } |
| 81 | + |
| 82 | + private static String normalizePath(String path) { |
| 83 | + if (path.isEmpty()) { |
| 84 | + return ""; |
| 85 | + } |
| 86 | + if (path.endsWith("/")) { |
| 87 | + return path; |
| 88 | + } |
| 89 | + return path + "/"; |
| 90 | + } |
| 91 | + |
| 92 | + private ImmutableList<ResourceInfo> scanClassPath() throws SkillSourceException { |
| 93 | + try { |
| 94 | + ClassPath classPath = ClassPath.from(classLoader); |
| 95 | + return classPath.getResources().stream() |
| 96 | + .filter(info -> info.getResourceName().startsWith(baseResourcePath)) |
| 97 | + .collect(toImmutableList()); |
| 98 | + } catch (IOException e) { |
| 99 | + throw new SkillSourceException( |
| 100 | + "Failed to scan classpath under " + baseResourcePath, SKILL_LOAD_ERROR, e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private ImmutableMap<String, ResourceInfo> extractSkillMds(ImmutableList<ResourceInfo> resources) |
| 105 | + throws SkillSourceException { |
| 106 | + Map<String, ResourceInfo> skillMdMap = new HashMap<>(); |
| 107 | + for (ResourceInfo info : resources) { |
| 108 | + String relPath = info.getResourceName().substring(baseResourcePath.length()); |
| 109 | + List<String> parts = PATH_SPLITTER.splitToList(relPath); |
| 110 | + // Check if the path format matches exactly {skillName}/SKILL.md (or skill.md |
| 111 | + // case-insensitively). |
| 112 | + if (parts.size() == 2 && Ascii.equalsIgnoreCase(parts.get(1), "SKILL.md")) { |
| 113 | + String skillName = parts.get(0); |
| 114 | + String logicalName = skillName.replace('_', '-'); |
| 115 | + if (skillMdMap.containsKey(logicalName)) { |
| 116 | + ResourceInfo existing = skillMdMap.get(logicalName); |
| 117 | + throw new SkillSourceException( |
| 118 | + "Conflicting SKILL.md files found for skill '" |
| 119 | + + logicalName |
| 120 | + + "': " |
| 121 | + + existing.getResourceName() |
| 122 | + + " and " |
| 123 | + + info.getResourceName(), |
| 124 | + SKILL_LOAD_ERROR); |
| 125 | + } |
| 126 | + skillMdMap.put(logicalName, info); |
| 127 | + } |
| 128 | + } |
| 129 | + return ImmutableMap.copyOf(skillMdMap); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Single<ImmutableList<String>> listResources(String skillName, String resourceDirectory) { |
| 134 | + String logicalSkillName = skillName.replace('_', '-'); |
| 135 | + String prefix = |
| 136 | + resourceDirectory.isEmpty() |
| 137 | + ? "" |
| 138 | + : (resourceDirectory.endsWith("/") ? resourceDirectory : resourceDirectory + "/"); |
| 139 | + |
| 140 | + // Support both standard ADK hyphenated directories and legacy underscore directories. |
| 141 | + String hyphenatedDir = logicalSkillName; |
| 142 | + String underscoredDir = logicalSkillName.replace('-', '_'); |
| 143 | + |
| 144 | + return allResourcesSingle.map( |
| 145 | + resources -> |
| 146 | + resources.stream() |
| 147 | + .map(info -> info.getResourceName().substring(baseResourcePath.length())) |
| 148 | + .filter( |
| 149 | + relPath -> |
| 150 | + relPath.startsWith(hyphenatedDir + "/" + prefix) |
| 151 | + || relPath.startsWith(underscoredDir + "/" + prefix)) |
| 152 | + .map( |
| 153 | + relPath -> { |
| 154 | + if (relPath.startsWith(hyphenatedDir + "/")) { |
| 155 | + return relPath.substring(hyphenatedDir.length() + 1); |
| 156 | + } else { |
| 157 | + return relPath.substring(underscoredDir.length() + 1); |
| 158 | + } |
| 159 | + }) |
| 160 | + .filter(path -> !Ascii.equalsIgnoreCase(path, "SKILL.md")) |
| 161 | + .collect(toImmutableList())); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + protected Flowable<SkillMdPath<ResourceInfo>> listSkills() { |
| 166 | + return skillMdsSingle |
| 167 | + .flattenAsFlowable(ImmutableMap::entrySet) |
| 168 | + .map(entry -> new SkillMdPath<>(entry.getKey(), entry.getValue())); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + protected Single<ResourceInfo> findSkillMdPath(String skillName) { |
| 173 | + String logicalSkillName = skillName.replace('_', '-'); |
| 174 | + return skillMdsSingle |
| 175 | + .map(map -> Optional.ofNullable(map.get(logicalSkillName))) |
| 176 | + .flatMap( |
| 177 | + opt -> |
| 178 | + opt.isPresent() |
| 179 | + ? Single.just(opt.get()) |
| 180 | + : Single.error( |
| 181 | + new SkillSourceException( |
| 182 | + "SKILL.md not found for skill: " + logicalSkillName, SKILL_NOT_FOUND))); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + protected Single<ResourceInfo> findResourcePath(String skillName, String resourcePath) { |
| 187 | + String logicalSkillName = skillName.replace('_', '-'); |
| 188 | + // Support both standard ADK hyphenated directories and legacy underscore directories. |
| 189 | + String hyphenatedDir = logicalSkillName; |
| 190 | + String underscoredDir = logicalSkillName.replace('-', '_'); |
| 191 | + |
| 192 | + String hyphenatedPath = baseResourcePath + hyphenatedDir + "/" + resourcePath; |
| 193 | + String underscoredPath = baseResourcePath + underscoredDir + "/" + resourcePath; |
| 194 | + |
| 195 | + return allResourcesSingle |
| 196 | + .map( |
| 197 | + resources -> |
| 198 | + resources.stream() |
| 199 | + .filter( |
| 200 | + info -> |
| 201 | + info.getResourceName().equals(hyphenatedPath) |
| 202 | + || info.getResourceName().equals(underscoredPath)) |
| 203 | + .findFirst()) |
| 204 | + .flatMap( |
| 205 | + opt -> |
| 206 | + opt.isPresent() |
| 207 | + ? Single.just(opt.get()) |
| 208 | + : Single.error( |
| 209 | + new SkillSourceException( |
| 210 | + "Resource not found: " |
| 211 | + + resourcePath |
| 212 | + + " for skill: " |
| 213 | + + logicalSkillName, |
| 214 | + RESOURCE_NOT_FOUND))); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + protected ReadableByteChannel openChannel(ResourceInfo path) throws IOException { |
| 219 | + return Channels.newChannel(path.asByteSource().openStream()); |
| 220 | + } |
| 221 | +} |
0 commit comments