common

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package common provides shared utilities used by multiple LLM provider implementations (openai_compat, azure, etc.).

Index

Constants

View Source
const (
	ToolSchemaTransformOff    = ""
	ToolSchemaTransformSimple = "simple"
)
View Source
const DefaultRequestTimeout = 120 * time.Second

Variables

This section is empty.

Functions

func AnswerError

func AnswerError(message string) error

AnswerError is the error of an upstream that answered with an error instead of a completion. Another instance may still serve the request.

func AnswerErrorMessage

func AnswerErrorMessage(raw json.RawMessage) string

AnswerErrorMessage returns the message of an error an upstream reported in a successful answer or a stream chunk: {"error": "..."} or {"error": {"message": "..."}}. It is "" when there is none.

func AsFloat

func AsFloat(v any) (float64, bool)

AsFloat converts various numeric types to float64.

func AsInt

func AsInt(v any) (int, bool)

AsInt converts various numeric types to int.

func ContentText

func ContentText(raw json.RawMessage) string

ContentText returns the text of a message's content: a string, null, or a list of parts whose text parts it joins.

func DecodeToolCallArguments

func DecodeToolCallArguments(raw json.RawMessage, name string) map[string]any

DecodeToolCallArguments decodes a tool call's arguments from raw JSON.

func LooksLikeHTML

func LooksLikeHTML(body []byte, contentType string) bool

LooksLikeHTML checks if the response body appears to be HTML.

func NewHTTPClient

func NewHTTPClient(proxy string) *http.Client

NewHTTPClient creates an *http.Client with an optional proxy and the default timeout.

func NormalizeFinishReason

func NormalizeFinishReason(reason string, hasToolCalls bool) string

NormalizeFinishReason returns the finish reason Compa records for an answer. "length" reads "truncated". An answer that carries tool calls finished to call them, whatever the upstream said - some finish such an answer with "stop", or with nothing - unless it was cut off. An empty reason is "stop".

func NormalizeToolSchemaTransform

func NormalizeToolSchemaTransform(raw string) (string, error)

NormalizeToolSchemaTransform resolves user-facing aliases to a canonical transform mode. Empty values and explicit "off"-style values disable schema transformation.

func ParseDataAudioURL

func ParseDataAudioURL(mediaURL string) (format, data string, ok bool)

ParseDataAudioURL extracts the format and base64 data from a data:audio/... URL.

func ResponsePreview

func ResponsePreview(body []byte, maxLen int) string

ResponsePreview returns a truncated preview of response body for error messages.

func SanitizeSchemaForGoogle

func SanitizeSchemaForGoogle(schema map[string]any) map[string]any

SanitizeSchemaForGoogle reduces a JSON Schema to the conservative subset accepted by Google/Gemini-style function declarations. It resolves local refs, collapses composition keywords like anyOf/oneOf/allOf, and strips advanced keywords that Gemini-compatible backends often reject.

func SerializeMessages

func SerializeMessages(messages []Message) []any

SerializeMessages converts internal Message structs to the OpenAI wire format.

  • Strips SystemParts (unknown to third-party endpoints)
  • Converts messages with Media to multipart content format (text + image_url parts)
  • Preserves ToolCallID, ToolCalls, and ReasoningContent for all messages

func ToolCallID

func ToolCallID(id string, index int) string

ToolCallID returns the id of the tool call at index of an answer: its own, or, for an upstream that sends none, a new one, so its result can name the call it answers.

Types

type ExtraContent

type ExtraContent = protocoltypes.ExtraContent

Re-export protocol types used across providers.

type FunctionCall

type FunctionCall = protocoltypes.FunctionCall

Re-export protocol types used across providers.

type GoogleExtra

type GoogleExtra = protocoltypes.GoogleExtra

Re-export protocol types used across providers.

type LLMResponse

type LLMResponse = protocoltypes.LLMResponse

Re-export protocol types used across providers.

func ParseResponse

func ParseResponse(body io.Reader) (*LLMResponse, error)

ParseResponse parses a JSON chat completion response body into an LLMResponse.

Every choice counts: some upstreams answer one message split across choices, its text in one and its tool calls in another. The first non-empty text of each kind is the answer's, and the tool calls of every choice are. Tool calls are the answer's whatever finish_reason says: an answer that carries them finished for them (see NormalizeFinishReason).

func ReadAndParseResponse

func ReadAndParseResponse(resp *http.Response, apiBase string) (*LLMResponse, error)

ReadAndParseResponse peeks at the response body to detect HTML errors, then parses the JSON response into an LLMResponse.

type Message

type Message = protocoltypes.Message

Re-export protocol types used across providers.

type ReasoningDetail

type ReasoningDetail = protocoltypes.ReasoningDetail

Re-export protocol types used across providers.

type ToolCall

type ToolCall = protocoltypes.ToolCall

Re-export protocol types used across providers.

type ToolDefinition

type ToolDefinition = protocoltypes.ToolDefinition

Re-export protocol types used across providers.

func TransformToolDefinitions

func TransformToolDefinitions(tools []ToolDefinition, transform string) ([]ToolDefinition, error)

TransformToolDefinitions clones tool definitions and applies the configured schema transform to function parameter schemas. When the transform is off, the original slice is returned unchanged.

type ToolFunctionDefinition

type ToolFunctionDefinition = protocoltypes.ToolFunctionDefinition

Re-export protocol types used across providers.

type UsageInfo

type UsageInfo = protocoltypes.UsageInfo

Re-export protocol types used across providers.

Jump to

Keyboard shortcuts

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