File size: 10,535 Bytes
31dd200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**

 *

 * SERVICES

 *

 * Stateless service layer for API communication and data operations.

 * Services handle protocol-level concerns (HTTP, WebSocket, MCP, IndexedDB)

 * without managing reactive state β€” that responsibility belongs to stores.

 *

 * **Design Principles:**

 * - All methods are static β€” no instance state

 * - Pure I/O operations (network requests, database queries)

 * - No Svelte runes or reactive primitives

 * - Error handling at the protocol level; business-level error handling in stores

 *

 * **Architecture (bottom to top):**

 * - **Services** (this layer): Stateless protocol communication

 * - **Stores**: Reactive state management consuming services

 * - **Components**: UI consuming stores

 *

 */

/**

 * **ChatService** - Chat Completions API communication layer

 *

 * Handles direct communication with the llama-server's `/v1/chat/completions` endpoint.

 * Provides streaming and non-streaming response parsing, message format conversion

 * (DatabaseMessage β†’ API format), and request lifecycle management.

 *

 * **Terminology - Chat vs Conversation:**

 * - **Chat**: The active interaction space with the Chat Completions API. Ephemeral and

 *   runtime-focused β€” sending messages, receiving streaming responses, managing request lifecycles.

 * - **Conversation**: The persistent database entity storing all messages and metadata.

 *   Managed by conversationsStore, conversations persist across sessions.

 *

 * **Architecture & Relationships:**

 * - **ChatService** (this class): Stateless API communication layer

 *   - Handles HTTP requests/responses with the llama-server

 *   - Manages streaming and non-streaming response parsing

 *   - Converts database messages to API format (multimodal, tool calls)

 *   - Handles error translation with user-friendly messages

 *

 * - **chatStore**: Primary consumer β€” uses ChatService for all AI model communication

 * - **agenticStore**: Uses ChatService for multi-turn agentic loop streaming

 * - **conversationsStore**: Provides message context for API requests

 *

 * **Key Responsibilities:**

 * - Streaming response handling with real-time content/reasoning/tool-call callbacks

 * - Non-streaming response parsing with complete response extraction

 * - Database message to API format conversion (attachments, tool calls, multimodal)

 * - Tool call delta merging for incremental streaming aggregation

 * - Request parameter assembly (sampling, penalties, custom params)

 * - File attachment processing (images, PDFs, audio, text, MCP prompts/resources)

 * - Reasoning content stripping from prompt history to avoid KV cache pollution

 * - Error translation (network, timeout, server errors β†’ user-friendly messages)

 *

 * @see chatStore in stores/chat.svelte.ts β€” primary consumer for chat state management

 * @see agenticStore in stores/agentic.svelte.ts β€” uses ChatService for agentic loop streaming

 * @see conversationsStore in stores/conversations.svelte.ts β€” provides message context

 */
export { ChatService } from './chat.service';

/**

 * **DatabaseService** - IndexedDB persistence layer via Dexie ORM

 *

 * Provides stateless data access for conversations and messages using IndexedDB.

 * Handles all low-level storage operations including branching tree structures,

 * cascade deletions, and transaction safety for multi-table operations.

 *

 * **Architecture & Relationships (bottom to top):**

 * - **DatabaseService** (this class): Stateless IndexedDB operations

 *   - Lowest layer β€” direct Dexie/IndexedDB communication

 *   - Pure CRUD operations without business logic

 *   - Handles branching tree structure (parent-child relationships)

 *   - Provides transaction safety for multi-table operations

 *

 * - **conversationsStore**: Reactive state management layer

 *   - Uses DatabaseService for all persistence operations

 *   - Manages conversation list, active conversation, and messages in memory

 *

 * - **chatStore**: Active AI interaction management

 *   - Uses conversationsStore for conversation context

 *   - Directly uses DatabaseService for message CRUD during streaming

 *

 * **Key Responsibilities:**

 * - Conversation CRUD (create, read, update, delete)

 * - Message CRUD with branching support (parent-child relationships)

 * - Root message and system prompt creation

 * - Cascade deletion of message branches (descendants)

 * - Transaction-safe multi-table operations

 * - Conversation import with duplicate detection

 *

 * **Database Schema:**

 * - `conversations`: id, lastModified, currNode, name

 * - `messages`: id, convId, type, role, timestamp, parent, children

 *

 * **Branching Model:**

 * Messages form a tree structure where each message can have multiple children,

 * enabling conversation branching and alternative response paths. The conversation's

 * `currNode` tracks the currently active branch endpoint.

 *

 * @see conversationsStore in stores/conversations.svelte.ts β€” reactive layer on top of DatabaseService

 * @see chatStore in stores/chat.svelte.ts β€” uses DatabaseService directly for message CRUD during streaming

 */
export { DatabaseService } from './database.service';

