| 52 | * before each agent run to route the file to the correct IM channel. |
| 53 | */ |
| 54 | export function createSendFileTool( |
| 55 | fileOutputCallbackRef: { current: FileOutputCallback | null }, |
| 56 | ): ToolDefinition<typeof SendFileParams> { |
| 57 | return { |
| 58 | name: "send_file", |
| 59 | label: "Send File", |
| 60 | description: |
| 61 | "Send a file to the user via the current chat channel (Telegram, Slack, or Web). " + |
| 62 | "IMPORTANT: Do NOT proactively send files. Only use this tool when the user EXPLICITLY " + |
| 63 | "asks you to send, share, or deliver a file (e.g. '把文件发给我', 'send me the file', " + |
| 64 | "'share the result'). Never send intermediate/temporary files. When the user asks, " + |
| 65 | "send only the specific file(s) the user requested, not all generated files.", |
| 66 | promptSnippet: |
| 67 | "send_file: Send a file to the user ONLY when they explicitly request it. " + |
| 68 | "Never send files proactively or automatically.", |
| 69 | parameters: SendFileParams, |
| 70 | async execute( |
| 71 | _toolCallId, |
| 72 | params: SendFileInput, |
| 73 | _signal, |
| 74 | _onUpdate, |
| 75 | _ctx, |
| 76 | ): Promise<AgentToolResult<undefined>> { |
| 77 | const { filePath, caption } = params; |
| 78 | |
| 79 | // Validate file exists |
| 80 | if (!fs.existsSync(filePath)) { |
| 81 | return { |
| 82 | content: [{ type: "text", text: `Error: File not found: ${filePath}` }], |
| 83 | details: undefined, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | const stats = fs.statSync(filePath); |
| 88 | if (!stats.isFile()) { |
| 89 | return { |
| 90 | content: [ |
| 91 | { type: "text", text: `Error: Path is not a file: ${filePath}` }, |
| 92 | ], |
| 93 | details: undefined, |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | const filename = path.basename(filePath); |
| 98 | const mimeType = detectMimeType(filePath); |
| 99 | |
| 100 | // Emit the file output event |
| 101 | const callback = fileOutputCallbackRef.current; |
| 102 | if (callback) { |
| 103 | callback({ |
| 104 | type: "file_output", |
| 105 | filePath, |
| 106 | filename, |
| 107 | mimeType, |
| 108 | caption, |
| 109 | }); |
| 110 | } |
| 111 | |