@mcowger/pi-plexus 1.8.1 → 1.10.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/README.md CHANGED
@@ -139,13 +139,18 @@ Inspect the effective URL, authentication availability, and catalog source witho
139
139
  /plexus status
140
140
  ```
141
141
 
142
- Choose the per-session OpenAI service tier:
142
+ Choose the per-session service tier for eligible Plexus models:
143
143
 
144
144
  ```
145
145
  /service-tier fast
146
146
  ```
147
147
 
148
- Tiers are `default` (aliases `off`, `none`), `fast` (alias `priority`), `flex`, and `ultrafast`; `/service-tier status` reports the active tier and whether the current model supports it. Eligibility follows OpenAI's per-tier model support: `priority` covers the Fast-mode set (GPT-5 and newer, GPT-4.1+, GPT-4o, and o3/o4-mini), while `flex` and `ultrafast` are limited to their documented models. The tier is injected as `service_tier` only for eligible Plexus models and resets to `default` on every session start. `/plexus-service-tier` is an alias for the same command.
148
+ Tiers are `default` (aliases `off`, `none`), `fast` (alias `priority`), `flex`, and `ultrafast`; `/service-tier status` reports the active tier and whether the current model supports it. The tier is applied per API:
149
+
150
+ - **OpenAI Responses** models get a `service_tier` field. `priority` covers OpenAI's Fast-mode set (GPT-5 and newer, GPT-4.1+, GPT-4o, and o3/o4-mini); `flex` and `ultrafast` are limited to their documented models.
151
+ - **Anthropic Messages** models use Claude Fast mode (`speed: "fast"` plus the `anthropic-beta: fast-mode-2026-02-01` flag) for `priority`, on `claude-opus-5` and `claude-opus-4-8`; `flex` and `ultrafast` are unsupported. This is a first-party research-preview feature, so it only works when the upstream allows it.
152
+
153
+ The tier resets to `default` on every session start. `/plexus-service-tier` is an alias for the same command.
149
154
 
150
155
  Select a Plexus model in `/model`. Save its startup default with the host's normal model-picker action; Plexus does not maintain a separate default-model setting.
151
156
 
@@ -171,21 +176,21 @@ To force a model refresh:
171
176
  /plexus-refresh
172
177
  ```
173
178
 
174
- `PLEXUS_API_URL` / `PLEXUS_BASE_URL` / `PLEXUS_API_KEY` env vars and plugin options (`plexusBaseURL`, `suppressModels`) are also honored. Until a URL is configured, the provider shows a single `plexus-unconfigured` placeholder. Plugin logs are written to `~/.local/share/opencode/plugins/plexus/plugin.log` (the OpenCode service discards plugin stdout).
179
+ `PLEXUS_API_URL` / `PLEXUS_BASE_URL` / `PLEXUS_API_KEY` env vars and plugin options (`plexusBaseURL`, `apiKey`, `apiKeyEnv`, `suppressModels`) are also honored. Until a URL is configured, the provider shows a single `plexus-unconfigured` placeholder. Plugin logs are written to `~/.local/share/opencode/plugins/plexus/plugin.log` (the OpenCode service discards plugin stdout).
175
180
 
176
181
  ## Configuration files
177
182
 
178
183
  ### Connection state
179
184
 
180
- Each adapter stores only non-secret Plexus settings in its own `config.json`:
185
+ Pi and OMP store only non-secret Plexus settings in their own `config.json`:
181
186
 
182
187
  ```text
183
- <agent-dir>/extensions/plexus/config.json # base URL and optional model suppression
188
+ <agent-dir>/extensions/plexus/config.json # base URL, apiKeyEnv, model suppression
184
189
  ```
185
190
 
186
191
  Credentials stay in the host credential store (`auth.json` for Pi, `agent.db` for OMP). Model catalogs stay in the host model registry/store. The extension no longer writes a second model cache, ETag file, raw API response, or default-model preference.
