Chat Integration

Processing Chat Conversations

import { EpistemicMeClient, DialecticType } from '@epistemicme/sdk';

interface ChatMessage {
  role: 'assistant' | 'user';
  content: string;
}

async function processChatConversation(
  client: EpistemicMeClient,
  userId: string,
  conversation: ChatMessage[]
) {
  // Create initial dialectic
  const dialecticResponse = await client.createDialectic({
    userId,
    dialecticType: DialecticType.DEFAULT
  });

  const dialecticId = dialecticResponse?.dialectic?.id;
  if (!dialecticId) {
    throw new Error('Failed to create dialectic');
  }

  // Process each message
  for (const message of conversation) {
    const answer = new UserAnswer({
      userAnswer: message.role === 'user' ? message.content : '',
      createdAtMillisUtc: BigInt(Date.now())
    });

    await client.updateDialectic({
      userId,
      dialecticId,
      answer,
      ...(message.role === 'assistant' && { proposedQuestion: message.content })
    });
  }
}

Example Usage

const conversation: ChatMessage[] = [
  { role: 'assistant', content: 'How would you describe your sleep habits?' },
  { role: 'user', content: 'I usually get about 8 hours of sleep per night' },
  { role: 'assistant', content: 'What time do you typically go to bed?' },
  { role: 'user', content: 'Around 10:30 PM most nights' }
];

await processChatConversation(client, 'user123', conversation);