import { checkFileSafety } from "../prompt/safety.js";
import { resolvePathUnderWorkspace } from "./tool-paths.js";
import { readFile } from "fs/promises";
import { basename } from "node:path";
import { Type } from "@sinclair/typebox";
//#region src/agent/tools/send-media.ts
const SendMediaSchema = Type.Object({
	filePath: Type.String({ description: "File to send. Relative paths are under the current agent workspace; absolute paths are used as given." }),
	mediaType: Type.Optional(Type.Enum({
		photo: "photo",
		video: "video",
		audio: "audio",
		document: "document"
	}, { description: "Type of media to send (auto-detected if not specified)" })),
	caption: Type.Optional(Type.String({ description: "Caption for the media" }))
});
function detectMediaType(filePath) {
	const ext = filePath.split(".").pop()?.toLowerCase();
	const imageExts = [
		"jpg",
		"jpeg",
		"png",
		"gif",
		"webp",
		"bmp",
		"svg"
	];
	const videoExts = [
		"mp4",
		"mov",
		"avi",
		"webm",
		"mkv"
	];
	const audioExts = [
		"mp3",
		"wav",
		"ogg",
		"m4a",
		"flac"
	];
	if (imageExts.includes(ext || "")) return "photo";
	if (videoExts.includes(ext || "")) return "video";
	if (audioExts.includes(ext || "")) return "audio";
	return "document";
}
function detectMimeType(filePath) {
	return {
		jpg: "image/jpeg",
		jpeg: "image/jpeg",
		png: "image/png",
		gif: "image/gif",
		webp: "image/webp",
		bmp: "image/bmp",
		svg: "image/svg+xml",
		mp4: "video/mp4",
		mov: "video/quicktime",
		avi: "video/x-msvideo",
		webm: "video/webm",
		mp3: "audio/mpeg",
		wav: "audio/wav",
		ogg: "audio/ogg",
		m4a: "audio/mp4",
		flac: "audio/flac",
		pdf: "application/pdf",
		doc: "application/msword",
		docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
		txt: "text/plain",
		zip: "application/zip",
		rar: "application/x-rar-compressed"
	}[filePath.split(".").pop()?.toLowerCase() || ""] || "application/octet-stream";
}
function createSendMediaTool(workspace, bus, getContext) {
	return {
		name: "send_media",
		description: "Send a media file (photo, video, audio, document) to the current conversation. Relative paths resolve to the current agent workspace (same as read_file).",
		parameters: SendMediaSchema,
		label: "📎 Send Media",
		async execute(_toolCallId, params, _signal) {
			const p = params;
			const ctx = getContext();
			if (!ctx) return {
				content: [{
					type: "text",
					text: "Error: No active conversation context"
				}],
				details: {}
			};
			const resolved = resolvePathUnderWorkspace(p.filePath, workspace);
			const safety = checkFileSafety("read", resolved);
			if (!safety.allowed) return {
				content: [{
					type: "text",
					text: `🚫 ${safety.message}`
				}],
				details: {}
			};
			try {
				const base64 = (await readFile(resolved)).toString("base64");
				const mimeType = detectMimeType(resolved);
				const mediaType = p.mediaType || detectMediaType(resolved);
				const dataUrl = `data:${mimeType};base64,${base64}`;
				const msg = {
					channel: ctx.channel,
					chat_id: ctx.chatId,
					content: p.caption || "",
					mediaUrl: dataUrl,
					mediaType
				};
				await bus.publishOutbound(msg);
				return {
					content: [{
						type: "text",
						text: `✅ Media sent: ${basename(resolved)} (${mediaType})`
					}],
					details: {
						filePath: resolved,
						mediaType
					}
				};
			} catch (error) {
				const errorMessage = error instanceof Error ? error.message : String(error);
				return {
					content: [{
						type: "text",
						text: `Error sending media: ${errorMessage}`
					}],
					details: { error: errorMessage }
				};
			}
		}
	};
}
//#endregion
export { createSendMediaTool };

//# sourceMappingURL=send-media.js.map