Documentation
¶
Overview ¶
Package wire implements the OpenCharUI relay wire format: the outer frame (relay-visible) and the inner frame (carried inside decrypted channel-0x01 payloads). See spec/PROTOCOL.md — this package is a direct transcription of §2 and §6 and must be kept in sync with it.
Index ¶
- Constants
- Variables
- func DecodeErrorCode(err error) string
- func EncodeInner(dst []byte, f InnerFrame) ([]byte, error)
- func EncodeOuter(channel Channel, payload []byte) []byte
- func IsClientInitiated(streamID uint32) bool
- func PutUint24(b []byte, v uint32)
- func PutUint32(b []byte, v uint32)
- func PutUint64(b []byte, v uint64)
- func Uint24(b []byte) uint32
- func Uint32(b []byte) uint32
- func Uint64(b []byte) uint64
- type Channel
- type ControlType
- type ErrorCode
- type ErrorMsg
- type GoingAway
- type Hello
- type HelloOK
- type InnerFrame
- type InnerType
- type Mode
- type OuterHeader
- type PeerStatus
- type Rekey
- type Role
Constants ¶
const ( CloseGoingAway = 1001 CloseBadFrame = 4400 CloseNonceViolation = 4401 CloseAgentOffline = 4404 CloseWriteTimeout = 4408 CloseDisplaced = 4409 CloseAgentGone = 4410 CloseFrameTooLarge = 4413 CloseRateLimited = 4429 CloseCapacity = 4503 )
Close codes (spec §8). WebSocket close codes in the 4000-4999 range are reserved for private use per RFC 6455 §7.4.2.
const MaxInnerPayload = 1<<24 - 1
MaxInnerPayload is the largest payload representable in the 3-byte length field (spec §6): 2^24 - 1.
Variables ¶
var ( ErrFrameTooShort = errors.New("wire: outer frame too short") ErrReservedFlagsSet = errors.New("wire: reserved flag bits set") ErrConnIDNotV1 = errors.New("wire: conn_id present but not supported in v1") )
var ( ErrInnerFrameTooShort = errors.New("wire: inner frame header truncated") ErrInnerPayloadTooLong = errors.New("wire: inner payload exceeds 24-bit length") ErrInnerTruncated = errors.New("wire: inner frame payload truncated") ErrInnerReservedType = errors.New("wire: reserved inner frame type used in v1") )
Functions ¶
func DecodeErrorCode ¶
DecodeErrorCode maps a wire package decode/encode error to the stable string used in test vectors' "expect_error" field — see the parallel proto.ErrorCode and the same rationale: a plain string is what Rust/ TypeScript can compare against, Go error identity is not. (Named DecodeErrorCode, not ErrorCode, because ErrorCode is already the control-channel message type defined in control.go.)
func EncodeInner ¶
func EncodeInner(dst []byte, f InnerFrame) ([]byte, error)
EncodeInner appends the wire encoding of f to dst and returns the extended slice, so callers can concatenate multiple frames into one ciphertext payload cheaply (spec §6 batching).
func EncodeOuter ¶
EncodeOuter builds a v1 outer frame (no conn_id — that field does not exist yet). Channel and payload are caller-supplied; flags is always 0.
func IsClientInitiated ¶
IsClientInitiated reports whether a stream_id was allocated by the web client (odd, per spec §6). Agent-initiated (even) streams are reserved for a future push-style extension and unused in v1.
func PutUint32 ¶
PutUint32 / Uint32: big-endian, re-exported for the same reason as PutUint64/Uint64 in frame.go.
Types ¶
type Channel ¶
type Channel byte
Channel identifies the outer-frame payload kind (spec §2).
const ( ChannelControl Channel = 0x00 ChannelCiphertext Channel = 0x01 ChannelHandshake Channel = 0x02 // ChannelPlain carries inner frames (§6) with no AEAD wrapping, and // exists solely for the OpenAI-compatible HTTP endpoint (§11): there, // the caller is an arbitrary third-party client with no PSK, so the // relay itself originates requests and necessarily sees plaintext. // It is only ever valid on a connection that declared mode:"http" in // its hello — an E2E session (0x01) must never accept or emit it, and // the server rejects it on any other connection with close 4400. ChannelPlain Channel = 0x03 )
type ControlType ¶
type ControlType struct {
T string `json:"t"`
}
ControlType peeks at just the `t` field, so a reader can dispatch a control message without committing to a concrete shape first.
type ErrorCode ¶
type ErrorCode string
ErrorCode enumerates the control-channel error codes (spec §3).
type GoingAway ¶
type GoingAway struct {
T string `json:"t"` // "going_away"
RetryAfterMS int `json:"retry_after_ms"`
}
GoingAway announces a graceful relay shutdown (spec §3, §8). Clients treat this close as "reconnect after RetryAfterMS with no backoff escalation" — distinct from an error close.
func NewGoingAway ¶
type Hello ¶
type Hello struct {
T string `json:"t"` // "hello"
V int `json:"v"`
Role Role `json:"role"`
Pair string `json:"pair"` // base64url, no padding, 16 raw bytes
Token string `json:"token"` // reserved, "" in v1
// Mode selects which agent protocol this connection speaks (§11).
// "" and ModeE2E are the ordinary PSK-handshake pairing that serves
// the web PWA; ModeHTTP is the OpenAI-compatible endpoint alone;
// ModeDual is both at once over a single socket, which is what amallo
// opens. Clients never set this.
Mode Mode `json:"mode,omitempty"`
// TokenHash is the base64url SHA-256 of the API key the agent is
// currently accepting, sent with ModeHTTP and ModeDual. The relay
// indexes the hash and never holds a usable key at rest — see §11.2.
TokenHash string `json:"token_hash,omitempty"`
}
Hello is sent by both agent and client immediately after the WebSocket upgrade. Token is reserved for future subscription gating (spec §9) and MUST be "" in v1.
type HelloOK ¶
type HelloOK struct {
T string `json:"t"` // "hello_ok"
Conn string `json:"conn"`
PingMS int `json:"ping_ms"`
}
HelloOK is the relay's acknowledgement of a valid Hello.
type InnerFrame ¶
InnerFrame is one decoded [type][stream_id][len][payload] record.
func DecodeInnerAll ¶
func DecodeInnerAll(buf []byte) ([]InnerFrame, error)
DecodeInnerAll parses every inner frame concatenated in buf. It is used on both sides of a session, since a single decrypted ciphertext payload may carry more than one inner frame (opportunistic batching — see the design notes in the plan; batching is a sender-side optimization with no wire-format footprint beyond "frames may be concatenated").
type InnerType ¶
type InnerType byte
InnerType identifies an inner frame (spec §6). Carried inside decrypted channel-0x01 payloads only — never seen by the relay itself.
const ( InnerReq InnerType = 0x01 InnerReqBody InnerType = 0x02 InnerReqEnd InnerType = 0x03 InnerResp InnerType = 0x04 InnerRespBody InnerType = 0x05 InnerRespEnd InnerType = 0x06 InnerCancel InnerType = 0x07 InnerError InnerType = 0x08 // Reserved — MUST NOT be sent in v1 (spec §6, §9). InnerWindowUpdate InnerType = 0x09 InnerPing InnerType = 0x0A InnerPong InnerType = 0x0B )
type Mode ¶
type Mode string
Mode distinguishes the agent connection protocols (§11).
const ( ModeE2E Mode = "e2e" ModeHTTP Mode = "http" // ModeDual carries both lanes on one socket: sealed session traffic on // 0x01/0x02 and the OpenAI endpoint's plaintext on 0x03. The agent is // registered in both the pair map and the token-hash map, and the two // lanes keep their own rate budgets — see Server.serveDualAgent. ModeDual Mode = "dual" )
func (Mode) Normalized ¶
Normalized reports the effective mode, treating an absent field as the E2E default so pre-§11 agents keep working unchanged.
func (Mode) WantsHTTPLane ¶
WantsHTTPLane reports whether this mode asks for OpenAI-endpoint routing, i.e. whether the hello's TokenHash is meaningful.
type OuterHeader ¶
type OuterHeader struct {
Channel Channel
Flags byte
// HeaderBytes is the exact byte sequence of channel+flags(+conn_id) as
// it appeared on the wire — this is what goes into the AAD, not a
// re-serialization, so a re-serialization bug can never silently
// change what was authenticated.
HeaderBytes []byte
}
OuterHeader is the parsed [channel][flags]([conn_id]) prefix of an outer frame, exactly as transmitted. Session.Seal/Open uses HeaderBytes as part of the AEAD associated data (spec §5).
func ParseOuter ¶
func ParseOuter(msg []byte) (OuterHeader, []byte, error)
ParseOuter splits a raw WebSocket binary message into its header and payload. It validates the reserved-bits rule (spec §2) but does not interpret the payload — that is the caller's job, based on Channel.
type PeerStatus ¶
type PeerStatus struct {
T string `json:"t"` // "peer_online" | "peer_offline"
}
PeerStatus notifies a connection that its peer became reachable or unreachable. This is a required trigger for the E2E handshake (spec §4.6 / §6 of the design), not cosmetic UI state.
func PeerOffline ¶
func PeerOffline() PeerStatus
func PeerOnline ¶
func PeerOnline() PeerStatus
type Rekey ¶
Rekey is the one control message an agent may send after its hello (§11.3). It republishes the API-key hash the agent is now accepting so the relay can re-index the connection in place, without the reconnect that publishing a new hash in a fresh hello would otherwise require.
An empty TokenHash removes the connection from the token index entirely — that is how switching the OpenAI endpoint off stops resolving keys without disturbing the paired browser session sharing the same socket.