@langchain/core 0.2.2 → 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.
@@ -1,9 +1,10 @@
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
- import { LogStreamCallbackHandler, RunLog, RunLogPatch, } from "../tracers/log_stream.js";
6
- import { EventStreamCallbackHandler, } from "../tracers/event_stream.js";
6
+ import { LogStreamCallbackHandler, RunLog, RunLogPatch, isLogStreamHandler, } from "../tracers/log_stream.js";
7
+ import { EventStreamCallbackHandler, isStreamEventsHandler, } from "../tracers/event_stream.js";
7
8
  import { Serializable } from "../load/serializable.js";
8
9
  import { IterableReadableStream, concat, atee, pipeGeneratorWithSetup, AsyncGeneratorWithSetup, } from "../utils/stream.js";
9
10
  import { DEFAULT_RECURSION_LIMIT, ensureConfig, getCallbackManagerForConfig, mergeConfigs, patchConfig, } from "./config.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 &&
@@ -280,13 +281,11 @@ export class Runnable extends Serializable {
280
281
  const pipe = await pipeGeneratorWithSetup(transformer.bind(this), wrapInputForTracing(), async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, config.runId, config.runType, undefined, undefined, config.runName ?? this.getName()), config);
281
282
  delete config.runId;
282
283
  runManager = pipe.setup;
283
- const isStreamEventsHandler = (handler) => handler.name === "event_stream_tracer";
284
284
  const streamEventsHandler = runManager?.handlers.find(isStreamEventsHandler);
285
285
  let iterator = pipe.output;
286
286
  if (streamEventsHandler !== undefined && runManager !== undefined) {
287
287
  iterator = streamEventsHandler.tapOutputIterable(runManager.runId, iterator);
288
288
  }
289
- const isLogStreamHandler = (handler) => handler.name === "log_stream_tracer";
290
289
  const streamLogHandler = runManager?.handlers.find(isLogStreamHandler);
291
290
  if (streamLogHandler !== undefined && runManager !== undefined) {
292
291
  iterator = streamLogHandler.tapOutputIterable(runManager.runId, iterator);
@@ -1374,6 +1373,68 @@ export class RunnableMap extends Runnable {
1374
1373
  return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
1375
1374
  }
1376
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
+ }
1377
1438
  /**
1378
1439
  * A runnable that runs a callable.
1379
1440
  */
@@ -1382,6 +1443,10 @@ export class RunnableLambda extends Runnable {
1382
1443
  return "RunnableLambda";
1383
1444
  }
1384
1445
  constructor(fields) {
1446
+ if (isTraceableFunction(fields.func)) {
1447
+ // eslint-disable-next-line no-constructor-return
1448
+ return RunnableTraceable.from(fields.func);
1449
+ }
1385
1450
  super(fields);
1386
1451
  Object.defineProperty(this, "lc_namespace", {
1387
1452
  enumerable: true,
@@ -1395,6 +1460,7 @@ export class RunnableLambda extends Runnable {
1395
1460
  writable: true,
1396
1461
  value: void 0
1397
1462
  });
1463
+ assertNonTraceableFunction(fields.func);
1398
1464
  this.func = fields.func;
1399
1465
  }
1400
1466
  static from(func) {
@@ -1442,7 +1508,7 @@ export class RunnableLambda extends Runnable {
1442
1508
  }
1443
1509
  output = finalOutput;
1444
1510
  }
1445
- else if (isIterator(output)) {
1511
+ else if (isIterableIterator(output)) {
1446
1512
  let finalOutput;
1447
1513
  for (const chunk of consumeIteratorInContext(childConfig, output)) {
1448
1514
  if (finalOutput === undefined) {
@@ -1520,7 +1586,7 @@ export class RunnableLambda extends Runnable {
1520
1586
  yield chunk;
1521
1587
  }
1522
1588
  }
1523
- else if (isIterator(output)) {
1589
+ else if (isIterableIterator(output)) {
1524
1590
  for (const chunk of consumeIteratorInContext(config, output)) {
1525
1591
  yield chunk;
1526
1592
  }
@@ -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 isIterator(thing) {
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" &&
@@ -1,5 +1,6 @@
1
1
  import { RunnableConfig } from "./config.js";
2
- export declare function isIterator(thing: unknown): thing is IterableIterator<unknown>;
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>;
@@ -1,11 +1,15 @@
1
1
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
2
- export function isIterator(thing) {
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 &&