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
a891f107
Commit
a891f107
authored
Dec 18, 2022
by
Travis Fischer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: fix 403 handling during sendMessage
parent
837619d9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
28 deletions
+53
-28
src/chatgpt-api-browser.ts
src/chatgpt-api-browser.ts
+53
-28
No files found.
src/chatgpt-api-browser.ts
View file @
a891f107
...
@@ -296,7 +296,9 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -296,7 +296,9 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
async
refreshSession
()
{
async
refreshSession
()
{
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" session expired (403); refreshing...`
)
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" session expired (403); refreshing...`
)
try
{
try
{
await
maximizePage
(
this
.
_page
)
if
(
!
this
.
_minimize
)
{
await
maximizePage
(
this
.
_page
)
}
await
this
.
_page
.
reload
({
await
this
.
_page
.
reload
({
waitUntil
:
'
networkidle2
'
,
waitUntil
:
'
networkidle2
'
,
timeout
:
2
*
60
*
1000
// 2 minutes
timeout
:
2
*
60
*
1000
// 2 minutes
...
@@ -404,25 +406,6 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -404,25 +406,6 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
// onProgress
// onProgress
}
=
opts
}
=
opts
if
(
!
(
await
this
.
getIsAuthenticated
()))
{
console
.
log
(
`chatgpt re-authenticating
${
this
.
_email
}
`
)
try
{
await
this
.
resetSession
()
}
catch
(
err
)
{
console
.
warn
(
`chatgpt error re-authenticating
${
this
.
_email
}
`
,
err
.
toString
()
)
}
if
(
!
(
await
this
.
getIsAuthenticated
()))
{
const
error
=
new
types
.
ChatGPTError
(
'
Not signed in
'
)
error
.
statusCode
=
401
throw
error
}
}
const
url
=
`https://chat.openai.com/backend-api/conversation`
const
url
=
`https://chat.openai.com/backend-api/conversation`
const
body
:
types
.
ConversationJSONBody
=
{
const
body
:
types
.
ConversationJSONBody
=
{
action
,
action
,
...
@@ -444,14 +427,56 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -444,14 +427,56 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
body
.
conversation_id
=
conversationId
body
.
conversation_id
=
conversationId
}
}
// console.log('>>> EVALUATE', url, this._accessToken, body)
let
result
:
types
.
ChatResponse
|
types
.
ChatError
const
result
=
await
this
.
_page
.
evaluate
(
let
numTries
=
0
browserPostEventStream
,
url
,
do
{
this
.
_accessToken
,
if
(
!
(
await
this
.
getIsAuthenticated
()))
{
body
,
console
.
log
(
`chatgpt re-authenticating
${
this
.
_email
}
`
)
timeoutMs
)
try
{
await
this
.
resetSession
()
}
catch
(
err
)
{
console
.
warn
(
`chatgpt error re-authenticating
${
this
.
_email
}
`
,
err
.
toString
()
)
}
if
(
!
(
await
this
.
getIsAuthenticated
()))
{
const
error
=
new
types
.
ChatGPTError
(
'
Not signed in
'
)
error
.
statusCode
=
401
throw
error
}
}
try
{
// console.log('>>> EVALUATE', url, this._accessToken, body)
result
=
await
this
.
_page
.
evaluate
(
browserPostEventStream
,
url
,
this
.
_accessToken
,
body
,
timeoutMs
)
}
catch
(
err
)
{
// We catch all errors in `browserPostEventStream`, so this should really
// only happen if the page is refreshed or closed during its invocation.
// This may happen if we encounter a 401/403 and refresh the page in it's
// response handler or if the user has closed the page manually.
if
(
++
numTries
>=
2
)
{
const
error
=
new
types
.
ChatGPTError
(
err
.
toString
())
error
.
statusCode
=
err
.
response
?.
statusCode
error
.
statusText
=
err
.
response
?.
statusText
throw
error
}
console
.
warn
(
'
chatgpt sendMessage error; retrying...
'
,
err
.
toString
())
await
delay
(
5000
)
}
}
while
(
!
result
)
// console.log('<<< EVALUATE', result)
// console.log('<<< EVALUATE', result)
if
(
'
error
'
in
result
)
{
if
(
'
error
'
in
result
)
{
...
...
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