Skip to content

Contract Message Protocol

Reese Hodge edited this page Nov 19, 2026 · 3 revisions

Contract: Message Protocol

This is the logical turn protocol layered on Contract-WebSocket-API. A frontend agent should implement the frame shapes from the WebSocket contract and the lifecycle below together.

Turn lifecycle

  1. Frontend sends user_message with { text, turnIndex, inputMethod }.
  2. Agent may stream repeated agent_token frames with { turnIndex, token, isFinal: false }.
  3. Agent sends one agent_turn_complete with { turnIndex, fullText, tokenCount, sessionTokenTotal, sessionTurnTotal }.
  4. Frontend replaces progressive text with fullText.
  5. Frontend synthesizes voice via Contract-Polly-TTS.
  6. Frontend updates local counters and mouth animation.

Frontend responsibilities

Moment Required behavior
session_start Select greeting from Contract-Greeting-Manifest, open WebSocket, send session_start, play pre-generated greeting immediately, show buffering until connection_ack.
user input Client-side rate-limit check, increment turn index, send user_message, set “thinking” UI, watch latency timer.
agent_token Append payload.token to current broadcast text and reset timeout.
agent_turn_complete Replace progressive text with payload.fullText, trigger Polly audio/viseme requests, sync counters from payload.
interruption Stop current audio immediately, clear/leave partial text per UX, send interrupt, then send new user_message with incremented turn index.
session_state_change Transition frontend state unconditionally to payload.newState.

Specified agent responsibilities (not verified as implemented)

Frame Required agent behavior
session_start Create/load session, bind actorId, load memory namespace /max-height/{actorId}/, initialize personality prompt, send connection_ack with agentCoreSessionId.
user_message Enforce 50 turns, 20,000 tokens, and 30 minutes; run Strands pipeline; call tools for news/weather/web search when needed; stream tokens; post-process personality/stutters; send agent_turn_complete. This is the contract, not the live HTTP behavior (#42, #45, #55).
interrupt Cancel current generation if possible and remember interruption context for the next response.
caps/rate/budget Send in-character final/error text, then session_state_change to ENDED, RATE_LIMITED, or BUDGET_CAPPED.

Tool results are not separate wire-frame types. News, weather, and web-search data are agent-internal tool results woven into ordinary agent_token and agent_turn_complete output.

Idle systems

System Trigger Limit Content source
Post-greeting nudge Visitor silent 4-10 seconds after greeting exactly one per session LLM-generated today; may become pre-recorded
Mid-session re-engagement Visitor silent 90-120 seconds during active conversation up to 2 per idle stretch Contract-Re-Engagement-Manifest static pool

The post-greeting nudge and mid-session re-engagement systems are independent; the first does not count toward the second.

Voice input protocol

Voice input is press-and-hold only. While pressed, the UI shows “ON AIR” and Web Speech API collection runs. On release, final transcript is sent as user_message with inputMethod: 'voice'. If speech recognition fails, show in-character “bad signal” copy and keep text input available.

Optional enhancements: recognition.processLocally = true where supported; contextual SpeechRecognitionPhrase biasing for Max-specific terms where the browser supports it.

Verified implementation status

packages/frontend/src/App.tsx currently sends text user_message frames through useWebSocket; useWebSocket.ts consumes connection_ack, agent_token, agent_turn_complete, session_state_change, and error frames according to the typed contract. The agent HTTP runtime in packages/agent/src/index.ts checks session caps and returns complete-text server-sent events from /invocations, not incremental tokens or the full WebSocket frame protocol. It calls neither personality-check nor stutter-injection helpers, and a fresh caller-chosen sessionId evades the cap. The WebSocket Lambda handler is the integration point that must still bridge this contract to the agent. See Contract-WebSocket-API, Component-Agent-Runtime and Feature-Max-Height-Core.

Clone this wiki locally