@dbx-tools/appkit-mastra 0.9.182 → 0.9.184
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 +11 -5
- package/index.ts +2 -2
- package/lib/index.d.ts +2 -2
- package/lib/index.js +3 -3
- package/lib/src/agents.js +7 -8
- package/lib/src/config.d.ts +1 -1
- package/lib/src/config.js +2 -2
- package/lib/src/feedback-mode.js +1 -1
- package/lib/src/mlflow.js +1 -1
- package/lib/src/model.d.ts +2 -1
- package/lib/src/model.js +3 -2
- package/lib/src/plugin.js +1 -1
- package/lib/src/processors.d.ts +12 -4
- package/lib/src/processors.js +245 -5
- package/package.json +18 -18
- package/src/agents.ts +10 -7
- package/src/config.ts +2 -2
- package/src/feedback-mode.ts +1 -4
- package/src/mlflow.ts +1 -3
- package/src/model.ts +2 -1
- package/src/plugin.ts +11 -13
- package/src/processors.ts +260 -5
package/src/processors.ts
CHANGED
|
@@ -1,18 +1,273 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Mastra stream processors wired onto the plugin's agents.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* cannot copy a stale id into the new turn's `[chart:<id>]` markers.
|
|
4
|
+
* Input processors apply provider-aware reasoning defaults, remove incompatible
|
|
5
|
+
* provider continuation state, and scrub turn-scoped chart ids from Memory.
|
|
7
6
|
*
|
|
8
7
|
* @module
|
|
9
8
|
*/
|
|
10
9
|
|
|
11
|
-
import { log } from "@dbx-tools/shared-core";
|
|
12
|
-
import
|
|
10
|
+
import { isRecord, log } from "@dbx-tools/shared-core";
|
|
11
|
+
import { ProviderHistoryCompat } from "@mastra/core/processors";
|
|
12
|
+
import type {
|
|
13
|
+
CompatRule,
|
|
14
|
+
InputProcessor,
|
|
15
|
+
ProcessInputArgs,
|
|
16
|
+
ProcessInputStepArgs,
|
|
17
|
+
ProcessInputStepResult,
|
|
18
|
+
} from "@mastra/core/processors";
|
|
19
|
+
import type { JSONValue } from "ai";
|
|
20
|
+
|
|
21
|
+
import { RESPONSES_PROVIDER_OPTIONS } from "./model.ts";
|
|
13
22
|
|
|
14
23
|
const logger = log.logger("mastra/processors");
|
|
15
24
|
|
|
25
|
+
const RESPONSES_PROVIDER_SUFFIX = ".responses";
|
|
26
|
+
const MODEL_BOUND_PROVIDER_FIELDS = [
|
|
27
|
+
"itemId",
|
|
28
|
+
"resultItemId",
|
|
29
|
+
"reasoningEncryptedContent",
|
|
30
|
+
] as const;
|
|
31
|
+
|
|
32
|
+
interface ModelBoundResponsesState {
|
|
33
|
+
encryptedReasoning: Set<string>;
|
|
34
|
+
itemIds: Set<string>;
|
|
35
|
+
reasoningItemIds: Set<string>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
39
|
+
return isRecord(value) ? value : undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function responsesProviderName(model: unknown): string | undefined {
|
|
43
|
+
const provider = asRecord(model)?.provider;
|
|
44
|
+
if (typeof provider !== "string" || !provider.endsWith(RESPONSES_PROVIDER_SUFFIX)) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
return provider.slice(0, -RESPONSES_PROVIDER_SUFFIX.length);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Add stateless Responses defaults only after Mastra resolves the step model. */
|
|
51
|
+
export function buildResponsesProviderOptionsProcessor(sendReasoning: boolean): InputProcessor {
|
|
52
|
+
return {
|
|
53
|
+
id: "databricks-responses-provider-options",
|
|
54
|
+
description:
|
|
55
|
+
"Applies stateless replay and visible reasoning defaults to Databricks Responses models.",
|
|
56
|
+
processInputStep(args: ProcessInputStepArgs) {
|
|
57
|
+
const providerName = responsesProviderName(args.model);
|
|
58
|
+
if (!providerName) return;
|
|
59
|
+
const existing = args.providerOptions?.[providerName] ?? {};
|
|
60
|
+
const next: Record<string, JSONValue> = {
|
|
61
|
+
...RESPONSES_PROVIDER_OPTIONS.openai,
|
|
62
|
+
...existing,
|
|
63
|
+
store: false,
|
|
64
|
+
};
|
|
65
|
+
if (!sendReasoning) delete next.reasoningSummary;
|
|
66
|
+
return {
|
|
67
|
+
providerOptions: {
|
|
68
|
+
...args.providerOptions,
|
|
69
|
+
[providerName]: next,
|
|
70
|
+
},
|
|
71
|
+
} satisfies ProcessInputStepResult;
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function providerState(metadata: unknown): {
|
|
77
|
+
encryptedReasoning: string[];
|
|
78
|
+
itemIds: string[];
|
|
79
|
+
} {
|
|
80
|
+
const encryptedReasoning: string[] = [];
|
|
81
|
+
const itemIds: string[] = [];
|
|
82
|
+
const providers = asRecord(metadata);
|
|
83
|
+
if (!providers) return { encryptedReasoning, itemIds };
|
|
84
|
+
for (const value of Object.values(providers)) {
|
|
85
|
+
const provider = asRecord(value);
|
|
86
|
+
if (!provider) continue;
|
|
87
|
+
if (typeof provider.reasoningEncryptedContent === "string") {
|
|
88
|
+
encryptedReasoning.push(provider.reasoningEncryptedContent);
|
|
89
|
+
}
|
|
90
|
+
for (const field of ["itemId", "resultItemId"] as const) {
|
|
91
|
+
if (typeof provider[field] === "string") itemIds.push(provider[field]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return { encryptedReasoning, itemIds };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function foreignResponsesState(
|
|
98
|
+
messageList: Parameters<NonNullable<CompatRule["applyToPrompt"]>>[0]["messageList"],
|
|
99
|
+
targetModelId: string,
|
|
100
|
+
): ModelBoundResponsesState {
|
|
101
|
+
const state: ModelBoundResponsesState = {
|
|
102
|
+
encryptedReasoning: new Set(),
|
|
103
|
+
itemIds: new Set(),
|
|
104
|
+
reasoningItemIds: new Set(),
|
|
105
|
+
};
|
|
106
|
+
if (!messageList) return state;
|
|
107
|
+
for (const message of messageList.get.all.db()) {
|
|
108
|
+
if (message.role !== "assistant") continue;
|
|
109
|
+
const originModelId = message.content.metadata?.modelId;
|
|
110
|
+
if (typeof originModelId !== "string" || originModelId === targetModelId) continue;
|
|
111
|
+
for (const part of message.content.parts) {
|
|
112
|
+
const values = providerState(part.providerMetadata);
|
|
113
|
+
for (const value of values.encryptedReasoning) state.encryptedReasoning.add(value);
|
|
114
|
+
for (const value of values.itemIds) {
|
|
115
|
+
state.itemIds.add(value);
|
|
116
|
+
if (part.type === "reasoning") state.reasoningItemIds.add(value);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return state;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function hasForeignReasoningState(part: unknown, state: ModelBoundResponsesState): boolean {
|
|
124
|
+
const record = asRecord(part);
|
|
125
|
+
const values = providerState(record?.providerOptions ?? record?.providerMetadata);
|
|
126
|
+
return (
|
|
127
|
+
values.encryptedReasoning.some((value) => state.encryptedReasoning.has(value)) ||
|
|
128
|
+
values.itemIds.some((value) => state.reasoningItemIds.has(value))
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function stripForeignItemIds<T>(part: T, state: ModelBoundResponsesState): T {
|
|
133
|
+
const record = asRecord(part);
|
|
134
|
+
const providerOptions = asRecord(record?.providerOptions);
|
|
135
|
+
if (!providerOptions) return part;
|
|
136
|
+
let changed = false;
|
|
137
|
+
const nextProviderOptions: Record<string, unknown> = {};
|
|
138
|
+
for (const [name, value] of Object.entries(providerOptions)) {
|
|
139
|
+
const provider = asRecord(value);
|
|
140
|
+
if (!provider) {
|
|
141
|
+
nextProviderOptions[name] = value;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const nextProvider = { ...provider };
|
|
145
|
+
for (const field of MODEL_BOUND_PROVIDER_FIELDS) {
|
|
146
|
+
const fieldValue = nextProvider[field];
|
|
147
|
+
const foreign =
|
|
148
|
+
typeof fieldValue === "string" &&
|
|
149
|
+
(field === "reasoningEncryptedContent"
|
|
150
|
+
? state.encryptedReasoning.has(fieldValue)
|
|
151
|
+
: state.itemIds.has(fieldValue));
|
|
152
|
+
if (!foreign) continue;
|
|
153
|
+
delete nextProvider[field];
|
|
154
|
+
changed = true;
|
|
155
|
+
}
|
|
156
|
+
if (Object.keys(nextProvider).length > 0) nextProviderOptions[name] = nextProvider;
|
|
157
|
+
}
|
|
158
|
+
if (!changed) return part;
|
|
159
|
+
return {
|
|
160
|
+
...record,
|
|
161
|
+
...(Object.keys(nextProviderOptions).length > 0
|
|
162
|
+
? { providerOptions: nextProviderOptions }
|
|
163
|
+
: { providerOptions: undefined }),
|
|
164
|
+
} as T;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function sanitizeResponsesReasoningPart<T>(part: T, providerName: string): T | undefined {
|
|
168
|
+
const record = asRecord(part);
|
|
169
|
+
if (record?.type !== "reasoning") return part;
|
|
170
|
+
const providerOptions = asRecord(record.providerOptions);
|
|
171
|
+
const provider = asRecord(providerOptions?.[providerName]);
|
|
172
|
+
const replayable =
|
|
173
|
+
provider &&
|
|
174
|
+
(typeof provider.reasoningEncryptedContent === "string" ||
|
|
175
|
+
Array.isArray(provider.reasoningSummary));
|
|
176
|
+
if (!provider || !replayable) return undefined;
|
|
177
|
+
if (provider.reasoningContent === null) return part;
|
|
178
|
+
return {
|
|
179
|
+
...record,
|
|
180
|
+
providerOptions: {
|
|
181
|
+
...providerOptions,
|
|
182
|
+
[providerName]: {
|
|
183
|
+
...provider,
|
|
184
|
+
reasoningContent: null,
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
} as T;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Omit incomplete thoughts and raw reasoning content that Responses cannot replay. */
|
|
191
|
+
export const sanitizeResponsesReasoningReplay: CompatRule = {
|
|
192
|
+
name: "databricks-sanitize-responses-reasoning-replay",
|
|
193
|
+
applyToPrompt({ prompt, model }) {
|
|
194
|
+
const providerName = responsesProviderName(model);
|
|
195
|
+
if (!providerName) return;
|
|
196
|
+
|
|
197
|
+
let changed = false;
|
|
198
|
+
const next = [];
|
|
199
|
+
for (const message of prompt) {
|
|
200
|
+
if (message.role !== "assistant" || !Array.isArray(message.content)) {
|
|
201
|
+
next.push(message);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const content = [];
|
|
205
|
+
let messageChanged = false;
|
|
206
|
+
for (const part of message.content) {
|
|
207
|
+
const sanitized = sanitizeResponsesReasoningPart(part, providerName);
|
|
208
|
+
if (sanitized !== part) {
|
|
209
|
+
changed = true;
|
|
210
|
+
messageChanged = true;
|
|
211
|
+
}
|
|
212
|
+
if (sanitized) content.push(sanitized);
|
|
213
|
+
}
|
|
214
|
+
if (!messageChanged) {
|
|
215
|
+
next.push(message);
|
|
216
|
+
} else if (content.length > 0) {
|
|
217
|
+
next.push({ ...message, content });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return changed ? next : undefined;
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/** Remove Responses continuation state produced by a different concrete model. */
|
|
225
|
+
export const stripForeignResponsesState: CompatRule = {
|
|
226
|
+
name: "databricks-strip-foreign-responses-state",
|
|
227
|
+
applyToPrompt({ prompt, model, messageList }) {
|
|
228
|
+
if (!responsesProviderName(model)) return;
|
|
229
|
+
const targetModelId = asRecord(model)?.modelId;
|
|
230
|
+
if (typeof targetModelId !== "string") return;
|
|
231
|
+
const state = foreignResponsesState(messageList, targetModelId);
|
|
232
|
+
if (state.encryptedReasoning.size === 0 && state.itemIds.size === 0) return;
|
|
233
|
+
|
|
234
|
+
let changed = false;
|
|
235
|
+
const next = [];
|
|
236
|
+
for (const message of prompt) {
|
|
237
|
+
if (message.role !== "assistant" || !Array.isArray(message.content)) {
|
|
238
|
+
next.push(message);
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
const content = [];
|
|
242
|
+
let messageChanged = false;
|
|
243
|
+
for (const part of message.content) {
|
|
244
|
+
if (part.type === "reasoning" && hasForeignReasoningState(part, state)) {
|
|
245
|
+
changed = true;
|
|
246
|
+
messageChanged = true;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
const stripped = stripForeignItemIds(part, state);
|
|
250
|
+
if (stripped !== part) {
|
|
251
|
+
changed = true;
|
|
252
|
+
messageChanged = true;
|
|
253
|
+
}
|
|
254
|
+
content.push(stripped);
|
|
255
|
+
}
|
|
256
|
+
if (!messageChanged) {
|
|
257
|
+
next.push(message);
|
|
258
|
+
} else if (content.length > 0) {
|
|
259
|
+
next.push({ ...message, content });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return changed ? next : undefined;
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/** Mastra-owned compatibility processor extended for Databricks Responses routing. */
|
|
267
|
+
export const providerHistoryCompatProcessor = new ProviderHistoryCompat({
|
|
268
|
+
additionalRules: [sanitizeResponsesReasoningReplay, stripForeignResponsesState],
|
|
269
|
+
});
|
|
270
|
+
|
|
16
271
|
/**
|
|
17
272
|
* Recursively clone `value`, omitting any property whose key is
|
|
18
273
|
* `chartId`. Arrays are mapped element-wise; primitives are
|