1import * as GCP from "@/GCP";
2import * as Effect from "effect/Effect";
3import { serveProbes } from "../../bindingHost.ts";
4
5const location = "us-central1";
6
7/**
8 * Model bindings need a trained model: training takes hours, so
9 * `GCP_TEST_TRANSLATE_MODEL=1` plus `GCP_TEST_TRANSLATE_MODEL_DATASET` (a
10 * dataset with imported sentence pairs) opt in. The gate values are
11 * forwarded to the host's environment so the deployed runtime binds the
12 * same set.
13 */
14export const modelDataset =
15 !process.env.FAST && process.env.GCP_TEST_TRANSLATE_MODEL === "1"
16 ? (process.env.GCP_TEST_TRANSLATE_MODEL_DATASET ?? "")
17 : "";
18
19/**
20 * Glossary-entry bindings need an existing glossary:
21 * `GCP_TEST_TRANSLATE_GLOSSARY=1` plus `GCP_TEST_TRANSLATE_GLOSSARY_NAME`.
22 */
23export const glossaryName =
24 !process.env.FAST && process.env.GCP_TEST_TRANSLATE_GLOSSARY === "1"
25 ? (process.env.GCP_TEST_TRANSLATE_GLOSSARY_NAME ?? "")
26 : "";
27
28/** Adaptive MT dataset (no sentence pairs) the dataset bindings bind. */
29export const Dataset = GCP.Translate.AdaptiveMtDataset("BindEnEs", {
30 location,
31 sourceLanguageCode: "en",
32 targetLanguageCode: "es",
33 displayName: "bindenes",
34});
35
36/** Custom model; declared only when {@link modelDataset} is set. */
37export const EnEs = GCP.Translate.Model("EnEs", {
38 location,
39 dataset: modelDataset,
40 displayName: "enes",
41});
42
43/** Glossary entry; declared only when {@link glossaryName} is set. */
44export const Hello = GCP.Translate.GlossariesGlossaryEntry("Hello", {
45 parent: glossaryName,
46 location,
47 description: "greeting",
48 termsPair: {
49 sourceTerm: { languageCode: "en", text: "hello" },
50 targetTerm: { languageCode: "es", text: "hola" },
51 },
52});
53
54const modelProbes = Effect.gen(function* () {
55 const getModel = yield* GCP.Translate.GetModel(EnEs);
56 const translateText = yield* GCP.Translate.TranslateText(EnEs);
57 return {
58 getModel: getModel(),
59 translateText: translateText({
60 body: {
61 contents: ["Hello, world"],
62 targetLanguageCode: "es",
63 sourceLanguageCode: "en",
64 mimeType: "text/plain",
65 },
66 }),
67 };
68});
69
70const glossaryProbes = Effect.gen(function* () {
71 const getEntry = yield* GCP.Translate.GetGlossariesGlossaryEntry(Hello);
72 return { getGlossaryEntry: getEntry() };
73});
74
75/**
76 * Effect-native Cloud Run service exercising every Translate binding as its
77 * own runtime service account. Deployed from {@link ../Bindings.test.ts}.
78 */
79export default class TranslateBindingsHost extends GCP.Function<TranslateBindingsHost>()(
80 "TranslateBindingsHost",
81 {
82 main: import.meta.url,
83 invokerIamDisabled: true,
84 env: {
85 GCP_TEST_TRANSLATE_MODEL: modelDataset ? "1" : "",
86 GCP_TEST_TRANSLATE_MODEL_DATASET: modelDataset,
87 GCP_TEST_TRANSLATE_GLOSSARY: glossaryName ? "1" : "",
88 GCP_TEST_TRANSLATE_GLOSSARY_NAME: glossaryName,
89 },
90 },
91 Effect.gen(function* () {
92 const getDataset = yield* GCP.Translate.GetAdaptiveMtDataset(Dataset);
93 const adaptiveTranslate = yield* GCP.Translate.AdaptiveMtTranslate(Dataset);
94 const model = modelDataset ? yield* modelProbes : {};
95 const glossary = glossaryName ? yield* glossaryProbes : {};
96
97 return {
98 fetch: serveProbes({
99 getAdaptiveMtDataset: getDataset(),
100 adaptiveMtTranslate: adaptiveTranslate({
101 body: { content: ["hello"] },
102 }),
103 ...model,
104 ...glossary,
105 }),
106 };
107 }).pipe(
108 Effect.provide(GCP.Translate.GetAdaptiveMtDatasetHttp),
109 Effect.provide(GCP.Translate.AdaptiveMtTranslateHttp),
110 Effect.provide(GCP.Translate.GetModelHttp),
111 Effect.provide(GCP.Translate.TranslateTextHttp),
112 Effect.provide(GCP.Translate.GetGlossariesGlossaryEntryHttp),
113 ),
114) {}