@dbx-tools/ui-mastra 0.6.218 → 0.6.219

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.
@@ -1,32 +1,76 @@
1
1
  import { error as sharedError, log } from "@dbx-tools/shared-core";
2
+ import type { ListMemoryThreadMessagesResponse } from "@mastra/client-js";
2
3
  import type { UIMessage } from "ai";
3
4
  import { useCallback, useEffect, useRef, useState } from "react";
4
- import type { MastraPluginClient } from "../support/mastra-client.ts";
5
5
  import type {
6
6
  ThreadMessageWriter,
7
7
  ThreadSessionReader,
8
8
  ThreadSessionUpdater,
9
9
  } from "./chat-sessions.ts";
10
+ import type { PendingApproval } from "./types.ts";
11
+ import type { MastraPluginClient } from "../support/mastra-client.ts";
10
12
 
11
13
  const HISTORY_PAGE_SIZE = 20;
12
14
  const logger = log.logger("ui-mastra/chat");
13
15
 
14
16
  interface UseChatHistoryOptions {
15
17
  activeKey: string;
16
- activeThreadId: string | undefined;
18
+ activeThreadId: string;
17
19
  agentId: string;
18
20
  getSession: ThreadSessionReader;
21
+ availability: "loading" | "present" | "missing";
19
22
  mastraClient: MastraPluginClient;
20
23
  updateSession: ThreadSessionUpdater;
21
24
  writeMessages: ThreadMessageWriter;
22
25
  }
23
26
 
27
+ type SuspendedRuns = Awaited<ReturnType<MastraPluginClient["suspendedRuns"]>>["runs"];
28
+
29
+ /** Read a newest-first native memory page as chronological UI messages. */
30
+ function toChronologicalUiMessages(response: ListMemoryThreadMessagesResponse): UIMessage[] {
31
+ return [...(response.uiMessages ?? [])].reverse() as UIMessage[];
32
+ }
33
+
34
+ /** Recover actionable approval cards from Mastra's persisted suspended runs. */
35
+ function pendingApprovals(
36
+ messages: UIMessage[],
37
+ runs: SuspendedRuns,
38
+ ): Record<string, PendingApproval[]> {
39
+ const approvals: Record<string, PendingApproval[]> = {};
40
+ const assistantMessages = messages.filter((message) => message.role === "assistant");
41
+ for (const run of runs) {
42
+ for (const toolCall of run.toolCalls) {
43
+ if (!toolCall.requiresApproval || !toolCall.toolCallId || !toolCall.toolName) continue;
44
+ const owner =
45
+ [...assistantMessages]
46
+ .reverse()
47
+ .find((message) =>
48
+ message.parts.some(
49
+ (part) => (part as { toolCallId?: unknown }).toolCallId === toolCall.toolCallId,
50
+ ),
51
+ ) ?? assistantMessages.at(-1);
52
+ if (!owner) continue;
53
+ approvals[owner.id] = [
54
+ ...(approvals[owner.id] ?? []),
55
+ {
56
+ runId: run.runId,
57
+ toolCallId: toolCall.toolCallId,
58
+ toolName: toolCall.toolName,
59
+ input: toolCall.args,
60
+ },
61
+ ];
62
+ }
63
+ }
64
+ return approvals;
65
+ }
66
+
24
67
  /** Hydrate and page the active thread while other thread sessions keep running. */
