Skip to content

Commit

Permalink
feat: export_file
Browse files Browse the repository at this point in the history
  • Loading branch information
veasion committed Dec 15, 2024
1 parent 4b70cef commit 31e6993
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
99 changes: 99 additions & 0 deletions src/background/tools/export_file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Tool, InputSchema } from "../../types/action.types";
import * as utils from "../utils";

/**
* Export file
*/
export class ExportFile implements Tool {
name: string;
description: string;
input_schema: InputSchema;

constructor() {
this.name = "export_file";
this.description = "Content exported as a file, support text format";
this.input_schema = {
type: "object",
properties: {
content: {
type: "string",
description: "Export file content",
},
fileType: {
type: "string",
description: "File format type",
enum: ["txt", "csv", "md", "html", "js", "xml", "json", "yml", "sql"],
},
filename: {
type: "string",
description: "File name",
},
},
required: ["fileType", "content"],
};
}

/**
* export
*
* @param {*} params { fileType: 'csv', content: 'field1,field2\ndata1,data2' }
* @returns { success: true }
*/
async execute(params: unknown): Promise<unknown> {
if (
typeof params !== "object" ||
params === null ||
!("content" in params)
) {
throw new Error(
'Invalid parameters. Expected an object with a "content" property.'
);
}
let { fileType, filename, content } = params as any;
if (!fileType) {
fileType = "txt";
}
let type = "text/plain";
switch (fileType) {
case "csv":
type = "text/csv";
break;
case "md":
type = "text/markdown";
break;
case "html":
type = "text/html";
break;
case "js":
type = "application/javascript";
break;
case "xml":
type = "text/xml";
break;
case "json":
type = "application/json";
break;
}
if (!filename) {
filename = new Date().getTime() + "." + fileType;
}
let tabId = await utils.getCurrentTabId();
await chrome.scripting.executeScript({
target: { tabId: tabId as number },
func: exportFile,
args: [filename, type, content],
});
return { success: true };
}
}

function exportFile(filename: string, type: string, content: string) {
const blob = new Blob([content], { type: type });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
}
2 changes: 1 addition & 1 deletion src/background/tools/web_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class WebSearch implements Tool {
input_schema: InputSchema;

constructor() {
this.name = "webSearch";
this.name = "web_search";
this.description = "A web search tool";
this.input_schema = {
type: "object",
Expand Down

0 comments on commit 31e6993

Please sign in to comment.