aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/run-agent.js
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-07-10 18:53:32 +0800
committerGitHub <noreply@github.com>2024-07-10 18:53:32 +0800
commit732eae532c8e8632db95ab80e0dde5071e744386 (patch)
tree94e9f6d15b64cff5a26d6bbf44f16c53deb8c324 /scripts/run-agent.js
parent01e07c0cc0be0b1600d688616d12ad0afa9edc71 (diff)
downloadllm-functions-docker-732eae532c8e8632db95ab80e0dde5071e744386.tar.gz
feat: adjust the way of returning data to LLM (#69)
Diffstat (limited to 'scripts/run-agent.js')
-rwxr-xr-xscripts/run-agent.js26
1 files changed, 19 insertions, 7 deletions
diff --git a/scripts/run-agent.js b/scripts/run-agent.js
index 69dd119..36fc66b 100755
--- a/scripts/run-agent.js
+++ b/scripts/run-agent.js
@@ -49,11 +49,19 @@ function parseRawData(data) {
}
function setupEnv(rootDir, agentName) {
- process.env["LLM_ROOT_DIR"] = rootDir;
loadEnv(path.resolve(rootDir, ".env"));
+ process.env["LLM_ROOT_DIR"] = rootDir;
process.env["LLM_AGENT_NAME"] = agentName;
- process.env["LLM_AGENT_ROOT_DIR"] = path.resolve(rootDir, "agents", agentName);
- process.env["LLM_AGENT_CACHE_DIR"] = path.resolve(rootDir, "cache", agentName);
+ process.env["LLM_AGENT_ROOT_DIR"] = path.resolve(
+ rootDir,
+ "agents",
+ agentName,
+ );
+ process.env["LLM_AGENT_CACHE_DIR"] = path.resolve(
+ rootDir,
+ "cache",
+ agentName,
+ );
}
function loadEnv(filePath) {
@@ -84,22 +92,26 @@ async function run(agentPath, agentFunc, agentData) {
throw new Error(`Not module function '${agentFunc}' at '${agentPath}'`);
}
const value = await mod[agentFunc](agentData);
- dumpValue(value);
+ returnToLLM(value);
}
-function dumpValue(value) {
+function returnToLLM(value) {
if (value === null || value === undefined) {
return;
}
+ let writer = process.stdout;
+ if (process.env["LLM_OUTPUT"]) {
+ writer = fs.createWriteStream(process.env["LLM_OUTPUT"]);
+ }
const type = typeof value;
if (type === "string" || type === "number" || type === "boolean") {
- console.log(value);
+ 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));
- console.log(valueStr);
+ writer.write(value);
}
}
}