common

package
v0.0.0-...-694302a Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const MaxRetriableAttempts = 3

MaxRetriableAttempts is the number of times a provider client retries a retriable LLM error before giving up.

View Source
const TokenEstimateFactor = 0.6

TokenEstimateFactor is the empirical factor for estimating tokens from rune count

Variables

This section is empty.

Functions

func ApplyTokenFallback

func ApplyTokenFallback(promptTokens, completionTokens int64, accumulatedContent string, requestMessages []types.Message, promptOverhead int64) (promptResult, completionResult, totalResult int64)

ApplyTokenFallback fills in token counts using FuzzyTokens if API didn't return them. API returned values take precedence, only applies fallback if missing. promptTokens: tokens from API response (0 if not returned) completionTokens: tokens from API response (0 if not returned) accumulatedContent: the accumulated content for completion token estimation requestMessages: the request messages for prompt token estimation promptOverhead: estimated tokens for system prompt and tool definitions

func ExtractJSON

func ExtractJSON(jsonContent string, model any) error

func FormatToolUseArgumentsError

func FormatToolUseArgumentsError(toolName, raw string) string

FormatToolUseArgumentsError builds a human-readable error for invalid tool arguments.

func IsRetriableError

func IsRetriableError(err error) bool

IsRetriableError reports whether err is worth retrying: rate limiting and transient server-side failures. It prefers typed SDK errors (exchanging the HTTP status code) and only falls back to tightened message matching for untyped errors, so digits that merely appear in request IDs or model names do not trigger a retry.

func IsRetriableStatus

func IsRetriableStatus(statusCode int) bool

IsRetriableStatus reports whether an HTTP status code is retriable: 429 (rate limit) and 500-529 (server errors, incl. 529 overloaded).

func NormalizeToolUseArguments

func NormalizeToolUseArguments(raw, toolName string) (string, string, bool)

NormalizeToolUseArguments replaces non-object args with "{}" and returns an error string. Returns (normalized, errorMessage, ok). When ok is true, the input was a valid object.

func ParseToolUseArguments

func ParseToolUseArguments(raw string) (map[string]any, bool)

ParseToolUseArguments strictly parses raw as a JSON object. Returns nil, false if raw is empty, invalid JSON, or not an object.

func ReadAllContent

func ReadAllContent(ctx context.Context, resp providers.Response) (string, error)

func RepairToolHistory

func RepairToolHistory(history []types.Message) []types.Message

RepairToolHistory performs only unambiguous, provider-agnostic cleanup. Provider-specific tool-call ordering rules must be enforced by each client.

func RetryBackoffDelay

func RetryBackoffDelay(attempt int) time.Duration

RetryBackoffDelay returns the backoff delay to wait before retry attempt (1-based), growing linearly: 10s, 20s, 30s...

func StructuredPredictWithFallback

func StructuredPredictWithFallback(
	ctx context.Context,
	req providers.Request,
	model any,
	nonStreaming func(context.Context, providers.Request) (string, error),
	streaming func(context.Context, providers.Request) providers.Response,
	log logger.Logger,
) error

func Truncate

func Truncate(s string, max int) string

Truncate shortens s to at most max bytes for logging, appending an ellipsis when truncation occurred. It complements FormatToolUseArgumentsError, which embeds the raw arguments in the error message.

func WaitBackoff

func WaitBackoff(ctx context.Context, delay time.Duration) error

WaitBackoff sleeps for the provided duration unless the context is canceled first.

Types

This section is empty.

Jump to

Keyboard shortcuts

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