agent_context

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetModelProviderName

func GetModelProviderName(protocol llmkit.Protocol) string

GetModelProviderName 按协议返回遥测服务商名称。

func GetModelServerAddress

func GetModelServerAddress(target *llmkit.Target) (string, int)

GetModelServerAddress 解析模型服务的主机名和端口。

func GetThresholdTokens

func GetThresholdTokens(window, maxOutputTokens int) int

GetThresholdTokens 计算上下文窗口的自动压缩阈值。

func GetTokenModality

func GetTokenModality(req *llmkit.Request) string

GetTokenModality 返回请求在遥测里的 token 模态。

func GetUsageStreamOptions

func GetUsageStreamOptions(options map[string]any, protocol llmkit.Protocol) map[string]any

GetUsageStreamOptions 按协议返回要求上报用量的流式选项。

func IsOutputChunk

func IsOutputChunk(event *llmkit.Event) bool

IsOutputChunk 这个流事件是否属于输出分块?

func IsSentToModel

func IsSentToModel(item *defs.Item) bool

IsSentToModel 这条 Item 是否会进入模型请求?

func NewTelemetryChatInput

func NewTelemetryChatInput(req *llmkit.Request) (systemInstructions []string, messages []telemetry.ChatMessage, toolNames []string)

NewTelemetryChatInput 将模型请求转换为遥测指令、消息和工具名列表。

func NewTelemetryChatMessages

func NewTelemetryChatMessages(items []*defs.Item) []telemetry.ChatMessage

NewTelemetryChatMessages 将 Turn 输入转换为遥测消息。

func NewTelemetryChatOutput

func NewTelemetryChatOutput(items []*defs.Item) []telemetry.ChatMessage

NewTelemetryChatOutput 将模型输出转换为遥测助手消息。

func NewTelemetryFinishReason

func NewTelemetryFinishReason(reason string) string

NewTelemetryFinishReason 将模型结束原因转换为遥测取值。

func NewTokenUsage

func NewTokenUsage(usage *llmkit.Usage) defs.TokenUsage

NewTokenUsage 将 llmkit 用量转换为输入、输出、缓存和推理用量。

Types

type AgentContext

type AgentContext interface {
	// Load 检查并替换全部指令和历史;检查失败时保留原内容。
	Load(param *ContextLoadParam) error
	// AppendItems 检查并追加一批 Item,遇到摘要时裁剪已覆盖的历史;失败时保留原内容。
	AppendItems(items ...*defs.Item) error
	// BuildModelRequest 将指令、历史和工具组装成模型请求,补齐缺失的工具结果。
	// 模型不能看图时图片换成占位文字;能看图时只发送最近 20 张,更早的换成占位文字。
	BuildModelRequest(tools []llmkit.Tool) (request *llmkit.Request, inputThroughItemID defs.ItemID, err error)
	// GetMeasurement 返回本地估算、利用最近实际用量计算的预计占用和 Item 分项。
	GetMeasurement(tools []llmkit.Tool) (*ContextMeasurement, error)
	// RecordRequestUsage 替换最近完整普通调用的报告和实际思考模式。
	RecordRequestUsage(contextUsage *defs.TurnContextUsage)
	// GetCompactionSource 返回待压缩历史的副本,包含已有摘要;没有可压缩内容时返回 nil。
	// 自动压缩要求窗口已知且用量达到阈值;手动压缩不检查阈值。两者都按窗口的 20% 保留最近历史,
	// 保留范围覆盖全部原文时,自动压缩返回 nil,手动压缩返回全部历史。
	GetCompactionSource(measurement *ContextMeasurement, isManual bool) []*defs.Item
	// BuildCompactionRequest 为给定模型组装禁用工具的摘要请求;新压缩传 nil 使用配置的摘要模型。
	BuildCompactionRequest(tools []llmkit.Tool, source []*defs.Item, compactModel *defs.ModelDefinition) (request *llmkit.Request, model *defs.ModelDefinition, client llmkit.Client, err error)
	// IsCompacted 当前历史是否以摘要开头?
	IsCompacted() bool
	// IsAgentsInstructionLoaded 当前历史里是否已有这个 Executor 下这个路径的目录规则?
	IsAgentsInstructionLoaded(executorID, path string) bool
	// IsMemoryIndexLoaded 当前历史里是否已有这个路径、这个项目的记忆目录?
	IsMemoryIndexLoaded(path, project string) bool
}

AgentContext 保存一次 Turn 的模型指令和历史,由 Agent 顺序读写;传入和返回的 Item 都使用副本。

func NewAgentContext

func NewAgentContext(opt *AgentContextOpt) (AgentContext, error)

NewAgentContext 检查配置并创建空上下文。

type AgentContextOpt

