Skip to content
This repository was archived by the owner on Mar 20, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions src/composers/LlmSchemaComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export namespace LlmSchemaComposer {
};

export const schema = (props: {
config: ILlmSchema.IConfig;
config?: Partial<ILlmSchema.IConfig>;
components: OpenApi.IComponents;
$defs: Record<string, ILlmSchema>;
schema: OpenApi.IJsonSchema;
Expand Down Expand Up @@ -782,14 +782,14 @@ export namespace LlmSchemaComposer {
}),
} satisfies OpenApi.IJsonSchema;
};
}

const getConfig = (
config?: Partial<ILlmSchema.IConfig> | undefined,
): ILlmSchema.IConfig => ({
reference: config?.reference ?? true,
strict: config?.strict ?? false,
});
export const getConfig = (
config?: Partial<ILlmSchema.IConfig> | undefined,
): ILlmSchema.IConfig => ({
reference: config?.reference ?? true,
strict: config?.strict ?? false,
});
}

const validateStrict = (
schema: OpenApi.IJsonSchema,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
import { TestValidator } from "@nestia/e2e";
import {
ILlmSchemaV3_1,
ILlmSchema,
IOpenApiSchemaError,
IResult,
LlmTypeCheckerV3_1,
LlmTypeChecker,
OpenApi,
OpenApiTypeChecker,
} from "@samchon/openapi";
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer";
import typia, { IJsonSchemaUnit } from "typia";

export const test_claude_schema_discriminator = (): void =>
validate_llm_schema_discriminator("claude");

export const test_llama_v31_schema_discriminator = (): void =>
validate_llm_schema_discriminator("3.1");

const validate_llm_schema_discriminator = (vendor: "claude" | "3.1"): void => {
const $defs: Record<string, ILlmSchemaV3_1> = {};
export const test_llm_schema_discriminator = (): void => {
const $defs: Record<string, ILlmSchema> = {};
const unit: IJsonSchemaUnit = typia.json.schema<ICat | IAnt>();
const result: IResult<ILlmSchemaV3_1, IOpenApiSchemaError> =
LlmSchemaComposer.schema(vendor)({
config: {
reference: true,
constraint: true,
},
const result: IResult<ILlmSchema, IOpenApiSchemaError> =
LlmSchemaComposer.schema({
$defs,
components: unit.components,
schema: unit.schema,
});
if (result.success === false) throw new Error("Failed to transform");
TestValidator.predicate("discriminator")(
() =>
LlmTypeCheckerV3_1.isOneOf(result.value) &&
result.value.discriminator !== undefined &&
result.value.discriminator.mapping !== undefined &&
Object.values(result.value.discriminator.mapping).every((k) =>
LlmTypeChecker.isAnyOf(result.value) &&
result.value["x-discriminator"] !== undefined &&
result.value["x-discriminator"].mapping !== undefined &&
Object.values(result.value["x-discriminator"].mapping).every((k) =>
k.startsWith("#/$defs/"),
),
);

const invert: OpenApi.IJsonSchema = LlmSchemaComposer.invert(vendor)({
const invert: OpenApi.IJsonSchema = LlmSchemaComposer.invert({
components: {},
$defs,
schema: result.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,7 @@ import {
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer";
import typia, { IJsonSchemaCollection } from "typia";

export const test_chatgpt_schema_tuple = (): void =>
validate_llm_schema_tuple("chatgpt");

export const test_claude_schema_tuple = (): void =>
validate_llm_schema_tuple("claude");

export const test_gemini_schema_tuple = (): void =>
validate_llm_schema_tuple("gemini");

export const test_llm_v30_schema_tuple = (): void =>
validate_llm_schema_tuple("3.0");

export const test_llm_v31_schema_tuple = (): void =>
validate_llm_schema_tuple("3.1");

const validate_llm_schema_tuple = <Model extends ILlmSchema.Model>(
model: Model,
): void => {
export const test_llm_schema_tuple = (): void => {
const collection: IJsonSchemaCollection = typia.json.schemas<
[
[string, number],
Expand All @@ -44,7 +27,7 @@ const validate_llm_schema_tuple = <Model extends ILlmSchema.Model>(
}>,
]
>();
const v = validate(model)(collection.components);
const v = validate(collection.components);
v(collection.schemas[0])(["$input"]);
v(collection.schemas[1])([
`$input.properties["input"]`,
Expand All @@ -56,22 +39,16 @@ const validate_llm_schema_tuple = <Model extends ILlmSchema.Model>(
};

const validate =
<Model extends ILlmSchema.Model>(model: Model) =>
(components: OpenApi.IComponents) =>
(schema: OpenApi.IJsonSchema) =>
(expected: string[]): void => {
const result: IResult<
ILlmSchema.IParameters<Model>,
IOpenApiSchemaError
> = LlmSchemaComposer.schema(model)({
config: LlmSchemaComposer.defaultConfig(
model,
) satisfies ILlmSchema.IConfig<Model> as any,
accessor: "$input",
components,
schema,
$defs: {},
} as any) as IResult<ILlmSchema.IParameters<Model>, IOpenApiSchemaError>;
const result: IResult<ILlmSchema, IOpenApiSchemaError> =
LlmSchemaComposer.schema({
accessor: "$input",
components,
schema,
$defs: {},
});
TestValidator.equals("success")(result.success)(false);
TestValidator.equals("errors")(
result.success ? [] : result.error.reasons.map((r) => r.accessor).sort(),
Expand Down
34 changes: 34 additions & 0 deletions test/src/features/llm/schema/test_llm_type_checker_cover_any.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { TestValidator } from "@nestia/e2e";
import { ILlmSchema, LlmTypeChecker, OpenApi } from "@samchon/openapi";
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer";
import typia, { IJsonSchemaCollection } from "typia";

export const test_llm_type_checker_cover_any = () => {
const collection: IJsonSchemaCollection = typia.json.schemas<[IBasic]>();
const result = LlmSchemaComposer.parameters({
components: collection.components,
schema: collection.schemas[0] as OpenApi.IJsonSchema.IReference,
});
if (result.success === false)
throw new Error(`Failed to compose parameters.`);

const parameters = result.value;
const check = (x: ILlmSchema, y: ILlmSchema): boolean =>
LlmTypeChecker.covers({
x,
y,
$defs: parameters.$defs,
});
TestValidator.equals("any covers (string | null)")(true)(
check(parameters.properties.any, parameters.properties.string_or_null),
);
TestValidator.equals("any covers (string | undefined)")(true)(
check(parameters.properties.any, parameters.properties.string_or_undefined),
);
};

interface IBasic {
any: any;
string_or_null: null | string;
string_or_undefined: string | undefined;
}
173 changes: 173 additions & 0 deletions test/src/features/llm/schema/test_llm_type_checker_cover_array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { TestValidator } from "@nestia/e2e";
import { LlmTypeChecker, OpenApi } from "@samchon/openapi";
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer";
import typia, { IJsonSchemaCollection } from "typia";

export const test_llm_type_checker_cover_array = () => {
const collection: IJsonSchemaCollection =
typia.json.schemas<[Plan2D, Plan3D, Box2D, Box3D]>();
const components: OpenApi.IComponents = collection.components as any;
const plan2D: OpenApi.IJsonSchema = components.schemas!.Plan2D;
const plan3D: OpenApi.IJsonSchema = components.schemas!.Plan3D;
const box2D: OpenApi.IJsonSchema = components.schemas!.Box2D;
const box3D: OpenApi.IJsonSchema = components.schemas!.Box3D;

const $defs = {};
const check = (x: OpenApi.IJsonSchema, y: OpenApi.IJsonSchema): boolean => {
const [a, b] = [x, y].map((schema) => {
const result = LlmSchemaComposer.schema({
components: collection.components,
schema: schema,
$defs,
});
if (result.success === false)
throw new Error(`Failed to compose schema.`);
return result.value;
});
return LlmTypeChecker.covers({
x: a,
y: b,
$defs,
});
};

TestValidator.equals("Plan3D[] covers Plan2D[]")(true)(
check({ type: "array", items: plan3D }, { type: "array", items: plan2D }),
);
TestValidator.equals("Box3D[] covers Box2D[]")(true)(
check({ type: "array", items: box3D }, { type: "array", items: box2D }),
);
TestValidator.equals("Array<Plan3D|Box3D> covers Array<Plan2D|Box2D>")(true)(
check(
{
type: "array",
items: {
oneOf: [plan3D, box3D],
},
},
{
type: "array",
items: {
oneOf: [plan2D, box2D],
},
},
),
);
TestValidator.equals("(Plan3D|Box3D)[] covers (Plan2D|Box2D)[]")(true)(
check(
{
oneOf: [
{ type: "array", items: plan3D },
{ type: "array", items: box3D },
],
},
{
oneOf: [
{ type: "array", items: plan2D },
{ type: "array", items: box2D },
],
},
),
);

TestValidator.equals("Plan2D[] can't cover Plan3D[]")(false)(
check({ type: "array", items: plan2D }, { type: "array", items: plan3D }),
);
TestValidator.equals("Box2D[] can't cover Box3D[]")(false)(
check({ type: "array", items: box2D }, { type: "array", items: box3D }),
);
TestValidator.equals("Array<Plan2D|Box2D> can't cover Array<Plan3D|Box3D>")(
false,
)(
check(
{
type: "array",
items: {
oneOf: [plan2D, box2D],
},
},
{
type: "array",
items: {
oneOf: [plan3D, box3D],
},
},
),
);
TestValidator.equals("(Plan2D[]|Box2D[]) can't cover (Plan3D[]|Box3D[])")(
false,
)(
check(
{
oneOf: [
{ type: "array", items: plan2D },
{ type: "array", items: box2D },
],
},
{
oneOf: [
{ type: "array", items: plan3D },
{ type: "array", items: box3D },
],
},
),
);
TestValidator.equals("Plan3D[] can't cover (Plan2D|Box2D)[]")(false)(
check(
{ type: "array", items: plan3D },
{
oneOf: [
{ type: "array", items: plan2D },
{ type: "array", items: box2D },
],
},
),
);
TestValidator.equals("Box3D[] can't cover Array<Plan2D|Box2D>")(false)(
check(
{ type: "array", items: box3D },
{
type: "array",
items: {
oneOf: [plan2D, box2D],
},
},
),
);
};

type Plan2D = {
center: Point2D;
size: Point2D;
geometries: Geometry2D[];
};
type Plan3D = {
center: Point3D;
size: Point3D;
geometries: Geometry3D[];
};
type Geometry3D = {
position: Point3D;
scale: Point3D;
};
type Geometry2D = {
position: Point2D;
scale: Point2D;
};
type Point2D = {
x: number;
y: number;
};
type Point3D = {
x: number;
y: number;
z: number;
};
type Box2D = {
size: Point2D;
nested: Point2D[];
};
type Box3D = {
size: Point3D;
nested: Point3D[];
};
Loading
Loading