pendra 0.6.1 → 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 +95 -58
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +89 -52
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.d.mts
CHANGED
|
@@ -49,9 +49,24 @@ interface Usage {
|
|
|
49
49
|
completion_tokens: number;
|
|
50
50
|
total_tokens: number;
|
|
51
51
|
}
|
|
52
|
+
interface ChatCompletionMessageToolCall {
|
|
53
|
+
/**
|
|
54
|
+
* Streaming only. A streamed tool call arrives as fragments, and `index` is
|
|
55
|
+
* what says which call each fragment belongs to. Absent on a non-streaming
|
|
56
|
+
* message, where every call arrives complete in one piece.
|
|
57
|
+
*/
|
|
58
|
+
index?: number;
|
|
59
|
+
id: string;
|
|
60
|
+
type: string;
|
|
61
|
+
function: {
|
|
62
|
+
name: string;
|
|
63
|
+
arguments: string;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
52
66
|
interface ChatCompletionMessage {
|
|
53
67
|
role: string;
|
|
54
68
|
content: string | null;
|
|
69
|
+
tool_calls?: ChatCompletionMessageToolCall[];
|
|
55
70
|
}
|
|
56
71
|
interface Choice {
|
|
57
72
|
index: number;
|
|
@@ -69,6 +84,7 @@ interface ChatCompletion {
|
|
|
69
84
|
interface ChoiceDelta {
|
|
70
85
|
role?: string | null;
|
|
71
86
|
content?: string | null;
|
|
87
|
+
tool_calls?: ChatCompletionMessageToolCall[];
|
|
72
88
|
}
|
|
73
89
|
interface StreamChoice {
|
|
74
90
|
index: number;
|
|
@@ -93,6 +109,17 @@ interface ChatCompletionCreateParams {
|
|
|
93
109
|
top_p?: number | null;
|
|
94
110
|
stop?: string | string[] | null;
|
|
95
111
|
stream?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Functions the model may call, in the OpenAI tools format. When the model
|
|
114
|
+
* calls one, the reply's `choices[0].message.tool_calls` is populated and
|
|
115
|
+
* `finish_reason` is `"tool_calls"`; you run the function and send the result
|
|
116
|
+
* back as a `{ role: "tool", ... }` message.
|
|
117
|
+
*/
|
|
118
|
+
tools?: Array<Record<string, unknown>> | null;
|
|
119
|
+
/** `"auto"`, `"none"`, or a specific function to force. */
|
|
120
|
+
tool_choice?: unknown;
|
|
121
|
+
/** Allow the model to request several tool calls in one reply. */
|
|
122
|
+
parallel_tool_calls?: boolean | null;
|
|
96
123
|
/**
|
|
97
124
|
* Optionally pin the request to a specific worker that serves this model.
|
|
98
125
|
* Sent as the `X-Pendra-Worker-Id` header; when omitted, the least-busy
|
|
@@ -324,20 +351,24 @@ interface EngineOptions {
|
|
|
324
351
|
declare class PrivateInferenceEngine {
|
|
325
352
|
private baseUrl;
|
|
326
353
|
private apiKey;
|
|
327
|
-
private
|
|
354
|
+
private pinnedFingerprints;
|
|
328
355
|
private affinitySalt;
|
|
329
356
|
private tofuPath;
|
|
330
|
-
private
|
|
357
|
+
private orgPubs;
|
|
331
358
|
constructor(opts: EngineOptions);
|
|
359
|
+
cachedOrgPublicKeys(): Uint8Array[] | null;
|
|
332
360
|
cachedOrgPublicKey(): Uint8Array | null;
|
|
333
361
|
private tofuIdentity;
|
|
334
362
|
/**
|
|
335
|
-
* Validate the key(s) returned by GET /v1/private-inference/key
|
|
336
|
-
*
|
|
337
|
-
*
|
|
363
|
+
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
364
|
+
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
365
|
+
* them (newest-seen first). The client seals to **all** of the returned keys
|
|
366
|
+
* so any worker holding any of them can serve the request. Throws
|
|
367
|
+
* PrivateInferenceError on absence, a malformed key, or (when pinned) an empty
|
|
368
|
+
* intersection with the pin set.
|
|
338
369
|
*/
|
|
339
|
-
resolveOrgKey(fetchedKeys: FetchedKey[]): Promise<Uint8Array>;
|
|
340
|
-
private
|
|
370
|
+
resolveOrgKey(fetchedKeys: FetchedKey[]): Promise<Uint8Array[]>;
|
|
371
|
+
private tofuTrust;
|
|
341
372
|
private loadTofu;
|
|
342
373
|
private saveTofu;
|
|
343
374
|
/**
|
|
@@ -428,4 +459,4 @@ declare class RateLimitError extends APIStatusError {
|
|
|
428
459
|
constructor(message?: string);
|
|
429
460
|
}
|
|
430
461
|
|
|
431
|
-
export { APIConnectionError, APIError, APIStatusError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionMessage, type Choice, type ChoiceDelta, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraOptions, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
|
|
462
|
+
export { APIConnectionError, APIError, APIStatusError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraOptions, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -49,9 +49,24 @@ interface Usage {
|
|
|
49
49
|
completion_tokens: number;
|
|
50
50
|
total_tokens: number;
|
|
51
51
|
}
|
|
52
|
+
interface ChatCompletionMessageToolCall {
|
|
53
|
+
/**
|
|
54
|
+
* Streaming only. A streamed tool call arrives as fragments, and `index` is
|
|
55
|
+
* what says which call each fragment belongs to. Absent on a non-streaming
|
|
56
|
+
* message, where every call arrives complete in one piece.
|
|
57
|
+
*/
|
|
58
|
+
index?: number;
|
|
59
|
+
id: string;
|
|
60
|
+
type: string;
|
|
61
|
+
function: {
|
|
62
|
+
name: string;
|
|
63
|
+
arguments: string;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
52
66
|
interface ChatCompletionMessage {
|
|
53
67
|
role: string;
|
|
54
68
|
content: string | null;
|
|
69
|
+
tool_calls?: ChatCompletionMessageToolCall[];
|
|
55
70
|
}
|
|
56
71
|
interface Choice {
|
|
57
72
|
index: number;
|
|
@@ -69,6 +84,7 @@ interface ChatCompletion {
|
|
|
69
84
|
interface ChoiceDelta {
|
|
70
85
|
role?: string | null;
|
|
71
86
|
content?: string | null;
|
|
87
|
+
tool_calls?: ChatCompletionMessageToolCall[];
|
|
72
88
|
}
|
|
73
89
|
interface StreamChoice {
|
|
74
90
|
index: number;
|
|
@@ -93,6 +109,17 @@ interface ChatCompletionCreateParams {
|
|
|
93
109
|
top_p?: number | null;
|
|
94
110
|
stop?: string | string[] | null;
|
|
95
111
|
stream?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Functions the model may call, in the OpenAI tools format. When the model
|
|
114
|
+
* calls one, the reply's `choices[0].message.tool_calls` is populated and
|
|
115
|
+
* `finish_reason` is `"tool_calls"`; you run the function and send the result
|
|
116
|
+
* back as a `{ role: "tool", ... }` message.
|
|
117
|
+
*/
|
|
118
|
+
tools?: Array<Record<string, unknown>> | null;
|
|
119
|
+
/** `"auto"`, `"none"`, or a specific function to force. */
|
|
120
|
+
tool_choice?: unknown;
|
|
121
|
+
/** Allow the model to request several tool calls in one reply. */
|
|
122
|
+
parallel_tool_calls?: boolean | null;
|
|
96
123
|
/**
|
|
97
124
|
* Optionally pin the request to a specific worker that serves this model.
|
|
98
125
|
* Sent as the `X-Pendra-Worker-Id` header; when omitted, the least-busy
|
|
@@ -324,20 +351,24 @@ interface EngineOptions {
|
|
|
324
351
|
declare class PrivateInferenceEngine {
|
|
325
352
|
private baseUrl;
|
|
326
353
|
private apiKey;
|
|
327
|
-
private
|
|
354
|
+
private pinnedFingerprints;
|
|
328
355
|
private affinitySalt;
|
|
329
356
|
private tofuPath;
|
|
330
|
-
private
|
|
357
|
+
private orgPubs;
|
|
331
358
|
constructor(opts: EngineOptions);
|
|
359
|
+
cachedOrgPublicKeys(): Uint8Array[] | null;
|
|
332
360
|
cachedOrgPublicKey(): Uint8Array | null;
|
|
333
361
|
private tofuIdentity;
|
|
334
362
|
/**
|
|
335
|
-
* Validate the key(s) returned by GET /v1/private-inference/key
|
|
336
|
-
*
|
|
337
|
-
*
|
|
363
|
+
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
364
|
+
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
365
|
+
* them (newest-seen first). The client seals to **all** of the returned keys
|
|
366
|
+
* so any worker holding any of them can serve the request. Throws
|
|
367
|
+
* PrivateInferenceError on absence, a malformed key, or (when pinned) an empty
|
|
368
|
+
* intersection with the pin set.
|
|
338
369
|
*/
|
|
339
|
-
resolveOrgKey(fetchedKeys: FetchedKey[]): Promise<Uint8Array>;
|
|
340
|
-
private
|
|
370
|
+
resolveOrgKey(fetchedKeys: FetchedKey[]): Promise<Uint8Array[]>;
|
|
371
|
+
private tofuTrust;
|
|
341
372
|
private loadTofu;
|
|
342
373
|
private saveTofu;
|
|
343
374
|
/**
|
|
@@ -428,4 +459,4 @@ declare class RateLimitError extends APIStatusError {
|
|
|
428
459
|
constructor(message?: string);
|
|
429
460
|
}
|
|
430
461
|
|
|
431
|
-
export { APIConnectionError, APIError, APIStatusError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionMessage, type Choice, type ChoiceDelta, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraOptions, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
|
|
462
|
+
export { APIConnectionError, APIError, APIStatusError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraOptions, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
|
package/dist/index.js
CHANGED
|
@@ -337,6 +337,10 @@ var Completions = class {
|
|
|
337
337
|
if (params.max_tokens != null) body.max_tokens = params.max_tokens;
|
|
338
338
|
if (params.top_p != null) body.top_p = params.top_p;
|
|
339
339
|
if (params.stop != null) body.stop = params.stop;
|
|
340
|
+
if (params.tools != null) body.tools = params.tools;
|
|
341
|
+
if (params.tool_choice != null) body.tool_choice = params.tool_choice;
|
|
342
|
+
if (params.parallel_tool_calls != null)
|
|
343
|
+
body.parallel_tool_calls = params.parallel_tool_calls;
|
|
340
344
|
const headers = params.worker_id ? { "X-Pendra-Worker-Id": params.worker_id } : void 0;
|
|
341
345
|
if (this.client.privateInferenceEnabled) {
|
|
342
346
|
return this.createEncrypted(params, body, headers);
|
|
@@ -614,12 +618,12 @@ var Rerank = class {
|
|
|
614
618
|
};
|
|
615
619
|
|
|
616
620
|
// src/_hpke.ts
|
|
617
|
-
var import_ed25519 = require("@noble/curves/ed25519");
|
|
618
|
-
var import_chacha = require("@noble/ciphers/chacha");
|
|
619
|
-
var
|
|
620
|
-
var import_hmac = require("@noble/hashes/hmac");
|
|
621
|
-
var import_hkdf = require("@noble/hashes/hkdf");
|
|
622
|
-
var import_utils = require("@noble/hashes/utils");
|
|
621
|
+
var import_ed25519 = require("@noble/curves/ed25519.js");
|
|
622
|
+
var import_chacha = require("@noble/ciphers/chacha.js");
|
|
623
|
+
var import_sha2 = require("@noble/hashes/sha2.js");
|
|
624
|
+
var import_hmac = require("@noble/hashes/hmac.js");
|
|
625
|
+
var import_hkdf = require("@noble/hashes/hkdf.js");
|
|
626
|
+
var import_utils = require("@noble/hashes/utils.js");
|
|
623
627
|
var KEM_ID = 32;
|
|
624
628
|
var KDF_ID = 1;
|
|
625
629
|
var AEAD_ID = 3;
|
|
@@ -655,10 +659,10 @@ var KEM_SUITE_ID = concat(bytes("KEM"), i2osp(KEM_ID, 2));
|
|
|
655
659
|
var HPKE_VERSION = bytes("HPKE-v1");
|
|
656
660
|
function hkdfExtract(salt, ikm) {
|
|
657
661
|
const effectiveSalt = salt.length === 0 ? new Uint8Array(NH) : salt;
|
|
658
|
-
return (0, import_hkdf.extract)(
|
|
662
|
+
return (0, import_hkdf.extract)(import_sha2.sha256, ikm, effectiveSalt);
|
|
659
663
|
}
|
|
660
664
|
function hkdfExpand(prk, info, length) {
|
|
661
|
-
return (0, import_hkdf.expand)(
|
|
665
|
+
return (0, import_hkdf.expand)(import_sha2.sha256, prk, info, length);
|
|
662
666
|
}
|
|
663
667
|
function labeledExtract(salt, label, ikm, suiteId) {
|
|
664
668
|
return hkdfExtract(salt, concat(HPKE_VERSION, suiteId, label, ikm));
|
|
@@ -672,7 +676,7 @@ function extractAndExpand(dh, kemContext) {
|
|
|
672
676
|
return labeledExpand(eaePrk, bytes("shared_secret"), kemContext, NSECRET, KEM_SUITE_ID);
|
|
673
677
|
}
|
|
674
678
|
function encap(pkR, skE) {
|
|
675
|
-
const skEObj = skE ?? import_ed25519.x25519.utils.
|
|
679
|
+
const skEObj = skE ?? import_ed25519.x25519.utils.randomSecretKey();
|
|
676
680
|
const enc = import_ed25519.x25519.getPublicKey(skEObj);
|
|
677
681
|
const dh = import_ed25519.x25519.getSharedSecret(skEObj, pkR);
|
|
678
682
|
const kemContext = concat(enc, pkR);
|
|
@@ -733,19 +737,19 @@ function setupBaseR(skR, enc, info) {
|
|
|
733
737
|
return keySchedule(sharedSecret, info);
|
|
734
738
|
}
|
|
735
739
|
function generateX25519Keypair() {
|
|
736
|
-
const priv = import_ed25519.x25519.utils.
|
|
740
|
+
const priv = import_ed25519.x25519.utils.randomSecretKey();
|
|
737
741
|
return { priv, pub: import_ed25519.x25519.getPublicKey(priv) };
|
|
738
742
|
}
|
|
739
743
|
function randomBytes(n) {
|
|
740
744
|
return (0, import_utils.randomBytes)(n);
|
|
741
745
|
}
|
|
742
746
|
function sha256Hex(data) {
|
|
743
|
-
return Array.from((0,
|
|
747
|
+
return Array.from((0, import_sha2.sha256)(data)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
744
748
|
}
|
|
745
749
|
|
|
746
750
|
// src/_privateInference.ts
|
|
747
|
-
var import_chacha2 = require("@noble/ciphers/chacha");
|
|
748
|
-
var import_hkdf2 = require("@noble/hashes/hkdf");
|
|
751
|
+
var import_chacha2 = require("@noble/ciphers/chacha.js");
|
|
752
|
+
var import_hkdf2 = require("@noble/hashes/hkdf.js");
|
|
749
753
|
var ENVELOPE_VERSION = 1;
|
|
750
754
|
var TEXT_ENCODER2 = new TextEncoder();
|
|
751
755
|
var TEXT_DECODER = new TextDecoder();
|
|
@@ -809,18 +813,35 @@ function seqNonce(seq) {
|
|
|
809
813
|
}
|
|
810
814
|
return nonce;
|
|
811
815
|
}
|
|
812
|
-
function sealRequest(
|
|
816
|
+
function sealRequest(orgPubs, body, opts) {
|
|
817
|
+
const pubs = Array.isArray(orgPubs) ? orgPubs : [orgPubs];
|
|
818
|
+
if (pubs.length === 0) {
|
|
819
|
+
throw new Error("sealRequest: no org public keys to seal to");
|
|
820
|
+
}
|
|
821
|
+
const seen = /* @__PURE__ */ new Set();
|
|
822
|
+
const uniquePubs = [];
|
|
823
|
+
for (const p of pubs) {
|
|
824
|
+
const fp = fingerprint(p);
|
|
825
|
+
if (!seen.has(fp)) {
|
|
826
|
+
seen.add(fp);
|
|
827
|
+
uniquePubs.push(p);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
813
830
|
const { priv: respPriv, pub: respPub } = generateX25519Keypair();
|
|
814
831
|
const respPkB64 = b64(respPub);
|
|
815
832
|
const dek = randomBytes(32);
|
|
816
833
|
const nonce = randomBytes(12);
|
|
817
834
|
const aad = buildAad(opts.model, opts.stream, opts.taskType, respPkB64);
|
|
818
835
|
const ciphertext = (0, import_chacha2.chacha20poly1305)(dek, nonce, aad).encrypt(body);
|
|
819
|
-
const
|
|
820
|
-
const
|
|
836
|
+
const recipients = [];
|
|
837
|
+
for (const pub of uniquePubs) {
|
|
838
|
+
const { enc, ctx: sealer } = setupBaseS(pub, SUITE_INFO);
|
|
839
|
+
const sealedDek = sealer.seal(new Uint8Array(0), dek);
|
|
840
|
+
recipients.push({ kid: fingerprint(pub), enc: b64(enc), sealed_dek: b64(sealedDek) });
|
|
841
|
+
}
|
|
821
842
|
const envelope = {
|
|
822
843
|
v: ENVELOPE_VERSION,
|
|
823
|
-
recipients
|
|
844
|
+
recipients,
|
|
824
845
|
nonce: b64(nonce),
|
|
825
846
|
ciphertext: b64(ciphertext),
|
|
826
847
|
response_pk: respPkB64
|
|
@@ -869,8 +890,8 @@ var ResponseDecryptor = class {
|
|
|
869
890
|
}
|
|
870
891
|
};
|
|
871
892
|
function deriveSalt(rawApiKey) {
|
|
872
|
-
const prk = (0, import_hkdf2.extract)(
|
|
873
|
-
return (0, import_hkdf2.expand)(
|
|
893
|
+
const prk = (0, import_hkdf2.extract)(import_sha2.sha256, utf8(rawApiKey), new Uint8Array(32));
|
|
894
|
+
return (0, import_hkdf2.expand)(import_sha2.sha256, prk, AFFINITY_SALT_INFO, 32);
|
|
874
895
|
}
|
|
875
896
|
function canonicalJson(value) {
|
|
876
897
|
if (value === null) return "null";
|
|
@@ -944,7 +965,7 @@ function affinityKey(rawApiKey, model, messages, saltOverride) {
|
|
|
944
965
|
}
|
|
945
966
|
if (!sawUser) return null;
|
|
946
967
|
const salt = saltOverride != null ? saltOverride : deriveSalt(rawApiKey);
|
|
947
|
-
const mac = import_hmac.hmac(
|
|
968
|
+
const mac = import_hmac.hmac(import_sha2.sha256, salt, utf8(parts.join("\0")));
|
|
948
969
|
return toHex(mac);
|
|
949
970
|
}
|
|
950
971
|
|
|
@@ -970,72 +991,88 @@ function b64d2(s) {
|
|
|
970
991
|
}
|
|
971
992
|
var PrivateInferenceEngine = class {
|
|
972
993
|
constructor(opts) {
|
|
973
|
-
this.
|
|
994
|
+
this.orgPubs = null;
|
|
974
995
|
this.baseUrl = opts.baseUrl.replace(/\/$/, "");
|
|
975
996
|
this.apiKey = opts.apiKey;
|
|
976
|
-
const
|
|
977
|
-
this.
|
|
997
|
+
const fps = (opts.pinnedFingerprint || "").split(",").map((f) => f.trim().toLowerCase()).filter((f) => f.length > 0);
|
|
998
|
+
this.pinnedFingerprints = fps.length > 0 ? new Set(fps) : null;
|
|
978
999
|
this.affinitySalt = opts.affinitySalt ?? null;
|
|
979
1000
|
this.tofuPath = opts.tofuPath || defaultTofuPath();
|
|
980
1001
|
}
|
|
981
1002
|
// --- key resolution ---
|
|
1003
|
+
cachedOrgPublicKeys() {
|
|
1004
|
+
return this.orgPubs;
|
|
1005
|
+
}
|
|
982
1006
|
cachedOrgPublicKey() {
|
|
983
|
-
return this.
|
|
1007
|
+
return this.orgPubs && this.orgPubs.length > 0 ? this.orgPubs[0] : null;
|
|
984
1008
|
}
|
|
985
1009
|
tofuIdentity() {
|
|
986
1010
|
const digest = sha256Hex(new TextEncoder().encode(this.apiKey)).slice(0, 16);
|
|
987
1011
|
return `${this.baseUrl}|${digest}`;
|
|
988
1012
|
}
|
|
989
1013
|
/**
|
|
990
|
-
* Validate the key(s) returned by GET /v1/private-inference/key
|
|
991
|
-
*
|
|
992
|
-
*
|
|
1014
|
+
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
1015
|
+
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
1016
|
+
* them (newest-seen first). The client seals to **all** of the returned keys
|
|
1017
|
+
* so any worker holding any of them can serve the request. Throws
|
|
1018
|
+
* PrivateInferenceError on absence, a malformed key, or (when pinned) an empty
|
|
1019
|
+
* intersection with the pin set.
|
|
993
1020
|
*/
|
|
994
1021
|
async resolveOrgKey(fetchedKeys) {
|
|
995
|
-
if (this.
|
|
1022
|
+
if (this.orgPubs !== null) return this.orgPubs;
|
|
996
1023
|
if (!fetchedKeys || fetchedKeys.length === 0) {
|
|
997
1024
|
throw new PrivateInferenceError(
|
|
998
1025
|
"No private-inference workload key is registered for this organisation. Run `pendra keygen --save` on your worker(s), then reconnect them."
|
|
999
1026
|
);
|
|
1000
1027
|
}
|
|
1001
|
-
const
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1028
|
+
const pubs = [];
|
|
1029
|
+
const fps = [];
|
|
1030
|
+
for (const entry of fetchedKeys) {
|
|
1031
|
+
let pub;
|
|
1032
|
+
try {
|
|
1033
|
+
pub = b64d2(entry.public_key);
|
|
1034
|
+
} catch (e) {
|
|
1035
|
+
throw new PrivateInferenceError(`Malformed workload key from the server: ${String(e)}`);
|
|
1036
|
+
}
|
|
1037
|
+
if (pub.length !== 32) {
|
|
1038
|
+
throw new PrivateInferenceError(
|
|
1039
|
+
`Workload key from the server is ${pub.length} bytes; expected a 32-byte X25519 key.`
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
pubs.push(pub);
|
|
1043
|
+
fps.push(fingerprint(pub));
|
|
1012
1044
|
}
|
|
1013
|
-
|
|
1014
|
-
if (this.
|
|
1015
|
-
|
|
1045
|
+
let accepted;
|
|
1046
|
+
if (this.pinnedFingerprints !== null) {
|
|
1047
|
+
accepted = pubs.filter((_p, i) => this.pinnedFingerprints.has(fps[i]));
|
|
1048
|
+
if (accepted.length === 0) {
|
|
1049
|
+
const pinned = Array.from(this.pinnedFingerprints).sort().join(", ");
|
|
1016
1050
|
throw new PrivateInferenceError(
|
|
1017
|
-
`
|
|
1051
|
+
`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.`
|
|
1018
1052
|
);
|
|
1019
1053
|
}
|
|
1020
1054
|
} else {
|
|
1021
|
-
await this.
|
|
1055
|
+
await this.tofuTrust(fps);
|
|
1056
|
+
accepted = pubs;
|
|
1022
1057
|
}
|
|
1023
|
-
this.
|
|
1024
|
-
return
|
|
1058
|
+
this.orgPubs = accepted;
|
|
1059
|
+
return accepted;
|
|
1025
1060
|
}
|
|
1026
|
-
async
|
|
1061
|
+
async tofuTrust(fps) {
|
|
1027
1062
|
const store = await this.loadTofu();
|
|
1028
1063
|
const identity = this.tofuIdentity();
|
|
1029
1064
|
const known = store[identity];
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1065
|
+
const trusted = /* @__PURE__ */ new Set();
|
|
1066
|
+
if (typeof known === "string") {
|
|
1067
|
+
trusted.add(known);
|
|
1068
|
+
} else if (Array.isArray(known)) {
|
|
1069
|
+
for (const f of known) if (typeof f === "string") trusted.add(f);
|
|
1034
1070
|
}
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
);
|
|
1071
|
+
const before = trusted.size;
|
|
1072
|
+
for (const f of fps) trusted.add(f);
|
|
1073
|
+
if (trusted.size !== before || !Array.isArray(known)) {
|
|
1074
|
+
store[identity] = Array.from(trusted).sort();
|
|
1075
|
+
await this.saveTofu(store);
|
|
1039
1076
|
}
|
|
1040
1077
|
}
|
|
1041
1078
|
async loadTofu() {
|
|
@@ -1075,7 +1112,7 @@ var PrivateInferenceEngine = class {
|
|
|
1075
1112
|
* `messages` (chat only) drives the affinity token; pass undefined for non-chat.
|
|
1076
1113
|
*/
|
|
1077
1114
|
seal(args) {
|
|
1078
|
-
if (this.
|
|
1115
|
+
if (this.orgPubs === null || this.orgPubs.length === 0) {
|
|
1079
1116
|
throw new PrivateInferenceError(
|
|
1080
1117
|
"private inference: org key not resolved yet (call resolveOrgKey first)"
|
|
1081
1118
|
);
|
|
@@ -1084,7 +1121,7 @@ var PrivateInferenceEngine = class {
|
|
|
1084
1121
|
if (args.messages != null) {
|
|
1085
1122
|
affinity = affinityKey(this.apiKey, args.model, args.messages, this.affinitySalt);
|
|
1086
1123
|
}
|
|
1087
|
-
const { envelope, responsePriv } = sealRequest(this.
|
|
1124
|
+
const { envelope, responsePriv } = sealRequest(this.orgPubs, args.body, {
|
|
1088
1125
|
model: args.model,
|
|
1089
1126
|
stream: args.stream,
|
|
1090
1127
|
taskType: args.taskType,
|
|
@@ -1164,7 +1201,7 @@ var Pendra = class {
|
|
|
1164
1201
|
if (!this._piEngine) {
|
|
1165
1202
|
throw new PrivateInferenceError("private inference is not enabled on this client");
|
|
1166
1203
|
}
|
|
1167
|
-
if (this._piEngine.
|
|
1204
|
+
if (this._piEngine.cachedOrgPublicKeys() !== null) {
|
|
1168
1205
|
return this._piEngine;
|
|
1169
1206
|
}
|
|
1170
1207
|
if (!this._piKeyFetch) {
|