drizzle-cube 0.4.17 → 0.4.19

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.
@@ -1,22 +1,22 @@
1
- import { h as $, Q as B, j, D as U } from "./mcp-transport-m1X1GtwG.js";
2
- import { handleDiscover as Z, handleLoad as G } from "./utils.js";
3
- function K(o) {
4
- if (o.length === 0)
1
+ import { h as H, Q as B, j, D as Z } from "./mcp-transport-m1X1GtwG.js";
2
+ import { handleDiscover as U, handleLoad as G } from "./utils.js";
3
+ function K(l) {
4
+ if (l.length === 0)
5
5
  return "No cubes are currently available.";
6
6
  const t = ["## Available Cubes", ""];
7
- for (const a of o) {
7
+ for (const a of l) {
8
8
  if (t.push(`### ${a.name}`), a.description && t.push(a.description), a.measures && a.measures.length > 0) {
9
9
  t.push(""), t.push("**Measures:**");
10
10
  for (const i of a.measures) {
11
- const e = i.description ? ` - ${i.description}` : "";
12
- t.push(`- \`${a.name}.${i.name}\` (${i.type})${e}`);
11
+ const d = i.description ? ` - ${i.description}` : "";
12
+ t.push(`- \`${a.name}.${i.name}\` (${i.type})${d}`);
13
13
  }
14
14
  }
15
15
  if (a.dimensions && a.dimensions.length > 0) {
16
16
  t.push(""), t.push("**Dimensions:**");
17
17
  for (const i of a.dimensions) {
18
- const e = i.description ? ` - ${i.description}` : "";
19
- t.push(`- \`${a.name}.${i.name}\` (${i.type})${e}`);
18
+ const d = i.description ? ` - ${i.description}` : "";
19
+ t.push(`- \`${a.name}.${i.name}\` (${i.type})${d}`);
20
20
  }
21
21
  }
22
22
  if (a.relationships && a.relationships.length > 0) {
@@ -29,12 +29,12 @@ function K(o) {
29
29
  return t.join(`
30
30
  `);
31
31
  }
32
- function I(o) {
33
- return o.messages.map((t) => t.content.text).join(`
32
+ function I(l) {
33
+ return l.messages.map((t) => t.content.text).join(`
34
34
 
35
35
  `);
36
36
  }
37
- function W(o) {
37
+ function W(l) {
38
38
  return [
39
39
  "# Drizzle Cube Analytics Agent",
40
40
  "",
@@ -160,7 +160,7 @@ function W(o) {
160
160
  "",
161
161
  "---",
162
162
  "",
163
- I($),
163
+ I(H),
164
164
  "",
165
165
  "---",
166
166
  "",
@@ -172,7 +172,7 @@ function W(o) {
172
172
  "",
173
173
  "---",
174
174
  "",
175
- I(U),
175
+ I(Z),
176
176
  "",
177
177
  "---",
178
178
  "",
@@ -226,7 +226,7 @@ function W(o) {
226
226
  "",
227
227
  "---",
228
228
  "",
229
- K(o)
229
+ K(l)
230
230
  ].join(`
231
231
  `);
232
232
  }
@@ -769,8 +769,8 @@ const X = {
769
769
  description: "Automatically size blocks to fill portlet width and height while maintaining aspect ratio"
770
770
  }
771
771
  ],
772
- validate: (o) => {
773
- const { dateField: t, valueField: a } = o;
772
+ validate: (l) => {
773
+ const { dateField: t, valueField: a } = l;
774
774
  return !t || Array.isArray(t) && t.length === 0 ? {
775
775
  isValid: !1,
776
776
  message: "Time dimension is required for activity grid"
@@ -938,7 +938,7 @@ const X = {
938
938
  }
939
939
  ],
940
940
  displayOptions: ["hideHeader"],
941
- validate: (o) => !o.yAxis || Array.isArray(o.yAxis) && o.yAxis.length === 0 ? {
941
+ validate: (l) => !l.yAxis || Array.isArray(l.yAxis) && l.yAxis.length === 0 ? {
942
942
  isValid: !1,
943
943
  message: "A measure is required for KPI Delta charts"
944
944
  } : { isValid: !0 }
@@ -1335,7 +1335,7 @@ Use --- for horizontal rules.`,
1335
1335
  description: "Number formatting for cell values and legend"
1336
1336
  }
1337
1337
  ],
1338
- validate: (o) => o.xAxis?.length ? o.yAxis?.length ? o.valueField?.length ? { isValid: !0 } : { isValid: !1, message: "Value measure required" } : { isValid: !1, message: "Y-axis dimension required" } : { isValid: !1, message: "X-axis dimension required" }
1338
+ validate: (l) => l.xAxis?.length ? l.yAxis?.length ? l.valueField?.length ? { isValid: !0 } : { isValid: !1, message: "Value measure required" } : { isValid: !1, message: "Y-axis dimension required" } : { isValid: !1, message: "X-axis dimension required" }
1339
1339
  }, fe = {
1340
1340
  label: "Retention Matrix",
1341
1341
  // RetentionHeatmap auto-configures from the retention data structure
@@ -1708,100 +1708,100 @@ Use --- for horizontal rules.`,
1708
1708
  measureProfile: ke,
1709
1709
  gauge: ve
1710
1710
  };
1711
- function Ce(o, t, a) {
1712
- const i = P[o];
1711
+ function Ce(l, t, a) {
1712
+ const i = P[l];
1713
1713
  if (!i)
1714
1714
  return { isValid: !0, errors: [] };
1715
1715
  if (i.skipQuery)
1716
1716
  return { isValid: !0, errors: [] };
1717
- const e = [];
1718
- for (const s of i.dropZones) {
1719
- if (!s.mandatory) continue;
1720
- const l = t?.[s.key];
1721
- if (!(Array.isArray(l) ? l.length > 0 : !!l)) {
1722
- const u = s.acceptTypes?.join("/") ?? "fields";
1723
- e.push(
1724
- `chartConfig.${s.key} is required for ${o} chart (${s.label}). Accepts: ${u}.`
1717
+ const d = [];
1718
+ for (const u of i.dropZones) {
1719
+ if (!u.mandatory) continue;
1720
+ const e = t?.[u.key];
1721
+ if (!(Array.isArray(e) ? e.length > 0 : !!e)) {
1722
+ const r = u.acceptTypes?.join("/") ?? "fields";
1723
+ d.push(
1724
+ `chartConfig.${u.key} is required for ${l} chart (${u.label}). Accepts: ${r}.`
1725
1725
  );
1726
1726
  }
1727
1727
  }
1728
- if (o === "bar") {
1729
- const s = t?.xAxis;
1730
- if (!(Array.isArray(s) ? s.length > 0 : !!s)) {
1731
- const d = a.dimensions ?? [], u = a.timeDimensions ?? [];
1732
- d.length > 0 || u.length > 0 ? e.push(
1728
+ if (l === "bar") {
1729
+ const u = t?.xAxis;
1730
+ if (!(Array.isArray(u) ? u.length > 0 : !!u)) {
1731
+ const o = a.dimensions ?? [], r = a.timeDimensions ?? [];
1732
+ o.length > 0 || r.length > 0 ? d.push(
1733
1733
  "chartConfig.xAxis is required for bar charts. Put a dimension in xAxis so bars have category labels."
1734
- ) : e.push(
1734
+ ) : d.push(
1735
1735
  'Bar charts need an xAxis dimension for category labels. Add a dimension to the query or use "table" chart type instead.'
1736
1736
  );
1737
1737
  }
1738
1738
  }
1739
1739
  if (t?.xAxis && t?.series) {
1740
- const s = new Set(
1740
+ const u = new Set(
1741
1741
  Array.isArray(t.xAxis) ? t.xAxis : [t.xAxis]
1742
- ), d = (Array.isArray(t.series) ? t.series : [t.series]).filter((u) => s.has(u));
1743
- d.length > 0 && e.push(
1744
- `chartConfig.series must not contain the same field as xAxis (found: ${d.join(", ")}). The series field is only for splitting into grouped/stacked sub-series by a DIFFERENT dimension. Remove the duplicate from series.`
1742
+ ), o = (Array.isArray(t.series) ? t.series : [t.series]).filter((r) => u.has(r));
1743
+ o.length > 0 && d.push(
1744
+ `chartConfig.series must not contain the same field as xAxis (found: ${o.join(", ")}). The series field is only for splitting into grouped/stacked sub-series by a DIFFERENT dimension. Remove the duplicate from series.`
1745
1745
  );
1746
1746
  }
1747
- return { isValid: e.length === 0, errors: e };
1747
+ return { isValid: d.length === 0, errors: d };
1748
1748
  }
1749
- function Te(o, t, a) {
1750
- const i = P[o];
1749
+ function Te(l, t, a) {
1750
+ const i = P[l];
1751
1751
  if (!i)
1752
1752
  return t ?? {};
1753
- const e = { ...t }, s = a.measures ?? [], l = a.dimensions ?? [], u = (a.timeDimensions ?? []).map((p) => p.dimension);
1754
- for (const p of i.dropZones) {
1755
- const m = e[p.key];
1756
- if (Array.isArray(m) ? m.length > 0 : !!m) continue;
1757
- const b = p.acceptTypes ?? [];
1758
- if (p.key === "sizeField" || p.key === "colorField") {
1759
- if (b.includes("measure")) {
1753
+ const d = { ...t }, u = a.measures ?? [], e = a.dimensions ?? [], r = (a.timeDimensions ?? []).map((s) => s.dimension);
1754
+ for (const s of i.dropZones) {
1755
+ const h = d[s.key];
1756
+ if (Array.isArray(h) ? h.length > 0 : !!h) continue;
1757
+ const c = s.acceptTypes ?? [];
1758
+ if (s.key === "sizeField" || s.key === "colorField") {
1759
+ if (c.includes("measure")) {
1760
1760
  const g = /* @__PURE__ */ new Set();
1761
- for (const S of i.dropZones) {
1762
- if (S.key === p.key) continue;
1763
- const x = e[S.key];
1764
- Array.isArray(x) ? x.forEach((V) => g.add(V)) : typeof x == "string" && g.add(x);
1761
+ for (const C of i.dropZones) {
1762
+ if (C.key === s.key) continue;
1763
+ const k = d[C.key];
1764
+ Array.isArray(k) ? k.forEach((E) => g.add(E)) : typeof k == "string" && g.add(k);
1765
1765
  }
1766
- const C = s.filter((S) => !g.has(S));
1767
- C.length > 0 && (e[p.key] = C[0]);
1766
+ const A = u.filter((C) => !g.has(C));
1767
+ A.length > 0 && (d[s.key] = A[0]);
1768
1768
  }
1769
1769
  continue;
1770
1770
  }
1771
- const y = [];
1772
- if (b.includes("dimension") && y.push(...l), b.includes("timeDimension") && y.push(...u), b.includes("measure") && y.push(...s), y.length === 0) continue;
1773
- let f = y;
1774
- if (p.key === "series") {
1771
+ const n = [];
1772
+ if (c.includes("dimension") && n.push(...e), c.includes("timeDimension") && n.push(...r), c.includes("measure") && n.push(...u), n.length === 0) continue;
1773
+ let x = n;
1774
+ if (s.key === "series") {
1775
1775
  const g = new Set(
1776
- Array.isArray(e.xAxis) ? e.xAxis : e.xAxis ? [e.xAxis] : []
1776
+ Array.isArray(d.xAxis) ? d.xAxis : d.xAxis ? [d.xAxis] : []
1777
1777
  );
1778
- if (f = y.filter((C) => !g.has(C)), f.length === 0) continue;
1778
+ if (x = n.filter((A) => !g.has(A)), x.length === 0) continue;
1779
1779
  }
1780
- const v = p.maxItems ?? 1 / 0, D = f.slice(0, v);
1781
- D.length > 0 && (e[p.key] = D);
1780
+ const b = s.maxItems ?? 1 / 0, y = x.slice(0, b);
1781
+ y.length > 0 && (d[s.key] = y);
1782
1782
  }
1783
- return e;
1783
+ return d;
1784
1784
  }
1785
- function Ae(o) {
1785
+ function Ae(l) {
1786
1786
  const t = [`
1787
1787
  Chart config requirements by type:`];
1788
- for (const a of o) {
1788
+ for (const a of l) {
1789
1789
  const i = P[a];
1790
1790
  if (!i) continue;
1791
- const e = i.description ?? "", s = i.useCase ?? "", l = [e, s].filter(Boolean).join(". "), d = l ? ` — ${l}.` : "", u = i.dropZones.filter((m) => m.mandatory);
1792
- if (u.length === 0 && !i.skipQuery) {
1793
- t.push(` ${a}${d} chartConfig auto-inferred from query.`);
1791
+ const d = i.description ?? "", u = i.useCase ?? "", e = [d, u].filter(Boolean).join(". "), o = e ? ` — ${e}.` : "", r = i.dropZones.filter((h) => h.mandatory);
1792
+ if (r.length === 0 && !i.skipQuery) {
1793
+ t.push(` ${a}${o} chartConfig auto-inferred from query.`);
1794
1794
  continue;
1795
1795
  }
1796
1796
  if (i.skipQuery) {
1797
- t.push(` ${a}${d} No query needed.`);
1797
+ t.push(` ${a}${o} No query needed.`);
1798
1798
  continue;
1799
1799
  }
1800
- const p = u.map((m) => {
1801
- const n = m.acceptTypes?.join("/") ?? "any", b = m.maxItems ? ` (max ${m.maxItems})` : "";
1802
- return `${m.key}=[${n}]${b}`;
1800
+ const s = r.map((h) => {
1801
+ const m = h.acceptTypes?.join("/") ?? "any", c = h.maxItems ? ` (max ${h.maxItems})` : "";
1802
+ return `${h.key}=[${m}]${c}`;
1803
1803
  });
1804
- t.push(` ${a}${d} Requires ${p.join(", ")}.`);
1804
+ t.push(` ${a}${o} Requires ${s.join(", ")}.`);
1805
1805
  }
1806
1806
  return t.join(`
1807
1807
  `);
@@ -2121,32 +2121,81 @@ The query is validated before adding. The portlet fetches its own data.`,
2121
2121
  }
2122
2122
  ];
2123
2123
  }
2124
- function Se(o) {
2125
- const { semanticLayer: t, securityContext: a } = o, i = /* @__PURE__ */ new Map();
2126
- return i.set("discover_cubes", async (e) => {
2127
- const s = await Z(t, {
2124
+ function Se(l) {
2125
+ const { semanticLayer: t, securityContext: a } = l, i = /* @__PURE__ */ new Map();
2126
+ i.set("discover_cubes", async (e) => {
2127
+ const o = await U(t, {
2128
2128
  topic: e.topic,
2129
2129
  intent: e.intent,
2130
2130
  limit: e.limit,
2131
2131
  minScore: e.minScore
2132
2132
  });
2133
- return { result: JSON.stringify(s, null, 2) };
2133
+ return { result: JSON.stringify(o, null, 2) };
2134
2134
  }), i.set("get_cube_metadata", async () => {
2135
2135
  const e = t.getMetadata();
2136
2136
  return { result: JSON.stringify(e, null, 2) };
2137
- }), i.set("execute_query", async (e) => {
2137
+ });
2138
+ const d = /* @__PURE__ */ new Map();
2139
+ for (const e of t.getMetadata())
2140
+ d.set(e.name, {
2141
+ measures: (e.measures || []).map((o) => o.name),
2142
+ dimensions: (e.dimensions || []).map((o) => o.name)
2143
+ });
2144
+ const u = (e, o) => {
2145
+ const r = d.get(e), s = o === "measures" ? r?.measures : r?.dimensions;
2146
+ return !s || s.length === 0 ? "" : ` Available ${o}: ${s.slice(0, 5).map((h) => `"${h}"`).join(", ")}`;
2147
+ };
2148
+ return i.set("execute_query", async (e) => {
2138
2149
  try {
2139
- const s = (u, p) => {
2140
- if (!Array.isArray(u)) return;
2141
- const m = u.filter((n) => typeof n == "string" && !n.includes("."));
2142
- if (m.length > 0)
2143
- throw new Error(
2144
- `Invalid ${p}: ${m.map((n) => `"${n}"`).join(", ")}. All ${p} must use "CubeName.fieldName" format (e.g., "PullRequests.count", "Teams.name"). Use discover_cubes or get_cube_metadata to find valid field names.`
2145
- );
2150
+ const o = (m, c) => {
2151
+ if (!Array.isArray(m)) return;
2152
+ const n = [], x = [];
2153
+ for (const b of m) {
2154
+ if (typeof b != "string") {
2155
+ x.push(b);
2156
+ continue;
2157
+ }
2158
+ const y = b.split(".");
2159
+ if (y.length === 1) {
2160
+ n.push(`"${b}" is not valid — must be "CubeName.fieldName".${u(b, c)}`);
2161
+ continue;
2162
+ }
2163
+ if (y.length === 3 && y[0] === y[1]) {
2164
+ const g = `${y[0]}.${y[2]}`;
2165
+ x.push(g);
2166
+ continue;
2167
+ }
2168
+ if (y.length === 2 && y[0] === y[1]) {
2169
+ n.push(`"${b}" is WRONG — "${y[0]}" is the cube name, not a ${c.replace(/s$/, "")}.${u(y[0], c)}`);
2170
+ continue;
2171
+ }
2172
+ x.push(b);
2173
+ }
2174
+ if (n.length > 0)
2175
+ throw new Error(`Invalid ${c}:
2176
+ ${n.join(`
2177
+ `)}`);
2178
+ return x;
2146
2179
  };
2147
- s(e.measures, "measures"), s(e.dimensions, "dimensions");
2148
- let l;
2149
- e.funnel ? l = { funnel: e.funnel } : e.flow ? l = { flow: e.flow } : e.retention ? l = { retention: e.retention } : l = {
2180
+ e.measures = o(e.measures, "measures") ?? e.measures, e.dimensions = o(e.dimensions, "dimensions") ?? e.dimensions;
2181
+ const r = (m) => {
2182
+ const c = m.split(".");
2183
+ return c.length === 3 && c[0] === c[1] ? `${c[0]}.${c[2]}` : m;
2184
+ };
2185
+ if (Array.isArray(e.filters))
2186
+ for (const m of e.filters)
2187
+ typeof m.member == "string" && (m.member = r(m.member));
2188
+ if (Array.isArray(e.timeDimensions))
2189
+ for (const m of e.timeDimensions)
2190
+ typeof m.dimension == "string" && (m.dimension = r(m.dimension));
2191
+ if (e.order && typeof e.order == "object" && !Array.isArray(e.order)) {
2192
+ const m = {};
2193
+ for (const [c, n] of Object.entries(e.order))
2194
+ m[r(c)] = n;
2195
+ e.order = m;
2196
+ }
2197
+ let s;
2198
+ e.funnel ? s = { funnel: e.funnel } : e.flow ? s = { flow: e.flow } : e.retention ? s = { retention: e.retention } : s = {
2150
2199
  measures: e.measures,
2151
2200
  dimensions: e.dimensions,
2152
2201
  filters: e.filters,
@@ -2154,135 +2203,152 @@ function Se(o) {
2154
2203
  order: e.order,
2155
2204
  limit: e.limit
2156
2205
  };
2157
- const d = await G(t, a, { query: l });
2206
+ const h = await G(t, a, { query: s });
2158
2207
  return {
2159
2208
  result: JSON.stringify({
2160
- rowCount: d.data.length,
2161
- data: d.data,
2162
- annotation: d.annotation
2209
+ rowCount: h.data.length,
2210
+ data: h.data,
2211
+ annotation: h.annotation
2163
2212
  }, null, 2)
2164
2213
  };
2165
- } catch (s) {
2214
+ } catch (o) {
2215
+ const r = {
2216
+ measures: e.measures,
2217
+ dimensions: e.dimensions,
2218
+ filters: e.filters,
2219
+ timeDimensions: e.timeDimensions,
2220
+ order: e.order,
2221
+ limit: e.limit,
2222
+ ...e.funnel ? { funnel: e.funnel } : {},
2223
+ ...e.flow ? { flow: e.flow } : {},
2224
+ ...e.retention ? { retention: e.retention } : {}
2225
+ };
2166
2226
  return {
2167
- result: `Query execution failed: ${s instanceof Error ? s.message : "Unknown error"}`,
2227
+ result: `Query execution failed: ${o instanceof Error ? o.message : "Unknown error"}
2228
+
2229
+ Attempted query:
2230
+ ${JSON.stringify(r, null, 2)}`,
2168
2231
  isError: !0
2169
2232
  };
2170
2233
  }
2171
2234
  }), i.set("add_portlet", async (e) => {
2172
- const l = {
2235
+ const r = {
2173
2236
  number: "kpiNumber",
2174
2237
  retention: "retentionHeatmap"
2175
2238
  }[e.chartType] ?? e.chartType;
2176
- let d;
2239
+ let s;
2177
2240
  try {
2178
- d = JSON.parse(e.query);
2241
+ s = JSON.parse(e.query);
2179
2242
  } catch {
2180
2243
  return {
2181
2244
  result: "Invalid query: could not parse JSON string. Ensure `query` is a valid JSON string.",
2182
2245
  isError: !0
2183
2246
  };
2184
2247
  }
2185
- const u = t.validateQuery(d);
2186
- if (!u.isValid)
2248
+ const h = t.validateQuery(s);
2249
+ if (!h.isValid)
2187
2250
  return {
2188
2251
  result: `Invalid query — fix these errors and retry:
2189
- ${u.errors.join(`
2190
- `)}`,
2252
+ ${h.errors.join(`
2253
+ `)}
2254
+
2255
+ Attempted query:
2256
+ ${JSON.stringify(s, null, 2)}`,
2191
2257
  isError: !0
2192
2258
  };
2193
- const p = !!(d.funnel || d.flow || d.retention);
2194
- let m;
2195
- if (p)
2196
- m = e.chartConfig ?? {};
2259
+ const m = !!(s.funnel || s.flow || s.retention);
2260
+ let c;
2261
+ if (m)
2262
+ c = e.chartConfig ?? {};
2197
2263
  else {
2198
- const y = Te(l, e.chartConfig, d), f = Ce(l, y, d);
2199
- if (!f.isValid)
2264
+ const b = Te(r, e.chartConfig, s), y = Ce(r, b, s);
2265
+ if (!y.isValid)
2200
2266
  return {
2201
2267
  result: `Chart config invalid — fix these errors and retry:
2202
- ${f.errors.join(`
2268
+ ${y.errors.join(`
2203
2269
  `)}`,
2204
2270
  isError: !0
2205
2271
  };
2206
- m = y;
2272
+ c = b;
2207
2273
  }
2208
- const n = `portlet-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`, b = {
2274
+ const n = `portlet-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`, x = {
2209
2275
  id: n,
2210
2276
  title: e.title,
2211
2277
  query: e.query,
2212
- chartType: l,
2213
- chartConfig: m,
2278
+ chartType: r,
2279
+ chartConfig: c,
2214
2280
  displayConfig: e.displayConfig
2215
2281
  };
2216
2282
  return {
2217
- result: `Portlet "${e.title}" added to notebook (id: ${n}, chart: ${l}). [Reminder: in your next response, start with a brief sentence about what you will do next BEFORE making any tool calls.]`,
2218
- sideEffect: { type: "add_portlet", data: b }
2283
+ result: `Portlet "${e.title}" added to notebook (id: ${n}, chart: ${r}). [Reminder: in your next response, start with a brief sentence about what you will do next BEFORE making any tool calls.]`,
2284
+ sideEffect: { type: "add_portlet", data: x }
2219
2285
  };
2220
2286
  }), i.set("add_markdown", async (e) => {
2221
- const s = `markdown-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`, l = {
2222
- id: s,
2287
+ const o = `markdown-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`, r = {
2288
+ id: o,
2223
2289
  title: e.title,
2224
2290
  content: e.content
2225
2291
  };
2226
2292
  return {
2227
- result: `Markdown block added to notebook (id: ${s}). [Reminder: in your next response, start with a brief sentence about what you will do next BEFORE making any tool calls.]`,
2228
- sideEffect: { type: "add_markdown", data: l }
2293
+ result: `Markdown block added to notebook (id: ${o}). [Reminder: in your next response, start with a brief sentence about what you will do next BEFORE making any tool calls.]`,
2294
+ sideEffect: { type: "add_markdown", data: r }
2229
2295
  };
2230
2296
  }), i.set("save_as_dashboard", async (e) => {
2231
2297
  try {
2232
- const s = e.portlets;
2233
- if (!s || s.length === 0)
2298
+ const o = e.portlets;
2299
+ if (!o || o.length === 0)
2234
2300
  return { result: "Dashboard must contain at least one portlet.", isError: !0 };
2235
- const l = [];
2236
- for (const n of s) {
2301
+ const r = [];
2302
+ for (const n of o) {
2237
2303
  if (n.chartType === "markdown") continue;
2238
- const y = n.query;
2239
- if (!y) {
2240
- l.push(`Portlet "${n.title}": missing query`);
2304
+ const b = n.query;
2305
+ if (!b) {
2306
+ r.push(`Portlet "${n.title}": missing query`);
2241
2307
  continue;
2242
2308
  }
2243
- let f;
2309
+ let y;
2244
2310
  try {
2245
- f = JSON.parse(y);
2311
+ y = JSON.parse(b);
2246
2312
  } catch {
2247
- l.push(`Portlet "${n.title}": invalid JSON query`);
2313
+ r.push(`Portlet "${n.title}": invalid JSON query`);
2248
2314
  continue;
2249
2315
  }
2250
- const v = t.validateQuery(f);
2251
- v.isValid || l.push(`Portlet "${n.title}": ${v.errors.join(", ")}`);
2316
+ const g = t.validateQuery(y);
2317
+ g.isValid || r.push(`Portlet "${n.title}": ${g.errors.join(", ")}`);
2252
2318
  }
2253
- if (l.length > 0)
2319
+ if (r.length > 0)
2254
2320
  return {
2255
2321
  result: `Dashboard has invalid portlets — fix these errors and retry:
2256
- ${l.join(`
2322
+ ${r.join(`
2257
2323
  `)}`,
2258
2324
  isError: !0
2259
2325
  };
2260
- const d = {
2261
- portlets: s.map((n) => {
2262
- const b = n.chartType, y = b === "markdown", f = y ? "query" : n.analysisType || "query", v = f === "funnel" ? "funnel" : f === "flow" ? "flow" : f === "retention" ? "retention" : "query", D = n.query || "{}";
2263
- let g;
2326
+ const s = {
2327
+ portlets: o.map((n) => {
2328
+ const x = n.chartType, b = x === "markdown", y = b ? "query" : n.analysisType || "query", g = y === "funnel" ? "funnel" : y === "flow" ? "flow" : y === "retention" ? "retention" : "query", A = n.query || "{}";
2329
+ let C;
2264
2330
  try {
2265
- g = JSON.parse(D);
2331
+ C = JSON.parse(A);
2266
2332
  } catch {
2267
- g = {};
2333
+ C = {};
2268
2334
  }
2269
- const C = {
2335
+ const k = {
2270
2336
  version: 1,
2271
- analysisType: v,
2337
+ analysisType: g,
2272
2338
  activeView: "chart",
2273
2339
  charts: {
2274
- [v]: {
2275
- chartType: b,
2340
+ [g]: {
2341
+ chartType: x,
2276
2342
  chartConfig: n.chartConfig || {},
2277
2343
  displayConfig: n.displayConfig || {}
2278
2344
  }
2279
2345
  },
2280
- query: y ? {} : g
2346
+ query: b ? {} : C
2281
2347
  };
2282
2348
  return {
2283
2349
  id: n.id,
2284
2350
  title: n.title,
2285
- analysisConfig: C,
2351
+ analysisConfig: k,
2286
2352
  dashboardFilterMapping: n.dashboardFilterMapping,
2287
2353
  w: n.w,
2288
2354
  h: n.h,
@@ -2292,35 +2358,35 @@ ${l.join(`
2292
2358
  }),
2293
2359
  filters: e.filters,
2294
2360
  colorPalette: e.colorPalette
2295
- }, u = e.title, p = d.portlets.length, m = d.filters?.length || 0;
2361
+ }, h = e.title, m = s.portlets.length, c = s.filters?.length || 0;
2296
2362
  return {
2297
- result: `Dashboard "${u}" created with ${p} portlets and ${m} filters.`,
2363
+ result: `Dashboard "${h}" created with ${m} portlets and ${c} filters.`,
2298
2364
  sideEffect: {
2299
2365
  type: "dashboard_saved",
2300
2366
  data: {
2301
- title: u,
2367
+ title: h,
2302
2368
  description: e.description,
2303
- dashboardConfig: d
2369
+ dashboardConfig: s
2304
2370
  }
2305
2371
  }
2306
2372
  };
2307
- } catch (s) {
2373
+ } catch (o) {
2308
2374
  return {
2309
- result: `Failed to save dashboard: ${s instanceof Error ? s.message : "Unknown error"}`,
2375
+ result: `Failed to save dashboard: ${o instanceof Error ? o.message : "Unknown error"}`,
2310
2376
  isError: !0
2311
2377
  };
2312
2378
  }
2313
2379
  }), i;
2314
2380
  }
2315
- async function* Ee(o) {
2316
- const { message: t, history: a, semanticLayer: i, securityContext: e, agentConfig: s, apiKey: l } = o, d = o.sessionId || crypto.randomUUID(), u = s.observability, p = crypto.randomUUID(), m = Date.now();
2317
- let n;
2381
+ async function* _e(l) {
2382
+ const { message: t, history: a, semanticLayer: i, securityContext: d, agentConfig: u, apiKey: e } = l, o = l.sessionId || crypto.randomUUID(), r = u.observability, s = crypto.randomUUID(), h = Date.now();
2383
+ let m;
2318
2384
  try {
2319
- const c = await import(
2385
+ const f = await import(
2320
2386
  /* webpackIgnore: true */
2321
2387
  "@anthropic-ai/sdk"
2322
2388
  );
2323
- n = c.default || c.Anthropic || c;
2389
+ m = f.default || f.Anthropic || f;
2324
2390
  } catch {
2325
2391
  yield {
2326
2392
  type: "error",
@@ -2330,171 +2396,171 @@ async function* Ee(o) {
2330
2396
  };
2331
2397
  return;
2332
2398
  }
2333
- const b = new n({ apiKey: l }), y = De(), f = Se({ semanticLayer: i, securityContext: e }), v = i.getMetadata(), D = W(v), g = s.model || "claude-sonnet-4-6", C = s.maxTurns || 25, S = s.maxTokens || 4096;
2399
+ const c = new m({ apiKey: e }), n = De(), x = Se({ semanticLayer: i, securityContext: d }), b = i.getMetadata(), y = W(b), g = u.model || "claude-sonnet-4-6", A = u.maxTurns || 25, C = u.maxTokens || 4096;
2334
2400
  try {
2335
- u?.onChatStart?.({
2336
- traceId: p,
2337
- sessionId: d,
2401
+ r?.onChatStart?.({
2402
+ traceId: s,
2403
+ sessionId: o,
2338
2404
  message: t,
2339
2405
  model: g,
2340
2406
  historyLength: a?.length ?? 0
2341
2407
  });
2342
2408
  } catch {
2343
2409
  }
2344
- const x = [];
2410
+ const k = [];
2345
2411
  if (a && a.length > 0) {
2346
- for (const c of a)
2347
- if (c.role === "user")
2348
- x.push({ role: "user", content: c.content });
2349
- else if (c.role === "assistant") {
2412
+ for (const f of a)
2413
+ if (f.role === "user")
2414
+ k.push({ role: "user", content: f.content });
2415
+ else if (f.role === "assistant") {
2350
2416
  const F = [];
2351
- if (c.content && F.push({ type: "text", text: c.content }), c.toolCalls && c.toolCalls.length > 0) {
2352
- for (const h of c.toolCalls)
2417
+ if (f.content && F.push({ type: "text", text: f.content }), f.toolCalls && f.toolCalls.length > 0) {
2418
+ for (const w of f.toolCalls)
2353
2419
  F.push({
2354
2420
  type: "tool_use",
2355
- id: h.id,
2356
- name: h.name,
2357
- input: h.input || {}
2421
+ id: w.id,
2422
+ name: w.name,
2423
+ input: w.input || {}
2358
2424
  });
2359
- x.push({ role: "assistant", content: F }), x.push({
2425
+ k.push({ role: "assistant", content: F }), k.push({
2360
2426
  role: "user",
2361
- content: c.toolCalls.map((h) => ({
2427
+ content: f.toolCalls.map((w) => ({
2362
2428
  type: "tool_result",
2363
- tool_use_id: h.id,
2364
- content: typeof h.result == "string" ? h.result : JSON.stringify(h.result ?? ""),
2365
- ...h.status === "error" ? { is_error: !0 } : {}
2429
+ tool_use_id: w.id,
2430
+ content: typeof w.result == "string" ? w.result : JSON.stringify(w.result ?? ""),
2431
+ ...w.status === "error" ? { is_error: !0 } : {}
2366
2432
  }))
2367
2433
  });
2368
- } else F.length > 0 && x.push({ role: "assistant", content: c.content });
2434
+ } else F.length > 0 && k.push({ role: "assistant", content: f.content });
2369
2435
  }
2370
2436
  }
2371
- x.push({ role: "user", content: t });
2372
- let V = 0;
2437
+ k.push({ role: "user", content: t });
2438
+ let E = 0;
2373
2439
  try {
2374
- for (let c = 0; c < C; c++) {
2375
- V = c + 1;
2376
- const F = await b.messages.create({
2440
+ for (let f = 0; f < A; f++) {
2441
+ E = f + 1;
2442
+ const F = await c.messages.create({
2377
2443
  model: g,
2378
- max_tokens: S,
2379
- system: D,
2380
- tools: y,
2381
- messages: x,
2444
+ max_tokens: C,
2445
+ system: y,
2446
+ tools: n,
2447
+ messages: k,
2382
2448
  stream: !0
2383
- }), h = [];
2384
- let q = -1, _ = "", O = "", L, R;
2385
- const H = Date.now();
2386
- for await (const w of F)
2387
- switch (w.type) {
2449
+ }), w = [];
2450
+ let q = -1, V = "", O = "", L, M;
2451
+ const z = Date.now();
2452
+ for await (const v of F)
2453
+ switch (v.type) {
2388
2454
  case "content_block_start": {
2389
2455
  q++;
2390
- const r = w.content_block;
2391
- r.type === "tool_use" ? (h.push({ type: "tool_use", id: r.id, name: r.name, input: {} }), _ = "", yield {
2456
+ const p = v.content_block;
2457
+ p.type === "tool_use" ? (w.push({ type: "tool_use", id: p.id, name: p.name, input: {} }), V = "", yield {
2392
2458
  type: "tool_use_start",
2393
- data: { id: r.id, name: r.name, input: void 0 }
2394
- }) : r.type === "text" && h.push({ type: "text", text: "" });
2459
+ data: { id: p.id, name: p.name, input: void 0 }
2460
+ }) : p.type === "text" && w.push({ type: "text", text: "" });
2395
2461
  break;
2396
2462
  }
2397
2463
  case "content_block_delta": {
2398
- const r = w.delta;
2399
- if (r.type === "text_delta" && r.text) {
2400
- const T = h[q];
2401
- T && (T.text = (T.text || "") + r.text), yield { type: "text_delta", data: r.text };
2402
- } else r.type === "input_json_delta" && r.partial_json && (_ += r.partial_json);
2464
+ const p = v.delta;
2465
+ if (p.type === "text_delta" && p.text) {
2466
+ const D = w[q];
2467
+ D && (D.text = (D.text || "") + p.text), yield { type: "text_delta", data: p.text };
2468
+ } else p.type === "input_json_delta" && p.partial_json && (V += p.partial_json);
2403
2469
  break;
2404
2470
  }
2405
2471
  case "content_block_stop": {
2406
- const r = h[q];
2407
- if (r?.type === "tool_use" && _) {
2472
+ const p = w[q];
2473
+ if (p?.type === "tool_use" && V) {
2408
2474
  try {
2409
- r.input = JSON.parse(_);
2475
+ p.input = JSON.parse(V);
2410
2476
  } catch {
2411
- r.input = {};
2477
+ p.input = {};
2412
2478
  }
2413
- _ = "";
2479
+ V = "";
2414
2480
  }
2415
2481
  break;
2416
2482
  }
2417
2483
  case "message_start": {
2418
- const r = w.message;
2419
- r?.usage?.input_tokens != null && (L = r.usage.input_tokens);
2484
+ const p = v.message;
2485
+ p?.usage?.input_tokens != null && (L = p.usage.input_tokens);
2420
2486
  break;
2421
2487
  }
2422
2488
  case "message_delta": {
2423
- const r = w.delta, T = w.usage;
2424
- T?.output_tokens != null && (R = T.output_tokens), r.stop_reason && (O = r.stop_reason);
2489
+ const p = v.delta, D = v.usage;
2490
+ D?.output_tokens != null && (M = D.output_tokens), p.stop_reason && (O = p.stop_reason);
2425
2491
  break;
2426
2492
  }
2427
2493
  }
2428
2494
  try {
2429
- u?.onGenerationEnd?.({
2430
- traceId: p,
2431
- turn: c,
2495
+ r?.onGenerationEnd?.({
2496
+ traceId: s,
2497
+ turn: f,
2432
2498
  model: g,
2433
2499
  stopReason: O,
2434
2500
  inputTokens: L,
2435
- outputTokens: R,
2436
- durationMs: Date.now() - H
2501
+ outputTokens: M,
2502
+ durationMs: Date.now() - z
2437
2503
  });
2438
2504
  } catch {
2439
2505
  }
2440
- if (x.push({ role: "assistant", content: h }), O !== "tool_use")
2506
+ if (k.push({ role: "assistant", content: w }), O !== "tool_use")
2441
2507
  break;
2442
- const E = [];
2443
- for (const w of h) {
2444
- if (w.type !== "tool_use") continue;
2445
- const r = w.name, T = w.input || {}, A = w.id, M = f.get(r);
2446
- if (!M) {
2447
- E.push({
2508
+ const _ = [];
2509
+ for (const v of w) {
2510
+ if (v.type !== "tool_use") continue;
2511
+ const p = v.name, D = v.input || {}, S = v.id, R = x.get(p);
2512
+ if (!R) {
2513
+ _.push({
2448
2514
  type: "tool_result",
2449
- tool_use_id: A,
2450
- content: `Unknown tool: ${r}`,
2515
+ tool_use_id: S,
2516
+ content: `Unknown tool: ${p}`,
2451
2517
  is_error: !0
2452
2518
  }), yield {
2453
2519
  type: "tool_use_result",
2454
- data: { id: A, name: r, result: `Unknown tool: ${r}`, isError: !0 }
2520
+ data: { id: S, name: p, result: `Unknown tool: ${p}`, isError: !0 }
2455
2521
  };
2456
2522
  continue;
2457
2523
  }
2458
2524
  const Y = Date.now();
2459
2525
  try {
2460
- const k = await M(T);
2461
- k.sideEffect && (yield k.sideEffect), E.push({
2526
+ const T = await R(D);
2527
+ T.sideEffect && (yield T.sideEffect), _.push({
2462
2528
  type: "tool_result",
2463
- tool_use_id: A,
2464
- content: k.result,
2465
- ...k.isError ? { is_error: !0 } : {}
2529
+ tool_use_id: S,
2530
+ content: T.result,
2531
+ ...T.isError ? { is_error: !0 } : {}
2466
2532
  }), yield {
2467
2533
  type: "tool_use_result",
2468
- data: { id: A, name: r, result: k.result, ...k.isError ? { isError: !0 } : {} }
2534
+ data: { id: S, name: p, result: T.result, ...T.isError ? { isError: !0 } : {} }
2469
2535
  };
2470
2536
  try {
2471
- u?.onToolEnd?.({
2472
- traceId: p,
2473
- turn: c,
2474
- toolName: r,
2475
- toolUseId: A,
2476
- isError: !!k.isError,
2537
+ r?.onToolEnd?.({
2538
+ traceId: s,
2539
+ turn: f,
2540
+ toolName: p,
2541
+ toolUseId: S,
2542
+ isError: !!T.isError,
2477
2543
  durationMs: Date.now() - Y
2478
2544
  });
2479
2545
  } catch {
2480
2546
  }
2481
- } catch (k) {
2482
- const z = k instanceof Error ? k.message : "Tool execution failed";
2483
- E.push({
2547
+ } catch (T) {
2548
+ const $ = T instanceof Error ? T.message : "Tool execution failed";
2549
+ _.push({
2484
2550
  type: "tool_result",
2485
- tool_use_id: A,
2486
- content: z,
2551
+ tool_use_id: S,
2552
+ content: $,
2487
2553
  is_error: !0
2488
2554
  }), yield {
2489
2555
  type: "tool_use_result",
2490
- data: { id: A, name: r, result: z, isError: !0 }
2556
+ data: { id: S, name: p, result: $, isError: !0 }
2491
2557
  };
2492
2558
  try {
2493
- u?.onToolEnd?.({
2494
- traceId: p,
2495
- turn: c,
2496
- toolName: r,
2497
- toolUseId: A,
2559
+ r?.onToolEnd?.({
2560
+ traceId: s,
2561
+ turn: f,
2562
+ toolName: p,
2563
+ toolUseId: S,
2498
2564
  isError: !0,
2499
2565
  durationMs: Date.now() - Y
2500
2566
  });
@@ -2502,60 +2568,60 @@ async function* Ee(o) {
2502
2568
  }
2503
2569
  }
2504
2570
  }
2505
- yield { type: "turn_complete", data: {} }, x.push({ role: "user", content: E });
2571
+ yield { type: "turn_complete", data: {} }, k.push({ role: "user", content: _ });
2506
2572
  }
2507
2573
  try {
2508
- u?.onChatEnd?.({
2509
- traceId: p,
2510
- sessionId: d,
2511
- totalTurns: V,
2512
- durationMs: Date.now() - m
2574
+ r?.onChatEnd?.({
2575
+ traceId: s,
2576
+ sessionId: o,
2577
+ totalTurns: E,
2578
+ durationMs: Date.now() - h
2513
2579
  });
2514
2580
  } catch {
2515
2581
  }
2516
2582
  yield {
2517
2583
  type: "done",
2518
- data: { sessionId: d || "", traceId: p }
2584
+ data: { sessionId: o || "", traceId: s }
2519
2585
  };
2520
- } catch (c) {
2586
+ } catch (f) {
2521
2587
  try {
2522
- u?.onChatEnd?.({
2523
- traceId: p,
2524
- sessionId: d,
2588
+ r?.onChatEnd?.({
2589
+ traceId: s,
2590
+ sessionId: o,
2525
2591
  totalTurns: 0,
2526
- durationMs: Date.now() - m,
2527
- error: c instanceof Error ? c.message : "Unknown error"
2592
+ durationMs: Date.now() - h,
2593
+ error: f instanceof Error ? f.message : "Unknown error"
2528
2594
  });
2529
2595
  } catch {
2530
2596
  }
2531
2597
  yield {
2532
2598
  type: "error",
2533
2599
  data: {
2534
- message: Fe(c)
2600
+ message: Fe(f)
2535
2601
  }
2536
2602
  };
2537
2603
  }
2538
2604
  }
2539
- function Fe(o) {
2540
- if (!o || !(o instanceof Error))
2605
+ function Fe(l) {
2606
+ if (!l || !(l instanceof Error))
2541
2607
  return "Something went wrong. Please try again.";
2542
- const t = o.message || "", a = {
2608
+ const t = l.message || "", a = {
2543
2609
  overloaded_error: "The AI service is temporarily overloaded. Please try again in a moment.",
2544
2610
  rate_limit_error: "Too many requests. Please wait a moment and try again.",
2545
2611
  api_error: "The AI service encountered an error. Please try again.",
2546
2612
  authentication_error: "Authentication failed. Please check your API key configuration.",
2547
2613
  invalid_request_error: "There was a problem with the request. Please try again."
2548
- }, i = o;
2614
+ }, i = l;
2549
2615
  if (i.status || i.type) {
2550
- const e = i.error?.type || i.type || "";
2551
- if (a[e])
2552
- return a[e];
2616
+ const d = i.error?.type || i.type || "";
2617
+ if (a[d])
2618
+ return a[d];
2553
2619
  }
2554
2620
  if (t.startsWith("{") || t.startsWith("Error: {")) {
2555
2621
  try {
2556
- const e = JSON.parse(t.replace(/^Error:\s*/, "")), s = e.error?.type || e.type || "";
2557
- if (a[s])
2558
- return a[s];
2622
+ const d = JSON.parse(t.replace(/^Error:\s*/, "")), u = d.error?.type || d.type || "";
2623
+ if (a[u])
2624
+ return a[u];
2559
2625
  } catch {
2560
2626
  }
2561
2627
  return "The AI service encountered an error. Please try again.";
@@ -2563,5 +2629,5 @@ function Fe(o) {
2563
2629
  return t;
2564
2630
  }
2565
2631
  export {
2566
- Ee as handleAgentChat
2632
+ _e as handleAgentChat
2567
2633
  };