type AgentContextOpt struct {
	// SessionID 标识本轮 Session,也用作 prompt_cache_key。
	SessionID defs.SessionID
	// TurnID 是摘要 Item 所属的 Turn。
	TurnID defs.TurnID
	// Client 发起摘要请求。
	Client llmkit.Client
	// Model 提供普通请求参数和窗口;自动压缩预留最大输出,窗口为 0 时不自动压缩。
	Model *defs.ModelDefinition
	// ReasoningEffort 是普通请求的推理强度,可以为空。
	ReasoningEffort string
	// CompactModel 是新压缩的模型及其默认推理强度;不准备新压缩时可为空。
	CompactModel *defs.ModelDefinition
}

AgentContextOpt 指定本轮身份、模型客户端和普通模型、摘要模型的配置。

func NewAgentContextOpt

func NewAgentContextOpt(sessionID defs.SessionID, turnID defs.TurnID, client llmkit.Client, model, compactModel *defs.ModelDefinition) *AgentContextOpt

NewAgentContextOpt 填写本轮身份和两个模型的配置。

type CompactionCall

type CompactionCall struct {
	ModelCallID defs.ModelCallID
	Usage       *defs.TokenUsage
}

CompactionCall 保存一次发起过的摘要模型调用的 ID 和用量,成功、失败时都会返回;未上报用量时 Usage 为 nil。

func GenerateSummary added in v0.5.0

func GenerateSummary(
	ctx context.Context,
	modelCallID defs.ModelCallID,
	client llmkit.Client,
	model *defs.ModelDefinition,
	request *llmkit.Request,
	isCompacted bool,
) (text string, call *CompactionCall, err error)

GenerateSummary 调用摘要模型并记录遥测,返回摘要文本和调用记录;未上报用量时用量为 nil。

type ContextCategory

type ContextCategory string

ContextCategory 区分模型可见 Item 的业务类别。

const (
	ContextCategoryInstruction     ContextCategory = "instruction"    // Skill、目录规则、记忆目录和 hook 输出
	ContextCategorySummary         ContextCategory = "summary"        // 上下文摘要
	ContextCategoryUserInput       ContextCategory = "user_input"     // 用户输入
	ContextCategoryAgentMessage    ContextCategory = "agent_message"  // 其他 Session 发来的消息,包括子 Agent 的结果通知
	ContextCategoryAssistantText   ContextCategory = "assistant_text" // 本 Session 模型输出的文本
	ContextCategoryReasoning       ContextCategory = "reasoning"
	ContextCategoryToolCall        ContextCategory = "tool_call"
	ContextCategoryToolResult      ContextCategory = "tool_result"
	ContextCategoryModelError      ContextCategory = "model_error"
	ContextCategoryModelChange     ContextCategory = "model_change"     // 模型切换提示
	ContextCategoryExecutorsChange ContextCategory = "executors_change" // Executor 列表变化提示
	ContextCategoryTurnInterrupted ContextCategory = "turn_interrupted" // 上一轮被中断提示
)

func GetContextCategory

func GetContextCategory(item *defs.Item) ContextCategory

GetContextCategory 返回 Item 的上下文类别。

type ContextLoadParam

type ContextLoadParam struct {
	// System 是系统指令。
	System string
	// Items 是有序历史:最新摘要加上没被它覆盖的原文。
	Items []*defs.Item
	// ContextUsage 是最近完整普通调用的报告;为 nil 时仅本地计数。
	ContextUsage *defs.TurnContextUsage
}

ContextLoadParam 是 Load 整体替换用的指令和历史;字段为空就清空对应内容。

type ContextMeasurement

type ContextMeasurement struct {
	// Model 是本次测量使用的模型,不依赖最近调用详情是否已加载。
	Model defs.ModelRef `json:"model"`
	// Window 是模型上下文窗口;0 表示窗口未知,此时不触发自动压缩。
	Window int `json:"window"`
	// ThresholdTokens 是预留最大输出后的自动压缩阈值;窗口未知时为 0。
	ThresholdTokens int `json:"threshold_tokens"`
	// RequestTokens 是利用最近实际用量和内容变化计算的预计占用;无适用报告时等于 EstimatedTokens。
	RequestTokens int `json:"request_tokens"`
	// EstimatedTokens 是未经校正的本地估算,等于 SystemTokens、ToolTokens 和 Items 各项 Tokens 之和。
	EstimatedTokens int `json:"estimated_tokens"`
	// IsUsageBased 本次预计占用是否使用了仍适用的模型上报用量?
	IsUsageBased bool `json:"is_usage_based"`
	// SystemTokens 是系统指令的估算用量。
	SystemTokens int `json:"system_tokens"`
	// ToolTokens 是工具定义的估算用量。
	ToolTokens int `json:"tool_tokens"`
	// Items 只含会发给模型的 Item,按上下文顺序排列。
	Items []ItemTokens `json:"items"`
}

ContextMeasurement 保存指令、工具和历史的估算用量,供压缩判断和用量展示读取。

type ItemTokens

type ItemTokens struct {
	ItemID   defs.ItemID     `json:"item_id"`
	TurnID   defs.TurnID     `json:"turn_id"`
	Category ContextCategory `json:"category"`
	Tokens   int             `json:"tokens"`
}

ItemTokens 保存一条模型可见 Item 的身份、类别和估算用量。

Jump to

Keyboard shortcuts

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