pendra 0.12.0 → 0.13.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/dist/index.d.mts +88 -1
- package/dist/index.d.ts +88 -1
- package/dist/index.js +186 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +184 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -9
package/dist/index.mjs
CHANGED
|
@@ -31,6 +31,33 @@ var RateLimitError = class extends APIStatusError {
|
|
|
31
31
|
this.name = "RateLimitError";
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
|
+
var _APIResponseValidationError = class _APIResponseValidationError extends APIError {
|
|
35
|
+
constructor(message, options = {}) {
|
|
36
|
+
const { status, body } = options;
|
|
37
|
+
let text = message;
|
|
38
|
+
if (body) {
|
|
39
|
+
const max = _APIResponseValidationError.BODY_EXCERPT_CHARS;
|
|
40
|
+
text = `${message} (body: ${body.slice(0, max)}${body.length > max ? "\u2026" : ""})`;
|
|
41
|
+
}
|
|
42
|
+
super(text, status);
|
|
43
|
+
this.name = "APIResponseValidationError";
|
|
44
|
+
this.body = body;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/** How much of the offending body to keep in the message. */
|
|
48
|
+
_APIResponseValidationError.BODY_EXCERPT_CHARS = 200;
|
|
49
|
+
var APIResponseValidationError = _APIResponseValidationError;
|
|
50
|
+
var APIStreamError = class extends APIError {
|
|
51
|
+
constructor(message, options = {}) {
|
|
52
|
+
super(message, typeof options.code === "number" ? options.code : void 0);
|
|
53
|
+
this.name = "APIStreamError";
|
|
54
|
+
this.type = options.type;
|
|
55
|
+
this.code = options.code;
|
|
56
|
+
this.requestId = options.requestId;
|
|
57
|
+
this.workerId = options.workerId;
|
|
58
|
+
this.workerName = options.workerName;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
34
61
|
|
|
35
62
|
// src/resources/audio/transcriptions.ts
|
|
36
63
|
var MAX_FILE_SIZE = 25 * 1024 * 1024;
|
|
@@ -160,6 +187,89 @@ var Audio = class {
|
|
|
160
187
|
}
|
|
161
188
|
};
|
|
162
189
|
|
|
190
|
+
// src/_parsing.ts
|
|
191
|
+
function asString(v) {
|
|
192
|
+
return typeof v === "string" ? v : void 0;
|
|
193
|
+
}
|
|
194
|
+
function throwIfInBandError(parsed) {
|
|
195
|
+
if (typeof parsed !== "object" || parsed === null) return;
|
|
196
|
+
const err = parsed.error;
|
|
197
|
+
if (typeof err !== "object" || err === null || Array.isArray(err)) return;
|
|
198
|
+
const e = err;
|
|
199
|
+
throw new APIStreamError(
|
|
200
|
+
asString(e.message) ?? "The server reported an error mid-stream",
|
|
201
|
+
{
|
|
202
|
+
type: asString(e.type),
|
|
203
|
+
code: typeof e.code === "number" ? e.code : void 0,
|
|
204
|
+
requestId: asString(e.request_id),
|
|
205
|
+
workerId: asString(e.worker_id),
|
|
206
|
+
workerName: asString(e.worker_name)
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
function isChunkShaped(parsed) {
|
|
211
|
+
if (typeof parsed !== "object" || parsed === null) return false;
|
|
212
|
+
const c = parsed;
|
|
213
|
+
return typeof c.id === "string" && typeof c.model === "string" && Array.isArray(c.choices);
|
|
214
|
+
}
|
|
215
|
+
function parseStreamFrame(data, label = "Stream frame") {
|
|
216
|
+
let parsed;
|
|
217
|
+
try {
|
|
218
|
+
parsed = JSON.parse(data);
|
|
219
|
+
} catch {
|
|
220
|
+
throw new APIResponseValidationError(`${label} was not valid JSON`, { body: data });
|
|
221
|
+
}
|
|
222
|
+
throwIfInBandError(parsed);
|
|
223
|
+
if (!isChunkShaped(parsed)) {
|
|
224
|
+
throw new APIResponseValidationError(`${label} was not a chat completion chunk`, {
|
|
225
|
+
body: data
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
return parsed;
|
|
229
|
+
}
|
|
230
|
+
function completionFrom(parsed, body, status) {
|
|
231
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
232
|
+
throw new APIResponseValidationError("Response was not a chat completion", { status, body });
|
|
233
|
+
}
|
|
234
|
+
const c = parsed;
|
|
235
|
+
if (typeof c.id !== "string" || typeof c.model !== "string" || !Array.isArray(c.choices)) {
|
|
236
|
+
throw new APIResponseValidationError("Response was not a chat completion", { status, body });
|
|
237
|
+
}
|
|
238
|
+
return parsed;
|
|
239
|
+
}
|
|
240
|
+
async function parseCompletion(response) {
|
|
241
|
+
const body = await response.text();
|
|
242
|
+
let parsed;
|
|
243
|
+
try {
|
|
244
|
+
parsed = JSON.parse(body);
|
|
245
|
+
} catch {
|
|
246
|
+
throw new APIResponseValidationError("Response was not valid JSON", {
|
|
247
|
+
status: response.status,
|
|
248
|
+
body
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return completionFrom(parsed, body, response.status);
|
|
252
|
+
}
|
|
253
|
+
function parseDecryptedCompletion(json, status) {
|
|
254
|
+
let parsed;
|
|
255
|
+
try {
|
|
256
|
+
parsed = JSON.parse(json);
|
|
257
|
+
} catch {
|
|
258
|
+
throw new APIResponseValidationError("Decrypted response was not valid JSON", {
|
|
259
|
+
status,
|
|
260
|
+
body: json
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
return completionFrom(parsed, json, status);
|
|
265
|
+
} catch {
|
|
266
|
+
throw new APIResponseValidationError("Decrypted response was not a chat completion", {
|
|
267
|
+
status,
|
|
268
|
+
body: json
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
163
273
|
// src/_sse.ts
|
|
164
274
|
var DEFAULT_EVENT = "message";
|
|
165
275
|
function fieldValue(line, name) {
|
|
@@ -249,10 +359,7 @@ var Stream = class {
|
|
|
249
359
|
this.done = true;
|
|
250
360
|
return;
|
|
251
361
|
}
|
|
252
|
-
|
|
253
|
-
yield JSON.parse(data);
|
|
254
|
-
} catch {
|
|
255
|
-
}
|
|
362
|
+
yield parseStreamFrame(data);
|
|
256
363
|
}
|
|
257
364
|
if (readerDone) {
|
|
258
365
|
this.done = true;
|
|
@@ -293,15 +400,16 @@ var EncryptedStream = class {
|
|
|
293
400
|
this.done = true;
|
|
294
401
|
return;
|
|
295
402
|
}
|
|
296
|
-
let
|
|
403
|
+
let inner;
|
|
297
404
|
try {
|
|
298
|
-
|
|
405
|
+
inner = JSON.parse(data).pendra_encrypted;
|
|
299
406
|
} catch {
|
|
300
|
-
|
|
407
|
+
throw new APIResponseValidationError("Stream frame was not valid JSON", {
|
|
408
|
+
body: data
|
|
409
|
+
});
|
|
301
410
|
}
|
|
302
|
-
const inner = parsed.pendra_encrypted;
|
|
303
411
|
if (!inner) {
|
|
304
|
-
yield
|
|
412
|
+
yield parseStreamFrame(data);
|
|
305
413
|
continue;
|
|
306
414
|
}
|
|
307
415
|
const plain = this.decryptor.openChunk(inner);
|
|
@@ -313,10 +421,7 @@ var EncryptedStream = class {
|
|
|
313
421
|
}
|
|
314
422
|
continue;
|
|
315
423
|
}
|
|
316
|
-
|
|
317
|
-
yield JSON.parse(chunkJson);
|
|
318
|
-
} catch {
|
|
319
|
-
}
|
|
424
|
+
yield parseStreamFrame(chunkJson, "Decrypted stream frame");
|
|
320
425
|
}
|
|
321
426
|
if (readerDone) {
|
|
322
427
|
this.done = true;
|
|
@@ -361,7 +466,7 @@ var Completions = class {
|
|
|
361
466
|
"/api/v1/chat/completions",
|
|
362
467
|
{ method: "POST", body, headers }
|
|
363
468
|
);
|
|
364
|
-
return
|
|
469
|
+
return parseCompletion(response);
|
|
365
470
|
}
|
|
366
471
|
async createEncrypted(params, body, headers) {
|
|
367
472
|
const engine = await this.client._privateEngine();
|
|
@@ -384,12 +489,21 @@ var Completions = class {
|
|
|
384
489
|
"/api/v1/chat/completions",
|
|
385
490
|
{ method: "POST", body: topLevel, headers }
|
|
386
491
|
);
|
|
387
|
-
const
|
|
492
|
+
const raw = await response.text();
|
|
493
|
+
let wrapper;
|
|
494
|
+
try {
|
|
495
|
+
wrapper = JSON.parse(raw);
|
|
496
|
+
} catch {
|
|
497
|
+
throw new APIResponseValidationError("Response was not valid JSON", {
|
|
498
|
+
status: response.status,
|
|
499
|
+
body: raw
|
|
500
|
+
});
|
|
501
|
+
}
|
|
388
502
|
if (!wrapper.pendra_encrypted) {
|
|
389
|
-
return
|
|
503
|
+
return parseDecryptedCompletion(raw, response.status);
|
|
390
504
|
}
|
|
391
505
|
const plain = decryptor.openResult(wrapper.pendra_encrypted);
|
|
392
|
-
return
|
|
506
|
+
return parseDecryptedCompletion(DECODER2.decode(plain), response.status);
|
|
393
507
|
}
|
|
394
508
|
};
|
|
395
509
|
|
|
@@ -622,6 +736,10 @@ var Rerank = class {
|
|
|
622
736
|
}
|
|
623
737
|
};
|
|
624
738
|
|
|
739
|
+
// src/version.ts
|
|
740
|
+
var VERSION = "0.13.0";
|
|
741
|
+
var USER_AGENT = `pendra-node/${VERSION}`;
|
|
742
|
+
|
|
625
743
|
// src/_hpke.ts
|
|
626
744
|
import { x25519 } from "@noble/curves/ed25519.js";
|
|
627
745
|
import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
@@ -1015,6 +1133,27 @@ var PrivateInferenceEngine = class {
|
|
|
1015
1133
|
const digest = sha256Hex(new TextEncoder().encode(this.apiKey)).slice(0, 16);
|
|
1016
1134
|
return `${this.baseUrl}|${digest}`;
|
|
1017
1135
|
}
|
|
1136
|
+
/**
|
|
1137
|
+
* Drop the cached key set so the next call re-fetches it.
|
|
1138
|
+
*
|
|
1139
|
+
* The cache is otherwise held for the whole process lifetime, which is
|
|
1140
|
+
* correct while a fleet keeps one key but fatal when it re-keys: a
|
|
1141
|
+
* long-running server would go on sealing to a fingerprint no worker holds
|
|
1142
|
+
* and fail EVERY request until someone restarted it. The API signals a re-key
|
|
1143
|
+
* with `X-Pendra-Error-Code: no_private_inference_worker`, and this is what
|
|
1144
|
+
* lets the client recover on its own.
|
|
1145
|
+
*
|
|
1146
|
+
* Pinned clients are deliberately NOT re-fetched on that signal — a pin is an
|
|
1147
|
+
* allowlist, so a fetch could only return fingerprints the pin already
|
|
1148
|
+
* rejects. They surface the error instead, which is what pinning means.
|
|
1149
|
+
*/
|
|
1150
|
+
invalidateOrgKey() {
|
|
1151
|
+
this.orgPubs = null;
|
|
1152
|
+
}
|
|
1153
|
+
/** Whether this client seals only to an explicit fingerprint allowlist. */
|
|
1154
|
+
get isPinned() {
|
|
1155
|
+
return this.pinnedFingerprints !== null;
|
|
1156
|
+
}
|
|
1018
1157
|
/**
|
|
1019
1158
|
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
1020
1159
|
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
@@ -1027,7 +1166,7 @@ var PrivateInferenceEngine = class {
|
|
|
1027
1166
|
if (this.orgPubs !== null) return this.orgPubs;
|
|
1028
1167
|
if (!fetchedKeys || fetchedKeys.length === 0) {
|
|
1029
1168
|
throw new PrivateInferenceError(
|
|
1030
|
-
"No
|
|
1169
|
+
"No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
|
|
1031
1170
|
);
|
|
1032
1171
|
}
|
|
1033
1172
|
const pubs = [];
|
|
@@ -1053,7 +1192,7 @@ var PrivateInferenceEngine = class {
|
|
|
1053
1192
|
if (accepted.length === 0) {
|
|
1054
1193
|
const pinned = Array.from(this.pinnedFingerprints).sort().join(", ");
|
|
1055
1194
|
throw new PrivateInferenceError(
|
|
1056
|
-
`None of the org's workload key fingerprints (${fps.join(", ")}) match your pinned fingerprint(s) (${pinned}). Refusing to seal (possible man-in-the-middle).
|
|
1195
|
+
`None of the org's workload key fingerprints (${fps.join(", ")}) match your pinned fingerprint(s) (${pinned}). Refusing to seal (possible man-in-the-middle). If your fleet re-keyed \u2014 which happens when every worker goes offline at once \u2014 update the pin to a current fingerprint from the console.`
|
|
1057
1196
|
);
|
|
1058
1197
|
}
|
|
1059
1198
|
} else {
|
|
@@ -1227,7 +1366,7 @@ var Pendra = class {
|
|
|
1227
1366
|
try {
|
|
1228
1367
|
response = await fetch(url, {
|
|
1229
1368
|
method: "GET",
|
|
1230
|
-
headers: { Authorization: `Bearer ${this.apiKey}
|
|
1369
|
+
headers: { Authorization: `Bearer ${this.apiKey}`, "User-Agent": USER_AGENT },
|
|
1231
1370
|
signal: controller.signal
|
|
1232
1371
|
});
|
|
1233
1372
|
} catch (err) {
|
|
@@ -1242,7 +1381,7 @@ var Pendra = class {
|
|
|
1242
1381
|
}
|
|
1243
1382
|
if (response.status === 404) {
|
|
1244
1383
|
throw new PrivateInferenceError(
|
|
1245
|
-
"No
|
|
1384
|
+
"No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
|
|
1246
1385
|
);
|
|
1247
1386
|
}
|
|
1248
1387
|
if (!response.ok) {
|
|
@@ -1267,6 +1406,7 @@ var Pendra = class {
|
|
|
1267
1406
|
const headers = {
|
|
1268
1407
|
Authorization: `Bearer ${this.apiKey}`,
|
|
1269
1408
|
"Content-Type": "application/json",
|
|
1409
|
+
"User-Agent": USER_AGENT,
|
|
1270
1410
|
...options.headers ?? {}
|
|
1271
1411
|
};
|
|
1272
1412
|
let response;
|
|
@@ -1293,6 +1433,7 @@ var Pendra = class {
|
|
|
1293
1433
|
}
|
|
1294
1434
|
if (!response.ok) {
|
|
1295
1435
|
clearTimeout(timeoutId);
|
|
1436
|
+
this.invalidateKeyCacheIfRekeyed(response);
|
|
1296
1437
|
let message;
|
|
1297
1438
|
try {
|
|
1298
1439
|
const body = await response.json();
|
|
@@ -1306,6 +1447,26 @@ var Pendra = class {
|
|
|
1306
1447
|
}
|
|
1307
1448
|
return { response, controller };
|
|
1308
1449
|
}
|
|
1450
|
+
/**
|
|
1451
|
+
* Drop the cached encryption keys when the API reports that no worker holds
|
|
1452
|
+
* the one this request was sealed to — the signal that the org's fleet has
|
|
1453
|
+
* re-keyed. Returns true if the cache was dropped.
|
|
1454
|
+
*
|
|
1455
|
+
* A PINNED client is left alone on purpose: a pin is an allowlist, so
|
|
1456
|
+
* re-fetching could only return fingerprints the pin already rejects and
|
|
1457
|
+
* would loop rather than recover. The error surfaces instead, which is
|
|
1458
|
+
* exactly what pinning is for.
|
|
1459
|
+
*/
|
|
1460
|
+
invalidateKeyCacheIfRekeyed(response) {
|
|
1461
|
+
const engine = this._piEngine;
|
|
1462
|
+
if (!engine || engine.isPinned) return false;
|
|
1463
|
+
if (response.headers?.get("X-Pendra-Error-Code") !== "no_private_inference_worker") {
|
|
1464
|
+
return false;
|
|
1465
|
+
}
|
|
1466
|
+
engine.invalidateOrgKey();
|
|
1467
|
+
this._piKeyFetch = null;
|
|
1468
|
+
return true;
|
|
1469
|
+
}
|
|
1309
1470
|
};
|
|
1310
1471
|
|
|
1311
1472
|
// src/index.ts
|
|
@@ -1313,7 +1474,9 @@ var index_default = Pendra;
|
|
|
1313
1474
|
export {
|
|
1314
1475
|
APIConnectionError,
|
|
1315
1476
|
APIError,
|
|
1477
|
+
APIResponseValidationError,
|
|
1316
1478
|
APIStatusError,
|
|
1479
|
+
APIStreamError,
|
|
1317
1480
|
AuthenticationError,
|
|
1318
1481
|
Pendra,
|
|
1319
1482
|
PrivateInferenceError,
|