/**

 * **ModelsService** - Model management API communication

 *

 * Handles communication with model-related endpoints for both MODEL (single model)

 * and ROUTER (multi-model) server modes. Provides model listing, loading/unloading,

 * and status checking without managing any model state.

 *

 * **Architecture & Relationships:**

 * - **ModelsService** (this class): Stateless HTTP communication

 *   - Sends requests to model endpoints

 *   - Parses and returns typed API responses

 *   - Provides model status utility methods

 *

 * - **modelsStore**: Primary consumer β€” manages reactive model state

 *   - Calls ModelsService for all model API operations

 *   - Handles polling, caching, and state updates

 *

 * **Key Responsibilities:**

 * - List available models via OpenAI-compatible `/v1/models` endpoint

 * - Load/unload models via `/models/load` and `/models/unload` (ROUTER mode)

 * - Model status queries (loaded, loading)

 *

 * **Server Mode Behavior:**

 * - **MODEL mode**: Only `list()` is relevant β€” single model always loaded

 * - **ROUTER mode**: Full lifecycle β€” `list()`, `listRouter()`, `load()`, `unload()`

 *

 * **Endpoints:**

 * - `GET /v1/models` β€” OpenAI-compatible model list (both modes)

 * - `POST /models/load` β€” Load a model (ROUTER mode only)

 * - `POST /models/unload` β€” Unload a model (ROUTER mode only)

 *

 * @see modelsStore in stores/models.svelte.ts β€” primary consumer for reactive model state

 */
export { ModelsService } from './models.service';

/**

 * **PropsService** - Server properties and capabilities retrieval

 *

 * Fetches server configuration, model information, and capabilities from the `/props`

 * endpoint. Supports both global server props and per-model props (ROUTER mode).

 *

 * **Architecture & Relationships:**

 * - **PropsService** (this class): Stateless HTTP communication

 *   - Fetches server properties from `/props` endpoint

 *   - Handles authentication and request parameters

 *   - Returns typed `ApiLlamaCppServerProps` responses

 *

 * - **serverStore**: Consumes global server properties (role detection, connection state)

 * - **modelsStore**: Consumes per-model properties (modalities, context size)

 * - **settingsStore**: Syncs default generation parameters from props response

 *

 * **Key Responsibilities:**

 * - Fetch global server properties (default generation settings, modalities)

 * - Fetch per-model properties in ROUTER mode via `?model=<id>` parameter

 * - Handle autoload control to prevent unintended model loading

 *

 * **API Behavior:**

 * - `GET /props` β†’ Global server props (MODEL mode: includes modalities)

 * - `GET /props?model=<id>` β†’ Per-model props (ROUTER mode: model-specific modalities)

 * - `&autoload=false` β†’ Prevents model auto-loading when querying props

 *

 * @see serverStore in stores/server.svelte.ts β€” consumes global server props

 * @see modelsStore in stores/models.svelte.ts β€” consumes per-model props for modalities

 * @see settingsStore in stores/settings.svelte.ts β€” syncs default generation params from props

 */
export { PropsService } from './props.service';

/**

 * **ParameterSyncService** - Server defaults and user settings synchronization

 *

 * Manages the complex logic of merging server-provided default parameters with

 * user-configured overrides. Ensures the UI reflects the actual server state

 * while preserving user customizations. Tracks parameter sources (server default

 * vs user override) for display in the settings UI.

 *

 * **Architecture & Relationships:**

 * - **ParameterSyncService** (this class): Stateless sync logic

 *   - Pure functions for parameter extraction, merging, and diffing

 *   - No side effects β€” receives data in, returns data out

 *   - Handles floating-point precision normalization

 *

 * - **settingsStore**: Primary consumer β€” calls sync methods during:

 *   - Initial load (`syncWithServerDefaults`)

 *   - Settings reset (`forceSyncWithServerDefaults`)

 *   - Parameter info queries (`getParameterInfo`)

 *

 * - **PropsService**: Provides raw server props that feed into extraction

 *

 * **Key Responsibilities:**

 * - Extract syncable parameters from server `/props` response

 * - Merge server defaults with user overrides (user wins)

 * - Track parameter source (Custom vs Default) for UI badges

 * - Validate server parameter values by type (number, string, boolean)

 * - Create diffs between current settings and server defaults

 * - Floating-point precision normalization for consistent comparisons

 *

 * **Parameter Source Priority:**

 * 1. **User Override** (Custom badge) β€” explicitly set by user in settings

 * 2. **Server Default** (Default badge) β€” from `/props` endpoint

 * 3. **App Default** β€” hardcoded fallback when server props unavailable

 *

 * **Exports:**

 * - `ParameterSyncService` class β€” static methods for sync logic

 * - `SYNCABLE_PARAMETERS` β€” mapping of webui setting keys to server parameter keys

 *

 * @see settingsStore in stores/settings.svelte.ts β€” primary consumer for settings sync

 * @see ChatSettingsParameterSourceIndicator β€” displays parameter source badges in UI

 */
export { ParameterSyncService } from './parameter-sync.service';