dialect

package
v1.0.178 Latest Latest
Warning

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

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

Documentation

Overview

Package dialect holds what the public API codecs share: decode and encode options, lenient/strict JSON object parsing with dialect-aware null handling, the sealed envelope for opaque reasoning state, and the mapping from router error kinds to HTTP status codes.

Concrete codecs live in subpackages: openai (Chat Completions and Responses) and anthropic (Messages). Each exposes Decode*/Encode*/EncodeError functions over bytes; none of them serves HTTP. A host handler reads a bounded body, decodes, calls router.Generate, and encodes the result or the error.

Index

Constants

View Source
const MaxDepth = 32

MaxDepth bounds JSON nesting accepted from clients.

View Source
const MaxOpaqueBytes = 512 << 10

MaxOpaqueBytes bounds a sealed opaque state string accepted from a client.

View Source
const MetadataUser = "user"

MetadataUser is the Request.Metadata key codecs use for the caller-supplied end-user identifier (OpenAI user / safety_identifier, Anthropic metadata.user_id).

Variables

This section is empty.

Functions

func AsRouterError added in v1.0.178

func AsRouterError(err error) *router.Error

AsRouterError returns the router error a codec encodes for err. It is router.AsError, except that a nil err, which a host should never pass, gives a KindInternal error instead of nil, so an EncodeError never dereferences nil.

func HTTPStatus

func HTTPStatus(kind router.Kind) int

HTTPStatus maps a router error kind to the HTTP status every supported dialect uses for it. Hosts may override the mapping in their own handlers.

func Index

func Index(path string, i int) string

Index formats an array element path such as "messages[2]".

func Inspect

func Inspect(raw []byte, opts DecodeOptions) error

Inspect checks that raw is one well-formed JSON value without trailing data and within MaxDepth. In strict mode it also rejects duplicate object keys. Null handling is decided per field by Object.

func IsArray

func IsArray(raw json.RawMessage) bool

IsArray reports whether raw is a JSON array.

func IsNull

func IsNull(raw json.RawMessage) bool

IsNull reports whether raw is absent or the JSON null literal.

func IsObject

func IsObject(raw json.RawMessage) bool

IsObject reports whether raw is a JSON object.

func IsString

func IsString(raw json.RawMessage) bool

IsString reports whether raw is a JSON string.

func OpenOpaque

func OpenOpaque(sealed string) (string, json.RawMessage, error)

OpenOpaque restores the source target and provider state from a sealed string. Malformed input, including a missing or JSON null state, is a client error.

func SealOpaque

func SealOpaque(source string, state json.RawMessage) (string, error)

SealOpaque packs a reasoning block's source target and provider state into one URL-safe string that clients replay verbatim.

Types

type DecodeOptions

type DecodeOptions struct {
	// Strict rejects unknown fields, duplicate keys and null values instead of
	// ignoring them. Known fields with unsupported non-default values are always
	// rejected, in both modes.
	Strict bool
}

DecodeOptions controls how much a codec tolerates in incoming requests.

type EncodeOptions

type EncodeOptions struct {
	// OmitReasoning removes reasoning items from the public response. The Go
	// Result still contains them; clients then cannot replay opaque state.
	OmitReasoning bool
}

EncodeOptions controls what a codec writes into public responses.

type Object

type Object struct {
	// contains filtered or unexported fields
}

Object is one decoded JSON object with dialect-aware null and unknown-field handling. Path names the object in error messages ("messages[2]").

func ParseObject

func ParseObject(raw json.RawMessage, path string, opts DecodeOptions, nullable ...string) (*Object, error)

ParseObject decodes raw as an object. In lenient mode null members are treated as absent. In strict mode a null member is rejected unless its key is listed in nullable. A non-object value is a client error naming path.

func (*Object) Array

func (o *Object) Array(key string) (items []json.RawMessage, present bool, err error)

Array reads an array member as raw elements.

func (*Object) Bool

func (o *Object) Bool(key string) (value bool, present bool, err error)

Bool reads a boolean member.

func (*Object) Field

func (o *Object) Field(key string) string

Field returns the error-message path of a member.

func (*Object) Finish

func (o *Object) Finish() error

Finish must be called after all known members were read. In strict mode any unread member is an unknown field and rejected; in lenient mode it is ignored.

func (*Object) Float

func (o *Object) Float(key string) (value float64, present bool, err error)

Float reads a finite number member.

func (*Object) Has

func (o *Object) Has(key string) bool

Has reports whether key is present (and, in lenient mode, non-null).

func (*Object) Int

func (o *Object) Int(key string) (value int64, present bool, err error)

Int reads an integral number member. Fractions and out-of-range values fail.

func (*Object) Object

func (o *Object) Object(key string, nullable ...string) (child *Object, present bool, err error)

Object reads a nested object member with the same options.

func (*Object) Options

func (o *Object) Options() DecodeOptions

Options returns the decode options this object was parsed with.

func (*Object) Path

func (o *Object) Path() string

Path returns the object's location for error messages.

func (*Object) Raw

func (o *Object) Raw(key string) json.RawMessage

Raw returns the member bytes, or nil when absent. It marks the key as known.

func (*Object) String

func (o *Object) String(key string) (value string, present bool, err error)

String reads a string member. present is false when absent.

func (*Object) StringMap

func (o *Object) StringMap(key string) (value map[string]string, present bool, err error)

StringMap reads an object of string values.

func (*Object) Unsupported

func (o *Object) Unsupported(key string) error

Unsupported reports a known member whose value the dialect cannot honor.

Directories

Path Synopsis
Package anthropic is the Anthropic Messages codec for the router.
Package anthropic is the Anthropic Messages codec for the router.
Package openai translates the OpenAI Chat Completions and Responses request and response shapes to and from the portable router contract.
Package openai translates the OpenAI Chat Completions and Responses request and response shapes to and from the portable router contract.

Jump to

Keyboard shortcuts

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