mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-22 08:15:53 +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:
26
packages/remote-control-server/src/routes/web/auth.ts
Normal file
26
packages/remote-control-server/src/routes/web/auth.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Hono } from "hono";
|
||||
import { storeGetSession, storeBindSession } from "../../store";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
/** POST /web/bind — Bind a session to a UUID (no-login auth) */
|
||||
app.post("/bind", async (c) => {
|
||||
const body = await c.req.json();
|
||||
const sessionId = body.sessionId;
|
||||
// UUID can come from query param (api.js sends it in URL) or body
|
||||
const uuid = c.req.query("uuid") || body.uuid;
|
||||
|
||||
if (!sessionId || !uuid) {
|
||||
return c.json({ error: "sessionId and uuid are required" }, 400);
|
||||
}
|
||||
|
||||
const session = storeGetSession(sessionId);
|
||||
if (!session) {
|
||||
return c.json({ error: "Session not found" }, 404);
|
||||
}
|
||||
|
||||
storeBindSession(sessionId, uuid);
|
||||
return c.json({ ok: true, sessionId });
|
||||
});
|
||||
|
||||
export default app;
|
||||
64
packages/remote-control-server/src/routes/web/control.ts
Normal file
64
packages/remote-control-server/src/routes/web/control.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Hono } from "hono";
|
||||
import { uuidAuth } from "../../auth/middleware";
|
||||
import { getSession, updateSessionStatus } from "../../services/session";
|
||||
import { publishSessionEvent } from "../../services/transport";
|
||||
import { getEventBus } from "../../transport/event-bus";
|
||||
import { storeIsSessionOwner } from "../../store";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
function checkOwnership(c: { get: (key: string) => string | undefined }, sessionId: string) {
|
||||
const uuid = c.get("uuid");
|
||||
if (!storeIsSessionOwner(sessionId, uuid)) {
|
||||
return { error: true, session: null };
|
||||
}
|
||||
const session = getSession(sessionId);
|
||||
if (!session) {
|
||||
return { error: true, session: null };
|
||||
}
|
||||
return { error: false, session };
|
||||
}
|
||||
|
||||
/** POST /web/sessions/:id/events — Send user message to session */
|
||||
app.post("/sessions/:id/events", uuidAuth, async (c) => {
|
||||
const sessionId = c.req.param("id")!;
|
||||
const { error } = checkOwnership(c, sessionId);
|
||||
if (error) {
|
||||
return c.json({ error: { type: "forbidden", message: "Not your session" } }, 403);
|
||||
}
|
||||
|
||||
const body = await c.req.json();
|
||||
const eventType = body.type || "user";
|
||||
console.log(`[RC-DEBUG] web -> server: POST /web/sessions/${sessionId}/events type=${eventType} content=${JSON.stringify(body).slice(0, 200)}`);
|
||||
const event = publishSessionEvent(sessionId, eventType, body, "outbound");
|
||||
console.log(`[RC-DEBUG] web -> server: published outbound event id=${event.id} type=${event.type} direction=${event.direction} subscribers=${getEventBus(sessionId).subscriberCount()}`);
|
||||
return c.json({ status: "ok", event }, 200);
|
||||
});
|
||||
|
||||
/** POST /web/sessions/:id/control — Send control request (permission approval etc) */
|
||||
app.post("/sessions/:id/control", uuidAuth, async (c) => {
|
||||
const sessionId = c.req.param("id")!;
|
||||
const { error } = checkOwnership(c, sessionId);
|
||||
if (error) {
|
||||
return c.json({ error: { type: "forbidden", message: "Not your session" } }, 403);
|
||||
}
|
||||
|
||||
const body = await c.req.json();
|
||||
const event = publishSessionEvent(sessionId, body.type || "control_request", body, "outbound");
|
||||
return c.json({ status: "ok", event }, 200);
|
||||
});
|
||||
|
||||
/** POST /web/sessions/:id/interrupt — Interrupt session */
|
||||
app.post("/sessions/:id/interrupt", uuidAuth, async (c) => {
|
||||
const sessionId = c.req.param("id")!;
|
||||
const { error } = checkOwnership(c, sessionId);
|
||||
if (error) {
|
||||
return c.json({ error: { type: "forbidden", message: "Not your session" } }, 403);
|
||||
}
|
||||
|
||||
publishSessionEvent(sessionId, "interrupt", { action: "interrupt" }, "outbound");
|
||||
updateSessionStatus(sessionId, "idle");
|
||||
return c.json({ status: "ok" }, 200);
|
||||
});
|
||||
|
||||
export default app;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Hono } from "hono";
|
||||
import { uuidAuth } from "../../auth/middleware";
|
||||
import { listActiveEnvironmentsResponse } from "../../services/environment";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
/** GET /web/environments — List active environments (UUID-based, no user filtering) */
|
||||
app.get("/environments", uuidAuth, async (c) => {
|
||||
// Environments are shared across all UUIDs for now
|
||||
const envs = listActiveEnvironmentsResponse();
|
||||
return c.json(envs, 200);
|
||||
});
|
||||
|
||||
export default app;
|
||||
100
packages/remote-control-server/src/routes/web/sessions.ts
Normal file
100
packages/remote-control-server/src/routes/web/sessions.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { Hono } from "hono";
|
||||
import { uuidAuth } from "../../auth/middleware";
|
||||
import { getSession, createSession } from "../../services/session";
|
||||
import { storeListSessionsByOwnerUuid, storeIsSessionOwner, storeBindSession } from "../../store";
|
||||
import { createWorkItem } from "../../services/work-dispatch";
|
||||
import { listSessionSummariesByOwnerUuid } from "../../services/session";
|
||||
import { createSSEStream } from "../../transport/sse-writer";
|
||||
import { getEventBus } from "../../transport/event-bus";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
/** POST /web/sessions — Create a session from web UI */
|
||||
app.post("/sessions", uuidAuth, async (c) => {
|
||||
const uuid = c.get("uuid");
|
||||
const body = await c.req.json();
|
||||
const session = createSession({
|
||||
environment_id: body.environment_id || null,
|
||||
title: body.title || "New Session",
|
||||
source: "web",
|
||||
permission_mode: body.permission_mode || "default",
|
||||
});
|
||||
|
||||
// Auto-bind to creator's UUID
|
||||
storeBindSession(session.id, uuid);
|
||||
|
||||
// Dispatch work to environment if specified
|
||||
if (body.environment_id) {
|
||||
try {
|
||||
await createWorkItem(body.environment_id, session.id);
|
||||
} catch (err) {
|
||||
console.error(`[RCS] Failed to create work item: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
return c.json(session, 200);
|
||||
});
|
||||
|
||||
/** GET /web/sessions — List sessions owned by the requesting UUID */
|
||||
app.get("/sessions", uuidAuth, async (c) => {
|
||||
const uuid = c.get("uuid");
|
||||
const sessions = storeListSessionsByOwnerUuid(uuid);
|
||||
return c.json(sessions, 200);
|
||||
});
|
||||
|
||||
/** GET /web/sessions/all — List sessions owned by the requesting UUID (unowned sessions excluded) */
|
||||
app.get("/sessions/all", uuidAuth, async (c) => {
|
||||
const uuid = c.get("uuid");
|
||||
const sessions = listSessionSummariesByOwnerUuid(uuid);
|
||||
return c.json(sessions, 200);
|
||||
});
|
||||
|
||||
/** GET /web/sessions/:id — Session detail */
|
||||
app.get("/sessions/:id", uuidAuth, async (c) => {
|
||||
const uuid = c.get("uuid");
|
||||
const sessionId = c.req.param("id")!;
|
||||
if (!storeIsSessionOwner(sessionId, uuid)) {
|
||||
return c.json({ error: { type: "forbidden", message: "Not your session" } }, 403);
|
||||
}
|
||||
const session = getSession(sessionId);
|
||||
if (!session) {
|
||||
return c.json({ error: { type: "not_found", message: "Session not found" } }, 404);
|
||||
}
|
||||
return c.json(session, 200);
|
||||
});
|
||||
|
||||
/** GET /web/sessions/:id/history — Historical events for session */
|
||||
app.get("/sessions/:id/history", uuidAuth, async (c) => {
|
||||
const uuid = c.get("uuid");
|
||||
const sessionId = c.req.param("id")!;
|
||||
if (!storeIsSessionOwner(sessionId, uuid)) {
|
||||
return c.json({ error: { type: "forbidden", message: "Not your session" } }, 403);
|
||||
}
|
||||
const session = getSession(sessionId);
|
||||
if (!session) {
|
||||
return c.json({ error: { type: "not_found", message: "Session not found" } }, 404);
|
||||
}
|
||||
|
||||
const bus = getEventBus(sessionId);
|
||||
const events = bus.getEventsSince(0);
|
||||
return c.json({ events }, 200);
|
||||
});
|
||||
|
||||
/** SSE /web/sessions/:id/events — Real-time event stream */
|
||||
app.get("/sessions/:id/events", uuidAuth, async (c) => {
|
||||
const uuid = c.get("uuid");
|
||||
const sessionId = c.req.param("id")!;
|
||||
if (!storeIsSessionOwner(sessionId, uuid)) {
|
||||
return c.json({ error: { type: "forbidden", message: "Not your session" } }, 403);
|
||||
}
|
||||
const session = getSession(sessionId);
|
||||
if (!session) {
|
||||
return c.json({ error: { type: "not_found", message: "Session not found" } }, 404);
|
||||
}
|
||||
|
||||
const lastEventId = c.req.header("Last-Event-ID");
|
||||
const fromSeqNum = lastEventId ? parseInt(lastEventId) : 0;
|
||||
return createSSEStream(c, sessionId, fromSeqNum);
|
||||
});
|
||||
|
||||
export default app;
|
||||
Reference in New Issue
Block a user