@dbx-tools/appkit-mastra 0.3.44 → 0.4.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.
Files changed (56) hide show
  1. package/lib/index.d.ts +71 -0
  2. package/lib/index.js +56 -0
  3. package/lib/src/agents.d.ts +347 -0
  4. package/lib/src/agents.js +554 -0
  5. package/lib/src/chart.d.ts +192 -0
  6. package/lib/src/chart.js +638 -0
  7. package/lib/src/config.d.ts +479 -0
  8. package/lib/src/config.js +190 -0
  9. package/lib/src/defaults.d.ts +68 -0
  10. package/lib/src/defaults.js +107 -0
  11. package/lib/src/filesystems.d.ts +208 -0
  12. package/lib/src/filesystems.js +958 -0
  13. package/lib/src/genie.d.ts +166 -0
  14. package/lib/src/genie.js +969 -0
  15. package/lib/src/history.d.ts +97 -0
  16. package/lib/src/history.js +264 -0
  17. package/lib/src/mcp.d.ts +66 -0
  18. package/lib/src/mcp.js +65 -0
  19. package/lib/src/memory.d.ts +111 -0
  20. package/lib/src/memory.js +275 -0
  21. package/lib/src/mlflow.d.ts +63 -0
  22. package/lib/src/mlflow.js +117 -0
  23. package/lib/src/model.d.ts +62 -0
  24. package/lib/src/model.js +168 -0
  25. package/lib/src/observability.d.ts +81 -0
  26. package/lib/src/observability.js +98 -0
  27. package/lib/src/pagination.d.ts +23 -0
  28. package/lib/src/pagination.js +31 -0
  29. package/lib/src/plugin.d.ts +352 -0
  30. package/lib/src/plugin.js +1015 -0
  31. package/lib/src/processors.d.ts +62 -0
  32. package/lib/src/processors.js +162 -0
  33. package/lib/src/rest.d.ts +36 -0
  34. package/lib/src/rest.js +46 -0
  35. package/lib/src/server.d.ts +155 -0
  36. package/lib/src/server.js +336 -0
  37. package/lib/src/serving-sanitize.d.ts +104 -0
  38. package/lib/src/serving-sanitize.js +228 -0
  39. package/lib/src/serving.d.ts +61 -0
  40. package/lib/src/serving.js +78 -0
  41. package/lib/src/statement.d.ts +51 -0
  42. package/lib/src/statement.js +83 -0
  43. package/lib/src/storage-schema.d.ts +14 -0
  44. package/lib/src/storage-schema.js +34 -0
  45. package/lib/src/summarize.d.ts +70 -0
  46. package/lib/src/summarize.js +142 -0
  47. package/lib/src/threads.d.ts +109 -0
  48. package/lib/src/threads.js +301 -0
  49. package/lib/src/validation.d.ts +19 -0
  50. package/lib/src/validation.js +17 -0
  51. package/lib/src/workspaces.d.ts +68 -0
  52. package/lib/src/workspaces.js +246 -0
  53. package/lib/src/writer.d.ts +25 -0
  54. package/lib/src/writer.js +40 -0
  55. package/lib/tsconfig.tsbuildinfo +1 -0
  56. package/package.json +17 -13
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Genie tools for Mastra.
3
+ *
4
+ * Surfaces each configured Genie space as a small set of flat Mastra
5
+ * tools the calling agent drives directly - no inner orchestrator
6
+ * agent. The central agent decomposes user questions, picks which
7
+ * space to ask, streams the per-turn wire events (status, thinking,
8
+ * sql, rows) through `ctx.writer`, and composes the final reply.
9
+ * Rows are never fetched eagerly: the agent reads a statement's
10
+ * values only when it needs to reason about them, otherwise it embeds
11
+ * a `[data:<statement_id>]` marker in prose and lets the host UI
12
+ * resolve the data. Charts are minted asynchronously and referenced
13
+ * by `[chart:<chartId>]` markers so prose isn't blocked on chart
14
+ * generation; the host UI fetches the cached spec by id once ready.
15
+ * Space description and serialized-space lookups are available for
16
+ * grounding when the agent needs schema context.
17
+ *
18
+ * Each tool's `execute` pulls the per-request
19
+ * {@link WorkspaceClient} off `ctx.requestContext` (stamped by
20
+ * `MastraServer` under {@link MASTRA_USER_KEY}) and the per-call
21
+ * `writer` / `abortSignal` off `ctx`, so the tools are stateless
22
+ * across requests and the central agent owns the loop.
23
+ *
24
+ * The tools talk to Genie directly via `@dbx-tools/genie`
25
+ * (`genieEventChat`); statement-row fetching is delegated to
26
+ * {@link fetchStatementData} from `./statement.js`, which wraps
27
+ * the workspace `statementExecution.getStatement` API. AppKit's
28
+ * stock `genie` plugin is honored only for its `spaces` config
29
+ * so existing AppKit-style wiring keeps working without change.
30
+ *
31
+ * Suggested orchestration prompt for the central agent lives in
32
+ * {@link GENIE_INSTRUCTIONS}; compose it into the agent's own
33
+ * `instructions` when you want the canonical "how to drive the
34
+ * Genie tools" guidance.
35
+ *
36
+ * @module
37
+ */
38
+ import { WorkspaceClient } from "@databricks/sdk-experimental";
39
+ import { plugin } from "@dbx-tools/appkit";
40
+ import type { MastraTools } from "./agents.js";
41
+ import type { MastraPluginConfig } from "./config.js";
42
+ /** Default alias used when a single unnamed Genie space is wired up. */
43
+ export declare const DEFAULT_GENIE_ALIAS = "default";
44
+ /** Per-space Genie agent configuration. */
45
+ export interface GenieSpaceConfig {
46
+ /** Genie `space_id`. Required; resolves via `client.genie.getSpace`. */
47
+ spaceId: string;
48
+ /**
49
+ * Optional human-readable description appended to the per-space
50
+ * tool descriptions so the calling LLM has hints about *what
51
+ * data* this space covers (e.g. "orders, returns,
52
+ * fulfillment"). When omitted, only the space's own
53
+ * `description` (fetched on first use of `get_space_description`)
54
+ * is shown.
55
+ */
56
+ hint?: string;
57
+ }
58
+ /** Map of alias -> space config. Accepts either explicit objects or bare space ids. */
59
+ export type GenieSpacesConfig = Record<string, GenieSpaceConfig | string>;
60
+ /**
61
+ * Suggested orchestration prompt for the central agent that owns
62
+ * the Genie tools. Compose into your agent's `instructions` to
63
+ * get the canonical "decompose questions, ask Genie focused
64
+ * sub-questions, place data / chart markers in prose" behavior:
65
+ *
66
+ * ```ts
67
+ * createAgent({
68
+ * instructions: `${myAgentInstructions}\n\n${GENIE_INSTRUCTIONS}`,
69
+ * tools(plugins) {
70
+ * return { ...plugins.genie?.toolkit() };
71
+ * },
72
+ * });
73
+ * ```
74
+ *
75
+ * The prompt references the bare tool names (`ask_genie`,
76
+ * `get_space_description`, `get_space_serialized`,
77
+ * `get_statement`, `prepare_chart`) used for the single-space
78
+ * default alias. Multi-space deployments should write their own
79
+ * variant that names the suffixed per-space tools
80
+ * (e.g. `ask_genie_sales`).
81
+ */
82
+ export declare const GENIE_INSTRUCTIONS: string;
83
+ /**
84
+ * Normalize the {@link GenieSpacesConfig} record. Bare-string
85
+ * entries (`{ default: "01ef..." }`) get wrapped as
86
+ * `{ spaceId: "01ef..." }`; object entries pass through unchanged.
87
+ *
88
+ * @throws ConfigurationError when an alias is present but carries no space id
89
+ * (`{ default: process.env.DATABRICKS_GENIE_SPACE_ID }` with the variable
90
+ * unset). An alias that resolves to nothing is a wiring contradiction: the
91
+ * agent would advertise no Genie tool for a space the deployment believes it
92
+ * configured, so it fails at construction instead of going quiet.
93
+ */
94
+ export declare function normalizeGenieSpaces(spaces: GenieSpacesConfig | Record<string, string | GenieSpaceConfig | undefined> | undefined): Record<string, GenieSpaceConfig>;
95
+ /**
96
+ * Discover Genie space aliases from every supported source and
97
+ * merge them into a single record. Precedence (highest first):
98
+ *
99
+ * 1. {@link MastraPluginConfig.genieSpaces} on the `mastra(...)`
100
+ * call. Explicit Mastra wiring always wins so users can
101
+ * override AppKit's defaults per-agent.
102
+ * 2. AppKit `genie({ spaces: { ... } })` plugin instance. Lets
103
+ * users keep using the existing AppKit config format
104
+ * (`genie({ spaces: { sales: "...", ops: "..." } })`)
105
+ * without restating the same record on the Mastra plugin.
106
+ * Read off the live plugin instance via a structural cast
107
+ * since `Plugin.config` is TS-protected (not runtime-private).
108
+ * 3. `DATABRICKS_GENIE_SPACE_ID` env var (registered under the
109
+ * well-known `default` alias). Matches the AppKit `genie()`
110
+ * plugin's fallback behavior so a bare `mastra()` + `genie()`
111
+ * pair just works.
112
+ *
113
+ * Aliases collide cleanly: a higher-precedence source's value
114
+ * replaces a lower one's wholesale. A source that contributes zero
115
+ * aliases is skipped; a source that names an alias without a space
116
+ * id fails through {@link normalizeGenieSpaces}.
117
+ */
118
+ export declare function resolveGenieSpaces(config: MastraPluginConfig, context: plugin.PluginContextLike | undefined): Record<string, GenieSpaceConfig>;
119
+ /**
120
+ * Build the flat Mastra tools record for every configured Genie
121
+ * space. Two shared, space-agnostic tools (`get_statement`,
122
+ * `prepare_chart`) are registered once regardless of how many
123
+ * spaces are wired; the per-space tools (`ask_genie`,
124
+ * `get_space_description`, `get_space_serialized`) are suffixed
125
+ * with `_<alias>` for non-default aliases so multi-space
126
+ * deployments stay disambiguated.
127
+ *
128
+ * Returns a record keyed by tool id, ready to spread into the
129
+ * central `Agent`'s `tools` map (or surfaced via the
130
+ * `plugins.genie?.toolkit()` callback). Returns an empty record
131
+ * when `spaces` resolves to zero entries so the caller can spread
132
+ * safely.
133
+ */
134
+ export declare function buildGenieTools(opts: {
135
+ spaces: GenieSpacesConfig | Record<string, GenieSpaceConfig>;
136
+ config: MastraPluginConfig;
137
+ }): MastraTools;
138
+ /**
139
+ * Plugin-toolkit adapter so the `plugins.genie?.toolkit()` lookup
140
+ * inside an agent's `tools(plugins)` callback returns the
141
+ * flat Genie tools record instead of throwing on missing plugin.
142
+ * Mirrors AppKit's `PluginToolkitProvider` shape.
143
+ */
144
+ export declare function buildGenieToolkitProvider(opts: {
145
+ spaces: GenieSpacesConfig | Record<string, GenieSpaceConfig>;
146
+ config: MastraPluginConfig;
147
+ }): {
148
+ toolkit(opts?: unknown): MastraTools;
149
+ };
150
+ /**
151
+ * Collect the curated starter questions across every resolved Genie
152
+ * space, deduped and capped. Each space's `sample_questions` are
153
+ * fetched once (cached for {@link SUGGESTION_CACHE_TTL_MS}) via
154
+ * {@link getGenieSpace} + {@link genieSampleQuestions}, then merged in
155
+ * alias-iteration order so a single-space app surfaces that space's
156
+ * questions and a multi-space app round-trips breadth-first up to the
157
+ * cap. A per-space fetch failure degrades to "no questions for that
158
+ * space" (logged, not thrown) so one unreachable space never blanks
159
+ * the whole list. Returns `[]` when no spaces are configured.
160
+ */
161
+ export declare function collectSpaceSuggestions(opts: {
162
+ spaces: Record<string, GenieSpaceConfig>;
163
+ client: WorkspaceClient;
164
+ signal?: AbortSignal;
165
+ limit?: number;
166
+ }): Promise<string[]>;