skill

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package skill 定义 Agent 技能(skill)调用框架的基础设施。

技能是比工具(tool)更高一层的可复用能力单元:一个技能既包含"元信息" (Definition:名称 / 描述 / 入参 schema,用于暴露给模型做 function calling), 也包含"指令正文"(Instructions:注入到上下文中的 markdown 说明),以及 可选的"执行体"(Invoke:真正运行技能逻辑,产出结果)。

与 tool 包一样,本包只回答三个问题:

  • 有哪些技能(Definition / Instructions 描述给模型);
  • 如何组织它们(Registry 按名注册 / 解析);
  • 如何调用它们(Invoker 解析技能 → 校验参数 → 套用中间件 → 执行)。

上层 internal/agent 通过 SkillRunner 把技能执行桥接到 Runtime(事件流 + 权限门禁), 因此本包不依赖 agent,避免循环引用。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound 表示技能未注册。
	ErrNotFound = errors.New("skill: skill not found")
	// ErrInvalidArguments 表示技能参数非法(无法解析为输入结构或不是合法 JSON)。
	ErrInvalidArguments = errors.New("skill: invalid arguments")
)

技能相关错误。

Functions

func BooleanProperty

func BooleanProperty(description string) map[string]any

BooleanProperty 构造一个 boolean 类型属性。

func IntegerProperty

func IntegerProperty(description string) map[string]any

IntegerProperty 构造一个 integer 类型属性。

func NumberProperty

func NumberProperty(description string) map[string]any

NumberProperty 构造一个 number 类型属性。

func Schema

func Schema(description string, properties map[string]any, required ...string) map[string]any

Schema 构建一个 object 类型的 JSON Schema,用于描述技能入参。

func StringProperty

func StringProperty(description string) map[string]any

StringProperty 构造一个 string 类型属性。

Types

type Call

type Call struct {
	ID        string          `json:"id,omitempty"`        // 调用唯一 ID(模型生成)
	Name      string          `json:"name"`                // 技能名
	Arguments json.RawMessage `json:"arguments,omitempty"` // 参数(JSON)
}

Call 是一次待执行的技能调用。

type Definition

type Definition struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	InputSchema map[string]any `json:"input_schema,omitempty"`
}

Definition 描述一个技能,用于暴露给模型(function calling 的技能定义)。

与工具不同,技能的 InputSchema 可为 nil:纯指令型技能无需参数, 模型只需在上下文里看到 Instructions 并按需"启用"它。

type Func

type Func[In, Out any] struct {
	// contains filtered or unexported fields
}

Func 把普通函数适配为 Skill:In 为入参结构,Out 为返回结构。

Instructions 可用于附带指令正文;对纯函数型技能可留空。

func NewFunc

func NewFunc[In, Out any](manifest Manifest, fn func(context.Context, In) (Out, error)) *Func[In, Out]

NewFunc 基于一个强类型函数构建技能。

  • manifest:技能的元信息(名称 / 描述 / 入参 schema / 指令正文);
  • fn:实际执行逻辑。

func (*Func[In, Out]) Definition

func (f *Func[In, Out]) Definition() Definition

Definition 实现 Skill。

func (*Func[In, Out]) Instructions

func (f *Func[In, Out]) Instructions() string

Instructions 实现 Skill。

func (*Func[In, Out]) Invoke

func (f *Func[In, Out]) Invoke(ctx context.Context, args json.RawMessage) (Result, error)

Invoke 实现 Skill:解析入参 → 调用函数。

func (*Func[In, Out]) Version

func (f *Func[In, Out]) Version() string

Version 返回技能版本号(可为空)。

type Invoker

type Invoker struct {
	// contains filtered or unexported fields
}

Invoker 执行技能调用:解析技能 → 校验参数 → 套用中间件 → 执行。

语义约定:所有失败(技能不存在 / 参数非法 / 超时 / panic / 技能返回错误)都 收敛为 Result.IsError=true,而不会向上抛出中断整个流程——模型据此纠正行为。

func NewInvoker

func NewInvoker(registry *Registry, opts ...InvokerOption) *Invoker

NewInvoker 创建执行器;默认在最内层附带 Recover 中间件(panic 不逃逸)。

func (*Invoker) Invoke

func (i *Invoker) Invoke(ctx context.Context, call Call) Result

Invoke 执行一次技能调用,始终返回 Result(不会因技能失败而中断)。

type InvokerOption

type InvokerOption func(*Invoker)

InvokerOption 配置 Invoker。

func WithMiddlewares

func WithMiddlewares(ms ...Middleware) InvokerOption

WithMiddlewares 追加中间件(先加入的越靠内层)。

type Loader

type Loader struct{}

Loader 从文件系统加载技能(SKILL.md)。

布局约定:

  • 一个技能目录 = 一个技能;目录内 SKILL.md 描述该技能;
  • 也支持直接加载单个 SKILL.md 文件。

SKILL.md 采用 frontmatter + 正文:

---
name: my-skill
description: 技能描述
version: 1.0.0
---
# 指令正文
...

frontmatter 只解析 name / description / version 三个字段,其余内容作为 指令正文(Instructions)原样保留。

func NewLoader

func NewLoader() *Loader

NewLoader 创建加载器。

func (*Loader) Load

func (l *Loader) Load(path string) (Skill, error)

Load 加载单个 SKILL.md 文件为技能。

若 frontmatter 未声明 name,则回退为 SKILL.md 所在目录名。

func (*Loader) LoadDir

