@brainervirus/opencode-commandcode 0.11.1 → 0.12.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/CHANGELOG.md CHANGED
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.12.1] - 2026-10-19
11
+
12
+ ## [0.12.0] - 2026-10-19
13
+
10
14
  ## [0.11.1] - 2026-10-18
11
15
 
12
16
  ## [0.11.0] - 2026-10-17
package/README.md CHANGED
@@ -28,6 +28,7 @@ This package is based on **[FanFan4204/opencode-commandcode-provider](https://gi
28
28
  - The provider's `supported_endpoints` metadata chooses each model's API route. Messages is preferred when advertised; otherwise Chat Completions is preferred, and Responses is used only when it is the sole advertised route. This avoids the malformed Responses function-call start event observed on DeepSeek V4.1 Flash `#max` in OpenCode V2.0.18: `response.output_item.added` contained a `function_call` item without its required `arguments` string. That exact tool-call flow now passes through Chat Completions on V1 and V2. Responses mappings remain available for response-only models, but the current catalog has none. Claude Sonnet 4.6 returned `MODEL_NOT_IN_PLAN` with the message “available in Pro and above plans or extra on-demand usage”; entitled Claude access has not been live-verified. If a model fails on an account entitled to use it, please [open an issue](https://github.com/BrainerVirus/opencode-commandcode/issues) with the model ID and OpenCode version, or submit a PR with a reproducible fix.
29
29
  - Quiet OpenCode startup (diagnostics go to `startup.json`, not stdout).
30
30
  - Free models show `(Free)` after their display name on V1 and V2 when all catalog prices are zero. Model IDs and routing stay unchanged; this is a name suffix, not a native desktop badge. Names that already end in `Free` or `(Free)` keep their existing label.
31
+ - Zero data retention on request: `CMD_ZDR=1` or `zdr: true` in the plugin config adds `x-cmd-zdr: 1` to every Command Code Provider API (`/provider/v1/*`) request on both OpenCode generations.
31
32
 
32
33
  ## How it works
33
34
 
@@ -81,13 +82,45 @@ Catalog patches ship in new npm releases. OpenCode loads the version in its pack
81
82
 
82
83
  ## Plugin config file
83
84
 
84
- Optional, `~/.config/opencode/opencode-commandcode.json` (legacy fallback name `commandcode-go-opencode-provider.json` still loads). Unknown keys are ignored; every field has a default.
85
+ Optional, `~/.config/opencode/opencode-commandcode.json` (legacy fallback name `commandcode-go-opencode-provider.json` still loads). Unknown keys are ignored; every field has a default, and an invalid field falls back on its own without discarding the valid ones (an invalid `zdr` value keeps ZDR on).
85
86
 
86
87
  | Key | Default | Effect |
87
88
  |---|---|---|
88
89
  | `commandCodePackagePath` | `""` | Maintainer override: extract the catalog from a local `command-code` checkout instead of the bundle. Same as env `COMMANDCODE_PACKAGE_PATH`. |
89
90
  | `debugStartupLogs` | `false` | Also mirror the startup summary to stderr. Default is quiet (`startup.json` only). |
90
91
  | `disableModelSync` | `false` | Accepted for forward compatibility; currently has no effect. |
92
+ | `zdr` | `false` | Send `x-cmd-zdr: 1` on every Provider API request (zero data retention). Same as env `CMD_ZDR=1`. A present but invalid value (e.g. the string `"true"`) counts as on. |
93
+
94
+ ## Zero data retention (ZDR)
95
+
96
+ Command Code enforces [zero data retention](https://commandcode.ai/docs/provider#zero-data-retention-zdr) with the
97
+ `x-cmd-zdr: 1` request header; the CLI exposes the same opt-in as `CMD_ZDR=1`. This plugin never invoked the CLI, so
98
+ set either:
99
+
100
+ - env `CMD_ZDR=1` (accepts `1`, `true`, `yes`, `on`; case-insensitive), or
101
+ - `"zdr": true` in `~/.config/opencode/opencode-commandcode.json`.
102
+
103
+ The plugin then adds the header to every Command Code Provider API (`/provider/v1/*`) request: V2 registers it on the
104
+ `commandcode` provider, V1 on the provider `options.headers`. A ZDR header you configure yourself
105
+ (`providers.commandcode.headers` on V2, `provider.commandcode.options.headers` on V1) wins over the plugin's value.
106
+ Header names are matched case-insensitively, and the plugin collapses any spelling (`X-Cmd-Zdr`) into a single lowercase
107
+ `x-cmd-zdr` key, so a request never carries two ZDR headers. Custom npm transports keep owning their options.
108
+
109
+ Behavior to expect from the API: when set, requests route only through ZDR-capable upstreams, and a model without one
110
+ **fails with `422 cmd_zdr_no_providers`** instead of falling back to a non-ZDR provider. ZDR can change which upstream
111
+ serves a request, so it may cost more. The header is sent on the Chat Completions, Messages, and Responses routes alike;
112
+ live verification so far covers Chat Completions only.
113
+
114
+ The programmatic SDK factory (`createCommandCode`) calls `/alpha/generate`, which the ZDR documentation does not cover,
115
+ so it fails closed: with ZDR requested (`CMD_ZDR` on, or an `x-cmd-zdr: 1` header in any casing),
116
+ `languageModel()` throws before any request is sent. An explicit `x-cmd-zdr: 0` header opts the SDK out of `CMD_ZDR`.
117
+
118
+ Live verification against the Provider API lives in `tests/integration/zdr.integration.test.ts` and is skipped unless
119
+ `COMMANDCODE_API_KEY` is set:
120
+
121
+ ```bash
122
+ COMMANDCODE_API_KEY=... bun run test:integration
123
+ ```
91
124
 
92
125
  ## Optional local CLI override
93
126
 
package/dist/plugin.js CHANGED
@@ -19488,9 +19488,10 @@ var AvailabilityPayloadSchema = exports_external.object({
19488
19488
  }
19489
19489
  });
19490
19490
  var PluginFileConfigSchema = exports_external.object({
19491
- disableModelSync: exports_external.boolean().optional().default(false),
19492
- commandCodePackagePath: exports_external.string().optional().default(""),
19493
- debugStartupLogs: exports_external.boolean().optional().default(false)
19491
+ disableModelSync: exports_external.boolean().optional().default(false).catch(false),
19492
+ commandCodePackagePath: exports_external.string().optional().default("").catch(""),
19493
+ debugStartupLogs: exports_external.boolean().optional().default(false).catch(false),
19494
+ zdr: exports_external.boolean().optional().default(false).catch(true)
19494
19495
  });
19495
19496
 
19496
19497
  // src/env.ts
@@ -20321,6 +20322,37 @@ var catalogPaths = () => {
20321
20322
  };
20322
20323
  };
20323
20324
 
20325
+ // src/zdr.ts
20326
+ var ZDR_HEADER = "x-cmd-zdr";
20327
+ var ZDR_HEADER_VALUE = "1";
20328
+ var ZDR_TRUTHY = new Set(["1", "true", "yes", "on"]);
20329
+ function zdrFromEnvValue(value) {
20330
+ return typeof value === "string" && ZDR_TRUTHY.has(value.trim().toLowerCase());
20331
+ }
20332
+ function zdrEnabled(config2, deps = liveEnv) {
20333
+ return config2.zdr === true || zdrFromEnvValue(deps.getEnv("CMD_ZDR"));
20334
+ }
20335
+ function asHeaderMap(value) {
20336
+ return typeof value === "object" && value !== null ? value : undefined;
20337
+ }
20338
+ function findZdrHeader(headers) {
20339
+ const current = asHeaderMap(headers);
20340
+ if (!current)
20341
+ return;
20342
+ if (ZDR_HEADER in current)
20343
+ return String(current[ZDR_HEADER]);
20344
+ const key = Object.keys(current).find((name) => name.toLowerCase() === ZDR_HEADER);
20345
+ return key === undefined ? undefined : String(current[key]);
20346
+ }
20347
+ function withZdrHeaders(existing, enabled) {
20348
+ const current = asHeaderMap(existing);
20349
+ if (!enabled)
20350
+ return current;
20351
+ const value = findZdrHeader(current) ?? ZDR_HEADER_VALUE;
20352
+ const rest = Object.fromEntries(Object.entries(current ?? {}).filter(([name]) => name.toLowerCase() !== ZDR_HEADER));
20353
+ return { [ZDR_HEADER]: value, ...rest };
20354
+ }
20355
+
20324
20356
  // plugin.ts
20325
20357
  var { models: MODELS_PATH, manifest: MANIFEST_PATH, version: VERSION_PATH } = catalogPaths();
20326
20358
  var V1_PROVIDER_SDK_NPM = "@ai-sdk/openai-compatible";
@@ -20494,6 +20526,7 @@ async function server() {
20494
20526
  return;
20495
20527
  const pluginCfg = loadPluginConfig();
20496
20528
  const debug = pluginCfg.debugStartupLogs === true;
20529
+ const zdr = zdrEnabled(pluginCfg);
20497
20530
  if (!cc.npm)
20498
20531
  cc.npm = V1_PROVIDER_SDK_NPM;
20499
20532
  if (!cc.name)
@@ -20507,6 +20540,8 @@ async function server() {
20507
20540
  const options = cc.options;
20508
20541
  if (!options.baseURL)
20509
20542
  options.baseURL = PROVIDER_API_BASE_URL;
20543
+ if (zdr)
20544
+ options.headers = withZdrHeaders(options.headers, true);
20510
20545
  }
20511
20546
  }
20512
20547
  if (cc.models)
@@ -20550,6 +20585,7 @@ async function server() {
20550
20585
  async function setup(ctx) {
20551
20586
  const pluginCfg = loadPluginConfig();
20552
20587
  const debug = pluginCfg.debugStartupLogs === true;
20588
+ const zdr = zdrEnabled(pluginCfg);
20553
20589
  const load = loadCatalogEntries();
20554
20590
  persistCatalogSideEffects(load, debug);
20555
20591
  const v2models = generateV2Models(load.models);
@@ -20564,6 +20600,7 @@ async function setup(ctx) {
20564
20600
  integrationID: "commandcode",
20565
20601
  env: ["COMMANDCODE_API_KEY"],
20566
20602
  package: V2_PROVIDER_PACKAGE,
20603
+ ...zdr ? { headers: withZdrHeaders(undefined, true) } : {},
20567
20604
  settings: {
20568
20605
  baseURL: PROVIDER_API_BASE_URL
20569
20606
  }
@@ -20577,6 +20614,8 @@ async function setup(ctx) {
20577
20614
  provider.activation = provider.activation ?? "auto";
20578
20615
  provider.integrationID = provider.integrationID ?? "commandcode";
20579
20616
  provider.env = provider.env ?? ["COMMANDCODE_API_KEY"];
20617
+ if (zdr)
20618
+ provider.headers = withZdrHeaders(provider.headers, true);
20580
20619
  provider.settings = provider.settings ?? {};
20581
20620
  if (typeof provider.settings === "object") {
20582
20621
  if (!provider.settings.baseURL)
@@ -21102,17 +21141,24 @@ class CommandCodeLanguageModel {
21102
21141
  }
21103
21142
 
21104
21143
  // index.ts
21144
+ function zdrRequested(headers) {
21145
+ return zdrFromEnvValue(findZdrHeader(headers) ?? process.env.CMD_ZDR);
21146
+ }
21105
21147
  function createCommandCode(options = {}) {
21106
21148
  const apiKey = resolveApiKey({ apiKey: options.apiKey, authPaths: options.authPaths });
21107
21149
  if (!apiKey) {
21108
21150
  throw new Error("Command Code API key not found. Set COMMANDCODE_API_KEY env var, create ~/.commandcode/auth.json, or pass apiKey option.");
21109
21151
  }
21152
+ const headers = typeof options.headers === "object" && options.headers !== null ? options.headers : undefined;
21110
21153
  return {
21111
21154
  languageModel(modelId) {
21155
+ if (zdrRequested(headers)) {
21156
+ throw new Error("Zero data retention is only supported on the Command Code Provider API (/provider/v1/*); createCommandCode uses /alpha/generate. Unset CMD_ZDR, or use the OpenCode provider plugin.");
21157
+ }
21112
21158
  return new CommandCodeLanguageModel(modelId, {
21113
21159
  apiKey,
21114
21160
  baseURL: typeof options.baseURL === "string" ? options.baseURL : undefined,
21115
- headers: typeof options.headers === "object" && options.headers !== null ? options.headers : undefined
21161
+ headers
21116
21162
  });
21117
21163
  }
21118
21164
  };
@@ -114,10 +114,13 @@ export declare const AvailabilityPayloadSchema: z.ZodObject<{
114
114
  /**
115
115
  * Plugin file config (`~/.config/opencode/opencode-commandcode.json`).
116
116
  * Deliberately lenient: user config files may carry extra keys, so unknown
117
- * keys are stripped and every field falls back to its default.
117
+ * keys are stripped, and an invalid field falls back on its own without
118
+ * discarding the valid ones. `zdr` fails safe instead: a present but invalid
119
+ * value (e.g. the string "true") keeps ZDR on rather than silently disabling it.
118
120
  */
119
121
  export declare const PluginFileConfigSchema: z.ZodObject<{
120
- disableModelSync: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
121
- commandCodePackagePath: z.ZodDefault<z.ZodOptional<z.ZodString>>;
122
- debugStartupLogs: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
122
+ disableModelSync: z.ZodCatch<z.ZodDefault<z.ZodOptional<z.ZodBoolean>>>;
123
+ commandCodePackagePath: z.ZodCatch<z.ZodDefault<z.ZodOptional<z.ZodString>>>;
124
+ debugStartupLogs: z.ZodCatch<z.ZodDefault<z.ZodOptional<z.ZodBoolean>>>;
125
+ zdr: z.ZodCatch<z.ZodDefault<z.ZodOptional<z.ZodBoolean>>>;
123
126
  }, z.core.$strip>;
@@ -0,0 +1,29 @@
1
+ import { type EnvDeps } from "./env.js";
2
+ /**
3
+ * Command Code zero-data-retention opt-in. The Provider API enforces ZDR with
4
+ * the `x-cmd-zdr: 1` request header (the CLI exposes the same opt-in as
5
+ * `CMD_ZDR=1`); ZDR-incapable models fail with `422 cmd_zdr_no_providers`
6
+ * instead of falling back to a non-ZDR upstream.
7
+ */
8
+ export declare const ZDR_HEADER = "x-cmd-zdr";
9
+ export declare const ZDR_HEADER_VALUE = "1";
10
+ /** Parse the `CMD_ZDR` environment value. */
11
+ export declare function zdrFromEnvValue(value: string | undefined): boolean;
12
+ /** ZDR is on when the plugin config opts in or `CMD_ZDR` is truthy. */
13
+ export declare function zdrEnabled(config: {
14
+ zdr?: boolean;
15
+ }, deps?: EnvDeps): boolean;
16
+ /** ZDR header map, or undefined when off so callers never create an empty `headers` key. */
17
+ export declare function zdrHeaders(enabled: boolean): Record<string, string> | undefined;
18
+ /**
19
+ * The caller's ZDR header value, matched case-insensitively (HTTP header names
20
+ * are). The lowercase spelling wins when several spellings are present.
21
+ */
22
+ export declare function findZdrHeader(headers: unknown): string | undefined;
23
+ /**
24
+ * Add the ZDR header without discarding headers already configured. An explicit
25
+ * user value in any casing (`X-Cmd-Zdr: 0`) wins over the plugin's opt-in, and
26
+ * every spelling collapses into the single lowercase key so a request never
27
+ * carries two ZDR headers (`1, 0`).
28
+ */
29
+ export declare function withZdrHeaders(existing: unknown, enabled: boolean): Record<string, string> | undefined;
package/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { resolveApiKey } from "./src/auth.js";
2
2
  import { CommandCodeLanguageModel } from "./src/model.js";
3
+ import { findZdrHeader, zdrFromEnvValue } from "./src/zdr.js";
3
4
 
4
5
  // Re-export the plugin definition so a directory plugin entry (<dir>/index.ts)
5
6
  // loads the dual V1+V2 plugin. No cycle: plugin.ts only imports from src/.
@@ -13,6 +14,15 @@ export interface CommandCodeProviderOptions {
13
14
  authPaths?: string[];
14
15
  }
15
16
 
17
+ /**
18
+ * ZDR is documented for the Provider API (`/provider/v1/*`) only, and this SDK
19
+ * calls `/alpha/generate`. A caller ZDR header (any casing) decides, so an
20
+ * explicit `x-cmd-zdr: 0` opts out of `CMD_ZDR`; otherwise `CMD_ZDR` decides.
21
+ */
22
+ function zdrRequested(headers: Record<string, string> | undefined): boolean {
23
+ return zdrFromEnvValue(findZdrHeader(headers) ?? process.env.CMD_ZDR);
24
+ }
25
+
16
26
  export function createCommandCode(options: CommandCodeProviderOptions = {}) {
17
27
  const apiKey = resolveApiKey({ apiKey: options.apiKey, authPaths: options.authPaths });
18
28
  if (!apiKey) {
@@ -21,15 +31,26 @@ export function createCommandCode(options: CommandCodeProviderOptions = {}) {
21
31
  );
22
32
  }
23
33
 
34
+ const headers =
35
+ typeof options.headers === "object" && options.headers !== null
36
+ ? (options.headers as Record<string, string>)
37
+ : undefined;
38
+
24
39
  return {
25
40
  languageModel(modelId: string): CommandCodeLanguageModel {
41
+ // Fail closed before any request: sending without enforced ZDR would leak the
42
+ // prompt the caller asked to keep out of retention. Every caller (direct import,
43
+ // or OpenCode's `create*` lookup on a provider `npm` set to this package)
44
+ // resolves a model here before it can send one.
45
+ if (zdrRequested(headers)) {
46
+ throw new Error(
47
+ "Zero data retention is only supported on the Command Code Provider API (/provider/v1/*); createCommandCode uses /alpha/generate. Unset CMD_ZDR, or use the OpenCode provider plugin.",
48
+ );
49
+ }
26
50
  return new CommandCodeLanguageModel(modelId, {
27
51
  apiKey,
28
52
  baseURL: typeof options.baseURL === "string" ? options.baseURL : undefined,
29
- headers:
30
- typeof options.headers === "object" && options.headers !== null
31
- ? (options.headers as Record<string, string>)
32
- : undefined,
53
+ headers,
33
54
  });
34
55
  },
35
56
  };
package/manifest.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
- "generatedAt": "2026-10-18T06:01:15.885Z",
4
- "pluginVersion": "0.11.1",
3
+ "generatedAt": "2026-10-19T18:01:42.245Z",
4
+ "pluginVersion": "0.12.1",
5
5
  "commandCodeVersion": "1.84.0",
6
6
  "commandCodeTarball": "https://registry.npmjs.org/command-code/-/command-code-1.84.0.tgz",
7
7
  "modelCount": 92,
@@ -13,14 +13,17 @@
13
13
  },
14
14
  "costSources": {
15
15
  "cli": 0,
16
- "officialDocs": 92,
17
- "thirdParty": 0,
16
+ "officialDocs": 90,
17
+ "thirdParty": 2,
18
18
  "free": 0,
19
19
  "fallback": 0,
20
20
  "unmatched": 0
21
21
  },
22
22
  "review": {
23
- "thirdParty": [],
23
+ "thirdParty": [
24
+ "deepseek/deepseek-v4.1-flash",
25
+ "deepseek/deepseek-v4.1-flash-fast"
26
+ ],
24
27
  "free": [],
25
28
  "unmatched": []
26
29
  },
package/models.json CHANGED
@@ -1119,9 +1119,9 @@
1119
1119
  ],
1120
1120
  "tool_call": true,
1121
1121
  "cost": {
1122
- "input": 0.15,
1123
- "output": 0.6,
1124
- "cache_read": 0.003
1122
+ "input": 0.39,
1123
+ "output": 1.56,
1124
+ "cache_read": 0.0078
1125
1125
  },
1126
1126
  "limit": {
1127
1127
  "context": 1000000,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brainervirus/opencode-commandcode",
3
- "version": "0.11.1",
3
+ "version": "0.12.1",
4
4
  "description": "Command Code API provider for opencode — use Claude, GPT, Gemini, DeepSeek, Qwen, Kimi, GLM, MiniMax, and Step models via Command Code",
5
5
  "keywords": [
6
6
  "ai",
package/plugin.ts CHANGED
@@ -13,6 +13,7 @@ import type { CatalogManifest } from "./src/manifest.js";
13
13
  import { generateV2Models } from "./src/v2models.js";
14
14
  import { catalogPaths } from "./src/paths.js";
15
15
  import { ManifestSchema, ModelEntrySchema, PluginFileConfigSchema } from "./src/schemas.js";
16
+ import { withZdrHeaders, zdrEnabled } from "./src/zdr.js";
16
17
 
17
18
  const { models: MODELS_PATH, manifest: MANIFEST_PATH, version: VERSION_PATH } = catalogPaths();
18
19
 
@@ -238,6 +239,7 @@ export async function server() {
238
239
 
239
240
  const pluginCfg = loadPluginConfig();
240
241
  const debug = pluginCfg.debugStartupLogs === true;
242
+ const zdr = zdrEnabled(pluginCfg);
241
243
 
242
244
  if (!cc.npm) cc.npm = V1_PROVIDER_SDK_NPM;
243
245
  if (!cc.name) cc.name = "Command Code";
@@ -245,11 +247,14 @@ export async function server() {
245
247
  // V1 resolves `npm` through BunProc.install and calls the first `create*` export, so
246
248
  // the SDK package must be the OpenAI-compatible one; give it the Provider API URL
247
249
  // unless the user already configured another baseURL (custom npm stays untouched).
250
+ // V1 merges `options` into every model of the provider, whichever `provider.npm` the
251
+ // model overrides, so one headers map reaches Chat, Messages, and Responses routes.
248
252
  if (cc.npm === V1_PROVIDER_SDK_NPM) {
249
253
  if (cc.options === undefined) cc.options = {};
250
254
  if (typeof cc.options === "object" && cc.options !== null) {
251
255
  const options = cc.options as Record<string, unknown>;
252
256
  if (!options.baseURL) options.baseURL = PROVIDER_API_BASE_URL;
257
+ if (zdr) options.headers = withZdrHeaders(options.headers, true);
253
258
  }
254
259
  }
255
260
 
@@ -293,6 +298,7 @@ export async function server() {
293
298
  async function setup(ctx: any): Promise<void> {
294
299
  const pluginCfg = loadPluginConfig();
295
300
  const debug = pluginCfg.debugStartupLogs === true;
301
+ const zdr = zdrEnabled(pluginCfg);
296
302
  const load = loadCatalogEntries();
297
303
  persistCatalogSideEffects(load, debug);
298
304
  const v2models = generateV2Models(load.models);
@@ -308,6 +314,9 @@ async function setup(ctx: any): Promise<void> {
308
314
  integrationID: "commandcode",
309
315
  env: ["COMMANDCODE_API_KEY"],
310
316
  package: V2_PROVIDER_PACKAGE,
317
+ // Provider headers reach every model of the provider, including the
318
+ // Messages/Responses package a model may select instead of the default.
319
+ ...(zdr ? { headers: withZdrHeaders(undefined, true) } : {}),
311
320
  settings: {
312
321
  baseURL: PROVIDER_API_BASE_URL,
313
322
  },
@@ -321,6 +330,7 @@ async function setup(ctx: any): Promise<void> {
321
330
  provider.activation = provider.activation ?? "auto";
322
331
  provider.integrationID = provider.integrationID ?? "commandcode";
323
332
  provider.env = provider.env ?? ["COMMANDCODE_API_KEY"];
333
+ if (zdr) provider.headers = withZdrHeaders(provider.headers, true);
324
334
  provider.settings = provider.settings ?? {};
325
335
  if (typeof provider.settings === "object") {
326
336
  if (!provider.settings.baseURL) provider.settings.baseURL = PROVIDER_API_BASE_URL;
package/src/schemas.ts CHANGED
@@ -126,10 +126,13 @@ export const AvailabilityPayloadSchema = z
126
126
  /**
127
127
  * Plugin file config (`~/.config/opencode/opencode-commandcode.json`).
128
128
  * Deliberately lenient: user config files may carry extra keys, so unknown
129
- * keys are stripped and every field falls back to its default.
129
+ * keys are stripped, and an invalid field falls back on its own without
130
+ * discarding the valid ones. `zdr` fails safe instead: a present but invalid
131
+ * value (e.g. the string "true") keeps ZDR on rather than silently disabling it.
130
132
  */
131
133
  export const PluginFileConfigSchema = z.object({
132
- disableModelSync: z.boolean().optional().default(false),
133
- commandCodePackagePath: z.string().optional().default(""),
134
- debugStartupLogs: z.boolean().optional().default(false),
134
+ disableModelSync: z.boolean().optional().default(false).catch(false),
135
+ commandCodePackagePath: z.string().optional().default("").catch(""),
136
+ debugStartupLogs: z.boolean().optional().default(false).catch(false),
137
+ zdr: z.boolean().optional().default(false).catch(true),
135
138
  });
package/src/zdr.ts ADDED
@@ -0,0 +1,65 @@
1
+ import { liveEnv, type EnvDeps } from "./env.js";
2
+
3
+ /**
4
+ * Command Code zero-data-retention opt-in. The Provider API enforces ZDR with
5
+ * the `x-cmd-zdr: 1` request header (the CLI exposes the same opt-in as
6
+ * `CMD_ZDR=1`); ZDR-incapable models fail with `422 cmd_zdr_no_providers`
7
+ * instead of falling back to a non-ZDR upstream.
8
+ */
9
+ export const ZDR_HEADER = "x-cmd-zdr";
10
+ export const ZDR_HEADER_VALUE = "1";
11
+
12
+ /** Values that turn `CMD_ZDR` on; everything else (including "0") leaves ZDR off. */
13
+ const ZDR_TRUTHY = new Set(["1", "true", "yes", "on"]);
14
+
15
+ /** Parse the `CMD_ZDR` environment value. */
16
+ export function zdrFromEnvValue(value: string | undefined): boolean {
17
+ return typeof value === "string" && ZDR_TRUTHY.has(value.trim().toLowerCase());
18
+ }
19
+
20
+ /** ZDR is on when the plugin config opts in or `CMD_ZDR` is truthy. */
21
+ export function zdrEnabled(config: { zdr?: boolean }, deps: EnvDeps = liveEnv): boolean {
22
+ return config.zdr === true || zdrFromEnvValue(deps.getEnv("CMD_ZDR"));
23
+ }
24
+
25
+ /** ZDR header map, or undefined when off so callers never create an empty `headers` key. */
26
+ export function zdrHeaders(enabled: boolean): Record<string, string> | undefined {
27
+ return enabled ? { [ZDR_HEADER]: ZDR_HEADER_VALUE } : undefined;
28
+ }
29
+
30
+ function asHeaderMap(value: unknown): Record<string, string> | undefined {
31
+ return typeof value === "object" && value !== null
32
+ ? (value as Record<string, string>)
33
+ : undefined;
34
+ }
35
+
36
+ /**
37
+ * The caller's ZDR header value, matched case-insensitively (HTTP header names
38
+ * are). The lowercase spelling wins when several spellings are present.
39
+ */
40
+ export function findZdrHeader(headers: unknown): string | undefined {
41
+ const current = asHeaderMap(headers);
42
+ if (!current) return undefined;
43
+ if (ZDR_HEADER in current) return String(current[ZDR_HEADER]);
44
+ const key = Object.keys(current).find((name) => name.toLowerCase() === ZDR_HEADER);
45
+ return key === undefined ? undefined : String(current[key]);
46
+ }
47
+
48
+ /**
49
+ * Add the ZDR header without discarding headers already configured. An explicit
50
+ * user value in any casing (`X-Cmd-Zdr: 0`) wins over the plugin's opt-in, and
51
+ * every spelling collapses into the single lowercase key so a request never
52
+ * carries two ZDR headers (`1, 0`).
53
+ */
54
+ export function withZdrHeaders(
55
+ existing: unknown,
56
+ enabled: boolean,
57
+ ): Record<string, string> | undefined {
58
+ const current = asHeaderMap(existing);
59
+ if (!enabled) return current;
60
+ const value = findZdrHeader(current) ?? ZDR_HEADER_VALUE;
61
+ const rest = Object.fromEntries(
62
+ Object.entries(current ?? {}).filter(([name]) => name.toLowerCase() !== ZDR_HEADER),
63
+ );
64
+ return { [ZDR_HEADER]: value, ...rest };
65
+ }