UNPKG

typia

Version:

Superfast runtime validators with only one line

102 lines (99 loc) 4.25 kB
import ts from 'typescript'; import { ExpressionFactory } from '../../../factories/ExpressionFactory.mjs'; import { LiteralFactory } from '../../../factories/LiteralFactory.mjs'; import { MetadataCollection } from '../../../factories/MetadataCollection.mjs'; import { MetadataFactory } from '../../../factories/MetadataFactory.mjs'; import { StatementFactory } from '../../../factories/StatementFactory.mjs'; import { LlmApplicationProgrammer } from '../../../programmers/llm/LlmApplicationProgrammer.mjs'; import { LlmModelPredicator } from '../../../programmers/llm/LlmModelPredicator.mjs'; import { TransformerError } from '../../TransformerError.mjs'; var LlmApplicationTransformer; (function (LlmApplicationTransformer) { LlmApplicationTransformer.transform = (props) => { // GET GENERIC ARGUMENT if (!props.expression.typeArguments?.length) throw new TransformerError({ code: "typia.llm.application", message: "no generic argument.", }); const top = props.expression.typeArguments[0]; if (ts.isTypeNode(top) === false) return props.expression; // GET TYPE const model = LlmModelPredicator.getModel({ checker: props.context.checker, method: "application", node: props.expression.typeArguments[1], }); const config = LlmModelPredicator.getConfig({ context: props.context, method: "application", model, node: props.expression.typeArguments[2], }); const type = props.context.checker.getTypeFromTypeNode(top); // VALIDATE TYPE const analyze = (validate) => { const result = MetadataFactory.analyze({ checker: props.context.checker, transformer: props.context.transformer, options: { absorb: validate, escape: true, constant: true, functional: true, validate: validate === true ? LlmApplicationProgrammer.validate({ model, config, }) : undefined, }, collection: new MetadataCollection({ replace: MetadataCollection.replace, }), type, }); if (result.success === false) throw TransformerError.from({ code: "typia.llm.application", errors: result.errors, }); return result.data; }; analyze(true); // GENERATE LLM APPLICATION const schema = LlmApplicationProgrammer.write({ model, context: props.context, modulo: props.modulo, metadata: analyze(false), config, name: top.getFullText().trim(), }); const literal = ts.factory.createAsExpression(LiteralFactory.write(schema), props.context.importer.type({ file: "@samchon/openapi", name: "ILlmApplication", arguments: [ ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(model)), ], })); if (!props.expression.arguments?.[0]) return literal; return ExpressionFactory.selfCall(ts.factory.createBlock([ StatementFactory.constant({ name: "app", value: literal, }), ts.factory.createExpressionStatement(ts.factory.createCallExpression(props.context.importer.internal("llmApplicationFinalize"), undefined, [ ts.factory.createIdentifier("app"), ...(props.expression.arguments?.[0] ? [props.expression.arguments[0]] : []), ])), ts.factory.createReturnStatement(ts.factory.createIdentifier("app")), ], true)); }; })(LlmApplicationTransformer || (LlmApplicationTransformer = {})); export { LlmApplicationTransformer }; //# sourceMappingURL=LlmApplicationTransformer.mjs.map