ir

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 1 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 ir defines the protocol-agnostic intermediate representation used by LLM protocol codecs.

Index

Constants

View Source
const (
	HeaderAuthorization    = "Authorization"
	HeaderContentType      = "Content-Type"
	HeaderConnection       = "Connection"
	HeaderCacheControl     = "Cache-Control"
	HeaderOpenAIOrg        = "OpenAI-Organization"
	HeaderXAPIKey          = "x-api-key"
	HeaderAnthropicVersion = "anthropic-version"

	ContentTypeJSON       = "application/json"
	ContentTypeSSE        = "text/event-stream"
	BearerPrefix          = "Bearer "
	CacheControlNoCache   = "no-cache"
	ConnectionKeepAlive   = "keep-alive"
	AnthropicVersionValue = "2023-06-01"
)
View Source
const (
	RouteChatCompletions = "/v1/chat/completions"
	RouteResponses       = "/v1/responses"
	RouteMessages        = "/v1/messages"
)
View Source
const (
	FinishReasonStop          = "stop"
	FinishReasonLength        = "length"
	FinishReasonToolCalls     = "tool_calls"
	FinishReasonContentFilter = "content_filter"

	ClaudeStopEndTurn   = "end_turn"
	ClaudeStopMaxTokens = "max_tokens"
	ClaudeStopToolUse   = "tool_use"
)
View Source
const (
	MessageStart      = "message_start"
	ContentBlockStart = "content_block_start"
	ContentBlockDelta = "content_block_delta"
	ContentBlockStop  = "content_block_stop"
	MessageDelta      = "message_delta"
	MessageStop       = "message_stop"

	ClaudeTextDelta      = "text_delta"
	ClaudeInputJSONDelta = "input_json_delta"
	ClaudeThinkingDelta  = "thinking_delta"
	ClaudeSignatureDelta = "signature_delta"
)
View Source
const (
	ResponseCreated            = "response.created"
	ResponseInProgress         = "response.in_progress"
	ResponseCompleted          = "response.completed"
	ResponseFailed             = "response.failed"
	ResponseIncomplete         = "response.incomplete"
	OutputItemAdded            = "response.output_item.added"
	OutputItemDone             = "response.output_item.done"
	ContentPartAdded           = "response.content_part.added"
	ContentPartDelta           = "response.content_part.delta"
	ContentPartDone            = "response.content_part.done"
	OutputTextDelta            = "response.output_text.delta"
	OutputTextDone             = "response.output_text.done"
	ReasoningTextDelta         = "response.reasoning_text.delta"
	ReasoningTextDone          = "response.reasoning_text.done"
	FunctionCallArgumentsDelta = "response.function_call_arguments.delta"
	FunctionCallArgumentsDone  = "response.function_call_arguments.done"
	RefusalDelta               = "response.refusal.delta"

	ChatStreamDone = "[DONE]"
)
View Source
const (
	ImageSourceBase64 = "base64"
	ImageSourceURL    = "url"
)

Image source type 常量。

Variables

This section is empty.

Functions

This section is empty.

Types

type ContentBlock

type ContentBlock struct {
	Type         ContentType       `json:"type"`
	Text         string            `json:"text,omitempty"`
	MediaURL     string            `json:"media_url,omitempty"`
	MediaB64     string            `json:"media_b64,omitempty"`
	MimeType     string            `json:"mime_type,omitempty"`
	Metadata     map[string]any    `json:"metadata,omitempty"`
	Reasoning    *ReasoningContent `json:"reasoning,omitempty"`
	ProviderData *ProviderData     `json:"provider_data,omitempty"`

	// RawJSON preserves the original JSON for unknown content block types.
	// When set, the encoder should emit this JSON as-is instead of
	// constructing from typed fields.
	RawJSON json.RawMessage `json:"raw_json,omitempty"`
}

ContentBlock is a single piece of typed content within a message.

type ContentType

type ContentType string

ContentType describes the kind of content carried by a ContentBlock.

