@mastra/mcp-apps-components 0.1.0-alpha.5 → 0.1.0-alpha.6

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 CHANGED
@@ -197,6 +197,24 @@ const host = useMemo(
197
197
 
198
198
  To let the user pin widgets too, add `pins: { pinnedIds, onPin, onUnpin }`. `observabilityWidgetPins` is the same helper with only pins.
199
199
 
200
+ #### Configuring a widget
201
+
202
+ Pass `onConfigure` to `observabilityWidgetHost` to show a settings button on each widget, for example to open your own dialog for it:
203
+
204
+ ```tsx
205
+ const host = useMemo(
206
+ () =>
207
+ observabilityWidgetHost({
208
+ resourceUri: 'ui://mastra/observability-widget',
209
+ onConfigure: ({ widget, datePreset, customRange, filters }) =>
210
+ openWidgetSettings(widget, { datePreset, customRange, filters }),
211
+ }),
212
+ [],
213
+ );
214
+ ```
215
+
216
+ The callback gets the widget as the opening tool returned it, with the date preset (and `customRange` when `custom`) and the filters it is drawn with, like a pin. The button only shows when the host passes `onConfigure` and accepts structured model context. It sits before the pin button and appears on the card's hover or keyboard focus, and always shows on touch screens.
217
+
200
218
  ### Validating stored dashboards
201
219
 
202
220
  `dashboardDefinitionSchema` from `@mastra/core/storage/dashboards` checks a definition's structure and queries, but not what each widget kind needs. A definition can pass it and still show an error in place of a widget. To store only dashboards that draw, check every write with `strictDashboardDefinitionSchema` in the code that stores them:
@@ -17,6 +17,13 @@ export type ObservabilityDatePreset = (typeof OBSERVABILITY_DATE_PRESETS)[number
17
17
  export declare const OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY = "mastra.ai/observability-widget-pinned";
18
18
  /** `structuredContent` key of the model context update the app sends when the user (un)pins. */
19
19
  export declare const OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY = "mastra.ai/observability-widget-pin";
20
+ /**
21
+ * Host context key the host sets to `true` when it can configure a widget. The settings button only
22
+ * shows when the host sends it and supports `updateModelContext.structuredContent`.
23
+ */
24
+ export declare const OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY = "mastra.ai/observability-widget-configurable";
25
+ /** `structuredContent` key of the model context update the app sends when the user opens a widget's settings. */
26
+ export declare const OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY = "mastra.ai/observability-widget-configure";
20
27
  /** Host context key holding what the host's page draws the widgets over (`ObservabilityWidgetView`). */
21
28
  export declare const OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY = "mastra.ai/observability-widget-view";
22
29
  /** A custom window, as ISO date strings. A missing `from` is 24h before `to`; a missing `to` is now. */
@@ -52,4 +59,6 @@ export type ObservabilityWidgetPin = {
52
59
  /** The filters the widget was drawn under (the host's, else the dashboard's `defaults.filters`), when any. */
53
60
  filters?: unknown[];
54
61
  };
62
+ /** What the app sends under `OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY`: the widget as the user sees it. */
63
+ export type ObservabilityWidgetConfigure = Omit<ObservabilityWidgetPin, 'pinned'>;
55
64
  //# sourceMappingURL=tool.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/apps/observability-widget/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAElE,sGAAsG;AACtG,eAAO,MAAM,wBAAwB,uBAAuB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,0BAA0B,YACrC,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,YAAY,CACJ,CAAC;AAEX,MAAM,MAAM,uBAAuB,GAAG,CAAC,OAAO,0BAA0B,CAAC,CAAC,MAAM,CAAC,CAAC;AAElF,mGAAmG;AACnG,eAAO,MAAM,0BAA0B,YAAI,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAA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,GAAG;IACnC,MAAM,EAAE,OAAO,CAAC;IAChB,oEAAoE;IACpE,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IAC/C,UAAU,EAAE,MAAM,CAAC;IACnB,2EAA2E;IAC3E,WAAW,CAAC,EAAE,4BAA4B,CAAC;IAC3C,8GAA8G;IAC9G,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC;CACrB,CAAC;AAEF,6GAA6G;AAC7G,MAAM,MAAM,4BAA4B,GAAG,IAAI,CAAC,sBAAsB,EAAE,QAAQ,CAAC,CAAC"}
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_observability_dashboard = require("./observability-dashboard-B85sO-8E.cjs");
2
+ const require_observability_dashboard = require("./observability-dashboard-D_iAY-Ff.cjs");
3
3
  let _mastra_core_storage_dashboards = require("@mastra/core/storage/dashboards");
4
4
  let zod_v4 = require("zod/v4");
5
5
  // src/dashboards.ts
@@ -1,4 +1,4 @@
1
- import { c as toObservabilityDashboard, d as zodIssues, i as createObservabilityWidgetInputSchema, l as toObservabilityWidget, r as createObservabilityDraftSchemas, s as observabilityWidgetInputSchema, t as OBSERVABILITY_CATALOG_MAX_METRIC_NAMES, u as widgetIssues } from "./observability-dashboard-r5BPo6Aq.js";
1
+ import { c as toObservabilityDashboard, d as zodIssues, i as createObservabilityWidgetInputSchema, l as toObservabilityWidget, r as createObservabilityDraftSchemas, s as observabilityWidgetInputSchema, t as OBSERVABILITY_CATALOG_MAX_METRIC_NAMES, u as widgetIssues } from "./observability-dashboard-BH7Qi5Nh.js";
2
2
  import { dashboardDefinitionSchema, dashboardWidgetSchema } from "@mastra/core/storage/dashboards";
3
3
  import { z } from "zod/v4";
4
4
  // src/dashboards.ts
package/dist/host.cjs CHANGED
@@ -1,27 +1,34 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_tool = require("./tool-B9qpVU16.cjs");
2
+ const require_tool = require("./tool--nuX8qzb.cjs");
3
3
  // src/host.ts
4
4
  const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
5
- /** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */
6
- function readPin(structuredContent) {
7
- const pin = structuredContent?.[require_tool.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
8
- if (!isRecord(pin) || typeof pin.pinned !== "boolean" || typeof pin.datePreset !== "string") return void 0;
9
- if (!isRecord(pin.widget) || typeof pin.widget.id !== "string") return void 0;
10
- if (pin.filters !== void 0 && !Array.isArray(pin.filters)) return void 0;
11
- const range = isRecord(pin.customRange) ? pin.customRange : void 0;
5
+ /** The widget the app sends with a pin or settings click, checked by shape only: the widget itself is validated when it's drawn. */
6
+ function readDrawnWidget(value) {
7
+ if (!isRecord(value) || typeof value.datePreset !== "string") return void 0;
8
+ if (!isRecord(value.widget) || typeof value.widget.id !== "string") return void 0;
9
+ if (value.filters !== void 0 && !Array.isArray(value.filters)) return void 0;
10
+ const range = isRecord(value.customRange) ? value.customRange : void 0;
12
11
  const customRange = range && {
13
12
  ...typeof range.from === "string" ? { from: range.from } : {},
14
13
  ...typeof range.to === "string" ? { to: range.to } : {}
15
14
  };
16
15
  return {
17
- pinned: pin.pinned,
18
16
  widget: {
19
- ...pin.widget,
20
- id: pin.widget.id
17
+ ...value.widget,
18
+ id: value.widget.id
21
19
  },
22
- datePreset: pin.datePreset,
20
+ datePreset: value.datePreset,
23
21
  ...customRange ? { customRange } : {},
24
- ...pin.filters ? { filters: pin.filters } : {}
22
+ ...value.filters ? { filters: value.filters } : {}
23
+ };
24
+ }
25
+ function readPin(structuredContent) {
26
+ const pin = structuredContent?.[require_tool.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
27
+ if (!isRecord(pin) || typeof pin.pinned !== "boolean") return void 0;
28
+ const drawn = readDrawnWidget(pin);
29
+ return drawn && {
30
+ pinned: pin.pinned,
31
+ ...drawn
25
32
  };
26
33
  }
27
34
  const toIso = (date) => date instanceof Date ? date.toISOString() : date;
@@ -47,8 +54,10 @@ const toIso = (date) => date instanceof Date ? date.toISOString() : date;
47
54
  *
48
55
  * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds
49
56
  * until the host's ids next change, so a host that turns a click down leaves it showing until then.
57
+ *
58
+ * Pass `onConfigure` to show a settings button on each widget, e.g. to open the host's own dialog for it.
50
59
  */
51
- function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins }) {
60
+ function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins, onConfigure }) {
52
61
  const from = toIso(customRange?.from);
53
62
  const to = toIso(customRange?.to);
54
63
  const view = {
@@ -63,11 +72,14 @@ function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters
63
72
  return {
64
73
  hostContext: (app) => app.resourceUri === resourceUri ? {
65
74
  [require_tool.OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,
66
- ...pins ? { [require_tool.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}
75
+ ...pins ? { [require_tool.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {},
76
+ ...onConfigure ? { [require_tool.OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY]: true } : {}
67
77
  } : void 0,
68
- onModelContext: pins ? (app, params) => {
78
+ onModelContext: pins || onConfigure ? (app, params) => {
69
79
  if (app.resourceUri !== resourceUri) return;
70
- const pin = readPin(params.structuredContent);
80
+ const configure = onConfigure && readDrawnWidget(params.structuredContent?.["mastra.ai/observability-widget-configure"]);
81
+ if (configure) onConfigure(configure, app);
82
+ const pin = pins && readPin(params.structuredContent);
71
83
  if (!pin) return;
72
84
  if (pin.pinned) {
73
85
  const { pinned: _pinned, ...pinned } = pin;
@@ -83,6 +95,8 @@ function observabilityWidgetPins({ resourceUri, ...pins }) {
83
95
  pins
84
96
  });
85
97
  }
98
+ exports.OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY;
99
+ exports.OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY;
86
100
  exports.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY;
87
101
  exports.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY;
88
102
  exports.OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY;
package/dist/host.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"host.cjs","names":["OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY","OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY","OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY"],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type {\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type {\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\n/** The MCP App instance a host callback is about: one tool call's app. */\nexport type McpAppRef = { toolName: string; toolCallId: string; resourceUri: string };\n\n/**\n * What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed\n * here by shape, so this package doesn't depend on the host's UI package.\n */\nexport type McpAppHostValue = {\n hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;\n onModelContext?: (\n app: McpAppRef,\n params: { content?: unknown[]; structuredContent?: Record<string, unknown> },\n ) => void;\n};\n\nexport type ObservabilityWidgetPins = {\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /**\n * The user pinned `pin.widget` over `pin.datePreset` (and `pin.customRange` when `custom`), under\n * `pin.filters` when it has them.\n */\n onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;\n /** The user unpinned the widget with this id. */\n onUnpin: (widgetId: string, app: McpAppRef) => void;\n};\n\nexport type ObservabilityWidgetHostOptions = Omit<ObservabilityWidgetView, 'customRange'> & {\n /** The window drawn when `datePreset` is `custom`: the Metrics page's `customRange` as is, or ISO strings. */\n customRange?: { from?: Date | string; to?: Date | string };\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** Lets the user pin widgets. Left out, the widgets show no pin button. */\n pins?: ObservabilityWidgetPins;\n};\n\nexport type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & { resourceUri: string };\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */\nfunction readPin(structuredContent: Record<string, unknown> | undefined): ObservabilityWidgetPin | undefined {\n const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];\n if (!isRecord(pin) || typeof pin.pinned !== 'boolean' || typeof pin.datePreset !== 'string') return undefined;\n if (!isRecord(pin.widget) || typeof pin.widget.id !== 'string') return undefined;\n if (pin.filters !== undefined && !Array.isArray(pin.filters)) return undefined;\n const range = isRecord(pin.customRange) ? pin.customRange : undefined;\n const customRange: ObservabilityWidgetDateRange | undefined = range && {\n ...(typeof range.from === 'string' ? { from: range.from } : {}),\n ...(typeof range.to === 'string' ? { to: range.to } : {}),\n };\n return {\n pinned: pin.pinned,\n widget: { ...pin.widget, id: pin.widget.id },\n datePreset: pin.datePreset,\n ...(customRange ? { customRange } : {}),\n ...(pin.filters ? { filters: pin.filters } : {}),\n };\n}\n\nconst toIso = (date: Date | string | undefined) => (date instanceof Date ? date.toISOString() : date);\n\n/**\n * What the host passes down to the observability widget app: the host page's `datePreset` (with its\n * `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every\n * `queryObservability` call. The app redraws when any of them changes.\n *\n * ```tsx\n * const host = useMemo(\n * () =>\n * observabilityWidgetHost({\n * resourceUri: 'ui://mastra/observability-widget',\n * datePreset,\n * customRange,\n * filters,\n * projectId,\n * }),\n * [datePreset, customRange, filters, projectId],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds\n * until the host's ids next change, so a host that turns a click down leaves it showing until then.\n */\nexport function observabilityWidgetHost({\n resourceUri,\n datePreset,\n customRange,\n filters,\n projectId,\n pins,\n}: ObservabilityWidgetHostOptions): McpAppHostValue {\n const from = toIso(customRange?.from);\n const to = toIso(customRange?.to);\n // Always sent, even empty, so a field the host stops setting is cleared in the app too.\n const view: ObservabilityWidgetView = {\n ...(datePreset ? { datePreset } : {}),\n ...(datePreset === 'custom' && customRange\n ? { customRange: { ...(from ? { from } : {}), ...(to ? { to } : {}) } }\n : {}),\n ...(filters ? { filters } : {}),\n ...(projectId ? { projectId } : {}),\n };\n return {\n hostContext: app =>\n app.resourceUri === resourceUri\n ? {\n [OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,\n ...(pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}),\n }\n : undefined,\n onModelContext: pins\n ? (app, params) => {\n if (app.resourceUri !== resourceUri) return;\n const pin = readPin(params.structuredContent);\n if (!pin) return;\n if (pin.pinned) {\n const { pinned: _pinned, ...pinned } = pin;\n pins.onPin(pinned, app);\n } else pins.onUnpin(pin.widget.id, app);\n }\n : undefined,\n };\n}\n\n/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */\nexport function observabilityWidgetPins({ resourceUri, ...pins }: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return observabilityWidgetHost({ resourceUri, pins });\n}\n"],"mappings":";;;AA4DA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,SAAS,QAAQ,mBAA4F;CAC3G,MAAM,MAAM,oBAAoBA,aAAAA;CAChC,IAAI,CAAC,SAAS,GAAG,KAAK,OAAO,IAAI,WAAW,aAAa,OAAO,IAAI,eAAe,UAAU,OAAO,KAAA;CACpG,IAAI,CAAC,SAAS,IAAI,MAAM,KAAK,OAAO,IAAI,OAAO,OAAO,UAAU,OAAO,KAAA;CACvE,IAAI,IAAI,YAAY,KAAA,KAAa,CAAC,MAAM,QAAQ,IAAI,OAAO,GAAG,OAAO,KAAA;CACrE,MAAM,QAAQ,SAAS,IAAI,WAAW,IAAI,IAAI,cAAc,KAAA;CAC5D,MAAM,cAAwD,SAAS;EACrE,GAAI,OAAO,MAAM,SAAS,WAAW,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC7D,GAAI,OAAO,MAAM,OAAO,WAAW,EAAE,IAAI,MAAM,GAAG,IAAI,CAAC;CACzD;CACA,OAAO;EACL,QAAQ,IAAI;EACZ,QAAQ;GAAE,GAAG,IAAI;GAAQ,IAAI,IAAI,OAAO;EAAG;EAC3C,YAAY,IAAI;EAChB,GAAI,cAAc,EAAE,YAAY,IAAI,CAAC;EACrC,GAAI,IAAI,UAAU,EAAE,SAAS,IAAI,QAAQ,IAAI,CAAC;CAChD;AACF;AAEA,MAA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GURABLE_CONTEXT_KEY"],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type {\n ObservabilityWidgetConfigure,\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type {\n ObservabilityWidgetConfigure,\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\n/** The MCP App instance a host callback is about: one tool call's app. */\nexport type McpAppRef = { toolName: string; toolCallId: string; resourceUri: string };\n\n/**\n * What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed\n * here by shape, so this package doesn't depend on the host's UI package.\n */\nexport type McpAppHostValue = {\n hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;\n onModelContext?: (\n app: McpAppRef,\n params: { content?: unknown[]; structuredContent?: Record<string, unknown> },\n ) => void;\n};\n\nexport type ObservabilityWidgetPins = {\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /**\n * The user pinned `pin.widget` over `pin.datePreset` (and `pin.customRange` when `custom`), under\n * `pin.filters` when it has them.\n */\n onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;\n /** The user unpinned the widget with this id. */\n onUnpin: (widgetId: string, app: McpAppRef) => void;\n};\n\nexport type ObservabilityWidgetHostOptions = Omit<ObservabilityWidgetView, 'customRange'> & {\n /** The window drawn when `datePreset` is `custom`: the Metrics page's `customRange` as is, or ISO strings. */\n customRange?: { from?: Date | string; to?: Date | string };\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** Lets the user pin widgets. Left out, the widgets show no pin button. */\n pins?: ObservabilityWidgetPins;\n /**\n * The user clicked the settings button of `configure.widget`, drawn over `configure.datePreset` (and\n * `configure.customRange` when `custom`) under `configure.filters` when it has them. Left out, the\n * widgets show no settings button.\n */\n onConfigure?: (configure: ObservabilityWidgetConfigure, app: McpAppRef) => void;\n};\n\nexport type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & { resourceUri: string };\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** The widget the app sends with a pin or settings click, checked by shape only: the widget itself is validated when it's drawn. */\nfunction readDrawnWidget(value: unknown): ObservabilityWidgetConfigure | undefined {\n if (!isRecord(value) || typeof value.datePreset !== 'string') return undefined;\n if (!isRecord(value.widget) || typeof value.widget.id !== 'string') return undefined;\n if (value.filters !== undefined && !Array.isArray(value.filters)) return undefined;\n const range = isRecord(value.customRange) ? value.customRange : undefined;\n const customRange: ObservabilityWidgetDateRange | undefined = range && {\n ...(typeof range.from === 'string' ? { from: range.from } : {}),\n ...(typeof range.to === 'string' ? { to: range.to } : {}),\n };\n return {\n widget: { ...value.widget, id: value.widget.id },\n datePreset: value.datePreset,\n ...(customRange ? { customRange } : {}),\n ...(value.filters ? { filters: value.filters } : {}),\n };\n}\n\nfunction readPin(structuredContent: Record<string, unknown> | undefined): ObservabilityWidgetPin | undefined {\n const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];\n if (!isRecord(pin) || typeof pin.pinned !== 'boolean') return undefined;\n const drawn = readDrawnWidget(pin);\n return drawn && { pinned: pin.pinned, ...drawn };\n}\n\nconst toIso = (date: Date | string | undefined) => (date instanceof Date ? date.toISOString() : date);\n\n/**\n * What the host passes down to the observability widget app: the host page's `datePreset` (with its\n * `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every\n * `queryObservability` call. The app redraws when any of them changes.\n *\n * ```tsx\n * const host = useMemo(\n * () =>\n * observabilityWidgetHost({\n * resourceUri: 'ui://mastra/observability-widget',\n * datePreset,\n * customRange,\n * filters,\n * projectId,\n * }),\n * [datePreset, customRange, filters, projectId],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds\n * until the host's ids next change, so a host that turns a click down leaves it showing until then.\n *\n * Pass `onConfigure` to show a settings button on each widget, e.g. to open the host's own dialog for it.\n */\nexport function observabilityWidgetHost({\n resourceUri,\n datePreset,\n customRange,\n filters,\n projectId,\n pins,\n onConfigure,\n}: ObservabilityWidgetHostOptions): McpAppHostValue {\n const from = toIso(customRange?.from);\n const to = toIso(customRange?.to);\n // Always sent, even empty, so a field the host stops setting is cleared in the app too.\n const view: ObservabilityWidgetView = {\n ...(datePreset ? { datePreset } : {}),\n ...(datePreset === 'custom' && customRange\n ? { customRange: { ...(from ? { from } : {}), ...(to ? { to } : {}) } }\n : {}),\n ...(filters ? { filters } : {}),\n ...(projectId ? { projectId } : {}),\n };\n return {\n hostContext: app =>\n app.resourceUri === resourceUri\n ? {\n [OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,\n ...(pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}),\n ...(onConfigure ? { [OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY]: true } : {}),\n }\n : undefined,\n onModelContext:\n pins || onConfigure\n ? (app, params) => {\n if (app.resourceUri !== resourceUri) return;\n const configure =\n onConfigure && readDrawnWidget(params.structuredContent?.[OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY]);\n if (configure) onConfigure(configure, app);\n const pin = pins && readPin(params.structuredContent);\n if (!pin) return;\n if (pin.pinned) {\n const { pinned: _pinned, ...pinned } = pin;\n pins.onPin(pinned, app);\n } else pins.onUnpin(pin.widget.id, app);\n }\n : undefined,\n };\n}\n\n/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */\nexport function observabilityWidgetPins({ resourceUri, ...pins }: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return observabilityWidgetHost({ resourceUri, pins });\n}\n"],"mappings":";;;AAwEA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,SAAS,gBAAgB,OAA0D;CACjF,IAAI,CAAC,SAAS,KAAK,KAAK,OAAO,MAAM,eAAe,UAAU,OAAO,KAAA;CACrE,IAAI,CAAC,SAAS,MAAM,MAAM,KAAK,OAAO,MAAM,OAAO,OAAO,UAAU,OAAO,KAAA;CAC3E,IAAI,MAAM,YAAY,KAAA,KAAa,CAAC,MAAM,QAAQ,MAAM,OAAO,GAAG,OAAO,KAAA;CACzE,MAAM,QAAQ,SAAS,MAAM,WAAW,IAAI,MAAM,cAAc,KAAA;CAChE,MAAM,cAAwD,SAAS;EACrE,GAAI,OAAO,MAAM,SAAS,WAAW,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC7D,GAAI,OAAO,MAAM,OAAO,WAAW,EAAE,IAAI,MAAM,GAAG,IAAI,CAAC;CACzD;CACA,OAAO;EACL,QAAQ;GAAE,GAAG,MAAM;GAAQ,IAAI,MAAM,OAAO;EAAG;EAC/C,YAAY,MAAM;EAClB,GAAI,cAAc,EAAE,YAAY,IAAI,CAAC;EACrC,GAAI,MAAM,UAAU,EAAE,SAAS,MAAM,QAAQ,IAAI,CAAC;CACpD;AACF;AAEA,SAAS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QAAQ,SAAS,GAAG,WAAW;IACvC,KAAK,MAAM,QAAQ,GAAG;GACxB,OAAO,KAAK,QAAQ,IAAI,OAAO,IAAI,GAAG;EACxC,IACA,KAAA;CACR;AACF;;AAGA,SAAgB,wBAAwB,EAAE,aAAa,GAAG,QAAyD;CACjH,OAAO,wBAAwB;EAAE;EAAa;CAAK,CAAC;AACtD"}
package/dist/host.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { ObservabilityWidgetPin, ObservabilityWidgetView } from './apps/observability-widget/tool.js';
2
- export { OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY, OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY, } from './apps/observability-widget/tool.js';
3
- export type { ObservabilityWidgetDateRange, ObservabilityWidgetPin, ObservabilityWidgetView, } from './apps/observability-widget/tool.js';
1
+ import type { ObservabilityWidgetConfigure, ObservabilityWidgetPin, ObservabilityWidgetView } from './apps/observability-widget/tool.js';
2
+ export { OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY, OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY, OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY, OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY, } from './apps/observability-widget/tool.js';
3
+ export type { ObservabilityWidgetConfigure, ObservabilityWidgetDateRange, ObservabilityWidgetPin, ObservabilityWidgetView, } from './apps/observability-widget/tool.js';
4
4
  /** The MCP App instance a host callback is about: one tool call's app. */
5
5
  export type McpAppRef = {
6
6
  toolName: string;
@@ -39,6 +39,12 @@ export type ObservabilityWidgetHostOptions = Omit<ObservabilityWidgetView, 'cust
39
39
  resourceUri: string;
40
40
  /** Lets the user pin widgets. Left out, the widgets show no pin button. */
41
41
  pins?: ObservabilityWidgetPins;
42
+ /**
43
+ * The user clicked the settings button of `configure.widget`, drawn over `configure.datePreset` (and
44
+ * `configure.customRange` when `custom`) under `configure.filters` when it has them. Left out, the
45
+ * widgets show no settings button.
46
+ */
47
+ onConfigure?: (configure: ObservabilityWidgetConfigure, app: McpAppRef) => void;
42
48
  };
43
49
  export type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & {
44
50
  resourceUri: string;
@@ -65,8 +71,10 @@ export type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & {
65
71
  *
66
72
  * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds
67
73
  * until the host's ids next change, so a host that turns a click down leaves it showing until then.
74
+ *
75
+ * Pass `onConfigure` to show a settings button on each widget, e.g. to open the host's own dialog for it.
68
76
  */
69
- export declare function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins, }: ObservabilityWidgetHostOptions): McpAppHostValue;
77
+ export declare function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins, onConfigure, }: ObservabilityWidgetHostOptions): McpAppHostValue;
70
78
  /** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */
71
79
  export declare function observabilityWidgetPins({ resourceUri, ...pins }: ObservabilityWidgetPinsOptions): McpAppHostValue;
72
80
  //# sourceMappingURL=host.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAEV,sBAAsB,EACtB,uBAAuB,EACxB,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,oCAAoC,EACpC,uCAAuC,EACvC,qCAAqC,GACtC,MAAM,kCAAkC,CAAC;AAC1C,YAAY,EACV,4BAA4B,EAC5B,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,kCAAkC,CAAC;AAE1C,0EAA0E;AAC1E,MAAM,MAAM,SAAS,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtF;;;GAG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AAG,EAAE,SAAS,KAAK,IAAI,CAAC;IAC7E,iDAAiD;IACjD,OAAO,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,KAAK,IAAI,CAAC;CACrD,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG,IAAI,CAAC,uBAAuB,EAAE,aAAa,CAAC,GAAG;IAC1F,8GAA8G;IAC9G,WAAW,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,IAAI,GAAG,MAAM,CAAC;QAAC,EAAE,CAAC,EAAE,IAAI,GAAG,MAAM,CAAA;KAAE,CAAC;IAC3D,mGAAmG;IACnG,WAAW,EAAE,MAAM,CAAC;IACpB,2EAA2E;IAC3E,IAAI,CAAC,EAAE,uBAAuB,CAAC;IAC/B;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,SAAS,EAAE,4BAA4B,EAAE,GAAG,EAAE,SAAS,KAAK,IAAI,CAAC;CACjF,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG,uBAAuB,GAAG;IAAE,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC;AAgC/F;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,uBAAuB,CAAC,EACtC,WAAW,EACX,UAAU,EACV,WAAW,EACX,OAAO,EACP,SAAS,EACT,IAAI,EACJ,WAAW,GACZ,EAAE,8BAA8B,GAAG,eAAe,CAqClD;AAED,yGAAyG;AACzG,wBAAgB,uBAAuB,CAAC,EAAE,WAAW,EAAE,GAAG,IAAI,EAAE,EAAE,8BAA8B,GAAG,eAAe,CAEjH"}
package/dist/host.js CHANGED
@@ -1,26 +1,33 @@
1
- import { a as OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY, i as OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, r as OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY } from "./tool-HqUC6iks.js";
1
+ import { a as OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY, i as OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY, o as OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, r as OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY, s as OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY } from "./tool-CKJgZfHk.js";
2
2
  // src/host.ts
3
3
  const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4
- /** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */
5
- function readPin(structuredContent) {
6
- const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
7
- if (!isRecord(pin) || typeof pin.pinned !== "boolean" || typeof pin.datePreset !== "string") return void 0;
8
- if (!isRecord(pin.widget) || typeof pin.widget.id !== "string") return void 0;
9
- if (pin.filters !== void 0 && !Array.isArray(pin.filters)) return void 0;
10
- const range = isRecord(pin.customRange) ? pin.customRange : void 0;
4
+ /** The widget the app sends with a pin or settings click, checked by shape only: the widget itself is validated when it's drawn. */
5
+ function readDrawnWidget(value) {
6
+ if (!isRecord(value) || typeof value.datePreset !== "string") return void 0;
7
+ if (!isRecord(value.widget) || typeof value.widget.id !== "string") return void 0;
8
+ if (value.filters !== void 0 && !Array.isArray(value.filters)) return void 0;
9
+ const range = isRecord(value.customRange) ? value.customRange : void 0;
11
10
  const customRange = range && {
12
11
  ...typeof range.from === "string" ? { from: range.from } : {},
13
12
  ...typeof range.to === "string" ? { to: range.to } : {}
14
13
  };
15
14
  return {
16
- pinned: pin.pinned,
17
15
  widget: {
18
- ...pin.widget,
19
- id: pin.widget.id
16
+ ...value.widget,
17
+ id: value.widget.id
20
18
  },
21
- datePreset: pin.datePreset,
19
+ datePreset: value.datePreset,
22
20
  ...customRange ? { customRange } : {},
23
- ...pin.filters ? { filters: pin.filters } : {}
21
+ ...value.filters ? { filters: value.filters } : {}
22
+ };
23
+ }
24
+ function readPin(structuredContent) {
25
+ const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
26
+ if (!isRecord(pin) || typeof pin.pinned !== "boolean") return void 0;
27
+ const drawn = readDrawnWidget(pin);
28
+ return drawn && {
29
+ pinned: pin.pinned,
30
+ ...drawn
24
31
  };
25
32
  }
26
33
  const toIso = (date) => date instanceof Date ? date.toISOString() : date;
@@ -46,8 +53,10 @@ const toIso = (date) => date instanceof Date ? date.toISOString() : date;
46
53
  *
47
54
  * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds
48
55
  * until the host's ids next change, so a host that turns a click down leaves it showing until then.
56
+ *
57
+ * Pass `onConfigure` to show a settings button on each widget, e.g. to open the host's own dialog for it.
49
58
  */
50
- function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins }) {
59
+ function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins, onConfigure }) {
51
60
  const from = toIso(customRange?.from);
52
61
  const to = toIso(customRange?.to);
53
62
  const view = {
@@ -62,11 +71,14 @@ function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters
62
71
  return {
63
72
  hostContext: (app) => app.resourceUri === resourceUri ? {
64
73
  [OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,
65
- ...pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}
74
+ ...pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {},
75
+ ...onConfigure ? { [OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY]: true } : {}
66
76
  } : void 0,
67
- onModelContext: pins ? (app, params) => {
77
+ onModelContext: pins || onConfigure ? (app, params) => {
68
78
  if (app.resourceUri !== resourceUri) return;
69
- const pin = readPin(params.structuredContent);
79
+ const configure = onConfigure && readDrawnWidget(params.structuredContent?.["mastra.ai/observability-widget-configure"]);
80
+ if (configure) onConfigure(configure, app);
81
+ const pin = pins && readPin(params.structuredContent);
70
82
  if (!pin) return;
71
83
  if (pin.pinned) {
72
84
  const { pinned: _pinned, ...pinned } = pin;
@@ -82,6 +94,6 @@ function observabilityWidgetPins({ resourceUri, ...pins }) {
82
94
  pins
83
95
  });
84
96
  }
85
- export { OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY, OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY, observabilityWidgetHost, observabilityWidgetPins };
97
+ export { OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY, OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY, OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY, OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY, observabilityWidgetHost, observabilityWidgetPins };
86
98
 
87
99
  //# sourceMappingURL=host.js.map
package/dist/host.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"host.js","names":[],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type {\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type {\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\n/** The MCP App instance a host callback is about: one tool call's app. */\nexport type McpAppRef = { toolName: string; toolCallId: string; resourceUri: string };\n\n/**\n * What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed\n * here by shape, so this package doesn't depend on the host's UI package.\n */\nexport type McpAppHostValue = {\n hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;\n onModelContext?: (\n app: McpAppRef,\n params: { content?: unknown[]; structuredContent?: Record<string, unknown> },\n ) => void;\n};\n\nexport type ObservabilityWidgetPins = {\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /**\n * The user pinned `pin.widget` over `pin.datePreset` (and `pin.customRange` when `custom`), under\n * `pin.filters` when it has them.\n */\n onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;\n /** The user unpinned the widget with this id. */\n onUnpin: (widgetId: string, app: McpAppRef) => void;\n};\n\nexport type ObservabilityWidgetHostOptions = Omit<ObservabilityWidgetView, 'customRange'> & {\n /** The window drawn when `datePreset` is `custom`: the Metrics page's `customRange` as is, or ISO strings. */\n customRange?: { from?: Date | string; to?: Date | string };\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** Lets the user pin widgets. Left out, the widgets show no pin button. */\n pins?: ObservabilityWidgetPins;\n};\n\nexport type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & { resourceUri: string };\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */\nfunction readPin(structuredContent: Record<string, unknown> | undefined): ObservabilityWidgetPin | undefined {\n const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];\n if (!isRecord(pin) || typeof pin.pinned !== 'boolean' || typeof pin.datePreset !== 'string') return undefined;\n if (!isRecord(pin.widget) || typeof pin.widget.id !== 'string') return undefined;\n if (pin.filters !== undefined && !Array.isArray(pin.filters)) return undefined;\n const range = isRecord(pin.customRange) ? pin.customRange : undefined;\n const customRange: ObservabilityWidgetDateRange | undefined = range && {\n ...(typeof range.from === 'string' ? { from: range.from } : {}),\n ...(typeof range.to === 'string' ? { to: range.to } : {}),\n };\n return {\n pinned: pin.pinned,\n widget: { ...pin.widget, id: pin.widget.id },\n datePreset: pin.datePreset,\n ...(customRange ? { customRange } : {}),\n ...(pin.filters ? { filters: pin.filters } : {}),\n };\n}\n\nconst toIso = (date: Date | string | undefined) => (date instanceof Date ? date.toISOString() : date);\n\n/**\n * What the host passes down to the observability widget app: the host page's `datePreset` (with its\n * `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every\n * `queryObservability` call. The app redraws when any of them changes.\n *\n * ```tsx\n * const host = useMemo(\n * () =>\n * observabilityWidgetHost({\n * resourceUri: 'ui://mastra/observability-widget',\n * datePreset,\n * customRange,\n * filters,\n * projectId,\n * }),\n * [datePreset, customRange, filters, projectId],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds\n * until the host's ids next change, so a host that turns a click down leaves it showing until then.\n */\nexport function observabilityWidgetHost({\n resourceUri,\n datePreset,\n customRange,\n filters,\n projectId,\n pins,\n}: ObservabilityWidgetHostOptions): McpAppHostValue {\n const from = toIso(customRange?.from);\n const to = toIso(customRange?.to);\n // Always sent, even empty, so a field the host stops setting is cleared in the app too.\n const view: ObservabilityWidgetView = {\n ...(datePreset ? { datePreset } : {}),\n ...(datePreset === 'custom' && customRange\n ? { customRange: { ...(from ? { from } : {}), ...(to ? { to } : {}) } }\n : {}),\n ...(filters ? { filters } : {}),\n ...(projectId ? { projectId } : {}),\n };\n return {\n hostContext: app =>\n app.resourceUri === resourceUri\n ? {\n [OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,\n ...(pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}),\n }\n : undefined,\n onModelContext: pins\n ? (app, params) => {\n if (app.resourceUri !== resourceUri) return;\n const pin = readPin(params.structuredContent);\n if (!pin) return;\n if (pin.pinned) {\n const { pinned: _pinned, ...pinned } = pin;\n pins.onPin(pinned, app);\n } else pins.onUnpin(pin.widget.id, app);\n }\n : undefined,\n };\n}\n\n/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */\nexport function observabilityWidgetPins({ resourceUri, ...pins }: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return observabilityWidgetHost({ resourceUri, pins });\n}\n"],"mappings":";;AA4DA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,SAAS,QAAQ,mBAA4F;CAC3G,MAAM,MAAM,oBAAoB;CAChC,IAAI,CAAC,SAAS,GAAG,KAAK,OAAO,IAAI,WAAW,aAAa,OAAO,IAAI,eAAe,UAAU,OAAO,KAAA;CACpG,IAAI,CAAC,SAAS,IAAI,MAAM,KAAK,OAAO,IAAI,OAAO,OAAO,UAAU,OAAO,KAAA;CACvE,IAAI,IAAI,YAAY,KAAA,KAAa,CAAC,MAAM,QAAQ,IAAI,OAAO,GAAG,OAAO,KAAA;CACrE,MAAM,QAAQ,SAAS,IAAI,WAAW,IAAI,IAAI,cAAc,KAAA;CAC5D,MAAM,cAAwD,SAAS;EACrE,GAAI,OAAO,MAAM,SAAS,WAAW,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC7D,GAAI,OAAO,MAAM,OAAO,WAAW,EAAE,IAAI,MAAM,GAAG,IAAI,CAAC;CACzD;CACA,OAAO;EACL,QAAQ,IAAI;EACZ,QAAQ;GAAE,GAAG,IAAI;GAAQ,IAAI,IAAI,OAAO;EAAG;EAC3C,YAAY,IAAI;EAChB,GAAI,cAAc,EAAE,YAAY,IAAI,CAAC;EACrC,GAAI,IAAI,UAAU,EAAE,SAAS,IAAI,QAAQ,IAAI,CAAC;CAChD;AACF;AAEA,MAAM,SAAS,SAAqC,gBAAgB,OAAO,KAAK,YAAY,IAAI;;;;;;;;;;;;;;;;;;;;;;;;AAyBhG,SAAgB,wBAAwB,EACtC,aACA,YACA,aACA,SACA,WACA,QACkD;CAClD,MAAM,OAAO,MAAM,aAAa,IAAI;CACpC,MAAM,KAAK,MAAM,aAAa,EAAE;CAEhC,MAAM,OAAgC;EACpC,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;EACnC,GAAI,eAAe,YAAY,cAC3B,EAAE,aAAa;GAAE,GAAI,OAAO,EAAE,KAAK,IAAI,CAAC;GAAI,GAAI,KAAK,EAAE,GAAG,IAAI,CAAC;EAAG,EAAE,IACpE,CAAC;EACL,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;EAC7B,GAAI,YAAY,EAAE,UAAU,IAAI,CAAC;CACnC;CACA,OAAO;EACL,cAAa,QACX,IAAI,gBAAgB,cAChB;IACG,wCAAwC;GACzC,GAAI,OAAO,GAAG,0CAA0C,KAAK,UAAU,IAAI,CAAC;EAC9E,IACA,KAAA;EACN,gBAAgB,QACX,KAAK,WAAW;GACf,IAAI,IAAI,gBAAgB,aAAa;GACrC,MAAM,MAAM,QAAQ,OAAO,iBAAiB;GAC5C,IAAI,CAAC,KAAK;GACV,IAAI,IAAI,QAAQ;IACd,MAAM,EAAE,QAAQ,SAAS,GAAG,WAAW;IACvC,KAAK,MAAM,QAAQ,GAAG;GACxB,OAAO,KAAK,QAAQ,IAAI,OAAO,IAAI,GAAG;EACxC,IACA,KAAA;CACN;AACF;;AAGA,SAAgB,wBAAwB,EAAE,aAAa,GAAG,QAAyD;CACjH,OAAO,wBAAwB;EAAE;EAAa;CAAK,CAAC;AACtD"}
1
+ {"version":3,"file":"host.js","names":[],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type {\n ObservabilityWidgetConfigure,\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type {\n ObservabilityWidgetConfigure,\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\n/** The MCP App instance a host callback is about: one tool call's app. */\nexport type McpAppRef = { toolName: string; toolCallId: string; resourceUri: string };\n\n/**\n * What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed\n * here by shape, so this package doesn't depend on the host's UI package.\n */\nexport type McpAppHostValue = {\n hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;\n onModelContext?: (\n app: McpAppRef,\n params: { content?: unknown[]; structuredContent?: Record<string, unknown> },\n ) => void;\n};\n\nexport type ObservabilityWidgetPins = {\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /**\n * The user pinned `pin.widget` over `pin.datePreset` (and `pin.customRange` when `custom`), under\n * `pin.filters` when it has them.\n */\n onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;\n /** The user unpinned the widget with this id. */\n onUnpin: (widgetId: string, app: McpAppRef) => void;\n};\n\nexport type ObservabilityWidgetHostOptions = Omit<ObservabilityWidgetView, 'customRange'> & {\n /** The window drawn when `datePreset` is `custom`: the Metrics page's `customRange` as is, or ISO strings. */\n customRange?: { from?: Date | string; to?: Date | string };\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** Lets the user pin widgets. Left out, the widgets show no pin button. */\n pins?: ObservabilityWidgetPins;\n /**\n * The user clicked the settings button of `configure.widget`, drawn over `configure.datePreset` (and\n * `configure.customRange` when `custom`) under `configure.filters` when it has them. Left out, the\n * widgets show no settings button.\n */\n onConfigure?: (configure: ObservabilityWidgetConfigure, app: McpAppRef) => void;\n};\n\nexport type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & { resourceUri: string };\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** The widget the app sends with a pin or settings click, checked by shape only: the widget itself is validated when it's drawn. */\nfunction readDrawnWidget(value: unknown): ObservabilityWidgetConfigure | undefined {\n if (!isRecord(value) || typeof value.datePreset !== 'string') return undefined;\n if (!isRecord(value.widget) || typeof value.widget.id !== 'string') return undefined;\n if (value.filters !== undefined && !Array.isArray(value.filters)) return undefined;\n const range = isRecord(value.customRange) ? value.customRange : undefined;\n const customRange: ObservabilityWidgetDateRange | undefined = range && {\n ...(typeof range.from === 'string' ? { from: range.from } : {}),\n ...(typeof range.to === 'string' ? { to: range.to } : {}),\n };\n return {\n widget: { ...value.widget, id: value.widget.id },\n datePreset: value.datePreset,\n ...(customRange ? { customRange } : {}),\n ...(value.filters ? { filters: value.filters } : {}),\n };\n}\n\nfunction readPin(structuredContent: Record<string, unknown> | undefined): ObservabilityWidgetPin | undefined {\n const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];\n if (!isRecord(pin) || typeof pin.pinned !== 'boolean') return undefined;\n const drawn = readDrawnWidget(pin);\n return drawn && { pinned: pin.pinned, ...drawn };\n}\n\nconst toIso = (date: Date | string | undefined) => (date instanceof Date ? date.toISOString() : date);\n\n/**\n * What the host passes down to the observability widget app: the host page's `datePreset` (with its\n * `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every\n * `queryObservability` call. The app redraws when any of them changes.\n *\n * ```tsx\n * const host = useMemo(\n * () =>\n * observabilityWidgetHost({\n * resourceUri: 'ui://mastra/observability-widget',\n * datePreset,\n * customRange,\n * filters,\n * projectId,\n * }),\n * [datePreset, customRange, filters, projectId],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds\n * until the host's ids next change, so a host that turns a click down leaves it showing until then.\n *\n * Pass `onConfigure` to show a settings button on each widget, e.g. to open the host's own dialog for it.\n */\nexport function observabilityWidgetHost({\n resourceUri,\n datePreset,\n customRange,\n filters,\n projectId,\n pins,\n onConfigure,\n}: ObservabilityWidgetHostOptions): McpAppHostValue {\n const from = toIso(customRange?.from);\n const to = toIso(customRange?.to);\n // Always sent, even empty, so a field the host stops setting is cleared in the app too.\n const view: ObservabilityWidgetView = {\n ...(datePreset ? { datePreset } : {}),\n ...(datePreset === 'custom' && customRange\n ? { customRange: { ...(from ? { from } : {}), ...(to ? { to } : {}) } }\n : {}),\n ...(filters ? { filters } : {}),\n ...(projectId ? { projectId } : {}),\n };\n return {\n hostContext: app =>\n app.resourceUri === resourceUri\n ? {\n [OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,\n ...(pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}),\n ...(onConfigure ? { [OBSERVABILITY_WIDGET_CONFIGURABLE_CONTEXT_KEY]: true } : {}),\n }\n : undefined,\n onModelContext:\n pins || onConfigure\n ? (app, params) => {\n if (app.resourceUri !== resourceUri) return;\n const configure =\n onConfigure && readDrawnWidget(params.structuredContent?.[OBSERVABILITY_WIDGET_CONFIGURE_CONTEXT_KEY]);\n if (configure) onConfigure(configure, app);\n const pin = pins && readPin(params.structuredContent);\n if (!pin) return;\n if (pin.pinned) {\n const { pinned: _pinned, ...pinned } = pin;\n pins.onPin(pinned, app);\n } else pins.onUnpin(pin.widget.id, app);\n }\n : undefined,\n };\n}\n\n/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */\nexport function observabilityWidgetPins({ resourceUri, ...pins }: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return observabilityWidgetHost({ resourceUri, pins });\n}\n"],"mappings":";;AAwEA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,SAAS,gBAAgB,OAA0D;CACjF,IAAI,CAAC,SAAS,KAAK,KAAK,OAAO,MAAM,eAAe,UAAU,OAAO,KAAA;CACrE,IAAI,CAAC,SAAS,MAAM,MAAM,KAAK,OAAO,MAAM,OAAO,OAAO,UAAU,OAAO,KAAA;CAC3E,IAAI,MAAM,YAAY,KAAA,KAAa,CAAC,MAAM,QAAQ,MAAM,OAAO,GAAG,OAAO,KAAA;CACzE,MAAM,QAAQ,SAAS,MAAM,WAAW,IAAI,MAAM,cAAc,KAAA;CAChE,MAAM,cAAwD,SAAS;EACrE,GAAI,OAAO,MAAM,SAAS,WAAW,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC7D,GAAI,OAAO,MAAM,OAAO,WAAW,EAAE,IAAI,MAAM,GAAG,IAAI,CAAC;CACzD;CACA,OAAO;EACL,QAAQ;GAAE,GAAG,MAAM;GAAQ,IAAI,MAAM,OAAO;EAAG;EAC/C,YAAY,MAAM;EAClB,GAAI,cAAc,EAAE,YAAY,IAAI,CAAC;EACrC,GAAI,MAAM,UAAU,EAAE,SAAS,MAAM,QAAQ,IAAI,CAAC;CACpD;AACF;AAEA,SAAS,QAAQ,mBAA4F;CAC3G,MAAM,MAAM,oBAAoB;CAChC,IAAI,CAAC,SAAS,GAAG,KAAK,OAAO,IAAI,WAAW,WAAW,OAAO,KAAA;CAC9D,MAAM,QAAQ,gBAAgB,GAAG;CACjC,OAAO,SAAS;EAAE,QAAQ,IAAI;EAAQ,GAAG;CAAM;AACjD;AAEA,MAAM,SAAS,SAAqC,gBAAgB,OAAO,KAAK,YAAY,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;AA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