Understanding Effect's Dependency Injection with `provideSystem` and Context Tags

In my continuous exploration of Effect some concepts are starting to click for me.
However, to be completely sure that my understanding is at least right, I decided to try to explain it and hopefully get confirmation or comments correcting what I am wrongly assuming.
Here is my question with a lot of exposition to see if my understanding is correct.
I had the following piece of code, which I took inspiration from Tim's examples to create it (copy-paste then modify it 😂)

const extractShoppingList = (message: string) =>
      Effect.gen(function*() {
        const result = yield* completions.structured({
          input: message,
          schema: ShoppingList,
        });
        return yield* result.value;
      }).pipe(
        AiInput.provideSystem(`Extract the shopping list from the provided message.`),
      );


At certain point I asked myself... how is that provideSystem reaching the inner completions if the effect that results from the completion call is not directly returned in the wrapping effect, and I don't see any "direct" way they can connect? Still, the openAi call was somehow including that system instruction in the payload.

Had a look at the implementation of provideSystem, which was surprisingly simple (removing the complex type annotations and dual wrappers...)

export class SystemInstruction extends Context.Tag("@effect/ai/AiInput/SystemInstruction")<
  SystemInstruction,
  string
>() {}

export const provideSystem = 
  <A, E, R>(effect: Effect.Effect<...>, input: string): Effect.Effect<A, E, Exclude<R, SystemInstruction>> =>
    Effect.provideService(effect, SystemInstruction, input)
)


So all it does is taking any effect, then provide the SystemInstruction service which is just a string service which is the provided string.

continue in thread
Was this page helpful?