@typia/transform 12.0.0-dev.20260316 → 12.0.0
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/package.json +4 -4
- package/src/CallExpressionTransformer.ts +581 -581
- package/src/features/llm/LlmApplicationTransformer.ts +226 -226
- package/src/features/llm/LlmStructuredOutputTransformer.ts +86 -86
|
@@ -1,226 +1,226 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ITypiaContext,
|
|
3
|
-
LlmApplicationProgrammer,
|
|
4
|
-
LlmMetadataFactory,
|
|
5
|
-
MetadataCollection,
|
|
6
|
-
MetadataFactory,
|
|
7
|
-
MetadataSchema,
|
|
8
|
-
} from "@typia/core";
|
|
9
|
-
import { ILlmApplication, ILlmSchema, ValidationPipe } from "@typia/interface";
|
|
10
|
-
import ts from "typescript";
|
|
11
|
-
|
|
12
|
-
import { ITransformProps } from "../../ITransformProps";
|
|
13
|
-
import { TransformerError } from "../../TransformerError";
|
|
14
|
-
|
|
15
|
-
export namespace LlmApplicationTransformer {
|
|
16
|
-
export const transform = (props: ITransformProps): ts.Expression => {
|
|
17
|
-
// GET GENERIC ARGUMENT
|
|
18
|
-
if (!props.expression.typeArguments?.length)
|
|
19
|
-
throw new TransformerError({
|
|
20
|
-
code: "typia.llm.application",
|
|
21
|
-
message: "no generic argument.",
|
|
22
|
-
});
|
|
23
|
-
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
24
|
-
if (ts.isTypeNode(top) === false) return props.expression;
|
|
25
|
-
|
|
26
|
-
// GET CONFIG
|
|
27
|
-
const config:
|
|
28
|
-
| Partial<
|
|
29
|
-
ILlmSchema.IConfig & {
|
|
30
|
-
equals: boolean;
|
|
31
|
-
}
|
|
32
|
-
>
|
|
33
|
-
| undefined = LlmMetadataFactory.getConfig({
|
|
34
|
-
context: props.context,
|
|
35
|
-
method: "application",
|
|
36
|
-
node: props.expression.typeArguments[1],
|
|
37
|
-
});
|
|
38
|
-
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
39
|
-
|
|
40
|
-
// VALIDATE TYPE
|
|
41
|
-
const analyze = (validate: boolean): MetadataSchema => {
|
|
42
|
-
const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
|
|
43
|
-
MetadataFactory.analyze({
|
|
44
|
-
checker: props.context.checker,
|
|
45
|
-
transformer: props.context.transformer,
|
|
46
|
-
options: {
|
|
47
|
-
absorb: validate,
|
|
48
|
-
escape: true,
|
|
49
|
-
constant: true,
|
|
50
|
-
functional: true,
|
|
51
|
-
validate:
|
|
52
|
-
validate === true
|
|
53
|
-
? (next) =>
|
|
54
|
-
LlmApplicationProgrammer.validate({
|
|
55
|
-
config,
|
|
56
|
-
metadata: next.metadata,
|
|
57
|
-
explore: next.explore,
|
|
58
|
-
top: next.top,
|
|
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.application",
|
|
70
|
-
errors: result.errors,
|
|
71
|
-
});
|
|
72
|
-
return result.data;
|
|
73
|
-
};
|
|
74
|
-
analyze(true);
|
|
75
|
-
|
|
76
|
-
// GENERATE LLM APPLICATION
|
|
77
|
-
return LlmApplicationProgrammer.write({
|
|
78
|
-
context: props.context,
|
|
79
|
-
modulo: props.modulo,
|
|
80
|
-
metadata: analyze(false),
|
|
81
|
-
name: top.getFullText().trim(),
|
|
82
|
-
config,
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/** @internal */
|
|
87
|
-
export const decompose = (
|
|
88
|
-
method: string,
|
|
89
|
-
props: ITransformProps,
|
|
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
|
+
ITypiaContext,
|
|
3
|
+
LlmApplicationProgrammer,
|
|
4
|
+
LlmMetadataFactory,
|
|
5
|
+
MetadataCollection,
|
|
6
|
+
MetadataFactory,
|
|
7
|
+
MetadataSchema,
|
|
8
|
+
} from "@typia/core";
|
|
9
|
+
import { ILlmApplication, ILlmSchema, ValidationPipe } from "@typia/interface";
|
|
10
|
+
import ts from "typescript";
|
|
11
|
+
|
|
12
|
+
import { ITransformProps } from "../../ITransformProps";
|
|
13
|
+
import { TransformerError } from "../../TransformerError";
|
|
14
|
+
|
|
15
|
+
export namespace LlmApplicationTransformer {
|
|
16
|
+
export const transform = (props: ITransformProps): ts.Expression => {
|
|
17
|
+
// GET GENERIC ARGUMENT
|
|
18
|
+
if (!props.expression.typeArguments?.length)
|
|
19
|
+
throw new TransformerError({
|
|
20
|
+
code: "typia.llm.application",
|
|
21
|
+
message: "no generic argument.",
|
|
22
|
+
});
|
|
23
|
+
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
24
|
+
if (ts.isTypeNode(top) === false) return props.expression;
|
|
25
|
+
|
|
26
|
+
// GET CONFIG
|
|
27
|
+
const config:
|
|
28
|
+
| Partial<
|
|
29
|
+
ILlmSchema.IConfig & {
|
|
30
|
+
equals: boolean;
|
|
31
|
+
}
|
|
32
|
+
>
|
|
33
|
+
| undefined = LlmMetadataFactory.getConfig({
|
|
34
|
+
context: props.context,
|
|
35
|
+
method: "application",
|
|
36
|
+
node: props.expression.typeArguments[1],
|
|
37
|
+
});
|
|
38
|
+
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
39
|
+
|
|
40
|
+
// VALIDATE TYPE
|
|
41
|
+
const analyze = (validate: boolean): MetadataSchema => {
|
|
42
|
+
const result: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
|
|
43
|
+
MetadataFactory.analyze({
|
|
44
|
+
checker: props.context.checker,
|
|
45
|
+
transformer: props.context.transformer,
|
|
46
|
+
options: {
|
|
47
|
+
absorb: validate,
|
|
48
|
+
escape: true,
|
|
49
|
+
constant: true,
|
|
50
|
+
functional: true,
|
|
51
|
+
validate:
|
|
52
|
+
validate === true
|
|
53
|
+
? (next) =>
|
|
54
|
+
LlmApplicationProgrammer.validate({
|
|
55
|
+
config,
|
|
56
|
+
metadata: next.metadata,
|
|
57
|
+
explore: next.explore,
|
|
58
|
+
top: next.top,
|
|
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.application",
|
|
70
|
+
errors: result.errors,
|
|
71
|
+
});
|
|
72
|
+
return result.data;
|
|
73
|
+
};
|
|
74
|
+
analyze(true);
|
|
75
|
+
|
|
76
|
+
// GENERATE LLM APPLICATION
|
|
77
|
+
return LlmApplicationProgrammer.write({
|
|
78
|
+
context: props.context,
|
|
79
|
+
modulo: props.modulo,
|
|
80
|
+
metadata: analyze(false),
|
|
81
|
+
name: top.getFullText().trim(),
|
|
82
|
+
config,
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** @internal */
|
|
87
|
+
export const decompose = (
|
|
88
|
+
method: string,
|
|
89
|
+
props: ITransformProps,
|
|
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,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
|
+
}
|