|
| 1 | +import { logger } from "@/core/logger"; |
| 2 | +import { program } from "@/core/program"; |
| 3 | +import { getGenerator } from "@/generators"; |
| 4 | +import { |
| 5 | + CollectedDataSave, |
| 6 | + CollectedDataSchema, |
| 7 | +} from "@/validation/collected-data-schema"; |
| 8 | +import { FileSchema } from "@/validation/file-schema"; |
| 9 | +import { JSONSchema } from "@/validation/json-schema"; |
| 10 | +import { AbstractGenerator } from "@peerbench/sdk"; |
| 11 | +import { z } from "zod"; |
| 12 | +import { savePrompts } from "./save-prompts"; |
| 13 | +import { hashFile } from "@/utils/hash-file"; |
| 14 | +import { signFile } from "@/utils/sign-file"; |
| 15 | +import { OptionOutputDir } from "../options/output-dir"; |
| 16 | +import { OptionTags } from "../options/tags"; |
| 17 | +import fs from "fs/promises"; |
| 18 | + |
| 19 | +program |
| 20 | + .command("generate") |
| 21 | + .description("Generates new Prompts") |
| 22 | + .requiredOption( |
| 23 | + "-g, --generator <identifier>", |
| 24 | + "Identifier of the Generator", |
| 25 | + (value) => { |
| 26 | + const generator = getGenerator(value); |
| 27 | + if (!generator) { |
| 28 | + throw new Error(`Generator "${value}" not found`); |
| 29 | + } |
| 30 | + return generator; |
| 31 | + } |
| 32 | + ) |
| 33 | + .requiredOption( |
| 34 | + "-f, --file <path>", |
| 35 | + "Path to the collected data file", |
| 36 | + (value) => |
| 37 | + FileSchema({ message: "Data file doesn't exist" }) |
| 38 | + .pipe( |
| 39 | + JSONSchema({ |
| 40 | + message: "Given data file is invalid", |
| 41 | + schema: CollectedDataSchema, |
| 42 | + }) |
| 43 | + ) |
| 44 | + .parse(value) |
| 45 | + ) |
| 46 | + .addOption(OptionOutputDir("generated").makeOptionMandatory(true)) |
| 47 | + .option( |
| 48 | + "--options <path or JSON string>", |
| 49 | + "Path to the options file or a JSON string that will be passed to the Generator", |
| 50 | + (value) => |
| 51 | + z |
| 52 | + .union([ |
| 53 | + JSONSchema<any>(), |
| 54 | + FileSchema().pipe( |
| 55 | + JSONSchema({ |
| 56 | + message: "Given options file is invalid", |
| 57 | + schema: z.record(z.string(), z.unknown()), |
| 58 | + }) |
| 59 | + ), |
| 60 | + ]) |
| 61 | + .parse(value) |
| 62 | + ) |
| 63 | + .option( |
| 64 | + "--args <path or JSON string>", |
| 65 | + "Path to the initialization args file or a JSON string that will be passed to the Generator", |
| 66 | + (value) => |
| 67 | + z |
| 68 | + .union([ |
| 69 | + JSONSchema<any>(), |
| 70 | + FileSchema().pipe( |
| 71 | + JSONSchema({ |
| 72 | + message: "Given args file is invalid", |
| 73 | + schema: z.array(z.unknown()), |
| 74 | + }) |
| 75 | + ), |
| 76 | + ]) |
| 77 | + .parse(value) |
| 78 | + ) |
| 79 | + .addOption(OptionTags()) |
| 80 | + .action( |
| 81 | + async (options: { |
| 82 | + generator: AbstractGenerator; |
| 83 | + file: CollectedDataSave; |
| 84 | + tags: string[]; |
| 85 | + options: Record<string, unknown>; |
| 86 | + args: unknown[]; |
| 87 | + output: string; |
| 88 | + }) => { |
| 89 | + // Ensure the output directory exists |
| 90 | + await fs.mkdir(options.output, { recursive: true }); |
| 91 | + |
| 92 | + logger.info("Initializing Generator"); |
| 93 | + |
| 94 | + // Initialize the Generator |
| 95 | + await options.generator.initialize(options.args); |
| 96 | + |
| 97 | + logger.info(`Checking the Generator eligibility for the given file`); |
| 98 | + const canHandle = await options.generator.canHandle(options.file); |
| 99 | + if (!canHandle) { |
| 100 | + throw new Error("Generator doesn't support the given file"); |
| 101 | + } |
| 102 | + |
| 103 | + logger.info(`Generating Prompts`); |
| 104 | + const prompts = await options.generator.generate( |
| 105 | + options.file.data, |
| 106 | + options.options |
| 107 | + ); |
| 108 | + logger.info(`Prompts generated successfully`); |
| 109 | + |
| 110 | + logger.info(`Saving Prompts`); |
| 111 | + // Save the data |
| 112 | + const filePath = await savePrompts({ |
| 113 | + generatorIdentifier: options.generator.identifier, |
| 114 | + outputDirectory: options.output, |
| 115 | + prompts, |
| 116 | + tags: options.tags, |
| 117 | + }); |
| 118 | + |
| 119 | + // Hash and sign the output file |
| 120 | + await hashFile(filePath); |
| 121 | + await signFile(`${filePath}.cid`); // Only sign the hash file |
| 122 | + |
| 123 | + logger.info(`Prompts saved to: ${filePath}`); |
| 124 | + } |
| 125 | + ); |
0 commit comments