Skip to content

Commit 82996b8

Browse files
committed
docs: Adds JSDoc comments and improves utils
Enhances code documentation by adding JSDoc comments to utility functions, improving clarity and maintainability. Refactors the utils/index.ts file to include a brief description of its purpose. Removes settings.ts which appears to be deprecated.
1 parent b54989e commit 82996b8

File tree

9 files changed

+108
-100
lines changed

9 files changed

+108
-100
lines changed

tools/server/webui/src/lib/types/settings.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { SETTING_CONFIG_DEFAULT } from "$lib/constants/settings-config";
2+
13
export type ConfigValue = string | number | boolean;
24

35
export interface FieldConfig {
@@ -6,3 +8,7 @@ export interface FieldConfig {
68
type: 'input' | 'textarea' | 'checkbox';
79
help?: string;
810
}
11+
12+
export type SettingsConfigType = typeof SETTING_CONFIG_DEFAULT & {
13+
[key: string]: string | number | boolean;
14+
};

tools/server/webui/src/lib/utils/autoresize-textarea.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Automatically resizes a textarea element to fit its content
3+
* @param textareaElement - The textarea element to resize
4+
*/
15
export default function autoResizeTextarea(textareaElement: HTMLTextAreaElement) {
26
if (textareaElement) {
37
textareaElement.style.height = 'auto';

tools/server/webui/src/lib/utils/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Utility functions for the webui application
3+
* Exports commonly used helper functions for file processing, UI interactions, and data manipulation
4+
*/
5+
16
import autoResizeTextarea from './autoresize-textarea';
27
import { copyCodeToClipboard, copyToClipboard } from './copy';
38
import { convertPDFToText, convertPDFToImage, isPdfMimeType } from './pdf-processing';

tools/server/webui/src/lib/utils/pdf-processing.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* PDF processing utilities using PDF.js
3+
* Handles PDF text extraction and image conversion in the browser
4+
*/
5+
16
import { browser } from '$app/environment';
27

38
// Types for PDF.js (imported conditionally)
@@ -12,7 +17,10 @@ type TextItem = {
1217
// PDF.js instance (loaded dynamically)
1318
let pdfjs: any = null;
1419

15-
// Initialize PDF.js only on the client side
20+
/**
21+
* Initialize PDF.js only on the client side
22+
* Sets up the PDF.js library and worker for processing
23+
*/
1624
async function initializePdfJs() {
1725
if (!browser || pdfjs) return;
1826

@@ -31,6 +39,11 @@ async function initializePdfJs() {
3139
}
3240
}
3341

42+
/**
43+
* Convert a File object to ArrayBuffer for PDF.js processing
44+
* @param file - The PDF file to convert
45+
* @returns Promise resolving to the file's ArrayBuffer
46+
*/
3447
async function getFileAsBuffer(file: File): Promise<ArrayBuffer> {
3548
return new Promise((resolve, reject) => {
3649
const reader = new FileReader();
@@ -49,6 +62,11 @@ async function getFileAsBuffer(file: File): Promise<ArrayBuffer> {
4962
}
5063

5164

65+
/**
66+
* Extract text content from a PDF file
67+
* @param file - The PDF file to process
68+
* @returns Promise resolving to the extracted text content
69+
*/
5270
export async function convertPDFToText(file: File): Promise<string> {
5371
if (!browser) {
5472
throw new Error('PDF processing is only available in the browser');
@@ -80,6 +98,12 @@ export async function convertPDFToText(file: File): Promise<string> {
8098
}
8199
}
82100

101+
/**
102+
* Convert PDF pages to PNG images as data URLs
103+
* @param file - The PDF file to convert
104+
* @param scale - Rendering scale factor (default: 1.5)
105+
* @returns Promise resolving to array of PNG data URLs
106+
*/
83107
export async function convertPDFToImage(file: File, scale: number = 1.5): Promise<string[]> {
84108
if (!browser) {
85109
throw new Error('PDF processing is only available in the browser');
@@ -124,10 +148,20 @@ export async function convertPDFToImage(file: File, scale: number = 1.5): Promis
124148
}
125149
}
126150

151+
/**
152+
* Check if a file is a PDF based on its MIME type
153+
* @param file - The file to check
154+
* @returns True if the file is a PDF
155+
*/
127156
export function isPdfFile(file: File): boolean {
128157
return file.type === 'application/pdf';
129158
}
130159

160+
/**
161+
* Check if a MIME type represents a PDF
162+
* @param mimeType - The MIME type to check
163+
* @returns True if the MIME type is application/pdf
164+
*/
131165
export function isPdfMimeType(mimeType: string): boolean {
132166
return mimeType === 'application/pdf';
133167
}

tools/server/webui/src/lib/utils/settings.ts

Lines changed: 0 additions & 99 deletions
This file was deleted.

tools/server/webui/src/lib/utils/svg-to-png.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Convert an SVG base64 data URL to a PNG data URL
3+
* @param base64UrlSvg - The SVG base64 data URL to convert
4+
* @param backgroundColor - Background color for the PNG (default: 'white')
5+
* @returns Promise resolving to PNG data URL
6+
*/
17
export function svgBase64UrlToPngDataURL(
28
base64UrlSvg: string,
39
backgroundColor: string = 'white'
@@ -46,10 +52,20 @@ export function svgBase64UrlToPngDataURL(
4652
});
4753
}
4854

55+
/**
56+
* Check if a file is an SVG based on its MIME type
57+
* @param file - The file to check
58+
* @returns True if the file is an SVG
59+
*/
4960
export function isSvgFile(file: File): boolean {
5061
return file.type === 'image/svg+xml';
5162
}
5263

64+
/**
65+
* Check if a MIME type represents an SVG
66+
* @param mimeType - The MIME type to check
67+
* @returns True if the MIME type is image/svg+xml
68+
*/
5369
export function isSvgMimeType(mimeType: string): boolean {
5470
return mimeType === 'image/svg+xml';
5571
}

tools/server/webui/src/lib/utils/text-files.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
/**
2+
* Text file processing utilities
3+
* Handles text file detection, reading, and validation
4+
*/
5+
16
import { TEXT_FILE_EXTENSIONS } from "$lib/constants/text-file-extensions";
27

8+
/**
9+
* Check if a filename indicates a text file based on its extension
10+
* @param filename - The filename to check
11+
* @returns True if the filename has a recognized text file extension
12+
*/
313
export function isTextFileByName(filename: string): boolean {
414
return TEXT_FILE_EXTENSIONS.some((ext) => filename.toLowerCase().endsWith(ext));
515
}
616

17+
/**
18+
* Read a file's content as text
19+
* @param file - The file to read
20+
* @returns Promise resolving to the file's text content
21+
*/
722
export async function readFileAsText(file: File): Promise<string> {
823
return new Promise((resolve, reject) => {
924
const reader = new FileReader();
@@ -19,6 +34,12 @@ export async function readFileAsText(file: File): Promise<string> {
1934
});
2035
}
2136

37+
/**
38+
* Heuristic check to determine if content is likely from a text file
39+
* Detects binary files by counting suspicious characters and null bytes
40+
* @param content - The file content to analyze
41+
* @returns True if the content appears to be text-based
42+
*/
2243
export function isLikelyTextFile(content: string): boolean {
2344
if (!content) return true;
2445

tools/server/webui/src/lib/utils/thinking.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Parses thinking content from a message that may contain <think> tags
33
* Returns an object with thinking content and cleaned message content
44
* Handles both complete <think>...</think> blocks and incomplete <think> blocks (streaming)
5+
* @param content - The message content to parse
6+
* @returns An object containing the extracted thinking content and the cleaned message content
57
*/
68
export function parseThinkingContent(content: string): {
79
thinking: string | null;
@@ -38,13 +40,17 @@ export function parseThinkingContent(content: string): {
3840

3941
/**
4042
* Checks if content contains an opening <think> tag (for streaming)
43+
* @param content - The message content to check
44+
* @returns True if the content contains an opening <think> tag
4145
*/
4246
export function hasThinkingStart(content: string): boolean {
4347
return content.includes('<think>');
4448
}
4549

4650
/**
4751
* Checks if content contains a closing </think> tag (for streaming)
52+
* @param content - The message content to check
53+
* @returns True if the content contains a closing </think> tag
4854
*/
4955
export function hasThinkingEnd(content: string): boolean {
5056
return content.includes('</think>');
@@ -53,6 +59,8 @@ export function hasThinkingEnd(content: string): boolean {
5359
/**
5460
* Extracts partial thinking content during streaming
5561
* Used when we have <think> but not yet </think>
62+
* @param content - The message content to extract partial thinking from
63+
* @returns An object containing the extracted partial thinking content and the remaining content
5664
*/
5765
export function extractPartialThinking(content: string): {
5866
thinking: string | null;

tools/server/webui/src/lib/utils/token-estimation.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export function estimateTokenCount(text: string): number {
2323

2424
/**
2525
* Estimate token count for a single message including its extras (attachments)
26+
* @param message - The message to estimate tokens for
27+
* @returns The estimated token count for the message
2628
*/
2729
export function estimateMessageTokens(message: DatabaseMessage): number {
2830
let totalTokens = 0;
@@ -63,6 +65,8 @@ export function estimateMessageTokens(message: DatabaseMessage): number {
6365

6466
/**
6567
* Estimate total token count for a conversation including all messages
68+
* @param messages - The messages to estimate tokens for
69+
* @returns The estimated token count for the conversation
6670
*/
6771
export function estimateConversationTokens(messages: DatabaseMessage[]): number {
6872
let totalTokens = 0;
@@ -82,6 +86,9 @@ export function estimateConversationTokens(messages: DatabaseMessage[]): number
8286

8387
/**
8488
* Estimate tokens for a new message with extras before sending
89+
* @param content - The content of the new message
90+
* @param extras - The extras (attachments) of the new message
91+
* @returns The estimated token count for the new message
8592
*/
8693
export function estimateNewMessageTokens(content: string, extras?: DatabaseMessageExtra[]): number {
8794
let totalTokens = estimateTokenCount(content);
@@ -115,6 +122,12 @@ export function estimateNewMessageTokens(content: string, extras?: DatabaseMessa
115122

116123
/**
117124
* Check if adding a new message would exceed the context length
125+
* @param existingMessages - The existing messages in the conversation
126+
* @param newMessageContent - The content of the new message
127+
* @param newMessageExtras - The extras (attachments) of the new message
128+
* @param maxContextLength - The maximum allowed context length
129+
* @param reserveTokens - Number of tokens to reserve for response generation (default: 512)
130+
* @returns An object containing the estimated tokens, whether it would exceed the context length, and the maximum allowed tokens
118131
*/
119132
export function wouldExceedContextLength(
120133
existingMessages: DatabaseMessage[],

0 commit comments

Comments
 (0)