@eduardo191labs/opencode-free-pi 0.3.5 → 0.3.6

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.
@@ -45,6 +45,7 @@ const FREE_PI_ERROR_TITLE = {
45
45
  unauthorized: "free-pi sign-in",
46
46
  expired_token: "free-pi sign-in",
47
47
  guest_allowance: "free-pi sign-in",
48
+ free_usage_limit: "free-pi free limit",
48
49
  guest_login_required: "free-pi sign-in",
49
50
  guest_unavailable: "free-pi sign-in",
50
51
  consent_required: "free-pi consent",
@@ -26,6 +26,10 @@ const RAW_FREE_PI_ERROR_PATTERNS = [
26
26
  "insufficient_credits",
27
27
  ],
28
28
  [/guest trial used up|guest_allowance/i, "guest_allowance"],
29
+ [
30
+ /free usage limit|monthly usage limit/i,
31
+ "free_usage_limit",
32
+ ],
29
33
  [
30
34
  /needs a github sign-in|sign in with github|guest_login_required/i,
31
35
  "guest_login_required",
@@ -91,11 +95,26 @@ export function parseRawFreePiError(error) {
91
95
  }
92
96
  return undefined;
93
97
  }
98
+ /**
99
+ * Recovers a code from the server's message text when the envelope carries no
100
+ * `code` field, for example the free-usage-limit 429 that only sends a message.
101
+ */
102
+ function codeFromText(message) {
103
+ if (typeof message !== "string" || !message.trim())
104
+ return undefined;
105
+ const text = message.trim();
106
+ for (const [pattern, code] of RAW_FREE_PI_ERROR_PATTERNS) {
107
+ if (pattern.test(text))
108
+ return code;
109
+ }
110
+ return undefined;
111
+ }
94
112
  export function parseFreePiError(error, allowRaw = false) {
95
113
  const candidates = [
96
114
  error?.response?.body,
97
115
  error?.data?.responseBody,
98
116
  error?.responseBody,
117
+ error?.data,
99
118
  error?.data?.message,
100
119
  error?.message,
101
120
  ];
@@ -129,7 +148,7 @@ export function parseFreePiError(error, allowRaw = false) {
129
148
  ? codeValue
130
149
  : status === 401
131
150
  ? "unauthorized"
132
- : undefined;
151
+ : codeFromText(message);
133
152
  if (!code && typeof message !== "string")
134
153
  continue;
135
154
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
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": "tsc -p tsconfig.json",
26
26
  "typecheck": "tsc -p tsconfig.json --noEmit",
27
- "test": "npm run build && node test/smoke.mjs && node test/registry.mjs && node test/models.mjs && node test/hooks.mjs && node test/title.mjs",
27
+ "test": "npm run build && node test/smoke.mjs && node test/registry.mjs && node test/models.mjs && node test/hooks.mjs && node test/title.mjs && node test/errors.mjs",
28
28
  "prepublishOnly": "npm run build",
29
29
  "postinstall": "node scripts/postinstall.mjs"
30
30
  },
@@ -67,6 +67,7 @@ const FREE_PI_ERROR_TITLE: Record<string, string> = {
67
67
  unauthorized: "free-pi sign-in",
68
68
  expired_token: "free-pi sign-in",
69
69
  guest_allowance: "free-pi sign-in",
70
+ free_usage_limit: "free-pi free limit",
70
71
  guest_login_required: "free-pi sign-in",
71
72
  guest_unavailable: "free-pi sign-in",
72
73
  consent_required: "free-pi consent",
@@ -29,6 +29,10 @@ const RAW_FREE_PI_ERROR_PATTERNS: Array<[RegExp, string]> = [
29
29
  "insufficient_credits",
30
30
  ],
31
31
  [/guest trial used up|guest_allowance/i, "guest_allowance"],
32
+ [
33
+ /free usage limit|monthly usage limit/i,
34
+ "free_usage_limit",
35
+ ],
32
36
  [
33
37
  /needs a github sign-in|sign in with github|guest_login_required/i,
34
38
  "guest_login_required",
@@ -91,6 +95,19 @@ export function parseRawFreePiError(error: any): FreePiError | undefined {
91
95
  return undefined;
92
96
  }
93
97
 
98
+ /**
99
+ * Recovers a code from the server's message text when the envelope carries no
100
+ * `code` field, for example the free-usage-limit 429 that only sends a message.
101
+ */
102
+ function codeFromText(message: unknown): string | undefined {
103
+ if (typeof message !== "string" || !message.trim()) return undefined;
104
+ const text = message.trim();
105
+ for (const [pattern, code] of RAW_FREE_PI_ERROR_PATTERNS) {
106
+ if (pattern.test(text)) return code;
107
+ }
108
+ return undefined;
109
+ }
110
+
94
111
  export function parseFreePiError(
95
112
  error: any,
96
113
  allowRaw = false,
@@ -99,6 +116,7 @@ export function parseFreePiError(
99
116
  error?.response?.body,
100
117
  error?.data?.responseBody,
101
118
  error?.responseBody,
119
+ error?.data,
102
120
  error?.data?.message,
103
121
  error?.message,
104
122
  ];
@@ -129,7 +147,7 @@ export function parseFreePiError(
129
147
  ? codeValue
130
148
  : status === 401
131
149
  ? "unauthorized"
132
- : undefined;
150
+ : codeFromText(message);
133
151
  if (!code && typeof message !== "string") continue;
134
152
  return {
135
153
  status: typeof body.status === "number" ? body.status : status,