| 12 | } |
| 13 | |
| 14 | export const createAttachFileTool: ToolFactory = (config: ToolConfiguration) => { |
| 15 | return tool({ |
| 16 | description: TOOL_DEFINITIONS.attach_file.description, |
| 17 | inputSchema: TOOL_DEFINITIONS.attach_file.schema, |
| 18 | execute: async ( |
| 19 | { path, mediaType, filename }, |
| 20 | { abortSignal } |
| 21 | ): Promise<AttachFileToolResult> => { |
| 22 | assert(typeof path === "string" && path.trim().length > 0, "attach_file requires a path"); |
| 23 | |
| 24 | try { |
| 25 | const result = await readAttachFileFromPath({ |
| 26 | path, |
| 27 | mediaType, |
| 28 | filename, |
| 29 | cwd: config.cwd, |
| 30 | runtime: config.runtime, |
| 31 | abortSignal, |
| 32 | }); |
| 33 | |
| 34 | if (result.type === "display") { |
| 35 | const label = formatDisplayOnlyFileLabel(result.file); |
| 36 | return { |
| 37 | type: "content", |
| 38 | value: [ |
| 39 | { |
| 40 | type: "text", |
| 41 | text: |
| 42 | `[File shown to user: ${label}. ` + |
| 43 | "This type is not supported as a model attachment, so the model will receive this notice. Use another tool to inspect or convert the file if needed.]", |
| 44 | }, |
| 45 | createDisplayOnlyFilePart(result.file), |
| 46 | ], |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | const attachment = result.attachment; |
| 51 | assert(attachment.data.length > 0, "attach_file produced empty attachment data"); |
| 52 | |
| 53 | return { |
| 54 | type: "content", |
| 55 | value: [ |
| 56 | { |
| 57 | type: "text", |
| 58 | text: `[Attachment prepared: ${attachment.filename ?? attachment.mediaType}]`, |
| 59 | }, |
| 60 | { |
| 61 | type: "media", |
| 62 | data: attachment.data, |
| 63 | mediaType: attachment.mediaType, |
| 64 | ...(attachment.filename ? { filename: attachment.filename } : {}), |
| 65 | }, |
| 66 | ], |
| 67 | }; |
| 68 | } catch (error) { |
| 69 | return { |
| 70 | success: false, |
| 71 | error: getErrorMessage(error), |