@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.
- package/dist/callbacks/manager.cjs +71 -23
- package/dist/callbacks/manager.js +71 -23
- package/dist/callbacks/tests/callbacks.test.js +3 -0
- package/dist/runnables/base.cjs +2 -2
- package/dist/runnables/base.d.ts +49 -3
- package/dist/runnables/base.js +2 -2
- package/dist/runnables/config.cjs +4 -7
- package/dist/runnables/config.d.ts +3 -17
- package/dist/runnables/config.js +5 -8
- package/dist/runnables/graph.d.ts +1 -1
- package/dist/runnables/iter.cjs +2 -4
- package/dist/runnables/iter.js +2 -4
- package/dist/runnables/tests/runnable_stream_events_v2.test.js +52 -48
- package/dist/runnables/types.d.ts +14 -1
- package/dist/singletons/index.cjs +35 -5
- package/dist/singletons/index.d.ts +2 -0
- package/dist/singletons/index.js +34 -4
- package/dist/singletons/tests/async_local_storage.test.js +2 -3
- package/dist/tracers/base.cjs +67 -13
- package/dist/tracers/base.d.ts +176 -1
- package/dist/tracers/base.js +65 -12
- package/dist/tracers/event_stream.cjs +15 -2
- package/dist/tracers/event_stream.js +15 -2
- package/dist/tracers/tests/langsmith_interop.test.d.ts +1 -0
- package/dist/tracers/tests/langsmith_interop.test.js +551 -0
- package/dist/tracers/tracer_langchain.cjs +73 -31
- package/dist/tracers/tracer_langchain.d.ts +3 -1
- package/dist/tracers/tracer_langchain.js +73 -31
- package/dist/utils/stream.cjs +8 -9
- package/dist/utils/stream.js +8 -9
- package/package.json +2 -2
package/dist/tracers/base.js
CHANGED
|
@@ -12,6 +12,9 @@ function convertToDottedOrderFormat(epoch, runId, executionOrder) {
|
|
|
12
12
|
const paddedOrder = executionOrder.toFixed(0).slice(0, 3).padStart(3, "0");
|
|
13
13
|
return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}${paddedOrder}Z`) + runId);
|
|
14
14
|
}
|
|
15
|
+
export function isBaseTracer(x) {
|
|
16
|
+
return typeof x._addRunToRunMap === "function";
|
|
17
|
+
}
|
|
15
18
|
export class BaseTracer extends BaseCallbackHandler {
|
|
16
19
|
constructor(_fields) {
|
|
17
20
|
super(...arguments);
|
|
@@ -38,7 +41,7 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
38
41
|
_addChildRun(parentRun, childRun) {
|
|
39
42
|
parentRun.child_runs.push(childRun);
|
|
40
43
|
}
|
|
41
|
-
|
|
44
|
+
_addRunToRunMap(run) {
|
|
42
45
|
const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id, run.execution_order);
|
|
43
46
|
const storedRun = { ...run };
|
|
44
47
|
if (storedRun.parent_run_id !== undefined) {
|
|
@@ -70,7 +73,7 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
70
73
|
storedRun.dotted_order = currentDottedOrder;
|
|
71
74
|
}
|
|
72
75
|
this.runMap.set(storedRun.id, storedRun);
|
|
73
|
-
|
|
76
|
+
return storedRun;
|
|
74
77
|
}
|
|
75
78
|
async _endTrace(run) {
|
|
76
79
|
const parentRun = run.parent_run_id !== undefined && this.runMap.get(run.parent_run_id);
|
|
@@ -91,7 +94,12 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
91
94
|
}
|
|
92
95
|
return parentRun.child_execution_order + 1;
|
|
93
96
|
}
|
|
94
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Create and add a run to the run map for LLM start events.
|
|
99
|
+
* This must sometimes be done synchronously to avoid race conditions
|
|
100
|
+
* when callbacks are backgrounded, so we expose it as a separate method here.
|
|
101
|
+
*/
|
|
102
|
+
_createRunForLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, name) {
|
|
95
103
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
96
104
|
const start_time = Date.now();
|
|
97
105
|
const finalExtraParams = metadata
|
|
@@ -117,11 +125,21 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
117
125
|
extra: finalExtraParams ?? {},
|
|
118
126
|
tags: tags || [],
|
|
119
127
|
};
|
|
120
|
-
|
|
128
|
+
return this._addRunToRunMap(run);
|
|
129
|
+
}
|
|
130
|
+
async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, name) {
|
|
131
|
+
const run = this.runMap.get(runId) ??
|
|
132
|
+
this._createRunForLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, name);
|
|
133
|
+
await this.onRunCreate?.(run);
|
|
121
134
|
await this.onLLMStart?.(run);
|
|
122
135
|
return run;
|
|
123
136
|
}
|
|
124
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Create and add a run to the run map for chat model start events.
|
|
139
|
+
* This must sometimes be done synchronously to avoid race conditions
|
|
140
|
+
* when callbacks are backgrounded, so we expose it as a separate method here.
|
|
141
|
+
*/
|
|
142
|
+
_createRunForChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, name) {
|
|
125
143
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
126
144
|
const start_time = Date.now();
|
|
127
145
|
const finalExtraParams = metadata
|
|
@@ -147,7 +165,12 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
147
165
|
extra: finalExtraParams ?? {},
|
|
148
166
|
tags: tags || [],
|
|
149
167
|
};
|
|
150
|
-
|
|
168
|
+
return this._addRunToRunMap(run);
|
|
169
|
+
}
|
|
170
|
+
async handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, name) {
|
|
171
|
+
const run = this.runMap.get(runId) ??
|
|
172
|
+
this._createRunForChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, name);
|
|
173
|
+
await this.onRunCreate?.(run);
|
|
151
174
|
await this.onLLMStart?.(run);
|
|
152
175
|
return run;
|
|
153
176
|
}
|
|
@@ -181,7 +204,12 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
181
204
|
await this._endTrace(run);
|
|
182
205
|
return run;
|
|
183
206
|
}
|
|
184
|
-
|
|
207
|
+
/**
|
|
208
|
+
* Create and add a run to the run map for chain start events.
|
|
209
|
+
* This must sometimes be done synchronously to avoid race conditions
|
|
210
|
+
* when callbacks are backgrounded, so we expose it as a separate method here.
|
|
211
|
+
*/
|
|
212
|
+
_createRunForChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, name) {
|
|
185
213
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
186
214
|
const start_time = Date.now();
|
|
187
215
|
const run = {
|
|
@@ -204,7 +232,12 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
204
232
|
extra: metadata ? { metadata } : {},
|
|
205
233
|
tags: tags || [],
|
|
206
234
|
};
|
|
207
|
-
|
|
235
|
+
return this._addRunToRunMap(run);
|
|
236
|
+
}
|
|
237
|
+
async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, name) {
|
|
238
|
+
const run = this.runMap.get(runId) ??
|
|
239
|
+
this._createRunForChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, name);
|
|
240
|
+
await this.onRunCreate?.(run);
|
|
208
241
|
await this.onChainStart?.(run);
|
|
209
242
|
return run;
|
|
210
243
|
}
|
|
@@ -244,7 +277,12 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
244
277
|
await this._endTrace(run);
|
|
245
278
|
return run;
|
|
246
279
|
}
|
|
247
|
-
|
|
280
|
+
/**
|
|
281
|
+
* Create and add a run to the run map for tool start events.
|
|
282
|
+
* This must sometimes be done synchronously to avoid race conditions
|
|
283
|
+
* when callbacks are backgrounded, so we expose it as a separate method here.
|
|
284
|
+
*/
|
|
285
|
+
_createRunForToolStart(tool, input, runId, parentRunId, tags, metadata, name) {
|
|
248
286
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
249
287
|
const start_time = Date.now();
|
|
250
288
|
const run = {
|
|
@@ -267,7 +305,12 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
267
305
|
extra: metadata ? { metadata } : {},
|
|
268
306
|
tags: tags || [],
|
|
269
307
|
};
|
|
270
|
-
|
|
308
|
+
return this._addRunToRunMap(run);
|
|
309
|
+
}
|
|
310
|
+
async handleToolStart(tool, input, runId, parentRunId, tags, metadata, name) {
|
|
311
|
+
const run = this.runMap.get(runId) ??
|
|
312
|
+
this._createRunForToolStart(tool, input, runId, parentRunId, tags, metadata, name);
|
|
313
|
+
await this.onRunCreate?.(run);
|
|
271
314
|
await this.onToolStart?.(run);
|
|
272
315
|
return run;
|
|
273
316
|
}
|
|
@@ -329,7 +372,12 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
329
372
|
});
|
|
330
373
|
await this.onAgentEnd?.(run);
|
|
331
374
|
}
|
|
332
|
-
|
|
375
|
+
/**
|
|
376
|
+
* Create and add a run to the run map for retriever start events.
|
|
377
|
+
* This must sometimes be done synchronously to avoid race conditions
|
|
378
|
+
* when callbacks are backgrounded, so we expose it as a separate method here.
|
|
379
|
+
*/
|
|
380
|
+
_createRunForRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
|
|
333
381
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
334
382
|
const start_time = Date.now();
|
|
335
383
|
const run = {
|
|
@@ -352,7 +400,12 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
352
400
|
extra: metadata ? { metadata } : {},
|
|
353
401
|
tags: tags || [],
|
|
354
402
|
};
|
|
355
|
-
|
|
403
|
+
return this._addRunToRunMap(run);
|
|
404
|
+
}
|
|
405
|
+
async handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
|
|
406
|
+
const run = this.runMap.get(runId) ??
|
|
407
|
+
this._createRunForRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name);
|
|
408
|
+
await this.onRunCreate?.(run);
|
|
356
409
|
await this.onRetrieverStart?.(run);
|
|
357
410
|
return run;
|
|
358
411
|
}
|
|
@@ -166,6 +166,13 @@ class EventStreamCallbackHandler extends base_js_1.BaseTracer {
|
|
|
166
166
|
yield firstChunk.value;
|
|
167
167
|
return;
|
|
168
168
|
}
|
|
169
|
+
// Match format from handlers below
|
|
170
|
+
function _formatOutputChunk(eventType, data) {
|
|
171
|
+
if (eventType === "llm" && typeof data === "string") {
|
|
172
|
+
return new outputs_js_1.GenerationChunk({ text: data });
|
|
173
|
+
}
|
|
174
|
+
return data;
|
|
175
|
+
}
|
|
169
176
|
let tappedPromise = this.tappedPromises.get(runId);
|
|
170
177
|
// if we are the first to tap, issue stream events
|
|
171
178
|
if (tappedPromise === undefined) {
|
|
@@ -185,7 +192,9 @@ class EventStreamCallbackHandler extends base_js_1.BaseTracer {
|
|
|
185
192
|
};
|
|
186
193
|
await this.send({
|
|
187
194
|
...event,
|
|
188
|
-
data: {
|
|
195
|
+
data: {
|
|
196
|
+
chunk: _formatOutputChunk(runInfo.runType, firstChunk.value),
|
|
197
|
+
},
|
|
189
198
|
}, runInfo);
|
|
190
199
|
yield firstChunk.value;
|
|
191
200
|
for await (const chunk of outputStream) {
|
|
@@ -194,7 +203,7 @@ class EventStreamCallbackHandler extends base_js_1.BaseTracer {
|
|
|
194
203
|
await this.send({
|
|
195
204
|
...event,
|
|
196
205
|
data: {
|
|
197
|
-
chunk,
|
|
206
|
+
chunk: _formatOutputChunk(runInfo.runType, chunk),
|
|
198
207
|
},
|
|
199
208
|
}, runInfo);
|
|
200
209
|
}
|
|
@@ -263,6 +272,10 @@ class EventStreamCallbackHandler extends base_js_1.BaseTracer {
|
|
|
263
272
|
if (runInfo === undefined) {
|
|
264
273
|
throw new Error(`onLLMNewToken: Run ID ${run.id} not found in run map.`);
|
|
265
274
|
}
|
|
275
|
+
// Top-level streaming events are covered by tapOutputIterable
|
|
276
|
+
if (run.parent_run_id === undefined) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
266
279
|
if (runInfo.runType === "chat_model") {
|
|
267
280
|
eventName = "on_chat_model_stream";
|
|
268
281
|
if (kwargs?.chunk === undefined) {
|
|
@@ -162,6 +162,13 @@ export class EventStreamCallbackHandler extends BaseTracer {
|
|
|
162
162
|
yield firstChunk.value;
|
|
163
163
|
return;
|
|
164
164
|
}
|
|
165
|
+
// Match format from handlers below
|
|
166
|
+
function _formatOutputChunk(eventType, data) {
|
|
167
|
+
if (eventType === "llm" && typeof data === "string") {
|
|
168
|
+
return new GenerationChunk({ text: data });
|
|
169
|
+
}
|
|
170
|
+
return data;
|
|
171
|
+
}
|
|
165
172
|
let tappedPromise = this.tappedPromises.get(runId);
|
|
166
173
|
// if we are the first to tap, issue stream events
|
|
167
174
|
if (tappedPromise === undefined) {
|
|
@@ -181,7 +188,9 @@ export class EventStreamCallbackHandler extends BaseTracer {
|
|
|
181
188
|
};
|
|
182
189
|
await this.send({
|
|
183
190
|
...event,
|
|
184
|
-
data: {
|
|
191
|
+
data: {
|
|
192
|
+
chunk: _formatOutputChunk(runInfo.runType, firstChunk.value),
|
|
193
|
+
},
|
|
185
194
|
}, runInfo);
|
|
186
195
|
yield firstChunk.value;
|
|
187
196
|
for await (const chunk of outputStream) {
|
|
@@ -190,7 +199,7 @@ export class EventStreamCallbackHandler extends BaseTracer {
|
|
|
190
199
|
await this.send({
|
|
191
200
|
...event,
|
|
192
201
|
data: {
|
|
193
|
-
chunk,
|
|
202
|
+
chunk: _formatOutputChunk(runInfo.runType, chunk),
|
|
194
203
|
},
|
|
195
204
|
}, runInfo);
|
|
196
205
|
}
|
|
@@ -259,6 +268,10 @@ export class EventStreamCallbackHandler extends BaseTracer {
|
|
|
259
268
|
if (runInfo === undefined) {
|
|
260
269
|
throw new Error(`onLLMNewToken: Run ID ${run.id} not found in run map.`);
|
|
261
270
|
}
|
|
271
|
+
// Top-level streaming events are covered by tapOutputIterable
|
|
272
|
+
if (run.parent_run_id === undefined) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
262
275
|
if (runInfo.runType === "chat_model") {
|
|
263
276
|
eventName = "on_chat_model_stream";
|
|
264
277
|
if (kwargs?.chunk === undefined) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|