@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.d.cts CHANGED
@@ -169,6 +169,47 @@ declare class PostgresAdapter implements DatabaseAdapter {
169
169
  introspect(options?: IntrospectOptions): Promise<SchemaIntrospection>;
170
170
  }
171
171
 
172
+ /**
173
+ * Error codes for the query pipeline
174
+ * These match the server-side error codes returned in API responses
175
+ */
176
+ declare const QueryErrorCode: {
177
+ readonly MODERATION_FAILED: "MODERATION_FAILED";
178
+ readonly RELEVANCE_CHECK_FAILED: "RELEVANCE_CHECK_FAILED";
179
+ readonly SECURITY_CHECK_FAILED: "SECURITY_CHECK_FAILED";
180
+ readonly SQL_GENERATION_FAILED: "SQL_GENERATION_FAILED";
181
+ readonly SQL_VALIDATION_FAILED: "SQL_VALIDATION_FAILED";
182
+ readonly CONTEXT_RETRIEVAL_FAILED: "CONTEXT_RETRIEVAL_FAILED";
183
+ readonly INTERNAL_ERROR: "INTERNAL_ERROR";
184
+ readonly AUTHENTICATION_REQUIRED: "AUTHENTICATION_REQUIRED";
185
+ readonly VALIDATION_ERROR: "VALIDATION_ERROR";
186
+ };
187
+ type QueryErrorCode = (typeof QueryErrorCode)[keyof typeof QueryErrorCode];
188
+ /**
189
+ * Error thrown when the query pipeline fails
190
+ */
191
+ declare class QueryPipelineError extends Error {
192
+ readonly code: QueryErrorCode;
193
+ readonly details?: Record<string, unknown> | undefined;
194
+ constructor(message: string, code: QueryErrorCode, details?: Record<string, unknown> | undefined);
195
+ /**
196
+ * Check if this is a moderation error
197
+ */
198
+ isModeration(): boolean;
199
+ /**
200
+ * Check if this is a relevance error (question not related to database)
201
+ */
202
+ isRelevanceError(): boolean;
203
+ /**
204
+ * Check if this is a security error (SQL injection, prompt injection, etc.)
205
+ */
206
+ isSecurityError(): boolean;
207
+ /**
208
+ * Check if this is any guardrail error (relevance or security)
209
+ */
210
+ isGuardrailError(): boolean;
211
+ }
212
+
172
213
  type ParamValue = string | number | boolean | string[] | number[];
173
214
  type ParamRecord = Record<string, ParamValue>;
174
215
 
