typia 7.0.0-dev.20241114 → 7.0.0-dev.20241122

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.
Files changed (40) hide show
  1. package/README.md +3 -3
  2. package/lib/executable/TypiaSetupWizard.js +1 -1
  3. package/lib/executable/TypiaSetupWizard.js.map +1 -1
  4. package/lib/factories/MetadataFactory.js +1 -1
  5. package/lib/factories/MetadataFactory.js.map +1 -1
  6. package/lib/index.mjs.map +1 -1
  7. package/lib/internal/_isUniqueItems.d.ts +1 -0
  8. package/lib/internal/_isUniqueItems.js +140 -0
  9. package/lib/internal/_isUniqueItems.js.map +1 -0
  10. package/lib/internal/_llmApplicationFinalize.d.ts +1 -1
  11. package/lib/internal/_llmApplicationFinalize.js +14 -6
  12. package/lib/internal/_llmApplicationFinalize.js.map +1 -1
  13. package/lib/llm.d.ts +4 -3
  14. package/lib/programmers/internal/check_array_length.js +8 -11
  15. package/lib/programmers/internal/check_array_length.js.map +1 -1
  16. package/lib/programmers/internal/check_bigint.js +8 -11
  17. package/lib/programmers/internal/check_bigint.js.map +1 -1
  18. package/lib/programmers/internal/check_number.js +8 -11
  19. package/lib/programmers/internal/check_number.js.map +1 -1
  20. package/lib/programmers/internal/check_string.js +8 -11
  21. package/lib/programmers/internal/check_string.js.map +1 -1
  22. package/lib/programmers/llm/LlmApplicationProgrammer.js +149 -104
  23. package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
  24. package/lib/programmers/llm/LlmSchemaProgrammer.d.ts +7 -2
  25. package/lib/programmers/llm/LlmSchemaProgrammer.js +40 -12
  26. package/lib/programmers/llm/LlmSchemaProgrammer.js.map +1 -1
  27. package/lib/tags/UniqueItems.d.ts +1 -1
  28. package/lib/transformers/features/llm/LlmSchemaTransformer.js +13 -2
  29. package/lib/transformers/features/llm/LlmSchemaTransformer.js.map +1 -1
  30. package/package.json +37 -28
  31. package/src/executable/TypiaSetupWizard.ts +1 -1
  32. package/src/internal/_isUniqueItems.ts +159 -0
  33. package/src/internal/_llmApplicationFinalize.ts +25 -6
  34. package/src/llm.ts +6 -6
  35. package/src/programmers/llm/LlmApplicationProgrammer.ts +175 -155
  36. package/src/programmers/llm/LlmSchemaProgrammer.ts +80 -17
  37. package/src/tags/Format.ts +50 -50
  38. package/src/tags/Type.ts +32 -32
  39. package/src/tags/UniqueItems.ts +1 -1
  40. package/src/transformers/features/llm/LlmSchemaTransformer.ts +40 -6
@@ -1,5 +1,13 @@
1
- import { ILlmApplication } from "@samchon/openapi";
2
- import { HttpLlmConverter } from "@samchon/openapi/lib/converters/HttpLlmConverter";
1
+ import {
2
+ IChatGptSchema,
3
+ IHttpLlmApplication,
4
+ ILlmApplication,
5
+ OpenApi,
6
+ } from "@samchon/openapi";
7
+ import { ChatGptConverter } from "@samchon/openapi/lib/converters/ChatGptConverter";
8
+ import { GeminiConverter } from "@samchon/openapi/lib/converters/GeminiConverter";
9
+ import { LlmConverterV3 } from "@samchon/openapi/lib/converters/LlmConverterV3";
10
+ import { LlmConverterV3_1 } from "@samchon/openapi/lib/converters/LlmConverterV3_1";
3
11
 
4
12
  import { IJsonSchemaCollection } from "../../schemas/json/IJsonSchemaCollection";
5
13
  import { Metadata } from "../../schemas/metadata/Metadata";
@@ -13,25 +21,42 @@ import { json_schema_string } from "../internal/json_schema_string";
13
21
  import { JsonSchemasProgrammer } from "../json/JsonSchemasProgrammer";
14
22
 
15
23
  export namespace LlmSchemaProgrammer {
24
+ export interface IOutput<Model extends ILlmApplication.Model> {
25
+ model: Model;
26
+ schema: ILlmApplication.ModelSchema[Model];
27
+ $defs: Record<string, IChatGptSchema>;
28
+ }
16
29
  export const write = <Model extends ILlmApplication.Model>(props: {
17
30
  model: Model;
18
31
  metadata: Metadata;
19
- }): ILlmApplication.ModelSchema[Model] => {
32
+ }): IOutput<Model> => {
20
33
  const collection: IJsonSchemaCollection<"3.1"> =
21
34
  JsonSchemasProgrammer.write({
22
35
  version: "3.1",
23
36
  metadatas: [props.metadata],
24
37
  });
25
- const schema: ILlmApplication.ModelSchema[Model] | null =
26
- HttpLlmConverter.schema({
27
- model: props.model,
28
- components: collection.components,
29
- schema: collection.schemas[0]!,
38
+
39
+ const $defs: Record<string, IChatGptSchema> = {};
40
+ const schema: ILlmApplication.ModelSchema[Model] | null = CASTERS[
41
+ props.model
42
+ ]({
43
+ options: {
30
44
  recursive: 3,
31
- });
45
+ reference: false,
46
+ constraint: false,
47
+ } satisfies Omit<ILlmApplication.IChatGptOptions, "separate"> &
48
+ Omit<ILlmApplication.ICommonOptions<any>, "separate"> as any,
49
+ components: collection.components,
50
+ schema: collection.schemas[0]!,
51
+ $defs,
52
+ }) as ILlmApplication.ModelSchema[Model] | null;
32
53
  if (schema === null)
33
54
  throw new Error("Failed to convert JSON schema to LLM schema.");
34
- return schema;
55
+ return {
56
+ model: props.model,
57
+ $defs,
58
+ schema,
59
+ };
35
60
  };
36
61
 
37
62
  export const validate =
@@ -64,13 +89,6 @@ export namespace LlmSchemaProgrammer {
64
89
  output.push(`LLM schema does not support ${native.name} type.`);
65
90
  if (model === "gemini" && size(metadata) > 1)
66
91
  output.push("Gemini model does not support the union type.");
67
- // if (
68
- // metadata.aliases.some((a) => a.type.recursive) ||
69
- // metadata.arrays.some((a) => a.type.recursive) ||
70
- // metadata.objects.some((o) => o.type.recursive) ||
71
- // metadata.tuples.some((t) => t.type.recursive)
72
- // )
73
- // output.push("LLM schema does not support recursive type.");
74
92
  return output;
75
93
  };
76
94
  }
@@ -112,3 +130,48 @@ const size = (metadata: Metadata): number =>
112
130
  }).length,
113
131
  )
114
132
  .reduce((a, b) => a + b, 0);
