|
| 1 | +import { publishFileSync } from '@agent-device/host-kit/file'; |
| 2 | +import { emitDiagnostic } from '@agent-device/host-kit/diagnostics'; |
| 3 | +import type { ArtifactAdapter, ReservedOutputFile } from '@agent-device/contracts/artifact-adapter'; |
| 4 | +import { AppError, errorMessage } from '@agent-device/kernel/errors'; |
| 5 | +import { promises as fs } from 'node:fs'; |
| 6 | +import os from 'node:os'; |
| 7 | +import path from 'node:path'; |
| 8 | + |
| 9 | +export function createDaemonScreenshotArtifactAdapter( |
| 10 | + atomic: boolean, |
| 11 | + signal: AbortSignal | undefined, |
| 12 | +): ArtifactAdapter { |
| 13 | + return { |
| 14 | + resolveInput: async () => { |
| 15 | + throw new AppError('UNSUPPORTED_OPERATION', 'screenshot does not resolve input artifacts'); |
| 16 | + }, |
| 17 | + reserveOutput: async (ref) => { |
| 18 | + try { |
| 19 | + let tempRoot: string | undefined; |
| 20 | + let outputPath: string; |
| 21 | + if (ref?.kind === 'path') { |
| 22 | + outputPath = ref.path; |
| 23 | + } else { |
| 24 | + tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-screenshot-')); |
| 25 | + outputPath = path.join(tempRoot, 'screenshot.png'); |
| 26 | + } |
| 27 | + await fs.mkdir(path.dirname(outputPath), { recursive: true }); |
| 28 | + if (atomic && ref?.kind === 'path') { |
| 29 | + return await reserveCroppedOutput(outputPath, signal); |
| 30 | + } |
| 31 | + return { |
| 32 | + path: outputPath, |
| 33 | + visibility: 'client-visible', |
| 34 | + publish: async () => undefined, |
| 35 | + ...(tempRoot |
| 36 | + ? { |
| 37 | + cleanup: async () => { |
| 38 | + await fs.rm(tempRoot, { recursive: true, force: true }); |
| 39 | + }, |
| 40 | + } |
| 41 | + : {}), |
| 42 | + }; |
| 43 | + } catch (error) { |
| 44 | + if (error instanceof AppError) throw error; |
| 45 | + throw screenshotPublicationError( |
| 46 | + error, |
| 47 | + ref?.kind === 'path' ? ref.path : '(temporary screenshot)', |
| 48 | + ); |
| 49 | + } |
| 50 | + }, |
| 51 | + createTempFile: async (options) => { |
| 52 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), `${options.prefix}-`)); |
| 53 | + return { |
| 54 | + path: path.join(root, `file${options.ext}`), |
| 55 | + visibility: 'internal', |
| 56 | + cleanup: async () => { |
| 57 | + await fs.rm(root, { recursive: true, force: true }); |
| 58 | + }, |
| 59 | + }; |
| 60 | + }, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +function screenshotPublicationError(error: unknown, outputPath: string): AppError { |
| 65 | + return new AppError('COMMAND_FAILED', 'Failed to publish screenshot', { |
| 66 | + reason: 'screenshot_publication_failed', |
| 67 | + path: outputPath, |
| 68 | + ...(error instanceof Error && 'code' in error ? { filesystemCode: error.code } : {}), |
| 69 | + cause: errorMessage(error), |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +async function reserveCroppedOutput( |
| 74 | + outputPath: string, |
| 75 | + signal: AbortSignal | undefined, |
| 76 | +): Promise<ReservedOutputFile> { |
| 77 | + const destination = await resolveScreenshotDestination(outputPath); |
| 78 | + const stagingRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-crop-')); |
| 79 | + const stagingPath = path.join(stagingRoot, `screenshot${path.extname(outputPath) || '.png'}`); |
| 80 | + const cleanup = async () => { |
| 81 | + await fs.rm(stagingRoot, { recursive: true, force: true }).catch((error: unknown) => { |
| 82 | + emitDiagnostic({ |
| 83 | + level: 'warn', |
| 84 | + phase: 'screenshot_crop_cleanup_failed', |
| 85 | + data: { tempRoot: stagingRoot, error: errorMessage(error) }, |
| 86 | + }); |
| 87 | + }); |
| 88 | + }; |
| 89 | + return { |
| 90 | + path: stagingPath, |
| 91 | + publishedPath: outputPath, |
| 92 | + visibility: 'client-visible', |
| 93 | + publish: async () => { |
| 94 | + try { |
| 95 | + const contents = await fs.readFile(stagingPath); |
| 96 | + signal?.throwIfAborted(); |
| 97 | + publishFileSync({ destination, contents, preserveExistingMode: true }); |
| 98 | + } catch (error) { |
| 99 | + if (signal?.aborted) throw signal.reason; |
| 100 | + throw screenshotPublicationError(error, outputPath); |
| 101 | + } |
| 102 | + await cleanup(); |
| 103 | + return undefined; |
| 104 | + }, |
| 105 | + cleanup, |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +async function resolveScreenshotDestination(outputPath: string): Promise<string> { |
| 110 | + let destination: string; |
| 111 | + try { |
| 112 | + const entry = await fs.lstat(outputPath).catch((error: NodeJS.ErrnoException) => { |
| 113 | + if (error.code === 'ENOENT') return undefined; |
| 114 | + throw error; |
| 115 | + }); |
| 116 | + destination = entry?.isSymbolicLink() ? await fs.realpath(outputPath) : outputPath; |
| 117 | + const target = await fs.stat(destination).catch((error: NodeJS.ErrnoException) => { |
| 118 | + if (error.code === 'ENOENT') return undefined; |
| 119 | + throw error; |
| 120 | + }); |
| 121 | + if (target && !target.isFile()) { |
| 122 | + throw new AppError('INVALID_ARGS', 'Screenshot destination must be a regular file', { |
| 123 | + reason: 'screenshot_destination_not_file', |
| 124 | + path: outputPath, |
| 125 | + }); |
| 126 | + } |
| 127 | + } catch (error) { |
| 128 | + if (error instanceof AppError) throw error; |
| 129 | + throw screenshotPublicationError(error, outputPath); |
| 130 | + } |
| 131 | + |
| 132 | + return destination; |
| 133 | +} |
0 commit comments