const (
	ContentTypeText       ContentType = "text"
	ContentTypeImage      ContentType = "image"
	ContentTypeAudio      ContentType = "audio"
	ContentTypeToolUse    ContentType = "tool_use"
	ContentTypeToolResult ContentType = "tool_result"
	ContentTypeThinking   ContentType = "thinking"
	ContentTypeInputText  ContentType = "input_text"
	ContentTypeOutputText ContentType = "output_text"
	ContentTypeImageURL   ContentType = "image_url"
	ContentTypeFunction   ContentType = "function"
)

type DeltaPayload

type DeltaPayload struct {
	ContentType ContentType    `json:"content_type,omitempty"`
	Text        string         `json:"text,omitempty"`
	Refusal     string         `json:"refusal,omitempty"`
	Signature   string         `json:"signature,omitempty"`
	ToolCall    *ToolCallDelta `json:"tool_call,omitempty"`
}

DeltaPayload carries incremental content for delta events.

type ErrorPayload

type ErrorPayload struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

ErrorPayload describes an error encountered during streaming.

type Event

type Event struct {
	ContentBlock      *ContentBlock      `json:"content_block,omitempty"`
	Type              EventType          `json:"type"`
	Delta             *DeltaPayload      `json:"delta,omitempty"`
	ToolCall          *StreamingToolCall `json:"tool_call,omitempty"`
	Usage             *Usage             `json:"usage,omitempty"`
	Error             *ErrorPayload      `json:"error,omitempty"`
	FinishReason      string             `json:"finish_reason,omitempty"`
	Model             string             `json:"model,omitempty"`   // upstream model name passthrough
	Created           int64              `json:"created,omitempty"` // upstream timestamp passthrough
	Metadata          map[string]any     `json:"metadata,omitempty"`
	RawPassthrough    *RawSSEEvent       `json:"raw_passthrough,omitempty"`
	Extras            map[string]any     `json:"extras,omitempty"` // non-streaming response unknown field passthrough
	ContentBlockIndex *int               `json:"content_block_index,omitempty"`
	StopSequence      string             `json:"stop_sequence,omitempty"`
	Reasoning         *ReasoningContent  `json:"reasoning,omitempty"`
	ReasoningStatus   ReasoningStatus    `json:"reasoning_status,omitempty"`
	ReasoningContext  ReasoningContext   `json:"reasoning_context,omitempty"`
}

Event is a single element in the IR event stream produced when streaming responses from an upstream provider.

type EventType

type EventType int

EventType identifies the kind of streaming event in an IR event stream.

const (
	EventStreamStart            EventType = iota // stream has started
	EventContentDelta                            // incremental text content
	EventToolCallDelta                           // Deprecated: split into EventToolCallStart / EventToolCallArgumentsDelta / EventToolCallEnd. Will be removed once all stream codecs migrate.
	EventThinkingDelta                           // incremental thinking/reasoning content
	EventUsage                                   // token usage report
	EventDone                                    // stream finished normally
	EventError                                   // stream finished with an error
	EventRawPassthrough                          // raw SSE event that could not be parsed into a known IR event
	EventContentBlockStop                        // content block ended
	EventSignatureDelta                          // thinking block signature
	EventToolCallStart                           // streaming: a tool call began (call_id + name)
	EventToolCallArgumentsDelta                  // streaming: incremental arguments fragment
	EventToolCallEnd                             // streaming: tool call ended with full accumulated arguments
	EventReasoningSummaryDelta                   // incremental readable reasoning summary
	EventReasoningContentDelta                   // incremental full reasoning content
	EventReasoningDone                           // reasoning block completed or was interrupted
	EventContentBlock                            // 完整的图片等内容块。
)

type Message

type Message struct {
	Role       Role           `json:"role"`
	Content    []ContentBlock `json:"content"`
	ToolCalls  []ToolCall     `json:"tool_calls,omitempty"`
	ToolCallID string         `json:"tool_call_id,omitempty"`

	// Signature 是上游要求随 assistant 历史原样回传的凭据,与可剥离的思考文本独立。
	Signature string `json:"signature,omitempty"`

	// RawJSON preserves the original JSON for unknown input item types.
	// When set with empty Role, the encoder should emit this JSON as-is.
	RawJSON json.RawMessage `json:"raw_json,omitempty"`
}

Message represents a single conversation turn.

func TextMessage

func TextMessage(role Role, text string) Message

TextMessage is a convenience constructor for a simple text message.

