Commit df94c613 authored by simon3000's avatar simon3000

optional params for Conversation

parent 9e891f52
...@@ -16,16 +16,21 @@ const USER_AGENT = ...@@ -16,16 +16,21 @@ const USER_AGENT =
* manually pass the conversation ID and parent message ID for each message. * manually pass the conversation ID and parent message ID for each message.
*/ */
class Conversation { class Conversation {
protected _api: ChatGPTAPI api: ChatGPTAPI
protected _conversationId: string = undefined conversationId: string = undefined
protected _parentMessageId: string = undefined parentMessageId: string = undefined
/** /**
* Creates a new conversation wrapper around the ChatGPT API. * Creates a new conversation wrapper around the ChatGPT API.
* @param api - The ChatGPT API instance to use. * @param api - The ChatGPT API instance to use.
*/ */
constructor(api: ChatGPTAPI) { constructor(
this._api = api api: ChatGPTAPI,
opts: { conversationId?: string; parentMessageId?: string } = {}
) {
this.api = api
this.conversationId = opts.conversationId
this.parentMessageId = opts.parentMessageId
} }
/** /**
...@@ -54,23 +59,23 @@ class Conversation { ...@@ -54,23 +59,23 @@ class Conversation {
} = {} } = {}
) { ) {
const { onProgress, onConversationResponse } = opts const { onProgress, onConversationResponse } = opts
if (!this._conversationId) { if (!this.conversationId) {
return this._api.sendMessage(message, { return this.api.sendMessage(message, {
onProgress, onProgress,
onConversationResponse: (response) => { onConversationResponse: (response) => {
this._conversationId = response.conversation_id this.conversationId = response.conversation_id
this._parentMessageId = response.message.id this.parentMessageId = response.message.id
onConversationResponse?.(response) onConversationResponse?.(response)
} }
}) })
} }
return this._api.sendMessage(message, { return this.api.sendMessage(message, {
conversationId: this._conversationId, conversationId: this.conversationId,
parentMessageId: this._parentMessageId, parentMessageId: this.parentMessageId,
onProgress, onProgress,
onConversationResponse: (response) => { onConversationResponse: (response) => {
this._parentMessageId = response.message.id this.parentMessageId = response.message.id
onConversationResponse?.(response) onConversationResponse?.(response)
} }
}) })
...@@ -285,9 +290,13 @@ export class ChatGPTAPI { ...@@ -285,9 +290,13 @@ export class ChatGPTAPI {
/** /**
* Get a new Conversation instance, which can be used to send multiple messages as part of a single conversation. * Get a new Conversation instance, which can be used to send multiple messages as part of a single conversation.
* *
* @param opts.conversationId - Optional Data of the previous message in a conversation
* @param opts.parentMessageId - Optional Data of the previous message in a conversation
* @returns a new Conversation instance * @returns a new Conversation instance
*/ */
getConversation() { getConversation(
return new Conversation(this) opts: { conversationId?: string; parentMessageId?: string } = {}
) {
return new Conversation(this, opts)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment