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.
Files changed (122) hide show
  1. package/README.md +3 -3
  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 -3
  7. package/esm/Tool.d.ts.map +1 -1
  8. package/esm/Tool.js +2 -3
  9. package/esm/Tool.js.map +1 -1
  10. package/esm/types/Ty.d.ts +21 -13
  11. package/esm/types/Ty.d.ts.map +1 -1
  12. package/esm/types/Ty.js +28 -7
  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 +1 -1
  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 +2 -1
  21. package/esm/types/constantUnion.js.map +1 -1
  22. package/esm/types/mod.d.ts +2 -3
  23. package/esm/types/mod.d.ts.map +1 -1
  24. package/esm/types/mod.js +2 -3
  25. package/esm/types/mod.js.map +1 -1
  26. package/esm/types/object.d.ts +3 -4
  27. package/esm/types/object.d.ts.map +1 -1
  28. package/esm/types/object.js +3 -3
  29. package/esm/types/object.js.map +1 -1
  30. package/esm/types/primitives.d.ts +3 -4
  31. package/esm/types/primitives.d.ts.map +1 -1
  32. package/esm/types/primitives.js +3 -12
  33. package/esm/types/primitives.js.map +1 -1
  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 +2 -2
  39. package/esm/types/std/Option.d.ts.map +1 -1
  40. package/esm/types/std/Option.js +2 -3
  41. package/esm/types/std/Option.js.map +1 -1
  42. package/esm/types/std/color.d.ts +8 -0
  43. package/esm/types/std/color.d.ts.map +1 -0
  44. package/esm/types/std/color.js +13 -0
  45. package/esm/types/std/color.js.map +1 -0
  46. package/esm/types/std/mod.d.ts +2 -1
  47. package/esm/types/std/mod.d.ts.map +1 -1
  48. package/esm/types/std/mod.js +2 -1
  49. package/esm/types/std/mod.js.map +1 -1
  50. package/esm/types/taggedUnion.d.ts +5 -3
  51. package/esm/types/taggedUnion.d.ts.map +1 -1
  52. package/esm/types/taggedUnion.js +8 -5
  53. package/esm/types/taggedUnion.js.map +1 -1
  54. package/esm/types/transform.d.ts +3 -0
  55. package/esm/types/transform.d.ts.map +1 -0
  56. package/esm/types/transform.js +5 -0
  57. package/esm/types/transform.js.map +1 -0
  58. package/esm/types/tuple.d.ts +5 -0
  59. package/esm/types/tuple.d.ts.map +1 -0
  60. package/esm/types/tuple.js +11 -0
  61. package/esm/types/tuple.js.map +1 -0
  62. package/esm/util/recombine.d.ts +2 -0
  63. package/esm/util/recombine.d.ts.map +1 -0
  64. package/esm/util/recombine.js +4 -0
  65. package/esm/util/recombine.js.map +1 -0
  66. package/esm/util/util_types.d.ts +2 -0
  67. package/esm/util/util_types.d.ts.map +1 -0
  68. package/esm/util/util_types.js +2 -0
  69. package/esm/util/util_types.js.map +1 -0
  70. package/package.json +1 -1
  71. package/src/ResponseFormat.ts +14 -17
  72. package/src/Tool.ts +5 -6
  73. package/src/types/Ty.ts +74 -32
  74. package/src/types/array.ts +9 -5
  75. package/src/types/constantUnion.ts +3 -2
  76. package/src/types/mod.ts +2 -3
  77. package/src/types/object.ts +15 -9
  78. package/src/types/primitives.ts +3 -13
  79. package/src/types/std/Date.ts +19 -0
  80. package/src/types/std/Option.ts +3 -4
  81. package/src/types/std/color.ts +27 -0
  82. package/src/types/std/mod.ts +2 -1
  83. package/src/types/taggedUnion.ts +25 -22
  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/src/util/util_types.ts +3 -0
  88. package/esm/oai.d.ts +0 -5
  89. package/esm/oai.d.ts.map +0 -1
  90. package/esm/oai.js +0 -2
  91. package/esm/oai.js.map +0 -1
  92. package/esm/types/Ref.d.ts +0 -4
  93. package/esm/types/Ref.d.ts.map +0 -1
  94. package/esm/types/Ref.js +0 -14
  95. package/esm/types/Ref.js.map +0 -1
  96. package/esm/types/RootTy.d.ts +0 -8
  97. package/esm/types/RootTy.d.ts.map +0 -1
  98. package/esm/types/RootTy.js +0 -17
  99. package/esm/types/RootTy.js.map +0 -1
  100. package/esm/types/constant.d.ts +0 -3
  101. package/esm/types/constant.d.ts.map +0 -1
  102. package/esm/types/constant.js +0 -7
  103. package/esm/types/constant.js.map +0 -1
  104. package/esm/types/std/Wrapper.d.ts +0 -6
  105. package/esm/types/std/Wrapper.d.ts.map +0 -1
  106. package/esm/types/std/Wrapper.js +0 -5
  107. package/esm/types/std/Wrapper.js.map +0 -1
  108. package/esm/util/phantoms.d.ts +0 -2
  109. package/esm/util/phantoms.d.ts.map +0 -1
  110. package/esm/util/phantoms.js +0 -4
  111. package/esm/util/phantoms.js.map +0 -1
  112. package/esm/util/recombineTaggedTemplateArgs.d.ts +0 -2
  113. package/esm/util/recombineTaggedTemplateArgs.d.ts.map +0 -1
  114. package/esm/util/recombineTaggedTemplateArgs.js +0 -4
  115. package/esm/util/recombineTaggedTemplateArgs.js.map +0 -1
  116. package/src/oai.ts +0 -5
  117. package/src/types/Ref.ts +0 -19
  118. package/src/types/RootTy.ts +0 -38
  119. package/src/types/constant.ts +0 -7
  120. package/src/types/std/Wrapper.ts +0 -6
  121. package/src/util/phantoms.ts +0 -3
  122. package/src/util/recombineTaggedTemplateArgs.ts +0 -6
