-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
302 lines (257 loc) · 10.6 KB
/
index.js
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
import {
extension_settings,
getContext,
} from "../../../extensions.js";
import { saveSettingsDebounced,
setEditedMessageId,
generateQuietPrompt,
is_send_press,
substituteParamsExtended,
} from "../../../../script.js";
import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js';
import { SlashCommand } from '../../../slash-commands/SlashCommand.js';
import { getMessageTimeStamp } from '../../../RossAscends-mods.js';
import { MacrosParser } from '../../../macros.js';
import { is_group_generating, selected_group } from '../../../group-chats.js';
const extensionName = "Sillytavern-CYOA";
const extensionFolderPath = `scripts/extensions/third-party/${extensionName}`;
const defaultSettings = {
enabled: false,
llm_prompt: `Stop the roleplay now and provide a response with {{suggestionNumber}} brief distinct single-sentence suggestions for the next story beat on {{user}} perspective. Ensure each suggestion aligns with its corresponding description:
1. Eases tension and improves the protagonist's situation
2. Creates or increases tension and worsens the protagonist's situation
3. Leads directly but believably to a wild twist or super weird event
4. Slowly moves the story forward without ending the current scene
5. Pushes the story forward, potentially ending the current scene if feasible
Each suggestion surrounded by \`<suggestion>\` tags. E.g:
<suggestion>suggestion_1</suggestion>
<suggestion>suggestion_2</suggestion>
...
Do not include any other content in your response.`,
llm_prompt_impersonate: `[Event Direction for the next story beat on {{user}} perspective: \`{{suggestionText}}\`]
[Based on the expected events, write the user response]`,
apply_wi_an: true,
num_responses: 5,
response_length: 500,
};
let inApiCall = false;
/**
* Parses the CYOA response and returns the suggestions buttons
* @param {string} response
* @returns {string} text
*/
function parseResponse(response) {
const suggestions = [];
const regex = /<suggestion>(.+?)<\/suggestion>|Suggestion\s+\d+\s*:\s*(.+)|Suggestion_\d+\s*:\s*(.+)|^\d+\.\s*(.+)/gim;
let match;
while ((match = regex.exec(`${response}\n`)) !== null) {
const suggestion = match[1] || match[2] || match[3] || match[4];
if (suggestion && suggestion.trim()) {
suggestions.push(suggestion.trim());
}
}
if (suggestions.length === 0) {
return;
}
const newResponse = suggestions.map((suggestion) =>
`<div class="suggestion"><button class="suggestion">${suggestion}</button><button class="edit-suggestion fa-solid fa-pen-to-square"><span class="text">${suggestion}</span></button></div>`);
return `<div class=\"suggestions\">${newResponse.join("")}</div>`
}
async function waitForGeneration() {
try {
// Wait for group to finish generating
if (selected_group) {
await waitUntilCondition(() => is_group_generating === false, 1000, 10);
}
// Wait for the send button to be released
waitUntilCondition(() => is_send_press === false, 30000, 100);
} catch {
console.debug('Timeout waiting for is_send_press');
return;
}
}
/**
* Handles the CYOA response generation
* @returns
*/
async function requestCYOAResponses() {
const context = getContext();
const chat = context.chat;
// no characters or group selected
if (!context.groupId && context.characterId === undefined) {
return;
}
// Currently summarizing or frozen state - skip
if (inApiCall) {
return;
}
// No new messages - do nothing
// if (chat.length === 0 || (lastMessageId === chat.length && getStringHash(chat[chat.length - 1].mes) === lastMessageHash)) {
if (chat.length === 0) {
return;
}
removeLastCYOAMessage(chat);
await waitForGeneration();
toastr.info('CYOA: Generating response...');
const prompt = extension_settings.cyoa_responses?.llm_prompt || defaultSettings.llm_prompt || "";
const useWIAN = extension_settings.cyoa_responses?.apply_wi_an || defaultSettings.apply_wi_an;
const responseLength = extension_settings.cyoa_responses?.response_length || defaultSettings.response_length;
// generateQuietPrompt(quiet_prompt, quietToLoud, skipWIAN, quietImage = null, quietName = null, responseLength = null, noContext = false)
const response = await generateQuietPrompt(prompt, false, !useWIAN, null, "Suggestion List", responseLength);
const parsedResponse = parseResponse(response);
if (!parsedResponse) {
toastr.error('CYOA: Failed to parse response');
return;
}
await sendMessageToUI(parsedResponse);
}
/**
* Removes the last CYOA message from the chat
* @param {getContext.chat} chat
*/
function removeLastCYOAMessage(chat = getContext().chat) {
let lastMessage = chat[chat.length - 1];
if (!lastMessage?.extra || lastMessage?.extra?.model !== 'cyoa') {
return;
}
const target = $('#chat').find(`.mes[mesid=${lastMessage.mesId}]`);
if (target.length === 0) {
return;
}
setEditedMessageId(lastMessage.mesId);
target.find('.mes_edit_delete').trigger('click', { fromSlashCommand: true });
}
/**
* Sends the parsed CYOA response to the SillyTavern UI
* @param {string} parsedResponse
*/
async function sendMessageToUI(parsedResponse) {
const context = getContext();
const chat = context.chat;
const messageObject = {
name: "CYOA Suggestions",
is_user: true,
is_system: false,
send_date: getMessageTimeStamp(),
mes: `${parsedResponse}`,
mesId: context.chat.length,
extra: {
api: 'manual',
model: 'cyoa',
}
};
context.chat.push(messageObject);
// await eventSource.emit(event_types.MESSAGE_SENT, (chat.length - 1));
context.addOneMessage(messageObject, { showSwipes: false, forceId: chat.length - 1 });
}
/**
* Handles the CYOA click event by doing impersonation
* @param {*} event
*/
async function handleCYOABtn(event) {
const $button = $(event.target);
const text = $button?.text()?.trim() || $button.find('.custom-text')?.text()?.trim();
if (text.length === 0) {
return;
}
await waitForGeneration();
removeLastCYOAMessage();
// Sleep for 500ms before continuing
await new Promise(resolve => setTimeout(resolve, 250));
const inputTextarea = document.querySelector('#send_textarea');
if (!(inputTextarea instanceof HTMLTextAreaElement)) {
return;
}
let impersonatePrompt = extension_settings.cyoa_responses?.llm_prompt_impersonate || '';
impersonatePrompt = substituteParamsExtended(String(extension_settings.cyoa_responses?.llm_prompt_impersonate), { suggestionText: text });
const quiet_prompt = `/impersonate await=true ${impersonatePrompt}`;
inputTextarea.value = quiet_prompt;
if ($button.hasClass('custom-edit-suggestion')) {
return; // Stop here if it's the edit button
}
inputTextarea.dispatchEvent(new Event('input', { bubbles: true }));
const sendButton = document.querySelector('#send_but');
if (sendButton instanceof HTMLElement) {
sendButton.click();
}
}
/**
* Handles the CYOA by sending the text to the User Input box
* @param {*} event
*/
// function handleCYOAEditBtn(event) {
// const $button = $(event.target);
// const text = $button.find('.custom-text').text().trim();
// if (text.length === 0) {
// return;
// }
// removeLastCYOAMessage();
// const inputTextarea = document.querySelector('#send_textarea');
// if (inputTextarea instanceof HTMLTextAreaElement) {
// inputTextarea.value = text;
// }
// }
/**
* Settings Stuff
*/
function loadSettings() {
extension_settings.cyoa_responses = extension_settings.cyoa_responses || {};
if (Object.keys(extension_settings.cyoa_responses).length === 0) {
extension_settings.cyoa_responses = {};
}
Object.assign(defaultSettings, extension_settings.cyoa_responses);
$('#cyoa_llm_prompt').val(extension_settings.cyoa_responses.llm_prompt).trigger('input');
$('#cyoa_llm_prompt_impersonate').val(extension_settings.cyoa_responses.llm_prompt_impersonate).trigger('input');
$('#cyoa_apply_wi_an').prop('checked', extension_settings.cyoa_responses.apply_wi_an).trigger('input');
$('#cyoa_num_responses').val(extension_settings.cyoa_responses.num_responses).trigger('input');
$('#cyoa_num_responses_value').text(extension_settings.cyoa_responses.num_responses);
$('#cyoa_response_length').val(extension_settings.cyoa_responses.response_length).trigger('input');
$('#cyoa_response_length_value').text(extension_settings.cyoa_responses.response_length);
}
function addEventListeners() {
$('#cyoa_llm_prompt').on('input', function() {
extension_settings.cyoa_responses.llm_prompt = $(this).val();
saveSettingsDebounced();
});
$('#cyoa_llm_prompt_impersonate').on('input', function() {
extension_settings.cyoa_responses.llm_prompt_impersonate = $(this).val();
saveSettingsDebounced();
});
$('#cyoa_apply_wi_an').on('change', function() {
extension_settings.cyoa_responses.apply_wi_an = !!$(this).prop('checked');
saveSettingsDebounced();
});
$('#cyoa_num_responses').on('input', function() {
const value = $(this).val();
extension_settings.cyoa_responses.num_responses = Number(value);
$('#cyoa_num_responses_value').text(value);
saveSettingsDebounced();
});
$('#cyoa_response_length').on('input', function() {
const value = $(this).val();
extension_settings.cyoa_responses.response_length = Number(value);
$('#cyoa_response_length_value').text(value);
saveSettingsDebounced();
});
}
// This function is called when the extension is loaded
jQuery(async () => {
//add a delay to possibly fix some conflicts
await new Promise(resolve => setTimeout(resolve, 900));
const settingsHtml = await $.get(`${extensionFolderPath}/settings.html`);
$("#extensions_settings").append(settingsHtml);
loadSettings();
addEventListeners();
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'cyoa',
callback: async () => {
await requestCYOAResponses();
return '';
},
helpString: 'Triggers CYOA responses generation.',
}));
MacrosParser.registerMacro('suggestionNumber', () => `${extension_settings.cyoa_responses?.num_responses || defaultSettings.num_responses}`);
// Event delegation for CYOA buttons
$(document).on('click', 'button.custom-edit-suggestion', handleCYOABtn);
$(document).on('click', 'button.custom-suggestion', handleCYOABtn);
});