sdkutil

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlushInterval  = 32 * time.Millisecond
	FlushChunkSize = 1024
)
View Source
const DataContractHash = "sha256:82b88dbae52bd9596cbb79a09704c0110f6bd7c0ae6088c30f6241be7c116853"

DataContractHash is a SHA-256 of the contents of DataContractFiles. It is validated by tests and can be used by downstream consumers to check that they are running against the contract version they were built for.

Format: "sha256:<hexstring>".

View Source
const DataContractVersion = "v1.0.0"

DataContractVersion is bumped when the *schema* of the contract types changes.

Variables

View Source
var DataContractFiles = []string{
	"../../spec/io_cache_control.go",
	"../../spec/io_citation.go",
	"../../spec/io_tool.go",
	"../../spec/io_union.go",
	"../../spec/output_other.go",
	"../../spec/param_model.go",
	"../../spec/param_output_control.go",
	"../../spec/param_reasoning.go",
}

DataContractFiles lists files that define the data contract. Paths are relative to the repo root.

These files should only contain data-contract types (structs/enums) that downstream consumers rely on structurally. Any change to these files will change the contract hash. It does NOT contain api contracts.

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

Functions

func BuildReasoningContinuationFingerprint added in v0.26.0

func BuildReasoningContinuationFingerprint(
	providerInfo *spec.ProviderParam,
) string

BuildReasoningContinuationFingerprint identifies the configured endpoint. Provider name, SDK type, model, API key, and CompletionKey are intentionally excluded.

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 ComputeDataContractHash added in v0.26.0

func ComputeDataContractHash() (string, error)

ComputeDataContractHash recomputes the SHA-256 hash of the contract files' contents. It is intended for use in tests and development tooling.

NOTE: This function assumes it is run in a source checkout of the module where the paths in DataContractFiles exist on disk. It is not suitable for use in production binaries where the Go source tree might not be available.

func FilterMessagesByTokenCount

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

func FilterReasoningInputsByContinuationFingerprint added in v0.26.0

func FilterReasoningInputsByContinuationFingerprint(
	inputs []spec.InputUnion,
	currentFingerprint string,
) []spec.InputUnion

FilterReasoningInputsByContinuationFingerprint preserves ordinary history and keeps only reasoning produced by the current provider route and model.

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 NewBufferedStreamerWithError added in v0.22.9

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

NewBufferedStreamerWithError buffers stream data while preserving callback errors from size-based, timer-based, and final flushes.

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 StampReasoningContinuationFingerprint added in v0.26.0

func StampReasoningContinuationFingerprint(
	response *spec.FetchCompletionResponse,
	fingerprint string,
)

StampReasoningContinuationFingerprint records the source route on every normalized reasoning output before it is returned to the caller.

func ToolDescription

func ToolDescription(ct spec.ToolChoice) string

func ValidateDataContract added in v0.26.0

func ValidateDataContract() (string, error)

ValidateDataContract recomputes the hash and compares it to DataContractHash. Tests in this module should call this to enforce that any schema change in the contract files is accompanied by an explicit update of DataContractHash (and, if breaking, DataContractVersion).

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 DataContractInfo added in v0.26.0

type DataContractInfo struct {
	Version string   `json:"version"`
	Hash    string   `json:"hash"`
	Files   []string `json:"files"`
}

DataContractInfo is the public shape returned to callers who want to validate they are compatible with this version of the contract.

func GetDataContractInfo added in v0.26.0

func GetDataContractInfo() DataContractInfo

GetDataContractInfo returns the current contract version/hash metadata.

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