@langchain/langgraph-sdk 1.4.4 → 1.4.5

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.
Files changed (196) hide show
  1. package/dist/_virtual/rolldown_runtime.cjs +29 -0
  2. package/dist/auth/error.cjs +79 -0
  3. package/dist/auth/error.cjs.map +1 -0
  4. package/dist/auth/error.d.cts +13 -0
  5. package/dist/auth/error.d.cts.map +1 -0
  6. package/dist/auth/error.d.ts +13 -0
  7. package/dist/auth/error.d.ts.map +1 -0
  8. package/dist/auth/error.js +78 -0
  9. package/dist/auth/error.js.map +1 -0
  10. package/dist/auth/index.cjs +39 -0
  11. package/dist/auth/index.cjs.map +1 -0
  12. package/dist/auth/index.d.cts +29 -0
  13. package/dist/auth/index.d.cts.map +1 -0
  14. package/dist/auth/index.d.ts +29 -0
  15. package/dist/auth/index.d.ts.map +1 -0
  16. package/dist/auth/index.js +37 -0
  17. package/dist/auth/index.js.map +1 -0
  18. package/dist/auth/types.d.cts +294 -0
  19. package/dist/auth/types.d.cts.map +1 -0
  20. package/dist/auth/types.d.ts +294 -0
  21. package/dist/auth/types.d.ts.map +1 -0
  22. package/dist/client.cjs +1210 -0
  23. package/dist/client.cjs.map +1 -0
  24. package/dist/client.d.cts +799 -0
  25. package/dist/client.d.cts.map +1 -0
  26. package/dist/client.d.ts +799 -0
  27. package/dist/client.d.ts.map +1 -0
  28. package/dist/client.js +1203 -0
  29. package/dist/client.js.map +1 -0
  30. package/dist/index.cjs +6 -0
  31. package/dist/index.d.cts +8 -0
  32. package/dist/index.d.ts +8 -0
  33. package/dist/index.js +4 -0
  34. package/dist/logging/index.cjs +35 -0
  35. package/dist/logging/index.cjs.map +1 -0
  36. package/dist/logging/index.d.cts +46 -0
  37. package/dist/logging/index.d.cts.map +1 -0
  38. package/dist/logging/index.d.ts +46 -0
  39. package/dist/logging/index.d.ts.map +1 -0
  40. package/dist/logging/index.js +34 -0
  41. package/dist/logging/index.js.map +1 -0
  42. package/dist/react/index.cjs +5 -0
  43. package/dist/react/index.d.cts +5 -0
  44. package/dist/react/index.d.ts +5 -0
  45. package/dist/react/index.js +4 -0
  46. package/dist/react/stream.cjs +18 -0
  47. package/dist/react/stream.cjs.map +1 -0
  48. package/dist/react/stream.custom.cjs +135 -0
  49. package/dist/react/stream.custom.cjs.map +1 -0
  50. package/dist/react/stream.custom.d.cts +40 -0
  51. package/dist/react/stream.custom.d.cts.map +1 -0
  52. package/dist/react/stream.custom.d.ts +40 -0
  53. package/dist/react/stream.custom.d.ts.map +1 -0
  54. package/dist/react/stream.custom.js +133 -0
  55. package/dist/react/stream.custom.js.map +1 -0
  56. package/dist/react/stream.d.cts +155 -0
  57. package/dist/react/stream.d.cts.map +1 -0
  58. package/dist/react/stream.d.ts +155 -0
  59. package/dist/react/stream.d.ts.map +1 -0
  60. package/dist/react/stream.js +17 -0
  61. package/dist/react/stream.js.map +1 -0
  62. package/dist/react/stream.lgp.cjs +448 -0
  63. package/dist/react/stream.lgp.cjs.map +1 -0
  64. package/dist/react/stream.lgp.js +447 -0
  65. package/dist/react/stream.lgp.js.map +1 -0
  66. package/dist/react/thread.cjs +21 -0
  67. package/dist/react/thread.cjs.map +1 -0
  68. package/dist/react/thread.js +20 -0
  69. package/dist/react/thread.js.map +1 -0
  70. package/dist/react/types.d.cts +152 -0
  71. package/dist/react/types.d.cts.map +1 -0
  72. package/dist/react/types.d.ts +152 -0
  73. package/dist/react/types.d.ts.map +1 -0
  74. package/dist/react-ui/client.cjs +138 -0
  75. package/dist/react-ui/client.cjs.map +1 -0
  76. package/dist/react-ui/client.d.cts +73 -0
  77. package/dist/react-ui/client.d.cts.map +1 -0
  78. package/dist/react-ui/client.d.ts +73 -0
  79. package/dist/react-ui/client.d.ts.map +1 -0
  80. package/dist/react-ui/client.js +132 -0
  81. package/dist/react-ui/client.js.map +1 -0
  82. package/dist/react-ui/index.cjs +14 -0
  83. package/dist/react-ui/index.cjs.map +1 -0
  84. package/dist/react-ui/index.d.cts +3 -0
  85. package/dist/react-ui/index.d.ts +3 -0
  86. package/dist/react-ui/index.js +9 -0
  87. package/dist/react-ui/index.js.map +1 -0
  88. package/dist/react-ui/server/index.cjs +5 -0
  89. package/dist/react-ui/server/index.d.cts +3 -0
  90. package/dist/react-ui/server/index.d.ts +3 -0
  91. package/dist/react-ui/server/index.js +4 -0
  92. package/dist/react-ui/server/server.cjs +57 -0
  93. package/dist/react-ui/server/server.cjs.map +1 -0
  94. package/dist/react-ui/server/server.d.cts +55 -0
  95. package/dist/react-ui/server/server.d.cts.map +1 -0
  96. package/dist/react-ui/server/server.d.ts +55 -0
  97. package/dist/react-ui/server/server.d.ts.map +1 -0
  98. package/dist/react-ui/server/server.js +56 -0
  99. package/dist/react-ui/server/server.js.map +1 -0
  100. package/dist/react-ui/types.cjs +38 -0
  101. package/dist/react-ui/types.cjs.map +1 -0
  102. package/dist/react-ui/types.d.cts +25 -0
  103. package/dist/react-ui/types.d.cts.map +1 -0
  104. package/dist/react-ui/types.d.ts +25 -0
  105. package/dist/react-ui/types.d.ts.map +1 -0
  106. package/dist/react-ui/types.js +35 -0
  107. package/dist/react-ui/types.js.map +1 -0
  108. package/dist/schema.d.cts +284 -0
  109. package/dist/schema.d.cts.map +1 -0
  110. package/dist/schema.d.ts +284 -0
  111. package/dist/schema.d.ts.map +1 -0
  112. package/dist/singletons/fetch.cjs +24 -0
  113. package/dist/singletons/fetch.cjs.map +1 -0
  114. package/dist/singletons/fetch.d.cts +14 -0
  115. package/dist/singletons/fetch.d.cts.map +1 -0
  116. package/dist/singletons/fetch.d.ts +14 -0
  117. package/dist/singletons/fetch.d.ts.map +1 -0
  118. package/dist/singletons/fetch.js +22 -0
  119. package/dist/singletons/fetch.js.map +1 -0
  120. package/dist/types.d.cts +187 -0
  121. package/dist/types.d.cts.map +1 -0
  122. package/dist/types.d.ts +187 -0
  123. package/dist/types.d.ts.map +1 -0
  124. package/dist/types.messages.d.cts +264 -0
  125. package/dist/types.messages.d.cts.map +1 -0
  126. package/dist/types.messages.d.ts +264 -0
  127. package/dist/types.messages.d.ts.map +1 -0
  128. package/dist/types.stream.d.cts +233 -0
  129. package/dist/types.stream.d.cts.map +1 -0
  130. package/dist/types.stream.d.ts +233 -0
  131. package/dist/types.stream.d.ts.map +1 -0
  132. package/dist/types.template.d.cts +19 -0
  133. package/dist/types.template.d.cts.map +1 -0
  134. package/dist/types.template.d.ts +19 -0
  135. package/dist/types.template.d.ts.map +1 -0
  136. package/dist/ui/branching.cjs +153 -0
  137. package/dist/ui/branching.cjs.map +1 -0
  138. package/dist/ui/branching.d.cts +19 -0
  139. package/dist/ui/branching.d.cts.map +1 -0
  140. package/dist/ui/branching.d.ts +19 -0
  141. package/dist/ui/branching.d.ts.map +1 -0
  142. package/dist/ui/branching.js +152 -0
  143. package/dist/ui/branching.js.map +1 -0
  144. package/dist/ui/errors.cjs +15 -0
  145. package/dist/ui/errors.cjs.map +1 -0
  146. package/dist/ui/errors.js +14 -0
  147. package/dist/ui/errors.js.map +1 -0
  148. package/dist/ui/manager.cjs +188 -0
  149. package/dist/ui/manager.cjs.map +1 -0
  150. package/dist/ui/manager.js +188 -0
  151. package/dist/ui/manager.js.map +1 -0
  152. package/dist/ui/messages.cjs +70 -0
  153. package/dist/ui/messages.cjs.map +1 -0
  154. package/dist/ui/messages.js +68 -0
  155. package/dist/ui/messages.js.map +1 -0
  156. package/dist/ui/types.d.cts +420 -0
  157. package/dist/ui/types.d.cts.map +1 -0
  158. package/dist/ui/types.d.ts +420 -0
  159. package/dist/ui/types.d.ts.map +1 -0
  160. package/dist/ui/utils.cjs +13 -0
  161. package/dist/ui/utils.cjs.map +1 -0
  162. package/dist/ui/utils.js +11 -0
  163. package/dist/ui/utils.js.map +1 -0
  164. package/dist/utils/async_caller.cjs +119 -0
  165. package/dist/utils/async_caller.cjs.map +1 -0
  166. package/dist/utils/async_caller.d.cts +51 -0
  167. package/dist/utils/async_caller.d.cts.map +1 -0
  168. package/dist/utils/async_caller.d.ts +51 -0
  169. package/dist/utils/async_caller.d.ts.map +1 -0
  170. package/dist/utils/async_caller.js +116 -0
  171. package/dist/utils/async_caller.js.map +1 -0
  172. package/dist/utils/env.cjs +13 -0
  173. package/dist/utils/env.cjs.map +1 -0
  174. package/dist/utils/env.js +12 -0
  175. package/dist/utils/env.js.map +1 -0
  176. package/dist/utils/error.cjs +17 -0
  177. package/dist/utils/error.cjs.map +1 -0
  178. package/dist/utils/error.js +16 -0
  179. package/dist/utils/error.js.map +1 -0
  180. package/dist/utils/signals.cjs +20 -0
  181. package/dist/utils/signals.cjs.map +1 -0
  182. package/dist/utils/signals.js +19 -0
  183. package/dist/utils/signals.js.map +1 -0
  184. package/dist/utils/sse.cjs +124 -0
  185. package/dist/utils/sse.cjs.map +1 -0
  186. package/dist/utils/sse.js +122 -0
  187. package/dist/utils/sse.js.map +1 -0
  188. package/dist/utils/stream.cjs +174 -0
  189. package/dist/utils/stream.cjs.map +1 -0
  190. package/dist/utils/stream.js +173 -0
  191. package/dist/utils/stream.js.map +1 -0
  192. package/dist/utils/tools.cjs +52 -0
  193. package/dist/utils/tools.cjs.map +1 -0
  194. package/dist/utils/tools.js +51 -0
  195. package/dist/utils/tools.js.map +1 -0
  196. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.custom.cjs","names":["BytesLineDecoder","SSEDecoder","IterableReadableStream","MessageTupleManager","StreamManager","useControllableThreadId","getToolCallsWithResults"],"sources":["../../src/react/stream.custom.tsx"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\n\"use client\";\n\nimport { useEffect, useRef, useState, useSyncExternalStore } from \"react\";\nimport { EventStreamEvent, StreamManager } from \"../ui/manager.js\";\nimport type {\n GetUpdateType,\n GetCustomEventType,\n GetInterruptType,\n GetToolCallsType,\n RunCallbackMeta,\n GetConfigurableType,\n UseStreamTransport,\n UseStreamCustomOptions,\n CustomSubmitOptions,\n} from \"../ui/types.js\";\nimport type { UseStreamCustom } from \"./types.js\";\nimport { type Message } from \"../types.messages.js\";\nimport { getToolCallsWithResults } from \"../utils/tools.js\";\nimport { MessageTupleManager } from \"../ui/messages.js\";\nimport { Interrupt } from \"../schema.js\";\nimport { BytesLineDecoder, SSEDecoder } from \"../utils/sse.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { useControllableThreadId } from \"./thread.js\";\nimport { Command } from \"../types.js\";\nimport type { BagTemplate } from \"../types.template.js\";\n\ninterface FetchStreamTransportOptions {\n /**\n * The URL of the API to use.\n */\n apiUrl: string;\n\n /**\n * Default headers to send with requests.\n */\n defaultHeaders?: HeadersInit;\n\n /**\n * Specify a custom fetch implementation.\n */\n fetch?: typeof fetch | ((...args: any[]) => any); // eslint-disable-line @typescript-eslint/no-explicit-any\n\n /**\n * Callback that is called before the request is made.\n */\n onRequest?: (\n url: string,\n init: RequestInit\n ) => Promise<RequestInit> | RequestInit;\n}\n\nexport class FetchStreamTransport<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n> implements UseStreamTransport<StateType, Bag>\n{\n constructor(private readonly options: FetchStreamTransportOptions) {}\n\n async stream(payload: {\n input: GetUpdateType<Bag, StateType> | null | undefined;\n context: GetConfigurableType<Bag> | undefined;\n command: Command | undefined;\n signal: AbortSignal;\n }): Promise<AsyncGenerator<{ id?: string; event: string; data: unknown }>> {\n const { signal, ...body } = payload;\n\n let requestInit: RequestInit = {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.options.defaultHeaders,\n },\n body: JSON.stringify(body),\n signal,\n };\n\n if (this.options.onRequest) {\n requestInit = await this.options.onRequest(\n this.options.apiUrl,\n requestInit\n );\n }\n const fetchFn = this.options.fetch ?? fetch;\n\n const response = await fetchFn(this.options.apiUrl, requestInit);\n if (!response.ok) {\n throw new Error(`Failed to stream: ${response.statusText}`);\n }\n\n const stream = (\n response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })\n )\n .pipeThrough(BytesLineDecoder())\n .pipeThrough(SSEDecoder());\n\n return IterableReadableStream.fromReadableStream(stream);\n }\n}\n\nexport function useStreamCustom<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n>(\n options: UseStreamCustomOptions<StateType, Bag>\n): UseStreamCustom<StateType, Bag> {\n type UpdateType = GetUpdateType<Bag, StateType>;\n type CustomType = GetCustomEventType<Bag>;\n type InterruptType = GetInterruptType<Bag>;\n type ConfigurableType = GetConfigurableType<Bag>;\n type ToolCallType = GetToolCallsType<StateType>;\n\n const [messageManager] = useState(() => new MessageTupleManager());\n const [stream] = useState(\n () =>\n new StreamManager<StateType, Bag>(messageManager, {\n throttle: options.throttle ?? false,\n })\n );\n\n useSyncExternalStore(\n stream.subscribe,\n stream.getSnapshot,\n stream.getSnapshot\n );\n\n const [threadId, onThreadId] = useControllableThreadId(options);\n const threadIdRef = useRef<string | null>(threadId);\n\n // Cancel the stream if thread ID has changed\n useEffect(() => {\n if (threadIdRef.current !== threadId) {\n threadIdRef.current = threadId;\n stream.clear();\n }\n }, [threadId, stream]);\n\n const getMessages = (value: StateType): Message[] => {\n const messagesKey = options.messagesKey ?? \"messages\";\n return Array.isArray(value[messagesKey])\n ? (value[messagesKey] as Message[])\n : [];\n };\n\n const setMessages = (current: StateType, messages: Message[]): StateType => {\n const messagesKey = options.messagesKey ?? \"messages\";\n return { ...current, [messagesKey]: messages };\n };\n\n const historyValues = options.initialValues ?? ({} as StateType);\n\n const stop = () => stream.stop(historyValues, { onStop: options.onStop });\n\n const submit = async (\n values: UpdateType | null | undefined,\n submitOptions?: CustomSubmitOptions<StateType, ConfigurableType>\n ) => {\n let callbackMeta: RunCallbackMeta | undefined;\n let usableThreadId = threadId;\n\n stream.setStreamValues(() => {\n if (submitOptions?.optimisticValues != null) {\n return {\n ...historyValues,\n ...(typeof submitOptions.optimisticValues === \"function\"\n ? submitOptions.optimisticValues(historyValues)\n : submitOptions.optimisticValues),\n };\n }\n\n return { ...historyValues };\n });\n\n await stream.start(\n async (signal: AbortSignal) => {\n if (!usableThreadId) {\n // generate random thread id\n usableThreadId = crypto.randomUUID();\n threadIdRef.current = usableThreadId;\n onThreadId(usableThreadId);\n }\n\n if (!usableThreadId) {\n throw new Error(\"Failed to obtain valid thread ID.\");\n }\n\n return options.transport.stream({\n input: values,\n context: submitOptions?.context,\n command: submitOptions?.command,\n signal,\n config: {\n ...submitOptions?.config,\n configurable: {\n thread_id: usableThreadId,\n ...submitOptions?.config?.configurable,\n } as unknown as GetConfigurableType<Bag>,\n },\n }) as Promise<\n AsyncGenerator<EventStreamEvent<StateType, UpdateType, CustomType>>\n >;\n },\n {\n getMessages,\n setMessages,\n\n initialValues: {} as StateType,\n callbacks: options,\n\n onSuccess: () => undefined,\n onError(error) {\n options.onError?.(error, callbackMeta);\n },\n }\n );\n };\n\n return {\n get values() {\n return stream.values ?? ({} as StateType);\n },\n\n error: stream.error,\n isLoading: stream.isLoading,\n\n stop,\n submit,\n\n get interrupt(): Interrupt<InterruptType> | undefined {\n if (\n stream.values != null &&\n \"__interrupt__\" in stream.values &&\n Array.isArray(stream.values.__interrupt__)\n ) {\n const valueInterrupts = stream.values.__interrupt__;\n if (valueInterrupts.length === 0) return { when: \"breakpoint\" };\n if (valueInterrupts.length === 1) return valueInterrupts[0];\n\n // TODO: fix the typing of interrupts if multiple interrupts are returned\n return valueInterrupts as Interrupt<InterruptType>;\n }\n\n return undefined;\n },\n\n get messages(): Message<ToolCallType>[] {\n if (!stream.values) return [];\n return getMessages(stream.values);\n },\n\n get toolCalls() {\n if (!stream.values) return [];\n const msgs = getMessages(stream.values);\n return getToolCallsWithResults<ToolCallType>(msgs);\n },\n\n getToolCalls(message) {\n if (!stream.values) return [];\n const msgs = getMessages(stream.values);\n const allToolCalls = getToolCallsWithResults<ToolCallType>(msgs);\n return allToolCalls.filter((tc) => tc.aiMessage.id === message.id);\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;AAqDA,IAAa,uBAAb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AG,QAAO,gBAAgB;AAGzD,WAAO;;;EAMX,IAAI,WAAoC;AACtC,OAAI,CAAC,OAAO,OAAQ,QAAO,EAAE;AAC7B,UAAO,YAAY,OAAO,OAAO;;EAGnC,IAAI,YAAY;AACd,OAAI,CAAC,OAAO,OAAQ,QAAO,EAAE;AAE7B,UAAOC,sCADM,YAAY,OAAO,OAAO,CACW;;EAGpD,aAAa,SAAS;AACpB,OAAI,CAAC,OAAO,OAAQ,QAAO,EAAE;AAG7B,UADqBA,sCADR,YAAY,OAAO,OAAO,CACyB,CAC5C,QAAQ,OAAO,GAAG,UAAU,OAAO,QAAQ,GAAG;;EAErE"}
