@lyxa.ai/core 1.4.283 → 1.4.285

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/dist/index.js CHANGED
@@ -67,7 +67,6 @@ const logger_kit_1 = require("./libraries/logger-kit");
67
67
  const lyxa_fcm_service_1 = require("./libraries/notification/lyxa-fcm.service");
68
68
  const lyxa_fcm_sender_1 = require("./libraries/notification/lyxa-fcm-sender");
69
69
  const firebase_message_builder_1 = require("./libraries/notification/firebase-message.builder");
70
- const google_translate_1 = require("./libraries/google-translate");
71
70
  const llm_provider_service_1 = require("./libraries/llm/llm-provider-service");
72
71
  var EnvironmentConfigurationType_2 = require("./libraries/configuration/types/EnvironmentConfigurationType");
73
72
  Object.defineProperty(exports, "EnvironmentConfigurationType", { enumerable: true, get: function () { return EnvironmentConfigurationType_2.EnvironmentConfigurationType; } });
@@ -120,7 +119,6 @@ async function registerLibraries(options) {
120
119
  typedi_1.Container.set(logger_kit_1.LyxaLoggerKit, loggerKit);
121
120
  const firebaseInstance = new firebase_1.FirebaseService(configurationInstance);
122
121
  typedi_1.Container.set(firebase_1.FirebaseService, firebaseInstance);
123
- typedi_1.Container.set(google_translate_1.GoogleTranslateService, typedi_1.Container.get(google_translate_1.GoogleTranslateService));
124
122
  const microserviceDispatcherInstance = new dispatcher_1.MicroServiceDispatcher(secretInstance);
125
123
  typedi_1.Container.set(dispatcher_1.MicroServiceDispatcher, microserviceDispatcherInstance);
126
124
  const twilioServiceInstance = new twilio_1.TwilioService();
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,sCA0BC;AAED,8CA4GC;AAED,oCAEC;AAjLD,qFAAqF;AAErF,IAAA,0CAAqB,GAAE,CAAC;AAExB,2CAA6C;AAC7C,mCAAmC;AACnC,6DAAuF;AACvF,+CAA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jF,CAAC;AACF,CAAC;AAED,SAAgB,YAAY;IAC3B,OAAO,kBAAS,CAAC,GAAG,CAAC,0BAAc,CAAC,CAAC;AACtC,CAAC","sourcesContent":["// Apply the soft delete plugin globally before any connections or models\nimport { applySoftDeletePlugin } from './libraries/mongo/plugins/soft-delete-plugin';\n\napplySoftDeletePlugin();\n\nimport { LibraryManager } from './libraries';\nimport { Container } from 'typedi';\nimport { ConfigurationOptions, ConfigurationService } from './libraries/configuration';\nimport { LyxaLogger } from './libraries/logger';\nimport { RedisService } from './libraries/redis';\nimport { initGlobalConnection, initLyxaMongoDbConnection } from './libraries/mongo';\nimport { SecretManagerService } from './libraries/secrets';\nimport { MicroServiceDispatcher } from './libraries/dispatcher';\nimport { TwilioService } from './libraries/twilio';\nimport { FirebaseService } from './libraries/firebase';\nimport { initRabbitMQConnection, subscribeToEvent } from './libraries/event';\nimport { SubscriptionRegistry } from './libraries/event/registry/SubscriptionRegistry';\nimport { GoogleMapsService, MapService } from './libraries/map';\nimport { CachedSettingsService } from './libraries/cache/settings';\nimport { FirebaseFcmService } from './libraries/notification/firebase-fcm.service';\nimport { BullMQService } from './libraries/bullmq/bull-mq-service';\nimport { BOBSecrets, BOBService, CashSettlementService } from './libraries/cash-settlement';\nimport { EnvironmentConfigurationType } from './libraries/configuration/types/EnvironmentConfigurationType';\nimport { OsrmMapService } from './libraries/map/providers/osrm';\nimport { RabbitMQUrlMap } from './utilities/environment/rabbitmq-environments';\nimport { disconnectAllEntitiesFromInstance } from './libraries/socket/connections/instance-disconnect';\nimport { LyxaLoggerKit } from './libraries/logger-kit';\nimport { LyxaFcmService } from './libraries/notification/lyxa-fcm.service';\nimport { LyxaFcmSender } from './libraries/notification/lyxa-fcm-sender';\nimport { FirebaseMessageBuilderFactory } from './libraries/notification/firebase-message.builder';\nimport { GoogleTranslateService } from './libraries/google-translate';\nimport { LLMCompletionService, LLMProvider } from './libraries/llm/llm-provider-service';\n\nexport { ConfigurationOptions } from './libraries/configuration';\nexport { EnvironmentConfigurationType } from './libraries/configuration/types/EnvironmentConfigurationType';\nexport { initGlobalConnection } from './libraries/mongo';\nexport * from './libraries/google-translate';\n\nexport async function bootstrapCore(options?: ConfigurationOptions) {\n\ttry {\n\t\tawait registerLibraries(options);\n\t\tgetLibraries().getLogger().logger.info('Successfully bootstrapped lyxa-core.');\n\n\t\tconst gracefulShutdown = async () => {\n\t\t\tgetLibraries().getLogger().logger.info('Received shutdown signal, shutting down gracefully...');\n\t\t\tif (options?.instanceType == 'socket') await disconnectAllEntitiesFromInstance();\n\t\t\tprocess.exit();\n\t\t};\n\n\t\tprocess.on('SIGTERM', gracefulShutdown);\n\t\tprocess.on('SIGINT', gracefulShutdown);\n\t\tprocess.on('uncaughtException', err => {\n\t\t\tconsole.log('err', err);\n\t\t\tgetLibraries().getLogger().logger.error('Uncaught exception, shutting down...', err);\n\t\t\tgracefulShutdown();\n\t\t});\n\t\tprocess.on('exit', code => {\n\t\t\tgetLibraries().getLogger().logger.info(`Exiting with code: ${code}`);\n\t\t});\n\n\t\tconsole.log('Created hooks.');\n\t} catch (e) {\n\t\tconsole.error(e);\n\t}\n}\n\nexport async function registerLibraries(options?: ConfigurationOptions) {\n\t// Setup configuration.\n\tconst configurationInstance = new ConfigurationService(options);\n\tContainer.set(ConfigurationService, configurationInstance);\n\n\t// Setup redis.\n\tconst redisInstance = new RedisService(configurationInstance.redisURL);\n\tContainer.set(RedisService, redisInstance);\n\n\t// Setup secret\n\tconst secretInstance = new SecretManagerService(configurationInstance, redisInstance);\n\tContainer.set(SecretManagerService, secretInstance);\n\n\t// Setup BullMQ\n\tconst bullMQInstance = new BullMQService(redisInstance);\n\tContainer.set(BullMQService, bullMQInstance);\n\n\tconst mongoUrl = options?.mongoDbUrl\n\t\t? options.mongoDbUrl\n\t\t: await getLibraries().getSecretsService().getKey('MONGO_URL');\n\n\tawait initLyxaMongoDbConnection(mongoUrl!, options);\n\tawait initGlobalConnection(mongoUrl!, options!.environment);\n\n\tconst loggerInstance = new LyxaLogger(options!.instanceType!, mongoUrl!);\n\tContainer.set(LyxaLogger, loggerInstance);\n\n\tconst loggerKit = new LyxaLoggerKit({ instanceType: options!.instanceType! });\n\tContainer.set(LyxaLoggerKit, loggerKit);\n\n\t// Setup firebase\n\tconst firebaseInstance = new FirebaseService(configurationInstance!);\n\tContainer.set(FirebaseService, firebaseInstance);\n\n\t//Setup google translate\n\tContainer.set(GoogleTranslateService, Container.get(GoogleTranslateService));\n\n\t// Setup microservice dispatcher\n\tconst microserviceDispatcherInstance = new MicroServiceDispatcher(secretInstance);\n\tContainer.set(MicroServiceDispatcher, microserviceDispatcherInstance);\n\n\t// Setup twilio\n\tconst twilioServiceInstance = new TwilioService();\n\tContainer.set(TwilioService, twilioServiceInstance);\n\n\t// Setup LLM service\n\tconst llmCompletionService = new LLMCompletionService(LLMProvider.OPENAI);\n\tContainer.set(LLMCompletionService, llmCompletionService);\n\n\t// Setup map service\n\tconst googleMapsKey = await getLibraries().getSecretsService().getKey('GOOGLE_MAPS_API_KEY');\n\tconst googleMapService = new GoogleMapsService(redisInstance, googleMapsKey as string);\n\tconst osrmMapService = new OsrmMapService();\n\tconst mapServiceInstance = new MapService(googleMapService, osrmMapService);\n\tContainer.set(MapService, mapServiceInstance);\n\n\t// Setup cash settlement service\n\tconst bobUsername = await getLibraries().getSecretsService().getKey('BOB_USER_NAME');\n\tconst bobPassword = await getLibraries().getSecretsService().getKey('BOB_PASSWORD');\n\tconst bobURL = await getLibraries().getSecretsService().getKey('BOB_URL');\n\n\tconst bobService = new BOBService({\n\t\tBOB_USER_NAME: bobUsername,\n\t\tBOB_PASSWORD: bobPassword,\n\t\tBOB_URL: bobURL,\n\t} as BOBSecrets);\n\tconst cashSettlementServiceInstance = new CashSettlementService(bobService);\n\tContainer.set(CashSettlementService, cashSettlementServiceInstance);\n\tContainer.set(BOBService, bobService);\n\n\tconst settingsServiceInstance = new CachedSettingsService(redisInstance);\n\tContainer.set(CachedSettingsService, settingsServiceInstance);\n\n\ttry {\n\t\tconst settings = await settingsServiceInstance.getSettings();\n\t\tconst { _setTimezone } = await import('./utilities/dayjs/dayjs');\n\t\t_setTimezone(settings?.timezone || 'Asia/Beirut');\n\t\tconsole.log('Timezone initialized:', settings?.timezone || 'Asia/Beirut');\n\t} catch (error) {\n\t\tconsole.error('Failed to initialize timezone:', error);\n\t}\n\n\t// Setup fcm service\n\tconst fcmService = new FirebaseFcmService();\n\tconst builder = new FirebaseMessageBuilderFactory();\n\tconst lyxaFcmSender = new LyxaFcmSender(fcmService, builder);\n\tconst lyxaFcmService = new LyxaFcmService(lyxaFcmSender);\n\n\tContainer.set(FirebaseFcmService, fcmService);\n\tContainer.set(LyxaFcmService, lyxaFcmService);\n\n\tconsole.log('Initialized everything');\n\n\tawait initRabbitMQConnection(RabbitMQUrlMap[options?.environment ?? EnvironmentConfigurationType.DEV]);\n\n\tconst allSubscriptions = SubscriptionRegistry.getAllSubscriptions();\n\n\tconst instantiatedServices = new Map<Function, any>();\n\n\tfor (const { eventId, targetConstructor, methodName } of allSubscriptions) {\n\t\tlet instance = instantiatedServices.get(targetConstructor);\n\t\tif (!instance) {\n\t\t\tinstance = Container.get(targetConstructor as any);\n\t\t\tinstantiatedServices.set(targetConstructor, instance);\n\t\t}\n\n\t\tawait subscribeToEvent(eventId, async payload => instance[methodName](payload));\n\t}\n}\n\nexport function getLibraries(): LibraryManager {\n\treturn Container.get(LibraryManager);\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,sCA0BC;AAED,8CA4GC;AAED,oCAEC;AAjLD,qFAAqF;AAErF,IAAA,0CAAqB,GAAE,CAAC;AAExB,2CAA6C;AAC7C,mCAAmC;AACnC,6DAAuF;AACvF,+CAAgD;AAChD,6CAAiD;AACjD,6CAAoF;AACpF,iDAA2D;AAC3D,uDAAgE;AAChE,+CAAmD;AACnD,mDAAuD;A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AC;IACpD,MAAM,aAAa,GAAG,IAAI,+BAAa,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAC7D,MAAM,cAAc,GAAG,IAAI,iCAAc,CAAC,aAAa,CAAC,CAAC;IAEzD,kBAAS,CAAC,GAAG,CAAC,yCAAkB,EAAE,UAAU,CAAC,CAAC;IAC9C,kBAAS,CAAC,GAAG,CAAC,iCAAc,EAAE,cAAc,CAAC,CAAC;IAE9C,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;IAEtC,MAAM,IAAA,8BAAsB,EAAC,sCAAc,CAAC,OAAO,EAAE,WAAW,IAAI,2DAA4B,CAAC,GAAG,CAAC,CAAC,CAAC;IAEvG,MAAM,gBAAgB,GAAG,2CAAoB,CAAC,mBAAmB,EAAE,CAAC;IAEpE,MAAM,oBAAoB,GAAG,IAAI,GAAG,EAAiB,CAAC;IAEtD,KAAK,MAAM,EAAE,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,IAAI,gBAAgB,EAAE,CAAC;QAC3E,IAAI,QAAQ,GAAG,oBAAoB,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QAC3D,IAAI,CAAC,QAAQ,EAAE,CAAC;YACf,QAAQ,GAAG,kBAAS,CAAC,GAAG,CAAC,iBAAwB,CAAC,CAAC;YACnD,oBAAoB,CAAC,GAAG,CAAC,iBAAiB,EAAE,QAAQ,CAAC,CAAC;QACvD,CAAC;QAED,MAAM,IAAA,wBAAgB,EAAC,OAAO,EAAE,KAAK,EAAC,OAAO,EAAC,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;IACjF,CAAC;AACF,CAAC;AAED,SAAgB,YAAY;IAC3B,OAAO,kBAAS,CAAC,GAAG,CAAC,0BAAc,CAAC,CAAC;AACtC,CAAC","sourcesContent":["// Apply the soft delete plugin globally before any connections or models\nimport { applySoftDeletePlugin } from './libraries/mongo/plugins/soft-delete-plugin';\n\napplySoftDeletePlugin();\n\nimport { LibraryManager } from './libraries';\nimport { Container } from 'typedi';\nimport { ConfigurationOptions, ConfigurationService } from './libraries/configuration';\nimport { LyxaLogger } from './libraries/logger';\nimport { RedisService } from './libraries/redis';\nimport { initGlobalConnection, initLyxaMongoDbConnection } from './libraries/mongo';\nimport { SecretManagerService } from './libraries/secrets';\nimport { MicroServiceDispatcher } from './libraries/dispatcher';\nimport { TwilioService } from './libraries/twilio';\nimport { FirebaseService } from './libraries/firebase';\nimport { initRabbitMQConnection, subscribeToEvent } from './libraries/event';\nimport { SubscriptionRegistry } from './libraries/event/registry/SubscriptionRegistry';\nimport { GoogleMapsService, MapService } from './libraries/map';\nimport { CachedSettingsService } from './libraries/cache/settings';\nimport { FirebaseFcmService } from './libraries/notification/firebase-fcm.service';\nimport { BullMQService } from './libraries/bullmq/bull-mq-service';\nimport { BOBSecrets, BOBService, CashSettlementService } from './libraries/cash-settlement';\nimport { EnvironmentConfigurationType } from './libraries/configuration/types/EnvironmentConfigurationType';\nimport { OsrmMapService } from './libraries/map/providers/osrm';\nimport { RabbitMQUrlMap } from './utilities/environment/rabbitmq-environments';\nimport { disconnectAllEntitiesFromInstance } from './libraries/socket/connections/instance-disconnect';\nimport { LyxaLoggerKit } from './libraries/logger-kit';\nimport { LyxaFcmService } from './libraries/notification/lyxa-fcm.service';\nimport { LyxaFcmSender } from './libraries/notification/lyxa-fcm-sender';\nimport { FirebaseMessageBuilderFactory } from './libraries/notification/firebase-message.builder';\nimport { GoogleTranslateService } from './libraries/google-translate';\nimport { LLMCompletionService, LLMProvider } from './libraries/llm/llm-provider-service';\n\nexport { ConfigurationOptions } from './libraries/configuration';\nexport { EnvironmentConfigurationType } from './libraries/configuration/types/EnvironmentConfigurationType';\nexport { initGlobalConnection } from './libraries/mongo';\nexport * from './libraries/google-translate';\n\nexport async function bootstrapCore(options?: ConfigurationOptions) {\n\ttry {\n\t\tawait registerLibraries(options);\n\t\tgetLibraries().getLogger().logger.info('Successfully bootstrapped lyxa-core.');\n\n\t\tconst gracefulShutdown = async () => {\n\t\t\tgetLibraries().getLogger().logger.info('Received shutdown signal, shutting down gracefully...');\n\t\t\tif (options?.instanceType == 'socket') await disconnectAllEntitiesFromInstance();\n\t\t\tprocess.exit();\n\t\t};\n\n\t\tprocess.on('SIGTERM', gracefulShutdown);\n\t\tprocess.on('SIGINT', gracefulShutdown);\n\t\tprocess.on('uncaughtException', err => {\n\t\t\tconsole.log('err', err);\n\t\t\tgetLibraries().getLogger().logger.error('Uncaught exception, shutting down...', err);\n\t\t\tgracefulShutdown();\n\t\t});\n\t\tprocess.on('exit', code => {\n\t\t\tgetLibraries().getLogger().logger.info(`Exiting with code: ${code}`);\n\t\t});\n\n\t\tconsole.log('Created hooks.');\n\t} catch (e) {\n\t\tconsole.error(e);\n\t}\n}\n\nexport async function registerLibraries(options?: ConfigurationOptions) {\n\t// Setup configuration.\n\tconst configurationInstance = new ConfigurationService(options);\n\tContainer.set(ConfigurationService, configurationInstance);\n\n\t// Setup redis.\n\tconst redisInstance = new RedisService(configurationInstance.redisURL);\n\tContainer.set(RedisService, redisInstance);\n\n\t// Setup secret\n\tconst secretInstance = new SecretManagerService(configurationInstance, redisInstance);\n\tContainer.set(SecretManagerService, secretInstance);\n\n\t// Setup BullMQ\n\tconst bullMQInstance = new BullMQService(redisInstance);\n\tContainer.set(BullMQService, bullMQInstance);\n\n\tconst mongoUrl = options?.mongoDbUrl\n\t\t? options.mongoDbUrl\n\t\t: await getLibraries().getSecretsService().getKey('MONGO_URL');\n\n\tawait initLyxaMongoDbConnection(mongoUrl!, options);\n\tawait initGlobalConnection(mongoUrl!, options!.environment);\n\n\tconst loggerInstance = new LyxaLogger(options!.instanceType!, mongoUrl!);\n\tContainer.set(LyxaLogger, loggerInstance);\n\n\tconst loggerKit = new LyxaLoggerKit({ instanceType: options!.instanceType! });\n\tContainer.set(LyxaLoggerKit, loggerKit);\n\n\t// Setup firebase\n\tconst firebaseInstance = new FirebaseService(configurationInstance!);\n\tContainer.set(FirebaseService, firebaseInstance);\n\n\t//Setup google translate\n\t// Container.set(GoogleTranslateService, Container.get(GoogleTranslateService));\n\n\t// Setup microservice dispatcher\n\tconst microserviceDispatcherInstance = new MicroServiceDispatcher(secretInstance);\n\tContainer.set(MicroServiceDispatcher, microserviceDispatcherInstance);\n\n\t// Setup twilio\n\tconst twilioServiceInstance = new TwilioService();\n\tContainer.set(TwilioService, twilioServiceInstance);\n\n\t// Setup LLM service\n\tconst llmCompletionService = new LLMCompletionService(LLMProvider.OPENAI);\n\tContainer.set(LLMCompletionService, llmCompletionService);\n\n\t// Setup map service\n\tconst googleMapsKey = await getLibraries().getSecretsService().getKey('GOOGLE_MAPS_API_KEY');\n\tconst googleMapService = new GoogleMapsService(redisInstance, googleMapsKey as string);\n\tconst osrmMapService = new OsrmMapService();\n\tconst mapServiceInstance = new MapService(googleMapService, osrmMapService);\n\tContainer.set(MapService, mapServiceInstance);\n\n\t// Setup cash settlement service\n\tconst bobUsername = await getLibraries().getSecretsService().getKey('BOB_USER_NAME');\n\tconst bobPassword = await getLibraries().getSecretsService().getKey('BOB_PASSWORD');\n\tconst bobURL = await getLibraries().getSecretsService().getKey('BOB_URL');\n\n\tconst bobService = new BOBService({\n\t\tBOB_USER_NAME: bobUsername,\n\t\tBOB_PASSWORD: bobPassword,\n\t\tBOB_URL: bobURL,\n\t} as BOBSecrets);\n\tconst cashSettlementServiceInstance = new CashSettlementService(bobService);\n\tContainer.set(CashSettlementService, cashSettlementServiceInstance);\n\tContainer.set(BOBService, bobService);\n\n\tconst settingsServiceInstance = new CachedSettingsService(redisInstance);\n\tContainer.set(CachedSettingsService, settingsServiceInstance);\n\n\ttry {\n\t\tconst settings = await settingsServiceInstance.getSettings();\n\t\tconst { _setTimezone } = await import('./utilities/dayjs/dayjs');\n\t\t_setTimezone(settings?.timezone || 'Asia/Beirut');\n\t\tconsole.log('Timezone initialized:', settings?.timezone || 'Asia/Beirut');\n\t} catch (error) {\n\t\tconsole.error('Failed to initialize timezone:', error);\n\t}\n\n\t// Setup fcm service\n\tconst fcmService = new FirebaseFcmService();\n\tconst builder = new FirebaseMessageBuilderFactory();\n\tconst lyxaFcmSender = new LyxaFcmSender(fcmService, builder);\n\tconst lyxaFcmService = new LyxaFcmService(lyxaFcmSender);\n\n\tContainer.set(FirebaseFcmService, fcmService);\n\tContainer.set(LyxaFcmService, lyxaFcmService);\n\n\tconsole.log('Initialized everything');\n\n\tawait initRabbitMQConnection(RabbitMQUrlMap[options?.environment ?? EnvironmentConfigurationType.DEV]);\n\n\tconst allSubscriptions = SubscriptionRegistry.getAllSubscriptions();\n\n\tconst instantiatedServices = new Map<Function, any>();\n\n\tfor (const { eventId, targetConstructor, methodName } of allSubscriptions) {\n\t\tlet instance = instantiatedServices.get(targetConstructor);\n\t\tif (!instance) {\n\t\t\tinstance = Container.get(targetConstructor as any);\n\t\t\tinstantiatedServices.set(targetConstructor, instance);\n\t\t}\n\n\t\tawait subscribeToEvent(eventId, async payload => instance[methodName](payload));\n\t}\n}\n\nexport function getLibraries(): LibraryManager {\n\treturn Container.get(LibraryManager);\n}\n"]}
@@ -1,7 +1,9 @@
1
1
  import { GoogleTranslateClient } from './google-translate.client';
2
+ import { SecretManagerService } from '../secrets';
2
3
  export declare class GoogleTranslateService {
3
4
  private readonly googleTranslateClient;
4
- constructor(googleTranslateClient: GoogleTranslateClient);
5
- translate(apiKey: string, text: string, targetLanguage: string, sourceLanguage?: string): Promise<string>;
6
- translateBatch(apiKey: string, texts: string[], targetLanguage: string, sourceLanguage?: string): Promise<string[]>;
5
+ private readonly secretManager;
6
+ constructor(googleTranslateClient: GoogleTranslateClient, secretManager: SecretManagerService);
7
+ translate(text: string, targetLanguage: string, sourceLanguage?: string): Promise<string>;
8
+ translateBatch(texts: string[], targetLanguage: string, sourceLanguage?: string): Promise<string[]>;
7
9
  }
@@ -16,12 +16,15 @@ exports.GoogleTranslateService = void 0;
16
16
  const typedi_1 = require("typedi");
17
17
  const google_translate_client_1 = require("./google-translate.client");
18
18
  const logSettings_1 = require("@typegoose/typegoose/lib/logSettings");
19
+ const secrets_1 = require("../secrets");
19
20
  let GoogleTranslateService = class GoogleTranslateService {
20
21
  googleTranslateClient;
21
- constructor(googleTranslateClient) {
22
+ secretManager;
23
+ constructor(googleTranslateClient, secretManager) {
22
24
  this.googleTranslateClient = googleTranslateClient;
25
+ this.secretManager = secretManager;
23
26
  }
24
- async translate(apiKey, text, targetLanguage, sourceLanguage) {
27
+ async translate(text, targetLanguage, sourceLanguage) {
25
28
  if (!text?.trim()) {
26
29
  return text;
27
30
  }
@@ -35,6 +38,10 @@ let GoogleTranslateService = class GoogleTranslateService {
35
38
  input,
36
39
  });
37
40
  try {
41
+ const apiKey = await this.secretManager.getKey('GOOGLE_TRANSLATE_API_KEY');
42
+ if (!apiKey) {
43
+ throw new Error('GOOGLE_TRANSLATE_API_KEY is not configured');
44
+ }
38
45
  const result = await this.googleTranslateClient.translate(apiKey, input);
39
46
  logSettings_1.logger.info('Google Translate response', {
40
47
  input,
@@ -50,7 +57,7 @@ let GoogleTranslateService = class GoogleTranslateService {
50
57
  throw error;
51
58
  }
52
59
  }
53
- async translateBatch(apiKey, texts, targetLanguage, sourceLanguage) {
60
+ async translateBatch(texts, targetLanguage, sourceLanguage) {
54
61
  if (!texts.length) {
55
62
  return [];
56
63
  }
@@ -64,6 +71,10 @@ let GoogleTranslateService = class GoogleTranslateService {
64
71
  input,
65
72
  });
66
73
  try {
74
+ const apiKey = await this.secretManager.getKey('GOOGLE_TRANSLATE_API_KEY');
75
+ if (!apiKey) {
76
+ throw new Error('GOOGLE_TRANSLATE_API_KEY is not configured');
77
+ }
67
78
  const result = await this.googleTranslateClient.translate(apiKey, input);
68
79
  logSettings_1.logger.info('Google Translate batch response', {
69
80
  input,
@@ -84,6 +95,8 @@ exports.GoogleTranslateService = GoogleTranslateService;
84
95
  exports.GoogleTranslateService = GoogleTranslateService = __decorate([
85
96
  (0, typedi_1.Service)(),
86
97
  __param(0, (0, typedi_1.Inject)()),
87
- __metadata("design:paramtypes", [google_translate_client_1.GoogleTranslateClient])
98
+ __param(1, (0, typedi_1.Inject)()),
99
+ __metadata("design:paramtypes", [google_translate_client_1.GoogleTranslateClient,
100
+ secrets_1.SecretManagerService])
88
101
  ], GoogleTranslateService);
89
102
  //# sourceMappingURL=google-translate.service.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"google-translate.service.js","sourceRoot":"/","sources":["libraries/google-translate/google-translate.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,mCAAyC;AACzC,uEAAkE;AAElE,sEAA8D;AAGvD,IAAM,sBAAsB,GAA5B,MAAM,sBAAsB;IAGhB;IAFlB,YAEkB,qBAA4C;QAA5C,0BAAqB,GAArB,qBAAqB,CAAuB;IAC3D,CAAC;IAEG,KAAK,CAAC,SAAS,CACrB,MAAc,EACd,IAAY,EACZ,cAAsB,EACtB,cAAuB;QAEvB,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC;QACb,CAAC;QAED,MAAM,KAAK,GAA4B;YACtC,CAAC,EAAE,IAAI;YACP,MAAM,EAAE,cAAc;YACtB,MAAM,EAAE,cAAc;YACtB,MAAM,EAAE,MAAM;SACd,CAAC;QACF,oBAAM,CAAC,IAAI,CAAC,0BAA0B,EAAE;YACvC,KAAK;SACL,CAAC,CAAC;QACH,IAAI,CAAC;YACJ,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,qBAAqB,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;YACzE,oBAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE;gBACxC,KAAK;gBACL,MAAM,EAAE,MAAM,CAAC,IAAI;aACnB,CAAC,CAAC;YAEH,OAAO,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,cAAc,IAAI,IAAI,CAAC;QAC5D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,oBAAM,CAAC,KAAK,CAAC,yBAAyB,EAAE;gBACvC,KAAK;gBACL,KAAK;aACL,CAAC,CAAC;YACH,MAAM,KAAK,CAAC;QACb,CAAC;IACF,CAAC;IAEM,KAAK,CAAC,cAAc,CAC1B,MAAc,EACd,KAAe,EACf,cAAsB,EACtB,cAAuB;QAEvB,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;YACnB,OAAO,EAAE,CAAC;QACX,CAAC;QAED,MAAM,KAAK,GAA4B;YACtC,CAAC,EAAE,KAAK;YACR,MAAM,EAAE,cAAc;YACtB,MAAM,EAAE,cAAc;YACtB,MAAM,EAAE,MAAM;SACd,CAAC;QACF,oBAAM,CAAC,IAAI,CAAC,0BAA0B,EAAE;YACvC,KAAK;SACL,CAAC,CAAC;QAEH,IAAI,CAAC;YACJ,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,qBAAqB,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;YACzE,oBAAM,CAAC,IAAI,CAAC,iCAAiC,EAAE;gBAC9C,KAAK;gBACL,MAAM,EAAE,MAAM,CAAC,IAAI;aACnB,CAAC,CAAC;YAEH,OAAO,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC;QAC5D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,oBAAM,CAAC,KAAK,CAAC,+BAA+B,EAAE;gBAC7C,KAAK;gBACL,KAAK;aACL,CAAC,CAAC;YAEH,MAAM,KAAK,CAAC;QACb,CAAC;IACF,CAAC;CACD,CAAA;AA/EY,wDAAsB;iCAAtB,sBAAsB;IADlC,IAAA,gBAAO,GAAE;IAGP,WAAA,IAAA,eAAM,GAAE,CAAA;qCAC+B,+CAAqB;GAHlD,sBAAsB,CA+ElC","sourcesContent":["import { Inject, Service } from 'typedi';\nimport { GoogleTranslateClient } from './google-translate.client';\nimport { GoogleTranslateInputDTO } from './google-translate.dto';\nimport { logger } from '@typegoose/typegoose/lib/logSettings';\n\n@Service()\nexport class GoogleTranslateService {\n\tconstructor(\n\t\t@Inject()\n\t\tprivate readonly googleTranslateClient: GoogleTranslateClient\n\t) {}\n\n\tpublic async translate(\n\t\tapiKey: string,\n\t\ttext: string,\n\t\ttargetLanguage: string,\n\t\tsourceLanguage?: string\n\t): Promise<string> {\n\t\tif (!text?.trim()) {\n\t\t\treturn text;\n\t\t}\n\n\t\tconst input: GoogleTranslateInputDTO = {\n\t\t\tq: text,\n\t\t\ttarget: targetLanguage,\n\t\t\tsource: sourceLanguage,\n\t\t\tformat: 'text',\n\t\t};\n\t\tlogger.info('Google Translate request', {\n\t\t\tinput,\n\t\t});\n\t\ttry {\n\t\t\tconst result = await this.googleTranslateClient.translate(apiKey, input);\n\t\t\tlogger.info('Google Translate response', {\n\t\t\t\tinput,\n\t\t\t\tresult: result.data,\n\t\t\t});\n\n\t\t\treturn result.data.translations[0]?.translatedText ?? text;\n\t\t} catch (error) {\n\t\t\tlogger.error('Google Translate failed', {\n\t\t\t\tinput,\n\t\t\t\terror,\n\t\t\t});\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tpublic async translateBatch(\n\t\tapiKey: string,\n\t\ttexts: string[],\n\t\ttargetLanguage: string,\n\t\tsourceLanguage?: string\n\t): Promise<string[]> {\n\t\tif (!texts.length) {\n\t\t\treturn [];\n\t\t}\n\n\t\tconst input: GoogleTranslateInputDTO = {\n\t\t\tq: texts,\n\t\t\ttarget: targetLanguage,\n\t\t\tsource: sourceLanguage,\n\t\t\tformat: 'text',\n\t\t};\n\t\tlogger.info('Google Translate request', {\n\t\t\tinput,\n\t\t});\n\n\t\ttry {\n\t\t\tconst result = await this.googleTranslateClient.translate(apiKey, input);\n\t\t\tlogger.info('Google Translate batch response', {\n\t\t\t\tinput,\n\t\t\t\tresult: result.data,\n\t\t\t});\n\n\t\t\treturn result.data.translations.map(x => x.translatedText);\n\t\t} catch (error) {\n\t\t\tlogger.error('Google Translate batch failed', {\n\t\t\t\tinput,\n\t\t\t\terror,\n\t\t\t});\n\n\t\t\tthrow error;\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"google-translate.service.js","sourceRoot":"/","sources":["libraries/google-translate/google-translate.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,mCAAyC;AACzC,uEAAkE;AAElE,sEAA8D;AAC9D,wCAAkD;AAG3C,IAAM,sBAAsB,GAA5B,MAAM,sBAAsB;IAGhB;IAEA;IAJlB,YAEkB,qBAA4C,EAE5C,aAAmC;QAFnC,0BAAqB,GAArB,qBAAqB,CAAuB;QAE5C,kBAAa,GAAb,aAAa,CAAsB;IAClD,CAAC;IAEG,KAAK,CAAC,SAAS,CAAC,IAAY,EAAE,cAAsB,EAAE,cAAuB;QACnF,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC;QACb,CAAC;QAED,MAAM,KAAK,GAA4B;YACtC,CAAC,EAAE,IAAI;YACP,MAAM,EAAE,cAAc;YACtB,MAAM,EAAE,cAAc;YACtB,MAAM,EAAE,MAAM;SACd,CAAC;QACF,oBAAM,CAAC,IAAI,CAAC,0BAA0B,EAAE;YACvC,KAAK;SACL,CAAC,CAAC;QACH,IAAI,CAAC;YACJ,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,0BAA0B,CAAC,CAAC;YAE3E,IAAI,CAAC,MAAM,EAAE,CAAC;gBACb,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;YAC/D,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,qBAAqB,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;YACzE,oBAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE;gBACxC,KAAK;gBACL,MAAM,EAAE,MAAM,CAAC,IAAI;aACnB,CAAC,CAAC;YAEH,OAAO,MAAM,CAAC,I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KAAK;aACL,CAAC,CAAC;YAEH,MAAM,KAAK,CAAC;QACb,CAAC;IACF,CAAC;CACD,CAAA;AApFY,wDAAsB;iCAAtB,sBAAsB;IADlC,IAAA,gBAAO,GAAE;IAGP,WAAA,IAAA,eAAM,GAAE,CAAA;IAER,WAAA,IAAA,eAAM,GAAE,CAAA;qCAD+B,+CAAqB;QAE7B,8BAAoB;GALzC,sBAAsB,CAoFlC","sourcesContent":["import { Inject, Service } from 'typedi';\nimport { GoogleTranslateClient } from './google-translate.client';\nimport { GoogleTranslateInputDTO } from './google-translate.dto';\nimport { logger } from '@typegoose/typegoose/lib/logSettings';\nimport { SecretManagerService } from '../secrets';\n\n@Service()\nexport class GoogleTranslateService {\n\tconstructor(\n\t\t@Inject()\n\t\tprivate readonly googleTranslateClient: GoogleTranslateClient,\n\t\t@Inject()\n\t\tprivate readonly secretManager: SecretManagerService\n\t) {}\n\n\tpublic async translate(text: string, targetLanguage: string, sourceLanguage?: string): Promise<string> {\n\t\tif (!text?.trim()) {\n\t\t\treturn text;\n\t\t}\n\n\t\tconst input: GoogleTranslateInputDTO = {\n\t\t\tq: text,\n\t\t\ttarget: targetLanguage,\n\t\t\tsource: sourceLanguage,\n\t\t\tformat: 'text',\n\t\t};\n\t\tlogger.info('Google Translate request', {\n\t\t\tinput,\n\t\t});\n\t\ttry {\n\t\t\tconst apiKey = await this.secretManager.getKey('GOOGLE_TRANSLATE_API_KEY');\n\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error('GOOGLE_TRANSLATE_API_KEY is not configured');\n\t\t\t}\n\t\t\tconst result = await this.googleTranslateClient.translate(apiKey, input);\n\t\t\tlogger.info('Google Translate response', {\n\t\t\t\tinput,\n\t\t\t\tresult: result.data,\n\t\t\t});\n\n\t\t\treturn result.data.translations[0]?.translatedText ?? text;\n\t\t} catch (error) {\n\t\t\tlogger.error('Google Translate failed', {\n\t\t\t\tinput,\n\t\t\t\terror,\n\t\t\t});\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tpublic async translateBatch(\n\t\ttexts: string[],\n\t\ttargetLanguage: string,\n\t\tsourceLanguage?: string\n\t): Promise<string[]> {\n\t\tif (!texts.length) {\n\t\t\treturn [];\n\t\t}\n\n\t\tconst input: GoogleTranslateInputDTO = {\n\t\t\tq: texts,\n\t\t\ttarget: targetLanguage,\n\t\t\tsource: sourceLanguage,\n\t\t\tformat: 'text',\n\t\t};\n\t\tlogger.info('Google Translate request', {\n\t\t\tinput,\n\t\t});\n\n\t\ttry {\n\t\t\tconst apiKey = await this.secretManager.getKey('GOOGLE_TRANSLATE_API_KEY');\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error('GOOGLE_TRANSLATE_API_KEY is not configured');\n\t\t\t}\n\t\t\tconst result = await this.googleTranslateClient.translate(apiKey, input);\n\t\t\tlogger.info('Google Translate batch response', {\n\t\t\t\tinput,\n\t\t\t\tresult: result.data,\n\t\t\t});\n\n\t\t\treturn result.data.translations.map(x => x.translatedText);\n\t\t} catch (error) {\n\t\t\tlogger.error('Google Translate batch failed', {\n\t\t\t\tinput,\n\t\t\t\terror,\n\t\t\t});\n\n\t\t\tthrow error;\n\t\t}\n\t}\n}\n"]}
@@ -1,11 +1,12 @@
1
- import { LLMProvider } from "../llm-provider-service";
1
+ import { LLMProvider } from '../llm-provider-service';
2
2
  export interface IAICompletionProvider {
3
- generatePromptVariantsBatch(items: ItemTextInput[], model?: string, instructions?: string, options?: {
3
+ generateSearchVariants(items: ItemTextInput[], model?: string, instructions?: string, options?: {
4
4
  pollIntervalMs?: number;
5
5
  maxWaitMs?: number;
6
6
  }): Promise<TextVariantResult[]>;
7
7
  }
8
8
  export interface TextVariantResult {
9
+ itemId: string;
9
10
  original: string;
10
11
  arabic?: string[];
11
12
  arabizi?: string[];
@@ -1 +1 @@
1
- {"version":3,"file":"common-llm-interfaces.js","sourceRoot":"/","sources":["libraries/llm/interfaces/common-llm-interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import { LLMProvider } from \"../llm-provider-service\";\n\nexport interface IAICompletionProvider {\n\tgeneratePromptVariantsBatch(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { pollIntervalMs?: number; maxWaitMs?: number }\n\t): Promise<TextVariantResult[]>;\n}\n\n// export interface TextVariantConfig {\n// \tisArabic?: boolean; // Arabic translation, Arabic script\n// \tisArabizi?: boolean; // Arabic word, Latin script (e.g. \"zaytoon\")\n// \tisArabicTransliteration?: boolean; // original word, spelled phonetically in Arabic script (e.g. \"أوليف\")\n// }\n\nexport interface TextVariantResult {\n\toriginal: string;\n\tarabic?: string[];\n\tarabizi?: string[];\n\tarabicTransliteration?: string[];\n}\n\nexport interface AiConfiguration {\n\tprovider: LLMProvider;\n\tmodel?: string;\n\tinstructions?: string;\n\tprompt?: string;\n}\n\nexport interface ItemTextInput {\n\titemId: string;\n\ttext: string;\n}\n"]}
1
+ {"version":3,"file":"common-llm-interfaces.js","sourceRoot":"/","sources":["libraries/llm/interfaces/common-llm-interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import { LLMProvider } from '../llm-provider-service';\n\nexport interface IAICompletionProvider {\n\tgenerateSearchVariants(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { pollIntervalMs?: number; maxWaitMs?: number }\n\t): Promise<TextVariantResult[]>;\n}\n\n// export interface TextVariantConfig {\n// \tisArabic?: boolean; // Arabic translation, Arabic script\n// \tisArabizi?: boolean; // Arabic word, Latin script (e.g. \"zaytoon\")\n// \tisArabicTransliteration?: boolean; // original word, spelled phonetically in Arabic script (e.g. \"أوليف\")\n// }\n\nexport interface TextVariantResult {\n\titemId: string;\n\toriginal: string;\n\tarabic?: string[];\n\tarabizi?: string[];\n\tarabicTransliteration?: string[];\n}\n\nexport interface AiConfiguration {\n\tprovider: LLMProvider;\n\tmodel?: string;\n\tinstructions?: string;\n\tprompt?: string;\n}\n\nexport interface ItemTextInput {\n\titemId: string;\n\ttext: string;\n}\n"]}
@@ -8,6 +8,6 @@ export declare class LLMCompletionService {
8
8
  private providerInstance;
9
9
  private readonly factories;
10
10
  constructor(provider: LLMProvider);
11
- generatePromptVariantsBatch(payload: ItemTextInput[], aiConfigurations: AiConfiguration): Promise<TextVariantResult[]>;
11
+ generatePromptVariants(payload: ItemTextInput[], aiConfigurations: AiConfiguration): Promise<TextVariantResult[]>;
12
12
  private getProvider;
13
13
  }
@@ -27,10 +27,10 @@ class LLMCompletionService {
27
27
  constructor(provider) {
28
28
  this.provider = provider;
29
29
  }
30
- async generatePromptVariantsBatch(payload, aiConfigurations) {
30
+ async generatePromptVariants(payload, aiConfigurations) {
31
31
  const provider = await this.getProvider();
32
32
  console.log(`You are using llm provider: ${this.provider} with model: ${aiConfigurations.model ?? 'gpt-5-mini'}`);
33
- return provider.generatePromptVariantsBatch(payload, aiConfigurations.model, aiConfigurations.instructions);
33
+ return provider.generateSearchVariants(payload, aiConfigurations.model, aiConfigurations.instructions);
34
34
  }
35
35
  async getProvider() {
36
36
  if (!this.providerInstance) {
@@ -1 +1 @@
1
- {"version":3,"file":"llm-provider-service.js","sourceRoot":"/","sources":["libraries/llm/llm-provider-service.ts"],"names":[],"mappings":";;;AAAA,6BAAqC;AAOrC,+DAA+D;AAC/D,iEAAuE;AAEvE,IAAY,WAGX;AAHD,WAAY,WAAW;IACtB,gCAAiB,CAAA;IACjB,gCAAiB,CAAA;AAClB,CAAC,EAHW,WAAW,2BAAX,WAAW,QAGtB;AAID,MAAa,oBAAoB;IAgBH;IAfrB,gBAAgB,GAAiC,IAAI,CAAC;IAE7C,SAAS,GAAyC;QAClE,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,KAAK,IAAI,EAAE;YAChC,MAAM,MAAM,GAAG,MAAM,IAAA,gBAAY,GAAE,CAAC,iBAAiB,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;YACjF,IAAI,CAAC,MAAM,EAAE,CAAC;gBACb,MAAM,IAAA,8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ICompletionProvider,\n} from './interfaces/common-llm-interfaces';\nimport { badRequestError } from '../../utilities/error-common';\nimport { OpenAICompletionProvider } from './providers/openai-provider';\n\nexport enum LLMProvider {\n\tOPENAI = 'openai',\n\tGEMINI = 'gemini',\n}\n\ntype ProviderFactory = () => Promise<IAICompletionProvider>;\n\nexport class LLMCompletionService {\n\tprivate providerInstance: IAICompletionProvider | null = null;\n\n\tprivate readonly factories: Record<LLMProvider, ProviderFactory> = {\n\t\t[LLMProvider.OPENAI]: async () => {\n\t\t\tconst apiKey = await getLibraries().getSecretsService().getKey('OPENAI_API_KEY');\t\t\t\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow badRequestError(`No API key found for provider ${LLMProvider.OPENAI}`);\n\t\t\t}\n\t\t\treturn new OpenAICompletionProvider(apiKey, 'gpt-5-mini');\n\t\t},\n\t\t[LLMProvider.GEMINI]: async () => {\n\t\t\tthrow badRequestError(`Provider ${LLMProvider.GEMINI} is not implemented yet`);\n\t\t},\n\t};\n\n\tconstructor(private readonly provider: LLMProvider) {}\n\t\n\tasync generatePromptVariantsBatch(payload: ItemTextInput[], aiConfigurations: AiConfiguration): Promise<TextVariantResult[]> {\n\t\tconst provider = await this.getProvider();\n\t\tconsole.log(`You are using llm provider: ${this.provider} with model: ${aiConfigurations.model ?? 'gpt-5-mini'}`);\n\t\treturn provider.generatePromptVariantsBatch(payload, aiConfigurations.model,aiConfigurations.instructions);\n\t}\n\n\tprivate async getProvider(): Promise<IAICompletionProvider> {\n\t\tif (!this.providerInstance) {\n\t\t\tconst factory = this.factories[this.provider];\n\t\t\tif (!factory) {\n\t\t\t\tthrow badRequestError(`Unsupported transliteration provider: ${this.provider}`);\n\t\t\t}\n\t\t\tthis.providerInstance = await factory();\n\t\t}\n\t\treturn this.providerInstance;\n\t}\n}\n"]}
1
+ {"version":3,"file":"llm-provider-service.js","sourceRoot":"/","sources":["libraries/llm/llm-provider-service.ts"],"names":[],"mappings":";;;AAAA,6BAAqC;AAOrC,+DAA+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,EAAE,CAAC;YAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC9C,IAAI,CAAC,OAAO,EAAE,CAAC;gBACd,MAAM,IAAA,8BAAe,EAAC,yCAAyC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;YACjF,CAAC;YACD,IAAI,CAAC,gBAAgB,GAAG,MAAM,OAAO,EAAE,CAAC;QACzC,CAAC;QACD,OAAO,IAAI,CAAC,gBAAgB,CAAC;IAC9B,CAAC;CACD;AAlCD,oDAkCC","sourcesContent":["import { getLibraries } from '../..';\nimport {\n\tItemTextInput,\t\n\tAiConfiguration,\t\n\tTextVariantResult,\n\tIAICompletionProvider,\n} from './interfaces/common-llm-interfaces';\nimport { badRequestError } from '../../utilities/error-common';\nimport { OpenAICompletionProvider } from './providers/openai-provider';\n\nexport enum LLMProvider {\n\tOPENAI = 'openai',\n\tGEMINI = 'gemini',\n}\n\ntype ProviderFactory = () => Promise<IAICompletionProvider>;\n\nexport class LLMCompletionService {\n\tprivate providerInstance: IAICompletionProvider | null = null;\n\n\tprivate readonly factories: Record<LLMProvider, ProviderFactory> = {\n\t\t[LLMProvider.OPENAI]: async () => {\n\t\t\tconst apiKey = await getLibraries().getSecretsService().getKey('OPENAI_API_KEY');\t\t\t\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow badRequestError(`No API key found for provider ${LLMProvider.OPENAI}`);\n\t\t\t}\n\t\t\treturn new OpenAICompletionProvider(apiKey, 'gpt-5-mini');\n\t\t},\n\t\t[LLMProvider.GEMINI]: async () => {\n\t\t\tthrow badRequestError(`Provider ${LLMProvider.GEMINI} is not implemented yet`);\n\t\t},\n\t};\n\n\tconstructor(private readonly provider: LLMProvider) {}\n\t\n\tasync generatePromptVariants(payload: ItemTextInput[], aiConfigurations: AiConfiguration): Promise<TextVariantResult[]> {\n\t\tconst provider = await this.getProvider();\n\t\tconsole.log(`You are using llm provider: ${this.provider} with model: ${aiConfigurations.model ?? 'gpt-5-mini'}`);\n\t\treturn provider.generateSearchVariants(payload, aiConfigurations.model,aiConfigurations.instructions);\n\t}\n\n\tprivate async getProvider(): Promise<IAICompletionProvider> {\n\t\tif (!this.providerInstance) {\n\t\t\tconst factory = this.factories[this.provider];\n\t\t\tif (!factory) {\n\t\t\t\tthrow badRequestError(`Unsupported transliteration provider: ${this.provider}`);\n\t\t\t}\n\t\t\tthis.providerInstance = await factory();\n\t\t}\n\t\treturn this.providerInstance;\n\t}\n}\n"]}
@@ -3,6 +3,17 @@ export declare class OpenAICompletionProvider implements IAICompletionProvider {
3
3
  private readonly client;
4
4
  private readonly model;
5
5
  constructor(apiKey: string | undefined, model?: string);
6
+ generateSearchVariants(items: ItemTextInput[], model?: string, instructions?: string, options?: {
7
+ batchThreshold?: number;
8
+ concurrency?: number;
9
+ maxRetries?: number;
10
+ pollIntervalMs?: number;
11
+ maxWaitMs?: number;
12
+ }): Promise<TextVariantResult[]>;
13
+ generatePromptVariantsRealtime(items: ItemTextInput[], model?: string, instructions?: string, options?: {
14
+ concurrency?: number;
15
+ maxRetries?: number;
16
+ }): Promise<TextVariantResult[]>;
6
17
  generatePromptVariantsBatch(items: ItemTextInput[], model?: string, instructions?: string, options?: {
7
18
  pollIntervalMs?: number;
8
19
  maxWaitMs?: number;
@@ -45,18 +45,121 @@ class OpenAICompletionProvider {
45
45
  this.client = new openai_1.default({ apiKey });
46
46
  this.model = model;
47
47
  }
48
+ async generateSearchVariants(items, model, instructions, options) {
49
+ const batchThreshold = options?.batchThreshold ?? 300;
50
+ const mode = items.length >= batchThreshold ? 'batch' : 'realtime';
51
+ console.log('generatePromptVariants: dispatching', {
52
+ itemCount: items.length,
53
+ batchThreshold,
54
+ mode,
55
+ });
56
+ if (mode === 'batch') {
57
+ console.log('Started batch processing generatePromptVariantsBatch');
58
+ return this.generatePromptVariantsBatch(items, model, instructions, {
59
+ pollIntervalMs: options?.pollIntervalMs,
60
+ maxWaitMs: options?.maxWaitMs,
61
+ });
62
+ }
63
+ console.log('Started realtime processing generatePromptVariantsRealtime');
64
+ return this.generatePromptVariantsRealtime(items, model, instructions, {
65
+ concurrency: options?.concurrency,
66
+ maxRetries: options?.maxRetries,
67
+ });
68
+ }
69
+ async generatePromptVariantsRealtime(items, model, instructions, options) {
70
+ const concurrency = options?.concurrency ?? 10;
71
+ const maxRetries = options?.maxRetries ?? 3;
72
+ try {
73
+ if (items.length === 0)
74
+ return [];
75
+ const { fields, instructions: defaultInstructions } = this.buildVariantRequest();
76
+ const finalInstructions = instructions ?? defaultInstructions;
77
+ const finalModel = model ?? this.model;
78
+ const schema = this.buildJsonSchema(fields);
79
+ console.log('generatePromptVariantsRealtime: starting', {
80
+ itemCount: items.length,
81
+ finalModel,
82
+ concurrency,
83
+ });
84
+ const runOne = async ({ itemId, text }, attempt = 1) => {
85
+ try {
86
+ const response = await this.client.responses.create({
87
+ model: finalModel,
88
+ instructions: finalInstructions,
89
+ input: text,
90
+ text: {
91
+ format: {
92
+ type: 'json_schema',
93
+ name: 'text_variants',
94
+ schema,
95
+ strict: true,
96
+ },
97
+ },
98
+ });
99
+ const outputText = response.output_text ??
100
+ response.output
101
+ ?.find((o) => o.type === 'message')
102
+ ?.content?.find((c) => c.type === 'output_text')?.text;
103
+ if (!outputText) {
104
+ throw new Error('empty output_text in response');
105
+ }
106
+ let parsed;
107
+ try {
108
+ parsed = JSON.parse(outputText);
109
+ }
110
+ catch {
111
+ console.error(`generatePromptVariantsRealtime: failed to parse output for itemId=${itemId}`, outputText);
112
+ return { itemId, original: text };
113
+ }
114
+ return { itemId, original: text, ...parsed };
115
+ }
116
+ catch (error) {
117
+ const status = error?.status ?? error?.response?.status;
118
+ const isRetryable = status === 429 || status === 500 || status === 503;
119
+ if (isRetryable && attempt <= maxRetries) {
120
+ const backoffMs = 1000 * 2 ** attempt;
121
+ console.warn(`generatePromptVariantsRealtime: retryable error (status=${status}) for itemId=${itemId}, attempt ${attempt}/${maxRetries}, retrying in ${backoffMs}ms`);
122
+ await new Promise(resolve => setTimeout(resolve, backoffMs));
123
+ return runOne({ itemId, text }, attempt + 1);
124
+ }
125
+ console.error(`generatePromptVariantsRealtime: failed for itemId=${itemId} after ${attempt} attempt(s)`, error);
126
+ return { itemId, original: text };
127
+ }
128
+ };
129
+ const results = [];
130
+ for (let i = 0; i < items.length; i += concurrency) {
131
+ const chunk = items.slice(i, i + concurrency);
132
+ const chunkResults = await Promise.all(chunk.map(item => runOne(item)));
133
+ results.push(...chunkResults);
134
+ console.log(`generatePromptVariantsRealtime: progress ${Math.min(i + concurrency, items.length)}/${items.length}`);
135
+ }
136
+ const failedCount = results.filter(r => Object.keys(r).length <= 2).length;
137
+ console.log('generatePromptVariantsRealtime: done', {
138
+ total: items.length,
139
+ failed: failedCount,
140
+ });
141
+ return results;
142
+ }
143
+ catch (error) {
144
+ console.error('generatePromptVariantsRealtime failed', {
145
+ error,
146
+ itemCount: items.length,
147
+ model,
148
+ });
149
+ throw error;
150
+ }
151
+ }
48
152
  async generatePromptVariantsBatch(items, model, instructions, options) {
49
153
  try {
50
154
  console.log({ items, model, instructions });
51
155
  const { fields, instructions: defaultInstructions } = this.buildVariantRequest();
52
156
  const finalInstructions = instructions ?? defaultInstructions;
53
157
  const finalModel = model ?? this.model;
54
- console.log('xx111');
158
+ console.log(`You are using llm provider model: ${finalModel}`);
55
159
  const schema = this.buildJsonSchema(fields);
56
160
  if (items.length > 50_000) {
57
161
  throw new Error('Batch API supports max 50,000 requests per batch — chunk your items first.');
58
162
  }
59
- console.log(1111);
60
163
  const jsonl = items
61
164
  .map(({ itemId, text }) => JSON.stringify({
62
165
  custom_id: itemId,
@@ -77,26 +180,21 @@ class OpenAICompletionProvider {
77
180
  },
78
181
  }))
79
182
  .join('\n');
80
- console.log(22221);
81
183
  const inputFile = await this.client.files.create({
82
184
  file: await (0, openai_1.toFile)(Buffer.from(jsonl, 'utf-8'), 'batch-input.jsonl'),
83
185
  purpose: 'batch',
84
186
  });
85
- console.log(22223);
86
187
  let batch = await this.client.batches.create({
87
188
  input_file_id: inputFile.id,
88
189
  endpoint: '/v1/responses',
89
190
  completion_window: '24h',
90
191
  });
91
- console.log(3333);
92
192
  batch = await this.pollBatch(batch.id, options?.pollIntervalMs, options?.maxWaitMs);
93
- console.log(5555);
94
193
  if (batch.status !== 'completed') {
95
194
  throw new Error(`Batch ${batch.id} ended with status "${batch.status}" (${batch.request_counts?.failed ?? 0} failed)`);
96
195
  }
97
196
  const outputById = await this.readJsonlFile(batch.output_file_id);
98
197
  const errorsById = await this.readJsonlFile(batch.error_file_id);
99
- console.log(4444);
100
198
  return items.map(({ itemId, text }) => {
101
199
  const record = outputById.get(itemId);
102
200
  const body = record?.response?.body;
@@ -147,6 +245,7 @@ class OpenAICompletionProvider {
147
245
  const start = Date.now();
148
246
  while (true) {
149
247
  const batch = await this.client.batches.retrieve(batchId);
248
+ console.log(`pollBatch: ${batchId} status=${batch.status} elapsed=${Math.round((Date.now() - start) / 1000)}s`);
150
249
  if (terminal.has(batch.status))
151
250
  return batch;
152
251
  if (Date.now() - start > maxWaitMs) {
@@ -1 +1 @@
1
- {"version":3,"file":"openai-provider.js","sourceRoot":"/","sources":["libraries/llm/providers/openai-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAwC;AAGxC,MAAa,wBAAwB;IACnB,MAAM,CAAS;IACf,KAAK,CAAS;IAE/B,YAAY,MAA0B,EAAE,QAAgB,YAAY;QACnE,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;QACrF,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,gBAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,2BAA2B,CAChC,KAAsB,EACtB,KAAc,EACd,YAAqB,EACrB,OAAyD;QAEz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AAiC;QAC5D,MAAM,MAAM,GAAG,IAAI,GAAG,EAAe,CAAC;QACtC,IAAI,CAAC,MAAM;YAAE,OAAO,MAAM,CAAC;QAE3B,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAC7D,MAAM,IAAI,GAAG,MAAM,YAAY,CAAC,IAAI,EAAE,CAAC;QAEvC,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YACrD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YAChC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;IAEO,eAAe,CAAC,MAAmC;QAC1D,MAAM,UAAU,GAAwB,EAAE,CAAC;QAC3C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC5B,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,CAAC;QAClE,CAAC;QAED,OAAO;YACN,IAAI,EAAE,QAAQ;YACd,UAAU;YACV,QAAQ,EAAE,MAAM;YAChB,oBAAoB,EAAE,KAAK;SAC3B,CAAC;IACH,CAAC;CACD;AA1MD,4DA0MC","sourcesContent":["import OpenAI, { toFile } from 'openai';\nimport { IAICompletionProvider, ItemTextInput, TextVariantResult } from '../interfaces/common-llm-interfaces';\n\nexport class OpenAICompletionProvider implements IAICompletionProvider {\n\tprivate readonly client: OpenAI;\n\tprivate readonly model: string;\n\n\tconstructor(apiKey: string | undefined, model: string = 'gpt-5-mini') {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error('OPENAI_API_KEY is required to construct OpenAICompletionProvider');\n\t\t}\n\t\tthis.client = new OpenAI({ apiKey });\n\t\tthis.model = model;\n\t}\n\n\tasync generatePromptVariantsBatch(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { pollIntervalMs?: number; maxWaitMs?: number }\n\t): Promise<TextVariantResult[]> {\n\t\ttry {\n\t\t\tconsole.log({ items, model, instructions });\n\n\t\t\tconst { fields, instructions: defaultInstructions } = this.buildVariantRequest();\n\t\t\tconst finalInstructions = instructions ?? defaultInstructions;\n\t\t\tconst finalModel = model ?? this.model;\n\t\t\tconsole.log('xx111');\n\t\t\tconst schema = this.buildJsonSchema(fields);\n\n\t\t\tif (items.length > 50_000) {\n\t\t\t\tthrow new Error('Batch API supports max 50,000 requests per batch — chunk your items first.');\n\t\t\t}\n\n\t\t\tconsole.log(1111);\n\n\t\t\t// 1. Build JSONL\n\t\t\tconst jsonl = items\n\t\t\t\t.map(({ itemId, text }) =>\n\t\t\t\t\tJSON.stringify({\n\t\t\t\t\t\tcustom_id: itemId,\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\turl: '/v1/responses',\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tmodel: finalModel,\n\t\t\t\t\t\t\tinstructions: finalInstructions,\n\t\t\t\t\t\t\tinput: text,\n\t\t\t\t\t\t\ttext: {\n\t\t\t\t\t\t\t\tformat: {\n\t\t\t\t\t\t\t\t\ttype: 'json_schema',\n\t\t\t\t\t\t\t\t\tname: 'text_variants',\n\t\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\t\tstrict: true,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t.join('\\n');\n\n\t\t\tconsole.log(22221);\n\n\t\t\t// 2. Upload input file\n\t\t\tconst inputFile = await this.client.files.create({\n\t\t\t\tfile: await toFile(Buffer.from(jsonl, 'utf-8'), 'batch-input.jsonl'),\n\t\t\t\tpurpose: 'batch',\n\t\t\t});\n\t\t\tconsole.log(22223);\n\t\t\t// 3. Create batch job\n\t\t\tlet batch = await this.client.batches.create({\n\t\t\t\tinput_file_id: inputFile.id,\n\t\t\t\tendpoint: '/v1/responses',\n\t\t\t\tcompletion_window: '24h',\n\t\t\t});\n\n\t\t\tconsole.log(3333);\n\n\t\t\t// 4. Poll until done\n\t\t\tbatch = await this.pollBatch(batch.id, options?.pollIntervalMs, options?.maxWaitMs);\n\t\t\tconsole.log(5555);\n\t\t\tif (batch.status !== 'completed') {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Batch ${batch.id} ended with status \"${batch.status}\" (${batch.request_counts?.failed ?? 0} failed)`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 5. Parse output\n\t\t\tconst outputById = await this.readJsonlFile(batch.output_file_id);\n\t\t\tconst errorsById = await this.readJsonlFile(batch.error_file_id);\n\n\t\t\tconsole.log(4444);\n\n\t\t\t// 6. Merge results\n\t\t\treturn items.map(({ itemId, text }) => {\n\t\t\t\tconst record = outputById.get(itemId);\n\t\t\t\tconst body = record?.response?.body;\n\n\t\t\t\tconst outputText =\n\t\t\t\t\tbody?.output_text ??\n\t\t\t\t\tbody?.output\n\t\t\t\t\t\t?.find((o: any) => o.type === 'message')\n\t\t\t\t\t\t?.content?.find((c: any) => c.type === 'output_text')?.text;\n\n\t\t\t\tif (!outputText) {\n\t\t\t\t\tconst err = errorsById.get(itemId);\n\t\t\t\t\tconsole.error(`generatePromptVariantsBatch: no output for itemId=${itemId}`, err ?? record?.error);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\treturn { itemId, original: text, ...JSON.parse(outputText) };\n\t\t\t\t} catch {\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`generatePromptVariantsBatch: failed to parse output for itemId=${itemId}`,\n\t\t\t\t\t\toutputText\n\t\t\t\t\t);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error('generatePromptVariantsBatch failed', {\n\t\t\t\terror,\n\t\t\t\titemCount: items.length,\n\t\t\t\tmodel,\n\t\t\t});\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// ── Helpers ──────────────────────────────────────────────────────\n\n\tprivate buildVariantRequest(): {\n\t\tfields: (keyof TextVariantResult)[];\n\t\tinstructions: string;\n\t} {\n\t\tconst fieldDescriptions: Record<Exclude<keyof TextVariantResult, 'original'>, string> = {\n\t\t\tarabic:\n\t\t\t\t'The Arabic translation of the word/phrase, written in Arabic script (e.g. \"زيتون\" for \"olive\"). Provide 1-3 common variants if applicable.',\n\t\t\tarabizi:\n\t\t\t\t'The Arabic word\\'s meaning, written phonetically in Latin script/numerals — i.e. Arabizi (e.g. \"zaytoon\"). Provide 1-3 common spelling variants.',\n\t\t\tarabicTransliteration:\n\t\t\t\t'The original word spelled phonetically in Arabic script, NOT translated (e.g. \"أوليف\" for \"olive\"). Useful when there is no native Arabic equivalent.',\n\t\t};\n\n\t\tconst fields = Object.keys(fieldDescriptions) as (keyof TextVariantResult)[];\n\n\t\tconst instructions = [\n\t\t\t'You generate search-name variants for a food delivery app product/shop name.',\n\t\t\t'For the given input text, return ONLY the following fields as arrays of strings:',\n\t\t\t...fields.map(f => `- ${f}: ${fieldDescriptions[f as Exclude<keyof TextVariantResult, 'original'>]}`),\n\t\t\t'Return strict JSON matching the schema. No extra commentary, no extra fields.',\n\t\t].join('\\n');\n\n\t\treturn { fields, instructions };\n\t}\n\n\tprivate async pollBatch(\n\t\tbatchId: string,\n\t\tintervalMs = 5000,\n\t\tmaxWaitMs = 24 * 60 * 60 * 1000\n\t): Promise<OpenAI.Batches.Batch> {\n\t\tconst terminal = new Set(['completed', 'failed', 'expired', 'cancelled']);\n\t\tconst start = Date.now();\n\n\t\twhile (true) {\n\t\t\tconst batch = await this.client.batches.retrieve(batchId);\n\t\t\tif (terminal.has(batch.status)) return batch;\n\n\t\t\tif (Date.now() - start > maxWaitMs) {\n\t\t\t\tthrow new Error(`Batch ${batchId} did not finish within ${maxWaitMs}ms (status: ${batch.status})`);\n\t\t\t}\n\n\t\t\tawait new Promise(resolve => setTimeout(resolve, intervalMs));\n\t\t}\n\t}\n\n\tprivate async readJsonlFile(fileId: string | null | undefined): Promise<Map<string, any>> {\n\t\tconst result = new Map<string, any>();\n\t\tif (!fileId) return result;\n\n\t\tconst fileResponse = await this.client.files.content(fileId);\n\t\tconst text = await fileResponse.text();\n\n\t\tfor (const line of text.split('\\n').filter(Boolean)) {\n\t\t\tconst record = JSON.parse(line);\n\t\t\tresult.set(record.custom_id, record);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate buildJsonSchema(fields: (keyof TextVariantResult)[]) {\n\t\tconst properties: Record<string, any> = {};\n\t\tfor (const field of fields) {\n\t\t\tproperties[field] = { type: 'array', items: { type: 'string' } };\n\t\t}\n\n\t\treturn {\n\t\t\ttype: 'object',\n\t\t\tproperties,\n\t\t\trequired: fields,\n\t\t\tadditionalProperties: false,\n\t\t};\n\t}\n}\n"]}
1
+ {"version":3,"file":"openai-provider.js","sourceRoot":"/","sources":["libraries/llm/providers/openai-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAwC;AAGxC,MAAa,wBAAwB;IACnB,MAAM,CAAS;IACf,KAAK,CAAS;IAE/B,YAAY,MAA0B,EAAE,QAAgB,YAAY;QACnE,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;QACrF,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,gBAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;IAOD,KAAK,CAAC,sBAAsB,CAC3B,KAAsB,EACtB,KAAc,EACd,YAAqB,EACrB,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AI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YACrD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YAChC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;IAEO,eAAe,CAAC,MAAmC;QAC1D,MAAM,UAAU,GAAwB,EAAE,CAAC;QAC3C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC5B,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,CAAC;QAClE,CAAC;QAED,OAAO;YACN,IAAI,EAAE,QAAQ;YACd,UAAU;YACV,QAAQ,EAAE,MAAM;YAChB,oBAAoB,EAAE,KAAK;SAC3B,CAAC;IACH,CAAC;CACD;AAvWD,4DAuWC","sourcesContent":["import OpenAI, { toFile } from 'openai';\nimport { IAICompletionProvider, ItemTextInput, TextVariantResult } from '../interfaces/common-llm-interfaces';\n\nexport class OpenAICompletionProvider implements IAICompletionProvider {\n\tprivate readonly client: OpenAI;\n\tprivate readonly model: string;\n\n\tconstructor(apiKey: string | undefined, model: string = 'gpt-5-mini') {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error('OPENAI_API_KEY is required to construct OpenAICompletionProvider');\n\t\t}\n\t\tthis.client = new OpenAI({ apiKey });\n\t\tthis.model = model;\n\t}\n\n\t// ── Public dispatcher ───────────────────────────────────────────\n\t// Use this as the default entry point. It picks realtime (fast,\n\t// synchronous fan-out) vs batch (cheap, unbounded latency) based\n\t// on item count so callers don't have to decide manually.\n\n\tasync generateSearchVariants(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: {\n\t\t\tbatchThreshold?: number; // item count at/above which we switch to Batch API\n\t\t\tconcurrency?: number; // realtime fan-out concurrency\n\t\t\tmaxRetries?: number; // realtime retry count on 429/500/503\n\t\t\tpollIntervalMs?: number; // batch poll interval\n\t\t\tmaxWaitMs?: number; // batch max wait\n\t\t}\n\t): Promise<TextVariantResult[]> {\n\t\tconst batchThreshold = options?.batchThreshold ?? 300;\n\t\tconst mode = items.length >= batchThreshold ? 'batch' : 'realtime';\n\n\t\tconsole.log('generatePromptVariants: dispatching', {\n\t\t\titemCount: items.length,\n\t\t\tbatchThreshold,\n\t\t\tmode,\n\t\t});\n\n\t\tif (mode === 'batch') {\n\t\t\tconsole.log('Started batch processing generatePromptVariantsBatch');\n\t\t\treturn this.generatePromptVariantsBatch(items, model, instructions, {\n\t\t\t\tpollIntervalMs: options?.pollIntervalMs,\n\t\t\t\tmaxWaitMs: options?.maxWaitMs,\n\t\t\t});\n\t\t}\n\n\t\tconsole.log('Started realtime processing generatePromptVariantsRealtime');\n\t\treturn this.generatePromptVariantsRealtime(items, model, instructions, {\n\t\t\tconcurrency: options?.concurrency,\n\t\t\tmaxRetries: options?.maxRetries,\n\t\t});\n\t}\n\n\t// ── Realtime path (fast) ────────────────────────────────────────\n\t// Direct synchronous calls, fanned out with bounded concurrency.\n\t// Use this whenever you need the result back in the same request/\n\t// job — seconds to low minutes for hundreds of items, not hours.\n\n\tasync generatePromptVariantsRealtime(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { concurrency?: number; maxRetries?: number }\n\t): Promise<TextVariantResult[]> {\n\t\tconst concurrency = options?.concurrency ?? 10;\n\t\tconst maxRetries = options?.maxRetries ?? 3;\n\n\t\ttry {\n\t\t\tif (items.length === 0) return [];\n\n\t\t\tconst { fields, instructions: defaultInstructions } = this.buildVariantRequest();\n\t\t\tconst finalInstructions = instructions ?? defaultInstructions;\n\t\t\tconst finalModel = model ?? this.model;\n\t\t\tconst schema = this.buildJsonSchema(fields);\n\n\t\t\tconsole.log('generatePromptVariantsRealtime: starting', {\n\t\t\t\titemCount: items.length,\n\t\t\t\tfinalModel,\n\t\t\t\tconcurrency,\n\t\t\t});\n\n\t\t\tconst runOne = async ({ itemId, text }: ItemTextInput, attempt = 1): Promise<TextVariantResult> => {\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await this.client.responses.create({\n\t\t\t\t\t\tmodel: finalModel,\n\t\t\t\t\t\tinstructions: finalInstructions,\n\t\t\t\t\t\tinput: text,\n\t\t\t\t\t\ttext: {\n\t\t\t\t\t\t\tformat: {\n\t\t\t\t\t\t\t\ttype: 'json_schema',\n\t\t\t\t\t\t\t\tname: 'text_variants',\n\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\tstrict: true,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\n\t\t\t\t\tconst outputText =\n\t\t\t\t\t\t(response as any).output_text ??\n\t\t\t\t\t\t(response as any).output\n\t\t\t\t\t\t\t?.find((o: any) => o.type === 'message')\n\t\t\t\t\t\t\t?.content?.find((c: any) => c.type === 'output_text')?.text;\n\n\t\t\t\t\tif (!outputText) {\n\t\t\t\t\t\tthrow new Error('empty output_text in response');\n\t\t\t\t\t}\n\n\t\t\t\t\tlet parsed: Record<string, unknown>;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tparsed = JSON.parse(outputText);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`generatePromptVariantsRealtime: failed to parse output for itemId=${itemId}`,\n\t\t\t\t\t\t\toutputText\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t\t}\n\n\t\t\t\t\treturn { itemId, original: text, ...parsed };\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\tconst status = error?.status ?? error?.response?.status;\n\t\t\t\t\tconst isRetryable = status === 429 || status === 500 || status === 503;\n\n\t\t\t\t\tif (isRetryable && attempt <= maxRetries) {\n\t\t\t\t\t\tconst backoffMs = 1000 * 2 ** attempt; // 2s, 4s, 8s...\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t`generatePromptVariantsRealtime: retryable error (status=${status}) for itemId=${itemId}, attempt ${attempt}/${maxRetries}, retrying in ${backoffMs}ms`\n\t\t\t\t\t\t);\n\t\t\t\t\t\tawait new Promise(resolve => setTimeout(resolve, backoffMs));\n\t\t\t\t\t\treturn runOne({ itemId, text }, attempt + 1);\n\t\t\t\t\t}\n\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`generatePromptVariantsRealtime: failed for itemId=${itemId} after ${attempt} attempt(s)`,\n\t\t\t\t\t\terror\n\t\t\t\t\t);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst results: TextVariantResult[] = [];\n\t\t\tfor (let i = 0; i < items.length; i += concurrency) {\n\t\t\t\tconst chunk = items.slice(i, i + concurrency);\n\t\t\t\tconst chunkResults = await Promise.all(chunk.map(item => runOne(item)));\n\t\t\t\tresults.push(...chunkResults);\n\t\t\t\tconsole.log(\n\t\t\t\t\t`generatePromptVariantsRealtime: progress ${Math.min(i + concurrency, items.length)}/${items.length}`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst failedCount = results.filter(r => Object.keys(r).length <= 2).length;\n\t\t\tconsole.log('generatePromptVariantsRealtime: done', {\n\t\t\t\ttotal: items.length,\n\t\t\t\tfailed: failedCount,\n\t\t\t});\n\n\t\t\treturn results;\n\t\t} catch (error) {\n\t\t\tconsole.error('generatePromptVariantsRealtime failed', {\n\t\t\t\terror,\n\t\t\t\titemCount: items.length,\n\t\t\t\tmodel,\n\t\t\t});\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// ── Batch path (cheap, slow, unbounded latency) ─────────────────\n\t// Only use directly for large (1000+), non-urgent jobs kicked off\n\t// from a background worker/cron — not from a request/response path.\n\tasync generatePromptVariantsBatch(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { pollIntervalMs?: number; maxWaitMs?: number }\n\t): Promise<TextVariantResult[]> {\n\t\ttry {\n\t\t\tconsole.log({ items, model, instructions });\n\n\t\t\tconst { fields, instructions: defaultInstructions } = this.buildVariantRequest();\n\t\t\tconst finalInstructions = instructions ?? defaultInstructions;\n\t\t\tconst finalModel = model ?? this.model;\n\t\t\t\n\t\t\tconsole.log(`You are using llm provider model: ${finalModel}`);\n\n\t\t\tconst schema = this.buildJsonSchema(fields);\n\n\t\t\tif (items.length > 50_000) {\n\t\t\t\tthrow new Error('Batch API supports max 50,000 requests per batch — chunk your items first.');\n\t\t\t}\n\n\t\t\t// 1. Build JSONL\n\t\t\tconst jsonl = items\n\t\t\t\t.map(({ itemId, text }) =>\n\t\t\t\t\tJSON.stringify({\n\t\t\t\t\t\tcustom_id: itemId,\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\turl: '/v1/responses',\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tmodel: finalModel,\n\t\t\t\t\t\t\tinstructions: finalInstructions,\n\t\t\t\t\t\t\tinput: text,\n\t\t\t\t\t\t\ttext: {\n\t\t\t\t\t\t\t\tformat: {\n\t\t\t\t\t\t\t\t\ttype: 'json_schema',\n\t\t\t\t\t\t\t\t\tname: 'text_variants',\n\t\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\t\tstrict: true,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t.join('\\n');\n\n\t\t\t// 2. Upload input file\n\t\t\tconst inputFile = await this.client.files.create({\n\t\t\t\tfile: await toFile(Buffer.from(jsonl, 'utf-8'), 'batch-input.jsonl'),\n\t\t\t\tpurpose: 'batch',\n\t\t\t});\n\n\t\t\t// 3. Create batch job\n\t\t\tlet batch = await this.client.batches.create({\n\t\t\t\tinput_file_id: inputFile.id,\n\t\t\t\tendpoint: '/v1/responses',\n\t\t\t\tcompletion_window: '24h',\n\t\t\t});\t\t\t\n\n\t\t\t// 4. Poll until done\n\t\t\tbatch = await this.pollBatch(batch.id, options?.pollIntervalMs, options?.maxWaitMs);\n\n\t\t\tif (batch.status !== 'completed') {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Batch ${batch.id} ended with status \"${batch.status}\" (${batch.request_counts?.failed ?? 0} failed)`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 5. Parse output\n\t\t\tconst outputById = await this.readJsonlFile(batch.output_file_id);\n\t\t\tconst errorsById = await this.readJsonlFile(batch.error_file_id);\t\t\t\n\n\t\t\t// 6. Merge results\n\t\t\treturn items.map(({ itemId, text }) => {\n\t\t\t\tconst record = outputById.get(itemId);\n\t\t\t\tconst body = record?.response?.body;\n\n\t\t\t\tconst outputText =\n\t\t\t\t\tbody?.output_text ??\n\t\t\t\t\tbody?.output\n\t\t\t\t\t\t?.find((o: any) => o.type === 'message')\n\t\t\t\t\t\t?.content?.find((c: any) => c.type === 'output_text')?.text;\n\n\t\t\t\tif (!outputText) {\n\t\t\t\t\tconst err = errorsById.get(itemId);\n\t\t\t\t\tconsole.error(`generatePromptVariantsBatch: no output for itemId=${itemId}`, err ?? record?.error);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\treturn { itemId, original: text, ...JSON.parse(outputText) };\n\t\t\t\t} catch {\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`generatePromptVariantsBatch: failed to parse output for itemId=${itemId}`,\n\t\t\t\t\t\toutputText\n\t\t\t\t\t);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error('generatePromptVariantsBatch failed', {\n\t\t\t\terror,\n\t\t\t\titemCount: items.length,\n\t\t\t\tmodel,\n\t\t\t});\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// ── Helpers ──────────────────────────────────────────────────────\n\n\tprivate buildVariantRequest(): {\n\t\tfields: (keyof TextVariantResult)[];\n\t\tinstructions: string;\n\t} {\n\t\ttype GeneratedField = Exclude<keyof TextVariantResult, 'original' | 'itemId'>;\n\n\t\tconst fieldDescriptions: Record<GeneratedField, string> = {\n\t\t\tarabic:\n\t\t\t\t'The Arabic translation of the word/phrase, written in Arabic script (e.g. \"زيتون\" for \"olive\"). Provide 1-3 common variants if applicable.',\n\t\t\tarabizi:\n\t\t\t\t'The Arabic word\\'s meaning, written phonetically in Latin script/numerals — i.e. Arabizi (e.g. \"zaytoon\"). Provide 1-3 common spelling variants.',\n\t\t\tarabicTransliteration:\n\t\t\t\t'The original word spelled phonetically in Arabic script, NOT translated (e.g. \"أوليف\" for \"olive\"). Useful when there is no native Arabic equivalent.',\n\t\t};\n\n\t\tconst fields = Object.keys(fieldDescriptions) as GeneratedField[];\n\n\t\tconst instructions = [\n\t\t\t'You generate search-name variants for a food delivery app product/shop name.',\n\t\t\t'For the given input text, return ONLY the following fields as arrays of strings:',\n\t\t\t...fields.map(f => `- ${f}: ${fieldDescriptions[f]}`),\n\t\t\t'Return strict JSON matching the schema. No extra commentary, no extra fields.',\n\t\t].join('\\n');\n\n\t\treturn { fields, instructions };\n\t}\n\n\tprivate async pollBatch(\n\t\tbatchId: string,\n\t\tintervalMs = 5000,\n\t\tmaxWaitMs = 24 * 60 * 60 * 1000\n\t): Promise<OpenAI.Batches.Batch> {\n\t\tconst terminal = new Set(['completed', 'failed', 'expired', 'cancelled']);\n\t\tconst start = Date.now();\n\n\t\twhile (true) {\n\t\t\tconst batch = await this.client.batches.retrieve(batchId);\n\t\t\tconsole.log(\n\t\t\t\t`pollBatch: ${batchId} status=${batch.status} elapsed=${Math.round((Date.now() - start) / 1000)}s`\n\t\t\t);\n\t\t\tif (terminal.has(batch.status)) return batch;\n\n\t\t\tif (Date.now() - start > maxWaitMs) {\n\t\t\t\tthrow new Error(`Batch ${batchId} did not finish within ${maxWaitMs}ms (status: ${batch.status})`);\n\t\t\t}\n\n\t\t\tawait new Promise(resolve => setTimeout(resolve, intervalMs));\n\t\t}\n\t}\n\n\tprivate async readJsonlFile(fileId: string | null | undefined): Promise<Map<string, any>> {\n\t\tconst result = new Map<string, any>();\n\t\tif (!fileId) return result;\n\n\t\tconst fileResponse = await this.client.files.content(fileId);\n\t\tconst text = await fileResponse.text();\n\n\t\tfor (const line of text.split('\\n').filter(Boolean)) {\n\t\t\tconst record = JSON.parse(line);\n\t\t\tresult.set(record.custom_id, record);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate buildJsonSchema(fields: (keyof TextVariantResult)[]) {\n\t\tconst properties: Record<string, any> = {};\n\t\tfor (const field of fields) {\n\t\t\tproperties[field] = { type: 'array', items: { type: 'string' } };\n\t\t}\n\n\t\treturn {\n\t\t\ttype: 'object',\n\t\t\tproperties,\n\t\t\trequired: fields,\n\t\t\tadditionalProperties: false,\n\t\t};\n\t}\n}\n"]}
@@ -22,7 +22,7 @@ Perfect for sharing types between frontend and backend applications.
22
22
 
23
23
  ## Version
24
24
 
25
- Version: 1.4.283
25
+ Version: 1.4.285
26
26
 
27
27
  ## Dependencies
28
28
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lyxa.ai/types",
3
- "version": "1.4.283",
3
+ "version": "1.4.285",
4
4
  "description": "Lyxa type definitions and validation schemas for both frontend and backend",
5
5
  "author": "elie <42282499+Internalizable@users.noreply.github.com>",
6
6
  "license": "MIT",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lyxa.ai/core",
3
- "version": "1.4.283",
3
+ "version": "1.4.285",
4
4
  "description": "The Core system of the Lyxa services.",
5
5
  "author": "elie <42282499+Internalizable@users.noreply.github.com>",
6
6
  "license": "MIT",