Files
claude-code/packages/remote-control-server/src/services/work-dispatch.ts
claude-code-best c8d08d235b Feat/integrate lint preview (#285)
* feat: 适配 zed acp 协议

* docs: 完善 acp 文档

* feat: integrate feature branches + daemon/job 命令层级化 + 跨平台后台引擎

Cherry-picked from origin/lint/preview (637c908), excluding lint-only changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: correct detectMimeFromBase64 to decode raw bytes from base64

Cherry-picked from origin/lint/preview (ee36954).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: daemon 子进程 spawn 跨平台修复 + CliLaunchSpec 集中化重构

Cherry-picked from origin/lint/preview (c5f52cd), excluding lint-only formatting changes.

- 新建 src/utils/cliLaunch.ts: 集中化 CLI 子进程启动层
- 修复 --daemon-worker=kind 等号格式解析
- 修复 daemon/bg fast path 缺少 setShellIfWindows()
- 修复 checkPathExists 用 existsSync 替代 execSync('dir')
- 7 个 spawn 站点迁移到 CliLaunchSpec

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: merge tsconfig.base.json into tsconfig.json with full compiler options

The cherry-pick from 637c908 dropped jsx/strict/etc settings when removing
tsconfig.base.json. This commit restores them in a single tsconfig.json.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: merge tsconfig.base.json into tsconfig.json with full compiler options

The cherry-pick from 637c908 dropped jsx/strict/etc settings when removing
tsconfig.base.json. This commit restores them in a single tsconfig.json.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 20:59:29 +08:00

100 lines
3.2 KiB
TypeScript

import { log, error as logError } from "../logger";
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 });
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);
}