anthropic

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package anthropic implements the Anthropic Messages–compatible HTTP frontend.

v1 subset vs vendor spec (cross-check)

Spec: https://docs.anthropic.com/en/api/messages

| Area | v1 status | Tests | |-------------------|------------|-------| | POST …/messages | Supported | integration_test.go, decode_test.go, encode_test.go | | system string/blocks (text) | Supported | decode_test.go | | messages user/assistant | Supported | decode_test.go | | tools, tool_choice | Supported (decode) | decode_test.go | | max_tokens, sampling | Supported | decode_test.go | | metadata | Ignored (not mapped to Call) | decode_test.go | | top_k | Ignored (not mapped to Call) | decode_test.go | | anthropic-version header | Passed via DecodeOptions (not validated; wire compatibility) | integration_test.go | | tool_use on response (stream + JSON) | Supported (encode) | encode_test.go, integration_test.go | | thinking / extended blocks | Supported (encode, thinking + signature) | encode_test.go |

Index

Constants

View Source
const (
	HeaderRouteSelector    = routeselect.HeaderRouteSelector
	HeaderAnthropicVersion = "anthropic-version"
)
View Source
const ID = "anthropic"

ID is the reserved plugin identifier for the Anthropic frontend.

Variables

This section is empty.

Functions

func ModelFromCall

func ModelFromCall(c *lipapi.Call) string

ModelFromCall returns the wire model string stored during decode.

func Mount

func Mount(mux *http.ServeMux, opts lipsdk.FrontendMountOptions) error

Mount registers the Anthropic Messages API handler on mux.

func RouteClaims

func RouteClaims(ownerID string) ([]httpcontract.RouteClaim, error)

RouteClaims describes the routes Mount registers for one frontend owner.

func WriteErrorJSON

func WriteErrorJSON(w http.ResponseWriter, status int, message, errType string) error

WriteErrorJSON writes an Anthropic-shaped JSON error before any streamed bytes.

func WriteNonStreamJSON

func WriteNonStreamJSON(ctx context.Context, w http.ResponseWriter, call *lipapi.Call, es lipapi.EventStream, opts EncodeOptions) error

WriteNonStreamJSON encodes a completed canonical stream as a Messages API JSON body.

func WriteStreamSSE

func WriteStreamSSE(ctx context.Context, w http.ResponseWriter, call *lipapi.Call, es lipapi.EventStream, opts EncodeOptions) error

Types

type Config

type Config = frontendconfig.Config

func DecodeConfig

func DecodeConfig(n yaml.Node) (Config, error)

type DecodeOptions

type DecodeOptions struct {
	// RouteSelector is required (e.g. "stub:claude-3-5-haiku"); usually from X-LIP-Route header.
	RouteSelector string
	// AnthropicVersion is the optional anthropic-version header; empty means default for decode.
	AnthropicVersion string
	Headers          http.Header
}

DecodeOptions configures decoding of a Messages API request.

type DecodedMessage

type DecodedMessage struct {
	Call   *lipapi.Call
	Stream bool
	Model  string
}

DecodedMessage is the result of decoding POST /v1/messages JSON.

func DecodeMessageRequest

func DecodeMessageRequest(body []byte, opts DecodeOptions) (*DecodedMessage, error)

DecodeMessageRequest maps Anthropic Messages JSON into a canonical call.

type EncodeOptions

type EncodeOptions struct {
	MessageID                string
	ExposeLipUsageExtensions bool
}

EncodeOptions controls wire identifiers for encoded Messages payloads.

type Handler

type Handler struct {
	Exec lipsdk.ExecutorView
	// DefaultRouteSelector is used when HeaderRouteSelector is absent.
	DefaultRouteSelector string
	// RoutePrefixes are backend route-selector prefixes accepted from body model.
	RoutePrefixes           routeselect.PrefixSet
	MaxRequestBodyBytes     int64
	Log                     *slog.Logger
	TrafficPorts            traffic.PortBundle
	DecodeAdmission         lipsdk.DecodeAdmission
	PreRequestKeepalive     lipsdk.FrontendKeepaliveConfig
	Config                  Config
	HTTPHeaders             lipsdk.HTTPHeaders
	StreamKeepaliveInterval time.Duration
	// contains filtered or unexported fields
}

Handler wires HTTP POST /v1/messages to decode → executor → encode.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements Messages create on POST …/messages.

type WireErrors

type WireErrors struct{}

WireErrors implements frontendpipe.WireErrors for Anthropic Messages API errors.

func (WireErrors) WriteAdmissionReject

func (WireErrors) WriteAdmissionReject(w http.ResponseWriter, d decodeqos.Decision) error

func (WireErrors) WriteBodyTooLarge

func (WireErrors) WriteBodyTooLarge(w http.ResponseWriter) error

func (WireErrors) WriteEncodeFailed

func (WireErrors) WriteEncodeFailed(w http.ResponseWriter) error

func (WireErrors) WriteExecuteError

func (WireErrors) WriteExecuteError(w http.ResponseWriter, out execerr.Outcome) error

func (WireErrors) WriteExecutorNotConfigured

func (WireErrors) WriteExecutorNotConfigured(w http.ResponseWriter) error

func (WireErrors) WriteInvalidJSON

func (WireErrors) WriteInvalidJSON(w http.ResponseWriter) error

func (WireErrors) WriteInvalidRequest

func (WireErrors) WriteInvalidRequest(w http.ResponseWriter) error

func (WireErrors) WritePreflightCanceled

func (WireErrors) WritePreflightCanceled(w http.ResponseWriter) error

func (WireErrors) WriteReadBodyFailed

func (WireErrors) WriteReadBodyFailed(w http.ResponseWriter) error

Jump to

Keyboard shortcuts

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