File size: 1,140 Bytes
087e4a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"use strict";

const path = require("path");

async function main() {
  const mode = process.argv[2];
  const toolPath = process.argv[3];
  const rawArgs = process.argv[4] || "{}";
  if (!["metadata", "execute"].includes(mode)) {
    throw new Error("mode must be metadata or execute");
  }
  if (!toolPath) {
    throw new Error("tool path is required");
  }
  const resolvedToolPath = path.resolve(toolPath);
  const tool = require(resolvedToolPath);
  if (!tool || typeof tool !== "object") {
    throw new Error("tool module must export an object");
  }
  if (!tool.metadata || typeof tool.metadata !== "object") {
    throw new Error("tool module must export metadata");
  }
  if (mode === "metadata") {
    console.log(JSON.stringify({ metadata: tool.metadata }));
    return;
  }
  if (typeof tool.execute !== "function") {
    throw new Error("tool module must export execute(args)");
  }
  const args = JSON.parse(rawArgs);
  const result = await tool.execute(args);
  console.log(JSON.stringify({ result }));
}

main().catch((error) => {
  console.error(error && error.stack ? error.stack : String(error));
  process.exit(1);
});