import fs from 'node:fs'; import path from 'node:path'; import { AppError } from '@agent-device/kernel/errors'; import { parseReplayScriptDetailed, readReplayScriptMetadata } from '@agent-device/ad-script'; import { resolveUserPath } from '@agent-device/host-kit/file'; import type { ReplayBackend } from '@agent-device/contracts/replay-backend'; import { writeCommandOutput } from './shared.ts'; import type { ClientCommandHandler } from './router-types.ts'; type ReplayCommandParams = Parameters[0]; export const replayCommand: ClientCommandHandler = async (params) => { const { positionals } = params; if (positionals[0] !== 'export') { return handleReplayRunCommand(params); } return await handleReplayExportCommand(params); }; function handleReplayRunCommand({ positionals, flags }: ReplayCommandParams): false { if (positionals.length > 1) { throw new AppError('INVALID_ARGS', 'replay accepts exactly one input path: replay '); } if (flags.out !== undefined) { throw new AppError('INVALID_ARGS', 'replay --out is only supported with replay export.'); } return false; } async function handleReplayExportCommand({ positionals, flags, host, }: ReplayCommandParams): Promise { validateReplayExportOptions(positionals, flags); const inputPath = positionals[1]; if (!inputPath) { throw new AppError('INVALID_ARGS', 'replay export requires an input path.'); } const sourcePath = resolveUserPath(inputPath, { cwd: host.cwd }); const script = fs.readFileSync(sourcePath, 'utf8'); const parsed = parseReplayScriptDetailed(script); // `replay export` exports to Maestro, so the conversion goes through that backend's registry // entry rather than naming the engine package from the CLI (#3377). This handler is loaded // lazily by the router, and the engine sits one further thunk behind the backend. const backend: ReplayBackend = await ( await import('@agent-device/replay-port/replay-backend-registry') ).getReplayBackend('maestro'); const result = backend.exportReplayScript(parsed.actions, { actionLines: parsed.actionLines, metadata: readReplayScriptMetadata(script), }); const outputPath = typeof flags.out === 'string' ? resolveUserPath(flags.out, { cwd: host.cwd }) : undefined; if (outputPath) { fs.mkdirSync(path.dirname(outputPath), { recursive: true }); fs.writeFileSync(outputPath, result.yaml); } for (const warning of result.warnings) { process.stderr.write(`Warning: line ${warning.line}: ${warning.message}\n`); } await writeCommandOutput( flags, { format: backend.id, sourcePath, ...(outputPath ? { path: outputPath } : { yaml: result.yaml }), warnings: result.warnings, }, () => outputPath ?? result.yaml, ); return true; } function validateReplayExportOptions( positionals: ReplayCommandParams['positionals'], flags: ReplayCommandParams['flags'], ): void { if (positionals.length > 2) { throw new AppError( 'INVALID_ARGS', 'replay export accepts exactly one input path: replay export ', ); } if (flags.replayUpdate) { throw new AppError('INVALID_ARGS', 'replay export does not support --update.'); } if (flags.replayMaestro) { throw new AppError('INVALID_ARGS', 'replay export reads .ad files; omit --maestro.'); } if (flags.replayEnv?.length) { throw new AppError('INVALID_ARGS', 'replay export does not evaluate --env substitutions.'); } }