protocol

package
v0.1.0-alpha.53 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: GPL-3.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package protocol defines the versioned voice worker wire format (ADR 0003): NDJSON frames exchanged between the core and a worker process over stdio. Workers depend only on this wire format — the package is a leaf so worker implementations (including the stub) never import core internals.

Index

Constants

View Source
const (
	RequestHello      = "hello"
	RequestHealth     = "health"
	RequestLoad       = "load"
	RequestUnload     = "unload"
	RequestSpeak      = "speak"
	RequestTranscribe = "transcribe"
	RequestCancel     = "cancel"
	RequestShutdown   = "shutdown"
)

Request types sent core → worker. One JSON object per line on stdin.

View Source
const (
	ResponseHello      = "hello"
	ResponseHealth     = "health"
	ResponseAudioChunk = "audio_chunk"
	ResponseTranscript = "transcript"
	ResponseDone       = "done"
	ResponseCanceled   = "canceled"
	ResponseError      = "error"
)

Response types sent worker → core. One JSON object per line on stdout.

View Source
const (
	ErrorCodeProtocolMismatch  = "protocol_mismatch"
	ErrorCodeInvalidRequest    = "invalid_request"
	ErrorCodeModelNotLoaded    = "model_not_loaded"
	ErrorCodeMissingDependency = "missing_dependency"
	ErrorCodeCanceled          = "canceled"
	ErrorCodeTimeout           = "timeout"
	ErrorCodeInternal          = "internal"
)

Structured worker error codes (ADR 0003: structured error payloads).

View Source
const (
	RejectedNoSpeech      = "no_speech"
	RejectedLowConfidence = "low_confidence"
)

Transcript rejection reasons. A rejected transcript never enters chat.

View Source
const (
	ModelStateUnloaded = "unloaded"
	ModelStateLoading  = "loading"
	ModelStateReady    = "ready"
)

Model states reported by health responses.

View Source
const MaxAudioBytes = 8 << 20

MaxAudioBytes bounds a complete utterance across the workers, core and browser.

View Source
const SynthesisTimeout = 10 * time.Minute

SynthesisTimeout bounds OpenAI-compatible speech generation in both the core and HTTP adapter. Model loading has a separate, longer lifecycle deadline.

View Source
const Version = 1

Version is the worker protocol version this core speaks. Workers answering hello with a different version are rejected with a clear error, never silently degraded.

Variables

This section is empty.

Functions

func RepairWAVLengths

func RepairWAVLengths(audio []byte) []byte

RepairWAVLengths replaces streaming sentinel lengths with the actual bounded response size. Faster Qwen's streaming WAV uses 0xffffffff because its final size is unknown when the first bytes are sent; browsers reject that header after MagicHandy has already retained the complete clip.

Types

type Request

type Request struct {
	Type string `json:"type"`
	ID   string `json:"id,omitempty"`

	// hello
	ProtocolVersion int `json:"protocol_version,omitempty"`

	// cancel
	TargetID string `json:"target_id,omitempty"`

	// speak
	Text          string  `json:"text,omitempty"`
	Voice         string  `json:"voice,omitempty"`
	Instruct      string  `json:"instruct,omitempty"`
	Seed          *uint32 `json:"seed,omitempty"`
	RandomizeSeed bool    `json:"randomize_seed,omitempty"`

	// transcribe; stubs and tests inline small payloads, real providers get a
	// file/stream reference instead of megabytes of base64 on one line
	AudioB64    string `json:"audio_b64,omitempty"`
	AudioRef    string `json:"audio_ref,omitempty"`
	AudioFormat string `json:"audio_format,omitempty"`

	// stub/testing knob: minimum processing time, used to exercise
	// cancellation and timeout paths without a real model
	DelayMillis int `json:"delay_ms,omitempty"`
}

Request is the core → worker frame. Exactly one type per frame; unused fields stay empty. Workers must ignore unknown fields for forward compatibility; version negotiation happens once at hello.

type Response

type Response struct {
	Type      string `json:"type"`
	RequestID string `json:"request_id,omitempty"`

	// hello
	ProtocolVersion int      `json:"protocol_version,omitempty"`
	Provider        string   `json:"provider,omitempty"`
	ProviderVersion string   `json:"provider_version,omitempty"`
	Role            Role     `json:"role,omitempty"`
	Capabilities    []string `json:"capabilities,omitempty"`

	// health
	ModelState string `json:"model_state,omitempty"`
	QueueDepth int    `json:"queue_depth,omitempty"`

	// audio_chunk
	Seq         int    `json:"seq,omitempty"`
	AudioB64    string `json:"audio_b64,omitempty"`
	AudioFormat string `json:"audio_format,omitempty"`

	// transcript
	Candidates []TranscriptCandidate `json:"candidates,omitempty"`
	Rejected   string                `json:"rejected,omitempty"`

	// error
	Error *WorkerError `json:"error,omitempty"`
}

Response is the worker → core frame. RequestID links it to the request it answers; hello/health/done/canceled/error are terminal for that request, audio_chunk and transcript are results (transcript is terminal, audio chunks end with done).

func (Response) Terminal

func (r Response) Terminal() bool

Terminal reports whether this response ends its request. Unary requests (hello, health, load, unload) are answered by hello/health frames; streaming requests end with done, canceled, error, or transcript.

type Role

type Role string

Role identifies which voice capability a worker provides.

const (
	// RoleTTS is a text-to-speech worker.
	RoleTTS Role = "tts"
	// RoleASR is a speech-recognition worker.
	RoleASR Role = "asr"
)

func ParseRole

func ParseRole(value string) (Role, error)

ParseRole validates a role string from the API edge.

func Roles

func Roles() []Role

Roles lists the supported worker roles in display order.

type TranscriptCandidate

type TranscriptCandidate struct {
	Text       string  `json:"text"`
	Confidence float64 `json:"confidence"`
}

TranscriptCandidate is one ASR hypothesis with its confidence.

type WorkerError

type WorkerError struct {
	Code      string `json:"code"`
	Message   string `json:"message"`
	Retryable bool   `json:"retryable,omitempty"`
}

WorkerError is the structured error payload carried by error responses.

func (*WorkerError) Error

func (e *WorkerError) Error() string

Jump to

Keyboard shortcuts

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