@typia/transform 12.0.0-dev.20260316 → 12.0.1
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/LICENSE +21 -21
- package/README.md +25 -25
- package/lib/features/llm/LlmApplicationTransformer.d.ts +0 -6
- package/lib/features/llm/LlmApplicationTransformer.js +4 -82
- package/lib/features/llm/LlmApplicationTransformer.js.map +1 -1
- package/lib/features/llm/LlmApplicationTransformer.mjs +3 -80
- package/lib/features/llm/LlmApplicationTransformer.mjs.map +1 -1
- package/lib/features/llm/LlmControllerTransformer.js +64 -36
- package/lib/features/llm/LlmControllerTransformer.js.map +1 -1
- package/lib/features/llm/LlmControllerTransformer.mjs +64 -36
- package/lib/features/llm/LlmControllerTransformer.mjs.map +1 -1
- package/package.json +4 -4
- package/src/CallExpressionTransformer.ts +581 -581
- package/src/features/llm/LlmApplicationTransformer.ts +89 -226
- package/src/features/llm/LlmControllerTransformer.ts +84 -65
- package/src/features/llm/LlmStructuredOutputTransformer.ts +86 -86
|
@@ -1,226 +1,89 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from "@typia/
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
): {
|
|
91
|
-
application: ILlmApplication.__IPrimitive;
|
|
92
|
-
type: ts.Type;
|
|
93
|
-
node: ts.TypeNode;
|
|
94
|
-
config:
|
|
95
|
-
| Partial<
|
|
96
|
-
ILlmSchema.IConfig & {
|
|
97
|
-
equals: boolean;
|
|
98
|
-
}
|
|
99
|
-
>
|
|
100
|
-
| undefined;
|
|
101
|
-
} | null => {
|
|
102
|
-
// GET GENERIC ARGUMENT
|
|
103
|
-
if (!props.expression.typeArguments?.length)
|
|
104
|
-
throw new TransformerError({
|
|
105
|
-
code: `typia.llm.${method}`,
|
|
106
|
-
message: "no generic argument.",
|
|
107
|
-
});
|
|
108
|
-
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
109
|
-
if (ts.isTypeNode(top) === false) return null;
|
|
110
|
-
|
|
111
|
-
// GET TYPE
|
|
112
|
-
const config:
|
|
113
|
-
| Partial<
|
|
114
|
-
ILlmSchema.IConfig & {
|
|
115
|
-
equals: boolean;
|
|
116
|
-
}
|
|
117
|
-
>
|
|
118
|
-
| undefined = LlmMetadataFactory.getConfig({
|
|
119
|
-
context: props.context,
|
|
120
|
-
method,
|
|
121
|
-
node: props.expression.typeArguments[1],
|
|
122
|
-
});
|
|
123
|
-
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
124
|
-
|
|
125
|
-
// VALIDATE TYPE
|
|
126
|
-
const analyze = (validate: boolean): MetadataSchema => {
|
|
127
|
-
const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
|
|
128
|
-
MetadataFactory.analyze({
|
|
129
|
-
checker: props.context.checker,
|
|
130
|
-
transformer: props.context.transformer,
|
|
131
|
-
options: {
|
|
132
|
-
absorb: validate,
|
|
133
|
-
escape: true,
|
|
134
|
-
constant: true,
|
|
135
|
-
functional: true,
|
|
136
|
-
validate:
|
|
137
|
-
validate === true
|
|
138
|
-
? (next) =>
|
|
139
|
-
LlmApplicationProgrammer.validate({
|
|
140
|
-
config,
|
|
141
|
-
metadata: next.metadata,
|
|
142
|
-
explore: next.explore,
|
|
143
|
-
top: next.top,
|
|
144
|
-
})
|
|
145
|
-
: undefined,
|
|
146
|
-
},
|
|
147
|
-
components: new MetadataCollection({
|
|
148
|
-
replace: MetadataCollection.replace,
|
|
149
|
-
}),
|
|
150
|
-
type,
|
|
151
|
-
});
|
|
152
|
-
if (result.success === false)
|
|
153
|
-
throw TransformerError.from({
|
|
154
|
-
code: `typia.llm.${method}`,
|
|
155
|
-
errors: result.errors,
|
|
156
|
-
});
|
|
157
|
-
return result.data;
|
|
158
|
-
};
|
|
159
|
-
analyze(true);
|
|
160
|
-
|
|
161
|
-
// GENERATE LLM APPLICATION
|
|
162
|
-
return {
|
|
163
|
-
application: LlmApplicationProgrammer.writeApplication({
|
|
164
|
-
context: props.context,
|
|
165
|
-
modulo: props.modulo,
|
|
166
|
-
metadata: analyze(false),
|
|
167
|
-
config,
|
|
168
|
-
name: top.getFullText().trim(),
|
|
169
|
-
}),
|
|
170
|
-
node: top,
|
|
171
|
-
type,
|
|
172
|
-
config,
|
|
173
|
-
};
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
export const getConfigArgument = (props: {
|
|
177
|
-
context: ITypiaContext;
|
|
178
|
-
argument: ts.Expression;
|
|
179
|
-
equals?: boolean | undefined;
|
|
180
|
-
}) => {
|
|
181
|
-
const satisfiesTypeNode: ts.TypeNode = ts.factory.createTypeReferenceNode(
|
|
182
|
-
ts.factory.createIdentifier("Partial"),
|
|
183
|
-
[
|
|
184
|
-
ts.factory.createTypeReferenceNode(
|
|
185
|
-
ts.factory.createIdentifier("Pick"),
|
|
186
|
-
[
|
|
187
|
-
ts.factory.createImportTypeNode(
|
|
188
|
-
ts.factory.createLiteralTypeNode(
|
|
189
|
-
ts.factory.createStringLiteral("typia"),
|
|
190
|
-
),
|
|
191
|
-
undefined,
|
|
192
|
-
ts.factory.createQualifiedName(
|
|
193
|
-
ts.factory.createIdentifier("ILlmApplication"),
|
|
194
|
-
ts.factory.createIdentifier("IConfig"),
|
|
195
|
-
),
|
|
196
|
-
undefined,
|
|
197
|
-
false,
|
|
198
|
-
),
|
|
199
|
-
ts.factory.createUnionTypeNode([
|
|
200
|
-
ts.factory.createLiteralTypeNode(
|
|
201
|
-
ts.factory.createStringLiteral("validate"),
|
|
202
|
-
),
|
|
203
|
-
]),
|
|
204
|
-
],
|
|
205
|
-
),
|
|
206
|
-
],
|
|
207
|
-
);
|
|
208
|
-
return ts.factory.createObjectLiteralExpression(
|
|
209
|
-
[
|
|
210
|
-
ts.factory.createSpreadAssignment(
|
|
211
|
-
ts.factory.createSatisfiesExpression(
|
|
212
|
-
props.argument,
|
|
213
|
-
satisfiesTypeNode,
|
|
214
|
-
),
|
|
215
|
-
),
|
|
216
|
-
ts.factory.createPropertyAssignment(
|
|
217
|
-
"equals",
|
|
218
|
-
props.equals === true
|
|
219
|
-
? ts.factory.createTrue()
|
|
220
|
-
: ts.factory.createFalse(),
|
|
221
|
-
),
|
|
222
|
-
],
|
|
223
|
-
true,
|
|
224
|
-
);
|
|
225
|
-
};
|
|
226
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
LlmApplicationProgrammer,
|
|
3
|
+
LlmMetadataFactory,
|
|
4
|
+
MetadataCollection,
|
|
5
|
+
MetadataFactory,
|
|
6
|
+
MetadataSchema,
|
|
7
|
+
} from "@typia/core";
|
|
8
|
+
import { ILlmSchema, ValidationPipe } from "@typia/interface";
|
|
9
|
+
import ts from "typescript";
|
|
10
|
+
|
|
11
|
+
import { ITransformProps } from "../../ITransformProps";
|
|
12
|
+
import { TransformerError } from "../../TransformerError";
|
|
13
|
+
|
|
14
|
+
export namespace LlmApplicationTransformer {
|
|
15
|
+
export const transform = (props: ITransformProps): ts.Expression => {
|
|
16
|
+
// GET GENERIC ARGUMENT
|
|
17
|
+
if (!props.expression.typeArguments?.length)
|
|
18
|
+
throw new TransformerError({
|
|
19
|
+
code: "typia.llm.application",
|
|
20
|
+
message: "no generic argument.",
|
|
21
|
+
});
|
|
22
|
+
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
23
|
+
if (ts.isTypeNode(top) === false) return props.expression;
|
|
24
|
+
|
|
25
|
+
// GET CONFIG
|
|
26
|
+
const config:
|
|
27
|
+
| Partial<
|
|
28
|
+
ILlmSchema.IConfig & {
|
|
29
|
+
equals: boolean;
|
|
30
|
+
}
|
|
31
|
+
>
|
|
32
|
+
| undefined = LlmMetadataFactory.getConfig({
|
|
33
|
+
context: props.context,
|
|
34
|
+
method: "application",
|
|
35
|
+
node: props.expression.typeArguments[1],
|
|
36
|
+
});
|
|
37
|
+
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
38
|
+
|
|
39
|
+
// VALIDATE TYPE
|
|
40
|
+
const analyze = (validate: boolean): MetadataSchema => {
|
|
41
|
+
const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
|
|
42
|
+
MetadataFactory.analyze({
|
|
43
|
+
checker: props.context.checker,
|
|
44
|
+
transformer: props.context.transformer,
|
|
45
|
+
options: {
|
|
46
|
+
absorb: validate,
|
|
47
|
+
escape: true,
|
|
48
|
+
constant: true,
|
|
49
|
+
functional: true,
|
|
50
|
+
validate:
|
|
51
|
+
validate === true
|
|
52
|
+
? (next) =>
|
|
53
|
+
LlmApplicationProgrammer.validate({
|
|
54
|
+
config,
|
|
55
|
+
metadata: next.metadata,
|
|
56
|
+
explore: next.explore,
|
|
57
|
+
top: next.top,
|
|
58
|
+
})
|
|
59
|
+
: undefined,
|
|
60
|
+
},
|
|
61
|
+
components: new MetadataCollection({
|
|
62
|
+
replace: MetadataCollection.replace,
|
|
63
|
+
}),
|
|
64
|
+
type,
|
|
65
|
+
});
|
|
66
|
+
if (result.success === false)
|
|
67
|
+
throw TransformerError.from({
|
|
68
|
+
code: "typia.llm.application",
|
|
69
|
+
errors: result.errors,
|
|
70
|
+
});
|
|
71
|
+
return result.data;
|
|
72
|
+
};
|
|
73
|
+
analyze(true);
|
|
74
|
+
|
|
75
|
+
// GENERATE LLM APPLICATION
|
|
76
|
+
return LlmApplicationProgrammer.write({
|
|
77
|
+
context: props.context,
|
|
78
|
+
modulo: props.modulo,
|
|
79
|
+
metadata: analyze(false),
|
|
80
|
+
name: top.getFullText().trim(),
|
|
81
|
+
config,
|
|
82
|
+
configArgument:
|
|
83
|
+
props.expression.arguments?.[0] !== undefined
|
|
84
|
+
? props.expression.arguments[0]
|
|
85
|
+
: undefined,
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
}
|
|
@@ -1,81 +1,100 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
LlmApplicationProgrammer,
|
|
3
|
+
LlmControllerProgrammer,
|
|
4
|
+
LlmMetadataFactory,
|
|
5
|
+
MetadataCollection,
|
|
6
|
+
MetadataFactory,
|
|
7
|
+
MetadataSchema,
|
|
8
|
+
} from "@typia/core";
|
|
9
|
+
import { ILlmSchema, ValidationPipe } from "@typia/interface";
|
|
2
10
|
import ts from "typescript";
|
|
3
11
|
|
|
4
12
|
import { ITransformProps } from "../../ITransformProps";
|
|
5
13
|
import { TransformerError } from "../../TransformerError";
|
|
6
|
-
import { LlmApplicationTransformer } from "./LlmApplicationTransformer";
|
|
7
14
|
|
|
8
15
|
export namespace LlmControllerTransformer {
|
|
9
16
|
export const transform = (props: ITransformProps): ts.Expression => {
|
|
10
|
-
|
|
11
|
-
if (
|
|
12
|
-
else if (props.expression.arguments[0] === undefined)
|
|
17
|
+
// GET GENERIC ARGUMENT
|
|
18
|
+
if (!props.expression.typeArguments?.length)
|
|
13
19
|
throw new TransformerError({
|
|
14
|
-
code:
|
|
15
|
-
message:
|
|
20
|
+
code: "typia.llm.controller",
|
|
21
|
+
message: "no generic argument.",
|
|
16
22
|
});
|
|
17
|
-
|
|
23
|
+
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
24
|
+
if (ts.isTypeNode(top) === false) return props.expression;
|
|
25
|
+
|
|
26
|
+
if (props.expression.arguments[0] === undefined)
|
|
27
|
+
throw new TransformerError({
|
|
28
|
+
code: "typia.llm.controller",
|
|
29
|
+
message: "no identifier name.",
|
|
30
|
+
});
|
|
31
|
+
if (props.expression.arguments[1] === undefined)
|
|
18
32
|
throw new TransformerError({
|
|
19
|
-
code:
|
|
20
|
-
message:
|
|
33
|
+
code: "typia.llm.controller",
|
|
34
|
+
message: "no executor.",
|
|
21
35
|
});
|
|
22
36
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
37
|
+
// GET CONFIG
|
|
38
|
+
const config:
|
|
39
|
+
| Partial<
|
|
40
|
+
ILlmSchema.IConfig & {
|
|
41
|
+
equals: boolean;
|
|
42
|
+
}
|
|
43
|
+
>
|
|
44
|
+
| undefined = LlmMetadataFactory.getConfig({
|
|
45
|
+
context: props.context,
|
|
46
|
+
method: "controller",
|
|
47
|
+
node: props.expression.typeArguments[1],
|
|
27
48
|
});
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
49
|
+
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
50
|
+
|
|
51
|
+
// VALIDATE TYPE
|
|
52
|
+
const analyze = (validate: boolean): MetadataSchema => {
|
|
53
|
+
const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
|
|
54
|
+
MetadataFactory.analyze({
|
|
55
|
+
checker: props.context.checker,
|
|
56
|
+
transformer: props.context.transformer,
|
|
57
|
+
options: {
|
|
58
|
+
absorb: validate,
|
|
59
|
+
escape: true,
|
|
60
|
+
constant: true,
|
|
61
|
+
functional: true,
|
|
62
|
+
validate:
|
|
63
|
+
validate === true
|
|
64
|
+
? (next) =>
|
|
65
|
+
LlmApplicationProgrammer.validate({
|
|
66
|
+
config,
|
|
67
|
+
metadata: next.metadata,
|
|
68
|
+
explore: next.explore,
|
|
69
|
+
top: next.top,
|
|
70
|
+
})
|
|
71
|
+
: undefined,
|
|
72
|
+
},
|
|
73
|
+
components: new MetadataCollection({
|
|
74
|
+
replace: MetadataCollection.replace,
|
|
75
|
+
}),
|
|
76
|
+
type,
|
|
77
|
+
});
|
|
78
|
+
if (result.success === false)
|
|
79
|
+
throw TransformerError.from({
|
|
80
|
+
code: "typia.llm.controller",
|
|
81
|
+
errors: result.errors,
|
|
82
|
+
});
|
|
83
|
+
return result.data;
|
|
84
|
+
};
|
|
85
|
+
analyze(true);
|
|
86
|
+
|
|
87
|
+
// GENERATE LLM CONTROLLER
|
|
88
|
+
return LlmControllerProgrammer.write({
|
|
89
|
+
context: props.context,
|
|
90
|
+
modulo: props.modulo,
|
|
91
|
+
metadata: analyze(false),
|
|
92
|
+
className: top.getFullText().trim(),
|
|
93
|
+
config,
|
|
94
|
+
node: top,
|
|
95
|
+
nameArgument: props.expression.arguments[0],
|
|
96
|
+
executeArgument: props.expression.arguments[1],
|
|
97
|
+
configArgument: props.expression.arguments[2],
|
|
32
98
|
});
|
|
33
|
-
const value: ts.ObjectLiteralExpression =
|
|
34
|
-
ts.factory.createObjectLiteralExpression(
|
|
35
|
-
[
|
|
36
|
-
ts.factory.createPropertyAssignment(
|
|
37
|
-
"protocol",
|
|
38
|
-
ts.factory.createStringLiteral("class"),
|
|
39
|
-
),
|
|
40
|
-
ts.factory.createPropertyAssignment(
|
|
41
|
-
"name",
|
|
42
|
-
props.expression.arguments[0],
|
|
43
|
-
),
|
|
44
|
-
ts.factory.createPropertyAssignment(
|
|
45
|
-
"execute",
|
|
46
|
-
props.expression.arguments[1],
|
|
47
|
-
),
|
|
48
|
-
ts.factory.createPropertyAssignment(
|
|
49
|
-
"application",
|
|
50
|
-
ts.factory.createCallExpression(
|
|
51
|
-
props.context.importer.internal("llmApplicationFinalize"),
|
|
52
|
-
undefined,
|
|
53
|
-
[
|
|
54
|
-
ts.factory.createAsExpression(
|
|
55
|
-
ts.factory.createSatisfiesExpression(
|
|
56
|
-
LiteralFactory.write(dec.application),
|
|
57
|
-
primitiveTypeNode,
|
|
58
|
-
),
|
|
59
|
-
primitiveTypeNode,
|
|
60
|
-
),
|
|
61
|
-
...(props.expression.arguments?.[2] !== undefined
|
|
62
|
-
? [
|
|
63
|
-
LlmApplicationTransformer.getConfigArgument({
|
|
64
|
-
context: props.context,
|
|
65
|
-
argument: props.expression.arguments[2],
|
|
66
|
-
equals: dec.config?.equals,
|
|
67
|
-
}),
|
|
68
|
-
]
|
|
69
|
-
: []),
|
|
70
|
-
],
|
|
71
|
-
),
|
|
72
|
-
),
|
|
73
|
-
],
|
|
74
|
-
true,
|
|
75
|
-
);
|
|
76
|
-
return ts.factory.createAsExpression(
|
|
77
|
-
ts.factory.createSatisfiesExpression(value, typeNode),
|
|
78
|
-
typeNode,
|
|
79
|
-
);
|
|
80
99
|
};
|
|
81
100
|
}
|
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LlmMetadataFactory,
|
|
3
|
-
LlmStructuredOutputProgrammer,
|
|
4
|
-
MetadataCollection,
|
|
5
|
-
MetadataFactory,
|
|
6
|
-
MetadataSchema,
|
|
7
|
-
} from "@typia/core";
|
|
8
|
-
import { ILlmSchema, ValidationPipe } from "@typia/interface";
|
|
9
|
-
import ts from "typescript";
|
|
10
|
-
|
|
11
|
-
import { ITransformProps } from "../../ITransformProps";
|
|
12
|
-
import { TransformerError } from "../../TransformerError";
|
|
13
|
-
|
|
14
|
-
export namespace LlmStructuredOutputTransformer {
|
|
15
|
-
export const transform = (props: ITransformProps): ts.Expression => {
|
|
16
|
-
// GET GENERIC ARGUMENT
|
|
17
|
-
if (!props.expression.typeArguments?.length)
|
|
18
|
-
throw new TransformerError({
|
|
19
|
-
code: "typia.llm.structuredOutput",
|
|
20
|
-
message: "no generic argument.",
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
24
|
-
if (ts.isTypeNode(top) === false) return props.expression;
|
|
25
|
-
|
|
26
|
-
// GET TYPE AND CONFIG
|
|
27
|
-
const config: Partial<ILlmSchema.IConfig & { equals: boolean }> =
|
|
28
|
-
LlmMetadataFactory.getConfig({
|
|
29
|
-
context: props.context,
|
|
30
|
-
method: "structuredOutput",
|
|
31
|
-
node: props.expression.typeArguments[1],
|
|
32
|
-
}) as Partial<ILlmSchema.IConfig & { equals: boolean }>;
|
|
33
|
-
|
|
34
|
-
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
35
|
-
|
|
36
|
-
if (type.isTypeParameter())
|
|
37
|
-
throw new TransformerError({
|
|
38
|
-
code: "typia.llm.structuredOutput",
|
|
39
|
-
message: "non-specified generic argument.",
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// VALIDATE TYPE
|
|
43
|
-
const analyze = (validate: boolean): MetadataSchema => {
|
|
44
|
-
const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
|
|
45
|
-
MetadataFactory.analyze({
|
|
46
|
-
checker: props.context.checker,
|
|
47
|
-
transformer: props.context.transformer,
|
|
48
|
-
options: {
|
|
49
|
-
absorb: validate,
|
|
50
|
-
escape: true,
|
|
51
|
-
constant: true,
|
|
52
|
-
validate:
|
|
53
|
-
validate === true
|
|
54
|
-
? (next) =>
|
|
55
|
-
LlmStructuredOutputProgrammer.validate({
|
|
56
|
-
config,
|
|
57
|
-
metadata: next.metadata,
|
|
58
|
-
explore: next.explore,
|
|
59
|
-
})
|
|
60
|
-
: undefined,
|
|
61
|
-
},
|
|
62
|
-
components: new MetadataCollection({
|
|
63
|
-
replace: MetadataCollection.replace,
|
|
64
|
-
}),
|
|
65
|
-
type,
|
|
66
|
-
});
|
|
67
|
-
if (result.success === false)
|
|
68
|
-
throw TransformerError.from({
|
|
69
|
-
code: "typia.llm.structuredOutput",
|
|
70
|
-
errors: result.errors,
|
|
71
|
-
});
|
|
72
|
-
return result.data;
|
|
73
|
-
};
|
|
74
|
-
analyze(true);
|
|
75
|
-
|
|
76
|
-
// GENERATE CODE
|
|
77
|
-
return LlmStructuredOutputProgrammer.write({
|
|
78
|
-
context: props.context,
|
|
79
|
-
modulo: props.modulo,
|
|
80
|
-
type,
|
|
81
|
-
metadata: analyze(false),
|
|
82
|
-
config,
|
|
83
|
-
name: (top as ts.TypeNode).getFullText().trim(),
|
|
84
|
-
});
|
|
85
|
-
};
|
|
86
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
LlmMetadataFactory,
|
|
3
|
+
LlmStructuredOutputProgrammer,
|
|
4
|
+
MetadataCollection,
|
|
5
|
+
MetadataFactory,
|
|
6
|
+
MetadataSchema,
|
|
7
|
+
} from "@typia/core";
|
|
8
|
+
import { ILlmSchema, ValidationPipe } from "@typia/interface";
|
|
9
|
+
import ts from "typescript";
|
|
10
|
+
|
|
11
|
+
import { ITransformProps } from "../../ITransformProps";
|
|
12
|
+
import { TransformerError } from "../../TransformerError";
|
|
13
|
+
|
|
14
|
+
export namespace LlmStructuredOutputTransformer {
|
|
15
|
+
export const transform = (props: ITransformProps): ts.Expression => {
|
|
16
|
+
// GET GENERIC ARGUMENT
|
|
17
|
+
if (!props.expression.typeArguments?.length)
|
|
18
|
+
throw new TransformerError({
|
|
19
|
+
code: "typia.llm.structuredOutput",
|
|
20
|
+
message: "no generic argument.",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
24
|
+
if (ts.isTypeNode(top) === false) return props.expression;
|
|
25
|
+
|
|
26
|
+
// GET TYPE AND CONFIG
|
|
27
|
+
const config: Partial<ILlmSchema.IConfig & { equals: boolean }> =
|
|
28
|
+
LlmMetadataFactory.getConfig({
|
|
29
|
+
context: props.context,
|
|
30
|
+
method: "structuredOutput",
|
|
31
|
+
node: props.expression.typeArguments[1],
|
|
32
|
+
}) as Partial<ILlmSchema.IConfig & { equals: boolean }>;
|
|
33
|
+
|
|
34
|
+
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
35
|
+
|
|
36
|
+
if (type.isTypeParameter())
|
|
37
|
+
throw new TransformerError({
|
|
38
|
+
code: "typia.llm.structuredOutput",
|
|
39
|
+
message: "non-specified generic argument.",
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// VALIDATE TYPE
|
|
43
|
+
const analyze = (validate: boolean): MetadataSchema => {
|
|
44
|
+
const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
|
|
45
|
+
MetadataFactory.analyze({
|
|
46
|
+
checker: props.context.checker,
|
|
47
|
+
transformer: props.context.transformer,
|
|
48
|
+
options: {
|
|
49
|
+
absorb: validate,
|
|
50
|
+
escape: true,
|
|
51
|
+
constant: true,
|
|
52
|
+
validate:
|
|
53
|
+
validate === true
|
|
54
|
+
? (next) =>
|
|
55
|
+
LlmStructuredOutputProgrammer.validate({
|
|
56
|
+
config,
|
|
57
|
+
metadata: next.metadata,
|
|
58
|
+
explore: next.explore,
|
|
59
|
+
})
|
|
60
|
+
: undefined,
|
|
61
|
+
},
|
|
62
|
+
components: new MetadataCollection({
|
|
63
|
+
replace: MetadataCollection.replace,
|
|
64
|
+
}),
|
|
65
|
+
type,
|
|
66
|
+
});
|
|
67
|
+
if (result.success === false)
|
|
68
|
+
throw TransformerError.from({
|
|
69
|
+
code: "typia.llm.structuredOutput",
|
|
70
|
+
errors: result.errors,
|
|
71
|
+
});
|
|
72
|
+
return result.data;
|
|
73
|
+
};
|
|
74
|
+
analyze(true);
|
|
75
|
+
|
|
76
|
+
// GENERATE CODE
|
|
77
|
+
return LlmStructuredOutputProgrammer.write({
|
|
78
|
+
context: props.context,
|
|
79
|
+
modulo: props.modulo,
|
|
80
|
+
type,
|
|
81
|
+
metadata: analyze(false),
|
|
82
|
+
config,
|
|
83
|
+
name: (top as ts.TypeNode).getFullText().trim(),
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
}
|