@ai-sdk/anthropic 4.0.0-beta.39 → 4.0.0-beta.40

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.
@@ -2888,6 +2888,152 @@ function mapAnthropicStopReason({
2888
2888
  }
2889
2889
  }
2890
2890
 
2891
+ // src/sanitize-json-schema.ts
2892
+ var SUPPORTED_STRING_FORMATS = /* @__PURE__ */ new Set([
2893
+ "date-time",
2894
+ "time",
2895
+ "date",
2896
+ "duration",
2897
+ "email",
2898
+ "hostname",
2899
+ "uri",
2900
+ "ipv4",
2901
+ "ipv6",
2902
+ "uuid"
2903
+ ]);
2904
+ var DESCRIPTION_CONSTRAINT_KEYS = [
2905
+ "minimum",
2906
+ "maximum",
2907
+ "exclusiveMinimum",
2908
+ "exclusiveMaximum",
2909
+ "multipleOf",
2910
+ "minLength",
2911
+ "maxLength",
2912
+ "pattern",
2913
+ "minItems",
2914
+ "maxItems",
2915
+ "uniqueItems",
2916
+ "minProperties",
2917
+ "maxProperties",
2918
+ "not"
2919
+ ];
2920
+ function sanitizeJsonSchema(schema) {
2921
+ return sanitizeSchema(schema);
2922
+ }
2923
+ function sanitizeDefinition(definition) {
2924
+ if (typeof definition === "boolean" || !isPlainObject(definition)) {
2925
+ return definition;
2926
+ }
2927
+ return sanitizeSchema(definition);
2928
+ }
2929
+ function sanitizeSchema(schema) {
2930
+ const result = {};
2931
+ const schemaWithDefs = schema;
2932
+ if (schema.$ref != null) {
2933
+ return { $ref: schema.$ref };
2934
+ }
2935
+ if (schema.$schema != null) {
2936
+ result.$schema = schema.$schema;
2937
+ }
2938
+ if (schema.$id != null) {
2939
+ result.$id = schema.$id;
2940
+ }
2941
+ if (schema.title != null) {
2942
+ result.title = schema.title;
2943
+ }
2944
+ if (schema.description != null) {
2945
+ result.description = schema.description;
2946
+ }
2947
+ if (schema.default !== void 0) {
2948
+ result.default = schema.default;
2949
+ }
2950
+ if (schema.const !== void 0) {
2951
+ result.const = schema.const;
2952
+ }
2953
+ if (schema.enum != null) {
2954
+ result.enum = schema.enum;
2955
+ }
2956
+ if (schema.type != null) {
2957
+ result.type = schema.type;
2958
+ }
2959
+ if (schema.anyOf != null) {
2960
+ result.anyOf = schema.anyOf.map(sanitizeDefinition);
2961
+ } else if (schema.oneOf != null) {
2962
+ result.anyOf = schema.oneOf.map(sanitizeDefinition);
2963
+ }
2964
+ if (schema.allOf != null) {
2965
+ result.allOf = schema.allOf.map(sanitizeDefinition);
2966
+ }
2967
+ if (schema.definitions != null) {
2968
+ result.definitions = Object.fromEntries(
2969
+ Object.entries(schema.definitions).map(([name, definition]) => [
2970
+ name,
2971
+ sanitizeDefinition(definition)
2972
+ ])
2973
+ );
2974
+ }
2975
+ if (schemaWithDefs.$defs != null) {
2976
+ const resultWithDefs = result;
2977
+ resultWithDefs.$defs = Object.fromEntries(
2978
+ Object.entries(schemaWithDefs.$defs).map(([name, definition]) => [
2979
+ name,
2980
+ sanitizeDefinition(definition)
2981
+ ])
2982
+ );
2983
+ }
2984
+ if (schema.type === "object" || schema.properties != null) {
2985
+ if (schema.properties != null) {
2986
+ result.properties = Object.fromEntries(
2987
+ Object.entries(schema.properties).map(([name, definition]) => [
2988
+ name,
2989
+ sanitizeDefinition(definition)
2990
+ ])
2991
+ );
2992
+ }
2993
+ result.additionalProperties = false;
2994
+ if (schema.required != null) {
2995
+ result.required = schema.required;
2996
+ }
2997
+ }
2998
+ if (schema.items != null) {
2999
+ result.items = Array.isArray(schema.items) ? schema.items.map(sanitizeDefinition) : sanitizeDefinition(schema.items);
3000
+ }
3001
+ if (typeof schema.format === "string" && SUPPORTED_STRING_FORMATS.has(schema.format)) {
3002
+ result.format = schema.format;
3003
+ }
3004
+ const constraintDescription = getConstraintDescription(schema);
3005
+ if (constraintDescription != null) {
3006
+ result.description = result.description == null ? constraintDescription : `${result.description}
3007
+ ${constraintDescription}`;
3008
+ }
3009
+ return result;
3010
+ }
3011
+ function getConstraintDescription(schema) {
3012
+ const descriptions = DESCRIPTION_CONSTRAINT_KEYS.flatMap((key) => {
3013
+ const value = schema[key];
3014
+ if (value == null || value === false) {
3015
+ return [];
3016
+ }
3017
+ return `${formatConstraintName(key)}: ${formatConstraintValue(value)}`;
3018
+ });
3019
+ if (typeof schema.format === "string" && !SUPPORTED_STRING_FORMATS.has(schema.format)) {
3020
+ descriptions.push(`format: ${schema.format}`);
3021
+ }
3022
+ return descriptions.length === 0 ? void 0 : `${descriptions.join("; ")}.`;
3023
+ }
3024
+ function formatConstraintName(key) {
3025
+ return key.replace(/[A-Z]/g, (match) => ` ${match.toLowerCase()}`);
3026
+ }
3027
+ function formatConstraintValue(value) {
3028
+ if (typeof value === "string") {
3029
+ return value;
3030
+ }
3031
+ return JSON.stringify(value);
3032
+ }
3033
+ function isPlainObject(value) {
3034
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3035
+ }
3036
+
2891
3037
  // src/anthropic-language-model.ts
2892
3038
  function createCitationSource(citation, citationDocuments, generateId2) {
2893
3039
  var _a;
@@ -3172,7 +3318,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
3172
3318
  ...useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && {
3173
3319
  format: {
3174
3320
  type: "json_schema",
3175
- schema: responseFormat.schema
3321
+ schema: sanitizeJsonSchema(responseFormat.schema)
3176
3322
  }
3177
3323
  }
3178
3324
  }