transform

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package transform provides IR-level request transformers (system prompt injection, role mapping, thinking pass-through/strip) that the relay pipeline applies between inbound decode and outbound encode.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyRoleMapping

func ApplyRoleMapping(messages []codec.Message, mapping map[codec.Role]codec.Role)

ApplyRoleMapping applies role mapping to a list of IR messages. Messages are modified in place. Unknown roles are preserved.

func ApplyThinkingPassthrough added in v0.0.8

func ApplyThinkingPassthrough(messages []codec.Message)

ApplyThinkingPassthrough 给"带 tool_calls 但无 thinking block"的 assistant 消息 补一个空文本占位 thinking block。逻辑从 ThinkingPassthroughTransformer 抽出,行为不变。

func ApplyThinkingStrip added in v0.0.8

func ApplyThinkingStrip(messages []codec.Message)

ApplyThinkingStrip 剥离 assistant 消息上的所有 thinking block。 逻辑从 ThinkingStripTransformer 抽出,行为不变。

Types

type RoleMappingConfig

type RoleMappingConfig struct {
	// Default mappings applied to all models in this channel
	Default map[codec.Role]codec.Role `json:"default,omitempty"`
	// Per-model mappings; keys support wildcard patterns (e.g., "claude-*")
	Models map[string]map[codec.Role]codec.Role `json:"models,omitempty"`
}

RoleMappingConfig represents the role mapping configuration for a channel.

func ParseRoleMapping

func ParseRoleMapping(raw string) *RoleMappingConfig

ParseRoleMapping parses a JSON string into a RoleMappingConfig. Returns nil if the input is empty or invalid.

func (*RoleMappingConfig) ResolveRoleMapping

func (c *RoleMappingConfig) ResolveRoleMapping(modelName string) map[codec.Role]codec.Role

ResolveRoleMapping returns the applicable role mapping for a given model name. Priority: exact model match > wildcard match > default. Returns nil if no mapping applies.

type RoleMappingTransformer

type RoleMappingTransformer struct{}

RoleMappingTransformer 按 channel 配置的 RoleMapping 改写 IR 中各消息的 Role。 复用同包内的 ParseRoleMapping / ApplyRoleMapping。模型匹配语义遵循 RoleMappingConfig.ResolveRoleMapping:精确 model > 通配符 > default。

func (RoleMappingTransformer) AppliesTo

AppliesTo 返回 true:所有出站协议都生效。

func (RoleMappingTransformer) Name

func (RoleMappingTransformer) Transform

func (RoleMappingTransformer) Transform(req *codec.Request, cfg *codec.ChannelConfig)

type SystemPromptInjector

type SystemPromptInjector struct{}

SystemPromptInjector 在 channel 配置了 SystemPrompt 时把它注入为 IR 头部的 system 消息(如果已存在 system message,则追加到现有 system 文本,用 "\n" 分隔; 否则 prepend 一个新的 system message)。

注意幂等性:底层 upstream.InjectSystemPrompt 不会去重,反复调用会重复 append。 在生产路径,ApplyIRTransformers 每个请求至多调用一次本 transformer, 所以 spec §4.4 要求的幂等性由调用约定保证。如果将来出现"transformer 链 内自我嵌套调用"的需求,需要重做 InjectSystemPrompt。

func (SystemPromptInjector) AppliesTo

func (SystemPromptInjector) AppliesTo(p codec.Protocol) bool

AppliesTo 返回 true:所有出站协议都需要 system_prompt 注入。

func (SystemPromptInjector) Name

func (SystemPromptInjector) Transform

func (SystemPromptInjector) Transform(req *codec.Request, cfg *codec.ChannelConfig)

type ThinkingPassthroughTransformer

type ThinkingPassthroughTransformer struct{}

ThinkingPassthroughTransformer 在 cfg.SendBackThinking=true 时, 给"带 tool_calls 但没有 ContentTypeThinking blocks 的 assistant 消息" 补一个空文本占位 thinking block。

DeepSeek V4 multi-turn tool calling 要求历史 assistant 消息上有 reasoning_content 字段,缺失会导致下游报错。占位 block 会被 chat_encode 序列化为 "reasoning_content": ""(字段存在但为空)。

仅挂 openai_chat 出站;其它协议自身已经能处理 thinking。

func (ThinkingPassthroughTransformer) AppliesTo

func (ThinkingPassthroughTransformer) Name

func (ThinkingPassthroughTransformer) Transform

type ThinkingStripTransformer

type ThinkingStripTransformer struct{}

ThinkingStripTransformer 在 cfg.SendBackThinking=false 时,剥离 IR assistant 消息上的所有 ContentTypeThinking blocks。 防止跨协议(如 Claude 入站)的 thinking blocks 漏给真 OpenAI 上游。 仅挂 openai_chat 出站;其它协议自身已经能处理 thinking。

func (ThinkingStripTransformer) AppliesTo

func (ThinkingStripTransformer) Name

func (ThinkingStripTransformer) Transform

Jump to

Keyboard shortcuts

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