@eduardo191labs/opencode-free-pi 0.3.8 → 0.3.9

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 CHANGED
@@ -151,12 +151,15 @@ consumo real se consulta en `/me` y `/me/stats` y se refleja en los mensajes de
151
151
  cuota y límite.
152
152
 
153
153
  Los reintentos siguen la política de pi: como máximo 3 reintentos, con espera
154
- `min(2s · 2^(n-1), 60s)`. El 429 `concurrent` («one request at a time») es la
155
- excepción: como el servidor puede dejar el slot de una sesión bloqueado, el
156
- plugin cambia el `x-session-id` de esa sesión, libera el anterior y reintenta
157
- hasta 6 veces con esperas de 1s a 15s. Si el catálogo asigna el experimento
158
- `compact100`, el plugin baja la ventana a 117 647 tokens para que OpenCode
159
- compacte al llegar a 100k, que es el mismo umbral que consigue
154
+ `min(2s · 2^(n-1), 60s)`. Los límites terminales (`free_usage_limit`, cupos
155
+ diarios u horarios, créditos, cuenta bloqueada) se convierten en un 400 con el
156
+ mensaje amigable y no se reintentan, igual que hace el CLI de pi; así OpenCode no
157
+ muestra un «Retrying…» que nunca termina. El 429 `concurrent` («one request at a
158
+ time») es la excepción: como el servidor puede dejar el slot de una sesión
159
+ bloqueado, el plugin cambia el `x-session-id` de esa sesión, libera el anterior y
160
+ reintenta hasta 6 veces con esperas de 1s a 15s. Si el catálogo asigna el
161
+ experimento `compact100`, el plugin baja la ventana a 117 647 tokens para que
162
+ OpenCode compacte al llegar a 100k, que es el mismo umbral que consigue
160
163
  `reserveTokens: 100000` en el CLI.
161
164
 
162
165
  ## Rutas por plataforma
