From 4f558ac842981a23587a11e3f707855dda4c3acd Mon Sep 17 00:00:00 2001 From: juanatsap Date: Wed, 8 Apr 2026 14:11:11 +0100 Subject: [PATCH] fix: replace Hyperscript with plain JS for chat interactions The Hyperscript trigger/call commands couldn't reliably trigger HTMX form submissions or call global JS functions. Moved all chat interactions to plain JavaScript: - toggleChatPanel(): open/close panel + icon swap - sendChatQuestion(q): set input + htmx.trigger(form, 'submit') - closeChatHelpAndAsk(q): close modal + open chat + send question - htmx:afterRequest listener clears input after submit Hyperscript kept only for site-wide patterns (closeOnBackdrop) that work reliably. Also: better error message for rate-limited API responses (429). --- internal/chat/handler.go | 7 +- .../partials/modals/chat-help-modal.html | 36 ++++---- templates/partials/widgets/chat-widget.html | 83 ++++++++++++------- 3 files changed, 76 insertions(+), 50 deletions(-) diff --git a/internal/chat/handler.go b/internal/chat/handler.go index 1dd296b..37bb57c 100644 --- a/internal/chat/handler.go +++ b/internal/chat/handler.go @@ -144,8 +144,11 @@ func (h *Handler) HandleChat(w http.ResponseWriter, r *http.Request) { if err != nil { log.Printf("Chat agent error: %v", err) w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.WriteHeader(http.StatusInternalServerError) - _, _ = fmt.Fprint(w, `
Something went wrong. Please try again.
`) + errMsg := "Something went wrong. Please try again in a moment." + if strings.Contains(err.Error(), "429") || strings.Contains(err.Error(), "RESOURCE_EXHAUSTED") { + errMsg = "The AI service is temporarily busy. Please try again in a few seconds." + } + _, _ = fmt.Fprintf(w, `
%s
`, errMsg) return } if event.IsFinalResponse() { diff --git a/templates/partials/modals/chat-help-modal.html b/templates/partials/modals/chat-help-modal.html index 52c2ea1..25b8805 100644 --- a/templates/partials/modals/chat-help-modal.html +++ b/templates/partials/modals/chat-help-modal.html @@ -31,10 +31,10 @@ {{if eq .Lang "es"}}Experiencia{{else}}Experience{{end}}
- - - - + + + +
@@ -45,11 +45,11 @@ {{if eq .Lang "es"}}Tecnologías{{else}}Technologies{{end}}
- - - - - + + + + +
@@ -60,9 +60,9 @@ {{if eq .Lang "es"}}Proyectos{{else}}Projects{{end}}
- - - + + +
@@ -73,9 +73,9 @@ {{if eq .Lang "es"}}Formación{{else}}Education{{end}}
- - - + + +
@@ -86,9 +86,9 @@ {{if eq .Lang "es"}}Habilidades{{else}}Skills{{end}}
- - - + + +
diff --git a/templates/partials/widgets/chat-widget.html b/templates/partials/widgets/chat-widget.html index 761f0c0..41d3984 100644 --- a/templates/partials/widgets/chat-widget.html +++ b/templates/partials/widgets/chat-widget.html @@ -6,14 +6,7 @@ class="chat-toggle-btn no-print has-tooltip tooltip-left" aria-label="{{if eq .Lang "es"}}Asistente del CV{{else}}CV Assistant{{end}}" data-tooltip="{{if eq .Lang "es"}}Asistente del CV{{else}}CV Assistant{{end}}" - _="on click toggle .chat-open on #chat-panel - then if #chat-panel matches .chat-open - then add .mascot-active to me - then set #chat-input.focus to true - then call #chat-input.focus() - else - remove .mascot-active from me - end"> + onclick="toggleChatPanel()"> @@ -46,27 +39,17 @@
{{if eq .Lang "es"}} - - - - - + + + + + {{else}} - - - - - + + + + + {{end}}
@@ -74,8 +57,7 @@ hx-post="/api/chat" hx-target="#chat-messages" hx-swap="beforeend scroll:#chat-messages:bottom" - hx-indicator="#chat-typing" - _="on htmx:afterRequest set #chat-input.value to ''"> + hx-indicator="#chat-typing"> + + + {{end}} {{end}}