Implementing Recursive Services in Effect Typescript

Are recursive services possible? For example in the service default implementation below, can it yield itself in the delegateStep function, and provide a different TaskConfig layer?

Alternatively I could of course not use a layer for task config, and just pass it into solve directly. Just continuing to experiment and try to get my head around how all of these effect DI primitives can be used and combined.

interface TaskConfigObj {
  id: string;
  agentId: string;
  steps: TaskConfigObj[];
}

class TaskConfig extends Context.Tag('Task/config')<TaskConfig, TaskConfigObj>() {}

class Task extends Effect.Service<Task>()('Task', {
  effect: Effect.gen(function* () {
    const { id, agentId, steps } = yield* TaskConfig;

    const solve = Effect.gen(function* () {
      for (const step of steps) {
        const isOwnStep = !step.agentId || step.agentId === agentId;
        yield* isOwnStep ? solveOwnStep(step) : delegateStep(step);
      }
    });

    const solveOwnStep = Effect.fn(function* (step: TaskConfigObj) {
      yield* Effect.log(`Agent ${agentId} is solving own step`);
    });

    const delegateStep = Effect.fn(function* (step: TaskConfigObj) {
      yield* Effect.log(`Agent ${agentId} is delegating step`);

      /**
       * Here I'd need to instantiate another task with task config of "step",
       * and yield a call to solve
       */
    });

    return {
      id,
      solve,
    };
  }),
}) {}
Was this page helpful?