requesthandling

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RequestParsingExtensionPoint  = "RequestParsing"
	ResponseParsingExtensionPoint = "ResponseParsing"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AnthropicContent

type AnthropicContent struct {
	Raw        string
	Structured []AnthropicContentBlock
}

AnthropicContent handles the Anthropic content format which can be either a plain string or an array of content blocks.

func (AnthropicContent) MarshalJSON

func (ac AnthropicContent) MarshalJSON() ([]byte, error)

func (*AnthropicContent) UnmarshalJSON

func (ac *AnthropicContent) UnmarshalJSON(data []byte) error

type AnthropicContentBlock

type AnthropicContentBlock struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
	// image source fields (base64 or URL)
	Source *AnthropicImageSource `json:"source,omitempty"`
}

type AnthropicImageSource

type AnthropicImageSource struct {
	Type      string `json:"type"`
	MediaType string `json:"media_type,omitempty"`
	Data      string `json:"data,omitempty"`
	URL       string `json:"url,omitempty"`
}

type AnthropicMessage

type AnthropicMessage struct {
	Role    string           `json:"role"`
	Content AnthropicContent `json:"content"`
}

type AudioBlock

type AudioBlock struct {
	Data   string `json:"data,omitempty"`
	Format string `json:"format,omitempty"`
}

type ChatCompletionsRequest

type ChatCompletionsRequest struct {
	/* parameters from the official OpenAI chat-completions API */
	Messages []Message `json:"messages,omitempty"`
	Tools    []any     `json:"tools,omitempty"`
	/* parameters from the HuggingFace transformers chat-templates API */
	Documents                 []any          `json:"documents,omitempty"`
	ChatTemplate              string         `json:"chat_template,omitempty"`
	ReturnAssistantTokensMask bool           `json:"return_assistant_tokens_mask,omitempty"`
	ContinueFinalMessage      bool           `json:"continue_final_message,omitempty"`
	AddGenerationPrompt       bool           `json:"add_generation_prompt,omitempty"`
	ChatTemplateKWArgs        map[string]any `json:"chat_template_kwargs,omitempty"`
	// CacheSalt is an optional request parameter to isolate prefix caches for security reasons.
	CacheSalt string `json:"cache_salt,omitempty"`
}

ChatCompletionsRequest is a structured representation of the fields we parse out of the v1/chat/completions request body. For detailed body fields, please refer to https://platform.openai.com/docs/api-reference/chat. This struct includes fields usable for plugins and scheduling decisions - and not the entire API spec.

func (*ChatCompletionsRequest) String

func (r *ChatCompletionsRequest) String() string

type Claims

type Claims struct {
	Paths     []string         // path patterns this parser claims (e.g., "chat/completions")
	Protocols []v1.AppProtocol // protocols this parser supports (e.g., "h2c")
}

Claims defines the matching criteria for a parser.

type CompletionsRequest

type CompletionsRequest struct {
	// Prompt is the prompt(s) sent in the request body; can be a string or an array of strings.
	Prompt Prompt `json:"prompt"`
	// CacheSalt is an optional request parameter to isolate prefix caches for security reasons.
	CacheSalt string `json:"cache_salt,omitempty"`
}

CompletionsRequest is a structured representation of the fields we parse out of the /v1/completions request body. For detailed body fields, please refer to https://platform.openai.com/docs/api-reference/completions. This struct includes fields usable for plugins and scheduling decisions - and not the entire API spec.

func (*CompletionsRequest) String

func (r *CompletionsRequest) String() string

type Content

type Content struct {
	Raw        string
	Structured []ContentBlock
}

func (Content) MarshalJSON

func (mc Content) MarshalJSON() ([]byte, error)

func (Content) PlainText

func (mc Content) PlainText() string

func (*Content) UnmarshalJSON

func (mc *Content) UnmarshalJSON(data []byte) error

UnmarshalJSON allow use both format

type ContentBlock

type ContentBlock struct {
	Type       string     `json:"type"`
	Text       string     `json:"text,omitempty"`
	ImageURL   ImageBlock `json:"image_url"`
	InputAudio AudioBlock `json:"input_audio"`
	VideoURL   VideoBlock `json:"video_url"`
}

type ConversationItem

type ConversationItem struct {
	// Type specifies the item type (message, file, etc.)
	Type string `json:"type,omitempty"`
	// Role specifies the role (user, assistant, system)
	Role string `json:"role,omitempty"`
	// Content contains the item content
	Content any `json:"content,omitempty"`
}

