feat: add support for Cloudflare 'clearanceToken'

pull/98/head
Travis Fischer 2022-12-11 17:18:51 -06:00
rodzic 262d6fba4e
commit c083ad64d9
1 zmienionych plików z 10 dodań i 3 usunięć

Wyświetl plik

@ -14,6 +14,7 @@ const USER_AGENT =
export class ChatGPTAPI { export class ChatGPTAPI {
protected _sessionToken: string protected _sessionToken: string
protected _clearanceToken: string
protected _markdown: boolean protected _markdown: boolean
protected _apiBaseUrl: string protected _apiBaseUrl: string
protected _backendApiBaseUrl: string protected _backendApiBaseUrl: string
@ -37,6 +38,7 @@ export class ChatGPTAPI {
*/ */
constructor(opts: { constructor(opts: {
sessionToken: string sessionToken: string
clearanceToken: string
/** @defaultValue `true` **/ /** @defaultValue `true` **/
markdown?: boolean markdown?: boolean
@ -57,6 +59,7 @@ export class ChatGPTAPI {
}) { }) {
const { const {
sessionToken, sessionToken,
clearanceToken,
markdown = true, markdown = true,
apiBaseUrl = 'https://chat.openai.com/api', apiBaseUrl = 'https://chat.openai.com/api',
backendApiBaseUrl = 'https://chat.openai.com/backend-api', backendApiBaseUrl = 'https://chat.openai.com/backend-api',
@ -66,6 +69,7 @@ export class ChatGPTAPI {
} = opts } = opts
this._sessionToken = sessionToken this._sessionToken = sessionToken
this._clearanceToken = clearanceToken
this._markdown = !!markdown this._markdown = !!markdown
this._apiBaseUrl = apiBaseUrl this._apiBaseUrl = apiBaseUrl
this._backendApiBaseUrl = backendApiBaseUrl this._backendApiBaseUrl = backendApiBaseUrl
@ -79,7 +83,9 @@ export class ChatGPTAPI {
} }
this._accessTokenCache = new ExpiryMap<string, string>(accessTokenTTL) this._accessTokenCache = new ExpiryMap<string, string>(accessTokenTTL)
this._accessTokenCache.set(KEY_ACCESS_TOKEN, accessToken ?? '') if (accessToken) {
this._accessTokenCache.set(KEY_ACCESS_TOKEN, accessToken)
}
if (!this._sessionToken) { if (!this._sessionToken) {
throw new types.ChatGPTError('ChatGPT invalid session token') throw new types.ChatGPTError('ChatGPT invalid session token')
@ -164,7 +170,8 @@ export class ChatGPTAPI {
...this._headers, ...this._headers,
Authorization: `Bearer ${accessToken}`, Authorization: `Bearer ${accessToken}`,
Accept: 'text/event-stream', Accept: 'text/event-stream',
'Content-Type': 'application/json' 'Content-Type': 'application/json',
Cookie: `cf_clearance=${this._clearanceToken}`
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
signal: abortSignal, signal: abortSignal,
@ -265,7 +272,7 @@ export class ChatGPTAPI {
const res = await fetch(`${this._apiBaseUrl}/auth/session`, { const res = await fetch(`${this._apiBaseUrl}/auth/session`, {
headers: { headers: {
...this._headers, ...this._headers,
cookie: `__Secure-next-auth.session-token=${this._sessionToken}` cookie: `cf_clearance=${this._clearanceToken}; __Secure-next-auth.session-token=${this._sessionToken}`
} }
}).then((r) => { }).then((r) => {
response = r response = r