Add Gradio API integration and refactor app.js. Introduced gradioPredict function for API calls, enhancing chat functionality and session management.
Browse files- assets/app.js +605 -574
- assets/gradio_api.js +52 -0
assets/app.js
CHANGED
|
@@ -1,574 +1,605 @@
|
|
| 1 |
-
import {
|
| 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 |
-
const
|
| 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 |
-
function
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
}
|
| 172 |
-
}
|
| 173 |
-
|
| 174 |
-
function
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
const
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
}
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
}
|
| 212 |
-
|
| 213 |
-
function
|
| 214 |
-
|
| 215 |
-
els.
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
els.
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
}
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
select.
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
element.
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
const
|
| 400 |
-
body.
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
const
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
const
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
}
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
}
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
}
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
setStatus(
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
}
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
}
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
els.
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { gradioPredict } from "/assets/gradio_api.js?v=2";
|
| 2 |
+
|
| 3 |
+
let markdownTools = null;
|
| 4 |
+
|
| 5 |
+
async function ensureMarkdownTools() {
|
| 6 |
+
if (markdownTools) {
|
| 7 |
+
return markdownTools;
|
| 8 |
+
}
|
| 9 |
+
const [{ marked }, { default: DOMPurify }] = await Promise.all([
|
| 10 |
+
import("https://cdn.jsdelivr.net/npm/marked@15.0.7/lib/marked.esm.js"),
|
| 11 |
+
import("https://cdn.jsdelivr.net/npm/dompurify@3.2.4/dist/purify.es.min.js"),
|
| 12 |
+
]);
|
| 13 |
+
marked.setOptions({
|
| 14 |
+
breaks: true,
|
| 15 |
+
gfm: true,
|
| 16 |
+
});
|
| 17 |
+
markdownTools = { marked, DOMPurify };
|
| 18 |
+
return markdownTools;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
const CHAT_SESSIONS_KEY = "borderless-chat-sessions";
|
| 22 |
+
|
| 23 |
+
const REQUIRED_FIELDS = [
|
| 24 |
+
"current-country",
|
| 25 |
+
"residence-status",
|
| 26 |
+
"education",
|
| 27 |
+
"occupation",
|
| 28 |
+
"experience",
|
| 29 |
+
"budget",
|
| 30 |
+
"family",
|
| 31 |
+
"timeline",
|
| 32 |
+
"goals",
|
| 33 |
+
];
|
| 34 |
+
|
| 35 |
+
function unwrapResult(result) {
|
| 36 |
+
if (result && Array.isArray(result.data)) {
|
| 37 |
+
return result.data.length === 1 ? result.data[0] : result.data;
|
| 38 |
+
}
|
| 39 |
+
return result;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
const state = {
|
| 43 |
+
sessionId: crypto.randomUUID(),
|
| 44 |
+
history: [],
|
| 45 |
+
globeState: emptyGlobeState(),
|
| 46 |
+
choices: null,
|
| 47 |
+
busy: false,
|
| 48 |
+
view: "form",
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
const els = {
|
| 52 |
+
formView: document.getElementById("form-view"),
|
| 53 |
+
chatView: document.getElementById("chat-view"),
|
| 54 |
+
formStatus: document.getElementById("form-status"),
|
| 55 |
+
statusBanner: document.getElementById("status-banner"),
|
| 56 |
+
chatMessages: document.getElementById("chat-messages"),
|
| 57 |
+
chatInput: document.getElementById("chat-input"),
|
| 58 |
+
chatSend: document.getElementById("chat-send"),
|
| 59 |
+
intakeForm: document.getElementById("intake-form"),
|
| 60 |
+
createPrompt: document.getElementById("create-prompt"),
|
| 61 |
+
personaList: document.getElementById("persona-list"),
|
| 62 |
+
authLogin: document.getElementById("auth-login"),
|
| 63 |
+
authLogout: document.getElementById("auth-logout"),
|
| 64 |
+
newChat: document.getElementById("new-chat"),
|
| 65 |
+
historyOpen: document.getElementById("history-open"),
|
| 66 |
+
historyDialog: document.getElementById("history-dialog"),
|
| 67 |
+
historyClose: document.getElementById("history-close"),
|
| 68 |
+
historyList: document.getElementById("history-list"),
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
function emptyGlobeState() {
|
| 72 |
+
return { version: 0, markers: [], highlights: [], fly_to: null };
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
function authRedirectTarget() {
|
| 76 |
+
return encodeURIComponent(window.location.pathname + window.location.search);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
function updateAuthUI({ logged_in: loggedIn, username }) {
|
| 80 |
+
const target = authRedirectTarget();
|
| 81 |
+
if (loggedIn) {
|
| 82 |
+
els.authLogin.hidden = true;
|
| 83 |
+
els.authLogout.hidden = false;
|
| 84 |
+
els.authLogout.textContent = username ? `Log out (${username})` : "Log out";
|
| 85 |
+
els.authLogout.href = `/logout?_target_url=${target}`;
|
| 86 |
+
} else {
|
| 87 |
+
els.authLogin.hidden = false;
|
| 88 |
+
els.authLogout.hidden = true;
|
| 89 |
+
els.authLogin.href = `/login/huggingface?_target_url=${target}`;
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
async function loadAuthStatus() {
|
| 94 |
+
try {
|
| 95 |
+
const response = await fetch("/api/auth/status", { credentials: "include" });
|
| 96 |
+
if (!response.ok) {
|
| 97 |
+
updateAuthUI({ logged_in: false });
|
| 98 |
+
return;
|
| 99 |
+
}
|
| 100 |
+
updateAuthUI(await response.json());
|
| 101 |
+
} catch {
|
| 102 |
+
updateAuthUI({ logged_in: false });
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
function activeStatusBanner() {
|
| 107 |
+
return state.view === "form" ? els.formStatus : els.statusBanner;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
function setStatus(message) {
|
| 111 |
+
const banner = activeStatusBanner();
|
| 112 |
+
const inactive =
|
| 113 |
+
state.view === "form" ? els.statusBanner : els.formStatus;
|
| 114 |
+
|
| 115 |
+
if (!message) {
|
| 116 |
+
banner.classList.remove("visible");
|
| 117 |
+
banner.textContent = "";
|
| 118 |
+
inactive.classList.remove("visible");
|
| 119 |
+
inactive.textContent = "";
|
| 120 |
+
return;
|
| 121 |
+
}
|
| 122 |
+
banner.textContent = message;
|
| 123 |
+
banner.classList.add("visible");
|
| 124 |
+
inactive.classList.remove("visible");
|
| 125 |
+
inactive.textContent = "";
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
function showChatView() {
|
| 129 |
+
state.view = "chat";
|
| 130 |
+
els.formView.classList.remove("is-active");
|
| 131 |
+
els.chatView.classList.add("is-active");
|
| 132 |
+
setStatus("");
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
function showFormView() {
|
| 136 |
+
state.view = "form";
|
| 137 |
+
els.chatView.classList.remove("is-active");
|
| 138 |
+
els.formView.classList.add("is-active");
|
| 139 |
+
setStatus("");
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
function resetForm() {
|
| 143 |
+
for (const id of REQUIRED_FIELDS) {
|
| 144 |
+
const element = document.getElementById(id);
|
| 145 |
+
if (element.tagName === "TEXTAREA") {
|
| 146 |
+
element.value = "";
|
| 147 |
+
} else {
|
| 148 |
+
element.value = "";
|
| 149 |
+
}
|
| 150 |
+
setFieldInvalid(id, false);
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
function sessionTitle(history) {
|
| 155 |
+
const firstUserMessage = history.find(
|
| 156 |
+
(message) => message.role === "user" && message.content,
|
| 157 |
+
);
|
| 158 |
+
if (!firstUserMessage) {
|
| 159 |
+
return "Untitled chat";
|
| 160 |
+
}
|
| 161 |
+
const text = String(firstUserMessage.content).trim().replace(/\s+/g, " ");
|
| 162 |
+
return text.length > 72 ? `${text.slice(0, 69)}...` : text;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
function formatSessionDate(timestamp) {
|
| 166 |
+
return new Date(timestamp).toLocaleString(undefined, {
|
| 167 |
+
month: "short",
|
| 168 |
+
day: "numeric",
|
| 169 |
+
hour: "numeric",
|
| 170 |
+
minute: "2-digit",
|
| 171 |
+
});
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
function loadSessions() {
|
| 175 |
+
try {
|
| 176 |
+
const sessions = JSON.parse(localStorage.getItem(CHAT_SESSIONS_KEY) || "[]");
|
| 177 |
+
return Array.isArray(sessions) ? sessions : [];
|
| 178 |
+
} catch {
|
| 179 |
+
return [];
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
function saveSessions(sessions) {
|
| 184 |
+
localStorage.setItem(CHAT_SESSIONS_KEY, JSON.stringify(sessions));
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
function persistActiveSession() {
|
| 188 |
+
if (!state.history.length) {
|
| 189 |
+
return;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
const sessions = loadSessions();
|
| 193 |
+
const now = Date.now();
|
| 194 |
+
const payload = {
|
| 195 |
+
id: state.sessionId,
|
| 196 |
+
title: sessionTitle(state.history),
|
| 197 |
+
updatedAt: now,
|
| 198 |
+
history: state.history,
|
| 199 |
+
globeState: state.globeState,
|
| 200 |
+
};
|
| 201 |
+
|
| 202 |
+
const index = sessions.findIndex((session) => session.id === state.sessionId);
|
| 203 |
+
if (index >= 0) {
|
| 204 |
+
sessions[index] = { ...sessions[index], ...payload };
|
| 205 |
+
} else {
|
| 206 |
+
sessions.unshift({ ...payload, createdAt: now });
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
sessions.sort((left, right) => right.updatedAt - left.updatedAt);
|
| 210 |
+
saveSessions(sessions);
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
function openHistoryDialog() {
|
| 214 |
+
renderHistoryList();
|
| 215 |
+
els.historyDialog.hidden = false;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
function closeHistoryDialog() {
|
| 219 |
+
els.historyDialog.hidden = true;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
function renderHistoryList() {
|
| 223 |
+
const sessions = loadSessions();
|
| 224 |
+
els.historyList.innerHTML = "";
|
| 225 |
+
|
| 226 |
+
if (!sessions.length) {
|
| 227 |
+
const empty = document.createElement("p");
|
| 228 |
+
empty.className = "history-list-empty";
|
| 229 |
+
empty.textContent = "No saved chats yet.";
|
| 230 |
+
els.historyList.appendChild(empty);
|
| 231 |
+
return;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
for (const session of sessions) {
|
| 235 |
+
const button = document.createElement("button");
|
| 236 |
+
button.type = "button";
|
| 237 |
+
button.className = "history-item";
|
| 238 |
+
if (session.id === state.sessionId) {
|
| 239 |
+
button.classList.add("is-active");
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
const title = document.createElement("span");
|
| 243 |
+
title.className = "history-item-title";
|
| 244 |
+
title.textContent = session.title || "Untitled chat";
|
| 245 |
+
|
| 246 |
+
const meta = document.createElement("span");
|
| 247 |
+
meta.className = "history-item-meta";
|
| 248 |
+
meta.textContent = formatSessionDate(session.updatedAt || session.createdAt);
|
| 249 |
+
|
| 250 |
+
button.appendChild(title);
|
| 251 |
+
button.appendChild(meta);
|
| 252 |
+
button.addEventListener("click", () => loadSession(session.id));
|
| 253 |
+
els.historyList.appendChild(button);
|
| 254 |
+
}
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
function loadSession(sessionId) {
|
| 258 |
+
const session = loadSessions().find((entry) => entry.id === sessionId);
|
| 259 |
+
if (!session) {
|
| 260 |
+
return;
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
if (state.history.length && state.sessionId !== sessionId) {
|
| 264 |
+
persistActiveSession();
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
state.sessionId = session.id;
|
| 268 |
+
state.history = session.history || [];
|
| 269 |
+
state.globeState = session.globeState || emptyGlobeState();
|
| 270 |
+
els.chatInput.value = "";
|
| 271 |
+
renderMessages();
|
| 272 |
+
applyGlobeState(state.globeState);
|
| 273 |
+
showChatView();
|
| 274 |
+
closeHistoryDialog();
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
function startNewChat() {
|
| 278 |
+
if (state.history.length) {
|
| 279 |
+
persistActiveSession();
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
state.sessionId = crypto.randomUUID();
|
| 283 |
+
state.history = [];
|
| 284 |
+
state.globeState = emptyGlobeState();
|
| 285 |
+
resetForm();
|
| 286 |
+
els.chatInput.value = "";
|
| 287 |
+
renderMessages();
|
| 288 |
+
applyGlobeState(state.globeState);
|
| 289 |
+
showFormView();
|
| 290 |
+
closeHistoryDialog();
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
function setBusy(busy) {
|
| 294 |
+
state.busy = busy;
|
| 295 |
+
els.chatSend.disabled = busy;
|
| 296 |
+
els.createPrompt.disabled = busy;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
function fillSelect(select, options, { multiple = false, empty = true } = {}) {
|
| 300 |
+
select.innerHTML = "";
|
| 301 |
+
if (empty) {
|
| 302 |
+
const option = document.createElement("option");
|
| 303 |
+
option.value = "";
|
| 304 |
+
option.textContent = multiple ? "Select one or more" : "Select one";
|
| 305 |
+
select.appendChild(option);
|
| 306 |
+
}
|
| 307 |
+
for (const value of options) {
|
| 308 |
+
const option = document.createElement("option");
|
| 309 |
+
option.value = value;
|
| 310 |
+
option.textContent = value;
|
| 311 |
+
select.appendChild(option);
|
| 312 |
+
}
|
| 313 |
+
select.multiple = multiple;
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
function selectedValues(select) {
|
| 317 |
+
if (select.multiple) {
|
| 318 |
+
return Array.from(select.selectedOptions)
|
| 319 |
+
.map((option) => option.value)
|
| 320 |
+
.filter(Boolean);
|
| 321 |
+
}
|
| 322 |
+
const value = select.value;
|
| 323 |
+
return value || null;
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
function setSelectValue(id, value) {
|
| 327 |
+
const select = document.getElementById(id);
|
| 328 |
+
const text = String(value || "").trim();
|
| 329 |
+
if (!text) {
|
| 330 |
+
select.value = "";
|
| 331 |
+
return;
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
let option = Array.from(select.options).find((entry) => entry.value === text);
|
| 335 |
+
if (!option) {
|
| 336 |
+
option = document.createElement("option");
|
| 337 |
+
option.value = text;
|
| 338 |
+
option.textContent = text;
|
| 339 |
+
select.appendChild(option);
|
| 340 |
+
}
|
| 341 |
+
select.value = text;
|
| 342 |
+
setFieldInvalid(id, false);
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
function fillPersonaForm(persona) {
|
| 346 |
+
const profile = persona.profile || {};
|
| 347 |
+
setSelectValue("current-country", profile.current_country);
|
| 348 |
+
setSelectValue("residence-status", profile.residence_status);
|
| 349 |
+
setSelectValue("education", profile.education);
|
| 350 |
+
setSelectValue("occupation", profile.occupation);
|
| 351 |
+
setSelectValue("experience", profile.experience);
|
| 352 |
+
setSelectValue("budget", profile.budget);
|
| 353 |
+
setSelectValue("family", profile.family);
|
| 354 |
+
setSelectValue("timeline", profile.timeline);
|
| 355 |
+
document.getElementById("goals").value = String(profile.goals || "").trim();
|
| 356 |
+
setFieldInvalid("goals", false);
|
| 357 |
+
setStatus("");
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
function fieldValue(id) {
|
| 361 |
+
const element = document.getElementById(id);
|
| 362 |
+
if (element.tagName === "TEXTAREA") {
|
| 363 |
+
return element.value.trim();
|
| 364 |
+
}
|
| 365 |
+
return selectedValues(element);
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
function setFieldInvalid(id, invalid) {
|
| 369 |
+
const element = document.getElementById(id);
|
| 370 |
+
element.classList.toggle("invalid", invalid);
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
function validateForm() {
|
| 374 |
+
let valid = true;
|
| 375 |
+
for (const id of REQUIRED_FIELDS) {
|
| 376 |
+
const empty = !fieldValue(id);
|
| 377 |
+
setFieldInvalid(id, empty);
|
| 378 |
+
if (empty) {
|
| 379 |
+
valid = false;
|
| 380 |
+
}
|
| 381 |
+
}
|
| 382 |
+
if (!valid) {
|
| 383 |
+
setStatus("Please fill in all required fields before submitting.");
|
| 384 |
+
}
|
| 385 |
+
return valid;
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
function clearFieldValidation() {
|
| 389 |
+
for (const id of REQUIRED_FIELDS) {
|
| 390 |
+
setFieldInvalid(id, false);
|
| 391 |
+
}
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
function shouldRenderMarkdown(message, isTool) {
|
| 395 |
+
return message.role === "assistant" && !isTool;
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
async function renderMessageBody(body, message, isTool) {
|
| 399 |
+
const content = message.content || "";
|
| 400 |
+
body.className = "chat-message-body";
|
| 401 |
+
|
| 402 |
+
if (!shouldRenderMarkdown(message, isTool)) {
|
| 403 |
+
body.textContent = content;
|
| 404 |
+
return;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
body.classList.add("markdown-body");
|
| 408 |
+
try {
|
| 409 |
+
const { marked, DOMPurify } = await ensureMarkdownTools();
|
| 410 |
+
const html = marked.parse(content);
|
| 411 |
+
body.innerHTML = DOMPurify.sanitize(html, {
|
| 412 |
+
USE_PROFILES: { html: true },
|
| 413 |
+
});
|
| 414 |
+
for (const link of body.querySelectorAll("a")) {
|
| 415 |
+
link.target = "_blank";
|
| 416 |
+
link.rel = "noopener noreferrer";
|
| 417 |
+
}
|
| 418 |
+
} catch {
|
| 419 |
+
body.textContent = content;
|
| 420 |
+
}
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
async function renderMessages() {
|
| 424 |
+
els.chatMessages.innerHTML = "";
|
| 425 |
+
for (const message of state.history) {
|
| 426 |
+
const node = document.createElement("div");
|
| 427 |
+
const metadata = message.metadata || {};
|
| 428 |
+
const isTool = Boolean(metadata.title || metadata.status);
|
| 429 |
+
node.className = `chat-message ${message.role}${isTool ? " tool" : ""}`;
|
| 430 |
+
if (metadata.status === "pending") {
|
| 431 |
+
node.classList.add("pending");
|
| 432 |
+
}
|
| 433 |
+
if (isTool) {
|
| 434 |
+
const title = document.createElement("div");
|
| 435 |
+
title.className = "tool-title";
|
| 436 |
+
title.textContent = metadata.title || "Tool";
|
| 437 |
+
node.appendChild(title);
|
| 438 |
+
}
|
| 439 |
+
const body = document.createElement("div");
|
| 440 |
+
await renderMessageBody(body, message, isTool);
|
| 441 |
+
node.appendChild(body);
|
| 442 |
+
els.chatMessages.appendChild(node);
|
| 443 |
+
}
|
| 444 |
+
els.chatMessages.scrollTop = els.chatMessages.scrollHeight;
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
function applyGlobeState(globeState) {
|
| 448 |
+
state.globeState = globeState;
|
| 449 |
+
window.BorderlessGlobe?.applyState(globeState);
|
| 450 |
+
}
|
| 451 |
+
|
| 452 |
+
function formPayload() {
|
| 453 |
+
return {
|
| 454 |
+
current_country: selectedValues(document.getElementById("current-country")),
|
| 455 |
+
residence_status: selectedValues(document.getElementById("residence-status")),
|
| 456 |
+
education: selectedValues(document.getElementById("education")),
|
| 457 |
+
occupation: selectedValues(document.getElementById("occupation")),
|
| 458 |
+
experience: selectedValues(document.getElementById("experience")),
|
| 459 |
+
budget: selectedValues(document.getElementById("budget")),
|
| 460 |
+
family: selectedValues(document.getElementById("family")),
|
| 461 |
+
timeline: selectedValues(document.getElementById("timeline")),
|
| 462 |
+
goals: document.getElementById("goals").value.trim(),
|
| 463 |
+
};
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
async function loadChoices() {
|
| 467 |
+
let choices = null;
|
| 468 |
+
|
| 469 |
+
try {
|
| 470 |
+
const response = await fetch("/api/intake_choices", { credentials: "include" });
|
| 471 |
+
if (!response.ok) {
|
| 472 |
+
throw new Error(`HTTP ${response.status}`);
|
| 473 |
+
}
|
| 474 |
+
choices = await response.json();
|
| 475 |
+
} catch (restError) {
|
| 476 |
+
const result = await gradioPredict("/get_intake_choices", {});
|
| 477 |
+
choices = unwrapResult(result);
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
state.choices = choices;
|
| 481 |
+
|
| 482 |
+
fillSelect(document.getElementById("current-country"), choices.countries);
|
| 483 |
+
fillSelect(document.getElementById("residence-status"), choices.residence_status);
|
| 484 |
+
fillSelect(document.getElementById("education"), choices.education);
|
| 485 |
+
fillSelect(document.getElementById("occupation"), choices.occupation);
|
| 486 |
+
fillSelect(document.getElementById("experience"), choices.experience);
|
| 487 |
+
fillSelect(document.getElementById("budget"), choices.budget);
|
| 488 |
+
fillSelect(document.getElementById("family"), choices.family);
|
| 489 |
+
fillSelect(document.getElementById("timeline"), choices.timeline);
|
| 490 |
+
|
| 491 |
+
els.personaList.innerHTML = "";
|
| 492 |
+
for (const persona of choices.personas || []) {
|
| 493 |
+
const button = document.createElement("button");
|
| 494 |
+
button.type = "button";
|
| 495 |
+
button.textContent = persona.label;
|
| 496 |
+
button.addEventListener("click", () => fillPersonaForm(persona));
|
| 497 |
+
els.personaList.appendChild(button);
|
| 498 |
+
}
|
| 499 |
+
}
|
| 500 |
+
|
| 501 |
+
async function runChat(message) {
|
| 502 |
+
const result = await gradioPredict("/chat", {
|
| 503 |
+
message,
|
| 504 |
+
history: state.history,
|
| 505 |
+
globe_state: state.globeState,
|
| 506 |
+
});
|
| 507 |
+
const payload = unwrapResult(result);
|
| 508 |
+
state.history = payload.history || state.history;
|
| 509 |
+
await renderMessages();
|
| 510 |
+
if (payload.globe_state) {
|
| 511 |
+
applyGlobeState(payload.globe_state);
|
| 512 |
+
}
|
| 513 |
+
persistActiveSession();
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
async function sendChatMessage(message) {
|
| 517 |
+
setBusy(true);
|
| 518 |
+
setStatus("Researching pathways...");
|
| 519 |
+
try {
|
| 520 |
+
await runChat(message);
|
| 521 |
+
setStatus("");
|
| 522 |
+
} catch (error) {
|
| 523 |
+
setStatus(`Chat failed: ${error.message || error}`);
|
| 524 |
+
throw error;
|
| 525 |
+
} finally {
|
| 526 |
+
setBusy(false);
|
| 527 |
+
}
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
async function submitForm(event) {
|
| 531 |
+
event.preventDefault();
|
| 532 |
+
if (state.busy || !validateForm()) {
|
| 533 |
+
return;
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
showChatView();
|
| 537 |
+
setBusy(true);
|
| 538 |
+
setStatus("Building research prompt...");
|
| 539 |
+
try {
|
| 540 |
+
const result = await gradioPredict("/build_research_prompt", formPayload());
|
| 541 |
+
const message = unwrapResult(result) || "";
|
| 542 |
+
if (!message) {
|
| 543 |
+
setStatus("Could not build prompt.");
|
| 544 |
+
return;
|
| 545 |
+
}
|
| 546 |
+
clearFieldValidation();
|
| 547 |
+
setStatus("Researching pathways...");
|
| 548 |
+
await runChat(message);
|
| 549 |
+
setStatus("");
|
| 550 |
+
} catch (error) {
|
| 551 |
+
setStatus(`Submission failed: ${error.message || error}`);
|
| 552 |
+
} finally {
|
| 553 |
+
setBusy(false);
|
| 554 |
+
}
|
| 555 |
+
}
|
| 556 |
+
|
| 557 |
+
async function sendChat() {
|
| 558 |
+
const message = els.chatInput.value.trim();
|
| 559 |
+
if (!message || state.busy) {
|
| 560 |
+
return;
|
| 561 |
+
}
|
| 562 |
+
|
| 563 |
+
els.chatInput.value = "";
|
| 564 |
+
try {
|
| 565 |
+
await sendChatMessage(message);
|
| 566 |
+
} catch {
|
| 567 |
+
els.chatInput.value = message;
|
| 568 |
+
}
|
| 569 |
+
}
|
| 570 |
+
|
| 571 |
+
for (const id of REQUIRED_FIELDS) {
|
| 572 |
+
const element = document.getElementById(id);
|
| 573 |
+
element.addEventListener("input", () => setFieldInvalid(id, false));
|
| 574 |
+
element.addEventListener("change", () => setFieldInvalid(id, false));
|
| 575 |
+
}
|
| 576 |
+
|
| 577 |
+
els.intakeForm.addEventListener("submit", submitForm);
|
| 578 |
+
els.chatSend.addEventListener("click", sendChat);
|
| 579 |
+
els.newChat.addEventListener("click", startNewChat);
|
| 580 |
+
els.historyOpen.addEventListener("click", openHistoryDialog);
|
| 581 |
+
els.historyClose.addEventListener("click", closeHistoryDialog);
|
| 582 |
+
els.historyDialog
|
| 583 |
+
.querySelector("[data-history-close]")
|
| 584 |
+
.addEventListener("click", closeHistoryDialog);
|
| 585 |
+
document.addEventListener("keydown", (event) => {
|
| 586 |
+
if (event.key === "Escape" && !els.historyDialog.hidden) {
|
| 587 |
+
closeHistoryDialog();
|
| 588 |
+
}
|
| 589 |
+
});
|
| 590 |
+
els.chatInput.addEventListener("keydown", (event) => {
|
| 591 |
+
if (event.key === "Enter" && !event.shiftKey) {
|
| 592 |
+
event.preventDefault();
|
| 593 |
+
sendChat();
|
| 594 |
+
}
|
| 595 |
+
});
|
| 596 |
+
|
| 597 |
+
await loadAuthStatus();
|
| 598 |
+
|
| 599 |
+
try {
|
| 600 |
+
await loadChoices();
|
| 601 |
+
} catch (error) {
|
| 602 |
+
setStatus(`Could not load form options: ${error.message || error}`);
|
| 603 |
+
}
|
| 604 |
+
|
| 605 |
+
await renderMessages();
|
assets/gradio_api.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export async function gradioPredict(apiName, payload = {}) {
|
| 2 |
+
const route = apiName.replace(/^\//, "");
|
| 3 |
+
const start = await fetch(`/gradio_api/call/v2/${route}`, {
|
| 4 |
+
method: "POST",
|
| 5 |
+
headers: { "Content-Type": "application/json" },
|
| 6 |
+
body: JSON.stringify(payload),
|
| 7 |
+
credentials: "include",
|
| 8 |
+
});
|
| 9 |
+
if (!start.ok) {
|
| 10 |
+
throw new Error(`Gradio API failed to start (${start.status})`);
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
const { event_id: eventId } = await start.json();
|
| 14 |
+
if (!eventId) {
|
| 15 |
+
throw new Error("Gradio API did not return an event id");
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
const stream = await fetch(`/gradio_api/call/${route}/${eventId}`, {
|
| 19 |
+
credentials: "include",
|
| 20 |
+
});
|
| 21 |
+
if (!stream.ok) {
|
| 22 |
+
throw new Error(`Gradio API stream failed (${stream.status})`);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
const body = await stream.text();
|
| 26 |
+
let resultData = null;
|
| 27 |
+
let errorMessage = null;
|
| 28 |
+
|
| 29 |
+
for (const line of body.split("\n")) {
|
| 30 |
+
if (line.startsWith("event: error")) {
|
| 31 |
+
errorMessage = "Gradio API returned an error";
|
| 32 |
+
continue;
|
| 33 |
+
}
|
| 34 |
+
if (!line.startsWith("data: ")) {
|
| 35 |
+
continue;
|
| 36 |
+
}
|
| 37 |
+
try {
|
| 38 |
+
resultData = JSON.parse(line.slice(6));
|
| 39 |
+
} catch {
|
| 40 |
+
// Ignore malformed SSE chunks and keep the last valid payload.
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
if (errorMessage) {
|
| 45 |
+
throw new Error(errorMessage);
|
| 46 |
+
}
|
| 47 |
+
if (resultData === null) {
|
| 48 |
+
throw new Error("Gradio API returned no data");
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
return { data: resultData };
|
| 52 |
+
}
|