Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
285 lines (244 loc) · 9.68 KB
/
Copy pathindex.js
File metadata and controls
285 lines (244 loc) · 9.68 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* ============================================================================
* VECTHARE - ADVANCED RAG SYSTEM
* ============================================================================
* Entry point - lean and clean
* All logic is in separate modules - see project guidelines
*
* @author Coneja Chibi
* @version 2.0.0-alpha
* ============================================================================
*/
import {
eventSource,
event_types,
extension_prompt_types,
} from '../../../../script.js';
import {
ModuleWorkerWrapper,
extension_settings,
} from '../../../extensions.js';
import { debounce } from '../../../utils.js';
import { debounce_timeout } from '../../../constants.js';
// VectHare modules - Core
import { synchronizeChat, rearrangeChat, vectorizeAll } from './core/chat-vectorization.js';
import { purgeAllVectorIndexes, purgeVectorIndex } from './core/core-vector-api.js';
import { getChatCollectionId } from './core/chat-vectorization.js';
import { getDefaultDecaySettings } from './core/temporal-decay.js';
import { migrateOldEnabledKeys } from './core/collection-metadata.js';
import { clearCollectionRegistry, discoverExistingCollections } from './core/collection-loader.js';
// VectHare modules - UI
import { renderSettings, openDiagnosticsModal, loadWebLlmModels, updateWebLlmStatus, refreshAutoSyncCheckbox } from './ui/ui-manager.js';
import { initializeVisualizer } from './ui/chunk-visualizer.js';
import { initializeDatabaseBrowser } from './ui/database-browser.js';
import { initializeSceneMarkers, updateAllMarkerStates, setSceneSettings } from './ui/scene-markers.js';
// VectHare modules - Cotton-Tales Integration
import './core/emotion-classifier.js'; // Exposes window.VectHareEmotionClassifier
// Constants
const MODULE_NAME = 'VectHare';
// Default settings
const defaultSettings = {
// Core vector settings
source: 'transformers',
vector_backend: 'standard', // Backend: 'standard' (ST Vectra), 'lancedb', 'qdrant'
qdrant_host: 'localhost',
qdrant_port: 6333,
qdrant_url: '',
qdrant_api_key: '',
qdrant_use_cloud: false,
milvus_host: 'localhost',
milvus_port: 19530,
milvus_username: '',
milvus_password: '',
milvus_token: '',
milvus_address: '',
alt_endpoint_url: '',
use_alt_endpoint: false,
rate_limit_calls: 5,
rate_limit_interval: 60, // seconds
togetherai_model: 'togethercomputer/m2-bert-80M-32k-retrieval',
openai_model: 'text-embedding-ada-002',
electronhub_model: 'text-embedding-3-small',
openrouter_model: 'openai/text-embedding-3-large',
cohere_model: 'embed-english-v3.0',
ollama_model: 'mxbai-embed-large',
ollama_keep: false,
vllm_model: '',
webllm_model: '',
google_model: 'text-embedding-005',
bananabread_rerank: false,
bananabread_api_key: '', // Stored here since custom keys aren't returned by ST's readSecretState()
// Chat vectorization
enabled_chats: false,
chunking_strategy: 'per_message', // per_message, conversation_turns, message_batch, adaptive
batch_size: 4, // Messages per batch for message_batch strategy
depth: 2,
position: extension_prompt_types.IN_PROMPT,
protect: 5,
insert: 3,
query: 2,
chunk_size: 500, // For adaptive strategy only
score_threshold: 0.25,
// Advanced features
temporal_decay: getDefaultDecaySettings(),
// RAG Prompt Context (Global level)
// Wraps ALL injected content with context prompts and/or XML tags
rag_context: '', // Natural language context shown before all RAG content
rag_xml_tag: '', // XML tag to wrap all RAG content (e.g., "retrieved_context")
// Collection-level metadata (managed by collection-metadata.js)
collections: {},
// Collection registry (list of known collection IDs)
vecthare_collection_registry: [],
};
// Runtime settings (merged with saved settings)
let settings = { ...defaultSettings };
// Module worker for automatic syncing
const moduleWorker = new ModuleWorkerWrapper(() => synchronizeChat(settings, getBatchSize()));
// Batch size based on provider
const getBatchSize = () => ['transformers', 'ollama'].includes(settings.source) ? 1 : 5;
// Chat event handler (debounced)
const onChatEvent = debounce(async () => await moduleWorker.update(), debounce_timeout.relaxed);
/**
* Generation interceptor - searches and injects relevant messages
*/
async function vecthare_rearrangeChat(chat, _contextSize, _abort, type) {
await rearrangeChat(chat, settings, type);
}
// Export to window for ST to call
window['vecthare_rearrangeChat'] = vecthare_rearrangeChat;
/**
* Action: Vectorize all messages in current chat
*/
async function onVectorizeAllClick() {
await vectorizeAll(settings, getBatchSize());
}
/**
* Action: Full purge - wipes ALL vector data and settings
*/
async function onPurgeClick() {
const confirmed = confirm(
'WARNING: This will delete ALL vector data and reset VectHare settings.\n\n' +
'This cannot be undone. Continue?'
);
if (!confirmed) {
toastr.info('Purge cancelled');
return;
}
try {
const { getRequestHeaders } = await import('../../../../script.js');
// 1. Delete entire vectors folder
await fetch('/api/plugins/similharity/purge-all', {
method: 'POST',
headers: getRequestHeaders(),
});
// 2. Clear extension_settings.vecthare
for (const key in extension_settings.vecthare) {
if (key !== 'enabled') {
delete extension_settings.vecthare[key];
}
}
// 3. Save settings
const { saveSettingsDebounced } = await import('../../../../script.js');
saveSettingsDebounced();
toastr.success('All vector data purged', 'Purge Complete');
} catch (error) {
console.error('VectHare: Purge failed:', error);
toastr.error('Purge failed: ' + error.message);
}
}
/**
* Action: Run diagnostics - opens the diagnostics modal
*/
function onRunDiagnosticsClick() {
openDiagnosticsModal();
}
/**
* Initialize VectHare extension
*/
jQuery(async () => {
console.log('VectHare: Initializing...');
// Load saved settings
if (!extension_settings.vecthare) {
extension_settings.vecthare = defaultSettings;
}
// Merge saved settings with defaults
settings = {
...defaultSettings,
...extension_settings.vecthare,
temporal_decay: {
...defaultSettings.temporal_decay,
...extension_settings.vecthare.temporal_decay
},
collections: {
...defaultSettings.collections,
...extension_settings.vecthare.collections
}
};
// Migrate old scattered enabled keys to new collections structure
const migrationResult = migrateOldEnabledKeys();
if (migrationResult.migrated > 0) {
console.log(`VectHare: Migrated ${migrationResult.migrated} old collection enabled keys`);
}
// Render UI
renderSettings('extensions_settings2', settings, {
onVectorizeAll: onVectorizeAllClick,
onPurge: onPurgeClick,
onRunDiagnostics: onRunDiagnosticsClick
});
// Initialize auto-sync checkbox state for current chat (if any)
refreshAutoSyncCheckbox(settings);
// Initialize visualizer
initializeVisualizer();
// Initialize database browser
initializeDatabaseBrowser(settings);
// Initialize scene markers on chat messages (settings needed for DB operations)
setSceneSettings(settings);
initializeSceneMarkers();
// Discover existing collections on load (async, non-blocking)
discoverExistingCollections(settings).then(collections => {
if (collections.length > 0) {
console.log(`VectHare: Discovered ${collections.length} existing collections`);
}
}).catch(err => {
console.warn('VectHare: Collection discovery failed:', err.message);
});
// Register event handlers
eventSource.on(event_types.MESSAGE_DELETED, onChatEvent);
eventSource.on(event_types.MESSAGE_EDITED, onChatEvent);
eventSource.on(event_types.MESSAGE_SENT, onChatEvent);
eventSource.on(event_types.MESSAGE_RECEIVED, onChatEvent);
eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent);
// When a chat is deleted, purge its vectors (not full purge, just that chat)
eventSource.on(event_types.CHAT_DELETED, async (chatId) => {
if (chatId) {
const collectionId = getChatCollectionId(chatId);
if (collectionId) {
await purgeVectorIndex(collectionId, settings);
console.log(`VectHare: Purged vectors for deleted chat: ${chatId}`);
}
}
});
eventSource.on(event_types.GROUP_CHAT_DELETED, async (chatId) => {
if (chatId) {
const collectionId = getChatCollectionId(chatId);
if (collectionId) {
await purgeVectorIndex(collectionId, settings);
console.log(`VectHare: Purged vectors for deleted group chat: ${chatId}`);
}
}
});
// When WebLLM extension is loaded, refresh the model list
eventSource.on(event_types.EXTENSION_SETTINGS_LOADED, async (manifest) => {
if (settings.source === 'webllm' && manifest?.display_name === 'WebLLM') {
console.log('VectHare: WebLLM extension loaded, refreshing models...');
updateWebLlmStatus();
await loadWebLlmModels(settings);
}
});
// When chat changes, refresh UI state to match settings
eventSource.on(event_types.CHAT_CHANGED, () => {
console.log('VectHare: Chat changed, refreshing UI state');
refreshAutoSyncCheckbox(settings);
});
console.log('VectHare: ✅ Initialized successfully');
});