Before starting a multi-message flow, verify the thread is still active:
Copy
async function sendMultipleMessages(threadId, messages) { // Check thread status first const threadInfo = await getThreadInfo(threadId); if (!threadInfo) { throw new Error('Thread expired - please create a new one'); } // Proceed with messages for (const msg of messages) { await chat({ threadId, message: msg }); }}
Display expiration warnings to users
Show users when their thread is about to expire:
Copy
function showExpirationWarning(expiresAt) { const minutesLeft = Math.floor( (new Date(expiresAt) - Date.now()) / 1000 / 60 ); if (minutesLeft < 5) { alert(`Your conversation will expire in ${minutesLeft} minutes!`); }}
Use for analytics and monitoring
Track thread usage patterns:
Copy
async function logThreadMetrics(threadId) { const { thread } = await getThreadInfo(threadId); await analytics.track('thread_activity', { threadId: thread.threadId, messageCount: thread.messageCount, duration: new Date(thread.lastActivity) - new Date(thread.createdAt), hasPatient: !!thread.patientId });}