pendra 0.15.0 → 0.15.2
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.js +97 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +97 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -343,6 +343,20 @@ function unsealedReplyError() {
|
|
|
343
343
|
"private_inference: refusing an unencrypted reply to an encrypted request (it could have been forged in transit)"
|
|
344
344
|
);
|
|
345
345
|
}
|
|
346
|
+
var STREAM_TERMINATOR_KEY = "pendra_stream_terminator";
|
|
347
|
+
function isStreamTerminatorDeclaration(plaintext) {
|
|
348
|
+
try {
|
|
349
|
+
const obj = JSON.parse(plaintext);
|
|
350
|
+
return obj !== null && typeof obj === "object" && !Array.isArray(obj) && Object.keys(obj).length === 1 && obj[STREAM_TERMINATOR_KEY] === 1;
|
|
351
|
+
} catch {
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function truncatedStreamError() {
|
|
356
|
+
return new Error(
|
|
357
|
+
"private_inference: the stream ended before the worker's sealed end-of-stream marker; the response may have been truncated in transit"
|
|
358
|
+
);
|
|
359
|
+
}
|
|
346
360
|
var ResponseDecryptor = class {
|
|
347
361
|
constructor(responseKeys) {
|
|
348
362
|
this.dek = null;
|
|
@@ -890,6 +904,10 @@ var EncryptedStream = class {
|
|
|
890
904
|
this.decoder = new TextDecoder();
|
|
891
905
|
this.sse = new SseDecoder();
|
|
892
906
|
this.done = false;
|
|
907
|
+
this.aborted = false;
|
|
908
|
+
this.openedAny = false;
|
|
909
|
+
this.declared = false;
|
|
910
|
+
this.terminated = false;
|
|
893
911
|
if (!response.body) {
|
|
894
912
|
throw new Error("Response body is null \u2014 streaming not supported");
|
|
895
913
|
}
|
|
@@ -901,13 +919,18 @@ var EncryptedStream = class {
|
|
|
901
919
|
async *[Symbol.asyncIterator]() {
|
|
902
920
|
try {
|
|
903
921
|
while (!this.done) {
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
this.idleTimeoutMs
|
|
907
|
-
)
|
|
922
|
+
let read;
|
|
923
|
+
try {
|
|
924
|
+
read = await readWithIdleTimeout(this.reader, this.idleTimeoutMs);
|
|
925
|
+
} catch (e) {
|
|
926
|
+
if (this.terminated) return;
|
|
927
|
+
throw e;
|
|
928
|
+
}
|
|
929
|
+
const { value, done: readerDone } = read;
|
|
908
930
|
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
909
931
|
for (const data of payloads) {
|
|
910
932
|
if (data === "[DONE]") {
|
|
933
|
+
this.checkEnded();
|
|
911
934
|
this.done = true;
|
|
912
935
|
return;
|
|
913
936
|
}
|
|
@@ -915,6 +938,11 @@ var EncryptedStream = class {
|
|
|
915
938
|
try {
|
|
916
939
|
frame = JSON.parse(data);
|
|
917
940
|
} catch {
|
|
941
|
+
if (this.aborted) continue;
|
|
942
|
+
if (readerDone) {
|
|
943
|
+
if (this.terminated) continue;
|
|
944
|
+
this.checkEnded();
|
|
945
|
+
}
|
|
918
946
|
throw new APIResponseValidationError("Stream frame was not valid JSON", {
|
|
919
947
|
body: data
|
|
920
948
|
});
|
|
@@ -922,24 +950,39 @@ var EncryptedStream = class {
|
|
|
922
950
|
const inner = frame?.pendra_encrypted;
|
|
923
951
|
if (!inner) {
|
|
924
952
|
if (isApiTimingChunk(frame)) {
|
|
953
|
+
this.checkEnded();
|
|
925
954
|
yield parseStreamFrame(data);
|
|
926
955
|
continue;
|
|
927
956
|
}
|
|
928
957
|
parseStreamFrame(data);
|
|
929
958
|
throw unsealedReplyError();
|
|
930
959
|
}
|
|
960
|
+
if (this.terminated) {
|
|
961
|
+
throw new Error(
|
|
962
|
+
"private_inference: refusing a frame after the worker's sealed end of stream"
|
|
963
|
+
);
|
|
964
|
+
}
|
|
931
965
|
const plain = this.decryptor.openChunk(inner);
|
|
932
966
|
const chunkJson = DECODER2.decode(plain).trim();
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
}
|
|
967
|
+
const first = !this.openedAny;
|
|
968
|
+
this.openedAny = true;
|
|
969
|
+
if (first && isStreamTerminatorDeclaration(chunkJson)) {
|
|
970
|
+
this.declared = true;
|
|
938
971
|
continue;
|
|
939
972
|
}
|
|
973
|
+
if (chunkJson === "[DONE]") {
|
|
974
|
+
if (this.declared) {
|
|
975
|
+
this.terminated = true;
|
|
976
|
+
continue;
|
|
977
|
+
}
|
|
978
|
+
this.done = true;
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
981
|
+
if (!chunkJson) continue;
|
|
940
982
|
yield parseStreamFrame(chunkJson, "Decrypted stream frame");
|
|
941
983
|
}
|
|
942
984
|
if (readerDone) {
|
|
985
|
+
this.checkEnded();
|
|
943
986
|
this.done = true;
|
|
944
987
|
break;
|
|
945
988
|
}
|
|
@@ -948,7 +991,18 @@ var EncryptedStream = class {
|
|
|
948
991
|
releaseReader(this.reader, this.controller, this.done);
|
|
949
992
|
}
|
|
950
993
|
}
|
|
994
|
+
/**
|
|
995
|
+
* The server ended the stream: it must have carried a sealed frame, and after
|
|
996
|
+
* a declaration, it must have ended on the sealed `[DONE]`.
|
|
997
|
+
*/
|
|
998
|
+
checkEnded() {
|
|
999
|
+
if (this.aborted) return;
|
|
1000
|
+
if (!this.openedAny || this.declared && !this.terminated) {
|
|
1001
|
+
throw truncatedStreamError();
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
951
1004
|
abort() {
|
|
1005
|
+
this.aborted = true;
|
|
952
1006
|
this.done = true;
|
|
953
1007
|
this.controller.abort();
|
|
954
1008
|
}
|
|
@@ -991,7 +1045,10 @@ var Completions = class {
|
|
|
991
1045
|
model: params.model,
|
|
992
1046
|
stream,
|
|
993
1047
|
taskType: "chat",
|
|
994
|
-
|
|
1048
|
+
// The terminator request rides inside the sealed body, so the relay can
|
|
1049
|
+
// neither strip nor add it: it asks the worker to seal the end of the
|
|
1050
|
+
// stream (#2157).
|
|
1051
|
+
body: ENCODER2.encode(JSON.stringify(stream ? { ...body, [STREAM_TERMINATOR_KEY]: 1 } : body)),
|
|
995
1052
|
messages: params.messages
|
|
996
1053
|
});
|
|
997
1054
|
if (stream) {
|
|
@@ -1274,7 +1331,7 @@ var Rerank = class {
|
|
|
1274
1331
|
};
|
|
1275
1332
|
|
|
1276
1333
|
// src/version.ts
|
|
1277
|
-
var VERSION = "0.15.
|
|
1334
|
+
var VERSION = "0.15.2";
|
|
1278
1335
|
var USER_AGENT = `pendra-node/${VERSION}`;
|
|
1279
1336
|
|
|
1280
1337
|
// src/_privateInferenceEngine.ts
|
|
@@ -1284,6 +1341,25 @@ var PrivateInferenceError = class extends Error {
|
|
|
1284
1341
|
this.name = "PrivateInferenceError";
|
|
1285
1342
|
}
|
|
1286
1343
|
};
|
|
1344
|
+
var FINGERPRINT_RE = /^[0-9a-f]{64}$/;
|
|
1345
|
+
function parsePinnedFingerprints(pin) {
|
|
1346
|
+
if (typeof pin !== "string") {
|
|
1347
|
+
throw new PrivateInferenceError(`pinnedFingerprint must be a string, got ${typeof pin}.`);
|
|
1348
|
+
}
|
|
1349
|
+
const fps = pin.split(",").map((f) => f.trim().toLowerCase()).filter((f) => f.length > 0);
|
|
1350
|
+
if (fps.length === 0) {
|
|
1351
|
+
throw new PrivateInferenceError(
|
|
1352
|
+
"pinnedFingerprint was set but contains no fingerprint. Pass the 64-character fingerprint shown in the console, or leave it unset to use automatic trust."
|
|
1353
|
+
);
|
|
1354
|
+
}
|
|
1355
|
+
const bad = fps.filter((f) => !FINGERPRINT_RE.test(f)).sort();
|
|
1356
|
+
if (bad.length > 0) {
|
|
1357
|
+
throw new PrivateInferenceError(
|
|
1358
|
+
`pinnedFingerprint "${bad[0]}" is not a valid fingerprint: expected 64 hexadecimal characters, as shown in the console.`
|
|
1359
|
+
);
|
|
1360
|
+
}
|
|
1361
|
+
return new Set(fps);
|
|
1362
|
+
}
|
|
1287
1363
|
function defaultTofuPath() {
|
|
1288
1364
|
const env = typeof process !== "undefined" ? process.env : void 0;
|
|
1289
1365
|
if (env?.PENDRA_TOFU_PATH) return env.PENDRA_TOFU_PATH;
|
|
@@ -1302,8 +1378,8 @@ var PrivateInferenceEngine = class {
|
|
|
1302
1378
|
this.orgPubs = null;
|
|
1303
1379
|
this.baseUrl = opts.baseUrl.replace(/\/$/, "");
|
|
1304
1380
|
this.apiKey = opts.apiKey;
|
|
1305
|
-
const
|
|
1306
|
-
this.pinnedFingerprints =
|
|
1381
|
+
const pin = opts.pinnedFingerprint;
|
|
1382
|
+
this.pinnedFingerprints = pin === null || pin === void 0 ? null : parsePinnedFingerprints(pin);
|
|
1307
1383
|
this.affinitySalt = opts.affinitySalt ?? null;
|
|
1308
1384
|
this.tofuPath = opts.tofuPath || defaultTofuPath();
|
|
1309
1385
|
}
|
|
@@ -1499,12 +1575,19 @@ var Pendra = class {
|
|
|
1499
1575
|
}
|
|
1500
1576
|
let pinnedFingerprint;
|
|
1501
1577
|
if (typeof piOpt === "object" && piOpt !== null) {
|
|
1578
|
+
for (const key of Object.keys(piOpt)) {
|
|
1579
|
+
if (key !== "pinnedFingerprint") {
|
|
1580
|
+
throw new TypeError(
|
|
1581
|
+
`Unknown privateInference option "${key}". Accepted options: pinnedFingerprint.`
|
|
1582
|
+
);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1502
1585
|
this.privateInferenceEnabled = true;
|
|
1503
1586
|
pinnedFingerprint = piOpt.pinnedFingerprint;
|
|
1504
1587
|
} else {
|
|
1505
1588
|
this.privateInferenceEnabled = Boolean(piOpt);
|
|
1506
1589
|
}
|
|
1507
|
-
pinnedFingerprint = pinnedFingerprint ?? env?.PENDRA_PINNED_ORG_FINGERPRINT
|
|
1590
|
+
pinnedFingerprint = pinnedFingerprint ?? (env?.PENDRA_PINNED_ORG_FINGERPRINT || void 0);
|
|
1508
1591
|
if (this.privateInferenceEnabled) {
|
|
1509
1592
|
const affinitySalt = env?.PENDRA_AFFINITY_SALT ? new TextEncoder().encode(env.PENDRA_AFFINITY_SALT) : null;
|
|
1510
1593
|
this._piEngine = new PrivateInferenceEngine({
|