@dbx-tools/appkit-mastra 0.1.0

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.
@@ -0,0 +1,306 @@
1
+ /**
2
+ * Agent registration for the Mastra AppKit plugin.
3
+ *
4
+ * Mirrors the shape of the AppKit `agents` plugin (`config.agents` map
5
+ * of {@link MastraAgentDefinition}, dual-form `tools` accepting a plain
6
+ * record or a `(plugins) => tools` callback). Resolves each definition
7
+ * into a Mastra `Agent` instance during plugin setup; user-supplied
8
+ * tool callbacks are invoked exactly once with a typed
9
+ * {@link MastraPlugins} map built from registered sibling plugins.
10
+ *
11
+ * When no agents are registered the plugin falls back to a single
12
+ * built-in analyst so the bare `mastra()` call still mounts a working
13
+ * `chatRoute` agent for demos.
14
+ */
15
+ import { logUtils, pluginUtils } from "@dbx-tools/appkit-shared";
16
+ import { Agent } from "@mastra/core/agent";
17
+ import type { AgentConfig, ToolsInput } from "@mastra/core/agent";
18
+ import type { Tool } from "@mastra/core/tools";
19
+ import type { PgVectorConfig, PostgresStoreConfig } from "@mastra/pg";
20
+ import type { MastraPluginConfig } from "./config.js";
21
+ import type { MemoryBuilder } from "./memory.js";
22
+ /**
23
+ * Tool record accepted by every Mastra `Agent.tools` field and by the
24
+ * `tools(plugins)` callback on {@link MastraAgentDefinition}.
25
+ *
26
+ * Alias of Mastra's `ToolsInput`, so it already accepts:
27
+ *
28
+ * - Mastra tools built with {@link createTool} (or `new Tool(...)`)
29
+ * - Mastra tools built with the AppKit-shaped {@link tool} wrapper
30
+ * below
31
+ * - Vercel AI SDK tools (`tool({ ... })` from `ai`)
32
+ * - Provider-defined tools (e.g. `openai.tools.webSearch(...)`)
33
+ *
34
+ * Existing tool libraries drop in as-is - nothing in this package
35
+ * forces a rebuild.
36
+ */
37
+ export type MastraTools = ToolsInput;
38
+ /** Re-export of Mastra's native `createTool` for full-feature access. */
39
+ export { createTool } from "@mastra/core/tools";
40
+ /**
41
+ * AppKit-shaped tool factory. Lets users mix-and-match tools across
42
+ * AppKit's `agents` plugin and `mastra` with a single import:
43
+ *
44
+ * ```ts
45
+ * import { tool } from "@dbx-tools/appkit-mastra";
46
+ * import { z } from "zod";
47
+ *
48
+ * get_weather: tool({
49
+ * description: "Weather",
50
+ * schema: z.object({ city: z.string() }),
51
+ * execute: async ({ city }) => `Sunny in ${city}`,
52
+ * }),
53
+ * ```
54
+ *
55
+ * Maps onto Mastra's `createTool`:
56
+ *
57
+ * - `description` -> `description` (required)
58
+ * - `schema` -> `inputSchema` (optional)
59
+ * - `execute(input)` -> `execute(input, ctx)` - Mastra already calls
60
+ * the first arg with the parsed inputs, so the body shape is
61
+ * identical. The Mastra `context` arg is forwarded as the second
62
+ * parameter when the caller declares it.
63
+ * - `id`: optional. Defaults to a stable identifier derived from
64
+ * `description` (slugified, with a short hash suffix for
65
+ * uniqueness). Pass an explicit `id` when you need a stable string
66
+ * for tracing or MCP exposure.
67
+ *
68
+ * Reach for {@link createTool} when you need Mastra-only fields
69
+ * (`outputSchema`, `suspendSchema`, `requireApproval`, `mcp`, etc.).
70
+ */
71
+ export declare function tool(opts: AppKitToolOptions): Tool;
72
+ /**
73
+ * Input shape for the AppKit-style {@link tool} factory. A trimmed
74
+ * subset of Mastra's `createTool` options that mirrors the
75
+ * `@databricks/appkit/beta` `tool({ description, schema, execute })`
76
+ * signature.
77
+ *
78
+ * Generics are intentionally absent - inference flows through the
79
+ * caller's `schema` (typically a Zod object), and the `execute` body
80
+ * destructures naturally from that. Reach for {@link createTool} when
81
+ * you need the fully-typed input/output schemas wired explicitly.
82
+ */
83
+ export interface AppKitToolOptions {
84
+ /** Optional stable identifier; auto-derived from `description` when omitted. */
85
+ id?: string;
86
+ /** Human-readable description shown to the model. Required. */
87
+ description: string;
88
+ /**
89
+ * Optional input schema (any Standard Schema instance, e.g. Zod).
90
+ * Maps to Mastra's `inputSchema`; passed through to the model
91
+ * verbatim.
92
+ */
93
+ schema?: unknown;
94
+ /**
95
+ * Execute body. First arg is the parsed input (typed off `schema`
96
+ * when supplied), second arg is the full Mastra execution context
97
+ * (request context, abort signal, mastra instance) if you need it.
98
+ */
99
+ execute: (input: any, context?: unknown) => unknown;
100
+ }
101
+ /**
102
+ * Identity helper that brands a definition as a Mastra agent. Mirrors
103
+ * AppKit's `createAgent(def)` so the registration shape matches:
104
+ *
105
+ * ```ts
106
+ * const support = createAgent({
107
+ * instructions: "...",
108
+ * model: "databricks-claude-sonnet-4-6",
109
+ * tools(plugins) { return { ... }; },
110
+ * });
111
+ * ```
112
+ *
113
+ * Returns the definition unchanged - the wrapper exists only to anchor
114
+ * type inference and to match the AppKit API surface.
115
+ */
116
+ export declare function createAgent<T extends MastraAgentDefinition>(def: T): T;
117
+ /**
118
+ * Filter / rename options accepted by every plugin's `.toolkit()`
119
+ * method. Mirrors AppKit's `ToolkitOptions` verbatim so options pass
120
+ * through unchanged - the underlying AppKit plugin does the filtering
121
+ * and we just adapt the resulting entries into Mastra tools.
122
+ */
123
+ export interface ToolkitOptions {
124
+ /**
125
+ * Key prefix prepended to every tool name. AppKit's default is
126
+ * `${pluginName}.` when omitted; pass an explicit `""` to drop it.
127
+ */
128
+ prefix?: string;
129
+ /** Allowlist of local tool names. */
130
+ only?: string[];
131
+ /** Denylist of local tool names. */
132
+ except?: string[];
133
+ /** Remap specific local names to different keys. */
134
+ rename?: Record<string, string>;
135
+ }
136
+ /**
137
+ * Toolkit provider shape every entry in the {@link MastraPlugins} map
138
+ * exposes. Identical to AppKit's `PluginToolkitProvider` - any AppKit
139
+ * plugin that implements the standard `ToolProvider` interface
140
+ * (`getAgentTools` + `executeAgentTool` + `toolkit`) is reachable
141
+ * through this surface automatically.
142
+ */
143
+ export interface MastraPluginToolkitProvider {
144
+ /**
145
+ * Returns a Mastra-shaped tools record adapted from the plugin's
146
+ * agent tools. Each tool dispatches back through the plugin's
147
+ * `executeAgentTool` so OBO auth and telemetry spans stay intact.
148
+ */
149
+ toolkit(opts?: ToolkitOptions): MastraTools;
150
+ }
151
+ /**
152
+ * Plugin map handed to the function form of
153
+ * {@link MastraAgentDefinition.tools}. Mirrors AppKit's `Plugins`
154
+ * type exactly: a string-keyed record where every value exposes
155
+ * `.toolkit(opts)`.
156
+ *
157
+ * Implemented as a runtime Proxy that auto-discovers any registered
158
+ * AppKit plugin implementing the standard `ToolProvider` interface
159
+ * (`analytics`, `files`, `lakebase`, `genie`, plus any third-party
160
+ * plugin that does the same). Unknown names resolve to `undefined`
161
+ * at runtime, so guard with `?.` and `?? {}` when spreading from a
162
+ * plugin that may not be registered in every environment.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * createAgent({
167
+ * instructions: "...",
168
+ * tools(plugins) {
169
+ * return {
170
+ * ...plugins.analytics.toolkit(),
171
+ * ...plugins.files.toolkit({ only: ["uploads.read"] }),
172
+ * get_weather: tool({
173
+ * description: "Weather",
174
+ * schema: z.object({ city: z.string() }),
175
+ * execute: async ({ city }) => `Sunny in ${city}`,
176
+ * }),
177
+ * };
178
+ * },
179
+ * });
180
+ * ```
181
+ */
182
+ export type MastraPlugins = Record<string, MastraPluginToolkitProvider>;
183
+ /** Function form of {@link MastraAgentDefinition.tools}. */
184
+ export type MastraToolsFn = (plugins: MastraPlugins) => MastraTools | Promise<MastraTools>;
185
+ /**
186
+ * A code-defined Mastra agent. Mirrors the shape AppKit's `agents`
187
+ * plugin uses for `AgentDefinition`. The registry key under
188
+ * `config.agents` is what `chatRoute` matches on; `name` is purely
189
+ * informational (defaults to the key).
190
+ */
191
+ export interface MastraAgentDefinition {
192
+ /** Display name used as `Agent.name`. Defaults to the registry key. */
193
+ name?: string;
194
+ /** Optional long-form description; surfaced as `Agent.description`. */
195
+ description?: string;
196
+ /** System prompt body. */
197
+ instructions: string;
198
+ /**
199
+ * Per-agent model override.
200
+ *
201
+ * - `undefined` (default): falls back to the workspace
202
+ * `/serving-endpoints` resolver that {@link buildModel} configures
203
+ * from the per-request `WorkspaceClient`.
204
+ * - `string`: shorthand for "use the default resolver but swap the
205
+ * `modelId`" (e.g. `"databricks-meta-llama-3-3-70b-instruct"`).
206
+ * - Any other Mastra `DynamicArgument<MastraModelConfig>`: passed
207
+ * straight through to `Agent.model`. Use this when you need full
208
+ * control over auth or providerId.
209
+ */
210
+ model?: AgentConfig["model"] | string;
211
+ /**
212
+ * Per-agent tool record. Either a plain map or a callback that
213
+ * receives the typed {@link MastraPlugins} sibling-plugin index and
214
+ * returns a map. The callback runs exactly once at agent setup; the
215
+ * result is cached for the agent's lifetime.
216
+ */
217
+ tools?: MastraTools | MastraToolsFn;
218
+ /**
219
+ * Per-agent semantic recall (PgVector) override. Cascades from
220
+ * `config.memory`; the agent value wins when set.
221
+ *
222
+ * - `undefined` (default): inherit `config.memory`. When that's
223
+ * enabled, the agent **shares the plugin-level singleton `PgVector`
224
+ * instance** (cross-agent semantic recall across the same index).
225
+ * - `false`: disable semantic recall for this agent only.
226
+ * - `true`: enable using the shared singleton (same as default when
227
+ * plugin memory is enabled; useful to opt in when plugin disabled).
228
+ * - {@link MastraMemoryConfig} object: dedicated `PgVector` for this
229
+ * agent (private recall index). Bypasses the shared singleton.
230
+ */
231
+ memory?: boolean | MastraMemoryConfigOverride;
232
+ /**
233
+ * Per-agent thread/message storage (`PostgresStore`) override.
234
+ * Cascades from `config.storage`; the agent value wins when set.
235
+ *
236
+ * - `undefined` (default): inherit `config.storage`. When that's
237
+ * enabled, the agent gets its **own per-agent `PostgresStore`**
238
+ * keyed by `schemaName: "mastra_<agentId>"` so threads and
239
+ * messages stay isolated between agents in the same database.
240
+ * - `false`: disable storage for this agent only (purely in-memory).
241
+ * - `true`: enable with the per-agent default schema.
242
+ * - {@link MastraStorageConfigOverride} object: dedicated
243
+ * `PostgresStore` config (custom schema, connection, etc.).
244
+ */
245
+ storage?: boolean | MastraStorageConfigOverride;
246
+ }
247
+ /**
248
+ * Distributive `Omit` so unions in `PostgresStoreConfig` /
249
+ * `PgVectorConfig` keep their discriminants after the override types
250
+ * strip `id`. The built-in `Omit` collapses unions to one shape with
251
+ * common fields only, which loses the connection-style discriminants.
252
+ */
253
+ type DistributiveOmit<T, K extends keyof never> = T extends unknown ? Omit<T, K> : never;
254
+ /**
255
+ * `PostgresStoreConfig` minus `id` - per-agent overrides accept any
256
+ * Mastra-supported storage shape. `id` is filled in automatically
257
+ * from the agent registry key so traces stay stable.
258
+ */
259
+ export type MastraStorageConfigOverride = DistributiveOmit<PostgresStoreConfig, "id"> & {
260
+ id?: string;
261
+ };
262
+ /**
263
+ * `PgVectorConfig` minus `id` - per-agent overrides accept any
264
+ * Mastra-supported vector shape. `id` is filled in automatically
265
+ * from the agent registry key.
266
+ */
267
+ export type MastraMemoryConfigOverride = DistributiveOmit<PgVectorConfig, "id"> & {
268
+ id?: string;
269
+ };
270
+ /** Output of {@link buildAgents}: resolved agents plus the default id. */
271
+ export interface BuiltAgents {
272
+ agents: Record<string, Agent>;
273
+ defaultAgentId: string;
274
+ }
275
+ /** Fallback agent id used when `config.agents` is omitted entirely. */
276
+ export declare const FALLBACK_AGENT_ID = "default";
277
+ /**
278
+ * Style guardrails appended to every agent's `instructions` to curb
279
+ * common LLM-isms (em dashes, emojis, sycophantic openers, excessive
280
+ * hedging, throwaway closers). Appended rather than prepended so the
281
+ * agent's role/context comes first; the model's recency bias then
282
+ * helps the style rules dominate the response surface.
283
+ *
284
+ * Override globally via {@link MastraPluginConfig.styleInstructions}
285
+ * (pass `false` to disable entirely, or a string to replace).
286
+ */
287
+ export declare const DEFAULT_STYLE_INSTRUCTIONS = "Output style:\n\n- Plain prose. Use hyphens (-) only. Never use em dashes (\u2014) or en dashes (\u2013).\n- Never use emojis.\n- Skip openers like \"Great question\", \"Absolutely\", \"I'd be happy to help\".\n- Skip closers like \"Let me know if you have any questions\".\n- Skip self-disclaimers (\"I should mention\", \"It's important to note\").\n- Answer directly. No preamble before the actual answer.\n- Use lists and headers only when they clarify a multi-part answer; not for short replies.\n- Quote numbers, code, identifiers, and tool output verbatim. Never paraphrase them.";
288
+ /**
289
+ * Resolve every entry in `config.agents` into a Mastra `Agent`
290
+ * instance. When `config.agents` is omitted the plugin registers a
291
+ * single built-in `default` analyst so the bare `mastra()` call still
292
+ * yields a working agent.
293
+ *
294
+ * Per-agent tool callbacks are invoked once with a typed
295
+ * {@link MastraPlugins} index built from registered sibling plugins
296
+ * (currently `genie`; extend `MastraPlugins` to surface more).
297
+ *
298
+ * @throws when `config.defaultAgent` is set to an id that isn't in the
299
+ * resolved registry; this is a wiring bug, not a runtime condition.
300
+ */
301
+ export declare function buildAgents(opts: {
302
+ config: MastraPluginConfig;
303
+ context: pluginUtils.PluginContextLike | undefined;
304
+ memoryBuilder?: MemoryBuilder;
305
+ log: logUtils.Logger;
306
+ }): Promise<BuiltAgents>;
@@ -0,0 +1,379 @@
1
+ /**
2
+ * Agent registration for the Mastra AppKit plugin.
3
+ *
4
+ * Mirrors the shape of the AppKit `agents` plugin (`config.agents` map
5
+ * of {@link MastraAgentDefinition}, dual-form `tools` accepting a plain
6
+ * record or a `(plugins) => tools` callback). Resolves each definition
7
+ * into a Mastra `Agent` instance during plugin setup; user-supplied
8
+ * tool callbacks are invoked exactly once with a typed
9
+ * {@link MastraPlugins} map built from registered sibling plugins.
10
+ *
11
+ * When no agents are registered the plugin falls back to a single
12
+ * built-in analyst so the bare `mastra()` call still mounts a working
13
+ * `chatRoute` agent for demos.
14
+ */
15
+ import { genie } from "@databricks/appkit";
16
+ import { logUtils, pluginUtils, stringUtils } from "@dbx-tools/appkit-shared";
17
+ import { Agent } from "@mastra/core/agent";
18
+ import { createTool } from "@mastra/core/tools";
19
+ import { buildGenieProvider } from "./genie.js";
20
+ import { buildModel } from "./model.js";
21
+ /** Re-export of Mastra's native `createTool` for full-feature access. */
22
+ export { createTool } from "@mastra/core/tools";
23
+ /**
24
+ * AppKit-shaped tool factory. Lets users mix-and-match tools across
25
+ * AppKit's `agents` plugin and `mastra` with a single import:
26
+ *
27
+ * ```ts
28
+ * import { tool } from "@dbx-tools/appkit-mastra";
29
+ * import { z } from "zod";
30
+ *
31
+ * get_weather: tool({
32
+ * description: "Weather",
33
+ * schema: z.object({ city: z.string() }),
34
+ * execute: async ({ city }) => `Sunny in ${city}`,
35
+ * }),
36
+ * ```
37
+ *
38
+ * Maps onto Mastra's `createTool`:
39
+ *
40
+ * - `description` -> `description` (required)
41
+ * - `schema` -> `inputSchema` (optional)
42
+ * - `execute(input)` -> `execute(input, ctx)` - Mastra already calls
43
+ * the first arg with the parsed inputs, so the body shape is
44
+ * identical. The Mastra `context` arg is forwarded as the second
45
+ * parameter when the caller declares it.
46
+ * - `id`: optional. Defaults to a stable identifier derived from
47
+ * `description` (slugified, with a short hash suffix for
48
+ * uniqueness). Pass an explicit `id` when you need a stable string
49
+ * for tracing or MCP exposure.
50
+ *
51
+ * Reach for {@link createTool} when you need Mastra-only fields
52
+ * (`outputSchema`, `suspendSchema`, `requireApproval`, `mcp`, etc.).
53
+ */
54
+ export function tool(opts) {
55
+ const id = opts.id ?? deriveToolId(opts.description);
56
+ return createTool({
57
+ id,
58
+ description: opts.description,
59
+ ...(opts.schema ? { inputSchema: opts.schema } : {}),
60
+ execute: opts.execute,
61
+ });
62
+ }
63
+ /**
64
+ * Build a deterministic Mastra tool id from a description.
65
+ * Delegates to {@link stringUtils.toUniqueSlug}: slug + always-on
66
+ * SHA-1 suffix so two tools with the same leading words don't
67
+ * collide in traces. Stable across runs.
68
+ */
69
+ function deriveToolId(description) {
70
+ return stringUtils.toUniqueSlug(description, { fallbackPrefix: "tool" });
71
+ }
72
+ /**
73
+ * Identity helper that brands a definition as a Mastra agent. Mirrors
74
+ * AppKit's `createAgent(def)` so the registration shape matches:
75
+ *
76
+ * ```ts
77
+ * const support = createAgent({
78
+ * instructions: "...",
79
+ * model: "databricks-claude-sonnet-4-6",
80
+ * tools(plugins) { return { ... }; },
81
+ * });
82
+ * ```
83
+ *
84
+ * Returns the definition unchanged - the wrapper exists only to anchor
85
+ * type inference and to match the AppKit API surface.
86
+ */
87
+ export function createAgent(def) {
88
+ return def;
89
+ }
90
+ /** Fallback agent id used when `config.agents` is omitted entirely. */
91
+ export const FALLBACK_AGENT_ID = "default";
92
+ const FALLBACK_AGENT_INSTRUCTIONS = `You are a data analyst. The user will ask questions about
93
+ business metrics and may share personal preferences you should remember across turns.
94
+
95
+ Rules:
96
+
97
+ 1. Quote numbers exactly. Never invent data.
98
+ 2. When the user states a preference or durable fact about themselves
99
+ ("I'm in EU so use EUR", "always show me the SQL"), acknowledge that
100
+ you will remember it.
101
+ 3. If you don't have enough information to answer, ask a clarifying
102
+ question instead of guessing.`;
103
+ /**
104
+ * Style guardrails appended to every agent's `instructions` to curb
105
+ * common LLM-isms (em dashes, emojis, sycophantic openers, excessive
106
+ * hedging, throwaway closers). Appended rather than prepended so the
107
+ * agent's role/context comes first; the model's recency bias then
108
+ * helps the style rules dominate the response surface.
109
+ *
110
+ * Override globally via {@link MastraPluginConfig.styleInstructions}
111
+ * (pass `false` to disable entirely, or a string to replace).
112
+ */
113
+ export const DEFAULT_STYLE_INSTRUCTIONS = `Output style:
114
+
115
+ - Plain prose. Use hyphens (-) only. Never use em dashes (—) or en dashes (–).
116
+ - Never use emojis.
117
+ - Skip openers like "Great question", "Absolutely", "I'd be happy to help".
118
+ - Skip closers like "Let me know if you have any questions".
119
+ - Skip self-disclaimers ("I should mention", "It's important to note").
120
+ - Answer directly. No preamble before the actual answer.
121
+ - Use lists and headers only when they clarify a multi-part answer; not for short replies.
122
+ - Quote numbers, code, identifiers, and tool output verbatim. Never paraphrase them.`;
123
+ /**
124
+ * Resolve the style block to append to every agent's instructions.
125
+ * Returns `null` when the caller opted out (`styleInstructions: false`).
126
+ */
127
+ function resolveStyleInstructions(config) {
128
+ if (config.styleInstructions === false)
129
+ return null;
130
+ if (typeof config.styleInstructions === "string") {
131
+ return config.styleInstructions;
132
+ }
133
+ return DEFAULT_STYLE_INSTRUCTIONS;
134
+ }
135
+ /**
136
+ * Join an agent's bespoke instructions with the resolved style block.
137
+ * Returns the bespoke text unchanged when the style block is disabled.
138
+ */
139
+ function composeInstructions(agentInstructions, style) {
140
+ if (!style)
141
+ return agentInstructions;
142
+ return `${agentInstructions.trimEnd()}\n\n${style}`;
143
+ }
144
+ /**
145
+ * Resolve every entry in `config.agents` into a Mastra `Agent`
146
+ * instance. When `config.agents` is omitted the plugin registers a
147
+ * single built-in `default` analyst so the bare `mastra()` call still
148
+ * yields a working agent.
149
+ *
150
+ * Per-agent tool callbacks are invoked once with a typed
151
+ * {@link MastraPlugins} index built from registered sibling plugins
152
+ * (currently `genie`; extend `MastraPlugins` to surface more).
153
+ *
154
+ * @throws when `config.defaultAgent` is set to an id that isn't in the
155
+ * resolved registry; this is a wiring bug, not a runtime condition.
156
+ */
157
+ export async function buildAgents(opts) {
158
+ const { config, context, memoryBuilder, log } = opts;
159
+ const definitions = resolveDefinitions(config);
160
+ const ids = Object.keys(definitions);
161
+ const defaultAgentId = config.defaultAgent ?? ids[0] ?? FALLBACK_AGENT_ID;
162
+ const plugins = buildPluginsMap(context);
163
+ const ambientTools = config.tools ?? {};
164
+ const style = resolveStyleInstructions(config);
165
+ const agents = {};
166
+ for (const [id, def] of Object.entries(definitions)) {
167
+ const tools = await resolveTools(def.tools, plugins, ambientTools);
168
+ const memory = memoryBuilder?.forAgent(id, def);
169
+ agents[id] = new Agent({
170
+ id,
171
+ name: def.name ?? id,
172
+ ...(def.description !== undefined ? { description: def.description } : {}),
173
+ instructions: composeInstructions(def.instructions, style),
174
+ model: resolveModel(config, def.model),
175
+ tools,
176
+ ...(memory ? { memory } : {}),
177
+ });
178
+ }
179
+ if (!agents[defaultAgentId]) {
180
+ throw new Error(`mastra: defaultAgent "${defaultAgentId}" not found in registered agents (${ids.join(", ") || "none"})`);
181
+ }
182
+ log.info("agents registered", { ids, defaultAgentId });
183
+ return { agents, defaultAgentId };
184
+ }
185
+ /**
186
+ * Normalize `config.agents` into a `Record<id, definition>`. Accepts
187
+ * any of the three shapes documented on
188
+ * {@link MastraPluginConfig.agents}:
189
+ *
190
+ * - Record - returned as-is when non-empty.
191
+ * - Single definition (detected via the required `instructions`
192
+ * field) - keyed by `slugify(def.name)` or `FALLBACK_AGENT_ID`.
193
+ * - Array - keyed by `slugify(def.name)` or `agent_${i}`; duplicate
194
+ * slugs fail loudly so users know to set explicit names.
195
+ *
196
+ * Omitted or empty inputs fall back to a single built-in analyst so
197
+ * the bare `mastra()` call still mounts a working chat route.
198
+ */
199
+ function resolveDefinitions(config) {
200
+ const input = config.agents;
201
+ if (!input)
202
+ return fallbackDefinitions();
203
+ if (Array.isArray(input)) {
204
+ if (input.length === 0)
205
+ return fallbackDefinitions();
206
+ const out = {};
207
+ input.forEach((def, i) => {
208
+ const key = deriveAgentKey(def, i);
209
+ if (out[key]) {
210
+ throw new Error(`mastra: duplicate agent id "${key}" derived from name "${def.name ?? ""}"; ` +
211
+ `set unique \`name\`s on each definition`);
212
+ }
213
+ out[key] = def;
214
+ });
215
+ return out;
216
+ }
217
+ // Single-definition shorthand: an agent always has `instructions: string`,
218
+ // a record-of-agents never has that field directly.
219
+ if (typeof input.instructions === "string") {
220
+ const def = input;
221
+ const key = deriveAgentKey(def);
222
+ return { [key]: def };
223
+ }
224
+ const record = input;
225
+ if (Object.keys(record).length === 0)
226
+ return fallbackDefinitions();
227
+ return record;
228
+ }
229
+ /** Derive a registry id from a definition's `name`, with a fallback. */
230
+ function deriveAgentKey(def, index) {
231
+ if (def.name) {
232
+ const slug = stringUtils.toIdentifier(def.name);
233
+ if (slug)
234
+ return slug;
235
+ }
236
+ return index === undefined ? FALLBACK_AGENT_ID : `agent_${index}`;
237
+ }
238
+ /** Built-in fallback registry used when `agents` is omitted / empty. */
239
+ function fallbackDefinitions() {
240
+ return {
241
+ [FALLBACK_AGENT_ID]: {
242
+ name: "Default Agent",
243
+ instructions: FALLBACK_AGENT_INSTRUCTIONS,
244
+ },
245
+ };
246
+ }
247
+ /**
248
+ * Pick the effective model spec for an agent. Fallback ladder, in
249
+ * order:
250
+ *
251
+ * 1. Per-agent `def.model` (string sugar or `DynamicArgument`).
252
+ * 2. Plugin-level `config.defaultModel` (string sugar or
253
+ * `DynamicArgument`) - mirrors AppKit's `agents({ defaultModel })`.
254
+ * 3. The auto-resolver that mints user-scoped tokens against
255
+ * `/serving-endpoints` via {@link buildModel}.
256
+ *
257
+ * String values are treated as `modelId` sugar and threaded through
258
+ * `buildModel`'s override hook so the runtime fuzzy matcher and the
259
+ * per-request `X-Mastra-Model` override layer on top of the static
260
+ * choice. Non-string `DynamicArgument`s are passed through verbatim;
261
+ * callers that need full control over `providerId` / `headers` /
262
+ * `modelId` bypass the resolver pipeline entirely.
263
+ */
264
+ function resolveModel(config, override) {
265
+ const effective = override ?? config.defaultModel;
266
+ if (effective === undefined) {
267
+ return ({ requestContext }) => buildModel(config, requestContext);
268
+ }
269
+ if (typeof effective === "string") {
270
+ const modelId = effective;
271
+ return ({ requestContext }) => buildModel(config, requestContext, { modelId });
272
+ }
273
+ return effective;
274
+ }
275
+ /**
276
+ * Resolve a definition's `tools` field to a flat `MastraTools` record,
277
+ * merging in plugin-level ambient tools (per-agent tools win on key
278
+ * collision). Callback errors propagate verbatim so the original stack
279
+ * survives - the caller already knows which agent was registering.
280
+ */
281
+ async function resolveTools(defTools, plugins, ambientTools) {
282
+ if (!defTools)
283
+ return { ...ambientTools };
284
+ const resolved = typeof defTools === "function" ? await defTools(plugins) : defTools;
285
+ return { ...ambientTools, ...resolved };
286
+ }
287
+ /**
288
+ * Build the {@link MastraPlugins} runtime proxy handed to
289
+ * `tools(plugins)` callbacks.
290
+ *
291
+ * Implemented as a `Proxy` over the AppKit plugin context so
292
+ * `plugins.<name>` resolves at first access. Any sibling plugin that
293
+ * implements AppKit's standard `ToolProvider` interface
294
+ * (`toolkit(opts?)` + `executeAgentTool(name, args, signal?)`) is
295
+ * auto-adapted into Mastra tools. Unknown names return `undefined`,
296
+ * matching AppKit's `Plugins` semantics so `plugins.foo?.toolkit()`
297
+ * remains safe in environments where `foo` isn't registered.
298
+ *
299
+ * `genie` is special-cased to swap the generic AppKit toolkit (which
300
+ * runs `executeAgentTool` and only emits a single final `tool-result`
301
+ * chunk per call) for the streaming-aware tools built by
302
+ * {@link buildGenieProvider}. The streaming variant forwards each
303
+ * Genie wire event (status, SQL, row counts, errors) out through the
304
+ * Mastra `ctx.writer`, so the UI gets `tool-output` chunks in real
305
+ * time instead of staring at a spinner for the full Genie round-trip.
306
+ */
307
+ function buildPluginsMap(context) {
308
+ const cache = new Map();
309
+ return new Proxy({}, {
310
+ get(_target, propName) {
311
+ if (typeof propName !== "string")
312
+ return undefined;
313
+ if (cache.has(propName))
314
+ return cache.get(propName) ?? undefined;
315
+ const provider = resolveProvider(context, propName);
316
+ cache.set(propName, provider);
317
+ return provider ?? undefined;
318
+ },
319
+ });
320
+ }
321
+ /**
322
+ * Pick the right {@link MastraPluginToolkitProvider} for a sibling
323
+ * plugin lookup. Returns the streaming-aware Genie adapter when the
324
+ * caller asks for `genie`; falls back to the generic AppKit
325
+ * `ToolProvider` adapter for every other plugin name.
326
+ */
327
+ function resolveProvider(context, propName) {
328
+ if (propName === "genie") {
329
+ const geniePlugin = pluginUtils.instance(context, genie);
330
+ if (!geniePlugin)
331
+ return null;
332
+ return buildGenieProvider(geniePlugin);
333
+ }
334
+ const plugin = context?.getPlugins().get(propName);
335
+ return adaptPluginToolkit(plugin);
336
+ }
337
+ /**
338
+ * Adapt an AppKit `ToolProvider` plugin instance into a
339
+ * {@link MastraPluginToolkitProvider}. Returns `null` for any plugin
340
+ * that doesn't implement both `toolkit` and `executeAgentTool` (e.g.
341
+ * `server`, `lakebase` when used only as a Postgres pool, etc.).
342
+ */
343
+ function adaptPluginToolkit(plugin) {
344
+ if (!plugin || typeof plugin !== "object")
345
+ return null;
346
+ const p = plugin;
347
+ if (typeof p.toolkit !== "function" || typeof p.executeAgentTool !== "function") {
348
+ return null;
349
+ }
350
+ return {
351
+ toolkit(opts) {
352
+ const entries = p.toolkit(opts);
353
+ const tools = {};
354
+ for (const [key, entry] of Object.entries(entries)) {
355
+ tools[key] = toolkitEntryToMastraTool(entry, p);
356
+ }
357
+ return tools;
358
+ },
359
+ };
360
+ }
361
+ /**
362
+ * Wrap a single {@link AppKitToolkitEntry} as a Mastra tool whose
363
+ * `execute` dispatches back through `plugin.executeAgentTool(...)` so
364
+ * AppKit's OBO auth (`asUser`) and telemetry spans stay intact. JSON
365
+ * Schema parameters pass through unchanged - Mastra's `PublicSchema`
366
+ * accepts `JSONSchema7` directly via `@mastra/schema-compat`.
367
+ */
368
+ function toolkitEntryToMastraTool(entry, plugin) {
369
+ return createTool({
370
+ id: `${entry.pluginName}__${entry.localName}`,
371
+ description: entry.def.description,
372
+ ...(entry.def.parameters ? { inputSchema: entry.def.parameters } : {}),
373
+ execute: async (input, context) => {
374
+ const signal = context
375
+ ?.abortSignal;
376
+ return plugin.executeAgentTool(entry.localName, input, signal);
377
+ },
378
+ });
379
+ }