realcoloride / node_characterai

Unofficial Character AI wrapper for node.
https://beta.character.ai/
341 stars 72 forks source link

TypeError: Cannot read properties of undefined (reading 'length') #153

Closed devaoto closed 6 months ago

devaoto commented 6 months ago

When I first example code shown on the readme, it returns this error:

F:\ncai\node_modules\node_characterai\chat.js:70
            for (let i = 0; i < replies.length; i++) {
                                        ^
TypeError: Cannot read properties of undefined (reading 'length')
    at Chat.sendAndAwaitResponse (F:\ncai\node_modules\node_characterai\chat.js:70:41)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async F:\ncai\index.js:14:20

I also tried using authentication, it throws the same error

devaoto commented 6 months ago

Nevermind, fixed.

SeoulSKY commented 6 months ago

How did you fix the issue? I'm getting the same error

devaoto commented 6 months ago

@SeoulSKY It happens when you authenticate with guest. You have to authenticate with your session token first then you can interact with your ai. You can modify this code as you like:

const CharacterAI = require('node_characterai');
const readline = require('readline');
const characterAI = new CharacterAI();
require('dotenv').config();

function getUserInput(question) {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  return new Promise((resolve) => {
    rl.question(question, (answer) => {
      rl.close();
      resolve(answer);
    });
  });
}

(async () => {
  await characterAI.authenticateWithToken(process.env.SESSION_TOKEN);

  const characterId = process.env.CHARACTER_ID;

  const chat = await characterAI.createOrContinueChat(characterId);

  while (true) {
    const userInput = await getUserInput('You: ');
    const response = await chat.sendAndAwaitResponse(userInput, true);

    console.log('AI:', response.text);
  }
})();
SeoulSKY commented 6 months ago

Thank you so much! As you suggested, I tried using my own token instead of the guest token, and the example in README.md now works.

realcoloride commented 6 months ago

Hello, please see #155.