@certalis/agent-smith 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/bin.js +533 -115
- package/dist/claude-companion/hooks/provider-choice.js +6 -1
- package/dist/client-launcher.js +501 -83
- package/dist/pool.js +4 -0
- package/dist/post-processor.worker.js +4 -0
- package/dist/server.js +4 -0
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -1985,6 +1985,9 @@ function sanitizeClientRouting(value) {
|
|
|
1985
1985
|
if (typeof value.keep_claude_login === "boolean") {
|
|
1986
1986
|
client.keep_claude_login = value.keep_claude_login;
|
|
1987
1987
|
}
|
|
1988
|
+
if (typeof value.remote_control === "boolean") {
|
|
1989
|
+
client.remote_control = value.remote_control;
|
|
1990
|
+
}
|
|
1988
1991
|
if (typeof value.pool_ssh_host === "string" && POOL_SSH_HOST_PATTERN.test(value.pool_ssh_host)) {
|
|
1989
1992
|
client.pool_ssh_host = value.pool_ssh_host;
|
|
1990
1993
|
}
|
|
@@ -2256,6 +2259,7 @@ function clientRouting(client) {
|
|
|
2256
2259
|
...client?.base_url && { baseUrl: client.base_url },
|
|
2257
2260
|
...client?.proxy_key_file && { proxyKeyFile: client.proxy_key_file },
|
|
2258
2261
|
...client?.keep_claude_login === true && { keepClaudeLogin: true },
|
|
2262
|
+
...client?.remote_control === false && { remoteControl: false },
|
|
2259
2263
|
...client?.pool_ssh_host && { poolSshHost: client.pool_ssh_host }
|
|
2260
2264
|
};
|
|
2261
2265
|
}
|
|
@@ -17945,35 +17949,31 @@ async function compressedBody(req, headers3) {
|
|
|
17945
17949
|
headers3.set("content-encoding", "gzip");
|
|
17946
17950
|
return Bun.gzipSync(body);
|
|
17947
17951
|
}
|
|
17952
|
+
async function fetchUpstream(target, init, unreachable) {
|
|
17953
|
+
try {
|
|
17954
|
+
const upstream = await fetch(target, { ...init, redirect: "manual" });
|
|
17955
|
+
return withSanitizedProxyHeaders(upstream);
|
|
17956
|
+
} catch (error) {
|
|
17957
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
17958
|
+
return Response.json({
|
|
17959
|
+
type: "error",
|
|
17960
|
+
error: { type: "api_error", message: `${unreachable}: ${reason}` }
|
|
17961
|
+
}, { status: 502 });
|
|
17962
|
+
}
|
|
17963
|
+
}
|
|
17964
|
+
async function relayToPool(req, target, headers3) {
|
|
17965
|
+
const body = await compressedBody(req, headers3);
|
|
17966
|
+
return fetchUpstream(target, { method: req.method, headers: headers3, body, signal: req.signal }, "Agent Smith relay could not reach the pool");
|
|
17967
|
+
}
|
|
17948
17968
|
function startClientRelay(remoteBaseUrl) {
|
|
17949
17969
|
const remote = remoteBaseUrl.replace(/\/+$/, "");
|
|
17950
17970
|
const server = Bun.serve({
|
|
17951
17971
|
hostname: "127.0.0.1",
|
|
17952
17972
|
port: 0,
|
|
17953
17973
|
idleTimeout: 0,
|
|
17954
|
-
|
|
17974
|
+
fetch(req) {
|
|
17955
17975
|
const url = new URL(req.url);
|
|
17956
|
-
|
|
17957
|
-
const body = await compressedBody(req, headers3);
|
|
17958
|
-
try {
|
|
17959
|
-
const upstream = await fetch(`${remote}${url.pathname}${url.search}`, {
|
|
17960
|
-
method: req.method,
|
|
17961
|
-
headers: headers3,
|
|
17962
|
-
body,
|
|
17963
|
-
signal: req.signal,
|
|
17964
|
-
redirect: "manual"
|
|
17965
|
-
});
|
|
17966
|
-
return withSanitizedProxyHeaders(upstream);
|
|
17967
|
-
} catch (error) {
|
|
17968
|
-
const reason = error instanceof Error ? error.message : String(error);
|
|
17969
|
-
return Response.json({
|
|
17970
|
-
type: "error",
|
|
17971
|
-
error: {
|
|
17972
|
-
type: "api_error",
|
|
17973
|
-
message: `Agent Smith relay could not reach the pool: ${reason}`
|
|
17974
|
-
}
|
|
17975
|
-
}, { status: 502 });
|
|
17976
|
-
}
|
|
17976
|
+
return relayToPool(req, `${remote}${url.pathname}${url.search}`, forwardedHeaders(req.headers));
|
|
17977
17977
|
}
|
|
17978
17978
|
});
|
|
17979
17979
|
return {
|
|
@@ -17994,22 +17994,373 @@ var init_client_relay = __esm(() => {
|
|
|
17994
17994
|
];
|
|
17995
17995
|
});
|
|
17996
17996
|
|
|
17997
|
-
// src/core/
|
|
17997
|
+
// src/core/client-forward-proxy.ts
|
|
17998
|
+
import { randomBytes as randomBytes3, timingSafeEqual as timingSafeEqual3, X509Certificate } from "crypto";
|
|
17998
17999
|
import {
|
|
17999
|
-
accessSync as accessSync2,
|
|
18000
18000
|
chmodSync as chmodSync2,
|
|
18001
|
-
constants as constants5,
|
|
18002
|
-
copyFileSync,
|
|
18003
18001
|
existsSync as existsSync3,
|
|
18004
18002
|
mkdirSync as mkdirSync4,
|
|
18003
|
+
mkdtempSync,
|
|
18005
18004
|
readFileSync as readFileSync5,
|
|
18005
|
+
renameSync as renameSync3,
|
|
18006
18006
|
rmSync as rmSync2,
|
|
18007
|
+
writeFileSync as writeFileSync3
|
|
18008
|
+
} from "fs";
|
|
18009
|
+
import {
|
|
18010
|
+
createConnection,
|
|
18011
|
+
createServer
|
|
18012
|
+
} from "net";
|
|
18013
|
+
import { tmpdir as tmpdir3 } from "os";
|
|
18014
|
+
import { join as join9 } from "path";
|
|
18015
|
+
function readCertificate(dir, renewAfter = Number.NEGATIVE_INFINITY) {
|
|
18016
|
+
try {
|
|
18017
|
+
const caFile = join9(dir, "ca.pem");
|
|
18018
|
+
const cert = readFileSync5(join9(dir, "leaf.pem"), "utf8");
|
|
18019
|
+
const key = readFileSync5(join9(dir, "leaf.key"), "utf8");
|
|
18020
|
+
if (!existsSync3(caFile))
|
|
18021
|
+
return;
|
|
18022
|
+
const expiresAt = Date.parse(new X509Certificate(cert).validTo);
|
|
18023
|
+
if (expiresAt - RENEW_BEFORE_MS <= renewAfter)
|
|
18024
|
+
return;
|
|
18025
|
+
return { caFile, cert, key };
|
|
18026
|
+
} catch {
|
|
18027
|
+
return;
|
|
18028
|
+
}
|
|
18029
|
+
}
|
|
18030
|
+
function runOpenssl(dir, args) {
|
|
18031
|
+
let result;
|
|
18032
|
+
try {
|
|
18033
|
+
result = Bun.spawnSync(["openssl", ...args], {
|
|
18034
|
+
cwd: dir,
|
|
18035
|
+
stdout: "ignore",
|
|
18036
|
+
stderr: "pipe"
|
|
18037
|
+
});
|
|
18038
|
+
} catch {
|
|
18039
|
+
throw new Error("Remote Control mode needs openssl on PATH");
|
|
18040
|
+
}
|
|
18041
|
+
if (!result.success)
|
|
18042
|
+
throw new Error(`openssl ${args[0]} failed: ${result.stderr?.toString().trim()}`);
|
|
18043
|
+
}
|
|
18044
|
+
function generateCertificate(dir) {
|
|
18045
|
+
mkdirSync4(dir, { recursive: true, mode: 448 });
|
|
18046
|
+
writeFileSync3(join9(dir, "openssl.cnf"), OPENSSL_CONFIG, { mode: 384 });
|
|
18047
|
+
const newKey = (file) => runOpenssl(dir, [
|
|
18048
|
+
"ecparam",
|
|
18049
|
+
"-name",
|
|
18050
|
+
"prime256v1",
|
|
18051
|
+
"-genkey",
|
|
18052
|
+
"-noout",
|
|
18053
|
+
"-out",
|
|
18054
|
+
file
|
|
18055
|
+
]);
|
|
18056
|
+
newKey("ca.key");
|
|
18057
|
+
runOpenssl(dir, [
|
|
18058
|
+
"req",
|
|
18059
|
+
"-x509",
|
|
18060
|
+
"-new",
|
|
18061
|
+
"-key",
|
|
18062
|
+
"ca.key",
|
|
18063
|
+
"-sha256",
|
|
18064
|
+
"-days",
|
|
18065
|
+
String(LEAF_DAYS + 1),
|
|
18066
|
+
"-subj",
|
|
18067
|
+
"/CN=Agent Smith Remote Control CA",
|
|
18068
|
+
"-config",
|
|
18069
|
+
"openssl.cnf",
|
|
18070
|
+
"-extensions",
|
|
18071
|
+
"v3_ca",
|
|
18072
|
+
"-out",
|
|
18073
|
+
"ca.pem"
|
|
18074
|
+
]);
|
|
18075
|
+
newKey("leaf.key");
|
|
18076
|
+
runOpenssl(dir, [
|
|
18077
|
+
"req",
|
|
18078
|
+
"-new",
|
|
18079
|
+
"-key",
|
|
18080
|
+
"leaf.key",
|
|
18081
|
+
"-subj",
|
|
18082
|
+
`/CN=${INTERCEPTED_HOST}`,
|
|
18083
|
+
"-config",
|
|
18084
|
+
"openssl.cnf",
|
|
18085
|
+
"-out",
|
|
18086
|
+
"leaf.csr"
|
|
18087
|
+
]);
|
|
18088
|
+
runOpenssl(dir, [
|
|
18089
|
+
"x509",
|
|
18090
|
+
"-req",
|
|
18091
|
+
"-in",
|
|
18092
|
+
"leaf.csr",
|
|
18093
|
+
"-CA",
|
|
18094
|
+
"ca.pem",
|
|
18095
|
+
"-CAkey",
|
|
18096
|
+
"ca.key",
|
|
18097
|
+
"-set_serial",
|
|
18098
|
+
`0x${randomBytes3(16).toString("hex")}`,
|
|
18099
|
+
"-days",
|
|
18100
|
+
String(LEAF_DAYS),
|
|
18101
|
+
"-sha256",
|
|
18102
|
+
"-extfile",
|
|
18103
|
+
"openssl.cnf",
|
|
18104
|
+
"-extensions",
|
|
18105
|
+
"v3_leaf",
|
|
18106
|
+
"-out",
|
|
18107
|
+
"leaf.pem"
|
|
18108
|
+
]);
|
|
18109
|
+
for (const file of ["ca.key", "leaf.csr", "openssl.cnf"])
|
|
18110
|
+
rmSync2(join9(dir, file), { force: true });
|
|
18111
|
+
for (const file of ["ca.pem", "leaf.pem", "leaf.key"])
|
|
18112
|
+
chmodSync2(join9(dir, file), 384);
|
|
18113
|
+
}
|
|
18114
|
+
function ensureInterceptionCertificate(dir, now = Date.now()) {
|
|
18115
|
+
const current = readCertificate(dir, now);
|
|
18116
|
+
if (current)
|
|
18117
|
+
return current;
|
|
18118
|
+
const suffix = `${process.pid}-${randomBytes3(4).toString("hex")}`;
|
|
18119
|
+
const staging = `${dir}.staging-${suffix}`;
|
|
18120
|
+
const retired = `${dir}.retired-${suffix}`;
|
|
18121
|
+
try {
|
|
18122
|
+
generateCertificate(staging);
|
|
18123
|
+
if (existsSync3(dir))
|
|
18124
|
+
renameSync3(dir, retired);
|
|
18125
|
+
renameSync3(staging, dir);
|
|
18126
|
+
} catch (error) {
|
|
18127
|
+
const raced = readCertificate(dir);
|
|
18128
|
+
if (raced)
|
|
18129
|
+
return raced;
|
|
18130
|
+
throw error;
|
|
18131
|
+
} finally {
|
|
18132
|
+
rmSync2(staging, { force: true, recursive: true });
|
|
18133
|
+
rmSync2(retired, { force: true, recursive: true });
|
|
18134
|
+
}
|
|
18135
|
+
const created = readCertificate(dir);
|
|
18136
|
+
if (!created)
|
|
18137
|
+
throw new Error("Agent Smith could not create its interception certificate");
|
|
18138
|
+
return created;
|
|
18139
|
+
}
|
|
18140
|
+
function parseConnectRequest(head) {
|
|
18141
|
+
const [requestLine = "", ...headerLines] = head.split(`\r
|
|
18142
|
+
`);
|
|
18143
|
+
const match = CONNECT_LINE.exec(requestLine);
|
|
18144
|
+
if (!match)
|
|
18145
|
+
return;
|
|
18146
|
+
const port = Number(match[2]);
|
|
18147
|
+
if (port < 1 || port > 65535)
|
|
18148
|
+
return;
|
|
18149
|
+
const authorization = headerLines.map((line) => line.split(/:(.*)/s, 2)).find(([name]) => name?.trim().toLowerCase() === "proxy-authorization");
|
|
18150
|
+
return {
|
|
18151
|
+
host: match[1].replace(/^\[|\]$/g, ""),
|
|
18152
|
+
port,
|
|
18153
|
+
...authorization?.[1] && {
|
|
18154
|
+
proxyAuthorization: authorization[1].trim()
|
|
18155
|
+
}
|
|
18156
|
+
};
|
|
18157
|
+
}
|
|
18158
|
+
function sameSecret(actual, expected) {
|
|
18159
|
+
if (actual === undefined)
|
|
18160
|
+
return false;
|
|
18161
|
+
const a = Buffer.from(actual);
|
|
18162
|
+
const b = Buffer.from(expected);
|
|
18163
|
+
return a.length === b.length && timingSafeEqual3(a, b);
|
|
18164
|
+
}
|
|
18165
|
+
function poolHeaders(original, deviceKey) {
|
|
18166
|
+
const headers3 = forwardedHeaders(original);
|
|
18167
|
+
headers3.delete("authorization");
|
|
18168
|
+
headers3.set("x-api-key", deviceKey);
|
|
18169
|
+
return headers3;
|
|
18170
|
+
}
|
|
18171
|
+
async function passThrough(req, target) {
|
|
18172
|
+
const hasBody = req.method !== "GET" && req.method !== "HEAD";
|
|
18173
|
+
return fetchUpstream(target, {
|
|
18174
|
+
method: req.method,
|
|
18175
|
+
headers: forwardedHeaders(req.headers),
|
|
18176
|
+
body: hasBody ? await req.arrayBuffer() : undefined,
|
|
18177
|
+
signal: req.signal
|
|
18178
|
+
}, "Agent Smith proxy could not reach Anthropic");
|
|
18179
|
+
}
|
|
18180
|
+
function webSocketRelay(req, upstreamUrl) {
|
|
18181
|
+
const headers3 = forwardedHeaders(req.headers);
|
|
18182
|
+
for (const name of [...headers3.keys()])
|
|
18183
|
+
if (name.startsWith("sec-websocket-") || name === "upgrade")
|
|
18184
|
+
headers3.delete(name);
|
|
18185
|
+
const protocols = (req.headers.get("sec-websocket-protocol") ?? "").split(",").map((protocol) => protocol.trim()).filter(Boolean);
|
|
18186
|
+
return {
|
|
18187
|
+
upstreamUrl,
|
|
18188
|
+
headers: Object.fromEntries(headers3),
|
|
18189
|
+
protocols,
|
|
18190
|
+
queued: []
|
|
18191
|
+
};
|
|
18192
|
+
}
|
|
18193
|
+
function refuse(client2, status, extra = "") {
|
|
18194
|
+
client2.end(`HTTP/1.1 ${status}\r
|
|
18195
|
+
${extra}Connection: close\r
|
|
18196
|
+
\r
|
|
18197
|
+
`);
|
|
18198
|
+
}
|
|
18199
|
+
async function startForwardProxy(options) {
|
|
18200
|
+
const token = randomBytes3(24).toString("hex");
|
|
18201
|
+
const expectedAuthorization = `Basic ${Buffer.from(`${FORWARD_PROXY_USER}:${token}`).toString("base64")}`;
|
|
18202
|
+
const anthropic = (options.anthropicOrigin ?? ANTHROPIC_ORIGIN).replace(/\/+$/, "");
|
|
18203
|
+
const pool = options.poolUrl.replace(/\/+$/, "");
|
|
18204
|
+
const socketDir = mkdtempSync(join9(tmpdir3(), "agent-smith-proxy-"));
|
|
18205
|
+
const interceptSocket = join9(socketDir, "tls.sock");
|
|
18206
|
+
const intercept = Bun.serve({
|
|
18207
|
+
unix: interceptSocket,
|
|
18208
|
+
idleTimeout: 0,
|
|
18209
|
+
tls: { cert: options.certificate.cert, key: options.certificate.key },
|
|
18210
|
+
fetch(req, server) {
|
|
18211
|
+
const url = new URL(req.url);
|
|
18212
|
+
const path = `${url.pathname}${url.search}`;
|
|
18213
|
+
if (POOL_PATHS.has(url.pathname))
|
|
18214
|
+
return relayToPool(req, `${pool}${path}`, poolHeaders(req.headers, options.deviceKey));
|
|
18215
|
+
if (req.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
|
18216
|
+
const relay = webSocketRelay(req, `${anthropic.replace(/^http/, "ws")}${path}`);
|
|
18217
|
+
if (server.upgrade(req, { data: relay }))
|
|
18218
|
+
return;
|
|
18219
|
+
return new Response("WebSocket upgrade failed", { status: 400 });
|
|
18220
|
+
}
|
|
18221
|
+
return passThrough(req, `${anthropic}${path}`);
|
|
18222
|
+
},
|
|
18223
|
+
websocket: {
|
|
18224
|
+
open(ws) {
|
|
18225
|
+
const upstream = new WebSocket(ws.data.upstreamUrl, {
|
|
18226
|
+
headers: ws.data.headers,
|
|
18227
|
+
protocols: ws.data.protocols
|
|
18228
|
+
});
|
|
18229
|
+
upstream.binaryType = "arraybuffer";
|
|
18230
|
+
ws.data.upstream = upstream;
|
|
18231
|
+
upstream.onopen = () => {
|
|
18232
|
+
for (const message of ws.data.queued.splice(0))
|
|
18233
|
+
upstream.send(message);
|
|
18234
|
+
};
|
|
18235
|
+
upstream.onmessage = (event) => ws.send(typeof event.data === "string" ? event.data : new Uint8Array(event.data));
|
|
18236
|
+
upstream.onclose = (event) => ws.close(closeCode(event.code), event.reason);
|
|
18237
|
+
upstream.onerror = () => ws.close(1011, "upstream error");
|
|
18238
|
+
},
|
|
18239
|
+
message(ws, message) {
|
|
18240
|
+
const payload = typeof message === "string" ? message : new Uint8Array(message);
|
|
18241
|
+
const upstream = ws.data.upstream;
|
|
18242
|
+
if (upstream?.readyState === WebSocket.OPEN)
|
|
18243
|
+
upstream.send(payload);
|
|
18244
|
+
else
|
|
18245
|
+
ws.data.queued.push(payload);
|
|
18246
|
+
},
|
|
18247
|
+
close(ws, code, reason) {
|
|
18248
|
+
ws.data.upstream?.close(closeCode(code), reason);
|
|
18249
|
+
}
|
|
18250
|
+
}
|
|
18251
|
+
});
|
|
18252
|
+
const sockets = new Set;
|
|
18253
|
+
const front = createServer((client2) => {
|
|
18254
|
+
sockets.add(client2);
|
|
18255
|
+
client2.on("close", () => sockets.delete(client2));
|
|
18256
|
+
client2.on("error", () => client2.destroy());
|
|
18257
|
+
let head = Buffer.alloc(0);
|
|
18258
|
+
const onData = (chunk) => {
|
|
18259
|
+
head = Buffer.concat([head, chunk]);
|
|
18260
|
+
const end = head.indexOf(`\r
|
|
18261
|
+
\r
|
|
18262
|
+
`);
|
|
18263
|
+
if (end === -1) {
|
|
18264
|
+
if (head.length > MAX_CONNECT_HEAD_BYTES)
|
|
18265
|
+
refuse(client2, "431 Request Header Fields Too Large");
|
|
18266
|
+
return;
|
|
18267
|
+
}
|
|
18268
|
+
client2.off("data", onData);
|
|
18269
|
+
client2.pause();
|
|
18270
|
+
const request3 = parseConnectRequest(head.subarray(0, end).toString("latin1"));
|
|
18271
|
+
const rest = head.subarray(end + 4);
|
|
18272
|
+
if (!request3)
|
|
18273
|
+
return refuse(client2, "405 Method Not Allowed");
|
|
18274
|
+
if (!sameSecret(request3.proxyAuthorization, expectedAuthorization))
|
|
18275
|
+
return refuse(client2, "407 Proxy Authentication Required", `Proxy-Authenticate: Basic realm="agent-smith"\r
|
|
18276
|
+
`);
|
|
18277
|
+
const intercepted = request3.host.toLowerCase() === INTERCEPTED_HOST && request3.port === 443;
|
|
18278
|
+
const upstream = createConnection(intercepted ? { path: interceptSocket } : { host: request3.host, port: request3.port });
|
|
18279
|
+
sockets.add(upstream);
|
|
18280
|
+
let connected = false;
|
|
18281
|
+
upstream.on("close", () => {
|
|
18282
|
+
sockets.delete(upstream);
|
|
18283
|
+
if (connected)
|
|
18284
|
+
client2.destroy();
|
|
18285
|
+
});
|
|
18286
|
+
upstream.on("error", () => {
|
|
18287
|
+
if (connected)
|
|
18288
|
+
client2.destroy();
|
|
18289
|
+
else
|
|
18290
|
+
refuse(client2, "502 Bad Gateway");
|
|
18291
|
+
});
|
|
18292
|
+
client2.on("close", () => upstream.destroy());
|
|
18293
|
+
upstream.on("connect", () => {
|
|
18294
|
+
connected = true;
|
|
18295
|
+
client2.write(`HTTP/1.1 200 Connection Established\r
|
|
18296
|
+
\r
|
|
18297
|
+
`);
|
|
18298
|
+
if (rest.length > 0)
|
|
18299
|
+
upstream.write(rest);
|
|
18300
|
+
client2.pipe(upstream);
|
|
18301
|
+
upstream.pipe(client2);
|
|
18302
|
+
client2.resume();
|
|
18303
|
+
});
|
|
18304
|
+
};
|
|
18305
|
+
client2.on("data", onData);
|
|
18306
|
+
});
|
|
18307
|
+
await new Promise((resolve4, reject) => {
|
|
18308
|
+
front.once("error", reject);
|
|
18309
|
+
front.listen(0, "127.0.0.1", () => resolve4());
|
|
18310
|
+
});
|
|
18311
|
+
const { port } = front.address();
|
|
18312
|
+
return {
|
|
18313
|
+
url: `http://${FORWARD_PROXY_USER}:${token}@127.0.0.1:${port}`,
|
|
18314
|
+
stop: () => {
|
|
18315
|
+
front.close();
|
|
18316
|
+
for (const socket of sockets)
|
|
18317
|
+
socket.destroy();
|
|
18318
|
+
intercept.stop(true);
|
|
18319
|
+
rmSync2(socketDir, { force: true, recursive: true });
|
|
18320
|
+
}
|
|
18321
|
+
};
|
|
18322
|
+
}
|
|
18323
|
+
var INTERCEPTED_HOST = "api.anthropic.com", FORWARD_PROXY_USER = "agent-smith", ANTHROPIC_ORIGIN, POOL_PATHS, MAX_CONNECT_HEAD_BYTES = 8192, LEAF_DAYS = 397, RENEW_BEFORE_MS, CONNECT_LINE, OPENSSL_CONFIG, closeCode = (code) => code >= 1000 && code <= 4999 && ![1004, 1005, 1006, 1015].includes(code) ? code : 1000;
|
|
18324
|
+
var init_client_forward_proxy = __esm(() => {
|
|
18325
|
+
init_client_relay();
|
|
18326
|
+
ANTHROPIC_ORIGIN = `https://${INTERCEPTED_HOST}`;
|
|
18327
|
+
POOL_PATHS = new Set(["/v1/messages", "/v1/messages/count_tokens"]);
|
|
18328
|
+
RENEW_BEFORE_MS = 30 * 24 * 60 * 60 * 1000;
|
|
18329
|
+
CONNECT_LINE = /^CONNECT ([A-Za-z0-9.-]+|\[[0-9A-Fa-f:.]+\]):(\d{1,5}) HTTP\/1\.[01]$/;
|
|
18330
|
+
OPENSSL_CONFIG = `[req]
|
|
18331
|
+
distinguished_name = dn
|
|
18332
|
+
[dn]
|
|
18333
|
+
[v3_ca]
|
|
18334
|
+
basicConstraints = critical,CA:TRUE,pathlen:0
|
|
18335
|
+
keyUsage = critical,keyCertSign,cRLSign
|
|
18336
|
+
subjectKeyIdentifier = hash
|
|
18337
|
+
nameConstraints = critical,permitted;DNS:${INTERCEPTED_HOST}
|
|
18338
|
+
[v3_leaf]
|
|
18339
|
+
basicConstraints = critical,CA:FALSE
|
|
18340
|
+
keyUsage = critical,digitalSignature
|
|
18341
|
+
extendedKeyUsage = serverAuth
|
|
18342
|
+
subjectAltName = DNS:${INTERCEPTED_HOST}
|
|
18343
|
+
authorityKeyIdentifier = keyid
|
|
18344
|
+
subjectKeyIdentifier = hash
|
|
18345
|
+
`;
|
|
18346
|
+
});
|
|
18347
|
+
|
|
18348
|
+
// src/core/local-routing.ts
|
|
18349
|
+
import {
|
|
18350
|
+
accessSync as accessSync2,
|
|
18351
|
+
chmodSync as chmodSync3,
|
|
18352
|
+
constants as constants5,
|
|
18353
|
+
copyFileSync,
|
|
18354
|
+
existsSync as existsSync4,
|
|
18355
|
+
mkdirSync as mkdirSync5,
|
|
18356
|
+
readFileSync as readFileSync6,
|
|
18357
|
+
rmSync as rmSync3,
|
|
18007
18358
|
statSync as statSync4,
|
|
18008
18359
|
unlinkSync as unlinkSync3,
|
|
18009
|
-
writeFileSync as
|
|
18360
|
+
writeFileSync as writeFileSync4
|
|
18010
18361
|
} from "fs";
|
|
18011
18362
|
import { homedir as homedir2 } from "os";
|
|
18012
|
-
import { dirname as dirname5, join as
|
|
18363
|
+
import { dirname as dirname5, join as join10 } from "path";
|
|
18013
18364
|
import { fileURLToPath } from "url";
|
|
18014
18365
|
function shellQuote(value) {
|
|
18015
18366
|
return `'${value.replaceAll("'", `'"'"'`)}'`;
|
|
@@ -18022,7 +18373,7 @@ function proxyBaseUrl(port, configuredBaseUrl, directProvider, wire, providerPoo
|
|
|
18022
18373
|
return `${configuredBaseUrl ?? `http://127.0.0.1:${port}`}${route}`;
|
|
18023
18374
|
}
|
|
18024
18375
|
function validateProxyKeyFile(proxyKeyFile) {
|
|
18025
|
-
const resolvedPath = proxyKeyFile.startsWith("~/") ?
|
|
18376
|
+
const resolvedPath = proxyKeyFile.startsWith("~/") ? join10(homedir2(), proxyKeyFile.slice(2)) : proxyKeyFile;
|
|
18026
18377
|
let metadata;
|
|
18027
18378
|
try {
|
|
18028
18379
|
metadata = statSync4(resolvedPath);
|
|
@@ -18035,7 +18386,7 @@ function validateProxyKeyFile(proxyKeyFile) {
|
|
|
18035
18386
|
if ((metadata.mode & 63) !== 0) {
|
|
18036
18387
|
throw new Error("Agent Smith forwarding-key file must be owner-only");
|
|
18037
18388
|
}
|
|
18038
|
-
const key =
|
|
18389
|
+
const key = readFileSync6(resolvedPath, "utf8").trim();
|
|
18039
18390
|
if (!/^[\x21-\x7e]{1,512}$/.test(key)) {
|
|
18040
18391
|
throw new Error("Agent Smith forwarding-key file is invalid");
|
|
18041
18392
|
}
|
|
@@ -18060,32 +18411,32 @@ export ANTHROPIC_CUSTOM_HEADERS="\${ANTHROPIC_CUSTOM_HEADERS:+$ANTHROPIC_CUSTOM_
|
|
|
18060
18411
|
}x-api-key: $AGENT_SMITH_PROXY_KEY"`;
|
|
18061
18412
|
}
|
|
18062
18413
|
function claudeCompanionSourceDir() {
|
|
18063
|
-
const bundled =
|
|
18064
|
-
if (
|
|
18414
|
+
const bundled = join10(import.meta.dir, "claude-companion");
|
|
18415
|
+
if (existsSync4(join10(bundled, ".claude-plugin", "plugin.json"))) {
|
|
18065
18416
|
return bundled;
|
|
18066
18417
|
}
|
|
18067
|
-
const source =
|
|
18068
|
-
if (
|
|
18418
|
+
const source = join10(import.meta.dir, "..", "claude-companion");
|
|
18419
|
+
if (existsSync4(join10(source, ".claude-plugin", "plugin.json"))) {
|
|
18069
18420
|
return source;
|
|
18070
18421
|
}
|
|
18071
18422
|
throw new Error("Agent-smith Claude companion files are missing");
|
|
18072
18423
|
}
|
|
18073
18424
|
function installClaudeCompanion(binDir) {
|
|
18074
18425
|
const sourceDir = claudeCompanionSourceDir();
|
|
18075
|
-
const targetDir =
|
|
18076
|
-
|
|
18426
|
+
const targetDir = join10(binDir, CLAUDE_COMPANION_DIRNAME);
|
|
18427
|
+
rmSync3(targetDir, { force: true, recursive: true });
|
|
18077
18428
|
for (const relativePath of CLAUDE_COMPANION_FILES) {
|
|
18078
|
-
const target =
|
|
18079
|
-
|
|
18080
|
-
copyFileSync(
|
|
18081
|
-
|
|
18429
|
+
const target = join10(targetDir, relativePath);
|
|
18430
|
+
mkdirSync5(dirname5(target), { recursive: true, mode: 448 });
|
|
18431
|
+
copyFileSync(join10(sourceDir, relativePath), target, constants5.COPYFILE_FICLONE);
|
|
18432
|
+
chmodSync3(target, 384);
|
|
18082
18433
|
}
|
|
18083
18434
|
for (const directory of [
|
|
18084
18435
|
targetDir,
|
|
18085
|
-
|
|
18086
|
-
|
|
18436
|
+
join10(targetDir, ".claude-plugin"),
|
|
18437
|
+
join10(targetDir, "hooks")
|
|
18087
18438
|
]) {
|
|
18088
|
-
|
|
18439
|
+
chmodSync3(directory, 448);
|
|
18089
18440
|
}
|
|
18090
18441
|
return targetDir;
|
|
18091
18442
|
}
|
|
@@ -18102,6 +18453,36 @@ export ANTHROPIC_BASE_URL=${shellQuote(baseUrl)}
|
|
|
18102
18453
|
exec ${shellQuote(claudePath)} --plugin-dir ${shellQuote(companionDir)} "$@"
|
|
18103
18454
|
`;
|
|
18104
18455
|
}
|
|
18456
|
+
function dropInheritedForwardProxy(env) {
|
|
18457
|
+
for (const name of PROXY_VARIABLES)
|
|
18458
|
+
if (env[name]?.startsWith(`http://${FORWARD_PROXY_USER}:`))
|
|
18459
|
+
delete env[name];
|
|
18460
|
+
}
|
|
18461
|
+
function remoteControlCertificate(configPath) {
|
|
18462
|
+
try {
|
|
18463
|
+
return ensureInterceptionCertificate(join10(dirname5(configPath), "remote-control"));
|
|
18464
|
+
} catch (error) {
|
|
18465
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
18466
|
+
console.error(`Agent Smith: Remote Control is off for this session: ${reason}`);
|
|
18467
|
+
return;
|
|
18468
|
+
}
|
|
18469
|
+
}
|
|
18470
|
+
function forwardProxyClientEnv(env, proxyUrl, caFile) {
|
|
18471
|
+
const routed = { ...env };
|
|
18472
|
+
for (const name of [
|
|
18473
|
+
"ANTHROPIC_API_KEY",
|
|
18474
|
+
"ANTHROPIC_AUTH_TOKEN",
|
|
18475
|
+
"ANTHROPIC_BASE_URL",
|
|
18476
|
+
"ANTHROPIC_UNIX_SOCKET"
|
|
18477
|
+
])
|
|
18478
|
+
delete routed[name];
|
|
18479
|
+
for (const name of PROXY_VARIABLES)
|
|
18480
|
+
routed[name] = proxyUrl;
|
|
18481
|
+
routed.NO_PROXY = NO_PROXY_HOSTS;
|
|
18482
|
+
routed.no_proxy = NO_PROXY_HOSTS;
|
|
18483
|
+
routed.NODE_EXTRA_CA_CERTS = caFile;
|
|
18484
|
+
return routed;
|
|
18485
|
+
}
|
|
18105
18486
|
function codexWrapper(codexPath, baseUrl, proxyKeyFile) {
|
|
18106
18487
|
const config = [
|
|
18107
18488
|
'model_provider="agent_smith"',
|
|
@@ -18126,58 +18507,58 @@ ${overrides} \\
|
|
|
18126
18507
|
async function installClientWrappers(options) {
|
|
18127
18508
|
if (options.clientConfigPath) {
|
|
18128
18509
|
const launcher = fileURLToPath(new URL(import.meta.url.endsWith(".ts") ? "./client-launcher.ts" : "./client-launcher.js", import.meta.url));
|
|
18129
|
-
|
|
18510
|
+
mkdirSync5(options.binDir, { recursive: true, mode: 448 });
|
|
18130
18511
|
for (const [tool, target] of [
|
|
18131
18512
|
["claude", options.claudePath],
|
|
18132
18513
|
["codex", options.codexPath]
|
|
18133
18514
|
]) {
|
|
18134
18515
|
if (!target)
|
|
18135
18516
|
continue;
|
|
18136
|
-
if (target ===
|
|
18517
|
+
if (target === join10(options.binDir, tool))
|
|
18137
18518
|
throw new Error("A client launcher cannot call itself");
|
|
18138
|
-
const wrapper =
|
|
18519
|
+
const wrapper = join10(options.binDir, tool);
|
|
18139
18520
|
const companionDir = tool === "claude" ? installClaudeCompanion(options.binDir) : "";
|
|
18140
|
-
|
|
18521
|
+
writeFileSync4(wrapper, `#!/bin/sh
|
|
18141
18522
|
${CONFIG_CLIENT_MARKER}
|
|
18142
18523
|
exec ${[process.execPath, launcher, options.clientConfigPath, tool, target, companionDir].map(shellQuote).join(" ")} "$@"
|
|
18143
18524
|
`, { encoding: "utf8", mode: 448 });
|
|
18144
|
-
|
|
18525
|
+
chmodSync3(wrapper, 448);
|
|
18145
18526
|
}
|
|
18146
18527
|
return;
|
|
18147
18528
|
}
|
|
18148
18529
|
if (options.baseUrl && !options.proxyKeyFile) {
|
|
18149
18530
|
throw new Error("A configured Agent Smith server requires a forwarding key file");
|
|
18150
18531
|
}
|
|
18151
|
-
|
|
18532
|
+
mkdirSync5(options.binDir, { recursive: true, mode: 448 });
|
|
18152
18533
|
const proxyKeyFile = options.proxyKeyFile ? validateProxyKeyFile(options.proxyKeyFile) : undefined;
|
|
18153
18534
|
if (options.claudePath) {
|
|
18154
|
-
const claude =
|
|
18535
|
+
const claude = join10(options.binDir, "claude");
|
|
18155
18536
|
const companionDir = installClaudeCompanion(options.binDir);
|
|
18156
|
-
|
|
18537
|
+
writeFileSync4(claude, claudeWrapper(options.claudePath, proxyBaseUrl(options.port, options.baseUrl, "claude-code", "anthropic-messages", options.providerPoolWires), proxyKeyFile, options.keepClaudeLogin === true, companionDir), {
|
|
18157
18538
|
encoding: "utf8",
|
|
18158
18539
|
mode: 448
|
|
18159
18540
|
});
|
|
18160
|
-
|
|
18541
|
+
chmodSync3(claude, 448);
|
|
18161
18542
|
}
|
|
18162
18543
|
if (options.codexPath) {
|
|
18163
|
-
const codex =
|
|
18164
|
-
|
|
18544
|
+
const codex = join10(options.binDir, "codex");
|
|
18545
|
+
writeFileSync4(codex, codexWrapper(options.codexPath, proxyBaseUrl(options.port, options.baseUrl, "codex", "openai-responses", options.providerPoolWires), proxyKeyFile), {
|
|
18165
18546
|
encoding: "utf8",
|
|
18166
18547
|
mode: 448
|
|
18167
18548
|
});
|
|
18168
|
-
|
|
18549
|
+
chmodSync3(codex, 448);
|
|
18169
18550
|
}
|
|
18170
18551
|
}
|
|
18171
18552
|
function defaultShellProfile() {
|
|
18172
18553
|
const shell = process.env.SHELL?.split("/").pop();
|
|
18173
|
-
return
|
|
18554
|
+
return join10(homedir2(), shell === "bash" ? ".bashrc" : ".zshrc");
|
|
18174
18555
|
}
|
|
18175
18556
|
function getLocalRoutingPaths() {
|
|
18176
18557
|
const configDir = dirname5(resolveConfigPath());
|
|
18177
18558
|
return {
|
|
18178
|
-
binDir:
|
|
18559
|
+
binDir: join10(configDir, "client-bin"),
|
|
18179
18560
|
shellProfile: defaultShellProfile(),
|
|
18180
|
-
statePath:
|
|
18561
|
+
statePath: join10(configDir, "client-routing.json")
|
|
18181
18562
|
};
|
|
18182
18563
|
}
|
|
18183
18564
|
function isExecutable(path) {
|
|
@@ -18192,17 +18573,17 @@ function findClientExecutable(name, binDir, path) {
|
|
|
18192
18573
|
for (const directory of path.split(":")) {
|
|
18193
18574
|
if (!directory || directory === binDir)
|
|
18194
18575
|
continue;
|
|
18195
|
-
const candidate =
|
|
18576
|
+
const candidate = join10(directory, name);
|
|
18196
18577
|
if (isExecutable(candidate))
|
|
18197
18578
|
return candidate;
|
|
18198
18579
|
}
|
|
18199
18580
|
throw new Error(`${name} was not found in PATH`);
|
|
18200
18581
|
}
|
|
18201
18582
|
function readSavedClientPaths(statePath) {
|
|
18202
|
-
if (!
|
|
18583
|
+
if (!existsSync4(statePath))
|
|
18203
18584
|
return null;
|
|
18204
18585
|
try {
|
|
18205
|
-
const saved = JSON.parse(
|
|
18586
|
+
const saved = JSON.parse(readFileSync6(statePath, "utf8"));
|
|
18206
18587
|
if (typeof saved === "object" && saved !== null) {
|
|
18207
18588
|
const { claudePath, codexPath } = saved;
|
|
18208
18589
|
const validClaude = typeof claudePath === "string" && isExecutable(claudePath) ? claudePath : undefined;
|
|
@@ -18220,10 +18601,10 @@ function installedRoutingStatus(paths2 = {}) {
|
|
|
18220
18601
|
const shellProfile = paths2.shellProfile ?? defaults.shellProfile;
|
|
18221
18602
|
let shellProfileHasRouting = false;
|
|
18222
18603
|
try {
|
|
18223
|
-
shellProfileHasRouting =
|
|
18604
|
+
shellProfileHasRouting = existsSync4(shellProfile) && readFileSync6(shellProfile, "utf8").includes(ROUTING_BEGIN);
|
|
18224
18605
|
} catch {}
|
|
18225
18606
|
return {
|
|
18226
|
-
enabled: shellProfileHasRouting && (
|
|
18607
|
+
enabled: shellProfileHasRouting && (existsSync4(join10(binDir, "claude")) || existsSync4(join10(binDir, "codex"))),
|
|
18227
18608
|
binDir,
|
|
18228
18609
|
shellProfile
|
|
18229
18610
|
};
|
|
@@ -18240,28 +18621,48 @@ function getLocalRoutingStatus(paths2 = {}) {
|
|
|
18240
18621
|
}
|
|
18241
18622
|
function configClientWrappersInstalled(paths2 = {}) {
|
|
18242
18623
|
const binDir = paths2.binDir ?? getLocalRoutingPaths().binDir;
|
|
18243
|
-
const files = ["claude", "codex"].map((name) =>
|
|
18244
|
-
return installedRoutingStatus(paths2).enabled && files.length > 0 && files.every((path) =>
|
|
18624
|
+
const files = ["claude", "codex"].map((name) => join10(binDir, name)).filter(existsSync4);
|
|
18625
|
+
return installedRoutingStatus(paths2).enabled && files.length > 0 && files.every((path) => readFileSync6(path, "utf8").includes(CONFIG_CLIENT_MARKER));
|
|
18245
18626
|
}
|
|
18246
18627
|
async function launchConfiguredClient(configPath, tool, target, claudeCompanionDir, args) {
|
|
18247
18628
|
if (tool !== "claude" && tool !== "codex")
|
|
18248
18629
|
throw new Error("Unknown Agent Smith client");
|
|
18630
|
+
dropInheritedForwardProxy(process.env);
|
|
18249
18631
|
const client2 = readClientRouting(configPath);
|
|
18250
|
-
|
|
18632
|
+
let env = { ...process.env };
|
|
18251
18633
|
delete env.AGENT_SMITH_ADMIN_KEY;
|
|
18252
18634
|
delete env.AGENT_SMITH_PROXY_KEY;
|
|
18253
18635
|
let command = [target, ...args];
|
|
18254
18636
|
let relay;
|
|
18637
|
+
let forwardProxy;
|
|
18255
18638
|
if (client2.enabled) {
|
|
18256
18639
|
if (!client2.baseUrl || !client2.proxyKeyFile)
|
|
18257
18640
|
throw new Error("client.base_url and client.proxy_key_file are required");
|
|
18258
18641
|
const keyFile = validateProxyKeyFile(client2.proxyKeyFile);
|
|
18259
18642
|
const wires = Object.keys(new Config(configPath).getProviderPools());
|
|
18260
|
-
|
|
18261
|
-
|
|
18262
|
-
|
|
18263
|
-
|
|
18264
|
-
|
|
18643
|
+
const certificate = tool === "claude" && client2.remoteControl !== false ? remoteControlCertificate(configPath) : undefined;
|
|
18644
|
+
if (certificate) {
|
|
18645
|
+
forwardProxy = await startForwardProxy({
|
|
18646
|
+
poolUrl: proxyBaseUrl(8080, client2.baseUrl, "claude-code", "anthropic-messages", wires),
|
|
18647
|
+
deviceKey: readFileSync6(keyFile, "utf8").trim(),
|
|
18648
|
+
certificate
|
|
18649
|
+
});
|
|
18650
|
+
env = forwardProxyClientEnv(env, forwardProxy.url, certificate.caFile);
|
|
18651
|
+
command = [
|
|
18652
|
+
target,
|
|
18653
|
+
"--settings",
|
|
18654
|
+
BASH_WITHOUT_PROXY_SETTINGS,
|
|
18655
|
+
"--plugin-dir",
|
|
18656
|
+
claudeCompanionDir,
|
|
18657
|
+
...args
|
|
18658
|
+
];
|
|
18659
|
+
} else {
|
|
18660
|
+
if (client2.baseUrl.startsWith("https://"))
|
|
18661
|
+
relay = startClientRelay(client2.baseUrl);
|
|
18662
|
+
const base = proxyBaseUrl(8080, relay?.url ?? client2.baseUrl, tool === "claude" ? "claude-code" : "codex", tool === "claude" ? "anthropic-messages" : "openai-responses", wires);
|
|
18663
|
+
const script = tool === "claude" ? claudeWrapper(target, base, keyFile, client2.keepClaudeLogin === true, claudeCompanionDir) : codexWrapper(target, base, keyFile);
|
|
18664
|
+
command = ["/bin/sh", "-c", script, "--", ...args];
|
|
18665
|
+
}
|
|
18265
18666
|
}
|
|
18266
18667
|
const child = Bun.spawn(command, {
|
|
18267
18668
|
env,
|
|
@@ -18282,6 +18683,7 @@ async function launchConfiguredClient(configPath, tool, target, claudeCompanionD
|
|
|
18282
18683
|
for (const [signal, listener] of Object.entries(forward))
|
|
18283
18684
|
process.off(signal, listener);
|
|
18284
18685
|
relay?.stop();
|
|
18686
|
+
forwardProxy?.stop();
|
|
18285
18687
|
}
|
|
18286
18688
|
}
|
|
18287
18689
|
async function enableLocalClientRouting(options) {
|
|
@@ -18314,14 +18716,14 @@ async function enableLocalClientRouting(options) {
|
|
|
18314
18716
|
clientConfigPath: options.clientConfigPath,
|
|
18315
18717
|
providerPoolWires: options.providerPoolWires ?? (options.clientConfigPath ? [] : Object.keys(new Config().getProviderPools()))
|
|
18316
18718
|
});
|
|
18317
|
-
const existingProfile =
|
|
18318
|
-
|
|
18319
|
-
|
|
18320
|
-
|
|
18719
|
+
const existingProfile = existsSync4(shellProfile) ? readFileSync6(shellProfile, "utf8") : "";
|
|
18720
|
+
writeFileSync4(shellProfile, updateShellProfileForRouting(existingProfile, binDir), { encoding: "utf8", mode: 384 });
|
|
18721
|
+
mkdirSync5(dirname5(statePath), { recursive: true, mode: 448 });
|
|
18722
|
+
writeFileSync4(statePath, JSON.stringify({ claudePath, codexPath }), {
|
|
18321
18723
|
encoding: "utf8",
|
|
18322
18724
|
mode: 384
|
|
18323
18725
|
});
|
|
18324
|
-
|
|
18726
|
+
chmodSync3(statePath, 384);
|
|
18325
18727
|
return getLocalRoutingStatus({ binDir, shellProfile, statePath });
|
|
18326
18728
|
}
|
|
18327
18729
|
function disableLocalClientRouting(paths2 = {}) {
|
|
@@ -18329,19 +18731,19 @@ function disableLocalClientRouting(paths2 = {}) {
|
|
|
18329
18731
|
const binDir = paths2.binDir ?? defaults.binDir;
|
|
18330
18732
|
const shellProfile = paths2.shellProfile ?? defaults.shellProfile;
|
|
18331
18733
|
const statePath = paths2.statePath ?? defaults.statePath;
|
|
18332
|
-
if (
|
|
18333
|
-
|
|
18734
|
+
if (existsSync4(shellProfile)) {
|
|
18735
|
+
writeFileSync4(shellProfile, removeRoutingFromShellProfile(readFileSync6(shellProfile, "utf8")), { encoding: "utf8", mode: 384 });
|
|
18334
18736
|
}
|
|
18335
18737
|
for (const name of ["claude", "codex"]) {
|
|
18336
|
-
const wrapper =
|
|
18337
|
-
if (
|
|
18738
|
+
const wrapper = join10(binDir, name);
|
|
18739
|
+
if (existsSync4(wrapper))
|
|
18338
18740
|
unlinkSync3(wrapper);
|
|
18339
18741
|
}
|
|
18340
|
-
|
|
18742
|
+
rmSync3(join10(binDir, CLAUDE_COMPANION_DIRNAME), {
|
|
18341
18743
|
force: true,
|
|
18342
18744
|
recursive: true
|
|
18343
18745
|
});
|
|
18344
|
-
if (
|
|
18746
|
+
if (existsSync4(statePath))
|
|
18345
18747
|
unlinkSync3(statePath);
|
|
18346
18748
|
return getLocalRoutingStatus({ binDir, shellProfile, statePath });
|
|
18347
18749
|
}
|
|
@@ -18367,9 +18769,10 @@ export PATH=${shellDoubleQuote(`${binDir}:$PATH`)}
|
|
|
18367
18769
|
${ROUTING_END}
|
|
18368
18770
|
`;
|
|
18369
18771
|
}
|
|
18370
|
-
var ROUTING_BEGIN = "# >>> agent-smith local routing >>>", ROUTING_END = "# <<< agent-smith local routing <<<", LOCAL_CLIENT_KEY = "agent-smith-local", CLAUDE_COMPANION_DIRNAME = "agent-smith-claude-companion", CLAUDE_COMPANION_FILES, CONFIG_CLIENT_MARKER = "# agent-smith config-driven client";
|
|
18772
|
+
var ROUTING_BEGIN = "# >>> agent-smith local routing >>>", ROUTING_END = "# <<< agent-smith local routing <<<", LOCAL_CLIENT_KEY = "agent-smith-local", CLAUDE_COMPANION_DIRNAME = "agent-smith-claude-companion", CLAUDE_COMPANION_FILES, CONFIG_CLIENT_MARKER = "# agent-smith config-driven client", PROXY_VARIABLES, NO_PROXY_HOSTS = "localhost,127.0.0.1,::1", BASH_WITHOUT_PROXY_SETTINGS;
|
|
18371
18773
|
var init_local_routing = __esm(() => {
|
|
18372
18774
|
init_src4();
|
|
18775
|
+
init_client_forward_proxy();
|
|
18373
18776
|
init_client_relay();
|
|
18374
18777
|
CLAUDE_COMPANION_FILES = [
|
|
18375
18778
|
".claude-plugin/plugin.json",
|
|
@@ -18377,6 +18780,21 @@ var init_local_routing = __esm(() => {
|
|
|
18377
18780
|
"hooks/provider-choice.js",
|
|
18378
18781
|
"hooks/register.js"
|
|
18379
18782
|
];
|
|
18783
|
+
PROXY_VARIABLES = ["HTTPS_PROXY", "https_proxy"];
|
|
18784
|
+
BASH_WITHOUT_PROXY_SETTINGS = JSON.stringify({
|
|
18785
|
+
hooks: {
|
|
18786
|
+
SessionStart: [
|
|
18787
|
+
{
|
|
18788
|
+
hooks: [
|
|
18789
|
+
{
|
|
18790
|
+
type: "command",
|
|
18791
|
+
command: `[ -z "$CLAUDE_ENV_FILE" ] || echo 'unset ${PROXY_VARIABLES.join(" ")}' >> "$CLAUDE_ENV_FILE"`
|
|
18792
|
+
}
|
|
18793
|
+
]
|
|
18794
|
+
}
|
|
18795
|
+
]
|
|
18796
|
+
}
|
|
18797
|
+
});
|
|
18380
18798
|
});
|
|
18381
18799
|
|
|
18382
18800
|
// src/core/logs.ts
|
|
@@ -18584,7 +19002,7 @@ var init_core = __esm(() => {
|
|
|
18584
19002
|
});
|
|
18585
19003
|
|
|
18586
19004
|
// src/core/client-command.ts
|
|
18587
|
-
import { existsSync as
|
|
19005
|
+
import { existsSync as existsSync5 } from "fs";
|
|
18588
19006
|
import { parseArgs as parseArgs2 } from "util";
|
|
18589
19007
|
async function runClientCommand(argv) {
|
|
18590
19008
|
const { positionals, values } = parseArgs2({
|
|
@@ -18597,7 +19015,7 @@ async function runClientCommand(argv) {
|
|
|
18597
19015
|
if (positionals.length !== 1 || !["on", "off", "status"].includes(command ?? ""))
|
|
18598
19016
|
throw new Error("usage: agent-smith client on|off|status [--raw]");
|
|
18599
19017
|
const configPath = resolveConfigPath();
|
|
18600
|
-
const client2 =
|
|
19018
|
+
const client2 = existsSync5(configPath) ? readClientRouting(configPath) : { enabled: false };
|
|
18601
19019
|
let installed = configClientWrappersInstalled();
|
|
18602
19020
|
if (command !== "status") {
|
|
18603
19021
|
const enabled = command === "on";
|
|
@@ -18803,15 +19221,15 @@ var init_menubar = __esm(() => {
|
|
|
18803
19221
|
|
|
18804
19222
|
// src/core/menubar-command.ts
|
|
18805
19223
|
import {
|
|
18806
|
-
chmodSync as
|
|
18807
|
-
existsSync as
|
|
18808
|
-
mkdirSync as
|
|
18809
|
-
readFileSync as
|
|
18810
|
-
rmSync as
|
|
18811
|
-
writeFileSync as
|
|
19224
|
+
chmodSync as chmodSync4,
|
|
19225
|
+
existsSync as existsSync6,
|
|
19226
|
+
mkdirSync as mkdirSync6,
|
|
19227
|
+
readFileSync as readFileSync7,
|
|
19228
|
+
rmSync as rmSync4,
|
|
19229
|
+
writeFileSync as writeFileSync5
|
|
18812
19230
|
} from "fs";
|
|
18813
19231
|
import { homedir as homedir3 } from "os";
|
|
18814
|
-
import { dirname as dirname6, join as
|
|
19232
|
+
import { dirname as dirname6, join as join11 } from "path";
|
|
18815
19233
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
18816
19234
|
import { parseArgs as parseArgs3 } from "util";
|
|
18817
19235
|
function swiftBarPluginDirectory() {
|
|
@@ -18826,12 +19244,12 @@ function swiftBarPluginDirectory() {
|
|
|
18826
19244
|
const path = result.exitCode === 0 ? result.stdout.toString().trim() : "";
|
|
18827
19245
|
if (!path)
|
|
18828
19246
|
return null;
|
|
18829
|
-
return path.startsWith("~/") ?
|
|
19247
|
+
return path.startsWith("~/") ? join11(homedir3(), path.slice(2)) : path;
|
|
18830
19248
|
}
|
|
18831
19249
|
async function remoteScript() {
|
|
18832
19250
|
const built = new URL("./pool-status.js", import.meta.url);
|
|
18833
|
-
if (
|
|
18834
|
-
return
|
|
19251
|
+
if (existsSync6(built))
|
|
19252
|
+
return readFileSync7(built, "utf8");
|
|
18835
19253
|
const output = await Bun.build({
|
|
18836
19254
|
entrypoints: [fileURLToPath2(new URL("../pool-status.ts", import.meta.url))],
|
|
18837
19255
|
target: "bun"
|
|
@@ -18859,7 +19277,7 @@ function cliBinDirectories() {
|
|
|
18859
19277
|
return [
|
|
18860
19278
|
...new Set([
|
|
18861
19279
|
...found,
|
|
18862
|
-
|
|
19280
|
+
join11(homedir3(), ".bun", "bin"),
|
|
18863
19281
|
"/opt/homebrew/bin",
|
|
18864
19282
|
"/usr/local/bin"
|
|
18865
19283
|
])
|
|
@@ -18868,7 +19286,7 @@ function cliBinDirectories() {
|
|
|
18868
19286
|
function defaultDeps() {
|
|
18869
19287
|
return {
|
|
18870
19288
|
configPath: resolveConfigPath(),
|
|
18871
|
-
cachePath:
|
|
19289
|
+
cachePath: join11(homedir3(), "Library", "Caches", "agent-smith", "menubar-status.json"),
|
|
18872
19290
|
binDirectories: cliBinDirectories,
|
|
18873
19291
|
pluginDirectory: swiftBarPluginDirectory,
|
|
18874
19292
|
fetchStatus: fetchPoolStatus,
|
|
@@ -18878,7 +19296,7 @@ function defaultDeps() {
|
|
|
18878
19296
|
}
|
|
18879
19297
|
function readCache(path) {
|
|
18880
19298
|
try {
|
|
18881
|
-
const cached = JSON.parse(
|
|
19299
|
+
const cached = JSON.parse(readFileSync7(path, "utf8"));
|
|
18882
19300
|
return typeof cached?.fetchedAt === "number" && Array.isArray(cached?.status?.accounts) ? cached : null;
|
|
18883
19301
|
} catch {
|
|
18884
19302
|
return null;
|
|
@@ -18893,8 +19311,8 @@ async function loadSnapshot(deps) {
|
|
|
18893
19311
|
fetchedAt: deps.now(),
|
|
18894
19312
|
status: await deps.fetchStatus(host)
|
|
18895
19313
|
};
|
|
18896
|
-
|
|
18897
|
-
|
|
19314
|
+
mkdirSync6(dirname6(deps.cachePath), { recursive: true, mode: 448 });
|
|
19315
|
+
writeFileSync5(deps.cachePath, JSON.stringify(cached), { mode: 384 });
|
|
18898
19316
|
return { cached, error: null };
|
|
18899
19317
|
} catch (error) {
|
|
18900
19318
|
return {
|
|
@@ -18920,19 +19338,19 @@ function install(deps) {
|
|
|
18920
19338
|
throw new Error("SwiftBar has no plugin folder. On macOS, install it (brew install --cask swiftbar), open it once and pick a plugin folder, then run this again.");
|
|
18921
19339
|
if (!readClientRouting(deps.configPath).poolSshHost)
|
|
18922
19340
|
throw new Error(`Set client.pool_ssh_host in ${deps.configPath} first`);
|
|
18923
|
-
const path =
|
|
18924
|
-
|
|
18925
|
-
|
|
19341
|
+
const path = join11(directory, PLUGIN_FILE);
|
|
19342
|
+
writeFileSync5(path, pluginScript(deps.binDirectories()));
|
|
19343
|
+
chmodSync4(path, 493);
|
|
18926
19344
|
deps.log(`Installed ${path}. SwiftBar shows the pool shortly and refreshes it every 5 minutes.`);
|
|
18927
19345
|
}
|
|
18928
19346
|
function uninstall(deps) {
|
|
18929
19347
|
const directory = deps.pluginDirectory();
|
|
18930
|
-
const path = directory ?
|
|
18931
|
-
if (!path || !
|
|
19348
|
+
const path = directory ? join11(directory, PLUGIN_FILE) : null;
|
|
19349
|
+
if (!path || !existsSync6(path) || !readFileSync7(path, "utf8").includes(PLUGIN_MARKER)) {
|
|
18932
19350
|
deps.log("No Agent Smith menu bar plugin is installed.");
|
|
18933
19351
|
return;
|
|
18934
19352
|
}
|
|
18935
|
-
|
|
19353
|
+
rmSync4(path);
|
|
18936
19354
|
deps.log(`Removed ${path}.`);
|
|
18937
19355
|
}
|
|
18938
19356
|
async function runMenubarCommand(argv, overrides = {}) {
|
|
@@ -18990,13 +19408,13 @@ __export(exports_theme, {
|
|
|
18990
19408
|
NAV_ITEMS: () => NAV_ITEMS,
|
|
18991
19409
|
C: () => C
|
|
18992
19410
|
});
|
|
18993
|
-
import { existsSync as
|
|
19411
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync7, readFileSync as readFileSync8, writeFileSync as writeFileSync6 } from "fs";
|
|
18994
19412
|
import { homedir as homedir4 } from "os";
|
|
18995
|
-
import { join as
|
|
19413
|
+
import { join as join12 } from "path";
|
|
18996
19414
|
function loadPersistedThemeId() {
|
|
18997
19415
|
try {
|
|
18998
|
-
if (
|
|
18999
|
-
const data = JSON.parse(
|
|
19416
|
+
if (existsSync7(CONFIG_FILE)) {
|
|
19417
|
+
const data = JSON.parse(readFileSync8(CONFIG_FILE, "utf-8"));
|
|
19000
19418
|
if (data?.themeId && getThemeById(data.themeId)) {
|
|
19001
19419
|
return data.themeId;
|
|
19002
19420
|
}
|
|
@@ -19006,10 +19424,10 @@ function loadPersistedThemeId() {
|
|
|
19006
19424
|
}
|
|
19007
19425
|
function persistThemeId(themeId) {
|
|
19008
19426
|
try {
|
|
19009
|
-
if (!
|
|
19010
|
-
|
|
19427
|
+
if (!existsSync7(CONFIG_DIR)) {
|
|
19428
|
+
mkdirSync7(CONFIG_DIR, { recursive: true });
|
|
19011
19429
|
}
|
|
19012
|
-
|
|
19430
|
+
writeFileSync6(CONFIG_FILE, JSON.stringify({ themeId }), "utf-8");
|
|
19013
19431
|
} catch {}
|
|
19014
19432
|
}
|
|
19015
19433
|
function setTuiTheme(themeId) {
|
|
@@ -19034,8 +19452,8 @@ function getAvailableThemes() {
|
|
|
19034
19452
|
var CONFIG_DIR, CONFIG_FILE, currentThemeId, currentColors, C, NAV_ITEMS, SCREEN_TITLES;
|
|
19035
19453
|
var init_theme = __esm(() => {
|
|
19036
19454
|
init_src16();
|
|
19037
|
-
CONFIG_DIR =
|
|
19038
|
-
CONFIG_FILE =
|
|
19455
|
+
CONFIG_DIR = join12(homedir4(), ".config", "agent-smith");
|
|
19456
|
+
CONFIG_FILE = join12(CONFIG_DIR, "theme.json");
|
|
19039
19457
|
currentThemeId = loadPersistedThemeId();
|
|
19040
19458
|
currentColors = themeToTuiColors(getThemeById(currentThemeId) ?? getDefaultTheme("dark"));
|
|
19041
19459
|
C = new Proxy({}, {
|