Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apps/desktop/src/ipc/methods/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,32 @@ describe("preview IPC methods", () => {
).toThrow();
});
});

describe("describePreviewIpcFailure", () => {
it("folds a desktop operation's sanitized cause into the text the renderer receives", () => {
const timeout = Object.assign(new Error("Operation timed out after '1s'"), {
_tag: "TimeoutError",
});
const failure = PreviewIpc.describePreviewIpcFailure(
new PreviewManager.PreviewOperationError({
operation: "automationSnapshot.capturePage",
tabId: "tab_1",
artifactPath: "/Users/someone/.t3/shot.png",
cause: timeout,
}),
);
expect(failure.message).toBe(
"Desktop preview operation failed: automationSnapshot.capturePage (tab tab_1, artifact /Users/someone/.t3/shot.png): TimeoutError: Operation timed out after '1s'",
);
});

it("leaves already-descriptive errors and causeless failures unchanged", () => {
const notFound = new PreviewManager.PreviewTabNotFoundError({ tabId: "tab_1" });
expect(PreviewIpc.describePreviewIpcFailure(notFound)).toBe(notFound);
const bare = new PreviewManager.PreviewOperationError({
operation: "navigate.loadURL",
cause: {},
});
expect(PreviewIpc.describePreviewIpcFailure(bare)).toBe(bare);
});
});
88 changes: 62 additions & 26 deletions apps/desktop/src/ipc/methods/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
DEFAULT_BROWSER_PROFILE_ID,
INCOGNITO_BROWSER_PROFILE_ID,
} from "@t3tools/contracts";
import { previewFailureReason } from "@t3tools/shared/previewFailureReason";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import * as NodeURL from "node:url";
Expand All @@ -39,6 +40,41 @@ import { PREVIEW_WEBVIEW_PREFERENCES } from "../../preview/WebviewPreferences.ts
import * as IpcChannels from "../channels.ts";
import * as DesktopIpc from "../DesktopIpc.ts";

/**
* Electron hands the renderer only an IPC failure's text. A `PreviewOperationError`
* names the step that failed but keeps its cause structured, so the cause (a
* capture timeout, a closed debugger target) is folded into the text here,
* sanitized, for the agent to act on.
*/
export class PreviewIpcOperationError extends Schema.TaggedError<PreviewIpcOperationError>()(
"PreviewIpcOperationError",
{ summary: Schema.String },
) {
override get message(): string {
return this.summary;
}
}

const isPreviewOperationError = Schema.is(PreviewManager.PreviewOperationError);

export const describePreviewIpcFailure = <E>(error: E): E | PreviewIpcOperationError => {
if (!isPreviewOperationError(error)) return error;
const reason = previewFailureReason(error.cause);
return reason === undefined
? error
: new PreviewIpcOperationError({ summary: `${error.message}: ${reason}` });
};

// The renderer only sees the failure's text, so widening the error type here is safe.
const makePreviewIpcMethod = ((registration) => {
const method = DesktopIpc.makeIpcMethod(registration);
return {
...method,
handler: (raw, event) =>
method.handler(raw, event).pipe(Effect.mapError(describePreviewIpcFailure)),
};
}) as typeof DesktopIpc.makeIpcMethod;

export const installPreviewEventForwarding = Effect.fn(
"desktop.ipc.preview.installEventForwarding",
)(function* () {
Expand All @@ -55,7 +91,7 @@ export const installPreviewEventForwarding = Effect.fn(
);
});

export const createTab = DesktopIpc.makeIpcMethod({
export const createTab = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL,
payload: DesktopPreviewCreateTabInputSchema,
result: Schema.Void,
Expand All @@ -69,7 +105,7 @@ export const createTab = DesktopIpc.makeIpcMethod({
}),
});

export const closeTab = DesktopIpc.makeIpcMethod({
export const closeTab = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_CLOSE_TAB_CHANNEL,
payload: DesktopPreviewTabInputSchema,
result: Schema.Void,
Expand All @@ -79,7 +115,7 @@ export const closeTab = DesktopIpc.makeIpcMethod({
}),
});

