@langchain/google-common 0.1.0 → 0.1.2

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReadableJsonStream = exports.ComplexJsonStream = exports.JsonStream = exports.simpleValue = exports.complexValue = void 0;
3
+ exports.ReadableSseJsonStream = exports.SseJsonStream = exports.ReadableSseStream = exports.SseStream = exports.ReadableJsonStream = exports.ReadableAbstractStream = exports.ComplexJsonStream = exports.JsonStream = exports.simpleValue = exports.complexValue = void 0;
4
4
  function complexValue(value) {
5
5
  if (value === null || typeof value === "undefined") {
6
6
  // I dunno what to put here. An error, probably
@@ -268,15 +268,21 @@ class ComplexJsonStream extends JsonStream {
268
268
  }
269
269
  }
270
270
  exports.ComplexJsonStream = ComplexJsonStream;
271
- class ReadableJsonStream extends JsonStream {
272
- constructor(body) {
273
- super();
271
+ class ReadableAbstractStream {
272
+ constructor(baseStream, body) {
273
+ Object.defineProperty(this, "baseStream", {
274
+ enumerable: true,
275
+ configurable: true,
276
+ writable: true,
277
+ value: void 0
278
+ });
274
279
  Object.defineProperty(this, "decoder", {
275
280
  enumerable: true,
276
281
  configurable: true,
277
282
  writable: true,
278
283
  value: void 0
279
284
  });
285
+ this.baseStream = baseStream;
280
286
  this.decoder = new TextDecoder("utf-8");
281
287
  if (body) {
282
288
  void this.run(body);
@@ -285,6 +291,19 @@ class ReadableJsonStream extends JsonStream {
285
291
  console.error("Unexpected empty body while streaming");
286
292
  }
287
293
  }
294
+ appendBuffer(data) {
295
+ return this.baseStream.appendBuffer(data);
296
+ }
297
+ closeBuffer() {
298
+ return this.baseStream.closeBuffer();
299
+ }
300
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
301
+ nextChunk() {
302
+ return this.baseStream.nextChunk();
303
+ }
304
+ get streamDone() {
305
+ return this.baseStream.streamDone;
306
+ }
288
307
  async run(body) {
289
308
  const reader = body.getReader();
290
309
  let isDone = false;
@@ -301,4 +320,165 @@ class ReadableJsonStream extends JsonStream {
301
320
  }
302
321
  }
303
322
  }
323
+ exports.ReadableAbstractStream = ReadableAbstractStream;
324
+ class ReadableJsonStream extends ReadableAbstractStream {
325
+ constructor(body) {
326
+ super(new JsonStream(), body);
327
+ }
328
+ }
304
329
  exports.ReadableJsonStream = ReadableJsonStream;
330
+ class SseStream {
331
+ constructor() {
332
+ Object.defineProperty(this, "_buffer", {
333
+ enumerable: true,
334
+ configurable: true,
335
+ writable: true,
336
+ value: ""
337
+ });
338
+ Object.defineProperty(this, "_bufferOpen", {
339
+ enumerable: true,
340
+ configurable: true,
341
+ writable: true,
342
+ value: true
343
+ });
344
+ // Set up a potential Promise that the handler can resolve.
345
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
346
+ Object.defineProperty(this, "_chunkResolution", {
347
+ enumerable: true,
348
+ configurable: true,
349
+ writable: true,
350
+ value: void 0
351
+ });
352
+ // If there is no Promise (it is null), the handler must add it to the queue
353
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
354
+ Object.defineProperty(this, "_chunkPending", {
355
+ enumerable: true,
356
+ configurable: true,
357
+ writable: true,
358
+ value: null
359
+ });
360
+ // A queue that will collect chunks while there is no Promise
361
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
362
+ Object.defineProperty(this, "_chunkQueue", {
363
+ enumerable: true,
364
+ configurable: true,
365
+ writable: true,
366
+ value: []
367
+ });
368
+ }
369
+ appendBuffer(data) {
370
+ this._buffer += data;
371
+ this._parseBuffer();
372
+ }
373
+ closeBuffer() {
374
+ this._bufferOpen = false;
375
+ this._parseBuffer();
376
+ }
377
+ /**
378
+ * Attempt to load an entire event.
379
+ * For each entire event we load,
380
+ * send them to be handled.
381
+ */
382
+ _parseBuffer() {
383
+ const events = this._buffer.split(/\n\n/);
384
+ this._buffer = events.pop() ?? "";
385
+ events.forEach((event) => this._handleEvent(event.trim()));
386
+ if (!this._bufferOpen) {
387
+ // No more data will be added, and we have parsed
388
+ // everything. So dump the rest.
389
+ this._handleEvent(null);
390
+ this._buffer = "";
391
+ }
392
+ }
393
+ /**
394
+ * Given an event string, get all the fields
395
+ * in the event. It is assumed there is one field
396
+ * per line, but that field names can be duplicated,
397
+ * indicating to append the new value to the previous value
398
+ * @param event
399
+ */
400
+ _parseEvent(event) {
401
+ if (!event || event.trim() === "") {
402
+ return null;
403
+ }
404
+ const ret = {};
405
+ const lines = event.split(/\n/);
406
+ lines.forEach((line) => {
407
+ const match = line.match(/^([^:]+): \s*(.+)\n*$/);
408
+ if (match && match.length === 3) {
409
+ const key = match[1];
410
+ const val = match[2];
411
+ const cur = ret[key] ?? "";
412
+ ret[key] = `${cur}${val}`;
413
+ }
414
+ });
415
+ return ret;
416
+ }
417
+ _handleEvent(event) {
418
+ const chunk = this._parseEvent(event);
419
+ if (this._chunkPending) {
420
+ this._chunkResolution(chunk);
421
+ this._chunkPending = null;
422
+ }
423
+ else {
424
+ this._chunkQueue.push(chunk);
425
+ }
426
+ }
427
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
428
+ async nextChunk() {
429
+ if (this._chunkQueue.length > 0) {
430
+ // If there is data in the queue, return the next queue chunk
431
+ return this._chunkQueue.shift();
432
+ }
433
+ else {
434
+ // Otherwise, set up a promise that handleChunk will cause to be resolved
435
+ this._chunkPending = new Promise((resolve) => {
436
+ this._chunkResolution = resolve;
437
+ });
438
+ return this._chunkPending;
439
+ }
440
+ }
441
+ get streamDone() {
442
+ return (!this._bufferOpen &&
443
+ this._buffer.length === 0 &&
444
+ this._chunkQueue.length === 0 &&
445
+ this._chunkPending === null);
446
+ }
447
+ }
448
+ exports.SseStream = SseStream;
449
+ class ReadableSseStream extends ReadableAbstractStream {
450
+ constructor(body) {
451
+ super(new SseStream(), body);
452
+ }
453
+ }
454
+ exports.ReadableSseStream = ReadableSseStream;
455
+ class SseJsonStream extends SseStream {
456
+ constructor(jsonAttribute) {
457
+ super();
458
+ Object.defineProperty(this, "_jsonAttribute", {
459
+ enumerable: true,
460
+ configurable: true,
461
+ writable: true,
462
+ value: "data"
463
+ });
464
+ this._jsonAttribute = jsonAttribute ?? this._jsonAttribute;
465
+ }
466
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
467
+ async nextChunk() {
468
+ const eventRecord = (await super.nextChunk());
469
+ const json = eventRecord?.[this._jsonAttribute];
470
+ if (!json) {
471
+ return null;
472
+ }
473
+ else {
474
+ return JSON.parse(json);
475
+ }
476
+ }
477
+ }
478
+ exports.SseJsonStream = SseJsonStream;
479
+ class ReadableSseJsonStream extends ReadableAbstractStream {
480
+ constructor(body) {
481
+ super(new SseJsonStream(), body);
482
+ }
483
+ }
484
+ exports.ReadableSseJsonStream = ReadableSseJsonStream;
@@ -1,6 +1,31 @@
1
+ export interface AbstractStream {
2
+ /**
3
+ * Add more text to the buffer
4
+ * @param data
5
+ */
6
+ appendBuffer(data: string): void;
7
+ /**
8
+ * Indicate that there is no more text to be added to the buffer
9
+ * (ie - our source material is done)
10
+ */
11
+ closeBuffer(): void;
12
+ /**
13
+ * Get the next chunk that is coming from the stream.
14
+ * This chunk may be null, usually indicating the last chunk in the stream.
15
+ */
16
+ nextChunk(): Promise<any>;
17
+ /**
18
+ * Is the stream done?
19
+ * A stream is only done if all of the following are true:
20
+ * - There is no more data to be added to the text buffer
21
+ * - There is no more data in the text buffer
22
+ * - There are no chunks that are waiting to be consumed
23
+ */
24
+ get streamDone(): boolean;
25
+ }
1
26
  export declare function complexValue(value: unknown): unknown;
2
27
  export declare function simpleValue(val: unknown): unknown;
3
- export declare class JsonStream {
28
+ export declare class JsonStream implements AbstractStream {
4
29
  _buffer: string;
5
30
  _bufferOpen: boolean;
6
31
  _firstRun: boolean;
@@ -63,8 +88,53 @@ export declare class JsonStream {
63
88
  export declare class ComplexJsonStream extends JsonStream {
64
89
  _simplifyObject(obj: unknown): object;
65
90
  }
66
- export declare class ReadableJsonStream extends JsonStream {
91
+ export declare class ReadableAbstractStream implements AbstractStream {
92
+ private baseStream;
67
93
  decoder: TextDecoder;
68
- constructor(body: ReadableStream | null);
94
+ constructor(baseStream: AbstractStream, body: ReadableStream | null);
95
+ appendBuffer(data: string): void;
96
+ closeBuffer(): void;
97
+ nextChunk(): Promise<any>;
98
+ get streamDone(): boolean;
69
99
  run(body: ReadableStream): Promise<void>;
70
100
  }
101
+ export declare class ReadableJsonStream extends ReadableAbstractStream {
102
+ constructor(body: ReadableStream | null);
103
+ }
104
+ export declare class SseStream implements AbstractStream {
105
+ _buffer: string;
106
+ _bufferOpen: boolean;
107
+ appendBuffer(data: string): void;
108
+ closeBuffer(): void;
109
+ /**
110
+ * Attempt to load an entire event.
111
+ * For each entire event we load,
112
+ * send them to be handled.
113
+ */
114
+ _parseBuffer(): void;
115
+ /**
116
+ * Given an event string, get all the fields
117
+ * in the event. It is assumed there is one field
118
+ * per line, but that field names can be duplicated,
119
+ * indicating to append the new value to the previous value
120
+ * @param event
121
+ */
122
+ _parseEvent(event: string | null): Record<string, string> | null;
123
+ _chunkResolution: (chunk: any) => void;
124
+ _chunkPending: Promise<any> | null;
125
+ _chunkQueue: any[];
126
+ _handleEvent(event: string | null): void;
127
+ nextChunk(): Promise<any>;
128
+ get streamDone(): boolean;
129
+ }
130
+ export declare class ReadableSseStream extends ReadableAbstractStream {
131
+ constructor(body: ReadableStream | null);
132
+ }
133
+ export declare class SseJsonStream extends SseStream {
134
+ _jsonAttribute: string;
135
+ constructor(jsonAttribute?: string);
136
+ nextChunk(): Promise<any>;
137
+ }
138
+ export declare class ReadableSseJsonStream extends ReadableAbstractStream {
139
+ constructor(body: ReadableStream | null);
140
+ }
@@ -261,15 +261,21 @@ export class ComplexJsonStream extends JsonStream {
261
261
  return simpleValue(obj);
262
262
  }
263
263
  }
264
- export class ReadableJsonStream extends JsonStream {
265
- constructor(body) {
266
- super();
264
+ export class ReadableAbstractStream {
265
+ constructor(baseStream, body) {
266
+ Object.defineProperty(this, "baseStream", {
267
+ enumerable: true,
268
+ configurable: true,
269
+ writable: true,
270
+ value: void 0
271
+ });
267
272
  Object.defineProperty(this, "decoder", {
268
273
  enumerable: true,
269
274
  configurable: true,
270
275
  writable: true,
271
276
  value: void 0
272
277
  });
278
+ this.baseStream = baseStream;
273
279
  this.decoder = new TextDecoder("utf-8");
274
280
  if (body) {
275
281
  void this.run(body);
@@ -278,6 +284,19 @@ export class ReadableJsonStream extends JsonStream {
278
284
  console.error("Unexpected empty body while streaming");
279
285
  }
280
286
  }
287
+ appendBuffer(data) {
288
+ return this.baseStream.appendBuffer(data);
289
+ }
290
+ closeBuffer() {
291
+ return this.baseStream.closeBuffer();
292
+ }
293
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
294
+ nextChunk() {
295
+ return this.baseStream.nextChunk();
296
+ }
297
+ get streamDone() {
298
+ return this.baseStream.streamDone;
299
+ }
281
300
  async run(body) {
282
301
  const reader = body.getReader();
283
302
  let isDone = false;
@@ -294,3 +313,159 @@ export class ReadableJsonStream extends JsonStream {
294
313
  }
295
314
  }
296
315
  }
316
+ export class ReadableJsonStream extends ReadableAbstractStream {
317
+ constructor(body) {
318
+ super(new JsonStream(), body);
319
+ }
320
+ }
321
+ export class SseStream {
322
+ constructor() {
323
+ Object.defineProperty(this, "_buffer", {
324
+ enumerable: true,
325
+ configurable: true,
326
+ writable: true,
327
+ value: ""
328
+ });
329
+ Object.defineProperty(this, "_bufferOpen", {
330
+ enumerable: true,
331
+ configurable: true,
332
+ writable: true,
333
+ value: true
334
+ });
335
+ // Set up a potential Promise that the handler can resolve.
336
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
337
+ Object.defineProperty(this, "_chunkResolution", {
338
+ enumerable: true,
339
+ configurable: true,
340
+ writable: true,
341
+ value: void 0
342
+ });
343
+ // If there is no Promise (it is null), the handler must add it to the queue
344
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
345
+ Object.defineProperty(this, "_chunkPending", {
346
+ enumerable: true,
347
+ configurable: true,
348
+ writable: true,
349
+ value: null
350
+ });
351
+ // A queue that will collect chunks while there is no Promise
352
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
353
+ Object.defineProperty(this, "_chunkQueue", {
354
+ enumerable: true,
355
+ configurable: true,
356
+ writable: true,
357
+ value: []
358
+ });
359
+ }
360
+ appendBuffer(data) {
361
+ this._buffer += data;
362
+ this._parseBuffer();
363
+ }
364
+ closeBuffer() {
365
+ this._bufferOpen = false;
366
+ this._parseBuffer();
367
+ }
368
+ /**
369
+ * Attempt to load an entire event.
370
+ * For each entire event we load,
371
+ * send them to be handled.
372
+ */
373
+ _parseBuffer() {
374
+ const events = this._buffer.split(/\n\n/);
375
+ this._buffer = events.pop() ?? "";
376
+ events.forEach((event) => this._handleEvent(event.trim()));
377
+ if (!this._bufferOpen) {
378
+ // No more data will be added, and we have parsed
379
+ // everything. So dump the rest.
380
+ this._handleEvent(null);
381
+ this._buffer = "";
382
+ }
383
+ }
384
+ /**
385
+ * Given an event string, get all the fields
386
+ * in the event. It is assumed there is one field
387
+ * per line, but that field names can be duplicated,
388
+ * indicating to append the new value to the previous value
389
+ * @param event
390
+ */
391
+ _parseEvent(event) {
392
+ if (!event || event.trim() === "") {
393
+ return null;
394
+ }
395
+ const ret = {};
396
+ const lines = event.split(/\n/);
397
+ lines.forEach((line) => {
398
+ const match = line.match(/^([^:]+): \s*(.+)\n*$/);
399
+ if (match && match.length === 3) {
400
+ const key = match[1];
401
+ const val = match[2];
402
+ const cur = ret[key] ?? "";
403
+ ret[key] = `${cur}${val}`;
404
+ }
405
+ });
406
+ return ret;
407
+ }
408
+ _handleEvent(event) {
409
+ const chunk = this._parseEvent(event);
410
+ if (this._chunkPending) {
411
+ this._chunkResolution(chunk);
412
+ this._chunkPending = null;
413
+ }
414
+ else {
415
+ this._chunkQueue.push(chunk);
416
+ }
417
+ }
418
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
419
+ async nextChunk() {
420
+ if (this._chunkQueue.length > 0) {
421
+ // If there is data in the queue, return the next queue chunk
422
+ return this._chunkQueue.shift();
423
+ }
424
+ else {
425
+ // Otherwise, set up a promise that handleChunk will cause to be resolved
426
+ this._chunkPending = new Promise((resolve) => {
427
+ this._chunkResolution = resolve;
428
+ });
429
+ return this._chunkPending;
430
+ }
431
+ }
432
+ get streamDone() {
433
+ return (!this._bufferOpen &&
434
+ this._buffer.length === 0 &&
435
+ this._chunkQueue.length === 0 &&
436
+ this._chunkPending === null);
437
+ }
438
+ }
439
+ export class ReadableSseStream extends ReadableAbstractStream {
440
+ constructor(body) {
441
+ super(new SseStream(), body);
442
+ }
443
+ }
444
+ export class SseJsonStream extends SseStream {
445
+ constructor(jsonAttribute) {
446
+ super();
447
+ Object.defineProperty(this, "_jsonAttribute", {
448
+ enumerable: true,
449
+ configurable: true,
450
+ writable: true,
451
+ value: "data"
452
+ });
453
+ this._jsonAttribute = jsonAttribute ?? this._jsonAttribute;
454
+ }
455
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
456
+ async nextChunk() {
457
+ const eventRecord = (await super.nextChunk());
458
+ const json = eventRecord?.[this._jsonAttribute];
459
+ if (!json) {
460
+ return null;
461
+ }
462
+ else {
463
+ return JSON.parse(json);
464
+ }
465
+ }
466
+ }
467
+ export class ReadableSseJsonStream extends ReadableAbstractStream {
468
+ constructor(body) {
469
+ super(new SseJsonStream(), body);
470
+ }
471
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-common",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Core types and classes for Google services.",
5
5
  "type": "module",
6
6
  "engines": {