187
192
 
188
- At runtime, configuration resolves as follows:
193
+ With `apiKeyEnv` omitted, Pi resolves configuration as follows:
189
194
 
190
195
  ```text
191
196
  base URL: PLEXUS_API_URL → PLEXUS_BASE_URL → saved Plexus base URL
@@ -194,10 +199,73 @@ catalog: host model store → live Plexus refresh
194
199
  startup model: host model-picker preference
195
200
  ```
196
201
 
197
- `PLEXUS_API_URL` and `PLEXUS_BASE_URL` are process-only URL overrides; `PLEXUS_API_KEY` is a process-only credential fallback. None are persisted. `/plexus status` reports the effective URL source, whether auth is available, and catalog state without exposing credentials.
202
+ OMP uses the same URL precedence, but its registered `PLEXUS_API_KEY` environment reference is a native config-key override, ahead of saved credentials. If that variable isn't set, OMP uses its host credential resolver. OpenCode uses `PLEXUS_API_KEY` before the saved `/connect` credential and plugin `apiKey` option.
203
+
204
+ `PLEXUS_API_URL` and `PLEXUS_BASE_URL` are process-only URL overrides; environment API keys aren't persisted. `/plexus status` reports the effective URL source, whether auth is available, and catalog state without exposing credentials.
198
205
 
199
206
  Plexus accepts a root URL or a `/v1` API URL. The adapter normalizes that only for the Plexus discovery request. Each model then receives its own API-specific base URL: OpenAI stays on `/v1`, Anthropic uses the root, and Google uses `/v1beta`.
200
207
 