ConversationItem represents a single item in a conversation

type ConversationsRequest

type ConversationsRequest struct {
	// Items is the array of conversation items (messages, files, etc.)
	Items []ConversationItem `json:"items,omitempty"`
	// Metadata provides additional context for the conversation
	Metadata map[string]any `json:"metadata,omitempty"`
	// CacheSalt isolates prefix caches for security
	CacheSalt string `json:"cache_salt,omitempty"`
}

ConversationsRequest represents the OpenAI /v1/conversations request body structure

func (*ConversationsRequest) String

func (c *ConversationsRequest) String() string

type EmbeddingsInput

type EmbeddingsInput struct {
	Raw      string
	Strings  []string
	TokenIDs []uint32
}

EmbeddingsInput represents the input field in a /v1/embeddings request. Per the OpenAI spec it can be a string, an array of strings, or an array of integers.

func (EmbeddingsInput) IsEmpty

func (e EmbeddingsInput) IsEmpty() bool

func (EmbeddingsInput) PlainText

func (e EmbeddingsInput) PlainText() string

func (EmbeddingsInput) TokenCountHint

func (e EmbeddingsInput) TokenCountHint() int

func (*EmbeddingsInput) UnmarshalJSON

func (e *EmbeddingsInput) UnmarshalJSON(data []byte) error

type EmbeddingsRequest

type EmbeddingsRequest struct {
	// Input is the text to embed (string or array of strings).
	Input EmbeddingsInput `json:"input,omitempty"`
	// CacheSalt is an optional request parameter to isolate prefix caches for security reasons.
	CacheSalt string `json:"cache_salt,omitempty"`
}

EmbeddingsRequest represents the OpenAI /v1/embeddings request body structure. Input can be a string or array of strings; see https://platform.openai.com/docs/api-reference/embeddings.

func (*EmbeddingsRequest) String

func (e *EmbeddingsRequest) String() string

type GenerateRequest

type GenerateRequest struct {
	// TokenIDs are the pre-tokenized input token IDs.
	TokenIDs []uint32 `json:"token_ids"`
	// Features carries multimodal metadata (per-modality content hashes and
	// placeholder ranges) parsed out of the wire `features` block. Populated
	// by UnmarshalJSON; not itself a JSON-tagged field.
	Features *tokenization.MultiModalFeatures `json:"-"`
	// CacheSalt is an optional request parameter to isolate prefix caches for security reasons.
	CacheSalt string `json:"cache_salt,omitempty"`
}

GenerateRequest is a structured representation of the fields we parse out of the vLLM request at /inference/v1/generate. Unlike the OpenAI-compatible endpoints, this API accepts pre-tokenized input (token IDs). This struct includes fields usable for plugins and scheduling decisions.

func (*GenerateRequest) String

func (r *GenerateRequest) String() string

func (*GenerateRequest) UnmarshalJSON

func (r *GenerateRequest) UnmarshalJSON(data []byte) error

type ImageBlock

type ImageBlock struct {
	URL string `json:"url,omitempty"`
}

type InferenceRequestBody

type InferenceRequestBody struct {
	// CompletionsRequest is the representation of the OpenAI /v1/completions request body.
	Completions *CompletionsRequest `json:"completions,omitempty"`
	// ChatCompletionsRequest is the representation of the OpenAI /v1/chat/completions request body.
	ChatCompletions *ChatCompletionsRequest `json:"chat_completions,omitempty"`
	// MessagesRequest is the representation of the Claude /v1/messages request body.
	Messages *MessagesRequest `json:"messages,omitempty"`
	// ResponsesRequest is the representation of the OpenAI /v1/responses request body.
	Responses *ResponsesRequest `json:"responses,omitempty"`
	// ConversationsRequest is the representation of the OpenAI /v1/conversations request body.
	Conversations *ConversationsRequest `json:"conversations,omitempty"`
	// EmbeddingsRequest is the representation of the OpenAI /v1/embeddings request body.
	Embeddings *EmbeddingsRequest `json:"embeddings,omitempty"`
	// GenerateRequest is the representation of the vLLM /inference/v1/generate request body.
	Generate *GenerateRequest `json:"generate,omitempty"`
	// Payload contains the unmarshaled request payload or raw bytes.
	// If the payload is unmarshaled, we can perform advanced processing (like prefix cache aware routing).
	// If it remains as raw bytes, such processing may not be supported.
	Payload RequestPayload `json:"-"`
	// TokenizedPrompt contains parser-derived tokenization results when available.
	// It is nil when the request was not already tokenized.
	TokenizedPrompt *TokenizedPrompt `json:"-"`

	// Stream indicates whether the request specifies a streaming response (e.g., via a stream field).
	// This typically implies the model server's response will be streamed.
	Stream bool `json:"-"`
}

