@aliou/pi-neuralwatt 0.18.0 → 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 +40 -51
  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 +12 -3
  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 +15 -5
  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 +50 -33
  15. package/extensions/provider/quota-store.ts +2 -1
  16. package/extensions/quota-warnings/index.ts +4 -1
  17. package/extensions/shared/truncate.ts +292 -0
  18. package/package.json +27 -17
  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/public-models.ts +0 -44
  31. package/{extensions/provider → src}/models/refresh.ts +16 -9
  32. package/{extensions/provider → src}/provider.ts +81 -46
  33. package/{extensions/provider → src}/sse-quotas.ts +5 -4
  34. package/{extensions/provider → src}/stream-simple.ts +3 -3
  35. /package/{src → extensions/_shared}/config/migration/03-rename-hidden-to-early-access.ts +0 -0
  36. /package/{src → extensions/_shared}/config/migration/04-remove-sub-bar-integration.ts +0 -0
  37. /package/{src → extensions/_shared}/config/migration/index.ts +0 -0
  38. /package/{extensions/provider → src}/api/types.ts +0 -0
  39. /package/{extensions/provider → src}/models/index.ts +0 -0
  40. /package/{extensions/provider → src}/rate-limit-error.ts +0 -0
@@ -0,0 +1 @@
1
+ export { configLoader } from "./loader";
@@ -1,12 +1,12 @@
1
1
  import { buildSchemaUrl, ConfigLoader } from "@aliou/pi-utils-settings";
2
- import packageJson from "../../package.json";
3
- import { DEFAULT_CONFIG } from "./defaults";
4
- import { migrations } from "./migration";
2
+ import packageJson from "../../../package.json";
3
+ import { DEFAULT_CONFIG } from "../../../src/config/defaults";
4
+ import { resolveApi, resolveApiBaseUrl } from "../../../src/config/resolve";
5
5
  import type {
6
- NeuralwattApi,
7
6
  NeuralwattConfig,
8
7
  ResolvedNeuralwattConfig,
9
- } from "./types";
8
+ } from "../../../src/config/types";
9
+ import { migrations } from "./migration";
10
10
 
11
11
  /**
12
12
  * Fill in every field the rest of the code reads. Migrations already normalized
@@ -26,6 +26,7 @@ function normalizeResolvedConfig(
26
26
  enabled:
27
27
  config.quotaWarnings?.enabled ?? DEFAULT_CONFIG.quotaWarnings.enabled,
28
28
  },
29
+ hostedTools: config.hostedTools,
29
30
  provider: {
30
31
  api: resolveApi(config.provider?.api),
31
32
  apiBaseUrl: resolveApiBaseUrl(config.provider?.apiBaseUrl),
@@ -33,34 +34,19 @@ function normalizeResolvedConfig(
33
34
  };
34
35
  }
35
36
 
36
- export function resolveApi(value: string | undefined): NeuralwattApi {
37
- if (value === "anthropic-messages" || value === "openai-completions") {
38
- return value;
39
- }
40
- return DEFAULT_CONFIG.provider.api;
41
- }
42
-
43
- export function resolveApiBaseUrl(value: string | undefined): string {
44
- const trimmed = value?.trim().replace(/\/+$/u, "");
45
- if (!trimmed) {
46
- return DEFAULT_CONFIG.provider.apiBaseUrl;
47
- }
37
+ export function configuredApiBaseUrl(): string {
48
38
  try {
49
- const url = new URL(trimmed);
50
- if (url.protocol !== "https:" && url.protocol !== "http:") {
51
- return DEFAULT_CONFIG.provider.apiBaseUrl;
52
- }
39
+ return configLoader.getConfig().provider.apiBaseUrl;
53
40
  } catch {
54
41
  return DEFAULT_CONFIG.provider.apiBaseUrl;
55
42
  }
56
- return trimmed;
57
43
  }
58
44
 
59
- export function configuredApiBaseUrl(): string {
45
+ export function hostedToolsOptOut(): boolean {
60
46
  try {
61
- return configLoader.getConfig().provider.apiBaseUrl;
47
+ return configLoader.getConfig().hostedTools?.enabled === false;
62
48
  } catch {
63
- return DEFAULT_CONFIG.provider.apiBaseUrl;
49
+ return false;
64
50
  }
65
51
  }
66
52
 
@@ -2,7 +2,7 @@ import { constants } from "node:fs";
2
2
  import { copyFile } from "node:fs/promises";
3
3
  import { basename, dirname, join } from "node:path";
4
4
  import type { Migration } from "@aliou/pi-utils-settings";
5
- import packageJson from "../../../package.json";
5
+ import packageJson from "../../../../package.json";
6
6
 
7
7
  /** The original flat config, replaced by per-feature sections. */
