-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
create_route.ts
96 lines (90 loc) · 3.28 KB
/
create_route.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
/*
* 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 type { IKibanaResponse } from '@kbn/core/server';
import { transformError } from '@kbn/securitysolution-es-utils';
import {
ELASTIC_AI_ASSISTANT_CONVERSATIONS_URL,
ConversationCreateProps,
ConversationResponse,
API_VERSIONS,
} from '@kbn/elastic-assistant-common';
import { buildRouteValidationWithZod } from '@kbn/elastic-assistant-common/impl/schemas/common';
import { ElasticAssistantPluginRouter } from '../../types';
import { buildResponse } from '../utils';
import { performChecks } from '../helpers';
export const createConversationRoute = (router: ElasticAssistantPluginRouter): void => {
router.versioned
.post({
access: 'public',
path: ELASTIC_AI_ASSISTANT_CONVERSATIONS_URL,
options: {
tags: ['access:elasticAssistant'],
},
})
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: {
request: {
body: buildRouteValidationWithZod(ConversationCreateProps),
},
},
},
async (context, request, response): Promise<IKibanaResponse<ConversationResponse>> => {
const assistantResponse = buildResponse(response);
try {
const ctx = await context.resolve(['core', 'elasticAssistant', 'licensing']);
// Perform license and authenticated user checks
const checkResponse = performChecks({
authenticatedUser: true,
context: ctx,
license: true,
request,
response,
});
if (checkResponse) {
return checkResponse;
}
const dataClient = await ctx.elasticAssistant.getAIAssistantConversationsDataClient();
const currentUser = ctx.elasticAssistant.getCurrentUser();
const userFilter = currentUser?.username
? `name: "${currentUser?.username}"`
: `id: "${currentUser?.profile_uid}"`;
const result = await dataClient?.findDocuments({
perPage: 100,
page: 1,
filter: `users:{ ${userFilter} } AND title:${request.body.title}`,
fields: ['title'],
});
if (result?.data != null && result.total > 0) {
return assistantResponse.error({
statusCode: 409,
body: `conversation title: "${request.body.title}" already exists`,
});
}
const createdConversation = await dataClient?.createConversation({
conversation: request.body,
});
if (createdConversation == null) {
return assistantResponse.error({
body: `conversation with title: "${request.body.title}" was not created`,
statusCode: 400,
});
}
return response.ok({
body: ConversationResponse.parse(createdConversation),
});
} catch (err) {
const error = transformError(err as Error);
return assistantResponse.error({
body: error.message,
statusCode: error.statusCode,
});
}
}
);
};