@ai-sdk/langchain 2.0.87 → 2.0.89

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,39 +1,45 @@
1
1
  /**
2
- * Configuration options and helper callback methods for stream lifecycle events.
2
+ * Callback options for stream lifecycle events.
3
3
  */
4
- export interface StreamCallbacks {
5
- /** `onStart`: Called once when the stream is initialized. */
4
+ export interface StreamCallbacks<TState = unknown> {
5
+ /** Called once when the stream is initialized. */
6
6
  onStart?: () => Promise<void> | void;
7
7
 
8
- /** `onFinal`: Called once when the stream is closed with the final completion message. */
9
- onFinal?: (completion: string) => Promise<void> | void;
10
-
11
- /** `onToken`: Called for each tokenized message. */
8
+ /** Called for each tokenized message. */
12
9
  onToken?: (token: string) => Promise<void> | void;
13
10
 
14
- /** `onText`: Called for each text chunk. */
11
+ /** Called for each text chunk. */
15
12
  onText?: (text: string) => Promise<void> | void;
13
+
14
+ /** Called with aggregated text when stream ends (success, error, or abort). */
15
+ onFinal?: (completion: string) => Promise<void> | void;
16
+
17
+ /**
18
+ * Called on successful completion. Receives final graph state for LangGraph
19
+ * streams (from last "values" event), undefined for other stream types.
20
+ */
21
+ onFinish?: (finalState: TState | undefined) => Promise<void> | void;
22
+
23
+ /** Called when the stream encounters an error. */
24
+ onError?: (error: Error) => Promise<void> | void;
25
+
26
+ /** Called when the stream is aborted. */
27
+ onAbort?: () => Promise<void> | void;
16
28
  }
17
29
 
18
30
  /**
19
- * Creates a transform stream that encodes input messages and invokes optional callback functions.
20
- * The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle.
21
- * - `onStart`: Called once when the stream is initialized.
22
- * - `onToken`: Called for each tokenized message.
23
- * - `onFinal`: Called once when the stream is closed with the final completion message.
31
+ * Creates a transform stream that invokes callbacks during text stream processing.
24
32
  *
25
- * This function is useful when you want to process a stream of messages and perform specific actions during the stream's lifecycle.
33
+ * Lifecycle:
34
+ * 1. `onStart` - Called once when stream initializes
35
+ * 2. `onToken` / `onText` - Called for each chunk as it flows through
36
+ * 3. `onFinal` - Called once when stream closes with aggregated text
26
37
  *
27
- * @param {StreamCallbacks} [callbacks] - An object containing the callback functions.
28
- * @return {TransformStream<string, string>} A transform stream that allows the execution of custom logic through callbacks.
38
+ * Note: This transformer only supports text-based callbacks. For LangGraph state
39
+ * callbacks (`onFinish`, `onError`, `onAbort`), use `toUIMessageStream` instead.
29
40
  *
30
- * @example
31
- * const callbacks = {
32
- * onStart: async () => console.log('Stream started'),
33
- * onToken: async (token) => console.log(`Token: ${token}`),
34
- * onFinal: async () => data.close()
35
- * };
36
- * const transformer = createCallbacksTransformer(callbacks);
41
+ * @param callbacks - Optional callback functions for stream lifecycle events.
42
+ * @returns A TransformStream that passes through messages while invoking callbacks.
37
43
  */
38
44
  export function createCallbacksTransformer(
39
45
  callbacks: StreamCallbacks | undefined = {},
package/src/utils.ts CHANGED
@@ -24,6 +24,19 @@ import {
24
24
  type ImageGenerationOutput,
25
25
  } from './types';
26
26
 
27
+ /**
28
+ * Parses a LangGraph event tuple into [type, data].
29
+ * Handles both 2-element [type, data] and 3-element [namespace, type, data] formats.
30
+ *
31
+ * @param event - The raw LangGraph event array.
32
+ * @returns A tuple of [type, data].
33
+ */
34
+ export function parseLangGraphEvent(
35
+ event: unknown[],
36
+ ): [type: unknown, data: unknown] {
37
+ return event.length === 3 ? [event[1], event[2]] : [event[0], event[1]];
38
+ }
39
+
27
40
  /**
28
41
  * Converts a ToolResultPart to a LangChain ToolMessage
29
42
  * @param block - The ToolResultPart to convert.
@@ -959,7 +972,7 @@ export function processLangGraphEvent(
959
972
  toolCallInfoByIndex,
960
973
  emittedToolCallsByKey,
961
974
  } = state;
962
- const [type, data] = event.length === 3 ? event.slice(1) : event;
975
+ const [type, data] = parseLangGraphEvent(event);
963
976
 
964
977
  switch (type) {
965
978
  case 'custom': {