@eduardo191labs/opencode-free-pi 0.7.9 → 0.7.12
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 +20 -0
- package/dist/api/session.js +2 -2
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +6 -0
- package/dist/network/proxy.d.ts +19 -0
- package/dist/network/proxy.js +118 -0
- package/dist/plugin.js +5 -0
- package/package.json +2 -2
- package/src/api/session.ts +5 -2
- package/src/constants.ts +6 -0
- package/src/network/proxy.ts +137 -0
- package/src/plugin.ts +5 -0
package/README.md
CHANGED
|
@@ -221,6 +221,26 @@ Después de iniciar sesión, ejecuta `/reload` en OpenCode o reinícialo.
|
|
|
221
221
|
mientras no haya ningún modelo registrado, así que un arranque sin red (VPN
|
|
222
222
|
caída, DPI bloqueando el gateway) se recupera solo cuando vuelve la conexión.
|
|
223
223
|
No hace falta reiniciar OpenCode ni elegir otra carpeta.
|
|
224
|
+
- **Proxy de VPN o red corporativa**: el CLI oficial de free-pi lee `HTTPS_PROXY`,
|
|
225
|
+
`HTTP_PROXY` y `ALL_PROXY` (en mayúsculas o minúsculas), pero `fetch` de Node
|
|
226
|
+
los ignora salvo que se active. Por eso, si el entorno define un proxy, el
|
|
227
|
+
plugin lo activa al arrancar con `http.setGlobalProxyFromEnv()`: el catálogo,
|
|
228
|
+
la cuota, los anuncios, la visión, los reintentos y las peticiones al gateway
|
|
229
|
+
pasan por el proxy en vez de salir directos y fallar con `ETIMEDOUT`. `ALL_PROXY`
|
|
230
|
+
se copia a `HTTPS_PROXY`/`HTTP_PROXY` porque Node solo lee esos dos. Sin
|
|
231
|
+
variables de proxy no cambia nada. `setGlobalProxyFromEnv()` existe desde Node
|
|
232
|
+
24.5; en un Node anterior el plugin escribe en el log que hay que arrancar
|
|
233
|
+
OpenCode con `NODE_USE_ENV_PROXY=1` (o actualizar Node), porque esa versión
|
|
234
|
+
ignora el proxy en `fetch` aunque se defina el resto. Un proxy SOCKS no se
|
|
235
|
+
puede usar con esta API y también se avisa. En Node 26 la
|
|
236
|
+
verificación fue real: con `HTTPS_PROXY` apuntando a un proxy local, las
|
|
237
|
+
conexiones a `api.freepi.ai`, `models.opencode.ai` y `registry.npmjs.org`
|
|
238
|
+
aparecieron en el proxy y una sesión completa respondió correctamente.
|
|
239
|
+
`fetch` solo admite el proxy de forma global en el proceso, así que la
|
|
240
|
+
activación equivale a arrancar con `NODE_USE_ENV_PROXY=1`: cualquier cliente
|
|
241
|
+
del proceso que respete el proxy global lo usará, y los demás proveedores del
|
|
242
|
+
mismo OpenCode también dejan de fallar detrás de una red que solo permite
|
|
243
|
+
proxy.
|
|
224
244
|
- **Aislamiento por proveedor**: todo lo que añade el plugin —herramientas,
|
|
225
245
|
cabeceras, anuncios, reglas de prompt, compactación, reintentos, topes de
|
|
226
246
|
tokens y ajustes— se aplica solo a `free-pi` y `free-pi-defense`. Los demás
|
package/dist/api/session.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Releases free-pi server-side session slots. */
|
|
2
2
|
import { resolveToken } from "../auth/token.js";
|
|
3
|
-
import { SESSION_RESET_COOLDOWN_MS } from "../constants.js";
|
|
3
|
+
import { SESSION_RESET_COOLDOWN_MS, SESSION_RESET_TIMEOUT_MS, } from "../constants.js";
|
|
4
4
|
import { trimMap } from "../utils/async.js";
|
|
5
5
|
import { resolveBaseUrl } from "../utils/urls.js";
|
|
6
6
|
const CLOSE_LIMIT = 200;
|
|
@@ -14,7 +14,7 @@ function resetRequest(deps, token, sessionID) {
|
|
|
14
14
|
return fetch(new URL("/session/reset", resolveBaseUrl()), {
|
|
15
15
|
method: "POST",
|
|
16
16
|
headers,
|
|
17
|
-
signal: AbortSignal.timeout(
|
|
17
|
+
signal: AbortSignal.timeout(SESSION_RESET_TIMEOUT_MS),
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
/**
|
package/dist/constants.d.ts
CHANGED
|
@@ -168,6 +168,12 @@ export declare const QUOTA_TTL_MS = 30000;
|
|
|
168
168
|
export declare const QUOTA_HTTP_TIMEOUT_MS = 5000;
|
|
169
169
|
export declare const QUOTA_REFRESH_DEBOUNCE_MS = 1000;
|
|
170
170
|
export declare const SESSION_RESET_COOLDOWN_MS = 15000;
|
|
171
|
+
/**
|
|
172
|
+
* Releasing a server slot is a tiny POST, but a VPN or a proxy adds a round
|
|
173
|
+
* trip; 3s let the reset expire before the gateway answered and left the slot
|
|
174
|
+
* locked until the next 429.
|
|
175
|
+
*/
|
|
176
|
+
export declare const SESSION_RESET_TIMEOUT_MS = 6000;
|
|
171
177
|
/**
|
|
172
178
|
* How long a free-pi session must stay quiet before its server slot is closed.
|
|
173
179
|
* Background compaction can start right after the last step, so closing on the
|
package/dist/constants.js
CHANGED
|
@@ -207,6 +207,12 @@ export const QUOTA_TTL_MS = 30_000;
|
|
|
207
207
|
export const QUOTA_HTTP_TIMEOUT_MS = 5_000;
|
|
208
208
|
export const QUOTA_REFRESH_DEBOUNCE_MS = 1_000;
|
|
209
209
|
export const SESSION_RESET_COOLDOWN_MS = 15_000;
|
|
210
|
+
/**
|
|
211
|
+
* Releasing a server slot is a tiny POST, but a VPN or a proxy adds a round
|
|
212
|
+
* trip; 3s let the reset expire before the gateway answered and left the slot
|
|
213
|
+
* locked until the next 429.
|
|
214
|
+
*/
|
|
215
|
+
export const SESSION_RESET_TIMEOUT_MS = 6_000;
|
|
210
216
|
/**
|
|
211
217
|
* How long a free-pi session must stay quiet before its server slot is closed.
|
|
212
218
|
* Background compaction can start right after the last step, so closing on the
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Honors the proxy a VPN or corporate network exports in the environment.
|
|
3
|
+
*
|
|
4
|
+
* The free-pi CLI reads `*_proxy`/`all_proxy` and routes its requests through
|
|
5
|
+
* an agent, so it keeps working when the network only allows traffic through a
|
|
6
|
+
* local proxy. Node's `fetch` ignores those variables unless the process opts
|
|
7
|
+
* in, which left the plugin unable to fetch the catalog, quota, ads or replay a
|
|
8
|
+
* request while the CLI worked. `http.setGlobalProxyFromEnv()` (Node 24.5+)
|
|
9
|
+
* turns the environment proxy on for every request of this process, including
|
|
10
|
+
* `fetch`; when the runtime is older the plugin keeps going direct and says so.
|
|
11
|
+
*/
|
|
12
|
+
import type { Logger } from "../logging.js";
|
|
13
|
+
/** First proxy URL exported in the environment, or undefined. */
|
|
14
|
+
export declare function proxyFromEnvironment(env?: NodeJS.ProcessEnv): string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Enables the environment proxy for this process when one is configured.
|
|
17
|
+
* Returns whether the plugin will use it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function applyEnvironmentProxy(log: Logger, env?: NodeJS.ProcessEnv): boolean;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Honors the proxy a VPN or corporate network exports in the environment.
|
|
3
|
+
*
|
|
4
|
+
* The free-pi CLI reads `*_proxy`/`all_proxy` and routes its requests through
|
|
5
|
+
* an agent, so it keeps working when the network only allows traffic through a
|
|
6
|
+
* local proxy. Node's `fetch` ignores those variables unless the process opts
|
|
7
|
+
* in, which left the plugin unable to fetch the catalog, quota, ads or replay a
|
|
8
|
+
* request while the CLI worked. `http.setGlobalProxyFromEnv()` (Node 24.5+)
|
|
9
|
+
* turns the environment proxy on for every request of this process, including
|
|
10
|
+
* `fetch`; when the runtime is older the plugin keeps going direct and says so.
|
|
11
|
+
*/
|
|
12
|
+
import http from "node:http";
|
|
13
|
+
const DIRECT_VARIABLES = [
|
|
14
|
+
"HTTPS_PROXY",
|
|
15
|
+
"https_proxy",
|
|
16
|
+
"HTTP_PROXY",
|
|
17
|
+
"http_proxy",
|
|
18
|
+
];
|
|
19
|
+
const ALL_VARIABLES = ["ALL_PROXY", "all_proxy"];
|
|
20
|
+
const PROXY_VARIABLES = [...DIRECT_VARIABLES, ...ALL_VARIABLES];
|
|
21
|
+
/** First proxy URL exported in the environment, or undefined. */
|
|
22
|
+
export function proxyFromEnvironment(env = process.env) {
|
|
23
|
+
for (const name of PROXY_VARIABLES) {
|
|
24
|
+
const value = env[name]?.trim();
|
|
25
|
+
if (value)
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
function firstValue(env, names) {
|
|
31
|
+
for (const name of names) {
|
|
32
|
+
const value = env[name]?.trim();
|
|
33
|
+
if (value)
|
|
34
|
+
return { name, value };
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
/** Proxy URL without credentials, safe for the log. */
|
|
39
|
+
function describeProxy(value) {
|
|
40
|
+
try {
|
|
41
|
+
const url = new URL(value);
|
|
42
|
+
const port = url.port ? `:${url.port}` : "";
|
|
43
|
+
return `${url.protocol}//${url.hostname}${port}`;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return "configured";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** HTTP(S) proxies only: Node's env support cannot dial a SOCKS URL. */
|
|
50
|
+
function hasSupportedScheme(value) {
|
|
51
|
+
try {
|
|
52
|
+
const url = new URL(value);
|
|
53
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Node reads `HTTPS_PROXY`/`HTTP_PROXY` and ignores `ALL_PROXY`, which is the
|
|
61
|
+
* generic name many VPN clients export. Copy it into the protocol-specific
|
|
62
|
+
* variables so a proxy-only network still works.
|
|
63
|
+
*/
|
|
64
|
+
function applyAllProxyFallback(env) {
|
|
65
|
+
const all = firstValue(env, ALL_VARIABLES);
|
|
66
|
+
if (!all || !hasSupportedScheme(all.value))
|
|
67
|
+
return;
|
|
68
|
+
for (const [specific, name] of [
|
|
69
|
+
["HTTPS_PROXY", "https_proxy"],
|
|
70
|
+
["HTTP_PROXY", "http_proxy"],
|
|
71
|
+
]) {
|
|
72
|
+
if (!env[specific]?.trim() && !env[name]?.trim()) {
|
|
73
|
+
env[specific] = all.value;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function proxySetter() {
|
|
78
|
+
const candidate = http
|
|
79
|
+
.setGlobalProxyFromEnv;
|
|
80
|
+
return typeof candidate === "function"
|
|
81
|
+
? candidate
|
|
82
|
+
: undefined;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Enables the environment proxy for this process when one is configured.
|
|
86
|
+
* Returns whether the plugin will use it.
|
|
87
|
+
*/
|
|
88
|
+
export function applyEnvironmentProxy(log, env = process.env) {
|
|
89
|
+
const proxy = proxyFromEnvironment(env);
|
|
90
|
+
if (!proxy)
|
|
91
|
+
return false;
|
|
92
|
+
if (!hasSupportedScheme(proxy)) {
|
|
93
|
+
log.warn("free-pi found a proxy in the environment that this runtime cannot use (only http/https proxies are supported); requests go out directly", { proxy: describeProxy(proxy) });
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
// Normalize ALL_PROXY first: it stays useful even when this runtime cannot
|
|
97
|
+
// enable the proxy, because a restart with NODE_USE_ENV_PROXY=1 reads it.
|
|
98
|
+
applyAllProxyFallback(env);
|
|
99
|
+
const setter = proxySetter();
|
|
100
|
+
if (!setter) {
|
|
101
|
+
log.warn("free-pi found a proxy in the environment but this Node runtime cannot apply it; start OpenCode with NODE_USE_ENV_PROXY=1 or use Node 24.5+ so free-pi requests follow the proxy", { node: process.version, proxy: describeProxy(proxy) });
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
setter();
|
|
106
|
+
log.info("free-pi requests follow the proxy configured in the environment", {
|
|
107
|
+
proxy: describeProxy(proxy),
|
|
108
|
+
});
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
log.warn("free-pi could not enable the environment proxy", {
|
|
113
|
+
proxy: describeProxy(proxy),
|
|
114
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
115
|
+
});
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
package/dist/plugin.js
CHANGED
|
@@ -13,6 +13,7 @@ import { applyProvider } from "./catalog/v2.js";
|
|
|
13
13
|
import { CATALOG_RECOVERY_DELAYS_MS, CATALOG_RECOVERY_INTERVAL_MS, CATALOG_RECOVERY_LOG_EVERY, DEFENSE_PROVIDER_ID, PROVIDER_ID, } from "./constants.js";
|
|
14
14
|
import { registerIntegration } from "./integration/setup.js";
|
|
15
15
|
import { createLogger } from "./logging.js";
|
|
16
|
+
import { applyEnvironmentProxy } from "./network/proxy.js";
|
|
16
17
|
import { subscribeEvents } from "./session/events.js";
|
|
17
18
|
import { registerSessionHooks } from "./session/hooks.js";
|
|
18
19
|
import { cancelAllFinalizeTimers } from "./session/track.js";
|
|
@@ -38,6 +39,10 @@ function activeCredentialResolver(ctx) {
|
|
|
38
39
|
}
|
|
39
40
|
async function setup(ctx) {
|
|
40
41
|
const log = createLogger("free-pi");
|
|
42
|
+
// A VPN or corporate setup may only allow traffic through a local proxy.
|
|
43
|
+
// Enable it before the first catalog request so the provider registers models
|
|
44
|
+
// instead of disappearing from the picker.
|
|
45
|
+
applyEnvironmentProxy(log);
|
|
41
46
|
const state = createRuntimeState();
|
|
42
47
|
const includeDefense = ctx.options?.includeDefense !== false;
|
|
43
48
|
const deps = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eduardo191labs/opencode-free-pi",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.12",
|
|
4
4
|
"description": "Ad-supported free-pi provider plugin for OpenCode on Windows, Linux, macOS and Termux",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json",
|
|
26
26
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
27
|
-
"test": "npm run build && node test/catalog.mjs && node test/compaction.mjs && node test/diet.mjs && node test/smoke.mjs && node test/registry.mjs && node test/models.mjs && node test/live.mjs && node test/hooks.mjs && node test/title.mjs && node test/errors.mjs && node test/vision.mjs && node test/agents.mjs && node test/wire.mjs && node test/tools.mjs",
|
|
27
|
+
"test": "npm run build && node test/catalog.mjs && node test/compaction.mjs && node test/diet.mjs && node test/smoke.mjs && node test/registry.mjs && node test/models.mjs && node test/live.mjs && node test/hooks.mjs && node test/title.mjs && node test/errors.mjs && node test/vision.mjs && node test/agents.mjs && node test/wire.mjs && node test/network.mjs && node test/tools.mjs",
|
|
28
28
|
"prepublishOnly": "npm run build",
|
|
29
29
|
"postinstall": "node scripts/postinstall.mjs"
|
|
30
30
|
},
|
package/src/api/session.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/** Releases free-pi server-side session slots. */
|
|
2
2
|
|
|
3
3
|
import { resolveToken } from "../auth/token.js";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
SESSION_RESET_COOLDOWN_MS,
|
|
6
|
+
SESSION_RESET_TIMEOUT_MS,
|
|
7
|
+
} from "../constants.js";
|
|
5
8
|
import type { Deps } from "../deps.js";
|
|
6
9
|
import type { SessionResetOutcome } from "../types.js";
|
|
7
10
|
import { trimMap } from "../utils/async.js";
|
|
@@ -18,7 +21,7 @@ function resetRequest(deps: Deps, token: string, sessionID?: string) {
|
|
|
18
21
|
return fetch(new URL("/session/reset", resolveBaseUrl()), {
|
|
19
22
|
method: "POST",
|
|
20
23
|
headers,
|
|
21
|
-
signal: AbortSignal.timeout(
|
|
24
|
+
signal: AbortSignal.timeout(SESSION_RESET_TIMEOUT_MS),
|
|
22
25
|
});
|
|
23
26
|
}
|
|
24
27
|
|
package/src/constants.ts
CHANGED
|
@@ -228,6 +228,12 @@ export const QUOTA_HTTP_TIMEOUT_MS = 5_000;
|
|
|
228
228
|
export const QUOTA_REFRESH_DEBOUNCE_MS = 1_000;
|
|
229
229
|
|
|
230
230
|
export const SESSION_RESET_COOLDOWN_MS = 15_000;
|
|
231
|
+
/**
|
|
232
|
+
* Releasing a server slot is a tiny POST, but a VPN or a proxy adds a round
|
|
233
|
+
* trip; 3s let the reset expire before the gateway answered and left the slot
|
|
234
|
+
* locked until the next 429.
|
|
235
|
+
*/
|
|
236
|
+
export const SESSION_RESET_TIMEOUT_MS = 6_000;
|
|
231
237
|
|
|
232
238
|
/**
|
|
233
239
|
* How long a free-pi session must stay quiet before its server slot is closed.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Honors the proxy a VPN or corporate network exports in the environment.
|
|
3
|
+
*
|
|
4
|
+
* The free-pi CLI reads `*_proxy`/`all_proxy` and routes its requests through
|
|
5
|
+
* an agent, so it keeps working when the network only allows traffic through a
|
|
6
|
+
* local proxy. Node's `fetch` ignores those variables unless the process opts
|
|
7
|
+
* in, which left the plugin unable to fetch the catalog, quota, ads or replay a
|
|
8
|
+
* request while the CLI worked. `http.setGlobalProxyFromEnv()` (Node 24.5+)
|
|
9
|
+
* turns the environment proxy on for every request of this process, including
|
|
10
|
+
* `fetch`; when the runtime is older the plugin keeps going direct and says so.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import http from "node:http";
|
|
14
|
+
import type { Logger } from "../logging.js";
|
|
15
|
+
|
|
16
|
+
const DIRECT_VARIABLES = [
|
|
17
|
+
"HTTPS_PROXY",
|
|
18
|
+
"https_proxy",
|
|
19
|
+
"HTTP_PROXY",
|
|
20
|
+
"http_proxy",
|
|
21
|
+
] as const;
|
|
22
|
+
const ALL_VARIABLES = ["ALL_PROXY", "all_proxy"] as const;
|
|
23
|
+
const PROXY_VARIABLES = [...DIRECT_VARIABLES, ...ALL_VARIABLES] as const;
|
|
24
|
+
|
|
25
|
+
/** First proxy URL exported in the environment, or undefined. */
|
|
26
|
+
export function proxyFromEnvironment(
|
|
27
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
28
|
+
): string | undefined {
|
|
29
|
+
for (const name of PROXY_VARIABLES) {
|
|
30
|
+
const value = env[name]?.trim();
|
|
31
|
+
if (value) return value;
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function firstValue(
|
|
37
|
+
env: NodeJS.ProcessEnv,
|
|
38
|
+
names: readonly string[],
|
|
39
|
+
): { name: string; value: string } | undefined {
|
|
40
|
+
for (const name of names) {
|
|
41
|
+
const value = env[name]?.trim();
|
|
42
|
+
if (value) return { name, value };
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Proxy URL without credentials, safe for the log. */
|
|
48
|
+
function describeProxy(value: string): string {
|
|
49
|
+
try {
|
|
50
|
+
const url = new URL(value);
|
|
51
|
+
const port = url.port ? `:${url.port}` : "";
|
|
52
|
+
return `${url.protocol}//${url.hostname}${port}`;
|
|
53
|
+
} catch {
|
|
54
|
+
return "configured";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** HTTP(S) proxies only: Node's env support cannot dial a SOCKS URL. */
|
|
59
|
+
function hasSupportedScheme(value: string): boolean {
|
|
60
|
+
try {
|
|
61
|
+
const url = new URL(value);
|
|
62
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
63
|
+
} catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Node reads `HTTPS_PROXY`/`HTTP_PROXY` and ignores `ALL_PROXY`, which is the
|
|
70
|
+
* generic name many VPN clients export. Copy it into the protocol-specific
|
|
71
|
+
* variables so a proxy-only network still works.
|
|
72
|
+
*/
|
|
73
|
+
function applyAllProxyFallback(env: NodeJS.ProcessEnv): void {
|
|
74
|
+
const all = firstValue(env, ALL_VARIABLES);
|
|
75
|
+
if (!all || !hasSupportedScheme(all.value)) return;
|
|
76
|
+
for (const [specific, name] of [
|
|
77
|
+
["HTTPS_PROXY", "https_proxy"],
|
|
78
|
+
["HTTP_PROXY", "http_proxy"],
|
|
79
|
+
] as const) {
|
|
80
|
+
if (!env[specific]?.trim() && !env[name]?.trim()) {
|
|
81
|
+
env[specific] = all.value;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
type ProxySetter = () => unknown;
|
|
87
|
+
|
|
88
|
+
function proxySetter(): ProxySetter | undefined {
|
|
89
|
+
const candidate = (http as { setGlobalProxyFromEnv?: unknown })
|
|
90
|
+
.setGlobalProxyFromEnv;
|
|
91
|
+
return typeof candidate === "function"
|
|
92
|
+
? (candidate as ProxySetter)
|
|
93
|
+
: undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Enables the environment proxy for this process when one is configured.
|
|
98
|
+
* Returns whether the plugin will use it.
|
|
99
|
+
*/
|
|
100
|
+
export function applyEnvironmentProxy(
|
|
101
|
+
log: Logger,
|
|
102
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
103
|
+
): boolean {
|
|
104
|
+
const proxy = proxyFromEnvironment(env);
|
|
105
|
+
if (!proxy) return false;
|
|
106
|
+
if (!hasSupportedScheme(proxy)) {
|
|
107
|
+
log.warn(
|
|
108
|
+
"free-pi found a proxy in the environment that this runtime cannot use (only http/https proxies are supported); requests go out directly",
|
|
109
|
+
{ proxy: describeProxy(proxy) },
|
|
110
|
+
);
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
// Normalize ALL_PROXY first: it stays useful even when this runtime cannot
|
|
114
|
+
// enable the proxy, because a restart with NODE_USE_ENV_PROXY=1 reads it.
|
|
115
|
+
applyAllProxyFallback(env);
|
|
116
|
+
const setter = proxySetter();
|
|
117
|
+
if (!setter) {
|
|
118
|
+
log.warn(
|
|
119
|
+
"free-pi found a proxy in the environment but this Node runtime cannot apply it; start OpenCode with NODE_USE_ENV_PROXY=1 or use Node 24.5+ so free-pi requests follow the proxy",
|
|
120
|
+
{ node: process.version, proxy: describeProxy(proxy) },
|
|
121
|
+
);
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
setter();
|
|
126
|
+
log.info("free-pi requests follow the proxy configured in the environment", {
|
|
127
|
+
proxy: describeProxy(proxy),
|
|
128
|
+
});
|
|
129
|
+
return true;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
log.warn("free-pi could not enable the environment proxy", {
|
|
132
|
+
proxy: describeProxy(proxy),
|
|
133
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
134
|
+
});
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
package/src/plugin.ts
CHANGED
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
import type { Deps } from "./deps.js";
|
|
43
43
|
import { registerIntegration } from "./integration/setup.js";
|
|
44
44
|
import { createLogger } from "./logging.js";
|
|
45
|
+
import { applyEnvironmentProxy } from "./network/proxy.js";
|
|
45
46
|
import { subscribeEvents } from "./session/events.js";
|
|
46
47
|
import { registerSessionHooks } from "./session/hooks.js";
|
|
47
48
|
import { cancelAllFinalizeTimers } from "./session/track.js";
|
|
@@ -67,6 +68,10 @@ function activeCredentialResolver(ctx: Plugin.Context) {
|
|
|
67
68
|
|
|
68
69
|
async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
|
|
69
70
|
const log = createLogger("free-pi");
|
|
71
|
+
// A VPN or corporate setup may only allow traffic through a local proxy.
|
|
72
|
+
// Enable it before the first catalog request so the provider registers models
|
|
73
|
+
// instead of disappearing from the picker.
|
|
74
|
+
applyEnvironmentProxy(log);
|
|
70
75
|
const state = createRuntimeState();
|
|
71
76
|
const includeDefense = ctx.options?.includeDefense !== false;
|
|
72
77
|
|