Spaces:
Sleeping
Sleeping
File size: 7,795 Bytes
7540aea 49cbb33 7540aea 49cbb33 7540aea 49cbb33 7540aea 49cbb33 7540aea 49cbb33 7540aea 49cbb33 7540aea 49cbb33 7540aea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | // βββ remote.ts β Matches original rust/crates/runtime/src/remote.rs ββββββββββ
// Upstream proxy, WebSocket bridge, subprocess environment, remote session context
import * as fs from "fs";
import * as path from "path";
// βββ Types βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export interface RemoteSessionContext {
enabled: boolean;
sessionId: string | undefined;
baseUrl: string;
}
export interface UpstreamProxyBootstrap {
enabled: boolean;
token: string | undefined;
sessionId: string | undefined;
baseUrl: string;
caBundlePath: string | undefined;
wsUrl: () => string;
shouldEnable: () => boolean;
stateForPort: (port: number) => UpstreamProxyState;
}
export interface UpstreamProxyState {
enabled: boolean;
proxyUrl: string | undefined;
caBundlePath: string | undefined;
token: string | undefined;
subprocessEnv: () => Record<string, string>;
}
// βββ Remote session context (from env) βββββββββββββββββββββββββββββββββββββββ
export function remoteSessionContextFromEnv(): RemoteSessionContext {
return remoteSessionContextFromEnvMap(process.env as Record<string, string>);
}
export function remoteSessionContextFromEnvMap(
env: Record<string, string>
): RemoteSessionContext {
const remoteFlag = env["CLAW_CODE_REMOTE"];
const enabled =
remoteFlag === "true" || remoteFlag === "1" || remoteFlag === "yes";
return {
enabled,
sessionId: env["CLAW_CODE_REMOTE_SESSION_ID"],
baseUrl: env["ANTHROPIC_BASE_URL"] || "https://api.anthropic.com",
};
}
// βββ Token reader ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export function readToken(tokenPath: string): string | undefined {
try {
const content = fs.readFileSync(tokenPath, "utf-8");
const trimmed = content.trim();
return trimmed.length > 0 ? trimmed : undefined;
} catch {
return undefined;
}
}
// βββ Upstream proxy URL ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export function upstreamProxyWsUrl(baseUrl: string): string {
// Convert http(s) to ws(s)
let wsUrl = baseUrl.replace(/\/$/, "");
if (wsUrl.startsWith("https://")) {
wsUrl = "wss://" + wsUrl.slice(8);
} else if (wsUrl.startsWith("http://")) {
wsUrl = "ws://" + wsUrl.slice(7);
}
return `${wsUrl}/v1/code/upstreamproxy/ws`;
}
// βββ No-proxy list βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export function noProxyList(): string {
return [
"localhost",
"127.0.0.1",
"::1",
"anthropic.com",
"api.anthropic.com",
"github.com",
"api.github.com",
"*.github.com",
"*.githubusercontent.com",
].join(",");
}
// βββ Inherited upstream proxy env (matches original remote.rs EXACTLY) ββββββ
// All 8 env keys from original Rust UPSTREAM_PROXY_ENV_KEYS
const UPSTREAM_PROXY_ENV_KEYS = [
"HTTPS_PROXY",
"https_proxy",
"NO_PROXY",
"no_proxy",
"SSL_CERT_FILE",
"NODE_EXTRA_CA_CERTS",
"REQUESTS_CA_BUNDLE",
"CURL_CA_BUNDLE",
];
export function inheritedUpstreamProxyEnv(
env: Record<string, string>
): Record<string, string> {
// Both HTTPS_PROXY and SSL_CERT_FILE must be present (matches original guard)
if (!env["HTTPS_PROXY"] || !env["SSL_CERT_FILE"]) {
return {};
}
const result: Record<string, string> = {};
for (const key of UPSTREAM_PROXY_ENV_KEYS) {
if (env[key]) {
result[key] = env[key];
}
}
return result;
}
// βββ Upstream proxy bootstrap ββββββββββββββββββββββββββββββββββββββββββββββββ
export function upstreamProxyBootstrapFromEnv(): UpstreamProxyBootstrap {
return upstreamProxyBootstrapFromEnvMap(
process.env as Record<string, string>
);
}
export function upstreamProxyBootstrapFromEnvMap(
env: Record<string, string>
): UpstreamProxyBootstrap {
const remoteFlag = env["CLAW_CODE_REMOTE"];
const proxyEnabled = env["CCR_UPSTREAM_PROXY_ENABLED"];
const sessionId = env["CLAW_CODE_REMOTE_SESSION_ID"];
const baseUrl = env["ANTHROPIC_BASE_URL"] || "https://api.anthropic.com";
const tokenPath = env["CCR_SESSION_TOKEN_PATH"];
const caBundlePath = env["CCR_CA_BUNDLE_PATH"];
const isRemote =
remoteFlag === "true" || remoteFlag === "1" || remoteFlag === "yes";
const isProxyEnabled =
proxyEnabled === "true" || proxyEnabled === "1" || proxyEnabled === "yes";
// Read token from file
const token = tokenPath ? readToken(tokenPath) : undefined;
const bootstrap: UpstreamProxyBootstrap = {
enabled: isRemote && isProxyEnabled,
token,
sessionId,
baseUrl,
caBundlePath,
wsUrl(): string {
return upstreamProxyWsUrl(baseUrl);
},
shouldEnable(): boolean {
return isRemote && isProxyEnabled && !!token && !!sessionId;
},
stateForPort(port: number): UpstreamProxyState {
if (!bootstrap.shouldEnable()) {
return {
enabled: false,
proxyUrl: undefined,
caBundlePath: undefined,
token: undefined,
subprocessEnv: () => ({}),
};
}
const proxyUrl = `http://127.0.0.1:${port}`;
const capturedToken = token;
const capturedCaBundle = caBundlePath;
return {
enabled: true,
proxyUrl,
caBundlePath: capturedCaBundle,
token: capturedToken,
subprocessEnv(): Record<string, string> {
const noProxy = noProxyList();
const result: Record<string, string> = {
HTTPS_PROXY: proxyUrl,
https_proxy: proxyUrl,
HTTP_PROXY: proxyUrl,
http_proxy: proxyUrl,
NO_PROXY: noProxy,
no_proxy: noProxy,
};
if (capturedCaBundle) {
result["SSL_CERT_FILE"] = capturedCaBundle;
result["NODE_EXTRA_CA_CERTS"] = capturedCaBundle;
result["REQUESTS_CA_BUNDLE"] = capturedCaBundle;
result["CURL_CA_BUNDLE"] = capturedCaBundle;
}
return result;
},
};
},
};
return bootstrap;
}
// βββ Remote session management βββββββββββββββββββββββββββββββββββββββββββββββ
export interface RemoteSession {
id: string;
baseUrl: string;
token: string | undefined;
proxyState: UpstreamProxyState;
isActive: boolean;
}
export function createRemoteSession(
context: RemoteSessionContext,
proxyState: UpstreamProxyState
): RemoteSession {
return {
id: context.sessionId || `remote-${Date.now()}`,
baseUrl: context.baseUrl,
token: proxyState.token,
proxyState,
isActive: context.enabled,
};
}
// βββ Heartbeat config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export interface HeartbeatConfig {
intervalMs: number;
timeoutMs: number;
maxRetries: number;
}
export function defaultHeartbeatConfig(): HeartbeatConfig {
return {
intervalMs: 30_000,
timeoutMs: 10_000,
maxRetries: 3,
};
}
|