import { n as trackSkillExecution } from "./reliability-B6TVU4OW.js";

//#region src/logging/tool-tracking.ts
/**
* Wrap an agent tool's `execute` method with duration/error tracking that
* feeds into the existing `trackSkillExecution()` → hub metrics push pipeline.
*
* Tracking is best-effort: failures in the tracking code never affect tool
* execution, and errors from the original execute are always re-thrown.
*/
function wrapToolWithTracking(tool, trackingName) {
	const originalExecute = tool.execute;
	tool.execute = async (toolCallId, params, signal, onUpdate) => {
		const startTime = Date.now();
		try {
			const result = await originalExecute.call(tool, toolCallId, params, signal, onUpdate);
			try {
				trackSkillExecution({
					skillName: trackingName,
					status: "ok",
					durationMs: Date.now() - startTime,
					occurredAt: startTime
				});
			} catch {}
			return result;
		} catch (err) {
			let status = "error";
			let errorMessage;
			try {
				if (signal?.aborted) status = "timeout";
				else if ((err && typeof err === "object" && "name" in err ? String(err.name) : "") === "AbortError") status = "timeout";
				else {
					const msg = err instanceof Error ? err.message : String(err);
					errorMessage = msg.slice(0, 200);
					const lowerMsg = msg.toLowerCase();
					if (lowerMsg.includes("unauthorized") || lowerMsg.includes("auth") || lowerMsg.includes("401") || lowerMsg.includes("forbidden")) status = "auth_error";
				}
				trackSkillExecution({
					skillName: trackingName,
					status,
					durationMs: Date.now() - startTime,
					errorMessage,
					occurredAt: startTime
				});
			} catch {}
			throw err;
		}
	};
	return tool;
}

//#endregion
export { wrapToolWithTracking as t };