You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
857 B

11 months ago
  1. const fs = require("fs");
  2. const path = require("path");
  3. /**
  4. * Plugin to save chat history to a json file
  5. */
  6. const fileHistory = {
  7. name: "file-history-plugin",
  8. startupConfig: {
  9. params: {},
  10. },
  11. plugin: function ({
  12. filename = `history/chat-history-${new Date().toISOString()}.json`,
  13. } = {}) {
  14. return {
  15. name: this.name,
  16. setup(aibitat) {
  17. const folderPath = path.dirname(filename);
  18. // get path from filename
  19. if (folderPath) {
  20. fs.mkdirSync(folderPath, { recursive: true });
  21. }
  22. aibitat.onMessage(() => {
  23. const content = JSON.stringify(aibitat.chats, null, 2);
  24. fs.writeFile(filename, content, (err) => {
  25. if (err) {
  26. console.error(err);
  27. }
  28. });
  29. });
  30. },
  31. };
  32. },
  33. };
  34. module.exports = { fileHistory };