tool

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithExecutionMeta

func ContextWithExecutionMeta(ctx context.Context, meta ExecutionMeta) context.Context

func DataSchemaToJSONSchema

func DataSchemaToJSONSchema(ds DataSchema) json.RawMessage

DataSchemaToJSONSchema 把 DataSchema 合成为标准 JSON Schema(object)。 这是从 planner 上移到 tool 包的"权威转换"——所有合成走这一处。 注意:DataSchema 只一层、无嵌套/枚举/约束,故合成结果也是浅层;这是 DataSchema 的固有局限, 原生持有 JSON Schema 的工具(DefinedTool)不受此限。

func EmitComplete

func EmitComplete(e ToolEmitter, msg string, data map[string]any)

func EmitFail

func EmitFail(e ToolEmitter, err error, data map[string]any)

func EmitLog

func EmitLog(e ToolEmitter, msg string, data map[string]any)

func EmitPipelineStage

func EmitPipelineStage(e ToolEmitter, data map[string]any)

EmitPipelineStage sends a generation_pipeline_stage event carrying the full pipeline snapshot. Each event is a self-contained full snapshot — clients replace their local state directly without incremental accumulation. Uses CustomType to bypass the "tool_" prefix, same as EmitTimeline.

func EmitProgress

func EmitProgress(e ToolEmitter, progress float64, data map[string]any)

func EmitStart

func EmitStart(e ToolEmitter, msg string, data map[string]any)

func EmitStream

func EmitStream(e ToolEmitter, message string, progress float64, data map[string]any)

func EmitTimeline

func EmitTimeline(e ToolEmitter, eventType string, data map[string]any)

EmitTimeline sends a timeline-specific event (scene_created, ai_shot_started, etc.) through the ToolEmitter. The eventType is forwarded as-is through CustomType, bypassing the "tool_" prefix in TaskEvent.Type.

func EmitWarning

func EmitWarning(e ToolEmitter, msg string, data map[string]any)

Types

type Annotations

type Annotations struct {
	Execution   ExecutionMode `json:"execution,omitempty"`    // Flux 扩展:sync/async
	ReadOnly    bool          `json:"readOnlyHint,omitempty"` // MCP hint(预留)
	Destructive bool          `json:"destructiveHint,omitempty"`
	Idempotent  bool          `json:"idempotentHint,omitempty"`
	OpenWorld   bool          `json:"openWorldHint,omitempty"`
}

Annotations 携带 MCP 标准提示 + Flux 扩展。本阶段只填 Execution(从 Mode 派生); MCP 的 readOnly/destructive 等提示预留,已知时再填。

type DataSchema

type DataSchema struct {
	Fields map[string]FieldSchema
}

DataSchema 数据契约模型

type DefinedTool

type DefinedTool interface {
	Definition() ToolDefinition
}

DefinedTool 是可选接口:原生就持有 JSON Schema 定义的工具实现它(如 MCP 适配器)。 实现了它的工具,DefinitionOf 会直接采用其原生定义,不走 DataSchema 合成。

type ExecutionMeta

type ExecutionMeta struct {
	UserID   int64
	TaskID   int64
	RootID   int64
	NodeName string
}

func ExecutionMetaFromContext

func ExecutionMetaFromContext(ctx context.Context) (ExecutionMeta, bool)

type ExecutionMode

type ExecutionMode string

ExecutionMode 执行模式 SyncExecution 短任务同步执行 AsyncExecution 长任务异步执行

const (
	SyncExecution  ExecutionMode = "sync"
	AsyncExecution ExecutionMode = "async"
)

type FieldSchema

type FieldSchema struct {
	Type     string // string | number | object | array
	Required bool
	Desc     string
}

type Registry

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

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Get

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

Get 根据工具名称在工具注册中心中查找工具

func (*Registry) List

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

func (*Registry) Register

func (r *Registry) Register(t Tool)

type Result

type Result struct {
	Success bool           `json:"success"`
	Data    map[string]any `json:"data,omitempty"`
	Error   string         `json:"error,omitempty"`
}

func Fail

func Fail(err error) *Result

func Success

func Success(data map[string]any) *Result

type Tool

type Tool interface {
	Name() string
	Description() string
	InputSchema() DataSchema
	OutputSchema() DataSchema
	Execute(ctx context.Context, input map[string]any, emitter ToolEmitter) (*Result, error)
	Mode() ExecutionMode
}

Tool 实际能力,标准化工具

func ResolvePreferredPollTool

