@r-33/opencode 0.4.7-rc.26 → 0.4.7-rc.28

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
@@ -42,8 +42,9 @@ the connect completes, the wizard takes over automatically:
42
42
 
43
43
  1. Run `/connect` and choose Route 33 — press `o` to open the sign-in
44
44
  link and finish the browser sign-in.
45
- 2. The budget picker follows in OpenCode — one of your Route 33 projects,
46
- or your personal key.
45
+ 2. The budget picker follows in OpenCode — your organisations come
46
+ first when you belong to several (budgets never mix organisations),
47
+ then that organisation's projects or your personal key.
47
48
  3. The route_33 models appear. Pick one and prompt away.
48
49
 
49
50
  A folder that is signed in but never attributed auto-runs the budget
@@ -100,8 +100,10 @@ var KeyLeaseError = class extends Error {
100
100
  );
101
101
  this.code = code;
102
102
  this.name = "KeyLeaseError";
103
+ this.statusCode = options?.statusCode;
103
104
  }
104
105
  code;
106
+ statusCode;
105
107
  };
106
108
 
107
109
  // src/key-lease/is-management-unauthorized/is-management-unauthorized.ts
@@ -239,6 +241,7 @@ var buildMintInput = (deps) => {
239
241
  managementSubject: deps.managementSubject
240
242
  });
