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 _apiBaseUrl: string
|
||||||
protected _backendApiBaseUrl: string
|
protected _backendApiBaseUrl: string
|
||||||
protected _userAgent: string
|
protected _userAgent: string
|
||||||
|
protected _headers: Record<string, string>
|
||||||
|
|
||||||
// Stores access tokens for `accessTokenTTL` milliseconds before needing to refresh
|
// Stores access tokens for `accessTokenTTL` milliseconds before needing to refresh
|
||||||
// (defaults to 60 seconds)
|
// (defaults to 60 seconds)
|
||||||
protected _accessTokenCache: ExpiryMap<string, string>
|
protected _accessTokenCache: ExpiryMap<string, string>
|
||||||
|
|
||||||
|
protected _user: types.User | null = null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new client wrapper around the unofficial ChatGPT REST API.
|
* Creates a new client wrapper around the unofficial ChatGPT REST API.
|
||||||
*
|
*
|
||||||
|
@ -64,6 +67,13 @@ export class ChatGPTAPI {
|
||||||
this._apiBaseUrl = apiBaseUrl
|
this._apiBaseUrl = apiBaseUrl
|
||||||
this._backendApiBaseUrl = backendApiBaseUrl
|
this._backendApiBaseUrl = backendApiBaseUrl
|
||||||
this._userAgent = userAgent
|
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)
|
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
|
* Sends a message to ChatGPT, waits for the response to resolve, and returns
|
||||||
* the response.
|
* the response.
|
||||||
|
@ -140,9 +157,10 @@ export class ChatGPTAPI {
|
||||||
fetchSSE(url, {
|
fetchSSE(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
...this._headers,
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
'Content-Type': 'application/json',
|
Accept: 'text/event-stream',
|
||||||
'User-Agent': this._userAgent
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
signal: abortSignal,
|
signal: abortSignal,
|
||||||
|
@ -240,10 +258,10 @@ export class ChatGPTAPI {
|
||||||
|
|
||||||
let response: Response
|
let response: Response
|
||||||
try {
|
try {
|
||||||
const res = await fetch('https://chat.openai.com/api/auth/session', {
|
const res = await fetch(`${this._apiBaseUrl}/auth/session`, {
|
||||||
headers: {
|
headers: {
|
||||||
cookie: `__Secure-next-auth.session-token=${this._sessionToken}`,
|
...this._headers,
|
||||||
'user-agent': this._userAgent
|
cookie: `__Secure-next-auth.session-token=${this._sessionToken}`
|
||||||
}
|
}
|
||||||
}).then((r) => {
|
}).then((r) => {
|
||||||
response = r
|
response = r
|
||||||
|
@ -286,6 +304,10 @@ export class ChatGPTAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (res.user) {
|
||||||
|
this._user = res.user
|
||||||
|
}
|
||||||
|
|
||||||
this._accessTokenCache.set(KEY_ACCESS_TOKEN, accessToken)
|
this._accessTokenCache.set(KEY_ACCESS_TOKEN, accessToken)
|
||||||
return accessToken
|
return accessToken
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|
|
@ -41,7 +41,7 @@ export type User = {
|
||||||
/**
|
/**
|
||||||
* Email of the user
|
* Email of the user
|
||||||
*/
|
*/
|
||||||
email: string
|
email?: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image of the user
|
* Image of the user
|
||||||
|
@ -56,12 +56,12 @@ export type User = {
|
||||||
/**
|
/**
|
||||||
* Groups the user is in
|
* Groups the user is in
|
||||||
*/
|
*/
|
||||||
groups: string[] | []
|
groups: string[]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Features the user is in
|
* Features the user is in
|
||||||
*/
|
*/
|
||||||
features: string[] | []
|
features: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Ładowanie…
Reference in New Issue