@dbx-tools/appkit-mastra 0.9.176 → 0.9.179

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/src/chat.ts CHANGED
@@ -4,16 +4,23 @@
4
4
  * @module
5
5
  */
6
6
 
7
- import { log, toError, errorMessage, isRecord } from "@dbx-tools/shared-core";
7
+ import { log, toError, errorMessage, isRecord, sleep } from "@dbx-tools/shared-core";
8
8
  import {
9
9
  MASTRA_ROUTES,
10
+ RESPONSE_COMPLETE_PART_TYPE,
11
+ THREAD_TITLE_PART_TYPE,
10
12
  isStaleMastraResumeError,
11
13
  STALE_MASTRA_RESUME_STREAM_TEXT,
12
14
  } from "@dbx-tools/shared-mastra";
13
15
  import { handleChatStream, toAISdkStream } from "@mastra/ai-sdk";
16
+ import type { Agent } from "@mastra/core/agent";
14
17
  import { isEventedAgent } from "@mastra/core/agent/durable";
18
+ import type { EventedAgent } from "@mastra/core/agent/durable";
19
+ import type { RequestContext } from "@mastra/core/request-context";
15
20
  import { registerApiRoute } from "@mastra/core/server";
21
+ import type { DataChunkType, MastraStreamTransform } from "@mastra/core/stream";
16
22
  import { createUIMessageStreamResponse } from "ai";
23
+ import type { UIMessageChunk } from "ai";
17
24
 
18
25
  import type { MastraPluginConfig } from "./config.ts";
