|
1 | 1 | import * as Effect from "effect/Effect"; |
2 | | -import type { |
3 | | - PreviewAutomationOperation, |
4 | | - PreviewAutomationOpenInput, |
| 2 | +import * as FileSystem from "effect/FileSystem"; |
| 3 | +import * as Schema from "effect/Schema"; |
| 4 | +import { |
| 5 | + PROVIDER_SEND_TURN_MAX_FILE_BYTES, |
| 6 | + PREVIEW_RECORDING_STOP_TIMEOUT_MS, |
| 7 | + PreviewAutomationRecordingTransferError, |
| 8 | + PreviewAutomationRecordingDesktopUpdateRequiredError, |
5 | 9 | PreviewAutomationRecordingArtifact, |
6 | | - PreviewAutomationRecordingStatus, |
7 | | - PreviewAutomationResizeResult, |
8 | | - PreviewAutomationSetColorSchemeResult, |
9 | | - PreviewAutomationSnapshot, |
10 | | - PreviewAutomationStatus, |
11 | | - PreviewTabId, |
| 10 | + type ThreadId, |
| 11 | + type PreviewAutomationOperation, |
| 12 | + type PreviewAutomationOpenInput, |
| 13 | + type PreviewAutomationRecordingStatus, |
| 14 | + type PreviewAutomationResizeResult, |
| 15 | + type PreviewAutomationSetColorSchemeResult, |
| 16 | + type PreviewAutomationSnapshot, |
| 17 | + type PreviewAutomationStatus, |
| 18 | + type PreviewTabId, |
12 | 19 | } from "@t3tools/contracts"; |
13 | 20 |
|
| 21 | +import { |
| 22 | + parseAttachmentUuid, |
| 23 | + parseAttachmentFileExtension, |
| 24 | + PENDING_ATTACHMENT_THREAD_SEGMENT, |
| 25 | + toSafeThreadAttachmentSegment, |
| 26 | +} from "../../../attachmentStore.ts"; |
| 27 | +import { resolveAttachmentRelativePath } from "../../../attachmentPaths.ts"; |
| 28 | +import * as ServerConfig from "../../../config.ts"; |
14 | 29 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
15 | 30 | import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts"; |
16 | 31 | import { PreviewSnapshotToolkit, PreviewStandardToolkit, PreviewToolkit } from "./tools.ts"; |
@@ -67,6 +82,79 @@ const invokeTargeted = <A>( |
67 | 82 | return invoke<A>(operation, operationInput, timeoutMs, tabId); |
68 | 83 | }; |
69 | 84 |
|
| 85 | +const UploadedRecordingArtifact = Schema.Struct({ |
| 86 | + ...PreviewAutomationRecordingArtifact.fields, |
| 87 | + uploadedAttachmentId: Schema.optional(Schema.String), |
| 88 | +}); |
| 89 | +const decodeUploadedRecordingArtifact = Schema.decodeUnknownEffect(UploadedRecordingArtifact); |
| 90 | + |
| 91 | +export const claimPreviewRecording = Effect.fn("PreviewToolkit.claimRecording")(function* ( |
| 92 | + threadId: ThreadId, |
| 93 | + response: unknown, |
| 94 | +) { |
| 95 | + const artifact = yield* decodeUploadedRecordingArtifact(response).pipe( |
| 96 | + Effect.mapError( |
| 97 | + (cause) => |
| 98 | + new PreviewAutomationRecordingTransferError({ |
| 99 | + threadId, |
| 100 | + cause, |
| 101 | + }), |
| 102 | + ), |
| 103 | + ); |
| 104 | + if (!artifact.uploadedAttachmentId) { |
| 105 | + return yield* new PreviewAutomationRecordingDesktopUpdateRequiredError({ threadId }); |
| 106 | + } |
| 107 | + const config = yield* ServerConfig.ServerConfig; |
| 108 | + const uuid = parseAttachmentUuid(artifact.uploadedAttachmentId); |
| 109 | + const extension = parseAttachmentFileExtension(artifact.uploadedAttachmentId); |
| 110 | + const threadSegment = toSafeThreadAttachmentSegment(threadId); |
| 111 | + const pendingId = `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${uuid}-${extension}`; |
| 112 | + if (!uuid || !extension || !threadSegment || artifact.uploadedAttachmentId !== pendingId) { |
| 113 | + return yield* new PreviewAutomationRecordingTransferError({ |
| 114 | + threadId, |
| 115 | + }); |
| 116 | + } |
| 117 | + // The same completed upload can be returned to overlapping stop requests. |
| 118 | + const finalId = `${threadSegment}-${uuid}-${extension}`; |
| 119 | + const currentPath = resolveAttachmentRelativePath({ |
| 120 | + attachmentsDir: config.attachmentsDir, |
| 121 | + relativePath: `${pendingId}.${extension}`, |
| 122 | + }); |
| 123 | + const finalPath = resolveAttachmentRelativePath({ |
| 124 | + attachmentsDir: config.attachmentsDir, |
| 125 | + relativePath: `${finalId}.${extension}`, |
| 126 | + }); |
| 127 | + if (!currentPath || !finalPath) { |
| 128 | + return yield* new PreviewAutomationRecordingTransferError({ threadId }); |
| 129 | + } |
| 130 | + const fileSystem = yield* FileSystem.FileSystem; |
| 131 | + const validateFile = (filePath: string) => |
| 132 | + fileSystem.stat(filePath).pipe( |
| 133 | + Effect.filterOrFail( |
| 134 | + (stat) => |
| 135 | + stat.type === "File" && |
| 136 | + Number(stat.size) === artifact.sizeBytes && |
| 137 | + artifact.sizeBytes > 0 && |
| 138 | + artifact.sizeBytes <= PROVIDER_SEND_TURN_MAX_FILE_BYTES, |
| 139 | + () => new PreviewAutomationRecordingTransferError({ threadId }), |
| 140 | + ), |
| 141 | + ); |
| 142 | + yield* Effect.gen(function* () { |
| 143 | + yield* validateFile(currentPath); |
| 144 | + yield* fileSystem.rename(currentPath, finalPath); |
| 145 | + }).pipe( |
| 146 | + // Another stop may already have claimed this exact upload for this thread. |
| 147 | + Effect.catch((cause) => |
| 148 | + cause._tag !== "PreviewAutomationRecordingTransferError" && cause.reason._tag === "NotFound" |
| 149 | + ? validateFile(finalPath) |
| 150 | + : Effect.fail(cause), |
| 151 | + ), |
| 152 | + Effect.mapError((cause) => new PreviewAutomationRecordingTransferError({ threadId, cause })), |
| 153 | + ); |
| 154 | + const { uploadedAttachmentId: _uploadedAttachmentId, ...recording } = artifact; |
| 155 | + return { ...recording, id: finalId, path: finalPath }; |
| 156 | +}); |
| 157 | + |
70 | 158 | const handlers = { |
71 | 159 | preview_status: (input) => invokeTargeted<PreviewAutomationStatus>("status", input ?? {}), |
72 | 160 | preview_open: (input) => |
@@ -94,7 +182,15 @@ const handlers = { |
94 | 182 | preview_recording_start: (input) => |
95 | 183 | invokeTargeted<PreviewAutomationRecordingStatus>("recordingStart", input ?? {}), |
96 | 184 | preview_recording_stop: (input) => |
97 | | - invokeTargeted<PreviewAutomationRecordingArtifact>("recordingStop", input ?? {}), |
| 185 | + Effect.gen(function* () { |
| 186 | + const scope = yield* McpInvocationContext.requireMcpCapability("preview"); |
| 187 | + const response = yield* invokeTargeted<unknown>( |
| 188 | + "recordingStop", |
| 189 | + { ...input, transferToEnvironment: true }, |
| 190 | + PREVIEW_RECORDING_STOP_TIMEOUT_MS, |
| 191 | + ); |
| 192 | + return yield* claimPreviewRecording(scope.threadId, response); |
| 193 | + }), |
98 | 194 | } satisfies Parameters<typeof PreviewToolkit.toLayer>[0]; |
99 | 195 |
|
100 | 196 | const { preview_snapshot, ...standardHandlers } = handlers; |
|
0 commit comments