pluginkit

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 5 Imported by: 0

README

pluginkit

启动期插件装配库:插件作者只注册类型,使用方用实例图和 root id 构造根插件;插件依赖既可以引用已有实例,也可以直接内联私有插件。

github.com/lengzhao/pluginkit          插件类型:Register / Lookup / Describe
github.com/lengzhao/pluginkit/config   配置识别:PluginUse / Parse
github.com/lengzhao/pluginkit/build    实例化:Build / BuildInto / Scaffold / GetByID / Collect
github.com/lengzhao/pluginkit/manager  Web 配置管理 UI(可选)

根包不读配置、不构造实例。config 不查注册表、不调用 Newbuild 不解释 agent / workflow / etl 等业务字段。

安装

go get github.com/lengzhao/pluginkit

插件作者

init() 只登记 kind 和构造函数:

func init() {
    pluginkit.Register("openai", New)
}

func New(cfg Config) (*OpenAI, error) {
    return &OpenAI{model: cfg.Model}, nil
}

构造函数只支持:

func New() (T, error)
func New(cfg Config) (T, error)
func New(cfg Config, deps Deps) (T, error)

使用方

默认用 root id 构造一张插件实例图:

workflow, _, err := build.Build[Workflow](ctx, cfg, "workflow")
workflow:
  use: sequential-workflow
  deps:
    steps:
      - use: http-step
      - use: save-step
        deps:
          store:
            use: sqlite-store

如果 workflow.deps.steps 里引用了不实现 Step 的实例,Build[Workflow] 会在 deps 阶段失败。

Agent 也可以建模成 root plugin:

agent:
  use: agent
  deps:
    llm:
      use: openai
      config:
        model: gpt-5.5
    tools:
      - use: read-file
      - use: shell

内联实例未写 id 时按路径生成,例如 workflow.steps[0]。需要复用的实例放到顶层并用 id 引用。插件自己的 config 按 JSON 解到构造函数参数,未知字段会失败。

BuildInto 仍可用于直接填充 target struct,但 workflow、agent、ETL 这类编排应优先建模为 root plugin,把引用和内联子插件放进插件 Deps

按类型收集与后置装配

Build 返回的 Result 保存本次 root 可达且已成功构造的全部实例。运行期入口可以按接口筛选贡献者,再装配到收集器,例如 slash command provider:

app, result, err := build.Build[*App](ctx, graph, "app")
if err != nil {
    return err
}

// 约定式:收集器实现 SetContributions([]T)
if err := build.WireSetter[command.Provider](result); err != nil {
    return err
}

// 自定义装配函数:适合 SetCommands 这类宿主自定义方法名
if err := build.WireContributions(
    result,
    func(collector command.Collector, providers []command.Provider) error {
        return collector.SetCommands(providers)
    },
); err != nil {
    return err
}

Collect[T] 只负责从 Result 筛选实例,不执行装配。CollectInstances[T] 会额外保留实例 idkind,便于诊断或冲突报错。收集范围是当前这次 build result,不是全局 registry,也不会扫描所有已注册 kind。

当多个插件向同一个 registry 贡献能力、又不想在 deps 里形成环时,把收集器建模为独立插件实例,build 完成后再 wire。没有贡献者时不会调用收集器;有贡献者但找不到可装配的收集器时返回 build.ErrNoContributionsCollectornil result 是 no-op。

查看插件配置与扩展点

按 kind 查看已注册插件类型的配置字段和依赖扩展点:

desc, ok := pluginkit.Describe("openai")
if !ok {
    // 未注册
}
for _, field := range desc.Config {
    // field.Name 对应 config 里的 key
}
for _, ext := range desc.Extensions {
    // ext.Name 对应 deps 里的 key;ext.List 表示多值;ext.Optional 表示可选
}

init() 注册后、New() 之前,可导出可填写的配置骨架:

desc, ok := pluginkit.Describe("agent")
if !ok {
    // 未注册
}
tmpl := desc.Template()

Template() 返回 map[string]any,格式与 build.Build 接受的 PluginUse 一致。config 为零值占位,必填 depsuse: "" 占位,可选 deps 会省略。需要 YAML 时由使用方自行 yaml.Marshal(tmpl)

use: agent
config:
  model: ""
deps:
  llm:
    use: ""
  tools:
    - use: ""

按接口类型查找可用插件、列出全部 kind:

for _, kind := range pluginkit.ListKinds() {
    // ...
}
for _, kind := range pluginkit.CompatibleKinds(reflect.TypeOf((*agent.Tool)(nil)).Elem()) {
    // ...
}

Web Manager

可嵌入宿主 binary 的可视化配置编辑器(内联 root 实例图):

import "github.com/lengzhao/pluginkit/manager"

func main() {
    // 先 import 自己的插件包,触发 init Register
    manager.Run(manager.Options{Addr: ":8080"})
}

Demo:

go run ./examples/manager
# 打开 http://localhost:8080

详见 docs/2026-08-21-web-manager.md

示例

cd examples/agent && go run .
cd examples/agent && go run . config.flat.yaml

cd examples/workflow && go run .
cd examples/workflow && go run . config.flat.yaml

cd examples/commands && go run .
cd examples/commands && go run . compact

go run ./examples/manager

Documentation

Overview

Package pluginkit 只登记插件类型:kind 与构造函数。

init() 里应只调用 Register,不读取配置、不构造实例。 按 kind 查看配置字段和依赖扩展点见 Describe;PluginDescription.Template 可导出可填写配置骨架。 配置识别见 github.com/lengzhao/pluginkit/config, 启动期实例化见 github.com/lengzhao/pluginkit/build

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompatibleKinds

func CompatibleKinds(want reflect.Type) []string

CompatibleKinds 返回构造返回值静态上可满足 want 的插件 kind。 精确匹配扩展点类型的 kind 排在前面,其次是具体类型实现,最后是接口返回的宽松匹配。

func FormatType

func FormatType(t reflect.Type) string

FormatType 把 reflect.Type 格式化为可读字符串,供 UI 展示。

func ListKinds

func ListKinds() []string

ListKinds 返回已注册插件 kind,按字典序排序。

func Register

func Register(kind string, constructor any)

Register 登记插件类型。kind 必须非空且不重复;constructor 必须是 New() (T, error)、New(cfg) (T, error) 或 New(cfg, deps) (T, error)。

非法登记会 panic,便于在 init() 中尽早失败。

Types

type FieldDescription

type FieldDescription struct {
	Name     string
	GoName   string
	Type     reflect.Type
	List     bool
	Optional bool
}

FieldDescription 描述 struct 顶层字段的配置名、Go 名、类型和单值/多值/可选规则。

type KindCandidate

type KindCandidate struct {
	Kind       string
	ReturnType string
	Exact      bool
}

KindCandidate 是空槽可插入的插件候选。

func KindCandidates

func KindCandidates(want reflect.Type) []KindCandidate

KindCandidates 返回可满足 want 的插件候选,含返回类型与是否精确匹配。

type PluginDescription

type PluginDescription struct {
	Kind       string
	Config     []FieldDescription
	Extensions []FieldDescription
	ReturnType reflect.Type
}

PluginDescription 描述已注册插件类型的配置字段和依赖扩展点字段。

func Describe

func Describe(kind string) (PluginDescription, bool)

Describe 按 kind 返回已注册插件类型的元信息。 未注册时返回 false,不报错。

func (PluginDescription) Template

func (d PluginDescription) Template() map[string]any

Template 返回可填入配置的骨架,格式与 build 接受的 PluginUse 一致。 init() 注册后即可调用,不构造实例。config 字段为零值占位;deps 使用 use: "" 占位; 可选 deps 会省略。

type Spec

type Spec struct {
	Kind       string
	ConfigType reflect.Type // 无 Config 参数时为 nil
	DepsType   reflect.Type // 无 Deps 参数时为 nil
	ReturnType reflect.Type
	// contains filtered or unexported fields
}

Spec 描述已注册插件类型的构造函数形态。

func Lookup

func Lookup(kind string) (Spec, bool)

Lookup 按 kind 查找已注册的构造函数形态。

func (Spec) Constructor

func (s Spec) Constructor() any

Constructor 返回注册时的构造函数。

Directories

Path Synopsis
Package build 在启动期按配置构造插件实例图。
Package build 在启动期按配置构造插件实例图。
Package config 识别 plugins 配置树,得到 PluginUse。
Package config 识别 plugins 配置树,得到 PluginUse。
examples
agent command
commands command
manager command
workflow command
Package manager 提供 pluginkit 插件配置的工作台 Web UI 与 HTTP API。
Package manager 提供 pluginkit 插件配置的工作台 Web UI 与 HTTP API。

Jump to

Keyboard shortcuts

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