kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: add timeout support to browser sendMessage
rodzic
0d09ca965f
commit
8e0f32b444
|
@ -1,5 +1,6 @@
|
||||||
import delay from 'delay'
|
import delay from 'delay'
|
||||||
import html2md from 'html-to-md'
|
import html2md from 'html-to-md'
|
||||||
|
import pTimeout from 'p-timeout'
|
||||||
import type { Browser, HTTPRequest, HTTPResponse, Page } from 'puppeteer'
|
import type { Browser, HTTPRequest, HTTPResponse, Page } from 'puppeteer'
|
||||||
|
|
||||||
import { getBrowser, getOpenAIAuth } from './openai-auth'
|
import { getBrowser, getOpenAIAuth } from './openai-auth'
|
||||||
|
@ -275,7 +276,14 @@ export class ChatGPTAPIBrowser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendMessage(message: string): Promise<string> {
|
async sendMessage(
|
||||||
|
message: string,
|
||||||
|
opts: {
|
||||||
|
timeoutMs?: number
|
||||||
|
} = {}
|
||||||
|
): Promise<string> {
|
||||||
|
const { timeoutMs } = opts
|
||||||
|
|
||||||
const inputBox = await this._getInputBox()
|
const inputBox = await this._getInputBox()
|
||||||
if (!inputBox) throw new Error('not signed in')
|
if (!inputBox) throw new Error('not signed in')
|
||||||
|
|
||||||
|
@ -294,6 +302,8 @@ export class ChatGPTAPIBrowser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const responseP = new Promise<string>(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
do {
|
do {
|
||||||
await delay(1000)
|
await delay(1000)
|
||||||
|
|
||||||
|
@ -303,10 +313,21 @@ export class ChatGPTAPIBrowser {
|
||||||
newLastMessage &&
|
newLastMessage &&
|
||||||
lastMessage?.toLowerCase() !== newLastMessage?.toLowerCase()
|
lastMessage?.toLowerCase() !== newLastMessage?.toLowerCase()
|
||||||
) {
|
) {
|
||||||
await delay(5000)
|
return resolve(newLastMessage)
|
||||||
return newLastMessage
|
|
||||||
}
|
}
|
||||||
} while (true)
|
} while (true)
|
||||||
|
} catch (err) {
|
||||||
|
return reject(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (timeoutMs) {
|
||||||
|
return pTimeout(responseP, {
|
||||||
|
milliseconds: timeoutMs
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return responseP
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async resetThread() {
|
async resetThread() {
|
||||||
|
|
Ładowanie…
Reference in New Issue