kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: add additional headers; add user accessor
rodzic
1a3afc3641
commit
14ac1af64b
|
@ -18,11 +18,14 @@ export class ChatGPTAPI {
|
|||
protected _apiBaseUrl: string
|
||||
protected _backendApiBaseUrl: string
|
||||
protected _userAgent: string
|
||||
protected _headers: Record<string, string>
|
||||
|
||||
// Stores access tokens for `accessTokenTTL` milliseconds before needing to refresh
|
||||
// (defaults to 60 seconds)
|
||||
protected _accessTokenCache: ExpiryMap<string, string>
|
||||
|
||||
protected _user: types.User | null = null
|
||||
|
||||
/**
|
||||
* Creates a new client wrapper around the unofficial ChatGPT REST API.
|
||||
*
|
||||
|
@ -64,6 +67,13 @@ export class ChatGPTAPI {
|
|||
this._apiBaseUrl = apiBaseUrl
|
||||
this._backendApiBaseUrl = backendApiBaseUrl
|
||||
this._userAgent = userAgent
|
||||
this._headers = {
|
||||
'User-Agent': this._userAgent,
|
||||
'x-openai-assistant-app-id': '',
|
||||
'accept-language': 'en-US,en;q=0.9',
|
||||
origin: 'https://chat.openai.com',
|
||||
referer: 'https://chat.openai.com/chat'
|
||||
}
|
||||
|
||||
this._accessTokenCache = new ExpiryMap<string, string>(accessTokenTTL)
|
||||
|
||||
|
@ -72,6 +82,13 @@ export class ChatGPTAPI {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently signed-in user, if authenticated, `null` otherwise.
|
||||
*/
|
||||
get user() {
|
||||
return this._user
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to ChatGPT, waits for the response to resolve, and returns
|
||||
* the response.
|
||||
|
@ -140,9 +157,10 @@ export class ChatGPTAPI {
|
|||
fetchSSE(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...this._headers,
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': this._userAgent
|
||||
Accept: 'text/event-stream',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
signal: abortSignal,
|
||||
|
@ -240,10 +258,10 @@ export class ChatGPTAPI {
|
|||
|
||||
let response: Response
|
||||
try {
|
||||
const res = await fetch('https://chat.openai.com/api/auth/session', {
|
||||
const res = await fetch(`${this._apiBaseUrl}/auth/session`, {
|
||||
headers: {
|
||||
cookie: `__Secure-next-auth.session-token=${this._sessionToken}`,
|
||||
'user-agent': this._userAgent
|
||||
...this._headers,
|
||||
cookie: `__Secure-next-auth.session-token=${this._sessionToken}`
|
||||
}
|
||||
}).then((r) => {
|
||||
response = r
|
||||
|
@ -286,6 +304,10 @@ export class ChatGPTAPI {
|
|||
}
|
||||
}
|
||||
|
||||
if (res.user) {
|
||||
this._user = res.user
|
||||
}
|
||||
|
||||
this._accessTokenCache.set(KEY_ACCESS_TOKEN, accessToken)
|
||||
return accessToken
|
||||
} catch (err: any) {
|
||||
|
|
|
@ -41,7 +41,7 @@ export type User = {
|
|||
/**
|
||||
* Email of the user
|
||||
*/
|
||||
email: string
|
||||
email?: string
|
||||
|
||||
/**
|
||||
* Image of the user
|
||||
|
@ -56,12 +56,12 @@ export type User = {
|
|||
/**
|
||||
* Groups the user is in
|
||||
*/
|
||||
groups: string[] | []
|
||||
groups: string[]
|
||||
|
||||
/**
|
||||
* Features the user is in
|
||||
*/
|
||||
features: string[] | []
|
||||
features: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Ładowanie…
Reference in New Issue