openairesponses

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: 13 Imported by: 0

Documentation

Overview

Package openairesponses is a reference backend emulator for the OpenAI Responses API. It serves POST /v1/responses (or any path suffix /responses) with JSON or SSE bodies compatible with github.com/openai/openai-go/v3.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckInputItems

func CheckInputItems(body []byte, want []InputItemExpect) error

func CheckReasoningInput

func CheckReasoningInput(body []byte, want []ReasoningInputExpect) error

func NewHandler

func NewHandler(cfg Config) http.Handler

NewHandler returns an http.Handler that emulates POST …/responses for the official SDK.

func ScriptedTurnJSON

func ScriptedTurnJSON(turn ScriptedTurn) string

func ScriptedTurnSSE

func ScriptedTurnSSE(turn ScriptedTurn) string

Types

type Config

type Config struct {
	// AllowMissingBearer, if true, skips the Authorization: Bearer check.
	AllowMissingBearer bool
	// OnAuthorizedCredential is invoked after local auth passes with the raw bearer
	// secret (Authorization without the "Bearer " prefix). Do not log this value.
	OnAuthorizedCredential func(secret string)
	// ForcedHTTPStatus, when http.StatusUnauthorized or http.StatusTooManyRequests, returns
	// that status with a provider-shaped JSON error instead of success (stream or JSON).
	ForcedHTTPStatus int
	// ForcedRetryAfter is sent as Retry-After when ForcedHTTPStatus is 429.
	ForcedRetryAfter string
	// ForcedErrorJSON overrides the forced-error JSON body; when empty a minimal default is used.
	ForcedErrorJSON string
	// OnRequestBody is invoked with the full request body after a successful route/auth
	// check and before the response is written.
	OnRequestBody func(body []byte)
	// Responder, when non-nil and ForcedHTTPStatus is zero, builds the HTTP response
	// per request. Must be safe for concurrent use.
	Responder Responder
	// NonStreamJSON overrides the JSON body for non-streaming responses. When empty, a
	// minimal completed response is returned.
	NonStreamJSON string
	// StreamSSE overrides the full SSE payload for streaming responses. When empty, a
	// minimal response.completed plus [DONE] stream is returned.
	StreamSSE string
}

Config tunes the emulator handler.

Response precedence after route/auth succeed:

  1. ForcedHTTPStatus (when non-zero) — Responder is not invoked
  2. Responder (when non-nil) — overrides NonStreamJSON / StreamSSE / defaults
  3. NonStreamJSON / StreamSSE fixed overrides
  4. built-in default JSON / SSE bodies

type EncryptedPresence

type EncryptedPresence string
const (
	EncryptedAbsent EncryptedPresence = "absent"
	EncryptedNull   EncryptedPresence = "null"
	EncryptedEmpty  EncryptedPresence = "empty"
	EncryptedValue  EncryptedPresence = "value"
)

type InputItemExpect

type InputItemExpect struct {
	Kind      string // reasoning|message|function_call
	Label     string
	ToolName  string
	Reasoning *ReasoningInputExpect
}

type OracleLedger

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

func NewOracleLedger

func NewOracleLedger(validators ...RequestValidator) *OracleLedger

func (*OracleLedger) Count

func (l *OracleLedger) Count() int

func (*OracleLedger) Err

func (l *OracleLedger) Err() error

func (*OracleLedger) Hook

func (l *OracleLedger) Hook() func([]byte)

func (*OracleLedger) Observe

func (l *OracleLedger) Observe(body []byte)

type ReasoningInputExpect

type ReasoningInputExpect struct {
	Label      string
	ID         string
	SummaryLen int
	ContentLen int
	HasContent bool
	Encrypted  EncryptedPresence
	Status     string
}

type ReasoningOutputItem

type ReasoningOutputItem struct {
	Label        string
	ID           string
	Summary      []TextPart
	Content      []TextPart
	EncryptedRaw json.RawMessage
	Status       string
}

type Request

type Request struct {
	Sequence int64
	Body     []byte
	Stream   bool
}

type RequestValidator

type RequestValidator func(body []byte) error

func ExpectInputItems

func ExpectInputItems(want []InputItemExpect) RequestValidator

func ExpectNoReasoningInput

func ExpectNoReasoningInput() RequestValidator

func ExpectReasoningInput

func ExpectReasoningInput(want []ReasoningInputExpect) RequestValidator

type Responder

type Responder func(Request) Response

func ScriptedResponder

func ScriptedResponder(turns []ScriptedTurn) Responder

type Response

type Response struct {
	Status  int
	Headers http.Header
	JSON    string
	SSE     string
}

type ScriptedPart

type ScriptedPart struct {
	Reasoning *ReasoningOutputItem
	Message   string
	Tool      *ToolCall
}

ScriptedPart is one ordered output item. Exactly one of Reasoning, Message, or Tool is set.

type ScriptedTurn

type ScriptedTurn struct {
	ResponseID  string
	Parts       []ScriptedPart
	Reasoning   []ReasoningOutputItem
	VisibleText string
	Tool        *ToolCall
}

type TextPart

type TextPart struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

type ToolCall

type ToolCall struct {
	ID        string
	Name      string
	Arguments string
}

Jump to

Keyboard shortcuts

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