Skip to content

Commit

Permalink
feat: controls displaying the results from function call (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Oct 18, 2024
1 parent b5377bf commit 6eb391b
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 20 deletions.
65 changes: 56 additions & 9 deletions scripts/run-agent.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/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() {
const [agentName, agentFunc, rawData] = parseArgv("run-agent.js");
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);
Expand Down Expand Up @@ -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",
Expand All @@ -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) => {
Expand All @@ -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) {
Expand All @@ -93,6 +95,7 @@ async function run(agentPath, agentFunc, agentData) {
}
const value = await mod[agentFunc](agentData);
returnToLLM(value);
await dumpResult();
}

function returnToLLM(value) {
Expand All @@ -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") {
Expand All @@ -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);
}
})();
47 changes: 44 additions & 3 deletions scripts/run-agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand All @@ -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)
32 changes: 32 additions & 0 deletions scripts/run-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -91,7 +92,38 @@ 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() {
Expand Down
51 changes: 43 additions & 8 deletions scripts/run-tool.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/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() {
const [toolName, rawData] = parseArgv("run-tool.js");
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);
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) {
Expand All @@ -81,6 +82,7 @@ async function run(toolPath, toolFunc, toolData) {
}
const value = await mod[toolFunc](toolData);
returnToLLM(value);
await dumpResult();
}

function returnToLLM(value) {
Expand All @@ -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") {
Expand All @@ -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();
Expand Down
32 changes: 32 additions & 0 deletions scripts/run-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 6eb391b

Please sign in to comment.