The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | AssetCreateUrlResult, |
| 3 | AssetResource, |
| 4 | EnvironmentId, |
| 5 | PreviewOpenInput, |
| 6 | PreviewSessionSnapshot, |
| 7 | ScopedThreadRef, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { mediaFileReference } from "@t3tools/client-runtime/media-reference"; |
| 10 | import { |
| 11 | type AtomCommandResult, |
| 12 | mapAtomCommandResult, |
| 13 | } from "@t3tools/client-runtime/state/runtime"; |
| 14 | import * as Cause from "effect/Cause"; |
| 15 | import * as Schema from "effect/Schema"; |
| 16 | import { AsyncResult } from "effect/reactivity"; |
| 17 | |
| 18 | import { resolveAssetUrl } from "~/assets/assetUrls"; |
| 19 | import { isPreviewAvailableFor, previewRuntimeFor } from "~/browser/previewRuntime"; |
| 20 | import { |
| 21 | applyPreviewServerSnapshot, |
| 22 | readThreadPreviewState, |
| 23 | rememberPreviewUrl, |
| 24 | setActivePreviewTab, |
| 25 | updatePreviewServerSnapshot, |
| 26 | } from "~/previewStateStore"; |
| 27 | import { |
| 28 | rightPanelHostRef, |
| 29 | selectSelectedRightPanelSurface, |
| 30 | useRightPanelStore, |
| 31 | } from "~/rightPanelStore"; |
| 32 | |
| 33 | import { resolveBrowserDefaults } from "./browserDefaults"; |
| 34 | import { msg } from "gt-react"; |
| 35 | import { m } from "~/i18n/messages"; |
| 36 | |
| 37 | export const isBrowserPreviewFile = (path: string): boolean => |
| 38 | /\.(?:html?|pdf)$/i.test(path.split(/[?#]/, 1)[0] ?? ""); |
| 39 | |
| 40 | export class BrowserPreviewUnavailableError extends Schema.TaggedError<BrowserPreviewUnavailableError>()( |
| 41 | "BrowserPreviewUnavailableError", |
| 42 | { message: Schema.String }, |
| 43 | ) {} |
| 44 | |
| 45 | export class BrowserSettingsReadError extends Schema.TaggedError<BrowserSettingsReadError>()( |
| 46 | "BrowserSettingsReadError", |
| 47 | { cause: Schema.Defect() }, |
| 48 | ) { |
| 49 | override get message(): string { |
| 50 | return m(msg("Saved browser settings could not be loaded.")); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | export const isBrowserSettingsReadError = Schema.is(BrowserSettingsReadError); |
| 55 | |
| 56 | export type OpenPreviewMutation<E = unknown> = (input: { |
| 57 | readonly environmentId: EnvironmentId; |
| 58 | readonly input: PreviewOpenInput; |
| 59 | }) => Promise<AtomCommandResult<PreviewSessionSnapshot, E>>; |
| 60 | |
| 61 | export async function openUrlInPreview<E>(input: { |
| 62 | readonly threadRef: ScopedThreadRef; |
| 63 | readonly url: string; |
| 64 | readonly openPreview: OpenPreviewMutation<E>; |
| 65 | readonly profileId?: PreviewOpenInput["profileId"]; |
| 66 | readonly background?: boolean; |
| 67 | }): Promise<AtomCommandResult<void, E | BrowserSettingsReadError>> { |
| 68 | // Read before any await: a side chat pane that unmounts meanwhile drops its host mapping. |
| 69 | const threadRef = rightPanelHostRef(input.threadRef); |
| 70 | const defaults = await resolveBrowserDefaults().catch( |
| 71 | (cause: unknown) => new BrowserSettingsReadError({ cause }), |
| 72 | ); |
| 73 | if (isBrowserSettingsReadError(defaults)) { |
| 74 | return AsyncResult.failure(Cause.fail(defaults)); |
| 75 | } |
| 76 | const runtime = previewRuntimeFor(threadRef.environmentId); |
| 77 | const previousActiveTabId = readThreadPreviewState(threadRef).activeTabId; |
| 78 | // The server changes the active tab, not panel selection; selection changes come from the user. |
| 79 | const selectedSurface = () => |
| 80 | selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, threadRef)?.id ?? |
| 81 | null; |
| 82 | const surfaceBeforeOpen = selectedSurface(); |
| 83 | const result = await input.openPreview({ |
| 84 | environmentId: threadRef.environmentId, |
| 85 | input: { |
| 86 | threadId: threadRef.threadId, |
| 87 | url: input.url, |
| 88 | viewport: defaults.viewport, |
| 89 | profileId: input.profileId ?? defaults.profileId, |
| 90 | ...(runtime === undefined |
| 91 | ? {} |
| 92 | : { runtime, zoomFactor: defaults.zoomFactor, colorScheme: defaults.appearance }), |
| 93 | }, |
| 94 | }); |
| 95 | return mapAtomCommandResult(result, (snapshot) => { |
| 96 | rememberPreviewUrl(threadRef, input.url); |
| 97 | if (input.background) { |
| 98 | updatePreviewServerSnapshot(threadRef, snapshot); |
| 99 | // Restore focus unless the user picked a tab while opening this one. |
| 100 | if ( |
| 101 | previousActiveTabId && |
| 102 | readThreadPreviewState(threadRef).activeTabId === snapshot.tabId && |
| 103 | selectedSurface() === surfaceBeforeOpen |
| 104 | ) { |
| 105 | setActivePreviewTab(threadRef, previousActiveTabId); |
| 106 | } |
| 107 | return; |
| 108 | } |
| 109 | applyPreviewServerSnapshot(threadRef, snapshot); |
| 110 | useRightPanelStore.getState().openBrowser(threadRef, snapshot.tabId); |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | export async function openFileInPreview<AssetError, PreviewError>(input: { |
| 115 | readonly threadRef: ScopedThreadRef; |
| 116 | readonly filePath: string; |
| 117 | readonly workspaceRoot: string | undefined; |
| 118 | readonly httpBaseUrl: string; |
| 119 | readonly createAssetUrl: (input: { |
| 120 | readonly environmentId: EnvironmentId; |
| 121 | readonly input: { readonly resource: AssetResource }; |
| 122 | }) => Promise<AtomCommandResult<AssetCreateUrlResult, AssetError>>; |
| 123 | readonly openPreview: OpenPreviewMutation<PreviewError>; |
| 124 | }): Promise< |
| 125 | AtomCommandResult< |
| 126 | void, |
| 127 | AssetError | PreviewError | BrowserPreviewUnavailableError | BrowserSettingsReadError |
| 128 | > |
| 129 | > { |
| 130 | if (!isPreviewAvailableFor(input.threadRef.environmentId)) { |
| 131 | return AsyncResult.failure( |
| 132 | Cause.fail( |
| 133 | new BrowserPreviewUnavailableError({ |
| 134 | message: m(msg("The integrated browser is unavailable in this runtime.")), |
| 135 | }), |
| 136 | ), |
| 137 | ); |
| 138 | } |
| 139 | const hostRef = rightPanelHostRef(input.threadRef); |
| 140 | const insideWorkspace = |
| 141 | mediaFileReference(input.filePath, input.workspaceRoot).relativePath !== undefined; |
| 142 | const assetResult = await input.createAssetUrl({ |
| 143 | environmentId: input.threadRef.environmentId, |
| 144 | input: { |
| 145 | resource: { |
| 146 | _tag: insideWorkspace ? "workspace-file" : "media-file", |
| 147 | threadId: input.threadRef.threadId, |
| 148 | path: input.filePath, |
| 149 | }, |
| 150 | }, |
| 151 | }); |
| 152 | if (assetResult._tag === "Failure") { |
| 153 | return AsyncResult.failure(assetResult.cause); |
| 154 | } |
| 155 | const assetUrl = resolveAssetUrl(input.httpBaseUrl, assetResult.value.relativeUrl); |
| 156 | if (assetUrl === null) { |
| 157 | return AsyncResult.failure( |
| 158 | Cause.die(new Error("The environment returned an invalid asset URL.")), |
| 159 | ); |
| 160 | } |
| 161 | return openUrlInPreview({ |
| 162 | threadRef: hostRef, |
| 163 | url: assetUrl, |
| 164 | openPreview: input.openPreview, |
| 165 | }); |
| 166 | } |