@typia/interface 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.
- package/lib/http/IHttpLlmApplication.d.ts +0 -10
- package/lib/http/IHttpLlmFunction.d.ts +8 -64
- package/lib/openapi/OpenApi.d.ts +1 -1
- package/lib/schema/IJsonParseResult.d.ts +124 -0
- package/lib/schema/IJsonParseResult.js +3 -0
- package/lib/schema/IJsonParseResult.js.map +1 -0
- package/lib/schema/ILlmApplication.d.ts +10 -17
- package/lib/schema/ILlmFunction.d.ts +30 -62
- package/lib/schema/index.d.ts +1 -0
- package/lib/schema/index.js +1 -0
- package/lib/schema/index.js.map +1 -1
- package/lib/typings/DeepPartial.d.ts +26 -0
- package/lib/typings/DeepPartial.js +3 -0
- package/lib/typings/DeepPartial.js.map +1 -0
- package/lib/typings/index.d.ts +1 -0
- package/lib/typings/index.js +1 -0
- package/lib/typings/index.js.map +1 -1
- package/package.json +1 -1
- package/src/http/IHttpLlmApplication.ts +0 -11
- package/src/http/IHttpLlmFunction.ts +8 -73
- package/src/openapi/OpenApi.ts +1 -1
- package/src/schema/IJsonParseResult.ts +136 -0
- package/src/schema/ILlmApplication.ts +14 -18
- package/src/schema/ILlmFunction.ts +31 -65
- package/src/schema/index.ts +1 -0
- package/src/typings/DeepPartial.ts +39 -0
- package/src/typings/index.ts +1 -0
|
@@ -18,7 +18,6 @@ import { IHttpMigrateRoute } from "./IHttpMigrateRoute";
|
|
|
18
18
|
*
|
|
19
19
|
* Configure behavior via {@link IHttpLlmApplication.IConfig}:
|
|
20
20
|
*
|
|
21
|
-
* - {@link IHttpLlmApplication.IConfig.separate}: Split LLM vs human parameters
|
|
22
21
|
* - {@link IHttpLlmApplication.IConfig.maxLength}: Function name length limit
|
|
23
22
|
* - {@link ILlmSchema.IConfig.strict}: OpenAI structured output mode
|
|
24
23
|
*
|
|
@@ -35,15 +34,6 @@ export interface IHttpLlmApplication {
|
|
|
35
34
|
export declare namespace IHttpLlmApplication {
|
|
36
35
|
/** Configuration for HTTP LLM application composition. */
|
|
37
36
|
interface IConfig extends ILlmSchema.IConfig {
|
|
38
|
-
/**
|
|
39
|
-
* Separates parameters into LLM and human parts.
|
|
40
|
-
*
|
|
41
|
-
* Use for file uploads or sensitive data that LLM cannot handle. Return
|
|
42
|
-
* `true` for human-composed, `false` for LLM-composed.
|
|
43
|
-
*
|
|
44
|
-
* @default null
|
|
45
|
-
*/
|
|
46
|
-
separate: null | ((schema: ILlmSchema) => boolean);
|
|
47
37
|
/**
|
|
48
38
|
* Maximum function name length. Truncated or UUID if exceeded.
|
|
49
39
|
*
|
|
@@ -1,85 +1,29 @@
|
|
|
1
1
|
import { OpenApi } from "../openapi/OpenApi";
|
|
2
|
-
import {
|
|
3
|
-
import { IValidation } from "../schema/IValidation";
|
|
2
|
+
import { ILlmFunction } from "../schema/ILlmFunction";
|
|
4
3
|
import { IHttpMigrateRoute } from "./IHttpMigrateRoute";
|
|
5
4
|
/**
|
|
6
5
|
* LLM function calling schema from OpenAPI operation.
|
|
7
6
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* {@link IHttpLlmApplication}.
|
|
7
|
+
* Extends {@link ILlmFunction} with HTTP-specific properties. Generated from
|
|
8
|
+
* {@link OpenApi.IOperation} as part of {@link IHttpLlmApplication}.
|
|
11
9
|
*
|
|
12
|
-
*
|
|
10
|
+
* - {@link method}, {@link path}: HTTP endpoint info
|
|
11
|
+
* - {@link operation}: Source OpenAPI operation
|
|
12
|
+
* - {@link route}: Source migration route
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
* - {@link parameters}: Input schema with path/query/body merged
|
|
16
|
-
* - {@link output}: Response schema (undefined if void)
|
|
17
|
-
* - {@link description}: Critical for LLM function selection
|
|
18
|
-
* - {@link validate}: Built-in argument validator for error feedback
|
|
19
|
-
*
|
|
20
|
-
* The {@link validate} function is essential: LLMs make frequent type errors
|
|
21
|
-
* (e.g., `"123"` instead of `123`). Validate and retry improves success rate
|
|
22
|
-
* from ~50% to 99%.
|
|
14
|
+
* Inherits {@link parse} and {@link validate} from {@link ILlmFunction}.
|
|
23
15
|
*
|
|
24
16
|
* @author Jeongho Nam - https://github.com/samchon
|
|
25
17
|
*/
|
|
26
|
-
export interface IHttpLlmFunction {
|
|
18
|
+
export interface IHttpLlmFunction extends ILlmFunction {
|
|
27
19
|
/** HTTP method of the endpoint. */
|
|
28
20
|
method: "get" | "post" | "patch" | "put" | "delete";
|
|
29
21
|
/** Path of the endpoint. */
|
|
30
22
|
path: string;
|
|
31
|
-
/**
|
|
32
|
-
* Function name composed from {@link IHttpMigrateRoute.accessor}.
|
|
33
|
-
*
|
|
34
|
-
* @maxLength 64
|
|
35
|
-
*/
|
|
36
|
-
name: string;
|
|
37
|
-
/**
|
|
38
|
-
* Parameter schema.
|
|
39
|
-
*
|
|
40
|
-
* With keyword mode: single object with pathParameters, query, body merged.
|
|
41
|
-
* Without keyword mode: array of [pathParameters..., query?, body?].
|
|
42
|
-
*/
|
|
43
|
-
parameters: ILlmSchema.IParameters;
|
|
44
|
-
/**
|
|
45
|
-
* Separated parameters when {@link IHttpLlmApplication.IConfig.separate} is
|
|
46
|
-
* set.
|
|
47
|
-
*/
|
|
48
|
-
separated?: IHttpLlmFunction.ISeparated;
|
|
49
|
-
/**
|
|
50
|
-
* Return type as an object parameters schema.
|
|
51
|
-
*
|
|
52
|
-
* Wraps the return type in an {@link ILlmSchema.IParameters} object with
|
|
53
|
-
* `$defs` for shared type definitions and `properties` for the structured
|
|
54
|
-
* output. `undefined` if the endpoint returns void.
|
|
55
|
-
*/
|
|
56
|
-
output?: ILlmSchema.IParameters | undefined;
|
|
57
|
-
/** Function description for LLM context. Critical for function selection. */
|
|
58
|
-
description?: string | undefined;
|
|
59
|
-
/** Whether the function is deprecated. */
|
|
60
|
-
deprecated?: boolean | undefined;
|
|
61
23
|
/** Category tags from {@link OpenApi.IOperation.tags}. */
|
|
62
24
|
tags?: string[];
|
|
63
|
-
/**
|
|
64
|
-
* Validates LLM-composed arguments.
|
|
65
|
-
*
|
|
66
|
-
* LLMs frequently make type errors. Use this to provide validation feedback
|
|
67
|
-
* and retry. Success rate improves from ~50% to 99% on retry.
|
|
68
|
-
*/
|
|
69
|
-
validate: (args: unknown) => IValidation<unknown>;
|
|
70
25
|
/** Returns the source {@link OpenApi.IOperation}. */
|
|
71
26
|
operation: () => OpenApi.IOperation;
|
|
72
27
|
/** Returns the source {@link IHttpMigrateRoute}. */
|
|
73
28
|
route: () => IHttpMigrateRoute;
|
|
74
29
|
}
|
|
75
|
-
export declare namespace IHttpLlmFunction {
|
|
76
|
-
/** Collection of separated parameters. */
|
|
77
|
-
interface ISeparated {
|
|
78
|
-
/** Parameters for LLM composition. Always at least empty object. */
|
|
79
|
-
llm: ILlmSchema.IParameters;
|
|
80
|
-
/** Parameters for human composition. */
|
|
81
|
-
human: ILlmSchema.IParameters | null;
|
|
82
|
-
/** Validates separated LLM arguments. */
|
|
83
|
-
validate?: ((args: unknown) => IValidation<unknown>) | undefined;
|
|
84
|
-
}
|
|
85
|
-
}
|
package/lib/openapi/OpenApi.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ export declare namespace OpenApi {
|
|
|
35
35
|
*
|
|
36
36
|
* Contains all API metadata, paths, operations, and reusable components. The
|
|
37
37
|
* `x-samchon-emended-v4` marker indicates this document has been processed by
|
|
38
|
-
*
|
|
38
|
+
* `samchon/typia` to normalize schema formats.
|
|
39
39
|
*/
|
|
40
40
|
interface IDocument {
|
|
41
41
|
/** OpenAPI version. */
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { DeepPartial } from "../typings/DeepPartial";
|
|
2
|
+
/**
|
|
3
|
+
* Result of lenient JSON parsing.
|
|
4
|
+
*
|
|
5
|
+
* `IJsonParseResult<T>` represents the result of parsing JSON that may be
|
|
6
|
+
* incomplete, malformed, or contain non-standard syntax (e.g., unquoted keys,
|
|
7
|
+
* trailing commas, missing quotes).
|
|
8
|
+
*
|
|
9
|
+
* Unlike standard JSON parsing which fails on any syntax error, lenient parsing
|
|
10
|
+
* attempts to recover as much data as possible while reporting issues.
|
|
11
|
+
*
|
|
12
|
+
* Check the {@link IJsonParseResult.success} discriminator:
|
|
13
|
+
*
|
|
14
|
+
* - `true` → {@link IJsonParseResult.ISuccess} with parsed
|
|
15
|
+
* {@link IJsonParseResult.ISuccess.data}
|
|
16
|
+
* - `false` → {@link IJsonParseResult.IFailure} with partial
|
|
17
|
+
* {@link IJsonParseResult.IFailure.data} and
|
|
18
|
+
* {@link IJsonParseResult.IFailure.errors}
|
|
19
|
+
*
|
|
20
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
21
|
+
* @template T The expected type after successful parsing
|
|
22
|
+
*/
|
|
23
|
+
export type IJsonParseResult<T = unknown> = IJsonParseResult.ISuccess<T> | IJsonParseResult.IFailure<T>;
|
|
24
|
+
export declare namespace IJsonParseResult {
|
|
25
|
+
/**
|
|
26
|
+
* Successful parsing result.
|
|
27
|
+
*
|
|
28
|
+
* Indicates the JSON was parsed without any errors. The data may still have
|
|
29
|
+
* been recovered from non-standard syntax (e.g., unquoted keys, trailing
|
|
30
|
+
* commas), but no information was lost.
|
|
31
|
+
*
|
|
32
|
+
* @template T The parsed type
|
|
33
|
+
*/
|
|
34
|
+
interface ISuccess<T = unknown> {
|
|
35
|
+
/**
|
|
36
|
+
* Success discriminator.
|
|
37
|
+
*
|
|
38
|
+
* Always `true` for successful parsing.
|
|
39
|
+
*/
|
|
40
|
+
success: true;
|
|
41
|
+
/** The parsed data with correct type. */
|
|
42
|
+
data: T;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Failed parsing result with partial data and errors.
|
|
46
|
+
*
|
|
47
|
+
* Indicates the JSON had syntax errors that could not be fully recovered. The
|
|
48
|
+
* {@link data} contains whatever could be parsed, and {@link errors} describes
|
|
49
|
+
* what went wrong.
|
|
50
|
+
*
|
|
51
|
+
* @template T The expected type (data may be partial)
|
|
52
|
+
*/
|
|
53
|
+
interface IFailure<T = unknown> {
|
|
54
|
+
/**
|
|
55
|
+
* Success discriminator.
|
|
56
|
+
*
|
|
57
|
+
* Always `false` for failed parsing.
|
|
58
|
+
*/
|
|
59
|
+
success: false;
|
|
60
|
+
/**
|
|
61
|
+
* Partially parsed data.
|
|
62
|
+
*
|
|
63
|
+
* Contains whatever could be recovered from the malformed JSON. May be
|
|
64
|
+
* incomplete or have missing properties.
|
|
65
|
+
*/
|
|
66
|
+
data: DeepPartial<T>;
|
|
67
|
+
/**
|
|
68
|
+
* The original input string that was parsed.
|
|
69
|
+
*
|
|
70
|
+
* Preserved for debugging or error correction purposes.
|
|
71
|
+
*/
|
|
72
|
+
input: string;
|
|
73
|
+
/**
|
|
74
|
+
* Array of parsing errors encountered.
|
|
75
|
+
*
|
|
76
|
+
* Each error describes a specific issue found during parsing, with location
|
|
77
|
+
* and suggested fix.
|
|
78
|
+
*/
|
|
79
|
+
errors: IError[];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Detailed information about a parsing error.
|
|
83
|
+
*/
|
|
84
|
+
interface IError {
|
|
85
|
+
/**
|
|
86
|
+
* Property path to the error location.
|
|
87
|
+
*
|
|
88
|
+
* A dot-notation path from the root to the error location. Uses `$input` as
|
|
89
|
+
* the root.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* $input.user.email;
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* $input.items[0].price;
|
|
96
|
+
*/
|
|
97
|
+
path: string;
|
|
98
|
+
/**
|
|
99
|
+
* What was expected at this location.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* JSON value (string, number, boolean, null, object, or array)
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* quoted string
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ":";
|
|
109
|
+
*/
|
|
110
|
+
expected: string;
|
|
111
|
+
/**
|
|
112
|
+
* Description of what was actually found.
|
|
113
|
+
*
|
|
114
|
+
* Human/AI-readable message explaining the issue.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* unquoted string 'abc' - did you forget quotes?
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* missing opening quote for 'hello'
|
|
121
|
+
*/
|
|
122
|
+
value: unknown;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IJsonParseResult.js","sourceRoot":"","sources":["../../src/schema/IJsonParseResult.ts"],"names":[],"mappings":""}
|
|
@@ -10,8 +10,6 @@ import { IValidation } from "./IValidation";
|
|
|
10
10
|
*
|
|
11
11
|
* Configure behavior via {@link ILlmApplication.IConfig}:
|
|
12
12
|
*
|
|
13
|
-
* - {@link ILlmApplication.IConfig.separate}: Split parameters into LLM-fillable
|
|
14
|
-
* vs human-required (e.g., file uploads, passwords)
|
|
15
13
|
* - {@link ILlmApplication.IConfig.validate}: Custom validation per method
|
|
16
14
|
* - {@link ILlmSchema.IConfig.strict}: OpenAI structured output mode
|
|
17
15
|
* - {@link ILlmSchema.IConfig.reference}: Control `$ref` inlining behavior
|
|
@@ -52,23 +50,10 @@ export declare namespace ILlmApplication {
|
|
|
52
50
|
* Configuration for LLM application generation.
|
|
53
51
|
*
|
|
54
52
|
* Extends {@link ILlmSchema.IConfig} with application-specific options for
|
|
55
|
-
*
|
|
56
|
-
*
|
|
53
|
+
* custom validation. These settings control how the application schema is
|
|
54
|
+
* generated from the source class.
|
|
57
55
|
*/
|
|
58
56
|
interface IConfig<Class extends object = any> extends ILlmSchema.IConfig {
|
|
59
|
-
/**
|
|
60
|
-
* Function to separate LLM-fillable from human-required parameters.
|
|
61
|
-
*
|
|
62
|
-
* When provided, this function is called for each parameter schema to
|
|
63
|
-
* determine if it should be filled by the LLM (`false`) or require human
|
|
64
|
-
* input (`true`). Use this for sensitive data like passwords, file uploads,
|
|
65
|
-
* or data the LLM cannot generate.
|
|
66
|
-
*
|
|
67
|
-
* @default null (no separation)
|
|
68
|
-
* @param schema - The parameter schema to evaluate
|
|
69
|
-
* @returns `true` if human input required, `false` if LLM can fill
|
|
70
|
-
*/
|
|
71
|
-
separate: null | ((schema: ILlmSchema) => boolean);
|
|
72
57
|
/**
|
|
73
58
|
* Custom validation functions per method name.
|
|
74
59
|
*
|
|
@@ -92,4 +77,12 @@ export declare namespace ILlmApplication {
|
|
|
92
77
|
type IValidationHook<Class extends object> = {
|
|
93
78
|
[K in keyof Class]?: Class[K] extends (args: infer Argument) => unknown ? (input: unknown) => IValidation<Argument> : never;
|
|
94
79
|
};
|
|
80
|
+
/**
|
|
81
|
+
* Internal type for typia transformer.
|
|
82
|
+
*
|
|
83
|
+
* @ignore
|
|
84
|
+
*/
|
|
85
|
+
interface __IPrimitive<Class extends object = any> extends Omit<ILlmApplication<Class>, "config" | "functions"> {
|
|
86
|
+
functions: Omit<ILlmFunction, "parse">[];
|
|
87
|
+
}
|
|
95
88
|
}
|
|
@@ -1,22 +1,17 @@
|
|
|
1
|
+
import { IJsonParseResult } from "./IJsonParseResult";
|
|
1
2
|
import { ILlmSchema } from "./ILlmSchema";
|
|
2
3
|
import { IValidation } from "./IValidation";
|
|
3
4
|
/**
|
|
4
|
-
* LLM function calling
|
|
5
|
+
* LLM function calling schema for TypeScript functions.
|
|
5
6
|
*
|
|
6
|
-
* `
|
|
7
|
-
*
|
|
7
|
+
* Generated by `typia.llm.application<App>()` as part of
|
|
8
|
+
* {@link ILlmApplication}.
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
-
* {@link parameters} schema
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* The built-in {@link validate} function checks LLM-generated arguments against
|
|
15
|
-
* the schema, enabling auto-correction when the LLM makes type errors (e.g.,
|
|
16
|
-
* returning `"123"` instead of `123`).
|
|
17
|
-
*
|
|
18
|
-
* Use {@link separated} when some parameters require human input (files,
|
|
19
|
-
* passwords) via {@link ILlmApplication.IConfig.separate}.
|
|
10
|
+
* - {@link name}: Function identifier (max 64 chars for OpenAI)
|
|
11
|
+
* - {@link parameters}: Input schema, {@link output}: Return schema
|
|
12
|
+
* - {@link description}: Guides LLM function selection
|
|
13
|
+
* - {@link parse}: Lenient JSON parser with type coercion
|
|
14
|
+
* - {@link validate}: Argument validator for LLM error correction
|
|
20
15
|
*
|
|
21
16
|
* @author Jeongho Nam - https://github.com/samchon
|
|
22
17
|
*/
|
|
@@ -38,20 +33,12 @@ export interface ILlmFunction {
|
|
|
38
33
|
* parameter.
|
|
39
34
|
*/
|
|
40
35
|
parameters: ILlmSchema.IParameters;
|
|
41
|
-
/**
|
|
42
|
-
* Parameters split between LLM and human input.
|
|
43
|
-
*
|
|
44
|
-
* Present when {@link ILlmApplication.IConfig.separate} is configured. Allows
|
|
45
|
-
* separating parameters that the LLM can fill from those requiring human
|
|
46
|
-
* input (e.g., file uploads, passwords).
|
|
47
|
-
*/
|
|
48
|
-
separated?: ILlmFunction.ISeparated;
|
|
49
36
|
/**
|
|
50
37
|
* Schema for the return type.
|
|
51
38
|
*
|
|
52
39
|
* Defines the expected output type as an object parameters schema, wrapping
|
|
53
|
-
* the return type in an {@link ILlmSchema.IParameters} object with `$defs`
|
|
54
|
-
*
|
|
40
|
+
* the return type in an {@link ILlmSchema.IParameters} object with `$defs` for
|
|
41
|
+
* shared type definitions and `properties` for the structured output.
|
|
55
42
|
*
|
|
56
43
|
* `undefined` when the function returns `void` or has no meaningful return
|
|
57
44
|
* value.
|
|
@@ -80,6 +67,25 @@ export interface ILlmFunction {
|
|
|
80
67
|
* functions and filtering the function list.
|
|
81
68
|
*/
|
|
82
69
|
tags?: string[] | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Lenient JSON parser with schema-based coercion.
|
|
72
|
+
*
|
|
73
|
+
* Handles incomplete/malformed JSON commonly produced by LLMs:
|
|
74
|
+
*
|
|
75
|
+
* - Unclosed brackets, strings, trailing commas
|
|
76
|
+
* - JavaScript-style comments (`//` and multi-line)
|
|
77
|
+
* - Unquoted object keys, incomplete keywords (`tru`, `fal`, `nul`)
|
|
78
|
+
* - Markdown code block extraction, junk prefix skipping
|
|
79
|
+
*
|
|
80
|
+
* Also coerces double-stringified values (`"42"` → `42`) using the
|
|
81
|
+
* {@link parameters} schema.
|
|
82
|
+
*
|
|
83
|
+
* Type validation is NOT performed — use {@link validate} after parsing.
|
|
84
|
+
*
|
|
85
|
+
* @param str Raw JSON string from LLM output
|
|
86
|
+
* @returns Parse result with data on success, or partial data with errors
|
|
87
|
+
*/
|
|
88
|
+
parse: (str: string) => IJsonParseResult<unknown>;
|
|
83
89
|
/**
|
|
84
90
|
* Validates LLM-generated arguments against the schema.
|
|
85
91
|
*
|
|
@@ -98,41 +104,3 @@ export interface ILlmFunction {
|
|
|
98
104
|
*/
|
|
99
105
|
validate: (args: unknown) => IValidation<unknown>;
|
|
100
106
|
}
|
|
101
|
-
export declare namespace ILlmFunction {
|
|
102
|
-
/**
|
|
103
|
-
* Separated parameter schemas for hybrid LLM/human input.
|
|
104
|
-
*
|
|
105
|
-
* When a function has parameters that cannot or should not be filled by the
|
|
106
|
-
* LLM (e.g., file uploads, passwords, sensitive data), the parameters are
|
|
107
|
-
* split into two schemas.
|
|
108
|
-
*/
|
|
109
|
-
interface ISeparated {
|
|
110
|
-
/**
|
|
111
|
-
* Parameters the LLM should fill.
|
|
112
|
-
*
|
|
113
|
-
* Contains only the parameters that are safe and appropriate for the LLM to
|
|
114
|
-
* generate values for.
|
|
115
|
-
*/
|
|
116
|
-
llm: ILlmSchema.IParameters;
|
|
117
|
-
/**
|
|
118
|
-
* Parameters requiring human input.
|
|
119
|
-
*
|
|
120
|
-
* Contains parameters that must be provided by the user directly, such as
|
|
121
|
-
* file uploads, passwords, or other sensitive data. `null` when all
|
|
122
|
-
* parameters can be filled by the LLM.
|
|
123
|
-
*/
|
|
124
|
-
human: ILlmSchema.IParameters | null;
|
|
125
|
-
/**
|
|
126
|
-
* Validates the LLM portion of separated parameters.
|
|
127
|
-
*
|
|
128
|
-
* Validates only the LLM-fillable portion, allowing human parameters to be
|
|
129
|
-
* validated separately with appropriate handling.
|
|
130
|
-
*
|
|
131
|
-
* When validation fails, use `stringifyValidationFailure()` from
|
|
132
|
-
* `@typia/utils` to format the error for LLM feedback.
|
|
133
|
-
*
|
|
134
|
-
* @see stringifyValidationFailure Format errors for LLM auto-correction
|
|
135
|
-
*/
|
|
136
|
-
validate?: ((args: unknown) => IValidation<unknown>) | undefined;
|
|
137
|
-
}
|
|
138
|
-
}
|
package/lib/schema/index.d.ts
CHANGED
package/lib/schema/index.js
CHANGED
|
@@ -25,4 +25,5 @@ __exportStar(require("./ILlmController"), exports);
|
|
|
25
25
|
__exportStar(require("./ILlmApplication"), exports);
|
|
26
26
|
__exportStar(require("./ILlmFunction"), exports);
|
|
27
27
|
__exportStar(require("./ILlmSchema"), exports);
|
|
28
|
+
__exportStar(require("./IJsonParseResult"), exports);
|
|
28
29
|
//# sourceMappingURL=index.js.map
|
package/lib/schema/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,gDAA8B;AAE9B,8DAA4C;AAC5C,2DAAyC;AACzC,0DAAwC;AACxC,oDAAkC;AAClC,yDAAuC;AAEvC,mDAAiC;AACjC,oDAAkC;AAClC,iDAA+B;AAC/B,+CAA6B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,gDAA8B;AAE9B,8DAA4C;AAC5C,2DAAyC;AACzC,0DAAwC;AACxC,oDAAkC;AAClC,yDAAuC;AAEvC,mDAAiC;AACjC,oDAAkC;AAClC,iDAA+B;AAC/B,+CAA6B;AAC7B,qDAAmC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively makes all properties of a type optional.
|
|
3
|
+
*
|
|
4
|
+
* `DeepPartial<T>` transforms a type by making every property optional at all
|
|
5
|
+
* nesting levels. Unlike TypeScript's built-in `Partial<T>` which only affects
|
|
6
|
+
* the top level, this utility recursively applies optionality to nested objects
|
|
7
|
+
* and arrays.
|
|
8
|
+
*
|
|
9
|
+
* Used primarily in {@link IJsonParseResult.IFailure} to represent partially
|
|
10
|
+
* recovered data from malformed JSON, where some properties may be missing due
|
|
11
|
+
* to parsing errors.
|
|
12
|
+
*
|
|
13
|
+
* Behavior:
|
|
14
|
+
*
|
|
15
|
+
* - **Primitives** (`string`, `number`, `boolean`, `bigint`, `symbol`, `null`,
|
|
16
|
+
* `undefined`): returned as-is
|
|
17
|
+
* - **Functions**: returned as-is
|
|
18
|
+
* - **Arrays**: element type becomes `DeepPartial<U>`
|
|
19
|
+
* - **Objects**: all properties become optional with `DeepPartial` applied
|
|
20
|
+
*
|
|
21
|
+
* @author Michael - https://github.com/8471919
|
|
22
|
+
* @template T The type to make deeply partial
|
|
23
|
+
*/
|
|
24
|
+
export type DeepPartial<T> = T extends string | number | boolean | bigint | symbol | null | undefined ? T : T extends (...args: unknown[]) => unknown ? T : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends object ? {
|
|
25
|
+
[P in keyof T]?: DeepPartial<T[P]>;
|
|
26
|
+
} : T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DeepPartial.js","sourceRoot":"","sources":["../../src/typings/DeepPartial.ts"],"names":[],"mappings":""}
|
package/lib/typings/index.d.ts
CHANGED
package/lib/typings/index.js
CHANGED
|
@@ -22,6 +22,7 @@ __exportStar(require("./Resolved"), exports);
|
|
|
22
22
|
__exportStar(require("./SnakeCase"), exports);
|
|
23
23
|
__exportStar(require("./Atomic"), exports);
|
|
24
24
|
__exportStar(require("./ClassProperties"), exports);
|
|
25
|
+
__exportStar(require("./DeepPartial"), exports);
|
|
25
26
|
__exportStar(require("./OmitNever"), exports);
|
|
26
27
|
__exportStar(require("./ProtobufAtomic"), exports);
|
|
27
28
|
__exportStar(require("./SpecialFields"), exports);
|
package/lib/typings/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/typings/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,8CAA4B;AAC5B,+CAA6B;AAC7B,8CAA4B;AAC5B,6CAA2B;AAC3B,8CAA4B;AAE5B,2CAAyB;AACzB,oDAAkC;AAClC,8CAA4B;AAC5B,mDAAiC;AACjC,kDAAgC;AAChC,mDAAiC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/typings/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,8CAA4B;AAC5B,+CAA6B;AAC7B,8CAA4B;AAC5B,6CAA2B;AAC3B,8CAA4B;AAE5B,2CAAyB;AACzB,oDAAkC;AAClC,gDAA8B;AAC9B,8CAA4B;AAC5B,mDAAiC;AACjC,kDAAgC;AAChC,mDAAiC"}
|
package/package.json
CHANGED
|
@@ -19,7 +19,6 @@ import { IHttpMigrateRoute } from "./IHttpMigrateRoute";
|
|
|
19
19
|
*
|
|
20
20
|
* Configure behavior via {@link IHttpLlmApplication.IConfig}:
|
|
21
21
|
*
|
|
22
|
-
* - {@link IHttpLlmApplication.IConfig.separate}: Split LLM vs human parameters
|
|
23
22
|
* - {@link IHttpLlmApplication.IConfig.maxLength}: Function name length limit
|
|
24
23
|
* - {@link ILlmSchema.IConfig.strict}: OpenAI structured output mode
|
|
25
24
|
*
|
|
@@ -38,16 +37,6 @@ export interface IHttpLlmApplication {
|
|
|
38
37
|
export namespace IHttpLlmApplication {
|
|
39
38
|
/** Configuration for HTTP LLM application composition. */
|
|
40
39
|
export interface IConfig extends ILlmSchema.IConfig {
|
|
41
|
-
/**
|
|
42
|
-
* Separates parameters into LLM and human parts.
|
|
43
|
-
*
|
|
44
|
-
* Use for file uploads or sensitive data that LLM cannot handle. Return
|
|
45
|
-
* `true` for human-composed, `false` for LLM-composed.
|
|
46
|
-
*
|
|
47
|
-
* @default null
|
|
48
|
-
*/
|
|
49
|
-
separate: null | ((schema: ILlmSchema) => boolean);
|
|
50
|
-
|
|
51
40
|
/**
|
|
52
41
|
* Maximum function name length. Truncated or UUID if exceeded.
|
|
53
42
|
*
|
|
@@ -1,99 +1,34 @@
|
|
|
1
1
|
import { OpenApi } from "../openapi/OpenApi";
|
|
2
|
-
import {
|
|
3
|
-
import { IValidation } from "../schema/IValidation";
|
|
2
|
+
import { ILlmFunction } from "../schema/ILlmFunction";
|
|
4
3
|
import { IHttpMigrateRoute } from "./IHttpMigrateRoute";
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* LLM function calling schema from OpenAPI operation.
|
|
8
7
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* {@link IHttpLlmApplication}.
|
|
8
|
+
* Extends {@link ILlmFunction} with HTTP-specific properties. Generated from
|
|
9
|
+
* {@link OpenApi.IOperation} as part of {@link IHttpLlmApplication}.
|
|
12
10
|
*
|
|
13
|
-
*
|
|
11
|
+
* - {@link method}, {@link path}: HTTP endpoint info
|
|
12
|
+
* - {@link operation}: Source OpenAPI operation
|
|
13
|
+
* - {@link route}: Source migration route
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
* - {@link parameters}: Input schema with path/query/body merged
|
|
17
|
-
* - {@link output}: Response schema (undefined if void)
|
|
18
|
-
* - {@link description}: Critical for LLM function selection
|
|
19
|
-
* - {@link validate}: Built-in argument validator for error feedback
|
|
20
|
-
*
|
|
21
|
-
* The {@link validate} function is essential: LLMs make frequent type errors
|
|
22
|
-
* (e.g., `"123"` instead of `123`). Validate and retry improves success rate
|
|
23
|
-
* from ~50% to 99%.
|
|
15
|
+
* Inherits {@link parse} and {@link validate} from {@link ILlmFunction}.
|
|
24
16
|
*
|
|
25
17
|
* @author Jeongho Nam - https://github.com/samchon
|
|
26
18
|
*/
|
|
27
|
-
export interface IHttpLlmFunction {
|
|
19
|
+
export interface IHttpLlmFunction extends ILlmFunction {
|
|
28
20
|
/** HTTP method of the endpoint. */
|
|
29
21
|
method: "get" | "post" | "patch" | "put" | "delete";
|
|
30
22
|
|
|
31
23
|
/** Path of the endpoint. */
|
|
32
24
|
path: string;
|
|
33
25
|
|
|
34
|
-
/**
|
|
35
|
-
* Function name composed from {@link IHttpMigrateRoute.accessor}.
|
|
36
|
-
*
|
|
37
|
-
* @maxLength 64
|
|
38
|
-
*/
|
|
39
|
-
name: string;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Parameter schema.
|
|
43
|
-
*
|
|
44
|
-
* With keyword mode: single object with pathParameters, query, body merged.
|
|
45
|
-
* Without keyword mode: array of [pathParameters..., query?, body?].
|
|
46
|
-
*/
|
|
47
|
-
parameters: ILlmSchema.IParameters;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Separated parameters when {@link IHttpLlmApplication.IConfig.separate} is
|
|
51
|
-
* set.
|
|
52
|
-
*/
|
|
53
|
-
separated?: IHttpLlmFunction.ISeparated;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Return type as an object parameters schema.
|
|
57
|
-
*
|
|
58
|
-
* Wraps the return type in an {@link ILlmSchema.IParameters} object with
|
|
59
|
-
* `$defs` for shared type definitions and `properties` for the structured
|
|
60
|
-
* output. `undefined` if the endpoint returns void.
|
|
61
|
-
*/
|
|
62
|
-
output?: ILlmSchema.IParameters | undefined;
|
|
63
|
-
|
|
64
|
-
/** Function description for LLM context. Critical for function selection. */
|
|
65
|
-
description?: string | undefined;
|
|
66
|
-
|
|
67
|
-
/** Whether the function is deprecated. */
|
|
68
|
-
deprecated?: boolean | undefined;
|
|
69
|
-
|
|
70
26
|
/** Category tags from {@link OpenApi.IOperation.tags}. */
|
|
71
27
|
tags?: string[];
|
|
72
28
|
|
|
73
|
-
/**
|
|
74
|
-
* Validates LLM-composed arguments.
|
|
75
|
-
*
|
|
76
|
-
* LLMs frequently make type errors. Use this to provide validation feedback
|
|
77
|
-
* and retry. Success rate improves from ~50% to 99% on retry.
|
|
78
|
-
*/
|
|
79
|
-
validate: (args: unknown) => IValidation<unknown>;
|
|
80
|
-
|
|
81
29
|
/** Returns the source {@link OpenApi.IOperation}. */
|
|
82
30
|
operation: () => OpenApi.IOperation;
|
|
83
31
|
|
|
84
32
|
/** Returns the source {@link IHttpMigrateRoute}. */
|
|
85
33
|
route: () => IHttpMigrateRoute;
|
|
86
34
|
}
|
|
87
|
-
export namespace IHttpLlmFunction {
|
|
88
|
-
/** Collection of separated parameters. */
|
|
89
|
-
export interface ISeparated {
|
|
90
|
-
/** Parameters for LLM composition. Always at least empty object. */
|
|
91
|
-
llm: ILlmSchema.IParameters;
|
|
92
|
-
|
|
93
|
-
/** Parameters for human composition. */
|
|
94
|
-
human: ILlmSchema.IParameters | null;
|
|
95
|
-
|
|
96
|
-
/** Validates separated LLM arguments. */
|
|
97
|
-
validate?: ((args: unknown) => IValidation<unknown>) | undefined;
|
|
98
|
-
}
|
|
99
|
-
}
|
package/src/openapi/OpenApi.ts
CHANGED
|
@@ -45,7 +45,7 @@ export namespace OpenApi {
|
|
|
45
45
|
*
|
|
46
46
|
* Contains all API metadata, paths, operations, and reusable components. The
|
|
47
47
|
* `x-samchon-emended-v4` marker indicates this document has been processed by
|
|
48
|
-
*
|
|
48
|
+
* `samchon/typia` to normalize schema formats.
|
|
49
49
|
*/
|
|
50
50
|
export interface IDocument {
|
|
51
51
|
/** OpenAPI version. */
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { DeepPartial } from "../typings/DeepPartial";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Result of lenient JSON parsing.
|
|
5
|
+
*
|
|
6
|
+
* `IJsonParseResult<T>` represents the result of parsing JSON that may be
|
|
7
|
+
* incomplete, malformed, or contain non-standard syntax (e.g., unquoted keys,
|
|
8
|
+
* trailing commas, missing quotes).
|
|
9
|
+
*
|
|
10
|
+
* Unlike standard JSON parsing which fails on any syntax error, lenient parsing
|
|
11
|
+
* attempts to recover as much data as possible while reporting issues.
|
|
12
|
+
*
|
|
13
|
+
* Check the {@link IJsonParseResult.success} discriminator:
|
|
14
|
+
*
|
|
15
|
+
* - `true` → {@link IJsonParseResult.ISuccess} with parsed
|
|
16
|
+
* {@link IJsonParseResult.ISuccess.data}
|
|
17
|
+
* - `false` → {@link IJsonParseResult.IFailure} with partial
|
|
18
|
+
* {@link IJsonParseResult.IFailure.data} and
|
|
19
|
+
* {@link IJsonParseResult.IFailure.errors}
|
|
20
|
+
*
|
|
21
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
22
|
+
* @template T The expected type after successful parsing
|
|
23
|
+
*/
|
|
24
|
+
export type IJsonParseResult<T = unknown> =
|
|
25
|
+
| IJsonParseResult.ISuccess<T>
|
|
26
|
+
| IJsonParseResult.IFailure<T>;
|
|
27
|
+
|
|
28
|
+
export namespace IJsonParseResult {
|
|
29
|
+
/**
|
|
30
|
+
* Successful parsing result.
|
|
31
|
+
*
|
|
32
|
+
* Indicates the JSON was parsed without any errors. The data may still have
|
|
33
|
+
* been recovered from non-standard syntax (e.g., unquoted keys, trailing
|
|
34
|
+
* commas), but no information was lost.
|
|
35
|
+
*
|
|
36
|
+
* @template T The parsed type
|
|
37
|
+
*/
|
|
38
|
+
export interface ISuccess<T = unknown> {
|
|
39
|
+
/**
|
|
40
|
+
* Success discriminator.
|
|
41
|
+
*
|
|
42
|
+
* Always `true` for successful parsing.
|
|
43
|
+
*/
|
|
44
|
+
success: true;
|
|
45
|
+
|
|
46
|
+
/** The parsed data with correct type. */
|
|
47
|
+
data: T;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Failed parsing result with partial data and errors.
|
|
52
|
+
*
|
|
53
|
+
* Indicates the JSON had syntax errors that could not be fully recovered. The
|
|
54
|
+
* {@link data} contains whatever could be parsed, and {@link errors} describes
|
|
55
|
+
* what went wrong.
|
|
56
|
+
*
|
|
57
|
+
* @template T The expected type (data may be partial)
|
|
58
|
+
*/
|
|
59
|
+
export interface IFailure<T = unknown> {
|
|
60
|
+
/**
|
|
61
|
+
* Success discriminator.
|
|
62
|
+
*
|
|
63
|
+
* Always `false` for failed parsing.
|
|
64
|
+
*/
|
|
65
|
+
success: false;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Partially parsed data.
|
|
69
|
+
*
|
|
70
|
+
* Contains whatever could be recovered from the malformed JSON. May be
|
|
71
|
+
* incomplete or have missing properties.
|
|
72
|
+
*/
|
|
73
|
+
data: DeepPartial<T>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The original input string that was parsed.
|
|
77
|
+
*
|
|
78
|
+
* Preserved for debugging or error correction purposes.
|
|
79
|
+
*/
|
|
80
|
+
input: string;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Array of parsing errors encountered.
|
|
84
|
+
*
|
|
85
|
+
* Each error describes a specific issue found during parsing, with location
|
|
86
|
+
* and suggested fix.
|
|
87
|
+
*/
|
|
88
|
+
errors: IError[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Detailed information about a parsing error.
|
|
93
|
+
*/
|
|
94
|
+
export interface IError {
|
|
95
|
+
/**
|
|
96
|
+
* Property path to the error location.
|
|
97
|
+
*
|
|
98
|
+
* A dot-notation path from the root to the error location. Uses `$input` as
|
|
99
|
+
* the root.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* $input.user.email;
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* $input.items[0].price;
|
|
106
|
+
*/
|
|
107
|
+
path: string;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* What was expected at this location.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* JSON value (string, number, boolean, null, object, or array)
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* quoted string
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ":";
|
|
120
|
+
*/
|
|
121
|
+
expected: string;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Description of what was actually found.
|
|
125
|
+
*
|
|
126
|
+
* Human/AI-readable message explaining the issue.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* unquoted string 'abc' - did you forget quotes?
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* missing opening quote for 'hello'
|
|
133
|
+
*/
|
|
134
|
+
value: unknown;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -11,8 +11,6 @@ import { IValidation } from "./IValidation";
|
|
|
11
11
|
*
|
|
12
12
|
* Configure behavior via {@link ILlmApplication.IConfig}:
|
|
13
13
|
*
|
|
14
|
-
* - {@link ILlmApplication.IConfig.separate}: Split parameters into LLM-fillable
|
|
15
|
-
* vs human-required (e.g., file uploads, passwords)
|
|
16
14
|
* - {@link ILlmApplication.IConfig.validate}: Custom validation per method
|
|
17
15
|
* - {@link ILlmSchema.IConfig.strict}: OpenAI structured output mode
|
|
18
16
|
* - {@link ILlmSchema.IConfig.reference}: Control `$ref` inlining behavior
|
|
@@ -55,25 +53,11 @@ export namespace ILlmApplication {
|
|
|
55
53
|
* Configuration for LLM application generation.
|
|
56
54
|
*
|
|
57
55
|
* Extends {@link ILlmSchema.IConfig} with application-specific options for
|
|
58
|
-
*
|
|
59
|
-
*
|
|
56
|
+
* custom validation. These settings control how the application schema is
|
|
57
|
+
* generated from the source class.
|
|
60
58
|
*/
|
|
61
59
|
export interface IConfig<Class extends object = any>
|
|
62
60
|
extends ILlmSchema.IConfig {
|
|
63
|
-
/**
|
|
64
|
-
* Function to separate LLM-fillable from human-required parameters.
|
|
65
|
-
*
|
|
66
|
-
* When provided, this function is called for each parameter schema to
|
|
67
|
-
* determine if it should be filled by the LLM (`false`) or require human
|
|
68
|
-
* input (`true`). Use this for sensitive data like passwords, file uploads,
|
|
69
|
-
* or data the LLM cannot generate.
|
|
70
|
-
*
|
|
71
|
-
* @default null (no separation)
|
|
72
|
-
* @param schema - The parameter schema to evaluate
|
|
73
|
-
* @returns `true` if human input required, `false` if LLM can fill
|
|
74
|
-
*/
|
|
75
|
-
separate: null | ((schema: ILlmSchema) => boolean);
|
|
76
|
-
|
|
77
61
|
/**
|
|
78
62
|
* Custom validation functions per method name.
|
|
79
63
|
*
|
|
@@ -100,4 +84,16 @@ export namespace ILlmApplication {
|
|
|
100
84
|
? (input: unknown) => IValidation<Argument>
|
|
101
85
|
: never;
|
|
102
86
|
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Internal type for typia transformer.
|
|
90
|
+
*
|
|
91
|
+
* @ignore
|
|
92
|
+
*/
|
|
93
|
+
export interface __IPrimitive<Class extends object = any> extends Omit<
|
|
94
|
+
ILlmApplication<Class>,
|
|
95
|
+
"config" | "functions"
|
|
96
|
+
> {
|
|
97
|
+
functions: Omit<ILlmFunction, "parse">[];
|
|
98
|
+
}
|
|
103
99
|
}
|
|
@@ -1,23 +1,18 @@
|
|
|
1
|
+
import { IJsonParseResult } from "./IJsonParseResult";
|
|
1
2
|
import { ILlmSchema } from "./ILlmSchema";
|
|
2
3
|
import { IValidation } from "./IValidation";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
* LLM function calling
|
|
6
|
+
* LLM function calling schema for TypeScript functions.
|
|
6
7
|
*
|
|
7
|
-
* `
|
|
8
|
-
*
|
|
8
|
+
* Generated by `typia.llm.application<App>()` as part of
|
|
9
|
+
* {@link ILlmApplication}.
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
* {@link parameters} schema
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* The built-in {@link validate} function checks LLM-generated arguments against
|
|
16
|
-
* the schema, enabling auto-correction when the LLM makes type errors (e.g.,
|
|
17
|
-
* returning `"123"` instead of `123`).
|
|
18
|
-
*
|
|
19
|
-
* Use {@link separated} when some parameters require human input (files,
|
|
20
|
-
* passwords) via {@link ILlmApplication.IConfig.separate}.
|
|
11
|
+
* - {@link name}: Function identifier (max 64 chars for OpenAI)
|
|
12
|
+
* - {@link parameters}: Input schema, {@link output}: Return schema
|
|
13
|
+
* - {@link description}: Guides LLM function selection
|
|
14
|
+
* - {@link parse}: Lenient JSON parser with type coercion
|
|
15
|
+
* - {@link validate}: Argument validator for LLM error correction
|
|
21
16
|
*
|
|
22
17
|
* @author Jeongho Nam - https://github.com/samchon
|
|
23
18
|
*/
|
|
@@ -41,21 +36,12 @@ export interface ILlmFunction {
|
|
|
41
36
|
*/
|
|
42
37
|
parameters: ILlmSchema.IParameters;
|
|
43
38
|
|
|
44
|
-
/**
|
|
45
|
-
* Parameters split between LLM and human input.
|
|
46
|
-
*
|
|
47
|
-
* Present when {@link ILlmApplication.IConfig.separate} is configured. Allows
|
|
48
|
-
* separating parameters that the LLM can fill from those requiring human
|
|
49
|
-
* input (e.g., file uploads, passwords).
|
|
50
|
-
*/
|
|
51
|
-
separated?: ILlmFunction.ISeparated;
|
|
52
|
-
|
|
53
39
|
/**
|
|
54
40
|
* Schema for the return type.
|
|
55
41
|
*
|
|
56
42
|
* Defines the expected output type as an object parameters schema, wrapping
|
|
57
|
-
* the return type in an {@link ILlmSchema.IParameters} object with `$defs`
|
|
58
|
-
*
|
|
43
|
+
* the return type in an {@link ILlmSchema.IParameters} object with `$defs` for
|
|
44
|
+
* shared type definitions and `properties` for the structured output.
|
|
59
45
|
*
|
|
60
46
|
* `undefined` when the function returns `void` or has no meaningful return
|
|
61
47
|
* value.
|
|
@@ -88,6 +74,26 @@ export interface ILlmFunction {
|
|
|
88
74
|
*/
|
|
89
75
|
tags?: string[] | undefined;
|
|
90
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Lenient JSON parser with schema-based coercion.
|
|
79
|
+
*
|
|
80
|
+
* Handles incomplete/malformed JSON commonly produced by LLMs:
|
|
81
|
+
*
|
|
82
|
+
* - Unclosed brackets, strings, trailing commas
|
|
83
|
+
* - JavaScript-style comments (`//` and multi-line)
|
|
84
|
+
* - Unquoted object keys, incomplete keywords (`tru`, `fal`, `nul`)
|
|
85
|
+
* - Markdown code block extraction, junk prefix skipping
|
|
86
|
+
*
|
|
87
|
+
* Also coerces double-stringified values (`"42"` → `42`) using the
|
|
88
|
+
* {@link parameters} schema.
|
|
89
|
+
*
|
|
90
|
+
* Type validation is NOT performed — use {@link validate} after parsing.
|
|
91
|
+
*
|
|
92
|
+
* @param str Raw JSON string from LLM output
|
|
93
|
+
* @returns Parse result with data on success, or partial data with errors
|
|
94
|
+
*/
|
|
95
|
+
parse: (str: string) => IJsonParseResult<unknown>;
|
|
96
|
+
|
|
91
97
|
/**
|
|
92
98
|
* Validates LLM-generated arguments against the schema.
|
|
93
99
|
*
|
|
@@ -106,43 +112,3 @@ export interface ILlmFunction {
|
|
|
106
112
|
*/
|
|
107
113
|
validate: (args: unknown) => IValidation<unknown>;
|
|
108
114
|
}
|
|
109
|
-
export namespace ILlmFunction {
|
|
110
|
-
/**
|
|
111
|
-
* Separated parameter schemas for hybrid LLM/human input.
|
|
112
|
-
*
|
|
113
|
-
* When a function has parameters that cannot or should not be filled by the
|
|
114
|
-
* LLM (e.g., file uploads, passwords, sensitive data), the parameters are
|
|
115
|
-
* split into two schemas.
|
|
116
|
-
*/
|
|
117
|
-
export interface ISeparated {
|
|
118
|
-
/**
|
|
119
|
-
* Parameters the LLM should fill.
|
|
120
|
-
*
|
|
121
|
-
* Contains only the parameters that are safe and appropriate for the LLM to
|
|
122
|
-
* generate values for.
|
|
123
|
-
*/
|
|
124
|
-
llm: ILlmSchema.IParameters;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Parameters requiring human input.
|
|
128
|
-
*
|
|
129
|
-
* Contains parameters that must be provided by the user directly, such as
|
|
130
|
-
* file uploads, passwords, or other sensitive data. `null` when all
|
|
131
|
-
* parameters can be filled by the LLM.
|
|
132
|
-
*/
|
|
133
|
-
human: ILlmSchema.IParameters | null;
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Validates the LLM portion of separated parameters.
|
|
137
|
-
*
|
|
138
|
-
* Validates only the LLM-fillable portion, allowing human parameters to be
|
|
139
|
-
* validated separately with appropriate handling.
|
|
140
|
-
*
|
|
141
|
-
* When validation fails, use `stringifyValidationFailure()` from
|
|
142
|
-
* `@typia/utils` to format the error for LLM feedback.
|
|
143
|
-
*
|
|
144
|
-
* @see stringifyValidationFailure Format errors for LLM auto-correction
|
|
145
|
-
*/
|
|
146
|
-
validate?: ((args: unknown) => IValidation<unknown>) | undefined;
|
|
147
|
-
}
|
|
148
|
-
}
|
package/src/schema/index.ts
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively makes all properties of a type optional.
|
|
3
|
+
*
|
|
4
|
+
* `DeepPartial<T>` transforms a type by making every property optional at all
|
|
5
|
+
* nesting levels. Unlike TypeScript's built-in `Partial<T>` which only affects
|
|
6
|
+
* the top level, this utility recursively applies optionality to nested objects
|
|
7
|
+
* and arrays.
|
|
8
|
+
*
|
|
9
|
+
* Used primarily in {@link IJsonParseResult.IFailure} to represent partially
|
|
10
|
+
* recovered data from malformed JSON, where some properties may be missing due
|
|
11
|
+
* to parsing errors.
|
|
12
|
+
*
|
|
13
|
+
* Behavior:
|
|
14
|
+
*
|
|
15
|
+
* - **Primitives** (`string`, `number`, `boolean`, `bigint`, `symbol`, `null`,
|
|
16
|
+
* `undefined`): returned as-is
|
|
17
|
+
* - **Functions**: returned as-is
|
|
18
|
+
* - **Arrays**: element type becomes `DeepPartial<U>`
|
|
19
|
+
* - **Objects**: all properties become optional with `DeepPartial` applied
|
|
20
|
+
*
|
|
21
|
+
* @author Michael - https://github.com/8471919
|
|
22
|
+
* @template T The type to make deeply partial
|
|
23
|
+
*/
|
|
24
|
+
export type DeepPartial<T> = T extends
|
|
25
|
+
| string
|
|
26
|
+
| number
|
|
27
|
+
| boolean
|
|
28
|
+
| bigint
|
|
29
|
+
| symbol
|
|
30
|
+
| null
|
|
31
|
+
| undefined
|
|
32
|
+
? T
|
|
33
|
+
: T extends (...args: unknown[]) => unknown
|
|
34
|
+
? T
|
|
35
|
+
: T extends Array<infer U>
|
|
36
|
+
? Array<DeepPartial<U>>
|
|
37
|
+
: T extends object
|
|
38
|
+
? { [P in keyof T]?: DeepPartial<T[P]> }
|
|
39
|
+
: T;
|
package/src/typings/index.ts
CHANGED