pendra 0.8.0 → 0.10.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/README.md +29 -0
- package/dist/index.d.mts +111 -6
- package/dist/index.d.ts +111 -6
- package/dist/index.js +87 -39
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +87 -39
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
package/dist/index.mjs
CHANGED
|
@@ -159,11 +159,78 @@ var Audio = class {
|
|
|
159
159
|
}
|
|
160
160
|
};
|
|
161
161
|
|
|
162
|
+
// src/_sse.ts
|
|
163
|
+
var DEFAULT_EVENT = "message";
|
|
164
|
+
function fieldValue(line, name) {
|
|
165
|
+
const value = line.slice(name.length + 1);
|
|
166
|
+
return value.startsWith(" ") ? value.slice(1) : value;
|
|
167
|
+
}
|
|
168
|
+
var SseDecoder = class {
|
|
169
|
+
constructor() {
|
|
170
|
+
this.buffer = "";
|
|
171
|
+
this.event = "";
|
|
172
|
+
this.data = [];
|
|
173
|
+
}
|
|
174
|
+
/** Feed a chunk of decoded text; returns the payloads it completed. */
|
|
175
|
+
push(text) {
|
|
176
|
+
this.buffer += text;
|
|
177
|
+
const lines = this.buffer.split("\n");
|
|
178
|
+
this.buffer = lines.pop() ?? "";
|
|
179
|
+
const out = [];
|
|
180
|
+
for (const line of lines) {
|
|
181
|
+
const payload = this.pushLine(line);
|
|
182
|
+
if (payload !== null) out.push(payload);
|
|
183
|
+
}
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Release what the stream ended without terminating: the held-back partial
|
|
188
|
+
* line, plus any frame that never got its blank line.
|
|
189
|
+
*
|
|
190
|
+
* The spec discards an unterminated final block; we keep it, so a connection
|
|
191
|
+
* that drops after the last `data:` line but before its blank line still
|
|
192
|
+
* delivers that payload rather than silently losing it.
|
|
193
|
+
*/
|
|
194
|
+
flush() {
|
|
195
|
+
const out = [];
|
|
196
|
+
if (this.buffer !== "") {
|
|
197
|
+
const payload = this.pushLine(this.buffer);
|
|
198
|
+
this.buffer = "";
|
|
199
|
+
if (payload !== null) out.push(payload);
|
|
200
|
+
}
|
|
201
|
+
const tail = this.endFrame();
|
|
202
|
+
if (tail !== null) out.push(tail);
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
pushLine(rawLine) {
|
|
206
|
+
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
|
|
207
|
+
if (line === "") return this.endFrame();
|
|
208
|
+
if (line.startsWith(":")) return null;
|
|
209
|
+
if (line.startsWith("event:")) {
|
|
210
|
+
this.event = fieldValue(line, "event");
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
if (line.startsWith("data:")) {
|
|
214
|
+
this.data.push(fieldValue(line, "data"));
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
endFrame() {
|
|
220
|
+
const { data, event } = this;
|
|
221
|
+
this.data = [];
|
|
222
|
+
this.event = "";
|
|
223
|
+
if (data.length === 0) return null;
|
|
224
|
+
if (event !== "" && event !== DEFAULT_EVENT) return null;
|
|
225
|
+
return data.join("\n");
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
162
229
|
// src/streaming.ts
|
|
163
230
|
var Stream = class {
|
|
164
231
|
constructor(response, controller) {
|
|
165
232
|
this.decoder = new TextDecoder();
|
|
166
|
-
this.
|
|
233
|
+
this.sse = new SseDecoder();
|
|
167
234
|
this.done = false;
|
|
168
235
|
if (!response.body) {
|
|
169
236
|
throw new Error("Response body is null \u2014 streaming not supported");
|
|
@@ -175,18 +242,8 @@ var Stream = class {
|
|
|
175
242
|
try {
|
|
176
243
|
while (!this.done) {
|
|
177
244
|
const { value, done: readerDone } = await this.reader.read();
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
this.buffer += this.decoder.decode(value, { stream: true });
|
|
183
|
-
const lines = this.buffer.split("\n");
|
|
184
|
-
this.buffer = lines.pop() ?? "";
|
|
185
|
-
for (const line of lines) {
|
|
186
|
-
const trimmed = line.trim();
|
|
187
|
-
if (!trimmed || trimmed.startsWith(":")) continue;
|
|
188
|
-
if (!trimmed.startsWith("data: ")) continue;
|
|
189
|
-
const data = trimmed.slice(6);
|
|
245
|
+
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
246
|
+
for (const data of payloads) {
|
|
190
247
|
if (data === "[DONE]") {
|
|
191
248
|
this.done = true;
|
|
192
249
|
return;
|
|
@@ -196,6 +253,10 @@ var Stream = class {
|
|
|
196
253
|
} catch {
|
|
197
254
|
}
|
|
198
255
|
}
|
|
256
|
+
if (readerDone) {
|
|
257
|
+
this.done = true;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
199
260
|
}
|
|
200
261
|
} finally {
|
|
201
262
|
this.reader.releaseLock();
|
|
@@ -211,7 +272,8 @@ var Stream = class {
|
|
|
211
272
|
var DECODER = new TextDecoder();
|
|
212
273
|
var EncryptedStream = class {
|
|
213
274
|
constructor(response, controller, decryptor) {
|
|
214
|
-
this.
|
|
275
|
+
this.decoder = new TextDecoder();
|
|
276
|
+
this.sse = new SseDecoder();
|
|
215
277
|
this.done = false;
|
|
216
278
|
if (!response.body) {
|
|
217
279
|
throw new Error("Response body is null \u2014 streaming not supported");
|
|
@@ -224,18 +286,8 @@ var EncryptedStream = class {
|
|
|
224
286
|
try {
|
|
225
287
|
while (!this.done) {
|
|
226
288
|
const { value, done: readerDone } = await this.reader.read();
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
break;
|
|
230
|
-
}
|
|
231
|
-
this.buffer += DECODER.decode(value, { stream: true });
|
|
232
|
-
const lines = this.buffer.split("\n");
|
|
233
|
-
this.buffer = lines.pop() ?? "";
|
|
234
|
-
for (const line of lines) {
|
|
235
|
-
const trimmed = line.trim();
|
|
236
|
-
if (!trimmed || trimmed.startsWith(":") || trimmed.startsWith("event:")) continue;
|
|
237
|
-
if (!trimmed.startsWith("data:")) continue;
|
|
238
|
-
const data = trimmed.slice("data:".length).trimStart();
|
|
289
|
+
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
290
|
+
for (const data of payloads) {
|
|
239
291
|
if (data === "[DONE]") {
|
|
240
292
|
this.done = true;
|
|
241
293
|
return;
|
|
@@ -265,6 +317,10 @@ var EncryptedStream = class {
|
|
|
265
317
|
} catch {
|
|
266
318
|
}
|
|
267
319
|
}
|
|
320
|
+
if (readerDone) {
|
|
321
|
+
this.done = true;
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
268
324
|
}
|
|
269
325
|
} finally {
|
|
270
326
|
this.reader.releaseLock();
|
|
@@ -284,19 +340,11 @@ var Completions = class {
|
|
|
284
340
|
this.client = client;
|
|
285
341
|
}
|
|
286
342
|
async create(params) {
|
|
287
|
-
const
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
if (params.temperature != null) body.temperature = params.temperature;
|
|
293
|
-
if (params.max_tokens != null) body.max_tokens = params.max_tokens;
|
|
294
|
-
if (params.top_p != null) body.top_p = params.top_p;
|
|
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;
|
|
343
|
+
const { worker_id: _workerId, ...rest } = params;
|
|
344
|
+
const body = {};
|
|
345
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
346
|
+
if (value != null) body[key] = value;
|
|
347
|
+
}
|
|
300
348
|
const headers = params.worker_id ? { "X-Pendra-Worker-Id": params.worker_id } : void 0;
|
|
301
349
|
if (this.client.privateInferenceEnabled) {
|
|
302
350
|
return this.createEncrypted(params, body, headers);
|