# 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)

```typescript
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

```typescript
{
  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

```typescript
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

```typescript
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);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.graphlit.dev/api-guides/use-cases/conversations/conversation-message-history.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