133
+
134
+ const CASTERS = {
135
+ "3.0": (props: {
136
+ components: OpenApi.IComponents;
137
+ schema: OpenApi.IJsonSchema;
138
+ options: IHttpLlmApplication.IOptions<"3.0">;
139
+ }) =>
140
+ LlmConverterV3.schema({
141
+ components: props.components,
142
+ schema: props.schema,
143
+ recursive: props.options.recursive,
144
+ }),
145
+ "3.1": (props: {
146
+ components: OpenApi.IComponents;
147
+ schema: OpenApi.IJsonSchema;
148
+ options: IHttpLlmApplication.IOptions<"3.1">;
149
+ }) =>
150
+ LlmConverterV3_1.schema({
151
+ components: props.components,
152
+ schema: props.schema,
153
+ recursive: props.options.recursive,
154
+ }),
155
+ chatgpt: (props: {
156
+ components: OpenApi.IComponents;
157
+ schema: OpenApi.IJsonSchema;
158
+ $defs: Record<string, IChatGptSchema>;
159
+ options: Omit<IHttpLlmApplication.IChatGptOptions, "separate">;
160
+ }) =>
161
+ ChatGptConverter.schema({
162
+ components: props.components,
163
+ schema: props.schema,
164
+ $defs: props.$defs,
165
+ options: props.options,
166
+ }),
167
+ gemini: (props: {
168
+ components: OpenApi.IComponents;
169
+ schema: OpenApi.IJsonSchema;
170
+ options: IHttpLlmApplication.IOptions<"gemini">;
171
+ }) =>
172
+ GeminiConverter.schema({
173
+ components: props.components,
174
+ schema: props.schema,
175
+ recursive: props.options.recursive,
176
+ }),
177
+ };
@@ -1,50 +1,50 @@
1
- import type { TagBase } from "./TagBase";
2
-
3
- export type Format<Value extends Format.Value> = TagBase<{
4
- target: "string";
5
- kind: "format";
6
- value: Value;
7
- validate: `$importInternal("isFormat${PascalizeString<Value>}")($input)`;
8
- exclusive: ["format", "pattern"];
9
- schema: {
10
- format: Value;
11
- };
12
- }>;
13
- export namespace Format {
14
- export type Value =
15
- | "byte"
16
- | "password"
17
- | "regex"
18
- | "uuid"
19
- | "email"
20
- | "hostname"
21
- | "idn-email"
22
- | "idn-hostname"
23
- | "iri"
24
- | "iri-reference"
25
- | "ipv4"
26
- | "ipv6"
27
- | "uri"
28
- | "uri-reference"
29
- | "uri-template"
30
- | "url"
31
- | "date-time"
32
- | "date"
33
- | "time"
34
- | "duration"
35
- | "json-pointer"
36
- | "relative-json-pointer";
37
- }
38
-
39
- type PascalizeString<Key extends string> = Key extends `-${infer R}`
40
- ? `${PascalizeString<R>}`
41
- : Key extends `${infer _F}-${infer _R}`
42
- ? PascalizeSnakeString<Key>
43
- : Capitalize<Key>;
44
- type PascalizeSnakeString<Key extends string> = Key extends `-${infer R}`
45
- ? PascalizeSnakeString<R>
46
- : Key extends `${infer F}${infer M}-${infer R}`
47
- ? `${Uppercase<F>}${Lowercase<M>}${PascalizeSnakeString<R>}`
48
- : Key extends `${infer F}${infer R}`
49
- ? `${Uppercase<F>}${Lowercase<R>}`
50
- : Key;
1
+ import type { TagBase } from "./TagBase";
2
+
3
+ export type Format<Value extends Format.Value> = TagBase<{
4
+ target: "string";
5
+ kind: "format";
6
+ value: Value;
7
+ validate: `$importInternal("isFormat${PascalizeString<Value>}")($input)`;
8
+ exclusive: ["format", "pattern"];
9
+ schema: {
10
+ format: Value;
11
+ };
12
+ }>;
13
+ export namespace Format {
14
+ export type Value =
15
+ | "byte"
16
+ | "password"
17
+ | "regex"
18
+ | "uuid"
19
+ | "email"
20
+ | "hostname"
21
+ | "idn-email"
22
+ | "idn-hostname"
23
+ | "iri"
24
+ | "iri-reference"
25
+ | "ipv4"
26
+ | "ipv6"
27
+ | "uri"
28
+ | "uri-reference"
29
+ | "uri-template"
30
+ | "url"
31
+ | "date-time"
32
+ | "date"
33
+ | "time"
34
+ | "duration"
35
+ | "json-pointer"
36
+ | "relative-json-pointer";
37
+ }
38
+
39
+ type PascalizeString<Key extends string> = Key extends `-${infer R}`
40
+ ? `${PascalizeString<R>}`
41
+ : Key extends `${infer _F}-${infer _R}`
42
+ ? PascalizeSnakeString<Key>
43
+ : Capitalize<Key>;
44
+ type PascalizeSnakeString<Key extends string> = Key extends `-${infer R}`
45
+ ? PascalizeSnakeString<R>
46
+ : Key extends `${infer F}${infer M}-${infer R}`
47
+ ? `${Uppercase<F>}${Lowercase<M>}${PascalizeSnakeString<R>}`
48
+ : Key extends `${infer F}${infer R}`
49
+ ? `${Uppercase<F>}${Lowercase<R>}`
50
+ : Key;
package/src/tags/Type.ts CHANGED
@@ -1,32 +1,32 @@
1
- import { TagBase } from "./TagBase";
2
-
3
- export type Type<
4
- Value extends "int32" | "uint32" | "int64" | "uint64" | "float" | "double",
5
- > = TagBase<{
6
- target: Value extends "int64" | "uint64" ? "bigint" | "number" : "number";
7
- kind: "type";
8
- value: Value;
9
- validate: Value extends "int32"
10
- ? `$importInternal("isTypeInt32")($input)`
11
- : Value extends "uint32"
12
- ? `$importInternal("isTypeUint32")($input)`
13
- : Value extends "int64"
14
- ? {
15
- number: `$importInternal("isTypeInt64")($input)`;
16
- bigint: `true`;
17
- }
18
- : Value extends "uint64"
19
- ? {
20
- number: `$importInternal("isTypeUint64")($input)`;
21
- bigint: `BigInt(0) <= $input`;
22
- }
23
- : Value extends "float"
24
- ? `$importInternal("isTypeFloat")($input)`
25
- : `true`;
26
- exclusive: true;
27
- schema: {
28
- type: Value extends "int32" | "uint32" | "int64" | "uint64"
29
- ? "integer"
30
- : "number";
31
- };
32
- }>;
1
+ import { TagBase } from "./TagBase";
2
+
3
+ export type Type<
4
+ Value extends "int32" | "uint32" | "int64" | "uint64" | "float" | "double",
5
+ > = TagBase<{
6
+ target: Value extends "int64" | "uint64" ? "bigint" | "number" : "number";
7
+ kind: "type";
8
+ value: Value;
9
+ validate: Value extends "int32"
10
+ ? `$importInternal("isTypeInt32")($input)`
11
+ : Value extends "uint32"
12
+ ? `$importInternal("isTypeUint32")($input)`
13
+ : Value extends "int64"
14
+ ? {
15
+ number: `$importInternal("isTypeInt64")($input)`;
16
+ bigint: `true`;
17
+ }
18
+ : Value extends "uint64"
19
+ ? {
20
+ number: `$importInternal("isTypeUint64")($input)`;
21
+ bigint: `BigInt(0) <= $input`;
22
+ }
23
+ : Value extends "float"
24
+ ? `$importInternal("isTypeFloat")($input)`
25
+ : `true`;
26
+ exclusive: true;
27
+ schema: {
28
+ type: Value extends "int32" | "uint32" | "int64" | "uint64"
29
+ ? "integer"
30
+ : "number";
31
+ };
32
+ }>;
@@ -5,7 +5,7 @@ export type UniqueItems<Value extends boolean = true> = TagBase<{
5
5
  kind: "uniqueItems";
6
6
  value: Value;
7
7
  validate: Value extends true
8
- ? `$input.length <= 1 || ($input.length === new Set($input).size)`
8
+ ? `$importInternal("isUniqueItems")($input)`
9
9
  : undefined;
10
10
  exclusive: true;
11
11
  schema: {
@@ -1,6 +1,7 @@
1
1
  import { ILlmApplication } from "@samchon/openapi";
2
2
  import ts from "typescript";
3
3
 
4
+ import { IdentifierFactory } from "../../../factories/IdentifierFactory";
4
5
  import { LiteralFactory } from "../../../factories/LiteralFactory";
5
6
  import { MetadataCollection } from "../../../factories/MetadataCollection";
6
7
  import { MetadataFactory } from "../../../factories/MetadataFactory";
@@ -66,12 +67,45 @@ export namespace LlmSchemaTransformer {
66
67
  });
67
68
 
68
69
  // GENERATE LLM SCHEMA
69
- const schema: ILlmApplication.ModelSchema[ILlmApplication.Model] =
70
- LlmSchemaProgrammer.write({
71
- model,
72
- metadata: result.data,
73
- });
74
- return LiteralFactory.write(schema);
70
+ const out: LlmSchemaProgrammer.IOutput<any> = LlmSchemaProgrammer.write({
71
+ model,
72
+ metadata: result.data,
73
+ });
74
+ if (Object.keys(out.$defs).length === 0)
75
+ return LiteralFactory.write(out.schema);
76
+ return ts.factory.createCallExpression(
77
+ ts.factory.createArrowFunction(
78
+ undefined,
79
+ undefined,
80
+ [
81
+ IdentifierFactory.parameter(
82
+ "$defs",
83
+ ts.factory.createTypeReferenceNode("Record<string, unknown>"),
84
+ undefined,
85
+ ),
86
+ ],
87
+ undefined,
88
+ undefined,
89
+ ts.factory.createBlock(
90
+ [
91
+ ts.factory.createExpressionStatement(
92
+ ts.factory.createCallExpression(
93
+ ts.factory.createIdentifier("Object.assign"),
94
+ undefined,
95
+ [
96
+ ts.factory.createIdentifier("$defs"),
97
+ LiteralFactory.write(out.$defs),
98
+ ],
99
+ ),
100
+ ),
101
+ ts.factory.createReturnStatement(LiteralFactory.write(out.schema)),
102
+ ],
103
+ true,
104
+ ),
105
+ ),
106
+ undefined,
107
+ [props.expression.arguments[0]!],
108
+ );
75
109
  };
76
110
 
77
111
  const get_parameter =