forked from Chanzhaoyu/chatgpt-web
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
62 lines (56 loc) · 1.89 KB
/
index.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
import { SHA256 } from 'crypto-js'
import type { AxiosProgressEvent, GenericAbortSignal } from 'axios'
import { post } from '@/utils/request'
import { useSettingStore } from '@/store'
export function fetchChatAPI<T = any>(
prompt: string,
options?: { conversationId?: string; parentMessageId?: string },
signal?: GenericAbortSignal,
) {
return post<T>({
url: '/chat',
data: { prompt, options },
signal,
})
}
export function fetchChatConfig<T = any>() {
return post<T>({
url: '/config',
})
}
export function fetchChatAPIProcess<T = any>(
params: {
prompt: string
options?: { conversationId?: string; parentMessageId?: string }
signal?: GenericAbortSignal
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void },
) {
const settingStore = useSettingStore()
const dataParams = { prompt: params.prompt, options: params.options, systemMessage: settingStore.systemMessage }
// Sign GPTFORCN
const json = JSON.stringify(dataParams)
const timenow = Date.now()
const unSignStr = `appId=${import.meta.env.VITE_APP_API_ID}&appSecret=${import.meta.env.VITE_APP_API_SECRET}&data=${json}×tamp=${timenow}`
return post<T>({
url: '/chat-process',
data: dataParams,
headers: { appId: import.meta.env.VITE_APP_API_ID, timestamp: timenow, sign: SHA256(unSignStr).toString() },
signal: params.signal,
onDownloadProgress: params.onDownloadProgress,
})
}
export function fetchSession<T>() {
// Sign GPTFORCN
const timenow = Date.now()
const unSignStr = `appId=${import.meta.env.VITE_APP_API_ID}&appSecret=${import.meta.env.VITE_APP_API_SECRET}&data=×tamp=${timenow}`
return post<T>({
url: '/session',
headers: { appId: import.meta.env.VITE_APP_API_ID, timestamp: timenow, sign: SHA256(unSignStr).toString() },
})
}
export function fetchVerify<T>(token: string) {
return post<T>({
url: '/verify',
data: { token },
})
}