@langchain/core 0.2.3 → 0.2.4
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/README.md +1 -1
- package/dist/callbacks/manager.cjs +6 -1
- package/dist/callbacks/manager.d.ts +1 -1
- package/dist/callbacks/manager.js +6 -1
- package/dist/runnables/base.cjs +72 -3
- package/dist/runnables/base.d.ts +18 -1
- package/dist/runnables/base.js +71 -3
- package/dist/runnables/iter.cjs +7 -2
- package/dist/runnables/iter.d.ts +2 -1
- package/dist/runnables/iter.js +5 -1
- package/dist/tracers/tracer_langchain.cjs +41 -0
- package/dist/tracers/tracer_langchain.d.ts +2 -0
- package/dist/tracers/tracer_langchain.js +41 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -83,7 +83,7 @@ Streaming (and streaming of intermediate steps) is needed to show the user that
|
|
|
83
83
|
Async interfaces are nice when moving into production.
|
|
84
84
|
Rather than having to write multiple implementations for all of those, LCEL allows you to write a runnable once and invoke it in many different ways.
|
|
85
85
|
|
|
86
|
-
For more check out the [LCEL docs](https://js.langchain.com/docs/
|
|
86
|
+
For more check out the [LCEL docs](https://js.langchain.com/v0.2/docs/concepts#langchain-expression-language).
|
|
87
87
|
|
|
88
88
|

|
|
89
89
|
|
|
@@ -617,7 +617,12 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
617
617
|
if (tracingEnabled &&
|
|
618
618
|
!callbackManager.handlers.some((handler) => handler.name === "langchain_tracer")) {
|
|
619
619
|
if (tracingV2Enabled) {
|
|
620
|
-
|
|
620
|
+
const tracerV2 = await (0, initialize_js_1.getTracingV2CallbackHandler)();
|
|
621
|
+
callbackManager.addHandler(tracerV2, true);
|
|
622
|
+
// handoff between langchain and langsmith/traceable
|
|
623
|
+
// override the parent run ID
|
|
624
|
+
callbackManager._parentRunId =
|
|
625
|
+
tracerV2.getTraceableRunTree()?.id ?? callbackManager._parentRunId;
|
|
621
626
|
}
|
|
622
627
|
}
|
|
623
628
|
}
|
|
@@ -120,7 +120,7 @@ export declare class CallbackManager extends BaseCallbackManager implements Base
|
|
|
120
120
|
metadata: Record<string, unknown>;
|
|
121
121
|
inheritableMetadata: Record<string, unknown>;
|
|
122
122
|
name: string;
|
|
123
|
-
|
|
123
|
+
_parentRunId?: string;
|
|
124
124
|
constructor(parentRunId?: string, options?: {
|
|
125
125
|
handlers?: BaseCallbackHandler[];
|
|
126
126
|
inheritableHandlers?: BaseCallbackHandler[];
|
|
@@ -608,7 +608,12 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
608
608
|
if (tracingEnabled &&
|
|
609
609
|
!callbackManager.handlers.some((handler) => handler.name === "langchain_tracer")) {
|
|
610
610
|
if (tracingV2Enabled) {
|
|
611
|
-
|
|
611
|
+
const tracerV2 = await getTracingV2CallbackHandler();
|
|
612
|
+
callbackManager.addHandler(tracerV2, true);
|
|
613
|
+
// handoff between langchain and langsmith/traceable
|
|
614
|
+
// override the parent run ID
|
|
615
|
+
callbackManager._parentRunId =
|
|
616
|
+
tracerV2.getTraceableRunTree()?.id ?? callbackManager._parentRunId;
|
|
612
617
|
}
|
|
613
618
|
}
|
|
614
619
|
}
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -3,10 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = exports._coerceToDict = void 0;
|
|
6
|
+
exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableTraceable = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = exports._coerceToDict = void 0;
|
|
7
7
|
const zod_1 = require("zod");
|
|
8
8
|
const p_retry_1 = __importDefault(require("p-retry"));
|
|
9
9
|
const uuid_1 = require("uuid");
|
|
10
|
+
const traceable_1 = require("langsmith/singletons/traceable");
|
|
10
11
|
const manager_js_1 = require("../callbacks/manager.cjs");
|
|
11
12
|
const log_stream_js_1 = require("../tracers/log_stream.cjs");
|
|
12
13
|
const event_stream_js_1 = require("../tracers/event_stream.cjs");
|
|
@@ -1385,6 +1386,69 @@ class RunnableMap extends Runnable {
|
|
|
1385
1386
|
}
|
|
1386
1387
|
}
|
|
1387
1388
|
exports.RunnableMap = RunnableMap;
|
|
1389
|
+
/**
|
|
1390
|
+
* A runnable that wraps a traced LangSmith function.
|
|
1391
|
+
*/
|
|
1392
|
+
class RunnableTraceable extends Runnable {
|
|
1393
|
+
constructor(fields) {
|
|
1394
|
+
super(fields);
|
|
1395
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
1396
|
+
enumerable: true,
|
|
1397
|
+
configurable: true,
|
|
1398
|
+
writable: true,
|
|
1399
|
+
value: false
|
|
1400
|
+
});
|
|
1401
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
1402
|
+
enumerable: true,
|
|
1403
|
+
configurable: true,
|
|
1404
|
+
writable: true,
|
|
1405
|
+
value: ["langchain_core", "runnables"]
|
|
1406
|
+
});
|
|
1407
|
+
Object.defineProperty(this, "func", {
|
|
1408
|
+
enumerable: true,
|
|
1409
|
+
configurable: true,
|
|
1410
|
+
writable: true,
|
|
1411
|
+
value: void 0
|
|
1412
|
+
});
|
|
1413
|
+
if (!(0, traceable_1.isTraceableFunction)(fields.func)) {
|
|
1414
|
+
throw new Error("RunnableTraceable requires a function that is wrapped in traceable higher-order function");
|
|
1415
|
+
}
|
|
1416
|
+
this.func = fields.func;
|
|
1417
|
+
}
|
|
1418
|
+
async invoke(input, options) {
|
|
1419
|
+
const [config] = this._getOptionsList(options ?? {}, 1);
|
|
1420
|
+
const callbacks = await (0, config_js_1.getCallbackManagerForConfig)(config);
|
|
1421
|
+
return (await this.func((0, config_js_1.patchConfig)(config, { callbacks }), input));
|
|
1422
|
+
}
|
|
1423
|
+
async *_streamIterator(input, options) {
|
|
1424
|
+
const result = await this.invoke(input, options);
|
|
1425
|
+
if ((0, iter_js_1.isAsyncIterable)(result)) {
|
|
1426
|
+
for await (const item of result) {
|
|
1427
|
+
yield item;
|
|
1428
|
+
}
|
|
1429
|
+
return;
|
|
1430
|
+
}
|
|
1431
|
+
if ((0, iter_js_1.isIterator)(result)) {
|
|
1432
|
+
while (true) {
|
|
1433
|
+
const state = result.next();
|
|
1434
|
+
if (state.done)
|
|
1435
|
+
break;
|
|
1436
|
+
yield state.value;
|
|
1437
|
+
}
|
|
1438
|
+
return;
|
|
1439
|
+
}
|
|
1440
|
+
yield result;
|
|
1441
|
+
}
|
|
1442
|
+
static from(func) {
|
|
1443
|
+
return new RunnableTraceable({ func });
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
exports.RunnableTraceable = RunnableTraceable;
|
|
1447
|
+
function assertNonTraceableFunction(func) {
|
|
1448
|
+
if ((0, traceable_1.isTraceableFunction)(func)) {
|
|
1449
|
+
throw new Error("RunnableLambda requires a function that is not wrapped in traceable higher-order function. This shouldn't happen.");
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1388
1452
|
/**
|
|
1389
1453
|
* A runnable that runs a callable.
|
|
1390
1454
|
*/
|
|
@@ -1393,6 +1457,10 @@ class RunnableLambda extends Runnable {
|
|
|
1393
1457
|
return "RunnableLambda";
|
|
1394
1458
|
}
|
|
1395
1459
|
constructor(fields) {
|
|
1460
|
+
if ((0, traceable_1.isTraceableFunction)(fields.func)) {
|
|
1461
|
+
// eslint-disable-next-line no-constructor-return
|
|
1462
|
+
return RunnableTraceable.from(fields.func);
|
|
1463
|
+
}
|
|
1396
1464
|
super(fields);
|
|
1397
1465
|
Object.defineProperty(this, "lc_namespace", {
|
|
1398
1466
|
enumerable: true,
|
|
@@ -1406,6 +1474,7 @@ class RunnableLambda extends Runnable {
|
|
|
1406
1474
|
writable: true,
|
|
1407
1475
|
value: void 0
|
|
1408
1476
|
});
|
|
1477
|
+
assertNonTraceableFunction(fields.func);
|
|
1409
1478
|
this.func = fields.func;
|
|
1410
1479
|
}
|
|
1411
1480
|
static from(func) {
|
|
@@ -1453,7 +1522,7 @@ class RunnableLambda extends Runnable {
|
|
|
1453
1522
|
}
|
|
1454
1523
|
output = finalOutput;
|
|
1455
1524
|
}
|
|
1456
|
-
else if ((0, iter_js_1.
|
|
1525
|
+
else if ((0, iter_js_1.isIterableIterator)(output)) {
|
|
1457
1526
|
let finalOutput;
|
|
1458
1527
|
for (const chunk of (0, iter_js_1.consumeIteratorInContext)(childConfig, output)) {
|
|
1459
1528
|
if (finalOutput === undefined) {
|
|
@@ -1531,7 +1600,7 @@ class RunnableLambda extends Runnable {
|
|
|
1531
1600
|
yield chunk;
|
|
1532
1601
|
}
|
|
1533
1602
|
}
|
|
1534
|
-
else if ((0, iter_js_1.
|
|
1603
|
+
else if ((0, iter_js_1.isIterableIterator)(output)) {
|
|
1535
1604
|
for (const chunk of (0, iter_js_1.consumeIteratorInContext)(config, output)) {
|
|
1536
1605
|
yield chunk;
|
|
1537
1606
|
}
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type TraceableFunction } from "langsmith/singletons/traceable";
|
|
1
2
|
import type { RunnableInterface, RunnableBatchOptions } from "./types.js";
|
|
2
3
|
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
3
4
|
import { LogStreamCallbackHandler, LogStreamCallbackHandlerInput, RunLogPatch } from "../tracers/log_stream.js";
|
|
@@ -463,6 +464,21 @@ export declare class RunnableMap<RunInput = any, RunOutput extends Record<string
|
|
|
463
464
|
transform(generator: AsyncGenerator<RunInput>, options?: Partial<RunnableConfig>): AsyncGenerator<RunOutput>;
|
|
464
465
|
stream(input: RunInput, options?: Partial<RunnableConfig>): Promise<IterableReadableStream<RunOutput>>;
|
|
465
466
|
}
|
|
467
|
+
type AnyTraceableFunction = TraceableFunction<(...any: any[]) => any>;
|
|
468
|
+
/**
|
|
469
|
+
* A runnable that wraps a traced LangSmith function.
|
|
470
|
+
*/
|
|
471
|
+
export declare class RunnableTraceable<RunInput, RunOutput> extends Runnable<RunInput, RunOutput> {
|
|
472
|
+
lc_serializable: boolean;
|
|
473
|
+
lc_namespace: string[];
|
|
474
|
+
protected func: AnyTraceableFunction;
|
|
475
|
+
constructor(fields: {
|
|
476
|
+
func: AnyTraceableFunction;
|
|
477
|
+
});
|
|
478
|
+
invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunOutput>;
|
|
479
|
+
_streamIterator(input: RunInput, options?: Partial<RunnableConfig>): AsyncGenerator<RunOutput>;
|
|
480
|
+
static from(func: AnyTraceableFunction): RunnableTraceable<unknown, unknown>;
|
|
481
|
+
}
|
|
466
482
|
/**
|
|
467
483
|
* A runnable that runs a callable.
|
|
468
484
|
*/
|
|
@@ -471,9 +487,10 @@ export declare class RunnableLambda<RunInput, RunOutput> extends Runnable<RunInp
|
|
|
471
487
|
lc_namespace: string[];
|
|
472
488
|
protected func: RunnableFunc<RunInput, RunOutput | Runnable<RunInput, RunOutput>>;
|
|
473
489
|
constructor(fields: {
|
|
474
|
-
func: RunnableFunc<RunInput, RunOutput | Runnable<RunInput, RunOutput
|
|
490
|
+
func: RunnableFunc<RunInput, RunOutput | Runnable<RunInput, RunOutput>> | TraceableFunction<RunnableFunc<RunInput, RunOutput | Runnable<RunInput, RunOutput>>>;
|
|
475
491
|
});
|
|
476
492
|
static from<RunInput, RunOutput>(func: RunnableFunc<RunInput, RunOutput | Runnable<RunInput, RunOutput>>): RunnableLambda<RunInput, RunOutput>;
|
|
493
|
+
static from<RunInput, RunOutput>(func: TraceableFunction<RunnableFunc<RunInput, RunOutput | Runnable<RunInput, RunOutput>>>): RunnableLambda<RunInput, RunOutput>;
|
|
477
494
|
_invoke(input: RunInput, config?: Partial<RunnableConfig>, runManager?: CallbackManagerForChainRun): Promise<RunOutput>;
|
|
478
495
|
invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunOutput>;
|
|
479
496
|
_transform(generator: AsyncGenerator<RunInput>, runManager?: CallbackManagerForChainRun, config?: Partial<RunnableConfig>): AsyncGenerator<RunOutput>;
|
package/dist/runnables/base.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import pRetry from "p-retry";
|
|
3
3
|
import { v4 as uuidv4 } from "uuid";
|
|
4
|
+
import { isTraceableFunction, } from "langsmith/singletons/traceable";
|
|
4
5
|
import { CallbackManager, } from "../callbacks/manager.js";
|
|
5
6
|
import { LogStreamCallbackHandler, RunLog, RunLogPatch, isLogStreamHandler, } from "../tracers/log_stream.js";
|
|
6
7
|
import { EventStreamCallbackHandler, isStreamEventsHandler, } from "../tracers/event_stream.js";
|
|
@@ -13,7 +14,7 @@ import { _RootEventFilter, isRunnableInterface } from "./utils.js";
|
|
|
13
14
|
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
14
15
|
import { Graph } from "./graph.js";
|
|
15
16
|
import { convertToHttpEventStream } from "./wrappers.js";
|
|
16
|
-
import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterator, } from "./iter.js";
|
|
17
|
+
import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterableIterator, isIterator, } from "./iter.js";
|
|
17
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
19
|
export function _coerceToDict(value, defaultKey) {
|
|
19
20
|
return value &&
|
|
@@ -1372,6 +1373,68 @@ export class RunnableMap extends Runnable {
|
|
|
1372
1373
|
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1373
1374
|
}
|
|
1374
1375
|
}
|
|
1376
|
+
/**
|
|
1377
|
+
* A runnable that wraps a traced LangSmith function.
|
|
1378
|
+
*/
|
|
1379
|
+
export class RunnableTraceable extends Runnable {
|
|
1380
|
+
constructor(fields) {
|
|
1381
|
+
super(fields);
|
|
1382
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
1383
|
+
enumerable: true,
|
|
1384
|
+
configurable: true,
|
|
1385
|
+
writable: true,
|
|
1386
|
+
value: false
|
|
1387
|
+
});
|
|
1388
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
1389
|
+
enumerable: true,
|
|
1390
|
+
configurable: true,
|
|
1391
|
+
writable: true,
|
|
1392
|
+
value: ["langchain_core", "runnables"]
|
|
1393
|
+
});
|
|
1394
|
+
Object.defineProperty(this, "func", {
|
|
1395
|
+
enumerable: true,
|
|
1396
|
+
configurable: true,
|
|
1397
|
+
writable: true,
|
|
1398
|
+
value: void 0
|
|
1399
|
+
});
|
|
1400
|
+
if (!isTraceableFunction(fields.func)) {
|
|
1401
|
+
throw new Error("RunnableTraceable requires a function that is wrapped in traceable higher-order function");
|
|
1402
|
+
}
|
|
1403
|
+
this.func = fields.func;
|
|
1404
|
+
}
|
|
1405
|
+
async invoke(input, options) {
|
|
1406
|
+
const [config] = this._getOptionsList(options ?? {}, 1);
|
|
1407
|
+
const callbacks = await getCallbackManagerForConfig(config);
|
|
1408
|
+
return (await this.func(patchConfig(config, { callbacks }), input));
|
|
1409
|
+
}
|
|
1410
|
+
async *_streamIterator(input, options) {
|
|
1411
|
+
const result = await this.invoke(input, options);
|
|
1412
|
+
if (isAsyncIterable(result)) {
|
|
1413
|
+
for await (const item of result) {
|
|
1414
|
+
yield item;
|
|
1415
|
+
}
|
|
1416
|
+
return;
|
|
1417
|
+
}
|
|
1418
|
+
if (isIterator(result)) {
|
|
1419
|
+
while (true) {
|
|
1420
|
+
const state = result.next();
|
|
1421
|
+
if (state.done)
|
|
1422
|
+
break;
|
|
1423
|
+
yield state.value;
|
|
1424
|
+
}
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
yield result;
|
|
1428
|
+
}
|
|
1429
|
+
static from(func) {
|
|
1430
|
+
return new RunnableTraceable({ func });
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
function assertNonTraceableFunction(func) {
|
|
1434
|
+
if (isTraceableFunction(func)) {
|
|
1435
|
+
throw new Error("RunnableLambda requires a function that is not wrapped in traceable higher-order function. This shouldn't happen.");
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1375
1438
|
/**
|
|
1376
1439
|
* A runnable that runs a callable.
|
|
1377
1440
|
*/
|
|
@@ -1380,6 +1443,10 @@ export class RunnableLambda extends Runnable {
|
|
|
1380
1443
|
return "RunnableLambda";
|
|
1381
1444
|
}
|
|
1382
1445
|
constructor(fields) {
|
|
1446
|
+
if (isTraceableFunction(fields.func)) {
|
|
1447
|
+
// eslint-disable-next-line no-constructor-return
|
|
1448
|
+
return RunnableTraceable.from(fields.func);
|
|
1449
|
+
}
|
|
1383
1450
|
super(fields);
|
|
1384
1451
|
Object.defineProperty(this, "lc_namespace", {
|
|
1385
1452
|
enumerable: true,
|
|
@@ -1393,6 +1460,7 @@ export class RunnableLambda extends Runnable {
|
|
|
1393
1460
|
writable: true,
|
|
1394
1461
|
value: void 0
|
|
1395
1462
|
});
|
|
1463
|
+
assertNonTraceableFunction(fields.func);
|
|
1396
1464
|
this.func = fields.func;
|
|
1397
1465
|
}
|
|
1398
1466
|
static from(func) {
|
|
@@ -1440,7 +1508,7 @@ export class RunnableLambda extends Runnable {
|
|
|
1440
1508
|
}
|
|
1441
1509
|
output = finalOutput;
|
|
1442
1510
|
}
|
|
1443
|
-
else if (
|
|
1511
|
+
else if (isIterableIterator(output)) {
|
|
1444
1512
|
let finalOutput;
|
|
1445
1513
|
for (const chunk of consumeIteratorInContext(childConfig, output)) {
|
|
1446
1514
|
if (finalOutput === undefined) {
|
|
@@ -1518,7 +1586,7 @@ export class RunnableLambda extends Runnable {
|
|
|
1518
1586
|
yield chunk;
|
|
1519
1587
|
}
|
|
1520
1588
|
}
|
|
1521
|
-
else if (
|
|
1589
|
+
else if (isIterableIterator(output)) {
|
|
1522
1590
|
for (const chunk of consumeIteratorInContext(config, output)) {
|
|
1523
1591
|
yield chunk;
|
|
1524
1592
|
}
|
package/dist/runnables/iter.cjs
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.consumeAsyncIterableInContext = exports.consumeIteratorInContext = exports.isAsyncIterable = exports.isIterator = void 0;
|
|
3
|
+
exports.consumeAsyncIterableInContext = exports.consumeIteratorInContext = exports.isAsyncIterable = exports.isIterator = exports.isIterableIterator = void 0;
|
|
4
4
|
const index_js_1 = require("../singletons/index.cjs");
|
|
5
|
-
function
|
|
5
|
+
function isIterableIterator(thing) {
|
|
6
6
|
return (typeof thing === "object" &&
|
|
7
7
|
thing !== null &&
|
|
8
8
|
typeof thing[Symbol.iterator] === "function" &&
|
|
9
9
|
// avoid detecting array/set as iterator
|
|
10
10
|
typeof thing.next === "function");
|
|
11
11
|
}
|
|
12
|
+
exports.isIterableIterator = isIterableIterator;
|
|
13
|
+
const isIterator = (x) => x != null &&
|
|
14
|
+
typeof x === "object" &&
|
|
15
|
+
"next" in x &&
|
|
16
|
+
typeof x.next === "function";
|
|
12
17
|
exports.isIterator = isIterator;
|
|
13
18
|
function isAsyncIterable(thing) {
|
|
14
19
|
return (typeof thing === "object" &&
|
package/dist/runnables/iter.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { RunnableConfig } from "./config.js";
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function isIterableIterator(thing: unknown): thing is IterableIterator<unknown>;
|
|
3
|
+
export declare const isIterator: (x: unknown) => x is Iterator<unknown, any, undefined>;
|
|
3
4
|
export declare function isAsyncIterable(thing: unknown): thing is AsyncIterable<unknown>;
|
|
4
5
|
export declare function consumeIteratorInContext<T>(context: Partial<RunnableConfig> | undefined, iter: IterableIterator<T>): IterableIterator<T>;
|
|
5
6
|
export declare function consumeAsyncIterableInContext<T>(context: Partial<RunnableConfig> | undefined, iter: AsyncIterable<T>): AsyncIterableIterator<T>;
|
package/dist/runnables/iter.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
2
|
-
export function
|
|
2
|
+
export function isIterableIterator(thing) {
|
|
3
3
|
return (typeof thing === "object" &&
|
|
4
4
|
thing !== null &&
|
|
5
5
|
typeof thing[Symbol.iterator] === "function" &&
|
|
6
6
|
// avoid detecting array/set as iterator
|
|
7
7
|
typeof thing.next === "function");
|
|
8
8
|
}
|
|
9
|
+
export const isIterator = (x) => x != null &&
|
|
10
|
+
typeof x === "object" &&
|
|
11
|
+
"next" in x &&
|
|
12
|
+
typeof x.next === "function";
|
|
9
13
|
export function isAsyncIterable(thing) {
|
|
10
14
|
return (typeof thing === "object" &&
|
|
11
15
|
thing !== null &&
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LangChainTracer = void 0;
|
|
4
4
|
const langsmith_1 = require("langsmith");
|
|
5
|
+
const traceable_1 = require("langsmith/singletons/traceable");
|
|
5
6
|
const env_js_1 = require("../utils/env.cjs");
|
|
6
7
|
const base_js_1 = require("./base.cjs");
|
|
7
8
|
class LangChainTracer extends base_js_1.BaseTracer {
|
|
@@ -38,6 +39,38 @@ class LangChainTracer extends base_js_1.BaseTracer {
|
|
|
38
39
|
(0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION");
|
|
39
40
|
this.exampleId = exampleId;
|
|
40
41
|
this.client = client ?? new langsmith_1.Client({});
|
|
42
|
+
// if we're inside traceable, we can obtain the traceable tree
|
|
43
|
+
// and populate the run map, which is used to correctly
|
|
44
|
+
// infer dotted order and execution order
|
|
45
|
+
const traceableTree = this.getTraceableRunTree();
|
|
46
|
+
if (traceableTree) {
|
|
47
|
+
let rootRun = traceableTree;
|
|
48
|
+
const visited = new Set();
|
|
49
|
+
while (rootRun.parent_run) {
|
|
50
|
+
if (visited.has(rootRun.id))
|
|
51
|
+
break;
|
|
52
|
+
visited.add(rootRun.id);
|
|
53
|
+
if (!rootRun.parent_run)
|
|
54
|
+
break;
|
|
55
|
+
rootRun = rootRun.parent_run;
|
|
56
|
+
}
|
|
57
|
+
visited.clear();
|
|
58
|
+
const queue = [rootRun];
|
|
59
|
+
while (queue.length > 0) {
|
|
60
|
+
const current = queue.shift();
|
|
61
|
+
if (!current || visited.has(current.id))
|
|
62
|
+
continue;
|
|
63
|
+
visited.add(current.id);
|
|
64
|
+
// @ts-expect-error Types of property 'events' are incompatible.
|
|
65
|
+
this.runMap.set(current.id, current);
|
|
66
|
+
if (current.child_runs) {
|
|
67
|
+
queue.push(...current.child_runs);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this.client = traceableTree.client ?? this.client;
|
|
71
|
+
this.projectName = traceableTree.project_name ?? this.projectName;
|
|
72
|
+
this.exampleId = traceableTree.reference_example_id ?? this.exampleId;
|
|
73
|
+
}
|
|
41
74
|
}
|
|
42
75
|
async _convertToCreate(run, example_id = undefined) {
|
|
43
76
|
return {
|
|
@@ -72,5 +105,13 @@ class LangChainTracer extends base_js_1.BaseTracer {
|
|
|
72
105
|
getRun(id) {
|
|
73
106
|
return this.runMap.get(id);
|
|
74
107
|
}
|
|
108
|
+
getTraceableRunTree() {
|
|
109
|
+
try {
|
|
110
|
+
return (0, traceable_1.getCurrentRunTree)();
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
75
116
|
}
|
|
76
117
|
exports.LangChainTracer = LangChainTracer;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Client } from "langsmith";
|
|
2
|
+
import { RunTree } from "langsmith/run_trees";
|
|
2
3
|
import { BaseRun, RunCreate, RunUpdate as BaseRunUpdate, KVMap } from "langsmith/schemas";
|
|
3
4
|
import { BaseTracer } from "./base.js";
|
|
4
5
|
import { BaseCallbackHandlerInput } from "../callbacks/base.js";
|
|
@@ -35,4 +36,5 @@ export declare class LangChainTracer extends BaseTracer implements LangChainTrac
|
|
|
35
36
|
onRunCreate(run: Run): Promise<void>;
|
|
36
37
|
onRunUpdate(run: Run): Promise<void>;
|
|
37
38
|
getRun(id: string): Run | undefined;
|
|
39
|
+
getTraceableRunTree(): RunTree | undefined;
|
|
38
40
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Client } from "langsmith";
|
|
2
|
+
import { getCurrentRunTree } from "langsmith/singletons/traceable";
|
|
2
3
|
import { getEnvironmentVariable, getRuntimeEnvironment } from "../utils/env.js";
|
|
3
4
|
import { BaseTracer } from "./base.js";
|
|
4
5
|
export class LangChainTracer extends BaseTracer {
|
|
@@ -35,6 +36,38 @@ export class LangChainTracer extends BaseTracer {
|
|
|
35
36
|
getEnvironmentVariable("LANGCHAIN_SESSION");
|
|
36
37
|
this.exampleId = exampleId;
|
|
37
38
|
this.client = client ?? new Client({});
|
|
39
|
+
// if we're inside traceable, we can obtain the traceable tree
|
|
40
|
+
// and populate the run map, which is used to correctly
|
|
41
|
+
// infer dotted order and execution order
|
|
42
|
+
const traceableTree = this.getTraceableRunTree();
|
|
43
|
+
if (traceableTree) {
|
|
44
|
+
let rootRun = traceableTree;
|
|
45
|
+
const visited = new Set();
|
|
46
|
+
while (rootRun.parent_run) {
|
|
47
|
+
if (visited.has(rootRun.id))
|
|
48
|
+
break;
|
|
49
|
+
visited.add(rootRun.id);
|
|
50
|
+
if (!rootRun.parent_run)
|
|
51
|
+
break;
|
|
52
|
+
rootRun = rootRun.parent_run;
|
|
53
|
+
}
|
|
54
|
+
visited.clear();
|
|
55
|
+
const queue = [rootRun];
|
|
56
|
+
while (queue.length > 0) {
|
|
57
|
+
const current = queue.shift();
|
|
58
|
+
if (!current || visited.has(current.id))
|
|
59
|
+
continue;
|
|
60
|
+
visited.add(current.id);
|
|
61
|
+
// @ts-expect-error Types of property 'events' are incompatible.
|
|
62
|
+
this.runMap.set(current.id, current);
|
|
63
|
+
if (current.child_runs) {
|
|
64
|
+
queue.push(...current.child_runs);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.client = traceableTree.client ?? this.client;
|
|
68
|
+
this.projectName = traceableTree.project_name ?? this.projectName;
|
|
69
|
+
this.exampleId = traceableTree.reference_example_id ?? this.exampleId;
|
|
70
|
+
}
|
|
38
71
|
}
|
|
39
72
|
async _convertToCreate(run, example_id = undefined) {
|
|
40
73
|
return {
|
|
@@ -69,4 +102,12 @@ export class LangChainTracer extends BaseTracer {
|
|
|
69
102
|
getRun(id) {
|
|
70
103
|
return this.runMap.get(id);
|
|
71
104
|
}
|
|
105
|
+
getTraceableRunTree() {
|
|
106
|
+
try {
|
|
107
|
+
return getCurrentRunTree();
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
72
113
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"camelcase": "6",
|
|
46
46
|
"decamelize": "1.2.0",
|
|
47
47
|
"js-tiktoken": "^1.0.12",
|
|
48
|
-
"langsmith": "~0.1.
|
|
48
|
+
"langsmith": "~0.1.30",
|
|
49
49
|
"ml-distance": "^4.0.0",
|
|
50
50
|
"mustache": "^4.2.0",
|
|
51
51
|
"p-queue": "^6.6.2",
|