@typia/utils 12.0.0-dev.20260309 → 12.0.0-dev.20260310

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 (92) hide show
  1. package/lib/http/internal/HttpLlmApplicationComposer.mjs +5 -1
  2. package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
  3. package/lib/index.mjs +9 -9
  4. package/lib/utils/LlmJson.mjs +9 -2
  5. package/lib/utils/LlmJson.mjs.map +1 -1
  6. package/lib/utils/internal/stringifyValidationFailure.js +17 -15
  7. package/lib/utils/internal/stringifyValidationFailure.js.map +1 -1
  8. package/lib/utils/internal/stringifyValidationFailure.mjs +17 -15
  9. package/lib/utils/internal/stringifyValidationFailure.mjs.map +1 -1
  10. package/lib/validators/internal/OpenApiOneOfValidator.mjs +5 -1
  11. package/lib/validators/internal/OpenApiOneOfValidator.mjs.map +1 -1
  12. package/package.json +2 -2
  13. package/src/converters/LlmSchemaConverter.ts +647 -647
  14. package/src/converters/OpenApiConverter.ts +285 -285
  15. package/src/converters/index.ts +5 -5
  16. package/src/converters/internal/LlmDescriptionInverter.ts +178 -178
  17. package/src/converters/internal/LlmParametersComposer.ts +52 -52
  18. package/src/converters/internal/OpenApiConstraintShifter.ts +154 -154
  19. package/src/converters/internal/OpenApiExclusiveEmender.ts +46 -46
  20. package/src/converters/internal/OpenApiV3Downgrader.ts +355 -355
  21. package/src/converters/internal/OpenApiV3Upgrader.ts +470 -470
  22. package/src/converters/internal/OpenApiV3_1Upgrader.ts +685 -685
  23. package/src/converters/internal/SwaggerV2Downgrader.ts +424 -424
  24. package/src/converters/internal/SwaggerV2Upgrader.ts +523 -523
  25. package/src/http/HttpError.ts +107 -107
  26. package/src/http/HttpLlm.ts +167 -167
  27. package/src/http/HttpMigration.ts +92 -92
  28. package/src/http/index.ts +3 -3
  29. package/src/http/internal/HttpLlmApplicationComposer.ts +361 -361
  30. package/src/http/internal/HttpLlmFunctionFetcher.ts +37 -37
  31. package/src/http/internal/HttpMigrateApplicationComposer.ts +56 -56
  32. package/src/http/internal/HttpMigrateRouteAccessor.ts +135 -135
  33. package/src/http/internal/HttpMigrateRouteComposer.ts +505 -505
  34. package/src/http/internal/HttpMigrateRouteFetcher.ts +203 -203
  35. package/src/index.ts +4 -4
  36. package/src/utils/ArrayUtil.ts +42 -42
  37. package/src/utils/LlmJson.ts +141 -141
  38. package/src/utils/MapUtil.ts +15 -15
  39. package/src/utils/NamingConvention.ts +205 -205
  40. package/src/utils/Singleton.ts +17 -17
  41. package/src/utils/StringUtil.ts +14 -14
  42. package/src/utils/dedent.ts +57 -57
  43. package/src/utils/index.ts +8 -8
  44. package/src/utils/internal/EndpointUtil.ts +44 -44
  45. package/src/utils/internal/JsonDescriptor.ts +70 -70
  46. package/src/utils/internal/OpenApiTypeCheckerBase.ts +822 -822
  47. package/src/utils/internal/coerceLlmArguments.ts +314 -314
  48. package/src/utils/internal/parseLenientJson.ts +894 -894
  49. package/src/utils/internal/stringifyValidationFailure.ts +415 -411
  50. package/src/validators/LlmTypeChecker.ts +402 -402
  51. package/src/validators/OpenApiTypeChecker.ts +297 -297
  52. package/src/validators/OpenApiV3TypeChecker.ts +70 -70
  53. package/src/validators/OpenApiV3_1TypeChecker.ts +86 -86
  54. package/src/validators/OpenApiValidator.ts +94 -94
  55. package/src/validators/SwaggerV2TypeChecker.ts +71 -71
  56. package/src/validators/functional/_isBigintString.ts +8 -8
  57. package/src/validators/functional/_isFormatByte.ts +7 -7
  58. package/src/validators/functional/_isFormatDate.ts +3 -3
  59. package/src/validators/functional/_isFormatDateTime.ts +4 -4
  60. package/src/validators/functional/_isFormatDuration.ts +4 -4
  61. package/src/validators/functional/_isFormatEmail.ts +4 -4
  62. package/src/validators/functional/_isFormatHostname.ts +4 -4
  63. package/src/validators/functional/_isFormatIdnEmail.ts +4 -4
  64. package/src/validators/functional/_isFormatIdnHostname.ts +4 -4
  65. package/src/validators/functional/_isFormatIpv4.ts +4 -4
  66. package/src/validators/functional/_isFormatIpv6.ts +4 -4
  67. package/src/validators/functional/_isFormatIri.ts +3 -3
  68. package/src/validators/functional/_isFormatIriReference.ts +4 -4
  69. package/src/validators/functional/_isFormatJsonPointer.ts +3 -3
  70. package/src/validators/functional/_isFormatPassword.ts +1 -1
  71. package/src/validators/functional/_isFormatRegex.ts +8 -8
  72. package/src/validators/functional/_isFormatRelativeJsonPointer.ts +4 -4
  73. package/src/validators/functional/_isFormatTime.ts +4 -4
  74. package/src/validators/functional/_isFormatUri.ts +6 -6
  75. package/src/validators/functional/_isFormatUriReference.ts +5 -5
  76. package/src/validators/functional/_isFormatUriTemplate.ts +4 -4
  77. package/src/validators/functional/_isFormatUrl.ts +4 -4
  78. package/src/validators/functional/_isFormatUuid.ts +3 -3
  79. package/src/validators/functional/_isUniqueItems.ts +159 -159
  80. package/src/validators/index.ts +14 -14
  81. package/src/validators/internal/IOpenApiValidatorContext.ts +17 -17
  82. package/src/validators/internal/OpenApiArrayValidator.ts +49 -49
  83. package/src/validators/internal/OpenApiBooleanValidator.ts +11 -11
  84. package/src/validators/internal/OpenApiConstantValidator.ts +11 -11
  85. package/src/validators/internal/OpenApiIntegerValidator.ts +49 -49
  86. package/src/validators/internal/OpenApiNumberValidator.ts +48 -48
  87. package/src/validators/internal/OpenApiObjectValidator.ts +83 -83
  88. package/src/validators/internal/OpenApiOneOfValidator.ts +309 -309
  89. package/src/validators/internal/OpenApiSchemaNamingRule.ts +124 -124
  90. package/src/validators/internal/OpenApiStationValidator.ts +115 -115
  91. package/src/validators/internal/OpenApiStringValidator.ts +88 -88
  92. package/src/validators/internal/OpenApiTupleValidator.ts +55 -55
