pendra 0.8.0 → 0.9.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 +21 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.js +90 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +90 -26
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -107,6 +107,18 @@ interface ChatCompletionCreateParams {
|
|
|
107
107
|
temperature?: number | null;
|
|
108
108
|
max_tokens?: number | null;
|
|
109
109
|
top_p?: number | null;
|
|
110
|
+
/** Keep only the `top_k` most likely tokens (0 = off). */
|
|
111
|
+
top_k?: number | null;
|
|
112
|
+
/** Drop tokens less likely than `min_p` × the top token's probability. */
|
|
113
|
+
min_p?: number | null;
|
|
114
|
+
/** Penalise tokens by how often they've appeared (-2 to 2; 0 = off). */
|
|
115
|
+
frequency_penalty?: number | null;
|
|
116
|
+
/** Penalise tokens that have appeared at all (-2 to 2; 0 = off). */
|
|
117
|
+
presence_penalty?: number | null;
|
|
118
|
+
/** Classic repetition penalty (1.0 = off; e.g. 1.1–1.3 discourages repeats). */
|
|
119
|
+
repeat_penalty?: number | null;
|
|
120
|
+
/** Seed the sampler for reproducible output. */
|
|
121
|
+
seed?: number | null;
|
|
110
122
|
stop?: string | string[] | null;
|
|
111
123
|
stream?: boolean;
|
|
112
124
|
/**
|
|
@@ -128,10 +140,18 @@ interface ChatCompletionCreateParams {
|
|
|
128
140
|
worker_id?: string | null;
|
|
129
141
|
}
|
|
130
142
|
|
|
143
|
+
/**
|
|
144
|
+
* Async-iterable reader for a streaming chat completion.
|
|
145
|
+
*
|
|
146
|
+
* Framing is handled by {@link SseDecoder}, which assembles whole SSE frames
|
|
147
|
+
* rather than filtering lines — so a frame the server tags with a named
|
|
148
|
+
* `event:` is dropped in one piece instead of having its `data:` line mistaken
|
|
149
|
+
* for a chat chunk (issue #1398).
|
|
150
|
+
*/
|
|
131
151
|
declare class Stream implements AsyncIterable<ChatCompletionChunk> {
|
|
132
152
|
private reader;
|
|
133
153
|
private decoder;
|
|
134
|
-
private
|
|
154
|
+
private sse;
|
|
135
155
|
private done;
|
|
136
156
|
private controller;
|
|
137
157
|
constructor(response: Response, controller: AbortController);
|
package/dist/index.d.ts
CHANGED
|
@@ -107,6 +107,18 @@ interface ChatCompletionCreateParams {
|
|
|
107
107
|
temperature?: number | null;
|
|
108
108
|
max_tokens?: number | null;
|
|
109
109
|
top_p?: number | null;
|
|
110
|
+
/** Keep only the `top_k` most likely tokens (0 = off). */
|
|
111
|
+
top_k?: number | null;
|
|
112
|
+
/** Drop tokens less likely than `min_p` × the top token's probability. */
|
|
113
|
+
min_p?: number | null;
|
|
114
|
+
/** Penalise tokens by how often they've appeared (-2 to 2; 0 = off). */
|
|
115
|
+
frequency_penalty?: number | null;
|
|
116
|
+
/** Penalise tokens that have appeared at all (-2 to 2; 0 = off). */
|
|
117
|
+
presence_penalty?: number | null;
|
|
118
|
+
/** Classic repetition penalty (1.0 = off; e.g. 1.1–1.3 discourages repeats). */
|
|
119
|
+
repeat_penalty?: number | null;
|
|
120
|
+
/** Seed the sampler for reproducible output. */
|
|
121
|
+
seed?: number | null;
|
|
110
122
|
stop?: string | string[] | null;
|
|
111
123
|
stream?: boolean;
|
|
112
124
|
/**
|
|
@@ -128,10 +140,18 @@ interface ChatCompletionCreateParams {
|
|
|
128
140
|
worker_id?: string | null;
|
|
129
141
|
}
|
|
130
142
|
|
|
143
|
+
/**
|
|
144
|
+
* Async-iterable reader for a streaming chat completion.
|
|
145
|
+
*
|
|
146
|
+
* Framing is handled by {@link SseDecoder}, which assembles whole SSE frames
|
|
147
|
+
* rather than filtering lines — so a frame the server tags with a named
|
|
148
|
+
* `event:` is dropped in one piece instead of having its `data:` line mistaken
|
|
149
|
+
* for a chat chunk (issue #1398).
|
|
150
|
+
*/
|
|
131
151
|
declare class Stream implements AsyncIterable<ChatCompletionChunk> {
|
|
132
152
|
private reader;
|
|
133
153
|
private decoder;
|
|
134
|
-
private
|
|
154
|
+
private sse;
|
|
135
155
|
private done;
|
|
136
156
|
private controller;
|
|
137
157
|
constructor(response: Response, controller: AbortController);
|
package/dist/index.js
CHANGED
|
@@ -203,11 +203,78 @@ var Audio = class {
|
|
|
203
203
|
}
|
|
204
204
|
};
|
|
205
205
|
|
|
206
|
+
// src/_sse.ts
|
|
207
|
+
var DEFAULT_EVENT = "message";
|
|
208
|
+
function fieldValue(line, name) {
|
|
209
|
+
const value = line.slice(name.length + 1);
|
|
210
|
+
return value.startsWith(" ") ? value.slice(1) : value;
|
|
211
|
+
}
|
|
212
|
+
var SseDecoder = class {
|
|
213
|
+
constructor() {
|
|
214
|
+
this.buffer = "";
|
|
215
|
+
this.event = "";
|
|
216
|
+
this.data = [];
|
|
217
|
+
}
|
|
218
|
+
/** Feed a chunk of decoded text; returns the payloads it completed. */
|
|
219
|
+
push(text) {
|
|
220
|
+
this.buffer += text;
|
|
221
|
+
const lines = this.buffer.split("\n");
|
|
222
|
+
this.buffer = lines.pop() ?? "";
|
|
223
|
+
const out = [];
|
|
224
|
+
for (const line of lines) {
|
|
225
|
+
const payload = this.pushLine(line);
|
|
226
|
+
if (payload !== null) out.push(payload);
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Release what the stream ended without terminating: the held-back partial
|
|
232
|
+
* line, plus any frame that never got its blank line.
|
|
233
|
+
*
|
|
234
|
+
* The spec discards an unterminated final block; we keep it, so a connection
|
|
235
|
+
* that drops after the last `data:` line but before its blank line still
|
|
236
|
+
* delivers that payload rather than silently losing it.
|
|
237
|
+
*/
|
|
238
|
+
flush() {
|
|
239
|
+
const out = [];
|
|
240
|
+
if (this.buffer !== "") {
|
|
241
|
+
const payload = this.pushLine(this.buffer);
|
|
242
|
+
this.buffer = "";
|
|
243
|
+
if (payload !== null) out.push(payload);
|
|
244
|
+
}
|
|
245
|
+
const tail = this.endFrame();
|
|
246
|
+
if (tail !== null) out.push(tail);
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
pushLine(rawLine) {
|
|
250
|
+
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
|
|
251
|
+
if (line === "") return this.endFrame();
|
|
252
|
+
if (line.startsWith(":")) return null;
|
|
253
|
+
if (line.startsWith("event:")) {
|
|
254
|
+
this.event = fieldValue(line, "event");
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
if (line.startsWith("data:")) {
|
|
258
|
+
this.data.push(fieldValue(line, "data"));
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
endFrame() {
|
|
264
|
+
const { data, event } = this;
|
|
265
|
+
this.data = [];
|
|
266
|
+
this.event = "";
|
|
267
|
+
if (data.length === 0) return null;
|
|
268
|
+
if (event !== "" && event !== DEFAULT_EVENT) return null;
|
|
269
|
+
return data.join("\n");
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
206
273
|
// src/streaming.ts
|
|
207
274
|
var Stream = class {
|
|
208
275
|
constructor(response, controller) {
|
|
209
276
|
this.decoder = new TextDecoder();
|
|
210
|
-
this.
|
|
277
|
+
this.sse = new SseDecoder();
|
|
211
278
|
this.done = false;
|
|
212
279
|
if (!response.body) {
|
|
213
280
|
throw new Error("Response body is null \u2014 streaming not supported");
|
|
@@ -219,18 +286,8 @@ var Stream = class {
|
|
|
219
286
|
try {
|
|
220
287
|
while (!this.done) {
|
|
221
288
|
const { value, done: readerDone } = await this.reader.read();
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
break;
|
|
225
|
-
}
|
|
226
|
-
this.buffer += this.decoder.decode(value, { stream: true });
|
|
227
|
-
const lines = this.buffer.split("\n");
|
|
228
|
-
this.buffer = lines.pop() ?? "";
|
|
229
|
-
for (const line of lines) {
|
|
230
|
-
const trimmed = line.trim();
|
|
231
|
-
if (!trimmed || trimmed.startsWith(":")) continue;
|
|
232
|
-
if (!trimmed.startsWith("data: ")) continue;
|
|
233
|
-
const data = trimmed.slice(6);
|
|
289
|
+
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
290
|
+
for (const data of payloads) {
|
|
234
291
|
if (data === "[DONE]") {
|
|
235
292
|
this.done = true;
|
|
236
293
|
return;
|
|
@@ -240,6 +297,10 @@ var Stream = class {
|
|
|
240
297
|
} catch {
|
|
241
298
|
}
|
|
242
299
|
}
|
|
300
|
+
if (readerDone) {
|
|
301
|
+
this.done = true;
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
243
304
|
}
|
|
244
305
|
} finally {
|
|
245
306
|
this.reader.releaseLock();
|
|
@@ -255,7 +316,8 @@ var Stream = class {
|
|
|
255
316
|
var DECODER = new TextDecoder();
|
|
256
317
|
var EncryptedStream = class {
|
|
257
318
|
constructor(response, controller, decryptor) {
|
|
258
|
-
this.
|
|
319
|
+
this.decoder = new TextDecoder();
|
|
320
|
+
this.sse = new SseDecoder();
|
|
259
321
|
this.done = false;
|
|
260
322
|
if (!response.body) {
|
|
261
323
|
throw new Error("Response body is null \u2014 streaming not supported");
|
|
@@ -268,18 +330,8 @@ var EncryptedStream = class {
|
|
|
268
330
|
try {
|
|
269
331
|
while (!this.done) {
|
|
270
332
|
const { value, done: readerDone } = await this.reader.read();
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
break;
|
|
274
|
-
}
|
|
275
|
-
this.buffer += DECODER.decode(value, { stream: true });
|
|
276
|
-
const lines = this.buffer.split("\n");
|
|
277
|
-
this.buffer = lines.pop() ?? "";
|
|
278
|
-
for (const line of lines) {
|
|
279
|
-
const trimmed = line.trim();
|
|
280
|
-
if (!trimmed || trimmed.startsWith(":") || trimmed.startsWith("event:")) continue;
|
|
281
|
-
if (!trimmed.startsWith("data:")) continue;
|
|
282
|
-
const data = trimmed.slice("data:".length).trimStart();
|
|
333
|
+
const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
|
|
334
|
+
for (const data of payloads) {
|
|
283
335
|
if (data === "[DONE]") {
|
|
284
336
|
this.done = true;
|
|
285
337
|
return;
|
|
@@ -309,6 +361,10 @@ var EncryptedStream = class {
|
|
|
309
361
|
} catch {
|
|
310
362
|
}
|
|
311
363
|
}
|
|
364
|
+
if (readerDone) {
|
|
365
|
+
this.done = true;
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
312
368
|
}
|
|
313
369
|
} finally {
|
|
314
370
|
this.reader.releaseLock();
|
|
@@ -336,6 +392,14 @@ var Completions = class {
|
|
|
336
392
|
if (params.temperature != null) body.temperature = params.temperature;
|
|
337
393
|
if (params.max_tokens != null) body.max_tokens = params.max_tokens;
|
|
338
394
|
if (params.top_p != null) body.top_p = params.top_p;
|
|
395
|
+
if (params.top_k != null) body.top_k = params.top_k;
|
|
396
|
+
if (params.min_p != null) body.min_p = params.min_p;
|
|
397
|
+
if (params.frequency_penalty != null)
|
|
398
|
+
body.frequency_penalty = params.frequency_penalty;
|
|
399
|
+
if (params.presence_penalty != null)
|
|
400
|
+
body.presence_penalty = params.presence_penalty;
|
|
401
|
+
if (params.repeat_penalty != null) body.repeat_penalty = params.repeat_penalty;
|
|
402
|
+
if (params.seed != null) body.seed = params.seed;
|
|
339
403
|
if (params.stop != null) body.stop = params.stop;
|
|
340
404
|
if (params.tools != null) body.tools = params.tools;
|
|
341
405
|
if (params.tool_choice != null) body.tool_choice = params.tool_choice;
|