refactor: DRY

This commit is contained in:
Aleksander Grygier 2026-01-25 01:17:59 +01:00
parent e7ff091881
commit 9bcfdc3483
2 changed files with 30 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import { getJsonHeaders } from '$lib/utils';
import { formatAttachmentText } from '$lib/utils/attachment-display';
import { AGENTIC_REGEX } from '$lib/constants/agentic';
import { AttachmentType, MessageRole, ReasoningFormat } from '$lib/enums';
import type { ApiChatMessageContentPart } from '$lib/types/api';
@ -671,7 +672,7 @@ export class ChatService {
for (const textFile of textFiles) {
contentParts.push({
type: 'text',
text: `\n\n--- File: ${textFile.name} ---\n${textFile.content}`
text: formatAttachmentText('File', textFile.name, textFile.content)
});
}
@ -684,7 +685,7 @@ export class ChatService {
for (const legacyContextFile of legacyContextFiles) {
contentParts.push({
type: 'text',
text: `\n\n--- File: ${legacyContextFile.name} ---\n${legacyContextFile.content}`
text: formatAttachmentText('File', legacyContextFile.name, legacyContextFile.content)
});
}
@ -719,7 +720,7 @@ export class ChatService {
} else {
contentParts.push({
type: 'text',
text: `\n\n--- PDF File: ${pdfFile.name} ---\n${pdfFile.content}`
text: formatAttachmentText('PDF File', pdfFile.name, pdfFile.content)
});
}
}
@ -732,7 +733,12 @@ export class ChatService {
for (const mcpPrompt of mcpPrompts) {
contentParts.push({
type: 'text',
text: `\n\n--- MCP Prompt: ${mcpPrompt.name} (${mcpPrompt.serverName}) ---\n${mcpPrompt.content}`
text: formatAttachmentText(
'MCP Prompt',
mcpPrompt.name,
mcpPrompt.content,
mcpPrompt.serverName
)
});
}

View File

@ -1,6 +1,26 @@
import { AttachmentType, FileTypeCategory, SpecialFileType } from '$lib/enums';
import { getFileTypeCategory, getFileTypeCategoryByExtension, isImageFile } from '$lib/utils';
/**
* Formats attachment content for API requests with consistent header style.
* Used when converting message attachments to text content parts.
*
* @param label - Type label (e.g., 'File', 'PDF File', 'MCP Prompt')
* @param name - File or attachment name
* @param content - The actual content to include
* @param extra - Optional extra info to append to name (e.g., server name for MCP)
* @returns Formatted string with header and content
*/
export function formatAttachmentText(
label: string,
name: string,
content: string,
extra?: string
): string {
const header = extra ? `${name} (${extra})` : name;
return `\n\n--- ${label}: ${header} ---\n${content}`;
}
export interface AttachmentDisplayItemsOptions {
uploadedFiles?: ChatUploadedFile[];
attachments?: DatabaseMessageExtra[];