Extends #30882's `model_invocable` semantics to workspace skills and to stored skills created through the API. Claude Code and Xum let skill authors set `disable-model-invocation: true` in SKILL.md frontmatter, so agents never pick the skill on their own and it runs only when a person invokes it. Coder Agents honored the key only when someone imported a SKILL.md file while adding a project skill (#30884). ## What changes - **Workspace skills:** when the pinned SKILL.md sets the key to the YAML boolean `true`, chatd leaves the skill out of `<available-skills>` before merging sources, so a listed skill with the same name keeps its bare name, and `read_skill` reports it as not found. The skill stays in the `/` menu, and invoking it still loads its body. `read_skill_file` serves its supporting files under the bare name or `workspace/<name>` and refuses its `SKILL.md` (case-insensitive, after `path.Clean`). Skills without the key behave as on the base. - **Invoked workspace skills:** invoking a workspace skill now stores the pinned skill directory on the invocation part, and the prompt renders it as `dir` on `<invoked-skill>`, escaped as an attribute value. Before, only `read_skill` gave the model a skill's directory, and it refuses a hidden skill, so a hidden skill could not run the scripts it bundles. The part also stores the chat's workspace, and the prompt shows `dir` only while the chat stays bound to it, as it does for workspace file references. This changes #30861's invocation code. Hook payloads drop the directory and its workspace like the body, and API responses strip both. - **Stored skills:** a create request whose content sets the key and that omits `model_invocable` stores `model_invocable = false`. An explicit value wins, and PATCH is unchanged. This covers personal, organization, and project skills, which share `createSkill`. - `workspacesdk.SkillDisablesModelInvocation` reads the key. The `CreateSkillRequest.ModelInvocable` doc comment, the generated API docs, and `extending-agents.md` (the SKILL.md format, a new "Turn off automatic use" section, and the notes on the skill index, the skill tools, and aliases) describe the new behavior. ## Follow-ups - The personal and organization skill editors have no "Agents can use it automatically" switch yet. Only the project editor from #30884 has one. ## Testing - Go tests cover the parser, the turn's skill index and aliases, `read_skill`, `read_skill_file`, invocation of a hidden skill and the directory it carries (attribute escaping, no directory after the chat changes workspace, no directory in hook payloads), and the create default for personal and organization skills. Each new test fails when its code path is removed. - Remote dogfood UAT with a real model failed rounds 1 to 3. Rounds 1 and 2 found the hidden SKILL.md still readable through `read_skill_file` by symlinks and stale pins, plus a bare-name lookup bug and a docs gap that round 2 fixed. Round 3 found that the content check added against those bypasses refused a listed skill's file whose own frontmatter sets the key. After round 3 we narrowed the rule to the hidden skill's `SKILL.md` path. The key decides what agents are offered (the index and `read_skill`), not which files they can read: `read_file` and `execute` already open any workspace file, so a content check in `read_skill_file` protected nothing and kept producing edge cases. - Round 4 passed on `c9ff25dc29f`. Later commits added a behavior-neutral cleanup (`cd617533c2e`), one test case from the test audit (`044c1e6e23a`), and a comment fix (`9ec3768b18b`), which passed validation without another UAT round. Round 5 passed on `2b518c6c2b1`, which adds the skill directory. A real model ran hidden skills' scripts from the `dir` on `<invoked-skill>`, including directories whose paths contain `&`, `'`, `"`, `<` and `>`, and API responses carried no directory. Merging main after #30884 landed (`2d7d3cb9674`) did not change this PR's diff, so UAT was not rerun. `8f043b2a342`, which shows the directory only while the chat stays bound to the skill's workspace, has Go tests but no UAT round. - The doc-check skill ran in author self-check mode on `2d7d3cb9674`, and `27de7f5c6d7` fixes its findings: the skill index sentence said every skill is listed, the stored-skill sentence claimed any project editor import honors the key and presented the API default as fixed, the API path came before the UI path, and some wording broke the docs style rules. The fix is docs-only, so UAT was not rerun. - Polish and the test audit stayed inside the implementation diff. No new dependencies. - Codex review found no major issues on `9ec3768b18b`, then raised one P2 on the same head when the PR was marked ready: an invoked hidden skill gets no skill directory. A probe confirmed it, and `2b518c6c2b1` fixes it. Its earlier finding on that head (honor `CODER_AGENT_EXP_SKILL_META_FILE`) was refuted, because the pinned skill snapshot always reads `SKILL.md`. Both threads are resolved, and Codex found no major issues on `2b518c6c2b1` or on the merge `2d7d3cb9674`. - On `27de7f5c6d7`, Codex raised two P2s. One was confirmed: an invoked skill's directory stayed in the prompt after the chat moved to another workspace, which a prompt test showed on that head, and `8f043b2a342` fixes it. The other was declined: on Windows, `scripts\..\SKILL.md` gets past the `SKILL.md` check in `read_skill_file`, but that check is not an access boundary (`read_file` opens any path), and Windows has more aliases (`SKILL.md.`, `SKILL.md::$DATA`, `SKILL~1.MD`) that normalizing backslashes would not stop. On `8f043b2a342`, Codex asked to bind the directory to the workspace build instead. That was declined: every workspace start is a new build with new agent IDs, so the directory would drop after each stop and start, and the pinned body and the live files can diverge without a rebuild anyway, as they do for `read_skill`. A later request on that head asked to limit a hidden skill's `read_skill_file` access to invocations in the current workspace. That was declined too: `read_skill_file` resolves names against the current pin for listed skills on main as well, and the key decides which skills agents are offered, not which files they can read. All four threads are resolved. `a697dcd927f` adds the `format:"uuid"` tag that `TestSDKFieldsFormatted` requires on the new field, and Codex found no major issues on `a697dcd927f`. `ae70b2c533f` is docs-only: it applies the doc-check suggestion to say that an invoked skill's directory is included only while the chat stays in that workspace. The author doc-check self-check found no blocking findings on it, and Codex found no major issues on `ae70b2c533f`. > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description. --------- Co-authored-by: Jaayden Halko <jaayden.halko@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
600 lines
18 KiB
Go
600 lines
18 KiB
Go
package chattool
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"path"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"charm.land/fantasy"
|
|
"golang.org/x/xerrors"
|
|
|
|
skillspkg "github.com/coder/coder/v2/coderd/x/skills"
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
)
|
|
|
|
const (
|
|
maxSkillMetaBytes = workspacesdk.MaxSkillMetaBytes
|
|
maxSkillFileBytes = 512 * 1024
|
|
|
|
// AvailableSkillsOpenTag is the XML start tag for the skill index block.
|
|
AvailableSkillsOpenTag = "<available-skills>"
|
|
// AvailableSkillsCloseTag is the XML end tag for the skill index block.
|
|
AvailableSkillsCloseTag = "</available-skills>"
|
|
|
|
// maxSkillIndexDescriptionRunes is the Agent Skills specification limit
|
|
// for a description. Longer stored descriptions are only cut in the index
|
|
// so it stays bounded; read_skill still returns the full skill.
|
|
maxSkillIndexDescriptionRunes = 1024
|
|
)
|
|
|
|
// SkillMeta is the frontmatter from a skill meta file discovered in a
|
|
// workspace. It carries just enough information to list the skill
|
|
// in the prompt index without reading the full body.
|
|
type SkillMeta struct {
|
|
Name string
|
|
Description string
|
|
// Dir is the absolute path to the skill directory inside
|
|
// the workspace filesystem.
|
|
Dir string
|
|
// Meta is the verbatim skill meta file (SKILL.md) content the
|
|
// agent pushed in the workspace context snapshot: front-matter
|
|
// plus body. When present, read_skill serves the body from it
|
|
// instead of reading the file back over the workspace
|
|
// connection, so a pinned chat keeps returning the same
|
|
// instructions even when the workspace is unreachable. It is
|
|
// empty on the legacy per-turn discovery path, where the body is
|
|
// read live.
|
|
Meta []byte
|
|
}
|
|
|
|
// ModelInvocable reports whether agents may list and load the skill on
|
|
// their own. A skill whose pinned meta file sets
|
|
// disable-model-invocation: true stays hidden from them but still loads
|
|
// when a person invokes it. A skill without a pinned meta file stays
|
|
// model-invocable.
|
|
func (s SkillMeta) ModelInvocable() bool {
|
|
return !workspacesdk.SkillDisablesModelInvocation(s.pinnedMeta())
|
|
}
|
|
|
|
func (s SkillMeta) pinnedMeta() string {
|
|
raw := s.Meta
|
|
if int64(len(raw)) > maxSkillMetaBytes {
|
|
raw = raw[:maxSkillMetaBytes]
|
|
}
|
|
return string(raw)
|
|
}
|
|
|
|
// SkillContent is the full body of a skill, loaded on demand
|
|
// when the model calls read_skill.
|
|
type SkillContent struct {
|
|
SkillMeta
|
|
// Body is the markdown content after the frontmatter
|
|
// delimiters have been stripped.
|
|
Body string
|
|
// Files lists relative paths of supporting files in the
|
|
// skill directory (everything except the skill meta file).
|
|
Files []string
|
|
}
|
|
|
|
// FormatResolvedSkillIndex renders an XML block listing all source-aware
|
|
// skills. Aliases are the names the model should pass to the skill tools.
|
|
func FormatResolvedSkillIndex(resolved []skillspkg.ResolvedSkill) string {
|
|
if len(resolved) == 0 {
|
|
return ""
|
|
}
|
|
|
|
entries := make([]skillIndexEntry, 0, len(resolved))
|
|
hasQualifiedAlias := false
|
|
hasWorkspaceSkill := false
|
|
for _, s := range resolved {
|
|
entries = append(entries, skillIndexEntry{
|
|
Alias: s.Alias,
|
|
Description: truncateSkillIndexDescription(s.Description),
|
|
})
|
|
if s.Source == skillspkg.SourceWorkspace {
|
|
hasWorkspaceSkill = true
|
|
}
|
|
if s.Alias == skillspkg.QualifiedAlias(s.Source, s.Name) {
|
|
hasQualifiedAlias = true
|
|
}
|
|
}
|
|
return renderSkillIndex(entries, skillIndexFormatOptions{
|
|
includeQualifiedAliasInstruction: hasQualifiedAlias,
|
|
includeReadSkillFileInstruction: hasWorkspaceSkill,
|
|
})
|
|
}
|
|
|
|
func truncateSkillIndexDescription(description string) string {
|
|
if utf8.RuneCountInString(description) <= maxSkillIndexDescriptionRunes {
|
|
return description
|
|
}
|
|
runes := 0
|
|
for i := range description {
|
|
// Reserve one rune for the ellipsis so the result stays within the limit.
|
|
if runes == maxSkillIndexDescriptionRunes-1 {
|
|
return description[:i] + "…"
|
|
}
|
|
runes++
|
|
}
|
|
return description
|
|
}
|
|
|
|
type skillIndexEntry struct {
|
|
Alias string
|
|
Description string
|
|
}
|
|
|
|
type skillIndexFormatOptions struct {
|
|
includeQualifiedAliasInstruction bool
|
|
includeReadSkillFileInstruction bool
|
|
}
|
|
|
|
func renderSkillIndex(entries []skillIndexEntry, opts skillIndexFormatOptions) string {
|
|
if len(entries) == 0 {
|
|
return ""
|
|
}
|
|
|
|
var b strings.Builder
|
|
_, _ = b.WriteString(AvailableSkillsOpenTag + "\n")
|
|
_, _ = b.WriteString(
|
|
"Use read_skill to load a skill's full instructions " +
|
|
"before following them.\n",
|
|
)
|
|
if opts.includeReadSkillFileInstruction {
|
|
_, _ = b.WriteString(
|
|
"Use read_skill_file to read supporting files " +
|
|
"referenced by a workspace skill.\n",
|
|
)
|
|
}
|
|
if opts.includeQualifiedAliasInstruction {
|
|
_, _ = b.WriteString(
|
|
"When a skill is listed with a source prefix, such as " +
|
|
"personal/name, pass that qualified alias to read_skill.\n",
|
|
)
|
|
}
|
|
_, _ = b.WriteString("\n")
|
|
for _, s := range entries {
|
|
_, _ = b.WriteString("- ")
|
|
_, _ = b.WriteString(s.Alias)
|
|
if s.Description != "" {
|
|
_, _ = b.WriteString(": ")
|
|
_, _ = b.WriteString(s.Description)
|
|
}
|
|
_, _ = b.WriteString("\n")
|
|
}
|
|
_, _ = b.WriteString(AvailableSkillsCloseTag)
|
|
return b.String()
|
|
}
|
|
|
|
// listSkillFiles lists the supporting files in a skill directory,
|
|
// excluding the skill meta file itself. Directory entries are
|
|
// suffixed with "/" so the model can tell them apart from files.
|
|
func listSkillFiles(
|
|
ctx context.Context,
|
|
conn workspacesdk.AgentConn,
|
|
dir, metaFile string,
|
|
) ([]string, error) {
|
|
lsResp, err := conn.LS(ctx, "", workspacesdk.LSRequest{
|
|
Path: []string{dir},
|
|
Relativity: workspacesdk.LSRelativityRoot,
|
|
})
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("list skill directory: %w", err)
|
|
}
|
|
|
|
var files []string
|
|
for _, entry := range lsResp.Contents {
|
|
if entry.Name == metaFile {
|
|
continue
|
|
}
|
|
name := entry.Name
|
|
if entry.IsDir {
|
|
name += "/"
|
|
}
|
|
files = append(files, name)
|
|
}
|
|
return files, nil
|
|
}
|
|
|
|
// loadPinnedWorkspaceSkillContent builds skill content from the
|
|
// SKILL.md the agent pushed in the workspace context snapshot. The
|
|
// body is parsed from the pinned bytes without dialing the workspace,
|
|
// so it keeps working when the workspace is unreachable. The
|
|
// supporting-file list is a best-effort live lookup, because the
|
|
// snapshot carries only the meta file (per the agent push contract):
|
|
// a missing connection or LS failure yields an empty file list rather
|
|
// than failing the read.
|
|
func loadPinnedWorkspaceSkillContent(
|
|
ctx context.Context,
|
|
options ReadSkillOptions,
|
|
skill SkillMeta,
|
|
) (SkillContent, error) {
|
|
body, err := PinnedWorkspaceSkillBody(skill)
|
|
if err != nil {
|
|
return SkillContent{}, err
|
|
}
|
|
|
|
return SkillContent{
|
|
SkillMeta: skill,
|
|
Body: body,
|
|
Files: bestEffortSkillFiles(ctx, options, skill),
|
|
}, nil
|
|
}
|
|
|
|
// PinnedWorkspaceSkillBody parses the body of a workspace skill from the
|
|
// SKILL.md pinned in the workspace context snapshot.
|
|
func PinnedWorkspaceSkillBody(skill SkillMeta) (string, error) {
|
|
_, _, body, err := workspacesdk.ParseSkillFrontmatter(skill.pinnedMeta())
|
|
if err != nil {
|
|
return "", xerrors.Errorf("parse skill frontmatter: %w", err)
|
|
}
|
|
return body, nil
|
|
}
|
|
|
|
// bestEffortSkillFiles lists a pinned skill's supporting files over the
|
|
// workspace connection, returning nil when the connection or listing
|
|
// fails so an unreachable workspace never blocks read_skill from
|
|
// returning the pinned body.
|
|
func bestEffortSkillFiles(
|
|
ctx context.Context,
|
|
options ReadSkillOptions,
|
|
skill SkillMeta,
|
|
) []string {
|
|
if options.GetWorkspaceConn == nil {
|
|
return nil
|
|
}
|
|
conn, err := options.GetWorkspaceConn(ctx)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
files, err := listSkillFiles(ctx, conn, skill.Dir, DefaultSkillMetaFile)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return files
|
|
}
|
|
|
|
// LoadSkillFile reads a supporting file from a skill's directory.
|
|
// The relativePath is validated to prevent directory traversal and
|
|
// access to hidden files.
|
|
func LoadSkillFile(
|
|
ctx context.Context,
|
|
conn workspacesdk.AgentConn,
|
|
skill SkillMeta,
|
|
relativePath string,
|
|
) (string, error) {
|
|
if err := validateSkillFilePath(relativePath); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
fullPath := path.Join(skill.Dir, relativePath)
|
|
|
|
reader, _, err := conn.ReadFile(
|
|
ctx, fullPath, 0, maxSkillFileBytes+1,
|
|
)
|
|
if err != nil {
|
|
return "", xerrors.Errorf(
|
|
"read skill file: %w", err,
|
|
)
|
|
}
|
|
raw, err := io.ReadAll(io.LimitReader(reader, maxSkillFileBytes+1))
|
|
reader.Close()
|
|
if err != nil {
|
|
return "", xerrors.Errorf(
|
|
"read skill file bytes: %w", err,
|
|
)
|
|
}
|
|
|
|
if int64(len(raw)) > maxSkillFileBytes {
|
|
raw = raw[:maxSkillFileBytes]
|
|
}
|
|
|
|
return string(raw), nil
|
|
}
|
|
|
|
// validateSkillFilePath rejects paths that could escape the skill
|
|
// directory or access hidden files. Only forward-relative,
|
|
// non-hidden paths are allowed.
|
|
func validateSkillFilePath(p string) error {
|
|
if p == "" {
|
|
return xerrors.New("path is required")
|
|
}
|
|
if strings.HasPrefix(p, "/") {
|
|
return xerrors.New(
|
|
"absolute paths are not allowed",
|
|
)
|
|
}
|
|
for _, component := range strings.Split(p, "/") {
|
|
if component == ".." {
|
|
return xerrors.New(
|
|
"path traversal is not allowed",
|
|
)
|
|
}
|
|
if strings.HasPrefix(component, ".") {
|
|
return xerrors.New(
|
|
"hidden file components are not allowed",
|
|
)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DefaultSkillMetaFile is the fallback skill meta file name used
|
|
// when loading skill bodies on demand from older agents.
|
|
const DefaultSkillMetaFile = "SKILL.md"
|
|
|
|
// ReadSkillOptions configures the read_skill and read_skill_file
|
|
// tools.
|
|
type ReadSkillOptions struct {
|
|
GetWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
|
|
GetSkills func() []SkillMeta
|
|
ResolveAlias func(string) (skillspkg.ResolvedSkill, error)
|
|
// LoadStoredSkillBody loads the current body of a personal,
|
|
// organization, or project skill. It returns skillspkg.ErrSkillNotFound
|
|
// when the skill is gone, disabled, or no longer readable by the chat
|
|
// owner.
|
|
LoadStoredSkillBody func(context.Context, skillspkg.Skill) (skillspkg.ParsedSkill, error)
|
|
}
|
|
|
|
// ReadSkillArgs are the parameters accepted by read_skill.
|
|
type ReadSkillArgs struct {
|
|
Name string `json:"name" description:"The name or qualified alias of the skill to read."`
|
|
}
|
|
|
|
// ReadSkill returns an AgentTool that reads the full instructions
|
|
// for a skill by name. The model should call this before
|
|
// following any skill's instructions.
|
|
func ReadSkill(options ReadSkillOptions) fantasy.AgentTool {
|
|
return fantasy.NewAgentTool(
|
|
"read_skill",
|
|
"Read the full instructions for a skill by name. "+
|
|
"Returns the skill meta file body and a list of "+
|
|
"supporting files. Use read_skill before "+
|
|
"following a skill's instructions.",
|
|
func(ctx context.Context, args ReadSkillArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
if args.Name == "" {
|
|
return fantasy.NewTextErrorResponse(
|
|
"name is required",
|
|
), nil
|
|
}
|
|
|
|
resolved, err := resolveSkillAlias(options, args.Name)
|
|
if err != nil {
|
|
return skillResolveErrorResponse(args.Name, err), nil
|
|
}
|
|
|
|
switch resolved.Source {
|
|
case skillspkg.SourcePersonal, skillspkg.SourceOrganization, skillspkg.SourceProject:
|
|
if options.LoadStoredSkillBody == nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
"stored skill loader is not configured",
|
|
), nil
|
|
}
|
|
content, err := options.LoadStoredSkillBody(ctx, resolved.Skill)
|
|
if err != nil {
|
|
if xerrors.Is(err, skillspkg.ErrSkillNotFound) {
|
|
return skillNotFoundResponse(args.Name), nil
|
|
}
|
|
return fantasy.NewTextErrorResponse(
|
|
fmt.Sprintf("failed to load %s skill %q", resolved.Source, args.Name),
|
|
), nil
|
|
}
|
|
return toolResponse(map[string]any{
|
|
"name": args.Name,
|
|
"body": content.Body,
|
|
"files": []string{},
|
|
}), nil
|
|
case skillspkg.SourceWorkspace:
|
|
content, response, ok := readWorkspaceSkillBody(ctx, options, args.Name, resolved.Name)
|
|
if ok {
|
|
return response, nil
|
|
}
|
|
// Include the absolute skill directory so the agent can
|
|
// reach supporting files with read_file and execute.
|
|
return toolResponse(map[string]any{
|
|
"name": args.Name,
|
|
"dir": content.Dir,
|
|
"body": content.Body,
|
|
"files": nonNilFiles(content.Files),
|
|
}), nil
|
|
default:
|
|
return skillNotFoundResponse(args.Name), nil
|
|
}
|
|
},
|
|
)
|
|
}
|
|
|
|
// ReadSkillFileArgs are the parameters accepted by
|
|
// read_skill_file.
|
|
type ReadSkillFileArgs struct {
|
|
Name string `json:"name" description:"The name or qualified alias of the skill to read."`
|
|
Path string `json:"path" description:"Relative path to a file in the skill directory (e.g. roles/security-reviewer.md)."`
|
|
}
|
|
|
|
// ReadSkillFile returns an AgentTool that reads a supporting file
|
|
// from a skill's directory.
|
|
func ReadSkillFile(options ReadSkillOptions) fantasy.AgentTool {
|
|
return fantasy.NewAgentTool(
|
|
"read_skill_file",
|
|
"Read a supporting file from a skill's directory "+
|
|
"(e.g. roles/security-reviewer.md).",
|
|
func(ctx context.Context, args ReadSkillFileArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
if args.Name == "" {
|
|
return fantasy.NewTextErrorResponse(
|
|
"name is required",
|
|
), nil
|
|
}
|
|
if args.Path == "" {
|
|
return fantasy.NewTextErrorResponse(
|
|
"path is required",
|
|
), nil
|
|
}
|
|
|
|
skill, response, ok := resolveSkillFileTarget(options, args.Name)
|
|
if !ok {
|
|
return response, nil
|
|
}
|
|
// Only a person's invocation loads the body of a skill hidden
|
|
// from the model, so its meta file is not served here.
|
|
if !skill.ModelInvocable() && strings.EqualFold(path.Clean(args.Path), DefaultSkillMetaFile) {
|
|
return hiddenSkillMetaFileResponse(args.Name, args.Path), nil
|
|
}
|
|
|
|
// Validate the path early so we reject bad
|
|
// inputs before dialing the workspace agent.
|
|
if err := validateSkillFilePath(args.Path); err != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
err.Error(),
|
|
), nil
|
|
}
|
|
|
|
if options.GetWorkspaceConn == nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
"workspace connection resolver is not configured",
|
|
), nil
|
|
}
|
|
conn, err := options.GetWorkspaceConn(ctx)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
err.Error(),
|
|
), nil
|
|
}
|
|
|
|
content, err := LoadSkillFile(
|
|
ctx, conn, skill, args.Path,
|
|
)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
err.Error(),
|
|
), nil
|
|
}
|
|
|
|
return toolResponse(map[string]any{
|
|
"content": content,
|
|
}), nil
|
|
},
|
|
)
|
|
}
|
|
|
|
func hiddenSkillMetaFileResponse(name, filePath string) fantasy.ToolResponse {
|
|
return fantasy.NewTextErrorResponse(fmt.Sprintf(
|
|
"%s of skill %q is the meta file of a skill hidden from agents and cannot be read with read_skill_file",
|
|
filePath, name,
|
|
))
|
|
}
|
|
|
|
// resolveSkillFileTarget finds the workspace skill whose supporting files
|
|
// read_skill_file serves. Besides the skills listed for the model, it
|
|
// accepts a workspace skill hidden from the model by bare or qualified
|
|
// name: a person can invoke such a skill, and its body may reference
|
|
// supporting files.
|
|
func resolveSkillFileTarget(options ReadSkillOptions, name string) (SkillMeta, fantasy.ToolResponse, bool) {
|
|
// Stored skills have no supporting files, so a name that names a
|
|
// hidden workspace skill means that skill, even when the bare name
|
|
// also names one or more listed stored skills.
|
|
bareName := strings.TrimPrefix(name, skillspkg.QualifiedAlias(skillspkg.SourceWorkspace, ""))
|
|
if skill, ok := findSkill(options.GetSkills, bareName); ok && !skill.ModelInvocable() {
|
|
return skill, fantasy.ToolResponse{}, true
|
|
}
|
|
resolved, err := resolveSkillAlias(options, name)
|
|
if err != nil {
|
|
return SkillMeta{}, skillResolveErrorResponse(name, err), false
|
|
}
|
|
if resolved.Source != skillspkg.SourceWorkspace {
|
|
return SkillMeta{}, fantasy.NewTextErrorResponse(fmt.Sprintf(
|
|
"read_skill_file is not supported for %s skills (no supporting files)",
|
|
resolved.Source,
|
|
)), false
|
|
}
|
|
skill, ok := findSkill(options.GetSkills, resolved.Name)
|
|
if !ok {
|
|
return SkillMeta{}, skillNotFoundResponse(name), false
|
|
}
|
|
return skill, fantasy.ToolResponse{}, true
|
|
}
|
|
|
|
func resolveSkillAlias(options ReadSkillOptions, name string) (skillspkg.ResolvedSkill, error) {
|
|
if options.ResolveAlias != nil {
|
|
return options.ResolveAlias(name)
|
|
}
|
|
|
|
skill, ok := findSkill(options.GetSkills, name)
|
|
if !ok || !skill.ModelInvocable() {
|
|
return skillspkg.ResolvedSkill{}, skillspkg.ErrSkillNotFound
|
|
}
|
|
return skillspkg.ResolvedSkill{
|
|
Skill: skillspkg.Skill{
|
|
Name: skill.Name,
|
|
Description: skill.Description,
|
|
Source: skillspkg.SourceWorkspace,
|
|
},
|
|
Alias: skill.Name,
|
|
}, nil
|
|
}
|
|
|
|
func readWorkspaceSkillBody(
|
|
ctx context.Context,
|
|
options ReadSkillOptions,
|
|
requestedName string,
|
|
canonicalName string,
|
|
) (SkillContent, fantasy.ToolResponse, bool) {
|
|
skill, ok := findSkill(options.GetSkills, canonicalName)
|
|
if !ok {
|
|
return SkillContent{}, skillNotFoundResponse(requestedName), true
|
|
}
|
|
|
|
// The SKILL.md body travels in the workspace context snapshot, so it
|
|
// is served from the pin without dialing the workspace. The supporting
|
|
// file list is still a best-effort live lookup; see
|
|
// loadPinnedWorkspaceSkillContent.
|
|
content, err := loadPinnedWorkspaceSkillContent(ctx, options, skill)
|
|
if err != nil {
|
|
return SkillContent{}, fantasy.NewTextErrorResponse(err.Error()), true
|
|
}
|
|
return content, fantasy.ToolResponse{}, false
|
|
}
|
|
|
|
func skillResolveErrorResponse(name string, err error) fantasy.ToolResponse {
|
|
if xerrors.Is(err, skillspkg.ErrSkillNotFound) {
|
|
return skillNotFoundResponse(name)
|
|
}
|
|
if xerrors.Is(err, skillspkg.ErrSkillAmbiguous) {
|
|
return fantasy.NewTextErrorResponse(err.Error())
|
|
}
|
|
return fantasy.NewTextErrorResponse(
|
|
fmt.Sprintf("failed to resolve skill %q", name),
|
|
)
|
|
}
|
|
|
|
func skillNotFoundResponse(name string) fantasy.ToolResponse {
|
|
return fantasy.NewTextErrorResponse(
|
|
fmt.Sprintf("skill %q not found", name),
|
|
)
|
|
}
|
|
|
|
func nonNilFiles(files []string) []string {
|
|
if files == nil {
|
|
return []string{}
|
|
}
|
|
return files
|
|
}
|
|
|
|
// findSkill looks up a skill by name in the current skill list.
|
|
func findSkill(
|
|
getSkills func() []SkillMeta,
|
|
name string,
|
|
) (SkillMeta, bool) {
|
|
if getSkills == nil {
|
|
return SkillMeta{}, false
|
|
}
|
|
for _, s := range getSkills() {
|
|
if s.Name == name {
|
|
return s, true
|
|
}
|
|
}
|
|
return SkillMeta{}, false
|
|
}
|