{"version":3,"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(actionParameters: Parameter[]): 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(jsonSchema: JSONSchema): 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(name, schema, requiredFields.includes(name));\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(schema.properties)) {\n          attributes.push(\n            convertJsonSchemaToParameter(propName, propSchema, requiredFields.includes(propName)),\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(schema.items.properties || {})) {\n          attributes.push(\n            convertJsonSchemaToParameter(propName, propSchema, requiredFields.includes(propName)),\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(jsonSchema: any, required: boolean): 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    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"],"mappings":";AAAA,SAAS,SAAS;AAuCX,SAAS,6BAA6B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