@dbx-tools/appkit-mastra 0.1.12 → 0.1.18

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/src/config.ts CHANGED
@@ -7,9 +7,14 @@
7
7
 
8
8
  import type { BasePluginConfig, getExecutionContext } from "@databricks/appkit";
9
9
  import type { AgentConfig } from "@mastra/core/agent";
10
+ import {
11
+ MASTRA_RESOURCE_ID_KEY,
12
+ MASTRA_THREAD_ID_KEY,
13
+ } from "@mastra/core/request-context";
10
14
  import type { PgVectorConfig, PostgresStoreConfig } from "@mastra/pg";
11
15
 
12
16
  import type { MastraAgentDefinition, MastraTools } from "./agents.js";
17
+ import type { GenieSpacesConfig } from "./genie.js";
13
18
 
14
19
  /**
15
20
  * `RequestContext` key under which {@link MastraServer} stores the
@@ -18,6 +23,51 @@ import type { MastraAgentDefinition, MastraTools } from "./agents.js";
18
23
  */
19
24
  export const MASTRA_USER_KEY = "mastra__user";
20
25
 
26
+ /**
27
+ * `RequestContext` keys for AppKit user metadata stamped by
28
+ * {@link MastraServer}. Surfaced as trace metadata via
29
+ * {@link TRACE_REQUEST_CONTEXT_KEYS} so traces are filterable by who
30
+ * issued the request without leaking the full user object.
31
+ */
32
+ export const MASTRA_USER_NAME_KEY = "mastra__userName";
33
+ export const MASTRA_USER_EMAIL_KEY = "mastra__userEmail";
34
+
35
+ /**
36
+ * `RequestContext` key for the per-HTTP-request id stamped by
37
+ * {@link MastraServer}. Reads `X-Request-Id` from the incoming
38
+ * headers when present (so an upstream load balancer / API gateway
39
+ * can keep its trace correlation), falls back to a freshly minted
40
+ * UUID. Echoed back on the response and surfaced on every span via
41
+ * {@link TRACE_REQUEST_CONTEXT_KEYS} so logs and traces share a
42
+ * join key.
43
+ */
44
+ export const MASTRA_REQUEST_ID_KEY = "mastra__requestId";
45
+
46
+ /**
47
+ * Canonical list of `RequestContext` keys we want Mastra to extract
48
+ * as metadata on every observability span (agent runs, model calls,
49
+ * tool invocations, workflow steps).
50
+ *
51
+ * Mirrors {@link https://mastra.ai/docs/observability/tracing/overview#automatic-metadata-from-requestcontext}:
52
+ * passed verbatim into `Observability.configs[*].requestContextKeys`,
53
+ * so any key listed here is read from `RequestContext` at trace
54
+ * start and attached as scalar span metadata. Keep the set to plain
55
+ * scalars - never include {@link MASTRA_USER_KEY} (it carries the
56
+ * full AppKit execution context with a `WorkspaceClient` reference).
57
+ *
58
+ * Order is purely cosmetic; Mastra de-dupes internally.
59
+ */
60
+ export const TRACE_REQUEST_CONTEXT_KEYS: readonly string[] = [
61
+ MASTRA_RESOURCE_ID_KEY,
62
+ MASTRA_THREAD_ID_KEY,
63
+ MASTRA_REQUEST_ID_KEY,
64
+ MASTRA_USER_NAME_KEY,
65
+ MASTRA_USER_EMAIL_KEY,
66
+ // Model override key is owned by `serving.ts`; spelled inline here
67
+ // so this module stays leaf-level (no cycles with `serving.ts`).
68
+ "mastra__model_override",
69
+ ];
70
+
21
71
  /** AppKit execution context plus the canonical user id. */
22
72
  export interface User {
23
73
  id: string;
@@ -189,4 +239,74 @@ export interface MastraPluginConfig extends BasePluginConfig {
189
239
  * and the style block leans on the model's recency bias.
190
240
  */
191
241
  styleInstructions?: string | false;
242
+ /**
243
+ * Genie spaces this plugin's agents can delegate to. One Mastra
244
+ * tool is registered per alias (`genie` for the well-known
245
+ * `default` alias, `genie_<alias>` otherwise). Each tool spins
246
+ * up a per-question Genie sub-agent that runs Databricks
247
+ * "agent mode" against the space, broadcasts wire events to the
248
+ * UI, fetches statement rows for non-empty results, and returns
249
+ * a `(string | data | chart)[]` summary the host UI renders
250
+ * inline.
251
+ *
252
+ * Entries accept either a full {@link GenieSpaceConfig} object
253
+ * or a bare `space_id` string when no extras are needed:
254
+ *
255
+ * ```ts
256
+ * mastra({
257
+ * genieSpaces: {
258
+ * default: "01ef0d3c0e1b1f4a8d2c3e4f5a6b7c8d",
259
+ * forecasts: { spaceId: "01ef...", hint: "weekly demand forecasts" },
260
+ * },
261
+ * });
262
+ * ```
263
+ *
264
+ * Reach the spaces from an agent's `tools(plugins)` callback via
265
+ * `plugins.genie?.toolkit()`; the resulting tools accept
266
+ * `{ content, conversationId? }` and return a hydrated summary.
267
+ *
268
+ * **Fallback discovery** (highest precedence first): if this
269
+ * field is omitted, the Genie agent also picks up spaces from
270
+ * (1) the AppKit `genie({ spaces: { ... } })` plugin instance
271
+ * when registered, and (2) the `DATABRICKS_GENIE_SPACE_ID`
272
+ * env var (registered under the `default` alias). This keeps
273
+ * existing AppKit deployments working without restating the
274
+ * spaces config in two places.
275
+ */
276
+ genieSpaces?: GenieSpacesConfig;
277
+ /**
278
+ * TTL for the in-memory Genie space metadata cache, in
279
+ * milliseconds. Defaults to 5 minutes. The Genie agent calls
280
+ * `client.genie.getSpace(...)` on every cold-start to get the
281
+ * title / description / warehouse id; cached responses skip the
282
+ * round-trip and concurrent callers coalesce on a single
283
+ * in-flight fetch. Drop to a smaller value when analysts are
284
+ * actively editing space metadata and you want changes visible
285
+ * within seconds; raise it to amortise the round-trip when
286
+ * space metadata is effectively frozen.
287
+ *
288
+ * Backed by AppKit's `CacheManager`, so the cache participates
289
+ * in telemetry spans (`cache.getOrExecute`) and benefits from
290
+ * Lakebase persistence when the `lakebase` plugin is wired up.
291
+ */
292
+ genieSpaceCacheTtlMs?: number;
293
+ /**
294
+ * Maximum LLM steps the Genie agent gets per turn.
295
+ * Each step is one round-trip to the underlying model
296
+ * (`get_space_description`, each `ask_genie` call, and the
297
+ * mandatory `submit_summary` closer all consume one step).
298
+ *
299
+ * Defaults to 16, which fits ~10 `ask_genie` calls plus
300
+ * grounding plus the summary closer - matches the
301
+ * decomposition pattern the Genie agent instructions prescribe
302
+ * ("2-6 ask_genie calls for any non-trivial question").
303
+ * Mastra's own `agent.generate` default of 5 would cut the
304
+ * Genie agent off after 2-3 Genie calls, so explicitly raising
305
+ * the ceiling here is what lets the agent-mode loop play out.
306
+ *
307
+ * Lower this when an unusually slow or expensive model makes
308
+ * long turns unaffordable; raise it for exploratory workloads
309
+ * that need to drill into a dataset.
310
+ */
311
+ genieAgentMaxSteps?: number;
192
312
  }