Skip to content

Commit

Permalink
fix: remote importing
Browse files Browse the repository at this point in the history
  • Loading branch information
Vali-98 committed Aug 22, 2024
1 parent 61bbacd commit 815f93d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 88 deletions.
8 changes: 1 addition & 7 deletions app/constants/APIState/KoboldAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Global } from '@constants/GlobalValues'
import { Logger } from 'app/constants/Logger'
import { mmkv } from 'app/constants/MMKV'
import { SamplerID } from 'app/constants/SamplerData'
import axios from 'axios'

import { APIBase, APISampler } from './BaseAPI'

Expand Down Expand Up @@ -58,12 +57,7 @@ class KoboldAPI extends APIBase {
return JSON.parse(item).token
},
() => {
axios
.create({ timeout: 1000 })
.post(new URL('/api/extra/abort', endpoint).toString())
.catch(() => {
Logger.log(`Abort signal failed`)
})
fetch(new URL('/api/extra/abort', endpoint).toString(), { method: 'GET' })
}
)
}
Expand Down
75 changes: 42 additions & 33 deletions app/constants/Characters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { db as database } from '@db'
import { copyFileRes } from '@dr.pogodin/react-native-fs'
import axios from 'axios'
import { copyFileRes, writeFile } from '@dr.pogodin/react-native-fs'
import { characterGreetings, characterTags, characters, tags } from 'db/schema'
import { eq, inArray, notInArray } from 'drizzle-orm'
import { randomUUID } from 'expo-crypto'
Expand Down Expand Up @@ -300,10 +299,13 @@ export namespace Characters {
})
.returning({ id: characters.id, image_id: characters.image_id })

const greetingdata = data.alternate_greetings.map((item) => ({
character_id: id,
greeting: item,
}))
const greetingdata =
typeof data?.alternate_greetings === 'object'
? data?.alternate_greetings?.map((item) => ({
character_id: id,
greeting: item,
})) ?? []
: []
if (greetingdata.length > 0)
for (const greeting of greetingdata)
await tx.insert(characterGreetings).values(greeting)
Expand Down Expand Up @@ -400,22 +402,23 @@ export namespace Characters {
export const importCharacterFromChub = async (character_id: string) => {
Logger.log(`Importing character from Chub: ${character_id}`, true)
try {
const res = await axios.create({ timeout: 10000 }).post(
'https://api.chub.ai/api/characters/download',
{
const res = await fetch('https://api.chub.ai/api/characters/download', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
format: 'tavern',
fullPath: character_id,
},
{ responseType: 'arraybuffer' }
)

const response = Buffer.from(res.data, 'base64').toString('base64')
const uuid = randomUUID()
return FS.writeAsStringAsync(`${FS.cacheDirectory}${uuid}.png`, response, {
encoding: FS.EncodingType.Base64,
}).then(async () => {
return createCharacterFromImage(`${FS.cacheDirectory}${uuid}.png`)
}),
})
const data = await res.arrayBuffer()
const dataArray = new Uint8Array(data)
let binaryString = ''
dataArray.forEach((byte) => (binaryString += String.fromCharCode(byte)))
const cardCacheDir = `${FS.cacheDirectory}${randomUUID()}.png`
await writeFile(cardCacheDir, btoa(binaryString), 'base64')
return createCharacterFromImage(cardCacheDir)
} catch (error) {
Logger.log(`Could not retreive card. ${error}`)
}
Expand All @@ -424,40 +427,48 @@ export namespace Characters {
export const importCharacterFromPyg = async (character_id: string) => {
Logger.log(`Loading from Pygmalion with id: ${character_id}`, true)

const data = await fetch(
const query = await fetch(
`https://server.pygmalion.chat/api/export/character/${character_id}/v2`,
{
method: 'GET',
headers: {
accept: 'application/json',
'content-type': 'application/json',
'Content-Type': 'application/json',
},
}
)
if (data.status !== 200) {
Logger.log(`Failed to retrieve card from Pygmalion: ${data.status}`, true)
if (query.status !== 200) {
Logger.log(`Failed to retrieve card from Pygmalion: ${query.status}`, true)
return
}

const { character } = await data.json()
const { character } = await query.json()

const res = await fetch(character.data.avatar, {
method: 'GET',
})
const buffer = await res.arrayBuffer()
const image = Buffer.from(buffer).toString('base64')
const data = await res.arrayBuffer()
const dataArray = new Uint8Array(data)
let binaryString = ''
dataArray.forEach((byte) => (binaryString += String.fromCharCode(byte)))
const uuid = randomUUID()
return FS.writeAsStringAsync(`${FS.cacheDirectory}${uuid}.png`, image, {
encoding: FS.EncodingType.Base64,
}).then(async () => {
return db.mutate.createCharacter(character, `${FS.cacheDirectory}${uuid}.png`)
})

await writeFile(`${FS.cacheDirectory}${uuid}.png`, btoa(binaryString), 'base64')
await db.mutate.createCharacter(character, `${FS.cacheDirectory}${uuid}.png`)
Logger.log('Imported Character: ' + character.data.name)
}

export const importCharacterFromRemote = async (text: string) => {
// UUID standard RFC 4122, only used by pyg for now
const uuidRegex =
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/

if (uuidRegex.test(text)) {
return importCharacterFromPyg(text)
}

if (/^[^/]+\/[^/]+$/.test(text)) return importCharacterFromChub(text)

const url = new URL(text)
if (/pygmalion.chat/.test(url.hostname)) {
const param = new URLSearchParams(text)
Expand All @@ -481,8 +492,6 @@ export namespace Characters {
}

// Regex checks for format of [name][/][character]
if (/^[^/]+\/[^/]+$/.test(text)) return importCharacterFromChub(text)
if (uuidRegex.test(text)) return importCharacterFromPyg(text)
Logger.log(`URL not recognized`, true)
}

Expand Down
47 changes: 0 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"@react-navigation/drawer": "^6.6.4",
"@react-navigation/native": "^6.0.2",
"@shopify/flash-list": "1.6.4",
"axios": "^1.7.4",
"babel-plugin-inline-import": "^3.0.0",
"babel-plugin-react-compiler": "^0.0.0-experimental-696af53-20240625",
"cui-llama.rn": "^1.1.1",
Expand Down

0 comments on commit 815f93d

Please sign in to comment.