Skip to content
rekon307Public

About

TypeScript CLI tool to extract and export conversations from Claude, ChatGPT, and Gemini

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Conversation Extractor

A robust TypeScript tool for extracting, parsing, filtering, and exporting conversation data from multiple formats.

Features

  • Multiple Input Formats: Parse conversations from JSON, WhatsApp exports, and plain text
  • Flexible Filtering: Filter conversations by participants, dates, keywords, and sender patterns
  • Multiple Export Formats: Export to JSON, CSV, plain text, or Markdown
  • Statistics: Generate detailed statistics about conversations
  • CLI Interface: Easy-to-use command-line interface
  • TypeScript Support: Full TypeScript definitions included
  • Comprehensive Testing: Well-tested with Jest

Installation

npm install
npm run build

Usage

Command Line Interface

Extract and Filter Conversations

# Extract conversation and show statistics
npm run cli extract examples/sample.json --format json --stats

# Filter by participants
npm run cli extract examples/whatsapp.txt --format whatsapp --participants "Alice,Bob" --output filtered.json

# Filter by date range
npm run cli extract examples/sample.json --format json --startDate "2024-01-01" --endDate "2024-12-31"

# Filter by keywords
npm run cli extract examples/sample.json --format json --keywords "meeting,urgent" --output meeting-messages.txt --exportFormat txt

# Export to different formats
npm run cli extract examples/sample.json --format json --output output.csv --exportFormat csv
npm run cli extract examples/sample.json --format json --output output.md --exportFormat markdown

Convert Between Formats

# Convert WhatsApp to JSON
npm run cli convert examples/whatsapp.txt converted.json --inputFormat whatsapp --outputFormat json

# Convert JSON to Markdown
npm run cli convert examples/sample.json output.md --inputFormat json --outputFormat markdown

# Convert to CSV
npm run cli convert examples/sample.json output.csv --inputFormat json --outputFormat csv

Programmatic Usage

import {
  ConversationExtractor,
  JSONParser,
  WhatsAppParser,
  JSONExporter,
  MarkdownExporter,
} from 'conversation-extractor';

// Parse conversations
const parser = new JSONParser();
const conversations = await parser.parseFile('conversation.json');

// Create extractor
const extractor = new ConversationExtractor(conversations);

// Filter conversations
const filtered = extractor.filterConversations({
  participants: ['Alice', 'Bob'],
  keywords: ['meeting'],
  startDate: new Date('2024-01-01'),
});

// Get statistics
const stats = extractor.getStatistics();
console.log(`Total messages: ${stats.totalMessages}`);
console.log(`Participants: ${stats.participants.join(', ')}`);

// Export to different formats
const jsonExporter = new JSONExporter({ format: 'json', prettify: true });
await jsonExporter.exportToFile(filtered, 'output.json');

const mdExporter = new MarkdownExporter({ format: 'markdown' });
await mdExporter.exportToFile(filtered, 'output.md');

API Reference

ConversationExtractor

Main class for managing and filtering conversations.

Methods

  • addConversation(conversation: Conversation): void - Add a conversation
  • addConversations(conversations: Conversation[]): void - Add multiple conversations
  • getConversations(): Conversation[] - Get all conversations
  • filterConversations(filter: ConversationFilter): Conversation[] - Filter conversations
  • extractMessages(filter: ConversationFilter): Message[] - Extract messages matching filter
  • getStatistics() - Get conversation statistics
  • mergeConversations(ids?: string[]): Conversation | null - Merge conversations
  • clear(): void - Clear all conversations

Parsers

JSONParser

Parse conversations from JSON format.

const parser = new JSONParser();
const conversations = await parser.parseFile('file.json');

WhatsAppParser

Parse conversations from WhatsApp export format.

const parser = new WhatsAppParser();
const conversations = await parser.parseFile('whatsapp.txt');

Supports formats:

  • [DD/MM/YYYY, HH:MM:SS] Sender: Message
  • DD/MM/YYYY, HH:MM - Sender: Message

TextParser

Parse conversations from generic text format.

const parser = new TextParser();
const conversations = await parser.parseFile('conversation.txt');

Supports formats:

  • [YYYY-MM-DD HH:MM:SS] Sender: Message
  • YYYY-MM-DD HH:MM:SS Sender: Message

Exporters

JSONExporter

Export conversations to JSON format.

const exporter = new JSONExporter({
  format: 'json',
  includeMetadata: true,
  prettify: true,
});
await exporter.exportToFile(conversations, 'output.json');

CSVExporter

Export conversations to CSV format.

const exporter = new CSVExporter({ format: 'csv' });
await exporter.exportToFile(conversations, 'output.csv');

TextExporter

Export conversations to plain text format.

const exporter = new TextExporter({ format: 'txt' });
await exporter.exportToFile(conversations, 'output.txt');

MarkdownExporter

Export conversations to Markdown format.

const exporter = new MarkdownExporter({ format: 'markdown' });
await exporter.exportToFile(conversations, 'output.md');

Data Structures

Conversation

interface Conversation {
  id?: string;
  title?: string;
  participants: string[];
  messages: Message[];
  startTime: Date;
  endTime: Date;
  metadata?: Record<string, unknown>;
}

Message

interface Message {
  id?: string;
  timestamp: Date;
  sender: string;
  content: string;
  metadata?: Record<string, unknown>;
}

ConversationFilter

interface ConversationFilter {
  participants?: string[];
  startDate?: Date;
  endDate?: Date;
  keywords?: string[];
  senderPattern?: RegExp;
}

Examples

See the examples/ directory for sample conversation files:

  • sample.json - JSON format example
  • whatsapp.txt - WhatsApp export example
  • text-conversation.txt - Plain text format example

Development

# Install dependencies
npm install

# Build the project
npm run build

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# Watch mode for tests
npm run test:watch

# Lint code
npm run lint

# Format code
npm run format

Testing

The project includes comprehensive tests using Jest. Run them with:

npm test

Coverage report:

npm run test:coverage

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

TypeScript CLI tool to extract and export conversations from Claude, ChatGPT, and Gemini

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages