refactor(remote): extract shared registerWithCloud() helper

Registration HTTP logic was duplicated three times across listen.ts
(initial + re-register) and listen.tsx. Extracted into
listen-register.ts with proper error handling for non-JSON responses,
response shape validation, and 5 focused tests. Removes ~110 lines
of duplication.

🐛 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <noreply@letta.com>
This commit is contained in:
cpacker
2026-03-01 11:52:42 -08:00
parent b4910cd410
commit 393ab7bddf
4 changed files with 203 additions and 110 deletions

View File

@@ -11,6 +11,7 @@ import type React from "react";
import { useState } from "react";
import { getServerUrl } from "../../agent/client";
import { settingsManager } from "../../settings-manager";
import { registerWithCloud } from "../../websocket/listen-register";
import { ListenerStatusUI } from "../components/ListenerStatusUI";
/**
@@ -154,50 +155,22 @@ export async function runListenSubcommand(argv: string[]): Promise<number> {
// Register with cloud
const serverUrl = getServerUrl();
const registerUrl = `${serverUrl}/v1/environments/register`;
if (debugMode) {
console.log(`[${formatTimestamp()}] Registering with ${registerUrl}`);
console.log(
`[${formatTimestamp()}] Registering with ${serverUrl}/v1/environments/register`,
);
console.log(`[${formatTimestamp()}] deviceId: ${deviceId}`);
console.log(`[${formatTimestamp()}] connectionName: ${connectionName}`);
}
const registerResponse = await fetch(registerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
"X-Letta-Source": "letta-code",
},
body: JSON.stringify({
deviceId,
connectionName,
}),
const { connectionId, wsUrl } = await registerWithCloud({
serverUrl,
apiKey,
deviceId,
connectionName,
});
if (!registerResponse.ok) {
let errorMessage = `Registration failed (HTTP ${registerResponse.status})`;
try {
const error = (await registerResponse.json()) as { message?: string };
if (error.message) errorMessage = error.message;
} catch {
const text = await registerResponse.text().catch(() => "");
if (text) errorMessage += `: ${text.slice(0, 200)}`;
}
console.error(errorMessage);
return 1;
}
let registerBody: { connectionId: string; wsUrl: string };
try {
registerBody = (await registerResponse.json()) as typeof registerBody;
} catch {
throw new Error(
"Registration endpoint returned non-JSON response — is the server running?",
);
}
const { connectionId, wsUrl } = registerBody;
if (debugMode) {
console.log(`[${formatTimestamp()}] Registered successfully`);
console.log(`[${formatTimestamp()}] connectionId: ${connectionId}`);