跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

client-context.mjs

54 行 · 2.1 KB历史下载
1let current = Object.freeze({ mode: "none", connection_id: "" });
2
3export function setClientContext(value) {
4 current = Object.freeze({ ...value });
5}
6
7export const getClientContext = () => current;
8export const isRemoteClient = () => current.mode === "remote";
9
10export function assertConnection(connectionID) {
11 if (current.connection_id !== connectionID) {
12 const error = new Error("工作环境已切换,请在当前环境重新操作。");
13 error.code = "connection_changed";
14 throw error;
15 }
16}
17
18export function connectionHeaders(path, context = current) {
19 if (path === "/session" || (path.startsWith("/client/") && !isProjectSharesPath(path))) return {};
20 return { "X-Cage-Connection": context.connection_id || "disconnected" };
21}
22
23export const isProjectSharesPath = (path) => path === "/client/project-shares" || path.startsWith("/client/project-shares/");
24
25export function runtimeURL(path, context = current) {
26 const url = new URL("/api" + path, globalThis.location?.origin || "http://localhost");
27 url.searchParams.set("connection", context.connection_id || "disconnected");
28 return url.pathname + url.search;
29}
30
31export function runtimeSocketURL(path, context = current) {
32 return `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}${runtimeURL(path, context)}`;
33}
34
35export function clientStorageKey(key, context = current) {
36 return context.mode === "remote"
37 ? `${key}:remote:${context.instance_id || context.host_id || "disconnected"}`
38 : key;
39}
40
41export function clientKeyboard(serverKeyboard, context = current) {
42 if (context.mode !== "remote") return serverKeyboard;
43 try {
44 const saved = localStorage.getItem(clientStorageKey("cage-keyboard", context));
45 if (["mac", "native"].includes(saved)) return saved;
46 } catch { /* The client platform remains a usable default without storage. */ }
47 return context.client_platform === "darwin" ? "mac" : "native";
48}
49
50export function saveClientKeyboard(value) {
51 if (!["mac", "native"].includes(value)) throw new Error("键盘习惯无效");
52 localStorage.setItem(clientStorageKey("cage-keyboard"), value);
53 window.dispatchEvent(new Event("cage-client-preferences"));
54}