Creating a Fallback Utility for Embedding Layers in Effect Typescript

I'm trying to write this utility to quickly add a fallback to my embedding layer, but I'm kind of stuck with Effect ninjitsu here.

Anybody have any ideas?

// This is how I intend to use
export const OpenAIEmbeddings = OpenAiEmbeddingModel.model("text-embedding-3-small", {
    mode: "batched"
}).pipe(Layer.provide(OpenAI), AiUtils.fallbackTo(AzureEmbeddings));


// I'm trying to write this utility
export const fallBackTo =
    <E1, E2, R1, R2>(other: Layer.Layer<AiEmbeddingModel.AiEmbeddingModel, E1, R1>) =>
    (self: Layer.Layer<AiEmbeddingModel.AiEmbeddingModel, E2, R2>) => {
        return Layer.effect(
            AiEmbeddingModel.AiEmbeddingModel,
            Effect.gen(function* () {
                // How do I do this? (doesn't compile)
                const otherModel = yield* Layer.build(other);
                const selfModel = yield* Layer.build(self);
                return {
                    embed: flow((v) => Effect.firstSuccessOf([otherModel.embed(v), selfModel.embed(v)])),
                    embedMany: flow((v) => Effect.firstSuccessOf([otherModel.embedMany(v), selfModel.embedMany(v)]))
                };
            })
        );
    };
Was this page helpful?