@aliou/pi-neuralwatt 0.18.1 → 0.19.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.
Files changed (40) hide show
  1. package/CHANGELOG.md +624 -0
  2. package/README.md +23 -0
  3. package/dist/provider/index.d.ts +28 -0
  4. package/dist/provider/index.js +1142 -0
  5. package/extensions/_shared/config/index.ts +1 -0
  6. package/{src → extensions/_shared}/config/loader.ts +11 -25
  7. package/{src → extensions/_shared}/config/migration/02-flat-to-nested-config.ts +1 -1
  8. package/extensions/command-quotas/command.ts +7 -2
  9. package/extensions/command-quotas/components/quota-tabs.ts +1 -1
  10. package/extensions/command-quotas/components/quotas-display.ts +2 -1
  11. package/extensions/command-quotas/index.ts +1 -1
  12. package/extensions/provider/commands/settings/api-base-url-editor.ts +2 -6
  13. package/extensions/provider/commands/settings/index.ts +31 -5
  14. package/extensions/provider/index.ts +39 -31
  15. package/extensions/provider/quota-store.ts +2 -1
  16. package/extensions/quota-warnings/index.ts +1 -1
  17. package/extensions/shared/truncate.ts +292 -0
  18. package/package.json +24 -8
  19. package/schema.json +14 -0
  20. package/{extensions/provider → src}/api/anthropic-messages.ts +22 -8
  21. package/{extensions/provider → src}/api/openai-completions.ts +38 -7
  22. package/{extensions/provider → src}/api/system-one.ts +5 -3
  23. package/src/config/index.ts +1 -2
  24. package/src/config/resolve.ts +25 -0
  25. package/src/config/types.ts +11 -0
  26. package/{extensions/provider → src}/constants.ts +1 -0
  27. package/src/lib/neuralwatt-api.ts +15 -7
  28. package/{extensions/provider → src}/models/build.ts +10 -10
  29. package/{extensions/provider → src}/models/catalog.ts +1 -1
  30. package/{extensions/provider → src}/models/refresh.ts +16 -9
  31. package/{extensions/provider → src}/provider.ts +81 -46
  32. package/{extensions/provider → src}/sse-quotas.ts +5 -4
  33. package/{extensions/provider → src}/stream-simple.ts +3 -3
  34. /package/{src → extensions/_shared}/config/migration/03-rename-hidden-to-early-access.ts +0 -0
  35. /package/{src → extensions/_shared}/config/migration/04-remove-sub-bar-integration.ts +0 -0
  36. /package/{src → extensions/_shared}/config/migration/index.ts +0 -0
  37. /package/{extensions/provider → src}/api/types.ts +0 -0
  38. /package/{extensions/provider → src}/models/index.ts +0 -0
  39. /package/{extensions/provider → src}/models/public-models.ts +0 -0
  40. /package/{extensions/provider → src}/rate-limit-error.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aliou/pi-neuralwatt",
3
- "version": "0.18.1",
3
+ "version": "0.19.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -13,6 +13,13 @@
13
13
  "type": "git",
14
14
  "url": "https://github.com/aliou/pi-neuralwatt"
15
15
  },
