Documentation
¶
Overview ¶
Package relaycontract defines the wire protocol spoken between `tap relay` and Tapper Hub. A relay dials Hub, registers the models it can serve, and then answers inference requests Hub pushes down the same connection.
Every frame is an Envelope. Payloads are decoded strictly: an unknown field is an error rather than something forwarded to a provider.
Index ¶
- Constants
- func CanonicalModel(id string) string
- func SelectProtocol(offered []int) int
- func ValidCanonical(name string) bool
- func ValidName(name string) bool
- type Catalog
- type CatalogBinding
- type Chunk
- type Done
- type Envelope
- type Error
- type Infer
- type Limits
- type Model
- type Register
- type Registered
- type RelayInfo
- type TranscriptionRequest
- type TranscriptionResult
- type Usage
Constants ¶
const ( TypeRegister = "register" TypeRegistered = "registered" TypeCatalog = "catalog" TypePing = "ping" TypePong = "pong" TypeInfer = "infer" TypeChunk = "chunk" TypeDone = "done" TypeError = "error" TypeCancel = "cancel" )
Frame types.
const ( CapabilityChat = "chat" CapabilityStream = "stream" CapabilityTranscription = "transcription" CapabilityEmbeddings = "embeddings" // CapabilityTools: the model can call tools (OpenAI function calling). CapabilityTools = "tools" // CapabilityVision: the model accepts image input. CapabilityVision = "vision" )
Model capabilities a relay advertises.
const ( // ReasoningToggle: thinking can be turned on or off, with no levels. ReasoningToggle = "toggle" // ReasoningEffort: thinking takes an effort level (low, medium, high). ReasoningEffort = "effort" )
Reasoning modes a relay advertises for a model. Empty means the model does not reason, or the relay cannot tell. A value Hub does not know reads as empty, so a newer relay never fails registration over it.
const ( CodeBadRequest = "bad_request" CodeUnsupported = "unsupported" CodeUnknownModel = "unknown_model" CodeProviderError = "provider_error" CodeOverloaded = "overloaded" CodeCancelled = "cancelled" CodeRelayDisconnected = "relay_disconnected" CodeInternal = "internal" // CodeDisconnected ends a session because the relay's owner disconnected // it from Hub. It is connection-level (no id) and the relay must not // reconnect: the owner asked for it to go away. CodeDisconnected = "disconnected" )
Error codes carried by an Error payload.
const ( MaxNameLength = 64 MaxModelIDLength = 200 MaxModels = 256 MaxConcurrentCap = 64 // MaxPriority is the lowest-ranked priority a model may carry. MaxPriority = 99 )
Limits on what a relay may advertise.
const APIOpenAIAudioTranscriptions = "openai.audio.transcriptions"
APIOpenAIAudioTranscriptions turns recorded speech into text. The request body is a TranscriptionRequest; the relay answers with exactly one chunk, a TranscriptionResult, then done. Hub only sends it to models advertising CapabilityTranscription, so a relay that predates it never sees one.
const APIOpenAIChatCompletions = "openai.chat.completions"
APIOpenAIChatCompletions is the chat inference API. The request body is an OpenAI chat completions request; each chunk is one OpenAI completion or completion-chunk object.
const APIOpenAIEmbeddings = "openai.embeddings"
APIOpenAIEmbeddings turns text into vectors. The request body is OpenAI's /embeddings JSON (input, and optionally dimensions and encoding_format); the relay answers with exactly one chunk, the provider's response, then done. Hub only sends it to models advertising CapabilityEmbeddings, so a relay that predates it never sees one.
const ConnectPath = "/api/v1/relay/connect"
ConnectPath is the Hub route a relay dials, relative to the hub base URL.
const MaxCanonicalLength = 128
MaxCanonicalLength bounds a canonical model name.
const MaxTranscriptionAudioBytes = 10 << 20
MaxTranscriptionAudioBytes bounds one recording. Base64 in a JSON frame, it stays well inside the 16 MiB frame limit both ends enforce.
const ProtocolVersion = 1
ProtocolVersion is the newest relay protocol this package speaks.
Variables ¶
This section is empty.
Functions ¶
func CanonicalModel ¶
CanonicalModel derives the provider-neutral name a model pools under from the provider's own model id, so the same weights at the same precision share one name wherever they run: Ollama's "qwen3.6:35b-mlx" and LM Studio's "qwen/qwen3.6-35b" both become "qwen3.6-35b", and "qwen3.6:35b-a3b-mxfp8" and "Qwen3.6-35B-A3B-8bit" both become "qwen3.6-35b-a3b-8bit".
It lowercases, drops any source prefix up to the last '/', drops Ollama's ":latest" tag, strips trailing format parts ("mlx", "gguf", ...), and rewrites a trailing quantization part ("q4_k_m", "nvfp4", "bf16", ...) as its bits per weight ("4bit", "16bit"), so different quantizations of one model do not pool together. A name made only of those parts keeps them.
func SelectProtocol ¶
SelectProtocol picks the highest version both sides speak, or 0.
func ValidCanonical ¶
ValidCanonical reports whether name is usable as a canonical model name: lowercase letters, digits, '.', '_' and '-', starting with a letter or digit, at most MaxCanonicalLength long.
Types ¶
type Catalog ¶
type Catalog struct {
Models []Model `json:"models"`
}
Catalog replaces the relay's advertised model list after registration.
type CatalogBinding ¶
type CatalogBinding struct {
CatalogID string `json:"catalogId"`
Provider string `json:"provider"`
Model string `json:"model"`
}
CatalogBinding maps a relay's model to the catalog id Hub assigned it.
type Chunk ¶
type Chunk struct {
Data json.RawMessage `json:"data"`
}
Chunk carries one provider response object for an in-flight request. For a non-streaming request the relay sends exactly one chunk holding the whole response.
type Done ¶
type Done struct {
Usage *Usage `json:"usage,omitempty"`
}
Done terminates a request successfully.
type Envelope ¶
type Envelope struct {
V int `json:"v"`
ID string `json:"id,omitempty"`
Type string `json:"type"`
Payload json.RawMessage `json:"payload,omitempty"`
}
Envelope is the frame shared by every message. ID correlates an infer request with its chunks and terminal frame; it is empty for connection-level frames such as register and ping.
func NewEnvelope ¶
NewEnvelope marshals payload into an envelope of the given type.
type Infer ¶
type Infer struct {
API string `json:"api"`
Provider string `json:"provider"`
Model string `json:"model"`
Stream bool `json:"stream"`
Body json.RawMessage `json:"body"`
}
Infer is one inference request pushed from Hub to a relay.
type Limits ¶
type Limits struct {
MaxConcurrent int `json:"maxConcurrent"`
}
Limits bounds how much work Hub may push at the relay at once.
type Model ¶
type Model struct {
ID string `json:"id"`
Provider string `json:"provider"`
ContextWindow int `json:"contextWindow,omitempty"`
MaxContextWindow int `json:"maxContextWindow,omitempty"`
Reasoning string `json:"reasoning,omitempty"`
Capabilities []string `json:"capabilities,omitempty"`
Priority int `json:"priority,omitempty"`
Canonical string `json:"canonical,omitempty"`
}
Model is one model a relay offers. ID is the provider's own model name. Priority is the relay owner's ranking, 1 to MaxPriority with lower preferred; 0 is unranked and sorts after every ranked model. Hub lists models and routes pool requests in that order.
ContextWindow is the window the provider actually serves the model with (for Ollama, the allocated num_ctx); MaxContextWindow is the most the model supports. Either is 0 when unknown. Reasoning is one of the Reasoning* modes, or empty.
Canonical is the provider-neutral name the model pools under, so the same weights served by different providers or relays share one Hub id. Empty means Hub derives it with CanonicalModel; a value ValidCanonical rejects is ignored the same way.
type Register ¶
type Register struct {
Relay RelayInfo `json:"relay"`
Limits Limits `json:"limits"`
Models []Model `json:"models"`
}
Register is the first frame a relay sends.
type Registered ¶
type Registered struct {
Protocol int `json:"protocol"`
Models []CatalogBinding `json:"models"`
}
Registered is Hub's answer to Register.
type RelayInfo ¶
type RelayInfo struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
Protocols []int `json:"protocols"`
}
RelayInfo identifies the relay process.
type TranscriptionRequest ¶
type TranscriptionRequest struct {
Audio []byte `json:"audio"`
MimeType string `json:"mimeType"`
Filename string `json:"filename,omitempty"`
Language string `json:"language,omitempty"`
Prompt string `json:"prompt,omitempty"`
}
TranscriptionRequest is the body of an APIOpenAIAudioTranscriptions infer. Audio is the recording's bytes (base64 on the wire).
func (*TranscriptionRequest) Validate ¶
func (t *TranscriptionRequest) Validate() error
Validate checks a transcription request.
type TranscriptionResult ¶
type TranscriptionResult struct {
Text string `json:"text"`
}
TranscriptionResult is the one chunk answering a transcription.