Skip to content
Merged
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
23 changes: 23 additions & 0 deletions app/(chat)/api/mastra-proxy/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ export async function POST(request: Request) {
// Forward the request body to Mastra backend
const body = await request.json();

// Check if the request is to stop the chat
if (body.action === 'stopChat') {
// Call the Mastra API to stop the chat with threadId and resourceId
console.log('Stopping chat for thread:', body.threadId, 'and resource:', body.resourceId);
const stopResponse = await fetch(`${mastraServerUrl}/stop-chat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
threadId: body.threadId,
resourceId: body.resourceId,
}),
});

console.log('Stop response:', stopResponse);

return new Response(stopResponse.body, {
status: stopResponse.status,
headers: stopResponse.headers,
});
}

const response = await fetch(`${mastraServerUrl}/chat`, {
method: 'POST',
headers: {
Expand Down
27 changes: 26 additions & 1 deletion components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function Chat({
setMessages,
sendMessage,
status,
stop,
stop: originalStop,
regenerate,
resumeStream,
} = useChat<ChatMessage>({
Expand Down Expand Up @@ -110,6 +110,31 @@ export function Chat({
},
});

// Custom stop function that sends stopChat action for web automation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to verify we've reviewed the other artifacts that are available in the AI Chatbot template (like the table artifact). I recall seeing stop controls there as well albeit they were not using App Router to send POSTs to a Mastra backend. Little doffeeent but there is a possibility we can adhere to the same pattern.

const stop = async () => {
// Always call the original stop to abort the stream
originalStop();

// For web automation model, also send stopChat action to backend
if (initialChatModel === 'web-automation-model') {
try {
await fetch('/api/mastra-proxy', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
action: 'stopChat',
threadId: id,
resourceId: session.user.id,
}),
});
} catch (error) {
console.error('Error sending stopChat action:', error);
}
}
};

const searchParams = useSearchParams();
const query = searchParams.get('query');

Expand Down