@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
@@ -1,9 +1,13 @@
1
- import type { Provider } from "@earendil-works/pi-ai";
2
- import type { NeuralwattApi } from "../../src/config";
1
+ import type {
2
+ Provider,
3
+ ProviderAuth,
4
+ ProviderStreams,
5
+ } from "@earendil-works/pi-ai";
3
6
  import { createAnthropicMessagesApi } from "./api/anthropic-messages";
4
7
  import { createOpenAiCompletionsApi } from "./api/openai-completions";
5
8
  import { classify, stampClassifierModels } from "./api/system-one";
6
9
  import type { NeuralwattApiHandler } from "./api/types";
10
+ import type { NeuralwattApi } from "./config";
7
11
  import {
8
12
  NEURALWATT_API_KEY_ENV,
9
13
  NEURALWATT_BASE_URL,
@@ -24,27 +28,87 @@ import type { AnyStreamSimple } from "./stream-simple";
24
28
 
25
29
  export { NEURALWATT_API_KEY_ENV, NEURALWATT_BASE_URL, NEURALWATT_PROVIDER_ID };
26
30
 
31
+ export function createNeuralwattApiKeyAuth(): ProviderAuth {
32
+ return {
33
+ apiKey: {
34
+ name: "Neuralwatt API key",
35
+ login: async (interaction) => ({
36
+ type: "api_key",
37
+ key: await interaction.prompt({
38
+ type: "secret",
39
+ message: "Enter Neuralwatt API key",
40
+ }),
41
+ }),
42
+ check: async ({ ctx, credential }) => {
43
+ if (credential?.type === "api_key" && credential.key) {
44
+ return { type: "api_key", source: "stored credential" };
45
+ }
46
+ if (await ctx.env(NEURALWATT_API_KEY_ENV)) {
47
+ return { type: "api_key", source: NEURALWATT_API_KEY_ENV };
48
+ }
49
+ return { type: "api_key", source: "anonymous" };
50
+ },
51
+ resolve: async ({ ctx, credential, signal }) => {
52
+ signal.throwIfAborted();
53
+ if (credential?.type === "api_key" && credential.key) {
54
+ return {
55
+ auth: { apiKey: credential.key },
56
+ env: credential.env,
57
+ source: "stored credential",
58
+ };
59
+ }
60
+ const envKey = await ctx.env(NEURALWATT_API_KEY_ENV);
61
+ signal.throwIfAborted();
62
+ if (envKey) {
63
+ return { auth: { apiKey: envKey }, source: NEURALWATT_API_KEY_ENV };
64
+ }
65
+ // pi-ai rejects empty keys before dispatch; "-" is the neutral
66
+ // placeholder that gateways strip or replace server-side.
67
+ return { auth: { apiKey: "-" }, source: "anonymous" };
68
+ },
69
+ },
70
+ };
71
+ }
72
+
27
73
  export interface NeuralwattProviderOptions {
28
74
  /** Active API surface; resolved once. Changes need a `/reload`. */
29
75
  api?: NeuralwattApi;
76
+ baseUrl?: string | (() => string);
77
+ openAiUpstream?: ProviderStreams;
78
+ messagesUpstream?: ProviderStreams;
79
+ toolsOptOut?: () => boolean;
30
80
  openAiStreamSimple?: AnyStreamSimple;
31
81
  messagesStreamSimple?: AnyStreamSimple;
32
82
  }
33
83
 
34
- function createApiHandler(
84
+ export function createApiHandler(
35
85
  api: NeuralwattApi,
36
86
  options?: NeuralwattProviderOptions,
37
87
  ): NeuralwattApiHandler {
88
+ const baseUrl = resolveBaseUrl(options);
38
89
  if (api === "anthropic-messages") {
39
90
  return createAnthropicMessagesApi({
91
+ upstream: options?.messagesUpstream,
92
+ baseUrl,
40
93
  streamSimple: options?.messagesStreamSimple,
41
94
  });
42
95
  }
43
96
  return createOpenAiCompletionsApi({
97
+ upstream: options?.openAiUpstream,
98
+ baseUrl,
99
+ toolsOptOut: options?.toolsOptOut,
44
100
  streamSimple: options?.openAiStreamSimple,
45
101
  });
46
102
  }
47
103
 
104
+ function resolveBaseUrl(options?: NeuralwattProviderOptions): string {
105
+ const value =
106
+ typeof options?.baseUrl === "function"
107
+ ? options.baseUrl()
108
+ : options?.baseUrl;
109
+ return value ?? NEURALWATT_BASE_URL;
110
+ }
111
+
48
112
  export function createNeuralwattProvider(
49
113
  staticModels: NeuralwattModel[],
50
114
  fetchApiModels: FetchNeuralwattApiModels,
@@ -60,59 +124,30 @@ export function createNeuralwattProvider(
60
124
  fetchApiModels,
61
125
  buildNeuralwattProviderModelsFromApi,
62
126
  buildNeuralwattProviderModelsFromStore,
127
+ () => resolveBaseUrl(options),
63
128
  );
64
129
 
65
130
  return {
66
131
  id: NEURALWATT_PROVIDER_ID,
67
132
  name: "Neuralwatt",
68
- baseUrl: NEURALWATT_BASE_URL,
69
- headers: NEURALWATT_REQUEST_HEADERS,
70
- auth: {
71
- apiKey: {
72
- name: "Neuralwatt API key",
73
- login: async (interaction) => ({
74
- type: "api_key",
75
- key: await interaction.prompt({
76
- type: "secret",
77
- message: "Enter Neuralwatt API key",
78
- }),
79
- }),
80
- check: async ({ ctx, credential }) => {
81
- if (credential?.type === "api_key" && credential.key) {
82
- return { type: "api_key", source: "stored credential" };
83
- }
84
- if (await ctx.env(NEURALWATT_API_KEY_ENV)) {
85
- return { type: "api_key", source: NEURALWATT_API_KEY_ENV };
86
- }
87
- return { type: "api_key", source: "anonymous" };
88
- },
89
- resolve: async ({ ctx, credential, signal }) => {
90
- signal.throwIfAborted();
91
- if (credential?.type === "api_key" && credential.key) {
92
- return {
93
- auth: { apiKey: credential.key },
94
- env: credential.env,
95
- source: "stored credential",
96
- };
97
- }
98
- const envKey = await ctx.env(NEURALWATT_API_KEY_ENV);
99
- signal.throwIfAborted();
100
- if (envKey) {
101
- return { auth: { apiKey: envKey }, source: NEURALWATT_API_KEY_ENV };
102
- }
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" };
106
- },
107
- },
133
+ get baseUrl() {
134
+ return resolveBaseUrl(options);
108
135
  },
136
+ headers: NEURALWATT_REQUEST_HEADERS,
137
+ auth: createNeuralwattApiKeyAuth(),
109
138
  getModels: () =>
110
- handler.stampModels(partitionNeuralwattModels(canonicalModels).chat),
139
+ createApiHandler(
140
+ options?.api ?? "openai-completions",
141
+ options,
142
+ ).stampModels(partitionNeuralwattModels(canonicalModels).chat),
111
143
  getAllModels: () => {
112
144
  const { chat, classifiers } = partitionNeuralwattModels(canonicalModels);
113
145
  return [
114
- ...handler.stampModels(chat),
115
- ...stampClassifierModels(classifiers),
146
+ ...createApiHandler(
147
+ options?.api ?? "openai-completions",
148
+ options,
149
+ ).stampModels(chat),
150
+ ...stampClassifierModels(classifiers, resolveBaseUrl(options)),
116
151
  ];
117
152
  },
118
153
  classify: (model, context, options) => classify(model, context, options),
@@ -1,4 +1,4 @@
1
- import type { NeuralwattQuotas } from "../../src/types/quota-api";
1
+ import type { NeuralwattQuotas } from "./types/quota-api";
2
2
 
3
3
  const JOULES_PER_KWH = 3_600_000;
4
4
 
@@ -85,13 +85,14 @@ export async function readQuotaCommentsFromTee(
85
85
  const final = decoder.decode(new Uint8Array(0), { stream: false });
86
86
  const remaining = (buffer + final).trim();
87
87
  if (remaining) onComment(remaining);
88
- } catch {
88
+ } catch (_error) {
89
89
  // The SDK side may abort the tee; quota comments are best-effort.
90
+ void _error;
90
91
  } finally {
91
92
  try {
92
93
  reader.releaseLock();
93
- } catch {
94
- // ignore
94
+ } catch (_error) {
95
+ void _error; // Intentionally ignored - the lock may already be released.
95
96
  }
96
97
  }
97
98
  }
@@ -16,7 +16,7 @@ import type {
16
16
  SimpleStreamOptions,
17
17
  TranscriptContext,
18
18
  } from "@earendil-works/pi-ai";
19
- import { configuredApiBaseUrl } from "../../src/config/loader";
19
+ import { NEURALWATT_BASE_URL } from "./constants";
20
20
  import {
21
21
  type NeuralwattRateLimitInfo,
22
22
  parseRateLimitHeaders,
@@ -68,10 +68,10 @@ function isProviderStreamUrl(
68
68
  export function wrapNeuralwattStreamSimple(
69
69
  base: AnyStreamSimple,
70
70
  callbacks: NeuralwattStreamCallbacks,
71
+ fallbackBaseUrl: string = NEURALWATT_BASE_URL,
71
72
  ): AnyStreamSimple {
72
73
  return (model, context, options = {}) => {
73
- const providerOrigin = new URL(model.baseUrl ?? configuredApiBaseUrl())
74
- .origin;
74
+ const providerOrigin = new URL(model.baseUrl ?? fallbackBaseUrl).origin;
75
75
  const callerFetch = options.fetch;
76
76
 
77
77
  const neuralwattFetch: FetchFunction = async (input, init) => {
File without changes
File without changes