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

wrap

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

输出截断续写(对标 ZCode turn-output-token-continuation):finish reason 为 length/max_tokens 且消息无工具调用时,保留部分输出并以「直接续写」指令再调, 内容顺序拼接,最多 maxContinuations 轮。被截断的评审结论不再整体作废重跑—— 续写成本远低于整轮重跑,且拼接补全 JSON 恰好是 jsonrepair 的前置救济。

纪律:

  • 带 ToolCalls 的截断消息不续写(ReAct 中间步的续写会污染工具调用状态);
  • 续写失败返回已得部分(部分结果优于整体失败);
  • Stream 直通不续写(当前无流式消费方,装饰器如实声明)。

Package wrap eino BaseChatModel 装饰器集:窗口拟合(FitModel)、输出截断续写 (ContinueModel)、限速(RateModel)。三者职责正交、可任意组合(推荐外层顺序 Rate→Fit→Continue:限速最内层保物理调用同桶,续写最外层对最终输出负责), WithTools 均做绑定透传再包装——ReAct 绑定工具后装饰不失效。

与 llm.Client 的分工:Client.fitInput 服务 Generator 链(单级截最长 user, 内嵌拟合);本包装饰器服务直调旁路(ReAct/RawModel 等裸 BaseChatModel), 三级递进且带真实 usage 校准。同进程两路并存无副作用(拟合只紧不松、幂等)。

收编自 argus internal/context(v0.10.32):ctxfit/ctxcontinue/ctxrate 三装饰器 在 PR 审查Bot 生产验证 ~1 个月(v3.36.0–v3.75),归位为 SDK 通用能力;观测 依赖已回调化(OnFit/OnContinue/OnBreakdown 均可 nil)。

上下文窗口治理(对标 ZCode compact/microcompact + context-usage-breakdown): BaseChatModel 装饰器,发送前输入自守恒,三级递进——

L1 microcompact:更早的工具结果内容置换占位(保最近 keepRecentTools 条不动;
  只替换内容、不动消息条数与 tool_call/tool_result 配对,语法安全);
  清理量不足 minClearSavings 时放弃(不值得损失信息);
L2 截最长 user 消息(与 llm.Client.fitInput 同策略,TruncNote 留痕);
L3 病理态兜底:迭代截最大工具结果直至入窗。

触发线与估算口径对齐 llm fitInput:rune 计数,limit=(窗口-输出预留)×3/2×0.9(CJK 感知)。 真实 usage 校准(对标 ZCode tokenSource=provider_usage)——**实例内响应 直采**(v0.12.0 起单源):Generate/Stream 的响应 usage 与本次实发 rune 数 在同一次调用内配对,写入 FitModel 实例校准槽;tokens/char 系数系统性偏低 的模型(中文为主)自动收紧有效预算。无全局注册表、无外部回灌 API—— 响应就在手里,配对天然同源同调用同模型(第八轮审计定案)。 观测:每次拟合结果经 OnFit 上报(passthrough 应占绝对多数),上下文构成 分解经 OnBreakdown 按 role 分桶上报,接入方自行接 Prometheus/日志。

限速装饰器:BaseChatModel 直调旁路的速率治理。直调旁路(ReAct/RawModel) 绕过 Generator 链(llm.Client/Resilient)的限速是历史缺口;RateModel 挂进 包装链后,直调出口每次物理调用先经全局 token bucket 等待——与 Generator 链同桶,真全局。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRateModel

func NewRateModel(inner einomodel.BaseChatModel, limiter *rate.Limiter) einomodel.BaseChatModel

NewRateModel 包装 inner;limiter 为 nil 时返回原对象(零开销直通)。

Types

type ContinueModel

type ContinueModel struct {
	einomodel.BaseChatModel
	Stage      string
	MaxRounds  int // ≤0 = maxContinuations
	OnContinue func(stage string, round, addedRunes int)
}

ContinueModel 输出截断续写装饰器;无可变状态,可跨任务共享。

func NewContinueModel

