@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/README.md CHANGED
@@ -45,14 +45,32 @@ the connect completes, the wizard takes over automatically:
45
45
  2. The budget picker follows in OpenCode — your organisations come
46
46
  first when you belong to several (budgets never mix organisations),
47
47
  then that organisation's projects or your personal key.
48
- 3. The route_33 models appear. Pick one and prompt away.
48
+ 3. Key setup follows for that budget: bind one of your existing keys for
49
+ it, or create one — name, monthly cap in EUR, description, expiry, and
50
+ the models it may use (tick several, or allow all).
51
+ 4. The route_33 models appear. Pick one and prompt away — OpenCode v2
52
+ asks you to pick when no route_33 model is active yet.
53
+
54
+ The cap is checked against what the budget allows you: the question shows
55
+ the maximum when the budget sets one, and asks again for a higher amount.
56
+ Binding a key whose secret is not stored on this machine rotates it — its
57
+ other users stop working — so you are asked first. Esc on any question
58
+ cancels the setup; nothing is created.
59
+
60
+ On OpenCode v2 the setup is blocking: a waiting dialog covers the pauses
61
+ between the steps until the key exists, and a key Route 33 still refuses
62
+ shows the reason and returns to the questions with your answers kept. The
63
+ model pick switches the model inside a session; from the home screen it
64
+ ends with a pointer to `/models`.
49
65
 
50
66
  A folder that is signed in but never attributed auto-runs the budget
51
67
  picker at launch — the prompt stays locked behind a setup mode until a
52
68
  budget is assigned, with Enter resuming setup; route_33 model requests
53
- from unattributed folders are refused server-side with the same guidance
54
- where the lock is unavailable. A browser is required for every sign-in;
55
- restarts afterwards sign in silently.
69
+ from unattributed folders are refused server-side where the lock is
70
+ unavailable. A message never names both commands: it points to `/connect`
71
+ while you are signed out, and to the `/r33` menu once you are signed in. A
72
+ browser is required for every sign-in; restarts afterwards sign in
73
+ silently.
56
74
 
57
75
  ## Commands
58
76
 
@@ -62,7 +80,13 @@ restarts afterwards sign in silently.
62
80
  | `/r33 inspect` | Show the assigned budget, the active key, and its rotation state |
63
81
  | `/r33 rotate` | Rotate the key immediately |
64
82
  | `/r33 reset` | Clear the assigned budget — the wizard runs again on the next start |
65
- | `/r33 logout` | Sign out of Route 33 |
83
+
84
+ To sign out, run `/connect`, choose Route 33, select your account and press
85
+ Ctrl+D twice. Once the last account is removed, the plugin forgets its
86
+ sign-in on this machine; your key and budget stay.
87
+
88
+ The menu's budget entry — **Attribute** on OpenCode v2, **Change budget**
89
+ on v1 — reopens the budget picker and the key setup for this folder.
66
90
 
67
91
  A browser is required for every sign-in; restarts afterwards sign in silently.
68
92
 
@@ -74,6 +98,12 @@ folder never changes who this one bills for. Keys are short-lived and rotate
74
98
  automatically; nothing secret is ever written to your project, logs, or
75
99
  terminal.
76
100
 
101
+ ## Session cost
102
+
103
+ The cost OpenCode shows for a session on a route_33 model is an estimate
104
+ from USD list prices. Route 33 meters your budget itself, in EUR — the two
105
+ differ, and a model without a published price shows no cost.
106
+
77
107
  ## Direct key mode
78
108
 
79
109
  Set `ROUTE33_API_KEY` to a gateway key and the wizard is bypassed entirely —
@@ -90,18 +120,21 @@ export ROUTE33_API_KEY=r33_sk_...
90
120
  Connecting is always explicit — run `/connect` yourself. What auto-starts
91
121
  at OpenCode launch is the attribution wizard for folders that are signed
92
122
  in but have no budget assigned yet. Suppress that with
