structured-outputs 0.1.0-beta.21 → 0.1.0-beta.23
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +46 -40
- 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 -2
- package/esm/Tool.d.ts.map +1 -1
- package/esm/Tool.js +2 -2
- package/esm/Tool.js.map +1 -1
- package/esm/types/Ty.d.ts +26 -23
- package/esm/types/Ty.d.ts.map +1 -1
- package/esm/types/Ty.js +29 -12
- 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 +2 -3
- 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 +16 -19
- package/esm/types/constantUnion.js.map +1 -1
- package/esm/types/mod.d.ts +4 -5
- package/esm/types/mod.d.ts.map +1 -1
- package/esm/types/mod.js +4 -5
- package/esm/types/mod.js.map +1 -1
- package/esm/types/object.d.ts +4 -5
- package/esm/types/object.d.ts.map +1 -1
- package/esm/types/object.js +3 -4
- package/esm/types/object.js.map +1 -1
- package/esm/types/primitives.d.ts +6 -0
- package/esm/types/primitives.d.ts.map +1 -0
- package/esm/types/primitives.js +14 -0
- package/esm/types/primitives.js.map +1 -0
- 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 +3 -2
- package/esm/types/std/Option.d.ts.map +1 -1
- package/esm/types/std/Option.js +2 -2
- package/esm/types/std/Option.js.map +1 -1
- package/esm/types/std/Wrapper.d.ts +0 -1
- package/esm/types/std/Wrapper.d.ts.map +1 -1
- package/esm/types/std/Wrapper.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 -0
- package/esm/types/std/mod.d.ts.map +1 -1
- package/esm/types/std/mod.js +2 -0
- package/esm/types/std/mod.js.map +1 -1
- package/esm/types/taggedUnion.d.ts +2 -2
- package/esm/types/taggedUnion.d.ts.map +1 -1
- package/esm/types/taggedUnion.js +20 -15
- 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/package.json +2 -2
- package/src/ResponseFormat.ts +14 -17
- package/src/Tool.ts +5 -5
- package/src/types/Ty.ts +75 -41
- package/src/types/array.ts +9 -6
- package/src/types/constantUnion.ts +17 -20
- package/src/types/mod.ts +4 -5
- package/src/types/object.ts +17 -13
- package/src/types/primitives.ts +17 -0
- package/src/types/std/Date.ts +19 -0
- package/src/types/std/Option.ts +3 -3
- package/src/types/std/Wrapper.ts +0 -2
- package/src/types/std/color.ts +27 -0
- package/src/types/std/mod.ts +2 -0
- package/src/types/taggedUnion.ts +29 -17
- package/src/types/transform.ts +8 -0
- package/src/types/tuple.ts +17 -0
- package/src/util/recombine.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 -11
- 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 -19
- 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 -8
- package/esm/types/constant.js.map +0 -1
- package/esm/types/leaves.d.ts +0 -6
- package/esm/types/leaves.d.ts.map +0 -1
- package/esm/types/leaves.js +0 -18
- package/esm/types/leaves.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 -16
- package/src/types/RootTy.ts +0 -40
- package/src/types/constant.ts +0 -8
- package/src/types/leaves.ts +0 -21
- package/src/util/phantoms.ts +0 -3
- package/src/util/recombineTaggedTemplateArgs.ts +0 -6
@@ -1,7 +1,8 @@
|
|
1
|
+
import { none } from "../primitives.js";
|
1
2
|
import { taggedUnion } from "../taggedUnion.js";
|
2
3
|
import type { Ty } from "../Ty.js";
|
3
|
-
export declare function
|
4
|
+
export declare function Option<S extends Ty>(Some: S): ReturnType<typeof taggedUnion<{
|
4
5
|
Some: S;
|
5
|
-
None:
|
6
|
+
None: typeof none;
|
6
7
|
}>>;
|
7
8
|
//# sourceMappingURL=Option.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Option.d.ts","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"Option.d.ts","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,UAAU,CAAA;AAElC,wBAAgB,MAAM,CAAC,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC,GAAG,UAAU,CACvD,OAAO,WAAW,CAAC;IACjB,IAAI,EAAE,CAAC,CAAA;IACP,IAAI,EAAE,OAAO,IAAI,CAAA;CAClB,CAAC,CACH,CAKA"}
|
package/esm/types/std/Option.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Option.js","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"Option.js","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAG/C,MAAM,UAAU,MAAM,CAAe,IAAO;IAM1C,OAAO,WAAW,CAAC;QACjB,IAAI;QACJ,IAAI,EAAE,IAAI;KACX,CAAC,CAAA,uCAAuC,CAAA;AAC3C,CAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../../src/types/std/Wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,
|
1
|
+
{"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../../src/types/std/Wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,wBAAgB,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,CAAC,GAAG,UAAU,CAAC,OAAO,MAAM,CAAC;IAAE,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC,CAEvF"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Wrapper.js","sourceRoot":"","sources":["../../../src/types/std/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;
|
1
|
+
{"version":3,"file":"Wrapper.js","sourceRoot":"","sources":["../../../src/types/std/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,MAAM,UAAU,OAAO,CAAe,KAAQ;IAC5C,OAAO,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAA;AAC1B,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"color.d.ts","sourceRoot":"","sources":["../../../src/types/std/color.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,UAAU,CAAA;AAElC,eAAO,MAAM,GAAG,EAAE,EAAE,CAClB;IACE,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;CACV,EACD,KAAK,EACL,IAAI,CAKJ,CAAA;AAEF,eAAO,MAAM,GAAG,EAAE,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAGvC,CAAA"}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { object } from "../object.js";
|
2
|
+
import { number } from "../primitives.js";
|
3
|
+
import { transform } from "../transform.js";
|
4
|
+
export const Rgb = object({
|
5
|
+
r: number `Between 0 and 255`,
|
6
|
+
g: number `Between 0 and 255`,
|
7
|
+
b: number `Between 0 and 255`,
|
8
|
+
});
|
9
|
+
export const Hex = transform(Rgb, ({ r, g, b }) => `${toHex(r)}${toHex(g)}${toHex(b)}"`);
|
10
|
+
function toHex(value) {
|
11
|
+
return value.toString(16).padStart(2, "0");
|
12
|
+
}
|
13
|
+
//# sourceMappingURL=color.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"color.js","sourceRoot":"","sources":["../../../src/types/std/color.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAG3C,MAAM,CAAC,MAAM,GAAG,GAQZ,MAAM,CAAC;IACT,CAAC,EAAE,MAAM,CAAA,mBAAmB;IAC5B,CAAC,EAAE,MAAM,CAAA,mBAAmB;IAC5B,CAAC,EAAE,MAAM,CAAA,mBAAmB;CAC7B,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,GAAG,GAA4B,SAAS,CACnD,GAAG,EACH,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,CACtD,CAAA;AAED,SAAS,KAAK,CAAC,KAAa;IAC1B,OAAO,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;AAC5C,CAAC"}
|
package/esm/types/std/mod.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../src/types/std/mod.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../src/types/std/mod.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
|
package/esm/types/std/mod.js
CHANGED
package/esm/types/std/mod.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../src/types/std/mod.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../src/types/std/mod.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
|
@@ -2,7 +2,7 @@ import { Ty } from "./Ty.js";
|
|
2
2
|
export declare function taggedUnion<M extends Record<string, Ty>>(members: M): Ty<{
|
3
3
|
[K in keyof M]: {
|
4
4
|
type: K;
|
5
|
-
value: M[K][
|
5
|
+
value: M[K]["T"];
|
6
6
|
};
|
7
|
-
}[keyof M], M[keyof M][
|
7
|
+
}[keyof M], M[keyof M]["P"], false>;
|
8
8
|
//# sourceMappingURL=taggedUnion.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"taggedUnion.d.ts","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"taggedUnion.d.ts","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,EAAE,EAAE,MAAM,SAAS,CAAA;AAEzC,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,CACvE;KACG,CAAC,IAAI,MAAM,CAAC,GAAG;QACd,IAAI,EAAE,CAAC,CAAA;QACP,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;KACjB;CACF,CAAC,MAAM,CAAC,CAAC,EACV,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EACf,KAAK,CACN,CAaA"}
|
package/esm/types/taggedUnion.js
CHANGED
@@ -1,20 +1,25 @@
|
|
1
1
|
import { Ty } from "./Ty.js";
|
2
2
|
export function taggedUnion(members) {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
const entries = Object.entries(members);
|
4
|
+
return Ty((subschema) => entries.length === 1
|
5
|
+
? variant(entries[0][0], subschema(entries[0][1]))
|
6
|
+
: {
|
7
|
+
discriminator: "type",
|
8
|
+
anyOf: entries.map(([k, v]) => variant(k, subschema(v))),
|
9
|
+
}, false, (v) => members[v.type][""].transform(v));
|
10
|
+
}
|
11
|
+
function variant(type, value) {
|
12
|
+
return {
|
13
|
+
type: "object",
|
14
|
+
properties: {
|
15
|
+
type: {
|
16
|
+
type: "string",
|
17
|
+
const: type,
|
14
18
|
},
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
value,
|
20
|
+
},
|
21
|
+
required: ["type", "value"],
|
22
|
+
additionalProperties: false,
|
23
|
+
};
|
19
24
|
}
|
20
25
|
//# sourceMappingURL=taggedUnion.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"taggedUnion.js","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"taggedUnion.js","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,EAAE,EAAE,MAAM,SAAS,CAAA;AAEzC,MAAM,UAAU,WAAW,CAA+B,OAAU;IAUlE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IACvC,OAAO,EAAE,CACP,CAAC,SAAS,EAAE,EAAE,CACZ,OAAO,CAAC,MAAM,KAAK,CAAC;QAClB,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACpD,CAAC,CAAC;YACA,aAAa,EAAE,MAAM;YACrB,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;SACzD,EACL,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;AAED,SAAS,OAAO,CAAC,IAAY,EAAE,KAAa;IAC1C,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE;YACV,IAAI,EAAE;gBACJ,IAAI,EAAE,QAAQ;gBACd,KAAK,EAAE,IAAI;aACZ;YACD,KAAK;SACN;QACD,QAAQ,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC;QAC3B,oBAAoB,EAAE,KAAK;KAC5B,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"}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "structured-outputs",
|
3
|
-
"version": "0.1.0-beta.
|
4
|
-
"description": "
|
3
|
+
"version": "0.1.0-beta.23",
|
4
|
+
"description": "A library for working with OpenAI's structured outputs.",
|
5
5
|
"repository": "github:harrysolovay/structured-outputs.git",
|
6
6
|
"license": "Apache-2.0",
|
7
7
|
"main": "./esm/mod.js",
|
package/src/ResponseFormat.ts
CHANGED
@@ -1,25 +1,23 @@
|
|
1
|
-
import type
|
2
|
-
import {
|
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,17 +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
|
-
export function Tool<T>(name: string, ty:
|
4
|
+
export function Tool<T>(name: string, ty: Ty<T, never, true>): Tool<T> {
|
5
5
|
return Tool_(name, ty)
|
6
6
|
}
|
7
7
|
|
8
|
-
function Tool_<T>(name: string, ty:
|
8
|
+
function Tool_<T>(name: string, ty: Ty<T, never, true>, description?: string): Tool<T> {
|
9
9
|
return Object.assign(
|
10
10
|
(template: TemplateStringsArray, ...quasis: Array<string>) =>
|
11
11
|
Tool_(
|
12
12
|
name,
|
13
13
|
ty,
|
14
|
-
description ? `${description} ${
|
14
|
+
description ? `${description} ${recombine(template, quasis)}` : undefined,
|
15
15
|
),
|
16
16
|
{
|
17
17
|
type: "function" as const,
|
package/src/types/Ty.ts
CHANGED
@@ -1,57 +1,91 @@
|
|
1
|
-
import
|
2
|
-
import { phantoms } from "../util/phantoms.js"
|
3
|
-
import type { RootTy } from "./RootTy.js"
|
1
|
+
import { recombine } from "../util/recombine.js"
|
4
2
|
|
5
|
-
export
|
6
|
-
|
7
|
-
|
3
|
+
export interface Ty<T = any, P extends keyof any = keyof any, R extends boolean = boolean> {
|
4
|
+
<P2 extends Array<keyof any>>(
|
5
|
+
template: TemplateStringsArray,
|
6
|
+
...placeheld: P2
|
7
|
+
): Ty<T, P | P2[number], R>
|
8
|
+
|
9
|
+
/** The native TypeScript type. The runtime value is nonexistent. */
|
10
|
+
T: T
|
11
|
+
/** The literal types of the placeheld keys. The runtime value is nonexistent. */
|
12
|
+
P: P
|
13
|
+
|
14
|
+
"": {
|
15
|
+
subschema: Subschema
|
16
|
+
root: R
|
17
|
+
transform: (value: any) => any
|
18
|
+
context: Array<Context>
|
19
|
+
applied: Applied
|
20
|
+
}
|
21
|
+
|
22
|
+
/** Inject context into that which is placeheld. */
|
23
|
+
fill: <A extends Partial<Record<P, number | string>>>(
|
24
|
+
values: A,
|
25
|
+
) => Ty<T, Exclude<P, keyof A>, R>
|
26
|
+
|
27
|
+
/** Get the corresponding JSON Schema. */
|
28
|
+
schema(this: Ty<T, never>): Schema
|
29
|
+
}
|
30
|
+
|
31
|
+
export function Ty<T, P extends keyof any, R extends boolean, I = T>(
|
32
|
+
subschema: Subschema,
|
33
|
+
root: R,
|
34
|
+
transform: (value: I) => T = (value) => value as never,
|
35
|
+
context: Array<Context> = [],
|
8
36
|
applied: Applied = {},
|
9
|
-
): Ty<T, P> {
|
37
|
+
): Ty<T, P, R> {
|
10
38
|
return Object.assign(
|
11
|
-
<P2 extends Array<
|
12
|
-
Ty<T, P | P2[number]
|
13
|
-
|
39
|
+
<P2 extends Array<keyof any>>(template: TemplateStringsArray, ...placeheld: P2) =>
|
40
|
+
Ty<T, P | P2[number], R, I>(
|
41
|
+
subschema,
|
42
|
+
root,
|
43
|
+
transform,
|
44
|
+
[{ template, placeheld }, ...context],
|
45
|
+
applied,
|
46
|
+
),
|
47
|
+
{} as { T: T; P: P },
|
14
48
|
{
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
49
|
+
"": {
|
50
|
+
subschema,
|
51
|
+
root,
|
52
|
+
transform,
|
53
|
+
context,
|
54
|
+
applied,
|
20
55
|
},
|
21
|
-
|
22
|
-
return
|
56
|
+
fill: <A extends Partial<Record<P, string | number>>>(values: A) => {
|
57
|
+
return Ty<T, Exclude<P, keyof A>, R, I>(subschema, root, transform, context, {
|
58
|
+
...applied,
|
59
|
+
...values,
|
60
|
+
})
|
61
|
+
},
|
62
|
+
schema(this: Ty<T, never>) {
|
63
|
+
return SubschemaFactory({})(this)
|
23
64
|
},
|
24
65
|
},
|
25
66
|
)
|
26
67
|
}
|
27
68
|
|
28
|
-
export
|
29
|
-
<P2 extends Array<string>>(
|
30
|
-
template: TemplateStringsArray,
|
31
|
-
...placeheld: P2
|
32
|
-
): Ty<T, P | P2[number]>
|
33
|
-
[Ty.T]: T
|
34
|
-
[Ty.P]: P
|
35
|
-
toSchema: ToSchema
|
36
|
-
descriptions: Array<Description>
|
37
|
-
applied: Applied
|
38
|
-
apply: <A extends Partial<Record<P, number | string>>>(values: A) => Ty<T, Exclude<P, keyof A>>
|
39
|
-
isRoot(): this is RootTy
|
40
|
-
}
|
69
|
+
export type Subschema = (subschema: SubschemaFactory) => Schema
|
41
70
|
|
42
|
-
export
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
71
|
+
export type SubschemaFactory = (ty: Ty) => Schema
|
72
|
+
export function SubschemaFactory(applied: Applied): SubschemaFactory {
|
73
|
+
return (ty) => {
|
74
|
+
applied = { ...applied, ...ty[""].applied }
|
75
|
+
const description = ty[""].context
|
76
|
+
.map(({ template, placeheld }) => recombine(template, placeheld.map((k) => applied[k]!)))
|
77
|
+
.join(" ")
|
78
|
+
return {
|
79
|
+
...ty[""].subschema(SubschemaFactory(applied)),
|
80
|
+
...description ? { description } : {},
|
81
|
+
}
|
82
|
+
}
|
47
83
|
}
|
48
84
|
|
49
|
-
|
50
|
-
export type Schema = Record<string, unknown>
|
51
|
-
|
52
|
-
export interface Description {
|
85
|
+
interface Context {
|
53
86
|
template: TemplateStringsArray
|
54
|
-
placeheld: Array<
|
87
|
+
placeheld: Array<keyof any>
|
55
88
|
}
|
89
|
+
type Applied = Record<keyof any, number | string>
|
56
90
|
|
57
|
-
export type
|
91
|
+
export type Schema = Record<string, unknown>
|
package/src/types/array.ts
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
import { Ty } from "./Ty.js"
|
2
2
|
|
3
|
-
export function array<E extends Ty>(element: E): Ty<Array<E[
|
4
|
-
return Ty(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
+
)
|
9
12
|
}
|
@@ -3,25 +3,22 @@ import { Ty } from "./Ty.js"
|
|
3
3
|
|
4
4
|
export function constantUnion<M extends Array<number | string>>(
|
5
5
|
...members: M
|
6
|
-
): Ty<M[number], never> {
|
6
|
+
): Ty<M[number], never, false> {
|
7
7
|
const [strings, numbers] = partition(members, (v) => typeof v === "string")
|
8
|
-
return Ty((
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
description,
|
25
|
-
}
|
26
|
-
})
|
8
|
+
return Ty(() => ({
|
9
|
+
anyOf: [
|
10
|
+
...strings.length
|
11
|
+
? [{
|
12
|
+
type: "string",
|
13
|
+
enum: strings,
|
14
|
+
}]
|
15
|
+
: [],
|
16
|
+
...numbers.length
|
17
|
+
? [{
|
18
|
+
type: "number",
|
19
|
+
enum: numbers,
|
20
|
+
}]
|
21
|
+
: [],
|
22
|
+
],
|
23
|
+
}), false)
|
27
24
|
}
|
package/src/types/mod.ts
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
export * from "./array.js"
|
2
|
-
export * from "./leaves.js"
|
3
|
-
export * from "./constant.js"
|
4
2
|
export * from "./constantUnion.js"
|
5
|
-
export * from "./std/mod.js"
|
6
3
|
export * from "./object.js"
|
7
|
-
export * from "./
|
4
|
+
export * from "./primitives.js"
|
5
|
+
export * from "./std/mod.js"
|
8
6
|
export * from "./taggedUnion.js"
|
7
|
+
export * from "./transform.js"
|
8
|
+
export * from "./tuple.js"
|
9
9
|
export * from "./Ty.js"
|
10
|
-
export * from "./RootTy.js"
|
package/src/types/object.ts
CHANGED
@@ -1,15 +1,19 @@
|
|
1
|
-
import {
|
2
|
-
import type { Ty } from "./Ty.js"
|
1
|
+
import { Ty } from "./Ty.js"
|
3
2
|
|
4
|
-
export function object<F extends Record<string, Ty>>(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
3
|
+
export function object<F extends Record<string, Ty>>(
|
4
|
+
fields: F,
|
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
|
+
)
|
15
19
|
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { Ty } from "./Ty.js"
|
2
|
+
|
3
|
+
export const none: Ty<null, never, false> = Ty(() => ({
|
4
|
+
type: "null",
|
5
|
+
}), false)
|
6
|
+
|
7
|
+
export const boolean: Ty<boolean, never, false> = Ty(() => ({
|
8
|
+
type: "boolean",
|
9
|
+
}), false)
|
10
|
+
|
11
|
+
export const number: Ty<number, never, false> = Ty(() => ({
|
12
|
+
type: "number",
|
13
|
+
}), false)
|
14
|
+
|
15
|
+
export const string: Ty<string, never, false> = Ty(() => ({
|
16
|
+
type: "string",
|
17
|
+
}), false)
|