@@ -0,0 +1,40 @@
1
+ import { Command } from "../types.cjs";
2
+ import { BagTemplate } from "../types.template.cjs";
3
+ import { GetConfigurableType, GetUpdateType, UseStreamTransport } from "../ui/types.cjs";
4
+
5
+ //#region src/react/stream.custom.d.ts
6
+ interface FetchStreamTransportOptions {
7
+ /**
8
+ * The URL of the API to use.
9
+ */
10
+ apiUrl: string;
11
+ /**
12
+ * Default headers to send with requests.
13
+ */
14
+ defaultHeaders?: HeadersInit;
15
+ /**
16
+ * Specify a custom fetch implementation.
17
+ */
18
+ fetch?: typeof fetch | ((...args: any[]) => any);
19
+ /**
20
+ * Callback that is called before the request is made.
21
+ */
22
+ onRequest?: (url: string, init: RequestInit) => Promise<RequestInit> | RequestInit;
23
+ }
24
+ declare class FetchStreamTransport<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> implements UseStreamTransport<StateType, Bag> {
25
+ private readonly options;
26
+ constructor(options: FetchStreamTransportOptions);
27
+ stream(payload: {
28
+ input: GetUpdateType<Bag, StateType> | null | undefined;
29
+ context: GetConfigurableType<Bag> | undefined;
30
+ command: Command | undefined;
31
+ signal: AbortSignal;
32
+ }): Promise<AsyncGenerator<{
33
+ id?: string;
34
+ event: string;
35
+ data: unknown;
36
+ }>>;
37
+ }
38
+ //#endregion
39
+ export { FetchStreamTransport };
40
+ //# sourceMappingURL=stream.custom.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.custom.d.cts","names":["GetUpdateType","GetConfigurableType","UseStreamTransport","UseStreamCustomOptions","UseStreamCustom","Command","BagTemplate","FetchStreamTransportOptions","HeadersInit","fetch","RequestInit","Promise","FetchStreamTransport","Record","StateType","Bag","AbortSignal","AsyncGenerator","useStreamCustom"],"sources":["../../src/react/stream.custom.d.ts"],"sourcesContent":["import type { GetUpdateType, GetConfigurableType, UseStreamTransport, UseStreamCustomOptions } from \"../ui/types.js\";\nimport type { UseStreamCustom } from \"./types.js\";\nimport { Command } from \"../types.js\";\nimport type { BagTemplate } from \"../types.template.js\";\ninterface FetchStreamTransportOptions {\n /**\n * The URL of the API to use.\n */\n apiUrl: string;\n /**\n * Default headers to send with requests.\n */\n defaultHeaders?: HeadersInit;\n /**\n * Specify a custom fetch implementation.\n */\n fetch?: typeof fetch | ((...args: any[]) => any);\n /**\n * Callback that is called before the request is made.\n */\n onRequest?: (url: string, init: RequestInit) => Promise<RequestInit> | RequestInit;\n}\nexport declare class FetchStreamTransport<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> implements UseStreamTransport<StateType, Bag> {\n private readonly options;\n constructor(options: FetchStreamTransportOptions);\n stream(payload: {\n input: GetUpdateType<Bag, StateType> | null | undefined;\n context: GetConfigurableType<Bag> | undefined;\n command: Command | undefined;\n signal: AbortSignal;\n }): Promise<AsyncGenerator<{\n id?: string;\n event: string;\n data: unknown;\n }>>;\n}\nexport declare function useStreamCustom<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamCustomOptions<StateType, Bag>): UseStreamCustom<StateType, Bag>;\nexport {};\n"],"mappings":";;;;;UAIUO,2BAAAA;EAAAA;;;QAYSE,EAAAA,MAAAA;;;;gBAIwDC,CAAAA,EARtDF,WAQsDE;EAAW;AAEtF;;OAA4DG,CAAAA,EAAAA,OANzCJ,KAMyCI,GAAAA,CAAAA,CAAAA,GAAAA,IAAAA,EAAAA,GAAAA,EAAAA,EAAAA,GAAAA,GAAAA,CAAAA;;;;WAAwHC,CAAAA,EAAAA,CAAAA,GAAAA,EAAAA,MAAAA,EAAAA,IAAAA,EAFhJJ,WAEgJI,EAAAA,GAFhIH,OAEgIG,CAFxHJ,WAEwHI,CAAAA,GAFzGJ,WAEyGI;;AAE3JP,cAFJK,oBAEIL,CAAAA,kBAFmCM,MAEnCN,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAF6DM,MAE7DN,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,EAAAA,YAFkGD,WAElGC,GAFgHD,WAEhHC,CAAAA,YAFwIL,kBAExIK,CAF2JO,SAE3JP,EAFsKQ,GAEtKR,CAAAA,CAAAA;mBAEIQ,OAAAA;aAAKD,CAAAA,OAAAA,EAFTP,2BAESO;QAAnBd,CAAAA,OAAAA,EAAAA;IACsBe,KAAAA,EADtBf,aACsBe,CADRA,GACQA,EADHD,SACGC,CAAAA,GAAAA,IAAAA,GAAAA,SAAAA;IAApBd,OAAAA,EAAAA,mBAAAA,CAAoBc,GAApBd,CAAAA,GAAAA,SAAAA;IACAI,OAAAA,EAAAA,OAAAA,GAAAA,SAAAA;IACDW,MAAAA,EAAAA,WAAAA;MACRL,OAAQM,CAAAA,cAAAA,CAAAA;IAARN,EAAAA,CAAAA,EAAAA,MAAAA;IARyJT,KAAAA,EAAAA,MAAAA;IAAkB,IAAA,EAAA,OAAA"}
@@ -0,0 +1,40 @@
1
+ import { Command } from "../types.js";
2
+ import { BagTemplate } from "../types.template.js";
3
+ import { GetConfigurableType, GetUpdateType, UseStreamTransport } from "../ui/types.js";
4
+
5
+ //#region src/react/stream.custom.d.ts
6
+ interface FetchStreamTransportOptions {
7
+ /**
8
+ * The URL of the API to use.
9
+ */
10
+ apiUrl: string;
11
+ /**
12
+ * Default headers to send with requests.
13
+ */
14
+ defaultHeaders?: HeadersInit;
15
+ /**
16
+ * Specify a custom fetch implementation.
17
+ */
18
+ fetch?: typeof fetch | ((...args: any[]) => any);
19
+ /**
20
+ * Callback that is called before the request is made.
21
+ */
22
+ onRequest?: (url: string, init: RequestInit) => Promise<RequestInit> | RequestInit;
23
+ }
24
+ declare class FetchStreamTransport<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> implements UseStreamTransport<StateType, Bag> {
25
+ private readonly options;
26
+ constructor(options: FetchStreamTransportOptions);
27
+ stream(payload: {
28
+ input: GetUpdateType<Bag, StateType> | null | undefined;
29
+ context: GetConfigurableType<Bag> | undefined;
30
+ command: Command | undefined;
31
+ signal: AbortSignal;
32
+ }): Promise<AsyncGenerator<{
33
+ id?: string;
34
+ event: string;
35
+ data: unknown;
36
+ }>>;
37
+ }
38
+ //#endregion
39
+ export { FetchStreamTransport };
40
+ //# sourceMappingURL=stream.custom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.custom.d.ts","names":["GetUpdateType","GetConfigurableType","UseStreamTransport","UseStreamCustomOptions","UseStreamCustom","Command","BagTemplate","FetchStreamTransportOptions","HeadersInit","fetch","RequestInit","Promise","FetchStreamTransport","Record","StateType","Bag","AbortSignal","AsyncGenerator","useStreamCustom"],"sources":["../../src/react/stream.custom.d.ts"],"sourcesContent":["import type { GetUpdateType, GetConfigurableType, UseStreamTransport, UseStreamCustomOptions } from \"../ui/types.js\";\nimport type { UseStreamCustom } from \"./types.js\";\nimport { Command } from \"../types.js\";\nimport type { BagTemplate } from \"../types.template.js\";\ninterface FetchStreamTransportOptions {\n /**\n * The URL of the API to use.\n */\n apiUrl: string;\n /**\n * Default headers to send with requests.\n */\n defaultHeaders?: HeadersInit;\n /**\n * Specify a custom fetch implementation.\n */\n fetch?: typeof fetch | ((...args: any[]) => any);\n /**\n * Callback that is called before the request is made.\n */\n onRequest?: (url: string, init: RequestInit) => Promise<RequestInit> | RequestInit;\n}\nexport declare class FetchStreamTransport<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> implements UseStreamTransport<StateType, Bag> {\n private readonly options;\n constructor(options: FetchStreamTransportOptions);\n stream(payload: {\n input: GetUpdateType<Bag, StateType> | null | undefined;\n context: GetConfigurableType<Bag> | undefined;\n command: Command | undefined;\n signal: AbortSignal;\n }): Promise<AsyncGenerator<{\n id?: string;\n event: string;\n data: unknown;\n }>>;\n}\nexport declare function useStreamCustom<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamCustomOptions<StateType, Bag>): UseStreamCustom<StateType, Bag>;\nexport {};\n"],"mappings":";;;;;UAIUO,2BAAAA;EAAAA;;;QAYSE,EAAAA,MAAAA;;;;gBAIwDC,CAAAA,EARtDF,WAQsDE;EAAW;AAEtF;;OAA4DG,CAAAA,EAAAA,OANzCJ,KAMyCI,GAAAA,CAAAA,CAAAA,GAAAA,IAAAA,EAAAA,GAAAA,EAAAA,EAAAA,GAAAA,GAAAA,CAAAA;;;;WAAwHC,CAAAA,EAAAA,CAAAA,GAAAA,EAAAA,MAAAA,EAAAA,IAAAA,EAFhJJ,WAEgJI,EAAAA,GAFhIH,OAEgIG,CAFxHJ,WAEwHI,CAAAA,GAFzGJ,WAEyGI;;AAE3JP,cAFJK,oBAEIL,CAAAA,kBAFmCM,MAEnCN,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAF6DM,MAE7DN,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,EAAAA,YAFkGD,WAElGC,GAFgHD,WAEhHC,CAAAA,YAFwIL,kBAExIK,CAF2JO,SAE3JP,EAFsKQ,GAEtKR,CAAAA,CAAAA;mBAEIQ,OAAAA;aAAKD,CAAAA,OAAAA,EAFTP,2BAESO;QAAnBd,CAAAA,OAAAA,EAAAA;IACsBe,KAAAA,EADtBf,aACsBe,CADRA,GACQA,EADHD,SACGC,CAAAA,GAAAA,IAAAA,GAAAA,SAAAA;IAApBd,OAAAA,EAAAA,mBAAAA,CAAoBc,GAApBd,CAAAA,GAAAA,SAAAA;IACAI,OAAAA,EAAAA,OAAAA,GAAAA,SAAAA;IACDW,MAAAA,EAAAA,WAAAA;MACRL,OAAQM,CAAAA,cAAAA,CAAAA;IAARN,EAAAA,CAAAA,EAAAA,MAAAA;IARyJT,KAAAA,EAAAA,MAAAA;IAAkB,IAAA,EAAA,OAAA"}
@@ -0,0 +1,133 @@
1
+ "use client";
2
+
3
+ import { BytesLineDecoder, SSEDecoder } from "../utils/sse.js";
4
+ import { IterableReadableStream } from "../utils/stream.js";
5
+ import { MessageTupleManager } from "../ui/messages.js";
6
+ import { StreamManager } from "../ui/manager.js";
7
+ import { getToolCallsWithResults } from "../utils/tools.js";
8
+ import { useControllableThreadId } from "./thread.js";
9
+ import { useEffect, useRef, useState, useSyncExternalStore } from "react";
10
+
11
+ //#region src/react/stream.custom.tsx
12
+ var FetchStreamTransport = class {
13
+ constructor(options) {
14
+ this.options = options;
15
+ }
16
+ async stream(payload) {
17
+ const { signal, ...body } = payload;
18
+ let requestInit = {
19
+ method: "POST",
20
+ headers: {
21
+ "Content-Type": "application/json",
22
+ ...this.options.defaultHeaders
23
+ },
24
+ body: JSON.stringify(body),
25
+ signal
26
+ };
27
+ if (this.options.onRequest) requestInit = await this.options.onRequest(this.options.apiUrl, requestInit);
28
+ const response = await (this.options.fetch ?? fetch)(this.options.apiUrl, requestInit);
29
+ if (!response.ok) throw new Error(`Failed to stream: ${response.statusText}`);
30
+ const stream = (response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })).pipeThrough(BytesLineDecoder()).pipeThrough(SSEDecoder());
31
+ return IterableReadableStream.fromReadableStream(stream);
32
+ }
33
+ };
34
+ function useStreamCustom(options) {
35
+ const [messageManager] = useState(() => new MessageTupleManager());
36
+ const [stream] = useState(() => new StreamManager(messageManager, { throttle: options.throttle ?? false }));
37
+ useSyncExternalStore(stream.subscribe, stream.getSnapshot, stream.getSnapshot);
38
+ const [threadId, onThreadId] = useControllableThreadId(options);
39
+ const threadIdRef = useRef(threadId);
40
+ useEffect(() => {
41
+ if (threadIdRef.current !== threadId) {
42
+ threadIdRef.current = threadId;
43
+ stream.clear();
44
+ }
45
+ }, [threadId, stream]);
46
+ const getMessages = (value) => {
47
+ const messagesKey = options.messagesKey ?? "messages";
48
+ return Array.isArray(value[messagesKey]) ? value[messagesKey] : [];
49
+ };
50
+ const setMessages = (current, messages) => {
51
+ const messagesKey = options.messagesKey ?? "messages";
52
+ return {
53
+ ...current,
54
+ [messagesKey]: messages
55
+ };
56
+ };
57
+ const historyValues = options.initialValues ?? {};
58
+ const stop = () => stream.stop(historyValues, { onStop: options.onStop });
59
+ const submit = async (values, submitOptions) => {
60
+ let callbackMeta;
61
+ let usableThreadId = threadId;
62
+ stream.setStreamValues(() => {
63
+ if (submitOptions?.optimisticValues != null) return {
64
+ ...historyValues,
65
+ ...typeof submitOptions.optimisticValues === "function" ? submitOptions.optimisticValues(historyValues) : submitOptions.optimisticValues
66
+ };
67
+ return { ...historyValues };
68
+ });
69
+ await stream.start(async (signal) => {
70
+ if (!usableThreadId) {
71
+ usableThreadId = crypto.randomUUID();
72
+ threadIdRef.current = usableThreadId;
73
+ onThreadId(usableThreadId);
74
+ }
75
+ if (!usableThreadId) throw new Error("Failed to obtain valid thread ID.");
76
+ return options.transport.stream({
77
+ input: values,
78
+ context: submitOptions?.context,
79
+ command: submitOptions?.command,
80
+ signal,
81
+ config: {
82
+ ...submitOptions?.config,
83
+ configurable: {
84
+ thread_id: usableThreadId,
85
+ ...submitOptions?.config?.configurable
86
+ }
87
+ }
88
+ });
89
+ }, {
90
+ getMessages,
91
+ setMessages,
92
+ initialValues: {},
93
+ callbacks: options,
94
+ onSuccess: () => void 0,
95
+ onError(error) {
96
+ options.onError?.(error, callbackMeta);
97
+ }
98
+ });
99
+ };
100
+ return {
101
+ get values() {
102
+ return stream.values ?? {};
103
+ },
104
+ error: stream.error,
105
+ isLoading: stream.isLoading,
106
+ stop,
107
+ submit,
108
+ get interrupt() {
109
+ if (stream.values != null && "__interrupt__" in stream.values && Array.isArray(stream.values.__interrupt__)) {
110
+ const valueInterrupts = stream.values.__interrupt__;
111
+ if (valueInterrupts.length === 0) return { when: "breakpoint" };
112
+ if (valueInterrupts.length === 1) return valueInterrupts[0];
113
+ return valueInterrupts;
114
+ }
115
+ },
116
+ get messages() {
117
+ if (!stream.values) return [];
118
+ return getMessages(stream.values);
119
+ },
120
+ get toolCalls() {
121
+ if (!stream.values) return [];
122
+ return getToolCallsWithResults(getMessages(stream.values));
123
+ },
124
+ getToolCalls(message) {
125
+ if (!stream.values) return [];
126
+ return getToolCallsWithResults(getMessages(stream.values)).filter((tc) => tc.aiMessage.id === message.id);
127
+ }
128
+ };
129
+ }
130
+
131
+ //#endregion
132
+ export { FetchStreamTransport, useStreamCustom };
133
+ //# sourceMappingURL=stream.custom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.custom.js","names":[],"sources":["../../src/react/stream.custom.tsx"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\n\"use client\";\n\nimport { useEffect, useRef, useState, useSyncExternalStore } from \"react\";\nimport { EventStreamEvent, StreamManager } from \"../ui/manager.js\";\nimport type {\n GetUpdateType,\n GetCustomEventType,\n GetInterruptType,\n GetToolCallsType,\n RunCallbackMeta,\n GetConfigurableType,\n UseStreamTransport,\n UseStreamCustomOptions,\n CustomSubmitOptions,\n} from \"../ui/types.js\";\nimport type { UseStreamCustom } from \"./types.js\";\nimport { type Message } from \"../types.messages.js\";\nimport { getToolCallsWithResults } from \"../utils/tools.js\";\nimport { MessageTupleManager } from \"../ui/messages.js\";\nimport { Interrupt } from \"../schema.js\";\nimport { BytesLineDecoder, SSEDecoder } from \"../utils/sse.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { useControllableThreadId } from \"./thread.js\";\nimport { Command } from \"../types.js\";\nimport type { BagTemplate } from \"../types.template.js\";\n\ninterface FetchStreamTransportOptions {\n /**\n * The URL of the API to use.\n */\n apiUrl: string;\n\n /**\n * Default headers to send with requests.\n */\n defaultHeaders?: HeadersInit;\n\n /**\n * Specify a custom fetch implementation.\n */\n fetch?: typeof fetch | ((...args: any[]) => any); // eslint-disable-line @typescript-eslint/no-explicit-any\n\n /**\n * Callback that is called before the request is made.\n */\n onRequest?: (\n url: string,\n init: RequestInit\n ) => Promise<RequestInit> | RequestInit;\n}\n\nexport class FetchStreamTransport<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n> implements UseStreamTransport<StateType, Bag>\n{\n constructor(private readonly options: FetchStreamTransportOptions) {}\n\n async stream(payload: {\n input: GetUpdateType<Bag, StateType> | null | undefined;\n context: GetConfigurableType<Bag> | undefined;\n command: Command | undefined;\n signal: AbortSignal;\n }): Promise<AsyncGenerator<{ id?: string; event: string; data: unknown }>> {\n const { signal, ...body } = payload;\n\n let requestInit: RequestInit = {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.options.defaultHeaders,\n },\n body: JSON.stringify(body),\n signal,\n };\n\n if (this.options.onRequest) {\n requestInit = await this.options.onRequest(\n this.options.apiUrl,\n requestInit\n );\n }\n const fetchFn = this.options.fetch ?? fetch;\n\n const response = await fetchFn(this.options.apiUrl, requestInit);\n if (!response.ok) {\n throw new Error(`Failed to stream: ${response.statusText}`);\n }\n\n const stream = (\n response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })\n )\n .pipeThrough(BytesLineDecoder())\n .pipeThrough(SSEDecoder());\n\n return IterableReadableStream.fromReadableStream(stream);\n }\n}\n\nexport function useStreamCustom<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n>(\n options: UseStreamCustomOptions<StateType, Bag>\n): UseStreamCustom<StateType, Bag> {\n type UpdateType = GetUpdateType<Bag, StateType>;\n type CustomType = GetCustomEventType<Bag>;\n type InterruptType = GetInterruptType<Bag>;\n type ConfigurableType = GetConfigurableType<Bag>;\n type ToolCallType = GetToolCallsType<StateType>;\n\n const [messageManager] = useState(() => new MessageTupleManager());\n const [stream] = useState(\n () =>\n new StreamManager<StateType, Bag>(messageManager, {\n throttle: options.throttle ?? false,\n })\n );\n\n useSyncExternalStore(\n stream.subscribe,\n stream.getSnapshot,\n stream.getSnapshot\n );\n\n const [threadId, onThreadId] = useControllableThreadId(options);\n const threadIdRef = useRef<string | null>(threadId);\n\n // Cancel the stream if thread ID has changed\n useEffect(() => {\n if (threadIdRef.current !== threadId) {\n threadIdRef.current = threadId;\n stream.clear();\n }\n }, [threadId, stream]);\n\n const getMessages = (value: StateType): Message[] => {\n const messagesKey = options.messagesKey ?? \"messages\";\n return Array.isArray(value[messagesKey])\n ? (value[messagesKey] as Message[])\n : [];\n };\n\n const setMessages = (current: StateType, messages: Message[]): StateType => {\n const messagesKey = options.messagesKey ?? \"messages\";\n return { ...current, [messagesKey]: messages };\n };\n\n const historyValues = options.initialValues ?? ({} as StateType);\n\n const stop = () => stream.stop(historyValues, { onStop: options.onStop });\n\n const submit = async (\n values: UpdateType | null | undefined,\n submitOptions?: CustomSubmitOptions<StateType, ConfigurableType>\n ) => {\n let callbackMeta: RunCallbackMeta | undefined;\n let usableThreadId = threadId;\n\n stream.setStreamValues(() => {\n if (submitOptions?.optimisticValues != null) {\n return {\n ...historyValues,\n ...(typeof submitOptions.optimisticValues === \"function\"\n ? submitOptions.optimisticValues(historyValues)\n : submitOptions.optimisticValues),\n };\n }\n\n return { ...historyValues };\n });\n\n await stream.start(\n async (signal: AbortSignal) => {\n if (!usableThreadId) {\n // generate random thread id\n usableThreadId = crypto.randomUUID();\n threadIdRef.current = usableThreadId;\n onThreadId(usableThreadId);\n }\n\n if (!usableThreadId) {\n throw new Error(\"Failed to obtain valid thread ID.\");\n }\n\n return options.transport.stream({\n input: values,\n context: submitOptions?.context,\n command: submitOptions?.command,\n signal,\n config: {\n ...submitOptions?.config,\n configurable: {\n thread_id: usableThreadId,\n ...submitOptions?.config?.configurable,\n } as unknown as GetConfigurableType<Bag>,\n },\n }) as Promise<\n AsyncGenerator<EventStreamEvent<StateType, UpdateType, CustomType>>\n >;\n },\n {\n getMessages,\n setMessages,\n\n initialValues: {} as StateType,\n callbacks: options,\n\n onSuccess: () => undefined,\n onError(error) {\n options.onError?.(error, callbackMeta);\n },\n }\n );\n };\n\n return {\n get values() {\n return stream.values ?? ({} as StateType);\n },\n\n error: stream.error,\n isLoading: stream.isLoading,\n\n stop,\n submit,\n\n get interrupt(): Interrupt<InterruptType> | undefined {\n if (\n stream.values != null &&\n \"__interrupt__\" in stream.values &&\n Array.isArray(stream.values.__interrupt__)\n ) {\n const valueInterrupts = stream.values.__interrupt__;\n if (valueInterrupts.length === 0) return { when: \"breakpoint\" };\n if (valueInterrupts.length === 1) return valueInterrupts[0];\n\n // TODO: fix the typing of interrupts if multiple interrupts are returned\n return valueInterrupts as Interrupt<InterruptType>;\n }\n\n return undefined;\n },\n\n get messages(): Message<ToolCallType>[] {\n if (!stream.values) return [];\n return getMessages(stream.values);\n },\n\n get toolCalls() {\n if (!stream.values) return [];\n const msgs = getMessages(stream.values);\n return getToolCallsWithResults<ToolCallType>(msgs);\n },\n\n getToolCalls(message) {\n if (!stream.values) return [];\n const msgs = getMessages(stream.values);\n const allToolCalls = getToolCallsWithResults<ToolCallType>(msgs);\n return allToolCalls.filter((tc) => tc.aiMessage.id === message.id);\n },\n };\n}\n"],"mappings":";;;;;;;;;;;AAqDA,IAAa,uBAAb,MAIA;CACE,YAAY,AAAiB,SAAsC;EAAtC;;CAE7B,MAAM,OAAO,SAK8D;EACzE,MAAM,EAAE,QAAQ,GAAG,SAAS;EAE5B,IAAI,cAA2B;GAC7B,QAAQ;GACR,SAAS;IACP,gBAAgB;IAChB,GAAG,KAAK,QAAQ;IACjB;GACD,MAAM,KAAK,UAAU,KAAK;GAC1B;GACD;AAED,MAAI,KAAK,QAAQ,UACf,eAAc,MAAM,KAAK,QAAQ,UAC/B,KAAK,QAAQ,QACb,YACD;EAIH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Y,OAAO,OAAO,CACyB,CAC5C,QAAQ,OAAO,GAAG,UAAU,OAAO,QAAQ,GAAG;;EAErE"}
@@ -0,0 +1,155 @@
1
+ import { Message } from "../types.messages.cjs";
2
+ import { BagTemplate } from "../types.template.cjs";
3
+ import { InferAgentToolCalls, UseStreamCustomOptions, UseStreamOptions } from "../ui/types.cjs";
4
+ import { UseStream, UseStreamCustom } from "./types.cjs";
5
+
6
+ //#region src/react/stream.d.ts
7
+
8
+ /**
9
+ * Helper type that infers StateType based on whether T is an agent-like type, a CompiledGraph/Pregel instance, or a state type.
10
+ * - If T has `~agentTypes`, returns a state with typed messages based on the agent's tools
11
+ * - If T has `~RunOutput` (CompiledGraph/CompiledStateGraph), returns the state type
12
+ * - If T has `~OutputType` (Pregel), returns the output type as state
13
+ * - Otherwise, returns T (direct state type)
14
+ */
15
+ type InferStateType<T> = T extends {
16
+ "~agentTypes": unknown;
17
+ } ? {
18
+ messages: Message<InferAgentToolCalls<T>>[];
19
+ } : T extends {
20
+ "~RunOutput": infer S;
21
+ } ? S extends Record<string, unknown> ? S : Record<string, unknown> : T extends {
22
+ "~OutputType": infer O;
23
+ } ? O extends Record<string, unknown> ? O : Record<string, unknown> : T extends Record<string, unknown> ? T : Record<string, unknown>;
24
+ /**
25
+ * Helper type that infers Bag based on whether T is an agent-like type.
26
+ * - If T has `~agentTypes`, extracts bag from the agent's tools
27
+ * - Otherwise, returns the default BagTemplate
28
+ */
29
+ type InferBag<T, B extends BagTemplate = BagTemplate> = T extends {
30
+ "~agentTypes": unknown;
31
+ } ? BagTemplate : B;
32
+ /**
33
+ * A React hook that provides seamless integration with LangGraph streaming capabilities.
34
+ *
35
+ * The `useStream` hook handles all the complexities of streaming, state management, and branching logic,
36
+ * letting you focus on building great chat experiences. It provides automatic state management for
37
+ * messages, interrupts, loading states, and errors.
38
+ *
39
+ * ## Usage with ReactAgent (recommended for createAgent users)
40
+ *
41
+ * When using `createAgent` from `@langchain/langgraph`, you can pass `typeof agent` as the
42
+ * type parameter to automatically infer tool call types:
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * // In your agent file (e.g., agent.ts)
47
+ * import { createAgent, tool } from "@langchain/langgraph";
48
+ * import { z } from "zod";
49
+ *
50
+ * const getWeather = tool(
51
+ * async ({ location }) => `Weather in ${location}`,
52
+ * { name: "get_weather", schema: z.object({ location: z.string() }) }
53
+ * );
54
+ *
55
+ * export const agent = createAgent({
56
+ * model: "openai:gpt-4o",
57
+ * tools: [getWeather],
58
+ * });
59
+ *
60
+ * // In your React component
61
+ * import { agent } from "./agent";
62
+ *
63
+ * function Chat() {
64
+ * // Tool calls are automatically typed from the agent's tools!
65
+ * const stream = useStream<typeof agent>({
66
+ * assistantId: "agent",
67
+ * apiUrl: "http://localhost:2024",
68
+ * });
69
+ *
70
+ * // stream.toolCalls[0].call.name is typed as "get_weather"
71
+ * // stream.toolCalls[0].call.args is typed as { location: string }
72
+ * }
73
+ * ```
74
+ *
75
+ * ## Usage with StateGraph (for custom LangGraph applications)
76
+ *
77
+ * When building custom graphs with `StateGraph`, embed your tool call types directly
78
+ * in your state's messages property using `Message<MyToolCalls>`:
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { Message } from "@langchain/langgraph-sdk";
83
+ *
84
+ * // Define your tool call types as a discriminated union
85
+ * type MyToolCalls =
86
+ * | { name: "search"; args: { query: string }; id?: string }
87
+ * | { name: "calculate"; args: { expression: string }; id?: string };
88
+ *
89
+ * // Embed tool call types in your state's messages
90
+ * interface MyGraphState {
91
+ * messages: Message<MyToolCalls>[];
92
+ * context?: string;
93
+ * }
94
+ *
95
+ * function Chat() {
96
+ * const stream = useStream<MyGraphState>({
97
+ * assistantId: "my-graph",
98
+ * apiUrl: "http://localhost:2024",
99
+ * });
100
+ *
101
+ * // stream.values is typed as MyGraphState
102
+ * // stream.toolCalls[0].call.name is typed as "search" | "calculate"
103
+ * }
104
+ * ```
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * // With additional type configuration (interrupts, configurable)
109
+ * interface MyGraphState {
110
+ * messages: Message<MyToolCalls>[];
111
+ * }
112
+ *
113
+ * function Chat() {
114
+ * const stream = useStream<MyGraphState, {
115
+ * InterruptType: { question: string };
116
+ * ConfigurableType: { userId: string };
117
+ * }>({
118
+ * assistantId: "my-graph",
119
+ * apiUrl: "http://localhost:2024",
120
+ * });
121
+ *
122
+ * // stream.interrupt is typed as { question: string } | undefined
123
+ * }
124
+ * ```
125
+ *
126
+ * @template T Either a ReactAgent type (with `~agentTypes`) or a state type (`Record<string, unknown>`)
127
+ * @template Bag Type configuration bag containing:
128
+ * - `ConfigurableType`: Type for the `config.configurable` property
129
+ * - `InterruptType`: Type for interrupt values
130
+ * - `CustomEventType`: Type for custom events
131
+ * - `UpdateType`: Type for the submit function updates
132
+ *
133
+ * @see {@link https://docs.langchain.com/langgraph-platform/use-stream-react | LangGraph React Integration Guide}
134
+ */
135
+ declare function useStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamOptions<InferStateType<T>, InferBag<T, Bag>>): UseStream<InferStateType<T>, InferBag<T, Bag>>;
136
+ /**
137
+ * A React hook that provides seamless integration with LangGraph streaming capabilities.
138
+ *
139
+ * The `useStream` hook handles all the complexities of streaming, state management, and branching logic,
140
+ * letting you focus on building great chat experiences. It provides automatic state management for
141
+ * messages, interrupts, loading states, and errors.
142
+ *
143
+ * @template T Either a ReactAgent type (with `~agentTypes`) or a state type (`Record<string, unknown>`)
144
+ * @template Bag Type configuration bag containing:
145
+ * - `ConfigurableType`: Type for the `config.configurable` property
146
+ * - `InterruptType`: Type for interrupt values
147
+ * - `CustomEventType`: Type for custom events
148
+ * - `UpdateType`: Type for the submit function updates
149
+ *
150
+ * @see {@link https://docs.langchain.com/langgraph-platform/use-stream-react | LangGraph React Integration Guide}
151
+ */
152
+ declare function useStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamCustomOptions<InferStateType<T>, InferBag<T, Bag>>): UseStreamCustom<InferStateType<T>, InferBag<T, Bag>>;
153
+ //#endregion
154
+ export { useStream };
155
+ //# sourceMappingURL=stream.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.cts","names":["UseStreamOptions","InferAgentToolCalls","Message","BagTemplate","UseStream","UseStreamCustom","UseStreamCustomOptions","InferStateType","T","S","Record","O","InferBag","B","useStream","Bag"],"sources":["../../src/react/stream.d.ts"],"sourcesContent":["import type { UseStreamOptions, InferAgentToolCalls } from \"../ui/types.js\";\nimport type { Message } from \"../types.messages.js\";\nimport type { BagTemplate } from \"../types.template.js\";\nimport type { UseStream, UseStreamCustom, UseStreamCustomOptions } from \"./types.js\";\n/**\n * Helper type that infers StateType based on whether T is an agent-like type, a CompiledGraph/Pregel instance, or a state type.\n * - If T has `~agentTypes`, returns a state with typed messages based on the agent's tools\n * - If T has `~RunOutput` (CompiledGraph/CompiledStateGraph), returns the state type\n * - If T has `~OutputType` (Pregel), returns the output type as state\n * - Otherwise, returns T (direct state type)\n */\ntype InferStateType<T> = T extends {\n \"~agentTypes\": unknown;\n} ? {\n messages: Message<InferAgentToolCalls<T>>[];\n} : T extends {\n \"~RunOutput\": infer S;\n} ? S extends Record<string, unknown> ? S : Record<string, unknown> : T extends {\n \"~OutputType\": infer O;\n} ? O extends Record<string, unknown> ? O : Record<string, unknown> : T extends Record<string, unknown> ? T : Record<string, unknown>;\n/**\n * Helper type that infers Bag based on whether T is an agent-like type.\n * - If T has `~agentTypes`, extracts bag from the agent's tools\n * - Otherwise, returns the default BagTemplate\n */\ntype InferBag<T, B extends BagTemplate = BagTemplate> = T extends {\n \"~agentTypes\": unknown;\n} ? BagTemplate : B;\n/**\n * A React hook that provides seamless integration with LangGraph streaming capabilities.\n *\n * The `useStream` hook handles all the complexities of streaming, state management, and branching logic,\n * letting you focus on building great chat experiences. It provides automatic state management for\n * messages, interrupts, loading states, and errors.\n *\n * ## Usage with ReactAgent (recommended for createAgent users)\n *\n * When using `createAgent` from `@langchain/langgraph`, you can pass `typeof agent` as the\n * type parameter to automatically infer tool call types:\n *\n * @example\n * ```typescript\n * // In your agent file (e.g., agent.ts)\n * import { createAgent, tool } from \"@langchain/langgraph\";\n * import { z } from \"zod\";\n *\n * const getWeather = tool(\n * async ({ location }) => `Weather in ${location}`,\n * { name: \"get_weather\", schema: z.object({ location: z.string() }) }\n * );\n *\n * export const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * tools: [getWeather],\n * });\n *\n * // In your React component\n * import { agent } from \"./agent\";\n *\n * function Chat() {\n * // Tool calls are automatically typed from the agent's tools!\n * const stream = useStream<typeof agent>({\n * assistantId: \"agent\",\n * apiUrl: \"http://localhost:2024\",\n * });\n *\n * // stream.toolCalls[0].call.name is typed as \"get_weather\"\n * // stream.toolCalls[0].call.args is typed as { location: string }\n * }\n * ```\n *\n * ## Usage with StateGraph (for custom LangGraph applications)\n *\n * When building custom graphs with `StateGraph`, embed your tool call types directly\n * in your state's messages property using `Message<MyToolCalls>`:\n *\n * @example\n * ```typescript\n * import { Message } from \"@langchain/langgraph-sdk\";\n *\n * // Define your tool call types as a discriminated union\n * type MyToolCalls =\n * | { name: \"search\"; args: { query: string }; id?: string }\n * | { name: \"calculate\"; args: { expression: string }; id?: string };\n *\n * // Embed tool call types in your state's messages\n * interface MyGraphState {\n * messages: Message<MyToolCalls>[];\n * context?: string;\n * }\n *\n * function Chat() {\n * const stream = useStream<MyGraphState>({\n * assistantId: \"my-graph\",\n * apiUrl: \"http://localhost:2024\",\n * });\n *\n * // stream.values is typed as MyGraphState\n * // stream.toolCalls[0].call.name is typed as \"search\" | \"calculate\"\n * }\n * ```\n *\n * @example\n * ```typescript\n * // With additional type configuration (interrupts, configurable)\n * interface MyGraphState {\n * messages: Message<MyToolCalls>[];\n * }\n *\n * function Chat() {\n * const stream = useStream<MyGraphState, {\n * InterruptType: { question: string };\n * ConfigurableType: { userId: string };\n * }>({\n * assistantId: \"my-graph\",\n * apiUrl: \"http://localhost:2024\",\n * });\n *\n * // stream.interrupt is typed as { question: string } | undefined\n * }\n * ```\n *\n * @template T Either a ReactAgent type (with `~agentTypes`) or a state type (`Record<string, unknown>`)\n * @template Bag Type configuration bag containing:\n * - `ConfigurableType`: Type for the `config.configurable` property\n * - `InterruptType`: Type for interrupt values\n * - `CustomEventType`: Type for custom events\n * - `UpdateType`: Type for the submit function updates\n *\n * @see {@link https://docs.langchain.com/langgraph-platform/use-stream-react | LangGraph React Integration Guide}\n */\nexport declare function useStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamOptions<InferStateType<T>, InferBag<T, Bag>>): UseStream<InferStateType<T>, InferBag<T, Bag>>;\n/**\n * A React hook that provides seamless integration with LangGraph streaming capabilities.\n *\n * The `useStream` hook handles all the complexities of streaming, state management, and branching logic,\n * letting you focus on building great chat experiences. It provides automatic state management for\n * messages, interrupts, loading states, and errors.\n *\n * @template T Either a ReactAgent type (with `~agentTypes`) or a state type (`Record<string, unknown>`)\n * @template Bag Type configuration bag containing:\n * - `ConfigurableType`: Type for the `config.configurable` property\n * - `InterruptType`: Type for interrupt values\n * - `CustomEventType`: Type for custom events\n * - `UpdateType`: Type for the submit function updates\n *\n * @see {@link https://docs.langchain.com/langgraph-platform/use-stream-react | LangGraph React Integration Guide}\n */\nexport declare function useStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamCustomOptions<InferStateType<T>, InferBag<T, Bag>>): UseStreamCustom<InferStateType<T>, InferBag<T, Bag>>;\nexport {};\n"],"mappings":";;;;;;;;;AAGqF;;;;;KAQhFO,cAGSL,CAAAA,CAAAA,CAAAA,GAHWM,CAGXN,SAAAA;eACVM,EAAAA,OAAAA;;UAEUE,EAHAR,OAGAQ,CAHQT,mBAGRS,CAH4BF,CAG5BE,CAAAA,CAAAA,EAAAA;IAFVF,UAEoCC;cAAIC,EAAAA,KAAAA,EAAAA;IAAxCD,UAAUC,MAAwDF,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAA9BC,CAA8BD,GAA1BE,MAA0BF,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAAAA,CAAAA,SAAAA;eAElEG,EAAAA,KAAAA,EAAAA;IAAAA,UAAUD,MAAAA,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAA0BC,CAA1BD,GAA8BA,MAA9BA,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAAwDF,CAAxDE,SAAkEA,MAAlEA,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAA4FF,CAA5FE,GAAgGA,MAAhGA,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA;;;;;;KAMTE,QANyGF,CAAAA,CAAAA,EAAAA,UAMnFP,WANmFO,GAMrEP,WANqEO,CAAAA,GAMtDF,CANsDE,SAAAA;EAAM,aAAA,EAAA,OAAA;AAAA,CAAA,GAQhHP,WAFS,GAEKU,CAFL;;;;;;;;AA0Gb;;;;;;;;;;;;;;;;;;AAiBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAjBwBC,cAAcJ,qCAAqCP,cAAcA,sBAAsBH,iBAAiBO,eAAeC,IAAII,SAASJ,GAAGO,QAAQX,UAAUG,eAAeC,IAAII,SAASJ,GAAGO;;;;;;;;;;;;;;;;;iBAiBxLD,cAAcJ,qCAAqCP,cAAcA,sBAAsBG,uBAAuBC,eAAeC,IAAII,SAASJ,GAAGO,QAAQV,gBAAgBE,eAAeC,IAAII,SAASJ,GAAGO"}
@@ -0,0 +1,155 @@
1
+ import { Message } from "../types.messages.js";
2
+ import { BagTemplate } from "../types.template.js";
3
+ import { InferAgentToolCalls, UseStreamCustomOptions, UseStreamOptions } from "../ui/types.js";
4
+ import { UseStream, UseStreamCustom } from "./types.js";
5
+
6
+ //#region src/react/stream.d.ts
7
+
8
+ /**
9
+ * Helper type that infers StateType based on whether T is an agent-like type, a CompiledGraph/Pregel instance, or a state type.
10
+ * - If T has `~agentTypes`, returns a state with typed messages based on the agent's tools
11
+ * - If T has `~RunOutput` (CompiledGraph/CompiledStateGraph), returns the state type
12
+ * - If T has `~OutputType` (Pregel), returns the output type as state
13
+ * - Otherwise, returns T (direct state type)
14
+ */
15
+ type InferStateType<T> = T extends {
16
+ "~agentTypes": unknown;
17
+ } ? {
18
+ messages: Message<InferAgentToolCalls<T>>[];
19
+ } : T extends {
20
+ "~RunOutput": infer S;
21
+ } ? S extends Record<string, unknown> ? S : Record<string, unknown> : T extends {
22
+ "~OutputType": infer O;
23
+ } ? O extends Record<string, unknown> ? O : Record<string, unknown> : T extends Record<string, unknown> ? T : Record<string, unknown>;
24
+ /**
25
+ * Helper type that infers Bag based on whether T is an agent-like type.
26
+ * - If T has `~agentTypes`, extracts bag from the agent's tools
27
+ * - Otherwise, returns the default BagTemplate
28
+ */
29
+ type InferBag<T, B extends BagTemplate = BagTemplate> = T extends {
30
+ "~agentTypes": unknown;
31
+ } ? BagTemplate : B;
32
+ /**
33
+ * A React hook that provides seamless integration with LangGraph streaming capabilities.
34
+ *
35
+ * The `useStream` hook handles all the complexities of streaming, state management, and branching logic,
36
+ * letting you focus on building great chat experiences. It provides automatic state management for
37
+ * messages, interrupts, loading states, and errors.
38
+ *
39
+ * ## Usage with ReactAgent (recommended for createAgent users)
40
+ *
41
+ * When using `createAgent` from `@langchain/langgraph`, you can pass `typeof agent` as the
42
+ * type parameter to automatically infer tool call types:
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * // In your agent file (e.g., agent.ts)
47
+ * import { createAgent, tool } from "@langchain/langgraph";
48
+ * import { z } from "zod";
49
+ *
50
+ * const getWeather = tool(
51
+ * async ({ location }) => `Weather in ${location}`,
52
+ * { name: "get_weather", schema: z.object({ location: z.string() }) }
53
+ * );
54
+ *
55
+ * export const agent = createAgent({
56
+ * model: "openai:gpt-4o",
57
+ * tools: [getWeather],
58
+ * });
59
+ *
60
+ * // In your React component
61
+ * import { agent } from "./agent";
62
+ *
63
+ * function Chat() {
64
+ * // Tool calls are automatically typed from the agent's tools!
65
+ * const stream = useStream<typeof agent>({
66
+ * assistantId: "agent",
67
+ * apiUrl: "http://localhost:2024",
68
+ * });
69
+ *
70
+ * // stream.toolCalls[0].call.name is typed as "get_weather"
71
+ * // stream.toolCalls[0].call.args is typed as { location: string }
72
+ * }
73
+ * ```
74
+ *
75
+ * ## Usage with StateGraph (for custom LangGraph applications)
76
+ *
77
+ * When building custom graphs with `StateGraph`, embed your tool call types directly
78
+ * in your state's messages property using `Message<MyToolCalls>`:
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { Message } from "@langchain/langgraph-sdk";
83
+ *
84
+ * // Define your tool call types as a discriminated union
85
+ * type MyToolCalls =
86
+ * | { name: "search"; args: { query: string }; id?: string }
87
+ * | { name: "calculate"; args: { expression: string }; id?: string };
88
+ *
89
+ * // Embed tool call types in your state's messages
90
+ * interface MyGraphState {
91
+ * messages: Message<MyToolCalls>[];
92
+ * context?: string;
93
+ * }
94
+ *
95
+ * function Chat() {
96
+ * const stream = useStream<MyGraphState>({
97
+ * assistantId: "my-graph",
98
+ * apiUrl: "http://localhost:2024",
99
+ * });
100
+ *
101
+ * // stream.values is typed as MyGraphState
102
+ * // stream.toolCalls[0].call.name is typed as "search" | "calculate"
103
+ * }
104
+ * ```
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * // With additional type configuration (interrupts, configurable)
109
+ * interface MyGraphState {
110
+ * messages: Message<MyToolCalls>[];
111
+ * }
112
+ *
113
+ * function Chat() {
114
+ * const stream = useStream<MyGraphState, {
115
+ * InterruptType: { question: string };
116
+ * ConfigurableType: { userId: string };
117
+ * }>({
118
+ * assistantId: "my-graph",
119
+ * apiUrl: "http://localhost:2024",
120
+ * });
121
+ *
122
+ * // stream.interrupt is typed as { question: string } | undefined
123
+ * }
124
+ * ```
125
+ *
126
+ * @template T Either a ReactAgent type (with `~agentTypes`) or a state type (`Record<string, unknown>`)
127
+ * @template Bag Type configuration bag containing:
128
+ * - `ConfigurableType`: Type for the `config.configurable` property
129
+ * - `InterruptType`: Type for interrupt values
130
+ * - `CustomEventType`: Type for custom events
131
+ * - `UpdateType`: Type for the submit function updates
132
+ *
133
+ * @see {@link https://docs.langchain.com/langgraph-platform/use-stream-react | LangGraph React Integration Guide}
134
+ */
135
+ declare function useStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamOptions<InferStateType<T>, InferBag<T, Bag>>): UseStream<InferStateType<T>, InferBag<T, Bag>>;
136
+ /**
137
+ * A React hook that provides seamless integration with LangGraph streaming capabilities.
138
+ *
139
+ * The `useStream` hook handles all the complexities of streaming, state management, and branching logic,
140
+ * letting you focus on building great chat experiences. It provides automatic state management for
141
+ * messages, interrupts, loading states, and errors.
142
+ *
143
+ * @template T Either a ReactAgent type (with `~agentTypes`) or a state type (`Record<string, unknown>`)
144
+ * @template Bag Type configuration bag containing:
145
+ * - `ConfigurableType`: Type for the `config.configurable` property
146
+ * - `InterruptType`: Type for interrupt values
147
+ * - `CustomEventType`: Type for custom events
148
+ * - `UpdateType`: Type for the submit function updates
149
+ *
150
+ * @see {@link https://docs.langchain.com/langgraph-platform/use-stream-react | LangGraph React Integration Guide}
151
+ */
152
+ declare function useStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamCustomOptions<InferStateType<T>, InferBag<T, Bag>>): UseStreamCustom<InferStateType<T>, InferBag<T, Bag>>;
153
+ //#endregion
154
+ export { useStream };
155
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","names":["UseStreamOptions","InferAgentToolCalls","Message","BagTemplate","UseStream","UseStreamCustom","UseStreamCustomOptions","InferStateType","T","S","Record","O","InferBag","B","useStream","Bag"],"sources":["../../src/react/stream.d.ts"],"sourcesContent":["import type { UseStreamOptions, InferAgentToolCalls } from \"../ui/types.js\";\nimport type { Message } from \"../types.messages.js\";\nimport type { BagTemplate } from \"../types.template.js\";\nimport type { UseStream, UseStreamCustom, UseStreamCustomOptions } from \"./types.js\";\n/**\n * Helper type that infers StateType based on whether T is an agent-like type, a CompiledGraph/Pregel instance, or a state type.\n * - If T has `~agentTypes`, returns a state with typed messages based on the agent's tools\n * - If T has `~RunOutput` (CompiledGraph/CompiledStateGraph), returns the state type\n * - If T has `~OutputType` (Pregel), returns the output type as state\n * - Otherwise, returns T (direct state type)\n */\ntype InferStateType<T> = T extends {\n \"~agentTypes\": unknown;\n} ? {\n messages: Message<InferAgentToolCalls<T>>[];\n} : T extends {\n \"~RunOutput\": infer S;\n} ? S extends Record<string, unknown> ? S : Record<string, unknown> : T extends {\n \"~OutputType\": infer O;\n} ? O extends Record<string, unknown> ? O : Record<string, unknown> : T extends Record<string, unknown> ? T : Record<string, unknown>;\n/**\n * Helper type that infers Bag based on whether T is an agent-like type.\n * - If T has `~agentTypes`, extracts bag from the agent's tools\n * - Otherwise, returns the default BagTemplate\n */\ntype InferBag<T, B extends BagTemplate = BagTemplate> = T extends {\n \"~agentTypes\": unknown;\n} ? BagTemplate : B;\n/**\n * A React hook that provides seamless integration with LangGraph streaming capabilities.\n *\n * The `useStream` hook handles all the complexities of streaming, state management, and branching logic,\n * letting you focus on building great chat experiences. It provides automatic state management for\n * messages, interrupts, loading states, and errors.\n *\n * ## Usage with ReactAgent (recommended for createAgent users)\n *\n * When using `createAgent` from `@langchain/langgraph`, you can pass `typeof agent` as the\n * type parameter to automatically infer tool call types:\n *\n * @example\n * ```typescript\n * // In your agent file (e.g., agent.ts)\n * import { createAgent, tool } from \"@langchain/langgraph\";\n * import { z } from \"zod\";\n *\n * const getWeather = tool(\n * async ({ location }) => `Weather in ${location}`,\n * { name: \"get_weather\", schema: z.object({ location: z.string() }) }\n * );\n *\n * export const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * tools: [getWeather],\n * });\n *\n * // In your React component\n * import { agent } from \"./agent\";\n *\n * function Chat() {\n * // Tool calls are automatically typed from the agent's tools!\n * const stream = useStream<typeof agent>({\n * assistantId: \"agent\",\n * apiUrl: \"http://localhost:2024\",\n * });\n *\n * // stream.toolCalls[0].call.name is typed as \"get_weather\"\n * // stream.toolCalls[0].call.args is typed as { location: string }\n * }\n * ```\n *\n * ## Usage with StateGraph (for custom LangGraph applications)\n *\n * When building custom graphs with `StateGraph`, embed your tool call types directly\n * in your state's messages property using `Message<MyToolCalls>`:\n *\n * @example\n * ```typescript\n * import { Message } from \"@langchain/langgraph-sdk\";\n *\n * // Define your tool call types as a discriminated union\n * type MyToolCalls =\n * | { name: \"search\"; args: { query: string }; id?: string }\n * | { name: \"calculate\"; args: { expression: string }; id?: string };\n *\n * // Embed tool call types in your state's messages\n * interface MyGraphState {\n * messages: Message<MyToolCalls>[];\n * context?: string;\n * }\n *\n * function Chat() {\n * const stream = useStream<MyGraphState>({\n * assistantId: \"my-graph\",\n * apiUrl: \"http://localhost:2024\",\n * });\n *\n * // stream.values is typed as MyGraphState\n * // stream.toolCalls[0].call.name is typed as \"search\" | \"calculate\"\n * }\n * ```\n *\n * @example\n * ```typescript\n * // With additional type configuration (interrupts, configurable)\n * interface MyGraphState {\n * messages: Message<MyToolCalls>[];\n * }\n *\n * function Chat() {\n * const stream = useStream<MyGraphState, {\n * InterruptType: { question: string };\n * ConfigurableType: { userId: string };\n * }>({\n * assistantId: \"my-graph\",\n * apiUrl: \"http://localhost:2024\",\n * });\n *\n * // stream.interrupt is typed as { question: string } | undefined\n * }\n * ```\n *\n * @template T Either a ReactAgent type (with `~agentTypes`) or a state type (`Record<string, unknown>`)\n * @template Bag Type configuration bag containing:\n * - `ConfigurableType`: Type for the `config.configurable` property\n * - `InterruptType`: Type for interrupt values\n * - `CustomEventType`: Type for custom events\n * - `UpdateType`: Type for the submit function updates\n *\n * @see {@link https://docs.langchain.com/langgraph-platform/use-stream-react | LangGraph React Integration Guide}\n */\nexport declare function useStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamOptions<InferStateType<T>, InferBag<T, Bag>>): UseStream<InferStateType<T>, InferBag<T, Bag>>;\n/**\n * A React hook that provides seamless integration with LangGraph streaming capabilities.\n *\n * The `useStream` hook handles all the complexities of streaming, state management, and branching logic,\n * letting you focus on building great chat experiences. It provides automatic state management for\n * messages, interrupts, loading states, and errors.\n *\n * @template T Either a ReactAgent type (with `~agentTypes`) or a state type (`Record<string, unknown>`)\n * @template Bag Type configuration bag containing:\n * - `ConfigurableType`: Type for the `config.configurable` property\n * - `InterruptType`: Type for interrupt values\n * - `CustomEventType`: Type for custom events\n * - `UpdateType`: Type for the submit function updates\n *\n * @see {@link https://docs.langchain.com/langgraph-platform/use-stream-react | LangGraph React Integration Guide}\n */\nexport declare function useStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseStreamCustomOptions<InferStateType<T>, InferBag<T, Bag>>): UseStreamCustom<InferStateType<T>, InferBag<T, Bag>>;\nexport {};\n"],"mappings":";;;;;;;;;AAGqF;;;;;KAQhFO,cAGSL,CAAAA,CAAAA,CAAAA,GAHWM,CAGXN,SAAAA;eACVM,EAAAA,OAAAA;;UAEUE,EAHAR,OAGAQ,CAHQT,mBAGRS,CAH4BF,CAG5BE,CAAAA,CAAAA,EAAAA;IAFVF,UAEoCC;cAAIC,EAAAA,KAAAA,EAAAA;IAAxCD,UAAUC,MAAwDF,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAA9BC,CAA8BD,GAA1BE,MAA0BF,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAAAA,CAAAA,SAAAA;eAElEG,EAAAA,KAAAA,EAAAA;IAAAA,UAAUD,MAAAA,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAA0BC,CAA1BD,GAA8BA,MAA9BA,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAAwDF,CAAxDE,SAAkEA,MAAlEA,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA,GAA4FF,CAA5FE,GAAgGA,MAAhGA,CAAAA,MAAAA,EAAAA,OAAAA,CAAAA;;;;;;KAMTE,QANyGF,CAAAA,CAAAA,EAAAA,UAMnFP,WANmFO,GAMrEP,WANqEO,CAAAA,GAMtDF,CANsDE,SAAAA;EAAM,aAAA,EAAA,OAAA;AAAA,CAAA,GAQhHP,WAFS,GAEKU,CAFL;;;;;;;;AA0Gb;;;;;;;;;;;;;;;;;;AAiBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAjBwBC,cAAcJ,qCAAqCP,cAAcA,sBAAsBH,iBAAiBO,eAAeC,IAAII,SAASJ,GAAGO,QAAQX,UAAUG,eAAeC,IAAII,SAASJ,GAAGO;;;;;;;;;;;;;;;;;iBAiBxLD,cAAcJ,qCAAqCP,cAAcA,sBAAsBG,uBAAuBC,eAAeC,IAAII,SAASJ,GAAGO,QAAQV,gBAAgBE,eAAeC,IAAII,SAASJ,GAAGO"}
@@ -0,0 +1,17 @@
1
+ import { useStreamLGP } from "./stream.lgp.js";
2
+ import { useStreamCustom } from "./stream.custom.js";
3
+ import { useState } from "react";
4
+
5
+ //#region src/react/stream.tsx
6
+ function isCustomOptions(options) {
7
+ return "transport" in options;
8
+ }
9
+ function useStream(options) {
10
+ const [isCustom] = useState(isCustomOptions(options));
11
+ if (isCustom) return useStreamCustom(options);
12
+ return useStreamLGP(options);
13
+ }
14
+
15
+ //#endregion
16
+ export { useStream };
17
+ //# sourceMappingURL=stream.js.map