Skip to content

Commit

Permalink
fixed formatting for stream responses
Browse files Browse the repository at this point in the history
  • Loading branch information
hrithikkoduri committed Feb 12, 2025
1 parent 3577075 commit 1d6b12a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
42 changes: 37 additions & 5 deletions frontend/src/app/rover/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,44 @@ export default function RoverPage() {

const processSSEMessage = (message: string) => {
try {
// Remove 'data: ' prefix and parse the JSON
const jsonStr = message.replace(/^data: /, '').trim();
const data = JSON.parse(jsonStr);

if (data.type === 'keepalive') return;

// Clean content if it matches the pattern
const cleanContent = (content: any) => {
// If content is already an array, return it directly
if (Array.isArray(content)) {
return content;
}

if (typeof content === 'string') {
try {
// Try to parse as JSON
const parsed = JSON.parse(content);
if (Array.isArray(parsed)) {
return parsed;
}
// If it's a string with ["..."] pattern
if (content.startsWith('["') && content.endsWith('"]')) {
return content.slice(2, -2);
}
} catch {
// If parsing fails and it has the pattern
if (content.startsWith('["') && content.endsWith('"]')) {
return content.slice(2, -2);
}
}
}
return content;
};

const processedData = {
type: data.type,
content: cleanContent(data.content)
};

// Handle different message types based on agent
switch (data.type) {
case 'thought':
Expand All @@ -71,7 +103,7 @@ export default function RoverPage() {
case 'final_response':
case 'dom_update':
case 'interaction':
setMessages(prev => [...prev, { type: data.type, content: data.content }]);
setMessages(prev => [...prev, processedData]);
break;

// Research specific events
Expand All @@ -80,7 +112,7 @@ export default function RoverPage() {
case 'close_tab':
case 'cleanup':
if (isResearchMode) {
setMessages(prev => [...prev, { type: data.type, content: data.content }]);
setMessages(prev => [...prev, processedData]);
}
break;

Expand All @@ -90,12 +122,12 @@ export default function RoverPage() {
case 'subtopic_status':
case 'compile':
if (isResearchMode && isDeepResearch) {
setMessages(prev => [...prev, { type: data.type, content: data.content }]);
setMessages(prev => [...prev, processedData]);
}
break;

case 'error':
setMessages(prev => [...prev, { type: 'error', content: data.content }]);
setMessages(prev => [...prev, processedData]);
break;
}
} catch (e) {
Expand Down
24 changes: 18 additions & 6 deletions frontend/src/components/rover/ResponseDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,26 @@ function useTemporaryMessages(messages: Message[]) {
}

// Helper function to stringify message content
function formatMessageContent(content: any): string {
if (typeof content === 'string') return content;
if (typeof content === 'object') {
if (content.thought) return content.thought;
return JSON.stringify(content, null, 2);
const formatMessageContent = (content: any) => {
if (Array.isArray(content)) {
return content.join('\n');
}

if (typeof content === 'string') {
try {
// Try to parse as JSON in case it's a stringified array
const parsed = JSON.parse(content);
if (Array.isArray(parsed)) {
return parsed.join('\n');
}
} catch {
// If parsing fails, return as is
return content;
}
}

return String(content);
}
};

interface MarkdownComponentProps {
children?: ReactNode;
Expand Down

0 comments on commit 1d6b12a

Please sign in to comment.