fix: chat submission and session handling + 37 Playwright tests
- Fix chip auto-submit: use htmx.trigger() instead of native submit to bypass browser validation on required field - Remove required from input (server validates already) - Fix session_id duplication: use hx-swap-oob to replace single input - Fix agent context: use background context with 30s timeout instead of HTTP request context (prevents premature cancellation) - Remove redundant close button from header (toggle button handles it) - Add 83-chat-mascot.test.mjs: 37 tests covering button, panel, help card, chips, typed questions, session, Spanish, positioning
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/juanatsap/cv-site/internal/cache"
|
||||
|
||||
@@ -132,11 +133,14 @@ func (h *Handler) HandleChat(w http.ResponseWriter, r *http.Request) {
|
||||
sessionID = created.Session.ID()
|
||||
}
|
||||
|
||||
// Run the agent
|
||||
// Run the agent with a dedicated context (not tied to HTTP request lifecycle)
|
||||
agentCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
userMsg := genai.NewContentFromText(message, genai.RoleUser)
|
||||
|
||||
var response strings.Builder
|
||||
for event, err := range h.runner.Run(ctx, "visitor", sessionID, userMsg, agent.RunConfig{}) {
|
||||
for event, err := range h.runner.Run(agentCtx, "visitor", sessionID, userMsg, agent.RunConfig{}) {
|
||||
if err != nil {
|
||||
log.Printf("Chat agent error: %v", err)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
@@ -168,8 +172,8 @@ func (h *Handler) HandleChat(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
_, _ = fmt.Fprintf(w, `<div class="chat-message chat-agent">%s</div>`, formatResponse(agentText))
|
||||
|
||||
// Hidden input to preserve session ID for next request
|
||||
_, _ = fmt.Fprintf(w, `<input type="hidden" name="session_id" value="%s" form="chat-form"/>`, sessionID)
|
||||
// Update session ID via OOB swap (replaces existing input, avoids duplicates)
|
||||
_, _ = fmt.Fprintf(w, `<input type="hidden" id="chat-session-id" name="session_id" value="%s" form="chat-form" hx-swap-oob="true"/>`, sessionID)
|
||||
}
|
||||
|
||||
// formatResponse converts basic markdown to HTML for the chat bubble.
|
||||
|
||||
Reference in New Issue
Block a user