@langchain/core 0.1.62 → 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.
@@ -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) => {
@@ -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 &&
@@ -1346,6 +1347,44 @@ class RunnableLambda extends Runnable {
1346
1347
  recursionLimit: (childConfig.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
1347
1348
  });
1348
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
+ }
1349
1388
  resolve(output);
1350
1389
  }
1351
1390
  catch (e) {
@@ -1400,6 +1439,16 @@ class RunnableLambda extends Runnable {
1400
1439
  yield chunk;
1401
1440
  }
1402
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
+ }
1403
1452
  else {
1404
1453
  yield output;
1405
1454
  }
@@ -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 &&
@@ -1333,6 +1334,44 @@ export class RunnableLambda extends Runnable {
1333
1334
  recursionLimit: (childConfig.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
1334
1335
  });
1335
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
+ }
1336
1375
  resolve(output);
1337
1376
  }
1338
1377
  catch (e) {
@@ -1387,6 +1426,16 @@ export class RunnableLambda extends Runnable {
1387
1426
  yield chunk;
1388
1427
  }
1389
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
+ }
1390
1439
  else {
1391
1440
  yield output;
1392
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
+ }
@@ -7,7 +7,7 @@ class MockAsyncLocalStorage {
7
7
  return undefined;
8
8
  }
9
9
  run(_store, callback) {
10
- callback();
10
+ return callback();
11
11
  }
12
12
  }
13
13
  exports.MockAsyncLocalStorage = MockAsyncLocalStorage;
@@ -1,10 +1,10 @@
1
1
  export interface AsyncLocalStorageInterface {
2
2
  getStore: () => any | undefined;
3
- run: (store: any, callback: () => any) => any;
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: () => any): any;
7
+ run<T>(_store: any, callback: () => T): T;
8
8
  }
9
9
  declare class AsyncLocalStorageProvider {
10
10
  private asyncLocalStorage;
@@ -4,7 +4,7 @@ export class MockAsyncLocalStorage {
4
4
  return undefined;
5
5
  }
6
6
  run(_store, callback) {
7
- callback();
7
+ return callback();
8
8
  }
9
9
  }
10
10
  class AsyncLocalStorageProvider {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.62",
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.8",
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",