type Protocol

type Protocol string

Protocol identifies a wire protocol supported by the gateway.

const (
	ProtocolOpenAIChat      Protocol = "openai_chat"
	ProtocolOpenAIResponses Protocol = "openai_responses"
	ProtocolClaude          Protocol = "claude"
	ProtocolGemini          Protocol = "gemini"
	ProtocolUnknown         Protocol = "unknown"

	ProtocolClaudeMessages = ProtocolClaude
)

type ProviderData

type ProviderData struct {
	Protocol Protocol                   `json:"protocol"`         // 产生这些字段的协议
	Models   []string                   `json:"models,omitempty"` // 每经过一层追加该层发给上游的模型名,最里层在前;为空表示使用者自带
	Fields   map[string]json.RawMessage `json:"fields"`           // 原生字段原样保存
}

ProviderData 保存厂商附在工具调用或内容块上的签名等原始字段。

type RawSSEEvent

type RawSSEEvent struct {
	EventName string `json:"event_name"`
	Data      string `json:"data"`
}

RawSSEEvent carries a raw SSE event that could not be parsed into a known IR event.

type ReasoningContent

type ReasoningContent struct {
	Summary   []string        `json:"summary,omitempty"`
	Content   []string        `json:"content,omitempty"`
	Encrypted string          `json:"encrypted,omitempty"`
	RawJSON   json.RawMessage `json:"raw_json,omitempty"`
}

ReasoningContent is the protocol-neutral representation of a model's reasoning block. Structured fields are authoritative over RawJSON when a codec re-encodes the value.

type ReasoningContext

type ReasoningContext string

ReasoningContext 是思考上下文模式:在请求中表示调用方指定的模式,在响应事件中表示服务端报告的实际模式。

const (
	ReasoningContextUnknown     ReasoningContext = ""
	ReasoningContextCurrentTurn ReasoningContext = "current_turn"
	ReasoningContextAllTurns    ReasoningContext = "all_turns"
)

type ReasoningStatus

type ReasoningStatus string

ReasoningStatus describes why a reasoning stream stopped.

const (
	ReasoningCompleted   ReasoningStatus = "completed"
	ReasoningInterrupted ReasoningStatus = "interrupted"
)

type Request

type Request struct {
	Model       string         `json:"model"`
	Messages    []Message      `json:"messages"`
	Tools       []Tool         `json:"tools,omitempty"`
	Stream      bool           `json:"stream"`
	MaxTokens   int            `json:"max_tokens,omitempty"`
	Temperature *float64       `json:"temperature,omitempty"`
	TopP        *float64       `json:"top_p,omitempty"`
	StopWords   []string       `json:"stop,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty"`

	// Model behavior fields (commonly used across protocols)
	ToolChoice        *ToolChoice `json:"tool_choice,omitempty"`
	ParallelToolCalls *bool       `json:"parallel_tool_calls,omitempty"`
	ReasoningEffort   string      `json:"reasoning_effort,omitempty"`
	// ReasoningContext 是请求指定的思考上下文模式;为空表示请求不发送,服务端按默认(auto)处理。
	ReasoningContext ReasoningContext `json:"reasoning_context,omitempty"`
	ThinkingEnabled  *bool            `json:"thinking_enabled,omitempty"`
	ThinkingBudget   *int             `json:"thinking_budget,omitempty"`
	IncludeThoughts  *bool            `json:"include_thoughts,omitempty"`
	Store            *bool            `json:"store,omitempty"`

	// Sampling / generation parameters
	FrequencyPenalty *float64       `json:"frequency_penalty,omitempty"`
	PresencePenalty  *float64       `json:"presence_penalty,omitempty"`
	Seed             *int64         `json:"seed,omitempty"`
	N                int            `json:"n,omitempty"`
	TopK             *int           `json:"top_k,omitempty"`
	LogitBias        map[string]int `json:"logit_bias,omitempty"`
	Logprobs         *bool          `json:"logprobs,omitempty"`
	TopLogprobs      *int           `json:"top_logprobs,omitempty"`
	User             string         `json:"user,omitempty"`
	ServiceTier      string         `json:"service_tier,omitempty"`
	InferenceGeo     string         `json:"inference_geo,omitempty"`
	SafetyIdentifier string         `json:"safety_identifier,omitempty"`
	ResponseFormat   any            `json:"response_format,omitempty"`
	StreamOptions    map[string]any `json:"stream_options,omitempty"`

	// Protocol-specific passthrough container
	Extras map[string]any `json:"extras,omitempty"`

	// InboundProtocol 由 InboundCodec.DecodeRequest 在成功解析后填写,标识
	// 该请求的原始入站协议。编码器据此判断"源协议 == 目标协议"做 passthrough。
	// 零值 "" 表示未知,ResolveTool 会退化到跨协议分支。
	InboundProtocol Protocol `json:"inbound_protocol,omitempty"`
}

