Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update @samchon/openapi, the Try<T, E> composer. #1400

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "7.0.0-dev.20241201",
"version": "7.0.0-dev.20241202",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -41,15 +41,16 @@
},
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "^2.0.0-dev.20241201-2",
"@samchon/openapi": "^2.0.0-dev.20241202-2",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5",
"package-manager-detector": "^0.2.0",
"randexp": "^0.5.3"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.7.0"
"typescript": ">=4.8.0 <5.7.0",
"@samchon/openapi": ">=2.0.0-dev.20241202-2 < 3.0.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^26.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export function random<T>(g?: Partial<IRandomGenerator>): T;
Typia is a transformer library supporting below features:

- Super-fast Runtime Validators
- Enhanced JSON functions
- LLM function calling application schema
- Enhanced JSON schema and serde functions
- LLM function calling schema and structured output
- Protocol Buffer encoder and decoder
- Random data generator

Expand Down
7 changes: 4 additions & 3 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "7.0.0-dev.20241130",
"version": "7.0.0-dev.20241202",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -37,10 +37,11 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "7.0.0-dev.20241130"
"typia": "7.0.0-dev.20241202"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.7.0"
"typescript": ">=4.8.0 <5.7.0",
"@samchon/openapi": ">=2.0.0-dev.20241202-2 < 3.0.0"
},
"stackblitz": {
"startCommand": "npm install && npm run test"
Expand Down
1 change: 0 additions & 1 deletion src/IValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export namespace IValidation {
export interface ISuccess<T = unknown> {
success: true;
data: T;
errors: [];
}

export interface IFailure {
Expand Down
47 changes: 35 additions & 12 deletions src/programmers/llm/LlmApplicationProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ILlmApplication, ILlmSchema, OpenApi } from "@samchon/openapi";
import {
ILlmApplication,
ILlmSchema,
IOpenApiSchemaError,
IResult,
OpenApi,
} from "@samchon/openapi";
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer";
import { ILlmFunction } from "@samchon/openapi/lib/structures/ILlmFunction";

Expand Down Expand Up @@ -221,15 +227,25 @@ export namespace LlmApplicationProgrammer {
}): ILlmSchema.ModelParameters[Model] | null => {
const schema = props.function.parameters[0]?.schema;
if (!schema) return null;
return LlmSchemaComposer.parameters(props.model)({

const result: IResult<
ILlmSchema.ModelParameters[Model],
IOpenApiSchemaError
> = LlmSchemaComposer.parameters(props.model)({
config: LlmSchemaComposer.defaultConfig(props.model) as any,
components: props.components,
schema: schema as
| OpenApi.IJsonSchema.IObject
| OpenApi.IJsonSchema.IReference,
errors: props.errors,
accessor: props.accessor,
}) as ILlmSchema.ModelParameters[Model] | null;
}) as IResult<ILlmSchema.ModelParameters[Model], IOpenApiSchemaError>;
if (result.success === false) {
props.errors.push(
...result.error.reasons.map((r) => ` - ${r.accessor}: ${r.message}`),
);
return null;
}
return result.value;
};

const writeOutput = <Model extends ILlmSchema.Model>(props: {
Expand All @@ -241,13 +257,20 @@ export namespace LlmApplicationProgrammer {
accessor: string;
}): ILlmSchema.ModelSchema[Model] | null | undefined => {
if (props.schema === null) return undefined;
return LlmSchemaComposer.schema(props.model)({
config: LlmSchemaComposer.defaultConfig(props.model) as any,
components: props.components,
schema: props.schema,
$defs: (props.parameters as any).$defs,
errors: props.errors,
accessor: props.accessor,
}) as ILlmSchema.ModelSchema[Model] | null;
const result: IResult<ILlmSchema.ModelSchema[Model], IOpenApiSchemaError> =
LlmSchemaComposer.schema(props.model)({
config: LlmSchemaComposer.defaultConfig(props.model) as any,
components: props.components,
schema: props.schema,
$defs: (props.parameters as any).$defs,
accessor: props.accessor,
}) as IResult<ILlmSchema.ModelSchema[Model], IOpenApiSchemaError>;
if (result.success === false) {
props.errors.push(
...result.error.reasons.map((r) => ` - ${r.accessor}: ${r.message}`),
);
return null;
}
return result.value;
};
}
38 changes: 23 additions & 15 deletions src/programmers/llm/LlmParametersProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ILlmSchema, OpenApi, OpenApiTypeChecker } from "@samchon/openapi";
import {
ILlmSchema,
IOpenApiSchemaError,
IResult,
OpenApi,
OpenApiTypeChecker,
} from "@samchon/openapi";
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer";

import { MetadataFactory } from "../../factories/MetadataFactory";
Expand Down Expand Up @@ -33,25 +39,27 @@ export namespace LlmParametersProgrammer {
throw new Error("Unreachable code. Failed to find the object schema.");
})();

const errors: string[] = [];
const parameters: ILlmSchema.ModelParameters[Model] | null =
LlmSchemaComposer.parameters(props.model)({
config: {
...LlmSchemaComposer.defaultConfig(props.model),
...props.config,
} as any,
components: collection.components,
schema,
errors,
}) as ILlmSchema.ModelParameters[Model] | null;
if (parameters === null)
const result: IResult<
ILlmSchema.ModelParameters[Model],
IOpenApiSchemaError
> = LlmSchemaComposer.parameters(props.model)({
config: {
...LlmSchemaComposer.defaultConfig(props.model),
...props.config,
} as any,
components: collection.components,
schema,
}) as IResult<ILlmSchema.ModelParameters[Model], IOpenApiSchemaError>;
if (result.success === false)
throw new TransformerError({
code: "typia.llm.parameters",
message:
"failed to convert JSON schema to LLM schema.\n\n" +
errors.map((str) => ` - ${str}`).join("\n"),
result.error.reasons
.map((r) => ` - ${r.accessor}: ${r.message}`)
.join("\n"),
});
return parameters;
return result.value;
};

