|
| 1 | +import { Agent, AgentContext } from "@eko-ai/eko"; |
| 2 | +import { LanguageModelV2ToolCallPart, ToolResult } from "@eko-ai/eko/types"; |
| 3 | + |
| 4 | +export default class WriteFileAgent extends Agent { |
| 5 | + constructor() { |
| 6 | + super({ |
| 7 | + name: "WriteFile", |
| 8 | + description: |
| 9 | + "File writing tool, used for writing content to local files.", |
| 10 | + tools: [ |
| 11 | + { |
| 12 | + name: "write_file", |
| 13 | + parameters: { |
| 14 | + type: "object", |
| 15 | + properties: { |
| 16 | + filename: { |
| 17 | + type: "string", |
| 18 | + description: |
| 19 | + "File name only, path is not supported. For example: data.md", |
| 20 | + }, |
| 21 | + content: { |
| 22 | + type: "string", |
| 23 | + description: "The content to write to the file.", |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + execute: async ( |
| 28 | + args: Record<string, unknown>, |
| 29 | + agentContext: AgentContext, |
| 30 | + toolCall: LanguageModelV2ToolCallPart |
| 31 | + ): Promise<ToolResult> => { |
| 32 | + return this.writeFile( |
| 33 | + args.filename as string, |
| 34 | + args.content as string |
| 35 | + ); |
| 36 | + }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + llms: [], |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + private async writeFile( |
| 44 | + filename: string, |
| 45 | + content: string |
| 46 | + ): Promise<ToolResult> { |
| 47 | + const sanitizedFilename = this.sanitizeFilename(filename); |
| 48 | + const encodedContent = encodeURIComponent(content); |
| 49 | + const dataUrl = `data:text/plain;charset=utf-8,${encodedContent}`; |
| 50 | + await new Promise<void>((resolve, reject) => { |
| 51 | + chrome.downloads.download( |
| 52 | + { |
| 53 | + url: dataUrl, |
| 54 | + filename: sanitizedFilename, |
| 55 | + saveAs: false, |
| 56 | + }, |
| 57 | + (downloadId) => { |
| 58 | + if (chrome.runtime.lastError) { |
| 59 | + reject(new Error(chrome.runtime.lastError.message)); |
| 60 | + } else if (downloadId === undefined) { |
| 61 | + reject(new Error("Failed to download: no download ID returned")); |
| 62 | + } else { |
| 63 | + resolve(); |
| 64 | + } |
| 65 | + } |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + return { |
| 70 | + content: [ |
| 71 | + { |
| 72 | + type: "text", |
| 73 | + text: `File written successfully: ${sanitizedFilename}`, |
| 74 | + }, |
| 75 | + ], |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + private sanitizeFilename(filename: string): string { |
| 80 | + const invalidChars = /[<>:"/\\|?*\x00-\x1f]/g; |
| 81 | + let sanitized = filename.replace(invalidChars, "_"); |
| 82 | + sanitized = sanitized.replace(/^[\s.]+|[\s.]+$/g, ""); |
| 83 | + if (!sanitized) { |
| 84 | + sanitized = "untitled.txt"; |
| 85 | + } |
| 86 | + if (sanitized.length > 255) { |
| 87 | + const ext = this.getFileExtension(sanitized); |
| 88 | + const nameWithoutExt = sanitized.slice(0, 255 - ext.length); |
| 89 | + sanitized = nameWithoutExt + ext; |
| 90 | + } |
| 91 | + return sanitized; |
| 92 | + } |
| 93 | + |
| 94 | + private getFileExtension(filename: string): string { |
| 95 | + const lastDot = filename.lastIndexOf("."); |
| 96 | + return lastDot !== -1 ? filename.slice(lastDot) : ""; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export { WriteFileAgent }; |
0 commit comments