openaichat

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

Documentation

Overview

Package openaichat is a reference backend emulator for the OpenAI Chat Completions API. It serves POST …/chat/completions 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 NewHandler

func NewHandler(cfg Config) http.Handler

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

func ScriptedTurnJSON

func ScriptedTurnJSON(turn ScriptedTurn) string

ScriptedTurnJSON builds a minimal chat.completion body.

func ScriptedTurnSSE

func ScriptedTurnSSE(turn ScriptedTurn) string

ScriptedTurnSSE builds a minimal chat.completion.chunk SSE body ending with [DONE].

func SyncOracleChannel

func SyncOracleChannel(ch chan<- []byte) func([]byte)

SyncOracleChannel returns an OnRequestBody hook that sends body clones to ch. The HTTP handler must not call testing.T; consumers drain ch from the test goroutine. ch should be buffered for the expected turn count so the handler does not block forever.

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 401 or 429, returns that status with provider-shaped JSON instead of success.
	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. See Request / Response.
	Responder Responder
	// NonStreamJSON overrides the JSON body for non-streaming responses. When empty, a
	// minimal chat.completion is returned.
	NonStreamJSON string
	// StreamSSE overrides the full SSE payload for streaming responses. When empty, a
	// minimal chat.completion.chunk stream ending with [DONE] 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 OracleLedger

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

OracleLedger is a concurrency-safe, precomputed per-request validator ledger. The HTTP handler must invoke Hook on a cloned body before the scripted response. It never calls testing.T and does not retain or log request payloads.

func NewOracleLedger

func NewOracleLedger(validators ...RequestValidator) *OracleLedger

NewOracleLedger builds a ledger with one validator per expected backend request, in order.

func (*OracleLedger) Count

func (l *OracleLedger) Count() int

Count returns how many backend requests were observed.

func (*OracleLedger) Err

func (l *OracleLedger) Err() error

Err returns the first content-safe validation error, if any. Intended to be read from the test goroutine after requests complete.

func (*OracleLedger) Hook

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

Hook returns an OnRequestBody callback suitable for Config.OnRequestBody.

func (*OracleLedger) Observe

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

Observe validates the next request body. Safe for concurrent handler goroutines. Records only the first error. Excess requests beyond precomputed validators are errors.

type Request

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

Request is a bounded, defensive view of one authorized chat/completions call. Body is a clone owned by the handler; responders must not assume it is retained after return, and the handler never retains responder-owned mutable buffers.

type RequestValidator

type RequestValidator func(body []byte) error

RequestValidator inspects one backend-bound request body after the proxy transform. It must return content-safe structural errors only (no payload/reasoning text).

type Responder

type Responder func(Request) Response

Responder builds a per-request Response. It must be safe for concurrent use.

func ScriptedResponder

func ScriptedResponder(turns []ScriptedTurn) Responder

ScriptedResponder returns a concurrency-safe Responder that serves ScriptedTurn bodies in order for successive authorized requests (sequence 1..N).

type Response

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

Response is a scripted HTTP response for one Request. For stream requests, SSE is written; otherwise JSON is written. Status zero means 200. Headers are optional extras (Content-Type is set by the handler).

Headers must not be mutated by the Responder (or any other goroutine) after Response is returned; the handler may snapshot them. Prefer a freshly allocated http.Header per call rather than a shared map reused across concurrent requests.

type ScriptedTurn

type ScriptedTurn struct {
	VisibleText string
	Reasoning   string
	ToolID      string
	ToolName    string
	ToolArgs    string
	FinishStop  bool // when true and no tool, finish_reason=stop
}

ScriptedTurn is one deterministic assistant response for the chat emulator.

Jump to

Keyboard shortcuts

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