Merge branch 'main' into screenreader
This commit is contained in:
7
.github/workflows/github-actions-builder.yml
vendored
7
.github/workflows/github-actions-builder.yml
vendored
@@ -50,13 +50,6 @@ jobs:
|
||||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pnpm-store-
|
||||
- uses: actions/cache@v3
|
||||
name: Setup rust cache
|
||||
with:
|
||||
path: src-tauri/target/
|
||||
key: ${{ runner.os }}-rust-cache
|
||||
restore-keys: |
|
||||
${{ runner.os }}-rust-cache
|
||||
- name: install frontend dependencies
|
||||
run: pnpm install --no-frozen-lockfile # change this to npm or pnpm depending on which one you use
|
||||
- if: matrix.platform == 'ubuntu-latest'
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
},
|
||||
"package": {
|
||||
"productName": "RisuAI",
|
||||
"version": "1.24.4"
|
||||
"version": "1.25.2"
|
||||
},
|
||||
"tauri": {
|
||||
"allowlist": {
|
||||
|
||||
@@ -309,4 +309,5 @@ export const languageChinese = {
|
||||
recent: '最新',
|
||||
downloads: '下载量',
|
||||
trending: "热度",
|
||||
imageCompression: "图像压缩"
|
||||
}
|
||||
@@ -312,6 +312,11 @@ export const languageEnglish = {
|
||||
enterMessageForTranslateToEnglish: "Enter Message for Translate to English",
|
||||
recent: 'Recent',
|
||||
downloads: 'Downloads',
|
||||
trending: "Trending"
|
||||
trending: "Trending",
|
||||
imageCompression: "Image Compression",
|
||||
notLoggedIn: "Not Logged in to Risu Account",
|
||||
googleDriveInfo: "Connect to google drive to sync your data.",
|
||||
googleDriveConnection: "Google Drive Connection",
|
||||
googleDriveConnected: "Google Drive Connected"
|
||||
|
||||
}
|
||||
@@ -282,4 +282,6 @@ export const languageKorean = {
|
||||
useChatCopy: "채팅 메시지 복사 사용",
|
||||
autoTranslateInput: "입력 자동 번역",
|
||||
enterMessageForTranslateToEnglish: "영어로 번역할 메시지를 입력해주세요",
|
||||
imageCompression: "이미지 압축"
|
||||
|
||||
}
|
||||
@@ -51,6 +51,9 @@
|
||||
<div class="flex items-center mt-4">
|
||||
<Check bind:check={$DataBase.showUnrecommended} name={language.showUnrecommended}/>
|
||||
</div>
|
||||
<div class="flex items-center mt-4">
|
||||
<Check bind:check={$DataBase.imageCompression} name={language.imageCompression}/>
|
||||
</div>
|
||||
<div class="flex items-center mt-4">
|
||||
<Check bind:check={$DataBase.useExperimental} name={language.useExperimental}/>
|
||||
</div>
|
||||
|
||||
@@ -1,10 +1,40 @@
|
||||
<script>
|
||||
<script lang="ts">
|
||||
import { language } from "src/lang";
|
||||
import Help from "src/lib/Others/Help.svelte";
|
||||
import { hubURL } from "src/ts/characterCards";
|
||||
import { getCharImage, selectUserImg } from "src/ts/characters";
|
||||
import { loadRisuAccountData, saveRisuAccountData } from "src/ts/drive/accounter";
|
||||
import { checkDriver } from "src/ts/drive/drive";
|
||||
import { DataBase } from "src/ts/storage/database";
|
||||
let openIframe = false
|
||||
let openIframeURL = ''
|
||||
let popup:Window = null
|
||||
</script>
|
||||
|
||||
<svelte:window on:message={async (e) => {
|
||||
if(e.origin.startsWith("https://sv.risuai.xyz") || e.origin.startsWith("http://127.0.0.1")){
|
||||
if(e.data.msg.type === 'drive'){
|
||||
console.log(e.data.msg)
|
||||
await loadRisuAccountData()
|
||||
$DataBase.account.data.refresh_token = e.data.msg.data.refresh_token
|
||||
$DataBase.account.data.access_token = e.data.msg.data.access_token
|
||||
$DataBase.account.data.expires_in = (e.data.msg.data.expires_in * 700) + Date.now()
|
||||
await saveRisuAccountData()
|
||||
popup.close()
|
||||
}
|
||||
else if(e.data.msg.data.vaild){
|
||||
openIframe = false
|
||||
$DataBase.account = {
|
||||
id: e.data.msg.id,
|
||||
token: e.data.msg.token,
|
||||
data: e.data.msg.data
|
||||
}
|
||||
}
|
||||
}
|
||||
}}></svelte:window>
|
||||
|
||||
<h2 class="mb-2 text-2xl font-bold mt-2">{language.user}</h2>
|
||||
|
||||
<span class="text-neutral-200 mt-2 mb-2">{language.userIcon}</span>
|
||||
<button on:click={() => {selectUserImg()}}>
|
||||
{#if $DataBase.userIcon === ''}
|
||||
@@ -19,3 +49,44 @@
|
||||
</button>
|
||||
<span class="text-neutral-200 mt-4">{language.username}</span>
|
||||
<input class="text-neutral-200 mt-2 mb-4 p-2 bg-transparent input-text focus:bg-selected" placeholder="User" bind:value={$DataBase.username}>
|
||||
{#if $DataBase.useExperimental}
|
||||
<div class="bg-darkbg p-3 rounded-md mb-2 flex flex-col items-start">
|
||||
<div class="w-full">
|
||||
<h1 class="text-3xl font-black min-w-0">Risu Account{#if $DataBase.account} <Help key="experimental"/>
|
||||
<button class="bg-selected p-1 text-sm font-light rounded-md hover:bg-green-500 transition-colors float-right" on:click={async () => {
|
||||
$DataBase.account = undefined
|
||||
}}>Logout</button>
|
||||
{/if}</h1>
|
||||
</div>
|
||||
{#if $DataBase.account}
|
||||
<span class="mb-4 text-gray-400">ID: {$DataBase.account.id}</span>
|
||||
<h1 class="text-xl font-bold mt-2">{language.googleDriveConnection}</h1>
|
||||
{#if !$DataBase.account.data.refresh_token}
|
||||
<span class="text-sm font-light mb-2 text-gray-400">{language.googleDriveInfo}</span>
|
||||
<button class="bg-selected p-2 rounded-md hover:bg-green-500 transition-colors" on:click={async () => {
|
||||
if((!popup) || popup.closed){
|
||||
popup = window.open(await checkDriver('reftoken'))
|
||||
}
|
||||
}}>
|
||||
Connect to Google Drive
|
||||
</button>
|
||||
{:else}
|
||||
<span class="text-sm font-light mb-2 text-gray-400">{language.googleDriveConnected}</span>
|
||||
{/if}
|
||||
{:else}
|
||||
<span>{language.notLoggedIn}</span>
|
||||
<button class="bg-selected p-2 rounded-md mt-2 hover:bg-green-500 transition-colors" on:click={() => {
|
||||
openIframeURL = hubURL + '/hub/login'
|
||||
openIframe = true
|
||||
}}>
|
||||
Login
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
{#if openIframe}
|
||||
<div class="fixed top-0 left-0 bg-black bg-opacity-50 w-full h-full flex justify-center items-center">
|
||||
<iframe src={openIframeURL} title="login" class="w-full h-full">
|
||||
</iframe>
|
||||
</div>
|
||||
{/if}
|
||||
@@ -11,6 +11,7 @@ import { characterFormatUpdate } from "./characters"
|
||||
import { checkCharOrder, downloadFile, readImage, saveAsset } from "./storage/globalApi"
|
||||
import { cloneDeep } from "lodash"
|
||||
import { selectedCharID } from "./stores"
|
||||
import { convertImage } from "./parser"
|
||||
|
||||
export const hubURL = import.meta.env.DEV ? "http://127.0.0.1:8787" : "https://sv.risuai.xyz"
|
||||
|
||||
@@ -532,7 +533,7 @@ export async function exportSpecV2(char:character) {
|
||||
msg: `Loading... (Adding Emotions ${i} / ${card.data.extensions.risuai.emotions.length})`
|
||||
})
|
||||
const rData = await readImage(card.data.extensions.risuai.emotions[i][1])
|
||||
char.emotionImages[i][1] = Buffer.from(rData).toString('base64')
|
||||
char.emotionImages[i][1] = Buffer.from(await convertImage(rData)).toString('base64')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,7 +545,7 @@ export async function exportSpecV2(char:character) {
|
||||
msg: `Loading... (Adding Additional Assets ${i} / ${card.data.extensions.risuai.additionalAssets.length})`
|
||||
})
|
||||
const rData = await readImage(card.data.extensions.risuai.additionalAssets[i][1])
|
||||
char.additionalAssets[i][1] = Buffer.from(rData).toString('base64')
|
||||
char.additionalAssets[i][1] = Buffer.from(await convertImage(rData)).toString('base64')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -611,7 +612,7 @@ export async function shareRisuHub(char:character, arg:{
|
||||
})
|
||||
const data = card.data.extensions.risuai.emotions[i][1]
|
||||
const rData = await readImage(data)
|
||||
resources.push([data, Buffer.from(rData).toString('base64')])
|
||||
resources.push([data, Buffer.from(await convertImage(rData)).toString('base64')])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -626,7 +627,7 @@ export async function shareRisuHub(char:character, arg:{
|
||||
})
|
||||
const data = card.data.extensions.risuai.additionalAssets[i][1]
|
||||
const rData = await readImage(data)
|
||||
resources.push([data, Buffer.from(rData).toString('base64')])
|
||||
resources.push([data, Buffer.from(await convertImage(rData)).toString('base64')])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,52 @@
|
||||
async function loginWithGoogle() {
|
||||
|
||||
import { get } from "svelte/store"
|
||||
import { hubURL } from "../characterCards"
|
||||
import { DataBase } from "../storage/database"
|
||||
import { alertError } from "../alert"
|
||||
|
||||
export async function risuLogin() {
|
||||
const win = window.open(hubURL + '/hub/login')
|
||||
window.addEventListener("message", (ev) => {
|
||||
console.log(ev)
|
||||
const data = JSON.parse(ev.data)
|
||||
console.log(data)
|
||||
win.close()
|
||||
})
|
||||
}
|
||||
|
||||
export async function saveRisuAccountData() {
|
||||
const db = get(DataBase)
|
||||
if(!db.account){
|
||||
alertError("Not logged in error")
|
||||
return
|
||||
}
|
||||
const s = await fetch(hubURL + '/hub/account/save', {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
token: db.account.token,
|
||||
save: db.account.data
|
||||
})
|
||||
})
|
||||
if(s.status !== 200){
|
||||
alertError(await s.text())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadRisuAccountData() {
|
||||
const db = get(DataBase)
|
||||
if(!db.account){
|
||||
alertError("Not logged in error")
|
||||
return
|
||||
}
|
||||
const s = await fetch(hubURL + '/hub/account/load', {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
token: db.account.token
|
||||
})
|
||||
})
|
||||
if(s.status !== 200){
|
||||
alertError(await s.text())
|
||||
return
|
||||
}
|
||||
db.account.data = await s.json()
|
||||
}
|
||||
@@ -7,10 +7,13 @@ import { BaseDirectory, exists, readBinaryFile, readDir, writeBinaryFile } from
|
||||
import { language } from "../../lang";
|
||||
import { relaunch } from '@tauri-apps/api/process';
|
||||
import { open } from '@tauri-apps/api/shell';
|
||||
import { cloneDeep, isEqual, last } from "lodash";
|
||||
import { sleep } from "../util";
|
||||
import { hubURL } from "../characterCards";
|
||||
|
||||
export async function checkDriver(type:'save'|'load'|'loadtauri'|'savetauri'|'reftoken'){
|
||||
const CLIENT_ID = '580075990041-l26k2d3c0nemmqiu3d3aag01npfrkn76.apps.googleusercontent.com';
|
||||
const REDIRECT_URI = (isTauri || isNodeServer) ? "https://risuai.xyz/" : `https://${location.host}/`
|
||||
const REDIRECT_URI = type === 'reftoken' ? 'https://sv.risuai.xyz/drive' : ((isTauri || isNodeServer) ? "https://risuai.xyz/" : `https://${location.host}/`)
|
||||
const SCOPE = 'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata';
|
||||
const encodedRedirectUri = encodeURIComponent(REDIRECT_URI);
|
||||
const authorizationUrl = `https://accounts.google.com/o/oauth2/auth?client_id=${CLIENT_ID}&redirect_uri=${encodedRedirectUri}&scope=${SCOPE}&response_type=code&state=${type}`;
|
||||
@@ -18,8 +21,7 @@ export async function checkDriver(type:'save'|'load'|'loadtauri'|'savetauri'|'re
|
||||
|
||||
if(type === 'reftoken'){
|
||||
const authorizationUrl = `https://accounts.google.com/o/oauth2/auth?client_id=${CLIENT_ID}&redirect_uri=${encodedRedirectUri}&scope=${SCOPE}&response_type=code&state=${"accesstauri"}&access_type=offline&prompt=consent`;
|
||||
openURL(authorizationUrl)
|
||||
return
|
||||
return authorizationUrl
|
||||
}
|
||||
|
||||
if(type === 'save' || type === 'load'){
|
||||
@@ -39,7 +41,7 @@ export async function checkDriver(type:'save'|'load'|'loadtauri'|'savetauri'|'re
|
||||
code = code.substring(code.lastIndexOf(' ')).trim()
|
||||
}
|
||||
if(type === 'loadtauri'){
|
||||
await loadDrive(code)
|
||||
await loadDrive(code, 'backup')
|
||||
}
|
||||
else{
|
||||
await backupDrive(code)
|
||||
@@ -69,7 +71,7 @@ export async function checkDriverInit() {
|
||||
await backupDrive(json.access_token)
|
||||
}
|
||||
else if(da === 'load'){
|
||||
await loadDrive(json.access_token)
|
||||
await loadDrive(json.access_token, 'backup')
|
||||
}
|
||||
else if(da === 'savetauri' || da === 'loadtauri'){
|
||||
alertStore.set({
|
||||
@@ -101,8 +103,95 @@ export async function checkDriverInit() {
|
||||
}
|
||||
}
|
||||
|
||||
let lastSaved:number = parseInt(localStorage.getItem('risu_lastsaved') ?? '-1')
|
||||
let BackupDb:Database = null
|
||||
|
||||
|
||||
export async function syncDrive() {
|
||||
BackupDb = cloneDeep(get(DataBase))
|
||||
while(true){
|
||||
const maindb = get(DataBase)
|
||||
if(maindb?.account?.data?.access_token && maindb?.account?.data?.refresh_token && maindb?.account?.data?.expires_in){
|
||||
if(maindb.account.data.expires_in < Date.now()){
|
||||
if(!maindb.account){
|
||||
alertError("Not logged in error")
|
||||
return
|
||||
}
|
||||
const s = await fetch(hubURL + '/drive/refresh', {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
token: maindb.account.token
|
||||
})
|
||||
})
|
||||
if(s.status !== 200){
|
||||
alertError(await s.text())
|
||||
return
|
||||
}
|
||||
maindb.account.data = await s.json()
|
||||
}
|
||||
const ACCESS_TOKEN = maindb.account.data.access_token
|
||||
const d = await loadDrive(ACCESS_TOKEN, 'sync')
|
||||
const hadNoSync = d === 'noSync'
|
||||
if((!isEqual(maindb, BackupDb)) || hadNoSync){
|
||||
BackupDb = cloneDeep(maindb)
|
||||
const files:DriveFile[] = await getFilesInFolder(ACCESS_TOKEN)
|
||||
const fileNames = files.map((d) => {
|
||||
return d.name
|
||||
})
|
||||
if(isTauri){
|
||||
const assets = await readDir('assets', {dir: BaseDirectory.AppData})
|
||||
let i = 0;
|
||||
for(let asset of assets){
|
||||
i += 1;
|
||||
if(hadNoSync){
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: `Uploading Sync Files... (${i} / ${assets.length})`
|
||||
})
|
||||
}
|
||||
const key = asset.name
|
||||
if(!key || !key.endsWith('.png')){
|
||||
continue
|
||||
}
|
||||
const formatedKey = formatKeys(key)
|
||||
if(!fileNames.includes(formatedKey)){
|
||||
await createFileInFolder(ACCESS_TOKEN, formatedKey, await readBinaryFile(asset.path))
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
const keys = await forageStorage.keys()
|
||||
|
||||
for(let i=0;i<keys.length;i++){
|
||||
if(hadNoSync){
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: `Uploading Sync Files... (${i} / ${keys.length})`
|
||||
})
|
||||
}
|
||||
const key = keys[i]
|
||||
if(!key.endsWith('.png')){
|
||||
continue
|
||||
}
|
||||
const formatedKey = formatKeys(key)
|
||||
if(!fileNames.includes(formatedKey)){
|
||||
await createFileInFolder(ACCESS_TOKEN, formatedKey, await forageStorage.getItem(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
const dbjson = JSON.stringify(get(DataBase))
|
||||
lastSaved = Math.floor(Date.now() / 1000)
|
||||
localStorage.setItem('risu_lastsaved', `${lastSaved}`)
|
||||
await createFileInFolder(ACCESS_TOKEN, `${lastSaved}-database.risudat2`, Buffer.from(dbjson, 'utf-8'))
|
||||
if(hadNoSync){
|
||||
alertNormal("First Setup Success")
|
||||
}
|
||||
}
|
||||
}
|
||||
await sleep(3000)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function backupDrive(ACCESS_TOKEN:string) {
|
||||
alertStore.set({
|
||||
@@ -176,11 +265,13 @@ type DriveFile = {
|
||||
id: string
|
||||
}
|
||||
|
||||
async function loadDrive(ACCESS_TOKEN:string) {
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: "Loading Backup..."
|
||||
})
|
||||
async function loadDrive(ACCESS_TOKEN:string, mode: 'backup'|'sync'):Promise<void|"noSync"> {
|
||||
if(mode === 'backup'){
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: "Loading Backup..."
|
||||
})
|
||||
}
|
||||
const files:DriveFile[] = await getFilesInFolder(ACCESS_TOKEN)
|
||||
let foragekeys:string[] = []
|
||||
let loadedForageKeys = false
|
||||
@@ -203,43 +294,89 @@ async function loadDrive(ACCESS_TOKEN:string) {
|
||||
|
||||
|
||||
let dbs:[DriveFile,number][] = []
|
||||
let noSyncData = true
|
||||
|
||||
for(const f of files){
|
||||
if(f.name.endsWith("-database.risudat")){
|
||||
const tm = parseInt(f.name.split('-')[0])
|
||||
if(isNaN(tm)){
|
||||
continue
|
||||
}
|
||||
else{
|
||||
dbs.push([f,tm])
|
||||
if(mode === 'backup'){
|
||||
for(const f of files){
|
||||
if(f.name.endsWith("-database.risudat")){
|
||||
const tm = parseInt(f.name.split('-')[0])
|
||||
if(isNaN(tm)){
|
||||
continue
|
||||
}
|
||||
else{
|
||||
dbs.push([f,tm])
|
||||
}
|
||||
}
|
||||
}
|
||||
dbs.sort((a,b) => {
|
||||
return b[1] - a[1]
|
||||
})
|
||||
}
|
||||
else if(mode === 'sync'){
|
||||
for(const f of files){
|
||||
if(f.name.endsWith("-database.risudat2")){
|
||||
const tm = parseInt(f.name.split('-')[0])
|
||||
if(isNaN(tm)){
|
||||
continue
|
||||
}
|
||||
else{
|
||||
if(tm > lastSaved){
|
||||
dbs.push([f,tm])
|
||||
}
|
||||
noSyncData = false
|
||||
}
|
||||
}
|
||||
}
|
||||
dbs.sort((a,b) => {
|
||||
return b[1] - a[1]
|
||||
})
|
||||
}
|
||||
|
||||
if(noSyncData && mode === 'sync'){
|
||||
return 'noSync'
|
||||
}
|
||||
dbs.sort((a,b) => {
|
||||
return b[1] - a[1]
|
||||
})
|
||||
|
||||
if(dbs.length !== 0){
|
||||
let selectables:string[] = []
|
||||
for(let i=0;i<dbs.length;i++){
|
||||
selectables.push(`Backup saved in ${(new Date(dbs[i][1] * 1000)).toLocaleString()}`)
|
||||
if(selectables.length > 7){
|
||||
break
|
||||
}
|
||||
if(mode === 'sync'){
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: "Sync Data..."
|
||||
})
|
||||
}
|
||||
const selectedIndex = (await alertSelect([language.loadLatest, language.loadOthers]) === '0') ? 0 : parseInt(await alertSelect(selectables))
|
||||
const selectedDb = dbs[selectedIndex][0]
|
||||
|
||||
const db:Database = JSON.parse(Buffer.from(pako.inflate(await getFileData(ACCESS_TOKEN, selectedDb.id))).toString('utf-8'))
|
||||
async function getDbFromList(){
|
||||
let selectables:string[] = []
|
||||
for(let i=0;i<dbs.length;i++){
|
||||
selectables.push(`Backup saved in ${(new Date(dbs[i][1] * 1000)).toLocaleString()}`)
|
||||
if(selectables.length > 7){
|
||||
break
|
||||
}
|
||||
}
|
||||
const selectedIndex = (await alertSelect([language.loadLatest, language.loadOthers]) === '0') ? 0 : parseInt(await alertSelect(selectables))
|
||||
const selectedDb = dbs[selectedIndex][0]
|
||||
const decompressedDb:Database = JSON.parse(Buffer.from(pako.inflate(await getFileData(ACCESS_TOKEN, selectedDb.id))).toString('utf-8'))
|
||||
return decompressedDb
|
||||
}
|
||||
|
||||
const db:Database = mode === 'backup' ? await getDbFromList() : JSON.parse(Buffer.from(await getFileData(ACCESS_TOKEN, dbs[0][0].id)).toString('utf-8'))
|
||||
lastSaved = Date.now()
|
||||
localStorage.setItem('risu_lastsaved', `${lastSaved}`)
|
||||
const requiredImages = (getUnpargeables(db))
|
||||
let ind = 0;
|
||||
for(const images of requiredImages){
|
||||
ind += 1
|
||||
const formatedImage = formatKeys(images)
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: `Loading Backup... (${ind} / ${requiredImages.length})`
|
||||
})
|
||||
if(mode === 'sync'){
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: `Sync Files... (${ind} / ${requiredImages.length})`
|
||||
})
|
||||
}
|
||||
else{
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: `Loading Backup... (${ind} / ${requiredImages.length})`
|
||||
})
|
||||
}
|
||||
if(await checkImageExists(images)){
|
||||
//skip process
|
||||
}
|
||||
@@ -276,7 +413,7 @@ async function loadDrive(ACCESS_TOKEN:string) {
|
||||
relaunch()
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: "Success, Refresh your app."
|
||||
msg: "Success, Refreshing your app."
|
||||
})
|
||||
}
|
||||
else{
|
||||
@@ -284,11 +421,11 @@ async function loadDrive(ACCESS_TOKEN:string) {
|
||||
location.search = ''
|
||||
alertStore.set({
|
||||
type: "wait",
|
||||
msg: "Success, Refresh your app."
|
||||
msg: "Success, Refreshing your app."
|
||||
})
|
||||
}
|
||||
}
|
||||
else{
|
||||
else if(mode === 'backup'){
|
||||
location.search = ''
|
||||
}
|
||||
}
|
||||
|
||||
104
src/ts/parser.ts
104
src/ts/parser.ts
@@ -1,8 +1,9 @@
|
||||
import DOMPurify from 'isomorphic-dompurify';
|
||||
import showdown from 'showdown';
|
||||
import type { character, groupChat } from './storage/database';
|
||||
import { DataBase, type character, type groupChat } from './storage/database';
|
||||
import { getFileSrc } from './storage/globalApi';
|
||||
import { processScript } from './process/scripts';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
const convertor = new showdown.Converter({
|
||||
simpleLineBreaks: true,
|
||||
@@ -65,4 +66,103 @@ export function parseMarkdownSafe(data:string) {
|
||||
|
||||
export async function hasher(data:Uint8Array){
|
||||
return Buffer.from(await crypto.subtle.digest("SHA-256", data)).toString('hex');
|
||||
}
|
||||
}
|
||||
|
||||
export async function convertImage(data:Uint8Array) {
|
||||
if(!get(DataBase).imageCompression){
|
||||
return data
|
||||
}
|
||||
const type = checkImageType(data)
|
||||
if(type !== 'Unknown' && type !== 'WEBP' && type !== 'AVIF'){
|
||||
if(type === 'PNG' && isAPNG(data)){
|
||||
return data
|
||||
}
|
||||
|
||||
console.log('converting')
|
||||
return await resizeAndConvert(data)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
async function resizeAndConvert(imageData: Uint8Array): Promise<Buffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const base64Image = 'data:image/png;base64,' + Buffer.from(imageData).toString('base64');
|
||||
const image = new Image();
|
||||
image.onload = () => {
|
||||
URL.revokeObjectURL(base64Image);
|
||||
|
||||
// Create a canvas
|
||||
const canvas = document.createElement('canvas');
|
||||
const context = canvas.getContext('2d');
|
||||
if (!context) {
|
||||
throw new Error('Unable to get 2D context');
|
||||
}
|
||||
|
||||
// Compute the new dimensions while maintaining aspect ratio
|
||||
let { width, height } = image;
|
||||
if (width > 3000 || height > 3000) {
|
||||
const aspectRatio = width / height;
|
||||
if (width > height) {
|
||||
width = 3000;
|
||||
height = Math.round(width / aspectRatio);
|
||||
} else {
|
||||
height = 3000;
|
||||
width = Math.round(height * aspectRatio);
|
||||
}
|
||||
}
|
||||
|
||||
// Resize and draw the image to the canvas
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
context.drawImage(image, 0, 0, width, height);
|
||||
|
||||
// Try to convert to WebP
|
||||
let base64 = canvas.toDataURL('image/webp', 90);
|
||||
|
||||
// If WebP is not supported, convert to JPEG
|
||||
if (base64.indexOf('data:image/webp') != 0) {
|
||||
base64 = canvas.toDataURL('image/jpeg', 90);
|
||||
}
|
||||
|
||||
// Convert it to Uint8Array
|
||||
const array = Buffer.from(base64.split(',')[1], 'base64');
|
||||
resolve(array);
|
||||
};
|
||||
image.src = base64Image;
|
||||
});
|
||||
}
|
||||
|
||||
type ImageType = 'JPEG' | 'PNG' | 'GIF' | 'BMP' | 'AVIF' | 'WEBP' | 'Unknown';
|
||||
|
||||
function checkImageType(arr:Uint8Array):ImageType {
|
||||
const isJPEG = arr[0] === 0xFF && arr[1] === 0xD8 && arr[arr.length-2] === 0xFF && arr[arr.length-1] === 0xD9;
|
||||
const isPNG = arr[0] === 0x89 && arr[1] === 0x50 && arr[2] === 0x4E && arr[3] === 0x47 && arr[4] === 0x0D && arr[5] === 0x0A && arr[6] === 0x1A && arr[7] === 0x0A;
|
||||
const isGIF = arr[0] === 0x47 && arr[1] === 0x49 && arr[2] === 0x46 && arr[3] === 0x38 && (arr[4] === 0x37 || arr[4] === 0x39) && arr[5] === 0x61;
|
||||
const isBMP = arr[0] === 0x42 && arr[1] === 0x4D;
|
||||
const isAVIF = arr[4] === 0x66 && arr[5] === 0x74 && arr[6] === 0x79 && arr[7] === 0x70 && arr[8] === 0x61 && arr[9] === 0x76 && arr[10] === 0x69 && arr[11] === 0x66;
|
||||
const isWEBP = arr[0] === 0x52 && arr[1] === 0x49 && arr[2] === 0x46 && arr[3] === 0x46 && arr[8] === 0x57 && arr[9] === 0x45 && arr[10] === 0x42 && arr[11] === 0x50;
|
||||
|
||||
if (isJPEG) return "JPEG";
|
||||
if (isPNG) return "PNG";
|
||||
if (isGIF) return "GIF";
|
||||
if (isBMP) return "BMP";
|
||||
if (isAVIF) return "AVIF";
|
||||
if (isWEBP) return "WEBP";
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
function isAPNG(pngData: Uint8Array): boolean {
|
||||
const pngSignature = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
const acTL = [0x61, 0x63, 0x54, 0x4C];
|
||||
|
||||
if (!pngData.slice(0, pngSignature.length).every((v, i) => v === pngSignature[i])) {
|
||||
throw new Error('Invalid PNG data');
|
||||
}
|
||||
|
||||
for (let i = pngSignature.length; i < pngData.length - 12; i += 4) {
|
||||
if (pngData.slice(i + 4, i + 8).every((v, j) => v === acTL[j])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import { defaultAutoSuggestPrompt, defaultJailbreak, defaultMainPrompt } from '.
|
||||
|
||||
export const DataBase = writable({} as any as Database)
|
||||
export const loadedStore = writable(false)
|
||||
export let appVer = '1.24.4'
|
||||
export let appVer = '1.25.2'
|
||||
|
||||
export function setDatabase(data:Database){
|
||||
if(checkNullish(data.characters)){
|
||||
@@ -257,6 +257,9 @@ export function setDatabase(data:Database){
|
||||
if(checkNullish(data.autoSuggestPrompt)){
|
||||
data.autoSuggestPrompt = defaultAutoSuggestPrompt
|
||||
}
|
||||
if(checkNullish(data.imageCompression)){
|
||||
data.imageCompression = true
|
||||
}
|
||||
|
||||
changeLanguage(data.language)
|
||||
DataBase.set(data)
|
||||
@@ -505,6 +508,16 @@ export interface Database{
|
||||
useChatCopy:boolean,
|
||||
novellistAPI:string,
|
||||
useAutoTranslateInput:boolean
|
||||
imageCompression:boolean
|
||||
account?:{
|
||||
token:string
|
||||
id:string,
|
||||
data: {
|
||||
refresh_token?:string,
|
||||
access_token?:string
|
||||
expires_in?: number
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface hordeConfig{
|
||||
|
||||
@@ -14,12 +14,13 @@ import { selectedCharID } from "../stores";
|
||||
import { Body, ResponseType, fetch as TauriFetch } from "@tauri-apps/api/http";
|
||||
import { loadPlugins } from "../process/plugins";
|
||||
import { alertError, alertStore } from "../alert";
|
||||
import { checkDriverInit } from "../drive/drive";
|
||||
import { checkDriverInit, syncDrive } from "../drive/drive";
|
||||
import { hasher } from "../parser";
|
||||
import { characterHubImport } from "../characterCards";
|
||||
import { cloneDeep } from "lodash";
|
||||
import { NodeStorage } from "./nodeStorage";
|
||||
import { defaultJailbreak, defaultMainPrompt, oldJailbreak, oldMainPrompt } from "./defaultPrompts";
|
||||
import { loadRisuAccountData } from "../drive/accounter";
|
||||
|
||||
//@ts-ignore
|
||||
export const isTauri = !!window.__TAURI__
|
||||
@@ -195,6 +196,7 @@ let lastSave = ''
|
||||
|
||||
export async function saveDb(){
|
||||
lastSave =JSON.stringify(get(DataBase))
|
||||
syncDrive()
|
||||
while(true){
|
||||
const dbjson = JSON.stringify(get(DataBase))
|
||||
if(dbjson !== lastSave){
|
||||
@@ -358,6 +360,11 @@ export async function loadData() {
|
||||
} catch (error) {}
|
||||
await checkNewFormat()
|
||||
updateTextTheme()
|
||||
if(get(DataBase).account){
|
||||
try {
|
||||
await loadRisuAccountData()
|
||||
} catch (error) {}
|
||||
}
|
||||
loadedStore.set(true)
|
||||
selectedCharID.set(-1)
|
||||
saveDb()
|
||||
|
||||
@@ -49,7 +49,6 @@ export class ChatTokenizer {
|
||||
this.useName = useName
|
||||
}
|
||||
async tokenizeChat(data:OpenAIChat) {
|
||||
console.log(data.content)
|
||||
let encoded = (await encode(data.content)).length + this.chatAdditonalTokens
|
||||
if(data.name && this.useName ==='name'){
|
||||
encoded += (await encode(data.name)).length
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"version":"1.24.4"}
|
||||
{"version":"1.25.2"}
|
||||
Reference in New Issue
Block a user