8
8
  interface FlatNeuralwattConfig {
@@ -5,6 +5,7 @@ import {
5
5
  } from "@earendil-works/pi-coding-agent";
6
6
  import { fetchQuotas } from "../../src/lib/neuralwatt-api";
7
7
  import { getNeuralwattApiKey } from "../_shared/auth";
8
+ import { configuredApiBaseUrl } from "../_shared/config/loader";
8
9
  import { QuotasComponent } from "./components/quotas-display";
9
10
 
10
11
  function missingAuthMessage(): string {
@@ -12,10 +13,14 @@ function missingAuthMessage(): string {
12
13
  return `Neuralwatt quota requires an API key. Add credentials to ${authPath} or set the NEURALWATT_API_KEY environment variable.`;
13
14
  }
14
15
 
15
- export function registerQuotasCommand(pi: ExtensionAPI): void {
16
+ export function registerQuotasCommand(
17
+ pi: ExtensionAPI,
18
+ isEnabled: () => boolean,
19
+ ): void {
16
20
  pi.registerCommand("neuralwatt:quota", {
17
21
  description: "Display Neuralwatt API usage and quota",
18
22
  handler: async (_args, ctx) => {
23
+ if (!isEnabled()) return;
19
24
  const apiKey = await getNeuralwattApiKey(ctx.modelRegistry);
20
25
  if (!apiKey) {
21
26
  ctx.ui.notify(missingAuthMessage(), "warning");
@@ -40,7 +45,11 @@ export function registerQuotasCommand(pi: ExtensionAPI): void {
40
45
  );
41
46
 
42
47
  async function loadQuotas(): Promise<void> {
43
- const fetchResult = await fetchQuotas(key, controller.signal);
48
+ const fetchResult = await fetchQuotas(
49
+ key,
50
+ configuredApiBaseUrl(),
51
+ controller.signal,
52
+ );
44
53
  if (controller.signal.aborted) return;
45
54
  if (fetchResult.success) {
46
55
  component.setState({
@@ -71,7 +80,7 @@ export function registerQuotasCommand(pi: ExtensionAPI): void {
71
80
 
72
81
  // Non-interactive fallback (RPC, print, JSON modes)
73
82
  if (result === undefined) {
74
- const fetchResult = await fetchQuotas(key);
83
+ const fetchResult = await fetchQuotas(key, configuredApiBaseUrl());
75
84
  if (!fetchResult.success) {
76
85
  ctx.ui.notify(
77
86
  JSON.stringify({ error: fetchResult.error.message }),
@@ -1,5 +1,4 @@
1
1
  import type { Theme } from "@earendil-works/pi-coding-agent";
2
- import { truncateToWidth } from "@earendil-works/pi-tui";
3
2
  import type { NeuralwattQuotas } from "../../../src/types/quota-api";
4
3
  import {
5
4
  percentCreditsRemaining,
@@ -11,6 +10,7 @@ import {
11
10
  formatTokens,
12
11
  formatUsd,
13
12
  } from "../../../src/utils/quota-format";
13
+ import { truncateToWidth } from "../../shared/truncate";
14
14
  import { renderProgressBar } from "./progress-bar";
15
15
 
16
16
  /**
@@ -1,8 +1,9 @@
1
1
  import type { Theme } from "@earendil-works/pi-coding-agent";
2
2
  import { DynamicBorder } from "@earendil-works/pi-coding-agent";
3
3
  import type { Component, TUI } from "@earendil-works/pi-tui";
4
- import { Loader, matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
4
+ import { Loader, matchesKey } from "@earendil-works/pi-tui";
5
5
  import type { NeuralwattQuotas } from "../../../src/types/quota-api";
6
+ import { truncateToWidth } from "../../shared/truncate";
6
7
  import {
7
8
  renderCreditsTab,
8
9
  renderSubscriptionTab,
@@ -1,19 +1,29 @@
1
+ import { bindProjectConfig } from "@aliou/pi-utils-settings";
1
2
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
- import { configLoader } from "../../src/config";
3
3
  import {
4
+ NEURALWATT_CONFIG_UPDATED_EVENT,
4
5
  NEURALWATT_EXTENSIONS_REGISTER_EVENT,
5
6
  NEURALWATT_EXTENSIONS_REQUEST_EVENT,
7
+ type NeuralwattConfigUpdatedPayload,
6
8
  } from "../../src/events";
9
+ import { configLoader } from "../_shared/config";
7
10
  import { registerQuotasCommand } from "./command";
8
11
 
9
12
  export default async function (pi: ExtensionAPI) {
10
13
  await configLoader.load();
14
+ bindProjectConfig(pi, configLoader);
11
15
 
12
- const config = configLoader.getConfig();
16
+ let enabled = configLoader.getConfig().quotaCommand.enabled;
17
+ registerQuotasCommand(pi, () => enabled);
13
18
 
14
- if (config.quotaCommand.enabled) {
15
- registerQuotasCommand(pi);
16
- }
19
+ pi.on("session_start", () => {
20
+ enabled = configLoader.getConfig().quotaCommand.enabled;
21
+ });
22
+
23
+ pi.events.on(NEURALWATT_CONFIG_UPDATED_EVENT, (data: unknown) => {
24
+ enabled = (data as NeuralwattConfigUpdatedPayload).config.quotaCommand
25
+ .enabled;
26
+ });
17
27
 
18
28
  pi.events.on(NEURALWATT_EXTENSIONS_REQUEST_EVENT, () => {
19
29
  pi.events.emit(NEURALWATT_EXTENSIONS_REGISTER_EVENT, {
@@ -1,10 +1,6 @@
1
1
  import type { Component, SettingsListTheme } from "@earendil-works/pi-tui";
2
- import {
3
- Input,
4
- Key,
5
- matchesKey,
6
- truncateToWidth,
7
- } from "@earendil-works/pi-tui";
2
+ import { Input, Key, matchesKey } from "@earendil-works/pi-tui";
3
+ import { truncateToWidth } from "../../../shared/truncate";
8
4
 
9
5
  export interface ApiBaseUrlEditorOptions {
10
6
  initial: string;
@@ -4,16 +4,16 @@ import {
4
4
  } from "@aliou/pi-utils-settings";
5
5
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
6
6
  import type { SettingItem } from "@earendil-works/pi-tui";
7
- import {
8
- configLoader,
9
- type NeuralwattApi,
10
- type NeuralwattConfig,
11
- type ResolvedNeuralwattConfig,
7
+ import type {
8
+ NeuralwattApi,
9
+ NeuralwattConfig,
10
+ ResolvedNeuralwattConfig,
12
11
  } from "../../../../src/config";
13
12
  import {
14
13
  NEURALWATT_CONFIG_UPDATED_EVENT,
15
14
  type NeuralwattFeatureId,
16
15
  } from "../../../../src/events";
16
+ import { configLoader } from "../../../_shared/config";
17
17
  import { ApiBaseUrlEditor } from "./api-base-url-editor";
18
18
 
19
19
  export interface RegisterNeuralwattSettingsOptions {
@@ -117,6 +117,19 @@ export function registerNeuralwattSettings(
117
117
  });
118
118
  },
119
119
  },
120
+ {
121
+ id: "hostedTools",
122
+ label: "Hosted tools",
123
+ description:
124
+ "Offer Neuralwatt hosted tools on OpenAI-surface requests; disabled sends an opt-out header with every request",
125
+ currentValue: featureValue(
126
+ tabConfig?.hostedTools,
127
+ resolved.hostedTools?.enabled ?? true,
128
+ )
129
+ ? "enabled"
130
+ : "disabled",
131
+ values: ["enabled", "disabled"],
132
+ },
120
133
  ],
121
134
  },
122
135
  {
@@ -161,6 +174,19 @@ export function registerNeuralwattSettings(
161
174
  };
162
175
  }
163
176
 
177
+ if (id === "hostedTools") {
178
+ if (newValue !== "enabled" && newValue !== "disabled") {
179
+ return null;
180
+ }
181
+ const updated = { ...config };
182
+ if (newValue === "disabled") {
183
+ updated.hostedTools = { ...config.hostedTools, enabled: false };
184
+ } else {
185
+ delete updated.hostedTools;
186
+ }
187
+ return updated;
188
+ }
189
+
164
190
  if (!getLoadedFeatures().has(id as NeuralwattFeatureId)) {
165
191
  return null;
166
192
  }
@@ -1,9 +1,12 @@
1
- import { getApiProvider } from "@earendil-works/pi-ai/compat";
1
+ import { bindProjectConfig } from "@aliou/pi-utils-settings";
2
+ import {
3
+ anthropicMessagesApi,
4
+ openAICompletionsApi,
5
+ } from "@earendil-works/pi-ai/compat";
2
6
  import type {
3
7
  ExtensionAPI,
4
8
  ModelRegistry,
5
9
  } from "@earendil-works/pi-coding-agent";
6
- import { configLoader } from "../../src/config";
7
10
  import {
8
11
  NEURALWATT_CONFIG_UPDATED_EVENT,
9
12
  NEURALWATT_EXTENSIONS_REGISTER_EVENT,
@@ -17,23 +20,28 @@ import {
17
20
  fetchNeuralwattModels,
18
21
  fetchQuotas,
19
22
  } from "../../src/lib/neuralwatt-api";
20
- import type { NeuralwattQuotas } from "../../src/types/quota-api";
21
- import { getNeuralwattApiKey } from "../_shared/auth";
22
- import { registerNeuralwattSettings } from "./commands/settings";
23
- import { normalizeNeuralwattContextOverflowError } from "./context-overflow";
24
- import { buildNeuralwattProviderModels } from "./models";
25
- import { createNeuralwattProvider } from "./provider";
26
- import { buildQuotasFromHeaders, fetchRequestedQuotas } from "./quota-store";
23
+ import { buildNeuralwattProviderModels } from "../../src/models";
24
+ import { createNeuralwattProvider } from "../../src/provider";
27
25
  import {
28
26
  type NeuralwattRateLimitInfo,
29
27
  normalizeNeuralwattRateLimitError,
30
28
  parseRateLimitHeaders,
31
- } from "./rate-limit-error";
32
- import { updateQuotasFromSseComment } from "./sse-quotas";
29
+ } from "../../src/rate-limit-error";
30
+ import { updateQuotasFromSseComment } from "../../src/sse-quotas";
33
31
  import {
34
32
  type NeuralwattStreamCallbacks,
35
33
  wrapNeuralwattStreamSimple,
36
- } from "./stream-simple";
34
+ } from "../../src/stream-simple";
35
+ import type { NeuralwattQuotas } from "../../src/types/quota-api";
36
+ import { getNeuralwattApiKey } from "../_shared/auth";
37
+ import { configLoader } from "../_shared/config";
38
+ import {
39
+ configuredApiBaseUrl,
40
+ hostedToolsOptOut,
41
+ } from "../_shared/config/loader";
42
+ import { registerNeuralwattSettings } from "./commands/settings";
43
+ import { normalizeNeuralwattContextOverflowError } from "./context-overflow";
44
+ import { buildQuotasFromHeaders, fetchRequestedQuotas } from "./quota-store";
37
45
 
38
46
  const HEADER_EMIT_THROTTLE_MS = 5_000;
39
47
 
@@ -49,29 +57,26 @@ function registerNeuralwattProvider(
49
57
  ): void {
50
58
  const staticModels = buildNeuralwattProviderModels();
51
59
 
52
- const apiProvider = getApiProvider("openai-completions");
53
- const baseStreamSimple = apiProvider?.streamSimple;
54
- const streamSimple = baseStreamSimple
55
- ? (wrapNeuralwattStreamSimple(
56
- baseStreamSimple as never,
57
- streamCallbacks,
58
- ) as never)
59
- : undefined;
60
-
61
- const messagesApiProvider = getApiProvider("anthropic-messages");
62
- const messagesBaseStreamSimple = messagesApiProvider?.streamSimple;
63
- const messagesStreamSimple = messagesBaseStreamSimple
64
- ? (wrapNeuralwattStreamSimple(
65
- messagesBaseStreamSimple as never,
66
- streamCallbacks,
67
- ) as never)
68
- : undefined;
60
+ const openAiUpstream = openAICompletionsApi();
61
+ const messagesUpstream = anthropicMessagesApi();
62
+ const streamSimple = wrapNeuralwattStreamSimple(
63
+ openAiUpstream.streamSimple as never,
64
+ streamCallbacks,
65
+ ) as never;
66
+ const messagesStreamSimple = wrapNeuralwattStreamSimple(
67
+ messagesUpstream.streamSimple as never,
68
+ streamCallbacks,
69
+ ) as never;
69
70
 
70
71
  pi.registerProvider(
71
72
  createNeuralwattProvider(
72
73
  staticModels,
73
74
  async (apiKey, signal) => {
74
- const result = await fetchNeuralwattModels(apiKey, signal);
75
+ const result = await fetchNeuralwattModels(
76
+ apiKey,
77
+ configuredApiBaseUrl(),
78
+ signal,
79
+ );
75
80
  if (!result.success) {
76
81
  throw new Error("Neuralwatt models API request failed");
77
82
  }
@@ -79,6 +84,10 @@ function registerNeuralwattProvider(
79
84
  },
80
85
  {
81
86
  api: configLoader.getConfig().provider.api,
87
+ baseUrl: configuredApiBaseUrl,
88
+ openAiUpstream,
89
+ messagesUpstream,
90
+ toolsOptOut: hostedToolsOptOut,
82
91
  openAiStreamSimple: streamSimple,
83
92
  messagesStreamSimple,
84
93
  },
@@ -88,6 +97,7 @@ function registerNeuralwattProvider(
88
97
 
89
98
  export default async function (pi: ExtensionAPI) {
90
99
  await configLoader.load();
100
+ bindProjectConfig(pi, configLoader);
91
101
 
92
102
  let latestQuotas: NeuralwattQuotas | undefined;
93
103
 
@@ -109,12 +119,14 @@ export default async function (pi: ExtensionAPI) {
109
119
  // text with actionable details from Neuralwatt's response headers.
110
120
  let pendingRateLimitInfo: NeuralwattRateLimitInfo | undefined;
111
121
 
112
- registerNeuralwattProvider(pi, {
122
+ const streamCallbacks: NeuralwattStreamCallbacks = {
113
123
  onSseQuota: handleSseQuota,
114
124
  onRateLimit: (info) => {
115
125
  pendingRateLimitInfo = info;
116
126
  },
117
- });
127
+ };
128
+ let registeredApi = configLoader.getConfig().provider.api;
129
+ registerNeuralwattProvider(pi, streamCallbacks);
118
130
 
119
131
  const loadedFeatures = new Set<NeuralwattFeatureId>();
120
132
 
@@ -233,6 +245,11 @@ export default async function (pi: ExtensionAPI) {
233
245
  });
234
246
 
235
247
  pi.on("session_start", async (_event, ctx) => {
248
+ const api = configLoader.getConfig().provider.api;
249
+ if (api !== registeredApi) {
250
+ registerNeuralwattProvider(pi, streamCallbacks);
251
+ registeredApi = api;
252
+ }
236
253
  currentModelRegistry = ctx.modelRegistry;
237
254
  pendingRateLimitInfo = undefined;
238
255
  const messages = [...new Set(configLoader.drainMessages())];
@@ -247,7 +264,7 @@ export default async function (pi: ExtensionAPI) {
247
264
  if (ctx.model?.provider !== "neuralwatt") return;
248
265
  const apiKey = await getNeuralwattApiKey(ctx.modelRegistry);
249
266
  if (!apiKey) return;
250
- const quotaResult = await fetchQuotas(apiKey);
267
+ const quotaResult = await fetchQuotas(apiKey, configuredApiBaseUrl());
251
268
  if (quotaResult.success) emitQuotas(quotaResult.data.quotas, "api");
252
269
  });
253
270
 
@@ -1,6 +1,7 @@
1
1
  import { parseQuotaHeaders } from "../../src/events";
2
2
  import { fetchQuotas } from "../../src/lib/neuralwatt-api";
3
3
  import type { NeuralwattQuotas } from "../../src/types/quota-api";
4
+ import { configuredApiBaseUrl } from "../_shared/config/loader";
4
5
 
5
6
  export function buildQuotasFromHeaders(
6
7
  headers: Record<string, string>,
@@ -45,7 +46,7 @@ export async function fetchRequestedQuotas(
45
46
  apiKey: string | undefined,
46
47
  ): Promise<NeuralwattQuotas | undefined> {
47
48
  if (!apiKey) return;
48
- const result = await fetchQuotas(apiKey);
49
+ const result = await fetchQuotas(apiKey, configuredApiBaseUrl());
49
50
  if (!result.success) return;
50
51
  return result.data.quotas;
51
52
  }
@@ -1,8 +1,8 @@
1
+ import { bindProjectConfig } from "@aliou/pi-utils-settings";
1
2
  import type {
2
3
  ExtensionAPI,
3
4
  ExtensionContext,
4
5
  } from "@earendil-works/pi-coding-agent";
5
- import { configLoader } from "../../src/config";
6
6
  import {
7
7
  NEURALWATT_CONFIG_UPDATED_EVENT,
8
8
  NEURALWATT_EXTENSIONS_REGISTER_EVENT,
@@ -11,10 +11,12 @@ import {
11
11
  type NeuralwattConfigUpdatedPayload,
12
12
  type NeuralwattQuotasUpdatedPayload,
13
13
  } from "../../src/events";
14
+ import { configLoader } from "../_shared/config";
14
15
  import { checkQuotas, clearAlertState } from "./notifier";
15
16
 
16
17
  export default async function (pi: ExtensionAPI) {
17
18
  await configLoader.load();
19
+ bindProjectConfig(pi, configLoader);
18
20
 
19
21
  let enabled = configLoader.getConfig().quotaWarnings.enabled;
20
22
  let currentProvider: string | undefined;
@@ -46,6 +48,7 @@ export default async function (pi: ExtensionAPI) {
46
48
  }
47
49
 
48
50
  pi.on("session_start", async (_event, ctx) => {
51
+ enabled = configLoader.getConfig().quotaWarnings.enabled;
49
52
  unsubscribeQuotas?.();
50
53
  currentProvider = ctx.model?.provider;
51
54
  unsubscribeQuotas = pi.events.on(NEURALWATT_QUOTAS_UPDATED_EVENT, (data) =>