Files
calendar/packages/lib/server/service/replexica.ts
T
62976a4c77 feat: AI description - Backend for adding translation objects to EventType model (#17657)
* feat: AI description - DB model + frontend + backend (fetch only)

* fix types and add validation to backend

* improve log

* improve

* import type

* add EventTypeTranslationRepository

* fix replexica error

* fix replexica error

* fix

* fix test

* wip

* wip

* add createManyDescriptionTranslations method

* improve logic

* refactor

* update replexica type

* improve typing

* Renamed descriptionTranslations to fieldTranslations

* log errors in replexica service

* handle translations in background

* don't create, upsert instead

* make sure that description is updated

* simplify

* fix

* Update packages/trpc/server/routers/viewer/eventTypes/update.handler.ts

* improve filter logic in frontend

* implement background job using Tasker

* update schema

* update schema again

* rename

* rename id to uid

* add migration

* fix type

* update

* update schema and migration sql

* fix migration

* fix type check

* fix type check 2

* improve find logic in EventMeta

* add more validation

* add logger

* rename

* upgrade replexica/sdk to 0.7.0

* upgrade replexica to 0.7.0

* use batchLocalizeText

* add comments

* override browser locale if user is a signed in calcom user

* fix type in replexica

* fix migration sql

* fix type

* use dynamic import

* do not use batchLocalizeText

* fix

* add more validations

* fix type error

* address comment

* update locales

* update locales

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
2024-11-25 18:37:42 +00:00

94 lines
2.5 KiB
TypeScript

import { ReplexicaEngine } from "@replexica/sdk";
import type { LocaleCode } from "@replexica/spec";
import { REPLEXICA_API_KEY } from "@calcom/lib/constants";
import logger from "@calcom/lib/logger";
export class ReplexicaService {
private static engine = new ReplexicaEngine({
apiKey: REPLEXICA_API_KEY,
});
/**
* Localizes text from one language to another
* @param text The text to localize
* @param sourceLocale The source language locale
* @param targetLocale The target language locale
* @returns The localized text
*/
static async localizeText(
text: string,
sourceLocale: string,
targetLocale: string
): Promise<string | null> {
if (!text?.trim()) {
return null;
}
try {
const result = await this.engine.localizeText(text, {
sourceLocale,
targetLocale,
});
return result;
} catch (error) {
logger.error(`ReplexicaService.localizeText() failed for targetLocale: ${targetLocale} - ${error}`);
return null;
}
}
/**
* Localize a text string to multiple target locales
* @param text The text to localize
* @param sourceLocale The source language locale
* @param targetLocales Array of the target language locales
* @returns The localized texts
*/
static async batchLocalizeText(
text: string,
sourceLocale: string,
targetLocales: string[]
): Promise<string[]> {
try {
const result = await this.engine.batchLocalizeText(text, {
sourceLocale: sourceLocale as LocaleCode,
targetLocales: targetLocales as LocaleCode[],
});
return result;
} catch (error) {
logger.error(`ReplexicaService.batchLocalizeText() failed: ${error}`);
return [];
}
}
/**
* Localizes an array of texts from one language to another
* @param texts Array of texts to localize
* @param sourceLocale The source language locale
* @param targetLocale The target language locale
* @returns The localized texts array
*/
static async localizeTexts(texts: string[], sourceLocale: string, targetLocale: string): Promise<string[]> {
if (!texts.length) {
return texts;
}
try {
const result = await this.engine.localizeChat(
texts.map((text) => ({ name: "NO_NAME", text: text.trim() })),
{
sourceLocale,
targetLocale,
}
);
return result.map((chat: { name: string; text: string }) => chat.text);
} catch (error) {
logger.error(`ReplexicaService.localizeTexts() failed: ${error}`);
return texts;
}
}
}