@langchain/core 0.2.1 → 0.2.2

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.
@@ -482,7 +482,7 @@ class Runnable extends serializable_js_1.Serializable {
482
482
  async *_streamEventsV2(input, options, streamOptions) {
483
483
  const eventStreamer = new event_stream_js_1.EventStreamCallbackHandler({
484
484
  ...streamOptions,
485
- autoClose: false,
485
+ autoClose: true,
486
486
  });
487
487
  const config = (0, config_js_1.ensureConfig)(options);
488
488
  const runId = config.runId ?? (0, uuid_1.v4)();
@@ -504,16 +504,11 @@ class Runnable extends serializable_js_1.Serializable {
504
504
  // add each chunk to the output stream
505
505
  const outerThis = this;
506
506
  async function consumeRunnableStream() {
507
- try {
508
- const runnableStream = await outerThis.stream(input, config);
509
- const tappedStream = eventStreamer.tapOutputIterable(runId, runnableStream);
510
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
511
- for await (const _ of tappedStream) {
512
- // Just iterate so that the callback handler picks up events
513
- }
514
- }
515
- finally {
516
- await eventStreamer.writer.close();
507
+ const runnableStream = await outerThis.stream(input, config);
508
+ const tappedStream = eventStreamer.tapOutputIterable(runId, runnableStream);
509
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
510
+ for await (const _ of tappedStream) {
511
+ // Just iterate so that the callback handler picks up events
517
512
  }
518
513
  }
519
514
  const runnableStreamConsumePromise = consumeRunnableStream();
@@ -475,7 +475,7 @@ export class Runnable extends Serializable {
475
475
  async *_streamEventsV2(input, options, streamOptions) {
476
476
  const eventStreamer = new EventStreamCallbackHandler({
477
477
  ...streamOptions,
478
- autoClose: false,
478
+ autoClose: true,
479
479
  });
480
480
  const config = ensureConfig(options);
481
481
  const runId = config.runId ?? uuidv4();
@@ -497,16 +497,11 @@ export class Runnable extends Serializable {
497
497
  // add each chunk to the output stream
498
498
  const outerThis = this;
499
499
  async function consumeRunnableStream() {
500
- try {
501
- const runnableStream = await outerThis.stream(input, config);
502
- const tappedStream = eventStreamer.tapOutputIterable(runId, runnableStream);
503
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
504
- for await (const _ of tappedStream) {
505
- // Just iterate so that the callback handler picks up events
506
- }
507
- }
508
- finally {
509
- await eventStreamer.writer.close();
500
+ const runnableStream = await outerThis.stream(input, config);
501
+ const tappedStream = eventStreamer.tapOutputIterable(runId, runnableStream);
502
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
503
+ for await (const _ of tappedStream) {
504
+ // Just iterate so that the callback handler picks up events
510
505
  }
511
506
  }
512
507
  const runnableStreamConsumePromise = consumeRunnableStream();
@@ -205,6 +205,7 @@ class EventStreamCallbackHandler extends base_js_1.BaseTracer {
205
205
  finally {
206
206
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
207
207
  tappedPromiseResolver();
208
+ // Don't delete from the map to keep track of which runs have been tapped.
208
209
  }
209
210
  }
210
211
  else {
@@ -489,5 +490,18 @@ class EventStreamCallbackHandler extends base_js_1.BaseTracer {
489
490
  metadata: runInfo.metadata,
490
491
  }, runInfo);
491
492
  }
493
+ async onRunCreate(run) {
494
+ if (this.rootId === undefined) {
495
+ this.rootId = run.id;
496
+ }
497
+ }
498
+ async onRunUpdate(run) {
499
+ if (run.id === this.rootId && this.autoClose) {
500
+ const pendingPromises = [...this.tappedPromises.values()];
501
+ void Promise.all(pendingPromises).finally(() => {
502
+ void this.writer.close();
503
+ });
504
+ }
505
+ }
492
506
  }
493
507
  exports.EventStreamCallbackHandler = EventStreamCallbackHandler;
@@ -133,5 +133,7 @@ export declare class EventStreamCallbackHandler extends BaseTracer {
133
133
  onToolEnd(run: Run): Promise<void>;
134
134
  onRetrieverStart(run: Run): Promise<void>;
135
135
  onRetrieverEnd(run: Run): Promise<void>;
136
+ onRunCreate(run: Run): Promise<void>;
137
+ onRunUpdate(run: Run): Promise<void>;
136
138
  }
137
139
  export {};
@@ -202,6 +202,7 @@ export class EventStreamCallbackHandler extends BaseTracer {
202
202
  finally {
203
203
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
204
204
  tappedPromiseResolver();
205
+ // Don't delete from the map to keep track of which runs have been tapped.
205
206
  }
206
207
  }
207
208
  else {
@@ -486,4 +487,17 @@ export class EventStreamCallbackHandler extends BaseTracer {
486
487
  metadata: runInfo.metadata,
487
488
  }, runInfo);
488
489
  }
490
+ async onRunCreate(run) {
491
+ if (this.rootId === undefined) {
492
+ this.rootId = run.id;
493
+ }
494
+ }
495
+ async onRunUpdate(run) {
496
+ if (run.id === this.rootId && this.autoClose) {
497
+ const pendingPromises = [...this.tappedPromises.values()];
498
+ void Promise.all(pendingPromises).finally(() => {
499
+ void this.writer.close();
500
+ });
501
+ }
502
+ }
489
503
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {