godcrm/utils/parseForwardedMessage.ts
GOD CRM Release f89e074dd1
Some checks failed
CI / Lint / Typecheck / Test / Build (push) Has been cancelled
CI / PostgreSQL Integration Tests (push) Has been cancelled
GOD CRM — public scrubbed snapshot
Governed substrate for autonomous agents: scoped identity (passports),
audited actions, MCP workspace. Infra IPs and secrets redacted for public release.
2026-08-10 04:01:45 +03:00

44 lines
1.3 KiB
TypeScript

/**
* Parse a forwarded message from the chat.
*
* Expected format:
* --- Переслано от <SenderName> (<timestamp>) ---
* <message content>
* --- конец пересланного сообщения ---
*
* The timestamp part (parenthesized) and the closing footer line are both optional.
*/
export interface ForwardedMessage {
senderName: string;
timestamp: string | null;
content: string;
}
const HEADER_REGEX = /^--- Переслано от (.+?)(?:\s*\(([^)]+)\))?\s*---$/;
const FOOTER = '--- конец пересланного сообщения ---';
export function parseForwardedMessage(text: string): ForwardedMessage | null {
if (!text) return null;
const lines = text.split('\n');
const headerMatch = lines[0].match(HEADER_REGEX);
if (!headerMatch) return null;
const senderName = headerMatch[1].trim();
const timestamp = headerMatch[2]?.trim() ?? null;
// Extract content between header and optional footer
const contentLines = lines.slice(1);
// Remove trailing footer line if present
const lastIdx = contentLines.length - 1;
if (lastIdx >= 0 && contentLines[lastIdx].trim() === FOOTER) {
contentLines.pop();
}
const content = contentLines.join('\n').trim();
return { senderName, timestamp, content };
}