@mastra/react 1.6.3 → 1.7.0-alpha.1
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/agent/hooks.d.ts +9 -1
- package/dist/agent/hooks.d.ts.map +1 -1
- package/dist/index.cjs +30 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +30 -8
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/agent/hooks.d.ts
CHANGED
|
@@ -27,6 +27,14 @@ export interface MastraChatProps {
|
|
|
27
27
|
enableThreadSignals?: boolean;
|
|
28
28
|
/** Override the legacy stream route for editor-owned hidden agents. */
|
|
29
29
|
streamPath?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Load stored thread messages through the thread subscription instead of
|
|
32
|
+
* `initialMessages`. The subscription sends them as one `thread-history`
|
|
33
|
+
* chunk and skips run parts already covered by it. Requires thread signals.
|
|
34
|
+
*/
|
|
35
|
+
withInitialHistory?: boolean | {
|
|
36
|
+
perPage?: number;
|
|
37
|
+
};
|
|
30
38
|
}
|
|
31
39
|
interface SharedArgs {
|
|
32
40
|
coreUserMessages: CoreUserMessage[];
|
|
@@ -68,7 +76,7 @@ export type StreamArgs = SharedArgs & {
|
|
|
68
76
|
export type NetworkArgs = SharedArgs & {
|
|
69
77
|
onNetworkChunk?: (chunk: NetworkChunkType) => Promise<void>;
|
|
70
78
|
};
|
|
71
|
-
export declare const useChat: ({ agentId, resourceId, threadId, initialMessages, requestContext: propsRequestContext, clientTools: hookClientTools, onSignalSent, onSignalEcho, onThreadSignalsUnsupported, enableThreadSignals, streamPath, }: MastraChatProps) => {
|
|
79
|
+
export declare const useChat: ({ agentId, resourceId, threadId, initialMessages, requestContext: propsRequestContext, clientTools: hookClientTools, onSignalSent, onSignalEcho, onThreadSignalsUnsupported, enableThreadSignals, streamPath, withInitialHistory, }: MastraChatProps) => {
|
|
72
80
|
setMessages: import("react").Dispatch<import("react").SetStateAction<MastraDBMessage[]>>;
|
|
73
81
|
sendMessage: ({ mode, ...args }: SendMessageArgs) => Promise<void>;
|
|
74
82
|
isRunning: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/agent/hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAY,eAAe,EAA4B,MAAM,iCAAiC,CAAC;AAE3G,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAEnE,OAAO,KAAK,EAAE,SAAS,EAAiB,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAkBtF,OAAO,KAAK,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AA0JpF,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,eAAe,EAAE,CAAC;IACpC,6FAA6F;IAC7F,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC;;;;OAIG;IACH,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAC/B,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3D,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1C,0BAA0B,CAAC,EAAE,MAAM,IAAI,CAAC;IACxC;;;OAGG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,uEAAuE;IACvE,UAAU,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/agent/hooks.ts"],"names":[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W,GAAG,UAAU,GAAG;IACrC,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7D,CAAC;AAoDF,eAAO,MAAM,OAAO,wOAajB,eAAe;;qCA2gCyC,eAAe;;;;;;;;;;;kCAzP7B,MAAM,eAAe,OAAO;kCAuE5B,MAAM;0CAoDE,MAAM;0CA2BN,MAAM;;;;oBAh5BvB,UAAU,GAAG,UAAU;;;uCA26BT,MAAM,UAAU,MAAM;uCAoCtB,MAAM,UAAU,MAAM;;;oBA58BtC,UAAU,GAAG,UAAU;;;CAsjCxD,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -1986,7 +1986,7 @@ const dbFromServerUiMessages = (uiMessages, metadata) => uiMessages.map((uiMsg)
|
|
|
1986
1986
|
}
|
|
1987
1987
|
};
|
|
1988
1988
|
});
|
|
1989
|
-
const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContext: propsRequestContext, clientTools: hookClientTools, onSignalSent, onSignalEcho, onThreadSignalsUnsupported, enableThreadSignals = false, streamPath }) => {
|
|
1989
|
+
const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContext: propsRequestContext, clientTools: hookClientTools, onSignalSent, onSignalEcho, onThreadSignalsUnsupported, enableThreadSignals = false, streamPath, withInitialHistory }) => {
|
|
1990
1990
|
const threadSignalsDisabled = enableThreadSignals === false;
|
|
1991
1991
|
const _currentRunId = (0, react.useRef)(void 0);
|
|
1992
1992
|
const _onChunk = (0, react.useRef)(void 0);
|
|
@@ -2011,17 +2011,20 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
|
|
|
2011
2011
|
const [isAwaitingToolApproval, setIsAwaitingToolApproval] = (0, react.useState)(false);
|
|
2012
2012
|
const baseClient = useMastraClient();
|
|
2013
2013
|
const [isRunning, setIsRunning] = (0, react.useState)(false);
|
|
2014
|
+
const [subscriptionHistory, setSubscriptionHistory] = (0, react.useState)(void 0);
|
|
2015
|
+
const historyKey = `${agentId}:${resourceId ?? ""}:${threadId ?? ""}`;
|
|
2016
|
+
const hydratedMessages = withInitialHistory && subscriptionHistory?.key === historyKey ? subscriptionHistory.messages : initialMessages;
|
|
2014
2017
|
const lastHydration = (0, react.useRef)(void 0);
|
|
2015
2018
|
(0, react.useEffect)(() => {
|
|
2016
2019
|
const previous = lastHydration.current;
|
|
2017
2020
|
const sameThread = previous?.agentId === agentId && previous.resourceId === resourceId && previous.threadId === threadId;
|
|
2018
|
-
if (sameThread && previous.initialMessages ===
|
|
2019
|
-
const formattedMessages = resolveInitialMessages(
|
|
2021
|
+
if (sameThread && previous.initialMessages === hydratedMessages) return;
|
|
2022
|
+
const formattedMessages = resolveInitialMessages(hydratedMessages ?? []);
|
|
2020
2023
|
lastHydration.current = {
|
|
2021
2024
|
agentId,
|
|
2022
2025
|
resourceId,
|
|
2023
2026
|
threadId,
|
|
2024
|
-
initialMessages,
|
|
2027
|
+
initialMessages: hydratedMessages,
|
|
2025
2028
|
formattedMessages
|
|
2026
2029
|
};
|
|
2027
2030
|
if (sameThread) {
|
|
@@ -2055,7 +2058,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
|
|
|
2055
2058
|
agentId,
|
|
2056
2059
|
resourceId,
|
|
2057
2060
|
threadId,
|
|
2058
|
-
|
|
2061
|
+
hydratedMessages,
|
|
2059
2062
|
isRunning,
|
|
2060
2063
|
isAwaitingToolApproval
|
|
2061
2064
|
]);
|
|
@@ -2202,7 +2205,8 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
|
|
|
2202
2205
|
}).getAgent(agentId, void 0, { stream: streamPath });
|
|
2203
2206
|
_threadSubscriptionPromiseRef.current = subscriptionAgent.subscribeToThread({
|
|
2204
2207
|
resourceId,
|
|
2205
|
-
threadId
|
|
2208
|
+
threadId,
|
|
2209
|
+
...withInitialHistory ? { withInitialHistory } : {}
|
|
2206
2210
|
}).then((response) => {
|
|
2207
2211
|
const subscription = response;
|
|
2208
2212
|
if (_threadSubscriptionAbortRef.current !== subscriptionAbort) {
|
|
@@ -2210,7 +2214,24 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
|
|
|
2210
2214
|
return;
|
|
2211
2215
|
}
|
|
2212
2216
|
_threadSubscriptionRef.current = subscription;
|
|
2213
|
-
subscription.processDataStream({ onChunk: (chunk) =>
|
|
2217
|
+
subscription.processDataStream({ onChunk: (chunk) => {
|
|
2218
|
+
if (chunk.type === "thread-history") {
|
|
2219
|
+
const history = resolveInitialMessages(chunk.payload.messages);
|
|
2220
|
+
const previousById = new Map((lastHydration.current?.formattedMessages ?? []).map((message) => [message.id, message]));
|
|
2221
|
+
setMessages((current) => {
|
|
2222
|
+
const live = current.filter((message) => previousById.get(message.id) !== message);
|
|
2223
|
+
const liveById = new Map(live.map((message) => [message.id, message]));
|
|
2224
|
+
const historyIds = new Set(history.map((message) => message.id));
|
|
2225
|
+
return [...history.map((message) => liveById.get(message.id) ?? message), ...live.filter((message) => !historyIds.has(message.id))];
|
|
2226
|
+
});
|
|
2227
|
+
setSubscriptionHistory({
|
|
2228
|
+
key: `${agentId}:${resourceId ?? ""}:${threadId}`,
|
|
2229
|
+
messages: chunk.payload.messages
|
|
2230
|
+
});
|
|
2231
|
+
return;
|
|
2232
|
+
}
|
|
2233
|
+
return processStreamChunk(chunk);
|
|
2234
|
+
} }).catch((error) => {
|
|
2214
2235
|
if (!isAbortError(error)) {
|
|
2215
2236
|
console.error("[useChat] Thread subscription failed", error);
|
|
2216
2237
|
setIsRunning(false);
|
|
@@ -2238,7 +2259,8 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
|
|
|
2238
2259
|
closeThreadSubscription,
|
|
2239
2260
|
markThreadSignalsUnsupported,
|
|
2240
2261
|
processStreamChunk,
|
|
2241
|
-
streamPath
|
|
2262
|
+
streamPath,
|
|
2263
|
+
withInitialHistory
|
|
2242
2264
|
]);
|
|
2243
2265
|
(0, react.useEffect)(() => {
|
|
2244
2266
|
_threadSignalsUnsupportedRef.current = false;
|