All files mineflayer-chatgpt.js

100% Statements 43/43
100% Branches 8/8
100% Functions 3/3
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 2x 2x 2x 7x 7x 3x 3x 1x 3x 2x 1x 1x 1x 1x 2x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x
"use strict"
import action from './action.js';
import Memory from './memory.js';
import OpenAI from 'openai';
 
const memory = new Memory();
 
function chatgpt(bot) {
 
  let api;
 
  bot.chatgpt = {};
 
  bot.chatgpt.setConfig = (apiKey, opts) => {
    api = new OpenAI({
      apiKey: apiKey
    });
    opts = opts || {};
    opts.model = opts.model || 'gpt-3.5-turbo';
  }
 
  bot.chatgpt.sendMessage = async (player, message) => {
    try {
      const reply = await action.respond(memory, api, player, message);
      bot.chat(reply);
    } catch (error) {
      if (error instanceof OpenAI.APIError) {
        console.error(`An OpenAI error has occurred: ${error.status} ${error.type} ${error.code} ${error.message}`);
      } else {
        console.error(`An unexpected error has occurred: ${error.message}`);
      }
    }
  };
 
}
 
const exports = {
  chatgpt: chatgpt
};
 
export {
  exports as default
};