@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/model.ts CHANGED
@@ -24,11 +24,7 @@
24
24
 
25
25
  import { createWorkspaceClient, type WorkspaceClient } from "@databricks/appkit";
26
26
  import { tryGetExecutionContext } from "@dbx-tools/appkit";
27
- import {
28
- SmartRouterService,
29
- SMART_ROUTER_DEFAULT_SETTINGS,
30
- smartRouterCandidates,
31
- } from "@dbx-tools/appkit-model-gateway";
27
+ import { SmartRouterService, smartRouterCandidates } from "@dbx-tools/appkit-model-gateway";
32
28
  import {
33
29
  classes,
34
30
  endpointCapabilities,
@@ -46,7 +42,12 @@ import { contracts, type ServingEndpointSummary } from "@dbx-tools/shared-model"
46
42
  import { SMART_ROUTER_MODEL_ID, isSmartRouterModelId } from "@dbx-tools/shared-model-gateway";
47
43
  import { RequestContext } from "@mastra/core/request-context";
48
44
 
49
- import { MASTRA_USER_KEY, type MastraPluginConfig, type User } from "./config.ts";
45
+ import {
46
+ executionContextUserId,
47
+ MASTRA_USER_KEY,
48
+ type MastraPluginConfig,
49
+ type User,
50
+ } from "./config.ts";
50
51
  import {
51
52
  MASTRA_MODEL_OVERRIDE_KEY,
52
53
  MASTRA_RESOLVED_MODEL_KEY,
@@ -56,9 +57,16 @@ import {
56
57
  import { recordActiveTraceAuth, recordActiveTraceModel } from "./telemetry.ts";
57
58
 
58
59
  type ModelClass = contracts.ModelClass;
60
+ type ModelOptimization = contracts.ModelOptimization;
59
61
  const { parseModelClass } = classes;
60
62
  const { selectModel } = resolve;
61
- const smartRouter = new SmartRouterService(SMART_ROUTER_DEFAULT_SETTINGS);
63
+ const smartRouter = new SmartRouterService();
64
+ const MASTRA_SMART_ROUTER_RESOLUTION_KEY = "mastra__smart_router_resolution";
65
+
66
+ interface SmartRouterModelResolution {
67
+ readonly modelId: string;
68
+ readonly source: string;
69
+ }
62
70
 
63
71
  /** Virtual endpoint shown when Smart Router is enabled for the active identity. */
64
72
  export const SMART_ROUTER_ENDPOINT: ServingEndpointSummary = {
@@ -90,14 +98,18 @@ export interface BuildModelOverrides {
90
98
  */
91
99
  modelId?: string;
92
100
  /**
93
- * Chat capability class to resolve when no explicit model id is
94
- * supplied. Used by internal agents (e.g. the chart planner asks for
95
- * {@link model.ModelClass.ChatFast}) to express intent without pinning an
96
- * endpoint name; the live catalogue is classified and the top
97
- * available model in the class is chosen, falling back to the
98
- * class's static list when the workspace has none.
101
+ * Chat capability class for internal model work. When no model id is
102
+ * supplied, this bypasses request and environment model overrides so
103
+ * background tasks such as titles and follow-ups cannot inherit the
104
+ * primary agent's Smart Router selection. The live catalogue chooses
105
+ * the top available model in the class, falling back to the class's
106
+ * static list when the workspace has none.
99
107
  */
100
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;
101
113
  /** Arbitrary request, messages, JSON, or text used to build Smart Router task context. */
102
114
  smartRouterInput?: unknown;
103
115
  /** Explicit workspace client for standalone callers such as CLIs. */
@@ -108,6 +120,7 @@ export interface BuildModelOverrides {
108
120
  interface ResolvedModelWorkspaceClient {
109
121
  client: WorkspaceClient;
110
122
  auth: "obo" | "service-principal";
123
+ cacheIdentity?: string;
111
124
  }
112
125
 
113
126
  /** Resolve an OBO workspace client when present, else a service-principal client. */
@@ -120,6 +133,7 @@ function resolveModelWorkspaceContext(
120
133
  return {
121
134
  client: executionContext.client,
122
135
  auth: "isUserContext" in executionContext ? "obo" : "service-principal",
136
+ cacheIdentity: executionContextUserId(executionContext),
123
137
  };
124
138
  }
125
139
  return { client: createWorkspaceClient(), auth: "service-principal" };
@@ -134,7 +148,9 @@ function selectionInput(
134
148
  config: MastraPluginConfig,
135
149
  requested: string | undefined,
136
150
  defaultClass: ModelClass | undefined,
151
+ cacheIdentity: string | undefined,
137
152
  serving = resolveServingConfig(config),
153
+ optimization?: Pick<BuildModelOverrides, "optimizeFor" | "requiresStructuredOutput">,
138
154
  ) {
139
155
  const requestedClass = requested !== undefined ? parseModelClass(requested) : null;
140
156
  const explicit = requestedClass === null ? requested : undefined;
@@ -144,9 +160,14 @@ function selectionInput(
144
160
  fuzzy: serving.fuzzy,
145
161
  threshold: serving.threshold,
146
162
  ...(modelClass !== undefined ? { modelClass } : {}),
163
+ ...(optimization?.optimizeFor !== undefined ? { optimizeFor: optimization.optimizeFor } : {}),
164
+ ...(optimization?.requiresStructuredOutput !== undefined
165
+ ? { requiresStructuredOutput: optimization.requiresStructuredOutput }
166
+ : {}),
147
167
  fallbacks: serving.fallbacks.slice(),
148
168
  liveOnly: requested === undefined,
149
169
  ttlMs: serving.ttlMs,
170
+ ...(cacheIdentity !== undefined ? { cacheIdentity } : {}),
150
171
  };
151
172
  }
152
173
 
@@ -166,8 +187,10 @@ export function resolveDefaultModelId(
166
187
  return SMART_ROUTER_MODEL_ID;
167
188
  }
168
189
  const requested = overrides.modelId ?? process.env.DATABRICKS_SERVING_ENDPOINT_NAME;
169
- return selection.resolveModel(endpoints, selectionInput(config, requested, overrides.modelClass))
170
- .modelId;
190
+ return selection.resolveModel(
191
+ endpoints,
192
+ selectionInput(config, requested, overrides.modelClass, undefined, undefined, overrides),
193
+ ).modelId;
171
194
  }
172
195
 
173
196
  /**
@@ -196,7 +219,7 @@ export async function buildModel(
196
219
  const resolved = overrides.workspaceClient
197
220
  ? { client: overrides.workspaceClient, auth: "service-principal" as const }
198
221
  : resolveModelWorkspaceContext(context);
199
- const { client, auth } = resolved;
222
+ const { client, auth, cacheIdentity } = resolved;
200
223
  recordActiveTraceAuth(auth);
201
224
  const clientConfig = client.config;
202
225
  const host = (await clientConfig.getHost()).toString();
@@ -204,20 +227,29 @@ export async function buildModel(
204
227
  await clientConfig.authenticate(headers);
205
228
  const logger = log.logger(config);
206
229
  const serving = resolveServingConfig(config);
207
- const override = serving.allowOverride
208
- ? (context.get(MASTRA_MODEL_OVERRIDE_KEY) as string | undefined)
209
- : undefined;
230
+ const internalClassOnly = overrides.modelClass !== undefined && overrides.modelId === undefined;
231
+ const override =
232
+ serving.allowOverride && !internalClassOnly
233
+ ? (context.get(MASTRA_MODEL_OVERRIDE_KEY) as string | undefined)
234
+ : undefined;
210
235
 
211
236
  // The override / agent default / env value can be either a concrete
212
237
  // endpoint name or a model class slug ("chat-thinking" /
213
238
  // "chat-balanced" / "chat-fast"). A class slug becomes a class intent
214
239
  // (let the live catalogue pick the best model in that band); anything
215
240
  // else is an explicit name fuzzy-matched against the catalogue. An
216
- // internal `overrides.modelClass` (e.g. the chart planner) is the
217
- // floor when nothing was requested.
218
- const requested = override ?? overrides.modelId ?? process.env.DATABRICKS_SERVING_ENDPOINT_NAME;
241
+ // internal `overrides.modelClass` (e.g. titles, follow-ups, or the chart
242
+ // planner) is authoritative unless that caller also pins `modelId`. It must
243
+ // not inherit the primary agent's request or environment selection.
244
+ const requested =
245
+ override ??
246
+ overrides.modelId ??
247
+ (!internalClassOnly ? process.env.DATABRICKS_SERVING_ENDPOINT_NAME : undefined);
219
248
  const smartRouterInput = overrides.smartRouterInput ?? context.get(MASTRA_SMART_ROUTER_INPUT_KEY);
220
249
  const smartRouterRequested = isSmartRouterModelId(requested);
250
+ if (smartRouterRequested && smartRouterInput === undefined) {
251
+ throw new Error("Smart Router requires task input before it can resolve a concrete model");
252
+ }
221
253
  let modelId: string;
222
254
  let source: string;
223
255
  if (
@@ -225,36 +257,62 @@ export async function buildModel(
225
257
  overrides.modelClass === undefined &&
226
258
  smartRouterInput !== undefined
227
259
  ) {
228
- const endpoints = await listServingEndpoints(client, host, { ttlMs: serving.ttlMs });
229
- const candidates = smartRouterCandidates(
230
- endpoints.filter((endpoint) => endpointCapabilities(endpoint).tools),
231
- "codex",
232
- );
233
- const enabled = await smartRouter.isEnabled(candidates, client);
234
- if (enabled) {
235
- const selected = await smartRouter.select(smartRouterInput, candidates, { client });
236
- modelId = selected.value.name;
237
- source = SMART_ROUTER_MODEL_ID;
238
- } else if (smartRouterRequested) {
239
- throw new Error("Databricks Smart Router is not enabled for the active workspace identity");
240
- } else {
241
- const resolved = await selectModel(
242
- client,
243
- host,
244
- selectionInput(config, requested, overrides.modelClass, serving),
245
- );
246
- modelId = resolved.modelId;
247
- source = resolved.source;
260
+ let pending = context.get(MASTRA_SMART_ROUTER_RESOLUTION_KEY) as
261
+ Promise<SmartRouterModelResolution> | undefined;
262
+ if (!pending) {
263
+ pending = (async () => {
264
+ const endpoints = await listServingEndpoints(client, host, {
265
+ ttlMs: serving.ttlMs,
266
+ ...(cacheIdentity !== undefined ? { cacheIdentity } : {}),
267
+ });
268
+ const candidates = smartRouterCandidates(
269
+ endpoints.filter((endpoint) => endpointCapabilities(endpoint).tools),
270
+ "codex",
271
+ );
272
+ // An explicit Smart Router request does not need a synthetic preflight:
273
+ // the real selection is the availability check. Auto mode still probes
274
+ // so it can fall back to the configured model policy when unavailable.
275
+ const enabled = smartRouterRequested || (await smartRouter.isEnabled(candidates, client));
276
+ if (enabled) {
277
+ const selected = await smartRouter.select(smartRouterInput, candidates, { client });
278
+ return { modelId: selected.value.name, source: SMART_ROUTER_MODEL_ID };
279
+ }
280
+ const fallback = await selectModel(
281
+ client,
282
+ host,
283
+ selectionInput(
284
+ config,
285
+ requested,
286
+ overrides.modelClass,
287
+ cacheIdentity,
288
+ serving,
289
+ overrides,
290
+ ),
291
+ );
292
+ return { modelId: fallback.modelId, source: fallback.source };
293
+ })();
294
+ context.set(MASTRA_SMART_ROUTER_RESOLUTION_KEY, pending);
295
+ }
296
+ try {
297
+ ({ modelId, source } = await pending);
298
+ } catch (error) {
299
+ if (context.get(MASTRA_SMART_ROUTER_RESOLUTION_KEY) === pending) {
300
+ context.set(MASTRA_SMART_ROUTER_RESOLUTION_KEY, undefined);
301
+ }
302
+ throw error;
248
303
  }
249
304
  } else {
250
305
  const resolved = await selectModel(
251
306
  client,
252
307
  host,
253
- selectionInput(config, requested, overrides.modelClass, serving),
308
+ selectionInput(config, requested, overrides.modelClass, cacheIdentity, serving, overrides),
254
309
  );
255
310
  modelId = resolved.modelId;
256
311
  source = resolved.source;
257
312
  }
313
+ if (isSmartRouterModelId(modelId)) {
314
+ throw new Error("Smart Router did not resolve to a concrete serving endpoint");
315
+ }
258
316
  logger.debug("model selected", { modelId, source, requested });
259
317
  context.set(MASTRA_RESOLVED_MODEL_KEY, modelId);
260
318
  recordActiveTraceModel(modelId);
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
@@ -1,16 +1,9 @@
1
1
  /**
2
2
  * Small-tier summarization for the Mastra plugin.
3
3
  *
4
- * Two surfaces, both backed by the fast / small chat tier
4
+ * A dedicated `summarize` tool backed by the fast / small chat tier
5
5
  * ({@link model.ModelClass.ChatFast}) resolved through the same
6
- * `/serving-endpoints` pipeline as the main agents:
7
- *
8
- * - A dedicated `summarize` tool (see {@link buildSummarizeTool})
9
- * agents can call to condense arbitrary text without burning the
10
- * heavyweight chat model.
11
- * - The model + instructions Mastra's memory uses to auto-name
12
- * conversation threads (`generateTitle`), so titling reuses the
13
- * same small tier rather than the agent's primary model.
6
+ * `/serving-endpoints` pipeline as the main agents.
14
7
  *
15
8
  * Mirrors the chart-planner wiring in `chart.ts`: a per-config cached
16
9
  * `Agent` on the fast tier, invoked via `agent.generate(...)` inside
@@ -25,6 +18,7 @@ import { Agent } from "@mastra/core/agent";
25
18
  import type { MastraModelConfig } from "@mastra/core/llm";
26
19
  import type { RequestContext } from "@mastra/core/request-context";
27
20
  import { createTool } from "@mastra/core/tools";
21
+ import { type LanguageModelMiddleware, wrapLanguageModel } from "ai";
28
22
  import { z } from "zod";
29
23
 
30
24
  import type { MastraPluginConfig } from "./config.ts";
@@ -33,6 +27,8 @@ import { TYPOGRAPHY_RULE } from "./style.ts";
33
27
 
34
28
  /** Fast / small chat tier used for both titling and summaries. */
35
29
  const SUMMARY_MODEL_CLASS = ModelClass.ChatFast;
30
+ const TITLE_INPUT_CHAR_LIMIT = 4_000;
31
+ const TITLE_OUTPUT_TOKEN_LIMIT = 32;
36
32
 
37
33
  /** System prompt for the summarizer agent (and the `summarize` tool). */
38
34
  const SUMMARIZER_INSTRUCTIONS = [
@@ -46,9 +42,9 @@ const SUMMARIZER_INSTRUCTIONS = [
46
42
  ].join("\n");
47
43
 
48
44
  /**
49
- * Instructions Mastra's `generateTitle` hands the small-tier model to
50
- * name a conversation thread from its opening turn. Kept terse so the
51
- * model returns a bare title with no decoration.
45
+ * Instructions Mastra's `generateTitle` hands the agent model to name a
46
+ * conversation thread from its opening turn. Kept terse so the model returns
47
+ * a bare title with no decoration.
52
48
  */
53
49
  export const TITLE_INSTRUCTIONS = [
54
50
  "Generate a short, specific title for this conversation, 3 to 6 words.",
@@ -59,8 +55,7 @@ export const TITLE_INSTRUCTIONS = [
59
55
  ].join(" ");
60
56
 
61
57
  /**
62
- * Resolve the small-tier model for summarization / titling. Reused by
63
- * both the summarizer agent and Mastra memory's `generateTitle`.
58
+ * Resolve the small-tier model for summarization.
64
59
  *
65
60
  * Returned as a `requestContext`-taking function (a Mastra
66
61
  * `DynamicArgument<MastraModelConfig>`) so each call mints user-scoped
@@ -73,6 +68,43 @@ export function summaryModel(
73
68
  buildModel(config, requestContext, { modelClass: SUMMARY_MODEL_CLASS });
74
69
  }
75
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
+
76
108
  /**
77
109
  * One summarizer `Agent` per plugin config, cached on config-object
78
110
  * identity so a hot tool path doesn't pay the constructor cost each