<const Steps extends NonEmptyReadonlyArray<make.Step>>(
...steps: Steps & { [K in keyof Steps]: make.Step }
): ExecutionPlan<{
provides: make.StepProvides<Steps>
input: make.StepInput<Steps>
error:
| (Steps[number]["provide"] extends
| Context.Context<infer _P>
| Layer.Layer<infer _P, infer E, infer _R>
? E
: never)
| (Steps[number]["while"] extends (
input: infer _I
) => Effect.Effect<infer _A, infer _E, infer _R>
? _E
: never)
requirements:
| (Steps[number]["provide"] extends Layer.Layer<
infer _A,
infer _E,
infer R
>
? R
: never)
| (Steps[number]["while"] extends (
input: infer _I
) => Effect.Effect<infer _A, infer _E, infer R>
? R
: never)
| (Steps[number]["schedule"] extends Schedule.Schedule<
infer _O,
infer _I,
infer R
>
? R
: never)
}>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.
Example (Creating an execution plan)
import { Effect, ExecutionPlan, Schedule } from "effect"
import type { Layer } from "effect"
import type { LanguageModel } from "effect/unstable/ai"
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)export const const make: <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }) => ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
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.
Example (Creating an execution plan)
import { Effect, ExecutionPlan, Schedule } from "effect"
import type { Layer } from "effect"
import type { LanguageModel } from "effect/unstable/ai"
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)
Namespace containing type helpers used by ExecutionPlan.make.
make = <const function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps extends type NonEmptyReadonlyArray<A> = readonly [A, ...A[]]A readonly array guaranteed to have at least one element.
When to use
Use when non-emptiness must be tracked at the type level while preventing mutation.
Many Array module functions accept or return this type.
Example (Typing a non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyReadonlyArray<number> = [1, 2, 3]
const head: number = nonEmpty[0] // guaranteed to exist
NonEmptyReadonlyArray<make.type make.Step = {
readonly provide: Context.Context<any> | Context.Context<never> | Layer.Any;
readonly attempts?: number | undefined;
readonly while?: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
readonly schedule?: Schedule.Schedule<any, any, any> | undefined;
}
Input shape for a single execution-plan step.
Details
Each step provides a Context or Layer and may limit attempts, add a
while predicate for retry decisions, or attach a Schedule for retry
timing.
Step>>(
...steps: Steps & {
[K in keyof Steps]: make.Step
}
steps: function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps & { [function (type parameter) KK in keyof function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps]: make.type make.Step = {
readonly provide: Context.Context<any> | Context.Context<never> | Layer.Any;
readonly attempts?: number | undefined;
readonly while?: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
readonly schedule?: Schedule.Schedule<any, any, any> | undefined;
}
Input shape for a single execution-plan step.
Details
Each step provides a Context or Layer and may limit attempts, add a
while predicate for retry decisions, or attach a Schedule for retry
timing.
Step }
): interface ExecutionPlan<Config extends { provides: any; input: any; error: any; requirements: any; }>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.
Example (Defining fallback execution steps)
import { Effect, ExecutionPlan, Schedule } from "effect"
import type { Layer } from "effect"
import type { LanguageModel } from "effect/unstable/ai"
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)
ExecutionPlan<{
provides: make.StepProvides<Steps, unknown>provides: make.type make.StepProvides<Steps extends ReadonlyArray<any>, Out = unknown> = Steps extends readonly [infer Step, ...infer Rest] ? make.StepProvides<Rest, Out & (Step extends {
readonly provide: Context.Context<infer P> | Layer.Layer<infer P, infer _E, infer _R>;
} ? P : unknown)> : Out
Computes the intersection of services provided by a list of execution-plan
steps.
StepProvides<function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps>
input: make.StepInput<Steps, unknown>input: make.type make.StepInput<Steps extends ReadonlyArray<any>, Out = unknown> = Steps extends readonly [infer Step, ...infer Rest] ? make.StepInput<Rest, Out & (Step extends {
readonly while: (input: infer I) => infer _;
} ? I : unknown) & (Step extends {
readonly schedule: Schedule.Schedule<infer _O, infer I, infer _R>;
} ? I : unknown)> : Out
Computes the input type consumed by the while predicates and schedules in
a list of execution-plan steps.
StepInput<function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps>
error: | (Steps[number]["provide"] extends
| Context.Context<infer _P>
| Layer.Layer<infer _P, infer E, infer _R>
? E
: never)
| (Steps[number]["while"] extends (
input: infer _I
) => Effect.Effect<
infer _A,
infer _E,
infer _R
>
? _E
: never)
error:
| (function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps[number]["provide"] extends import ContextContext.interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<infer function (type parameter) _P_P> | import LayerLayer.interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<infer function (type parameter) _P_P, infer function (type parameter) EE, infer function (type parameter) _R_R> ? function (type parameter) EE
: never)
| (function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps[number]["while"] extends (input: _Iinput: infer function (type parameter) _I_I) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<infer function (type parameter) _A_A, infer function (type parameter) _E_E, infer function (type parameter) _R_R> ? function (type parameter) _E_E : never)
requirements: | (Steps[number]["provide"] extends Layer.Layer<
infer _A,
infer _E,
infer R
>
? R
: never)
| (Steps[number]["while"] extends (
input: infer _I
) => Effect.Effect<
infer _A,
infer _E,
infer R
>
? R
: never)
| (Steps[number]["schedule"] extends Schedule.Schedule<
infer _O,
infer _I,
infer R
>
? R
: never)
requirements:
| (function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps[number]["provide"] extends import LayerLayer.interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<infer function (type parameter) _A_A, infer function (type parameter) _E_E, infer function (type parameter) RR> ? function (type parameter) RR : never)
| (function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps[number]["while"] extends (input: _Iinput: infer function (type parameter) _I_I) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<infer function (type parameter) _A_A, infer function (type parameter) _E_E, infer function (type parameter) RR> ? function (type parameter) RR : never)
| (function (type parameter) Steps in <const Steps extends NonEmptyReadonlyArray<make.Step>>(...steps: Steps & { [K in keyof Steps]: make.Step; }): ExecutionPlan<{
provides: make.StepProvides<Steps>;
input: make.StepInput<Steps>;
error: (Steps[number]["provide"] extends Context.Context<infer _P> | Layer.Layer<infer _P, infer E, infer _R> ? E : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer _R> ? _E : never);
requirements: (Steps[number]["provide"] extends Layer.Layer<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["while"] extends (input: infer _I) => Effect.Effect<infer _A, infer _E, infer R> ? R : never) | (Steps[number]["schedule"] extends Schedule.Schedule<infer _O, infer _I, infer R> ? R : never);
}>
Steps[number]["schedule"] extends import ScheduleSchedule.interface Schedule<out Output, in Input = unknown, out Error = never, out Env = never>A Schedule defines a strategy for repeating or retrying effects based on some policy.
Example (Defining retry and repeat schedules)
import { Console, Data, Effect, Schedule } from "effect"
class NetworkError extends Data.TaggedError("NetworkError")<{
readonly attempt: number
}> {}
// Basic retry schedule - retry up to 3 times with exponential backoff
const retrySchedule = Schedule.max([
Schedule.exponential("100 millis"),
Schedule.recurs(3)
])
// Basic repeat schedule - repeat every 30 seconds forever
const repeatSchedule: Schedule.Schedule<number, unknown, never> = Schedule
.spaced("30 seconds")
const program = Effect.gen(function*() {
let attempts = 0
const result1 = yield* Effect.retry(
Effect.gen(function*() {
attempts++
if (attempts < 3) {
return yield* Effect.fail(new NetworkError({ attempt: attempts }))
}
return "Success"
}),
retrySchedule
)
console.log(result1) // "Success"
yield* Console.log("heartbeat").pipe(
Effect.repeat(repeatSchedule.pipe(Schedule.upTo({ times: 5 })))
)
})
The Schedule namespace contains types and utilities for working with schedules.
Schedule<infer function (type parameter) _O_O, infer function (type parameter) _I_I, infer function (type parameter) RR> ? function (type parameter) RR : never)
}> =>
const makeProto: <
Provides,
In,
PlanE,
PlanR
>(
steps: ExecutionPlan<{
provides: Provides
input: In
error: PlanE
requirements: PlanR
}>["steps"]
) => any
makeProto(steps: Steps & {
[K in keyof Steps]: make.Step
}
steps.ReadonlyArray<T>.map<{
schedule: Schedule.Schedule<any, any, any, never> | undefined;
attempts: number | undefined;
while: ((input: any) => Effect.Effect<boolean, any, any>) | undefined;
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
}>(callbackfn: (value: make.Step, index: number, array: readonly make.Step[]) => {
schedule: Schedule.Schedule<any, any, any, never> | undefined;
attempts: number | undefined;
while: ((input: any) => Effect.Effect<boolean, any, any>) | undefined;
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
}, thisArg?: any): {
schedule: Schedule.Schedule<any, any, any, never> | undefined;
attempts: number | undefined;
while: ((input: any) => Effect.Effect<boolean, any, any>) | undefined;
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
}[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.
map((options: make.Step(parameter) options: {
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
attempts: number | undefined;
while: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
schedule: Schedule.Schedule<any, any, any> | undefined;
}
options, i: numberi) => {
if (options: make.Step(parameter) options: {
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
attempts: number | undefined;
while: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
schedule: Schedule.Schedule<any, any, any> | undefined;
}
options.attempts?: number | undefinedattempts && options: make.Step(parameter) options: {
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
attempts: number | undefined;
while: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
schedule: Schedule.Schedule<any, any, any> | undefined;
}
options.attempts?: numberattempts < 1) {
throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error(`ExecutionPlan.make: step[${i: numberi}].attempts must be greater than 0`)
}
return {
schedule: | Schedule.Schedule<any, any, any, never>
| undefined
schedule: options: make.Step(parameter) options: {
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
attempts: number | undefined;
while: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
schedule: Schedule.Schedule<any, any, any> | undefined;
}
options.schedule?: | Schedule.Schedule<any, any, any>
| undefined
schedule,
attempts: number | undefinedattempts: options: make.Step(parameter) options: {
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
attempts: number | undefined;
while: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
schedule: Schedule.Schedule<any, any, any> | undefined;
}
options.attempts?: number | undefinedattempts,
while: | ((
input: any
) => Effect.Effect<boolean, any, any>)
| undefined
while: options: make.Step(parameter) options: {
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
attempts: number | undefined;
while: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
schedule: Schedule.Schedule<any, any, any> | undefined;
}
options.while?: | ((
input: any
) =>
| boolean
| Effect.Effect<boolean, any, any>)
| undefined
while
? (input: anyinput: any) =>
import effecteffect.const suspend: <A, E, R>(
evaluate: LazyArg<Effect.Effect<A, E, R>>
) => Effect.Effect<A, E, R>
suspend(() => {
const const result:
| boolean
| Effect.Effect<boolean, any, any>
result = options: make.Step(parameter) options: {
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
attempts: number | undefined;
while: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
schedule: Schedule.Schedule<any, any, any> | undefined;
}
options.while?: | ((
input: any
) =>
| boolean
| Effect.Effect<boolean, any, any>)
| undefined
while!(input: anyinput)
return typeof const result:
| boolean
| Effect.Effect<boolean, any, any>
result === "boolean" ? import effecteffect.const succeed: <A>(
value: A
) => Effect.Effect<A>
succeed(const result: booleanresult) : const result: Effect.Effect<
boolean,
any,
any
>
const result: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
result
})
: var undefinedundefined,
provide: | Context.Context<any>
| Context.Context<never>
| Layer.Any
provide: options: make.Step(parameter) options: {
provide: Context.Context<any> | Context.Context<never> | Layer.Any;
attempts: number | undefined;
while: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
schedule: Schedule.Schedule<any, any, any> | undefined;
}
options.provide: | Context.Context<any>
| Context.Context<never>
| Layer.Any
provide
}
}) as any)
/**
* Namespace containing type helpers used by `ExecutionPlan.make`.
*
* @since 3.16.0
*/
export declare namespace make {
/**
* Input shape for a single execution-plan step.
*
* **Details**
*
* Each step provides a `Context` or `Layer` and may limit attempts, add a
* `while` predicate for retry decisions, or attach a `Schedule` for retry
* timing.
*
* @category models
* @since 3.16.0
*/
export type type make.Step = {
readonly provide: Context.Context<any> | Context.Context<never> | Layer.Any;
readonly attempts?: number | undefined;
readonly while?: ((input: any) => boolean | Effect.Effect<boolean, any, any>) | undefined;
readonly schedule?: Schedule.Schedule<any, any, any> | undefined;
}
Input shape for a single execution-plan step.
Details
Each step provides a Context or Layer and may limit attempts, add a
while predicate for retry decisions, or attach a Schedule for retry
timing.
Step = {
readonly provide: | Context.Context<any>
| Context.Context<never>
| Layer.Any
provide: import ContextContext.interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<any> | import ContextContext.interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<never> | import LayerLayer.Any
readonly attempts?: number | undefinedattempts?: number | undefined
readonly while?: | ((
input: any
) =>
| boolean
| Effect.Effect<boolean, any, any>)
| undefined
while?: ((input: anyinput: any) => boolean | import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<boolean, any, any>) | undefined
readonly schedule?: | Schedule.Schedule<any, any, any>
| undefined
schedule?: import ScheduleSchedule.interface Schedule<out Output, in Input = unknown, out Error = never, out Env = never>A Schedule defines a strategy for repeating or retrying effects based on some policy.
Example (Defining retry and repeat schedules)
import { Console, Data, Effect, Schedule } from "effect"
class NetworkError extends Data.TaggedError("NetworkError")<{
readonly attempt: number
}> {}
// Basic retry schedule - retry up to 3 times with exponential backoff
const retrySchedule = Schedule.max([
Schedule.exponential("100 millis"),
Schedule.recurs(3)
])
// Basic repeat schedule - repeat every 30 seconds forever
const repeatSchedule: Schedule.Schedule<number, unknown, never> = Schedule
.spaced("30 seconds")
const program = Effect.gen(function*() {
let attempts = 0
const result1 = yield* Effect.retry(
Effect.gen(function*() {
attempts++
if (attempts < 3) {
return yield* Effect.fail(new NetworkError({ attempt: attempts }))
}
return "Success"
}),
retrySchedule
)
console.log(result1) // "Success"
yield* Console.log("heartbeat").pipe(
Effect.repeat(repeatSchedule.pipe(Schedule.upTo({ times: 5 })))
)
})
The Schedule namespace contains types and utilities for working with schedules.
Schedule<any, any, any> | undefined
}
/**
* Computes the intersection of services provided by a list of execution-plan
* steps.
*
* @category utility types
* @since 3.16.1
*/
export type type make.StepProvides<Steps extends ReadonlyArray<any>, Out = unknown> = Steps extends readonly [infer Step, ...infer Rest] ? StepProvides<Rest, Out & (Step extends {
readonly provide: Context.Context<infer P> | Layer.Layer<infer P, infer _E, infer _R>;
} ? P : unknown)> : Out
Computes the intersection of services provided by a list of execution-plan
steps.
StepProvides<function (type parameter) Steps in type make.StepProvides<Steps extends ReadonlyArray<any>, Out = unknown>Steps extends interface ReadonlyArray<T>ReadonlyArray<any>, function (type parameter) Out in type make.StepProvides<Steps extends ReadonlyArray<any>, Out = unknown>Out = unknown> = function (type parameter) Steps in type make.StepProvides<Steps extends ReadonlyArray<any>, Out = unknown>Steps extends
readonly [infer function (type parameter) StepStep, ...infer function (type parameter) RestRest] ? type make.StepProvides<Steps extends ReadonlyArray<any>, Out = unknown> = Steps extends readonly [infer Step, ...infer Rest] ? StepProvides<Rest, Out & (Step extends {
readonly provide: Context.Context<infer P> | Layer.Layer<infer P, infer _E, infer _R>;
} ? P : unknown)> : Out
Computes the intersection of services provided by a list of execution-plan
steps.
StepProvides<
function (type parameter) RestRest,
& function (type parameter) Out in type make.StepProvides<Steps extends ReadonlyArray<any>, Out = unknown>Out
& (
(function (type parameter) StepStep extends { readonly provide: | Context.Context<infer P>
| Layer.Layer<infer P, infer _E, infer _R>
provide: import ContextContext.interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<infer function (type parameter) PP> | import LayerLayer.interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<infer function (type parameter) PP, infer function (type parameter) _E_E, infer function (type parameter) _R_R> } ? function (type parameter) PP
: unknown)
)
> :
function (type parameter) Out in type make.StepProvides<Steps extends ReadonlyArray<any>, Out = unknown>Out
/**
* Computes the intersection of services provided by a list of execution plans.
*
* @category utility types
* @since 3.16.1
*/
export type type make.PlanProvides<Plans extends ReadonlyArray<any>, Out = unknown> = Plans extends readonly [infer Plan, ...infer Rest] ? PlanProvides<Rest, Out & (Plan extends ExecutionPlan<infer T extends {
provides: any;
input: any;
error: any;
requirements: any;
}> ? T["provides"] : unknown)> : Out
Computes the intersection of services provided by a list of execution plans.
PlanProvides<function (type parameter) Plans in type make.PlanProvides<Plans extends ReadonlyArray<any>, Out = unknown>Plans extends interface ReadonlyArray<T>ReadonlyArray<any>, function (type parameter) Out in type make.PlanProvides<Plans extends ReadonlyArray<any>, Out = unknown>Out = unknown> = function (type parameter) Plans in type make.PlanProvides<Plans extends ReadonlyArray<any>, Out = unknown>Plans extends
readonly [infer function (type parameter) PlanPlan, ...infer function (type parameter) RestRest] ?
type make.PlanProvides<Plans extends ReadonlyArray<any>, Out = unknown> = Plans extends readonly [infer Plan, ...infer Rest] ? PlanProvides<Rest, Out & (Plan extends ExecutionPlan<infer T extends {
provides: any;
input: any;
error: any;
requirements: any;
}> ? T["provides"] : unknown)> : Out
Computes the intersection of services provided by a list of execution plans.
PlanProvides<function (type parameter) RestRest, function (type parameter) Out in type make.PlanProvides<Plans extends ReadonlyArray<any>, Out = unknown>Out & (function (type parameter) PlanPlan extends interface ExecutionPlan<Config extends { provides: any; input: any; error: any; requirements: any; }>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.
Example (Defining fallback execution steps)
import { Effect, ExecutionPlan, Schedule } from "effect"
import type { Layer } from "effect"
import type { LanguageModel } from "effect/unstable/ai"
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)
ExecutionPlan<infer function (type parameter) TT> ? function (type parameter) TT["provides"] : unknown)> :
function (type parameter) Out in type make.PlanProvides<Plans extends ReadonlyArray<any>, Out = unknown>Out
/**
* Computes the input type consumed by the `while` predicates and schedules in
* a list of execution-plan steps.
*
* @category utility types
* @since 3.16.0
*/
export type type make.StepInput<Steps extends ReadonlyArray<any>, Out = unknown> = Steps extends readonly [infer Step, ...infer Rest] ? StepInput<Rest, Out & (Step extends {
readonly while: (input: infer I) => infer _;
} ? I : unknown) & (Step extends {
readonly schedule: Schedule.Schedule<infer _O, infer I, infer _R>;
} ? I : unknown)> : Out
Computes the input type consumed by the while predicates and schedules in
a list of execution-plan steps.
StepInput<function (type parameter) Steps in type make.StepInput<Steps extends ReadonlyArray<any>, Out = unknown>Steps extends interface ReadonlyArray<T>ReadonlyArray<any>, function (type parameter) Out in type make.StepInput<Steps extends ReadonlyArray<any>, Out = unknown>Out = unknown> = function (type parameter) Steps in type make.StepInput<Steps extends ReadonlyArray<any>, Out = unknown>Steps extends
readonly [infer function (type parameter) StepStep, ...infer function (type parameter) RestRest] ? type make.StepInput<Steps extends ReadonlyArray<any>, Out = unknown> = Steps extends readonly [infer Step, ...infer Rest] ? StepInput<Rest, Out & (Step extends {
readonly while: (input: infer I) => infer _;
} ? I : unknown) & (Step extends {
readonly schedule: Schedule.Schedule<infer _O, infer I, infer _R>;
} ? I : unknown)> : Out
Computes the input type consumed by the while predicates and schedules in
a list of execution-plan steps.
StepInput<
function (type parameter) RestRest,
& function (type parameter) Out in type make.StepInput<Steps extends ReadonlyArray<any>, Out = unknown>Out
& (
& (function (type parameter) StepStep extends { readonly while: (input: infer I) => infer _while: (input: Iinput: infer function (type parameter) II) => infer function (type parameter) __ } ? function (type parameter) II : unknown)
& (function (type parameter) StepStep extends { readonly schedule: Schedule.Schedule<
infer _O,
infer I,
infer _R
>
(property) schedule: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schedule: import ScheduleSchedule.interface Schedule<out Output, in Input = unknown, out Error = never, out Env = never>A Schedule defines a strategy for repeating or retrying effects based on some policy.
Example (Defining retry and repeat schedules)
import { Console, Data, Effect, Schedule } from "effect"
class NetworkError extends Data.TaggedError("NetworkError")<{
readonly attempt: number
}> {}
// Basic retry schedule - retry up to 3 times with exponential backoff
const retrySchedule = Schedule.max([
Schedule.exponential("100 millis"),
Schedule.recurs(3)
])
// Basic repeat schedule - repeat every 30 seconds forever
const repeatSchedule: Schedule.Schedule<number, unknown, never> = Schedule
.spaced("30 seconds")
const program = Effect.gen(function*() {
let attempts = 0
const result1 = yield* Effect.retry(
Effect.gen(function*() {
attempts++
if (attempts < 3) {
return yield* Effect.fail(new NetworkError({ attempt: attempts }))
}
return "Success"
}),
retrySchedule
)
console.log(result1) // "Success"
yield* Console.log("heartbeat").pipe(
Effect.repeat(repeatSchedule.pipe(Schedule.upTo({ times: 5 })))
)
})
The Schedule namespace contains types and utilities for working with schedules.
Schedule<infer function (type parameter) _O_O, infer function (type parameter) II, infer function (type parameter) _R_R> } ? function (type parameter) II : unknown)
)
> :
function (type parameter) Out in type make.StepInput<Steps extends ReadonlyArray<any>, Out = unknown>Out
/**
* Computes the combined input type consumed by a list of execution plans.
*
* @category utility types
* @since 3.16.0
*/
export type type make.PlanInput<Plans extends ReadonlyArray<any>, Out = unknown> = Plans extends readonly [infer Plan, ...infer Rest] ? PlanInput<Rest, Out & (Plan extends ExecutionPlan<infer T extends {
provides: any;
input: any;
error: any;
requirements: any;
}> ? T["input"] : unknown)> : Out
Computes the combined input type consumed by a list of execution plans.
PlanInput<function (type parameter) Plans in type make.PlanInput<Plans extends ReadonlyArray<any>, Out = unknown>Plans extends interface ReadonlyArray<T>ReadonlyArray<any>, function (type parameter) Out in type make.PlanInput<Plans extends ReadonlyArray<any>, Out = unknown>Out = unknown> = function (type parameter) Plans in type make.PlanInput<Plans extends ReadonlyArray<any>, Out = unknown>Plans extends
readonly [infer function (type parameter) PlanPlan, ...infer function (type parameter) RestRest] ?
type make.PlanInput<Plans extends ReadonlyArray<any>, Out = unknown> = Plans extends readonly [infer Plan, ...infer Rest] ? PlanInput<Rest, Out & (Plan extends ExecutionPlan<infer T extends {
provides: any;
input: any;
error: any;
requirements: any;
}> ? T["input"] : unknown)> : Out
Computes the combined input type consumed by a list of execution plans.
PlanInput<function (type parameter) RestRest, function (type parameter) Out in type make.PlanInput<Plans extends ReadonlyArray<any>, Out = unknown>Out & (function (type parameter) PlanPlan extends interface ExecutionPlan<Config extends { provides: any; input: any; error: any; requirements: any; }>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.
Example (Defining fallback execution steps)
import { Effect, ExecutionPlan, Schedule } from "effect"
import type { Layer } from "effect"
import type { LanguageModel } from "effect/unstable/ai"
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)
ExecutionPlan<infer function (type parameter) TT> ? function (type parameter) TT["input"] : unknown)> :
function (type parameter) Out in type make.PlanInput<Plans extends ReadonlyArray<any>, Out = unknown>Out
}