Skip to content

Commit

Permalink
Fix additionalProperties's separation problem
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Dec 11, 2024
1 parent 56bad25 commit 0431aae
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wrtnio/schema",
"version": "3.0.0",
"version": "3.0.1",
"description": "JSON and LLM function calling schemas extended for Wrtn Studio Pro",
"main": "lib/index.js",
"module": "./lib/index.mjs",
Expand Down Expand Up @@ -31,7 +31,7 @@
},
"homepage": "https://github.com/wrtnio/schema#readme",
"dependencies": {
"@samchon/openapi": "^2.0.0"
"@samchon/openapi": "^2.0.3"
},
"devDependencies": {
"@nestia/core": "^4.0.0",
Expand All @@ -45,10 +45,12 @@
"@rollup/plugin-typescript": "^11.1.6",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/multer": "^1.4.12",
"chalk": "4.1.2",
"multer": "^1.4.5-lts.1",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"rollup": "^4.22.0",
"source-map-support": "^0.5.21",
"ts-patch": "^3.2.1",
"typescript": "5.5.4",
"typescript-transform-paths": "^3.5.2",
Expand Down
13 changes: 8 additions & 5 deletions src/internal/HttpOpenAiSeparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ export namespace HttpOpenAiSeparator {
(
input: IOpenAiSchema.IObject,
): [IOpenAiSchema.IObject | null, IOpenAiSchema.IObject | null] => {
if (
!!input.additionalProperties ||
Object.keys(input.properties ?? {}).length === 0
)
return [input, null];
const llm = {
...input,
properties: {} as Record<string, IOpenAiSchema>,
Expand All @@ -78,6 +73,14 @@ export namespace HttpOpenAiSeparator {
if (x !== null) llm.properties[key] = x;
if (y !== null) human.properties[key] = y;
}
if (
typeof input.additionalProperties === "object" &&
input.additionalProperties !== null
) {
const [dx, dy] = schema(predicator)(input.additionalProperties);
llm.additionalProperties = dx ?? false;
human.additionalProperties = dy ?? false;
}
return [
Object.keys(llm.properties).length === 0 ? null : shrinkRequired(llm),
Object.keys(human.properties).length === 0
Expand Down
47 changes: 47 additions & 0 deletions test/features/test_http_llm_merge_parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { TestValidator } from "@nestia/e2e";
import { IHttpOpenAiFunction, IOpenAiSchema } from "@wrtnio/schema";
import { LlmDataMerger } from "@wrtnio/schema/lib/internal/HttpOpenAiMerger";

export const test_http_llm_merge_parameters = (): void => {
const p = LlmDataMerger.parameters({
function: {
parameters: [
{
type: "array",
items: {},
},
] satisfies IOpenAiSchema[],
separated: {
human: [
{
index: 0,
schema: {
type: "array",
items: {},
},
},
],
llm: [
{
index: 0,
schema: {
type: "array",
items: {},
},
},
],
} satisfies IHttpOpenAiFunction.ISeparated,
} as IHttpOpenAiFunction,
llm: [],
human: [
{
a: 1,
},
],
});
TestValidator.equals(p as any)([
{
a: 1,
},
]);
};
31 changes: 31 additions & 0 deletions test/features/test_http_llm_separate_parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TestValidator } from "@nestia/e2e";
import { OpenApi } from "@samchon/openapi";
import { HttpOpenAi, OpenAiTypeChecker } from "@wrtnio/schema";
import typia from "typia";

export const test_http_llm_separate_parameters = async (): Promise<void> => {
const document: OpenApi.IDocument = typia.assert<OpenApi.IDocument>(
await fetch(
"https://wrtnio.github.io/connectors/swagger/swagger.json",
).then((r) => r.json()),
);
for (const path of Object.keys(document.paths ?? {}))
if (path !== "/connector/notion/database-item/{databaseId}")
delete document.paths?.[path];

const app = HttpOpenAi.application({
document,
options: {
separate: (schema) =>
OpenAiTypeChecker.isString(schema) &&
schema["x-wrtn-secret-key"] !== undefined,
},
});
const func = app.functions.find(
(f) =>
f.method === "post" &&
f.path === "/connector/notion/database-item/{databaseId}",
);
if (func === undefined) throw new Error("Function not found");
TestValidator.equals("human")(func.separated?.human.at(0)?.index)(1);
};

0 comments on commit 0431aae

Please sign in to comment.