ai 6.0.0-beta.69 → 6.0.0-beta.70

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # ai
2
2
 
3
+ ## 6.0.0-beta.70
4
+
5
+ ### Patch Changes
6
+
7
+ - d7bae86: feat(ai): add Output.choice()
8
+
3
9
  ## 6.0.0-beta.69
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -672,12 +672,14 @@ interface Output<OUTPUT = any, PARTIAL = any> {
672
672
  }
673
673
  /**
674
674
  * Output specification for text generation.
675
+ * This is the default output mode that generates plain text.
675
676
  *
676
677
  * @returns An output specification for generating text.
677
678
  */
678
679
  declare const text: () => Output<string, string>;
679
680
  /**
680
681
  * Output specification for typed object generation using schemas.
682
+ * When the model generates a text response, it will return an object that matches the schema.
681
683
  *
682
684
  * @param schema - The schema of the object to generate.
683
685
  *
@@ -686,18 +688,37 @@ declare const text: () => Output<string, string>;
686
688
  declare const object: <OUTPUT>({ schema: inputSchema, }: {
687
689
  schema: FlexibleSchema<OUTPUT>;
688
690
  }) => Output<OUTPUT, DeepPartial<OUTPUT>>;
691
+ /**
692
+ * Array output specification for text generation.
693
+ * When the model generates a text response, it will return an array of elements.
694
+ *
695
+ * @param element - The schema of the element to generate.
696
+ * @returns An output specification for generating an array of elements.
697
+ */
689
698
  declare const array: <ELEMENT>({ element: inputElementSchema, }: {
690
699
  element: FlexibleSchema<ELEMENT>;
691
700
  }) => Output<Array<ELEMENT>, Array<ELEMENT>>;
701
+ /**
702
+ * Choice output specification for text generation.
703
+ * When the model generates a text response, it will return a one of the choice options.
704
+ *
705
+ * @param options - The options to choose from.
706
+ * @returns An output specification for generating a choice.
707
+ */
708
+ declare const choice: <ELEMENT extends string>({ options: choiceOptions, }: {
709
+ options: Array<ELEMENT>;
710
+ }) => Output<ELEMENT, ELEMENT>;
692
711
 
693
712
  type output_Output<OUTPUT = any, PARTIAL = any> = Output<OUTPUT, PARTIAL>;
694
713
  declare const output_array: typeof array;
714
+ declare const output_choice: typeof choice;
695
715
  declare const output_object: typeof object;
696
716
  declare const output_text: typeof text;
