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