-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chat.sendAndAwaitResponse throws TypeError: Cannot read properties of undefined (reading 'status') #157
Comments
Hello, (I think that) the reason you get this error is that it was loading data for a response and got another response so it mixed and did something wrong. A solution would be to swap to neo endpoints (the ones with the websocket) or find another solution for puppeteer (that i'm not aware of). |
I found a workaround. I used async-mutex to implement the Monitor design pattern, which prevents race conditions and ensures data consistency. A minimum working example: import CharacterAI from "node_characterai";
import {Mutex} from "async-mutex";
let chat;
const mutex = new Mutex();
(async () => {
const characterAI = new CharacterAI();
await characterAI.authenticateWithToken("TOKEN");
chat = await characterAI.createOrContinueChat("8_1NyR8w1dOXmI1uWaieQcd147hecbdIK7CeEAIrdJw");
})();
/**
* Send a message to the AI and get the response
* @param id {string} chat ID
* @param message {string} message to send
* @returns {Promise<Reply>} response from the AI
*/
export async function sendMessage(id, message) {
return await mutex.runExclusive(async () => {
await chat.changeToConversationId(id);
return await chat.sendAndAwaitResponse(message, true);
});
} Although I used another library for mutex, implementing it from scratch shouldn't be super difficult |
This is interesting! Thank you for the sample @SeoulSKY |
Hello there, Thank you for the contribution! |
Version: 1.2.7
I found the closed issue #26 which describes the exact same issue, but it seems like it's not resolved yet.
Error log
Looks like the problem is that it throws this error when you send another request before getting a response from the previous request.
I think it would be awesome if the library implements queuing requests if sendAndAwaitResponse() is called again before the previous sendAndAwaitResponse returns the response, or at least throws an error saying you can't call sendAndAwaitResponse() before the previous sendAndAwaitResponse() returns.
The text was updated successfully, but these errors were encountered: