File size: 1,319 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useQuery } from '@tanstack/react-query';
import { PerformanceMetricsItemQueryResponse } from 'calypso/data/site-profiler/types';
import wp from 'calypso/lib/wp';

function mapResult( response: WPComSupportQueryResponse ) {
	const messages = response.messages?.[ 0 ]?.content ?? '';
	const chatId = response.chat_id;
	return {
		messages,
		chatId,
	};
}

export const useSupportChatLLMQuery = (
	insight: PerformanceMetricsItemQueryResponse,
	hash: string,
	is_wpcom: boolean,
	enable: boolean,
	localeSlug?: string,
	deviceStrategy?: string
) => {
	return useQuery( {
		// eslint-disable-next-line @tanstack/query/exhaustive-deps
		queryKey: [ 'support', 'chat', hash, insight.title, is_wpcom, localeSlug, deviceStrategy ],
		queryFn: () =>
			wp.req.post(
				{
					path: `/odie/assistant/performance-profiler?hash=${ hash }`,
					apiNamespace: 'wpcom/v2',
				},
				{
					insight,
					is_wpcom,
					locale: localeSlug,
					device_strategy: deviceStrategy,
				}
			),
		select: mapResult,
		enabled: !! insight.title && enable,
		retry: false,
		refetchOnWindowFocus: false,
		staleTime: 1000 * 60 * 5, // 5 minutes
	} );
};

type WPComSupportQueryResponse = {
	chat_id: number;
	messages: Array< WPComSupportMessageQueryResponse >;
};

type WPComSupportMessageQueryResponse = {
	content: string;
};