93
- `ROUTE33_AUTOCONNECT=0` and assign a budget manually instead — `/r33` or
94
- `/connect`. On remote or headless machines, use `ROUTE33_API_KEY` instead
95
- of the wizard.
123
+ `ROUTE33_AUTOCONNECT=0` and assign the budget from the `/r33` menu when
124
+ you choose to; on OpenCode v1 the opt-out switches the `/r33` menu off as
125
+ well. On remote or headless machines, use `ROUTE33_API_KEY` instead of the
126
+ wizard.
96
127
 
97
128
  ## Troubleshooting
98
129
 
99
- | Symptom | What to do |
100
- | --------------------------------------------- | -------------------------------------------------------------------------------- |
101
- | No route_33 models after start | `/connect` → Route 33 — the silent sign-in expired; sign in once and they return |
102
- | "This Route 33 build has no OAuth client id" | Update the plugin to a released build, or set `ROUTE33_CLIENT_ID` and restart |
103
- | "needs a Git worktree" | Open a Git repository (or `git init`) in the folder, then connect again |
104
- | Sign-in link never appears / SSH-only machine | Sign-in needs a browser on the same machine — there is no device-flow fallback |
130
+ | Symptom | What to do |
131
+ | ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
132
+ | No route_33 models after start | `/connect` → Route 33 — the silent sign-in expired; sign in once and they return |
133
+ | "This Route 33 build has no OAuth client id" | Update the plugin to a released build, or set `ROUTE33_CLIENT_ID` and restart |
134
+ | "To assign a Route 33 budget, open a Git repository." | Open a Git repository (or `git init`) in the folder, then connect again |
135
+ | Sign-in link never appears / SSH-only machine | Sign-in needs a browser on the same machine — there is no device-flow fallback |
136
+ | "Route 33 — key not created" (OpenCode v2) | The dialog gives Route 33's reason, such as a cap above your allowance — change the answer and confirm again |
137
+ | "Choosing individual models needs a fresh Route 33 sign-in." | A personal budget lists its models only for a sign-in that carries the `models:read` permission; the next browser sign-in adds it |
105
138
 
106
139
  ## Configuration
107
140
 
@@ -113,3 +146,7 @@ of the wizard.
113
146
  | `ROUTE33_AUTH_ISSUER` | OAuth issuer | `https://auth.route33.ai` |
114
147
  | `ROUTE33_API_BASE` | Management API base | `https://mapi.route33.ai/api/v1` |
115
148
  | `ROUTE33_BASE_URL` | Gateway models base | `https://api.route33.ai/v1` |
149
+
150
+ A custom `ROUTE33_CLIENT_ID` must allow every scope the plugin requests —
151
+ `models:read`, which lists the models of a personal budget, among them —
152
+ or the auth server refuses the sign-in with `invalid_scope`.
@@ -240,9 +240,11 @@ var KeyLeaseError = class extends Error {
240
240
  );
241
241
  this.code = code;
242
242
  this.name = "KeyLeaseError";
243
+ this.rejection = options?.rejection;
243
244
  this.statusCode = options?.statusCode;
244
245
  }
245
246
  code;
247
+ rejection;
246
248
  statusCode;
247
249
  };
248
250
 
@@ -407,6 +409,33 @@ var commitStagedReplacement = async (stage, save, persist) => {
407
409
  }
408
410
  };
409
411
 
412
+ // src/management/management-error/management-error.ts
413
+ var ManagementError = class extends Error {
414
+ constructor(code, options) {
415
+ super(
416
+ "Route 33 management request failed",
417
+ options?.cause === void 0 ? void 0 : { cause: "Route 33 management dependency failed" }
418
+ );
419
+ this.code = code;
420
+ this.name = "ManagementError";
421
+ this.rejection = options?.rejection;
422
+ this.statusCode = options?.statusCode;
423
+ }
424
+ code;
425
+ rejection;
426
+ statusCode;
427
+ };
428
+
429
+ // src/key-lease/mint-failure/mint-failure.ts
430
+ var mintFailure = (cause) => {
431
+ const response = cause instanceof ManagementError ? cause : void 0;
432
+ return new KeyLeaseError("KEY_LEASE_MINT_FAILED", {
433
+ cause,
434
+ ...response?.rejection === void 0 ? {} : { rejection: response.rejection },
435
+ ...response?.statusCode === void 0 ? {} : { statusCode: response.statusCode }
436
+ });
437
+ };
438
+
410
439
  // src/key-lease/mint-fresh-lease/mint-fresh-lease.ts
