Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
"use server";
import { db } from "@cap/database";
import { videos } from "@cap/database/schema";
import { provideOptionalAuth, Storage, VideosPolicy } from "@cap/web-backend";
import { Policy, type Video } from "@cap/web-domain";
import { generateText } from "ai";
import { eq } from "drizzle-orm";
import { Effect, Exit, Option } from "effect";
import { isAiConfigured } from "@/lib/ai/provider";
import { runWithAiProviders } from "@/lib/ai/run";
import { isRateLimited, RATE_LIMIT_IDS } from "@/lib/rate-limit";
import * as EffectRuntime from "@/lib/server";
import { runPromise } from "@/lib/server";
import { decodeStorageVideo } from "@/lib/video-storage";
import {
type LanguageCode,
SUPPORTED_LANGUAGES,
} from "./translation-languages";
import {
isCompleteTranslation,
joinTranslationChunks,
readCachedTranslation,
splitTranslationChunks,
withSourceFingerprint,
} from "./translation-vtt";
interface TranslateResult {
success: boolean;
translatedVtt?: string;
message: string;
}
export async function translateTranscript(
videoId: Video.VideoId,
targetLanguage: LanguageCode,
): Promise<TranslateResult> {
if (!videoId || !targetLanguage) {
return {
success: false,
message: "Missing required parameters",
};
}
if (!SUPPORTED_LANGUAGES[targetLanguage]) {
return {
success: false,
message: "Unsupported language",
};
}
if (!isAiConfigured()) {
return {
success: false,
message: "Translation service not configured",
};
}
if (await isRateLimited(RATE_LIMIT_IDS.TRANSLATE_TRANSCRIPT)) {
return { success: false, message: "Too many requests" };
}
const exit = await Effect.gen(function* () {
const videosPolicy = yield* VideosPolicy;
return yield* Effect.promise(() =>
db().select({ video: videos }).from(videos).where(eq(videos.id, videoId)),
).pipe(Policy.withPublicPolicy(videosPolicy.canView(videoId)));
}).pipe(provideOptionalAuth, EffectRuntime.runPromiseExit);
if (Exit.isFailure(exit)) {
return { success: false, message: "Video not found" };
}
const query = exit.value;
if (query.length === 0 || !query[0]?.video) {
return { success: false, message: "Video not found" };
}
const { video } = query[0];
const translatedKey = `${video.ownerId}/${videoId}/transcription.${targetLanguage}.vtt`;
const originalVtt = await Effect.gen(function* () {
const [bucket] = yield* Storage.getAccessForVideo(
decodeStorageVideo(video),
);
return yield* bucket.getObject(
`${video.ownerId}/${videoId}/transcription.vtt`,
);
}).pipe(runPromise);
if (Option.isNone(originalVtt)) {
return { success: false, message: "Original transcript not found" };
}
try {
const existingTranslation = await Effect.gen(function* () {
const [bucket] = yield* Storage.getAccessForVideo(
decodeStorageVideo(video),
);
return yield* bucket.getObject(translatedKey);
}).pipe(runPromise);
const cachedTranslation = Option.isSome(existingTranslation)
? readCachedTranslation(originalVtt.value, existingTranslation.value)
: null;
if (cachedTranslation) {
return {
success: true,
translatedVtt: cachedTranslation,
message: "Retrieved cached translation",
};
}
} catch (e) {
console.debug("[translateTranscript] No cached translation found:", e);
}
const translatedVtt = await translateVttContent(
originalVtt.value,
targetLanguage,
);
if (!translatedVtt) {
return { success: false, message: "Translation failed" };
}
const cacheBody = withSourceFingerprint(originalVtt.value, translatedVtt);
if (cacheBody) {
try {
await Effect.gen(function* () {
const [bucket] = yield* Storage.getAccessForVideo(
decodeStorageVideo(video),
);
yield* bucket.putObject(translatedKey, cacheBody, {
contentType: "text/vtt",
});
}).pipe(runPromise);
} catch (error) {
console.error(
"[translateTranscript] Failed to cache translation:",
error,
);
}
}
return {
success: true,
translatedVtt,
message: "Translation completed",
};
}
async function translateVttContent(
vttContent: string,
targetLanguage: LanguageCode,
): Promise<string | null> {
const targetLanguageName = SUPPORTED_LANGUAGES[targetLanguage];
const chunks = splitTranslationChunks(vttContent);
if (!chunks) return null;
const translations: string[] = [];
for (const chunk of chunks) {
const prompt = `Translate the following WebVTT subtitle file to ${targetLanguageName}.
IMPORTANT RULES:
1. Keep the "WEBVTT" header exactly as is
2. Keep all timestamp lines exactly as they are (e.g., "00:00:01.234 --> 00:00:03.456")
3. Keep all cue numbers exactly as they are
4. Only translate the actual text content on each line
5. Preserve all newlines and formatting
6. Do not add any explanations or comments
7. Return ONLY the translated VTT content
VTT content to translate:
${chunk}`;
try {
const translatedChunk = await runWithAiProviders(
"generation",
async (selection) => {
const response = await generateText({
model: selection.model(),
prompt,
maxOutputTokens: 8000,
...(selection.supportsTemperature ? { temperature: 0.3 } : {}),
});
if (
response.finishReason === "length" ||
!isCompleteTranslation(chunk, response.text)
) {
throw new Error("translation response has missing or changed cues");
}
return response.text.trim();
},
);
translations.push(translatedChunk);
} catch (error) {
console.error("[translateVttContent] Translation error:", error);
return null;
}
}
return joinTranslationChunks(vttContent, chunks, translations);
}