@dbx-tools/appkit-mastra 0.1.67 → 0.1.68

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/memory.ts DELETED
@@ -1,293 +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
- * Additionally, {@link MemoryBuilder.instanceStorage} returns a
18
- * **Mastra-instance-level** `PostgresStore` (schema `mastra_instance`)
19
- * used for workflow snapshots - the persistence layer
20
- * `agent.resumeStream()` reads from when waking a suspended
21
- * `requireApproval` tool call. Per-agent stores are not enough for
22
- * this: workflow runs are scoped to the Mastra instance, not an
23
- * individual agent's `Memory`.
24
- *
25
- * Plugin-level `config.storage` / `config.memory` act as the baseline
26
- * (auto-defaulted to `true` in `plugin.ts` when the `lakebase` plugin
27
- * is registered); per-agent settings cascade on top of that.
28
- */
29
-
30
- import { getUsernameWithApiLookup } from "@databricks/appkit";
31
- import { logUtils } from "@dbx-tools/shared";
32
- import { fastembed } from "@mastra/fastembed";
33
- import { Memory } from "@mastra/memory";
34
- import { PgVector, PostgresStore } from "@mastra/pg";
35
- import { randomUUID } from "node:crypto";
36
- import { Pool, type PoolConfig } from "pg";
37
-
38
- import type { MastraAgentDefinition, MastraMemoryConfigOverride } from "./agents.js";
39
- import type { MastraPluginConfig } from "./config.js";
40
-
41
- const log = logUtils.logger("mastra/memory");
42
-
43
- /**
44
- * Build a dedicated **service-principal** Lakebase pool for Mastra
45
- * memory from the lakebase plugin's resolved SP pg config.
46
- *
47
- * The plugin's `exports().pool` is a `RoutingPool` that switches to
48
- * the per-user (OBO) pool whenever a query runs inside an `asUser`
49
- * scope - exactly the context the mastra plugin establishes around
50
- * every chat turn. Memory (threads / messages + semantic recall) must
51
- * instead always act as the app service principal: it owns the
52
- * auto-created `mastra_*` schemas (a per-user role usually can't
53
- * `CREATE SCHEMA`) and is shared across users, so it cannot inherit a
54
- * request's OBO identity.
55
- *
56
- * `pgConfig` must be the plugin's `exports().getPgConfig()` evaluated
57
- * **outside** any `asUser` scope (i.e. during setup), so it carries
58
- * the SP connection target, OAuth token-refresh `password` callback,
59
- * and any `lakebase({ pool })` tuning overrides - all of which this
60
- * pool inherits. See the call site in `plugin.ts`.
61
- */
62
- export async function createServicePrincipalPool(pgConfig: PoolConfig): Promise<Pool> {
63
- // `getPgConfig()` resolves the SP username synchronously from
64
- // `PGUSER` / `DATABRICKS_CLIENT_ID`; fall back to the async API
65
- // lookup (e.g. local dev authenticating via PAT) so the pool always
66
- // has an identity to connect with.
67
- const user = pgConfig.user ?? (await getUsernameWithApiLookup());
68
- return new Pool({ ...pgConfig, user });
69
- }
70
-
71
- /** Effective per-knob setting after the plugin/agent cascade. */
72
- type StorageSetting = MastraAgentDefinition["storage"];
73
- type MemorySetting = MastraAgentDefinition["memory"];
74
-
75
- /**
76
- * True when any plugin-level or per-agent setting could need the
77
- * Lakebase pool. Used by `plugin.ts` to gate creation of the
78
- * service-principal pool and the {@link MemoryBuilder} that consumes
79
- * it; when false neither is built.
80
- */
81
- export function needsLakebase(config: MastraPluginConfig): boolean {
82
- if (settingNeedsSharedPool(config.storage)) return true;
83
- if (settingNeedsSharedPool(config.memory)) return true;
84
- const defs = collectAgentDefinitions(config);
85
- return defs.some(
86
- (d) => settingNeedsSharedPool(d.storage) || settingNeedsSharedPool(d.memory),
87
- );
88
- }
89
-
90
- /**
91
- * Construct a per-agent {@link Memory} factory bound to the supplied
92
- * service-principal pool (see {@link createServicePrincipalPool}).
93
- * Caches the shared `PgVector` singleton (built on first need) so each
94
- * agent build is O(1) after the first.
95
- */
96
- export function createMemoryBuilder(
97
- config: MastraPluginConfig,
98
- servicePrincipalPool: Pool,
99
- ): MemoryBuilder {
100
- return new MemoryBuilder(config, servicePrincipalPool);
101
- }
102
-
103
- /**
104
- * Builds one `Memory` per agent against a shared service-principal
105
- * Lakebase pool. Per-instance state keeps the shared `PgVector` alive
106
- * across calls so registering N agents stays cheap.
107
- */
108
- export class MemoryBuilder {
109
- private sharedVector: PgVector | undefined;
110
-
111
- constructor(
112
- private readonly config: MastraPluginConfig,
113
- private readonly servicePrincipalPool: Pool,
114
- ) {}
115
-
116
- /**
117
- * Build a `Memory` for `agentId` after the plugin/agent cascade.
118
- * Returns `undefined` when the agent has neither storage nor a
119
- * vector store enabled - Mastra accepts a missing `memory` field
120
- * and treats the agent as stateless.
121
- */
122
- /**
123
- * Build the Mastra-instance-level storage used for workflow
124
- * snapshots. Returns `undefined` when plugin-level `storage` is
125
- * disabled, in which case `agent.resumeStream()` (and therefore
126
- * the `requireApproval` flow) will not be available.
127
- *
128
- * The store lives in a dedicated `mastra_instance` schema so it
129
- * never collides with per-agent `mastra_<agentId>` namespaces.
130
- * Workflow snapshots are not per-agent state; they belong to the
131
- * `Mastra` instance that owns the workflow execution.
132
- */
133
- instanceStorage(): PostgresStore | undefined {
134
- const setting = this.config.storage;
135
- if (!setting) return undefined;
136
- if (typeof setting === "object") {
137
- return new PostgresStore(
138
- withId(setting, "mastra-store__instance") as ConstructorParameters<
139
- typeof PostgresStore
140
- >[0],
141
- );
142
- }
143
- return new PostgresStore({
144
- id: "mastra-store__instance",
145
- schemaName: "mastra_instance",
146
- pool: this.servicePrincipalPool,
147
- });
148
- }
149
-
150
- forAgent(agentId: string, def: MastraAgentDefinition): Memory | undefined {
151
- const storageSetting = def.storage ?? this.config.storage;
152
- const memorySetting = def.memory ?? this.config.memory;
153
-
154
- const storage = this.buildStorage(agentId, storageSetting);
155
- const vector = this.buildVector(memorySetting);
156
- if (!storage && !vector) {
157
- log.debug("agent:stateless", { agentId });
158
- return undefined;
159
- }
160
-
161
- log.debug("agent:configured", {
162
- agentId,
163
- storage: storage !== undefined,
164
- vector: vector !== undefined,
165
- vectorMode:
166
- vector === undefined
167
- ? "off"
168
- : typeof memorySetting === "object"
169
- ? "dedicated"
170
- : "shared",
171
- });
172
-
173
- return new Memory({
174
- ...(storage ? { storage } : {}),
175
- ...(vector ? { vector, embedder: fastembed } : {}),
176
- options: {
177
- lastMessages: 10,
178
- ...(vector ? { semanticRecall: { topK: 3, messageRange: 2 } } : {}),
179
- },
180
- });
181
- }
182
-
183
- private buildStorage(
184
- agentId: string,
185
- setting: StorageSetting,
186
- ): PostgresStore | undefined {
187
- if (!setting) return undefined;
188
- if (typeof setting === "boolean") {
189
- return new PostgresStore({
190
- id: `mastra-store__${agentId}`,
191
- schemaName: `mastra_${agentId}`,
192
- pool: this.servicePrincipalPool,
193
- });
194
- }
195
- // Cast: `withId` guarantees `id` is set, but the distributive
196
- // Omit + `id?: string` shape doesn't structurally narrow to the
197
- // discriminated union members. Runtime shape is identical.
198
- return new PostgresStore(
199
- withId(setting, `mastra-store__${agentId}`) as ConstructorParameters<
200
- typeof PostgresStore
201
- >[0],
202
- );
203
- }
204
-
205
- /**
206
- * Resolve the agent's vector store. Cascade:
207
- *
208
- * - falsy: no vector.
209
- * - `boolean` / `undefined-inheriting-true`: return the shared
210
- * singleton (built lazily on first call). All agents that
211
- * default-enable memory write into and recall from one index.
212
- * - object: build a dedicated `PgVector` for this agent.
213
- */
214
- private buildVector(setting: MemorySetting): PgVector | undefined {
215
- if (!setting) return undefined;
216
- if (typeof setting === "boolean") return this.getSharedVector();
217
- return buildPgVector(setting);
218
- }
219
-
220
- private getSharedVector(): PgVector {
221
- if (!this.sharedVector) {
222
- this.sharedVector = buildSharedPgVector(this.servicePrincipalPool);
223
- }
224
- return this.sharedVector;
225
- }
226
- }
227
-
228
- /**
229
- * Build the shared `PgVector` that backs the default
230
- * `def.memory === true` case across every agent.
231
- *
232
- * `PgVector`'s constructor accepts only connection-style configs
233
- * (`HostConfig` / `ConnectionStringConfig` / `ClientConfig`); there is
234
- * no `{ pool }` shorthand the way `PostgresStore` has one. Worse, the
235
- * constructor synchronously kicks off a `cacheWarmupPromise` IIFE that
236
- * calls `this.pool.connect()` before returning, so we can't cleanly
237
- * hand it an inert config and patch the pool afterwards.
238
- *
239
- * The trick: pass illegal-but-validation-passing placeholders so the
240
- * warmup's `net.connect()` rejects synchronously with `RangeError`
241
- * (Node validates `0 <= port < 65536`). The IIFE's `catch {}` swallows
242
- * it, no DNS lookup or TCP attempt happens, and we then swap
243
- * `pgVector.pool` to the lakebase pool. Every subsequent `PgVector`
244
- * method reads `this.pool` at call time, so all real I/O goes through
245
- * the lakebase pool from then on. The placeholder pool is `.end()`'d
246
- * so its socket book-keeping is released.
247
- */
248
- function buildSharedPgVector(pool: Pool): PgVector {
249
- const vector = new PgVector({
250
- id: `pg${randomUUID()}`,
251
- host: "-1",
252
- port: -1,
253
- database: "_",
254
- user: "_",
255
- password: "_",
256
- });
257
- const placeholder = vector.pool;
258
- vector.pool = pool;
259
- void placeholder.end().catch(() => undefined);
260
- return vector;
261
- }
262
-
263
- /** Per-agent dedicated `PgVector` (rare; opt-in via object override). */
264
- function buildPgVector(setting: MastraMemoryConfigOverride): PgVector {
265
- return new PgVector(
266
- withId(setting, `pg-vector__${randomUUID()}`) as ConstructorParameters<
267
- typeof PgVector
268
- >[0],
269
- );
270
- }
271
-
272
- /** True when this setting requires the shared Lakebase pool. */
273
- function settingNeedsSharedPool(
274
- setting: StorageSetting | MemorySetting | undefined,
275
- ): boolean {
276
- return setting === true;
277
- }
278
-
279
- /** Walk the three shapes of `config.agents` into a flat list. */
280
- function collectAgentDefinitions(config: MastraPluginConfig): MastraAgentDefinition[] {
281
- const agents = config.agents;
282
- if (!agents) return [];
283
- if (Array.isArray(agents)) return agents;
284
- if (typeof (agents as MastraAgentDefinition).instructions === "string") {
285
- return [agents as MastraAgentDefinition];
286
- }
287
- return Object.values(agents as Record<string, MastraAgentDefinition>);
288
- }
289
-
290
- /** Fill in a default `id` when the caller didn't supply one. */
291
- function withId<T extends { id?: string }>(value: T, fallback: string): T {
292
- return value.id ? value : { ...value, id: fallback };
293
- }
package/src/model.ts DELETED
@@ -1,263 +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
- * This module only adds the Mastra-specific glue. The actual model
11
- * selection - listing the workspace catalogue and resolving an
12
- * explicit name / class / fallback chain to a real endpoint id - lives
13
- * in `@dbx-tools/model` ({@link selectModel}) so non-Mastra consumers
14
- * (e.g. a job that just needs a model name) can reuse it. Here we
15
- * assemble the explicit ask from Mastra's request context (the
16
- * per-request override under {@link MASTRA_MODEL_OVERRIDE_KEY}, the
17
- * agent / plugin `modelId`, or `DATABRICKS_SERVING_ENDPOINT_NAME`),
18
- * pass the plugin's fuzzy / class / fallback knobs through, and wrap
19
- * the resolved id in the OpenAI-compatible provider config Mastra
20
- * expects. Catalogue fetches fail loud: network / auth errors
21
- * propagate so callers see the real SDK message.
22
- */
23
-
24
- import { getExecutionContext } from "@databricks/appkit";
25
- import { type ModelClass, parseModelClass, selectModel } from "@dbx-tools/model";
26
- import { commonUtils, logUtils, netUtils, stringUtils } from "@dbx-tools/shared";
27
- import type { MastraModelConfig } from "@mastra/core/llm";
28
- import type { RequestContext } from "@mastra/core/request-context";
29
-
30
- import { MASTRA_USER_KEY, type MastraPluginConfig, type User } from "./config.js";
31
- import { MASTRA_MODEL_OVERRIDE_KEY, resolveServingConfig } from "./serving.js";
32
-
33
- export {
34
- classifyEndpoints,
35
- FALLBACK_MODEL_IDS,
36
- ModelClass,
37
- modelForClass,
38
- modelsForClass,
39
- } from "@dbx-tools/model";
40
-
41
- /** Optional overrides accepted by {@link buildModel}. */
42
- export interface BuildModelOverrides {
43
- /**
44
- * Static model id from the agent / plugin config (string sugar on
45
- * `def.model` or `config.defaultModel`). Loses to the per-request
46
- * override but wins over env / class / fallback.
47
- */
48
- modelId?: string;
49
- /**
50
- * Chat capability class to resolve when no explicit model id is
51
- * supplied. Used by internal agents (e.g. the chart planner asks for
52
- * {@link ModelClass.ChatFast}) to express intent without pinning an
53
- * endpoint name; the live catalogue is classified and the top
54
- * available model in the class is chosen, falling back to the
55
- * class's static list when the workspace has none.
56
- */
57
- modelClass?: ModelClass;
58
- }
59
-
60
- /**
61
- * Resolve a `MastraModelConfig` for the current agent step. Runs
62
- * while `agent.stream` is inside the `asUser(req)` scope so tokens
63
- * are user-scoped; outside an active user context the workspace
64
- * client falls back to the service principal.
65
- */
66
- export async function buildModel(
67
- config: MastraPluginConfig,
68
- requestContext: RequestContext,
69
- overrides: BuildModelOverrides = {},
70
- ): Promise<MastraModelConfig> {
71
- void setupFetchInterceptor();
72
- // The chat path stamps the AppKit user on the request context via
73
- // `MastraServer`. The MCP transport routes don't thread that context
74
- // into tool execution, so fall back to the ambient execution context
75
- // (the active OBO scope, or the service principal) when it's absent.
76
- const user = requestContext.get(MASTRA_USER_KEY) as User | undefined;
77
- const executionContext = user?.executionContext ?? getExecutionContext();
78
- const clientConfig = executionContext.client.config;
79
- const host = (await clientConfig.getHost()).toString();
80
- const headers = new Headers();
81
- await clientConfig.authenticate(headers);
82
- // The OpenAI Node SDK appends paths like `/chat/completions` to whatever
83
- // URL we hand it. Drop the trailing slash so the resulting URL stays
84
- // well-formed (`/serving-endpoints/chat/completions`).
85
- const url = new URL("/serving-endpoints", host).toString().replace(/\/$/, "");
86
-
87
- const log = logUtils.logger(config);
88
- const serving = resolveServingConfig(config);
89
- const override = serving.allowOverride
90
- ? (requestContext.get(MASTRA_MODEL_OVERRIDE_KEY) as string | undefined)
91
- : undefined;
92
-
93
- // The override / agent default / env value can be either a concrete
94
- // endpoint name or a model class slug ("chat-thinking" /
95
- // "chat-balanced" / "chat-fast"). A class slug becomes a class intent
96
- // (let the live catalogue pick the best model in that band); anything
97
- // else is an explicit name fuzzy-matched against the catalogue. An
98
- // internal `overrides.modelClass` (e.g. the chart planner) is the
99
- // floor when nothing was requested.
100
- const requested =
101
- override ?? overrides.modelId ?? process.env.DATABRICKS_SERVING_ENDPOINT_NAME;
102
- const requestedClass = requested !== undefined ? parseModelClass(requested) : null;
103
- const explicit = requestedClass === null ? requested : undefined;
104
- const modelClass = requestedClass ?? overrides.modelClass;
105
-
106
- const { modelId, source } = await selectModel(executionContext.client, host, {
107
- ...(explicit !== undefined ? { explicit } : {}),
108
- fuzzy: serving.fuzzy,
109
- threshold: serving.threshold,
110
- ...(modelClass !== undefined ? { modelClass } : {}),
111
- fallbacks: serving.fallbacks,
112
- ttlMs: serving.ttlMs,
113
- });
114
- log.debug("model selected", { modelId, source, requested });
115
-
116
- return {
117
- providerId: config.providerId ?? "databricks",
118
- modelId,
119
- url,
120
- headers: Object.fromEntries(headers.entries()),
121
- };
122
- }
123
-
124
- /** Path prefix that identifies a Databricks Model Serving REST call. */
125
- const SERVING_ENDPOINTS_PATH_PREFIX = "/serving-endpoints/";
126
-
127
- /**
128
- * OpenAI-flavoured chat message shape we need to mutate. We do not
129
- * import the OpenAI / AI SDK types because both packages keep these
130
- * fields under internal namespaces; the wire payload is the contract
131
- * here and it's stable enough to inline.
132
- */
133
- interface ChatMessage {
134
- role: "system" | "user" | "assistant" | "tool";
135
- content?: string;
136
- tool_calls?: Array<{ id: string; type: string; function: unknown }>;
137
- tool_call_id?: string;
138
- }
139
-
140
- /**
141
- * Install a single shared `globalThis.fetch` wrapper for every POST to
142
- * `/serving-endpoints/...`. The wrapper does two things:
143
- *
144
- * 1. Rewrites the outgoing `messages` array to repair Mastra/AI SDK
145
- * stream-replay quirks that Databricks-hosted Claude rejects (see
146
- * {@link sanitizeServingMessages}).
147
- * 2. At `LOG_LEVEL=debug`, dumps the (post-sanitize) JSON body so
148
- * 4xx debugging doesn't have to fight AI SDK's `[Array]`
149
- * formatter.
150
- *
151
- * Safe to call from any hot path: {@link commonUtils.memoize} ensures
152
- * the wrapper is installed at most once per process, so subsequent
153
- * calls collapse to a single cached promise even when
154
- * {@link buildModel} fires on every agent step.
155
- */
156
- const setupFetchInterceptor = commonUtils.memoize((): void => {
157
- const log = logUtils.logger("mastra/llm");
158
- const original = globalThis.fetch.bind(globalThis);
159
- globalThis.fetch = (async (input, init) => {
160
- const url = netUtils.urlBuilder(input);
161
- if (
162
- !url ||
163
- !url.pathname.startsWith(SERVING_ENDPOINTS_PATH_PREFIX) ||
164
- typeof init?.body !== "string"
165
- ) {
166
- return original(input, init);
167
- }
168
- const rewritten = rewriteServingBody(init.body);
169
- if (rewritten !== init.body) {
170
- init = { ...init, body: rewritten };
171
- }
172
- try {
173
- log.debug("POST", { url: url.toString(), body: JSON.parse(rewritten) });
174
- } catch {
175
- log.debug("POST", { url: url.toString(), bodyType: "non-JSON" });
176
- }
177
- return original(input, init);
178
- }) as typeof globalThis.fetch;
179
- });
180
-
181
- /**
182
- * Parse, sanitize, and re-serialize a `/serving-endpoints/...` POST
183
- * body. Returns the original string verbatim when the body is not
184
- * JSON, has no `messages`, or no rewrite was needed; this lets the
185
- * caller skip the allocation of a new `init` object in the common
186
- * pass-through case.
187
- */
188
- function rewriteServingBody(body: string): string {
189
- let parsed: { messages?: unknown };
190
- try {
191
- parsed = JSON.parse(body);
192
- } catch {
193
- return body;
194
- }
195
- if (!Array.isArray(parsed.messages)) return body;
196
- const changed = sanitizeServingMessages(parsed.messages as ChatMessage[]);
197
- return changed ? JSON.stringify(parsed) : body;
198
- }
199
-
200
- /**
201
- * Repair a Mastra/AI SDK message replay that Databricks-hosted Claude
202
- * rejects with `"This model does not support assistant message
203
- * prefill. The conversation must end with a user message."`.
204
- *
205
- * The bug pattern: when an assistant turn streams text *and* a
206
- * `tool_call`, the AI SDK persists them as two separate assistant
207
- * entries (text-only and tool-call-only). On the next agent step the
208
- * tool-call entry is replayed *before* the tool result and the
209
- * text entry is replayed *after* it, so the conversation ends with a
210
- * trailing assistant text message. Anthropic interprets that as a
211
- * prefill request and rejects it on Databricks (the upstream Bedrock
212
- * route disallows prefill).
213
- *
214
- * Fix: when the last message is an assistant text with no `tool_calls`
215
- * and the chain immediately before it is `assistant(tool_calls=...)`
216
- * followed only by `tool(...)` results, fold the trailing text back
217
- * into the `content` of that opening assistant and drop the duplicate.
218
- * The result is the canonical OpenAI shape
219
- * `[..., user, assistant(text + tool_calls), tool(...)]` which both
220
- * Databricks Claude and every other endpoint accept.
221
- *
222
- * Mutates `messages` in place; returns `true` when something changed
223
- * so the caller knows whether to re-serialize.
224
- */
225
- function sanitizeServingMessages(messages: ChatMessage[]): boolean {
226
- if (messages.length < 2) return false;
227
- const last = messages[messages.length - 1];
228
- if (
229
- !last ||
230
- last.role !== "assistant" ||
231
- (last.tool_calls && last.tool_calls.length > 0)
232
- ) {
233
- return false;
234
- }
235
-
236
- // Walk back through any contiguous tool-result messages to find the
237
- // assistant turn that opened this tool sequence.
238
- let i = messages.length - 2;
239
- while (i >= 0 && messages[i]?.role === "tool") i--;
240
- if (i < 0) return false;
241
- const opener = messages[i];
242
- if (
243
- !opener ||
244
- opener.role !== "assistant" ||
245
- !opener.tool_calls ||
246
- opener.tool_calls.length === 0
247
- ) {
248
- return false;
249
- }
250
-
251
- // `trimToNull` collapses the `typeof string && trimmed` dance and
252
- // drops blank fragments before the `\n\n` join below, so the merge
253
- // never introduces stray leading / trailing whitespace.
254
- const merged = [
255
- stringUtils.trimToNull(opener.content),
256
- stringUtils.trimToNull(last.content),
257
- ]
258
- .filter((s): s is string => s !== null)
259
- .join("\n\n");
260
- opener.content = merged;
261
- messages.pop();
262
- return true;
263
- }
@@ -1,114 +0,0 @@
1
- /**
2
- * Mastra observability wired through the same OTel pipeline AppKit's
3
- * built-in plugins (e.g. `agents`) use, via `@mastra/otel-bridge`.
4
- *
5
- * How traces flow:
6
- *
7
- * 1. `@databricks/appkit` boots a global `NodeSDK` in
8
- * `TelemetryManager.initialize()` (during `createApp`) when
9
- * `OTEL_EXPORTER_OTLP_ENDPOINT` is set in the process env.
10
- * 2. Every AppKit plugin span (e.g. the `agents` plugin's
11
- * `executeStream`) is created via the global OTel tracer
12
- * (`trace.getTracer(<plugin>)`), so it lands on that NodeSDK and
13
- * is shipped through its OTLP exporter.
14
- * 3. The Mastra `OtelBridge` ALSO creates real OTel spans on the same
15
- * global tracer for every Mastra operation (agent runs, model
16
- * calls, tool invocations, workflow steps). They inherit the
17
- * ambient OTel context, so when Mastra is invoked from inside an
18
- * AppKit HTTP span the trace stays connected.
19
- *
20
- * Net effect: Mastra spans get exactly the treatment AppKit's
21
- * `agents` plugin gets. No custom OTLP pipeline lives in this
22
- * package; the OTLP endpoint, headers, and resource attributes are
23
- * driven by the standard OTel env vars
24
- * (`OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_HEADERS`,
25
- * `OTEL_SERVICE_NAME`, `OTEL_RESOURCE_ATTRIBUTES`, ...) and consumed
26
- * by AppKit's `TelemetryManager`. Set those once and both AppKit and
27
- * Mastra spans end up at the same backend.
28
- *
29
- * When `OTEL_EXPORTER_OTLP_ENDPOINT` is unset the bridge's spans go
30
- * to the global noop tracer, mirroring how the `agents` plugin
31
- * silently no-ops in the same situation.
32
- */
33
-
34
- import { logUtils, projectUtils } from "@dbx-tools/shared";
35
- import { Observability } from "@mastra/observability";
36
- import { OtelBridge } from "@mastra/otel-bridge";
37
-
38
- import { TRACE_REQUEST_CONTEXT_KEYS } from "./config.js";
39
-
40
- const log = logUtils.logger("mastra/observability");
41
-
42
- const DEFAULT_SERVICE_NAME = "mastra";
43
-
44
- export interface BuildObservabilityOptions {
45
- /**
46
- * Service name attached to the Mastra `Observability` config. Used
47
- * as the tracer scope name on bridged OTel spans (the `service.name`
48
- * resource attribute is owned by AppKit's `TelemetryManager` instead
49
- * - it reads `OTEL_SERVICE_NAME` / `DATABRICKS_APP_NAME` at
50
- * `createApp` time).
51
- *
52
- * Defaults to project name then `"mastra"`.
53
- */
54
- serviceName?: string;
55
- /**
56
- * `RequestContext` keys to extract as span metadata on every Mastra
57
- * trace. Defaults to {@link TRACE_REQUEST_CONTEXT_KEYS} (user id,
58
- * thread id, request id, environment, model override, ...).
59
- *
60
- * Supports dot notation for nested values per the Mastra docs.
61
- */
62
- requestContextKeys?: readonly string[];
63
- }
64
-
65
- /**
66
- * Build a Mastra `Observability` whose spans ride AppKit's global
67
- * OTel pipeline via `@mastra/otel-bridge`.
68
- *
69
- * Returns `undefined` only if someone explicitly opts out in the
70
- * future; today it always returns an `Observability` because the
71
- * bridge degrades gracefully (no-op tracer) when no global OTel SDK
72
- * is registered. Callers can spread `...(observability ? { observability } : {})`
73
- * either way to stay forward-compatible.
74
- */
75
- export async function buildObservability(
76
- options?: BuildObservabilityOptions,
77
- ): Promise<Observability | undefined> {
78
- const serviceName =
79
- options?.serviceName ?? (await projectUtils.name()) ?? DEFAULT_SERVICE_NAME;
80
- const requestContextKeys = [
81
- ...(options?.requestContextKeys ?? TRACE_REQUEST_CONTEXT_KEYS),
82
- ];
83
-
84
- // The OTel HTTP exporter treats `OTEL_EXPORTER_OTLP_ENDPOINT` as a
85
- // *base* URL and appends the signal path itself (e.g.
86
- // `http://localhost:6006` -> `http://localhost:6006/v1/traces`). Log
87
- // the resolved POST URL so misconfigurations (e.g. accidentally
88
- // setting the base var to a `/v1/traces`-suffixed URL, which makes
89
- // the SDK POST to `.../v1/traces/v1/traces` and Phoenix 404s) are
90
- // obvious in startup output.
91
- const otelBase = process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
92
- const otelTracesOverride = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT;
93
- const resolvedTracesUrl = otelTracesOverride
94
- ? otelTracesOverride
95
- : otelBase
96
- ? `${otelBase.replace(/\/+$/, "")}/v1/traces`
97
- : undefined;
98
- log.info("Mastra observability wired through OTel bridge", {
99
- serviceName,
100
- requestContextKeys,
101
- otelBase: otelBase ?? "<unset>",
102
- resolvedTracesUrl: resolvedTracesUrl ?? "<noop; OTLP endpoint unset>",
103
- });
104
-
105
- return new Observability({
106
- configs: {
107
- serviceName: {
108
- serviceName,
109
- bridge: new OtelBridge(),
110
- requestContextKeys,
111
- },
112
- },
113
- });
114
- }