Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Chatgpt Puppeteer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
Chatgpt Puppeteer
Commits
14ac1af6
Commit
14ac1af6
authored
Dec 11, 2022
by
Travis Fischer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add additional headers; add user accessor
parent
1a3afc36
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
8 deletions
+30
-8
src/chatgpt-api.ts
src/chatgpt-api.ts
+27
-5
src/types.ts
src/types.ts
+3
-3
No files found.
src/chatgpt-api.ts
View file @
14ac1af6
...
...
@@ -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
)
{
...
...
src/types.ts
View file @
14ac1af6
...
...
@@ -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
[]
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment