|
| 1 | +import {useEffect} from 'react'; |
1 | 2 | import SearchBar from '@theme-original/SearchBar'; |
2 | 3 | import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; |
3 | 4 |
|
4 | | -// Wrap SearchBar to inject agentStudio: true into askAi config. |
5 | | -// Docusaurus 3.9.2's Joi validation doesn't recognize agentStudio yet, |
6 | | -// but @docsearch/react 4.6+ requires it for Agent Studio assistants. |
| 5 | +// Max suggestions to show (Hick's Law: fewer choices = faster decisions) |
| 6 | +const MAX_SUGGESTIONS = 4; |
| 7 | +const ALLOWED_PARTS = new Set(['step-start', 'text']); |
| 8 | + |
| 9 | +function injectSuggestionButtons(suggestions) { |
| 10 | + document.querySelectorAll('.askai-suggestions').forEach((el) => el.remove()); |
| 11 | + |
| 12 | + const responseArea = document.querySelector( |
| 13 | + '.DocSearch-AskAiScreen-Response-Container' |
| 14 | + ); |
| 15 | + if (!responseArea || !suggestions.length) return; |
| 16 | + |
| 17 | + const limited = suggestions.slice(0, MAX_SUGGESTIONS); |
| 18 | + |
| 19 | + // Accessible wrapper: role="group" + aria-live for screen readers |
| 20 | + const wrapper = document.createElement('div'); |
| 21 | + wrapper.className = 'askai-suggestions'; |
| 22 | + wrapper.setAttribute('role', 'group'); |
| 23 | + wrapper.setAttribute('aria-label', 'Follow-up suggestions'); |
| 24 | + wrapper.setAttribute('aria-live', 'polite'); |
| 25 | + |
| 26 | + // Screen reader announcement |
| 27 | + const srAnnounce = document.createElement('span'); |
| 28 | + srAnnounce.className = 'askai-sr-only'; |
| 29 | + srAnnounce.textContent = `${limited.length} follow-up suggestions available`; |
| 30 | + wrapper.appendChild(srAnnounce); |
| 31 | + |
| 32 | + limited.forEach((text) => { |
| 33 | + const btn = document.createElement('button'); |
| 34 | + btn.className = 'askai-suggestion-btn'; |
| 35 | + btn.textContent = text; |
| 36 | + btn.type = 'button'; |
| 37 | + btn.onclick = () => { |
| 38 | + const input = document.querySelector( |
| 39 | + '.DocSearch-Input, input[placeholder*="Ask"]' |
| 40 | + ); |
| 41 | + if (input) { |
| 42 | + const nativeSetter = Object.getOwnPropertyDescriptor( |
| 43 | + HTMLInputElement.prototype, |
| 44 | + 'value' |
| 45 | + ).set; |
| 46 | + nativeSetter.call(input, text); |
| 47 | + input.dispatchEvent(new Event('input', {bubbles: true})); |
| 48 | + input.focus(); |
| 49 | + input.dispatchEvent( |
| 50 | + new KeyboardEvent('keydown', { |
| 51 | + key: 'Enter', |
| 52 | + code: 'Enter', |
| 53 | + keyCode: 13, |
| 54 | + bubbles: true, |
| 55 | + }) |
| 56 | + ); |
| 57 | + } |
| 58 | + }; |
| 59 | + wrapper.appendChild(btn); |
| 60 | + }); |
| 61 | + |
| 62 | + const containers = document.querySelectorAll( |
| 63 | + '.DocSearch-AskAiScreen-Response-Container' |
| 64 | + ); |
| 65 | + const last = containers[containers.length - 1] || responseArea; |
| 66 | + last.after(wrapper); |
| 67 | +} |
| 68 | + |
| 69 | +function extractSuggestionsFromStream(response) { |
| 70 | + if (!response.ok || !response.body) return; |
| 71 | + const clone = response.clone(); |
| 72 | + const reader = clone.body.getReader(); |
| 73 | + const decoder = new TextDecoder(); |
| 74 | + let buffer = ''; |
| 75 | + |
| 76 | + function pump() { |
| 77 | + reader.read().then(({done, value}) => { |
| 78 | + if (done) return; |
| 79 | + buffer += decoder.decode(value, {stream: true}); |
| 80 | + const lines = buffer.split('\n'); |
| 81 | + buffer = lines.pop() || ''; |
| 82 | + for (const line of lines) { |
| 83 | + if (line.startsWith('data: ')) { |
| 84 | + try { |
| 85 | + const data = JSON.parse(line.slice(6)); |
| 86 | + if ( |
| 87 | + data.type === 'data-suggestions' && |
| 88 | + data.data?.suggestions |
| 89 | + ) { |
| 90 | + setTimeout( |
| 91 | + () => injectSuggestionButtons(data.data.suggestions), |
| 92 | + 500 |
| 93 | + ); |
| 94 | + } |
| 95 | + } catch { |
| 96 | + // ignore |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + pump(); |
| 101 | + }); |
| 102 | + } |
| 103 | + pump(); |
| 104 | +} |
| 105 | + |
| 106 | +function useAgentStudioFetchPatch(appId) { |
| 107 | + useEffect(() => { |
| 108 | + if (!appId) return; |
| 109 | + const endpoint = `https://${appId}.algolia.net/agent-studio/1`; |
| 110 | + const originalFetch = window.fetch; |
| 111 | + |
| 112 | + window.fetch = function (url, options) { |
| 113 | + if ( |
| 114 | + typeof url === 'string' && |
| 115 | + url.startsWith(endpoint) && |
| 116 | + options?.method === 'POST' && |
| 117 | + options?.body |
| 118 | + ) { |
| 119 | + try { |
| 120 | + const body = JSON.parse(options.body); |
| 121 | + |
| 122 | + // Fix assistant message parts for follow-ups |
| 123 | + if (Array.isArray(body.messages) && body.messages.length > 1) { |
| 124 | + body.messages = body.messages.map((msg) => { |
| 125 | + if (msg.role === 'assistant' && Array.isArray(msg.parts)) { |
| 126 | + const cleaned = msg.parts.filter((p) => |
| 127 | + ALLOWED_PARTS.has(p.type) |
| 128 | + ); |
| 129 | + if (!cleaned.some((p) => p.type === 'step-start')) { |
| 130 | + cleaned.unshift({type: 'step-start'}); |
| 131 | + } |
| 132 | + return {...msg, parts: cleaned}; |
| 133 | + } |
| 134 | + return msg; |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + const result = originalFetch.call(this, url, { |
| 139 | + ...options, |
| 140 | + body: JSON.stringify(body), |
| 141 | + }); |
| 142 | + |
| 143 | + result.then(extractSuggestionsFromStream); |
| 144 | + |
| 145 | + return result; |
| 146 | + } catch { |
| 147 | + // pass through |
| 148 | + } |
| 149 | + } |
| 150 | + return originalFetch.apply(this, arguments); |
| 151 | + }; |
| 152 | + |
| 153 | + return () => { |
| 154 | + window.fetch = originalFetch; |
| 155 | + }; |
| 156 | + }, [appId]); |
| 157 | +} |
| 158 | + |
7 | 159 | export default function SearchBarWrapper(props) { |
8 | 160 | const {siteConfig} = useDocusaurusContext(); |
9 | 161 | const askAi = siteConfig.themeConfig.algolia?.askAi; |
10 | | - if (askAi && !askAi.agentStudio) { |
11 | | - askAi.agentStudio = true; |
| 162 | + if (askAi) { |
| 163 | + if (!askAi.agentStudio) askAi.agentStudio = true; |
12 | 164 | } |
| 165 | + useAgentStudioFetchPatch(siteConfig.themeConfig.algolia?.appId); |
13 | 166 | return <SearchBar {...props} />; |
14 | 167 | } |
0 commit comments