Files
llmx/codex-cli/src/utils/extract-applied-patches.ts
Fouad Matin b3b195351e feat: /diff command to view git diff (#426)
Adds `/diff` command to view git diff
2025-04-19 16:23:27 -07:00

37 lines
998 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
/**
* Extracts the patch texts of all `apply_patch` tool calls from the given
* message history. Returns an empty string when none are found.
*/
export function extractAppliedPatches(items: Array<ResponseItem>): string {
const patches: Array<string> = [];
for (const item of items) {
if (item.type !== "function_call") {
continue;
}
const { name: toolName, arguments: argsString } = item as unknown as {
name: unknown;
arguments: unknown;
};
if (toolName !== "apply_patch" || typeof argsString !== "string") {
continue;
}
try {
const args = JSON.parse(argsString) as { patch?: string };
if (typeof args.patch === "string" && args.patch.length > 0) {
patches.push(args.patch.trim());
}
} catch {
// Ignore malformed JSON we never want to crash the overlay.
continue;
}
}
return patches.join("\n\n");
}