@dbx-tools/ui-mastra 0.9.118 → 0.9.120

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.
@@ -33,6 +33,17 @@ export interface MastraStreamResponse {
33
33
  stream: ReadableStream<UIMessageChunk>;
34
34
  }
35
35
 
36
+ /** Terminal HTTP failure returned before a chat stream was established. */
37
+ export class MastraChatRequestError extends Error {
38
+ constructor(
39
+ message: string,
40
+ readonly status: number,
41
+ ) {
42
+ super(message);
43
+ this.name = "MastraChatRequestError";
44
+ }
45
+ }
46
+
36
47
  /** One memory thread returned by the native Mastra client. */
37
48
  export type MastraMemoryThread = ListMemoryThreadsResponse["threads"][number];
38
49
 
@@ -130,6 +141,7 @@ export class MastraPluginClient extends MastraClient {
130
141
  fetch: async (input, init) => {
131
142
  const response = await fetch(input, init);
132
143
  headers = new Headers(response.headers);
144
+ await assertChatResponse(response);
133
145
  return response;
134
146
  },
135
147
  });
@@ -160,6 +172,7 @@ export class MastraPluginClient extends MastraClient {
160
172
  fetch: async (input, init) => {
161
173
  const response = await fetch(input, init);
162
174
  headers = new Headers(response.headers);
175
+ await assertChatResponse(response);
163
176
  return response;
164
177
  },
165
178
  });
@@ -620,6 +633,38 @@ export class MastraPluginClient extends MastraClient {
620
633
  }
621
634
  }
622
635
 
636
+ async function assertChatResponse(response: Response): Promise<void> {
637
+ if (response.ok) return;
638
+ const body = await response
639
+ .clone()
640
+ .json()
641
+ .catch(() => undefined);
642
+ const message =
643
+ responseErrorMessage(body) ??
644
+ (await response
645
+ .clone()
646
+ .text()
647
+ .catch(() => ""));
648
+ throw new MastraChatRequestError(
649
+ message.trim() || `Chat request failed (${response.status})`,
650
+ response.status,
651
+ );
652
+ }
653
+
654
+ function responseErrorMessage(body: unknown): string | undefined {
655
+ if (!body || typeof body !== "object") return undefined;
656
+ const record = body as Record<string, unknown>;
657
+ for (const field of ["error", "message"] as const) {
658
+ const value = record[field];
659
+ if (typeof value === "string" && value.trim()) return value.trim();
660
+ if (value && typeof value === "object") {
661
+ const nested = (value as Record<string, unknown>).message;
662
+ if (typeof nested === "string" && nested.trim()) return nested.trim();
663
+ }
664
+ }
665
+ return undefined;
666
+ }
667
+
623
668
  /**
624
669
  * Read the Mastra plugin's `clientConfig()` payload (`basePath`,
625
670
  * default agent, registered agent list). One call per render; values
@@ -42,6 +42,7 @@ export type ThreadSession = {
42
42
  /** Map key for the classic single-thread chat (no explicit thread id). */
43
43
  export const DEFAULT_THREAD_SESSION_KEY = "__session__";
44
44
 
45
+ /** Create the empty transcript and run state used for a new conversation thread. */
45
46
  export function createThreadSession(): ThreadSession {
46
47
  return {
47
48
  messages: [],
@@ -61,6 +62,7 @@ export function createThreadSession(): ThreadSession {
61
62
  };
62
63
  }
63
64
 
65
+ /** Return whether a thread has a submitted or actively streaming run. */
64
66
  export function isSessionRunning(session: ThreadSession): boolean {
65
67
  return session.status === "submitted" || session.status === "streaming";
66
68
  }
@@ -98,6 +100,7 @@ export function reorderSteers<T extends QueuedSteer>(queue: T[], orderedIds: str
98
100
  return next;
99
101
  }
100
102
 
103
+ /** Resolve an explicit thread id or the shared key used by single-thread chat. */
101
104
  export function sessionKey(activeThreadId: string | undefined): string {
102
105
  return activeThreadId ?? DEFAULT_THREAD_SESSION_KEY;
103
106
  }