streamguard

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: None detected not legal advice Imports: 0 Imported by: 0

Documentation

Overview

SSE 流状态守卫 -- 边界情况检测 + 空闲看门狗 + 停顿诊断.

精妙之处(CLEVER): parseAnthropicSSE 是裸解析器(收到什么推什么),StreamGuard 叠加 状态追踪和边界防御.两层职责清晰--解析器负责"解析正确", StreamGuard 负责"检测异常".

覆盖的生产边界情况:

  1. 空响应:200 OK 但无 SSE 事件(代理故障,返回 HTML/空体)
  2. 部分流:有内容事件但未收到 UsageEvent(网络中断)
  3. 空闲挂起:流中间长时间无数据(代理/防火墙静默断开)
  4. 停顿:两次事件间隔过长(网络抖动诊断)
  5. Scanner 错误:行过长或 I/O 错误

升华改进(ELEVATED): 从 StreamEvent(Anthropic 专有中间类型)改为 flyto.Event-- StreamGuard 现在对所有 provider 通用,不绑定 Anthropic 语义. 替代方案:<每个 provider 实现自己的流守卫> - 否决:重复代码, 且 Gemini / OpenAI 的可靠性检测逻辑与 Anthropic 完全相同.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StreamGuard

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

StreamGuard 包装 flyto.Event channel,添加边界检测和空闲看门狗.

func NewStreamGuard

func NewStreamGuard(cfg *StreamGuardConfig) *StreamGuard

NewStreamGuard 创建流守卫.

func (*StreamGuard) Stats

func (g *StreamGuard) Stats() *StreamStats

Stats 返回流统计信息.

func (*StreamGuard) Watch

func (g *StreamGuard) Watch(ctx context.Context, rawCh <-chan flyto.Event) <-chan flyto.Event

Watch 包装原始 flyto.Event channel,返回加守卫的新 channel.

type StreamGuardConfig

type StreamGuardConfig struct {
	// IdleTimeout 空闲超时:流中间多久无事件视为挂起(默认 90s).
	// 精妙之处(CLEVER): SDK 的 request timeout 只管初始连接,
	// 不管流中间断了.没有看门狗,静默断开的连接会让进程永远挂起.
	IdleTimeout time.Duration

	// IdleWarningAt 空闲警告时间点(默认 IdleTimeout/2).
	IdleWarningAt time.Duration

	// StallThreshold 停顿阈值(默认 30s).
	StallThreshold time.Duration

	// OnIdleWarning 空闲警告回调(可选)
	OnIdleWarning func(elapsed time.Duration)

	// OnIdleTimeout 空闲超时回调(可选)
	OnIdleTimeout func()

	// OnStall 停顿检测回调(可选)
	OnStall func(gap time.Duration, stallCount int, totalStallTime time.Duration)

	// OnStreamEnd 流结束回调(可选)
	OnStreamEnd func(stats *StreamStats)
}

StreamGuardConfig 配置流守卫的行为.

func DefaultStreamGuardConfig

func DefaultStreamGuardConfig() *StreamGuardConfig

DefaultStreamGuardConfig 返回默认配置.

type StreamStats

type StreamStats struct {
	// HasContent 是否收到过内容事件(TextDelta / ToolUse / ThinkingDelta)
	HasContent bool
	// HasUsage 是否收到 UsageEvent(流正常结束的标志)
	HasUsage bool
	// StopReason 最终的 stop_reason(来自 UsageEvent)
	StopReason string
	// ContentBlockCount 完成的内容块数量(Text + ToolUse)
	ContentBlockCount int
	// EventCount 总事件数量
	EventCount int
	// StallCount 停顿次数
	StallCount int
	// TotalStallTime 累计停顿时间
	TotalStallTime time.Duration
	// Duration 流的总持续时间
	Duration time.Duration
	// IdleAborted 是否因空闲超时被中止
	IdleAborted bool
}

StreamStats 是流结束时的统计信息.

func (*StreamStats) IsEmpty

func (s *StreamStats) IsEmpty() bool

IsEmpty 检查流是否为空响应(200 但无实质事件).

func (*StreamStats) IsIncomplete

func (s *StreamStats) IsIncomplete() bool

IsIncomplete 检查流是否不完整(有内容但未正常结束).

Jump to

Keyboard shortcuts

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