[feat] nai login

This commit is contained in:
kwaroran
2023-07-29 09:31:21 +09:00
parent 5dfa585f6e
commit fbc5b0bc8c
4 changed files with 101 additions and 1 deletions

View File

@@ -1,6 +1,9 @@
import { DataBase } from "src/ts/storage/database"
import { DataBase, setDatabase } from "src/ts/storage/database"
import type { OpenAIChat } from ".."
import { get } from "svelte/store"
import { globalFetch } from "src/ts/storage/globalApi"
import { alertError, alertInput, alertNormal, alertWait } from "src/ts/alert"
import { sleep } from "src/ts/util"
export function stringlizeNAIChat(formated:OpenAIChat[], char:string = ''){
const db = get(DataBase)
@@ -28,6 +31,68 @@ export function stringlizeNAIChat(formated:OpenAIChat[], char:string = ''){
return resultString.join('\n\n') + `\n\n${char}:`
}
export const novelLogin = async () => {
try {
const username = await alertInput("NovelAI ID")
const password = await alertInput("NovelAI Password")
alertWait('Logging in to NovelAI')
const _sodium = await import('libsodium-wrappers-sumo')
await sleep(1000)
await _sodium.ready
const sodium = _sodium;
// I don't know why, but this is needed to make it work
console.log(sodium)
await sleep(1000)
const key = sodium
.crypto_pwhash(
64,
new Uint8Array(Buffer.from(password)),
sodium.crypto_generichash(
sodium.crypto_pwhash_SALTBYTES,
password.slice(0, 6) + username + 'novelai_data_access_key'
),
2,
2e6,
sodium.crypto_pwhash_ALG_ARGON2ID13,
'base64'
)
.slice(0, 64)
const r = await globalFetch('https://api.novelai.net/user/login', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body:{
key: key
}
})
if ((!r.ok) || (!r.data?.accessToken)) {
alertError(`Failed to authenticate with NovelAI: ${r.data?.message ?? r.data}`)
return
}
const data = r.data?.accessToken
const db = get(DataBase)
db.novelai.token = data
alertNormal('Logged in to NovelAI')
setDatabase(db)
} catch (error) {
alertError(`Failed to authenticate with NovelAI: ${error}`)
}
}
export interface NAISettings{
topK: number
topP: number