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 ¶
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.
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.
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).
const ( RejectedNoSpeech = "no_speech" RejectedLowConfidence = "low_confidence" )
Transcript rejection reasons. A rejected transcript never enters chat.
const ( ModelStateUnloaded = "unloaded" ModelStateLoading = "loading" ModelStateReady = "ready" )
Model states reported by health responses.
const MaxAudioBytes = 8 << 20
MaxAudioBytes bounds a complete utterance across the workers, core and browser.
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.
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 ¶
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).
type Role ¶
type Role string
Role identifies which voice capability a worker provides.
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