structured-outputs 0.1.0-beta.22 → 0.1.0-beta.24
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +3 -3
- package/esm/ResponseFormat.d.ts +8 -8
- package/esm/ResponseFormat.d.ts.map +1 -1
- package/esm/ResponseFormat.js +4 -7
- package/esm/ResponseFormat.js.map +1 -1
- package/esm/Tool.d.ts +2 -3
- package/esm/Tool.d.ts.map +1 -1
- package/esm/Tool.js +2 -3
- package/esm/Tool.js.map +1 -1
- package/esm/types/Ty.d.ts +21 -13
- package/esm/types/Ty.d.ts.map +1 -1
- package/esm/types/Ty.js +28 -7
- package/esm/types/Ty.js.map +1 -1
- package/esm/types/array.d.ts +1 -1
- package/esm/types/array.d.ts.map +1 -1
- package/esm/types/array.js +1 -1
- package/esm/types/array.js.map +1 -1
- package/esm/types/constantUnion.d.ts +1 -1
- package/esm/types/constantUnion.d.ts.map +1 -1
- package/esm/types/constantUnion.js +2 -1
- package/esm/types/constantUnion.js.map +1 -1
- package/esm/types/mod.d.ts +2 -3
- package/esm/types/mod.d.ts.map +1 -1
- package/esm/types/mod.js +2 -3
- package/esm/types/mod.js.map +1 -1
- package/esm/types/object.d.ts +3 -4
- package/esm/types/object.d.ts.map +1 -1
- package/esm/types/object.js +3 -3
- package/esm/types/object.js.map +1 -1
- package/esm/types/primitives.d.ts +3 -4
- package/esm/types/primitives.d.ts.map +1 -1
- package/esm/types/primitives.js +3 -12
- package/esm/types/primitives.js.map +1 -1
- package/esm/types/std/Date.d.ts +4 -0
- package/esm/types/std/Date.d.ts.map +1 -0
- package/esm/types/std/Date.js +14 -0
- package/esm/types/std/Date.js.map +1 -0
- package/esm/types/std/Option.d.ts +2 -2
- package/esm/types/std/Option.d.ts.map +1 -1
- package/esm/types/std/Option.js +2 -3
- package/esm/types/std/Option.js.map +1 -1
- package/esm/types/std/color.d.ts +8 -0
- package/esm/types/std/color.d.ts.map +1 -0
- package/esm/types/std/color.js +13 -0
- package/esm/types/std/color.js.map +1 -0
- package/esm/types/std/mod.d.ts +2 -1
- package/esm/types/std/mod.d.ts.map +1 -1
- package/esm/types/std/mod.js +2 -1
- package/esm/types/std/mod.js.map +1 -1
- package/esm/types/taggedUnion.d.ts +5 -3
- package/esm/types/taggedUnion.d.ts.map +1 -1
- package/esm/types/taggedUnion.js +8 -5
- package/esm/types/taggedUnion.js.map +1 -1
- package/esm/types/transform.d.ts +3 -0
- package/esm/types/transform.d.ts.map +1 -0
- package/esm/types/transform.js +5 -0
- package/esm/types/transform.js.map +1 -0
- package/esm/types/tuple.d.ts +5 -0
- package/esm/types/tuple.d.ts.map +1 -0
- package/esm/types/tuple.js +11 -0
- package/esm/types/tuple.js.map +1 -0
- package/esm/util/recombine.d.ts +2 -0
- package/esm/util/recombine.d.ts.map +1 -0
- package/esm/util/recombine.js +4 -0
- package/esm/util/recombine.js.map +1 -0
- package/esm/util/util_types.d.ts +2 -0
- package/esm/util/util_types.d.ts.map +1 -0
- package/esm/util/util_types.js +2 -0
- package/esm/util/util_types.js.map +1 -0
- package/package.json +1 -1
- package/src/ResponseFormat.ts +14 -17
- package/src/Tool.ts +5 -6
- package/src/types/Ty.ts +74 -32
- package/src/types/array.ts +9 -5
- package/src/types/constantUnion.ts +3 -2
- package/src/types/mod.ts +2 -3
- package/src/types/object.ts +15 -9
- package/src/types/primitives.ts +3 -13
- package/src/types/std/Date.ts +19 -0
- package/src/types/std/Option.ts +3 -4
- package/src/types/std/color.ts +27 -0
- package/src/types/std/mod.ts +2 -1
- package/src/types/taggedUnion.ts +25 -22
- package/src/types/transform.ts +8 -0
- package/src/types/tuple.ts +17 -0
- package/src/util/recombine.ts +3 -0
- package/src/util/util_types.ts +3 -0
- package/esm/oai.d.ts +0 -5
- package/esm/oai.d.ts.map +0 -1
- package/esm/oai.js +0 -2
- package/esm/oai.js.map +0 -1
- package/esm/types/Ref.d.ts +0 -4
- package/esm/types/Ref.d.ts.map +0 -1
- package/esm/types/Ref.js +0 -14
- package/esm/types/Ref.js.map +0 -1
- package/esm/types/RootTy.d.ts +0 -8
- package/esm/types/RootTy.d.ts.map +0 -1
- package/esm/types/RootTy.js +0 -17
- package/esm/types/RootTy.js.map +0 -1
- package/esm/types/constant.d.ts +0 -3
- package/esm/types/constant.d.ts.map +0 -1
- package/esm/types/constant.js +0 -7
- package/esm/types/constant.js.map +0 -1
- package/esm/types/std/Wrapper.d.ts +0 -6
- package/esm/types/std/Wrapper.d.ts.map +0 -1
- package/esm/types/std/Wrapper.js +0 -5
- package/esm/types/std/Wrapper.js.map +0 -1
- package/esm/util/phantoms.d.ts +0 -2
- package/esm/util/phantoms.d.ts.map +0 -1
- package/esm/util/phantoms.js +0 -4
- package/esm/util/phantoms.js.map +0 -1
- package/esm/util/recombineTaggedTemplateArgs.d.ts +0 -2
- package/esm/util/recombineTaggedTemplateArgs.d.ts.map +0 -1
- package/esm/util/recombineTaggedTemplateArgs.js +0 -4
- package/esm/util/recombineTaggedTemplateArgs.js.map +0 -1
- package/src/oai.ts +0 -5
- package/src/types/Ref.ts +0 -19
- package/src/types/RootTy.ts +0 -38
- package/src/types/constant.ts +0 -7
- package/src/types/std/Wrapper.ts +0 -6
- package/src/util/phantoms.ts +0 -3
- package/src/util/recombineTaggedTemplateArgs.ts +0 -6
package/esm/types/taggedUnion.js
CHANGED
@@ -1,19 +1,22 @@
|
|
1
1
|
import { Ty } from "./Ty.js";
|
2
2
|
export function taggedUnion(members) {
|
3
|
-
|
3
|
+
const entries = Object.entries(members);
|
4
|
+
return Ty((subschema) => ({
|
4
5
|
discriminator: "type",
|
5
|
-
anyOf:
|
6
|
+
anyOf: entries.map(([k, v]) => ({
|
6
7
|
type: "object",
|
7
8
|
properties: {
|
8
9
|
type: {
|
9
10
|
type: "string",
|
10
11
|
const: k,
|
11
12
|
},
|
12
|
-
|
13
|
+
...(v === null ? {} : {
|
14
|
+
value: subschema(v),
|
15
|
+
}),
|
13
16
|
},
|
14
|
-
required: ["type", "value"],
|
17
|
+
required: ["type", ...v === null ? [] : ["value"]],
|
15
18
|
additionalProperties: false,
|
16
19
|
})),
|
17
|
-
}));
|
20
|
+
}), false, (v) => members[v.type][""].transform(v));
|
18
21
|
}
|
19
22
|
//# sourceMappingURL=taggedUnion.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"taggedUnion.js","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,UAAU,WAAW,
|
1
|
+
{"version":3,"file":"taggedUnion.js","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,UAAU,WAAW,CAAsC,OAAU;IAKzE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IACvC,OAAO,EAAE,CACP,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;QACd,aAAa,EAAE,MAAM;QACrB,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAC9B,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,IAAI,EAAE;oBACJ,IAAI,EAAE,QAAQ;oBACd,KAAK,EAAE,CAAC;iBACT;gBACD,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;oBACpB,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC;iBACpB,CAAC;aACH;YACD,QAAQ,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;YAClD,oBAAoB,EAAE,KAAK;SAC5B,CAAC,CAAC;KACJ,CAAC,EACF,KAAK,EACL,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAE,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CACzC,CAAA;AACH,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/types/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,EAAE,CAAC,SAAS,OAAO,EAAE,CAAC,EACpE,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EACf,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GACzB,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAEb"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"transform.js","sourceRoot":"","sources":["../../src/types/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,UAAU,SAAS,CACvB,EAAe,EACf,SAA0B;IAE1B,OAAO,EAAE,CAAa,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;AACnF,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../../src/types/tuple.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,wBAAgB,KAAK,CAAC,CAAC,SAAS,EAAE,EAAE,EAClC,GAAG,QAAQ,EAAE,CAAC,GACb,EAAE,CAAC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;CAAE,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAYzD"}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { Ty } from "./Ty.js";
|
2
|
+
export function tuple(...elements) {
|
3
|
+
const { length } = elements;
|
4
|
+
return Ty((ref) => ({
|
5
|
+
type: "object",
|
6
|
+
properties: Object.fromEntries(Array.from({ length }, (_0, i) => [i, ref(elements[i])])),
|
7
|
+
additionalProperties: false,
|
8
|
+
required: Object.keys(Array.from({ length }, (_0, i) => i)),
|
9
|
+
}), true, (v) => Array.from({ length }, (_0, i) => elements[i][""].transform(v[i])));
|
10
|
+
}
|
11
|
+
//# sourceMappingURL=tuple.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"tuple.js","sourceRoot":"","sources":["../../src/types/tuple.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,UAAU,KAAK,CACnB,GAAG,QAAW;IAEd,MAAM,EAAE,MAAM,EAAE,GAAG,QAAQ,CAAA;IAC3B,OAAO,EAAE,CACP,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACR,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,CAAC;QACzF,oBAAoB,EAAE,KAAK;QAC3B,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;KAC5D,CAAC,EACF,IAAI,EACJ,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAE,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAU,CACpF,CAAA;AACH,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"recombine.d.ts","sourceRoot":"","sources":["../../src/util/recombine.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM,CAE/F"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"recombine.js","sourceRoot":"","sources":["../../src/util/recombine.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,SAAS,CAAC,OAA6B,EAAE,MAA8B;IACrF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,CAAC,CAAA;AAC9E,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"util_types.d.ts","sourceRoot":"","sources":["../../src/util/util_types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,SACvF,KAAK,GAAG,CAAC,GACP,KAAK,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"util_types.js","sourceRoot":"","sources":["../../src/util/util_types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
package/src/ResponseFormat.ts
CHANGED
@@ -1,25 +1,23 @@
|
|
1
|
-
import type
|
2
|
-
import type {
|
3
|
-
import {
|
1
|
+
import type Openai from "openai"
|
2
|
+
import type { Ty } from "./types/mod.js"
|
3
|
+
import { recombine } from "./util/recombine.js"
|
4
4
|
|
5
5
|
export interface ResponseFormat<T> {
|
6
6
|
(template: TemplateStringsArray, ...quasis: Array<string>): ResponseFormat<T>
|
7
7
|
type: "json_schema"
|
8
8
|
/** The desired return type in JSON Schema. */
|
9
|
-
json_schema:
|
10
|
-
/**
|
11
|
-
|
12
|
-
/** Parse all choice contents into an array of typed object. */
|
13
|
-
parseChoices(completion: ChatCompletion): Array<T>
|
9
|
+
json_schema: Openai.ResponseFormatJSONSchema["json_schema"]
|
10
|
+
/** Transform the content of the first choice into a typed object. */
|
11
|
+
into(completion: ChatCompletion): T
|
14
12
|
}
|
15
13
|
|
16
|
-
export function ResponseFormat<T>(name: string, ty:
|
14
|
+
export function ResponseFormat<T>(name: string, ty: Ty<T, never, true>): ResponseFormat<T> {
|
17
15
|
return ResponseFormat_(name, ty)
|
18
16
|
}
|
19
17
|
|
20
18
|
function ResponseFormat_<T>(
|
21
19
|
name: string,
|
22
|
-
ty:
|
20
|
+
ty: Ty<T, never>,
|
23
21
|
description?: string,
|
24
22
|
): ResponseFormat<T> {
|
25
23
|
return Object.assign(
|
@@ -27,7 +25,7 @@ function ResponseFormat_<T>(
|
|
27
25
|
ResponseFormat_(
|
28
26
|
name,
|
29
27
|
ty,
|
30
|
-
description ? `${description} ${
|
28
|
+
description ? `${description} ${recombine(template, quasis)}` : undefined,
|
31
29
|
),
|
32
30
|
{
|
33
31
|
type: "json_schema" as const,
|
@@ -37,11 +35,8 @@ function ResponseFormat_<T>(
|
|
37
35
|
schema: ty.schema(),
|
38
36
|
strict: true,
|
39
37
|
},
|
40
|
-
|
41
|
-
return JSON.parse(ResponseFormat.unwrapFirstChoice(completion))
|
42
|
-
},
|
43
|
-
parseChoices: (completion: ChatCompletion): Array<T> => {
|
44
|
-
return ResponseFormat.unwrapChoices(completion).map((content) => JSON.parse(content))
|
38
|
+
into: (completion: ChatCompletion): T => {
|
39
|
+
return ty[""].transform(JSON.parse(ResponseFormat.unwrapFirstChoice(completion)))
|
45
40
|
},
|
46
41
|
toJSON() {
|
47
42
|
const { type, json_schema } = this
|
@@ -64,7 +59,7 @@ export namespace ResponseFormat {
|
|
64
59
|
return completions.choices.map(unwrapChoice)
|
65
60
|
}
|
66
61
|
|
67
|
-
function unwrapChoice(choice:
|
62
|
+
function unwrapChoice(choice: Openai.Chat.Completions.ChatCompletion.Choice) {
|
68
63
|
const { finish_reason, message } = choice
|
69
64
|
if (finish_reason !== "stop") {
|
70
65
|
throw new ResponseFormatUnwrapError(
|
@@ -87,3 +82,5 @@ export namespace ResponseFormat {
|
|
87
82
|
export class ResponseFormatUnwrapError extends Error {
|
88
83
|
override readonly name = "UnwrapResponseError"
|
89
84
|
}
|
85
|
+
|
86
|
+
type ChatCompletion = Openai.Chat.ChatCompletion
|
package/src/Tool.ts
CHANGED
@@ -1,18 +1,17 @@
|
|
1
|
-
import type {
|
2
|
-
import {
|
1
|
+
import type { Schema, Ty } from "./types/mod.js"
|
2
|
+
import { recombine } from "./util/recombine.js"
|
3
3
|
|
4
|
-
|
5
|
-
export function Tool<T>(name: string, ty: RootTy<T, never>): Tool<T> {
|
4
|
+
export function Tool<T>(name: string, ty: Ty<T, never, true>): Tool<T> {
|
6
5
|
return Tool_(name, ty)
|
7
6
|
}
|
8
7
|
|
9
|
-
function Tool_<T>(name: string, ty:
|
8
|
+
function Tool_<T>(name: string, ty: Ty<T, never, true>, description?: string): Tool<T> {
|
10
9
|
return Object.assign(
|
11
10
|
(template: TemplateStringsArray, ...quasis: Array<string>) =>
|
12
11
|
Tool_(
|
13
12
|
name,
|
14
13
|
ty,
|
15
|
-
description ? `${description} ${
|
14
|
+
description ? `${description} ${recombine(template, quasis)}` : undefined,
|
16
15
|
),
|
17
16
|
{
|
18
17
|
type: "function" as const,
|
package/src/types/Ty.ts
CHANGED
@@ -1,50 +1,92 @@
|
|
1
|
-
import {
|
2
|
-
import type {
|
3
|
-
import type { RootTy } from "./RootTy.js"
|
1
|
+
import { recombine } from "../util/recombine.js"
|
2
|
+
import type { EnsureLiteralKeys } from "../util/util_types.js"
|
4
3
|
|
5
|
-
export
|
6
|
-
|
4
|
+
export interface Ty<T = any, P extends keyof any = keyof any, R extends boolean = boolean> {
|
5
|
+
<P2 extends Array<keyof any>>(
|
6
|
+
template: TemplateStringsArray,
|
7
|
+
...params: EnsureLiteralKeys<P2>
|
8
|
+
): Ty<T, P | P2[number], R>
|
9
|
+
|
10
|
+
/** The native TypeScript type. The runtime value is nonexistent. */
|
11
|
+
T: T
|
12
|
+
/** The literal types of the parameter keys. The runtime value is nonexistent. */
|
13
|
+
P: P
|
14
|
+
|
15
|
+
"": {
|
16
|
+
subschema: Subschema
|
17
|
+
root: R
|
18
|
+
transform: (value: any) => any
|
19
|
+
context: Array<Context>
|
20
|
+
applied: Applied
|
21
|
+
}
|
22
|
+
|
23
|
+
/** Apply context to parameters. */
|
24
|
+
fill: <A extends Partial<Record<P, number | string>>>(
|
25
|
+
values: A,
|
26
|
+
) => Ty<T, Exclude<P, keyof A>, R>
|
27
|
+
|
28
|
+
/** Get the corresponding JSON Schema. */
|
29
|
+
schema(this: Ty<T, never>): Schema
|
30
|
+
}
|
31
|
+
|
32
|
+
export function Ty<T, P extends keyof any, R extends boolean, I = T>(
|
33
|
+
subschema: Subschema,
|
34
|
+
root: R,
|
35
|
+
transform: (value: I) => T = (value) => value as never,
|
7
36
|
context: Array<Context> = [],
|
8
37
|
applied: Applied = {},
|
9
|
-
): Ty<T, P> {
|
38
|
+
): Ty<T, P, R> {
|
10
39
|
return Object.assign(
|
11
|
-
<P2 extends Array<keyof any>>(template: TemplateStringsArray, ...
|
12
|
-
Ty<T, P | P2[number]
|
13
|
-
|
40
|
+
<P2 extends Array<keyof any>>(template: TemplateStringsArray, ...params: P2) =>
|
41
|
+
Ty<T, P | P2[number], R, I>(
|
42
|
+
subschema,
|
43
|
+
root,
|
44
|
+
transform,
|
45
|
+
[{ template, params }, ...context],
|
46
|
+
applied,
|
47
|
+
),
|
48
|
+
{} as { T: T; P: P },
|
14
49
|
{
|
15
|
-
"": {
|
50
|
+
"": {
|
51
|
+
subschema,
|
52
|
+
root,
|
53
|
+
transform,
|
54
|
+
context,
|
55
|
+
applied,
|
56
|
+
},
|
16
57
|
fill: <A extends Partial<Record<P, string | number>>>(values: A) => {
|
17
|
-
return Ty<T, Exclude<P, keyof A
|
58
|
+
return Ty<T, Exclude<P, keyof A>, R, I>(subschema, root, transform, context, {
|
59
|
+
...applied,
|
60
|
+
...values,
|
61
|
+
})
|
18
62
|
},
|
19
|
-
|
20
|
-
return
|
63
|
+
schema(this: Ty<T, never>) {
|
64
|
+
return SubschemaFactory({})(this)
|
21
65
|
},
|
22
66
|
},
|
23
67
|
)
|
24
68
|
}
|
25
69
|
|
26
|
-
export
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
70
|
+
export type Subschema = (subschema: SubschemaFactory) => Schema
|
71
|
+
|
72
|
+
export type SubschemaFactory = (ty: Ty) => Schema
|
73
|
+
export function SubschemaFactory(applied: Applied): SubschemaFactory {
|
74
|
+
return (ty) => {
|
75
|
+
applied = { ...applied, ...ty[""].applied }
|
76
|
+
const description = ty[""].context
|
77
|
+
.map(({ template, params }) => recombine(template, params.map((k) => applied[k]!)))
|
78
|
+
.join(" ")
|
79
|
+
return {
|
80
|
+
...ty[""].subschema(SubschemaFactory(applied)),
|
81
|
+
...description ? { description } : {},
|
82
|
+
}
|
37
83
|
}
|
38
|
-
fill: <A extends Partial<Record<P, number | string>>>(values: A) => Ty<T, Exclude<P, keyof A>>
|
39
|
-
isRoot(): this is RootTy
|
40
84
|
}
|
41
85
|
|
42
|
-
|
43
|
-
export type Schema = Record<string, unknown>
|
44
|
-
|
45
|
-
export interface Context {
|
86
|
+
interface Context {
|
46
87
|
template: TemplateStringsArray
|
47
|
-
|
88
|
+
params: Array<keyof any>
|
48
89
|
}
|
90
|
+
type Applied = Record<keyof any, number | string>
|
49
91
|
|
50
|
-
export type
|
92
|
+
export type Schema = Record<string, unknown>
|
package/src/types/array.ts
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
import { Ty } from "./Ty.js"
|
2
2
|
|
3
|
-
export function array<E extends Ty>(element: E): Ty<Array<E["T"]>, E["P"]> {
|
4
|
-
return Ty(
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
export function array<E extends Ty>(element: E): Ty<Array<E["T"]>, E["P"], false> {
|
4
|
+
return Ty(
|
5
|
+
(ref) => ({
|
6
|
+
type: "array",
|
7
|
+
items: ref(element),
|
8
|
+
}),
|
9
|
+
false,
|
10
|
+
(value) => value.map(element[""].transform),
|
11
|
+
)
|
8
12
|
}
|
@@ -1,9 +1,10 @@
|
|
1
1
|
import { partition } from "../deps/jsr.io/@std/collections/1.0.9/mod.js"
|
2
2
|
import { Ty } from "./Ty.js"
|
3
3
|
|
4
|
+
// TODO: use single `enum` type in cases where there aren't both numbers and strings
|
4
5
|
export function constantUnion<M extends Array<number | string>>(
|
5
6
|
...members: M
|
6
|
-
): Ty<M[number], never> {
|
7
|
+
): Ty<M[number], never, false> {
|
7
8
|
const [strings, numbers] = partition(members, (v) => typeof v === "string")
|
8
9
|
return Ty(() => ({
|
9
10
|
anyOf: [
|
@@ -20,5 +21,5 @@ export function constantUnion<M extends Array<number | string>>(
|
|
20
21
|
}]
|
21
22
|
: [],
|
22
23
|
],
|
23
|
-
}))
|
24
|
+
}), false)
|
24
25
|
}
|
package/src/types/mod.ts
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
export * from "./array.js"
|
2
|
-
export * from "./constant.js"
|
3
2
|
export * from "./constantUnion.js"
|
4
3
|
export * from "./object.js"
|
5
4
|
export * from "./primitives.js"
|
6
|
-
export * from "./Ref.js"
|
7
|
-
export * from "./RootTy.js"
|
8
5
|
export * from "./std/mod.js"
|
9
6
|
export * from "./taggedUnion.js"
|
7
|
+
export * from "./transform.js"
|
8
|
+
export * from "./tuple.js"
|
10
9
|
export * from "./Ty.js"
|
package/src/types/object.ts
CHANGED
@@ -1,13 +1,19 @@
|
|
1
|
-
import {
|
2
|
-
import type { Ty } from "./Ty.js"
|
1
|
+
import { Ty } from "./Ty.js"
|
3
2
|
|
4
3
|
export function object<F extends Record<string, Ty>>(
|
5
4
|
fields: F,
|
6
|
-
):
|
7
|
-
return
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
5
|
+
): Ty<{ [K in keyof F]: F[K]["T"] }, F[keyof F]["P"], true> {
|
6
|
+
return Ty(
|
7
|
+
(ref) => ({
|
8
|
+
type: "object",
|
9
|
+
properties: Object.fromEntries(Object.entries(fields).map(([k, v]) => [k, ref(v)])),
|
10
|
+
additionalProperties: false,
|
11
|
+
required: Object.keys(fields),
|
12
|
+
}),
|
13
|
+
true,
|
14
|
+
(v) =>
|
15
|
+
Object.fromEntries(
|
16
|
+
Object.entries(v).map(([k, v]) => [k, fields[k]![""].transform(v)]),
|
17
|
+
) as never,
|
18
|
+
)
|
13
19
|
}
|
package/src/types/primitives.ts
CHANGED
@@ -1,17 +1,7 @@
|
|
1
1
|
import { Ty } from "./Ty.js"
|
2
2
|
|
3
|
-
export const
|
4
|
-
type: "null",
|
5
|
-
}))
|
3
|
+
export const boolean: Ty<boolean, never, false> = Ty(() => ({ type: "boolean" }), false)
|
6
4
|
|
7
|
-
export const
|
8
|
-
type: "boolean",
|
9
|
-
}))
|
5
|
+
export const number: Ty<number, never, false> = Ty(() => ({ type: "number" }), false)
|
10
6
|
|
11
|
-
export const
|
12
|
-
type: "number",
|
13
|
-
}))
|
14
|
-
|
15
|
-
export const string: Ty<string, never> = Ty(() => ({
|
16
|
-
type: "string",
|
17
|
-
}))
|
7
|
+
export const string: Ty<string, never, false> = Ty(() => ({ type: "string" }), false)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { object } from "../object.js"
|
2
|
+
import { number } from "../primitives.js"
|
3
|
+
import { transform } from "../transform.js"
|
4
|
+
import type { Ty } from "../Ty.js"
|
5
|
+
|
6
|
+
export { Date_ as Date }
|
7
|
+
const Date_: Ty<Date, never, true> = transform(
|
8
|
+
object({
|
9
|
+
year: number,
|
10
|
+
month: number`Zero-based (0 for January, 11 for December).`,
|
11
|
+
day: number`Day of the month (1-31). Ensure within available range for the specified month.`,
|
12
|
+
hour: number`Zero-based (0-23)`,
|
13
|
+
minute: number`Zero-based (0-59)`,
|
14
|
+
second: number`Zero-based (0-59)`,
|
15
|
+
millisecond: number`Zero-based (0-999)`,
|
16
|
+
}),
|
17
|
+
({ year, month, day, hour, minute, second, millisecond }) =>
|
18
|
+
new Date(year, month, day, hour, minute, second, millisecond),
|
19
|
+
)
|
package/src/types/std/Option.ts
CHANGED
@@ -1,15 +1,14 @@
|
|
1
|
-
import { none } from "../primitives.js"
|
2
1
|
import { taggedUnion } from "../taggedUnion.js"
|
3
2
|
import type { Ty } from "../Ty.js"
|
4
3
|
|
5
|
-
export function
|
4
|
+
export function Option<S extends Ty>(Some: S): ReturnType<
|
6
5
|
typeof taggedUnion<{
|
7
6
|
Some: S
|
8
|
-
None:
|
7
|
+
None: null
|
9
8
|
}>
|
10
9
|
> {
|
11
10
|
return taggedUnion({
|
12
11
|
Some,
|
13
|
-
None:
|
12
|
+
None: null,
|
14
13
|
})`An option of specified \`some\` type.`
|
15
14
|
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { object } from "../object.js"
|
2
|
+
import { number } from "../primitives.js"
|
3
|
+
import { transform } from "../transform.js"
|
4
|
+
import type { Ty } from "../Ty.js"
|
5
|
+
|
6
|
+
export const Rgb: Ty<
|
7
|
+
{
|
8
|
+
r: number
|
9
|
+
g: number
|
10
|
+
b: number
|
11
|
+
},
|
12
|
+
never,
|
13
|
+
true
|
14
|
+
> = object({
|
15
|
+
r: number`Between 0 and 255`,
|
16
|
+
g: number`Between 0 and 255`,
|
17
|
+
b: number`Between 0 and 255`,
|
18
|
+
})
|
19
|
+
|
20
|
+
export const Hex: Ty<string, never, true> = transform(
|
21
|
+
Rgb,
|
22
|
+
({ r, g, b }) => `${toHex(r)}${toHex(g)}${toHex(b)}"`,
|
23
|
+
)
|
24
|
+
|
25
|
+
function toHex(value: number): string {
|
26
|
+
return value.toString(16).padStart(2, "0")
|
27
|
+
}
|
package/src/types/std/mod.ts
CHANGED
package/src/types/taggedUnion.ts
CHANGED
@@ -1,27 +1,30 @@
|
|
1
1
|
import { Ty } from "./Ty.js"
|
2
2
|
|
3
|
-
export function taggedUnion<M extends Record<string, Ty>>(members: M): Ty<
|
4
|
-
{
|
5
|
-
|
6
|
-
|
7
|
-
value: M[K]["T"]
|
8
|
-
}
|
9
|
-
}[keyof M],
|
10
|
-
M[keyof M]["P"]
|
3
|
+
export function taggedUnion<M extends Record<string, Ty | null>>(members: M): Ty<
|
4
|
+
{ [K in keyof M]: M[K] extends Ty ? { type: K; value: M[K]["T"] } : { type: K } }[keyof M],
|
5
|
+
Exclude<M[keyof M], null>["P"],
|
6
|
+
false
|
11
7
|
> {
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
type:
|
18
|
-
|
19
|
-
|
8
|
+
const entries = Object.entries(members)
|
9
|
+
return Ty(
|
10
|
+
(subschema) => ({
|
11
|
+
discriminator: "type",
|
12
|
+
anyOf: entries.map(([k, v]) => ({
|
13
|
+
type: "object",
|
14
|
+
properties: {
|
15
|
+
type: {
|
16
|
+
type: "string",
|
17
|
+
const: k,
|
18
|
+
},
|
19
|
+
...(v === null ? {} : {
|
20
|
+
value: subschema(v),
|
21
|
+
}),
|
20
22
|
},
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
required: ["type", ...v === null ? [] : ["value"]],
|
24
|
+
additionalProperties: false,
|
25
|
+
})),
|
26
|
+
}),
|
27
|
+
false,
|
28
|
+
(v) => members[v.type]![""].transform(v),
|
29
|
+
)
|
27
30
|
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { Ty } from "./Ty.js"
|
2
|
+
|
3
|
+
export function tuple<F extends Ty[]>(
|
4
|
+
...elements: F
|
5
|
+
): Ty<{ [K in keyof F]: F[K]["T"] }, F[number]["P"], true> {
|
6
|
+
const { length } = elements
|
7
|
+
return Ty(
|
8
|
+
(ref) => ({
|
9
|
+
type: "object",
|
10
|
+
properties: Object.fromEntries(Array.from({ length }, (_0, i) => [i, ref(elements[i]!)])),
|
11
|
+
additionalProperties: false,
|
12
|
+
required: Object.keys(Array.from({ length }, (_0, i) => i)),
|
13
|
+
}),
|
14
|
+
true,
|
15
|
+
(v) => Array.from({ length }, (_0, i) => elements[i]![""].transform(v[i])) as never,
|
16
|
+
)
|
17
|
+
}
|
package/esm/oai.d.ts
DELETED
@@ -1,5 +0,0 @@
|
|
1
|
-
import type Openai from "openai";
|
2
|
-
export type JsonSchema = Openai.ResponseFormatJSONSchema["json_schema"];
|
3
|
-
export type ChatCompletion = Openai.Chat.ChatCompletion;
|
4
|
-
export type ChatCompletionChoice = Openai.Chat.Completions.ChatCompletion.Choice;
|
5
|
-
//# sourceMappingURL=oai.d.ts.map
|
package/esm/oai.d.ts.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"oai.d.ts","sourceRoot":"","sources":["../src/oai.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAEhC,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,aAAa,CAAC,CAAA;AACvE,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,cAAc,CAAA;AACvD,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,MAAM,CAAA"}
|
package/esm/oai.js
DELETED
package/esm/oai.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"oai.js","sourceRoot":"","sources":["../src/oai.ts"],"names":[],"mappings":""}
|