@r-33/opencode 0.4.7-rc.30 → 0.4.7-rc.32

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/dist/index.js CHANGED
@@ -2183,9 +2183,11 @@ var KeyLeaseError = class extends Error {
2183
2183
  );
2184
2184
  this.code = code;
2185
2185
  this.name = "KeyLeaseError";
2186
+ this.rejection = options?.rejection;
2186
2187
  this.statusCode = options?.statusCode;
2187
2188
  }
2188
2189
  code;
2190
+ rejection;
2189
2191
  statusCode;
2190
2192
  };
2191
2193
 
@@ -2350,6 +2352,33 @@ var commitStagedReplacement = async (stage, save, persist) => {
2350
2352
  }
2351
2353
  };
2352
2354
 
2355
+ // src/management/management-error/management-error.ts
2356
+ var ManagementError = class extends Error {
2357
+ constructor(code, options) {
2358
+ super(
2359
+ "Route 33 management request failed",
2360
+ options?.cause === void 0 ? void 0 : { cause: "Route 33 management dependency failed" }
2361
+ );
2362
+ this.code = code;
2363
+ this.name = "ManagementError";
2364
+ this.rejection = options?.rejection;
2365
+ this.statusCode = options?.statusCode;
2366
+ }
2367
+ code;
2368
+ rejection;
2369
+ statusCode;
2370
+ };
2371
+
2372
+ // src/key-lease/mint-failure/mint-failure.ts
2373
+ var mintFailure = (cause) => {
2374
+ const response = cause instanceof ManagementError ? cause : void 0;
2375
+ return new KeyLeaseError("KEY_LEASE_MINT_FAILED", {
2376
+ cause,
2377
+ ...response?.rejection === void 0 ? {} : { rejection: response.rejection },
2378
+ ...response?.statusCode === void 0 ? {} : { statusCode: response.statusCode }
2379
+ });
2380
+ };
2381
+
2353
2382
  // src/key-lease/mint-fresh-lease/mint-fresh-lease.ts
2354
2383
  var mintFreshLease = async (deps) => {
2355
2384
  const now = deps.clock();
@@ -2380,7 +2409,7 @@ var mintFreshLease = async (deps) => {
2380
2409
  input
2381
2410
  );
2382
2411
  } catch (cause) {
2383
- throw new KeyLeaseError("KEY_LEASE_MINT_FAILED", { cause });
2412
+ throw mintFailure(cause);
2384
2413
  }
2385
2414
  const minted = {
2386
2415
  key: material.key,
@@ -2463,21 +2492,6 @@ var rejectLease = async (options) => {
2463
2492
  }
2464
2493
  };
2465
2494
 
2466
- // src/management/management-error/management-error.ts
2467
- var ManagementError = class extends Error {
2468
- constructor(code, options) {
2469
- super(
2470
- "Route 33 management request failed",
2471
- options?.cause === void 0 ? void 0 : { cause: "Route 33 management dependency failed" }
2472
- );
2473
- this.code = code;
2474
- this.name = "ManagementError";
2475
- this.statusCode = options?.statusCode;
2476
- }
2477
- code;
2478
- statusCode;
2479
- };
2480
-
2481
2495
  // src/key-lease/mint-key/mint-key.ts
