@langchain/react 0.2.0 → 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.
- package/README.md +22 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/stream.cjs +5 -3
- package/dist/stream.cjs.map +1 -1
- package/dist/stream.custom.cjs +1 -1
- package/dist/stream.custom.cjs.map +1 -1
- package/dist/stream.custom.js +2 -2
- package/dist/stream.custom.js.map +1 -1
- package/dist/stream.d.cts +3 -47
- package/dist/stream.d.cts.map +1 -1
- package/dist/stream.d.ts +3 -47
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +5 -3
- package/dist/stream.js.map +1 -1
- package/dist/stream.lgp.cjs +2 -2
- package/dist/stream.lgp.cjs.map +1 -1
- package/dist/stream.lgp.js +3 -3
- package/dist/stream.lgp.js.map +1 -1
- package/dist/suspense-stream.cjs +32 -16
- package/dist/suspense-stream.cjs.map +1 -1
- package/dist/suspense-stream.d.cts +23 -4
- package/dist/suspense-stream.d.cts.map +1 -1
- package/dist/suspense-stream.d.ts +23 -4
- package/dist/suspense-stream.d.ts.map +1 -1
- package/dist/suspense-stream.js +32 -17
- package/dist/suspense-stream.js.map +1 -1
- package/dist/types.d.cts +3 -3
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suspense-stream.cjs","names":["Client","useStreamLGP"],"sources":["../src/suspense-stream.tsx"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\n\"use client\";\n\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport type { ThreadState, BagTemplate } from \"@langchain/langgraph-sdk\";\nimport { Client, getClientConfigHash } from \"@langchain/langgraph-sdk/client\";\nimport type {\n UseStreamThread,\n ResolveStreamOptions,\n ResolveStreamInterface,\n InferBag,\n} from \"@langchain/langgraph-sdk/ui\";\nimport { useStreamLGP } from \"./stream.lgp.js\";\nimport type { WithClassMessages } from \"./stream.js\";\n\n// ---------------------------------------------------------------------------\n// Suspense cache\n// ---------------------------------------------------------------------------\n\ntype SuspenseCacheEntry<T> =\n | { status: \"pending\"; promise: Promise<void> }\n | { status: \"resolved\"; data: T }\n | { status: \"rejected\"; error: unknown };\n\nconst threadCache = new Map<string, SuspenseCacheEntry<unknown>>();\n\nfunction getCacheKey(\n client: Client,\n threadId: string,\n limit: boolean | number,\n): string {\n return `suspense:${getClientConfigHash(client)}:${threadId}:${limit}`;\n}\n\nfunction fetchThreadHistory<StateType extends Record<string, unknown>>(\n client: Client,\n threadId: string,\n options?: { limit?: boolean | number },\n): Promise<ThreadState<StateType>[]> {\n if (options?.limit === false) {\n return client.threads.getState<StateType>(threadId).then((state) => {\n if (state.checkpoint == null) return [];\n return [state];\n });\n }\n\n const limit = typeof options?.limit === \"number\" ? options.limit : 10;\n return client.threads.getHistory<StateType>(threadId, { limit });\n}\n\nfunction getOrCreateCacheEntry<StateType extends Record<string, unknown>>(\n client: Client,\n threadId: string,\n limit: boolean | number,\n): SuspenseCacheEntry<ThreadState<StateType>[]> {\n const key = getCacheKey(client, threadId, limit);\n let entry = threadCache.get(key) as\n | SuspenseCacheEntry<ThreadState<StateType>[]>\n | undefined;\n\n if (!entry) {\n // Start fetch. The promise always resolves (never rejects) so React\n // Suspense correctly waits for it and then retries the render.\n const promise = fetchThreadHistory<StateType>(client, threadId, { limit })\n .then((data) => {\n threadCache.set(key, { status: \"resolved\", data });\n })\n .catch((error: unknown) => {\n threadCache.set(key, { status: \"rejected\", error });\n });\n\n entry = { status: \"pending\", promise };\n threadCache.set(key, entry);\n }\n\n return entry;\n}\n\n/**\n * Clear the internal Suspense cache used by {@link useSuspenseStream}.\n *\n * Call this from an Error Boundary's `onReset` callback so that a retry\n * triggers a fresh thread-history fetch rather than re-throwing the\n * cached error.\n *\n * @example\n * ```tsx\n * <ErrorBoundary\n * onReset={() => invalidateSuspenseCache()}\n * fallbackRender={({ resetErrorBoundary }) => (\n * <button onClick={resetErrorBoundary}>Retry</button>\n * )}\n * >\n * <Suspense fallback={<Spinner />}>\n * <Chat />\n * </Suspense>\n * </ErrorBoundary>\n * ```\n */\nexport function invalidateSuspenseCache(): void {\n threadCache.clear();\n}\n\n// ---------------------------------------------------------------------------\n// Return-type helper\n// ---------------------------------------------------------------------------\n\ntype WithSuspense<T> = Omit<T, \"isLoading\" | \"error\" | \"isThreadLoading\"> & {\n isStreaming: boolean;\n};\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * A Suspense-compatible variant of {@link useStream} for LangGraph Platform.\n *\n * `useSuspenseStream` suspends the component while the initial thread\n * history is being fetched and throws errors to the nearest React Error\n * Boundary. During active streaming the component stays rendered and\n * `isStreaming` indicates whether tokens are arriving.\n *\n * @example\n * ```tsx\n * <ErrorBoundary fallback={<ErrorDisplay />}>\n * <Suspense fallback={<Spinner />}>\n * <Chat />\n * </Suspense>\n * </ErrorBoundary>\n *\n * function Chat() {\n * const { messages, submit, isStreaming } = useSuspenseStream({\n * assistantId: \"agent\",\n * apiUrl: \"http://localhost:2024\",\n * });\n * return <MessageList messages={messages} />;\n * }\n * ```\n *\n * @template T - Either a ReactAgent / DeepAgent type or a state record type.\n * @template Bag - Type configuration bag (ConfigurableType, InterruptType, …).\n */\nexport function useSuspenseStream<\n T = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate,\n>(\n options: ResolveStreamOptions<T, InferBag<T, Bag>>,\n): WithClassMessages<WithSuspense<ResolveStreamInterface<T, InferBag<T, Bag>>>>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function useSuspenseStream(options: any): any {\n type StateType = Record<string, unknown>;\n\n // ---- client (needed before useStreamLGP for cache key derivation) ----\n const client = useMemo(\n () =>\n options.client ??\n new Client({\n apiUrl: options.apiUrl,\n apiKey: options.apiKey,\n callerOptions: options.callerOptions,\n defaultHeaders: options.defaultHeaders,\n }),\n [\n options.client,\n options.apiKey,\n options.apiUrl,\n options.callerOptions,\n options.defaultHeaders,\n ],\n );\n\n const { threadId } = options;\n\n const historyLimit: boolean | number =\n typeof options.fetchStateHistory === \"object\" &&\n options.fetchStateHistory != null\n ? (options.fetchStateHistory.limit ?? false)\n : (options.fetchStateHistory ?? false);\n\n // Only manage history via the suspense cache when the caller hasn't\n // supplied an external `thread` and there's a threadId to load.\n const needsHistoryFetch = threadId != null && options.thread == null;\n\n // ---- suspense cache lookup (synchronous, may create fetch) ----\n let cacheEntry: SuspenseCacheEntry<ThreadState<StateType>[]> | undefined;\n\n if (needsHistoryFetch) {\n cacheEntry = getOrCreateCacheEntry<StateType>(\n client,\n threadId,\n historyLimit,\n );\n }\n\n const cachedData =\n cacheEntry?.status === \"resolved\" ? cacheEntry.data : undefined;\n\n // ---- mutable ref so `mutate` always writes the freshest data ----\n const cachedDataRef = useRef(cachedData);\n if (cachedData != null) {\n cachedDataRef.current = cachedData;\n }\n\n // Re-render trigger after external mutate calls.\n const [, setMutateVersion] = useState(0);\n\n const mutate = useCallback(\n async (\n mutateId?: string,\n ): Promise<ThreadState<StateType>[] | null | undefined> => {\n const fetchId = mutateId ?? threadId;\n if (!fetchId) return undefined;\n try {\n const data = await fetchThreadHistory<StateType>(client, fetchId, {\n limit: historyLimit,\n });\n const key = getCacheKey(client, fetchId, historyLimit);\n threadCache.set(key, { status: \"resolved\", data });\n cachedDataRef.current = data;\n setMutateVersion((v) => v + 1);\n return data;\n } catch {\n return undefined;\n }\n },\n [client, threadId, historyLimit],\n );\n\n // ---- build thread override for useStreamLGP ----\n const thread: UseStreamThread<StateType> | undefined = useMemo(() => {\n if (!needsHistoryFetch) return options.thread;\n return {\n data: cachedDataRef.current,\n error: undefined,\n isLoading: false,\n mutate,\n };\n // `cachedData` is included so the memo recomputes when the cache\n // transitions from pending → resolved across suspend/retry cycles.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [needsHistoryFetch, options.thread, cachedData, mutate]);\n\n // ---- delegate to useStreamLGP (must always run – Rules of Hooks) ----\n const stream = useStreamLGP({\n ...options,\n client,\n thread,\n });\n\n // ---- post-hook: suspend or throw ----\n\n // Suspend while thread history is loading, but only when the stream\n // itself is idle. If an active stream is running (e.g. the thread was\n // just created during submit), suspending would discard the stream\n // state, so we skip it.\n if (needsHistoryFetch && cacheEntry && !stream.isLoading) {\n if (cacheEntry.status === \"pending\") {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw cacheEntry.promise;\n }\n if (cacheEntry.status === \"rejected\") {\n // Clear cache so a subsequent retry (ErrorBoundary reset) starts\n // a fresh fetch instead of re-throwing the stale error.\n const key = getCacheKey(client, threadId!, historyLimit);\n threadCache.delete(key);\n // eslint-disable-next-line no-instanceof/no-instanceof\n throw cacheEntry.error instanceof Error\n ? cacheEntry.error\n : new Error(String(cacheEntry.error));\n }\n }\n\n // Throw non-streaming errors to the nearest Error Boundary.\n if (stream.error != null && !stream.isLoading) {\n // eslint-disable-next-line no-instanceof/no-instanceof\n throw stream.error instanceof Error\n ? stream.error\n : new Error(String(stream.error));\n }\n\n // Build return object explicitly to avoid triggering throwing getters\n // (e.g. `history` throws when `fetchStateHistory` is not set).\n return {\n get values() {\n return stream.values;\n },\n get messages() {\n return stream.messages;\n },\n get toolCalls() {\n return stream.toolCalls;\n },\n get toolProgress() {\n return stream.toolProgress;\n },\n getToolCalls: stream.getToolCalls.bind(stream),\n get interrupt() {\n return stream.interrupt;\n },\n get interrupts() {\n return stream.interrupts;\n },\n get subagents() {\n return stream.subagents;\n },\n get activeSubagents() {\n return stream.activeSubagents;\n },\n getSubagent: stream.getSubagent.bind(stream),\n getSubagentsByType: stream.getSubagentsByType.bind(stream),\n getSubagentsByMessage: stream.getSubagentsByMessage.bind(stream),\n getMessagesMetadata: stream.getMessagesMetadata.bind(stream),\n get history() {\n return stream.history;\n },\n get experimental_branchTree() {\n return stream.experimental_branchTree;\n },\n stop: stream.stop,\n submit: stream.submit,\n switchThread: stream.switchThread,\n joinStream: stream.joinStream,\n branch: stream.branch,\n setBranch: stream.setBranch,\n client: stream.client,\n assistantId: stream.assistantId,\n queue: stream.queue,\n isStreaming: stream.isLoading,\n };\n}\n"],"mappings":";;;;;AAyBA,MAAM,8BAAc,IAAI,KAA0C;AAElE,SAAS,YACP,QACA,UACA,OACQ;AACR,QAAO,aAAA,GAAA,gCAAA,qBAAgC,OAAO,CAAC,GAAG,SAAS,GAAG;;AAGhE,SAAS,mBACP,QACA,UACA,SACmC;AACnC,KAAI,SAAS,UAAU,MACrB,QAAO,OAAO,QAAQ,SAAoB,SAAS,CAAC,MAAM,UAAU;AAClE,MAAI,MAAM,cAAc,KAAM,QAAO,EAAE;AACvC,SAAO,CAAC,MAAM;GACd;CAGJ,MAAM,QAAQ,OAAO,SAAS,UAAU,WAAW,QAAQ,QAAQ;AACnE,QAAO,OAAO,QAAQ,WAAsB,UAAU,EAAE,OAAO,CAAC;;AAGlE,SAAS,sBACP,QACA,UACA,OAC8C;CAC9C,MAAM,MAAM,YAAY,QAAQ,UAAU,MAAM;CAChD,IAAI,QAAQ,YAAY,IAAI,IAAI;AAIhC,KAAI,CAAC,OAAO;AAWV,UAAQ;GAAE,QAAQ;GAAW,SARb,mBAA8B,QAAQ,UAAU,EAAE,OAAO,CAAC,CACvE,MAAM,SAAS;AACd,gBAAY,IAAI,KAAK;KAAE,QAAQ;KAAY;KAAM,CAAC;KAClD,CACD,OAAO,UAAmB;AACzB,gBAAY,IAAI,KAAK;KAAE,QAAQ;KAAY;KAAO,CAAC;KACnD;GAEkC;AACtC,cAAY,IAAI,KAAK,MAAM;;AAG7B,QAAO;;;;;;;;;;;;;;;;;;;;;;;AAwBT,SAAgB,0BAAgC;AAC9C,aAAY,OAAO;;AAmDrB,SAAgB,kBAAkB,S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OAAO;EACpB,OAAO,OAAO;EACd,aAAa,OAAO;EACrB"}
|
|
1
|
+
{"version":3,"file":"suspense-stream.cjs","names":["Client","useStreamLGP"],"sources":["../src/suspense-stream.tsx"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\n\"use client\";\n\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport type { ThreadState, BagTemplate } from \"@langchain/langgraph-sdk\";\nimport { Client, getClientConfigHash } from \"@langchain/langgraph-sdk/client\";\nimport type {\n UseStreamThread,\n ResolveStreamOptions,\n ResolveStreamInterface,\n InferBag,\n} from \"@langchain/langgraph-sdk/ui\";\nimport { useStreamLGP } from \"./stream.lgp.js\";\nimport type { WithClassMessages } from \"./stream.js\";\n\n// ---------------------------------------------------------------------------\n// Suspense cache\n// ---------------------------------------------------------------------------\n\nexport type SuspenseCacheEntry<T> =\n | { status: \"pending\"; promise: Promise<void> }\n | { status: \"resolved\"; data: T }\n | { status: \"rejected\"; error: unknown };\n\nexport type SuspenseCache = Map<string, SuspenseCacheEntry<unknown>>;\n\nconst defaultSuspenseCache: SuspenseCache = new Map();\n\nexport function createSuspenseCache(): SuspenseCache {\n return new Map();\n}\n\nfunction getCacheKey(\n client: Client,\n threadId: string,\n limit: boolean | number,\n): string {\n return `suspense:${getClientConfigHash(client)}:${threadId}:${limit}`;\n}\n\nfunction fetchThreadHistory<StateType extends Record<string, unknown>>(\n client: Client,\n threadId: string,\n options?: { limit?: boolean | number },\n): Promise<ThreadState<StateType>[]> {\n if (options?.limit === false) {\n return client.threads.getState<StateType>(threadId).then((state) => {\n if (state.checkpoint == null) return [];\n return [state];\n });\n }\n\n const limit = typeof options?.limit === \"number\" ? options.limit : 10;\n return client.threads.getHistory<StateType>(threadId, { limit });\n}\n\nfunction getOrCreateCacheEntry<StateType extends Record<string, unknown>>(\n cache: SuspenseCache,\n client: Client,\n threadId: string,\n limit: boolean | number,\n): SuspenseCacheEntry<ThreadState<StateType>[]> {\n const key = getCacheKey(client, threadId, limit);\n let entry = cache.get(key) as\n | SuspenseCacheEntry<ThreadState<StateType>[]>\n | undefined;\n\n if (!entry) {\n // Start fetch. The promise always resolves (never rejects) so React\n // Suspense correctly waits for it and then retries the render.\n const promise = fetchThreadHistory<StateType>(client, threadId, { limit })\n .then((data) => {\n cache.set(key, { status: \"resolved\", data });\n })\n .catch((error: unknown) => {\n cache.set(key, { status: \"rejected\", error });\n });\n\n entry = { status: \"pending\", promise };\n cache.set(key, entry);\n }\n\n return entry;\n}\n\n/**\n * Clear the internal Suspense cache used by {@link useSuspenseStream}.\n *\n * Call this from an Error Boundary's `onReset` callback so that a retry\n * triggers a fresh thread-history fetch rather than re-throwing the\n * cached error.\n *\n * @example\n * ```tsx\n * <ErrorBoundary\n * onReset={() => invalidateSuspenseCache()}\n * fallbackRender={({ resetErrorBoundary }) => (\n * <button onClick={resetErrorBoundary}>Retry</button>\n * )}\n * >\n * <Suspense fallback={<Spinner />}>\n * <Chat />\n * </Suspense>\n * </ErrorBoundary>\n * ```\n */\nexport function invalidateSuspenseCache(\n cache: SuspenseCache = defaultSuspenseCache,\n): void {\n cache.clear();\n}\n\n// ---------------------------------------------------------------------------\n// Return-type helper\n// ---------------------------------------------------------------------------\n\ntype WithSuspense<T> = Omit<T, \"isLoading\" | \"error\" | \"isThreadLoading\"> & {\n isStreaming: boolean;\n};\n\ntype UseSuspenseStreamOptions<\n T = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate,\n> = ResolveStreamOptions<T, InferBag<T, Bag>> & {\n /**\n * Optional cache store used by Suspense history prefetching.\n * Provide a custom cache in tests to avoid cross-test cache sharing.\n */\n suspenseCache?: SuspenseCache;\n};\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * A Suspense-compatible variant of {@link useStream} for LangGraph Platform.\n *\n * `useSuspenseStream` suspends the component while the initial thread\n * history is being fetched and throws errors to the nearest React Error\n * Boundary. During active streaming the component stays rendered and\n * `isStreaming` indicates whether tokens are arriving.\n *\n * @example\n * ```tsx\n * <ErrorBoundary fallback={<ErrorDisplay />}>\n * <Suspense fallback={<Spinner />}>\n * <Chat />\n * </Suspense>\n * </ErrorBoundary>\n *\n * function Chat() {\n * const { messages, submit, isStreaming } = useSuspenseStream({\n * assistantId: \"agent\",\n * apiUrl: \"http://localhost:2024\",\n * });\n * return <MessageList messages={messages} />;\n * }\n * ```\n *\n * @template T - Either a ReactAgent / DeepAgent type or a state record type.\n * @template Bag - Type configuration bag (ConfigurableType, InterruptType, …).\n */\nexport function useSuspenseStream<\n T = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate,\n>(\n options: UseSuspenseStreamOptions<T, InferBag<T, Bag>>,\n): WithClassMessages<WithSuspense<ResolveStreamInterface<T, InferBag<T, Bag>>>>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function useSuspenseStream(options: any): any {\n type StateType = Record<string, unknown>;\n const cache: SuspenseCache = options.suspenseCache ?? defaultSuspenseCache;\n\n // ---- client (needed before useStreamLGP for cache key derivation) ----\n const client = useMemo(\n () =>\n options.client ??\n new Client({\n apiUrl: options.apiUrl,\n apiKey: options.apiKey,\n callerOptions: options.callerOptions,\n defaultHeaders: options.defaultHeaders,\n }),\n [\n options.client,\n options.apiKey,\n options.apiUrl,\n options.callerOptions,\n options.defaultHeaders,\n ],\n );\n\n const { threadId } = options;\n\n const historyLimit: boolean | number =\n typeof options.fetchStateHistory === \"object\" &&\n options.fetchStateHistory != null\n ? (options.fetchStateHistory.limit ?? false)\n : (options.fetchStateHistory ?? false);\n\n // Only manage history via the suspense cache when the caller hasn't\n // supplied an external `thread` and there's a threadId to load.\n const needsHistoryFetch = threadId != null && options.thread == null;\n\n // ---- suspense cache lookup (synchronous, may create fetch) ----\n let cacheEntry: SuspenseCacheEntry<ThreadState<StateType>[]> | undefined;\n\n if (needsHistoryFetch) {\n cacheEntry = getOrCreateCacheEntry<StateType>(\n cache,\n client,\n threadId,\n historyLimit,\n );\n }\n\n const cachedData =\n cacheEntry?.status === \"resolved\" ? cacheEntry.data : undefined;\n\n // ---- mutable ref so `mutate` always writes the freshest data ----\n const cachedDataRef = useRef(cachedData);\n if (cachedData != null) {\n cachedDataRef.current = cachedData;\n }\n\n // Re-render trigger after external mutate calls.\n const [, setMutateVersion] = useState(0);\n\n const mutate = useCallback(\n async (\n mutateId?: string,\n ): Promise<ThreadState<StateType>[] | null | undefined> => {\n const fetchId = mutateId ?? threadId;\n if (!fetchId) return undefined;\n try {\n const data = await fetchThreadHistory<StateType>(client, fetchId, {\n limit: historyLimit,\n });\n const key = getCacheKey(client, fetchId, historyLimit);\n cache.set(key, { status: \"resolved\", data });\n cachedDataRef.current = data;\n setMutateVersion((v) => v + 1);\n return data;\n } catch {\n return undefined;\n }\n },\n [cache, client, threadId, historyLimit],\n );\n\n // ---- build thread override for useStreamLGP ----\n const thread: UseStreamThread<StateType> | undefined = useMemo(() => {\n if (!needsHistoryFetch) return options.thread;\n return {\n data: cachedDataRef.current,\n error: undefined,\n isLoading: false,\n mutate,\n };\n // `cachedData` is included so the memo recomputes when the cache\n // transitions from pending → resolved across suspend/retry cycles.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [needsHistoryFetch, options.thread, cachedData, mutate]);\n\n // ---- delegate to useStreamLGP (must always run – Rules of Hooks) ----\n const stream = useStreamLGP({\n ...options,\n client,\n thread,\n });\n\n // ---- post-hook: suspend or throw ----\n\n // Suspend while thread history is loading, but only when the stream\n // itself is idle. If an active stream is running (e.g. the thread was\n // just created during submit), suspending would discard the stream\n // state, so we skip it.\n if (needsHistoryFetch && cacheEntry && !stream.isLoading) {\n if (cacheEntry.status === \"pending\") {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw cacheEntry.promise;\n }\n if (cacheEntry.status === \"rejected\") {\n // Clear cache so a subsequent retry (ErrorBoundary reset) starts\n // a fresh fetch instead of re-throwing the stale error.\n const key = getCacheKey(client, threadId!, historyLimit);\n cache.delete(key);\n // eslint-disable-next-line no-instanceof/no-instanceof\n throw cacheEntry.error instanceof Error\n ? cacheEntry.error\n : new Error(String(cacheEntry.error));\n }\n }\n\n // Throw non-streaming errors to the nearest Error Boundary.\n if (stream.error != null && !stream.isLoading) {\n // eslint-disable-next-line no-instanceof/no-instanceof\n throw stream.error instanceof Error\n ? stream.error\n : new Error(String(stream.error));\n }\n\n // Build return object explicitly to avoid triggering throwing getters\n // (e.g. `history` throws when `fetchStateHistory` is not set).\n return {\n get values() {\n return stream.values;\n },\n get messages() {\n return stream.messages;\n },\n get toolCalls() {\n return stream.toolCalls;\n },\n get toolProgress() {\n return stream.toolProgress;\n },\n getToolCalls: stream.getToolCalls.bind(stream),\n get interrupt() {\n return stream.interrupt;\n },\n get interrupts() {\n return stream.interrupts;\n },\n get subagents() {\n return stream.subagents;\n },\n get activeSubagents() {\n return stream.activeSubagents;\n },\n getSubagent: stream.getSubagent.bind(stream),\n getSubagentsByType: stream.getSubagentsByType.bind(stream),\n getSubagentsByMessage: stream.getSubagentsByMessage.bind(stream),\n getMessagesMetadata: stream.getMessagesMetadata.bind(stream),\n get history() {\n return stream.history;\n },\n get experimental_branchTree() {\n return stream.experimental_branchTree;\n },\n stop: stream.stop,\n submit: stream.submit,\n switchThread: stream.switchThread,\n joinStream: stream.joinStream,\n get branch() {\n return stream.branch;\n },\n setBranch: stream.setBranch,\n get client() {\n return stream.client;\n },\n get assistantId() {\n return stream.assistantId;\n },\n get queue() {\n return stream.queue;\n },\n get isStreaming() {\n return stream.isLoading;\n },\n };\n}\n"],"mappings":";;;;;AA2BA,MAAM,uCAAsC,IAAI,KAAK;AAErD,SAAgB,sBAAqC;AACnD,wBAAO,IAAI,KAAK;;AAGlB,SAAS,YACP,QACA,UACA,OACQ;AACR,QAAO,aAAA,GAAA,gCAAA,qBAAgC,OAAO,CAAC,GAAG,SAAS,GAAG;;AAGhE,SAAS,mBACP,QACA,UACA,SACmC;AACnC,KAAI,SAAS,UAAU,MACrB,QAAO,OAAO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AO,UAElC,OAAM,OAAO,iBAAiB,QAC1B,OAAO,QACP,IAAI,MAAM,OAAO,OAAO,MAAM,CAAC;AAKrC,QAAO;EACL,IAAI,SAAS;AACX,UAAO,OAAO;;EAEhB,IAAI,WAAW;AACb,UAAO,OAAO;;EAEhB,IAAI,YAAY;AACd,UAAO,OAAO;;EAEhB,IAAI,eAAe;AACjB,UAAO,OAAO;;EAEhB,cAAc,OAAO,aAAa,KAAK,OAAO;EAC9C,IAAI,YAAY;AACd,UAAO,OAAO;;EAEhB,IAAI,aAAa;AACf,UAAO,OAAO;;EAEhB,IAAI,YAAY;AACd,UAAO,OAAO;;EAEhB,IAAI,kBAAkB;AACpB,UAAO,OAAO;;EAEhB,aAAa,OAAO,YAAY,KAAK,OAAO;EAC5C,oBAAoB,OAAO,mBAAmB,KAAK,OAAO;EAC1D,uBAAuB,OAAO,sBAAsB,KAAK,OAAO;EAChE,qBAAqB,OAAO,oBAAoB,KAAK,OAAO;EAC5D,IAAI,UAAU;AACZ,UAAO,OAAO;;EAEhB,IAAI,0BAA0B;AAC5B,UAAO,OAAO;;EAEhB,MAAM,OAAO;EACb,QAAQ,OAAO;EACf,cAAc,OAAO;EACrB,YAAY,OAAO;EACnB,IAAI,SAAS;AACX,UAAO,OAAO;;EAEhB,WAAW,OAAO;EAClB,IAAI,SAAS;AACX,UAAO,OAAO;;EAEhB,IAAI,cAAc;AAChB,UAAO,OAAO;;EAEhB,IAAI,QAAQ;AACV,UAAO,OAAO;;EAEhB,IAAI,cAAc;AAChB,UAAO,OAAO;;EAEjB"}
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import { WithClassMessages } from "./stream.cjs";
|
|
1
|
+
import { WithClassMessages as WithClassMessages$1 } from "./stream.cjs";
|
|
2
2
|
import { BagTemplate } from "@langchain/langgraph-sdk";
|
|
3
3
|
import { InferBag, ResolveStreamInterface, ResolveStreamOptions } from "@langchain/langgraph-sdk/ui";
|
|
4
4
|
|
|
5
5
|
//#region src/suspense-stream.d.ts
|
|
6
|
+
type SuspenseCacheEntry<T> = {
|
|
7
|
+
status: "pending";
|
|
8
|
+
promise: Promise<void>;
|
|
9
|
+
} | {
|
|
10
|
+
status: "resolved";
|
|
11
|
+
data: T;
|
|
12
|
+
} | {
|
|
13
|
+
status: "rejected";
|
|
14
|
+
error: unknown;
|
|
15
|
+
};
|
|
16
|
+
type SuspenseCache = Map<string, SuspenseCacheEntry<unknown>>;
|
|
17
|
+
declare function createSuspenseCache(): SuspenseCache;
|
|
6
18
|
/**
|
|
7
19
|
* Clear the internal Suspense cache used by {@link useSuspenseStream}.
|
|
8
20
|
*
|
|
@@ -24,10 +36,17 @@ import { InferBag, ResolveStreamInterface, ResolveStreamOptions } from "@langcha
|
|
|
24
36
|
* </ErrorBoundary>
|
|
25
37
|
* ```
|
|
26
38
|
*/
|
|
27
|
-
declare function invalidateSuspenseCache(): void;
|
|
39
|
+
declare function invalidateSuspenseCache(cache?: SuspenseCache): void;
|
|
28
40
|
type WithSuspense<T> = Omit<T, "isLoading" | "error" | "isThreadLoading"> & {
|
|
29
41
|
isStreaming: boolean;
|
|
30
42
|
};
|
|
43
|
+
type UseSuspenseStreamOptions<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> = ResolveStreamOptions<T, InferBag<T, Bag>> & {
|
|
44
|
+
/**
|
|
45
|
+
* Optional cache store used by Suspense history prefetching.
|
|
46
|
+
* Provide a custom cache in tests to avoid cross-test cache sharing.
|
|
47
|
+
*/
|
|
48
|
+
suspenseCache?: SuspenseCache;
|
|
49
|
+
};
|
|
31
50
|
/**
|
|
32
51
|
* A Suspense-compatible variant of {@link useStream} for LangGraph Platform.
|
|
33
52
|
*
|
|
@@ -56,7 +75,7 @@ type WithSuspense<T> = Omit<T, "isLoading" | "error" | "isThreadLoading"> & {
|
|
|
56
75
|
* @template T - Either a ReactAgent / DeepAgent type or a state record type.
|
|
57
76
|
* @template Bag - Type configuration bag (ConfigurableType, InterruptType, …).
|
|
58
77
|
*/
|
|
59
|
-
declare function useSuspenseStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options:
|
|
78
|
+
declare function useSuspenseStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseSuspenseStreamOptions<T, InferBag<T, Bag>>): WithClassMessages$1<WithSuspense<ResolveStreamInterface<T, InferBag<T, Bag>>>>;
|
|
60
79
|
//#endregion
|
|
61
|
-
export { invalidateSuspenseCache, useSuspenseStream };
|
|
80
|
+
export { SuspenseCache, createSuspenseCache, invalidateSuspenseCache, useSuspenseStream };
|
|
62
81
|
//# sourceMappingURL=suspense-stream.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suspense-stream.d.cts","names":[],"sources":["../src/suspense-stream.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"suspense-stream.d.cts","names":[],"sources":["../src/suspense-stream.tsx"],"mappings":";;;;;KAoBY,kBAAA;EACN,MAAA;EAAmB,OAAA,EAAS,OAAA;AAAA;EAC5B,MAAA;EAAoB,IAAA,EAAM,CAAA;AAAA;EAC1B,MAAA;EAAoB,KAAA;AAAA;AAAA,KAEd,aAAA,GAAgB,GAAA,SAAY,kBAAA;AAAA,iBAIxB,mBAAA,CAAA,GAAuB,aAAA;;;;;;AAJvC;;;;;AAIA;;;;;AA8EA;;;;;AAIC;iBAJe,uBAAA,CACd,KAAA,GAAO,aAAA;AAAA,KASJ,YAAA,MAAkB,IAAA,CAAK,CAAA;EAC1B,WAAA;AAAA;AAAA,KAGG,wBAAA,KACC,MAAA,+BACQ,WAAA,GAAc,WAAA,IACxB,oBAAA,CAAqB,CAAA,EAAG,QAAA,CAAS,CAAA,EAAG,GAAA;EAPZ;;;;EAY1B,aAAA,GAAgB,aAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmClB;;iBAAgB,iBAAA,KACV,MAAA,+BACQ,WAAA,GAAc,WAAA,CAAA,CAE1B,OAAA,EAAS,wBAAA,CAAyB,CAAA,EAAG,QAAA,CAAS,CAAA,EAAG,GAAA,KAChD,mBAAA,CAAkB,YAAA,CAAa,sBAAA,CAAuB,CAAA,EAAG,QAAA,CAAS,CAAA,EAAG,GAAA"}
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import { WithClassMessages } from "./stream.js";
|
|
1
|
+
import { WithClassMessages as WithClassMessages$1 } from "./stream.js";
|
|
2
2
|
import { InferBag, ResolveStreamInterface, ResolveStreamOptions } from "@langchain/langgraph-sdk/ui";
|
|
3
3
|
import { BagTemplate } from "@langchain/langgraph-sdk";
|
|
4
4
|
|
|
5
5
|
//#region src/suspense-stream.d.ts
|
|
6
|
+
type SuspenseCacheEntry<T> = {
|
|
7
|
+
status: "pending";
|
|
8
|
+
promise: Promise<void>;
|
|
9
|
+
} | {
|
|
10
|
+
status: "resolved";
|
|
11
|
+
data: T;
|
|
12
|
+
} | {
|
|
13
|
+
status: "rejected";
|
|
14
|
+
error: unknown;
|
|
15
|
+
};
|
|
16
|
+
type SuspenseCache = Map<string, SuspenseCacheEntry<unknown>>;
|
|
17
|
+
declare function createSuspenseCache(): SuspenseCache;
|
|
6
18
|
/**
|
|
7
19
|
* Clear the internal Suspense cache used by {@link useSuspenseStream}.
|
|
8
20
|
*
|
|
@@ -24,10 +36,17 @@ import { BagTemplate } from "@langchain/langgraph-sdk";
|
|
|
24
36
|
* </ErrorBoundary>
|
|
25
37
|
* ```
|
|
26
38
|
*/
|
|
27
|
-
declare function invalidateSuspenseCache(): void;
|
|
39
|
+
declare function invalidateSuspenseCache(cache?: SuspenseCache): void;
|
|
28
40
|
type WithSuspense<T> = Omit<T, "isLoading" | "error" | "isThreadLoading"> & {
|
|
29
41
|
isStreaming: boolean;
|
|
30
42
|
};
|
|
43
|
+
type UseSuspenseStreamOptions<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> = ResolveStreamOptions<T, InferBag<T, Bag>> & {
|
|
44
|
+
/**
|
|
45
|
+
* Optional cache store used by Suspense history prefetching.
|
|
46
|
+
* Provide a custom cache in tests to avoid cross-test cache sharing.
|
|
47
|
+
*/
|
|
48
|
+
suspenseCache?: SuspenseCache;
|
|
49
|
+
};
|
|
31
50
|
/**
|
|
32
51
|
* A Suspense-compatible variant of {@link useStream} for LangGraph Platform.
|
|
33
52
|
*
|
|
@@ -56,7 +75,7 @@ type WithSuspense<T> = Omit<T, "isLoading" | "error" | "isThreadLoading"> & {
|
|
|
56
75
|
* @template T - Either a ReactAgent / DeepAgent type or a state record type.
|
|
57
76
|
* @template Bag - Type configuration bag (ConfigurableType, InterruptType, …).
|
|
58
77
|
*/
|
|
59
|
-
declare function useSuspenseStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options:
|
|
78
|
+
declare function useSuspenseStream<T = Record<string, unknown>, Bag extends BagTemplate = BagTemplate>(options: UseSuspenseStreamOptions<T, InferBag<T, Bag>>): WithClassMessages$1<WithSuspense<ResolveStreamInterface<T, InferBag<T, Bag>>>>;
|
|
60
79
|
//#endregion
|
|
61
|
-
export { invalidateSuspenseCache, useSuspenseStream };
|
|
80
|
+
export { SuspenseCache, createSuspenseCache, invalidateSuspenseCache, useSuspenseStream };
|
|
62
81
|
//# sourceMappingURL=suspense-stream.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suspense-stream.d.ts","names":[],"sources":["../src/suspense-stream.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"suspense-stream.d.ts","names":[],"sources":["../src/suspense-stream.tsx"],"mappings":";;;;;KAoBY,kBAAA;EACN,MAAA;EAAmB,OAAA,EAAS,OAAA;AAAA;EAC5B,MAAA;EAAoB,IAAA,EAAM,CAAA;AAAA;EAC1B,MAAA;EAAoB,KAAA;AAAA;AAAA,KAEd,aAAA,GAAgB,GAAA,SAAY,kBAAA;AAAA,iBAIxB,mBAAA,CAAA,GAAuB,aAAA;;;;;;AAJvC;;;;;AAIA;;;;;AA8EA;;;;;AAIC;iBAJe,uBAAA,CACd,KAAA,GAAO,aAAA;AAAA,KASJ,YAAA,MAAkB,IAAA,CAAK,CAAA;EAC1B,WAAA;AAAA;AAAA,KAGG,wBAAA,KACC,MAAA,+BACQ,WAAA,GAAc,WAAA,IACxB,oBAAA,CAAqB,CAAA,EAAG,QAAA,CAAS,CAAA,EAAG,GAAA;EAPZ;;;;EAY1B,aAAA,GAAgB,aAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmClB;;iBAAgB,iBAAA,KACV,MAAA,+BACQ,WAAA,GAAc,WAAA,CAAA,CAE1B,OAAA,EAAS,wBAAA,CAAyB,CAAA,EAAG,QAAA,CAAS,CAAA,EAAG,GAAA,KAChD,mBAAA,CAAkB,YAAA,CAAa,sBAAA,CAAuB,CAAA,EAAG,QAAA,CAAS,CAAA,EAAG,GAAA"}
|
package/dist/suspense-stream.js
CHANGED
|
@@ -3,7 +3,10 @@ import { useStreamLGP } from "./stream.lgp.js";
|
|
|
3
3
|
import { useCallback, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { Client, getClientConfigHash } from "@langchain/langgraph-sdk/client";
|
|
5
5
|
//#region src/suspense-stream.tsx
|
|
6
|
-
const
|
|
6
|
+
const defaultSuspenseCache = /* @__PURE__ */ new Map();
|
|
7
|
+
function createSuspenseCache() {
|
|
8
|
+
return /* @__PURE__ */ new Map();
|
|
9
|
+
}
|
|
7
10
|
function getCacheKey(client, threadId, limit) {
|
|
8
11
|
return `suspense:${getClientConfigHash(client)}:${threadId}:${limit}`;
|
|
9
12
|
}
|
|
@@ -15,25 +18,25 @@ function fetchThreadHistory(client, threadId, options) {
|
|
|
15
18
|
const limit = typeof options?.limit === "number" ? options.limit : 10;
|
|
16
19
|
return client.threads.getHistory(threadId, { limit });
|
|
17
20
|
}
|
|
18
|
-
function getOrCreateCacheEntry(client, threadId, limit) {
|
|
21
|
+
function getOrCreateCacheEntry(cache, client, threadId, limit) {
|
|
19
22
|
const key = getCacheKey(client, threadId, limit);
|
|
20
|
-
let entry =
|
|
23
|
+
let entry = cache.get(key);
|
|
21
24
|
if (!entry) {
|
|
22
25
|
entry = {
|
|
23
26
|
status: "pending",
|
|
24
27
|
promise: fetchThreadHistory(client, threadId, { limit }).then((data) => {
|
|
25
|
-
|
|
28
|
+
cache.set(key, {
|
|
26
29
|
status: "resolved",
|
|
27
30
|
data
|
|
28
31
|
});
|
|
29
32
|
}).catch((error) => {
|
|
30
|
-
|
|
33
|
+
cache.set(key, {
|
|
31
34
|
status: "rejected",
|
|
32
35
|
error
|
|
33
36
|
});
|
|
34
37
|
})
|
|
35
38
|
};
|
|
36
|
-
|
|
39
|
+
cache.set(key, entry);
|
|
37
40
|
}
|
|
38
41
|
return entry;
|
|
39
42
|
}
|
|
@@ -58,10 +61,11 @@ function getOrCreateCacheEntry(client, threadId, limit) {
|
|
|
58
61
|
* </ErrorBoundary>
|
|
59
62
|
* ```
|
|
60
63
|
*/
|
|
61
|
-
function invalidateSuspenseCache() {
|
|
62
|
-
|
|
64
|
+
function invalidateSuspenseCache(cache = defaultSuspenseCache) {
|
|
65
|
+
cache.clear();
|
|
63
66
|
}
|
|
64
67
|
function useSuspenseStream(options) {
|
|
68
|
+
const cache = options.suspenseCache ?? defaultSuspenseCache;
|
|
65
69
|
const client = useMemo(() => options.client ?? new Client({
|
|
66
70
|
apiUrl: options.apiUrl,
|
|
67
71
|
apiKey: options.apiKey,
|
|
@@ -78,7 +82,7 @@ function useSuspenseStream(options) {
|
|
|
78
82
|
const historyLimit = typeof options.fetchStateHistory === "object" && options.fetchStateHistory != null ? options.fetchStateHistory.limit ?? false : options.fetchStateHistory ?? false;
|
|
79
83
|
const needsHistoryFetch = threadId != null && options.thread == null;
|
|
80
84
|
let cacheEntry;
|
|
81
|
-
if (needsHistoryFetch) cacheEntry = getOrCreateCacheEntry(client, threadId, historyLimit);
|
|
85
|
+
if (needsHistoryFetch) cacheEntry = getOrCreateCacheEntry(cache, client, threadId, historyLimit);
|
|
82
86
|
const cachedData = cacheEntry?.status === "resolved" ? cacheEntry.data : void 0;
|
|
83
87
|
const cachedDataRef = useRef(cachedData);
|
|
84
88
|
if (cachedData != null) cachedDataRef.current = cachedData;
|
|
@@ -89,7 +93,7 @@ function useSuspenseStream(options) {
|
|
|
89
93
|
try {
|
|
90
94
|
const data = await fetchThreadHistory(client, fetchId, { limit: historyLimit });
|
|
91
95
|
const key = getCacheKey(client, fetchId, historyLimit);
|
|
92
|
-
|
|
96
|
+
cache.set(key, {
|
|
93
97
|
status: "resolved",
|
|
94
98
|
data
|
|
95
99
|
});
|
|
@@ -100,6 +104,7 @@ function useSuspenseStream(options) {
|
|
|
100
104
|
return;
|
|
101
105
|
}
|
|
102
106
|
}, [
|
|
107
|
+
cache,
|
|
103
108
|
client,
|
|
104
109
|
threadId,
|
|
105
110
|
historyLimit
|
|
@@ -127,7 +132,7 @@ function useSuspenseStream(options) {
|
|
|
127
132
|
if (cacheEntry.status === "pending") throw cacheEntry.promise;
|
|
128
133
|
if (cacheEntry.status === "rejected") {
|
|
129
134
|
const key = getCacheKey(client, threadId, historyLimit);
|
|
130
|
-
|
|
135
|
+
cache.delete(key);
|
|
131
136
|
throw cacheEntry.error instanceof Error ? cacheEntry.error : new Error(String(cacheEntry.error));
|
|
132
137
|
}
|
|
133
138
|
}
|
|
@@ -172,15 +177,25 @@ function useSuspenseStream(options) {
|
|
|
172
177
|
submit: stream.submit,
|
|
173
178
|
switchThread: stream.switchThread,
|
|
174
179
|
joinStream: stream.joinStream,
|
|
175
|
-
branch
|
|
180
|
+
get branch() {
|
|
181
|
+
return stream.branch;
|
|
182
|
+
},
|
|
176
183
|
setBranch: stream.setBranch,
|
|
177
|
-
client
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
184
|
+
get client() {
|
|
185
|
+
return stream.client;
|
|
186
|
+
},
|
|
187
|
+
get assistantId() {
|
|
188
|
+
return stream.assistantId;
|
|
189
|
+
},
|
|
190
|
+
get queue() {
|
|
191
|
+
return stream.queue;
|
|
192
|
+
},
|
|
193
|
+
get isStreaming() {
|
|
194
|
+
return stream.isLoading;
|
|
195
|
+
}
|
|
181
196
|
};
|
|
182
197
|
}
|
|
183
198
|
//#endregion
|
|
184
|
-
export { invalidateSuspenseCache, useSuspenseStream };
|
|
199
|
+
export { createSuspenseCache, invalidateSuspenseCache, useSuspenseStream };
|
|
185
200
|
|
|
186
201
|
//# sourceMappingURL=suspense-stream.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suspense-stream.js","names":[],"sources":["../src/suspense-stream.tsx"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\n\"use client\";\n\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport type { ThreadState, BagTemplate } from \"@langchain/langgraph-sdk\";\nimport { Client, getClientConfigHash } from \"@langchain/langgraph-sdk/client\";\nimport type {\n UseStreamThread,\n ResolveStreamOptions,\n ResolveStreamInterface,\n InferBag,\n} from \"@langchain/langgraph-sdk/ui\";\nimport { useStreamLGP } from \"./stream.lgp.js\";\nimport type { WithClassMessages } from \"./stream.js\";\n\n// ---------------------------------------------------------------------------\n// Suspense cache\n// ---------------------------------------------------------------------------\n\ntype SuspenseCacheEntry<T> =\n | { status: \"pending\"; promise: Promise<void> }\n | { status: \"resolved\"; data: T }\n | { status: \"rejected\"; error: unknown };\n\nconst threadCache = new Map<string, SuspenseCacheEntry<unknown>>();\n\nfunction getCacheKey(\n client: Client,\n threadId: string,\n limit: boolean | number,\n): string {\n return `suspense:${getClientConfigHash(client)}:${threadId}:${limit}`;\n}\n\nfunction fetchThreadHistory<StateType extends Record<string, unknown>>(\n client: Client,\n threadId: string,\n options?: { limit?: boolean | number },\n): Promise<ThreadState<StateType>[]> {\n if (options?.limit === false) {\n return client.threads.getState<StateType>(threadId).then((state) => {\n if (state.checkpoint == null) return [];\n return [state];\n });\n }\n\n const limit = typeof options?.limit === \"number\" ? options.limit : 10;\n return client.threads.getHistory<StateType>(threadId, { limit });\n}\n\nfunction getOrCreateCacheEntry<StateType extends Record<string, unknown>>(\n client: Client,\n threadId: string,\n limit: boolean | number,\n): SuspenseCacheEntry<ThreadState<StateType>[]> {\n const key = getCacheKey(client, threadId, limit);\n let entry = threadCache.get(key) as\n | SuspenseCacheEntry<ThreadState<StateType>[]>\n | undefined;\n\n if (!entry) {\n // Start fetch. The promise always resolves (never rejects) so React\n // Suspense correctly waits for it and then retries the render.\n const promise = fetchThreadHistory<StateType>(client, threadId, { limit })\n .then((data) => {\n threadCache.set(key, { status: \"resolved\", data });\n })\n .catch((error: unknown) => {\n threadCache.set(key, { status: \"rejected\", error });\n });\n\n entry = { status: \"pending\", promise };\n threadCache.set(key, entry);\n }\n\n return entry;\n}\n\n/**\n * Clear the internal Suspense cache used by {@link useSuspenseStream}.\n *\n * Call this from an Error Boundary's `onReset` callback so that a retry\n * triggers a fresh thread-history fetch rather than re-throwing the\n * cached error.\n *\n * @example\n * ```tsx\n * <ErrorBoundary\n * onReset={() => invalidateSuspenseCache()}\n * fallbackRender={({ resetErrorBoundary }) => (\n * <button onClick={resetErrorBoundary}>Retry</button>\n * )}\n * >\n * <Suspense fallback={<Spinner />}>\n * <Chat />\n * </Suspense>\n * </ErrorBoundary>\n * ```\n */\nexport function invalidateSuspenseCache(): void {\n threadCache.clear();\n}\n\n// ---------------------------------------------------------------------------\n// Return-type helper\n// ---------------------------------------------------------------------------\n\ntype WithSuspense<T> = Omit<T, \"isLoading\" | \"error\" | \"isThreadLoading\"> & {\n isStreaming: boolean;\n};\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * A Suspense-compatible variant of {@link useStream} for LangGraph Platform.\n *\n * `useSuspenseStream` suspends the component while the initial thread\n * history is being fetched and throws errors to the nearest React Error\n * Boundary. During active streaming the component stays rendered and\n * `isStreaming` indicates whether tokens are arriving.\n *\n * @example\n * ```tsx\n * <ErrorBoundary fallback={<ErrorDisplay />}>\n * <Suspense fallback={<Spinner />}>\n * <Chat />\n * </Suspense>\n * </ErrorBoundary>\n *\n * function Chat() {\n * const { messages, submit, isStreaming } = useSuspenseStream({\n * assistantId: \"agent\",\n * apiUrl: \"http://localhost:2024\",\n * });\n * return <MessageList messages={messages} />;\n * }\n * ```\n *\n * @template T - Either a ReactAgent / DeepAgent type or a state record type.\n * @template Bag - Type configuration bag (ConfigurableType, InterruptType, …).\n */\nexport function useSuspenseStream<\n T = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate,\n>(\n options: ResolveStreamOptions<T, InferBag<T, Bag>>,\n): WithClassMessages<WithSuspense<ResolveStreamInterface<T, InferBag<T, Bag>>>>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function useSuspenseStream(options: any): any {\n type StateType = Record<string, unknown>;\n\n // ---- client (needed before useStreamLGP for cache key derivation) ----\n const client = useMemo(\n () =>\n options.client ??\n new Client({\n apiUrl: options.apiUrl,\n apiKey: options.apiKey,\n callerOptions: options.callerOptions,\n defaultHeaders: options.defaultHeaders,\n }),\n [\n options.client,\n options.apiKey,\n options.apiUrl,\n options.callerOptions,\n options.defaultHeaders,\n ],\n );\n\n const { threadId } = options;\n\n const historyLimit: boolean | number =\n typeof options.fetchStateHistory === \"object\" &&\n options.fetchStateHistory != null\n ? (options.fetchStateHistory.limit ?? false)\n : (options.fetchStateHistory ?? false);\n\n // Only manage history via the suspense cache when the caller hasn't\n // supplied an external `thread` and there's a threadId to load.\n const needsHistoryFetch = threadId != null && options.thread == null;\n\n // ---- suspense cache lookup (synchronous, may create fetch) ----\n let cacheEntry: SuspenseCacheEntry<ThreadState<StateType>[]> | undefined;\n\n if (needsHistoryFetch) {\n cacheEntry = getOrCreateCacheEntry<StateType>(\n client,\n threadId,\n historyLimit,\n );\n }\n\n const cachedData =\n cacheEntry?.status === \"resolved\" ? cacheEntry.data : undefined;\n\n // ---- mutable ref so `mutate` always writes the freshest data ----\n const cachedDataRef = useRef(cachedData);\n if (cachedData != null) {\n cachedDataRef.current = cachedData;\n }\n\n // Re-render trigger after external mutate calls.\n const [, setMutateVersion] = useState(0);\n\n const mutate = useCallback(\n async (\n mutateId?: string,\n ): Promise<ThreadState<StateType>[] | null | undefined> => {\n const fetchId = mutateId ?? threadId;\n if (!fetchId) return undefined;\n try {\n const data = await fetchThreadHistory<StateType>(client, fetchId, {\n limit: historyLimit,\n });\n const key = getCacheKey(client, fetchId, historyLimit);\n threadCache.set(key, { status: \"resolved\", data });\n cachedDataRef.current = data;\n setMutateVersion((v) => v + 1);\n return data;\n } catch {\n return undefined;\n }\n },\n [client, threadId, historyLimit],\n );\n\n // ---- build thread override for useStreamLGP ----\n const thread: UseStreamThread<StateType> | undefined = useMemo(() => {\n if (!needsHistoryFetch) return options.thread;\n return {\n data: cachedDataRef.current,\n error: undefined,\n isLoading: false,\n mutate,\n };\n // `cachedData` is included so the memo recomputes when the cache\n // transitions from pending → resolved across suspend/retry cycles.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [needsHistoryFetch, options.thread, cachedData, mutate]);\n\n // ---- delegate to useStreamLGP (must always run – Rules of Hooks) ----\n const stream = useStreamLGP({\n ...options,\n client,\n thread,\n });\n\n // ---- post-hook: suspend or throw ----\n\n // Suspend while thread history is loading, but only when the stream\n // itself is idle. If an active stream is running (e.g. the thread was\n // just created during submit), suspending would discard the stream\n // state, so we skip it.\n if (needsHistoryFetch && cacheEntry && !stream.isLoading) {\n if (cacheEntry.status === \"pending\") {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw cacheEntry.promise;\n }\n if (cacheEntry.status === \"rejected\") {\n // Clear cache so a subsequent retry (ErrorBoundary reset) starts\n // a fresh fetch instead of re-throwing the stale error.\n const key = getCacheKey(client, threadId!, historyLimit);\n threadCache.delete(key);\n // eslint-disable-next-line no-instanceof/no-instanceof\n throw cacheEntry.error instanceof Error\n ? cacheEntry.error\n : new Error(String(cacheEntry.error));\n }\n }\n\n // Throw non-streaming errors to the nearest Error Boundary.\n if (stream.error != null && !stream.isLoading) {\n // eslint-disable-next-line no-instanceof/no-instanceof\n throw stream.error instanceof Error\n ? stream.error\n : new Error(String(stream.error));\n }\n\n // Build return object explicitly to avoid triggering throwing getters\n // (e.g. `history` throws when `fetchStateHistory` is not set).\n return {\n get values() {\n return stream.values;\n },\n get messages() {\n return stream.messages;\n },\n get toolCalls() {\n return stream.toolCalls;\n },\n get toolProgress() {\n return stream.toolProgress;\n },\n getToolCalls: stream.getToolCalls.bind(stream),\n get interrupt() {\n return stream.interrupt;\n },\n get interrupts() {\n return stream.interrupts;\n },\n get subagents() {\n return stream.subagents;\n },\n get activeSubagents() {\n return stream.activeSubagents;\n },\n getSubagent: stream.getSubagent.bind(stream),\n getSubagentsByType: stream.getSubagentsByType.bind(stream),\n getSubagentsByMessage: stream.getSubagentsByMessage.bind(stream),\n getMessagesMetadata: stream.getMessagesMetadata.bind(stream),\n get history() {\n return stream.history;\n },\n get experimental_branchTree() {\n return stream.experimental_branchTree;\n },\n stop: stream.stop,\n submit: stream.submit,\n switchThread: stream.switchThread,\n joinStream: stream.joinStream,\n branch: stream.branch,\n setBranch: stream.setBranch,\n client: stream.client,\n assistantId: stream.assistantId,\n queue: stream.queue,\n isStreaming: stream.isLoading,\n };\n}\n"],"mappings":";;;;;AAyBA,MAAM,8BAAc,IAAI,KAA0C;AAElE,SAAS,YACP,QACA,UACA,OACQ;AACR,QAAO,YAAY,oBAAoB,OAAO,CAAC,GAAG,SAAS,GAAG;;AAGhE,SAAS,mBACP,QACA,UACA,SACmC;AACnC,KAAI,SAAS,UAAU,MACrB,QAAO,OAAO,QAAQ,SAAoB,SAAS,CAAC,MAAM,UAAU;AAClE,MAAI,MAAM,cAAc,KAAM,QAAO,EAAE;AACvC,SAAO,CAAC,MAAM;GACd;CAGJ,MAAM,QAAQ,OAAO,SAAS,UAAU,WAAW,QAAQ,QAAQ;AACnE,QAAO,OAAO,QAAQ,WAAsB,UAAU,EAAE,OAAO,CAAC;;AAGlE,SAAS,sBACP,QACA,UACA,OAC8C;CAC9C,MAAM,MAAM,YAA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OAAO;;EAEhB,aAAa,OAAO,YAAY,KAAK,OAAO;EAC5C,oBAAoB,OAAO,mBAAmB,KAAK,OAAO;EAC1D,uBAAuB,OAAO,sBAAsB,KAAK,OAAO;EAChE,qBAAqB,OAAO,oBAAoB,KAAK,OAAO;EAC5D,IAAI,UAAU;AACZ,UAAO,OAAO;;EAEhB,IAAI,0BAA0B;AAC5B,UAAO,OAAO;;EAEhB,MAAM,OAAO;EACb,QAAQ,OAAO;EACf,cAAc,OAAO;EACrB,YAAY,OAAO;EACnB,QAAQ,OAAO;EACf,WAAW,OAAO;EAClB,QAAQ,OAAO;EACf,aAAa,OAAO;EACpB,OAAO,OAAO;EACd,aAAa,OAAO;EACrB"}
|
|
1
|
+
{"version":3,"file":"suspense-stream.js","names":[],"sources":["../src/suspense-stream.tsx"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\n\"use client\";\n\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport type { ThreadState, BagTemplate } from \"@langchain/langgraph-sdk\";\nimport { Client, getClientConfigHash } from \"@langchain/langgraph-sdk/client\";\nimport type {\n UseStreamThread,\n ResolveStreamOptions,\n ResolveStreamInterface,\n InferBag,\n} from \"@langchain/langgraph-sdk/ui\";\nimport { useStreamLGP } from \"./stream.lgp.js\";\nimport type { WithClassMessages } from \"./stream.js\";\n\n// ---------------------------------------------------------------------------\n// Suspense cache\n// ---------------------------------------------------------------------------\n\nexport type SuspenseCacheEntry<T> =\n | { status: \"pending\"; promise: Promise<void> }\n | { status: \"resolved\"; data: T }\n | { status: \"rejected\"; error: unknown };\n\nexport type SuspenseCache = Map<string, SuspenseCacheEntry<unknown>>;\n\nconst defaultSuspenseCache: SuspenseCache = new Map();\n\nexport function createSuspenseCache(): SuspenseCache {\n return new Map();\n}\n\nfunction getCacheKey(\n client: Client,\n threadId: string,\n limit: boolean | number,\n): string {\n return `suspense:${getClientConfigHash(client)}:${threadId}:${limit}`;\n}\n\nfunction fetchThreadHistory<StateType extends Record<string, unknown>>(\n client: Client,\n threadId: string,\n options?: { limit?: boolean | number },\n): Promise<ThreadState<StateType>[]> {\n if (options?.limit === false) {\n return client.threads.getState<StateType>(threadId).then((state) => {\n if (state.checkpoint == null) return [];\n return [state];\n });\n }\n\n const limit = typeof options?.limit === \"number\" ? options.limit : 10;\n return client.threads.getHistory<StateType>(threadId, { limit });\n}\n\nfunction getOrCreateCacheEntry<StateType extends Record<string, unknown>>(\n cache: SuspenseCache,\n client: Client,\n threadId: string,\n limit: boolean | number,\n): SuspenseCacheEntry<ThreadState<StateType>[]> {\n const key = getCacheKey(client, threadId, limit);\n let entry = cache.get(key) as\n | SuspenseCacheEntry<ThreadState<StateType>[]>\n | undefined;\n\n if (!entry) {\n // Start fetch. The promise always resolves (never rejects) so React\n // Suspense correctly waits for it and then retries the render.\n const promise = fetchThreadHistory<StateType>(client, threadId, { limit })\n .then((data) => {\n cache.set(key, { status: \"resolved\", data });\n })\n .catch((error: unknown) => {\n cache.set(key, { status: \"rejected\", error });\n });\n\n entry = { status: \"pending\", promise };\n cache.set(key, entry);\n }\n\n return entry;\n}\n\n/**\n * Clear the internal Suspense cache used by {@link useSuspenseStream}.\n *\n * Call this from an Error Boundary's `onReset` callback so that a retry\n * triggers a fresh thread-history fetch rather than re-throwing the\n * cached error.\n *\n * @example\n * ```tsx\n * <ErrorBoundary\n * onReset={() => invalidateSuspenseCache()}\n * fallbackRender={({ resetErrorBoundary }) => (\n * <button onClick={resetErrorBoundary}>Retry</button>\n * )}\n * >\n * <Suspense fallback={<Spinner />}>\n * <Chat />\n * </Suspense>\n * </ErrorBoundary>\n * ```\n */\nexport function invalidateSuspenseCache(\n cache: SuspenseCache = defaultSuspenseCache,\n): void {\n cache.clear();\n}\n\n// ---------------------------------------------------------------------------\n// Return-type helper\n// ---------------------------------------------------------------------------\n\ntype WithSuspense<T> = Omit<T, \"isLoading\" | \"error\" | \"isThreadLoading\"> & {\n isStreaming: boolean;\n};\n\ntype UseSuspenseStreamOptions<\n T = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate,\n> = ResolveStreamOptions<T, InferBag<T, Bag>> & {\n /**\n * Optional cache store used by Suspense history prefetching.\n * Provide a custom cache in tests to avoid cross-test cache sharing.\n */\n suspenseCache?: SuspenseCache;\n};\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * A Suspense-compatible variant of {@link useStream} for LangGraph Platform.\n *\n * `useSuspenseStream` suspends the component while the initial thread\n * history is being fetched and throws errors to the nearest React Error\n * Boundary. During active streaming the component stays rendered and\n * `isStreaming` indicates whether tokens are arriving.\n *\n * @example\n * ```tsx\n * <ErrorBoundary fallback={<ErrorDisplay />}>\n * <Suspense fallback={<Spinner />}>\n * <Chat />\n * </Suspense>\n * </ErrorBoundary>\n *\n * function Chat() {\n * const { messages, submit, isStreaming } = useSuspenseStream({\n * assistantId: \"agent\",\n * apiUrl: \"http://localhost:2024\",\n * });\n * return <MessageList messages={messages} />;\n * }\n * ```\n *\n * @template T - Either a ReactAgent / DeepAgent type or a state record type.\n * @template Bag - Type configuration bag (ConfigurableType, InterruptType, …).\n */\nexport function useSuspenseStream<\n T = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate,\n>(\n options: UseSuspenseStreamOptions<T, InferBag<T, Bag>>,\n): WithClassMessages<WithSuspense<ResolveStreamInterface<T, InferBag<T, Bag>>>>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function useSuspenseStream(options: any): any {\n type StateType = Record<string, unknown>;\n const cache: SuspenseCache = options.suspenseCache ?? defaultSuspenseCache;\n\n // ---- client (needed before useStreamLGP for cache key derivation) ----\n const client = useMemo(\n () =>\n options.client ??\n new Client({\n apiUrl: options.apiUrl,\n apiKey: options.apiKey,\n callerOptions: options.callerOptions,\n defaultHeaders: options.defaultHeaders,\n }),\n [\n options.client,\n options.apiKey,\n options.apiUrl,\n options.callerOptions,\n options.defaultHeaders,\n ],\n );\n\n const { threadId } = options;\n\n const historyLimit: boolean | number =\n typeof options.fetchStateHistory === \"object\" &&\n options.fetchStateHistory != null\n ? (options.fetchStateHistory.limit ?? false)\n : (options.fetchStateHistory ?? false);\n\n // Only manage history via the suspense cache when the caller hasn't\n // supplied an external `thread` and there's a threadId to load.\n const needsHistoryFetch = threadId != null && options.thread == null;\n\n // ---- suspense cache lookup (synchronous, may create fetch) ----\n let cacheEntry: SuspenseCacheEntry<ThreadState<StateType>[]> | undefined;\n\n if (needsHistoryFetch) {\n cacheEntry = getOrCreateCacheEntry<StateType>(\n cache,\n client,\n threadId,\n historyLimit,\n );\n }\n\n const cachedData =\n cacheEntry?.status === \"resolved\" ? cacheEntry.data : undefined;\n\n // ---- mutable ref so `mutate` always writes the freshest data ----\n const cachedDataRef = useRef(cachedData);\n if (cachedData != null) {\n cachedDataRef.current = cachedData;\n }\n\n // Re-render trigger after external mutate calls.\n const [, setMutateVersion] = useState(0);\n\n const mutate = useCallback(\n async (\n mutateId?: string,\n ): Promise<ThreadState<StateType>[] | null | undefined> => {\n const fetchId = mutateId ?? threadId;\n if (!fetchId) return undefined;\n try {\n const data = await fetchThreadHistory<StateType>(client, fetchId, {\n limit: historyLimit,\n });\n const key = getCacheKey(client, fetchId, historyLimit);\n cache.set(key, { status: \"resolved\", data });\n cachedDataRef.current = data;\n setMutateVersion((v) => v + 1);\n return data;\n } catch {\n return undefined;\n }\n },\n [cache, client, threadId, historyLimit],\n );\n\n // ---- build thread override for useStreamLGP ----\n const thread: UseStreamThread<StateType> | undefined = useMemo(() => {\n if (!needsHistoryFetch) return options.thread;\n return {\n data: cachedDataRef.current,\n error: undefined,\n isLoading: false,\n mutate,\n };\n // `cachedData` is included so the memo recomputes when the cache\n // transitions from pending → resolved across suspend/retry cycles.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [needsHistoryFetch, options.thread, cachedData, mutate]);\n\n // ---- delegate to useStreamLGP (must always run – Rules of Hooks) ----\n const stream = useStreamLGP({\n ...options,\n client,\n thread,\n });\n\n // ---- post-hook: suspend or throw ----\n\n // Suspend while thread history is loading, but only when the stream\n // itself is idle. If an active stream is running (e.g. the thread was\n // just created during submit), suspending would discard the stream\n // state, so we skip it.\n if (needsHistoryFetch && cacheEntry && !stream.isLoading) {\n if (cacheEntry.status === \"pending\") {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw cacheEntry.promise;\n }\n if (cacheEntry.status === \"rejected\") {\n // Clear cache so a subsequent retry (ErrorBoundary reset) starts\n // a fresh fetch instead of re-throwing the stale error.\n const key = getCacheKey(client, threadId!, historyLimit);\n cache.delete(key);\n // eslint-disable-next-line no-instanceof/no-instanceof\n throw cacheEntry.error instanceof Error\n ? cacheEntry.error\n : new Error(String(cacheEntry.error));\n }\n }\n\n // Throw non-streaming errors to the nearest Error Boundary.\n if (stream.error != null && !stream.isLoading) {\n // eslint-disable-next-line no-instanceof/no-instanceof\n throw stream.error instanceof Error\n ? stream.error\n : new Error(String(stream.error));\n }\n\n // Build return object explicitly to avoid triggering throwing getters\n // (e.g. `history` throws when `fetchStateHistory` is not set).\n return {\n get values() {\n return stream.values;\n },\n get messages() {\n return stream.messages;\n },\n get toolCalls() {\n return stream.toolCalls;\n },\n get toolProgress() {\n return stream.toolProgress;\n },\n getToolCalls: stream.getToolCalls.bind(stream),\n get interrupt() {\n return stream.interrupt;\n },\n get interrupts() {\n return stream.interrupts;\n },\n get subagents() {\n return stream.subagents;\n },\n get activeSubagents() {\n return stream.activeSubagents;\n },\n getSubagent: stream.getSubagent.bind(stream),\n getSubagentsByType: stream.getSubagentsByType.bind(stream),\n getSubagentsByMessage: stream.getSubagentsByMessage.bind(stream),\n getMessagesMetadata: stream.getMessagesMetadata.bind(stream),\n get history() {\n return stream.history;\n },\n get experimental_branchTree() {\n return stream.experimental_branchTree;\n },\n stop: stream.stop,\n submit: stream.submit,\n switchThread: stream.switchThread,\n joinStream: stream.joinStream,\n get branch() {\n return stream.branch;\n },\n setBranch: stream.setBranch,\n get client() {\n return stream.client;\n },\n get assistantId() {\n return stream.assistantId;\n },\n get queue() {\n return stream.queue;\n },\n get isStreaming() {\n return stream.isLoading;\n },\n };\n}\n"],"mappings":";;;;;AA2BA,MAAM,uCAAsC,IAAI,KAAK;AAErD,SAAgB,sBAAqC;AACnD,wBAAO,IAAI,KAAK;;AAGlB,SAAS,YACP,QACA,UACA,OACQ;AACR,QAAO,YAAY,oBAAoB,OAAO,CAAC,GAAG,SAAS,GAAG;;AAGhE,SAAS,mBACP,QACA,UACA,SACmC;AACnC,KAAI,SAAS,UAAU,MACrB,QAAO,OAAO,QAAQ,SAAoB,SAAS,CAAC,MAAM,UAAU;AAClE,MAAI,MAAM,cAAc,KAAM,QAAO,EAAE;AACvC,SAAO,CAAC,MAAM;GACd;CAGJ,MAAM,QAAQ,OAAO,SAAS,UAAU,WAAW,QAAQ,QAAQ;AACnE,QAAO,OAAO,QAAQ,WAAsB,UAAU,EAAE,OAAO,CAAC;;AAGlE,SAAS,sBACP,OACA,QACA,UACA,OAC8C;CAC9C,MAAM,MAAM,YAAY,QAAQ,UAAU,MAAM;CAChD,IAAI,QAAQ,MAAM,IAAI,IAAI;AAI1B,KAAI,CAAC,OAAO;AAWV,UAAQ;GAAE,QAAQ;GAAW,SARb,mBAA8B,QAAQ,UAAU,EAAE,OAAO,CAAC,CACvE,MAAM,SAAS;AACd,UAAM,IAAI,KAAK;KAAE,QAAQ;KAAY;KAAM,CAAC;KAC5C,CACD,OAAO,UAAmB;AACzB,UAAM,IAAI,KAAK;KAAE,QAAQ;KAAY;KAAO,CAAC;KAC7C;GAEkC;AACtC,QAAM,IAAI,KAAK,MAAM;;AAGvB,QAAO;;;;;;;;;;;;;;;;;;;;;;;AAwBT,SAAgB,wBACd,QAAuB,sBACjB;AACN,OAAM,OAAO;;AA8Df,SAAgB,kBAAkB,SAAmB;CAEnD,MAAM,QAAuB,QAAQ,iBAAiB;CAGtD,MAAM,SAAS,cAEX,QAAQ,UACR,IAAI,OAAO;EACT,QAAQ,Q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AAO;;EAEjB"}
|
package/dist/types.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseMessage } from "@langchain/core/messages";
|
|
2
2
|
import { BagTemplate, Client, StreamEvent, StreamMode, ThreadState, ToolProgress } from "@langchain/langgraph-sdk";
|
|
3
|
-
import { CustomSubmitOptions, DefaultSubagentStates, GetConfigurableType, GetInterruptType, GetToolCallsType, GetUpdateType, MessageMetadata
|
|
3
|
+
import { CustomSubmitOptions, DefaultSubagentStates, GetConfigurableType, GetInterruptType, GetToolCallsType, GetUpdateType, MessageMetadata, QueueInterface, Sequence, StreamBase, SubagentStream, SubagentStreamInterface, SubmitOptions, UseStreamCustomOptions as UseStreamCustomOptions$1 } from "@langchain/langgraph-sdk/ui";
|
|
4
4
|
|
|
5
5
|
//#region src/types.d.ts
|
|
6
6
|
interface UseStream<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate, SubagentStates extends Record<string, unknown> = DefaultSubagentStates> extends Omit<StreamBase<StateType, GetToolCallsType<StateType>, GetInterruptType<Bag>, SubagentStates>, "messages"> {
|
|
@@ -45,7 +45,7 @@ interface UseStream<StateType extends Record<string, unknown> = Record<string, u
|
|
|
45
45
|
* @param index - The index of the message in the thread.
|
|
46
46
|
* @returns The metadata for the message.
|
|
47
47
|
*/
|
|
48
|
-
getMessagesMetadata: (message: BaseMessage, index?: number) => MessageMetadata
|
|
48
|
+
getMessagesMetadata: (message: BaseMessage, index?: number) => MessageMetadata<StateType> | undefined;
|
|
49
49
|
/**
|
|
50
50
|
* Progress of tool executions during streaming.
|
|
51
51
|
*/
|
|
@@ -105,5 +105,5 @@ type UseStreamCustom<StateType extends Record<string, unknown> = Record<string,
|
|
|
105
105
|
queue: QueueInterface<StateType, CustomSubmitOptions<StateType, GetConfigurableType<Bag>>>;
|
|
106
106
|
};
|
|
107
107
|
//#endregion
|
|
108
|
-
export { type SubagentStream, type SubagentStreamInterface
|
|
108
|
+
export { type SubagentStream, type SubagentStreamInterface, UseStream, UseStreamCustom, type UseStreamCustomOptions$1 as UseStreamCustomOptions, UseSuspenseStream };
|
|
109
109
|
//# sourceMappingURL=types.d.cts.map
|
package/dist/types.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.tsx"],"mappings":";;;;;UAgFiB,SAAA,mBACG,MAAA,oBAA0B,MAAA,+BAChC,WAAA,GAAc,WAAA,yBACH,MAAA,oBAA0B,qBAAA,UACzC,IAAA,CACR,UAAA,CACE,SAAA,EACA,gBAAA,CAAiB,SAAA,GACjB,gBAAA,CAAiB,GAAA,GACjB,cAAA;EATa;;;EAgBf,QAAA,EAAU,WAAA;EAfkC;;;EAoB5C,eAAA;EAlBiD;;;EAuBjD,IAAA,QAAY,OAAA;EAlBO;;;EAuBnB,MAAA,GACE,MAAA,EAAQ,aAAA,CAAc,GAAA,EAAK,SAAA,sBAC3B,OAAA,GAAU,aAAA,CAAc,SAAA,EAAW,mBAAA,CAAoB,GAAA,OACpD,OAAA;EAlBK;;;EAuBV,MAAA;EAPU;;;EAYV,SAAA,GAAY,MAAA;EAXA;;;EAgBZ,OAAA,EAAS,WAAA,CAAY,SAAA;EAMa;;;;EAAlC,uBAAA,EAAyB,QAAA,CAAS,SAAA;EAkBpB;;;;;;;;EARd,mBAAA,GACE,OAAA,EAAS,WAAA,EACT,KAAA,cACG,
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.tsx"],"mappings":";;;;;UAgFiB,SAAA,mBACG,MAAA,oBAA0B,MAAA,+BAChC,WAAA,GAAc,WAAA,yBACH,MAAA,oBAA0B,qBAAA,UACzC,IAAA,CACR,UAAA,CACE,SAAA,EACA,gBAAA,CAAiB,SAAA,GACjB,gBAAA,CAAiB,GAAA,GACjB,cAAA;EATa;;;EAgBf,QAAA,EAAU,WAAA;EAfkC;;;EAoB5C,eAAA;EAlBiD;;;EAuBjD,IAAA,QAAY,OAA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AC3B,OAAA,GAAU,mBAAA,CAAoB,SAAA,EAAW,mBAAA,CAAoB,GAAA,OAC1D,OAAA;EAEL,YAAA,GAAe,WAAA;EAtGM;;;EA2GrB,KAAA,EAAO,cAAA,CACL,SAAA,EACA,mBAAA,CAAoB,SAAA,EAAW,mBAAA,CAAoB,GAAA;AAAA"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CustomSubmitOptions, DefaultSubagentStates, GetConfigurableType, GetInterruptType, GetToolCallsType, GetUpdateType, MessageMetadata
|
|
1
|
+
import { CustomSubmitOptions, DefaultSubagentStates, GetConfigurableType, GetInterruptType, GetToolCallsType, GetUpdateType, MessageMetadata, QueueInterface, Sequence, StreamBase, SubagentStream, SubagentStreamInterface, SubmitOptions, UseStreamCustomOptions as UseStreamCustomOptions$1 } from "@langchain/langgraph-sdk/ui";
|
|
2
2
|
import { BaseMessage } from "@langchain/core/messages";
|
|
3
3
|
import { BagTemplate, Client, StreamEvent, StreamMode, ThreadState, ToolProgress } from "@langchain/langgraph-sdk";
|
|
4
4
|
|
|
@@ -45,7 +45,7 @@ interface UseStream<StateType extends Record<string, unknown> = Record<string, u
|
|
|
45
45
|
* @param index - The index of the message in the thread.
|
|
46
46
|
* @returns The metadata for the message.
|
|
47
47
|
*/
|
|
48
|
-
getMessagesMetadata: (message: BaseMessage, index?: number) => MessageMetadata
|
|
48
|
+
getMessagesMetadata: (message: BaseMessage, index?: number) => MessageMetadata<StateType> | undefined;
|
|
49
49
|
/**
|
|
50
50
|
* Progress of tool executions during streaming.
|
|
51
51
|
*/
|
|
@@ -105,5 +105,5 @@ type UseStreamCustom<StateType extends Record<string, unknown> = Record<string,
|
|
|
105
105
|
queue: QueueInterface<StateType, CustomSubmitOptions<StateType, GetConfigurableType<Bag>>>;
|
|
106
106
|
};
|
|
107
107
|
//#endregion
|
|
108
|
-
export { type SubagentStream, type SubagentStreamInterface
|
|
108
|
+
export { type SubagentStream, type SubagentStreamInterface, UseStream, UseStreamCustom, type UseStreamCustomOptions$1 as UseStreamCustomOptions, UseSuspenseStream };
|
|
109
109
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.tsx"],"mappings":";;;;;UAgFiB,SAAA,mBACG,MAAA,oBAA0B,MAAA,+BAChC,WAAA,GAAc,WAAA,yBACH,MAAA,oBAA0B,qBAAA,UACzC,IAAA,CACR,UAAA,CACE,SAAA,EACA,gBAAA,CAAiB,SAAA,GACjB,gBAAA,CAAiB,GAAA,GACjB,cAAA;EATa;;;EAgBf,QAAA,EAAU,WAAA;EAfkC;;;EAoB5C,eAAA;EAlBiD;;;EAuBjD,IAAA,QAAY,OAAA;EAlBO;;;EAuBnB,MAAA,GACE,MAAA,EAAQ,aAAA,CAAc,GAAA,EAAK,SAAA,sBAC3B,OAAA,GAAU,aAAA,CAAc,SAAA,EAAW,mBAAA,CAAoB,GAAA,OACpD,OAAA;EAlBK;;;EAuBV,MAAA;EAPU;;;EAYV,SAAA,GAAY,MAAA;EAXA;;;EAgBZ,OAAA,EAAS,WAAA,CAAY,SAAA;EAMa;;;;EAAlC,uBAAA,EAAyB,QAAA,CAAS,SAAA;EAkBpB;;;;;;;;EARd,mBAAA,GACE,OAAA,EAAS,WAAA,EACT,KAAA,cACG,
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.tsx"],"mappings":";;;;;UAgFiB,SAAA,mBACG,MAAA,oBAA0B,MAAA,+BAChC,WAAA,GAAc,WAAA,yBACH,MAAA,oBAA0B,qBAAA,UACzC,IAAA,CACR,UAAA,CACE,SAAA,EACA,gBAAA,CAAiB,SAAA,GACjB,gBAAA,CAAiB,GAAA,GACjB,cAAA;EATa;;;EAgBf,QAAA,EAAU,WAAA;EAfkC;;;EAoB5C,eAAA;EAlBiD;;;EAuBjD,IAAA,QAAY,OAAA;EAlBO;;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,GAAU,mBAAA,CAAoB,SAAA,EAAW,mBAAA,CAAoB,GAAA,OAC1D,OAAA;EAEL,YAAA,GAAe,WAAA;EAtGM;;;EA2GrB,KAAA,EAAO,cAAA,CACL,SAAA,EACA,mBAAA,CAAoB,SAAA,EAAW,mBAAA,CAAoB,GAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "React integration for LangGraph & LangChain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"directory": "libs/sdk-react"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@langchain/langgraph-sdk": "^1.
|
|
13
|
+
"@langchain/langgraph-sdk": "^1.8.1"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@hono/node-server": "^1.19.11",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"vitest-browser-react": "^2.0.5",
|
|
40
40
|
"webdriverio": "^9.25.0",
|
|
41
41
|
"zod": "^4.3.6",
|
|
42
|
-
"@langchain/langgraph": "^1.2.
|
|
42
|
+
"@langchain/langgraph": "^1.2.6",
|
|
43
43
|
"@langchain/langgraph-api": "^1.1.16",
|
|
44
44
|
"@langchain/langgraph-checkpoint": "^1.0.1"
|
|
45
45
|
},
|