Spaces:
Paused
Paused
dushyantsingh0 commited on
Commit ·
90d0eab
1
Parent(s): 796f501
backend stabilization
Browse files
api/v1/nexus_stream.py
CHANGED
|
@@ -161,9 +161,20 @@ async def stream_nexus_master(
|
|
| 161 |
if cache_hit:
|
| 162 |
async def cached_event_generator():
|
| 163 |
yield f"data: {json.dumps({'type': 'status', 'message': 'Semantic Cache Hit! Bypassing LLM...'})}\n\n"
|
|
|
|
| 164 |
yield f"data: {json.dumps({'type': 'token', 'content': cache_hit['answer']})}\n\n"
|
|
|
|
| 165 |
yield f"data: {json.dumps({'type': 'done'})}\n\n"
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
if payload.ticket_id:
|
| 169 |
from models.ticket import Ticket
|
|
@@ -244,8 +255,13 @@ async def stream_nexus_master(
|
|
| 244 |
app = create_nexus_graph() if is_agent else create_customer_graph()
|
| 245 |
config = {"configurable": {"thread_id": str(thread_id)}}
|
| 246 |
|
| 247 |
-
# Return
|
| 248 |
return StreamingResponse(
|
| 249 |
nexus_event_generator_v2(app, initial_state, config, thread_id),
|
| 250 |
-
media_type="text/event-stream"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
)
|
|
|
|
| 161 |
if cache_hit:
|
| 162 |
async def cached_event_generator():
|
| 163 |
yield f"data: {json.dumps({'type': 'status', 'message': 'Semantic Cache Hit! Bypassing LLM...'})}\n\n"
|
| 164 |
+
await asyncio.sleep(0.01) # Micro-delay for frontend state normalization
|
| 165 |
yield f"data: {json.dumps({'type': 'token', 'content': cache_hit['answer']})}\n\n"
|
| 166 |
+
await asyncio.sleep(0.01)
|
| 167 |
yield f"data: {json.dumps({'type': 'done'})}\n\n"
|
| 168 |
+
|
| 169 |
+
return StreamingResponse(
|
| 170 |
+
cached_event_generator(),
|
| 171 |
+
media_type="text/event-stream",
|
| 172 |
+
headers={
|
| 173 |
+
"X-Accel-Buffering": "no",
|
| 174 |
+
"Cache-Control": "no-cache",
|
| 175 |
+
"Connection": "keep-alive"
|
| 176 |
+
}
|
| 177 |
+
)
|
| 178 |
|
| 179 |
if payload.ticket_id:
|
| 180 |
from models.ticket import Ticket
|
|
|
|
| 255 |
app = create_nexus_graph() if is_agent else create_customer_graph()
|
| 256 |
config = {"configurable": {"thread_id": str(thread_id)}}
|
| 257 |
|
| 258 |
+
# Return StreamingResponse with media_type text/event-stream for SSE
|
| 259 |
return StreamingResponse(
|
| 260 |
nexus_event_generator_v2(app, initial_state, config, thread_id),
|
| 261 |
+
media_type="text/event-stream",
|
| 262 |
+
headers={
|
| 263 |
+
"X-Accel-Buffering": "no",
|
| 264 |
+
"Cache-Control": "no-cache",
|
| 265 |
+
"Connection": "keep-alive"
|
| 266 |
+
}
|
| 267 |
)
|
frontend/packages/shared/src/hooks/useNexus.ts
CHANGED
|
@@ -148,6 +148,7 @@ export const useNexus = (initialThreadId?: string) => {
|
|
| 148 |
const decoder = new TextDecoder();
|
| 149 |
let accumulatedAnswer = "";
|
| 150 |
let currentThreadId = state.threadId;
|
|
|
|
| 151 |
|
| 152 |
if (!reader) throw new Error("Stream reader not available");
|
| 153 |
|
|
@@ -156,49 +157,58 @@ export const useNexus = (initialThreadId?: string) => {
|
|
| 156 |
if (done) break;
|
| 157 |
|
| 158 |
const chunk = decoder.decode(value, { stream: true });
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
for (const line of lines) {
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
| 196 |
}
|
|
|
|
|
|
|
|
|
|
| 197 |
}
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
-
//
|
| 202 |
setState(prev => ({ ...prev, loading: false, statusMessage: null }));
|
| 203 |
|
| 204 |
// Refresh sessions if it was a new thread
|
|
|
|
| 148 |
const decoder = new TextDecoder();
|
| 149 |
let accumulatedAnswer = "";
|
| 150 |
let currentThreadId = state.threadId;
|
| 151 |
+
let lineBuffer = ""; // NEW: Buffer for fragmented packets
|
| 152 |
|
| 153 |
if (!reader) throw new Error("Stream reader not available");
|
| 154 |
|
|
|
|
| 157 |
if (done) break;
|
| 158 |
|
| 159 |
const chunk = decoder.decode(value, { stream: true });
|
| 160 |
+
lineBuffer += chunk;
|
| 161 |
+
|
| 162 |
+
const lines = lineBuffer.split('\n');
|
| 163 |
+
// The last element of split('\n') is either empty (if line ended in \n)
|
| 164 |
+
// or a partial line. We keep it in the buffer.
|
| 165 |
+
lineBuffer = lines.pop() || "";
|
| 166 |
|
| 167 |
for (const line of lines) {
|
| 168 |
+
const trimmedLine = line.trim();
|
| 169 |
+
if (!trimmedLine || !trimmedLine.startsWith('data: ')) continue;
|
| 170 |
+
|
| 171 |
+
try {
|
| 172 |
+
const data = JSON.parse(trimmedLine.slice(6));
|
| 173 |
+
|
| 174 |
+
if (data.type === 'token') {
|
| 175 |
+
accumulatedAnswer += data.content;
|
| 176 |
+
setState(prev => {
|
| 177 |
+
const newMessages = [...prev.messages];
|
| 178 |
+
const lastMsg = newMessages[newMessages.length - 1];
|
| 179 |
+
|
| 180 |
+
if (lastMsg && lastMsg.role === 'assistant') {
|
| 181 |
+
// Standard update
|
| 182 |
+
lastMsg.content = accumulatedAnswer;
|
| 183 |
+
} else {
|
| 184 |
+
// First token initialization
|
| 185 |
+
newMessages.push({
|
| 186 |
+
role: 'assistant',
|
| 187 |
+
content: accumulatedAnswer,
|
| 188 |
+
senderName: 'Nexus',
|
| 189 |
+
timestamp: new Date().toISOString()
|
| 190 |
+
});
|
| 191 |
+
}
|
| 192 |
+
return { ...prev, messages: newMessages, statusMessage: null };
|
| 193 |
+
});
|
| 194 |
+
} else if (data.type === 'status') {
|
| 195 |
+
setState(prev => ({ ...prev, statusMessage: data.message }));
|
| 196 |
+
} else if (data.type === 'thread_id') {
|
| 197 |
+
currentThreadId = data.thread_id;
|
| 198 |
+
setState(prev => ({ ...prev, threadId: data.thread_id }));
|
| 199 |
+
} else if (data.type === 'error') {
|
| 200 |
+
throw new Error(data.message);
|
| 201 |
+
} else if (data.type === 'done') {
|
| 202 |
+
setState(prev => ({ ...prev, loading: false, statusMessage: null }));
|
| 203 |
}
|
| 204 |
+
} catch (e) {
|
| 205 |
+
console.warn("Nexus Stream Parser: Skipping malformed line fragment", line);
|
| 206 |
+
// If it's a real parsing error in a seemingly complete line, we skip it
|
| 207 |
}
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
+
// Cleanup: Final loading state check
|
| 212 |
setState(prev => ({ ...prev, loading: false, statusMessage: null }));
|
| 213 |
|
| 214 |
// Refresh sessions if it was a new thread
|