openrouter

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

Documentation

Overview

Package openrouter is a reference backend emulator for OpenRouter's API surface. It serves both POST /api/v1/chat/completions and POST /api/v1/responses with JSON or SSE bodies compatible with github.com/openai/openai-go/v3, adding OpenRouter-specific header/body capture for integration test assertions.

Index

Constants

View Source
const ChatStreamWithUsageSSE = "data: {\"id\":\"gen-or-refbackend-stream\",\"object\":\"chat.completion.chunk\",\"created\":1715620000,\"model\":\"openai/gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"or-stream-ok\"},\"finish_reason\":null}]}\n\n" +
	"data: {\"id\":\"gen-or-refbackend-stream\",\"object\":\"chat.completion.chunk\",\"created\":1715620000,\"model\":\"openai/gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n" +
	"data: {\"id\":\"gen-or-refbackend-stream\",\"object\":\"chat.completion.chunk\",\"created\":1715620000,\"model\":\"openai/gpt-4o-mini\",\"choices\":[],\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":7,\"total_tokens\":10,\"cost\":0.00014}}\n\n" +
	"data: [DONE]\n\n"

ChatStreamWithUsageSSE includes a final usage chunk before [DONE].

View Source
const DefaultChatNonStreamJSON = `` /* 339-byte string literal not displayed */

DefaultChatNonStreamJSON is a minimal Chat Completions response with OpenRouter extensions.

View Source
const DefaultChatStreamSSE = "data: {\"id\":\"gen-or-refbackend-stream\",\"object\":\"chat.completion.chunk\",\"created\":1715620000,\"model\":\"openai/gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"or-stream-ok\"},\"finish_reason\":null}]}\n\n" +
	"data: [DONE]\n\n"

DefaultChatStreamSSE is a minimal streaming Chat Completions response.

View Source
const DefaultResponsesNonStreamJSON = `` /* 422-byte string literal not displayed */

DefaultResponsesNonStreamJSON is a minimal Responses API completed response.

View Source
const DefaultResponsesStreamSSE = "event: response.completed\n" +
	"data: {\"type\":\"response.completed\",\"sequence_number\":1,\"response\":{\"id\":\"resp_or_refbackend_stream\",\"object\":\"response\",\"created_at\":1715620000,\"status\":\"completed\",\"model\":\"openai/gpt-4o-mini\",\"output\":[{\"type\":\"message\",\"id\":\"m1\",\"status\":\"completed\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"or-stream-ok\"}]}],\"usage\":{\"input_tokens\":3,\"output_tokens\":7,\"total_tokens\":10}}}\n\n" +
	"data: [DONE]\n\n"

DefaultResponsesStreamSSE is a minimal Responses SSE stream.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(cfg Config) http.Handler

NewHandler returns an http.Handler that emulates both OpenRouter endpoints: POST /api/v1/chat/completions and POST /api/v1/responses.

Types

type Config

type Config struct {
	AllowMissingBearer     bool
	OnAuthorizedCredential func(secret string)
	ForcedHTTPStatus       int
	ForcedRetryAfter       string
	ForcedErrorJSON        string
	OnRequestBody          func(body []byte)
	OnRequestHeaders       func(h http.Header)

	// Chat completions overrides.
	ChatNonStreamJSON string
	ChatStreamSSE     string

	// Responses overrides.
	ResponsesNonStreamJSON string
	ResponsesStreamSSE     string
}

Config tunes the emulator handler.

Jump to

Keyboard shortcuts

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