Skip to content

Commit

Permalink
Refactoring for significant naming
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Nov 30, 2024
1 parent dcd0536 commit c008a99
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 139 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@samchon/openapi",
"version": "2.0.0-dev.20241201",
"version": "2.0.0-dev.20241201-2",
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
Expand Down
34 changes: 6 additions & 28 deletions src/composers/HttpLlmApplicationComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IHttpLlmApplication } from "../structures/IHttpLlmApplication";
import { IHttpLlmFunction } from "../structures/IHttpLlmFunction";
import { IHttpMigrateApplication } from "../structures/IHttpMigrateApplication";
import { IHttpMigrateRoute } from "../structures/IHttpMigrateRoute";
import { ILlmFunction } from "../structures/ILlmFunction";
import { ILlmSchema } from "../structures/ILlmSchema";
import { LlmSchemaComposer } from "./LlmSchemaComposer";

Expand Down Expand Up @@ -77,29 +78,6 @@ export namespace HttpLlmComposer {
};
};

export const separate = <Model extends ILlmSchema.Model>(props: {
model: Model;
parameters: ILlmSchema.ModelParameters[Model];
predicate: (schema: ILlmSchema.ModelSchema[Model]) => boolean;
}): IHttpLlmFunction.ISeparated<Model> => {
const separator: (props: {
predicate: (schema: ILlmSchema.ModelSchema[Model]) => boolean;
schema: ILlmSchema.ModelSchema[Model];
}) => [
ILlmSchema.ModelParameters[Model] | null,
ILlmSchema.ModelParameters[Model] | null,
] = LlmSchemaComposer.separate(props.model) as any;

const [llm, human] = separator({
predicate: props.predicate,
schema: props.parameters,
});
return {
llm,
human,
} satisfies IHttpLlmFunction.ISeparated<Model>;
};

