aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/run-agent.js
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-12-12 12:22:51 +0800
committerGitHub <noreply@github.com>2024-12-12 12:22:51 +0800
commit3584b5c31f4f1f1baa1c350fa46a18752f8bf7c4 (patch)
tree586df9099399c8c8a3a0e9abd85d7d4577726231 /scripts/run-agent.js
parent2fc9b476907e6fb830fd57f69d59c794548ad28b (diff)
downloadllm-functions-docker-3584b5c31f4f1f1baa1c350fa46a18752f8bf7c4.tar.gz
feat: support env var `LLM_DUMP_RESULTS` (#144)
Diffstat (limited to 'scripts/run-agent.js')
-rwxr-xr-xscripts/run-agent.js53
1 files changed, 22 insertions, 31 deletions
diff --git a/scripts/run-agent.js b/scripts/run-agent.js
index 44cc39f..6a14990 100755
--- a/scripts/run-agent.js
+++ b/scripts/run-agent.js
@@ -1,8 +1,9 @@
#!/usr/bin/env node
+// Usage: ./run-agent.js <agent-name> <agent-func> <agent-data>
+
const path = require("path");
-const { createWriteStream } = require("fs");
-const { readFile } = require("fs/promises");
+const { readFile, writeFile } = require("fs/promises");
const os = require("os");
async function main() {
@@ -13,7 +14,7 @@ async function main() {
await setupEnv(rootDir, agentName, agentFunc);
const agentToolsPath = path.resolve(rootDir, `agents/${agentName}/tools.js`);
- await run(agentToolsPath, agentFunc, agentData);
+ await run(agentName, agentToolsPath, agentFunc, agentData);
}
function parseArgv(thisFileName) {
@@ -104,7 +105,7 @@ async function loadEnv(filePath) {
}
}
-async function run(agentPath, agentFunc, agentData) {
+async function run(agentName, agentPath, agentFunc, agentData) {
let mod;
if (os.platform() === "win32") {
agentPath = `file://${agentPath}`;
@@ -118,55 +119,45 @@ async function run(agentPath, agentFunc, agentData) {
throw new Error(`Not module function '${agentFunc}' at '${agentPath}'`);
}
const value = await mod[agentFunc](agentData);
- returnToLLM(value);
- await dumpResult();
+ await returnToLLM(value);
+ await dumpResult(`${agentName}:${agentFunc}`);
}
-function returnToLLM(value) {
+async 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 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") {
- writer.write(value.toString());
+ 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));
- writer.write(valueStr);
+ await write(valueStr);
}
}
}
-async function dumpResult() {
- if (!process.stdout.isTTY) {
- return;
- }
- if (!process.env["LLM_OUTPUT"]) {
+async function dumpResult(name) {
+ if (!process.env["LLM_DUMP_RESULTS"] || !process.env["LLM_OUTPUT"] || !process.stdout.isTTY) {
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') {
+ try {
+ if (new RegExp(`\\b(${process.env["LLM_DUMP_RESULTS"]})\\b`).test(name)) {
showResult = true;
}
- }
+ } catch { }
+
if (!showResult) {
return;
}