@bastani/pi-ai 0.9.58 → 0.9.59

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.
@@ -1,14 +1,15 @@
1
1
  import type { Provider } from "../models.ts";
2
2
  /**
3
- * GitHub Copilot host routing for `COPILOT_GITHUB_TOKEN` (PAT) auth.
3
+ * GitHub Copilot host routing for raw-token (PAT) auth, whether the token comes
4
+ * from `COPILOT_GITHUB_TOKEN` or from an API key saved through `/login`.
4
5
  *
5
6
  * Upstream pi pins `github-copilot` to `https://api.individual.githubcopilot.com`
6
7
  * and only derives a per-tenant host inside the OAuth loader, which returns an
7
8
  * `auth.baseUrl` from the token's `proxy-ep` segment. `envApiKeyAuth` resolves
8
- * `COPILOT_GITHUB_TOKEN` without a base URL, so business/enterprise tokens are
9
- * sent to the individual CAPI host and GitHub answers `421 Misdirected Request`.
9
+ * raw tokens without a base URL, so business/enterprise tokens are sent to the
10
+ * individual CAPI host and GitHub answers `421 Misdirected Request`.
10
11
  *
11
- * This module restores the routing precedence for the env-token path only; the
12
+ * This module restores the routing precedence for the raw-token path only; the
12
13
  * OAuth path stays exactly upstream. A `models.json` provider `baseUrl` still
13
14
  * wins, because it is applied as a later overlay in `applyModelsJson`.
14
15
  */
@@ -26,11 +27,15 @@ export type CopilotRoutingEnv = Readonly<Record<string, string | undefined>>;
26
27
  */
27
28
  export declare function resolveCopilotEnvBaseUrl(env: CopilotRoutingEnv, enterpriseDomain?: string): string | undefined;
28
29
  /**
29
- * Rewrite the builtin Copilot provider's host when `COPILOT_GITHUB_TOKEN` is set.
30
+ * Rewrite the builtin Copilot provider's host when a raw Copilot token is configured.
31
+ *
32
+ * `getStoredApiKey` returns the API key saved through `/login`, if any. It wins
33
+ * over `COPILOT_GITHUB_TOKEN`, matching credential resolution, and is read on
34
+ * every access so a login or logout reroutes without rebuilding the provider.
30
35
  *
31
36
  * Applied to the builtin layer so a `models.json` `baseUrl` override, which is
32
- * composed afterwards, still takes precedence. Providers without the env token
33
- * are returned untouched so OAuth logins keep pi's exact behavior.
37
+ * composed afterwards, still takes precedence. Without a raw token the provider
38
+ * keeps pi's exact behavior, so OAuth logins are unaffected.
34
39
  */
35
- export declare function withCopilotEnvBaseUrl(provider: Provider, env: CopilotRoutingEnv): Provider;
40
+ export declare function withCopilotEnvBaseUrl(provider: Provider, env: CopilotRoutingEnv, getStoredApiKey?: () => string | undefined): Provider;
36
41
  //# sourceMappingURL=github-copilot-env.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"github-copilot-env.d.ts","sourceRoot":"","sources":["../../src/providers/github-copilot-env.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAG7C;;;;;;;;;;;;GAYG;AAEH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAyC7E;;;;;;;;;;GAUG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,iBAAiB,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAoB9G;AAMD;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,QAAQ,EAAE,GAAG,EAAE,iBAAiB,GAAG,QAAQ,CAU1F"}
