refactor: Types

This commit is contained in:
Aleksander Grygier 2026-01-25 01:39:49 +01:00
parent ba39f8cc7b
commit b58b823b57
21 changed files with 280 additions and 207 deletions

View File

@ -27,7 +27,15 @@ import { mcpClient } from '$lib/clients';
import { ChatService } from '$lib/services';
import { config } from '$lib/stores/settings.svelte';
import { agenticStore } from '$lib/stores/agentic.svelte';
import type { AgenticMessage, AgenticToolCallList, AgenticConfig } from '$lib/types/agentic';
import type {
AgenticMessage,
AgenticToolCallList,
AgenticConfig,
AgenticFlowCallbacks,
AgenticFlowOptions,
AgenticFlowParams,
AgenticFlowResult
} from '$lib/types/agentic';
import type {
ApiChatCompletionToolCall,
ApiChatMessageData,
@ -44,8 +52,7 @@ import type { MCPToolCall } from '$lib/types';
import type {
DatabaseMessage,
DatabaseMessageExtra,
DatabaseMessageExtraImageFile,
McpServerOverride
DatabaseMessageExtraImageFile
} from '$lib/types/database';
import { AttachmentType, MessageRole } from '$lib/enums';
@ -88,46 +95,6 @@ function toAgenticMessages(messages: ApiChatMessageData[]): AgenticMessage[] {
});
}
export interface AgenticFlowCallbacks {
onChunk?: (chunk: string) => void;
onReasoningChunk?: (chunk: string) => void;
onToolCallChunk?: (serializedToolCalls: string) => void;
onAttachments?: (extras: DatabaseMessageExtra[]) => void;
onModel?: (model: string) => void;
onComplete?: (
content: string,
reasoningContent?: string,
timings?: ChatMessageTimings,
toolCalls?: string
) => void;
onError?: (error: Error) => void;
onTimings?: (timings?: ChatMessageTimings, promptProgress?: ChatMessagePromptProgress) => void;
}
export interface AgenticFlowOptions {
stream?: boolean;
model?: string;
temperature?: number;
max_tokens?: number;
[key: string]: unknown;
}
export interface AgenticFlowParams {
/** Conversation ID for per-conversation state tracking */
conversationId: string;
messages: (ApiChatMessageData | (DatabaseMessage & { extra?: DatabaseMessageExtra[] }))[];
options?: AgenticFlowOptions;
callbacks: AgenticFlowCallbacks;
signal?: AbortSignal;
/** Per-chat MCP server overrides */
perChatOverrides?: McpServerOverride[];
}
export interface AgenticFlowResult {
handled: boolean;
error?: Error;
}
interface AgenticStoreStateCallbacks {
setRunning: (conversationId: string, running: boolean) => void;
setCurrentTurn: (conversationId: string, turn: number) => void;

View File

@ -18,57 +18,16 @@ import { agenticStore } from '$lib/stores/agentic.svelte';
import { DEFAULT_CONTEXT } from '$lib/constants/default-context';
import { SYSTEM_MESSAGE_PLACEHOLDER } from '$lib/constants/ui';
import { REASONING_TAGS } from '$lib/constants/agentic';
import type { ChatMessageTimings, ChatMessagePromptProgress } from '$lib/types/chat';
import type {
ChatMessageTimings,
ChatMessagePromptProgress,
ChatStreamCallbacks,
ErrorDialogState
} from '$lib/types/chat';
import type { DatabaseMessage, DatabaseMessageExtra } from '$lib/types/database';
import type { ApiProcessingState } from '$lib/types/api';
import { MessageRole, MessageType } from '$lib/enums';
export interface ApiProcessingState {
status: 'idle' | 'preparing' | 'generating';
tokensDecoded: number;
tokensRemaining: number;
contextUsed: number;
contextTotal: number;
outputTokensUsed: number;
outputTokensMax: number;
hasNextToken: boolean;
tokensPerSecond: number;
temperature: number;
topP: number;
speculative: boolean;
progressPercent?: number;
promptProgress?: {
total: number;
cache: number;
processed: number;
time_ms: number;
};
promptTokens: number;
promptMs?: number;
cacheTokens: number;
}
export interface ErrorDialogState {
type: 'timeout' | 'server';
message: string;
contextInfo?: { n_prompt_tokens: number; n_ctx: number };
}
export interface ChatStreamCallbacks {
onChunk?: (chunk: string) => void;
onReasoningChunk?: (chunk: string) => void;
onToolCallChunk?: (chunk: string) => void;
onAttachments?: (extras: DatabaseMessageExtra[]) => void;
onModel?: (model: string) => void;
onTimings?: (timings?: ChatMessageTimings, promptProgress?: ChatMessagePromptProgress) => void;
onComplete?: (
content?: string,
reasoningContent?: string,
timings?: ChatMessageTimings,
toolCallContent?: string
) => void;
onError?: (error: Error) => void;
}
interface ChatStoreStateCallbacks {
setChatLoading: (convId: string, loading: boolean) => void;
setChatStreaming: (convId: string, response: string, messageId: string) => void;

View File

@ -21,16 +21,9 @@ export { MCPClient, mcpClient } from './mcp.client';
// Chat Client
export { ChatClient, chatClient } from './chat.client';
export type { ChatStreamCallbacks, ApiProcessingState, ErrorDialogState } from './chat.client';
// Agentic Client
export { AgenticClient, agenticClient } from './agentic.client';
export type {
AgenticFlowCallbacks,
AgenticFlowOptions,
AgenticFlowParams,
AgenticFlowResult
} from './agentic.client';
// Conversations Client
export { ConversationsClient, conversationsClient } from './conversations.client';

View File

@ -1,11 +1,4 @@
export interface BinaryDetectionOptions {
/** Number of characters to check from the beginning of the file */
prefixLength: number;
/** Maximum ratio of suspicious characters allowed (0.0 to 1.0) */
suspiciousCharThresholdRatio: number;
/** Maximum absolute number of null bytes allowed */
maxAbsoluteNullBytes: number;
}
import type { BinaryDetectionOptions } from '$lib/types';
export const DEFAULT_BINARY_DETECTION_OPTIONS: BinaryDetectionOptions = {
prefixLength: 1024 * 10, // Check the first 10KB of the string

View File

@ -1,20 +1,7 @@
import { activeProcessingState } from '$lib/stores/chat.svelte';
import { config } from '$lib/stores/settings.svelte';
import { STATS_UNITS } from '$lib/constants/processing-info';
export interface LiveProcessingStats {
tokensProcessed: number;
totalTokens: number;
timeMs: number;
tokensPerSecond: number;
etaSecs?: number;
}
export interface LiveGenerationStats {
tokensGenerated: number;
timeMs: number;
tokensPerSecond: number;
}
import type { ApiProcessingState, LiveProcessingStats, LiveGenerationStats } from '$lib/types';
export interface UseProcessingStateReturn {
readonly processingState: ApiProcessingState | null;

View File

@ -13,24 +13,13 @@
*/
import { normalizeFloatingPoint } from '$lib/utils';
export type ParameterSource = 'default' | 'custom';
export type ParameterValue = string | number | boolean;
export type ParameterRecord = Record<string, ParameterValue>;
export interface ParameterInfo {
value: string | number | boolean;
source: ParameterSource;
serverDefault?: string | number | boolean;
userOverride?: string | number | boolean;
}
export interface SyncableParameter {
key: string;
serverKey: string;
type: 'number' | 'string' | 'boolean';
canSync: boolean;
}
import type {
SyncableParameter,
ParameterRecord,
ParameterInfo,
ParameterValue,
ParameterSource
} from '$lib/types';
/**
* Mapping of webui setting keys to server parameter keys

View File

@ -26,21 +26,18 @@
*/
import { browser } from '$app/environment';
import type { AgenticFlowParams, AgenticFlowResult } from '$lib/clients';
import type { AgenticSession, AgenticConfig } from '$lib/types/agentic';
import type { SettingsConfigType } from '$lib/types/settings';
import type { McpServerOverride } from '$lib/types/database';
import type {
AgenticFlowParams,
AgenticFlowResult,
AgenticSession,
AgenticConfig,
SettingsConfigType,
McpServerOverride
} from '$lib/types';
import { DEFAULT_AGENTIC_CONFIG } from '$lib/constants/agentic';
import { mcpStore } from '$lib/stores/mcp.svelte';
import { agenticClient } from '$lib/clients/agentic.client';
export type {
AgenticFlowCallbacks,
AgenticFlowOptions,
AgenticFlowParams,
AgenticFlowResult
} from '$lib/clients';
/**
* Creates a fresh agentic session with default values.
*/

View File

@ -21,12 +21,15 @@
import { SvelteMap } from 'svelte/reactivity';
import { browser } from '$app/environment';
import { chatClient, type ApiProcessingState, type ErrorDialogState } from '$lib/clients';
import type { DatabaseMessage, DatabaseMessageExtra } from '$lib/types/database';
import { chatClient } from '$lib/clients';
import type {
ApiProcessingState,
ErrorDialogState,
DatabaseMessage,
DatabaseMessageExtra
} from '$lib/types';
import { MessageRole, MessageType } from '$lib/enums';
export type { ApiProcessingState, ErrorDialogState };
class ChatStore {
activeProcessingState = $state<ApiProcessingState | null>(null);
currentResponse = $state('');

View File

@ -1,5 +1,11 @@
import type { MessageRole } from '$lib/enums';
import type { ApiChatCompletionRequest, ApiChatMessageContentPart } from './api';
import type {
ApiChatCompletionRequest,
ApiChatMessageContentPart,
ApiChatMessageData
} from './api';
import type { ChatMessageTimings, ChatMessagePromptProgress } from './chat';
import type { DatabaseMessage, DatabaseMessageExtra, McpServerOverride } from './database';
/**
* Agentic orchestration configuration.
@ -61,3 +67,53 @@ export interface AgenticSession {
lastError: Error | null;
streamingToolCall: { name: string; arguments: string } | null;
}
/**
* Callbacks for agentic flow execution
*/
export interface AgenticFlowCallbacks {
onChunk?: (chunk: string) => void;
onReasoningChunk?: (chunk: string) => void;
onToolCallChunk?: (serializedToolCalls: string) => void;
onAttachments?: (extras: DatabaseMessageExtra[]) => void;
onModel?: (model: string) => void;
onComplete?: (
content: string,
reasoningContent?: string,
timings?: ChatMessageTimings,
toolCalls?: string
) => void;
onError?: (error: Error) => void;
onTimings?: (timings?: ChatMessageTimings, promptProgress?: ChatMessagePromptProgress) => void;
}
/**
* Options for agentic flow execution
*/
export interface AgenticFlowOptions {
stream?: boolean;
model?: string;
temperature?: number;
max_tokens?: number;
[key: string]: unknown;
}
/**
* Parameters for starting an agentic flow
*/
export interface AgenticFlowParams {
conversationId: string;
messages: (ApiChatMessageData | (DatabaseMessage & { extra?: DatabaseMessageExtra[] }))[];
options?: AgenticFlowOptions;
callbacks: AgenticFlowCallbacks;
signal?: AbortSignal;
perChatOverrides?: McpServerOverride[];
}
/**
* Result of an agentic flow execution
*/
export interface AgenticFlowResult {
handled: boolean;
error?: Error;
}

View File

@ -1,3 +1,5 @@
import type { DatabaseMessageExtra } from './database';
export interface ChatUploadedFile {
id: string;
name: string;
@ -93,3 +95,67 @@ export interface ChatMessageToolCallTiming {
duration_ms: number;
success: boolean;
}
/**
* Callbacks for streaming chat responses
*/
export interface ChatStreamCallbacks {
onChunk?: (chunk: string) => void;
onReasoningChunk?: (chunk: string) => void;
onToolCallChunk?: (chunk: string) => void;
onAttachments?: (extras: DatabaseMessageExtra[]) => void;
onModel?: (model: string) => void;
onTimings?: (timings?: ChatMessageTimings, promptProgress?: ChatMessagePromptProgress) => void;
onComplete?: (
content?: string,
reasoningContent?: string,
timings?: ChatMessageTimings,
toolCallContent?: string
) => void;
onError?: (error: Error) => void;
}
/**
* Error dialog state for displaying server/timeout errors
*/
export interface ErrorDialogState {
type: 'timeout' | 'server';
message: string;
contextInfo?: { n_prompt_tokens: number; n_ctx: number };
}
/**
* Live processing stats during prompt evaluation
*/
export interface LiveProcessingStats {
tokensProcessed: number;
totalTokens: number;
timeMs: number;
tokensPerSecond: number;
etaSecs?: number;
}
/**
* Live generation stats during token generation
*/
export interface LiveGenerationStats {
tokensGenerated: number;
timeMs: number;
tokensPerSecond: number;
}
/**
* Options for getting attachment display items
*/
export interface AttachmentDisplayItemsOptions {
uploadedFiles?: ChatUploadedFile[];
attachments?: DatabaseMessageExtra[];
}
/**
* Result of file processing operation
*/
export interface FileProcessingResult {
extras: DatabaseMessageExtra[];
emptyFiles: string[];
}

View File

@ -1,3 +1,5 @@
import type { AttachmentType } from '$lib/enums';
/**
* Common utility types used across the application
*/
@ -10,3 +12,32 @@ export interface KeyValuePair {
key: string;
value: string;
}
/**
* Binary detection configuration options
*/
export interface BinaryDetectionOptions {
/** Number of characters to check from the beginning of the file */
prefixLength: number;
/** Maximum ratio of suspicious characters allowed (0.0 to 1.0) */
suspiciousCharThresholdRatio: number;
/** Maximum absolute number of null bytes allowed */
maxAbsoluteNullBytes: number;
}
/**
* Format for text attachments when copied to clipboard
*/
export interface ClipboardTextAttachment {
type: typeof AttachmentType.TEXT;
name: string;
content: string;
}
/**
* Parsed result from clipboard content
*/
export interface ParsedClipboardContent {
message: string;
textAttachments: ClipboardTextAttachment[];
}

View File

@ -39,11 +39,21 @@ export type {
ChatAttachmentPreviewItem,
ChatMessageSiblingInfo,
ChatMessagePromptProgress,
ChatMessageTimings
ChatMessageTimings,
ChatMessageAgenticTimings,
ChatMessageAgenticTurnStats,
ChatMessageToolCallTiming,
ChatStreamCallbacks,
ErrorDialogState,
LiveProcessingStats,
LiveGenerationStats,
AttachmentDisplayItemsOptions,
FileProcessingResult
} from './chat.d';
// Database types
export type {
McpServerOverride,
DatabaseConversation,
DatabaseMessageExtraAudioFile,
DatabaseMessageExtraImageFile,
@ -58,18 +68,28 @@ export type {
} from './database';
// Model types
export type { ModelModalities, ModelOption } from './models';
export type { ModelModalities, ModelOption, ModalityCapabilities } from './models';
// Settings types
export type {
SettingsConfigValue,
SettingsFieldConfig,
SettingsChatServiceOptions,
SettingsConfigType
SettingsConfigType,
ParameterSource,
ParameterValue,
ParameterRecord,
ParameterInfo,
SyncableParameter
} from './settings';
// Common types
export type { KeyValuePair } from './common';
export type {
KeyValuePair,
BinaryDetectionOptions,
ClipboardTextAttachment,
ParsedClipboardContent
} from './common';
// MCP types
export type {
@ -99,3 +119,18 @@ export type {
GetPromptResult,
PromptMessage
} from './mcp';
// Agentic types
export type {
AgenticConfig,
AgenticToolCallPayload,
AgenticMessage,
AgenticAssistantMessage,
AgenticToolCallList,
AgenticChatCompletionRequest,
AgenticSession,
AgenticFlowCallbacks,
AgenticFlowOptions,
AgenticFlowParams,
AgenticFlowResult
} from './agentic';

View File

@ -15,3 +15,11 @@ export interface ModelOption {
details?: ApiModelDetails['details'];
meta?: ApiModelDataEntry['meta'];
}
/**
* Modality capabilities for file validation
*/
export interface ModalityCapabilities {
hasVision: boolean;
hasAudio: boolean;
}

View File

@ -69,3 +69,24 @@ export interface SettingsChatServiceOptions {
export type SettingsConfigType = typeof SETTING_CONFIG_DEFAULT & {
[key: string]: SettingsConfigValue;
};
/**
* Parameter synchronization types for server defaults and user overrides
*/
export type ParameterSource = 'default' | 'custom';
export type ParameterValue = string | number | boolean;
export type ParameterRecord = Record<string, ParameterValue>;
export interface ParameterInfo {
value: string | number | boolean;
source: ParameterSource;
serverDefault?: string | number | boolean;
userOverride?: string | number | boolean;
}
export interface SyncableParameter {
key: string;
serverKey: string;
type: 'number' | 'string' | 'boolean';
canSync: boolean;
}

View File

@ -1,5 +1,10 @@
import { AttachmentType, FileTypeCategory, SpecialFileType } from '$lib/enums';
import { getFileTypeCategory, getFileTypeCategoryByExtension, isImageFile } from '$lib/utils';
import type {
AttachmentDisplayItemsOptions,
ChatUploadedFile,
DatabaseMessageExtra
} from '$lib/types';
/**
* Formats attachment content for API requests with consistent header style.
@ -21,11 +26,6 @@ export function formatAttachmentText(
return `\n\n--- ${label}: ${header} ---\n${content}`;
}
export interface AttachmentDisplayItemsOptions {
uploadedFiles?: ChatUploadedFile[];
attachments?: DatabaseMessageExtra[];
}
/**
* Check if an uploaded file is an MCP prompt
*/

View File

@ -23,7 +23,7 @@ export {
} from './pdf-processing';
// File conversion utilities (depends on pdf-processing)
export { parseFilesToMessageExtras, type FileProcessingResult } from './convert-files-to-extra';
export { parseFilesToMessageExtras } from './convert-files-to-extra';
// File upload processing utilities (depends on pdf-processing, svg-to-png, webp-to-png)
export { processFilesToChatUploaded } from './process-uploaded-files';

View File

@ -3,8 +3,10 @@ import { AttachmentType } from '$lib/enums';
import type {
DatabaseMessageExtra,
DatabaseMessageExtraTextFile,
DatabaseMessageExtraLegacyContext
} from '$lib/types/database';
DatabaseMessageExtraLegacyContext,
ClipboardTextAttachment,
ParsedClipboardContent
} from '$lib/types';
/**
* Copy text to clipboard with toast notification
@ -68,23 +70,6 @@ export async function copyCodeToClipboard(
return copyToClipboard(rawCode, successMessage, errorMessage);
}
/**
* Format for text attachments when copied to clipboard
*/
export interface ClipboardTextAttachment {
type: typeof AttachmentType.TEXT;
name: string;
content: string;
}
/**
* Parsed result from clipboard content
*/
export interface ParsedClipboardContent {
message: string;
textAttachments: ClipboardTextAttachment[];
}
/**
* Formats a message with text attachments for clipboard copying.
*

View File

@ -7,6 +7,7 @@ import { modelsStore } from '$lib/stores/models.svelte';
import { getFileTypeCategory } from '$lib/utils';
import { readFileAsText, isLikelyTextFile } from './text-files';
import { toast } from 'svelte-sonner';
import type { FileProcessingResult, ChatUploadedFile, DatabaseMessageExtra } from '$lib/types';
function readFileAsBase64(file: File): Promise<string> {
return new Promise((resolve, reject) => {
@ -25,11 +26,6 @@ function readFileAsBase64(file: File): Promise<string> {
});
}
export interface FileProcessingResult {
extras: DatabaseMessageExtra[];
emptyFiles: string[];
}
export async function parseFilesToMessageExtras(
files: ChatUploadedFile[],
activeModelId?: string

View File

@ -12,10 +12,7 @@ export { getAuthHeaders, getJsonHeaders } from './api-headers';
export { validateApiKey } from './api-key-validation';
// Attachment utilities
export {
getAttachmentDisplayItems,
type AttachmentDisplayItemsOptions
} from './attachment-display';
export { getAttachmentDisplayItems } from './attachment-display';
export { isTextFile, isImageFile, isPdfFile, isAudioFile } from './attachment-type';
// Textarea utilities
@ -45,9 +42,7 @@ export {
copyCodeToClipboard,
formatMessageForClipboard,
parseClipboardContent,
hasClipboardAttachments,
type ClipboardTextAttachment,
type ParsedClipboardContent
hasClipboardAttachments
} from './clipboard';
// File preview utilities
@ -82,8 +77,7 @@ export { maskInlineLaTeX, preprocessLaTeX } from './latex-protection';
export {
isFileTypeSupportedByModel,
filterFilesByModalities,
generateModalityErrorMessage,
type ModalityCapabilities
generateModalityErrorMessage
} from './modality-file-validation';
// Model name utilities

View File

@ -5,12 +5,7 @@
import { getFileTypeCategory } from '$lib/utils';
import { FileTypeCategory } from '$lib/enums';
/** Modality capabilities for file validation */
export interface ModalityCapabilities {
hasVision: boolean;
hasAudio: boolean;
}
import type { ModalityCapabilities } from '$lib/types';
/**
* Check if a file type is supported by the given modalities

View File

@ -3,10 +3,8 @@
* Handles text file detection, reading, and validation
*/
import {
DEFAULT_BINARY_DETECTION_OPTIONS,
type BinaryDetectionOptions
} from '$lib/constants/binary-detection';
import { DEFAULT_BINARY_DETECTION_OPTIONS } from '$lib/constants/binary-detection';
import type { BinaryDetectionOptions } from '$lib/types';
import { FileExtensionText } from '$lib/enums';
/**