diff --git a/packages/core/src/codemode/tool.ts b/packages/core/src/codemode/tool.ts index c8ba658ecd0..8c757a7c360 100644 --- a/packages/core/src/codemode/tool.ts +++ b/packages/core/src/codemode/tool.ts @@ -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. diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index 2dcfa983d8b..05023200b08 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -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( diff --git a/packages/util/src/execute-output.test.ts b/packages/util/src/execute-output.test.ts index e9843e6235e..0b82aa35924 100644 --- a/packages/util/src/execute-output.test.ts +++ b/packages/util/src/execute-output.test.ts @@ -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", + ) + }) })