structured-outputs 0.1.0-beta.1
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +202 -0
- package/README.md +8 -0
- package/esm/F.d.ts +13 -0
- package/esm/F.d.ts.map +1 -0
- package/esm/F.js +10 -0
- package/esm/F.js.map +1 -0
- package/esm/ResponseFormat.d.ts +14 -0
- package/esm/ResponseFormat.d.ts.map +1 -0
- package/esm/ResponseFormat.js +13 -0
- package/esm/ResponseFormat.js.map +1 -0
- package/esm/_base.d.ts +13 -0
- package/esm/_base.d.ts.map +1 -0
- package/esm/_base.js +40 -0
- package/esm/_base.js.map +1 -0
- package/esm/array.d.ts +4 -0
- package/esm/array.d.ts.map +1 -0
- package/esm/array.js +9 -0
- package/esm/array.js.map +1 -0
- package/esm/common.d.ts +6 -0
- package/esm/common.d.ts.map +1 -0
- package/esm/common.js +2 -0
- package/esm/common.js.map +1 -0
- package/esm/literal.d.ts +4 -0
- package/esm/literal.d.ts.map +1 -0
- package/esm/literal.js +8 -0
- package/esm/literal.js.map +1 -0
- package/esm/mod.d.ts +16 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +12 -0
- package/esm/mod.js.map +1 -0
- package/esm/number.d.ts +4 -0
- package/esm/number.d.ts.map +1 -0
- package/esm/number.js +8 -0
- package/esm/number.js.map +1 -0
- package/esm/object.d.ts +7 -0
- package/esm/object.d.ts.map +1 -0
- package/esm/object.js +11 -0
- package/esm/object.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/schema.d.ts +4 -0
- package/esm/schema.d.ts.map +1 -0
- package/esm/schema.js +9 -0
- package/esm/schema.js.map +1 -0
- package/esm/string.d.ts +4 -0
- package/esm/string.d.ts.map +1 -0
- package/esm/string.js +8 -0
- package/esm/string.js.map +1 -0
- package/esm/union.d.ts +6 -0
- package/esm/union.d.ts.map +1 -0
- package/esm/union.js +8 -0
- package/esm/union.js.map +1 -0
- package/package.json +20 -0
- package/src/F.ts +27 -0
- package/src/ResponseFormat.ts +31 -0
- package/src/_base.ts +25 -0
- package/src/array.ts +11 -0
- package/src/common.ts +6 -0
- package/src/literal.ts +10 -0
- package/src/mod.ts +19 -0
- package/src/number.ts +10 -0
- package/src/object.ts +17 -0
- package/src/schema.ts +10 -0
- package/src/string.ts +10 -0
- package/src/union.ts +12 -0
package/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright 2024 Harry Solovay
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# `structured-outputs`
|
2
|
+
|
3
|
+
Refining this as I work on a story generation use case. Was annoyed by the Zod dependency and figured why not go down
|
4
|
+
the rabbit hole.
|
5
|
+
|
6
|
+
<!-- A **WIP** purpose-built virtual type system for modeling
|
7
|
+
[OpenAI structured outputs](https://platform.openai.com/docs/guides/structured-outputs) without duplicate definitions
|
8
|
+
nor codegen. -->
|
package/esm/F.d.ts
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
import type { Ty } from "./_base.js";
|
2
|
+
import type { ValidRootKey } from "./common.js";
|
3
|
+
export type F = {
|
4
|
+
type: "function";
|
5
|
+
/** The name of the function. */
|
6
|
+
name: string;
|
7
|
+
/** The description of the function. */
|
8
|
+
description: string;
|
9
|
+
/** Parameters of the function in JSON Schema. */
|
10
|
+
parameters: unknown;
|
11
|
+
};
|
12
|
+
export declare function F<M extends Record<string, Ty>>(name: string, description: string, models: M, rootKey: ValidRootKey<M>): F;
|
13
|
+
//# sourceMappingURL=F.d.ts.map
|
package/esm/F.d.ts.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"F.d.ts","sourceRoot":"","sources":["../src/F.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAG/C,MAAM,MAAM,CAAC,GAAG;IACd,IAAI,EAAE,UAAU,CAAA;IAChB,gCAAgC;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,uCAAuC;IACvC,WAAW,EAAE,MAAM,CAAA;IACnB,iDAAiD;IACjD,UAAU,EAAE,OAAO,CAAA;CACpB,CAAA;AAED,wBAAgB,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,EAC5C,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,CAAC,EACT,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GACvB,CAAC,CAOH"}
|
package/esm/F.js
ADDED
package/esm/F.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"F.js","sourceRoot":"","sources":["../src/F.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAYpC,MAAM,UAAU,CAAC,CACf,IAAY,EACZ,WAAmB,EACnB,MAAS,EACT,OAAwB;IAExB,OAAO;QACL,IAAI,EAAE,UAAU;QAChB,IAAI;QACJ,WAAW;QACX,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;KACpC,CAAA;AACH,CAAC"}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import type { Ty } from "./_base.js";
|
2
|
+
import type { ValidRootKey } from "./common.js";
|
3
|
+
export type ResponseFormat = {
|
4
|
+
type: "json_schema";
|
5
|
+
/** The desired return type in JSON Schema. */
|
6
|
+
json_schema: {
|
7
|
+
name: string;
|
8
|
+
description: string;
|
9
|
+
schema: Record<string, unknown>;
|
10
|
+
strict: true;
|
11
|
+
};
|
12
|
+
};
|
13
|
+
export declare function ResponseFormat<M extends Record<string, Ty>>(name: string, description: string, models: M, rootKey: ValidRootKey<M>): ResponseFormat;
|
14
|
+
//# sourceMappingURL=ResponseFormat.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ResponseFormat.d.ts","sourceRoot":"","sources":["../src/ResponseFormat.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAG/C,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,aAAa,CAAA;IACnB,8CAA8C;IAC9C,WAAW,EAAE;QACX,IAAI,EAAE,MAAM,CAAA;QACZ,WAAW,EAAE,MAAM,CAAA;QACnB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QAC/B,MAAM,EAAE,IAAI,CAAA;KACb,CAAA;CACF,CAAA;AAED,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,EACzD,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,CAAC,EACT,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GACvB,cAAc,CAUhB"}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { schema } from "./schema.js";
|
2
|
+
export function ResponseFormat(name, description, models, rootKey) {
|
3
|
+
return {
|
4
|
+
type: "json_schema",
|
5
|
+
json_schema: {
|
6
|
+
name,
|
7
|
+
description,
|
8
|
+
schema: schema(models, rootKey),
|
9
|
+
strict: true,
|
10
|
+
},
|
11
|
+
};
|
12
|
+
}
|
13
|
+
//# sourceMappingURL=ResponseFormat.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ResponseFormat.js","sourceRoot":"","sources":["../src/ResponseFormat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAapC,MAAM,UAAU,cAAc,CAC5B,IAAY,EACZ,WAAmB,EACnB,MAAS,EACT,OAAwB;IAExB,OAAO;QACL,IAAI,EAAE,aAAa;QACnB,WAAW,EAAE;YACX,IAAI;YACJ,WAAW;YACX,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;YAC/B,MAAM,EAAE,IAAI;SACb;KACF,CAAA;AACH,CAAC"}
|
package/esm/_base.d.ts
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
export interface Ty<N = any> {
|
2
|
+
new (): N;
|
3
|
+
schema(ctx: Context): Record<string, unknown>;
|
4
|
+
}
|
5
|
+
export declare function make<Ty_ extends Ty>(schema: (ctx: Context) => unknown): Ty_;
|
6
|
+
export declare class Context {
|
7
|
+
readonly root: Ty;
|
8
|
+
readonly models?: Record<string, Ty> | undefined;
|
9
|
+
defs: Map<Ty, string>;
|
10
|
+
constructor(root: Ty, models?: Record<string, Ty> | undefined);
|
11
|
+
ref(value: Ty): Record<string, unknown>;
|
12
|
+
}
|
13
|
+
//# sourceMappingURL=_base.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"_base.d.ts","sourceRoot":"","sources":["../src/_base.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,EAAE,CAAC,CAAC,GAAG,GAAG;IACzB,QAAO,CAAC,CAAA;IACR,MAAM,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAC9C;AAED,wBAAgB,IAAI,CAAC,GAAG,SAAS,EAAE,EAAE,MAAM,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,GAG/D,GAAG,CACT;AAED,qBAAa,OAAO;IAGhB,QAAQ,CAAC,IAAI,EAAE,EAAE;IACjB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC;IAHtC,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,CAAA;gBAEV,IAAI,EAAE,EAAE,EACR,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,YAAA;IAKtC,GAAG,CAAC,KAAK,EAAE,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAIxC"}
|
package/esm/_base.js
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
export function make(schema) {
|
2
|
+
var _a;
|
3
|
+
return _a = class Ty {
|
4
|
+
},
|
5
|
+
Object.defineProperty(_a, "schema", {
|
6
|
+
enumerable: true,
|
7
|
+
configurable: true,
|
8
|
+
writable: true,
|
9
|
+
value: schema
|
10
|
+
}),
|
11
|
+
_a;
|
12
|
+
}
|
13
|
+
export class Context {
|
14
|
+
constructor(root, models) {
|
15
|
+
Object.defineProperty(this, "root", {
|
16
|
+
enumerable: true,
|
17
|
+
configurable: true,
|
18
|
+
writable: true,
|
19
|
+
value: root
|
20
|
+
});
|
21
|
+
Object.defineProperty(this, "models", {
|
22
|
+
enumerable: true,
|
23
|
+
configurable: true,
|
24
|
+
writable: true,
|
25
|
+
value: models
|
26
|
+
});
|
27
|
+
Object.defineProperty(this, "defs", {
|
28
|
+
enumerable: true,
|
29
|
+
configurable: true,
|
30
|
+
writable: true,
|
31
|
+
value: void 0
|
32
|
+
});
|
33
|
+
this.defs = new Map(models ? Object.entries(models).map(([k, v]) => [v, k]) : []);
|
34
|
+
}
|
35
|
+
ref(value) {
|
36
|
+
const key = this.defs.get(value);
|
37
|
+
return key ? { $ref: `#/$defs/${key}` } : value.schema(this);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
//# sourceMappingURL=_base.js.map
|
package/esm/_base.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"_base.js","sourceRoot":"","sources":["../src/_base.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU,IAAI,CAAiB,MAAiC;;IACpE,OAAO,KAAA,MAAM,EAAE;SAEd;QADQ;;;;mBAAS,MAAM;UAAA;UAChB,CAAA;AACV,CAAC;AAED,MAAM,OAAO,OAAO;IAElB,YACW,IAAQ,EACR,MAA2B;QADpC;;;;mBAAS,IAAI;WAAI;QACjB;;;;mBAAS,MAAM;WAAqB;QAHtC;;;;;WAAqB;QAKnB,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IACnF,CAAC;IAED,GAAG,CAAC,KAAS;QACX,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QAChC,OAAO,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,WAAW,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC9D,CAAC;CACF"}
|
package/esm/array.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAE,EAAE,MAAM,YAAY,CAAA;AAE1C,wBAAgB,KAAK,CAAC,CAAC,SAAS,EAAE,EAAE,WAAW,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAM3F;AAED,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,EAAE,GAAG,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA"}
|
package/esm/array.js
ADDED
package/esm/array.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../src/array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAW,MAAM,YAAY,CAAA;AAE1C,MAAM,UAAU,KAAK,CAAe,WAA+B,EAAE,OAAU;IAC7E,OAAO,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACpB,IAAI,EAAE,OAAO;QACb,WAAW;QACX,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC;KACxB,CAAC,CAAC,CAAA;AACL,CAAC"}
|
package/esm/common.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,MAAM;KAC5D,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK;CAClE,CAAA"}
|
package/esm/common.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":""}
|
package/esm/literal.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"literal.d.ts","sourceRoot":"","sources":["../src/literal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAE,EAAE,MAAM,YAAY,CAAA;AAE1C,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAK1G;AAED,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,CAAA"}
|
package/esm/literal.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"literal.js","sourceRoot":"","sources":["../src/literal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAW,MAAM,YAAY,CAAA;AAE1C,MAAM,UAAU,OAAO,CAA4B,WAA+B,EAAE,KAAQ;IAC1F,OAAO,IAAI,CAAe,GAAG,EAAE,CAAC,CAAC;QAC/B,KAAK,EAAE,KAAK;QACZ,WAAW;KACZ,CAAC,CAAC,CAAA;AACL,CAAC"}
|
package/esm/mod.d.ts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
import type { ArrayTy } from "./array.js";
|
2
|
+
import type { NumberTy } from "./number.js";
|
3
|
+
import type { ObjectTy } from "./object.js";
|
4
|
+
import type { StringTy } from "./string.js";
|
5
|
+
export type RootTy = NumberTy | StringTy | ObjectTy | ArrayTy;
|
6
|
+
export * from "./array.js";
|
7
|
+
export * from "./common.js";
|
8
|
+
export * from "./F.js";
|
9
|
+
export * from "./literal.js";
|
10
|
+
export * from "./number.js";
|
11
|
+
export * from "./object.js";
|
12
|
+
export * from "./ResponseFormat.js";
|
13
|
+
export * from "./schema.js";
|
14
|
+
export * from "./string.js";
|
15
|
+
export * from "./union.js";
|
16
|
+
//# sourceMappingURL=mod.d.ts.map
|
package/esm/mod.d.ts.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACzC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,MAAM,MAAM,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,OAAO,CAAA;AAI7D,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,QAAQ,CAAA;AACtB,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
|
package/esm/mod.js
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
// moderate --exclude _base.ts code_critique.ts
|
2
|
+
export * from "./array.js";
|
3
|
+
export * from "./common.js";
|
4
|
+
export * from "./F.js";
|
5
|
+
export * from "./literal.js";
|
6
|
+
export * from "./number.js";
|
7
|
+
export * from "./object.js";
|
8
|
+
export * from "./ResponseFormat.js";
|
9
|
+
export * from "./schema.js";
|
10
|
+
export * from "./string.js";
|
11
|
+
export * from "./union.js";
|
12
|
+
//# sourceMappingURL=mod.js.map
|
package/esm/mod.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAOA,+CAA+C;AAE/C,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,QAAQ,CAAA;AACtB,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
|
package/esm/number.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAE,EAAE,MAAM,YAAY,CAAA;AAE1C,wBAAgB,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,QAAQ,CAKrD;AAED,MAAM,MAAM,QAAQ,GAAG,EAAE,CAAC,MAAM,CAAC,CAAA"}
|
package/esm/number.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"number.js","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAW,MAAM,YAAY,CAAA;AAE1C,MAAM,UAAU,MAAM,CAAC,WAAoB;IACzC,OAAO,IAAI,CAAW,GAAG,EAAE,CAAC,CAAC;QAC3B,IAAI,EAAE,QAAQ;QACd,WAAW;KACZ,CAAC,CAAC,CAAA;AACL,CAAC"}
|
package/esm/object.d.ts
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
import { type Ty } from "./_base.js";
|
2
|
+
export declare function object<F extends ObjectTyFields>(description: string | undefined, fields: F): ObjectTy<F>;
|
3
|
+
export type ObjectTyFields = Record<string, Ty>;
|
4
|
+
export type ObjectTy<F extends ObjectTyFields = any> = Ty<{
|
5
|
+
[K in keyof F]: F[K] extends Ty ? InstanceType<F[K]> : F[K];
|
6
|
+
}>;
|
7
|
+
//# sourceMappingURL=object.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAE,EAAE,MAAM,YAAY,CAAA;AAE1C,wBAAgB,MAAM,CAAC,CAAC,SAAS,cAAc,EAAE,WAAW,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAQxG;AAED,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;AAE/C,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,cAAc,GAAG,GAAG,IAAI,EAAE,CACvD;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAChE,CAAA"}
|
package/esm/object.js
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
import { make } from "./_base.js";
|
2
|
+
export function object(description, fields) {
|
3
|
+
return make((ctx) => ({
|
4
|
+
type: "object",
|
5
|
+
description,
|
6
|
+
properties: Object.fromEntries(Object.entries(fields).map(([k, v]) => [k, ctx.ref(v)])),
|
7
|
+
additionalProperties: false,
|
8
|
+
required: Object.keys(fields),
|
9
|
+
}));
|
10
|
+
}
|
11
|
+
//# sourceMappingURL=object.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAW,MAAM,YAAY,CAAA;AAE1C,MAAM,UAAU,MAAM,CAA2B,WAA+B,EAAE,MAAS;IACzF,OAAO,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACpB,IAAI,EAAE,QAAQ;QACd,WAAW;QACX,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACvF,oBAAoB,EAAE,KAAK;QAC3B,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;KAC9B,CAAC,CAAC,CAAA;AACL,CAAC"}
|
package/esm/package.json
ADDED
package/esm/schema.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,EAAE,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAMjH"}
|
package/esm/schema.js
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
import { Context } from "./_base.js";
|
2
|
+
export function schema(models, rootKey) {
|
3
|
+
const root = models[rootKey];
|
4
|
+
const ctx = new Context(root, models);
|
5
|
+
return Object.assign(root.schema(ctx), {
|
6
|
+
$defs: Object.fromEntries(Object.entries(models).map(([k, v]) => [k, v.schema(ctx)])),
|
7
|
+
});
|
8
|
+
}
|
9
|
+
//# sourceMappingURL=schema.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAW,MAAM,YAAY,CAAA;AAG7C,MAAM,UAAU,MAAM,CAA+B,MAAS,EAAE,OAAwB;IACtF,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAE,CAAA;IAC7B,MAAM,GAAG,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACrC,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;QACrC,KAAK,EAAE,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;KACtF,CAAC,CAAA;AACJ,CAAC"}
|
package/esm/string.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAE,EAAE,MAAM,YAAY,CAAA;AAE1C,wBAAgB,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,QAAQ,CAKrD;AAED,MAAM,MAAM,QAAQ,GAAG,EAAE,CAAC,MAAM,CAAC,CAAA"}
|
package/esm/string.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"string.js","sourceRoot":"","sources":["../src/string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAW,MAAM,YAAY,CAAA;AAE1C,MAAM,UAAU,MAAM,CAAC,WAAoB;IACzC,OAAO,IAAI,CAAW,GAAG,EAAE,CAAC,CAAC;QAC3B,IAAI,EAAE,QAAQ;QACd,WAAW;KACZ,CAAC,CAAC,CAAA;AACL,CAAC"}
|
package/esm/union.d.ts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import { type Ty } from "./_base.js";
|
2
|
+
export declare function union<M extends Ty[]>(description: string | undefined, members: [...M]): UnionTy<M>;
|
3
|
+
export type UnionTy<M extends Ty[]> = Ty<{
|
4
|
+
[K in keyof M]: M[K] extends Ty ? InstanceType<M[K]> : M[K];
|
5
|
+
}[number]>;
|
6
|
+
//# sourceMappingURL=union.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../src/union.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAE,EAAE,MAAM,YAAY,CAAA;AAE1C,wBAAgB,KAAK,CAAC,CAAC,SAAS,EAAE,EAAE,EAAE,WAAW,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAKlG;AAED,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,CACtC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC,MAAM,CAAC,CACxE,CAAA"}
|
package/esm/union.js
ADDED
package/esm/union.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"union.js","sourceRoot":"","sources":["../src/union.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAW,MAAM,YAAY,CAAA;AAE1C,MAAM,UAAU,KAAK,CAAiB,WAA+B,EAAE,OAAe;IACpF,OAAO,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACpB,WAAW;QACX,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;KAChD,CAAC,CAAC,CAAA;AACL,CAAC"}
|
package/package.json
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
{
|
2
|
+
"name": "structured-outputs",
|
3
|
+
"version": "0.1.0-beta.1",
|
4
|
+
"description": "Lightweight virtual types for OpenAI structured outputs.",
|
5
|
+
"repository": "github:harrysolovay/structured-outputs.git",
|
6
|
+
"license": "Apache-2.0",
|
7
|
+
"module": "./esm/mod.js",
|
8
|
+
"exports": {
|
9
|
+
".": {
|
10
|
+
"import": "./esm/mod.js"
|
11
|
+
}
|
12
|
+
},
|
13
|
+
"dependencies": {
|
14
|
+
"tslib": "^2.6.2"
|
15
|
+
},
|
16
|
+
"devDependencies": {
|
17
|
+
"@types/node": "^20.9.0"
|
18
|
+
},
|
19
|
+
"_generatedBy": "dnt@dev"
|
20
|
+
}
|
package/src/F.ts
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import type { Ty } from "./_base.js"
|
2
|
+
import type { ValidRootKey } from "./common.js"
|
3
|
+
import { schema } from "./schema.js"
|
4
|
+
|
5
|
+
export type F = {
|
6
|
+
type: "function"
|
7
|
+
/** The name of the function. */
|
8
|
+
name: string
|
9
|
+
/** The description of the function. */
|
10
|
+
description: string
|
11
|
+
/** Parameters of the function in JSON Schema. */
|
12
|
+
parameters: unknown
|
13
|
+
}
|
14
|
+
|
15
|
+
export function F<M extends Record<string, Ty>>(
|
16
|
+
name: string,
|
17
|
+
description: string,
|
18
|
+
models: M,
|
19
|
+
rootKey: ValidRootKey<M>,
|
20
|
+
): F {
|
21
|
+
return {
|
22
|
+
type: "function",
|
23
|
+
name,
|
24
|
+
description,
|
25
|
+
parameters: schema(models, rootKey),
|
26
|
+
}
|
27
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import type { Ty } from "./_base.js"
|
2
|
+
import type { ValidRootKey } from "./common.js"
|
3
|
+
import { schema } from "./schema.js"
|
4
|
+
|
5
|
+
export type ResponseFormat = {
|
6
|
+
type: "json_schema"
|
7
|
+
/** The desired return type in JSON Schema. */
|
8
|
+
json_schema: {
|
9
|
+
name: string
|
10
|
+
description: string
|
11
|
+
schema: Record<string, unknown>
|
12
|
+
strict: true
|
13
|
+
}
|
14
|
+
}
|
15
|
+
|
16
|
+
export function ResponseFormat<M extends Record<string, Ty>>(
|
17
|
+
name: string,
|
18
|
+
description: string,
|
19
|
+
models: M,
|
20
|
+
rootKey: ValidRootKey<M>,
|
21
|
+
): ResponseFormat {
|
22
|
+
return {
|
23
|
+
type: "json_schema",
|
24
|
+
json_schema: {
|
25
|
+
name,
|
26
|
+
description,
|
27
|
+
schema: schema(models, rootKey),
|
28
|
+
strict: true,
|
29
|
+
},
|
30
|
+
}
|
31
|
+
}
|
package/src/_base.ts
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
export interface Ty<N = any> {
|
2
|
+
new(): N
|
3
|
+
schema(ctx: Context): Record<string, unknown>
|
4
|
+
}
|
5
|
+
|
6
|
+
export function make<Ty_ extends Ty>(schema: (ctx: Context) => unknown) {
|
7
|
+
return class Ty {
|
8
|
+
static schema = schema
|
9
|
+
} as Ty_
|
10
|
+
}
|
11
|
+
|
12
|
+
export class Context {
|
13
|
+
defs: Map<Ty, string>
|
14
|
+
constructor(
|
15
|
+
readonly root: Ty,
|
16
|
+
readonly models?: Record<string, Ty>,
|
17
|
+
) {
|
18
|
+
this.defs = new Map(models ? Object.entries(models).map(([k, v]) => [v, k]) : [])
|
19
|
+
}
|
20
|
+
|
21
|
+
ref(value: Ty): Record<string, unknown> {
|
22
|
+
const key = this.defs.get(value)
|
23
|
+
return key ? { $ref: `#/$defs/${key}` } : value.schema(this)
|
24
|
+
}
|
25
|
+
}
|
package/src/array.ts
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
import { make, type Ty } from "./_base.js"
|
2
|
+
|
3
|
+
export function array<E extends Ty>(description: string | undefined, element: E): ArrayTy<E> {
|
4
|
+
return make((ctx) => ({
|
5
|
+
type: "array",
|
6
|
+
description,
|
7
|
+
items: ctx.ref(element),
|
8
|
+
}))
|
9
|
+
}
|
10
|
+
|
11
|
+
export type ArrayTy<E extends Ty = any> = Ty<Array<InstanceType<E>>>
|
package/src/common.ts
ADDED
package/src/literal.ts
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
import { make, type Ty } from "./_base.js"
|
2
|
+
|
3
|
+
export function literal<V extends number | string>(description: string | undefined, value: V): LiteralTy<V> {
|
4
|
+
return make<LiteralTy<V>>(() => ({
|
5
|
+
const: value,
|
6
|
+
description,
|
7
|
+
}))
|
8
|
+
}
|
9
|
+
|
10
|
+
export type LiteralTy<V extends number | string = any> = Ty<V>
|
package/src/mod.ts
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
import type { ArrayTy } from "./array.js"
|
2
|
+
import type { NumberTy } from "./number.js"
|
3
|
+
import type { ObjectTy } from "./object.js"
|
4
|
+
import type { StringTy } from "./string.js"
|
5
|
+
|
6
|
+
export type RootTy = NumberTy | StringTy | ObjectTy | ArrayTy
|
7
|
+
|
8
|
+
// moderate --exclude _base.ts code_critique.ts
|
9
|
+
|
10
|
+
export * from "./array.js"
|
11
|
+
export * from "./common.js"
|
12
|
+
export * from "./F.js"
|
13
|
+
export * from "./literal.js"
|
14
|
+
export * from "./number.js"
|
15
|
+
export * from "./object.js"
|
16
|
+
export * from "./ResponseFormat.js"
|
17
|
+
export * from "./schema.js"
|
18
|
+
export * from "./string.js"
|
19
|
+
export * from "./union.js"
|
package/src/number.ts
ADDED
package/src/object.ts
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
import { make, type Ty } from "./_base.js"
|
2
|
+
|
3
|
+
export function object<F extends ObjectTyFields>(description: string | undefined, fields: F): ObjectTy<F> {
|
4
|
+
return make((ctx) => ({
|
5
|
+
type: "object",
|
6
|
+
description,
|
7
|
+
properties: Object.fromEntries(Object.entries(fields).map(([k, v]) => [k, ctx.ref(v)])),
|
8
|
+
additionalProperties: false,
|
9
|
+
required: Object.keys(fields),
|
10
|
+
}))
|
11
|
+
}
|
12
|
+
|
13
|
+
export type ObjectTyFields = Record<string, Ty>
|
14
|
+
|
15
|
+
export type ObjectTy<F extends ObjectTyFields = any> = Ty<
|
16
|
+
{ [K in keyof F]: F[K] extends Ty ? InstanceType<F[K]> : F[K] }
|
17
|
+
>
|
package/src/schema.ts
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
import { Context, type Ty } from "./_base.js"
|
2
|
+
import type { ValidRootKey } from "./common.js"
|
3
|
+
|
4
|
+
export function schema<M extends Record<string, Ty>>(models: M, rootKey: ValidRootKey<M>): Record<string, unknown> {
|
5
|
+
const root = models[rootKey]!
|
6
|
+
const ctx = new Context(root, models)
|
7
|
+
return Object.assign(root.schema(ctx), {
|
8
|
+
$defs: Object.fromEntries(Object.entries(models).map(([k, v]) => [k, v.schema(ctx)])),
|
9
|
+
})
|
10
|
+
}
|
package/src/string.ts
ADDED
package/src/union.ts
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
import { make, type Ty } from "./_base.js"
|
2
|
+
|
3
|
+
export function union<M extends Ty[]>(description: string | undefined, members: [...M]): UnionTy<M> {
|
4
|
+
return make((ctx) => ({
|
5
|
+
description,
|
6
|
+
oneOf: members.map((member) => ctx.ref(member)),
|
7
|
+
}))
|
8
|
+
}
|
9
|
+
|
10
|
+
export type UnionTy<M extends Ty[]> = Ty<
|
11
|
+
{ [K in keyof M]: M[K] extends Ty ? InstanceType<M[K]> : M[K] }[number]
|
12
|
+
>
|