{"version":3,"file":"json-schema.cjs","names":["z"],"sources":["../../src/utils/json-schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n  type: \"string\";\n  description?: string;\n  enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n  type: \"number\";\n  description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n  type: \"boolean\";\n  description?: string;\n};\n\nexport type JSONSchemaObject = {\n  type: \"object\";\n  properties?: Record<string, JSONSchema>;\n  required?: string[];\n  description?: string;\n};\n\nexport type JSONSchemaArray = {\n  type: \"array\";\n  items: JSONSchema;\n  description?: string;\n};\n\nexport type JSONSchema =\n  | JSONSchemaString\n  | JSONSchemaNumber\n  | JSONSchemaBoolean\n  | JSONSchemaObject\n  | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(\n  actionParameters: Parameter[],\n): JSONSchema {\n  // Create the parameters object based on the argumentAnnotations\n  let parameters: { [key: string]: any } = {};\n  for (let parameter of actionParameters || []) {\n    parameters[parameter.name] = convertAttribute(parameter);\n  }\n\n  let requiredParameterNames: string[] = [];\n  for (let arg of actionParameters || []) {\n    if (arg.required !== false) {\n      requiredParameterNames.push(arg.name);\n    }\n  }\n\n  // Create the ChatCompletionFunctions object\n  return {\n    type: \"object\",\n    properties: parameters,\n    required: requiredParameterNames,\n  };\n}\n\n// Convert JSONSchema to Parameter[]\nexport function jsonSchemaToActionParameters(\n  jsonSchema: JSONSchema,\n): Parameter[] {\n  if (jsonSchema.type !== \"object\" || !jsonSchema.properties) {\n    return [];\n  }\n\n  const parameters: Parameter[] = [];\n  const requiredFields = jsonSchema.required || [];\n\n  for (const [name, schema] of Object.entries(jsonSchema.properties)) {\n    const parameter = convertJsonSchemaToParameter(\n      name,\n      schema,\n      requiredFields.includes(name),\n    );\n    parameters.push(parameter);\n  }\n\n  return parameters;\n}\n\n// Convert JSONSchema property to Parameter\nfunction convertJsonSchemaToParameter(\n  name: string,\n  schema: JSONSchema,\n  isRequired: boolean,\n): Parameter {\n  const baseParameter: Parameter = {\n    name,\n    description: schema.description,\n  };\n\n  if (!isRequired) {\n    baseParameter.required = false;\n  }\n\n  switch (schema.type) {\n    case \"string\":\n      return {\n        ...baseParameter,\n        type: \"string\",\n        ...(schema.enum && { enum: schema.enum }),\n      };\n    case \"number\":\n    case \"boolean\":\n      return {\n        ...baseParameter,\n        type: schema.type,\n      };\n    case \"object\":\n      if (schema.properties) {\n        const attributes: Parameter[] = [];\n        const requiredFields = schema.required || [];\n\n        for (const [propName, propSchema] of Object.entries(\n          schema.properties,\n        )) {\n          attributes.push(\n            convertJsonSchemaToParameter(\n              propName,\n              propSchema,\n              requiredFields.includes(propName),\n            ),\n          );\n        }\n\n        return {\n          ...baseParameter,\n          type: \"object\",\n          attributes,\n        };\n      }\n      return {\n        ...baseParameter,\n        type: \"object\",\n      };\n    case \"array\":\n      if (schema.items.type === \"object\" && \"properties\" in schema.items) {\n        const attributes: Parameter[] = [];\n        const requiredFields = schema.items.required || [];\n\n        for (const [propName, propSchema] of Object.entries(\n          schema.items.properties || {},\n        )) {\n          attributes.push(\n            convertJsonSchemaToParameter(\n              propName,\n              propSchema,\n              requiredFields.includes(propName),\n            ),\n          );\n        }\n\n        return {\n          ...baseParameter,\n          type: \"object[]\",\n          attributes,\n        };\n      } else if (schema.items.type === \"array\") {\n        throw new Error(\"Nested arrays are not supported\");\n      } else {\n        return {\n          ...baseParameter,\n          type: `${schema.items.type}[]`,\n        };\n      }\n    default:\n      return {\n        ...baseParameter,\n        type: \"string\",\n      };\n  }\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n  switch (attribute.type) {\n    case \"string\":\n      return {\n        type: \"string\",\n        description: attribute.description,\n        ...(attribute.enum && { enum: attribute.enum }),\n      };\n    case \"number\":\n    case \"boolean\":\n      return {\n        type: attribute.type,\n        description: attribute.description,\n      };\n    case \"object\":\n    case \"object[]\":\n      const properties = attribute.attributes?.reduce(\n        (acc, attr) => {\n          acc[attr.name] = convertAttribute(attr);\n          return acc;\n        },\n        {} as Record<string, any>,\n      );\n      const required = attribute.attributes\n        ?.filter((attr) => attr.required !== false)\n        .map((attr) => attr.name);\n      if (attribute.type === \"object[]\") {\n        return {\n          type: \"array\",\n          items: {\n            type: \"object\",\n            ...(properties && { properties }),\n            ...(required && required.length > 0 && { required }),\n          },\n          description: attribute.description,\n        };\n      }\n      return {\n        type: \"object\",\n        description: attribute.description,\n        ...(properties && { properties }),\n        ...(required && required.length > 0 && { required }),\n      };\n    default:\n      // Handle arrays of primitive types and undefined attribute.type\n      if (attribute.type?.endsWith(\"[]\")) {\n        const itemType = attribute.type.slice(0, -2);\n        return {\n          type: \"array\",\n          items: { type: itemType as any },\n          description: attribute.description,\n        };\n      }\n      // Fallback for undefined type or any other unexpected type\n      return {\n        type: \"string\",\n        description: attribute.description,\n      };\n  }\n}\n\nexport function convertJsonSchemaToZodSchema(\n  jsonSchema: any,\n  required: boolean,\n): z.ZodSchema {\n  if (jsonSchema.type === \"object\") {\n    const spec: { [key: string]: z.ZodSchema } = {};\n\n    if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {\n      return !required ? z.object(spec).optional() : z.object(spec);\n    }\n\n    for (const [key, value] of Object.entries(jsonSchema.properties)) {\n      spec[key] = convertJsonSchemaToZodSchema(\n        value,\n        jsonSchema.required ? jsonSchema.required.includes(key) : false,\n      );\n    }\n    let schema = z.object(spec).describe(jsonSchema.description);\n    return required ? schema : schema.optional();\n  } else if (jsonSchema.type === \"string\") {\n    if (jsonSchema.enum && jsonSchema.enum.length > 0) {\n      let schema = z\n        .enum(jsonSchema.enum as [string, ...string[]])\n        .describe(jsonSchema.description);\n      return required ? schema : schema.optional();\n    }\n    let schema = z.string().describe(jsonSchema.description);\n    return required ? schema : schema.optional();\n  } else if (jsonSchema.type === \"number\") {\n    let schema = z.number().describe(jsonSchema.description);\n    return required ? schema : schema.optional();\n  } else if (jsonSchema.type === \"boolean\") {\n    let schema = z.boolean().describe(jsonSchema.description);\n    return required ? schema : schema.optional();\n  } else if (jsonSchema.type === \"array\") {\n    let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);\n    let schema = z.array(itemSchema).describe(jsonSchema.description);\n    return required ? schema : schema.optional();\n  }\n  throw new Error(\"Invalid JSON schema\");\n}\n\nexport function getZodParameters<T extends [] | Parameter[] | undefined>(\n  parameters: T,\n): any {\n  if (!parameters) return z.object({});\n  const jsonParams = actionParametersToJsonSchema(parameters);\n  return convertJsonSchemaToZodSchema(jsonParams, true);\n}\n"],"mappings":";;;;AAuCA,SAAgB,6BACd,kBACY;CAEZ,IAAI,aAAqC,EAAE;AAC3C,MAAK,IAAI,aAAa,oBAAoB,EAAE,CAC1C,YAAW,UAAU,QAAQ,iBAAiB,UAAU;CAG1D,IAAI,yBAAmC,EAAE;AACzC,MAAK,IAAI,OAAO,oBAAoB,EAAE,CACpC,KAAI,IAAI,aAAa,MACnB,wBAAuB,KAAK,IAAI,KAAK;AAKzC,QAAO;EACL,MAAM;EACN,YAAY;EACZ,UAAU;EACX;;AAIH,SAAgB,6BACd,YACa;AACb,KAAI,WAAW,SAAS,YAAY,CAAC,WAAW,WAC9C,QAAO,EAAE;CAGX,MAAM,aAA0B,EAAE;CAClC,MAAM,iBAAiB,WAAW,YAAY,EAAE;AAEhD,MAAK,MAAM,CAAC,MAAM,WAAW,OAAO,QAAQ,WAAW,WAAW,EAAE;EAClE,MAAM,YAAY,6BAChB,MACA,QACA,eAAe,SAAS,KAAK,CAC9B;AACD,aAAW,KAAK,UAAU;;AAG5B,QAAO;;AAIT,SAAS,6BACP,MACA,QACA,YACW;CACX,MAAM,gBAA2B;EAC/B;EACA,aAAa,OAAO;EACrB;AAED,KAAI,CAAC,WACH,eAAc,WAAW;AAG3B,SAAQ,OAAO,MAAf;EACE,KAAK,SACH,QAAO;GACL,GAAG;GACH,MAAM;GACN,GAAI,OAAO,QAAQ,EAAE,MAAM,OAAO,MAAM;GACzC;EACH,KAAK;EACL,KAAK,UACH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