Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Desktop: agents use the system's proxy (#125)

riverisbuilding613350d30602
diff --git a/.changeset/agents-system-proxy.md b/.changeset/agents-system-proxy.md
new file mode 100644
index 0000000..43e220a
--- /dev/null
+++ b/.changeset/agents-system-proxy.md
@@ -0,0 +1,5 @@
+---
+"@openfilm/desktop": patch
+---
+
+Agents use your system's proxy. When a proxy app turns on the system proxy (on Windows or macOS) and you have not set HTTPS_PROXY yourself, OpenFilm gives the agents that proxy as HTTPS_PROXY, HTTP_PROXY and NO_PROXY. Codex no longer waits through five WebSocket timeouts before your first reply.
diff --git a/apps/desktop/src/environment.mjs b/apps/desktop/src/environment.mjs
index 5187de8..b90880c 100644
--- a/apps/desktop/src/environment.mjs
+++ b/apps/desktop/src/environment.mjs
@@ -6,10 +6,11 @@
* ANTHROPIC_BASE_URL and the like, which an agent started here would wrongly inherit. Opened from the Dock, it has
* only PATH=/usr/bin:/bin:/usr/sbin:/sbin, where neither Homebrew's ffmpeg nor the person's `claude` can be found.
* Both get the same answer: the OS identity variables, plus the PATH (and proxies, and where the agents keep their
- * sign-in) of the person's login shell. On Windows there is no login shell, and the PATH a program inherits from
- * Explorer can be old (what an installer added since the person signed in is missing): the PATH is read where Windows
- * keeps it now, the registry (with the Windows system variables a program needs). Either is read again while the app
- * runs, so an agent installed meanwhile is found without restarting it. Reads no Electron module, so `node --test`
+ * sign-in) of the person's login shell; with no proxy set there, the system's own (a proxy app's). On Windows there
+ * is no login shell, and the PATH a program inherits from Explorer can be old (what an installer added since the
+ * person signed in is missing): the PATH is read where Windows keeps it now, the registry (with the Windows system
+ * variables a program needs). Either is read again while the app runs, so an agent installed meanwhile is found
+ * without restarting it. Reads no Electron module, so `node --test`
* can use it.
*/
import { execFile, spawn } from 'node:child_process';
@@ -136,15 +137,16 @@ function captureLoginShell(shell, env, timeoutMs) {
}
/**
- * `reg query <key>` output, read: each value's name and its text (REG_SZ and REG_EXPAND_SZ only). `expandable`, when
- * given, gets the names of the REG_EXPAND_SZ ones: Windows expands those, never a REG_SZ.
- * @param {string} text @param {Set<string>} [expandable]
+ * `reg query <key>` output, read: each value's name and its text, of `types` (REG_SZ and REG_EXPAND_SZ unless asked
+ * for others). `expandable`, when given, gets the names of the REG_EXPAND_SZ ones: Windows expands those, never a
+ * REG_SZ.
+ * @param {string} text @param {Set<string>} [expandable] @param {string[]} [types]
*/
-export function parseRegQuery(text, expandable) {
+export function parseRegQuery(text, expandable, types = ['REG_SZ', 'REG_EXPAND_SZ']) {
const values = {};
for (const line of String(text ?? '').split(/\r?\n/)) {
- const m = /^\s{2,}(\S(?:.*?\S)?)\s{2,}(REG_SZ|REG_EXPAND_SZ)\s{2,}(.*)$/.exec(line);
- if (!m) continue;
+ const m = /^\s{2,}(\S(?:.*?\S)?)\s{2,}(REG_[A-Z_]+)\s{2,}(.*)$/.exec(line);
+ if (!m || !types.includes(m[2])) continue;
values[m[1]] = m[3].trim();
if (m[2] === 'REG_EXPAND_SZ') expandable?.add(m[1]);
}
@@ -188,13 +190,13 @@ export function windowsFreshPath({ machine = {}, user = {}, source = process.env
const REG_MACHINE = 'HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment';
const REG_USER = 'HKCU\\Environment';
-function regQuery(key, timeoutMs) {
+function regQuery(key, timeoutMs, types = undefined) {
return new Promise((resolve) => {
const reg = join(process.env.SystemRoot ?? 'C:\\Windows', 'System32', 'reg.exe');
execFile(reg, ['query', key], { timeout: timeoutMs, encoding: 'utf8', windowsHide: true }, (error, stdout) => {
if (error) return resolve(null);
const expandable = new Set();
- resolve({ values: parseRegQuery(stdout, expandable), expandable });
+ resolve({ values: parseRegQuery(stdout, expandable, types), expandable });
});
});
}
@@ -263,16 +265,136 @@ async function readLoginShell(source, timeoutMs) {
return shellVariables(parsed, base.HOME);
}
+/* ── the system's proxy ──
+ A proxy app usually turns on the system's proxy only: browsers use it, and an agent's HTTPS
+ calls may too, but not every connection it makes (Codex's WebSocket to its model goes straight out, times out five
+ times, then falls back to HTTPS). Unless the person set proxy variables themselves, the agents are given the
+ system's proxy as HTTPS_PROXY, HTTP_PROXY and NO_PROXY, as a terminal with the proxy app's variables would have. */
+
+const SET_PROXY_KEYS = ['https_proxy', 'http_proxy', 'all_proxy'];
+const REG_INTERNET = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings';
+
+/**
+ * `scutil --proxy` output, read: the keys of its top dictionary (HTTPSEnable, HTTPSProxy, HTTPSPort…) and the entries
+ * of its ExceptionsList; the per-interface ones under __SCOPED__ left out.
+ */
+export function parseScutilProxy(text) {
+ const values = {};
+ const exceptions = [];
+ let depth = 0;
+ let list = null;
+ for (const line of String(text ?? '').split(/\r?\n/)) {
+ const m = /^\s*([^\s:][^:]*?)\s*:\s*(.*?)\s*$/.exec(line);
+ if (m && depth === 1 && !m[2].endsWith('{')) values[m[1]] = m[2];
+ if (m && depth === 2 && list === 'ExceptionsList') exceptions.push(m[2]);
+ if (line.trim().endsWith('{')) { depth += 1; if (depth === 2) list = m?.[1] ?? null; }
+ if (line.trim() === '}') { depth -= 1; if (depth < 2) list = null; }
+ }
+ return { values, exceptions };
+}
+
+/** macOS's proxy (parseScutilProxy's), each kind as host:port; null when none is on. */
+export function macProxy({ values = {}, exceptions = [] } = {}) {
+ const at = (kind, port) => (values[`${kind}Enable`] === '1' && values[`${kind}Proxy`] ? `${values[`${kind}Proxy`]}:${values[`${kind}Port`] || port}` : null);
+ const found = { http: at('HTTP', 80), https: at('HTTPS', 443), socks: at('SOCKS', 1080), bypass: exceptions };
+ return found.http || found.https || found.socks ? found : null;
+}
+
+/**
+ * Windows's proxy (the Internet settings' ProxyEnable, ProxyServer, ProxyOverride), each kind as host:port; null when
+ * it is off. ProxyServer is one `host:port` for all, or `http=…;https=…;socks=…`.
+ */
+export function windowsProxy(settings = {}) {
+ if (!/^(?:0x)?0*1$/i.test(String(settings.ProxyEnable ?? '').trim())) return null;
+ const server = String(settings.ProxyServer ?? '').trim();
+ if (!server) return null;
+ const by = {};
+ if (!server.includes('=')) by.http = by.https = server;
+ else {
+ for (const part of server.split(';')) {
+ const eq = part.indexOf('=');
+ if (eq > 0 && part.slice(eq + 1).trim()) by[part.slice(0, eq).trim().toLowerCase()] = part.slice(eq + 1).trim();
+ }
+ }
+ const found = { http: by.http ?? null, https: by.https ?? null, socks: by.socks ?? null, bypass: String(settings.ProxyOverride ?? '').split(';') };
+ return found.http || found.https || found.socks ? found : null;
+}
+
+/**
+ * A proxy's exceptions as NO_PROXY has them: this computer always; `*.example.com` as `.example.com`, `10.*` and
+ * `169.254/16` as address ranges; Windows's `<local>` and other wildcards, which NO_PROXY cannot say, left out.
+ */
+export function noProxyList(bypass = []) {
+ const out = ['localhost', '127.0.0.1', '::1'];
+ for (const raw of bypass) {
+ const entry = String(raw ?? '').trim();
+ if (!entry || entry === '<local>') continue;
+ let m;
+ if ((m = /^((?:\d{1,3}\.){1,3})\*$/.exec(entry))) {
+ const octets = m[1].slice(0, -1).split('.');
+ out.push(`${[...octets, '0', '0', '0'].slice(0, 4).join('.')}/${octets.length * 8}`);
+ } else if ((m = /^(\d{1,3}(?:\.\d{1,3}){0,3})\/(\d{1,2})$/.exec(entry))) {
+ out.push(`${[...m[1].split('.'), '0', '0', '0'].slice(0, 4).join('.')}/${m[2]}`);
+ } else if (entry.startsWith('*.') && !entry.slice(2).includes('*')) out.push(entry.slice(1));
+ else if (!entry.includes('*')) out.push(entry);
+ }
+ return [...new Set(out)].join(',');
+}
+
+/** A proxy (macProxy's, windowsProxy's) as the variables programs read, in both cases; none: {}. */
+export function proxyVariables(found) {
+ if (!found) return {};
+ const env = {};
+ const set = (key, value) => { env[key] = value; env[key.toLowerCase()] = value; };
+ const url = (at, scheme) => (/^[a-z][a-z\d+.-]*:\/\//i.test(at) ? at : `${scheme}://${at}`);
+ const https = found.https ?? found.http;
+ const http = found.http ?? found.https;
+ if (https) set('HTTPS_PROXY', url(https, 'http'));
+ if (http) set('HTTP_PROXY', url(http, 'http'));
+ if (!https && !http && found.socks) set('ALL_PROXY', url(found.socks, 'socks5h'));
+ set('NO_PROXY', noProxyList(found.bypass));
+ return env;
+}
+
+/**
+ * The person's variables, with the system's proxy (`proxy`, proxyVariables') unless they set one themselves; a
+ * NO_PROXY of theirs stays.
+ */
+export function withSystemProxy(env, proxy) {
+ if (Object.keys(env).some((key) => SET_PROXY_KEYS.includes(key.toLowerCase()) && env[key])) return env;
+ return { ...proxy, ...env };
+}
+
+/** The system's proxy as proxy variables (Windows, macOS); {} when none is on, or it could not be read. */
+async function systemProxyVariables(timeoutMs) {
+ if (windows) return proxyVariables(windowsProxy((await regQuery(REG_INTERNET, timeoutMs, ['REG_SZ', 'REG_DWORD']))?.values ?? {}));
+ if (process.platform !== 'darwin') return {};
+ const text = await new Promise((resolve) => {
+ execFile('/usr/sbin/scutil', ['--proxy'], { timeout: timeoutMs, encoding: 'utf8' }, (error, stdout) => resolve(error ? '' : stdout));
+ });
+ return proxyVariables(macProxy(parseScutilProxy(text)));
+}
+
+/** The person's variables (the registry's, or the login shell's), with the system's proxy: null when they failed. */
+async function readPersonVariables(source, timeoutMs) {
+ const [env, proxy] = await Promise.all([
+ windows ? readRegistry(source, timeoutMs) : readLoginShell(source, timeoutMs),
+ systemProxyVariables(timeoutMs).catch(() => ({})),
+ ]);
+ return env && withSystemProxy(env, proxy);
+}
+
let shellCache = null;
/**
- * Everything the person's login shell sets (`shellVariables`); on Windows, the registry's. Read again after a minute
+ * Everything the person's login shell sets (`shellVariables`); on Windows, the registry's. With no proxy of their own
+ * there, the system's (withSystemProxy). Read again after a minute
* (Windows: a few seconds), or sooner for a caller that asks (`maxAgeMs`: the agents' list, which is looked at again
* as the person comes back from installing one; `staleOk`, the last one read while a new one is read for next time).
* Never rejects: on failure, the last one read, else the fallback PATH.
* What the agents are given, so they run with what they have in the person's terminal.
*/
export function loginShellVariables({ source = process.env, timeoutMs = 5000, maxAgeMs, staleOk = false } = {}) {
- shellCache ??= environmentCache(() => (windows ? readRegistry(source, timeoutMs) : readLoginShell(source, timeoutMs)), {
+ shellCache ??= environmentCache(() => readPersonVariables(source, timeoutMs), {
forMs: windows ? 5000 : 60_000,
fallback: () => {
pathSource = windows ? 'process' : 'fallback';
diff --git a/apps/desktop/src/environment.test.mjs b/apps/desktop/src/environment.test.mjs
index c23eeec..26e802d 100644
--- a/apps/desktop/src/environment.test.mjs
+++ b/apps/desktop/src/environment.test.mjs
@@ -3,8 +3,8 @@ import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import {
- childEnvironment, environmentCache, expandWindowsVars, lastPathSource, loginShellVariables, parseRegQuery, registryVariables, shellSubset,
- shellVariables, windowsFreshPath,
+ childEnvironment, environmentCache, expandWindowsVars, lastPathSource, loginShellVariables, macProxy, noProxyList, parseRegQuery,
+ parseScutilProxy, proxyVariables, registryVariables, shellSubset, shellVariables, windowsFreshPath, windowsProxy, withSystemProxy,
} from './environment.mjs';
const PROFILE = ['C:', 'Users', 'someone'].join('\\');
@@ -140,3 +140,80 @@ test("the agents get all the person's shell sets, but what the shell and its ter
assert.ok(all.PATH.startsWith('/opt/homebrew/bin:/usr/bin'));
assert.deepEqual(Object.keys(shellSubset(all, '/home/me/')).sort(), ['HTTPS_PROXY', 'PATH']);
});
+
+const SCUTIL = `<dictionary> {
+ ExceptionsList : <array> {
+ 0 : *.local
+ 1 : 169.254/16
+ }
+ FTPPassive : 1
+ HTTPEnable : 1
+ HTTPPort : 7897
+ HTTPProxy : 127.0.0.1
+ HTTPSEnable : 1
+ HTTPSPort : 7897
+ HTTPSProxy : 127.0.0.1
+ SOCKSEnable : 0
+ __SCOPED__ : <dictionary> {
+ en0 : <dictionary> {
+ HTTPSEnable : 1
+ HTTPSPort : 9999
+ HTTPSProxy : 10.0.0.9
+ }
+ }
+}
+`;
+
+const INTERNET = `\r
+HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\r
+ ProxyEnable REG_DWORD 0x1\r
+ ProxyServer REG_SZ 127.0.0.1:7897\r
+ ProxyOverride REG_SZ localhost;127.*;10.*;172.16.*;192.168.*;*.lan;<local>\r
+`;
+
+test("macOS's proxy is read from its top settings, not an interface's", () => {
+ const found = macProxy(parseScutilProxy(SCUTIL));
+ assert.deepEqual(found, { http: '127.0.0.1:7897', https: '127.0.0.1:7897', socks: null, bypass: ['*.local', '169.254/16'] });
+ assert.equal(macProxy(parseScutilProxy('<dictionary> {\n HTTPEnable : 0\n HTTPSEnable : 0\n}\n')), null);
+ assert.equal(macProxy(parseScutilProxy('')), null);
+});
+
+test("Windows's proxy is read from the Internet settings: one server for all, or one per kind; off is none", () => {
+ const settings = parseRegQuery(INTERNET, undefined, ['REG_SZ', 'REG_DWORD']);
+ assert.equal(settings.ProxyEnable, '0x1');
+ assert.deepEqual(windowsProxy(settings), {
+ http: '127.0.0.1:7897', https: '127.0.0.1:7897', socks: null,
+ bypass: ['localhost', '127.*', '10.*', '172.16.*', '192.168.*', '*.lan', '<local>'],
+ });
+ assert.deepEqual(windowsProxy({ ProxyEnable: '0x1', ProxyServer: 'http=127.0.0.1:8080;https=127.0.0.1:8443;socks=127.0.0.1:1080' }),
+ { http: '127.0.0.1:8080', https: '127.0.0.1:8443', socks: '127.0.0.1:1080', bypass: [''] });
+ assert.equal(windowsProxy({ ProxyEnable: '0x0', ProxyServer: '127.0.0.1:7897' }), null);
+ assert.equal(windowsProxy({ ProxyEnable: '0x1' }), null);
+ assert.equal(windowsProxy(), null);
+});
+
+test('the exceptions become NO_PROXY: this computer always, wildcards as domains and address ranges', () => {
+ assert.equal(noProxyList(['localhost', '127.*', '10.*', '172.16.*', '192.168.*', '*.lan', '<local>', 'intra*', 'example.com', '169.254/16']),
+ 'localhost,127.0.0.1,::1,127.0.0.0/8,10.0.0.0/8,172.16.0.0/16,192.168.0.0/16,.lan,example.com,169.254.0.0/16');
+ assert.equal(noProxyList(), 'localhost,127.0.0.1,::1');
+});
+
+test("the system's proxy is given as the variables programs read, in both cases", () => {
+ const env = proxyVariables({ http: '127.0.0.1:7897', https: null, socks: null, bypass: ['*.lan'] });
+ assert.equal(env.HTTPS_PROXY, 'http://127.0.0.1:7897');
+ assert.equal(env.https_proxy, 'http://127.0.0.1:7897');
+ assert.equal(env.HTTP_PROXY, 'http://127.0.0.1:7897');
+ assert.equal(env.no_proxy, 'localhost,127.0.0.1,::1,.lan');
+ assert.equal(env.ALL_PROXY, undefined);
+ assert.deepEqual(proxyVariables({ http: null, https: null, socks: '127.0.0.1:1080', bypass: [] }).ALL_PROXY, 'socks5h://127.0.0.1:1080');
+ assert.equal(proxyVariables({ http: 'socks5://127.0.0.1:1080', https: null, socks: null, bypass: [] }).HTTPS_PROXY, 'socks5://127.0.0.1:1080');
+ assert.deepEqual(proxyVariables(null), {});
+});
+
+test("the person's own proxy wins over the system's; with none, the system's is added", () => {
+ const proxy = proxyVariables({ http: '127.0.0.1:7897', https: '127.0.0.1:7897', socks: null, bypass: [] });
+ const mine = { PATH: '/usr/bin', https_proxy: 'http://10.0.0.1:3128' };
+ assert.equal(withSystemProxy(mine, proxy), mine);
+ assert.deepEqual(withSystemProxy({ PATH: '/usr/bin', NO_PROXY: 'corp' }, proxy), { ...proxy, PATH: '/usr/bin', NO_PROXY: 'corp' });
+ assert.deepEqual(withSystemProxy({ PATH: '/usr/bin' }, {}), { PATH: '/usr/bin' });
+});