UNPKG

hataraku

Version:

An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.

173 lines 7.13 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.applyDiffTool = void 0; const zod_1 = require("zod"); const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); // Helper function to apply a diff block to content function applyDiff(originalContent, diffContent, startLine, endLine) { try { // Parse the diff content to extract search and replace blocks const searchMatch = diffContent.match(/<<<<<<< SEARCH\n([\s\S]*?)\n=======\n([\s\S]*?)\n>>>>>>> REPLACE/); if (!searchMatch) { return { success: false, error: 'Invalid diff format. Expected <<<<<<< SEARCH, =======, and >>>>>>> REPLACE markers.', details: { diffContent } }; } const [, searchBlock, replaceBlock] = searchMatch; const lines = originalContent.split('\n'); // If line range is specified, only apply to that range if (typeof startLine === 'number' && typeof endLine === 'number') { if (startLine < 1 || endLine > lines.length || startLine > endLine) { return { success: false, error: `Invalid line range: ${startLine}-${endLine}. File has ${lines.length} lines.`, details: { startLine, endLine, fileLength: lines.length } }; } // Extract the target section const beforeSection = lines.slice(0, startLine - 1); const targetSection = lines.slice(startLine - 1, endLine).join('\n'); const afterSection = lines.slice(endLine); // Check if the search block matches if (targetSection.trim() !== searchBlock.trim()) { return { success: false, error: 'Search content does not match the specified lines in the file.', details: { expected: searchBlock.trim(), found: targetSection.trim(), lineRange: `${startLine}-${endLine}` } }; } // Apply the replacement const modifiedContent = [ ...beforeSection, ...replaceBlock.split('\n'), ...afterSection ].join('\n'); return { success: true, content: modifiedContent }; } // If no line range, search the entire content const fullContent = lines.join('\n'); if (!fullContent.includes(searchBlock.trim())) { return { success: false, error: 'Search content not found in file.', details: { searchContent: searchBlock.trim() } }; } // Apply the replacement to the full content const modifiedContent = fullContent.replace(searchBlock, replaceBlock); return { success: true, content: modifiedContent }; } catch (error) { return { success: false, error: `Error applying diff: ${error instanceof Error ? error.message : String(error)}`, details: { error } }; } } exports.applyDiffTool = { description: "Apply a diff to a file, replacing specific content with new content. The diff should be in a block format with SEARCH and REPLACE sections.", parameters: zod_1.z.object({ path: zod_1.z.string().describe('The path of the file to modify'), diff: zod_1.z.string().describe('The diff content in block format with SEARCH and REPLACE sections'), start_line: zod_1.z.number().optional().describe('Starting line number for the replacement (1-based)'), end_line: zod_1.z.number().optional().describe('Ending line number for the replacement (1-based)') }), execute: async ({ path: filePath, diff, start_line, end_line }) => { try { const absolutePath = path.resolve(process.cwd(), filePath); // Check if file exists try { await fs.access(absolutePath); } catch { return { isError: true, content: [{ type: "text", text: `File not found at path: ${filePath}` }] }; } // Read the original content const originalContent = await fs.readFile(absolutePath, 'utf-8'); // Apply the diff const result = applyDiff(originalContent, diff, start_line, end_line); if (!result.success) { return { isError: true, content: [{ type: "text", text: `Failed to apply diff: ${result.error}` }] }; } // Write the modified content back to the file await fs.writeFile(absolutePath, result.content, 'utf-8'); return { content: [{ type: "text", text: `Successfully applied diff to ${filePath}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error applying diff: ${error instanceof Error ? error.message : String(error)}` }] }; } } }; //# sourceMappingURL=apply-diff.js.map