'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'
classConversation{
protected_api:ChatGPTAPI
protected_conversationId:string=undefined
protected_parentMessageId:string=undefined
constructor(api:ChatGPTAPI){
this._api=api
}
/**
* Sends a message to ChatGPT, waits for the response to resolve, and returns
* the response. If the conversation has not yet been started, this will
* automatically start the conversation.
* @param message - The plaintext message to send.
* @param opts.onProgress - Optional listener which will be called every time the partial response is updated
* @param opts.onConversationResponse - Optional listener which will be called every time a conversation response is received
* @returns The plaintext response from ChatGPT.
*/
asyncsendMessage(
message:string,
opts:{
onProgress?:(partialResponse:string)=>void
onConversationResponse?:(
response:types.ConversationResponseEvent
)=>void
}={}
){
const{onProgress,onConversationResponse}=opts
if(!this._conversationId){
returnthis._api.sendMessage(message,{
onProgress,
onConversationResponse:(response)=>{
this._conversationId=response.conversation_id
this._parentMessageId=response.message.id
onConversationResponse?.(response)
}
})
}
returnthis._api.sendMessage(message,{
conversationId:this._conversationId,
parentMessageId:this._parentMessageId,
onProgress,
onConversationResponse:(response)=>{
this._parentMessageId=response.message.id
onConversationResponse?.(response)
}
})
}
}
exportclassChatGPTAPI{
protected_sessionToken:string
protected_markdown:boolean
...
...
@@ -82,15 +133,25 @@ export class ChatGPTAPI {
* @param message - The plaintext message to send.
* @param opts.conversationId - Optional ID of the previous message in a conversation
* @param opts.onProgress - Optional listener which will be called every time the partial response is updated
* @param opts.onConversationResponse - Optional listener which will be called every time the partial response is updated with the full conversation response