diff --git a/assets/css/docs-chat.css b/assets/css/docs-chat.css index 350cf67bfeff..6af6e9f3aca4 100644 --- a/assets/css/docs-chat.css +++ b/assets/css/docs-chat.css @@ -209,6 +209,9 @@ .gordon-send:hover:not(:disabled) { background: var(--color-primary-hover); } +.gordon-stop .ui-icon svg { + fill: currentColor; +} .gordon-send:disabled { background: var(--color-muted); color: var(--color-muted-foreground); diff --git a/assets/icons/trident/manifest.json b/assets/icons/trident/manifest.json index 33c3efbfbb05..eae7fe067a93 100644 --- a/assets/icons/trident/manifest.json +++ b/assets/icons/trident/manifest.json @@ -87,6 +87,7 @@ "smartphone.svg": "c0c7e8095e94862d5bac19c94a85fc0eb7f326ccf2d7204e65fa83f8df5bf71c", "sparkles.svg": "d280a594ef348ac262a7980ca837d4a52f35455205882dc729ca4de160ef3ff3", "square-pen.svg": "b313b64591a93e8aaf742592524bc45728e4734f7457cf7fa96eaf757cd38c22", + "square.svg": "bc37f3f8da7f8aa67cc5e258e0ae3fa345a40e63bca5d03c1c0ab8179fa876d2", "store.svg": "886dc45f5f92eca818a35ff27a8c26edb71d53dbc272ba6dd3fef1fc859ddf85", "sun.svg": "087147223eba41798492fddbcd6a94f4d0edd07ac0c0acdb8b8362e0f727b7c4", "terminal.svg": "0d7baa612415776511349b340eb0084e199f12180c6599dfaf350adb6876631e", diff --git a/assets/icons/trident/square.svg b/assets/icons/trident/square.svg new file mode 100644 index 000000000000..6455c4249795 --- /dev/null +++ b/assets/icons/trident/square.svg @@ -0,0 +1,15 @@ + + + + diff --git a/assets/js/src/alpine.js b/assets/js/src/alpine.js index 4afdcb13bd87..5ea25a666dbe 100644 --- a/assets/js/src/alpine.js +++ b/assets/js/src/alpine.js @@ -106,6 +106,63 @@ function loadGordonCards() { Alpine.magic("gordonCards", () => loadGordonCards); +// With the "browser" Gordon backend, the assistant runs in the reader's browser. Its loader +// script defines window.gordonAssistant and is only added once a reader shows interest in Gordon, +// never on a plain page view. +let gordonAssistantLoading; + +function loadGordonAssistant() { + gordonAssistantLoading ??= new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.src = window.GORDON_LOADER_URL; + script.onload = () => { + if (window.gordonAssistant) { + resolve(window.gordonAssistant); + } else { + gordonAssistantLoading = undefined; + reject( + gordonUnavailable("The Gordon loader did not define gordonAssistant"), + ); + } + }; + script.onerror = () => { + // A later call (such as opening the panel again) can try again. + gordonAssistantLoading = undefined; + script.remove(); + reject(gordonUnavailable("Failed to load the Gordon loader")); + }; + document.head.append(script); + }); + return gordonAssistantLoading; +} + +// Same code as the assistant's own load failures, so the chat panel falls back to the docs AI +// service for both. +function gordonUnavailable(message) { + return Object.assign(new Error(message), { code: "unavailable" }); +} + +Alpine.magic("gordonAssistant", () => loadGordonAssistant); + +// Even with the "browser" backend, the docs AI service can send readers back to the legacy chat. +// The page asks once per page view, before it loads the assistant. If the request fails, the page +// uses the legacy chat: the assistant needs the same service. +let gordonBackend; + +function resolveGordonBackend() { + if (window.GORDON_BACKEND !== "browser") return Promise.resolve("legacy"); + gordonBackend ??= fetch(window.GORDON_BACKEND_URL, { + signal: AbortSignal.timeout(5000), + }) + .then((response) => (response.ok ? response.json() : null)) + .then((body) => (body?.backend === "browser" ? "browser" : "legacy")) + .catch((err) => { + console.error("Gordon: using the legacy chat:", err); + return "legacy"; + }); + return gordonBackend; +} + // Swaps each ```openui-lang block for the card it describes. Until the card code has loaded, or // while a block is still streaming in and doesn't parse yet, it shows a placeholder. If the card // code can't load, or a finished block isn't a card, the block stays a plain code block. @@ -270,6 +327,26 @@ Alpine.store("gordon", { close() { this.isOpen = false; }, + // Downloads the in-browser assistant ahead of time, when a reader hovers over or focuses an + // "Ask Docker" control, or types in the home page composer (where Enter asks Docker). + prefetch() { + if (window.GORDON_BACKEND !== "browser") return; + resolveGordonBackend() + .then(async (backend) => { + if (backend !== "browser") return; + const assistant = await loadGordonAssistant(); + assistant.prefetch(); + }) + .catch((err) => console.error("Gordon:", err)); + }, + // Resolves to "browser" or "legacy": where this page view gets its answers. + backend() { + return resolveGordonBackend(); + }, + // For the rest of this page view, after the assistant failed to run. + useLegacyBackend() { + gordonBackend = Promise.resolve("legacy"); + }, }); Alpine.data("docsSearch", docsSearch); diff --git a/hugo.yaml b/hugo.yaml index 65430e6a0a25..6b099cc471a4 100644 --- a/hugo.yaml +++ b/hugo.yaml @@ -168,6 +168,10 @@ params: # Lets Gordon answer with cards (steps, tables, notes, snippets, follow-up # questions) in production builds. Always on in other environments. cards: true + # Where Gordon gets its answers: "legacy" (the docs AI service) or + # "browser" (an assistant that runs in the reader's browser). Override + # with the HUGO_GORDON_BACKEND environment variable. + backend: legacy # === Example versions === # The following parameters are used in various ways in content and templates. diff --git a/layouts/_partials/ask-dock.html b/layouts/_partials/ask-dock.html index dc685eede848..9ab839987953 100644 --- a/layouts/_partials/ask-dock.html +++ b/layouts/_partials/ask-dock.html @@ -35,6 +35,7 @@ placeholder="Ask a question about Docker…" autocomplete="off" x-model="query" + @focus="$store.gordon.prefetch()" />