common

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 11 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 DefaultRequestTimeout = 120 * time.Second

Variables

This section is empty.

Functions

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 DecodeToolCallArguments

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

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

func HandleErrorResponse

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

HandleErrorResponse reads a non-200 response body and returns an appropriate error.

func InferToolNameFromCallID added in v0.2.8

func InferToolNameFromCallID(toolCallID string) string

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

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 NormalizeBaseURL added in v0.2.8

func NormalizeBaseURL(apiBase, defaultBaseURL string, appendV1Suffix bool) string

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

func NormalizeStoredToolCall(tc protocoltypes.ToolCall) (string, map[string]any, string)

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 ParseDataAudioURL added in v0.2.8

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

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

func ResolveToolResponseName added in v0.2.8

func ResolveToolResponseName(toolCallID string, toolCallNames map[string]string) string

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

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

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

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 WrapHTMLResponseError

func WrapHTMLResponseError(statusCode int, body []byte, contentType, apiBase string) error

WrapHTMLResponseError creates a descriptive error for HTML responses.

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 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.

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