@querypanel/node-sdk 1.3.0-beta.2 → 1.3.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.
package/dist/index.d.cts CHANGED
@@ -360,10 +360,15 @@ interface ChartCreateInput {
360
360
  title: string;
361
361
  prompt?: string;
362
362
  description?: string;
363
- sql: string;
363
+ /**
364
+ * Required when `query_id` is not provided. Ignored (with `sql_params`) when
365
+ * `query_id` is present: the server saves the SQL and params it stored for that query.
366
+ */
367
+ sql?: string;
364
368
  sql_params?: Record<string, unknown>;
365
369
  vega_lite_spec: Record<string, unknown>;
366
370
  spec_type?: 'vega-lite' | 'vizspec';
371
+ /** Server-issued query id from ask() / modifyChart(). Preferred over `sql`. */
367
372
  query_id?: string;
368
373
  target_db?: string;
369
374
  }
@@ -373,6 +378,8 @@ interface ChartUpdateInput {
373
378
  description?: string;
374
379
  sql?: string;
375
380
  sql_params?: Record<string, unknown>;
381
+ /** When present, the server replaces sql / sql_params with the stored query. */
382
+ query_id?: string;
376
383
  vega_lite_spec?: Record<string, unknown>;
377
384
  spec_type?: 'vega-lite' | 'vizspec';
378
385
  target_db?: string;
@@ -917,11 +924,22 @@ interface VizModifications {
917
924
  */
918
925
  interface ChartModifyInput {
919
926
  /**
920
- * The SQL query to modify or re-execute.
927
+ * Server-issued id of the query to modify or re-execute.
928
+ * From ask() / modifyChart() response: response.queryId
929
+ * From saved chart: chart.query_id
930
+ *
931
+ * Preferred over `sql`: when present the SQL and params are loaded from the server
932
+ * (scoped to the authenticated tenant) and `sql` / `params` are ignored.
933
+ */
934
+ queryId?: string;
935
+ /**
936
+ * The SQL query to modify or re-execute. Required when `queryId` is not provided.
921
937
  * From ask() response: response.sql
922
938
  * From saved chart: chart.sql
939
+ *
940
+ * Kept for backward compatibility; prefer `queryId`.
923
941
  */
924
- sql: string;
942
+ sql?: string;
925
943
  /**
926
944
  * The original natural language question.
927
945
  * Used when regenerating SQL with modifications.
@@ -1313,7 +1331,13 @@ declare class QueryPanelSdkAPI {
1313
1331
  * to be attached (e.g. via attachPostgres) and options.tenantId.
1314
1332
  */
1315
1333
  runSqlForDashboard(input: {
1316
- sql: string;
1334
+ /**
1335
+ * Server-issued id from ask() / modifyChart(). Takes priority: the stored SQL
1336
+ * and params are executed and `sql` / `params` are ignored.
1337
+ */
1338
+ queryId?: string;
1339
+ /** Required when `queryId` is not provided (backward compatible raw SQL). */
1340
+ sql?: string;
1317
1341
  params?: Record<string, unknown>;
1318
1342
  database?: string;
1319
1343
  }, options: {
package/dist/index.d.ts CHANGED
@@ -360,10 +360,15 @@ interface ChartCreateInput {
360
360
  title: string;
361
361
  prompt?: string;
362
362
  description?: string;
363
- sql: string;
363
+ /**
364
+ * Required when `query_id` is not provided. Ignored (with `sql_params`) when
365
+ * `query_id` is present: the server saves the SQL and params it stored for that query.
366
+ */
367
+ sql?: string;
364
368
  sql_params?: Record<string, unknown>;
365
369
  vega_lite_spec: Record<string, unknown>;
366
370
  spec_type?: 'vega-lite' | 'vizspec';
371
+ /** Server-issued query id from ask() / modifyChart(). Preferred over `sql`. */
367
372
  query_id?: string;
368
373
  target_db?: string;
369
374
  }
@@ -373,6 +378,8 @@ interface ChartUpdateInput {
373
378
  description?: string;
374
379
  sql?: string;
375
380
  sql_params?: Record<string, unknown>;
381
+ /** When present, the server replaces sql / sql_params with the stored query. */
382
+ query_id?: string;
376
383
  vega_lite_spec?: Record<string, unknown>;
377
384
  spec_type?: 'vega-lite' | 'vizspec';
378
385
  target_db?: string;
@@ -917,11 +924,22 @@ interface VizModifications {
917
924
  */
918
925
  interface ChartModifyInput {
919
926
  /**
920
- * The SQL query to modify or re-execute.
927
+ * Server-issued id of the query to modify or re-execute.
928
+ * From ask() / modifyChart() response: response.queryId
929
+ * From saved chart: chart.query_id
930
+ *
931
+ * Preferred over `sql`: when present the SQL and params are loaded from the server
932
+ * (scoped to the authenticated tenant) and `sql` / `params` are ignored.
933
+ */
934
+ queryId?: string;
935
+ /**
936
+ * The SQL query to modify or re-execute. Required when `queryId` is not provided.
921
937
  * From ask() response: response.sql
922
938
  * From saved chart: chart.sql
939
+ *
940
+ * Kept for backward compatibility; prefer `queryId`.
923
941
  */
924
- sql: string;
942
+ sql?: string;
925
943
  /**
926
944
  * The original natural language question.
927
945
  * Used when regenerating SQL with modifications.
@@ -1313,7 +1331,13 @@ declare class QueryPanelSdkAPI {
1313
1331
  * to be attached (e.g. via attachPostgres) and options.tenantId.
1314
1332
  */
1315
1333
  runSqlForDashboard(input: {
1316
- sql: string;
1334
+ /**
1335
+ * Server-issued id from ask() / modifyChart(). Takes priority: the stored SQL
1336
+ * and params are executed and `sql` / `params` are ignored.
1337
+ */
1338
+ queryId?: string;
1339
+ /** Required when `queryId` is not provided (backward compatible raw SQL). */
1340
+ sql?: string;
1317
1341
  params?: Record<string, unknown>;
1318
1342
  database?: string;
1319
1343
  }, options: {
package/dist/index.js CHANGED
@@ -2147,6 +2147,25 @@ function anonymizeResults(rows) {
2147
2147
  });
2148
2148
  }
2149
2149
 
2150
+ // src/routes/queries.ts
2151
+ async function resolveQuery(client, queryEngine, queryId, options, signal) {
2152
+ const response = await client.get(
2153
+ `/v2/queries/${encodeURIComponent(queryId)}`,
2154
+ options.tenantId,
2155
+ options.userId,
2156
+ options.scopes,
2157
+ signal
2158
+ );
2159
+ const paramMetadata = Array.isArray(response.params) ? response.params : [];
2160
+ return {
2161
+ queryId: response.queryId,
2162
+ sql: response.sql,
2163
+ params: queryEngine.mapGeneratedParams(paramMetadata),
2164
+ paramMetadata,
2165
+ dialect: response.dialect
2166
+ };
2167
+ }
2168
+
2150
2169
  // src/routes/modify.ts
2151
2170
  function buildModifiedQuestion(originalQuestion, modifications, pipeline, hasSessionContext = false) {
2152
2171
  const hints = [];
@@ -2301,11 +2320,28 @@ async function modifyChart(client, queryEngine, input, options, signal) {
2301
2320
  const hasCustomSql = !!input.sqlModifications?.customSql;
2302
2321
  const pipeline = resolveQueryPipeline(options?.pipeline);
2303
2322
  const queryEndpoint = pipeline === "v2" ? "/v2/query" : "/query";
2304
- let finalSql = input.sql;
2323
+ let currentSql = input.sql;
2305
2324
  let finalParams = input.params ?? {};
2306
2325
  let paramMetadata = [];
2307
2326
  let rationale;
2308
2327
  let queryId;
2328
+ if (input.queryId) {
2329
+ const resolved = await resolveQuery(
2330
+ client,
2331
+ queryEngine,
2332
+ input.queryId,
2333
+ { tenantId, userId: options?.userId, scopes: options?.scopes },
2334
+ signal
2335
+ );
2336
+ currentSql = resolved.sql;
2337
+ finalParams = resolved.params;
2338
+ paramMetadata = resolved.paramMetadata;
2339
+ queryId = resolved.queryId;
2340
+ }
2341
+ if (!currentSql) {
2342
+ throw new Error("modifyChart requires either queryId or sql.");
2343
+ }
2344
+ let finalSql = currentSql;
2309
2345
  let sqlChanged = false;
2310
2346
  let finalQuestion = input.question;
2311
2347
  const databaseName = input.database ?? queryEngine.getDefaultDatabase();
@@ -2333,6 +2369,7 @@ async function modifyChart(client, queryEngine, input, options, signal) {
2333
2369
  finalSql = input.sqlModifications.customSql;
2334
2370
  finalParams = {};
2335
2371
  paramMetadata = [];
2372
+ queryId = void 0;
2336
2373
  sqlChanged = true;
2337
2374
  } else if (hasSqlMods && !hasCustomSql) {
2338
2375
  const modifiedQuestion = buildModifiedQuestion(
@@ -2349,7 +2386,7 @@ async function modifyChart(client, queryEngine, input, options, signal) {
2349
2386
  {
2350
2387
  question: modifiedQuestion,
2351
2388
  session_id: querypanelSessionId,
2352
- previous_sql: input.sql,
2389
+ previous_sql: currentSql,
2353
2390
  ...options?.maxRetry ? { max_retry: options.maxRetry } : {},
2354
2391
  ...tenantSettings ? { tenant_settings: tenantSettings } : {},
2355
2392
  ...databaseName ? { database: databaseName } : {},
@@ -2374,7 +2411,7 @@ async function modifyChart(client, queryEngine, input, options, signal) {
2374
2411
  }
2375
2412
  rationale = queryResponse.rationale;
2376
2413
  queryId = queryResponse.queryId;
2377
- sqlChanged = finalSql !== input.sql;
2414
+ sqlChanged = finalSql !== currentSql;
2378
2415
  }
2379
2416
  const execution = await queryEngine.validateAndExecute(
2380
2417
  finalSql,
@@ -2819,9 +2856,23 @@ var QueryPanelSdkAPI = class _QueryPanelSdkAPI {
2819
2856
  throw new Error("tenantId is required");
2820
2857
  }
2821
2858
  const databaseName = input.database ?? "db";
2822
- const params = input.params ?? {};
2859
+ let sql = input.sql;
2860
+ let params = input.params ?? {};
2861
+ if (input.queryId) {
2862
+ const resolved = await resolveQuery(
2863
+ this.client,
2864
+ this.queryEngine,
2865
+ input.queryId,
2866
+ { tenantId }
2867
+ );
2868
+ sql = resolved.sql;
2869
+ params = resolved.params;
2870
+ }
2871
+ if (!sql) {
2872
+ throw new Error("Either queryId or sql is required");
2873
+ }
2823
2874
  const result = await this.queryEngine.validateAndExecute(
2824
- input.sql,
2875
+ sql,
2825
2876
  params,
2826
2877
  databaseName,
2827
2878
  tenantId