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.d.mts
CHANGED
|
@@ -370,6 +370,12 @@ interface ChatCompletionCreateParams {
|
|
|
370
370
|
* rather than filtering lines — so a frame the server tags with a named
|
|
371
371
|
* `event:` is dropped in one piece instead of having its `data:` line mistaken
|
|
372
372
|
* for a chat chunk (issue #1398).
|
|
373
|
+
*
|
|
374
|
+
* Each surviving frame goes through {@link parseStreamFrame}, which throws on
|
|
375
|
+
* the API's in-band `{"error": …}` failure reports and on a frame that isn't a
|
|
376
|
+
* chunk. The error frames used to be yielded *as* chunks (`JSON.parse` succeeds
|
|
377
|
+
* on them), so a worker crash surfaced as a `TypeError` on `chunk.choices[0]`
|
|
378
|
+
* rather than as something a caller could catch and read (issue #2252).
|
|
373
379
|
*/
|
|
374
380
|
declare class Stream implements AsyncIterable<ChatCompletionChunk> {
|
|
375
381
|
private reader;
|
|
@@ -602,6 +608,23 @@ declare class PrivateInferenceEngine {
|
|
|
602
608
|
cachedOrgPublicKeys(): Uint8Array[] | null;
|
|
603
609
|
cachedOrgPublicKey(): Uint8Array | null;
|
|
604
610
|
private tofuIdentity;
|
|
611
|
+
/**
|
|
612
|
+
* Drop the cached key set so the next call re-fetches it.
|
|
613
|
+
*
|
|
614
|
+
* The cache is otherwise held for the whole process lifetime, which is
|
|
615
|
+
* correct while a fleet keeps one key but fatal when it re-keys: a
|
|
616
|
+
* long-running server would go on sealing to a fingerprint no worker holds
|
|
617
|
+
* and fail EVERY request until someone restarted it. The API signals a re-key
|
|
618
|
+
* with `X-Pendra-Error-Code: no_private_inference_worker`, and this is what
|
|
619
|
+
* lets the client recover on its own.
|
|
620
|
+
*
|
|
621
|
+
* Pinned clients are deliberately NOT re-fetched on that signal — a pin is an
|
|
622
|
+
* allowlist, so a fetch could only return fingerprints the pin already
|
|
623
|
+
* rejects. They surface the error instead, which is what pinning means.
|
|
624
|
+
*/
|
|
625
|
+
invalidateOrgKey(): void;
|
|
626
|
+
/** Whether this client seals only to an explicit fingerprint allowlist. */
|
|
627
|
+
get isPinned(): boolean;
|
|
605
628
|
/**
|
|
606
629
|
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
607
630
|
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
@@ -682,6 +705,17 @@ declare class Pendra {
|
|
|
682
705
|
response: Response;
|
|
683
706
|
controller: AbortController;
|
|
684
707
|
}>;
|
|
708
|
+
/**
|
|
709
|
+
* Drop the cached encryption keys when the API reports that no worker holds
|
|
710
|
+
* the one this request was sealed to — the signal that the org's fleet has
|
|
711
|
+
* re-keyed. Returns true if the cache was dropped.
|
|
712
|
+
*
|
|
713
|
+
* A PINNED client is left alone on purpose: a pin is an allowlist, so
|
|
714
|
+
* re-fetching could only return fingerprints the pin already rejects and
|
|
715
|
+
* would loop rather than recover. The error surfaces instead, which is
|
|
716
|
+
* exactly what pinning is for.
|
|
717
|
+
*/
|
|
718
|
+
private invalidateKeyCacheIfRekeyed;
|
|
685
719
|
}
|
|
686
720
|
|
|
687
721
|
declare class APIError extends Error {
|
|
@@ -701,5 +735,58 @@ declare class AuthenticationError extends APIStatusError {
|
|
|
701
735
|
declare class RateLimitError extends APIStatusError {
|
|
702
736
|
constructor(message?: string);
|
|
703
737
|
}
|
|
738
|
+
/**
|
|
739
|
+
* Raised when a response arrives but isn't the shape the SDK expects.
|
|
740
|
+
*
|
|
741
|
+
* A server or proxy problem, not a transport one: the request reached Pendra
|
|
742
|
+
* and came back, but the body could not be read as the documented type — a 200
|
|
743
|
+
* missing a required field, a captive-portal HTML page from a corporate proxy,
|
|
744
|
+
* a truncated body.
|
|
745
|
+
*
|
|
746
|
+
* Deliberately NOT an `APIConnectionError`: retrying a response that will never
|
|
747
|
+
* parse just burns the retry budget, so callers that retry on connection
|
|
748
|
+
* failures should let this one through. `body` holds a short excerpt of what
|
|
749
|
+
* actually came back.
|
|
750
|
+
*/
|
|
751
|
+
declare class APIResponseValidationError extends APIError {
|
|
752
|
+
/** How much of the offending body to keep in the message. */
|
|
753
|
+
static readonly BODY_EXCERPT_CHARS = 200;
|
|
754
|
+
readonly body: string | undefined;
|
|
755
|
+
constructor(message: string, options?: {
|
|
756
|
+
status?: number;
|
|
757
|
+
body?: string;
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Raised when a stream reports a failure in-band, after HTTP 200.
|
|
762
|
+
*
|
|
763
|
+
* A streaming response commits its status line before the worker has done any
|
|
764
|
+
* work, so anything that goes wrong afterwards — the worker crashing, the
|
|
765
|
+
* request timing out, the fleet being at capacity — can only be described in
|
|
766
|
+
* the body. The API sends `data: {"error": {…}}` followed by `data: [DONE]`,
|
|
767
|
+
* and this error is how the SDK surfaces that instead of ending the stream as
|
|
768
|
+
* though it had succeeded.
|
|
769
|
+
*
|
|
770
|
+
* `requestId` and `workerId` identify the failed inference to Pendra support.
|
|
771
|
+
*
|
|
772
|
+
* Note this extends `APIError`, not `APIStatusError`, because the API omits
|
|
773
|
+
* `code` on some frames (the timeout and the generic catch-all) — so `status`
|
|
774
|
+
* is genuinely optional here, which `APIStatusError`'s always-present `status`
|
|
775
|
+
* cannot express. Check `status` for undefined before comparing it.
|
|
776
|
+
*/
|
|
777
|
+
declare class APIStreamError extends APIError {
|
|
778
|
+
readonly type: string | undefined;
|
|
779
|
+
readonly code: number | undefined;
|
|
780
|
+
readonly requestId: string | undefined;
|
|
781
|
+
readonly workerId: string | undefined;
|
|
782
|
+
readonly workerName: string | undefined;
|
|
783
|
+
constructor(message: string, options?: {
|
|
784
|
+
type?: string;
|
|
785
|
+
code?: number;
|
|
786
|
+
requestId?: string;
|
|
787
|
+
workerId?: string;
|
|
788
|
+
workerName?: string;
|
|
789
|
+
});
|
|
790
|
+
}
|
|
704
791
|
|
|
705
|
-
export { APIConnectionError, APIError, APIStatusError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionContentPart, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CompletionTokensDetails, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraExtension, type PendraNotice, type PendraOptions, type PendraTiming, type PendraWebToolStep, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
|
|
792
|
+
export { APIConnectionError, APIError, APIResponseValidationError, APIStatusError, APIStreamError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionContentPart, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CompletionTokensDetails, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraExtension, type PendraNotice, type PendraOptions, type PendraTiming, type PendraWebToolStep, 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
|
@@ -370,6 +370,12 @@ interface ChatCompletionCreateParams {
|
|
|
370
370
|
* rather than filtering lines — so a frame the server tags with a named
|
|
371
371
|
* `event:` is dropped in one piece instead of having its `data:` line mistaken
|
|
372
372
|
* for a chat chunk (issue #1398).
|
|
373
|
+
*
|
|
374
|
+
* Each surviving frame goes through {@link parseStreamFrame}, which throws on
|
|
375
|
+
* the API's in-band `{"error": …}` failure reports and on a frame that isn't a
|
|
376
|
+
* chunk. The error frames used to be yielded *as* chunks (`JSON.parse` succeeds
|
|
377
|
+
* on them), so a worker crash surfaced as a `TypeError` on `chunk.choices[0]`
|
|
378
|
+
* rather than as something a caller could catch and read (issue #2252).
|
|
373
379
|
*/
|
|
374
380
|
declare class Stream implements AsyncIterable<ChatCompletionChunk> {
|
|
375
381
|
private reader;
|
|
@@ -602,6 +608,23 @@ declare class PrivateInferenceEngine {
|
|
|
602
608
|
cachedOrgPublicKeys(): Uint8Array[] | null;
|
|
603
609
|
cachedOrgPublicKey(): Uint8Array | null;
|
|
604
610
|
private tofuIdentity;
|
|
611
|
+
/**
|
|
612
|
+
* Drop the cached key set so the next call re-fetches it.
|
|
613
|
+
*
|
|
614
|
+
* The cache is otherwise held for the whole process lifetime, which is
|
|
615
|
+
* correct while a fleet keeps one key but fatal when it re-keys: a
|
|
616
|
+
* long-running server would go on sealing to a fingerprint no worker holds
|
|
617
|
+
* and fail EVERY request until someone restarted it. The API signals a re-key
|
|
618
|
+
* with `X-Pendra-Error-Code: no_private_inference_worker`, and this is what
|
|
619
|
+
* lets the client recover on its own.
|
|
620
|
+
*
|
|
621
|
+
* Pinned clients are deliberately NOT re-fetched on that signal — a pin is an
|
|
622
|
+
* allowlist, so a fetch could only return fingerprints the pin already
|
|
623
|
+
* rejects. They surface the error instead, which is what pinning means.
|
|
624
|
+
*/
|
|
625
|
+
invalidateOrgKey(): void;
|
|
626
|
+
/** Whether this client seals only to an explicit fingerprint allowlist. */
|
|
627
|
+
get isPinned(): boolean;
|
|
605
628
|
/**
|
|
606
629
|
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
607
630
|
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
@@ -682,6 +705,17 @@ declare class Pendra {
|
|
|
682
705
|
response: Response;
|
|
683
706
|
controller: AbortController;
|
|
684
707
|
}>;
|
|
708
|
+
/**
|
|
709
|
+
* Drop the cached encryption keys when the API reports that no worker holds
|
|
710
|
+
* the one this request was sealed to — the signal that the org's fleet has
|
|
711
|
+
* re-keyed. Returns true if the cache was dropped.
|
|
712
|
+
*
|
|
713
|
+
* A PINNED client is left alone on purpose: a pin is an allowlist, so
|
|
714
|
+
* re-fetching could only return fingerprints the pin already rejects and
|
|
715
|
+
* would loop rather than recover. The error surfaces instead, which is
|
|
716
|
+
* exactly what pinning is for.
|
|
717
|
+
*/
|
|
718
|
+
private invalidateKeyCacheIfRekeyed;
|
|
685
719
|
}
|
|
686
720
|
|
|
687
721
|
declare class APIError extends Error {
|
|
@@ -701,5 +735,58 @@ declare class AuthenticationError extends APIStatusError {
|
|
|
701
735
|
declare class RateLimitError extends APIStatusError {
|
|
702
736
|
constructor(message?: string);
|
|
703
737
|
}
|
|
738
|
+
/**
|
|
739
|
+
* Raised when a response arrives but isn't the shape the SDK expects.
|
|
740
|
+
*
|
|
741
|
+
* A server or proxy problem, not a transport one: the request reached Pendra
|
|
742
|
+
* and came back, but the body could not be read as the documented type — a 200
|
|
743
|
+
* missing a required field, a captive-portal HTML page from a corporate proxy,
|
|
744
|
+
* a truncated body.
|
|
745
|
+
*
|
|
746
|
+
* Deliberately NOT an `APIConnectionError`: retrying a response that will never
|
|
747
|
+
* parse just burns the retry budget, so callers that retry on connection
|
|
748
|
+
* failures should let this one through. `body` holds a short excerpt of what
|
|
749
|
+
* actually came back.
|
|
750
|
+
*/
|
|
751
|
+
declare class APIResponseValidationError extends APIError {
|
|
752
|
+
/** How much of the offending body to keep in the message. */
|
|
753
|
+
static readonly BODY_EXCERPT_CHARS = 200;
|
|
754
|
+
readonly body: string | undefined;
|
|
755
|
+
constructor(message: string, options?: {
|
|
756
|
+
status?: number;
|
|
757
|
+
body?: string;
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Raised when a stream reports a failure in-band, after HTTP 200.
|
|
762
|
+
*
|
|
763
|
+
* A streaming response commits its status line before the worker has done any
|
|
764
|
+
* work, so anything that goes wrong afterwards — the worker crashing, the
|
|
765
|
+
* request timing out, the fleet being at capacity — can only be described in
|
|
766
|
+
* the body. The API sends `data: {"error": {…}}` followed by `data: [DONE]`,
|
|
767
|
+
* and this error is how the SDK surfaces that instead of ending the stream as
|
|
768
|
+
* though it had succeeded.
|
|
769
|
+
*
|
|
770
|
+
* `requestId` and `workerId` identify the failed inference to Pendra support.
|
|
771
|
+
*
|
|
772
|
+
* Note this extends `APIError`, not `APIStatusError`, because the API omits
|
|
773
|
+
* `code` on some frames (the timeout and the generic catch-all) — so `status`
|
|
774
|
+
* is genuinely optional here, which `APIStatusError`'s always-present `status`
|
|
775
|
+
* cannot express. Check `status` for undefined before comparing it.
|
|
776
|
+
*/
|
|
777
|
+
declare class APIStreamError extends APIError {
|
|
778
|
+
readonly type: string | undefined;
|
|
779
|
+
readonly code: number | undefined;
|
|
780
|
+
readonly requestId: string | undefined;
|
|
781
|
+
readonly workerId: string | undefined;
|
|
782
|
+
readonly workerName: string | undefined;
|
|
783
|
+
constructor(message: string, options?: {
|
|
784
|
+
type?: string;
|
|
785
|
+
code?: number;
|
|
786
|
+
requestId?: string;
|
|
787
|
+
workerId?: string;
|
|
788
|
+
workerName?: string;
|
|
789
|
+
});
|
|
790
|
+
}
|
|
704
791
|
|
|
705
|
-
export { APIConnectionError, APIError, APIStatusError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionContentPart, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CompletionTokensDetails, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraExtension, type PendraNotice, type PendraOptions, type PendraTiming, type PendraWebToolStep, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
|
|
792
|
+
export { APIConnectionError, APIError, APIResponseValidationError, APIStatusError, APIStreamError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionContentPart, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CompletionTokensDetails, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraExtension, type PendraNotice, type PendraOptions, type PendraTiming, type PendraWebToolStep, 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
|
@@ -32,7 +32,9 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
APIConnectionError: () => APIConnectionError,
|
|
34
34
|
APIError: () => APIError,
|
|
35
|
+
APIResponseValidationError: () => APIResponseValidationError,
|
|
35
36
|
APIStatusError: () => APIStatusError,
|
|
37
|
+
APIStreamError: () => APIStreamError,
|
|
36
38
|
AuthenticationError: () => AuthenticationError,
|
|
37
39
|
Pendra: () => Pendra,
|
|
38
40
|
PrivateInferenceError: () => PrivateInferenceError,
|
|
@@ -75,6 +77,33 @@ var RateLimitError = class extends APIStatusError {
|
|
|
75
77
|
this.name = "RateLimitError";
|
|
76
78
|
}
|
|
77
79
|
};
|
|
80
|
+
var _APIResponseValidationError = class _APIResponseValidationError extends APIError {
|
|
81
|
+
constructor(message, options = {}) {
|
|
82
|
+
const { status, body } = options;
|
|
83
|
+
let text = message;
|
|
84
|
+
if (body) {
|
|
85
|
+
const max = _APIResponseValidationError.BODY_EXCERPT_CHARS;
|
|
86
|
+
text = `${message} (body: ${body.slice(0, max)}${body.length > max ? "\u2026" : ""})`;
|
|
87
|
+
}
|
|
88
|
+
super(text, status);
|
|
89
|
+
this.name = "APIResponseValidationError";
|
|
90
|
+
this.body = body;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/** How much of the offending body to keep in the message. */
|
|
94
|
+
_APIResponseValidationError.BODY_EXCERPT_CHARS = 200;
|
|
95
|
+
var APIResponseValidationError = _APIResponseValidationError;
|
|
96
|
+
var APIStreamError = class extends APIError {
|
|
97
|
+
constructor(message, options = {}) {
|
|
98
|
+
super(message, typeof options.code === "number" ? options.code : void 0);
|
|
99
|
+
this.name = "APIStreamError";
|
|
100
|
+
this.type = options.type;
|
|
101
|
+
this.code = options.code;
|
|
102
|
+
this.requestId = options.requestId;
|
|
103
|
+
this.workerId = options.workerId;
|
|
104
|
+
this.workerName = options.workerName;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
78
107
|
|
|
79
108
|
// src/resources/audio/transcriptions.ts
|
|
80
109
|
var MAX_FILE_SIZE = 25 * 1024 * 1024;
|
|
@@ -204,6 +233,89 @@ var Audio = class {
|
|
|
204
233
|
}
|
|
205
234
|
};
|
|
206
235
|
|
|
236
|
+
// src/_parsing.ts
|
|
237
|
+
function asString(v) {
|
|
238
|
+
return typeof v === "string" ? v : void 0;
|
|
239
|
+
}
|
|
240
|
+
function throwIfInBandError(parsed) {
|
|
241
|
+
if (typeof parsed !== "object" || parsed === null) return;
|
|
242
|
+
const err = parsed.error;
|
|
243
|
+
if (typeof err !== "object" || err === null || Array.isArray(err)) return;
|
|
244
|
+
const e = err;
|
|
245
|
+
throw new APIStreamError(
|
|
246
|
+
asString(e.message) ?? "The server reported an error mid-stream",
|
|
247
|
+
{
|
|
248
|
+
type: asString(e.type),
|
|
249
|
+
code: typeof e.code === "number" ? e.code : void 0,
|
|
250
|
+
requestId: asString(e.request_id),
|
|
251
|
+
workerId: asString(e.worker_id),
|
|
252
|
+
workerName: asString(e.worker_name)
|
|
253
|
+
}
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
function isChunkShaped(parsed) {
|
|
257
|
+
if (typeof parsed !== "object" || parsed === null) return false;
|
|
258
|
+
const c = parsed;
|
|
259
|
+
return typeof c.id === "string" && typeof c.model === "string" && Array.isArray(c.choices);
|
|
260
|
+
}
|
|
261
|
+
function parseStreamFrame(data, label = "Stream frame") {
|
|
262
|
+
let parsed;
|
|
263
|
+
try {
|
|
264
|
+
parsed = JSON.parse(data);
|
|
265
|
+
} catch {
|
|
266
|
+
throw new APIResponseValidationError(`${label} was not valid JSON`, { body: data });
|
|
267
|
+
}
|
|
268
|
+
throwIfInBandError(parsed);
|
|
269
|
+
if (!isChunkShaped(parsed)) {
|
|
270
|
+
throw new APIResponseValidationError(`${label} was not a chat completion chunk`, {
|
|
271
|
+
body: data
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return parsed;
|
|
275
|
+
}
|
|
276
|
+
function completionFrom(parsed, body, status) {
|
|
277
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
278
|
+
throw new APIResponseValidationError("Response was not a chat completion", { status, body });
|
|
279
|
+
}
|
|
280
|
+
const c = parsed;
|
|
281
|
+
if (typeof c.id !== "string" || typeof c.model !== "string" || !Array.isArray(c.choices)) {
|
|
282
|
+
throw new APIResponseValidationError("Response was not a chat completion", { status, body });
|
|
283
|
+
}
|
|
284
|
+
return parsed;
|
|
285
|
+
}
|
|
286
|
+
async function parseCompletion(response) {
|
|
287
|
+
const body = await response.text();
|
|
288
|
+
let parsed;
|
|
289
|
+
try {
|
|
290
|
+
parsed = JSON.parse(body);
|
|
291
|
+
} catch {
|
|
292
|
+
throw new APIResponseValidationError("Response was not valid JSON", {
|
|
293
|
+
status: response.status,
|
|
294
|
+
body
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return completionFrom(parsed, body, response.status);
|
|
298
|
+
}
|
|
299
|
+
function parseDecryptedCompletion(json, status) {
|
|
300
|
+
let parsed;
|
|
301
|
+
try {
|
|
302
|
+
parsed = JSON.parse(json);
|
|
303
|
+
} catch {
|
|
304
|
+
throw new APIResponseValidationError("Decrypted response was not valid JSON", {
|
|
305
|
+
status,
|
|
306
|
+
body: json
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
return completionFrom(parsed, json, status);
|
|
311
|
+
} catch {
|
|
312
|
+
throw new APIResponseValidationError("Decrypted response was not a chat completion", {
|
|
313
|
+
status,
|
|
314
|
+
body: json
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
207
319
|
// src/_sse.ts
|
|
208
320
|
var DEFAULT_EVENT = "message";
|
|
209
321
|
function fieldValue(line, name) {
|
|
@@ -293,10 +405,7 @@ var Stream = class {
|
|
|
293
405
|
this.done = true;
|
|
294
406
|
return;
|
|
295
407
|
}
|
|
296
|
-
|
|
297
|
-
yield JSON.parse(data);
|
|
298
|
-
} catch {
|
|
299
|
-
}
|
|
408
|
+
yield parseStreamFrame(data);
|
|
300
409
|
}
|
|
301
410
|
if (readerDone) {
|
|
302
411
|
this.done = true;
|
|
@@ -337,15 +446,16 @@ var EncryptedStream = class {
|
|
|
337
446
|
this.done = true;
|
|
338
447
|
return;
|
|
339
448
|
}
|
|
340
|
-
let
|
|
449
|
+
let inner;
|
|
341
450
|
try {
|
|
342
|
-
|
|
451
|
+
inner = JSON.parse(data).pendra_encrypted;
|
|
343
452
|
} catch {
|
|
344
|
-
|
|
453
|
+
throw new APIResponseValidationError("Stream frame was not valid JSON", {
|
|
454
|
+
body: data
|
|
455
|
+
});
|
|
345
456
|
}
|
|
346
|
-
const inner = parsed.pendra_encrypted;
|
|
347
457
|
if (!inner) {
|
|
348
|
-
yield
|
|
458
|
+
yield parseStreamFrame(data);
|
|
349
459
|
continue;
|
|
350
460
|
}
|
|
351
461
|
const plain = this.decryptor.openChunk(inner);
|
|
@@ -357,10 +467,7 @@ var EncryptedStream = class {
|
|
|
357
467
|
}
|
|
358
468
|
continue;
|
|
359
469
|
}
|
|
360
|
-
|
|
361
|
-
yield JSON.parse(chunkJson);
|
|
362
|
-
} catch {
|
|
363
|
-
}
|
|
470
|
+
yield parseStreamFrame(chunkJson, "Decrypted stream frame");
|
|
364
471
|
}
|
|
365
472
|
if (readerDone) {
|
|
366
473
|
this.done = true;
|
|
@@ -405,7 +512,7 @@ var Completions = class {
|
|
|
405
512
|
"/api/v1/chat/completions",
|
|
406
513
|
{ method: "POST", body, headers }
|
|
407
514
|
);
|
|
408
|
-
return
|
|
515
|
+
return parseCompletion(response);
|
|
409
516
|
}
|
|
410
517
|
async createEncrypted(params, body, headers) {
|
|
411
518
|
const engine = await this.client._privateEngine();
|
|
@@ -428,12 +535,21 @@ var Completions = class {
|
|
|
428
535
|
"/api/v1/chat/completions",
|
|
429
536
|
{ method: "POST", body: topLevel, headers }
|
|
430
537
|
);
|
|
431
|
-
const
|
|
538
|
+
const raw = await response.text();
|
|
539
|
+
let wrapper;
|
|
540
|
+
try {
|
|
541
|
+
wrapper = JSON.parse(raw);
|
|
542
|
+
} catch {
|
|
543
|
+
throw new APIResponseValidationError("Response was not valid JSON", {
|
|
544
|
+
status: response.status,
|
|
545
|
+
body: raw
|
|
546
|
+
});
|
|
547
|
+
}
|
|
432
548
|
if (!wrapper.pendra_encrypted) {
|
|
433
|
-
return
|
|
549
|
+
return parseDecryptedCompletion(raw, response.status);
|
|
434
550
|
}
|
|
435
551
|
const plain = decryptor.openResult(wrapper.pendra_encrypted);
|
|
436
|
-
return
|
|
552
|
+
return parseDecryptedCompletion(DECODER2.decode(plain), response.status);
|
|
437
553
|
}
|
|
438
554
|
};
|
|
439
555
|
|
|
@@ -666,6 +782,10 @@ var Rerank = class {
|
|
|
666
782
|
}
|
|
667
783
|
};
|
|
668
784
|
|
|
785
|
+
// src/version.ts
|
|
786
|
+
var VERSION = "0.13.0";
|
|
787
|
+
var USER_AGENT = `pendra-node/${VERSION}`;
|
|
788
|
+
|
|
669
789
|
// src/_hpke.ts
|
|
670
790
|
var import_ed25519 = require("@noble/curves/ed25519.js");
|
|
671
791
|
var import_chacha = require("@noble/ciphers/chacha.js");
|
|
@@ -1059,6 +1179,27 @@ var PrivateInferenceEngine = class {
|
|
|
1059
1179
|
const digest = sha256Hex(new TextEncoder().encode(this.apiKey)).slice(0, 16);
|
|
1060
1180
|
return `${this.baseUrl}|${digest}`;
|
|
1061
1181
|
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Drop the cached key set so the next call re-fetches it.
|
|
1184
|
+
*
|
|
1185
|
+
* The cache is otherwise held for the whole process lifetime, which is
|
|
1186
|
+
* correct while a fleet keeps one key but fatal when it re-keys: a
|
|
1187
|
+
* long-running server would go on sealing to a fingerprint no worker holds
|
|
1188
|
+
* and fail EVERY request until someone restarted it. The API signals a re-key
|
|
1189
|
+
* with `X-Pendra-Error-Code: no_private_inference_worker`, and this is what
|
|
1190
|
+
* lets the client recover on its own.
|
|
1191
|
+
*
|
|
1192
|
+
* Pinned clients are deliberately NOT re-fetched on that signal — a pin is an
|
|
1193
|
+
* allowlist, so a fetch could only return fingerprints the pin already
|
|
1194
|
+
* rejects. They surface the error instead, which is what pinning means.
|
|
1195
|
+
*/
|
|
1196
|
+
invalidateOrgKey() {
|
|
1197
|
+
this.orgPubs = null;
|
|
1198
|
+
}
|
|
1199
|
+
/** Whether this client seals only to an explicit fingerprint allowlist. */
|
|
1200
|
+
get isPinned() {
|
|
1201
|
+
return this.pinnedFingerprints !== null;
|
|
1202
|
+
}
|
|
1062
1203
|
/**
|
|
1063
1204
|
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
1064
1205
|
* allowlist (or auto-trust TOFU), cache the accepted public keys, and return
|
|
@@ -1071,7 +1212,7 @@ var PrivateInferenceEngine = class {
|
|
|
1071
1212
|
if (this.orgPubs !== null) return this.orgPubs;
|
|
1072
1213
|
if (!fetchedKeys || fetchedKeys.length === 0) {
|
|
1073
1214
|
throw new PrivateInferenceError(
|
|
1074
|
-
"No
|
|
1215
|
+
"No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
|
|
1075
1216
|
);
|
|
1076
1217
|
}
|
|
1077
1218
|
const pubs = [];
|
|
@@ -1097,7 +1238,7 @@ var PrivateInferenceEngine = class {
|
|
|
1097
1238
|
if (accepted.length === 0) {
|
|
1098
1239
|
const pinned = Array.from(this.pinnedFingerprints).sort().join(", ");
|
|
1099
1240
|
throw new PrivateInferenceError(
|
|
1100
|
-
`None of the org's workload key fingerprints (${fps.join(", ")}) match your pinned fingerprint(s) (${pinned}). Refusing to seal (possible man-in-the-middle).
|
|
1241
|
+
`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.`
|
|
1101
1242
|
);
|
|
1102
1243
|
}
|
|
1103
1244
|
} else {
|
|
@@ -1271,7 +1412,7 @@ var Pendra = class {
|
|
|
1271
1412
|
try {
|
|
1272
1413
|
response = await fetch(url, {
|
|
1273
1414
|
method: "GET",
|
|
1274
|
-
headers: { Authorization: `Bearer ${this.apiKey}
|
|
1415
|
+
headers: { Authorization: `Bearer ${this.apiKey}`, "User-Agent": USER_AGENT },
|
|
1275
1416
|
signal: controller.signal
|
|
1276
1417
|
});
|
|
1277
1418
|
} catch (err) {
|
|
@@ -1286,7 +1427,7 @@ var Pendra = class {
|
|
|
1286
1427
|
}
|
|
1287
1428
|
if (response.status === 404) {
|
|
1288
1429
|
throw new PrivateInferenceError(
|
|
1289
|
-
"No
|
|
1430
|
+
"No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
|
|
1290
1431
|
);
|
|
1291
1432
|
}
|
|
1292
1433
|
if (!response.ok) {
|
|
@@ -1311,6 +1452,7 @@ var Pendra = class {
|
|
|
1311
1452
|
const headers = {
|
|
1312
1453
|
Authorization: `Bearer ${this.apiKey}`,
|
|
1313
1454
|
"Content-Type": "application/json",
|
|
1455
|
+
"User-Agent": USER_AGENT,
|
|
1314
1456
|
...options.headers ?? {}
|
|
1315
1457
|
};
|
|
1316
1458
|
let response;
|
|
@@ -1337,6 +1479,7 @@ var Pendra = class {
|
|
|
1337
1479
|
}
|
|
1338
1480
|
if (!response.ok) {
|
|
1339
1481
|
clearTimeout(timeoutId);
|
|
1482
|
+
this.invalidateKeyCacheIfRekeyed(response);
|
|
1340
1483
|
let message;
|
|
1341
1484
|
try {
|
|
1342
1485
|
const body = await response.json();
|
|
@@ -1350,6 +1493,26 @@ var Pendra = class {
|
|
|
1350
1493
|
}
|
|
1351
1494
|
return { response, controller };
|
|
1352
1495
|
}
|
|
1496
|
+
/**
|
|
1497
|
+
* Drop the cached encryption keys when the API reports that no worker holds
|
|
1498
|
+
* the one this request was sealed to — the signal that the org's fleet has
|
|
1499
|
+
* re-keyed. Returns true if the cache was dropped.
|
|
1500
|
+
*
|
|
1501
|
+
* A PINNED client is left alone on purpose: a pin is an allowlist, so
|
|
1502
|
+
* re-fetching could only return fingerprints the pin already rejects and
|
|
1503
|
+
* would loop rather than recover. The error surfaces instead, which is
|
|
1504
|
+
* exactly what pinning is for.
|
|
1505
|
+
*/
|
|
1506
|
+
invalidateKeyCacheIfRekeyed(response) {
|
|
1507
|
+
const engine = this._piEngine;
|
|
1508
|
+
if (!engine || engine.isPinned) return false;
|
|
1509
|
+
if (response.headers?.get("X-Pendra-Error-Code") !== "no_private_inference_worker") {
|
|
1510
|
+
return false;
|
|
1511
|
+
}
|
|
1512
|
+
engine.invalidateOrgKey();
|
|
1513
|
+
this._piKeyFetch = null;
|
|
1514
|
+
return true;
|
|
1515
|
+
}
|
|
1353
1516
|
};
|
|
1354
1517
|
|
|
1355
1518
|
// src/index.ts
|
|
@@ -1358,7 +1521,9 @@ var index_default = Pendra;
|
|
|
1358
1521
|
0 && (module.exports = {
|
|
1359
1522
|
APIConnectionError,
|
|
1360
1523
|
APIError,
|
|
1524
|
+
APIResponseValidationError,
|
|
1361
1525
|
APIStatusError,
|
|
1526
|
+
APIStreamError,
|
|
1362
1527
|
AuthenticationError,
|
|
1363
1528
|
Pendra,
|
|
1364
1529
|
PrivateInferenceError,
|