@mastra/react 1.6.3 → 1.7.0-alpha.0

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/dist/index.js CHANGED
@@ -1985,7 +1985,7 @@ const dbFromServerUiMessages = (uiMessages, metadata) => uiMessages.map((uiMsg)
1985
1985
  }
1986
1986
  };
1987
1987
  });
1988
- const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContext: propsRequestContext, clientTools: hookClientTools, onSignalSent, onSignalEcho, onThreadSignalsUnsupported, enableThreadSignals = false, streamPath }) => {
1988
+ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContext: propsRequestContext, clientTools: hookClientTools, onSignalSent, onSignalEcho, onThreadSignalsUnsupported, enableThreadSignals = false, streamPath, withInitialHistory }) => {
1989
1989
  const threadSignalsDisabled = enableThreadSignals === false;
1990
1990
  const _currentRunId = useRef(void 0);
1991
1991
  const _onChunk = useRef(void 0);
@@ -2010,17 +2010,20 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2010
2010
  const [isAwaitingToolApproval, setIsAwaitingToolApproval] = useState(false);
2011
2011
  const baseClient = useMastraClient();
2012
2012
  const [isRunning, setIsRunning] = useState(false);
2013
+ const [subscriptionHistory, setSubscriptionHistory] = useState(void 0);
2014
+ const historyKey = `${agentId}:${resourceId ?? ""}:${threadId ?? ""}`;
2015
+ const hydratedMessages = withInitialHistory && subscriptionHistory?.key === historyKey ? subscriptionHistory.messages : initialMessages;
2013
2016
  const lastHydration = useRef(void 0);
2014
2017
  useEffect(() => {
2015
2018
  const previous = lastHydration.current;
2016
2019
  const sameThread = previous?.agentId === agentId && previous.resourceId === resourceId && previous.threadId === threadId;
2017
- if (sameThread && previous.initialMessages === initialMessages) return;
2018
- const formattedMessages = resolveInitialMessages(initialMessages ?? []);
2020
+ if (sameThread && previous.initialMessages === hydratedMessages) return;
2021
+ const formattedMessages = resolveInitialMessages(hydratedMessages ?? []);
2019
2022
  lastHydration.current = {
2020
2023
  agentId,
2021
2024
  resourceId,
2022
2025
  threadId,
2023
- initialMessages,
2026
+ initialMessages: hydratedMessages,
2024
2027
  formattedMessages
2025
2028
  };
2026
2029
  if (sameThread) {
@@ -2054,7 +2057,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2054
2057
  agentId,
2055
2058
  resourceId,
2056
2059
  threadId,
2057
- initialMessages,
2060
+ hydratedMessages,
2058
2061
  isRunning,
2059
2062
  isAwaitingToolApproval
2060
2063
  ]);
@@ -2201,7 +2204,8 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2201
2204
  }).getAgent(agentId, void 0, { stream: streamPath });
2202
2205
  _threadSubscriptionPromiseRef.current = subscriptionAgent.subscribeToThread({
2203
2206
  resourceId,
2204
- threadId
2207
+ threadId,
2208
+ ...withInitialHistory ? { withInitialHistory } : {}
2205
2209
  }).then((response) => {
2206
2210
  const subscription = response;
2207
2211
  if (_threadSubscriptionAbortRef.current !== subscriptionAbort) {
@@ -2209,7 +2213,24 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2209
2213
  return;
2210
2214
  }
2211
2215
  _threadSubscriptionRef.current = subscription;
2212
- subscription.processDataStream({ onChunk: (chunk) => processStreamChunk(chunk) }).catch((error) => {
2216
+ subscription.processDataStream({ onChunk: (chunk) => {
2217
+ if (chunk.type === "thread-history") {
2218
+ const history = resolveInitialMessages(chunk.payload.messages);
2219
+ const previousById = new Map((lastHydration.current?.formattedMessages ?? []).map((message) => [message.id, message]));
2220
+ setMessages((current) => {
2221
+ const live = current.filter((message) => previousById.get(message.id) !== message);
2222
+ const liveById = new Map(live.map((message) => [message.id, message]));
2223
+ const historyIds = new Set(history.map((message) => message.id));
2224
+ return [...history.map((message) => liveById.get(message.id) ?? message), ...live.filter((message) => !historyIds.has(message.id))];
2225
+ });
2226
+ setSubscriptionHistory({
2227
+ key: `${agentId}:${resourceId ?? ""}:${threadId}`,
2228
+ messages: chunk.payload.messages
2229
+ });
2230
+ return;
2231
+ }
2232
+ return processStreamChunk(chunk);
2233
+ } }).catch((error) => {
2213
2234
  if (!isAbortError(error)) {
2214
2235
  console.error("[useChat] Thread subscription failed", error);
2215
2236
  setIsRunning(false);
@@ -2237,7 +2258,8 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2237
2258
  closeThreadSubscription,
2238
2259
  markThreadSignalsUnsupported,
2239
2260
  processStreamChunk,
2240
- streamPath
2261
+ streamPath,
2262
+ withInitialHistory
2241
2263
  ]);
2242
2264
  useEffect(() => {
2243
2265
  _threadSignalsUnsupportedRef.current = false;