16
+ "sideEffects": false,
17
+ "exports": {
18
+ "./provider": {
19
+ "types": "./dist/provider/index.d.ts",
20
+ "import": "./dist/provider/index.js"
21
+ }
22
+ },
16
23
  "pi": {
17
24
  "extensions": [
18
25
  "./extensions/provider/index.ts",
@@ -27,7 +34,9 @@
27
34
  "files": [
28
35
  "src",
29
36
  "extensions",
37
+ "dist",
30
38
  "schema.json",
39
+ "CHANGELOG.md",
31
40
  "README.md",
32
41
  "!src/**/*.test.ts",
33
42
  "!extensions/**/*.test.ts"
@@ -37,26 +46,30 @@
37
46
  "@aliou/pi-utils-ui": "^0.5.0"
38
47
  },
39
48
  "peerDependencies": {
40
- "@earendil-works/pi-ai": ">=1.0.0",
49
+ "@earendil-works/pi-ai": "1.5.0",
41
50
  "@earendil-works/pi-coding-agent": ">=1.4.0",
42
- "@earendil-works/pi-tui": "*"
51
+ "@earendil-works/pi-tui": "*",
52
+ "typebox": "*"
43
53
  },
44
54
  "devDependencies": {
45
- "@aliou/biome-plugins": "^0.11.0",
46
- "@biomejs/biome": "^2.4.15",
55
+ "@aliou/biome-plugins": "^0.16.0",
56
+ "@biomejs/biome": "^2.6.4",
47
57
  "@changesets/cli": "^2.27.11",
48
- "@earendil-works/pi-ai": "1.4.0",
49
- "@earendil-works/pi-coding-agent": "1.4.0",
50
- "@earendil-works/pi-tui": "1.4.0",
58
+ "@earendil-works/pi-ai": "1.5.0",
59
+ "@earendil-works/pi-coding-agent": "1.5.0",
60
+ "@earendil-works/pi-tui": "1.5.0",
51
61
  "@types/node": "^25.0.10",
52
62
  "husky": "^9.1.7",
53
63
  "ts-json-schema-generator": "^2.4.0",
64
+ "tsdown": "^0.23.4",
54
65
  "tsx": "4.23.12",
55
66
  "typebox": "1.3.27",
56
67
  "typescript": "^5.9.3",
57
68
  "vitest": "^4.0.18"
58
69
  },
59
70
  "scripts": {
71
+ "build": "tsdown",
72
+ "prepack": "pnpm build",
60
73
  "typecheck": "tsc --noEmit",
61
74
  "lint": "biome check",
62
75
  "format": "biome check --write",
@@ -82,6 +95,9 @@
82
95
  },
83
96
  "@earendil-works/pi-tui": {
84
97
  "optional": true
98
+ },
99
+ "typebox": {
100
+ "optional": true
85
101
  }
86
102
  }
87
103
  }
package/schema.json CHANGED
@@ -16,6 +16,10 @@
16
16
  "$ref": "#/definitions/NeuralwattQuotaWarningsConfig",
17
17
  "description": "Quota warning feature."
18
18
  },
19
+ "hostedTools": {
20
+ "$ref": "#/definitions/NeuralwattHostedToolsConfig",
21
+ "description": "Hosted tools (gateway-resolved nw_* tools)."
22
+ },
19
23
  "provider": {
20
24
  "$ref": "#/definitions/NeuralwattProviderConfig",
21
25
  "description": "Provider behavior (API surface)."
@@ -56,6 +60,16 @@
56
60
  },
57
61
  "additionalProperties": false
58
62
  },
