@langchain/core 0.1.61 → 0.1.63
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/language_models/base.cjs +3 -0
- package/dist/language_models/base.js +3 -0
- package/dist/runnables/base.cjs +59 -39
- package/dist/runnables/base.d.ts +11 -0
- package/dist/runnables/base.js +59 -39
- 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/remote.d.ts +4 -0
- package/dist/runnables/wrappers.cjs +18 -0
- package/dist/runnables/wrappers.d.ts +2 -0
- package/dist/runnables/wrappers.js +14 -0
- package/dist/singletons/index.cjs +1 -1
- package/dist/singletons/index.d.ts +2 -2
- package/dist/singletons/index.js +1 -1
- package/dist/tracers/base.cjs +4 -4
- package/dist/tracers/base.js +4 -4
- package/package.json +2 -2
|
@@ -21,6 +21,9 @@ const getModelNameForTiktoken = (modelName) => {
|
|
|
21
21
|
if (modelName.startsWith("gpt-4-")) {
|
|
22
22
|
return "gpt-4";
|
|
23
23
|
}
|
|
24
|
+
if (modelName.startsWith("gpt-4o")) {
|
|
25
|
+
return "gpt-4o";
|
|
26
|
+
}
|
|
24
27
|
return modelName;
|
|
25
28
|
};
|
|
26
29
|
exports.getModelNameForTiktoken = getModelNameForTiktoken;
|
|
@@ -18,6 +18,9 @@ export const getModelNameForTiktoken = (modelName) => {
|
|
|
18
18
|
if (modelName.startsWith("gpt-4-")) {
|
|
19
19
|
return "gpt-4";
|
|
20
20
|
}
|
|
21
|
+
if (modelName.startsWith("gpt-4o")) {
|
|
22
|
+
return "gpt-4o";
|
|
23
|
+
}
|
|
21
24
|
return modelName;
|
|
22
25
|
};
|
|
23
26
|
export const getEmbeddingContextSize = (modelName) => {
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -16,6 +16,8 @@ const root_listener_js_1 = require("../tracers/root_listener.cjs");
|
|
|
16
16
|
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
|
+
const wrappers_js_1 = require("./wrappers.cjs");
|
|
20
|
+
const iter_js_1 = require("./iter.cjs");
|
|
19
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
22
|
function _coerceToDict(value, defaultKey) {
|
|
21
23
|
return value &&
|
|
@@ -448,46 +450,16 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
448
450
|
await runnableStreamConsumePromise;
|
|
449
451
|
}
|
|
450
452
|
}
|
|
451
|
-
/**
|
|
452
|
-
* Generate a stream of events emitted by the internal steps of the runnable.
|
|
453
|
-
*
|
|
454
|
-
* Use to create an iterator over StreamEvents that provide real-time information
|
|
455
|
-
* about the progress of the runnable, including StreamEvents from intermediate
|
|
456
|
-
* results.
|
|
457
|
-
*
|
|
458
|
-
* A StreamEvent is a dictionary with the following schema:
|
|
459
|
-
*
|
|
460
|
-
* - `event`: string - Event names are of the format: on_[runnable_type]_(start|stream|end).
|
|
461
|
-
* - `name`: string - The name of the runnable that generated the event.
|
|
462
|
-
* - `run_id`: string - Randomly generated ID associated with the given execution of
|
|
463
|
-
* the runnable that emitted the event. A child runnable that gets invoked as part of the execution of a
|
|
464
|
-
* parent runnable is assigned its own unique ID.
|
|
465
|
-
* - `tags`: string[] - The tags of the runnable that generated the event.
|
|
466
|
-
* - `metadata`: Record<string, any> - The metadata of the runnable that generated the event.
|
|
467
|
-
* - `data`: Record<string, any>
|
|
468
|
-
*
|
|
469
|
-
* Below is a table that illustrates some events that might be emitted by various
|
|
470
|
-
* chains. Metadata fields have been omitted from the table for brevity.
|
|
471
|
-
* Chain definitions have been included after the table.
|
|
472
|
-
*
|
|
473
|
-
* | event | name | chunk | input | output |
|
|
474
|
-
* |----------------------|------------------|------------------------------------|-----------------------------------------------|-------------------------------------------------|
|
|
475
|
-
* | on_llm_start | [model name] | | {'input': 'hello'} | |
|
|
476
|
-
* | on_llm_stream | [model name] | 'Hello' OR AIMessageChunk("hello") | | |
|
|
477
|
-
* | on_llm_end | [model name] | | 'Hello human!' |
|
|
478
|
-
* | on_chain_start | format_docs | | | |
|
|
479
|
-
* | on_chain_stream | format_docs | "hello world!, goodbye world!" | | |
|
|
480
|
-
* | on_chain_end | format_docs | | [Document(...)] | "hello world!, goodbye world!" |
|
|
481
|
-
* | on_tool_start | some_tool | | {"x": 1, "y": "2"} | |
|
|
482
|
-
* | on_tool_stream | some_tool | {"x": 1, "y": "2"} | | |
|
|
483
|
-
* | on_tool_end | some_tool | | | {"x": 1, "y": "2"} |
|
|
484
|
-
* | on_retriever_start | [retriever name] | | {"query": "hello"} | |
|
|
485
|
-
* | on_retriever_chunk | [retriever name] | {documents: [...]} | | |
|
|
486
|
-
* | on_retriever_end | [retriever name] | | {"query": "hello"} | {documents: [...]} |
|
|
487
|
-
* | on_prompt_start | [template_name] | | {"question": "hello"} | |
|
|
488
|
-
* | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) |
|
|
489
|
-
*/
|
|
490
453
|
async *streamEvents(input, options, streamOptions) {
|
|
454
|
+
if (options.encoding === "text/event-stream") {
|
|
455
|
+
const stream = await this._streamEvents(input, options, streamOptions);
|
|
456
|
+
yield* (0, wrappers_js_1.convertToHttpEventStream)(stream);
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
yield* this._streamEvents(input, options, streamOptions);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
async *_streamEvents(input, options, streamOptions) {
|
|
491
463
|
if (options.version !== "v1") {
|
|
492
464
|
throw new Error(`Only version "v1" of the events schema is currently supported.`);
|
|
493
465
|
}
|
|
@@ -1375,6 +1347,44 @@ class RunnableLambda extends Runnable {
|
|
|
1375
1347
|
recursionLimit: (childConfig.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
|
|
1376
1348
|
});
|
|
1377
1349
|
}
|
|
1350
|
+
else if ((0, iter_js_1.isAsyncIterable)(output)) {
|
|
1351
|
+
let finalOutput;
|
|
1352
|
+
for await (const chunk of (0, iter_js_1.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 = (0, stream_js_1.concat)(finalOutput, chunk);
|
|
1361
|
+
}
|
|
1362
|
+
catch (e) {
|
|
1363
|
+
finalOutput = chunk;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
output = finalOutput;
|
|
1368
|
+
}
|
|
1369
|
+
else if ((0, iter_js_1.isIterator)(output)) {
|
|
1370
|
+
let finalOutput;
|
|
1371
|
+
for (const chunk of (0, iter_js_1.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 = (0, stream_js_1.concat)(finalOutput, chunk);
|
|
1380
|
+
}
|
|
1381
|
+
catch (e) {
|
|
1382
|
+
finalOutput = chunk;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
output = finalOutput;
|
|
1387
|
+
}
|
|
1378
1388
|
resolve(output);
|
|
1379
1389
|
}
|
|
1380
1390
|
catch (e) {
|
|
@@ -1429,6 +1439,16 @@ class RunnableLambda extends Runnable {
|
|
|
1429
1439
|
yield chunk;
|
|
1430
1440
|
}
|
|
1431
1441
|
}
|
|
1442
|
+
else if ((0, iter_js_1.isAsyncIterable)(output)) {
|
|
1443
|
+
for await (const chunk of (0, iter_js_1.consumeAsyncIterableInContext)(config, output)) {
|
|
1444
|
+
yield chunk;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
else if ((0, iter_js_1.isIterator)(output)) {
|
|
1448
|
+
for (const chunk of (0, iter_js_1.consumeIteratorInContext)(config, output)) {
|
|
1449
|
+
yield chunk;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1432
1452
|
else {
|
|
1433
1453
|
yield output;
|
|
1434
1454
|
}
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -199,6 +199,13 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
199
199
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
200
200
|
version: "v1";
|
|
201
201
|
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
|
|
202
|
+
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
203
|
+
version: "v1";
|
|
204
|
+
encoding: "text/event-stream";
|
|
205
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<Uint8Array>;
|
|
206
|
+
_streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
207
|
+
version: "v1";
|
|
208
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
|
|
202
209
|
static isRunnable(thing: any): thing is Runnable;
|
|
203
210
|
/**
|
|
204
211
|
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
|
@@ -257,6 +264,10 @@ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends Ru
|
|
|
257
264
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
258
265
|
version: "v1";
|
|
259
266
|
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
|
|
267
|
+
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
268
|
+
version: "v1";
|
|
269
|
+
encoding: "text/event-stream";
|
|
270
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<Uint8Array>;
|
|
260
271
|
static isRunnableBinding(thing: any): thing is RunnableBinding<any, any, any>;
|
|
261
272
|
/**
|
|
262
273
|
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
package/dist/runnables/base.js
CHANGED
|
@@ -10,6 +10,8 @@ import { RootListenersTracer } from "../tracers/root_listener.js";
|
|
|
10
10
|
import { _RootEventFilter, isRunnableInterface } from "./utils.js";
|
|
11
11
|
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
12
12
|
import { Graph } from "./graph.js";
|
|
13
|
+
import { convertToHttpEventStream } from "./wrappers.js";
|
|
14
|
+
import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterator, } from "./iter.js";
|
|
13
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
16
|
export function _coerceToDict(value, defaultKey) {
|
|
15
17
|
return value &&
|
|
@@ -441,46 +443,16 @@ export class Runnable extends Serializable {
|
|
|
441
443
|
await runnableStreamConsumePromise;
|
|
442
444
|
}
|
|
443
445
|
}
|
|
444
|
-
/**
|
|
445
|
-
* Generate a stream of events emitted by the internal steps of the runnable.
|
|
446
|
-
*
|
|
447
|
-
* Use to create an iterator over StreamEvents that provide real-time information
|
|
448
|
-
* about the progress of the runnable, including StreamEvents from intermediate
|
|
449
|
-
* results.
|
|
450
|
-
*
|
|
451
|
-
* A StreamEvent is a dictionary with the following schema:
|
|
452
|
-
*
|
|
453
|
-
* - `event`: string - Event names are of the format: on_[runnable_type]_(start|stream|end).
|
|
454
|
-
* - `name`: string - The name of the runnable that generated the event.
|
|
455
|
-
* - `run_id`: string - Randomly generated ID associated with the given execution of
|
|
456
|
-
* the runnable that emitted the event. A child runnable that gets invoked as part of the execution of a
|
|
457
|
-
* parent runnable is assigned its own unique ID.
|
|
458
|
-
* - `tags`: string[] - The tags of the runnable that generated the event.
|
|
459
|
-
* - `metadata`: Record<string, any> - The metadata of the runnable that generated the event.
|
|
460
|
-
* - `data`: Record<string, any>
|
|
461
|
-
*
|
|
462
|
-
* Below is a table that illustrates some events that might be emitted by various
|
|
463
|
-
* chains. Metadata fields have been omitted from the table for brevity.
|
|
464
|
-
* Chain definitions have been included after the table.
|
|
465
|
-
*
|
|
466
|
-
* | event | name | chunk | input | output |
|
|
467
|
-
* |----------------------|------------------|------------------------------------|-----------------------------------------------|-------------------------------------------------|
|
|
468
|
-
* | on_llm_start | [model name] | | {'input': 'hello'} | |
|
|
469
|
-
* | on_llm_stream | [model name] | 'Hello' OR AIMessageChunk("hello") | | |
|
|
470
|
-
* | on_llm_end | [model name] | | 'Hello human!' |
|
|
471
|
-
* | on_chain_start | format_docs | | | |
|
|
472
|
-
* | on_chain_stream | format_docs | "hello world!, goodbye world!" | | |
|
|
473
|
-
* | on_chain_end | format_docs | | [Document(...)] | "hello world!, goodbye world!" |
|
|
474
|
-
* | on_tool_start | some_tool | | {"x": 1, "y": "2"} | |
|
|
475
|
-
* | on_tool_stream | some_tool | {"x": 1, "y": "2"} | | |
|
|
476
|
-
* | on_tool_end | some_tool | | | {"x": 1, "y": "2"} |
|
|
477
|
-
* | on_retriever_start | [retriever name] | | {"query": "hello"} | |
|
|
478
|
-
* | on_retriever_chunk | [retriever name] | {documents: [...]} | | |
|
|
479
|
-
* | on_retriever_end | [retriever name] | | {"query": "hello"} | {documents: [...]} |
|
|
480
|
-
* | on_prompt_start | [template_name] | | {"question": "hello"} | |
|
|
481
|
-
* | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) |
|
|
482
|
-
*/
|
|
483
446
|
async *streamEvents(input, options, streamOptions) {
|
|
447
|
+
if (options.encoding === "text/event-stream") {
|
|
448
|
+
const stream = await this._streamEvents(input, options, streamOptions);
|
|
449
|
+
yield* convertToHttpEventStream(stream);
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
yield* this._streamEvents(input, options, streamOptions);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
async *_streamEvents(input, options, streamOptions) {
|
|
484
456
|
if (options.version !== "v1") {
|
|
485
457
|
throw new Error(`Only version "v1" of the events schema is currently supported.`);
|
|
486
458
|
}
|
|
@@ -1362,6 +1334,44 @@ export class RunnableLambda extends Runnable {
|
|
|
1362
1334
|
recursionLimit: (childConfig.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1363
1335
|
});
|
|
1364
1336
|
}
|
|
1337
|
+
else if (isAsyncIterable(output)) {
|
|
1338
|
+
let finalOutput;
|
|
1339
|
+
for await (const chunk of consumeAsyncIterableInContext(childConfig, output)) {
|
|
1340
|
+
if (finalOutput === undefined) {
|
|
1341
|
+
finalOutput = chunk;
|
|
1342
|
+
}
|
|
1343
|
+
else {
|
|
1344
|
+
// Make a best effort to gather, for any type that supports concat.
|
|
1345
|
+
try {
|
|
1346
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1347
|
+
finalOutput = concat(finalOutput, chunk);
|
|
1348
|
+
}
|
|
1349
|
+
catch (e) {
|
|
1350
|
+
finalOutput = chunk;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
output = finalOutput;
|
|
1355
|
+
}
|
|
1356
|
+
else if (isIterator(output)) {
|
|
1357
|
+
let finalOutput;
|
|
1358
|
+
for (const chunk of consumeIteratorInContext(childConfig, output)) {
|
|
1359
|
+
if (finalOutput === undefined) {
|
|
1360
|
+
finalOutput = chunk;
|
|
1361
|
+
}
|
|
1362
|
+
else {
|
|
1363
|
+
// Make a best effort to gather, for any type that supports concat.
|
|
1364
|
+
try {
|
|
1365
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1366
|
+
finalOutput = concat(finalOutput, chunk);
|
|
1367
|
+
}
|
|
1368
|
+
catch (e) {
|
|
1369
|
+
finalOutput = chunk;
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
output = finalOutput;
|
|
1374
|
+
}
|
|
1365
1375
|
resolve(output);
|
|
1366
1376
|
}
|
|
1367
1377
|
catch (e) {
|
|
@@ -1416,6 +1426,16 @@ export class RunnableLambda extends Runnable {
|
|
|
1416
1426
|
yield chunk;
|
|
1417
1427
|
}
|
|
1418
1428
|
}
|
|
1429
|
+
else if (isAsyncIterable(output)) {
|
|
1430
|
+
for await (const chunk of consumeAsyncIterableInContext(config, output)) {
|
|
1431
|
+
yield chunk;
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
else if (isIterator(output)) {
|
|
1435
|
+
for (const chunk of consumeIteratorInContext(config, output)) {
|
|
1436
|
+
yield chunk;
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1419
1439
|
else {
|
|
1420
1440
|
yield output;
|
|
1421
1441
|
}
|
|
@@ -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
|
+
}
|
|
@@ -30,5 +30,9 @@ export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends Run
|
|
|
30
30
|
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
31
31
|
version: "v1";
|
|
32
32
|
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
|
|
33
|
+
streamEvents(input: RunInput, options: Partial<CallOptions> & {
|
|
34
|
+
version: "v1";
|
|
35
|
+
encoding: "text/event-stream";
|
|
36
|
+
}, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<Uint8Array>;
|
|
33
37
|
}
|
|
34
38
|
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertToHttpEventStream = void 0;
|
|
4
|
+
const stream_js_1 = require("../utils/stream.cjs");
|
|
5
|
+
function convertToHttpEventStream(stream) {
|
|
6
|
+
const encoder = new TextEncoder();
|
|
7
|
+
const finalStream = new ReadableStream({
|
|
8
|
+
async start(controller) {
|
|
9
|
+
for await (const chunk of stream) {
|
|
10
|
+
controller.enqueue(encoder.encode(`event: data\ndata: ${JSON.stringify(chunk)}\n\n`));
|
|
11
|
+
}
|
|
12
|
+
controller.enqueue(encoder.encode("event: end\n\n"));
|
|
13
|
+
controller.close();
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
return stream_js_1.IterableReadableStream.fromReadableStream(finalStream);
|
|
17
|
+
}
|
|
18
|
+
exports.convertToHttpEventStream = convertToHttpEventStream;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IterableReadableStream } from "../utils/stream.js";
|
|
2
|
+
export function convertToHttpEventStream(stream) {
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
const finalStream = new ReadableStream({
|
|
5
|
+
async start(controller) {
|
|
6
|
+
for await (const chunk of stream) {
|
|
7
|
+
controller.enqueue(encoder.encode(`event: data\ndata: ${JSON.stringify(chunk)}\n\n`));
|
|
8
|
+
}
|
|
9
|
+
controller.enqueue(encoder.encode("event: end\n\n"));
|
|
10
|
+
controller.close();
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
return IterableReadableStream.fromReadableStream(finalStream);
|
|
14
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export interface AsyncLocalStorageInterface {
|
|
2
2
|
getStore: () => any | undefined;
|
|
3
|
-
run: (store: any, callback: () =>
|
|
3
|
+
run: <T>(store: any, callback: () => T) => T;
|
|
4
4
|
}
|
|
5
5
|
export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface {
|
|
6
6
|
getStore(): any;
|
|
7
|
-
run(_store: any, callback: () =>
|
|
7
|
+
run<T>(_store: any, callback: () => T): T;
|
|
8
8
|
}
|
|
9
9
|
declare class AsyncLocalStorageProvider {
|
|
10
10
|
private asyncLocalStorage;
|
package/dist/singletons/index.js
CHANGED
package/dist/tracers/base.cjs
CHANGED
|
@@ -11,9 +11,9 @@ function _coerceToDict(value, defaultKey) {
|
|
|
11
11
|
function stripNonAlphanumeric(input) {
|
|
12
12
|
return input.replace(/[-:.]/g, "");
|
|
13
13
|
}
|
|
14
|
-
function convertToDottedOrderFormat(epoch, runId) {
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
function convertToDottedOrderFormat(epoch, runId, executionOrder) {
|
|
15
|
+
const paddedOrder = executionOrder.toFixed(0).slice(0, 3).padStart(3, "0");
|
|
16
|
+
return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}${paddedOrder}Z`) + runId);
|
|
17
17
|
}
|
|
18
18
|
class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
19
19
|
constructor(_fields) {
|
|
@@ -42,7 +42,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
42
42
|
parentRun.child_runs.push(childRun);
|
|
43
43
|
}
|
|
44
44
|
async _startTrace(run) {
|
|
45
|
-
const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id);
|
|
45
|
+
const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id, run.execution_order);
|
|
46
46
|
const storedRun = { ...run };
|
|
47
47
|
if (storedRun.parent_run_id !== undefined) {
|
|
48
48
|
const parentRun = this.runMap.get(storedRun.parent_run_id);
|
package/dist/tracers/base.js
CHANGED
|
@@ -8,9 +8,9 @@ function _coerceToDict(value, defaultKey) {
|
|
|
8
8
|
function stripNonAlphanumeric(input) {
|
|
9
9
|
return input.replace(/[-:.]/g, "");
|
|
10
10
|
}
|
|
11
|
-
function convertToDottedOrderFormat(epoch, runId) {
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
function convertToDottedOrderFormat(epoch, runId, executionOrder) {
|
|
12
|
+
const paddedOrder = executionOrder.toFixed(0).slice(0, 3).padStart(3, "0");
|
|
13
|
+
return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}${paddedOrder}Z`) + runId);
|
|
14
14
|
}
|
|
15
15
|
export class BaseTracer extends BaseCallbackHandler {
|
|
16
16
|
constructor(_fields) {
|
|
@@ -39,7 +39,7 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
39
39
|
parentRun.child_runs.push(childRun);
|
|
40
40
|
}
|
|
41
41
|
async _startTrace(run) {
|
|
42
|
-
const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id);
|
|
42
|
+
const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id, run.execution_order);
|
|
43
43
|
const storedRun = { ...run };
|
|
44
44
|
if (storedRun.parent_run_id !== undefined) {
|
|
45
45
|
const parentRun = this.runMap.get(storedRun.parent_run_id);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.63",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"ansi-styles": "^5.0.0",
|
|
44
44
|
"camelcase": "6",
|
|
45
45
|
"decamelize": "1.2.0",
|
|
46
|
-
"js-tiktoken": "^1.0.
|
|
46
|
+
"js-tiktoken": "^1.0.12",
|
|
47
47
|
"langsmith": "~0.1.7",
|
|
48
48
|
"ml-distance": "^4.0.0",
|
|
49
49
|
"mustache": "^4.2.0",
|