@langchain/core 0.2.17 → 0.2.18

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,4 +1,5 @@
1
1
  import { Client } from "langsmith";
2
+ import { RunTree } from "langsmith/run_trees";
2
3
  import { getCurrentRunTree } from "langsmith/singletons/traceable";
3
4
  import { getEnvironmentVariable, getRuntimeEnvironment } from "../utils/env.js";
4
5
  import { BaseTracer } from "./base.js";
@@ -36,37 +37,9 @@ export class LangChainTracer extends BaseTracer {
36
37
  getEnvironmentVariable("LANGCHAIN_SESSION");
37
38
  this.exampleId = exampleId;
38
39
  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();
40
+ const traceableTree = LangChainTracer.getTraceableRunTree();
43
41
  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;
42
+ this.updateFromRunTree(traceableTree);
70
43
  }
71
44
  }
72
45
  async _convertToCreate(run, example_id = undefined) {
@@ -102,7 +75,76 @@ export class LangChainTracer extends BaseTracer {
102
75
  getRun(id) {
103
76
  return this.runMap.get(id);
104
77
  }
105
- getTraceableRunTree() {
78
+ updateFromRunTree(runTree) {
79
+ let rootRun = runTree;
80
+ const visited = new Set();
81
+ while (rootRun.parent_run) {
82
+ if (visited.has(rootRun.id))
83
+ break;
84
+ visited.add(rootRun.id);
85
+ if (!rootRun.parent_run)
86
+ break;
87
+ rootRun = rootRun.parent_run;
88
+ }
89
+ visited.clear();
90
+ const queue = [rootRun];
91
+ while (queue.length > 0) {
92
+ const current = queue.shift();
93
+ if (!current || visited.has(current.id))
94
+ continue;
95
+ visited.add(current.id);
96
+ // @ts-expect-error Types of property 'events' are incompatible.
97
+ this.runMap.set(current.id, current);
98
+ if (current.child_runs) {
99
+ queue.push(...current.child_runs);
100
+ }
101
+ }
102
+ this.client = runTree.client ?? this.client;
103
+ this.projectName = runTree.project_name ?? this.projectName;
104
+ this.exampleId = runTree.reference_example_id ?? this.exampleId;
105
+ }
106
+ convertToRunTree(id) {
107
+ const runTreeMap = {};
108
+ const runTreeList = [];
109
+ for (const [id, run] of this.runMap) {
110
+ // by converting the run map to a run tree, we are doing a copy
111
+ // thus, any mutation performed on the run tree will not be reflected
112
+ // back in the run map
113
+ // TODO: Stop using `this.runMap` in favour of LangSmith's `RunTree`
114
+ const runTree = new RunTree({
115
+ ...run,
116
+ child_runs: [],
117
+ parent_run: undefined,
118
+ // inherited properties
119
+ client: this.client,
120
+ project_name: this.projectName,
121
+ reference_example_id: this.exampleId,
122
+ tracingEnabled: true,
123
+ });
124
+ runTreeMap[id] = runTree;
125
+ runTreeList.push([id, run.dotted_order]);
126
+ }
127
+ runTreeList.sort((a, b) => {
128
+ if (!a[1] || !b[1])
129
+ return 0;
130
+ return a[1].localeCompare(b[1]);
131
+ });
132
+ for (const [id] of runTreeList) {
133
+ const run = this.runMap.get(id);
134
+ const runTree = runTreeMap[id];
135
+ if (!run || !runTree)
136
+ continue;
137
+ if (run.parent_run_id) {
138
+ const parentRunTree = runTreeMap[run.parent_run_id];
139
+ if (parentRunTree) {
140
+ parentRunTree.child_runs.push(runTree);
141
+ runTree.parent_run = parentRunTree;
142
+ }
143
+ }
144
+ }
145
+ return runTreeMap[id];
146
+ }
147
+ static getTraceableRunTree() {
106
148
  try {
107
149
  return getCurrentRunTree();
108
150
  }
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pipeGeneratorWithSetup = exports.AsyncGeneratorWithSetup = exports.concat = exports.atee = exports.IterableReadableStream = void 0;
4
- // Make this a type to override ReadableStream's async iterator type in case
5
- // the popular web-streams-polyfill is imported - the supplied types
6
4
  const index_js_1 = require("../singletons/index.cjs");
7
5
  /*
8
6
  * Support async iterator syntax for ReadableStreams in all environments.
@@ -208,8 +206,7 @@ class AsyncGeneratorWithSetup {
208
206
  // needs to happen in logical order, ie. in the order in which input to
209
207
  // to each generator is available.
210
208
  this.setup = new Promise((resolve, reject) => {
211
- const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
212
- void storage.run(params.config, async () => {
209
+ void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(params.config, async () => {
213
210
  this.firstResult = params.generator.next();
214
211
  if (params.startSetup) {
215
212
  this.firstResult.then(params.startSetup).then(resolve, reject);
@@ -217,7 +214,7 @@ class AsyncGeneratorWithSetup {
217
214
  else {
218
215
  this.firstResult.then((_result) => resolve(undefined), reject);
219
216
  }
220
- });
217
+ }, true);
221
218
  });
222
219
  }
223
220
  async next(...args) {
@@ -225,10 +222,9 @@ class AsyncGeneratorWithSetup {
225
222
  this.firstResultUsed = true;
226
223
  return this.firstResult;
227
224
  }
228
- const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
229
- return storage.run(this.config, async () => {
225
+ return index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(this.config, async () => {
230
226
  return this.generator.next(...args);
231
- });
227
+ }, true);
232
228
  }
233
229
  async return(value) {
234
230
  return this.generator.return(value);
@@ -242,7 +238,10 @@ class AsyncGeneratorWithSetup {
242
238
  }
243
239
  exports.AsyncGeneratorWithSetup = AsyncGeneratorWithSetup;
244
240
  async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
245
- const gen = new AsyncGeneratorWithSetup({ generator, startSetup });
241
+ const gen = new AsyncGeneratorWithSetup({
242
+ generator,
243
+ startSetup,
244
+ });
246
245
  const setup = await gen.setup;
247
246
  return { output: to(gen, setup, ...args), setup };
248
247
  }
@@ -1,5 +1,3 @@
1
- // Make this a type to override ReadableStream's async iterator type in case
2
- // the popular web-streams-polyfill is imported - the supplied types
3
1
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
4
2
  /*
5
3
  * Support async iterator syntax for ReadableStreams in all environments.
@@ -202,8 +200,7 @@ export class AsyncGeneratorWithSetup {
202
200
  // needs to happen in logical order, ie. in the order in which input to
203
201
  // to each generator is available.
204
202
  this.setup = new Promise((resolve, reject) => {
205
- const storage = AsyncLocalStorageProviderSingleton.getInstance();
206
- void storage.run(params.config, async () => {
203
+ void AsyncLocalStorageProviderSingleton.runWithConfig(params.config, async () => {
207
204
  this.firstResult = params.generator.next();
208
205
  if (params.startSetup) {
209
206
  this.firstResult.then(params.startSetup).then(resolve, reject);
@@ -211,7 +208,7 @@ export class AsyncGeneratorWithSetup {
211
208
  else {
212
209
  this.firstResult.then((_result) => resolve(undefined), reject);
213
210
  }
214
- });
211
+ }, true);
215
212
  });
216
213
  }
217
214
  async next(...args) {
@@ -219,10 +216,9 @@ export class AsyncGeneratorWithSetup {
219
216
  this.firstResultUsed = true;
220
217
  return this.firstResult;
221
218
  }
222
- const storage = AsyncLocalStorageProviderSingleton.getInstance();
223
- return storage.run(this.config, async () => {
219
+ return AsyncLocalStorageProviderSingleton.runWithConfig(this.config, async () => {
224
220
  return this.generator.next(...args);
225
- });
221
+ }, true);
226
222
  }
227
223
  async return(value) {
228
224
  return this.generator.return(value);
@@ -235,7 +231,10 @@ export class AsyncGeneratorWithSetup {
235
231
  }
236
232
  }
237
233
  export async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
238
- const gen = new AsyncGeneratorWithSetup({ generator, startSetup });
234
+ const gen = new AsyncGeneratorWithSetup({
235
+ generator,
236
+ startSetup,
237
+ });
239
238
  const setup = await gen.setup;
240
239
  return { output: to(gen, setup, ...args), setup };
241
240
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.2.17",
3
+ "version": "0.2.18",
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.30",
48
+ "langsmith": "~0.1.39",
49
49
  "ml-distance": "^4.0.0",
50
50
  "mustache": "^4.2.0",
51
51
  "p-queue": "^6.6.2",