InferenceRequestBody contains the request-body fields that we parse out as user input, to be used in forming scheduling decisions. An InferenceRequestBody must contain exactly one of CompletionsRequest, ChatCompletionsRequest, ResponsesRequest, ConversationsRequest, EmbeddingsRequest, GenerateRequest, or MessagesRequest.

type Message

type Message struct {
	// Role is the message Role, optional values are 'user', 'assistant', ...
	Role string `json:"role,omitempty"`
	// Content defines text of this message
	Content Content `json:"content"`
	// ToolCalls contains assistant tool calls for chat template rendering.
	ToolCalls []any `json:"tool_calls,omitempty"`
}

Message represents a single message in a chat-completions request.

type MessagesRequest

type MessagesRequest struct {
	// Messages is the array of conversation messages with alternating user/assistant roles.
	Messages []AnthropicMessage `json:"messages,omitempty"`
	// System is the system prompt. In the Anthropic API this is a top-level field,
	// not a message with role "system".
	System AnthropicContent `json:"system,omitempty"`
	// Tools field for tool use capabilities.
	Tools []any `json:"tools,omitempty"`
	// CacheSalt isolates prefix caches for security.
	CacheSalt string `json:"cache_salt,omitempty"`
}

MessagesRequest is a structured representation of the fields we parse out of the /v1/messages request body. For detailed body fields, please refer to https://docs.anthropic.com/en/api/messages. This struct includes fields usable for plugins and scheduling decisions - and not the entire API spec.

func (*MessagesRequest) String

func (r *MessagesRequest) String() string

type Modality

type Modality string

Modality identifies the type of multimodal content in a prompt.

const (
	ModalityImage Modality = "image"
	ModalityAudio Modality = "audio"
	ModalityVideo Modality = "video"
)

Modality values match the model-server's multimodal hash keys so labels agree across backends.

type MultiModalFeature

type MultiModalFeature struct {
	// Modality identifies the type of content.
	Modality Modality
	// Hash is the content hash of the item, used for KV-cache reuse across requests.
	Hash string
	// Offset is the index of the first placeholder token for this item in TokenIDs.
	Offset int
	// Length is the number of placeholder tokens this item occupies in TokenIDs.
	Length int
}

MultiModalFeature holds all data needed for prefix-cache scoring of a single multimodal item. Items are ordered by token position within the prompt. Currently only ModalityImage is supported.

type ParseResult

type ParseResult struct {
	// Body contains the parsed inference request body.
	Body *InferenceRequestBody
	// SkipResponseProcessing indicates whether to skip EPP stream interception for this request.
	// When set to true, the request will still go through the scheduling director
	// (allowing routing decisions, profiles, and admission control to run),
	// but the EPP will stop intercepting the stream after the request phase completes
	// (e.g., response headers and body will not be processed).
	//
	// This allows fallback or non-standard requests to be routed using the configured
	// policies without paying the overhead of response-phase interception.
	SkipResponseProcessing bool
}

ParseResult contains the result of parsing the request.

type ParsedResponse

type ParsedResponse struct {
	// Usage is only populate when the raw response has usage.
	Usage *Usage
}

type Parser

type Parser interface {
	fwkplugin.Plugin
	// ParseRequest parses the request body and headers and returns the parsed result.
	// There are three outcomes based on the return values:
	// 1. err != nil: The request is invalid or cannot be parsed. The framework will fail the request early.
	// 2. err == nil and result.SkipResponseProcessing == true: The request is valid but EPP should stop intercepting the stream
	//    after the request phase. The scheduling director will still route the request, but subsequent
	//    response interception phases will be skipped.
	// 3. err == nil and result.SkipResponseProcessing == false: The request is valid and will be processed by the scheduling framework.
	ParseRequest(ctx context.Context, body []byte, headers map[string]string) (*ParseResult, error)

	// ParseResponse parses the response payload.
	// For streaming responses , this method is invoked multiple times (once per chunk),
	// where 'endOfStream' is set to true only for the final chunk.
	// For non-streaming responses, this method is invoked exactly once with the full
	// buffered response body and 'endOfStream' set to true.
	ParseResponse(ctx context.Context, body []byte, headers map[string]string, endofStream bool) (*ParsedResponse, error)

	// Claims returns the paths and protocols claimed by this parser.
	Claims() Claims
}

