@querypanel/node-sdk 1.0.52 → 1.0.54

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/dist/index.cjs CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -1455,21 +1454,33 @@ function anonymizeResults(rows) {
1455
1454
  }
1456
1455
 
1457
1456
  // src/routes/modify.ts
1458
- function buildModifiedQuestion(originalQuestion, modifications) {
1457
+ function buildModifiedQuestion(originalQuestion, modifications, pipeline) {
1459
1458
  const hints = [];
1460
1459
  if (modifications.timeGranularity) {
1461
1460
  hints.push(`group results by ${modifications.timeGranularity}`);
1462
1461
  }
1463
1462
  if (modifications.dateRange) {
1464
- const parts = [];
1465
- if (modifications.dateRange.from) {
1466
- parts.push(`from ${modifications.dateRange.from}`);
1467
- }
1468
- if (modifications.dateRange.to) {
1469
- parts.push(`to ${modifications.dateRange.to}`);
1470
- }
1471
- if (parts.length > 0) {
1472
- hints.push(`filter date range ${parts.join(" ")}`);
1463
+ if (pipeline === "v2") {
1464
+ const from = normalizeDateInput(modifications.dateRange.from);
1465
+ const to = normalizeDateInput(modifications.dateRange.to);
1466
+ if (from && to) {
1467
+ hints.push(`change date range to ${from} through ${to}`);
1468
+ } else if (from) {
1469
+ hints.push(`change start date to ${from}`);
1470
+ } else if (to) {
1471
+ hints.push(`change end date to ${to}`);
1472
+ }
1473
+ } else {
1474
+ const parts = [];
1475
+ if (modifications.dateRange.from) {
1476
+ parts.push(`from ${modifications.dateRange.from}`);
1477
+ }
1478
+ if (modifications.dateRange.to) {
1479
+ parts.push(`to ${modifications.dateRange.to}`);
1480
+ }
1481
+ if (parts.length > 0) {
1482
+ hints.push(`filter date range ${parts.join(" ")}`);
1483
+ }
1473
1484
  }
1474
1485
  }
1475
1486
  if (modifications.additionalInstructions) {
@@ -1480,6 +1491,67 @@ function buildModifiedQuestion(originalQuestion, modifications) {
1480
1491
  }
1481
1492
  return `${originalQuestion} (${hints.join(", ")})`;
1482
1493
  }
1494
+ var START_PARAM_KEY_REGEX = /(^|_)(start|from)(_|$)/i;
1495
+ var END_PARAM_KEY_REGEX = /(^|_)(end|to)(_|$)/i;
1496
+ var ISO_DATETIME_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?$/;
1497
+ var SQL_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
1498
+ var SQL_DATETIME_RE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
1499
+ function normalizeDateInput(value) {
1500
+ if (!value) return void 0;
1501
+ const trimmed = value.trim();
1502
+ if (!trimmed) return void 0;
1503
+ if (ISO_DATETIME_RE.test(trimmed)) {
1504
+ return trimmed.replace("T", " ").replace(/\.\d+Z?$/, "").replace(/Z$/, "");
1505
+ }
1506
+ return trimmed;
1507
+ }
1508
+ function keyLooksLikeDateBoundary(key, boundary) {
1509
+ return boundary === "start" ? START_PARAM_KEY_REGEX.test(key) : END_PARAM_KEY_REGEX.test(key);
1510
+ }
1511
+ function hasTimeComponent(value) {
1512
+ return typeof value === "string" && /\d{2}:\d{2}:\d{2}/.test(value);
1513
+ }
1514
+ function formatDateOverride(dateValue, boundary, existingValue) {
1515
+ if (SQL_DATE_RE.test(dateValue) && !hasTimeComponent(existingValue)) {
1516
+ return dateValue;
1517
+ }
1518
+ if (SQL_DATE_RE.test(dateValue)) {
1519
+ return `${dateValue} ${boundary === "start" ? "00:00:00" : "23:59:59"}`;
1520
+ }
1521
+ if (SQL_DATETIME_RE.test(dateValue)) {
1522
+ return dateValue;
1523
+ }
1524
+ return dateValue;
1525
+ }
1526
+ function normalizeGeneratedParamKey(param, index) {
1527
+ const nameCandidate = typeof param.name === "string" && param.name.trim() || typeof param.placeholder === "string" && param.placeholder.trim() || typeof param.position === "number" && String(param.position) || String(index + 1);
1528
+ return nameCandidate.replace(/[{}]/g, "").replace(/(.+):.*$/, "$1").replace(/^[:$]/, "").trim();
1529
+ }
1530
+ function applyDateRangeOverrides(dateRange, params, paramMetadata) {
1531
+ if (!dateRange) return;
1532
+ const from = normalizeDateInput(dateRange.from);
1533
+ const to = normalizeDateInput(dateRange.to);
1534
+ if (!from && !to) return;
1535
+ for (const [key, value] of Object.entries(params)) {
1536
+ if (from && keyLooksLikeDateBoundary(key, "start")) {
1537
+ params[key] = formatDateOverride(from, "start", value);
1538
+ }
1539
+ if (to && keyLooksLikeDateBoundary(key, "end")) {
1540
+ params[key] = formatDateOverride(to, "end", value);
1541
+ }
1542
+ }
1543
+ for (let i = 0; i < paramMetadata.length; i++) {
1544
+ const param = paramMetadata[i];
1545
+ if (!param) continue;
1546
+ const key = normalizeGeneratedParamKey(param, i);
1547
+ if (from && keyLooksLikeDateBoundary(key, "start")) {
1548
+ param.value = formatDateOverride(from, "start", param.value);
1549
+ }
1550
+ if (to && keyLooksLikeDateBoundary(key, "end")) {
1551
+ param.value = formatDateOverride(to, "end", param.value);
1552
+ }
1553
+ }
1554
+ }
1483
1555
  function buildVizHints(modifications) {
1484
1556
  const hints = {};
1485
1557
  if (modifications.kind) {
@@ -1524,6 +1596,7 @@ function resolveTenantId5(client, tenantId) {
1524
1596
  async function modifyChart(client, queryEngine, input, options, signal) {
1525
1597
  const tenantId = resolveTenantId5(client, options?.tenantId);
1526
1598
  const sessionId = import_node_crypto4.default.randomUUID();
1599
+ const querypanelSessionId = options?.querypanelSessionId ?? sessionId;
1527
1600
  const chartType = options?.chartType ?? "vega-lite";
1528
1601
  const hasSqlMods = !!input.sqlModifications;
1529
1602
  const hasVizMods = !!input.vizModifications;
@@ -1535,6 +1608,7 @@ async function modifyChart(client, queryEngine, input, options, signal) {
1535
1608
  let rationale;
1536
1609
  let queryId;
1537
1610
  let sqlChanged = false;
1611
+ let finalQuestion = input.question;
1538
1612
  const databaseName = input.database ?? queryEngine.getDefaultDatabase();
1539
1613
  if (!databaseName) {
1540
1614
  throw new Error(
@@ -1558,12 +1632,17 @@ async function modifyChart(client, queryEngine, input, options, signal) {
1558
1632
  } else if (hasSqlMods && !hasCustomSql) {
1559
1633
  const modifiedQuestion = buildModifiedQuestion(
1560
1634
  input.question,
1561
- input.sqlModifications
1635
+ input.sqlModifications,
1636
+ options?.pipeline
1562
1637
  );
1638
+ if (options?.pipeline === "v2") {
1639
+ finalQuestion = modifiedQuestion;
1640
+ }
1563
1641
  const queryResponse = await client.post(
1564
1642
  queryEndpoint,
1565
1643
  {
1566
1644
  question: modifiedQuestion,
1645
+ session_id: querypanelSessionId,
1567
1646
  previous_sql: input.sql,
1568
1647
  ...options?.maxRetry ? { max_retry: options.maxRetry } : {},
1569
1648
  ...tenantSettings ? { tenant_settings: tenantSettings } : {},
@@ -1579,6 +1658,13 @@ async function modifyChart(client, queryEngine, input, options, signal) {
1579
1658
  finalSql = queryResponse.sql;
1580
1659
  paramMetadata = Array.isArray(queryResponse.params) ? queryResponse.params : [];
1581
1660
  finalParams = queryEngine.mapGeneratedParams(paramMetadata);
1661
+ if (options?.pipeline === "v2") {
1662
+ applyDateRangeOverrides(
1663
+ input.sqlModifications?.dateRange,
1664
+ finalParams,
1665
+ paramMetadata
1666
+ );
1667
+ }
1582
1668
  rationale = queryResponse.rationale;
1583
1669
  queryId = queryResponse.queryId;
1584
1670
  sqlChanged = finalSql !== input.sql;
@@ -1601,7 +1687,7 @@ async function modifyChart(client, queryEngine, input, options, signal) {
1601
1687
  const vizspecResponse = await client.post(
1602
1688
  "/vizspec",
1603
1689
  {
1604
- question: input.question,
1690
+ question: finalQuestion,
1605
1691
  sql: finalSql,
1606
1692
  rationale,
1607
1693
  fields: execution.fields,
@@ -1626,7 +1712,7 @@ async function modifyChart(client, queryEngine, input, options, signal) {
1626
1712
  const chartResponse = await client.post(
1627
1713
  "/chart",
1628
1714
  {
1629
- question: input.question,
1715
+ question: finalQuestion,
1630
1716
  sql: finalSql,
1631
1717
  rationale,
1632
1718
  fields: execution.fields,
@@ -1659,6 +1745,7 @@ async function modifyChart(client, queryEngine, input, options, signal) {
1659
1745
  rationale,
1660
1746
  dialect: metadata?.dialect ?? "unknown",
1661
1747
  queryId,
1748
+ querypanelSessionId,
1662
1749
  rows,
1663
1750
  fields: execution.fields,
1664
1751
  chart,