|
| 1 | +--- |
| 2 | +name: effect-patterns-services-agents |
| 3 | +description: Effect-TS service and agent patterns for this repo. Use when implementing or modifying services in packages/mcp-server, adding agents, or following Effect.Service, Layer, and error-handling conventions. |
| 4 | +--- |
| 5 | + |
| 6 | +# Effect Patterns – Services & Agents |
| 7 | + |
| 8 | +## Effect.Service pattern |
| 9 | + |
| 10 | +Use Effect's Service pattern for composable, type-safe dependency injection: |
| 11 | + |
| 12 | +```typescript |
| 13 | +import { Effect, Context } from 'effect'; |
| 14 | + |
| 15 | +export class MyService extends Context.Tag('MyService')< |
| 16 | + MyService, |
| 17 | + { |
| 18 | + method: () => Effect.Effect<string>; |
| 19 | + } |
| 20 | +>() {} |
| 21 | + |
| 22 | +const effect = Effect.gen(function* () { |
| 23 | + const service = yield* MyService; |
| 24 | + const result = yield* service.method(); |
| 25 | + return result; |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +Modern form with `Effect.Service`: |
| 30 | + |
| 31 | +```typescript |
| 32 | +export class MyAgent extends Effect.Service<MyAgent>()("MyAgent", { |
| 33 | + effect: Effect.gen(function* () { |
| 34 | + return { |
| 35 | + analyze: (input: string) => Effect.succeed("result") |
| 36 | + } |
| 37 | + }) |
| 38 | +}) |
| 39 | +``` |
| 40 | +
|
| 41 | +## Service / agent structure |
| 42 | +
|
| 43 | +Use this layout for agents and service modules: |
| 44 | +
|
| 45 | +``` |
| 46 | +agents/ |
| 47 | +├── analyzer/ |
| 48 | +│ ├── api.ts # Public interface |
| 49 | +│ ├── schema.ts # Type definitions |
| 50 | +│ ├── service.ts # Core logic |
| 51 | +│ ├── types.ts # Domain types |
| 52 | +│ └── __tests__/ # Test suite |
| 53 | +``` |
| 54 | +
|
| 55 | +## Error handling as values |
| 56 | +
|
| 57 | +Use tagged error types; recover with `catchTag`: |
| 58 | +
|
| 59 | +```typescript |
| 60 | +import { Data } from "effect" |
| 61 | + |
| 62 | +export class APIError extends Data.TaggedError("APIError")<{ |
| 63 | + readonly status: number |
| 64 | + readonly message: string |
| 65 | +}> {} |
| 66 | + |
| 67 | +Effect.catchTag("APIError", (err) => /* handle */) |
| 68 | +``` |
| 69 | + |
| 70 | +## Layered service composition |
| 71 | + |
| 72 | +Compose services via `Effect.Layer`; provide to the app with `Effect.provide`: |
| 73 | + |
| 74 | +```typescript |
| 75 | +const appLayer = Layer.mergeAll( |
| 76 | + ConfigService.layer, |
| 77 | + CacheService.layer, |
| 78 | + CircuitBreakerService.layer, |
| 79 | + RateLimiterService.layer, |
| 80 | + ReviewCodeService.layer, |
| 81 | +); |
| 82 | + |
| 83 | +Effect.provide(appEffect, appLayer); |
| 84 | +``` |
| 85 | + |
| 86 | +## Conventions in this repo |
| 87 | + |
| 88 | +- **Effect-TS native**: Use Effect for composability and error handling. |
| 89 | +- **Type safety**: Use @effect/schema for types and validation. |
| 90 | +- **No path aliases**: Use `workspace:*` dependencies; run from project root. |
| 91 | +- **Debug from root**: e.g. `scripts/debug-blocking.ts` for correct module resolution. |
0 commit comments