00:00 / 00:00

Chatterbox

Kakashi Protocol

The カカシ・プロトコル aka. Kakashi Protocol is how Copy Ninja talks. The Chatterbox userscript and kakashi-server share it through the @laplace.live/kakashi-protocol package in the monorepo. This page is for developers who want to build their own client, server, or debugging tool; to simply use the feature, follow the Copy Ninja page

Overview

  • Transport: WebSocket text frames, each one a JSON object with a type field
  • Default address: ws://127.0.0.1:9697 (port 9696 belongs to LAPLACE Event Bridge). Clients use the loopback IP rather than localhost, which may resolve to ::1 first
  • Protocol version: currently 1. Any breaking change bumps it, and peers on different versions are refused
  • Validation: both sides validate every frame strictly; malformed frames are dropped and unknown fields are ignored
  • Limits: frames up to 16 KiB; an idle connection closes after 60 seconds, and the server sends pings to keep it alive
  • Status check: a plain HTTP GET to the server returns { "name": "kakashi-server", "version": "…", "protocol": 1 }

Connection and Handshake

  1. Origin check: the server checks the WebSocket handshake's Origin header and accepts only https://live.bilibili.com plus any origins allowed with --allow-origin; anything else gets HTTP 403. Connections without an Origin header (non-browser clients) are let through
  2. hello: once connected, the client must send hello within 10 seconds, or the socket closes with 4003
  3. Version check: the server reads hello.protocol before full validation, so a newer client gets a clear version error (4002) rather than a parse error
  4. Token check: when the server runs with --token, hello.token must match exactly (compared in constant time), or the socket closes with 4001
  5. welcome: once the hello passes, the server replies with welcome and then broadcasts the latest roster to every page

Close Codes

CodeNameTriggerChatterbox Behavior
4001UNAUTHORIZEDWrong tokenStops reconnecting until settings change
4002PROTOCOL_MISMATCHProtocol versions differStops reconnecting until settings change
4003BAD_HELLOFirst frame isn't a valid hello, or 10 s timeoutReconnects

On any other disconnect, Chatterbox reconnects automatically, starting at 1 second and doubling each time up to 30 seconds

Roles and Master Election

  • Each connection declares its role in hello / presence: master or mirror
  • The server folds all pages with the same uid into one member, whose role in the roster is one of:
RoleDescription
masterThe active master account; there is only ever one
standbyClaims master while another account holds the seat, so it waits
mirrorA mirror account
  • With no master, the earliest-connected page among those claiming master wins, and its account becomes the master
  • When every page of the master account disconnects (a page reload, for example), its seat is held for 15 seconds. Reconnecting in that window keeps the seat; after it, a standby account takes over
  • When the master account is still online but switches to mirror, the seat is released at once

Action Routing

  1. The master page sends one action for each danmaku segment bilibili's send API reports as successful, so a long message split into segments is sent segment by segment. An action from any other account gets an error back and is not relayed
  2. The server assigns each action an eventId (a UUID). For every mirror account, it picks one of that account's pages in the action.roomId live room to deliver to: a visible page first, then the most recently active one (a hello, a hidden-to-visible switch, or a room change all count as activity)
  3. When a mirror account has no page in that room, the server replies skipped ("not in the same live room") to the master on its behalf
  4. The master's own pages and standby accounts never receive actions
  5. After handling it, the mirror sends a result with the same eventId, which the server forwards to the master page that broadcast the action. Results arriving more than 5 minutes later are no longer forwarded
StatusMeaning
sentCopied successfully
failedAttempted, but bilibili rejected it
droppedIgnored at random by decay
skippedNot attempted — for example the source isn't followed, wrong room, expired, paused, or emote unavailable

Action Sources

DanmakuAction.source names the feature the master sent the danmaku through, and mirrors decide whether to copy based on it:

sourceFeature
manualNormal send in the "Send" tab
loopAuto Send
repeatThe "+1" button next to a danmaku
emoteEmote picker
memeMemes
blendAuto Blend
aiAI Blend
sttSimultaneous Interpretation

Copies a mirror sends are tagged copy inside the client, and sends relayed from the LAPLACE Chat dashboard are tagged laplace-chat; neither is ever broadcast

Room Following

  • roster.masterRoomId is the live room of the master account's most recently active page
  • For each mirror account with no page in that room, the server picks one of its pages that has follow: true and a resolved room, and sends it follow
  • follow is re-sent on every connection or presence update; a client already waiting to move to that same room ignores the repeat
  • On follow, the client re-checks for itself (following on, not paused, still the master's room) and moves after a random 2 to 5 second delay

Message Reference

Client → Server

hello

Prop

Type

presence

Prop

Type

action

Prop

Type

result

Prop

Type

Server → Client

welcome

Prop

Type

roster

Prop

Type

Each entry in members:

Prop

Type

action

Prop

Type

result

Prop

Type

follow

Prop

Type

error

Prop

Type

DanmakuAction

Prop

Type

Example

The master sends a danmaku, the server delivers it to a mirror, and the mirror reports back:

// Master → server
{ "type": "action", "action": { "kind": "danmaku", "roomId": 21452505, "text": "awsl", "source": "manual" } }

// Server → mirror
{
  "type": "action",
  "eventId": "6f1c2a4e-8d3b-4f5a-9c7e-1b2d3e4f5a6b",
  "from": { "uid": "1234567", "uname": "MasterAccount" },
  "action": { "kind": "danmaku", "roomId": 21452505, "text": "awsl", "source": "manual" }
}

// Mirror → server
{ "type": "result", "eventId": "6f1c2a4e-8d3b-4f5a-9c7e-1b2d3e4f5a6b", "status": "sent" }

// Server → master
{ "type": "result", "eventId": "6f1c2a4e-8d3b-4f5a-9c7e-1b2d3e4f5a6b", "uid": "7654321", "uname": "MirrorAccount", "status": "sent" }

Last updated on November 29, 2026

Tech otakus destroy the world