diff options
| author | sigoden <sigoden@gmail.com> | 2025-02-12 21:01:42 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-12 21:01:42 +0800 |
| commit | 8e2511493d33d001f86fdd5304db109e63eeccd9 (patch) | |
| tree | f0ca9e76a5bf3a1e7d7b22990151d2f61d45d00e | |
| parent | 4ac72b584526765f980baca4454b613e0f56c1fb (diff) | |
| download | llm-functions-docker-8e2511493d33d001f86fdd5304db109e63eeccd9.tar.gz | |
feat(agents): json-viewer use jq cli other than node-jq module (#163)
| -rw-r--r-- | agents/json-viewer/package.json | 3 | ||||
| -rw-r--r-- | agents/json-viewer/tools.js | 22 |
2 files changed, 18 insertions, 7 deletions
diff --git a/agents/json-viewer/package.json b/agents/json-viewer/package.json index 3b6bbc6..3a274cf 100644 --- a/agents/json-viewer/package.json +++ b/agents/json-viewer/package.json @@ -1,7 +1,6 @@ { "dependencies": { "@inquirer/input": "^4.0.2", - "json-schema-generator": "^2.0.6", - "node-jq": "^6.0.1" + "json-schema-generator": "^2.0.6" } } diff --git a/agents/json-viewer/tools.js b/agents/json-viewer/tools.js index b1f032d..ea673b9 100644 --- a/agents/json-viewer/tools.js +++ b/agents/json-viewer/tools.js @@ -1,10 +1,9 @@ const fs = require("node:fs/promises"); -const { exec } = require("node:child_process"); +const { exec, spawn } = require("node:child_process"); const { promisify } = require("node:util"); const path = require("node:path"); const { tmpdir } = require("node:os"); -const jq = require("node-jq"); const jsonSchemaGenerator = require("json-schema-generator"); const input = require("@inquirer/input").default; @@ -17,7 +16,7 @@ exports._instructions = async function () { json_file_path = value; } catch { generate_json_command_context = `command_to_generate_json: \`${value}\`\n`; - const { stdout } = await promisify(exec)(value, { maxBuffer: 100 * 1024 * 1024}); + const { stdout } = await promisify(exec)(value, { maxBuffer: 100 * 1024 * 1024 }); json_file_path = path.join(tmpdir(), `${process.env.LLM_AGENT_NAME}-${process.pid}.data.json`); await fs.writeFile(json_file_path, stdout); console.log(`ⓘ Generated json data saved to: ${json_file_path}`); @@ -44,6 +43,19 @@ json_schema: ${JSON.stringify(json_schema, null, 2)} */ exports.print_json = async function (args) { const { json_file_path, jq_expr } = args; - const result = await jq.run(jq_expr, json_file_path, { raw: true }); - console.log(result); + return new Promise((resolve, reject) => { + const child = spawn("jq", ["-r", jq_expr, json_file_path], { stdio: "inherit" }); + + child.on('close', code => { + if (code === 0) { + resolve(); + } else { + reject(new Error(`jq exited with code ${code}`)); + } + }); + + child.on('error', err => { + reject(err); + }); + }); } |