@@ -381,7 +422,7 @@ interface ContextDocument {
381
422
  interface ChartEnvelope {
382
423
  vegaLiteSpec?: Record<string, unknown> | null;
383
424
  vizSpec?: VizSpec | null;
384
- specType: 'vega-lite' | 'vizspec';
425
+ specType: "vega-lite" | "vizspec";
385
426
  notes: string | null;
386
427
  }
387
428
  interface AskOptions {
@@ -393,7 +434,7 @@ interface AskOptions {
393
434
  previousSql?: string;
394
435
  maxRetry?: number;
395
436
  chartMaxRetries?: number;
396
- chartType?: 'vega-lite' | 'vizspec';
437
+ chartType?: "vega-lite" | "vizspec";
397
438
  }
398
439
  interface AskResponse {
399
440
  sql: string;
@@ -977,4 +1018,4 @@ declare class QueryPanelSdkAPI {
977
1018
  }, signal?: AbortSignal): Promise<void>;
978
1019
  }
979
1020
 
980
- export { type ActiveChartCreateInput, type ActiveChartListOptions, type ActiveChartUpdateInput, type AggregateOp, type AskOptions, type AskResponse, type AxisField, type AxisFieldInput, type ChartCreateInput, type ChartEncoding, type ChartEnvelope, type ChartListOptions, type ChartModifyInput, type ChartModifyOptions, type ChartModifyResponse, type ChartSpec, type ChartType, type ChartUpdateInput, ClickHouseAdapter, type ClickHouseAdapterOptions, type ClickHouseClientFn, type ContextDocument, type DatabaseAdapter, type DatabaseDialect, type DateRangeInput, type FieldRef, type FieldRefInput, type FieldType, type IngestResponse, type MetricEncoding, type MetricField, type MetricSpec, type PaginatedResponse, type PaginationInfo, type PaginationQuery, type ParamRecord, type ParamValue, PostgresAdapter, type PostgresAdapterOptions, type PostgresClientFn, QueryPanelSdkAPI, type SchemaIntrospection, type SchemaSyncOptions, type SdkActiveChart, type SdkChart, type SqlModifications, type StackingMode, type TableColumn, type TableEncoding, type TableSpec, type TimeUnit, type VizModifications, type VizSpec, type VizSpecGenerateInput, type VizSpecGenerateOptions, type VizSpecResponse, anonymizeResults };
1021
+ export { type ActiveChartCreateInput, type ActiveChartListOptions, type ActiveChartUpdateInput, type AggregateOp, type AskOptions, type AskResponse, type AxisField, type AxisFieldInput, type ChartCreateInput, type ChartEncoding, type ChartEnvelope, type ChartListOptions, type ChartModifyInput, type ChartModifyOptions, type ChartModifyResponse, type ChartSpec, type ChartType, type ChartUpdateInput, ClickHouseAdapter, type ClickHouseAdapterOptions, type ClickHouseClientFn, type ContextDocument, type DatabaseAdapter, type DatabaseDialect, type DateRangeInput, type FieldRef, type FieldRefInput, type FieldType, type IngestResponse, type MetricEncoding, type MetricField, type MetricSpec, type PaginatedResponse, type PaginationInfo, type PaginationQuery, type ParamRecord, type ParamValue, PostgresAdapter, type PostgresAdapterOptions, type PostgresClientFn, QueryErrorCode, QueryErrorCode as QueryErrorCodeType, QueryPanelSdkAPI, QueryPipelineError, type SchemaIntrospection, type SchemaSyncOptions, type SdkActiveChart, type SdkChart, type SqlModifications, type StackingMode, type TableColumn, type TableEncoding, type TableSpec, type TimeUnit, type VizModifications, type VizSpec, type VizSpecGenerateInput, type VizSpecGenerateOptions, type VizSpecResponse, anonymizeResults };
package/dist/index.d.ts CHANGED
@@ -169,6 +169,47 @@ declare class PostgresAdapter implements DatabaseAdapter {
169
169
  introspect(options?: IntrospectOptions): Promise<SchemaIntrospection>;
170
170
  }
171
171
 
172
+ /**
173
+ * Error codes for the query pipeline
174
+ * These match the server-side error codes returned in API responses
175
+ */
176
+ declare const QueryErrorCode: {
177
+ readonly MODERATION_FAILED: "MODERATION_FAILED";
178
+ readonly RELEVANCE_CHECK_FAILED: "RELEVANCE_CHECK_FAILED";
179
+ readonly SECURITY_CHECK_FAILED: "SECURITY_CHECK_FAILED";
180
+ readonly SQL_GENERATION_FAILED: "SQL_GENERATION_FAILED";
181
+ readonly SQL_VALIDATION_FAILED: "SQL_VALIDATION_FAILED";
182
+ readonly CONTEXT_RETRIEVAL_FAILED: "CONTEXT_RETRIEVAL_FAILED";
183
+ readonly INTERNAL_ERROR: "INTERNAL_ERROR";
184
+ readonly AUTHENTICATION_REQUIRED: "AUTHENTICATION_REQUIRED";
185
+ readonly VALIDATION_ERROR: "VALIDATION_ERROR";
186
+ };
187
+ type QueryErrorCode = (typeof QueryErrorCode)[keyof typeof QueryErrorCode];
188
+ /**
189
+ * Error thrown when the query pipeline fails
190
+ */
191
+ declare class QueryPipelineError extends Error {
192
+ readonly code: QueryErrorCode;
193
+ readonly details?: Record<string, unknown> | undefined;
194
+ constructor(message: string, code: QueryErrorCode, details?: Record<string, unknown> | undefined);
195
+ /**
196
+ * Check if this is a moderation error
197
+ */
198
+ isModeration(): boolean;
199
+ /**
200
+ * Check if this is a relevance error (question not related to database)
201
+ */
202
+ isRelevanceError(): boolean;
203
+ /**
204
+ * Check if this is a security error (SQL injection, prompt injection, etc.)
205
+ */
206
+ isSecurityError(): boolean;
207
+ /**
208
+ * Check if this is any guardrail error (relevance or security)
209
+ */
210
+ isGuardrailError(): boolean;
211
+ }
212
+
172
213
  type ParamValue = string | number | boolean | string[] | number[];
173
214
  type ParamRecord = Record<string, ParamValue>;
174
215
 
@@ -381,7 +422,7 @@ interface ContextDocument {
381
422
  interface ChartEnvelope {
382
423
  vegaLiteSpec?: Record<string, unknown> | null;
383
424
  vizSpec?: VizSpec | null;
384
- specType: 'vega-lite' | 'vizspec';
425
+ specType: "vega-lite" | "vizspec";
385
426
  notes: string | null;
386
427
  }
387
428
  interface AskOptions {
@@ -393,7 +434,7 @@ interface AskOptions {
393
434
  previousSql?: string;
394
435
  maxRetry?: number;
395
436
  chartMaxRetries?: number;
396
- chartType?: 'vega-lite' | 'vizspec';
437
+ chartType?: "vega-lite" | "vizspec";
397
438
  }
398
439
  interface AskResponse {
399
440
  sql: string;
@@ -977,4 +1018,4 @@ declare class QueryPanelSdkAPI {
977
1018
  }, signal?: AbortSignal): Promise<void>;
978
1019
  }
979
1020
 
980
- export { type ActiveChartCreateInput, type ActiveChartListOptions, type ActiveChartUpdateInput, type AggregateOp, type AskOptions, type AskResponse, type AxisField, type AxisFieldInput, type ChartCreateInput, type ChartEncoding, type ChartEnvelope, type ChartListOptions, type ChartModifyInput, type ChartModifyOptions, type ChartModifyResponse, type ChartSpec, type ChartType, type ChartUpdateInput, ClickHouseAdapter, type ClickHouseAdapterOptions, type ClickHouseClientFn, type ContextDocument, type DatabaseAdapter, type DatabaseDialect, type DateRangeInput, type FieldRef, type FieldRefInput, type FieldType, type IngestResponse, type MetricEncoding, type MetricField, type MetricSpec, type PaginatedResponse, type PaginationInfo, type PaginationQuery, type ParamRecord, type ParamValue, PostgresAdapter, type PostgresAdapterOptions, type PostgresClientFn, QueryPanelSdkAPI, type SchemaIntrospection, type SchemaSyncOptions, type SdkActiveChart, type SdkChart, type SqlModifications, type StackingMode, type TableColumn, type TableEncoding, type TableSpec, type TimeUnit, type VizModifications, type VizSpec, type VizSpecGenerateInput, type VizSpecGenerateOptions, type VizSpecResponse, anonymizeResults };
1021
+ export { type ActiveChartCreateInput, type ActiveChartListOptions, type ActiveChartUpdateInput, type AggregateOp, type AskOptions, type AskResponse, type AxisField, type AxisFieldInput, type ChartCreateInput, type ChartEncoding, type ChartEnvelope, type ChartListOptions, type ChartModifyInput, type ChartModifyOptions, type ChartModifyResponse, type ChartSpec, type ChartType, type ChartUpdateInput, ClickHouseAdapter, type ClickHouseAdapterOptions, type ClickHouseClientFn, type ContextDocument, type DatabaseAdapter, type DatabaseDialect, type DateRangeInput, type FieldRef, type FieldRefInput, type FieldType, type IngestResponse, type MetricEncoding, type MetricField, type MetricSpec, type PaginatedResponse, type PaginationInfo, type PaginationQuery, type ParamRecord, type ParamValue, PostgresAdapter, type PostgresAdapterOptions, type PostgresClientFn, QueryErrorCode, QueryErrorCode as QueryErrorCodeType, QueryPanelSdkAPI, QueryPipelineError, type SchemaIntrospection, type SchemaSyncOptions, type SdkActiveChart, type SdkChart, type SqlModifications, type StackingMode, type TableColumn, type TableEncoding, type TableSpec, type TimeUnit, type VizModifications, type VizSpec, type VizSpecGenerateInput, type VizSpecGenerateOptions, type VizSpecResponse, anonymizeResults };
package/dist/index.js CHANGED
@@ -842,6 +842,57 @@ var QueryEngine = class {
842
842
  }
843
843
  };
844
844
 
845
+ // src/errors.ts
846
+ var QueryErrorCode = {
847
+ // Moderation errors
848
+ MODERATION_FAILED: "MODERATION_FAILED",
849
+ // Guardrail errors
850
+ RELEVANCE_CHECK_FAILED: "RELEVANCE_CHECK_FAILED",
851
+ SECURITY_CHECK_FAILED: "SECURITY_CHECK_FAILED",
852
+ // SQL generation errors
853
+ SQL_GENERATION_FAILED: "SQL_GENERATION_FAILED",
854
+ // SQL validation errors
855
+ SQL_VALIDATION_FAILED: "SQL_VALIDATION_FAILED",
856
+ // Context retrieval errors
857
+ CONTEXT_RETRIEVAL_FAILED: "CONTEXT_RETRIEVAL_FAILED",
858
+ // General errors
859
+ INTERNAL_ERROR: "INTERNAL_ERROR",
860
+ AUTHENTICATION_REQUIRED: "AUTHENTICATION_REQUIRED",
861
+ VALIDATION_ERROR: "VALIDATION_ERROR"
862
+ };
863
+ var QueryPipelineError = class extends Error {
864
+ constructor(message, code, details) {
865
+ super(message);
866
+ this.code = code;
867
+ this.details = details;
868
+ this.name = "QueryPipelineError";
869
+ }
870
+ /**
871
+ * Check if this is a moderation error
872
+ */
873
+ isModeration() {
874
+ return this.code === QueryErrorCode.MODERATION_FAILED;
875
+ }
876
+ /**
877
+ * Check if this is a relevance error (question not related to database)
878
+ */
879
+ isRelevanceError() {
880
+ return this.code === QueryErrorCode.RELEVANCE_CHECK_FAILED;
881
+ }
882
+ /**
883
+ * Check if this is a security error (SQL injection, prompt injection, etc.)
884
+ */
885
+ isSecurityError() {
886
+ return this.code === QueryErrorCode.SECURITY_CHECK_FAILED;
887
+ }
888
+ /**
889
+ * Check if this is any guardrail error (relevance or security)
890
+ */
891
+ isGuardrailError() {
892
+ return this.isRelevanceError() || this.isSecurityError();
893
+ }
894
+ };
895
+
845
896
  // src/routes/charts.ts
846
897
  async function createChart(client, body, options, signal) {
847
898
  const tenantId = resolveTenantId(client, options?.tenantId);
@@ -878,15 +929,10 @@ async function listCharts(client, queryEngine, options, signal) {
878
929
  );
879
930
  if (options?.includeData) {
880
931
  response.data = await Promise.all(
881
- response.data.map(async (chart) => ({
882
- ...chart,
883
- vega_lite_spec: {
884
- ...chart.vega_lite_spec,
885
- data: {
886
- values: await executeChartQuery(queryEngine, chart, tenantId)
887
- }
888
- }
889
- }))
932
+ response.data.map(async (chart) => {
933
+ const rows = await executeChartQuery(queryEngine, chart, tenantId);
934
+ return hydrateChartWithData(chart, rows);
935
+ })
890
936
  );
891
937
  }
892
938
  return response;
@@ -900,15 +946,8 @@ async function getChart(client, queryEngine, id, options, signal) {
900
946
  options?.scopes,
901
947
  signal
902
948
  );
903
- return {
904
- ...chart,
905
- vega_lite_spec: {
906
- ...chart.vega_lite_spec,
907
- data: {
908
- values: await executeChartQuery(queryEngine, chart, tenantId)
909
- }
910
- }
911
- };
949
+ const rows = await executeChartQuery(queryEngine, chart, tenantId);
950
+ return hydrateChartWithData(chart, rows);
912
951
  }
913
952
  async function updateChart(client, id, body, options, signal) {
914
953
  const tenantId = resolveTenantId(client, options?.tenantId);
@@ -959,6 +998,31 @@ async function executeChartQuery(queryEngine, chart, tenantId) {
959
998
  return [];
960
999
  }
961
1000
  }
1001
+ function hydrateChartWithData(chart, rows) {
1002
+ const spec = chart.vega_lite_spec;
1003
+ if (chart.spec_type === "vizspec") {
1004
+ const existingData = spec.data ?? {};
1005
+ return {
1006
+ ...chart,
1007
+ vega_lite_spec: {
1008
+ ...spec,
1009
+ data: {
1010
+ ...existingData,
1011
+ values: rows
1012
+ }
1013
+ }
1014
+ };
1015
+ }
1016
+ return {
1017
+ ...chart,
1018
+ vega_lite_spec: {
1019
+ ...spec,
1020
+ data: {
1021
+ values: rows
1022
+ }
1023
+ }
1024
+ };
1025
+ }
962
1026
 
963
1027
  // src/routes/active-charts.ts
964
1028
  async function createActiveChart(client, body, options, signal) {
@@ -1166,6 +1230,13 @@ async function ask(client, queryEngine, question, options, signal) {
1166
1230
  signal,
1167
1231
  sessionId
1168
1232
  );
1233
+ if (!queryResponse.success) {
1234
+ throw new QueryPipelineError(
1235
+ queryResponse.error || "Query generation failed",
1236
+ queryResponse.code || "INTERNAL_ERROR",
1237
+ queryResponse.details
1238
+ );
1239
+ }
1169
1240
  const dbName = queryResponse.database ?? options.database ?? queryEngine.getDefaultDatabase();
1170
1241
  if (!dbName) {
1171
1242
  throw new Error(
@@ -2009,7 +2080,9 @@ var QueryPanelSdkAPI = class {
2009
2080
  export {
2010
2081
  ClickHouseAdapter,
2011
2082
  PostgresAdapter,
2083
+ QueryErrorCode,
2012
2084
  QueryPanelSdkAPI,
2085
+ QueryPipelineError,
2013
2086
  anonymizeResults
2014
2087
  };
2015
2088
  //# sourceMappingURL=index.js.map