fix(ui): Fix SSE in chat
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -215,7 +216,7 @@ func (a *App) ImportAgent(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||
os.MkdirAll("./uploads", os.ModePerm)
|
||||
|
||||
// Safely save the file to prevent path traversal
|
||||
destination := fmt.Sprintf("./uploads/%s", file.Filename)
|
||||
destination := filepath.Join("./uploads", file.Filename)
|
||||
if err := c.SaveFile(file, destination); err != nil {
|
||||
// Handle error
|
||||
return err
|
||||
@@ -385,7 +386,7 @@ func (a *App) ChatAPI(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||
xlog.Error("Error marshaling error message", "error", err)
|
||||
} else {
|
||||
manager.Send(
|
||||
sse.NewMessage(string(errorData)).WithEvent("error"))
|
||||
sse.NewMessage(string(errorData)).WithEvent("json_error"))
|
||||
}
|
||||
} else {
|
||||
// Send agent response
|
||||
|
||||
11
webui/react-ui/src/hooks/useChat.js
vendored
11
webui/react-ui/src/hooks/useChat.js
vendored
@@ -109,6 +109,7 @@ export function useChat(agentName) {
|
||||
setSending(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// Add user message to the local state immediately for better UX
|
||||
const messageId = `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
|
||||
@@ -124,7 +125,6 @@ export function useChat(agentName) {
|
||||
// Track this message content to avoid duplication from SSE
|
||||
localMessageContents.current.add(content);
|
||||
|
||||
try {
|
||||
// Use the JSON-based API endpoint
|
||||
await chatApi.sendMessage(agentName, content);
|
||||
return true;
|
||||
@@ -133,8 +133,15 @@ export function useChat(agentName) {
|
||||
console.error('Error sending message:', err);
|
||||
setSending(false);
|
||||
return false;
|
||||
} finally {
|
||||
// Ensure sending state is reset after a timeout in case SSE doesn't update
|
||||
setTimeout(() => {
|
||||
if (sending) {
|
||||
setSending(false);
|
||||
}
|
||||
}, [agentName]);
|
||||
}, 5000); // 5 second timeout
|
||||
}
|
||||
}, [agentName, sending]);
|
||||
|
||||
// Clear chat history
|
||||
const clearChat = useCallback(() => {
|
||||
|
||||
6
webui/react-ui/src/hooks/useSSE.js
vendored
6
webui/react-ui/src/hooks/useSSE.js
vendored
@@ -63,8 +63,8 @@ export function useSSE(agentName) {
|
||||
}
|
||||
});
|
||||
|
||||
// Handle 'status' event
|
||||
eventSource.addEventListener('status', (event) => {
|
||||
// Handle 'json_status' event
|
||||
eventSource.addEventListener('json_status', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
const timestamp = data.timestamp || new Date().toISOString();
|
||||
@@ -81,7 +81,7 @@ export function useSSE(agentName) {
|
||||
});
|
||||
|
||||
// Handle 'error' event
|
||||
eventSource.addEventListener('error', (event) => {
|
||||
eventSource.addEventListener('json_error', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
const timestamp = data.timestamp || new Date().toISOString();
|
||||
|
||||
Reference in New Issue
Block a user