@devries-cameron20 / t3code Mirror · read-only / commits / 7eda6ba5fc
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 22:45 UTC

[codex] Migrate desktop shell and SSH Effect services (#3194)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 20, 2026 at 00:36 UTC 7eda6ba5fce8290b3a13d1ab730827fc36a34e53
4 files changed +240 -160
apps/desktop/src/shell/DesktopShellEnvironment.ts
+22 -23
@@ -3,7 +3,8 @@ import * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
6 -import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
6 +import * as ChildProcess from "effect/unstable/process/ChildProcess";
7 +import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
8
9 import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
10
@@ -19,13 +20,11 @@ interface WindowsProbeOptions {
20 readonly loadProfile: boolean;
21 }
22
22 -export interface DesktopShellEnvironmentShape {
23 - readonly installIntoProcess: Effect.Effect<void, never>;
24 -}
25 -
23 export class DesktopShellEnvironment extends Context.Service<
24 DesktopShellEnvironment,
28 - DesktopShellEnvironmentShape
25 + {
26 + readonly installIntoProcess: Effect.Effect<void>;
27 + }
28 >()("@t3tools/desktop/shell/DesktopShellEnvironment") {}
29
30 const LOGIN_SHELL_ENV_NAMES = [
@@ -336,20 +335,20 @@ const installShellEnvironment = (
335 return Effect.void;
336 };
337
339 -export const layer = Layer.effect(
340 - DesktopShellEnvironment,
341 - Effect.gen(function* () {
342 - const environment = yield* DesktopEnvironment.DesktopEnvironment;
343 - const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
344 - return DesktopShellEnvironment.of({
345 - installIntoProcess: installShellEnvironment({
346 - env: process.env,
347 - platform: environment.platform,
348 - userShell: Option.none(),
349 - }).pipe(
350 - Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
351 - Effect.withSpan("desktop.shellEnvironment.installIntoProcess"),
352 - ),
353 - });
354 - }),
355 -);
338 +export const make = Effect.gen(function* () {
339 + const environment = yield* DesktopEnvironment.DesktopEnvironment;
340 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
341 + const installIntoProcess: DesktopShellEnvironment["Service"]["installIntoProcess"] =
342 + installShellEnvironment({
343 + env: process.env,
344 + platform: environment.platform,
345 + userShell: Option.none(),
346 + }).pipe(
347 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
348 + Effect.withSpan("desktop.shellEnvironment.installIntoProcess"),
349 + );
350 +
351 + return DesktopShellEnvironment.of({ installIntoProcess });
352 +});
353 +
354 +export const layer = Layer.effect(DesktopShellEnvironment, make);
apps/desktop/src/ssh/DesktopSshEnvironment.test.ts
+14 -1
@@ -19,6 +19,20 @@ function makeTempHomeDir() {
19 }
20
21 describe("sshEnvironment", () => {
22 + it("keeps prompt presentation diagnostics distinct from the legacy wrapper message", () => {
23 + const cause = new DesktopSshPasswordPrompts.DesktopSshPromptPresentationError({
24 + requestId: "prompt-1",
25 + destination: "devbox",
26 + cause: new Error("renderer send failed"),
27 + });
28 +
29 + assert.equal(cause.message, "Failed to present SSH password prompt for devbox.");
30 + assert.equal(
31 + DesktopSshEnvironment.toSshPasswordPromptError(cause).message,
32 + "T3 Code window is not available for SSH authentication.",
33 + );
34 + });
35 +
36 it("treats password prompt timeouts as cancellable authentication prompts", () => {
37 assert.equal(
38 DesktopSshEnvironment.isDesktopSshPasswordPromptCancellation(
@@ -104,7 +118,6 @@ describe("sshEnvironment", () => {
118 Layer.succeed(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {
119 request: () => Effect.die("unexpected password prompt request"),
120 resolve: () => Effect.die("unexpected password prompt resolution"),
107 - cancelPending: () => Effect.void,
121 }),
122 ),
123 Layer.provideMerge(NodeServices.layer),
apps/desktop/src/ssh/DesktopSshEnvironment.ts
+61 -41
@@ -4,11 +4,7 @@ import type {
4 DesktopSshEnvironmentTarget,
5 } from "@t3tools/contracts";
6 import * as NetService from "@t3tools/shared/Net";
7 -import {
8 - SshPasswordPrompt,
9 - type SshPasswordPromptShape,
10 - type SshPasswordRequest,
11 -} from "@t3tools/ssh/auth";
7 +import * as SshAuth from "@t3tools/ssh/auth";
8 import { discoverSshHosts } from "@t3tools/ssh/config";
9 import {
10 SshCommandError,
@@ -19,14 +15,14 @@ import {
15 SshPasswordPromptError,
16 SshReadinessError,
17 } from "@t3tools/ssh/errors";
22 -import { SshEnvironmentManager, type RemoteT3RunnerOptions } from "@t3tools/ssh/tunnel";
18 +import * as SshTunnel from "@t3tools/ssh/tunnel";
19 import * as Context from "effect/Context";
20 import * as Effect from "effect/Effect";
21 import * as FileSystem from "effect/FileSystem";
22 import * as Layer from "effect/Layer";
23 import * as Path from "effect/Path";
28 -import { HttpClient } from "effect/unstable/http";
29 -import { ChildProcessSpawner } from "effect/unstable/process";
24 +import * as HttpClient from "effect/unstable/http/HttpClient";
25 +import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
26
27 import * as DesktopSshPasswordPrompts from "./DesktopSshPasswordPrompts.ts";
28
@@ -52,27 +48,25 @@ export type DesktopSshEnvironmentError =
48 | DesktopSshEnvironmentDiscoverError
49 | DesktopSshEnvironmentOperationError;
50
55 -export interface DesktopSshEnvironmentShape {
56 - readonly discoverHosts: (input?: {
57 - readonly homeDir?: string;
58 - }) => Effect.Effect<readonly DesktopDiscoveredSshHost[], DesktopSshEnvironmentDiscoverError>;
59 - readonly ensureEnvironment: (
60 - target: DesktopSshEnvironmentTarget,
61 - options?: { readonly issuePairingToken?: boolean },
62 - ) => Effect.Effect<DesktopSshEnvironmentBootstrap, DesktopSshEnvironmentOperationError>;
63 - readonly disconnectEnvironment: (
64 - target: DesktopSshEnvironmentTarget,
65 - ) => Effect.Effect<void, DesktopSshEnvironmentOperationError>;
66 -}
67 -
51 export class DesktopSshEnvironment extends Context.Service<
52 DesktopSshEnvironment,
70 - DesktopSshEnvironmentShape
53 + {
54 + readonly discoverHosts: (input?: {
55 + readonly homeDir?: string;
56 + }) => Effect.Effect<readonly DesktopDiscoveredSshHost[], DesktopSshEnvironmentDiscoverError>;
57 + readonly ensureEnvironment: (
58 + target: DesktopSshEnvironmentTarget,
59 + options?: { readonly issuePairingToken?: boolean },
60 + ) => Effect.Effect<DesktopSshEnvironmentBootstrap, DesktopSshEnvironmentOperationError>;
61 + readonly disconnectEnvironment: (
62 + target: DesktopSshEnvironmentTarget,
63 + ) => Effect.Effect<void, DesktopSshEnvironmentOperationError>;
64 + }
65 >()("@t3tools/desktop/ssh/DesktopSshEnvironment") {}
66
67 export interface DesktopSshEnvironmentLayerOptions {
68 readonly resolveCliPackageSpec?: () => string;
75 - readonly resolveCliRunner?: Effect.Effect<RemoteT3RunnerOptions>;
69 + readonly resolveCliRunner?: Effect.Effect<SshTunnel.RemoteT3RunnerOptions>;
70 }
71
72 function discoverDesktopSshHostsEffect(input?: { readonly homeDir?: string }) {
@@ -88,27 +82,53 @@ export function isDesktopSshPasswordPromptCancellation(
82 );
83 }
84
85 +function unexpectedPasswordPromptError(error: never): never {
86 + throw new Error(`Unhandled desktop SSH password prompt error: ${String(error)}`);
87 +}
88 +
89 +export function toSshPasswordPromptError(
90 + cause: DesktopSshPasswordPrompts.DesktopSshPasswordPromptRequestError,
91 +): SshPasswordPromptError {
92 + let message: string;
93 + switch (cause._tag) {
94 + case "DesktopSshPromptRequestIdGenerationError":
95 + message = "Secure randomness is unavailable.";
96 + break;
97 + case "DesktopSshPromptWindowUnavailableError":
98 + case "DesktopSshPromptPresentationError":
99 + message = "T3 Code window is not available for SSH authentication.";
100 + break;
101 + case "DesktopSshPromptTimedOutError":
102 + message = `SSH authentication timed out for ${cause.destination}.`;
103 + break;
104 + case "DesktopSshPromptCancelledError":
105 + message = `SSH authentication cancelled for ${cause.destination}.`;
106 + break;
107 + case "DesktopSshPromptWindowClosedError":
108 + message = "SSH authentication was cancelled because the app window closed.";
109 + break;
110 + case "DesktopSshPromptServiceStoppedError":
111 + message = "SSH password prompt service stopped.";
112 + break;
113 + default:
114 + return unexpectedPasswordPromptError(cause);
115 + }
116 + return new SshPasswordPromptError({ message, cause });
117 +}
118 +
119 const makePasswordPrompt = (
92 - prompts: DesktopSshPasswordPrompts.DesktopSshPasswordPromptsShape,
93 -): SshPasswordPromptShape => ({
120 + prompts: DesktopSshPasswordPrompts.DesktopSshPasswordPrompts["Service"],
121 +): SshAuth.SshPasswordPrompt["Service"] => ({
122 isAvailable: true,
95 - request: (request: SshPasswordRequest) =>
96 - prompts.request(request).pipe(
97 - Effect.mapError(
98 - (cause) =>
99 - new SshPasswordPromptError({
100 - message: cause.message,
101 - cause,
102 - }),
103 - ),
104 - ),
123 + request: (request: SshAuth.SshPasswordRequest) =>
124 + prompts.request(request).pipe(Effect.mapError(toSshPasswordPromptError)),
125 });
126
107 -const make = Effect.gen(function* () {
108 - const manager = yield* SshEnvironmentManager;
127 +export const make = Effect.gen(function* () {
128 + const manager = yield* SshTunnel.SshEnvironmentManager;
129 const prompts = yield* DesktopSshPasswordPrompts.DesktopSshPasswordPrompts;
130 const runtimeContext = yield* Effect.context<DesktopSshEnvironmentRuntimeServices>();
111 - const passwordPrompt = SshPasswordPrompt.of(makePasswordPrompt(prompts));
131 + const passwordPrompt = SshAuth.SshPasswordPrompt.of(makePasswordPrompt(prompts));
132
133 return DesktopSshEnvironment.of({
134 discoverHosts: (input) =>
@@ -120,7 +140,7 @@ const make = Effect.gen(function* () {
140 manager
141 .ensureEnvironment(target, ensureOptions)
142 .pipe(
123 - Effect.provideService(SshPasswordPrompt, passwordPrompt),
143 + Effect.provideService(SshAuth.SshPasswordPrompt, passwordPrompt),
144 Effect.provide(runtimeContext),
145 Effect.withSpan("desktop.ssh.ensureEnvironment"),
146 ),
@@ -128,7 +148,7 @@ const make = Effect.gen(function* () {
148 manager
149 .disconnectEnvironment(target)
150 .pipe(
131 - Effect.provideService(SshPasswordPrompt, passwordPrompt),
151 + Effect.provideService(SshAuth.SshPasswordPrompt, passwordPrompt),
152 Effect.provide(runtimeContext),
153 Effect.withSpan("desktop.ssh.disconnectEnvironment"),
154 ),
@@ -138,7 +158,7 @@ const make = Effect.gen(function* () {
158 export const layer = (options: DesktopSshEnvironmentLayerOptions = {}) =>
159 Layer.effect(DesktopSshEnvironment, make).pipe(
160 Layer.provide(
141 - SshEnvironmentManager.layer({
161 + SshTunnel.SshEnvironmentManager.layer({
162 ...(options.resolveCliPackageSpec === undefined
163 ? {}
164 : { resolveCliPackageSpec: options.resolveCliPackageSpec }),
apps/desktop/src/ssh/DesktopSshPasswordPrompts.ts
+143 -95
@@ -3,7 +3,6 @@ import { DesktopSshPasswordPromptResolutionInputSchema } from "@t3tools/contract
3 import type { SshPasswordRequest } from "@t3tools/ssh/auth";
4 import * as Context from "effect/Context";
5 import * as Crypto from "effect/Crypto";
6 -import * as Data from "effect/Data";
6 import * as DateTime from "effect/DateTime";
7 import * as Deferred from "effect/Deferred";
8 import * as Duration from "effect/Duration";
@@ -11,93 +10,134 @@ import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11 import * as Option from "effect/Option";
12 import * as Ref from "effect/Ref";
13 +import * as Schema from "effect/Schema";
14
15 -import * as IpcChannels from "../ipc/channels.ts";
15 import * as ElectronWindow from "../electron/ElectronWindow.ts";
16 +import { SSH_PASSWORD_PROMPT_CHANNEL } from "../ipc/channels.ts";
17
18 const DEFAULT_SSH_PASSWORD_PROMPT_TIMEOUT_MS = 3 * 60 * 1000;
19 -const WINDOW_UNAVAILABLE_MESSAGE = "T3 Code window is not available for SSH authentication.";
19
20 type DesktopSshPasswordPromptResolutionInput =
21 typeof DesktopSshPasswordPromptResolutionInputSchema.Type;
22
24 -export class DesktopSshPromptUnavailableError extends Data.TaggedError(
25 - "DesktopSshPromptUnavailableError",
26 -)<{
27 - readonly reason: string;
28 -}> {
29 - override get message() {
30 - return this.reason;
23 +const WINDOW_UNAVAILABLE_MESSAGE = "T3 Code window is not available for SSH authentication.";
24 +
25 +export class DesktopSshPromptRequestIdGenerationError extends Schema.TaggedErrorClass<DesktopSshPromptRequestIdGenerationError>()(
26 + "DesktopSshPromptRequestIdGenerationError",
27 + {
28 + destination: Schema.String,
29 + cause: Schema.Defect(),
30 + },
31 +) {
32 + override get message(): string {
33 + return "Secure randomness is unavailable.";
34 }
35 }
36
34 -export class DesktopSshPromptWindowUnavailableError extends Data.TaggedError(
37 +export class DesktopSshPromptWindowUnavailableError extends Schema.TaggedErrorClass<DesktopSshPromptWindowUnavailableError>()(
38 "DesktopSshPromptWindowUnavailableError",
36 -)<{
37 - readonly destination: string;
38 -}> {
39 - override get message() {
39 + {
40 + destination: Schema.String,
41 + },
42 +) {
43 + override get message(): string {
44 return WINDOW_UNAVAILABLE_MESSAGE;
45 }
46 }
47
44 -export class DesktopSshPromptSendError extends Data.TaggedError("DesktopSshPromptSendError")<{
45 - readonly requestId: string;
46 - readonly destination: string;
47 - readonly cause: unknown;
48 -}> {
49 - override get message() {
50 - return WINDOW_UNAVAILABLE_MESSAGE;
48 +const isDesktopSshPromptWindowUnavailableError = Schema.is(DesktopSshPromptWindowUnavailableError);
49 +
50 +export class DesktopSshPromptPresentationError extends Schema.TaggedErrorClass<DesktopSshPromptPresentationError>()(
51 + "DesktopSshPromptPresentationError",
52 + {
53 + requestId: Schema.String,
54 + destination: Schema.String,
55 + cause: Schema.Defect(),
56 + },
57 +) {
58 + override get message(): string {
59 + return `Failed to present SSH password prompt for ${this.destination}.`;
60 }
61 }
62
54 -export class DesktopSshPromptTimedOutError extends Data.TaggedError(
63 +export class DesktopSshPromptTimedOutError extends Schema.TaggedErrorClass<DesktopSshPromptTimedOutError>()(
64 "DesktopSshPromptTimedOutError",
56 -)<{
57 - readonly requestId: string;
58 - readonly destination: string;
59 -}> {
60 - override get message() {
65 + {
66 + requestId: Schema.String,
67 + destination: Schema.String,
68 + },
69 +) {
70 + override get message(): string {
71 return `SSH authentication timed out for ${this.destination}.`;
72 }
73 }
74
65 -export class DesktopSshPromptCancelledError extends Data.TaggedError(
75 +export class DesktopSshPromptCancelledError extends Schema.TaggedErrorClass<DesktopSshPromptCancelledError>()(
76 "DesktopSshPromptCancelledError",
67 -)<{
68 - readonly requestId: string;
69 - readonly destination: string;
70 - readonly reason: string;
71 -}> {
72 - override get message() {
73 - return this.reason;
77 + {
78 + requestId: Schema.String,
79 + destination: Schema.String,
80 + },
81 +) {
82 + override get message(): string {
83 + return `SSH authentication cancelled for ${this.destination}.`;
84 }
85 }
86
77 -export class DesktopSshPromptInvalidRequestIdError extends Data.TaggedError(
87 +export class DesktopSshPromptWindowClosedError extends Schema.TaggedErrorClass<DesktopSshPromptWindowClosedError>()(
88 + "DesktopSshPromptWindowClosedError",
89 + {
90 + requestId: Schema.String,
91 + destination: Schema.String,
92 + },
93 +) {
94 + override get message(): string {
95 + return "SSH authentication was cancelled because the app window closed.";
96 + }
97 +}
98 +
99 +export class DesktopSshPromptServiceStoppedError extends Schema.TaggedErrorClass<DesktopSshPromptServiceStoppedError>()(
100 + "DesktopSshPromptServiceStoppedError",
101 + {
102 + requestId: Schema.String,
103 + destination: Schema.String,
104 + },
105 +) {
106 + override get message(): string {
107 + return "SSH password prompt service stopped.";
108 + }
109 +}
110 +
111 +export class DesktopSshPromptInvalidRequestIdError extends Schema.TaggedErrorClass<DesktopSshPromptInvalidRequestIdError>()(
112 "DesktopSshPromptInvalidRequestIdError",
79 -)<{
80 - readonly requestId: string;
81 -}> {
82 - override get message() {
113 + {
114 + requestId: Schema.String,
115 + },
116 +) {
117 + override get message(): string {
118 return "Invalid SSH password prompt id.";
119 }
120 }
121
87 -export class DesktopSshPromptExpiredError extends Data.TaggedError("DesktopSshPromptExpiredError")<{
88 - readonly requestId: string;
89 -}> {
90 - override get message() {
122 +export class DesktopSshPromptExpiredError extends Schema.TaggedErrorClass<DesktopSshPromptExpiredError>()(
123 + "DesktopSshPromptExpiredError",
124 + {
125 + requestId: Schema.String,
126 + },
127 +) {
128 + override get message(): string {
129 return "SSH password prompt expired. Try connecting again.";
130 }
131 }
132
133 export type DesktopSshPasswordPromptRequestError =
96 - | DesktopSshPromptUnavailableError
134 + | DesktopSshPromptRequestIdGenerationError
135 | DesktopSshPromptWindowUnavailableError
98 - | DesktopSshPromptSendError
136 + | DesktopSshPromptPresentationError
137 | DesktopSshPromptTimedOutError
100 - | DesktopSshPromptCancelledError;
138 + | DesktopSshPromptCancelledError
139 + | DesktopSshPromptWindowClosedError
140 + | DesktopSshPromptServiceStoppedError;
141
142 export type DesktopSshPasswordPromptResolveError =
143 | DesktopSshPromptInvalidRequestIdError
@@ -107,28 +147,28 @@ export type DesktopSshPasswordPromptError =
147 | DesktopSshPasswordPromptRequestError
148 | DesktopSshPasswordPromptResolveError;
149
110 -export function isDesktopSshPasswordPromptCancellation(
111 - error: unknown,
112 -): error is DesktopSshPromptCancelledError | DesktopSshPromptTimedOutError {
113 - return (
114 - error instanceof DesktopSshPromptCancelledError ||
115 - error instanceof DesktopSshPromptTimedOutError
116 - );
117 -}
150 +export const DesktopSshPasswordPromptCancellation = Schema.Union([
151 + DesktopSshPromptCancelledError,
152 + DesktopSshPromptWindowClosedError,
153 + DesktopSshPromptServiceStoppedError,
154 + DesktopSshPromptTimedOutError,
155 +]);
156 +export type DesktopSshPasswordPromptCancellation = typeof DesktopSshPasswordPromptCancellation.Type;
157
119 -export interface DesktopSshPasswordPromptsShape {
120 - readonly request: (
121 - request: SshPasswordRequest,
122 - ) => Effect.Effect<string, DesktopSshPasswordPromptRequestError>;
123 - readonly resolve: (
124 - input: DesktopSshPasswordPromptResolutionInput,
125 - ) => Effect.Effect<void, DesktopSshPasswordPromptResolveError>;
126 - readonly cancelPending: (reason: string) => Effect.Effect<void>;
127 -}
158 +export const isDesktopSshPasswordPromptCancellation = Schema.is(
159 + DesktopSshPasswordPromptCancellation,
160 +);
161
162 export class DesktopSshPasswordPrompts extends Context.Service<
163 DesktopSshPasswordPrompts,
131 - DesktopSshPasswordPromptsShape
164 + {
165 + readonly request: (
166 + request: SshPasswordRequest,
167 + ) => Effect.Effect<string, DesktopSshPasswordPromptRequestError>;
168 + readonly resolve: (
169 + input: DesktopSshPasswordPromptResolutionInput,
170 + ) => Effect.Effect<void, DesktopSshPasswordPromptResolveError>;
171 + }
172 >()("@t3tools/desktop/ssh/DesktopSshPasswordPrompts") {}
173
174 interface PendingSshPasswordPrompt {
@@ -137,7 +177,7 @@ interface PendingSshPasswordPrompt {
177 readonly deferred: Deferred.Deferred<string, DesktopSshPasswordPromptRequestError>;
178 }
179
140 -interface LayerOptions {
180 +export interface DesktopSshPasswordPromptsOptions {
181 readonly passwordPromptTimeoutMs?: number;
182 }
183
@@ -161,14 +201,16 @@ const failPending = (
201 error: DesktopSshPasswordPromptRequestError,
202 ) => Deferred.fail(pending.deferred, error).pipe(Effect.asVoid);
203
164 -const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (options: LayerOptions = {}) {
204 +export const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (
205 + options: DesktopSshPasswordPromptsOptions = {},
206 +) {
207 const electronWindow = yield* ElectronWindow.ElectronWindow;
208 const crypto = yield* Crypto.Crypto;
209 const pendingRef = yield* Ref.make(new Map<string, PendingSshPasswordPrompt>());
210 const passwordPromptTimeoutMs =
211 options.passwordPromptTimeoutMs ?? DEFAULT_SSH_PASSWORD_PROMPT_TIMEOUT_MS;
212
171 - const cancelPending = (reason: string): Effect.Effect<void> =>
213 + const cancelPending = () =>
214 Ref.getAndSet(pendingRef, new Map()).pipe(
215 Effect.flatMap((pending) =>
216 Effect.forEach(
@@ -176,10 +218,9 @@ const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (options: La
218 (entry) =>
219 failPending(
220 entry,
179 - new DesktopSshPromptCancelledError({
221 + new DesktopSshPromptServiceStoppedError({
222 requestId: entry.requestId,
223 destination: entry.destination,
182 - reason,
224 }),
225 ),
226 { discard: true },
@@ -188,13 +229,11 @@ const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (options: La
229 Effect.asVoid,
230 );
231
191 - yield* Effect.addFinalizer(() =>
192 - cancelPending("SSH password prompt service stopped.").pipe(Effect.ignore),
193 - );
232 + yield* Effect.addFinalizer(() => cancelPending().pipe(Effect.ignore));
233
195 - const resolve = Effect.fn("desktop.sshPasswordPrompts.resolve")(function* (
196 - input: DesktopSshPasswordPromptResolutionInput,
197 - ): Effect.fn.Return<void, DesktopSshPasswordPromptResolveError> {
234 + const resolve: DesktopSshPasswordPrompts["Service"]["resolve"] = Effect.fn(
235 + "desktop.sshPasswordPrompts.resolve",
236 + )(function* (input) {
237 const requestId = input.requestId.trim();
238 if (requestId.length === 0) {
239 return yield* new DesktopSshPromptInvalidRequestIdError({ requestId: input.requestId });
@@ -212,7 +251,6 @@ const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (options: La
251 new DesktopSshPromptCancelledError({
252 requestId,
253 destination: entry.destination,
215 - reason: `SSH authentication cancelled for ${entry.destination}.`,
254 }),
255 );
256 return;
@@ -221,9 +259,9 @@ const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (options: La
259 yield* Deferred.succeed(entry.deferred, input.password).pipe(Effect.asVoid);
260 });
261
224 - const request = Effect.fn("desktop.sshPasswordPrompts.request")(function* (
225 - input: SshPasswordRequest,
226 - ): Effect.fn.Return<string, DesktopSshPasswordPromptRequestError> {
262 + const request: DesktopSshPasswordPrompts["Service"]["request"] = Effect.fn(
263 + "desktop.sshPasswordPrompts.request",
264 + )(function* (input) {
265 const window = yield* electronWindow.main;
266 if (Option.isNone(window) || window.value.isDestroyed()) {
267 return yield* new DesktopSshPromptWindowUnavailableError({
@@ -233,7 +271,11 @@ const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (options: La
271
272 const requestId = yield* crypto.randomUUIDv4.pipe(
273 Effect.mapError(
236 - () => new DesktopSshPromptUnavailableError({ reason: "Secure randomness is unavailable." }),
274 + (cause) =>
275 + new DesktopSshPromptRequestIdGenerationError({
276 + destination: input.destination,
277 + cause,
278 + }),
279 ),
280 );
281 const now = yield* DateTime.now;
@@ -267,10 +309,9 @@ const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (options: La
309 onSome: (pending) =>
310 failPending(
311 pending,
270 - new DesktopSshPromptCancelledError({
312 + new DesktopSshPromptWindowClosedError({
313 requestId,
314 destination: input.destination,
273 - reason: "SSH authentication was cancelled because the app window closed.",
315 }),
316 ),
317 }),
@@ -302,36 +343,43 @@ const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (options: La
343 return yield* Effect.try({
344 try: () => {
345 if (window.value.isDestroyed()) {
305 - throw new Error(WINDOW_UNAVAILABLE_MESSAGE);
346 + throw new DesktopSshPromptWindowUnavailableError({
347 + destination: input.destination,
348 + });
349 }
350 window.value.once("closed", cancelOnWindowClosed);
308 - window.value.webContents.send(IpcChannels.SSH_PASSWORD_PROMPT_CHANNEL, promptRequest);
351 + window.value.webContents.send(SSH_PASSWORD_PROMPT_CHANNEL, promptRequest);
352 if (window.value.isDestroyed()) {
310 - throw new Error(WINDOW_UNAVAILABLE_MESSAGE);
353 + throw new DesktopSshPromptWindowUnavailableError({
354 + destination: input.destination,
355 + });
356 }
357 if (window.value.isMinimized()) {
358 window.value.restore();
359 }
360 if (window.value.isDestroyed()) {
316 - throw new Error(WINDOW_UNAVAILABLE_MESSAGE);
361 + throw new DesktopSshPromptWindowUnavailableError({
362 + destination: input.destination,
363 + });
364 }
365 window.value.focus();
366 },
367 catch: (cause) =>
321 - new DesktopSshPromptSendError({
322 - requestId,
323 - destination: input.destination,
324 - cause,
325 - }),
368 + isDesktopSshPromptWindowUnavailableError(cause)
369 + ? cause
370 + : new DesktopSshPromptPresentationError({
371 + requestId,
372 + destination: input.destination,
373 + cause,
374 + }),
375 }).pipe(Effect.andThen(waitForPassword), Effect.ensuring(cleanup));
376 });
377
378 return DesktopSshPasswordPrompts.of({
379 request,
380 resolve,
332 - cancelPending,
381 });
382 });
383
336 -export const layer = (options: LayerOptions = {}) =>
384 +export const layer = (options: DesktopSshPasswordPromptsOptions = {}) =>
385 Layer.effect(DesktopSshPasswordPrompts, make(options));