feat: controls displaying the results from function call (#111)
This commit is contained in:
+56
-9
@@ -1,7 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const { createWriteStream } = require("fs");
|
||||
const { readFile } = require("fs/promises");
|
||||
const os = require("os");
|
||||
|
||||
async function main() {
|
||||
@@ -9,7 +10,7 @@ async function main() {
|
||||
const agentData = parseRawData(rawData);
|
||||
|
||||
const rootDir = path.resolve(__dirname, "..");
|
||||
setupEnv(rootDir, agentName);
|
||||
await setupEnv(rootDir, agentName, agentFunc);
|
||||
|
||||
const agentToolsPath = path.resolve(rootDir, `agents/${agentName}/tools.js`);
|
||||
await run(agentToolsPath, agentFunc, agentData);
|
||||
@@ -48,10 +49,11 @@ function parseRawData(data) {
|
||||
}
|
||||
}
|
||||
|
||||
function setupEnv(rootDir, agentName) {
|
||||
loadEnv(path.resolve(rootDir, ".env"));
|
||||
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",
|
||||
@@ -64,9 +66,9 @@ function setupEnv(rootDir, agentName) {
|
||||
);
|
||||
}
|
||||
|
||||
function loadEnv(filePath) {
|
||||
async function loadEnv(filePath) {
|
||||
try {
|
||||
const data = fs.readFileSync(filePath, "utf-8");
|
||||
const data = await readFile(filePath, "utf-8");
|
||||
const lines = data.split("\n");
|
||||
|
||||
lines.forEach((line) => {
|
||||
@@ -75,7 +77,7 @@ function loadEnv(filePath) {
|
||||
const [key, ...value] = line.split("=");
|
||||
process.env[key.trim()] = value.join("=").trim();
|
||||
});
|
||||
} catch {}
|
||||
} catch { }
|
||||
}
|
||||
|
||||
async function run(agentPath, agentFunc, agentData) {
|
||||
@@ -93,6 +95,7 @@ async function run(agentPath, agentFunc, agentData) {
|
||||
}
|
||||
const value = await mod[agentFunc](agentData);
|
||||
returnToLLM(value);
|
||||
await dumpResult();
|
||||
}
|
||||
|
||||
function returnToLLM(value) {
|
||||
@@ -101,7 +104,7 @@ function returnToLLM(value) {
|
||||
}
|
||||
let writer = process.stdout;
|
||||
if (process.env["LLM_OUTPUT"]) {
|
||||
writer = fs.createWriteStream(process.env["LLM_OUTPUT"]);
|
||||
writer = createWriteStream(process.env["LLM_OUTPUT"]);
|
||||
}
|
||||
const type = typeof value;
|
||||
if (type === "string" || type === "number" || type === "boolean") {
|
||||
@@ -116,4 +119,48 @@ function returnToLLM(value) {
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
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];
|
||||
|
||||
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);
|
||||
}
|
||||
})();
|
||||
|
||||
+44
-3
@@ -11,7 +11,7 @@ def main():
|
||||
agent_data = parse_raw_data(raw_data)
|
||||
|
||||
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||
setup_env(root_dir, agent_name)
|
||||
setup_env(root_dir, agent_name, agent_func)
|
||||
|
||||
agent_tools_path = os.path.join(root_dir, f"agents/{agent_name}/tools.py")
|
||||
run(agent_tools_path, agent_func, agent_data)
|
||||
@@ -49,10 +49,11 @@ def parse_argv(this_file_name):
|
||||
return agent_name, agent_func, agent_data
|
||||
|
||||
|
||||
def setup_env(root_dir, agent_name):
|
||||
def setup_env(root_dir, agent_name, agent_func):
|
||||
load_env(os.path.join(root_dir, ".env"))
|
||||
os.environ["LLM_ROOT_DIR"] = root_dir
|
||||
os.environ["LLM_AGENT_NAME"] = agent_name
|
||||
os.environ["LLM_AGENT_FUNC"] = agent_func
|
||||
os.environ["LLM_AGENT_ROOT_DIR"] = os.path.join(root_dir, "agents", agent_name)
|
||||
os.environ["LLM_AGENT_CACHE_DIR"] = os.path.join(root_dir, "cache", agent_name)
|
||||
|
||||
@@ -86,6 +87,42 @@ def run(agent_path, agent_func, agent_data):
|
||||
|
||||
value = getattr(mod, agent_func)(**agent_data)
|
||||
return_to_llm(value)
|
||||
dump_result()
|
||||
|
||||
|
||||
def dump_result():
|
||||
if not os.isatty(1):
|
||||
return
|
||||
|
||||
if not os.getenv("LLM_OUTPUT"):
|
||||
return
|
||||
|
||||
show_result = False
|
||||
agent_name = os.environ["LLM_AGENT_NAME"].upper().replace("-", "_")
|
||||
agent_env_name = f"LLM_AGENT_DUMP_RESULT_{agent_name}"
|
||||
agent_env_value = os.getenv(agent_env_name)
|
||||
|
||||
func_name = os.environ["LLM_AGENT_FUNC"].upper().replace("-", "_")
|
||||
func_env_name = f"{agent_env_name}_{func_name}"
|
||||
func_env_value = os.getenv(func_env_name)
|
||||
|
||||
if agent_env_value in ("1", "true"):
|
||||
if func_env_value not in ("0", "false"):
|
||||
show_result = True
|
||||
else:
|
||||
if func_env_value in ("1", "true"):
|
||||
show_result = True
|
||||
|
||||
if not show_result:
|
||||
return
|
||||
|
||||
try:
|
||||
with open(os.environ["LLM_OUTPUT"], "r", encoding="utf-8") as f:
|
||||
data = f.read()
|
||||
except:
|
||||
return
|
||||
|
||||
print(f"\x1b[2m----------------------\n{data}\n----------------------\x1b[0m")
|
||||
|
||||
|
||||
def return_to_llm(value):
|
||||
@@ -107,4 +144,8 @@ def return_to_llm(value):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
try:
|
||||
main()
|
||||
except Exception as e:
|
||||
print(e, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
@@ -29,6 +29,7 @@ setup_env() {
|
||||
load_env "$root_dir/.env"
|
||||
export LLM_ROOT_DIR="$root_dir"
|
||||
export LLM_AGENT_NAME="$agent_name"
|
||||
export LLM_AGENT_FUNC="$agent_func"
|
||||
export LLM_AGENT_ROOT_DIR="$LLM_ROOT_DIR/agents/$agent_name"
|
||||
export LLM_AGENT_CACHE_DIR="$LLM_ROOT_DIR/cache/$agent_name"
|
||||
}
|
||||
@@ -91,9 +92,40 @@ EOF
|
||||
eval "'$tools_path' '$agent_func' $args"
|
||||
if [[ "$no_llm_output" -eq 1 ]]; then
|
||||
cat "$LLM_OUTPUT"
|
||||
else
|
||||
dump_result
|
||||
fi
|
||||
}
|
||||
|
||||
dump_result() {
|
||||
if [ ! -t 1 ]; then
|
||||
return;
|
||||
fi
|
||||
|
||||
local agent_env_name agent_env_value func_env_name func_env_value show_result=0
|
||||
agent_env_name="LLM_AGENT_DUMP_RESULT_$(echo "$LLM_AGENT_NAME" | tr '[:lower:]' '[:upper:]' | tr '-' '_')"
|
||||
agent_env_value="${!agent_env_name}"
|
||||
func_env_name="${agent_env_name}_$(echo "$LLM_AGENT_FUNC" | tr '[:lower:]' '[:upper:]' | tr '-' '_')"
|
||||
func_env_value="${!func_env_name}"
|
||||
if [[ "$agent_env_value" == "1" || "$agent_env_value" == "true" ]]; then
|
||||
if [[ "$func_env_value" != "0" && "$func_env_value" != "false" ]]; then
|
||||
show_result=1
|
||||
fi
|
||||
else
|
||||
if [[ "$func_env_value" == "1" || "$func_env_value" == "true" ]]; then
|
||||
show_result=1
|
||||
fi
|
||||
fi
|
||||
if [[ "$show_result" -ne 1 ]]; then
|
||||
return
|
||||
fi
|
||||
cat <<EOF
|
||||
$(echo -e "\e[2m")----------------------
|
||||
$(cat "$LLM_OUTPUT")
|
||||
----------------------$(echo -e "\e[0m")
|
||||
EOF
|
||||
}
|
||||
|
||||
die() {
|
||||
echo "$*" >&2
|
||||
exit 1
|
||||
|
||||
+43
-8
@@ -1,7 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const { createWriteStream } = require("fs");
|
||||
const { readFile } = require("fs/promises");
|
||||
const os = require("os");
|
||||
|
||||
async function main() {
|
||||
@@ -9,7 +10,7 @@ async function main() {
|
||||
const toolData = parseRawData(rawData);
|
||||
|
||||
const rootDir = path.resolve(__dirname, "..");
|
||||
setupEnv(rootDir, toolName);
|
||||
await setupEnv(rootDir, toolName);
|
||||
|
||||
const toolPath = path.resolve(rootDir, `tools/${toolName}.js`);
|
||||
await run(toolPath, "run", toolData);
|
||||
@@ -45,16 +46,16 @@ function parseRawData(data) {
|
||||
}
|
||||
}
|
||||
|
||||
function setupEnv(rootDir, toolName) {
|
||||
loadEnv(path.resolve(rootDir, ".env"));
|
||||
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);
|
||||
}
|
||||
|
||||
function loadEnv(filePath) {
|
||||
async function loadEnv(filePath) {
|
||||
try {
|
||||
const data = fs.readFileSync(filePath, "utf-8");
|
||||
const data = await readFile(filePath, "utf-8");
|
||||
const lines = data.split("\n");
|
||||
|
||||
lines.forEach((line) => {
|
||||
@@ -63,7 +64,7 @@ function loadEnv(filePath) {
|
||||
const [key, ...value] = line.split("=");
|
||||
process.env[key.trim()] = value.join("=").trim();
|
||||
});
|
||||
} catch {}
|
||||
} catch { }
|
||||
}
|
||||
|
||||
async function run(toolPath, toolFunc, toolData) {
|
||||
@@ -81,6 +82,7 @@ async function run(toolPath, toolFunc, toolData) {
|
||||
}
|
||||
const value = await mod[toolFunc](toolData);
|
||||
returnToLLM(value);
|
||||
await dumpResult();
|
||||
}
|
||||
|
||||
function returnToLLM(value) {
|
||||
@@ -89,7 +91,7 @@ function returnToLLM(value) {
|
||||
}
|
||||
let writer = process.stdout;
|
||||
if (process.env["LLM_OUTPUT"]) {
|
||||
writer = fs.createWriteStream(process.env["LLM_OUTPUT"]);
|
||||
writer = createWriteStream(process.env["LLM_OUTPUT"]);
|
||||
}
|
||||
const type = typeof value;
|
||||
if (type === "string" || type === "number" || type === "boolean") {
|
||||
@@ -104,6 +106,39 @@ function returnToLLM(value) {
|
||||
}
|
||||
}
|
||||
|
||||
async function dumpResult() {
|
||||
if (!process.stdout.isTTY) {
|
||||
return;
|
||||
}
|
||||
if (!process.env["LLM_OUTPUT"]) {
|
||||
return;
|
||||
}
|
||||
let showResult = false;
|
||||
const toolName = process.env["LLM_TOOL_NAME"].toUpperCase().replace(/-/g, '_');
|
||||
const envName = `LLM_TOOL_DUMP_RESULT_${toolName}`;
|
||||
const envValue = process.env[envName];
|
||||
if (process.env.LLM_TOOL_DUMP_RESULT === '1' || process.env.LLM_TOOL_DUMP_RESULT === 'true') {
|
||||
if (envValue !== '0' && envValue !== 'false') {
|
||||
showResult = true;
|
||||
}
|
||||
} else {
|
||||
if (envValue === '1' || envValue === '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();
|
||||
|
||||
@@ -82,6 +82,7 @@ def run(tool_path, tool_func, tool_data):
|
||||
|
||||
value = getattr(mod, tool_func)(**tool_data)
|
||||
return_to_llm(value)
|
||||
dump_result()
|
||||
|
||||
|
||||
def return_to_llm(value):
|
||||
@@ -102,6 +103,37 @@ def return_to_llm(value):
|
||||
writer.write(value_str)
|
||||
|
||||
|
||||
def dump_result():
|
||||
if not os.isatty(1):
|
||||
return
|
||||
|
||||
if not os.getenv("LLM_OUTPUT"):
|
||||
return
|
||||
|
||||
show_result = False
|
||||
tool_name = os.environ["LLM_TOOL_NAME"].upper().replace("-", "_")
|
||||
env_name = f"LLM_TOOL_DUMP_RESULT_{tool_name}"
|
||||
env_value = os.getenv(env_name)
|
||||
|
||||
if os.getenv("LLM_TOOL_DUMP_RESULT") in ("1", "true"):
|
||||
if env_value not in ("0", "false"):
|
||||
show_result = True
|
||||
else:
|
||||
if env_value in ("1", "true"):
|
||||
show_result = True
|
||||
|
||||
if not show_result:
|
||||
return
|
||||
|
||||
try:
|
||||
with open(os.environ["LLM_OUTPUT"], "r", encoding="utf-8") as f:
|
||||
data = f.read()
|
||||
except:
|
||||
return
|
||||
|
||||
print(f"\x1b[2m----------------------\n{data}\n----------------------\x1b[0m")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
|
||||
@@ -88,9 +88,37 @@ EOF
|
||||
eval "'$tool_path' $args"
|
||||
if [[ "$no_llm_output" -eq 1 ]]; then
|
||||
cat "$LLM_OUTPUT"
|
||||
else
|
||||
dump_result
|
||||
fi
|
||||
}
|
||||
|
||||
dump_result() {
|
||||
if [ ! -t 1 ]; then
|
||||
return;
|
||||
fi
|
||||
local env_name env_value show_result=0
|
||||
env_name="LLM_TOOL_DUMP_RESULT_$(echo "$LLM_TOOL_NAME" | tr '[:lower:]' '[:upper:]' | tr '-' '_')"
|
||||
env_value="${!env_name}"
|
||||
if [[ "$LLM_TOOL_DUMP_RESULT" == "1" || "$LLM_TOOL_DUMP_RESULT" == "true" ]]; then
|
||||
if [[ "$env_value" != "0" && "$env_value" != "false" ]]; then
|
||||
show_result=1
|
||||
fi
|
||||
else
|
||||
if [[ "$env_value" == "1" || "$env_value" == "true" ]]; then
|
||||
show_result=1
|
||||
fi
|
||||
fi
|
||||
if [[ "$show_result" -ne 1 ]]; then
|
||||
return
|
||||
fi
|
||||
cat <<EOF
|
||||
$(echo -e "\e[2m")----------------------
|
||||
$(cat "$LLM_OUTPUT")
|
||||
----------------------$(echo -e "\e[0m")
|
||||
EOF
|
||||
}
|
||||
|
||||
die() {
|
||||
echo "$*" >&2
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user