-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
169 lines (146 loc) · 5.72 KB
/
index.js
File metadata and controls
169 lines (146 loc) · 5.72 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
import { renderExtensionTemplateAsync } from '../../../extensions.js';
import { eventSource, event_types, saveSettingsDebounced } from '../../../../script.js';
import { clearExtensionPrompt, getSettings, initializeSettings, setSettings } from './core/settings.js';
import { getChatState, saveChatState, ensureChatState, getActiveChatId, preloadChat } from './core/storage.js';
import { invalidateCache } from './core/localforage-store.js';
import { getAllDossiers, getDossier } from './core/dossier-store.js';
import { registerEventHooks } from './runtime/event-hooks.js';
import { runGenerationInterceptor } from './runtime/generation-hook.js';
import { processCompletedTurn } from './runtime/postgen-hook.js';
import { renderPanel } from './ui/panel.js';
import { registerSlashCommands } from './commands/slash.js';
import { registerMemoryTool, unregisterMemoryTool } from './tools/memory-tool.js';
export const EXTENSION_NAME = 'anchor-memory';
export const EXTENSION_FOLDER = `third-party/${EXTENSION_NAME}`;
let initialized = false;
let previousChatId = null;
export async function initAnchorMemory() {
if (initialized) {
renderPanel(getSettings());
return getSettings();
}
initialized = true;
initializeSettings();
const settings = getSettings();
registerEventHooks({
onBeforeGenerate: runGenerationInterceptor,
});
if (!document.getElementById('anchor_memory_settings')) {
const settingsHtml = $(await renderExtensionTemplateAsync(EXTENSION_FOLDER, 'settings'));
$('#extensions_settings').append(settingsHtml);
}
const activeChatId = getActiveChatId();
await preloadChat(activeChatId);
previousChatId = activeChatId;
renderPanel(settings);
bindUi();
bindRuntimeEvents();
registerSlashCommands();
ensureActiveChatState();
if (settings.memoryToolEnabled) {
registerMemoryTool();
}
if (typeof window !== 'undefined') {
window.AnchorMemory = {
clearPrompt: () => clearExtensionPrompt(),
getAllDossiers,
getChatState,
getDossier,
getSettings,
initAnchorMemory,
processCompletedTurn,
saveChatState,
setSettings,
};
}
return settings;
}
function bindUi() {
const settings = getSettings();
$('#am_enabled').prop('checked', settings.enabled);
$('#am_prompt_position').val(settings.promptPosition);
$('#am_prompt_depth').val(settings.promptDepth);
$('#am_memory_format').val(settings.memoryFormat);
$('#am_memory_model_source').val(settings.memoryModelSource);
$('#am_memory_model').val(settings.memoryModel);
$('#am_llm_consolidation').prop('checked', settings.llmConsolidation);
$('#am_memory_tool_enabled').prop('checked', settings.memoryToolEnabled);
$('#am_enabled').off('change').on('change', function () {
updateSettings({ enabled: $(this).prop('checked') });
if (!$(this).prop('checked')) {
clearExtensionPrompt();
renderPanel(getSettings());
}
});
$('#am_prompt_position').off('change').on('change', function () {
updateSettings({ promptPosition: String($(this).val() || 'in_chat') });
});
$('#am_prompt_depth').off('input').on('input', function () {
updateSettings({ promptDepth: toPositiveInt($(this).val(), 1) });
});
$('#am_memory_format').off('change').on('change', function () {
updateSettings({ memoryFormat: String($(this).val() || 'text') });
});
$('#am_memory_model_source').off('input').on('input', function () {
updateSettings({ memoryModelSource: String($(this).val() || '') });
});
$('#am_memory_model').off('input').on('input', function () {
updateSettings({ memoryModel: String($(this).val() || '') });
});
$('#am_llm_consolidation').off('change').on('change', function () {
updateSettings({ llmConsolidation: $(this).prop('checked') });
});
$('#am_memory_tool_enabled').off('change').on('change', function () {
const enabled = $(this).prop('checked');
updateSettings({ memoryToolEnabled: enabled });
if (enabled) {
registerMemoryTool();
} else {
unregisterMemoryTool();
}
});
$('#am_debug_retrieval_logging').prop('checked', settings.debugRetrievalLogging);
$('#am_debug_retrieval_logging').off('change').on('change', function () {
updateSettings({ debugRetrievalLogging: $(this).prop('checked') });
});
}
function bindRuntimeEvents() {
eventSource.on(event_types.CHAT_CHANGED, async () => {
const newChatId = getActiveChatId();
if (previousChatId && previousChatId !== newChatId) {
invalidateCache(previousChatId);
}
await preloadChat(newChatId);
previousChatId = newChatId;
ensureActiveChatState();
renderPanel(getSettings());
});
eventSource.on(event_types.MESSAGE_RECEIVED, async () => {
renderPanel(getSettings());
});
eventSource.on(event_types.GENERATION_STOPPED, () => {
if (!getSettings().enabled) {
clearExtensionPrompt();
}
});
}
function ensureActiveChatState() {
ensureChatState(getActiveChatId());
}
function updateSettings(next) {
setSettings(next);
saveSettingsDebounced();
renderPanel(getSettings());
}
function toPositiveInt(value, fallback) {
const parsed = Number(value);
if (!Number.isFinite(parsed) || parsed < 0) return fallback;
return Math.round(parsed);
}
if (typeof window !== 'undefined') {
window.AnchorMemoryBootstrap = initAnchorMemory;
window.anchor_memory_intercept = (...args) => runGenerationInterceptor(...args);
}
jQuery(async () => {
await initAnchorMemory();
});