@dbx-tools/appkit-mastra 0.1.28 → 0.1.29
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/dist/src/agents.js +4 -1
- package/dist/src/model.js +2 -2
- package/dist/src/plugin.js +1 -39
- package/dist/src/processor.d.ts +8 -0
- package/dist/src/processor.js +40 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/agents.ts +4 -1
- package/src/model.ts +2 -7
- package/src/plugin.ts +1 -41
- package/src/processor.ts +52 -0
package/package.json
CHANGED
|
@@ -9,13 +9,13 @@
|
|
|
9
9
|
}
|
|
10
10
|
},
|
|
11
11
|
"name": "@dbx-tools/appkit-mastra",
|
|
12
|
-
"version": "0.1.
|
|
12
|
+
"version": "0.1.29",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@databricks/sdk-experimental": "^0.17",
|
|
15
|
-
"@dbx-tools/appkit-mastra-shared": "0.1.
|
|
16
|
-
"@dbx-tools/genie": "0.1.
|
|
17
|
-
"@dbx-tools/genie-shared": "0.1.
|
|
18
|
-
"@dbx-tools/shared": "0.1.
|
|
15
|
+
"@dbx-tools/appkit-mastra-shared": "0.1.29",
|
|
16
|
+
"@dbx-tools/genie": "0.1.29",
|
|
17
|
+
"@dbx-tools/genie-shared": "0.1.29",
|
|
18
|
+
"@dbx-tools/shared": "0.1.29",
|
|
19
19
|
"@mastra/ai-sdk": "^1",
|
|
20
20
|
"@mastra/core": "^1",
|
|
21
21
|
"@mastra/express": "^1",
|
package/src/agents.ts
CHANGED
|
@@ -26,6 +26,7 @@ import type { MemoryBuilder } from "./memory.js";
|
|
|
26
26
|
import { buildModel, FALLBACK_MODEL_IDS } from "./model.js";
|
|
27
27
|
import { stripStaleChartsProcessor } from "./processors/strip-stale-charts.js";
|
|
28
28
|
import { buildRenderDataTool } from "./chart.js";
|
|
29
|
+
import { ResultProcessor } from "./processor.js";
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* Tool record accepted by every Mastra `Agent.tools` field and by the
|
|
@@ -433,6 +434,7 @@ export async function buildAgents(opts: {
|
|
|
433
434
|
// chartIds from prior assistant tool results into the new
|
|
434
435
|
// turn's `[chart:<id>]` markers. Opt out per-plugin via
|
|
435
436
|
// `config.stripStaleCharts: false`.
|
|
437
|
+
const outputProcessors = [new ResultProcessor()];
|
|
436
438
|
const inputProcessors =
|
|
437
439
|
config.stripStaleCharts === false ? [] : [stripStaleChartsProcessor];
|
|
438
440
|
const agents: Record<string, Agent> = {};
|
|
@@ -451,7 +453,8 @@ export async function buildAgents(opts: {
|
|
|
451
453
|
},
|
|
452
454
|
tools,
|
|
453
455
|
...(memory ? { memory } : {}),
|
|
454
|
-
|
|
456
|
+
inputProcessors,
|
|
457
|
+
outputProcessors,
|
|
455
458
|
});
|
|
456
459
|
// Surface the effective default model per agent so operators can
|
|
457
460
|
// see at a glance which endpoint each agent points at without
|
package/src/model.ts
CHANGED
|
@@ -25,12 +25,7 @@
|
|
|
25
25
|
* the input verbatim and let Databricks return the canonical error.
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
-
import {
|
|
29
|
-
commonUtils,
|
|
30
|
-
logUtils,
|
|
31
|
-
netUtils,
|
|
32
|
-
stringUtils,
|
|
33
|
-
} from "@dbx-tools/shared";
|
|
28
|
+
import { commonUtils, logUtils, netUtils, stringUtils } from "@dbx-tools/shared";
|
|
34
29
|
import type { MastraModelConfig } from "@mastra/core/llm";
|
|
35
30
|
import type { RequestContext } from "@mastra/core/request-context";
|
|
36
31
|
|
|
@@ -390,7 +385,7 @@ const setupFetchInterceptor = commonUtils.memoize((): void => {
|
|
|
390
385
|
const log = logUtils.logger("mastra/llm");
|
|
391
386
|
const original = globalThis.fetch.bind(globalThis);
|
|
392
387
|
globalThis.fetch = (async (input, init) => {
|
|
393
|
-
const url = netUtils.
|
|
388
|
+
const url = netUtils.urlBuilder(input);
|
|
394
389
|
if (
|
|
395
390
|
!url ||
|
|
396
391
|
!url.pathname.startsWith(SERVING_ENDPOINTS_PATH_PREFIX) ||
|
package/src/plugin.ts
CHANGED
|
@@ -54,10 +54,6 @@ import { historyRoute } from "./history.js";
|
|
|
54
54
|
import { createMemoryBuilder, needsLakebase } from "./memory.js";
|
|
55
55
|
import { buildObservability } from "./observability.js";
|
|
56
56
|
import { attachRoutePatchMiddleware, MastraServer } from "./server.js";
|
|
57
|
-
import {
|
|
58
|
-
installStreamEventInterceptor,
|
|
59
|
-
type StreamFrameInterceptor,
|
|
60
|
-
} from "./intercept.js";
|
|
61
57
|
import {
|
|
62
58
|
clearServingEndpointsCache,
|
|
63
59
|
listServingEndpoints,
|
|
@@ -69,6 +65,7 @@ import {
|
|
|
69
65
|
isStatementNotFoundError,
|
|
70
66
|
STATEMENT_ROW_CAP,
|
|
71
67
|
} from "./statement.js";
|
|
68
|
+
import { ResultProcessor } from "./processor.js";
|
|
72
69
|
|
|
73
70
|
const GENIE_MANIFEST = appkitUtils.data(genie).plugin.manifest;
|
|
74
71
|
const LAKEBASE_MANIFEST = appkitUtils.data(lakebase).plugin.manifest;
|
|
@@ -311,11 +308,6 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
311
308
|
|
|
312
309
|
router.use("", (req, res, next) => {
|
|
313
310
|
if (!this.mastraApp) return res.status(503).end();
|
|
314
|
-
// Run each Mastra `/stream` SSE frame through the interceptor
|
|
315
|
-
// (keep / rewrite / drop). Only engages on a
|
|
316
|
-
// `200 text/event-stream` body, so the JSON routes above and any
|
|
317
|
-
// error response are untouched.
|
|
318
|
-
installStreamEventInterceptor(res, interceptStreamFrame);
|
|
319
311
|
return this.userScopedSelf(req).mastraApp!(req, res, next);
|
|
320
312
|
});
|
|
321
313
|
}
|
|
@@ -502,36 +494,4 @@ function parseStatementLimit(raw: unknown): number | undefined {
|
|
|
502
494
|
return Math.floor(n);
|
|
503
495
|
}
|
|
504
496
|
|
|
505
|
-
/**
|
|
506
|
-
* {@link StreamFrameInterceptor} for Mastra `/stream` responses.
|
|
507
|
-
*
|
|
508
|
-
* Removes large, redundant payloads from terminal `step-finish`,
|
|
509
|
-
* `finish`, and `tool-result` frames before they reach the browser.
|
|
510
|
-
* These frames often repeat information already delivered incrementally
|
|
511
|
-
* via streamed text and tool events, including full tool outputs,
|
|
512
|
-
* accumulated responses, message history, SQL results, chart data, and
|
|
513
|
-
* other large result payloads.
|
|
514
|
-
*
|
|
515
|
-
* The interceptor deletes heavyweight payload properties
|
|
516
|
-
* (`output`, `messages`, `response`, and `result`) while preserving the
|
|
517
|
-
* event envelope and lifecycle metadata required by the client.
|
|
518
|
-
*
|
|
519
|
-
* Deletion is key based, so streams that do not contain these
|
|
520
|
-
* fields are passed through unchanged.
|
|
521
|
-
*/
|
|
522
|
-
const interceptStreamFrame: StreamFrameInterceptor = (chunk) => {
|
|
523
|
-
if (!commonUtils.isRecord(chunk)) return true;
|
|
524
|
-
if (!["step-finish", "finish", "tool-result"].find((type) => type === chunk.type))
|
|
525
|
-
return true;
|
|
526
|
-
const payload = chunk.payload;
|
|
527
|
-
if (!commonUtils.isRecord(payload)) return true;
|
|
528
|
-
const trimmedPayload = commonUtils.deleteKeys(payload, [
|
|
529
|
-
"output",
|
|
530
|
-
"messages",
|
|
531
|
-
"response",
|
|
532
|
-
"result",
|
|
533
|
-
]);
|
|
534
|
-
return trimmedPayload ? { replace: chunk } : true;
|
|
535
|
-
};
|
|
536
|
-
|
|
537
497
|
export const mastra = toPlugin(MastraPlugin);
|
package/src/processor.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Processor,
|
|
3
|
+
ProcessAPIErrorArgs,
|
|
4
|
+
ProcessAPIErrorResult,
|
|
5
|
+
ProcessOutputStreamArgs,
|
|
6
|
+
} from "@mastra/core/processors";
|
|
7
|
+
import type { ChunkType } from "@mastra/core/workflows";
|
|
8
|
+
|
|
9
|
+
export class ResultProcessor implements Processor {
|
|
10
|
+
id = "result-processor";
|
|
11
|
+
|
|
12
|
+
// Tell Mastra to also route tool/data parts to this processor method
|
|
13
|
+
processDataParts = true;
|
|
14
|
+
|
|
15
|
+
async processOutputStream({ part }: { part: any }): Promise<any | null> {
|
|
16
|
+
// 1. Guard clause: Ensure the chunk is a valid object
|
|
17
|
+
if (!part || typeof part !== "object") {
|
|
18
|
+
return part;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 2. Filter for the targeted frame types
|
|
22
|
+
const targetedTypes = ["step-finish", "finish", "tool-result", "data-tool-agent"];
|
|
23
|
+
if (!targetedTypes.includes(part.type)) {
|
|
24
|
+
return part; // Return unchanged to pass-through
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 3. Check for the presence of a payload object
|
|
28
|
+
const payload = part.payload;
|
|
29
|
+
if (!payload || typeof payload !== "object") {
|
|
30
|
+
return part;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 4. Safely delete the unwanted keys from the payload reference
|
|
34
|
+
const keysToDelete = ["output", "messages", "response", "result"];
|
|
35
|
+
for (const key of keysToDelete) {
|
|
36
|
+
if (key in payload) {
|
|
37
|
+
const value = payload[key];
|
|
38
|
+
if (typeof value === "object") {
|
|
39
|
+
payload[key] = {};
|
|
40
|
+
} else if (Array.isArray(value)) {
|
|
41
|
+
payload[key] = [];
|
|
42
|
+
} else {
|
|
43
|
+
delete payload[key];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 5. Return the modified part object. Mastra handles re-serialization
|
|
49
|
+
// for the outbound SSE client stream automatically.
|
|
50
|
+
return part;
|
|
51
|
+
}
|
|
52
|
+
}
|