@langchain/core 0.1.61 → 0.1.62

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.
@@ -16,6 +16,7 @@ const root_listener_js_1 = require("../tracers/root_listener.cjs");
16
16
  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
+ const wrappers_js_1 = require("./wrappers.cjs");
19
20
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
21
  function _coerceToDict(value, defaultKey) {
21
22
  return value &&
@@ -448,46 +449,16 @@ class Runnable extends serializable_js_1.Serializable {
448
449
  await runnableStreamConsumePromise;
449
450
  }
450
451
  }
451
- /**
452
- * Generate a stream of events emitted by the internal steps of the runnable.
453
- *
454
- * Use to create an iterator over StreamEvents that provide real-time information
455
- * about the progress of the runnable, including StreamEvents from intermediate
456
- * results.
457
- *
458
- * A StreamEvent is a dictionary with the following schema:
459
- *
460
- * - `event`: string - Event names are of the format: on_[runnable_type]_(start|stream|end).
461
- * - `name`: string - The name of the runnable that generated the event.
462
- * - `run_id`: string - Randomly generated ID associated with the given execution of
463
- * the runnable that emitted the event. A child runnable that gets invoked as part of the execution of a
464
- * parent runnable is assigned its own unique ID.
465
- * - `tags`: string[] - The tags of the runnable that generated the event.
466
- * - `metadata`: Record<string, any> - The metadata of the runnable that generated the event.
467
- * - `data`: Record<string, any>
468
- *
469
- * Below is a table that illustrates some events that might be emitted by various
470
- * chains. Metadata fields have been omitted from the table for brevity.
471
- * Chain definitions have been included after the table.
472
- *
473
- * | event | name | chunk | input | output |
474
- * |----------------------|------------------|------------------------------------|-----------------------------------------------|-------------------------------------------------|
475
- * | on_llm_start | [model name] | | {'input': 'hello'} | |
476
- * | on_llm_stream | [model name] | 'Hello' OR AIMessageChunk("hello") | | |
477
- * | on_llm_end | [model name] | | 'Hello human!' |
478
- * | on_chain_start | format_docs | | | |
479
- * | on_chain_stream | format_docs | "hello world!, goodbye world!" | | |
480
- * | on_chain_end | format_docs | | [Document(...)] | "hello world!, goodbye world!" |
481
- * | on_tool_start | some_tool | | {"x": 1, "y": "2"} | |
482
- * | on_tool_stream | some_tool | {"x": 1, "y": "2"} | | |
483
- * | on_tool_end | some_tool | | | {"x": 1, "y": "2"} |
484
- * | on_retriever_start | [retriever name] | | {"query": "hello"} | |
485
- * | on_retriever_chunk | [retriever name] | {documents: [...]} | | |
486
- * | on_retriever_end | [retriever name] | | {"query": "hello"} | {documents: [...]} |
487
- * | on_prompt_start | [template_name] | | {"question": "hello"} | |
488
- * | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) |
489
- */
490
452
  async *streamEvents(input, options, streamOptions) {
453
+ if (options.encoding === "text/event-stream") {
454
+ const stream = await this._streamEvents(input, options, streamOptions);
455
+ yield* (0, wrappers_js_1.convertToHttpEventStream)(stream);
456
+ }
457
+ else {
458
+ yield* this._streamEvents(input, options, streamOptions);
459
+ }
460
+ }
461
+ async *_streamEvents(input, options, streamOptions) {
491
462
  if (options.version !== "v1") {
492
463
  throw new Error(`Only version "v1" of the events schema is currently supported.`);
493
464
  }
@@ -199,6 +199,13 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
199
199
  streamEvents(input: RunInput, options: Partial<CallOptions> & {
200
200
  version: "v1";
201
201
  }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
202
+ streamEvents(input: RunInput, options: Partial<CallOptions> & {
203
+ version: "v1";
204
+ encoding: "text/event-stream";
205
+ }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<Uint8Array>;
206
+ _streamEvents(input: RunInput, options: Partial<CallOptions> & {
207
+ version: "v1";
208
+ }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
202
209
  static isRunnable(thing: any): thing is Runnable;
203
210
  /**
204
211
  * Bind lifecycle listeners to a Runnable, returning a new Runnable.
@@ -257,6 +264,10 @@ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends Ru
257
264
  streamEvents(input: RunInput, options: Partial<CallOptions> & {
258
265
  version: "v1";
259
266
  }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
267
+ streamEvents(input: RunInput, options: Partial<CallOptions> & {
268
+ version: "v1";
269
+ encoding: "text/event-stream";
270
+ }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<Uint8Array>;
260
271
  static isRunnableBinding(thing: any): thing is RunnableBinding<any, any, any>;
261
272
  /**
262
273
  * Bind lifecycle listeners to a Runnable, returning a new Runnable.
@@ -10,6 +10,7 @@ import { RootListenersTracer } from "../tracers/root_listener.js";
10
10
  import { _RootEventFilter, isRunnableInterface } from "./utils.js";
11
11
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
12
12
  import { Graph } from "./graph.js";
13
+ import { convertToHttpEventStream } from "./wrappers.js";
13
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
15
  export function _coerceToDict(value, defaultKey) {
15
16
  return value &&
@@ -441,46 +442,16 @@ export class Runnable extends Serializable {
441
442
  await runnableStreamConsumePromise;
442
443
  }
443
444
  }
444
- /**
445
- * Generate a stream of events emitted by the internal steps of the runnable.
446
- *
447
- * Use to create an iterator over StreamEvents that provide real-time information
448
- * about the progress of the runnable, including StreamEvents from intermediate
449
- * results.
450
- *
451
- * A StreamEvent is a dictionary with the following schema:
452
- *
453
- * - `event`: string - Event names are of the format: on_[runnable_type]_(start|stream|end).
454
- * - `name`: string - The name of the runnable that generated the event.
455
- * - `run_id`: string - Randomly generated ID associated with the given execution of
456
- * the runnable that emitted the event. A child runnable that gets invoked as part of the execution of a
457
- * parent runnable is assigned its own unique ID.
458
- * - `tags`: string[] - The tags of the runnable that generated the event.
459
- * - `metadata`: Record<string, any> - The metadata of the runnable that generated the event.
460
- * - `data`: Record<string, any>
461
- *
462
- * Below is a table that illustrates some events that might be emitted by various
463
- * chains. Metadata fields have been omitted from the table for brevity.
464
- * Chain definitions have been included after the table.
465
- *
466
- * | event | name | chunk | input | output |
467
- * |----------------------|------------------|------------------------------------|-----------------------------------------------|-------------------------------------------------|
468
- * | on_llm_start | [model name] | | {'input': 'hello'} | |
469
- * | on_llm_stream | [model name] | 'Hello' OR AIMessageChunk("hello") | | |
470
- * | on_llm_end | [model name] | | 'Hello human!' |
471
- * | on_chain_start | format_docs | | | |
472
- * | on_chain_stream | format_docs | "hello world!, goodbye world!" | | |
473
- * | on_chain_end | format_docs | | [Document(...)] | "hello world!, goodbye world!" |
474
- * | on_tool_start | some_tool | | {"x": 1, "y": "2"} | |
475
- * | on_tool_stream | some_tool | {"x": 1, "y": "2"} | | |
476
- * | on_tool_end | some_tool | | | {"x": 1, "y": "2"} |
477
- * | on_retriever_start | [retriever name] | | {"query": "hello"} | |
478
- * | on_retriever_chunk | [retriever name] | {documents: [...]} | | |
479
- * | on_retriever_end | [retriever name] | | {"query": "hello"} | {documents: [...]} |
480
- * | on_prompt_start | [template_name] | | {"question": "hello"} | |
481
- * | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) |
482
- */
483
445
  async *streamEvents(input, options, streamOptions) {
446
+ if (options.encoding === "text/event-stream") {
447
+ const stream = await this._streamEvents(input, options, streamOptions);
448
+ yield* convertToHttpEventStream(stream);
449
+ }
450
+ else {
451
+ yield* this._streamEvents(input, options, streamOptions);
452
+ }
453
+ }
454
+ async *_streamEvents(input, options, streamOptions) {
484
455
  if (options.version !== "v1") {
485
456
  throw new Error(`Only version "v1" of the events schema is currently supported.`);
486
457
  }
@@ -30,5 +30,9 @@ export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends Run
30
30
  streamEvents(input: RunInput, options: Partial<CallOptions> & {
31
31
  version: "v1";
32
32
  }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<StreamEvent>;
33
+ streamEvents(input: RunInput, options: Partial<CallOptions> & {
34
+ version: "v1";
35
+ encoding: "text/event-stream";
36
+ }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<Uint8Array>;
33
37
  }
34
38
  export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToHttpEventStream = void 0;
4
+ const stream_js_1 = require("../utils/stream.cjs");
5
+ function convertToHttpEventStream(stream) {
6
+ const encoder = new TextEncoder();
7
+ const finalStream = new ReadableStream({
8
+ async start(controller) {
9
+ for await (const chunk of stream) {
10
+ controller.enqueue(encoder.encode(`event: data\ndata: ${JSON.stringify(chunk)}\n\n`));
11
+ }
12
+ controller.enqueue(encoder.encode("event: end\n\n"));
13
+ controller.close();
14
+ },
15
+ });
16
+ return stream_js_1.IterableReadableStream.fromReadableStream(finalStream);
17
+ }
18
+ exports.convertToHttpEventStream = convertToHttpEventStream;
@@ -0,0 +1,2 @@
1
+ import { IterableReadableStream } from "../utils/stream.js";
2
+ export declare function convertToHttpEventStream(stream: AsyncGenerator): IterableReadableStream<Uint8Array>;
@@ -0,0 +1,14 @@
1
+ import { IterableReadableStream } from "../utils/stream.js";
2
+ export function convertToHttpEventStream(stream) {
3
+ const encoder = new TextEncoder();
4
+ const finalStream = new ReadableStream({
5
+ async start(controller) {
6
+ for await (const chunk of stream) {
7
+ controller.enqueue(encoder.encode(`event: data\ndata: ${JSON.stringify(chunk)}\n\n`));
8
+ }
9
+ controller.enqueue(encoder.encode("event: end\n\n"));
10
+ controller.close();
11
+ },
12
+ });
13
+ return IterableReadableStream.fromReadableStream(finalStream);
14
+ }
@@ -11,9 +11,9 @@ function _coerceToDict(value, defaultKey) {
11
11
  function stripNonAlphanumeric(input) {
12
12
  return input.replace(/[-:.]/g, "");
13
13
  }
14
- function convertToDottedOrderFormat(epoch, runId) {
15
- return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}000Z`) +
16
- runId);
14
+ function convertToDottedOrderFormat(epoch, runId, executionOrder) {
15
+ const paddedOrder = executionOrder.toFixed(0).slice(0, 3).padStart(3, "0");
16
+ return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}${paddedOrder}Z`) + runId);
17
17
  }
18
18
  class BaseTracer extends base_js_1.BaseCallbackHandler {
19
19
  constructor(_fields) {
@@ -42,7 +42,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
42
42
  parentRun.child_runs.push(childRun);
43
43
  }
44
44
  async _startTrace(run) {
45
- const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id);
45
+ const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id, run.execution_order);
46
46
  const storedRun = { ...run };
47
47
  if (storedRun.parent_run_id !== undefined) {
48
48
  const parentRun = this.runMap.get(storedRun.parent_run_id);
@@ -8,9 +8,9 @@ function _coerceToDict(value, defaultKey) {
8
8
  function stripNonAlphanumeric(input) {
9
9
  return input.replace(/[-:.]/g, "");
10
10
  }
11
- function convertToDottedOrderFormat(epoch, runId) {
12
- return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}000Z`) +
13
- runId);
11
+ function convertToDottedOrderFormat(epoch, runId, executionOrder) {
12
+ const paddedOrder = executionOrder.toFixed(0).slice(0, 3).padStart(3, "0");
13
+ return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}${paddedOrder}Z`) + runId);
14
14
  }
15
15
  export class BaseTracer extends BaseCallbackHandler {
16
16
  constructor(_fields) {
@@ -39,7 +39,7 @@ export class BaseTracer extends BaseCallbackHandler {
39
39
  parentRun.child_runs.push(childRun);
40
40
  }
41
41
  async _startTrace(run) {
42
- const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id);
42
+ const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id, run.execution_order);
43
43
  const storedRun = { ...run };
44
44
  if (storedRun.parent_run_id !== undefined) {
45
45
  const parentRun = this.runMap.get(storedRun.parent_run_id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.61",
3
+ "version": "0.1.62",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {