EvanZhouDev / bard-ai

A lightweight library to access Google Bard.
https://www.npmjs.com/package/bard-ai
GNU General Public License v3.0
117 stars 31 forks source link

🐛 [BUG]: TypeError: Cannot read properties of undefined (reading 'createChat') - [v2.0.3] #63

Closed mashwishi closed 1 year ago

mashwishi commented 1 year ago

Prerequisites

Bug Description

I am trying to implement from 1.5.X to 2.0.3 and I have encountered TypeError: Cannot read properties of undefined (reading 'createChat') while following this new documentation for V2.

Console log:

TypeError: Cannot read properties of undefined (reading 'createChat')
    at handler (webpack-internal:///(api)/./pages/api/ai.ts:170:67)
    at D:\Mechi\node_modules\next\dist\server\api-utils\node.js:463:16
    at D:\Mechi\node_modules\next\dist\server\lib\trace\tracer.js:117:36
    at NoopContextManager.with (D:\Mechi\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:7057)
    at ContextAPI.with (D:\Mechi\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:516)
    at NoopTracer.startActiveSpan (D:\Mechi\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:18086)
    at ProxyTracer.startActiveSpan (D:\Mechi\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:18847)
    at D:\Mechi\node_modules\next\dist\server\lib\trace\tracer.js:106:107
    at NoopContextManager.with (D:\Mechi\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:7057)
    at ContextAPI.with (D:\Mechi\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:516)
    at NextTracerImpl.trace (D:\Mechi\node_modules\next\dist\server\lib\trace\tracer.js:106:32)
    at apiResolver (D:\Mechi\node_modules\next\dist\server\api-utils\node.js:461:63)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async DevServer.runApi (D:\Mechi\node_modules\next\dist\server\next-server.js:675:9)
    at async Object.fn (D:\Mechi\node_modules\next\dist\server\next-server.js:1146:35)
    at async Router.execute (D:\Mechi\node_modules\next\dist\server\router.js:315:32)
    at async DevServer.runImpl (D:\Mechi\node_modules\next\dist\server\base-server.js:615:29)
    at async DevServer.run (D:\Mechi\node_modules\next\dist\server\dev\next-dev-server.js:908:20)
    at async DevServer.handleRequestImpl (D:\Mechi\node_modules\next\dist\server\base-server.js:546:20)

Reproduction Steps

Code:


//other imports..
import Bard from "bard-ai";
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
//other code..
switch (req.method) {
  case 'POST':
    //other code..
    case 'bard':
      try {

        //For google bard only

        //If new conversation
        const newBard = req.body.newBard; 

        //If already have a previous
        const conversationID = req.body.conversationID;
        const responseID = req.body.responseID;
        const choiceID = req.body.choiceID;
        const _reqID = req.body._reqID;

        const myBard  = new Bard({
          "__Secure-1PSID": 'XXXXXXXXXXXXXXX',
          "__Secure-1PSIDTS": 'XXXXXXXXXXXXXXXXXXX',
        });

        const myChat = myBard.createChat();

        if(newBard) {

          const response = await myChat.ask(message);

          res.status(200).send({ bot: response, bard: myChat.export() });

          let obj: any;

          if (typeof response === 'string') {
            // response is already a string, so use it as it is
            obj = response;
          } else {
            // response is of type queryBardValidRes, stringify the object
            obj = JSON.stringify(response);
          }

        }else{

          let myConversationContinued = newBard.createChat({
            conversationID: conversationID,
            responseID: responseID,
            choiceID: choiceID,
            _reqID: _reqID,
          });

          const response = await myConversationContinued.ask(message);

          res.status(200).send({ bot: response });

          let obj: any;

          if (typeof response === 'string') {
            // response is already a string, so use it as it is
            obj = response;
          } else {
            // response is of type queryBardValidRes, stringify the object
            obj = JSON.stringify(response);
          }

        }
      }
      catch (error) {
        //Send temporary error message to users in the chat
        res.status(200).send({ bot: 'Something went wrong! I am experiencing high volume of request at the moment, Please try again later.' });
        console.error(error);
      }
    break;
    //other code..
  default:
    res.status(405).end(); //Method Not Allowed
  }
};

Other information

{
  "name": "XXXX",
  "version": "XXXX",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@headlessui/react": "^1.7.15",
    "@types/node": "20.4.1",
    "@types/react": "18.2.14",
    "@types/react-dom": "18.2.6",
    "autoprefixer": "10.4.14",
    "bard-ai": "^2.0.3",
    "daisyui": "^3.2.1",
    "eslint": "8.44.0",
    "eslint-config-next": "13.4.9",
    "flowbite": "^1.7.0",
    "next": "13.4.9",
    "next-pwa": "^5.6.0",
    "next-themes": "^0.2.1",
    "node-fetch": "^3.3.1",
    "postcss": "8.4.25",
    "react": "18.2.0",
    "tailwindcss": "3.3.2",
    "typescript": "5.1.6",
    "webpack": "^5.88.1",
    "zustand": "^4.3.9"
  },
  "devDependencies": { }
}

Occurance Rate

I can reproduce this bug 100% of the time.

mashwishi commented 1 year ago

This issue is fixed now..

Issue: no newBard in the payload that's why it is falling to the createChat that requires ids.. Solution: add newBard to the payload

{
  "message": "hello",
  "ai": "bard",
  "newBard": true
}

Now I have new issue which is related to initialization, I was not able to make work even I added 1PSID also tried 1PSID and 1PSIDTS also tried the other XPSID and tried other XPSID with XPSIDTS none of it works...

image

mashwishi commented 1 year ago

image

Fixed now i relogged in my gmail

time to relogin me 12 gmails :')

hahaha

mashwishi commented 1 year ago

Re-opening, issue still same when trying to recover conversation.

image

mashwishi commented 1 year ago

image is this how it really supposed to be ? well i can't see error from the syntax because it isnt throwing any..

mashwishi commented 1 year ago

image

Solution: it is not newBard it is myBard that variable doc use for the new Bard.

imagine coding at 2am-3am and you're reading this code T_T took me ours to realize.

mashwishi commented 1 year ago

I will keep this open until docs is updated so people can check this if they missed this part also

ThatXliner commented 1 year ago

I will keep this open until docs is updated so people can check this if they missed this part also

You could add an entry to the FAQ

EvanZhouDev commented 1 year ago

Should be all fixed.