@cli4ai/gmail 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/threads.ts DELETED
@@ -1,164 +0,0 @@
1
- import { getGmail, output, formatDate, extractHeaders, extractBody, cleanBody, type OutputOptions, type MessagePayload } from './api';
2
-
3
- /** Thread message */
4
- export interface ThreadMessage {
5
- id: string;
6
- date: string | null;
7
- from: string;
8
- to: string;
9
- subject: string;
10
- body: string;
11
- }
12
-
13
- /** Thread data */
14
- export interface ThreadData {
15
- id: string;
16
- subject: string;
17
- messageCount: number;
18
- messages: ThreadMessage[];
19
- }
20
-
21
- /** Thread summary */
22
- export interface ThreadSummary {
23
- id: string;
24
- subject: string;
25
- from: string;
26
- lastFrom: string;
27
- date: string | null;
28
- messageCount: number;
29
- snippet: string;
30
- }
31
-
32
- /** Thread action result */
33
- export interface ThreadActionResult {
34
- ok: boolean;
35
- archived?: string;
36
- trashed?: string;
37
- }
38
-
39
- /** Thread options */
40
- export interface ThreadOptions extends OutputOptions {
41
- fullBody?: boolean;
42
- limit?: number;
43
- query?: string;
44
- }
45
-
46
- /**
47
- * Get full thread (conversation)
48
- */
49
- export async function get(threadId: string, options: ThreadOptions = {}): Promise<ThreadData> {
50
- const gmail = await getGmail();
51
-
52
- const res = await gmail.users.threads.get({
53
- userId: 'me',
54
- id: threadId,
55
- format: 'full'
56
- });
57
-
58
- const messages: ThreadMessage[] = (res.data.messages || []).map(msg => {
59
- const headers = extractHeaders(msg.payload?.headers as Array<{ name: string; value: string }>);
60
- return {
61
- id: msg.id!,
62
- date: formatDate(msg.internalDate),
63
- from: headers.from || '',
64
- to: headers.to || '',
65
- subject: headers.subject || '(no subject)',
66
- body: cleanBody(extractBody(msg.payload as MessagePayload), options.fullBody ? null : 2000)
67
- };
68
- });
69
-
70
- const thread: ThreadData = {
71
- id: res.data.id!,
72
- subject: messages[0]?.subject || '(no subject)',
73
- messageCount: messages.length,
74
- messages: messages
75
- };
76
-
77
- output(thread, options);
78
- return thread;
79
- }
80
-
81
- /**
82
- * List recent threads
83
- */
84
- export async function list(options: ThreadOptions = {}): Promise<ThreadSummary[]> {
85
- const gmail = await getGmail();
86
- const limit = options.limit || 20;
87
- const query = options.query || '';
88
-
89
- const res = await gmail.users.threads.list({
90
- userId: 'me',
91
- maxResults: limit,
92
- q: query
93
- });
94
-
95
- if (!res.data.threads || res.data.threads.length === 0) {
96
- output([], options);
97
- return [];
98
- }
99
-
100
- // Get summary of each thread
101
- const threads = await Promise.all(
102
- res.data.threads.map(async (thread) => {
103
- const threadData = await gmail.users.threads.get({
104
- userId: 'me',
105
- id: thread.id!,
106
- format: 'metadata',
107
- metadataHeaders: ['From', 'Subject', 'Date']
108
- });
109
-
110
- const firstMsg = threadData.data.messages?.[0];
111
- const lastMsg = threadData.data.messages?.[threadData.data.messages.length - 1];
112
- const firstHeaders = extractHeaders(firstMsg?.payload?.headers as Array<{ name: string; value: string }>);
113
- const lastHeaders = extractHeaders(lastMsg?.payload?.headers as Array<{ name: string; value: string }>);
114
-
115
- return {
116
- id: thread.id!,
117
- subject: firstHeaders.subject || '(no subject)',
118
- from: firstHeaders.from || '',
119
- lastFrom: lastHeaders.from || '',
120
- date: formatDate(lastMsg?.internalDate),
121
- messageCount: threadData.data.messages?.length || 0,
122
- snippet: thread.snippet || ''
123
- };
124
- })
125
- );
126
-
127
- output(threads, options);
128
- return threads;
129
- }
130
-
131
- /**
132
- * Archive entire thread
133
- */
134
- export async function archive(threadId: string, options: OutputOptions = {}): Promise<ThreadActionResult> {
135
- const gmail = await getGmail();
136
-
137
- await gmail.users.threads.modify({
138
- userId: 'me',
139
- id: threadId,
140
- requestBody: {
141
- removeLabelIds: ['INBOX']
142
- }
143
- });
144
-
145
- const result: ThreadActionResult = { ok: true, archived: threadId };
146
- output(result, options);
147
- return result;
148
- }
149
-
150
- /**
151
- * Trash entire thread
152
- */
153
- export async function trash(threadId: string, options: OutputOptions = {}): Promise<ThreadActionResult> {
154
- const gmail = await getGmail();
155
-
156
- await gmail.users.threads.trash({
157
- userId: 'me',
158
- id: threadId
159
- });
160
-
161
- const result: ThreadActionResult = { ok: true, trashed: threadId };
162
- output(result, options);
163
- return result;
164
- }