From 14ac1af64be310c2bb39bceaa2225e4aa0671a9f Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Sun, 11 Dec 2022 05:43:39 -0600 Subject: [PATCH] feat: add additional headers; add user accessor --- src/chatgpt-api.ts | 32 +++++++++++++++++++++++++++----- src/types.ts | 6 +++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/chatgpt-api.ts b/src/chatgpt-api.ts index 05551bb..0ffcf98 100644 --- a/src/chatgpt-api.ts +++ b/src/chatgpt-api.ts @@ -18,11 +18,14 @@ export class ChatGPTAPI { protected _apiBaseUrl: string protected _backendApiBaseUrl: string protected _userAgent: string + protected _headers: Record // Stores access tokens for `accessTokenTTL` milliseconds before needing to refresh // (defaults to 60 seconds) protected _accessTokenCache: ExpiryMap + 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(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) { diff --git a/src/types.ts b/src/types.ts index 0e69c4b..61023e7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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[] } /**