sdkutil

package
v0.22.3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlushInterval  = 32 * time.Millisecond
	FlushChunkSize = 1024
)

Variables

View Source
var EmptyJSONArgs = map[string]any{
	"type":                 "object",
	"properties":           map[string]any{},
	"additionalProperties": false,
}

Functions

func BuildToolChoiceNameMapping

func BuildToolChoiceNameMapping(
	tools []spec.ToolChoice,
) (toolNames []toolName, toolNameMap map[string]spec.ToolChoice)

BuildToolChoiceNameMapping assigns short, human‑readable function names to tools.

Rules:

  • base name is the sanitized tool slug (lower‑cased, [a‑z0‑9_-] only)
  • first tool with a given slug gets "<slug>"
  • subsequent tools with the same slug get "<slug>_2", "<slug>_3", ...
  • names are truncated to 64 characters (OpenAI function-tool limit)

Returns:

  • ordered: same cardinality/order as input tools, but with the derived function name for each tool.
  • nameToTool: map[functionName] => FetchCompletionToolChoice; used to translate tool calls back to the original identity.

func ClampIntToInt32 added in v0.13.0

func ClampIntToInt32(v int) int32

func CloneBoolPtr added in v0.20.0

func CloneBoolPtr(p *bool) *bool

func CloneFetchCompletionRequest added in v0.11.0

func CloneFetchCompletionRequest(req *spec.FetchCompletionRequest) (*spec.FetchCompletionRequest, error)

func CloneFloat64Ptr added in v0.20.0

func CloneFloat64Ptr(in *float64) *float64

func CloneIntPtr added in v0.20.0

func CloneIntPtr(p *int) *int

func CloneStringMap added in v0.6.1

func CloneStringMap(in map[string]string) map[string]string

func CloneStringPtr added in v0.20.0

func CloneStringPtr(in *string) *string

func FilterMessagesByTokenCount

func FilterMessagesByTokenCount(
	messages []spec.InputUnion,
	maxTokenCount int,
) []spec.InputUnion

func IsInputUnionEmpty

func IsInputUnionEmpty(in spec.InputUnion) bool

IsInputUnionEmpty reports whether an InputUnion has nothing "worth sending". Metadata-only messages (e.g. only IDs/roles with no actual content) are treated as empty. Adjust the helpers below if your semantics differ.

func NewBufferedStreamer

func NewBufferedStreamer(
	onDataFlush func(string) error,
	flushInterval time.Duration,
	maxSize int,
) (write func(string) error, flush func())

NewBufferedStreamer returns two functions:

  • write(chunk) -> use this instead of onDataFlush
  • flush() -> call once when streaming is finished

func NormalizeRequestForSDK added in v0.9.1

func NormalizeRequestForSDK(
	ctx context.Context,
	req *spec.FetchCompletionRequest,
	opts *spec.FetchCompletionOptions,
	sdkType spec.ProviderSDKType,
	providerCapabilities spec.ModelCapabilities,
) (cappedReq *spec.FetchCompletionRequest, effectiveCapabilities *spec.ModelCapabilities, warnings []spec.Warning, err error)

func Recover

func Recover(msg string, fields ...any)

Recover logs a panic (if any) at error level and prevents it from bringing down the goroutine's caller. It does not modify any returned error.

func SafeCallStreamHandler

func SafeCallStreamHandler(handler spec.StreamHandler, event spec.StreamEvent) (err error)

SafeCallStreamHandler invokes the provided StreamHandler and converts any panic into an error while logging the panic details. This prevents user callbacks from crashing the streaming loop.

func ToolDescription

func ToolDescription(ct spec.ToolChoice) string

Types

type CompletionProvider added in v0.9.1

type CompletionProvider interface {
	InitLLM(ctx context.Context) error
	DeInitLLM(ctx context.Context) error
	GetProviderInfo(ctx context.Context) *spec.ProviderParam
	IsConfigured(ctx context.Context) bool
	SetProviderAPIKey(ctx context.Context, apiKey string) error
	GetProviderCapability(ctx context.Context) (spec.ModelCapabilities, error)

	FetchCompletion(
		ctx context.Context,
		fetchCompletionRequest *spec.FetchCompletionRequest,
		opts *spec.FetchCompletionOptions,
	) (*spec.FetchCompletionResponse, error)
}

type ResolvedAllowedTool added in v0.7.0

type ResolvedAllowedTool struct {
	Type spec.ToolType
	Name string
}

func ResolveAllowedTools added in v0.7.0

func ResolveAllowedTools(
	allowed []spec.AllowedTool,
	toolChoiceNameMap map[string]spec.ToolChoice,
) ([]ResolvedAllowedTool, error)

type ResolvedStreamConfig

type ResolvedStreamConfig struct {
	FlushInterval  time.Duration
	FlushChunkSize int
}

ResolvedStreamConfig is the fully-specified streaming configuration used by providers after applying sensible defaults.

func ResolveStreamConfig

func ResolveStreamConfig(opts *spec.FetchCompletionOptions) ResolvedStreamConfig

ResolveStreamConfig converts optional FetchCompletionOptions into a concrete ResolvedStreamConfig, falling back to library defaults as needed.

Jump to

Keyboard shortcuts

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