-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.js
More file actions
312 lines (254 loc) · 11.8 KB
/
main.js
File metadata and controls
312 lines (254 loc) · 11.8 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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2025 Philipp Emanuel Weidmann <pew@worldwidemann.com>
import { renderExtensionTemplateAsync } from "../../../extensions.js";
import { power_user } from "../../../power-user.js";
import { promptManager } from "../../../openai.js";
import { executeSlashCommandsWithOptions } from "../../../slash-commands.js";
import { eventSource, event_types, streamingProcessor, saveSettingsDebounced } from "../../../../script.js";
import { Handlebars, hljs } from "../../../../lib.js";
import { NAME } from "./common.js";
import { loadSettings } from "./settings.js";
// @ts-ignore: Hack to suppress IDE errors due to SillyTavern's
// weird mix of imports and globally defined objects.
const $ = window.$;
const settings = loadSettings();
function addScript(script = {}) {
const ids = settings.scripts.map(script => script.id);
const nextId = Math.max(...ids, 0) + 1;
const scriptCopy = Object.assign({
condition: "",
stscript: "",
javascript: ""
}, script, {
id: nextId,
enabled: true
});
settings.scripts.push(scriptCopy);
saveSettingsDebounced();
return scriptCopy;
}
function getEnabledScripts() {
if (settings.enabled) {
return settings.scripts.filter(script =>
script.enabled &&
script.condition.trim().length > 0 &&
(script.stscript.trim().length > 0 || script.javascript.trim().length > 0)
);
} else {
return [];
}
}
function getMainPrompt() {
for (const prompt of promptManager.serviceSettings.prompts) {
if (prompt.identifier === "main") {
return prompt;
}
}
}
let instructionsInjected = false;
let originalSystemPrompt;
let originalMainPrompt;
let enabledScripts;
let hookToBeInstalled;
function injectInstructions() {
// Due to SillyTavern's "dry run" mechanism, it is possible for two generations to interleave
// under certain circumstances. This leads to `injectInstructions` being invoked twice in a row
// without a call to `restorePrompts` in between, which would result in two copies of the instructions
// being injected. This guard protects against this rare occurrence, and prevents the second injection.
if (!instructionsInjected) {
originalSystemPrompt = power_user.sysprompt.content;
originalMainPrompt = getMainPrompt().content;
enabledScripts = getEnabledScripts();
if (enabledScripts.length > 0) {
const instructionsTemplate = Handlebars.compile(settings.instructions, { noEscape: true });
const instructions = instructionsTemplate({ scripts: enabledScripts });
power_user.sysprompt.content += instructions;
getMainPrompt().content += instructions;
hookToBeInstalled = true;
} else {
hookToBeInstalled = false;
}
instructionsInjected = true;
}
}
function restorePrompts() {
if (instructionsInjected) {
power_user.sysprompt.content = originalSystemPrompt;
getMainPrompt().content = originalMainPrompt;
instructionsInjected = false;
}
}
function runSTscript(stscript) {
executeSlashCommandsWithOptions(stscript);
}
function runJavaScript(javascript) {
Function(javascript)();
}
function runScript(script) {
console.log(`${NAME}: Running script ${script.id}...`);
if (settings.flashIcon) {
const element = $("#sorcery-button");
element.addClass("flashing-icon");
setTimeout(() => element.removeClass("flashing-icon"), 300);
}
if (script.stscript.trim().length > 0) {
runSTscript(script.stscript);
}
if (script.javascript.trim().length > 0) {
runJavaScript(script.javascript);
}
}
function installStreamHook() {
if (hookToBeInstalled) {
const markerRegex = new RegExp(settings.markerRegex, "g");
const partialMarkerRegex = new RegExp(settings.partialMarkerRegex);
const processedIndices = new Set();
const originalOnProgressStreaming = streamingProcessor.onProgressStreaming.bind(streamingProcessor);
function onProgressStreaming(messageId, text, isFinal) {
for (const match of text.matchAll(markerRegex)) {
if (!processedIndices.has(match.index)) {
// `match[0]` is the whole match, `match[1]` is the first capturing group, i.e. the script ID.
const scriptId = parseInt(match[1]);
// This is probably faster overall than constructing an ID => script map beforehand and using a lookup,
// because many messages don't contain any markers, and in that case the upfront cost is saved.
for (const script of enabledScripts) {
if (script.id === scriptId) {
runScript(script);
break;
}
}
processedIndices.add(match.index);
}
}
// A partial marker at the end of a partial message might turn into a complete marker later,
// so we remove such partial markers during mid-streaming in order to prevent partial markers
// flickering in and out.
//
// Note that for maximum correctness, this removal must happen before the removal of complete
// markers below, because the latter might remove a marker at the end of the message, turning
// a non-terminal partial marker into a terminal one, causing incorrect removal of a partial
// marker that is guaranteed to never turn into a complete one.
if (!isFinal) {
text = text.replace(partialMarkerRegex, "");
}
text = text.replaceAll(markerRegex, "");
return originalOnProgressStreaming(messageId, text, isFinal);
}
streamingProcessor.onProgressStreaming = onProgressStreaming;
hookToBeInstalled = false;
}
}
eventSource.on(event_types.GENERATION_AFTER_COMMANDS, injectInstructions);
eventSource.on(event_types.GENERATE_AFTER_DATA, restorePrompts);
// This is less than ideal because `STREAM_TOKEN_RECEIVED` is emitted for each token during generation
// even though we only need to install the hook once. Unfortunately, it is the *only* event emitted
// between the instantiation of the `StreamingProcessor` and the first call to `onProgressStreaming`,
// so we have no choice.
eventSource.on(event_types.STREAM_TOKEN_RECEIVED, installStreamHook);
function template(templateId, templateData = {}, sanitize = true, localize = true) {
return renderExtensionTemplateAsync(`third-party/${NAME}`, templateId, templateData, sanitize, localize);
}
// The top bar button is created too late to be covered by the standard event handlers.
// Unfortunately, the function `doNavbarIconClick` that is used to handle top bar clicks
// is not exported by `script.js`. This hack extracts the function from another button's
// event handler, and then attaches it to the extension button.
function clickHandlerHack() {
const element = document.querySelector("#extensions-settings-button .drawer-toggle");
const events = $._data(element, "events");
const doNavbarIconClick = events.click[0].handler;
$("#sorcery-button .drawer-toggle").on("click", doNavbarIconClick);
}
// Register a copy of the STscript language definition in order to avoid inheriting
// the ugly global styles from the Quick Reply extension. This is a ridiculous hack,
// but SillyTavern's complete lack of encapsulation makes it hard to find a clean solution.
hljs.registerLanguage("sorcery-stscript", () => hljs.getLanguage("stscript"));
$(async () => {
// The code-input library is neither a module with exports, nor does it add its symbols
// to any global objects. It must be loaded as a regular DOM script.
const libPath = `/scripts/extensions/third-party/${NAME}/lib`;
$("head").append(`<link rel="stylesheet" href="${libPath}/code-input.css">`);
$("head").append(`<script src="${libPath}/code-input.js"></script>`);
$("head").append(`<script>codeInput.registerTemplate("syntax-highlighted", codeInput.templates.hljs(hljs, []));</script>`);
const settingsHtml = await template("settings");
$("#extensions-settings-button").after(settingsHtml);
clickHandlerHack();
$("#sorcery-enabled").prop("checked", settings.enabled).change(function () {
settings.enabled = this.checked;
saveSettingsDebounced();
});
$("#sorcery-flash-icon").prop("checked", settings.flashIcon).change(function () {
settings.flashIcon = this.checked;
saveSettingsDebounced();
});
// Sanitization must be disabled because the template uses custom HTML elements,
// which DOMPurify removes.
const scriptHtml = await template("script", {}, false);
const scriptsElement = $("#sorcery-scripts");
function expandScriptElement(scriptElement) {
scriptElement.find(".inline-drawer-toggle").removeClass("down fa-circle-chevron-down").addClass("up fa-circle-chevron-up");
scriptElement.find(".inline-drawer-content").attr("style", "");
}
function focusConditionInput(scriptElement) {
scriptElement.find(".sorcery-condition textarea").trigger("focus");
}
function addScriptElement(script) {
const scriptElement = $(scriptHtml);
scriptsElement.prepend(scriptElement);
scriptElement.find(".sorcery-duplicate").click(() => {
const s = addScript(script);
const scriptElement = addScriptElement(s);
expandScriptElement(scriptElement);
focusConditionInput(scriptElement);
});
scriptElement.find(".sorcery-delete").click(() => {
settings.scripts = settings.scripts.filter(s => s.id !== script.id);
saveSettingsDebounced();
scriptElement.remove();
});
scriptElement.find(".sorcery-enabled").addClass(script.enabled ? "fa-toggle-on" : "fa-toggle-off").click(function () {
if ($(this).hasClass("fa-toggle-on")) {
$(this).removeClass("fa-toggle-on").addClass("fa-toggle-off");
script.enabled = false;
} else {
$(this).removeClass("fa-toggle-off").addClass("fa-toggle-on");
script.enabled = true;
}
saveSettingsDebounced();
});
scriptElement.find(".sorcery-condition").val(script.condition).on("input", function () {
script.condition = this.value;
saveSettingsDebounced();
});
scriptElement.find(".sorcery-stscript").val(script.stscript).on("input", function () {
script.stscript = this.value;
saveSettingsDebounced();
});
scriptElement.find(".sorcery-run-stscript").click(() => {
runSTscript(script.stscript);
});
scriptElement.find(".sorcery-javascript").val(script.javascript).on("input", function () {
script.javascript = this.value;
saveSettingsDebounced();
});
scriptElement.find(".sorcery-run-javascript").click(() => {
runJavaScript(script.javascript);
});
return scriptElement;
}
if (settings.scripts.length === 1) {
// Improve first-use experience by expanding the script editor if there is only one
// (such as with the default settings).
const scriptElement = addScriptElement(settings.scripts[0]);
expandScriptElement(scriptElement);
} else {
for (const script of settings.scripts) {
addScriptElement(script);
}
}
$("#sorcery-new").click(() => {
const script = addScript();
const scriptElement = addScriptElement(script);
expandScriptElement(scriptElement);
focusConditionInput(scriptElement);
});
});