@dbx-tools/appkit-mastra 0.1.12 → 0.1.14

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.
Files changed (57) hide show
  1. package/README.md +303 -637
  2. package/index.ts +46 -38
  3. package/package.json +58 -43
  4. package/src/agents.ts +224 -66
  5. package/src/chart.ts +531 -429
  6. package/src/config.ts +270 -19
  7. package/src/filesystems.ts +1090 -0
  8. package/src/genie.ts +1000 -660
  9. package/src/history.ts +166 -79
  10. package/src/mcp.ts +105 -0
  11. package/src/memory.ts +119 -81
  12. package/src/mlflow.ts +149 -0
  13. package/src/model.ts +75 -408
  14. package/src/observability.ts +144 -0
  15. package/src/pagination.ts +34 -0
  16. package/src/plugin.ts +566 -59
  17. package/src/processors.ts +168 -0
  18. package/src/rest.ts +67 -0
  19. package/src/server.ts +232 -45
  20. package/src/serving-sanitize.ts +167 -0
  21. package/src/serving.ts +27 -243
  22. package/src/statement.ts +89 -0
  23. package/src/storage-schema.ts +41 -0
  24. package/src/summarize.ts +176 -0
  25. package/src/threads.ts +338 -0
  26. package/src/workspaces.ts +346 -0
  27. package/src/writer.ts +44 -0
  28. package/tsconfig.json +41 -0
  29. package/dist/index.d.ts +0 -20
  30. package/dist/index.js +0 -20
  31. package/dist/src/agents.d.ts +0 -306
  32. package/dist/src/agents.js +0 -403
  33. package/dist/src/chart.d.ts +0 -170
  34. package/dist/src/chart.js +0 -491
  35. package/dist/src/config.d.ts +0 -183
  36. package/dist/src/config.js +0 -12
  37. package/dist/src/genie.d.ts +0 -131
  38. package/dist/src/genie.js +0 -630
  39. package/dist/src/history.d.ts +0 -67
  40. package/dist/src/history.js +0 -172
  41. package/dist/src/memory.d.ts +0 -79
  42. package/dist/src/memory.js +0 -210
  43. package/dist/src/model.d.ts +0 -159
  44. package/dist/src/model.js +0 -427
  45. package/dist/src/plugin.d.ts +0 -130
  46. package/dist/src/plugin.js +0 -261
  47. package/dist/src/processors/strip-stale-charts.d.ts +0 -29
  48. package/dist/src/processors/strip-stale-charts.js +0 -96
  49. package/dist/src/server.d.ts +0 -46
  50. package/dist/src/server.js +0 -123
  51. package/dist/src/serving.d.ts +0 -156
  52. package/dist/src/serving.js +0 -231
  53. package/dist/src/tools/email.d.ts +0 -74
  54. package/dist/src/tools/email.js +0 -122
  55. package/dist/tsconfig.build.tsbuildinfo +0 -1
  56. package/src/processors/strip-stale-charts.ts +0 -105
  57. package/src/tools/email.ts +0 -147
