@mastra/react 1.15.1-alpha.3 → 1.15.1-alpha.4

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.
@@ -0,0 +1,15 @@
1
+ import { o as isObservabilityUnavailableError, s as isUnsupportedObservabilityOperationError } from "./query-utils-DvMERIwf.js";
2
+ // src/hooks/feedback/feedback-refetch-interval.ts
3
+ const FEEDBACK_REFETCH_INTERVAL_MS = 3e4;
4
+ const PENDING_FEEDBACK_REFETCH_INTERVAL_MS = 3e3;
5
+ /**
6
+ * Disables polling for unsupported or unavailable feedback storage. Otherwise polls every 30 seconds,
7
+ * or every 3 seconds while the list shows a comment the server has not returned yet.
8
+ */
9
+ function getFeedbackRefetchInterval(query, hasPendingFeedback = false) {
10
+ if (isUnsupportedObservabilityOperationError(query.state.error, "feedback") || isObservabilityUnavailableError(query.state.error)) return false;
11
+ return hasPendingFeedback ? PENDING_FEEDBACK_REFETCH_INTERVAL_MS : FEEDBACK_REFETCH_INTERVAL_MS;
12
+ }
13
+ export { getFeedbackRefetchInterval as t };
14
+
15
+ //# sourceMappingURL=feedback-refetch-interval-D7vqhO1C.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feedback-refetch-interval-D7vqhO1C.js","names":[],"sources":["../src/hooks/feedback/feedback-refetch-interval.ts"],"sourcesContent":["import { isObservabilityUnavailableError, isUnsupportedObservabilityOperationError } from '../query/query-utils';\n\nconst FEEDBACK_REFETCH_INTERVAL_MS = 30_000;\n\n// TODO: Feedback writes are eventually consistent on Mastra Platform (Pub/Sub -> ClickHouse writer batching), so a\n// just-submitted comment is not queryable right away. While one is pending, the list polls quickly to pick up the\n// stored record. Remove once the backend offers read-your-writes for feedback.\nconst PENDING_FEEDBACK_REFETCH_INTERVAL_MS = 3_000;\n\n/**\n * Disables polling for unsupported or unavailable feedback storage. Otherwise polls every 30 seconds,\n * or every 3 seconds while the list shows a comment the server has not returned yet.\n */\nexport function getFeedbackRefetchInterval(query: { state: { error: unknown } }, hasPendingFeedback = false) {\n if (\n isUnsupportedObservabilityOperationError(query.state.error, 'feedback') ||\n isObservabilityUnavailableError(query.state.error)\n ) {\n return false;\n }\n return hasPendingFeedback ? PENDING_FEEDBACK_REFETCH_INTERVAL_MS : FEEDBACK_REFETCH_INTERVAL_MS;\n}\n"],"mappings":";;AAEA,MAAM,+BAA+B;AAKrC,MAAM,uCAAuC;;;;;AAM7C,SAAgB,2BAA2B,OAAsC,qBAAqB,OAAO;CAC3G,IACE,yCAAyC,MAAM,MAAM,OAAO,UAAU,KACtE,gCAAgC,MAAM,MAAM,KAAK,GAEjD,OAAO;CAET,OAAO,qBAAqB,uCAAuC;AACrE"}
@@ -1,10 +1,14 @@
1
1
  const require_query_utils = require("./query-utils-BVgE_xxj.cjs");
2
2
  // src/hooks/feedback/feedback-refetch-interval.ts
3
3
  const FEEDBACK_REFETCH_INTERVAL_MS = 3e4;
