Help us shape the future of Go! Take the 2026 Go Developer Survey

toolprior

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package toolprior 工具优先级决策层:给 ReAct agent 的工具表加上 提示词指引(软)、结构顺序(隐式)、调用限制(硬)三层约束。

问题背景:扁平工具表里所有工具平等竞争模型注意力——模型可能跳过 核心证据工具(get_file)直接刷外部 MCP 工具,且外部调用无止损。

三层机制:

  1. StrategyPrompt()——把"何时用哪个、成本多高"渲染成提示词段,注入 instruction
  2. Ordered()——工具表按优先级稳定排序(模型对表顺序有注意力偏好)
  3. LimitCalls()——调用次数硬上限,超限返回固定提示文本软止损(模型可见, 防 runaway 循环)

Index

Constants

View Source
const (
	// PriorityCore 核心证据工具:判定必需,必须最先使用(get_file 等)。
	PriorityCore = 0
	// PrioritySupport 支撑工具:核心证据不足时补充(knowledge/skill)。
	PrioritySupport = 1
	// PriorityExternal 外部工具:有网络/进程开销,本地证据穷尽后再用(MCP)。
	PriorityExternal = 2
)

优先级档位(数值越小越优先;自定义档位可插在中间)。

Variables

This section is empty.

Functions

func LimitCalls

func LimitCalls(t tool.InvokableTool, max int) tool.BaseTool

LimitCalls 包装工具:调用超过 max 次后返回固定提示文本(nil error)软止损。 必须返回文本而非 Go error——eino ToolsNode 会把工具 error 直接上抛中止整个 agent 运行,模型永远看不到提示、此前轮次的部分结论全部丢弃;返回文本则模型 可见,可基于已有信息收尾(软止损 + 保留部分进展)。 max<=0 = 不限制(原样返回)。包装每次新建——计数不跨任务共享。 入参需 InvokableTool(嵌入 BaseTool,Info 由内层透出)。

Types

type Entry

type Entry struct {
	Tool tool.BaseTool
	// Priority 优先级(越小越先;同值保持注册序)。
	Priority int
	// When 何时使用(决策指引,注入提示词;空 = 只排序不渲染指引)。
	When string
	// Cost 成本提示(low/medium/high;空 = 不标注)。
	Cost string
}

Entry 一条工具注册项。

type Table

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

Table 工具优先级表。构建期写入(Add)、构建后只读(Ordered/StrategyPrompt)—— 非并发安全,调用方需保证"先构建后共享"。

func NewTable

func NewTable() *Table

NewTable 创建空表。

func (*Table) Add

func (t *Table) Add(e Entry) *Table

Add 注册一条(链式)。Tool 为 nil 时 panic——注册期配置错误应尽早暴露, 运行期才炸的 nil Tool 难排查。

func (*Table) Len

func (t *Table) Len() int

Len 当前条目数。

func (*Table) Ordered

func (t *Table) Ordered(ctx context.Context) []tool.BaseTool

Ordered 按优先级稳定排序返回工具表(同优先级保持注册序)。 Info 失败的条目在本优先级档内排后(跨档仍按优先级——StrategyPrompt 与 Ordered 共用同一视图)(排序键不可得时不遮挡可用工具)。

func (*Table) StrategyPrompt

func (t *Table) StrategyPrompt(ctx context.Context) string

StrategyPrompt 渲染"工具使用策略"提示词段(按优先级序,与 Ordered 同一视图)。 空表返回空串。调用方拼进 agent instruction。

Jump to

Keyboard shortcuts

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