|
| 1 | +/** |
| 2 | + * @since 3.13.0 |
| 3 | + */ |
| 4 | +import * as Context from "./Context.js" |
| 5 | +import type { DurationInput } from "./Duration.js" |
| 6 | +import * as Effect from "./Effect.js" |
| 7 | +import { identity } from "./Function.js" |
| 8 | +import * as Layer from "./Layer.js" |
| 9 | +import * as RcMap from "./RcMap.js" |
| 10 | +import type * as Scope from "./Scope.js" |
| 11 | + |
| 12 | +/** |
| 13 | + * @since 3.13.0 |
| 14 | + * @category Symbols |
| 15 | + */ |
| 16 | +export const TypeId: unique symbol = Symbol.for("effect/LayerMap") |
| 17 | + |
| 18 | +/** |
| 19 | + * @since 3.13.0 |
| 20 | + * @category Symbols |
| 21 | + */ |
| 22 | +export type TypeId = typeof TypeId |
| 23 | + |
| 24 | +/** |
| 25 | + * @since 3.13.0 |
| 26 | + * @category Models |
| 27 | + */ |
| 28 | +export interface LayerMap<in K, out I, out S, out E = never> { |
| 29 | + readonly [TypeId]: TypeId |
| 30 | + |
| 31 | + /** |
| 32 | + * The internal RcMap that stores the resources. |
| 33 | + */ |
| 34 | + readonly rcMap: RcMap.RcMap<K, S, E> |
| 35 | + |
| 36 | + /** |
| 37 | + * Retrieves an instance of the resource associated with the key. |
| 38 | + */ |
| 39 | + get(key: K): Effect.Effect<S, E, Scope.Scope> |
| 40 | + |
| 41 | + /** |
| 42 | + * Provides an instance of the resource associated with the key |
| 43 | + * to the given effect. |
| 44 | + */ |
| 45 | + provide(key: K): <A, EX, R>(effect: Effect.Effect<A, EX, R>) => Effect.Effect<A, EX | E, Exclude<R, I> | Scope.Scope> |
| 46 | + |
| 47 | + /** |
| 48 | + * Invalidates the resource associated with the key. |
| 49 | + */ |
| 50 | + invalidate(key: K): Effect.Effect<void> |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * @since 3.13.0 |
| 55 | + * @category Constructors |
| 56 | + * |
| 57 | + * A `LayerMap` allows you to create a map of Layer's that can be used to |
| 58 | + * dynamically access resources based on a key. |
| 59 | + * |
| 60 | + * ```ts |
| 61 | + * import { Completions } from "@effect/ai" |
| 62 | + * import { OpenAiClient, OpenAiCompletions } from "@effect/ai-openai" |
| 63 | + * import { FetchHttpClient } from "@effect/platform" |
| 64 | + * import { NodeRuntime } from "@effect/platform-node" |
| 65 | + * import { Config, Effect, Layer, LayerMap } from "effect" |
| 66 | + * |
| 67 | + * // create the openai client layer |
| 68 | + * const OpenAiLayer = OpenAiClient.layerConfig({ |
| 69 | + * apiKey: Config.redacted("OPENAI_API_KEY") |
| 70 | + * }).pipe(Layer.provide(FetchHttpClient.layer)) |
| 71 | + * |
| 72 | + * // create a service that wraps a LayerMap |
| 73 | + * class AiClients extends Effect.Service<AiClients>()("AiClients", { |
| 74 | + * scoped: LayerMap.fromRecord(Completions.Completions, { |
| 75 | + * gpt4o: OpenAiCompletions.layer({ model: "gpt-4o" }), |
| 76 | + * gpt3Turbo: OpenAiCompletions.layer({ model: "gpt-3.5-turbo" }) |
| 77 | + * }), |
| 78 | + * dependencies: [OpenAiLayer] |
| 79 | + * }) {} |
| 80 | + * |
| 81 | + * // You could also use the `make` constructor to create a LayerMap from a lookup function |
| 82 | + * LayerMap.make( |
| 83 | + * Completions.Completions, |
| 84 | + * (model: string) => OpenAiCompletions.layer({ model }) |
| 85 | + * ) |
| 86 | + * |
| 87 | + * // usage |
| 88 | + * Effect.gen(function*() { |
| 89 | + * // get the AiClients service |
| 90 | + * const clients = yield* AiClients |
| 91 | + * // retrieve the gpt4o client |
| 92 | + * const client = yield* clients.get("gpt4o") |
| 93 | + * // create a completion |
| 94 | + * const response = yield* client.create("Hello, world!") |
| 95 | + * console.log(response.text) |
| 96 | + * }).pipe( |
| 97 | + * Effect.scoped, |
| 98 | + * Effect.provide(AiClients.Default), |
| 99 | + * NodeRuntime.runMain |
| 100 | + * ) |
| 101 | + * ``` |
| 102 | + */ |
| 103 | +export const make: <I, S, K, L extends Layer.Layer<I, any, any>>( |
| 104 | + tag: Context.Tag<I, S>, |
| 105 | + lookup: (key: K) => L, |
| 106 | + options?: { |
| 107 | + readonly idleTimeToLive?: DurationInput | undefined |
| 108 | + } | undefined |
| 109 | +) => Effect.Effect< |
| 110 | + LayerMap<K, I, S, L extends Layer.Layer<infer _A, infer _E, infer _R> ? _E : never>, |
| 111 | + never, |
| 112 | + Scope.Scope | (L extends Layer.Layer<infer _A, infer _E, infer _R> ? _R : never) |
| 113 | +> = Effect.fnUntraced(function*<I, S, K, L extends Layer.Layer<I, any, any>>( |
| 114 | + tag: Context.Tag<I, S>, |
| 115 | + lookup: (key: K) => L, |
| 116 | + options?: { |
| 117 | + readonly idleTimeToLive?: DurationInput | undefined |
| 118 | + } |
| 119 | +) { |
| 120 | + const context = yield* Effect.context<never>() |
| 121 | + |
| 122 | + // If we are inside another layer build, use the current memo map, |
| 123 | + // otherwise create a new one. |
| 124 | + const memoMap = context.unsafeMap.has(Layer.CurrentMemoMap.key) |
| 125 | + ? Context.get(context, Layer.CurrentMemoMap) |
| 126 | + : yield* Layer.makeMemoMap |
| 127 | + |
| 128 | + const rcMap = yield* RcMap.make({ |
| 129 | + lookup: (key: K) => |
| 130 | + Effect.scopeWith((scope) => |
| 131 | + Layer.buildWithMemoMap(lookup(key), memoMap, scope) as Effect.Effect< |
| 132 | + Context.Context<I> |
| 133 | + > |
| 134 | + ).pipe( |
| 135 | + Effect.map((context) => Context.get(context, tag as any) as S) |
| 136 | + ), |
| 137 | + idleTimeToLive: options?.idleTimeToLive |
| 138 | + }) |
| 139 | + |
| 140 | + const get = (key: K): Effect.Effect<S, never, Scope.Scope> => RcMap.get(rcMap, key) |
| 141 | + |
| 142 | + return identity<LayerMap<K, I, S, any>>({ |
| 143 | + [TypeId]: TypeId, |
| 144 | + rcMap, |
| 145 | + get, |
| 146 | + provide: (key) => Effect.provideServiceEffect(tag, get(key)), |
| 147 | + invalidate: (key) => RcMap.invalidate(rcMap, key) |
| 148 | + }) |
| 149 | +}) |
| 150 | + |
| 151 | +/** |
| 152 | + * @since 3.13.0 |
| 153 | + * @category Constructors |
| 154 | + */ |
| 155 | +export const fromRecord = <I, S, const Layers extends Record<string, Layer.Layer<I, any, any>>>( |
| 156 | + tag: Context.Tag<I, S>, |
| 157 | + layers: Layers |
| 158 | +): Effect.Effect< |
| 159 | + LayerMap<keyof Layers, I, S, Layers[keyof Layers] extends Layer.Layer<infer _A, infer _E, infer _R> ? _E : never>, |
| 160 | + never, |
| 161 | + Scope.Scope | (Layers[keyof Layers] extends Layer.Layer<infer _A, infer _E, infer _R> ? _R : never) |
| 162 | +> => make(tag, (key: keyof Layers) => layers[key]) |
0 commit comments