UNPKG

openai-code

Version:

An unofficial proxy layer that lets you use Anthropic Claude Code with any OpenAI API backend.

61 lines (53 loc) 2.3 kB
// if Claude Code / OpenAI model is a bit stupid again and attempts to modify a file before read, // we adjust it into a view first operation to prevent an error. We also instruct the model to try again. // unused atm export const viewFirstAgent = async({ useTools, formattedMessages, toolCallHistory }) => { // iterate over the useTools array to find Edit or Replace tool calls for (let i = 0; i < useTools.length; i++) { const tool = useTools[i]; if ((tool.name === "Edit" || tool.name === "Replace") && !useTools.some(t => t.name === "View")) { // check the last two tool calls in toolCallHistory for a View tool with the same file_path const toolCallEntries = Array.from(toolCallHistory.entries()); const lastTwoToolCalls = toolCallEntries.slice(-2); const hasRecentViewToolCall = lastTwoToolCalls.some(([_, record]) => { return record.payload.name === "View" && record.payload.input.file_path === tool.parameters.file_path; }); if (hasRecentViewToolCall) { console.log('ViewFirstAgent: found Edit or Replace tool call with a recent View tool call. Skipping optimization.'); return { useTools, formattedMessages, reReason: false }; } console.log('ViewFirstAgent: found Edit or Replace tool call without a previous View tool call. Replacing with a View tool call.'); // replace the Edit or Replace tool call with a View tool call useTools[i] = { name: "View", parameters: { file_path: tool.parameters.file_path } }; // add a formatted message explaining the optimization formattedMessages.push({ role: "user", content: [{ type: 'text', text: `The operation "${tool.name}" was replaced by a "View" tool call to read the file before performing the operation. You MUST remember your previous planned action. And execute this afterwards (e.g. adapting to the latest changes!): ${JSON.stringify(tool)}` }] }); // immediately return to prevent further processing return { useTools, formattedMessages, reReason: false }; } } return { useTools, formattedMessages, reReason: false }; }