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 <[email protected]>
This commit is contained in:
1 parent
93489709a8
commit
62976a4c77
14 files changed
+290
-22
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { m } from "framer-motion";
|
||||
import { useSession } from "next-auth/react";
|
||||
import dynamic from "next/dynamic";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { shallow } from "zustand/shallow";
|
||||
@@ -11,6 +12,8 @@ import { EventMetaBlock } from "@calcom/features/bookings/components/event-meta/
|
||||
import { useTimePreferences } from "@calcom/features/bookings/lib";
|
||||
import type { BookerEvent } from "@calcom/features/bookings/types";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { locales as i18nLocales } from "@calcom/lib/i18n";
|
||||
import { EventTypeAutoTranslatedField } from "@calcom/prisma/enums";
|
||||
|
||||
import { fadeInUp } from "../config";
|
||||
import { useBookerStore } from "../store";
|
||||
@@ -59,6 +62,7 @@ export const EventMeta = ({
|
||||
eventMetaTimezoneSelect?: string;
|
||||
};
|
||||
}) => {
|
||||
const session = useSession();
|
||||
const { setTimezone, timeFormat, timezone } = useTimePreferences();
|
||||
const selectedDuration = useBookerStore((state) => state.selectedDuration);
|
||||
const selectedTimeslot = useBookerStore((state) => state.selectedTimeslot);
|
||||
@@ -103,9 +107,13 @@ export const EventMeta = ({
|
||||
: isHalfFull
|
||||
? "text-yellow-500"
|
||||
: "text-bookinghighlight";
|
||||
const browserLocale = navigator.language; // e.g. "en-US", "es-ES", "fr-FR"
|
||||
const translatedDescription = (event?.fieldTranslations ?? []).find((translation) =>
|
||||
browserLocale.startsWith(translation.targetLang)
|
||||
const userLocale = session.data?.user.locale ?? navigator.language;
|
||||
const translatedDescription = (event?.fieldTranslations ?? []).find(
|
||||
(trans) =>
|
||||
trans.field === EventTypeAutoTranslatedField.DESCRIPTION &&
|
||||
i18nLocales.includes(trans.targetLocale) &&
|
||||
// browser language looks like "en-US", "es-ES", "fr-FR", etc
|
||||
(userLocale === trans.targetLocale || userLocale.split("-")[0] === trans.targetLocale)
|
||||
)?.translatedText;
|
||||
|
||||
return (
|
||||
|
||||
@@ -71,7 +71,8 @@ const publicEventSelect = Prisma.validator<Prisma.EventTypeSelect>()({
|
||||
fieldTranslations: {
|
||||
select: {
|
||||
translatedText: true,
|
||||
targetLang: true,
|
||||
targetLocale: true,
|
||||
field: true,
|
||||
},
|
||||
},
|
||||
requiresBookerEmailVerification: true,
|
||||
|
||||
@@ -14,6 +14,9 @@ type TaskPayloads = {
|
||||
triggerFormSubmittedNoEventWebhook: z.infer<
|
||||
typeof import("./tasks/triggerFormSubmittedNoEvent/triggerFormSubmittedNoEventWebhook").ZTriggerFormSubmittedNoEventWebhookPayloadSchema
|
||||
>;
|
||||
translateEventTypeDescription: z.infer<
|
||||
typeof import("./tasks/translateEventTypeDescription").ZTranslateEventTypeDescriptionPayloadSchema
|
||||
>;
|
||||
};
|
||||
export type TaskTypes = keyof TaskPayloads;
|
||||
export type TaskHandler = (payload: string) => Promise<void>;
|
||||
|
||||
@@ -17,6 +17,8 @@ const tasks: Record<TaskTypes, () => Promise<TaskHandler>> = {
|
||||
(module) => module.triggerFormSubmittedNoEventWebhook
|
||||
),
|
||||
sendSms: () => Promise.resolve(() => Promise.reject(new Error("Not implemented"))),
|
||||
translateEventTypeDescription: () =>
|
||||
import("./translateEventTypeDescription").then((module) => module.translateEventTypeDescription),
|
||||
};
|
||||
|
||||
export default tasks;
|
||||
@@ -0,0 +1,77 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { locales as i18nLocales } from "@calcom/lib/i18n";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { EventTypeTranslationRepository } from "@calcom/lib/server/repository/eventTypeTranslation";
|
||||
|
||||
export const ZTranslateEventTypeDescriptionPayloadSchema = z.object({
|
||||
eventTypeId: z.number(),
|
||||
userId: z.number(),
|
||||
description: z.string(),
|
||||
userLocale: z.string(),
|
||||
});
|
||||
|
||||
const SUPPORTED_LOCALES = [
|
||||
"en", // English
|
||||
"es", // Spanish
|
||||
"de", // German
|
||||
"pt", // Portuguese
|
||||
"pt-BR", // Portuguese Brazilian
|
||||
"fr", // French
|
||||
"it", // Italian
|
||||
"ar", // Arabic
|
||||
"ru", // Russian
|
||||
"zh-CN", // Simplified Chinese
|
||||
"nl", // Dutch
|
||||
"zh-TW", // Traditional Chinese
|
||||
"ko", // Korean
|
||||
"ja", // Japanese
|
||||
"sv", // Swedish
|
||||
"da", // Danish
|
||||
] as const;
|
||||
|
||||
export async function translateEventTypeDescription(payload: string): Promise<void> {
|
||||
const { eventTypeId, description, userLocale, userId } = ZTranslateEventTypeDescriptionPayloadSchema.parse(
|
||||
JSON.parse(payload)
|
||||
);
|
||||
|
||||
const targetLocales = SUPPORTED_LOCALES.filter(
|
||||
(locale) => locale !== userLocale && i18nLocales.includes(locale)
|
||||
);
|
||||
|
||||
const { ReplexicaService } = await import("@calcom/lib/server/service/replexica");
|
||||
try {
|
||||
const translatedDescriptions = await Promise.all(
|
||||
targetLocales.map((targetLocale) =>
|
||||
ReplexicaService.localizeText(description, userLocale, targetLocale)
|
||||
)
|
||||
);
|
||||
|
||||
const validTranslations = translatedDescriptions
|
||||
.map((translatedText, index) => ({
|
||||
translatedText,
|
||||
targetLocale: targetLocales[index],
|
||||
}))
|
||||
.filter(
|
||||
(item): item is { translatedText: string; targetLocale: (typeof SUPPORTED_LOCALES)[number] } =>
|
||||
item.translatedText !== null
|
||||
);
|
||||
|
||||
if (validTranslations.length > 0) {
|
||||
await EventTypeTranslationRepository.upsertManyDescriptionTranslations(
|
||||
validTranslations.map(({ translatedText, targetLocale }) => ({
|
||||
eventTypeId,
|
||||
sourceLocale: userLocale,
|
||||
targetLocale,
|
||||
// Keep old fields during transition
|
||||
sourceLang: userLocale, // TODO: remove after migration
|
||||
targetLang: targetLocale, // TODO: remove after migration
|
||||
translatedText,
|
||||
userId,
|
||||
}))
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error("Failed to process event type description translations:", error);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user