Parser defines the interface for parsing payload(requests and responses).

type PayloadMap

type PayloadMap map[string]any

PayloadMap represents a JSON request body unmarshaled into a map.

func (PayloadMap) AsMap

func (p PayloadMap) AsMap() (PayloadMap, bool)

func (PayloadMap) IsParsed

func (p PayloadMap) IsParsed() bool

type PayloadProto

type PayloadProto struct {
	proto.Message
}

PayloadProto represents a gRPC request body unmarshaled into a proto.Message.

func (PayloadProto) AsMap

func (PayloadProto) AsMap() (PayloadMap, bool)

func (PayloadProto) IsParsed

func (PayloadProto) IsParsed() bool

type Prompt

type Prompt struct {
	Raw      string
	Strings  []string
	TokenIDs []uint32
}

Prompt represents the prompt field in a /v1/completions request. Per the OpenAI spec it can be a string or an array of strings. See https://platform.openai.com/docs/api-reference/completions/create#completions-create-prompt

func (Prompt) IsEmpty

func (p Prompt) IsEmpty() bool

func (Prompt) MarshalJSON

func (p Prompt) MarshalJSON() ([]byte, error)

func (Prompt) PlainText

func (p Prompt) PlainText() string

func (Prompt) TokenCountHint

func (p Prompt) TokenCountHint() int

func (*Prompt) UnmarshalJSON

func (p *Prompt) UnmarshalJSON(data []byte) error

type PromptTokenDetails

type PromptTokenDetails struct {
	CachedTokens int `json:"cached_tokens"`
}

type RawPayload

type RawPayload []byte

RawPayload represents an unparsed request body kept as raw bytes.

func (RawPayload) AsMap

func (RawPayload) AsMap() (PayloadMap, bool)

func (RawPayload) IsParsed

func (RawPayload) IsParsed() bool

type RequestPayload

type RequestPayload interface {
	IsParsed() bool
	// AsMap returns the parsed JSON map
	AsMap() (PayloadMap, bool)
	// contains filtered or unexported methods
}

RequestPayload represents a strongly-typed unmarshaled request payload or raw bytes.

type ResponsesRequest

type ResponsesRequest struct {
	// Input can be either a string or an array of conversation items
	Input any `json:"input,omitempty"`
	// Instructions provides optional system-level guidance
	Instructions any `json:"instructions,omitempty"`
	// Tools field for function calling capabilities
	Tools any `json:"tools,omitempty"`
	// CacheSalt isolates prefix caches for security
	CacheSalt string `json:"cache_salt,omitempty"`
}

ResponsesRequest represents the OpenAI /v1/responses request body structure

func (*ResponsesRequest) String

func (r *ResponsesRequest) String() string

type TokenizedPrompt

type TokenizedPrompt struct {
	// PerPromptTokens holds the token IDs for each prompt in the request.
	// Single-prompt requests (chat, generate, single-string completions) use a
	// length-1 outer slice. Multi-string completions use one inner slice per
	// prompt string.
	PerPromptTokens [][]uint32
	// MultiModalFeatures holds one entry per multimodal item in prompt order.
	// Nil if the prompt contains no multimodal content. Offsets are relative
	// to PerPromptTokens[0] (always single-prompt when multimodal content is
	// present).
	MultiModalFeatures []MultiModalFeature
	// CacheSalt isolates prefix caches across requests. Populated by the token-producer.
	CacheSalt string
}

TokenizedPrompt contains the result of tokenizing the request prompt. It is consumed by scheduling and request-control plugins that benefit from actual token data such as prefix-cache awareness.

func (*TokenizedPrompt) TokenCount

func (tp *TokenizedPrompt) TokenCount() int

TokenCount returns the total number of tokens across all prompts.

type Usage

type Usage struct {
	PromptTokens       int                 `json:"prompt_tokens"`
	CompletionTokens   int                 `json:"completion_tokens"`
	TotalTokens        int                 `json:"total_tokens"`
	PromptTokenDetails *PromptTokenDetails `json:"prompt_tokens_details,omitempty"`
}

Usage counts parsed from the response body.

type VideoBlock

type VideoBlock struct {
	URL string `json:"url,omitempty"`
}

Jump to

Keyboard shortcuts

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