@langchain/google-common 0.2.17 → 1.0.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.
Files changed (169) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +6 -6
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/auth.cjs +82 -116
  5. package/dist/auth.cjs.map +1 -0
  6. package/dist/auth.d.cts +46 -0
  7. package/dist/auth.d.cts.map +1 -0
  8. package/dist/auth.d.ts +41 -36
  9. package/dist/auth.d.ts.map +1 -0
  10. package/dist/auth.js +80 -110
  11. package/dist/auth.js.map +1 -0
  12. package/dist/chat_models.cjs +251 -466
  13. package/dist/chat_models.cjs.map +1 -0
  14. package/dist/chat_models.d.cts +98 -0
  15. package/dist/chat_models.d.cts.map +1 -0
  16. package/dist/chat_models.d.ts +87 -73
  17. package/dist/chat_models.d.ts.map +1 -0
  18. package/dist/chat_models.js +245 -457
  19. package/dist/chat_models.js.map +1 -0
  20. package/dist/connection.cjs +321 -466
  21. package/dist/connection.cjs.map +1 -0
  22. package/dist/connection.d.cts +109 -0
  23. package/dist/connection.d.cts.map +1 -0
  24. package/dist/connection.d.ts +98 -91
  25. package/dist/connection.d.ts.map +1 -0
  26. package/dist/connection.js +317 -459
  27. package/dist/connection.js.map +1 -0
  28. package/dist/embeddings.cjs +135 -186
  29. package/dist/embeddings.cjs.map +1 -0
  30. package/dist/embeddings.d.cts +44 -0
  31. package/dist/embeddings.d.cts.map +1 -0
  32. package/dist/embeddings.d.ts +38 -32
  33. package/dist/embeddings.d.ts.map +1 -0
  34. package/dist/embeddings.js +133 -181
  35. package/dist/embeddings.js.map +1 -0
  36. package/dist/experimental/media.cjs +380 -482
  37. package/dist/experimental/media.cjs.map +1 -0
  38. package/dist/experimental/media.d.cts +198 -0
  39. package/dist/experimental/media.d.cts.map +1 -0
  40. package/dist/experimental/media.d.ts +190 -202
  41. package/dist/experimental/media.d.ts.map +1 -0
  42. package/dist/experimental/media.js +369 -468
  43. package/dist/experimental/media.js.map +1 -0
  44. package/dist/experimental/utils/media_core.cjs +403 -517
  45. package/dist/experimental/utils/media_core.cjs.map +1 -0
  46. package/dist/experimental/utils/media_core.d.cts +215 -0
  47. package/dist/experimental/utils/media_core.d.cts.map +1 -0
  48. package/dist/experimental/utils/media_core.d.ts +171 -165
  49. package/dist/experimental/utils/media_core.d.ts.map +1 -0
  50. package/dist/experimental/utils/media_core.js +395 -506
  51. package/dist/experimental/utils/media_core.js.map +1 -0
  52. package/dist/index.cjs +58 -27
  53. package/dist/index.d.cts +13 -0
  54. package/dist/index.d.ts +13 -11
  55. package/dist/index.js +13 -11
  56. package/dist/llms.cjs +157 -244
  57. package/dist/llms.cjs.map +1 -0
  58. package/dist/llms.d.cts +72 -0
  59. package/dist/llms.d.cts.map +1 -0
  60. package/dist/llms.d.ts +64 -54
  61. package/dist/llms.d.ts.map +1 -0
  62. package/dist/llms.js +154 -238
  63. package/dist/llms.js.map +1 -0
  64. package/dist/output_parsers.cjs +148 -173
  65. package/dist/output_parsers.cjs.map +1 -0
  66. package/dist/output_parsers.d.cts +53 -0
  67. package/dist/output_parsers.d.cts.map +1 -0
  68. package/dist/output_parsers.d.ts +46 -42
  69. package/dist/output_parsers.d.ts.map +1 -0
  70. package/dist/output_parsers.js +146 -168
  71. package/dist/output_parsers.js.map +1 -0
  72. package/dist/types-anthropic.d.cts +229 -0
  73. package/dist/types-anthropic.d.cts.map +1 -0
  74. package/dist/types-anthropic.d.ts +221 -215
  75. package/dist/types-anthropic.d.ts.map +1 -0
  76. package/dist/types.cjs +51 -62
  77. package/dist/types.cjs.map +1 -0
  78. package/dist/types.d.cts +748 -0
  79. package/dist/types.d.cts.map +1 -0
  80. package/dist/types.d.ts +669 -656
  81. package/dist/types.d.ts.map +1 -0
  82. package/dist/types.js +46 -45
  83. package/dist/types.js.map +1 -0
  84. package/dist/utils/anthropic.cjs +598 -821
  85. package/dist/utils/anthropic.cjs.map +1 -0
  86. package/dist/utils/anthropic.js +597 -818
  87. package/dist/utils/anthropic.js.map +1 -0
  88. package/dist/utils/common.cjs +130 -211
  89. package/dist/utils/common.cjs.map +1 -0
  90. package/dist/utils/common.d.cts +13 -0
  91. package/dist/utils/common.d.cts.map +1 -0
  92. package/dist/utils/common.d.ts +12 -7
  93. package/dist/utils/common.d.ts.map +1 -0
  94. package/dist/utils/common.js +128 -207
  95. package/dist/utils/common.js.map +1 -0
  96. package/dist/utils/failed_handler.cjs +28 -30
  97. package/dist/utils/failed_handler.cjs.map +1 -0
  98. package/dist/utils/failed_handler.d.cts +9 -0
  99. package/dist/utils/failed_handler.d.cts.map +1 -0
  100. package/dist/utils/failed_handler.d.ts +8 -2
  101. package/dist/utils/failed_handler.d.ts.map +1 -0
  102. package/dist/utils/failed_handler.js +28 -28
  103. package/dist/utils/failed_handler.js.map +1 -0
  104. package/dist/utils/gemini.cjs +1020 -1488
  105. package/dist/utils/gemini.cjs.map +1 -0
  106. package/dist/utils/gemini.d.cts +51 -0
  107. package/dist/utils/gemini.d.cts.map +1 -0
  108. package/dist/utils/gemini.d.ts +51 -48
  109. package/dist/utils/gemini.d.ts.map +1 -0
  110. package/dist/utils/gemini.js +1015 -1479
  111. package/dist/utils/gemini.js.map +1 -0
  112. package/dist/utils/index.cjs +38 -23
  113. package/dist/utils/index.d.cts +8 -0
  114. package/dist/utils/index.d.ts +8 -7
  115. package/dist/utils/index.js +8 -7
  116. package/dist/utils/palm.d.cts +11 -0
  117. package/dist/utils/palm.d.cts.map +1 -0
  118. package/dist/utils/palm.d.ts +9 -4
  119. package/dist/utils/palm.d.ts.map +1 -0
  120. package/dist/utils/safety.cjs +13 -22
  121. package/dist/utils/safety.cjs.map +1 -0
  122. package/dist/utils/safety.d.cts +12 -0
  123. package/dist/utils/safety.d.cts.map +1 -0
  124. package/dist/utils/safety.d.ts +10 -4
  125. package/dist/utils/safety.d.ts.map +1 -0
  126. package/dist/utils/safety.js +13 -19
  127. package/dist/utils/safety.js.map +1 -0
  128. package/dist/utils/stream.cjs +296 -475
  129. package/dist/utils/stream.cjs.map +1 -0
  130. package/dist/utils/stream.d.cts +165 -0
  131. package/dist/utils/stream.d.cts.map +1 -0
  132. package/dist/utils/stream.d.ts +156 -131
  133. package/dist/utils/stream.d.ts.map +1 -0
  134. package/dist/utils/stream.js +293 -469
  135. package/dist/utils/stream.js.map +1 -0
  136. package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
  137. package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
  138. package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
  139. package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
  140. package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
  141. package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
  142. package/dist/utils/zod_to_gemini_parameters.js +40 -76
  143. package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
  144. package/package.json +69 -85
  145. package/dist/types-anthropic.cjs +0 -2
  146. package/dist/types-anthropic.js +0 -1
  147. package/dist/utils/anthropic.d.ts +0 -4
  148. package/dist/utils/palm.cjs +0 -2
  149. package/dist/utils/palm.js +0 -1
  150. package/experimental/media.cjs +0 -1
  151. package/experimental/media.d.cts +0 -1
  152. package/experimental/media.d.ts +0 -1
  153. package/experimental/media.js +0 -1
  154. package/experimental/utils/media_core.cjs +0 -1
  155. package/experimental/utils/media_core.d.cts +0 -1
  156. package/experimental/utils/media_core.d.ts +0 -1
  157. package/experimental/utils/media_core.js +0 -1
  158. package/index.cjs +0 -1
  159. package/index.d.cts +0 -1
  160. package/index.d.ts +0 -1
  161. package/index.js +0 -1
  162. package/types.cjs +0 -1
  163. package/types.d.cts +0 -1
  164. package/types.d.ts +0 -1
  165. package/types.js +0 -1
  166. package/utils.cjs +0 -1
  167. package/utils.d.cts +0 -1
  168. package/utils.d.ts +0 -1
  169. package/utils.js +0 -1
