shared

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 4 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

Overview

Package shared provides types and utilities shared across all transformer providers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsRecord

func AsRecord(v any) (map[string]any, bool)

func AsTrimmedString

func AsTrimmedString(v any) string

func BuildSyntheticOpenAiChunks

func BuildSyntheticOpenAiChunks(normalized NormalizedFinalResponse) []map[string]any

func ComputeNovelResponsesDelta

func ComputeNovelResponsesDelta(existing, incoming string) string

func ConsumeThinkTaggedText

func ConsumeThinkTaggedText(state *ThinkTagParserState, chunk string) (content, reasoning string)

func EnsureIntTimestamp

func EnsureIntTimestamp(v any, fallback int64) int64

func ExtractReasoningDetailsText

func ExtractReasoningDetailsText(raw any) string

ExtractReasoningDetailsText pulls MiniMax-style reasoning_details[].text (and similar nested text fields) into a single reasoning string.

func FlushThinkTaggedText

func FlushThinkTaggedText(state *ThinkTagParserState) (content, reasoning string)

func IsNonEmptyString

func IsNonEmptyString(v any) bool

func IsRecord

func IsRecord(v any) bool

func JoinIndexedResponsesText

func JoinIndexedResponsesText(m map[int]string) string

func JoinNonEmpty

func JoinNonEmpty(parts []string) string

func NormalizeStopReason

func NormalizeStopReason(raw string) string

func ParseDownstreamChatRequest

func ParseDownstreamChatRequest(body any, format DownstreamFormat) (*ParsedDownstreamChatRequest, *ErrorPayload)

func ParseJSONLike

func ParseJSONLike(raw string) any

func PickFiniteInt

func PickFiniteInt(v any) int

func PickFiniteNumber

func PickFiniteNumber(v any) float64

func PickPositiveInt

func PickPositiveInt(v any) int

func SafeJSONString

func SafeJSONString(v any) string

func SerializeFinalResponse

func SerializeFinalResponse(format DownstreamFormat, normalized NormalizedFinalResponse, usage struct{ PromptTokens, CompletionTokens, TotalTokens int }) map[string]any

func SerializeSSE

func SerializeSSE(event string, data any) string

func StringifyUnknownValue

func StringifyUnknownValue(v any) string

func ToClaudeStopReason

func ToClaudeStopReason(finishReason string) string

Types

type ClaudeDownstreamContext

type ClaudeDownstreamContext struct {
	MessageStarted        bool
	ContentBlockStarted   bool
	DoneSent              bool
	TextBlockIndex        *int
	NextContentBlockIndex int
	ToolBlocks            map[int]*ClaudeToolBlockState

	// Extended fields
	ThinkingBlockIndex *int
	RedactedBlockIndex *int
	PendingSignature   string
	ActiveToolSlot     *int
}

ClaudeDownstreamContext tracks Anthropic content-block state during downstream serialization.

func CreateClaudeDownstreamContext

func CreateClaudeDownstreamContext() *ClaudeDownstreamContext

type ClaudeToolBlockState

type ClaudeToolBlockState struct {
	ContentIndex int
	ID           string
	Name         string
	Open         bool
}

type DownstreamFormat

type DownstreamFormat string

DownstreamFormat identifies the downstream serialization format.

const (
	FormatOpenAI DownstreamFormat = "openai"
	FormatClaude DownstreamFormat = "claude"
)

type ErrorPayload

type ErrorPayload struct {
	StatusCode int
	Payload    any
}

func (*ErrorPayload) Error

func (e *ErrorPayload) Error() string

type NormalizedFinalResponse

type NormalizedFinalResponse struct {
	ID                       string     `json:"id"`
	Model                    string     `json:"model"`
	Created                  int64      `json:"created"`
	Content                  string     `json:"content"`
	ReasoningContent         string     `json:"reasoningContent"`
	ReasoningSignature       string     `json:"reasoningSignature,omitempty"`
	RedactedReasoningContent string     `json:"redactedReasoningContent,omitempty"`
	FinishReason             string     `json:"finishReason"`
	ToolCalls                []ToolCall `json:"toolCalls"`
}

NormalizedFinalResponse is the universal final response type.

func NormalizeUpstreamFinalResponse

