aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/run-tool.js
blob: 841fbc07a6879baaa78c3755dca6f35570cd69a3 (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node

// Usage: ./run-tool.js <tool-name> <tool-data>

const path = require("path");
const { readFile, writeFile } = require("fs/promises");
const os = require("os");

async function main() {
  const [toolName, rawData] = parseArgv("run-tool.js");
  const toolData = parseRawData(rawData);

  const rootDir = path.resolve(__dirname, "..");
  await setupEnv(rootDir, toolName);

  const toolPath = path.resolve(rootDir, `tools/${toolName}.js`);
  await run(toolName, toolPath, "run", toolData);
}

function parseArgv(thisFileName) {
  let toolName = process.argv[1];
  let toolData = null;

  if (toolName.endsWith(thisFileName)) {
    toolName = process.argv[2];
    toolData = process.argv[3];
  } else {
    toolName = path.basename(toolName);
    toolData = process.argv[2];
  }

  if (toolName && toolName.endsWith(".js")) {
    toolName = toolName.slice(0, -3);
  }

  if (!toolData || !toolName) {
    console.log(`Usage: ./run-tools.js <tool-name> <tool-data>`);
    process.exit(1);
  }

  return [toolName, toolData];
}

function parseRawData(data) {
  if (!data) {
    throw new Error("No JSON data");
  }
  try {
    return JSON.parse(data);
  } catch {
    throw new Error("Invalid JSON data");
  }
}

async function setupEnv(rootDir, toolName) {
  await loadEnv(path.resolve(rootDir, ".env"));
  process.env["LLM_ROOT_DIR"] = rootDir;
  process.env["LLM_TOOL_NAME"] = toolName;
  process.env["LLM_TOOL_CACHE_DIR"] = path.resolve(rootDir, "cache", toolName);
}

async function loadEnv(filePath) {
  let lines = [];
  try {
    const data = await readFile(filePath, "utf-8");
    lines = data.split("\n");
  } catch {
    return;
  }

  const envVars = new Map();

  for (const line of lines) {
    if (line.trim().startsWith("#") || line.trim() === "") {
      continue;
    }

    const [key, ...valueParts] = line.split("=");
    const envName = key.trim();

    if (!process.env[envName]) {
      let envValue = valueParts.join("=").trim();
      if ((envValue.startsWith('"') && envValue.endsWith('"')) || (envValue.startsWith("'") && envValue.endsWith("'"))) {
        envValue = envValue.slice(1, -1);
      }
      envVars.set(envName, envValue);
    }
  }

  for (const [envName, envValue] of envVars.entries()) {
    process.env[envName] = envValue;
  }
}

async function run(toolName, toolPath, toolFunc, toolData) {
  if (os.platform() === "win32") {
    toolPath = `file://${toolPath}`;
  }
  const mod = await import(toolPath);
  if (!mod || !mod[toolFunc]) {
    throw new Error(`Not module function '${toolFunc}' at '${toolPath}'`);
  }
  const value = await mod[toolFunc](toolData);
  await returnToLLM(value);
  await dumpResult(toolName);
}

async function returnToLLM(value) {
  if (value === null || value === undefined) {
    return;
  }
  const write = async (value) => {
    if (process.env["LLM_OUTPUT"]) {
      await writeFile(process.env["LLM_OUTPUT"], value);
    } else {
      process.stdout.write(value);
    }
  }
  const type = typeof value;
  if (type === "string" || type === "number" || type === "boolean") {
    await write(value.toString());
  } else if (type === "object") {
    const proto = Object.prototype.toString.call(value);
    if (proto === "[object Object]" || proto === "[object Array]") {
      const valueStr = JSON.stringify(value, null, 2);
      require("assert").deepStrictEqual(value, JSON.parse(valueStr));
      await write(valueStr);
    }
  }
}

async function dumpResult(name) {
  if (!process.env["LLM_DUMP_RESULTS"] || !process.env["LLM_OUTPUT"] || !process.stdout.isTTY) {
    return;
  }
  let showResult = false;
  try {
    if (new RegExp(`\\b(${process.env["LLM_DUMP_RESULTS"]})\\b`).test(name)) {
      showResult = true;
    }
  } catch { }

  if (!showResult) {
    return;
  }

  let data = "";
  try {
    data = await readFile(process.env["LLM_OUTPUT"], "utf-8");
  } catch {
    return;
  }
  process.stdout.write(`\x1b[2m----------------------\n${data}\n----------------------\x1b[0m\n`);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});