-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
update_route.ts
91 lines (87 loc) · 3.26 KB
/
update_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
/*
* 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 {
API_VERSIONS,
ELASTIC_AI_ASSISTANT_CONVERSATIONS_URL_BY_ID,
} from '@kbn/elastic-assistant-common';
import {
ConversationResponse,
ConversationUpdateProps,
} from '@kbn/elastic-assistant-common/impl/schemas/conversations/common_attributes.gen';
import { UpdateConversationRequestParams } from '@kbn/elastic-assistant-common/impl/schemas/conversations/crud_conversation_route.gen';
import { buildRouteValidationWithZod } from '@kbn/elastic-assistant-common/impl/schemas/common';
import { ElasticAssistantPluginRouter } from '../../types';
import { buildResponse } from '../utils';
import { performChecks } from '../helpers';
export const updateConversationRoute = (router: ElasticAssistantPluginRouter) => {
router.versioned
.put({
access: 'public',
path: ELASTIC_AI_ASSISTANT_CONVERSATIONS_URL_BY_ID,
options: {
tags: ['access:elasticAssistant'],
},
})
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: {
request: {
body: buildRouteValidationWithZod(ConversationUpdateProps),
params: buildRouteValidationWithZod(UpdateConversationRequestParams),
},
},
},
async (context, request, response): Promise<IKibanaResponse<ConversationResponse>> => {
const assistantResponse = buildResponse(response);
const { id } = request.params;
try {
const ctx = await context.resolve(['core', 'elasticAssistant', 'licensing']);
const authenticatedUser = ctx.elasticAssistant.getCurrentUser();
// 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 existingConversation = await dataClient?.getConversation({ id, authenticatedUser });
if (existingConversation == null) {
return assistantResponse.error({
body: `conversation id: "${id}" not found`,
statusCode: 404,
});
}
const conversation = await dataClient?.updateConversation({
conversationUpdateProps: request.body,
});
if (conversation == null) {
return assistantResponse.error({
body: `conversation id: "${id}" was not updated`,
statusCode: 400,
});
}
return response.ok({
body: conversation,
});
} catch (err) {
const error = transformError(err);
return assistantResponse.error({
body: error.message,
statusCode: error.statusCode,
});
}
}
);
};