From 970475ba80433a69f9c4f099618e820bfca36aa9 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Tue, 23 Jul 2024 15:35:15 +0900 Subject: [PATCH] Add `OpenAiFetcher.propagate()` function. --- package.json | 12 +++++------ src/OpenAiFetcher.ts | 51 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 3560f56..cae7095 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wrtnio/openai-function-schema", - "version": "0.2.0", + "version": "0.2.1", "description": "OpenAI LLM function schema from OpenAPI (Swagger) document", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -37,16 +37,16 @@ "author": "", "license": "ISC", "dependencies": { - "@nestia/fetcher": "^3.7.0", + "@nestia/fetcher": "^3.8.0", "@samchon/openapi": "^0.4.2", "commander": "^10.0.0", "inquirer": "^8.2.5", - "typia": "^6.5.0" + "typia": "^6.5.4" }, "devDependencies": { - "@nestia/core": "^3.7.0", + "@nestia/core": "^3.8.0", "@nestia/e2e": "^0.7.0", - "@nestia/sdk": "^3.7.0", + "@nestia/sdk": "^3.8.0", "@nestjs/common": "^10.3.10", "@nestjs/core": "^10.3.10", "@nestjs/platform-express": "^10.3.10", @@ -63,7 +63,7 @@ "rollup": "^4.18.0", "ts-node": "^10.9.2", "ts-patch": "^3.2.1", - "typescript": "^5.5.3", + "typescript": "^5.5.4", "typescript-transform-paths": "^3.4.7", "uuid": "^10.0.0" }, diff --git a/src/OpenAiFetcher.ts b/src/OpenAiFetcher.ts index c218a77..96f442e 100644 --- a/src/OpenAiFetcher.ts +++ b/src/OpenAiFetcher.ts @@ -1,4 +1,4 @@ -import { IConnection } from "@nestia/fetcher"; +import { IConnection, IPropagation } from "@nestia/fetcher"; import { PlainFetcher } from "@nestia/fetcher/lib/PlainFetcher"; import { IMigrateRoute } from "@samchon/openapi"; @@ -6,7 +6,7 @@ import { IOpenAiDocument } from "./structures/IOpenAiDocument"; import { IOpenAiFunction } from "./structures/IOpenAiFunction"; /** - * Function call executor for OpenAI. + * Function call executors for OpenAI. * * `OpenAiFetcher` is a module for function call execution with target function's * metadata {@link IOpenAiFunction} and OpenAI composed arguments. @@ -47,10 +47,10 @@ export namespace OpenAiFetcher { /** * Execute the function call. * - * `OpenAiFetcher.fetch()` is a function executing the target API endpoint with + * `OpenAiFetcher.execute()` is a function executing the target API endpoint with * the operation's metadata {@link IOpenAiFunction} and OpenAI composed arguments. * - * Also, `OpenAiFetcher.fetch()` is designed to consider + * Also, `OpenAiFetcher.execute()` is designed to consider * {@link IOpenAiDocument.IOptions.keyword} option, so that it can unwrap the * composed arguments into the function call arguments automatically. * @@ -58,12 +58,43 @@ export namespace OpenAiFetcher { * to use {@link OpenAiDataCombiner.parameters} function to combine the LLM and * human arguments into one, by yourself manually. * - * @param props - * @returns + * @param props Function call properties. + * @returns Response of the function call. */ - export const execute = async (props: IProps): Promise => { - const route: IMigrateRoute = props.function.route(); - return PlainFetcher.fetch( + export const execute = (props: IProps): Promise => + PlainFetcher.fetch(...getFetcherArguments(props)); + + /** + * Propagate the function call. + * + * `OpenAiFetcher.propagate()` is a function propagating the target API endpoint with + * the operation's metadata {@link IOpenAiFunction} and OpenAI composed arguments. + * + * Also, `OpenAiFetcher.execute()` is designed to consider + * {@link IOpenAiDocument.IOptions.keyword} option, so that it can unwrap the + * composed arguments into the function call arguments automatically. + * + * However, about the {@link IOpenAiDocument.IOptions.separate} case, you have + * to use {@link OpenAiDataCombiner.parameters} function to combine the LLM and + * human arguments into one, by yourself manually. + * + * > For reference, the propagation means always returning the response even if the + * > request is failled, with the response's status code. About detailed concept + * > of the propagation, refer to the {@link IPropagation} type. + * + * @param props Function call properties. + * @returns Propagation of the function call. + */ + export const propagate = ( + props: IProps, + ): Promise> => + PlainFetcher.propagate(...getFetcherArguments(props)) as Promise< + IPropagation.IBranch + >; + + const getFetcherArguments = (props: IProps) => { + const route = props.function.route(); + return [ props.connection, { method: props.function.method.toUpperCase() as "POST", @@ -89,7 +120,7 @@ export namespace OpenAiFetcher { ? props.arguments[0].body : props.arguments[route.parameters.length + (route.query ? 1 : 0)] : undefined, - ); + ] as const; }; const getKeywordPath = (props: {