pi-cliproxyapi-provider 0.16.1 → 0.16.3

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.
@@ -7,7 +7,6 @@ import { ProviderRuntime } from "../src/runtime.ts";
7
7
  import { buildProviderRegistration } from "../src/registration.ts";
8
8
  import { buildUnavailableProviderModels } from "../src/provider.ts";
9
9
  import { registerCliproxyapiCommand } from "../src/commands.ts";
10
- import { getDiscoveryApiKey } from "../src/auth.ts";
11
10
  import { loadProviderSettings } from "../src/settings.ts";
12
11
  import { registerCodexCompatiblePayloadAdapter } from "../src/codex-compat.ts";
13
12
  import { registerGatewayTelemetry } from "../src/gateway-telemetry.ts";
@@ -28,7 +27,6 @@ export default async function (pi: ExtensionAPI) {
28
27
  config,
29
28
  gpt56ContextWindow: settings.gpt56ContextWindow,
30
29
  bundledModelsDevPath,
31
- getApiKey: () => getDiscoveryApiKey(config.providerName),
32
30
  });
33
31
  const nextRuntime = new ProviderRuntime({ pi, config, catalog: nextCatalog });
34
32
  await nextRuntime.start();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cliproxyapi-provider",
3
- "version": "0.16.1",
3
+ "version": "0.16.3",
4
4
  "description": "Pi provider package for CLIProxyAPI with automatic model discovery and models.dev enrichment.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/catalog.ts CHANGED