Request is the protocol-agnostic representation of an AI completion request.

type Role

type Role string

Role represents the role of a message participant.

const (
	RoleSystem    Role = "system"
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
	RoleDeveloper Role = "developer"
)

type StreamingToolCall

type StreamingToolCall struct {
	CallID    string `json:"call_id"`
	Index     int    `json:"index"`
	Name      string `json:"name,omitempty"`
	Namespace string `json:"namespace,omitempty"`
	Arguments string `json:"arguments,omitempty"`

	ProviderData *ToolCallProviderData `json:"provider_data,omitempty"` // Start 带当时已有的,End 带完整的
}

StreamingToolCall carries per-event state for the 3 streaming tool_call events. CallID is required on all 3 events to bind identity. Index mirrors the chat protocol's tool_calls[].index for parallel calls. Name is set on Start. Arguments holds a fragment on ArgumentsDelta and the full accumulated value on End.

type Tool

type Tool struct {
	Name                 string  `json:"name"`
	Namespace            string  `json:"namespace,omitempty"`
	NamespaceGroupID     string  `json:"-"` // original Responses namespace container; preserves same-protocol boundaries
	NamespaceDescription *string `json:"-"` // original Responses namespace description; shared by its expanded functions
	Description          string  `json:"description"`
	InputSchema          any     `json:"input_schema"`
	Type                 string  `json:"type,omitempty"` // "function", "web_search", "custom", etc.
	Strict               *bool   `json:"strict,omitempty"`
	RawConfig            any     `json:"raw_config,omitempty"` // full config for non-function tools
}

Tool describes a tool (function) that the model may invoke.

type ToolCall

type ToolCall struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Namespace string `json:"namespace,omitempty"`
	Arguments string `json:"arguments"`

	ProviderData *ToolCallProviderData `json:"provider_data,omitempty"`
}

ToolCall represents a model-initiated tool invocation.

type ToolCallDelta

type ToolCallDelta struct {
	ID        string `json:"id,omitempty"`
	Name      string `json:"name,omitempty"`
	Namespace string `json:"namespace,omitempty"`
	Arguments string `json:"arguments,omitempty"`

	ProviderData *ToolCallProviderData `json:"provider_data,omitempty"`
}

ToolCallDelta carries incremental data for a streaming tool call.

type ToolCallProviderData

type ToolCallProviderData = ProviderData

ToolCallProviderData 保留工具调用厂商数据的公开类型名称。

type ToolChoice

type ToolChoice struct {
	Type      string `json:"type"`                // "auto", "required", "none", "function", "custom"
	Name      string `json:"name,omitempty"`      // tool name, when Type="function" or "custom"
	Namespace string `json:"namespace,omitempty"` // Responses namespace, when Type="function"
}

ToolChoice defines the canonical IR form for tool selection.

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	CacheReadTokens  int `json:"cache_read_tokens,omitempty"`
	CacheWriteTokens int `json:"cache_write_tokens,omitempty"`
	TotalTokens      int `json:"total_tokens"`

	// Detailed token breakdowns (OpenAI completion_tokens_details / prompt_tokens_details)
	ReasoningTokens          int `json:"reasoning_tokens,omitempty"`
	CachedTokens             int `json:"cached_tokens,omitempty"`
	AcceptedPredictionTokens int `json:"accepted_prediction_tokens,omitempty"`
	RejectedPredictionTokens int `json:"rejected_prediction_tokens,omitempty"`
}

Usage reports token consumption for a request.

Jump to

Keyboard shortcuts

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