Skip to content

ExecutionPlan

8 exports Added in v3.16.0 Source

Combining

merge

Added in v3.16.0 Source

Signature

declare function merge<Plans extends readonly [ExecutionPlan<any>, ExecutionPlan<any>]>(
  ...plans: Plans
): ExecutionPlan<{
  error: Plans[number] extends ExecutionPlan<T> ? T["error"] : never;
  input: PlanInput<Plans>;
  provides: PlanProvides<Plans>;
  requirements: Plans[number] extends ExecutionPlan<T> ? T["requirements"] : never;
}>;

Constructors

make

Added in v3.16.0 Source

Create an ExecutionPlan, which can be used with Effect.withExecutionPlan or Stream.withExecutionPlan, allowing you to provide different resources for each step of execution until the effect succeeds or the plan is exhausted.

Signature

declare function make<Steps extends readonly [Step, Step]>(
  ...steps: Steps & { [K in string | number | symbol]: Step }
): ExecutionPlan<{
  error: Steps[number]["provide"] extends Context<_P> | Layer<_P, E, _R>
    ? E
    : never | Steps[number]["while"] extends (input: _I) => Effect<_A, _E, _R>
      ? _E
      : never;
  input: StepInput<Steps>;
  provides: StepProvides<Steps>;
  requirements: Steps[number]["provide"] extends Layer<_A, _E, R>
    ? R
    : never | Steps[number]["while"] extends (input: _I) => Effect<_A, _E, R>
      ? R
      : never | Steps[number]["schedule"] extends Schedule<_O, _I, R>
        ? R
        : never;
}>;

Example

import type { LanguageModel } from "@effect/ai"
import type { Layer } from "effect"
import { Effect, ExecutionPlan, Schedule } from "effect"

declare const layerBad: Layer.Layer<LanguageModel.LanguageModel>
declare const layerGood: Layer.Layer<LanguageModel.LanguageModel>

const ThePlan = ExecutionPlan.make(
  {
    // First try with the bad layer 2 times with a 3 second delay between attempts
    provide: layerBad,
    attempts: 2,
    schedule: Schedule.spaced(3000),
  },
  // Then try with the bad layer 3 times with a 1 second delay between attempts
  {
    provide: layerBad,
    attempts: 3,
    schedule: Schedule.spaced(1000),
  },
  // Finally try with the good layer.
  //
  // If `attempts` is omitted, the plan will only attempt once, unless a schedule is provided.
  {
    provide: layerGood,
  },
)

declare const effect: Effect.Effect<void, never, LanguageModel.LanguageModel>
const withPlan: Effect.Effect<void> = Effect.withExecutionPlan(effect, ThePlan)

Guards

Signature

declare const isExecutionPlan: (u: unknown) => u is ExecutionPlan<any>;

Models

ExecutionPlan interface

Added in v3.16.0 Source

A ExecutionPlan can be used with Effect.withExecutionPlan or Stream.withExecutionPlan, allowing you to provide different resources for each step of execution until the effect succeeds or the plan is exhausted.

Signature

interface ExecutionPlan<
  Types extends {
    error: any;
    input: any;
    provides: any;
    requirements: any;
  },
> extends Pipeable {
  readonly [TypeId]: typeof TypeId;
  readonly steps: readonly [
    {
      readonly attempts?: number;
      readonly provide:
        | Context<Types["provides"]>
        | Layer<Types["provides"], Types["error"], Types["requirements"]>;
      readonly schedule?: Schedule<any, Types["input"], Types["requirements"]>;
      readonly while?: (
        input: Types["input"],
      ) => Effect<boolean, Types["error"], Types["requirements"]>;
    },
    {
      readonly attempts?: number;
      readonly provide:
        | Context<Types["provides"]>
        | Layer<Types["provides"], Types["error"], Types["requirements"]>;
      readonly schedule?: Schedule<any, Types["input"], Types["requirements"]>;
      readonly while?: (
        input: Types["input"],
      ) => Effect<boolean, Types["error"], Types["requirements"]>;
    },
  ];
  readonly withRequirements: Effect<
    ExecutionPlan<{
      error: Types["error"];
      input: Types["input"];
      provides: Types["provides"];
      requirements: never;
    }>,
    never,
    Types["requirements"]
  >;
}

Example

import type { LanguageModel } from "@effect/ai"
import type { Layer } from "effect"
import { Effect, ExecutionPlan, Schedule } from "effect"

declare const layerBad: Layer.Layer<LanguageModel.LanguageModel>
declare const layerGood: Layer.Layer<LanguageModel.LanguageModel>

const ThePlan = ExecutionPlan.make(
  {
    // First try with the bad layer 2 times with a 3 second delay between attempts
    provide: layerBad,
    attempts: 2,
    schedule: Schedule.spaced(3000),
  },
  // Then try with the bad layer 3 times with a 1 second delay between attempts
  {
    provide: layerBad,
    attempts: 3,
    schedule: Schedule.spaced(1000),
  },
  // Finally try with the good layer.
  //
  // If `attempts` is omitted, the plan will only attempt once, unless a schedule is provided.
  {
    provide: layerGood,
  },
)

declare const effect: Effect.Effect<void, never, LanguageModel.LanguageModel>
const withPlan: Effect.Effect<void> = Effect.withExecutionPlan(effect, ThePlan)

Other

make

Added in v3.16.0 Source

TypesBase type

Added in v3.16.0 Source

Signature

type TypesBase = {
  error: any;
  input: any;
  provides: any;
  requirements: any;
};

Symbols

TypeId

Added in v3.16.0 Source

Signature

declare const TypeId: unique symbol;

TypeId type

Added in v3.16.0 Source

Signature

type TypeId = typeof TypeId;