Skip to content

Commit c957d40

Browse files
authored
Merge pull request #100 from samchon/feat/result
Completed v2 publishing
2 parents 507f62e + f0bac83 commit c957d40

9 files changed

+489
-21
lines changed

README.md

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
> ## Next version is coming.
2-
>
3-
> This is the `next` version README document.
4-
>
5-
> If you wanna see the latest version, go to the [`v1.0` branch](https://github.com/samchon/openapi/tree/v1.0).
6-
71
# `@samchon/openapi`
82
```mermaid
93
flowchart
@@ -18,12 +12,12 @@ flowchart
1812
lfc --"OpenAI"--> chatgpt("ChatGPT")
1913
lfc --"Anthropic"--> claude("Claude")
2014
lfc --"Google"--> gemini("Gemini")
21-
lfc --"Meta (Facebook)"--> llama("Llama")
15+
lfc --"Meta"--> llama("Llama")
2216
end
2317
```
2418

2519
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/samchon/openapi/blob/master/LICENSE)
26-
[![npm version](https://img.shields.io/npm/v/@samchon/openapi/next.svg)](https://www.npmjs.com/package/@samchon/openapi/next.svg)
20+
[![npm version](https://img.shields.io/npm/v/@samchon/openapi.svg)](https://www.npmjs.com/package/@samchon/openapi.svg)
2721
[![Downloads](https://img.shields.io/npm/dm/@samchon/openapi.svg)](https://www.npmjs.com/package/@samchon/openapi)
2822
[![Build Status](https://github.com/samchon/openapi/workflows/build/badge.svg)](https://github.com/samchon/openapi/actions?query=workflow%3Abuild)
2923

@@ -55,10 +49,10 @@ OpenAPI definitions, converters and LLM function calling application composer.
5549

5650
## Setup
5751
```bash
58-
npm install @samchon/openapi --tag next
52+
npm install @samchon/openapi
5953
```
6054

61-
Just install by `npm i @samchon/openapi --tag next` command.
55+
Just install by `npm i @samchon/openapi` command.
6256

6357
Here is an example code utilizing the `@samchon/openapi` for LLM function calling purpose.
6458

@@ -213,7 +207,7 @@ flowchart TD
213207
lfc --"OpenAI"--> chatgpt("ChatGPT")
214208
lfc --"Anthropic"--> claude("Claude")
215209
lfc --"Google"--> gemini("Gemini")
216-
lfc --"Meta (Facebook)"--> llama("Llama")
210+
lfc --"Meta"--> llama("Llama")
217211
end
218212
```
219213

@@ -231,9 +225,9 @@ Let's enjoy the fantastic LLM function calling feature very easily with `@samcho
231225
- [`IHttpLlmFunction`](https://github.com/samchon/openapi/blob/master/src/structures/ILlmFunction.ts)
232226
- Schemas
233227
- [`IChatGptSchema`](https://github.com/samchon/openapi/blob/master/src/structures/IChatGptSchema.ts): OpenAI ChatGPT
234-
- [`IClaudeSchema`](https://github.com/samchon/openapi/blob/master/src/structures/IClaudeSchema.ts): Anthropic Claude (same with [`ILlmSchemaV3_1`](https://github.com/samchon/openapi/blob/master/src/structures/ILlmSchemaV3_1.ts))
228+
- [`IClaudeSchema`](https://github.com/samchon/openapi/blob/master/src/structures/IClaudeSchema.ts): Anthropic Claude
235229
- [`IGeminiSchema`](https://github.com/samchon/openapi/blob/master/src/structures/IGeminiSchema.ts): Google Gemini
236-
- [`ILlamaSchema`](https://github.com/samchon/openapi/blob/master/src/structures/ILlamaSchema.ts): Meta (Facebook) Llama (same with [`ILlmSchemaV3_1`](https://github.com/samchon/openapi/blob/master/src/structures/ILlmSchemaV3_1.ts))
230+
- [`ILlamaSchema`](https://github.com/samchon/openapi/blob/master/src/structures/ILlamaSchema.ts): Meta Llama
237231
- [`ILlmSchemaV3`](https://github.com/samchon/openapi/blob/master/src/structures/ILlmSchemaV3.ts): middle layer based on OpenAPI v3.0 specification
238232
- [`ILlmSchemaV3_1`](https://github.com/samchon/openapi/blob/master/src/structures/ILlmSchemaV3_1.ts): middle layer based on OpenAPI v3.1 specification
239233
- Type Checkers

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@samchon/openapi",
3-
"version": "2.0.0-dev.20241202-2",
3+
"version": "2.0.0",
44
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.",
55
"main": "./lib/index.js",
66
"module": "./lib/index.mjs",

src/structures/IOpenApiSchemaError.ts

+44
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,58 @@
11
import { OpenApi } from "../OpenApi";
22

3+
/**
4+
* OpenAPI schema related error.
5+
*
6+
* `IOpenApiSchemaError` is a type representing an error that occured during the
7+
* iteration or transformation of the OpenAPI schema (JSON schema) of
8+
* {@link OpenApi.IJsonSchema} type.
9+
*
10+
* The most `IOpenApiSchemaError` is occured by the transformation process from
11+
* {@link OpenApi.IJsonSchema} to {@link ILlmSchema} type. The transformation can
12+
* be failed by following reasons:
13+
*
14+
* - Unable to find the {@link OpenApi.IJsonSchema.IReference} directing.
15+
* - Non-supported type in LLM schema models
16+
* - Every models do not support {@link OpenApi.IJsonSchema.ITuple}
17+
* - Gemini does not support {@link OpenApi.IJsonSchema.IOneOf}
18+
* - ChatGPT and Gemini do not support {@link OpenApi.IJsonSchema.IObject.additionalProperties}
19+
*
20+
* @author Jeongho Nam - https://github.com/samchon
21+
*/
322
export interface IOpenApiSchemaError {
23+
/**
24+
* Method that caused the error.
25+
*/
426
method: string;
27+
28+
/**
29+
* Message of the error.
30+
*/
531
message: string;
32+
33+
/**
34+
* The detailed reasons of the error.
35+
*/
636
reasons: IOpenApiSchemaError.IReason[];
737
}
838
export namespace IOpenApiSchemaError {
39+
/**
40+
* Detailed reason of the error.
41+
*/
942
export interface IReason {
43+
/**
44+
* Schema that caused the error.
45+
*/
1046
schema: OpenApi.IJsonSchema;
47+
48+
/**
49+
* Accessor to the schema.
50+
*/
1151
accessor: string;
52+
53+
/**
54+
* Message of the reason.
55+
*/
1256
message: string;
1357
}
1458
}

src/typings/IResult.ts

+35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
1+
/**
2+
* Result of an operation that can either succeed or fail.
3+
*
4+
* `IResult` is an union type that represents the result of an operation
5+
* that can either succeed or fail.
6+
*
7+
* You can distinguise the result by checking the {@link IResult.success} value,
8+
* and if it's `true`, the success value is stored in {@link IResult.value}.
9+
* Otherwise, if it's `false`, the error value is stored in {@link IResult.error}.
10+
*
11+
* @template T Type of the success value.
12+
* @template E Type of the error value.
13+
* @author Jeongho Nam - https://github.com/samchon
14+
*/
115
export type IResult<T, E> = IResult.ISuccess<T> | IResult.IFailure<E>;
216
export namespace IResult {
17+
/**
18+
* Success type of {@link IResult}.
19+
*/
320
export interface ISuccess<T> {
21+
/**
22+
* Success flag.
23+
*/
424
success: true;
25+
26+
/**
27+
* Success value.
28+
*/
529
value: T;
630
}
31+
32+
/**
33+
* Failure type of {@link IResult}.
34+
*/
735
export interface IFailure<E> {
36+
/**
37+
* Success flag.
38+
*/
839
success: false;
40+
41+
/**
42+
* The error value.
43+
*/
944
error: E;
1045
}
1146
}

src/utils/ChatGptTypeChecker.ts

+80
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,113 @@ export namespace ChatGptTypeChecker {
55
/* -----------------------------------------------------------
66
TYPE CHECKERS
77
----------------------------------------------------------- */
8+
/**
9+
* Test whether the schema is a nul type.
10+
*
11+
* @param schema Target schema
12+
* @returns Whether null type or not
13+
*/
814
export const isNull = (
915
schema: IChatGptSchema,
1016
): schema is IChatGptSchema.INull =>
1117
(schema as IChatGptSchema.INull).type === "null";
1218

19+
/**
20+
* Test whether the schema is an unknown type.
21+
*
22+
* @param schema Target schema
23+
* @returns Whether unknown type or not
24+
*/
1325
export const isUnknown = (
1426
schema: IChatGptSchema,
1527
): schema is IChatGptSchema.IUnknown =>
1628
(schema as IChatGptSchema.IUnknown).type === undefined &&
1729
!isAnyOf(schema) &&
1830
!isReference(schema);
1931

32+
/**
33+
* Test whether the schema is a boolean type.
34+
*
35+
* @param schema Target schema
36+
* @returns Whether boolean type or not
37+
*/
2038
export const isBoolean = (
2139
schema: IChatGptSchema,
2240
): schema is IChatGptSchema.IBoolean =>
2341
(schema as IChatGptSchema.IBoolean).type === "boolean";
2442

43+
/**
44+
* Test whether the schema is an integer type.
45+
*
46+
* @param schema Target schema
47+
* @returns Whether integer type or not
48+
*/
2549
export const isInteger = (
2650
schema: IChatGptSchema,
2751
): schema is IChatGptSchema.IInteger =>
2852
(schema as IChatGptSchema.IInteger).type === "integer";
2953

54+
/**
55+
* Test whether the schema is a number type.
56+
*
57+
* @param schema Target schema
58+
* @returns Whether number type or not
59+
*/
3060
export const isNumber = (
3161
schema: IChatGptSchema,
3262
): schema is IChatGptSchema.INumber =>
3363
(schema as IChatGptSchema.INumber).type === "number";
3464

65+
/**
66+
* Test whether the schema is a string type.
67+
*
68+
* @param schema Target schema
69+
* @returns Whether string type or not
70+
*/
3571
export const isString = (
3672
schema: IChatGptSchema,
3773
): schema is IChatGptSchema.IString =>
3874
(schema as IChatGptSchema.IString).type === "string";
3975

76+
/**
77+
* Test whether the schema is an array type.
78+
*
79+
* @param schema Target schema
80+
* @returns Whether array type or not
81+
*/
4082
export const isArray = (
4183
schema: IChatGptSchema,
4284
): schema is IChatGptSchema.IArray =>
4385
(schema as IChatGptSchema.IArray).type === "array" &&
4486
(schema as IChatGptSchema.IArray).items !== undefined;
4587

88+
/**
89+
* Test whether the schema is an object type.
90+
*
91+
* @param schema Target schema
92+
* @returns Whether object type or not
93+
*/
4694
export const isObject = (
4795
schema: IChatGptSchema,
4896
): schema is IChatGptSchema.IObject =>
4997
(schema as IChatGptSchema.IObject).type === "object";
5098

99+
/**
100+
* Test whether the schema is a reference type.
101+
*
102+
* @param schema Target schema
103+
* @returns Whether reference type or not
104+
*/
51105
export const isReference = (
52106
schema: IChatGptSchema,
53107
): schema is IChatGptSchema.IReference => (schema as any).$ref !== undefined;
54108

109+
/**
110+
* Test whether the schema is an union type.
111+
*
112+
* @param schema Target schema
113+
* @returns Whether union type or not
114+
*/
55115
export const isAnyOf = (
56116
schema: IChatGptSchema,
57117
): schema is IChatGptSchema.IAnyOf =>
@@ -60,6 +120,20 @@ export namespace ChatGptTypeChecker {
60120
/* -----------------------------------------------------------
61121
OPERATORS
62122
----------------------------------------------------------- */
123+
/**
124+
* Visit every nested schemas.
125+
*
126+
* Visit every nested schemas of the target, and apply the `props.closure` function.
127+
*
128+
* Here is the list of occuring nested visitings:
129+
*
130+
* - {@link IChatGptSchema.IAnyOf.anyOf}
131+
* - {@link IChatGptSchema.IReference}
132+
* - {@link IChatGptSchema.IObject.properties}
133+
* - {@link IChatGptSchema.IArray.items}
134+
*
135+
* @param props Properties for visiting
136+
*/
63137
export const visit = (props: {
64138
closure: (schema: IChatGptSchema, accessor: string) => void;
65139
$defs?: Record<string, IChatGptSchema> | undefined;
@@ -88,6 +162,12 @@ export namespace ChatGptTypeChecker {
88162
next(props.schema, props.accessor ?? "$input.schemas");
89163
};
90164

165+
/**
166+
* Test whether the `x` schema covers the `y` schema.
167+
*
168+
* @param props Properties for testing
169+
* @returns Whether the `x` schema covers the `y` schema
170+
*/
91171
export const covers = (props: {
92172
$defs?: Record<string, IChatGptSchema> | undefined;
93173
x: IChatGptSchema;

0 commit comments

Comments
 (0)