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
119 changes: 119 additions & 0 deletions core/src/main/java/com/google/adk/skills/AbstractSkillSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.skills;

import static com.google.common.base.Preconditions.checkArgument;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UncheckedIOException;

/**
* Abstract base class for SkillSource implementations reading from markdown files.
*
* @param <PathT> the type of path object
*/
public abstract class AbstractSkillSource<PathT> implements SkillSource {

private static final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());

@Override
public Frontmatter loadFrontmatter(String skillName) {
PathT skillMdPath = getSkillMdPath(skillName);
return loadFrontmatter(skillMdPath);
}

protected final Frontmatter loadFrontmatter(PathT skillMdPath) {
try (BufferedReader reader = openSkillReader(skillMdPath)) {
String yaml = readFrontmatterYaml(reader);
Frontmatter frontmatter = parseFrontmatter(yaml);
String skillName = getSkillNameFromPath(skillMdPath);
checkArgument(
frontmatter.name().equals(skillName),
"Skill name '%s' does not match directory name '%s'.",
frontmatter.name(),
skillName);
return frontmatter;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public String loadInstructions(String skillName) {
PathT skillMdPath = getSkillMdPath(skillName);
try (BufferedReader reader = openSkillReader(skillMdPath)) {
return readInstructions(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/** Returns the path to the SKILL.md file for the given skill. */
protected abstract PathT getSkillMdPath(String skillName);

/** Returns the skill name based on the given SKILL.md path. */
protected abstract String getSkillNameFromPath(PathT skillMdPath);

/** Opens a reader for the SKILL.md file of the given skill. */
protected abstract BufferedReader openSkillReader(PathT skillMdPath);

private String readFrontmatterYaml(BufferedReader reader) throws IOException {
String line = reader.readLine();
checkArgument(line != null && line.trim().equals("---"), "Skill file must start with ---");

StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
if (line.trim().equals("---")) {
return sb.toString();
}
sb.append(line).append("\n");
}
throw new IllegalArgumentException("Skill file frontmatter not properly closed with ---");
}

private String readInstructions(BufferedReader reader) throws IOException {
// Skip the frontmatter block
String line = reader.readLine();
checkArgument(line != null && line.trim().equals("---"), "Skill file must start with ---");
boolean dashClosed = false;
while ((line = reader.readLine()) != null) {
if (line.trim().equals("---")) {
dashClosed = true;
break;
}
}
checkArgument(dashClosed, "Skill file frontmatter not properly closed with ---");

// Read the instructions till the end of the file
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString().trim();
}

private Frontmatter parseFrontmatter(String yaml) {
try {
return yamlMapper.readValue(yaml, Frontmatter.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
143 changes: 143 additions & 0 deletions core/src/main/java/com/google/adk/skills/Frontmatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.skills;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.adk.JsonBaseModel;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.escape.Escaper;
import com.google.common.html.HtmlEscapers;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

/** L1 skill content: metadata parsed from SKILL.md for skill discovery. */
@AutoValue
@JsonDeserialize(builder = Frontmatter.Builder.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class Frontmatter extends JsonBaseModel {

private static final Pattern NAME_PATTERN = Pattern.compile("^[a-z0-9]+(-[a-z0-9]+)*$");

/** Skill name in kebab-case. */
@JsonProperty("name")
public abstract String name();

/** What the skill does and when the model should use it. */
@JsonProperty("description")
public abstract String description();

/** License for the skill. */
@JsonProperty("license")
public abstract Optional<String> license();

/** Compatibility information for the skill. */
@JsonProperty("compatibility")
public abstract Optional<String> compatibility();

/** A space-delimited list of tools that are pre-approved to run. */
@JsonProperty("allowed-tools")
public abstract Optional<String> allowedTools();

/** Key-value pairs for client-specific properties. */
@JsonProperty("metadata")
public abstract ImmutableMap<String, Object> metadata();

public String toXml() {
Escaper escaper = HtmlEscapers.htmlEscaper();
return String.format(
"""
<skill>
<name>
%s
</name>
<description>
%s
</description>
</skill>
""",
escaper.escape(name()), escaper.escape(description()));
}

public static Builder builder() {
return new AutoValue_Frontmatter.Builder().metadata(ImmutableMap.of());
}

@AutoValue.Builder
public abstract static class Builder {

@JsonCreator
private static Builder create() {
return builder();
}

@CanIgnoreReturnValue
@JsonProperty("name")
public abstract Builder name(String name);

@CanIgnoreReturnValue
@JsonProperty("description")
public abstract Builder description(String description);

@CanIgnoreReturnValue
@JsonProperty("license")
public abstract Builder license(String license);

@CanIgnoreReturnValue
@JsonProperty("compatibility")
public abstract Builder compatibility(String compatibility);

@CanIgnoreReturnValue
@JsonProperty("allowed-tools")
@JsonAlias({"allowed_tools"})
public abstract Builder allowedTools(String allowedTools);

@CanIgnoreReturnValue
@JsonProperty("metadata")
public abstract Builder metadata(Map<String, Object> metadata);

abstract Frontmatter autoBuild();

public Frontmatter build() {
Frontmatter fm = autoBuild();
if (fm.name().length() > 64) {
throw new IllegalArgumentException("name must be at most 64 characters");
}
if (!NAME_PATTERN.matcher(fm.name()).matches()) {
throw new IllegalArgumentException(
"name must be lowercase kebab-case (a-z, 0-9, hyphens), with no leading, trailing, or"
+ " consecutive hyphens");
}
if (fm.description().isEmpty()) {
throw new IllegalArgumentException("description must not be empty");
}
if (fm.description().length() > 1024) {
throw new IllegalArgumentException("description must be at most 1024 characters");
}
if (fm.compatibility().isPresent() && fm.compatibility().get().length() > 500) {
throw new IllegalArgumentException("compatibility must be at most 500 characters");
}
return fm;
}
}
}
Loading
Loading