@@ -1,172 +0,0 @@
1
- /**
2
- * Thread history loader exposed as a Mastra custom API route.
3
- *
4
- * Backed entirely by native Mastra: looks up the active agent by id,
5
- * asks its `Memory` instance to `recall` a page of `MastraDBMessage`s,
6
- * and converts the result to AI SDK V5 `UIMessage`s with the official
7
- * {@link toAISdkV5Messages} helper from `@mastra/ai-sdk/ui`. No direct
8
- * database reads.
9
- *
10
- * The route is registered through {@link historyRoute} as a Mastra
11
- * `registerApiRoute` so it sits in the same dispatcher pipeline as
12
- * `chatRoute`. That means the `MastraServer` auth middleware (in
13
- * `./server.ts`) has already populated `RequestContext` with
14
- * `MASTRA_THREAD_ID_KEY` and `MASTRA_RESOURCE_ID_KEY` by the time
15
- * the handler runs - no cookie or user lookups happen here, and the
16
- * session-cookie logic stays the single source of truth in `server.ts`.
17
- */
18
- import { logUtils } from "@dbx-tools/appkit-shared";
19
- import { toAISdkV5Messages } from "@mastra/ai-sdk/ui";
20
- import { MASTRA_RESOURCE_ID_KEY, MASTRA_THREAD_ID_KEY, } from "@mastra/core/request-context";
21
- import { registerApiRoute } from "@mastra/core/server";
22
- const log = logUtils.logger("mastra/history");
23
- /** Default history page size; matches the Mastra storage default. */
24
- const DEFAULT_PER_PAGE = 20;
25
- /** Hard cap so a misbehaving client can't fetch the whole thread at once. */
26
- const MAX_PER_PAGE = 200;
27
- /**
28
- * Fetch a page of UI-formatted messages for a thread.
29
- *
30
- * Uses the agent's resolved `Memory` (`getMemory()`) so per-agent
31
- * storage namespaces (`mastra_<agentId>` schemas) and any future
32
- * memory-side filters apply automatically. When the agent has no
33
- * memory configured the response is a successful empty page so
34
- * callers don't have to special-case stateless agents.
35
- *
36
- * Pagination is descending-by-default: page 0 is the most recent
37
- * page, page 1 the page before that, etc. The returned `uiMessages`
38
- * are always re-sorted into chronological order (oldest -> newest)
39
- * so the client can prepend them above the existing transcript
40
- * without sorting locally.
41
- */
42
- export async function loadHistory(opts) {
43
- const perPage = clampPerPage(opts.perPage);
44
- const page = Math.max(0, Math.trunc(opts.page ?? 0));
45
- const memory = await opts.agent.getMemory();
46
- if (!memory) {
47
- log.debug("recall:no-memory", { agentId: opts.agent.id, threadId: opts.threadId });
48
- return { uiMessages: [], page, perPage, total: 0, hasMore: false };
49
- }
50
- const startedAt = Date.now();
51
- const result = await memory.recall({
52
- threadId: opts.threadId,
53
- ...(opts.resourceId ? { resourceId: opts.resourceId } : {}),
54
- page,
55
- perPage,
56
- orderBy: {
57
- field: "createdAt",
58
- direction: opts.ascending ? "ASC" : "DESC",
59
- },
60
- });
61
- const chronological = sortChronological(result.messages);
62
- const uiMessages = toAISdkV5Messages(chronological);
63
- log.debug("recall:done", {
64
- agentId: opts.agent.id,
65
- threadId: opts.threadId,
66
- page,
67
- perPage,
68
- returned: uiMessages.length,
69
- total: result.total,
70
- hasMore: result.hasMore,
71
- elapsedMs: Date.now() - startedAt,
72
- });
73
- return {
74
- uiMessages,
75
- page,
76
- perPage,
77
- total: result.total,
78
- hasMore: result.hasMore,
79
- };
80
- }
81
- /**
82
- * Register a `GET <path>` Mastra custom API route that returns a page
83
- * of AI SDK V5 `UIMessage`s for the caller's current thread.
84
- *
85
- * Modeled after `chatRoute` from `@mastra/ai-sdk`: pass `agent` for a
86
- * fixed-agent mount, or include `:agentId` in the path for dynamic
87
- * routing. Pairs cleanly with the AppKit Mastra plugin's chat route
88
- * layout (`/route/chat` + `/route/chat/:agentId`).
89
- *
90
- * The handler reads `threadId` and `resourceId` from `RequestContext`
91
- * (populated upstream by `MastraServer.registerAuthMiddleware`), so
92
- * no cookie or user lookups happen here.
93
- */
94
- export function historyRoute(options) {
95
- const { path } = options;
96
- const fixedAgent = "agent" in options ? options.agent : undefined;
97
- if (!fixedAgent && !path.includes(":agentId")) {
98
- throw new Error("historyRoute path must include `:agentId` or `agent` must be passed explicitly");
99
- }
100
- return registerApiRoute(path, {
101
- method: "GET",
102
- handler: async (c) => {
103
- const mastra = c.get("mastra");
104
- const requestContext = c.get("requestContext");
105
- const agentId = fixedAgent ?? c.req.param("agentId");
106
- if (!agentId) {
107
- return c.json({ error: "agentId is required" }, 400);
108
- }
109
- const agent = mastra.getAgentById(agentId);
110
- if (!agent) {
111
- return c.json({ error: `Unknown agent "${agentId}"` }, 404);
112
- }
113
- const threadId = requestContext.get(MASTRA_THREAD_ID_KEY);
114
- if (!threadId) {
115
- return c.json({ error: "thread id missing from request context" }, 400);
116
- }
117
- const resourceId = requestContext.get(MASTRA_RESOURCE_ID_KEY);
118
- const payload = await loadHistory({
119
- agent,
120
- threadId,
121
- ...(resourceId ? { resourceId } : {}),
122
- page: parseIntParam(c.req.query("page")),
123
- perPage: parseIntParam(c.req.query("perPage")),
124
- });
125
- return c.json(payload);
126
- },
127
- });
128
- }
129
- /** Coerce / clamp `perPage`; falls back to the page-size default. */
130
- function clampPerPage(value) {
131
- if (value === undefined || Number.isNaN(value))
132
- return DEFAULT_PER_PAGE;
133
- const n = Math.trunc(value);
134
- if (n <= 0)
135
- return DEFAULT_PER_PAGE;
136
- return Math.min(n, MAX_PER_PAGE);
137
- }
138
- /**
139
- * Sort messages oldest-first by `createdAt`, falling back to whatever
140
- * order the storage returned them in. The native `recall` call honors
141
- * `orderBy` but doesn't guarantee a stable secondary sort, so we
142
- * normalize here before handing the page to the AI SDK converter.
143
- */
144
- function sortChronological(messages) {
145
- return [...messages].sort((a, b) => {
146
- const ta = toEpoch(a.createdAt);
147
- const tb = toEpoch(b.createdAt);
148
- return ta - tb;
149
- });
150
- }
151
- function toEpoch(value) {
152
- if (value instanceof Date)
153
- return value.getTime();
154
- if (typeof value === "string" || typeof value === "number") {
155
- const parsed = new Date(value).getTime();
156
- return Number.isNaN(parsed) ? 0 : parsed;
157
- }
158
- return 0;
159
- }
160
- /**
161
- * Coerce a Hono query value into a non-negative integer. Returns
162
- * `undefined` for empty / non-numeric / negative inputs so
163
- * {@link loadHistory} can apply its built-in defaults.
164
- */
165
- function parseIntParam(value) {
166
- if (!value)
167
- return undefined;
168
- const n = Number(value);
169
- if (!Number.isFinite(n) || n < 0)
170
- return undefined;
171
- return Math.trunc(n);
172
- }
@@ -1,79 +0,0 @@
1
- /**
2
- * Lakebase-backed Mastra memory wiring.
3
- *
4
- * Provides a {@link MemoryBuilder} that mints one `Memory` per agent
5
- * with two independent knobs:
6
- *
7
- * - **Storage** (threads / messages via `PostgresStore`): defaults to
8
- * **per-agent** namespacing via `schemaName: "mastra_<agentId>"` so
9
- * conversation history stays isolated between agents in the same
10
- * database. `PostgresStore` auto-creates the schema with
11
- * `CREATE SCHEMA IF NOT EXISTS` on init.
12
- * - **Memory** (semantic recall via `PgVector`): defaults to a single
13
- * **shared** instance across every agent. Cross-agent recall on one
14
- * index is almost always what users want; opt into per-agent recall
15
- * by passing a {@link MastraMemoryConfigOverride} on the agent.
16
- *
17
- * Plugin-level `config.storage` / `config.memory` act as the baseline
18
- * (auto-defaulted to `true` in `plugin.ts` when the `lakebase` plugin
19
- * is registered); per-agent settings cascade on top of that.
20
- */
21
- import { lakebase } from "@databricks/appkit";
22
- import { pluginUtils } from "@dbx-tools/appkit-shared";
23
- import { Memory } from "@mastra/memory";
24
- import type { MastraAgentDefinition } from "./agents.js";
25
- import type { MastraPluginConfig } from "./config.js";
26
- /** Pool handle returned by the AppKit `lakebase` plugin `exports().pool`. */
27
- export type LakebasePool = ReturnType<InstanceType<ReturnType<typeof lakebase>["plugin"]>["exports"]>["pool"];
28
- /**
29
- * True when any plugin-level or per-agent setting could need the
30
- * Lakebase pool. Used by `plugin.ts` to gate pool acquisition; the
31
- * builder also acquires lazily so missed cases still fail with a
32
- * clear lakebase-not-registered error.
33
- */
34
- export declare function needsLakebase(config: MastraPluginConfig): boolean;
35
- /**
36
- * Look up the `lakebase` plugin and return its managed `pg.Pool`.
37
- * Throws when the sibling plugin is not registered; enabling
38
- * `storage` / `memory` without lakebase is a wiring bug, not a runtime
39
- * condition we can recover from.
40
- */
41
- export declare function resolveLakebasePool(context: pluginUtils.PluginContextLike | undefined, caller: MastraPluginConfig): LakebasePool;
42
- /**
43
- * Construct a per-agent {@link Memory} factory. Caches the shared
44
- * `PgVector` singleton (built on first need) and the lazily-resolved
45
- * Lakebase pool so each agent build is O(1) after the first.
46
- */
47
- export declare function createMemoryBuilder(config: MastraPluginConfig, context: pluginUtils.PluginContextLike | undefined): MemoryBuilder;
48
- /**
49
- * Builds one `Memory` per agent. Per-instance state keeps the shared
50
- * `PgVector` and the resolved Lakebase pool alive across calls so
51
- * registering N agents stays cheap.
52
- */
53
- export declare class MemoryBuilder {
54
- private readonly config;
55
- private readonly context;
56
- private sharedVector;
57
- private pool;
58
- constructor(config: MastraPluginConfig, context: pluginUtils.PluginContextLike | undefined);
59
- /**
60
- * Build a `Memory` for `agentId` after the plugin/agent cascade.
61
- * Returns `undefined` when the agent has neither storage nor a
62
- * vector store enabled - Mastra accepts a missing `memory` field
63
- * and treats the agent as stateless.
64
- */
65
- forAgent(agentId: string, def: MastraAgentDefinition): Memory | undefined;
66
- private buildStorage;
67
- /**
68
- * Resolve the agent's vector store. Cascade:
69
- *
70
- * - falsy: no vector.
71
- * - `boolean` / `undefined-inheriting-true`: return the shared
72
- * singleton (built lazily on first call). All agents that
73
- * default-enable memory write into and recall from one index.
74
- * - object: build a dedicated `PgVector` for this agent.
75
- */
76
- private buildVector;
77
- private getSharedVector;
78
- private requirePool;
79
- }
@@ -1,210 +0,0 @@
1
- /**
2
- * Lakebase-backed Mastra memory wiring.
3
- *
4
- * Provides a {@link MemoryBuilder} that mints one `Memory` per agent
5
- * with two independent knobs:
6
- *
7
- * - **Storage** (threads / messages via `PostgresStore`): defaults to
8
- * **per-agent** namespacing via `schemaName: "mastra_<agentId>"` so
9
- * conversation history stays isolated between agents in the same
10
- * database. `PostgresStore` auto-creates the schema with
11
- * `CREATE SCHEMA IF NOT EXISTS` on init.
12
- * - **Memory** (semantic recall via `PgVector`): defaults to a single
13
- * **shared** instance across every agent. Cross-agent recall on one
14
- * index is almost always what users want; opt into per-agent recall
15
- * by passing a {@link MastraMemoryConfigOverride} on the agent.
16
- *
17
- * Plugin-level `config.storage` / `config.memory` act as the baseline
18
- * (auto-defaulted to `true` in `plugin.ts` when the `lakebase` plugin
19
- * is registered); per-agent settings cascade on top of that.
20
- */
21
- import { lakebase } from "@databricks/appkit";
22
- import { logUtils, pluginUtils } from "@dbx-tools/appkit-shared";
23
- import { fastembed } from "@mastra/fastembed";
24
- import { Memory } from "@mastra/memory";
25
- import { PgVector, PostgresStore } from "@mastra/pg";
26
- import { randomUUID } from "node:crypto";
27
- const log = logUtils.logger("mastra/memory");
28
- /**
29
- * True when any plugin-level or per-agent setting could need the
30
- * Lakebase pool. Used by `plugin.ts` to gate pool acquisition; the
31
- * builder also acquires lazily so missed cases still fail with a
32
- * clear lakebase-not-registered error.
33
- */
34
- export function needsLakebase(config) {
35
- if (settingNeedsSharedPool(config.storage))
36
- return true;
37
- if (settingNeedsSharedPool(config.memory))
38
- return true;
39
- const defs = collectAgentDefinitions(config);
40
- return defs.some((d) => settingNeedsSharedPool(d.storage) || settingNeedsSharedPool(d.memory));
41
- }
42
- /**
43
- * Look up the `lakebase` plugin and return its managed `pg.Pool`.
44
- * Throws when the sibling plugin is not registered; enabling
45
- * `storage` / `memory` without lakebase is a wiring bug, not a runtime
46
- * condition we can recover from.
47
- */
48
- export function resolveLakebasePool(context, caller) {
49
- return pluginUtils.require(context, lakebase, caller).exports().pool;
50
- }
51
- /**
52
- * Construct a per-agent {@link Memory} factory. Caches the shared
53
- * `PgVector` singleton (built on first need) and the lazily-resolved
54
- * Lakebase pool so each agent build is O(1) after the first.
55
- */
56
- export function createMemoryBuilder(config, context) {
57
- return new MemoryBuilder(config, context);
58
- }
59
- /**
60
- * Builds one `Memory` per agent. Per-instance state keeps the shared
61
- * `PgVector` and the resolved Lakebase pool alive across calls so
62
- * registering N agents stays cheap.
63
- */
64
- export class MemoryBuilder {
65
- config;
66
- context;
67
- sharedVector;
68
- pool;
69
- constructor(config, context) {
70
- this.config = config;
71
- this.context = context;
72
- }
73
- /**
74
- * Build a `Memory` for `agentId` after the plugin/agent cascade.
75
- * Returns `undefined` when the agent has neither storage nor a
76
- * vector store enabled - Mastra accepts a missing `memory` field
77
- * and treats the agent as stateless.
78
- */
79
- forAgent(agentId, def) {
80
- const storageSetting = def.storage ?? this.config.storage;
81
- const memorySetting = def.memory ?? this.config.memory;
82
- const storage = this.buildStorage(agentId, storageSetting);
83
- const vector = this.buildVector(memorySetting);
84
- if (!storage && !vector) {
85
- log.debug("agent:stateless", { agentId });
86
- return undefined;
87
- }
88
- log.debug("agent:configured", {
89
- agentId,
90
- storage: storage !== undefined,
91
- vector: vector !== undefined,
92
- vectorMode: vector === undefined
93
- ? "off"
94
- : typeof memorySetting === "object"
95
- ? "dedicated"
96
- : "shared",
97
- });
98
- return new Memory({
99
- ...(storage ? { storage } : {}),
100
- ...(vector ? { vector, embedder: fastembed } : {}),
101
- options: {
102
- lastMessages: 10,
103
- ...(vector
104
- ? { semanticRecall: { topK: 3, messageRange: 2 } }
105
- : {}),
106
- },
107
- });
108
- }
109
- buildStorage(agentId, setting) {
110
- if (!setting)
111
- return undefined;
112
- if (typeof setting === "boolean") {
113
- return new PostgresStore({
114
- id: `mastra-store__${agentId}`,
115
- schemaName: `mastra_${agentId}`,
116
- pool: this.requirePool(),
117
- });
118
- }
119
- // Cast: `withId` guarantees `id` is set, but the distributive
120
- // Omit + `id?: string` shape doesn't structurally narrow to the
121
- // discriminated union members. Runtime shape is identical.
122
- return new PostgresStore(withId(setting, `mastra-store__${agentId}`));
123
- }
124
- /**
125
- * Resolve the agent's vector store. Cascade:
126
- *
127
- * - falsy: no vector.
128
- * - `boolean` / `undefined-inheriting-true`: return the shared
129
- * singleton (built lazily on first call). All agents that
130
- * default-enable memory write into and recall from one index.
131
- * - object: build a dedicated `PgVector` for this agent.
132
- */
133
- buildVector(setting) {
134
- if (!setting)
135
- return undefined;
136
- if (typeof setting === "boolean")
137
- return this.getSharedVector();
138
- return buildPgVector(setting);
139
- }
140
- getSharedVector() {
141
- if (!this.sharedVector) {
142
- this.sharedVector = buildSharedPgVector(this.requirePool());
143
- }
144
- return this.sharedVector;
145
- }
146
- requirePool() {
147
- if (!this.pool) {
148
- this.pool = resolveLakebasePool(this.context, this.config);
149
- }
150
- return this.pool;
151
- }
152
- }
153
- /**
154
- * Build the shared `PgVector` that backs the default
155
- * `def.memory === true` case across every agent.
156
- *
157
- * `PgVector`'s constructor accepts only connection-style configs
158
- * (`HostConfig` / `ConnectionStringConfig` / `ClientConfig`); there is
159
- * no `{ pool }` shorthand the way `PostgresStore` has one. Worse, the
160
- * constructor synchronously kicks off a `cacheWarmupPromise` IIFE that
161
- * calls `this.pool.connect()` before returning, so we can't cleanly
162
- * hand it an inert config and patch the pool afterwards.
163
- *
164
- * The trick: pass illegal-but-validation-passing placeholders so the
165
- * warmup's `net.connect()` rejects synchronously with `RangeError`
166
- * (Node validates `0 <= port < 65536`). The IIFE's `catch {}` swallows
167
- * it, no DNS lookup or TCP attempt happens, and we then swap
168
- * `pgVector.pool` to the lakebase pool. Every subsequent `PgVector`
169
- * method reads `this.pool` at call time, so all real I/O goes through
170
- * the lakebase pool from then on. The placeholder pool is `.end()`'d
171
- * so its socket book-keeping is released.
172
- */
173
- function buildSharedPgVector(pool) {
174
- const vector = new PgVector({
175
- id: `pg${randomUUID()}`,
176
- host: "-1",
177
- port: -1,
178
- database: "_",
179
- user: "_",
180
- password: "_",
181
- });
182
- const placeholder = vector.pool;
183
- vector.pool = pool;
184
- void placeholder.end().catch(() => undefined);
185
- return vector;
186
- }
187
- /** Per-agent dedicated `PgVector` (rare; opt-in via object override). */
188
- function buildPgVector(setting) {
189
- return new PgVector(withId(setting, `pg-vector__${randomUUID()}`));
190
- }
191
- /** True when this setting requires the shared Lakebase pool. */
192
- function settingNeedsSharedPool(setting) {
193
- return setting === true;
194
- }
195
- /** Walk the three shapes of `config.agents` into a flat list. */
196
- function collectAgentDefinitions(config) {
197
- const agents = config.agents;
198
- if (!agents)
199
- return [];
200
- if (Array.isArray(agents))
201
- return agents;
202
- if (typeof agents.instructions === "string") {
203
- return [agents];
204
- }
205
- return Object.values(agents);
206
- }
207
- /** Fill in a default `id` when the caller didn't supply one. */
208
- function withId(value, fallback) {
209
- return value.id ? value : { ...value, id: fallback };
210
- }
@@ -1,159 +0,0 @@
1
- /**
2
- * Databricks Model Serving resolver for Mastra agents.
3
- *
4
- * Each agent step calls {@link buildModel} with the active
5
- * `RequestContext`. The user stamped by `MastraServer` carries an
6
- * AppKit `WorkspaceClient`; we ask it for the workspace host and a
7
- * fresh bearer header, then point Mastra's OpenAI-compatible provider
8
- * at `/serving-endpoints` on that host.
9
- *
10
- * Model id resolution walks three sources before falling back to the
11
- * hard-coded default, **in this priority order**:
12
- *
13
- * 1. Per-request override stashed by the auth middleware under
14
- * {@link MASTRA_MODEL_OVERRIDE_KEY} (header / query / body).
15
- * 2. The static `modelId` passed in by the agent / plugin (string
16
- * sugar on `def.model` or `config.defaultModel`).
17
- * 3. `DATABRICKS_SERVING_ENDPOINT_NAME` env var.
18
- * 4. {@link FALLBACK_MODEL_ID}.
19
- *
20
- * Whatever wins is then fuzzy-matched against the live
21
- * `/serving-endpoints` list ({@link listServingEndpoints}) so loose
22
- * names like `"claude sonnet"` resolve to the real endpoint name.
23
- * Fuzzy matching is best-effort: when the workspace client throws
24
- * (network blip, expired token at cache-fill time) we fall back to
25
- * the input verbatim and let Databricks return the canonical error.
26
- */
27
- import type { MastraModelConfig } from "@mastra/core/llm";
28
- import type { RequestContext } from "@mastra/core/request-context";
29
- import { type MastraPluginConfig } from "./config.js";
30
- /**
31
- * Capability tiers for Databricks Foundation Model API endpoints.
32
- *
33
- * - {@link ModelTier.Thinking}: deepest reasoning / "thinking" models
34
- * (Claude Opus, GPT-5.5 Pro, Gemini Pro, Llama 4 Maverick, etc).
35
- * Highest cost and latency; reserve for hard multi-step reasoning.
36
- * - {@link ModelTier.Balanced}: cost/latency sweet spot for general
37
- * agent work (Claude Sonnet, GPT-5.x, Gemini Flash, Llama 3.3 70B).
38
- * The right default for most agents.
39
- * - {@link ModelTier.Fast}: cheap and quick; classification, routing,
40
- * tool-arg extraction, simple summarisation (Claude Haiku, GPT-5
41
- * mini/nano, Gemini Flash Lite, GPT-OSS 20B, Llama 3.1 8B).
42
- *
43
- * String enum so the value is the slug we use in cache keys, logs,
44
- * and as the value users see in serialized configs.
45
- */
46
- export declare enum ModelTier {
47
- Thinking = "thinking",
48
- Balanced = "balanced",
49
- Fast = "fast"
50
- }
51
- /**
52
- * Catalogue of Databricks-hosted Foundation Model API endpoints,
53
- * grouped by capability {@link ModelTier} and then by provider. Each
54
- * inner array is priority-ordered (most powerful first within the
55
- * same provider+tier).
56
- *
57
- * Provider buckets:
58
- *
59
- * - `claude`: Anthropic Claude family (closed; flagship reasoning).
60
- * - `gpt`: OpenAI GPT-5 family (closed; "ChatGPT" on Databricks FMAPI).
61
- * - `gemini`: Google Gemini family (closed; multimodal + web-search).
62
- * - `openSource`: open-weights models (widest regional / SKU availability).
63
- *
64
- * The list is curated by hand; refresh from the Databricks "supported
65
- * foundation models" doc when new endpoints land.
66
- */
67
- export declare const MODEL_CATALOG: {
68
- readonly thinking: {
69
- readonly claude: readonly ["databricks-claude-opus-4-8", "databricks-claude-opus-4-7", "databricks-claude-opus-4-6", "databricks-claude-opus-4-5", "databricks-claude-opus-4-1"];
70
- readonly gpt: readonly ["databricks-gpt-5-5-pro"];
71
- readonly gemini: readonly ["databricks-gemini-3-1-pro", "databricks-gemini-3-pro", "databricks-gemini-2-5-pro"];
72
- readonly openSource: readonly ["databricks-llama-4-maverick", "databricks-gpt-oss-120b", "databricks-meta-llama-3-1-405b-instruct"];
73
- };
74
- readonly balanced: {
75
- readonly claude: readonly ["databricks-claude-sonnet-4-6", "databricks-claude-sonnet-4-5", "databricks-claude-sonnet-4"];
76
- readonly gpt: readonly ["databricks-gpt-5-5", "databricks-gpt-5-4", "databricks-gpt-5-2", "databricks-gpt-5-1", "databricks-gpt-5"];
77
- readonly gemini: readonly ["databricks-gemini-3-5-flash", "databricks-gemini-3-flash", "databricks-gemini-2-5-flash"];
78
- readonly openSource: readonly ["databricks-meta-llama-3-3-70b-instruct", "databricks-qwen3-next-80b-a3b-instruct", "databricks-qwen35-122b-a10b"];
79
- };
80
- readonly fast: {
81
- readonly claude: readonly ["databricks-claude-haiku-4-5"];
82
- readonly gpt: readonly ["databricks-gpt-5-4-mini", "databricks-gpt-5-4-nano", "databricks-gpt-5-mini", "databricks-gpt-5-nano"];
83
- readonly gemini: readonly ["databricks-gemini-3-1-flash-lite"];
84
- readonly openSource: readonly ["databricks-gpt-oss-20b", "databricks-gemma-3-12b", "databricks-meta-llama-3-1-8b-instruct"];
85
- };
86
- };
87
- /**
88
- * Priority-ordered model ids for a single capability {@link ModelTier},
89
- * interleaved across providers so a workspace missing the top Claude
90
- * still lands on a flagship GPT / Gemini on the next probe.
91
- *
92
- * Provider order within the interleave: Claude, GPT, Gemini, then the
93
- * open-weights tail appended verbatim as the universal floor (widest
94
- * regional availability).
95
- *
96
- * @example
97
- * ```ts
98
- * mastra({
99
- * defaultModelFallbacks: modelsForTier(ModelTier.Fast),
100
- * });
101
- * ```
102
- */
103
- export declare function modelsForTier(tier: ModelTier): readonly string[];
104
- /**
105
- * Top model id at the given {@link ModelTier}. Sync; the agent-step
106
- * resolver fuzzy-matches it against the workspace catalogue at call
107
- * time, so this works even when the literal top pick isn't deployed.
108
- *
109
- * Use when wiring a tier-appropriate model into an agent definition:
110
- *
111
- * @example
112
- * ```ts
113
- * const classifier = createAgent({
114
- * instructions: "Classify this email",
115
- * model: modelForTier(ModelTier.Fast), // cheap, quick
116
- * });
117
- *
118
- * const planner = createAgent({
119
- * instructions: "Plan a multi-step migration",
120
- * model: modelForTier(ModelTier.Thinking), // deep reasoning
121
- * });
122
- * ```
123
- */
124
- export declare function modelForTier(tier: ModelTier): string;
125
- /**
126
- * Last-resort model ids used when neither `config.defaultModel`,
127
- * per-agent `model`, nor `DATABRICKS_SERVING_ENDPOINT_NAME` is set.
128
- *
129
- * Walked in order at resolve time: the first id whose endpoint is
130
- * actually present in the workspace's `/serving-endpoints` listing
131
- * wins. Workspaces vary - not every region / SKU has every model,
132
- * and the list of Foundation Model APIs evolves quickly - so the
133
- * resolver degrades all the way from "best thinking model" down to
134
- * "smallest commodity Llama" before giving up.
135
- *
136
- * Built by chaining the per-tier interleaves (Thinking -> Balanced
137
- * -> Fast); within each tier the providers are round-robin-zipped
138
- * (Claude, GPT, Gemini, then open-weights tail). Override the entire
139
- * list via `MastraPluginConfig.defaultModelFallbacks` (e.g. to pin a
140
- * regulated workspace to a specific approved subset, or to bias the
141
- * priority toward a particular tier).
142
- */
143
- export declare const FALLBACK_MODEL_IDS: readonly string[];
144
- /** Optional overrides accepted by {@link buildModel}. */
145
- export interface BuildModelOverrides {
146
- /**
147
- * Static model id from the agent / plugin config (string sugar on
148
- * `def.model` or `config.defaultModel`). Loses to the per-request
149
- * override but wins over env / fallback.
150
- */
151
- modelId?: string;
152
- }
153
- /**
154
- * Resolve a `MastraModelConfig` for the current agent step. Runs
155
- * while `agent.stream` is inside the `asUser(req)` scope so tokens
156
- * are user-scoped; outside an active user context the workspace
157
- * client falls back to the service principal.
158
- */
159
- export declare function buildModel(config: MastraPluginConfig, requestContext: RequestContext, overrides?: BuildModelOverrides): Promise<MastraModelConfig>;