@querypanel/node-sdk 1.0.43 → 1.0.44

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
@@ -32,7 +32,9 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  ClickHouseAdapter: () => ClickHouseAdapter,
34
34
  PostgresAdapter: () => PostgresAdapter,
35
+ QueryErrorCode: () => QueryErrorCode,
35
36
  QueryPanelSdkAPI: () => QueryPanelSdkAPI,
37
+ QueryPipelineError: () => QueryPipelineError,
36
38
  anonymizeResults: () => anonymizeResults
37
39
  });
38
40
  module.exports = __toCommonJS(index_exports);
@@ -881,6 +883,57 @@ var QueryEngine = class {
881
883
  }
882
884
  };
883
885
 
886
+ // src/errors.ts
887
+ var QueryErrorCode = {
888
+ // Moderation errors
889
+ MODERATION_FAILED: "MODERATION_FAILED",
890
+ // Guardrail errors
891
+ RELEVANCE_CHECK_FAILED: "RELEVANCE_CHECK_FAILED",
892
+ SECURITY_CHECK_FAILED: "SECURITY_CHECK_FAILED",
893
+ // SQL generation errors
894
+ SQL_GENERATION_FAILED: "SQL_GENERATION_FAILED",
895
+ // SQL validation errors
896
+ SQL_VALIDATION_FAILED: "SQL_VALIDATION_FAILED",
897
+ // Context retrieval errors
898
+ CONTEXT_RETRIEVAL_FAILED: "CONTEXT_RETRIEVAL_FAILED",
899
+ // General errors
900
+ INTERNAL_ERROR: "INTERNAL_ERROR",
901
+ AUTHENTICATION_REQUIRED: "AUTHENTICATION_REQUIRED",
902
+ VALIDATION_ERROR: "VALIDATION_ERROR"
903
+ };
904
+ var QueryPipelineError = class extends Error {
905
+ constructor(message, code, details) {
906
+ super(message);
907
+ this.code = code;
908
+ this.details = details;
909
+ this.name = "QueryPipelineError";
910
+ }
911
+ /**
912
+ * Check if this is a moderation error
913
+ */
914
+ isModeration() {
915
+ return this.code === QueryErrorCode.MODERATION_FAILED;
916
+ }
917
+ /**
918
+ * Check if this is a relevance error (question not related to database)
919
+ */
920
+ isRelevanceError() {
921
+ return this.code === QueryErrorCode.RELEVANCE_CHECK_FAILED;
922
+ }
923
+ /**
924
+ * Check if this is a security error (SQL injection, prompt injection, etc.)
925
+ */
926
+ isSecurityError() {
927
+ return this.code === QueryErrorCode.SECURITY_CHECK_FAILED;
928
+ }
929
+ /**
930
+ * Check if this is any guardrail error (relevance or security)
931
+ */
932
+ isGuardrailError() {
933
+ return this.isRelevanceError() || this.isSecurityError();
934
+ }
935
+ };
936
+
884
937
  // src/routes/charts.ts
885
938
  async function createChart(client, body, options, signal) {
886
939
  const tenantId = resolveTenantId(client, options?.tenantId);
@@ -917,15 +970,10 @@ async function listCharts(client, queryEngine, options, signal) {
917
970
  );
918
971
  if (options?.includeData) {
919
972
  response.data = await Promise.all(
920
- response.data.map(async (chart) => ({
921
- ...chart,
922
- vega_lite_spec: {
923
- ...chart.vega_lite_spec,
924
- data: {
925
- values: await executeChartQuery(queryEngine, chart, tenantId)
926
- }
927
- }
928
- }))
973
+ response.data.map(async (chart) => {
974
+ const rows = await executeChartQuery(queryEngine, chart, tenantId);
975
+ return hydrateChartWithData(chart, rows);
976
+ })
929
977
  );
930
978
  }
931
979
  return response;
@@ -939,15 +987,8 @@ async function getChart(client, queryEngine, id, options, signal) {
939
987
  options?.scopes,
940
988
  signal
941
989
  );
942
- return {
943
- ...chart,
944
- vega_lite_spec: {
945
- ...chart.vega_lite_spec,
946
- data: {
947
- values: await executeChartQuery(queryEngine, chart, tenantId)
948
- }
949
- }
950
- };
990
+ const rows = await executeChartQuery(queryEngine, chart, tenantId);
991
+ return hydrateChartWithData(chart, rows);
951
992
  }
952
993
  async function updateChart(client, id, body, options, signal) {
953
994
  const tenantId = resolveTenantId(client, options?.tenantId);
@@ -998,6 +1039,31 @@ async function executeChartQuery(queryEngine, chart, tenantId) {
998
1039
  return [];
999
1040
  }
1000
1041
  }
1042
+ function hydrateChartWithData(chart, rows) {
1043
+ const spec = chart.vega_lite_spec;
1044
+ if (chart.spec_type === "vizspec") {
1045
+ const existingData = spec.data ?? {};
1046
+ return {
1047
+ ...chart,
1048
+ vega_lite_spec: {
1049
+ ...spec,
1050
+ data: {
1051
+ ...existingData,
1052
+ values: rows
1053
+ }
1054
+ }
1055
+ };
1056
+ }
1057
+ return {
1058
+ ...chart,
1059
+ vega_lite_spec: {
1060
+ ...spec,
1061
+ data: {
1062
+ values: rows
1063
+ }
1064
+ }
1065
+ };
1066
+ }
1001
1067
 
1002
1068
  // src/routes/active-charts.ts
1003
1069
  async function createActiveChart(client, body, options, signal) {
@@ -1205,6 +1271,13 @@ async function ask(client, queryEngine, question, options, signal) {
1205
1271
  signal,
1206
1272
  sessionId
1207
1273
  );
1274
+ if (!queryResponse.success) {
1275
+ throw new QueryPipelineError(
1276
+ queryResponse.error || "Query generation failed",
1277
+ queryResponse.code || "INTERNAL_ERROR",
1278
+ queryResponse.details
1279
+ );
1280
+ }
1208
1281
  const dbName = queryResponse.database ?? options.database ?? queryEngine.getDefaultDatabase();
1209
1282
  if (!dbName) {
1210
1283
  throw new Error(
@@ -2049,7 +2122,9 @@ var QueryPanelSdkAPI = class {
2049
2122
  0 && (module.exports = {
2050
2123
  ClickHouseAdapter,
2051
2124
  PostgresAdapter,
2125
+ QueryErrorCode,
2052
2126
  QueryPanelSdkAPI,
2127
+ QueryPipelineError,
2053
2128
  anonymizeResults
2054
2129
  });
2055
2130
  //# sourceMappingURL=index.cjs.map