forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insight.tsx
160 lines (145 loc) · 5.14 KB
/
insight.tsx
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
/*
* 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 { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { AbortError } from '@kbn/kibana-utils-plugin/common';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import type { Subscription } from 'rxjs';
import { MessageRole, type Message } from '../../../common/types';
import { useGenAIConnectors } from '../../hooks/use_genai_connectors';
import { useObservabilityAIAssistant } from '../../hooks/use_observability_ai_assistant';
import type { PendingMessage } from '../../types';
import { ChatFlyout } from '../chat/chat_flyout';
import { ConnectorSelectorBase } from '../connector_selector/connector_selector_base';
import { MessagePanel } from '../message_panel/message_panel';
import { MessageText } from '../message_panel/message_text';
import { RegenerateResponseButton } from '../buttons/regenerate_response_button';
import { StartChatButton } from '../buttons/start_chat_button';
import { StopGeneratingButton } from '../buttons/stop_generating_button';
import { InsightBase } from './insight_base';
import { MissingCredentialsCallout } from '../missing_credentials_callout';
import { getConnectorsManagementHref } from '../../utils/get_connectors_management_href';
function ChatContent({ messages, connectorId }: { messages: Message[]; connectorId: string }) {
const service = useObservabilityAIAssistant();
const [pendingMessage, setPendingMessage] = useState<PendingMessage | undefined>();
const [loading, setLoading] = useState(false);
const [subscription, setSubscription] = useState<Subscription | undefined>();
const reloadReply = useCallback(() => {
setLoading(true);
const nextSubscription = service.chat({ messages, connectorId }).subscribe({
next: (msg) => {
setPendingMessage(() => msg);
},
complete: () => {
setLoading(false);
},
});
setSubscription(nextSubscription);
}, [messages, connectorId, service]);
useEffect(() => {
reloadReply();
}, [reloadReply]);
const [isOpen, setIsOpen] = useState(false);
const initialConversation = useMemo(() => {
const time = new Date().toISOString();
return {
'@timestamp': time,
messages: pendingMessage?.message.content
? messages.concat({
'@timestamp': time,
message: {
role: MessageRole.Assistant,
content: pendingMessage.message.content,
},
})
: messages,
conversation: {
title: '',
},
labels: {},
numeric_labels: {},
public: false,
};
}, [pendingMessage, messages]);
return (
<>
<MessagePanel
body={<MessageText content={pendingMessage?.message.content ?? ''} loading={loading} />}
error={pendingMessage?.error}
controls={
loading ? (
<StopGeneratingButton
onClick={() => {
subscription?.unsubscribe();
setLoading(false);
setPendingMessage((prev) => ({
message: {
role: MessageRole.Assistant,
...prev?.message,
},
aborted: true,
error: new AbortError(),
}));
}}
/>
) : (
<EuiFlexGroup direction="row">
<EuiFlexItem grow={false}>
<RegenerateResponseButton
onClick={() => {
reloadReply();
}}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<StartChatButton
onClick={() => {
setIsOpen(() => true);
}}
/>
</EuiFlexItem>
</EuiFlexGroup>
)
}
/>
<ChatFlyout
isOpen={isOpen}
onClose={() => {
setIsOpen(() => false);
}}
initialConversation={initialConversation}
/>
</>
);
}
export function Insight({ messages, title }: { messages: Message[]; title: string }) {
const [hasOpened, setHasOpened] = useState(false);
const connectors = useGenAIConnectors();
const {
services: { http },
} = useKibana();
let children: React.ReactNode = null;
if (hasOpened && connectors.selectedConnector) {
children = <ChatContent messages={messages} connectorId={connectors.selectedConnector} />;
} else if (!connectors.loading && !connectors.connectors?.length) {
children = (
<MissingCredentialsCallout connectorsManagementHref={getConnectorsManagementHref(http!)} />
);
}
return (
<InsightBase
title={title}
onToggle={(isOpen) => {
setHasOpened((prevHasOpened) => prevHasOpened || isOpen);
}}
controls={<ConnectorSelectorBase {...connectors} />}
loading={connectors.loading}
>
{children}
</InsightBase>
);
}