export const registerWebview = DesktopIpc.makeIpcMethod({
export const registerWebview = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_REGISTER_WEBVIEW_CHANNEL,
payload: DesktopPreviewRegisterWebviewInputSchema,
result: Schema.Void,
Expand All @@ -89,7 +125,7 @@ export const registerWebview = DesktopIpc.makeIpcMethod({
}),
});

export const navigate = DesktopIpc.makeIpcMethod({
export const navigate = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_NAVIGATE_CHANNEL,
payload: DesktopPreviewNavigateInputSchema,
result: Schema.Void,
Expand All @@ -107,7 +143,7 @@ const tabMethod = (
tabId: string,
) => Effect.Effect<void, PreviewManager.PreviewManagerError>,
) =>
DesktopIpc.makeIpcMethod({
makePreviewIpcMethod({
channel,
payload: DesktopPreviewTabInputSchema,
result: Schema.Void,
Expand Down Expand Up @@ -152,7 +188,7 @@ export const hardReload = tabMethod(
"desktop.ipc.preview.hardReload",
(manager, tabId) => manager.hardReload(tabId),
);
export const setColorScheme = DesktopIpc.makeIpcMethod({
export const setColorScheme = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
payload: DesktopPreviewSetColorSchemeInputSchema,
result: Schema.Void,
Expand All @@ -161,7 +197,7 @@ export const setColorScheme = DesktopIpc.makeIpcMethod({
yield* manager.setColorScheme(tabId, colorScheme);
}),
});
export const setAudioMuted = DesktopIpc.makeIpcMethod({
export const setAudioMuted = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL,
payload: DesktopPreviewSetAudioMutedInputSchema,
result: Schema.Void,
Expand Down Expand Up @@ -201,7 +237,7 @@ export const closePictureInPicture = tabMethod(
(manager, tabId) => manager.closePictureInPicture(tabId),
);

export const clearCookies = DesktopIpc.makeIpcMethod({
export const clearCookies = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
payload: DesktopPreviewClearDataInputSchema,
result: Schema.Void,
Expand All @@ -211,7 +247,7 @@ export const clearCookies = DesktopIpc.makeIpcMethod({
}),
});

export const clearCache = DesktopIpc.makeIpcMethod({
export const clearCache = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
payload: DesktopPreviewClearDataInputSchema,
result: Schema.Void,
Expand Down Expand Up @@ -269,7 +305,7 @@ const resolveClearPartitions = Effect.fn("desktop.ipc.preview.resolveClearPartit
return [yield* manager.getBrowserPartition(scope, persistent, namespace)];
});

export const getPreviewConfig = DesktopIpc.makeIpcMethod({
export const getPreviewConfig = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
payload: DesktopPreviewConfigInputSchema,
result: DesktopPreviewWebviewConfigSchema,
Expand All @@ -293,7 +329,7 @@ export const getPreviewConfig = DesktopIpc.makeIpcMethod({
* context and their own failure type, so they do not unify with the
* manager-backed handlers the shared loop iterates.
*/
export const listBrowserImportSources = DesktopIpc.makeIpcMethod({
export const listBrowserImportSources = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL,
payload: Schema.Void,
result: Schema.Array(BrowserImportSource),
Expand All @@ -303,7 +339,7 @@ export const listBrowserImportSources = DesktopIpc.makeIpcMethod({
}),
});

export const importBrowserCookies = DesktopIpc.makeIpcMethod({
export const importBrowserCookies = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_IMPORT_COOKIES_CHANNEL,
payload: DesktopPreviewImportCookiesInputSchema,
result: BrowserImportResult,
Expand All @@ -327,7 +363,7 @@ export const importBrowserCookies = DesktopIpc.makeIpcMethod({
}),
});

export const setAnnotationTheme = DesktopIpc.makeIpcMethod({
export const setAnnotationTheme = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL,
payload: DesktopPreviewAnnotationThemeInputSchema,
result: Schema.Void,
Expand All @@ -337,7 +373,7 @@ export const setAnnotationTheme = DesktopIpc.makeIpcMethod({
}),
});

export const pickElement = DesktopIpc.makeIpcMethod({
export const pickElement = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
payload: DesktopPreviewTabInputSchema,
result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema),
Expand All @@ -347,7 +383,7 @@ export const pickElement = DesktopIpc.makeIpcMethod({
}),
});

export const captureScreenshot = DesktopIpc.makeIpcMethod({
export const captureScreenshot = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL,
payload: DesktopPreviewTabInputSchema,
result: DesktopPreviewScreenshotArtifactSchema,
Expand All @@ -357,7 +393,7 @@ export const captureScreenshot = DesktopIpc.makeIpcMethod({
}),
});

export const revealArtifact = DesktopIpc.makeIpcMethod({
export const revealArtifact = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL,
payload: DesktopPreviewArtifactInputSchema,
result: Schema.Void,
Expand All @@ -367,7 +403,7 @@ export const revealArtifact = DesktopIpc.makeIpcMethod({
}),
});

export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
export const copyArtifactToClipboard = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL,
payload: DesktopPreviewArtifactInputSchema,
result: Schema.Void,
Expand All @@ -377,7 +413,7 @@ export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
}),
});

export const automationStatus = DesktopIpc.makeIpcMethod({
export const automationStatus = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_STATUS_CHANNEL,
payload: DesktopPreviewTabInputSchema,
result: DesktopPreviewAutomationStatusSchema,
Expand All @@ -387,7 +423,7 @@ export const automationStatus = DesktopIpc.makeIpcMethod({
}),
});

export const automationSnapshot = DesktopIpc.makeIpcMethod({
export const automationSnapshot = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_SNAPSHOT_CHANNEL,
payload: DesktopPreviewTabInputSchema,
result: PreviewAutomationSnapshot,
Expand All @@ -397,7 +433,7 @@ export const automationSnapshot = DesktopIpc.makeIpcMethod({
}),
});

export const automationClick = DesktopIpc.makeIpcMethod({
export const automationClick = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_CLICK_CHANNEL,
payload: DesktopPreviewAutomationClickInputSchema,
result: Schema.Void,
Expand All @@ -407,7 +443,7 @@ export const automationClick = DesktopIpc.makeIpcMethod({
}),
});

export const automationType = DesktopIpc.makeIpcMethod({
export const automationType = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_TYPE_CHANNEL,
payload: DesktopPreviewAutomationTypeInputSchema,
result: Schema.Void,
Expand All @@ -417,7 +453,7 @@ export const automationType = DesktopIpc.makeIpcMethod({
}),
});

export const automationPress = DesktopIpc.makeIpcMethod({
export const automationPress = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_PRESS_CHANNEL,
payload: DesktopPreviewAutomationPressInputSchema,
result: Schema.Void,
Expand All @@ -427,7 +463,7 @@ export const automationPress = DesktopIpc.makeIpcMethod({
}),
});

export const automationScroll = DesktopIpc.makeIpcMethod({
export const automationScroll = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_SCROLL_CHANNEL,
payload: DesktopPreviewAutomationScrollInputSchema,
result: Schema.Void,
Expand All @@ -437,7 +473,7 @@ export const automationScroll = DesktopIpc.makeIpcMethod({
}),
});

export const automationEvaluate = DesktopIpc.makeIpcMethod({
export const automationEvaluate = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_EVALUATE_CHANNEL,
payload: DesktopPreviewAutomationEvaluateInputSchema,
result: Schema.Unknown,
Expand All @@ -447,7 +483,7 @@ export const automationEvaluate = DesktopIpc.makeIpcMethod({
}),
});

export const automationWaitFor = DesktopIpc.makeIpcMethod({
export const automationWaitFor = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_WAIT_FOR_CHANNEL,
payload: DesktopPreviewAutomationWaitForInputSchema,
result: Schema.Void,
Expand All @@ -457,7 +493,7 @@ export const automationWaitFor = DesktopIpc.makeIpcMethod({
}),
});

export const saveRecording = DesktopIpc.makeIpcMethod({
export const saveRecording = makePreviewIpcMethod({
channel: IpcChannels.PREVIEW_RECORDING_SAVE_CHANNEL,
payload: DesktopPreviewRecordingSaveInputSchema,
result: DesktopPreviewRecordingArtifactSchema,
Expand Down
Loading
Loading