@@ -1,19 +1,22 @@
1
1
  import { Ty } from "./Ty.js";
2
2
  export function taggedUnion(members) {
3
- return Ty((ref) => ({
3
+ const entries = Object.entries(members);
4
+ return Ty((subschema) => ({
4
5
  discriminator: "type",
5
- anyOf: Object.entries(members).map(([k, v]) => ({
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
- value: ref(v),
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,CAA+B,OAAU;IASlE,OAAO,EAAE,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QAClB,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;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,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"}
@@ -0,0 +1,2 @@
1
+ export type EnsureLiteralKeys<T extends Array<keyof any>> = Extract<keyof any, T[number]> extends never ? T : never;
2
+ //# sourceMappingURL=util_types.d.ts.map
@@ -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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=util_types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util_types.js","sourceRoot":"","sources":["../../src/util/util_types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "structured-outputs",
3
- "version": "0.1.0-beta.22",
3
+ "version": "0.1.0-beta.24",
4
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",
@@ -1,25 +1,23 @@
1
- import type { ChatCompletion, ChatCompletionChoice, JsonSchema } from "./oai.js"
2
- import type { RootTy } from "./types/mod.js"
3
- import { recombineTaggedTemplateArgs } from "./util/recombineTaggedTemplateArgs.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,18 +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
- /** @experimental */
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: RootTy<T, never>, description?: string): Tool<T> {
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} ${recombineTaggedTemplateArgs(template, quasis)}` : undefined,
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 { phantoms } from "../util/phantoms.js"
2
- import type { Ref } from "./Ref.js"
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 function Ty<T, P extends keyof any = never>(
6
- toSchema: ToSchema,
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, ...placeheld: P2) =>
12
- Ty<T, P | P2[number]>(toSchema, [{ template, placeheld }, ...context], applied),
13
- phantoms<{ T: T; P: P }>(),
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
- "": { toSchema, context, applied },
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>>(toSchema, context, { ...applied, ...values })
58
+ return Ty<T, Exclude<P, keyof A>, R, I>(subschema, root, transform, context, {
59
+ ...applied,
60
+ ...values,
61
+ })
18
62
  },
19
- isRoot(): this is RootTy {
20
- return false
63
+ schema(this: Ty<T, never>) {
64
+ return SubschemaFactory({})(this)
21
65
  },
22
66
  },
23
67
  )
24
68
  }
25
69
 
26
- export interface Ty<T = any, P extends keyof any = keyof any> {
27
- <P2 extends Array<keyof any>>(
28
- template: TemplateStringsArray,
29
- ...placeheld: P2
30
- ): Ty<T, P | P2[number]>
31
- T: T
32
- P: P
33
- "": {
34
- toSchema: ToSchema
35
- context: Array<Context>
36
- applied: Applied
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
- export type ToSchema = (ref: Ref) => Schema
43
- export type Schema = Record<string, unknown>
44
-
45
- export interface Context {
86
+ interface Context {
46
87
  template: TemplateStringsArray
47
- placeheld: Array<keyof any>
88
+ params: Array<keyof any>
48
89
  }
90
+ type Applied = Record<keyof any, number | string>
49
91
 
50
- export type Applied = Record<keyof any, number | string>
92
+ export type Schema = Record<string, unknown>
@@ -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((ref) => ({
5
- type: "array",
6
- items: ref(element),
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"
@@ -1,13 +1,19 @@
1
- import { RootTy } from "./RootTy.js"
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
- ): RootTy<{ [K in keyof F]: F[K]["T"] }, F[keyof F]["P"]> {
7
- return RootTy((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
- }))
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
  }
@@ -1,17 +1,7 @@
1
1
  import { Ty } from "./Ty.js"
2
2
 
3
- export const none: Ty<null, never> = Ty(() => ({
4
- type: "null",
5
- }))
3
+ export const boolean: Ty<boolean, never, false> = Ty(() => ({ type: "boolean" }), false)
6
4
 
7
- export const boolean: Ty<boolean, never> = Ty(() => ({
8
- type: "boolean",
9
- }))
5
+ export const number: Ty<number, never, false> = Ty(() => ({ type: "number" }), false)
10
6
 
11
- export const number: Ty<number, never> = Ty(() => ({
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
+ )
@@ -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 option<S extends Ty>(Some: S): ReturnType<
4
+ export function Option<S extends Ty>(Some: S): ReturnType<
6
5
  typeof taggedUnion<{
7
6
  Some: S
8
- None: Ty<null, never>
7
+ None: null
9
8
  }>
10
9
  > {
11
10
  return taggedUnion({
12
11
  Some,
13
- None: 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
+ }
@@ -1,2 +1,3 @@
1
+ export * from "./color.js"
2
+ export * from "./Date.js"
1
3
  export * from "./Option.js"
2
- export * from "./Wrapper.js"
@@ -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
- [K in keyof M]: {
6
- type: K
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
- return Ty((ref) => ({
13
- discriminator: "type",
14
- anyOf: Object.entries(members).map(([k, v]) => ({
15
- type: "object",
16
- properties: {
17
- type: {
18
- type: "string",
19
- const: k,
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
- value: ref(v),
22
- },
23
- required: ["type", "value"],
24
- additionalProperties: false,
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,8 @@
1
+ import { Ty } from "./Ty.js"
2
+
3
+ export function transform<T, P extends keyof any, R extends boolean, I>(
4
+ ty: Ty<I, P, R>,
5
+ transform: (value: I) => T,
6
+ ): Ty<T, P, R> {
7
+ return Ty<T, P, R, I>((ref) => ref(ty), ty[""].root, (value) => transform(value))
8
+ }
@@ -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
+ }
@@ -0,0 +1,3 @@
1
+ export function recombine(strings: TemplateStringsArray, values: Array<number | string>): string {
2
+ return strings.reduce((acc, cur, i) => `${acc}${cur}${values[i] ?? ""}`, "")
3
+ }
@@ -0,0 +1,3 @@
1
+ export type EnsureLiteralKeys<T extends Array<keyof any>> = Extract<keyof any, T[number]> extends
2
+ never ? T
3
+ : never
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
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=oai.js.map
package/esm/oai.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"oai.js","sourceRoot":"","sources":["../src/oai.ts"],"names":[],"mappings":""}