feat: handle session expiration gracefully

pull/142/head
Travis Fischer 2022-12-15 00:44:13 -06:00
rodzic 6b6de5845c
commit 0d09ca965f
1 zmienionych plików z 50 dodań i 36 usunięć

Wyświetl plik

@ -2,9 +2,8 @@ import delay from 'delay'
import html2md from 'html-to-md' import html2md from 'html-to-md'
import type { Browser, HTTPRequest, HTTPResponse, Page } from 'puppeteer' import type { Browser, HTTPRequest, HTTPResponse, Page } from 'puppeteer'
import * as types from './types'
import { getBrowser, getOpenAIAuth } from './openai-auth' import { getBrowser, getOpenAIAuth } from './openai-auth'
import { isRelevantRequest, minimizePage } from './utils' import { isRelevantRequest, maximizePage, minimizePage } from './utils'
export class ChatGPTAPIBrowser { export class ChatGPTAPIBrowser {
protected _markdown: boolean protected _markdown: boolean
@ -103,7 +102,7 @@ export class ChatGPTAPIBrowser {
return false return false
} }
// await minimizePage(this._page) await minimizePage(this._page)
this._page.on('request', this._onRequest.bind(this)) this._page.on('request', this._onRequest.bind(this))
this._page.on('response', this._onResponse.bind(this)) this._page.on('response', this._onResponse.bind(this))
@ -112,8 +111,6 @@ export class ChatGPTAPIBrowser {
} }
_onRequest = (request: HTTPRequest) => { _onRequest = (request: HTTPRequest) => {
if (!this._debug) return
const url = request.url() const url = request.url()
if (!isRelevantRequest(url)) { if (!isRelevantRequest(url)) {
return return
@ -140,17 +137,17 @@ export class ChatGPTAPIBrowser {
// } // }
} }
console.log('\nrequest', { if (this._debug) {
url, console.log('\nrequest', {
method, url,
headers: request.headers(), method,
body headers: request.headers(),
}) body
})
}
} }
_onResponse = async (response: HTTPResponse) => { _onResponse = async (response: HTTPResponse) => {
if (!this._debug) return
const request = response.request() const request = response.request()
const url = response.url() const url = response.url()
@ -158,37 +155,54 @@ export class ChatGPTAPIBrowser {
return return
} }
const status = response.status()
let body: any let body: any
try { try {
body = await response.json() body = await response.json()
} catch (_) {} } catch (_) {}
if (url.endsWith('/conversation')) { if (this._debug) {
// const parser = createParser((event) => { console.log('\nresponse', {
// if (event.type === 'event') { url,
// onMessage(event.data) ok: response.ok(),
// } status,
// }) statusText: response.statusText(),
// await response.buffer() headers: response.headers(),
// for await (const chunk of streamAsyncIterable(response.body)) { body,
// const str = new TextDecoder().decode(chunk) request: {
// parser.feed(str) method: request.method(),
// } headers: request.headers(),
body: request.postData()
}
})
} }
console.log('\nresponse', { if (url.endsWith('/conversation')) {
url, if (status === 403) {
ok: response.ok(), await this.handle403Error()
status: response.status(),
statusText: response.statusText(),
headers: response.headers(),
body,
request: {
method: request.method(),
headers: request.headers(),
body: request.postData()
} }
}) } else if (url.endsWith('api/auth/session')) {
if (status === 403) {
await this.handle403Error()
}
}
}
async handle403Error() {
console.log(`ChatGPT "${this._email}" session expired; refreshing...`)
try {
await maximizePage(this._page)
await this._page.reload({
waitUntil: 'networkidle0'
})
await minimizePage(this._page)
} catch (err) {
console.error(
`ChatGPT "${this._email}" error refreshing session`,
err.toString()
)
}
} }
async getIsAuthenticated() { async getIsAuthenticated() {