Skip to content

Commit e62ff3e

Browse files
shivamhwpjuliusmarminge
authored andcommitted
Load PTY adapter at runtime (pingdotgg#1311)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent a2411b4 commit e62ff3e

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

apps/server/src/serverLayers.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
2-
import { Effect, FileSystem, Layer } from "effect";
2+
import { Effect, FileSystem, Layer, Path } from "effect";
33
import * as SqlClient from "effect/unstable/sql/SqlClient";
44

55
import { CheckpointDiffQueryLive } from "./checkpointing/Layers/CheckpointDiffQuery";
@@ -33,10 +33,26 @@ import { GitCoreLive } from "./git/Layers/GitCore";
3333
import { GitHubCliLive } from "./git/Layers/GitHubCli";
3434
import { CodexTextGenerationLive } from "./git/Layers/CodexTextGeneration";
3535
import { GitServiceLive } from "./git/Layers/GitService";
36-
import { BunPtyAdapterLive } from "./terminal/Layers/BunPTY";
37-
import { NodePtyAdapterLive } from "./terminal/Layers/NodePTY";
36+
import { PtyAdapter } from "./terminal/Services/PTY";
3837
import { AnalyticsService } from "./telemetry/Services/AnalyticsService";
3938

39+
type RuntimePtyAdapterLoader = {
40+
layer: Layer.Layer<PtyAdapter, never, FileSystem.FileSystem | Path.Path>;
41+
};
42+
43+
const runtimePtyAdapterLoaders = {
44+
bun: () => import("./terminal/Layers/BunPTY"),
45+
node: () => import("./terminal/Layers/NodePTY"),
46+
} satisfies Record<string, () => Promise<RuntimePtyAdapterLoader>>;
47+
48+
const makeRuntimePtyAdapterLayer = () =>
49+
Effect.gen(function* () {
50+
const runtime = process.versions.bun !== undefined ? "bun" : "node";
51+
const loader = runtimePtyAdapterLoaders[runtime];
52+
const ptyAdapterModule = yield* Effect.promise<RuntimePtyAdapterLoader>(loader);
53+
return ptyAdapterModule.layer;
54+
}).pipe(Layer.unwrap);
55+
4056
export function makeServerProviderLayer(): Layer.Layer<
4157
ProviderService,
4258
ProviderUnsupportedError,
@@ -109,13 +125,7 @@ export function makeServerRuntimeServicesLayer() {
109125
Layer.provideMerge(checkpointReactorLayer),
110126
);
111127

112-
const terminalLayer = TerminalManagerLive.pipe(
113-
Layer.provide(
114-
typeof Bun !== "undefined" && process.platform !== "win32"
115-
? BunPtyAdapterLive
116-
: NodePtyAdapterLive,
117-
),
118-
);
128+
const terminalLayer = TerminalManagerLive.pipe(Layer.provide(makeRuntimePtyAdapterLayer()));
119129

120130
const gitManagerLayer = GitManagerLive.pipe(
121131
Layer.provideMerge(gitCoreLayer),

apps/server/src/terminal/Layers/BunPTY.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ class BunPtyProcess implements PtyProcess {
8686
}
8787
}
8888

89-
export const BunPtyAdapterLive = Layer.effect(
89+
export const layer = Layer.effect(
9090
PtyAdapter,
9191
Effect.gen(function* () {
9292
if (process.platform === "win32") {
93-
return yield* Effect.die("Bun PTY terminal support is unavailable on Windows.");
93+
return yield* Effect.die(
94+
"Bun PTY terminal support is unavailable on Windows. Please use Node.js (e.g. by running `npx t3`) instead.",
95+
);
9496
}
9597
return {
9698
spawn: (input) =>

apps/server/src/terminal/Layers/NodePTY.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class NodePtyProcess implements PtyProcess {
8484
}
8585
}
8686

87-
export const NodePtyAdapterLive = Layer.effect(
87+
export const layer = Layer.effect(
8888
PtyAdapter,
8989
Effect.gen(function* () {
9090
const fs = yield* FileSystem.FileSystem;

0 commit comments

Comments
 (0)