handleEnvelope
handleEnvelope(
engine,raw):Promise<ServerEnvelope|null>
Defined in: packages/engine/src/lib/envelope-handler.ts:116
The single dispatch entry point every WebSocket engine host shares: decode a
raw client frame, route it to the Engine, and map the outcome to the
ServerEnvelope the host should send back (or null when the frame
warrants no reply).
Centralizing this keeps every host from re-deriving the decode → dispatch → error-mapping logic (and drifting on error correlation). The mapping, by frame type:
- decode failure → an
engine.errorcarrying the EnvelopeError message and itskindascode. Correlation ids are recovered from the raw frame when present so the client’s pending request is rejected rather than left to time out. chart_request→engine.handle; an engineErrmaps toengine.error(the import(‘./errors.js’).EngineError_tagascode), anOkchart_responsetoengine.maelstrom.response, any otherOkoutput toengine.errorwithcode: 'UnexpectedOutput'(a contract violation, not a case that should strand the client silently).assistant_message→engine.handle, then alwaysnull. This legacy fire-and-forget arm emits no result or error frame, including when Engine returns or throws a history failure.client.assistant_message.acknowledged→ the same assistant injection semantics throughengine.handle; anOkassistant_message_resultmaps to a correlatedengine.assistant_message.result. Unexpected output, Engine errors, and thrown failures map to a correlatedengine.error.client.history.get→engine.getHistory;Ok→engine.history.response,Err→engine.error.
Never rejects. A thrown exception (an engine contract violation) is caught
and mapped to an engine.error correlated to the in-scope frame, with no
code.
Parameters
Section titled “Parameters”engine
Section titled “engine”unknown
Returns
Section titled “Returns”Promise<ServerEnvelope | null>
Example
Section titled “Example”// WebSocket host message loopws.on('message', async (raw) => { const reply = await handleEnvelope(engine, raw); if (reply) ws.send(encodeEnvelope(reply));});