dataflow

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package dataflow 把 channel 内部、llmkit 编码前的公共 IR 请求处理 表达成一串独立的 Step,由 ChannelDataFlow 统一运行与描述。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneRequest

func CloneRequest(r *llmkit.Request) *llmkit.Request

CloneRequest 深拷贝一个 IR Request,用于冻结 Pass.Original。 用 JSON round-trip 实现深拷贝:IR 完全可 JSON 序列化,代价是一次 marshal, 对请求路径可忽略。Original 仅供读取稳定输入值(当前只读 Model), round-trip 对这些字段无损。

注意:JSON round-trip 会把 any-typed 字段(如 ResponseFormat、Tool.InputSchema / Tool.RawConfig)退化成 map[string]any / []any,丢失原始具体类型。今天没问题—— Original 只读且只消费 .Model;但若将来有 Step 要读 Original.Tools 之类的字段, 应改用真正的深拷贝 / 重新求值,别依赖这里的 round-trip。

另注:marshal 失败时(不应发生)兜底走浅拷贝,而浅拷贝与入参共享 slice/map 引用, 因此这条非预期路径上 Original 不再与 Working 完全独立。

Types

type ChannelDataFlow

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

ChannelDataFlow 是某条 channel 内部、按序排好的一串工序。 relay 路径用 Run 跑请求;前端链路图用 Describe 报出每道工序说明。

func BuildChannelRequestFlow added in v0.0.18

func BuildChannelRequestFlow(ch *models.Channel, protocol llmkit.Protocol, deps StepDeps) *ChannelDataFlow

BuildChannelRequestFlow 装配 llmkit.Client 编码前运行的公共 IR 步骤。

func (*ChannelDataFlow) Describe

func (f *ChannelDataFlow) Describe() []StepInfo

Describe 报出每道工序的展示信息(顺序与执行顺序一致)。

func (*ChannelDataFlow) Run

func (f *ChannelDataFlow) Run(ctx context.Context, p *Pass) error

Run 按序应用所有 Step。任一 Step 返回 error 立即中止并包裹返回; 任一 Step 置 p.Aborted(如脚本 reject,响应已写回)也立即停止后续 Step。

type Pass

type Pass struct {
	Original *llmkit.Request // 冻结只读快照
	Working  *llmkit.Request // 被各 Step 加工的工作副本

	Rec *trace.Recorder // 透传给 Step 打 stage / WithFail;可能为 nil

	// Aborted 表示某 Step 已写回响应并要求终止(如脚本 reject)。
	// native 据此直接返回 AbortResult,不再 dispatch。
	Aborted     bool
	AbortResult state.AttemptResult
}

Pass 是一次请求在 channel 内部流动时携带的数据。

各 Step 只作用在 Working(IR)上。Original 是进入本 channel 处理那一刻的 冻结快照,只读—— 需要"原始输入值"(如请求模型 Original.Model)的 Step 读它;默认语义是读 Working。

type Step

type Step interface {
	// Key 是稳定标识(如 "model_mapping"),供默认顺序列表引用、供前端图使用。
	Key() string
	// Apply 在 Pass 上加工。返回 error 表示该工序失败(对齐现状:多数工序遇非法配置
	// noop 并打 warn 返回 nil;只有 encode / script 这类会真正失败)。
	Apply(ctx context.Context, p *Pass) error
	// Describe 自我描述,纯供前端画图使用,不参与运行逻辑。
	Describe() StepInfo
}

Step 是 channel 内部的一道处理工序。

type StepDeps

type StepDeps struct {
	Agent   app.AgentApplication // StepUpstreamScript / StepInlineImages 用
	GinCtx  *gin.Context         // StepUpstreamScript 用(写回 reject 响应)
	RCtx    *state.RelayContext  // StepUpstreamScript 用(取 UserInfo / Header)
	Attempt state.Attempt        // StepUpstreamScript 用(取完整 channel 来源和真实模型)
	Logger  *zap.Logger          // StepInlineImages 用;可能为 nil
}

StepDeps 是装配 Step 时需要的运行期依赖(脚本引擎、日志等)。 StepUpstreamScript 要求 Agent / GinCtx / RCtx 非 nil;StepInlineImages 在 Apply 时 也要求 Agent 非 nil(经 Agent.GetCache().Settings() 读抓取参数);其余 Step 不读它们。 注:仅 Describe() 的装配路径(master dataflow.go)可传 Agent==nil——它不跑 Apply。

type StepInfo

type StepInfo struct {
	Key       string // 同 Key()
	Title     string // 一句人话标题(中文权威标签;前端按 key 走 i18n,Title 作兜底)
	ConfigRef string // 该工序配置所在页/锚点,前端点击跳转用
	Detail    string // 本工序配置摘要,语言中立数据(计数/字段名/协议 key);裁剪掉的工序为空
}

StepInfo 是一道工序的展示信息。不含任何模型名专属字段——模型名只是 Pass 里 一个普通字段,"读 Original 还是 Working"是所有字段共用的通用机制。

func AllStepInfos

func AllStepInfos() []StepInfo

AllStepInfos 按 defaultStepOrder 返回全部工序的静态 StepInfo(与配置无关)。 给前端画"全 11 道含跳过灰节点"的链路图用。

func DescribeChannelDataFlow added in v0.0.18

func DescribeChannelDataFlow(ch *models.Channel, protocol llmkit.Protocol) []StepInfo

DescribeChannelDataFlow 保留管理 API 原 11 步的静态展示能力,但不把 encode 及其后的 wire 动作重新伪装成运行时 dataflow Step。

type StepInjectSystemPrompt

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

StepInjectSystemPrompt 把 channel 配置的 SystemPrompt 注入 Working 消息头部。 复用 upstream.InjectSystemPrompt(空 prompt 自动 noop)。

func (*StepInjectSystemPrompt) Apply

func (*StepInjectSystemPrompt) Describe

func (s *StepInjectSystemPrompt) Describe() StepInfo

func (*StepInjectSystemPrompt) Key

func (s *StepInjectSystemPrompt) Key() string

type StepInlineImages added in v0.0.13

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

StepInlineImages 在出站编码前把消息里 MediaURL 形式的图片下载内联成 base64。 仅当渠道 InlineImageURL 开时装配。抓取失败降级:保留原 URL + warn,Apply 从不返回 error。

func (*StepInlineImages) Apply added in v0.0.13

func (s *StepInlineImages) Apply(ctx context.Context, p *Pass) error

func (*StepInlineImages) Describe added in v0.0.13

func (s *StepInlineImages) Describe() StepInfo

func (*StepInlineImages) Key added in v0.0.13

func (s *StepInlineImages) Key() string

type StepModelMapping

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

StepModelMapping 把 Working.Model 经 channel 的 model_mapping 映射成上游 model。 对应原 state.ApplyModelMapping。mapping 在装配时解析好;空表 / 未命中 → 不变。

func (*StepModelMapping) Apply

func (s *StepModelMapping) Apply(_ context.Context, p *Pass) error

func (*StepModelMapping) Describe

func (s *StepModelMapping) Describe() StepInfo

func (*StepModelMapping) Key

func (s *StepModelMapping) Key() string

type StepRoleMapping

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

StepRoleMapping 按 channel 的 role_mapping 改写 Working 消息的 Role。 按 Original.Model(请求模型,= 今天 cfg.InboundModel)匹配规则,不按映射后的模型。

func (*StepRoleMapping) Apply

func (s *StepRoleMapping) Apply(_ context.Context, p *Pass) error

func (*StepRoleMapping) Describe

func (s *StepRoleMapping) Describe() StepInfo

func (*StepRoleMapping) Key

func (s *StepRoleMapping) Key() string

type StepThinkingPassthrough

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

StepThinkingPassthrough 在 SendBack(Working.Model)=true 时补占位 thinking block。 仅 openai_chat 出站时进链(由装配器决定)。

func (*StepThinkingPassthrough) Apply

func (*StepThinkingPassthrough) Describe

func (s *StepThinkingPassthrough) Describe() StepInfo

func (*StepThinkingPassthrough) Key

type StepThinkingStrip

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

StepThinkingStrip 在 SendBack(Working.Model)=false 时剥离 thinking block。 仅 openai_chat 出站时进链。

func (*StepThinkingStrip) Apply

func (s *StepThinkingStrip) Apply(_ context.Context, p *Pass) error

func (*StepThinkingStrip) Describe

func (s *StepThinkingStrip) Describe() StepInfo

func (*StepThinkingStrip) Key

func (s *StepThinkingStrip) Key() string

Jump to

Keyboard shortcuts

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