19
26
  import {
@@ -23,11 +30,220 @@ import {
23
30
  FOLLOW_UP_USER_TEXT_KEY,
24
31
  resolveFollowUpSuggestionsConfig,
25
32
  } from "./follow-up-suggestions.ts";
26
- import { withRequestMemoryScope } from "./memory.ts";
33
+ import { requestMemoryScope, withRequestMemoryScope } from "./memory.ts";
34
+ import { titleModel, TITLE_INSTRUCTIONS } from "./summarize.ts";
27
35
  import { activeChatTracingOptions } from "./telemetry.ts";
28
36
 
29
37
  const logger = log.logger("mastra/chat");
30
38
 
39
+ /** Keep a quiet chat stream below the Databricks Apps ingress idle window. */
40
+ export const CHAT_STREAM_KEEPALIVE_MS = 10_000;
41
+
42
+ /** Bound reconnects to producerless durable-run topics before ingress times out. */
43
+ export const CHAT_RUN_OBSERVE_IDLE_TIMEOUT_MS = 15_000;
44
+
45
+ /** Maximum age for a persisted `running` snapshot without a local producer. */
46
+ const CHAT_REMOTE_RUN_FRESHNESS_MS = 60_000;
47
+
48
+ const chatStreamHeartbeat = (): UIMessageChunk => ({
49
+ type: "data-stream-heartbeat",
50
+ data: null,
51
+ transient: true,
52
+ });
53
+
54
+ /** Mark the answer complete while post-turn processors continue before finish. */
55
+ export function buildResponseCompleteTransform(): MastraStreamTransform {
56
+ return () => {
57
+ let emitted = false;
58
+ return new TransformStream({
59
+ transform(chunk, controller) {
60
+ controller.enqueue(chunk);
61
+ if (
62
+ !emitted &&
63
+ chunk.type === "step-finish" &&
64
+ chunk.payload.stepResult.isContinued !== true &&
65
+ (chunk.payload.output.toolCalls?.length ?? 0) === 0
66
+ ) {
67
+ emitted = true;
68
+ const part: DataChunkType = {
69
+ type: RESPONSE_COMPLETE_PART_TYPE,
70
+ data: null,
71
+ transient: true,
72
+ };
73
+ controller.enqueue(part as never);
74
+ }
75
+ },
76
+ });
77
+ };
78
+ }
79
+
80
+ function chatStreamTransforms(
81
+ runId: string,
82
+ generatedFollowUps: boolean,
83
+ titleSettled?: Promise<string | undefined>,
84
+ ): readonly MastraStreamTransform[] {
85
+ return [
86
+ buildResponseCompleteTransform(),
87
+ ...(generatedFollowUps ? [buildFollowUpSuggestionsTransform(runId)] : []),
88
+ ...(titleSettled ? [buildThreadTitleTransform(titleSettled)] : []),
89
+ ];
90
+ }
91
+
92
+ type ThreadTitleAgent = Pick<Agent, "generateTitleFromUserMessage" | "getMemory">;
93
+
94
+ /** Start one title generation and persistence future for a new thread. */
95
+ export function startThreadTitle(options: {
96
+ agent: ThreadTitleAgent;
97
+ config: MastraPluginConfig;
98
+ requestContext: RequestContext;
99
+ userText: string | undefined;
100
+ }): Promise<string | undefined> {
101
+ const { agent, config, requestContext, userText } = options;
102
+ const scope = requestMemoryScope(requestContext);
103
+ if (!scope || !userText) return Promise.resolve(undefined);
104
+
105
+ return (async () => {
106
+ const memory = await agent.getMemory({ requestContext });
107
+ if (!memory) return undefined;
108
+ const existing = await memory.getThreadById({ threadId: scope.thread });
109
+ if (existing?.title?.trim()) return undefined;
110
+
111
+ const title = (
112
+ await agent.generateTitleFromUserMessage({
113
+ message: userText,
114
+ requestContext,
115
+ model: titleModel(config),
116
+ instructions: TITLE_INSTRUCTIONS,
117
+ })
118
+ )?.trim();
119
+ if (!title) return undefined;
120
+
121
+ const current = await memory.getThreadById({ threadId: scope.thread });
122
+ if (current?.title?.trim()) return undefined;
123
+ if (current) {
124
+ await memory.updateThread({ id: scope.thread, title });
125
+ return title;
126
+ }
127
+ await memory.createThread({
128
+ threadId: scope.thread,
129
+ resourceId: scope.resource,
130
+ title,
131
+ });
132
+ return title;
133
+ })().catch((error) => {
134
+ logger.warn("thread title generation failed", { error: toError(error) });
135
+ return undefined;
136
+ });
137
+ }
138
+
139
+ /** Hold the terminal finish until the shared title future has persisted. */
140
+ export function buildThreadTitleTransform(
141
+ titleSettled: Promise<string | undefined>,
142
+ ): MastraStreamTransform {
143
+ return () =>
144
+ new TransformStream({
145
+ async transform(chunk, controller) {
146
+ if (chunk.type === "finish") {
147
+ const title = await titleSettled;
148
+ if (title) {
149
+ const part: DataChunkType = {
150
+ type: THREAD_TITLE_PART_TYPE,
151
+ data: { title },
152
+ transient: true,
153
+ };
154
+ controller.enqueue(part as never);
155
+ }
156
+ }
157
+ controller.enqueue(chunk);
158
+ },
159
+ });
160
+ }
161
+
162
+ function deferredChatStream(open: () => Promise<ReadableStream<UIMessageChunk>>): Response {
163
+ let reader: ReadableStreamDefaultReader<UIMessageChunk> | undefined;
164
+ let timer: ReturnType<typeof setInterval> | undefined;
165
+ let settled = false;
166
+
167
+ const stream = new ReadableStream<UIMessageChunk>({
168
+ start(controller) {
169
+ const writeHeartbeat = () => {
170
+ if (!settled) controller.enqueue(chatStreamHeartbeat());
171
+ };
172
+ writeHeartbeat();
173
+ timer = setInterval(writeHeartbeat, CHAT_STREAM_KEEPALIVE_MS);
174
+ timer.unref?.();
175
+
176
+ void (async () => {
177
+ try {
178
+ const opened = await open();
179
+ if (settled) {
180
+ await opened.cancel();
181
+ return;
182
+ }
183
+ reader = opened.getReader();
184
+ while (true) {
185
+ const { done, value } = await reader.read();
186
+ if (done) break;
187
+ controller.enqueue(value);
188
+ }
189
+ } catch (error) {
190
+ if (!settled) {
191
+ controller.enqueue({ type: "error", errorText: errorMessage(error) });
192
+ }
193
+ } finally {
194
+ const shouldClose = !settled;
195
+ settled = true;
196
+ if (timer) clearInterval(timer);
197
+ reader?.releaseLock();
198
+ if (shouldClose) controller.close();
199
+ }
200
+ })();
201
+ },
202
+ async cancel(reason) {
203
+ settled = true;
204
+ if (timer) clearInterval(timer);
205
+ await reader?.cancel(reason);
206
+ },
207
+ });
208
+ return createUIMessageStreamResponse({
209
+ stream,
210
+ keepAliveMs: CHAT_STREAM_KEEPALIVE_MS,
211
+ });
212
+ }
213
+
214
+ async function backgroundRunAvailable(agent: EventedAgent, runId: string): Promise<boolean> {
215
+ if (agent.runRegistry.has(runId)) return true;
216
+ const active = await agent.listActiveRuns();
217
+ const run = active.runs.find((candidate) => candidate.runId === runId);
218
+ if (!run) return false;
219
+ const updatedAt =
220
+ run.updatedAt instanceof Date ? run.updatedAt.getTime() : Date.parse(String(run.updatedAt));
221
+ return Number.isFinite(updatedAt) && Date.now() - updatedAt <= CHAT_REMOTE_RUN_FRESHNESS_MS;
222
+ }
223
+
224
+ async function observeBackgroundRun(agent: EventedAgent, runId: string) {
225
+ const available = await Promise.race([
226
+ backgroundRunAvailable(agent, runId),
227
+ sleep(CHAT_RUN_OBSERVE_IDLE_TIMEOUT_MS).then(() => false),
228
+ ]);
229
+ if (!available) throw new Error(`Run ${runId} is no longer available`);
230
+
231
+ const pending = agent.observe(runId, {
232
+ idleTimeoutMs: CHAT_RUN_OBSERVE_IDLE_TIMEOUT_MS,
233
+ isAlive: () =>
234
+ Promise.race([
235
+ backgroundRunAvailable(agent, runId),
236
+ sleep(CHAT_RUN_OBSERVE_IDLE_TIMEOUT_MS).then(() => false),
237
+ ]),
238
+ });
239
+ return Promise.race([
240
+ pending,
241
+ sleep(CHAT_RUN_OBSERVE_IDLE_TIMEOUT_MS).then(() => {
242
+ throw new Error(`Run ${runId} is no longer available`);
243
+ }),
244
+ ]);
245
+ }
246
+
31
247
  /** Client request cancellation is detached only for evented background agents. */
32
248
  export function agentAbortSignal(agent: unknown, signal: AbortSignal): AbortSignal | undefined {
33
249
  return isEventedAgent(agent) ? undefined : signal;
@@ -86,41 +302,46 @@ export function agentChatRoutes(config: MastraPluginConfig = {}) {
86
302
  throw new Error('Query parameter "status" must be "draft" or "published"');
87
303
  }
88
304
  const status = rawStatus as "draft" | "published" | undefined;
89
- const abortSignal = agentAbortSignal(mastra.getAgentById(agentId), context.req.raw.signal);
305
+ const agent = mastra.getAgentById(agentId);
306
+ const abortSignal = agentAbortSignal(agent, context.req.raw.signal);
90
307
  const runId =
91
308
  typeof params.runId === "string" && params.runId ? params.runId : crypto.randomUUID();
92
309
  const userText = latestUserText(params.messages);
93
310
  if (userText) requestContext.setRaw(FOLLOW_UP_USER_TEXT_KEY, userText);
94
311
  requestContext.setRaw(FOLLOW_UP_NATIVE_QUESTIONS_KEY, []);
95
312
  requestContext.setRaw(FOLLOW_UP_RUN_ID_KEY, runId);
313
+ const titleSettled = startThreadTitle({ agent, config, requestContext, userText });
96
314
  const tracingOptions = activeChatTracingOptions(undefined);
97
- const stream = await handleChatStream({
98
- mastra,
99
- agentId,
100
- ...(versionId
101
- ? { agentVersion: { versionId } }
102
- : status
103
- ? { agentVersion: { status } }
104
- : {}),
105
- params: withRequestMemoryScope(
106
- {
107
- ...params,
108
- runId,
315
+ return deferredChatStream(async () => {
316
+ return handleChatStream({
317
+ mastra,
318
+ agentId,
319
+ ...(versionId
320
+ ? { agentVersion: { versionId } }
321
+ : status
322
+ ? { agentVersion: { status } }
323
+ : {}),
324
+ params: withRequestMemoryScope(
325
+ {
326
+ ...params,
327
+ runId,
328
+ requestContext,
329
+ ...(tracingOptions ? { tracingOptions } : {}),
330
+ ...(abortSignal ? { abortSignal } : {}),
331
+ },
109
332
  requestContext,
110
- ...(tracingOptions ? { tracingOptions } : {}),
111
- ...(abortSignal ? { abortSignal } : {}),
112
- },
113
- requestContext,
114
- ),
115
- ...(followUps && followUps.mode !== "genie"
116
- ? { experimentalTransform: buildFollowUpSuggestionsTransform(runId) }
117
- : {}),
118
- version: "v7",
119
- sendReasoning,
120
- sendSources: true,
121
- onError: serializeChatStreamError,
333
+ ),
334
+ experimentalTransform: chatStreamTransforms(
335
+ runId,
336
+ Boolean(followUps && followUps.mode !== "genie"),
337
+ titleSettled,
338
+ ),
339
+ version: "v7",
340
+ sendReasoning,
341
+ sendSources: true,
342
+ onError: serializeChatStreamError,
343
+ });
122
344
  });
123
- return createUIMessageStreamResponse({ stream });
124
345
  },
125
346
  }),
126
347
  registerApiRoute(runPath, {
@@ -137,18 +358,20 @@ export function agentChatRoutes(config: MastraPluginConfig = {}) {
137
358
  { status: 409 },
138
359
  );
139
360
  }
140
- const observed = await agent.observe(runId);
141
- const stream = toAISdkStream(observed.output, {
142
- from: "agent",
143
- ...(followUps && followUps.mode !== "genie"
144
- ? { experimentalTransform: buildFollowUpSuggestionsTransform(runId) }
145
- : {}),
146
- version: "v7",
147
- sendReasoning,
148
- sendSources: true,
149
- onError: serializeChatStreamError,
361
+ return deferredChatStream(async () => {
362
+ const observed = await observeBackgroundRun(agent, runId);
363
+ return toAISdkStream(observed.output, {
364
+ from: "agent",
365
+ experimentalTransform: chatStreamTransforms(
366
+ runId,
367
+ Boolean(followUps && followUps.mode !== "genie"),
368
+ ),
369
+ version: "v7",
370
+ sendReasoning,
371
+ sendSources: true,
372
+ onError: serializeChatStreamError,
373
+ });
150
374
  });
151
- return createUIMessageStreamResponse({ stream });
152
375
  },
153
376
  }),
154
377
  registerApiRoute(abortPath, {
package/src/config.ts CHANGED
@@ -275,8 +275,9 @@ export interface MastraPluginConfig extends BasePluginConfig {
275
275
  /** Native Mastra workspace tool availability, approval, and hook configuration. */
276
276
  workspaceTools?: WorkspaceToolsConfig;
277
277
  /**
278
- * Use Mastra's native workspace skill manager. Defaults to true. Pass an
279
- * object to tune on-demand discovery and loaded-skill state.
278
+ * Use Mastra's native workspace-file skill manager. Defaults to true. Pass
279
+ * false to disable workspace-file discovery on auto-created workspaces, or
280
+ * an object to tune its process-cached, asynchronously refreshed index.
280
281
  */
281
282
  workspaceSkills?: boolean | MastraWorkspaceSkillsConfig;
282
283
  /**
@@ -541,9 +542,10 @@ export interface MastraPluginConfig extends BasePluginConfig {
541
542
  */
542
543
  observability?: boolean;
543
544
  /**
544
- * Feedback destination. `auto` prefers Genie when Genie Agent Mode is
545
- * available, otherwise MLflow when tracing is configured, otherwise none.
546
- * Explicit `genie` or `mlflow` fails startup when unavailable.
545
+ * Feedback destinations. `auto` enables Genie for Genie-backed messages and
546
+ * MLflow for other traced agent responses when each capability is available.
547
+ * Explicit `genie` or `mlflow` restricts feedback to that destination and
548
+ * fails startup when unavailable.
547
549
  */
548
550
  feedbackMode?: FeedbackModeOption;
549
551
  /**
@@ -748,7 +750,7 @@ export const MASTRA_CONFIG_SCHEMA: ConfigSchema = {
748
750
  workspaceSkills: {
749
751
  type: ["boolean", "object"],
750
752
  description:
751
- "Use Mastra's native on-demand workspace skill search and writable skill saves. Defaults to true.",
753
+ "Use Mastra's native process-cached workspace-file skill search with asynchronous refresh. Defaults to true; false disables workspace-file discovery on auto-created workspaces.",
752
754
  },
753
755
  workspaceTools: {
754
756
  type: "object",
@@ -835,7 +837,7 @@ export const MASTRA_CONFIG_SCHEMA: ConfigSchema = {
835
837
  type: "string",
836
838
  enum: ["auto", "genie", "mlflow", "none"],
837
839
  description:
838
- "Feedback destination. Auto prefers Genie in Genie Agent Mode, otherwise MLflow when configured, otherwise none. Explicit unavailable modes fail startup.",
840
+ "Feedback destinations. Auto routes Genie-backed messages to Genie and other traced responses to MLflow when available. Explicit unavailable modes fail startup.",
839
841
  },
840
842
  mcp: {
841
843
  type: ["boolean", "object"],
@@ -5,7 +5,10 @@
5
5
  */
6
6
 
7
7
  import { ConfigurationError } from "@databricks/appkit";
8
- import type { FeedbackMode, FeedbackModeOption } from "@dbx-tools/shared-mastra";
8
+ import type {
9
+ FeedbackDestination,
10
+ FeedbackModeOption,
11
+ } from "@dbx-tools/shared-mastra";
9
12
 
10
13
  /** Runtime availability used by automatic feedback selection. */
11
14
  export interface FeedbackAvailability {
@@ -15,19 +18,20 @@ export interface FeedbackAvailability {
15
18
  mlflow: boolean;
16
19
  }
17
20
 
18
- /** Resolve auto feedback or fail an explicitly unavailable destination. */
19
- export function resolveFeedbackMode(
21
+ /** Resolve available feedback destinations or fail an explicit unavailable choice. */
22
+ export function resolveFeedbackModes(
20
23
  configured: FeedbackModeOption | undefined,
21
24
  availability: FeedbackAvailability,
22
- ): FeedbackMode {
25
+ ): FeedbackDestination[] {
23
26
  const mode = configured ?? "auto";
24
- if (mode === "none") return "none";
27
+ if (mode === "none") return [];
25
28
  if (mode === "auto") {
26
- if (availability.genie) return "genie";
27
- if (availability.mlflow) return "mlflow";
28
- return "none";
29
+ return [
30
+ ...(availability.genie ? (["genie"] as const) : []),
31
+ ...(availability.mlflow ? (["mlflow"] as const) : []),
32
+ ];
29
33
  }
30
- if (availability[mode]) return mode;
34
+ if (availability[mode]) return [mode];
31
35
  throw new ConfigurationError(
32
36
  `mastra: feedbackMode "${mode}" is unavailable in this configuration`,
33
37
  { context: { feedbackMode: mode } },
@@ -4,20 +4,19 @@
4
4
  * @module
5
5
  */
6
6
 
7
- import { log, combineAbortSignals, isRecord, toError, toDescription } from "@dbx-tools/shared-core";
7
+ import { log, combineAbortSignals, toError, toDescription } from "@dbx-tools/shared-core";
8
8
  import {
9
9
  FOLLOW_UP_SUGGESTIONS_PART_TYPE,
10
10
  type FollowUpSuggestionsPartData,
11
11
  FollowUpSuggestionsPartDataSchema,
12
12
  } from "@dbx-tools/shared-mastra";
13
13
  import { ModelClass } from "@dbx-tools/shared-model";
14
- import { Agent } from "@mastra/core/agent";
15
14
  import type { MessageList } from "@mastra/core/agent/message-list";
16
- import type { MastraModelConfig } from "@mastra/core/llm";
17
15
  import type { OutputProcessor } from "@mastra/core/processors";
18
16
  import type { RequestContext } from "@mastra/core/request-context";
19
- import type { DataChunkType, LLMStepResult, MastraStreamTransform } from "@mastra/core/stream";
17
+ import type { DataChunkType, MastraStreamTransform } from "@mastra/core/stream";
20
18
  import { createTool } from "@mastra/core/tools";
19
+ import { generateText, Output } from "ai";
21
20
  import { z } from "zod";
22
21
 
23
22
  import type { FollowUpSuggestionsConfig, MastraPluginConfig } from "./config.ts";
@@ -30,6 +29,9 @@ const DEFAULT_QUESTION_LIMIT = 3;
30
29
  const MAX_QUESTION_LIMIT = 10;
31
30
  const DEFAULT_TIMEOUT_MS = 10_000;
32
31
  const MAX_TURN_CONTEXT_CHARS = 16_000;
32
+ const MAX_USER_TEXT_CHARS = 4_000;
33
+ const MAX_ANSWER_CHARS = 16_000;
34
+ const MAX_OUTPUT_TOKENS = 256;
33
35
  const GENERIC_QUESTIONS = new Set([
34
36
  "anything else",
35
37
  "can you tell me more",
@@ -63,7 +65,7 @@ export interface ResolvedFollowUpSuggestionsConfig {
63
65
  export interface SuggestFollowUpsOptions {
64
66
  userText: string;
65
67
  answer: string;
66
- /** Bounded serialized tool inputs and outputs that ground the next questions. */
68
+ /** Optional bounded tool context supplied only by the explicit suggestion tool. */
67
69
  turnContext?: string;
68
70
  nativeQuestions?: readonly string[];
69
71
  requestContext?: RequestContext;
@@ -107,7 +109,7 @@ const FOLLOW_UP_INSTRUCTIONS = [
107
109
  "Write every item in the user's voice as a message for the assistant to answer when clicked.",
108
110
  "Never address the user or ask what they would like, want, prefer, or how the response should be customized.",
109
111
  "Every question must be answerable from the same tools and data domain as the completed turn.",
110
- "Ground questions in the answer and turn tool data, including displayed chart or table values.",
112
+ "Ground questions only in the user message and assistant answer.",
111
113
  "Prefer concrete analyses, comparisons, rankings, calculations, filters, or explanations of the result.",
112
114
  "For a chart or table, ask about the values and patterns before suggesting presentation changes.",
113
115
  "Good examples include 'Which category has the highest value?', 'How large is the gap between the highest and lowest categories?', and 'What pattern stands out in these values?'.",
@@ -117,7 +119,6 @@ const FOLLOW_UP_INSTRUCTIONS = [
117
119
  TYPOGRAPHY_RULE,
118
120
  ].join(" ");
119
121
 
120
- const suggestionAgents = new WeakMap<MastraPluginConfig, Agent>();
121
122
  const generatedRunParts = new Map<string, FollowUpSuggestionsPartData>();
122
123
  const GENERATED_RUN_PART_TTL_MS = 5 * 60_000;
123
124
 
@@ -135,28 +136,6 @@ export function resolveFollowUpSuggestionsConfig(
135
136
  };
136
137
  }
137
138
 
138
- function followUpModel(
139
- config: MastraPluginConfig,
140
- ): (args: { requestContext: RequestContext }) => Promise<MastraModelConfig> {
141
- return ({ requestContext }) =>
142
- buildModel(config, requestContext, { modelClass: FOLLOW_UP_MODEL_CLASS });
143
- }
144
-
145
- function suggestionAgent(config: MastraPluginConfig): Agent {
146
- let agent = suggestionAgents.get(config);
147
- if (!agent) {
148
- agent = new Agent({
149
- id: "follow-up-suggestions",
150
- name: "Follow-up suggestions",
151
- description: "Generates grounded next questions using the fast chat model.",
152
- instructions: FOLLOW_UP_INSTRUCTIONS,
153
- model: followUpModel(config),
154
- });
155
- suggestionAgents.set(config, agent);
156
- }
157
- return agent;
158
- }
159
-
160
139
  function normalizedQuestion(value: string): string {
161
140
  return value
162
141
  .trim()
@@ -165,6 +144,11 @@ function normalizedQuestion(value: string): string {
165
144
  .replace(/\s+/g, " ");
166
145
  }
167
146
 
147
+ function boundedText(value: string, maxChars: number): string {
148
+ const trimmed = value.trim();
149
+ return trimmed.length <= maxChars ? trimmed : trimmed.slice(0, maxChars).trimEnd();
150
+ }
151
+
168
152
  function acceptedQuestions(
169
153
  questions: readonly string[],
170
154
  options: SuggestFollowUpsOptions,
@@ -194,48 +178,6 @@ function acceptedQuestions(
194
178
  return accepted;
195
179
  }
196
180
 
197
- function toolActivityValue(value: unknown): Record<string, unknown> | undefined {
198
- if (!isRecord(value)) return undefined;
199
- return isRecord(value.payload) ? value.payload : value;
200
- }
201
-
202
- function toolActivityName(value: Record<string, unknown>): string | undefined {
203
- if (typeof value.toolName === "string") return value.toolName;
204
- return typeof value.name === "string" ? value.name : undefined;
205
- }
206
-
207
- function serializedTurnContext(steps: readonly LLMStepResult[]): string | undefined {
208
- const activity = steps.flatMap((step) => [
209
- ...(step.toolCalls ?? []).flatMap((call) => {
210
- const value = toolActivityValue(call);
211
- const tool = value ? toolActivityName(value) : undefined;
212
- if (!value || !tool) return [];
213
- return [{ tool, input: value.args ?? value.input }];
214
- }),
215
- ...(step.toolResults ?? []).flatMap((result) => {
216
- const value = toolActivityValue(result);
217
- const tool = value ? toolActivityName(value) : undefined;
218
- if (!value || !tool) return [];
219
- return [
220
- {
221
- tool,
222
- output: value.result ?? value.output,
223
- ...(value.isError || value.error ? { isError: true } : {}),
224
- },
225
- ];
226
- }),
227
- ]);
228
- if (activity.length === 0) return undefined;
229
- try {
230
- const serialized = JSON.stringify(activity.slice(-12));
231
- return serialized.length > MAX_TURN_CONTEXT_CHARS
232
- ? serialized.slice(0, MAX_TURN_CONTEXT_CHARS)
233
- : serialized;
234
- } catch {
235
- return undefined;
236
- }
237
- }
238
-
239
181
  /** Generate grounded follow-ups through ChatFast, failing open to an empty list. */
240
182
  export async function suggestFollowUps(
241
183
  config: MastraPluginConfig,
@@ -246,21 +188,33 @@ export async function suggestFollowUps(
246
188
  const timeout = AbortSignal.timeout(settings.timeoutMs);
247
189
  const abortSignal = combineAbortSignals(options.abortSignal, timeout);
248
190
  const prompt = toDescription({
249
- "User message": options.userText,
250
- "Assistant answer": options.answer,
191
+ "User message": boundedText(options.userText, MAX_USER_TEXT_CHARS),
192
+ "Assistant answer": boundedText(options.answer, MAX_ANSWER_CHARS),
251
193
  ...(options.turnContext ? { "Turn tool data": options.turnContext } : {}),
252
194
  ...(options.nativeQuestions?.length
253
- ? { "Existing suggestions": JSON.stringify(options.nativeQuestions) }
195
+ ? {
196
+ "Existing suggestions": JSON.stringify(
197
+ options.nativeQuestions.slice(0, MAX_QUESTION_LIMIT),
198
+ ),
199
+ }
254
200
  : {}),
255
201
  "Maximum questions": String(settings.limit),
256
202
  });
257
203
  try {
258
- const result = await suggestionAgent(config).generate(prompt, {
259
- structuredOutput: { schema: FollowUpOutputSchema },
260
- ...(options.requestContext ? { requestContext: options.requestContext } : {}),
204
+ const model = await buildModel(config, options.requestContext, {
205
+ modelClass: FOLLOW_UP_MODEL_CLASS,
206
+ optimizeFor: "speed",
207
+ requiresStructuredOutput: true,
208
+ });
209
+ const result = await generateText({
210
+ model,
211
+ instructions: FOLLOW_UP_INSTRUCTIONS,
212
+ prompt,
213
+ output: Output.object({ schema: FollowUpOutputSchema }),
214
+ maxOutputTokens: MAX_OUTPUT_TOKENS,
261
215
  abortSignal,
262
216
  });
263
- const parsed = FollowUpOutputSchema.parse(result.object);
217
+ const parsed = FollowUpOutputSchema.parse(result.output);
264
218
  return acceptedQuestions(parsed.questions, options, settings.limit);
265
219
  } catch (error) {
266
220
  if (!options.abortSignal?.aborted) {
@@ -344,11 +298,9 @@ export function buildFollowUpSuggestionsProcessor(
344
298
  settings.mode === "auto" && Array.isArray(nativeValue)
345
299
  ? nativeValue.filter((value): value is string => typeof value === "string")
346
300
  : [];
347
- const turnContext = serializedTurnContext(result.steps);
348
301
  const suggestionOptions: SuggestFollowUpsOptions = {
349
302
  userText,
350
303
  answer,
351
- ...(turnContext ? { turnContext } : {}),
352
304
  ...(nativeQuestions.length > 0 ? { nativeQuestions } : {}),
353
305
  requestContext,
354
306
  ...(abortSignal ? { abortSignal } : {}),
package/src/memory.ts CHANGED
@@ -48,14 +48,13 @@ import { Pool, type PoolConfig } from "pg";
48
48
  import type { AnyMastraAgentDefinition, MastraMemoryConfigOverride } from "./agents.ts";
49
49
  import type { MastraPluginConfig } from "./config.ts";
50
50
  import { agentStorageSchemaName } from "./storage-schema.ts";
51
- import { summaryModel, TITLE_INSTRUCTIONS } from "./summarize.ts";
52
51
 
53
52
  const logger = log.logger("mastra/memory");
54
53
 
55
54
  /** Resolve the trusted Mastra memory scope stamped by AppKit middleware. */
56
55
  export function requestMemoryScope(
57
56
  requestContext: Pick<RequestContext, "get"> | undefined,
58
- ): AgentMemoryOption | undefined {
57
+ ): { thread: string; resource: string } | undefined {
59
58
  const threadId = requestContext?.get(MASTRA_THREAD_ID_KEY);
60
59
  const resourceId = requestContext?.get(MASTRA_RESOURCE_ID_KEY);
61
60
  if (typeof threadId !== "string" || typeof resourceId !== "string") return undefined;
@@ -257,20 +256,6 @@ export class MemoryBuilder {
257
256
  ...(vector
258
257
  ? { semanticRecall: { topK: 3, messageRange: 2, scope: "resource" as const } }
259
258
  : {}),
260
- // Auto-name each thread from its opening turn so the
261
- // conversation list the UI renders shows meaningful titles
262
- // instead of raw ids. Titling runs on the small / fast chat
263
- // tier (see `summarize.ts`) rather than the agent's primary
264
- // model, so naming a thread never spends the heavyweight model.
265
- // Only meaningful when storage is on; harmless otherwise.
266
- ...(storage
267
- ? {
268
- generateTitle: {
269
- model: summaryModel(this.config),
270
- instructions: TITLE_INSTRUCTIONS,
271
- },
272
- }
273
- : {}),
274
259
  },
275
260
  });
276
261
  }