adapter

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package adapter defines the contract every upstream provider satisfies and a registry that lets the server look adapters up by name.

Adding a new provider is a single self-contained sub-package:

package myprovider
import "github.com/1mb-dev/shim/internal/adapter"

func New(opts Opts) (adapter.Adapter, error) { ... }

cmd/shim/main.go constructs the adapter via its New, then calls adapter.Register with the returned instance — no init()-time registration, no blank-import side effects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Names

func Names() []string

Names returns the sorted list of registered adapter names. Useful for error messages when Get fails.

func Register

func Register(a Adapter)

Register adds a in the global registry. Called from cmd/shim/main.go after constructing the adapter via its New. Panics on duplicate registration so misconfiguration fails at startup, not at first request.

Types

type Adapter

type Adapter interface {
	// Name returns the lookup key used by config (e.g. "deepseek").
	Name() string

	// MapModel converts an Anthropic-style model name (which may be e.g.
	// "claude-3-5-sonnet-20240620") into the upstream's expected name.
	// Adapters handle the empty-input case themselves — typically by
	// returning a "default" model — so callers never need a separate
	// DefaultModel() probe.
	MapModel(anthropicModel string) string

	// Validate confirms the adapter has all configuration it needs to serve
	// requests. Called once at server startup; non-nil error blocks startup.
	// Replaces the prior substring-match preflight backchannel on the
	// request path.
	Validate() error

	// BuildRequest takes an already-translated OpenAI ChatCompletions body
	// and returns an http.Request bound to {BaseURL}/chat/completions with
	// auth headers set. Adapter must call req.WithContext(ctx).
	BuildRequest(ctx context.Context, body []byte) (*http.Request, error)

	// NormalizeResponse reads the upstream response and returns a canonical
	// OpenAI-shape JSON body. Used by the translator. Adapter is responsible
	// for unwrapping any provider-specific envelope.
	NormalizeResponse(resp *http.Response) ([]byte, error)
}

Adapter normalises one OpenAI-compatible upstream. The translator emits a canonical OpenAI ChatCompletions request body; the adapter wraps it in an http.Request bound to its upstream, then normalises the response back to canonical OpenAI shape so the translator can convert to Anthropic Messages.

Quirks (model-name format, header peculiarities, response-shape variances) live INSIDE the adapter. The translator stays pure.

func Get

func Get(name string) (Adapter, bool)

Get returns the adapter registered under name. The bool is false when no such adapter exists — callers must handle this loudly (T2).

Directories

Path Synopsis
Package deepseek implements the Adapter against DeepSeek's OpenAI-compatible chat-completions API.
Package deepseek implements the Adapter against DeepSeek's OpenAI-compatible chat-completions API.

Jump to

Keyboard shortcuts

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