@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.
- package/CHANGELOG.md +624 -0
- package/README.md +40 -51
- package/dist/provider/index.d.ts +28 -0
- package/dist/provider/index.js +1142 -0
- package/extensions/_shared/config/index.ts +1 -0
- package/{src → extensions/_shared}/config/loader.ts +11 -25
- package/{src → extensions/_shared}/config/migration/02-flat-to-nested-config.ts +1 -1
- package/extensions/command-quotas/command.ts +12 -3
- package/extensions/command-quotas/components/quota-tabs.ts +1 -1
- package/extensions/command-quotas/components/quotas-display.ts +2 -1
- package/extensions/command-quotas/index.ts +15 -5
- package/extensions/provider/commands/settings/api-base-url-editor.ts +2 -6
- package/extensions/provider/commands/settings/index.ts +31 -5
- package/extensions/provider/index.ts +50 -33
- package/extensions/provider/quota-store.ts +2 -1
- package/extensions/quota-warnings/index.ts +4 -1
- package/extensions/shared/truncate.ts +292 -0
- package/package.json +27 -17
- package/schema.json +14 -0
- package/{extensions/provider → src}/api/anthropic-messages.ts +22 -8
- package/{extensions/provider → src}/api/openai-completions.ts +38 -7
- package/{extensions/provider → src}/api/system-one.ts +5 -3
- package/src/config/index.ts +1 -2
- package/src/config/resolve.ts +25 -0
- package/src/config/types.ts +11 -0
- package/{extensions/provider → src}/constants.ts +1 -0
- package/src/lib/neuralwatt-api.ts +15 -7
- package/{extensions/provider → src}/models/build.ts +10 -10
- package/{extensions/provider → src}/models/catalog.ts +1 -1
- package/{extensions/provider → src}/models/public-models.ts +0 -44
- package/{extensions/provider → src}/models/refresh.ts +16 -9
- package/{extensions/provider → src}/provider.ts +81 -46
- package/{extensions/provider → src}/sse-quotas.ts +5 -4
- package/{extensions/provider → src}/stream-simple.ts +3 -3
- /package/{src → extensions/_shared}/config/migration/03-rename-hidden-to-early-access.ts +0 -0
- /package/{src → extensions/_shared}/config/migration/04-remove-sub-bar-integration.ts +0 -0
- /package/{src → extensions/_shared}/config/migration/index.ts +0 -0
- /package/{extensions/provider → src}/api/types.ts +0 -0
- /package/{extensions/provider → src}/models/index.ts +0 -0
- /package/{extensions/provider → src}/rate-limit-error.ts +0 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Truncate text to `maxWidth` display cells, appending `ellipsis` when text
|
|
5
|
+
* overflows. Unlike `truncateToWidth` from `@earendil-works/pi-tui`, this
|
|
6
|
+
* never injects resets (so caller-applied styling survives truncation) and
|
|
7
|
+
* its CSI parser accepts every escape sequence final, so non-SGR sequences
|
|
8
|
+
* neither swallow visible text nor skew the measured width.
|
|
9
|
+
*/
|
|
10
|
+
export function truncateToWidth(
|
|
11
|
+
text: string,
|
|
12
|
+
maxWidth: number,
|
|
13
|
+
ellipsis = "…",
|
|
14
|
+
pad = false,
|
|
15
|
+
): string {
|
|
16
|
+
if (maxWidth <= 0) return "";
|
|
17
|
+
if (text.length === 0) return pad ? " ".repeat(maxWidth) : "";
|
|
18
|
+
|
|
19
|
+
const ellipsisWidth = visibleWidth(ellipsis);
|
|
20
|
+
if (ellipsisWidth >= maxWidth) {
|
|
21
|
+
const textWidth = visibleWidth(text);
|
|
22
|
+
if (textWidth <= maxWidth) {
|
|
23
|
+
return pad ? text + " ".repeat(maxWidth - textWidth) : text;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const clippedEllipsis = truncateFragmentToWidth(ellipsis, maxWidth);
|
|
27
|
+
if (clippedEllipsis.width === 0) {
|
|
28
|
+
return pad ? " ".repeat(maxWidth) : "";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return finalizeTruncatedResult(
|
|
32
|
+
"",
|
|
33
|
+
0,
|
|
34
|
+
clippedEllipsis.text,
|
|
35
|
+
clippedEllipsis.width,
|
|
36
|
+
maxWidth,
|
|
37
|
+
pad,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (isPrintableAscii(text)) {
|
|
42
|
+
if (text.length <= maxWidth) {
|
|
43
|
+
return pad ? text + " ".repeat(maxWidth - text.length) : text;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const targetWidth = maxWidth - ellipsisWidth;
|
|
47
|
+
return finalizeTruncatedResult(
|
|
48
|
+
text.slice(0, targetWidth),
|
|
49
|
+
targetWidth,
|
|
50
|
+
ellipsis,
|
|
51
|
+
ellipsisWidth,
|
|
52
|
+
maxWidth,
|
|
53
|
+
pad,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const targetWidth = maxWidth - ellipsisWidth;
|
|
58
|
+
let result = "";
|
|
59
|
+
let visibleSoFar = 0;
|
|
60
|
+
let keptWidth = 0;
|
|
61
|
+
let keepContiguousPrefix = true;
|
|
62
|
+
let overflowed = false;
|
|
63
|
+
let exhaustedInput = false;
|
|
64
|
+
const hasAnsi = text.includes("\u001b");
|
|
65
|
+
const hasTabs = text.includes("\t");
|
|
66
|
+
|
|
67
|
+
if (!hasAnsi && !hasTabs) {
|
|
68
|
+
for (const { segment } of segmenter.segment(text)) {
|
|
69
|
+
const width = visibleWidth(segment);
|
|
70
|
+
if (keepContiguousPrefix && keptWidth + width <= targetWidth) {
|
|
71
|
+
result += segment;
|
|
72
|
+
keptWidth += width;
|
|
73
|
+
} else {
|
|
74
|
+
keepContiguousPrefix = false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
visibleSoFar += width;
|
|
78
|
+
if (visibleSoFar > maxWidth) {
|
|
79
|
+
overflowed = true;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exhaustedInput = !overflowed;
|
|
84
|
+
} else {
|
|
85
|
+
let index = 0;
|
|
86
|
+
let pendingAnsi = "";
|
|
87
|
+
while (index < text.length) {
|
|
88
|
+
const ansi = readAnsiSequence(text, index);
|
|
89
|
+
if (ansi) {
|
|
90
|
+
pendingAnsi += ansi;
|
|
91
|
+
index += ansi.length;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (text[index] === "\t") {
|
|
96
|
+
if (keepContiguousPrefix && keptWidth + 3 <= targetWidth) {
|
|
97
|
+
if (pendingAnsi) {
|
|
98
|
+
result += pendingAnsi;
|
|
99
|
+
pendingAnsi = "";
|
|
100
|
+
}
|
|
101
|
+
result += "\t";
|
|
102
|
+
keptWidth += 3;
|
|
103
|
+
} else {
|
|
104
|
+
keepContiguousPrefix = false;
|
|
105
|
+
pendingAnsi = "";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
visibleSoFar += 3;
|
|
109
|
+
if (visibleSoFar > maxWidth) {
|
|
110
|
+
overflowed = true;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
index++;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let end = index;
|
|
118
|
+
while (end < text.length && text[end] !== "\t") {
|
|
119
|
+
const nextAnsi = readAnsiSequence(text, end);
|
|
120
|
+
if (nextAnsi) break;
|
|
121
|
+
end++;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
for (const { segment } of segmenter.segment(text.slice(index, end))) {
|
|
125
|
+
const width = visibleWidth(segment);
|
|
126
|
+
if (keepContiguousPrefix && keptWidth + width <= targetWidth) {
|
|
127
|
+
if (pendingAnsi) {
|
|
128
|
+
result += pendingAnsi;
|
|
129
|
+
pendingAnsi = "";
|
|
130
|
+
}
|
|
131
|
+
result += segment;
|
|
132
|
+
keptWidth += width;
|
|
133
|
+
} else {
|
|
134
|
+
keepContiguousPrefix = false;
|
|
135
|
+
pendingAnsi = "";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
visibleSoFar += width;
|
|
139
|
+
if (visibleSoFar > maxWidth) {
|
|
140
|
+
overflowed = true;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (overflowed) break;
|
|
146
|
+
index = end;
|
|
147
|
+
}
|
|
148
|
+
exhaustedInput = index >= text.length;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!overflowed && exhaustedInput) {
|
|
152
|
+
return pad ? text + " ".repeat(Math.max(0, maxWidth - visibleSoFar)) : text;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return finalizeTruncatedResult(
|
|
156
|
+
result,
|
|
157
|
+
keptWidth,
|
|
158
|
+
ellipsis,
|
|
159
|
+
ellipsisWidth,
|
|
160
|
+
maxWidth,
|
|
161
|
+
pad,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
166
|
+
|
|
167
|
+
function isPrintableAscii(text: string): boolean {
|
|
168
|
+
for (let index = 0; index < text.length; index++) {
|
|
169
|
+
const code = text.charCodeAt(index);
|
|
170
|
+
if (code < 0x20 || code > 0x7e) return false;
|
|
171
|
+
}
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function truncateFragmentToWidth(
|
|
176
|
+
text: string,
|
|
177
|
+
maxWidth: number,
|
|
178
|
+
): { text: string; width: number } {
|
|
179
|
+
if (maxWidth <= 0 || text.length === 0) {
|
|
180
|
+
return { text: "", width: 0 };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (isPrintableAscii(text)) {
|
|
184
|
+
const clipped = text.slice(0, maxWidth);
|
|
185
|
+
return { text: clipped, width: clipped.length };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const hasAnsi = text.includes("\u001b");
|
|
189
|
+
const hasTabs = text.includes("\t");
|
|
190
|
+
if (!hasAnsi && !hasTabs) {
|
|
191
|
+
let result = "";
|
|
192
|
+
let width = 0;
|
|
193
|
+
|
|
194
|
+
for (const { segment } of segmenter.segment(text)) {
|
|
195
|
+
const segmentWidth = visibleWidth(segment);
|
|
196
|
+
if (width + segmentWidth > maxWidth) break;
|
|
197
|
+
result += segment;
|
|
198
|
+
width += segmentWidth;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return { text: result, width };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let result = "";
|
|
205
|
+
let width = 0;
|
|
206
|
+
let index = 0;
|
|
207
|
+
let pendingAnsi = "";
|
|
208
|
+
|
|
209
|
+
while (index < text.length) {
|
|
210
|
+
const ansi = readAnsiSequence(text, index);
|
|
211
|
+
if (ansi) {
|
|
212
|
+
pendingAnsi += ansi;
|
|
213
|
+
index += ansi.length;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (text[index] === "\t") {
|
|
218
|
+
if (width + 3 > maxWidth) break;
|
|
219
|
+
if (pendingAnsi) {
|
|
220
|
+
result += pendingAnsi;
|
|
221
|
+
pendingAnsi = "";
|
|
222
|
+
}
|
|
223
|
+
result += "\t";
|
|
224
|
+
width += 3;
|
|
225
|
+
index++;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
let end = index;
|
|
230
|
+
while (end < text.length && text[end] !== "\t") {
|
|
231
|
+
const nextAnsi = readAnsiSequence(text, end);
|
|
232
|
+
if (nextAnsi) break;
|
|
233
|
+
end++;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
for (const { segment } of segmenter.segment(text.slice(index, end))) {
|
|
237
|
+
const segmentWidth = visibleWidth(segment);
|
|
238
|
+
if (width + segmentWidth > maxWidth) {
|
|
239
|
+
return { text: result, width };
|
|
240
|
+
}
|
|
241
|
+
if (pendingAnsi) {
|
|
242
|
+
result += pendingAnsi;
|
|
243
|
+
pendingAnsi = "";
|
|
244
|
+
}
|
|
245
|
+
result += segment;
|
|
246
|
+
width += segmentWidth;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
index = end;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return { text: result, width };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function finalizeTruncatedResult(
|
|
256
|
+
prefix: string,
|
|
257
|
+
prefixWidth: number,
|
|
258
|
+
ellipsis: string,
|
|
259
|
+
ellipsisWidth: number,
|
|
260
|
+
maxWidth: number,
|
|
261
|
+
pad: boolean,
|
|
262
|
+
): string {
|
|
263
|
+
const width = prefixWidth + ellipsisWidth;
|
|
264
|
+
const result = ellipsis.length > 0 ? `${prefix}${ellipsis}` : prefix;
|
|
265
|
+
return pad ? result + " ".repeat(Math.max(0, maxWidth - width)) : result;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function readAnsiSequence(text: string, index: number): string | null {
|
|
269
|
+
const ESCAPE = "\u001b";
|
|
270
|
+
if (text[index] !== ESCAPE) return null;
|
|
271
|
+
|
|
272
|
+
const next = text[index + 1];
|
|
273
|
+
if (next === "[") {
|
|
274
|
+
const code = text.slice(index);
|
|
275
|
+
const pattern = new RegExp(`^${ESCAPE}\\[[0-?]*[ -/]*[@-~]`, "u");
|
|
276
|
+
const match = code.match(pattern);
|
|
277
|
+
return match?.[0] ?? text[index];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (next === "]" || next === "_") {
|
|
281
|
+
const rest = text.slice(index);
|
|
282
|
+
const bel = rest.indexOf("\u0007");
|
|
283
|
+
const st = rest.indexOf("\u001b\\");
|
|
284
|
+
const end = [bel, st]
|
|
285
|
+
.filter((value) => value >= 0)
|
|
286
|
+
.sort((a, b) => a - b)[0];
|
|
287
|
+
if (end === undefined) return text[index];
|
|
288
|
+
return rest.slice(0, end + (end === st ? 2 : 1));
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return text.slice(index, Math.min(index + 2, text.length));
|
|
292
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aliou/pi-neuralwatt",
|
|
3
|
-
"version": "0.
|
|
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,42 +34,42 @@
|
|
|
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"
|
|
34
43
|
],
|
|
35
44
|
"dependencies": {
|
|
36
|
-
"@aliou/pi-utils-settings": "^0.
|
|
45
|
+
"@aliou/pi-utils-settings": "^0.24.0",
|
|
37
46
|
"@aliou/pi-utils-ui": "^0.5.0"
|
|
38
47
|
},
|
|
39
48
|
"peerDependencies": {
|
|
40
|
-
"@earendil-works/pi-ai": "
|
|
41
|
-
"@earendil-works/pi-coding-agent": ">=1.
|
|
42
|
-
"@earendil-works/pi-tui": "*"
|
|
49
|
+
"@earendil-works/pi-ai": "1.5.0",
|
|
50
|
+
"@earendil-works/pi-coding-agent": ">=1.4.0",
|
|
51
|
+
"@earendil-works/pi-tui": "*",
|
|
52
|
+
"typebox": "*"
|
|
43
53
|
},
|
|
44
54
|
"devDependencies": {
|
|
45
|
-
"@aliou/biome-plugins": "^0.
|
|
46
|
-
"@biomejs/biome": "^2.4
|
|
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.
|
|
49
|
-
"@earendil-works/pi-coding-agent": "1.
|
|
50
|
-
"@earendil-works/pi-tui": "1.
|
|
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
|
-
"pnpm": {
|
|
60
|
-
"overrides": {
|
|
61
|
-
"@earendil-works/pi-ai": "$@earendil-works/pi-coding-agent",
|
|
62
|
-
"@earendil-works/pi-tui": "$@earendil-works/pi-coding-agent"
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
70
|
"scripts": {
|
|
71
|
+
"build": "tsdown",
|
|
72
|
+
"prepack": "pnpm build",
|
|
66
73
|
"typecheck": "tsc --noEmit",
|
|
67
74
|
"lint": "biome check",
|
|
68
75
|
"format": "biome check --write",
|
|
@@ -78,7 +85,7 @@
|
|
|
78
85
|
"version": "changeset version",
|
|
79
86
|
"release": "pnpm changeset publish"
|
|
80
87
|
},
|
|
81
|
-
"packageManager": "pnpm@
|
|
88
|
+
"packageManager": "pnpm@11.27.0",
|
|
82
89
|
"peerDependenciesMeta": {
|
|
83
90
|
"@earendil-works/pi-ai": {
|
|
84
91
|
"optional": true
|
|
@@ -88,6 +95,9 @@
|
|
|
88
95
|
},
|
|
89
96
|
"@earendil-works/pi-tui": {
|
|
90
97
|
"optional": true
|
|
98
|
+
},
|
|
99
|
+
"typebox": {
|
|
100
|
+
"optional": true
|
|
91
101
|
}
|
|
92
102
|
}
|
|
93
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 ??
|
|
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(
|
|
116
|
+
requireUpstream().stream(
|
|
117
|
+
model,
|
|
118
|
+
context,
|
|
119
|
+
withReasoning(streamOptions) as never,
|
|
120
|
+
),
|
|
107
121
|
streamSimple: (model, context, simpleOptions) =>
|
|
108
|
-
(
|
|
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 ??
|
|
97
|
+
baseUrl: model.baseUrl ?? fallbackBaseUrl,
|
|
71
98
|
headers: NEURALWATT_REQUEST_HEADERS,
|
|
72
99
|
};
|
|
73
100
|
}),
|
|
74
101
|
stream: (model, context, streamOptions) =>
|
|
75
|
-
stream(
|
|
102
|
+
requireUpstream().stream(
|
|
103
|
+
model,
|
|
104
|
+
context,
|
|
105
|
+
withOptOutHeader(withReasoningReplay(streamOptions)) as never,
|
|
106
|
+
),
|
|
76
107
|
streamSimple: (model, context, simpleOptions) =>
|
|
77
|
-
(
|
|
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
|
|
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 ??
|
|
241
|
+
baseUrl: model.baseUrl ?? baseUrl,
|
|
240
242
|
headers: NEURALWATT_REQUEST_HEADERS,
|
|
241
243
|
}));
|
|
242
244
|
}
|
package/src/config/index.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/config/types.ts
CHANGED
|
@@ -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;
|