208
+ ### Separate API-key environment variables
209
+
210
+ Set `apiKeyEnv` to the **name** of an environment variable, not its value or a `$VAR` template. The default is `PLEXUS_API_KEY`; no client-specific name is built in.
211
+
212
+ For Pi, edit `~/.pi/agent/extensions/plexus/config.json` (or `<agent-dir>/extensions/plexus/config.json` when using a custom agent directory):
213
+
214
+ ```json
215
+ {
216
+ "baseUrl": "https://plexus.example.com",
217
+ "apiKeyEnv": "AIHOME_PI_API_KEY"
218
+ }
219
+ ```
220
+
221
+ For OMP, edit `~/.omp/agent/extensions/plexus/config.json` (under the active profile's agent directory when using profiles):
222
+
223
+ ```json
224
+ {
225
+ "baseUrl": "https://plexus.example.com",
226
+ "apiKeyEnv": "AIHOME_OMP_API_KEY"
227
+ }
228
+ ```
229
+
230
+ For OpenCode V2, put `apiKeyEnv` in the plugin's `options` in `~/.config/opencode/opencode.jsonc` (or your project `opencode.jsonc`):
231
+
232
+ ```jsonc
233
+ {
234
+ "plugins": [
235
+ {
236
+ "package": "@mcowger/opencode-plexus",
237
+ "options": {
238
+ "plexusBaseURL": "https://plexus.example.com",
239
+ "apiKeyEnv": "AIHOME_OPENCODE_API_KEY"
240
+ }
241
+ }
242
+ ]
243
+ }
244
+ ```
245
+
246
+ Set the named variable in the process environment that launches the client. Restart Pi/OMP after editing their config; restart the OpenCode service after editing plugin options or changing its environment.
247
+
248
+ Credential precedence:
249
+
250
+ | Client | `apiKeyEnv` omitted | `apiKeyEnv` explicitly set |
251
+ |---|---|---|
252
+ | Pi | Saved host credential, then `PLEXUS_API_KEY` fallback | Only the named environment variable, ahead of saved credentials |
253
+ | OMP | Registered `PLEXUS_API_KEY` env override, then host credentials | Only the named environment variable, ahead of saved credentials |
254
+ | OpenCode V2 | `PLEXUS_API_KEY`, then saved `/connect` credential, then plugin `apiKey` option | Only the named environment variable, ahead of both saved credentials and `apiKey` |
255
+
256
+ An explicit setting is authoritative even when it names `PLEXUS_API_KEY`. If the selected variable is missing, empty, or whitespace-only, the plugin reports an error naming the variable without showing its value. It does not fall back to a saved key, OpenCode's `apiKey` option, or the shared `PLEXUS_API_KEY`. Saved credentials remain stored; remove `apiKeyEnv` to restore the default precedence.
257
+
258
+ ### Native login with `apiKeyEnv`
259
+
260
+ `apiKeyEnv` changes which key authenticates requests, not the native login UI:
261
+
262
+ - Pi and OMP: `/login plexus` still prompts for the base URL and API key, and saves both through the normal configuration and credential stores.
263
+ - OpenCode V2: `/connect` still stores the Plexus base URL and API key normally.
264
+
265
+ With an explicit `apiKeyEnv`, discovery and model requests use the named environment variable rather than the key entered during login. OMP's immediate post-login refresh also uses the environment key. Login remains useful for configuring the base URL, but entering a different key won't override the environment setting.
266
+
267
+ If the named variable is missing or empty at startup, the plugin fails to load. Native login cannot repair that setting: set the variable in the launching process environment or remove `apiKeyEnv`, then restart. Removing the setting restores the default precedence above without deleting saved credentials.
268
+
201
269
  ---
202
270
 
203
271
  ## Model suppression
package/dist/extension.js CHANGED
@@ -462,6 +462,47 @@ function getBaseUrlResolution() {
462
462
  const saved = resolveStringOption(config.baseUrl);
463
463
  return { baseUrl: saved ?? null, source: saved ? "saved" : "none" };
464
464
  }
465
+ function resolveApiKeyEnvSetting() {
466
+ const raw = getConfigSync().apiKeyEnv;
467
+ if (raw === undefined || raw === null)
468
+ return { name: ENV_API_KEY, explicit: false };
469
+ const name = typeof raw === "string" ? raw.trim() : "";
470
+ if (!ENV_VAR_NAME_RE.test(name)) {
471
+ throw new Error("Invalid apiKeyEnv: expected an environment variable name");
472
+ }
473
+ return { name, explicit: true };
474
+ }
475
+ function getApiKeyEnvName() {
476
+ return resolveApiKeyEnvSetting().name;
477
+ }
478
+ function isApiKeyEnvExplicit() {
479
+ return resolveApiKeyEnvSetting().explicit;
480
+ }
481
+ function requireApiKeyEnvValue(name, raw) {
482
+ const value = raw?.trim();
483
+ if (!value) {
484
+ throw new Error(`apiKeyEnv environment variable "${name}" is missing or empty`);
485
+ }
486
+ return value;
487
+ }
488
+ async function resolveExplicitApiKeyFromEnv(readEnv) {
489
+ const { name, explicit } = resolveApiKeyEnvSetting();
490
+ if (!explicit)
491
+ return;
492
+ return requireApiKeyEnvValue(name, await readEnv(name));
493
+ }
494
+ function resolveExplicitApiKey() {
495
+ const { name, explicit } = resolveApiKeyEnvSetting();
496
+ if (!explicit)
497
+ return;
498
+ return requireApiKeyEnvValue(name, process.env[name]);
499
+ }
500
+ function resolveApiKey(storedKey) {
501
+ const explicit = resolveExplicitApiKey();
502
+ if (explicit)
503
+ return explicit;
504
+ return storedKey || getEnvApiKey() || undefined;
505
+ }
465
506
  function getEnvApiKey() {
466
507
  return resolveStringOption(process.env[ENV_API_KEY]) ?? null;
467
508
  }
@@ -677,7 +718,6 @@ var ULTRAFAST_FAMILIES = [/^gpt-6(?:\.\d+)?-astra(?=$|-)/i, /^gpt-5\.6-sol(?=$|-
677
718
  var OPENAI_RESPONSES_DIALECT = {
678
719
  id: "openai-responses",
679
720
  apis: OPENAI_RESPONSES_APIS,
680
- parameter: "service_tier",
681
721
  tiers: ["priority", "flex", "ultrafast"],
682
722
  supportsModel: isPriorityEligible,
683
723
  supportsTier(modelId, tier) {
@@ -689,9 +729,34 @@ var OPENAI_RESPONSES_DIALECT = {
689
729
  case "ultrafast":
690
730
  return ULTRAFAST_FAMILIES.some((pattern) => pattern.test(modelId));
691
731
  }
732
+ },
733
+ inject(payload, _modelId, tier) {
734
+ return { ...payload, service_tier: tier };
735
+ }
736
+ };
737
+ var ANTHROPIC_MESSAGES_APIS = ["anthropic-messages"];
738
+ var ANTHROPIC_FAST_MODE_BETA = "fast-mode-2026-02-01";
739
+ var CLAUDE_FAST_FAMILIES = [/^claude-opus-5(?=$|-)/i, /^claude-opus-4-8(?=$|-)/i];
740
+ var ANTHROPIC_MESSAGES_DIALECT = {
741
+ id: "anthropic-messages",
742
+ apis: ANTHROPIC_MESSAGES_APIS,
743
+ tiers: ["priority"],
744
+ supportsModel(modelId) {
745
+ return /^claude-/i.test(modelId.trim());
746
+ },
747
+ supportsTier(modelId, tier) {
748
+ return tier === "priority" && CLAUDE_FAST_FAMILIES.some((pattern) => pattern.test(modelId.trim()));
749
+ },
750
+ inject(payload, _modelId, _tier) {
751
+ const existing = Array.isArray(payload.betas) ? payload.betas.filter((beta) => typeof beta === "string") : [];
752
+ const betas = existing.includes(ANTHROPIC_FAST_MODE_BETA) ? existing : [...existing, ANTHROPIC_FAST_MODE_BETA];
753
+ return { ...payload, speed: "fast", betas };
692
754
  }
693
755
  };
694
- var SERVICE_TIER_DIALECTS = [OPENAI_RESPONSES_DIALECT];
756
+ var SERVICE_TIER_DIALECTS = [
757
+ OPENAI_RESPONSES_DIALECT,
758
+ ANTHROPIC_MESSAGES_DIALECT
759
+ ];
695
760
  function dialectForApi(api, dialects = SERVICE_TIER_DIALECTS) {
696
761
  if (!api)
697
762
  return;
@@ -716,7 +781,7 @@ function applyServiceTier(payload, model, tier, options) {
716
781
  }
717
782
  if (!payload || typeof payload !== "object" || Array.isArray(payload))
718
783
  return payload;
719
- return { ...payload, [dialect.parameter]: tier };
784
+ return dialect.inject(payload, model.id, tier);
720
785
  }
721
786
  var SERVICE_TIER_NOTIFICATION_TYPE = "plexus.serviceTier";
722
787
  var SERVICE_TIER_COMMAND = "service-tier";
@@ -734,7 +799,6 @@ function buildServiceTierNotification(input) {
734
799
 
735
800
  // src/extension.ts
736
801
  var PROVIDER_NAME = "plexus";
737
- var PROVIDER_API_KEY_TEMPLATE = "${PLEXUS_API_KEY}";
738
802
  var PLEXUS_CREDENTIAL_EXPIRES_AT = 253402300799000;
739
803
  var PLACEHOLDER_BASE_URL = "http://localhost/v1";
740
804
  var currentModels = [];
@@ -764,8 +828,54 @@ function enforceMinimumOutputTokens(payload) {
764
828
  }
765
829
  return changed ? next : payload;
766
830
  }
831
+ function withAuthoritativeApiKeyEnv(provider, envName, getHeaderModels = () => currentModels) {
832
+ const apiKey = provider.auth.apiKey;
833
+ if (!apiKey)
834
+ return provider;
835
+ const withModelHeaders = (models) => {
836
+ const headersById = new Map;
837
+ for (const model of getHeaderModels()) {
838
+ if (model.headers)
839
+ headersById.set(model.id, model.headers);
840
+ }
841
+ if (headersById.size === 0)
842
+ return [...models];
843
+ return models.map((model) => {
844
+ const headers = headersById.get(model.id);
845
+ return headers ? { ...model, headers: { ...model.headers, ...headers } } : model;
846
+ });
847
+ };
848
+ return {
849
+ ...provider,
850
+ auth: {
851
+ ...provider.auth,
852
+ apiKey: {
853
+ ...apiKey,
854
+ resolve: async (input) => {
855
+ const explicit = await resolveExplicitApiKeyFromEnv((name) => input.ctx.env(name));
856
+ if (explicit === undefined)
857
+ return apiKey.resolve(input);
858
+ const result = await apiKey.resolve({
859
+ ...input,
860
+ credential: {
861
+ type: "api_key",
862
+ key: explicit,
863
+ ...input.credential?.env ? { env: input.credential.env } : {}
864
+ }
865
+ });
866
+ return result ? { ...result, source: `${envName} (apiKeyEnv)` } : result;
867
+ }
868
+ }
869
+ },
870
+ getModels: () => withModelHeaders(provider.getModels()),
871
+ getAllModels: () => withModelHeaders(provider.getAllModels?.() ?? provider.getModels())
872
+ };
873
+ }
767
874
  function plexusExtension(pi) {
768
- const envApiKey = getEnvApiKey();
875
+ const apiKeyEnvExplicit = isApiKeyEnvExplicit();
876
+ const explicitApiKey = apiKeyEnvExplicit ? resolveExplicitApiKey() : undefined;
877
+ const apiKeyEnvName = getApiKeyEnvName();
878
+ const envApiKey = explicitApiKey ?? getEnvApiKey();
769
879
  const startupBaseUrl = getBaseUrl();
770
880
  const suppressPatterns = getSuppressedModels();
771
881
  const storedCatalog = readStoredModelsSync();
@@ -791,13 +901,27 @@ function plexusExtension(pi) {
791
901
  pi.on("message_end", (event) => normalizeMalformedFunctionCall(event.message, PROVIDER_NAME));
792
902
  pi.registerProvider(PROVIDER_NAME, {
793
903
  api: "openai-completions",
794
- ...envApiKey ? { apiKey: PROVIDER_API_KEY_TEMPLATE } : {},
904
+ ...envApiKey ? { apiKey: `\${${apiKeyEnvName}}` } : {},
795
905
  authHeader: true,
796
906
  baseUrl: startupBaseUrl ?? PLACEHOLDER_BASE_URL,
797
907
  models: startupModels,
798
908
  refreshModels: refreshPlexusModels,
799
909
  oauth: createPlexusLoginProvider()
800
910
  });
911
+ if (apiKeyEnvExplicit) {
912
+ let authOverrideApplied = false;
913
+ pi.on("session_start", (_event, ctx) => {
914
+ if (authOverrideApplied)
915
+ return;
916
+ const composed = ctx.modelRegistry.getProvider(PROVIDER_NAME);
917
+ if (!composed?.auth.apiKey) {
918
+ throw new Error("Plexus apiKeyEnv override failed: provider API-key auth is unavailable");
919
+ }
920
+ pi.registerProvider(withAuthoritativeApiKeyEnv(composed, apiKeyEnvName));
921
+ authOverrideApplied = true;
922
+ log("auth: applied authoritative apiKeyEnv override", { apiKeyEnv: apiKeyEnvName });
923
+ });
924
+ }
801
925
  pi.registerCommand("plexus", {
802
926
  description: "Plexus provider commands: refresh, status (setup: /login plexus)",
803
927
  getArgumentCompletions: (prefix) => {
@@ -861,7 +985,7 @@ function notifyServiceTier(ctx, tier, success) {
861
985
  async function refreshPlexusModels(context) {
862
986
  const baseUrl = getBaseUrl();
863
987
  const modelsUrl = getModelsUrl();
864
- const apiKey = credentialApiKey(context.credential) ?? getEnvApiKey() ?? undefined;
988
+ const apiKey = resolveApiKey(credentialApiKey(context.credential));
865
989
  const suppress = getSuppressedModels();
866
990
  if (!context.allowNetwork || !apiKey || !modelsUrl || !baseUrl) {
867
991
  if (currentModels.length > 0) {
@@ -948,7 +1072,8 @@ function createPlexusLoginProvider() {
948
1072
  return { ...credentials, expires: PLEXUS_CREDENTIAL_EXPIRES_AT };
949
1073
  },
950
1074
  getApiKey(credentials) {
951
- return String(credentials.access || credentials.refresh || "");
1075
+ const stored = String(credentials.access || credentials.refresh || "");
1076
+ return resolveExplicitApiKey() ?? stored;
952
1077
  },
953
1078
  modifyModels(models, credentials) {
954
1079
  const baseUrl = credentials.plexusBaseUrl;
@@ -960,7 +1085,13 @@ function createPlexusLoginProvider() {
960
1085
  };
961
1086
  }
962
1087
  async function handleRefresh(ctx) {
963
- const apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER_NAME);
1088
+ let apiKey;
1089
+ try {
1090
+ apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER_NAME);
1091
+ } catch (error) {
1092
+ ctx.ui.notify(`Plexus API key resolution failed: ${error instanceof Error ? error.message : String(error)}`, "error");
1093
+ return;
1094
+ }
964
1095
  if (!apiKey) {
965
1096
  ctx.ui.notify("No Plexus API key configured. Run /login plexus first.", "error");
966
1097
  return;
@@ -980,10 +1111,14 @@ async function handleRefresh(ctx) {
980
1111
  }
981
1112
  async function handleStatus(ctx) {
982
1113
  const baseUrl = getBaseUrlResolution();
983
- const apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER_NAME);
1114
+ const apiKeyEnvName = getApiKeyEnvName();
1115
+ const apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER_NAME).catch(() => {
1116
+ return;
1117
+ });
1118
+ const explicitApiKey = apiKey ? resolveExplicitApiKey() : undefined;
984
1119
  ctx.ui.notify([
985
1120
  `Plexus base URL: ${baseUrl.baseUrl ?? "not configured"} (${baseUrl.source})`,
986
- `API key: ${apiKey ? getEnvApiKey() ? "host credential or PLEXUS_API_KEY fallback" : "host credential" : "not configured"}`,
1121
+ `API key: ${apiKey ? explicitApiKey !== undefined ? `${apiKeyEnvName} (apiKeyEnv)` : getEnvApiKey() ? `host credential or ${apiKeyEnvName} fallback` : "host credential" : "not configured"}`,
987
1122
  `Catalog: ${currentModels.length} models (${catalogSource})`,
988
1123
  "Default model: managed by Pi. Use /model and save the selection there."
989
1124
  ].join(`
@@ -991,5 +1126,6 @@ async function handleStatus(ctx) {
991
1126
  }
992
1127
  export {
993
1128
  plexusExtension as default,
994
- enforceMinimumOutputTokens
1129
+ enforceMinimumOutputTokens,
1130
+ withAuthoritativeApiKeyEnv
995
1131
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcowger/pi-plexus",
3
- "version": "1.8.1",
3
+ "version": "1.10.0",
4
4
  "description": "Plexus AI proxy plugin for the pi coding agent",
5
5
  "type": "module",
6
6
  "files": [