@aliou/pi-neuralwatt 0.16.2 → 0.17.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.
@@ -4,8 +4,24 @@ import type {
4
4
  NeuralwattReasoningEffort,
5
5
  } from "../../../src/types/models-api";
6
6
 
7
+ export type ProviderChatModelConfig = Extract<
8
+ ProviderModelConfig,
9
+ { type?: "chat" }
10
+ >;
11
+
12
+ export type ProviderClassifierModelConfig = Extract<
13
+ ProviderModelConfig,
14
+ { type: "classifier" }
15
+ >;
16
+
17
+ export const NEURALWATT_SYSTEM_ONE_API = "typesafe-system-one" as const;
18
+
19
+ export type NeuralwattClassifierModel = ProviderClassifierModelConfig & {
20
+ api: typeof NEURALWATT_SYSTEM_ONE_API;
21
+ };
22
+
7
23
  export type ThinkingLevelMap = NonNullable<
8
- ProviderModelConfig["thinkingLevelMap"]
24
+ ProviderChatModelConfig["thinkingLevelMap"]
9
25
  >;
10
26
 
11
27
  /**
@@ -13,7 +29,7 @@ export type ThinkingLevelMap = NonNullable<
13
29
  * retained for anthropic-messages map derivation. Rides the models store
14
30
  * (JSON passthrough); stripped from stamped runtime models.
15
31
  */
16
- export type NeuralwattCompiledModel = ProviderModelConfig & {
32
+ export type NeuralwattCompiledModel = ProviderChatModelConfig & {
17
33
  reasoningContract?: NeuralwattReasoningMapSource;
18
34
  };
19
35
 
@@ -157,7 +173,7 @@ export function buildNeuralwattModel(
157
173
  ): NeuralwattCompiledModel {
158
174
  const vision = variant.vision ?? family.vision;
159
175
 
160
- const compat: NonNullable<ProviderModelConfig["compat"]> = {
176
+ const compat: NonNullable<ProviderChatModelConfig["compat"]> = {
161
177
  supportsDeveloperRole: false,
162
178
  maxTokensField: "max_tokens",
163
179
  };
@@ -1,19 +1,45 @@
1
- import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
2
1
  import type { NeuralwattApiModel } from "../../../src/types/models-api";
3
2
  import {
4
3
  buildThinkingLevelMap,
4
+ NEURALWATT_SYSTEM_ONE_API,
5
+ type NeuralwattClassifierModel,
5
6
  type NeuralwattCompiledModel,
7
+ type ProviderChatModelConfig,
6
8
  resolveMaxTokens,
7
9
  type ThinkingLevelMap,
8
10
  } from "./build";
9
11
  import { NEURALWATT_MODELS } from "./public-models";
10
12
 
11
- export type NeuralwattModel = NeuralwattCompiledModel;
13
+ export type NeuralwattChatModel = NeuralwattCompiledModel;
14
+
15
+ export type NeuralwattModel = NeuralwattChatModel | NeuralwattClassifierModel;
16
+
17
+ export function isNeuralwattClassifierModel(
18
+ model: NeuralwattModel,
19
+ ): model is NeuralwattClassifierModel {
20
+ return model.type === "classifier";
21
+ }
22
+
23
+ export function partitionNeuralwattModels(models: readonly NeuralwattModel[]): {
24
+ chat: NeuralwattChatModel[];
25
+ classifiers: NeuralwattClassifierModel[];
26
+ } {
27
+ const chat: NeuralwattChatModel[] = [];
28
+ const classifiers: NeuralwattClassifierModel[] = [];
29
+ for (const model of models) {
30
+ if (isNeuralwattClassifierModel(model)) {
31
+ classifiers.push(model);
32
+ } else {
33
+ chat.push(model);
34
+ }
35
+ }
36
+ return { chat, classifiers };
37
+ }
12
38
 
13
39
  // Chat-template thinking: the API exposes a `reasoning` block, but the
14
40
  // underlying mechanism is chat_template_kwargs, so Pi needs the mapping.
15
41
  const COMPAT_OVERRIDES: Partial<
16
- Record<string, Partial<NonNullable<ProviderModelConfig["compat"]>>>
42
+ Record<string, Partial<NonNullable<ProviderChatModelConfig["compat"]>>>
17
43
  > = {
18
44
  "Qwen/Qwen3.8-27B-FP8": {
19
45
  thinkingFormat: "chat-template",
@@ -23,7 +49,36 @@ const COMPAT_OVERRIDES: Partial<
23
49
  },
24
50
  };
25
51
 
26
- function apiModelToProviderModel(model: NeuralwattApiModel): NeuralwattModel {
52
+ function apiModelToClassifierModel(
53
+ model: NeuralwattApiModel,
54
+ ): NeuralwattClassifierModel {
55
+ const meta = model.metadata;
56
+ if (!meta)
57
+ throw new Error(
58
+ `Neuralwatt API returned model "${model.id}" without metadata`,
59
+ );
60
+
61
+ return {
62
+ type: "classifier",
63
+ api: NEURALWATT_SYSTEM_ONE_API,
64
+ id: model.id,
65
+ name: meta.display_name ?? model.id,
66
+ input: meta.capabilities.vision
67
+ ? (["text", "image"] as const)
68
+ : (["text"] as const),
69
+ cost: {
70
+ input: meta.pricing.input_per_million,
71
+ output: meta.pricing.output_per_million,
72
+ cacheRead: meta.pricing.cached_input_per_million ?? 0,
73
+ cacheWrite: meta.pricing.cached_output_per_million ?? 0,
74
+ },
75
+ contextWindow: model.max_model_len,
76
+ };
77
+ }
78
+
79
+ function apiModelToProviderModel(
80
+ model: NeuralwattApiModel,
81
+ ): NeuralwattChatModel {
27
82
  const meta = model.metadata;
28
83
  if (!meta)
29
84
  throw new Error(
@@ -32,7 +87,7 @@ function apiModelToProviderModel(model: NeuralwattApiModel): NeuralwattModel {
32
87
 
33
88
  const reasoning = meta.capabilities.reasoning;
34
89
 
35
- const compat: NonNullable<ProviderModelConfig["compat"]> = {
90
+ const compat: NonNullable<ProviderChatModelConfig["compat"]> = {
36
91
  supportsDeveloperRole: meta.capabilities.developer_role,
37
92
  maxTokensField: "max_tokens",
38
93
  ...COMPAT_OVERRIDES[model.id],
@@ -83,13 +138,18 @@ export function buildNeuralwattProviderModelsFromApi(
83
138
  m.metadata &&
84
139
  !m.metadata.deprecated &&
85
140
  !m.metadata.pricing.pricing_tbd &&
86
- // Exclude non-chat models (e.g. embeddings) by task
87
141
  !(
88
142
  m.metadata.capabilities.task &&
89
- !["chat", "completions"].includes(m.metadata.capabilities.task)
143
+ !["chat", "completions", "decision"].includes(
144
+ m.metadata.capabilities.task,
145
+ )
90
146
  ),
91
147
  )
92
- .map(apiModelToProviderModel);
148
+ .map((m) =>
149
+ m.metadata?.capabilities.task === "decision"
150
+ ? apiModelToClassifierModel(m)
151
+ : apiModelToProviderModel(m),
152
+ );
93
153
  return models;
94
154
  }
95
155
 
@@ -1,8 +1,8 @@
1
- import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
2
1
  import {
3
2
  buildNeuralwattFamily,
4
3
  type NeuralwattModelFamily,
5
4
  type NeuralwattVariantSpec,
5
+ type ProviderChatModelConfig,
6
6
  } from "./build";
7
7
 
8
8
  // Public models returned by https://api.neuralwatt.com/v1/models.
@@ -116,6 +116,62 @@ const KIMI_K2_7_CODE: NeuralwattModelFamily = {
116
116
  },
117
117
  };
118
118
 
119
+ // Xiaomi. MiMo-V2.6-Pro only has two reasoning behaviors: `high` (default)
120
+ // and `none`; the API aliases every other effort onto those, with `minimal`
121
+ // landing on `none`.
122
+ const MIMO_V2_6_PRO: NeuralwattModelFamily = {
123
+ cost: { input: 0.87, output: 1.74, cacheRead: 0.036 },
124
+ vision: true,
125
+ reasoningMetadata: {
126
+ supported_efforts: ["high", "none"],
127
+ mandatory: false,
128
+ effort_aliases: {
129
+ max: "high",
130
+ xhigh: "high",
131
+ medium: "high",
132
+ low: "high",
133
+ minimal: "none",
134
+ },
135
+ },
136
+ };
137
+
138
+ // Neuralwatt tracking alias: the current large model, today Kimi K3. The
139
+ // alias moves to newer models over time, so it snapshots K3's contract
140
+ // instead of sharing the KIMI_K3 family.
141
+ const NW_LARGE: NeuralwattModelFamily = {
142
+ cost: { input: 3, output: 15, cacheRead: 0.3 },
143
+ vision: true,
144
+ reasoningMetadata: {
145
+ supported_efforts: ["max", "high", "low", "none"],
146
+ mandatory: false,
147
+ effort_aliases: { xhigh: "max", medium: "high", minimal: "low" },
148
+ },
149
+ };
150
+
151
+ // Neuralwatt tracking alias: the current cheap, fast model, today DeepSeek
152
+ // V4.1 Flash. Same moving-alias caveat as NW_LARGE.
153
+ const NW_FLASH: NeuralwattModelFamily = {
154
+ cost: { input: 0.15, output: 0.6, cacheRead: 0.015 },
155
+ vision: true,
156
+ reasoningMetadata: {
157
+ supported_efforts: ["max", "xhigh", "high", "low", "none"],
158
+ mandatory: false,
159
+ effort_aliases: { medium: "high", minimal: "low" },
160
+ },
161
+ };
162
+
163
+ // Neuralwatt tracking alias: the current small model, today Qwen3.8-27B.
164
+ // Same moving-alias caveat as NW_LARGE.
165
+ const NW_SMALL: NeuralwattModelFamily = {
166
+ cost: { input: 0.45, output: 3.2, cacheRead: 0.25 },
167
+ vision: true,
168
+ reasoningMetadata: {
169
+ supported_efforts: ["xhigh", "medium", "low", "none"],
170
+ mandatory: false,
171
+ effort_aliases: { max: "xhigh", high: "xhigh", minimal: "low" },
172
+ },
173
+ };
174
+
119
175
  // Qwen. Qwen3.6 35B only advertises `high` and `none`.
120
176
  const QWEN_3_6_35B: NeuralwattModelFamily = {
121
177
  cost: { input: 0.29, output: 1.15, cacheRead: 0.029 },
@@ -192,6 +248,13 @@ const FAMILIES: [NeuralwattModelFamily, NeuralwattVariantSpec[]][] = [
192
248
  reasoning: true,
193
249
  costMultiplier: 0.65,
194
250
  },
251
+ {
252
+ id: "deepseek-v4.1-flash-speed",
253
+ name: "DeepSeek V4.1 Flash (Speed)",
254
+ contextWindow: 1048560,
255
+ maxOutputTokens: 393216,
256
+ reasoning: true,
257
+ },
195
258
  ],
196
259
  ],
197
260
  [
@@ -302,6 +365,86 @@ const FAMILIES: [NeuralwattModelFamily, NeuralwattVariantSpec[]][] = [
302
365
  },
303
366
  ],
304
367
  ],
368
+ [
369
+ MIMO_V2_6_PRO,
370
+ [
371
+ {
372
+ id: "mimo-v2.6-pro",
373
+ name: "MiMo-V2.6-Pro",
374
+ contextWindow: 1048560,
375
+ maxOutputTokens: 393216,
376
+ reasoning: true,
377
+ },
378
+ {
379
+ id: "mimo-v2.6-pro-flex",
380
+ name: "MiMo-V2.6-Pro (flex)",
381
+ contextWindow: 1048560,
382
+ maxOutputTokens: 393216,
383
+ reasoning: true,
384
+ costMultiplier: 0.65,
385
+ },
386
+ ],
387
+ ],
388
+ [
389
+ NW_LARGE,
390
+ [
391
+ {
392
+ id: "nw-large",
393
+ name: "Neuralwatt Large",
394
+ contextWindow: 1048560,
395
+ maxOutputTokens: null,
396
+ reasoning: true,
397
+ },
398
+ {
399
+ id: "nw-large-flex",
400
+ name: "Neuralwatt Large (flex)",
401
+ contextWindow: 1048560,
402
+ maxOutputTokens: null,
403
+ reasoning: true,
404
+ costMultiplier: 0.65,
405
+ },
406
+ ],
407
+ ],
408
+ [
409
+ NW_FLASH,
410
+ [
411
+ {
412
+ id: "nw-flash",
413
+ name: "Neuralwatt Flash",
414
+ contextWindow: 1048560,
415
+ maxOutputTokens: 393216,
416
+ reasoning: true,
417
+ },
418
+ {
419
+ id: "nw-flash-flex",
420
+ name: "Neuralwatt Flash (flex)",
421
+ contextWindow: 1048560,
422
+ maxOutputTokens: 393216,
423
+ reasoning: true,
424
+ costMultiplier: 0.65,
425
+ },
426
+ ],
427
+ ],
428
+ [
429
+ NW_SMALL,
430
+ [
431
+ {
432
+ id: "nw-small",
433
+ name: "Neuralwatt Small",
434
+ contextWindow: 262128,
435
+ maxOutputTokens: 131072,
436
+ reasoning: true,
437
+ },
438
+ {
439
+ id: "nw-small-flex",
440
+ name: "Neuralwatt Small (flex)",
441
+ contextWindow: 262128,
442
+ maxOutputTokens: 131072,
443
+ reasoning: true,
444
+ costMultiplier: 0.65,
445
+ },
446
+ ],
447
+ ],
305
448
  [
306
449
  QWEN_3_6_35B,
307
450
  [
@@ -357,6 +500,6 @@ const FAMILIES: [NeuralwattModelFamily, NeuralwattVariantSpec[]][] = [
357
500
  // `costMultiplier: 0.65` per variant.
358
501
  // https://docs.neuralwatt.com/guides/flex-tier.md
359
502
 
360
- export const NEURALWATT_MODELS: ProviderModelConfig[] = FAMILIES.flatMap(
503
+ export const NEURALWATT_MODELS: ProviderChatModelConfig[] = FAMILIES.flatMap(
361
504
  ([family, variants]) => buildNeuralwattFamily(family, variants),
362
505
  );
@@ -1,7 +1,9 @@
1
1
  import type {
2
+ AnyModel,
2
3
  ModelsStoreEntry,
3
4
  RefreshModelsContext,
4
5
  } from "@earendil-works/pi-ai";
6
+ import { configuredApiBaseUrl } from "../../../src/config/loader";
5
7
  import type { NeuralwattApiModel } from "../../../src/types/models-api";
6
8
  import type {
7
9
  buildNeuralwattProviderModels,
@@ -17,7 +19,8 @@ export const MODEL_STORE_TTL_MS = 4 * 60 * 60 * 1000;
17
19
  * key-scoped catalog (preview, grant-gated, private models), so an entry
18
20
  * stamped "public" must not shadow a keyed refresh — and a key-scoped entry
19
21
  * must not be replayed for an anonymous user. Matches the anonymous-key
20
- * convention in src/lib/neuralwatt-api.ts (authHeaders).
22
+ * convention in src/lib/neuralwatt-api.ts (authHeaders). The base URL is part
23
+ * of the key because a gateway can serve a different catalog than upstream.
21
24
  */
22
25
  const CATALOG_SCOPE_VERSION = "v2";
23
26
  type CatalogScope = "public" | "key";
@@ -30,18 +33,15 @@ function catalogScope(apiKey: string | undefined): CatalogScope {
30
33
  : "public";
31
34
  }
32
35
 
33
- function storedCatalogKey(entry: ScopedModelsStoreEntry): string | undefined {
34
- return entry.catalogKey;
36
+ function catalogKey(scope: CatalogScope): string {
37
+ return `${scope} ${CATALOG_SCOPE_VERSION} ${configuredApiBaseUrl()}`;
35
38
  }
36
39
 
37
40
  function catalogKeyMatches(
38
41
  entry: ScopedModelsStoreEntry | undefined,
39
42
  scope: CatalogScope,
40
43
  ): boolean {
41
- return (
42
- entry !== undefined &&
43
- storedCatalogKey(entry) === `${scope} ${CATALOG_SCOPE_VERSION}`
44
- );
44
+ return entry !== undefined && entry.catalogKey === catalogKey(scope);
45
45
  }
46
46
 
47
47
  export type FetchNeuralwattApiModels = (
@@ -49,6 +49,10 @@ export type FetchNeuralwattApiModels = (
49
49
  signal?: AbortSignal,
50
50
  ) => Promise<readonly NeuralwattApiModel[]>;
51
51
 
52
+ function asStoredChatModels(models: readonly AnyModel[]): NeuralwattModel[] {
53
+ return models as unknown as NeuralwattModel[];
54
+ }
55
+
52
56
  function isFreshStoreEntry(
53
57
  entry: Readonly<ModelsStoreEntry> | undefined,
54
58
  ): entry is ModelsStoreEntry {
@@ -82,11 +86,11 @@ export function createNeuralwattRefreshModels(
82
86
  const stored = context.stored as ScopedModelsStoreEntry | undefined;
83
87
  if (!context.allowNetwork) {
84
88
  return stored !== undefined && catalogKeyMatches(stored, scope)
85
- ? buildFromStore(stored.models)
89
+ ? buildFromStore(asStoredChatModels(stored.models))
86
90
  : fallback;
87
91
  }
88
92
  if (!context.force && isUsableStoreEntry(stored, scope)) {
89
- return buildFromStore(stored.models);
93
+ return buildFromStore(asStoredChatModels(stored.models));
90
94
  }
91
95
  const apiModels = await fetchApiModels(apiKey, context.signal);
92
96
  context.signal.throwIfAborted();
@@ -94,7 +98,7 @@ export function createNeuralwattRefreshModels(
94
98
  const entry: ScopedModelsStoreEntry = {
95
99
  models: models as unknown as ModelsStoreEntry["models"],
96
100
  checkedAt: Date.now(),
97
- catalogKey: `${scope} ${CATALOG_SCOPE_VERSION}`,
101
+ catalogKey: catalogKey(scope),
98
102
  };
99
103
  await context.publish({ persist: entry }).catch(() => undefined);
100
104
  context.signal.throwIfAborted();
@@ -2,6 +2,7 @@ import type { Provider } from "@earendil-works/pi-ai";
2
2
  import type { NeuralwattApi } from "../../src/config";
3
3
  import { createAnthropicMessagesApi } from "./api/anthropic-messages";
4
4
  import { createOpenAiCompletionsApi } from "./api/openai-completions";
5
+ import { classify, stampClassifierModels } from "./api/system-one";
5
6
  import type { NeuralwattApiHandler } from "./api/types";
6
7
  import {
7
8
  NEURALWATT_API_KEY_ENV,
@@ -13,6 +14,7 @@ import type { NeuralwattModel } from "./models/catalog";
13
14
  import {
14
15
  buildNeuralwattProviderModelsFromApi,
15
16
  buildNeuralwattProviderModelsFromStore,
17
+ partitionNeuralwattModels,
16
18
  } from "./models/catalog";
17
19
  import {
18
20
  createNeuralwattRefreshModels,
@@ -98,11 +100,22 @@ export function createNeuralwattProvider(
98
100
  if (envKey) {
99
101
  return { auth: { apiKey: envKey }, source: NEURALWATT_API_KEY_ENV };
100
102
  }
101
- return { auth: { apiKey: "" }, source: "anonymous" };
103
+ // pi-ai rejects empty keys before dispatch; "-" is the neutral
104
+ // placeholder that gateways strip or replace server-side.
105
+ return { auth: { apiKey: "-" }, source: "anonymous" };
102
106
  },
103
107
  },
104
108
  },
105
- getModels: () => handler.stampModels(canonicalModels),
109
+ getModels: () =>
110
+ handler.stampModels(partitionNeuralwattModels(canonicalModels).chat),
111
+ getAllModels: () => {
112
+ const { chat, classifiers } = partitionNeuralwattModels(canonicalModels);
113
+ return [
114
+ ...handler.stampModels(chat),
115
+ ...stampClassifierModels(classifiers),
116
+ ];
117
+ },
118
+ classify: (model, context, options) => classify(model, context, options),
106
119
  refreshModels: async (context) => {
107
120
  const models = await refreshCatalog(context);
108
121
  await context.publish({
@@ -1,32 +1,39 @@
1
1
  // Neuralwatt is OpenAI-compatible, but the OpenAI SDK throws on non-2xx
2
2
  // responses before Pi's after_provider_response hook can see the raw headers.
3
- // We wrap the built-in openai-completions streamSimple so 429 rate-limit
4
- // headers can be captured before the SDK turns them into a generic error, while
5
- // still delegating normal streaming behavior to Pi's provider implementation.
3
+ // We inject a per-request `options.fetch` wrapper so 429
4
+ // rate-limit headers can be captured before the SDK turns them into a generic
5
+ // error, and tee successful SSE bodies for live quota comments, while still
6
+ // delegating normal streaming behavior to Pi's provider implementation.
7
+ // `globalThis.fetch` is never touched.
6
8
  //
7
9
  // The SSE tee used for live quota comments is inspired by:
8
10
  // https://github.com/monotykamary/pi-neuralwatt-provider
9
11
 
10
- import {
11
- type AssistantMessageEventStream,
12
- type Context,
13
- createAssistantMessageEventStream,
14
- type Model,
15
- type SimpleStreamOptions,
12
+ import type {
13
+ AssistantMessageEventStream,
14
+ FetchFunction,
15
+ Model,
16
+ SimpleStreamOptions,
17
+ TranscriptContext,
16
18
  } from "@earendil-works/pi-ai";
19
+ import { configuredApiBaseUrl } from "../../src/config/loader";
17
20
  import {
18
21
  type NeuralwattRateLimitInfo,
19
- normalizeNeuralwattRateLimitError,
20
22
  parseRateLimitHeaders,
21
23
  } from "./rate-limit-error";
22
24
  import { readQuotaCommentsFromTee } from "./sse-quotas";
23
25
 
24
26
  export type AnyStreamSimple = (
25
27
  model: Model<string>,
26
- context: Context,
28
+ context: TranscriptContext,
27
29
  options?: SimpleStreamOptions,
28
30
  ) => AssistantMessageEventStream;
29
31
 
32
+ export interface NeuralwattStreamCallbacks {
33
+ onSseQuota: (line: string) => void;
34
+ onRateLimit: (info: NeuralwattRateLimitInfo | undefined) => void;
35
+ }
36
+
30
37
  function headersToRecord(headers: Headers): Record<string, string> {
31
38
  const record: Record<string, string> = {};
32
39
  headers.forEach((value, key) => {
@@ -58,56 +65,34 @@ function isProviderStreamUrl(
58
65
  }
59
66
  }
60
67
 
61
- async function forwardStream(
62
- stream: AssistantMessageEventStream,
63
- outer: AssistantMessageEventStream,
64
- getRateLimitInfo: () => NeuralwattRateLimitInfo | undefined,
65
- restoreFetch: () => void,
66
- ): Promise<void> {
67
- try {
68
- for await (const event of stream) {
69
- const rateLimitInfo = getRateLimitInfo();
70
- if (event.type === "error" && rateLimitInfo) {
71
- outer.push({
72
- ...event,
73
- error: normalizeNeuralwattRateLimitError(event.error, rateLimitInfo),
74
- });
75
- } else {
76
- outer.push(event);
77
- }
78
- }
79
- } finally {
80
- restoreFetch();
81
- outer.end();
82
- }
83
- }
84
-
85
68
  export function wrapNeuralwattStreamSimple(
86
69
  base: AnyStreamSimple,
87
- onSseQuota: (line: string) => void,
70
+ callbacks: NeuralwattStreamCallbacks,
88
71
  ): AnyStreamSimple {
89
72
  return (model, context, options = {}) => {
90
- let rateLimitInfo: NeuralwattRateLimitInfo | undefined;
91
- let sseQuotaTask: Promise<void> | undefined;
92
- const outer = createAssistantMessageEventStream();
93
- const providerOrigin = new URL(
94
- model.baseUrl ?? "https://api.neuralwatt.com/v1",
95
- ).origin;
96
- const originalFetch = globalThis.fetch;
97
- const wrappedFetch: typeof fetch = async (input, init) => {
98
- const response = await originalFetch(input, init);
73
+ const providerOrigin = new URL(model.baseUrl ?? configuredApiBaseUrl())
74
+ .origin;
75
+ const callerFetch = options.fetch;
76
+
77
+ const neuralwattFetch: FetchFunction = async (input, init) => {
78
+ // Chain onto a caller-provided fetch when present; otherwise resolve
79
+ // the global at request time so late host/test overrides still apply.
80
+ const response = await (callerFetch ?? globalThis.fetch)(input, init);
99
81
 
100
82
  if (!isProviderStreamUrl(input, providerOrigin)) return response;
101
83
 
102
- const headers = headersToRecord(response.headers);
103
84
  if (response.status === 429) {
104
- rateLimitInfo = parseRateLimitHeaders(headers);
85
+ callbacks.onRateLimit(
86
+ parseRateLimitHeaders(headersToRecord(response.headers)),
87
+ );
105
88
  return response;
106
89
  }
107
90
 
108
91
  if (response.ok && response.body) {
109
92
  const [sdkBody, quotaBody] = response.body.tee();
110
- sseQuotaTask = readQuotaCommentsFromTee(quotaBody, onSseQuota);
93
+ // Best-effort side channel; failures inside are swallowed by the
94
+ // reader so quota comments can never break the SDK stream.
95
+ void readQuotaCommentsFromTee(quotaBody, callbacks.onSseQuota);
111
96
  return new Response(sdkBody, {
112
97
  headers: response.headers,
113
98
  status: response.status,
@@ -118,22 +103,6 @@ export function wrapNeuralwattStreamSimple(
118
103
  return response;
119
104
  };
120
105
 
121
- globalThis.fetch = wrappedFetch;
122
-
123
- const restoreFetch = () => {
124
- if (globalThis.fetch === wrappedFetch) globalThis.fetch = originalFetch;
125
- sseQuotaTask?.catch(() => {});
126
- };
127
-
128
- const stream = base(model, context, options);
129
- const originalOuterEnd = outer.end.bind(outer);
130
- outer.end = (result?: Parameters<typeof originalOuterEnd>[0]) => {
131
- restoreFetch();
132
- originalOuterEnd(result);
133
- };
134
-
135
- void forwardStream(stream, outer, () => rateLimitInfo, restoreFetch);
136
-
137
- return outer;
106
+ return base(model, context, { ...options, fetch: neuralwattFetch });
138
107
  };
139
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aliou/pi-neuralwatt",
3
- "version": "0.16.2",
3
+ "version": "0.17.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -38,21 +38,22 @@
38
38
  "@aliou/pi-utils-ui": "^0.5.0"
39
39
  },
40
40
  "peerDependencies": {
41
- "@earendil-works/pi-ai": ">=0.84.0",
42
- "@earendil-works/pi-coding-agent": ">=0.84.0",
41
+ "@earendil-works/pi-ai": ">=1.0.0",
42
+ "@earendil-works/pi-coding-agent": ">=1.0.0",
43
43
  "@earendil-works/pi-tui": "*"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@aliou/biome-plugins": "^0.11.0",
47
47
  "@biomejs/biome": "^2.4.15",
48
48
  "@changesets/cli": "^2.27.11",
49
- "@earendil-works/pi-ai": "0.84.0",
50
- "@earendil-works/pi-coding-agent": "0.84.0",
51
- "@earendil-works/pi-tui": "0.84.0",
49
+ "@earendil-works/pi-ai": "1.1.0",
50
+ "@earendil-works/pi-coding-agent": "1.1.0",
51
+ "@earendil-works/pi-tui": "1.1.0",
52
52
  "@types/node": "^25.0.10",
53
53
  "husky": "^9.1.7",
54
54
  "ts-json-schema-generator": "^2.4.0",
55
55
  "tsx": "4.23.12",
56
+ "typebox": "1.3.27",
56
57
  "typescript": "^5.9.3",
57
58
  "vitest": "^4.0.18"
58
59
  },
package/schema.json CHANGED
@@ -76,6 +76,10 @@
76
76
  "api": {
77
77
  "$ref": "#/definitions/NeuralwattApi",
78
78
  "description": "Which API serves model requests."
79
+ },
80
+ "apiBaseUrl": {
81
+ "type": "string",
82
+ "description": "Fallback base URL for Neuralwatt API calls, used only by models that carry no baseUrl of their own (models.json, custom providers, and proxy routing all take precedence) and by the extension's own catalog and quota fetches. Defaults to the direct upstream."
79
83
  }
80
84
  },
81
85
  "additionalProperties": false
@@ -1,5 +1,7 @@
1
1
  import type { ResolvedNeuralwattConfig } from "./types";
2
2
 
3
+ export const NEURALWATT_API_BASE_URL = "https://api.neuralwatt.com/v1";
4
+
3
5
  export const DEFAULT_CONFIG: ResolvedNeuralwattConfig = {
4
6
  quotaCommand: {
5
7
  enabled: true,
@@ -12,5 +14,6 @@ export const DEFAULT_CONFIG: ResolvedNeuralwattConfig = {
12
14
  },
13
15
  provider: {
14
16
  api: "openai-completions",
17
+ apiBaseUrl: NEURALWATT_API_BASE_URL,
15
18
  },
16
19
  };