@cortexkit/pi-claustrum-auth 0.1.0 → 0.1.2

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.
Files changed (2) hide show
  1. package/dist/index.js +56 -5
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -116,6 +116,26 @@ class SlotQueue {
116
116
  return result;
117
117
  }
118
118
  }
119
+ // ../core/src/balance.ts
120
+ var balanceProbeMs = 5 * 60 * 1000;
121
+ function isBalanceFailure(service, body) {
122
+ if (service === "deepseek")
123
+ return true;
124
+ if (service !== "openrouter")
125
+ return false;
126
+ try {
127
+ const value = JSON.parse(body ?? "");
128
+ if (typeof value !== "object" || value === null || !("error" in value))
129
+ return false;
130
+ const error = value.error;
131
+ if (typeof error !== "object" || error === null || !("metadata" in error))
132
+ return false;
133
+ const metadata = error.metadata;
134
+ return typeof metadata === "object" && metadata !== null && "limit_source" in metadata && metadata.limit_source === "openrouter_credits" && (!("reason" in metadata) || metadata.reason !== "weight_exceeds_budget");
135
+ } catch {
136
+ return false;
137
+ }
138
+ }
119
139
  // ../../node_modules/.bun/@cortexkit+common-auth@0.29.1+e881539e648aba62/node_modules/@cortexkit/common-auth/dist/redaction/index.js
120
140
  import { types } from "node:util";
121
141
 
@@ -1098,7 +1118,7 @@ function retryExpiry(response, now) {
1098
1118
  if (Number.isFinite(date) && date > now)
1099
1119
  return date;
1100
1120
  }
1101
- return now + 60000;
1121
+ return now + (response.status === 402 ? balanceProbeMs : 60000);
1102
1122
  }
1103
1123
  function limited(marks, now) {
1104
1124
  const expiries = [...marks.values()].filter((expiry) => expiry > now);
@@ -1131,6 +1151,7 @@ function createFetch(options) {
1131
1151
  const tried = new Set;
1132
1152
  const secrets = [];
1133
1153
  let saw429 = false;
1154
+ let saw402 = false;
1134
1155
  try {
1135
1156
  for (;; ) {
1136
1157
  signal.throwIfAborted();
@@ -1141,6 +1162,7 @@ function createFetch(options) {
1141
1162
  throw new ClaustrumAuthError("no-vault-account");
1142
1163
  }
1143
1164
  tried.add(route);
1165
+ const previousMark = rateLimitMarks.get(route);
1144
1166
  let target = url;
1145
1167
  let method = template.method;
1146
1168
  let bytes = body;
@@ -1190,11 +1212,26 @@ function createFetch(options) {
1190
1212
  }
1191
1213
  target = next;
1192
1214
  }
1193
- if (response.status !== 429 && response.status !== 401)
1215
+ let balance402 = false;
1216
+ if (response.status === 402) {
1217
+ let text;
1218
+ if (options.entry.service === "openrouter") {
1219
+ try {
1220
+ text = await response.clone().text();
1221
+ } catch {}
1222
+ }
1223
+ balance402 = isBalanceFailure(options.entry.service, text);
1224
+ }
1225
+ if ((response.ok || response.status === 402 && !balance402) && previousMark !== undefined && previousMark <= now() && rateLimitMarks.get(route) === previousMark)
1226
+ rateLimitMarks.delete(route);
1227
+ if (response.status !== 401 && response.status !== 429 && !balance402)
1194
1228
  return await redactResponse(response, secrets);
1195
1229
  signal.throwIfAborted();
1196
- if (response.status === 429) {
1197
- saw429 = true;
1230
+ if (response.status === 429 || balance402) {
1231
+ if (response.status === 429)
1232
+ saw429 = true;
1233
+ else
1234
+ saw402 = true;
1198
1235
  rateLimitMarks.set(route, retryExpiry(response, now()));
1199
1236
  }
1200
1237
  const next = orderedRoutes(routing, tried)[0];
@@ -1202,7 +1239,7 @@ function createFetch(options) {
1202
1239
  await response.body?.cancel().catch(() => {});
1203
1240
  continue;
1204
1241
  }
1205
- if (response.status === 401 && saw429) {
1242
+ if (saw429 && (response.status === 401 || saw402)) {
1206
1243
  await response.body?.cancel().catch(() => {});
1207
1244
  return limited(rateLimitMarks, now());
1208
1245
  }
@@ -7206,6 +7243,13 @@ var supportedProviders = [
7206
7243
  class: "A",
7207
7244
  origins: ["https://api.kimi.com", "https://api.kimi.ai"]
7208
7245
  },
7246
+ {
7247
+ service: "minimax-coding-plan",
7248
+ ids: ["minimax-coding-plan"],
7249
+ hosts: { opencode2: "minimax-coding-plan" },
7250
+ class: "A",
7251
+ origins: ["https://api.minimax.io"]
7252
+ },
7209
7253
  {
7210
7254
  service: "openrouter",
7211
7255
  ids: ["openrouter"],
@@ -7217,6 +7261,13 @@ var supportedProviders = [
7217
7261
  class: "A",
7218
7262
  origins: ["https://openrouter.ai"]
7219
7263
  },
7264
+ {
7265
+ service: "synthetic",
7266
+ ids: ["synthetic"],
7267
+ hosts: { opencode1: "synthetic", opencode2: "synthetic" },
7268
+ class: "A",
7269
+ origins: ["https://api.synthetic.new"]
7270
+ },
7220
7271
  {
7221
7272
  service: "typesafe",
7222
7273
  ids: ["typesafe"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-claustrum-auth",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Claustrum vault credentials for Pi",
5
5
  "type": "module",
6
6
  "license": "MIT",