The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 4 | import type { HttpClient } from "effect/http/HttpClient"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Logger from "effect/Logger"; |
| 7 | import * as LogLevel from "effect/LogLevel"; |
| 8 | import type { PlatformError } from "effect/PlatformError"; |
| 9 | import { type AlchemyContext, AlchemyContextLive } from "../AlchemyContext.ts"; |
| 10 | import { ArtifactStore, createArtifactStore } from "../Artifacts.ts"; |
| 11 | import { type CredentialsStore, CredentialsStoreLive } from "../Auth/Credentials.ts"; |
| 12 | import { type ProfileStore, ProfileStoreLive } from "../Auth/Profile.ts"; |
| 13 | import * as Interaction from "../Interaction.ts"; |
| 14 | import { makePlainConsoleSink } from "../Util/ConsoleSink.ts"; |
| 15 | import { PlatformServices } from "../Util/PlatformServices.ts"; |
| 16 | import { authRegistryLayer } from "./AuthRegistry.ts"; |
| 17 | |
| 18 | /** The services {@link layer} provides; every Alchemist operation runs under them. */ |
| 19 | export type Services = |
| 20 | | AlchemyContext |
| 21 | | ArtifactStore |
| 22 | | CredentialsStore |
| 23 | | HttpClient |
| 24 | | Interaction.Interaction |
| 25 | | ProfileStore |
| 26 | | PlatformServices; |
| 27 | |
| 28 | /** |
| 29 | * Log lines on the console. The caller's own loggers stay; only Effect's |
| 30 | * default console logger gives way to this sink, so lines print once. Typed |
| 31 | * events are left to `onEvent` subscribers (a renderer, or the caller's own |
| 32 | * handler). |
| 33 | * |
| 34 | * Info and above only: records replayed from sidecars and child processes |
| 35 | * bypass the fiber's minimum level, so the sink applies its own. |
| 36 | */ |
| 37 | const consoleSink = makePlainConsoleSink(); |
| 38 | const ConsoleLog = Layer.unwrap( |
| 39 | Effect.map(Logger.CurrentLoggers, (current) => |
| 40 | Logger.layer([ |
| 41 | ...[...current].filter((logger) => logger !== Logger.defaultLogger), |
| 42 | Logger.make<unknown, void>((options) => { |
| 43 | if (LogLevel.isGreaterThanOrEqualTo(options.logLevel, "Info")) consoleSink.log(options); |
| 44 | }), |
| 45 | ]), |
| 46 | ), |
| 47 | ); |
| 48 | |
| 49 | /** |
| 50 | * All services required by the programmatic Alchemist API. |
| 51 | * |
| 52 | * Log lines print on the console. Typed events are log records too; observe |
| 53 | * them around a call with {@link import("../Events.ts").onEvent}. |
| 54 | * The non-interactive Interaction default serves the engine paths that need |
| 55 | * one (state-store confirms fail typed; profile probes never prompt). |
| 56 | * |
| 57 | * @example |
| 58 | * ```ts |
| 59 | * Effect.runPromise( |
| 60 | * program.pipe(Effect.provide(Alchemist.layer()), Effect.scoped), |
| 61 | * ) |
| 62 | * ``` |
| 63 | */ |
| 64 | export const layer = (): Layer.Layer<Services, PlatformError> => |
| 65 | Layer.mergeAll( |
| 66 | Layer.mergeAll(AlchemyContextLive, ProfileStoreLive, CredentialsStoreLive).pipe( |
| 67 | Layer.provideMerge(PlatformServices), |
| 68 | ), |
| 69 | FetchHttpClient.layer, |
| 70 | ConfigProvider.layer(ConfigProvider.fromEnv()), |
| 71 | Layer.succeed(ArtifactStore, createArtifactStore()), |
| 72 | Interaction.layerNonInteractive(), |
| 73 | ConsoleLog, |
| 74 | authRegistryLayer, |
| 75 | ); |