typia 13.0.0-dev.20260430 → 13.0.0-dev.20260501
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/executable/TypiaGenerateWizard.js +55 -8
- package/lib/executable/TypiaGenerateWizard.js.map +1 -1
- package/lib/executable/TypiaGenerateWizard2.mjs +55 -8
- package/lib/executable/TypiaGenerateWizard2.mjs.map +1 -1
- package/lib/transform.d.ts +2 -13
- package/lib/transform.js +4 -11
- package/lib/transform.js.map +1 -1
- package/lib/transform2.mjs +6 -14
- package/lib/transform2.mjs.map +1 -1
- package/native/adapter/cleanup.go +2 -19
- package/native/cmd/ttsc-typia/transform.go +163 -4
- package/native/core/factories/LiteralFactory.go +14 -1
- package/native/core/programmers/llm/LlmApplicationProgrammer.go +16 -4
- package/native/core/programmers/llm/LlmParametersProgrammer.go +10 -37
- package/native/core/programmers/llm/LlmSchemaProgrammer.go +510 -153
- package/package.json +4 -4
- package/src/executable/TypiaGenerateWizard.ts +78 -9
- package/src/transform.ts +6 -11
|
@@ -2,6 +2,9 @@ package llm
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"fmt"
|
|
5
|
+
"reflect"
|
|
6
|
+
"sort"
|
|
7
|
+
"strconv"
|
|
5
8
|
"strings"
|
|
6
9
|
|
|
7
10
|
shimast "github.com/microsoft/typescript-go/shim/ast"
|
|
@@ -41,13 +44,6 @@ type LlmSchemaProgrammer_IWriteProps struct {
|
|
|
41
44
|
var llmSchemaProgrammer_factory = shimast.NewNodeFactory(shimast.NodeFactoryHooks{})
|
|
42
45
|
|
|
43
46
|
func (llmSchemaProgrammerNamespace) Write(props LlmSchemaProgrammer_IWriteProps) *shimast.Node {
|
|
44
|
-
collection := nativejson.JsonSchemasProgrammer.WriteSchemas(struct {
|
|
45
|
-
Version string
|
|
46
|
-
Metadatas []*schemametadata.MetadataSchema
|
|
47
|
-
}{
|
|
48
|
-
Version: "3.1",
|
|
49
|
-
Metadatas: []*schemametadata.MetadataSchema{props.Metadata},
|
|
50
|
-
})
|
|
51
47
|
output := LlmSchemaProgrammer.WriteSchema(struct {
|
|
52
48
|
Metadata *schemametadata.MetadataSchema
|
|
53
49
|
Config map[string]any
|
|
@@ -55,17 +51,13 @@ func (llmSchemaProgrammerNamespace) Write(props LlmSchemaProgrammer_IWriteProps)
|
|
|
55
51
|
Metadata: props.Metadata,
|
|
56
52
|
Config: props.Config,
|
|
57
53
|
})
|
|
58
|
-
schema := nativeiterate.JsonSchema{}
|
|
59
|
-
if len(collection.Schemas) != 0 {
|
|
60
|
-
schema = collection.Schemas[0]
|
|
61
|
-
}
|
|
62
54
|
schemaTypeNode := llmProgrammer_import_type(props.Context, nativeprogrammers.ImportProgrammer_TypeProps{
|
|
63
55
|
File: "typia",
|
|
64
56
|
Name: "ILlmSchema",
|
|
65
57
|
})
|
|
66
58
|
schemaLiteral := llmSchemaProgrammer_factory.NewAsExpression(
|
|
67
59
|
llmSchemaProgrammer_factory.NewSatisfiesExpression(
|
|
68
|
-
|
|
60
|
+
nativefactories.LiteralFactory.Write(output.Schema),
|
|
69
61
|
schemaTypeNode,
|
|
70
62
|
),
|
|
71
63
|
schemaTypeNode,
|
|
@@ -73,13 +65,7 @@ func (llmSchemaProgrammerNamespace) Write(props LlmSchemaProgrammer_IWriteProps)
|
|
|
73
65
|
if len(output.Defs) == 0 {
|
|
74
66
|
return schemaLiteral
|
|
75
67
|
}
|
|
76
|
-
recordType :=
|
|
77
|
-
llmSchemaProgrammer_factory.NewIdentifier("Record"),
|
|
78
|
-
llmSchemaProgrammer_factory.NewNodeList([]*shimast.Node{
|
|
79
|
-
nativefactories.TypeFactory.Keyword("string"),
|
|
80
|
-
schemaTypeNode,
|
|
81
|
-
}),
|
|
82
|
-
)
|
|
68
|
+
recordType := llmSchemaProgrammer_record_type(schemaTypeNode)
|
|
83
69
|
return llmSchemaProgrammer_factory.NewArrowFunction(
|
|
84
70
|
nil,
|
|
85
71
|
nil,
|
|
@@ -90,23 +76,36 @@ func (llmSchemaProgrammerNamespace) Write(props LlmSchemaProgrammer_IWriteProps)
|
|
|
90
76
|
nil,
|
|
91
77
|
llmSchemaProgrammer_factory.NewToken(shimast.KindEqualsGreaterThanToken),
|
|
92
78
|
llmSchemaProgrammer_factory.NewBlock(llmSchemaProgrammer_factory.NewNodeList([]*shimast.Node{
|
|
93
|
-
llmSchemaProgrammer_factory.
|
|
94
|
-
llmSchemaProgrammer_factory.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
props.Config,
|
|
79
|
+
llmSchemaProgrammer_factory.NewExpressionStatement(
|
|
80
|
+
llmSchemaProgrammer_factory.NewCallExpression(
|
|
81
|
+
llmSchemaProgrammer_factory.NewIdentifier("Object.assign"),
|
|
82
|
+
nil,
|
|
83
|
+
nil,
|
|
84
|
+
llmSchemaProgrammer_factory.NewNodeList([]*shimast.Node{
|
|
100
85
|
llmSchemaProgrammer_factory.NewIdentifier("$defs"),
|
|
101
|
-
|
|
102
|
-
|
|
86
|
+
llmSchemaProgrammer_factory.NewAsExpression(
|
|
87
|
+
nativefactories.LiteralFactory.Write(output.Defs),
|
|
88
|
+
llmSchemaProgrammer_record_type(schemaTypeNode),
|
|
89
|
+
),
|
|
90
|
+
}),
|
|
91
|
+
shimast.NodeFlagsNone,
|
|
103
92
|
),
|
|
104
|
-
|
|
105
|
-
)
|
|
93
|
+
),
|
|
94
|
+
llmSchemaProgrammer_factory.NewReturnStatement(schemaLiteral),
|
|
106
95
|
}), true),
|
|
107
96
|
)
|
|
108
97
|
}
|
|
109
98
|
|
|
99
|
+
func llmSchemaProgrammer_record_type(schemaTypeNode *shimast.Node) *shimast.Node {
|
|
100
|
+
return llmSchemaProgrammer_factory.NewTypeReferenceNode(
|
|
101
|
+
llmSchemaProgrammer_factory.NewIdentifier("Record"),
|
|
102
|
+
llmSchemaProgrammer_factory.NewNodeList([]*shimast.Node{
|
|
103
|
+
nativefactories.TypeFactory.Keyword("string"),
|
|
104
|
+
schemaTypeNode,
|
|
105
|
+
}),
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
110
109
|
func (llmSchemaProgrammerNamespace) WriteSchema(props struct {
|
|
111
110
|
Metadata *schemametadata.MetadataSchema
|
|
112
111
|
Config map[string]any
|
|
@@ -121,7 +120,7 @@ func (llmSchemaProgrammerNamespace) WriteSchema(props struct {
|
|
|
121
120
|
defs := map[string]any{}
|
|
122
121
|
schema := map[string]any{}
|
|
123
122
|
if len(collection.Schemas) != 0 {
|
|
124
|
-
schema =
|
|
123
|
+
schema = llmSchemaProgrammer_convert_schema_config(collection.Schemas[0], collection.Components, defs, props.Config)
|
|
125
124
|
}
|
|
126
125
|
return LlmSchemaProgrammer_IOutput{
|
|
127
126
|
Defs: defs,
|
|
@@ -254,56 +253,358 @@ func llmSchemaProgrammer_size(metadata *schemametadata.MetadataSchema) int {
|
|
|
254
253
|
}
|
|
255
254
|
|
|
256
255
|
func llmSchemaProgrammer_convert_schema(schema nativeiterate.JsonSchema, components *nativeiterate.OpenApi_IComponents, defs map[string]any) map[string]any {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
256
|
+
return llmSchemaProgrammer_convert_schema_config(schema, components, defs, nil)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
func llmSchemaProgrammer_convert_schema_config(schema nativeiterate.JsonSchema, components *nativeiterate.OpenApi_IComponents, defs map[string]any, config map[string]any) map[string]any {
|
|
260
|
+
strict := llmSchemaProgrammer_strict(config)
|
|
261
|
+
attribute := llmSchemaProgrammer_attribute(schema)
|
|
262
|
+
union := []map[string]any{}
|
|
263
|
+
|
|
264
|
+
insertConstant := func(value any) {
|
|
265
|
+
typ := llmSchemaProgrammer_constant_type(value)
|
|
266
|
+
for _, elem := range union {
|
|
267
|
+
if elem["type"] == typ {
|
|
268
|
+
elem["enum"] = append(llmSchemaProgrammer_array(elem["enum"]), value)
|
|
269
|
+
return
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
union = append(union, map[string]any{
|
|
273
|
+
"type": typ,
|
|
274
|
+
"enum": []any{value},
|
|
275
|
+
})
|
|
276
|
+
}
|
|
277
|
+
var visitConstant func(nativeiterate.JsonSchema)
|
|
278
|
+
visitConstant = func(input nativeiterate.JsonSchema) {
|
|
279
|
+
if value, ok := input["const"]; ok {
|
|
280
|
+
insertConstant(value)
|
|
281
|
+
return
|
|
282
|
+
}
|
|
283
|
+
for _, elem := range llmSchemaProgrammer_schema_array(input["oneOf"]) {
|
|
284
|
+
visitConstant(elem)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
var visit func(nativeiterate.JsonSchema)
|
|
289
|
+
visit = func(input nativeiterate.JsonSchema) {
|
|
290
|
+
if oneOf := llmSchemaProgrammer_schema_array(input["oneOf"]); len(oneOf) != 0 {
|
|
291
|
+
for _, elem := range oneOf {
|
|
292
|
+
visit(elem)
|
|
293
|
+
}
|
|
294
|
+
return
|
|
295
|
+
}
|
|
296
|
+
if ref, ok := input["$ref"].(string); ok {
|
|
297
|
+
key := llmSchemaProgrammer_ref_key(ref)
|
|
298
|
+
if _, exists := defs[key]; exists == false {
|
|
299
|
+
defs[key] = map[string]any{}
|
|
300
|
+
if target, found := llmSchemaProgrammer_component(components, key); found {
|
|
301
|
+
defs[key] = llmSchemaProgrammer_convert_schema_config(target, components, defs, config)
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
union = append(union, map[string]any{"$ref": "#/$defs/" + key})
|
|
305
|
+
return
|
|
306
|
+
}
|
|
307
|
+
if input["type"] == "object" {
|
|
308
|
+
properties := map[string]any{}
|
|
309
|
+
if raw, ok := llmSchemaProgrammer_schema_map(input["properties"]); ok {
|
|
310
|
+
keys := make([]string, 0, len(raw))
|
|
311
|
+
for key := range raw {
|
|
312
|
+
keys = append(keys, key)
|
|
313
|
+
}
|
|
314
|
+
sort.Strings(keys)
|
|
315
|
+
for _, key := range keys {
|
|
316
|
+
if value, ok := llmSchemaProgrammer_schema_from_any(raw[key]); ok {
|
|
317
|
+
properties[key] = llmSchemaProgrammer_convert_schema_config(value, components, defs, config)
|
|
270
318
|
}
|
|
271
319
|
}
|
|
272
320
|
}
|
|
273
|
-
|
|
321
|
+
output := llmSchemaProgrammer_clone(input)
|
|
322
|
+
output["properties"] = properties
|
|
323
|
+
if additional, ok := llmSchemaProgrammer_schema_from_any(input["additionalProperties"]); ok {
|
|
324
|
+
output["additionalProperties"] = llmSchemaProgrammer_convert_schema_config(additional, components, defs, config)
|
|
325
|
+
} else if strict {
|
|
326
|
+
output["additionalProperties"] = false
|
|
327
|
+
} else if value, ok := input["additionalProperties"]; ok {
|
|
328
|
+
output["additionalProperties"] = value
|
|
329
|
+
}
|
|
330
|
+
if _, ok := input["required"]; ok {
|
|
331
|
+
output["required"] = input["required"]
|
|
332
|
+
} else {
|
|
333
|
+
output["required"] = []any{}
|
|
334
|
+
}
|
|
335
|
+
if strict {
|
|
336
|
+
if description := llmSchemaProgrammer_json_descriptor_take(input); description != nil {
|
|
337
|
+
output["description"] = *description
|
|
338
|
+
} else {
|
|
339
|
+
delete(output, "description")
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
union = append(union, output)
|
|
343
|
+
return
|
|
274
344
|
}
|
|
275
|
-
if
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
345
|
+
if input["type"] == "array" {
|
|
346
|
+
output := llmSchemaProgrammer_clone(input)
|
|
347
|
+
if items, ok := llmSchemaProgrammer_schema_from_any(input["items"]); ok {
|
|
348
|
+
output["items"] = llmSchemaProgrammer_convert_schema_config(items, components, defs, config)
|
|
349
|
+
}
|
|
350
|
+
if strict {
|
|
351
|
+
output = llmSchemaProgrammer_shift_array(output)
|
|
352
|
+
}
|
|
353
|
+
union = append(union, output)
|
|
354
|
+
return
|
|
279
355
|
}
|
|
280
|
-
if
|
|
281
|
-
|
|
282
|
-
|
|
356
|
+
if input["type"] == "string" {
|
|
357
|
+
output := llmSchemaProgrammer_clone(input)
|
|
358
|
+
if strict {
|
|
359
|
+
output = llmSchemaProgrammer_shift_string(output)
|
|
283
360
|
}
|
|
284
|
-
|
|
361
|
+
union = append(union, output)
|
|
362
|
+
return
|
|
363
|
+
}
|
|
364
|
+
if input["type"] == "number" || input["type"] == "integer" {
|
|
365
|
+
output := llmSchemaProgrammer_clone(input)
|
|
366
|
+
if strict {
|
|
367
|
+
output = llmSchemaProgrammer_shift_numeric(output)
|
|
368
|
+
}
|
|
369
|
+
union = append(union, output)
|
|
370
|
+
return
|
|
371
|
+
}
|
|
372
|
+
if _, ok := input["const"]; ok {
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
union = append(union, llmSchemaProgrammer_clone(input))
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
visitConstant(schema)
|
|
379
|
+
visit(schema)
|
|
380
|
+
|
|
381
|
+
if len(union) == 0 {
|
|
382
|
+
output := llmSchemaProgrammer_clone(attribute)
|
|
383
|
+
output["type"] = nil
|
|
384
|
+
return output
|
|
385
|
+
}
|
|
386
|
+
if len(union) == 1 {
|
|
387
|
+
output := llmSchemaProgrammer_merge(attribute, union[0])
|
|
388
|
+
if strict && llmSchemaProgrammer_is_reference(union[0]) {
|
|
389
|
+
delete(output, "description")
|
|
390
|
+
} else if description, ok := union[0]["description"]; ok {
|
|
391
|
+
output["description"] = description
|
|
392
|
+
} else if description, ok := attribute["description"]; ok {
|
|
393
|
+
output["description"] = description
|
|
394
|
+
}
|
|
395
|
+
return output
|
|
396
|
+
}
|
|
397
|
+
anyOf := make([]any, 0, len(union))
|
|
398
|
+
for _, elem := range union {
|
|
399
|
+
output := llmSchemaProgrammer_clone(elem)
|
|
400
|
+
if strict && llmSchemaProgrammer_is_reference(elem) {
|
|
401
|
+
delete(output, "description")
|
|
402
|
+
}
|
|
403
|
+
anyOf = append(anyOf, output)
|
|
404
|
+
}
|
|
405
|
+
output := llmSchemaProgrammer_clone(attribute)
|
|
406
|
+
output["anyOf"] = anyOf
|
|
407
|
+
if x := llmSchemaProgrammer_discriminator(schema, union); x != nil {
|
|
408
|
+
output["x-discriminator"] = x
|
|
409
|
+
}
|
|
410
|
+
return output
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
func llmSchemaProgrammer_strict(config map[string]any) bool {
|
|
414
|
+
strict, _ := config["strict"].(bool)
|
|
415
|
+
return strict
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
func llmSchemaProgrammer_attribute(schema nativeiterate.JsonSchema) map[string]any {
|
|
419
|
+
output := map[string]any{}
|
|
420
|
+
for _, key := range []string{
|
|
421
|
+
"title",
|
|
422
|
+
"description",
|
|
423
|
+
"deprecated",
|
|
424
|
+
"readOnly",
|
|
425
|
+
"writeOnly",
|
|
426
|
+
"example",
|
|
427
|
+
"examples",
|
|
428
|
+
} {
|
|
429
|
+
if value, ok := schema[key]; ok && llmSchemaProgrammer_is_nil_like(value) == false {
|
|
430
|
+
output[key] = value
|
|
285
431
|
}
|
|
286
|
-
output[key] = llmSchemaProgrammer_convert_value(value, components, defs)
|
|
287
432
|
}
|
|
288
|
-
|
|
289
|
-
if x
|
|
290
|
-
output[
|
|
433
|
+
for key, value := range schema {
|
|
434
|
+
if strings.HasPrefix(key, "x-") && llmSchemaProgrammer_is_nil_like(value) == false {
|
|
435
|
+
output[key] = value
|
|
291
436
|
}
|
|
292
437
|
}
|
|
293
438
|
return output
|
|
294
439
|
}
|
|
295
440
|
|
|
296
|
-
func
|
|
441
|
+
func llmSchemaProgrammer_clone(input map[string]any) map[string]any {
|
|
442
|
+
output := map[string]any{}
|
|
443
|
+
for key, value := range input {
|
|
444
|
+
if llmSchemaProgrammer_is_nil_like(value) {
|
|
445
|
+
continue
|
|
446
|
+
}
|
|
447
|
+
output[key] = value
|
|
448
|
+
}
|
|
449
|
+
return output
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
func llmSchemaProgrammer_is_nil_like(value any) bool {
|
|
453
|
+
if value == nil {
|
|
454
|
+
return true
|
|
455
|
+
}
|
|
456
|
+
reflected := reflect.ValueOf(value)
|
|
457
|
+
switch reflected.Kind() {
|
|
458
|
+
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
|
459
|
+
return reflected.IsNil()
|
|
460
|
+
default:
|
|
461
|
+
return false
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
func llmSchemaProgrammer_merge(left map[string]any, right map[string]any) map[string]any {
|
|
466
|
+
output := llmSchemaProgrammer_clone(left)
|
|
467
|
+
for key, value := range right {
|
|
468
|
+
output[key] = value
|
|
469
|
+
}
|
|
470
|
+
return output
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
func llmSchemaProgrammer_array(value any) []any {
|
|
474
|
+
switch v := value.(type) {
|
|
475
|
+
case []any:
|
|
476
|
+
return append([]any{}, v...)
|
|
477
|
+
case []string:
|
|
478
|
+
output := make([]any, 0, len(v))
|
|
479
|
+
for _, elem := range v {
|
|
480
|
+
output = append(output, elem)
|
|
481
|
+
}
|
|
482
|
+
return output
|
|
483
|
+
case []nativeiterate.JsonSchema:
|
|
484
|
+
output := make([]any, 0, len(v))
|
|
485
|
+
for _, elem := range v {
|
|
486
|
+
output = append(output, elem)
|
|
487
|
+
}
|
|
488
|
+
return output
|
|
489
|
+
default:
|
|
490
|
+
return []any{}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
func llmSchemaProgrammer_schema_array(value any) []nativeiterate.JsonSchema {
|
|
495
|
+
switch v := value.(type) {
|
|
496
|
+
case []nativeiterate.JsonSchema:
|
|
497
|
+
return append([]nativeiterate.JsonSchema{}, v...)
|
|
498
|
+
case []map[string]any:
|
|
499
|
+
output := make([]nativeiterate.JsonSchema, 0, len(v))
|
|
500
|
+
for _, elem := range v {
|
|
501
|
+
output = append(output, nativeiterate.JsonSchema(elem))
|
|
502
|
+
}
|
|
503
|
+
return output
|
|
504
|
+
case []any:
|
|
505
|
+
output := make([]nativeiterate.JsonSchema, 0, len(v))
|
|
506
|
+
for _, elem := range v {
|
|
507
|
+
if schema, ok := llmSchemaProgrammer_schema_from_any(elem); ok {
|
|
508
|
+
output = append(output, schema)
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return output
|
|
512
|
+
default:
|
|
513
|
+
return nil
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
func llmSchemaProgrammer_schema_map(value any) (map[string]any, bool) {
|
|
518
|
+
switch v := value.(type) {
|
|
519
|
+
case nativeiterate.JsonSchema:
|
|
520
|
+
output := map[string]any{}
|
|
521
|
+
for key, value := range v {
|
|
522
|
+
output[key] = value
|
|
523
|
+
}
|
|
524
|
+
return output, true
|
|
525
|
+
case map[string]any:
|
|
526
|
+
return v, true
|
|
527
|
+
default:
|
|
528
|
+
return nil, false
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
func llmSchemaProgrammer_schema_from_any(value any) (nativeiterate.JsonSchema, bool) {
|
|
533
|
+
switch v := value.(type) {
|
|
534
|
+
case nativeiterate.JsonSchema:
|
|
535
|
+
return v, true
|
|
536
|
+
case map[string]any:
|
|
537
|
+
return nativeiterate.JsonSchema(v), true
|
|
538
|
+
default:
|
|
539
|
+
return nil, false
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
func llmSchemaProgrammer_constant_type(value any) string {
|
|
544
|
+
reflected := reflect.ValueOf(value)
|
|
545
|
+
if reflected.IsValid() == false {
|
|
546
|
+
return fmt.Sprintf("%T", value)
|
|
547
|
+
}
|
|
548
|
+
switch reflected.Kind() {
|
|
549
|
+
case reflect.Bool:
|
|
550
|
+
return "boolean"
|
|
551
|
+
case reflect.String:
|
|
552
|
+
return "string"
|
|
553
|
+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
|
554
|
+
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
|
555
|
+
reflect.Float32, reflect.Float64:
|
|
556
|
+
return "number"
|
|
557
|
+
default:
|
|
558
|
+
return fmt.Sprintf("%T", value)
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
func llmSchemaProgrammer_ref_key(ref string) string {
|
|
563
|
+
if strings.Contains(ref, "#/components/schemas/") {
|
|
564
|
+
return strings.Split(ref, "#/components/schemas/")[1]
|
|
565
|
+
}
|
|
566
|
+
index := strings.LastIndex(ref, "/")
|
|
567
|
+
if index == -1 {
|
|
568
|
+
return ref
|
|
569
|
+
}
|
|
570
|
+
return ref[index+1:]
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
func llmSchemaProgrammer_component(components *nativeiterate.OpenApi_IComponents, key string) (nativeiterate.JsonSchema, bool) {
|
|
574
|
+
if components == nil || components.Schemas == nil {
|
|
575
|
+
return nil, false
|
|
576
|
+
}
|
|
577
|
+
schema, ok := components.Schemas[key]
|
|
578
|
+
return schema, ok
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
func llmSchemaProgrammer_is_reference(schema map[string]any) bool {
|
|
582
|
+
_, ok := schema["$ref"].(string)
|
|
583
|
+
return ok
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
func llmSchemaProgrammer_is_null(schema map[string]any) bool {
|
|
587
|
+
return schema["type"] == "null"
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
func llmSchemaProgrammer_discriminator(schema nativeiterate.JsonSchema, union []map[string]any) map[string]any {
|
|
591
|
+
discriminator, ok := llmSchemaProgrammer_schema_map(schema["discriminator"])
|
|
592
|
+
if ok == false || len(llmSchemaProgrammer_schema_array(schema["oneOf"])) != len(union) {
|
|
593
|
+
return nil
|
|
594
|
+
}
|
|
595
|
+
for _, elem := range union {
|
|
596
|
+
if llmSchemaProgrammer_is_reference(elem) == false && llmSchemaProgrammer_is_null(elem) == false {
|
|
597
|
+
return nil
|
|
598
|
+
}
|
|
599
|
+
}
|
|
297
600
|
output := map[string]any{}
|
|
298
601
|
if propertyName, ok := discriminator["propertyName"].(string); ok {
|
|
299
602
|
output["propertyName"] = propertyName
|
|
300
603
|
}
|
|
301
|
-
if rawMapping, ok := discriminator["mapping"]
|
|
604
|
+
if rawMapping, ok := llmSchemaProgrammer_schema_map(discriminator["mapping"]); ok {
|
|
302
605
|
mapping := map[string]any{}
|
|
303
606
|
for key, value := range rawMapping {
|
|
304
|
-
|
|
305
|
-
name := ref[strings.LastIndex(ref, "/")+1:]
|
|
306
|
-
mapping[key] = "#/$defs/" + name
|
|
607
|
+
mapping[key] = "#/$defs/" + llmSchemaProgrammer_ref_key(fmt.Sprint(value))
|
|
307
608
|
}
|
|
308
609
|
output["mapping"] = mapping
|
|
309
610
|
}
|
|
@@ -313,113 +614,169 @@ func llmSchemaProgrammer_convert_discriminator(discriminator map[string]any) map
|
|
|
313
614
|
return output
|
|
314
615
|
}
|
|
315
616
|
|
|
316
|
-
func
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
617
|
+
func llmSchemaProgrammer_shift_array(schema map[string]any) map[string]any {
|
|
618
|
+
output := llmSchemaProgrammer_clone(schema)
|
|
619
|
+
tags := []string{}
|
|
620
|
+
if value, ok := output["minItems"]; ok {
|
|
621
|
+
tags = append(tags, "@minItems "+fmt.Sprint(value))
|
|
622
|
+
delete(output, "minItems")
|
|
623
|
+
}
|
|
624
|
+
if value, ok := output["maxItems"]; ok {
|
|
625
|
+
tags = append(tags, "@maxItems "+fmt.Sprint(value))
|
|
626
|
+
delete(output, "maxItems")
|
|
627
|
+
}
|
|
628
|
+
if value, ok := output["uniqueItems"].(bool); ok {
|
|
629
|
+
if value {
|
|
630
|
+
tags = append(tags, "@uniqueItems")
|
|
326
631
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
632
|
+
delete(output, "uniqueItems")
|
|
633
|
+
}
|
|
634
|
+
llmSchemaProgrammer_write_tag_with_description(output, tags)
|
|
635
|
+
return output
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
func llmSchemaProgrammer_shift_numeric(schema map[string]any) map[string]any {
|
|
639
|
+
output := llmSchemaProgrammer_clone(schema)
|
|
640
|
+
llmSchemaProgrammer_emend_exclusive(output)
|
|
641
|
+
tags := []string{}
|
|
642
|
+
for _, key := range []string{"minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf"} {
|
|
643
|
+
if value, ok := output[key]; ok {
|
|
644
|
+
tags = append(tags, "@"+key+" "+fmt.Sprint(value))
|
|
645
|
+
delete(output, key)
|
|
332
646
|
}
|
|
333
|
-
return out
|
|
334
|
-
default:
|
|
335
|
-
return value
|
|
336
647
|
}
|
|
648
|
+
llmSchemaProgrammer_write_tag_with_description(output, tags)
|
|
649
|
+
delete(output, "default")
|
|
650
|
+
return output
|
|
337
651
|
}
|
|
338
652
|
|
|
339
|
-
func
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
)
|
|
346
|
-
|
|
347
|
-
defs = llmSchemaProgrammer_factory.NewObjectLiteralExpression(llmSchemaProgrammer_factory.NewNodeList(nil), false)
|
|
653
|
+
func llmSchemaProgrammer_shift_string(schema map[string]any) map[string]any {
|
|
654
|
+
output := llmSchemaProgrammer_clone(schema)
|
|
655
|
+
tags := []string{}
|
|
656
|
+
for _, key := range []string{"minLength", "maxLength", "format", "pattern", "contentMediaType", "default"} {
|
|
657
|
+
if value, ok := output[key]; ok {
|
|
658
|
+
tags = append(tags, "@"+key+" "+fmt.Sprint(value))
|
|
659
|
+
delete(output, key)
|
|
660
|
+
}
|
|
348
661
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
llmProgrammer_object_property("components", nativefactories.LiteralFactory.Write(components)),
|
|
352
|
-
llmProgrammer_object_property("schema", nativefactories.LiteralFactory.Write(schema)),
|
|
353
|
-
llmProgrammer_object_property("$defs", defs),
|
|
354
|
-
})
|
|
662
|
+
llmSchemaProgrammer_write_tag_with_description(output, tags)
|
|
663
|
+
return output
|
|
355
664
|
}
|
|
356
665
|
|
|
357
|
-
func
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
)
|
|
368
|
-
return nativefactories.ExpressionFactory.SelfCall(
|
|
369
|
-
llmSchemaProgrammer_factory.NewBlock(llmSchemaProgrammer_factory.NewNodeList([]*shimast.Node{
|
|
370
|
-
nativefactories.StatementFactory.Constant(nativefactories.StatementFactory_ConstantProps{
|
|
371
|
-
Name: "__result",
|
|
372
|
-
Value: call,
|
|
373
|
-
}),
|
|
374
|
-
llmSchemaProgrammer_factory.NewIfStatement(
|
|
375
|
-
llmSchemaProgrammer_factory.NewBinaryExpression(
|
|
376
|
-
nil,
|
|
377
|
-
nativefactories.IdentifierFactory.Access(result, "success"),
|
|
378
|
-
nil,
|
|
379
|
-
llmSchemaProgrammer_factory.NewToken(shimast.KindEqualsEqualsEqualsToken),
|
|
380
|
-
llmSchemaProgrammer_factory.NewKeywordExpression(shimast.KindFalseKeyword),
|
|
381
|
-
),
|
|
382
|
-
llmSchemaProgrammer_factory.NewThrowStatement(llmSchemaProgrammer_factory.NewNewExpression(
|
|
383
|
-
llmSchemaProgrammer_factory.NewIdentifier("Error"),
|
|
384
|
-
nil,
|
|
385
|
-
llmSchemaProgrammer_factory.NewNodeList([]*shimast.Node{
|
|
386
|
-
llmSchemaProgrammer_factory.NewStringLiteral("failed to convert JSON schema to LLM schema: "+code, shimast.TokenFlagsNone),
|
|
387
|
-
}),
|
|
388
|
-
)),
|
|
389
|
-
nil,
|
|
390
|
-
),
|
|
391
|
-
llmSchemaProgrammer_factory.NewReturnStatement(nativefactories.IdentifierFactory.Access(result, "value")),
|
|
392
|
-
}), true),
|
|
393
|
-
)
|
|
666
|
+
func llmSchemaProgrammer_write_tag_with_description(schema map[string]any, tags []string) {
|
|
667
|
+
if len(tags) == 0 {
|
|
668
|
+
return
|
|
669
|
+
}
|
|
670
|
+
lines := []string{}
|
|
671
|
+
if description, ok := schema["description"].(string); ok && len(description) != 0 {
|
|
672
|
+
lines = append(lines, description, "\n")
|
|
673
|
+
}
|
|
674
|
+
lines = append(lines, tags...)
|
|
675
|
+
schema["description"] = strings.Join(lines, "\n")
|
|
394
676
|
}
|
|
395
677
|
|
|
396
|
-
func
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
678
|
+
func llmSchemaProgrammer_emend_exclusive(schema map[string]any) {
|
|
679
|
+
if exclusiveMinimum, ok := llmSchemaProgrammer_number(schema["exclusiveMinimum"]); ok {
|
|
680
|
+
if minimum, found := llmSchemaProgrammer_number(schema["minimum"]); found {
|
|
681
|
+
if minimum > exclusiveMinimum {
|
|
682
|
+
schema["minimum"] = minimum
|
|
683
|
+
delete(schema, "exclusiveMinimum")
|
|
684
|
+
} else {
|
|
685
|
+
schema["exclusiveMinimum"] = exclusiveMinimum
|
|
686
|
+
delete(schema, "minimum")
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
if exclusiveMaximum, ok := llmSchemaProgrammer_number(schema["exclusiveMaximum"]); ok {
|
|
691
|
+
if maximum, found := llmSchemaProgrammer_number(schema["maximum"]); found {
|
|
692
|
+
if maximum < exclusiveMaximum {
|
|
693
|
+
schema["maximum"] = maximum
|
|
694
|
+
delete(schema, "exclusiveMaximum")
|
|
695
|
+
} else {
|
|
696
|
+
schema["exclusiveMaximum"] = exclusiveMaximum
|
|
697
|
+
delete(schema, "maximum")
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
404
701
|
}
|
|
405
702
|
|
|
406
|
-
func
|
|
407
|
-
|
|
408
|
-
|
|
703
|
+
func llmSchemaProgrammer_number(value any) (float64, bool) {
|
|
704
|
+
reflected := reflect.ValueOf(value)
|
|
705
|
+
if reflected.IsValid() == false {
|
|
706
|
+
return 0, false
|
|
707
|
+
}
|
|
708
|
+
switch reflected.Kind() {
|
|
709
|
+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
710
|
+
return float64(reflected.Int()), true
|
|
711
|
+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
712
|
+
return float64(reflected.Uint()), true
|
|
713
|
+
case reflect.Float32, reflect.Float64:
|
|
714
|
+
return reflected.Convert(reflect.TypeOf(float64(0))).Float(), true
|
|
715
|
+
default:
|
|
716
|
+
return 0, false
|
|
409
717
|
}
|
|
410
|
-
return nativefactories.LiteralFactory.Write(config)
|
|
411
718
|
}
|
|
412
719
|
|
|
413
|
-
func
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
720
|
+
func llmSchemaProgrammer_json_descriptor_take(schema nativeiterate.JsonSchema) *string {
|
|
721
|
+
parts := []string{}
|
|
722
|
+
if description, ok := schema["description"].(string); ok && len(description) != 0 {
|
|
723
|
+
parts = append(parts, description)
|
|
724
|
+
}
|
|
725
|
+
properties, ok := llmSchemaProgrammer_schema_map(schema["properties"])
|
|
726
|
+
if ok {
|
|
727
|
+
keys := make([]string, 0, len(properties))
|
|
728
|
+
for key := range properties {
|
|
729
|
+
keys = append(keys, key)
|
|
730
|
+
}
|
|
731
|
+
sort.Strings(keys)
|
|
732
|
+
for _, key := range keys {
|
|
733
|
+
value, ok := llmSchemaProgrammer_schema_from_any(properties[key])
|
|
734
|
+
if ok == false || llmSchemaProgrammer_is_reference(value) == false {
|
|
735
|
+
continue
|
|
736
|
+
}
|
|
737
|
+
description, ok := value["description"].(string)
|
|
738
|
+
if ok == false || len(description) == 0 {
|
|
739
|
+
continue
|
|
740
|
+
}
|
|
741
|
+
name := key
|
|
742
|
+
if llmSchemaProgrammer_is_variable_name(key) == false {
|
|
743
|
+
name = strconv.Quote(key)
|
|
744
|
+
}
|
|
745
|
+
parts = append(parts, "### Description of {@link "+name+"} property:\n\n"+llmSchemaProgrammer_quote_description(description))
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
if len(parts) == 0 {
|
|
749
|
+
return nil
|
|
750
|
+
}
|
|
751
|
+
output := strings.Join(parts, "\n\n")
|
|
752
|
+
return &output
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
func llmSchemaProgrammer_quote_description(description string) string {
|
|
756
|
+
lines := strings.Split(description, "\n")
|
|
757
|
+
for i, line := range lines {
|
|
758
|
+
lines[i] = "> " + line
|
|
759
|
+
}
|
|
760
|
+
return strings.Join(lines, "\n")
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
func llmSchemaProgrammer_is_variable_name(str string) bool {
|
|
764
|
+
if len(str) == 0 {
|
|
765
|
+
return false
|
|
766
|
+
}
|
|
767
|
+
for i, r := range str {
|
|
768
|
+
if i == 0 {
|
|
769
|
+
if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || r == '_' || r == '$' {
|
|
770
|
+
continue
|
|
771
|
+
}
|
|
772
|
+
return false
|
|
773
|
+
}
|
|
774
|
+
if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '_' || r == '$' {
|
|
775
|
+
continue
|
|
776
|
+
}
|
|
777
|
+
return false
|
|
421
778
|
}
|
|
422
|
-
return
|
|
779
|
+
return true
|
|
423
780
|
}
|
|
424
781
|
|
|
425
782
|
func llmProgrammer_internal(context nativecontext.ITypiaContext, name string) *shimast.Node {
|