@querypanel/node-sdk 1.0.28 → 1.0.29

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.ts CHANGED
@@ -1,11 +1,3 @@
1
- import { QueryParams, DataFormat } from '@clickhouse/client';
2
-
3
- /**
4
- * Simplified schema types aligned with backend IngestRequest format
5
- * Following Ousterhout's principle: "Define errors out of existence"
6
- * - Removed indexes, constraints, foreign keys, statistics
7
- * - Only collect what the backend needs for vectorization
8
- */
9
1
  type DatabaseKind = "clickhouse" | "postgres" | string;
10
2
  interface DatabaseIdentifier {
11
3
  kind: DatabaseKind;
@@ -13,24 +5,66 @@ interface DatabaseIdentifier {
13
5
  schema?: string;
14
6
  version?: string;
15
7
  }
8
+ interface ColumnStatistics {
9
+ compressedBytes?: number;
10
+ uncompressedBytes?: number;
11
+ distinctValues?: number;
12
+ }
16
13
  interface ColumnSchema {
17
14
  name: string;
18
15
  type: string;
19
16
  rawType?: string;
20
- isPrimaryKey: boolean;
17
+ nullable: boolean;
18
+ defaultKind?: string;
19
+ defaultExpression?: string;
21
20
  comment?: string;
21
+ isPrimaryKey: boolean;
22
+ isForeignKey: boolean;
23
+ foreignKeyTable?: string;
24
+ foreignKeyColumn?: string;
25
+ maxLength?: number;
26
+ precision?: number;
27
+ scale?: number;
28
+ codec?: string;
29
+ ttlExpression?: string;
30
+ statistics?: ColumnStatistics;
31
+ }
32
+ interface IndexSchema {
33
+ name: string;
34
+ columns: string[];
35
+ unique: boolean;
36
+ type: string;
37
+ definition?: string;
38
+ }
39
+ interface ConstraintSchema {
40
+ name: string;
41
+ type: "PRIMARY KEY" | "FOREIGN KEY" | "UNIQUE" | "CHECK" | string;
42
+ columns: string[];
43
+ referencedTable?: string;
44
+ referencedColumns?: string[];
45
+ definition?: string;
46
+ }
47
+ interface TableStatistics {
48
+ totalRows?: number;
49
+ totalBytes?: number;
50
+ uncompressedBytes?: number;
22
51
  }
23
52
  interface TableSchema {
24
53
  name: string;
25
54
  schema: string;
26
55
  type: "table" | "view" | string;
56
+ engine?: string;
27
57
  comment?: string;
58
+ statistics?: TableStatistics;
28
59
  columns: ColumnSchema[];
60
+ indexes: IndexSchema[];
61
+ constraints: ConstraintSchema[];
29
62
  }
30
63
  interface SchemaIntrospection {
31
64
  db: DatabaseIdentifier;
32
65
  tables: TableSchema[];
33
66
  introspectedAt: string;
67
+ metadata?: Record<string, unknown>;
34
68
  }
35
69
  interface IntrospectOptions {
36
70
  /** Optional allow-list of table names to introspect (schema-qualified or bare). */
@@ -74,11 +108,17 @@ interface DatabaseAdapter {
74
108
  close?(): Promise<void>;
75
109
  }
76
110
 
111
+ type QueryParams = {
112
+ query: string;
113
+ format?: string;
114
+ };
115
+ type ClickHouseSettings = Record<string, unknown>;
116
+ type Row = Record<string, unknown>;
77
117
  interface ClickHouseAdapterOptions {
78
118
  /** Optional logical database name used in introspection metadata. */
79
119
  database?: string;
80
120
  /** Override the default response format used for query execution. */
81
- defaultFormat?: DataFormat;
121
+ defaultFormat?: QueryParams["format"];
82
122
  /**
83
123
  * Optional database kind label. Defaults to "clickhouse" but allows
84
124
  * sub-classing/custom branding if needed.
@@ -94,12 +134,10 @@ interface ClickHouseAdapterOptions {
94
134
  type ClickHouseQueryResult = {
95
135
  json: () => Promise<unknown>;
96
136
  };
97
- type ClickHouseClientFn = (params: QueryParams) => Promise<ClickHouseQueryResult | Array<Record<string, unknown>> | Record<string, unknown>[]>;
98
- /**
99
- * Simplified ClickHouse adapter following IngestRequest format
100
- * Removed: indexes, constraints, statistics
101
- * Kept only: tables, columns (name, type, isPrimaryKey, comment)
102
- */
137
+ type ClickHouseClientFn = (params: QueryParams & {
138
+ query_params?: Record<string, unknown>;
139
+ clickhouse_settings?: ClickHouseSettings;
140
+ }) => Promise<ClickHouseQueryResult | Array<Record<string, unknown>> | Row[]>;
103
141
  declare class ClickHouseAdapter implements DatabaseAdapter {
104
142
  private readonly clientFn;
105
143
  private readonly databaseName;
@@ -110,11 +148,11 @@ declare class ClickHouseAdapter implements DatabaseAdapter {
110
148
  execute(sql: string, params?: Record<string, string | number | boolean | string[] | number[]>): Promise<DatabaseExecutionResult>;
111
149
  validate(sql: string, params?: Record<string, string | number | boolean | string[] | number[]>): Promise<void>;
112
150
  getDialect(): "clickhouse";
151
+ introspect(options?: IntrospectOptions): Promise<SchemaIntrospection>;
113
152
  /**
114
- * Simplified introspection: only collect table/column metadata for IngestRequest
115
- * No indexes, constraints, or statistics
153
+ * Validate that the SQL query only references allowed tables.
154
+ * This is a basic validation that extracts table-like patterns from the query.
116
155
  */
117
- introspect(options?: IntrospectOptions): Promise<SchemaIntrospection>;
118
156
  private validateQueryTables;
119
157
  close(): Promise<void>;
120
158
  private query;
@@ -141,11 +179,6 @@ interface PostgresAdapterOptions {
141
179
  */
142
180
  allowedTables?: string[];
143
181
  }
144
- /**
145
- * Simplified PostgreSQL adapter following IngestRequest format
146
- * Removed: indexes, constraints, foreign keys, statistics
147
- * Kept only: tables, columns (name, type, isPrimaryKey, comment)
148
- */
149
182
  declare class PostgresAdapter implements DatabaseAdapter {
150
183
  private readonly clientFn;
151
184
  private readonly databaseName;
@@ -154,24 +187,112 @@ declare class PostgresAdapter implements DatabaseAdapter {
154
187
  private readonly allowedTables?;
155
188
  constructor(clientFn: PostgresClientFn, options?: PostgresAdapterOptions);
156
189
  execute(sql: string, params?: Record<string, string | number | boolean | string[] | number[]>): Promise<DatabaseExecutionResult>;
190
+ /**
191
+ * Validate that the SQL query only references allowed tables.
192
+ * This is a basic validation that extracts table-like patterns from the query.
193
+ */
157
194
  private validateQueryTables;
158
195
  /**
159
196
  * Convert named params to positional array for PostgreSQL
160
197
  * PostgreSQL expects $1, $2, $3 in SQL and an array of values [val1, val2, val3]
198
+ *
199
+ * Supports two formats:
200
+ * 1. Numeric keys: { '1': 'value1', '2': 'value2' } - maps directly to $1, $2
201
+ * 2. Named keys: { 'tenant_id': 'value' } - values extracted in alphabetical order
202
+ * 3. Mixed: { '1': 'value1', 'tenant_id': 'value' } - numeric keys first, then named keys
161
203
  */
162
204
  private convertNamedToPositionalParams;
163
205
  validate(sql: string, params?: Record<string, string | number | boolean | string[] | number[]>): Promise<void>;
164
206
  getDialect(): "postgres";
165
- /**
166
- * Simplified introspection: only collect table/column metadata for IngestRequest
167
- * No indexes, constraints, or statistics
168
- */
169
207
  introspect(options?: IntrospectOptions): Promise<SchemaIntrospection>;
170
208
  }
171
209
 
172
210
  type ParamValue = string | number | boolean | string[] | number[];
173
211
  type ParamRecord = Record<string, ParamValue>;
174
-
212
+ interface ContextDocument {
213
+ source?: string;
214
+ pageContent: string;
215
+ metadata?: Record<string, unknown>;
216
+ score?: number;
217
+ }
218
+ interface ChartEnvelope {
219
+ vegaLiteSpec: Record<string, unknown> | null;
220
+ notes: string | null;
221
+ }
222
+ interface AskOptions {
223
+ tenantId?: string;
224
+ userId?: string;
225
+ scopes?: string[];
226
+ database?: string;
227
+ lastError?: string;
228
+ previousSql?: string;
229
+ maxRetry?: number;
230
+ chartMaxRetries?: number;
231
+ disableAutoSync?: boolean;
232
+ }
233
+ interface AskResponse {
234
+ sql: string;
235
+ params: ParamRecord;
236
+ paramMetadata: Array<Record<string, unknown>>;
237
+ rationale?: string;
238
+ dialect: string;
239
+ queryId?: string;
240
+ rows: Array<Record<string, unknown>>;
241
+ fields: string[];
242
+ chart: ChartEnvelope;
243
+ context?: ContextDocument[];
244
+ }
245
+ interface SchemaSyncOptions {
246
+ tenantId?: string;
247
+ userId?: string;
248
+ scopes?: string[];
249
+ tables?: string[];
250
+ force?: boolean;
251
+ }
252
+ interface KnowledgeBaseAnnotation {
253
+ id: string;
254
+ organization_id: string;
255
+ target_identifier: string;
256
+ content: string;
257
+ created_by: string;
258
+ updated_by: string;
259
+ created_at: string;
260
+ updated_at: string;
261
+ }
262
+ interface KnowledgeBaseAnnotationInput {
263
+ targetIdentifier: string;
264
+ content: string;
265
+ userId: string;
266
+ tenantId?: string;
267
+ }
268
+ interface KnowledgeBaseChunkTable {
269
+ table_name: string;
270
+ gold_sql?: Array<{
271
+ sql: string;
272
+ description?: string;
273
+ name?: string;
274
+ }>;
275
+ glossary?: Array<{
276
+ term: string;
277
+ definition: string;
278
+ }>;
279
+ }
280
+ interface KnowledgeBaseChunkRequest {
281
+ database: string;
282
+ dialect: string;
283
+ tables: KnowledgeBaseChunkTable[];
284
+ tenantId?: string;
285
+ }
286
+ interface KnowledgeBaseChunksResponse {
287
+ success: boolean;
288
+ message: string;
289
+ chunks: {
290
+ total: number;
291
+ gold_sql: number;
292
+ glossary: number;
293
+ chunks_with_annotations: number;
294
+ };
295
+ }
175
296
  interface SdkChart {
176
297
  id: string;
177
298
  title: string;
@@ -186,7 +307,19 @@ interface SdkChart {
186
307
  created_at: string | null;
187
308
  updated_at: string | null;
188
309
  active?: boolean;
189
- target_db?: string | null;
310
+ database?: string | null;
311
+ }
312
+ interface SdkActiveChart {
313
+ id: string;
314
+ chart_id: string;
315
+ order: number | null;
316
+ meta: Record<string, unknown> | null;
317
+ organization_id: string | null;
318
+ tenant_id: string | null;
319
+ user_id: string | null;
320
+ created_at: string | null;
321
+ updated_at: string | null;
322
+ chart?: SdkChart | null;
190
323
  }
191
324
  interface ChartCreateInput {
192
325
  title: string;
@@ -195,7 +328,7 @@ interface ChartCreateInput {
195
328
  sql_params?: Record<string, unknown>;
196
329
  vega_lite_spec: Record<string, unknown>;
197
330
  query_id?: string;
198
- target_db?: string;
331
+ database?: string;
199
332
  }
200
333
  interface ChartUpdateInput {
201
334
  title?: string;
@@ -203,7 +336,17 @@ interface ChartUpdateInput {
203
336
  sql?: string;
204
337
  sql_params?: Record<string, unknown>;
205
338
  vega_lite_spec?: Record<string, unknown>;
206
- target_db?: string;
339
+ database?: string;
340
+ }
341
+ interface ActiveChartCreateInput {
342
+ chart_id: string;
343
+ order?: number;
344
+ meta?: Record<string, unknown>;
345
+ }
346
+ interface ActiveChartUpdateInput {
347
+ chart_id?: string;
348
+ order?: number;
349
+ meta?: Record<string, unknown>;
207
350
  }
208
351
  interface PaginationQuery {
209
352
  page?: number;
@@ -236,33 +379,9 @@ interface ChartListOptions {
236
379
  updatedTo?: string;
237
380
  includeData?: boolean;
238
381
  }
239
-
240
- interface SdkActiveChart {
241
- id: string;
242
- chart_id: string;
243
- order: number | null;
244
- meta: Record<string, unknown> | null;
245
- organization_id: string | null;
246
- tenant_id: string | null;
247
- user_id: string | null;
248
- created_at: string | null;
249
- updated_at: string | null;
250
- chart?: SdkChart | null;
251
- }
252
- interface ActiveChartCreateInput {
253
- chart_id: string;
254
- order?: number;
255
- meta?: Record<string, unknown>;
256
- }
257
- interface ActiveChartUpdateInput {
258
- chart_id?: string;
259
- order?: number;
260
- meta?: Record<string, unknown>;
261
- }
262
382
  interface ActiveChartListOptions extends ChartListOptions {
263
383
  withData?: boolean;
264
384
  }
265
-
266
385
  interface IngestResponse {
267
386
  success: boolean;
268
387
  message: string;
@@ -273,56 +392,24 @@ interface IngestResponse {
273
392
  drift_detected?: boolean;
274
393
  skipped?: boolean;
275
394
  }
276
- interface SchemaSyncOptions {
395
+ interface RequestOptions {
277
396
  tenantId?: string;
278
397
  userId?: string;
279
398
  scopes?: string[];
280
- tables?: string[];
281
399
  }
282
-
283
- interface ContextDocument {
284
- source?: string;
285
- pageContent: string;
286
- metadata?: Record<string, unknown>;
287
- score?: number;
288
- }
289
- interface ChartEnvelope {
290
- vegaLiteSpec: Record<string, unknown> | null;
291
- notes: string | null;
292
- }
293
- interface AskOptions {
294
- tenantId?: string;
295
- userId?: string;
296
- scopes?: string[];
297
- database?: string;
298
- lastError?: string;
299
- previousSql?: string;
300
- maxRetry?: number;
301
- chartMaxRetries?: number;
302
- }
303
- interface AskResponse {
304
- sql: string;
305
- params: ParamRecord;
306
- paramMetadata: Array<Record<string, unknown>>;
307
- rationale?: string;
308
- dialect: string;
309
- queryId?: string;
310
- rows: Array<Record<string, unknown>>;
311
- fields: string[];
312
- chart: ChartEnvelope;
313
- context?: ContextDocument[];
314
- attempts?: number;
315
- }
316
- declare function anonymizeResults(rows: Array<Record<string, unknown>>): Array<Record<string, string>>;
317
-
318
- /**
319
- * Main SDK class - Thin orchestrator
320
- * Delegates to deep modules (ApiClient, QueryEngine, route modules)
321
- * Following Ousterhout's principle: "Simple interface hiding complexity"
322
- */
323
400
  declare class QueryPanelSdkAPI {
324
- private readonly client;
325
- private readonly queryEngine;
401
+ private readonly baseUrl;
402
+ private readonly privateKey;
403
+ private readonly organizationId;
404
+ private readonly defaultTenantId?;
405
+ private readonly additionalHeaders?;
406
+ private readonly fetchImpl;
407
+ private cachedPrivateKey?;
408
+ private databases;
409
+ private databaseMetadata;
410
+ private defaultDatabase?;
411
+ private lastSyncHashes;
412
+ private syncedDatabases;
326
413
  constructor(baseUrl: string, privateKey: string, organizationId: string, options?: {
327
414
  defaultTenantId?: string;
328
415
  additionalHeaders?: Record<string, string>;
@@ -342,47 +429,40 @@ declare class QueryPanelSdkAPI {
342
429
  enforceTenantIsolation?: boolean;
343
430
  }): void;
344
431
  attachDatabase(name: string, adapter: DatabaseAdapter): void;
345
- introspect(databaseName: string, tables?: string[]): Promise<SchemaIntrospection>;
346
432
  syncSchema(databaseName: string, options: SchemaSyncOptions, signal?: AbortSignal): Promise<IngestResponse>;
433
+ ingestKnowledgeBaseChunks(payload: KnowledgeBaseChunkRequest, options?: RequestOptions, signal?: AbortSignal): Promise<KnowledgeBaseChunksResponse>;
434
+ introspect(databaseName: string, tables?: string[]): Promise<SchemaIntrospection>;
347
435
  ask(question: string, options: AskOptions, signal?: AbortSignal): Promise<AskResponse>;
348
- createChart(body: ChartCreateInput, options?: {
349
- tenantId?: string;
350
- userId?: string;
351
- scopes?: string[];
352
- }, signal?: AbortSignal): Promise<SdkChart>;
436
+ upsertAnnotation(input: KnowledgeBaseAnnotationInput, options?: RequestOptions, signal?: AbortSignal): Promise<KnowledgeBaseAnnotation>;
437
+ listAnnotations(options?: RequestOptions, signal?: AbortSignal): Promise<KnowledgeBaseAnnotation[]>;
438
+ getAnnotation(targetIdentifier: string, options?: RequestOptions, signal?: AbortSignal): Promise<KnowledgeBaseAnnotation | null>;
439
+ deleteAnnotation(targetIdentifier: string, options?: RequestOptions, signal?: AbortSignal): Promise<void>;
440
+ createChart(body: ChartCreateInput, options?: RequestOptions, signal?: AbortSignal): Promise<SdkChart>;
353
441
  listCharts(options?: ChartListOptions, signal?: AbortSignal): Promise<PaginatedResponse<SdkChart>>;
354
- getChart(id: string, options?: {
355
- tenantId?: string;
356
- userId?: string;
357
- scopes?: string[];
358
- }, signal?: AbortSignal): Promise<SdkChart>;
359
- updateChart(id: string, body: ChartUpdateInput, options?: {
360
- tenantId?: string;
361
- userId?: string;
362
- scopes?: string[];
363
- }, signal?: AbortSignal): Promise<SdkChart>;
364
- deleteChart(id: string, options?: {
365
- tenantId?: string;
366
- userId?: string;
367
- scopes?: string[];
368
- }, signal?: AbortSignal): Promise<void>;
369
- createActiveChart(body: ActiveChartCreateInput, options?: {
370
- tenantId?: string;
371
- userId?: string;
372
- scopes?: string[];
373
- }, signal?: AbortSignal): Promise<SdkActiveChart>;
442
+ getChart(id: string, options?: RequestOptions, signal?: AbortSignal): Promise<SdkChart>;
443
+ updateChart(id: string, body: ChartUpdateInput, options?: RequestOptions, signal?: AbortSignal): Promise<SdkChart>;
444
+ deleteChart(id: string, options?: RequestOptions, signal?: AbortSignal): Promise<void>;
445
+ createActiveChart(body: ActiveChartCreateInput, options?: RequestOptions, signal?: AbortSignal): Promise<SdkActiveChart>;
374
446
  listActiveCharts(options?: ActiveChartListOptions, signal?: AbortSignal): Promise<PaginatedResponse<SdkActiveChart>>;
375
447
  getActiveChart(id: string, options?: ActiveChartListOptions, signal?: AbortSignal): Promise<SdkActiveChart>;
376
- updateActiveChart(id: string, body: ActiveChartUpdateInput, options?: {
377
- tenantId?: string;
378
- userId?: string;
379
- scopes?: string[];
380
- }, signal?: AbortSignal): Promise<SdkActiveChart>;
381
- deleteActiveChart(id: string, options?: {
382
- tenantId?: string;
383
- userId?: string;
384
- scopes?: string[];
385
- }, signal?: AbortSignal): Promise<void>;
448
+ updateActiveChart(id: string, body: ActiveChartUpdateInput, options?: RequestOptions, signal?: AbortSignal): Promise<SdkActiveChart>;
449
+ deleteActiveChart(id: string, options?: RequestOptions, signal?: AbortSignal): Promise<void>;
450
+ private getDatabase;
451
+ private ensureSchemasSynced;
452
+ private resolveTenantId;
453
+ private headers;
454
+ private request;
455
+ private get;
456
+ private post;
457
+ private put;
458
+ private delete;
459
+ private generateJWT;
460
+ private buildSchemaRequest;
461
+ private hashSchemaRequest;
462
+ private mapGeneratedParams;
463
+ private ensureTenantIsolation;
464
+ private runSafeQueryOnClient;
386
465
  }
466
+ declare function anonymizeResults(rows: Array<Record<string, unknown>>): Array<Record<string, string>>;
387
467
 
388
- export { type ActiveChartCreateInput, type ActiveChartListOptions, type ActiveChartUpdateInput, type AskOptions, type AskResponse, type ChartCreateInput, type ChartEnvelope, type ChartListOptions, type ChartUpdateInput, ClickHouseAdapter, type ClickHouseAdapterOptions, type ClickHouseClientFn, type ContextDocument, type DatabaseAdapter, type DatabaseDialect, type IngestResponse, type PaginatedResponse, type PaginationInfo, type PaginationQuery, type ParamRecord, type ParamValue, PostgresAdapter, type PostgresAdapterOptions, type PostgresClientFn, QueryPanelSdkAPI, type SchemaIntrospection, type SchemaSyncOptions, type SdkActiveChart, type SdkChart, anonymizeResults };
468
+ export { type ActiveChartCreateInput, type ActiveChartListOptions, type ActiveChartUpdateInput, type AskOptions, type AskResponse, type ChartCreateInput, type ChartEnvelope, type ChartListOptions, type ChartUpdateInput, ClickHouseAdapter, type ClickHouseAdapterOptions, type ClickHouseClientFn, type ContextDocument, type DatabaseAdapter, type DatabaseDialect, type IngestResponse, type KnowledgeBaseAnnotation, type KnowledgeBaseAnnotationInput, type KnowledgeBaseChunkRequest, type KnowledgeBaseChunkTable, type KnowledgeBaseChunksResponse, type PaginatedResponse, type PaginationInfo, type PaginationQuery, type ParamRecord, type ParamValue, PostgresAdapter, type PostgresAdapterOptions, type PostgresClientFn, QueryPanelSdkAPI, type SchemaIntrospection, type SchemaSyncOptions, type SdkActiveChart, type SdkChart, anonymizeResults };