|
|
|
@@ -1,5 +1,6 @@
|
|
|
|
|
import { execFileSync } from 'node:child_process';
|
|
|
|
|
import { existsSync } from 'node:fs';
|
|
|
|
|
import { execFileSync, spawn } from 'node:child_process';
|
|
|
|
|
import { existsSync, mkdirSync, createWriteStream } from 'node:fs';
|
|
|
|
|
import { join } from 'node:path';
|
|
|
|
|
|
|
|
|
|
type ToolResult = {
|
|
|
|
|
content: Array<{ type: string; text: string }>;
|
|
|
|
@@ -16,11 +17,16 @@ type Params = {
|
|
|
|
|
after?: string;
|
|
|
|
|
content_type?: string;
|
|
|
|
|
folder?: string;
|
|
|
|
|
email_id?: string;
|
|
|
|
|
attachment_idx?: number;
|
|
|
|
|
limit?: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const DEFAULT_LIMIT = 20;
|
|
|
|
|
const MAX_LIMIT = 100;
|
|
|
|
|
const DEFAULT_LIMIT = 0; // 0 = no limit
|
|
|
|
|
|
|
|
|
|
function limitClause(limit: number): string {
|
|
|
|
|
return limit > 0 ? ` LIMIT ${limit}` : '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDbPath(): string {
|
|
|
|
|
return process.env.OFFICER_EMAIL_DB ?? '/officer/emails.db';
|
|
|
|
@@ -58,16 +64,16 @@ function err(text: string): ToolResult {
|
|
|
|
|
return { content: [{ type: 'text', text }], isError: true };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatRows(rows: Record<string, unknown>[], limit: number): string {
|
|
|
|
|
function formatRows(rows: Record<string, unknown>[]): string {
|
|
|
|
|
if (rows.length === 0) return 'No results.';
|
|
|
|
|
|
|
|
|
|
const cols = Object.keys(rows[0]!);
|
|
|
|
|
const lines = rows.slice(0, limit).map((row, i) => {
|
|
|
|
|
const lines = rows.map((row, i) => {
|
|
|
|
|
const fields = cols.map((c) => `${c}: ${row[c] ?? ''}`).join(' | ');
|
|
|
|
|
return `${i + 1}. ${fields}`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const header = `${rows.length} result${rows.length !== 1 ? 's' : ''}${rows.length > limit ? ` (showing first ${limit})` : ''}:`;
|
|
|
|
|
const header = `${rows.length} result${rows.length !== 1 ? 's' : ''}:`;
|
|
|
|
|
return [header, '', ...lines].join('\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -117,13 +123,13 @@ function runQuery(sql: string, limit: number): string {
|
|
|
|
|
throw new Error('Only SELECT statements are allowed in query action.');
|
|
|
|
|
}
|
|
|
|
|
const rows = queryJson(sql);
|
|
|
|
|
return formatRows(rows, limit);
|
|
|
|
|
return formatRows(rows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function searchEmails(params: Params, limit: number): string {
|
|
|
|
|
const where = withActive(buildWhereClause(params, 'inbox'));
|
|
|
|
|
const rows = queryJson(`SELECT id, from_name, from_address, subject, date, snippet, attachment_count FROM emails ${where} ORDER BY date DESC LIMIT ${limit}`);
|
|
|
|
|
return formatRows(rows, limit);
|
|
|
|
|
const rows = queryJson(`SELECT id, from_name, from_address, subject, date, snippet, attachment_count FROM emails ${where} ORDER BY date DESC ${limitClause(limit)}`);
|
|
|
|
|
return formatRows(rows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getStats(params: Params): string {
|
|
|
|
@@ -174,7 +180,7 @@ function countEmails(params: Params): string {
|
|
|
|
|
|
|
|
|
|
function listDomains(params: Params, limit: number): string {
|
|
|
|
|
const where = withActive(buildWhereClause({ action: 'domains', folder: params.folder }, 'inbox'));
|
|
|
|
|
const rows = queryJson(`SELECT from_domain, COUNT(*) as count FROM emails ${where} GROUP BY from_domain ORDER BY count DESC LIMIT ${limit}`);
|
|
|
|
|
const rows = queryJson(`SELECT from_domain, COUNT(*) as count FROM emails ${where} GROUP BY from_domain ORDER BY count DESC ${limitClause(limit)}`);
|
|
|
|
|
if (rows.length === 0) return 'No emails in database.';
|
|
|
|
|
|
|
|
|
|
const lines = rows.map((d, i) => `${i + 1}. ${d.from_domain} — ${d.count} email${(d.count as number) !== 1 ? 's' : ''}`);
|
|
|
|
@@ -183,7 +189,7 @@ function listDomains(params: Params, limit: number): string {
|
|
|
|
|
|
|
|
|
|
function listSenders(params: Params, limit: number): string {
|
|
|
|
|
const where = withActive(buildWhereClause({ action: 'senders', folder: params.folder }, 'inbox'));
|
|
|
|
|
const rows = queryJson(`SELECT from_address, from_name, COUNT(*) as count FROM emails ${where} GROUP BY from_address ORDER BY count DESC LIMIT ${limit}`);
|
|
|
|
|
const rows = queryJson(`SELECT from_address, from_name, COUNT(*) as count FROM emails ${where} GROUP BY from_address ORDER BY count DESC ${limitClause(limit)}`);
|
|
|
|
|
if (rows.length === 0) return 'No emails in database.';
|
|
|
|
|
|
|
|
|
|
const lines = rows.map((s, i) => {
|
|
|
|
@@ -194,7 +200,7 @@ function listSenders(params: Params, limit: number): string {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function listAttachmentTypes(limit: number): string {
|
|
|
|
|
const rows = queryJson(`SELECT content_type, COUNT(*) as count FROM attachments a JOIN emails e ON a.email_id = e.id WHERE e.deleted = 0 GROUP BY content_type ORDER BY count DESC LIMIT ${limit}`);
|
|
|
|
|
const rows = queryJson(`SELECT content_type, COUNT(*) as count FROM attachments a JOIN emails e ON a.email_id = e.id WHERE e.deleted = 0 GROUP BY content_type ORDER BY count DESC ${limitClause(limit)}`);
|
|
|
|
|
if (rows.length === 0) return 'No attachments in database.';
|
|
|
|
|
|
|
|
|
|
const totalRows = queryJson('SELECT COUNT(*) as count FROM attachments a JOIN emails e ON a.email_id = e.id WHERE e.deleted = 0');
|
|
|
|
@@ -225,8 +231,73 @@ function searchAttachments(params: Params, limit: number): string {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const where = `WHERE ${conditions.join(' AND ')}`;
|
|
|
|
|
const rows = queryJson(`SELECT a.filename, a.size, a.content_type, e.id as email_id, e.from_address, e.subject, e.date FROM attachments a JOIN emails e ON a.email_id = e.id ${where} ORDER BY e.date DESC LIMIT ${limit}`);
|
|
|
|
|
return formatRows(rows, limit);
|
|
|
|
|
const rows = queryJson(`SELECT a.filename, a.size, a.content_type, e.id as email_id, e.from_address, e.subject, e.date FROM attachments a JOIN emails e ON a.email_id = e.id ${where} ORDER BY e.date DESC ${limitClause(limit)}`);
|
|
|
|
|
return formatRows(rows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function extractAttachment(params: Params): Promise<string> {
|
|
|
|
|
if (!params.email_id) throw new Error('email_id is required for extract-attachment action.');
|
|
|
|
|
if (params.attachment_idx === undefined) throw new Error('attachment_idx is required for extract-attachment action.');
|
|
|
|
|
|
|
|
|
|
// Metadata query (small)
|
|
|
|
|
const rows = queryJson(
|
|
|
|
|
`SELECT a.filename, a.content_type, e.date, e.from_address FROM attachments a JOIN emails e ON a.email_id = e.id WHERE a.email_id = ${sqlStr(params.email_id)} AND a.idx = ${params.attachment_idx}`,
|
|
|
|
|
);
|
|
|
|
|
if (rows.length === 0) throw new Error(`No attachment found for email_id=${params.email_id} idx=${params.attachment_idx}.`);
|
|
|
|
|
const row = rows[0]!;
|
|
|
|
|
|
|
|
|
|
const rawName = (row.filename as string) || `attachment_${params.attachment_idx}`;
|
|
|
|
|
// Decode MIME encoded-words (e.g. =?iso-8859-1?Q?PRE=C7OS?=)
|
|
|
|
|
const originalName = rawName.replace(/=\?([^?]+)\?(B|Q)\?([^?]+)\?=/gi, (_, _charset: string, encoding: string, encoded: string) => {
|
|
|
|
|
if (encoding.toUpperCase() === 'B') return Buffer.from(encoded, 'base64').toString('utf-8');
|
|
|
|
|
return encoded.replace(/=([0-9A-Fa-f]{2})/g, (__, hex: string) => String.fromCharCode(parseInt(hex, 16))).replace(/_/g, ' ');
|
|
|
|
|
});
|
|
|
|
|
const ext = originalName.includes('.') ? originalName.slice(originalName.lastIndexOf('.')) : '';
|
|
|
|
|
const baseName = originalName.includes('.') ? originalName.slice(0, originalName.lastIndexOf('.')) : originalName;
|
|
|
|
|
const timestamp = ((row.date as string) ?? '').slice(0, 10).replace(/-/g, '');
|
|
|
|
|
const sender = (row.from_address as string) ?? 'unknown';
|
|
|
|
|
const filename = `${timestamp}_${sender}_${params.email_id}_${params.attachment_idx}_${baseName}${ext}`;
|
|
|
|
|
const outDir = join(process.env.HOME ?? '/tmp', 'Downloads');
|
|
|
|
|
mkdirSync(outDir, { recursive: true });
|
|
|
|
|
const outPath = join(outDir, filename);
|
|
|
|
|
|
|
|
|
|
// Stream base64 content from sqlite3 → decode → write to file (constant memory)
|
|
|
|
|
const totalBytes = await new Promise<number>((resolve, reject) => {
|
|
|
|
|
const proc = spawn('sqlite3', [getDbPath()]);
|
|
|
|
|
const out = createWriteStream(outPath);
|
|
|
|
|
let remainder = '';
|
|
|
|
|
let bytes = 0;
|
|
|
|
|
|
|
|
|
|
proc.stdout.on('data', (chunk: Buffer) => {
|
|
|
|
|
const str = remainder + chunk.toString().replace(/[\s\r\n]/g, '');
|
|
|
|
|
const validLen = str.length - (str.length % 4);
|
|
|
|
|
if (validLen > 0) {
|
|
|
|
|
const decoded = Buffer.from(str.slice(0, validLen), 'base64');
|
|
|
|
|
out.write(decoded);
|
|
|
|
|
bytes += decoded.length;
|
|
|
|
|
}
|
|
|
|
|
remainder = str.slice(validLen);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
proc.stdout.on('end', () => {
|
|
|
|
|
if (remainder.length > 0) {
|
|
|
|
|
const decoded = Buffer.from(remainder, 'base64');
|
|
|
|
|
out.write(decoded);
|
|
|
|
|
bytes += decoded.length;
|
|
|
|
|
}
|
|
|
|
|
out.end(() => resolve(bytes));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
proc.stderr.on('data', (chunk: Buffer) => reject(new Error(chunk.toString())));
|
|
|
|
|
proc.on('error', reject);
|
|
|
|
|
|
|
|
|
|
proc.stdin.write(`SELECT content FROM attachments WHERE email_id = ${sqlStr(params.email_id)} AND idx = ${params.attachment_idx};\n`);
|
|
|
|
|
proc.stdin.end();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (totalBytes === 0) throw new Error('Attachment content is empty in the database.');
|
|
|
|
|
|
|
|
|
|
return `Extracted "${filename}" (${row.content_type}, ${totalBytes} bytes) to:\n${outPath}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deleteEmails(params: Params): string {
|
|
|
|
@@ -247,7 +318,7 @@ function deleteEmails(params: Params): string {
|
|
|
|
|
// ── Main ──
|
|
|
|
|
|
|
|
|
|
export async function execute(_toolCallId: string, params: Params): Promise<ToolResult> {
|
|
|
|
|
const limit = Math.min(params.limit ?? DEFAULT_LIMIT, MAX_LIMIT);
|
|
|
|
|
const limit = params.limit ?? DEFAULT_LIMIT;
|
|
|
|
|
|
|
|
|
|
const dbPath = getDbPath();
|
|
|
|
|
if (!existsSync(dbPath)) {
|
|
|
|
@@ -287,11 +358,14 @@ export async function execute(_toolCallId: string, params: Params): Promise<Tool
|
|
|
|
|
}
|
|
|
|
|
return ok(searchAttachments(params, limit));
|
|
|
|
|
|
|
|
|
|
case 'extract-attachment':
|
|
|
|
|
return ok(await extractAttachment(params));
|
|
|
|
|
|
|
|
|
|
case 'delete':
|
|
|
|
|
return ok(deleteEmails(params));
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return err(`Unknown action: "${params.action}". Available: query, search, stats, count, domains, senders, attachments, attachment-types, delete.`);
|
|
|
|
|
return err(`Unknown action: "${params.action}". Available: query, search, stats, count, domains, senders, attachments, attachment-types, extract-attachment, delete.`);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(`Email DB error: ${e instanceof Error ? e.message : String(e)}`);
|
|
|
|
|