forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryExport.ts
More file actions
100 lines (92 loc) · 2.66 KB
/
Copy pathmemoryExport.ts
File metadata and controls
100 lines (92 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import type { Memory } from '@/types/conversation';
/**
* Export memories to CSV format
*/
export function exportMemoriesToCSV(memories: Memory[]): string {
const headers = ['id', 'content', 'category', 'tags', 'created_at', 'updated_at'];
const rows = memories.map((m) => [
m.id,
`"${m.content.replace(/"/g, '""')}"`, // Escape quotes in CSV
m.category,
`"${(m.tags ?? []).map((tag) => tag.replace(/"/g, '""')).join(', ')}"`,
m.created_at || '',
m.updated_at || '',
]);
return [headers.join(','), ...rows.map((r) => r.join(','))].join('\n');
}
/**
* Export memories to JSON format
*/
export function exportMemoriesToJSON(memories: Memory[]): string {
return JSON.stringify(memories, null, 2);
}
/**
* Export memories to Markdown format
*/
export function exportMemoriesToMarkdown(memories: Memory[]): string {
return memories
.map((m) => {
const tags = (m.tags ?? []).length > 0 ? ` [${(m.tags ?? []).join(', ')}]` : '';
const date = m.created_at ? new Date(m.created_at).toLocaleDateString() : '';
return `## ${m.category}${tags}\n\n${m.content}\n\n_${date}_\n`;
})
.join('\n---\n\n');
}
/**
* Copy memories to clipboard as plain text
*/
export async function copyMemoriesToClipboard(memories: Memory[]): Promise<void> {
const text = memories
.map((m) => {
const tags = (m.tags ?? []).length > 0 ? ` [${(m.tags ?? []).join(', ')}]` : '';
return `[${m.category}]${tags}\n${m.content}`;
})
.join('\n\n---\n\n');
await navigator.clipboard.writeText(text);
}
/**
* Download a file with the given content
*/
function downloadFile(content: string, filename: string, mimeType: string): void {
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
/**
* Export and download memories in the specified format
*/
export function downloadMemories(
memories: Memory[],
format: 'csv' | 'json' | 'markdown',
): void {
const timestamp = new Date().toISOString().split('T')[0];
switch (format) {
case 'csv':
downloadFile(
exportMemoriesToCSV(memories),
`memories-${timestamp}.csv`,
'text/csv',
);
break;
case 'json':
downloadFile(
exportMemoriesToJSON(memories),
`memories-${timestamp}.json`,
'application/json',
);
break;
case 'markdown':
downloadFile(
exportMemoriesToMarkdown(memories),
`memories-${timestamp}.md`,
'text/markdown',
);
break;
}
}