revert: Revert Refactor function getCharImage
This commit is contained in:
@@ -1,499 +1,471 @@
|
|||||||
import { get, writable } from "svelte/store";
|
import { get, writable } from "svelte/store";
|
||||||
import {
|
import { DataBase, saveImage, setDatabase, type character, type Chat, defaultSdDataFunc } from "./database";
|
||||||
DataBase,
|
import exifr from 'exifr'
|
||||||
saveImage,
|
import { alertConfirm, alertError, alertNormal, alertSelect, alertStore } from "./alert";
|
||||||
setDatabase,
|
|
||||||
type character,
|
|
||||||
type Chat,
|
|
||||||
defaultSdDataFunc,
|
|
||||||
} from "./database";
|
|
||||||
import exifr from "exifr";
|
|
||||||
import {
|
|
||||||
alertConfirm,
|
|
||||||
alertError,
|
|
||||||
alertNormal,
|
|
||||||
alertSelect,
|
|
||||||
alertStore,
|
|
||||||
} from "./alert";
|
|
||||||
import { language } from "../lang";
|
import { language } from "../lang";
|
||||||
import { PngMetadata } from "./exif";
|
import { PngMetadata } from "./exif";
|
||||||
import {
|
import { encode as encodeMsgpack, decode as decodeMsgpack } from "@msgpack/msgpack";
|
||||||
encode as encodeMsgpack,
|
import { checkNullish, findCharacterbyId, selectMultipleFile, selectSingleFile, sleep } from "./util";
|
||||||
decode as decodeMsgpack,
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
} from "@msgpack/msgpack";
|
|
||||||
import {
|
|
||||||
checkNullish,
|
|
||||||
findCharacterbyId,
|
|
||||||
selectMultipleFile,
|
|
||||||
selectSingleFile,
|
|
||||||
sleep,
|
|
||||||
} from "./util";
|
|
||||||
import { v4 as uuidv4 } from "uuid";
|
|
||||||
import { selectedCharID } from "./stores";
|
import { selectedCharID } from "./stores";
|
||||||
import { downloadFile, getFileSrc, readImage } from "./globalApi";
|
import { downloadFile, getFileSrc, readImage } from "./globalApi";
|
||||||
|
|
||||||
export function createNewCharacter() {
|
export function createNewCharacter() {
|
||||||
let db = get(DataBase);
|
let db = get(DataBase)
|
||||||
db.characters.push(createBlankChar());
|
db.characters.push(createBlankChar())
|
||||||
setDatabase(db);
|
setDatabase(db)
|
||||||
return db.characters.length - 1;
|
return db.characters.length - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createNewGroup() {
|
export function createNewGroup(){
|
||||||
let db = get(DataBase);
|
let db = get(DataBase)
|
||||||
db.characters.push({
|
db.characters.push({
|
||||||
type: "group",
|
type: 'group',
|
||||||
name: "",
|
name: "",
|
||||||
firstMessage: "",
|
firstMessage: "",
|
||||||
chats: [
|
chats: [{
|
||||||
{
|
message: [],
|
||||||
message: [],
|
note: '',
|
||||||
note: "",
|
name: 'Chat 1',
|
||||||
name: "Chat 1",
|
localLore: []
|
||||||
localLore: [],
|
}], chatPage: 0,
|
||||||
},
|
viewScreen: 'none',
|
||||||
],
|
globalLore: [],
|
||||||
chatPage: 0,
|
characters: [],
|
||||||
viewScreen: "none",
|
autoMode: false,
|
||||||
globalLore: [],
|
useCharacterLore: true,
|
||||||
characters: [],
|
emotionImages: [],
|
||||||
autoMode: false,
|
customscript: [],
|
||||||
useCharacterLore: true,
|
chaId: uuidv4(),
|
||||||
emotionImages: [],
|
firstMsgIndex: -1
|
||||||
customscript: [],
|
})
|
||||||
chaId: uuidv4(),
|
setDatabase(db)
|
||||||
firstMsgIndex: -1,
|
return db.characters.length - 1
|
||||||
});
|
|
||||||
setDatabase(db);
|
|
||||||
return db.characters.length - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCharImage(
|
export async function getCharImage(loc:string, type:'plain'|'css'|'contain') {
|
||||||
loc: string,
|
if(!loc || loc === ''){
|
||||||
type: "plain" | "css" | "contain" // TODO: REFACTOR BY REMOVE THIS PARAMETER
|
if(type ==='css'){
|
||||||
) {
|
return ''
|
||||||
const filesrc = await getFileSrc(loc);
|
}
|
||||||
return filesrc;
|
return null
|
||||||
|
}
|
||||||
|
const filesrc = await getFileSrc(loc)
|
||||||
|
if(type === 'plain'){
|
||||||
|
return filesrc
|
||||||
|
}
|
||||||
|
else if(type ==='css'){
|
||||||
|
return `background: url("${filesrc}");background-size: cover;`
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return `background: url("${filesrc}");background-size: contain;background-repeat: no-repeat;background-position: center;`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function selectCharImg(charId: number) {
|
export async function selectCharImg(charId:number) {
|
||||||
const selected = await selectSingleFile(["png"]);
|
const selected = await selectSingleFile(['png'])
|
||||||
if (!selected) {
|
if(!selected){
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
const img = selected.data;
|
const img = selected.data
|
||||||
let db = get(DataBase);
|
let db = get(DataBase)
|
||||||
const imgp = await saveImage(img);
|
const imgp = await saveImage(img)
|
||||||
db.characters[charId].image = imgp;
|
db.characters[charId].image = imgp
|
||||||
setDatabase(db);
|
setDatabase(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function selectUserImg() {
|
export async function selectUserImg() {
|
||||||
const selected = await selectSingleFile(["png"]);
|
const selected = await selectSingleFile(['png'])
|
||||||
if (!selected) {
|
if(!selected){
|
||||||
return;
|
return
|
||||||
}
|
|
||||||
const img = selected.data;
|
|
||||||
let db = get(DataBase);
|
|
||||||
const imgp = await saveImage(img);
|
|
||||||
db.userIcon = imgp;
|
|
||||||
setDatabase(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const addingEmotion = writable(false);
|
|
||||||
|
|
||||||
export async function addCharEmotion(charId: number) {
|
|
||||||
addingEmotion.set(true);
|
|
||||||
const selected = await selectMultipleFile(["png", "webp", "gif"]);
|
|
||||||
if (!selected) {
|
|
||||||
addingEmotion.set(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let db = get(DataBase);
|
|
||||||
for (const f of selected) {
|
|
||||||
console.log(f);
|
|
||||||
const img = f.data;
|
|
||||||
const imgp = await saveImage(img);
|
|
||||||
const name = f.name.replace(".png", "").replace(".webp", "");
|
|
||||||
let dbChar = db.characters[charId];
|
|
||||||
if (dbChar.type !== "group") {
|
|
||||||
dbChar.emotionImages.push([name, imgp]);
|
|
||||||
db.characters[charId] = dbChar;
|
|
||||||
}
|
}
|
||||||
setDatabase(db);
|
const img = selected.data
|
||||||
}
|
let db = get(DataBase)
|
||||||
addingEmotion.set(false);
|
const imgp = await saveImage(img)
|
||||||
|
db.userIcon = imgp
|
||||||
|
setDatabase(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function rmCharEmotion(charId: number, emotionId: number) {
|
export const addingEmotion = writable(false)
|
||||||
let db = get(DataBase);
|
|
||||||
let dbChar = db.characters[charId];
|
|
||||||
if (dbChar.type !== "group") {
|
|
||||||
dbChar.emotionImages.splice(emotionId, 1);
|
|
||||||
db.characters[charId] = dbChar;
|
|
||||||
}
|
|
||||||
setDatabase(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function exportChat(page: number) {
|
export async function addCharEmotion(charId:number) {
|
||||||
try {
|
addingEmotion.set(true)
|
||||||
const mode = await alertSelect(["Export as JSON", "Export as TXT"]);
|
const selected = await selectMultipleFile(['png', 'webp', 'gif'])
|
||||||
const selectedID = get(selectedCharID);
|
if(!selected){
|
||||||
const db = get(DataBase);
|
addingEmotion.set(false)
|
||||||
const chat = db.characters[selectedID].chats[page];
|
return
|
||||||
const char = db.characters[selectedID];
|
|
||||||
const date = new Date().toJSON();
|
|
||||||
console.log(mode);
|
|
||||||
if (mode === "0") {
|
|
||||||
const stringl = Buffer.from(
|
|
||||||
JSON.stringify({
|
|
||||||
type: "risuChat",
|
|
||||||
ver: 1,
|
|
||||||
data: chat,
|
|
||||||
}),
|
|
||||||
"utf-8"
|
|
||||||
);
|
|
||||||
|
|
||||||
await downloadFile(
|
|
||||||
`${char.name}_${date}_chat`.replace(/[<>:"/\\|?*\.\,]/g, "") + ".json",
|
|
||||||
stringl
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
let stringl = chat.message
|
|
||||||
.map((v) => {
|
|
||||||
if (v.saying) {
|
|
||||||
return `${findCharacterbyId(v.saying).name}\n${v.data}`;
|
|
||||||
} else {
|
|
||||||
return `${v.role === "char" ? char.name : db.username}\n${v.data}`;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.join("\n\n");
|
|
||||||
|
|
||||||
if (char.type !== "group") {
|
|
||||||
stringl = `${char.name}\n${char.firstMessage}\n\n` + stringl;
|
|
||||||
}
|
|
||||||
|
|
||||||
await downloadFile(
|
|
||||||
`${char.name}_${date}_chat`.replace(/[<>:"/\\|?*\.\,]/g, "") + ".txt",
|
|
||||||
Buffer.from(stringl, "utf-8")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
alertNormal(language.successExport);
|
let db = get(DataBase)
|
||||||
} catch (error) {
|
for(const f of selected){
|
||||||
alertError(`${error}`);
|
console.log(f)
|
||||||
}
|
const img = f.data
|
||||||
|
const imgp = await saveImage(img)
|
||||||
|
const name = f.name.replace('.png','').replace('.webp','')
|
||||||
|
let dbChar = db.characters[charId]
|
||||||
|
if(dbChar.type !== 'group'){
|
||||||
|
dbChar.emotionImages.push([name,imgp])
|
||||||
|
db.characters[charId] = dbChar
|
||||||
|
}
|
||||||
|
setDatabase(db)
|
||||||
|
}
|
||||||
|
addingEmotion.set(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function importChat() {
|
export async function rmCharEmotion(charId:number, emotionId:number) {
|
||||||
const dat = await selectSingleFile(["json", "jsonl"]);
|
let db = get(DataBase)
|
||||||
if (!dat) {
|
let dbChar = db.characters[charId]
|
||||||
return;
|
if(dbChar.type !== 'group'){
|
||||||
}
|
dbChar.emotionImages.splice(emotionId, 1)
|
||||||
try {
|
db.characters[charId] = dbChar
|
||||||
const selectedID = get(selectedCharID);
|
}
|
||||||
let db = get(DataBase);
|
setDatabase(db)
|
||||||
|
}
|
||||||
|
|
||||||
if (dat.name.endsWith("jsonl")) {
|
|
||||||
const lines = Buffer.from(dat.data).toString("utf-8").split("\n");
|
|
||||||
let newChat: Chat = {
|
|
||||||
message: [],
|
|
||||||
note: "",
|
|
||||||
name: "Imported Chat",
|
|
||||||
localLore: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
let isFirst = true;
|
export async function exportChat(page:number){
|
||||||
for (const line of lines) {
|
try {
|
||||||
const presedLine = JSON.parse(line);
|
|
||||||
if ((presedLine.name && presedLine.is_user, presedLine.mes)) {
|
const mode = await alertSelect(['Export as JSON', "Export as TXT"])
|
||||||
if (!isFirst) {
|
const selectedID = get(selectedCharID)
|
||||||
newChat.message.push({
|
const db = get(DataBase)
|
||||||
role: presedLine.is_user ? "user" : "char",
|
const chat = db.characters[selectedID].chats[page]
|
||||||
data: formatTavernChat(
|
const char = db.characters[selectedID]
|
||||||
presedLine.mes,
|
const date = new Date().toJSON();
|
||||||
db.characters[selectedID].name
|
console.log(mode)
|
||||||
),
|
if(mode === '0'){
|
||||||
});
|
const stringl = Buffer.from(JSON.stringify({
|
||||||
}
|
type: 'risuChat',
|
||||||
|
ver: 1,
|
||||||
|
data: chat
|
||||||
|
}), 'utf-8')
|
||||||
|
|
||||||
|
await downloadFile(`${char.name}_${date}_chat`.replace(/[<>:"/\\|?*\.\,]/g, "") + '.json', stringl)
|
||||||
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
|
||||||
|
let stringl = chat.message.map((v) => {
|
||||||
|
if(v.saying){
|
||||||
|
return `${findCharacterbyId(v.saying).name}\n${v.data}`
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return `${v.role === 'char' ? char.name : db.username}\n${v.data}`
|
||||||
|
}
|
||||||
|
}).join('\n\n')
|
||||||
|
|
||||||
|
if(char.type !== 'group'){
|
||||||
|
stringl = `${char.name}\n${char.firstMessage}\n\n` + stringl
|
||||||
|
}
|
||||||
|
|
||||||
|
await downloadFile(`${char.name}_${date}_chat`.replace(/[<>:"/\\|?*\.\,]/g, "") + '.txt', Buffer.from(stringl, 'utf-8'))
|
||||||
|
|
||||||
|
}
|
||||||
|
alertNormal(language.successExport)
|
||||||
|
} catch (error) {
|
||||||
|
alertError(`${error}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function importChat(){
|
||||||
|
const dat =await selectSingleFile(['json','jsonl'])
|
||||||
|
if(!dat){
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const selectedID = get(selectedCharID)
|
||||||
|
let db = get(DataBase)
|
||||||
|
|
||||||
|
if(dat.name.endsWith('jsonl')){
|
||||||
|
const lines = Buffer.from(dat.data).toString('utf-8').split('\n')
|
||||||
|
let newChat:Chat = {
|
||||||
|
message: [],
|
||||||
|
note: "",
|
||||||
|
name: "Imported Chat",
|
||||||
|
localLore: []
|
||||||
|
}
|
||||||
|
|
||||||
|
let isFirst = true
|
||||||
|
for(const line of lines){
|
||||||
|
|
||||||
|
const presedLine = JSON.parse(line)
|
||||||
|
if(presedLine.name && presedLine.is_user, presedLine.mes){
|
||||||
|
if(!isFirst){
|
||||||
|
newChat.message.push({
|
||||||
|
role: presedLine.is_user ? "user" : 'char',
|
||||||
|
data: formatTavernChat(presedLine.mes, db.characters[selectedID].name)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isFirst = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if(newChat.message.length === 0){
|
||||||
|
alertError(language.errors.noData)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db.characters[selectedID].chats.push(newChat)
|
||||||
|
setDatabase(db)
|
||||||
|
alertNormal(language.successImport)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
const json = JSON.parse(Buffer.from(dat.data).toString('utf-8'))
|
||||||
|
if(json.type === 'risuChat' && json.ver === 1){
|
||||||
|
const das:Chat = json.data
|
||||||
|
if(!(checkNullish(das.message) || checkNullish(das.note) || checkNullish(das.name) || checkNullish(das.localLore))){
|
||||||
|
db.characters[selectedID].chats.push(das)
|
||||||
|
setDatabase(db)
|
||||||
|
alertNormal(language.successImport)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
alertError(language.errors.noData)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
alertError(language.errors.noData)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isFirst = false;
|
} catch (error) {
|
||||||
}
|
alertError(`${error}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (newChat.message.length === 0) {
|
function formatTavernChat(chat:string, charName:string){
|
||||||
alertError(language.errors.noData);
|
const db = get(DataBase)
|
||||||
return;
|
return chat.replace(/<([Uu]ser)>|\{\{([Uu]ser)\}\}/g, db.username).replace(/((\{\{)|<)([Cc]har)(=.+)?((\}\})|>)/g, charName)
|
||||||
}
|
}
|
||||||
|
|
||||||
db.characters[selectedID].chats.push(newChat);
|
export function characterFormatUpdate(index:number|character){
|
||||||
setDatabase(db);
|
let db = get(DataBase)
|
||||||
alertNormal(language.successImport);
|
let cha = typeof(index) === 'number' ? db.characters[index] : index
|
||||||
} else {
|
if(cha.chats.length === 0){
|
||||||
const json = JSON.parse(Buffer.from(dat.data).toString("utf-8"));
|
cha.chats = [{
|
||||||
if (json.type === "risuChat" && json.ver === 1) {
|
message: [],
|
||||||
const das: Chat = json.data;
|
note: '',
|
||||||
if (
|
name: 'Chat 1',
|
||||||
!(
|
localLore: []
|
||||||
checkNullish(das.message) ||
|
}]
|
||||||
checkNullish(das.note) ||
|
}
|
||||||
checkNullish(das.name) ||
|
if(!cha.chats[cha.chatPage]){
|
||||||
checkNullish(das.localLore)
|
cha.chatPage = 0
|
||||||
)
|
}
|
||||||
) {
|
if(!cha.chats[cha.chatPage].message){
|
||||||
db.characters[selectedID].chats.push(das);
|
cha.chats[cha.chatPage].message = []
|
||||||
setDatabase(db);
|
}
|
||||||
alertNormal(language.successImport);
|
if(!cha.type){
|
||||||
return;
|
cha.type = 'character'
|
||||||
} else {
|
}
|
||||||
alertError(language.errors.noData);
|
if(!cha.chaId){
|
||||||
return;
|
cha.chaId = uuidv4()
|
||||||
|
}
|
||||||
|
if(cha.type !== 'group'){
|
||||||
|
if(checkNullish(cha.sdData)){
|
||||||
|
cha.sdData = defaultSdDataFunc()
|
||||||
}
|
}
|
||||||
} else {
|
if(checkNullish(cha.utilityBot)){
|
||||||
alertError(language.errors.noData);
|
cha.utilityBot = false
|
||||||
return;
|
}
|
||||||
}
|
cha.alternateGreetings = cha.alternateGreetings ?? []
|
||||||
|
cha.exampleMessage = cha.exampleMessage ?? ''
|
||||||
|
cha.creatorNotes = cha.creatorNotes ?? ''
|
||||||
|
cha.systemPrompt = cha.systemPrompt ?? ''
|
||||||
|
cha.postHistoryInstructions = cha.postHistoryInstructions ?? ''
|
||||||
|
cha.tags = cha.tags ?? []
|
||||||
|
cha.creator = cha.creator ?? ''
|
||||||
|
cha.characterVersion = cha.characterVersion ?? 0
|
||||||
|
cha.personality = cha.personality ?? ''
|
||||||
|
cha.scenario = cha.scenario ?? ''
|
||||||
|
cha.firstMsgIndex = cha.firstMsgIndex ?? -1
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
if(checkNullish(cha.customscript)){
|
||||||
alertError(`${error}`);
|
cha.customscript = []
|
||||||
}
|
}
|
||||||
|
if(typeof(index) === 'number'){
|
||||||
|
db.characters[index] = cha
|
||||||
|
setDatabase(db)
|
||||||
|
}
|
||||||
|
return cha
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatTavernChat(chat: string, charName: string) {
|
|
||||||
const db = get(DataBase);
|
export function createBlankChar():character{
|
||||||
return chat
|
return {
|
||||||
.replace(/<([Uu]ser)>|\{\{([Uu]ser)\}\}/g, db.username)
|
name: '',
|
||||||
.replace(/((\{\{)|<)([Cc]har)(=.+)?((\}\})|>)/g, charName);
|
firstMessage: '',
|
||||||
|
desc: '',
|
||||||
|
notes: '',
|
||||||
|
chats: [{
|
||||||
|
message: [],
|
||||||
|
note: '',
|
||||||
|
name: 'Chat 1',
|
||||||
|
localLore: []
|
||||||
|
}],
|
||||||
|
chatPage: 0,
|
||||||
|
emotionImages: [],
|
||||||
|
bias: [],
|
||||||
|
viewScreen: 'none',
|
||||||
|
globalLore: [],
|
||||||
|
chaId: uuidv4(),
|
||||||
|
type: 'character',
|
||||||
|
sdData: defaultSdDataFunc(),
|
||||||
|
utilityBot: false,
|
||||||
|
customscript: [],
|
||||||
|
exampleMessage: '',
|
||||||
|
creatorNotes:'',
|
||||||
|
systemPrompt:'',
|
||||||
|
postHistoryInstructions:'',
|
||||||
|
alternateGreetings:[],
|
||||||
|
tags:[],
|
||||||
|
creator:"",
|
||||||
|
characterVersion: 0,
|
||||||
|
personality:"",
|
||||||
|
scenario:"",
|
||||||
|
firstMsgIndex: -1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function characterFormatUpdate(index: number | character) {
|
|
||||||
let db = get(DataBase);
|
|
||||||
let cha = typeof index === "number" ? db.characters[index] : index;
|
|
||||||
if (cha.chats.length === 0) {
|
|
||||||
cha.chats = [
|
|
||||||
{
|
|
||||||
message: [],
|
|
||||||
note: "",
|
|
||||||
name: "Chat 1",
|
|
||||||
localLore: [],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
if (!cha.chats[cha.chatPage]) {
|
|
||||||
cha.chatPage = 0;
|
|
||||||
}
|
|
||||||
if (!cha.chats[cha.chatPage].message) {
|
|
||||||
cha.chats[cha.chatPage].message = [];
|
|
||||||
}
|
|
||||||
if (!cha.type) {
|
|
||||||
cha.type = "character";
|
|
||||||
}
|
|
||||||
if (!cha.chaId) {
|
|
||||||
cha.chaId = uuidv4();
|
|
||||||
}
|
|
||||||
if (cha.type !== "group") {
|
|
||||||
if (checkNullish(cha.sdData)) {
|
|
||||||
cha.sdData = defaultSdDataFunc();
|
|
||||||
}
|
|
||||||
if (checkNullish(cha.utilityBot)) {
|
|
||||||
cha.utilityBot = false;
|
|
||||||
}
|
|
||||||
cha.alternateGreetings = cha.alternateGreetings ?? [];
|
|
||||||
cha.exampleMessage = cha.exampleMessage ?? "";
|
|
||||||
cha.creatorNotes = cha.creatorNotes ?? "";
|
|
||||||
cha.systemPrompt = cha.systemPrompt ?? "";
|
|
||||||
cha.postHistoryInstructions = cha.postHistoryInstructions ?? "";
|
|
||||||
cha.tags = cha.tags ?? [];
|
|
||||||
cha.creator = cha.creator ?? "";
|
|
||||||
cha.characterVersion = cha.characterVersion ?? 0;
|
|
||||||
cha.personality = cha.personality ?? "";
|
|
||||||
cha.scenario = cha.scenario ?? "";
|
|
||||||
cha.firstMsgIndex = cha.firstMsgIndex ?? -1;
|
|
||||||
}
|
|
||||||
if (checkNullish(cha.customscript)) {
|
|
||||||
cha.customscript = [];
|
|
||||||
}
|
|
||||||
if (typeof index === "number") {
|
|
||||||
db.characters[index] = cha;
|
|
||||||
setDatabase(db);
|
|
||||||
}
|
|
||||||
return cha;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createBlankChar(): character {
|
|
||||||
return {
|
|
||||||
name: "",
|
|
||||||
firstMessage: "",
|
|
||||||
desc: "",
|
|
||||||
notes: "",
|
|
||||||
chats: [
|
|
||||||
{
|
|
||||||
message: [],
|
|
||||||
note: "",
|
|
||||||
name: "Chat 1",
|
|
||||||
localLore: [],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
chatPage: 0,
|
|
||||||
emotionImages: [],
|
|
||||||
bias: [],
|
|
||||||
viewScreen: "none",
|
|
||||||
globalLore: [],
|
|
||||||
chaId: uuidv4(),
|
|
||||||
type: "character",
|
|
||||||
sdData: defaultSdDataFunc(),
|
|
||||||
utilityBot: false,
|
|
||||||
customscript: [],
|
|
||||||
exampleMessage: "",
|
|
||||||
creatorNotes: "",
|
|
||||||
systemPrompt: "",
|
|
||||||
postHistoryInstructions: "",
|
|
||||||
alternateGreetings: [],
|
|
||||||
tags: [],
|
|
||||||
creator: "",
|
|
||||||
characterVersion: 0,
|
|
||||||
personality: "",
|
|
||||||
scenario: "",
|
|
||||||
firstMsgIndex: -1,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function makeGroupImage() {
|
export async function makeGroupImage() {
|
||||||
try {
|
try {
|
||||||
alertStore.set({
|
alertStore.set({
|
||||||
type: "wait",
|
type: 'wait',
|
||||||
msg: `Loading..`,
|
msg: `Loading..`
|
||||||
});
|
})
|
||||||
const db = get(DataBase);
|
const db = get(DataBase)
|
||||||
const charID = get(selectedCharID);
|
const charID = get(selectedCharID)
|
||||||
const group = db.characters[charID];
|
const group = db.characters[charID]
|
||||||
if (group.type !== "group") {
|
if(group.type !== 'group'){
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageUrls = await Promise.all(
|
const imageUrls = await Promise.all(group.characters.map((v) => {
|
||||||
group.characters.map((v) => {
|
return getCharImage(findCharacterbyId(v).image, 'plain')
|
||||||
return getCharImage(findCharacterbyId(v).image, "plain");
|
}))
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
canvas.width = 256;
|
canvas.width = 256
|
||||||
canvas.height = 256;
|
canvas.height = 256
|
||||||
const ctx = canvas.getContext("2d");
|
const ctx = canvas.getContext("2d");
|
||||||
|
|
||||||
// Load the images
|
// Load the images
|
||||||
const images = [];
|
const images = [];
|
||||||
let loadedImages = 0;
|
let loadedImages = 0;
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
imageUrls.map(
|
imageUrls.map(
|
||||||
(url) =>
|
(url) =>
|
||||||
new Promise<void>((resolve) => {
|
new Promise<void>((resolve) => {
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.crossOrigin = "anonymous";
|
img.crossOrigin="anonymous"
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
images.push(img);
|
images.push(img);
|
||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
img.src = url;
|
img.src = url;
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
|
||||||
|
|
||||||
// Calculate dimensions and draw the grid
|
|
||||||
const numImages = images.length;
|
|
||||||
const numCols = Math.ceil(Math.sqrt(images.length));
|
|
||||||
const numRows = Math.ceil(images.length / numCols);
|
|
||||||
const cellWidth = canvas.width / numCols;
|
|
||||||
const cellHeight = canvas.height / numRows;
|
|
||||||
|
|
||||||
for (let row = 0; row < numRows; row++) {
|
|
||||||
for (let col = 0; col < numCols; col++) {
|
|
||||||
const index = row * numCols + col;
|
|
||||||
if (index >= numImages) break;
|
|
||||||
ctx.drawImage(
|
|
||||||
images[index],
|
|
||||||
col * cellWidth,
|
|
||||||
row * cellHeight,
|
|
||||||
cellWidth,
|
|
||||||
cellHeight
|
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
// Calculate dimensions and draw the grid
|
||||||
|
const numImages = images.length;
|
||||||
|
const numCols = Math.ceil(Math.sqrt(images.length));
|
||||||
|
const numRows = Math.ceil(images.length / numCols);
|
||||||
|
const cellWidth = canvas.width / numCols;
|
||||||
|
const cellHeight = canvas.height / numRows;
|
||||||
|
|
||||||
|
for (let row = 0; row < numRows; row++) {
|
||||||
|
for (let col = 0; col < numCols; col++) {
|
||||||
|
const index = row * numCols + col;
|
||||||
|
if (index >= numImages) break;
|
||||||
|
ctx.drawImage(
|
||||||
|
images[index],
|
||||||
|
col * cellWidth,
|
||||||
|
row * cellHeight,
|
||||||
|
cellWidth,
|
||||||
|
cellHeight
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the image URI
|
||||||
|
|
||||||
|
const uri = canvas.toDataURL()
|
||||||
|
console.log(uri)
|
||||||
|
canvas.remove()
|
||||||
|
db.characters[charID].image = await saveImage(dataURLtoBuffer(uri));
|
||||||
|
setDatabase(db)
|
||||||
|
alertStore.set({
|
||||||
|
type: 'none',
|
||||||
|
msg: ''
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
alertError(`${error}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the image URI
|
|
||||||
|
|
||||||
const uri = canvas.toDataURL();
|
|
||||||
console.log(uri);
|
|
||||||
canvas.remove();
|
|
||||||
db.characters[charID].image = await saveImage(dataURLtoBuffer(uri));
|
|
||||||
setDatabase(db);
|
|
||||||
alertStore.set({
|
|
||||||
type: "none",
|
|
||||||
msg: "",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
alertError(`${error}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function dataURLtoBuffer(string: string) {
|
function dataURLtoBuffer(string:string){
|
||||||
const regex = /^data:.+\/(.+);base64,(.*)$/;
|
const regex = /^data:.+\/(.+);base64,(.*)$/;
|
||||||
|
|
||||||
const matches = string.match(regex);
|
const matches = string.match(regex);
|
||||||
const ext = matches[1];
|
const ext = matches[1];
|
||||||
const data = matches[2];
|
const data = matches[2];
|
||||||
return Buffer.from(data, "base64");
|
return Buffer.from(data, 'base64');
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function addDefaultCharacters() {
|
export async function addDefaultCharacters() {
|
||||||
const imgs = [fetch("/sample/rika.png"), fetch("/sample/yuzu.png")];
|
const imgs = [fetch('/sample/rika.png'),fetch('/sample/yuzu.png')]
|
||||||
|
|
||||||
alertStore.set({
|
alertStore.set({
|
||||||
type: "wait",
|
type: 'wait',
|
||||||
msg: `Loading Sample bots...`,
|
msg: `Loading Sample bots...`
|
||||||
});
|
})
|
||||||
|
|
||||||
for (const img of imgs) {
|
for(const img of imgs){
|
||||||
const imgBuffer = await (await img).arrayBuffer();
|
const imgBuffer = await (await img).arrayBuffer()
|
||||||
const readed = await exifr.parse(imgBuffer, true);
|
const readed = (await exifr.parse(imgBuffer, true))
|
||||||
await sleep(10);
|
await sleep(10)
|
||||||
const va = decodeMsgpack(Buffer.from(readed.risuai, "base64")) as any;
|
const va = decodeMsgpack(Buffer.from(readed.risuai, 'base64')) as any
|
||||||
if (va.type !== 101) {
|
if(va.type !== 101){
|
||||||
alertError(language.errors.noData);
|
alertError(language.errors.noData)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
let char: character = va.data;
|
let char:character = va.data
|
||||||
let db = get(DataBase);
|
let db = get(DataBase)
|
||||||
if (char.emotionImages && char.emotionImages.length > 0) {
|
if(char.emotionImages && char.emotionImages.length > 0){
|
||||||
for (let i = 0; i < char.emotionImages.length; i++) {
|
for(let i=0;i<char.emotionImages.length;i++){
|
||||||
await sleep(10);
|
await sleep(10)
|
||||||
const imgp = await saveImage(char.emotionImages[i][1] as any);
|
const imgp = await saveImage(char.emotionImages[i][1] as any)
|
||||||
char.emotionImages[i][1] = imgp;
|
char.emotionImages[i][1] = imgp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char.chats = [
|
char.chats = [{
|
||||||
{
|
message: [],
|
||||||
message: [],
|
note: '',
|
||||||
note: "",
|
name: 'Chat 1',
|
||||||
name: "Chat 1",
|
localLore: []
|
||||||
localLore: [],
|
}]
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
if (checkNullish(char.sdData)) {
|
if(checkNullish(char.sdData)){
|
||||||
char.sdData = defaultSdDataFunc();
|
char.sdData = defaultSdDataFunc()
|
||||||
|
}
|
||||||
|
|
||||||
|
char.chatPage = 0
|
||||||
|
char.image = await saveImage(PngMetadata.filter(Buffer.from(imgBuffer)))
|
||||||
|
char.chaId = uuidv4()
|
||||||
|
db.characters.push(characterFormatUpdate(char))
|
||||||
|
setDatabase(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
char.chatPage = 0;
|
alertStore.set({
|
||||||
char.image = await saveImage(PngMetadata.filter(Buffer.from(imgBuffer)));
|
type: 'none',
|
||||||
char.chaId = uuidv4();
|
msg: ''
|
||||||
db.characters.push(characterFormatUpdate(char));
|
})
|
||||||
setDatabase(db);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
alertStore.set({
|
|
||||||
type: "none",
|
|
||||||
msg: "",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user