chore: sync local workspace changes
This commit is contained in:
@@ -14,10 +14,117 @@ export type PreviewItem = {
|
||||
source: 'message' | 'context';
|
||||
};
|
||||
|
||||
const CHAT_RESOURCE_INTERNAL_SEGMENTS = new Set(['resource', 'uploads', 'source', 'src']);
|
||||
const CHAT_RESOURCE_HIDDEN_FILE_NAMES = new Set(['.env']);
|
||||
const CHAT_API_RESOURCE_MARKER = '/api/chat/resources/';
|
||||
const RESOURCE_STRIP_ALLOWED_KINDS = new Set<PreviewKind>([
|
||||
'image',
|
||||
'video',
|
||||
'markdown',
|
||||
'code',
|
||||
'diff',
|
||||
'document',
|
||||
'pdf',
|
||||
'file',
|
||||
]);
|
||||
|
||||
function normalizePreviewUrl(value: string) {
|
||||
return normalizeChatResourceUrl(value);
|
||||
}
|
||||
|
||||
function parsePreviewUrl(url: string) {
|
||||
try {
|
||||
return new URL(url, typeof window !== 'undefined' ? window.location.origin : 'https://local.invalid');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function extractInternalChatResourcePath(pathname: string) {
|
||||
const normalizedPathname = String(pathname ?? '').trim();
|
||||
|
||||
if (!normalizedPathname) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (normalizedPathname.includes('/.codex_chat/')) {
|
||||
const markerIndex = normalizedPathname.lastIndexOf('/.codex_chat/');
|
||||
return normalizedPathname.slice(markerIndex + 1);
|
||||
}
|
||||
|
||||
const apiMarkerIndex = normalizedPathname.lastIndexOf(CHAT_API_RESOURCE_MARKER);
|
||||
if (apiMarkerIndex >= 0) {
|
||||
return normalizedPathname.slice(apiMarkerIndex + CHAT_API_RESOURCE_MARKER.length).replace(/^\/+/, '');
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function hasVisibleFileExtension(fileName: string) {
|
||||
return /\.[a-z0-9]{1,16}$/i.test(fileName);
|
||||
}
|
||||
|
||||
function hasSupportedPreviewFileExtension(fileName: string) {
|
||||
return /\.(png|jpe?g|gif|webp|svg|bmp|ico|mp4|webm|mov|m4v|ogg|md|markdown|diff|patch|ts|tsx|js|jsx|json|css|scss|html|xml|java|kt|sql|sh|py|go|rs|c|cpp|h|hpp|yml|yaml|txt|log|csv|pdf)$/i.test(
|
||||
fileName,
|
||||
);
|
||||
}
|
||||
|
||||
function isMarkdownResourceFile(fileName: string) {
|
||||
return /\.(md|markdown)$/i.test(fileName);
|
||||
}
|
||||
|
||||
function shouldHideInternalChatResource(url: string) {
|
||||
const parsed = parsePreviewUrl(url);
|
||||
const pathname = parsed?.pathname ?? '';
|
||||
const internalResourcePath = extractInternalChatResourcePath(pathname);
|
||||
const normalizedInternalResourcePath = internalResourcePath.toLowerCase();
|
||||
|
||||
if (!normalizedInternalResourcePath.startsWith('.codex_chat/')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const segments = internalResourcePath.split('/').filter(Boolean);
|
||||
const lastSegment = segments.at(-1)?.trim() ?? '';
|
||||
const normalizedLastSegment = lastSegment.toLowerCase();
|
||||
const resourceSegmentIndex = segments.findIndex((segment) => segment === 'resource');
|
||||
const nextSegment = resourceSegmentIndex >= 0 ? segments[resourceSegmentIndex + 1]?.toLowerCase() ?? '' : '';
|
||||
|
||||
if (!lastSegment || pathname.endsWith('/')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (CHAT_RESOURCE_INTERNAL_SEGMENTS.has(normalizedLastSegment)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!hasVisibleFileExtension(lastSegment)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (CHAT_RESOURCE_INTERNAL_SEGMENTS.has(nextSegment) && !hasVisibleFileExtension(lastSegment)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (normalizedLastSegment.startsWith('.')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (CHAT_RESOURCE_HIDDEN_FILE_NAMES.has(normalizedLastSegment)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (resourceSegmentIndex >= 0 && nextSegment === 'src' && !isMarkdownResourceFile(lastSegment)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!hasSupportedPreviewFileExtension(lastSegment)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function isPreviewRouteUrl(url: string) {
|
||||
if (typeof window === 'undefined') {
|
||||
return false;
|
||||
@@ -118,15 +225,23 @@ export function extractPreviewItems(messages: ChatMessage[]) {
|
||||
)
|
||||
.map((part) => part.url);
|
||||
const matches = [
|
||||
...extractAutoDetectedPreviewUrls(message.text),
|
||||
...extractHiddenPreviewUrls(message.text),
|
||||
...structuredLinkUrls,
|
||||
...extractAutoDetectedPreviewUrls(message.text),
|
||||
];
|
||||
|
||||
matches.forEach((matchedUrl) => {
|
||||
const normalizedUrl = normalizePreviewUrl(matchedUrl);
|
||||
const kind = classifyPreviewKind(normalizedUrl);
|
||||
|
||||
if (shouldHideInternalChatResource(normalizedUrl)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!RESOURCE_STRIP_ALLOWED_KINDS.has(kind)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (seen.has(normalizedUrl)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user