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.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.buffer = "";
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
- if (readerDone) {
179
- this.done = true;
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.buffer = "";
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
- if (readerDone) {
228
- this.done = true;
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();
@@ -292,6 +348,14 @@ var Completions = class {
292
348
  if (params.temperature != null) body.temperature = params.temperature;
293
349
  if (params.max_tokens != null) body.max_tokens = params.max_tokens;
294
350
  if (params.top_p != null) body.top_p = params.top_p;
351
+ if (params.top_k != null) body.top_k = params.top_k;
352
+ if (params.min_p != null) body.min_p = params.min_p;
353
+ if (params.frequency_penalty != null)
354
+ body.frequency_penalty = params.frequency_penalty;
355
+ if (params.presence_penalty != null)
356
+ body.presence_penalty = params.presence_penalty;
357
+ if (params.repeat_penalty != null) body.repeat_penalty = params.repeat_penalty;
358
+ if (params.seed != null) body.seed = params.seed;
295
359
  if (params.stop != null) body.stop = params.stop;
296
360
  if (params.tools != null) body.tools = params.tools;
297
361
  if (params.tool_choice != null) body.tool_choice = params.tool_choice;