@eduardo191labs/opencode-free-pi 0.7.11 → 0.7.13
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 +10 -2
- package/dist/network/proxy.d.ts +1 -1
- package/dist/network/proxy.js +48 -5
- package/dist/session/hooks.js +56 -0
- package/package.json +1 -1
- package/src/network/proxy.ts +57 -5
- package/src/session/hooks.ts +54 -0
package/README.md
CHANGED
|
@@ -155,6 +155,12 @@ Después de iniciar sesión, ejecuta `/reload` en OpenCode o reinícialo.
|
|
|
155
155
|
publica el upstream (32k por defecto, hasta 131k). No se fuerza temperatura ni
|
|
156
156
|
`top_p`: la petición real no los incluye, así que el gateway aplica su propio
|
|
157
157
|
valor. El prompt se envía con rol `system`, no `developer`.
|
|
158
|
+
- **Sin razonamiento reenviado**: OpenCode guarda el pensamiento del modelo en
|
|
159
|
+
los mensajes del asistente y lo devuelve dentro de `reasoning_content`. La
|
|
160
|
+
captura con `curl_cffi` mostró que el gateway lo cobra como entrada (mismo
|
|
161
|
+
prompt y misma respuesta pasaron de 27 a 188 tokens en `glm-5.3-flash`), así
|
|
162
|
+
que el plugin lo quita antes de enviar. No cambia la respuesta: el modelo ya
|
|
163
|
+
razonó en su turno y el historial conserva el texto visible.
|
|
158
164
|
- **Límites medidos, no inventados**: `/client-version` solo devuelve id,
|
|
159
165
|
nombre, `free`, `section`, `note`, `stats_id` y `usage_x`; no hay endpoint de
|
|
160
166
|
capacidades ni de ventana. El plugin no se queda en constantes: confirma la
|
|
@@ -226,11 +232,13 @@ Después de iniciar sesión, ejecuta `/reload` en OpenCode o reinícialo.
|
|
|
226
232
|
los ignora salvo que se active. Por eso, si el entorno define un proxy, el
|
|
227
233
|
plugin lo activa al arrancar con `http.setGlobalProxyFromEnv()`: el catálogo,
|
|
228
234
|
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`.
|
|
235
|
+
pasan por el proxy en vez de salir directos y fallar con `ETIMEDOUT`. `ALL_PROXY`
|
|
236
|
+
se copia a `HTTPS_PROXY`/`HTTP_PROXY` porque Node solo lee esos dos. Sin
|
|
230
237
|
variables de proxy no cambia nada. `setGlobalProxyFromEnv()` existe desde Node
|
|
231
238
|
24.5; en un Node anterior el plugin escribe en el log que hay que arrancar
|
|
232
239
|
OpenCode con `NODE_USE_ENV_PROXY=1` (o actualizar Node), porque esa versión
|
|
233
|
-
ignora el proxy en `fetch` aunque se defina el resto.
|
|
240
|
+
ignora el proxy en `fetch` aunque se defina el resto. Un proxy SOCKS no se
|
|
241
|
+
puede usar con esta API y también se avisa. En Node 26 la
|
|
234
242
|
verificación fue real: con `HTTPS_PROXY` apuntando a un proxy local, las
|
|
235
243
|
conexiones a `api.freepi.ai`, `models.opencode.ai` y `registry.npmjs.org`
|
|
236
244
|
aparecieron en el proxy y una sesión completa respondió correctamente.
|
package/dist/network/proxy.d.ts
CHANGED
|
@@ -16,4 +16,4 @@ export declare function proxyFromEnvironment(env?: NodeJS.ProcessEnv): string |
|
|
|
16
16
|
* Enables the environment proxy for this process when one is configured.
|
|
17
17
|
* Returns whether the plugin will use it.
|
|
18
18
|
*/
|
|
19
|
-
export declare function applyEnvironmentProxy(log: Logger): boolean;
|
|
19
|
+
export declare function applyEnvironmentProxy(log: Logger, env?: NodeJS.ProcessEnv): boolean;
|
package/dist/network/proxy.js
CHANGED
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
* `fetch`; when the runtime is older the plugin keeps going direct and says so.
|
|
11
11
|
*/
|
|
12
12
|
import http from "node:http";
|
|
13
|
-
const
|
|
13
|
+
const DIRECT_VARIABLES = [
|
|
14
14
|
"HTTPS_PROXY",
|
|
15
15
|
"https_proxy",
|
|
16
16
|
"HTTP_PROXY",
|
|
17
17
|
"http_proxy",
|
|
18
|
-
"ALL_PROXY",
|
|
19
|
-
"all_proxy",
|
|
20
18
|
];
|
|
19
|
+
const ALL_VARIABLES = ["ALL_PROXY", "all_proxy"];
|
|
20
|
+
const PROXY_VARIABLES = [...DIRECT_VARIABLES, ...ALL_VARIABLES];
|
|
21
21
|
/** First proxy URL exported in the environment, or undefined. */
|
|
22
22
|
export function proxyFromEnvironment(env = process.env) {
|
|
23
23
|
for (const name of PROXY_VARIABLES) {
|
|
@@ -27,6 +27,14 @@ export function proxyFromEnvironment(env = process.env) {
|
|
|
27
27
|
}
|
|
28
28
|
return undefined;
|
|
29
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
|
+
}
|
|
30
38
|
/** Proxy URL without credentials, safe for the log. */
|
|
31
39
|
function describeProxy(value) {
|
|
32
40
|
try {
|
|
@@ -38,6 +46,34 @@ function describeProxy(value) {
|
|
|
38
46
|
return "configured";
|
|
39
47
|
}
|
|
40
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
|
+
}
|
|
41
77
|
function proxySetter() {
|
|
42
78
|
const candidate = http
|
|
43
79
|
.setGlobalProxyFromEnv;
|
|
@@ -49,10 +85,17 @@ function proxySetter() {
|
|
|
49
85
|
* Enables the environment proxy for this process when one is configured.
|
|
50
86
|
* Returns whether the plugin will use it.
|
|
51
87
|
*/
|
|
52
|
-
export function applyEnvironmentProxy(log) {
|
|
53
|
-
const proxy = proxyFromEnvironment();
|
|
88
|
+
export function applyEnvironmentProxy(log, env = process.env) {
|
|
89
|
+
const proxy = proxyFromEnvironment(env);
|
|
54
90
|
if (!proxy)
|
|
55
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);
|
|
56
99
|
const setter = proxySetter();
|
|
57
100
|
if (!setter) {
|
|
58
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) });
|
package/dist/session/hooks.js
CHANGED
|
@@ -206,6 +206,61 @@ function applyContextDiet(deps, event) {
|
|
|
206
206
|
tokensAfter: result.tokensAfter,
|
|
207
207
|
});
|
|
208
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Drops the reasoning OpenCode stores and echoes back inside assistant
|
|
211
|
+
* messages.
|
|
212
|
+
*
|
|
213
|
+
* The provider request carries it as `reasoning_content`, but it is not input:
|
|
214
|
+
* the gateway billed it on some models (a measured glm-5.3-flash request went
|
|
215
|
+
* from 27 to 188 prompt tokens with the same answer) and the token estimate used
|
|
216
|
+
* for diet/compaction assumes it is not sent. Removing it keeps the request
|
|
217
|
+
* identical in behavior and smaller in credits.
|
|
218
|
+
*/
|
|
219
|
+
function stripAssistantReasoning(deps, event) {
|
|
220
|
+
const messages = event.messages;
|
|
221
|
+
if (!Array.isArray(messages) || messages.length === 0)
|
|
222
|
+
return;
|
|
223
|
+
let chars = 0;
|
|
224
|
+
let changed = false;
|
|
225
|
+
const next = messages.map((message) => {
|
|
226
|
+
if (!message || message.role !== "assistant")
|
|
227
|
+
return message;
|
|
228
|
+
let copy;
|
|
229
|
+
const ensureCopy = () => {
|
|
230
|
+
if (!copy)
|
|
231
|
+
copy = { ...message };
|
|
232
|
+
return copy;
|
|
233
|
+
};
|
|
234
|
+
for (const key of ["reasoning_content", "reasoning"]) {
|
|
235
|
+
if (!(key in message))
|
|
236
|
+
continue;
|
|
237
|
+
const value = message[key];
|
|
238
|
+
if (typeof value === "string")
|
|
239
|
+
chars += value.length;
|
|
240
|
+
ensureCopy();
|
|
241
|
+
delete copy[key];
|
|
242
|
+
changed = true;
|
|
243
|
+
}
|
|
244
|
+
if (Array.isArray(message.content)) {
|
|
245
|
+
const kept = message.content.filter((part) => part?.type !== "reasoning" && part?.type !== "thinking");
|
|
246
|
+
if (kept.length !== message.content.length) {
|
|
247
|
+
ensureCopy();
|
|
248
|
+
copy.content = kept;
|
|
249
|
+
changed = true;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return copy ?? message;
|
|
253
|
+
});
|
|
254
|
+
if (!changed)
|
|
255
|
+
return;
|
|
256
|
+
event.messages = next;
|
|
257
|
+
if (chars > 0) {
|
|
258
|
+
deps.log.info("free-pi dropped stored reasoning from the request", {
|
|
259
|
+
sessionID: event.sessionID,
|
|
260
|
+
chars,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
209
264
|
/** Caps output tokens, applies the efficiency rule and derives the ad turn. */
|
|
210
265
|
async function applyGeneration(deps, event, kind) {
|
|
211
266
|
if (!isFreePiProvider(event.model?.providerID))
|
|
@@ -218,6 +273,7 @@ async function applyGeneration(deps, event, kind) {
|
|
|
218
273
|
event.result = localTitle(event.messages);
|
|
219
274
|
return;
|
|
220
275
|
}
|
|
276
|
+
stripAssistantReasoning(deps, event);
|
|
221
277
|
const modelID = event.model?.id;
|
|
222
278
|
const limit = deps.state.outputLimits.get(`${event.model.providerID}/${modelID}`);
|
|
223
279
|
// The compaction summary is the one response that must never be cut: a
|
package/package.json
CHANGED
package/src/network/proxy.ts
CHANGED
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
import http from "node:http";
|
|
14
14
|
import type { Logger } from "../logging.js";
|
|
15
15
|
|
|
16
|
-
const
|
|
16
|
+
const DIRECT_VARIABLES = [
|
|
17
17
|
"HTTPS_PROXY",
|
|
18
18
|
"https_proxy",
|
|
19
19
|
"HTTP_PROXY",
|
|
20
20
|
"http_proxy",
|
|
21
|
-
"ALL_PROXY",
|
|
22
|
-
"all_proxy",
|
|
23
21
|
] as const;
|
|
22
|
+
const ALL_VARIABLES = ["ALL_PROXY", "all_proxy"] as const;
|
|
23
|
+
const PROXY_VARIABLES = [...DIRECT_VARIABLES, ...ALL_VARIABLES] as const;
|
|
24
24
|
|
|
25
25
|
/** First proxy URL exported in the environment, or undefined. */
|
|
26
26
|
export function proxyFromEnvironment(
|
|
@@ -33,6 +33,17 @@ export function proxyFromEnvironment(
|
|
|
33
33
|
return undefined;
|
|
34
34
|
}
|
|
35
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
|
+
|
|
36
47
|
/** Proxy URL without credentials, safe for the log. */
|
|
37
48
|
function describeProxy(value: string): string {
|
|
38
49
|
try {
|
|
@@ -44,6 +55,34 @@ function describeProxy(value: string): string {
|
|
|
44
55
|
}
|
|
45
56
|
}
|
|
46
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
|
+
|
|
47
86
|
type ProxySetter = () => unknown;
|
|
48
87
|
|
|
49
88
|
function proxySetter(): ProxySetter | undefined {
|
|
@@ -58,9 +97,22 @@ function proxySetter(): ProxySetter | undefined {
|
|
|
58
97
|
* Enables the environment proxy for this process when one is configured.
|
|
59
98
|
* Returns whether the plugin will use it.
|
|
60
99
|
*/
|
|
61
|
-
export function applyEnvironmentProxy(
|
|
62
|
-
|
|
100
|
+
export function applyEnvironmentProxy(
|
|
101
|
+
log: Logger,
|
|
102
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
103
|
+
): boolean {
|
|
104
|
+
const proxy = proxyFromEnvironment(env);
|
|
63
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);
|
|
64
116
|
const setter = proxySetter();
|
|
65
117
|
if (!setter) {
|
|
66
118
|
log.warn(
|
package/src/session/hooks.ts
CHANGED
|
@@ -338,6 +338,58 @@ function applyContextDiet(deps: Deps, event: any): void {
|
|
|
338
338
|
);
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Drops the reasoning OpenCode stores and echoes back inside assistant
|
|
343
|
+
* messages.
|
|
344
|
+
*
|
|
345
|
+
* The provider request carries it as `reasoning_content`, but it is not input:
|
|
346
|
+
* the gateway billed it on some models (a measured glm-5.3-flash request went
|
|
347
|
+
* from 27 to 188 prompt tokens with the same answer) and the token estimate used
|
|
348
|
+
* for diet/compaction assumes it is not sent. Removing it keeps the request
|
|
349
|
+
* identical in behavior and smaller in credits.
|
|
350
|
+
*/
|
|
351
|
+
function stripAssistantReasoning(deps: Deps, event: any): void {
|
|
352
|
+
const messages = event.messages;
|
|
353
|
+
if (!Array.isArray(messages) || messages.length === 0) return;
|
|
354
|
+
let chars = 0;
|
|
355
|
+
let changed = false;
|
|
356
|
+
const next = messages.map((message: any) => {
|
|
357
|
+
if (!message || message.role !== "assistant") return message;
|
|
358
|
+
let copy: any | undefined;
|
|
359
|
+
const ensureCopy = () => {
|
|
360
|
+
if (!copy) copy = { ...message };
|
|
361
|
+
return copy;
|
|
362
|
+
};
|
|
363
|
+
for (const key of ["reasoning_content", "reasoning"] as const) {
|
|
364
|
+
if (!(key in message)) continue;
|
|
365
|
+
const value = message[key];
|
|
366
|
+
if (typeof value === "string") chars += value.length;
|
|
367
|
+
ensureCopy();
|
|
368
|
+
delete copy[key];
|
|
369
|
+
changed = true;
|
|
370
|
+
}
|
|
371
|
+
if (Array.isArray(message.content)) {
|
|
372
|
+
const kept = message.content.filter(
|
|
373
|
+
(part: any) => part?.type !== "reasoning" && part?.type !== "thinking",
|
|
374
|
+
);
|
|
375
|
+
if (kept.length !== message.content.length) {
|
|
376
|
+
ensureCopy();
|
|
377
|
+
copy.content = kept;
|
|
378
|
+
changed = true;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return copy ?? message;
|
|
382
|
+
});
|
|
383
|
+
if (!changed) return;
|
|
384
|
+
event.messages = next;
|
|
385
|
+
if (chars > 0) {
|
|
386
|
+
deps.log.info("free-pi dropped stored reasoning from the request", {
|
|
387
|
+
sessionID: event.sessionID,
|
|
388
|
+
chars,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
341
393
|
/** Caps output tokens, applies the efficiency rule and derives the ad turn. */
|
|
342
394
|
async function applyGeneration(
|
|
343
395
|
deps: Deps,
|
|
@@ -355,6 +407,8 @@ async function applyGeneration(
|
|
|
355
407
|
return;
|
|
356
408
|
}
|
|
357
409
|
|
|
410
|
+
stripAssistantReasoning(deps, event);
|
|
411
|
+
|
|
358
412
|
const modelID = event.model?.id;
|
|
359
413
|
const limit = deps.state.outputLimits.get(
|
|
360
414
|
`${event.model.providerID}/${modelID}`,
|