').addClass('acc-avatar');
+ if (role === 'user') {
+ avatarDiv.html('
').addClass('acc-message-content');
+
+ msgDiv.append(avatarDiv);
+ msgDiv.append(contentDiv);
+ stream.append(msgDiv);
+
+ if (role === 'assistant') {
+ let i = 0;
+ const speed = 2;
+ const chunkSize = 5;
+
+ function typeWriter() {
+ if (i < formattedContent.length) {
+ let chunk = "";
+ let count = 0;
+
+ while (count < chunkSize && i < formattedContent.length) {
+ if (formattedContent.charAt(i) === '<') {
+ const tagEnd = formattedContent.indexOf('>', i);
+ if (tagEnd !== -1) {
+ chunk += formattedContent.substring(i, tagEnd + 1);
+ i = tagEnd + 1;
+ } else {
+ chunk += formattedContent.charAt(i);
+ i++;
+ }
+ } else {
+ chunk += formattedContent.charAt(i);
+ i++;
+ }
+ count++;
+ }
+
+ contentDiv.html(contentDiv.html() + chunk);
+ stream.scrollTop(stream[0].scrollHeight);
+ setTimeout(typeWriter, speed);
+ }
+ }
+ typeWriter();
+ } else {
+ contentDiv.html(formattedContent);
+ stream.scrollTop(stream[0].scrollHeight);
+ }
+}
+
+async function updatePreview(toolName, args) {
+ const container = $('#acc-preview-container');
+
+ if (toolName === 'update_character_card' || toolName === 'edit_character_text') {
+ const chid = args.chid !== undefined ? args.chid : $('#acc-target-char').val();
+ if (chid !== undefined) {
+ const charData = await tools.read_character_card({ chid });
+ const char = JSON.parse(charData);
+
+ let html = `
角色预览: ${char.name}
`;
+
+ const fields = ['description', 'personality', 'first_mes', 'scenario'];
+ fields.forEach(field => {
+ const oldVal = previousCharData[field] || '';
+ const newVal = char[field] || '';
+ let contentHtml = newVal;
+
+ if (oldVal !== newVal) {
+ contentHtml = `
${newVal}
`;
+ if (oldVal) {
+ contentHtml += `
${oldVal}
`;
+ }
+ }
+
+ html += `
`;
+ });
+
+ container.html(html);
+ previousCharData = char;
+ }
+ } else if (toolName === 'write_world_info_entry') {
+ const bookName = args.book_name || $('#acc-target-world').val();
+ if (bookName) {
+ const entriesData = await tools.read_world_info({ book_name: bookName });
+ const entries = JSON.parse(entriesData);
+
+ let html = `
世界书预览: ${bookName}
`;
+ entries.forEach(entry => {
+
+ let isModified = false;
+ if (args.entries) {
+ const modifiedEntries = Array.isArray(args.entries) ? args.entries : [args.entries];
+ isModified = modifiedEntries.some(e => e.key === entry.key || (Array.isArray(entry.keys) && entry.keys.includes(e.key)));
+ }
+
+ const contentClass = isModified ? 'diff-added' : '';
+
+ html += `
+
Key: ${Array.isArray(entry.keys) ? entry.keys.join(', ') : entry.key}
+
Content:${entry.content}
+
`;
+ });
+
+ container.html(html);
+ }
+ }
+}