passthroughparse

package
v1.12.0 Latest Latest
Warning

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

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

Documentation

Overview

Package passthroughparse decodes the raw request and response bodies of a passthrough call into Bifrost's typed request/response schemas, so passthrough traffic can be logged and rendered like any other request. Routing is shared with the providers' passthrough usage extractors (each provider's ClassifyPassthroughPath and vertex.PassthroughWireFormat), so a path is classified the same way for billing and for logging.

Everything here is a pure transformation of the bytes it is handed: no network, no logging, no shared state. Classify and InferRequestType only read the route and suit the request path. Parse and RedactStreamBody read whole bodies: the logging plugin parses from its batch writer, and request-path callers use them on a complete body (a response, or a held stream's final chunk).

Index

Constants

This section is empty.

Variables

View Source
var ErrUpstreamStreamFailed = errors.New("upstream ended the stream with an error")

ErrUpstreamStreamFailed reports a recorded stream that ended on an error event the upstream sent, such as Anthropic's overloaded_error, rather than on bytes that did not decode. The response Parse returns alongside it holds everything that streamed before the error.

Functions

func InferRequestType

func InferRequestType(provider schemas.ModelProvider, model, method, path string, stream bool) schemas.RequestType

InferRequestType returns the request type a passthrough call addresses from its route alone (base provider, model, method, path) without reading either body, or "" when the route has no typed equivalent. stream selects the streaming variant where one exists. It is cheap enough for the request path: a few string suffix checks.

func RedactStreamBody added in v1.12.0

func RedactStreamBody(body []byte, format schemas.RawResponseFormat, replacements map[string]string) ([]byte, bool)

RedactStreamBody applies literal replacements to the text of a recorded stream body written in format: an SSE body, or a Gemini JSON array of chunks. Values split across events are matched whole (see schemas.RedactRawResponseText). Only the rewritten JSON strings change; framing, events without text, and every other byte are kept. ok reports framing only: it is false, with body unchanged, when an event's data spans several data: lines, which leaves its payload without a single byte range. A payload that is not a valid JSON object ([DONE], a cut-off last event) is left as it is, so callers that must not release such text check the result themselves.

func SummarizeMultipart

func SummarizeMultipart(contentType string, body []byte) (summary []byte, ok bool)

SummarizeMultipart turns a multipart/form-data body into the JSON of its MultipartSummary. It returns ok=false when contentType is not multipart or the body does not read as one, so the caller can keep its previous behaviour (no body logged).

It runs on the request path (PreLLMHook, content logging on), so it never copies or decodes file parts: the body is already in memory, and each part's extent is found with bytes.Index on the delimiter, which skips file contents at memchr speed instead of streaming them through a reader.

Types

type Input

type Input struct {
	Provider     schemas.ModelProvider // base provider type; custom providers pass their BaseProviderType
	Model        string                // resolved model (from the path or the body's "model" field)
	Method       string
	Path         string // stripped request path, as in PassthroughLogParams.Path
	StatusCode   int    // upstream status; error bodies (>= 400) are never parsed as responses
	RequestBody  []byte
	ResponseBody []byte
	Stream       bool
}

Input is one passthrough call as the logging plugin recorded it.

type MultipartFile

type MultipartFile struct {
	Field       string `json:"field"`
	Filename    string `json:"filename,omitempty"`
	ContentType string `json:"content_type,omitempty"`
	Bytes       int64  `json:"bytes"`
}

MultipartFile describes one file part of a multipart body.

type MultipartSummary

type MultipartSummary struct {
	Fields    map[string]any  `json:"fields"` // a string, or a []string for a repeated field
	Files     []MultipartFile `json:"files,omitempty"`
	Truncated bool            `json:"truncated,omitempty"` // some field text was cut or left out to bound the summary
}

MultipartSummary is the loggable form of a multipart/form-data passthrough body (audio transcriptions, image edits): its text fields as sent, and only the name, type and size of each file part. File contents are never kept.

type Result

type Result struct {
	RequestType schemas.RequestType // the inner kind the path addresses, e.g. ChatCompletionRequest
	Request     *schemas.BifrostRequest
	Response    *schemas.BifrostResponse
}

Result is the typed view of a passthrough call. Request and Response are independent: either can be nil when its body was absent or did not decode, and the other is still usable.

func Parse

func Parse(in Input) (*Result, error)

Parse decodes a passthrough call on a best-effort basis. It returns (nil, nil) for routes with no typed equivalent (unsupported providers, list/retrieve calls, Bedrock agent routes), so the caller keeps the raw bodies only. A non-nil error describes a body that matched a known route but failed to decode; the Result alongside it still carries whichever side did decode. Parse never panics: a converter that panics on an unexpected body is reported as an error for that side.

type Route added in v1.12.0

type Route struct {
	Wire        schemas.ModelProvider // API the bodies are written in: OpenAI, Anthropic, or Gemini
	RequestType schemas.RequestType   // inner request kind, e.g. ChatCompletionRequest; never a streaming variant
}

Route is the API shape and inner request kind a passthrough call addresses.

func Classify added in v1.12.0

func Classify(provider schemas.ModelProvider, model, method, path string) Route

Classify returns the API shape and request kind a passthrough call addresses from its route alone (base provider, model, method, path), without reading either body. The zero Route means the route has no typed equivalent. Like InferRequestType it is a few string checks, cheap enough for the request path.

func (Route) StreamFormat added in v1.12.0

func (route Route) StreamFormat() schemas.RawResponseFormat

StreamFormat names the declared format of the route's stream events, as RedactStreamBody and schemas.RedactRawResponseText read it, or "" when Parse cannot assemble the route's stream.

Jump to

Keyboard shortcuts

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