This repository was archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOpenAiTypeChecker.ts
142 lines (132 loc) · 3.97 KB
/
OpenAiTypeChecker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { IOpenAiSchema } from "./structures/IOpenAiSchema";
/**
* Type checker for OpenAI function call schema.
*
* `OpenAiTypeChecker` is a type checker of {@link IOpenAiSchema}.
*
* @author Samchon
*/
export namespace OpenAiTypeChecker {
/**
* Visit every nested schemas.
*
* Visit every nested schemas of the target, and apply the callback function
* to them.
*
* If the visitor meets an union type, it will visit every individual schemas
* in the union type. Otherwise meets an object type, it will visit every
* properties and additional properties. If the visitor meets an array type,
* it will visit the item type.
*
* @param schema Target schema to visit
* @param callback Callback function to apply
*/
export const visit = (
schema: IOpenAiSchema,
callback: (schema: IOpenAiSchema) => void,
): void => {
callback(schema);
if (isOneOf(schema)) schema.oneOf.forEach((s) => visit(s, callback));
else if (isObject(schema)) {
for (const [_, s] of Object.entries(schema.properties ?? {}))
visit(s, callback);
if (
typeof schema.additionalProperties === "object" &&
schema.additionalProperties !== null
)
visit(schema.additionalProperties, callback);
} else if (isArray(schema)) visit(schema.items, callback);
};
/**
* Test whether the schema is an union type.
*
* @param schema Target schema
* @returns Whether union type or not
*/
export const isOneOf = (
schema: IOpenAiSchema,
): schema is IOpenAiSchema.IOneOf =>
(schema as IOpenAiSchema.IOneOf).oneOf !== undefined;
/**
* Test whether the schema is an object type.
*
* @param schema Target schema
* @returns Whether object type or not
*/
export const isObject = (
schema: IOpenAiSchema,
): schema is IOpenAiSchema.IObject =>
(schema as IOpenAiSchema.IObject).type === "object";
/**
* Test whether the schema is an array type.
*
* @param schema Target schema
* @returns Whether array type or not
*/
export const isArray = (
schema: IOpenAiSchema,
): schema is IOpenAiSchema.IArray =>
(schema as IOpenAiSchema.IArray).type === "array";
/**
* Test whether the schema is a boolean type.
*
* @param schema Target schema
* @returns Whether boolean type or not
*/
export const isBoolean = (
schema: IOpenAiSchema,
): schema is IOpenAiSchema.IBoolean =>
(schema as IOpenAiSchema.IBoolean).type === "boolean";
/**
* Test whether the schema is a number type.
*
* @param schema Target schema
* @returns Whether number type or not
*/
export const isNumber = (
schema: IOpenAiSchema,
): schema is IOpenAiSchema.INumber =>
(schema as IOpenAiSchema.INumber).type === "number";
/**
* Test whether the schema is a string type.
*
* @param schema Target schema
* @returns Whether string type or not
*/
export const isString = (
schema: IOpenAiSchema,
): schema is IOpenAiSchema.IString =>
(schema as IOpenAiSchema.IString).type === "string";
/**
* Test whether the schema is a null type.
*
* @param schema Target schema
* @returns Whether null type or not
*/
export const isNullOnly = (
schema: IOpenAiSchema,
): schema is IOpenAiSchema.INullOnly =>
(schema as IOpenAiSchema.INullOnly).type === "null";
/**
* Test whether the schema is a nullable type.
*
* @param schema Target schema
* @returns Whether nullable type or not
*/
export const isNullable = (schema: IOpenAiSchema): boolean =>
!isUnknown(schema) &&
(isNullOnly(schema) ||
(isOneOf(schema)
? schema.oneOf.some(isNullable)
: schema.nullable === true));
/**
* Test whether the schema is an unknown type.
*
* @param schema Target schema
* @returns Whether unknown type or not
*/
export const isUnknown = (
schema: IOpenAiSchema,
): schema is IOpenAiSchema.IUnknown =>
!isOneOf(schema) && (schema as IOpenAiSchema.IUnknown).type === undefined;
}