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 pathIOpenAiSchema.ts
529 lines (485 loc) · 14.2 KB
/
IOpenAiSchema.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**
* Type schema info of OpenAI function call.
*
* `IOpenAiSchema` is a type schema info of OpenAI function call.
*
* `IOpenAiSchema` is basically follows the JSON schema definition of
* OpenAI v3.0: {@link OpenApiV3.IJsonSchema}. However, `IOpenAiSchema` does not
* have the reference type {@link OpenApiV3.IJsonSchema.IReference}. It's because
* the OpenAI cannot compose
* {@link OpenAiFetcher.IProps.arguments function call arguments} of
* the reference type.
*
* For reference, the OpenAPI v3.0 based JSON schema definition can't express
* the tuple array type. It has been supported since OpenAPI v3.1. Therefore,
* it would better to avoid using the tuple array type.
*
* @author Samchon
*/
export type IOpenAiSchema =
| IOpenAiSchema.IBoolean
| IOpenAiSchema.IInteger
| IOpenAiSchema.INumber
| IOpenAiSchema.IString
| IOpenAiSchema.IArray
| IOpenAiSchema.IObject
| IOpenAiSchema.IUnknown
| IOpenAiSchema.INullOnly
| IOpenAiSchema.IOneOf;
export namespace IOpenAiSchema {
/**
* Boolean type schema info.
*/
export interface IBoolean extends __ISignificant<"boolean"> {
/**
* Default value.
*/
default?: boolean;
/**
* Enumeration values.
*/
enum?: boolean[];
}
/**
* Integer type schema info.
*/
export interface IInteger extends __ISignificant<"integer"> {
/**
* Default value.
*
* @type int64
*/
default?: number;
/**
* Enumeration values.
*
* @type int64
*/
enum?: number[];
/**
* Minimum value restriction.
*
* @type int64
*/
minimum?: number;
/**
* Maximum value restriction.
*
* @type int64
*/
maximum?: number;
/**
* Exclusive minimum value restriction.
*
* For reference, even though your Swagger (or OpenAPI) document has
* defined the `exclusiveMinimum` value as `number`, {@link OpenAiComposer}
* forcibly converts it to `boolean` type, and assign the numeric value to
* the {@link minimum} property.
*/
exclusiveMinimum?: boolean;
/**
* Exclusive maximum value restriction.
*
* For reference, even though your Swagger (or OpenAPI) document has
* defined the `exclusiveMaximum` value as `number`, {@link OpenAiComposer}
* forcibly converts it to `boolean` type, and assign the numeric value to
* the {@link maximum} property.
*/
exclusiveMaximum?: boolean;
/**
* Multiple of value restriction.
*
* @type uint64
* @exclusiveMinimum 0
*/
multipleOf?: number;
}
/**
* Number type schema info.
*/
export interface INumber extends __ISignificant<"number"> {
/**
* Default value.
*/
default?: number;
/**
* Enumeration values.
*/
enum?: number[];
/**
* Minimum value restriction.
*/
minimum?: number;
/**
* Maximum value restriction.
*/
maximum?: number;
/**
* Exclusive minimum value restriction.
*
* For reference, even though your Swagger (or OpenAPI) document has
* defined the `exclusiveMinimum` value as `number`, {@link OpenAiComposer}
* forcibly converts it to `boolean` type, and assign the numeric value to
* the {@link minimum} property.
*/
exclusiveMinimum?: boolean;
/**
* Exclusive maximum value restriction.
*
* For reference, even though your Swagger (or OpenAPI) document has
* defined the `exclusiveMaximum` value as `number`, {@link OpenAiComposer}
* forcibly converts it to `boolean` type, and assign the numeric value to
* the {@link maximum} property.
*/
exclusiveMaximum?: boolean;
/**
* Multiple of value restriction.
*
* @exclusiveMinimum 0
*/
multipleOf?: number;
}
/**
* String type schema info.
*/
export interface IString extends __ISignificant<"string"> {
/**
* Default value.
*/
default?: string;
/**
* Enumeration values.
*/
enum?: string[];
/**
* Format restriction.
*/
format?:
| "binary"
| "byte"
| "password"
| "regex"
| "uuid"
| "email"
| "hostname"
| "idn-email"
| "idn-hostname"
| "iri"
| "iri-reference"
| "ipv4"
| "ipv6"
| "uri"
| "uri-reference"
| "uri-template"
| "url"
| "date-time"
| "date"
| "time"
| "duration"
| "json-pointer"
| "relative-json-pointer"
| (string & {});
/**
* Pattern restriction.
*/
pattern?: string;
/**
* Minimum length restriction.
*
* @type uint64
*/
minLength?: number;
/**
* Maximum length restriction.
*
* @type uint64
*/
maxLength?: number;
/**
* Content media type restriction.
*/
contentMediaType?: string;
/**
* Secret key for the schema.
*
* `x-wrtn-secret-key` is a property means a secret key that is required
* for the target API endpoint calling. If the secret key is not filled,
* the API call would be failed.
*/
"x-wrtn-secret-key"?: string;
/**
* Secret scopes for the schema.
*
* `x-wrtn-secret-scopes` is a property means a list of secret scopes that
* are required for the target API endpoint calling. If the secret scopes
* are not satisfied, the API call would be failed.
*/
"x-wrtn-secret-scopes"?: string[];
}
/**
* Array type schema info.
*/
export interface IArray extends __ISignificant<"array"> {
/**
* Items type schema info.
*
* The `items` means the type of the array elements. In other words, it is
* the type schema info of the `T` in the TypeScript array type `Array<T>`.
*/
items: IOpenAiSchema;
/**
* Unique items restriction.
*
* If this property value is `true`, target array must have unique items.
*/
uniqueItems?: boolean;
/**
* Minimum items restriction.
*
* Restriction of minumum number of items in the array.
*
* @type uint64
*/
minItems?: number;
/**
* Maximum items restriction.
*
* Restriction of maximum number of items in the array.
*
* @type uint64
*/
maxItems?: number;
}
/**
* Object type schema info.
*/
export interface IObject extends __ISignificant<"object"> {
/**
* Properties of the object.
*
* The `properties` means a list of key-value pairs of the object's
* regular properties. The key is the name of the regular property,
* and the value is the type schema info.
*
* If you need additional properties that is represented by dynamic key,
* you can use the {@link additionalProperties} instead.
*/
properties?: Record<string, IOpenAiSchema>;
/**
* List of key values of the required properties.
*
* The `required` means a list of the key values of the required
* {@link properties}. If some property key is not listed in the `required`
* list, it means that property is optional. Otherwise some property key
* exists in the `required` list, it means that the property must be filled.
*
* Below is an example of the {@link properties} and `required`.
*
* ```typescript
* interface SomeObject {
* id: string;
* email: string;
* name?: string;
* }
* ```
*
* As you can see, `id` and `email` {@link properties} are {@link required},
* so that they are listed in the `required` list.
*
* ```json
* {
* "type": "object",
* "properties": {
* "id": { "type": "string" },
* "email": { "type": "string" },
* "name": { "type": "string" }
* },
* "required": ["id", "email"]
* }
* ```
*/
required?: string[];
/**
* Additional properties' info.
*
* The `additionalProperties` means the type schema info of the additional
* properties that are not listed in the {@link properties}.
*
* If the value is `true`, it means that the additional properties are not
* restricted. They can be any type. Otherwise, if the value is
* {@link IOpenAiSchema} type, it means that the additional properties must
* follow the type schema info.
*
* - `true`: `Record<string, any>`
* - `IOpenAiSchema`: `Record<string, T>`
*/
additionalProperties?: boolean | IOpenAiSchema;
}
/**
* Unknown type schema info.
*
* It means the type of the value is `any`.
*/
export interface IUnknown extends __IAttribute {
/**
* Type is never be defined.
*/
type?: undefined;
}
/**
* Null only type schema info.
*/
export interface INullOnly extends __IAttribute {
/**
* Type is always `null`.
*/
type: "null";
}
/**
* One of type schema info.
*
* `IOneOf` represents an union type of the TypeScript (`A | B | C`).
*
* For reference, even though your Swagger (or OpenAPI) document has
* defined `anyOf` instead of the `oneOf`, {@link OpenAiComposer} forcibly
* converts it to `oneOf` type.
*/
export interface IOneOf extends __IAttribute {
/**
* List of the union types.
*/
oneOf: IOpenAiSchema[];
}
/**
* Significant attributes that can be applied to the most types.
*/
export interface __ISignificant<Type extends string> extends __IAttribute {
/**
* Discriminator value of the type.
*/
type: Type;
/**
* Whether to allow `null` value or not.
*/
nullable?: boolean;
}
/**
* Common attributes that can be applied to all types.
*/
export interface __IAttribute {
/**
* Title of the schema.
*/
title?: string;
/**
* Detailed description of the schema.
*/
description?: string;
/**
* Whether the type is deprecated or not.
*/
deprecated?: boolean;
/**
* Placeholder value for frontend application.
*
* Placeholder means the value to be shown in the input field as a hint.
* For example, when an email input field exists, the placeholder value
* would be "Insert your email address here".
*/
"x-wrtn-placeholder"?: string;
/**
* Prerequisite API endpoint for the schema.
*
* `x-wrtn-prerequisite` is a property representing the prerequisite API
* interaction. It means that, the endpoint API should be called before
* calling the target API, for composing some argument value.
*
* @reference https://github.com/wrtnio/decorators/blob/main/src/Prerequisite.ts
*/
"x-wrtn-prerequisite"?: {
/**
* HTTP method to call the endpoint.
*/
method: "get" | "post" | "patch" | "put" | "delete";
/**
* Path of the endpoint.
*/
path: string;
} & (
| {
/**
* Function returning transformed values using JMESPath expression.
*
* `Prerequisite.Props.jmesPath` is a string typed property that extracts desired values
* from the prerequisite API response using a JMESPath expression. This property simplifies
* and replaces the `label`, `value`, and `array` properties.
*
* JMESPath expressions are used to extract the desired data based on the API response.
* The expression must always be a valid JMESPath syntax.
*
* - Type: `jmesPath: string`
* - Example: `"members[*].data.title"`
* - Usage: `jmespath.search(response, jmesPath)`
*
* Note: The `label`, `value`, and `array` properties are no longer in use.
*/
jmesPath: string;
}
| {
/**
* Transform function returning label.
*
* `Prerequisite.Props.label` is a string typed property representing
* a function returning the label from the element instance of the
* prerequisite API respond array.
*
* The function script must be a string value that can be parsed by
* `new Function(string)` statement. Also, its parameter names are
* always `elem`, `index` and `array`. Of course, the function's
* return type must be always `string`.
*
* - type: `label: (elem: Element, index: number, array: Element[]) => string`
* - example: `return elem.title`
* - how to use: `new Function("elem", "index", "array", labelScript)(...)`
*/
label: string;
/**
* Transform function returning target value.
*
* `Prerequisite.Props.value` is a string typed property representing
* a function returning the target value from the element instance of
* the prerequisite API respond array. If you've defined this `Prerequisite`
* type to a `number` type, the returned value must be actual number type.
*
* The function script must be a string value that can be parsed by
* `new Function(string)` statement. Also, its parameter names are always
* `elem`, `index` and `array`.
*
* - type: `value: (elem: Element, index: number, array: Element[]) => Value`
* - example: `return elem.no`
* - how to use: `new Function("elem", "index", "array", valueScript)(...)`
*/
value: string;
/**
* Transform function returning array instance.
*
* `Prerequisite.Props.array` is a string typed property representing
* a function returning an array instance from the response of the
* prerequisite API.
*
* The function script must be a string value that can be parsed by
* `new Function(string)` statement. Also, its parameter name is
* always `response`.
*
* If the prerequisite API responses an array and it is the desired one,
* you don't need to specify this property.
*
* - type: `array: (response: Response) => Elemenet[]`
* - example: `return response.members.map(m => m.data)`
* - how to use: `new Function("response", arrayScript)(response)`
*/
array?: string;
}
);
}
}