-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
request_context_factory.ts
124 lines (100 loc) · 3.97 KB
/
request_context_factory.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
/*
* 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 { memoize } from 'lodash';
import type { Logger, KibanaRequest, RequestHandlerContext } from '@kbn/core/server';
import { DEFAULT_NAMESPACE_STRING } from '@kbn/core-saved-objects-utils-server';
import {
ElasticAssistantApiRequestHandlerContext,
ElasticAssistantPluginCoreSetupDependencies,
ElasticAssistantPluginSetupDependencies,
ElasticAssistantRequestHandlerContext,
} from '../types';
import { AIAssistantService } from '../ai_assistant_service';
import { appContextService } from '../services/app_context';
export interface IRequestContextFactory {
create(
context: RequestHandlerContext,
request: KibanaRequest
): Promise<ElasticAssistantApiRequestHandlerContext>;
}
interface ConstructorOptions {
logger: Logger;
core: ElasticAssistantPluginCoreSetupDependencies;
plugins: ElasticAssistantPluginSetupDependencies;
kibanaVersion: string;
assistantService: AIAssistantService;
}
export class RequestContextFactory implements IRequestContextFactory {
private readonly logger: Logger;
private readonly assistantService: AIAssistantService;
constructor(private readonly options: ConstructorOptions) {
this.logger = options.logger;
this.assistantService = options.assistantService;
}
public async create(
context: Omit<ElasticAssistantRequestHandlerContext, 'elasticAssistant'>,
request: KibanaRequest
): Promise<ElasticAssistantApiRequestHandlerContext> {
const { options } = this;
const { core } = options;
const [, startPlugins] = await core.getStartServices();
const coreContext = await context.core;
const getSpaceId = (): string =>
startPlugins.spaces?.spacesService?.getSpaceId(request) || DEFAULT_NAMESPACE_STRING;
const getCurrentUser = () => startPlugins.security?.authc.getCurrentUser(request);
return {
core: coreContext,
actions: startPlugins.actions,
logger: this.logger,
getServerBasePath: () => core.http.basePath.serverBasePath,
getSpaceId,
getCurrentUser,
getRegisteredTools: (pluginName: string) => {
return appContextService.getRegisteredTools(pluginName);
},
getRegisteredFeatures: (pluginName: string) => {
return appContextService.getRegisteredFeatures(pluginName);
},
telemetry: core.analytics,
// Note: Due to plugin lifecycle and feature flag registration timing, we need to pass in the feature flag here
// Remove `initializeKnowledgeBase` once 'assistantKnowledgeBaseByDefault' feature flag is removed
getAIAssistantKnowledgeBaseDataClient: memoize((initializeKnowledgeBase = false) => {
const currentUser = getCurrentUser();
return this.assistantService.createAIAssistantKnowledgeBaseDataClient({
spaceId: getSpaceId(),
logger: this.logger,
currentUser,
initializeKnowledgeBase,
});
}),
getAIAssistantPromptsDataClient: memoize(() => {
const currentUser = getCurrentUser();
return this.assistantService.createAIAssistantPromptsDataClient({
spaceId: getSpaceId(),
logger: this.logger,
currentUser,
});
}),
getAIAssistantAnonymizationFieldsDataClient: memoize(() => {
const currentUser = getCurrentUser();
return this.assistantService.createAIAssistantAnonymizationFieldsDataClient({
spaceId: getSpaceId(),
logger: this.logger,
currentUser,
});
}),
getAIAssistantConversationsDataClient: memoize(async () => {
const currentUser = getCurrentUser();
return this.assistantService.createAIAssistantConversationsDataClient({
spaceId: getSpaceId(),
logger: this.logger,
currentUser,
});
}),
};
}
}