-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
index.ts
517 lines (459 loc) · 19.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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
406
407
408
409
410
411
412
413
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
439
440
441
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DataStreamSpacesAdapter, FieldMap } from '@kbn/data-stream-adapter';
import { DEFAULT_NAMESPACE_STRING } from '@kbn/core-saved-objects-utils-server';
import type { AuthenticatedUser, Logger, ElasticsearchClient } from '@kbn/core/server';
import type { TaskManagerSetupContract } from '@kbn/task-manager-plugin/server';
import type { MlPluginSetup } from '@kbn/ml-plugin/server';
import { Subject } from 'rxjs';
import { attackDiscoveryFieldMap } from '../ai_assistant_data_clients/attack_discovery/field_maps_configuration';
import { getDefaultAnonymizationFields } from '../../common/anonymization';
import { AssistantResourceNames, GetElser } from '../types';
import { AIAssistantConversationsDataClient } from '../ai_assistant_data_clients/conversations';
import {
InitializationPromise,
ResourceInstallationHelper,
createResourceInstallationHelper,
errorResult,
successResult,
} from './create_resource_installation_helper';
import { conversationsFieldMap } from '../ai_assistant_data_clients/conversations/field_maps_configuration';
import { assistantPromptsFieldMap } from '../ai_assistant_data_clients/prompts/field_maps_configuration';
import { assistantAnonymizationFieldsFieldMap } from '../ai_assistant_data_clients/anonymization_fields/field_maps_configuration';
import { AIAssistantDataClient } from '../ai_assistant_data_clients';
import { knowledgeBaseFieldMapV2 } from '../ai_assistant_data_clients/knowledge_base/field_maps_configuration';
import {
AIAssistantKnowledgeBaseDataClient,
GetAIAssistantKnowledgeBaseDataClientParams,
} from '../ai_assistant_data_clients/knowledge_base';
import { AttackDiscoveryDataClient } from '../ai_assistant_data_clients/attack_discovery';
import { createGetElserId, createPipeline, pipelineExists } from './helpers';
const TOTAL_FIELDS_LIMIT = 2500;
function getResourceName(resource: string) {
return `.kibana-elastic-ai-assistant-${resource}`;
}
export interface AIAssistantServiceOpts {
logger: Logger;
kibanaVersion: string;
elasticsearchClientPromise: Promise<ElasticsearchClient>;
ml: MlPluginSetup;
taskManager: TaskManagerSetupContract;
pluginStop$: Subject<void>;
}
export interface CreateAIAssistantClientParams {
logger: Logger;
spaceId: string;
currentUser: AuthenticatedUser | null;
}
export type CreateDataStream = (params: {
resource:
| 'anonymizationFields'
| 'conversations'
| 'knowledgeBase'
| 'prompts'
| 'attackDiscovery';
fieldMap: FieldMap;
kibanaVersion: string;
spaceId?: string;
}) => DataStreamSpacesAdapter;
export class AIAssistantService {
private initialized: boolean;
private isInitializing: boolean = false;
private getElserId: GetElser;
private conversationsDataStream: DataStreamSpacesAdapter;
private knowledgeBaseDataStream: DataStreamSpacesAdapter;
private promptsDataStream: DataStreamSpacesAdapter;
private anonymizationFieldsDataStream: DataStreamSpacesAdapter;
private attackDiscoveryDataStream: DataStreamSpacesAdapter;
private resourceInitializationHelper: ResourceInstallationHelper;
private initPromise: Promise<InitializationPromise>;
private isKBSetupInProgress: boolean = false;
// Temporary 'feature flag' to determine if we should initialize the new kb mappings, toggled when accessing kbDataClient
private v2KnowledgeBaseEnabled: boolean = false;
constructor(private readonly options: AIAssistantServiceOpts) {
this.initialized = false;
this.getElserId = createGetElserId(options.ml.trainedModelsProvider);
this.conversationsDataStream = this.createDataStream({
resource: 'conversations',
kibanaVersion: options.kibanaVersion,
fieldMap: conversationsFieldMap,
});
this.knowledgeBaseDataStream = this.createDataStream({
resource: 'knowledgeBase',
kibanaVersion: options.kibanaVersion,
fieldMap: knowledgeBaseFieldMapV2, // TODO: use v2 if FF is enabled
});
this.promptsDataStream = this.createDataStream({
resource: 'prompts',
kibanaVersion: options.kibanaVersion,
fieldMap: assistantPromptsFieldMap,
});
this.anonymizationFieldsDataStream = this.createDataStream({
resource: 'anonymizationFields',
kibanaVersion: options.kibanaVersion,
fieldMap: assistantAnonymizationFieldsFieldMap,
});
this.attackDiscoveryDataStream = this.createDataStream({
resource: 'attackDiscovery',
kibanaVersion: options.kibanaVersion,
fieldMap: attackDiscoveryFieldMap,
});
this.initPromise = this.initializeResources();
this.resourceInitializationHelper = createResourceInstallationHelper(
this.options.logger,
this.initPromise,
this.installAndUpdateSpaceLevelResources.bind(this)
);
}
public isInitialized() {
return this.initialized;
}
public getIsKBSetupInProgress() {
return this.isKBSetupInProgress;
}
public setIsKBSetupInProgress(isInProgress: boolean) {
this.isKBSetupInProgress = isInProgress;
}
private createDataStream: CreateDataStream = ({ resource, kibanaVersion, fieldMap }) => {
const newDataStream = new DataStreamSpacesAdapter(this.resourceNames.aliases[resource], {
kibanaVersion,
totalFieldsLimit: TOTAL_FIELDS_LIMIT,
});
newDataStream.setComponentTemplate({
name: this.resourceNames.componentTemplate[resource],
fieldMap,
});
newDataStream.setIndexTemplate({
name: this.resourceNames.indexTemplate[resource],
componentTemplateRefs: [this.resourceNames.componentTemplate[resource]],
// Apply `default_pipeline` if pipeline exists for resource
...(resource in this.resourceNames.pipelines
? {
template: {
settings: {
'index.default_pipeline':
this.resourceNames.pipelines[
resource as keyof typeof this.resourceNames.pipelines
],
},
},
}
: {}),
});
return newDataStream;
};
private async initializeResources(): Promise<InitializationPromise> {
this.isInitializing = true;
try {
this.options.logger.debug(`Initializing resources for AIAssistantService`);
const esClient = await this.options.elasticsearchClientPromise;
await this.conversationsDataStream.install({
esClient,
logger: this.options.logger,
pluginStop$: this.options.pluginStop$,
});
await this.knowledgeBaseDataStream.install({
esClient,
logger: this.options.logger,
pluginStop$: this.options.pluginStop$,
});
// TODO: Pipeline creation is temporary as we'll be moving to semantic_text field once available in ES
const pipelineCreated = await pipelineExists({
esClient,
id: this.resourceNames.pipelines.knowledgeBase,
});
if (!pipelineCreated || this.v2KnowledgeBaseEnabled) {
this.options.logger.debug(
`Installing ingest pipeline - ${this.resourceNames.pipelines.knowledgeBase}`
);
const response = await createPipeline({
esClient,
id: this.resourceNames.pipelines.knowledgeBase,
modelId: await this.getElserId(),
});
this.options.logger.debug(`Installed ingest pipeline: ${response}`);
} else {
this.options.logger.debug(
`Ingest pipeline already exists - ${this.resourceNames.pipelines.knowledgeBase}`
);
}
await this.promptsDataStream.install({
esClient,
logger: this.options.logger,
pluginStop$: this.options.pluginStop$,
});
await this.anonymizationFieldsDataStream.install({
esClient,
logger: this.options.logger,
pluginStop$: this.options.pluginStop$,
});
await this.attackDiscoveryDataStream.install({
esClient,
logger: this.options.logger,
pluginStop$: this.options.pluginStop$,
});
} catch (error) {
this.options.logger.error(`Error initializing AI assistant resources: ${error.message}`);
this.initialized = false;
this.isInitializing = false;
return errorResult(error.message);
}
this.initialized = true;
this.isInitializing = false;
return successResult();
}
private readonly resourceNames: AssistantResourceNames = {
componentTemplate: {
conversations: getResourceName('component-template-conversations'),
knowledgeBase: getResourceName('component-template-knowledge-base'),
prompts: getResourceName('component-template-prompts'),
anonymizationFields: getResourceName('component-template-anonymization-fields'),
attackDiscovery: getResourceName('component-template-attack-discovery'),
},
aliases: {
conversations: getResourceName('conversations'),
knowledgeBase: getResourceName('knowledge-base'),
prompts: getResourceName('prompts'),
anonymizationFields: getResourceName('anonymization-fields'),
attackDiscovery: getResourceName('attack-discovery'),
},
indexPatterns: {
conversations: getResourceName('conversations*'),
knowledgeBase: getResourceName('knowledge-base*'),
prompts: getResourceName('prompts*'),
anonymizationFields: getResourceName('anonymization-fields*'),
attackDiscovery: getResourceName('attack-discovery*'),
},
indexTemplate: {
conversations: getResourceName('index-template-conversations'),
knowledgeBase: getResourceName('index-template-knowledge-base'),
prompts: getResourceName('index-template-prompts'),
anonymizationFields: getResourceName('index-template-anonymization-fields'),
attackDiscovery: getResourceName('index-template-attack-discovery'),
},
pipelines: {
knowledgeBase: getResourceName('ingest-pipeline-knowledge-base'),
},
};
private async checkResourcesInstallation(opts: CreateAIAssistantClientParams) {
// Check if resources installation has succeeded
const { result: initialized, error } = await this.getSpaceResourcesInitializationPromise(
opts.spaceId
);
// If space level resources initialization failed, retry
if (!initialized && error) {
let initRetryPromise: Promise<InitializationPromise> | undefined;
// If !this.initialized, we know that resource initialization failed
// and we need to retry this before retrying the spaceId specific resources
if (!this.initialized) {
if (!this.isInitializing) {
this.options.logger.info(`Retrying common resource initialization`);
initRetryPromise = this.initializeResources();
} else {
this.options.logger.info(
`Skipped retrying common resource initialization because it is already being retried.`
);
}
}
this.resourceInitializationHelper.retry(opts.spaceId, initRetryPromise);
const retryResult = await this.resourceInitializationHelper.getInitializedResources(
opts.spaceId ?? DEFAULT_NAMESPACE_STRING
);
if (!retryResult.result) {
const errorLogPrefix = `There was an error in the framework installing spaceId-level resources and creating concrete indices for spaceId "${opts.spaceId}" - `;
// Retry also failed
this.options.logger.warn(
retryResult.error && error
? `${errorLogPrefix}Retry failed with errors: ${error}`
: `${errorLogPrefix}Original error: ${error}; Error after retry: ${retryResult.error}`
);
return null;
} else {
this.options.logger.info(
`Resource installation for "${opts.spaceId}" succeeded after retry`
);
}
}
}
public async createAIAssistantConversationsDataClient(
opts: CreateAIAssistantClientParams
): Promise<AIAssistantConversationsDataClient | null> {
const res = await this.checkResourcesInstallation(opts);
if (res === null) {
return null;
}
return new AIAssistantConversationsDataClient({
logger: this.options.logger,
elasticsearchClientPromise: this.options.elasticsearchClientPromise,
spaceId: opts.spaceId,
kibanaVersion: this.options.kibanaVersion,
indexPatternsResourceName: this.resourceNames.aliases.conversations,
currentUser: opts.currentUser,
});
}
public async createAIAssistantKnowledgeBaseDataClient(
opts: CreateAIAssistantClientParams & GetAIAssistantKnowledgeBaseDataClientParams
): Promise<AIAssistantKnowledgeBaseDataClient | null> {
// If modelIdOverride is set, swap getElserId(), and ensure the pipeline is re-created with the correct model
if (opts.modelIdOverride != null) {
const modelIdOverride = opts.modelIdOverride;
this.getElserId = async () => modelIdOverride;
}
// Note: Due to plugin lifecycle and feature flag registration timing, we need to pass in the feature flag here
// Remove this param and initialization when the `assistantKnowledgeBaseByDefault` feature flag is removed
if (opts.v2KnowledgeBaseEnabled) {
this.v2KnowledgeBaseEnabled = true;
}
// If either v2 KB or a modelIdOverride is provided, we need to reinitialize all persistence resources to make sure
// they're using the correct model/mappings. Technically all existing KB data is stale since it was created
// with a different model/mappings, but modelIdOverride is only intended for testing purposes at this time
if (opts.v2KnowledgeBaseEnabled || opts.modelIdOverride != null) {
await this.initializeResources();
}
const res = await this.checkResourcesInstallation(opts);
if (res === null) {
return null;
}
return new AIAssistantKnowledgeBaseDataClient({
logger: this.options.logger.get('knowledgeBase'),
currentUser: opts.currentUser,
elasticsearchClientPromise: this.options.elasticsearchClientPromise,
indexPatternsResourceName: this.resourceNames.aliases.knowledgeBase,
ingestPipelineResourceName: this.resourceNames.pipelines.knowledgeBase,
getElserId: this.getElserId,
getIsKBSetupInProgress: this.getIsKBSetupInProgress.bind(this),
kibanaVersion: this.options.kibanaVersion,
ml: this.options.ml,
setIsKBSetupInProgress: this.setIsKBSetupInProgress.bind(this),
spaceId: opts.spaceId,
v2KnowledgeBaseEnabled: opts.v2KnowledgeBaseEnabled ?? false,
});
}
public async createAttackDiscoveryDataClient(
opts: CreateAIAssistantClientParams
): Promise<AttackDiscoveryDataClient | null> {
const res = await this.checkResourcesInstallation(opts);
if (res === null) {
return null;
}
return new AttackDiscoveryDataClient({
logger: this.options.logger.get('attackDiscovery'),
currentUser: opts.currentUser,
elasticsearchClientPromise: this.options.elasticsearchClientPromise,
indexPatternsResourceName: this.resourceNames.aliases.attackDiscovery,
kibanaVersion: this.options.kibanaVersion,
spaceId: opts.spaceId,
});
}
public async createAIAssistantPromptsDataClient(
opts: CreateAIAssistantClientParams
): Promise<AIAssistantDataClient | null> {
const res = await this.checkResourcesInstallation(opts);
if (res === null) {
return null;
}
return new AIAssistantDataClient({
logger: this.options.logger,
elasticsearchClientPromise: this.options.elasticsearchClientPromise,
spaceId: opts.spaceId,
kibanaVersion: this.options.kibanaVersion,
indexPatternsResourceName: this.resourceNames.aliases.prompts,
currentUser: opts.currentUser,
});
}
public async createAIAssistantAnonymizationFieldsDataClient(
opts: CreateAIAssistantClientParams
): Promise<AIAssistantDataClient | null> {
const res = await this.checkResourcesInstallation(opts);
if (res === null) {
return null;
}
return new AIAssistantDataClient({
logger: this.options.logger,
elasticsearchClientPromise: this.options.elasticsearchClientPromise,
spaceId: opts.spaceId,
kibanaVersion: this.options.kibanaVersion,
indexPatternsResourceName: this.resourceNames.aliases.anonymizationFields,
currentUser: opts.currentUser,
});
}
public async getSpaceResourcesInitializationPromise(
spaceId: string | undefined = DEFAULT_NAMESPACE_STRING
): Promise<InitializationPromise> {
const result = await this.resourceInitializationHelper.getInitializedResources(spaceId);
// If the spaceId is unrecognized and spaceId is not the default, we
// need to kick off resource installation and return the promise
if (
result.error &&
result.error.includes(`Unrecognized spaceId`) &&
spaceId !== DEFAULT_NAMESPACE_STRING
) {
this.resourceInitializationHelper.add(spaceId);
return this.resourceInitializationHelper.getInitializedResources(spaceId);
}
return result;
}
private async installAndUpdateSpaceLevelResources(
spaceId: string | undefined = DEFAULT_NAMESPACE_STRING
) {
try {
this.options.logger.debug(`Initializing spaceId level resources for AIAssistantService`);
const conversationsIndexName = await this.conversationsDataStream.getInstalledSpaceName(
spaceId
);
if (!conversationsIndexName) {
await this.conversationsDataStream.installSpace(spaceId);
}
const knowledgeBaseIndexName = await this.knowledgeBaseDataStream.getInstalledSpaceName(
spaceId
);
if (!knowledgeBaseIndexName) {
await this.knowledgeBaseDataStream.installSpace(spaceId);
}
const promptsIndexName = await this.promptsDataStream.getInstalledSpaceName(spaceId);
if (!promptsIndexName) {
await this.promptsDataStream.installSpace(spaceId);
}
const anonymizationFieldsIndexName =
await this.anonymizationFieldsDataStream.getInstalledSpaceName(spaceId);
if (!anonymizationFieldsIndexName) {
await this.anonymizationFieldsDataStream.installSpace(spaceId);
await this.createDefaultAnonymizationFields(spaceId);
}
} catch (error) {
this.options.logger.error(
`Error initializing AI assistant namespace level resources: ${error.message}`
);
throw error;
}
}
private async createDefaultAnonymizationFields(spaceId: string) {
const dataClient = new AIAssistantDataClient({
logger: this.options.logger,
elasticsearchClientPromise: this.options.elasticsearchClientPromise,
spaceId,
kibanaVersion: this.options.kibanaVersion,
indexPatternsResourceName: this.resourceNames.aliases.anonymizationFields,
currentUser: null,
});
const existingAnonymizationFields = await (
await dataClient?.getReader()
).search({
body: {
size: 1,
},
allow_no_indices: true,
});
if (existingAnonymizationFields.hits.total.value === 0) {
const writer = await dataClient?.getWriter();
const res = await writer?.bulk({
documentsToCreate: getDefaultAnonymizationFields(spaceId),
});
this.options.logger.info(`Created default anonymization fields: ${res?.docs_created.length}`);
}
}
}