const composeFunction = <Model extends ILlmSchema.Model>(props: {
model: Model;
components: OpenApi.IComponents;
Expand Down Expand Up @@ -207,11 +185,11 @@ export namespace HttpLlmComposer {
strict: true,
parameters,
separated: props.options.separate
? separate({
model: props.model,
predicate: props.options.separate,
parameters,
})
? (LlmSchemaComposer.separateParameters(props.model)({
predicate: props.options.separate as any,
parameters:
parameters satisfies ILlmSchema.ModelParameters[Model] as any,
}) as ILlmFunction.ISeparated<Model>)
: undefined,
output: output as any,
description: (() => {
Expand Down
21 changes: 11 additions & 10 deletions src/composers/LlmSchemaComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ export namespace LlmSchemaComposer {
export const schema = <Model extends ILlmSchema.Model>(model: Model) =>
SCHEMA_CASTERS[model];

export const separate = <Model extends ILlmSchema.Model>(model: Model) =>
SEPARATORS[model];

export const defaultConfig = <Model extends ILlmSchema.Model>(model: Model) =>
DEFAULT_CONFIGS[model];

export const typeChecker = <Model extends ILlmSchema.Model>(model: Model) =>
TYPE_CHECKERS[model];

export const separateParameters = <Model extends ILlmSchema.Model>(
model: Model,
) => SEPARATE_PARAMETERS[model];
}

const PARAMETERS_CASTERS = {
Expand All @@ -53,13 +54,13 @@ const SCHEMA_CASTERS = {
"3.1": LlmSchemaV3_1Composer.schema,
};

const SEPARATORS = {
chatgpt: ChatGptSchemaComposer.separate,
claude: ClaudeSchemaComposer.separate,
gemini: GeminiSchemaComposer.separate,
llama: LlamaSchemaComposer.separate,
"3.0": LlmSchemaV3Composer.separate,
"3.1": LlmSchemaV3_1Composer.separate,
const SEPARATE_PARAMETERS = {
chatgpt: ChatGptSchemaComposer.separateParameters,
claude: ClaudeSchemaComposer.separateParameters,
gemini: GeminiSchemaComposer.separateParameters,
llama: LlamaSchemaComposer.separateParameters,
"3.0": LlmSchemaV3Composer.separateParameters,
"3.1": LlmSchemaV3_1Composer.separateParameters,
};

const DEFAULT_CONFIGS = {
Expand Down
38 changes: 18 additions & 20 deletions src/composers/llm/ChatGptSchemaComposer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OpenApi } from "../../OpenApi";
import { IChatGptSchema } from "../../structures/IChatGptSchema";
import { ILlmFunction } from "../../structures/ILlmFunction";
import { ILlmSchemaV3_1 } from "../../structures/ILlmSchemaV3_1";
import { ChatGptTypeChecker } from "../../utils/ChatGptTypeChecker";
import { LlmTypeCheckerV3_1 } from "../../utils/LlmTypeCheckerV3_1";
Expand Down Expand Up @@ -150,44 +151,41 @@ export namespace ChatGptSchemaComposer {
};
};

export const separate = (props: {
export const separateParameters = (props: {
predicate: (schema: IChatGptSchema) => boolean;
schema: IChatGptSchema.IParameters;
}): [
IChatGptSchema.IParameters | null,
IChatGptSchema.IParameters | null,
] => {
parameters: IChatGptSchema.IParameters;
}): ILlmFunction.ISeparated<"chatgpt"> => {
const [llm, human] = separateObject({
$defs: props.schema.$defs,
$defs: props.parameters.$defs,
predicate: props.predicate,
schema: props.schema,
schema: props.parameters,
});
if (llm === null || human === null)
return [
llm as IChatGptSchema.IParameters | null,
human as IChatGptSchema.IParameters | null,
];
const output: [IChatGptSchema.IParameters, IChatGptSchema.IParameters] = [
{
return {
llm: llm as IChatGptSchema.IParameters | null,
human: human as IChatGptSchema.IParameters | null,
};
const output: ILlmFunction.ISeparated<"chatgpt"> = {
llm: {
...llm,
$defs: Object.fromEntries(
Object.entries(props.schema.$defs).filter(([key]) =>
Object.entries(props.parameters.$defs).filter(([key]) =>
key.endsWith(".Llm"),
),
),
},
{
human: {
...human,
$defs: Object.fromEntries(
Object.entries(props.schema.$defs).filter(([key]) =>
Object.entries(props.parameters.$defs).filter(([key]) =>
key.endsWith(".Human"),
),
),
},
];
for (const key of Object.keys(props.schema.$defs))
};
for (const key of Object.keys(props.parameters.$defs))
if (key.endsWith(".Llm") === false && key.endsWith(".Human") === false)
delete props.schema.$defs[key];
delete props.parameters.$defs[key];
return output;
};

Expand Down
11 changes: 7 additions & 4 deletions src/composers/llm/ClaudeSchemaComposer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OpenApi } from "../../OpenApi";
import { IClaudeSchema } from "../../structures/IClaudeSchema";
import { ILlmFunction } from "../../structures/ILlmFunction";
import { LlmSchemaV3_1Composer } from "./LlmSchemaV3_1Composer";

export namespace ClaudeSchemaComposer {
Expand Down Expand Up @@ -35,9 +36,11 @@ export namespace ClaudeSchemaComposer {
},
});

export const separate = (props: {
export const separateParameters = (props: {
predicate: (schema: IClaudeSchema) => boolean;
schema: IClaudeSchema.IParameters;
}): [IClaudeSchema | null, IClaudeSchema | null] =>
LlmSchemaV3_1Composer.separate(props);
parameters: IClaudeSchema.IParameters;
}): ILlmFunction.ISeparated<"claude"> =>
LlmSchemaV3_1Composer.separateParameters(
props,
) as any as ILlmFunction.ISeparated<"claude">;
}
13 changes: 7 additions & 6 deletions src/composers/llm/GeminiSchemaComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OpenApi } from "../../OpenApi";
import { OpenApiV3 } from "../../OpenApiV3";
import { OpenApiV3_1 } from "../../OpenApiV3_1";
import { IGeminiSchema } from "../../structures/IGeminiSchema";
import { ILlmFunction } from "../../structures/ILlmFunction";
import { ILlmSchemaV3 } from "../../structures/ILlmSchemaV3";
import { LlmTypeCheckerV3 } from "../../utils/LlmTypeCheckerV3";
import { OpenApiTypeChecker } from "../../utils/OpenApiTypeChecker";
Expand Down Expand Up @@ -91,16 +92,16 @@ export namespace GeminiSchemaComposer {
return schema as IGeminiSchema;
};

export const separate = (props: {
export const separateParameters = (props: {
predicate: (schema: IGeminiSchema) => boolean;
schema: IGeminiSchema.IParameters;
}): [IGeminiSchema.IParameters | null, IGeminiSchema.IParameters | null] =>
LlmSchemaV3Composer.separate(
parameters: IGeminiSchema.IParameters;
}): ILlmFunction.ISeparated<"gemini"> =>
LlmSchemaV3Composer.separateParameters(
props as {
predicate: (schema: ILlmSchemaV3) => boolean;
schema: ILlmSchemaV3.IParameters;
parameters: ILlmSchemaV3.IParameters;
},
);
) as any as ILlmFunction.ISeparated<"gemini">;
}

const isOneOf =
Expand Down
11 changes: 7 additions & 4 deletions src/composers/llm/LlamaSchemaComposer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OpenApi } from "../../OpenApi";
import { ILlamaSchema } from "../../structures/ILlamaSchema";
import { ILlmFunction } from "../../structures/ILlmFunction";
import { LlmSchemaV3_1Composer } from "./LlmSchemaV3_1Composer";

export namespace LlamaSchemaComposer {
Expand Down Expand Up @@ -35,9 +36,11 @@ export namespace LlamaSchemaComposer {
},
});

export const separate = (props: {
export const separateParameters = (props: {
predicate: (schema: ILlamaSchema) => boolean;
schema: ILlamaSchema.IParameters;
}): [ILlamaSchema | null, ILlamaSchema | null] =>
LlmSchemaV3_1Composer.separate(props);
parameters: ILlamaSchema.IParameters;
}): ILlmFunction.ISeparated<"llama"> =>
LlmSchemaV3_1Composer.separateParameters(
props,
) as any as ILlmFunction.ISeparated<"llama">;
}
13 changes: 8 additions & 5 deletions src/composers/llm/LlmSchemaV3Composer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OpenApi } from "../../OpenApi";
import { OpenApiV3Downgrader } from "../../converters/OpenApiV3Downgrader";
import { ILlmFunction } from "../../structures/ILlmFunction";
import { ILlmSchemaV3 } from "../../structures/ILlmSchemaV3";
import { LlmTypeCheckerV3 } from "../../utils/LlmTypeCheckerV3";
import { OpenApiContraintShifter } from "../../utils/OpenApiContraintShifter";
Expand Down Expand Up @@ -122,14 +123,16 @@ export namespace LlmSchemaV3Composer {
return downgraded;
};

export const separate = (props: {
export const separateParameters = (props: {
predicate: (schema: ILlmSchemaV3) => boolean;
schema: ILlmSchemaV3.IParameters;
}): [ILlmSchemaV3.IParameters | null, ILlmSchemaV3.IParameters | null] =>
separateObject({
parameters: ILlmSchemaV3.IParameters;
}): ILlmFunction.ISeparated<"3.0"> => {
const [llm, human] = separateObject({
predicate: props.predicate,
schema: props.schema,
schema: props.parameters,
});
return { llm, human };
};

const separateStation = (props: {
predicate: (schema: ILlmSchemaV3) => boolean;
Expand Down
38 changes: 18 additions & 20 deletions src/composers/llm/LlmSchemaV3_1Composer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OpenApi } from "../../OpenApi";
import { ILlmFunction } from "../../structures/ILlmFunction";
import { ILlmSchemaV3_1 } from "../../structures/ILlmSchemaV3_1";
import { LlmTypeCheckerV3_1 } from "../../utils/LlmTypeCheckerV3_1";
import { OpenApiContraintShifter } from "../../utils/OpenApiContraintShifter";
Expand Down Expand Up @@ -274,44 +275,41 @@ export namespace LlmSchemaV3_1Composer {
};
};

export const separate = (props: {
export const separateParameters = (props: {
predicate: (schema: ILlmSchemaV3_1) => boolean;
schema: ILlmSchemaV3_1.IParameters;
}): [
ILlmSchemaV3_1.IParameters | null,
ILlmSchemaV3_1.IParameters | null,
] => {
parameters: ILlmSchemaV3_1.IParameters;
}): ILlmFunction.ISeparated<"3.1"> => {
const [llm, human] = separateObject({
$defs: props.schema.$defs,
$defs: props.parameters.$defs,
predicate: props.predicate,
schema: props.schema,
schema: props.parameters,
});
if (llm === null || human === null)
return [
llm as ILlmSchemaV3_1.IParameters | null,
human as ILlmSchemaV3_1.IParameters | null,
];
const output: [ILlmSchemaV3_1.IParameters, ILlmSchemaV3_1.IParameters] = [
{
return {
llm: llm as ILlmSchemaV3_1.IParameters | null,
human: human as ILlmSchemaV3_1.IParameters | null,
};
const output: ILlmFunction.ISeparated<"3.1"> = {
llm: {
...llm,
$defs: Object.fromEntries(
Object.entries(props.schema.$defs).filter(([key]) =>
Object.entries(props.parameters.$defs).filter(([key]) =>
key.endsWith(".Llm"),
),
),
},
{
human: {
...human,
$defs: Object.fromEntries(
Object.entries(props.schema.$defs).filter(([key]) =>
Object.entries(props.parameters.$defs).filter(([key]) =>
key.endsWith(".Human"),
),
),
},
];
for (const key of Object.keys(props.schema.$defs))
};
for (const key of Object.keys(props.parameters.$defs))
if (key.endsWith(".Llm") === false && key.endsWith(".Human") === false)
delete props.schema.$defs[key];
delete props.parameters.$defs[key];
return output;
};

Expand Down
19 changes: 14 additions & 5 deletions test/features/llm/validate_llm_parameters_separate_array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const validate_llm_parameters_separate_array = <Model extends ILlmSchema.Model>(
constraint: boolean,
): void => {
const separator = (schema: ILlmSchema.IParameters<Model>) =>
LlmSchemaComposer.separate(model)({
LlmSchemaComposer.separateParameters(model)({
predicate: (s) =>
LlmSchemaComposer.typeChecker(model).isString(
s as OpenApi.IJsonSchema.IString,
) &&
(constraint
? (s as OpenApi.IJsonSchema.IString).contentMediaType !== undefined
: s.description?.includes("@contentMediaType") === true),
schema: schema as any,
parameters: schema as any,
});
const member: ILlmSchema.IParameters<Model> = schema(
model,
Expand All @@ -53,9 +53,18 @@ const validate_llm_parameters_separate_array = <Model extends ILlmSchema.Model>(
constraint,
)(typia.json.schemas<[IManagement<ICombined>]>());

TestValidator.equals("member")(separator(member))([member, null]);
TestValidator.equals("upload")(separator(upload))([null, upload]);
TestValidator.equals("combined")(separator(combined))([member, upload]);
TestValidator.equals("member")(separator(member))({
llm: member,
human: null,
});
TestValidator.equals("upload")(separator(upload))({
llm: null,
human: upload,
});
TestValidator.equals("combined")(separator(combined))({
llm: member,
human: upload,
});
};

interface IManagement<T> {
Expand Down
Loading

0 comments on commit c008a99

Please sign in to comment.