@xsai/generate-object 0.1.0-beta.6 → 0.1.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -6,10 +6,15 @@ interface GenerateObjectOptions<T extends Schema> extends GenerateTextOptions {
6
6
  schemaDescription?: string;
7
7
  schemaName?: string;
8
8
  }
9
- interface GenerateObjectResult<T extends Schema> extends Omit<GenerateTextResult, 'text'> {
10
- object: Infer<T>;
11
- }
12
- /** @experimental WIP */
13
- declare const generateObject: <T extends Schema>(options: GenerateObjectOptions<T>) => Promise<GenerateObjectResult<T>>;
9
+ type GenerateObjectResult<O> = GenerateTextResult & {
10
+ object: O;
11
+ };
12
+ declare function generateObject<T extends Schema>(options: GenerateObjectOptions<T> & {
13
+ output: 'array';
14
+ }): Promise<GenerateObjectResult<Array<Infer<T>>>>;
15
+ declare function generateObject<T extends Schema>(options: GenerateObjectOptions<T> & {
16
+ output: 'object';
17
+ }): Promise<GenerateObjectResult<Infer<T>>>;
18
+ declare function generateObject<T extends Schema>(options: GenerateObjectOptions<T>): Promise<GenerateObjectResult<Infer<T>>>;
14
19
 
15
20
  export { type GenerateObjectOptions, type GenerateObjectResult, generateObject };
package/dist/index.js CHANGED
@@ -1,32 +1,69 @@
1
1
  // src/index.ts
2
2
  import { generateText } from "@xsai/generate-text";
3
3
  import { toJSONSchema, validate } from "xsschema";
4
- var generateObject = async (options) => generateText({
5
- ...options,
6
- response_format: {
7
- json_schema: {
8
- description: options.schemaDescription,
9
- name: options.schemaName ?? "json_schema",
10
- schema: await toJSONSchema(options.schema),
11
- strict: true
12
- },
13
- type: "json_schema"
4
+
5
+ // src/wrap.ts
6
+ var wrap = ({ $schema, ...schema }) => ({
7
+ properties: {
8
+ elements: {
9
+ items: schema,
10
+ type: "array"
11
+ }
14
12
  },
15
- schema: void 0,
16
- schemaDescription: void 0,
17
- schemaName: void 0
18
- }).then(async ({ finishReason, messages, steps, text, toolCalls, toolResults, usage }) => {
19
- const object = await validate(options.schema, JSON.parse(text));
20
- return {
21
- finishReason,
22
- messages,
23
- object,
24
- steps,
25
- toolCalls,
26
- toolResults,
27
- usage
28
- };
13
+ required: ["elements"],
14
+ type: "object"
29
15
  });
16
+
17
+ // src/index.ts
18
+ async function generateObject(options) {
19
+ const { schema: schemaValidator } = options;
20
+ let schema = await toJSONSchema(schemaValidator);
21
+ if (options.output === "array")
22
+ schema = wrap(schema);
23
+ return generateText({
24
+ ...options,
25
+ response_format: {
26
+ json_schema: {
27
+ description: options.schemaDescription,
28
+ name: options.schemaName ?? "json_schema",
29
+ schema,
30
+ strict: true
31
+ },
32
+ type: "json_schema"
33
+ },
34
+ schema: void 0,
35
+ // Remove schema from options
36
+ schemaDescription: void 0,
37
+ // Remove schemaDescription from options
38
+ schemaName: void 0
39
+ // Remove schemaName from options
40
+ }).then(async ({ finishReason, messages, steps, text, toolCalls, toolResults, usage }) => {
41
+ const json = JSON.parse(text);
42
+ if (options.output === "array") {
43
+ return {
44
+ finishReason,
45
+ messages,
46
+ object: await Promise.all(json.elements.map(async (element) => validate(schemaValidator, element))),
47
+ steps,
48
+ text,
49
+ toolCalls,
50
+ toolResults,
51
+ usage
52
+ };
53
+ } else {
54
+ return {
55
+ finishReason,
56
+ messages,
57
+ object: await validate(options.schema, json),
58
+ steps,
59
+ text,
60
+ toolCalls,
61
+ toolResults,
62
+ usage
63
+ };
64
+ }
65
+ });
66
+ }
30
67
  export {
31
68
  generateObject
32
69
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/generate-object",
3
3
  "type": "module",
4
- "version": "0.1.0-beta.6",
4
+ "version": "0.1.0-beta.8",
5
5
  "description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",