@@ -10,6 +10,7 @@ const LONG_LIMIT_CODES = new Set([
10
10
  "global_cap",
11
11
  "premium_allowance",
12
12
  "guest_allowance",
13
+ "free_usage_limit",
13
14
  "concurrent_session",
14
15
  "model_requires_credits",
15
16
  "insufficient_credits",
@@ -15,6 +15,7 @@ const FREE_PI_ERROR_TEXT = {
15
15
  account_review: "This account is under review; contact support.",
16
16
  ads_required: "Ads are not displaying for this account, so free completions are paused. Run `npx free-pi-cli@latest` so ads can show, then retry.",
17
17
  guest_allowance: "Guest trial used up; sign in with GitHub for a larger allowance.",
18
+ free_usage_limit: "Free usage limit reached. Wait for the allowance to reset, switch models or add credits.",
18
19
  guest_unavailable: "Guest access is unavailable right now.",
19
20
  guest_login_required: "This needs a GitHub sign-in.",
20
21
  region_unavailable: "free-pi is not available in your region.",
@@ -5,6 +5,7 @@ import { resolveToken } from "../auth/token.js";
5
5
  import { COST_EXPERIMENT_HEADER, CONCURRENT_RETRY_BASE_MS, CONCURRENT_RETRY_MAX, CONCURRENT_RETRY_MAX_DELAY_MS, DEFENSE_PROVIDER_ID, EFFICIENCY_RULE, MAX_OUTPUT_TOKENS, PROVIDER_ID, RETRY_BASE_DELAY_MS, RETRY_MAX_DELAY_MS, RETRY_MAX_RETRIES, SUBAGENT_HEADER, } from "../constants.js";
6
6
  import { closeFreePiSession } from "../api/session.js";
7
7
  import { rewriteContextOverflowResponse, rewriteLimitResponse, } from "../errors/limit.js";
8
+ import { parseFreePiError } from "../errors/parse.js";
8
9
  import { trimMap } from "../utils/async.js";
9
10
  import { watchFreePiNotice } from "./notices.js";
10
11
  import { localTitle } from "./title.js";
@@ -119,6 +120,45 @@ function isConcurrentFailure(error) {
119
120
  /one request at a time/i.test(body) ||
120
121
  /"concurrent"/.test(body));
121
122
  }
123
+ /**
124
+ * Limits and account blocks do not recover by retrying. pi's own retry helper
125
+ * treats them as terminal (`NON_RETRYABLE_PROVIDER_LIMIT_ERROR_PATTERN`), but
126
+ * OpenCode retries every 429 by default, which surfaces as an endless
127
+ * "Retrying…". The response rewrite turns most of these into a 400; this guard
128
+ * covers the rest and keeps the CLI's behavior.
129
+ */
130
+ const TERMINAL_RETRY_CODES = new Set([
131
+ "daily_cap",
132
+ "hourly_ceiling",
133
+ "rolling_cap",
134
+ "global_cap",
135
+ "account_ceiling",
136
+ "free_usage_limit",
137
+ "premium_allowance",
138
+ "guest_allowance",
139
+ "model_requires_credits",
140
+ "insufficient_credits",
141
+ "ads_required",
142
+ "account_review",
143
+ "region_unavailable",
144
+ "unauthorized",
145
+ "expired_token",
146
+ "guest_login_required",
147
+ "consent_required",
148
+ "upgrade_required",
149
+ "reward_required",
150
+ "content_filter",
151
+ "subagents_unavailable",
152
+ ]);
153
+ const TERMINAL_RETRY_PATTERN = /free usage limit|monthly usage limit|available balance|insufficient_quota|out of budget|quota exceeded|billing|subscription_sharing_usage_limit_exceeded/i;
154
+ function isTerminalFailure(error) {
155
+ const parsed = parseFreePiError(error, true);
156
+ if (parsed?.code && TERMINAL_RETRY_CODES.has(parsed.code))
157
+ return true;
158
+ const body = typeof error?.response?.body === "string" ? error.response.body : "";
159
+ const message = typeof error?.message === "string" ? error.message : "";
160
+ return TERMINAL_RETRY_PATTERN.test(`${message}\n${body}`);
161
+ }
122
162
  /**
123
163
  * pi retries at most three times with `min(2s * 2^(n-1), 60s)`. OpenCode's
124
164
  * `attempt` counts the initial call as 1, so pi's attempt is `attempt - 1`.
@@ -151,6 +191,10 @@ function applyRetryPolicy(deps, event) {
151
191
  };
152
192
  return;
153
193
  }
194
+ if (isTerminalFailure(event.error)) {
195
+ event.decision = { retry: false };
196
+ return;
197
+ }
154
198
  if (event.attempt > RETRY_MAX_RETRIES + 1) {
155
199
  event.decision = { retry: false };
156
200
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
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",
@@ -13,6 +13,7 @@ const LONG_LIMIT_CODES = new Set([
13
13
  "global_cap",
14
14
  "premium_allowance",
15
15
  "guest_allowance",
16
+ "free_usage_limit",
16
17
  "concurrent_session",
17
18
  "model_requires_credits",
18
19
  "insufficient_credits",
@@ -26,6 +26,8 @@ const FREE_PI_ERROR_TEXT: Record<string, string> = {
26
26
  "Ads are not displaying for this account, so free completions are paused. Run `npx free-pi-cli@latest` so ads can show, then retry.",
27
27
  guest_allowance:
28
28
  "Guest trial used up; sign in with GitHub for a larger allowance.",
29
+ free_usage_limit:
30
+ "Free usage limit reached. Wait for the allowance to reset, switch models or add credits.",
29
31
  guest_unavailable: "Guest access is unavailable right now.",
30
32
  guest_login_required: "This needs a GitHub sign-in.",
31
33
  region_unavailable: "free-pi is not available in your region.",
@@ -25,6 +25,7 @@ import {
25
25
  rewriteContextOverflowResponse,
26
26
  rewriteLimitResponse,
27
27
  } from "../errors/limit.js";
28
+ import { parseFreePiError } from "../errors/parse.js";
28
29
  import { trimMap } from "../utils/async.js";
29
30
  import { watchFreePiNotice } from "./notices.js";
30
31
  import { localTitle } from "./title.js";
@@ -195,6 +196,49 @@ function isConcurrentFailure(error: any): boolean {
195
196
  );
196
197
  }
197
198
 
199
+ /**
200
+ * Limits and account blocks do not recover by retrying. pi's own retry helper
201
+ * treats them as terminal (`NON_RETRYABLE_PROVIDER_LIMIT_ERROR_PATTERN`), but
202
+ * OpenCode retries every 429 by default, which surfaces as an endless
203
+ * "Retrying…". The response rewrite turns most of these into a 400; this guard
204
+ * covers the rest and keeps the CLI's behavior.
205
+ */
206
+ const TERMINAL_RETRY_CODES = new Set([
207
+ "daily_cap",
208
+ "hourly_ceiling",
209
+ "rolling_cap",
210
+ "global_cap",
211
+ "account_ceiling",
212
+ "free_usage_limit",
213
+ "premium_allowance",
214
+ "guest_allowance",
215
+ "model_requires_credits",
216
+ "insufficient_credits",
217
+ "ads_required",
218
+ "account_review",
219
+ "region_unavailable",
220
+ "unauthorized",
221
+ "expired_token",
222
+ "guest_login_required",
223
+ "consent_required",
224
+ "upgrade_required",
225
+ "reward_required",
226
+ "content_filter",
227
+ "subagents_unavailable",
228
+ ]);
229
+
230
+ const TERMINAL_RETRY_PATTERN =
231
+ /free usage limit|monthly usage limit|available balance|insufficient_quota|out of budget|quota exceeded|billing|subscription_sharing_usage_limit_exceeded/i;
232
+
233
+ function isTerminalFailure(error: any): boolean {
234
+ const parsed = parseFreePiError(error, true);
235
+ if (parsed?.code && TERMINAL_RETRY_CODES.has(parsed.code)) return true;
236
+ const body =
237
+ typeof error?.response?.body === "string" ? error.response.body : "";
238
+ const message = typeof error?.message === "string" ? error.message : "";
239
+ return TERMINAL_RETRY_PATTERN.test(`${message}\n${body}`);
240
+ }
241
+
198
242
  /**
199
243
  * pi retries at most three times with `min(2s * 2^(n-1), 60s)`. OpenCode's
200
244
  * `attempt` counts the initial call as 1, so pi's attempt is `attempt - 1`.
@@ -230,6 +274,10 @@ function applyRetryPolicy(deps: Deps, event: any): void {
230
274
  };
231
275
  return;
232
276
  }
277
+ if (isTerminalFailure(event.error)) {
278
+ event.decision = { retry: false };
279
+ return;
280
+ }
233
281
  if (event.attempt > RETRY_MAX_RETRIES + 1) {
234
282
  event.decision = { retry: false };
235
283
  return;