func NewContinueModel(inner einomodel.BaseChatModel, stage string, onContinue func(string, int, int)) *ContinueModel

NewContinueModel 包装 inner;轮数上限 ≤0 时取缺省 3。

func (*ContinueModel) Generate

func (m *ContinueModel) Generate(ctx context.Context, input []*schema.Message, opts ...einomodel.Option) (*schema.Message, error)

func (*ContinueModel) WithTools

WithTools 工具绑定透传:绑定产物再包一层 ContinueModel(与 FitModel 同纪律)。

type FitAction

type FitAction string

FitAction 拟合动作(观测标签;passthrough 应占绝对多数)。

const (
	FitPass       FitAction = "passthrough"  // 未触发
	FitMicro      FitAction = "microcompact" // 仅 L1
	FitTruncated  FitAction = "truncated"    // L2/L3(无 L1 或 L1 不足)
	FitMicroTrunc FitAction = "micro+trunc"  // L1+L2/L3
)

type FitModel

type FitModel struct {
	einomodel.BaseChatModel
	Stage     string
	ModelName string

	WindowTokens int // 模型上下文窗口(token);≤0 = 直通
	MaxOutput    int // 输出预留(token);≤0 = 窗口/20
	// OnFit 拟合事件回调(nil 安全;仅非 passthrough 触发)。
	OnFit func(stage string, res FitResult)
	// OnBreakdown 上下文构成分解回调(nil 安全):每次发送按 role 分桶累计
	// 本次输入量(rune)——回答「token 花在哪」。接入方自行接指标系统。
	OnBreakdown func(stage string, byRole map[string]int)
	// contains filtered or unexported fields
}

FitModel 输入自守恒装饰器:Generate/Stream 前做两级拟合,窗口 ≤0 时直通。 无可变状态(校准在包级注册表),可跨任务共享。

func NewFitModel

func NewFitModel(inner einomodel.BaseChatModel, stage, modelName string, windowTokens, maxOutputTokens int, onFit func(string, FitResult)) *FitModel

NewFitModel 包装 inner;WindowTokens ≤0 时保持直通(不改行为)。

func (*FitModel) Generate

func (f *FitModel) Generate(ctx context.Context, input []*schema.Message, opts ...einomodel.Option) (*schema.Message, error)

func (*FitModel) Stream

func (f *FitModel) Stream(ctx context.Context, input []*schema.Message, opts ...einomodel.Option) (*schema.StreamReader[*schema.Message], error)

func (*FitModel) WithTools

func (f *FitModel) WithTools(tools []*schema.ToolInfo) (einomodel.ToolCallingChatModel, error)

WithTools 工具绑定透传:绑定产物再包一层 FitModel——ReAct 绑定工具后拟合不失效。

type FitResult

type FitResult struct {
	Action  FitAction
	Before  int // 拟合前总 rune
	After   int
	Limit   int // 生效字符预算
	Cleared int // L1 清理的工具结果条数
}

FitResult 拟合结果(rune 口径)。

func FitMessages

func FitMessages(msgs []*schema.Message, limitChars int) ([]*schema.Message, FitResult)

FitMessages 两级拟合纯内核。limitChars ≤0 或未超限时原样返回(同一切片, 不复制——零开销直通路径)。

type RateModel

type RateModel struct {
	einomodel.BaseChatModel
	Limiter *rate.Limiter
}

RateModel 限速装饰器;Limiter nil = 直通。

func (*RateModel) Generate

func (m *RateModel) Generate(ctx context.Context, input []*schema.Message, opts ...einomodel.Option) (*schema.Message, error)

func (*RateModel) Stream

func (m *RateModel) Stream(ctx context.Context, input []*schema.Message, opts ...einomodel.Option) (*schema.StreamReader[*schema.Message], error)

func (*RateModel) WithTools

func (m *RateModel) WithTools(tools []*schema.ToolInfo) (einomodel.ToolCallingChatModel, error)

WithTools 工具绑定透传:绑定产物保持限速(与 Fit/Continue 同纪律)。

Jump to

Keyboard shortcuts

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