pendra 0.20.0 → 0.21.1
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/README.md +8 -3
- package/dist/index.d.mts +25 -4
- package/dist/index.d.ts +25 -4
- package/dist/index.js +254 -176
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +254 -176
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -31,8 +31,18 @@ var RateLimitError = class extends APIStatusError {
|
|
|
31
31
|
constructor(message = "Rate limit exceeded", options = {}) {
|
|
32
32
|
super(message, 429, options);
|
|
33
33
|
this.name = "RateLimitError";
|
|
34
|
+
this.retryAfter = options.retryAfter;
|
|
35
|
+
this.queueDepth = options.queueDepth;
|
|
34
36
|
}
|
|
35
37
|
};
|
|
38
|
+
function secondsFrom(...candidates) {
|
|
39
|
+
for (const value of candidates) {
|
|
40
|
+
if (typeof value !== "number" && (typeof value !== "string" || value.trim() === "")) continue;
|
|
41
|
+
const n = Number(value);
|
|
42
|
+
if (Number.isFinite(n) && n >= 0) return n;
|
|
43
|
+
}
|
|
44
|
+
return void 0;
|
|
45
|
+
}
|
|
36
46
|
var _APIResponseValidationError = class _APIResponseValidationError extends APIError {
|
|
37
47
|
constructor(message, options = {}) {
|
|
38
48
|
const { status, body } = options;
|
|
@@ -58,6 +68,7 @@ var APIStreamError = class extends APIError {
|
|
|
58
68
|
this.requestId = options.requestId;
|
|
59
69
|
this.workerId = options.workerId;
|
|
60
70
|
this.workerName = options.workerName;
|
|
71
|
+
this.retryAfter = options.retryAfter;
|
|
61
72
|
}
|
|
62
73
|
};
|
|
63
74
|
var LOC_SOURCES = /* @__PURE__ */ new Set(["body", "query", "path", "header", "cookie"]);
|
|
@@ -119,7 +130,7 @@ async function readErrorBody(response) {
|
|
|
119
130
|
return raw;
|
|
120
131
|
}
|
|
121
132
|
}
|
|
122
|
-
function statusErrorFromBody(status, body, fallback) {
|
|
133
|
+
function statusErrorFromBody(status, body, fallback, headers) {
|
|
123
134
|
const detail = isRecord(body) ? body.detail : void 0;
|
|
124
135
|
const err = isRecord(body) ? body.error : void 0;
|
|
125
136
|
let message = (isRecord(err) ? nonEmptyString(err.message) : void 0) ?? messageFromDetail(detail) ?? (isRecord(body) ? nonEmptyString(body.message) : void 0);
|
|
@@ -131,7 +142,13 @@ function statusErrorFromBody(status, body, fallback) {
|
|
|
131
142
|
}
|
|
132
143
|
const options = { body, detail };
|
|
133
144
|
if (status === 401) return new AuthenticationError(message, options);
|
|
134
|
-
if (status === 429)
|
|
145
|
+
if (status === 429) {
|
|
146
|
+
return new RateLimitError(message, {
|
|
147
|
+
...options,
|
|
148
|
+
retryAfter: secondsFrom(headers?.get("Retry-After"), isRecord(err) ? err.retry_after : void 0),
|
|
149
|
+
queueDepth: secondsFrom(headers?.get("X-Pendra-Queue-Depth"), isRecord(err) ? err.queue_depth : void 0)
|
|
150
|
+
});
|
|
151
|
+
}
|
|
135
152
|
return new APIStatusError(message, status, options);
|
|
136
153
|
}
|
|
137
154
|
|
|
@@ -533,7 +550,8 @@ function throwIfInBandError(parsed, fallback = "The server reported an error mid
|
|
|
533
550
|
code: typeof e.code === "number" ? e.code : void 0,
|
|
534
551
|
requestId: asString(e.request_id),
|
|
535
552
|
workerId: asString(e.worker_id),
|
|
536
|
-
workerName: asString(e.worker_name)
|
|
553
|
+
workerName: asString(e.worker_name),
|
|
554
|
+
retryAfter: secondsFrom(e.retry_after)
|
|
537
555
|
}
|
|
538
556
|
);
|
|
539
557
|
}
|
|
@@ -746,6 +764,18 @@ function defaultTofuPath() {
|
|
|
746
764
|
const home = env?.HOME || env?.USERPROFILE || ".";
|
|
747
765
|
return `${home.replace(/\/$/, "")}/.pendra/known_org_keys.json`;
|
|
748
766
|
}
|
|
767
|
+
function isFsError(e) {
|
|
768
|
+
const err = e;
|
|
769
|
+
return e instanceof Error && typeof err.code === "string" && typeof err.syscall === "string";
|
|
770
|
+
}
|
|
771
|
+
var warnedTofuPaths = /* @__PURE__ */ new Set();
|
|
772
|
+
function warnTofuUnwritable(tofuPath, e) {
|
|
773
|
+
if (warnedTofuPaths.has(tofuPath)) return;
|
|
774
|
+
warnedTofuPaths.add(tofuPath);
|
|
775
|
+
console.warn(
|
|
776
|
+
`pendra: could not update the private-inference trust store at ${tofuPath} (${e.message}). Continuing: the store only records trusted key fingerprints and requests are still encrypted. Set PENDRA_TOFU_PATH to a writable file to silence this warning.`
|
|
777
|
+
);
|
|
778
|
+
}
|
|
749
779
|
function b64d2(s) {
|
|
750
780
|
if (typeof Buffer !== "undefined") return new Uint8Array(Buffer.from(s, "base64"));
|
|
751
781
|
const binary = atob(s);
|
|
@@ -837,7 +867,12 @@ var PrivateInferenceEngine = class {
|
|
|
837
867
|
);
|
|
838
868
|
}
|
|
839
869
|
} else {
|
|
840
|
-
|
|
870
|
+
try {
|
|
871
|
+
await this.tofuTrust(fps);
|
|
872
|
+
} catch (e) {
|
|
873
|
+
if (!isFsError(e)) throw e;
|
|
874
|
+
warnTofuUnwritable(this.tofuPath, e);
|
|
875
|
+
}
|
|
841
876
|
accepted = pubs;
|
|
842
877
|
}
|
|
843
878
|
this.orgPubs = accepted;
|
|
@@ -861,14 +896,21 @@ var PrivateInferenceEngine = class {
|
|
|
861
896
|
}
|
|
862
897
|
}
|
|
863
898
|
async loadTofu() {
|
|
899
|
+
const fs = await import("fs");
|
|
900
|
+
let raw;
|
|
901
|
+
try {
|
|
902
|
+
raw = fs.readFileSync(this.tofuPath, "utf-8");
|
|
903
|
+
} catch (e) {
|
|
904
|
+
if (e.code === "ENOENT") return {};
|
|
905
|
+
throw e;
|
|
906
|
+
}
|
|
907
|
+
let data;
|
|
864
908
|
try {
|
|
865
|
-
|
|
866
|
-
const raw = fs.readFileSync(this.tofuPath, "utf-8");
|
|
867
|
-
const data = JSON.parse(raw);
|
|
868
|
-
return data && typeof data === "object" && !Array.isArray(data) ? data : {};
|
|
909
|
+
data = JSON.parse(raw);
|
|
869
910
|
} catch {
|
|
870
911
|
return {};
|
|
871
912
|
}
|
|
913
|
+
return data && typeof data === "object" && !Array.isArray(data) ? data : {};
|
|
872
914
|
}
|
|
873
915
|
async saveTofu(store) {
|
|
874
916
|
const fs = await import("fs");
|
|
@@ -1019,6 +1061,201 @@ var Speech = class {
|
|
|
1019
1061
|
}
|
|
1020
1062
|
};
|
|
1021
1063
|
|
|
1064
|
+
// src/_sse.ts
|
|
1065
|
+
var DEFAULT_EVENT = "message";
|
|
1066
|
+
function fieldValue(line, name) {
|
|
1067
|
+
const value = line.slice(name.length + 1);
|
|
1068
|
+
return value.startsWith(" ") ? value.slice(1) : value;
|
|
1069
|
+
}
|
|
1070
|
+
var SseDecoder = class {
|
|
1071
|
+
constructor() {
|
|
1072
|
+
this.buffer = "";
|
|
1073
|
+
this.event = "";
|
|
1074
|
+
this.data = [];
|
|
1075
|
+
}
|
|
1076
|
+
/** Feed a chunk of decoded text; returns the payloads it completed. */
|
|
1077
|
+
push(text) {
|
|
1078
|
+
this.buffer += text;
|
|
1079
|
+
const lines = this.buffer.split("\n");
|
|
1080
|
+
this.buffer = lines.pop() ?? "";
|
|
1081
|
+
const out = [];
|
|
1082
|
+
for (const line of lines) {
|
|
1083
|
+
const payload = this.pushLine(line);
|
|
1084
|
+
if (payload !== null) out.push(payload);
|
|
1085
|
+
}
|
|
1086
|
+
return out;
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Release what the stream ended without terminating: the held-back partial
|
|
1090
|
+
* line, plus any frame that never got its blank line.
|
|
1091
|
+
*
|
|
1092
|
+
* The spec discards an unterminated final block; we keep it, so a connection
|
|
1093
|
+
* that drops after the last `data:` line but before its blank line still
|
|
1094
|
+
* delivers that payload rather than silently losing it.
|
|
1095
|
+
*/
|
|
1096
|
+
flush() {
|
|
1097
|
+
const out = [];
|
|
1098
|
+
if (this.buffer !== "") {
|
|
1099
|
+
const payload = this.pushLine(this.buffer);
|
|
1100
|
+
this.buffer = "";
|
|
1101
|
+
if (payload !== null) out.push(payload);
|
|
1102
|
+
}
|
|
1103
|
+
const tail = this.endFrame();
|
|
1104
|
+
if (tail !== null) out.push(tail);
|
|
1105
|
+
return out;
|
|
1106
|
+
}
|
|
1107
|
+
pushLine(rawLine) {
|
|
1108
|
+
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
|
|
1109
|
+
if (line === "") return this.endFrame();
|
|
1110
|
+
if (line.startsWith(":")) return null;
|
|
1111
|
+
if (line.startsWith("event:")) {
|
|
1112
|
+
this.event = fieldValue(line, "event");
|
|
1113
|
+
return null;
|
|
1114
|
+
}
|
|
1115
|
+
if (line.startsWith("data:")) {
|
|
1116
|
+
this.data.push(fieldValue(line, "data"));
|
|
1117
|
+
return null;
|
|
1118
|
+
}
|
|
1119
|
+
return null;
|
|
1120
|
+
}
|
|
1121
|
+
endFrame() {
|
|
1122
|
+
const { data, event } = this;
|
|
1123
|
+
this.data = [];
|
|
1124
|
+
this.event = "";
|
|
1125
|
+
if (data.length === 0) return null;
|
|
1126
|
+
if (event !== "" && event !== DEFAULT_EVENT) return null;
|
|
1127
|
+
return data.join("\n");
|
|
1128
|
+
}
|
|
1129
|
+
};
|
|
1130
|
+
|
|
1131
|
+
// src/streaming.ts
|
|
1132
|
+
var Stream = class {
|
|
1133
|
+
constructor(response, controller, idleTimeoutMs) {
|
|
1134
|
+
this.decoder = new TextDecoder();
|
|
1135
|
+
this.sse = new SseDecoder();
|
|
1136
|
+
this.done = false;
|
|
1137
|
+
this.placement = new PlacementTracker();
|
|
1138
|
+
if (!response.body) {
|
|
1139
|
+
throw new Error("Response body is null \u2014 streaming not supported");
|
|
1140
|
+
}
|
|
1141
|
+
this.reader = response.body.getReader();
|
|
1142
|
+
this.controller = controller;
|
|
1143
|
+
this.idleTimeoutMs = idleTimeoutMs;
|
|
1144
|
+
}
|
|
1145
|
+
async *[Symbol.asyncIterator]() {
|
|
1146
|
+
try {
|
|
1147
|
+
while (!this.done) {
|
|
1148
|
+
const { value, done: readerDone } = await readWithIdleTimeout(
|
|
1149
|
+
this.reader,
|
|
1150
|
+
this.idleTimeoutMs
|
|
1151
|
+
);
|
|
1152
|
+
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
1153
|
+
for (const data of payloads) {
|
|
1154
|
+
if (data === "[DONE]") {
|
|
1155
|
+
this.done = true;
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
yield this.placement.observe(parseStreamFrame(data));
|
|
1159
|
+
}
|
|
1160
|
+
if (readerDone) {
|
|
1161
|
+
this.done = true;
|
|
1162
|
+
break;
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
} finally {
|
|
1166
|
+
releaseReader(this.reader, this.controller, this.done);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* The worker that served this stream, or `null` until the chunk reporting it
|
|
1171
|
+
* — the trailer, just before `[DONE]` — has been iterated (and on an API too
|
|
1172
|
+
* old to report it). Same as that chunk's `pendra.worker`.
|
|
1173
|
+
*/
|
|
1174
|
+
get worker() {
|
|
1175
|
+
return this.placement.worker;
|
|
1176
|
+
}
|
|
1177
|
+
/**
|
|
1178
|
+
* Pendra's id for this request, reported alongside {@link Stream.worker};
|
|
1179
|
+
* `null` until then.
|
|
1180
|
+
*/
|
|
1181
|
+
get requestId() {
|
|
1182
|
+
return this.placement.requestId;
|
|
1183
|
+
}
|
|
1184
|
+
abort() {
|
|
1185
|
+
this.done = true;
|
|
1186
|
+
this.controller.abort();
|
|
1187
|
+
}
|
|
1188
|
+
};
|
|
1189
|
+
var PlacementTracker = class {
|
|
1190
|
+
constructor() {
|
|
1191
|
+
this.worker = null;
|
|
1192
|
+
this.requestId = null;
|
|
1193
|
+
}
|
|
1194
|
+
/** Record `chunk`'s placement, if it carries any, and hand it back. */
|
|
1195
|
+
observe(chunk) {
|
|
1196
|
+
const { worker, requestId } = chunkPlacement(chunk);
|
|
1197
|
+
if (worker) this.worker = worker;
|
|
1198
|
+
if (requestId) this.requestId = requestId;
|
|
1199
|
+
return chunk;
|
|
1200
|
+
}
|
|
1201
|
+
};
|
|
1202
|
+
async function readWithIdleTimeout(reader, idleTimeoutMs, what = "Stream") {
|
|
1203
|
+
if (!idleTimeoutMs) return reader.read();
|
|
1204
|
+
let timer;
|
|
1205
|
+
const idle = new Promise((_, reject) => {
|
|
1206
|
+
timer = setTimeout(
|
|
1207
|
+
() => reject(new APIConnectionError(`${what} received no data for ${idleTimeoutMs}ms`)),
|
|
1208
|
+
idleTimeoutMs
|
|
1209
|
+
);
|
|
1210
|
+
});
|
|
1211
|
+
try {
|
|
1212
|
+
return await Promise.race([reader.read(), idle]);
|
|
1213
|
+
} finally {
|
|
1214
|
+
clearTimeout(timer);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
function withIdleTimeout(response, controller, idleTimeoutMs) {
|
|
1218
|
+
const body = response.body;
|
|
1219
|
+
if (!idleTimeoutMs || !body || typeof body.getReader !== "function") return response;
|
|
1220
|
+
if (response.status < 200 || response.status > 599) return response;
|
|
1221
|
+
const reader = body.getReader();
|
|
1222
|
+
const guarded = new ReadableStream(
|
|
1223
|
+
{
|
|
1224
|
+
async pull(sink) {
|
|
1225
|
+
const chunk = await readWithIdleTimeout(reader, idleTimeoutMs, "Response body").catch(
|
|
1226
|
+
(err) => {
|
|
1227
|
+
controller.abort();
|
|
1228
|
+
reader.cancel().catch(() => {
|
|
1229
|
+
});
|
|
1230
|
+
throw err;
|
|
1231
|
+
}
|
|
1232
|
+
);
|
|
1233
|
+
if (chunk.done) sink.close();
|
|
1234
|
+
else sink.enqueue(chunk.value);
|
|
1235
|
+
},
|
|
1236
|
+
cancel(reason) {
|
|
1237
|
+
controller.abort();
|
|
1238
|
+
return reader.cancel(reason);
|
|
1239
|
+
}
|
|
1240
|
+
},
|
|
1241
|
+
// Read only on demand, so no idle timer runs while nobody is reading.
|
|
1242
|
+
{ highWaterMark: 0 }
|
|
1243
|
+
);
|
|
1244
|
+
return new Response(guarded, {
|
|
1245
|
+
status: response.status,
|
|
1246
|
+
statusText: response.statusText,
|
|
1247
|
+
headers: response.headers
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
function releaseReader(reader, controller, finished) {
|
|
1251
|
+
if (!finished) {
|
|
1252
|
+
controller.abort();
|
|
1253
|
+
reader.cancel().catch(() => {
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1256
|
+
reader.releaseLock();
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1022
1259
|
// src/resources/audio/transcriptions.ts
|
|
1023
1260
|
var ENCODER = new TextEncoder();
|
|
1024
1261
|
var DECODER = new TextDecoder();
|
|
@@ -1121,9 +1358,10 @@ var Transcriptions = class {
|
|
|
1121
1358
|
throw new APIConnectionError(String(err));
|
|
1122
1359
|
}
|
|
1123
1360
|
clearTimeout(timeoutId);
|
|
1361
|
+
response = withIdleTimeout(response, controller, timeout);
|
|
1124
1362
|
if (response.status >= 400) {
|
|
1125
1363
|
const body = await readErrorBody(response);
|
|
1126
|
-
throw statusErrorFromBody(response.status, body, response.statusText);
|
|
1364
|
+
throw statusErrorFromBody(response.status, body, response.statusText, response.headers);
|
|
1127
1365
|
}
|
|
1128
1366
|
const format = params.response_format;
|
|
1129
1367
|
if (format === "text" || format === "srt" || format === "vtt") {
|
|
@@ -1192,168 +1430,6 @@ var Audio = class {
|
|
|
1192
1430
|
}
|
|
1193
1431
|
};
|
|
1194
1432
|
|
|
1195
|
-
// src/_sse.ts
|
|
1196
|
-
var DEFAULT_EVENT = "message";
|
|
1197
|
-
function fieldValue(line, name) {
|
|
1198
|
-
const value = line.slice(name.length + 1);
|
|
1199
|
-
return value.startsWith(" ") ? value.slice(1) : value;
|
|
1200
|
-
}
|
|
1201
|
-
var SseDecoder = class {
|
|
1202
|
-
constructor() {
|
|
1203
|
-
this.buffer = "";
|
|
1204
|
-
this.event = "";
|
|
1205
|
-
this.data = [];
|
|
1206
|
-
}
|
|
1207
|
-
/** Feed a chunk of decoded text; returns the payloads it completed. */
|
|
1208
|
-
push(text) {
|
|
1209
|
-
this.buffer += text;
|
|
1210
|
-
const lines = this.buffer.split("\n");
|
|
1211
|
-
this.buffer = lines.pop() ?? "";
|
|
1212
|
-
const out = [];
|
|
1213
|
-
for (const line of lines) {
|
|
1214
|
-
const payload = this.pushLine(line);
|
|
1215
|
-
if (payload !== null) out.push(payload);
|
|
1216
|
-
}
|
|
1217
|
-
return out;
|
|
1218
|
-
}
|
|
1219
|
-
/**
|
|
1220
|
-
* Release what the stream ended without terminating: the held-back partial
|
|
1221
|
-
* line, plus any frame that never got its blank line.
|
|
1222
|
-
*
|
|
1223
|
-
* The spec discards an unterminated final block; we keep it, so a connection
|
|
1224
|
-
* that drops after the last `data:` line but before its blank line still
|
|
1225
|
-
* delivers that payload rather than silently losing it.
|
|
1226
|
-
*/
|
|
1227
|
-
flush() {
|
|
1228
|
-
const out = [];
|
|
1229
|
-
if (this.buffer !== "") {
|
|
1230
|
-
const payload = this.pushLine(this.buffer);
|
|
1231
|
-
this.buffer = "";
|
|
1232
|
-
if (payload !== null) out.push(payload);
|
|
1233
|
-
}
|
|
1234
|
-
const tail = this.endFrame();
|
|
1235
|
-
if (tail !== null) out.push(tail);
|
|
1236
|
-
return out;
|
|
1237
|
-
}
|
|
1238
|
-
pushLine(rawLine) {
|
|
1239
|
-
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
|
|
1240
|
-
if (line === "") return this.endFrame();
|
|
1241
|
-
if (line.startsWith(":")) return null;
|
|
1242
|
-
if (line.startsWith("event:")) {
|
|
1243
|
-
this.event = fieldValue(line, "event");
|
|
1244
|
-
return null;
|
|
1245
|
-
}
|
|
1246
|
-
if (line.startsWith("data:")) {
|
|
1247
|
-
this.data.push(fieldValue(line, "data"));
|
|
1248
|
-
return null;
|
|
1249
|
-
}
|
|
1250
|
-
return null;
|
|
1251
|
-
}
|
|
1252
|
-
endFrame() {
|
|
1253
|
-
const { data, event } = this;
|
|
1254
|
-
this.data = [];
|
|
1255
|
-
this.event = "";
|
|
1256
|
-
if (data.length === 0) return null;
|
|
1257
|
-
if (event !== "" && event !== DEFAULT_EVENT) return null;
|
|
1258
|
-
return data.join("\n");
|
|
1259
|
-
}
|
|
1260
|
-
};
|
|
1261
|
-
|
|
1262
|
-
// src/streaming.ts
|
|
1263
|
-
var Stream = class {
|
|
1264
|
-
constructor(response, controller, idleTimeoutMs) {
|
|
1265
|
-
this.decoder = new TextDecoder();
|
|
1266
|
-
this.sse = new SseDecoder();
|
|
1267
|
-
this.done = false;
|
|
1268
|
-
this.placement = new PlacementTracker();
|
|
1269
|
-
if (!response.body) {
|
|
1270
|
-
throw new Error("Response body is null \u2014 streaming not supported");
|
|
1271
|
-
}
|
|
1272
|
-
this.reader = response.body.getReader();
|
|
1273
|
-
this.controller = controller;
|
|
1274
|
-
this.idleTimeoutMs = idleTimeoutMs;
|
|
1275
|
-
}
|
|
1276
|
-
async *[Symbol.asyncIterator]() {
|
|
1277
|
-
try {
|
|
1278
|
-
while (!this.done) {
|
|
1279
|
-
const { value, done: readerDone } = await readWithIdleTimeout(
|
|
1280
|
-
this.reader,
|
|
1281
|
-
this.idleTimeoutMs
|
|
1282
|
-
);
|
|
1283
|
-
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
1284
|
-
for (const data of payloads) {
|
|
1285
|
-
if (data === "[DONE]") {
|
|
1286
|
-
this.done = true;
|
|
1287
|
-
return;
|
|
1288
|
-
}
|
|
1289
|
-
yield this.placement.observe(parseStreamFrame(data));
|
|
1290
|
-
}
|
|
1291
|
-
if (readerDone) {
|
|
1292
|
-
this.done = true;
|
|
1293
|
-
break;
|
|
1294
|
-
}
|
|
1295
|
-
}
|
|
1296
|
-
} finally {
|
|
1297
|
-
releaseReader(this.reader, this.controller, this.done);
|
|
1298
|
-
}
|
|
1299
|
-
}
|
|
1300
|
-
/**
|
|
1301
|
-
* The worker that served this stream, or `null` until the chunk reporting it
|
|
1302
|
-
* — the trailer, just before `[DONE]` — has been iterated (and on an API too
|
|
1303
|
-
* old to report it). Same as that chunk's `pendra.worker`.
|
|
1304
|
-
*/
|
|
1305
|
-
get worker() {
|
|
1306
|
-
return this.placement.worker;
|
|
1307
|
-
}
|
|
1308
|
-
/**
|
|
1309
|
-
* Pendra's id for this request, reported alongside {@link Stream.worker};
|
|
1310
|
-
* `null` until then.
|
|
1311
|
-
*/
|
|
1312
|
-
get requestId() {
|
|
1313
|
-
return this.placement.requestId;
|
|
1314
|
-
}
|
|
1315
|
-
abort() {
|
|
1316
|
-
this.done = true;
|
|
1317
|
-
this.controller.abort();
|
|
1318
|
-
}
|
|
1319
|
-
};
|
|
1320
|
-
var PlacementTracker = class {
|
|
1321
|
-
constructor() {
|
|
1322
|
-
this.worker = null;
|
|
1323
|
-
this.requestId = null;
|
|
1324
|
-
}
|
|
1325
|
-
/** Record `chunk`'s placement, if it carries any, and hand it back. */
|
|
1326
|
-
observe(chunk) {
|
|
1327
|
-
const { worker, requestId } = chunkPlacement(chunk);
|
|
1328
|
-
if (worker) this.worker = worker;
|
|
1329
|
-
if (requestId) this.requestId = requestId;
|
|
1330
|
-
return chunk;
|
|
1331
|
-
}
|
|
1332
|
-
};
|
|
1333
|
-
async function readWithIdleTimeout(reader, idleTimeoutMs) {
|
|
1334
|
-
if (!idleTimeoutMs) return reader.read();
|
|
1335
|
-
let timer;
|
|
1336
|
-
const idle = new Promise((_, reject) => {
|
|
1337
|
-
timer = setTimeout(
|
|
1338
|
-
() => reject(new APIConnectionError(`Stream received no data for ${idleTimeoutMs}ms`)),
|
|
1339
|
-
idleTimeoutMs
|
|
1340
|
-
);
|
|
1341
|
-
});
|
|
1342
|
-
try {
|
|
1343
|
-
return await Promise.race([reader.read(), idle]);
|
|
1344
|
-
} finally {
|
|
1345
|
-
clearTimeout(timer);
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
function releaseReader(reader, controller, finished) {
|
|
1349
|
-
if (!finished) {
|
|
1350
|
-
controller.abort();
|
|
1351
|
-
reader.cancel().catch(() => {
|
|
1352
|
-
});
|
|
1353
|
-
}
|
|
1354
|
-
reader.releaseLock();
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
1433
|
// src/_privateStreaming.ts
|
|
1358
1434
|
var DECODER2 = new TextDecoder();
|
|
1359
1435
|
var EncryptedStream = class {
|
|
@@ -1496,7 +1572,7 @@ var Completions = class {
|
|
|
1496
1572
|
if (params.stream) {
|
|
1497
1573
|
const { response: response2, controller } = await this.client._fetch(
|
|
1498
1574
|
"/api/v1/chat/completions",
|
|
1499
|
-
{ method: "POST", body, headers }
|
|
1575
|
+
{ method: "POST", body, headers, stream: true }
|
|
1500
1576
|
);
|
|
1501
1577
|
return new Stream(response2, controller, this.client.timeout);
|
|
1502
1578
|
}
|
|
@@ -1523,7 +1599,7 @@ var Completions = class {
|
|
|
1523
1599
|
if (stream) {
|
|
1524
1600
|
const { response: response2, controller } = await this.client._fetch(
|
|
1525
1601
|
"/api/v1/chat/completions",
|
|
1526
|
-
{ method: "POST", body: topLevel, headers }
|
|
1602
|
+
{ method: "POST", body: topLevel, headers, stream: true }
|
|
1527
1603
|
);
|
|
1528
1604
|
return new EncryptedStream(response2, controller, decryptor, this.client.timeout);
|
|
1529
1605
|
}
|
|
@@ -1911,7 +1987,7 @@ var Rerank = class {
|
|
|
1911
1987
|
};
|
|
1912
1988
|
|
|
1913
1989
|
// src/version.ts
|
|
1914
|
-
var VERSION = "0.
|
|
1990
|
+
var VERSION = "0.21.1";
|
|
1915
1991
|
var USER_AGENT = `pendra-node/${VERSION}`;
|
|
1916
1992
|
|
|
1917
1993
|
// src/client.ts
|
|
@@ -2018,6 +2094,7 @@ var Pendra = class {
|
|
|
2018
2094
|
} finally {
|
|
2019
2095
|
clearTimeout(timeoutId);
|
|
2020
2096
|
}
|
|
2097
|
+
response = withIdleTimeout(response, controller, this.timeout);
|
|
2021
2098
|
if (response.status === 404) {
|
|
2022
2099
|
const body = await readErrorBody(response);
|
|
2023
2100
|
const detail = typeof body === "object" && body !== null ? body.detail : void 0;
|
|
@@ -2026,7 +2103,7 @@ var Pendra = class {
|
|
|
2026
2103
|
);
|
|
2027
2104
|
}
|
|
2028
2105
|
if (!response.ok) {
|
|
2029
|
-
throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText);
|
|
2106
|
+
throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText, response.headers);
|
|
2030
2107
|
}
|
|
2031
2108
|
const json = await response.json();
|
|
2032
2109
|
await engine.resolveOrgKey(json.keys ?? []);
|
|
@@ -2062,9 +2139,10 @@ var Pendra = class {
|
|
|
2062
2139
|
);
|
|
2063
2140
|
}
|
|
2064
2141
|
clearTimeout(timeoutId);
|
|
2142
|
+
if (!options.stream || !response.ok) response = withIdleTimeout(response, controller, timeout);
|
|
2065
2143
|
if (!response.ok) {
|
|
2066
2144
|
this.invalidateKeyCacheIfRekeyed(response);
|
|
2067
|
-
throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText);
|
|
2145
|
+
throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText, response.headers);
|
|
2068
2146
|
}
|
|
2069
2147
|
return { response, controller };
|
|
2070
2148
|
}
|