export const validate =
Expand Down
16 changes: 8 additions & 8 deletions src/programmers/llm/LlmSchemaProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ILlmSchema } from "@samchon/openapi";
import { ILlmSchema, IOpenApiSchemaError, IResult } from "@samchon/openapi";
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer";

import { IJsonSchemaCollection } from "../../schemas/json/IJsonSchemaCollection";
Expand Down Expand Up @@ -33,8 +33,7 @@ export namespace LlmSchemaProgrammer {
});

const $defs: Record<string, ILlmSchema.ModelSchema[Model]> = {};
const errors: string[] = [];
const schema: ILlmSchema.ModelSchema[Model] | null =
const result: IResult<ILlmSchema.ModelSchema[Model], IOpenApiSchemaError> =
LlmSchemaComposer.schema(props.model)({
config: {
...LlmSchemaComposer.defaultConfig(props.model),
Expand All @@ -43,19 +42,20 @@ export namespace LlmSchemaProgrammer {
components: collection.components,
schema: collection.schemas[0]!,
$defs: $defs as any,
errors,
}) as ILlmSchema.ModelSchema[Model] | null;
if (schema === null)
}) as IResult<ILlmSchema.ModelSchema[Model], IOpenApiSchemaError>;
if (result.success === false)
throw new TransformerError({
code: "typia.llm.schema",
message:
"failed to convert JSON schema to LLM schema.\n\n" +
errors.map((str) => ` - ${str}`).join("\n"),
result.error.reasons
.map((r) => ` - ${r.accessor}: ${r.message}`)
.join("\n"),
});
return {
model: props.model,
$defs,
schema,
schema: result.value,
};
};

Expand Down
7 changes: 4 additions & 3 deletions test/src/internal/_test_functional_validateEqualsFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export const _test_functional_validateEqualsFunction =
if (expected.length === 0) return;

const [x, y] = callback(input);
const actual: string[] = validate(() => y)(x)
.errors.map((err) => err.path)
.sort();
const result: IValidation<T> = validate(() => y)(x);
const actual: string[] = result.success
? []
: result.errors.map((err) => err.path).sort();
if (
expected.length !== actual.length ||
expected.every((str, i) => str === actual[i]) === false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export const _test_functional_validateEqualsFunctionAsync =
if (expected.length === 0) return;

const [x, y] = callback(input);
const actual: string[] = (await validate(async () => y)(x)).errors
.map((err) => err.path)
.sort();
const result: IValidation<T> = await validate(async () => y)(x);
const actual: string[] = result.success
? []
: result.errors.map((err) => err.path).sort();
if (
expected.length !== actual.length ||
expected.every((str, i) => str === actual[i]) === false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export const _test_functional_validateEqualsParameters =
if (expected.length === 0) return;

const [x, y] = callback(input);
const actual: string[] = validate(() => y)(x)
.errors.map((err) => err.path)
.sort();
const result: IValidation<T> = validate(() => y)(x);
const actual: string[] = result.success
? []
: result.errors.map((err) => err.path).sort();
if (
expected.length !== actual.length ||
expected.every((str, i) => str === actual[i]) === false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export const _test_functional_validateEqualsParametersAsync =
if (expected.length === 0) return;

const [x, y] = callback(input);
const actual: string[] = (await validate(async () => y)(x)).errors
.map((err) => err.path)
.sort();
const result: IValidation<T> = await validate(async () => y)(x);
const actual: string[] = result.success
? []
: result.errors.map((err) => err.path).sort();
if (
expected.length !== actual.length ||
expected.every((str, i) => str === actual[i]) === false
Expand Down
7 changes: 4 additions & 3 deletions test/src/internal/_test_functional_validateEqualsReturn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export const _test_functional_validateEqualsReturn =
if (expected.length === 0) return;

const [x, y] = callback(input);
const actual: string[] = validate(() => y)(x)
.errors.map((err) => err.path)
.sort();
const result: IValidation<T> = validate(() => y)(x);
const actual: string[] = result.success
? []
: result.errors.map((err) => err.path).sort();
if (
expected.length !== actual.length ||
expected.every((str, i) => str === actual[i]) === false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export const _test_functional_validateEqualsReturnAsync =
if (expected.length === 0) return;

const [x, y] = callback(input);
const actual: string[] = (await validate(async () => y)(x)).errors
.map((err) => err.path)
.sort();
const result: IValidation<T> = await validate(async () => y)(x);
const actual: string[] = result.success
? []
: result.errors.map((err) => err.path).sort();
if (
expected.length !== actual.length ||
expected.every((str, i) => str === actual[i]) === false
Expand Down
7 changes: 4 additions & 3 deletions test/src/internal/_test_validateEquals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ export const _test_validateEquals =
if (expected.length === 0) return;

// SOLUTION
const actual: string[] = validateEquals(input)
.errors.map((err) => err.path)
.sort();
const result: IValidation<T> = validateEquals(input);
const actual: string[] = result.success
? []
: result.errors.map((err) => err.path).sort();

// COMPARE
if (
Expand Down