utils

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const ThinkingSep = "\x02"

ThinkingSep 是思考文本与正文之间的分隔标记。 StreamFilter 在思考文本段前插入此标记,TUI 据此切换渲染样式。

Variables

This section is empty.

Functions

func CleanInputLine added in v0.3.0

func CleanInputLine(s string) string

CleanInputLine 清洗单行人工输入,并去掉首尾空白。

func CleanInputRunes added in v0.3.0

func CleanInputRunes(runes []rune) string

func CleanInputText added in v0.3.0

func CleanInputText(s string) string

CleanInputText 删除终端输入中没有业务意义的控制字符,保留用户可见文本。 单行输入场景下,粘贴文本里的换行和制表符会被归一为空格。

func ContainsControl added in v0.3.0

func ContainsControl(s string) bool

func DecodeText added in v0.5.0

func DecodeText(data []byte) string

DecodeText 把用户提供的文本文件字节解码为 UTF-8:非法 UTF-8 时按 GB18030 (GBK 超集)转码——网络流传的中文小说 txt 大量为 GBK 编码,直接当 UTF-8 读 全是乱码。非 GBK 的字节序列会被解码器替换为 U+FFFD(本就是乱码,由调用方的 零命中兜底报错引导用户)。最后剥离 UTF-8 BOM(否则行首匹配会带上它)。

Types

type JSONFieldExtractor

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

JSONFieldExtractor 从流式 JSON 碎片中提取指定字段的字符串值。

LLM 流式生成 tool call 时,参数是逐片段到达的(OpenAI/Anthropic) 或一次性到达的(Gemini)。本提取器用状态机逐字符扫描, 检测到目标 key 后提取其字符串值,处理 JSON 转义。

func NewFieldExtractor

func NewFieldExtractor(fieldName string) *JSONFieldExtractor

func (*JSONFieldExtractor) Feed

func (e *JSONFieldExtractor) Feed(delta string) string

Feed 处理一段 delta,返回提取到的文本(可能为空)。

func (*JSONFieldExtractor) Reset

func (e *JSONFieldExtractor) Reset()

Reset 重置状态(新 LLM 消息轮次时调用)。

type StreamFilter

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

StreamFilter 区分 SubAgent 的文本回复和 JSON 工具调用。 文本回复标记为思考内容(前缀 ThinkingSep);JSON 工具调用只提取指定字段。

判断依据:遇到 { 进入 JSON 模式(追踪大括号深度), 深度归零后回到文本模式。

func NewStreamFilter

func NewStreamFilter(fieldName string) *StreamFilter

func (*StreamFilter) Feed

func (f *StreamFilter) Feed(delta string) string

Feed 处理一段 delta,返回可展示文本。 文本回复直接输出;JSON 中的目标字段值被提取输出;其余 JSON 结构丢弃。

func (*StreamFilter) Reset

func (f *StreamFilter) Reset()

Reset 重置状态。

Jump to

Keyboard shortcuts

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