25
68
  export function useChatHistory({
26
69
  activeKey,
27
70
  activeThreadId,
28
71
  agentId,
29
72
  getSession,
73
+ availability,
30
74
  mastraClient,
31
75
  updateSession,
32
76
  writeMessages,
@@ -44,6 +88,19 @@ export function useChatHistory({
44
88
  setIsLoadingHistory(false);
45
89
  return;
46
90
  }
91
+ if (availability === "loading") {
92
+ setIsLoadingHistory(true);
93
+ return;
94
+ }
95
+ if (availability === "missing") {
96
+ updateSession(threadId, (current) => ({
97
+ ...current,
98
+ historyLoaded: true,
99
+ hasMoreHistory: false,
100
+ }));
101
+ setIsLoadingHistory(false);
102
+ return;
103
+ }
47
104
 
48
105
  let cancelled = false;
49
106
  const controller = new AbortController();
@@ -57,16 +114,28 @@ export function useChatHistory({
57
114
  perPage: HISTORY_PAGE_SIZE,
58
115
  signal: controller.signal,
59
116
  })
60
- .then((response) => {
117
+ .then(async (response) => {
118
+ const suspended = await mastraClient
119
+ .suspendedRuns(agentId, activeThreadId, controller.signal)
120
+ .catch((error: unknown) => {
121
+ if ((error as { name?: string }).name === "AbortError") throw error;
122
+ logger.warn("suspended-run discovery error", {
123
+ error: sharedError.errorMessage(error),
124
+ });
125
+ return { runs: [], total: 0 };
126
+ });
127
+ return [response, suspended] as const;
128
+ })
129
+ .then(([response, suspended]) => {
61
130
  if (cancelled) return;
131
+ const messages = toChronologicalUiMessages(response);
62
132
  updateSession(threadId, (current) => ({
63
133
  ...current,
64
- messages: response.uiMessages as unknown as UIMessage[],
134
+ messages,
65
135
  historyLoaded: true,
66
- hasMoreHistory: response.hasMore,
136
+ hasMoreHistory: response.hasMore ?? false,
67
137
  historyPage: 1,
68
- toolEventsByMessage: {},
69
- pendingApprovalsByMessage: {},
138
+ pendingApprovalsByMessage: pendingApprovals(messages, suspended.runs),
70
139
  feedbackByMessage: {},
71
140
  }));
72
141
  })
@@ -89,7 +158,7 @@ export function useChatHistory({
89
158
  cancelled = true;
90
159
  controller.abort();
91
160
  };
92
- }, [activeKey, activeThreadId, agentId, getSession, mastraClient, updateSession]);
161
+ }, [activeKey, activeThreadId, agentId, availability, getSession, mastraClient, updateSession]);
93
162
 
94
163
  const loadOlderHistory = useCallback(() => {
95
164
  const threadId = activeKey;
@@ -102,13 +171,13 @@ export function useChatHistory({
102
171
  mastraClient
103
172
  .history({ agentId, threadId: activeThreadId, page, perPage: HISTORY_PAGE_SIZE })
104
173
  .then((response) => {
105
- const uiMessages = response.uiMessages as unknown as UIMessage[];
174
+ const uiMessages = toChronologicalUiMessages(response);
106
175
  if (uiMessages.length > 0) {
107
176
  writeMessages(threadId, [...uiMessages, ...getSession(threadId).messages]);
108
177
  }
109
178
  updateSession(threadId, (current) => ({
110
179
  ...current,
111
- hasMoreHistory: response.hasMore,
180
+ hasMoreHistory: response.hasMore ?? false,
112
181
  }));
113
182
  })
114
183
  .catch((error: unknown) => {
@@ -1,16 +1,12 @@
1
1
  import { feedback } from "@dbx-tools/shared-mastra";
2
+ import { readUIMessageStream, type UIMessage } from "ai";
2
3
  import { useCallback } from "react";
3
- import type { MastraStreamResponse } from "../support/mastra-stream.ts";
4
4
  import type {
5
5
  ThreadMessageWriter,
6
6
  ThreadSessionReader,
7
7
  ThreadSessionUpdater,
8
8
  } from "./chat-sessions.ts";
9
- import {
10
- chatStreamAssistantMessage,
11
- createChatStreamState,
12
- reduceChatStreamChunk,
13
- } from "./chat-stream-reducer.ts";
9
+ import type { MastraStreamResponse } from "../support/mastra-client.ts";
14
10
 
15
11
  /** Read the MLflow trace id captured by the server on a stream response. */
16
12
  const readMlflowTraceId = (stream: unknown): string | undefined => {
@@ -20,13 +16,22 @@ const readMlflowTraceId = (stream: unknown): string | undefined => {
20
16
 
21
17
  class StreamAborted extends Error {}
22
18
 
19
+ /** Replace or append one assistant message without disturbing the transcript. */
20
+ function upsertAssistant(messages: UIMessage[], assistant: UIMessage): UIMessage[] {
21
+ const next = [...messages];
22
+ const index = next.findIndex((message) => message.id === assistant.id);
23
+ if (index === -1) next.push(assistant);
24
+ else next[index] = assistant;
25
+ return next;
26
+ }
27
+
23
28
  interface UseChatStreamOptions {
24
29
  getSession: ThreadSessionReader;
25
30
  updateSession: ThreadSessionUpdater;
26
31
  writeMessages: ThreadMessageWriter;
27
32
  }
28
33
 
29
- /** Translate one validated Mastra data stream into thread messages and tool state. */
34
+ /** Project one Mastra stream through the native AI SDK UI message converter. */
30
35
  export function useChatStream({ getSession, updateSession, writeMessages }: UseChatStreamOptions) {
31
36
  return useCallback(
32
37
  async (
@@ -54,59 +59,24 @@ export function useChatStream({ getSession, updateSession, writeMessages }: UseC
54
59
  );
55
60
  }
56
61
 
57
- const session = getSession(threadId);
58
- const existing = session.messages.find((message) => message.id === assistantId);
59
- let state = createChatStreamState({
60
- ...(existing ? { existing } : {}),
61
- pendingApprovals: session.pendingApprovalsByMessage[assistantId] ?? [],
62
- runId: runIdRef.current,
63
- toolEvents: session.toolEventsByMessage[assistantId] ?? [],
64
- });
65
-
66
- const upsertAssistant = () => {
67
- const next = [...getSession(threadId).messages];
68
- const index = next.findIndex((message) => message.id === assistantId);
69
- const message = chatStreamAssistantMessage(assistantId, state);
70
- if (index === -1) next.push(message);
71
- else next[index] = message;
72
- writeMessages(threadId, next);
73
- };
74
-
75
62
  try {
76
- await stream.processDataStream({
77
- onChunk: async (chunk) => {
78
- if (signal.aborted) throw new StreamAborted();
79
- const next = reduceChatStreamChunk(state, chunk);
80
- state = next.state;
81
- runIdRef.current = state.runId;
82
-
83
- if (Object.values(next.changes).some(Boolean)) {
84
- updateSession(threadId, (current) => ({
85
- ...current,
86
- ...(next.changes.runId ? { runId: state.runId } : {}),
87
- ...(next.changes.status ? { status: "streaming" as const } : {}),
88
- ...(next.changes.toolEvents
89
- ? {
90
- toolEventsByMessage: {
91
- ...current.toolEventsByMessage,
92
- [assistantId]: state.toolEvents,
93
- },
94
- }
95
- : {}),
96
- ...(next.changes.pendingApprovals
97
- ? {
98
- pendingApprovalsByMessage: {
99
- ...current.pendingApprovalsByMessage,
100
- [assistantId]: state.pendingApprovals,
101
- },
102
- }
103
- : {}),
104
- }));
105
- }
106
- if (next.assistantChanged) upsertAssistant();
107
- if (next.error) throw new Error(next.error);
108
- },
109
- });
63
+ const existing = getSession(threadId).messages.find(
64
+ (message) => message.id === assistantId,
65
+ );
66
+ for await (const message of readUIMessageStream({
67
+ ...(existing ? { message: existing } : {}),
68
+ stream: stream.stream,
69
+ terminateOnError: true,
70
+ })) {
71
+ if (signal.aborted) throw new StreamAborted();
72
+ const assistant = message.id === assistantId ? message : { ...message, id: assistantId };
73
+ writeMessages(threadId, upsertAssistant(getSession(threadId).messages, assistant));
74
+ updateSession(threadId, (current) => ({
75
+ ...current,
76
+ runId: runIdRef.current,
77
+ status: "streaming",
78
+ }));
79
+ }
110
80
  } catch (error) {
111
81
  if (error instanceof StreamAborted || signal.aborted) return;
112
82
  throw error;
@@ -37,6 +37,10 @@ type ChatThreadLayoutProps = {
37
37
  className: ChatViewProps["className"];
38
38
  threads: ChatViewProps["threads"];
39
39
  threadPlacement: NonNullable<ChatViewProps["threadPlacement"]>;
40
+ threadBarActions: ChatViewProps["threadBarActions"];
41
+ showThreadBarActions: NonNullable<ChatViewProps["showThreadBarActions"]>;
42
+ threadNewIcon: ChatViewProps["threadNewIcon"];
43
+ threadHistoryIcon: ChatViewProps["threadHistoryIcon"];
40
44
  activeThreadId: ChatViewProps["activeThreadId"];
41
45
  streamingThreadIds: NonNullable<ChatViewProps["streamingThreadIds"]>;
42
46
  isLoadingThreads: NonNullable<ChatViewProps["isLoadingThreads"]>;
@@ -87,6 +91,10 @@ export const ChatThreadLayout = ({
87
91
  className,
88
92
  threads,
89
93
  threadPlacement,
94
+ threadBarActions,
95
+ showThreadBarActions,
96
+ threadNewIcon,
97
+ threadHistoryIcon,
90
98
  activeThreadId,
91
99
  streamingThreadIds,
92
100
  isLoadingThreads,
@@ -179,7 +187,15 @@ export const ChatThreadLayout = ({
179
187
  ))}
180
188
  <div className="flex h-full min-w-0 flex-1 flex-col">
181
189
  {tabbedThreads && (
182
- <ThreadTabs {...threadListProps} openThreadIds={openTabIds} onCloseTab={closeTab} />
190
+ <ThreadTabs
191
+ {...threadListProps}
192
+ openThreadIds={openTabIds}
193
+ onCloseTab={closeTab}
194
+ actions={threadBarActions}
195
+ showActions={showThreadBarActions}
196
+ newIcon={threadNewIcon}
197
+ historyIcon={threadHistoryIcon}
198
+ />
183
199
  )}
184
200
  {showSidebarToggle && (
185
201
  <div
@@ -29,6 +29,7 @@ export const ChatView = ({
29
29
  onModelChange,
30
30
  defaultModelName,
31
31
  composerActions,
32
+ composerLeadingActions,
32
33
  onLoadMore,
33
34
  isLoadingMore = false,
34
35
  hasMore = false,
@@ -38,6 +39,10 @@ export const ChatView = ({
38
39
  onClear,
39
40
  threads,
40
41
  threadPlacement = "auto",
42
+ threadBarActions,
43
+ showThreadBarActions = true,
44
+ threadNewIcon,
45
+ threadHistoryIcon,
41
46
  activeThreadId,
42
47
  streamingThreadIds = [],
43
48
  isLoadingThreads = false,
@@ -67,6 +72,10 @@ export const ChatView = ({
67
72
  className={className}
68
73
  threads={threads}
69
74
  threadPlacement={threadPlacement}
75
+ threadBarActions={threadBarActions}
76
+ showThreadBarActions={showThreadBarActions}
77
+ threadNewIcon={threadNewIcon}
78
+ threadHistoryIcon={threadHistoryIcon}
70
79
  activeThreadId={activeThreadId}
71
80
  streamingThreadIds={streamingThreadIds}
72
81
  isLoadingThreads={isLoadingThreads}
@@ -110,6 +119,7 @@ export const ChatView = ({
110
119
  onModelChange={onModelChange}
111
120
  defaultModelName={defaultModelName}
112
121
  composerActions={composerActions}
122
+ composerLeadingActions={composerLeadingActions}
113
123
  isLoadingHistory={isLoadingHistory}
114
124
  onClear={onClear}
115
125
  onExportConversation={onExportConversation}
@@ -45,7 +45,14 @@ export const ExportMenu = ({
45
45
  disabled?: boolean;
46
46
  }) => {
47
47
  const trigger = iconOnly ? (
48
- <Button type="button" size="icon" variant="ghost" className="size-7" disabled={disabled}>
48
+ <Button
49
+ type="button"
50
+ size="icon"
51
+ variant="ghost"
52
+ className="size-7"
53
+ disabled={disabled}
54
+ aria-label={tooltip}
55
+ >
49
56
  <DownloadIcon className="size-3" />
50
57
  </Button>
51
58
  ) : (
@@ -2,6 +2,8 @@
2
2
  //
3
3
  // - `MastraChat` / `useMastraChat`: the self-contained drop-in (and its
4
4
  // headless driver) that wire themselves from the Mastra plugin config.
5
+ // - `MastraAssistant` / `useMastraAssistant`: the persistent app shell and
6
+ // controller for docked/overlay chat launched from anywhere below it.
5
7
  // - `ChatView`: the controlled, presentational shell for callers that
6
8
  // own message state and transport themselves.
7
9
  // - The Mastra plugin client + hooks (model catalogue, history paging,
@@ -27,13 +29,27 @@ export {
27
29
  export type { ByIdFetchState } from "../support/mastra-client.ts";
28
30
  export { ChatView } from "./chat-view.tsx";
29
31
  export { ExportMenu } from "./export-menu.tsx";
32
+ export { MastraAssistant, useMastraAssistant } from "./mastra-assistant.tsx";
33
+ export type {
34
+ MastraAssistantController,
35
+ MastraAssistantLauncherOptions,
36
+ MastraAssistantLauncherPosition,
37
+ MastraAssistantMode,
38
+ MastraAssistantProps,
39
+ MastraAssistantResizeOptions,
40
+ MastraAssistantSide,
41
+ } from "./mastra-assistant.tsx";
42
+ export type {
43
+ MastraRequestContextInput,
44
+ MastraRequestContextSource,
45
+ } from "../support/request-context.ts";
30
46
  export { dedupeSuggestions } from "./suggestions.ts";
31
47
  export { MastraChat, useMastraChat } from "./mastra-chat.tsx";
32
48
  export type { MastraChatProps, UseMastraChatOptions } from "./mastra-chat.tsx";
33
49
  export { ThreadSidebar } from "./thread-sidebar.tsx";
34
50
  export type { ThreadSidebarProps } from "./thread-sidebar.tsx";
35
- export { ThreadTabs } from "./thread-tabs.tsx";
36
- export type { ThreadTabsProps } from "./thread-tabs.tsx";
51
+ export { ThreadActions, ThreadTabs } from "./thread-tabs.tsx";
52
+ export type { ThreadActionsProps, ThreadTabsProps } from "./thread-tabs.tsx";
37
53
  export type {
38
54
  ApprovalDecision,
39
55
  ChatModelOption,