Parse Claude JSON output to return text replies

This commit is contained in:
Peter Steinberger
2025-11-25 01:41:52 +01:00
parent 134edc5e49
commit ba3b271c39
2 changed files with 103 additions and 3 deletions

View File

@@ -172,13 +172,43 @@ describe("config and templating", () => {
const argv = runSpy.mock.calls[0][0];
expect(argv[0]).toBe("claude");
expect(argv.at(-1)).toBe("hi");
// The helper should auto-add print and output format flags without disturbing the prompt position.
expect(argv.includes("-p") || argv.includes("--print")).toBe(true);
const outputIdx = argv.findIndex(
(part) => part === "--output-format" || part.startsWith("--output-format="),
(part) =>
part === "--output-format" || part.startsWith("--output-format="),
);
expect(outputIdx).toBeGreaterThan(-1);
expect(argv[outputIdx + 1]).toBe("text");
});
it("parses Claude JSON output and returns text content", async () => {
const runSpy = vi.spyOn(index, "runCommandWithTimeout").mockResolvedValue({
stdout: '{"text":"hello world"}\n',
stderr: "",
code: 0,
signal: null,
killed: false,
});
const cfg = {
inbound: {
reply: {
mode: "command" as const,
command: ["claude", "{{Body}}"],
claudeOutputFormat: "json" as const,
},
},
};
const result = await index.getReplyFromConfig(
{ Body: "hi", From: "+1", To: "+2" },
undefined,
cfg,
runSpy,
);
expect(result).toBe("hello world");
});
});
describe("twilio interactions", () => {