4
- /** Disables polling for unsupported or unavailable feedback storage; otherwise retries every thirty seconds. */
5
- function getFeedbackRefetchInterval(query) {
4
+ const PENDING_FEEDBACK_REFETCH_INTERVAL_MS = 3e3;
5
+ /**
6
+ * Disables polling for unsupported or unavailable feedback storage. Otherwise polls every 30 seconds,
7
+ * or every 3 seconds while the list shows a comment the server has not returned yet.
8
+ */
9
+ function getFeedbackRefetchInterval(query, hasPendingFeedback = false) {
6
10
  if (require_query_utils.isUnsupportedObservabilityOperationError(query.state.error, "feedback") || require_query_utils.isObservabilityUnavailableError(query.state.error)) return false;
7
- return FEEDBACK_REFETCH_INTERVAL_MS;
11
+ return hasPendingFeedback ? PENDING_FEEDBACK_REFETCH_INTERVAL_MS : FEEDBACK_REFETCH_INTERVAL_MS;
8
12
  }
9
13
  Object.defineProperty(exports, "getFeedbackRefetchInterval", {
10
14
  enumerable: true,
@@ -13,4 +17,4 @@ Object.defineProperty(exports, "getFeedbackRefetchInterval", {
13
17
  }
14
18
  });
15
19
 
16
- //# sourceMappingURL=feedback-refetch-interval-BBcT9r8s.cjs.map
20
+ //# sourceMappingURL=feedback-refetch-interval-ZJMk81Sb.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feedback-refetch-interval-ZJMk81Sb.cjs","names":["isUnsupportedObservabilityOperationError","isObservabilityUnavailableError"],"sources":["../src/hooks/feedback/feedback-refetch-interval.ts"],"sourcesContent":["import { isObservabilityUnavailableError, isUnsupportedObservabilityOperationError } from '../query/query-utils';\n\nconst FEEDBACK_REFETCH_INTERVAL_MS = 30_000;\n\n// TODO: Feedback writes are eventually consistent on Mastra Platform (Pub/Sub -> ClickHouse writer batching), so a\n// just-submitted comment is not queryable right away. While one is pending, the list polls quickly to pick up the\n// stored record. Remove once the backend offers read-your-writes for feedback.\nconst PENDING_FEEDBACK_REFETCH_INTERVAL_MS = 3_000;\n\n/**\n * Disables polling for unsupported or unavailable feedback storage. Otherwise polls every 30 seconds,\n * or every 3 seconds while the list shows a comment the server has not returned yet.\n */\nexport function getFeedbackRefetchInterval(query: { state: { error: unknown } }, hasPendingFeedback = false) {\n if (\n isUnsupportedObservabilityOperationError(query.state.error, 'feedback') ||\n isObservabilityUnavailableError(query.state.error)\n ) {\n return false;\n }\n return hasPendingFeedback ? PENDING_FEEDBACK_REFETCH_INTERVAL_MS : FEEDBACK_REFETCH_INTERVAL_MS;\n}\n"],"mappings":";;AAEA,MAAM,+BAA+B;AAKrC,MAAM,uCAAuC;;;;;AAM7C,SAAgB,2BAA2B,OAAsC,qBAAqB,OAAO;CAC3G,IACEA,oBAAAA,yCAAyC,MAAM,MAAM,OAAO,UAAU,KACtEC,oBAAAA,gCAAgC,MAAM,MAAM,KAAK,GAEjD,OAAO;CAET,OAAO,qBAAqB,uCAAuC;AACrE"}
@@ -1,7 +1,10 @@
1
- /** Disables polling for unsupported or unavailable feedback storage; otherwise retries every thirty seconds. */
1
+ /**
2
+ * Disables polling for unsupported or unavailable feedback storage. Otherwise polls every 30 seconds,
3
+ * or every 3 seconds while the list shows a comment the server has not returned yet.
4
+ */
2
5
  export declare function getFeedbackRefetchInterval(query: {
3
6
  state: {
4
7
  error: unknown;
5
8
  };
6
- }): 30000 | false;
9
+ }, hasPendingFeedback?: boolean): 3000 | 30000 | false;
7
10
  //# sourceMappingURL=feedback-refetch-interval.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"feedback-refetch-interval.d.ts","sourceRoot":"","sources":["../../../src/hooks/feedback/feedback-refetch-interval.ts"],"names":[],"mappings":"AAIA,gHAAgH;AAChH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE;IAAE,KAAK,EAAE;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAA;CAAE,iBAQ9E"}
1
+ {"version":3,"file":"feedback-refetch-interval.d.ts","sourceRoot":"","sources":["../../../src/hooks/feedback/feedback-refetch-interval.ts"],"names":[],"mappings":"AASA;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE;IAAE,KAAK,EAAE;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAA;CAAE,EAAE,kBAAkB,UAAQ,wBAQ1G"}
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_mastra_client_context = require("../../mastra-client-context-DX8wJM9p.cjs");
3
- const require_feedback_refetch_interval = require("../../feedback-refetch-interval-BBcT9r8s.cjs");
3
+ const require_feedback_refetch_interval = require("../../feedback-refetch-interval-ZJMk81Sb.cjs");
4
4
  let _tanstack_react_query = require("@tanstack/react-query");
5
5
  // src/hooks/feedback/use-feedback.ts
