@dbx-tools/appkit-mastra 0.9.177 → 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.
@@ -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,7 +4,7 @@
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,
@@ -14,7 +14,7 @@ import { ModelClass } from "@dbx-tools/shared-model";
14
14
  import type { MessageList } from "@mastra/core/agent/message-list";
15
15
  import type { OutputProcessor } from "@mastra/core/processors";
16
16
  import type { RequestContext } from "@mastra/core/request-context";
17
- import type { DataChunkType, LLMStepResult, MastraStreamTransform } from "@mastra/core/stream";
17
+ import type { DataChunkType, MastraStreamTransform } from "@mastra/core/stream";
18
18
  import { createTool } from "@mastra/core/tools";
19
19
  import { generateText, Output } from "ai";
20
20
  import { z } from "zod";
@@ -29,6 +29,9 @@ const DEFAULT_QUESTION_LIMIT = 3;
29
29
  const MAX_QUESTION_LIMIT = 10;
30
30
  const DEFAULT_TIMEOUT_MS = 10_000;
31
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;
32
35
  const GENERIC_QUESTIONS = new Set([
33
36
  "anything else",
34
37
  "can you tell me more",
@@ -62,7 +65,7 @@ export interface ResolvedFollowUpSuggestionsConfig {
62
65
  export interface SuggestFollowUpsOptions {
63
66
  userText: string;
64
67
  answer: string;
65
- /** Bounded serialized tool inputs and outputs that ground the next questions. */
68
+ /** Optional bounded tool context supplied only by the explicit suggestion tool. */
66
69
  turnContext?: string;
67
70
  nativeQuestions?: readonly string[];
68
71
  requestContext?: RequestContext;
@@ -106,7 +109,7 @@ const FOLLOW_UP_INSTRUCTIONS = [
106
109
  "Write every item in the user's voice as a message for the assistant to answer when clicked.",
107
110
  "Never address the user or ask what they would like, want, prefer, or how the response should be customized.",
108
111
  "Every question must be answerable from the same tools and data domain as the completed turn.",
109
- "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.",
110
113
  "Prefer concrete analyses, comparisons, rankings, calculations, filters, or explanations of the result.",
111
114
  "For a chart or table, ask about the values and patterns before suggesting presentation changes.",
112
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?'.",
@@ -141,6 +144,11 @@ function normalizedQuestion(value: string): string {
141
144
  .replace(/\s+/g, " ");
142
145
  }
143
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
+
144
152
  function acceptedQuestions(
145
153
  questions: readonly string[],
146
154
  options: SuggestFollowUpsOptions,
@@ -170,48 +178,6 @@ function acceptedQuestions(
170
178
  return accepted;
171
179
  }
172
180
 
173
- function toolActivityValue(value: unknown): Record<string, unknown> | undefined {
174
- if (!isRecord(value)) return undefined;
175
- return isRecord(value.payload) ? value.payload : value;
176
- }
177
-
178
- function toolActivityName(value: Record<string, unknown>): string | undefined {
179
- if (typeof value.toolName === "string") return value.toolName;
180
- return typeof value.name === "string" ? value.name : undefined;
181
- }
182
-
183
- function serializedTurnContext(steps: readonly LLMStepResult[]): string | undefined {
184
- const activity = steps.flatMap((step) => [
185
- ...(step.toolCalls ?? []).flatMap((call) => {
186
- const value = toolActivityValue(call);
187
- const tool = value ? toolActivityName(value) : undefined;
188
- if (!value || !tool) return [];
189
- return [{ tool, input: value.args ?? value.input }];
190
- }),
191
- ...(step.toolResults ?? []).flatMap((result) => {
192
- const value = toolActivityValue(result);
193
- const tool = value ? toolActivityName(value) : undefined;
194
- if (!value || !tool) return [];
195
- return [
196
- {
197
- tool,
198
- output: value.result ?? value.output,
199
- ...(value.isError || value.error ? { isError: true } : {}),
200
- },
201
- ];
202
- }),
203
- ]);
204
- if (activity.length === 0) return undefined;
205
- try {
206
- const serialized = JSON.stringify(activity.slice(-12));
207
- return serialized.length > MAX_TURN_CONTEXT_CHARS
208
- ? serialized.slice(0, MAX_TURN_CONTEXT_CHARS)
209
- : serialized;
210
- } catch {
211
- return undefined;
212
- }
213
- }
214
-
215
181
  /** Generate grounded follow-ups through ChatFast, failing open to an empty list. */
216
182
  export async function suggestFollowUps(
217
183
  config: MastraPluginConfig,
@@ -222,23 +188,30 @@ export async function suggestFollowUps(
222
188
  const timeout = AbortSignal.timeout(settings.timeoutMs);
223
189
  const abortSignal = combineAbortSignals(options.abortSignal, timeout);
224
190
  const prompt = toDescription({
225
- "User message": options.userText,
226
- "Assistant answer": options.answer,
191
+ "User message": boundedText(options.userText, MAX_USER_TEXT_CHARS),
192
+ "Assistant answer": boundedText(options.answer, MAX_ANSWER_CHARS),
227
193
  ...(options.turnContext ? { "Turn tool data": options.turnContext } : {}),
228
194
  ...(options.nativeQuestions?.length
229
- ? { "Existing suggestions": JSON.stringify(options.nativeQuestions) }
195
+ ? {
196
+ "Existing suggestions": JSON.stringify(
197
+ options.nativeQuestions.slice(0, MAX_QUESTION_LIMIT),
198
+ ),
199
+ }
230
200
  : {}),
231
201
  "Maximum questions": String(settings.limit),
232
202
  });
233
203
  try {
234
204
  const model = await buildModel(config, options.requestContext, {
235
205
  modelClass: FOLLOW_UP_MODEL_CLASS,
206
+ optimizeFor: "speed",
207
+ requiresStructuredOutput: true,
236
208
  });
237
209
  const result = await generateText({
238
210
  model,
239
211
  instructions: FOLLOW_UP_INSTRUCTIONS,
240
212
  prompt,
241
213
  output: Output.object({ schema: FollowUpOutputSchema }),
214
+ maxOutputTokens: MAX_OUTPUT_TOKENS,
242
215
  abortSignal,
243
216
  });
244
217
  const parsed = FollowUpOutputSchema.parse(result.output);
@@ -325,11 +298,9 @@ export function buildFollowUpSuggestionsProcessor(
325
298
  settings.mode === "auto" && Array.isArray(nativeValue)
326
299
  ? nativeValue.filter((value): value is string => typeof value === "string")
327
300
  : [];
328
- const turnContext = serializedTurnContext(result.steps);
329
301
  const suggestionOptions: SuggestFollowUpsOptions = {
330
302
  userText,
331
303
  answer,
332
- ...(turnContext ? { turnContext } : {}),
333
304
  ...(nativeQuestions.length > 0 ? { nativeQuestions } : {}),
334
305
  requestContext,
335
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,18 +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 using the fast chat tier.
261
- // Title generation is awaited before Mastra settles the response stream,
262
- // so reusing the primary agent model can keep the UI busy long after the
263
- // answer text has finished streaming.
264
- ...(storage
265
- ? {
266
- generateTitle: {
267
- model: summaryModel(this.config),
268
- instructions: TITLE_INSTRUCTIONS,
269
- },
270
- }
271
- : {}),
272
259
  },
273
260
  });
274
261
  }
package/src/model.ts CHANGED
@@ -57,6 +57,7 @@ import {
57
57
  import { recordActiveTraceAuth, recordActiveTraceModel } from "./telemetry.ts";
58
58
 
59
59
  type ModelClass = contracts.ModelClass;
60
+ type ModelOptimization = contracts.ModelOptimization;
60
61
  const { parseModelClass } = classes;
61
62
  const { selectModel } = resolve;
62
63
  const smartRouter = new SmartRouterService();
@@ -105,6 +106,10 @@ export interface BuildModelOverrides {
105
106
  * static list when the workspace has none.
106
107
  */
107
108
  modelClass?: ModelClass;
109
+ /** Profile dimension used to rank eligible automatic model candidates. */
110
+ optimizeFor?: ModelOptimization;
111
+ /** Require documented structured-output support for the selected endpoint. */
112
+ requiresStructuredOutput?: boolean;
108
113
  /** Arbitrary request, messages, JSON, or text used to build Smart Router task context. */
109
114
  smartRouterInput?: unknown;
110
115
  /** Explicit workspace client for standalone callers such as CLIs. */
@@ -145,6 +150,7 @@ function selectionInput(
145
150
  defaultClass: ModelClass | undefined,
146
151
  cacheIdentity: string | undefined,
147
152
  serving = resolveServingConfig(config),
153
+ optimization?: Pick<BuildModelOverrides, "optimizeFor" | "requiresStructuredOutput">,
148
154
  ) {
149
155
  const requestedClass = requested !== undefined ? parseModelClass(requested) : null;
150
156
  const explicit = requestedClass === null ? requested : undefined;
@@ -154,6 +160,10 @@ function selectionInput(
154
160
  fuzzy: serving.fuzzy,
155
161
  threshold: serving.threshold,
156
162
  ...(modelClass !== undefined ? { modelClass } : {}),
163
+ ...(optimization?.optimizeFor !== undefined ? { optimizeFor: optimization.optimizeFor } : {}),
164
+ ...(optimization?.requiresStructuredOutput !== undefined
165
+ ? { requiresStructuredOutput: optimization.requiresStructuredOutput }
166
+ : {}),
157
167
  fallbacks: serving.fallbacks.slice(),
158
168
  liveOnly: requested === undefined,
159
169
  ttlMs: serving.ttlMs,
@@ -179,7 +189,7 @@ export function resolveDefaultModelId(
179
189
  const requested = overrides.modelId ?? process.env.DATABRICKS_SERVING_ENDPOINT_NAME;
180
190
  return selection.resolveModel(
181
191
  endpoints,
182
- selectionInput(config, requested, overrides.modelClass, undefined),
192
+ selectionInput(config, requested, overrides.modelClass, undefined, undefined, overrides),
183
193
  ).modelId;
184
194
  }
185
195
 
@@ -270,7 +280,14 @@ export async function buildModel(
270
280
  const fallback = await selectModel(
271
281
  client,
272
282
  host,
273
- selectionInput(config, requested, overrides.modelClass, cacheIdentity, serving),
283
+ selectionInput(
284
+ config,
285
+ requested,
286
+ overrides.modelClass,
287
+ cacheIdentity,
288
+ serving,
289
+ overrides,
290
+ ),
274
291
  );
275
292
  return { modelId: fallback.modelId, source: fallback.source };
276
293
  })();
@@ -288,7 +305,7 @@ export async function buildModel(
288
305
  const resolved = await selectModel(
289
306
  client,
290
307
  host,
291
- selectionInput(config, requested, overrides.modelClass, cacheIdentity, serving),
308
+ selectionInput(config, requested, overrides.modelClass, cacheIdentity, serving, overrides),
292
309
  );
293
310
  modelId = resolved.modelId;
294
311
  source = resolved.source;
package/src/plugin.ts CHANGED
@@ -82,7 +82,7 @@ import {
82
82
  type GenieChatRequest,
83
83
  type MastraClientConfig,
84
84
  type StatementData,
85
- type FeedbackMode,
85
+ type FeedbackDestination,
86
86
  MastraFeedbackRequestSchema,
87
87
  type MlflowFeedbackRequest,
88
88
  type GenieFeedbackRequest,
@@ -122,7 +122,7 @@ import {
122
122
  modelCatalogueDefaults,
123
123
  statementDataDefaults,
124
124
  } from "./defaults.ts";
125
- import { resolveFeedbackMode } from "./feedback-mode.ts";
125
+ import { resolveFeedbackModes } from "./feedback-mode.ts";
126
126
  import { resolveFollowUpSuggestionsConfig } from "./follow-up-suggestions.ts";
127
127
  import {
128
128
  deleteGenieThread,
@@ -274,8 +274,8 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
274
274
  * {@link buildAgentAndServer} and registered on the Mastra instance.
275
275
  */
276
276
  private mcp: ResolvedMcp | null = null;
277
- /** Resolved feedback destination published to server and browser clients. */
278
- private resolvedFeedbackMode: FeedbackMode = "none";
277
+ /** Resolved feedback destinations published to server and browser clients. */
278
+ private resolvedFeedbackModes: FeedbackDestination[] = [];
279
279
  /**
280
280
  * Resolved AppKit identity mode: the identity the chat path's Databricks
281
281
  * calls run as. Read once at setup so an invalid `genieIdentity` fails the app
@@ -287,11 +287,10 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
287
287
  // app boot with a clear message rather than 500-ing the first chat request.
288
288
  this.identityMode = resolveIdentityMode(this.config.genieIdentity);
289
289
  const spaces = resolveGenieSpaces(this.config, this.context);
290
- this.resolvedFeedbackMode = resolveFeedbackMode(this.config.feedbackMode, {
290
+ this.resolvedFeedbackModes = resolveFeedbackModes(this.config.feedbackMode, {
291
291
  genie: this.config.genieAgentMode !== false && Object.keys(spaces).length > 0,
292
292
  mlflow: mlflowEnabled(),
293
293
  });
294
- this.config.feedbackMode = this.resolvedFeedbackMode;
295
294
  if (this.config.observability !== false) {
296
295
  await configureNativeHttpTelemetry({
297
296
  // A low-level request without an active semantic span becomes a noisy
@@ -497,7 +496,7 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
497
496
  basePath: this.basePath,
498
497
  defaultAgent: this.built?.defaultAgentId ?? FALLBACK_AGENT_ID,
499
498
  agents: Object.keys(this.built?.agents ?? {}),
500
- feedbackMode: this.feedbackMode(),
499
+ feedbackModes: this.feedbackModes(),
501
500
  backgroundTurns: this.config.backgroundTurns !== false,
502
501
  followUpSuggestions: followUps?.mode ?? "none",
503
502
  followUpSuggestionLimit: followUps?.limit ?? 3,
@@ -509,9 +508,14 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
509
508
  return config as unknown as Record<string, unknown>;
510
509
  }
511
510
 
512
- /** Resolve the only feedback destination exposed for this plugin mode. */
513
- private feedbackMode(): FeedbackMode {
514
- return this.resolvedFeedbackMode;
511
+ /** Resolve the feedback destinations exposed for per-message routing. */
512
+ private feedbackModes(): FeedbackDestination[] {
513
+ return this.resolvedFeedbackModes;
514
+ }
515
+
516
+ /** Whether one feedback destination is enabled. */
517
+ private feedbackEnabled(mode: FeedbackDestination): boolean {
518
+ return this.resolvedFeedbackModes.includes(mode);
515
519
  }
516
520
 
517
521
  override injectRoutes(router: IAppRouter): void {
@@ -984,15 +988,14 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
984
988
  return this.execute(() => write(client), genieConversationWriteDefaults);
985
989
  }
986
990
 
987
- /** Register the single feedback destination selected by plugin mode. */
991
+ /** Register every feedback destination enabled by plugin mode. */
988
992
  private registerFeedbackRoutes(router: IAppRouter): void {
989
993
  this.route(router, {
990
994
  name: "feedback",
991
995
  method: "post",
992
996
  path: MASTRA_ROUTES.feedback,
993
997
  handler: async (req, res) => {
994
- const mode = this.feedbackMode();
995
- if (mode === "none") {
998
+ if (this.resolvedFeedbackModes.length === 0) {
996
999
  res.status(404).json({ ok: false });
997
1000
  return;
998
1001
  }
@@ -1008,7 +1011,7 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
1008
1011
  });
1009
1012
  return;
1010
1013
  }
1011
- if (parsed.data.mode !== mode) {
1014
+ if (!this.feedbackEnabled(parsed.data.mode)) {
1012
1015
  res.status(400).json({ ok: false, error: INVALID_FEEDBACK_MESSAGE });
1013
1016
  return;
1014
1017
  }
@@ -1042,7 +1045,7 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
1042
1045
  method: "get",
1043
1046
  path: MASTRA_ROUTES.mlflowExperiment,
1044
1047
  handler: async (req, res) => {
1045
- if (this.feedbackMode() !== "mlflow") {
1048
+ if (!this.feedbackEnabled("mlflow")) {
1046
1049
  res.json({ url: null });
1047
1050
  return;
1048
1051
  }
@@ -1454,7 +1457,10 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
1454
1457
  identity: (request) =>
1455
1458
  useServicePrincipal(this.identityMode, request) ? "service-principal" : "obo",
1456
1459
  });
1457
- this.mastraServer = new MastraServer(this.config, {
1460
+ this.mastraServer = new MastraServer(
1461
+ this.config,
1462
+ { mlflowFeedback: this.feedbackEnabled("mlflow") },
1463
+ {
1458
1464
  app: this.mastraApp,
1459
1465
  mastra: this.mastra,
1460
1466
  prefix: "",
@@ -1465,7 +1471,8 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
1465
1471
  version: "v7",
1466
1472
  }),
1467
1473
  ],
1468
- });
1474
+ },
1475
+ );
1469
1476
  await this.mastraServer.init();
1470
1477
  attachStaleResumeRecovery(this.mastraApp);
1471
1478
  this.logger.info("ready", {
@@ -1476,7 +1483,7 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
1476
1483
  apiAccess: this.config.apiAccess ?? "scoped",
1477
1484
  mcp: this.mcp ? `${this.basePath}${this.mcp.httpPath}` : "off",
1478
1485
  lakebase: memoryPool !== undefined,
1479
- feedback: this.feedbackMode(),
1486
+ feedback: this.feedbackModes(),
1480
1487
  observability: observability !== undefined ? "mlflow" : "off",
1481
1488
  });
1482
1489
  }
package/src/server.ts CHANGED
@@ -232,11 +232,12 @@ export class MastraServer extends ExpressMastraServer {
232
232
 
233
233
  constructor(
234
234
  private config: MastraPluginConfig,
235
+ feedback: { mlflowFeedback: boolean },
235
236
  ...args: ConstructorParameters<typeof ExpressMastraServer>
236
237
  ) {
237
238
  super(...args);
238
239
  this.log = log.logger(config);
239
- this.mlflowFeedback = config.feedbackMode === "mlflow";
240
+ this.mlflowFeedback = feedback.mlflowFeedback;
240
241
  }
241
242
 
242
243
  override registerAuthMiddleware(): void {
package/src/summarize.ts CHANGED
@@ -18,6 +18,7 @@ import { Agent } from "@mastra/core/agent";
18
18
  import type { MastraModelConfig } from "@mastra/core/llm";
19
19
  import type { RequestContext } from "@mastra/core/request-context";
20
20
  import { createTool } from "@mastra/core/tools";
21
+ import { type LanguageModelMiddleware, wrapLanguageModel } from "ai";
21
22
  import { z } from "zod";
22
23
 
23
24
  import type { MastraPluginConfig } from "./config.ts";
@@ -26,6 +27,8 @@ import { TYPOGRAPHY_RULE } from "./style.ts";
26
27
 
27
28
  /** Fast / small chat tier used for both titling and summaries. */
28
29
  const SUMMARY_MODEL_CLASS = ModelClass.ChatFast;
30
+ const TITLE_INPUT_CHAR_LIMIT = 4_000;
31
+ const TITLE_OUTPUT_TOKEN_LIMIT = 32;
29
32
 
30
33
  /** System prompt for the summarizer agent (and the `summarize` tool). */
31
34
  const SUMMARIZER_INSTRUCTIONS = [
@@ -65,6 +68,43 @@ export function summaryModel(
65
68
  buildModel(config, requestContext, { modelClass: SUMMARY_MODEL_CLASS });
66
69
  }
67
70
 
71
+ const titleModelLimits: LanguageModelMiddleware = {
72
+ async transformParams({ params }) {
73
+ let remaining = TITLE_INPUT_CHAR_LIMIT;
74
+ const prompt = params.prompt.map((message) => {
75
+ if (message.role !== "user") return message;
76
+ return {
77
+ ...message,
78
+ content: message.content.map((part) => {
79
+ if (part.type !== "text") return part;
80
+ const text = remaining > 0 ? part.text.slice(0, remaining) : "";
81
+ remaining -= text.length;
82
+ return { ...part, text };
83
+ }),
84
+ };
85
+ });
86
+ return {
87
+ ...params,
88
+ prompt,
89
+ maxOutputTokens: TITLE_OUTPUT_TOKEN_LIMIT,
90
+ };
91
+ },
92
+ };
93
+
94
+ /** Resolve a bounded, tool-free ChatFast model for automatic thread titles. */
95
+ export function titleModel(
96
+ config: MastraPluginConfig,
97
+ ): (args: { requestContext: RequestContext }) => Promise<MastraModelConfig> {
98
+ return async ({ requestContext }) =>
99
+ wrapLanguageModel({
100
+ model: await buildModel(config, requestContext, {
101
+ modelClass: SUMMARY_MODEL_CLASS,
102
+ optimizeFor: "speed",
103
+ }),
104
+ middleware: titleModelLimits,
105
+ });
106
+ }
107
+
68
108
  /**
69
109
  * One summarizer `Agent` per plugin config, cached on config-object
70
110
  * identity so a hot tool path doesn't pay the constructor cost each