opencode-cmd-provider 1.3.0 → 1.4.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.4.0 - 2026-08-23
4
+
5
+ Feature: after a successful `/connect`, the credential is mirrored under
6
+ `command-code` in OpenCode's auth store and to `~/.commandcode/auth.json`
7
+ (official CLI layout), so ecosystem consumers such as OpenChamber's Usage
8
+ page find the key without manual setup.
9
+
10
+ ### Features
11
+
12
+ - **Credential mirroring** (issue #64): `/connect` now writes the credential
13
+ under `command-code` in OpenCode's auth store — refreshed on every
14
+ successful re-auth so the mirror stays in sync — and to
15
+ `~/.commandcode/auth.json` in the official CLI layout, which is only
16
+ written when it does not already hold a different credential, so an
17
+ official CLI login is never clobbered. Writes are atomic; new auth files
18
+ get owner-only permissions and existing file modes are preserved.
19
+ - Mirroring is best-effort: failures are swallowed and never fail `/connect`
20
+ itself, and `mirror: false` opts out (used by the oauth tests). Users who
21
+ authenticate via `COMMANDCODE_API_KEY` alone are unaffected.
22
+ - Existing users re-run `/connect` once (or copy the entry manually) to pick
23
+ the mirror up.
24
+
25
+ ### Chores
26
+
27
+ - New `tests/auth-mirror.test.ts` suite: preserves unrelated auth-store
28
+ entries, owner-only permissions on create, stale-entry refresh on re-auth,
29
+ no-clobber of a differing CLI credential, idempotent no-op, blank-key
30
+ guard, and the end-to-end `/connect` flow including `mirror: false` —
31
+ wired into `test:unit`.
32
+ - Docs: README notes the mirror locations and that mirroring is best-effort.
33
+
3
34
  ## 1.3.0 - 2026-08-23
4
35
 
5
36
  Feature: dual-transport Provider API — non-Go plans now use the documented
package/README.md CHANGED
@@ -49,6 +49,8 @@ Select **Command Code**, complete the browser flow, and pick a model with `/mode
49
49
 
50
50
  Run `/connect` in OpenCode and select **Command Code**. The browser flow stores the returned credential in OpenCode's auth store.
51
51
 
52
+ The credential is also mirrored under `command-code` in OpenCode's auth store and to `~/.commandcode/auth.json` (official CLI layout, only written when that file does not already hold a different credential). Ecosystem consumers such as OpenChamber's Usage page read those locations. Mirroring is best-effort; if it fails, `/connect` still succeeds.
53
+
52
54
  If automatic transfer from the browser fails, copy the API key shown by Command Code and export it as `COMMANDCODE_API_KEY` (see below).
53
55
 
54
56
  ### Environment variable
@@ -0,0 +1,11 @@
1
+ export interface MirrorOptions {
2
+ opencodeAuthFile?: string;
3
+ legacyAuthFile?: string;
4
+ }
5
+ export interface MirrorResult {
6
+ opencodeAuthUpdated: boolean;
7
+ legacyAuthUpdated: boolean;
8
+ }
9
+ export declare function defaultOpencodeAuthFile(): string;
10
+ export declare function defaultLegacyAuthFile(): string;
11
+ export declare function mirrorCredential(key: string, options?: MirrorOptions): MirrorResult;
@@ -0,0 +1,74 @@
1
+ // src/plugin/auth-mirror.ts — mirror /connect credentials for ecosystem consumers
2
+ //
3
+ // OpenChamber's command-code quota provider reads the API key from OpenCode's
4
+ // auth store under the key "command-code" (the provider id its own archived
5
+ // plugin used). OpenCode stores this plugin's credential under "commandcode",
6
+ // so after a successful /connect we mirror it under "command-code" as well.
7
+ // We also mirror to ~/.commandcode/auth.json in the official CLI layout,
8
+ // which resolveApiKey already reads as a legacy fallback.
9
+ import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
10
+ import { homedir } from "node:os";
11
+ import { basename, join } from "node:path";
12
+ function dataHome() {
13
+ return process.env.XDG_DATA_HOME ?? join(homedir(), ".local", "share");
14
+ }
15
+ export function defaultOpencodeAuthFile() {
16
+ return join(dataHome(), "opencode", "auth.json");
17
+ }
18
+ export function defaultLegacyAuthFile() {
19
+ return join(homedir(), ".commandcode", "auth.json");
20
+ }
21
+ function readJsonObject(path) {
22
+ try {
23
+ const parsed = JSON.parse(readFileSync(path, "utf-8"));
24
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
25
+ return parsed;
26
+ }
27
+ }
28
+ catch {
29
+ // Missing or malformed file: start from an empty document.
30
+ }
31
+ return {};
32
+ }
33
+ function writeJsonAtomic(path, value) {
34
+ const dir = join(path, "..");
35
+ mkdirSync(dir, { recursive: true });
36
+ const existingMode = existsSync(path) ? statSync(path).mode & 0o777 : 0o600;
37
+ const tmp = join(dir, `.${basename(path)}.${process.pid}.tmp`);
38
+ writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, { mode: existingMode });
39
+ renameSync(tmp, path);
40
+ }
41
+ function isCredentialRecord(value) {
42
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
43
+ }
44
+ // OpenCode's auth store is owned by this plugin's ecosystem entry point, so a
45
+ // stale mirror there is refreshed on every successful /connect. The legacy
46
+ // file belongs to the official Command Code CLI; if it already holds a
47
+ // different credential we leave it alone rather than clobber another tool's
48
+ // login.
49
+ export function mirrorCredential(key, options = {}) {
50
+ const trimmed = key.trim();
51
+ if (!trimmed)
52
+ return { opencodeAuthUpdated: false, legacyAuthUpdated: false };
53
+ const entry = { type: "api", key: trimmed };
54
+ const opencodeAuthFile = options.opencodeAuthFile ?? defaultOpencodeAuthFile();
55
+ const opencodeDoc = readJsonObject(opencodeAuthFile);
56
+ const currentOpencode = opencodeDoc["command-code"];
57
+ let opencodeAuthUpdated = false;
58
+ if (!isCredentialRecord(currentOpencode) ||
59
+ currentOpencode.type !== "api" ||
60
+ currentOpencode.key !== trimmed) {
61
+ opencodeDoc["command-code"] = entry;
62
+ writeJsonAtomic(opencodeAuthFile, opencodeDoc);
63
+ opencodeAuthUpdated = true;
64
+ }
65
+ const legacyAuthFile = options.legacyAuthFile ?? defaultLegacyAuthFile();
66
+ const legacyDoc = readJsonObject(legacyAuthFile);
67
+ let legacyAuthUpdated = false;
68
+ if (!isCredentialRecord(legacyDoc["command-code"])) {
69
+ legacyDoc["command-code"] = entry;
70
+ writeJsonAtomic(legacyAuthFile, legacyDoc);
71
+ legacyAuthUpdated = true;
72
+ }
73
+ return { opencodeAuthUpdated, legacyAuthUpdated };
74
+ }
@@ -1,6 +1,9 @@
1
+ import { type MirrorOptions } from "./auth-mirror.js";
1
2
  import type { AuthOAuthResult } from "@opencode-ai/plugin";
2
3
  export interface RunAuthFlowOptions {
3
4
  startPort?: number;
4
5
  timeoutMs?: number;
6
+ /** Credential mirroring targets; pass `false` to disable mirroring. */
7
+ mirror?: MirrorOptions | false;
5
8
  }
6
9
  export declare function runAuthFlow(options?: RunAuthFlowOptions): Promise<AuthOAuthResult>;
@@ -3,8 +3,12 @@
3
3
  // Wraps the local callback server in the opencode AuthOAuthResult shape:
4
4
  // the studio URL is opened in the browser, the studio POSTs the API key to
5
5
  // the local /callback endpoint, and callback() resolves with the key.
6
+ // On success the credential is also mirrored under "command-code" (see
7
+ // auth-mirror.ts) so ecosystem consumers such as OpenChamber's quota
8
+ // provider can find it.
6
9
  import { randomBytes } from "node:crypto";
7
10
  import { startAuthServer } from "./auth-server.js";
11
+ import { mirrorCredential } from "./auth-mirror.js";
8
12
  const STUDIO_BASE_URL = "https://commandcode.ai";
9
13
  const DEFAULT_AUTH_TIMEOUT_MS = 15_000;
10
14
  function generateStateToken() {
@@ -27,6 +31,14 @@ export async function runAuthFlow(options = {}) {
27
31
  authServer.server.close();
28
32
  if (callback.state !== stateToken)
29
33
  return { type: "failed" };
34
+ if (options.mirror !== false) {
35
+ try {
36
+ mirrorCredential(callback.apiKey, options.mirror ?? {});
37
+ }
38
+ catch {
39
+ // Mirroring is best-effort; it must never fail the login itself.
40
+ }
41
+ }
30
42
  return { type: "success", key: callback.apiKey };
31
43
  }
32
44
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-cmd-provider",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Command Code provider + plugin for opencode",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,7 +22,7 @@
22
22
  "refresh:deals": "node scripts/refresh-deals.mjs",
23
23
  "refresh": "npm run refresh:snapshot && npm run refresh:deals -- --fixtures",
24
24
  "test": "npm run typecheck && npm run test:unit && npm run test:integration && npm run test:contract && npm run format:check",
25
- "test:unit": "tsx tests/env.test.ts && tsx tests/auth-key.test.ts && tsx tests/converters.test.ts && tsx tests/stream.test.ts && tsx tests/provider-codecs.test.ts && tsx tests/provider-transport.test.ts && tsx tests/provider-parity.test.ts && tsx tests/provider-upgrade-fallback.test.ts && tsx tests/provider-zdr.test.ts && tsx tests/redact.test.ts && tsx tests/cost.test.ts && tsx tests/retry.test.ts && tsx tests/reasoning.test.ts && tsx tests/modalities.test.ts && tsx tests/snapshot.test.ts && tsx tests/refresh-snapshot.test.ts && tsx tests/oauth.test.ts && tsx tests/parse-facts.test.ts && tsx tests/parse-modalities.test.ts && tsx tests/catalog-metadata.test.ts && tsx tests/plugin-models.test.ts && tsx tests/vendor.test.ts && tsx tests/deals-enrichment.test.ts && tsx tests/deals-coverage.test.ts && tsx tests/plan-summary.test.ts && tsx tests/html-tables.test.ts && tsx tests/parse-docs.test.ts && tsx tests/refresh-deals.test.ts && tsx tests/tui-deals-panel.test.ts && tsx tests/plugin-install.test.ts",
25
+ "test:unit": "tsx tests/env.test.ts && tsx tests/auth-key.test.ts && tsx tests/converters.test.ts && tsx tests/stream.test.ts && tsx tests/provider-codecs.test.ts && tsx tests/provider-transport.test.ts && tsx tests/provider-parity.test.ts && tsx tests/provider-upgrade-fallback.test.ts && tsx tests/provider-zdr.test.ts && tsx tests/redact.test.ts && tsx tests/cost.test.ts && tsx tests/retry.test.ts && tsx tests/reasoning.test.ts && tsx tests/modalities.test.ts && tsx tests/snapshot.test.ts && tsx tests/refresh-snapshot.test.ts && tsx tests/oauth.test.ts && tsx tests/auth-mirror.test.ts && tsx tests/parse-facts.test.ts && tsx tests/parse-modalities.test.ts && tsx tests/catalog-metadata.test.ts && tsx tests/plugin-models.test.ts && tsx tests/vendor.test.ts && tsx tests/deals-enrichment.test.ts && tsx tests/deals-coverage.test.ts && tsx tests/plan-summary.test.ts && tsx tests/html-tables.test.ts && tsx tests/parse-docs.test.ts && tsx tests/refresh-deals.test.ts && tsx tests/tui-deals-panel.test.ts && tsx tests/plugin-install.test.ts",
26
26
  "test:integration": "tsx tests/integration-do-stream.test.ts && tsx tests/integration-do-generate.test.ts",
27
27
  "test:contract": "tsx tests/contract.test.ts",
28
28
  "test:e2e": "node tests/e2e-opencode.mjs",