mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-22 18:24:21 +01:00
show combined output of bash tool progressively
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { exec } from "child_process"
|
import { exec } from "child_process"
|
||||||
import { text } from "stream/consumers"
|
|
||||||
import { Tool } from "./tool"
|
import { Tool } from "./tool"
|
||||||
import DESCRIPTION from "./bash.txt"
|
import DESCRIPTION from "./bash.txt"
|
||||||
import { App } from "../app/app"
|
import { App } from "../app/app"
|
||||||
@@ -130,8 +130,35 @@ export const BashTool = Tool.define("bash", {
|
|||||||
timeout,
|
timeout,
|
||||||
})
|
})
|
||||||
|
|
||||||
const stdoutPromise = text(process.stdout!)
|
let output = ""
|
||||||
const stderrPromise = text(process.stderr!)
|
|
||||||
|
// 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) => {
|
await new Promise<void>((resolve) => {
|
||||||
process.on("close", () => {
|
process.on("close", () => {
|
||||||
@@ -139,18 +166,22 @@ export const BashTool = Tool.define("bash", {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const stdout = await stdoutPromise
|
ctx.metadata({
|
||||||
const stderr = await stderrPromise
|
metadata: {
|
||||||
|
output: output,
|
||||||
|
exit: process.exitCode,
|
||||||
|
description: params.description,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title: params.command,
|
title: params.command,
|
||||||
metadata: {
|
metadata: {
|
||||||
stderr,
|
output,
|
||||||
stdout,
|
|
||||||
exit: process.exitCode,
|
exit: process.exitCode,
|
||||||
description: params.description,
|
description: params.description,
|
||||||
},
|
},
|
||||||
output: [`<stdout>`, stdout ?? "", `</stdout>`, `<stderr>`, stderr ?? "", `</stderr>`].join("\n"),
|
output,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ describe("tool.bash", () => {
|
|||||||
ctx,
|
ctx,
|
||||||
)
|
)
|
||||||
expect(result.metadata.exit).toBe(0)
|
expect(result.metadata.exit).toBe(0)
|
||||||
expect(result.metadata.stdout).toContain("test")
|
expect(result.metadata.output).toContain("test")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -569,13 +569,9 @@ func renderToolDetails(
|
|||||||
case "bash":
|
case "bash":
|
||||||
command := toolInputMap["command"].(string)
|
command := toolInputMap["command"].(string)
|
||||||
body = fmt.Sprintf("```console\n$ %s\n", command)
|
body = fmt.Sprintf("```console\n$ %s\n", command)
|
||||||
stdout := metadata["stdout"]
|
output := metadata["output"]
|
||||||
if stdout != nil {
|
if output != nil {
|
||||||
body += ansi.Strip(fmt.Sprintf("%s", stdout))
|
body += ansi.Strip(fmt.Sprintf("%s", output))
|
||||||
}
|
|
||||||
stderr := metadata["stderr"]
|
|
||||||
if stderr != nil {
|
|
||||||
body += ansi.Strip(fmt.Sprintf("%s", stderr))
|
|
||||||
}
|
}
|
||||||
body += "```"
|
body += "```"
|
||||||
body = util.ToMarkdown(body, width, backgroundColor)
|
body = util.ToMarkdown(body, width, backgroundColor)
|
||||||
|
|||||||
@@ -605,7 +605,7 @@ export function BashTool(props: ToolProps) {
|
|||||||
return (
|
return (
|
||||||
<ContentBash
|
<ContentBash
|
||||||
command={props.state.input.command}
|
command={props.state.input.command}
|
||||||
output={props.state.metadata?.stdout || ""}
|
output={props.state.metadata.output ?? props.state.metadata?.stdout}
|
||||||
description={props.state.metadata.description}
|
description={props.state.metadata.description}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user