Skip to content

Commit a84ef48

Browse files
authored
add LayerMap module (#4441)
1 parent bd2147f commit a84ef48

File tree

6 files changed

+491
-2
lines changed

6 files changed

+491
-2
lines changed

.changeset/calm-spoons-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
expose the Layer.MemoMap via Layer.CurrentMemoMap to the layers being built

.changeset/tricky-apricots-flow.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
add LayerMap module
6+
7+
A `LayerMap` allows you to create a map of Layer's that can be used to
8+
dynamically access resources based on a key.
9+
10+
Here is an example of how you can use a `LayerMap` to create a service that
11+
provides access to multiple OpenAI completions services.
12+
13+
```ts
14+
import { Completions } from "@effect/ai"
15+
import { OpenAiClient, OpenAiCompletions } from "@effect/ai-openai"
16+
import { FetchHttpClient } from "@effect/platform"
17+
import { NodeRuntime } from "@effect/platform-node"
18+
import { Config, Effect, Layer, LayerMap } from "effect"
19+
20+
// create the openai client layer
21+
const OpenAiLayer = OpenAiClient.layerConfig({
22+
apiKey: Config.redacted("OPENAI_API_KEY")
23+
}).pipe(Layer.provide(FetchHttpClient.layer))
24+
25+
// create a service that wraps a LayerMap
26+
class AiClients extends LayerMap.Service<AiClients>()("AiClients", {
27+
// this LayerMap will provide the ai Completions service
28+
provides: Completions.Completions,
29+
30+
// define the lookup function for the layer map
31+
//
32+
// The returned Layer will be used to provide the Completions service for the
33+
// given model.
34+
lookup: (model: OpenAiCompletions.Model) =>
35+
OpenAiCompletions.layer({ model }),
36+
37+
// If a layer is not used for a certain amount of time, it can be removed
38+
idleTimeToLive: "5 seconds",
39+
40+
// Supply the dependencies for the layers in the LayerMap
41+
dependencies: [OpenAiLayer]
42+
}) {}
43+
44+
// usage
45+
Effect.gen(function* () {
46+
// access and use the generic Completions service
47+
const ai = yield* Completions.Completions
48+
const response = yield* ai.create("Hello, world!")
49+
console.log(response.text)
50+
}).pipe(
51+
// use the AiClients service to provide a variant of the Completions service
52+
AiClients.provide("gpt-4o"),
53+
// provide the LayerMap service
54+
Effect.provide(AiClients.Default),
55+
NodeRuntime.runMain
56+
)
57+
```

packages/effect/src/Layer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ export interface MemoMap {
133133
) => Effect.Effect<Context.Context<ROut>, E, RIn>
134134
}
135135

136+
/**
137+
* @since 3.13.0
138+
* @category models
139+
*/
140+
export interface CurrentMemoMap {
141+
readonly _: unique symbol
142+
}
143+
144+
/**
145+
* @since 3.13.0
146+
* @category models
147+
*/
148+
export const CurrentMemoMap: Context.Reference<CurrentMemoMap, MemoMap> = internal.CurrentMemoMap
149+
136150
/**
137151
* Returns `true` if the specified value is a `Layer`, `false` otherwise.
138152
*

0 commit comments

Comments
 (0)