697
717
  declare namespace output {
698
718
  export {
699
719
  output_Output as Output,
700
720
  output_array as array,
721
+ output_choice as choice,
701
722
  output_object as object,
702
723
  output_text as text,
703
724
  };
package/dist/index.d.ts CHANGED
@@ -672,12 +672,14 @@ interface Output<OUTPUT = any, PARTIAL = any> {
672
672
  }
673
673
  /**
674
674
  * Output specification for text generation.
675
+ * This is the default output mode that generates plain text.
675
676
  *
676
677
  * @returns An output specification for generating text.
677
678
  */
678
679
  declare const text: () => Output<string, string>;
679
680
  /**
680
681
  * Output specification for typed object generation using schemas.
682
+ * When the model generates a text response, it will return an object that matches the schema.
681
683
  *
682
684
  * @param schema - The schema of the object to generate.
683
685
  *
@@ -686,18 +688,37 @@ declare const text: () => Output<string, string>;
686
688
  declare const object: <OUTPUT>({ schema: inputSchema, }: {
687
689
  schema: FlexibleSchema<OUTPUT>;
688
690
  }) => Output<OUTPUT, DeepPartial<OUTPUT>>;
691
+ /**
692
+ * Array output specification for text generation.
693
+ * When the model generates a text response, it will return an array of elements.
694
+ *
695
+ * @param element - The schema of the element to generate.
696
+ * @returns An output specification for generating an array of elements.
697
+ */
689
698
  declare const array: <ELEMENT>({ element: inputElementSchema, }: {
690
699
  element: FlexibleSchema<ELEMENT>;
691
700
  }) => Output<Array<ELEMENT>, Array<ELEMENT>>;
701
+ /**
702
+ * Choice output specification for text generation.
703
+ * When the model generates a text response, it will return a one of the choice options.
704
+ *
705
+ * @param options - The options to choose from.
706
+ * @returns An output specification for generating a choice.
707
+ */
708
+ declare const choice: <ELEMENT extends string>({ options: choiceOptions, }: {
709
+ options: Array<ELEMENT>;
710
+ }) => Output<ELEMENT, ELEMENT>;
692
711
 
693
712
  type output_Output<OUTPUT = any, PARTIAL = any> = Output<OUTPUT, PARTIAL>;
694
713
  declare const output_array: typeof array;
714
+ declare const output_choice: typeof choice;
695
715
  declare const output_object: typeof object;
696
716
  declare const output_text: typeof text;
697
717
  declare namespace output {
698
718
  export {
699
719
  output_Output as Output,
700
720
  output_array as array,
721
+ output_choice as choice,
701
722
  output_object as object,
702
723
  output_text as text,
703
724
  };
package/dist/index.js CHANGED
@@ -876,7 +876,7 @@ function detectMediaType({
876
876
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
877
877
 
878
878
  // src/version.ts
879
- var VERSION = true ? "6.0.0-beta.69" : "0.0.0-test";
879
+ var VERSION = true ? "6.0.0-beta.70" : "0.0.0-test";
880
880
 
881
881
  // src/util/download/download.ts
882
882
  var download = async ({ url }) => {
@@ -9120,6 +9120,7 @@ var DefaultSpeechResult = class {
9120
9120
  var output_exports = {};
9121
9121
  __export(output_exports, {
9122
9122
  array: () => array,
9123
+ choice: () => choice,
9123
9124
  object: () => object,
9124
9125
  text: () => text
9125
9126
  });
@@ -9201,7 +9202,7 @@ var array = ({
9201
9202
  const elementSchema = (0, import_provider_utils29.asSchema)(inputElementSchema);
9202
9203
  return {
9203
9204
  type: "object",
9204
- // returns a JSON schema that describes an array of elements:
9205
+ // JSON schema that describes an array of elements:
9205
9206
  responseFormat: (0, import_provider_utils29.resolve)(elementSchema.jsonSchema).then((jsonSchema3) => {
9206
9207
  const { $schema, ...itemSchema } = jsonSchema3;
9207
9208
  return {
@@ -9295,6 +9296,82 @@ var array = ({
9295
9296
  }
9296
9297
  };
9297
9298
  };
9299
+ var choice = ({
9300
+ options: choiceOptions
9301
+ }) => {
9302
+ return {
9303
+ type: "object",
9304
+ // JSON schema that describes an enumeration:
9305
+ responseFormat: Promise.resolve({
9306
+ type: "json",
9307
+ schema: {
9308
+ $schema: "http://json-schema.org/draft-07/schema#",
9309
+ type: "object",
9310
+ properties: {
9311
+ result: { type: "string", enum: choiceOptions }
9312
+ },
9313
+ required: ["result"],
9314
+ additionalProperties: false
9315
+ }
9316
+ }),
9317
+ async parseOutput({ text: text2 }, context) {
9318
+ const parseResult = await (0, import_provider_utils29.safeParseJSON)({ text: text2 });
9319
+ if (!parseResult.success) {
9320
+ throw new NoObjectGeneratedError({
9321
+ message: "No object generated: could not parse the response.",
9322
+ cause: parseResult.error,
9323
+ text: text2,
9324
+ response: context.response,
9325
+ usage: context.usage,
9326
+ finishReason: context.finishReason
9327
+ });
9328
+ }
9329
+ const outerValue = parseResult.value;
9330
+ if (outerValue == null || typeof outerValue !== "object" || !("result" in outerValue) || typeof outerValue.result !== "string" || !choiceOptions.includes(outerValue.result)) {
9331
+ throw new NoObjectGeneratedError({
9332
+ message: "No object generated: response did not match schema.",
9333
+ cause: new import_provider28.TypeValidationError({
9334
+ value: outerValue,
9335
+ cause: "response must be an object that contains a choice value."
9336
+ }),
9337
+ text: text2,
9338
+ response: context.response,
9339
+ usage: context.usage,
9340
+ finishReason: context.finishReason
9341
+ });
9342
+ }
9343
+ return outerValue.result;
9344
+ },
9345
+ async parsePartial({ text: text2 }) {
9346
+ const result = await parsePartialJson(text2);
9347
+ switch (result.state) {
9348
+ case "failed-parse":
9349
+ case "undefined-input": {
9350
+ return void 0;
9351
+ }
9352
+ case "repaired-parse":
9353
+ case "successful-parse": {
9354
+ const outerValue = result.value;
9355
+ if (outerValue == null || typeof outerValue !== "object" || !("result" in outerValue) || typeof outerValue.result !== "string") {
9356
+ return void 0;
9357
+ }
9358
+ const potentialMatches = choiceOptions.filter(
9359
+ (choiceOption) => choiceOption.startsWith(outerValue.result)
9360
+ );
9361
+ if (result.state === "successful-parse") {
9362
+ return potentialMatches.includes(outerValue.result) ? { partial: outerValue.result } : void 0;
9363
+ } else {
9364
+ return potentialMatches.length === 1 ? { partial: potentialMatches[0] } : void 0;
9365
+ }
9366
+ }
9367
+ default: {
9368
+ const _exhaustiveCheck = result.state;
9369
+ throw new Error(`Unsupported parse state: ${_exhaustiveCheck}`);
9370
+ }
9371
+ }
9372
+ }
9373
+ };
9374
+ };
9298
9375
 
9299
9376
  // src/generate-text/prune-messages.ts
9300
9377
  function pruneMessages({