common

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 14 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"
)

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: a JSON object, or a JSON string holding one. Arguments that do not decode come back as {"raw": <their text>}.

func InvalidToolArgumentsError

func InvalidToolArgumentsError(tool string) error

InvalidToolArgumentsError is the failure of an answer that calls tool with arguments that are not a JSON object: the call is not run. Another target may answer properly.

func LooksLikeHTML

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

LooksLikeHTML checks if the response body appears to be HTML.

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 SerializeMessagesWithSystemParts

func SerializeMessagesWithSystemParts(messages []Message) []any

SerializeMessagesWithSystemParts is SerializeMessages for an upstream that caches prompt prefixes by content part, as Anthropic does, reached through translation: a system message whose SystemParts render its content and mark a cache breakpoint is sent as those parts, each with its cache_control. A system message whose parts no longer render its content, as after a hook rewrote it, is sent as its content.

Translation joins the text parts of one cached block with "\n" and starts the next block after a breakpoint fresh, so each part after the first carries the rest of the separator: the system text the model gets is the message's content either way.

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