refactor: Cleanup

This commit is contained in:
Aleksander Grygier 2026-02-03 14:48:45 +01:00
parent 16f333e4ec
commit 3120a9fc94
5 changed files with 36 additions and 11 deletions

View File

@ -0,0 +1,3 @@
export const ATTACHMENT_LABEL_FILE = 'File';
export const ATTACHMENT_LABEL_PDF_FILE = 'PDF File';
export const ATTACHMENT_LABEL_MCP_PROMPT = 'MCP Prompt';

View File

@ -0,0 +1,8 @@
export const CODE_BLOCK_SCROLL_CONTAINER_CLASS = 'code-block-scroll-container';
export const CODE_BLOCK_WRAPPER_CLASS = 'code-block-wrapper';
export const CODE_BLOCK_HEADER_CLASS = 'code-block-header';
export const CODE_BLOCK_ACTIONS_CLASS = 'code-block-actions';
export const CODE_LANGUAGE_CLASS = 'code-language';
export const COPY_CODE_BTN_CLASS = 'copy-code-btn';
export const PREVIEW_CODE_BTN_CLASS = 'preview-code-btn';
export const RELATIVE_CLASS = 'relative';

View File

@ -13,6 +13,16 @@
import type { Plugin } from 'unified'; import type { Plugin } from 'unified';
import type { Root, Element, ElementContent } from 'hast'; import type { Root, Element, ElementContent } from 'hast';
import { visit } from 'unist-util-visit'; import { visit } from 'unist-util-visit';
import {
CODE_BLOCK_SCROLL_CONTAINER_CLASS,
CODE_BLOCK_WRAPPER_CLASS,
CODE_BLOCK_HEADER_CLASS,
CODE_BLOCK_ACTIONS_CLASS,
CODE_LANGUAGE_CLASS,
COPY_CODE_BTN_CLASS,
PREVIEW_CODE_BTN_CLASS,
RELATIVE_CLASS
} from '$lib/constants/code-blocks';
declare global { declare global {
interface Window { interface Window {
@ -42,7 +52,7 @@ function createCopyButton(codeId: string): Element {
type: 'element', type: 'element',
tagName: 'button', tagName: 'button',
properties: { properties: {
className: ['copy-code-btn'], className: [COPY_CODE_BTN_CLASS],
'data-code-id': codeId, 'data-code-id': codeId,
title: 'Copy code', title: 'Copy code',
type: 'button' type: 'button'
@ -56,7 +66,7 @@ function createPreviewButton(codeId: string): Element {
type: 'element', type: 'element',
tagName: 'button', tagName: 'button',
properties: { properties: {
className: ['preview-code-btn'], className: [PREVIEW_CODE_BTN_CLASS],
'data-code-id': codeId, 'data-code-id': codeId,
title: 'Preview code', title: 'Preview code',
type: 'button' type: 'button'
@ -75,18 +85,18 @@ function createHeader(language: string, codeId: string): Element {
return { return {
type: 'element', type: 'element',
tagName: 'div', tagName: 'div',
properties: { className: ['code-block-header'] }, properties: { className: [CODE_BLOCK_HEADER_CLASS] },
children: [ children: [
{ {
type: 'element', type: 'element',
tagName: 'span', tagName: 'span',
properties: { className: ['code-language'] }, properties: { className: [CODE_LANGUAGE_CLASS] },
children: [{ type: 'text', value: language }] children: [{ type: 'text', value: language }]
}, },
{ {
type: 'element', type: 'element',
tagName: 'div', tagName: 'div',
properties: { className: ['code-block-actions'] }, properties: { className: [CODE_BLOCK_ACTIONS_CLASS] },
children: actions children: actions
} }
] ]
@ -97,7 +107,7 @@ function createScrollContainer(preElement: Element): Element {
return { return {
type: 'element', type: 'element',
tagName: 'div', tagName: 'div',
properties: { className: ['code-block-scroll-container'] }, properties: { className: [CODE_BLOCK_SCROLL_CONTAINER_CLASS] },
children: [preElement] children: [preElement]
}; };
} }
@ -106,7 +116,7 @@ function createWrapper(header: Element, preElement: Element): Element {
return { return {
type: 'element', type: 'element',
tagName: 'div', tagName: 'div',
properties: { className: ['code-block-wrapper', 'relative'] }, properties: { className: [CODE_BLOCK_WRAPPER_CLASS, RELATIVE_CLASS] },
children: [header, createScrollContainer(preElement)] children: [header, createScrollContainer(preElement)]
}; };
} }

View File

@ -1,5 +1,9 @@
import { getJsonHeaders, formatAttachmentText, isAbortError } from '$lib/utils'; import { getJsonHeaders, formatAttachmentText, isAbortError } from '$lib/utils';
import { AGENTIC_REGEX } from '$lib/constants/agentic'; import { AGENTIC_REGEX } from '$lib/constants/agentic';
import {
ATTACHMENT_LABEL_PDF_FILE,
ATTACHMENT_LABEL_MCP_PROMPT
} from '$lib/constants/attachment-labels';
import { AttachmentType, ContentPartType, MessageRole, ReasoningFormat } from '$lib/enums'; import { AttachmentType, ContentPartType, MessageRole, ReasoningFormat } from '$lib/enums';
import type { ApiChatMessageContentPart, ApiChatCompletionToolCall } from '$lib/types/api'; import type { ApiChatMessageContentPart, ApiChatCompletionToolCall } from '$lib/types/api';
import type { DatabaseMessageExtraMcpPrompt } from '$lib/types'; import type { DatabaseMessageExtraMcpPrompt } from '$lib/types';
@ -764,7 +768,7 @@ export class ChatService {
} else { } else {
contentParts.push({ contentParts.push({
type: ContentPartType.TEXT, type: ContentPartType.TEXT,
text: formatAttachmentText('PDF File', pdfFile.name, pdfFile.content) text: formatAttachmentText(ATTACHMENT_LABEL_PDF_FILE, pdfFile.name, pdfFile.content)
}); });
} }
} }
@ -778,7 +782,7 @@ export class ChatService {
contentParts.push({ contentParts.push({
type: ContentPartType.TEXT, type: ContentPartType.TEXT,
text: formatAttachmentText( text: formatAttachmentText(
'MCP Prompt', ATTACHMENT_LABEL_MCP_PROMPT,
mcpPrompt.name, mcpPrompt.name,
mcpPrompt.content, mcpPrompt.content,
mcpPrompt.serverName mcpPrompt.serverName

View File

@ -432,7 +432,7 @@ class ChatStore {
return await DatabaseService.createMessageBranch( return await DatabaseService.createMessageBranch(
{ {
convId: activeConv.id, convId: activeConv.id,
type: 'text', type: MessageType.TEXT,
role: MessageRole.ASSISTANT, role: MessageRole.ASSISTANT,
content: '', content: '',
timestamp: Date.now(), timestamp: Date.now(),
@ -1234,7 +1234,7 @@ class ChatStore {
const assistantMessage = await DatabaseService.createMessageBranch( const assistantMessage = await DatabaseService.createMessageBranch(
{ {
convId: activeConv.id, convId: activeConv.id,
type: 'text', type: MessageType.TEXT,
timestamp: Date.now(), timestamp: Date.now(),
role: MessageRole.ASSISTANT, role: MessageRole.ASSISTANT,
content: '', content: '',