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
f9fe5b5b
Commit
f9fe5b5b
authored
Dec 11, 2022
by
Travis Fischer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: WIP attempting to match OpenAI's HTTP behavior
parent
e412ddcf
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
9 deletions
+18
-9
src/chatgpt-api.ts
src/chatgpt-api.ts
+18
-9
No files found.
src/chatgpt-api.ts
View file @
f9fe5b5b
...
@@ -10,7 +10,7 @@ import { markdownToText } from './utils'
...
@@ -10,7 +10,7 @@ import { markdownToText } from './utils'
const
KEY_ACCESS_TOKEN
=
'
accessToken
'
const
KEY_ACCESS_TOKEN
=
'
accessToken
'
const
USER_AGENT
=
const
USER_AGENT
=
'
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/10
7
.0.0.0 Safari/537.36
'
'
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/10
8
.0.0.0 Safari/537.36
'
export
class
ChatGPTAPI
{
export
class
ChatGPTAPI
{
protected
_sessionToken
:
string
protected
_sessionToken
:
string
...
@@ -22,7 +22,6 @@ export class ChatGPTAPI {
...
@@ -22,7 +22,6 @@ export class ChatGPTAPI {
protected
_headers
:
Record
<
string
,
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)
protected
_accessTokenCache
:
ExpiryMap
<
string
,
string
>
protected
_accessTokenCache
:
ExpiryMap
<
string
,
string
>
protected
_user
:
types
.
User
|
null
=
null
protected
_user
:
types
.
User
|
null
=
null
...
@@ -52,7 +51,7 @@ export class ChatGPTAPI {
...
@@ -52,7 +51,7 @@ export class ChatGPTAPI {
/** @defaultValue `'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'` **/
/** @defaultValue `'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'` **/
userAgent
?:
string
userAgent
?:
string
/** @defaultValue
60000 (60 seconds)
*/
/** @defaultValue
1 hour
*/
accessTokenTTL
?:
number
accessTokenTTL
?:
number
accessToken
?:
string
accessToken
?:
string
...
@@ -64,7 +63,7 @@ export class ChatGPTAPI {
...
@@ -64,7 +63,7 @@ export class ChatGPTAPI {
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
'
,
userAgent
=
USER_AGENT
,
userAgent
=
USER_AGENT
,
accessTokenTTL
=
60
000
,
// 60 seconds
accessTokenTTL
=
60
*
60000
,
// 1 hour
accessToken
accessToken
}
=
opts
}
=
opts
...
@@ -79,7 +78,13 @@ export class ChatGPTAPI {
...
@@ -79,7 +78,13 @@ export class ChatGPTAPI {
'
x-openai-assistant-app-id
'
:
''
,
'
x-openai-assistant-app-id
'
:
''
,
'
accept-language
'
:
'
en-US,en;q=0.9
'
,
'
accept-language
'
:
'
en-US,en;q=0.9
'
,
origin
:
'
https://chat.openai.com
'
,
origin
:
'
https://chat.openai.com
'
,
referer
:
'
https://chat.openai.com/chat
'
referer
:
'
https://chat.openai.com/chat
'
,
'
sec-ch-ua
'
:
'
"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"
'
,
'
sec-ch-ua-platform
'
:
'
"macOS"
'
,
'
sec-fetch-dest
'
:
'
empty
'
,
'
sec-fetch-mode
'
:
'
cors
'
,
'
sec-fetch-site
'
:
'
same-origin
'
}
}
this
.
_accessTokenCache
=
new
ExpiryMap
<
string
,
string
>
(
accessTokenTTL
)
this
.
_accessTokenCache
=
new
ExpiryMap
<
string
,
string
>
(
accessTokenTTL
)
...
@@ -269,11 +274,15 @@ export class ChatGPTAPI {
...
@@ -269,11 +274,15 @@ export class ChatGPTAPI {
let
response
:
Response
let
response
:
Response
try
{
try
{
const
headers
=
{
...
this
.
_headers
,
cookie
:
`cf_clearance=
${
this
.
_clearanceToken
}
; __Secure-next-auth.session-token=
${
this
.
_sessionToken
}
`
,
accept
:
'
*/*
'
}
console
.
log
(
`
${
this
.
_apiBaseUrl
}
/auth/session`
,
headers
)
const
res
=
await
fetch
(
`
${
this
.
_apiBaseUrl
}
/auth/session`
,
{
const
res
=
await
fetch
(
`
${
this
.
_apiBaseUrl
}
/auth/session`
,
{
headers
:
{
headers
...
this
.
_headers
,
cookie
:
`cf_clearance=
${
this
.
_clearanceToken
}
; __Secure-next-auth.session-token=
${
this
.
_sessionToken
}
`
}
}).
then
((
r
)
=>
{
}).
then
((
r
)
=>
{
response
=
r
response
=
r
...
...
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