@dbx-tools/appkit-mastra 0.6.7 → 0.6.9
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 +46 -0
- package/index.ts +3 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -1
- package/lib/src/config.d.ts +22 -0
- package/lib/src/config.js +5 -1
- package/lib/src/databricks-aitools.d.ts +83 -0
- package/lib/src/databricks-aitools.js +150 -0
- package/lib/src/plugin.js +14 -2
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +10 -10
- package/src/config.ts +28 -0
- package/src/databricks-aitools.ts +216 -0
- package/src/plugin.ts +14 -1
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Startup provisioning of Databricks AI Tools skills for a Mastra workspace.
|
|
3
|
+
*
|
|
4
|
+
* Databricks AI Tools (`databricks aitools`) are Databricks-owned Agent-Skill
|
|
5
|
+
* (`SKILL.md`) trees installed and kept up to date through the Databricks CLI.
|
|
6
|
+
* The CLI installs them globally under `~/.databricks/aitools/skills/<name>/`
|
|
7
|
+
* (with a `.state.json` manifest) and can also write a resolved, agent-agnostic
|
|
8
|
+
* copy to any directory with `databricks aitools install --path <dir>
|
|
9
|
+
* --skills-only`.
|
|
10
|
+
*
|
|
11
|
+
* {@link provisionDatabricksAITools} folds those skills into a Mastra workspace
|
|
12
|
+
* as extra LOCAL skill scan paths so an agent can use them without anyone
|
|
13
|
+
* hand-copying `SKILL.md` files. It never reimplements the CLI's sourcing: it
|
|
14
|
+
* either points Mastra at the already-installed global tree, or shells out to
|
|
15
|
+
* the CLI to materialize a curated subset into a temp dir.
|
|
16
|
+
*
|
|
17
|
+
* The option is `false | true | "auto" | DatabricksAIToolsOptions`:
|
|
18
|
+
*
|
|
19
|
+
* - `false` (default) - off.
|
|
20
|
+
* - `"auto"` - enable only when the global tree already exists OR the
|
|
21
|
+
* `databricks` CLI is resolvable; otherwise silently no-op. The "on if the
|
|
22
|
+
* CLI is around" default.
|
|
23
|
+
* - `true` - required: fail startup (subject to `failOnError`) when neither the
|
|
24
|
+
* installed tree nor the CLI can supply skills.
|
|
25
|
+
* - an options bag for a specific `skills` subset, `experimental` skills, a
|
|
26
|
+
* `refresh` that re-runs the CLI even when a tree exists, or an explicit
|
|
27
|
+
* `path`.
|
|
28
|
+
*
|
|
29
|
+
* @module
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import { mkdtemp } from "node:fs/promises";
|
|
33
|
+
import { existsSync } from "node:fs";
|
|
34
|
+
import { homedir, tmpdir } from "node:os";
|
|
35
|
+
import { join } from "node:path";
|
|
36
|
+
import { spawn } from "@dbx-tools/core";
|
|
37
|
+
import { error, log, string } from "@dbx-tools/shared-core";
|
|
38
|
+
|
|
39
|
+
const logger = log.logger("mastra/databricks-aitools");
|
|
40
|
+
|
|
41
|
+
/** Global skills tree the `databricks aitools` CLI installs into. */
|
|
42
|
+
const GLOBAL_AITOOLS_SKILLS_PATH = join(homedir(), ".databricks", "aitools", "skills");
|
|
43
|
+
|
|
44
|
+
/** The Databricks CLI binary name; resolved on `PATH`. */
|
|
45
|
+
const DATABRICKS_CLI = "databricks";
|
|
46
|
+
|
|
47
|
+
/* -------------------------------- types -------------------------------- */
|
|
48
|
+
|
|
49
|
+
/** How aggressively to enable Databricks AI Tools skills. */
|
|
50
|
+
export type DatabricksAIToolsMode = "auto" | "require";
|
|
51
|
+
|
|
52
|
+
/** Options for {@link provisionDatabricksAITools}. */
|
|
53
|
+
export interface DatabricksAIToolsOptions {
|
|
54
|
+
/**
|
|
55
|
+
* `"auto"` (default) enables only when the installed tree exists or the CLI
|
|
56
|
+
* is resolvable; `"require"` fails startup when neither can supply skills.
|
|
57
|
+
*/
|
|
58
|
+
mode?: DatabricksAIToolsMode;
|
|
59
|
+
/** Install only these skill names (comma/space list or array). */
|
|
60
|
+
skills?: string | string[];
|
|
61
|
+
/** Include experimental skills when the CLI has to fetch. */
|
|
62
|
+
experimental?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Re-run the CLI to (re)materialize skills into a fresh dir even when the
|
|
65
|
+
* global tree already exists. Defaults to `false` (reuse the installed tree).
|
|
66
|
+
*/
|
|
67
|
+
refresh?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Explicit directory to materialize skills into with the CLI. Defaults to a
|
|
70
|
+
* temp dir. Ignored when the installed global tree is used as-is.
|
|
71
|
+
*/
|
|
72
|
+
path?: string;
|
|
73
|
+
/** Absolute path to the `databricks` CLI. Defaults to `databricks` on PATH. */
|
|
74
|
+
cli?: string;
|
|
75
|
+
/**
|
|
76
|
+
* Fail app startup when required skills can't be provisioned. Defaults to
|
|
77
|
+
* `true` for `mode: "require"` and `false` for `"auto"`.
|
|
78
|
+
*/
|
|
79
|
+
failOnError?: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The `databricksAITools` config option: a toggle, `"auto"`, or an options bag. */
|
|
83
|
+
export type DatabricksAIToolsOption = boolean | "auto" | DatabricksAIToolsOptions;
|
|
84
|
+
|
|
85
|
+
/** What {@link provisionDatabricksAITools} resolved. */
|
|
86
|
+
export interface ProvisionedDatabricksAITools {
|
|
87
|
+
/** Extra LOCAL skill scan paths to hand Mastra. Empty when disabled/unresolved. */
|
|
88
|
+
localSkillPaths: string[];
|
|
89
|
+
/** How the skills were sourced, for logging. */
|
|
90
|
+
source?: "installed" | "cli";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* ------------------------------- helpers ------------------------------- */
|
|
94
|
+
|
|
95
|
+
/** Normalize the `databricksAITools` option into a flat options bag, or `undefined` when off. */
|
|
96
|
+
export function normalizeDatabricksAIToolsOption(
|
|
97
|
+
option: DatabricksAIToolsOption | undefined,
|
|
98
|
+
): DatabricksAIToolsOptions | undefined {
|
|
99
|
+
if (option === undefined || option === false) return undefined;
|
|
100
|
+
if (option === true) return { mode: "require" };
|
|
101
|
+
if (option === "auto") return { mode: "auto" };
|
|
102
|
+
return { mode: option.mode ?? "auto", ...option };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Materialize Databricks AI Tools skills at app startup and return them as
|
|
107
|
+
* extra local skill scan paths.
|
|
108
|
+
*
|
|
109
|
+
* Resolution: reuse the installed global tree when present (no CLI call) unless
|
|
110
|
+
* `refresh`/`skills`/`path` ask for a fresh CLI materialization; otherwise run
|
|
111
|
+
* `databricks aitools install --path <dir> --skills-only` when the CLI resolves.
|
|
112
|
+
* A `"require"` provision that yields nothing fails startup unless
|
|
113
|
+
* `failOnError: false`.
|
|
114
|
+
*/
|
|
115
|
+
export async function provisionDatabricksAITools(
|
|
116
|
+
option: DatabricksAIToolsOption | undefined,
|
|
117
|
+
): Promise<ProvisionedDatabricksAITools> {
|
|
118
|
+
const options = normalizeDatabricksAIToolsOption(option);
|
|
119
|
+
const empty: ProvisionedDatabricksAITools = { localSkillPaths: [] };
|
|
120
|
+
if (!options) return empty;
|
|
121
|
+
|
|
122
|
+
const mode = options.mode ?? "auto";
|
|
123
|
+
const failOnError = options.failOnError ?? mode === "require";
|
|
124
|
+
const wantsCliFetch =
|
|
125
|
+
options.refresh === true ||
|
|
126
|
+
options.path !== undefined ||
|
|
127
|
+
string.parseList(options.skills).length > 0 ||
|
|
128
|
+
options.experimental === true;
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
// Fast path: reuse the already-installed global tree, no CLI call.
|
|
132
|
+
if (!wantsCliFetch && installedTreeExists()) {
|
|
133
|
+
logger.debug("using installed aitools tree", { path: GLOBAL_AITOOLS_SKILLS_PATH });
|
|
134
|
+
return { localSkillPaths: [GLOBAL_AITOOLS_SKILLS_PATH], source: "installed" };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const cliPath = await resolveCli(options.cli);
|
|
138
|
+
if (cliPath) {
|
|
139
|
+
const dir = await materializeViaCli(cliPath, options);
|
|
140
|
+
if (dir) return { localSkillPaths: [dir], source: "cli" };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Nothing fetched. Fall back to the installed tree if it's there.
|
|
144
|
+
if (installedTreeExists()) {
|
|
145
|
+
return { localSkillPaths: [GLOBAL_AITOOLS_SKILLS_PATH], source: "installed" };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (failOnError) {
|
|
149
|
+
throw new Error(
|
|
150
|
+
`Databricks AI Tools requested but no installed skills tree at "${GLOBAL_AITOOLS_SKILLS_PATH}" and the "${DATABRICKS_CLI}" CLI is not resolvable. Install with \`databricks aitools install\`, or set databricksAITools: "auto".`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
logger.debug("databricks aitools unavailable; skipping");
|
|
154
|
+
return empty;
|
|
155
|
+
} catch (err) {
|
|
156
|
+
if (failOnError) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
`failed to provision Databricks AI Tools skills: ${error.errorMessage(err)}`,
|
|
159
|
+
{ cause: error.toError(err) },
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
logger.warn("skipped", { error: error.errorMessage(err) });
|
|
163
|
+
return empty;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Whether the CLI's global skills tree exists and holds at least one skill. */
|
|
168
|
+
function installedTreeExists(): boolean {
|
|
169
|
+
return existsSync(GLOBAL_AITOOLS_SKILLS_PATH);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Resolve the `databricks` CLI, returning its invocation path or `undefined`. */
|
|
173
|
+
async function resolveCli(cli: string | undefined): Promise<string | undefined> {
|
|
174
|
+
const command = string.trimToNull(cli) ?? DATABRICKS_CLI;
|
|
175
|
+
try {
|
|
176
|
+
const result = await spawn(command, ["aitools", "version"], {
|
|
177
|
+
stdout: "capture",
|
|
178
|
+
stderr: "capture",
|
|
179
|
+
check: false,
|
|
180
|
+
});
|
|
181
|
+
if (result.exitCode === 0) return command;
|
|
182
|
+
} catch {
|
|
183
|
+
// CLI absent or `aitools` unsupported - caller falls back / fails.
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Run `databricks aitools install --path <dir> --skills-only` to materialize a
|
|
190
|
+
* resolved, agent-agnostic skill set into a directory (no agents, no state).
|
|
191
|
+
*/
|
|
192
|
+
async function materializeViaCli(
|
|
193
|
+
cli: string,
|
|
194
|
+
options: DatabricksAIToolsOptions,
|
|
195
|
+
): Promise<string | undefined> {
|
|
196
|
+
const dir =
|
|
197
|
+
string.trimToNull(options.path) ?? (await mkdtemp(join(tmpdir(), "databricks-aitools-")));
|
|
198
|
+
const args = ["aitools", "install", "--path", dir, "--skills-only"];
|
|
199
|
+
const skills = string.parseList(options.skills);
|
|
200
|
+
if (skills.length > 0) args.push("--skills", skills.join(","));
|
|
201
|
+
if (options.experimental) args.push("--experimental");
|
|
202
|
+
|
|
203
|
+
const result = await spawn(cli, args, {
|
|
204
|
+
stdout: "capture",
|
|
205
|
+
stderr: "capture",
|
|
206
|
+
check: false,
|
|
207
|
+
});
|
|
208
|
+
if (result.exitCode !== 0) {
|
|
209
|
+
throw new Error(
|
|
210
|
+
`databricks aitools install failed (exit ${result.exitCode}): ${result.stderr || result.stdout}`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
if (!existsSync(dir)) return undefined;
|
|
214
|
+
logger.debug("materialized aitools skills via CLI", { path: dir, skills });
|
|
215
|
+
return dir;
|
|
216
|
+
}
|
package/src/plugin.ts
CHANGED
|
@@ -97,6 +97,7 @@ import { createMemoryBuilder, createServicePrincipalPool, needsLakebase } from "
|
|
|
97
97
|
import { logFeedback, resolveFeedbackEnabled } from "./mlflow.ts";
|
|
98
98
|
import { buildObservability } from "./observability.ts";
|
|
99
99
|
import { provisionRemoteSkills } from "./remote-skills.ts";
|
|
100
|
+
import { provisionDatabricksAITools } from "./databricks-aitools.ts";
|
|
100
101
|
import {
|
|
101
102
|
attachRoutePatchMiddleware,
|
|
102
103
|
createRequestContext,
|
|
@@ -1011,12 +1012,24 @@ export class MastraPlugin extends Plugin<MastraPluginConfig> {
|
|
|
1011
1012
|
});
|
|
1012
1013
|
}
|
|
1013
1014
|
|
|
1015
|
+
// Fold Databricks AI Tools skills (`databricks aitools`) in as extra local
|
|
1016
|
+
// skill scan paths: reuse the CLI's installed tree when present, else shell
|
|
1017
|
+
// out to the CLI to materialize a curated set.
|
|
1018
|
+
const aiTools = await provisionDatabricksAITools(this.config.databricksAITools);
|
|
1019
|
+
if (aiTools.localSkillPaths.length > 0) {
|
|
1020
|
+
this.logger.info("databricks ai tools provisioned", {
|
|
1021
|
+
source: aiTools.source,
|
|
1022
|
+
localSkillPaths: aiTools.localSkillPaths.length,
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
const extraSkillPaths = [...provisioned.localSkillPaths, ...aiTools.localSkillPaths];
|
|
1026
|
+
|
|
1014
1027
|
this.built = await buildAgents({
|
|
1015
1028
|
config: this.config,
|
|
1016
1029
|
context: this.context,
|
|
1017
1030
|
memoryBuilder,
|
|
1018
1031
|
log: this.logger,
|
|
1019
|
-
extraSkillPaths
|
|
1032
|
+
extraSkillPaths,
|
|
1020
1033
|
});
|
|
1021
1034
|
|
|
1022
1035
|
// `mastra.server.apiRoutes` is only honored by Mastra's standalone
|