package main import ( "encoding/json" "net/http" "time" ) func registerAutoReplyRoutes(router *http.ServeMux) { router.HandleFunc("/api/auto-reply/status", handleAutoReplyStatus) router.HandleFunc("/api/auto-reply/reload", handleAutoReplyReload) router.HandleFunc("/api/auto-reply/rebuild-knowledge", handleAutoReplyRebuildKnowledge) router.HandleFunc("/api/auto-reply/sync-materials", handleAutoReplySyncMaterials) router.HandleFunc("/api/auto-reply/refresh-contacts", handleAutoReplyRefreshContacts) router.HandleFunc("/api/auto-reply/identity-options", handleAutoReplyIdentityOptions) router.HandleFunc("/api/auto-reply/refresh-groups", handleAutoReplyRefreshGroups) router.HandleFunc("/api/auto-reply/group-options", handleAutoReplyGroupOptions) router.HandleFunc("/api/auto-reply/sync-internal-groups", handleAutoReplySyncInternalGroups) router.HandleFunc("/api/auto-reply/test-ai", handleAutoReplyTestAI) router.HandleFunc("/api/auto-reply/test-handoff", handleAutoReplyTestHandoff) } func handleAutoReplyStatus(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } status := getAutoReplyEngine().snapshotStatus() sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "ok", "data": status, }) } func handleAutoReplyReload(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } getAutoReplyEngine().reloadConfig() sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "reloaded", "data": getAutoReplyEngine().snapshotStatus(), }) } func handleAutoReplyRebuildKnowledge(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } start := time.Now() idx, err := getAutoReplyEngine().rebuildKnowledgeIndex() if err != nil { sendJSONResponse(w, http.StatusInternalServerError, map[string]interface{}{ "success": false, "message": err.Error(), }) return } sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "rebuilt", "data": map[string]interface{}{ "fileCount": idx.FileCount, "chunkCount": len(idx.Chunks), "failedFiles": idx.FailedFiles, "durationMs": time.Since(start).Milliseconds(), }, }) } func handleAutoReplySyncMaterials(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } result, err := getAutoReplyEngine().syncAutoReplyMaterials() if err != nil { sendJSONResponse(w, http.StatusInternalServerError, map[string]interface{}{ "success": false, "message": err.Error(), }) return } sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "materials synced", "data": result, }) } func handleAutoReplyRefreshContacts(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } getAutoReplyEngine().refreshIdentityContactsAsync("manual") sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "contact refresh started", "data": getAutoReplyEngine().snapshotStatus(), }) } func handleAutoReplyIdentityOptions(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "ok", "data": getAutoReplyEngine().identityOptionsSnapshot(), }) } func handleAutoReplyRefreshGroups(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } getAutoReplyEngine().refreshIdentityGroupsAsync("manual") sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "group refresh started", "data": getAutoReplyEngine().snapshotStatus(), }) } func handleAutoReplyGroupOptions(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "ok", "data": getAutoReplyEngine().identityGroupOptionsSnapshot(), }) } func handleAutoReplySyncInternalGroups(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } getAutoReplyEngine().syncConfiguredInternalGroupsAsync("manual_group_sync") sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "internal group member sync started", "data": getAutoReplyEngine().snapshotStatus(), }) } func handleAutoReplyTestAI(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } start := time.Now() result, err := getAutoReplyEngine().testAIConnection() if err != nil { sendJSONResponse(w, http.StatusInternalServerError, map[string]interface{}{ "success": false, "message": err.Error(), "data": map[string]interface{}{ "durationMs": time.Since(start).Milliseconds(), }, }) return } if result != nil && result.DurationMS <= 0 { result.DurationMS = time.Since(start).Milliseconds() } sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "ok", "data": result, }) } func handleAutoReplyTestHandoff(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } var ignored map[string]interface{} _ = json.NewDecoder(r.Body).Decode(&ignored) if err := getAutoReplyEngine().testHandoff(); err != nil { sendJSONResponse(w, http.StatusInternalServerError, map[string]interface{}{ "success": false, "message": err.Error(), }) return } sendJSONResponse(w, http.StatusOK, map[string]interface{}{ "success": true, "message": "handoff test sent", }) }