pi-nvidia-nim 1.1.21 → 1.1.22

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.
Files changed (2) hide show
  1. package/index.ts +43 -5
  2. package/package.json +3 -2
package/index.ts CHANGED
@@ -498,10 +498,29 @@ function getNimApiKey(): string | undefined {
498
498
  return apiKey?.trim() || undefined;
499
499
  }
500
500
 
501
+ function getNimProviderApiKeyConfig(): string {
502
+ return `$${getNimApiKeyEnv() ?? NVIDIA_NIM_API_KEY_ENV}`;
503
+ }
504
+
501
505
  function isNimApiKeyEnvName(value: string): value is NimApiKeyEnvName {
502
506
  return NVIDIA_API_KEY_ENV_NAMES.includes(value as NimApiKeyEnvName);
503
507
  }
504
508
 
509
+ function isNimApiKeyEnvReference(value: string): boolean {
510
+ return value.startsWith("$") && isNimApiKeyEnvName(value.slice(1));
511
+ }
512
+
513
+ function isNimApiKeyEnvPlaceholder(value: string): boolean {
514
+ return isNimApiKeyEnvName(value) || isNimApiKeyEnvReference(value);
515
+ }
516
+
517
+ function resolveNimApiKeyEnvReference(value: string): string | undefined {
518
+ if (!isNimApiKeyEnvReference(value)) return undefined;
519
+
520
+ const envValue = process.env[value.slice(1)]?.trim();
521
+ return envValue || undefined;
522
+ }
523
+
505
524
  function isNimApiKeyEnvValue(value: string): boolean {
506
525
  return NVIDIA_API_KEY_ENV_NAMES.some((envName) => process.env[envName]?.trim() === value);
507
526
  }
@@ -570,15 +589,19 @@ function resolveNimApiKey(apiKey: string | undefined, authStorage?: AuthStorageL
570
589
 
571
590
  if (
572
591
  hasStoredCommandCredential &&
573
- (!resolvedApiKey || isNimApiKeyEnvName(resolvedApiKey) || isNimApiKeyEnvValue(resolvedApiKey))
592
+ (!resolvedApiKey || isNimApiKeyEnvPlaceholder(resolvedApiKey) || isNimApiKeyEnvValue(resolvedApiKey))
574
593
  ) {
575
594
  throw new Error("NVIDIA NIM API key command resolved to an empty value.");
576
595
  }
577
596
 
578
597
  const storedApiKey = getStoredResolvedNimApiKey(authStorage);
579
- if (storedApiKey && (!resolvedApiKey || isNimApiKeyEnvName(resolvedApiKey))) return storedApiKey;
598
+ if (storedApiKey && (!resolvedApiKey || isNimApiKeyEnvPlaceholder(resolvedApiKey))) return storedApiKey;
580
599
 
581
- if (resolvedApiKey && !isNimApiKeyEnvName(resolvedApiKey)) return resolvedApiKey;
600
+ if (resolvedApiKey) {
601
+ const envReferenceApiKey = resolveNimApiKeyEnvReference(resolvedApiKey);
602
+ if (envReferenceApiKey) return envReferenceApiKey;
603
+ if (!isNimApiKeyEnvPlaceholder(resolvedApiKey)) return resolvedApiKey;
604
+ }
582
605
 
583
606
  return getNimApiKey();
584
607
  }
@@ -615,6 +638,20 @@ function buildThinkingKwargs(
615
638
  return kwargs;
616
639
  }
617
640
 
641
+ function buildNimRequestHeaders(headers: SimpleStreamOptions["headers"], apiKey: string): Record<string, string> {
642
+ const resolvedHeaders: Record<string, string> = {};
643
+
644
+ for (const [key, value] of Object.entries(headers ?? {})) {
645
+ if (key.toLowerCase() === "authorization") continue;
646
+ resolvedHeaders[key] = value;
647
+ }
648
+
649
+ return {
650
+ ...resolvedHeaders,
651
+ Authorization: `Bearer ${apiKey}`,
652
+ };
653
+ }
654
+
618
655
  function nimStreamSimple(
619
656
  model: Model<Api>,
620
657
  context: Context,
@@ -654,6 +691,7 @@ function nimStreamSimple(
654
691
  ...options,
655
692
  reasoning: effectiveReasoning,
656
693
  apiKey: nimApiKey,
694
+ headers: buildNimRequestHeaders(options?.headers, nimApiKey),
657
695
  onPayload: (params: unknown) => {
658
696
  const p = params as Record<string, unknown>;
659
697
 
@@ -834,7 +872,7 @@ async function fetchNimModels(apiKey: string): Promise<NimModelFetchResult> {
834
872
  // =============================================================================
835
873
 
836
874
  export default function (pi: ExtensionAPI) {
837
- const providerApiKeyConfig = getNimApiKeyEnv() ?? NVIDIA_NIM_API_KEY_ENV;
875
+ const providerApiKeyConfig = getNimProviderApiKeyConfig();
838
876
 
839
877
  // Always register the curated model list. The request path resolves credentials
840
878
  // through pi first (CLI override, auth.json, shell command), then falls back to
@@ -897,7 +935,7 @@ export default function (pi: ExtensionAPI) {
897
935
  const allModels = Array.from(modelMap.values());
898
936
  ctx.modelRegistry.registerProvider(PROVIDER_NAME, {
899
937
  baseUrl: NVIDIA_NIM_BASE_URL,
900
- apiKey: getNimApiKeyEnv() ?? NVIDIA_NIM_API_KEY_ENV,
938
+ apiKey: getNimProviderApiKeyConfig(),
901
939
  api: "openai-completions",
902
940
  authHeader: true,
903
941
  models: allModels,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-nvidia-nim",
3
- "version": "1.1.21",
3
+ "version": "1.1.22",
4
4
  "description": "NVIDIA NIM API provider extension for pi coding agent — access 100+ models from build.nvidia.com",
5
5
  "type": "module",
6
6
  "files": [
@@ -25,7 +25,8 @@
25
25
  "devDependencies": {
26
26
  "@earendil-works/pi-ai": "^0.74.0",
27
27
  "@earendil-works/pi-coding-agent": "^0.74.0",
28
- "@types/node": "^22.0.0"
28
+ "@types/node": "^22.0.0",
29
+ "typescript": "^6.0.3"
29
30
  },
30
31
  "repository": {
31
32
  "type": "git",