agentruntime

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoPermissionResolver = errors.New("agent: no permission resolver in runtime")

ErrNoPermissionResolver 表示当前 Runtime 没有绑定权限解析器(无法等待人工决策)。

原定义在 standalone.go(已删除),ToolRunner 在 Runtime 缺失时仍需要该错误值。

Functions

func AskUserToolDefinition

func AskUserToolDefinition() tool.Definition

AskUserToolDefinition 返回 ask_user 工具的定义(名称 / 描述 / 入参 schema)。

各 Provider 用它把 ask_user 暴露给模型:模型发起调用时,Provider 解析入参为 AskUserInput,并调用 Runtime.AskUser 阻塞等待用户作答,再把结果回填给模型。

func FactsOf

func FactsOf(rt Runtime) TaskFacts

FactsOf 返回 Runtime 绑定的任务事实快照;Runtime 未实现 TaskAware 时返回零值。

func RenderSystemPromptTemplate

func RenderSystemPromptTemplate(template string, vars map[string]any) (string, error)

RenderSystemPromptTemplate 用 vars 渲染 AgentProfile 的系统提示词模板。

Profile.SystemPrompt 是一段 pongo2 模板(例如 "workspace_directory: {{ workspace_directory }}"), 其中可引用由 Handler 依据业务 env(type,id) 解析出的变量。未定义的变量按 pongo2 默认行为 渲染为空串(配合 {% if %} 可让整行按需出现),因此调用方无需保证变量齐全。

template 为空时返回空串;模板语法错误或渲染失败时返回错误。

渲染实现位于 tool.RenderTemplate(统一模板语法);技能工具(get_skill / load_skill_resource)也复用它,以避免 skill → agent 的循环引用。

func ToolRuntimeContextFromRequest

func ToolRuntimeContextFromRequest(req Request) *runtimecontext.RuntimeContext

ToolRuntimeContextFromRequest 取出一次调用的运行时上下文,用于注入工具执行上下文。

运行时上下文由 HTTP Handler 的 RuntimeContextResolver 解析 env(type,id) 产出并放入 Request.RuntimeContext;Provider 在每次调用时把它注入工具执行 ctx,供 builtin 工具 (如 run_analysis_node)在执行期读取 SystemPromptVars。

返回 nil 表示本轮没有运行时上下文(普通调用 / 无业务 env)。

func WithToolFacts

func WithToolFacts(ctx context.Context, rt Runtime) context.Context

WithToolFacts 把 Runtime 绑定的执行事实注入工具 ctx。

工具通过 tool.UserIDFrom / SessionIDFrom / ProjectIDFrom 读取,因此这些事实 只需要在 Provider 组装工具 ctx 时注入一次,不必逐层传参。

func WithToolPermission

func WithToolPermission(ctx context.Context, rt Runtime) context.Context

WithToolPermission 把工具执行期的授权请求桥接到 Agent Runtime:

tool.PermissionRequest → agent.Operation → Runtime.RequestPermission

工具在真正操作文件 / 执行命令前自行调用 tool.RequirePermission,Provider 只需 在每次执行工具前注入本函数。rt 为 nil 时原样返回 ctx(工具侧视为免授权)。

func WithToolRuntimeContext

func WithToolRuntimeContext(ctx context.Context, rc *runtimecontext.RuntimeContext) context.Context

WithToolRuntimeContext 把运行时上下文注入工具执行 ctx;rc 为 nil 时原样返回 ctx。

Types

type ToolCallProvider

type ToolCallProvider func(ctx context.Context, results []tool.Result) ([]ToolCall, error)

ToolCallProvider 由 Provider 提供:根据上一轮结果决定下一批待执行的工具调用。 返回空切片表示不再有工具调用,循环结束。

type ToolLoop

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

ToolLoop 驱动标准的 tool-call 循环:执行 → 回传结果 → 再执行,直至无更多工具调用。

Provider 侧通常这样配合:

resp := model(messages, tools)
for resp.hasToolCalls() {
    results := loop.Run(ctx, func(ctx, last) ([]agent.ToolCall, error) {
        messages = appendToolResults(messages, last) // 把结果回填
        resp = model(messages, tools)
        return resp.toolCalls(), nil
    })
}

func NewToolLoop

func NewToolLoop(runner *ToolRunner) *ToolLoop

NewToolLoop 创建工具调用循环。

func (*ToolLoop) Run

func (l *ToolLoop) Run(ctx context.Context, next ToolCallProvider) ([]tool.Result, error)

Run 驱动循环并返回累积的全部工具结果。

type ToolPermissionResolver

type ToolPermissionResolver func(ctx context.Context, call ToolCall) (Operation, bool)

ToolPermissionResolver 把一次工具调用映射为需要授权的 Operation。

返回 (op, true) 表示该工具需要权限确认(执行前阻塞等待决策); 返回 (op, false) 表示直接放行。

type ToolResultEvent

type ToolResultEvent struct {
	CallID  string `json:"call_id"`
	Name    string `json:"name"`
	Content string `json:"content"`
	IsError bool   `json:"is_error"`
}

ToolResultEvent 是 tool_result 事件的结构化载荷。

type ToolRunner

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

ToolRunner 把 tool.Executor 与 Runtime 桥接:执行工具调用并输出事件, 同时承载可选的权限门禁(把工具调用映射为 Operation,执行前请求授权)。

func NewToolRunner

func NewToolRunner(exec *tool.Executor, rt Runtime) *ToolRunner

NewToolRunner 创建工具执行器;exec 为 nil 时使用空注册表的执行器。

func (*ToolRunner) Run

func (r *ToolRunner) Run(ctx context.Context, call ToolCall) tool.Result

Run 执行一次工具调用:

  1. 输出 tool_call 事件(完整块,落库);
  2. 若配置了权限映射,执行前通过 Runtime.RequestPermission 阻塞等待决策;
  3. 执行工具;
  4. 输出 tool_result 事件并返回结果。

func (*ToolRunner) RunAll

func (r *ToolRunner) RunAll(ctx context.Context, calls []ToolCall) []tool.Result

RunAll 顺序执行一批工具调用。

func (*ToolRunner) SetPermissionResolver

func (r *ToolRunner) SetPermissionResolver(resolver ToolPermissionResolver) *ToolRunner

SetPermissionResolver 设置权限映射(可选),返回自身便于链式调用。

Jump to

Keyboard shortcuts

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