librechat-data-provider 0.2.5 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.es.js +2 -1215
- package/dist/index.es.js.map +1 -0
- package/dist/index.js +2 -1303
- package/dist/index.js.map +1 -0
- package/dist/react-query/index.es.js +2 -0
- package/dist/react-query/index.es.js.map +1 -0
- package/dist/react-query/package.json +10 -0
- package/package.json +21 -4
- package/react-query/package.json +10 -0
- package/rollup.config.js +61 -4
- package/src/api-endpoints.ts +8 -0
- package/src/createPayload.ts +20 -17
- package/src/data-service.ts +61 -15
- package/src/index.ts +10 -3
- package/src/keys.ts +27 -0
- package/src/react-query/assistants.ts +138 -0
- package/src/react-query/index.ts +2 -0
- package/src/{react-query-service.ts → react-query/react-query-service.ts} +59 -69
- package/src/request.ts +48 -47
- package/src/schemas.ts +404 -30
- package/src/sse.js +2 -2
- package/src/types/assistants.ts +72 -0
- package/src/types/files.ts +42 -0
- package/src/types/mutations.ts +28 -0
- package/src/types.ts +3 -4
- package/tsconfig.json +6 -4
- package/types/api-endpoints.d.ts +0 -30
- package/types/createPayload.d.ts +0 -39
- package/types/data-service.d.ts +0 -35
- package/types/headers-helpers.d.ts +0 -2
- package/types/index.d.ts +0 -7
- package/types/react-query-service.d.ts +0 -58
- package/types/request.d.ts +0 -18
- package/types/schemas.d.ts +0 -1763
- package/types/types.d.ts +0 -165
package/src/data-service.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import * as f from './types/files';
|
|
2
|
+
import * as m from './types/mutations';
|
|
3
|
+
import * as a from './types/assistants';
|
|
1
4
|
import * as t from './types';
|
|
2
5
|
import * as s from './schemas';
|
|
3
|
-
/* TODO: fix dependency cycle */
|
|
4
|
-
// eslint-disable-next-line import/no-cycle
|
|
5
6
|
import request from './request';
|
|
6
7
|
import * as endpoints from './api-endpoints';
|
|
7
8
|
|
|
@@ -35,6 +36,9 @@ export function revokeAllUserKeys(): Promise<unknown> {
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
export function getMessagesByConvoId(conversationId: string): Promise<s.TMessage[]> {
|
|
39
|
+
if (conversationId === 'new') {
|
|
40
|
+
return Promise.resolve([]);
|
|
41
|
+
}
|
|
38
42
|
return request.get(endpoints.messages(conversationId));
|
|
39
43
|
}
|
|
40
44
|
|
|
@@ -70,15 +74,15 @@ export function getPresets(): Promise<s.TPreset[]> {
|
|
|
70
74
|
return request.get(endpoints.presets());
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
export function createPreset(payload: s.TPreset): Promise<s.TPreset
|
|
77
|
+
export function createPreset(payload: s.TPreset): Promise<s.TPreset> {
|
|
74
78
|
return request.post(endpoints.presets(), payload);
|
|
75
79
|
}
|
|
76
80
|
|
|
77
|
-
export function updatePreset(payload: s.TPreset): Promise<s.TPreset
|
|
81
|
+
export function updatePreset(payload: s.TPreset): Promise<s.TPreset> {
|
|
78
82
|
return request.post(endpoints.presets(), payload);
|
|
79
83
|
}
|
|
80
84
|
|
|
81
|
-
export function deletePreset(arg: s.TPreset |
|
|
85
|
+
export function deletePreset(arg: s.TPreset | undefined): Promise<m.PresetDeleteResponse> {
|
|
82
86
|
return request.post(endpoints.deletePreset(), arg);
|
|
83
87
|
}
|
|
84
88
|
|
|
@@ -101,14 +105,6 @@ export const searchConversations = async (
|
|
|
101
105
|
return request.get(endpoints.search(q, pageNumber));
|
|
102
106
|
};
|
|
103
107
|
|
|
104
|
-
export const getAIEndpoints = (): Promise<t.TEndpointsConfig> => {
|
|
105
|
-
return request.get(endpoints.aiEndpoints());
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export const getModels = async (): Promise<t.TModelsConfig> => {
|
|
109
|
-
return request.get(endpoints.models());
|
|
110
|
-
};
|
|
111
|
-
|
|
112
108
|
export const updateTokenCount = (text: string) => {
|
|
113
109
|
return request.post(endpoints.tokenizer(), { arg: text });
|
|
114
110
|
};
|
|
@@ -125,8 +121,6 @@ export const register = (payload: t.TRegisterUser) => {
|
|
|
125
121
|
return request.post(endpoints.register(), payload);
|
|
126
122
|
};
|
|
127
123
|
|
|
128
|
-
export const refreshToken = (retry?: boolean) => request.post(endpoints.refreshToken(retry));
|
|
129
|
-
|
|
130
124
|
export const userKeyQuery = (name: string): Promise<t.TCheckUserKeyResponse> =>
|
|
131
125
|
request.get(endpoints.userKeyQuery(name));
|
|
132
126
|
|
|
@@ -152,6 +146,58 @@ export const updateUserPlugins = (payload: t.TUpdateUserPlugins) => {
|
|
|
152
146
|
return request.post(endpoints.userPlugins(), payload);
|
|
153
147
|
};
|
|
154
148
|
|
|
149
|
+
/* Config */
|
|
150
|
+
|
|
155
151
|
export const getStartupConfig = (): Promise<t.TStartupConfig> => {
|
|
156
152
|
return request.get(endpoints.config());
|
|
157
153
|
};
|
|
154
|
+
|
|
155
|
+
export const getAIEndpoints = (): Promise<t.TEndpointsConfig> => {
|
|
156
|
+
return request.get(endpoints.aiEndpoints());
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const getModels = async (): Promise<t.TModelsConfig> => {
|
|
160
|
+
return request.get(endpoints.models());
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export const getEndpointsConfigOverride = (): Promise<unknown | boolean> => {
|
|
164
|
+
return request.get(endpoints.endpointsConfigOverride());
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/* Assistants */
|
|
168
|
+
|
|
169
|
+
export const createAssistant = (data: a.AssistantCreateParams): Promise<a.Assistant> => {
|
|
170
|
+
return request.post(endpoints.assistants(), data);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export const getAssistantById = (assistant_id: string): Promise<a.Assistant> => {
|
|
174
|
+
return request.get(endpoints.assistants(assistant_id));
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export const updateAssistant = (
|
|
178
|
+
assistant_id: string,
|
|
179
|
+
data: a.AssistantUpdateParams,
|
|
180
|
+
): Promise<a.Assistant> => {
|
|
181
|
+
return request.patch(endpoints.assistants(assistant_id), data);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export const deleteAssistant = (assistant_id: string): Promise<void> => {
|
|
185
|
+
return request.delete(endpoints.assistants(assistant_id));
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
export const listAssistants = (
|
|
189
|
+
params?: a.AssistantListParams,
|
|
190
|
+
): Promise<a.AssistantListResponse> => {
|
|
191
|
+
return request.get(endpoints.assistants(), { params });
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/* Files */
|
|
195
|
+
|
|
196
|
+
export const uploadImage = (data: FormData): Promise<f.FileUploadResponse> => {
|
|
197
|
+
return request.postMultiPart(endpoints.images(), data);
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
export const deleteFiles = async (files: f.BatchFile[]): Promise<f.DeleteFilesResponse> =>
|
|
201
|
+
request.deleteWithOptions(endpoints.files(), {
|
|
202
|
+
data: { files },
|
|
203
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
export { default as request } from './request';
|
|
1
|
+
/* types/schemas/schema helpers */
|
|
3
2
|
export * from './types';
|
|
4
|
-
export * from './
|
|
3
|
+
export * from './types/assistants';
|
|
4
|
+
export * from './types/files';
|
|
5
|
+
export * from './types/mutations';
|
|
6
|
+
export * from './keys';
|
|
7
|
+
/* api call helpers */
|
|
5
8
|
export * from './headers-helpers';
|
|
9
|
+
export { default as request } from './request';
|
|
10
|
+
import * as dataService from './data-service';
|
|
11
|
+
export { dataService };
|
|
12
|
+
/* general helpers */
|
|
6
13
|
export * from './sse';
|
|
7
14
|
export { default as createPayload } from './createPayload';
|
package/src/keys.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export enum QueryKeys {
|
|
2
|
+
messages = 'messages',
|
|
3
|
+
allConversations = 'allConversations',
|
|
4
|
+
conversation = 'conversation',
|
|
5
|
+
searchEnabled = 'searchEnabled',
|
|
6
|
+
user = 'user',
|
|
7
|
+
name = 'name', // user key name
|
|
8
|
+
models = 'models',
|
|
9
|
+
balance = 'balance',
|
|
10
|
+
endpoints = 'endpoints',
|
|
11
|
+
presets = 'presets',
|
|
12
|
+
searchResults = 'searchResults',
|
|
13
|
+
tokenCount = 'tokenCount',
|
|
14
|
+
availablePlugins = 'availablePlugins',
|
|
15
|
+
startupConfig = 'startupConfig',
|
|
16
|
+
assistants = 'assistants',
|
|
17
|
+
assistant = 'assistant',
|
|
18
|
+
endpointsConfigOverride = 'endpointsConfigOverride',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export enum MutationKeys {
|
|
22
|
+
imageUpload = 'imageUpload',
|
|
23
|
+
fileDelete = 'fileDelete',
|
|
24
|
+
updatePreset = 'updatePreset',
|
|
25
|
+
deletePreset = 'deletePreset',
|
|
26
|
+
logoutUser = 'logoutUser',
|
|
27
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { useQuery, useMutation, useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
|
|
2
|
+
import type {
|
|
3
|
+
UseQueryOptions,
|
|
4
|
+
UseMutationResult,
|
|
5
|
+
QueryObserverResult,
|
|
6
|
+
UseInfiniteQueryOptions,
|
|
7
|
+
} from '@tanstack/react-query';
|
|
8
|
+
import * as t from '../types/assistants';
|
|
9
|
+
import * as dataService from '../data-service';
|
|
10
|
+
import { QueryKeys } from '../keys';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hook for listing all assistants, with optional parameters provided for pagination and sorting
|
|
14
|
+
*/
|
|
15
|
+
export const useListAssistantsQuery = <TData = t.AssistantListResponse>(
|
|
16
|
+
params?: t.AssistantListParams,
|
|
17
|
+
config?: UseQueryOptions<t.AssistantListResponse, unknown, TData>,
|
|
18
|
+
): QueryObserverResult<TData> => {
|
|
19
|
+
return useQuery<t.AssistantListResponse, unknown, TData>(
|
|
20
|
+
[QueryKeys.assistants, params],
|
|
21
|
+
() => dataService.listAssistants(params),
|
|
22
|
+
{
|
|
23
|
+
// Example selector to sort them by created_at
|
|
24
|
+
// select: (res) => {
|
|
25
|
+
// return res.data.sort((a, b) => a.created_at - b.created_at);
|
|
26
|
+
// },
|
|
27
|
+
refetchOnWindowFocus: false,
|
|
28
|
+
refetchOnReconnect: false,
|
|
29
|
+
refetchOnMount: false,
|
|
30
|
+
retry: false,
|
|
31
|
+
...config,
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const useListAssistantsInfiniteQuery = (
|
|
37
|
+
params?: t.AssistantListParams,
|
|
38
|
+
config?: UseInfiniteQueryOptions<t.AssistantListResponse, Error>,
|
|
39
|
+
) => {
|
|
40
|
+
return useInfiniteQuery<t.AssistantListResponse, Error>(
|
|
41
|
+
['assistantsList', params],
|
|
42
|
+
({ pageParam = '' }) => dataService.listAssistants({ ...params, after: pageParam }),
|
|
43
|
+
{
|
|
44
|
+
getNextPageParam: (lastPage) => {
|
|
45
|
+
// lastPage is of type AssistantListResponse, you can use the has_more and last_id from it directly
|
|
46
|
+
if (lastPage.has_more) {
|
|
47
|
+
return lastPage.last_id;
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
},
|
|
51
|
+
...config,
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Hook for creating a new assistant
|
|
58
|
+
*/
|
|
59
|
+
export const useCreateAssistantMutation = (): UseMutationResult<
|
|
60
|
+
t.Assistant,
|
|
61
|
+
Error,
|
|
62
|
+
t.AssistantCreateParams
|
|
63
|
+
> => {
|
|
64
|
+
const queryClient = useQueryClient();
|
|
65
|
+
return useMutation(
|
|
66
|
+
(newAssistantData: t.AssistantCreateParams) => dataService.createAssistant(newAssistantData),
|
|
67
|
+
{
|
|
68
|
+
onSuccess: () => {
|
|
69
|
+
// Invalidate and refetch assistants query to update list
|
|
70
|
+
queryClient.invalidateQueries([QueryKeys.assistants]);
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Hook for retrieving details about a single assistant
|
|
78
|
+
*/
|
|
79
|
+
export const useGetAssistantByIdQuery = (
|
|
80
|
+
assistant_id: string,
|
|
81
|
+
config?: UseQueryOptions<t.Assistant>,
|
|
82
|
+
): QueryObserverResult<t.Assistant> => {
|
|
83
|
+
return useQuery<t.Assistant>(
|
|
84
|
+
[QueryKeys.assistant, assistant_id],
|
|
85
|
+
() => dataService.getAssistantById(assistant_id),
|
|
86
|
+
{
|
|
87
|
+
enabled: !!assistant_id, // Query will not execute until the assistant_id exists
|
|
88
|
+
refetchOnWindowFocus: false,
|
|
89
|
+
refetchOnReconnect: false,
|
|
90
|
+
refetchOnMount: false,
|
|
91
|
+
retry: false,
|
|
92
|
+
...config,
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Hook for updating an assistant
|
|
99
|
+
*/
|
|
100
|
+
export const useUpdateAssistantMutation = (): UseMutationResult<
|
|
101
|
+
t.Assistant,
|
|
102
|
+
Error,
|
|
103
|
+
{ assistant_id: string; data: t.AssistantUpdateParams }
|
|
104
|
+
> => {
|
|
105
|
+
const queryClient = useQueryClient();
|
|
106
|
+
return useMutation(
|
|
107
|
+
({ assistant_id, data }: { assistant_id: string; data: t.AssistantUpdateParams }) =>
|
|
108
|
+
dataService.updateAssistant(assistant_id, data),
|
|
109
|
+
{
|
|
110
|
+
onSuccess: (_, { assistant_id }) => {
|
|
111
|
+
// Invalidate and refetch assistant details query
|
|
112
|
+
queryClient.invalidateQueries([QueryKeys.assistant, assistant_id]);
|
|
113
|
+
// Optionally invalidate and refetch list of assistants
|
|
114
|
+
queryClient.invalidateQueries([QueryKeys.assistants]);
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Hook for deleting an assistant
|
|
122
|
+
*/
|
|
123
|
+
export const useDeleteAssistantMutation = (): UseMutationResult<
|
|
124
|
+
void,
|
|
125
|
+
Error,
|
|
126
|
+
{ assistant_id: string }
|
|
127
|
+
> => {
|
|
128
|
+
const queryClient = useQueryClient();
|
|
129
|
+
return useMutation(
|
|
130
|
+
({ assistant_id }: { assistant_id: string }) => dataService.deleteAssistant(assistant_id),
|
|
131
|
+
{
|
|
132
|
+
onSuccess: () => {
|
|
133
|
+
// Invalidate and refetch assistant list query
|
|
134
|
+
queryClient.invalidateQueries([QueryKeys.assistants]);
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
);
|
|
138
|
+
};
|
|
@@ -6,26 +6,12 @@ import {
|
|
|
6
6
|
UseMutationResult,
|
|
7
7
|
QueryObserverResult,
|
|
8
8
|
} from '@tanstack/react-query';
|
|
9
|
-
import * as t from '
|
|
10
|
-
import * as s from '
|
|
11
|
-
import * as
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
allConversations = 'allConversations',
|
|
16
|
-
conversation = 'conversation',
|
|
17
|
-
searchEnabled = 'searchEnabled',
|
|
18
|
-
user = 'user',
|
|
19
|
-
name = 'name', // user key name
|
|
20
|
-
models = 'models',
|
|
21
|
-
balance = 'balance',
|
|
22
|
-
endpoints = 'endpoints',
|
|
23
|
-
presets = 'presets',
|
|
24
|
-
searchResults = 'searchResults',
|
|
25
|
-
tokenCount = 'tokenCount',
|
|
26
|
-
availablePlugins = 'availablePlugins',
|
|
27
|
-
startupConfig = 'startupConfig',
|
|
28
|
-
}
|
|
9
|
+
import * as t from '../types';
|
|
10
|
+
import * as s from '../schemas';
|
|
11
|
+
import * as m from '../types/mutations';
|
|
12
|
+
import * as dataService from '../data-service';
|
|
13
|
+
import request from '../request';
|
|
14
|
+
import { QueryKeys } from '../keys';
|
|
29
15
|
|
|
30
16
|
export const useAbortRequestWithMessage = (): UseMutationResult<
|
|
31
17
|
void,
|
|
@@ -56,11 +42,11 @@ export const useGetUserQuery = (
|
|
|
56
42
|
});
|
|
57
43
|
};
|
|
58
44
|
|
|
59
|
-
export const useGetMessagesByConvoId = (
|
|
45
|
+
export const useGetMessagesByConvoId = <TData = s.TMessage[]>(
|
|
60
46
|
id: string,
|
|
61
|
-
config?: UseQueryOptions<s.TMessage[]>,
|
|
62
|
-
): QueryObserverResult<
|
|
63
|
-
return useQuery<s.TMessage[]>(
|
|
47
|
+
config?: UseQueryOptions<s.TMessage[], unknown, TData>,
|
|
48
|
+
): QueryObserverResult<TData> => {
|
|
49
|
+
return useQuery<s.TMessage[], unknown, TData>(
|
|
64
50
|
[QueryKeys.messages, id],
|
|
65
51
|
() => dataService.getMessagesByConvoId(id),
|
|
66
52
|
{
|
|
@@ -99,6 +85,40 @@ export const useGetConversationByIdQuery = (
|
|
|
99
85
|
);
|
|
100
86
|
};
|
|
101
87
|
|
|
88
|
+
/* like above, but first try the convos query data */
|
|
89
|
+
export const useGetConvoIdQuery = (
|
|
90
|
+
id: string,
|
|
91
|
+
config?: UseQueryOptions<s.TConversation>,
|
|
92
|
+
): QueryObserverResult<s.TConversation> => {
|
|
93
|
+
const queryClient = useQueryClient();
|
|
94
|
+
return useQuery<s.TConversation>(
|
|
95
|
+
[QueryKeys.conversation, id],
|
|
96
|
+
() => {
|
|
97
|
+
const defaultQuery = () => dataService.getConversationById(id);
|
|
98
|
+
|
|
99
|
+
const convosQueryKey = [QueryKeys.allConversations, { pageNumber: '1', active: true }];
|
|
100
|
+
const convosQuery = queryClient.getQueryData<t.TGetConversationsResponse>(convosQueryKey);
|
|
101
|
+
|
|
102
|
+
if (!convosQuery) {
|
|
103
|
+
return defaultQuery();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const convo = convosQuery.conversations?.find((c) => c.conversationId === id);
|
|
107
|
+
if (convo) {
|
|
108
|
+
return convo;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return defaultQuery();
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
refetchOnWindowFocus: false,
|
|
115
|
+
refetchOnReconnect: false,
|
|
116
|
+
refetchOnMount: false,
|
|
117
|
+
...config,
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
};
|
|
121
|
+
|
|
102
122
|
//This isn't ideal because its just a query and we're using mutation, but it was the only way
|
|
103
123
|
//to make it work with how the Chat component is structured
|
|
104
124
|
export const useGetConversationByIdMutation = (id: string): UseMutationResult<s.TConversation> => {
|
|
@@ -208,7 +228,7 @@ export const useGetConversationsQuery = (
|
|
|
208
228
|
config?: UseQueryOptions<t.TGetConversationsResponse>,
|
|
209
229
|
): QueryObserverResult<t.TGetConversationsResponse> => {
|
|
210
230
|
return useQuery<t.TGetConversationsResponse>(
|
|
211
|
-
[QueryKeys.allConversations, pageNumber],
|
|
231
|
+
[QueryKeys.allConversations, { pageNumber, active: true }],
|
|
212
232
|
() => dataService.getConversations(pageNumber),
|
|
213
233
|
{
|
|
214
234
|
refetchOnReconnect: false,
|
|
@@ -237,6 +257,7 @@ export const useGetEndpointsQuery = <TData = t.TEndpointsConfig>(
|
|
|
237
257
|
[QueryKeys.endpoints],
|
|
238
258
|
() => dataService.getAIEndpoints(),
|
|
239
259
|
{
|
|
260
|
+
staleTime: Infinity,
|
|
240
261
|
refetchOnWindowFocus: false,
|
|
241
262
|
refetchOnReconnect: false,
|
|
242
263
|
refetchOnMount: false,
|
|
@@ -249,6 +270,7 @@ export const useGetModelsQuery = (
|
|
|
249
270
|
config?: UseQueryOptions<t.TModelsConfig>,
|
|
250
271
|
): QueryObserverResult<t.TModelsConfig> => {
|
|
251
272
|
return useQuery<t.TModelsConfig>([QueryKeys.models], () => dataService.getModels(), {
|
|
273
|
+
staleTime: Infinity,
|
|
252
274
|
refetchOnWindowFocus: false,
|
|
253
275
|
refetchOnReconnect: false,
|
|
254
276
|
refetchOnMount: false,
|
|
@@ -257,52 +279,27 @@ export const useGetModelsQuery = (
|
|
|
257
279
|
};
|
|
258
280
|
|
|
259
281
|
export const useCreatePresetMutation = (): UseMutationResult<
|
|
260
|
-
s.TPreset[],
|
|
261
|
-
unknown,
|
|
262
282
|
s.TPreset,
|
|
263
|
-
unknown
|
|
264
|
-
> => {
|
|
265
|
-
const queryClient = useQueryClient();
|
|
266
|
-
return useMutation((payload: s.TPreset) => dataService.createPreset(payload), {
|
|
267
|
-
onSuccess: () => {
|
|
268
|
-
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
269
|
-
},
|
|
270
|
-
});
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
export const useUpdatePresetMutation = (): UseMutationResult<
|
|
274
|
-
s.TPreset[],
|
|
275
283
|
unknown,
|
|
276
284
|
s.TPreset,
|
|
277
285
|
unknown
|
|
278
286
|
> => {
|
|
279
287
|
const queryClient = useQueryClient();
|
|
280
|
-
return useMutation((payload: s.TPreset) => dataService.
|
|
288
|
+
return useMutation((payload: s.TPreset) => dataService.createPreset(payload), {
|
|
281
289
|
onSuccess: () => {
|
|
282
290
|
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
283
291
|
},
|
|
284
292
|
});
|
|
285
293
|
};
|
|
286
294
|
|
|
287
|
-
export const useGetPresetsQuery = (
|
|
288
|
-
config?: UseQueryOptions<s.TPreset[]>,
|
|
289
|
-
): QueryObserverResult<s.TPreset[], unknown> => {
|
|
290
|
-
return useQuery<s.TPreset[]>([QueryKeys.presets], () => dataService.getPresets(), {
|
|
291
|
-
refetchOnWindowFocus: false,
|
|
292
|
-
refetchOnReconnect: false,
|
|
293
|
-
refetchOnMount: false,
|
|
294
|
-
...config,
|
|
295
|
-
});
|
|
296
|
-
};
|
|
297
|
-
|
|
298
295
|
export const useDeletePresetMutation = (): UseMutationResult<
|
|
299
|
-
|
|
296
|
+
m.PresetDeleteResponse,
|
|
300
297
|
unknown,
|
|
301
|
-
s.TPreset |
|
|
298
|
+
s.TPreset | undefined,
|
|
302
299
|
unknown
|
|
303
300
|
> => {
|
|
304
301
|
const queryClient = useQueryClient();
|
|
305
|
-
return useMutation((payload: s.TPreset |
|
|
302
|
+
return useMutation((payload: s.TPreset | undefined) => dataService.deletePreset(payload), {
|
|
306
303
|
onSuccess: () => {
|
|
307
304
|
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
308
305
|
},
|
|
@@ -348,11 +345,8 @@ export const useLoginUserMutation = (): UseMutationResult<
|
|
|
348
345
|
> => {
|
|
349
346
|
const queryClient = useQueryClient();
|
|
350
347
|
return useMutation((payload: t.TLoginUser) => dataService.login(payload), {
|
|
351
|
-
onSuccess: () => {
|
|
352
|
-
queryClient.invalidateQueries([QueryKeys.user]);
|
|
353
|
-
},
|
|
354
348
|
onMutate: () => {
|
|
355
|
-
queryClient.
|
|
349
|
+
queryClient.removeQueries();
|
|
356
350
|
},
|
|
357
351
|
});
|
|
358
352
|
};
|
|
@@ -371,15 +365,6 @@ export const useRegisterUserMutation = (): UseMutationResult<
|
|
|
371
365
|
});
|
|
372
366
|
};
|
|
373
367
|
|
|
374
|
-
export const useLogoutUserMutation = (): UseMutationResult<unknown> => {
|
|
375
|
-
const queryClient = useQueryClient();
|
|
376
|
-
return useMutation(() => dataService.logout(), {
|
|
377
|
-
onSuccess: () => {
|
|
378
|
-
queryClient.invalidateQueries([QueryKeys.user]);
|
|
379
|
-
},
|
|
380
|
-
});
|
|
381
|
-
};
|
|
382
|
-
|
|
383
368
|
export const useRefreshTokenMutation = (): UseMutationResult<
|
|
384
369
|
t.TRefreshTokenResponse,
|
|
385
370
|
unknown,
|
|
@@ -387,9 +372,14 @@ export const useRefreshTokenMutation = (): UseMutationResult<
|
|
|
387
372
|
unknown
|
|
388
373
|
> => {
|
|
389
374
|
const queryClient = useQueryClient();
|
|
390
|
-
return useMutation(() =>
|
|
375
|
+
return useMutation(() => request.refreshToken(), {
|
|
391
376
|
onMutate: () => {
|
|
392
|
-
queryClient.
|
|
377
|
+
queryClient.removeQueries();
|
|
378
|
+
localStorage.removeItem('lastConversationSetup');
|
|
379
|
+
localStorage.removeItem('lastSelectedModel');
|
|
380
|
+
localStorage.removeItem('lastSelectedTools');
|
|
381
|
+
localStorage.removeItem('filesToDelete');
|
|
382
|
+
localStorage.removeItem('lastAssistant');
|
|
393
383
|
},
|
|
394
384
|
});
|
|
395
385
|
};
|
package/src/request.ts
CHANGED
|
@@ -1,13 +1,57 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import axios, { AxiosRequestConfig, AxiosError } from 'axios';
|
|
3
|
-
/* TODO: fix dependency cycle */
|
|
4
|
-
// eslint-disable-next-line import/no-cycle
|
|
5
|
-
import { refreshToken } from './data-service';
|
|
6
3
|
import { setTokenHeader } from './headers-helpers';
|
|
4
|
+
import * as endpoints from './api-endpoints';
|
|
5
|
+
|
|
6
|
+
async function _get<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
|
|
7
|
+
const response = await axios.get(url, { ...options });
|
|
8
|
+
return response.data;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function _post(url: string, data?: any) {
|
|
12
|
+
const response = await axios.post(url, JSON.stringify(data), {
|
|
13
|
+
headers: { 'Content-Type': 'application/json' },
|
|
14
|
+
});
|
|
15
|
+
return response.data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function _postMultiPart(url: string, formData: FormData, options?: AxiosRequestConfig) {
|
|
19
|
+
const response = await axios.post(url, formData, {
|
|
20
|
+
...options,
|
|
21
|
+
headers: { 'Content-Type': 'multipart/form-data' },
|
|
22
|
+
});
|
|
23
|
+
return response.data;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function _put(url: string, data?: any) {
|
|
27
|
+
const response = await axios.put(url, JSON.stringify(data), {
|
|
28
|
+
headers: { 'Content-Type': 'application/json' },
|
|
29
|
+
});
|
|
30
|
+
return response.data;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function _delete<T>(url: string): Promise<T> {
|
|
34
|
+
const response = await axios.delete(url);
|
|
35
|
+
return response.data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function _deleteWithOptions<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
|
|
39
|
+
const response = await axios.delete(url, { ...options });
|
|
40
|
+
return response.data;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function _patch(url: string, data?: any) {
|
|
44
|
+
const response = await axios.patch(url, JSON.stringify(data), {
|
|
45
|
+
headers: { 'Content-Type': 'application/json' },
|
|
46
|
+
});
|
|
47
|
+
return response.data;
|
|
48
|
+
}
|
|
7
49
|
|
|
8
50
|
let isRefreshing = false;
|
|
9
51
|
let failedQueue: { resolve: (value?: any) => void; reject: (reason?: any) => void }[] = [];
|
|
10
52
|
|
|
53
|
+
const refreshToken = (retry?: boolean) => _post(endpoints.refreshToken(retry));
|
|
54
|
+
|
|
11
55
|
const processQueue = (error: AxiosError | null, token: string | null = null) => {
|
|
12
56
|
failedQueue.forEach((prom) => {
|
|
13
57
|
if (error) {
|
|
@@ -68,50 +112,6 @@ axios.interceptors.response.use(
|
|
|
68
112
|
},
|
|
69
113
|
);
|
|
70
114
|
|
|
71
|
-
async function _get<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
|
|
72
|
-
const response = await axios.get(url, { ...options });
|
|
73
|
-
return response.data;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async function _post(url: string, data?: any) {
|
|
77
|
-
const response = await axios.post(url, JSON.stringify(data), {
|
|
78
|
-
headers: { 'Content-Type': 'application/json' },
|
|
79
|
-
});
|
|
80
|
-
return response.data;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async function _postMultiPart(url: string, formData: FormData, options?: AxiosRequestConfig) {
|
|
84
|
-
const response = await axios.post(url, formData, {
|
|
85
|
-
...options,
|
|
86
|
-
headers: { 'Content-Type': 'multipart/form-data' },
|
|
87
|
-
});
|
|
88
|
-
return response.data;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async function _put(url: string, data?: any) {
|
|
92
|
-
const response = await axios.put(url, JSON.stringify(data), {
|
|
93
|
-
headers: { 'Content-Type': 'application/json' },
|
|
94
|
-
});
|
|
95
|
-
return response.data;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async function _delete<T>(url: string): Promise<T> {
|
|
99
|
-
const response = await axios.delete(url);
|
|
100
|
-
return response.data;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async function _deleteWithOptions<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
|
|
104
|
-
const response = await axios.delete(url, { ...options });
|
|
105
|
-
return response.data;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async function _patch(url: string, data?: any) {
|
|
109
|
-
const response = await axios.patch(url, JSON.stringify(data), {
|
|
110
|
-
headers: { 'Content-Type': 'application/json' },
|
|
111
|
-
});
|
|
112
|
-
return response.data;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
115
|
export default {
|
|
116
116
|
get: _get,
|
|
117
117
|
post: _post,
|
|
@@ -120,4 +120,5 @@ export default {
|
|
|
120
120
|
delete: _delete,
|
|
121
121
|
deleteWithOptions: _deleteWithOptions,
|
|
122
122
|
patch: _patch,
|
|
123
|
+
refreshToken,
|
|
123
124
|
};
|