func NormalizeUpstreamFinalResponse(payload any, fallbackModel, fallbackText string) NormalizedFinalResponse

type NormalizedStreamEvent

type NormalizedStreamEvent struct {
	Role                     string          `json:"role,omitempty"`
	ContentDelta             string          `json:"contentDelta,omitempty"`
	ReasoningDelta           string          `json:"reasoningDelta,omitempty"`
	ReasoningSignature       string          `json:"reasoningSignature,omitempty"`
	RedactedReasoningContent string          `json:"redactedReasoningContent,omitempty"`
	ToolCallDeltas           []ToolCallDelta `json:"toolCallDeltas,omitempty"`
	FinishReason             string          `json:"finishReason,omitempty"`
	Done                     bool            `json:"done,omitempty"`
}

NormalizedStreamEvent is the universal stream event type.

func NormalizeUpstreamStreamEvent

func NormalizeUpstreamStreamEvent(payload any, ctx *StreamTransformContext, fallbackModel string) NormalizedStreamEvent

type NormalizedUsage

type NormalizedUsage struct {
	PromptTokens             int `json:"promptTokens"`
	CompletionTokens         int `json:"completionTokens"`
	TotalTokens              int `json:"totalTokens"`
	CachedTokens             int `json:"cachedTokens"`
	CacheReadTokens          int `json:"cacheReadTokens"`
	CacheCreationTokens      int `json:"cacheCreationTokens"`
	ReasoningTokens          int `json:"reasoningTokens"`
	AudioInputTokens         int `json:"audioInputTokens"`
	AudioOutputTokens        int `json:"audioOutputTokens"`
	AcceptedPredictionTokens int `json:"acceptedPredictionTokens"`
	RejectedPredictionTokens int `json:"rejectedPredictionTokens"`
}

NormalizedUsage has 11 counters per spec.

type ParsedDownstreamChatRequest

type ParsedDownstreamChatRequest struct {
	RequestedModel     string
	IsStream           bool
	UpstreamBody       map[string]any
	ClaudeOriginalBody map[string]any
}

type ParsedSseEvent

type ParsedSseEvent struct {
	Event string // event: value (empty for OpenAI)
	Data  string // data: value
}

ParsedSseEvent is a single parsed SSE event (event line + data line).

func PullSseEventsWithDone

func PullSseEventsWithDone(buffer string) (events []ParsedSseEvent, rest string)

type StreamTransformContext

type StreamTransformContext struct {
	ID                                  string
	Model                               string
	Created                             int64
	RoleSent                            bool
	DoneSent                            bool
	ToolCalls                           map[int]*ToolCallAccumulator
	ResponsesToolCallIndexByOutputIndex map[int]int
	ResponsesToolCallIndexByID          map[string]int
	NextResponsesToolCallIndex          int
	ResponsesTextByIndex                map[int]string
	ResponsesReasoningByIndex           map[int]string
	ThinkTagParser                      *ThinkTagParserState
}

func CreateStreamTransformContext

func CreateStreamTransformContext(modelName string) *StreamTransformContext

type TextReasoning

type TextReasoning struct {
	Content   string
	Reasoning string
}

TextReasoning is the return type for text/reasoning split functions.

func ExtractInlineThinkTags

func ExtractInlineThinkTags(text string) TextReasoning

type ThinkTagParserState

type ThinkTagParserState struct {
	Mode    string // "content" | "reasoning"
	Pending string // buffered partial tag match
}

ThinkTagParserState tracks streaming think-tag parse state.

func CreateThinkTagParserState

func CreateThinkTagParserState() *ThinkTagParserState

CreateThinkTagParserState creates a new parser in content mode.

type ToolCall

type ToolCall struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

ToolCall is a complete tool call in a final response.

type ToolCallAccumulator

type ToolCallAccumulator struct {
	ID, Name, Arguments string
}

type ToolCallDelta

type ToolCallDelta struct {
	Index          int    `json:"index"`
	ID             string `json:"id,omitempty"`
	Name           string `json:"name,omitempty"`
	ArgumentsDelta string `json:"argumentsDelta,omitempty"`
}

ToolCallDelta represents an incremental tool call update in a stream.

Jump to

Keyboard shortcuts

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