2482
2496
  var mintKey = async (options) => {
2483
2497
  const now = options.clock();
@@ -2511,10 +2525,7 @@ var mintKey = async (options) => {
2511
2525
  if (isManagementUnauthorized(cause)) {
2512
2526
  throw new KeyLeaseError("KEY_LEASE_UNAUTHORIZED", { cause });
2513
2527
  }
2514
- throw new KeyLeaseError("KEY_LEASE_MINT_FAILED", {
2515
- cause,
2516
- ...cause instanceof ManagementError && cause.statusCode !== void 0 ? { statusCode: cause.statusCode } : {}
2517
- });
2528
+ throw mintFailure(cause);
2518
2529
  }
2519
2530
  return {
2520
2531
  key: material.key,
@@ -2852,6 +2863,7 @@ var inspectSession = (options) => {
2852
2863
  const lease = options.readLease();
2853
2864
  return {
2854
2865
  attribution,
2866
+ connected: options.connected,
2855
2867
  ...lease === void 0 ? {} : { lease: { expiresAt: lease.expiresAt, keyId: lease.keyId } }
2856
2868
  };
2857
2869
  };
@@ -2882,10 +2894,10 @@ var WorkspaceKeyBinding = class {
2882
2894
  * @returns Secret-free options; local options do not need destructive consent.
2883
2895
  */
2884
2896
  async candidates(choice) {
2885
- const listing = this.#listings.get(choice.orgId) ?? this.deps.client.list(this.deps.token, choice.orgId);
2897
+ const listing = this.#listings.get(choice.orgId) ?? this.listKeys(choice.orgId);
2886
2898
  this.#listings.set(choice.orgId, listing);
2887
2899
  const rows = await listing;
2888
- const keys = rows.data.filter((row) => this.matches(row, choice));
2900
+ const keys = rows.filter((row) => this.matches(row, choice));
2889
2901
  if (keys.length === 0) return [];
2890
2902
  const local = await this.localKeys(new Set(keys.map((row) => row.id)));
2891
2903
  return keys.map((row) => ({
@@ -2894,8 +2906,18 @@ var WorkspaceKeyBinding = class {
2894
2906
  local: local.has(row.id)
2895
2907
  }));
2896
2908
  }
2909
+ async listKeys(orgId) {
2910
+ try {
2911
+ return (await this.deps.client.list(this.deps.token, orgId)).data;
2912
+ } catch {
2913
+ logger.warn(
2914
+ "Existing keys could not be listed; only new keys can be offered."
2915
+ );
2916
+ return [];
2917
+ }
2918
+ }
2897
2919
  matches(row, choice) {
2898
- return row.status === "active" && !row.revoked && Date.parse(row.expires_at) > this.deps.now && (choice.orgId === void 0 || row.org_id === choice.orgId) && row.owner_user_id === this.deps.subject && (choice.kind === "project" ? row.project_id === choice.projectId && row.ownership_type === "user_project" : row.project_id === null && row.ownership_type === "user_personal");
2920
+ return row.status === "active" && !row.revoked && Date.parse(row.expires_at) > this.deps.now && (choice.orgId === void 0 || row.org_id === choice.orgId) && row.user_id === this.deps.subject && (choice.kind === "project" ? row.project_id === choice.projectId && row.ownership_type === "user_project" : row.project_id === null && row.ownership_type === "user_personal");
2899
2921
  }
2900
2922
  async localKeys(ids) {
2901
2923
  const found = /* @__PURE__ */ new Map();
@@ -2994,6 +3016,8 @@ var createAttributionPrompt = (projects, options = {}) => {
2994
3016
  return `Personal \u2014 your own key${marker}${orgSuffix}`;
2995
3017
  };
2996
3018
  const budgetAllowance = (project) => {
3019
+ if (project.memberCap !== void 0)
3020
+ return `cap ${String(project.memberCap)} EUR`;
2997
3021
  const budget = project.effective_budget ?? project.max_budget;
2998
3022
  return budget === null ? "no cap set" : `cap ${budget.amount} ${budget.currency}`;
2999
3023
  };
@@ -3008,6 +3032,7 @@ var createAttributionPrompt = (projects, options = {}) => {
3008
3032
  };
3009
3033
  return {
3010
3034
  label: `${project.organisation.name} \xB7 ${project.name} \u2014 ${budgetAllowance(project)}`,
3035
+ maximum: project.memberCap,
3011
3036
  selection
3012
3037
  };
3013
3038
  });
@@ -3027,6 +3052,7 @@ var createAttributionPrompt = (projects, options = {}) => {
3027
3052
  choicesBySelection[personal] = { kind: "personal", orgId: org.id };
3028
3053
  promptOptions.push({
3029
3054
  label: personalLabel(options.orgs.length > 1 ? org.name : void 0),
3055
+ maximum: org.personalBudget?.cap ?? void 0,
3030
3056
  selection: personal
3031
3057
  });
3032
3058
  }
@@ -3042,6 +3068,7 @@ var createAttributionPrompt = (projects, options = {}) => {
3042
3068
  };
3043
3069
  promptOptions.push({
3044
3070
  label: `${project.organisation.name} \xB7 ${project.name} \u2014 ${budgetAllowance(project)}`,
3071
+ maximum: project.memberCap,
3045
3072
  selection
3046
3073
  });
3047
3074
  }
@@ -3061,8 +3088,8 @@ var isPersonalSelection = (selection) => selection === "personal" || selection.e
3061
3088
  // src/ux/attribution-prompt-error/attribution-prompt-error.ts
3062
3089
  var MESSAGES = {
3063
3090
  ATTRIBUTION_BACK: "Left budget selection without changing attribution.",
3064
- ATTRIBUTION_PRESENTER_UNAVAILABLE: "Route 33 needs OpenCode v2 to assign a budget for this folder. Update to OpenCode v2, run /connect, choose Route 33, and follow the steps.",
3065
- ATTRIBUTION_SELECTION_INVALID: "That Route 33 option is no longer available. Connect again and pick another."
3091
+ ATTRIBUTION_PRESENTER_UNAVAILABLE: "Update to OpenCode v2 to assign a Route 33 budget in this folder.",
3092
+ ATTRIBUTION_SELECTION_INVALID: "That Route 33 option is no longer available; choose another budget."
3066
3093
  };
3067
3094
  var AttributionPromptError = class extends GatewayCredentialError {
3068
3095
  code;
@@ -3181,7 +3208,7 @@ import {
3181
3208
  } from "@r-33/auth-browser";
3182
3209
 
3183
3210
  // src/requested-scopes/requested-scopes.ts
3184
- var REQUESTED_OAUTH_SCOPES = "openid offline_access orgs:read profile projects:read users:read keys:read keys:write";
3211
+ var REQUESTED_OAUTH_SCOPES = "openid offline_access orgs:read profile projects:read users:read keys:read keys:write models:read";
3185
3212
 
3186
3213
  // src/route33-login/route33-login.ts
3187
3214
  var route33Login = async (options) => {
@@ -3210,20 +3237,34 @@ var LoginSession = class {
3210
3237
  #options;
3211
3238
  #result;
3212
3239
  #expiresAt;
3240
+ #clears = 0;
3213
3241
  constructor(options) {
3214
3242
  this.#options = options;
3215
3243
  }
3216
3244
  /**
3217
3245
  * Clears the session: the cached login result and the stored refresh
3218
- * token. The next login starts from scratch — /r33 logout and the
3219
- * auto-reconnect path both land here.
3246
+ * token. The next login starts from scratch — the removal of the host's
3247
+ * last Route 33 account and the auto-reconnect path both land here. A refresh still on its way to the
3248
+ * auth server is disowned: its rotated token is never stored.
3220
3249
  * @returns Resolved once the session and the stored refresh token are gone.
3221
3250
  */
3222
3251
  async clear() {
3252
+ this.#clears += 1;
3223
3253
  this.#result = void 0;
3224
3254
  this.#expiresAt = void 0;
3225
3255
  await this.#options.tokenStore?.set(void 0);
3226
3256
  }
3257
+ /**
3258
+ * Whether a session exists, judged without using it: a login resolved or
3259
+ * in flight in this process, or a stored refresh token. Read-only — the
3260
+ * token is never presented, so asking cannot rotate or revoke anything.
3261
+ * @returns True when there is a session to sign in with.
3262
+ */
3263
+ async hasSession() {
3264
+ if (this.#result !== void 0) return true;
3265
+ const stored = await this.#options.tokenStore?.get().catch(() => void 0);
3266
+ return stored !== void 0 && stored !== "";
3267
+ }
3227
3268
  /**
3228
3269
  * Resolves the login once per process, sharing in-flight attempts.
3229
3270
  * @param policy - Whether this attempt may start a browser login; an in-flight attempt is always shared.
@@ -3232,11 +3273,15 @@ var LoginSession = class {
3232
3273
  result(policy = {}) {
3233
3274
  if (this.#result !== void 0 && this.#expiresAt !== void 0 && Date.now() >= this.#expiresAt - EXPIRY_SKEW_MS) {
3234
3275
  this.#result = void 0;
3276
+ this.#expiresAt = void 0;
3277
+ }
3278
+ if (this.#result === void 0) {
3279
+ const attempt = this.#start(policy).catch((cause) => {
3280
+ if (this.#result === attempt) this.#result = void 0;
3281
+ throw cause;
3282
+ });
3283
+ this.#result = attempt;
3235
3284
  }
3236
- this.#result ??= this.#start(policy).catch((cause) => {
3237
- this.#result = void 0;
3238
- throw cause;
3239
- });
3240
3285
  return this.#result;
3241
3286
  }
3242
3287
  /**
@@ -3266,6 +3311,7 @@ var LoginSession = class {
3266
3311
  if (tokenStore === void 0) {
3267
3312
  return void 0;
3268
3313
  }
3314
+ const clears = this.#clears;
3269
3315
  const stored = await tokenStore.get();
3270
3316
  if (stored === void 0 || stored === "") {
3271
3317
  return void 0;
@@ -3274,6 +3320,9 @@ var LoginSession = class {
3274
3320
  if (tokenSet === void 0) {
3275
3321
  return void 0;
3276
3322
  }
3323
+ if (clears !== this.#clears) {
3324
+ throw new AuthError2("AUTH_REAUTHENTICATION_REQUIRED");
3325
+ }
3277
3326
  let credential;
3278
3327
  try {
3279
3328
  credential = parseManagementJwt(tokenSet.access_token);
@@ -3407,7 +3456,7 @@ var Route33Runtime = class {
3407
3456
  const projectsById = new Map(
3408
3457
  listings.flatMap(({ data }) => data).map((project) => [project.id, project])
3409
3458
  );
3410
- return rows.flatMap(({ org_id, project_id }) => {
3459
+ return rows.flatMap(({ org_id, project_id, cap }) => {
3411
3460
  const project = projectsById.get(project_id);
3412
3461
  if (project === void 0) {
3413
3462
  return [];
@@ -3415,6 +3464,7 @@ var Route33Runtime = class {
3415
3464
  return [
3416
3465
  {
3417
3466
  ...project,
3467
+ memberCap: cap,
3418
3468
  organisation: {
3419
3469
  id: org_id,
3420
3470
  name: organisationNames.get(org_id) ?? org_id
@@ -3530,7 +3580,10 @@ var Route33Runtime = class {
3530
3580
  }),
3531
3581
  logout: () => this.#session.clear(),
3532
3582
  "rotate-now": () => rotateNow({
3533
- lease: (policy) => this.#lease({ ...this.#options.policy, ...policy })
3583
+ lease: async (policy) => {
3584
+ await this.#session.credential({ allowBrowser: false });
3585
+ return this.#lease({ ...this.#options.policy, ...policy });
3586
+ }
3534
3587
  })
3535
3588
  };
3536
3589
  }
@@ -3544,6 +3597,7 @@ var Route33Runtime = class {
3544
3597
  await this.#hydrateState(installation);
3545
3598
  }
3546
3599
  const base = inspectSession({
3600
+ connected: await this.#session.hasSession(),
3547
3601
  readAttribution: () => attribution,
3548
3602
  readLease: () => installation?.state.snapshot()
3549
3603
  });
@@ -3551,10 +3605,9 @@ var Route33Runtime = class {
3551
3605
  if (attribution === void 0 || keyId === void 0) {
3552
3606
  return base;
3553
3607
  }
3608
+ const credential = await this.#session.credential({ allowBrowser: false }).catch(() => void 0);
3609
+ if (credential === void 0) return { ...base, connected: false };
3554
3610
  try {
3555
- const credential = await this.#session.credential({
3556
- allowBrowser: false
3557
- });
3558
3611
  const details = await enrichInspection({
3559
3612
  attribution,
3560
3613
  client: this.#client(),
@@ -3567,16 +3620,24 @@ var Route33Runtime = class {
3567
3620
  return base;
3568
3621
  }
3569
3622
  }
3623
+ /**
3624
+ * Lists the models a key on this budget may be limited to. Never opens a
3625
+ * browser; an organisation-level (personal) catalog needs the `models:read`
3626
+ * grant, so sessions signed in before that grant reject here.
3627
+ * @param scope - The budget being configured.
3628
+ * @returns Key-facing model ids with their readable names.
3629
+ */
3630
+ async catalogModelChoices(scope) {
3631
+ const credential = await this.#session.credential({ allowBrowser: false });
3632
+ return this.#client().listModelChoices(credential, scope);
3633
+ }
3570
3634
  /**
3571
3635
  * @param scope - The budget being configured. Omit to discover models for the current key.
3572
3636
  * @returns The management catalog for a budget, or the current key's gateway catalog.
3573
3637
  */
3574
3638
  async catalogModelIds(scope) {
3575
3639
  if (scope !== void 0) {
3576
- const credential = await this.#session.credential({
3577
- allowBrowser: false
3578
- });
3579
- return this.#client().listModelIds(credential, scope);
3640
+ return (await this.catalogModelChoices(scope)).map(({ id }) => id);
3580
3641
  }
3581
3642
  const workspace = await this.storedReusableKey();
3582
3643
  const personal = workspace === void 0 ? await this.#storedPersonalKey() : void 0;
@@ -3585,7 +3646,8 @@ var Route33Runtime = class {
3585
3646
  const credential = await this.#session.credential({
3586
3647
  allowBrowser: false
3587
3648
  });
3588
- return this.#client().listModelIds(credential, {});
3649
+ const choices = await this.#client().listModelChoices(credential, {});
3650
+ return choices.map(({ id }) => id);
3589
3651
  }
3590
3652
  return gatewayModelIds({
3591
3653
  baseURL: readEnvBaseUrl() ?? DEFAULT_BASE_URL,
@@ -3666,6 +3728,29 @@ var Route33Runtime = class {
3666
3728
  const key = keys.find((item) => item.id === keyId);
3667
3729
  return key === void 0 ? void 0 : { allowedModels: key.allowed_models };
3668
3730
  }
3731
+ /** V1 captures only the choice, not the plan; read only that owner's policy.
3732
+ * @param choice - The selected budget and its active organisation.
3733
+ * @returns The effective EUR cap, if the owner has a readable allowance.
3734
+ */
3735
+ async ownerBudgetCap(choice) {
3736
+ const token = await this.#session.credential({ allowBrowser: false });
3737
+ const subject = this.#subject(token);
3738
+ const client = this.#client();
3739
+ if (choice.kind === "personal") {
3740
+ const policy = await client.listBudgetPolicy(
3741
+ token,
3742
+ subject,
3743
+ choice.orgId
3744
+ );
3745
+ return policy.cap ?? void 0;
3746
+ }
3747
+ const budgets = await client.listProjectBudgets(
3748
+ token,
3749
+ subject,
3750
+ choice.orgId
3751
+ );
3752
+ return budgets.data.find((row) => row.project_id === choice.projectId)?.cap;
3753
+ }
3669
3754
  #client() {
3670
3755
  const client = this.#options.management.client;
3671
3756
  if (typeof client !== "function") {
@@ -3781,8 +3866,8 @@ var Route33Runtime = class {
3781
3866
  };
3782
3867
 
3783
3868
  // src/v1-entry/unattributed-prompt-gate/unattributed-prompt-gate.ts
3784
- var CONNECTION_COPY = "Not connected to Route 33. Run /connect, choose Route 33, and complete sign-in before assigning a budget with /r33.";
3785
- var ATTRIBUTION_COPY = "This folder has no Route 33 budget assigned. Run /r33, choose Route 33, and assign a budget.";
3869
+ var CONNECTION_COPY = "To sign in, run /connect and choose Route 33.";
3870
+ var ATTRIBUTION_COPY = "This folder has no Route 33 budget; open /r33 and choose Change budget.";
3786
3871
  var createUnattributedPromptGate = ({ directory, isConnected, outsideWorktree }) => async (input) => {
3787
3872
  if (input.model?.providerID !== "route_33") return Promise.resolve();
3788
3873
  if (!await isConnected()) throw new Error(CONNECTION_COPY);
@@ -3929,7 +4014,7 @@ var parseApiKey = (value) => {
3929
4014
  name: field("name"),
3930
4015
  org_id: field("org_id"),
3931
4016
  ownership_type: value["ownership_type"],
3932
- ...typeof value["owner_user_id"] === "string" ? { owner_user_id: value["owner_user_id"] } : {},
4017
+ ...isNullableString(value["user_id"]) ? { user_id: value["user_id"] } : {},
3933
4018
  project_id: value["project_id"],
3934
4019
  revoked: value["revoked"] === true,
3935
4020
  status
@@ -4172,6 +4257,18 @@ var keyReadOperations = (call) => {
4172
4257
  url.searchParams.set("cursor", cursor);
4173
4258
  return new Request(url, request);
4174
4259
  };
4260
+ const parsePage = (page) => {
4261
+ const collection = parseCollection(page, (row) => row);
4262
+ const data = collection.data.flatMap((row) => {
4263
+ try {
4264
+ return [parseApiKey(row)];
4265
+ } catch (error) {
4266
+ if (error instanceof ContractParseError) return [];
4267
+ throw error;
4268
+ }
4269
+ });
4270
+ return { ...collection, data };
4271
+ };
4175
4272
  return {
4176
4273
  get: async (accessToken, id, orgId) => parseApiKey(
4177
4274
  await call(
@@ -4186,7 +4283,7 @@ var keyReadOperations = (call) => {
4186
4283
  (cursor) => call(accessToken, orgId, (client) => {
4187
4284
  if (cursor !== "") client.use({ onRequest: withCursor(cursor) });
4188
4285
  return client.GET("/api/v1/api-keys", {});
4189
- }).then((page) => parseCollection(page, parseApiKey))
4286
+ }).then(parsePage)
4190
4287
  )
4191
4288
  };
4192
4289
  };
@@ -4250,29 +4347,55 @@ var keyWriteOperations = (call) => ({
4250
4347
  )
4251
4348
  });
4252
4349
 
4253
- // src/management/sdk-management-keys/exposed-catalog-ids/exposed-catalog-ids.ts
4254
- var exposedCatalogIds = async (readPage) => {
4255
- const ids = /* @__PURE__ */ new Set();
4350
+ // src/management/client-model-id/client-model-id.ts
4351
+ var clientModelId = (offeringId) => {
4352
+ const [provider, region, scope, vendor, ...modelParts] = offeringId.split(":");
4353
+ const slugParts = modelParts.at(-1) === "zdr" ? modelParts.slice(0, -1) : modelParts;
4354
+ const parts = [provider, region, scope, vendor, slugParts.join(":")];
4355
+ return parts.some((part) => part === void 0 || part === "") || /[\s,]/u.test(offeringId) ? void 0 : `${vendor ?? ""}/${slugParts.join(":")}`;
4356
+ };
4357
+
4358
+ // src/management/exposed-catalog-models/catalog-row-choices/catalog-row-choices.ts
4359
+ var catalogRowChoices = (row) => {
4360
+ if (!isRecord(row) || typeof row.id !== "string" || !row.id.trim() || typeof row.status !== "string")
4361
+ throw new ContractParseError("invalid model catalog item");
4362
+ if (row.status !== "exposed" || !Array.isArray(row.offerings)) return [];
4363
+ const label = typeof row.name === "string" && row.name.trim() !== "" ? row.name.trim() : void 0;
4364
+ return row.offerings.flatMap((offering) => {
4365
+ if (!isRecord(offering) || typeof offering.id !== "string" || offering.enabled !== true)
4366
+ return [];
4367
+ const id = clientModelId(offering.id);
4368
+ return id === void 0 ? [] : [{ id, name: label ?? id }];
4369
+ });
4370
+ };
4371
+
4372
+ // src/management/exposed-catalog-models/exposed-catalog-models.ts
4373
+ var exposedCatalogModels = async (readPage) => {
4374
+ const readRows = async (offset2) => {
4375
+ const page = await readPage(offset2);
4376
+ if (!isRecord(page) || !Array.isArray(page.models) || page.offset !== offset2 || typeof page.total !== "number" || !Number.isSafeInteger(page.total) || page.total < 0) {
4377
+ throw new ContractParseError("invalid model catalog page");
4378
+ }
4379
+ return { rows: page.models, total: page.total };
4380
+ };
4381
+ const names = /* @__PURE__ */ new Map();
4256
4382
  let offset = 0;
4257
4383
  for (; ; ) {
4258
- const page = await readPage(offset);
4259
- if (!isRecord(page) || !Array.isArray(page.models) || page.offset !== offset || typeof page.total !== "number" || !Number.isSafeInteger(page.total) || page.total < 0) {
4260
- throw new ContractParseError("invalid model catalog page");
4384
+ const { rows, total } = await readRows(offset);
4385
+ for (const { id, name } of rows.flatMap(catalogRowChoices)) {
4386
+ names.set(id, name);
4261
4387
  }
4262
- page.models.forEach((row) => {
4263
- if (!isRecord(row) || typeof row.id !== "string" || !row.id.trim() || typeof row.status !== "string")
4264
- throw new ContractParseError("invalid model catalog item");
4265
- if (row.status === "exposed") ids.add(row.id);
4266
- });
4267
- offset += page.models.length;
4268
- if (offset >= page.total || page.models.length === 0)
4269
- return [...ids].sort((left, right) => left.localeCompare(right));
4388
+ offset += rows.length;
4389
+ if (offset >= total || rows.length === 0) break;
4270
4390
  }
4391
+ return [...names].map(([id, name]) => ({ id, name })).sort(
4392
+ (left, right) => left.name.localeCompare(right.name) || left.id.localeCompare(right.id)
4393
+ );
4271
4394
  };
4272
4395
 
4273
4396
  // src/management/model-catalog-operations/model-catalog-operations.ts
4274
4397
  var modelCatalogOperations = (call) => ({
4275
- listModelIds: (token, { orgId, projectId }) => exposedCatalogIds(
4398
+ listModelChoices: (token, { orgId, projectId }) => exposedCatalogModels(
4276
4399
  (offset) => call(token, orgId, (client) => {
4277
4400
  const query = { offset };
4278
4401
  if (projectId !== void 0)
@@ -4329,6 +4452,29 @@ import {
4329
4452
  createRoute33Client,
4330
4453
  Route33SdkError
4331
4454
  } from "@r-33/sdk";
4455
+
4456
+ // src/management/sdk-management-call/rejection-of/rejection-of.ts
4457
+ var REFUSED = 422;
4458
+ var REJECTION_MAX_LENGTH = 200;
4459
+ var CONTROL = /\p{Cc}+/gu;
4460
+ var SECRET_PREFIXES = ["r33_", "eyj", "bearer "];
4461
+ var OPAQUE_MIN_LENGTH = 20;
4462
+ var rejectionOf = (outcome) => {
4463
+ const body = outcome.error;
4464
+ if (outcome.response?.status !== REFUSED || !isRecord(body)) return void 0;
4465
+ const raw = body["error"];
4466
+ if (typeof raw !== "string" || raw.length > REJECTION_MAX_LENGTH) {
4467
+ return void 0;
4468
+ }
4469
+ const sentence = raw.replaceAll(CONTROL, " ").replaceAll(/\s+/gu, " ").trim();
4470
+ const lower = sentence.toLowerCase();
4471
+ const secretShaped = SECRET_PREFIXES.some((prefix) => lower.includes(prefix)) || lower.split(" ").some(
4472
+ (word) => word.length >= OPAQUE_MIN_LENGTH && /\d/u.test(word) && /[a-z]/u.test(word)
4473
+ );
4474
+ return sentence === "" || secretShaped ? void 0 : sentence;
4475
+ };
4476
+
4477
+ // src/management/sdk-management-call/sdk-management-call.ts
4332
4478
  var API_PREFIX_PATTERN = /\/api\/v1\/?$/;
4333
4479
  var ACTIVE_ORG_HEADER = "X-Active-Org-Id";
4334
4480
  var createSdkCall = (options) => {
@@ -4375,9 +4521,13 @@ var createSdkCall = (options) => {
4375
4521
  const outcome = await run(callClient(accessToken, activeOrgId));
4376
4522
  const code = outcome.response?.status;
4377
4523
  if (code === void 0 || code < 200 || code >= 300) {
4524
+ const rejection = rejectionOf(outcome);
4378
4525
  throw new ManagementError(
4379
4526
  code === 401 ? "MANAGEMENT_UNAUTHORIZED" : "MANAGEMENT_HTTP_ERROR",
4380
- { statusCode: code }
4527
+ {
4528
+ ...rejection === void 0 ? {} : { rejection },
4529
+ statusCode: code
4530
+ }
4381
4531
  );
4382
4532
  }
4383
4533
  return outcome.data;