@langchain/core 0.2.0-rc.0 → 0.2.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/callbacks/base.cjs +9 -1
- package/dist/callbacks/base.d.ts +3 -0
- package/dist/callbacks/base.js +9 -1
- package/dist/callbacks/manager.cjs +51 -0
- package/dist/callbacks/manager.js +51 -0
- package/dist/language_models/base.cjs +3 -0
- package/dist/language_models/base.js +3 -0
- package/dist/language_models/chat_models.cjs +21 -3
- package/dist/language_models/chat_models.d.ts +9 -0
- package/dist/language_models/chat_models.js +21 -3
- package/dist/runnables/base.cjs +88 -14
- package/dist/runnables/base.d.ts +4 -4
- package/dist/runnables/base.js +88 -14
- package/dist/runnables/iter.cjs +46 -0
- package/dist/runnables/iter.d.ts +5 -0
- package/dist/runnables/iter.js +39 -0
- package/dist/runnables/passthrough.cjs +1 -0
- package/dist/runnables/passthrough.d.ts +1 -1
- package/dist/runnables/passthrough.js +1 -0
- package/dist/runnables/remote.cjs +60 -48
- package/dist/runnables/remote.d.ts +6 -2
- package/dist/runnables/remote.js +61 -49
- package/dist/singletons/index.cjs +1 -1
- package/dist/singletons/index.d.ts +2 -2
- package/dist/singletons/index.js +1 -1
- package/dist/utils/stream.cjs +27 -11
- package/dist/utils/stream.d.ts +6 -1
- package/dist/utils/stream.js +27 -11
- package/package.json +2 -2
package/dist/runnables/base.cjs
CHANGED
|
@@ -17,6 +17,7 @@ const utils_js_1 = require("./utils.cjs");
|
|
|
17
17
|
const index_js_1 = require("../singletons/index.cjs");
|
|
18
18
|
const graph_js_1 = require("./graph.cjs");
|
|
19
19
|
const wrappers_js_1 = require("./wrappers.cjs");
|
|
20
|
+
const iter_js_1 = require("./iter.cjs");
|
|
20
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
22
|
function _coerceToDict(value, defaultKey) {
|
|
22
23
|
return value &&
|
|
@@ -167,7 +168,11 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
167
168
|
async stream(input, options) {
|
|
168
169
|
// Buffer the first streamed chunk to allow for initial errors
|
|
169
170
|
// to surface immediately.
|
|
170
|
-
const
|
|
171
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
172
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
|
|
173
|
+
generator: this._streamIterator(input, config),
|
|
174
|
+
config,
|
|
175
|
+
});
|
|
171
176
|
await wrappedGenerator.setup;
|
|
172
177
|
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
173
178
|
}
|
|
@@ -449,13 +454,13 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
449
454
|
await runnableStreamConsumePromise;
|
|
450
455
|
}
|
|
451
456
|
}
|
|
452
|
-
|
|
457
|
+
streamEvents(input, options, streamOptions) {
|
|
458
|
+
const stream = this._streamEvents(input, options, streamOptions);
|
|
453
459
|
if (options.encoding === "text/event-stream") {
|
|
454
|
-
|
|
455
|
-
yield* (0, wrappers_js_1.convertToHttpEventStream)(stream);
|
|
460
|
+
return (0, wrappers_js_1.convertToHttpEventStream)(stream);
|
|
456
461
|
}
|
|
457
462
|
else {
|
|
458
|
-
|
|
463
|
+
return stream_js_1.IterableReadableStream.fromAsyncGenerator(stream);
|
|
459
464
|
}
|
|
460
465
|
}
|
|
461
466
|
async *_streamEvents(input, options, streamOptions) {
|
|
@@ -739,11 +744,16 @@ class RunnableBinding extends Runnable {
|
|
|
739
744
|
generator, options) {
|
|
740
745
|
yield* this.bound.transform(generator, await this._mergeConfig((0, config_js_1.ensureConfig)(options), this.kwargs));
|
|
741
746
|
}
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
+
streamEvents(input, options, streamOptions) {
|
|
748
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
749
|
+
const outerThis = this;
|
|
750
|
+
const generator = async function* () {
|
|
751
|
+
yield* outerThis.bound.streamEvents(input, {
|
|
752
|
+
...(await outerThis._mergeConfig((0, config_js_1.ensureConfig)(options), outerThis.kwargs)),
|
|
753
|
+
version: options.version,
|
|
754
|
+
}, streamOptions);
|
|
755
|
+
};
|
|
756
|
+
return stream_js_1.IterableReadableStream.fromAsyncGenerator(generator());
|
|
747
757
|
}
|
|
748
758
|
static isRunnableBinding(
|
|
749
759
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1291,7 +1301,11 @@ class RunnableMap extends Runnable {
|
|
|
1291
1301
|
async function* generator() {
|
|
1292
1302
|
yield input;
|
|
1293
1303
|
}
|
|
1294
|
-
const
|
|
1304
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
1305
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
|
|
1306
|
+
generator: this.transform(generator(), config),
|
|
1307
|
+
config,
|
|
1308
|
+
});
|
|
1295
1309
|
await wrappedGenerator.setup;
|
|
1296
1310
|
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1297
1311
|
}
|
|
@@ -1346,6 +1360,44 @@ class RunnableLambda extends Runnable {
|
|
|
1346
1360
|
recursionLimit: (childConfig.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
|
|
1347
1361
|
});
|
|
1348
1362
|
}
|
|
1363
|
+
else if ((0, iter_js_1.isAsyncIterable)(output)) {
|
|
1364
|
+
let finalOutput;
|
|
1365
|
+
for await (const chunk of (0, iter_js_1.consumeAsyncIterableInContext)(childConfig, output)) {
|
|
1366
|
+
if (finalOutput === undefined) {
|
|
1367
|
+
finalOutput = chunk;
|
|
1368
|
+
}
|
|
1369
|
+
else {
|
|
1370
|
+
// Make a best effort to gather, for any type that supports concat.
|
|
1371
|
+
try {
|
|
1372
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1373
|
+
finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
|
|
1374
|
+
}
|
|
1375
|
+
catch (e) {
|
|
1376
|
+
finalOutput = chunk;
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
output = finalOutput;
|
|
1381
|
+
}
|
|
1382
|
+
else if ((0, iter_js_1.isIterator)(output)) {
|
|
1383
|
+
let finalOutput;
|
|
1384
|
+
for (const chunk of (0, iter_js_1.consumeIteratorInContext)(childConfig, output)) {
|
|
1385
|
+
if (finalOutput === undefined) {
|
|
1386
|
+
finalOutput = chunk;
|
|
1387
|
+
}
|
|
1388
|
+
else {
|
|
1389
|
+
// Make a best effort to gather, for any type that supports concat.
|
|
1390
|
+
try {
|
|
1391
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1392
|
+
finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
|
|
1393
|
+
}
|
|
1394
|
+
catch (e) {
|
|
1395
|
+
finalOutput = chunk;
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
output = finalOutput;
|
|
1400
|
+
}
|
|
1349
1401
|
resolve(output);
|
|
1350
1402
|
}
|
|
1351
1403
|
catch (e) {
|
|
@@ -1400,6 +1452,16 @@ class RunnableLambda extends Runnable {
|
|
|
1400
1452
|
yield chunk;
|
|
1401
1453
|
}
|
|
1402
1454
|
}
|
|
1455
|
+
else if ((0, iter_js_1.isAsyncIterable)(output)) {
|
|
1456
|
+
for await (const chunk of (0, iter_js_1.consumeAsyncIterableInContext)(config, output)) {
|
|
1457
|
+
yield chunk;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
else if ((0, iter_js_1.isIterator)(output)) {
|
|
1461
|
+
for (const chunk of (0, iter_js_1.consumeIteratorInContext)(config, output)) {
|
|
1462
|
+
yield chunk;
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1403
1465
|
else {
|
|
1404
1466
|
yield output;
|
|
1405
1467
|
}
|
|
@@ -1411,7 +1473,11 @@ class RunnableLambda extends Runnable {
|
|
|
1411
1473
|
async function* generator() {
|
|
1412
1474
|
yield input;
|
|
1413
1475
|
}
|
|
1414
|
-
const
|
|
1476
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
1477
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
|
|
1478
|
+
generator: this.transform(generator(), config),
|
|
1479
|
+
config,
|
|
1480
|
+
});
|
|
1415
1481
|
await wrappedGenerator.setup;
|
|
1416
1482
|
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1417
1483
|
}
|
|
@@ -1615,7 +1681,11 @@ class RunnableAssign extends Runnable {
|
|
|
1615
1681
|
async function* generator() {
|
|
1616
1682
|
yield input;
|
|
1617
1683
|
}
|
|
1618
|
-
const
|
|
1684
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
1685
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
|
|
1686
|
+
generator: this.transform(generator(), config),
|
|
1687
|
+
config,
|
|
1688
|
+
});
|
|
1619
1689
|
await wrappedGenerator.setup;
|
|
1620
1690
|
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1621
1691
|
}
|
|
@@ -1683,7 +1753,11 @@ class RunnablePick extends Runnable {
|
|
|
1683
1753
|
async function* generator() {
|
|
1684
1754
|
yield input;
|
|
1685
1755
|
}
|
|
1686
|
-
const
|
|
1756
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
1757
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
|
|
1758
|
+
generator: this.transform(generator(), config),
|
|
1759
|
+
config,
|
|
1760
|
+
});
|
|
1687
1761
|
await wrappedGenerator.setup;
|
|
1688
1762
|
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1689
1763
|
}
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -198,11 +198,11 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
198
198
|
*/
|
|
199
199
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
200
200
|
version: "v1";
|
|
201
|
-
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">):
|
|
201
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<StreamEvent>;
|
|
202
202
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
203
203
|
version: "v1";
|
|
204
204
|
encoding: "text/event-stream";
|
|
205
|
-
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">):
|
|
205
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<Uint8Array>;
|
|
206
206
|
_streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
207
207
|
version: "v1";
|
|
208
208
|
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
|
|
@@ -263,11 +263,11 @@ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends Ru
|
|
|
263
263
|
transform(generator: AsyncGenerator<RunInput>, options: Partial<CallOptions>): AsyncGenerator<RunOutput>;
|
|
264
264
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
265
265
|
version: "v1";
|
|
266
|
-
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">):
|
|
266
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<StreamEvent>;
|
|
267
267
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
268
268
|
version: "v1";
|
|
269
269
|
encoding: "text/event-stream";
|
|
270
|
-
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">):
|
|
270
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<Uint8Array>;
|
|
271
271
|
static isRunnableBinding(thing: any): thing is RunnableBinding<any, any, any>;
|
|
272
272
|
/**
|
|
273
273
|
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
package/dist/runnables/base.js
CHANGED
|
@@ -11,6 +11,7 @@ import { _RootEventFilter, isRunnableInterface } from "./utils.js";
|
|
|
11
11
|
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
12
12
|
import { Graph } from "./graph.js";
|
|
13
13
|
import { convertToHttpEventStream } from "./wrappers.js";
|
|
14
|
+
import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterator, } from "./iter.js";
|
|
14
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
16
|
export function _coerceToDict(value, defaultKey) {
|
|
16
17
|
return value &&
|
|
@@ -160,7 +161,11 @@ export class Runnable extends Serializable {
|
|
|
160
161
|
async stream(input, options) {
|
|
161
162
|
// Buffer the first streamed chunk to allow for initial errors
|
|
162
163
|
// to surface immediately.
|
|
163
|
-
const
|
|
164
|
+
const config = ensureConfig(options);
|
|
165
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
166
|
+
generator: this._streamIterator(input, config),
|
|
167
|
+
config,
|
|
168
|
+
});
|
|
164
169
|
await wrappedGenerator.setup;
|
|
165
170
|
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
166
171
|
}
|
|
@@ -442,13 +447,13 @@ export class Runnable extends Serializable {
|
|
|
442
447
|
await runnableStreamConsumePromise;
|
|
443
448
|
}
|
|
444
449
|
}
|
|
445
|
-
|
|
450
|
+
streamEvents(input, options, streamOptions) {
|
|
451
|
+
const stream = this._streamEvents(input, options, streamOptions);
|
|
446
452
|
if (options.encoding === "text/event-stream") {
|
|
447
|
-
|
|
448
|
-
yield* convertToHttpEventStream(stream);
|
|
453
|
+
return convertToHttpEventStream(stream);
|
|
449
454
|
}
|
|
450
455
|
else {
|
|
451
|
-
|
|
456
|
+
return IterableReadableStream.fromAsyncGenerator(stream);
|
|
452
457
|
}
|
|
453
458
|
}
|
|
454
459
|
async *_streamEvents(input, options, streamOptions) {
|
|
@@ -731,11 +736,16 @@ export class RunnableBinding extends Runnable {
|
|
|
731
736
|
generator, options) {
|
|
732
737
|
yield* this.bound.transform(generator, await this._mergeConfig(ensureConfig(options), this.kwargs));
|
|
733
738
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
+
streamEvents(input, options, streamOptions) {
|
|
740
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
741
|
+
const outerThis = this;
|
|
742
|
+
const generator = async function* () {
|
|
743
|
+
yield* outerThis.bound.streamEvents(input, {
|
|
744
|
+
...(await outerThis._mergeConfig(ensureConfig(options), outerThis.kwargs)),
|
|
745
|
+
version: options.version,
|
|
746
|
+
}, streamOptions);
|
|
747
|
+
};
|
|
748
|
+
return IterableReadableStream.fromAsyncGenerator(generator());
|
|
739
749
|
}
|
|
740
750
|
static isRunnableBinding(
|
|
741
751
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1279,7 +1289,11 @@ export class RunnableMap extends Runnable {
|
|
|
1279
1289
|
async function* generator() {
|
|
1280
1290
|
yield input;
|
|
1281
1291
|
}
|
|
1282
|
-
const
|
|
1292
|
+
const config = ensureConfig(options);
|
|
1293
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1294
|
+
generator: this.transform(generator(), config),
|
|
1295
|
+
config,
|
|
1296
|
+
});
|
|
1283
1297
|
await wrappedGenerator.setup;
|
|
1284
1298
|
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1285
1299
|
}
|
|
@@ -1333,6 +1347,44 @@ export class RunnableLambda extends Runnable {
|
|
|
1333
1347
|
recursionLimit: (childConfig.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1334
1348
|
});
|
|
1335
1349
|
}
|
|
1350
|
+
else if (isAsyncIterable(output)) {
|
|
1351
|
+
let finalOutput;
|
|
1352
|
+
for await (const chunk of consumeAsyncIterableInContext(childConfig, output)) {
|
|
1353
|
+
if (finalOutput === undefined) {
|
|
1354
|
+
finalOutput = chunk;
|
|
1355
|
+
}
|
|
1356
|
+
else {
|
|
1357
|
+
// Make a best effort to gather, for any type that supports concat.
|
|
1358
|
+
try {
|
|
1359
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1360
|
+
finalOutput = concat(finalOutput, chunk);
|
|
1361
|
+
}
|
|
1362
|
+
catch (e) {
|
|
1363
|
+
finalOutput = chunk;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
output = finalOutput;
|
|
1368
|
+
}
|
|
1369
|
+
else if (isIterator(output)) {
|
|
1370
|
+
let finalOutput;
|
|
1371
|
+
for (const chunk of consumeIteratorInContext(childConfig, output)) {
|
|
1372
|
+
if (finalOutput === undefined) {
|
|
1373
|
+
finalOutput = chunk;
|
|
1374
|
+
}
|
|
1375
|
+
else {
|
|
1376
|
+
// Make a best effort to gather, for any type that supports concat.
|
|
1377
|
+
try {
|
|
1378
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1379
|
+
finalOutput = concat(finalOutput, chunk);
|
|
1380
|
+
}
|
|
1381
|
+
catch (e) {
|
|
1382
|
+
finalOutput = chunk;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
output = finalOutput;
|
|
1387
|
+
}
|
|
1336
1388
|
resolve(output);
|
|
1337
1389
|
}
|
|
1338
1390
|
catch (e) {
|
|
@@ -1387,6 +1439,16 @@ export class RunnableLambda extends Runnable {
|
|
|
1387
1439
|
yield chunk;
|
|
1388
1440
|
}
|
|
1389
1441
|
}
|
|
1442
|
+
else if (isAsyncIterable(output)) {
|
|
1443
|
+
for await (const chunk of consumeAsyncIterableInContext(config, output)) {
|
|
1444
|
+
yield chunk;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
else if (isIterator(output)) {
|
|
1448
|
+
for (const chunk of consumeIteratorInContext(config, output)) {
|
|
1449
|
+
yield chunk;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1390
1452
|
else {
|
|
1391
1453
|
yield output;
|
|
1392
1454
|
}
|
|
@@ -1398,7 +1460,11 @@ export class RunnableLambda extends Runnable {
|
|
|
1398
1460
|
async function* generator() {
|
|
1399
1461
|
yield input;
|
|
1400
1462
|
}
|
|
1401
|
-
const
|
|
1463
|
+
const config = ensureConfig(options);
|
|
1464
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1465
|
+
generator: this.transform(generator(), config),
|
|
1466
|
+
config,
|
|
1467
|
+
});
|
|
1402
1468
|
await wrappedGenerator.setup;
|
|
1403
1469
|
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1404
1470
|
}
|
|
@@ -1598,7 +1664,11 @@ export class RunnableAssign extends Runnable {
|
|
|
1598
1664
|
async function* generator() {
|
|
1599
1665
|
yield input;
|
|
1600
1666
|
}
|
|
1601
|
-
const
|
|
1667
|
+
const config = ensureConfig(options);
|
|
1668
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1669
|
+
generator: this.transform(generator(), config),
|
|
1670
|
+
config,
|
|
1671
|
+
});
|
|
1602
1672
|
await wrappedGenerator.setup;
|
|
1603
1673
|
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1604
1674
|
}
|
|
@@ -1665,7 +1735,11 @@ export class RunnablePick extends Runnable {
|
|
|
1665
1735
|
async function* generator() {
|
|
1666
1736
|
yield input;
|
|
1667
1737
|
}
|
|
1668
|
-
const
|
|
1738
|
+
const config = ensureConfig(options);
|
|
1739
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup({
|
|
1740
|
+
generator: this.transform(generator(), config),
|
|
1741
|
+
config,
|
|
1742
|
+
});
|
|
1669
1743
|
await wrappedGenerator.setup;
|
|
1670
1744
|
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1671
1745
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.consumeAsyncIterableInContext = exports.consumeIteratorInContext = exports.isAsyncIterable = exports.isIterator = void 0;
|
|
4
|
+
const index_js_1 = require("../singletons/index.cjs");
|
|
5
|
+
function isIterator(thing) {
|
|
6
|
+
return (typeof thing === "object" &&
|
|
7
|
+
thing !== null &&
|
|
8
|
+
typeof thing[Symbol.iterator] === "function" &&
|
|
9
|
+
// avoid detecting array/set as iterator
|
|
10
|
+
typeof thing.next === "function");
|
|
11
|
+
}
|
|
12
|
+
exports.isIterator = isIterator;
|
|
13
|
+
function isAsyncIterable(thing) {
|
|
14
|
+
return (typeof thing === "object" &&
|
|
15
|
+
thing !== null &&
|
|
16
|
+
typeof thing[Symbol.asyncIterator] ===
|
|
17
|
+
"function");
|
|
18
|
+
}
|
|
19
|
+
exports.isAsyncIterable = isAsyncIterable;
|
|
20
|
+
function* consumeIteratorInContext(context, iter) {
|
|
21
|
+
const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
|
|
22
|
+
while (true) {
|
|
23
|
+
const { value, done } = storage.run(context, iter.next.bind(iter));
|
|
24
|
+
if (done) {
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
yield value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.consumeIteratorInContext = consumeIteratorInContext;
|
|
33
|
+
async function* consumeAsyncIterableInContext(context, iter) {
|
|
34
|
+
const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
|
|
35
|
+
const iterator = iter[Symbol.asyncIterator]();
|
|
36
|
+
while (true) {
|
|
37
|
+
const { value, done } = await storage.run(context, iterator.next.bind(iter));
|
|
38
|
+
if (done) {
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
yield value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.consumeAsyncIterableInContext = consumeAsyncIterableInContext;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { RunnableConfig } from "./config.js";
|
|
2
|
+
export declare function isIterator(thing: unknown): thing is IterableIterator<unknown>;
|
|
3
|
+
export declare function isAsyncIterable(thing: unknown): thing is AsyncIterable<unknown>;
|
|
4
|
+
export declare function consumeIteratorInContext<T>(context: Partial<RunnableConfig> | undefined, iter: IterableIterator<T>): IterableIterator<T>;
|
|
5
|
+
export declare function consumeAsyncIterableInContext<T>(context: Partial<RunnableConfig> | undefined, iter: AsyncIterable<T>): AsyncIterableIterator<T>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
2
|
+
export function isIterator(thing) {
|
|
3
|
+
return (typeof thing === "object" &&
|
|
4
|
+
thing !== null &&
|
|
5
|
+
typeof thing[Symbol.iterator] === "function" &&
|
|
6
|
+
// avoid detecting array/set as iterator
|
|
7
|
+
typeof thing.next === "function");
|
|
8
|
+
}
|
|
9
|
+
export function isAsyncIterable(thing) {
|
|
10
|
+
return (typeof thing === "object" &&
|
|
11
|
+
thing !== null &&
|
|
12
|
+
typeof thing[Symbol.asyncIterator] ===
|
|
13
|
+
"function");
|
|
14
|
+
}
|
|
15
|
+
export function* consumeIteratorInContext(context, iter) {
|
|
16
|
+
const storage = AsyncLocalStorageProviderSingleton.getInstance();
|
|
17
|
+
while (true) {
|
|
18
|
+
const { value, done } = storage.run(context, iter.next.bind(iter));
|
|
19
|
+
if (done) {
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
yield value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export async function* consumeAsyncIterableInContext(context, iter) {
|
|
28
|
+
const storage = AsyncLocalStorageProviderSingleton.getInstance();
|
|
29
|
+
const iterator = iter[Symbol.asyncIterator]();
|
|
30
|
+
while (true) {
|
|
31
|
+
const { value, done } = await storage.run(context, iterator.next.bind(iter));
|
|
32
|
+
if (done) {
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
yield value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -27,7 +27,7 @@ type RunnablePassthroughFunc<RunInput = any> = ((input: RunInput) => void) | ((i
|
|
|
27
27
|
* );
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
export declare class RunnablePassthrough<RunInput> extends Runnable<RunInput, RunInput> {
|
|
30
|
+
export declare class RunnablePassthrough<RunInput = any> extends Runnable<RunInput, RunInput> {
|
|
31
31
|
static lc_name(): string;
|
|
32
32
|
lc_namespace: string[];
|
|
33
33
|
lc_serializable: boolean;
|
|
@@ -405,58 +405,70 @@ class RemoteRunnable extends base_js_1.Runnable {
|
|
|
405
405
|
}
|
|
406
406
|
await runManager?.handleChainEnd(runLog?.state.final_output);
|
|
407
407
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
408
|
+
_streamEvents(input, options, streamOptions) {
|
|
409
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
410
|
+
const outerThis = this;
|
|
411
|
+
const generator = async function* () {
|
|
412
|
+
const [config, kwargs] = outerThis._separateRunnableConfigFromCallOptions(options);
|
|
413
|
+
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(options);
|
|
414
|
+
const runManager = await callbackManager_?.handleChainStart(outerThis.toJSON(), (0, base_js_1._coerceToDict)(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
415
|
+
// The type is in camelCase but the API only accepts snake_case.
|
|
416
|
+
const camelCaseStreamOptions = {
|
|
417
|
+
include_names: streamOptions?.includeNames,
|
|
418
|
+
include_types: streamOptions?.includeTypes,
|
|
419
|
+
include_tags: streamOptions?.includeTags,
|
|
420
|
+
exclude_names: streamOptions?.excludeNames,
|
|
421
|
+
exclude_types: streamOptions?.excludeTypes,
|
|
422
|
+
exclude_tags: streamOptions?.excludeTags,
|
|
423
|
+
};
|
|
424
|
+
const events = [];
|
|
425
|
+
try {
|
|
426
|
+
const response = await outerThis.post("/stream_events", {
|
|
427
|
+
input,
|
|
428
|
+
config: removeCallbacks(config),
|
|
429
|
+
kwargs,
|
|
430
|
+
...camelCaseStreamOptions,
|
|
431
|
+
diff: false,
|
|
432
|
+
});
|
|
433
|
+
const { body, ok } = response;
|
|
434
|
+
if (!ok) {
|
|
435
|
+
throw new Error(`${response.status} Error: ${await response.text()}`);
|
|
436
|
+
}
|
|
437
|
+
if (!body) {
|
|
438
|
+
throw new Error("Could not begin remote stream events. Please check the given URL and try again.");
|
|
439
|
+
}
|
|
440
|
+
const runnableStream = (0, event_source_parse_js_1.convertEventStreamToIterableReadableDataStream)(body);
|
|
441
|
+
for await (const log of runnableStream) {
|
|
442
|
+
const chunk = revive(JSON.parse(log));
|
|
443
|
+
const event = {
|
|
444
|
+
event: chunk.event,
|
|
445
|
+
name: chunk.name,
|
|
446
|
+
run_id: chunk.run_id,
|
|
447
|
+
tags: chunk.tags,
|
|
448
|
+
metadata: chunk.metadata,
|
|
449
|
+
data: chunk.data,
|
|
450
|
+
};
|
|
451
|
+
yield event;
|
|
452
|
+
events.push(event);
|
|
453
|
+
}
|
|
436
454
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
const runnableStream = (0, event_source_parse_js_1.convertEventStreamToIterableReadableDataStream)(body);
|
|
441
|
-
for await (const log of runnableStream) {
|
|
442
|
-
const chunk = revive(JSON.parse(log));
|
|
443
|
-
const event = {
|
|
444
|
-
event: chunk.event,
|
|
445
|
-
name: chunk.name,
|
|
446
|
-
run_id: chunk.run_id,
|
|
447
|
-
tags: chunk.tags,
|
|
448
|
-
metadata: chunk.metadata,
|
|
449
|
-
data: chunk.data,
|
|
450
|
-
};
|
|
451
|
-
yield event;
|
|
452
|
-
events.push(event);
|
|
455
|
+
catch (err) {
|
|
456
|
+
await runManager?.handleChainError(err);
|
|
457
|
+
throw err;
|
|
453
458
|
}
|
|
459
|
+
await runManager?.handleChainEnd(events);
|
|
460
|
+
};
|
|
461
|
+
return generator();
|
|
462
|
+
}
|
|
463
|
+
streamEvents(input, options, streamOptions) {
|
|
464
|
+
if (options?.version !== "v1") {
|
|
465
|
+
throw new Error(`Only version "v1" of the events schema is currently supported.`);
|
|
454
466
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
throw err;
|
|
467
|
+
if (options.encoding !== undefined) {
|
|
468
|
+
throw new Error("Special encodings are not supported for this runnable.");
|
|
458
469
|
}
|
|
459
|
-
|
|
470
|
+
const eventStream = this._streamEvents(input, options, streamOptions);
|
|
471
|
+
return stream_js_1.IterableReadableStream.fromAsyncGenerator(eventStream);
|
|
460
472
|
}
|
|
461
473
|
}
|
|
462
474
|
exports.RemoteRunnable = RemoteRunnable;
|
|
@@ -2,6 +2,7 @@ import { Runnable, RunnableBatchOptions } from "./base.js";
|
|
|
2
2
|
import { type RunnableConfig } from "./config.js";
|
|
3
3
|
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
4
4
|
import { RunLogPatch, type LogStreamCallbackHandlerInput, type StreamEvent } from "../tracers/log_stream.js";
|
|
5
|
+
import { IterableReadableStream } from "../utils/stream.js";
|
|
5
6
|
type RemoteRunnableOptions = {
|
|
6
7
|
timeout?: number;
|
|
7
8
|
headers?: Record<string, unknown>;
|
|
@@ -27,12 +28,15 @@ export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends Run
|
|
|
27
28
|
batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
|
|
28
29
|
_streamIterator(input: RunInput, options?: Partial<CallOptions>): AsyncGenerator<RunOutput>;
|
|
29
30
|
streamLog(input: RunInput, options?: Partial<CallOptions>, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<RunLogPatch>;
|
|
31
|
+
_streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
32
|
+
version: "v1";
|
|
33
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose"> | undefined): AsyncGenerator<StreamEvent>;
|
|
30
34
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
31
35
|
version: "v1";
|
|
32
|
-
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">):
|
|
36
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<StreamEvent>;
|
|
33
37
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
34
38
|
version: "v1";
|
|
35
39
|
encoding: "text/event-stream";
|
|
36
|
-
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">):
|
|
40
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<Uint8Array>;
|
|
37
41
|
}
|
|
38
42
|
export {};
|