@@ -1,471 +1,295 @@
1
- export function complexValue(value) {
2
- if (value === null || typeof value === "undefined") {
3
- // I dunno what to put here. An error, probably
4
- return undefined;
5
- }
6
- else if (typeof value === "object") {
7
- if (Array.isArray(value)) {
8
- return {
9
- list_val: value.map((avalue) => complexValue(avalue)),
10
- };
11
- }
12
- else {
13
- const ret = {};
14
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- const v = value;
16
- Object.keys(v).forEach((key) => {
17
- ret[key] = complexValue(v[key]);
18
- });
19
- return { struct_val: ret };
20
- }
21
- }
22
- else if (typeof value === "number") {
23
- if (Number.isInteger(value)) {
24
- return { int_val: value };
25
- }
26
- else {
27
- return { float_val: value };
28
- }
29
- }
30
- else {
31
- return {
32
- string_val: [value],
33
- };
34
- }
1
+ //#region src/utils/stream.ts
2
+ function complexValue(value) {
3
+ if (value === null || typeof value === "undefined") return void 0;
4
+ else if (typeof value === "object") if (Array.isArray(value)) return { list_val: value.map((avalue) => complexValue(avalue)) };
5
+ else {
6
+ const ret = {};
7
+ const v = value;
8
+ Object.keys(v).forEach((key) => {
9
+ ret[key] = complexValue(v[key]);
10
+ });
11
+ return { struct_val: ret };
12
+ }
13
+ else if (typeof value === "number") if (Number.isInteger(value)) return { int_val: value };
14
+ else return { float_val: value };
15
+ else return { string_val: [value] };
35
16
  }
36
- export function simpleValue(val) {
37
- if (val && typeof val === "object" && !Array.isArray(val)) {
38
- // eslint-disable-next-line no-prototype-builtins
39
- if (val.hasOwnProperty("stringVal")) {
40
- return val.stringVal[0];
41
- // eslint-disable-next-line no-prototype-builtins
42
- }
43
- else if (val.hasOwnProperty("boolVal")) {
44
- return val.boolVal[0];
45
- // eslint-disable-next-line no-prototype-builtins
46
- }
47
- else if (val.hasOwnProperty("listVal")) {
48
- const { listVal } = val;
49
- return listVal.map((aval) => simpleValue(aval));
50
- // eslint-disable-next-line no-prototype-builtins
51
- }
52
- else if (val.hasOwnProperty("structVal")) {
53
- const ret = {};
54
- const struct = val.structVal;
55
- Object.keys(struct).forEach((key) => {
56
- ret[key] = simpleValue(struct[key]);
57
- });
58
- return ret;
59
- }
60
- else {
61
- const ret = {};
62
- const struct = val;
63
- Object.keys(struct).forEach((key) => {
64
- ret[key] = simpleValue(struct[key]);
65
- });
66
- return ret;
67
- }
68
- }
69
- else if (Array.isArray(val)) {
70
- return val.map((aval) => simpleValue(aval));
71
- }
72
- else {
73
- return val;
74
- }
75
- }
76
- export class JsonStream {
77
- constructor() {
78
- Object.defineProperty(this, "_buffer", {
79
- enumerable: true,
80
- configurable: true,
81
- writable: true,
82
- value: ""
83
- });
84
- Object.defineProperty(this, "_bufferOpen", {
85
- enumerable: true,
86
- configurable: true,
87
- writable: true,
88
- value: true
89
- });
90
- Object.defineProperty(this, "_firstRun", {
91
- enumerable: true,
92
- configurable: true,
93
- writable: true,
94
- value: true
95
- });
96
- // Set up a potential Promise that the handler can resolve.
97
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
- Object.defineProperty(this, "_chunkResolution", {
99
- enumerable: true,
100
- configurable: true,
101
- writable: true,
102
- value: void 0
103
- });
104
- // If there is no Promise (it is null), the handler must add it to the queue
105
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
- Object.defineProperty(this, "_chunkPending", {
107
- enumerable: true,
108
- configurable: true,
109
- writable: true,
110
- value: null
111
- });
112
- // A queue that will collect chunks while there is no Promise
113
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
114
- Object.defineProperty(this, "_chunkQueue", {
115
- enumerable: true,
116
- configurable: true,
117
- writable: true,
118
- value: []
119
- });
120
- }
121
- /**
122
- * Add data to the buffer. This may cause chunks to be generated, if available.
123
- * @param data
124
- */
125
- appendBuffer(data) {
126
- this._buffer += data;
127
- // Our first time, skip to the opening of the array
128
- if (this._firstRun) {
129
- this._skipTo("[");
130
- this._firstRun = false;
131
- }
132
- this._parseBuffer();
133
- }
134
- /**
135
- * Indicate there is no more data that will be added to the text buffer.
136
- * This should be called when all the data has been read and added to indicate
137
- * that we should process everything remaining in the buffer.
138
- */
139
- closeBuffer() {
140
- this._bufferOpen = false;
141
- this._parseBuffer();
142
- }
143
- /**
144
- * Skip characters in the buffer till we get to the start of an object.
145
- * Then attempt to read a full object.
146
- * If we do read a full object, turn it into a chunk and send it to the chunk handler.
147
- * Repeat this for as much as we can.
148
- */
149
- _parseBuffer() {
150
- let obj = null;
151
- do {
152
- this._skipTo("{");
153
- obj = this._getFullObject();
154
- if (obj !== null) {
155
- const chunk = this._simplifyObject(obj);
156
- this._handleChunk(chunk);
157
- }
158
- } while (obj !== null);
159
- if (!this._bufferOpen) {
160
- // No more data will be added, and we have parsed everything we could,
161
- // so everything else is garbage.
162
- this._handleChunk(null);
163
- this._buffer = "";
164
- }
165
- }
166
- /**
167
- * If the string is present, move the start of the buffer to the first occurrence
168
- * of that string. This is useful for skipping over elements or parts that we're not
169
- * really interested in parsing. (ie - the opening characters, comma separators, etc.)
170
- * @param start The string to start the buffer with
171
- */
172
- _skipTo(start) {
173
- const index = this._buffer.indexOf(start);
174
- if (index > 0) {
175
- this._buffer = this._buffer.slice(index);
176
- }
177
- }
178
- /**
179
- * Given what is in the buffer, parse a single object out of it.
180
- * If a complete object isn't available, return null.
181
- * Assumes that we are at the start of an object to parse.
182
- */
183
- _getFullObject() {
184
- let ret = null;
185
- // Loop while we don't have something to return AND we have something in the buffer
186
- let index = 0;
187
- while (ret === null && this._buffer.length > index) {
188
- // Advance to the next close bracket after our current index
189
- index = this._buffer.indexOf("}", index + 1);
190
- // If we don't find one, exit with null
191
- if (index === -1) {
192
- return null;
193
- }
194
- // If we have one, try to turn it into an object to return
195
- try {
196
- const objStr = this._buffer.substring(0, index + 1);
197
- ret = JSON.parse(objStr);
198
- // We only get here if it parsed it ok
199
- // If we did turn it into an object, remove it from the buffer
200
- this._buffer = this._buffer.slice(index + 1);
201
- }
202
- catch (xx) {
203
- // It didn't parse it correctly, so we swallow the exception and continue
204
- }
205
- }
206
- return ret;
207
- }
208
- _simplifyObject(obj) {
209
- return obj;
210
- }
211
- /**
212
- * Register that we have another chunk available for consumption.
213
- * If we are waiting for a chunk, resolve the promise waiting for it immediately.
214
- * If not, then add it to the queue.
215
- * @param chunk
216
- */
217
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
218
- _handleChunk(chunk) {
219
- if (this._chunkPending) {
220
- this._chunkResolution(chunk);
221
- this._chunkPending = null;
222
- }
223
- else {
224
- this._chunkQueue.push(chunk);
225
- }
226
- }
227
- /**
228
- * Get the next chunk that is coming from the stream.
229
- * This chunk may be null, usually indicating the last chunk in the stream.
230
- */
231
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
232
- async nextChunk() {
233
- if (this._chunkQueue.length > 0) {
234
- // If there is data in the queue, return the next queue chunk
235
- return this._chunkQueue.shift();
236
- }
237
- else {
238
- // Otherwise, set up a promise that handleChunk will cause to be resolved
239
- this._chunkPending = new Promise((resolve) => {
240
- this._chunkResolution = resolve;
241
- });
242
- return this._chunkPending;
243
- }
244
- }
245
- /**
246
- * Is the stream done?
247
- * A stream is only done if all of the following are true:
248
- * - There is no more data to be added to the text buffer
249
- * - There is no more data in the text buffer
250
- * - There are no chunks that are waiting to be consumed
251
- */
252
- get streamDone() {
253
- return (!this._bufferOpen &&
254
- this._buffer.length === 0 &&
255
- this._chunkQueue.length === 0 &&
256
- this._chunkPending === null);
257
- }
258
- }
259
- export class ComplexJsonStream extends JsonStream {
260
- _simplifyObject(obj) {
261
- return simpleValue(obj);
262
- }
263
- }
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
- });
272
- Object.defineProperty(this, "decoder", {
273
- enumerable: true,
274
- configurable: true,
275
- writable: true,
276
- value: void 0
277
- });
278
- this.baseStream = baseStream;
279
- this.decoder = new TextDecoder("utf-8");
280
- if (body) {
281
- void this.run(body);
282
- }
283
- else {
284
- console.error("Unexpected empty body while streaming");
285
- }
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
- }
300
- // Should be a ReadableStream, but the Gaxios Readable stream isn't.
301
- // But both should support async iterators, so make sure of that.
302
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
303
- async run(body) {
304
- if (typeof body[Symbol.asyncIterator] === "function") {
305
- for await (const value of body) {
306
- const svalue = this.decoder.decode(value, { stream: true });
307
- this.appendBuffer(svalue);
308
- }
309
- this.closeBuffer();
310
- }
311
- else {
312
- throw Error("Stream must implement async iterator.");
313
- }
314
- }
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
- }
17
+ function simpleValue(val) {
18
+ if (val && typeof val === "object" && !Array.isArray(val)) if (val.hasOwnProperty("stringVal")) return val.stringVal[0];
19
+ else if (val.hasOwnProperty("boolVal")) return val.boolVal[0];
20
+ else if (val.hasOwnProperty("listVal")) {
21
+ const { listVal } = val;
22
+ return listVal.map((aval) => simpleValue(aval));
23
+ } else if (val.hasOwnProperty("structVal")) {
24
+ const ret = {};
25
+ const struct = val.structVal;
26
+ Object.keys(struct).forEach((key) => {
27
+ ret[key] = simpleValue(struct[key]);
28
+ });
29
+ return ret;
30
+ } else {
31
+ const ret = {};
32
+ const struct = val;
33
+ Object.keys(struct).forEach((key) => {
34
+ ret[key] = simpleValue(struct[key]);
35
+ });
36
+ return ret;
37
+ }
38
+ else if (Array.isArray(val)) return val.map((aval) => simpleValue(aval));
39
+ else return val;
471
40
  }
