show combined output of bash tool progressively
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { z } from "zod"
|
||||
import { exec } from "child_process"
|
||||
import { text } from "stream/consumers"
|
||||
|
||||
import { Tool } from "./tool"
|
||||
import DESCRIPTION from "./bash.txt"
|
||||
import { App } from "../app/app"
|
||||
@@ -130,8 +130,35 @@ export const BashTool = Tool.define("bash", {
|
||||
timeout,
|
||||
})
|
||||
|
||||
const stdoutPromise = text(process.stdout!)
|
||||
const stderrPromise = text(process.stderr!)
|
||||
let output = ""
|
||||
|
||||
// Initialize metadata with empty output
|
||||
ctx.metadata({
|
||||
metadata: {
|
||||
output: "",
|
||||
description: params.description,
|
||||
},
|
||||
})
|
||||
|
||||
process.stdout?.on("data", (chunk) => {
|
||||
output += chunk.toString()
|
||||
ctx.metadata({
|
||||
metadata: {
|
||||
output: output,
|
||||
description: params.description,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
process.stderr?.on("data", (chunk) => {
|
||||
output += chunk.toString()
|
||||
ctx.metadata({
|
||||
metadata: {
|
||||
output: output,
|
||||
description: params.description,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
process.on("close", () => {
|
||||
@@ -139,18 +166,22 @@ export const BashTool = Tool.define("bash", {
|
||||
})
|
||||
})
|
||||
|
||||
const stdout = await stdoutPromise
|
||||
const stderr = await stderrPromise
|
||||
ctx.metadata({
|
||||
metadata: {
|
||||
output: output,
|
||||
exit: process.exitCode,
|
||||
description: params.description,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
title: params.command,
|
||||
metadata: {
|
||||
stderr,
|
||||
stdout,
|
||||
output,
|
||||
exit: process.exitCode,
|
||||
description: params.description,
|
||||
},
|
||||
output: [`<stdout>`, stdout ?? "", `</stdout>`, `<stderr>`, stderr ?? "", `</stderr>`].join("\n"),
|
||||
output,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
@@ -27,7 +27,7 @@ describe("tool.bash", () => {
|
||||
ctx,
|
||||
)
|
||||
expect(result.metadata.exit).toBe(0)
|
||||
expect(result.metadata.stdout).toContain("test")
|
||||
expect(result.metadata.output).toContain("test")
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user