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