pendra 0.6.0 → 0.8.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 +39 -8
- package/dist/index.d.ts +39 -8
- package/dist/index.js +97 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +91 -54
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -293,6 +293,10 @@ var Completions = class {
|
|
|
293
293
|
if (params.max_tokens != null) body.max_tokens = params.max_tokens;
|
|
294
294
|
if (params.top_p != null) body.top_p = params.top_p;
|
|
295
295
|
if (params.stop != null) body.stop = params.stop;
|
|
296
|
+
if (params.tools != null) body.tools = params.tools;
|
|
297
|
+
if (params.tool_choice != null) body.tool_choice = params.tool_choice;
|
|
298
|
+
if (params.parallel_tool_calls != null)
|
|
299
|
+
body.parallel_tool_calls = params.parallel_tool_calls;
|
|
296
300
|
const headers = params.worker_id ? { "X-Pendra-Worker-Id": params.worker_id } : void 0;
|
|
297
301
|
if (this.client.privateInferenceEnabled) {
|
|
298
302
|
return this.createEncrypted(params, body, headers);
|
|
@@ -570,12 +574,12 @@ var Rerank = class {
|
|
|
570
574
|
};
|
|
571
575
|
|
|
572
576
|
// 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";
|
|
577
|
+
import { x25519 } from "@noble/curves/ed25519.js";
|
|
578
|
+
import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
579
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
580
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
581
|
+
import { extract, expand } from "@noble/hashes/hkdf.js";
|
|
582
|
+
import { randomBytes as nobleRandomBytes } from "@noble/hashes/utils.js";
|
|
579
583
|
var KEM_ID = 32;
|
|
580
584
|
var KDF_ID = 1;
|
|
581
585
|
var AEAD_ID = 3;
|
|
@@ -628,7 +632,7 @@ function extractAndExpand(dh, kemContext) {
|
|
|
628
632
|
return labeledExpand(eaePrk, bytes("shared_secret"), kemContext, NSECRET, KEM_SUITE_ID);
|
|
629
633
|
}
|
|
630
634
|
function encap(pkR, skE) {
|
|
631
|
-
const skEObj = skE ?? x25519.utils.
|
|
635
|
+
const skEObj = skE ?? x25519.utils.randomSecretKey();
|
|
632
636
|
const enc = x25519.getPublicKey(skEObj);
|
|
633
637
|
const dh = x25519.getSharedSecret(skEObj, pkR);
|
|
634
638
|
const kemContext = concat(enc, pkR);
|
|
@@ -689,7 +693,7 @@ function setupBaseR(skR, enc, info) {
|
|
|
689
693
|
return keySchedule(sharedSecret, info);
|
|
690
694
|
}
|
|
691
695
|
function generateX25519Keypair() {
|
|
692
|
-
const priv = x25519.utils.
|
|
696
|
+
const priv = x25519.utils.randomSecretKey();
|
|
693
697
|
return { priv, pub: x25519.getPublicKey(priv) };
|
|
694
698
|
}
|
|
695
699
|
function randomBytes(n) {
|
|
@@ -700,8 +704,8 @@ function sha256Hex(data) {
|
|
|
700
704
|
}
|
|
701
705
|
|
|
702
706
|
// src/_privateInference.ts
|
|
703
|
-
import { chacha20poly1305 as chacha20poly13052 } from "@noble/ciphers/chacha";
|
|
704
|
-
import { extract as extract2, expand as expand2 } from "@noble/hashes/hkdf";
|
|
707
|
+
import { chacha20poly1305 as chacha20poly13052 } from "@noble/ciphers/chacha.js";
|
|
708
|
+
import { extract as extract2, expand as expand2 } from "@noble/hashes/hkdf.js";
|
|
705
709
|
var ENVELOPE_VERSION = 1;
|
|
706
710
|
var TEXT_ENCODER2 = new TextEncoder();
|
|
707
711
|
var TEXT_DECODER = new TextDecoder();
|
|
@@ -765,18 +769,35 @@ function seqNonce(seq) {
|
|
|
765
769
|
}
|
|
766
770
|
return nonce;
|
|
767
771
|
}
|
|
768
|
-
function sealRequest(
|
|
772
|
+
function sealRequest(orgPubs, body, opts) {
|
|
773
|
+
const pubs = Array.isArray(orgPubs) ? orgPubs : [orgPubs];
|
|
774
|
+
if (pubs.length === 0) {
|
|
775
|
+
throw new Error("sealRequest: no org public keys to seal to");
|
|
776
|
+
}
|
|
777
|
+
const seen = /* @__PURE__ */ new Set();
|
|
778
|
+
const uniquePubs = [];
|
|
779
|
+
for (const p of pubs) {
|
|
780
|
+
const fp = fingerprint(p);
|
|
781
|
+
if (!seen.has(fp)) {
|
|
782
|
+
seen.add(fp);
|
|
783
|
+
uniquePubs.push(p);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
769
786
|
const { priv: respPriv, pub: respPub } = generateX25519Keypair();
|
|
770
787
|
const respPkB64 = b64(respPub);
|
|
771
788
|
const dek = randomBytes(32);
|
|
772
789
|
const nonce = randomBytes(12);
|
|
773
790
|
const aad = buildAad(opts.model, opts.stream, opts.taskType, respPkB64);
|
|
774
791
|
const ciphertext = chacha20poly13052(dek, nonce, aad).encrypt(body);
|
|
775
|
-
const
|
|
776
|
-
const
|
|
792
|
+
const recipients = [];
|
|
793
|
+
for (const pub of uniquePubs) {
|
|
794
|
+
const { enc, ctx: sealer } = setupBaseS(pub, SUITE_INFO);
|
|
795
|
+
const sealedDek = sealer.seal(new Uint8Array(0), dek);
|
|
796
|
+
recipients.push({ kid: fingerprint(pub), enc: b64(enc), sealed_dek: b64(sealedDek) });
|
|
797
|
+
}
|
|
777
798
|
const envelope = {
|
|
778
799
|
v: ENVELOPE_VERSION,
|
|
779
|
-
recipients
|
|
800
|
+
recipients,
|
|
780
801
|
nonce: b64(nonce),
|
|
781
802
|
ciphertext: b64(ciphertext),
|
|
782
803
|
response_pk: respPkB64
|
|
@@ -926,72 +947,88 @@ function b64d2(s) {
|
|
|
926
947
|
}
|
|
927
948
|
var PrivateInferenceEngine = class {
|
|
928
949
|
constructor(opts) {
|
|
929
|
-
this.
|
|
950
|
+
this.orgPubs = null;
|
|
930
951
|
this.baseUrl = opts.baseUrl.replace(/\/$/, "");
|
|
931
952
|
this.apiKey = opts.apiKey;
|
|
932
|
-
const
|
|
933
|
-
this.
|
|
953
|
+
const fps = (opts.pinnedFingerprint || "").split(",").map((f) => f.trim().toLowerCase()).filter((f) => f.length > 0);
|
|
954
|
+
this.pinnedFingerprints = fps.length > 0 ? new Set(fps) : null;
|
|
934
955
|
this.affinitySalt = opts.affinitySalt ?? null;
|
|
935
956
|
this.tofuPath = opts.tofuPath || defaultTofuPath();
|
|
936
957
|
}
|
|
937
958
|
// --- key resolution ---
|
|
959
|
+
cachedOrgPublicKeys() {
|
|
960
|
+
return this.orgPubs;
|
|
961
|
+
}
|
|
938
962
|
cachedOrgPublicKey() {
|
|
939
|
-
return this.
|
|
963
|
+
return this.orgPubs && this.orgPubs.length > 0 ? this.orgPubs[0] : null;
|
|
940
964
|
}
|
|
941
965
|
tofuIdentity() {
|
|
942
966
|
const digest = sha256Hex(new TextEncoder().encode(this.apiKey)).slice(0, 16);
|
|
943
967
|
return `${this.baseUrl}|${digest}`;
|
|
944
968
|
}
|
|
945
969
|
/**
|
|
946
|
-
* Validate the key(s) returned by GET /v1/private-inference/key
|
|
947
|
-
*
|
|
948
|
-
*
|
|
970
|
+
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
971
|
+
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
972
|
+
* them (newest-seen first). The client seals to **all** of the returned keys
|
|
973
|
+
* so any worker holding any of them can serve the request. Throws
|
|
974
|
+
* PrivateInferenceError on absence, a malformed key, or (when pinned) an empty
|
|
975
|
+
* intersection with the pin set.
|
|
949
976
|
*/
|
|
950
977
|
async resolveOrgKey(fetchedKeys) {
|
|
951
|
-
if (this.
|
|
978
|
+
if (this.orgPubs !== null) return this.orgPubs;
|
|
952
979
|
if (!fetchedKeys || fetchedKeys.length === 0) {
|
|
953
980
|
throw new PrivateInferenceError(
|
|
954
|
-
"No private-inference workload key is registered for this organisation. Run `pendra keygen
|
|
981
|
+
"No private-inference workload key is registered for this organisation. Run `pendra keygen --save` on your worker(s), then reconnect them."
|
|
955
982
|
);
|
|
956
983
|
}
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
984
|
+
const pubs = [];
|
|
985
|
+
const fps = [];
|
|
986
|
+
for (const entry of fetchedKeys) {
|
|
987
|
+
let pub;
|
|
988
|
+
try {
|
|
989
|
+
pub = b64d2(entry.public_key);
|
|
990
|
+
} catch (e) {
|
|
991
|
+
throw new PrivateInferenceError(`Malformed workload key from the server: ${String(e)}`);
|
|
992
|
+
}
|
|
993
|
+
if (pub.length !== 32) {
|
|
994
|
+
throw new PrivateInferenceError(
|
|
995
|
+
`Workload key from the server is ${pub.length} bytes; expected a 32-byte X25519 key.`
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
pubs.push(pub);
|
|
999
|
+
fps.push(fingerprint(pub));
|
|
968
1000
|
}
|
|
969
|
-
|
|
970
|
-
if (this.
|
|
971
|
-
|
|
1001
|
+
let accepted;
|
|
1002
|
+
if (this.pinnedFingerprints !== null) {
|
|
1003
|
+
accepted = pubs.filter((_p, i) => this.pinnedFingerprints.has(fps[i]));
|
|
1004
|
+
if (accepted.length === 0) {
|
|
1005
|
+
const pinned = Array.from(this.pinnedFingerprints).sort().join(", ");
|
|
972
1006
|
throw new PrivateInferenceError(
|
|
973
|
-
`
|
|
1007
|
+
`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
1008
|
);
|
|
975
1009
|
}
|
|
976
1010
|
} else {
|
|
977
|
-
await this.
|
|
1011
|
+
await this.tofuTrust(fps);
|
|
1012
|
+
accepted = pubs;
|
|
978
1013
|
}
|
|
979
|
-
this.
|
|
980
|
-
return
|
|
1014
|
+
this.orgPubs = accepted;
|
|
1015
|
+
return accepted;
|
|
981
1016
|
}
|
|
982
|
-
async
|
|
1017
|
+
async tofuTrust(fps) {
|
|
983
1018
|
const store = await this.loadTofu();
|
|
984
1019
|
const identity = this.tofuIdentity();
|
|
985
1020
|
const known = store[identity];
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1021
|
+
const trusted = /* @__PURE__ */ new Set();
|
|
1022
|
+
if (typeof known === "string") {
|
|
1023
|
+
trusted.add(known);
|
|
1024
|
+
} else if (Array.isArray(known)) {
|
|
1025
|
+
for (const f of known) if (typeof f === "string") trusted.add(f);
|
|
990
1026
|
}
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
);
|
|
1027
|
+
const before = trusted.size;
|
|
1028
|
+
for (const f of fps) trusted.add(f);
|
|
1029
|
+
if (trusted.size !== before || !Array.isArray(known)) {
|
|
1030
|
+
store[identity] = Array.from(trusted).sort();
|
|
1031
|
+
await this.saveTofu(store);
|
|
995
1032
|
}
|
|
996
1033
|
}
|
|
997
1034
|
async loadTofu() {
|
|
@@ -1031,7 +1068,7 @@ var PrivateInferenceEngine = class {
|
|
|
1031
1068
|
* `messages` (chat only) drives the affinity token; pass undefined for non-chat.
|
|
1032
1069
|
*/
|
|
1033
1070
|
seal(args) {
|
|
1034
|
-
if (this.
|
|
1071
|
+
if (this.orgPubs === null || this.orgPubs.length === 0) {
|
|
1035
1072
|
throw new PrivateInferenceError(
|
|
1036
1073
|
"private inference: org key not resolved yet (call resolveOrgKey first)"
|
|
1037
1074
|
);
|
|
@@ -1040,7 +1077,7 @@ var PrivateInferenceEngine = class {
|
|
|
1040
1077
|
if (args.messages != null) {
|
|
1041
1078
|
affinity = affinityKey(this.apiKey, args.model, args.messages, this.affinitySalt);
|
|
1042
1079
|
}
|
|
1043
|
-
const { envelope, responsePriv } = sealRequest(this.
|
|
1080
|
+
const { envelope, responsePriv } = sealRequest(this.orgPubs, args.body, {
|
|
1044
1081
|
model: args.model,
|
|
1045
1082
|
stream: args.stream,
|
|
1046
1083
|
taskType: args.taskType,
|
|
@@ -1120,7 +1157,7 @@ var Pendra = class {
|
|
|
1120
1157
|
if (!this._piEngine) {
|
|
1121
1158
|
throw new PrivateInferenceError("private inference is not enabled on this client");
|
|
1122
1159
|
}
|
|
1123
|
-
if (this._piEngine.
|
|
1160
|
+
if (this._piEngine.cachedOrgPublicKeys() !== null) {
|
|
1124
1161
|
return this._piEngine;
|
|
1125
1162
|
}
|
|
1126
1163
|
if (!this._piKeyFetch) {
|
|
@@ -1156,7 +1193,7 @@ var Pendra = class {
|
|
|
1156
1193
|
}
|
|
1157
1194
|
if (response.status === 404) {
|
|
1158
1195
|
throw new PrivateInferenceError(
|
|
1159
|
-
"No private-inference workload key is registered for this organisation. Run `pendra keygen
|
|
1196
|
+
"No private-inference workload key is registered for this organisation. Run `pendra keygen --save` on your worker(s), then reconnect them."
|
|
1160
1197
|
);
|
|
1161
1198
|
}
|
|
1162
1199
|
if (!response.ok) {
|