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.

96 lines
3.4 KiB

11 months ago
  1. const { Deduplicator } = require("../utils/dedupe");
  2. const saveFileInBrowser = {
  3. name: "save-file-to-browser",
  4. startupConfig: {
  5. params: {},
  6. },
  7. plugin: function () {
  8. return {
  9. name: this.name,
  10. setup(aibitat) {
  11. // List and summarize the contents of files that are embedded in the workspace
  12. aibitat.function({
  13. super: aibitat,
  14. tracker: new Deduplicator(),
  15. name: this.name,
  16. description:
  17. "Save content to a file when the user explicity asks for a download of the file.",
  18. examples: [
  19. {
  20. prompt: "Save me that to a file named 'output'",
  21. call: JSON.stringify({
  22. file_content:
  23. "<content of the file we will write previous conversation>",
  24. filename: "output.txt",
  25. }),
  26. },
  27. {
  28. prompt: "Save me that to my desktop",
  29. call: JSON.stringify({
  30. file_content:
  31. "<content of the file we will write previous conversation>",
  32. filename: "<relevant filename>.txt",
  33. }),
  34. },
  35. {
  36. prompt: "Save me that to a file",
  37. call: JSON.stringify({
  38. file_content:
  39. "<content of the file we will write from previous conversation>",
  40. filename: "<descriptive filename>.txt",
  41. }),
  42. },
  43. ],
  44. parameters: {
  45. $schema: "http://json-schema.org/draft-07/schema#",
  46. type: "object",
  47. properties: {
  48. file_content: {
  49. type: "string",
  50. description: "The content of the file that will be saved.",
  51. },
  52. filename: {
  53. type: "string",
  54. description:
  55. "filename to save the file as with extension. Extension should be plaintext file extension.",
  56. },
  57. },
  58. additionalProperties: false,
  59. },
  60. handler: async function ({ file_content = "", filename }) {
  61. try {
  62. if (
  63. this.tracker.isDuplicate(this.name, { file_content, filename })
  64. ) {
  65. this.super.handlerProps.log(
  66. `${this.name} was called, but exited early since it was not a unique call.`
  67. );
  68. return `${filename} file has been saved successfully!`;
  69. }
  70. this.super.socket.send("fileDownload", {
  71. filename,
  72. b64Content:
  73. "data:text/plain;base64," +
  74. Buffer.from(file_content, "utf8").toString("base64"),
  75. });
  76. this.super.introspect(`${this.caller}: Saving file ${filename}.`);
  77. this.tracker.trackRun(this.name, { file_content, filename });
  78. return `${filename} file has been saved successfully and will be downloaded automatically to the users browser.`;
  79. } catch (error) {
  80. this.super.handlerProps.log(
  81. `save-file-to-browser raised an error. ${error.message}`
  82. );
  83. return `Let the user know this action was not successful. An error was raised while saving a file to the browser. ${error.message}`;
  84. }
  85. },
  86. });
  87. },
  88. };
  89. },
  90. };
  91. module.exports = {
  92. saveFileInBrowser,
  93. };