Skip to content

Commit 8e25114

Browse files
authored
feat(agents): json-viewer use jq cli other than node-jq module (#163)
1 parent 4ac72b5 commit 8e25114

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

agents/json-viewer/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"dependencies": {
33
"@inquirer/input": "^4.0.2",
4-
"json-schema-generator": "^2.0.6",
5-
"node-jq": "^6.0.1"
4+
"json-schema-generator": "^2.0.6"
65
}
76
}

agents/json-viewer/tools.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const fs = require("node:fs/promises");
2-
const { exec } = require("node:child_process");
2+
const { exec, spawn } = require("node:child_process");
33
const { promisify } = require("node:util");
44
const path = require("node:path");
55
const { tmpdir } = require("node:os");
66

7-
const jq = require("node-jq");
87
const jsonSchemaGenerator = require("json-schema-generator");
98
const input = require("@inquirer/input").default;
109

@@ -17,7 +16,7 @@ exports._instructions = async function () {
1716
json_file_path = value;
1817
} catch {
1918
generate_json_command_context = `command_to_generate_json: \`${value}\`\n`;
20-
const { stdout } = await promisify(exec)(value, { maxBuffer: 100 * 1024 * 1024});
19+
const { stdout } = await promisify(exec)(value, { maxBuffer: 100 * 1024 * 1024 });
2120
json_file_path = path.join(tmpdir(), `${process.env.LLM_AGENT_NAME}-${process.pid}.data.json`);
2221
await fs.writeFile(json_file_path, stdout);
2322
console.log(`ⓘ Generated json data saved to: ${json_file_path}`);
@@ -44,6 +43,19 @@ json_schema: ${JSON.stringify(json_schema, null, 2)}
4443
*/
4544
exports.print_json = async function (args) {
4645
const { json_file_path, jq_expr } = args;
47-
const result = await jq.run(jq_expr, json_file_path, { raw: true });
48-
console.log(result);
46+
return new Promise((resolve, reject) => {
47+
const child = spawn("jq", ["-r", jq_expr, json_file_path], { stdio: "inherit" });
48+
49+
child.on('close', code => {
50+
if (code === 0) {
51+
resolve();
52+
} else {
53+
reject(new Error(`jq exited with code ${code}`));
54+
}
55+
});
56+
57+
child.on('error', err => {
58+
reject(err);
59+
});
60+
});
4961
}

0 commit comments

Comments
 (0)