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 21, 04:45 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as FileSystem from "effect/FileSystem"; |
| 3 | import * as Record from "effect/Record"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { |
| 6 | PROVIDER_SEND_TURN_MAX_FILE_BYTES, |
| 7 | PREVIEW_RECORDING_STOP_TIMEOUT_MS, |
| 8 | PreviewAutomationRecordingTransferError, |
| 9 | PreviewAutomationRecordingDesktopUpdateRequiredError, |
| 10 | PreviewAutomationRecordingArtifact, |
| 11 | PreviewAutomationActInputs, |
| 12 | OrchestratorMcpFailure, |
| 13 | type ToolActivityIcon, |
| 14 | type ThreadId, |
| 15 | type PreviewAutomationOperation, |
| 16 | type PreviewAutomationOpenInput, |
| 17 | type PreviewAutomationRecordingStatus, |
| 18 | type PreviewAutomationResizeResult, |
| 19 | type PreviewAutomationSetColorSchemeResult, |
| 20 | type PreviewAutomationSnapshot, |
| 21 | type PreviewAutomationSnapshotMetadata, |
| 22 | type PreviewAutomationStatus, |
| 23 | type PreviewTabId, |
| 24 | } from "@t3tools/contracts"; |
| 25 | |
| 26 | import { |
| 27 | parseAttachmentUuid, |
| 28 | parseAttachmentFileExtension, |
| 29 | PENDING_ATTACHMENT_THREAD_SEGMENT, |
| 30 | toSafeThreadAttachmentSegment, |
| 31 | } from "../../../attachmentStore.ts"; |
| 32 | import { resolveAttachmentRelativePath } from "../../../attachmentPaths.ts"; |
| 33 | import * as ServerConfig from "../../../config.ts"; |
| 34 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 35 | import * as McpToolAccess from "../../McpToolAccess.ts"; |
| 36 | import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts"; |
| 37 | import { PreviewSnapshotToolkit, PreviewStandardToolkit, PreviewToolkit } from "./tools.ts"; |
| 38 | |
| 39 | /** Whether a preview the agent |
| 40 | * said nothing about surfaces is the user's `browserAutoShowFloatingPreview` |
| 41 | * preference, which is desktop-local and unreadable from here — filling in |
| 42 | * `true` would silently override it for every `preview_open`. |
| 43 | */ |
| 44 | function normalizePreviewOpenInput(input: PreviewAutomationOpenInput): PreviewAutomationOpenInput { |
| 45 | const open = input.open ?? input.show; |
| 46 | return { |
| 47 | ...input, |
| 48 | ...(open === undefined ? {} : { open, show: open }), |
| 49 | reuseExistingTab: input.reuseExistingTab ?? true, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | const invoke = Effect.fn("PreviewToolkit.invoke")(function* <A>( |
| 54 | operation: PreviewAutomationOperation, |
| 55 | input: unknown, |
| 56 | timeoutMs?: number, |
| 57 | tabId?: PreviewTabId, |
| 58 | ): Effect.fn.Return< |
| 59 | { result: A; toolIcon?: ToolActivityIcon }, |
| 60 | import("@t3tools/contracts").PreviewAutomationError, |
| 61 | McpInvocationContext.McpInvocationContext | PreviewAutomationBroker.PreviewAutomationBroker |
| 62 | > { |
| 63 | const scope = yield* McpInvocationContext.requireThreadMcpCapability("preview"); |
| 64 | const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker; |
| 65 | let targetTabId = tabId; |
| 66 | const result = yield* broker.invoke<A>({ |
| 67 | onTargetTab: (resolvedTabId) => { |
| 68 | targetTabId = resolvedTabId; |
| 69 | }, |
| 70 | scope, |
| 71 | operation, |
| 72 | input, |
| 73 | ...(timeoutMs === undefined ? {} : { timeoutMs }), |
| 74 | ...(tabId === undefined ? {} : { tabId }), |
| 75 | }); |
| 76 | if (["status", "open", "navigate", "snapshot"].includes(operation)) return { result }; |
| 77 | const statusTabId = |
| 78 | (operation !== "evaluate" && typeof result === "object" && result !== null |
| 79 | ? (result as { tabId?: PreviewTabId }).tabId |
| 80 | : undefined) ?? targetTabId; |
| 81 | const page = yield* broker |
| 82 | .invoke<PreviewAutomationStatus>({ |
| 83 | scope, |
| 84 | operation: "status", |
| 85 | input: {}, |
| 86 | timeoutMs: 500, |
| 87 | updateCurrentTab: false, |
| 88 | ...(statusTabId === undefined ? {} : { tabId: statusTabId }), |
| 89 | }) |
| 90 | .pipe(Effect.orElseSucceed(() => null)); |
| 91 | return { |
| 92 | result, |
| 93 | ...(page?.url && /^https?:\/\//i.test(page.url) && page.url.length <= 4096 |
| 94 | ? { toolIcon: { _tag: "website" as const, pageUrl: page.url } } |
| 95 | : {}), |
| 96 | }; |
| 97 | }); |
| 98 | |
| 99 | const invokeTargeted = <A extends object>( |
| 100 | operation: PreviewAutomationOperation, |
| 101 | input: { |
| 102 | readonly tabId?: PreviewTabId | undefined; |
| 103 | readonly [key: string]: unknown; |
| 104 | }, |
| 105 | timeoutMs?: number, |
| 106 | ) => { |
| 107 | const { tabId, ...operationInput } = input; |
| 108 | return invoke<A>(operation, operationInput, timeoutMs, tabId).pipe( |
| 109 | Effect.map(({ result, toolIcon }) => ({ |
| 110 | ...result, |
| 111 | ...(toolIcon ? { toolIcon } : {}), |
| 112 | })), |
| 113 | ); |
| 114 | }; |
| 115 | |
| 116 | const decodeActInput = Record.map(PreviewAutomationActInputs, (schema) => |
| 117 | Schema.decodeUnknownEffect(schema), |
| 118 | ); |
| 119 | |
| 120 | const UploadedRecordingArtifact = Schema.Struct({ |
| 121 | ...PreviewAutomationRecordingArtifact.fields, |
| 122 | uploadedAttachmentId: Schema.optional(Schema.String), |
| 123 | }); |
| 124 | const decodeUploadedRecordingArtifact = Schema.decodeUnknownEffect(UploadedRecordingArtifact); |
| 125 | |
| 126 | export const claimPreviewRecording = Effect.fn("PreviewToolkit.claimRecording")(function* ( |
| 127 | threadId: ThreadId, |
| 128 | response: unknown, |
| 129 | ) { |
| 130 | const artifact = yield* decodeUploadedRecordingArtifact(response).pipe( |
| 131 | Effect.mapError( |
| 132 | (cause) => |
| 133 | new PreviewAutomationRecordingTransferError({ |
| 134 | threadId, |
| 135 | cause, |
| 136 | }), |
| 137 | ), |
| 138 | ); |
| 139 | if (!artifact.uploadedAttachmentId) { |
| 140 | return yield* new PreviewAutomationRecordingDesktopUpdateRequiredError({ threadId }); |
| 141 | } |
| 142 | const config = yield* ServerConfig.ServerConfig; |
| 143 | const uuid = parseAttachmentUuid(artifact.uploadedAttachmentId); |
| 144 | const extension = parseAttachmentFileExtension(artifact.uploadedAttachmentId); |
| 145 | const threadSegment = toSafeThreadAttachmentSegment(threadId); |
| 146 | const pendingId = `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${uuid}-${extension}`; |
| 147 | if (!uuid || !extension || !threadSegment || artifact.uploadedAttachmentId !== pendingId) { |
| 148 | return yield* new PreviewAutomationRecordingTransferError({ |
| 149 | threadId, |
| 150 | }); |
| 151 | } |
| 152 | const finalId = `${threadSegment}-${uuid}-${extension}`; |
| 153 | const currentPath = resolveAttachmentRelativePath({ |
| 154 | attachmentsDir: config.attachmentsDir, |
| 155 | relativePath: `${pendingId}.${extension}`, |
| 156 | }); |
| 157 | const finalPath = resolveAttachmentRelativePath({ |
| 158 | attachmentsDir: config.attachmentsDir, |
| 159 | relativePath: `${finalId}.${extension}`, |
| 160 | }); |
| 161 | if (!currentPath || !finalPath) { |
| 162 | return yield* new PreviewAutomationRecordingTransferError({ threadId }); |
| 163 | } |
| 164 | const fileSystem = yield* FileSystem.FileSystem; |
| 165 | const validateFile = (filePath: string) => |
| 166 | fileSystem.stat(filePath).pipe( |
| 167 | Effect.filterOrFail( |
| 168 | (stat) => |
| 169 | stat.type === "File" && |
| 170 | Number(stat.size) === artifact.sizeBytes && |
| 171 | artifact.sizeBytes > 0 && |
| 172 | artifact.sizeBytes <= PROVIDER_SEND_TURN_MAX_FILE_BYTES, |
| 173 | () => new PreviewAutomationRecordingTransferError({ threadId }), |
| 174 | ), |
| 175 | ); |
| 176 | yield* Effect.gen(function* () { |
| 177 | yield* validateFile(currentPath); |
| 178 | yield* fileSystem.rename(currentPath, finalPath); |
| 179 | }).pipe( |
| 180 | Effect.catchIf( |
| 181 | (cause) => |
| 182 | cause._tag !== "PreviewAutomationRecordingTransferError" && |
| 183 | cause.reason._tag === "NotFound", |
| 184 | () => validateFile(finalPath), |
| 185 | ), |
| 186 | Effect.mapError((cause) => new PreviewAutomationRecordingTransferError({ threadId, cause })), |
| 187 | ); |
| 188 | const { uploadedAttachmentId: _uploadedAttachmentId, ...recording } = artifact; |
| 189 | return { ...recording, id: finalId, path: finalPath }; |
| 190 | }); |
| 191 | |
| 192 | const handlers = { |
| 193 | preview_dialog: McpToolAccess.actsAsCaller((input) => |
| 194 | invokeTargeted<PreviewAutomationStatus>("dialog", input), |
| 195 | ), |
| 196 | preview_status: McpToolAccess.readsAsCaller((input) => |
| 197 | invokeTargeted<PreviewAutomationStatus>("status", input ?? {}), |
| 198 | ), |
| 199 | // Models often pass a dev server as both `url` and `target`; the target knows the environment. |
| 200 | preview_open: McpToolAccess.actsAsCaller(({ target, url, ...input }) => |
| 201 | Effect.gen(function* () { |
| 202 | const opened = yield* invokeTargeted<PreviewAutomationStatus>( |
| 203 | "open", |
| 204 | normalizePreviewOpenInput( |
| 205 | target === undefined && url !== undefined ? { ...input, url } : input, |
| 206 | ), |
| 207 | ); |
| 208 | if (target === undefined || opened.tabId === null) return opened; |
| 209 | return yield* invokeTargeted<PreviewAutomationStatus>("navigate", { |
| 210 | target, |
| 211 | tabId: opened.tabId, |
| 212 | }); |
| 213 | }), |
| 214 | ), |
| 215 | preview_navigate: McpToolAccess.actsAsCaller((input) => |
| 216 | invokeTargeted<PreviewAutomationStatus>("navigate", input, input.timeoutMs), |
| 217 | ), |
| 218 | preview_resize: McpToolAccess.actsAsCaller((input) => |
| 219 | invokeTargeted<PreviewAutomationResizeResult>("resize", input, input.timeoutMs), |
| 220 | ), |
| 221 | preview_set_appearance: McpToolAccess.actsAsCaller((input) => |
| 222 | invokeTargeted<PreviewAutomationSetColorSchemeResult>("setColorScheme", input), |
| 223 | ), |
| 224 | preview_snapshot: McpToolAccess.readsAsCaller((input) => { |
| 225 | const { includeImage, save, ...operationInput } = input ?? {}; |
| 226 | // The saved file is for people, so it keeps the resolution the agent's image is capped below. |
| 227 | return invokeTargeted<PreviewAutomationSnapshot | PreviewAutomationSnapshotMetadata>( |
| 228 | "snapshot", |
| 229 | { |
| 230 | ...operationInput, |
| 231 | ...(save === true ? { fullScreenshot: true } : {}), |
| 232 | ...(includeImage === true || save === true ? {} : { pixelsRequired: false }), |
| 233 | }, |
| 234 | ); |
| 235 | }), |
| 236 | preview_act: McpToolAccess.actsAsCaller(({ action, ...input }) => |
| 237 | decodeActInput[action](input).pipe( |
| 238 | Effect.mapError( |
| 239 | (cause) => new OrchestratorMcpFailure({ code: "invalid_request", message: cause.message }), |
| 240 | ), |
| 241 | Effect.flatMap((decoded) => |
| 242 | invokeTargeted<object>( |
| 243 | action, |
| 244 | decoded, |
| 245 | "timeoutMs" in decoded ? decoded.timeoutMs : undefined, |
| 246 | ), |
| 247 | ), |
| 248 | ), |
| 249 | ), |
| 250 | preview_evaluate: McpToolAccess.actsAsCaller(({ tabId, ...input }) => |
| 251 | invoke<unknown>("evaluate", input, undefined, tabId).pipe( |
| 252 | Effect.map(({ result, toolIcon }) => ({ |
| 253 | value: result ?? null, |
| 254 | ...(toolIcon ? { toolIcon } : {}), |
| 255 | })), |
| 256 | ), |
| 257 | ), |
| 258 | preview_wait_for: McpToolAccess.readsAsCaller((input) => |
| 259 | invokeTargeted<object>("waitFor", input, input.timeoutMs), |
| 260 | ), |
| 261 | preview_recording_start: McpToolAccess.actsAsCaller((input) => |
| 262 | invokeTargeted<PreviewAutomationRecordingStatus>("recordingStart", input ?? {}), |
| 263 | ), |
| 264 | preview_recording_stop: McpToolAccess.actsAsCaller((input) => |
| 265 | Effect.gen(function* () { |
| 266 | const scope = yield* McpInvocationContext.requireThreadMcpCapability("preview"); |
| 267 | const { tabId, ...operationInput } = input; |
| 268 | const response = yield* invoke<unknown>( |
| 269 | "recordingStop", |
| 270 | { ...operationInput, transferToEnvironment: true }, |
| 271 | PREVIEW_RECORDING_STOP_TIMEOUT_MS, |
| 272 | tabId, |
| 273 | ); |
| 274 | const artifact = yield* claimPreviewRecording(scope.thread.threadId, response.result); |
| 275 | return { ...artifact, ...(response.toolIcon ? { toolIcon: response.toolIcon } : {}) }; |
| 276 | }), |
| 277 | ), |
| 278 | } satisfies McpToolAccess.Handlers<typeof PreviewToolkit.tools>; |
| 279 | |
| 280 | const { preview_snapshot, ...standardHandlers } = handlers; |
| 281 | |
| 282 | export const layerStandard = McpToolAccess.toLayer(PreviewStandardToolkit, standardHandlers); |
| 283 | |
| 284 | export const layerSnapshot = McpToolAccess.toLayer(PreviewSnapshotToolkit, { |
| 285 | preview_snapshot, |
| 286 | }); |