@typia/utils 12.0.0-dev.20260306 → 12.0.0-dev.20260307

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 (60) hide show
  1. package/README.md +5 -5
  2. package/lib/converters/LlmSchemaConverter.d.ts +1 -20
  3. package/lib/converters/LlmSchemaConverter.js +0 -189
  4. package/lib/converters/LlmSchemaConverter.js.map +1 -1
  5. package/lib/converters/LlmSchemaConverter.mjs +0 -227
  6. package/lib/converters/LlmSchemaConverter.mjs.map +1 -1
  7. package/lib/http/HttpLlm.d.ts +2 -32
  8. package/lib/http/HttpLlm.js +4 -27
  9. package/lib/http/HttpLlm.js.map +1 -1
  10. package/lib/http/HttpLlm.mjs +1 -24
  11. package/lib/http/HttpLlm.mjs.map +1 -1
  12. package/lib/http/internal/HttpLlmApplicationComposer.js +13 -19
  13. package/lib/http/internal/HttpLlmApplicationComposer.js.map +1 -1
  14. package/lib/http/internal/HttpLlmApplicationComposer.mjs +7 -9
  15. package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
  16. package/lib/index.mjs +10 -10
  17. package/lib/utils/LlmJson.d.ts +67 -0
  18. package/lib/utils/LlmJson.js +106 -0
  19. package/lib/utils/LlmJson.js.map +1 -0
  20. package/lib/utils/LlmJson.mjs +113 -0
  21. package/lib/utils/LlmJson.mjs.map +1 -0
  22. package/lib/utils/index.d.ts +1 -1
  23. package/lib/utils/index.js +1 -1
  24. package/lib/utils/index.js.map +1 -1
  25. package/lib/utils/index.mjs +1 -1
  26. package/lib/utils/internal/coerceLlmArguments.d.ts +1 -0
  27. package/lib/utils/internal/coerceLlmArguments.js +233 -0
  28. package/lib/utils/internal/coerceLlmArguments.js.map +1 -0
  29. package/lib/utils/internal/coerceLlmArguments.mjs +232 -0
  30. package/lib/utils/internal/coerceLlmArguments.mjs.map +1 -0
  31. package/lib/utils/internal/parseLenientJson.d.ts +1 -0
  32. package/lib/utils/internal/parseLenientJson.js +639 -0
  33. package/lib/utils/internal/parseLenientJson.js.map +1 -0
  34. package/lib/utils/internal/parseLenientJson.mjs +640 -0
  35. package/lib/utils/internal/parseLenientJson.mjs.map +1 -0
  36. package/lib/utils/internal/stringifyValidationFailure.d.ts +2 -0
  37. package/lib/utils/{stringifyValidationFailure.js → internal/stringifyValidationFailure.js} +55 -63
  38. package/lib/utils/internal/stringifyValidationFailure.js.map +1 -0
  39. package/lib/utils/{stringifyValidationFailure.mjs → internal/stringifyValidationFailure.mjs} +54 -63
  40. package/lib/utils/internal/stringifyValidationFailure.mjs.map +1 -0
  41. package/lib/validators/internal/OpenApiOneOfValidator.mjs +5 -1
  42. package/lib/validators/internal/OpenApiOneOfValidator.mjs.map +1 -1
  43. package/package.json +2 -2
  44. package/src/converters/LlmSchemaConverter.ts +0 -277
  45. package/src/http/HttpLlm.ts +1 -44
  46. package/src/http/internal/HttpLlmApplicationComposer.ts +3 -9
  47. package/src/utils/LlmJson.ts +115 -0
  48. package/src/utils/index.ts +1 -1
  49. package/src/utils/internal/coerceLlmArguments.ts +297 -0
  50. package/src/utils/internal/parseLenientJson.ts +731 -0
  51. package/src/utils/{stringifyValidationFailure.ts → internal/stringifyValidationFailure.ts} +66 -70
  52. package/lib/http/internal/LlmDataMerger.d.ts +0 -48
  53. package/lib/http/internal/LlmDataMerger.js +0 -60
  54. package/lib/http/internal/LlmDataMerger.js.map +0 -1
  55. package/lib/http/internal/LlmDataMerger.mjs +0 -59
  56. package/lib/http/internal/LlmDataMerger.mjs.map +0 -1
  57. package/lib/utils/stringifyValidationFailure.d.ts +0 -25
  58. package/lib/utils/stringifyValidationFailure.js.map +0 -1
  59. package/lib/utils/stringifyValidationFailure.mjs.map +0 -1
  60. package/src/http/internal/LlmDataMerger.ts +0 -73
