Help us shape the future of Go! Take the 2026 Go Developer Survey

adapterkit

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package adapterkit holds the helpers every first-party adapter needs but no vendor SDK touches: schema rendering, the terminal-error rule, and the FilePart exactly-one guard. They lived as byte-identical copies in all three adapters until the 2026-09-18 review flagged the drift risk; they move here rather than into the root's public API — THE-END-GOAL principle 3 names internal/ as the tool that keeps the guaranteed surface small, and Go's path-based internal rule lets github.com/weftgo/weft/{openai,anthropic,google} import the package while third-party adapters cannot. Helpers that mention an SDK type (the stream readers) stay per-adapter by design (ADR 0013).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneHeaders added in v0.3.0

func CloneHeaders(h http.Header) http.Header

CloneHeaders copies h with each value slice cloned — ExtraHeaders' construction-time snapshot, the same isolation CloneJSON gives ExtraBody.

func CloneJSON added in v0.3.0

func CloneJSON(v any) any

CloneJSON deep-copies a JSON-shaped value — the container shapes MergeBody recurses through (maps and slices), with everything else returned as is: scalars and structs are copied by their interface conversion already, and only the decoded-JSON shapes alias. The ExtraBody options snapshot the caller's map at construction with this, so a Model stays safe for concurrent runs while the caller keeps mutating what it passed (ADR 0013's Models-are-immutable stance).

func FilePartSource

func FilePartSource(p weft.FilePart) error

FilePartSource validates that a FilePart sets exactly one of Data and URL — the shape every provider's file input takes. Both or neither is a caller bug, refused wrapping ErrUnsupported before any request.

func MergeBody added in v0.3.0

func MergeBody(body []byte, extra map[string]any) ([]byte, error)

MergeBody deep-merges extra into a JSON request body and returns the re-encoded bytes: nested maps merge recursively, every other value — scalars, arrays, null — replaces. The caller's key wins on conflict at every level; this is the one semantic all three adapters' ExtraBody options share, matching genai's native recursiveMapMerge (ADR 0013's 2026-09-22 amendment). A body that is not a JSON object fails, rather than being silently replaced.

func NextCallID

func NextCallID(used map[string]bool, ordinal int) string

NextCallID synthesises the id for a streamed tool call whose server omitted one: "call_<n>" with n starting at ordinal+1, skipping ids the step has already claimed and marking the chosen id in used. Skipping matters: a repeated id within one step fails the run with ErrModelContract — the exact failure the synthesis exists to prevent — when a server populates some ids of the step itself (llama.cpp-style) with the same call_<n> shape.

func SchemaMap

func SchemaMap(s *weft.Schema) map[string]any

SchemaMap renders a weft.Schema as the plain JSON map the vendors' tool-parameter fields expect (openai-go's FunctionParameters, anthropic-sdk-go's InputSchema). Nil stays nil: a schema-less tool takes the provider's default shape. The rendering goes through json.Marshal, which honours Schema.MarshalJSON — so a schema parsed with weft.ParseSchema reaches the provider as its verbatim foreign bytes (enum, oneOf and all), where the hand-built map this replaced degraded them to the struct's own vocabulary. One rendering change rode along: an unconstrained node (a recursion cut, an interface field) marshalled as {"type": ""} before and is {} now (ADR 0003's 2026-09-19 amendment).

func TerminalErr

func TerminalErr(ctx context.Context, err error) error

TerminalErr reports a stream's terminal error the way the Model contract expects: ctx.Err() when the caller's context ended (the vendor SDKs wrap cancellation in their own error types, and the reader goroutine can exit its handshake with a nil SDK error), the error unchanged otherwise — so callers can errors.As the SDK's type.

Types

This section is empty.

Jump to

Keyboard shortcuts

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