@@ -36,7 +36,6 @@ export interface ProviderCatalogOptions {
36
36
  config: CpaProviderConfig;
37
37
  gpt56ContextWindow: Gpt56ContextWindowMode;
38
38
  bundledModelsDevPath: string;
39
- getApiKey: () => Promise<string | undefined>;
40
39
  backgroundTimeoutMs?: number;
41
40
  manualTimeoutMs?: number;
42
41
  writeSnapshot?: typeof writeCache;
@@ -158,7 +157,11 @@ export class ProviderCatalog {
158
157
 
159
158
  if (models.attempted) {
160
159
  try {
161
- const apiKey = await (getDiscoveryApiKey ?? this.options.getApiKey)();
160
+ const requiresKey = this.options.config.authRequired && this.options.config.authHeader;
161
+ const apiKey = requiresKey ? await getDiscoveryApiKey?.() : undefined;
162
+ if (requiresKey && !apiKey) {
163
+ throw new Error("CPA discovery requires a resolved credential");
164
+ }
162
165
  const fresh = await fetchCpaModels(
163
166
  this.options.config.baseUrl,
164
167
  discoveryHeaders(this.options.config, apiKey),
package/src/config.ts CHANGED
@@ -201,6 +201,56 @@ function parseModelOverrides(value: unknown, scope: string): ProviderModelOverri
201
201
  return parsed;
202
202
  }
203
203
 
204
+ const ALLOWED_API_KEY_COMMANDS = new Map<string, readonly string[]>([
205
+ ["bw", ["get"]],
206
+ ["op", ["read"]],
207
+ ["cat", []],
208
+ ["secret-tool", ["lookup"]],
209
+ ["gpg", ["--decrypt", "-d"]],
210
+ ["pass", ["show"]],
211
+ ["security", ["find-generic-password", "find-internet-password"]],
212
+ ]);
213
+
214
+ const API_KEY_COMMAND_USAGE = "bw get, op read, cat, secret-tool lookup, gpg --decrypt, pass show, security find-generic-password";
215
+
216
+ function validateApiKeyCommand(value: string, path: string): void {
217
+ const command = value.slice(1);
218
+ const tokens: string[] = [];
219
+ let token = "";
220
+ let quote: "'" | '"' | undefined;
221
+ for (const char of command) {
222
+ if (char === "\n" || char === "\r") {
223
+ throw new Error(`apiKey command must be one of: ${API_KEY_COMMAND_USAGE} in config file: ${path}`);
224
+ }
225
+ if (quote) {
226
+ if (char === quote) quote = undefined;
227
+ else token += char;
228
+ continue;
229
+ }
230
+ if (char === "'" || char === '"') {
231
+ quote = char;
232
+ continue;
233
+ }
234
+ if (/\s/u.test(char)) {
235
+ if (token) {
236
+ tokens.push(token);
237
+ token = "";
238
+ }
239
+ continue;
240
+ }
241
+ if (";&|<>$()`\\".includes(char)) {
242
+ throw new Error(`apiKey command must be one of: ${API_KEY_COMMAND_USAGE} without shell operators in config file: ${path}`);
243
+ }
244
+ token += char;
245
+ }
246
+ if (quote) throw new Error(`apiKey command has an unterminated quote in config file: ${path}`);
247
+ if (token) tokens.push(token);
248
+ const allowedSubcommands = ALLOWED_API_KEY_COMMANDS.get(tokens[0] ?? "");
249
+ if (!allowedSubcommands || (allowedSubcommands.length > 0 && !allowedSubcommands.includes(tokens[1] ?? ""))) {
250
+ throw new Error(`apiKey command must be one of: ${API_KEY_COMMAND_USAGE} in config file: ${path}`);
251
+ }
252
+ }
253
+
204
254
  function validateConfigLayer(value: unknown, path: string): ConfigLayer {
205
255
  if (!value || typeof value !== "object" || Array.isArray(value)) {
206
256
  throw new Error(`Config file must contain a JSON object: ${path}`);
@@ -221,6 +271,13 @@ function validateConfigLayer(value: unknown, path: string): ConfigLayer {
221
271
  throw new Error(`metadataFallbackProvider must be a non-empty string or null in config file: ${path}`);
222
272
  }
223
273
 
274
+ if (record.apiKey !== undefined && (typeof record.apiKey !== "string" || !record.apiKey.trim())) {
275
+ throw new Error(`apiKey must be a non-empty string in config file: ${path}`);
276
+ }
277
+ if (typeof record.apiKey === "string" && record.apiKey.startsWith("!")) {
278
+ validateApiKeyCommand(record.apiKey, path);
279
+ }
280
+
224
281
  const booleanFields = ["authRequired", "authHeader", "modelsDevEnabled"];
225
282
  for (const field of booleanFields) {
226
283
  if (record[field] !== undefined && typeof record[field] !== "boolean") {
@@ -64,7 +64,7 @@ export function buildProviderRegistration(
64
64
  name: `CLIProxyAPI (${config.providerName})`,
65
65
  baseUrl: config.baseUrl,
66
66
  api: "openai-completions",
67
- apiKey: config.authRequired ? "$CLIPROXYAPI_API_KEY" : "cliproxyapi-no-auth",
67
+ apiKey: config.authRequired ? config.apiKey ?? "$CLIPROXYAPI_API_KEY" : "cliproxyapi-no-auth",
68
68
  authHeader: config.authRequired && config.authHeader,
69
69
  headers: Object.keys(config.headers).length > 0 ? config.headers : undefined,
70
70
  models: normalizeProviderModels(models, config.baseUrl),
package/src/runtime.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { RefreshModelsContext } from "@earendil-works/pi-ai";
2
2
  import type { ExtensionAPI, ProviderModelConfig } from "@earendil-works/pi-coding-agent";
3
- import { getDiscoveryApiKey } from "./auth.ts";
4
3
  import { ProviderCatalog, type CatalogRefreshResult, type CatalogSnapshot, type RefreshTarget } from "./catalog.ts";
5
4
  import { buildUnavailableProviderModels } from "./provider.ts";
6
5
  import { buildProviderRegistration, normalizeProviderModels } from "./registration.ts";
@@ -48,8 +47,9 @@ export class ProviderRuntime {
48
47
  const credential = context.credential;
49
48
  const keyFn: () => Promise<string | undefined> = credential?.type === "api_key"
50
49
  ? async () => credential.key
51
- : () => getDiscoveryApiKey(this.options.config.providerName);
50
+ : async () => undefined;
52
51
  const result = await this.options.catalog.refresh("models", mode, keyFn, context.signal);
52
+ if (result.models.error) throw result.models.error;
53
53
  // Pi publishes refreshModels' return value synchronously. Registering here as
54
54
  // well would create a second, competing catalog publication.
55
55
  return normalizeProviderModels(
package/src/types.ts CHANGED
@@ -21,6 +21,7 @@ export interface CpaProviderConfig {
21
21
  providerName: string;
22
22
  baseUrl: string;
23
23
  authRequired: boolean;
24
+ apiKey?: string;
24
25
  authHeader: boolean;
25
26
  headers: Record<string, string>;
26
27
  modelsDevEnabled: boolean;
package/src/auth.ts DELETED
@@ -1,11 +0,0 @@
1
- import { readStoredCredential } from "@earendil-works/pi-coding-agent";
2
-
3
- export async function getDiscoveryApiKey(providerName: string, env: NodeJS.ProcessEnv = process.env): Promise<string | undefined> {
4
- try {
5
- const credential = await readStoredCredential(providerName);
6
- if (credential?.type === "api_key") return credential.key;
7
- return env.CLIPROXYAPI_API_KEY;
8
- } catch {
9
- return env.CLIPROXYAPI_API_KEY;
10
- }
11
- }