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.
Files changed (121) hide show
  1. package/README.md +46 -40
  2. package/esm/ResponseFormat.d.ts +8 -8
  3. package/esm/ResponseFormat.d.ts.map +1 -1
  4. package/esm/ResponseFormat.js +4 -7
  5. package/esm/ResponseFormat.js.map +1 -1
  6. package/esm/Tool.d.ts +2 -2
  7. package/esm/Tool.d.ts.map +1 -1
  8. package/esm/Tool.js +2 -2
  9. package/esm/Tool.js.map +1 -1
  10. package/esm/types/Ty.d.ts +26 -23
  11. package/esm/types/Ty.d.ts.map +1 -1
  12. package/esm/types/Ty.js +29 -12
  13. package/esm/types/Ty.js.map +1 -1
  14. package/esm/types/array.d.ts +1 -1
  15. package/esm/types/array.d.ts.map +1 -1
  16. package/esm/types/array.js +2 -3
  17. package/esm/types/array.js.map +1 -1
  18. package/esm/types/constantUnion.d.ts +1 -1
  19. package/esm/types/constantUnion.d.ts.map +1 -1
  20. package/esm/types/constantUnion.js +16 -19
  21. package/esm/types/constantUnion.js.map +1 -1
  22. package/esm/types/mod.d.ts +4 -5
  23. package/esm/types/mod.d.ts.map +1 -1
  24. package/esm/types/mod.js +4 -5
  25. package/esm/types/mod.js.map +1 -1
  26. package/esm/types/object.d.ts +4 -5
  27. package/esm/types/object.d.ts.map +1 -1
  28. package/esm/types/object.js +3 -4
  29. package/esm/types/object.js.map +1 -1
  30. package/esm/types/primitives.d.ts +6 -0
  31. package/esm/types/primitives.d.ts.map +1 -0
  32. package/esm/types/primitives.js +14 -0
  33. package/esm/types/primitives.js.map +1 -0
  34. package/esm/types/std/Date.d.ts +4 -0
  35. package/esm/types/std/Date.d.ts.map +1 -0
  36. package/esm/types/std/Date.js +14 -0
  37. package/esm/types/std/Date.js.map +1 -0
  38. package/esm/types/std/Option.d.ts +3 -2
  39. package/esm/types/std/Option.d.ts.map +1 -1
  40. package/esm/types/std/Option.js +2 -2
  41. package/esm/types/std/Option.js.map +1 -1
  42. package/esm/types/std/Wrapper.d.ts +0 -1
  43. package/esm/types/std/Wrapper.d.ts.map +1 -1
  44. package/esm/types/std/Wrapper.js.map +1 -1
  45. package/esm/types/std/color.d.ts +8 -0
  46. package/esm/types/std/color.d.ts.map +1 -0
  47. package/esm/types/std/color.js +13 -0
  48. package/esm/types/std/color.js.map +1 -0
  49. package/esm/types/std/mod.d.ts +2 -0
  50. package/esm/types/std/mod.d.ts.map +1 -1
  51. package/esm/types/std/mod.js +2 -0
  52. package/esm/types/std/mod.js.map +1 -1
  53. package/esm/types/taggedUnion.d.ts +2 -2
  54. package/esm/types/taggedUnion.d.ts.map +1 -1
  55. package/esm/types/taggedUnion.js +20 -15
  56. package/esm/types/taggedUnion.js.map +1 -1
  57. package/esm/types/transform.d.ts +3 -0
  58. package/esm/types/transform.d.ts.map +1 -0
  59. package/esm/types/transform.js +5 -0
  60. package/esm/types/transform.js.map +1 -0
  61. package/esm/types/tuple.d.ts +5 -0
  62. package/esm/types/tuple.d.ts.map +1 -0
  63. package/esm/types/tuple.js +11 -0
  64. package/esm/types/tuple.js.map +1 -0
  65. package/esm/util/recombine.d.ts +2 -0
  66. package/esm/util/recombine.d.ts.map +1 -0
  67. package/esm/util/recombine.js +4 -0
  68. package/esm/util/recombine.js.map +1 -0
  69. package/package.json +2 -2
  70. package/src/ResponseFormat.ts +14 -17
  71. package/src/Tool.ts +5 -5
  72. package/src/types/Ty.ts +75 -41
  73. package/src/types/array.ts +9 -6
  74. package/src/types/constantUnion.ts +17 -20
  75. package/src/types/mod.ts +4 -5
  76. package/src/types/object.ts +17 -13
  77. package/src/types/primitives.ts +17 -0
  78. package/src/types/std/Date.ts +19 -0
  79. package/src/types/std/Option.ts +3 -3
  80. package/src/types/std/Wrapper.ts +0 -2
  81. package/src/types/std/color.ts +27 -0
  82. package/src/types/std/mod.ts +2 -0
  83. package/src/types/taggedUnion.ts +29 -17
  84. package/src/types/transform.ts +8 -0
  85. package/src/types/tuple.ts +17 -0
  86. package/src/util/recombine.ts +3 -0
  87. package/esm/oai.d.ts +0 -5
  88. package/esm/oai.d.ts.map +0 -1
  89. package/esm/oai.js +0 -2
  90. package/esm/oai.js.map +0 -1
  91. package/esm/types/Ref.d.ts +0 -4
  92. package/esm/types/Ref.d.ts.map +0 -1
  93. package/esm/types/Ref.js +0 -11
  94. package/esm/types/Ref.js.map +0 -1
  95. package/esm/types/RootTy.d.ts +0 -8
  96. package/esm/types/RootTy.d.ts.map +0 -1
  97. package/esm/types/RootTy.js +0 -19
  98. package/esm/types/RootTy.js.map +0 -1
  99. package/esm/types/constant.d.ts +0 -3
  100. package/esm/types/constant.d.ts.map +0 -1
  101. package/esm/types/constant.js +0 -8
  102. package/esm/types/constant.js.map +0 -1
  103. package/esm/types/leaves.d.ts +0 -6
  104. package/esm/types/leaves.d.ts.map +0 -1
  105. package/esm/types/leaves.js +0 -18
  106. package/esm/types/leaves.js.map +0 -1
  107. package/esm/util/phantoms.d.ts +0 -2
  108. package/esm/util/phantoms.d.ts.map +0 -1
  109. package/esm/util/phantoms.js +0 -4
  110. package/esm/util/phantoms.js.map +0 -1
  111. package/esm/util/recombineTaggedTemplateArgs.d.ts +0 -2
  112. package/esm/util/recombineTaggedTemplateArgs.d.ts.map +0 -1
  113. package/esm/util/recombineTaggedTemplateArgs.js +0 -4
  114. package/esm/util/recombineTaggedTemplateArgs.js.map +0 -1
  115. package/src/oai.ts +0 -5
  116. package/src/types/Ref.ts +0 -16
  117. package/src/types/RootTy.ts +0 -40
  118. package/src/types/constant.ts +0 -8
  119. package/src/types/leaves.ts +0 -21
  120. package/src/util/phantoms.ts +0 -3
  121. 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 option<S extends Ty>(Some: S): ReturnType<typeof taggedUnion<{
4
+ export declare function Option<S extends Ty>(Some: S): ReturnType<typeof taggedUnion<{
4
5
  Some: S;
5
- None: Ty<null, never>;
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":"AACA,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,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;CACtB,CAAC,CACH,CAKA"}
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"}
@@ -1,6 +1,6 @@
1
- import { none } from "../leaves.js";
1
+ import { none } from "../primitives.js";
2
2
  import { taggedUnion } from "../taggedUnion.js";
3
- export function option(Some) {
3
+ export function Option(Some) {
4
4
  return taggedUnion({
5
5
  Some,
6
6
  None: none,
@@ -1 +1 @@
1
- {"version":3,"file":"Option.js","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AACnC,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
+ {"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,6 +1,5 @@
1
1
  import type { Ty } from "../mod.js";
2
2
  import { object } from "../object.js";
3
- export type Wrapper<X extends Ty = Ty> = ReturnType<typeof Wrapper<X>>;
4
3
  export declare function Wrapper<X extends Ty>(value: X): ReturnType<typeof object<{
5
4
  value: X;
6
5
  }>>;
@@ -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,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,EAAE,GAAG,EAAE,IAAI,UAAU,CAAC,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;AAEtE,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
+ {"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;AAIrC,MAAM,UAAU,OAAO,CAAe,KAAQ;IAC5C,OAAO,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAA;AAC1B,CAAC"}
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,8 @@
1
+ import type { Ty } from "../Ty.js";
2
+ export declare const Rgb: Ty<{
3
+ r: number;
4
+ g: number;
5
+ b: number;
6
+ }, never, true>;
7
+ export declare const Hex: Ty<string, never, true>;
8
+ //# sourceMappingURL=color.d.ts.map
@@ -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"}
@@ -1,3 +1,5 @@
1
+ export * from "./color.js";
2
+ export * from "./Date.js";
1
3
  export * from "./Option.js";
2
4
  export * from "./Wrapper.js";
3
5
  //# sourceMappingURL=mod.d.ts.map
@@ -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"}
@@ -1,3 +1,5 @@
1
+ export * from "./color.js";
2
+ export * from "./Date.js";
1
3
  export * from "./Option.js";
2
4
  export * from "./Wrapper.js";
3
5
  //# sourceMappingURL=mod.js.map
@@ -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][Ty.T];
5
+ value: M[K]["T"];
6
6
  };
7
- }[keyof M], M[keyof M][Ty.P]>;
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,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,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,EAAE,CAAC,CAAC,CAAC,CAAA;KAClB;CACF,CAAC,MAAM,CAAC,CAAC,EACV,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CACjB,CAiBA"}
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"}
@@ -1,20 +1,25 @@
1
1
  import { Ty } from "./Ty.js";
2
2
  export function taggedUnion(members) {
3
- return Ty((description, ref) => ({
4
- description,
5
- discriminator: "type",
6
- anyOf: Object.entries(members).map(([k, v]) => ({
7
- type: "object",
8
- properties: {
9
- type: {
10
- type: "string",
11
- const: k,
12
- },
13
- value: ref(v),
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
- required: ["type", "value"],
16
- additionalProperties: false,
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,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,UAAU,WAAW,CAA+B,OAAU;IASlE,OAAO,EAAE,CAAC,CAAC,WAAW,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QAC/B,WAAW;QACX,aAAa,EAAE,MAAM;QACrB,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAC9C,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,IAAI,EAAE;oBACJ,IAAI,EAAE,QAAQ;oBACd,KAAK,EAAE,CAAC;iBACT;gBACD,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;aACd;YACD,QAAQ,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC;YAC3B,oBAAoB,EAAE,KAAK;SAC5B,CAAC,CAAC;KACJ,CAAC,CAAC,CAAA;AACL,CAAC"}
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,3 @@
1
+ import { Ty } from "./Ty.js";
2
+ export declare function transform<T, P extends keyof any, R extends boolean, I>(ty: Ty<I, P, R>, transform: (value: I) => T): Ty<T, P, R>;
3
+ //# sourceMappingURL=transform.d.ts.map
@@ -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,5 @@
1
+ import { Ty } from "./Ty.js";
2
+ export function transform(ty, transform) {
3
+ return Ty((ref) => ref(ty), ty[""].root, (value) => transform(value));
4
+ }
5
+ //# sourceMappingURL=transform.js.map
@@ -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,5 @@
1
+ import { Ty } from "./Ty.js";
2
+ export declare function tuple<F extends Ty[]>(...elements: F): Ty<{
3
+ [K in keyof F]: F[K]["T"];
4
+ }, F[number]["P"], true>;
5
+ //# sourceMappingURL=tuple.d.ts.map
@@ -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,2 @@
1
+ export declare function recombine(strings: TemplateStringsArray, values: Array<number | string>): string;
2
+ //# sourceMappingURL=recombine.d.ts.map
@@ -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,4 @@
1
+ export function recombine(strings, values) {
2
+ return strings.reduce((acc, cur, i) => `${acc}${cur}${values[i] ?? ""}`, "");
3
+ }
4
+ //# sourceMappingURL=recombine.js.map
@@ -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.21",
4
- "description": "Compose OpenAI structured output schemas.",
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",
@@ -1,25 +1,23 @@
1
- import type { RootTy } from "./types/mod.js"
2
- import { recombineTaggedTemplateArgs } from "./util/recombineTaggedTemplateArgs.js"
3
- import type { ChatCompletion, ChatCompletionChoice, JsonSchema } from "./oai.js"
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: JsonSchema
10
- /** Parse the content of the first choice into a typed object. */
11
- parseFirstChoice(completion: ChatCompletion): T
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: RootTy<T, never>): ResponseFormat<T> {
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: RootTy<T, never>,
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} ${recombineTaggedTemplateArgs(template, quasis)}` : undefined,
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
- parseFirstChoice: (completion: ChatCompletion): T => {
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: ChatCompletionChoice) {
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 { RootTy, Schema } from "./types/mod.js"
2
- import { recombineTaggedTemplateArgs } from "./util/recombineTaggedTemplateArgs.js"
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: RootTy<T, never>): Tool<T> {
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: RootTy<T, never>, description?: string): Tool<T> {
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} ${recombineTaggedTemplateArgs(template, quasis)}` : undefined,
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 type { Ref } from "./Ref.js"
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 function Ty<T, P extends string = never>(
6
- toSchema: ToSchema,
7
- descriptions: Array<Description> = [],
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<string>>(template: TemplateStringsArray, ...placeheld: P2) =>
12
- Ty<T, P | P2[number]>(toSchema, [{ template, placeheld }, ...descriptions], applied),
13
- phantoms<{ [Ty.T]: T; [Ty.P]: P }>(),
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
- toSchema,
16
- descriptions,
17
- applied,
18
- apply: <A extends Partial<Record<P, string | number>>>(values: A) => {
19
- return Ty<T, Exclude<P, keyof A>>(toSchema, descriptions, { ...applied, ...values })
49
+ "": {
50
+ subschema,
51
+ root,
52
+ transform,
53
+ context,
54
+ applied,
20
55
  },
21
- isRoot(): this is RootTy {
22
- return false
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 interface Ty<T = any, P extends string = string> {
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 namespace Ty {
43
- export type T = typeof T
44
- export declare const T: unique symbol
45
- export type P = typeof P
46
- export declare const P: unique symbol
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
- export type ToSchema = (description: string | undefined, ref: Ref) => Schema
50
- export type Schema = Record<string, unknown>
51
-
52
- export interface Description {
85
+ interface Context {
53
86
  template: TemplateStringsArray
54
- placeheld: Array<string>
87
+ placeheld: Array<keyof any>
55
88
  }
89
+ type Applied = Record<keyof any, number | string>
56
90
 
57
- export type Applied = Record<string, number | string>
91
+ export type Schema = Record<string, unknown>
@@ -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[Ty.T]>, E[Ty.P]> {
4
- return Ty((description, ref) => ({
5
- type: "array",
6
- description,
7
- items: ref(element),
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((description) => {
9
- return {
10
- anyOf: [
11
- ...strings.length
12
- ? [{
13
- type: "string",
14
- enum: strings,
15
- }]
16
- : [],
17
- ...numbers.length
18
- ? [{
19
- type: "number",
20
- enum: numbers,
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 "./Ref.js"
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"
@@ -1,15 +1,19 @@
1
- import { RootTy } from "./RootTy.js"
2
- import type { Ty } from "./Ty.js"
1
+ import { Ty } from "./Ty.js"
3
2
 
4
- export function object<F extends Record<string, Ty>>(fields: F): RootTy<
5
- { [K in keyof F]: F[K][Ty.T] },
6
- F[keyof F][Ty.P]
7
- > {
8
- return RootTy((description, ref) => ({
9
- type: "object",
10
- description,
11
- properties: Object.fromEntries(Object.entries(fields).map(([k, v]) => [k, ref(v)])),
12
- additionalProperties: false,
13
- required: Object.keys(fields),
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)