mirror of
https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 17:25:51 +00:00
Add files via upload
This commit is contained in:
@@ -1,24 +1,91 @@
|
||||
import { characters, saveCharacterDebounced, this_chid, getCharacters, getRequestHeaders } from "/script.js";
|
||||
import { getContext } from "/scripts/extensions.js";
|
||||
import { extensionName } from "../../utils/settings.js";
|
||||
|
||||
async function saveCharacterById(chid) {
|
||||
const char = characters[chid];
|
||||
if (!char) return;
|
||||
let currentChid = undefined;
|
||||
|
||||
|
||||
try {
|
||||
const context = getContext();
|
||||
if (context) currentChid = context.characterId;
|
||||
} catch (e) {}
|
||||
|
||||
|
||||
if (currentChid === undefined) currentChid = this_chid;
|
||||
|
||||
|
||||
if (currentChid === undefined && typeof window !== 'undefined' && window.this_chid !== undefined) {
|
||||
currentChid = window.this_chid;
|
||||
}
|
||||
|
||||
|
||||
if (currentChid === undefined && typeof $ !== 'undefined') {
|
||||
|
||||
const selected = $('.character_select.selected, .character-list-item.selected');
|
||||
if (selected.length) {
|
||||
currentChid = selected.attr('chid');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (typeof saveCharacterDebounced === 'function') {
|
||||
|
||||
|
||||
|
||||
if (currentChid === undefined || chid == currentChid) {
|
||||
saveCharacterDebounced();
|
||||
console.log(`[Amily2 CharAPI] Triggered saveCharacterDebounced for character ${chid} (Detected: ${currentChid})`);
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('avatar_url', char.avatar);
|
||||
formData.append('ch_name', char.name);
|
||||
formData.append('description', char.description || '');
|
||||
formData.append('personality', char.personality || '');
|
||||
formData.append('scenario', char.scenario || '');
|
||||
formData.append('first_mes', char.first_mes || '');
|
||||
formData.append('mes_example', char.mes_example || '');
|
||||
formData.append('creator', char.creator || '');
|
||||
formData.append('creator_notes', char.creator_notes || '');
|
||||
formData.append('tags', Array.isArray(char.tags) ? char.tags.join(',') : (char.tags || ''));
|
||||
formData.append('talkativeness', char.talkativeness || '0.5');
|
||||
formData.append('fav', char.fav || 'false');
|
||||
|
||||
if (char.data) {
|
||||
formData.append('extensions', JSON.stringify(char.data));
|
||||
}
|
||||
|
||||
|
||||
if (char.data && Array.isArray(char.data.alternate_greetings)) {
|
||||
for (const value of char.data.alternate_greetings) {
|
||||
formData.append('alternate_greetings', value);
|
||||
}
|
||||
}
|
||||
|
||||
const response = await fetch('/api/characters/edit', {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
body: JSON.stringify(char)
|
||||
headers: getRequestHeaders({ omitContentType: true }),
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`[Amily2 CharAPI] Failed to save character ${chid}:`, response.statusText);
|
||||
const errorText = await response.text();
|
||||
console.error(`[Amily2 CharAPI] Failed to save character ${chid}:`, response.statusText, errorText);
|
||||
return { success: false, message: `Save failed: ${response.statusText}` };
|
||||
} else {
|
||||
console.log(`[Amily2 CharAPI] Successfully saved character ${chid}`);
|
||||
console.log(`[Amily2 CharAPI] Successfully saved character ${chid} (Background)`);
|
||||
return { success: true };
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`[Amily2 CharAPI] Error saving character ${chid}:`, e);
|
||||
return { success: false, message: `Save error: ${e.message}` };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +97,7 @@ export function getCharacter(chid = this_chid) {
|
||||
return characters[chid];
|
||||
}
|
||||
|
||||
export function updateCharacter(chid, updates) {
|
||||
export async function updateCharacter(chid, updates) {
|
||||
const char = getCharacter(chid);
|
||||
if (!char) return false;
|
||||
|
||||
@@ -45,9 +112,12 @@ export function updateCharacter(chid, updates) {
|
||||
});
|
||||
|
||||
if (changed) {
|
||||
saveCharacterById(chid);
|
||||
console.log(`[Amily2 CharAPI] Updated character ${chid}:`, Object.keys(updates));
|
||||
return true;
|
||||
const success = await saveCharacterById(chid);
|
||||
if (success) {
|
||||
console.log(`[Amily2 CharAPI] Updated character ${chid}:`, Object.keys(updates));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -63,7 +133,7 @@ export function getFirstMessages(chid) {
|
||||
return messages;
|
||||
}
|
||||
|
||||
export function addFirstMessage(chid, message) {
|
||||
export async function addFirstMessage(chid, message) {
|
||||
const char = getCharacter(chid);
|
||||
if (!char) return false;
|
||||
|
||||
@@ -73,12 +143,15 @@ export function addFirstMessage(chid, message) {
|
||||
}
|
||||
|
||||
char.data.alternate_greetings.push(message);
|
||||
saveCharacterById(chid);
|
||||
console.log(`[Amily2 CharAPI] Added alternate greeting to character ${chid}`);
|
||||
return true;
|
||||
const success = await saveCharacterById(chid);
|
||||
if (success) {
|
||||
console.log(`[Amily2 CharAPI] Added alternate greeting to character ${chid}`);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function updateFirstMessage(chid, index, message) {
|
||||
export async function updateFirstMessage(chid, index, message) {
|
||||
const char = getCharacter(chid);
|
||||
if (!char) return false;
|
||||
|
||||
@@ -93,12 +166,15 @@ export function updateFirstMessage(chid, index, message) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
saveCharacterById(chid);
|
||||
console.log(`[Amily2 CharAPI] Updated greeting ${index} for character ${chid}`);
|
||||
return true;
|
||||
const success = await saveCharacterById(chid);
|
||||
if (success) {
|
||||
console.log(`[Amily2 CharAPI] Updated greeting ${index} for character ${chid}`);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function removeFirstMessage(chid, index) {
|
||||
export async function removeFirstMessage(chid, index) {
|
||||
const char = getCharacter(chid);
|
||||
if (!char) return false;
|
||||
|
||||
@@ -114,9 +190,12 @@ export function removeFirstMessage(chid, index) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
saveCharacterById(chid);
|
||||
console.log(`[Amily2 CharAPI] Removed greeting ${index} for character ${chid}`);
|
||||
return true;
|
||||
const success = await saveCharacterById(chid);
|
||||
if (success) {
|
||||
console.log(`[Amily2 CharAPI] Removed greeting ${index} for character ${chid}`);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function createNewCharacter(name) {
|
||||
|
||||
Reference in New Issue
Block a user