func (l *Loader) LoadDir(dir string) ([]Skill, error)

LoadDir 递归加载目录下所有 SKILL.md(每个技能目录一个),返回全部技能。

type Manifest

type Manifest struct {
	Definition
	Version      string `json:"version,omitempty"`
	Instructions string `json:"instructions,omitempty"` // markdown 指令正文,注入到上下文
}

Manifest 是一个技能的完整元信息(Definition + 指令正文 + 版本)。

func ParseManifest

func ParseManifest(content string) (Manifest, error)

ParseManifest 解析 SKILL.md 内容(frontmatter + 正文)为 Manifest。

type Middleware

type Middleware func(next Next) Next

Middleware 包裹技能执行:在真正调用技能前后做校验 / 超时 / 恢复 / 日志等。

中间件在 Invoker 中按注册顺序包裹(先注册的越靠内层)。约定:中间件应调用 next 继续执行,并在需要时改造 ctx / call / 返回值。

func Logging

func Logging(logf func(format string, args ...any)) Middleware

Logging 在技能执行前后记录日志;logf 为 nil 时使用标准库 log。

func Recover

func Recover() Middleware

Recover 把技能执行中的 panic 转为 error,避免单个技能拖垮整个调用流程。

func Timeout

func Timeout(d time.Duration) Middleware

Timeout 为技能执行设置超时;超时返回 error。

type Next

type Next func(ctx context.Context, call Call) (Result, error)

Next 是中间件链中的下一个处理器。

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry 注册并解析技能。

技能按名称唯一;List 返回全部技能的 Definition,供 Provider 暴露给模型 (即 function calling 里的 skills 参数)。

func NewRegistry

func NewRegistry() *Registry

NewRegistry 创建空注册表。

func NewRegistryWith

func NewRegistryWith(skills ...Skill) *Registry

NewRegistryWith 创建注册表并注册给定技能。

func (*Registry) Get

func (r *Registry) Get(name string) (Skill, bool)

Get 按名称解析技能。

func (*Registry) Has

func (r *Registry) Has(name string) bool

Has 报告指定技能是否已注册。

func (*Registry) Instructions

func (r *Registry) Instructions() []Manifest

Instructions 返回全部技能的指令正文(按名称升序),用于把技能说明批量注入到 系统提示词 / 上下文,让模型在没有 function calling 的 Provider 上也能感知技能。

func (*Registry) List

func (r *Registry) List() []Definition

List 返回全部技能定义(按名称升序),供暴露给模型。

func (*Registry) Manifests

func (r *Registry) Manifests() []Manifest

Manifests 返回全部技能的完整 Manifest(含版本号与指令正文),按名称升序。

与 Instructions 不同,这里会尽量回填 Version 字段,适合用于观测 / 查看技能详情。

func (*Registry) Names

func (r *Registry) Names() []string

Names 返回全部技能名(升序),便于调试 / 校验。

func (*Registry) Register

func (r *Registry) Register(s Skill)

Register 注册技能;同名覆盖。nil 或空名技能会被忽略。

type Result

type Result struct {
	Content string `json:"content"`        // 回传给模型的文本
	Data    any    `json:"data,omitempty"` // 结构化结果(供上层业务使用)
	IsError bool   `json:"is_error"`       // 是否为失败结果
	Err     error  `json:"-"`              // 底层错误(仅供日志 / 观测,不回传模型)
}

Result 是一次技能调用的结果。

无论成功还是失败,Content 都会回传给模型(因此始终是可读文本); IsError 标识这次调用是否失败,模型可据此纠正后续行为。

func Failure

func Failure(err error) Result

Failure 构造失败结果(IsError=true)。

func Success

func Success(data any) Result

Success 构造成功结果,Content 由 data 序列化而来。

type Skill

type Skill interface {
	// Definition 返回技能的元信息(名称 / 描述 / 入参 schema)。
	Definition() Definition
	// Instructions 返回注入到模型上下文的 markdown 指令正文;可为空。
	Instructions() string
	// Invoke 执行技能;args 是模型生成的参数(JSON,空时视为 {})。
	Invoke(ctx context.Context, args json.RawMessage) (Result, error)
}

Skill 是可被 Agent 启用并调用的一个技能。

实现约定:

  • Invoke 返回的 error 会被上层包装为"技能执行失败"的结果(Result.IsError=true), 而不会中断整个调用流程——模型可以看到错误信息并尝试纠正。
  • 返回的 any 会被序列化为结果内容(Result.Content)回传给模型。
  • 纯指令型技能可以只提供 Instructions,Invoke 直接返回指令正文即可。

type Static

type Static struct {
	// contains filtered or unexported fields
}

Static 是一个纯指令型技能:只有指令正文,无独立执行逻辑。

Invoke 直接返回指令正文作为结果,便于把技能作为"可调用的提示词片段"使用。

func NewStatic

func NewStatic(manifest Manifest) *Static

NewStatic 创建一个纯指令型技能。

func (*Static) Definition

func (s *Static) Definition() Definition

Definition 实现 Skill。

func (*Static) Instructions

func (s *Static) Instructions() string

Instructions 实现 Skill。

func (*Static) Invoke

func (s *Static) Invoke(_ context.Context, _ json.RawMessage) (Result, error)

Invoke 实现 Skill:直接返回指令正文。

func (*Static) Version

func (s *Static) Version() string

Version 返回技能版本号(可为空)。

Directories

Path Synopsis
Package builtin 提供框架内置的技能(如 echo)。
Package builtin 提供框架内置的技能(如 echo)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL