responses

package
v0.16.21 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package responses provides OpenAI Responses API transformer and compact mode.

Index

Constants

View Source
const (
	ResponsesInputTypeReasoning = "reasoning"
	ResponsesInputTypeMessage   = "message"
)

Reasoning item types that must carry required multi-turn content fields (summary / content / encrypted_content) when replayed into input.

View Source
const PreviousResponseIDField = "previous_response_id"

PreviousResponseIDField is the OpenAI Responses continuity field name.

Variables

This section is empty.

Functions

func EnsureCompactResponsesJSONAcceptHeader

func EnsureCompactResponsesJSONAcceptHeader(headers map[string]string, sitePlatform string) map[string]string

EnsureCompactResponsesJSONAcceptHeader forces accept: application/json for codex/sub2api.

func HasPreviousResponseID

func HasPreviousResponseID(body map[string]any) bool

HasPreviousResponseID reports whether body carries a non-empty id.

func HasReasoningInputItems

func HasReasoningInputItems(body map[string]any) bool

HasReasoningInputItems reports whether body.input contains any reasoning items.

func Inbound

func Inbound(body any) (map[string]any, error)

Inbound parses a responses request body.

func IsUnsupportedPreviousResponseIDError

func IsUnsupportedPreviousResponseIDError(rawErrText string) bool

IsUnsupportedPreviousResponseIDError reports whether an upstream error text indicates rejection of previous_response_id (for retry/strip helpers).

func SanitizeCompactResponsesRequestBody

func SanitizeCompactResponsesRequestBody(body map[string]any, sitePlatform string) map[string]any

Input items (including multi-turn reasoning with encrypted_content / summary / content) are preserved verbatim — compact must not drop required reasoning content needed for Hermes/Codex second-turn replay.

func SanitizeResponsesInputItems

func SanitizeResponsesInputItems(input any) (any, error)

SanitizeResponsesInputItems normalizes the Responses `input` field for multi-turn Hermes/Codex traffic:

1. Reasoning items keep encrypted_content / summary (never dropped). 2. Reasoning items always expose a top-level `content` key derived from summary text (or "" when only encrypted_content is present) so strict OpenAI-compatible gateways that validate input[n].content do not 400. 3. Message items keep empty content as "" rather than omitting the key. 4. Returns ReasoningInputError when a reasoning item has no summary text, no content, and no encrypted_content.

String / non-array inputs are returned unchanged.

func ShouldFallbackCompactResponsesToResponses

func ShouldFallbackCompactResponsesToResponses(status int, rawErrText, requestPath string) bool

ShouldFallbackCompactResponsesToResponses checks error conditions for compact fallback.

func ShouldForceResponsesUpstreamStream

func ShouldForceResponsesUpstreamStream(sitePlatform string, isCompactRequest bool) bool

ShouldForceResponsesUpstreamStream returns true when stream must be forced for upstream.

func ShouldStripCompactResponsesStore

func ShouldStripCompactResponsesStore(sitePlatform string) bool

ShouldStripCompactResponsesStore returns true for codex/sub2api platforms.

func StripPreviousResponseID

func StripPreviousResponseID(body map[string]any) map[string]any

StripPreviousResponseID removes previous_response_id from a body copy.

func SupportsResponsesPreviousResponseID

func SupportsResponsesPreviousResponseID(sitePlatform string) bool

Chat and Messages never support the field (strip).

Types

type ContinuityAction

type ContinuityAction string

ContinuityAction describes how previous_response_id should be handled for an upstream attempt.

const (
	// ContinuityForward keeps previous_response_id on the upstream body.
	// Use when the target protocol is Responses and the platform is known to
	// accept continuity ids.
	ContinuityForward ContinuityAction = "forward"

	// ContinuityStrip removes previous_response_id without failing the request.
	// Use for chat/messages fallback, compact paths that cannot continue a
	// stored response, or Responses-shaped platforms that reject the field.
	ContinuityStrip ContinuityAction = "strip"

	// ContinuityReject returns a clear client error instead of forwarding a
	// body that would produce an opaque upstream 400.
	// Reserved for cases where the client requires continuity and no safe
	// strip/forward path exists.
	ContinuityReject ContinuityAction = "reject"
)

type ContinuityDecision

type ContinuityDecision struct {
	Action ContinuityAction
	// Reason is a short machine-oriented explanation for logs/tests/docs.
	Reason string
	// ClientMessage is a clear error message when Action == ContinuityReject.
	ClientMessage string
}

ContinuityDecision is the resolved action for previous_response_id.

func ApplyPreviousResponseIDPolicy

func ApplyPreviousResponseIDPolicy(body map[string]any, input ContinuityPolicyInput) (map[string]any, ContinuityDecision, error)

ApplyPreviousResponseIDPolicy mutates a shallow copy of body according to the continuity policy. Returns the next body, the decision, and a non-nil error only when Action == ContinuityReject and the field was present.

func ResolvePreviousResponseIDPolicy

func ResolvePreviousResponseIDPolicy(input ContinuityPolicyInput) ContinuityDecision

ResolvePreviousResponseIDPolicy decides forward / strip / reject.

func SanitizeResponsesRequestBody

func SanitizeResponsesRequestBody(body map[string]any, input ContinuityPolicyInput) (map[string]any, ContinuityDecision, error)

Reasoning input sanitization runs for every protocol: chat/messages fallback still benefits from explicit missing-content errors and content preservation before any later protocol conversion.

type ContinuityError

type ContinuityError struct {
	Message string
	Reason  string
}

ContinuityError is a clear client-facing validation error for continuity policy rejects (avoids opaque upstream 400 "Unsupported parameter").

func (*ContinuityError) Error

func (e *ContinuityError) Error() string

type ContinuityPolicyInput

type ContinuityPolicyInput struct {
	// SitePlatform is the selected site platform id (e.g. "openai", "codex").
	SitePlatform string
	// Protocol is the target upstream protocol for this attempt.
	Protocol UpstreamProtocol
	// UpstreamPath is optional; used when Protocol is empty to infer protocol.
	UpstreamPath string
	// IsCompactRequest is true for /v1/responses/compact (and aliases).
	IsCompactRequest bool
	// RequireContinuity forces ContinuityReject when the field cannot be
	// forwarded safely. Default false → strip instead of reject.
	RequireContinuity bool
}

ContinuityPolicyInput selects how previous_response_id is handled.

type ReasoningInputError

type ReasoningInputError struct {
	Message string
	Index   int
	Reason  string
}

ReasoningInputError is a clear client-facing validation error when a multi-turn reasoning item omits every required content field. Avoids opaque upstream 400 "Missing required parameter: 'input[n].content'".

func (*ReasoningInputError) Error

func (e *ReasoningInputError) Error() string

type UpstreamProtocol

type UpstreamProtocol string

UpstreamProtocol is the chat-family protocol used for one upstream attempt.

const (
	ProtocolResponses UpstreamProtocol = "responses"
	ProtocolChat      UpstreamProtocol = "chat"
	ProtocolMessages  UpstreamProtocol = "messages"
	ProtocolUnknown   UpstreamProtocol = "unknown"
)

func NormalizeUpstreamProtocol

func NormalizeUpstreamProtocol(protocol UpstreamProtocol, upstreamPath string) UpstreamProtocol

NormalizeUpstreamProtocol maps free-form protocol/path hints to a protocol.

Jump to

Keyboard shortcuts

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