const require_chunk = require("./chunk-CKQMccvm.cjs");
const require_TimelineEntry = require("./TimelineEntry-B_1zZF-e.cjs");
let clsx = require("clsx");
clsx = require_chunk.__toESM(clsx);
let lucide_react = require("lucide-react");
let react = require("react");
let react_jsx_runtime = require("react/jsx-runtime");
let _openuidev_react_headless = require("@openuidev/react-headless");
//#region src/components/ToolCall/BehindTheScenes.tsx
const BehindTheScenes = ({ isStreaming, toolCallsComplete, children }) => {
	const [userOverride, setUserOverride] = (0, react.useState)(null);
	const hasCompletedOnce = (0, react.useRef)(false);
	const prevStreaming = (0, react.useRef)(isStreaming);
	(0, react.useEffect)(() => {
		if (isStreaming && !prevStreaming.current) {
			setUserOverride(null);
			hasCompletedOnce.current = false;
		}
		prevStreaming.current = isStreaming;
	}, [isStreaming]);
	if (toolCallsComplete && !hasCompletedOnce.current) hasCompletedOnce.current = true;
	const toolsActive = !!isStreaming && !hasCompletedOnce.current;
	const isExpanded = userOverride !== null ? userOverride : toolsActive;
	const toggle = () => {
		setUserOverride((prev) => prev !== null ? !prev : !isExpanded);
	};
	const panelId = (0, react.useId)();
	return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
		className: "openui-behind-the-scenes",
		children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
			className: "openui-behind-the-scenes__toggle",
			onClick: toggle,
			type: "button",
			"aria-expanded": isExpanded,
			"aria-controls": panelId,
			children: [isExpanded ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.ChevronUp, {
				size: 14,
				className: "openui-behind-the-scenes__toggle-icon"
			}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.ChevronDown, {
				size: 14,
				className: "openui-behind-the-scenes__toggle-icon"
			}), toolsActive ? "Working..." : "Behind the scenes"]
		}), isExpanded && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
			className: "openui-behind-the-scenes__items",
			id: panelId,
			children
		})]
	});
};
//#endregion
//#region src/components/ToolCall/ToolCall.tsx
const ToolCallComponent = ({ toolCall, isStreaming, toolsDone, isLast = false, className }) => {
	const isRunning = !!isStreaming && !toolsDone;
	const actionLabel = isRunning ? `Calling the ${toolCall.function.name} tool` : `Called the ${toolCall.function.name} tool`;
	let parsedArgs = null;
	try {
		parsedArgs = JSON.parse(toolCall.function.arguments);
	} catch {}
	const hasRequest = parsedArgs && parsedArgs._request != null;
	const hasResponse = parsedArgs && parsedArgs._response != null;
	const requestStr = hasRequest ? JSON.stringify(parsedArgs._request, null, 2) : null;
	const responseStr = hasResponse ? JSON.stringify(parsedArgs._response, null, 2) : null;
	const plainArgs = !hasRequest && !hasResponse && toolCall.function.arguments ? (() => {
		try {
			return JSON.stringify(JSON.parse(toolCall.function.arguments), null, 2);
		} catch {
			return toolCall.function.arguments;
		}
	})() : null;
	return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
		className: (0, clsx.default)("openui-tool-call", className),
		children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
			className: "openui-tool-call__title-row",
			children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
				className: (0, clsx.default)("openui-tool-call__icon-wrapper", { "openui-tool-call__icon--blinking": isRunning && isLast }),
				children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.SquareCode, {
					size: 14,
					className: "openui-tool-call__icon"
				})
			}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
				className: (0, clsx.default)("openui-tool-call__name", { "openui-tool-call__name--shimmer": isRunning && isLast }),
				children: actionLabel
			})]
		}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
			className: (0, clsx.default)("openui-tool-call__connector", { "openui-tool-call__connector--last": isLast }),
			children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
				className: "openui-tool-call__args-block",
				children: [
					requestStr && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ToolCodeBlock, {
						type: "request",
						code: requestStr,
						isRunning: isRunning && !hasResponse,
						toolName: toolCall.function.name
					}),
					responseStr && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ToolCodeBlock, {
						type: "response",
						code: responseStr,
						isRunning: isRunning && isLast,
						toolName: toolCall.function.name
					}),
					plainArgs && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ToolCodeBlock, {
						type: "request",
						code: plainArgs,
						isRunning,
						toolName: toolCall.function.name
					})
				]
			})
		})]
	});
};
const ToolCodeBlock = ({ type, code, isRunning = false, toolName }) => {
	const [isExpanded, setIsExpanded] = (0, react.useState)(false);
	const label = type === "request" ? "Tool Request" : "Tool Response";
	const runningLabel = type === "request" ? `Sending request to ${toolName}...` : `Awaiting response from ${toolName}...`;
	return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
		className: "openui-tool-code-block",
		children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
			className: "openui-tool-code-block__header",
			onClick: () => setIsExpanded((v) => !v),
			type: "button",
			children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
				className: (0, clsx.default)("openui-tool-code-block__label", { "openui-tool-code-block__label--loading": isRunning }),
				children: isRunning ? runningLabel : label
			}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.ChevronDown, {
				size: 14,
				className: (0, clsx.default)("openui-tool-code-block__chevron", { "openui-tool-code-block__chevron--expanded": isExpanded })
			})]
		}), isExpanded && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
			className: "openui-tool-code-block__content",
			children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("pre", {
				className: "openui-tool-code-block__code",
				children: code
			})
		})]
	});
};
//#endregion
//#region src/components/ToolCall/SourceIcon.tsx
/**
* Favicon for a web-search source, falling back to a globe glyph when the image
* is missing, fails to load, or resolves to a default 16×16 placeholder.
*
* @category Components
*/
const SourceIcon = ({ src }) => {
	const [failed, setFailed] = (0, react.useState)(false);
	if (!src || failed) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.Globe, {
		size: 16,
		className: "openui-tool-call__icon"
	});
	return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
		src,
		alt: "",
		className: "openui-tool-call__source-logo",
		onLoad: (e) => {
			const img = e.currentTarget;
			if (img.naturalWidth <= 16 && img.naturalHeight <= 16) setFailed(true);
		},
		onError: () => setFailed(true)
	});
};
//#endregion
//#region src/components/ToolCall/ToolCallTimeline.tsx
const REVEAL_INTERVAL = 600;
/** Visually hidden but available to screen readers; inline so we don't depend on
*  scss (a sibling agent owns the stylesheet). */
const VISUALLY_HIDDEN = {
	position: "absolute",
	width: 1,
	height: 1,
	overflow: "hidden",
	clipPath: "inset(50%)",
	whiteSpace: "nowrap",
	border: 0,
	padding: 0,
	margin: -1
};
const isRunning = (a) => a.status === "streaming" || a.status === "executing";
/**
* The "Working… / Behind the scenes" timeline wrapper, driven by
* {@link ToolActivity}[] from `useToolActivities` instead of the branch's
* `ThinkItem[]`. Keeps the staggered reveal + toggle; "running" is read from the
* activity status (not an `isThinking` prop), and "current item ready" asks the
* status union instead of peeking at magic `_request`/`_response` keys.
*
* (The compact reveal uses a CSS fade — framer-motion is intentionally not a
* dependency of this package.)
*
* @category Components
*/
function ToolCallTimeline({ activities, isLast = false, detailedViewPanel, forceDefault = false }) {
	const thinking = (0, _openuidev_react_headless.useThread)((s) => s.isRunning) && isLast && activities.length > 0 && isRunning(activities[activities.length - 1]);
	const [expanded, setExpanded] = (0, react.useState)(false);
	const [revealedCount, setRevealedCount] = (0, react.useState)(() => isLast ? 1 : Math.max(activities.length, 1));
	const prevThinking = (0, react.useRef)(thinking);
	(0, react.useEffect)(() => {
		if (!prevThinking.current && thinking) {
			setRevealedCount(1);
			setExpanded(false);
		}
		if (prevThinking.current && !thinking) {
			setExpanded(false);
			setRevealedCount(activities.length);
		}
		prevThinking.current = thinking;
	}, [thinking, activities.length]);
	const currentReady = (() => {
		const a = activities[revealedCount - 1];
		return a ? a.status !== "streaming" : true;
	})();
	(0, react.useEffect)(() => {
		if (isLast && revealedCount < activities.length && currentReady) {
			const t = setTimeout(() => setRevealedCount((c) => c + 1), REVEAL_INTERVAL);
			return () => clearTimeout(t);
		}
	}, [
		isLast,
		activities.length,
		revealedCount,
		currentReady
	]);
	if (activities.length === 0) return null;
	const revealing = revealedCount < activities.length;
	const showCompact = (thinking || revealing) && !expanded;
	const current = activities[Math.min(revealedCount - 1, activities.length - 1)];
	const liveLabel = current.statusMessage ?? require_TimelineEntry.defaultLabel(current.status, current.toolName);
	const failedCount = !thinking && !revealing ? activities.filter((a) => a.status === "error").length : 0;
	const toggleLabel = thinking || revealing ? "Working..." : failedCount > 0 ? `Behind the scenes · ${failedCount} failed` : "Behind the scenes";
	return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
		className: "openui-behind-the-scenes",
		children: [
			/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
				role: "status",
				"aria-live": "polite",
				style: VISUALLY_HIDDEN,
				children: liveLabel
			}),
			/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
				className: "openui-behind-the-scenes__toggle",
				type: "button",
				"aria-expanded": expanded,
				onClick: () => setExpanded((v) => !v),
				children: [expanded ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.ChevronUp, {
					size: 14,
					className: "openui-behind-the-scenes__toggle-icon"
				}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.ChevronDown, {
					size: 14,
					className: "openui-behind-the-scenes__toggle-icon"
				}), toggleLabel]
			}),
			showCompact && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
				className: "openui-behind-the-scenes__items",
				children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
					className: "openui-behind-the-scenes__reveal-item",
					style: { width: "100%" },
					children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_TimelineEntry.TimelineEntry, {
						activity: current,
						isLast: true,
						detailedViewPanel,
						forceDefault
					})
				}, revealedCount)
			}),
			expanded && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
				className: "openui-behind-the-scenes__items",
				children: activities.map((a, idx) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
					style: { width: "100%" },
					children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_TimelineEntry.TimelineEntry, {
						activity: a,
						isLast: isLast && idx === activities.length - 1,
						detailedViewPanel,
						forceDefault
					})
				}, a.id))
			})
		]
	});
}
//#endregion
Object.defineProperty(exports, "BehindTheScenes", {
	enumerable: true,
	get: function() {
		return BehindTheScenes;
	}
});
Object.defineProperty(exports, "SourceIcon", {
	enumerable: true,
	get: function() {
		return SourceIcon;
	}
});
Object.defineProperty(exports, "ToolCallComponent", {
	enumerable: true,
	get: function() {
		return ToolCallComponent;
	}
});
Object.defineProperty(exports, "ToolCallTimeline", {
	enumerable: true,
	get: function() {
		return ToolCallTimeline;
	}
});

//# sourceMappingURL=ToolCall-TkSgO8op.cjs.map