mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(codemode): drop the null result line before logs (#64344)
This commit is contained in:
committed by
GitHub
Unverified
parent
a0777f3182
commit
d54fe2628d
@@ -434,7 +434,9 @@ function formatResult(result: CodeMode.Result) {
|
||||
? `Warnings:\n${result.warnings.map((item) => `- [${item.kind}] ${item.message}`).join("\n")}`
|
||||
: undefined
|
||||
const logs = result.logs && result.logs.length > 0 ? `Logs:\n${result.logs.join("\n")}` : undefined
|
||||
return [output, warnings, logs].filter((part) => part !== undefined && part !== "").join("\n\n")
|
||||
// A program that only logs returns undefined (sent as null); a "null" line before its logs says nothing.
|
||||
const value = result.ok && result.value === null && (warnings || logs) ? undefined : output
|
||||
return [value, warnings, logs].filter((part) => part !== undefined && part !== "").join("\n\n")
|
||||
}
|
||||
|
||||
// Compact JSON: indentation adds tokens on every later step without helping the model. Clients pretty-print it.
|
||||
|
||||
@@ -152,6 +152,29 @@ test("execute rejects filesystem fetches", async () => {
|
||||
expect(result.output.output).toContain("fetch cannot be used for filesystem access.")
|
||||
})
|
||||
|
||||
describe("execute output", () => {
|
||||
const run = async (code: string) =>
|
||||
(await Effect.runPromise(createCodeMode(new Map()).execute({ code }, context))).output.output
|
||||
|
||||
test("a program that only logs shows just its logs", async () => {
|
||||
expect(await run('console.log("found", 12, "files")')).toBe("Logs:\nfound 12 files")
|
||||
})
|
||||
|
||||
test("a program that only warns shows just its warnings", async () => {
|
||||
expect(await run('const rejected = Promise.reject(new Error("boom"))')).toBe(
|
||||
"Warnings:\n- [ExecutionFailure] Unhandled rejection from an un-awaited promise: Error: boom",
|
||||
)
|
||||
})
|
||||
|
||||
test("a null result with nothing else still shows null", async () => {
|
||||
expect(await run("return null")).toBe("null")
|
||||
})
|
||||
|
||||
test("a returned value keeps its line before the logs", async () => {
|
||||
expect(await run('console.log("hi"); return { n: 1 }')).toBe('{"n":1}\n\nLogs:\nhi')
|
||||
})
|
||||
})
|
||||
|
||||
describe("streaming fetch", () => {
|
||||
const run = (url: URL, code: string, permission = allowAll) =>
|
||||
createCodeMode(new Map(), permission).execute(
|
||||
|
||||
@@ -26,4 +26,11 @@ describe("execute output", () => {
|
||||
})
|
||||
expect(formatExecuteOutput("{not json")).toEqual({ json: undefined, rest: undefined, text: "{not json" })
|
||||
})
|
||||
|
||||
test("keeps output without a result line as it is", () => {
|
||||
expect(formatExecuteOutput("Logs:\nhi").text).toBe("Logs:\nhi")
|
||||
expect(formatExecuteOutput("Warnings:\n- [unhandled] boom\n\nLogs:\nhi").text).toBe(
|
||||
"Warnings:\n- [unhandled] boom\n\nLogs:\nhi",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user