1
1
import { config } from "@shared/config" ;
2
- import { CardBundleWithoutID , CardData , Result , cardSchema , cardTagsSchema } from "@shared/types" ;
2
+ import { CardBundleWithoutID , CardData , Result , cardSchema , cardTagSchema } from "@shared/types" ;
3
3
import { deepFreeze , isValidFileName , toPathEscapedStr } from "@shared/utils" ;
4
4
import archiver from "archiver" ;
5
5
import { app , dialog } from "electron" ;
@@ -205,7 +205,7 @@ async function _sillyImport(filePath: string): Promise<Result<void, Error>> {
205
205
if ( ext !== ".json" && ext !== ".png" ) {
206
206
return { kind : "err" , error : new Error ( "Invalid file type for SillyTavern card. Must be .json or .png." ) } ;
207
207
}
208
-
208
+ // Read silly tavern data depending on if it's a .png or .json file
209
209
const isPNG = ext === ".png" ;
210
210
let data : string ;
211
211
if ( isPNG ) {
@@ -218,13 +218,15 @@ async function _sillyImport(filePath: string): Promise<Result<void, Error>> {
218
218
data = await fsp . readFile ( filePath , "utf8" ) ;
219
219
}
220
220
221
+ // Parse SillyTavern card data
221
222
const parseResult = sillyCardSchema . safeParse ( JSON . parse ( data ) ) ;
222
223
if ( ! parseResult . success ) {
223
224
const hrError = fromError ( parseResult . error ) ;
224
225
return { kind : "err" , error : new Error ( `Invalid SillyTavern card: ${ hrError } ` ) } ;
225
226
}
226
227
const sillyCard : SillyCardData = parseResult . data ;
227
228
229
+ // Convert SillyTavern card to anime.gf format
228
230
const agfCardRes = await _sillyCardToAGFCard ( sillyCard ) ;
229
231
if ( agfCardRes . kind === "err" ) {
230
232
return agfCardRes ;
@@ -237,6 +239,7 @@ async function _sillyImport(filePath: string): Promise<Result<void, Error>> {
237
239
}
238
240
const { dirName, dirPath } = cardDirRes . value ;
239
241
242
+ // Create the anime.gf card in the cards directory
240
243
try {
241
244
await fsp . mkdir ( dirPath ) ;
242
245
// Write .png file to avatar.png
@@ -249,7 +252,6 @@ async function _sillyImport(filePath: string): Promise<Result<void, Error>> {
249
252
if ( sillyCard . data . avatar && sillyCard . data . avatar === "none" ) {
250
253
// download avatar from url
251
254
const avatarPath = path . join ( dirPath , "avatar.png" ) ;
252
-
253
255
const bufferRes = await downloadImageBuffer ( sillyCard . data . avatar ) ;
254
256
if ( bufferRes . kind === "err" ) {
255
257
return bufferRes ;
@@ -272,11 +274,14 @@ async function _sillyCardToAGFCard(sillyCard: SillyCardData): Promise<Result<Car
272
274
const tags = sillyCard . data . tags
273
275
. map ( ( tag ) => tag . toLowerCase ( ) . trim ( ) )
274
276
. filter ( ( tag ) => {
275
- const res = cardTagsSchema . safeParse ( tag ) ;
277
+ const res = cardTagSchema . safeParse ( tag ) ;
278
+ console . log ( `tag ${ tag } is ${ res . success ? "valid" : "invalid" } ` ) ;
276
279
return res . success ;
277
280
} )
278
281
. slice ( 0 , config . card . tagsMaxCount ) ;
279
282
283
+ // Coerce name to /^[\p{L}\p{N}_ -]+$/u,
284
+
280
285
const agfCard : CardData = {
281
286
spec : "anime.gf" ,
282
287
spec_version : "1.0" ,
@@ -304,6 +309,12 @@ async function _sillyCardToAGFCard(sillyCard: SillyCardData): Promise<Result<Car
304
309
}
305
310
} ;
306
311
312
+ console . log ( JSON . stringify ( agfCard , null , 2 ) ) ;
313
+ console . log ( "SillyCard" , JSON . stringify ( sillyCard , null , 2 ) ) ;
314
+ console . log ( "cardcreator" , sillyCard . data . creator ) ;
315
+ console . log ( "cardname" , sillyCard . data . name ) ;
316
+ console . log ( "cardtags" , tags ) ;
317
+
307
318
const res = cardSchema . safeParse ( agfCard ) ;
308
319
if ( ! res . success ) {
309
320
const hrError = fromError ( res . error ) ;
0 commit comments