@dbx-tools/appkit-mastra 0.9.177 → 0.9.181
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/README.md +5 -5
- package/index.ts +4 -4
- package/lib/index.d.ts +4 -4
- package/lib/index.js +5 -5
- package/lib/src/chat.d.ts +13 -0
- package/lib/src/chat.js +68 -6
- package/lib/src/config.d.ts +4 -3
- package/lib/src/config.js +2 -2
- package/lib/src/feedback-mode.d.ts +3 -3
- package/lib/src/feedback-mode.js +9 -10
- package/lib/src/follow-up-suggestions.d.ts +1 -1
- package/lib/src/follow-up-suggestions.js +18 -53
- package/lib/src/memory.d.ts +4 -1
- package/lib/src/memory.js +1 -14
- package/lib/src/mlflow.d.ts +1 -1
- package/lib/src/mlflow.js +18 -9
- package/lib/src/model.d.ts +5 -0
- package/lib/src/model.js +9 -5
- package/lib/src/plugin.d.ts +7 -5
- package/lib/src/plugin.js +23 -21
- package/lib/src/server.d.ts +3 -1
- package/lib/src/server.js +3 -3
- package/lib/src/summarize.d.ts +4 -0
- package/lib/src/summarize.js +38 -1
- package/package.json +18 -18
- package/src/chat.ts +81 -2
- package/src/config.ts +5 -4
- package/src/feedback-mode.ts +13 -9
- package/src/follow-up-suggestions.ts +22 -51
- package/src/memory.ts +1 -14
- package/src/mlflow.ts +17 -8
- package/src/model.ts +20 -3
- package/src/plugin.ts +34 -21
- package/src/server.ts +2 -1
- package/src/summarize.ts +40 -0
package/src/feedback-mode.ts
CHANGED
|
@@ -5,7 +5,10 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { ConfigurationError } from "@databricks/appkit";
|
|
8
|
-
import type {
|
|
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
|
|
19
|
-
export function
|
|
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
|
-
):
|
|
25
|
+
): FeedbackDestination[] {
|
|
23
26
|
const mode = configured ?? "auto";
|
|
24
|
-
if (mode === "none") return
|
|
27
|
+
if (mode === "none") return [];
|
|
25
28
|
if (mode === "auto") {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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,
|
|
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,
|
|
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
|
-
/**
|
|
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
|
|
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
|
-
? {
|
|
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
|
-
):
|
|
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/mlflow.ts
CHANGED
|
@@ -631,9 +631,10 @@ export function mlflowEnabled(): boolean {
|
|
|
631
631
|
*/
|
|
632
632
|
export async function mlflowExperimentManagerUrl(
|
|
633
633
|
client: WorkspaceClient,
|
|
634
|
+
viewerUserName?: string,
|
|
634
635
|
): Promise<string | undefined> {
|
|
635
636
|
try {
|
|
636
|
-
return await resolveMlflowExperimentManagerUrl(client);
|
|
637
|
+
return await resolveMlflowExperimentManagerUrl(client, viewerUserName);
|
|
637
638
|
} catch {
|
|
638
639
|
return undefined;
|
|
639
640
|
}
|
|
@@ -641,6 +642,7 @@ export async function mlflowExperimentManagerUrl(
|
|
|
641
642
|
|
|
642
643
|
async function resolveMlflowExperimentManagerUrl(
|
|
643
644
|
client: WorkspaceClient,
|
|
645
|
+
viewerUserName?: string,
|
|
644
646
|
): Promise<string | undefined> {
|
|
645
647
|
const experimentId = await resolveExperimentId(client);
|
|
646
648
|
if (!experimentId) return undefined;
|
|
@@ -653,21 +655,28 @@ async function resolveMlflowExperimentManagerUrl(
|
|
|
653
655
|
{ method: "GET" },
|
|
654
656
|
),
|
|
655
657
|
]);
|
|
656
|
-
if (!
|
|
658
|
+
if (!permissionsResponse.ok) return undefined;
|
|
657
659
|
|
|
658
|
-
const current =
|
|
660
|
+
const current = currentResponse.ok
|
|
661
|
+
? CurrentUserResponseSchema.safeParse(await readResponseJson(currentResponse))
|
|
662
|
+
: undefined;
|
|
659
663
|
const permissions = ExperimentPermissionsResponseSchema.safeParse(
|
|
660
664
|
await readResponseJson(permissionsResponse),
|
|
661
665
|
);
|
|
662
|
-
if (!
|
|
666
|
+
if (!permissions.success || (!viewerUserName && !current?.success)) return undefined;
|
|
663
667
|
|
|
664
668
|
const userNames = new Set(
|
|
665
|
-
[
|
|
666
|
-
|
|
667
|
-
|
|
669
|
+
[
|
|
670
|
+
viewerUserName,
|
|
671
|
+
...(current?.success
|
|
672
|
+
? [current.data.userName, current.data.applicationId, current.data.displayName]
|
|
673
|
+
: []),
|
|
674
|
+
]
|
|
675
|
+
.map((value) => value?.trim())
|
|
676
|
+
.filter((value): value is string => Boolean(value)),
|
|
668
677
|
);
|
|
669
678
|
const groupNames = new Set(
|
|
670
|
-
(current.data.groups ?? []).flatMap(({ value, display }) =>
|
|
679
|
+
(current?.success ? (current.data.groups ?? []) : []).flatMap(({ value, display }) =>
|
|
671
680
|
[value, display].filter((entry): entry is string => Boolean(entry)),
|
|
672
681
|
),
|
|
673
682
|
);
|
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(
|
|
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
|
@@ -64,6 +64,7 @@ import {
|
|
|
64
64
|
configureNativeHttpTelemetry,
|
|
65
65
|
excludeUnparentedOutgoingHttpTelemetry,
|
|
66
66
|
deferredNativeTracerProvider,
|
|
67
|
+
requestUserEmail,
|
|
67
68
|
requestUserId,
|
|
68
69
|
useServicePrincipal,
|
|
69
70
|
} from "@dbx-tools/appkit";
|
|
@@ -82,7 +83,7 @@ import {
|
|
|
82
83
|
type GenieChatRequest,
|
|
83
84
|
type MastraClientConfig,
|
|
84
85
|
type StatementData,
|
|
85
|
-
type
|
|
86
|
+
type FeedbackDestination,
|
|
86
87
|
MastraFeedbackRequestSchema,
|
|
87
88
|
type MlflowFeedbackRequest,
|
|
88
89
|
type GenieFeedbackRequest,
|
|
@@ -122,7 +123,7 @@ import {
|
|
|
122
123
|
modelCatalogueDefaults,
|
|
123
124
|
statementDataDefaults,
|
|
124
125
|
} from "./defaults.ts";
|
|
125
|
-
import {
|
|
126
|
+
import { resolveFeedbackModes } from "./feedback-mode.ts";
|
|
126
127
|
import { resolveFollowUpSuggestionsConfig } from "./follow-up-suggestions.ts";
|
|
127
128
|
import {
|
|
128
129
|
deleteGenieThread,
|
|
@@ -274,8 +275,8 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
274
275
|
* {@link buildAgentAndServer} and registered on the Mastra instance.
|
|
275
276
|
*/
|
|
276
277
|
private mcp: ResolvedMcp | null = null;
|
|
277
|
-
/** Resolved feedback
|
|
278
|
-
private
|
|
278
|
+
/** Resolved feedback destinations published to server and browser clients. */
|
|
279
|
+
private resolvedFeedbackModes: FeedbackDestination[] = [];
|
|
279
280
|
/**
|
|
280
281
|
* Resolved AppKit identity mode: the identity the chat path's Databricks
|
|
281
282
|
* calls run as. Read once at setup so an invalid `genieIdentity` fails the app
|
|
@@ -287,11 +288,10 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
287
288
|
// app boot with a clear message rather than 500-ing the first chat request.
|
|
288
289
|
this.identityMode = resolveIdentityMode(this.config.genieIdentity);
|
|
289
290
|
const spaces = resolveGenieSpaces(this.config, this.context);
|
|
290
|
-
this.
|
|
291
|
+
this.resolvedFeedbackModes = resolveFeedbackModes(this.config.feedbackMode, {
|
|
291
292
|
genie: this.config.genieAgentMode !== false && Object.keys(spaces).length > 0,
|
|
292
293
|
mlflow: mlflowEnabled(),
|
|
293
294
|
});
|
|
294
|
-
this.config.feedbackMode = this.resolvedFeedbackMode;
|
|
295
295
|
if (this.config.observability !== false) {
|
|
296
296
|
await configureNativeHttpTelemetry({
|
|
297
297
|
// A low-level request without an active semantic span becomes a noisy
|
|
@@ -497,7 +497,7 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
497
497
|
basePath: this.basePath,
|
|
498
498
|
defaultAgent: this.built?.defaultAgentId ?? FALLBACK_AGENT_ID,
|
|
499
499
|
agents: Object.keys(this.built?.agents ?? {}),
|
|
500
|
-
|
|
500
|
+
feedbackModes: this.feedbackModes(),
|
|
501
501
|
backgroundTurns: this.config.backgroundTurns !== false,
|
|
502
502
|
followUpSuggestions: followUps?.mode ?? "none",
|
|
503
503
|
followUpSuggestionLimit: followUps?.limit ?? 3,
|
|
@@ -509,9 +509,14 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
509
509
|
return config as unknown as Record<string, unknown>;
|
|
510
510
|
}
|
|
511
511
|
|
|
512
|
-
/** Resolve the
|
|
513
|
-
private
|
|
514
|
-
return this.
|
|
512
|
+
/** Resolve the feedback destinations exposed for per-message routing. */
|
|
513
|
+
private feedbackModes(): FeedbackDestination[] {
|
|
514
|
+
return this.resolvedFeedbackModes;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** Whether one feedback destination is enabled. */
|
|
518
|
+
private feedbackEnabled(mode: FeedbackDestination): boolean {
|
|
519
|
+
return this.resolvedFeedbackModes.includes(mode);
|
|
515
520
|
}
|
|
516
521
|
|
|
517
522
|
override injectRoutes(router: IAppRouter): void {
|
|
@@ -984,15 +989,14 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
984
989
|
return this.execute(() => write(client), genieConversationWriteDefaults);
|
|
985
990
|
}
|
|
986
991
|
|
|
987
|
-
/** Register
|
|
992
|
+
/** Register every feedback destination enabled by plugin mode. */
|
|
988
993
|
private registerFeedbackRoutes(router: IAppRouter): void {
|
|
989
994
|
this.route(router, {
|
|
990
995
|
name: "feedback",
|
|
991
996
|
method: "post",
|
|
992
997
|
path: MASTRA_ROUTES.feedback,
|
|
993
998
|
handler: async (req, res) => {
|
|
994
|
-
|
|
995
|
-
if (mode === "none") {
|
|
999
|
+
if (this.resolvedFeedbackModes.length === 0) {
|
|
996
1000
|
res.status(404).json({ ok: false });
|
|
997
1001
|
return;
|
|
998
1002
|
}
|
|
@@ -1008,7 +1012,7 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
1008
1012
|
});
|
|
1009
1013
|
return;
|
|
1010
1014
|
}
|
|
1011
|
-
if (parsed.data.mode
|
|
1015
|
+
if (!this.feedbackEnabled(parsed.data.mode)) {
|
|
1012
1016
|
res.status(400).json({ ok: false, error: INVALID_FEEDBACK_MESSAGE });
|
|
1013
1017
|
return;
|
|
1014
1018
|
}
|
|
@@ -1042,11 +1046,11 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
1042
1046
|
method: "get",
|
|
1043
1047
|
path: MASTRA_ROUTES.mlflowExperiment,
|
|
1044
1048
|
handler: async (req, res) => {
|
|
1045
|
-
if (this.
|
|
1049
|
+
if (!this.feedbackEnabled("mlflow")) {
|
|
1046
1050
|
res.json({ url: null });
|
|
1047
1051
|
return;
|
|
1048
1052
|
}
|
|
1049
|
-
const result = await this.scopedSelf(req).fetchMlflowExperimentUrl();
|
|
1053
|
+
const result = await this.scopedSelf(req).fetchMlflowExperimentUrl(requestUserEmail(req));
|
|
1050
1054
|
res.json({ url: result.ok ? (result.data ?? null) : null });
|
|
1051
1055
|
},
|
|
1052
1056
|
});
|
|
@@ -1229,9 +1233,14 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
1229
1233
|
* Resolve an experiment link under the active request identity. The helper
|
|
1230
1234
|
* returns no URL unless the viewer has effective `CAN_MANAGE` permission.
|
|
1231
1235
|
*/
|
|
1232
|
-
private async fetchMlflowExperimentUrl(
|
|
1236
|
+
private async fetchMlflowExperimentUrl(
|
|
1237
|
+
viewerUserName?: string,
|
|
1238
|
+
): Promise<ExecutionResult<string | undefined>> {
|
|
1233
1239
|
const client = getExecutionContext().client;
|
|
1234
|
-
return this.execute(
|
|
1240
|
+
return this.execute(
|
|
1241
|
+
() => mlflowExperimentManagerUrl(client, viewerUserName),
|
|
1242
|
+
feedbackWriteDefaults,
|
|
1243
|
+
);
|
|
1235
1244
|
}
|
|
1236
1245
|
|
|
1237
1246
|
/**
|
|
@@ -1454,7 +1463,10 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
1454
1463
|
identity: (request) =>
|
|
1455
1464
|
useServicePrincipal(this.identityMode, request) ? "service-principal" : "obo",
|
|
1456
1465
|
});
|
|
1457
|
-
this.mastraServer = new MastraServer(
|
|
1466
|
+
this.mastraServer = new MastraServer(
|
|
1467
|
+
this.config,
|
|
1468
|
+
{ mlflowFeedback: this.feedbackEnabled("mlflow") },
|
|
1469
|
+
{
|
|
1458
1470
|
app: this.mastraApp,
|
|
1459
1471
|
mastra: this.mastra,
|
|
1460
1472
|
prefix: "",
|
|
@@ -1465,7 +1477,8 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
1465
1477
|
version: "v7",
|
|
1466
1478
|
}),
|
|
1467
1479
|
],
|
|
1468
|
-
|
|
1480
|
+
},
|
|
1481
|
+
);
|
|
1469
1482
|
await this.mastraServer.init();
|
|
1470
1483
|
attachStaleResumeRecovery(this.mastraApp);
|
|
1471
1484
|
this.logger.info("ready", {
|
|
@@ -1476,7 +1489,7 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
1476
1489
|
apiAccess: this.config.apiAccess ?? "scoped",
|
|
1477
1490
|
mcp: this.mcp ? `${this.basePath}${this.mcp.httpPath}` : "off",
|
|
1478
1491
|
lakebase: memoryPool !== undefined,
|
|
1479
|
-
feedback: this.
|
|
1492
|
+
feedback: this.feedbackModes(),
|
|
1480
1493
|
observability: observability !== undefined ? "mlflow" : "off",
|
|
1481
1494
|
});
|
|
1482
1495
|
}
|
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 =
|
|
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
|