241
243
  return {
244
+ ...deps.attribution.orgId === void 0 ? {} : { activeOrgId: deps.attribution.orgId },
242
245
  allowedModels: deps.extras?.allowedModels,
243
246
  description: deps.extras?.description,
244
247
  expiresAt: new Date(
@@ -341,6 +344,21 @@ var rejectLease = async (options) => {
341
344
  }
342
345
  };
343
346
 
347
+ // src/management/management-error/management-error.ts
348
+ var ManagementError = class extends Error {
349
+ constructor(code, options) {
350
+ super(
351
+ "Route 33 management request failed",
352
+ options?.cause === void 0 ? void 0 : { cause: "Route 33 management dependency failed" }
353
+ );
354
+ this.code = code;
355
+ this.name = "ManagementError";
356
+ this.statusCode = options?.statusCode;
357
+ }
358
+ code;
359
+ statusCode;
360
+ };
361
+
344
362
  // src/key-lease/mint-key/mint-key.ts
345
363
  var mintKey = async (options) => {
346
364
  const now = options.clock();
@@ -374,7 +392,10 @@ var mintKey = async (options) => {
374
392
  if (isManagementUnauthorized(cause)) {
375
393
  throw new KeyLeaseError("KEY_LEASE_UNAUTHORIZED", { cause });
376
394
  }
377
- throw new KeyLeaseError("KEY_LEASE_MINT_FAILED", { cause });
395
+ throw new KeyLeaseError("KEY_LEASE_MINT_FAILED", {
396
+ cause,
397
+ ...cause instanceof ManagementError && cause.statusCode !== void 0 ? { statusCode: cause.statusCode } : {}
398
+ });
378
399
  }
379
400
  return {
380
401
  key: material.key,
@@ -615,16 +636,33 @@ var leaseForAttribution = async (options) => {
615
636
  import * as path from "node:path";
616
637
  var LEGACY_KEY_RELATIVE_PATH = path.join(".opencode", "route33.key");
617
638
 
639
+ // src/attribution/parse-attribution/read-org-id/read-org-id.ts
640
+ var readOrgId = (value) => {
641
+ const orgId = value["orgId"];
642
+ if (orgId === void 0) {
643
+ return void 0;
644
+ }
645
+ if (typeof orgId !== "string") {
646
+ throw new ContractParseError("invalid attribution");
647
+ }
648
+ return orgId;
649
+ };
650
+
618
651
  // src/attribution/parse-attribution/parse-attribution.ts
619
652
  var parseAttribution = (value) => {
620
653
  if (!isRecord(value)) {
621
654
  throw new ContractParseError("invalid attribution");
622
655
  }
656
+ const orgId = readOrgId(value);
623
657
  if (value["kind"] === "personal") {
624
- return { kind: "personal" };
658
+ return { kind: "personal", ...orgId === void 0 ? {} : { orgId } };
625
659
  }
626
660
  if (value["kind"] === "project" && typeof value["projectId"] === "string") {
627
- return { kind: "project", projectId: value["projectId"] };
661
+ return {
662
+ kind: "project",
663
+ ...orgId === void 0 ? {} : { orgId },
664
+ projectId: value["projectId"]
665
+ };
628
666
  }
629
667
  throw new ContractParseError("invalid attribution");
630
668
  };
@@ -732,21 +770,6 @@ var drainPages = async (fetchPage) => {
732
770
  return { data, page: { ...page, has_more: false, next_cursor: null } };
733
771
  };
734
772
 
735
- // src/management/management-error/management-error.ts
736
- var ManagementError = class extends Error {
737
- constructor(code, options) {
738
- super(
739
- "Route 33 management request failed",
740
- options?.cause === void 0 ? void 0 : { cause: "Route 33 management dependency failed" }
741
- );
742
- this.code = code;
743
- this.name = "ManagementError";
744
- this.statusCode = options?.statusCode;
745
- }
746
- code;
747
- statusCode;
748
- };
749
-
750
773
  // src/management/sdk-management-keys/sdk-management-keys.ts
751
774
  import {
752
775
  createRoute33Client,
@@ -784,6 +807,26 @@ var parseApiKey = (value) => {
784
807
  };
785
808
  };
786
809
 
810
+ // src/contract/management-keys/parse-budget-policy/parse-budget-policy.ts
811
+ var parseBudgetPolicy = (value) => {
812
+ if (!isRecord(value)) {
813
+ throw new ContractParseError("invalid budget policy");
814
+ }
815
+ const cap = value["cap"];
816
+ if (typeof cap !== "number" && cap !== null) {
817
+ throw new ContractParseError("invalid budget policy");
818
+ }
819
+ const orgId = value["org_id"];
820
+ if (typeof orgId !== "string") {
821
+ throw new ContractParseError("invalid budget policy");
822
+ }
823
+ const source = value["source"];
824
+ if (source !== "own_cap" && source !== "project_default" && source !== "org_default" && source !== "tenant_default" && source !== "platform_default") {
825
+ throw new ContractParseError("invalid budget policy");
826
+ }
827
+ return { cap, org_id: orgId, source };
828
+ };
829
+
787
830
  // src/contract/management-keys/parse-collection/parse-collection.ts
788
831
  var parseCollection = (value, parseItem) => {
789
832
  if (!isRecord(value)) {
@@ -993,28 +1036,46 @@ var parseUserMemberships = (value) => {
993
1036
  };
994
1037
  };
995
1038
 
1039
+ // src/management/sdk-management-keys/mint-request-body/mint-request-body.ts
1040
+ var buildMintRequestBody = (input) => ({
1041
+ ...input.allowedModels === void 0 ? {} : { allowed_models: [...input.allowedModels] },
1042
+ budget_currency: "EUR",
1043
+ ...input.description === void 0 ? {} : { description: input.description },
1044
+ expires_at: input.expiresAt,
1045
+ ...input.maxBudgetAmount === void 0 ? {} : { max_budget_amount: input.maxBudgetAmount },
1046
+ name: input.name,
1047
+ ...input.ownerId === null ? {} : { owner_user_id: input.ownerId },
1048
+ owner_type: input.ownerType,
1049
+ ...input.projectId === null ? {} : { project_id: input.projectId }
1050
+ });
1051
+
996
1052
  // src/management/sdk-management-keys/sdk-management-keys.ts
997
1053
  var API_PREFIX_PATTERN = /\/api\/v1\/?$/;
1054
+ var ACTIVE_ORG_HEADER = "X-Active-Org-Id";
998
1055
  var sdkManagementKeys = (options) => {
999
1056
  const baseUrl = options.apiBaseUrl.replace(API_PREFIX_PATTERN, "");
1000
1057
  const timeoutMs = options.timeoutMs ?? 1e4;
1001
1058
  const fetchImpl = options.fetchImpl;
1002
- const observingFetch = (status) => fetchImpl === void 0 ? void 0 : async (input, init) => {
1003
- const response = await fetchImpl(input, init);
1004
- status.code = response.status;
1005
- return response;
1059
+ const activeOrgFetch = (activeOrgId) => activeOrgId === void 0 ? void 0 : (input, init) => {
1060
+ if (input instanceof Request) {
1061
+ input.headers.set(ACTIVE_ORG_HEADER, activeOrgId);
1062
+ }
1063
+ if (init?.headers instanceof Headers) {
1064
+ init.headers.set(ACTIVE_ORG_HEADER, activeOrgId);
1065
+ }
1066
+ return (fetchImpl ?? globalThis.fetch)(input, init);
1006
1067
  };
1007
- const callClient = (accessToken, status) => {
1068
+ const callClient = (accessToken, activeOrgId) => {
1008
1069
  const provider = {
1009
1070
  getToken: () => Promise.resolve(accessToken.value),
1010
1071
  onUnauthorized: () => Promise.resolve(accessToken.value)
1011
1072
  };
1012
- const observedFetch = observingFetch(status);
1073
+ const fetch2 = activeOrgFetch(activeOrgId) ?? fetchImpl;
1013
1074
  return createRoute33Client({
1014
1075
  baseUrl,
1015
1076
  provider,
1016
1077
  timeoutMs,
1017
- ...observedFetch === void 0 ? {} : { fetch: observedFetch }
1078
+ ...fetch2 === void 0 ? {} : { fetch: fetch2 }
1018
1079
  });
1019
1080
  };
1020
1081
  const mapFailure = (cause) => {
@@ -1030,11 +1091,10 @@ var sdkManagementKeys = (options) => {
1030
1091
  }
1031
1092
  return new ManagementError("MANAGEMENT_NETWORK_ERROR", { cause });
1032
1093
  };
1033
- const call = async (accessToken, run) => {
1034
- const status = { code: void 0 };
1094
+ const call = async (accessToken, activeOrgId, run) => {
1035
1095
  try {
1036
- const outcome = await run(callClient(accessToken, status));
1037
- const code = status.code;
1096
+ const outcome = await run(callClient(accessToken, activeOrgId));
1097
+ const code = outcome.response?.status;
1038
1098
  if (code === void 0 || code < 200 || code >= 300) {
1039
1099
  throw new ManagementError(
1040
1100
  code === 401 ? "MANAGEMENT_UNAUTHORIZED" : "MANAGEMENT_HTTP_ERROR",
@@ -1050,24 +1110,16 @@ var sdkManagementKeys = (options) => {
1050
1110
  create: async (accessToken, input) => parseKeyMaterial(
1051
1111
  await call(
1052
1112
  accessToken,
1113
+ input.activeOrgId,
1053
1114
  (client) => client.POST("/api/v1/api-keys", {
1054
- body: {
1055
- ...input.allowedModels === void 0 ? {} : { allowed_models: [...input.allowedModels] },
1056
- budget_currency: "EUR",
1057
- ...input.description === void 0 ? {} : { description: input.description },
1058
- expires_at: input.expiresAt,
1059
- ...input.maxBudgetAmount === void 0 ? {} : { max_budget_amount: input.maxBudgetAmount },
1060
- name: input.name,
1061
- ...input.ownerId === null ? {} : { owner_user_id: input.ownerId },
1062
- owner_type: input.ownerType,
1063
- ...input.projectId === null ? {} : { project_id: input.projectId }
1064
- }
1115
+ body: buildMintRequestBody(input)
1065
1116
  })
1066
1117
  )
1067
1118
  ),
1068
1119
  delete: async (accessToken, id) => {
1069
1120
  await call(
1070
1121
  accessToken,
1122
+ void 0,
1071
1123
  (client) => client.DELETE("/api/v1/api-keys/{api_key_id}", {
1072
1124
  params: { path: { api_key_id: id } }
1073
1125
  })
@@ -1076,30 +1128,50 @@ var sdkManagementKeys = (options) => {
1076
1128
  get: async (accessToken, id) => parseApiKey(
1077
1129
  await call(
1078
1130
  accessToken,
1131
+ void 0,
1079
1132
  (client) => client.GET("/api/v1/api-keys/{api_key_id}", {
1080
1133
  params: { path: { api_key_id: id } }
1081
1134
  })
1082
1135
  )
1083
1136
  ),
1084
1137
  list: async (accessToken) => parseCollection(
1085
- await call(accessToken, (client) => client.GET("/api/v1/api-keys", {})),
1138
+ await call(
1139
+ accessToken,
1140
+ void 0,
1141
+ (client) => client.GET("/api/v1/api-keys", {})
1142
+ ),
1086
1143
  parseApiKey
1087
1144
  ),
1088
1145
  listOrganizations: async (accessToken) => parseCollection(
1089
- await call(accessToken, (client) => client.GET("/api/v1/orgs", {})),
1146
+ await call(
1147
+ accessToken,
1148
+ void 0,
1149
+ (client) => client.GET("/api/v1/orgs", {})
1150
+ ),
1090
1151
  parseOrganization
1091
1152
  ),
1092
1153
  listMemberships: async (accessToken, userId) => parseUserMemberships(
1093
1154
  await call(
1094
1155
  accessToken,
1156
+ void 0,
1095
1157
  (client) => client.GET("/api/v1/users/{user_id}/memberships", {
1096
1158
  params: { path: { user_id: userId } }
1097
1159
  })
1098
1160
  )
1099
1161
  ),
1100
- listProjectBudgets: async (accessToken, userId) => parseCollection(
1162
+ listBudgetPolicy: async (accessToken, userId, activeOrgId) => parseBudgetPolicy(
1163
+ await call(
1164
+ accessToken,
1165
+ activeOrgId,
1166
+ (client) => client.GET("/api/v1/users/{user_id}/budget-policy", {
1167
+ params: { path: { user_id: userId } }
1168
+ })
1169
+ )
1170
+ ),
1171
+ listProjectBudgets: async (accessToken, userId, activeOrgId) => parseCollection(
1101
1172
  await call(
1102
1173
  accessToken,
1174
+ activeOrgId,
1103
1175
  (client) => client.GET("/api/v1/users/{user_id}/project-budgets", {
1104
1176
  params: { path: { user_id: userId } }
1105
1177
  })
@@ -1118,6 +1190,7 @@ var sdkManagementKeys = (options) => {
1118
1190
  };
1119
1191
  const fetchPage = (cursor) => call(
1120
1192
  accessToken,
1193
+ void 0,
1121
1194
  (client) => client.GET("/api/v1/projects", {
1122
1195
  params: {
1123
1196
  query: cursor === "" ? query : { ...query, cursor }
@@ -1129,6 +1202,7 @@ var sdkManagementKeys = (options) => {
1129
1202
  regenerate: async (accessToken, id, expiresAt) => parseKeyMaterial(
1130
1203
  await call(
1131
1204
  accessToken,
1205
+ void 0,
1132
1206
  (client) => client.POST("/api/v1/api-keys/{api_key_id}/secrets", {
1133
1207
  body: expiresAt === void 0 ? {} : { expires_at: expiresAt },
1134
1208
  params: { path: { api_key_id: id } }
@@ -1138,6 +1212,7 @@ var sdkManagementKeys = (options) => {
1138
1212
  update: async (accessToken, id, input) => parseApiKey(
1139
1213
  await call(
1140
1214
  accessToken,
1215
+ void 0,
1141
1216
  (client) => client.PATCH("/api/v1/api-keys/{api_key_id}", {
1142
1217
  body: {
1143
1218
  budget_currency: "EUR",
@@ -1158,7 +1233,7 @@ import {
1158
1233
  } from "@r-33/auth-browser";
1159
1234
 
1160
1235
  // src/requested-scopes/requested-scopes.ts
1161
- var REQUESTED_OAUTH_SCOPES = "openid offline_access orgs:read projects:read users:read keys:read keys:write";
1236
+ var REQUESTED_OAUTH_SCOPES = "openid offline_access orgs:read profile projects:read users:read keys:read keys:write";
1162
1237
 
1163
1238
  // src/ux/callback-page-copy/callback-page-copy.ts
1164
1239
  var CALLBACK_PAGE_COPY = {