Documentation
¶
Overview ¶
Package common provides shared utilities used by multiple LLM provider implementations (openai_compat, azure, etc.).
Index ¶
- Constants
- func AsFloat(v any) (float64, bool)
- func AsInt(v any) (int, bool)
- func DecodeToolCallArguments(raw json.RawMessage, name string) map[string]any
- func HandleErrorResponse(resp *http.Response, apiBase string) error
- func InferToolNameFromCallID(toolCallID string) string
- func LooksLikeHTML(body []byte, contentType string) bool
- func NewHTTPClient(proxy string) *http.Client
- func NormalizeBaseURL(apiBase, defaultBaseURL string, appendV1Suffix bool) string
- func NormalizeStoredToolCall(tc protocoltypes.ToolCall) (string, map[string]any, string)
- func NormalizeToolSchemaTransform(raw string) (string, error)
- func ParseDataAudioURL(mediaURL string) (format, data string, ok bool)
- func ResolveToolResponseName(toolCallID string, toolCallNames map[string]string) string
- func ResponsePreview(body []byte, maxLen int) string
- func SanitizeSchemaForGemini(schema map[string]any) map[string]any
- func SanitizeSchemaForGoogle(schema map[string]any) map[string]any
- func SerializeMessages(messages []Message) []any
- func WrapHTMLResponseError(statusCode int, body []byte, contentType, apiBase string) error
- 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" )
const DefaultRequestTimeout = 120 * time.Second
Variables ¶
This section is empty.
Functions ¶
func DecodeToolCallArguments ¶
func DecodeToolCallArguments(raw json.RawMessage, name string) map[string]any
DecodeToolCallArguments decodes a tool call's arguments from raw JSON.
func HandleErrorResponse ¶
HandleErrorResponse reads a non-200 response body and returns an appropriate error.
func InferToolNameFromCallID ¶ added in v0.2.8
InferToolNameFromCallID extracts a tool name from a call ID in the format "call_<name>_<suffix>". Returns the original ID if it doesn't match.
func LooksLikeHTML ¶
LooksLikeHTML checks if the response body appears to be HTML.
func NewHTTPClient ¶
NewHTTPClient creates an *http.Client with an optional proxy and the default timeout.
func NormalizeBaseURL ¶ added in v0.2.8
NormalizeBaseURL ensures the Anthropic base URL is properly formatted. It removes a trailing /v1 suffix if present (to avoid duplication), then re-appends /v1 when appendV1Suffix is true. An empty apiBase falls back to defaultBaseURL.
func NormalizeStoredToolCall ¶ added in v0.2.8
NormalizeStoredToolCall extracts the tool name, arguments, and thought signature from a stored ToolCall. It handles both the top-level fields and the nested Function struct used by different API formats.
func NormalizeToolSchemaTransform ¶ added in v0.2.9
NormalizeToolSchemaTransform resolves user-facing aliases to a canonical transform mode. Empty values and explicit "off"-style values disable schema transformation.
func ParseDataAudioURL ¶ added in v0.2.8
ParseDataAudioURL extracts the format and base64 data from a data:audio/... URL.
func ResolveToolResponseName ¶ added in v0.2.8
ResolveToolResponseName returns the tool name for a given tool call ID. It first checks the provided name map, then falls back to inferring the name from the call ID format.
func ResponsePreview ¶
ResponsePreview returns a truncated preview of response body for error messages.
func SanitizeSchemaForGemini ¶ added in v0.2.9
SanitizeSchemaForGemini is kept as a compatibility alias for the original Google/Gemini sanitizer name.
func SanitizeSchemaForGoogle ¶ added in v0.2.9
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
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.
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 ¶ added in v0.2.9
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.