@r-33/opencode 0.5.0 → 0.5.1

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
@@ -98,6 +98,11 @@ folder never changes who this one bills for. Keys are short-lived and rotate
98
98
  automatically; nothing secret is ever written to your project, logs, or
99
99
  terminal.
100
100
 
101
+ Outside a Git repository a folder has no budget of its own: `/connect`
102
+ offers your personal key, and that one choice covers every such folder on
103
+ this machine. To change it, open `/r33`: **Attribute** in such a folder,
104
+ **Global key** inside a repository.
105
+
101
106
  ## Session cost
102
107
 
103
108
  The cost OpenCode shows for a session on a route_33 model is an estimate
@@ -131,7 +136,6 @@ wizard.
131
136
  | ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
132
137
  | No route_33 models after start | `/connect` → Route 33 — the silent sign-in expired; sign in once and they return |
133
138
  | "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
139
  | Sign-in link never appears / SSH-only machine | Sign-in needs a browser on the same machine — there is no device-flow fallback |
136
140
  | "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
141
  | "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 |
@@ -90,6 +90,12 @@ var parseManagementSubject = (credential) => {
90
90
  return subject;
91
91
  };
92
92
 
93
+ // src/cli/read-env-client-id/read-env-client-id.ts
94
+ var readEnvClientId = (env = process.env) => {
95
+ const value = env["ROUTE33_CLIENT_ID"]?.trim();
96
+ return value === "" ? void 0 : value;
97
+ };
98
+
93
99
  // src/attribution/read-legacy-key/read-legacy-key.ts
94
100
  import * as path from "node:path";
95
101
  var LEGACY_KEY_RELATIVE_PATH = path.join(".opencode", "route33.key");
@@ -1646,12 +1652,6 @@ var createLiveProductionSmokePlan = (options) => {
1646
1652
  };
1647
1653
  };
1648
1654
 
1649
- // src/cli/read-env-client-id/read-env-client-id.ts
1650
- var readEnvClientId = (env = process.env) => {
1651
- const value = env["ROUTE33_CLIENT_ID"]?.trim();
1652
- return value === "" ? void 0 : value;
1653
- };
1654
-
1655
1655
  // src/cli/production-smoke/production-smoke.ts
1656
1656
  var productionSmokeClientId = readEnvClientId() ?? DEFAULT_ROUTE33_CLIENT_ID;
1657
1657
  var ProductionSmoke = class {
package/dist/index.js CHANGED
@@ -1746,14 +1746,14 @@ var DEFAULT_GATEWAY_KEY_TTL_MS = 15 * 60 * 1e3;
1746
1746
  // src/key-lease/file-secret-store/file-secret-store.ts
1747
1747
  import { randomUUID as randomUUID2 } from "node:crypto";
1748
1748
  import * as fs13 from "node:fs/promises";
1749
- import * as path12 from "node:path";
1749
+ import * as path13 from "node:path";
1750
1750
 
1751
1751
  // src/attribution/clear-attribution/clear-attribution.ts
1752
1752
  import * as fs8 from "node:fs";
1753
1753
 
1754
1754
  // src/attribution/resolve-attribution-file/resolve-attribution-file.ts
1755
1755
  import * as fs7 from "node:fs";
1756
- import * as path8 from "node:path";
1756
+ import * as path9 from "node:path";
1757
1757
 
1758
1758
  // src/attribution/detect-worktree-root/detect-worktree-root.ts
1759
1759
  import * as fs6 from "node:fs";
@@ -1776,18 +1776,34 @@ var detectWorktreeRoot = (startDirectory) => {
1776
1776
  }
1777
1777
  };
1778
1778
 
1779
+ // src/attribution/global-attribution-file/global-attribution-file.ts
1780
+ import * as path8 from "node:path";
1781
+ var globalAttributionFile = () => path8.join(resolveV1SecretStoreDirectory(), "route33.attribution.json");
1782
+
1779
1783
  // src/attribution/resolve-attribution-file/resolve-attribution-file.ts
