Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apps/bubblelab-api/src/services/ai/milktea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,18 @@ export async function runMilkTea(
let agentOutput: MilkTeaAgentOutput;
try {
const responseText = result.data?.response || '';
agentOutput = MilkTeaAgentOutputSchema.parse(JSON.parse(responseText));
// Use parseJsonWithFallbacks to handle markdown code blocks (e.g., ```json ... ```)
const parseResult = parseJsonWithFallbacks(responseText);
if (!parseResult.success || !parseResult.parsed) {
throw new Error(parseResult.error || 'Failed to parse JSON response');
}
agentOutput = MilkTeaAgentOutputSchema.parse(parseResult.parsed);
} catch (error) {
console.error('[MilkTea] Failed to parse agent output:', error);
return {
type: 'reject',
message: 'Failed to parse agent response',
message:
'Failed to process your request. Please try rephrasing your question.',
success: false,
error: error instanceof Error ? error.message : 'Unknown parsing error',
};
Expand Down
17 changes: 11 additions & 6 deletions apps/bubblelab-api/src/services/ai/pearl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,13 @@ export async function runPearl(
const responseText = result.data?.response || '';
try {
console.log('[Pearl] Agent response:', responseText);
// Try to parse as object first, then as array (take first element)
let parsedResponse = JSON.parse(responseText);
// Use parseJsonWithFallbacks to handle markdown code blocks (e.g., ```json ... ```)
// that the AI might wrap around the JSON response
const parseResult = parseJsonWithFallbacks(responseText);
if (!parseResult.success || !parseResult.parsed) {
throw new Error(parseResult.error || 'Failed to parse JSON response');
}
let parsedResponse = parseResult.parsed;
if (Array.isArray(parsedResponse) && parsedResponse.length > 0) {
parsedResponse = parsedResponse[0];
}
Expand All @@ -607,8 +612,7 @@ export async function runPearl(
return {
type: 'reject',
message:
'Error parsing agent response, original response: ' +
responseText,
'Unable to understand your request. Please try rephrasing your question.',
success: false,
};
}
Expand Down Expand Up @@ -668,11 +672,12 @@ export async function runPearl(
continue;
}

// Don't include raw responseText in user-facing message as it may contain markdown formatting
// The detailed error is logged above and available in the error field
return {
type: 'reject',
message:
'Failed to parse agent response, original response: ' +
responseText,
'Failed to process your request. Please try rephrasing your question or try again.',
success: false,
error:
error instanceof Error ? error.message : 'Unknown parsing error',
Expand Down