protocol

package
v0.0.0-...-365d465 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderSize is type(1) + payload length(2) = 3 bytes.
	HeaderSize = 3
	// SessionIDSize is 16 bytes (UUID without dashes).
	SessionIDSize = 16
	// MaxPayloadSize is the maximum payload size (64KB - 1).
	MaxPayloadSize = 65535
)

Variables

View Source
var (
	ErrMessageTooShort  = errors.New("message too short")
	ErrPayloadTooLarge  = errors.New("payload exceeds max size")
	ErrInvalidSessionID = errors.New("session ID must be 16 bytes for terminal messages")
	ErrInvalidMsgType   = errors.New("invalid message type")
)

Functions

func Encode

func Encode(msg Message) ([]byte, error)

Encode serializes a Message into binary format:

[1 byte type][2 bytes payload length][N bytes payload]

For terminal messages (TermOutput, TermInput, Scrollback), the payload is:

[16 bytes session ID][raw terminal bytes]

For all other messages, the payload is the raw Payload bytes (typically JSON).

func MarshalPayload

func MarshalPayload(v interface{}) ([]byte, error)

MarshalPayload encodes a typed struct into JSON bytes for use in Message.Payload.

func UnmarshalPayload

func UnmarshalPayload(data []byte, v interface{}) error

UnmarshalPayload decodes JSON bytes from Message.Payload into a typed struct.

Types

type ApprovalRequest

type ApprovalRequest struct {
	ID        string `json:"id"`
	SessionID string `json:"session"`
	Prompt    string `json:"prompt"`
	Context   string `json:"context"`
	Timestamp int64  `json:"ts"`
}

ApprovalRequest is the payload for MsgApprovalReq.

type ApprovalResponse

type ApprovalResponse struct {
	ID       string `json:"id"`
	Approved bool   `json:"approved"`
}

ApprovalResponse is the payload for MsgApprovalResp.

type ErrorPayload

type ErrorPayload struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

ErrorPayload is the payload for MsgError.

type Message

type Message struct {
	Type      MsgType
	SessionID string // 16-byte UUID (no dashes) for term messages, empty for control
	Payload   []byte
}

Message is the envelope for all protocol messages.

func Decode

func Decode(data []byte) (Message, error)

Decode deserializes binary data into a Message.

type MsgType

type MsgType uint8

MsgType identifies the type of message sent over WebRTC DataChannel.

const (
	MsgTermOutput    MsgType = 0x01 // CLI -> Phone: terminal output bytes
	MsgTermInput     MsgType = 0x02 // Phone -> CLI: terminal input bytes
	MsgTermResize    MsgType = 0x03 // Phone -> CLI: resize{rows, cols}
	MsgApprovalReq   MsgType = 0x04 // CLI -> Phone: approval needed
	MsgApprovalResp  MsgType = 0x05 // Phone -> CLI: approved/denied
	MsgSessionList   MsgType = 0x06 // CLI -> Phone: list of sessions
	MsgSessionCreate MsgType = 0x07 // Phone -> CLI: create new session
	MsgSessionClose  MsgType = 0x08 // Phone -> CLI: close session
	MsgSessionSwitch MsgType = 0x09 // Phone -> CLI: switch active session
	MsgScrollback    MsgType = 0x0A // CLI -> Phone: scrollback buffer
	MsgPing          MsgType = 0x0B // bidirectional keepalive
	MsgPong          MsgType = 0x0C
	MsgError         MsgType = 0x0F // error notification
)

func (MsgType) IsTerminalMsg

func (m MsgType) IsTerminalMsg() bool

IsTerminalMsg returns true if the message carries raw terminal bytes (output or input) where Payload is raw bytes, not JSON.

type SessionCreateReq

type SessionCreateReq struct {
	Command string   `json:"cmd"`
	Args    []string `json:"args"`
}

SessionCreateReq is the payload for MsgSessionCreate.

type SessionInfo

type SessionInfo struct {
	ID      string `json:"id"`
	Command string `json:"cmd"`
	Status  string `json:"status"` // "running", "exited"
	Created int64  `json:"created"`
}

SessionInfo describes a single PTY session.

type TermResize

type TermResize struct {
	Rows uint16 `json:"rows"`
	Cols uint16 `json:"cols"`
}

TermResize is the payload for MsgTermResize.

Jump to

Keyboard shortcuts

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