Documentation
¶
Overview ¶
Package common provides shared utilities used by multiple LLM provider implementations (openai_compat, azure, etc.).
Index ¶
- Constants
- func AnswerError(message string) error
- func AnswerErrorMessage(raw json.RawMessage) string
- func AsFloat(v any) (float64, bool)
- func AsInt(v any) (int, bool)
- func ContentText(raw json.RawMessage) string
- func DecodeToolCallArguments(raw json.RawMessage, name string) map[string]any
- func InvalidToolArgumentsError(tool string) error
- func LooksLikeHTML(body []byte, contentType string) bool
- func NormalizeFinishReason(reason string, hasToolCalls bool) string
- func NormalizeToolSchemaTransform(raw string) (string, error)
- func ParseDataAudioURL(mediaURL string) (format, data string, ok bool)
- func ResponsePreview(body []byte, maxLen int) string
- func SanitizeSchemaForGoogle(schema map[string]any) map[string]any
- func SerializeMessages(messages []Message) []any
- func SerializeMessagesWithSystemParts(messages []Message) []any
- func ToolCallID(id string, index int) string
- type ExtraContent
- type FunctionCall
- type GoogleExtra
- type LLMResponse
- type Message
- type ReasoningDetail
- type ToolCall
- type ToolDefinition
- type ToolFunctionDefinition
- type UsageInfo
Constants ¶
const ( ToolSchemaTransformOff = "" ToolSchemaTransformSimple = "simple" )
Variables ¶
This section is empty.
Functions ¶
func AnswerError ¶
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 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 ¶
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 ¶
LooksLikeHTML checks if the response body appears to be HTML.
func NormalizeFinishReason ¶
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 ¶
NormalizeToolSchemaTransform resolves user-facing aliases to a canonical transform mode. Empty values and explicit "off"-style values disable schema transformation.
func ParseDataAudioURL ¶
ParseDataAudioURL extracts the format and base64 data from a data:audio/... URL.
func ResponsePreview ¶
ResponsePreview returns a truncated preview of response body for error messages.
func SanitizeSchemaForGoogle ¶
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 ¶
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 ¶
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 ¶
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 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.