1784
+ var STATE_FILE = "route33.attribution.json";
1780
1785
  var resolveAttributionFile = (options) => {
1786
+ if (options.scope === "global") return globalAttributionFile();
1781
1787
  const worktreeRoot = detectWorktreeRoot(options.directory);
1782
1788
  if (worktreeRoot !== void 0) {
1783
- return path8.join(worktreeRoot, ".opencode", "route33.attribution.json");
1789
+ return path9.join(worktreeRoot, ".opencode", STATE_FILE);
1790
+ }
1791
+ switch (options.outsideWorktree) {
1792
+ case "use-directory":
1793
+ return path9.join(
1794
+ fs7.realpathSync(options.directory),
1795
+ ".opencode",
1796
+ STATE_FILE
1797
+ );
1798
+ case "use-global":
1799
+ return globalAttributionFile();
1800
+ case "return-undefined":
1801
+ return void 0;
1802
+ default: {
1803
+ const unexpected = options.outsideWorktree;
1804
+ throw new TypeError(`Unexpected policy: ${String(unexpected)}`);
1805
+ }
1784
1806
  }
1785
- const fallbackRoots = {
1786
- "return-undefined": void 0,
1787
- "use-directory": fs7.realpathSync(options.directory)
1788
- };
1789
- const fallbackRoot = fallbackRoots[options.outsideWorktree];
1790
- return fallbackRoot === void 0 ? void 0 : path8.join(fallbackRoot, ".opencode", "route33.attribution.json");
1791
1807
  };
1792
1808
 
1793
1809
  // src/attribution/clear-attribution/clear-attribution.ts
@@ -1800,16 +1816,22 @@ var clearAttribution = (options) => {
1800
1816
  return true;
1801
1817
  };
1802
1818
 
1819
+ // src/attribution/is-global-attribution/is-global-attribution.ts
1820
+ var isGlobalAttribution = (options) => options.scope === "global" || options.outsideWorktree === "use-global" && detectWorktreeRoot(options.directory) === void 0;
1821
+
1822
+ // src/attribution/is-global-choice/is-global-choice.ts
1823
+ var isGlobalChoice = (attribution) => attribution.kind === "personal" && attribution.boundKeyId === void 0;
1824
+
1803
1825
  // src/attribution/read-legacy-key/read-legacy-key.ts
1804
1826
  import * as fs9 from "node:fs";