411
440
  var mintFreshLease = async (deps) => {
412
441
  const now = deps.clock();
@@ -437,7 +466,7 @@ var mintFreshLease = async (deps) => {
437
466
  input
438
467
  );
439
468
  } catch (cause) {
440
- throw new KeyLeaseError("KEY_LEASE_MINT_FAILED", { cause });
469
+ throw mintFailure(cause);
441
470
  }
442
471
  const minted = {
443
472
  key: material.key,
@@ -520,21 +549,6 @@ var rejectLease = async (options) => {
520
549
  }
521
550
  };
522
551
 
523
- // src/management/management-error/management-error.ts
524
- var ManagementError = class extends Error {
525
- constructor(code, options) {
526
- super(
527
- "Route 33 management request failed",
528
- options?.cause === void 0 ? void 0 : { cause: "Route 33 management dependency failed" }
529
- );
530
- this.code = code;
531
- this.name = "ManagementError";
532
- this.statusCode = options?.statusCode;
533
- }
534
- code;
535
- statusCode;
536
- };
537
-
538
552
  // src/key-lease/mint-key/mint-key.ts
539
553
  var mintKey = async (options) => {
540
554
  const now = options.clock();
@@ -568,10 +582,7 @@ var mintKey = async (options) => {
568
582
  if (isManagementUnauthorized(cause)) {
569
583
  throw new KeyLeaseError("KEY_LEASE_UNAUTHORIZED", { cause });
570
584
  }
571
- throw new KeyLeaseError("KEY_LEASE_MINT_FAILED", {
572
- cause,
573
- ...cause instanceof ManagementError && cause.statusCode !== void 0 ? { statusCode: cause.statusCode } : {}
574
- });
585
+ throw mintFailure(cause);
575
586
  }
576
587
  return {
577
588
  key: material.key,
@@ -849,7 +860,7 @@ var parseApiKey = (value) => {
849
860
  name: field("name"),
850
861
  org_id: field("org_id"),
851
862
  ownership_type: value["ownership_type"],
852
- ...typeof value["owner_user_id"] === "string" ? { owner_user_id: value["owner_user_id"] } : {},
863
+ ...isNullableString(value["user_id"]) ? { user_id: value["user_id"] } : {},
853
864
  project_id: value["project_id"],
854
865
  revoked: value["revoked"] === true,
855
866
  status
@@ -1092,6 +1103,18 @@ var keyReadOperations = (call) => {
1092
1103
  url.searchParams.set("cursor", cursor);
1093
1104
  return new Request(url, request);
1094
1105
  };
1106
+ const parsePage = (page) => {
1107
+ const collection = parseCollection(page, (row) => row);
1108
+ const data = collection.data.flatMap((row) => {
1109
+ try {
1110
+ return [parseApiKey(row)];
1111
+ } catch (error) {
1112
+ if (error instanceof ContractParseError) return [];
1113
+ throw error;
1114
+ }
1115
+ });
1116
+ return { ...collection, data };
1117
+ };
1095
1118
  return {
1096
1119
  get: async (accessToken, id, orgId) => parseApiKey(
1097
1120
  await call(
@@ -1106,7 +1129,7 @@ var keyReadOperations = (call) => {
1106
1129
  (cursor) => call(accessToken, orgId, (client) => {
1107
1130
  if (cursor !== "") client.use({ onRequest: withCursor(cursor) });
1108
1131
  return client.GET("/api/v1/api-keys", {});
1109
- }).then((page) => parseCollection(page, parseApiKey))
1132
+ }).then(parsePage)
1110
1133
  )
1111
1134
  };
1112
1135
  };
@@ -1170,29 +1193,55 @@ var keyWriteOperations = (call) => ({
1170
1193
  )
1171
1194
  });
1172
1195
 
1173
- // src/management/sdk-management-keys/exposed-catalog-ids/exposed-catalog-ids.ts
1174
- var exposedCatalogIds = async (readPage) => {
1175
- const ids = /* @__PURE__ */ new Set();
1196
+ // src/management/client-model-id/client-model-id.ts
1197
+ var clientModelId = (offeringId) => {
1198
+ const [provider, region, scope, vendor, ...modelParts] = offeringId.split(":");
1199
+ const slugParts = modelParts.at(-1) === "zdr" ? modelParts.slice(0, -1) : modelParts;
1200
+ const parts = [provider, region, scope, vendor, slugParts.join(":")];
1201
+ return parts.some((part) => part === void 0 || part === "") || /[\s,]/u.test(offeringId) ? void 0 : `${vendor ?? ""}/${slugParts.join(":")}`;
1202
+ };
1203
+
1204
+ // src/management/exposed-catalog-models/catalog-row-choices/catalog-row-choices.ts
1205
+ var catalogRowChoices = (row) => {
1206
+ if (!isRecord(row) || typeof row.id !== "string" || !row.id.trim() || typeof row.status !== "string")
1207
+ throw new ContractParseError("invalid model catalog item");
1208
+ if (row.status !== "exposed" || !Array.isArray(row.offerings)) return [];
1209
+ const label = typeof row.name === "string" && row.name.trim() !== "" ? row.name.trim() : void 0;
1210
+ return row.offerings.flatMap((offering) => {
1211
+ if (!isRecord(offering) || typeof offering.id !== "string" || offering.enabled !== true)
1212
+ return [];
1213
+ const id = clientModelId(offering.id);
1214
+ return id === void 0 ? [] : [{ id, name: label ?? id }];
1215
+ });
1216
+ };
1217
+
1218
+ // src/management/exposed-catalog-models/exposed-catalog-models.ts
1219
+ var exposedCatalogModels = async (readPage) => {
1220
+ const readRows = async (offset2) => {
1221
+ const page = await readPage(offset2);
1222
+ if (!isRecord(page) || !Array.isArray(page.models) || page.offset !== offset2 || typeof page.total !== "number" || !Number.isSafeInteger(page.total) || page.total < 0) {
1223
+ throw new ContractParseError("invalid model catalog page");
1224
+ }
1225
+ return { rows: page.models, total: page.total };
1226
+ };
1227
+ const names = /* @__PURE__ */ new Map();
1176
1228
  let offset = 0;
1177
1229
  for (; ; ) {
1178
- const page = await readPage(offset);
1179
- if (!isRecord(page) || !Array.isArray(page.models) || page.offset !== offset || typeof page.total !== "number" || !Number.isSafeInteger(page.total) || page.total < 0) {
1180
- throw new ContractParseError("invalid model catalog page");
1230
+ const { rows, total } = await readRows(offset);
1231
+ for (const { id, name } of rows.flatMap(catalogRowChoices)) {
1232
+ names.set(id, name);
1181
1233
  }
1182
- page.models.forEach((row) => {
1183
- if (!isRecord(row) || typeof row.id !== "string" || !row.id.trim() || typeof row.status !== "string")
1184
- throw new ContractParseError("invalid model catalog item");
1185
- if (row.status === "exposed") ids.add(row.id);
1186
- });
1187
- offset += page.models.length;
1188
- if (offset >= page.total || page.models.length === 0)
1189
- return [...ids].sort((left, right) => left.localeCompare(right));
1234
+ offset += rows.length;
1235
+ if (offset >= total || rows.length === 0) break;
1190
1236
  }
1237
+ return [...names].map(([id, name]) => ({ id, name })).sort(
1238
+ (left, right) => left.name.localeCompare(right.name) || left.id.localeCompare(right.id)
1239
+ );
1191
1240
  };
1192
1241
 
1193
1242
  // src/management/model-catalog-operations/model-catalog-operations.ts
1194
1243
  var modelCatalogOperations = (call) => ({
1195
- listModelIds: (token, { orgId, projectId }) => exposedCatalogIds(
1244
+ listModelChoices: (token, { orgId, projectId }) => exposedCatalogModels(
1196
1245
  (offset) => call(token, orgId, (client) => {
1197
1246
  const query = { offset };
1198
1247
  if (projectId !== void 0)
@@ -1249,6 +1298,29 @@ import {
1249
1298
  createRoute33Client,
1250
1299
  Route33SdkError
1251
1300
  } from "@r-33/sdk";
1301
+
1302
+ // src/management/sdk-management-call/rejection-of/rejection-of.ts
1303
+ var REFUSED = 422;
1304
+ var REJECTION_MAX_LENGTH = 200;
1305
+ var CONTROL = /\p{Cc}+/gu;
1306
+ var SECRET_PREFIXES = ["r33_", "eyj", "bearer "];
1307
+ var OPAQUE_MIN_LENGTH = 20;
1308
+ var rejectionOf = (outcome) => {
1309
+ const body = outcome.error;
1310
+ if (outcome.response?.status !== REFUSED || !isRecord(body)) return void 0;
1311
+ const raw = body["error"];
1312
+ if (typeof raw !== "string" || raw.length > REJECTION_MAX_LENGTH) {
1313
+ return void 0;
1314
+ }
1315
+ const sentence = raw.replaceAll(CONTROL, " ").replaceAll(/\s+/gu, " ").trim();
1316
+ const lower = sentence.toLowerCase();
1317
+ const secretShaped = SECRET_PREFIXES.some((prefix) => lower.includes(prefix)) || lower.split(" ").some(
1318
+ (word) => word.length >= OPAQUE_MIN_LENGTH && /\d/u.test(word) && /[a-z]/u.test(word)
1319
+ );
1320
+ return sentence === "" || secretShaped ? void 0 : sentence;
1321
+ };
1322
+
1323
+ // src/management/sdk-management-call/sdk-management-call.ts
1252
1324
  var API_PREFIX_PATTERN = /\/api\/v1\/?$/;
1253
1325
  var ACTIVE_ORG_HEADER = "X-Active-Org-Id";
1254
1326
  var createSdkCall = (options) => {
@@ -1295,9 +1367,13 @@ var createSdkCall = (options) => {
1295
1367
  const outcome = await run(callClient(accessToken, activeOrgId));
1296
1368
  const code = outcome.response?.status;
1297
1369
  if (code === void 0 || code < 200 || code >= 300) {
1370
+ const rejection = rejectionOf(outcome);
1298
1371
  throw new ManagementError(
1299
1372
  code === 401 ? "MANAGEMENT_UNAUTHORIZED" : "MANAGEMENT_HTTP_ERROR",
1300
- { statusCode: code }
1373
+ {
1374
+ ...rejection === void 0 ? {} : { rejection },
1375
+ statusCode: code
1376
+ }
1301
1377
  );
1302
1378
  }
1303
1379
  return outcome.data;
@@ -1358,7 +1434,7 @@ import {
1358
1434
  } from "@r-33/auth-browser";
1359
1435
 
1360
1436
  // src/requested-scopes/requested-scopes.ts
1361
- var REQUESTED_OAUTH_SCOPES = "openid offline_access orgs:read profile projects:read users:read keys:read keys:write";
1437
+ var REQUESTED_OAUTH_SCOPES = "openid offline_access orgs:read profile projects:read users:read keys:read keys:write models:read";
1362
1438
 
1363
1439
  // src/ux/callback-page-copy/callback-page-copy.ts
1364
1440
  var CALLBACK_PAGE_COPY = {