Files
claude-code/packages/remote-control-server/src/services/environment.ts
claude-code-best 2da6514095 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>
2026-04-09 17:40:50 +08:00

69 lines
2.1 KiB
TypeScript

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" });
}