Skip to content

Commit

Permalink
Merge pull request #1259 from samchon/feature/llm-application
Browse files Browse the repository at this point in the history
`typia.llm.application<T>()` function.
  • Loading branch information
samchon committed Sep 7, 2024
2 parents a9d22b5 + a98fe94 commit 0d65c87
Show file tree
Hide file tree
Showing 203 changed files with 5,700 additions and 3,217 deletions.
2 changes: 1 addition & 1 deletion debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"dependencies": {
"tstl": "^3.0.0",
"typia": "../typia-6.10.0-dev.20240823.tgz",
"typia": "../typia-6.10.0-dev.20240907.tgz",
"uuid": "^10.0.0"
}
}
26 changes: 0 additions & 26 deletions debug/src/llm-schema.ts

This file was deleted.

7 changes: 7 additions & 0 deletions debug/src/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "typia";

interface Something<T> {
setValue: (x: T) => T;
}

typia.llm.application<Something<number>>();
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "6.10.0-dev.20240906",
"version": "6.10.0-dev.20240907",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -83,7 +83,7 @@
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/inquirer": "^8.2.5",
"@types/node": "^18.15.12",
"@types/ts-expose-internals": "npm:[email protected].3",
"@types/ts-expose-internals": "npm:[email protected].4",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"chalk": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/factories/MetadataFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export namespace MetadataFactory {
escape: boolean;
constant: boolean;
absorb: boolean;
functional?: boolean;
validate?: Validator;
onError?: (node: ts.Node | undefined, message: string) => void;
}
Expand Down
2 changes: 1 addition & 1 deletion src/factories/MetadataTypeTagSchemaFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace MetadataTypeTagSchemaFactory {
meta.atomics.length ||
meta.arrays.length ||
meta.natives.length ||
meta.functional
meta.functions.length
)
report(`${parent.object.name}.${parent.key} has non-literal type`);
else if (meta.size() > 1)
Expand Down
2 changes: 1 addition & 1 deletion src/factories/ProtobufFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export namespace ProtobufFactory {
// PROHIBIT ANY TYPE
if (meta.any) noSupport("any type");
// PROHIBIT FUNCTIONAL TYPE
if (meta.functional) noSupport("functional type");
if (meta.functions.length) noSupport("functional type");
// PROHIBIT TUPLE TYPE
if (meta.tuples.length) noSupport("tuple type");
// PROHIBIT SET TYPE
Expand Down
2 changes: 1 addition & 1 deletion src/factories/TypeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export namespace TypeFactory {
export const isFunction = (type: ts.Type): boolean =>
getFunction(type) !== null;

const getFunction = (type: ts.Type) => {
export const getFunction = (type: ts.Type) => {
const node = type.symbol?.declarations?.[0];
if (node === undefined) return null;

Expand Down
16 changes: 10 additions & 6 deletions src/factories/internal/metadata/emplace_metadata_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const emplace_metadata_object =

// PREPARE ASSETS
const isClass: boolean = parent.isClass();
const isProperty = significant(!!options.functional);
const pred: (node: ts.Declaration) => boolean = isClass
? (node) => {
const kind: ts.SyntaxKind | undefined = node
Expand Down Expand Up @@ -162,12 +163,15 @@ export const emplace_metadata_object =
return obj;
};

const isProperty = (node: ts.Declaration) =>
ts.isParameter(node) ||
ts.isPropertyDeclaration(node) ||
ts.isPropertyAssignment(node) ||
ts.isPropertySignature(node) ||
ts.isTypeLiteralNode(node);
const significant = (functional: boolean) =>
functional
? (node: ts.Declaration) => !ts.isAccessor(node)
: (node: ts.Declaration) =>
ts.isParameter(node) ||
ts.isPropertyDeclaration(node) ||
ts.isPropertyAssignment(node) ||
ts.isPropertySignature(node) ||
ts.isTypeLiteralNode(node);

const iterate_optional_coalesce = (meta: Metadata, type: ts.Type): void => {
if (type.isUnionOrIntersection())
Expand Down
6 changes: 6 additions & 0 deletions src/factories/internal/metadata/iterate_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { iterate_metadata_atomic } from "./iterate_metadata_atomic";
import { iterate_metadata_coalesce } from "./iterate_metadata_coalesce";
import { iterate_metadata_constant } from "./iterate_metadata_constant";
import { iterate_metadata_escape } from "./iterate_metadata_escape";
import { iterate_metadata_function } from "./iterate_metadata_function";
import { iterate_metadata_intersection } from "./iterate_metadata_intersection";
import { iterate_metadata_map } from "./iterate_metadata_map";
import { iterate_metadata_native } from "./iterate_metadata_native";
Expand Down Expand Up @@ -62,6 +63,11 @@ export const iterate_metadata =

// ITERATE CASES
iterate_metadata_coalesce(meta, type) ||
iterate_metadata_function(checker)(options)(collection)(errors)(
meta,
type,
explore,
) ||
iterate_metadata_constant(checker)(options)(meta, type) ||
iterate_metadata_template(checker)(options)(collection)(errors)(
meta,
Expand Down
5 changes: 0 additions & 5 deletions src/factories/internal/metadata/iterate_metadata_coalesce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { Metadata } from "../../../schemas/metadata/Metadata";

import { Writable } from "../../../typings/Writable";

import { TypeFactory } from "../../TypeFactory";

export const iterate_metadata_coalesce = (
meta: Metadata,
type: ts.Type,
Expand All @@ -25,9 +23,6 @@ export const iterate_metadata_coalesce = (
) {
Writable(meta).required = false;
return true;
} else if (TypeFactory.isFunction(type) === true) {
Writable(meta).functional = true;
return true;
}
return false;
};
85 changes: 85 additions & 0 deletions src/factories/internal/metadata/iterate_metadata_function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import ts from "typescript";

import { FunctionalGeneralProgrammer } from "../../../programmers/functional/internal/FunctionalGeneralProgrammer";

import { Metadata } from "../../../schemas/metadata/Metadata";
import { MetadataFunction } from "../../../schemas/metadata/MetadataFunction";
import { MetadataParameter } from "../../../schemas/metadata/MetadataParameter";

import { CommentFactory } from "../../CommentFactory";
import { MetadataCollection } from "../../MetadataCollection";
import { MetadataFactory } from "../../MetadataFactory";
import { TypeFactory } from "../../TypeFactory";
import { explore_metadata } from "./explore_metadata";

export const iterate_metadata_function =
(checker: ts.TypeChecker) =>
(options: MetadataFactory.IOptions) =>
(collection: MetadataCollection) =>
(errors: MetadataFactory.IError[]) =>
(
metadata: Metadata,
type: ts.Type,
explore: MetadataFactory.IExplore,
): boolean => {
const declaration: ts.SignatureDeclaration | null =
TypeFactory.getFunction(type);
if (declaration === null) return false;
else if (!options.functional) {
if (metadata.functions.length === 0)
metadata.functions.push(
MetadataFunction.create({
parameters: [],
output: Metadata.initialize(),
async: false,
}),
);
} else {
const [signature] = type.getCallSignatures();
if (signature === undefined || signature.declaration === undefined)
metadata.functions.push(
MetadataFunction.create({
parameters: [],
output: Metadata.initialize(),
async: false,
}),
);
else {
const { async }: FunctionalGeneralProgrammer.IOutput =
FunctionalGeneralProgrammer.getReturnType(checker)(declaration);
metadata.functions.push(
MetadataFunction.create({
parameters: signature.parameters.map((p) =>
MetadataParameter.create({
name: p.name,
type: explore_metadata(checker)(options)(collection)(errors)(
checker.getTypeOfSymbol(p),
{
...explore,
top: false,
},
),
description: CommentFactory.description(p) ?? null,
jsDocTags: p?.getJsDocTags() ?? [],
}),
),
async,
output: explore_metadata(checker)({
...options,
functional: false,
})(collection)(errors)(
async
? (signature.getReturnType().aliasTypeArguments?.[0] ??
checker.getTypeFromTypeNode(TypeFactory.keyword("any")))
: signature.getReturnType(),
{
...explore,
top: false,
},
),
}),
);
}
}
return true;
};
12 changes: 11 additions & 1 deletion src/llm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { ILlmSchema } from "@samchon/openapi";
import { ILlmApplication, ILlmSchema } from "@samchon/openapi";

export function application(): never;
export function application<T extends object>(): ILlmApplication;

/**
* @internal
*/
export function application(): never {
halt("application");
}

export function schema(): never;
export function schema<T>(): ILlmSchema;
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/CheckerProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export namespace CheckerProgrammer {
);

// FUNCTIONAL
if (meta.functional === true)
if (meta.functions.length)
if (OptionPredicator.functional(project.options) || meta.size() !== 1)
add(
true,
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/RandomProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export namespace RandomProgrammer {
expressions.push(ts.factory.createStringLiteral("any type used..."));

// NULL COALESCING
if (meta.isRequired() === false || meta.functional === true)
if (meta.isRequired() === false || meta.functions.length)
expressions.push(ts.factory.createIdentifier("undefined"));
if (meta.nullable === true) expressions.push(ts.factory.createNull());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export namespace FunctionalGeneralProgrammer {
}
export const getReturnType =
(checker: ts.TypeChecker) =>
(declaration: ts.FunctionDeclaration): IOutput => {
(
declaration: ts.FunctionDeclaration | ts.SignatureDeclaration,
): IOutput => {
const signature: ts.Signature | undefined =
checker.getSignatureFromDeclaration(declaration);
const type: ts.Type =
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/internal/application_v30_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const create_object_schema =
for (const property of obj.properties) {
if (
// FUNCTIONAL TYPE
property.value.functional === true &&
property.value.functions.length &&
property.value.nullable === false &&
property.value.isRequired() === true &&
property.value.size() === 0
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/internal/application_v31_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const create_object_schema =
for (const property of obj.properties) {
if (
// FUNCTIONAL TYPE
property.value.functional === true &&
property.value.functions.length &&
property.value.nullable === false &&
property.value.isRequired() === true &&
property.value.size() === 0
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/internal/llm_schema_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const llm_schema_object = (props: {
for (const property of props.object.properties) {
if (
// FUNCTIONAL TYPE
property.value.functional === true &&
property.value.functions.length &&
property.value.nullable === false &&
property.value.isRequired() === true &&
property.value.size() === 0
Expand Down
8 changes: 4 additions & 4 deletions src/programmers/internal/stringify_regular_properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export const stringify_regular_properties = (
(entry.meta.isRequired() === false &&
entry.meta.nullable === false &&
entry.meta.size() === 0) ||
(entry.meta.functional &&
(!!entry.meta.functions.length &&
entry.meta.nullable === false &&
entry.meta.size() === 1);

if (empty === true) return;
else if (
entry.meta.isRequired() === false ||
entry.meta.functional === true ||
entry.meta.functions.length ||
entry.meta.any === true
)
output.push(
Expand All @@ -52,7 +52,7 @@ export const stringify_regular_properties = (
entry.input,
),
);
if (entry.meta.functional || entry.meta.any)
if (entry.meta.functions.length || entry.meta.any)
conditions.push(
ts.factory.createStrictEquality(
ts.factory.createStringLiteral("function"),
Expand All @@ -78,4 +78,4 @@ export const stringify_regular_properties = (
* @internal
*/
const sequence = (meta: Metadata): number =>
meta.any || !meta.isRequired() || meta.functional ? 0 : 1;
meta.any || !meta.isRequired() || meta.functions.length ? 0 : 1;
4 changes: 2 additions & 2 deletions src/programmers/json/JsonStringifyProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export namespace JsonStringifyProgrammer {
explore,
),
});
else if (meta.functional === true)
else if (meta.functions.length)
unions.push({
type: "functional",
is: () => IsProgrammer.decode_functional(input),
Expand Down Expand Up @@ -797,7 +797,7 @@ export namespace JsonStringifyProgrammer {
meta: Metadata,
explore: FeatureProgrammer.IExplore,
): ((expression: ts.Expression) => ts.Expression) => {
if (meta.functional === false) return (expression) => expression;
if (meta.functions.length === 0) return (expression) => expression;
return (expression) =>
ts.factory.createConditionalExpression(
ts.factory.createStrictInequality(
Expand Down
Loading

0 comments on commit 0d65c87

Please sign in to comment.