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
- func AsRouterError(err error) *router.Error
- func HTTPStatus(kind router.Kind) int
- func Index(path string, i int) string
- func Inspect(raw []byte, opts DecodeOptions) error
- func IsArray(raw json.RawMessage) bool
- func IsNull(raw json.RawMessage) bool
- func IsObject(raw json.RawMessage) bool
- func IsString(raw json.RawMessage) bool
- func OpenOpaque(sealed string) (string, json.RawMessage, error)
- func SealOpaque(source string, state json.RawMessage) (string, error)
- type DecodeOptions
- type EncodeOptions
- type Object
- func (o *Object) Array(key string) (items []json.RawMessage, present bool, err error)
- func (o *Object) Bool(key string) (value bool, present bool, err error)
- func (o *Object) Field(key string) string
- func (o *Object) Finish() error
- func (o *Object) Float(key string) (value float64, present bool, err error)
- func (o *Object) Has(key string) bool
- func (o *Object) Int(key string) (value int64, present bool, err error)
- func (o *Object) Object(key string, nullable ...string) (child *Object, present bool, err error)
- func (o *Object) Options() DecodeOptions
- func (o *Object) Path() string
- func (o *Object) Raw(key string) json.RawMessage
- func (o *Object) String(key string) (value string, present bool, err error)
- func (o *Object) StringMap(key string) (value map[string]string, present bool, err error)
- func (o *Object) Unsupported(key string) error
Constants ¶
const MaxDepth = 32
MaxDepth bounds JSON nesting accepted from clients.
const MaxOpaqueBytes = 512 << 10
MaxOpaqueBytes bounds a sealed opaque state string accepted from a client.
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
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 ¶
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 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 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) Finish ¶
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) Options ¶
func (o *Object) Options() DecodeOptions
Options returns the decode options this object was parsed with.
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) Unsupported ¶
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. |