Welcome to the Bridge project! If you've ever wondered how a terminal application (like Claude Code) can talk to a web interface in real-time, you are in the right place.
We begin our journey with the Remote Bridge Core, often called "The Brain."
Imagine you want to control your computer from your phone. You need a central system that:
In the Bridge project, this is the Remote Bridge Core. It is the central controller for the "Env-less" (v2) connection.
Old vs. New: In older versions (v1), the system acted like a mailman, checking a mailbox every few seconds ("polling") to see if there was work. The new Env-less Core acts like a telephone operator, establishing a direct, streaming line that stays open.
Let's say a user starts a session in their terminal. They want this session to appear on a web dashboard so they can monitor it remotely.
The Goal: Initialize a session, get a secure ID, and connect the wires.
Before we look at code, let's understand the three pillars of the Core:
The main entry point is a function called initEnvLessBridgeCore. It takes configuration (like your API URL and OAuth token) and returns a set of controls.
You provide the basics: "Who am I?" (Auth Token) and "Where is the server?" (Base URL).
You get a Handle. This is like a remote control with buttons like writeMessages or teardown.
// Ideally, this is how you start the brain
const bridgeHandle = await initEnvLessBridgeCore({
baseUrl: "https://api.anthropic.com",
getAccessToken: () => "my-secret-oauth-token",
title: "My Terminal Session",
// What to do when the server sends a message
onInboundMessage: (msg) => console.log("Received:", msg)
});
// Now we can use the handle!
bridgeHandle.writeMessages([{ role: "user", content: "Hello!" }]);
What happens internally when we call that function? It follows a strict "Handshake" protocol.
Let's look at how remoteBridgeCore.ts implements this logic. We will break it down into the 4 main steps handled by the function initEnvLessBridgeCore.
First, we need to register the session with the server. We don't need a complex environment setup (hence "Env-less"), just a valid OAuth token.
// remoteBridgeCore.ts
// 1. Create session (POST /v1/code/sessions)
const createdSessionId = await withRetry(
() => createCodeSession(baseUrl, accessToken, title, timeout, tags),
'createCodeSession',
cfg,
);
if (!createdSessionId) return null; // Failed to start
Explanation: We ask the server to create a "Code Session". If successful, we get an ID (e.g., cse_xyz). If this fails, the bridge cannot start.
Now that we have a Session ID, we need the specific credentials to join it as a "worker" (the entity that executes code).
// remoteBridgeCore.ts
// 2. Fetch bridge credentials (POST /bridge)
const credentials = await withRetry(
() => fetchRemoteCredentials(sessionId, baseUrl, token, timeout),
'fetchRemoteCredentials',
cfg,
);
// Returns: { worker_jwt, expires_in, api_base_url, worker_epoch }
Explanation: The fetchRemoteCredentials function exchanges our high-level OAuth token for a specific Worker JWT. This JWT is short-lived and safer to use for the continuous connection.
The Brain doesn't move the data itself; it hires a specialist. It initializes the Transport layer using the credentials it just fetched.
// remoteBridgeCore.ts
// 3. Build v2 transport
transport = await createV2ReplTransport({
sessionUrl: buildCCRv2SdkUrl(credentials.api_base_url, sessionId),
ingressToken: credentials.worker_jwt,
sessionId,
// ... other config
});
transport.connect(); // Turn it on!
Explanation: Here, the Core hands off the Session ID and JWT to the Transport layer. You'll learn exactly how this createV2ReplTransport works in Unified Transport Layer (The "Pipe").
Security tokens expire. The Brain is smartβit sets an alarm to wake up before the token dies to get a new one.
// remoteBridgeCore.ts
// 5. JWT refresh scheduler
const refresh = createTokenRefreshScheduler({
refreshBufferMs: cfg.token_refresh_buffer_ms, // e.g., 5 minutes before expiry
onRefresh: async (sid, oauthToken) => {
// 1. Get new credentials from server
// 2. Rebuild transport with new keys
await rebuildTransport(freshCreds, 'proactive_refresh');
}
});
refresh.scheduleFromExpiresIn(sessionId, credentials.expires_in);
Explanation: This acts like a heartbeat. If the session lasts hours, the Brain ensures the connection never drops due to expired security badges. It coordinates with Authentication & Security (The "Keycard") to handle 401 errors if things go wrong.
When the user types exit, the Brain performs a "Graceful Shutdown." It doesn't just cut the cord; it archives the session so history is preserved.
// remoteBridgeCore.ts
async function teardown() {
transport.reportState('idle'); // Tell server we are done
// Save the history to the server
await archiveSession(sessionId, baseUrl, token, orgUUID, timeout);
transport.close(); // Close the websocket/connection
}
The Remote Bridge Core is the orchestrator. It:
It acts as the container for everything else. Now that we have a running "Brain," we need to understand exactly what creates the context for this session and how different versions of the app work together.
Next Chapter: Session Lifecycle & Compatibility (The "Container")
Generated by Code IQ