@ottervoice/protocol
Documentation / @ottervoice/protocol
@ottervoice/protocol
Section titled “@ottervoice/protocol”Versioned JSON wire protocol for transporting OtterVoice session events across processes and native boundaries.
Install
Section titled “Install”npm install @ottervoice/core @ottervoice/protocolimport { encodeMessage, parseMessage, serializeMessage } from '@ottervoice/protocol';
const encoded = serializeMessage(encodeMessage('asr_final', { text: 'Hello', confidence: 0.98,}));const message = parseMessage(encoded);The package exports the protocol version, supported message types, envelope types, serializers, parsers, and runtime validation helpers.
License
Section titled “License”MIT
Classes
Section titled “Classes”ProtocolError
Section titled “ProtocolError”Defined in: packages/protocol/src/index.ts:57
Thrown when JSON is malformed or fails protocol validation.
Extends
Section titled “Extends”Error
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ProtocolError(message): ProtocolError;Defined in: packages/protocol/src/index.ts:61
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
message |
string |
Human-readable validation / parse failure. |
Returns
Section titled “Returns”Overrides
Section titled “Overrides”Error.constructorProperties
Section titled “Properties”Methods
Section titled “Methods”captureStackTrace()
Section titled “captureStackTrace()”Call Signature
Section titled “Call Signature”static captureStackTrace(targetObject, constructorOpt?): void;Defined in: node_modules/.bun/@types+node@26.0.1/node_modules/@types/node/globals.d.ts:51
Creates a .stack property on targetObject, which when accessed returns
a string representing the location in the code at which
Error.captureStackTrace() was called.
const myObject = {};Error.captureStackTrace(myObject);myObject.stack; // Similar to `new Error().stack`The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}.
The optional constructorOpt argument accepts a function. If given, all frames
above constructorOpt, including constructorOpt, will be omitted from the
generated stack trace.
The constructorOpt argument is useful for hiding implementation
details of error generation from the user. For instance:
function a() { b();}
function b() { c();}
function c() { // Create an error without stack trace to avoid calculating the stack trace twice. const { stackTraceLimit } = Error; Error.stackTraceLimit = 0; const error = new Error(); Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace throw error;}
a();Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
targetObject |
object |
constructorOpt? |
Function |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”Error.captureStackTraceCall Signature
Section titled “Call Signature”static captureStackTrace(targetObject, constructorOpt?): void;Defined in: node_modules/.bun/bun-types@1.3.14/node_modules/bun-types/globals.d.ts:1042
Create .stack property on a target object
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
targetObject |
object |
constructorOpt? |
Function |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”Error.captureStackTraceisError()
Section titled “isError()”static isError(value): value is Error;Defined in: node_modules/.bun/bun-types@1.3.14/node_modules/bun-types/globals.d.ts:1037
Check if a value is an instance of Error
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
unknown |
The value to check |
Returns
Section titled “Returns”value is Error
True if the value is an instance of Error, false otherwise
Inherited from
Section titled “Inherited from”Error.isErrorprepareStackTrace()
Section titled “prepareStackTrace()”static prepareStackTrace(err, stackTraces): any;Defined in: node_modules/.bun/@types+node@26.0.1/node_modules/@types/node/globals.d.ts:55
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
err |
Error |
stackTraces |
CallSite[] |
Returns
Section titled “Returns”any
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
Inherited from
Section titled “Inherited from”Error.prepareStackTraceInterfaces
Section titled “Interfaces”ProtocolEnvelope
Section titled “ProtocolEnvelope”Defined in: packages/protocol/src/index.ts:40
Versioned JSON envelope for one session event on the wire.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
T extends ProtocolMessageType |
ProtocolMessageType |
Event name; narrows ProtocolEnvelope.payload. |
Properties
Section titled “Properties”| Property | Type | Description | Defined in |
|---|---|---|---|
payload |
VoiceSessionEventMap[T] |
Event payload matching the core session event map. | packages/protocol/src/index.ts:48 |
type |
T |
Event name (same keys as core VoiceSessionEventMap). | packages/protocol/src/index.ts:46 |
v |
1 |
Protocol schema version; must equal PROTOCOL_VERSION. | packages/protocol/src/index.ts:44 |
Type Aliases
Section titled “Type Aliases”ProtocolMessageType
Section titled “ProtocolMessageType”type ProtocolMessageType = keyof VoiceSessionEventMap;Defined in: packages/protocol/src/index.ts:15
Event name carried in ProtocolEnvelope.type (mirrors core session events).
VoiceProtocolMessage
Section titled “VoiceProtocolMessage”type VoiceProtocolMessage = { [T in ProtocolMessageType]: ProtocolEnvelope<T> }[ProtocolMessageType];Defined in: packages/protocol/src/index.ts:52
Discriminated union of all protocol envelopes.
Variables
Section titled “Variables”PROTOCOL_MESSAGE_TYPES
Section titled “PROTOCOL_MESSAGE_TYPES”const PROTOCOL_MESSAGE_TYPES: readonly ProtocolMessageType[];Defined in: packages/protocol/src/index.ts:18
Every event type that may appear on the wire, in a stable order.
PROTOCOL_VERSION
Section titled “PROTOCOL_VERSION”const PROTOCOL_VERSION: 1;Defined in: packages/protocol/src/index.ts:12
Bumped when the envelope shape or payload contracts change incompatibly.
Functions
Section titled “Functions”encodeMessage()
Section titled “encodeMessage()”function encodeMessage<T>(type, payload): ProtocolEnvelope<T>;Defined in: packages/protocol/src/index.ts:74
Build a typed envelope for an event.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends keyof VoiceSessionEventMap |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
type |
T |
Event name. |
payload |
VoiceSessionEventMap[T] |
Payload for that event. |
Returns
Section titled “Returns”Envelope ready for serializeMessage.
isProtocolMessage()
Section titled “isProtocolMessage()”function isProtocolMessage(value): value is VoiceProtocolMessage;Defined in: packages/protocol/src/index.ts:99
Structurally validate a decoded value as a ProtocolEnvelope without trusting the version. Returns a boolean type-guard.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
value |
unknown |
Returns
Section titled “Returns”value is VoiceProtocolMessage
parseMessage()
Section titled “parseMessage()”function parseMessage(raw): VoiceProtocolMessage;Defined in: packages/protocol/src/index.ts:119
Parse a JSON string into a validated ProtocolEnvelope.
Throws ProtocolError on malformed JSON, an unknown/old version, an unknown message type, or a missing payload.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
raw |
string |
JSON string from the wire. |
Returns
Section titled “Returns”A typed VoiceProtocolMessage.
serializeMessage()
Section titled “serializeMessage()”function serializeMessage(message): string;Defined in: packages/protocol/src/index.ts:87
Serialize an envelope (or any event via encodeMessage) to JSON.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
message |
VoiceProtocolMessage |
Validated VoiceProtocolMessage. |
Returns
Section titled “Returns”string
JSON string for transport.