{"version":3,"sources":["../src/index.ts"],"names":["src_exports","__export","pluginSecureProxy","__toCommonJS","import_client","getPropertyValue","obj","path","current","key","getPropertiesValues","schema","properties","prop","value","pluginParams","proxy","_db","_id","params","values","embeddings","term"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,uBAAAE,IAAA,eAAAC,EAAAH,GACA,IAAAI,EAAsD,8BAyBtD,SAASC,EAAiBC,EAAaC,EAAc,CACnD,OAAOA,EACJ,MAAM,GAAG,EACT,OAAO,CAACC,EAASC,IAASD,GAAWA,EAAQC,CAAG,IAAM,OAAYD,EAAQC,CAAG,EAAI,OAAYH,CAAG,CACrG,CAEA,SAASI,EAAoBC,EAAgBC,EAAsB,CACjE,OAAOA,EACJ,IAAKC,GAASR,EAAiBM,EAAQE,CAAI,CAAC,EAC5C,OAAQC,GAAUA,IAAU,MAAS,EACrC,KAAK,IAAI,CACd,CAEO,SAASZ,EAAkBa,EAA2E,CAC3G,GAAI,CAACA,EAAa,OAAQ,MAAM,IAAI,MAAM,oDAAoD,EAC9F,GAAI,CAACA,EAAa,WAAW,gBAC3B,MAAM,IAAI,MAAM,wEAAwE,EAC1F,GAAI,CAACA,EAAa,WAAW,MAAO,MAAM,IAAI,MAAM,8DAA8D,EAElH,IAAMC,EAAQ,IAAI,aAAW,CAC3B,QAASD,EAAa,MACxB,CAAC,EAED,MAAO,CACL,KAAM,qBACN,MAAO,CACL,MAAAC,EACA,aAAAD,CACF,EAEA,MAAM,aAA2CE,EAAeC,EAAaC,EAA8B,CACzG,GAAI,CAACJ,EAAa,YAAY,UAAU,SACtC,OAGF,GAAI,CAACA,EAAa,YAAY,UAAU,WACtC,MAAM,IAAI,MAAM,yEAAyE,EAG3F,IAAMH,EAAaG,EAAa,WAAW,SAAS,WAC9CK,EAASV,EAAoBS,EAAQP,CAAU,EAEjDG,EAAa,WAAW,SAAS,SACnC,QAAQ,IAAI,wCAAwCH,EAAW,KAAK,IAAI,CAAC,KAAKQ,CAAM,EAAE,EAGxF,IAAMC,EAAa,MAAML,EAAM,mBAAmBI,EAAQL,EAAa,WAAW,KAAK,EAEvFI,EAAOJ,EAAa,WAAW,eAAe,EAAIM,CACpD,EAEA,MAAM,aAAiCJ,EAAeE,EAA6C,CAKjG,GAJIA,EAAO,OAAS,UAAYA,EAAO,OAAS,UAI5CA,GAAQ,QAAQ,MAClB,OAGF,GAAI,CAACA,EAAO,KACV,MAAM,IAAI,MAAM,sDAAsD,EAGxE,IAAMG,EAAOH,EAAO,KACdE,EAAa,MAAML,EAAM,mBAAmBM,EAAMP,EAAa,WAAW,KAAK,EAEhFI,EAAO,SACVA,EAAO,OAAS,CAGd,SAAUA,GAAQ,QAAQ,UAAYJ,EAAa,WAAW,gBAC9D,MAAOM,CACT,GAGFF,EAAO,OAAO,MAAQE,CACxB,CACF,CACF","sourcesContent":["import type { AnyOrama, SearchParams, TypedDocument, OramaPluginSync, PartialSchemaDeep } from '@orama/orama'\nimport { OramaProxy, EmbeddingModel, ChatModel } from '@oramacloud/client'\n\nexport type SecureProxyExtra = {\n  proxy: OramaProxy\n  pluginParams: SecureProxyPluginOptions\n}\n\nexport type LLMModel = ChatModel\n\nexport type SecureProxyPluginOptions = {\n  apiKey: string\n  embeddings: {\n    defaultProperty: string\n    model: EmbeddingModel\n    onInsert?: {\n      generate: boolean\n      properties: string[]\n      verbose?: boolean\n    }\n  }\n  chat?: {\n    model: LLMModel\n  }\n}\n\nfunction getPropertyValue(obj: object, path: string) {\n  return path\n    .split('.')\n    .reduce((current, key) => (current && current[key] !== undefined ? current[key] : undefined), obj)\n}\n\nfunction getPropertiesValues(schema: object, properties: string[]) {\n  return properties\n    .map((prop) => getPropertyValue(schema, prop))\n    .filter((value) => value !== undefined)\n    .join('. ')\n}\n\nexport function pluginSecureProxy(pluginParams: SecureProxyPluginOptions): OramaPluginSync<SecureProxyExtra> {\n  if (!pluginParams.apiKey) throw new Error('Missing \"apiKey\" parameter for plugin-secure-proxy')\n  if (!pluginParams.embeddings.defaultProperty)\n    throw new Error('Missing \"embeddings.defaultProperty\" parameter for plugin-secure-proxy')\n  if (!pluginParams.embeddings.model) throw new Error('Missing \"embeddings.model\" parameter for plugin-secure-proxy')\n\n  const proxy = new OramaProxy({\n    api_key: pluginParams.apiKey\n  })\n\n  return {\n    name: 'orama-secure-proxy',\n    extra: {\n      proxy,\n      pluginParams\n    },\n\n    async beforeInsert<T extends TypedDocument<any>>(_db: AnyOrama, _id: string, params: PartialSchemaDeep<T>) {\n      if (!pluginParams.embeddings?.onInsert?.generate) {\n        return\n      }\n\n      if (!pluginParams.embeddings?.onInsert?.properties) {\n        throw new Error('Missing \"embeddingsConfig.properties\" parameter for plugin-secure-proxy')\n      }\n\n      const properties = pluginParams.embeddings.onInsert.properties\n      const values = getPropertiesValues(params, properties)\n\n      if (pluginParams.embeddings.onInsert.verbose) {\n        console.log(`Generating embeddings for properties ${properties.join(', ')}: ${values}`)\n      }\n\n      const embeddings = await proxy.generateEmbeddings(values, pluginParams.embeddings.model)\n\n      params[pluginParams.embeddings.defaultProperty] = embeddings\n    },\n\n    async beforeSearch<T extends AnyOrama>(_db: AnyOrama, params: SearchParams<T, TypedDocument<any>>) {\n      if (params.mode !== 'vector' && params.mode !== 'hybrid') {\n        return\n      }\n\n      if (params?.vector?.value) {\n        return\n      }\n\n      if (!params.term) {\n        throw new Error('Neither \"term\" nor \"vector\" parameters were provided')\n      }\n\n      const term = params.term\n      const embeddings = await proxy.generateEmbeddings(term, pluginParams.embeddings.model)\n\n      if (!params.vector) {\n        params.vector = {\n          // eslint-disable-next-line\n          // @ts-ignore\n          property: params?.vector?.property ?? pluginParams.embeddings.defaultProperty,\n          value: embeddings\n        }\n      }\n\n      params.vector.value = embeddings\n    }\n  }\n}\n"]}