/* * Copyright (C) 2025 TomTom Navigation B.V. * Licensed under the Apache License, Version 2.0 */ import "./ui-visibility.css"; import { isRecord } from "./record"; function showUiFlag(value: unknown): boolean | undefined { if (!isRecord(value) || typeof value.show_ui !== "boolean") return undefined; return value.show_ui; } /** The first text block, parsed, for a host that dropped the result `_meta`. */ function textPayload(result: Record): unknown { if (!Array.isArray(result.content)) return undefined; const first = result.content[0]; if (!isRecord(first) || first.type !== "text" || typeof first.text !== "string") return undefined; try { return JSON.parse(first.text); } catch { return undefined; } } /** * True unless `show_ui` is false. The result `_meta` wins; the text's `show_ui` * is the fallback when a host drops that `_meta`. */ export function shouldShowUI(result: unknown): boolean { if (!isRecord(result)) return true; return showUiFlag(result._meta) ?? showUiFlag(textPayload(result)) ?? true; } /** * Hides the map container and shows a compact status indicator. * Call this when show_ui is false. */ export function hideMapUI(): void { // Add class to collapse the widget height document.documentElement.classList.add("ui-hidden"); const mapContainer = document.getElementById("sdk-map"); if (mapContainer) { mapContainer.classList.remove("visible"); mapContainer.style.display = "none"; } let indicator = document.getElementById("ui-hidden-indicator"); if (!indicator) { indicator = document.createElement("div"); indicator.id = "ui-hidden-indicator"; indicator.innerHTML = `
Data processed
`; document.body.appendChild(indicator); } indicator.style.display = "block"; } /** * Shows a user-friendly error indicator when the tool call fails. * The actual error details are already visible to the agent in the tool response * for retry decisions — this is purely for the user's UI. */ export function showErrorUI(): void { // Collapse the widget like hideMapUI document.documentElement.classList.add("ui-hidden"); const mapContainer = document.getElementById("sdk-map"); if (mapContainer) { mapContainer.classList.remove("visible"); mapContainer.style.display = "none"; } const hiddenIndicator = document.getElementById("ui-hidden-indicator"); if (hiddenIndicator) { hiddenIndicator.style.display = "none"; } let indicator = document.getElementById("ui-error-indicator"); if (!indicator) { indicator = document.createElement("div"); indicator.id = "ui-error-indicator"; indicator.innerHTML = `
Something went wrong
`; document.body.appendChild(indicator); } indicator.style.display = "block"; } /** * Shows the map container and hides the minimal indicator. * Call this when show_ui is true (default behavior). */ export function showMapUI(): void { // Remove compact mode class document.documentElement.classList.remove("ui-hidden"); const mapContainer = document.getElementById("sdk-map"); if (mapContainer) { mapContainer.style.display = "block"; // Use requestAnimationFrame to ensure display:block is applied before adding visible class requestAnimationFrame(() => { mapContainer.classList.add("visible"); }); } const indicator = document.getElementById("ui-hidden-indicator"); if (indicator) { indicator.style.display = "none"; } }