diff --git a/libs/sdk-js/src/react/stream.tsx b/libs/sdk-js/src/react/stream.tsx index 4586aa3c4..b7e574cff 100644 --- a/libs/sdk-js/src/react/stream.tsx +++ b/libs/sdk-js/src/react/stream.tsx @@ -782,6 +782,30 @@ export function useStream< submittingRef.current = true; abortRef.current = new AbortController(); + // Unbranch things + const newPath = submitOptions?.checkpoint?.checkpoint_id + ? branchByCheckpoint[submitOptions?.checkpoint?.checkpoint_id]?.branch + : undefined; + + if (newPath != null) setBranch(newPath ?? ""); + + // Assumption: we're setting the initial value + // Used for instant feedback + setStreamValues(() => { + const values = { ...historyValues }; + + if (submitOptions?.optimisticValues != null) { + return { + ...values, + ...(typeof submitOptions.optimisticValues === "function" + ? submitOptions.optimisticValues(values) + : submitOptions.optimisticValues), + }; + } + + return values; + }); + let usableThreadId = threadId; if (!usableThreadId) { const thread = await client.threads.create(); @@ -818,30 +842,6 @@ export function useStream< streamMode, }) as AsyncGenerator; - // Unbranch things - const newPath = submitOptions?.checkpoint?.checkpoint_id - ? branchByCheckpoint[submitOptions?.checkpoint?.checkpoint_id]?.branch - : undefined; - - if (newPath != null) setBranch(newPath ?? ""); - - // Assumption: we're setting the initial value - // Used for instant feedback - setStreamValues(() => { - const values = { ...historyValues }; - - if (submitOptions?.optimisticValues != null) { - return { - ...values, - ...(typeof submitOptions.optimisticValues === "function" - ? submitOptions.optimisticValues(values) - : submitOptions.optimisticValues), - }; - } - - return values; - }); - let streamError: StreamError | undefined; for await (const { event, data } of run) { if (event === "error") {