1805
- import * as path9 from "node:path";
1806
- var LEGACY_KEY_RELATIVE_PATH = path9.join(".opencode", "route33.key");
1827
+ import * as path10 from "node:path";
1828
+ var LEGACY_KEY_RELATIVE_PATH = path10.join(".opencode", "route33.key");
1807
1829
  var readLegacyKey = (worktree) => {
1808
1830
  if (worktree.trim() === "") return void 0;
1809
1831
  let raw;
1810
1832
  try {
1811
1833
  raw = fs9.readFileSync(
1812
- path9.join(worktree, LEGACY_KEY_RELATIVE_PATH),
1834
+ path10.join(worktree, LEGACY_KEY_RELATIVE_PATH),
1813
1835
  "utf8"
1814
1836
  );
1815
1837
  } catch {
@@ -1881,7 +1903,8 @@ var readAttribution = (options) => {
1881
1903
  }
1882
1904
  try {
1883
1905
  const parsed = JSON.parse(fs10.readFileSync(file, "utf8"));
1884
- return parseAttribution(parsed);
1906
+ const attribution = parseAttribution(parsed);
1907
+ return isGlobalAttribution(options) && !isGlobalChoice(attribution) ? void 0 : attribution;
1885
1908
  } catch {
1886
1909
  return void 0;
1887
1910
  }
@@ -1889,11 +1912,11 @@ var readAttribution = (options) => {
1889
1912
 
1890
1913
  // src/attribution/remove-legacy-key/remove-legacy-key.ts
1891
1914
  import * as fs11 from "node:fs";
1892
- import * as path10 from "node:path";
1893
- var LEGACY_KEY_RELATIVE_PATH2 = path10.join(".opencode", "route33.key");
1915
+ import * as path11 from "node:path";
1916
+ var LEGACY_KEY_RELATIVE_PATH2 = path11.join(".opencode", "route33.key");
1894
1917
  var removeLegacyKey = (worktree) => {
1895
1918
  try {
1896
- fs11.rmSync(path10.join(worktree, LEGACY_KEY_RELATIVE_PATH2), { force: true });
1919
+ fs11.rmSync(path11.join(worktree, LEGACY_KEY_RELATIVE_PATH2), { force: true });
1897
1920
  return true;
1898
1921
  } catch {
1899
1922
  return false;
@@ -1903,13 +1926,16 @@ var removeLegacyKey = (worktree) => {
1903
1926
  // src/attribution/write-attribution/write-attribution.ts
1904
1927
  import { randomUUID } from "node:crypto";
1905
1928
  import * as fs12 from "node:fs";
1906
- import * as path11 from "node:path";
1929
+ import * as path12 from "node:path";
1907
1930
  var writeAttribution = (options, attribution) => {
1908
1931
  const file = resolveAttributionFile(options);
1909
1932
  if (file === void 0) {
1910
1933
  return false;
1911
1934
  }
1912
- fs12.mkdirSync(path11.dirname(file), { recursive: true });
1935
+ if (isGlobalAttribution(options) && !isGlobalChoice(attribution)) {
1936
+ return false;
1937
+ }
1938
+ fs12.mkdirSync(path12.dirname(file), { recursive: true });
1913
1939
  const temporaryFile = `${file}.${String(process.pid)}.${randomUUID()}.tmp`;
1914
1940
  try {
1915
1941
  fs12.writeFileSync(temporaryFile, `${JSON.stringify(attribution)}
@@ -2039,7 +2065,7 @@ var FileSecretStore = class {
2039
2065
  throw new TypeError("File secret store scope is not portable");
2040
2066
  }
2041
2067
  this.#directory = options.directory;
2042
- this.#file = path12.join(options.directory, `lease-${options.scopeId}.json`);
2068
+ this.#file = path13.join(options.directory, `lease-${options.scopeId}.json`);
2043
2069
  }
2044
2070
  /** Reads and parses the scoped gateway credential.
2045
2071
  * @returns The persisted credential, when present.
@@ -2092,7 +2118,7 @@ var FileSecretStore = class {
2092
2118
  async stage(key, lease) {
2093
2119
  await fs13.mkdir(this.#directory, { mode: 448, recursive: true });
2094
2120
  await fs13.chmod(this.#directory, 448);
2095
- const temporary = path12.join(this.#directory, `.lease-${randomUUID2()}.tmp`);
2121
+ const temporary = path13.join(this.#directory, `.lease-${randomUUID2()}.tmp`);
2096
2122
  try {
2097
2123
  const record = lease === void 0 ? key.value : { key: key.value, lease };
2098
2124
  await fs13.writeFile(temporary, `${JSON.stringify(record)}
@@ -2894,6 +2920,7 @@ var WorkspaceKeyBinding = class {
2894
2920
  * @returns Secret-free options; local options do not need destructive consent.
2895
2921
  */
2896
2922
  async candidates(choice) {
2923
+ if (detectWorktreeRoot(this.deps.state.directory) === void 0) return [];
2897
2924
  const listing = this.#listings.get(choice.orgId) ?? this.listKeys(choice.orgId);
2898
2925
  this.#listings.set(choice.orgId, listing);
2899
2926
  const rows = await listing;
@@ -2941,6 +2968,8 @@ var WorkspaceKeyBinding = class {
2941
2968
  * @returns The attribution to commit after saving the secret.
2942
2969
  */
2943
2970
  async bind(choice, id, confirmed) {
2971
+ if (detectWorktreeRoot(this.deps.state.directory) === void 0)
2972
+ throw new GatewayCredentialError("Key binding requires a worktree.");
2944
2973
  const row = await this.deps.client.get(this.deps.token, id, choice.orgId);
2945
2974
  if (row.id !== id || !this.matches(row, choice))
2946
2975
  throw new GatewayCredentialError(
@@ -3085,6 +3114,22 @@ var createAttributionPrompt = (projects, options = {}) => {
3085
3114
  var PERSONAL_SELECTION_SUFFIX = ":personal";
3086
3115
  var isPersonalSelection = (selection) => selection === "personal" || selection.endsWith(PERSONAL_SELECTION_SUFFIX);
3087
3116
 
3117
+ // src/ux/attribution-plan/attribution-plan.ts
3118
+ var GLOBAL_ATTRIBUTION_NOTICE = "This folder is not a Git repository, so it cannot have its own budget. Choose the personal key for all such folders on this machine.";
3119
+ var attributionPlan = (input) => {
3120
+ const options = {
3121
+ orgs: input.orgs,
3122
+ personalKeyActive: input.personalKeyActive
3123
+ };
3124
+ return input.global ? {
3125
+ ...createAttributionPrompt([], options),
3126
+ notice: GLOBAL_ATTRIBUTION_NOTICE
3127
+ } : createAttributionPrompt(
3128
+ input.orgs.flatMap(({ projects }) => projects),
3129
+ options
3130
+ );
3131
+ };
3132
+
3088
3133
  // src/ux/attribution-prompt-error/attribution-prompt-error.ts
3089
3134
  var MESSAGES = {
3090
3135
  ATTRIBUTION_BACK: "Left budget selection without changing attribution.",
@@ -3497,10 +3542,11 @@ var Route33Runtime = class {
3497
3542
  }
3498
3543
  /**
3499
3544
  * Reads the stored key for a saved attribution without any login.
3545
+ * @param state - Whose attribution to read; this folder's by default.
3500
3546
  * @returns The reusable key and lease when the stored lease is still valid.
3501
3547
  */
3502
- async storedReusableKey() {
3503
- const attribution = readAttribution(this.#options.state);
3548
+ async storedReusableKey(state = this.#options.state) {
3549
+ const attribution = readAttribution(state);
3504
3550
  if (attribution === void 0) {
3505
3551
  return void 0;
3506
3552
  }
@@ -3549,7 +3595,7 @@ var Route33Runtime = class {
3549
3595
  async resolveWorkspaceCredential(directory) {
3550
3596
  const attribution = readAttribution({
3551
3597
  directory,
3552
- outsideWorktree: "return-undefined"
3598
+ outsideWorktree: this.#options.state.outsideWorktree
3553
3599
  });
3554
3600
  if (attribution === void 0) {
3555
3601
  const installation = this.#installation({ kind: "personal" });
@@ -3667,10 +3713,11 @@ var Route33Runtime = class {
3667
3713
  * @param extras - The wizard's key-creation answers.
3668
3714
  * @param directory - Optional worktree override for project leases.
3669
3715
  * @param saveAttribution - Wizard save that must succeed before replacing a live key.
3716
+ * @param state - Where that save lands; the directory's own state by default.
3670
3717
  * @returns The fresh key material with its lease metadata.
3671
3718
  */
3672
- async mintWorkspaceKey(attribution, extras, directory = this.#options.state.directory, saveAttribution) {
3673
- const file = resolveAttributionFile({ ...this.#options.state, directory });
3719
+ async mintWorkspaceKey(attribution, extras, directory = this.#options.state.directory, saveAttribution, state = { ...this.#options.state, directory }) {
3720
+ const file = resolveAttributionFile(state);
3674
3721
  if (file === void 0)
3675
3722
  throw new TypeError("Workspace attribution requires a worktree");
3676
3723
  const previous = statSync2(file, { bigint: true, throwIfNoEntry: false });
@@ -3713,7 +3760,7 @@ var Route33Runtime = class {
3713
3760
  async workspaceKeyAllowance(directory) {
3714
3761
  const attribution = readAttribution({
3715
3762
  directory,
3716
- outsideWorktree: "return-undefined"
3763
+ outsideWorktree: this.#options.state.outsideWorktree
3717
3764
  });
3718
3765
  if (attribution === void 0) {
3719
3766
  return void 0;
@@ -3899,6 +3946,7 @@ var createRoute33Plugin = (options = {}) => {
3899
3946
  ...options.config
3900
3947
  };
3901
3948
  const runtimeOptions = options.runtime;
3949
+ const outsideWorktree = options.outsideWorktree ?? "return-undefined";
3902
3950
  const installation = runtimeOptions === void 0 ? void 0 : runtimeOptions.installation ?? createLeaseInstallationFactory({
3903
3951
  listScopes: () => FileSecretStore.scopes(
3904
3952
  resolveV1SecretStoreDirectory({
@@ -3917,10 +3965,7 @@ var createRoute33Plugin = (options = {}) => {
3917
3965
  ...runtimeOptions,
3918
3966
  auth: { ...runtimeOptions.auth, clientId: clientId2 },
3919
3967
  installation,
3920
- state: {
3921
- directory: input.worktree,
3922
- outsideWorktree: "return-undefined"
3923
- }
3968
+ state: { directory: input.worktree, outsideWorktree }
3924
3969
  });
3925
3970
  if (runtime2 !== void 0) {
3926
3971
  options.bindRuntime?.(runtime2);
@@ -3958,7 +4003,7 @@ var createRoute33Plugin = (options = {}) => {
3958
4003
  return false;
3959
4004
  }
3960
4005
  },
3961
- outsideWorktree: "return-undefined"
4006
+ outsideWorktree
3962
4007
  });
3963
4008
  }
3964
4009
  return Promise.resolve({
@@ -4895,6 +4940,7 @@ export {
4895
4940
  DEFAULT_GATEWAY_KEY_RENEWAL_RATIO,
4896
4941
  DEFAULT_GATEWAY_KEY_TTL_MS,
4897
4942
  FileSecretStore,
4943
+ GLOBAL_ATTRIBUTION_NOTICE,
4898
4944
  GatewayApiKey,
4899
4945
  HostStorageSecretStore,
4900
4946
  KeyLeaseError,
@@ -4907,6 +4953,7 @@ export {
4907
4953
  Route33Plugin,
4908
4954
  Route33Runtime,
4909
4955
  TRUSTED_PARTY_COPY,
4956
+ attributionPlan,
4910
4957
  changeAttribution,
4911
4958
  clearAttribution,
4912
4959
  contractFixtures,
@@ -4918,7 +4965,10 @@ export {
4918
4965
  detectWorktreeRoot,
4919
4966
  drainPages,
4920
4967
  fixtureToWire,
4968
+ globalAttributionFile,
4921
4969
  inspectSession,
4970
+ isGlobalAttribution,
4971
+ isGlobalChoice,
4922
4972
  isPersonalSelection,
4923
4973
  leaseForAttribution,
4924
4974
  mintKey,