aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/run-agent.js
blob: 6a027924f98fc75d1cc0a4eafb28b053a2ca59a7 (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
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node

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

async function main() {
  const [agentName, agentFunc, rawData] = parseArgv("run-agent.js");
  const agentData = parseRawData(rawData);

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

  const agentToolsPath = path.resolve(rootDir, `agents/${agentName}/tools.js`);
  await run(agentToolsPath, agentFunc, agentData);
}

function parseArgv(thisFileName) {
  let agentName = process.argv[1];
  let agentFunc = "";
  let agentData = null;

  if (agentName.endsWith(thisFileName)) {
    agentName = process.argv[2];
    agentFunc = process.argv[3];
    agentData = process.argv[4];
  } else {
    agentName = path.basename(agentName);
    agentFunc = process.argv[2];
    agentData = process.argv[3];
  }

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

  return [agentName, agentFunc, agentData];
}

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, agentName, agentFunc) {
  await loadEnv(path.resolve(rootDir, ".env"));
  process.env["LLM_ROOT_DIR"] = rootDir;
  process.env["LLM_AGENT_NAME"] = agentName;
  process.env["LLM_AGENT_FUNC"] = agentFunc;
  process.env["LLM_AGENT_ROOT_DIR"] = path.resolve(
    rootDir,
    "agents",
    agentName,
  );
  process.env["LLM_AGENT_CACHE_DIR"] = path.resolve(
    rootDir,
    "cache",
    agentName,
  );
}

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

    lines.forEach((line) => {
      if (line.trim().startsWith("#") || line.trim() === "") return;

      const [key, ...value] = line.split("=");
      const envName = key.trim();
      if (!process.env[envName]) {
        process.env[envName] = value.join("=").trim();
      }
    });
  } catch { }
}

async function run(agentPath, agentFunc, agentData) {
  let mod;
  if (os.platform() === "win32") {
    agentPath = `file://${agentPath}`;
  }
  try {
    mod = await import(agentPath);
  } catch {
    throw new Error(`Unable to load agent tools at '${agentPath}'`);
  }
  if (!mod || !mod[agentFunc]) {
    throw new Error(`Not module function '${agentFunc}' at '${agentPath}'`);
  }
  const value = await mod[agentFunc](agentData);
  returnToLLM(value);
  await dumpResult();
}

function returnToLLM(value) {
  if (value === null || value === undefined) {
    return;
  }
  let writer = process.stdout;
  if (process.env["LLM_OUTPUT"]) {
    writer = createWriteStream(process.env["LLM_OUTPUT"]);
  }
  const type = typeof value;
  if (type === "string" || type === "number" || type === "boolean") {
    writer.write(value);
  } 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));
      writer.write(valueStr);
    }
  }
}

async function dumpResult() {
  if (!process.stdout.isTTY) {
    return;
  }
  if (!process.env["LLM_OUTPUT"]) {
    return;
  }
  let showResult = false;
  const agentName = process.env["LLM_AGENT_NAME"].toUpperCase().replace(/-/g, '_');
  const agentEnvName = `LLM_AGENT_DUMP_RESULT_${agentName}`;
  const agentEnvValue = process.env[agentEnvName] || process.env["LLM_AGENT_DUMP_RESULT"];

  const funcName = process.env["LLM_AGENT_FUNC"].toUpperCase().replace(/-/g, '_');
  const funcEnvName = `${agentEnvName}_${funcName}`;
  const funcEnvValue = process.env[funcEnvName];
  if (agentEnvValue === '1' || agentEnvValue === 'true') {
    if (funcEnvValue !== '0' && funcEnvValue !== 'false') {
      showResult = true;
    }
  } else {
    if (funcEnvValue === '1' || funcEnvValue === 'true') {
      showResult = true;
    }
  }
  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`);
}

(async () => {
  try {
    await main();
  } catch (err) {
    console.error(err?.message || err);
    process.exit(1);
  }
})();