|
| 1 | +import type { Argv } from "yargs" |
| 2 | +import { cmd } from "./cmd" |
| 3 | +import { bootstrap } from "../bootstrap" |
| 4 | +import { UI } from "../ui" |
| 5 | +import { Executor } from "../../molecule/executor" |
| 6 | +import type { Molecule } from "../../molecule/types" |
| 7 | +import { BashTool } from "../../tool/bash" |
| 8 | +import { WriteTool } from "../../tool/write" |
| 9 | +import { EditTool } from "../../tool/edit" |
| 10 | +import { ReadTool } from "../../tool/read" |
| 11 | +import { GrepTool } from "../../tool/grep" |
| 12 | +import { GlobTool } from "../../tool/glob" |
| 13 | +import { ListTool } from "../../tool/ls" |
| 14 | + |
| 15 | +export const MoleculeCommand = cmd({ |
| 16 | + command: "molecule", |
| 17 | + describe: "Execute molecules - atomic, auditable work units", |
| 18 | + builder: (yargs: Argv) => { |
| 19 | + return yargs.command( |
| 20 | + "run <spec-file>", |
| 21 | + "Execute a molecule from spec file", |
| 22 | + (yargs) => { |
| 23 | + return yargs |
| 24 | + .positional("spec-file", { |
| 25 | + describe: "Path to molecule spec file (JSON or TypeScript)", |
| 26 | + type: "string", |
| 27 | + demandOption: true, |
| 28 | + }) |
| 29 | + .option("dry-run", { |
| 30 | + describe: "Validate without executing", |
| 31 | + type: "boolean", |
| 32 | + default: false, |
| 33 | + }) |
| 34 | + }, |
| 35 | + async (args) => { |
| 36 | + const specFile = args["spec-file"] as string |
| 37 | + |
| 38 | + await bootstrap(process.cwd(), async () => { |
| 39 | + const file = Bun.file(specFile) |
| 40 | + if (!(await file.exists())) { |
| 41 | + UI.error(`Spec file not found: ${specFile}`) |
| 42 | + process.exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + const content = await file.text() |
| 46 | + let spec: Molecule.Spec |
| 47 | + |
| 48 | + if (specFile.endsWith(".json")) { |
| 49 | + spec = JSON.parse(content) |
| 50 | + } else if (specFile.endsWith(".ts") || specFile.endsWith(".js")) { |
| 51 | + const module = await import(specFile) |
| 52 | + spec = module.default || module.spec |
| 53 | + } else { |
| 54 | + UI.error("Spec file must be .json, .ts, or .js") |
| 55 | + process.exit(1) |
| 56 | + } |
| 57 | + |
| 58 | + if (args["dry-run"]) { |
| 59 | + UI.println(UI.Style.TEXT_SUCCESS_BOLD + "✓ Spec is valid") |
| 60 | + UI.println(UI.Style.TEXT_NORMAL + " ID: " + spec.id) |
| 61 | + UI.println(UI.Style.TEXT_NORMAL + " Description: " + spec.description) |
| 62 | + UI.println(UI.Style.TEXT_NORMAL + " Actions: " + spec.actions.length) |
| 63 | + UI.println(UI.Style.TEXT_NORMAL + " Oracles: " + spec.oracles.length) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + const toolRegistry = new Map() |
| 68 | + toolRegistry.set("bash", BashTool) |
| 69 | + toolRegistry.set("write", WriteTool) |
| 70 | + toolRegistry.set("edit", EditTool) |
| 71 | + toolRegistry.set("read", ReadTool) |
| 72 | + toolRegistry.set("grep", GrepTool) |
| 73 | + toolRegistry.set("glob", GlobTool) |
| 74 | + toolRegistry.set("list", ListTool) |
| 75 | + |
| 76 | + const ctx: Executor.Context = { |
| 77 | + sessionID: "molecule-" + Date.now(), |
| 78 | + messageID: "msg-" + Date.now(), |
| 79 | + agent: "build", |
| 80 | + toolRegistry, |
| 81 | + } |
| 82 | + |
| 83 | + UI.println(UI.Style.TEXT_INFO_BOLD + "▸ Executing molecule: " + spec.id) |
| 84 | + UI.println(UI.Style.TEXT_DIM + " " + spec.description) |
| 85 | + |
| 86 | + const startTime = Date.now() |
| 87 | + const result = await Executor.execute(spec, ctx) |
| 88 | + const duration = Date.now() - startTime |
| 89 | + |
| 90 | + if (result.success) { |
| 91 | + UI.println(UI.Style.TEXT_SUCCESS_BOLD + "✓ Success" + UI.Style.TEXT_DIM + ` (${duration}ms)`) |
| 92 | + UI.println(UI.Style.TEXT_NORMAL + " Input hash: " + result.attestation.inputHash.slice(0, 16) + "...") |
| 93 | + UI.println(UI.Style.TEXT_NORMAL + " Output hash: " + result.attestation.outputHash.slice(0, 16) + "...") |
| 94 | + |
| 95 | + if (result.attestation.oracleResults.length > 0) { |
| 96 | + UI.println(UI.Style.TEXT_INFO_BOLD + " Oracles:") |
| 97 | + for (const oracle of result.attestation.oracleResults) { |
| 98 | + const status = oracle.passed ? "✓" : "✗" |
| 99 | + const color = oracle.passed ? UI.Style.TEXT_SUCCESS : UI.Style.TEXT_DANGER |
| 100 | + UI.println(color + " " + status + " " + oracle.oracle.check) |
| 101 | + } |
| 102 | + } |
| 103 | + } else { |
| 104 | + UI.println(UI.Style.TEXT_DANGER_BOLD + "✗ Failed" + UI.Style.TEXT_DIM + ` (${duration}ms)`) |
| 105 | + if (result.errors) { |
| 106 | + for (const error of result.errors) { |
| 107 | + UI.println(UI.Style.TEXT_DANGER + " " + error.message) |
| 108 | + } |
| 109 | + } |
| 110 | + process.exit(1) |
| 111 | + } |
| 112 | + }) |
| 113 | + }, |
| 114 | + ) |
| 115 | + }, |
| 116 | + handler: () => {}, |
| 117 | +}) |
0 commit comments