diffs: harden PDF rendering and remove reserved format schema key

This commit is contained in:
Gustavo Madeira Santana
2026-03-02 01:52:34 -05:00
parent 3fed54c80c
commit 44bc5964a4
4 changed files with 20 additions and 3 deletions

View File

@@ -137,6 +137,9 @@ describe("PlaywrightDiffScreenshotter", () => {
expect(launchMock).toHaveBeenCalledTimes(1);
expect(pages).toHaveLength(1);
expect(pages[0]?.pdf).toHaveBeenCalledTimes(1);
const pdfCall = pages[0]?.pdf.mock.calls[0]?.[0] as Record<string, unknown> | undefined;
expect(pdfCall).toBeDefined();
expect(pdfCall).not.toHaveProperty("pageRanges");
expect(pages[0]?.screenshot).toHaveBeenCalledTimes(0);
await expect(fs.readFile(pdfPath, "utf8")).resolves.toContain("%PDF-1.7");
});

View File

@@ -189,7 +189,6 @@ export class PlaywrightDiffScreenshotter implements DiffScreenshotter {
bottom: "0",
left: "0",
},
pageRanges: "1",
});
return params.outputPath;
}

View File

@@ -39,6 +39,18 @@ describe("diffs tool", () => {
expect((result?.details as Record<string, unknown>).viewerUrl).toBeDefined();
});
it("does not expose reserved format in the tool schema", async () => {
const tool = createDiffsTool({
api: createApi(),
store,
defaults: DEFAULT_DIFFS_TOOL_DEFAULTS,
});
const parameters = tool.parameters as { properties?: Record<string, unknown> };
expect(parameters.properties).toBeDefined();
expect(parameters.properties).not.toHaveProperty("format");
});
it("returns an image artifact in image mode", async () => {
const cleanupSpy = vi.spyOn(store, "scheduleCleanup");
const screenshotter = {

View File

@@ -89,7 +89,6 @@ const DiffsToolSchema = Type.Object(
imageQuality: Type.Optional(
stringEnum(DIFF_IMAGE_QUALITY_PRESETS, "Deprecated alias for fileQuality."),
),
format: Type.Optional(stringEnum(DIFF_OUTPUT_FORMATS, "Deprecated alias for fileFormat.")),
imageFormat: Type.Optional(stringEnum(DIFF_OUTPUT_FORMATS, "Deprecated alias for fileFormat.")),
imageScale: Type.Optional(
Type.Number({
@@ -126,6 +125,10 @@ const DiffsToolSchema = Type.Object(
);
type DiffsToolParams = Static<typeof DiffsToolSchema>;
type DiffsToolRawParams = DiffsToolParams & {
// Keep backward compatibility for direct calls that still pass `format`.
format?: DiffOutputFormat;
};
export function createDiffsTool(params: {
api: OpenClawPluginApi;
@@ -140,7 +143,7 @@ export function createDiffsTool(params: {
"Create a read-only diff viewer from before/after text or a unified patch. Returns a gateway viewer URL for canvas use and can also render the same diff to a PNG or PDF.",
parameters: DiffsToolSchema,
execute: async (_toolCallId, rawParams) => {
const toolParams = rawParams as DiffsToolParams;
const toolParams = rawParams as DiffsToolRawParams;
const input = normalizeDiffInput(toolParams);
const mode = normalizeMode(toolParams.mode, params.defaults.mode);
const theme = normalizeTheme(toolParams.theme, params.defaults.theme);