func ResolvePreferredPollTool(reg *Registry, requested string) (string, Tool, bool)

type ToolDefinition

type ToolDefinition struct {
	Name         string          `json:"name"`
	Title        string          `json:"title,omitempty"`
	Description  string          `json:"description"`
	InputSchema  json.RawMessage `json:"inputSchema"`            // JSON Schema(MCP: inputSchema)
	OutputSchema json.RawMessage `json:"outputSchema,omitempty"` // JSON Schema(MCP: outputSchema)
	Annotations  Annotations     `json:"annotations,omitempty"`
}

ToolDefinition 是工具的"定义层"——MCP 形状、可序列化、与执行解耦。 它是把工具描述给 LLM / MCP 客户端的统一货币(JSON Schema),取代弱的 DataSchema 旁路。

主线二 阶段 C:之前 MCP 适配器靠临时的 RawInputSchema() 直供原生 schema,planner 内联 断言取用。这里把它"转正"——任何工具都可经 DefinedTool 暴露原生定义,本地工具则由 DefinitionOf 从 DataSchema 合成。本地工具与 MCP 工具的 schema 自此走同一出口。

注意范围:本阶段只统一"定义",不动 tool.Tool 接口签名、不动 Execute、不动 workflow 引擎。

func DefinitionOf

func DefinitionOf(t Tool) ToolDefinition

DefinitionOf 是"给我任意工具的 JSON-Schema 定义"的唯一入口(统一出口):

  • 工具实现了 DefinedTool(如 MCP 适配器)→ 直接用其原生定义;
  • 否则 → 从 Name/Description/InputSchema(DataSchema)/Mode 合成一个等价定义。

planner / 未来的 MCP-expose 都只调它,从而本地工具与 MCP 工具被一视同仁。

type ToolEmitter

type ToolEmitter interface {
	EmitToolEvent(event ToolEvent)
}

ToolEmitter 天然适配流式输出,为实时字幕、前端进度展示预留了扩展点 —— 这是接入 LLM 流式能力的关键。

type ToolEvent

type ToolEvent struct {
	Type       string // started | stream | stream_end | log | completed | failed
	CustomType string // 不为空时直接作为 TaskEvent.Type,绕过 "tool_" 前缀
	Message    string
	Progress   float64
	Data       map[string]any
	LogLevel   string // log level
}

ToolEvent 工具事件,不会直接对外发送,而是被 转换为 TaskEvent

type UsageAware

type UsageAware interface {
	UsageSchema() DataSchema
	BuildUsageFacts(input map[string]any, output map[string]any) ([]map[string]any, error)
}

UsageAware 为有成本语义的工具提供可选 usage facts 能力。 它不会改变 Tool 主接口,只让需要记账的工具显式声明用量结构与产出方式。

type WorkflowAsTool

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

WorkflowAsTool 将一个已有的 Workflow 包装为 tool.Tool,使 Agent 可以像调用普通工具一样 选择已有的预制工作流(如 text_to_image、goods_video_pro_v3)。

预制工作流是经过生产验证的手写 DSL,Agent 可以选择直接使用它们, 而不是每次都从零生成 DAG。

func NewWorkflowAsTool

func NewWorkflowAsTool(name, description string, schema DataSchema, executor func(ctx context.Context, input map[string]any, emitter ToolEmitter) (*Result, error)) *WorkflowAsTool

NewWorkflowAsTool 创建一个预制工作流工具。 executor: 宿主提供的执行函数,通常委托给 v1 engine。

func (*WorkflowAsTool) Description

func (t *WorkflowAsTool) Description() string

func (*WorkflowAsTool) Execute

func (t *WorkflowAsTool) Execute(ctx context.Context, input map[string]any, emitter ToolEmitter) (*Result, error)

func (*WorkflowAsTool) InputSchema

func (t *WorkflowAsTool) InputSchema() DataSchema

func (*WorkflowAsTool) Mode

func (t *WorkflowAsTool) Mode() ExecutionMode

func (*WorkflowAsTool) Name

func (t *WorkflowAsTool) Name() string

func (*WorkflowAsTool) OutputSchema

func (t *WorkflowAsTool) OutputSchema() DataSchema

func (*WorkflowAsTool) SetExecutor

func (t *WorkflowAsTool) SetExecutor(fn func(ctx context.Context, input map[string]any, emitter ToolEmitter) (*Result, error))

SetExecutor 设置工作流的执行函数(用于延迟注入,如 engine 创建后)。

Jump to

Keyboard shortcuts

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