@@ -0,0 +1,297 @@
1
+ import { IJsonParseResult, ILlmSchema } from "@typia/interface";
2
+
3
+ import { LlmTypeChecker } from "../../validators/LlmTypeChecker";
4
+ import { parseLenientJson } from "./parseLenientJson";
5
+
6
+ /**
7
+ * Coerce LLM arguments by parsing double-stringified JSON values.
8
+ *
9
+ * When LLM produces stringified JSON for non-string schema types, this function
10
+ * attempts to parse them using the lenient JSON parser.
11
+ *
12
+ * Only applies coercion when value is string but schema expects non-string.
13
+ * Type validation is handled separately by `ILlmFunction.validate`.
14
+ *
15
+ * @param value Parsed JSON value (potentially with stringified nested values)
16
+ * @param parameters LLM parameters schema
17
+ * @returns Coerced value with double-stringified JSON parsed
18
+ * @internal
19
+ */
20
+ export function coerceLlmArguments(
21
+ value: unknown,
22
+ parameters: ILlmSchema.IParameters,
23
+ ): unknown {
24
+ return coerceValue(value, parameters, parameters.$defs);
25
+ }
26
+
27
+ function coerceValue(
28
+ value: unknown,
29
+ schema: ILlmSchema,
30
+ $defs: Record<string, ILlmSchema> | undefined,
31
+ ): unknown {
32
+ // Resolve reference
33
+ if (LlmTypeChecker.isReference(schema)) {
34
+ const key: string = schema.$ref.replace("#/$defs/", "");
35
+ const resolved: ILlmSchema | undefined = $defs?.[key];
36
+ if (resolved !== undefined) {
37
+ return coerceValue(value, resolved, $defs);
38
+ }
39
+ return value;
40
+ }
41
+
42
+ // Handle anyOf
43
+ if (LlmTypeChecker.isAnyOf(schema)) {
44
+ // Value is string
45
+ if (typeof value === "string") {
46
+ // If string is in union, don't parse - it's valid as-is
47
+ const hasString: boolean = schema.anyOf.some((s: ILlmSchema): boolean =>
48
+ LlmTypeChecker.isString(resolveSchema(s, $defs)),
49
+ );
50
+ if (hasString) {
51
+ return value;
52
+ }
53
+ // String value but no string in union - try to parse
54
+ const parsed: IJsonParseResult<unknown> = parseLenientJson(value);
55
+ if (parsed.success) {
56
+ // Find uniquely matching schema via type + x-discriminator
57
+ const matched: ILlmSchema | undefined = findMatchingAnyOfSchema(
58
+ parsed.data,
59
+ schema,
60
+ $defs,
61
+ );
62
+ if (matched !== undefined) {
63
+ return coerceValue(parsed.data, matched, $defs);
64
+ }
65
+ return parsed.data;
66
+ }
67
+ return value;
68
+ }
69
+ // Value is object - find matching schema via discriminated union
70
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
71
+ const matched: ILlmSchema | undefined = findMatchingObjectInAnyOf(
72
+ value as Record<string, unknown>,
73
+ schema,
74
+ $defs,
75
+ );
76
+ if (matched !== undefined) {
77
+ return coerceValue(value, matched, $defs);
78
+ }
79
+ return value;
80
+ }
81
+ // Value is array - find matching array schema (only if unambiguous)
82
+ if (Array.isArray(value)) {
83
+ const arraySchemas: ILlmSchema[] = schema.anyOf.filter(
84
+ (s: ILlmSchema): boolean =>
85
+ LlmTypeChecker.isArray(resolveSchema(s, $defs)),
86
+ );
87
+ if (arraySchemas.length === 1) {
88
+ return coerceValue(value, arraySchemas[0]!, $defs);
89
+ }
90
+ // Multiple or no array schemas - skip coercion
91
+ return value;
92
+ }
93
+ // Non-string primitive or no matching schema - return as-is
94
+ return value;
95
+ }
96
+
97
+ // String schema - no coercion needed (value stays as-is)
98
+ if (LlmTypeChecker.isString(schema)) {
99
+ return value;
100
+ }
101
+
102
+ // Unknown schema - no coercion needed
103
+ if (LlmTypeChecker.isUnknown(schema)) {
104
+ return value;
105
+ }
106
+
107
+ // Value is string but schema is non-string - try to parse
108
+ if (typeof value === "string") {
109
+ const parsed: IJsonParseResult<unknown> = parseLenientJson(value);
110
+ if (parsed.success) {
111
+ // Continue coercion on the parsed value (for nested stringified values)
112
+ return coerceValue(parsed.data, schema, $defs);
113
+ }
114
+ // Parse failed, return original - validate will catch type error
115
+ return value;
116
+ }
117
+
118
+ // Value is array and schema is array - recurse into items
119
+ if (Array.isArray(value) && LlmTypeChecker.isArray(schema)) {
120
+ return value.map((item: unknown): unknown =>
121
+ coerceValue(item, schema.items, $defs),
122
+ );
123
+ }
124
+
125
+ // Value is object and schema is object - recurse into properties
126
+ if (
127
+ typeof value === "object" &&
128
+ value !== null &&
129
+ !Array.isArray(value) &&
130
+ LlmTypeChecker.isObject(schema)
131
+ ) {
132
+ return coerceObject(value as Record<string, unknown>, schema, $defs);
133
+ }
134
+
135
+ // Everything else (null, boolean, number, integer) - return as-is
136
+ return value;
137
+ }
138
+
139
+ function coerceObject(
140
+ value: Record<string, unknown>,
141
+ schema: ILlmSchema.IObject,
142
+ $defs: Record<string, ILlmSchema> | undefined,
143
+ ): Record<string, unknown> {
144
+ const result: Record<string, unknown> = {};
145
+
146
+ // Coerce known properties
147
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
148
+ if (key in value) {
149
+ result[key] = coerceValue(value[key], propSchema, $defs);
150
+ }
151
+ }
152
+
153
+ // Preserve additional properties - let validation handle rejection
154
+ const additionalSchema: ILlmSchema | undefined =
155
+ typeof schema.additionalProperties === "object"
156
+ ? schema.additionalProperties
157
+ : undefined;
158
+
159
+ for (const key of Object.keys(value)) {
160
+ if (!(key in schema.properties)) {
161
+ result[key] = additionalSchema
162
+ ? coerceValue(value[key], additionalSchema, $defs)
163
+ : value[key];
164
+ }
165
+ }
166
+
167
+ return result;
168
+ }
169
+
170
+ function resolveSchema(
171
+ schema: ILlmSchema,
172
+ $defs: Record<string, ILlmSchema> | undefined,
173
+ ): ILlmSchema {
174
+ if (LlmTypeChecker.isReference(schema)) {
175
+ const key: string = schema.$ref.replace("#/$defs/", "");
176
+ const resolved: ILlmSchema | undefined = $defs?.[key];
177
+ if (resolved !== undefined) {
178
+ return resolveSchema(resolved, $defs);
179
+ }
180
+ }
181
+ return schema;
182
+ }
183
+
184
+ /**
185
+ * Check if value roughly matches the expected schema type. Used for anyOf
186
+ * matching after parsing.
187
+ */
188
+ function matchesSchemaType(
189
+ value: unknown,
190
+ schema: ILlmSchema,
191
+ $defs: Record<string, ILlmSchema> | undefined,
192
+ ): boolean {
193
+ if (LlmTypeChecker.isReference(schema)) {
194
+ const key: string = schema.$ref.replace("#/$defs/", "");
195
+ const resolved: ILlmSchema | undefined = $defs?.[key];
196
+ if (resolved) return matchesSchemaType(value, resolved, $defs);
197
+ return false;
198
+ }
199
+ if (LlmTypeChecker.isNull(schema)) return value === null;
200
+ if (LlmTypeChecker.isBoolean(schema)) return typeof value === "boolean";
201
+ if (LlmTypeChecker.isInteger(schema))
202
+ return typeof value === "number" && Number.isInteger(value);
203
+ if (LlmTypeChecker.isNumber(schema)) return typeof value === "number";
204
+ if (LlmTypeChecker.isString(schema)) return typeof value === "string";
205
+ if (LlmTypeChecker.isArray(schema)) return Array.isArray(value);
206
+ if (LlmTypeChecker.isObject(schema))
207
+ return typeof value === "object" && value !== null && !Array.isArray(value);
208
+ if (LlmTypeChecker.isUnknown(schema)) return true;
209
+ if (LlmTypeChecker.isAnyOf(schema))
210
+ return schema.anyOf.some((s) => matchesSchemaType(value, s, $defs));
211
+ return false;
212
+ }
213
+
214
+ /**
215
+ * Find the uniquely matching schema for a value among anyOf alternatives. Uses
216
+ * `x-discriminator` for object disambiguation. Returns undefined if no unique
217
+ * match can be determined.
218
+ */
219
+ function findMatchingAnyOfSchema(
220
+ value: unknown,
221
+ schema: ILlmSchema.IAnyOf,
222
+ $defs: Record<string, ILlmSchema> | undefined,
223
+ ): ILlmSchema | undefined {
224
+ const matching: ILlmSchema[] = schema.anyOf.filter((s: ILlmSchema): boolean =>
225
+ matchesSchemaType(value, s, $defs),
226
+ );
227
+ if (matching.length === 1) return matching[0];
228
+ if (matching.length === 0) return undefined;
229
+ // Multiple type matches - try x-discriminator for objects
230
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
231
+ return findMatchingObjectInAnyOf(
232
+ value as Record<string, unknown>,
233
+ schema,
234
+ $defs,
235
+ );
236
+ }
237
+ return undefined;
238
+ }
239
+
240
+ /**
241
+ * Find the matching object schema among anyOf using `x-discriminator`. If only
242
+ * one object schema exists, returns it directly. If multiple exist but no
243
+ * x-discriminator, gives up.
244
+ */
245
+ function findMatchingObjectInAnyOf(
246
+ value: Record<string, unknown>,
247
+ schema: ILlmSchema.IAnyOf,
248
+ $defs: Record<string, ILlmSchema> | undefined,
249
+ ): ILlmSchema | undefined {
250
+ const objectSchemas: ILlmSchema[] = schema.anyOf.filter(
251
+ (s: ILlmSchema): boolean =>
252
+ LlmTypeChecker.isObject(resolveSchema(s, $defs)),
253
+ );
254
+ if (objectSchemas.length === 0) return undefined;
255
+ if (objectSchemas.length === 1) return objectSchemas[0];
256
+
257
+ // Multiple object schemas - require x-discriminator
258
+ const discriminator: ILlmSchema.IAnyOf.IDiscriminator | undefined =
259
+ schema["x-discriminator"];
260
+ if (discriminator === undefined) return undefined;
261
+
262
+ const key: string = discriminator.propertyName;
263
+ const discriminatorValue: unknown = value[key];
264
+
265
+ // Use mapping for direct $ref lookup
266
+ if (
267
+ discriminator.mapping !== undefined &&
268
+ typeof discriminatorValue === "string"
269
+ ) {
270
+ const ref: string | undefined = discriminator.mapping[discriminatorValue];
271
+ if (ref !== undefined) {
272
+ for (const s of schema.anyOf) {
273
+ if (LlmTypeChecker.isReference(s) && s.$ref === ref) {
274
+ return s;
275
+ }
276
+ }
277
+ }
278
+ return undefined;
279
+ }
280
+
281
+ // No mapping - match by enum values on the discriminator property
282
+ for (const s of objectSchemas) {
283
+ const resolved: ILlmSchema = resolveSchema(s, $defs);
284
+ if (!LlmTypeChecker.isObject(resolved)) continue;
285
+ const propSchema: ILlmSchema | undefined = resolved.properties?.[key];
286
+ if (propSchema === undefined) continue;
287
+ const resolvedProp: ILlmSchema = resolveSchema(propSchema, $defs);
288
+ if (
289
+ LlmTypeChecker.isString(resolvedProp) &&
290
+ resolvedProp.enum?.includes(discriminatorValue as string)
291
+ ) {
292
+ return s;
293
+ }
294
+ }
295
+
296
+ return undefined;
297
+ }