Skip to content
Merged
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
24 changes: 21 additions & 3 deletions app/api/agent/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,30 @@ export async function POST(req: Request) {
stopWhen: stepCountIs(20),
});

return createAgentUIStreamResponse({
const response = await createAgentUIStreamResponse({
agent,
uiMessages: messages,
});
} finally {
// Don't await — let it clean up in background so response isn't delayed

// Clean up sandbox after the stream finishes (not before).
// The original `finally` block killed the sandbox immediately when
// createAgentUIStreamResponse returned, before any tool calls ran.
const body = response.body;
if (body) {
const transform = new TransformStream();
body.pipeTo(transform.writable).finally(() => {
sandbox.stop().catch(() => {});
});
return new Response(transform.readable, {
headers: response.headers,
status: response.status,
});
}

sandbox.stop().catch(() => {});
return response;
} catch (error) {
sandbox.stop().catch(() => {});
throw error;
}
}