6
6
  function useUpdateFeedbackReviewStatus({ queryOptions } = {}) {
@@ -1,3 +1,4 @@
1
1
  export * from './use-feedback.js';
2
2
  export * from './feedback-refetch-interval.js';
3
+ export type { FeedbackListItem, FeedbackListResponse } from './optimistic-feedback.js';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/feedback/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/feedback/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,6BAA6B,CAAC;AAC5C,YAAY,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { n as useMastraClient } from "../../mastra-client-context-DQ5z_ze9.js";
2
- import { t as getFeedbackRefetchInterval } from "../../feedback-refetch-interval-C12Nzb2r.js";
2
+ import { t as getFeedbackRefetchInterval } from "../../feedback-refetch-interval-D7vqhO1C.js";
3
3
  import { useMutation, useQueryClient } from "@tanstack/react-query";
4
4
  // src/hooks/feedback/use-feedback.ts
5
5
  function useUpdateFeedbackReviewStatus({ queryOptions } = {}) {
@@ -0,0 +1,44 @@
1
+ import type { MastraClient } from '@mastra/client-js';
2
+ import type { QueryClient } from '@tanstack/react-query';
3
+ type ListFeedbackResponse = Awaited<ReturnType<MastraClient['listFeedback']>>;
4
+ type ServerFeedbackItem = ListFeedbackResponse['feedback'][number];
5
+ /**
6
+ * A row of `useTraceFeedback` / `useSpanFeedback`. `isPending` marks a comment shown before the
7
+ * server returns it: it has no stored record yet, so review/delete actions should not target it.
8
+ */
9
+ export type FeedbackListItem = ServerFeedbackItem & {
10
+ isPending?: boolean;
11
+ };
12
+ export type FeedbackListResponse = Omit<ListFeedbackResponse, 'feedback'> & {
13
+ feedback: FeedbackListItem[];
14
+ };
15
+ /** How long a pending comment stays visible if the server never returns it (e.g. a lost write). */
16
+ export declare const OPTIMISTIC_FEEDBACK_TTL_MS = 60000;
17
+ export declare function addPendingFeedback(queryClient: QueryClient, { traceId, spanId, item }: {
18
+ traceId: string;
19
+ spanId?: string;
20
+ item: ServerFeedbackItem;
21
+ }): void;
22
+ export declare function discardPendingFeedback(queryClient: QueryClient, { traceId, spanId, feedbackId }: {
23
+ traceId: string;
24
+ spanId?: string;
25
+ feedbackId: string;
26
+ }): void;
27
+ /**
28
+ * Prepends pending comments the server does not return yet (newest first, flagged `isPending`)
29
+ * and counts them in the total.
30
+ */
31
+ export declare function mergePendingFeedback(data: ListFeedbackResponse, pending: ServerFeedbackItem[]): FeedbackListResponse;
32
+ /** Stops tracking pending comments once a fetched first page returns them. */
33
+ export declare function settlePendingFeedback(queryClient: QueryClient, { traceId, spanId }: {
34
+ traceId: string;
35
+ spanId?: string;
36
+ }, data: ListFeedbackResponse): void;
37
+ /** Pending comments for a trace (no `spanId`) or a span, to merge into the first page of its list. */
38
+ export declare function usePendingFeedback({ traceId, spanId, enabled, }: {
39
+ traceId: string;
40
+ spanId?: string;
41
+ enabled: boolean;
42
+ }): ServerFeedbackItem[];
43
+ export {};
44
+ //# sourceMappingURL=optimistic-feedback.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optimistic-feedback.d.ts","sourceRoot":"","sources":["../../../src/hooks/feedback/optimistic-feedback.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAczD,KAAK,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;AAC9E,KAAK,kBAAkB,GAAG,oBAAoB,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnE;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,kBAAkB,GAAG;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAC5E,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,oBAAoB,EAAE,UAAU,CAAC,GAAG;IAAE,QAAQ,EAAE,gBAAgB,EAAE,CAAA;CAAE,CAAC;AAE7G,mGAAmG;AACnG,eAAO,MAAM,0BAA0B,QAAS,CAAC;AAqBjD,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,WAAW,EACxB,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,kBAAkB,CAAA;CAAE,QAM1F;AAED,wBAAgB,sBAAsB,CACpC,WAAW,EAAE,WAAW,EACxB,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,QAG1F;AAID;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,kBAAkB,EAAE,GAAG,oBAAoB,CAapH;AAED,8EAA8E;AAC9E,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,WAAW,EACxB,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,EACzD,IAAI,EAAE,oBAAoB,QAI3B;AAED,sGAAsG;AACtG,wBAAgB,kBAAkB,CAAC,EACjC,OAAO,EACP,MAAM,EACN,OAAO,GACR,EAAE;IACD,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB,GAAG,kBAAkB,EAAE,CA0BvB"}
@@ -3,9 +3,10 @@ const require_mastra_client_context = require("../../mastra-client-context-DX8wJ
3
3
  const require_use_in_view = require("../../use-in-view-u7xWhAjL.cjs");
4
4
  const require_map = require("../../map-BGoWBTGy.cjs");
5
5
  const require_query_utils = require("../../query-utils-BVgE_xxj.cjs");
6
- const require_feedback_refetch_interval = require("../../feedback-refetch-interval-BBcT9r8s.cjs");
6
+ const require_feedback_refetch_interval = require("../../feedback-refetch-interval-ZJMk81Sb.cjs");
7
7
  let _mastra_client_js = require("@mastra/client-js");
8
8
  let react = require("react");
9
+ let _lukeed_uuid = require("@lukeed/uuid");
9
10
  let _tanstack_react_query = require("@tanstack/react-query");
10
11
  let _mastra_core_observability = require("@mastra/core/observability");
11
12
  // src/hooks/traces/discovery-cache.ts
@@ -1220,33 +1221,173 @@ const useTraces = ({ filters, listMode = "traces", polling = {}, enabled = true,
1220
1221
  recentlyAddedKeys
1221
1222
  };
1222
1223
  };
1224
+ // src/hooks/feedback/optimistic-feedback.ts
1225
+ /** How long a pending comment stays visible if the server never returns it (e.g. a lost write). */
1226
+ const OPTIMISTIC_FEEDBACK_TTL_MS = 6e4;
1227
+ const NO_PENDING = [];
1228
+ const pendingFeedbackKey = (traceId, spanId) => [
1229
+ "feedback-optimistic",
1230
+ traceId,
1231
+ spanId || null
1232
+ ];
1233
+ function removePendingFeedback(queryClient, traceId, spanId, shouldRemove) {
1234
+ queryClient.setQueryData(pendingFeedbackKey(traceId, spanId), (entries) => {
1235
+ if (!entries) return entries;
1236
+ const kept = entries.filter((entry) => !shouldRemove(entry));
1237
+ return kept.length === entries.length ? entries : kept;
1238
+ });
1239
+ }
1240
+ function addPendingFeedback(queryClient, { traceId, spanId, item }) {
1241
+ queryClient.setQueryData(pendingFeedbackKey(traceId, spanId), (entries = []) => [...entries.filter((entry) => entry.item.feedbackId !== item.feedbackId), {
1242
+ item,
1243
+ expiresAt: Date.now() + OPTIMISTIC_FEEDBACK_TTL_MS
1244
+ }]);
1245
+ }
1246
+ function discardPendingFeedback(queryClient, { traceId, spanId, feedbackId }) {
1247
+ removePendingFeedback(queryClient, traceId, spanId, (entry) => entry.item.feedbackId === feedbackId);
1248
+ }
1249
+ const timeOf = (item) => new Date(item.timestamp).getTime();
1250
+ /**
1251
+ * Prepends pending comments the server does not return yet (newest first, flagged `isPending`)
1252
+ * and counts them in the total.
1253
+ */
1254
+ function mergePendingFeedback(data, pending) {
1255
+ if (pending.length === 0) return data;
1256
+ const serverIds = new Set(data.feedback.map((item) => item.feedbackId));
1257
+ const missing = pending.filter((item) => !serverIds.has(item.feedbackId)).sort((a, b) => timeOf(b) - timeOf(a)).map((item) => ({
1258
+ ...item,
1259
+ isPending: true
1260
+ }));
1261
+ if (missing.length === 0) return data;
1262
+ return {
1263
+ ...data,
1264
+ feedback: [...missing, ...data.feedback],
1265
+ ...data.pagination ? { pagination: {
1266
+ ...data.pagination,
1267
+ total: data.pagination.total + missing.length
1268
+ } } : {}
1269
+ };
1270
+ }
1271
+ /** Stops tracking pending comments once a fetched first page returns them. */
1272
+ function settlePendingFeedback(queryClient, { traceId, spanId }, data) {
1273
+ const serverIds = new Set(data.feedback.map((item) => item.feedbackId));
1274
+ removePendingFeedback(queryClient, traceId, spanId, (entry) => serverIds.has(entry.item.feedbackId));
1275
+ }
1276
+ /** Pending comments for a trace (no `spanId`) or a span, to merge into the first page of its list. */
1277
+ function usePendingFeedback({ traceId, spanId, enabled }) {
1278
+ const queryClient = (0, _tanstack_react_query.useQueryClient)();
1279
+ const queryKey = pendingFeedbackKey(traceId, spanId);
1280
+ const { data: entries } = (0, _tanstack_react_query.useQuery)({
1281
+ queryKey,
1282
+ queryFn: () => queryClient.getQueryData(queryKey) ?? [],
1283
+ enabled: false,
1284
+ staleTime: Infinity,
1285
+ gcTime: OPTIMISTIC_FEEDBACK_TTL_MS
1286
+ });
1287
+ (0, react.useEffect)(() => {
1288
+ if (!entries?.length) return;
1289
+ const nextExpiry = Math.min(...entries.map((entry) => entry.expiresAt));
1290
+ const timer = setTimeout(() => removePendingFeedback(queryClient, traceId, spanId, (entry) => entry.expiresAt <= Date.now()), Math.max(0, nextExpiry - Date.now()));
1291
+ return () => clearTimeout(timer);
1292
+ }, [
1293
+ entries,
1294
+ queryClient,
1295
+ traceId,
1296
+ spanId
1297
+ ]);
1298
+ return (0, react.useMemo)(() => enabled && entries?.length ? entries.map((entry) => entry.item) : NO_PENDING, [enabled, entries]);
1299
+ }
1223
1300
  // src/hooks/traces/use-create-feedback.tsx
1224
1301
  /**
1225
- * Submits a free-text comment as a feedback record, scoped to the trace or to a
1226
- * single span, then invalidates the matching feedback list so it refreshes.
1302
+ * Identity generated per submit, so the optimistic comment (added in onMutate) and the posted
1303
+ * record (sent in mutationFn) share one feedbackId. TanStack passes the same variables object
1304
+ * to every callback and retry of a mutation run; `mutate` copies the caller's variables so a
1305
+ * reused object still gets a fresh identity on each submit.
1306
+ *
1307
+ * The timestamp comes from the client clock, so a skewed clock can order a pending comment
1308
+ * differently from how the server later sorts the stored one.
1309
+ */
1310
+ const identities = /* @__PURE__ */ new WeakMap();
1311
+ function identityFor(variables) {
1312
+ let identity = identities.get(variables);
1313
+ if (!identity) {
1314
+ identity = {
1315
+ feedbackId: (0, _lukeed_uuid.v4)(),
1316
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1317
+ };
1318
+ identities.set(variables, identity);
1319
+ }
1320
+ return identity;
1321
+ }
1322
+ /**
1323
+ * Submits a free-text comment as a feedback record, scoped to the trace or to a single span.
1324
+ * The comment shows up right away in `useTraceFeedback` / `useSpanFeedback`, is removed again
1325
+ * if the submit fails, and the matching feedback list is invalidated on success.
1326
+ *
1327
+ * Caller `onMutate`, `onError` and `onSuccess` run in addition to this behavior, not instead of it.
1227
1328
  */
1228
1329
  const useCreateFeedback = ({ traceId, spanId, contextMode, feedbackUserId, queryOptions }) => {
1229
1330
  const client = require_mastra_client_context.useMastraClient();
1230
1331
  const queryClient = (0, _tanstack_react_query.useQueryClient)();
1231
- return (0, _tanstack_react_query.useMutation)({
1232
- mutationFn: ({ text }) => client.createFeedback({
1332
+ const mutation = (0, _tanstack_react_query.useMutation)({
1333
+ mutationFn: (variables) => client.createFeedback({
1233
1334
  ...contextMode !== void 0 ? { contextMode } : {},
1234
1335
  feedback: {
1336
+ ...identityFor(variables),
1235
1337
  traceId,
1236
1338
  ...spanId ? { spanId } : {},
1237
1339
  ...feedbackUserId !== void 0 ? { feedbackUserId } : {},
1238
1340
  feedbackType: "comment",
1239
1341
  feedbackSource: "user",
1240
- value: text
1342
+ value: variables.text
1241
1343
  }
1242
1344
  }),
1243
- onSuccess: () => queryClient.invalidateQueries({ queryKey: spanId ? [
1244
- "span-feedback",
1245
- traceId,
1246
- spanId
1247
- ] : ["trace-feedback", traceId] }),
1248
- ...queryOptions
1345
+ ...queryOptions,
1346
+ onMutate: (...args) => {
1347
+ const [variables] = args;
1348
+ const { feedbackId, timestamp } = identityFor(variables);
1349
+ addPendingFeedback(queryClient, {
1350
+ traceId,
1351
+ spanId,
1352
+ item: {
1353
+ feedbackId,
1354
+ timestamp,
1355
+ traceId,
1356
+ ...spanId ? { spanId } : {},
1357
+ ...feedbackUserId !== void 0 ? { feedbackUserId } : {},
1358
+ feedbackType: "comment",
1359
+ feedbackSource: "user",
1360
+ value: variables.text,
1361
+ reviewStatus: "needs-review"
1362
+ }
1363
+ });
1364
+ return queryOptions?.onMutate?.(...args);
1365
+ },
1366
+ onError: (...args) => {
1367
+ const [, variables] = args;
1368
+ discardPendingFeedback(queryClient, {
1369
+ traceId,
1370
+ spanId,
1371
+ feedbackId: identityFor(variables).feedbackId
1372
+ });
1373
+ return queryOptions?.onError?.(...args);
1374
+ },
1375
+ onSuccess: async (...args) => {
1376
+ await Promise.all([queryClient.invalidateQueries({ queryKey: spanId ? [
1377
+ "span-feedback",
1378
+ traceId,
1379
+ spanId
1380
+ ] : ["trace-feedback", traceId] }), queryOptions?.onSuccess?.(...args)]);
1381
+ }
1249
1382
  });
1383
+ const { mutate: mutateRun, mutateAsync: mutateAsyncRun } = mutation;
1384
+ const mutate = (0, react.useCallback)((variables, options) => mutateRun({ ...variables }, options), [mutateRun]);
1385
+ const mutateAsync = (0, react.useCallback)((variables, options) => mutateAsyncRun({ ...variables }, options), [mutateAsyncRun]);
1386
+ return {
1387
+ ...mutation,
1388
+ mutate,
1389
+ mutateAsync
1390
+ };
1250
1391
  };
1251
1392
  // src/hooks/traces/use-delete-feedback.tsx
1252
1393
  /**
@@ -1270,12 +1411,25 @@ const useDeleteFeedback = ({ traceId, spanId, queryOptions }) => {
1270
1411
  /**
1271
1412
  * Feedback scoped to a single span. Both identifiers are required: without a `spanId`
1272
1413
  * the query stays disabled rather than falling back to trace-wide feedback.
1414
+ * The first page also shows comments submitted through `useCreateFeedback` that the server
1415
+ * does not return yet (feedback writes are eventually consistent on Mastra Platform).
1273
1416
  *
1274
1417
  * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.
1275
1418
  */
1276
1419
  const useSpanFeedback = ({ traceId = "", spanId = "", page, queryOptions }) => {
1277
1420
  const client = require_mastra_client_context.useMastraClient();
1421
+ const queryClient = (0, _tanstack_react_query.useQueryClient)();
1278
1422
  const pageNumber = page ?? 0;
1423
+ const pending = usePendingFeedback({
1424
+ traceId,
1425
+ spanId,
1426
+ enabled: pageNumber === 0
1427
+ });
1428
+ const { select: callerSelect, ...restQueryOptions } = queryOptions ?? {};
1429
+ const select = (0, react.useCallback)((data) => {
1430
+ const merged = mergePendingFeedback(data, pending);
1431
+ return callerSelect ? callerSelect(merged) : merged;
1432
+ }, [pending, callerSelect]);
1279
1433
  return (0, _tanstack_react_query.useQuery)({
1280
1434
  queryKey: [
1281
1435
  "span-feedback",
@@ -1283,62 +1437,85 @@ const useSpanFeedback = ({ traceId = "", spanId = "", page, queryOptions }) => {
1283
1437
  spanId,
1284
1438
  pageNumber
1285
1439
  ],
1286
- queryFn: () => client.listFeedback({
1287
- filters: {
1440
+ queryFn: async ({ signal }) => {
1441
+ const data = await client.listFeedback({
1442
+ filters: {
1443
+ traceId,
1444
+ spanId
1445
+ },
1446
+ pagination: {
1447
+ page: pageNumber,
1448
+ perPage: 10
1449
+ }
1450
+ });
1451
+ if (pageNumber === 0 && !signal.aborted) settlePendingFeedback(queryClient, {
1288
1452
  traceId,
1289
1453
  spanId
1290
- },
1291
- pagination: {
1292
- page: pageNumber,
1293
- perPage: 10
1294
- }
1295
- }),
1296
- refetchInterval: require_feedback_refetch_interval.getFeedbackRefetchInterval,
1454
+ }, data);
1455
+ return data;
1456
+ },
1457
+ select,
1458
+ refetchInterval: (query) => require_feedback_refetch_interval.getFeedbackRefetchInterval(query, pending.length > 0),
1297
1459
  gcTime: 0,
1298
1460
  staleTime: 0,
1299
- ...queryOptions
1461
+ ...restQueryOptions
1300
1462
  });
1301
1463
  };
1302
1464
  // src/hooks/traces/use-trace-feedback.tsx
1303
- /** Loads a page of trace-level feedback and stops polling when storage cannot serve feedback. */
1304
1465
  /**
1466
+ * Loads a page of trace-level feedback and stops polling when storage cannot serve feedback.
1467
+ * The first page also shows comments submitted through `useCreateFeedback` that the server
1468
+ * does not return yet (feedback writes are eventually consistent on Mastra Platform).
1469
+ *
1305
1470
  * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.
1306
1471
  */
1307
1472
  const useTraceFeedback = ({ traceId = "", page, queryOptions }) => {
1308
1473
  const client = require_mastra_client_context.useMastraClient();
1474
+ const queryClient = (0, _tanstack_react_query.useQueryClient)();
1309
1475
  const pageNumber = page ?? 0;
1476
+ const pending = usePendingFeedback({
1477
+ traceId,
1478
+ enabled: pageNumber === 0
1479
+ });
1480
+ const { select: callerSelect, ...restQueryOptions } = queryOptions ?? {};
1481
+ const select = (0, react.useCallback)((data) => {
1482
+ const merged = mergePendingFeedback(data, pending);
1483
+ const feedback = merged.feedback.filter((item) => !item.spanId);
1484
+ const traceLevel = merged.pagination ? {
1485
+ ...merged,
1486
+ feedback,
1487
+ pagination: {
1488
+ ...merged.pagination,
1489
+ total: feedback.length
1490
+ }
1491
+ } : {
1492
+ ...merged,
1493
+ feedback
1494
+ };
1495
+ return callerSelect ? callerSelect(traceLevel) : traceLevel;
1496
+ }, [pending, callerSelect]);
1310
1497
  return (0, _tanstack_react_query.useQuery)({
1311
1498
  queryKey: [
1312
1499
  "trace-feedback",
1313
1500
  traceId,
1314
1501
  pageNumber
1315
1502
  ],
1316
- queryFn: () => client.listFeedback({
1317
- filters: { traceId },
1318
- pagination: {
1319
- page: pageNumber,
1320
- perPage: 10
1321
- }
1322
- }),
1323
- select: (data) => {
1324
- const feedback = data.feedback.filter((item) => !item.spanId);
1325
- if (!data.pagination) return {
1326
- ...data,
1327
- feedback
1328
- };
1329
- return {
1330
- ...data,
1331
- feedback,
1503
+ queryFn: async ({ signal }) => {
1504
+ const data = await client.listFeedback({
1505
+ filters: { traceId },
1332
1506
  pagination: {
1333
- ...data.pagination,
1334
- total: feedback.length
1507
+ page: pageNumber,
1508
+ perPage: 10
1335
1509
  }
1336
- };
1510
+ });
1511
+ if (pageNumber === 0 && !signal.aborted) settlePendingFeedback(queryClient, { traceId }, data);
1512
+ return data;
1337
1513
  },
1338
- refetchInterval: require_feedback_refetch_interval.getFeedbackRefetchInterval,
1514
+ select,
1515
+ refetchInterval: (query) => require_feedback_refetch_interval.getFeedbackRefetchInterval(query, pending.length > 0),
1339
1516
  gcTime: 0,
1340
1517
  staleTime: 0,
1341
- ...queryOptions
1518
+ ...restQueryOptions
1342
1519
  });
1343
1520
  };
1344
1521
  // src/hooks/traces/use-trace-metadata-filter-fields.ts