1
+ {"version":3,"file":"github-copilot-env.d.ts","sourceRoot":"","sources":["../../src/providers/github-copilot-env.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;GAaG;AAEH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAyC7E;;;;;;;;;;GAUG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,iBAAiB,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAoB9G;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CACpC,QAAQ,EAAE,QAAQ,EAClB,GAAG,EAAE,iBAAiB,EACtB,eAAe,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,GACxC,QAAQ,CAoBV"}
@@ -72,23 +72,37 @@ function withModelBaseUrl(models, baseUrl) {
72
72
  return models.map((model) => ({ ...model, baseUrl }));
73
73
  }
74
74
  /**
75
- * Rewrite the builtin Copilot provider's host when `COPILOT_GITHUB_TOKEN` is set.
75
+ * Rewrite the builtin Copilot provider's host when a raw Copilot token is configured.
76
+ *
77
+ * `getStoredApiKey` returns the API key saved through `/login`, if any. It wins
78
+ * over `COPILOT_GITHUB_TOKEN`, matching credential resolution, and is read on
79
+ * every access so a login or logout reroutes without rebuilding the provider.
76
80
  *
77
81
  * Applied to the builtin layer so a `models.json` `baseUrl` override, which is
78
- * composed afterwards, still takes precedence. Providers without the env token
79
- * are returned untouched so OAuth logins keep pi's exact behavior.
82
+ * composed afterwards, still takes precedence. Without a raw token the provider
83
+ * keeps pi's exact behavior, so OAuth logins are unaffected.
80
84
  */
81
- export function withCopilotEnvBaseUrl(provider, env) {
82
- if (provider.id !== COPILOT_PROVIDER_ID || !env.COPILOT_GITHUB_TOKEN?.trim())
85
+ export function withCopilotEnvBaseUrl(provider, env, getStoredApiKey) {
86
+ if (provider.id !== COPILOT_PROVIDER_ID)
83
87
  return provider;
84
- const baseUrl = resolveCopilotEnvBaseUrl(env);
85
- if (!baseUrl || baseUrl === provider.baseUrl)
88
+ if (!getStoredApiKey && !env.COPILOT_GITHUB_TOKEN?.trim())
86
89
  return provider;
90
+ const routedBaseUrl = () => {
91
+ const token = getStoredApiKey?.()?.trim() || env.COPILOT_GITHUB_TOKEN?.trim();
92
+ return token ? resolveCopilotEnvBaseUrl({ ...env, COPILOT_GITHUB_TOKEN: token }) : undefined;
93
+ };
94
+ const routed = (models) => {
95
+ const baseUrl = routedBaseUrl();
96
+ return baseUrl ? withModelBaseUrl(models, baseUrl) : models;
97
+ };
98
+ const { getAllModels } = provider;
87
99
  return {
88
100
  ...provider,
89
- baseUrl,
90
- // Wrap the getter so dynamically refreshed models are routed too.
91
- getModels: () => withModelBaseUrl(provider.getModels(), baseUrl),
101
+ get baseUrl() {
102
+ return routedBaseUrl() ?? provider.baseUrl;
103
+ },
104
+ getModels: () => routed(provider.getModels()),
105
+ ...(getAllModels ? { getAllModels: () => routed(getAllModels.call(provider)) } : {}),
92
106
  };
93
107
  }
94
108
  //# sourceMappingURL=github-copilot-env.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"github-copilot-env.js","sourceRoot":"","sources":["../../src/providers/github-copilot-env.ts"],"names":[],"mappings":"AAmBA,MAAM,mBAAmB,GAAG,gBAAgB,CAAC;AAC7C,qFAAqF;AACrF,MAAM,kBAAkB,GAAG,+BAA+B,CAAC;AAC3D,MAAM,uBAAuB,GAAG,0CAA0C,CAAC;AAE3E,SAAS,gBAAgB,CAAC,KAAa;IACtC,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAClD,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC;IACxB,OAAO,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,OAAO,EAAE,CAAC;AACvE,CAAC;AAED,mFAAmF;AACnF,SAAS,gBAAgB,CAAC,KAAa;IACtC,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;IAC/C,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAAE,OAAO,SAAS,CAAC;IAClC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IAC1D,OAAO,IAAI,CAAC,CAA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O,CAAC;KAChE,CAAC;AACH,CAAC","sourcesContent":["import type { Provider } from \"../models.ts\";\nimport type { Api, Model } from \"../types.ts\";\n\n/**\n * GitHub Copilot host routing for `COPILOT_GITHUB_TOKEN` (PAT) auth.\n *\n * Upstream pi pins `github-copilot` to `https://api.individual.githubcopilot.com`\n * and only derives a per-tenant host inside the OAuth loader, which returns an\n * `auth.baseUrl` from the token's `proxy-ep` segment. `envApiKeyAuth` resolves\n * `COPILOT_GITHUB_TOKEN` without a base URL, so business/enterprise tokens are\n * sent to the individual CAPI host and GitHub answers `421 Misdirected Request`.\n *\n * This module restores the routing precedence for the env-token path only; the\n * OAuth path stays exactly upstream. A `models.json` provider `baseUrl` still\n * wins, because it is applied as a later overlay in `applyModelsJson`.\n */\n\nexport type CopilotRoutingEnv = Readonly<Record<string, string | undefined>>;\n\nconst COPILOT_PROVIDER_ID = \"github-copilot\";\n/** Public routing hub: resolves the caller's plan-specific CAPI host server-side. */\nconst PUBLIC_COPILOT_HUB = \"https://api.githubcopilot.com\";\nconst ENTERPRISE_COPILOT_HOST = \"https://api.enterprise.githubcopilot.com\";\n\nfunction normalizeBaseUrl(value: string): string {\n\tconst trimmed = value.trim().replace(/\\/+$/u, \"\");\n\tif (!trimmed) return \"\";\n\treturn /^https?:\\/\\//u.test(trimmed) ? trimmed : `https://${trimmed}`;\n}\n\n/** `tid=...;exp=...;proxy-ep=proxy.business.githubcopilot.com;...` -> API host. */\nfunction baseUrlFromToken(token: string): string | undefined {\n\tconst match = token.match(/proxy-ep=([^;]+)/u);\n\tif (!match?.[1]) return undefined;\n\tconst host = match[1].trim().replace(/^proxy\\./u, \"api.\");\n\treturn host ? normalizeBaseUrl(host) : undefined;\n}\n\nfunction hostFromServerUrl(serverUrl: string): string | undefined {\n\ttry {\n\t\treturn new URL(normalizeBaseUrl(serverUrl)).hostname.toLowerCase() || undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction baseUrlFromServerHost(host: string): string | undefined {\n\tif (host === \"github.com\" || host.endsWith(\".github.com\")) return undefined;\n\t// Tenant-scoped GHE.com: octocorp.ghe.com -> copilot-api.octocorp.ghe.com\n\tif (host.endsWith(\".ghe.com\")) {\n\t\tconst tenant = host.slice(0, -\".ghe.com\".length).split(\".\").pop();\n\t\treturn tenant ? `https://copilot-api.${tenant}.ghe.com` : ENTERPRISE_COPILOT_HOST;\n\t}\n\t// GitHub Enterprise Server on a self-hosted domain routes through the\n\t// enterprise CAPI host rather than a per-appliance Copilot endpoint.\n\treturn ENTERPRISE_COPILOT_HOST;\n}\n\n/**\n * Resolve the Copilot API base URL for env-token auth, highest precedence first:\n *\n * 1. `COPILOT_API_TARGET` / `GITHUB_COPILOT_BASE_URL` explicit override\n * 2. the `proxy-ep` segment embedded in `COPILOT_GITHUB_TOKEN`\n * 3. an explicit `enterpriseDomain`\n * 4. `GITHUB_SERVER_URL` (`*.ghe.com` tenant host, else the enterprise host)\n * 5. the public Copilot routing hub when `COPILOT_GITHUB_TOKEN` is set\n *\n * Returns `undefined` when nothing applies, leaving pi's individual-host default.\n */\nexport function resolveCopilotEnvBaseUrl(env: CopilotRoutingEnv, enterpriseDomain?: string): string | undefined {\n\tconst override = env.COPILOT_API_TARGET?.trim() || env.GITHUB_COPILOT_BASE_URL?.trim();\n\tif (override) return normalizeBaseUrl(override);\n\n\tconst token = env.COPILOT_GITHUB_TOKEN?.trim();\n\tif (token) {\n\t\tconst fromToken = baseUrlFromToken(token);\n\t\tif (fromToken) return fromToken;\n\t}\n\n\tif (enterpriseDomain?.trim()) return `https://copilot-api.${enterpriseDomain.trim()}`;\n\n\tconst serverUrl = env.GITHUB_SERVER_URL?.trim();\n\tif (serverUrl) {\n\t\tconst host = hostFromServerUrl(serverUrl);\n\t\tconst fromServer = host ? baseUrlFromServerHost(host) : undefined;\n\t\tif (fromServer) return fromServer;\n\t}\n\n\treturn token ? PUBLIC_COPILOT_HUB : undefined;\n}\n\nfunction withModelBaseUrl<TApi extends Api>(models: readonly Model<TApi>[], baseUrl: string): Model<TApi>[] {\n\treturn models.map((model) => ({ ...model, baseUrl }));\n}\n\n/**\n * Rewrite the builtin Copilot provider's host when `COPILOT_GITHUB_TOKEN` is set.\n *\n * Applied to the builtin layer so a `models.json` `baseUrl` override, which is\n * composed afterwards, still takes precedence. Providers without the env token\n * are returned untouched so OAuth logins keep pi's exact behavior.\n */\nexport function withCopilotEnvBaseUrl(provider: Provider, env: CopilotRoutingEnv): Provider {\n\tif (provider.id !== COPILOT_PROVIDER_ID || !env.COPILOT_GITHUB_TOKEN?.trim()) return provider;\n\tconst baseUrl = resolveCopilotEnvBaseUrl(env);\n\tif (!baseUrl || baseUrl === provider.baseUrl) return provider;\n\treturn {\n\t\t...provider,\n\t\tbaseUrl,\n\t\t// Wrap the getter so dynamically refreshed models are routed too.\n\t\tgetModels: () => withModelBaseUrl(provider.getModels(), baseUrl),\n\t};\n}\n"]}
1
+ {"version":3,"file":"github-copilot-env.js","sourceRoot":"","sources":["../../src/providers/github-copilot-env.ts"],"names":[],"mappings":"AAmBA,MAAM,mBAAmB,GAAG,gBAAgB,CAAC;AAC7C,qFAAqF;AACrF,MAAM,kBAAkB,GAAG,+BAA+B,CAAC;AAC3D,MAAM,uBAAuB,GAAG,0CAA0C,CAAC;AAE3E,SAAS,gBAAgB,CAAC,KAAa;IACtC,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAClD,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC;IACxB,OAAO,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,OAAO,EAAE,CAAC;AACvE,CAAC;AAED,mFAAmF;AACnF,SAAS,gBAAgB,CAAC,KAAa;IACtC,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;IAC/C,IAAI,CAAC,KAAK,EAAE,CA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;QAC9C,MAAM,KAAK,GAAG,eAAe,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,GAAG,CAAC,oBAAoB,EAAE,IAAI,EAAE,CAAC;QAC9E,OAAO,KAAK,CAAC,CAAC,CAAC,wBAAwB,CAAC,EAAE,GAAG,GAAG,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC9F,CAAC,CAAC;IACF,MAAM,MAAM,GAAG,CAAqC,MAAyB,EAAqB,EAAE;QACnG,MAAM,OAAO,GAAG,aAAa,EAAE,CAAC;QAChC,OAAO,OAAO,CAAC,CAAC,CAAC,gBAAgB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAC7D,CAAC,CAAC;IACF,MAAM,EAAE,YAAY,EAAE,GAAG,QAAQ,CAAC;IAClC,OAAO;QACN,GAAG,QAAQ;QACX,IAAI,OAAO;YACV,OAAO,aAAa,EAAE,IAAI,QAAQ,CAAC,OAAO,CAAC;QAC5C,CAAC;QACD,SAAS,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;QAC7C,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACpF,CAAC;AACH,CAAC","sourcesContent":["import type { Provider } from \"../models.ts\";\n\n/**\n * GitHub Copilot host routing for raw-token (PAT) auth, whether the token comes\n * from `COPILOT_GITHUB_TOKEN` or from an API key saved through `/login`.\n *\n * Upstream pi pins `github-copilot` to `https://api.individual.githubcopilot.com`\n * and only derives a per-tenant host inside the OAuth loader, which returns an\n * `auth.baseUrl` from the token's `proxy-ep` segment. `envApiKeyAuth` resolves\n * raw tokens without a base URL, so business/enterprise tokens are sent to the\n * individual CAPI host and GitHub answers `421 Misdirected Request`.\n *\n * This module restores the routing precedence for the raw-token path only; the\n * OAuth path stays exactly upstream. A `models.json` provider `baseUrl` still\n * wins, because it is applied as a later overlay in `applyModelsJson`.\n */\n\nexport type CopilotRoutingEnv = Readonly<Record<string, string | undefined>>;\n\nconst COPILOT_PROVIDER_ID = \"github-copilot\";\n/** Public routing hub: resolves the caller's plan-specific CAPI host server-side. */\nconst PUBLIC_COPILOT_HUB = \"https://api.githubcopilot.com\";\nconst ENTERPRISE_COPILOT_HOST = \"https://api.enterprise.githubcopilot.com\";\n\nfunction normalizeBaseUrl(value: string): string {\n\tconst trimmed = value.trim().replace(/\\/+$/u, \"\");\n\tif (!trimmed) return \"\";\n\treturn /^https?:\\/\\//u.test(trimmed) ? trimmed : `https://${trimmed}`;\n}\n\n/** `tid=...;exp=...;proxy-ep=proxy.business.githubcopilot.com;...` -> API host. */\nfunction baseUrlFromToken(token: string): string | undefined {\n\tconst match = token.match(/proxy-ep=([^;]+)/u);\n\tif (!match?.[1]) return undefined;\n\tconst host = match[1].trim().replace(/^proxy\\./u, \"api.\");\n\treturn host ? normalizeBaseUrl(host) : undefined;\n}\n\nfunction hostFromServerUrl(serverUrl: string): string | undefined {\n\ttry {\n\t\treturn new URL(normalizeBaseUrl(serverUrl)).hostname.toLowerCase() || undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction baseUrlFromServerHost(host: string): string | undefined {\n\tif (host === \"github.com\" || host.endsWith(\".github.com\")) return undefined;\n\t// Tenant-scoped GHE.com: octocorp.ghe.com -> copilot-api.octocorp.ghe.com\n\tif (host.endsWith(\".ghe.com\")) {\n\t\tconst tenant = host.slice(0, -\".ghe.com\".length).split(\".\").pop();\n\t\treturn tenant ? `https://copilot-api.${tenant}.ghe.com` : ENTERPRISE_COPILOT_HOST;\n\t}\n\t// GitHub Enterprise Server on a self-hosted domain routes through the\n\t// enterprise CAPI host rather than a per-appliance Copilot endpoint.\n\treturn ENTERPRISE_COPILOT_HOST;\n}\n\n/**\n * Resolve the Copilot API base URL for env-token auth, highest precedence first:\n *\n * 1. `COPILOT_API_TARGET` / `GITHUB_COPILOT_BASE_URL` explicit override\n * 2. the `proxy-ep` segment embedded in `COPILOT_GITHUB_TOKEN`\n * 3. an explicit `enterpriseDomain`\n * 4. `GITHUB_SERVER_URL` (`*.ghe.com` tenant host, else the enterprise host)\n * 5. the public Copilot routing hub when `COPILOT_GITHUB_TOKEN` is set\n *\n * Returns `undefined` when nothing applies, leaving pi's individual-host default.\n */\nexport function resolveCopilotEnvBaseUrl(env: CopilotRoutingEnv, enterpriseDomain?: string): string | undefined {\n\tconst override = env.COPILOT_API_TARGET?.trim() || env.GITHUB_COPILOT_BASE_URL?.trim();\n\tif (override) return normalizeBaseUrl(override);\n\n\tconst token = env.COPILOT_GITHUB_TOKEN?.trim();\n\tif (token) {\n\t\tconst fromToken = baseUrlFromToken(token);\n\t\tif (fromToken) return fromToken;\n\t}\n\n\tif (enterpriseDomain?.trim()) return `https://copilot-api.${enterpriseDomain.trim()}`;\n\n\tconst serverUrl = env.GITHUB_SERVER_URL?.trim();\n\tif (serverUrl) {\n\t\tconst host = hostFromServerUrl(serverUrl);\n\t\tconst fromServer = host ? baseUrlFromServerHost(host) : undefined;\n\t\tif (fromServer) return fromServer;\n\t}\n\n\treturn token ? PUBLIC_COPILOT_HUB : undefined;\n}\n\nfunction withModelBaseUrl<TModel extends { baseUrl: string }>(models: readonly TModel[], baseUrl: string): TModel[] {\n\treturn models.map((model) => ({ ...model, baseUrl }));\n}\n\n/**\n * Rewrite the builtin Copilot provider's host when a raw Copilot token is configured.\n *\n * `getStoredApiKey` returns the API key saved through `/login`, if any. It wins\n * over `COPILOT_GITHUB_TOKEN`, matching credential resolution, and is read on\n * every access so a login or logout reroutes without rebuilding the provider.\n *\n * Applied to the builtin layer so a `models.json` `baseUrl` override, which is\n * composed afterwards, still takes precedence. Without a raw token the provider\n * keeps pi's exact behavior, so OAuth logins are unaffected.\n */\nexport function withCopilotEnvBaseUrl(\n\tprovider: Provider,\n\tenv: CopilotRoutingEnv,\n\tgetStoredApiKey?: () => string | undefined,\n): Provider {\n\tif (provider.id !== COPILOT_PROVIDER_ID) return provider;\n\tif (!getStoredApiKey && !env.COPILOT_GITHUB_TOKEN?.trim()) return provider;\n\tconst routedBaseUrl = (): string | undefined => {\n\t\tconst token = getStoredApiKey?.()?.trim() || env.COPILOT_GITHUB_TOKEN?.trim();\n\t\treturn token ? resolveCopilotEnvBaseUrl({ ...env, COPILOT_GITHUB_TOKEN: token }) : undefined;\n\t};\n\tconst routed = <TModel extends { baseUrl: string }>(models: readonly TModel[]): readonly TModel[] => {\n\t\tconst baseUrl = routedBaseUrl();\n\t\treturn baseUrl ? withModelBaseUrl(models, baseUrl) : models;\n\t};\n\tconst { getAllModels } = provider;\n\treturn {\n\t\t...provider,\n\t\tget baseUrl() {\n\t\t\treturn routedBaseUrl() ?? provider.baseUrl;\n\t\t},\n\t\tgetModels: () => routed(provider.getModels()),\n\t\t...(getAllModels ? { getAllModels: () => routed(getAllModels.call(provider)) } : {}),\n\t};\n}\n"]}
@@ -1,2 +1,2 @@
1
- export declare const PI_AI_VERSION = "0.9.58";
1
+ export declare const PI_AI_VERSION = "0.9.59";
2
2
  //# sourceMappingURL=version.d.ts.map
@@ -1,3 +1,3 @@
1
1
  // The @bastani/pi-ai package version. scripts/bump-version.ts keeps it in sync with package.json.
2
- export const PI_AI_VERSION = "0.9.58";
2
+ export const PI_AI_VERSION = "0.9.59";
3
3
  //# sourceMappingURL=version.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/utils/version.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC","sourcesContent":["// The @bastani/pi-ai package version. scripts/bump-version.ts keeps it in sync with package.json.\nexport const PI_AI_VERSION = \"0.9.58\";\n"]}
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/utils/version.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC","sourcesContent":["// The @bastani/pi-ai package version. scripts/bump-version.ts keeps it in sync with package.json.\nexport const PI_AI_VERSION = \"0.9.59\";\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bastani/pi-ai",
3
- "version": "0.9.58",
3
+ "version": "0.9.59",
4
4
  "description": "Bastani-branded unified LLM API with automatic model discovery and provider configuration",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",