Skip to content

Commit f0af1ef

Browse files
committed
feat: Improves chat service architecture, starts implementing Settings and enhances PDF processing
Improves a dedicated ChatService for handling chat completions, providing better organization and testability. Moves settings-dependent logic for paste length and PDF handling into the ChatService and ChatScreen components, respectively, utilizing the settings store for dynamic configuration. Adds support for processing PDFs as images based on user settings, falling back to text extraction if image processing fails. Removes direct property passing and related code from ChatForm component. Updates UI components to improve layout and readability.
1 parent 3cde171 commit f0af1ef

File tree

7 files changed

+298
-41
lines changed

7 files changed

+298
-41
lines changed

tools/server/webui/src/app.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ declare global {
6666
SettingsConfigValue,
6767
SettingsFieldConfig,
6868
SettingsConfigType,
69+
SettingsChatServiceOptions,
6970
}
7071
}

tools/server/webui/src/lib/components/app/chat/ChatForm/ChatForm.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { ChatFormActionButtons, ChatFormFileInput, ChatFormHelperText, ChatFormTextarea } from '$lib/components/app';
44
import { inputClasses } from '$lib/constants/input-classes';
55
import { onMount } from 'svelte';
6+
import { config } from '$lib/stores/settings.svelte';
67
78
interface Props {
89
class?: string;
@@ -12,7 +13,6 @@
1213
onFileUpload?: (files: File[]) => void;
1314
onSend?: (message: string, files?: ChatUploadedFile[]) => Promise<boolean>;
1415
onStop?: () => void;
15-
pasteLongTextToFileLength?: number;
1616
showHelperText?: boolean;
1717
uploadedFiles?: ChatUploadedFile[];
1818
}
@@ -25,11 +25,14 @@
2525
onFileUpload,
2626
onSend,
2727
onStop,
28-
pasteLongTextToFileLength = 2500,
2928
showHelperText = true,
3029
uploadedFiles = $bindable([]),
3130
}: Props = $props();
3231
32+
// Get settings
33+
const currentConfig = $derived(config());
34+
const pasteLongTextToFileLength = $derived(Number(currentConfig.pasteLongTextToFileLen) || 2500);
35+
3336
let message = $state('');
3437
let fileInputRef: ChatFormFileInput | undefined;
3538
let previousIsLoading = $state(isLoading);

tools/server/webui/src/lib/components/app/chat/ChatScreen/ChatScreen.svelte

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import { Upload } from '@lucide/svelte';
1414
import {
1515
convertPDFToText,
16+
convertPDFToImage,
1617
isLikelyTextFile,
1718
isPdfMimeType,
1819
isSvgMimeType,
@@ -21,6 +22,7 @@
2122
svgBase64UrlToPngDataURL
2223
} from '$lib/utils';
2324
import { serverStore } from '$lib/stores/server.svelte';
25+
import { config } from '$lib/stores/settings.svelte';
2426
2527
let { showCenteredEmpty = false } = $props();
2628
let chatScrollContainer: HTMLDivElement | undefined = $state();
@@ -91,16 +93,41 @@
9193
}
9294
} else if (isPdfMimeType(file.type)) {
9395
try {
94-
// For now, always process PDF as text
95-
// todo: Add settings to allow PDF as images for vision models
96-
const content = await convertPDFToText(file.file);
97-
98-
extras.push({
99-
type: 'pdfFile',
100-
name: file.name,
101-
content: content,
102-
processedAsImages: false
103-
});
96+
const currentConfig = config();
97+
const shouldProcessAsImages = Boolean(currentConfig.pdfAsImage);
98+
99+
if (shouldProcessAsImages) {
100+
// Process PDF as images
101+
try {
102+
const images = await convertPDFToImage(file.file);
103+
extras.push({
104+
type: 'pdfFile',
105+
name: file.name,
106+
content: `PDF file with ${images.length} pages`,
107+
images: images,
108+
processedAsImages: true
109+
});
110+
} catch (imageError) {
111+
console.warn(`Failed to process PDF ${file.name} as images, falling back to text:`, imageError);
112+
// Fallback to text processing
113+
const content = await convertPDFToText(file.file);
114+
extras.push({
115+
type: 'pdfFile',
116+
name: file.name,
117+
content: content,
118+
processedAsImages: false
119+
});
120+
}
121+
} else {
122+
// Process PDF as text (default)
123+
const content = await convertPDFToText(file.file);
124+
extras.push({
125+
type: 'pdfFile',
126+
name: file.name,
127+
content: content,
128+
processedAsImages: false
129+
});
130+
}
104131
} catch (error) {
105132
console.error(`Failed to process PDF file ${file.name}:`, error);
106133
}

tools/server/webui/src/lib/components/app/chat/ChatSettings/ChatSettingsSection.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</script>
1212

1313
<div>
14-
<div class="border-border/30 flex items-center gap-2 border-b pb-2">
14+
<div class="border-border/30 flex items-center gap-2 border-b pb-6 mb-6">
1515
{@render icon({ class: 'h-5 w-5' })}
1616
<h3 class="text-lg font-semibold">{title}</h3>
1717
</div>

0 commit comments

Comments
 (0)