Unable to Generate Responses in AppGyver from Google Gemini API

I’m encountering errors within the Node-RED browser environment that hinder response generation in my AppGyver app. The flow integrates with Google Generative AI and functions correctly in Postman and local VS Code, but I cannot get a response in AppGyver . Below is the code;

const { GoogleGenerativeAI } = require(“@google/generative-ai”);
const moment = require(‘moment’);

// Ensure API key is accessible
apiKey = “apiKey”; // Replace with your actual API key

// Initialize API client
// Access the API key from the incoming message:
const genAI = new GoogleGenerativeAI(msg.apiKey);

async function query(userText, messages = ) {
try {
// Prepare input data
const data = {
user_text: userText,
messages: messages, // Adjust messages array as needed
};

// Initiate chat with existing history
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const chat = model.startChat({
  history: [
    ...data.messages.map(msg => ({ role: "user", parts: msg.user_input })),
    ...data.messages.map(msg => ({ role: "model", parts: msg.response })),
  ],
  generationConfig: {
    maxOutputTokens: 100
  }
});

// Send user text directly (no need for extraction)
const result = await chat.sendMessage(userText);

const response = await result.response;
const generatedText = response.text();

// Format output
const formattedOutput = {
  datetime: moment().format(),
  response: generatedText,
  user_input: userText,
};

return formattedOutput; // Output for AppGyver

} catch (error) {
console.error(error);
return { error: ‘Error: Could not generate response. Please try again.’ };
}
}

// Example usage within AppGyver (replace with your actual logic):
const userInput = “What is generative AI?”;
const chatHistory = ; // Adjust based on your chat data
const formattedResponse = await query(userInput, chatHistory);
// Use formattedResponse.response within your AppGyver components