package cc.unitmesh.viewer.web.webedit import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow /** * Abstract bridge for communication between Kotlin and WebView * * This interface defines the contract for bidirectional communication * between the Kotlin/Compose layer and the embedded WebView for web editing. */ interface WebEditBridge { /** * Current URL being viewed */ val currentUrl: StateFlow /** * Page title */ val pageTitle: StateFlow /** * Whether the page is loading */ val isLoading: StateFlow /** * Load progress (0-100) */ val loadProgress: StateFlow /** * Current DOM tree */ val domTree: StateFlow /** * Currently selected element */ val selectedElement: StateFlow /** * Last received D2Snap tree (compressed DOM snapshot for LLM consumption) */ val d2SnapTree: StateFlow /** * Last received accessibility tree (semantic snapshot for LLM consumption) */ val accessibilityTree: StateFlow /** * Last received actionable elements list (from accessibility tree extraction) */ val actionableElements: StateFlow> /** * Last action execution result from the browser */ val lastActionResult: StateFlow /** * Last captured screenshot from the WebView (for Vision LLM fallback) */ val lastScreenshot: StateFlow /** * Perform a structured browser action. This is the preferred API for LLM-driven automation * because callers can attach a unique [WebEditAction.id] and reliably correlate results. */ suspend fun performAction(action: WebEditAction) /** * Whether selection mode is enabled */ val isSelectionMode: StateFlow /** * Whether the bridge is ready */ val isReady: StateFlow /** * Last error message, if any */ val errorMessage: StateFlow /** * Navigate to a URL */ suspend fun navigateTo(url: String) /** * Reload the current page */ suspend fun reload() /** * Go back in history */ suspend fun goBack() /** * Go forward in history */ suspend fun goForward() /** * Enable or disable selection mode */ suspend fun setSelectionMode(enabled: Boolean) /** * Enable inspect mode (with Shadow DOM support) */ suspend fun enableInspectMode() /** * Disable inspect mode */ suspend fun disableInspectMode() /** * Highlight a specific element */ suspend fun highlightElement(selector: String) /** * Clear all highlights */ suspend fun clearHighlights() /** * Scroll to an element */ suspend fun scrollToElement(selector: String) /** * Refresh the DOM tree */ suspend fun refreshDOMTree() /** * Get D2Snap compressed DOM tree (optimized for LLM consumption) * Based on: https://arxiv.org/html/2508.04412v2 */ suspend fun refreshD2SnapTree() /** * Get Accessibility Tree (semantic tree for screen readers/LLM) * Based on: https://arxiv.org/html/2508.04412v2 */ suspend fun refreshAccessibilityTree() /** * Get only actionable elements from the page */ suspend fun refreshActionableElements() /** * Get element at specific coordinates */ suspend fun getElementAtPoint(x: Int, y: Int): DOMElement? /** * Get the HTML content of the selected element */ suspend fun getSelectedElementHtml(): String? /** * Click an element by selector (shadow DOM aware in injected script). */ suspend fun click(selector: String) /** * Type text into an element by selector (input/textarea/contenteditable). */ suspend fun typeText(selector: String, text: String, clearFirst: Boolean = true) /** * Select an option for a