41
+ var JsonStream = class {
42
+ _buffer = "";
43
+ _bufferOpen = true;
44
+ _firstRun = true;
45
+ /**
46
+ * Add data to the buffer. This may cause chunks to be generated, if available.
47
+ * @param data
48
+ */
49
+ appendBuffer(data) {
50
+ this._buffer += data;
51
+ if (this._firstRun) {
52
+ this._skipTo("[");
53
+ this._firstRun = false;
54
+ }
55
+ this._parseBuffer();
56
+ }
57
+ /**
58
+ * Indicate there is no more data that will be added to the text buffer.
59
+ * This should be called when all the data has been read and added to indicate
60
+ * that we should process everything remaining in the buffer.
61
+ */
62
+ closeBuffer() {
63
+ this._bufferOpen = false;
64
+ this._parseBuffer();
65
+ }
66
+ /**
67
+ * Skip characters in the buffer till we get to the start of an object.
68
+ * Then attempt to read a full object.
69
+ * If we do read a full object, turn it into a chunk and send it to the chunk handler.
70
+ * Repeat this for as much as we can.
71
+ */
72
+ _parseBuffer() {
73
+ let obj = null;
74
+ do {
75
+ this._skipTo("{");
76
+ obj = this._getFullObject();
77
+ if (obj !== null) {
78
+ const chunk = this._simplifyObject(obj);
79
+ this._handleChunk(chunk);
80
+ }
81
+ } while (obj !== null);
82
+ if (!this._bufferOpen) {
83
+ this._handleChunk(null);
84
+ this._buffer = "";
85
+ }
86
+ }
87
+ /**
88
+ * If the string is present, move the start of the buffer to the first occurrence
89
+ * of that string. This is useful for skipping over elements or parts that we're not
90
+ * really interested in parsing. (ie - the opening characters, comma separators, etc.)
91
+ * @param start The string to start the buffer with
92
+ */
93
+ _skipTo(start) {
94
+ const index = this._buffer.indexOf(start);
95
+ if (index > 0) this._buffer = this._buffer.slice(index);
96
+ }
97
+ /**
98
+ * Given what is in the buffer, parse a single object out of it.
99
+ * If a complete object isn't available, return null.
100
+ * Assumes that we are at the start of an object to parse.
101
+ */
102
+ _getFullObject() {
103
+ let ret = null;
104
+ let index = 0;
105
+ while (ret === null && this._buffer.length > index) {
106
+ index = this._buffer.indexOf("}", index + 1);
107
+ if (index === -1) return null;
108
+ try {
109
+ const objStr = this._buffer.substring(0, index + 1);
110
+ ret = JSON.parse(objStr);
111
+ this._buffer = this._buffer.slice(index + 1);
112
+ } catch {}
113
+ }
114
+ return ret;
115
+ }
116
+ _simplifyObject(obj) {
117
+ return obj;
118
+ }
119
+ _chunkResolution;
120
+ _chunkPending = null;
121
+ _chunkQueue = [];
122
+ /**
123
+ * Register that we have another chunk available for consumption.
124
+ * If we are waiting for a chunk, resolve the promise waiting for it immediately.
125
+ * If not, then add it to the queue.
126
+ * @param chunk
127
+ */
128
+ _handleChunk(chunk) {
129
+ if (this._chunkPending) {
130
+ this._chunkResolution(chunk);
131
+ this._chunkPending = null;
132
+ } else this._chunkQueue.push(chunk);
133
+ }
134
+ /**
135
+ * Get the next chunk that is coming from the stream.
136
+ * This chunk may be null, usually indicating the last chunk in the stream.
137
+ */
138
+ async nextChunk() {
139
+ if (this._chunkQueue.length > 0) return this._chunkQueue.shift();
140
+ else {
141
+ this._chunkPending = new Promise((resolve) => {
142
+ this._chunkResolution = resolve;
143
+ });
144
+ return this._chunkPending;
145
+ }
146
+ }
147
+ /**
148
+ * Is the stream done?
149
+ * A stream is only done if all of the following are true:
150
+ * - There is no more data to be added to the text buffer
151
+ * - There is no more data in the text buffer
152
+ * - There are no chunks that are waiting to be consumed
153
+ */
154
+ get streamDone() {
155
+ return !this._bufferOpen && this._buffer.length === 0 && this._chunkQueue.length === 0 && this._chunkPending === null;
156
+ }
157
+ };
158
+ var ComplexJsonStream = class extends JsonStream {
159
+ _simplifyObject(obj) {
160
+ return simpleValue(obj);
161
+ }
162
+ };
163
+ var ReadableAbstractStream = class {
164
+ baseStream;
165
+ decoder;
166
+ constructor(baseStream, body) {
167
+ this.baseStream = baseStream;
168
+ this.decoder = new TextDecoder("utf-8");
169
+ if (body) this.run(body);
170
+ else console.error("Unexpected empty body while streaming");
171
+ }
172
+ appendBuffer(data) {
173
+ return this.baseStream.appendBuffer(data);
174
+ }
175
+ closeBuffer() {
176
+ return this.baseStream.closeBuffer();
177
+ }
178
+ nextChunk() {
179
+ return this.baseStream.nextChunk();
180
+ }
181
+ get streamDone() {
182
+ return this.baseStream.streamDone;
183
+ }
184
+ async run(body) {
185
+ if (typeof body[Symbol.asyncIterator] === "function") {
186
+ for await (const value of body) {
187
+ const svalue = this.decoder.decode(value, { stream: true });
188
+ this.appendBuffer(svalue);
189
+ }
190
+ this.closeBuffer();
191
+ } else throw Error("Stream must implement async iterator.");
192
+ }
193
+ };
194
+ var ReadableJsonStream = class extends ReadableAbstractStream {
195
+ constructor(body) {
196
+ super(new JsonStream(), body);
197
+ }
198
+ };
199
+ var SseStream = class {
200
+ _buffer = "";
201
+ _bufferOpen = true;
202
+ appendBuffer(data) {
203
+ this._buffer += data;
204
+ this._parseBuffer();
205
+ }
206
+ closeBuffer() {
207
+ this._bufferOpen = false;
208
+ this._parseBuffer();
209
+ }
210
+ /**
211
+ * Attempt to load an entire event.
212
+ * For each entire event we load,
213
+ * send them to be handled.
214
+ */
215
+ _parseBuffer() {
216
+ const events = this._buffer.split(/\n\n/);
217
+ this._buffer = events.pop() ?? "";
218
+ events.forEach((event) => this._handleEvent(event.trim()));
219
+ if (!this._bufferOpen) {
220
+ this._handleEvent(null);
221
+ this._buffer = "";
222
+ }
223
+ }
224
+ /**
225
+ * Given an event string, get all the fields
226
+ * in the event. It is assumed there is one field
227
+ * per line, but that field names can be duplicated,
228
+ * indicating to append the new value to the previous value
229
+ * @param event
230
+ */
231
+ _parseEvent(event) {
232
+ if (!event || event.trim() === "") return null;
233
+ const ret = {};
234
+ const lines = event.split(/\n/);
235
+ lines.forEach((line) => {
236
+ const match = line.match(/^([^:]+): \s*(.+)\n*$/);
237
+ if (match && match.length === 3) {
238
+ const key = match[1];
239
+ const val = match[2];
240
+ const cur = ret[key] ?? "";
241
+ ret[key] = `${cur}${val}`;
242
+ }
243
+ });
244
+ return ret;
245
+ }
246
+ _chunkResolution;
247
+ _chunkPending = null;
248
+ _chunkQueue = [];
249
+ _handleEvent(event) {
250
+ const chunk = this._parseEvent(event);
251
+ if (this._chunkPending) {
252
+ this._chunkResolution(chunk);
253
+ this._chunkPending = null;
254
+ } else this._chunkQueue.push(chunk);
255
+ }
256
+ async nextChunk() {
257
+ if (this._chunkQueue.length > 0) return this._chunkQueue.shift();
258
+ else {
259
+ this._chunkPending = new Promise((resolve) => {
260
+ this._chunkResolution = resolve;
261
+ });
262
+ return this._chunkPending;
263
+ }
264
+ }
265
+ get streamDone() {
266
+ return !this._bufferOpen && this._buffer.length === 0 && this._chunkQueue.length === 0 && this._chunkPending === null;
267
+ }
268
+ };
269
+ var ReadableSseStream = class extends ReadableAbstractStream {
270
+ constructor(body) {
271
+ super(new SseStream(), body);
272
+ }
273
+ };
274
+ var SseJsonStream = class extends SseStream {
275
+ _jsonAttribute = "data";
276
+ constructor(jsonAttribute) {
277
+ super();
278
+ this._jsonAttribute = jsonAttribute ?? this._jsonAttribute;
279
+ }
280
+ async nextChunk() {
281
+ const eventRecord = await super.nextChunk();
282
+ const json = eventRecord?.[this._jsonAttribute];
283
+ if (!json) return null;
284
+ else return JSON.parse(json);
285
+ }
286
+ };
287
+ var ReadableSseJsonStream = class extends ReadableAbstractStream {
288
+ constructor(body) {
289
+ super(new SseJsonStream(), body);
290
+ }
291
+ };
292
+
293
+ //#endregion
294
+ export { ComplexJsonStream, JsonStream, ReadableAbstractStream, ReadableJsonStream, ReadableSseJsonStream, ReadableSseStream, SseJsonStream, SseStream, complexValue, simpleValue };
295
+ //# sourceMappingURL=stream.js.map