mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 22:35:51 +00:00
feat: 支持自托管的 remote-control-server (#214)
* feat: 支持自托管的 remote-control-server (#214) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { storeListActiveEnvironments, storeUpdateEnvironment } from "../store";
|
||||
import { storeListSessions, storeUpdateSession } from "../store";
|
||||
import { config } from "../config";
|
||||
|
||||
export function startDisconnectMonitor() {
|
||||
const timeoutMs = config.disconnectTimeout * 1000;
|
||||
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
|
||||
// Check environment heartbeat timeout
|
||||
const envs = storeListActiveEnvironments();
|
||||
for (const env of envs) {
|
||||
if (env.lastPollAt && now - env.lastPollAt.getTime() > timeoutMs) {
|
||||
console.log(`[RCS] Environment ${env.id} timed out (no poll for ${Math.round((now - env.lastPollAt.getTime()) / 1000)}s)`);
|
||||
storeUpdateEnvironment(env.id, { status: "disconnected" });
|
||||
}
|
||||
}
|
||||
|
||||
// Check session timeout (2x disconnect timeout with no update)
|
||||
const sessions = storeListSessions();
|
||||
for (const session of sessions) {
|
||||
if (session.status === "running" || session.status === "idle") {
|
||||
const elapsed = now - session.updatedAt.getTime();
|
||||
if (elapsed > timeoutMs * 2) {
|
||||
console.log(`[RCS] Session ${session.id} marked inactive (no update for ${Math.round(elapsed / 1000)}s)`);
|
||||
storeUpdateSession(session.id, { status: "inactive" });
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 60_000); // Check every minute
|
||||
}
|
||||
68
packages/remote-control-server/src/services/environment.ts
Normal file
68
packages/remote-control-server/src/services/environment.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { config } from "../config";
|
||||
import {
|
||||
storeCreateEnvironment,
|
||||
storeGetEnvironment,
|
||||
storeUpdateEnvironment,
|
||||
storeListActiveEnvironments,
|
||||
storeListActiveEnvironmentsByUsername,
|
||||
} from "../store";
|
||||
import type { RegisterEnvironmentRequest, EnvironmentResponse } from "../types/api";
|
||||
import type { EnvironmentRecord } from "../store";
|
||||
|
||||
function toResponse(row: EnvironmentRecord): EnvironmentResponse {
|
||||
return {
|
||||
id: row.id,
|
||||
machine_name: row.machineName,
|
||||
directory: row.directory,
|
||||
branch: row.branch,
|
||||
status: row.status,
|
||||
username: row.username,
|
||||
last_poll_at: row.lastPollAt ? row.lastPollAt.getTime() / 1000 : null,
|
||||
};
|
||||
}
|
||||
|
||||
export function registerEnvironment(req: RegisterEnvironmentRequest & { metadata?: { worker_type?: string }; username?: string }) {
|
||||
const secret = config.apiKeys[0] || "";
|
||||
const workerType = req.worker_type || req.metadata?.worker_type;
|
||||
const record = storeCreateEnvironment({
|
||||
secret,
|
||||
machineName: req.machine_name,
|
||||
directory: req.directory,
|
||||
branch: req.branch,
|
||||
gitRepoUrl: req.git_repo_url,
|
||||
maxSessions: req.max_sessions,
|
||||
workerType,
|
||||
bridgeId: req.bridge_id,
|
||||
username: req.username,
|
||||
});
|
||||
|
||||
return { environment_id: record.id, environment_secret: record.secret, status: record.status as "active" };
|
||||
}
|
||||
|
||||
export function deregisterEnvironment(envId: string) {
|
||||
storeUpdateEnvironment(envId, { status: "deregistered" });
|
||||
}
|
||||
|
||||
export function getEnvironment(envId: string) {
|
||||
return storeGetEnvironment(envId);
|
||||
}
|
||||
|
||||
export function updatePollTime(envId: string) {
|
||||
storeUpdateEnvironment(envId, { lastPollAt: new Date() });
|
||||
}
|
||||
|
||||
export function listActiveEnvironments() {
|
||||
return storeListActiveEnvironments();
|
||||
}
|
||||
|
||||
export function listActiveEnvironmentsResponse(): EnvironmentResponse[] {
|
||||
return storeListActiveEnvironments().map(toResponse);
|
||||
}
|
||||
|
||||
export function listActiveEnvironmentsByUsername(username: string): EnvironmentResponse[] {
|
||||
return storeListActiveEnvironmentsByUsername(username).map(toResponse);
|
||||
}
|
||||
|
||||
export function reconnectEnvironment(envId: string) {
|
||||
storeUpdateEnvironment(envId, { status: "active" });
|
||||
}
|
||||
103
packages/remote-control-server/src/services/session.ts
Normal file
103
packages/remote-control-server/src/services/session.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import {
|
||||
storeCreateSession,
|
||||
storeGetSession,
|
||||
storeUpdateSession,
|
||||
storeListSessions,
|
||||
storeListSessionsByUsername,
|
||||
storeListSessionsByEnvironment,
|
||||
storeListSessionsByOwnerUuid,
|
||||
} from "../store";
|
||||
import { removeEventBus } from "../transport/event-bus";
|
||||
import type { CreateSessionRequest, CreateCodeSessionRequest, SessionResponse, SessionSummaryResponse } from "../types/api";
|
||||
|
||||
function toResponse(row: { id: string; environmentId: string | null; title: string | null; status: string; source: string; permissionMode: string | null; workerEpoch: number; username: string | null; createdAt: Date; updatedAt: Date }): SessionResponse {
|
||||
return {
|
||||
id: row.id,
|
||||
environment_id: row.environmentId,
|
||||
title: row.title,
|
||||
status: row.status,
|
||||
source: row.source,
|
||||
permission_mode: row.permissionMode,
|
||||
worker_epoch: row.workerEpoch,
|
||||
username: row.username,
|
||||
created_at: row.createdAt.getTime() / 1000,
|
||||
updated_at: row.updatedAt.getTime() / 1000,
|
||||
};
|
||||
}
|
||||
|
||||
export function createSession(req: CreateSessionRequest & { username?: string }): SessionResponse {
|
||||
const record = storeCreateSession({
|
||||
environmentId: req.environment_id,
|
||||
title: req.title,
|
||||
source: req.source,
|
||||
permissionMode: req.permission_mode,
|
||||
username: req.username,
|
||||
});
|
||||
return toResponse(record);
|
||||
}
|
||||
|
||||
export function createCodeSession(req: CreateCodeSessionRequest): SessionResponse {
|
||||
const record = storeCreateSession({
|
||||
idPrefix: "cse_",
|
||||
title: req.title,
|
||||
source: req.source,
|
||||
permissionMode: req.permission_mode,
|
||||
});
|
||||
return toResponse(record);
|
||||
}
|
||||
|
||||
export function getSession(sessionId: string): SessionResponse | null {
|
||||
const record = storeGetSession(sessionId);
|
||||
return record ? toResponse(record) : null;
|
||||
}
|
||||
|
||||
export function updateSessionTitle(sessionId: string, title: string) {
|
||||
storeUpdateSession(sessionId, { title });
|
||||
}
|
||||
|
||||
export function updateSessionStatus(sessionId: string, status: string) {
|
||||
storeUpdateSession(sessionId, { status });
|
||||
}
|
||||
|
||||
export function archiveSession(sessionId: string) {
|
||||
storeUpdateSession(sessionId, { status: "archived" });
|
||||
removeEventBus(sessionId);
|
||||
}
|
||||
|
||||
export function incrementEpoch(sessionId: string): number {
|
||||
const record = storeGetSession(sessionId);
|
||||
if (!record) throw new Error("Session not found");
|
||||
const newEpoch = record.workerEpoch + 1;
|
||||
storeUpdateSession(sessionId, { workerEpoch: newEpoch });
|
||||
return newEpoch;
|
||||
}
|
||||
|
||||
export function listSessions() {
|
||||
return storeListSessions().map(toResponse);
|
||||
}
|
||||
|
||||
function toSummaryResponse(row: { id: string; title: string | null; status: string; username: string | null; updatedAt: Date }): SessionSummaryResponse {
|
||||
return {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
status: row.status,
|
||||
username: row.username,
|
||||
updated_at: row.updatedAt.getTime() / 1000,
|
||||
};
|
||||
}
|
||||
|
||||
export function listSessionSummaries(): SessionSummaryResponse[] {
|
||||
return storeListSessions().map(toSummaryResponse);
|
||||
}
|
||||
|
||||
export function listSessionSummariesByOwnerUuid(uuid: string): SessionSummaryResponse[] {
|
||||
return storeListSessionsByOwnerUuid(uuid).map(toSummaryResponse);
|
||||
}
|
||||
|
||||
export function listSessionSummariesByUsername(username: string): SessionSummaryResponse[] {
|
||||
return storeListSessionsByUsername(username).map(toSummaryResponse);
|
||||
}
|
||||
|
||||
export function listSessionsByEnvironment(envId: string) {
|
||||
return storeListSessionsByEnvironment(envId).map(toResponse);
|
||||
}
|
||||
93
packages/remote-control-server/src/services/transport.ts
Normal file
93
packages/remote-control-server/src/services/transport.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { getEventBus } from "../transport/event-bus";
|
||||
import { v4 as uuid } from "uuid";
|
||||
|
||||
/**
|
||||
* Extract plain text from various message payload formats.
|
||||
* Handles:
|
||||
* { content: "text" }
|
||||
* { message: { role: "user", content: "text" } }
|
||||
* { message: { content: [{type:"text",text:"..."}] } }
|
||||
*/
|
||||
function extractContent(payload: unknown): string {
|
||||
if (!payload || typeof payload !== "object") {
|
||||
return typeof payload === "string" ? payload : "";
|
||||
}
|
||||
|
||||
const p = payload as Record<string, unknown>;
|
||||
|
||||
// Direct content field
|
||||
if (typeof p.content === "string" && p.content) return p.content;
|
||||
|
||||
// message.content (child process format)
|
||||
const msg = p.message;
|
||||
if (msg && typeof msg === "object") {
|
||||
const mc = (msg as Record<string, unknown>).content;
|
||||
if (typeof mc === "string") return mc;
|
||||
if (Array.isArray(mc)) {
|
||||
return mc
|
||||
.filter((b: unknown) => typeof b === "object" && b !== null && (b as Record<string, unknown>).type === "text")
|
||||
.map((b: Record<string, unknown>) => (b as Record<string, unknown>).text || "")
|
||||
.join("");
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize event payload into a flat structure with guaranteed `content` string.
|
||||
* Preserves original payload in `raw` field and keeps tool-specific fields.
|
||||
*/
|
||||
export function normalizePayload(type: string, payload: unknown): Record<string, unknown> {
|
||||
if (!payload || typeof payload !== "object") {
|
||||
return { content: typeof payload === "string" ? payload : "", raw: payload };
|
||||
}
|
||||
|
||||
const p = payload as Record<string, unknown>;
|
||||
const content = extractContent(payload);
|
||||
|
||||
const normalized: Record<string, unknown> = {
|
||||
content,
|
||||
raw: payload,
|
||||
};
|
||||
|
||||
// Preserve tool fields
|
||||
if (p.tool_name) normalized.tool_name = p.tool_name;
|
||||
if (p.name) normalized.tool_name = p.name;
|
||||
if (p.tool_input) normalized.tool_input = p.tool_input;
|
||||
if (p.input) normalized.tool_input = p.input;
|
||||
|
||||
// Preserve permission fields
|
||||
if (p.request_id) normalized.request_id = p.request_id;
|
||||
if (p.request) normalized.request = p.request;
|
||||
if (p.approved !== undefined) normalized.approved = p.approved;
|
||||
if (p.updated_input) normalized.updated_input = p.updated_input;
|
||||
|
||||
// Preserve message field for backward compat
|
||||
if (p.message) normalized.message = p.message;
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/** Publish an event to a session's bus (in-memory only) */
|
||||
export function publishSessionEvent(
|
||||
sessionId: string,
|
||||
type: string,
|
||||
payload: unknown,
|
||||
direction: "inbound" | "outbound",
|
||||
) {
|
||||
const bus = getEventBus(sessionId);
|
||||
const eventId = uuid();
|
||||
|
||||
const normalized = normalizePayload(type, payload);
|
||||
|
||||
const event = bus.publish({
|
||||
id: eventId,
|
||||
sessionId,
|
||||
type,
|
||||
payload: normalized,
|
||||
direction,
|
||||
});
|
||||
|
||||
return event;
|
||||
}
|
||||
98
packages/remote-control-server/src/services/work-dispatch.ts
Normal file
98
packages/remote-control-server/src/services/work-dispatch.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import {
|
||||
storeCreateWorkItem,
|
||||
storeGetWorkItem,
|
||||
storeGetPendingWorkItem,
|
||||
storeUpdateWorkItem,
|
||||
storeListSessionsByEnvironment,
|
||||
storeGetEnvironment,
|
||||
} from "../store";
|
||||
import { config } from "../config";
|
||||
import { getBaseUrl } from "../config";
|
||||
import type { WorkResponse } from "../types/api";
|
||||
|
||||
/** Encode work secret as base64 JSON (no JWT — just API key as token) */
|
||||
function encodeWorkSecret(): string {
|
||||
const payload = {
|
||||
version: 1,
|
||||
session_ingress_token: config.apiKeys[0] || "",
|
||||
api_base_url: getBaseUrl(),
|
||||
sources: [] as string[],
|
||||
auth: [] as string[],
|
||||
use_code_sessions: false,
|
||||
};
|
||||
return Buffer.from(JSON.stringify(payload)).toString("base64url");
|
||||
}
|
||||
|
||||
export async function createWorkItem(environmentId: string, sessionId: string): Promise<string> {
|
||||
// Validate environment exists and is active
|
||||
const env = storeGetEnvironment(environmentId);
|
||||
if (!env) {
|
||||
throw new Error(`Environment ${environmentId} not found`);
|
||||
}
|
||||
if (env.status !== "active") {
|
||||
throw new Error(`Environment ${environmentId} is not active (status: ${env.status})`);
|
||||
}
|
||||
|
||||
const secret = encodeWorkSecret();
|
||||
const record = storeCreateWorkItem({ environmentId, sessionId, secret });
|
||||
console.log(`[RCS] Work item created: ${record.id} for env=${environmentId} session=${sessionId}`);
|
||||
return record.id;
|
||||
}
|
||||
|
||||
/** Long-poll for work — blocks until work is available or timeout.
|
||||
* Returns null when no work is available, matching the CLI bridge client protocol. */
|
||||
export async function pollWork(environmentId: string, timeoutSeconds = config.pollTimeout): Promise<WorkResponse | null> {
|
||||
const deadline = Date.now() + timeoutSeconds * 1000;
|
||||
|
||||
while (Date.now() < deadline) {
|
||||
const item = storeGetPendingWorkItem(environmentId);
|
||||
|
||||
if (item) {
|
||||
storeUpdateWorkItem(item.id, { state: "dispatched" });
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
type: "work",
|
||||
environment_id: environmentId,
|
||||
state: "dispatched",
|
||||
data: {
|
||||
type: "session",
|
||||
id: item.sessionId,
|
||||
},
|
||||
secret: item.secret,
|
||||
created_at: item.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function ackWork(workId: string) {
|
||||
storeUpdateWorkItem(workId, { state: "acked" });
|
||||
}
|
||||
|
||||
export function stopWork(workId: string) {
|
||||
storeUpdateWorkItem(workId, { state: "completed" });
|
||||
}
|
||||
|
||||
export function heartbeatWork(workId: string): { lease_extended: boolean; state: string; last_heartbeat: string; ttl_seconds: number } {
|
||||
storeUpdateWorkItem(workId, {} as any); // just bump updatedAt
|
||||
const item = storeGetWorkItem(workId);
|
||||
const now = new Date();
|
||||
return {
|
||||
lease_extended: true,
|
||||
state: item?.state ?? "acked",
|
||||
last_heartbeat: now.toISOString(),
|
||||
ttl_seconds: config.heartbeatInterval * 2,
|
||||
};
|
||||
}
|
||||
|
||||
/** Reconnect: re-queue sessions associated with an environment */
|
||||
export function reconnectWorkForEnvironment(envId: string) {
|
||||
const activeSessions = storeListSessionsByEnvironment(envId).filter((s) => s.status === "idle");
|
||||
const promises = activeSessions.map((s) => createWorkItem(envId, s.id));
|
||||
return Promise.all(promises);
|
||||
}
|
||||
Reference in New Issue
Block a user