-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal-lm-chatbot-safety-fix.js
More file actions
345 lines (316 loc) · 13 KB
/
Copy pathsignal-lm-chatbot-safety-fix.js
File metadata and controls
345 lines (316 loc) · 13 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
(function () {
if (window.__signalLmChatbotSafetyFix) return;
window.__signalLmChatbotSafetyFix = true;
var SETTINGS_KEY = 'lmStudioLite.settings.v1';
var MESSAGES_KEY = 'lmStudioLite.messages.v1';
var FETCH_FLAG = '__signalLmChatbotLoopFetchPatch';
var SUBMIT_FLAG = '__signalLmChatbotLoopSubmitPatch';
var APPLY_FLAG = '__signalLmChatbotLoopApplyPatch';
var JSON_CLASS = 'json-source-preserved';
var recentPrompts = [];
var recentFetches = [];
var recentApplies = [];
var WINDOW_MS = 90000;
var APPLY_WINDOW_MS = 45000;
var MAX_REPEAT = 2;
var lastInteraction = 0;
var userHasJustSubmitted = false;
var submissionTimeout = null;
function trackInteraction() {
lastInteraction = Date.now();
}
function flagUserSubmission() {
userHasJustSubmitted = true;
trackInteraction();
if (submissionTimeout) clearTimeout(submissionTimeout);
submissionTimeout = setTimeout(function () {
userHasJustSubmitted = false;
}, 60000);
}
if (typeof document !== 'undefined') {
document.addEventListener('keydown', trackInteraction, true);
document.addEventListener('mousedown', trackInteraction, true);
document.addEventListener('touchstart', trackInteraction, true);
document.addEventListener('input', trackInteraction, true);
document.addEventListener('submit', function (e) {
if (e.target && (e.target.id === 'chat-form' || e.target.classList.contains('input-area'))) {
flagUserSubmission();
} else {
trackInteraction();
}
}, true);
document.addEventListener('click', function (e) {
var target = e.target;
var isSubmitClick = false;
while (target && target !== document) {
if (target.id === 'send-btn' || target.classList.contains('send-btn') || target.classList.contains('submit-btn')) {
isSubmitClick = true;
break;
}
target = target.parentNode;
}
if (isSubmitClick) {
flagUserSubmission();
} else {
trackInteraction();
}
}, true);
}
function isUserInteracting() {
return (Date.now() - lastInteraction) < 4000;
}
function now() { return Date.now(); }
function readSettings() { try { return JSON.parse(localStorage.getItem(SETTINGS_KEY) || '{}') || {}; } catch (error) { return {}; } }
function mcpEnabled() { return Boolean(readSettings().mcpEnabled); }
function runtime() { return window.SignalLMChatCommands || {}; }
function toast(message) { var api = runtime(); if (api && typeof api.toast === 'function') api.toast(message); }
function edits() { return Array.isArray(window.pendingEdits) ? window.pendingEdits : []; }
function escapeHtml(value) {
return String(value || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
}
function hashString(value) {
var str = String(value || '');
var hash = 2166136261;
for (var i = 0; i < str.length; i++) {
hash ^= str.charCodeAt(i);
hash = Math.imul(hash, 16777619);
}
return (hash >>> 0).toString(16);
}
function normalizePrompt(value) {
return String(value || '')
.replace(/response_id\s*[:=]\s*[\w.-]+/gi, 'response_id:<id>')
.replace(/\b[0-9a-f]{8,}\b/gi, '<hex>')
.replace(/\s+/g, ' ')
.trim()
.slice(0, 20000);
}
function rememberAndCheck(bucket, key, windowMs, maxRepeat) {
var t = now();
for (var i = bucket.length - 1; i >= 0; i--) {
if (t - bucket[i].time > windowMs) bucket.splice(i, 1);
}
var count = bucket.filter(function (item) { return item.key === key; }).length;
bucket.push({ key: key, time: t });
return count >= maxRepeat;
}
function normalizeFetchBody(body) {
if (!body || typeof body !== 'object') return '';
var input = typeof body.input === 'string'
? body.input
: Array.isArray(body.input)
? body.input.map(function (part) { return part && (part.text || part.content || ''); }).join('\n')
: '';
var toolNames = Array.isArray(body.integrations)
? body.integrations.map(function (item) {
if (!item) return '';
if (typeof item === 'string') return item;
return [item.type, item.server_label, item.id, Array.isArray(item.allowed_tools) ? item.allowed_tools.join(',') : ''].join(':');
}).join('|')
: '';
return normalizePrompt([body.model || '', input, toolNames].join('\n---\n'));
}
function isMcpChatRequest(resource, body) {
if (!mcpEnabled()) return false;
var url = typeof resource === 'string' ? resource : resource && resource.url ? resource.url : String(resource || '');
return /\/api\/v1\/chat(?:[?#].*)?$/i.test(url) || Boolean(body && Array.isArray(body.integrations));
}
function installFetchLoopGuard() {
if (window[FETCH_FLAG] || typeof window.fetch !== 'function') return false;
window[FETCH_FLAG] = true;
var originalFetch = window.fetch.bind(window);
window.fetch = function signalLmLoopGuardFetch(resource, init) {
try {
var raw = init && init.body;
if (typeof raw === 'string' && raw.trim().charAt(0) === '{') {
var parsed = JSON.parse(raw);
if (isMcpChatRequest(resource, parsed)) {
var key = hashString(normalizeFetchBody(parsed));
if (userHasJustSubmitted) {
userHasJustSubmitted = false;
if (submissionTimeout) {
clearTimeout(submissionTimeout);
submissionTimeout = null;
}
rememberAndCheck(recentFetches, key, WINDOW_MS, MAX_REPEAT);
return originalFetch(resource, init);
}
if (rememberAndCheck(recentFetches, key, WINDOW_MS, MAX_REPEAT)) {
if (isUserInteracting()) {
return originalFetch(resource, init);
}
var message = 'Loop guard stopped a repeated MCP/chatbot request. The draft remains staged; change the request or clear the staged edits before retrying.';
toast(message);
return Promise.reject(new Error(message));
}
}
}
} catch (error) {
// Fall through to the original request when body parsing fails.
}
return originalFetch(resource, init);
};
return true;
}
function installSubmitLoopGuard() {
var api = runtime();
if (!api || typeof api.submitPrompt !== 'function' || api.submitPrompt[SUBMIT_FLAG]) return false;
var originalSubmit = api.submitPrompt;
api.submitPrompt = function signalLmLoopGuardSubmit(prompt) {
flagUserSubmission();
var key = hashString(normalizePrompt(prompt));
if (rememberAndCheck(recentPrompts, key, WINDOW_MS, MAX_REPEAT)) {
if (isUserInteracting()) {
return originalSubmit.apply(this, arguments);
}
toast('Loop guard stopped a repeated chatbot prompt. Edit the prompt or clear staged edits before retrying.');
return false;
}
return originalSubmit.apply(this, arguments);
};
api.submitPrompt[SUBMIT_FLAG] = true;
return true;
}
function applyKey() {
var data = edits().map(function (edit) {
return [edit && edit.path || '', hashString(edit && edit.content || '')].join(':');
}).join('|');
var selected = '';
try {
var helper = window.SignalLMMcpFilePath;
selected = helper && typeof helper.getMcpFilePath === 'function' ? helper.getMcpFilePath() : (readSettings().mcpFilePath || '');
} catch (error) {}
return hashString(String(selected || '') + '\n' + data);
}
function installApplyLoopGuard() {
var api = runtime();
var originalApply = window.applyPendingEdits || (api && api.applyPendingEdits);
if (!api || typeof originalApply !== 'function' || originalApply[APPLY_FLAG]) return false;
var patched = function signalLmLoopGuardApply() {
trackInteraction();
if (edits().length) {
var key = applyKey();
if (rememberAndCheck(recentApplies, key, APPLY_WINDOW_MS, 1)) {
if (isUserInteracting()) {
return originalApply.apply(this, arguments);
}
toast('Loop guard stopped a repeated Apply for the same staged draft. The draft is still staged.');
return true;
}
}
return originalApply.apply(this, arguments);
};
patched[APPLY_FLAG] = true;
patched.__originalApplyPendingEdits = originalApply;
window.applyPendingEdits = patched;
api.applyPendingEdits = patched;
return true;
}
function readMessages() {
try { return JSON.parse(localStorage.getItem(MESSAGES_KEY) || '[]') || []; }
catch (error) { return []; }
}
function extractVisibleJsonBlocks(text) {
var blocks = [];
var raw = String(text || '');
var re = /```(json|lmstudio-edits)\s*\n([\s\S]*?)```/gi;
var match;
while ((match = re.exec(raw))) {
var lang = match[1] || 'json';
var code = String(match[2] || '').trim();
if (!code) continue;
var shouldPreserve = false;
try {
var parsed = JSON.parse(code);
shouldPreserve = isToolOrEditJson(parsed);
} catch (error) {
shouldPreserve = false;
}
if (shouldPreserve) blocks.push({ lang: lang, code: code });
}
return blocks;
}
function isToolOrEditJson(parsed) {
if (!parsed || typeof parsed !== 'object') return false;
if (Array.isArray(parsed)) return parsed.some(isToolOrEditJson);
if (Array.isArray(parsed.files) || Array.isArray(parsed.changes)) return true;
if (parsed.type === 'tool_use' || parsed.tool || parsed.tool_name || parsed.name || parsed.function_call || parsed.arguments) return true;
return false;
}
function makeJsonBlock(block, index) {
var outer = document.createElement('details');
outer.className = JSON_CLASS;
outer.open = true;
outer.dataset.jsonIndex = String(index);
outer.innerHTML = '<summary>Original tool/edit ' + escapeHtml(block.lang) + ' block</summary>' +
'<pre><code class="language-json">' + escapeHtml(block.code) + '</code></pre>';
return outer;
}
function annotateJsonBlocks() {
var stored = readMessages();
if (!stored.length) return;
var rows = Array.prototype.slice.call(document.querySelectorAll('.message-row'));
var messageIndex = 0;
rows.forEach(function (row) {
if (row.querySelector('.command-result')) return;
var expectedRole = row.classList.contains('ai') ? 'assistant' : row.classList.contains('user') ? 'user' : '';
var message = null;
while (messageIndex < stored.length) {
var candidate = stored[messageIndex++];
if ((candidate && candidate.role) === expectedRole) { message = candidate; break; }
}
if (!message || expectedRole !== 'assistant') return;
var content = message.displayContent || message.content || '';
var blocks = extractVisibleJsonBlocks(content);
if (!blocks.length) return;
var bubble = row.querySelector('.bubble');
if (!bubble || bubble.dataset.jsonSourcePreserved === hashString(content)) return;
bubble.querySelectorAll('.' + JSON_CLASS).forEach(function (node) { node.remove(); });
blocks.forEach(function (block, index) { bubble.appendChild(makeJsonBlock(block, index)); });
bubble.dataset.jsonSourcePreserved = hashString(content);
if (window.Prism) bubble.querySelectorAll('.' + JSON_CLASS + ' code').forEach(function (code) { Prism.highlightElement(code); });
});
}
function installJsonPreserver() {
if (window.__signalLmJsonVisibilityPreserver) return false;
window.__signalLmJsonVisibilityPreserver = true;
var originalSetItem = Storage.prototype.setItem;
Storage.prototype.setItem = function signalLmStorageSetItem(key, value) {
var result = originalSetItem.apply(this, arguments);
if (key === MESSAGES_KEY) setTimeout(annotateJsonBlocks, 0);
return result;
};
var target = document.getElementById('messages');
if (target && window.MutationObserver) {
var observer = new MutationObserver(function () { setTimeout(annotateJsonBlocks, 0); });
observer.observe(target, { childList: true, subtree: true });
}
setTimeout(annotateJsonBlocks, 0);
return true;
}
function hookRuntimeCommands() {
if (typeof window.executeSlashCommand === 'function' && !window.executeSlashCommand.__patchedForInteraction) {
var originalExec = window.executeSlashCommand;
window.executeSlashCommand = function () {
flagUserSubmission();
return originalExec.apply(this, arguments);
};
window.executeSlashCommand.__patchedForInteraction = true;
}
}
function install() {
installFetchLoopGuard();
installSubmitLoopGuard();
installApplyLoopGuard();
installJsonPreserver();
hookRuntimeCommands();
}
window.SignalLMChatbotSafetyFix = {
install: install,
annotateJsonBlocks: annotateJsonBlocks
};
var timer = setInterval(install, 250);
setTimeout(function () { clearInterval(timer); }, 12000);
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', install);
else install();
})();