Get Message History

User Intent

"I want to retrieve the complete message history from a conversation"

Operation

  • SDK Method: graphlit.getConversation()

  • GraphQL: getConversation query

  • Entity Type: Conversation

  • Common Use Cases: View chat history, export conversations, display message thread

TypeScript (Canonical)

import { Graphlit } from 'graphlit-client';
import { Types } from 'graphlit-client/dist/generated/graphql-types';

const graphlit = new Graphlit();

const conversationId = 'conversation-id-here';

// Get conversation with full message history
const conversation = await graphlit.getConversation(conversationId);

console.log(`\nConversation: ${conversation.conversation.name}`);
console.log(`Total messages: ${conversation.conversation.messages?.length || 0}\n`);

// Display message history
conversation.conversation.messages?.forEach((msg, index) => {
  console.log(`${index + 1}. [${msg.role}] ${msg.timestamp}`);
  console.log(msg.message);
  
  // Show citations if available
  if (msg.citations && msg.citations.length > 0) {
    console.log(`\nCitations:`);
    msg.citations.forEach((citation, citIndex) => {
      console.log(`  [${citIndex + 1}] ${citation.content?.name}`);
    });
  }
  
  console.log('---\n');
});

Parameters

  • id (string): Conversation ID

Response

{
  conversation: {
    id: string;
    name: string;
    messages: Message[];
  }
}

interface Message {
  role: MessageRole;  // USER, ASSISTANT, SYSTEM
  message: string;
  timestamp: Date;
  citations?: Citation[];
}

Developer Hints

Filter by Role

const conversation = await graphlit.getConversation(conversationId);

// Get only user messages
const userMessages = conversation.conversation.messages?.filter(
  m => m.role === MessageRole.User
);

// Get only assistant responses
const assistantMessages = conversation.conversation.messages?.filter(
  m => m.role === MessageRole.Assistant
);

console.log(`User messages: ${userMessages?.length || 0}`);
console.log(`Assistant responses: ${assistantMessages?.length || 0}`);

Export Conversation

async function exportConversation(conversationId: string) {
  const conv = await graphlit.getConversation(conversationId);
  
  const exported = {
    name: conv.conversation.name,
    created: conv.conversation.creationDate,
    messageCount: conv.conversation.messages?.length || 0,
    messages: conv.conversation.messages?.map(msg => ({
      role: msg.role,
      timestamp: msg.timestamp,
      message: msg.message,
      citations: msg.citations?.map(c => c.content?.name)
    }))
  };
  
  return JSON.stringify(exported, null, 2);
}

const json = await exportConversation(conversationId);
console.log(json);

Last updated

Was this helpful?