63
+ "NeuralwattHostedToolsConfig": {
64
+ "type": "object",
65
+ "properties": {
66
+ "enabled": {
67
+ "type": "boolean",
68
+ "description": "Offer Neuralwatt hosted tools on OpenAI-surface requests. Unset leaves them alone; false sends the opt-out header with every request."
69
+ }
70
+ },
71
+ "additionalProperties": false
72
+ },
59
73
  "NeuralwattProviderConfig": {
60
74
  "type": "object",
61
75
  "properties": {
@@ -1,7 +1,6 @@
1
- import type { StreamOptions } from "@earendil-works/pi-ai";
2
- import { stream, streamSimple } from "@earendil-works/pi-ai/compat";
3
- import { configuredApiBaseUrl } from "../../../src/config/loader";
1
+ import type { ProviderStreams, StreamOptions } from "@earendil-works/pi-ai";
4
2
  import {
3
+ NEURALWATT_BASE_URL,
5
4
  NEURALWATT_PROVIDER_ID,
6
5
  NEURALWATT_REQUEST_HEADERS,
7
6
  } from "../constants";
@@ -61,7 +60,7 @@ function toMessagesBaseUrl(baseUrl: string): string {
61
60
  return baseUrl.replace(/\/v1\/?$/, "");
62
61
  }
63
62
 
64
- function stampAnthropicModels(models: NeuralwattChatModel[]) {
63
+ function stampAnthropicModels(models: NeuralwattChatModel[], baseUrl: string) {
65
64
  return models.map((model) => {
66
65
  const { reasoningContract, ...compiled } = model;
67
66
  // No retained contract: the identity map is the alias-free special case.
@@ -77,7 +76,7 @@ function stampAnthropicModels(models: NeuralwattChatModel[]) {
77
76
  ...compiled,
78
77
  api: "anthropic-messages" as const,
79
78
  provider: NEURALWATT_PROVIDER_ID,
80
- baseUrl: toMessagesBaseUrl(model.baseUrl ?? configuredApiBaseUrl()),
79
+ baseUrl: toMessagesBaseUrl(model.baseUrl ?? baseUrl),
81
80
  headers: NEURALWATT_REQUEST_HEADERS,
82
81
  compat: {
83
82
  forceAdaptiveThinking: true,
@@ -91,8 +90,19 @@ function stampAnthropicModels(models: NeuralwattChatModel[]) {
91
90
  }
92
91
 
93
92
  export function createAnthropicMessagesApi(options?: {
93
+ upstream?: ProviderStreams;
94
94
  streamSimple?: AnyStreamSimple;
95
+ baseUrl?: string;
95
96
  }): NeuralwattApiHandler {
97
+ const requireUpstream = (): ProviderStreams => {
98
+ if (!options?.upstream) {
99
+ throw new Error(
100
+ "createAnthropicMessagesApi requires an upstream: pass pi-ai's anthropicMessagesApi() streams",
101
+ );
102
+ }
103
+ return options.upstream;
104
+ };
105
+ const fallbackBaseUrl = options?.baseUrl ?? NEURALWATT_BASE_URL;
96
106
  const withReasoning = (options?: {
97
107
  onPayload?: StreamOptions["onPayload"];
98
108
  }) => ({
@@ -101,11 +111,15 @@ export function createAnthropicMessagesApi(options?: {
101
111
  });
102
112
 
103
113
  return {
104
- stampModels: stampAnthropicModels,
114
+ stampModels: (models) => stampAnthropicModels(models, fallbackBaseUrl),
105
115
  stream: (model, context, streamOptions) =>
106
- stream(model, context, withReasoning(streamOptions) as never),
116
+ requireUpstream().stream(
117
+ model,
118
+ context,
119
+ withReasoning(streamOptions) as never,
120
+ ),
107
121
  streamSimple: (model, context, simpleOptions) =>
108
- ((options?.streamSimple ?? streamSimple) as AnyStreamSimple)(
122
+ (options?.streamSimple ?? requireUpstream().streamSimple)(
109
123
  model,
110
124
  context,
111
125
  withReasoning(simpleOptions),
@@ -1,9 +1,9 @@
1
- import type { StreamOptions } from "@earendil-works/pi-ai";
2
- import { stream, streamSimple } from "@earendil-works/pi-ai/compat";
3
- import { configuredApiBaseUrl } from "../../../src/config/loader";
1
+ import type { ProviderStreams, StreamOptions } from "@earendil-works/pi-ai";
4
2
  import {
3
+ NEURALWATT_BASE_URL,
5
4
  NEURALWATT_PROVIDER_ID,
6
5
  NEURALWATT_REQUEST_HEADERS,
6
+ NEURALWATT_TOOLS_OPT_OUT_HEADER,
7
7
  } from "../constants";
8
8
  import type { NeuralwattChatModel } from "../models/catalog";
9
9
  import type { AnyStreamSimple } from "../stream-simple";
@@ -49,9 +49,36 @@ function makeReasoningReplayInjector(
49
49
  };
50
50
  }
51
51
 
52
+ type StreamRequestOptions = {
53
+ onPayload?: StreamOptions["onPayload"];
54
+ headers?: StreamOptions["headers"];
55
+ };
56
+
52
57
  export function createOpenAiCompletionsApi(options?: {
58
+ upstream?: ProviderStreams;
53
59
  streamSimple?: AnyStreamSimple;
60
+ baseUrl?: string;
61
+ toolsOptOut?: () => boolean;
54
62
  }): NeuralwattApiHandler {
63
+ const requireUpstream = (): ProviderStreams => {
64
+ if (!options?.upstream) {
65
+ throw new Error(
66
+ "createOpenAiCompletionsApi requires an upstream: pass pi-ai's openAICompletionsApi() streams",
67
+ );
68
+ }
69
+ return options.upstream;
70
+ };
71
+ const fallbackBaseUrl = options?.baseUrl ?? NEURALWATT_BASE_URL;
72
+ const withOptOutHeader = (requestOptions?: StreamRequestOptions) => {
73
+ if (!options?.toolsOptOut?.()) return requestOptions;
74
+ return {
75
+ ...requestOptions,
76
+ headers: {
77
+ ...requestOptions?.headers,
78
+ [NEURALWATT_TOOLS_OPT_OUT_HEADER]: "true",
79
+ },
80
+ };
81
+ };
55
82
  const withReasoningReplay = (options?: {
56
83
  onPayload?: StreamOptions["onPayload"];
57
84
  }) => ({
@@ -67,17 +94,21 @@ export function createOpenAiCompletionsApi(options?: {
67
94
  ...compiled,
68
95
  api: "openai-completions" as const,
69
96
  provider: NEURALWATT_PROVIDER_ID,
70
- baseUrl: model.baseUrl ?? configuredApiBaseUrl(),
97
+ baseUrl: model.baseUrl ?? fallbackBaseUrl,
71
98
  headers: NEURALWATT_REQUEST_HEADERS,
72
99
  };
73
100
  }),
74
101
  stream: (model, context, streamOptions) =>
75
- stream(model, context, withReasoningReplay(streamOptions) as never),
102
+ requireUpstream().stream(
103
+ model,
104
+ context,
105
+ withOptOutHeader(withReasoningReplay(streamOptions)) as never,
106
+ ),
76
107
  streamSimple: (model, context, simpleOptions) =>
77
- ((options?.streamSimple ?? streamSimple) as AnyStreamSimple)(
108
+ (options?.streamSimple ?? requireUpstream().streamSimple)(
78
109
  model,
79
110
  context,
80
- withReasoningReplay(simpleOptions),
111
+ withOptOutHeader(withReasoningReplay(simpleOptions)),
81
112
  ),
82
113
  };
83
114
  }
@@ -11,8 +11,8 @@ import { calculateCost } from "@earendil-works/pi-ai";
11
11
  import type { Static } from "typebox";
12
12
  import { Type } from "typebox";
13
13
  import { Check, Parse, ParseError } from "typebox/value";
14
- import { configuredApiBaseUrl } from "../../../src/config/loader";
15
14
  import {
15
+ NEURALWATT_BASE_URL,
16
16
  NEURALWATT_PROVIDER_ID,
17
17
  NEURALWATT_REQUEST_HEADERS,
18
18
  } from "../constants";
@@ -162,7 +162,8 @@ async function request(
162
162
  options.signal?.throwIfAborted();
163
163
 
164
164
  const rawPayload = formatRequest(model, context);
165
- const payload = (await options.onPayload?.(rawPayload, model)) ?? rawPayload;
165
+ const formattedPayload = await options.onPayload?.(rawPayload, model);
166
+ const payload = formattedPayload ?? rawPayload;
166
167
 
167
168
  const url = new URL("systemone", `${model.baseUrl.replace(/\/+$/u, "")}/`);
168
169
 
@@ -231,12 +232,13 @@ export const classify: ClassifierFunction = async (
231
232
 
232
233
  export function stampClassifierModels(
233
234
  models: NeuralwattClassifierModel[],
235
+ baseUrl: string = NEURALWATT_BASE_URL,
234
236
  ): ClassifierModel<ClassifierApi>[] {
235
237
  return models.map((model) => ({
236
238
  ...model,
237
239
  api: NEURALWATT_SYSTEM_ONE_API,
238
240
  provider: NEURALWATT_PROVIDER_ID,
239
- baseUrl: model.baseUrl ?? configuredApiBaseUrl(),
241
+ baseUrl: model.baseUrl ?? baseUrl,
240
242
  headers: NEURALWATT_REQUEST_HEADERS,
241
243
  }));
242
244
  }
@@ -1,6 +1,5 @@
1
1
  export { DEFAULT_CONFIG } from "./defaults";
2
- export { configLoader, resolveApi } from "./loader";
3
- export { migrations } from "./migration";
2
+ export { resolveApi, resolveApiBaseUrl } from "./resolve";
4
3
  export type {
5
4
  NeuralwattApi,
6
5
  NeuralwattConfig,
@@ -0,0 +1,25 @@
1
+ import { DEFAULT_CONFIG } from "./defaults";
2
+ import type { NeuralwattApi } from "./types";
3
+
4
+ export function resolveApi(value: string | undefined): NeuralwattApi {
5
+ if (value === "anthropic-messages" || value === "openai-completions") {
6
+ return value;
7
+ }
8
+ return DEFAULT_CONFIG.provider.api;
9
+ }
10
+
11
+ export function resolveApiBaseUrl(value: string | undefined): string {
12
+ const trimmed = value?.trim().replace(/\/+$/u, "");
13
+ if (!trimmed) {
14
+ return DEFAULT_CONFIG.provider.apiBaseUrl;
15
+ }
16
+ try {
17
+ const url = new URL(trimmed);
18
+ if (url.protocol !== "https:" && url.protocol !== "http:") {
19
+ return DEFAULT_CONFIG.provider.apiBaseUrl;
20
+ }
21
+ } catch {
22
+ return DEFAULT_CONFIG.provider.apiBaseUrl;
23
+ }
24
+ return trimmed;
25
+ }
@@ -15,6 +15,14 @@ export interface NeuralwattQuotaWarningsConfig {
15
15
  */
16
16
  export type NeuralwattApi = "openai-completions" | "anthropic-messages";
17
17
 
18
+ export interface NeuralwattHostedToolsConfig {
19
+ /**
20
+ * Offer Neuralwatt hosted tools on OpenAI-surface requests. Unset leaves
21
+ * them alone; false sends the opt-out header with every request.
22
+ */
23
+ enabled?: boolean;
24
+ }
25
+
18
26
  export interface NeuralwattProviderConfig {
19
27
  /** Which API serves model requests. */
20
28
  api?: NeuralwattApi;
@@ -38,6 +46,8 @@ export interface NeuralwattConfig {
38
46
  /** Quota warning feature. */
39
47
  quotaWarnings?: NeuralwattQuotaWarningsConfig;
40
48
 
49
+ /** Hosted tools (gateway-resolved nw_* tools). */
50
+ hostedTools?: NeuralwattHostedToolsConfig;
41
51
  /** Provider behavior (API surface). */
42
52
  provider?: NeuralwattProviderConfig;
43
53
  }
@@ -49,6 +59,7 @@ export interface ResolvedNeuralwattConfig {
49
59
  quotaWarnings: {
50
60
  enabled: boolean;
51
61
  };
62
+ hostedTools?: NeuralwattHostedToolsConfig;
52
63
  provider: {
53
64
  api: NeuralwattApi;
54
65
  apiBaseUrl: string;
@@ -5,3 +5,4 @@ export const NEURALWATT_REQUEST_HEADERS = {
5
5
  Referer: "https://pi.dev",
6
6
  "X-Title": "npm:@aliou/pi-neuralwatt",
7
7
  };
8
+ export const NEURALWATT_TOOLS_OPT_OUT_HEADER = "X-NW-Tools-Opt-Out";
@@ -1,4 +1,3 @@
1
- import { configuredApiBaseUrl } from "../config/loader";
2
1
  import type {
3
2
  NeuralwattApiModel,
4
3
  NeuralwattApiModelsResponse,
@@ -27,11 +26,12 @@ function combineSignals(signal?: AbortSignal): AbortSignal {
27
26
 
28
27
  async function neuralwattFetch(
29
28
  path: string,
29
+ baseUrl: string,
30
30
  apiKey: string | undefined,
31
31
  signal?: AbortSignal,
32
32
  headers?: Record<string, string>,
33
33
  ): Promise<Response> {
34
- return fetch(`${configuredApiBaseUrl()}${path}`, {
34
+ return fetch(`${baseUrl}${path}`, {
35
35
  headers: { ...authHeaders(apiKey), ...headers },
36
36
  signal: combineSignals(signal),
37
37
  });
@@ -43,15 +43,22 @@ export type NeuralwattModelsResult =
43
43
 
44
44
  export async function fetchNeuralwattModels(
45
45
  apiKey: string | undefined,
46
+ baseUrl: string,
46
47
  signal?: AbortSignal,
47
48
  ): Promise<NeuralwattModelsResult> {
48
49
  const combined = combineSignals(signal);
49
50
 
50
51
  try {
51
- const response = await neuralwattFetch("/models", apiKey, combined, {
52
- Referer: "https://pi.dev",
53
- "X-Title": "npm:@aliou/pi-neuralwatt",
54
- });
52
+ const response = await neuralwattFetch(
53
+ "/models",
54
+ baseUrl,
55
+ apiKey,
56
+ combined,
57
+ {
58
+ Referer: "https://pi.dev",
59
+ "X-Title": "npm:@aliou/pi-neuralwatt",
60
+ },
61
+ );
55
62
 
56
63
  if (!response.ok) {
57
64
  return { success: false };
@@ -66,6 +73,7 @@ export async function fetchNeuralwattModels(
66
73
 
67
74
  export async function fetchQuotas(
68
75
  apiKey: string,
76
+ baseUrl: string,
69
77
  signal?: AbortSignal,
70
78
  ): Promise<QuotasResult> {
71
79
  if (!apiKey) {
@@ -78,7 +86,7 @@ export async function fetchQuotas(
78
86
  const combined = combineSignals(signal);
79
87
 
80
88
  try {
81
- const response = await neuralwattFetch("/quota", apiKey, combined);
89
+ const response = await neuralwattFetch("/quota", baseUrl, apiKey, combined);
82
90
 
83
91
  if (!response.ok) {
84
92
  let message = response.statusText;
@@ -1,18 +1,18 @@
1
- import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
1
+ import type { Api, ClassifierModel, Model } from "@earendil-works/pi-ai";
2
2
  import type {
3
3
  NeuralwattApiModelReasoning,
4
4
  NeuralwattReasoningEffort,
5
- } from "../../../src/types/models-api";
5
+ } from "../types/models-api";
6
6
 
7
- export type ProviderChatModelConfig = Extract<
8
- ProviderModelConfig,
9
- { type?: "chat" }
10
- >;
7
+ export type ProviderChatModelConfig = Omit<
8
+ Model<Api>,
9
+ "api" | "provider" | "baseUrl"
10
+ > & { baseUrl?: string };
11
11
 
12
- export type ProviderClassifierModelConfig = Extract<
13
- ProviderModelConfig,
14
- { type: "classifier" }
15
- >;
12
+ export type ProviderClassifierModelConfig = Omit<
13
+ ClassifierModel<"typesafe-system-one">,
14
+ "api" | "provider" | "baseUrl"
15
+ > & { baseUrl?: string };
16
16
 
17
17
  export const NEURALWATT_SYSTEM_ONE_API = "typesafe-system-one" as const;
18
18
 
@@ -1,4 +1,4 @@
1
- import type { NeuralwattApiModel } from "../../../src/types/models-api";
1
+ import type { NeuralwattApiModel } from "../types/models-api";
2
2
  import {
3
3
  buildThinkingLevelMap,
4
4
  NEURALWATT_SYSTEM_ONE_API,
@@ -3,8 +3,7 @@ import type {
3
3
  ModelsStoreEntry,
4
4
  RefreshModelsContext,
5
5
  } from "@earendil-works/pi-ai";
6
- import { configuredApiBaseUrl } from "../../../src/config/loader";
7
- import type { NeuralwattApiModel } from "../../../src/types/models-api";
6
+ import type { NeuralwattApiModel } from "../types/models-api";
8
7
  import type {
9
8
  buildNeuralwattProviderModels,
10
9
  buildNeuralwattProviderModelsFromApi,
@@ -33,15 +32,16 @@ function catalogScope(apiKey: string | undefined): CatalogScope {
33
32
  : "public";
34
33
  }
35
34
 
36
- function catalogKey(scope: CatalogScope): string {
37
- return `${scope} ${CATALOG_SCOPE_VERSION} ${configuredApiBaseUrl()}`;
35
+ function catalogKey(scope: CatalogScope, baseUrl: string): string {
36
+ return `${scope} ${CATALOG_SCOPE_VERSION} ${baseUrl}`;
38
37
  }
39
38
 
40
39
  function catalogKeyMatches(
41
40
  entry: ScopedModelsStoreEntry | undefined,
42
41
  scope: CatalogScope,
42
+ baseUrl: string,
43
43
  ): boolean {
44
- return entry !== undefined && entry.catalogKey === catalogKey(scope);
44
+ return entry !== undefined && entry.catalogKey === catalogKey(scope, baseUrl);
45
45
  }
46
46
 
47
47
  export type FetchNeuralwattApiModels = (
@@ -64,8 +64,9 @@ function isFreshStoreEntry(
64
64
  function isUsableStoreEntry(
65
65
  entry: Readonly<ModelsStoreEntry> | undefined,
66
66
  scope: CatalogScope,
67
+ baseUrl: string,
67
68
  ): entry is ModelsStoreEntry {
68
- return isFreshStoreEntry(entry) && catalogKeyMatches(entry, scope);
69
+ return isFreshStoreEntry(entry) && catalogKeyMatches(entry, scope, baseUrl);
69
70
  }
70
71
 
71
72
  export function createNeuralwattRefreshModels(
@@ -73,9 +74,11 @@ export function createNeuralwattRefreshModels(
73
74
  fetchApiModels: FetchNeuralwattApiModels,
74
75
  buildFromApi: typeof buildNeuralwattProviderModelsFromApi,
75
76
  buildFromStore: typeof buildNeuralwattProviderModelsFromStore,
77
+ baseUrl: string | (() => string),
76
78
  ) {
77
79
  return async (context: RefreshModelsContext): Promise<NeuralwattModel[]> => {
78
80
  context.signal.throwIfAborted();
81
+ const resolvedBaseUrl = typeof baseUrl === "function" ? baseUrl() : baseUrl;
79
82
  const fallback = buildFromStore(staticModels);
80
83
  try {
81
84
  const apiKey =
@@ -85,11 +88,15 @@ export function createNeuralwattRefreshModels(
85
88
  const scope = catalogScope(apiKey);
86
89
  const stored = context.stored as ScopedModelsStoreEntry | undefined;
87
90
  if (!context.allowNetwork) {
88
- return stored !== undefined && catalogKeyMatches(stored, scope)
91
+ return stored !== undefined &&
92
+ catalogKeyMatches(stored, scope, resolvedBaseUrl)
89
93
  ? buildFromStore(asStoredChatModels(stored.models))
90
94
  : fallback;
91
95
  }
92
- if (!context.force && isUsableStoreEntry(stored, scope)) {
96
+ if (
97
+ !context.force &&
98
+ isUsableStoreEntry(stored, scope, resolvedBaseUrl)
99
+ ) {
93
100
  return buildFromStore(asStoredChatModels(stored.models));
94
101
  }
95
102
  const apiModels = await fetchApiModels(apiKey, context.signal);
@@ -98,7 +105,7 @@ export function createNeuralwattRefreshModels(
98
105
  const entry: ScopedModelsStoreEntry = {
99
106
  models: models as unknown as ModelsStoreEntry["models"],
100
107
  checkedAt: Date.now(),
101
- catalogKey: catalogKey(scope),
108
+ catalogKey: catalogKey(scope, resolvedBaseUrl),
102
109
  };
103
110
  await context.publish({ persist: entry }).catch(() => undefined);
104
111
  context.signal.throwIfAborted();