|
23 | 23 |
|
24 | 24 | #input-container {
|
25 | 25 | display: flex;
|
| 26 | + flex-direction: column; |
| 27 | + gap: 10px; |
| 28 | + } |
| 29 | + |
| 30 | + #persons-container { |
| 31 | + position: relative; |
| 32 | + display: flex; |
| 33 | + align-items: center; |
| 34 | + gap: 10px; |
| 35 | + } |
| 36 | + |
| 37 | + #query-container { |
| 38 | + display: flex; |
| 39 | + align-items: center; |
| 40 | + gap: 10px; |
| 41 | + } |
| 42 | + |
| 43 | + #persons-input { |
| 44 | + flex-grow: 1; |
| 45 | + padding: 8px; |
| 46 | + border: 1px solid #ccc; |
| 47 | + border-radius: 4px; |
26 | 48 | }
|
27 | 49 |
|
28 | 50 | #query-input {
|
29 | 51 | flex-grow: 1;
|
30 | 52 | padding: 8px;
|
31 | 53 | margin-right: 10px;
|
| 54 | + border: 1px solid #ccc; |
| 55 | + border-radius: 4px; |
| 56 | + } |
| 57 | + |
| 58 | + #autocomplete-list { |
| 59 | + position: absolute; |
| 60 | + top: 100%; |
| 61 | + left: 0; |
| 62 | + right: 100px; |
| 63 | + background: white; |
| 64 | + border: 1px solid #ccc; |
| 65 | + border-top: none; |
| 66 | + max-height: 200px; |
| 67 | + overflow-y: auto; |
| 68 | + z-index: 1000; |
| 69 | + display: none; |
| 70 | + } |
| 71 | + |
| 72 | + .autocomplete-item { |
| 73 | + padding: 10px; |
| 74 | + cursor: pointer; |
| 75 | + border-bottom: 1px solid #eee; |
| 76 | + } |
| 77 | + |
| 78 | + .autocomplete-item:hover { |
| 79 | + background-color: #f0f0f0; |
| 80 | + } |
| 81 | + |
| 82 | + .autocomplete-item.selected { |
| 83 | + background-color: #e6f3ff; |
32 | 84 | }
|
33 | 85 |
|
34 | 86 | button {
|
|
37 | 89 | color: white;
|
38 | 90 | border: none;
|
39 | 91 | cursor: pointer;
|
| 92 | + border-radius: 4px; |
| 93 | + } |
| 94 | + |
| 95 | + button:hover { |
| 96 | + background: #45a049; |
| 97 | + } |
| 98 | + |
| 99 | + .clear-btn { |
| 100 | + background: #f44336; |
| 101 | + padding: 8px 12px; |
| 102 | + } |
| 103 | + |
| 104 | + .clear-btn:hover { |
| 105 | + background: #da190b; |
40 | 106 | }
|
41 | 107 |
|
42 | 108 | .user-message {
|
|
47 | 113 | color: green;
|
48 | 114 | }
|
49 | 115 |
|
| 116 | + .error { |
| 117 | + color: red; |
| 118 | + } |
| 119 | + |
50 | 120 | pre {
|
51 | 121 | white-space: pre-wrap;
|
52 | 122 | background: #f5f5f5;
|
53 | 123 | padding: 10px;
|
54 | 124 | border-radius: 5px;
|
55 | 125 | }
|
| 126 | + |
| 127 | + label { |
| 128 | + font-weight: bold; |
| 129 | + min-width: 100px; |
| 130 | + flex-shrink: 0; |
| 131 | + } |
56 | 132 | </style>
|
57 | 133 | </head>
|
58 | 134 | <body>
|
59 | 135 | <div id="chat-container">
|
60 |
| - <h1>Assistant</h1> |
| 136 | + <h1>Chat with currently edited Wikipedia pages</h1> |
61 | 137 | <div id="messages"></div>
|
62 | 138 | <div id="input-container">
|
63 |
| - <input type="text" id="query-input" placeholder="What do you know about {a person found in index}"> |
64 |
| - <button onclick="sendQuery()">Send</button> |
| 139 | + <div id="persons-container"> |
| 140 | + <label for="persons-input">Select Person</label> |
| 141 | + <input type="text" id="persons-input" placeholder="Start typing to search for persons (or **)"> |
| 142 | + <button class="clear-btn" onclick="clearPersonSelection()">Clear</button> |
| 143 | + <div id="autocomplete-list"></div> |
| 144 | + </div> |
| 145 | + <div id="query-container"> |
| 146 | + <label for="query-input">Query</label> |
| 147 | + <input type="text" id="query-input" placeholder="What do you know about the selected person?"> |
| 148 | + <button onclick="sendQuery()">Send</button> |
| 149 | + </div> |
65 | 150 | </div>
|
66 | 151 | </div>
|
67 | 152 |
|
68 | 153 | <script>
|
| 154 | + let selectedPerson = null; |
| 155 | + let autocompleteData = []; |
| 156 | + let currentSelection = -1; |
| 157 | + |
| 158 | + // Initialize autocomplete functionality |
| 159 | + document.addEventListener('DOMContentLoaded', function () { |
| 160 | + const personsInput = document.getElementById('persons-input'); |
| 161 | + const autocompleteList = document.getElementById('autocomplete-list'); |
| 162 | + |
| 163 | + personsInput.addEventListener('input', function () { |
| 164 | + const query = this.value.trim(); |
| 165 | + if (query.length >= 2) { |
| 166 | + searchPersons(query); |
| 167 | + } else { |
| 168 | + hideAutocomplete(); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + personsInput.addEventListener('keydown', function (event) { |
| 173 | + const items = autocompleteList.querySelectorAll('.autocomplete-item'); |
| 174 | + |
| 175 | + if (event.key === 'ArrowDown') { |
| 176 | + event.preventDefault(); |
| 177 | + currentSelection = Math.min(currentSelection + 1, items.length - 1); |
| 178 | + updateSelection(items); |
| 179 | + } else if (event.key === 'ArrowUp') { |
| 180 | + event.preventDefault(); |
| 181 | + currentSelection = Math.max(currentSelection - 1, -1); |
| 182 | + updateSelection(items); |
| 183 | + } else if (event.key === 'Enter') { |
| 184 | + event.preventDefault(); |
| 185 | + if (currentSelection >= 0 && items[currentSelection]) { |
| 186 | + selectPerson(items[currentSelection].dataset.person); |
| 187 | + } |
| 188 | + } else if (event.key === 'Escape') { |
| 189 | + hideAutocomplete(); |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + // Hide autocomplete when clicking outside |
| 194 | + document.addEventListener('click', function (event) { |
| 195 | + if (!personsInput.contains(event.target) && !autocompleteList.contains(event.target)) { |
| 196 | + hideAutocomplete(); |
| 197 | + } |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + function searchPersons(query) { |
| 202 | + fetch('/assistant/personsSearch', { |
| 203 | + method: 'POST', |
| 204 | + headers: { |
| 205 | + 'Content-Type': 'application/json', |
| 206 | + }, |
| 207 | + body: JSON.stringify({query: query}), |
| 208 | + }) |
| 209 | + .then(response => response.json()) |
| 210 | + .then(data => { |
| 211 | + autocompleteData = data.persons || []; |
| 212 | + showAutocomplete(autocompleteData); |
| 213 | + }) |
| 214 | + .catch(error => { |
| 215 | + console.error('Error searching persons:', error); |
| 216 | + hideAutocomplete(); |
| 217 | + }); |
| 218 | + } |
| 219 | + |
| 220 | + function showAutocomplete(persons) { |
| 221 | + const autocompleteList = document.getElementById('autocomplete-list'); |
| 222 | + autocompleteList.innerHTML = ''; |
| 223 | + currentSelection = -1; |
| 224 | + |
| 225 | + if (persons.length === 0) { |
| 226 | + const noResultsItem = document.createElement('div'); |
| 227 | + noResultsItem.className = 'autocomplete-item'; |
| 228 | + noResultsItem.textContent = 'No persons found'; |
| 229 | + noResultsItem.style.color = '#999'; |
| 230 | + autocompleteList.appendChild(noResultsItem); |
| 231 | + } else { |
| 232 | + persons.forEach((person) => { |
| 233 | + const item = document.createElement('div'); |
| 234 | + item.className = 'autocomplete-item'; |
| 235 | + item.textContent = person.name; |
| 236 | + item.dataset.person = JSON.stringify(person); |
| 237 | + item.addEventListener('click', () => selectPerson(person)); |
| 238 | + autocompleteList.appendChild(item); |
| 239 | + }); |
| 240 | + } |
| 241 | + |
| 242 | + autocompleteList.style.display = 'block'; |
| 243 | + } |
| 244 | + |
| 245 | + function hideAutocomplete() { |
| 246 | + const autocompleteList = document.getElementById('autocomplete-list'); |
| 247 | + autocompleteList.style.display = 'none'; |
| 248 | + currentSelection = -1; |
| 249 | + } |
| 250 | + |
| 251 | + function updateSelection(items) { |
| 252 | + items.forEach((item, index) => { |
| 253 | + item.classList.toggle('selected', index === currentSelection); |
| 254 | + }); |
| 255 | + } |
| 256 | + |
| 257 | + function selectPerson(person) { |
| 258 | + if (typeof person === 'string') { |
| 259 | + person = JSON.parse(person); |
| 260 | + } |
| 261 | + |
| 262 | + selectedPerson = person; |
| 263 | + const personsInput = document.getElementById('persons-input'); |
| 264 | + personsInput.value = person.name; |
| 265 | + hideAutocomplete(); |
| 266 | + |
| 267 | + const queryInput = document.getElementById('query-input'); |
| 268 | + const placeholder = `What do you know about ${person.name}?`; |
| 269 | + queryInput.placeholder = placeholder; |
| 270 | + queryInput.value = placeholder; |
| 271 | + } |
| 272 | + |
| 273 | + function clearPersonSelection() { |
| 274 | + selectedPerson = null; |
| 275 | + const personsInput = document.getElementById('persons-input'); |
| 276 | + const queryInput = document.getElementById('query-input'); |
| 277 | + |
| 278 | + personsInput.value = ''; |
| 279 | + queryInput.placeholder = 'What do you know about the selected person?'; |
| 280 | + hideAutocomplete(); |
| 281 | + } |
| 282 | + |
69 | 283 | function sendQuery() {
|
70 | 284 | const queryInput = document.getElementById('query-input');
|
71 | 285 | const query = queryInput.value.trim();
|
72 | 286 |
|
73 | 287 | if (query === '') return;
|
74 | 288 |
|
75 |
| - addMessage('You: ' + query, 'user-message'); |
| 289 | + // Include selected person in the query context |
| 290 | + const queryContext = { |
| 291 | + query: query, |
| 292 | + selectedPerson: selectedPerson |
| 293 | + }; |
| 294 | + |
| 295 | + const displayQuery = selectedPerson |
| 296 | + ? `You: ${query} (about ${selectedPerson.name})` |
| 297 | + : `You: ${query}`; |
| 298 | + |
| 299 | + addMessage(displayQuery, 'user-message'); |
76 | 300 | queryInput.value = '';
|
77 | 301 |
|
78 | 302 | fetch('/assistant/query', {
|
79 | 303 | method: 'POST',
|
80 | 304 | headers: {
|
81 | 305 | 'Content-Type': 'application/json',
|
82 | 306 | },
|
83 |
| - body: JSON.stringify({query: query}), |
| 307 | + body: JSON.stringify(queryContext), |
84 | 308 | })
|
85 | 309 | .then(response => response.json())
|
86 | 310 | .then(data => {
|
|
0 commit comments