pendra 0.12.2 → 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 +60 -1
- package/dist/index.d.ts +60 -1
- package/dist/index.js +136 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -8
package/dist/index.mjs
CHANGED
|
@@ -31,6 +31,33 @@ var RateLimitError = class extends APIStatusError {
|
|
|
31
31
|
this.name = "RateLimitError";
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
|
+
var _APIResponseValidationError = class _APIResponseValidationError extends APIError {
|
|
35
|
+
constructor(message, options = {}) {
|
|
36
|
+
const { status, body } = options;
|
|
37
|
+
let text = message;
|
|
38
|
+
if (body) {
|
|
39
|
+
const max = _APIResponseValidationError.BODY_EXCERPT_CHARS;
|
|
40
|
+
text = `${message} (body: ${body.slice(0, max)}${body.length > max ? "\u2026" : ""})`;
|
|
41
|
+
}
|
|
42
|
+
super(text, status);
|
|
43
|
+
this.name = "APIResponseValidationError";
|
|
44
|
+
this.body = body;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/** How much of the offending body to keep in the message. */
|
|
48
|
+
_APIResponseValidationError.BODY_EXCERPT_CHARS = 200;
|
|
49
|
+
var APIResponseValidationError = _APIResponseValidationError;
|
|
50
|
+
var APIStreamError = class extends APIError {
|
|
51
|
+
constructor(message, options = {}) {
|
|
52
|
+
super(message, typeof options.code === "number" ? options.code : void 0);
|
|
53
|
+
this.name = "APIStreamError";
|
|
54
|
+
this.type = options.type;
|
|
55
|
+
this.code = options.code;
|
|
56
|
+
this.requestId = options.requestId;
|
|
57
|
+
this.workerId = options.workerId;
|
|
58
|
+
this.workerName = options.workerName;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
34
61
|
|
|
35
62
|
// src/resources/audio/transcriptions.ts
|
|
36
63
|
var MAX_FILE_SIZE = 25 * 1024 * 1024;
|
|
@@ -160,6 +187,89 @@ var Audio = class {
|
|
|
160
187
|
}
|
|
161
188
|
};
|
|
162
189
|
|
|
190
|
+
// src/_parsing.ts
|
|
191
|
+
function asString(v) {
|
|
192
|
+
return typeof v === "string" ? v : void 0;
|
|
193
|
+
}
|
|
194
|
+
function throwIfInBandError(parsed) {
|
|
195
|
+
if (typeof parsed !== "object" || parsed === null) return;
|
|
196
|
+
const err = parsed.error;
|
|
197
|
+
if (typeof err !== "object" || err === null || Array.isArray(err)) return;
|
|
198
|
+
const e = err;
|
|
199
|
+
throw new APIStreamError(
|
|
200
|
+
asString(e.message) ?? "The server reported an error mid-stream",
|
|
201
|
+
{
|
|
202
|
+
type: asString(e.type),
|
|
203
|
+
code: typeof e.code === "number" ? e.code : void 0,
|
|
204
|
+
requestId: asString(e.request_id),
|
|
205
|
+
workerId: asString(e.worker_id),
|
|
206
|
+
workerName: asString(e.worker_name)
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
function isChunkShaped(parsed) {
|
|
211
|
+
if (typeof parsed !== "object" || parsed === null) return false;
|
|
212
|
+
const c = parsed;
|
|
213
|
+
return typeof c.id === "string" && typeof c.model === "string" && Array.isArray(c.choices);
|
|
214
|
+
}
|
|
215
|
+
function parseStreamFrame(data, label = "Stream frame") {
|
|
216
|
+
let parsed;
|
|
217
|
+
try {
|
|
218
|
+
parsed = JSON.parse(data);
|
|
219
|
+
} catch {
|
|
220
|
+
throw new APIResponseValidationError(`${label} was not valid JSON`, { body: data });
|
|
221
|
+
}
|
|
222
|
+
throwIfInBandError(parsed);
|
|
223
|
+
if (!isChunkShaped(parsed)) {
|
|
224
|
+
throw new APIResponseValidationError(`${label} was not a chat completion chunk`, {
|
|
225
|
+
body: data
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
return parsed;
|
|
229
|
+
}
|
|
230
|
+
function completionFrom(parsed, body, status) {
|
|
231
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
232
|
+
throw new APIResponseValidationError("Response was not a chat completion", { status, body });
|
|
233
|
+
}
|
|
234
|
+
const c = parsed;
|
|
235
|
+
if (typeof c.id !== "string" || typeof c.model !== "string" || !Array.isArray(c.choices)) {
|
|
236
|
+
throw new APIResponseValidationError("Response was not a chat completion", { status, body });
|
|
237
|
+
}
|
|
238
|
+
return parsed;
|
|
239
|
+
}
|
|
240
|
+
async function parseCompletion(response) {
|
|
241
|
+
const body = await response.text();
|
|
242
|
+
let parsed;
|
|
243
|
+
try {
|
|
244
|
+
parsed = JSON.parse(body);
|
|
245
|
+
} catch {
|
|
246
|
+
throw new APIResponseValidationError("Response was not valid JSON", {
|
|
247
|
+
status: response.status,
|
|
248
|
+
body
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return completionFrom(parsed, body, response.status);
|
|
252
|
+
}
|
|
253
|
+
function parseDecryptedCompletion(json, status) {
|
|
254
|
+
let parsed;
|
|
255
|
+
try {
|
|
256
|
+
parsed = JSON.parse(json);
|
|
257
|
+
} catch {
|
|
258
|
+
throw new APIResponseValidationError("Decrypted response was not valid JSON", {
|
|
259
|
+
status,
|
|
260
|
+
body: json
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
return completionFrom(parsed, json, status);
|
|
265
|
+
} catch {
|
|
266
|
+
throw new APIResponseValidationError("Decrypted response was not a chat completion", {
|
|
267
|
+
status,
|
|
268
|
+
body: json
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
163
273
|
// src/_sse.ts
|
|
164
274
|
var DEFAULT_EVENT = "message";
|
|
165
275
|
function fieldValue(line, name) {
|
|
@@ -249,10 +359,7 @@ var Stream = class {
|
|
|
249
359
|
this.done = true;
|
|
250
360
|
return;
|
|
251
361
|
}
|
|
252
|
-
|
|
253
|
-
yield JSON.parse(data);
|
|
254
|
-
} catch {
|
|
255
|
-
}
|
|
362
|
+
yield parseStreamFrame(data);
|
|
256
363
|
}
|
|
257
364
|
if (readerDone) {
|
|
258
365
|
this.done = true;
|
|
@@ -293,15 +400,16 @@ var EncryptedStream = class {
|
|
|
293
400
|
this.done = true;
|
|
294
401
|
return;
|
|
295
402
|
}
|
|
296
|
-
let
|
|
403
|
+
let inner;
|
|
297
404
|
try {
|
|
298
|
-
|
|
405
|
+
inner = JSON.parse(data).pendra_encrypted;
|
|
299
406
|
} catch {
|
|
300
|
-
|
|
407
|
+
throw new APIResponseValidationError("Stream frame was not valid JSON", {
|
|
408
|
+
body: data
|
|
409
|
+
});
|
|
301
410
|
}
|
|
302
|
-
const inner = parsed.pendra_encrypted;
|
|
303
411
|
if (!inner) {
|
|
304
|
-
yield
|
|
412
|
+
yield parseStreamFrame(data);
|
|
305
413
|
continue;
|
|
306
414
|
}
|
|
307
415
|
const plain = this.decryptor.openChunk(inner);
|
|
@@ -313,10 +421,7 @@ var EncryptedStream = class {
|
|
|
313
421
|
}
|
|
314
422
|
continue;
|
|
315
423
|
}
|
|
316
|
-
|
|
317
|
-
yield JSON.parse(chunkJson);
|
|
318
|
-
} catch {
|
|
319
|
-
}
|
|
424
|
+
yield parseStreamFrame(chunkJson, "Decrypted stream frame");
|
|
320
425
|
}
|
|
321
426
|
if (readerDone) {
|
|
322
427
|
this.done = true;
|
|
@@ -361,7 +466,7 @@ var Completions = class {
|
|
|
361
466
|
"/api/v1/chat/completions",
|
|
362
467
|
{ method: "POST", body, headers }
|
|
363
468
|
);
|
|
364
|
-
return
|
|
469
|
+
return parseCompletion(response);
|
|
365
470
|
}
|
|
366
471
|
async createEncrypted(params, body, headers) {
|
|
367
472
|
const engine = await this.client._privateEngine();
|
|
@@ -384,12 +489,21 @@ var Completions = class {
|
|
|
384
489
|
"/api/v1/chat/completions",
|
|
385
490
|
{ method: "POST", body: topLevel, headers }
|
|
386
491
|
);
|
|
387
|
-
const
|
|
492
|
+
const raw = await response.text();
|
|
493
|
+
let wrapper;
|
|
494
|
+
try {
|
|
495
|
+
wrapper = JSON.parse(raw);
|
|
496
|
+
} catch {
|
|
497
|
+
throw new APIResponseValidationError("Response was not valid JSON", {
|
|
498
|
+
status: response.status,
|
|
499
|
+
body: raw
|
|
500
|
+
});
|
|
501
|
+
}
|
|
388
502
|
if (!wrapper.pendra_encrypted) {
|
|
389
|
-
return
|
|
503
|
+
return parseDecryptedCompletion(raw, response.status);
|
|
390
504
|
}
|
|
391
505
|
const plain = decryptor.openResult(wrapper.pendra_encrypted);
|
|
392
|
-
return
|
|
506
|
+
return parseDecryptedCompletion(DECODER2.decode(plain), response.status);
|
|
393
507
|
}
|
|
394
508
|
};
|
|
395
509
|
|
|
@@ -623,7 +737,7 @@ var Rerank = class {
|
|
|
623
737
|
};
|
|
624
738
|
|
|
625
739
|
// src/version.ts
|
|
626
|
-
var VERSION = "0.
|
|
740
|
+
var VERSION = "0.13.0";
|
|
627
741
|
var USER_AGENT = `pendra-node/${VERSION}`;
|
|
628
742
|
|
|
629
743
|
// src/_hpke.ts
|
|
@@ -1360,7 +1474,9 @@ var index_default = Pendra;
|
|
|
1360
1474
|
export {
|
|
1361
1475
|
APIConnectionError,
|
|
1362
1476
|
APIError,
|
|
1477
|
+
APIResponseValidationError,
|
|
1363
1478
|
APIStatusError,
|
|
1479
|
+
APIStreamError,
|
|
1364
1480
|
AuthenticationError,
|
|
1365
1481
|
Pendra,
|
|
1366
1482
|
PrivateInferenceError,
|