@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,192 @@
1
+ /**
2
+ * Chart planner + chart cache.
3
+ *
4
+ * Self-contained chart subsystem with two layers:
5
+ *
6
+ * 1. Inner planner agent (private). Pure dataset-in /
7
+ * `EChartsOption`-out brain. Driven by {@link prepareChart};
8
+ * callers never instantiate it directly.
9
+ * 2. {@link prepareChart}: orchestration on top of the planner.
10
+ * Mints a `chartId`, caches an empty `{ chartId }` record
11
+ * synchronously, then resolves the dataset and runs the
12
+ * planner in the background. The terminal entry settles with
13
+ * either `result` (success) or `error` (failure). Both
14
+ * undefined means the entry is still processing.
15
+ *
16
+ * The cache surface ({@link fetchChart}) is the only state the
17
+ * HTTP route and the chart-producing tools share. `prepareChart`
18
+ * is dataset-agnostic - callers supply a `resolveData` callback
19
+ * that fetches the rows however they like (Genie statement, inline
20
+ * dataset, custom API). The module has no knowledge of Genie or
21
+ * statement ids; those concerns live in the tools that wrap it.
22
+ *
23
+ * Wire-format schemas live in `@dbx-tools/shared-mastra` so
24
+ * the demo client and any other UI consumer share the exact same
25
+ * shape this module reads and writes.
26
+ *
27
+ * @module
28
+ */
29
+ import { type Chart } from "@dbx-tools/shared-mastra";
30
+ import type { RequestContext } from "@mastra/core/request-context";
31
+ import { z } from "zod";
32
+ import { type MastraPluginConfig } from "./config.js";
33
+ /**
34
+ * Compact, model-friendly representation of an Echarts spec. The
35
+ * planner agent emits this; {@link planToEchartsOption} expands it
36
+ * into a real `EChartsOption` JSON. Two layers because letting the
37
+ * model fill in a fully-typed `EChartsOption` is brittle (hundreds
38
+ * of optional fields, deep unions, version-dependent shapes). A
39
+ * small "chart plan" schema is much more reliable for a fast model
40
+ * and keeps animation / tooltip / styling defaults consistent
41
+ * across charts.
42
+ */
43
+ export declare const chartPlanSchema: z.ZodObject<{
44
+ chartType: z.ZodUnion<readonly [z.ZodLiteral<"bar">, z.ZodLiteral<"line">, z.ZodLiteral<"area">, z.ZodLiteral<"scatter">, z.ZodLiteral<"pie">]>;
45
+ title: z.ZodOptional<z.ZodString>;
46
+ xAxisLabel: z.ZodOptional<z.ZodString>;
47
+ yAxisLabel: z.ZodOptional<z.ZodString>;
48
+ categories: z.ZodOptional<z.ZodArray<z.ZodString>>;
49
+ series: z.ZodArray<z.ZodObject<{
50
+ name: z.ZodString;
51
+ data: z.ZodArray<z.ZodCatch<z.ZodPipe<z.ZodTransform<number | number[] | {
52
+ name: string;
53
+ value: number;
54
+ } | null, unknown>, z.ZodUnion<readonly [z.ZodNumber, z.ZodNull, z.ZodArray<z.ZodNumber>, z.ZodObject<{
55
+ name: z.ZodString;
56
+ value: z.ZodNumber;
57
+ }, z.core.$strip>]>>>>;
58
+ }, z.core.$strip>>;
59
+ }, z.core.$strip>;
60
+ /**
61
+ * Canonical planner input shape. Tools that source rows from an
62
+ * inline dataset (`render_data`) use it as their `inputSchema`
63
+ * verbatim; tools that resolve rows from a remote (`prepare_chart`
64
+ * over a Genie statement) `omit({ data })` and `extend` with their
65
+ * own identifier field, so the field-level `.describe()` text
66
+ * stays a single source of truth. Server-only - the UI never
67
+ * sees a planner request, only the resolved {@link Chart}.
68
+ */
69
+ export declare const chartPlannerRequestSchema: z.ZodObject<{
70
+ title: z.ZodString;
71
+ description: z.ZodOptional<z.ZodString>;
72
+ data: z.ZodReadonly<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
73
+ }, z.core.$strip>;
74
+ export type ChartPlannerRequest = z.infer<typeof chartPlannerRequestSchema>;
75
+ /** Inputs to {@link prepareChart}. */
76
+ export interface PrepareChartOptions {
77
+ /** Plugin config; resolves the planner agent's model. */
78
+ config: MastraPluginConfig;
79
+ /**
80
+ * Identity that owns the minted chart. Only a {@link fetchChart} call
81
+ * carrying the same key resolves it. Use {@link resolveUserKey}.
82
+ */
83
+ userKey: string;
84
+ /** Display title forwarded to the planner agent. */
85
+ title?: string;
86
+ /** Optional intent hint forwarded to the planner agent. */
87
+ description?: string;
88
+ /**
89
+ * Resolves the rows to chart. Called once, in the background.
90
+ * Any thrown error lands in the cache as the entry's `error`
91
+ * field (never propagated to the caller of {@link prepareChart}).
92
+ * An empty `rows` array is rejected as `"dataset has no rows;
93
+ * nothing to chart"`.
94
+ */
95
+ resolveData: (signal?: AbortSignal) => Promise<{
96
+ rows: ReadonlyArray<Record<string, unknown>>;
97
+ }>;
98
+ /**
99
+ * Per-request `RequestContext`. Forwarded to the planner agent so
100
+ * user-scoped model resolution (OBO) stays in effect.
101
+ */
102
+ requestContext?: RequestContext;
103
+ /**
104
+ * Cooperative cancellation. Forwarded to `resolveData` and the
105
+ * planner agent. Note: the chart task continues running in the
106
+ * background after the parent request ends, so external abort
107
+ * signals are best-effort; typical use is to leave this unset
108
+ * and let the 1h TTL cap stale entries.
109
+ */
110
+ signal?: AbortSignal;
111
+ }
112
+ /**
113
+ * Mint a `chartId`, cache an empty `{ chartId }` placeholder
114
+ * synchronously, and kick off a background task that resolves the
115
+ * dataset and runs the planner. Returns the `chartId` once the
116
+ * placeholder lands so the first {@link fetchChart} call always
117
+ * sees an entry (no spurious 404 race).
118
+ *
119
+ * The background task swallows its own failures and writes them
120
+ * as `error` entries, so callers never see a rejected promise.
121
+ * Cache state machine:
122
+ *
123
+ * - just after this call returns: `{ chartId }` (processing)
124
+ * - on planner success: `{ chartId, result }`
125
+ * - on data / planner failure: `{ chartId, error }`
126
+ */
127
+ export declare function prepareChart(opts: PrepareChartOptions): Promise<{
128
+ chartId: string;
129
+ }>;
130
+ /** Inputs to {@link fetchChart}. */
131
+ export interface FetchChartOptions {
132
+ /**
133
+ * Identity the chart must belong to. A chart minted under a different key
134
+ * is indistinguishable from an unknown id. Use {@link resolveUserKey}.
135
+ */
136
+ userKey: string;
137
+ /**
138
+ * Server-side polling budget in ms. When the entry stays in
139
+ * the processing state past this window, the helper returns the
140
+ * last seen value (still processing) so the client can re-poll.
141
+ * Defaults to {@link DEFAULT_FETCH_TIMEOUT_MS} (60s).
142
+ */
143
+ timeoutMs?: number;
144
+ /**
145
+ * Poll interval in ms. Defaults to
146
+ * {@link DEFAULT_FETCH_INTERVAL_MS} (250ms).
147
+ */
148
+ intervalMs?: number;
149
+ /** External cancellation handle (e.g. request `req.signal`). */
150
+ signal?: AbortSignal;
151
+ }
152
+ /**
153
+ * Long-poll the chart cache until the entry settles (`result` or
154
+ * `error` set), the entry is missing, or the server-side timeout
155
+ * elapses.
156
+ *
157
+ * Returns:
158
+ * - the resolved {@link Chart} when it settled, errored, or
159
+ * stayed in processing past `timeoutMs` (so the client can
160
+ * re-poll);
161
+ * - `undefined` when the entry is missing, expired, or owned by
162
+ * another identity (the consumer should treat as 404).
163
+ *
164
+ * `signal` lets the caller cancel ahead of timeout (e.g. the HTTP
165
+ * request closed). Cancellation propagates to the inter-poll sleep
166
+ * so the helper returns immediately.
167
+ */
168
+ export declare function fetchChart(chartId: string, options: FetchChartOptions): Promise<Chart | undefined>;
169
+ /**
170
+ * Build the `render_data` Mastra tool bound to the given plugin
171
+ * config. Auto-wired as a system tool on every agent (see
172
+ * `agents.ts`); per-agent tools can shadow it by registering a
173
+ * same-named entry.
174
+ *
175
+ * Thin wrapper over {@link prepareChart} for callers that already
176
+ * have a dataset in hand. Mints a `chartId` synchronously, caches
177
+ * an empty placeholder, and kicks off the chart-planner in the
178
+ * background. Returns just the `chartId`; the host UI resolves
179
+ * `[chart:<chartId>]` markers by hitting the plugin's
180
+ * `/embed/chart/:id` route.
181
+ *
182
+ * For Genie statement results, prefer the Genie agent's
183
+ * `prepare_chart` tool, which accepts a `statement_id` and
184
+ * resolves the rows lazily.
185
+ */
186
+ export declare function buildRenderDataTool(config: MastraPluginConfig): import("@mastra/core/tools").Tool<{
187
+ title: string;
188
+ data: readonly Record<string, unknown>[];
189
+ description?: string | undefined;
190
+ }, {
191
+ chartId: string;
192
+ }, unknown, unknown, import("@mastra/core/tools").ToolExecutionContext<unknown, unknown, unknown>, "render_data", unknown>;