withcatai / catai

Run AI ✨ assistant locally! with simple API for Node.js 🚀
https://withcatai.github.io/catai/
MIT License
450 stars 29 forks source link

remote-catai does not work , should wait ws.on('open') #65

Closed scenaristeur closed 8 months ago

scenaristeur commented 8 months ago

remote-catai example does not work :

the example send prompt before the ws is open. we should first modify modify remote-catai, adding in the _init() function

        this._ws.on('open', () => {
            this.emit("open")
         });

then we should wait for the 'open' event to send the prompt

import { RemoteCatAI } from "catai";

const catai = new RemoteCatAI("ws://localhost:3000");

catai.on("open", async () => {
  console.log("Connected");
  const response = await catai.prompt("Write me 100 words story", (token) => {
    process.stdout.write(token);
  });

  console.log(`Total text length: ${response.length}`);
  catai.close();
});