@@ -6,6 +6,7 @@ import { IOpenApiSchemaError } from "../../structures/IOpenApiSchemaError";
6
6
import { IResult } from "../../typings/IResult" ;
7
7
import { ChatGptTypeChecker } from "../../utils/ChatGptTypeChecker" ;
8
8
import { LlmTypeCheckerV3_1 } from "../../utils/LlmTypeCheckerV3_1" ;
9
+ import { NamingConvention } from "../../utils/NamingConvention" ;
9
10
import { OpenApiTypeChecker } from "../../utils/OpenApiTypeChecker" ;
10
11
import { LlmDescriptionInverter } from "./LlmDescriptionInverter" ;
11
12
import { LlmSchemaV3_1Composer } from "./LlmSchemaV3_1Composer" ;
@@ -223,12 +224,17 @@ export namespace ChatGptSchemaComposer {
223
224
SEPARATORS
224
225
----------------------------------------------------------- */
225
226
export const separateParameters = ( props : {
226
- predicate : ( schema : IChatGptSchema ) => boolean ;
227
227
parameters : IChatGptSchema . IParameters ;
228
+ predicate : ( schema : IChatGptSchema ) => boolean ;
229
+ convention ?: ( key : string , type : "llm" | "human" ) => string ;
228
230
} ) : ILlmFunction . ISeparated < "chatgpt" > => {
231
+ const convention =
232
+ props . convention ??
233
+ ( ( key , type ) => `${ key } .${ NamingConvention . capitalize ( type ) } ` ) ;
229
234
const [ llm , human ] = separateObject ( {
230
- $defs : props . parameters . $defs ,
231
235
predicate : props . predicate ,
236
+ convention,
237
+ $defs : props . parameters . $defs ,
232
238
schema : props . parameters ,
233
239
} ) ;
234
240
if ( llm === null || human === null )
@@ -269,8 +275,9 @@ export namespace ChatGptSchemaComposer {
269
275
} ;
270
276
271
277
const separateStation = ( props : {
272
- $defs : Record < string , IChatGptSchema > ;
273
278
predicate : ( schema : IChatGptSchema ) => boolean ;
279
+ convention : ( key : string , type : "llm" | "human" ) => string ;
280
+ $defs : Record < string , IChatGptSchema > ;
274
281
schema : IChatGptSchema ;
275
282
} ) : [ IChatGptSchema | null , IChatGptSchema | null ] => {
276
283
if ( props . predicate ( props . schema ) === true ) return [ null , props . schema ] ;
@@ -281,33 +288,38 @@ export namespace ChatGptSchemaComposer {
281
288
return [ props . schema , null ] ;
282
289
else if ( ChatGptTypeChecker . isObject ( props . schema ) )
283
290
return separateObject ( {
284
- $defs : props . $defs ,
285
291
predicate : props . predicate ,
292
+ convention : props . convention ,
293
+ $defs : props . $defs ,
286
294
schema : props . schema ,
287
295
} ) ;
288
296
else if ( ChatGptTypeChecker . isArray ( props . schema ) )
289
297
return separateArray ( {
290
- $defs : props . $defs ,
291
298
predicate : props . predicate ,
299
+ convention : props . convention ,
300
+ $defs : props . $defs ,
292
301
schema : props . schema ,
293
302
} ) ;
294
303
else if ( ChatGptTypeChecker . isReference ( props . schema ) )
295
304
return separateReference ( {
296
- $defs : props . $defs ,
297
305
predicate : props . predicate ,
306
+ convention : props . convention ,
307
+ $defs : props . $defs ,
298
308
schema : props . schema ,
299
309
} ) ;
300
310
return [ props . schema , null ] ;
301
311
} ;
302
312
303
313
const separateArray = ( props : {
304
- $defs : Record < string , IChatGptSchema > ;
305
314
predicate : ( schema : IChatGptSchema ) => boolean ;
315
+ convention : ( key : string , type : "llm" | "human" ) => string ;
316
+ $defs : Record < string , IChatGptSchema > ;
306
317
schema : IChatGptSchema . IArray ;
307
318
} ) : [ IChatGptSchema . IArray | null , IChatGptSchema . IArray | null ] => {
308
319
const [ x , y ] = separateStation ( {
309
- $defs : props . $defs ,
310
320
predicate : props . predicate ,
321
+ convention : props . convention ,
322
+ $defs : props . $defs ,
311
323
schema : props . schema . items ,
312
324
} ) ;
313
325
return [
@@ -329,6 +341,7 @@ export namespace ChatGptSchemaComposer {
329
341
const separateObject = ( props : {
330
342
$defs : Record < string , IChatGptSchema > ;
331
343
predicate : ( schema : IChatGptSchema ) => boolean ;
344
+ convention : ( key : string , type : "llm" | "human" ) => string ;
332
345
schema : IChatGptSchema . IObject ;
333
346
} ) : [ IChatGptSchema . IObject | null , IChatGptSchema . IObject | null ] => {
334
347
// EMPTY OBJECT
@@ -350,8 +363,9 @@ export namespace ChatGptSchemaComposer {
350
363
351
364
for ( const [ key , value ] of Object . entries ( props . schema . properties ?? { } ) ) {
352
365
const [ x , y ] = separateStation ( {
353
- $defs : props . $defs ,
354
366
predicate : props . predicate ,
367
+ convention : props . convention ,
368
+ $defs : props . $defs ,
355
369
schema : value ,
356
370
} ) ;
357
371
if ( x !== null ) llm . properties [ key ] = x ;
@@ -362,8 +376,9 @@ export namespace ChatGptSchemaComposer {
362
376
props . schema . additionalProperties !== null
363
377
) {
364
378
const [ dx , dy ] = separateStation ( {
365
- $defs : props . $defs ,
366
379
predicate : props . predicate ,
380
+ convention : props . convention ,
381
+ $defs : props . $defs ,
367
382
schema : props . schema . additionalProperties ,
368
383
} ) ;
369
384
llm . additionalProperties = dx ?? false ;
@@ -380,45 +395,49 @@ export namespace ChatGptSchemaComposer {
380
395
} ;
381
396
382
397
const separateReference = ( props : {
383
- $defs : Record < string , IChatGptSchema > ;
384
398
predicate : ( schema : IChatGptSchema ) => boolean ;
399
+ convention : ( key : string , type : "llm" | "human" ) => string ;
400
+ $defs : Record < string , IChatGptSchema > ;
385
401
schema : IChatGptSchema . IReference ;
386
402
} ) : [ IChatGptSchema . IReference | null , IChatGptSchema . IReference | null ] => {
387
403
const key : string = props . schema . $ref . split ( "#/$defs/" ) [ 1 ] ;
404
+ const humanKey : string = props . convention ( key , "human" ) ;
405
+ const llmKey : string = props . convention ( key , "llm" ) ;
388
406
389
407
// FIND EXISTING
390
- if ( props . $defs ?. [ ` ${ key } .Human` ] || props . $defs ?. [ ` ${ key } .Llm` ] )
408
+ if ( props . $defs ?. [ humanKey ] || props . $defs ?. [ llmKey ] )
391
409
return [
392
- props . $defs ?. [ ` ${ key } .Llm` ]
410
+ props . $defs ?. [ llmKey ]
393
411
? {
394
412
...props . schema ,
395
- $ref : `#/$defs/${ key } .Llm ` ,
413
+ $ref : `#/$defs/${ llmKey } ` ,
396
414
}
397
415
: null ,
398
- props . $defs ?. [ ` ${ key } .Human` ]
416
+ props . $defs ?. [ humanKey ]
399
417
? {
400
418
...props . schema ,
401
- $ref : `#/$defs/${ key } .Human ` ,
419
+ $ref : `#/$defs/${ humanKey } ` ,
402
420
}
403
421
: null ,
404
422
] ;
405
423
406
424
// PRE-ASSIGNMENT
407
- props . $defs ! [ ` ${ key } .Llm` ] = { } ;
408
- props . $defs ! [ ` ${ key } .Human` ] = { } ;
425
+ props . $defs ! [ llmKey ] = { } ;
426
+ props . $defs ! [ humanKey ] = { } ;
409
427
410
428
// DO COMPOSE
411
429
const schema : IChatGptSchema = props . $defs ?. [ key ] ! ;
412
430
const [ llm , human ] = separateStation ( {
413
- $defs : props . $defs ,
414
431
predicate : props . predicate ,
432
+ convention : props . convention ,
433
+ $defs : props . $defs ,
415
434
schema,
416
435
} ) ;
417
436
418
437
// ONLY ONE
419
438
if ( llm === null || human === null ) {
420
- delete props . $defs [ ` ${ key } .Llm` ] ;
421
- delete props . $defs [ ` ${ key } .Human` ] ;
439
+ delete props . $defs [ llmKey ] ;
440
+ delete props . $defs [ humanKey ] ;
422
441
return llm === null ? [ null , props . schema ] : [ props . schema , null ] ;
423
442
}
424
443
@@ -427,13 +446,13 @@ export namespace ChatGptSchemaComposer {
427
446
llm !== null
428
447
? {
429
448
...props . schema ,
430
- $ref : `#/$defs/${ key } .Llm ` ,
449
+ $ref : `#/$defs/${ llmKey } ` ,
431
450
}
432
451
: null ,
433
452
human !== null
434
453
? {
435
454
...props . schema ,
436
- $ref : `#/$defs/${ key } .Human ` ,
455
+ $ref : `#/$defs/${ humanKey } ` ,
437
456
}
438
457
: null ,
439
458
] ;
0 commit comments