plugin-elastic-report 0.7.0 → 0.9.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/client-v2.d.ts +2 -2
- package/client-v2.js +1 -1
- package/client.d.ts +2 -2
- package/client.js +1 -1
- package/{ai → dist/ai}/skills/elastic-report/SKILLS.md +52 -34
- package/dist/client-v2/851.35e416bfb3fb565f.js +10 -0
- package/dist/client-v2/877.81b10beb7b7f4bd7.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +2 -1
- package/dist/locale/en-US.json +213 -198
- package/dist/locale/vi-VN.json +213 -198
- package/dist/locale/zh-CN.json +213 -198
- package/dist/node_modules/@elastic/elasticsearch/package.json +1 -1
- package/dist/server/mcp/ElasticReportToolCatalog.js +6 -4
- package/dist/server/plugin.js +4 -4
- package/dist/server/report-block-tools.js +340 -0
- package/dist/server/services/ElasticReportService.js +12 -6
- package/dist/shared/constants.js +9 -0
- package/package.json +2 -3
- package/server.d.ts +2 -2
- package/server.js +1 -1
- package/src/ai/skills/elastic-report/SKILLS.md +52 -0
- package/src/client/index.tsx +34 -34
- package/src/client/workflow-instructions.tsx +44 -44
- package/src/client-v2/__tests__/elastic-report.test.tsx +879 -791
- package/src/client-v2/__tests__/kql-autocomplete.test.ts +178 -178
- package/src/client-v2/api.ts +320 -320
- package/src/client-v2/bus/DashboardFilterBus.ts +212 -212
- package/src/client-v2/components/ElasticBasicCharts.tsx +652 -652
- package/src/client-v2/components/ElasticFilterBar.tsx +372 -372
- package/src/client-v2/components/ElasticKqlInput.tsx +421 -421
- package/src/client-v2/components/ElasticReportBlock.tsx +306 -306
- package/src/client-v2/components/ElasticReportRenderer.tsx +1257 -1257
- package/src/client-v2/components/HighlightText.tsx +50 -50
- package/src/client-v2/components/ReportSelect.tsx +85 -85
- package/src/client-v2/index.tsx +14 -14
- package/src/client-v2/locale.ts +23 -23
- package/src/client-v2/models/ElasticFilterBarBlockModel.tsx +115 -115
- package/src/client-v2/models/ElasticReportBlockModel.tsx +118 -118
- package/src/client-v2/pages/ElasticReportManager.tsx +1453 -1453
- package/src/client-v2/plugin.tsx +107 -72
- package/src/client-v2/report-destination.tsx +126 -0
- package/src/client-v2/utils/kqlAutocomplete.ts +861 -861
- package/src/client-v2/workflow/instructions.tsx +149 -149
- package/src/index.ts +11 -11
- package/src/locale/en-US.json +213 -198
- package/src/locale/vi-VN.json +213 -198
- package/src/locale/zh-CN.json +213 -198
- package/src/server/__tests__/ai-blocks.integration.test.ts +292 -0
- package/src/server/__tests__/query-compiler.test.ts +620 -620
- package/src/server/__tests__/report-mcp-acl.integration.test.ts +645 -645
- package/src/server/acl-guard.ts +194 -194
- package/src/server/collections/es-report-data-views.ts +64 -64
- package/src/server/collections/es-reports.ts +80 -80
- package/src/server/index.ts +15 -15
- package/src/server/mcp/ElasticReportMcpServer.ts +391 -391
- package/src/server/mcp/ElasticReportToolCatalog.ts +667 -663
- package/src/server/plugin.ts +599 -599
- package/src/server/report-block-tools.ts +400 -0
- package/src/server/services/ElasticReportService.ts +494 -486
- package/src/server/services/ElasticsearchConnectionService.ts +728 -728
- package/src/server/services/ReportExportService.ts +282 -282
- package/src/server/services/ReportQueryCompiler.ts +1552 -1552
- package/src/server/workflow/ElasticReportQueryInstruction.ts +124 -124
- package/src/shared/constants.ts +39 -36
- package/src/shared/types.ts +272 -272
- package/dist/client-v2/851.142c93fc6d666b28.js +0 -10
- package/dist/client-v2/877.f4095a3dbcadf98f.js +0 -10
- package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv +0 -15
- package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv.cmd +0 -7
package/src/server/plugin.ts
CHANGED
|
@@ -1,599 +1,599 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { resolve } from 'path';
|
|
11
|
-
import type { Context, Next } from '@nocobase/actions';
|
|
12
|
-
import type WorkflowPlugin from '@nocobase/plugin-workflow';
|
|
13
|
-
import { Plugin, type InstallOptions } from '@nocobase/server';
|
|
14
|
-
import {
|
|
15
|
-
ACL_SNIPPET,
|
|
16
|
-
DATA_VIEWS_COLLECTION,
|
|
17
|
-
MCP_RELATIVE_PATH,
|
|
18
|
-
MCP_RESOURCE_NAME,
|
|
19
|
-
MCP_SERVER_NAME,
|
|
20
|
-
REPORTS_COLLECTION,
|
|
21
|
-
SERVICE_RESOURCE,
|
|
22
|
-
WORKFLOW_INSTRUCTION_TYPE,
|
|
23
|
-
} from '../shared/constants';
|
|
24
|
-
import type { ExportFormat, ID, ReportRuntimeOverrides } from '../shared/types';
|
|
25
|
-
import {
|
|
26
|
-
assertCanAccessDataView,
|
|
27
|
-
assertCanAccessReport,
|
|
28
|
-
assertCanManageReports,
|
|
29
|
-
canAccessReport,
|
|
30
|
-
normalizeRoleList,
|
|
31
|
-
} from './acl-guard';
|
|
32
|
-
import { ElasticReportMcpServer } from './mcp/ElasticReportMcpServer';
|
|
33
|
-
import {
|
|
34
|
-
import {
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
import {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
...
|
|
68
|
-
...
|
|
69
|
-
...
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (typeof value === '
|
|
76
|
-
|
|
77
|
-
err
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
public
|
|
99
|
-
public
|
|
100
|
-
public
|
|
101
|
-
|
|
102
|
-
private
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
this.
|
|
110
|
-
this.
|
|
111
|
-
this.
|
|
112
|
-
this.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
this.
|
|
122
|
-
this.
|
|
123
|
-
this.
|
|
124
|
-
this.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
await
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
await
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
:
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
:
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
:
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
:
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
const
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
const
|
|
336
|
-
const
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
const
|
|
352
|
-
const
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
let
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
const
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
const
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
.
|
|
419
|
-
.
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
...(
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
ctx.
|
|
439
|
-
ctx.
|
|
440
|
-
ctx.
|
|
441
|
-
ctx.
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
'
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
`${DATA_VIEWS_COLLECTION}:
|
|
494
|
-
`${DATA_VIEWS_COLLECTION}:
|
|
495
|
-
`${DATA_VIEWS_COLLECTION}:
|
|
496
|
-
`${DATA_VIEWS_COLLECTION}:
|
|
497
|
-
`${
|
|
498
|
-
`${REPORTS_COLLECTION}:
|
|
499
|
-
`${REPORTS_COLLECTION}:
|
|
500
|
-
`${REPORTS_COLLECTION}:
|
|
501
|
-
`${REPORTS_COLLECTION}:
|
|
502
|
-
`${
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
const
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
for (const [attributeName, attribute] of Object.entries(
|
|
581
|
-
const columnName = attribute.field || attributeName;
|
|
582
|
-
if (columns && columnName in columns) {
|
|
583
|
-
continue;
|
|
584
|
-
}
|
|
585
|
-
const columnDefinition = { ...attribute };
|
|
586
|
-
delete columnDefinition.Model;
|
|
587
|
-
delete columnDefinition.fieldName;
|
|
588
|
-
await queryInterface.addColumn(tableName, columnName, columnDefinition);
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
private async ensureSchema() {
|
|
593
|
-
for (const collectionName of this.schemaCollections) {
|
|
594
|
-
await this.ensureCollectionSchema(collectionName);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
export default PluginElasticReportServer;
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { resolve } from 'path';
|
|
11
|
+
import type { Context, Next } from '@nocobase/actions';
|
|
12
|
+
import type WorkflowPlugin from '@nocobase/plugin-workflow';
|
|
13
|
+
import { Plugin, type InstallOptions } from '@nocobase/server';
|
|
14
|
+
import {
|
|
15
|
+
ACL_SNIPPET,
|
|
16
|
+
DATA_VIEWS_COLLECTION,
|
|
17
|
+
MCP_RELATIVE_PATH,
|
|
18
|
+
MCP_RESOURCE_NAME,
|
|
19
|
+
MCP_SERVER_NAME,
|
|
20
|
+
REPORTS_COLLECTION,
|
|
21
|
+
SERVICE_RESOURCE,
|
|
22
|
+
WORKFLOW_INSTRUCTION_TYPE,
|
|
23
|
+
} from '../shared/constants';
|
|
24
|
+
import type { ExportFormat, ID, ReportRuntimeOverrides } from '../shared/types';
|
|
25
|
+
import {
|
|
26
|
+
assertCanAccessDataView,
|
|
27
|
+
assertCanAccessReport,
|
|
28
|
+
assertCanManageReports,
|
|
29
|
+
canAccessReport,
|
|
30
|
+
normalizeRoleList,
|
|
31
|
+
} from './acl-guard';
|
|
32
|
+
import { ElasticReportMcpServer } from './mcp/ElasticReportMcpServer';
|
|
33
|
+
import { createReportBlockTools } from './report-block-tools';
|
|
34
|
+
import { createElasticReportToolCatalog, type ElasticReportToolDefinition } from './mcp/ElasticReportToolCatalog';
|
|
35
|
+
import { ElasticsearchConnectionService } from './services/ElasticsearchConnectionService';
|
|
36
|
+
import { ElasticReportService, extractCurrentUserId, extractServerUserContext } from './services/ElasticReportService';
|
|
37
|
+
import { ReportExportService } from './services/ReportExportService';
|
|
38
|
+
import { ElasticReportQueryInstruction } from './workflow/ElasticReportQueryInstruction';
|
|
39
|
+
|
|
40
|
+
interface AIToolsManagerLike {
|
|
41
|
+
registerTools: (
|
|
42
|
+
tools: Array<{
|
|
43
|
+
scope: 'CUSTOM' | 'GENERAL' | 'SPECIFIED';
|
|
44
|
+
defaultPermission: 'ALLOW' | 'ASK';
|
|
45
|
+
execution: 'backend' | 'frontend';
|
|
46
|
+
introduction: { title: string; about: string };
|
|
47
|
+
definition: { name: string; description: string; schema: Record<string, unknown> };
|
|
48
|
+
invoke: (ctx: Context, args: Record<string, unknown>) => Promise<unknown>;
|
|
49
|
+
}>,
|
|
50
|
+
) => void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface AIMcpManagerLike {
|
|
54
|
+
registerMCP: (registration: Record<string, unknown>) => Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
58
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getActionPayload(ctx: Context): Record<string, unknown> {
|
|
62
|
+
const query = isRecord(ctx.request?.query) ? ctx.request.query : {};
|
|
63
|
+
const actionParams = isRecord(ctx.action?.params) ? ctx.action.params : {};
|
|
64
|
+
const body = isRecord(ctx.request?.body) ? ctx.request.body : {};
|
|
65
|
+
const paramValues = isRecord(actionParams.values) ? actionParams.values : {};
|
|
66
|
+
return {
|
|
67
|
+
...query,
|
|
68
|
+
...actionParams,
|
|
69
|
+
...body,
|
|
70
|
+
...paramValues,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function parseIdParam(value: unknown, label: string): ID {
|
|
75
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
76
|
+
if (typeof value === 'string' && value.trim()) return value.trim();
|
|
77
|
+
const err = new Error(`${label} is required`) as Error & { status?: number };
|
|
78
|
+
err.status = 400;
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parseOverridesParam(raw: unknown): ReportRuntimeOverrides | undefined {
|
|
83
|
+
if (isRecord(raw)) {
|
|
84
|
+
return raw as ReportRuntimeOverrides;
|
|
85
|
+
}
|
|
86
|
+
if (typeof raw === 'string' && raw.trim()) {
|
|
87
|
+
try {
|
|
88
|
+
const parsed = JSON.parse(raw);
|
|
89
|
+
return isRecord(parsed) ? (parsed as ReportRuntimeOverrides) : undefined;
|
|
90
|
+
} catch {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export class PluginElasticReportServer extends Plugin {
|
|
98
|
+
public connectionService!: ElasticsearchConnectionService;
|
|
99
|
+
public reportService!: ElasticReportService;
|
|
100
|
+
public exportService!: ReportExportService;
|
|
101
|
+
public mcpServer!: ElasticReportMcpServer;
|
|
102
|
+
private toolsCatalog: ElasticReportToolDefinition[] = [];
|
|
103
|
+
private readonly schemaCollections = [DATA_VIEWS_COLLECTION, REPORTS_COLLECTION];
|
|
104
|
+
|
|
105
|
+
async beforeLoad() {
|
|
106
|
+
await this.db.import({
|
|
107
|
+
directory: resolve(__dirname, 'collections'),
|
|
108
|
+
});
|
|
109
|
+
this.connectionService = new ElasticsearchConnectionService(this.app);
|
|
110
|
+
this.reportService = new ElasticReportService(this.app, this.connectionService);
|
|
111
|
+
this.exportService = new ReportExportService(this.connectionService, this.reportService);
|
|
112
|
+
this.toolsCatalog = createElasticReportToolCatalog(this.app, this.reportService);
|
|
113
|
+
this.mcpServer = new ElasticReportMcpServer({
|
|
114
|
+
version: this.options?.version || '0.8.0',
|
|
115
|
+
tools: this.toolsCatalog,
|
|
116
|
+
logger: this.app.logger,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async load() {
|
|
121
|
+
this.registerServiceResource();
|
|
122
|
+
this.registerMcpEndpoint();
|
|
123
|
+
this.registerAclRules();
|
|
124
|
+
this.registerWorkflowInstructions();
|
|
125
|
+
this.registerAITools();
|
|
126
|
+
await this.registerPluginMcpClientEntry();
|
|
127
|
+
|
|
128
|
+
this.app.on('beforeStop', async () => {
|
|
129
|
+
await this.connectionService?.close();
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private registerWorkflowInstructions() {
|
|
134
|
+
const workflow = this.app.pm.get('workflow') as WorkflowPlugin | undefined;
|
|
135
|
+
if (!workflow || typeof workflow.registerInstruction !== 'function') {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
workflow.registerInstruction(
|
|
139
|
+
WORKFLOW_INSTRUCTION_TYPE,
|
|
140
|
+
new ElasticReportQueryInstruction(workflow, this.reportService),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async install(_options?: InstallOptions) {
|
|
145
|
+
await this.ensureSchema();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async upgrade() {
|
|
149
|
+
await this.ensureSchema();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private getApiBasePath(): string {
|
|
153
|
+
const rawPrefix = this.app.resourcer?.options?.prefix ?? process.env.API_BASE_PATH ?? '/api';
|
|
154
|
+
const cleaned = rawPrefix.replace(/\/+/g, '/').replace(/\/$/, '');
|
|
155
|
+
return cleaned;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private getMcpEndpointPath(): string {
|
|
159
|
+
return `${this.getApiBasePath()}${MCP_RELATIVE_PATH}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private resolveInternalMcpUrl(): string {
|
|
163
|
+
const port = process.env.APP_PORT || '13000';
|
|
164
|
+
const host = process.env.ELASTIC_REPORT_MCP_INTERNAL_HOST || `http://127.0.0.1:${port}`;
|
|
165
|
+
return `${host.replace(/\/$/, '')}${this.getMcpEndpointPath()}`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private registerServiceResource() {
|
|
169
|
+
this.app.resourceManager.define({
|
|
170
|
+
name: SERVICE_RESOURCE,
|
|
171
|
+
actions: {
|
|
172
|
+
listConnections: async (ctx: Context, next: Next) => {
|
|
173
|
+
assertCanManageReports(this.app, ctx);
|
|
174
|
+
ctx.body = await this.reportService.listConnections();
|
|
175
|
+
await next();
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
testConnection: async (ctx: Context, next: Next) => {
|
|
179
|
+
assertCanManageReports(this.app, ctx);
|
|
180
|
+
const payload = getActionPayload(ctx);
|
|
181
|
+
const dataSourceKey = typeof payload.dataSourceKey === 'string' ? payload.dataSourceKey.trim() : '';
|
|
182
|
+
if (!dataSourceKey) {
|
|
183
|
+
ctx.throw(400, 'dataSourceKey is required');
|
|
184
|
+
}
|
|
185
|
+
const indexPattern =
|
|
186
|
+
typeof payload.indexPattern === 'string' && payload.indexPattern.trim() ? payload.indexPattern.trim() : '*';
|
|
187
|
+
try {
|
|
188
|
+
ctx.body = await this.reportService.testConnection(dataSourceKey, indexPattern);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
ctx.throw(400, error instanceof Error ? error.message : String(error));
|
|
191
|
+
}
|
|
192
|
+
await next();
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
listRoles: async (ctx: Context, next: Next) => {
|
|
196
|
+
assertCanManageReports(this.app, ctx);
|
|
197
|
+
ctx.body = await this.reportService.listAvailableRoles();
|
|
198
|
+
await next();
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
listIndices: async (ctx: Context, next: Next) => {
|
|
202
|
+
assertCanManageReports(this.app, ctx);
|
|
203
|
+
const payload = getActionPayload(ctx);
|
|
204
|
+
const dataSourceKey = typeof payload.dataSourceKey === 'string' ? payload.dataSourceKey.trim() : '';
|
|
205
|
+
if (!dataSourceKey) {
|
|
206
|
+
ctx.throw(400, 'dataSourceKey is required');
|
|
207
|
+
}
|
|
208
|
+
const indexPattern =
|
|
209
|
+
typeof payload.indexPattern === 'string' && payload.indexPattern.trim() ? payload.indexPattern.trim() : '*';
|
|
210
|
+
try {
|
|
211
|
+
ctx.body = await this.reportService.listIndices(dataSourceKey, indexPattern);
|
|
212
|
+
} catch (error) {
|
|
213
|
+
ctx.throw(400, error instanceof Error ? error.message : String(error));
|
|
214
|
+
}
|
|
215
|
+
await next();
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
inspectFields: async (ctx: Context, next: Next) => {
|
|
219
|
+
const payload = getActionPayload(ctx);
|
|
220
|
+
try {
|
|
221
|
+
if (payload.dataViewId !== undefined && payload.dataViewId !== null && String(payload.dataViewId).trim()) {
|
|
222
|
+
const dvId = parseIdParam(payload.dataViewId, 'dataViewId');
|
|
223
|
+
const refresh = payload.refresh === true || payload.refresh === 'true';
|
|
224
|
+
if (refresh) {
|
|
225
|
+
assertCanManageReports(this.app, ctx);
|
|
226
|
+
} else {
|
|
227
|
+
const dv = await this.reportService.getDataView(dvId);
|
|
228
|
+
assertCanAccessDataView(this.app, ctx, dv);
|
|
229
|
+
}
|
|
230
|
+
ctx.body = await this.reportService.inspectDataViewFields(dvId, refresh);
|
|
231
|
+
} else {
|
|
232
|
+
assertCanManageReports(this.app, ctx);
|
|
233
|
+
const dataSourceKey = typeof payload.dataSourceKey === 'string' ? payload.dataSourceKey.trim() : '';
|
|
234
|
+
const indexPattern = typeof payload.indexPattern === 'string' ? payload.indexPattern.trim() : '';
|
|
235
|
+
if (!dataSourceKey || !indexPattern) {
|
|
236
|
+
ctx.throw(400, 'Either dataViewId or (dataSourceKey + indexPattern) is required');
|
|
237
|
+
}
|
|
238
|
+
const fields = await this.reportService.inspectFieldsBySource(dataSourceKey, indexPattern);
|
|
239
|
+
ctx.body = { fields };
|
|
240
|
+
}
|
|
241
|
+
} catch (error) {
|
|
242
|
+
const status = (error as { status?: number } | undefined)?.status ?? 400;
|
|
243
|
+
ctx.throw(status, error instanceof Error ? error.message : String(error));
|
|
244
|
+
}
|
|
245
|
+
await next();
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
upsertDataView: async (ctx: Context, next: Next) => {
|
|
249
|
+
assertCanManageReports(this.app, ctx);
|
|
250
|
+
const payload = getActionPayload(ctx);
|
|
251
|
+
try {
|
|
252
|
+
ctx.body = await this.reportService.upsertDataView(
|
|
253
|
+
{
|
|
254
|
+
...(payload.id !== undefined && payload.id !== null && String(payload.id).trim() !== ''
|
|
255
|
+
? { id: parseIdParam(payload.id, 'id') }
|
|
256
|
+
: {}),
|
|
257
|
+
title: typeof payload.title === 'string' ? payload.title : '',
|
|
258
|
+
dataSourceKey: typeof payload.dataSourceKey === 'string' ? payload.dataSourceKey : '',
|
|
259
|
+
indexPattern: typeof payload.indexPattern === 'string' ? payload.indexPattern : '',
|
|
260
|
+
timeField: typeof payload.timeField === 'string' ? payload.timeField : null,
|
|
261
|
+
description: typeof payload.description === 'string' ? payload.description : null,
|
|
262
|
+
...(payload.allowedRoles !== undefined
|
|
263
|
+
? { allowedRoles: normalizeRoleList(payload.allowedRoles) }
|
|
264
|
+
: {}),
|
|
265
|
+
},
|
|
266
|
+
extractCurrentUserId(ctx),
|
|
267
|
+
);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
const status = (error as { status?: number } | undefined)?.status ?? 400;
|
|
270
|
+
ctx.throw(status, error instanceof Error ? error.message : String(error));
|
|
271
|
+
}
|
|
272
|
+
await next();
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
previewReport: async (ctx: Context, next: Next) => {
|
|
276
|
+
assertCanManageReports(this.app, ctx);
|
|
277
|
+
const payload = getActionPayload(ctx);
|
|
278
|
+
try {
|
|
279
|
+
const dataViewId = parseIdParam(payload.dataViewId, 'dataViewId');
|
|
280
|
+
ctx.body = await this.reportService.previewReport({
|
|
281
|
+
dataViewId,
|
|
282
|
+
spec: payload.spec,
|
|
283
|
+
overrides: parseOverridesParam(payload.overrides),
|
|
284
|
+
userContext: extractServerUserContext(ctx),
|
|
285
|
+
});
|
|
286
|
+
} catch (error) {
|
|
287
|
+
const status = (error as { status?: number } | undefined)?.status ?? 400;
|
|
288
|
+
ctx.throw(status, error instanceof Error ? error.message : String(error));
|
|
289
|
+
}
|
|
290
|
+
await next();
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
saveReport: async (ctx: Context, next: Next) => {
|
|
294
|
+
assertCanManageReports(this.app, ctx);
|
|
295
|
+
const payload = getActionPayload(ctx);
|
|
296
|
+
try {
|
|
297
|
+
ctx.body = await this.reportService.saveReport(
|
|
298
|
+
{
|
|
299
|
+
...(payload.id !== undefined && payload.id !== null && String(payload.id).trim() !== ''
|
|
300
|
+
? { id: parseIdParam(payload.id, 'id') }
|
|
301
|
+
: {}),
|
|
302
|
+
title: typeof payload.title === 'string' ? payload.title : '',
|
|
303
|
+
description: typeof payload.description === 'string' ? payload.description : null,
|
|
304
|
+
dataViewId: parseIdParam(payload.dataViewId, 'dataViewId'),
|
|
305
|
+
spec: payload.spec,
|
|
306
|
+
published: payload.published !== false,
|
|
307
|
+
...(payload.allowedRoles !== undefined
|
|
308
|
+
? { allowedRoles: normalizeRoleList(payload.allowedRoles) }
|
|
309
|
+
: {}),
|
|
310
|
+
},
|
|
311
|
+
extractCurrentUserId(ctx),
|
|
312
|
+
);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
const status = (error as { status?: number } | undefined)?.status ?? 400;
|
|
315
|
+
ctx.throw(status, error instanceof Error ? error.message : String(error));
|
|
316
|
+
}
|
|
317
|
+
await next();
|
|
318
|
+
},
|
|
319
|
+
|
|
320
|
+
listPublishedReports: async (ctx: Context, next: Next) => {
|
|
321
|
+
const payload = getActionPayload(ctx);
|
|
322
|
+
const search = typeof payload.search === 'string' ? payload.search : undefined;
|
|
323
|
+
const publishedReports = await this.reportService.listReports({
|
|
324
|
+
publishedOnly: true,
|
|
325
|
+
search,
|
|
326
|
+
});
|
|
327
|
+
ctx.body = publishedReports.filter((report) => canAccessReport(this.app, ctx, report, report.dataView));
|
|
328
|
+
await next();
|
|
329
|
+
},
|
|
330
|
+
|
|
331
|
+
getReportMeta: async (ctx: Context, next: Next) => {
|
|
332
|
+
const payload = getActionPayload(ctx);
|
|
333
|
+
const rawId = payload.reportId ?? ctx.action?.params?.filterByTk;
|
|
334
|
+
try {
|
|
335
|
+
const reportId = parseIdParam(rawId, 'reportId');
|
|
336
|
+
const report = await this.reportService.getReport(reportId);
|
|
337
|
+
const { dataView, fields } = await this.reportService.inspectDataViewFields(report.dataViewId, false);
|
|
338
|
+
assertCanAccessReport(this.app, ctx, report, dataView);
|
|
339
|
+
ctx.body = { report, dataView, fields };
|
|
340
|
+
} catch (error) {
|
|
341
|
+
const status = (error as { status?: number } | undefined)?.status ?? 400;
|
|
342
|
+
ctx.throw(status, error instanceof Error ? error.message : String(error));
|
|
343
|
+
}
|
|
344
|
+
await next();
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
runReport: async (ctx: Context, next: Next) => {
|
|
348
|
+
const payload = getActionPayload(ctx);
|
|
349
|
+
const rawId = payload.reportId ?? ctx.action?.params?.filterByTk;
|
|
350
|
+
try {
|
|
351
|
+
const reportId = parseIdParam(rawId, 'reportId');
|
|
352
|
+
const report = await this.reportService.getReport(reportId);
|
|
353
|
+
const dataView = report.dataView ?? (await this.reportService.getDataView(report.dataViewId));
|
|
354
|
+
assertCanAccessReport(this.app, ctx, report, dataView);
|
|
355
|
+
ctx.body = await this.reportService.runSavedReport({
|
|
356
|
+
reportId,
|
|
357
|
+
overrides: parseOverridesParam(payload.overrides),
|
|
358
|
+
requirePublished: false,
|
|
359
|
+
userContext: extractServerUserContext(ctx),
|
|
360
|
+
});
|
|
361
|
+
} catch (error) {
|
|
362
|
+
const status = (error as { status?: number } | undefined)?.status ?? 400;
|
|
363
|
+
ctx.throw(status, error instanceof Error ? error.message : String(error));
|
|
364
|
+
}
|
|
365
|
+
await next();
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
inspectTopValues: async (ctx: Context, next: Next) => {
|
|
369
|
+
const payload = getActionPayload(ctx);
|
|
370
|
+
try {
|
|
371
|
+
const field = typeof payload.field === 'string' ? payload.field.trim() : '';
|
|
372
|
+
if (!field) {
|
|
373
|
+
ctx.throw(400, 'field is required');
|
|
374
|
+
}
|
|
375
|
+
let dataViewId: ID;
|
|
376
|
+
let reportId: ID | undefined;
|
|
377
|
+
if (payload.reportId !== undefined && payload.reportId !== null && String(payload.reportId).trim() !== '') {
|
|
378
|
+
reportId = parseIdParam(payload.reportId, 'reportId');
|
|
379
|
+
const report = await this.reportService.getReport(reportId);
|
|
380
|
+
const dataView = report.dataView ?? (await this.reportService.getDataView(report.dataViewId));
|
|
381
|
+
assertCanAccessReport(this.app, ctx, report, dataView);
|
|
382
|
+
dataViewId = report.dataViewId;
|
|
383
|
+
} else {
|
|
384
|
+
dataViewId = parseIdParam(payload.dataViewId, 'dataViewId');
|
|
385
|
+
const dataView = await this.reportService.getDataView(dataViewId);
|
|
386
|
+
assertCanAccessDataView(this.app, ctx, dataView);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
ctx.body = await this.reportService.inspectFieldTopValues({
|
|
390
|
+
dataViewId,
|
|
391
|
+
...(reportId !== undefined ? { reportId } : {}),
|
|
392
|
+
field,
|
|
393
|
+
size: typeof payload.size === 'number' ? payload.size : Number(payload.size) || 10,
|
|
394
|
+
overrides: parseOverridesParam(payload.overrides),
|
|
395
|
+
userContext: extractServerUserContext(ctx),
|
|
396
|
+
});
|
|
397
|
+
} catch (error) {
|
|
398
|
+
const status = (error as { status?: number } | undefined)?.status ?? 400;
|
|
399
|
+
ctx.throw(status, error instanceof Error ? error.message : String(error));
|
|
400
|
+
}
|
|
401
|
+
await next();
|
|
402
|
+
},
|
|
403
|
+
|
|
404
|
+
exportReport: async (ctx: Context, next: Next) => {
|
|
405
|
+
const payload = getActionPayload(ctx);
|
|
406
|
+
const rawId = payload.reportId ?? ctx.action?.params?.filterByTk;
|
|
407
|
+
try {
|
|
408
|
+
const reportId = parseIdParam(rawId, 'reportId');
|
|
409
|
+
const report = await this.reportService.getReport(reportId);
|
|
410
|
+
const dataView = report.dataView ?? (await this.reportService.getDataView(report.dataViewId));
|
|
411
|
+
assertCanAccessReport(this.app, ctx, report, dataView);
|
|
412
|
+
|
|
413
|
+
const format: ExportFormat = payload.format === 'ndjson' ? 'ndjson' : 'csv';
|
|
414
|
+
const columns = Array.isArray(payload.columns)
|
|
415
|
+
? payload.columns.filter((c): c is string => typeof c === 'string' && Boolean(c.trim()))
|
|
416
|
+
: typeof payload.columns === 'string' && payload.columns.trim()
|
|
417
|
+
? payload.columns
|
|
418
|
+
.split(',')
|
|
419
|
+
.map((c) => c.trim())
|
|
420
|
+
.filter(Boolean)
|
|
421
|
+
: undefined;
|
|
422
|
+
const maxRows =
|
|
423
|
+
typeof payload.maxRows === 'number'
|
|
424
|
+
? payload.maxRows
|
|
425
|
+
: payload.maxRows !== undefined
|
|
426
|
+
? Number(payload.maxRows)
|
|
427
|
+
: undefined;
|
|
428
|
+
|
|
429
|
+
const { stream, filename, contentType } = await this.exportService.createExportStream({
|
|
430
|
+
reportId,
|
|
431
|
+
format,
|
|
432
|
+
overrides: parseOverridesParam(payload.overrides),
|
|
433
|
+
...(columns ? { columns } : {}),
|
|
434
|
+
...(maxRows !== undefined && Number.isFinite(maxRows) ? { maxRows } : {}),
|
|
435
|
+
userContext: extractServerUserContext(ctx),
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
ctx.withoutDataWrapping = true;
|
|
439
|
+
ctx.status = 200;
|
|
440
|
+
ctx.type = contentType;
|
|
441
|
+
ctx.set('Content-Disposition', `attachment; filename="${filename}"`);
|
|
442
|
+
ctx.body = stream;
|
|
443
|
+
} catch (error) {
|
|
444
|
+
const status = (error as { status?: number } | undefined)?.status ?? 400;
|
|
445
|
+
ctx.throw(status, error instanceof Error ? error.message : String(error));
|
|
446
|
+
}
|
|
447
|
+
await next();
|
|
448
|
+
},
|
|
449
|
+
|
|
450
|
+
getMcpInfo: async (ctx: Context, next: Next) => {
|
|
451
|
+
ctx.body = {
|
|
452
|
+
serverName: MCP_SERVER_NAME,
|
|
453
|
+
endpointPath: this.getMcpEndpointPath(),
|
|
454
|
+
internalUrl: this.resolveInternalMcpUrl(),
|
|
455
|
+
useUserContext: true,
|
|
456
|
+
tools: this.mcpServer.getToolsMetadata(),
|
|
457
|
+
};
|
|
458
|
+
await next();
|
|
459
|
+
},
|
|
460
|
+
},
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
private registerMcpEndpoint() {
|
|
465
|
+
const mcpActionHandler = async (ctx: Context) => {
|
|
466
|
+
await this.mcpServer.handleRequest(ctx);
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
this.app.resourceManager.define({
|
|
470
|
+
name: MCP_RESOURCE_NAME,
|
|
471
|
+
actions: {
|
|
472
|
+
list: mcpActionHandler,
|
|
473
|
+
create: mcpActionHandler,
|
|
474
|
+
},
|
|
475
|
+
only: ['list', 'create'],
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private registerAclRules() {
|
|
480
|
+
// Viewers on pages can list published reports, inspect report metadata/top-values, run, and export published reports.
|
|
481
|
+
this.app.acl.allow(
|
|
482
|
+
SERVICE_RESOURCE,
|
|
483
|
+
['listPublishedReports', 'getReportMeta', 'runReport', 'inspectTopValues', 'exportReport'],
|
|
484
|
+
'loggedIn',
|
|
485
|
+
);
|
|
486
|
+
// Dedicated MCP endpoint requires authentication; tool-level ACL enforces read vs manage permissions.
|
|
487
|
+
this.app.acl.allow(MCP_RESOURCE_NAME, '*', 'loggedIn');
|
|
488
|
+
|
|
489
|
+
// Full studio & data-view management is grouped under the plugin settings snippet.
|
|
490
|
+
this.app.acl.registerSnippet({
|
|
491
|
+
name: ACL_SNIPPET,
|
|
492
|
+
actions: [
|
|
493
|
+
`${DATA_VIEWS_COLLECTION}:list`,
|
|
494
|
+
`${DATA_VIEWS_COLLECTION}:get`,
|
|
495
|
+
`${DATA_VIEWS_COLLECTION}:create`,
|
|
496
|
+
`${DATA_VIEWS_COLLECTION}:update`,
|
|
497
|
+
`${DATA_VIEWS_COLLECTION}:destroy`,
|
|
498
|
+
`${REPORTS_COLLECTION}:list`,
|
|
499
|
+
`${REPORTS_COLLECTION}:get`,
|
|
500
|
+
`${REPORTS_COLLECTION}:create`,
|
|
501
|
+
`${REPORTS_COLLECTION}:update`,
|
|
502
|
+
`${REPORTS_COLLECTION}:destroy`,
|
|
503
|
+
`${SERVICE_RESOURCE}:*`,
|
|
504
|
+
],
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
private registerAITools() {
|
|
509
|
+
const toolsManager = (this.app as unknown as { aiManager?: { toolsManager?: AIToolsManagerLike } }).aiManager
|
|
510
|
+
?.toolsManager;
|
|
511
|
+
if (!toolsManager) {
|
|
512
|
+
this.app.logger?.info?.(
|
|
513
|
+
'[plugin-elastic-report] aiManager.toolsManager is not present; skipping direct AI tool registration',
|
|
514
|
+
);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
toolsManager.registerTools(
|
|
519
|
+
[...this.toolsCatalog, ...createReportBlockTools(this.app, this.reportService)].map((tool) => ({
|
|
520
|
+
scope: 'CUSTOM',
|
|
521
|
+
defaultPermission: tool.defaultPermission,
|
|
522
|
+
execution: 'backend',
|
|
523
|
+
introduction: {
|
|
524
|
+
title: tool.title,
|
|
525
|
+
about: tool.description,
|
|
526
|
+
},
|
|
527
|
+
definition: {
|
|
528
|
+
name: `${MCP_SERVER_NAME}-${tool.name}`,
|
|
529
|
+
description: tool.description,
|
|
530
|
+
schema: tool.inputSchema,
|
|
531
|
+
},
|
|
532
|
+
invoke: async (ctx: Context, args: Record<string, unknown>) => {
|
|
533
|
+
return tool.execute(isRecord(args) ? args : {}, ctx);
|
|
534
|
+
},
|
|
535
|
+
})),
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
private async registerPluginMcpClientEntry() {
|
|
540
|
+
const mcpManager = (this.app as unknown as { aiManager?: { mcpManager?: AIMcpManagerLike } }).aiManager?.mcpManager;
|
|
541
|
+
if (!mcpManager || typeof mcpManager.registerMCP !== 'function') {
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
try {
|
|
546
|
+
await mcpManager.registerMCP({
|
|
547
|
+
[MCP_SERVER_NAME]: {
|
|
548
|
+
title: 'Elastic Report MCP',
|
|
549
|
+
description: 'Dedicated MCP server for Kibana-style Elasticsearch reports and Data Views',
|
|
550
|
+
transport: 'http',
|
|
551
|
+
url: this.resolveInternalMcpUrl(),
|
|
552
|
+
headers: {
|
|
553
|
+
Authorization: 'Bearer {{ctx.request.token}}',
|
|
554
|
+
'X-Role': '{{ctx.request.headers.x-role}}',
|
|
555
|
+
},
|
|
556
|
+
useUserContext: true,
|
|
557
|
+
enabled: true,
|
|
558
|
+
},
|
|
559
|
+
});
|
|
560
|
+
} catch (error) {
|
|
561
|
+
this.app.logger?.warn?.('[plugin-elastic-report] Could not pre-register MCP client entry:', error);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
private async ensureCollectionSchema(collectionName: string) {
|
|
566
|
+
const collection = this.db.getCollection(collectionName);
|
|
567
|
+
if (!collection) return;
|
|
568
|
+
|
|
569
|
+
const queryInterface = this.db.sequelize.getQueryInterface();
|
|
570
|
+
const tableName = collection.getTableNameWithSchema();
|
|
571
|
+
let columns: Record<string, unknown> | null = null;
|
|
572
|
+
|
|
573
|
+
try {
|
|
574
|
+
columns = await queryInterface.describeTable(tableName);
|
|
575
|
+
} catch {
|
|
576
|
+
await collection.model.sync();
|
|
577
|
+
columns = await queryInterface.describeTable(tableName);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
for (const [attributeName, attribute] of Object.entries(collection.model.rawAttributes)) {
|
|
581
|
+
const columnName = attribute.field || attributeName;
|
|
582
|
+
if (columns && columnName in columns) {
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
const columnDefinition: typeof attribute & { Model?: unknown; fieldName?: string } = { ...attribute };
|
|
586
|
+
delete columnDefinition.Model;
|
|
587
|
+
delete columnDefinition.fieldName;
|
|
588
|
+
await queryInterface.addColumn(tableName, columnName, columnDefinition);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
private async ensureSchema() {
|
|
593
|
+
for (const collectionName of this.schemaCollections) {
|
|
594
|
+
await this.ensureCollectionSchema(collectionName);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
export default PluginElasticReportServer;
|