| 23 | } |
| 24 | |
| 25 | export function toJSON(conv: Conversation, options: ExportOptions): string { |
| 26 | let messages = conv.messages; |
| 27 | |
| 28 | if (options.dateRange) { |
| 29 | const { start, end } = options.dateRange; |
| 30 | messages = messages.filter((m) => m.createdAt >= start && m.createdAt <= end); |
| 31 | } |
| 32 | |
| 33 | const output = { |
| 34 | id: conv.id, |
| 35 | title: conv.title, |
| 36 | model: conv.model, |
| 37 | createdAt: options.includeTimestamps ? conv.createdAt : undefined, |
| 38 | updatedAt: options.includeTimestamps ? conv.updatedAt : undefined, |
| 39 | messageCount: messages.length, |
| 40 | messages: messages.map((m) => filterMessage(m, options)), |
| 41 | exportedAt: new Date().toISOString(), |
| 42 | }; |
| 43 | |
| 44 | return JSON.stringify(output, null, 2); |
| 45 | } |