feat: add markdown composer backend foundation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
David KayaandCopilot committed 2026-03-25 19:19:58 +01:00
1 parent a8c6ba94f5
commit d07bf30e81
16 files changed
+539 -121

No files matched your search

+2 -1
View File
@@ -4,6 +4,7 @@ import {
type ApprovalPolicy,
validateApprovalPolicy,
} from '@shared/domain/approval';
import { buildMarkdownExcerpt } from '@shared/utils/markdownText';
export type OrchestrationMode =
| 'single'
@@ -1009,5 +1010,5 @@ export function buildSessionTitle(pattern: PatternDefinition, messages: ChatMess
return pattern.name;
}
return firstUserMessage.content.slice(0, 48).trim() || pattern.name;
return buildMarkdownExcerpt(firstUserMessage.content, 48) ?? pattern.name;
}
+12
View File
@@ -0,0 +1,12 @@
export function normalizeChatMessageLineEndings(value: string): string {
return value.replace(/\r\n?/g, '\n');
}
export function hasMeaningfulChatMessageContent(value: string): boolean {
return normalizeChatMessageLineEndings(value).trim().length > 0;
}
export function prepareChatMessageContent(value: string): string | undefined {
const normalized = normalizeChatMessageLineEndings(value);
return normalized.trim().length > 0 ? normalized : undefined;
}
+58
View File
@@ -0,0 +1,58 @@
import { normalizeChatMessageLineEndings } from '@shared/utils/chatMessage';
const fencedCodeBlockPattern = /```[^\n]*\n([\s\S]*?)```/g;
const inlineCodePattern = /`([^`]+)`/g;
const imagePattern = /!\[([^\]]*)]\([^)]+\)/g;
const linkPattern = /\[([^\]]+)]\([^)]+\)/g;
const autoLinkPattern = /<((?:https?:\/\/|mailto:)[^>]+)>/g;
const headingPattern = /^\s{0,3}#{1,6}\s+/gm;
const blockquotePattern = /^\s{0,3}>\s?/gm;
const listPattern = /^\s{0,3}(?:[-*+]|\d+\.)\s+/gm;
const checklistPattern = /^\s*\[[ xX]\]\s+/gm;
const setextHeadingPattern = /^[=-]{2,}\s*$/gm;
const thematicBreakPattern = /^[ \t]{0,3}(?:[-*_][ \t]*){3,}$/gm;
const tableSeparatorPattern = /^\s*\|?(?:\s*:?-{3,}:?\s*\|)+\s*:?-{3,}:?\s*\|?\s*$/gm;
const emphasisPattern = /(\*\*|__|~~|\*|_)/g;
const markdownEscapePattern = /\\([\\`*_{}\[\]()#+\-.!>])/g;
export function extractPlainTextFromMarkdown(markdown: string): string {
const normalized = normalizeChatMessageLineEndings(markdown);
const plain = normalized
.replace(fencedCodeBlockPattern, '$1')
.replace(inlineCodePattern, '$1')
.replace(imagePattern, '$1')
.replace(linkPattern, '$1')
.replace(autoLinkPattern, '$1')
.replace(headingPattern, '')
.replace(blockquotePattern, '')
.replace(listPattern, '')
.replace(checklistPattern, '')
.replace(setextHeadingPattern, ' ')
.replace(thematicBreakPattern, ' ')
.replace(tableSeparatorPattern, ' ')
.replace(/\|/g, ' ')
.replace(emphasisPattern, '')
.replace(markdownEscapePattern, '$1')
.replace(/\s+/g, ' ')
.trim();
return plain;
}
export function buildMarkdownExcerpt(markdown: string | undefined, maxLength = 80): string | undefined {
if (!markdown) {
return undefined;
}
const plainText = extractPlainTextFromMarkdown(markdown);
if (!plainText) {
return undefined;
}
if (plainText.length <= maxLength) {
return plainText;
}
return `${plainText.slice(0, maxLength).trimEnd()}…`;
}