// Delete thread after successful task completionasync function completeRefillRequest(threadId) { // Process refill through conversation await chat({ threadId, message: "I'd like to refill my medication" }); // ... more conversation ... // Once refill is confirmed, clean up await axios.delete( `https://app.mycura.org/api/cura/threads/${threadId}`, { headers: { 'X-API-Key': 'YOUR_API_KEY' } } ); console.log('Refill complete, thread deleted');}
Delete threads when users log out or close sessions to protect privacy:
Copy
auth.onLogout(async () => { if (currentThreadId) { await deleteThread(currentThreadId); }});
Handle deletion failures gracefully
Don’t block user actions if thread deletion fails:
Copy
try { await deleteThread(threadId);} catch (error) { // Log error but don't prevent logout console.error('Thread cleanup failed:', error);}// Continue with user action regardlessproceedWithLogout();
Use sendBeacon for browser close
For reliable cleanup on page unload, use navigator.sendBeacon():
Copy
window.addEventListener('beforeunload', () => { if (threadId) { // This is guaranteed to send even if page is closing navigator.sendBeacon( '/api/cleanup', JSON.stringify({ threadId }) ); }});
Track deletion in analytics
Monitor how users are ending conversations:
Copy
async function deleteThread(threadId) { await axios.delete( `https://app.mycura.org/api/cura/threads/${threadId}`, { headers: { 'X-API-Key': 'YOUR_API_KEY' } } ); // Track the deletion analytics.track('thread_deleted', { threadId, trigger: 'user_action', timestamp: new Date().toISOString() });}
Reminder: Threads automatically delete after 30 minutes of inactivity. You don’t need to manually delete every thread - this endpoint is for when you want to end a conversation early.