pendra 0.6.1 → 0.9.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 +60 -9
- package/dist/index.d.ts +60 -9
- package/dist/index.js +185 -84
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +179 -78
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -159,11 +159,78 @@ var Audio = class {
|
|
|
159
159
|
}
|
|
160
160
|
};
|
|
161
161
|
|
|
162
|
+
// src/_sse.ts
|
|
163
|
+
var DEFAULT_EVENT = "message";
|
|
164
|
+
function fieldValue(line, name) {
|
|
165
|
+
const value = line.slice(name.length + 1);
|
|
166
|
+
return value.startsWith(" ") ? value.slice(1) : value;
|
|
167
|
+
}
|
|
168
|
+
var SseDecoder = class {
|
|
169
|
+
constructor() {
|
|
170
|
+
this.buffer = "";
|
|
171
|
+
this.event = "";
|
|
172
|
+
this.data = [];
|
|
173
|
+
}
|
|
174
|
+
/** Feed a chunk of decoded text; returns the payloads it completed. */
|
|
175
|
+
push(text) {
|
|
176
|
+
this.buffer += text;
|
|
177
|
+
const lines = this.buffer.split("\n");
|
|
178
|
+
this.buffer = lines.pop() ?? "";
|
|
179
|
+
const out = [];
|
|
180
|
+
for (const line of lines) {
|
|
181
|
+
const payload = this.pushLine(line);
|
|
182
|
+
if (payload !== null) out.push(payload);
|
|
183
|
+
}
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Release what the stream ended without terminating: the held-back partial
|
|
188
|
+
* line, plus any frame that never got its blank line.
|
|
189
|
+
*
|
|
190
|
+
* The spec discards an unterminated final block; we keep it, so a connection
|
|
191
|
+
* that drops after the last `data:` line but before its blank line still
|
|
192
|
+
* delivers that payload rather than silently losing it.
|
|
193
|
+
*/
|
|
194
|
+
flush() {
|
|
195
|
+
const out = [];
|
|
196
|
+
if (this.buffer !== "") {
|
|
197
|
+
const payload = this.pushLine(this.buffer);
|
|
198
|
+
this.buffer = "";
|
|
199
|
+
if (payload !== null) out.push(payload);
|
|
200
|
+
}
|
|
201
|
+
const tail = this.endFrame();
|
|
202
|
+
if (tail !== null) out.push(tail);
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
pushLine(rawLine) {
|
|
206
|
+
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
|
|
207
|
+
if (line === "") return this.endFrame();
|
|
208
|
+
if (line.startsWith(":")) return null;
|
|
209
|
+
if (line.startsWith("event:")) {
|
|
210
|
+
this.event = fieldValue(line, "event");
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
if (line.startsWith("data:")) {
|
|
214
|
+
this.data.push(fieldValue(line, "data"));
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
endFrame() {
|
|
220
|
+
const { data, event } = this;
|
|
221
|
+
this.data = [];
|
|
222
|
+
this.event = "";
|
|
223
|
+
if (data.length === 0) return null;
|
|
224
|
+
if (event !== "" && event !== DEFAULT_EVENT) return null;
|
|
225
|
+
return data.join("\n");
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
162
229
|
// src/streaming.ts
|
|
163
230
|
var Stream = class {
|
|
164
231
|
constructor(response, controller) {
|
|
165
232
|
this.decoder = new TextDecoder();
|
|
166
|
-
this.
|
|
233
|
+
this.sse = new SseDecoder();
|
|
167
234
|
this.done = false;
|
|
168
235
|
if (!response.body) {
|
|
169
236
|
throw new Error("Response body is null \u2014 streaming not supported");
|
|
@@ -175,18 +242,8 @@ var Stream = class {
|
|
|
175
242
|
try {
|
|
176
243
|
while (!this.done) {
|
|
177
244
|
const { value, done: readerDone } = await this.reader.read();
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
this.buffer += this.decoder.decode(value, { stream: true });
|
|
183
|
-
const lines = this.buffer.split("\n");
|
|
184
|
-
this.buffer = lines.pop() ?? "";
|
|
185
|
-
for (const line of lines) {
|
|
186
|
-
const trimmed = line.trim();
|
|
187
|
-
if (!trimmed || trimmed.startsWith(":")) continue;
|
|
188
|
-
if (!trimmed.startsWith("data: ")) continue;
|
|
189
|
-
const data = trimmed.slice(6);
|
|
245
|
+
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
246
|
+
for (const data of payloads) {
|
|
190
247
|
if (data === "[DONE]") {
|
|
191
248
|
this.done = true;
|
|
192
249
|
return;
|
|
@@ -196,6 +253,10 @@ var Stream = class {
|
|
|
196
253
|
} catch {
|
|
197
254
|
}
|
|
198
255
|
}
|
|
256
|
+
if (readerDone) {
|
|
257
|
+
this.done = true;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
199
260
|
}
|
|
200
261
|
} finally {
|
|
201
262
|
this.reader.releaseLock();
|
|
@@ -211,7 +272,8 @@ var Stream = class {
|
|
|
211
272
|
var DECODER = new TextDecoder();
|
|
212
273
|
var EncryptedStream = class {
|
|
213
274
|
constructor(response, controller, decryptor) {
|
|
214
|
-
this.
|
|
275
|
+
this.decoder = new TextDecoder();
|
|
276
|
+
this.sse = new SseDecoder();
|
|
215
277
|
this.done = false;
|
|
216
278
|
if (!response.body) {
|
|
217
279
|
throw new Error("Response body is null \u2014 streaming not supported");
|
|
@@ -224,18 +286,8 @@ var EncryptedStream = class {
|
|
|
224
286
|
try {
|
|
225
287
|
while (!this.done) {
|
|
226
288
|
const { value, done: readerDone } = await this.reader.read();
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
break;
|
|
230
|
-
}
|
|
231
|
-
this.buffer += DECODER.decode(value, { stream: true });
|
|
232
|
-
const lines = this.buffer.split("\n");
|
|
233
|
-
this.buffer = lines.pop() ?? "";
|
|
234
|
-
for (const line of lines) {
|
|
235
|
-
const trimmed = line.trim();
|
|
236
|
-
if (!trimmed || trimmed.startsWith(":") || trimmed.startsWith("event:")) continue;
|
|
237
|
-
if (!trimmed.startsWith("data:")) continue;
|
|
238
|
-
const data = trimmed.slice("data:".length).trimStart();
|
|
289
|
+
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
290
|
+
for (const data of payloads) {
|
|
239
291
|
if (data === "[DONE]") {
|
|
240
292
|
this.done = true;
|
|
241
293
|
return;
|
|
@@ -265,6 +317,10 @@ var EncryptedStream = class {
|
|
|
265
317
|
} catch {
|
|
266
318
|
}
|
|
267
319
|
}
|
|
320
|
+
if (readerDone) {
|
|
321
|
+
this.done = true;
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
268
324
|
}
|
|
269
325
|
} finally {
|
|
270
326
|
this.reader.releaseLock();
|
|
@@ -292,7 +348,19 @@ var Completions = class {
|
|
|
292
348
|
if (params.temperature != null) body.temperature = params.temperature;
|
|
293
349
|
if (params.max_tokens != null) body.max_tokens = params.max_tokens;
|
|
294
350
|
if (params.top_p != null) body.top_p = params.top_p;
|
|
351
|
+
if (params.top_k != null) body.top_k = params.top_k;
|
|
352
|
+
if (params.min_p != null) body.min_p = params.min_p;
|
|
353
|
+
if (params.frequency_penalty != null)
|
|
354
|
+
body.frequency_penalty = params.frequency_penalty;
|
|
355
|
+
if (params.presence_penalty != null)
|
|
356
|
+
body.presence_penalty = params.presence_penalty;
|
|
357
|
+
if (params.repeat_penalty != null) body.repeat_penalty = params.repeat_penalty;
|
|
358
|
+
if (params.seed != null) body.seed = params.seed;
|
|
295
359
|
if (params.stop != null) body.stop = params.stop;
|
|
360
|
+
if (params.tools != null) body.tools = params.tools;
|
|
361
|
+
if (params.tool_choice != null) body.tool_choice = params.tool_choice;
|
|
362
|
+
if (params.parallel_tool_calls != null)
|
|
363
|
+
body.parallel_tool_calls = params.parallel_tool_calls;
|
|
296
364
|
const headers = params.worker_id ? { "X-Pendra-Worker-Id": params.worker_id } : void 0;
|
|
297
365
|
if (this.client.privateInferenceEnabled) {
|
|
298
366
|
return this.createEncrypted(params, body, headers);
|
|
@@ -570,12 +638,12 @@ var Rerank = class {
|
|
|
570
638
|
};
|
|
571
639
|
|
|
572
640
|
// src/_hpke.ts
|
|
573
|
-
import { x25519 } from "@noble/curves/ed25519";
|
|
574
|
-
import { chacha20poly1305 } from "@noble/ciphers/chacha";
|
|
575
|
-
import { sha256 } from "@noble/hashes/
|
|
576
|
-
import { hmac } from "@noble/hashes/hmac";
|
|
577
|
-
import { extract, expand } from "@noble/hashes/hkdf";
|
|
578
|
-
import { randomBytes as nobleRandomBytes } from "@noble/hashes/utils";
|
|
641
|
+
import { x25519 } from "@noble/curves/ed25519.js";
|
|
642
|
+
import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
643
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
644
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
645
|
+
import { extract, expand } from "@noble/hashes/hkdf.js";
|
|
646
|
+
import { randomBytes as nobleRandomBytes } from "@noble/hashes/utils.js";
|
|
579
647
|
var KEM_ID = 32;
|
|
580
648
|
var KDF_ID = 1;
|
|
581
649
|
var AEAD_ID = 3;
|
|
@@ -628,7 +696,7 @@ function extractAndExpand(dh, kemContext) {
|
|
|
628
696
|
return labeledExpand(eaePrk, bytes("shared_secret"), kemContext, NSECRET, KEM_SUITE_ID);
|
|
629
697
|
}
|
|
630
698
|
function encap(pkR, skE) {
|
|
631
|
-
const skEObj = skE ?? x25519.utils.
|
|
699
|
+
const skEObj = skE ?? x25519.utils.randomSecretKey();
|
|
632
700
|
const enc = x25519.getPublicKey(skEObj);
|
|
633
701
|
const dh = x25519.getSharedSecret(skEObj, pkR);
|
|
634
702
|
const kemContext = concat(enc, pkR);
|
|
@@ -689,7 +757,7 @@ function setupBaseR(skR, enc, info) {
|
|
|
689
757
|
return keySchedule(sharedSecret, info);
|
|
690
758
|
}
|
|
691
759
|
function generateX25519Keypair() {
|
|
692
|
-
const priv = x25519.utils.
|
|
760
|
+
const priv = x25519.utils.randomSecretKey();
|
|
693
761
|
return { priv, pub: x25519.getPublicKey(priv) };
|
|
694
762
|
}
|
|
695
763
|
function randomBytes(n) {
|
|
@@ -700,8 +768,8 @@ function sha256Hex(data) {
|
|
|
700
768
|
}
|
|
701
769
|
|
|
702
770
|
// src/_privateInference.ts
|
|
703
|
-
import { chacha20poly1305 as chacha20poly13052 } from "@noble/ciphers/chacha";
|
|
704
|
-
import { extract as extract2, expand as expand2 } from "@noble/hashes/hkdf";
|
|
771
|
+
import { chacha20poly1305 as chacha20poly13052 } from "@noble/ciphers/chacha.js";
|
|
772
|
+
import { extract as extract2, expand as expand2 } from "@noble/hashes/hkdf.js";
|
|
705
773
|
var ENVELOPE_VERSION = 1;
|
|
706
774
|
var TEXT_ENCODER2 = new TextEncoder();
|
|
707
775
|
var TEXT_DECODER = new TextDecoder();
|
|
@@ -765,18 +833,35 @@ function seqNonce(seq) {
|
|
|
765
833
|
}
|
|
766
834
|
return nonce;
|
|
767
835
|
}
|
|
768
|
-
function sealRequest(
|
|
836
|
+
function sealRequest(orgPubs, body, opts) {
|
|
837
|
+
const pubs = Array.isArray(orgPubs) ? orgPubs : [orgPubs];
|
|
838
|
+
if (pubs.length === 0) {
|
|
839
|
+
throw new Error("sealRequest: no org public keys to seal to");
|
|
840
|
+
}
|
|
841
|
+
const seen = /* @__PURE__ */ new Set();
|
|
842
|
+
const uniquePubs = [];
|
|
843
|
+
for (const p of pubs) {
|
|
844
|
+
const fp = fingerprint(p);
|
|
845
|
+
if (!seen.has(fp)) {
|
|
846
|
+
seen.add(fp);
|
|
847
|
+
uniquePubs.push(p);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
769
850
|
const { priv: respPriv, pub: respPub } = generateX25519Keypair();
|
|
770
851
|
const respPkB64 = b64(respPub);
|
|
771
852
|
const dek = randomBytes(32);
|
|
772
853
|
const nonce = randomBytes(12);
|
|
773
854
|
const aad = buildAad(opts.model, opts.stream, opts.taskType, respPkB64);
|
|
774
855
|
const ciphertext = chacha20poly13052(dek, nonce, aad).encrypt(body);
|
|
775
|
-
const
|
|
776
|
-
const
|
|
856
|
+
const recipients = [];
|
|
857
|
+
for (const pub of uniquePubs) {
|
|
858
|
+
const { enc, ctx: sealer } = setupBaseS(pub, SUITE_INFO);
|
|
859
|
+
const sealedDek = sealer.seal(new Uint8Array(0), dek);
|
|
860
|
+
recipients.push({ kid: fingerprint(pub), enc: b64(enc), sealed_dek: b64(sealedDek) });
|
|
861
|
+
}
|
|
777
862
|
const envelope = {
|
|
778
863
|
v: ENVELOPE_VERSION,
|
|
779
|
-
recipients
|
|
864
|
+
recipients,
|
|
780
865
|
nonce: b64(nonce),
|
|
781
866
|
ciphertext: b64(ciphertext),
|
|
782
867
|
response_pk: respPkB64
|
|
@@ -926,72 +1011,88 @@ function b64d2(s) {
|
|
|
926
1011
|
}
|
|
927
1012
|
var PrivateInferenceEngine = class {
|
|
928
1013
|
constructor(opts) {
|
|
929
|
-
this.
|
|
1014
|
+
this.orgPubs = null;
|
|
930
1015
|
this.baseUrl = opts.baseUrl.replace(/\/$/, "");
|
|
931
1016
|
this.apiKey = opts.apiKey;
|
|
932
|
-
const
|
|
933
|
-
this.
|
|
1017
|
+
const fps = (opts.pinnedFingerprint || "").split(",").map((f) => f.trim().toLowerCase()).filter((f) => f.length > 0);
|
|
1018
|
+
this.pinnedFingerprints = fps.length > 0 ? new Set(fps) : null;
|
|
934
1019
|
this.affinitySalt = opts.affinitySalt ?? null;
|
|
935
1020
|
this.tofuPath = opts.tofuPath || defaultTofuPath();
|
|
936
1021
|
}
|
|
937
1022
|
// --- key resolution ---
|
|
1023
|
+
cachedOrgPublicKeys() {
|
|
1024
|
+
return this.orgPubs;
|
|
1025
|
+
}
|
|
938
1026
|
cachedOrgPublicKey() {
|
|
939
|
-
return this.
|
|
1027
|
+
return this.orgPubs && this.orgPubs.length > 0 ? this.orgPubs[0] : null;
|
|
940
1028
|
}
|
|
941
1029
|
tofuIdentity() {
|
|
942
1030
|
const digest = sha256Hex(new TextEncoder().encode(this.apiKey)).slice(0, 16);
|
|
943
1031
|
return `${this.baseUrl}|${digest}`;
|
|
944
1032
|
}
|
|
945
1033
|
/**
|
|
946
|
-
* Validate the key(s) returned by GET /v1/private-inference/key
|
|
947
|
-
*
|
|
948
|
-
*
|
|
1034
|
+
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
1035
|
+
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
1036
|
+
* them (newest-seen first). The client seals to **all** of the returned keys
|
|
1037
|
+
* so any worker holding any of them can serve the request. Throws
|
|
1038
|
+
* PrivateInferenceError on absence, a malformed key, or (when pinned) an empty
|
|
1039
|
+
* intersection with the pin set.
|
|
949
1040
|
*/
|
|
950
1041
|
async resolveOrgKey(fetchedKeys) {
|
|
951
|
-
if (this.
|
|
1042
|
+
if (this.orgPubs !== null) return this.orgPubs;
|
|
952
1043
|
if (!fetchedKeys || fetchedKeys.length === 0) {
|
|
953
1044
|
throw new PrivateInferenceError(
|
|
954
1045
|
"No private-inference workload key is registered for this organisation. Run `pendra keygen --save` on your worker(s), then reconnect them."
|
|
955
1046
|
);
|
|
956
1047
|
}
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
1048
|
+
const pubs = [];
|
|
1049
|
+
const fps = [];
|
|
1050
|
+
for (const entry of fetchedKeys) {
|
|
1051
|
+
let pub;
|
|
1052
|
+
try {
|
|
1053
|
+
pub = b64d2(entry.public_key);
|
|
1054
|
+
} catch (e) {
|
|
1055
|
+
throw new PrivateInferenceError(`Malformed workload key from the server: ${String(e)}`);
|
|
1056
|
+
}
|
|
1057
|
+
if (pub.length !== 32) {
|
|
1058
|
+
throw new PrivateInferenceError(
|
|
1059
|
+
`Workload key from the server is ${pub.length} bytes; expected a 32-byte X25519 key.`
|
|
1060
|
+
);
|
|
1061
|
+
}
|
|
1062
|
+
pubs.push(pub);
|
|
1063
|
+
fps.push(fingerprint(pub));
|
|
968
1064
|
}
|
|
969
|
-
|
|
970
|
-
if (this.
|
|
971
|
-
|
|
1065
|
+
let accepted;
|
|
1066
|
+
if (this.pinnedFingerprints !== null) {
|
|
1067
|
+
accepted = pubs.filter((_p, i) => this.pinnedFingerprints.has(fps[i]));
|
|
1068
|
+
if (accepted.length === 0) {
|
|
1069
|
+
const pinned = Array.from(this.pinnedFingerprints).sort().join(", ");
|
|
972
1070
|
throw new PrivateInferenceError(
|
|
973
|
-
`
|
|
1071
|
+
`None of the org's workload key fingerprints (${fps.join(", ")}) match your pinned fingerprint(s) (${pinned}). Refusing to seal (possible man-in-the-middle). Verify the fingerprint(s) from \`pendra keygen\` and update the pin if you rotated keys.`
|
|
974
1072
|
);
|
|
975
1073
|
}
|
|
976
1074
|
} else {
|
|
977
|
-
await this.
|
|
1075
|
+
await this.tofuTrust(fps);
|
|
1076
|
+
accepted = pubs;
|
|
978
1077
|
}
|
|
979
|
-
this.
|
|
980
|
-
return
|
|
1078
|
+
this.orgPubs = accepted;
|
|
1079
|
+
return accepted;
|
|
981
1080
|
}
|
|
982
|
-
async
|
|
1081
|
+
async tofuTrust(fps) {
|
|
983
1082
|
const store = await this.loadTofu();
|
|
984
1083
|
const identity = this.tofuIdentity();
|
|
985
1084
|
const known = store[identity];
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1085
|
+
const trusted = /* @__PURE__ */ new Set();
|
|
1086
|
+
if (typeof known === "string") {
|
|
1087
|
+
trusted.add(known);
|
|
1088
|
+
} else if (Array.isArray(known)) {
|
|
1089
|
+
for (const f of known) if (typeof f === "string") trusted.add(f);
|
|
990
1090
|
}
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
);
|
|
1091
|
+
const before = trusted.size;
|
|
1092
|
+
for (const f of fps) trusted.add(f);
|
|
1093
|
+
if (trusted.size !== before || !Array.isArray(known)) {
|
|
1094
|
+
store[identity] = Array.from(trusted).sort();
|
|
1095
|
+
await this.saveTofu(store);
|
|
995
1096
|
}
|
|
996
1097
|
}
|
|
997
1098
|
async loadTofu() {
|
|
@@ -1031,7 +1132,7 @@ var PrivateInferenceEngine = class {
|
|
|
1031
1132
|
* `messages` (chat only) drives the affinity token; pass undefined for non-chat.
|
|
1032
1133
|
*/
|
|
1033
1134
|
seal(args) {
|
|
1034
|
-
if (this.
|
|
1135
|
+
if (this.orgPubs === null || this.orgPubs.length === 0) {
|
|
1035
1136
|
throw new PrivateInferenceError(
|
|
1036
1137
|
"private inference: org key not resolved yet (call resolveOrgKey first)"
|
|
1037
1138
|
);
|
|
@@ -1040,7 +1141,7 @@ var PrivateInferenceEngine = class {
|
|
|
1040
1141
|
if (args.messages != null) {
|
|
1041
1142
|
affinity = affinityKey(this.apiKey, args.model, args.messages, this.affinitySalt);
|
|
1042
1143
|
}
|
|
1043
|
-
const { envelope, responsePriv } = sealRequest(this.
|
|
1144
|
+
const { envelope, responsePriv } = sealRequest(this.orgPubs, args.body, {
|
|
1044
1145
|
model: args.model,
|
|
1045
1146
|
stream: args.stream,
|
|
1046
1147
|
taskType: args.taskType,
|
|
@@ -1120,7 +1221,7 @@ var Pendra = class {
|
|
|
1120
1221
|
if (!this._piEngine) {
|
|
1121
1222
|
throw new PrivateInferenceError("private inference is not enabled on this client");
|
|
1122
1223
|
}
|
|
1123
|
-
if (this._piEngine.
|
|
1224
|
+
if (this._piEngine.cachedOrgPublicKeys() !== null) {
|
|
1124
1225
|
return this._piEngine;
|
|
1125
1226
|
}
|
|
1126
1227
|
if (!this._piKeyFetch) {
|