forked from SciSharp/BotSharp-UI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.js
More file actions
225 lines (195 loc) · 7.71 KB
/
http.js
File metadata and controls
225 lines (195 loc) · 7.71 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import axios from 'axios';
import { getUserStore, globalErrorStore, loaderStore } from '$lib/helpers/store.js';
// Add a request interceptor to attach authentication tokens or headers
axios.interceptors.request.use(
(config) => {
// Add your authentication logic here
const user = getUserStore();
if (!skipLoader(config)) {
loaderStore.set(true);
}
// For example, attach an authentication token to the request headers
if (user.token)
config.headers.Authorization = `Bearer ${user.token}`;
return config;
},
(error) => {
loaderStore.set(false);
return Promise.reject(error);
}
);
// Add a response interceptor to handle 401 errors globally
axios.interceptors.response.use(
(response) => {
// If the request was successful, return the response
loaderStore.set(false);
const user = getUserStore();
const isExpired = Date.now() / 1000 > user.expires;
if (isExpired || response?.status === 401) {
redirectToLogin();
return Promise.reject('user token expired!');
}
return response;
},
(error) => {
loaderStore.set(false);
const user = getUserStore();
const isExpired = Date.now() / 1000 > user.expires;
if (isExpired || error?.response?.status === 401) {
redirectToLogin();
return Promise.reject(error);
} else if (!skipGlobalError(error.config)) {
globalErrorStore.set(true);
setTimeout(() => {
globalErrorStore.set(false);
}, 2500);
}
// Return the error to the calling function
return Promise.reject(error);
}
);
function redirectToLogin() {
const curUrl = window.location.pathname + window.location.search;
let loginUrl = 'login';
if (curUrl) {
loginUrl += `?redirect=${encodeURIComponent(curUrl)}`;
}
window.location.href = loginUrl;
}
/** @param {import('axios').InternalAxiosRequestConfig<any>} config */
function skipLoader(config) {
/** @type {RegExp[]} */
const postRegexes = [
new RegExp('http(s*)://(.*?)/conversation/(.*?)/(.*?)', 'g'),
new RegExp('http(s*)://(.*?)/agent', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/page', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/(.*?)/search', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/create', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/document/(.*?)/page', 'g'),
new RegExp('http(s*)://(.*?)/users', 'g'),
new RegExp('http(s*)://(.*?)/instruct/chat-completion', 'g'),
new RegExp('http(s*)://(.*?)/agent/(.*?)/code-scripts', 'g')
];
/** @type {RegExp[]} */
const putRegexes = [
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/update', 'g'),
new RegExp('http(s*)://(.*?)/conversation/(.*?)/update-message', 'g'),
new RegExp('http(s*)://(.*?)/conversation/(.*?)/update-tags', 'g'),
new RegExp('http(s*)://(.*?)/users', 'g'),
];
/** @type {RegExp[]} */
const deleteRegexes = [
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/delete-collection', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/data/(.*?)', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/data', 'g'),
];
/** @type {RegExp[]} */
const getRegexes = [
new RegExp('http(s*)://(.*?)/setting/(.*?)', 'g'),
new RegExp('http(s*)://(.*?)/user/me', 'g'),
new RegExp('http(s*)://(.*?)/plugin/menu', 'g'),
new RegExp('http(s*)://(.*?)/address/options(.*?)', 'g'),
new RegExp('http(s*)://(.*?)/conversation/(.*?)/files/(.*?)', 'g'),
new RegExp('http(s*)://(.*?)/llm-provider/(.*?)/models', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/collections', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/exist', 'g'),
new RegExp('http(s*)://(.*?)/role/options', 'g'),
new RegExp('http(s*)://(.*?)/role/(.*?)/details', 'g'),
new RegExp('http(s*)://(.*?)/user/(.*?)/details', 'g'),
new RegExp('http(s*)://(.*?)/agent/labels', 'g'),
new RegExp('http(s*)://(.*?)/conversation/state/keys', 'g'),
new RegExp('http(s*)://(.*?)/logger/instruction/log/keys', 'g'),
new RegExp('http(s*)://(.*?)/logger/conversation/(.*?)/content-log', 'g'),
new RegExp('http(s*)://(.*?)/logger/conversation/(.*?)/state-log', 'g'),
new RegExp('http(s*)://(.*?)/mcp/server-configs', 'g')
];
if (config.method === 'post' && postRegexes.some(regex => regex.test(config.url || ''))) {
return true;
}
if (config.method === 'put' && putRegexes.some(regex => regex.test(config.url || ''))) {
return true;
}
if (config.method === 'delete' && deleteRegexes.some(regex => regex.test(config.url || ''))) {
return true;
}
if (config.method === 'get' && getRegexes.some(regex => regex.test(config.url || ''))) {
return true;
}
return false;
}
/** @param {import('axios').InternalAxiosRequestConfig<any>} config */
function skipGlobalError(config) {
/** @type {RegExp[]} */
const postRegexes = [
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/page', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/(.*?)/search', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/create', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/document/(.*?)/page', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/create-collection', 'g'),
new RegExp('http(s*)://(.*?)/refresh-agents', 'g')
];
/** @type {RegExp[]} */
const putRegexes = [
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/update', 'g'),
new RegExp('http(s*)://(.*?)/role', 'g'),
new RegExp('http(s*)://(.*?)/user', 'g'),
new RegExp('http(s*)://(.*?)/conversation/(.*?)/update-message', 'g'),
new RegExp('http(s*)://(.*?)/conversation/(.*?)/update-tags', 'g')
];
/** @type {RegExp[]} */
const deleteRegexes = [
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/delete-collection', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/data/(.*?)', 'g'),
new RegExp('http(s*)://(.*?)/knowledge/vector/(.*?)/data', 'g'),
];
/** @type {RegExp[]} */
const getRegexes = [];
if (config.method === 'post' && postRegexes.some(regex => regex.test(config.url || ''))) {
return true;
}
if (config.method === 'put' && putRegexes.some(regex => regex.test(config.url || ''))) {
return true;
}
if (config.method === 'delete' && deleteRegexes.some(regex => regex.test(config.url || ''))) {
return true;
}
if (config.method === 'get' && getRegexes.some(regex => regex.test(config.url || ''))) {
return true;
}
return false;
}
/**
* @param {String} url
* @param {Object} args
* @returns {String}
*/
export function replaceUrl(url, args) {
const keys = Object.keys(args);
keys.forEach(key => {
// @ts-ignore
url = url.replace("{" + key + "}", args[key]);
});
return url;
}
/**
* Replace new line as <br>
* @param {string} text
* @returns string
*/
export function replaceNewLine(text) {
return text.replace(/(?:\r\n|\r|\n)/g, '<br>');
}
/**
* Replace unnecessary markdown
* @param {string} text
* @returns {string}
*/
export function replaceMarkdown(text) {
let res = text.replace(/#([\s]+)/g, '\\# ').replace(/[-|=]{3,}/g, '@@@');
let regex1 = new RegExp('\\*(.*)\\*', 'g');
let regex2 = new RegExp('\\*([\\*]+)\\*', 'g');
if (!regex1.test(text) || regex2.test(text)) {
res = res.replace(/\*/g, '\\*');
}
return res;
}