@@ -1,141 +1,141 @@
1
- import {
2
- IJsonParseResult,
3
- ILlmSchema,
4
- IValidation,
5
- OpenApi,
6
- } from "@typia/interface";
7
-
8
- import { LlmSchemaConverter } from "../converters";
9
- import { OpenApiValidator } from "../validators";
10
- import { coerceLlmArguments } from "./internal/coerceLlmArguments";
11
- import { parseLenientJson } from "./internal/parseLenientJson";
12
- import { stringifyValidationFailure } from "./internal/stringifyValidationFailure";
13
-
14
- /**
15
- * JSON utilities for LLM function calling.
16
- *
17
- * - {@link LlmJson.parse}: Lenient JSON parser for incomplete/malformed JSON
18
- * - {@link LlmJson.stringify}: Format validation errors for LLM feedback
19
- * - {@link LlmJson.validate}: Create a reusable validator from schema
20
- *
21
- * @author Jeongho Nam - https://github.com/samchon
22
- */
23
- export namespace LlmJson {
24
- /**
25
- * Coerce LLM arguments to match expected schema types.
26
- *
27
- * LLMs often return values with incorrect types (e.g., numbers as strings).
28
- * This function recursively coerces values based on the schema:
29
- *
30
- * - `"42"` → `42` (when schema expects number)
31
- * - `"true"` → `true` (when schema expects boolean)
32
- * - `"null"` → `null` (when schema expects null)
33
- * - `"{...}"` → `{...}` (when schema expects object)
34
- * - `"[...]"` → `[...]` (when schema expects array)
35
- *
36
- * Use this when SDK provides already-parsed objects but values may have wrong
37
- * types. For raw JSON strings, use {@link parse} instead.
38
- *
39
- * @param input Parsed arguments object from LLM
40
- * @param parameters LLM function parameters schema for type coercion
41
- * @returns Coerced arguments with corrected types
42
- */
43
- export function coerce<T = unknown>(
44
- input: unknown,
45
- parameters: ILlmSchema.IParameters,
46
- ): T {
47
- return coerceLlmArguments(input, parameters);
48
- }
49
-
50
- /**
51
- * Parse lenient JSON with optional schema-based coercion.
52
- *
53
- * Handles incomplete/malformed JSON commonly produced by LLMs:
54
- *
55
- * - Unclosed brackets, strings, trailing commas
56
- * - JavaScript-style comments (`//` and multi-line)
57
- * - Unquoted object keys, incomplete keywords (`tru`, `fal`, `nul`)
58
- * - Markdown code block extraction, junk prefix skipping
59
- *
60
- * When `parameters` schema is provided, also coerces double-stringified
61
- * values: `"42"` → `42`, `"true"` → `true`, `"{...}"` → `{...}` based on
62
- * expected types.
63
- *
64
- * Type validation is NOT performed - use {@link ILlmFunction.validate}.
65
- *
66
- * @param input Raw JSON string (potentially incomplete or malformed)
67
- * @param parameters Optional LLM parameters schema for type coercion
68
- * @returns Parse result with data on success, or partial data with errors
69
- */
70
- export function parse<T = unknown>(
71
- input: string,
72
- parameters?: ILlmSchema.IParameters,
73
- ): IJsonParseResult<T> {
74
- const result: IJsonParseResult<T> = parseLenientJson<T>(input);
75
-
76
- // Apply schema-based coercion if parameters provided and parsing succeeded
77
- if (parameters !== undefined && result.success) {
78
- return {
79
- success: true,
80
- data: coerceLlmArguments(result.data, parameters) as T,
81
- };
82
- }
83
- return result;
84
- }
85
-
86
- /**
87
- * Format validation failure for LLM auto-correction feedback.
88
- *
89
- * When LLM generates invalid function call arguments, this produces annotated
90
- * JSON with inline `// ❌` error comments at each invalid property. The output
91
- * is wrapped in a markdown code block so that LLM can understand and correct
92
- * its mistakes in the next turn.
93
- *
94
- * Below is an example of the output format:
95
- *
96
- * ```json
97
- * {
98
- * "name": "John",
99
- * "age": "twenty", // ❌ [{"path":"$input.age","expected":"number & Type<\"uint32\">"}]
100
- * "email": "not-an-email", // ❌ [{"path":"$input.email","expected":"string & Format<\"email\">"}]
101
- * "hobbies": "reading" // ❌ [{"path":"$input.hobbies","expected":"Array<string>"}]
102
- * }
103
- * ```
104
- *
105
- * @param failure Validation failure from {@link ILlmFunction.validate}
106
- */
107
- export function stringify(failure: IValidation.IFailure): string {
108
- return stringifyValidationFailure(failure);
109
- }
110
-
111
- /**
112
- * Create a reusable validator from LLM parameters schema.
113
- *
114
- * When validation fails, format the failure with {@link stringify} for LLM
115
- * auto-correction feedback.
116
- *
117
- * @param parameters LLM function parameters schema
118
- * @param equals If `true`, reject extraneous properties not defined in the
119
- * schema. Otherwise, extra properties are ignored.
120
- * @returns Validator function that checks data against the schema
121
- */
122
- export function validate(
123
- parameters: ILlmSchema.IParameters,
124
- equals?: boolean | undefined,
125
- ) {
126
- const components: OpenApi.IComponents = {
127
- schemas: {},
128
- };
129
- const schema: OpenApi.IJsonSchema = LlmSchemaConverter.invert({
130
- components,
131
- schema: parameters,
132
- $defs: parameters.$defs,
133
- });
134
- return OpenApiValidator.create({
135
- components,
136
- schema,
137
- required: true,
138
- equals,
139
- });
140
- }
141
- }
1
+ import {
2
+ IJsonParseResult,
3
+ ILlmSchema,
4
+ IValidation,
5
+ OpenApi,
6
+ } from "@typia/interface";
7
+
8
+ import { LlmSchemaConverter } from "../converters";
9
+ import { OpenApiValidator } from "../validators";
10
+ import { coerceLlmArguments } from "./internal/coerceLlmArguments";
11
+ import { parseLenientJson } from "./internal/parseLenientJson";
12
+ import { stringifyValidationFailure } from "./internal/stringifyValidationFailure";
13
+
14
+ /**
15
+ * JSON utilities for LLM function calling.
16
+ *
17
+ * - {@link LlmJson.parse}: Lenient JSON parser for incomplete/malformed JSON
18
+ * - {@link LlmJson.stringify}: Format validation errors for LLM feedback
19
+ * - {@link LlmJson.validate}: Create a reusable validator from schema
20
+ *
21
+ * @author Jeongho Nam - https://github.com/samchon
22
+ */
23
+ export namespace LlmJson {
24
+ /**
25
+ * Coerce LLM arguments to match expected schema types.
26
+ *
27
+ * LLMs often return values with incorrect types (e.g., numbers as strings).
28
+ * This function recursively coerces values based on the schema:
29
+ *
30
+ * - `"42"` → `42` (when schema expects number)
31
+ * - `"true"` → `true` (when schema expects boolean)
32
+ * - `"null"` → `null` (when schema expects null)
33
+ * - `"{...}"` → `{...}` (when schema expects object)
34
+ * - `"[...]"` → `[...]` (when schema expects array)
35
+ *
36
+ * Use this when SDK provides already-parsed objects but values may have wrong
37
+ * types. For raw JSON strings, use {@link parse} instead.
38
+ *
39
+ * @param input Parsed arguments object from LLM
40
+ * @param parameters LLM function parameters schema for type coercion
41
+ * @returns Coerced arguments with corrected types
42
+ */
43
+ export function coerce<T = unknown>(
44
+ input: unknown,
45
+ parameters: ILlmSchema.IParameters,
46
+ ): T {
47
+ return coerceLlmArguments(input, parameters);
48
+ }
49
+
50
+ /**
51
+ * Parse lenient JSON with optional schema-based coercion.
52
+ *
53
+ * Handles incomplete/malformed JSON commonly produced by LLMs:
54
+ *
55
+ * - Unclosed brackets, strings, trailing commas
56
+ * - JavaScript-style comments (`//` and multi-line)
57
+ * - Unquoted object keys, incomplete keywords (`tru`, `fal`, `nul`)
58
+ * - Markdown code block extraction, junk prefix skipping
59
+ *
60
+ * When `parameters` schema is provided, also coerces double-stringified
61
+ * values: `"42"` → `42`, `"true"` → `true`, `"{...}"` → `{...}` based on
62
+ * expected types.
63
+ *
64
+ * Type validation is NOT performed - use {@link ILlmFunction.validate}.
65
+ *
66
+ * @param input Raw JSON string (potentially incomplete or malformed)
67
+ * @param parameters Optional LLM parameters schema for type coercion
68
+ * @returns Parse result with data on success, or partial data with errors
69
+ */
70
+ export function parse<T = unknown>(
71
+ input: string,
72
+ parameters?: ILlmSchema.IParameters,
73
+ ): IJsonParseResult<T> {
74
+ const result: IJsonParseResult<T> = parseLenientJson<T>(input);
75
+
76
+ // Apply schema-based coercion if parameters provided and parsing succeeded
77
+ if (parameters !== undefined && result.success) {
78
+ return {
79
+ success: true,
80
+ data: coerceLlmArguments(result.data, parameters) as T,
81
+ };
82
+ }
83
+ return result;
84
+ }
85
+
86
+ /**
87
+ * Format validation failure for LLM auto-correction feedback.
88
+ *
89
+ * When LLM generates invalid function call arguments, this produces annotated
90
+ * JSON with inline `// ❌` error comments at each invalid property. The output
91
+ * is wrapped in a markdown code block so that LLM can understand and correct
92
+ * its mistakes in the next turn.
93
+ *
94
+ * Below is an example of the output format:
95
+ *
96
+ * ```json
97
+ * {
98
+ * "name": "John",
99
+ * "age": "twenty", // ❌ [{"path":"$input.age","expected":"number & Type<\"uint32\">"}]
100
+ * "email": "not-an-email", // ❌ [{"path":"$input.email","expected":"string & Format<\"email\">"}]
101
+ * "hobbies": "reading" // ❌ [{"path":"$input.hobbies","expected":"Array<string>"}]
102
+ * }
103
+ * ```
104
+ *
105
+ * @param failure Validation failure from {@link ILlmFunction.validate}
106
+ */
107
+ export function stringify(failure: IValidation.IFailure): string {
108
+ return stringifyValidationFailure(failure);
109
+ }
110
+
111
+ /**
112
+ * Create a reusable validator from LLM parameters schema.
113
+ *
114
+ * When validation fails, format the failure with {@link stringify} for LLM
115
+ * auto-correction feedback.
116
+ *
117
+ * @param parameters LLM function parameters schema
118
+ * @param equals If `true`, reject extraneous properties not defined in the
119
+ * schema. Otherwise, extra properties are ignored.
120
+ * @returns Validator function that checks data against the schema
121
+ */
122
+ export function validate(
123
+ parameters: ILlmSchema.IParameters,
124
+ equals?: boolean | undefined,
125
+ ) {
126
+ const components: OpenApi.IComponents = {
127
+ schemas: {},
128
+ };
129
+ const schema: OpenApi.IJsonSchema = LlmSchemaConverter.invert({
130
+ components,
131
+ schema: parameters,
132
+ $defs: parameters.$defs,
133
+ });
134
+ return OpenApiValidator.create({
135
+ components,
136
+ schema,
137
+ required: true,
138
+ equals,
139
+ });
140
+ }
141
+ }
@@ -1,15 +1,15 @@
1
- /** @internal */
2
- export namespace MapUtil {
3
- export const take = <Key, T>(
4
- dict: Map<Key, T>,
5
- key: Key,
6
- generator: () => T,
7
- ): T => {
8
- const oldbie: T | undefined = dict.get(key);
9
- if (oldbie) return oldbie;
10
-
11
- const value: T = generator();
12
- dict.set(key, value);
13
- return value;
14
- };
15
- }
1
+ /** @internal */
2
+ export namespace MapUtil {
3
+ export const take = <Key, T>(
4
+ dict: Map<Key, T>,
5
+ key: Key,
6
+ generator: () => T,
7
+ ): T => {
8
+ const oldbie: T | undefined = dict.get(key);
9
+ if (oldbie) return oldbie;
10
+
11
+ const value: T = generator();
12
+ dict.set(key, value);
13
+ return value;
14
+ };
15
+ }